]> git.pond.sub.org Git - empserver/blobdiff - src/lib/gen/tcp_listen.c
Fix wildcard bind to bind both IPv6 and IPv4 on Windows & BSD
[empserver] / src / lib / gen / tcp_listen.c
index ce7f12819a738666fe76cd2199d745e913f84390..f2ec86d090ed692f70f3ed666036eae1d09ad1d9 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
  *  ---
  *
  *  tcp_listen.c: Create a socket and listen on it
- * 
+ *
  *  Known contributors to this file:
- *     Markus Armbruster, 2005
+ *     Markus Armbruster, 2005-2010
  */
 
 #include <config.h>
 
+#include <assert.h>
 #include <ctype.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#ifdef _WIN32
-#define WIN32
-#include "winsock2.h"
-#undef NS_ALL
-#else
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <netdb.h>
 #include <unistd.h>
-#endif
 #include "prototypes.h"
 
-static int cant_listen(char *, char *, const char *);
+/* Portability cruft, should become unnecessary eventually */
+#ifndef AI_ADDRCONFIG
+#define AI_ADDRCONFIG 0
+#endif
+
+static void cant_listen(char *, char *, const char *)
+    ATTRIBUTE((noreturn));
 
 int
 tcp_listen(char *host, char *serv, size_t *addrlenp)
@@ -64,52 +64,48 @@ tcp_listen(char *host, char *serv, size_t *addrlenp)
      * Inspired by example code from W. Richard Stevens: UNIX Network
      * Programming, Vol. 1
      */
-    int n;
-    struct addrinfo hints, *res, *ressave;
+    int err;
+    struct addrinfo hints, *first_ai, *ai;
 
     memset(&hints, 0, sizeof(struct addrinfo));
-    hints.ai_flags = AI_PASSIVE;
+    hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
     hints.ai_family = AF_UNSPEC;
     hints.ai_socktype = SOCK_STREAM;
 
-    if ((n = getaddrinfo(host, serv, &hints, &res)) != 0)
-       cant_listen(host, serv, gai_strerror(n));
-    ressave = res;
+    if ((err = getaddrinfo(host, serv, &hints, &first_ai)) != 0)
+       cant_listen(host, serv, gai_strerror(err));
+    assert(first_ai);
 
-    do {
-       fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
+    for (ai = first_ai; ai; ai = ai->ai_next) {
+       fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
        if (fd < 0)
            continue;           /* error, try next one */
 
-#ifndef _WIN32
-       /*
-        * SO_REUSEADDR requests to permit another bind even when the
-        * port is still in state TIME_WAIT.  Windows' SO_REUSEADDR is
-        * broken: it makes bind() succeed no matter what, even if
-        * there's another server running on the same port.  Luckily,
-        * bind() seems to be broken as well: it seems to suceed while
-        * the port in state TIME_WAIT by default; thus we get the
-        * behavior we want by not setting SO_REUSEADDR.
-        */
-       if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
-           cant_listen(host, serv, strerror(errno));
+#ifdef IPV6_V6ONLY
+       if (ai->ai_family == AF_INET6) {
+           int off = 0;
+
+           setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(off));
+       }
 #endif
-       if (bind(fd, res->ai_addr, res->ai_addrlen) == 0)
+
+       setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
+       if (bind(fd, ai->ai_addr, ai->ai_addrlen) == 0)
            break;              /* success */
 
        close(fd);              /* error, close and try next one */
-    } while ((res = res->ai_next) != NULL);
+    }
 
-    if (res == NULL)        /* errno from final socket() or bind() */
+    if (ai == NULL)         /* errno from final socket() or bind() */
        cant_listen(host, serv, strerror(errno));
 
     if (listen(fd, SOMAXCONN) < 0)
        cant_listen(host, serv, strerror(errno));
 
     if (addrlenp)
-       *addrlenp = res->ai_addrlen;
+       *addrlenp = ai->ai_addrlen;
 
-    freeaddrinfo(ressave);
+    freeaddrinfo(first_ai);
 
 #else  /* !HAVE_GETADDRINFO */
     struct sockaddr_in sin;
@@ -138,11 +134,7 @@ tcp_listen(char *host, char *serv, size_t *addrlenp)
     }
     if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        cant_listen(host, serv, strerror(errno));
-#ifndef _WIN32
-    /* see comment on setsockopt() above */
-    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
-       cant_listen(host, serv, strerror(errno));
-#endif
+    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
     if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
        cant_listen(host, serv, strerror(errno));
     if (listen(fd, SOMAXCONN) < 0)
@@ -156,8 +148,7 @@ tcp_listen(char *host, char *serv, size_t *addrlenp)
     return fd;
 }
 
-
-static int
+static void
 cant_listen(char *host, char *serv, const char *err)
 {
     fprintf(stderr, "Can't listen on %s%s%s: %s\n",