2 * Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: dispatch.c,v 1.155.12.11 2009/12/02 23:26:28 marka Exp $ */
25 #include <sys/types.h>
29 #include <isc/entropy.h>
31 #include <isc/mutex.h>
32 #include <isc/portset.h>
33 #include <isc/print.h>
34 #include <isc/random.h>
35 #include <isc/stats.h>
36 #include <isc/string.h>
42 #include <dns/dispatch.h>
43 #include <dns/events.h>
45 #include <dns/message.h>
46 #include <dns/portlist.h>
47 #include <dns/stats.h>
48 #include <dns/tcpmsg.h>
49 #include <dns/types.h>
51 typedef ISC_LIST(dns_dispentry_t) dns_displist_t;
53 typedef struct dispsocket dispsocket_t;
54 typedef ISC_LIST(dispsocket_t) dispsocketlist_t;
56 typedef struct dispportentry dispportentry_t;
57 typedef ISC_LIST(dispportentry_t) dispportlist_t;
59 /* ARC4 Random generator state */
60 typedef struct arc4ctx {
65 isc_entropy_t *entropy; /*%< entropy source for ARC4 */
69 typedef struct dns_qid {
71 unsigned int qid_nbuckets; /*%< hash table size */
72 unsigned int qid_increment; /*%< id increment on collision */
74 dns_displist_t *qid_table; /*%< the table itself */
75 dispsocketlist_t *sock_table; /*%< socket table */
78 struct dns_dispatchmgr {
83 dns_portlist_t *portlist;
85 isc_entropy_t *entropy; /*%< entropy source */
87 /* Locked by "lock". */
90 ISC_LIST(dns_dispatch_t) list;
92 /* Locked by arc4_lock. */
93 isc_mutex_t arc4_lock;
94 arc4ctx_t arc4ctx; /*%< ARC4 context for QID */
96 /* locked by buffer lock */
98 isc_mutex_t buffer_lock;
99 unsigned int buffers; /*%< allocated buffers */
100 unsigned int buffersize; /*%< size of each buffer */
101 unsigned int maxbuffers; /*%< max buffers */
103 /* Locked internally. */
104 isc_mutex_t pool_lock;
105 isc_mempool_t *epool; /*%< memory pool for events */
106 isc_mempool_t *rpool; /*%< memory pool for replies */
107 isc_mempool_t *dpool; /*%< dispatch allocations */
108 isc_mempool_t *bpool; /*%< memory pool for buffers */
109 isc_mempool_t *spool; /*%< memory pool for dispsocs */
112 * Locked by qid->lock if qid exists; otherwise, can be used without
114 * Memory footprint considerations: this is a simple implementation of
115 * available ports, i.e., an ordered array of the actual port numbers.
116 * This will require about 256KB of memory in the worst case (128KB for
117 * each of IPv4 and IPv6). We could reduce it by representing it as a
118 * more sophisticated way such as a list (or array) of ranges that are
119 * searched to identify a specific port. Our decision here is the saved
120 * memory isn't worth the implementation complexity, considering the
121 * fact that the whole BIND9 process (which is mainly named) already
122 * requires a pretty large memory footprint. We may, however, have to
123 * revisit the decision when we want to use it as a separate module for
124 * an environment where memory requirement is severer.
126 in_port_t *v4ports; /*%< available ports for IPv4 */
127 unsigned int nv4ports; /*%< # of available ports for IPv4 */
128 in_port_t *v6ports; /*%< available ports for IPv4 */
129 unsigned int nv6ports; /*%< # of available ports for IPv4 */
132 #define MGR_SHUTTINGDOWN 0x00000001U
133 #define MGR_IS_SHUTTINGDOWN(l) (((l)->state & MGR_SHUTTINGDOWN) != 0)
135 #define IS_PRIVATE(d) (((d)->attributes & DNS_DISPATCHATTR_PRIVATE) != 0)
137 struct dns_dispentry {
139 dns_dispatch_t *disp;
145 isc_taskaction_t action;
147 isc_boolean_t item_out;
148 dispsocket_t *dispsocket;
149 ISC_LIST(dns_dispatchevent_t) items;
150 ISC_LINK(dns_dispentry_t) link;
154 * Maximum number of dispatch sockets that can be pooled for reuse. The
155 * appropriate value may vary, but experiments have shown a busy caching server
156 * may need more than 1000 sockets concurrently opened. The maximum allowable
157 * number of dispatch sockets (per manager) will be set to the double of this
160 #ifndef DNS_DISPATCH_POOLSOCKS
161 #define DNS_DISPATCH_POOLSOCKS 2048
165 * Quota to control the number of dispatch sockets. If a dispatch has more
166 * than the quota of sockets, new queries will purge oldest ones, so that
167 * a massive number of outstanding queries won't prevent subsequent queries
168 * (especially if the older ones take longer time and result in timeout).
170 #ifndef DNS_DISPATCH_SOCKSQUOTA
171 #define DNS_DISPATCH_SOCKSQUOTA 3072
176 isc_socket_t *socket;
177 dns_dispatch_t *disp;
179 in_port_t localport; /* XXX: should be removed later */
180 dispportentry_t *portentry;
181 dns_dispentry_t *resp;
183 ISC_LINK(dispsocket_t) link;
185 ISC_LINK(dispsocket_t) blink;
189 * A port table entry. We remember every port we first open in a table with a
190 * reference counter so that we can 'reuse' the same port (with different
191 * destination addresses) using the SO_REUSEADDR socket option.
193 struct dispportentry {
196 ISC_LINK(struct dispportentry) link;
199 #ifndef DNS_DISPATCH_PORTTABLESIZE
200 #define DNS_DISPATCH_PORTTABLESIZE 1024
203 #define INVALID_BUCKET (0xffffdead)
206 * Number of tasks for each dispatch that use separate sockets for different
207 * transactions. This must be a power of 2 as it will divide 32 bit numbers
208 * to get an uniformly random tasks selection. See get_dispsocket().
210 #define MAX_INTERNAL_TASKS 64
212 struct dns_dispatch {
214 unsigned int magic; /*%< magic */
215 dns_dispatchmgr_t *mgr; /*%< dispatch manager */
218 * internal task buckets. We use multiple tasks to distribute various
219 * socket events well when using separate dispatch sockets. We use the
220 * 1st task (task[0]) for internal control events.
222 isc_task_t *task[MAX_INTERNAL_TASKS];
223 isc_socket_t *socket; /*%< isc socket attached to */
224 isc_sockaddr_t local; /*%< local address */
225 in_port_t localport; /*%< local UDP port */
226 unsigned int maxrequests; /*%< max requests */
227 isc_event_t *ctlevent;
229 /*% Locked by mgr->lock. */
230 ISC_LINK(dns_dispatch_t) link;
232 /* Locked by "lock". */
233 isc_mutex_t lock; /*%< locks all below */
234 isc_sockettype_t socktype;
235 unsigned int attributes;
236 unsigned int refcount; /*%< number of users */
237 dns_dispatchevent_t *failsafe_ev; /*%< failsafe cancel event */
238 unsigned int shutting_down : 1,
242 recv_pending : 1; /*%< is a recv() pending? */
243 isc_result_t shutdown_why;
244 ISC_LIST(dispsocket_t) activesockets;
245 ISC_LIST(dispsocket_t) inactivesockets;
246 unsigned int nsockets;
247 unsigned int requests; /*%< how many requests we have */
248 unsigned int tcpbuffers; /*%< allocated buffers */
249 dns_tcpmsg_t tcpmsg; /*%< for tcp streams */
251 arc4ctx_t arc4ctx; /*%< for QID/UDP port num */
252 dispportlist_t *port_table; /*%< hold ports 'owned' by us */
253 isc_mempool_t *portpool; /*%< port table entries */
256 #define QID_MAGIC ISC_MAGIC('Q', 'i', 'd', ' ')
257 #define VALID_QID(e) ISC_MAGIC_VALID((e), QID_MAGIC)
259 #define RESPONSE_MAGIC ISC_MAGIC('D', 'r', 's', 'p')
260 #define VALID_RESPONSE(e) ISC_MAGIC_VALID((e), RESPONSE_MAGIC)
262 #define DISPSOCK_MAGIC ISC_MAGIC('D', 's', 'o', 'c')
263 #define VALID_DISPSOCK(e) ISC_MAGIC_VALID((e), DISPSOCK_MAGIC)
265 #define DISPATCH_MAGIC ISC_MAGIC('D', 'i', 's', 'p')
266 #define VALID_DISPATCH(e) ISC_MAGIC_VALID((e), DISPATCH_MAGIC)
268 #define DNS_DISPATCHMGR_MAGIC ISC_MAGIC('D', 'M', 'g', 'r')
269 #define VALID_DISPATCHMGR(e) ISC_MAGIC_VALID((e), DNS_DISPATCHMGR_MAGIC)
271 #define DNS_QID(disp) ((disp)->socktype == isc_sockettype_tcp) ? \
272 (disp)->qid : (disp)->mgr->qid
273 #define DISP_ARC4CTX(disp) ((disp)->socktype == isc_sockettype_udp) ? \
274 (&(disp)->arc4ctx) : (&(disp)->mgr->arc4ctx)
277 * Locking a query port buffer is a bit tricky. We access the buffer without
278 * locking until qid is created. Technically, there is a possibility of race
279 * between the creation of qid and access to the port buffer; in practice,
280 * however, this should be safe because qid isn't created until the first
281 * dispatch is created and there should be no contending situation until then.
283 #define PORTBUFLOCK(mgr) if ((mgr)->qid != NULL) LOCK(&((mgr)->qid->lock))
284 #define PORTBUFUNLOCK(mgr) if ((mgr)->qid != NULL) UNLOCK((&(mgr)->qid->lock))
289 static dns_dispentry_t *entry_search(dns_qid_t *, isc_sockaddr_t *,
290 dns_messageid_t, in_port_t, unsigned int);
291 static isc_boolean_t destroy_disp_ok(dns_dispatch_t *);
292 static void destroy_disp(isc_task_t *task, isc_event_t *event);
293 static void destroy_dispsocket(dns_dispatch_t *, dispsocket_t **);
294 static void deactivate_dispsocket(dns_dispatch_t *, dispsocket_t *);
295 static void udp_exrecv(isc_task_t *, isc_event_t *);
296 static void udp_shrecv(isc_task_t *, isc_event_t *);
297 static void udp_recv(isc_event_t *, dns_dispatch_t *, dispsocket_t *);
298 static void tcp_recv(isc_task_t *, isc_event_t *);
299 static isc_result_t startrecv(dns_dispatch_t *, dispsocket_t *);
300 static isc_uint32_t dns_hash(dns_qid_t *, isc_sockaddr_t *, dns_messageid_t,
302 static void free_buffer(dns_dispatch_t *disp, void *buf, unsigned int len);
303 static void *allocate_udp_buffer(dns_dispatch_t *disp);
304 static inline void free_event(dns_dispatch_t *disp, dns_dispatchevent_t *ev);
305 static inline dns_dispatchevent_t *allocate_event(dns_dispatch_t *disp);
306 static void do_cancel(dns_dispatch_t *disp);
307 static dns_dispentry_t *linear_first(dns_qid_t *disp);
308 static dns_dispentry_t *linear_next(dns_qid_t *disp,
309 dns_dispentry_t *resp);
310 static void dispatch_free(dns_dispatch_t **dispp);
311 static isc_result_t get_udpsocket(dns_dispatchmgr_t *mgr,
312 dns_dispatch_t *disp,
313 isc_socketmgr_t *sockmgr,
314 isc_sockaddr_t *localaddr,
315 isc_socket_t **sockp);
316 static isc_result_t dispatch_createudp(dns_dispatchmgr_t *mgr,
317 isc_socketmgr_t *sockmgr,
318 isc_taskmgr_t *taskmgr,
319 isc_sockaddr_t *localaddr,
320 unsigned int maxrequests,
321 unsigned int attributes,
322 dns_dispatch_t **dispp);
323 static isc_boolean_t destroy_mgr_ok(dns_dispatchmgr_t *mgr);
324 static void destroy_mgr(dns_dispatchmgr_t **mgrp);
325 static isc_result_t qid_allocate(dns_dispatchmgr_t *mgr, unsigned int buckets,
326 unsigned int increment, dns_qid_t **qidp,
327 isc_boolean_t needaddrtable);
328 static void qid_destroy(isc_mem_t *mctx, dns_qid_t **qidp);
329 static isc_result_t open_socket(isc_socketmgr_t *mgr, isc_sockaddr_t *local,
330 unsigned int options, isc_socket_t **sockp);
331 static isc_boolean_t portavailable(dns_dispatchmgr_t *mgr, isc_socket_t *sock,
332 isc_sockaddr_t *sockaddrp);
334 #define LVL(x) ISC_LOG_DEBUG(x)
337 mgr_log(dns_dispatchmgr_t *mgr, int level, const char *fmt, ...)
338 ISC_FORMAT_PRINTF(3, 4);
341 mgr_log(dns_dispatchmgr_t *mgr, int level, const char *fmt, ...) {
345 if (! isc_log_wouldlog(dns_lctx, level))
349 vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
352 isc_log_write(dns_lctx,
353 DNS_LOGCATEGORY_DISPATCH, DNS_LOGMODULE_DISPATCH,
354 level, "dispatchmgr %p: %s", mgr, msgbuf);
358 inc_stats(dns_dispatchmgr_t *mgr, isc_statscounter_t counter) {
359 if (mgr->stats != NULL)
360 isc_stats_increment(mgr->stats, counter);
364 dispatch_log(dns_dispatch_t *disp, int level, const char *fmt, ...)
365 ISC_FORMAT_PRINTF(3, 4);
368 dispatch_log(dns_dispatch_t *disp, int level, const char *fmt, ...) {
372 if (! isc_log_wouldlog(dns_lctx, level))
376 vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
379 isc_log_write(dns_lctx,
380 DNS_LOGCATEGORY_DISPATCH, DNS_LOGMODULE_DISPATCH,
381 level, "dispatch %p: %s", disp, msgbuf);
385 request_log(dns_dispatch_t *disp, dns_dispentry_t *resp,
386 int level, const char *fmt, ...)
387 ISC_FORMAT_PRINTF(4, 5);
390 request_log(dns_dispatch_t *disp, dns_dispentry_t *resp,
391 int level, const char *fmt, ...)
397 if (! isc_log_wouldlog(dns_lctx, level))
401 vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
404 if (VALID_RESPONSE(resp)) {
405 isc_sockaddr_format(&resp->host, peerbuf, sizeof(peerbuf));
406 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DISPATCH,
407 DNS_LOGMODULE_DISPATCH, level,
408 "dispatch %p response %p %s: %s", disp, resp,
411 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DISPATCH,
412 DNS_LOGMODULE_DISPATCH, level,
413 "dispatch %p req/resp %p: %s", disp, resp,
419 * ARC4 random number generator derived from OpenBSD.
420 * Only dispatch_arc4random() and dispatch_arc4uniformrandom() are expected
421 * to be called from general dispatch routines; the rest of them are subroutines
424 * The original copyright follows:
425 * Copyright (c) 1996, David Mazieres <dm@uun.org>
426 * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
428 * Permission to use, copy, modify, and distribute this software for any
429 * purpose with or without fee is hereby granted, provided that the above
430 * copyright notice and this permission notice appear in all copies.
432 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
433 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
434 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
435 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
436 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
437 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
438 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
441 dispatch_arc4init(arc4ctx_t *actx, isc_entropy_t *entropy, isc_mutex_t *lock) {
443 for (n = 0; n < 256; n++)
448 actx->entropy = entropy; /* don't have to attach */
453 dispatch_arc4addrandom(arc4ctx_t *actx, unsigned char *dat, int datlen) {
458 for (n = 0; n < 256; n++) {
459 actx->i = (actx->i + 1);
460 si = actx->s[actx->i];
461 actx->j = (actx->j + si + dat[n % datlen]);
462 actx->s[actx->i] = actx->s[actx->j];
463 actx->s[actx->j] = si;
468 static inline isc_uint8_t
469 dispatch_arc4get8(arc4ctx_t *actx) {
472 actx->i = (actx->i + 1);
473 si = actx->s[actx->i];
474 actx->j = (actx->j + si);
475 sj = actx->s[actx->j];
476 actx->s[actx->i] = sj;
477 actx->s[actx->j] = si;
479 return (actx->s[(si + sj) & 0xff]);
482 static inline isc_uint16_t
483 dispatch_arc4get16(arc4ctx_t *actx) {
486 val = dispatch_arc4get8(actx) << 8;
487 val |= dispatch_arc4get8(actx);
493 dispatch_arc4stir(arc4ctx_t *actx) {
496 unsigned char rnd[128];
497 isc_uint32_t rnd32[32];
501 if (actx->entropy != NULL) {
503 * We accept any quality of random data to avoid blocking.
505 result = isc_entropy_getdata(actx->entropy, rnd.rnd,
506 sizeof(rnd), NULL, 0);
507 RUNTIME_CHECK(result == ISC_R_SUCCESS);
509 for (i = 0; i < 32; i++)
510 isc_random_get(&rnd.rnd32[i]);
512 dispatch_arc4addrandom(actx, rnd.rnd, sizeof(rnd.rnd));
515 * Discard early keystream, as per recommendations in:
516 * http://www.wisdom.weizmann.ac.il/~itsik/RC4/Papers/Rc4_ksa.ps
518 for (i = 0; i < 256; i++)
519 (void)dispatch_arc4get8(actx);
522 * Derived from OpenBSD's implementation. The rationale is not clear,
523 * but should be conservative enough in safety, and reasonably large
526 actx->count = 1600000;
530 dispatch_arc4random(arc4ctx_t *actx) {
533 if (actx->lock != NULL)
536 actx->count -= sizeof(isc_uint16_t);
537 if (actx->count <= 0)
538 dispatch_arc4stir(actx);
539 result = dispatch_arc4get16(actx);
541 if (actx->lock != NULL)
548 dispatch_arc4uniformrandom(arc4ctx_t *actx, isc_uint16_t upper_bound) {
555 * Ensure the range of random numbers [min, 0xffff] be a multiple of
556 * upper_bound and contain at least a half of the 16 bit range.
559 if (upper_bound > 0x8000)
560 min = 1 + ~upper_bound; /* 0x8000 - upper_bound */
562 min = (isc_uint16_t)(0x10000 % (isc_uint32_t)upper_bound);
565 * This could theoretically loop forever but each retry has
566 * p > 0.5 (worst case, usually far better) of selecting a
567 * number inside the range we need, so it should rarely need
571 r = dispatch_arc4random(actx);
576 return (r % upper_bound);
580 * Return a hash of the destination and message id.
583 dns_hash(dns_qid_t *qid, isc_sockaddr_t *dest, dns_messageid_t id,
588 ret = isc_sockaddr_hash(dest, ISC_TRUE);
589 ret ^= (id << 16) | port;
590 ret %= qid->qid_nbuckets;
592 INSIST(ret < qid->qid_nbuckets);
598 * Find the first entry in 'qid'. Returns NULL if there are no entries.
600 static dns_dispentry_t *
601 linear_first(dns_qid_t *qid) {
602 dns_dispentry_t *ret;
607 while (bucket < qid->qid_nbuckets) {
608 ret = ISC_LIST_HEAD(qid->qid_table[bucket]);
618 * Find the next entry after 'resp' in 'qid'. Return NULL if there are
621 static dns_dispentry_t *
622 linear_next(dns_qid_t *qid, dns_dispentry_t *resp) {
623 dns_dispentry_t *ret;
626 ret = ISC_LIST_NEXT(resp, link);
630 bucket = resp->bucket;
632 while (bucket < qid->qid_nbuckets) {
633 ret = ISC_LIST_HEAD(qid->qid_table[bucket]);
643 * The dispatch must be locked.
646 destroy_disp_ok(dns_dispatch_t *disp)
648 if (disp->refcount != 0)
651 if (disp->recv_pending != 0)
654 if (!ISC_LIST_EMPTY(disp->activesockets))
657 if (disp->shutting_down == 0)
664 * Called when refcount reaches 0 (and safe to destroy).
666 * The dispatcher must not be locked.
667 * The manager must be locked.
670 destroy_disp(isc_task_t *task, isc_event_t *event) {
671 dns_dispatch_t *disp;
672 dns_dispatchmgr_t *mgr;
673 isc_boolean_t killmgr;
674 dispsocket_t *dispsocket;
677 INSIST(event->ev_type == DNS_EVENT_DISPATCHCONTROL);
681 disp = event->ev_arg;
685 ISC_LIST_UNLINK(mgr->list, disp, link);
687 dispatch_log(disp, LVL(90),
688 "shutting down; detaching from sock %p, task %p",
689 disp->socket, disp->task[0]); /* XXXX */
691 if (disp->socket != NULL)
692 isc_socket_detach(&disp->socket);
693 while ((dispsocket = ISC_LIST_HEAD(disp->inactivesockets)) != NULL) {
694 ISC_LIST_UNLINK(disp->inactivesockets, dispsocket, link);
695 destroy_dispsocket(disp, &dispsocket);
697 for (i = 0; i < disp->ntasks; i++)
698 isc_task_detach(&disp->task[i]);
699 isc_event_free(&event);
701 dispatch_free(&disp);
703 killmgr = destroy_mgr_ok(mgr);
710 * Manipulate port table per dispatch: find an entry for a given port number,
711 * create a new entry, and decrement a given entry with possible clean-up.
713 static dispportentry_t *
714 port_search(dns_dispatch_t *disp, in_port_t port) {
715 dispportentry_t *portentry;
717 REQUIRE(disp->port_table != NULL);
719 portentry = ISC_LIST_HEAD(disp->port_table[port %
720 DNS_DISPATCH_PORTTABLESIZE]);
721 while (portentry != NULL) {
722 if (portentry->port == port)
724 portentry = ISC_LIST_NEXT(portentry, link);
730 static dispportentry_t *
731 new_portentry(dns_dispatch_t *disp, in_port_t port) {
732 dispportentry_t *portentry;
734 REQUIRE(disp->port_table != NULL);
736 portentry = isc_mempool_get(disp->portpool);
737 if (portentry == NULL)
740 portentry->port = port;
742 ISC_LINK_INIT(portentry, link);
743 ISC_LIST_APPEND(disp->port_table[port % DNS_DISPATCH_PORTTABLESIZE],
750 * The caller must not hold the qid->lock.
753 deref_portentry(dns_dispatch_t *disp, dispportentry_t **portentryp) {
754 dispportentry_t *portentry = *portentryp;
757 REQUIRE(disp->port_table != NULL);
758 REQUIRE(portentry != NULL && portentry->refs > 0);
763 if (portentry->refs == 0) {
764 ISC_LIST_UNLINK(disp->port_table[portentry->port %
765 DNS_DISPATCH_PORTTABLESIZE],
767 isc_mempool_put(disp->portpool, portentry);
775 * Find a dispsocket for socket address 'dest', and port number 'port'.
776 * Return NULL if no such entry exists.
778 static dispsocket_t *
779 socket_search(dns_qid_t *qid, isc_sockaddr_t *dest, in_port_t port,
782 dispsocket_t *dispsock;
784 REQUIRE(bucket < qid->qid_nbuckets);
786 dispsock = ISC_LIST_HEAD(qid->sock_table[bucket]);
788 while (dispsock != NULL) {
789 if (dispsock->portentry != NULL &&
790 dispsock->portentry->port == port &&
791 isc_sockaddr_equal(dest, &dispsock->host))
793 dispsock = ISC_LIST_NEXT(dispsock, blink);
800 * Make a new socket for a single dispatch with a random port number.
801 * The caller must hold the disp->lock and qid->lock.
804 get_dispsocket(dns_dispatch_t *disp, isc_sockaddr_t *dest,
805 isc_socketmgr_t *sockmgr, dns_qid_t *qid,
806 dispsocket_t **dispsockp, in_port_t *portp)
810 dns_dispatchmgr_t *mgr = disp->mgr;
811 isc_socket_t *sock = NULL;
812 isc_result_t result = ISC_R_FAILURE;
814 isc_sockaddr_t localaddr;
815 unsigned int bucket = 0;
816 dispsocket_t *dispsock;
819 unsigned int bindoptions;
820 dispportentry_t *portentry = NULL;
822 if (isc_sockaddr_pf(&disp->local) == AF_INET) {
823 nports = disp->mgr->nv4ports;
824 ports = disp->mgr->v4ports;
826 nports = disp->mgr->nv6ports;
827 ports = disp->mgr->v6ports;
830 return (ISC_R_ADDRNOTAVAIL);
832 dispsock = ISC_LIST_HEAD(disp->inactivesockets);
833 if (dispsock != NULL) {
834 ISC_LIST_UNLINK(disp->inactivesockets, dispsock, link);
835 sock = dispsock->socket;
836 dispsock->socket = NULL;
838 dispsock = isc_mempool_get(mgr->spool);
839 if (dispsock == NULL)
840 return (ISC_R_NOMEMORY);
843 dispsock->socket = NULL;
844 dispsock->disp = disp;
845 dispsock->resp = NULL;
846 dispsock->portentry = NULL;
848 dispsock->task = NULL;
849 isc_task_attach(disp->task[r % disp->ntasks], &dispsock->task);
850 ISC_LINK_INIT(dispsock, link);
851 ISC_LINK_INIT(dispsock, blink);
852 dispsock->magic = DISPSOCK_MAGIC;
856 * Pick up a random UDP port and open a new socket with it. Avoid
857 * choosing ports that share the same destination because it will be
858 * very likely to fail in bind(2) or connect(2).
860 localaddr = disp->local;
861 for (i = 0; i < 64; i++) {
862 port = ports[dispatch_arc4uniformrandom(DISP_ARC4CTX(disp),
864 isc_sockaddr_setport(&localaddr, port);
866 bucket = dns_hash(qid, dest, 0, port);
867 if (socket_search(qid, dest, port, bucket) != NULL)
870 portentry = port_search(disp, port);
871 if (portentry != NULL)
872 bindoptions |= ISC_SOCKET_REUSEADDRESS;
873 result = open_socket(sockmgr, &localaddr, bindoptions, &sock);
874 if (result == ISC_R_SUCCESS) {
875 if (portentry == NULL) {
876 portentry = new_portentry(disp, port);
877 if (portentry == NULL) {
878 result = ISC_R_NOMEMORY;
884 } else if (result != ISC_R_ADDRINUSE)
888 if (result == ISC_R_SUCCESS) {
889 dispsock->socket = sock;
890 dispsock->host = *dest;
891 dispsock->portentry = portentry;
892 dispsock->bucket = bucket;
893 ISC_LIST_APPEND(qid->sock_table[bucket], dispsock, blink);
894 *dispsockp = dispsock;
898 * We could keep it in the inactive list, but since this should
899 * be an exceptional case and might be resource shortage, we'd
903 isc_socket_detach(&sock);
904 destroy_dispsocket(disp, &dispsock);
911 * Destroy a dedicated dispatch socket.
914 destroy_dispsocket(dns_dispatch_t *disp, dispsocket_t **dispsockp) {
915 dispsocket_t *dispsock;
919 * The dispatch must be locked.
922 REQUIRE(dispsockp != NULL && *dispsockp != NULL);
923 dispsock = *dispsockp;
924 REQUIRE(!ISC_LINK_LINKED(dispsock, link));
928 if (dispsock->portentry != NULL)
929 deref_portentry(disp, &dispsock->portentry);
930 if (dispsock->socket != NULL)
931 isc_socket_detach(&dispsock->socket);
932 if (ISC_LINK_LINKED(dispsock, blink)) {
935 ISC_LIST_UNLINK(qid->sock_table[dispsock->bucket], dispsock,
939 if (dispsock->task != NULL)
940 isc_task_detach(&dispsock->task);
941 isc_mempool_put(disp->mgr->spool, dispsock);
947 * Deactivate a dedicated dispatch socket. Move it to the inactive list for
948 * future reuse unless the total number of sockets are exceeding the maximum.
951 deactivate_dispsocket(dns_dispatch_t *disp, dispsocket_t *dispsock) {
956 * The dispatch must be locked.
958 ISC_LIST_UNLINK(disp->activesockets, dispsock, link);
959 if (dispsock->resp != NULL) {
960 INSIST(dispsock->resp->dispsocket == dispsock);
961 dispsock->resp->dispsocket = NULL;
964 INSIST(dispsock->portentry != NULL);
965 deref_portentry(disp, &dispsock->portentry);
967 if (disp->nsockets > DNS_DISPATCH_POOLSOCKS)
968 destroy_dispsocket(disp, &dispsock);
970 result = isc_socket_close(dispsock->socket);
974 ISC_LIST_UNLINK(qid->sock_table[dispsock->bucket], dispsock,
978 if (result == ISC_R_SUCCESS)
979 ISC_LIST_APPEND(disp->inactivesockets, dispsock, link);
982 * If the underlying system does not allow this
983 * optimization, destroy this temporary structure (and
984 * create a new one for a new transaction).
986 INSIST(result == ISC_R_NOTIMPLEMENTED);
987 destroy_dispsocket(disp, &dispsock);
993 * Find an entry for query ID 'id', socket address 'dest', and port number
995 * Return NULL if no such entry exists.
997 static dns_dispentry_t *
998 entry_search(dns_qid_t *qid, isc_sockaddr_t *dest, dns_messageid_t id,
999 in_port_t port, unsigned int bucket)
1001 dns_dispentry_t *res;
1003 REQUIRE(bucket < qid->qid_nbuckets);
1005 res = ISC_LIST_HEAD(qid->qid_table[bucket]);
1007 while (res != NULL) {
1008 if (res->id == id && isc_sockaddr_equal(dest, &res->host) &&
1009 res->port == port) {
1012 res = ISC_LIST_NEXT(res, link);
1019 free_buffer(dns_dispatch_t *disp, void *buf, unsigned int len) {
1020 INSIST(buf != NULL && len != 0);
1023 switch (disp->socktype) {
1024 case isc_sockettype_tcp:
1025 INSIST(disp->tcpbuffers > 0);
1027 isc_mem_put(disp->mgr->mctx, buf, len);
1029 case isc_sockettype_udp:
1030 LOCK(&disp->mgr->buffer_lock);
1031 INSIST(disp->mgr->buffers > 0);
1032 INSIST(len == disp->mgr->buffersize);
1033 disp->mgr->buffers--;
1034 isc_mempool_put(disp->mgr->bpool, buf);
1035 UNLOCK(&disp->mgr->buffer_lock);
1044 allocate_udp_buffer(dns_dispatch_t *disp) {
1047 LOCK(&disp->mgr->buffer_lock);
1048 temp = isc_mempool_get(disp->mgr->bpool);
1051 disp->mgr->buffers++;
1052 UNLOCK(&disp->mgr->buffer_lock);
1058 free_event(dns_dispatch_t *disp, dns_dispatchevent_t *ev) {
1059 if (disp->failsafe_ev == ev) {
1060 INSIST(disp->shutdown_out == 1);
1061 disp->shutdown_out = 0;
1066 isc_mempool_put(disp->mgr->epool, ev);
1069 static inline dns_dispatchevent_t *
1070 allocate_event(dns_dispatch_t *disp) {
1071 dns_dispatchevent_t *ev;
1073 ev = isc_mempool_get(disp->mgr->epool);
1076 ISC_EVENT_INIT(ev, sizeof(*ev), 0, NULL, 0,
1077 NULL, NULL, NULL, NULL, NULL);
1083 udp_exrecv(isc_task_t *task, isc_event_t *ev) {
1084 dispsocket_t *dispsock = ev->ev_arg;
1088 REQUIRE(VALID_DISPSOCK(dispsock));
1089 udp_recv(ev, dispsock->disp, dispsock);
1093 udp_shrecv(isc_task_t *task, isc_event_t *ev) {
1094 dns_dispatch_t *disp = ev->ev_arg;
1098 REQUIRE(VALID_DISPATCH(disp));
1099 udp_recv(ev, disp, NULL);
1105 * If I/O result == CANCELED or error, free the buffer.
1107 * If query, free the buffer, restart.
1110 * Allocate event, fill in details.
1111 * If cannot allocate, free buffer, restart.
1112 * find target. If not found, free buffer, restart.
1113 * if event queue is not empty, queue. else, send.
1117 udp_recv(isc_event_t *ev_in, dns_dispatch_t *disp, dispsocket_t *dispsock) {
1118 isc_socketevent_t *ev = (isc_socketevent_t *)ev_in;
1121 isc_buffer_t source;
1123 dns_dispentry_t *resp = NULL;
1124 dns_dispatchevent_t *rev;
1125 unsigned int bucket;
1126 isc_boolean_t killit;
1127 isc_boolean_t queue_response;
1128 dns_dispatchmgr_t *mgr;
1130 isc_netaddr_t netaddr;
1133 isc_boolean_t qidlocked = ISC_FALSE;
1140 dispatch_log(disp, LVL(90),
1141 "got packet: requests %d, buffers %d, recvs %d",
1142 disp->requests, disp->mgr->buffers, disp->recv_pending);
1144 if (dispsock == NULL && ev->ev_type == ISC_SOCKEVENT_RECVDONE) {
1146 * Unless the receive event was imported from a listening
1147 * interface, in which case the event type is
1148 * DNS_EVENT_IMPORTRECVDONE, receive operation must be pending.
1150 INSIST(disp->recv_pending != 0);
1151 disp->recv_pending = 0;
1154 if (dispsock != NULL &&
1155 (ev->result == ISC_R_CANCELED || dispsock->resp == NULL)) {
1157 * dispsock->resp can be NULL if this transaction was canceled
1158 * just after receiving a response. Since this socket is
1159 * exclusively used and there should be at most one receive
1160 * event the canceled event should have been no effect. So
1161 * we can (and should) deactivate the socket right now.
1163 deactivate_dispsocket(disp, dispsock);
1167 if (disp->shutting_down) {
1169 * This dispatcher is shutting down.
1171 free_buffer(disp, ev->region.base, ev->region.length);
1173 isc_event_free(&ev_in);
1176 killit = destroy_disp_ok(disp);
1177 UNLOCK(&disp->lock);
1179 isc_task_send(disp->task[0], &disp->ctlevent);
1184 if ((disp->attributes & DNS_DISPATCHATTR_EXCLUSIVE) != 0) {
1185 if (dispsock != NULL) {
1186 resp = dispsock->resp;
1188 if (ev->result != ISC_R_SUCCESS) {
1190 * This is most likely a network error on a
1191 * connected socket. It makes no sense to
1192 * check the address or parse the packet, but it
1193 * will help to return the error to the caller.
1198 free_buffer(disp, ev->region.base, ev->region.length);
1200 UNLOCK(&disp->lock);
1201 isc_event_free(&ev_in);
1204 } else if (ev->result != ISC_R_SUCCESS) {
1205 free_buffer(disp, ev->region.base, ev->region.length);
1207 if (ev->result != ISC_R_CANCELED)
1208 dispatch_log(disp, ISC_LOG_ERROR,
1209 "odd socket result in udp_recv(): %s",
1210 isc_result_totext(ev->result));
1212 UNLOCK(&disp->lock);
1213 isc_event_free(&ev_in);
1218 * If this is from a blackholed address, drop it.
1220 isc_netaddr_fromsockaddr(&netaddr, &ev->address);
1221 if (disp->mgr->blackhole != NULL &&
1222 dns_acl_match(&netaddr, NULL, disp->mgr->blackhole,
1223 NULL, &match, NULL) == ISC_R_SUCCESS &&
1226 if (isc_log_wouldlog(dns_lctx, LVL(10))) {
1227 char netaddrstr[ISC_NETADDR_FORMATSIZE];
1228 isc_netaddr_format(&netaddr, netaddrstr,
1229 sizeof(netaddrstr));
1230 dispatch_log(disp, LVL(10),
1231 "blackholed packet from %s",
1234 free_buffer(disp, ev->region.base, ev->region.length);
1239 * Peek into the buffer to see what we can see.
1241 isc_buffer_init(&source, ev->region.base, ev->region.length);
1242 isc_buffer_add(&source, ev->n);
1243 dres = dns_message_peekheader(&source, &id, &flags);
1244 if (dres != ISC_R_SUCCESS) {
1245 free_buffer(disp, ev->region.base, ev->region.length);
1246 dispatch_log(disp, LVL(10), "got garbage packet");
1250 dispatch_log(disp, LVL(92),
1251 "got valid DNS message header, /QR %c, id %u",
1252 ((flags & DNS_MESSAGEFLAG_QR) ? '1' : '0'), id);
1255 * Look at flags. If query, drop it. If response,
1256 * look to see where it goes.
1258 queue_response = ISC_FALSE;
1259 if ((flags & DNS_MESSAGEFLAG_QR) == 0) {
1261 free_buffer(disp, ev->region.base, ev->region.length);
1266 * Search for the corresponding response. If we are using an exclusive
1267 * socket, we've already identified it and we can skip the search; but
1268 * the ID and the address must match the expected ones.
1271 bucket = dns_hash(qid, &ev->address, id, disp->localport);
1273 qidlocked = ISC_TRUE;
1274 resp = entry_search(qid, &ev->address, id, disp->localport,
1276 dispatch_log(disp, LVL(90),
1277 "search for response in bucket %d: %s",
1278 bucket, (resp == NULL ? "not found" : "found"));
1281 inc_stats(mgr, dns_resstatscounter_mismatch);
1282 free_buffer(disp, ev->region.base, ev->region.length);
1285 } else if (resp->id != id || !isc_sockaddr_equal(&ev->address,
1287 dispatch_log(disp, LVL(90),
1288 "response to an exclusive socket doesn't match");
1289 inc_stats(mgr, dns_resstatscounter_mismatch);
1290 free_buffer(disp, ev->region.base, ev->region.length);
1295 * Now that we have the original dispatch the query was sent
1296 * from check that the address and port the response was
1297 * sent to make sense.
1299 if (disp != resp->disp) {
1304 * Check that the socket types and ports match.
1306 if (disp->socktype != resp->disp->socktype ||
1307 isc_sockaddr_getport(&disp->local) !=
1308 isc_sockaddr_getport(&resp->disp->local)) {
1309 free_buffer(disp, ev->region.base, ev->region.length);
1314 * If both dispatches are bound to an address then fail as
1315 * the addresses can't be equal (enforced by the IP stack).
1317 * Note under Linux a packet can be sent out via IPv4 socket
1318 * and the response be received via a IPv6 socket.
1320 * Requests sent out via IPv6 should always come back in
1323 if (isc_sockaddr_pf(&resp->disp->local) == PF_INET6 &&
1324 isc_sockaddr_pf(&disp->local) != PF_INET6) {
1325 free_buffer(disp, ev->region.base, ev->region.length);
1328 isc_sockaddr_anyofpf(&a1, isc_sockaddr_pf(&resp->disp->local));
1329 isc_sockaddr_anyofpf(&a2, isc_sockaddr_pf(&disp->local));
1330 if (!isc_sockaddr_eqaddr(&a1, &resp->disp->local) &&
1331 !isc_sockaddr_eqaddr(&a2, &disp->local)) {
1332 free_buffer(disp, ev->region.base, ev->region.length);
1338 queue_response = resp->item_out;
1339 rev = allocate_event(resp->disp);
1341 free_buffer(disp, ev->region.base, ev->region.length);
1346 * At this point, rev contains the event we want to fill in, and
1347 * resp contains the information on the place to send it to.
1348 * Send the event off.
1350 isc_buffer_init(&rev->buffer, ev->region.base, ev->region.length);
1351 isc_buffer_add(&rev->buffer, ev->n);
1352 rev->result = ev->result;
1354 rev->addr = ev->address;
1355 rev->pktinfo = ev->pktinfo;
1356 rev->attributes = ev->attributes;
1357 if (queue_response) {
1358 ISC_LIST_APPEND(resp->items, rev, ev_link);
1360 ISC_EVENT_INIT(rev, sizeof(*rev), 0, NULL,
1362 resp->action, resp->arg, resp, NULL, NULL);
1363 request_log(disp, resp, LVL(90),
1364 "[a] Sent event %p buffer %p len %d to task %p",
1365 rev, rev->buffer.base, rev->buffer.length,
1367 resp->item_out = ISC_TRUE;
1368 isc_task_send(resp->task, ISC_EVENT_PTR(&rev));
1375 * Restart recv() to get the next packet.
1378 result = startrecv(disp, dispsock);
1379 if (result != ISC_R_SUCCESS && dispsock != NULL) {
1381 * XXX: wired. There seems to be no recovery process other than
1382 * deactivate this socket anyway (since we cannot start
1383 * receiving, we won't be able to receive a cancel event
1386 deactivate_dispsocket(disp, dispsock);
1388 UNLOCK(&disp->lock);
1390 isc_event_free(&ev_in);
1396 * If I/O result == CANCELED, EOF, or error, notify everyone as the
1397 * various queues drain.
1399 * If query, restart.
1402 * Allocate event, fill in details.
1403 * If cannot allocate, restart.
1404 * find target. If not found, restart.
1405 * if event queue is not empty, queue. else, send.
1409 tcp_recv(isc_task_t *task, isc_event_t *ev_in) {
1410 dns_dispatch_t *disp = ev_in->ev_arg;
1411 dns_tcpmsg_t *tcpmsg = &disp->tcpmsg;
1415 dns_dispentry_t *resp;
1416 dns_dispatchevent_t *rev;
1417 unsigned int bucket;
1418 isc_boolean_t killit;
1419 isc_boolean_t queue_response;
1422 char buf[ISC_SOCKADDR_FORMATSIZE];
1426 REQUIRE(VALID_DISPATCH(disp));
1430 dispatch_log(disp, LVL(90),
1431 "got TCP packet: requests %d, buffers %d, recvs %d",
1432 disp->requests, disp->tcpbuffers, disp->recv_pending);
1436 INSIST(disp->recv_pending != 0);
1437 disp->recv_pending = 0;
1439 if (disp->refcount == 0) {
1441 * This dispatcher is shutting down. Force cancelation.
1443 tcpmsg->result = ISC_R_CANCELED;
1446 if (tcpmsg->result != ISC_R_SUCCESS) {
1447 switch (tcpmsg->result) {
1448 case ISC_R_CANCELED:
1452 dispatch_log(disp, LVL(90), "shutting down on EOF");
1456 case ISC_R_CONNECTIONRESET:
1457 level = ISC_LOG_INFO;
1461 level = ISC_LOG_ERROR;
1463 isc_sockaddr_format(&tcpmsg->address, buf, sizeof(buf));
1464 dispatch_log(disp, level, "shutting down due to TCP "
1465 "receive error: %s: %s", buf,
1466 isc_result_totext(tcpmsg->result));
1472 * The event is statically allocated in the tcpmsg
1473 * structure, and destroy_disp() frees the tcpmsg, so we must
1474 * free the event *before* calling destroy_disp().
1476 isc_event_free(&ev_in);
1478 disp->shutting_down = 1;
1479 disp->shutdown_why = tcpmsg->result;
1482 * If the recv() was canceled pass the word on.
1484 killit = destroy_disp_ok(disp);
1485 UNLOCK(&disp->lock);
1487 isc_task_send(disp->task[0], &disp->ctlevent);
1491 dispatch_log(disp, LVL(90), "result %d, length == %d, addr = %p",
1493 tcpmsg->buffer.length, tcpmsg->buffer.base);
1496 * Peek into the buffer to see what we can see.
1498 dres = dns_message_peekheader(&tcpmsg->buffer, &id, &flags);
1499 if (dres != ISC_R_SUCCESS) {
1500 dispatch_log(disp, LVL(10), "got garbage packet");
1504 dispatch_log(disp, LVL(92),
1505 "got valid DNS message header, /QR %c, id %u",
1506 ((flags & DNS_MESSAGEFLAG_QR) ? '1' : '0'), id);
1509 * Allocate an event to send to the query or response client, and
1510 * allocate a new buffer for our use.
1514 * Look at flags. If query, drop it. If response,
1515 * look to see where it goes.
1517 queue_response = ISC_FALSE;
1518 if ((flags & DNS_MESSAGEFLAG_QR) == 0) {
1528 bucket = dns_hash(qid, &tcpmsg->address, id, disp->localport);
1530 resp = entry_search(qid, &tcpmsg->address, id, disp->localport, bucket);
1531 dispatch_log(disp, LVL(90),
1532 "search for response in bucket %d: %s",
1533 bucket, (resp == NULL ? "not found" : "found"));
1537 queue_response = resp->item_out;
1538 rev = allocate_event(disp);
1543 * At this point, rev contains the event we want to fill in, and
1544 * resp contains the information on the place to send it to.
1545 * Send the event off.
1547 dns_tcpmsg_keepbuffer(tcpmsg, &rev->buffer);
1549 rev->result = ISC_R_SUCCESS;
1551 rev->addr = tcpmsg->address;
1552 if (queue_response) {
1553 ISC_LIST_APPEND(resp->items, rev, ev_link);
1555 ISC_EVENT_INIT(rev, sizeof(*rev), 0, NULL, DNS_EVENT_DISPATCH,
1556 resp->action, resp->arg, resp, NULL, NULL);
1557 request_log(disp, resp, LVL(90),
1558 "[b] Sent event %p buffer %p len %d to task %p",
1559 rev, rev->buffer.base, rev->buffer.length,
1561 resp->item_out = ISC_TRUE;
1562 isc_task_send(resp->task, ISC_EVENT_PTR(&rev));
1568 * Restart recv() to get the next packet.
1571 (void)startrecv(disp, NULL);
1573 UNLOCK(&disp->lock);
1575 isc_event_free(&ev_in);
1579 * disp must be locked.
1582 startrecv(dns_dispatch_t *disp, dispsocket_t *dispsock) {
1584 isc_region_t region;
1585 isc_socket_t *socket;
1587 if (disp->shutting_down == 1)
1588 return (ISC_R_SUCCESS);
1590 if ((disp->attributes & DNS_DISPATCHATTR_NOLISTEN) != 0)
1591 return (ISC_R_SUCCESS);
1593 if (disp->recv_pending != 0 && dispsock == NULL)
1594 return (ISC_R_SUCCESS);
1596 if (disp->mgr->buffers >= disp->mgr->maxbuffers)
1597 return (ISC_R_NOMEMORY);
1599 if ((disp->attributes & DNS_DISPATCHATTR_EXCLUSIVE) != 0 &&
1601 return (ISC_R_SUCCESS);
1603 if (dispsock != NULL)
1604 socket = dispsock->socket;
1606 socket = disp->socket;
1607 INSIST(socket != NULL);
1609 switch (disp->socktype) {
1611 * UDP reads are always maximal.
1613 case isc_sockettype_udp:
1614 region.length = disp->mgr->buffersize;
1615 region.base = allocate_udp_buffer(disp);
1616 if (region.base == NULL)
1617 return (ISC_R_NOMEMORY);
1618 if (dispsock != NULL) {
1619 res = isc_socket_recv(socket, ®ion, 1,
1620 dispsock->task, udp_exrecv,
1622 if (res != ISC_R_SUCCESS) {
1623 free_buffer(disp, region.base, region.length);
1627 res = isc_socket_recv(socket, ®ion, 1,
1628 disp->task[0], udp_shrecv, disp);
1629 if (res != ISC_R_SUCCESS) {
1630 free_buffer(disp, region.base, region.length);
1631 disp->shutdown_why = res;
1632 disp->shutting_down = 1;
1634 return (ISC_R_SUCCESS); /* recover by cancel */
1636 INSIST(disp->recv_pending == 0);
1637 disp->recv_pending = 1;
1641 case isc_sockettype_tcp:
1642 res = dns_tcpmsg_readmessage(&disp->tcpmsg, disp->task[0],
1644 if (res != ISC_R_SUCCESS) {
1645 disp->shutdown_why = res;
1646 disp->shutting_down = 1;
1648 return (ISC_R_SUCCESS); /* recover by cancel */
1650 INSIST(disp->recv_pending == 0);
1651 disp->recv_pending = 1;
1658 return (ISC_R_SUCCESS);
1662 * Mgr must be locked when calling this function.
1664 static isc_boolean_t
1665 destroy_mgr_ok(dns_dispatchmgr_t *mgr) {
1666 mgr_log(mgr, LVL(90),
1667 "destroy_mgr_ok: shuttingdown=%d, listnonempty=%d, "
1668 "epool=%d, rpool=%d, dpool=%d",
1669 MGR_IS_SHUTTINGDOWN(mgr), !ISC_LIST_EMPTY(mgr->list),
1670 isc_mempool_getallocated(mgr->epool),
1671 isc_mempool_getallocated(mgr->rpool),
1672 isc_mempool_getallocated(mgr->dpool));
1673 if (!MGR_IS_SHUTTINGDOWN(mgr))
1675 if (!ISC_LIST_EMPTY(mgr->list))
1677 if (isc_mempool_getallocated(mgr->epool) != 0)
1679 if (isc_mempool_getallocated(mgr->rpool) != 0)
1681 if (isc_mempool_getallocated(mgr->dpool) != 0)
1688 * Mgr must be unlocked when calling this function.
1691 destroy_mgr(dns_dispatchmgr_t **mgrp) {
1693 dns_dispatchmgr_t *mgr;
1702 DESTROYLOCK(&mgr->lock);
1705 DESTROYLOCK(&mgr->arc4_lock);
1707 isc_mempool_destroy(&mgr->epool);
1708 isc_mempool_destroy(&mgr->rpool);
1709 isc_mempool_destroy(&mgr->dpool);
1710 isc_mempool_destroy(&mgr->bpool);
1711 isc_mempool_destroy(&mgr->spool);
1713 DESTROYLOCK(&mgr->pool_lock);
1715 if (mgr->entropy != NULL)
1716 isc_entropy_detach(&mgr->entropy);
1717 if (mgr->qid != NULL)
1718 qid_destroy(mctx, &mgr->qid);
1720 DESTROYLOCK(&mgr->buffer_lock);
1722 if (mgr->blackhole != NULL)
1723 dns_acl_detach(&mgr->blackhole);
1725 if (mgr->stats != NULL)
1726 isc_stats_detach(&mgr->stats);
1728 if (mgr->v4ports != NULL) {
1729 isc_mem_put(mctx, mgr->v4ports,
1730 mgr->nv4ports * sizeof(in_port_t));
1732 if (mgr->v6ports != NULL) {
1733 isc_mem_put(mctx, mgr->v6ports,
1734 mgr->nv6ports * sizeof(in_port_t));
1736 isc_mem_put(mctx, mgr, sizeof(dns_dispatchmgr_t));
1737 isc_mem_detach(&mctx);
1741 open_socket(isc_socketmgr_t *mgr, isc_sockaddr_t *local,
1742 unsigned int options, isc_socket_t **sockp)
1745 isc_result_t result;
1749 result = isc_socket_create(mgr, isc_sockaddr_pf(local),
1750 isc_sockettype_udp, &sock);
1751 if (result != ISC_R_SUCCESS)
1753 isc_socket_setname(sock, "dispatcher", NULL);
1755 result = isc_socket_open(sock);
1756 if (result != ISC_R_SUCCESS)
1760 #ifndef ISC_ALLOW_MAPPED
1761 isc_socket_ipv6only(sock, ISC_TRUE);
1763 result = isc_socket_bind(sock, local, options);
1764 if (result != ISC_R_SUCCESS) {
1766 isc_socket_detach(&sock);
1768 isc_socket_close(sock);
1773 return (ISC_R_SUCCESS);
1777 * Create a temporary port list to set the initial default set of dispatch
1778 * ports: [1024, 65535]. This is almost meaningless as the application will
1779 * normally set the ports explicitly, but is provided to fill some minor corner
1783 create_default_portset(isc_mem_t *mctx, isc_portset_t **portsetp) {
1784 isc_result_t result;
1786 result = isc_portset_create(mctx, portsetp);
1787 if (result != ISC_R_SUCCESS)
1789 isc_portset_addrange(*portsetp, 1024, 65535);
1791 return (ISC_R_SUCCESS);
1799 dns_dispatchmgr_create(isc_mem_t *mctx, isc_entropy_t *entropy,
1800 dns_dispatchmgr_t **mgrp)
1802 dns_dispatchmgr_t *mgr;
1803 isc_result_t result;
1804 isc_portset_t *v4portset = NULL;
1805 isc_portset_t *v6portset = NULL;
1807 REQUIRE(mctx != NULL);
1808 REQUIRE(mgrp != NULL && *mgrp == NULL);
1810 mgr = isc_mem_get(mctx, sizeof(dns_dispatchmgr_t));
1812 return (ISC_R_NOMEMORY);
1815 isc_mem_attach(mctx, &mgr->mctx);
1817 mgr->blackhole = NULL;
1820 result = isc_mutex_init(&mgr->lock);
1821 if (result != ISC_R_SUCCESS)
1824 result = isc_mutex_init(&mgr->arc4_lock);
1825 if (result != ISC_R_SUCCESS)
1828 result = isc_mutex_init(&mgr->buffer_lock);
1829 if (result != ISC_R_SUCCESS)
1830 goto kill_arc4_lock;
1832 result = isc_mutex_init(&mgr->pool_lock);
1833 if (result != ISC_R_SUCCESS)
1834 goto kill_buffer_lock;
1837 if (isc_mempool_create(mgr->mctx, sizeof(dns_dispatchevent_t),
1838 &mgr->epool) != ISC_R_SUCCESS) {
1839 result = ISC_R_NOMEMORY;
1840 goto kill_pool_lock;
1844 if (isc_mempool_create(mgr->mctx, sizeof(dns_dispentry_t),
1845 &mgr->rpool) != ISC_R_SUCCESS) {
1846 result = ISC_R_NOMEMORY;
1851 if (isc_mempool_create(mgr->mctx, sizeof(dns_dispatch_t),
1852 &mgr->dpool) != ISC_R_SUCCESS) {
1853 result = ISC_R_NOMEMORY;
1857 isc_mempool_setname(mgr->epool, "dispmgr_epool");
1858 isc_mempool_setfreemax(mgr->epool, 1024);
1859 isc_mempool_associatelock(mgr->epool, &mgr->pool_lock);
1861 isc_mempool_setname(mgr->rpool, "dispmgr_rpool");
1862 isc_mempool_setfreemax(mgr->rpool, 1024);
1863 isc_mempool_associatelock(mgr->rpool, &mgr->pool_lock);
1865 isc_mempool_setname(mgr->dpool, "dispmgr_dpool");
1866 isc_mempool_setfreemax(mgr->dpool, 1024);
1867 isc_mempool_associatelock(mgr->dpool, &mgr->pool_lock);
1870 mgr->buffersize = 0;
1871 mgr->maxbuffers = 0;
1874 mgr->entropy = NULL;
1877 ISC_LIST_INIT(mgr->list);
1878 mgr->v4ports = NULL;
1879 mgr->v6ports = NULL;
1882 mgr->magic = DNS_DISPATCHMGR_MAGIC;
1884 result = create_default_portset(mctx, &v4portset);
1885 if (result == ISC_R_SUCCESS) {
1886 result = create_default_portset(mctx, &v6portset);
1887 if (result == ISC_R_SUCCESS) {
1888 result = dns_dispatchmgr_setavailports(mgr,
1893 if (v4portset != NULL)
1894 isc_portset_destroy(mctx, &v4portset);
1895 if (v6portset != NULL)
1896 isc_portset_destroy(mctx, &v6portset);
1897 if (result != ISC_R_SUCCESS)
1900 if (entropy != NULL)
1901 isc_entropy_attach(entropy, &mgr->entropy);
1903 dispatch_arc4init(&mgr->arc4ctx, mgr->entropy, &mgr->arc4_lock);
1906 return (ISC_R_SUCCESS);
1909 isc_mempool_destroy(&mgr->dpool);
1911 isc_mempool_destroy(&mgr->rpool);
1913 isc_mempool_destroy(&mgr->epool);
1915 DESTROYLOCK(&mgr->pool_lock);
1917 DESTROYLOCK(&mgr->buffer_lock);
1919 DESTROYLOCK(&mgr->arc4_lock);
1921 DESTROYLOCK(&mgr->lock);
1923 isc_mem_put(mctx, mgr, sizeof(dns_dispatchmgr_t));
1924 isc_mem_detach(&mctx);
1930 dns_dispatchmgr_setblackhole(dns_dispatchmgr_t *mgr, dns_acl_t *blackhole) {
1931 REQUIRE(VALID_DISPATCHMGR(mgr));
1932 if (mgr->blackhole != NULL)
1933 dns_acl_detach(&mgr->blackhole);
1934 dns_acl_attach(blackhole, &mgr->blackhole);
1938 dns_dispatchmgr_getblackhole(dns_dispatchmgr_t *mgr) {
1939 REQUIRE(VALID_DISPATCHMGR(mgr));
1940 return (mgr->blackhole);
1944 dns_dispatchmgr_setblackportlist(dns_dispatchmgr_t *mgr,
1945 dns_portlist_t *portlist)
1947 REQUIRE(VALID_DISPATCHMGR(mgr));
1950 /* This function is deprecated: use dns_dispatchmgr_setavailports(). */
1955 dns_dispatchmgr_getblackportlist(dns_dispatchmgr_t *mgr) {
1956 REQUIRE(VALID_DISPATCHMGR(mgr));
1957 return (NULL); /* this function is deprecated */
1961 dns_dispatchmgr_setavailports(dns_dispatchmgr_t *mgr, isc_portset_t *v4portset,
1962 isc_portset_t *v6portset)
1964 in_port_t *v4ports, *v6ports, p;
1965 unsigned int nv4ports, nv6ports, i4, i6;
1967 REQUIRE(VALID_DISPATCHMGR(mgr));
1969 nv4ports = isc_portset_nports(v4portset);
1970 nv6ports = isc_portset_nports(v6portset);
1973 if (nv4ports != 0) {
1974 v4ports = isc_mem_get(mgr->mctx, sizeof(in_port_t) * nv4ports);
1975 if (v4ports == NULL)
1976 return (ISC_R_NOMEMORY);
1979 if (nv6ports != 0) {
1980 v6ports = isc_mem_get(mgr->mctx, sizeof(in_port_t) * nv6ports);
1981 if (v6ports == NULL) {
1982 if (v4ports != NULL) {
1983 isc_mem_put(mgr->mctx, v4ports,
1985 isc_portset_nports(v4portset));
1987 return (ISC_R_NOMEMORY);
1995 if (isc_portset_isset(v4portset, p)) {
1996 INSIST(i4 < nv4ports);
1999 if (isc_portset_isset(v6portset, p)) {
2000 INSIST(i6 < nv6ports);
2003 } while (p++ < 65535);
2004 INSIST(i4 == nv4ports && i6 == nv6ports);
2007 if (mgr->v4ports != NULL) {
2008 isc_mem_put(mgr->mctx, mgr->v4ports,
2009 mgr->nv4ports * sizeof(in_port_t));
2011 mgr->v4ports = v4ports;
2012 mgr->nv4ports = nv4ports;
2014 if (mgr->v6ports != NULL) {
2015 isc_mem_put(mgr->mctx, mgr->v6ports,
2016 mgr->nv6ports * sizeof(in_port_t));
2018 mgr->v6ports = v6ports;
2019 mgr->nv6ports = nv6ports;
2022 return (ISC_R_SUCCESS);
2026 dns_dispatchmgr_setudp(dns_dispatchmgr_t *mgr,
2027 unsigned int buffersize, unsigned int maxbuffers,
2028 unsigned int maxrequests, unsigned int buckets,
2029 unsigned int increment)
2031 isc_result_t result;
2033 REQUIRE(VALID_DISPATCHMGR(mgr));
2034 REQUIRE(buffersize >= 512 && buffersize < (64 * 1024));
2035 REQUIRE(maxbuffers > 0);
2036 REQUIRE(buckets < 2097169); /* next prime > 65536 * 32 */
2037 REQUIRE(increment > buckets);
2040 * Keep some number of items around. This should be a config
2041 * option. For now, keep 8, but later keep at least two even
2042 * if the caller wants less. This allows us to ensure certain
2043 * things, like an event can be "freed" and the next allocation
2044 * will always succeed.
2046 * Note that if limits are placed on anything here, we use one
2047 * event internally, so the actual limit should be "wanted + 1."
2055 LOCK(&mgr->buffer_lock);
2057 /* Create or adjust buffer pool */
2058 if (mgr->bpool != NULL) {
2060 * We only increase the maxbuffers to avoid accidental buffer
2061 * shortage. Ideally we'd separate the manager-wide maximum
2062 * from per-dispatch limits and respect the latter within the
2063 * global limit. But at this moment that's deemed to be
2064 * overkilling and isn't worth additional implementation
2067 if (maxbuffers > mgr->maxbuffers) {
2068 isc_mempool_setmaxalloc(mgr->bpool, maxbuffers);
2069 mgr->maxbuffers = maxbuffers;
2072 result = isc_mempool_create(mgr->mctx, buffersize, &mgr->bpool);
2073 if (result != ISC_R_SUCCESS) {
2074 UNLOCK(&mgr->buffer_lock);
2077 isc_mempool_setname(mgr->bpool, "dispmgr_bpool");
2078 isc_mempool_setmaxalloc(mgr->bpool, maxbuffers);
2079 isc_mempool_associatelock(mgr->bpool, &mgr->pool_lock);
2082 /* Create or adjust socket pool */
2083 if (mgr->spool != NULL) {
2084 isc_mempool_setmaxalloc(mgr->spool, DNS_DISPATCH_POOLSOCKS * 2);
2085 UNLOCK(&mgr->buffer_lock);
2086 return (ISC_R_SUCCESS);
2088 result = isc_mempool_create(mgr->mctx, sizeof(dispsocket_t),
2090 if (result != ISC_R_SUCCESS) {
2091 UNLOCK(&mgr->buffer_lock);
2094 isc_mempool_setname(mgr->spool, "dispmgr_spool");
2095 isc_mempool_setmaxalloc(mgr->spool, maxrequests);
2096 isc_mempool_associatelock(mgr->spool, &mgr->pool_lock);
2098 result = qid_allocate(mgr, buckets, increment, &mgr->qid, ISC_TRUE);
2099 if (result != ISC_R_SUCCESS)
2102 mgr->buffersize = buffersize;
2103 mgr->maxbuffers = maxbuffers;
2104 UNLOCK(&mgr->buffer_lock);
2105 return (ISC_R_SUCCESS);
2108 isc_mempool_destroy(&mgr->bpool);
2109 if (mgr->spool != NULL)
2110 isc_mempool_destroy(&mgr->spool);
2111 UNLOCK(&mgr->buffer_lock);
2116 dns_dispatchmgr_destroy(dns_dispatchmgr_t **mgrp) {
2117 dns_dispatchmgr_t *mgr;
2118 isc_boolean_t killit;
2120 REQUIRE(mgrp != NULL);
2121 REQUIRE(VALID_DISPATCHMGR(*mgrp));
2127 mgr->state |= MGR_SHUTTINGDOWN;
2129 killit = destroy_mgr_ok(mgr);
2132 mgr_log(mgr, LVL(90), "destroy: killit=%d", killit);
2139 dns_dispatchmgr_setstats(dns_dispatchmgr_t *mgr, isc_stats_t *stats) {
2140 REQUIRE(VALID_DISPATCHMGR(mgr));
2141 REQUIRE(ISC_LIST_EMPTY(mgr->list));
2142 REQUIRE(mgr->stats == NULL);
2144 isc_stats_attach(stats, &mgr->stats);
2148 port_cmp(const void *key, const void *ent) {
2149 in_port_t p1 = *(const in_port_t *)key;
2150 in_port_t p2 = *(const in_port_t *)ent;
2160 static isc_boolean_t
2161 portavailable(dns_dispatchmgr_t *mgr, isc_socket_t *sock,
2162 isc_sockaddr_t *sockaddrp)
2164 isc_sockaddr_t sockaddr;
2165 isc_result_t result;
2166 in_port_t *ports, port;
2167 unsigned int nports;
2168 isc_boolean_t available = ISC_FALSE;
2170 REQUIRE(sock != NULL || sockaddrp != NULL);
2174 sockaddrp = &sockaddr;
2175 result = isc_socket_getsockname(sock, sockaddrp);
2176 if (result != ISC_R_SUCCESS)
2180 if (isc_sockaddr_pf(sockaddrp) == AF_INET) {
2181 ports = mgr->v4ports;
2182 nports = mgr->nv4ports;
2184 ports = mgr->v6ports;
2185 nports = mgr->nv6ports;
2190 port = isc_sockaddr_getport(sockaddrp);
2191 if (bsearch(&port, ports, nports, sizeof(in_port_t), port_cmp) != NULL)
2192 available = ISC_TRUE;
2199 #define ATTRMATCH(_a1, _a2, _mask) (((_a1) & (_mask)) == ((_a2) & (_mask)))
2201 static isc_boolean_t
2202 local_addr_match(dns_dispatch_t *disp, isc_sockaddr_t *addr) {
2203 isc_sockaddr_t sockaddr;
2204 isc_result_t result;
2206 REQUIRE(disp->socket != NULL);
2212 * Don't match wildcard ports unless the port is available in the
2213 * current configuration.
2215 if (isc_sockaddr_getport(addr) == 0 &&
2216 isc_sockaddr_getport(&disp->local) == 0 &&
2217 !portavailable(disp->mgr, disp->socket, NULL)) {
2222 * Check if we match the binding <address,port>.
2223 * Wildcard ports match/fail here.
2225 if (isc_sockaddr_equal(&disp->local, addr))
2227 if (isc_sockaddr_getport(addr) == 0)
2231 * Check if we match a bound wildcard port <address,port>.
2233 if (!isc_sockaddr_eqaddr(&disp->local, addr))
2235 result = isc_socket_getsockname(disp->socket, &sockaddr);
2236 if (result != ISC_R_SUCCESS)
2239 return (isc_sockaddr_equal(&sockaddr, addr));
2243 * Requires mgr be locked.
2245 * No dispatcher can be locked by this thread when calling this function.
2249 * If a matching dispatcher is found, it is locked after this function
2250 * returns, and must be unlocked by the caller.
2253 dispatch_find(dns_dispatchmgr_t *mgr, isc_sockaddr_t *local,
2254 unsigned int attributes, unsigned int mask,
2255 dns_dispatch_t **dispp)
2257 dns_dispatch_t *disp;
2258 isc_result_t result;
2261 * Make certain that we will not match a private or exclusive dispatch.
2263 attributes &= ~(DNS_DISPATCHATTR_PRIVATE|DNS_DISPATCHATTR_EXCLUSIVE);
2264 mask |= (DNS_DISPATCHATTR_PRIVATE|DNS_DISPATCHATTR_EXCLUSIVE);
2266 disp = ISC_LIST_HEAD(mgr->list);
2267 while (disp != NULL) {
2269 if ((disp->shutting_down == 0)
2270 && ATTRMATCH(disp->attributes, attributes, mask)
2271 && local_addr_match(disp, local))
2273 UNLOCK(&disp->lock);
2274 disp = ISC_LIST_NEXT(disp, link);
2278 result = ISC_R_NOTFOUND;
2283 result = ISC_R_SUCCESS;
2290 qid_allocate(dns_dispatchmgr_t *mgr, unsigned int buckets,
2291 unsigned int increment, dns_qid_t **qidp,
2292 isc_boolean_t needsocktable)
2296 isc_result_t result;
2298 REQUIRE(VALID_DISPATCHMGR(mgr));
2299 REQUIRE(buckets < 2097169); /* next prime > 65536 * 32 */
2300 REQUIRE(increment > buckets);
2301 REQUIRE(qidp != NULL && *qidp == NULL);
2303 qid = isc_mem_get(mgr->mctx, sizeof(*qid));
2305 return (ISC_R_NOMEMORY);
2307 qid->qid_table = isc_mem_get(mgr->mctx,
2308 buckets * sizeof(dns_displist_t));
2309 if (qid->qid_table == NULL) {
2310 isc_mem_put(mgr->mctx, qid, sizeof(*qid));
2311 return (ISC_R_NOMEMORY);
2314 qid->sock_table = NULL;
2315 if (needsocktable) {
2316 qid->sock_table = isc_mem_get(mgr->mctx, buckets *
2317 sizeof(dispsocketlist_t));
2318 if (qid->sock_table == NULL) {
2319 isc_mem_put(mgr->mctx, qid, sizeof(*qid));
2320 isc_mem_put(mgr->mctx, qid->qid_table,
2321 buckets * sizeof(dns_displist_t));
2322 return (ISC_R_NOMEMORY);
2326 result = isc_mutex_init(&qid->lock);
2327 if (result != ISC_R_SUCCESS) {
2328 if (qid->sock_table != NULL) {
2329 isc_mem_put(mgr->mctx, qid->sock_table,
2330 buckets * sizeof(dispsocketlist_t));
2332 isc_mem_put(mgr->mctx, qid->qid_table,
2333 buckets * sizeof(dns_displist_t));
2334 isc_mem_put(mgr->mctx, qid, sizeof(*qid));
2338 for (i = 0; i < buckets; i++) {
2339 ISC_LIST_INIT(qid->qid_table[i]);
2340 if (qid->sock_table != NULL)
2341 ISC_LIST_INIT(qid->sock_table[i]);
2344 qid->qid_nbuckets = buckets;
2345 qid->qid_increment = increment;
2346 qid->magic = QID_MAGIC;
2348 return (ISC_R_SUCCESS);
2352 qid_destroy(isc_mem_t *mctx, dns_qid_t **qidp) {
2355 REQUIRE(qidp != NULL);
2358 REQUIRE(VALID_QID(qid));
2362 isc_mem_put(mctx, qid->qid_table,
2363 qid->qid_nbuckets * sizeof(dns_displist_t));
2364 if (qid->sock_table != NULL) {
2365 isc_mem_put(mctx, qid->sock_table,
2366 qid->qid_nbuckets * sizeof(dispsocketlist_t));
2368 DESTROYLOCK(&qid->lock);
2369 isc_mem_put(mctx, qid, sizeof(*qid));
2373 * Allocate and set important limits.
2376 dispatch_allocate(dns_dispatchmgr_t *mgr, unsigned int maxrequests,
2377 dns_dispatch_t **dispp)
2379 dns_dispatch_t *disp;
2380 isc_result_t result;
2382 REQUIRE(VALID_DISPATCHMGR(mgr));
2383 REQUIRE(dispp != NULL && *dispp == NULL);
2386 * Set up the dispatcher, mostly. Don't bother setting some of
2387 * the options that are controlled by tcp vs. udp, etc.
2390 disp = isc_mempool_get(mgr->dpool);
2392 return (ISC_R_NOMEMORY);
2396 disp->maxrequests = maxrequests;
2397 disp->attributes = 0;
2398 ISC_LINK_INIT(disp, link);
2400 disp->recv_pending = 0;
2401 memset(&disp->local, 0, sizeof(disp->local));
2402 disp->localport = 0;
2403 disp->shutting_down = 0;
2404 disp->shutdown_out = 0;
2405 disp->connected = 0;
2406 disp->tcpmsg_valid = 0;
2407 disp->shutdown_why = ISC_R_UNEXPECTED;
2409 disp->tcpbuffers = 0;
2411 ISC_LIST_INIT(disp->activesockets);
2412 ISC_LIST_INIT(disp->inactivesockets);
2414 dispatch_arc4init(&disp->arc4ctx, mgr->entropy, NULL);
2415 disp->port_table = NULL;
2416 disp->portpool = NULL;
2418 result = isc_mutex_init(&disp->lock);
2419 if (result != ISC_R_SUCCESS)
2422 disp->failsafe_ev = allocate_event(disp);
2423 if (disp->failsafe_ev == NULL) {
2424 result = ISC_R_NOMEMORY;
2428 disp->magic = DISPATCH_MAGIC;
2431 return (ISC_R_SUCCESS);
2437 DESTROYLOCK(&disp->lock);
2439 isc_mempool_put(mgr->dpool, disp);
2446 * MUST be unlocked, and not used by anything.
2449 dispatch_free(dns_dispatch_t **dispp)
2451 dns_dispatch_t *disp;
2452 dns_dispatchmgr_t *mgr;
2455 REQUIRE(VALID_DISPATCH(*dispp));
2460 REQUIRE(VALID_DISPATCHMGR(mgr));
2462 if (disp->tcpmsg_valid) {
2463 dns_tcpmsg_invalidate(&disp->tcpmsg);
2464 disp->tcpmsg_valid = 0;
2467 INSIST(disp->tcpbuffers == 0);
2468 INSIST(disp->requests == 0);
2469 INSIST(disp->recv_pending == 0);
2470 INSIST(ISC_LIST_EMPTY(disp->activesockets));
2471 INSIST(ISC_LIST_EMPTY(disp->inactivesockets));
2473 isc_mempool_put(mgr->epool, disp->failsafe_ev);
2474 disp->failsafe_ev = NULL;
2476 if (disp->qid != NULL)
2477 qid_destroy(mgr->mctx, &disp->qid);
2479 if (disp->port_table != NULL) {
2480 for (i = 0; i < DNS_DISPATCH_PORTTABLESIZE; i++)
2481 INSIST(ISC_LIST_EMPTY(disp->port_table[i]));
2482 isc_mem_put(mgr->mctx, disp->port_table,
2483 sizeof(disp->port_table[0]) *
2484 DNS_DISPATCH_PORTTABLESIZE);
2487 if (disp->portpool != NULL)
2488 isc_mempool_destroy(&disp->portpool);
2491 DESTROYLOCK(&disp->lock);
2493 isc_mempool_put(mgr->dpool, disp);
2497 dns_dispatch_createtcp(dns_dispatchmgr_t *mgr, isc_socket_t *sock,
2498 isc_taskmgr_t *taskmgr, unsigned int buffersize,
2499 unsigned int maxbuffers, unsigned int maxrequests,
2500 unsigned int buckets, unsigned int increment,
2501 unsigned int attributes, dns_dispatch_t **dispp)
2503 isc_result_t result;
2504 dns_dispatch_t *disp;
2509 REQUIRE(VALID_DISPATCHMGR(mgr));
2510 REQUIRE(isc_socket_gettype(sock) == isc_sockettype_tcp);
2511 REQUIRE((attributes & DNS_DISPATCHATTR_TCP) != 0);
2512 REQUIRE((attributes & DNS_DISPATCHATTR_UDP) == 0);
2514 attributes |= DNS_DISPATCHATTR_PRIVATE; /* XXXMLG */
2519 * dispatch_allocate() checks mgr for us.
2520 * qid_allocate() checks buckets and increment for us.
2523 result = dispatch_allocate(mgr, maxrequests, &disp);
2524 if (result != ISC_R_SUCCESS) {
2529 result = qid_allocate(mgr, buckets, increment, &disp->qid, ISC_FALSE);
2530 if (result != ISC_R_SUCCESS)
2531 goto deallocate_dispatch;
2533 disp->socktype = isc_sockettype_tcp;
2534 disp->socket = NULL;
2535 isc_socket_attach(sock, &disp->socket);
2538 disp->task[0] = NULL;
2539 result = isc_task_create(taskmgr, 0, &disp->task[0]);
2540 if (result != ISC_R_SUCCESS)
2543 disp->ctlevent = isc_event_allocate(mgr->mctx, disp,
2544 DNS_EVENT_DISPATCHCONTROL,
2546 sizeof(isc_event_t));
2547 if (disp->ctlevent == NULL) {
2548 result = ISC_R_NOMEMORY;
2552 isc_task_setname(disp->task[0], "tcpdispatch", disp);
2554 dns_tcpmsg_init(mgr->mctx, disp->socket, &disp->tcpmsg);
2555 disp->tcpmsg_valid = 1;
2557 disp->attributes = attributes;
2560 * Append it to the dispatcher list.
2562 ISC_LIST_APPEND(mgr->list, disp, link);
2565 mgr_log(mgr, LVL(90), "created TCP dispatcher %p", disp);
2566 dispatch_log(disp, LVL(90), "created task %p", disp->task[0]);
2570 return (ISC_R_SUCCESS);
2576 isc_task_detach(&disp->task[0]);
2578 isc_socket_detach(&disp->socket);
2579 deallocate_dispatch:
2580 dispatch_free(&disp);
2588 dns_dispatch_getudp(dns_dispatchmgr_t *mgr, isc_socketmgr_t *sockmgr,
2589 isc_taskmgr_t *taskmgr, isc_sockaddr_t *localaddr,
2590 unsigned int buffersize,
2591 unsigned int maxbuffers, unsigned int maxrequests,
2592 unsigned int buckets, unsigned int increment,
2593 unsigned int attributes, unsigned int mask,
2594 dns_dispatch_t **dispp)
2596 isc_result_t result;
2597 dns_dispatch_t *disp = NULL;
2599 REQUIRE(VALID_DISPATCHMGR(mgr));
2600 REQUIRE(sockmgr != NULL);
2601 REQUIRE(localaddr != NULL);
2602 REQUIRE(taskmgr != NULL);
2603 REQUIRE(buffersize >= 512 && buffersize < (64 * 1024));
2604 REQUIRE(maxbuffers > 0);
2605 REQUIRE(buckets < 2097169); /* next prime > 65536 * 32 */
2606 REQUIRE(increment > buckets);
2607 REQUIRE(dispp != NULL && *dispp == NULL);
2608 REQUIRE((attributes & DNS_DISPATCHATTR_TCP) == 0);
2610 result = dns_dispatchmgr_setudp(mgr, buffersize, maxbuffers,
2611 maxrequests, buckets, increment);
2612 if (result != ISC_R_SUCCESS)
2617 if ((attributes & DNS_DISPATCHATTR_EXCLUSIVE) != 0) {
2618 REQUIRE(isc_sockaddr_getport(localaddr) == 0);
2623 * See if we have a dispatcher that matches.
2625 result = dispatch_find(mgr, localaddr, attributes, mask, &disp);
2626 if (result == ISC_R_SUCCESS) {
2629 if (disp->maxrequests < maxrequests)
2630 disp->maxrequests = maxrequests;
2632 if ((disp->attributes & DNS_DISPATCHATTR_NOLISTEN) == 0 &&
2633 (attributes & DNS_DISPATCHATTR_NOLISTEN) != 0)
2635 disp->attributes |= DNS_DISPATCHATTR_NOLISTEN;
2636 if (disp->recv_pending != 0)
2637 isc_socket_cancel(disp->socket, disp->task[0],
2638 ISC_SOCKCANCEL_RECV);
2641 UNLOCK(&disp->lock);
2646 return (ISC_R_SUCCESS);
2653 result = dispatch_createudp(mgr, sockmgr, taskmgr, localaddr,
2654 maxrequests, attributes, &disp);
2655 if (result != ISC_R_SUCCESS) {
2662 return (ISC_R_SUCCESS);
2666 * mgr should be locked.
2669 #ifndef DNS_DISPATCH_HELD
2670 #define DNS_DISPATCH_HELD 20U
2674 get_udpsocket(dns_dispatchmgr_t *mgr, dns_dispatch_t *disp,
2675 isc_socketmgr_t *sockmgr, isc_sockaddr_t *localaddr,
2676 isc_socket_t **sockp)
2679 isc_socket_t *held[DNS_DISPATCH_HELD];
2680 isc_sockaddr_t localaddr_bound;
2681 isc_socket_t *sock = NULL;
2682 isc_result_t result = ISC_R_SUCCESS;
2683 isc_boolean_t anyport;
2685 INSIST(sockp != NULL && *sockp == NULL);
2687 localaddr_bound = *localaddr;
2688 anyport = ISC_TF(isc_sockaddr_getport(localaddr) == 0);
2691 unsigned int nports;
2695 * If no port is specified, we first try to pick up a random
2696 * port by ourselves.
2698 if (isc_sockaddr_pf(&disp->local) == AF_INET) {
2699 nports = disp->mgr->nv4ports;
2700 ports = disp->mgr->v4ports;
2702 nports = disp->mgr->nv6ports;
2703 ports = disp->mgr->v6ports;
2706 return (ISC_R_ADDRNOTAVAIL);
2708 for (i = 0; i < 1024; i++) {
2711 prt = ports[dispatch_arc4uniformrandom(
2714 isc_sockaddr_setport(&localaddr_bound, prt);
2715 result = open_socket(sockmgr, &localaddr_bound,
2717 if (result == ISC_R_SUCCESS ||
2718 result != ISC_R_ADDRINUSE) {
2719 disp->localport = prt;
2726 * If this fails 1024 times, we then ask the kernel for
2730 /* Allow to reuse address for non-random ports. */
2731 result = open_socket(sockmgr, localaddr,
2732 ISC_SOCKET_REUSEADDRESS, &sock);
2734 if (result == ISC_R_SUCCESS)
2740 memset(held, 0, sizeof(held));
2743 for (j = 0; j < 0xffffU; j++) {
2744 result = open_socket(sockmgr, localaddr, 0, &sock);
2745 if (result != ISC_R_SUCCESS)
2749 else if (portavailable(mgr, sock, NULL))
2751 if (held[i] != NULL)
2752 isc_socket_detach(&held[i]);
2755 if (i == DNS_DISPATCH_HELD)
2759 mgr_log(mgr, ISC_LOG_ERROR,
2760 "avoid-v%s-udp-ports: unable to allocate "
2761 "an available port",
2762 isc_sockaddr_pf(localaddr) == AF_INET ? "4" : "6");
2763 result = ISC_R_FAILURE;
2769 for (i = 0; i < DNS_DISPATCH_HELD; i++) {
2770 if (held[i] != NULL)
2771 isc_socket_detach(&held[i]);
2778 dispatch_createudp(dns_dispatchmgr_t *mgr, isc_socketmgr_t *sockmgr,
2779 isc_taskmgr_t *taskmgr,
2780 isc_sockaddr_t *localaddr,
2781 unsigned int maxrequests,
2782 unsigned int attributes,
2783 dns_dispatch_t **dispp)
2785 isc_result_t result;
2786 dns_dispatch_t *disp;
2787 isc_socket_t *sock = NULL;
2791 * dispatch_allocate() checks mgr for us.
2794 result = dispatch_allocate(mgr, maxrequests, &disp);
2795 if (result != ISC_R_SUCCESS)
2798 if ((attributes & DNS_DISPATCHATTR_EXCLUSIVE) == 0) {
2799 result = get_udpsocket(mgr, disp, sockmgr, localaddr, &sock);
2800 if (result != ISC_R_SUCCESS)
2801 goto deallocate_dispatch;
2803 isc_sockaddr_t sa_any;
2806 * For dispatches using exclusive sockets with a specific
2807 * source address, we only check if the specified address is
2808 * available on the system. Query sockets will be created later
2811 isc_sockaddr_anyofpf(&sa_any, isc_sockaddr_pf(localaddr));
2812 if (!isc_sockaddr_eqaddr(&sa_any, localaddr)) {
2813 result = open_socket(sockmgr, localaddr, 0, &sock);
2815 isc_socket_detach(&sock);
2816 if (result != ISC_R_SUCCESS)
2817 goto deallocate_dispatch;
2820 disp->port_table = isc_mem_get(mgr->mctx,
2821 sizeof(disp->port_table[0]) *
2822 DNS_DISPATCH_PORTTABLESIZE);
2823 if (disp->port_table == NULL)
2824 goto deallocate_dispatch;
2825 for (i = 0; i < DNS_DISPATCH_PORTTABLESIZE; i++)
2826 ISC_LIST_INIT(disp->port_table[i]);
2828 result = isc_mempool_create(mgr->mctx, sizeof(dispportentry_t),
2830 if (result != ISC_R_SUCCESS)
2831 goto deallocate_dispatch;
2832 isc_mempool_setname(disp->portpool, "disp_portpool");
2833 isc_mempool_setfreemax(disp->portpool, 128);
2835 disp->socktype = isc_sockettype_udp;
2836 disp->socket = sock;
2837 disp->local = *localaddr;
2839 if ((attributes & DNS_DISPATCHATTR_EXCLUSIVE) != 0)
2840 disp->ntasks = MAX_INTERNAL_TASKS;
2843 for (i = 0; i < disp->ntasks; i++) {
2844 disp->task[i] = NULL;
2845 result = isc_task_create(taskmgr, 0, &disp->task[i]);
2846 if (result != ISC_R_SUCCESS) {
2848 isc_task_destroy(&disp->task[i]);
2851 isc_task_setname(disp->task[i], "udpdispatch", disp);
2854 disp->ctlevent = isc_event_allocate(mgr->mctx, disp,
2855 DNS_EVENT_DISPATCHCONTROL,
2857 sizeof(isc_event_t));
2858 if (disp->ctlevent == NULL) {
2859 result = ISC_R_NOMEMORY;
2863 attributes &= ~DNS_DISPATCHATTR_TCP;
2864 attributes |= DNS_DISPATCHATTR_UDP;
2865 disp->attributes = attributes;
2868 * Append it to the dispatcher list.
2870 ISC_LIST_APPEND(mgr->list, disp, link);
2872 mgr_log(mgr, LVL(90), "created UDP dispatcher %p", disp);
2873 dispatch_log(disp, LVL(90), "created task %p", disp->task[0]); /* XXX */
2874 if (disp->socket != NULL)
2875 dispatch_log(disp, LVL(90), "created socket %p", disp->socket);
2884 for (i = 0; i < disp->ntasks; i++)
2885 isc_task_detach(&disp->task[i]);
2887 if (disp->socket != NULL)
2888 isc_socket_detach(&disp->socket);
2889 deallocate_dispatch:
2890 dispatch_free(&disp);
2896 dns_dispatch_attach(dns_dispatch_t *disp, dns_dispatch_t **dispp) {
2897 REQUIRE(VALID_DISPATCH(disp));
2898 REQUIRE(dispp != NULL && *dispp == NULL);
2902 UNLOCK(&disp->lock);
2908 * It is important to lock the manager while we are deleting the dispatch,
2909 * since dns_dispatch_getudp will call dispatch_find, which returns to
2910 * the caller a dispatch but does not attach to it until later. _getudp
2911 * locks the manager, however, so locking it here will keep us from attaching
2912 * to a dispatcher that is in the process of going away.
2915 dns_dispatch_detach(dns_dispatch_t **dispp) {
2916 dns_dispatch_t *disp;
2917 dispsocket_t *dispsock;
2918 isc_boolean_t killit;
2920 REQUIRE(dispp != NULL && VALID_DISPATCH(*dispp));
2927 INSIST(disp->refcount > 0);
2930 if (disp->refcount == 0) {
2931 if (disp->recv_pending > 0)
2932 isc_socket_cancel(disp->socket, disp->task[0],
2933 ISC_SOCKCANCEL_RECV);
2934 for (dispsock = ISC_LIST_HEAD(disp->activesockets);
2936 dispsock = ISC_LIST_NEXT(dispsock, link)) {
2937 isc_socket_cancel(dispsock->socket, dispsock->task,
2938 ISC_SOCKCANCEL_RECV);
2940 disp->shutting_down = 1;
2943 dispatch_log(disp, LVL(90), "detach: refcount %d", disp->refcount);
2945 killit = destroy_disp_ok(disp);
2946 UNLOCK(&disp->lock);
2948 isc_task_send(disp->task[0], &disp->ctlevent);
2952 dns_dispatch_addresponse2(dns_dispatch_t *disp, isc_sockaddr_t *dest,
2953 isc_task_t *task, isc_taskaction_t action, void *arg,
2954 dns_messageid_t *idp, dns_dispentry_t **resp,
2955 isc_socketmgr_t *sockmgr)
2957 dns_dispentry_t *res;
2958 unsigned int bucket;
2959 in_port_t localport = 0;
2964 dispsocket_t *dispsocket = NULL;
2965 isc_result_t result;
2967 REQUIRE(VALID_DISPATCH(disp));
2968 REQUIRE(task != NULL);
2969 REQUIRE(dest != NULL);
2970 REQUIRE(resp != NULL && *resp == NULL);
2971 REQUIRE(idp != NULL);
2972 if ((disp->attributes & DNS_DISPATCHATTR_EXCLUSIVE) != 0)
2973 REQUIRE(sockmgr != NULL);
2977 if (disp->shutting_down == 1) {
2978 UNLOCK(&disp->lock);
2979 return (ISC_R_SHUTTINGDOWN);
2982 if (disp->requests >= disp->maxrequests) {
2983 UNLOCK(&disp->lock);
2984 return (ISC_R_QUOTA);
2987 if ((disp->attributes & DNS_DISPATCHATTR_EXCLUSIVE) != 0 &&
2988 disp->nsockets > DNS_DISPATCH_SOCKSQUOTA) {
2989 dispsocket_t *oldestsocket;
2990 dns_dispentry_t *oldestresp;
2991 dns_dispatchevent_t *rev;
2994 * Kill oldest outstanding query if the number of sockets
2995 * exceeds the quota to keep the room for new queries.
2997 oldestsocket = ISC_LIST_HEAD(disp->activesockets);
2998 oldestresp = oldestsocket->resp;
2999 if (oldestresp != NULL && !oldestresp->item_out) {
3000 rev = allocate_event(oldestresp->disp);
3002 rev->buffer.base = NULL;
3003 rev->result = ISC_R_CANCELED;
3004 rev->id = oldestresp->id;
3005 ISC_EVENT_INIT(rev, sizeof(*rev), 0,
3006 NULL, DNS_EVENT_DISPATCH,
3008 oldestresp->arg, oldestresp,
3010 oldestresp->item_out = ISC_TRUE;
3011 isc_task_send(oldestresp->task,
3012 ISC_EVENT_PTR(&rev));
3013 inc_stats(disp->mgr,
3014 dns_resstatscounter_dispabort);
3019 * Move this entry to the tail so that it won't (easily) be
3020 * examined before actually being canceled.
3022 ISC_LIST_UNLINK(disp->activesockets, oldestsocket, link);
3023 ISC_LIST_APPEND(disp->activesockets, oldestsocket, link);
3026 qid = DNS_QID(disp);
3029 if ((disp->attributes & DNS_DISPATCHATTR_EXCLUSIVE) != 0) {
3031 * Get a separate UDP socket with a random port number.
3033 result = get_dispsocket(disp, dest, sockmgr, qid, &dispsocket,
3035 if (result != ISC_R_SUCCESS) {
3037 UNLOCK(&disp->lock);
3038 inc_stats(disp->mgr, dns_resstatscounter_dispsockfail);
3042 localport = disp->localport;
3046 * Try somewhat hard to find an unique ID.
3048 id = (dns_messageid_t)dispatch_arc4random(DISP_ARC4CTX(disp));
3049 bucket = dns_hash(qid, dest, id, localport);
3051 for (i = 0; i < 64; i++) {
3052 if (entry_search(qid, dest, id, localport, bucket) == NULL) {
3056 id += qid->qid_increment;
3058 bucket = dns_hash(qid, dest, id, localport);
3063 UNLOCK(&disp->lock);
3064 return (ISC_R_NOMORE);
3067 res = isc_mempool_get(disp->mgr->rpool);
3070 UNLOCK(&disp->lock);
3071 if (dispsocket != NULL)
3072 destroy_dispsocket(disp, &dispsocket);
3073 return (ISC_R_NOMEMORY);
3079 isc_task_attach(task, &res->task);
3082 res->port = localport;
3083 res->bucket = bucket;
3085 res->action = action;
3087 res->dispsocket = dispsocket;
3088 if (dispsocket != NULL)
3089 dispsocket->resp = res;
3090 res->item_out = ISC_FALSE;
3091 ISC_LIST_INIT(res->items);
3092 ISC_LINK_INIT(res, link);
3093 res->magic = RESPONSE_MAGIC;
3094 ISC_LIST_APPEND(qid->qid_table[bucket], res, link);
3097 request_log(disp, res, LVL(90),
3098 "attached to task %p", res->task);
3100 if (((disp->attributes & DNS_DISPATCHATTR_UDP) != 0) ||
3101 ((disp->attributes & DNS_DISPATCHATTR_CONNECTED) != 0)) {
3102 result = startrecv(disp, dispsocket);
3103 if (result != ISC_R_SUCCESS) {
3105 ISC_LIST_UNLINK(qid->qid_table[bucket], res, link);
3108 if (dispsocket != NULL)
3109 destroy_dispsocket(disp, &dispsocket);
3114 UNLOCK(&disp->lock);
3115 isc_task_detach(&res->task);
3116 isc_mempool_put(disp->mgr->rpool, res);
3121 if (dispsocket != NULL)
3122 ISC_LIST_APPEND(disp->activesockets, dispsocket, link);
3124 UNLOCK(&disp->lock);
3129 if ((disp->attributes & DNS_DISPATCHATTR_EXCLUSIVE) != 0)
3130 INSIST(res->dispsocket != NULL);
3132 return (ISC_R_SUCCESS);
3136 dns_dispatch_addresponse(dns_dispatch_t *disp, isc_sockaddr_t *dest,
3137 isc_task_t *task, isc_taskaction_t action, void *arg,
3138 dns_messageid_t *idp, dns_dispentry_t **resp)
3140 REQUIRE(VALID_DISPATCH(disp));
3141 REQUIRE((disp->attributes & DNS_DISPATCHATTR_EXCLUSIVE) == 0);
3143 return (dns_dispatch_addresponse2(disp, dest, task, action, arg,
3148 dns_dispatch_starttcp(dns_dispatch_t *disp) {
3150 REQUIRE(VALID_DISPATCH(disp));
3152 dispatch_log(disp, LVL(90), "starttcp %p", disp->task[0]);
3155 disp->attributes |= DNS_DISPATCHATTR_CONNECTED;
3156 (void)startrecv(disp, NULL);
3157 UNLOCK(&disp->lock);
3161 dns_dispatch_removeresponse(dns_dispentry_t **resp,
3162 dns_dispatchevent_t **sockevent)
3164 dns_dispatchmgr_t *mgr;
3165 dns_dispatch_t *disp;
3166 dns_dispentry_t *res;
3167 dispsocket_t *dispsock;
3168 dns_dispatchevent_t *ev;
3169 unsigned int bucket;
3170 isc_boolean_t killit;
3172 isc_eventlist_t events;
3175 REQUIRE(resp != NULL);
3176 REQUIRE(VALID_RESPONSE(*resp));
3182 REQUIRE(VALID_DISPATCH(disp));
3184 REQUIRE(VALID_DISPATCHMGR(mgr));
3186 qid = DNS_QID(disp);
3188 if (sockevent != NULL) {
3189 REQUIRE(*sockevent != NULL);
3198 INSIST(disp->requests > 0);
3200 INSIST(disp->refcount > 0);
3203 if (disp->refcount == 0) {
3204 if (disp->recv_pending > 0)
3205 isc_socket_cancel(disp->socket, disp->task[0],
3206 ISC_SOCKCANCEL_RECV);
3207 for (dispsock = ISC_LIST_HEAD(disp->activesockets);
3209 dispsock = ISC_LIST_NEXT(dispsock, link)) {
3210 isc_socket_cancel(dispsock->socket, dispsock->task,
3211 ISC_SOCKCANCEL_RECV);
3213 disp->shutting_down = 1;
3216 bucket = res->bucket;
3219 ISC_LIST_UNLINK(qid->qid_table[bucket], res, link);
3222 if (ev == NULL && res->item_out) {
3224 * We've posted our event, but the caller hasn't gotten it
3225 * yet. Take it back.
3227 ISC_LIST_INIT(events);
3228 n = isc_task_unsend(res->task, res, DNS_EVENT_DISPATCH,
3231 * We had better have gotten it back.
3234 ev = (dns_dispatchevent_t *)ISC_LIST_HEAD(events);
3238 REQUIRE(res->item_out == ISC_TRUE);
3239 res->item_out = ISC_FALSE;
3240 if (ev->buffer.base != NULL)
3241 free_buffer(disp, ev->buffer.base, ev->buffer.length);
3242 free_event(disp, ev);
3245 request_log(disp, res, LVL(90), "detaching from task %p", res->task);
3246 isc_task_detach(&res->task);
3248 if (res->dispsocket != NULL) {
3249 isc_socket_cancel(res->dispsocket->socket,
3250 res->dispsocket->task, ISC_SOCKCANCEL_RECV);
3251 res->dispsocket->resp = NULL;
3255 * Free any buffered requests as well
3257 ev = ISC_LIST_HEAD(res->items);
3258 while (ev != NULL) {
3259 ISC_LIST_UNLINK(res->items, ev, ev_link);
3260 if (ev->buffer.base != NULL)
3261 free_buffer(disp, ev->buffer.base, ev->buffer.length);
3262 free_event(disp, ev);
3263 ev = ISC_LIST_HEAD(res->items);
3266 isc_mempool_put(disp->mgr->rpool, res);
3267 if (disp->shutting_down == 1)
3270 (void)startrecv(disp, NULL);
3272 killit = destroy_disp_ok(disp);
3273 UNLOCK(&disp->lock);
3275 isc_task_send(disp->task[0], &disp->ctlevent);
3279 do_cancel(dns_dispatch_t *disp) {
3280 dns_dispatchevent_t *ev;
3281 dns_dispentry_t *resp;
3284 if (disp->shutdown_out == 1)
3287 qid = DNS_QID(disp);
3290 * Search for the first response handler without packets outstanding
3291 * unless a specific hander is given.
3294 for (resp = linear_first(qid);
3295 resp != NULL && resp->item_out;
3297 resp = linear_next(qid, resp);
3300 * No one to send the cancel event to, so nothing to do.
3306 * Send the shutdown failsafe event to this resp.
3308 ev = disp->failsafe_ev;
3309 ISC_EVENT_INIT(ev, sizeof(*ev), 0, NULL, DNS_EVENT_DISPATCH,
3310 resp->action, resp->arg, resp, NULL, NULL);
3311 ev->result = disp->shutdown_why;
3312 ev->buffer.base = NULL;
3313 ev->buffer.length = 0;
3314 disp->shutdown_out = 1;
3315 request_log(disp, resp, LVL(10),
3316 "cancel: failsafe event %p -> task %p",
3318 resp->item_out = ISC_TRUE;
3319 isc_task_send(resp->task, ISC_EVENT_PTR(&ev));
3325 dns_dispatch_getsocket(dns_dispatch_t *disp) {
3326 REQUIRE(VALID_DISPATCH(disp));
3328 return (disp->socket);
3332 dns_dispatch_getentrysocket(dns_dispentry_t *resp) {
3333 REQUIRE(VALID_RESPONSE(resp));
3335 if (resp->dispsocket != NULL)
3336 return (resp->dispsocket->socket);
3342 dns_dispatch_getlocaladdress(dns_dispatch_t *disp, isc_sockaddr_t *addrp) {
3344 REQUIRE(VALID_DISPATCH(disp));
3345 REQUIRE(addrp != NULL);
3347 if (disp->socktype == isc_sockettype_udp) {
3348 *addrp = disp->local;
3349 return (ISC_R_SUCCESS);
3351 return (ISC_R_NOTIMPLEMENTED);
3355 dns_dispatch_cancel(dns_dispatch_t *disp) {
3356 REQUIRE(VALID_DISPATCH(disp));
3360 if (disp->shutting_down == 1) {
3361 UNLOCK(&disp->lock);
3365 disp->shutdown_why = ISC_R_CANCELED;
3366 disp->shutting_down = 1;
3369 UNLOCK(&disp->lock);
3375 dns_dispatch_getattributes(dns_dispatch_t *disp) {
3376 REQUIRE(VALID_DISPATCH(disp));
3379 * We don't bother locking disp here; it's the caller's responsibility
3380 * to use only non volatile flags.
3382 return (disp->attributes);
3386 dns_dispatch_changeattributes(dns_dispatch_t *disp,
3387 unsigned int attributes, unsigned int mask)
3389 REQUIRE(VALID_DISPATCH(disp));
3390 /* Exclusive attribute can only be set on creation */
3391 REQUIRE((attributes & DNS_DISPATCHATTR_EXCLUSIVE) == 0);
3392 /* Also, a dispatch with randomport specified cannot start listening */
3393 REQUIRE((disp->attributes & DNS_DISPATCHATTR_EXCLUSIVE) == 0 ||
3394 (attributes & DNS_DISPATCHATTR_NOLISTEN) == 0);
3397 * Should check for valid attributes here!
3402 if ((mask & DNS_DISPATCHATTR_NOLISTEN) != 0) {
3403 if ((disp->attributes & DNS_DISPATCHATTR_NOLISTEN) != 0 &&
3404 (attributes & DNS_DISPATCHATTR_NOLISTEN) == 0) {
3405 disp->attributes &= ~DNS_DISPATCHATTR_NOLISTEN;
3406 (void)startrecv(disp, NULL);
3407 } else if ((disp->attributes & DNS_DISPATCHATTR_NOLISTEN)
3409 (attributes & DNS_DISPATCHATTR_NOLISTEN) != 0) {
3410 disp->attributes |= DNS_DISPATCHATTR_NOLISTEN;
3411 if (disp->recv_pending != 0)
3412 isc_socket_cancel(disp->socket, disp->task[0],
3413 ISC_SOCKCANCEL_RECV);
3417 disp->attributes &= ~mask;
3418 disp->attributes |= (attributes & mask);
3419 UNLOCK(&disp->lock);
3423 dns_dispatch_importrecv(dns_dispatch_t *disp, isc_event_t *event) {
3425 isc_socketevent_t *sevent, *newsevent;
3427 REQUIRE(VALID_DISPATCH(disp));
3428 REQUIRE((disp->attributes & DNS_DISPATCHATTR_NOLISTEN) != 0);
3429 REQUIRE(event != NULL);
3431 sevent = (isc_socketevent_t *)event;
3433 INSIST(sevent->n <= disp->mgr->buffersize);
3434 newsevent = (isc_socketevent_t *)
3435 isc_event_allocate(disp->mgr->mctx, NULL,
3436 DNS_EVENT_IMPORTRECVDONE, udp_shrecv,
3437 disp, sizeof(isc_socketevent_t));
3438 if (newsevent == NULL)
3441 buf = allocate_udp_buffer(disp);
3443 isc_event_free(ISC_EVENT_PTR(&newsevent));
3446 memcpy(buf, sevent->region.base, sevent->n);
3447 newsevent->region.base = buf;
3448 newsevent->region.length = disp->mgr->buffersize;
3449 newsevent->n = sevent->n;
3450 newsevent->result = sevent->result;
3451 newsevent->address = sevent->address;
3452 newsevent->timestamp = sevent->timestamp;
3453 newsevent->pktinfo = sevent->pktinfo;
3454 newsevent->attributes = sevent->attributes;
3456 isc_task_send(disp->task[0], ISC_EVENT_PTR(&newsevent));
3461 dns_dispatchmgr_dump(dns_dispatchmgr_t *mgr) {
3462 dns_dispatch_t *disp;
3465 disp = ISC_LIST_HEAD(mgr->list);
3466 while (disp != NULL) {
3467 isc_sockaddr_format(&disp->local, foo, sizeof(foo));
3468 printf("\tdispatch %p, addr %s\n", disp, foo);
3469 disp = ISC_LIST_NEXT(disp, link);