From a3ad03bdb528f401a7ec70cb81eb181ba869172b Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Tue, 13 Mar 2018 16:20:35 +0100 Subject: [PATCH] Add actual used ip family to --print-address output in case of listening on tcp Specifying a dbus tcp address without a family let dbus-daemon the choice for listen on ipv4 and/or ipv6 but it did not return the real used ip family, which is fixed with this commit. This bug has been reported at https://bugs.freedesktop.org/show_bug.cgi?id=61922#c56 Signed-off-by: Ralf Habacker https://bugs.freedesktop.org/show_bug.cgi?id=105489 --- dbus/dbus-server-socket.c | 23 ++++++++++++++++------- dbus/dbus-sysdeps-unix.c | 8 ++++++++ dbus/dbus-sysdeps-win.c | 9 +++++++++ dbus/dbus-sysdeps.h | 1 + 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/dbus/dbus-server-socket.c b/dbus/dbus-server-socket.c index 2da75605..22fad5ef 100644 --- a/dbus/dbus-server-socket.c +++ b/dbus/dbus-server-socket.c @@ -428,6 +428,7 @@ _dbus_server_new_for_tcp_socket (const char *host, DBusString address = _DBUS_STRING_INIT_INVALID; DBusString host_str; /* Initialized as const later, not freed */ DBusString port_str = _DBUS_STRING_INIT_INVALID; + DBusString family_str = _DBUS_STRING_INIT_INVALID; DBusNonceFile *noncefile = NULL; _DBUS_ASSERT_ERROR_IS_CLEAR (error); @@ -444,6 +445,12 @@ _dbus_server_new_for_tcp_socket (const char *host, goto failed; } + if (!_dbus_string_init (&family_str)) + { + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + goto failed; + } + if (host == NULL) host = "localhost"; @@ -457,6 +464,7 @@ _dbus_server_new_for_tcp_socket (const char *host, nlisten_fds =_dbus_listen_tcp_socket (bind, port, family, &port_str, + &family_str, &listen_fds, error); if (nlisten_fds <= 0) { @@ -473,13 +481,12 @@ _dbus_server_new_for_tcp_socket (const char *host, dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto failed; } - if (family && - (!_dbus_string_append (&address, ",family=") || - !_dbus_string_append (&address, family))) - { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); - goto failed; - } + if(!_dbus_string_append (&address, ",family=") || + !_dbus_string_append (&address, _dbus_string_get_const_data(&family_str))) + { + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + goto failed; + } if (use_nonce) { @@ -500,6 +507,7 @@ _dbus_server_new_for_tcp_socket (const char *host, /* server has taken ownership of noncefile and the fds in listen_fds */ _dbus_string_free (&port_str); + _dbus_string_free (&family_str); _dbus_string_free (&address); dbus_free(listen_fds); @@ -516,6 +524,7 @@ failed: } _dbus_string_free (&port_str); + _dbus_string_free (&family_str); _dbus_string_free (&address); return NULL; } diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index 249cf77e..3cf86492 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -1501,6 +1501,7 @@ _dbus_listen_tcp_socket (const char *host, const char *port, const char *family, DBusString *retport, + DBusString *retfamily, DBusSocket **fds_p, DBusError *error) { @@ -1648,6 +1649,13 @@ _dbus_listen_tcp_socket (const char *host, listen_fd[nlisten_fd].fd = fd; nlisten_fd++; + if (!_dbus_string_get_length(retfamily)) + if(!_dbus_string_append(retfamily, tmp->ai_addr->sa_family == AF_INET ? "ipv4" : "ipv6")) + { + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + goto failed; + } + if (!_dbus_string_get_length(retport)) { /* If the user didn't specify a port, or used 0, then diff --git a/dbus/dbus-sysdeps-win.c b/dbus/dbus-sysdeps-win.c index dede8e29..bddfb62f 100644 --- a/dbus/dbus-sysdeps-win.c +++ b/dbus/dbus-sysdeps-win.c @@ -1652,6 +1652,7 @@ out: * @param port the port to listen on, if zero a free port will be used * @param family the address family to listen on, NULL for all * @param retport string to return the actual port listened on + * @param retfamily string to return the actual ip family listened on * @param fds_p location to store returned file descriptors * @param error return location for errors * @returns the number of listening file descriptors or -1 on error @@ -1662,6 +1663,7 @@ _dbus_listen_tcp_socket (const char *host, const char *port, const char *family, DBusString *retport, + DBusString *retfamily, DBusSocket **fds_p, DBusError *error) { @@ -1831,6 +1833,13 @@ _dbus_listen_tcp_socket (const char *host, listen_fd[nlisten_fd] = fd; nlisten_fd++; + if (!_dbus_string_get_length(retfamily)) + if(!_dbus_string_append(retfamily, tmp->ai_addr->sa_family == AF_INET ? "ipv4" : "ipv6")) + { + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + goto failed; + } + if (!_dbus_string_get_length(retport)) { /* If the user didn't specify a port, or used 0, then diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index 171af7b5..2f45fb55 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -237,6 +237,7 @@ int _dbus_listen_tcp_socket (const char *host, const char *port, const char *family, DBusString *retport, + DBusString *retfamily, DBusSocket **fds_p, DBusError *error); DBusSocket _dbus_accept (DBusSocket listen_fd); -- 2.12.3