From 51ed40fcb03e1d5e6a164bbe952cc8a8f86418fd Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Fri, 30 Jan 2015 20:19:01 +0100 Subject: [PATCH] Fix 'dbus-daemon on Windows: Failed to bind socket "localhost:xxxx": Address already in use'. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=87999 Reviewed-by: Simon McVittie --- dbus/dbus-sysdeps-win.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/dbus/dbus-sysdeps-win.c b/dbus/dbus-sysdeps-win.c index 26cab69..68fa72b 100644 --- a/dbus/dbus-sysdeps-win.c +++ b/dbus/dbus-sysdeps-win.c @@ -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); -- 1.8.4.5