From fe7ed2ba24a88f8ef13d6689cd2d35c02d0e3d81 Mon Sep 17 00:00:00 2001 From: Evan Hunt Date: Tue, 31 Jan 2023 13:30:12 -0800 Subject: [PATCH] update stream sockets with bound address/port when isc_nm_listenstreamdns() is called with a local port of 0, a random port is chosen. call uv_getsockname() to determine what the port is as soon as the socket is bound, and add a function isc_nmsocket_getaddr() to retrieve it, so that the caller can connect to the listening socket. this will be used in cases where the same process is acting as both client and server. --- lib/isc/include/isc/netmgr.h | 6 ++++++ lib/isc/netmgr/netmgr.c | 6 ++++++ lib/isc/netmgr/streamdns.c | 5 +++++ lib/isc/netmgr/tcp.c | 18 ++++++++++++++++++ lib/isc/netmgr/tlsstream.c | 5 +++++ 5 files changed, 40 insertions(+) diff --git a/lib/isc/include/isc/netmgr.h b/lib/isc/include/isc/netmgr.h index 5c7a4e735a..aa90e6f80a 100644 --- a/lib/isc/include/isc/netmgr.h +++ b/lib/isc/include/isc/netmgr.h @@ -729,3 +729,9 @@ isc_nmhandle_set_tcp_nodelay(isc_nmhandle_t *handle, const bool value); * * \li 'handle' is a valid netmgr handle object. */ + +isc_sockaddr_t +isc_nmsocket_getaddr(isc_nmsocket_t *sock); +/*%< + * Return the local address of 'sock'. + */ diff --git a/lib/isc/netmgr/netmgr.c b/lib/isc/netmgr/netmgr.c index 42b8f834c6..bdc5905aeb 100644 --- a/lib/isc/netmgr/netmgr.c +++ b/lib/isc/netmgr/netmgr.c @@ -2538,6 +2538,12 @@ isc_nmhandle_set_tcp_nodelay(isc_nmhandle_t *handle, const bool value) { return (result); } +isc_sockaddr_t +isc_nmsocket_getaddr(isc_nmsocket_t *sock) { + REQUIRE(VALID_NMSOCK(sock)); + return (sock->iface); +} + #if ISC_NETMGR_TRACE /* * Dump all active sockets in netmgr. We output to stderr diff --git a/lib/isc/netmgr/streamdns.c b/lib/isc/netmgr/streamdns.c index 4e3cf5b5dc..2b3c8c38b9 100644 --- a/lib/isc/netmgr/streamdns.c +++ b/lib/isc/netmgr/streamdns.c @@ -748,6 +748,11 @@ isc_nm_listenstreamdns(isc_nm_t *mgr, uint32_t workers, isc_sockaddr_t *iface, return (result); } + /* copy the actual port we're listening on into sock->iface */ + if (isc_sockaddr_getport(iface) == 0) { + listener->iface = listener->outer->iface; + } + listener->result = result; atomic_store(&listener->active, true); atomic_store(&listener->listening, true); diff --git a/lib/isc/netmgr/tcp.c b/lib/isc/netmgr/tcp.c index 948f7c1722..5de84444ad 100644 --- a/lib/isc/netmgr/tcp.c +++ b/lib/isc/netmgr/tcp.c @@ -353,6 +353,7 @@ start_tcp_child_job(void *arg) { int r, flags = 0; isc_result_t result = ISC_R_UNSET; isc_loop_t *loop = sock->worker->loop; + struct sockaddr_storage ss; (void)isc__nm_socket_min_mtu(sock->fd, sa_family); (void)isc__nm_socket_tcp_maxseg(sock->fd, NM_MAXSEG); @@ -417,8 +418,25 @@ start_tcp_child_job(void *arg) { atomic_store(&sock->listening, true); + if (sock->tid == 0) { + r = uv_tcp_getsockname(&sock->uv_handle.tcp, + (struct sockaddr *)&ss, + &(int){ sizeof(ss) }); + if (r != 0) { + goto done; + } + + result = isc_sockaddr_fromsockaddr(&sock->parent->iface, + (struct sockaddr *)&ss); + if (result != ISC_R_SUCCESS) { + goto done_result; + } + } + done: result = isc_uverr2result(r); + +done_result: atomic_fetch_add(&sock->parent->rchildren, 1); if (result != ISC_R_SUCCESS) { diff --git a/lib/isc/netmgr/tlsstream.c b/lib/isc/netmgr/tlsstream.c index 3f74d9608b..6d6cc12df0 100644 --- a/lib/isc/netmgr/tlsstream.c +++ b/lib/isc/netmgr/tlsstream.c @@ -937,6 +937,11 @@ isc_nm_listentls(isc_nm_t *mgr, uint32_t workers, isc_sockaddr_t *iface, return (result); } + /* copy the actual port we're listening on into sock->iface */ + if (isc_sockaddr_getport(iface) == 0) { + tlssock->iface = tlssock->outer->iface; + } + /* wait for listen result */ isc__nmsocket_attach(tlssock->outer, &tsock); tlssock->result = result;