]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
update stream sockets with bound address/port
authorEvan Hunt <each@isc.org>
Tue, 31 Jan 2023 21:30:12 +0000 (13:30 -0800)
committerEvan Hunt <each@isc.org>
Tue, 28 Mar 2023 19:38:28 +0000 (12:38 -0700)
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
lib/isc/netmgr/netmgr.c
lib/isc/netmgr/streamdns.c
lib/isc/netmgr/tcp.c
lib/isc/netmgr/tlsstream.c

index 5c7a4e735af78114d478cab929e1d7e1aa70d3ea..aa90e6f80a689cf2377a29cd918fef11e62c762c 100644 (file)
@@ -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'.
+ */
index 42b8f834c6d3f99f411cc2b9b5c3cc22413ad9f1..bdc5905aeb09aa86f2721bfe806ec946d7a09432 100644 (file)
@@ -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
index 4e3cf5b5dc406dae674568d46d201df4794f0f25..2b3c8c38b968b377f732bac26e176a7dec206e26 100644 (file)
@@ -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);
index 948f7c1722fe634441790f3a98c70d1d3e2de641..5de84444ad5284b1e7c3b09c5439a64ef1041728 100644 (file)
@@ -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) {
index 3f74d9608b9087868725ca1cc4ec06a91fe93625..6d6cc12df01bac522f5433d2e95e9dd1c53133ee 100644 (file)
@@ -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;