]> The Tcpdump Group git mirrors - libpcap/blobdiff - sockutils.c
Merge pull request #771 from ether42/reuseaddr
[libpcap] / sockutils.c
index b08e835c2e08dafc0ba78bc2696bb6cbc3355d9e..3d71cd311cdc754068ce1245e2930e7cd4eb86f6 100644 (file)
@@ -340,6 +340,18 @@ SOCKET sock_open(struct addrinfo *addrinfo, int server, int nconn, char *errbuf,
        /* This is a server socket */
        if (server)
        {
+               /*
+                * Allow a new server to bind the socket after the old one exited,
+                * even if lingering sockets are still present.
+                */
+               int optval = 1;
+               if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0)
+               {
+                       sock_geterror("socket(): ", errbuf, errbuflen);
+                       /* dont treat an error as a failure */
+                       SOCK_DEBUG_MESSAGE(errbuf);
+               }
+
 #if defined(IPV6_V6ONLY) || defined(IPV6_BINDV6ONLY)
                /*
                 * Force the use of IPv6-only addresses.