]> git.pond.sub.org Git - empserver/blobdiff - src/client/host.c
Use IPv4 and v6 only when suitable interfaces are configured
[empserver] / src / client / host.c
index cb309721c1ea8aeb3cf8c10bf6690cb3a265714b..6687f71ab4aa9c028b295f613de902438f4d6f9a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -26,7 +26,7 @@
  *  ---
  *
  *  host.c: make stream connection to empire
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1989
  *     Steve McClure, 1998
 #include <netdb.h>
 #include <unistd.h>
 #else
-#include <winsock2.h>
-#include <ws2tcpip.h>
+#define close(fd) w32_close_socket((fd))
 #endif
 #include "misc.h"
 
+/* Portability cruft, should become unnecessary eventually */
+#ifndef AI_ADDRCONFIG
+#define AI_ADDRCONFIG 0
+#endif
+
 #ifdef HAVE_GETADDRINFO
 /*
  * Inspired by example code from W. Richard Stevens: UNIX Network
@@ -66,6 +70,7 @@ tcp_connect(char *host, char *serv)
     struct addrinfo hints, *res, *ressave;
 
     memset(&hints, 0, sizeof(struct addrinfo));
+    hints.ai_flags = AI_ADDRCONFIG;
     hints.ai_family = AF_UNSPEC;
     hints.ai_socktype = SOCK_STREAM;
 
@@ -83,11 +88,7 @@ tcp_connect(char *host, char *serv)
 
        if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
            break;              /* success */
-#ifdef _WIN32
-       closesocket(sockfd);    /* ignore this one */
-#else
        close(sockfd);          /* ignore this one */
-#endif
     } while ((res = res->ai_next) != NULL);
 
     if (res == NULL) {         /* errno set from final connect() */
@@ -147,19 +148,11 @@ hostconnect(struct sockaddr_in *addr)
 
     s = socket(AF_INET, SOCK_STREAM, 0);
     if (s < 0) {
-#ifdef _WIN32
-       errno = WSAGetLastError();
-#endif
        return -1;
     }
     addr->sin_family = AF_INET;
     if (connect(s, (struct sockaddr *)addr, sizeof(*addr)) < 0) {
-#ifdef _WIN32
-       errno = WSAGetLastError();
-       (void)closesocket(s);
-#else
        (void)close(s);
-#endif
        return -1;
     }
     return s;