Fix 'dbus-daemon on Windows: Failed to bind socket "localhost:xxxx": Address already...
authorRalf Habacker <ralf.habacker@freenet.de>
Fri, 30 Jan 2015 19:19:01 +0000 (20:19 +0100)
committerRalf Habacker <ralf.habacker@freenet.de>
Fri, 30 Jan 2015 21:07:10 +0000 (22:07 +0100)
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=87999
Reviewed-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
dbus/dbus-sysdeps-win.c

index 26cab69..68fa72b 100644 (file)
@@ -1715,10 +1715,19 @@ _dbus_listen_tcp_socket (const char     *host,
       if (bind (fd, (struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen) == SOCKET_ERROR)
         {
           DBUS_SOCKET_SET_ERRNO ();
+          closesocket (fd);
+          if (errno == WSAEADDRINUSE)
+          {
+              /* Calling this function with port=0 tries to
+               * bind the same port twice, so we should
+               * ignore the second bind.
+               */
+              tmp = tmp->ai_next;
+              continue;
+          }
           dbus_set_error (error, _dbus_error_from_errno (errno),
                           "Failed to bind socket \"%s:%s\": %s",
                           host ? host : "*", port, _dbus_strerror_from_errno ());
-          closesocket (fd);
           goto failed;
     }
 
@@ -1764,7 +1773,10 @@ _dbus_listen_tcp_socket (const char     *host,
                                   host ? host : "*", port, _dbus_strerror_from_errno());
                   goto failed;
                 }
-              snprintf( portbuf, sizeof( portbuf ) - 1, "%d", addr.AddressIn.sin_port );
+              if (addr.AddressIn.sin_family = AF_INET)
+                  snprintf( portbuf, sizeof( portbuf ) - 1, "%d", ntohs(addr.AddressIn.sin_port) );
+              else
+                  snprintf( portbuf, sizeof( portbuf ) - 1, "%d", ntohs(addr.AddressIn6.sin6_port) );
               if (!_dbus_string_append(retport, portbuf))
                 {
                   dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);