From eff58367b486ca94ec596262ed82b7531d9dc295 Mon Sep 17 00:00:00 2001 From: Kfir Itzhak Date: Fri, 22 Nov 2013 15:30:05 +0200 Subject: [PATCH] IPv6 improvements --- src/zm_remote_camera.cpp | 28 ++++++++++++---------------- src/zm_remote_camera.h | 4 +--- src/zm_remote_camera_http.cpp | 29 ++++++++++++++++++++--------- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/zm_remote_camera.cpp b/src/zm_remote_camera.cpp index 3b63cdbd8..4ede09e69 100644 --- a/src/zm_remote_camera.cpp +++ b/src/zm_remote_camera.cpp @@ -35,6 +35,10 @@ RemoteCamera::RemoteCamera( int p_id, const std::string &p_protocol, const std:: RemoteCamera::~RemoteCamera() { + if(hp != NULL) { + freeaddrinfo(hp); + hp = NULL; + } } void RemoteCamera::Initialise() @@ -61,23 +65,15 @@ void RemoteCamera::Initialise() auth64 = base64Encode( auth ); } - if ( !hp ) - { - struct addrinfo hints; - memset(&hints, 0, sizeof(hints)); - hints.ai_family = PF_UNSPEC; - hints.ai_flags = AI_PASSIVE; - hints.ai_socktype = SOCK_STREAM; + struct addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; - if ( getaddrinfo(host.c_str(), NULL, &hints, &hp) != 0 ) - { - Fatal( "Can't gethostbyname(%s): %s", host.c_str(), strerror(h_errno) ); - } - //memcpy((char *)&sa.sin_addr, (char *)hp->h_addr, hp->h_length); - //sa.sin_family = hp->h_addrtype; - //sa.sin_port = htons(atoi(port.c_str())); - sa = hp->ai_addr; - ((struct sockaddr_in *)sa)->sin_port = htons(atoi(port.c_str())); + int ret = getaddrinfo(host.c_str(), port.c_str(), &hints, &hp); + if ( ret != 0 ) + { + Fatal( "Can't getaddrinfo(%s port %s): %s", host.c_str(), port.c_str(), gai_strerror(ret) ); } } diff --git a/src/zm_remote_camera.h b/src/zm_remote_camera.h index 19cffdcf0..2738e4a5d 100644 --- a/src/zm_remote_camera.h +++ b/src/zm_remote_camera.h @@ -23,7 +23,6 @@ #include "zm_camera.h" #include -//#include #include #include #include @@ -43,8 +42,7 @@ protected: std::string auth64; protected: - struct addrinfo *hp; - struct sockaddr *sa; + struct addrinfo *hp; public: RemoteCamera( int p_id, const std::string &p_proto, const std::string &p_host, const std::string &p_port, const std::string &p_path, int p_width, int p_height, int p_colours, int p_brightness, int p_contrast, int p_hue, int p_colour, bool p_capture ); diff --git a/src/zm_remote_camera_http.cpp b/src/zm_remote_camera_http.cpp index bf50432f0..deae375db 100644 --- a/src/zm_remote_camera_http.cpp +++ b/src/zm_remote_camera_http.cpp @@ -88,22 +88,34 @@ void RemoteCameraHttp::Initialise() int RemoteCameraHttp::Connect() { - if ( sd < 0 ) + struct addrinfo *p; + + for(p = hp; p != NULL; p = p->ai_next) { - sd = socket( hp->ai_family, SOCK_STREAM, 0 ); + sd = socket( p->ai_family, p->ai_socktype, p->ai_protocol ); if ( sd < 0 ) { - Error( "Can't create socket: %s", strerror(errno) ); - return( -1 ); + Warning("Can't create socket: %s", strerror(errno) ); + continue; } - if ( connect( sd, (struct sockaddr *)sa, hp->ai_addrlen ) < 0 ) + if ( connect( sd, p->ai_addr, p->ai_addrlen ) < 0 ) { - Error( "Can't connect to remote camera: %s", strerror(errno) ); - Disconnect(); - return( -1 ); + close(sd); + sd = -1; + Warning("Can't connect to remote camera: %s", strerror(errno) ); + continue; } + + /* If we got here, we must have connected successfully */ + break; } + + if(p == NULL) { + Error("Unable to connect to the remote camera, aborting"); + return( -1 ); + } + Debug( 3, "Connected to host, socket = %d", sd ); return( sd ); } @@ -112,7 +124,6 @@ int RemoteCameraHttp::Disconnect() { close( sd ); sd = -1; - freeaddrinfo(hp); Debug( 3, "Disconnected from host" ); return( 0 ); }