]> git.mxchange.org Git - simgear.git/blobdiff - simgear/io/raw_socket.cxx
Further tweaks to only consider IP4 addresses for the moment, when using getaddrinfo.
[simgear.git] / simgear / io / raw_socket.cxx
index 7fe2c6884cb61ca49dd07915a74a7bcce7e93bc2..d972c14531e4f5b292600cf0519bd901818ee0d8 100644 (file)
@@ -39,7 +39,8 @@
 #include <errno.h>
 
 #if defined(WINSOCK)
-#  include <winsock.h>
+#  include <winsock2.h>
+#  include <ws2tcpip.h>
 #  include <stdarg.h>
 #else
 #  include <sys/types.h>
@@ -114,17 +115,33 @@ void IPAddress::set ( const char* host, int port )
     return;
   }
   
-  struct addrinfo* result = NULL;
-  int err = getaddrinfo(host, NULL, NULL /* no hints */, &result);
+  struct addrinfo hints;
+  memset(&hints, 0, sizeof(struct addrinfo));
+  hints.ai_family = AF_INET;
+  
+  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");
   } 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
 }
 
@@ -328,7 +345,7 @@ int Socket::bind ( const char* host, int port )
     }
   }
 #if defined(WINSOCK)
-  else if( (result = ::bind(handle,addr->getAddr(), addr->getAddrLen())) < 0 ) {
+  else if( (result = ::bind(handle,addr.getAddr(), addr.getAddrLen())) < 0 ) {
     SG_LOG(SG_IO, SG_ALERT, "bind(" << host << ":" << port << ") failed. Errno " << errno << " (" << strerror(errno) << ")");
     return result;
   }