<listen> element of the configuration file

Ralf Habacker ralf.habacker at freenet.de
Fri Mar 9 12:51:16 PST 2007


Ralf Habacker schrieb:

Here is an updated patch which should work on win32 and unix. Can anyone 
try it on unix ?

bus\session.conf
....
<listen>tcp:host=localhost</listen> -> does not work
....

F:\daten\windbus\cvs-commit2-msvc-build>bin\debug\dbus-daemon 
--config-file=%CD%\bus\session.conf --print-address
2908: assertion failed "value != NULL" file 
"..\..\cvs-commit2\dbus\dbus-string.c" line 212 function _dbus_string_init_c
onst

bus\session.conf
....
<listen>tcp:host=localhost,port=0</listen> works
....
F:\daten\windbus\cvs-commit2-msvc-build>bin\debug\dbus-daemon 
--config-file=%CD%\bus\session.conf --print-address
tcp:host=localhost,port=4577,guid=5021176fdd2eeca4068e990045f1c81d
F:\daten\windbus\cvs-commit2-msvc-build>

<kill>

F:\daten\windbus\cvs-commit2-msvc-build>bin\debug\dbus-daemon 
--config-file=%CD%\bus\session.conf --print-address
tcp:host=localhost,port=4580,guid=2c5613ca6bf648503a1a250045f1c867

This address could then be used for clients. 

If there are no more problems I would check this in.

Ralf


-------------- next part --------------
Index: dbus/dbus-server-socket.c
===================================================================
RCS file: /cvs/dbus/dbus/dbus/dbus-server-socket.c,v
retrieving revision 1.3
diff -u -r1.3 dbus-server-socket.c
--- dbus/dbus-server-socket.c	1 Oct 2006 15:36:18 -0000	1.3
+++ dbus/dbus-server-socket.c	9 Mar 2007 20:34:54 -0000
@@ -323,6 +323,9 @@
 
   if (host == NULL)
     host = "localhost";
+  
+  listen_fd = _dbus_listen_tcp_socket (host, &port, error);
+  _dbus_fd_set_close_on_exec (listen_fd);
 
   _dbus_string_init_const (&host_str, host);
   if (!_dbus_string_append (&address, "tcp:host=") ||
@@ -334,9 +337,7 @@
       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
       return NULL;
     }
-  
-  listen_fd = _dbus_listen_tcp_socket (host, port, error);
-  _dbus_fd_set_close_on_exec (listen_fd);
+
   
   if (listen_fd < 0)
     {
@@ -391,17 +392,14 @@
       long lport;
       dbus_bool_t sresult;
           
-      if (port == NULL)
-        {
-          _dbus_set_bad_address(error, "tcp", "port", NULL);
-          return DBUS_SERVER_LISTEN_BAD_ADDRESS;
-        }
+      if (port == NULL)
+        port = 0; /* autoselecting tcp port */
 
       _dbus_string_init_const (&str, port);
       sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
       _dbus_string_free (&str);
           
-      if (sresult == FALSE || lport <= 0 || lport > 65535)
+      if (sresult == FALSE || lport < 0 || lport > 65535)
         {
           _dbus_set_bad_address(error, NULL, NULL, 
                                 "Port is not an integer between 0 and 65535");
Index: dbus/dbus-sysdeps-unix.c
===================================================================
RCS file: /cvs/dbus/dbus/dbus/dbus-sysdeps-unix.c,v
retrieving revision 1.20
diff -u -r1.20 dbus-sysdeps-unix.c
--- dbus/dbus-sysdeps-unix.c	8 Mar 2007 20:25:15 -0000	1.20
+++ dbus/dbus-sysdeps-unix.c	9 Mar 2007 20:32:04 -0000
@@ -774,13 +774,14 @@
  */
 int
 _dbus_listen_tcp_socket (const char     *host,
-                         dbus_uint32_t   port,
+                         dbus_uint32_t  *port,
                          DBusError      *error)
 {
   int listen_fd;
   struct sockaddr_in addr;
   struct hostent *he;
   struct in_addr *haddr;
+  socklen_t len = (socklen_t) sizeof (struct sockaddr);
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
   
@@ -808,13 +809,13 @@
   _DBUS_ZERO (addr);
   memcpy (&addr.sin_addr, haddr, sizeof (struct in_addr));
   addr.sin_family = AF_INET;
-  addr.sin_port = htons (port);
+  addr.sin_port = htons (*port);
 
   if (bind (listen_fd, (struct sockaddr*) &addr, sizeof (struct sockaddr)))
     {
       dbus_set_error (error, _dbus_error_from_errno (errno),
                       "Failed to bind socket \"%s:%d\": %s",
-                      host, port, _dbus_strerror (errno));
+                      host, *port, _dbus_strerror (errno));
       _dbus_close (listen_fd, NULL);
       return -1;
     }
@@ -823,11 +824,14 @@
     {
       dbus_set_error (error, _dbus_error_from_errno (errno),  
                       "Failed to listen on socket \"%s:%d\": %s",
-                      host, port, _dbus_strerror (errno));
+                      host, *port, _dbus_strerror (errno));
       _dbus_close (listen_fd, NULL);
       return -1;
     }
 
+  getsockname(listen_fd, (struct sockaddr*) &addr, &len);
+  *port = (dbus_uint32_t) ntohs(addr.sin_port);
+
   if (!_dbus_set_fd_nonblocking (listen_fd, error))
     {
       _dbus_close (listen_fd, NULL);
Index: dbus/dbus-sysdeps-win.c
===================================================================
RCS file: /cvs/dbus/dbus/dbus/dbus-sysdeps-win.c,v
retrieving revision 1.3
diff -u -r1.3 dbus-sysdeps-win.c
--- dbus/dbus-sysdeps-win.c	8 Mar 2007 13:40:16 -0000	1.3
+++ dbus/dbus-sysdeps-win.c	9 Mar 2007 20:33:41 -0000
@@ -3447,7 +3447,7 @@
 
 int
 _dbus_listen_tcp_socket (const char     *host,
-                         dbus_uint32_t   port,
+                         dbus_uint32_t  *port,
                          DBusError      *error)
 {
   DBusSocket slisten;
@@ -3455,7 +3455,7 @@
   struct sockaddr_in addr;
   struct hostent *he;
   struct in_addr *haddr;
-  int len =  sizeof (struct sockaddr);
+  socklen_t len = (socklen_t) sizeof (struct sockaddr);
   struct in_addr ina;
 
 
@@ -3504,14 +3504,14 @@
   _DBUS_ZERO (addr);
   memcpy (&addr.sin_addr, haddr, sizeof (struct in_addr));
   addr.sin_family = AF_INET;
-  addr.sin_port = htons (port);
+  addr.sin_port = htons (*port);
 
   if (bind (slisten.fd, (struct sockaddr*) &addr, sizeof (struct sockaddr)))
     {
       DBUS_SOCKET_SET_ERRNO ();
       dbus_set_error (error, _dbus_error_from_errno (errno),
                       "Failed to bind socket \"%s:%d\": %s",
-                      host, port, _dbus_strerror (errno));
+                      host, *port, _dbus_strerror (errno));
       DBUS_CLOSE_SOCKET (slisten.fd);
       return -1;
     }
@@ -3521,14 +3521,14 @@
       DBUS_SOCKET_SET_ERRNO ();
       dbus_set_error (error, _dbus_error_from_errno (errno),
                       "Failed to listen on socket \"%s:%d\": %s",
-                      host, port, _dbus_strerror (errno));
+                      host, *port, _dbus_strerror (errno));
       DBUS_CLOSE_SOCKET (slisten.fd);
       return -1;
     }
 
-
   getsockname(slisten.fd, (struct sockaddr*) &addr, &len);
-
+  *port = (dbus_uint32_t) ntohs(addr.sin_port);
+  
   _dbus_daemon_init(host, ntohs(addr.sin_port));
 
   handle = _dbus_socket_to_handle (&slisten);
Index: dbus/dbus-sysdeps.h
===================================================================
RCS file: /cvs/dbus/dbus/dbus/dbus-sysdeps.h,v
retrieving revision 1.69
diff -u -r1.69 dbus-sysdeps.h
--- dbus/dbus-sysdeps.h	8 Mar 2007 20:25:15 -0000	1.69
+++ dbus/dbus-sysdeps.h	9 Mar 2007 20:30:50 -0000
@@ -141,7 +141,7 @@
                                dbus_uint32_t   port,
                                DBusError      *error);
 int _dbus_listen_tcp_socket   (const char     *host,
-                               dbus_uint32_t   port,
+                               dbus_uint32_t  *port,
                                DBusError      *error);
 int _dbus_accept              (int             listen_fd);
 
Index: dbus/dbus-transport-socket.c
===================================================================
RCS file: /cvs/dbus/dbus/dbus/dbus-transport-socket.c,v
retrieving revision 1.4
diff -u -r1.4 dbus-transport-socket.c
--- dbus/dbus-transport-socket.c	20 Oct 2006 03:05:00 -0000	1.4
+++ dbus/dbus-transport-socket.c	9 Mar 2007 20:40:30 -0000
@@ -1294,7 +1294,7 @@
       sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
       _dbus_string_free (&str);
           
-      if (sresult == FALSE || lport <= 0 || lport > 65535)
+      if (sresult == FALSE || lport < 0 || lport > 65535)
         {
           _dbus_set_bad_address (error, NULL, NULL,
                                  "Port is not an integer between 0 and 65535");


More information about the dbus mailing list