From c7152af1917c1388fe3a944e7481a271cce797ba Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 26 Aug 2011 11:27:50 +0100 Subject: [PATCH] Further tweaks to only consider IP4 addresses for the moment, when using getaddrinfo. --- simgear/io/raw_socket.cxx | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/simgear/io/raw_socket.cxx b/simgear/io/raw_socket.cxx index c0716580..d972c145 100644 --- a/simgear/io/raw_socket.cxx +++ b/simgear/io/raw_socket.cxx @@ -119,19 +119,29 @@ void IPAddress::set ( const char* host, int port ) memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_INET; - struct addrinfo* result = NULL; - int err = getaddrinfo(host, NULL, NULL /* no hints */, &result); + struct addrinfo* result0 = NULL; + int err = getaddrinfo(host, NULL, &hints, &result0); if (err) { SG_LOG(SG_IO, SG_WARN, "getaddrinfo failed for '" << host << "' : " << gai_strerror(err)); - } else if (result->ai_addrlen != getAddrLen()) { - SG_LOG(SG_IO, SG_ALERT, "mismatch in socket address sizes: got " << - result->ai_addrlen << ", expected " << getAddrLen()); - SG_LOG(SG_IO, SG_ALERT, "family:" << result->ai_family); } else { - memcpy(addr, result->ai_addr, result->ai_addrlen); - } - - freeaddrinfo(result); + struct addrinfo* result; + for (result = result0; result != NULL; result = result->ai_next) { + if (result->ai_family != AF_INET) { // only accept IP4 for the moment + continue; + } + + if (result->ai_addrlen != getAddrLen()) { + SG_LOG(SG_IO, SG_ALERT, "mismatch in socket address sizes: got " << + result->ai_addrlen << ", expected " << getAddrLen()); + continue; + } + + memcpy(addr, result->ai_addr, result->ai_addrlen); + break; + } // of getaddrinfo results iteration + } // of getaddrinfo succeeded + + freeaddrinfo(result0); addr->sin_port = htons (port); // fix up port after getaddrinfo } -- 2.39.5