From 1bd4ecc1256f38d7ab0da736434d46762507348b Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Fri, 8 Jun 2007 20:43:35 +0000 Subject: 2007-06-08 Havoc Pennington * backport fix to allow a server to use port=0 or omit port so the port can be auto-selected by the OS --- dbus/dbus-sysdeps-unix.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'dbus/dbus-sysdeps-unix.c') diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index 1c96d6e3..eedbe33b 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -757,21 +757,24 @@ _dbus_connect_tcp_socket (const char *host, * Creates a socket and binds it to the given path, * then listens on the socket. The socket is * set to be nonblocking. + * In case of port=0 a random free port is used and + * returned in the port parameter. * * @param host the host name to listen on - * @param port the prot to listen on + * @param port the prot to listen on, if zero a free port will be used * @param error return location for errors * @returns the listening file descriptor or -1 on error */ 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); @@ -799,13 +802,13 @@ _dbus_listen_tcp_socket (const char *host, _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; } @@ -814,11 +817,14 @@ _dbus_listen_tcp_socket (const char *host, { 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); -- cgit