]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Move isc_socket_connect() calls into dispatch
authorEvan Hunt <each@isc.org>
Sat, 19 Dec 2020 09:34:41 +0000 (01:34 -0800)
committerEvan Hunt <each@isc.org>
Sat, 2 Oct 2021 18:39:56 +0000 (11:39 -0700)
dns_dispatch_connect() connects a dispatch socket (for TCP) or a
dispatch entry socket (for UDP). This is the next step in moving all
uses of the isc_socket code into the dispatch module.

This API is temporary; it needs to be cleaned up further so that it can
be called the same way for both TCP and UDP.

lib/dns/dispatch.c
lib/dns/include/dns/dispatch.h
lib/dns/request.c
lib/dns/resolver.c
lib/dns/xfrin.c

index f021b7110727cb4ff9d50c2afeae9fb0adf00976..2f65f7d4c74767d2faecf6d69f5501b9deff8441 100644 (file)
@@ -71,22 +71,6 @@ struct dns_dispatchmgr {
 
        isc_refcount_t irefs;
 
-       /*%
-        * Locked by qid->lock if qid exists; otherwise, can be used without
-        * being locked.
-        *
-        * Memory footprint considerations: this is a simple implementation of
-        * available ports, i.e., an ordered array of the actual port numbers.
-        * This will require about 256KB of memory in the worst case (128KB for
-        * each of IPv4 and IPv6).  We could reduce it by representing it as a
-        * more sophisticated way such as a list (or array) of ranges that are
-        * searched to identify a specific port.  Our decision here is the saved
-        * memory isn't worth the implementation complexity, considering the
-        * fact that the whole BIND9 process (which is mainly named) already
-        * requires a pretty large memory footprint.  We may, however, have to
-        * revisit the decision when we want to use it as a separate module for
-        * an environment where memory requirement is severer.
-        */
        in_port_t *v4ports;    /*%< available ports for IPv4 */
        unsigned int nv4ports; /*%< # of available ports for IPv4 */
        in_port_t *v6ports;    /*%< available ports for IPv4 */
@@ -2591,6 +2575,28 @@ dns_dispatch_removeresponse(dns_dispentry_t **resp,
        }
 }
 
+isc_result_t
+dns_dispatch_connect(dns_dispatch_t *disp, dns_dispentry_t *resp,
+                    isc_task_t *task, isc_taskaction_t action, void *arg) {
+       isc_socket_t *sock = NULL;
+       isc_sockaddr_t *address = NULL;
+
+       if (resp != NULL) {
+               REQUIRE(VALID_RESPONSE(resp));
+               sock = resp->dispsocket->socket;
+               address = &resp->host;
+       } else if (disp != NULL) {
+               REQUIRE(VALID_DISPATCH(disp));
+               sock = disp->socket;
+               address = &disp->peer;
+       } else {
+               INSIST(0);
+               ISC_UNREACHABLE();
+       }
+
+       return (isc_socket_connect(sock, address, task, action, arg));
+}
+
 /*
  * disp must be locked.
  */
index 69e0d81c62c69a34fb32da311cbee56cee63af4b..2854a7bcb6f42e0478a95729da79df5878dde109 100644 (file)
@@ -264,6 +264,18 @@ dns_dispatch_detach(dns_dispatch_t **dispp);
  *\li  dispp != NULL and *dispp be a valid dispatch.
  */
 
+isc_result_t
+dns_dispatch_connect(dns_dispatch_t *disp, dns_dispentry_t *resp,
+                    isc_task_t *task, isc_taskaction_t action, void *arg);
+/*%<
+ * Connect the UDP socket in 'resp' or the TCP socket in 'disp' to the
+ * remote server, and run the specified callback.
+ *
+ * Requires:
+ *\li  'resp' is NULL and 'disp' is valid, or
+ *\li  'disp' is NULL and 'resp' is valid.
+ */
+
 void
 dns_dispatch_starttcp(dns_dispatch_t *disp);
 /*%<
index 960ff04d95fbaceddd9f0951c994a07f9e98a72a..1a16f2d0e5552690ca5610c34693a7254d9d3859 100644 (file)
@@ -729,8 +729,8 @@ again:
 
        request->destaddr = *destaddr;
        if (tcp && !connected) {
-               result = isc_socket_connect(sock, destaddr, task, req_connected,
-                                           request);
+               result = dns_dispatch_connect(request->dispatch, NULL, task,
+                                             req_connected, request);
                if (result != ISC_R_SUCCESS) {
                        goto unlink;
                }
@@ -912,8 +912,8 @@ use_tcp:
 
        request->destaddr = *destaddr;
        if (tcp && !connected) {
-               result = isc_socket_connect(sock, destaddr, task, req_connected,
-                                           request);
+               result = dns_dispatch_connect(request->dispatch, NULL, task,
+                                             req_connected, request);
                if (result != ISC_R_SUCCESS) {
                        goto unlink;
                }
index 5d156e5edc243417aaf5eabaae2e38398a434c37..59079b0c5f0048980bbb5d60d6abe32f5f57c87f 100644 (file)
@@ -2165,14 +2165,11 @@ fctx_query(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
        query->magic = QUERY_MAGIC;
 
        if ((query->options & DNS_FETCHOPT_TCP) != 0) {
-               isc_socket_t *sock = NULL;
-
                /*
                 * Connect to the remote server.
                 */
-               sock = dns_dispatch_getsocket(query->dispatch);
-               result = isc_socket_connect(sock, &addrinfo->sockaddr, task,
-                                           resquery_connected, query);
+               result = dns_dispatch_connect(query->dispatch, NULL, task,
+                                             resquery_connected, query);
                if (result != ISC_R_SUCCESS) {
                        goto cleanup_dispatch;
                }
@@ -2314,11 +2311,6 @@ addr2buf(void *buf, const size_t bufsize, const isc_sockaddr_t *sockaddr) {
        return (0);
 }
 
-static inline isc_socket_t *
-query2sock(const resquery_t *query) {
-       return (dns_dispatch_getentrysocket(query->dispentry));
-}
-
 static inline size_t
 add_serveraddr(uint8_t *buf, const size_t bufsize, const resquery_t *query) {
        return (addr2buf(buf, bufsize, &query->addrinfo->sockaddr));
@@ -2371,17 +2363,17 @@ issecuredomain(dns_view_t *view, const dns_name_t *name, dns_rdatatype_t type,
 
 static isc_result_t
 resquery_send(resquery_t *query) {
-       fetchctx_t *fctx;
+       fetchctx_t *fctx = NULL;
        isc_result_t result;
        dns_name_t *qname = NULL;
        dns_rdataset_t *qrdataset = NULL;
        isc_region_t r;
-       dns_resolver_t *res;
+       dns_resolver_t *res = NULL;
        isc_task_t *task;
        isc_socket_t *sock;
        isc_buffer_t tcpbuffer;
-       isc_sockaddr_t *address;
-       isc_buffer_t *buffer;
+       isc_sockaddr_t *address = NULL;
+       isc_buffer_t *buffer = NULL;
        isc_netaddr_t ipaddr;
        dns_tsigkey_t *tsigkey = NULL;
        dns_peer_t *peer = NULL;
@@ -2406,7 +2398,6 @@ resquery_send(resquery_t *query) {
 
        res = fctx->res;
        task = res->buckets[fctx->bucketnum].task;
-       address = NULL;
 
        if (tcp) {
                /*
@@ -2805,22 +2796,20 @@ resquery_send(resquery_t *query) {
         */
        dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
 
-       sock = query2sock(query);
-
-       /*
-        * Send the query!
-        */
        if (!tcp) {
-               address = &query->addrinfo->sockaddr;
-               result = isc_socket_connect(sock, address, task,
-                                           resquery_udpconnected, query);
+               /* Connect the UDP socket */
+               result = dns_dispatch_connect(NULL, query->dispentry, task,
+                                             resquery_udpconnected, query);
                if (result != ISC_R_SUCCESS) {
                        goto cleanup_message;
                }
                query->connects++;
        }
+
        isc_buffer_usedregion(buffer, &r);
 
+       sock = dns_dispatch_getentrysocket(query->dispentry);
+
        /*
         * XXXRTH  Make sure we don't send to ourselves!  We should probably
         *              prune out these addresses when we get them from the ADB.
@@ -2841,6 +2830,7 @@ resquery_send(resquery_t *query) {
                }
        }
 
+       address = tcp ? NULL : &query->addrinfo->sockaddr;
        result = isc_socket_sendto2(sock, &r, task, address, NULL,
                                    &query->sendevent, 0);
        INSIST(result == ISC_R_SUCCESS);
@@ -9848,8 +9838,7 @@ rctx_logpacket(respctx_t *rctx) {
                dtmsgtype = DNS_DTTYPE_RR;
        }
 
-       sock = query2sock(rctx->query);
-
+       sock = dns_dispatch_getentrysocket(rctx->query);
        if (sock != NULL) {
                result = isc_socket_getsockname(sock, &localaddr);
                if (result == ISC_R_SUCCESS) {
index fe3b233a0fcfca82d2afa9f73a0ff53080e75321..0dc308839704a850f2bb95c7c22bb923f78a6817 100644 (file)
@@ -956,7 +956,6 @@ xfrin_start(dns_xfrin_ctx_t *xfr) {
                ISC_UNREACHABLE();
        }
 
-       /* TODO isc_socket_dscp(xfr->socket, xfr->dscp); */
        return (ISC_R_SUCCESS);
 
 failure:
@@ -1035,6 +1034,7 @@ xfrin_connect_done(isc_nmhandle_t *handle, isc_result_t result, void *cbarg) {
        xfr->handle = handle;
        sockaddr = isc_nmhandle_peeraddr(handle);
        isc_sockaddr_format(&sockaddr, sourcetext, sizeof(sourcetext));
+       /* TODO set DSCP */
 
        if (xfr->tsigkey != NULL && xfr->tsigkey->key != NULL) {
                dns_name_format(dst_key_name(xfr->tsigkey->key), signerbuf,