]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
Add actual used ip family to --print-address output in case of listening on tcp
authorRalf Habacker <ralf.habacker@freenet.de>
Fri, 16 Mar 2018 20:36:11 +0000 (21:36 +0100)
committerRalf Habacker <ralf.habacker@freenet.de>
Mon, 19 Mar 2018 21:24:09 +0000 (22:24 +0100)
Specifying a dbus tcp address without a family let dbus-daemon the choice
for listen on ipv4 or ipv6, but did not return the real used ip family,
which is fixed with this commit.

Bug:https://bugs.freedesktop.org/show_bug.cgi?id=105489
Reviewed-by: Simon McVittie <smcv@collabora.com>
dbus/dbus-server-socket.c
dbus/dbus-sysdeps-unix.c
dbus/dbus-sysdeps-win.c
dbus/dbus-sysdeps.h

index 2da756052c111f1b39d3a5d70f8150f17a9a4256..e51e946e74d7ae1901b6dfb8145cb82582bd062b 100644 (file)
@@ -429,7 +429,8 @@ _dbus_server_new_for_tcp_socket (const char     *host,
   DBusString host_str;    /* Initialized as const later, not freed */
   DBusString port_str = _DBUS_STRING_INIT_INVALID;
   DBusNonceFile *noncefile = NULL;
-  
+  const char *family_used = NULL;
+
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
   if (!_dbus_string_init (&address))
@@ -457,6 +458,7 @@ _dbus_server_new_for_tcp_socket (const char     *host,
 
   nlisten_fds =_dbus_listen_tcp_socket (bind, port, family,
                                         &port_str,
+                                        &family_used,
                                         &listen_fds, error);
   if (nlisten_fds <= 0)
     {
@@ -473,9 +475,9 @@ _dbus_server_new_for_tcp_socket (const char     *host,
       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
       goto failed;
     }
-  if (family &&
+  if (family_used &&
       (!_dbus_string_append (&address, ",family=") ||
-       !_dbus_string_append (&address, family)))
+       !_dbus_string_append (&address, family_used)))
     {
       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
       goto failed;
index 249cf77e91f79885392f03cf63e2783c07d9ea47..f802cace30365cebfc38dbd7f721d2c9407d08d6 100644 (file)
@@ -1492,6 +1492,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 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
@@ -1501,6 +1502,7 @@ _dbus_listen_tcp_socket (const char     *host,
                          const char     *port,
                          const char     *family,
                          DBusString     *retport,
+                         const char    **retfamily,
                          DBusSocket    **fds_p,
                          DBusError      *error)
 {
@@ -1512,6 +1514,8 @@ _dbus_listen_tcp_socket (const char     *host,
   struct addrinfo hints;
   struct addrinfo *ai, *tmp;
   unsigned int reuseaddr;
+  dbus_bool_t have_ipv4 = FALSE;
+  dbus_bool_t have_ipv6 = FALSE;
 
   *fds_p = NULL;
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
@@ -1648,6 +1652,11 @@ _dbus_listen_tcp_socket (const char     *host,
       listen_fd[nlisten_fd].fd = fd;
       nlisten_fd++;
 
+      if (tmp->ai_addr->sa_family == AF_INET)
+        have_ipv4 = TRUE;
+      else if (tmp->ai_addr->sa_family == AF_INET6)
+        have_ipv6 = TRUE;
+
       if (!_dbus_string_get_length(retport))
         {
           /* If the user didn't specify a port, or used 0, then
@@ -1707,6 +1716,11 @@ _dbus_listen_tcp_socket (const char     *host,
       goto failed;
     }
 
+  if (have_ipv4 && !have_ipv6)
+    *retfamily = "ipv4";
+  else if (!have_ipv4 && have_ipv6)
+    *retfamily = "ipv6";
+
   for (i = 0 ; i < nlisten_fd ; i++)
     {
       if (!_dbus_set_fd_nonblocking (listen_fd[i].fd, error))
index dede8e292aa2246e7c8534e97ab6207ad49f9142..70200e7f0d0bd58ce742c8adcf2430601e2332fb 100644 (file)
@@ -1652,16 +1652,17 @@ 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 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
  */
-
 int
 _dbus_listen_tcp_socket (const char     *host,
                          const char     *port,
                          const char     *family,
                          DBusString     *retport,
+                         const char    **retfamily,
                          DBusSocket    **fds_p,
                          DBusError      *error)
 {
@@ -1672,6 +1673,8 @@ _dbus_listen_tcp_socket (const char     *host,
   DBusSocket *listen_fd = NULL;
   struct addrinfo hints;
   struct addrinfo *ai, *tmp;
+  dbus_bool_t have_ipv4 = FALSE;
+  dbus_bool_t have_ipv6 = FALSE;
 
   // On Vista, sockaddr_gen must be a sockaddr_in6, and not a sockaddr_in6_old
   //That's required for family == IPv6(which is the default on Vista if family is not given)
@@ -1831,6 +1834,11 @@ _dbus_listen_tcp_socket (const char     *host,
       listen_fd[nlisten_fd] = fd;
       nlisten_fd++;
 
+      if (tmp->ai_addr->sa_family == AF_INET)
+        have_ipv4 = TRUE;
+      else if (tmp->ai_addr->sa_family == AF_INET6)
+        have_ipv6 = TRUE;
+
       if (!_dbus_string_get_length(retport))
         {
           /* If the user didn't specify a port, or used 0, then
@@ -1886,6 +1894,11 @@ _dbus_listen_tcp_socket (const char     *host,
       goto failed;
     }
 
+  if (have_ipv4 && !have_ipv6)
+    *retfamily = "ipv4";
+  else if (!have_ipv4 && have_ipv6)
+    *retfamily = "ipv6";
+
   sscanf(_dbus_string_get_const_data(retport), "%d", &port_num);
 
   for (i = 0 ; i < nlisten_fd ; i++)
index 8961c33febe16e57e96ed506bc0e307ebf85ad88..8642dda56d45a7eecd314c0435b0d2aab9c48494 100644 (file)
@@ -237,6 +237,7 @@ int _dbus_listen_tcp_socket   (const char     *host,
                                const char     *port,
                                const char     *family,
                                DBusString     *retport,
+                               const char    **retfamily,
                                DBusSocket    **fds_p,
                                DBusError      *error);
 DBusSocket _dbus_accept       (DBusSocket      listen_fd);