]> git.pond.sub.org Git - empserver/blobdiff - src/lib/gen/tcp_listen.c
Update known contributors comments
[empserver] / src / lib / gen / tcp_listen.c
index 909dec5eaadae18d08a672a2f634388f13919fc1..2743743b8c55fc6a7c367d2d7b3ba5192cd0d8d5 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,9 +26,9 @@
  *  ---
  *
  *  tcp_listen.c: Create a socket and listen on it
- * 
+ *
  *  Known contributors to this file:
- *     Markus Armbruster, 2005
+ *     Markus Armbruster, 2005-2009
  */
 
 #include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#ifdef _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"
 
+/* Portability cruft, should become unnecessary eventually */
+#ifndef AI_ADDRCONFIG
+#define AI_ADDRCONFIG 0
+#endif
+
 static void cant_listen(char *, char *, const char *);
 
 int
@@ -67,7 +67,7 @@ tcp_listen(char *host, char *serv, size_t *addrlenp)
     struct addrinfo hints, *res, *ressave;
 
     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;
 
@@ -80,19 +80,8 @@ tcp_listen(char *host, char *serv, size_t *addrlenp)
        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 succeed while
-        * the port is 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));
-#endif
        if (bind(fd, res->ai_addr, res->ai_addrlen) == 0)
            break;              /* success */
 
@@ -137,11 +126,8 @@ 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
     if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
        cant_listen(host, serv, strerror(errno));
     if (listen(fd, SOMAXCONN) < 0)