[libvirt] [PATCH] Don't use SO_REUSEADDR on Win32 platforms

Daniel P. Berrange berrange at redhat.com
Fri Apr 25 16:47:46 UTC 2014


SO_REUSEADDR on Windows is actually akin to SO_REUSEPORT
on Linux/BSD. ie it allows 2 apps to listen to the same
port at once. Thus we must not set it on Win32 platforms

See http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx

Signed-off-by: Daniel P. Berrange <berrange at redhat.com>
---
 src/rpc/virnetsocket.c      | 16 ++++++++++++++++
 src/util/virportallocator.c |  9 +++++++++
 2 files changed, 25 insertions(+)

diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c
index 2e94a6c..65efdb6 100644
--- a/src/rpc/virnetsocket.c
+++ b/src/rpc/virnetsocket.c
@@ -255,11 +255,18 @@ int virNetSocketNewListenTCP(const char *nodename,
             goto error;
         }
 
+#ifndef WIN32
+        /*
+         * SO_REUSEADDR on Windows is actually akin to SO_REUSEPORT
+         * on Linux/BSD. ie it allows 2 apps to listen to the same
+         * port at once. Thus we must not set it on Win32 platforms
+         */
         int opt = 1;
         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
             virReportSystemError(errno, "%s", _("Unable to enable port reuse"));
             goto error;
         }
+#endif
 
 #ifdef IPV6_V6ONLY
         if (runp->ai_family == PF_INET6) {
@@ -460,7 +467,9 @@ int virNetSocketNewConnectTCP(const char *nodename,
 
     runp = ai;
     while (runp) {
+#ifndef WIN32
         int opt = 1;
+#endif
 
         if ((fd = socket(runp->ai_family, runp->ai_socktype,
                          runp->ai_protocol)) < 0) {
@@ -468,9 +477,16 @@ int virNetSocketNewConnectTCP(const char *nodename,
             goto error;
         }
 
+#ifndef WIN32
+        /*
+         * SO_REUSEADDR on Windows is actually akin to SO_REUSEPORT
+         * on Linux/BSD. ie it allows 2 apps to listen to the same
+         * port at once. Thus we must not set it on Win32 platforms
+         */
         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
             VIR_WARN("Unable to enable port reuse");
         }
+#endif
 
         if (connect(fd, runp->ai_addr, runp->ai_addrlen) >= 0)
             break;
diff --git a/src/util/virportallocator.c b/src/util/virportallocator.c
index ed7bdc2..0f22ea1 100644
--- a/src/util/virportallocator.c
+++ b/src/util/virportallocator.c
@@ -116,7 +116,9 @@ static int virPortAllocatorBindToPort(bool *used,
     struct sockaddr* addr;
     size_t addrlen;
     int v6only = 1;
+#ifndef WIN32
     int reuse = 1;
+#endif
     int ret = -1;
     int fd = -1;
     bool ipv6 = false;
@@ -143,12 +145,19 @@ static int virPortAllocatorBindToPort(bool *used,
         goto cleanup;
     }
 
+#ifndef WIN32
+        /*
+         * SO_REUSEADDR on Windows is actually akin to SO_REUSEPORT
+         * on Linux/BSD. ie it allows 2 apps to listen to the same
+         * port at once. Thus we must not set it on Win32 platforms
+         */
     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*)&reuse,
                    sizeof(reuse)) < 0) {
         virReportSystemError(errno, "%s",
                              _("Unable to set socket reuse addr flag"));
         goto cleanup;
     }
+#endif
 
     if (ipv6 && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&v6only,
                            sizeof(v6only)) < 0) {
-- 
1.9.0




More information about the libvir-list mailing list