2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2002 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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: request.c,v 1.64.2.1.10.6 2004/03/08 09:04:31 marka Exp $ */
22 #include <isc/magic.h>
25 #include <isc/timer.h>
29 #include <dns/compress.h>
30 #include <dns/dispatch.h>
31 #include <dns/events.h>
33 #include <dns/message.h>
34 #include <dns/rdata.h>
35 #include <dns/rdatastruct.h>
36 #include <dns/request.h>
37 #include <dns/result.h>
40 #define REQUESTMGR_MAGIC ISC_MAGIC('R', 'q', 'u', 'M')
41 #define VALID_REQUESTMGR(mgr) ISC_MAGIC_VALID(mgr, REQUESTMGR_MAGIC)
43 #define REQUEST_MAGIC ISC_MAGIC('R', 'q', 'u', '!')
44 #define VALID_REQUEST(request) ISC_MAGIC_VALID(request, REQUEST_MAGIC)
46 typedef ISC_LIST(dns_request_t) dns_requestlist_t;
48 #define DNS_REQUEST_NLOCKS 7
50 struct dns_requestmgr {
58 isc_timermgr_t *timermgr;
59 isc_socketmgr_t *socketmgr;
60 isc_taskmgr_t *taskmgr;
61 dns_dispatchmgr_t *dispatchmgr;
62 dns_dispatch_t *dispatchv4;
63 dns_dispatch_t *dispatchv6;
64 isc_boolean_t exiting;
65 isc_eventlist_t whenshutdown;
67 isc_mutex_t locks[DNS_REQUEST_NLOCKS];
68 dns_requestlist_t requests;
76 ISC_LINK(dns_request_t) link;
79 dns_requestevent_t *event;
80 dns_dispatch_t *dispatch;
81 dns_dispentry_t *dispentry;
83 dns_requestmgr_t *requestmgr;
85 dns_tsigkey_t *tsigkey;
87 isc_boolean_t canceling; /* ctlevent outstanding */
88 isc_sockaddr_t destaddr;
89 unsigned int udpcount;
92 #define DNS_REQUEST_F_CONNECTING 0x0001
93 #define DNS_REQUEST_F_SENDING 0x0002
94 #define DNS_REQUEST_F_CANCELED 0x0004 /* ctlevent received, or otherwise
95 synchronously canceled */
96 #define DNS_REQUEST_F_TIMEDOUT 0x0008 /* cancelled due to a timeout */
97 #define DNS_REQUEST_F_TCP 0x0010 /* This request used TCP */
98 #define DNS_REQUEST_CANCELED(r) \
99 (((r)->flags & DNS_REQUEST_F_CANCELED) != 0)
100 #define DNS_REQUEST_CONNECTING(r) \
101 (((r)->flags & DNS_REQUEST_F_CONNECTING) != 0)
102 #define DNS_REQUEST_SENDING(r) \
103 (((r)->flags & DNS_REQUEST_F_SENDING) != 0)
104 #define DNS_REQUEST_TIMEDOUT(r) \
105 (((r)->flags & DNS_REQUEST_F_TIMEDOUT) != 0)
112 static void mgr_destroy(dns_requestmgr_t *requestmgr);
113 static void mgr_shutdown(dns_requestmgr_t *requestmgr);
114 static unsigned int mgr_gethash(dns_requestmgr_t *requestmgr);
115 static void send_shutdown_events(dns_requestmgr_t *requestmgr);
117 static isc_result_t req_render(dns_message_t *message, isc_buffer_t **buffer,
118 unsigned int options, isc_mem_t *mctx);
119 static void req_senddone(isc_task_t *task, isc_event_t *event);
120 static void req_response(isc_task_t *task, isc_event_t *event);
121 static void req_timeout(isc_task_t *task, isc_event_t *event);
122 static void req_connected(isc_task_t *task, isc_event_t *event);
123 static void req_sendevent(dns_request_t *request, isc_result_t result);
124 static void req_cancel(dns_request_t *request);
125 static void req_destroy(dns_request_t *request);
126 static void req_log(int level, const char *fmt, ...) ISC_FORMAT_PRINTF(2, 3);
127 static void do_cancel(isc_task_t *task, isc_event_t *event);
134 dns_requestmgr_create(isc_mem_t *mctx,
135 isc_timermgr_t *timermgr,
136 isc_socketmgr_t *socketmgr,
137 isc_taskmgr_t *taskmgr,
138 dns_dispatchmgr_t *dispatchmgr,
139 dns_dispatch_t *dispatchv4,
140 dns_dispatch_t *dispatchv6,
141 dns_requestmgr_t **requestmgrp)
143 dns_requestmgr_t *requestmgr;
144 isc_socket_t *socket;
148 req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_create");
150 REQUIRE(requestmgrp != NULL && *requestmgrp == NULL);
151 REQUIRE(timermgr != NULL);
152 REQUIRE(socketmgr != NULL);
153 REQUIRE(taskmgr != NULL);
154 REQUIRE(dispatchmgr != NULL);
155 if (dispatchv4 != NULL) {
156 socket = dns_dispatch_getsocket(dispatchv4);
157 REQUIRE(isc_socket_gettype(socket) == isc_sockettype_udp);
159 if (dispatchv6 != NULL) {
160 socket = dns_dispatch_getsocket(dispatchv6);
161 REQUIRE(isc_socket_gettype(socket) == isc_sockettype_udp);
164 requestmgr = isc_mem_get(mctx, sizeof(*requestmgr));
165 if (requestmgr == NULL)
166 return (ISC_R_NOMEMORY);
168 result = isc_mutex_init(&requestmgr->lock);
169 if (result != ISC_R_SUCCESS) {
170 isc_mem_put(mctx, requestmgr, sizeof(*requestmgr));
173 for (i = 0; i < DNS_REQUEST_NLOCKS; i++) {
174 result = isc_mutex_init(&requestmgr->locks[i]);
175 if (result != ISC_R_SUCCESS) {
177 DESTROYLOCK(&requestmgr->locks[i]);
178 DESTROYLOCK(&requestmgr->lock);
179 isc_mem_put(mctx, requestmgr, sizeof(*requestmgr));
183 requestmgr->timermgr = timermgr;
184 requestmgr->socketmgr = socketmgr;
185 requestmgr->taskmgr = taskmgr;
186 requestmgr->dispatchmgr = dispatchmgr;
187 requestmgr->dispatchv4 = NULL;
188 if (dispatchv4 != NULL)
189 dns_dispatch_attach(dispatchv4, &requestmgr->dispatchv4);
190 requestmgr->dispatchv6 = NULL;
191 if (dispatchv6 != NULL)
192 dns_dispatch_attach(dispatchv6, &requestmgr->dispatchv6);
193 requestmgr->mctx = NULL;
194 isc_mem_attach(mctx, &requestmgr->mctx);
195 requestmgr->eref = 1; /* implict attach */
196 requestmgr->iref = 0;
197 ISC_LIST_INIT(requestmgr->whenshutdown);
198 ISC_LIST_INIT(requestmgr->requests);
199 requestmgr->exiting = ISC_FALSE;
200 requestmgr->hash = 0;
201 requestmgr->magic = REQUESTMGR_MAGIC;
203 req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_create: %p", requestmgr);
205 *requestmgrp = requestmgr;
206 return (ISC_R_SUCCESS);
210 dns_requestmgr_whenshutdown(dns_requestmgr_t *requestmgr, isc_task_t *task,
211 isc_event_t **eventp)
216 req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_whenshutdown");
218 REQUIRE(VALID_REQUESTMGR(requestmgr));
219 REQUIRE(eventp != NULL);
224 LOCK(&requestmgr->lock);
226 if (requestmgr->exiting) {
228 * We're already shutdown. Send the event.
230 event->ev_sender = requestmgr;
231 isc_task_send(task, &event);
234 isc_task_attach(task, &clone);
235 event->ev_sender = clone;
236 ISC_LIST_APPEND(requestmgr->whenshutdown, event, ev_link);
238 UNLOCK(&requestmgr->lock);
242 dns_requestmgr_shutdown(dns_requestmgr_t *requestmgr) {
244 REQUIRE(VALID_REQUESTMGR(requestmgr));
246 req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_shutdown: %p", requestmgr);
248 LOCK(&requestmgr->lock);
249 mgr_shutdown(requestmgr);
250 UNLOCK(&requestmgr->lock);
254 mgr_shutdown(dns_requestmgr_t *requestmgr) {
255 dns_request_t *request;
260 if (!requestmgr->exiting) {
261 requestmgr->exiting = ISC_TRUE;
262 for (request = ISC_LIST_HEAD(requestmgr->requests);
264 request = ISC_LIST_NEXT(request, link)) {
265 dns_request_cancel(request);
267 if (requestmgr->iref == 0) {
268 INSIST(ISC_LIST_EMPTY(requestmgr->requests));
269 send_shutdown_events(requestmgr);
275 requestmgr_attach(dns_requestmgr_t *source, dns_requestmgr_t **targetp) {
281 REQUIRE(VALID_REQUESTMGR(source));
282 REQUIRE(targetp != NULL && *targetp == NULL);
284 REQUIRE(!source->exiting);
289 req_log(ISC_LOG_DEBUG(3), "requestmgr_attach: %p: eref %d iref %d",
290 source, source->eref, source->iref);
294 requestmgr_detach(dns_requestmgr_t **requestmgrp) {
295 dns_requestmgr_t *requestmgr;
296 isc_boolean_t need_destroy = ISC_FALSE;
298 REQUIRE(requestmgrp != NULL);
299 requestmgr = *requestmgrp;
300 REQUIRE(VALID_REQUESTMGR(requestmgr));
303 LOCK(&requestmgr->lock);
304 INSIST(requestmgr->iref > 0);
307 req_log(ISC_LOG_DEBUG(3), "requestmgr_detach: %p: eref %d iref %d",
308 requestmgr, requestmgr->eref, requestmgr->iref);
310 if (requestmgr->iref == 0 && requestmgr->exiting) {
311 INSIST(ISC_LIST_HEAD(requestmgr->requests) == NULL);
312 send_shutdown_events(requestmgr);
313 if (requestmgr->eref == 0)
314 need_destroy = ISC_TRUE;
316 UNLOCK(&requestmgr->lock);
319 mgr_destroy(requestmgr);
323 dns_requestmgr_attach(dns_requestmgr_t *source, dns_requestmgr_t **targetp) {
325 REQUIRE(VALID_REQUESTMGR(source));
326 REQUIRE(targetp != NULL && *targetp == NULL);
327 REQUIRE(!source->exiting);
332 UNLOCK(&source->lock);
334 req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_attach: %p: eref %d iref %d",
335 source, source->eref, source->iref);
339 dns_requestmgr_detach(dns_requestmgr_t **requestmgrp) {
340 dns_requestmgr_t *requestmgr;
341 isc_boolean_t need_destroy = ISC_FALSE;
343 REQUIRE(requestmgrp != NULL);
344 requestmgr = *requestmgrp;
345 REQUIRE(VALID_REQUESTMGR(requestmgr));
347 LOCK(&requestmgr->lock);
348 INSIST(requestmgr->eref > 0);
351 req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_detach: %p: eref %d iref %d",
352 requestmgr, requestmgr->eref, requestmgr->iref);
354 if (requestmgr->eref == 0 && requestmgr->iref == 0) {
355 INSIST(requestmgr->exiting &&
356 ISC_LIST_HEAD(requestmgr->requests) == NULL);
357 need_destroy = ISC_TRUE;
359 UNLOCK(&requestmgr->lock);
362 mgr_destroy(requestmgr);
368 send_shutdown_events(dns_requestmgr_t *requestmgr) {
369 isc_event_t *event, *next_event;
372 req_log(ISC_LOG_DEBUG(3), "send_shutdown_events: %p", requestmgr);
375 * Caller must be holding the manager lock.
377 for (event = ISC_LIST_HEAD(requestmgr->whenshutdown);
379 event = next_event) {
380 next_event = ISC_LIST_NEXT(event, ev_link);
381 ISC_LIST_UNLINK(requestmgr->whenshutdown, event, ev_link);
382 etask = event->ev_sender;
383 event->ev_sender = requestmgr;
384 isc_task_sendanddetach(&etask, &event);
389 mgr_destroy(dns_requestmgr_t *requestmgr) {
393 req_log(ISC_LOG_DEBUG(3), "mgr_destroy");
395 REQUIRE(requestmgr->eref == 0);
396 REQUIRE(requestmgr->iref == 0);
398 DESTROYLOCK(&requestmgr->lock);
399 for (i = 0; i < DNS_REQUEST_NLOCKS; i++)
400 DESTROYLOCK(&requestmgr->locks[i]);
401 if (requestmgr->dispatchv4 != NULL)
402 dns_dispatch_detach(&requestmgr->dispatchv4);
403 if (requestmgr->dispatchv6 != NULL)
404 dns_dispatch_detach(&requestmgr->dispatchv6);
405 requestmgr->magic = 0;
406 mctx = requestmgr->mctx;
407 isc_mem_put(mctx, requestmgr, sizeof(*requestmgr));
408 isc_mem_detach(&mctx);
412 mgr_gethash(dns_requestmgr_t *requestmgr) {
413 req_log(ISC_LOG_DEBUG(3), "mgr_gethash");
418 return (requestmgr->hash % DNS_REQUEST_NLOCKS);
421 static inline isc_result_t
422 req_send(dns_request_t *request, isc_task_t *task, isc_sockaddr_t *address) {
424 isc_socket_t *socket;
427 req_log(ISC_LOG_DEBUG(3), "req_send: request %p", request);
429 REQUIRE(VALID_REQUEST(request));
430 socket = dns_dispatch_getsocket(request->dispatch);
431 isc_buffer_usedregion(request->query, &r);
432 result = isc_socket_sendto(socket, &r, task, req_senddone,
433 request, address, NULL);
434 if (result == ISC_R_SUCCESS)
435 request->flags |= DNS_REQUEST_F_SENDING;
440 new_request(isc_mem_t *mctx, dns_request_t **requestp) {
441 dns_request_t *request;
443 request = isc_mem_get(mctx, sizeof(*request));
445 return (ISC_R_NOMEMORY);
451 request->mctx = NULL;
453 ISC_LINK_INIT(request, link);
454 request->query = NULL;
455 request->answer = NULL;
456 request->event = NULL;
457 request->dispatch = NULL;
458 request->dispentry = NULL;
459 request->timer = NULL;
460 request->requestmgr = NULL;
461 request->tsig = NULL;
462 request->tsigkey = NULL;
463 ISC_EVENT_INIT(&request->ctlevent, sizeof(request->ctlevent), 0, NULL,
464 DNS_EVENT_REQUESTCONTROL, do_cancel, request, NULL,
466 request->canceling = ISC_FALSE;
467 request->udpcount = 0;
469 isc_mem_attach(mctx, &request->mctx);
471 request->magic = REQUEST_MAGIC;
473 return (ISC_R_SUCCESS);
478 isblackholed(dns_dispatchmgr_t *dispatchmgr, isc_sockaddr_t *destaddr) {
479 dns_acl_t *blackhole;
480 isc_netaddr_t netaddr;
482 isc_boolean_t drop = ISC_FALSE;
483 char netaddrstr[ISC_NETADDR_FORMATSIZE];
485 blackhole = dns_dispatchmgr_getblackhole(dispatchmgr);
486 if (blackhole != NULL) {
487 isc_netaddr_fromsockaddr(&netaddr, destaddr);
488 if (dns_acl_match(&netaddr, NULL, blackhole,
489 NULL, &match, NULL) == ISC_R_SUCCESS &&
494 isc_netaddr_format(&netaddr, netaddrstr, sizeof(netaddrstr));
495 req_log(ISC_LOG_DEBUG(10), "blackholed address %s", netaddrstr);
501 create_tcp_dispatch(dns_requestmgr_t *requestmgr, isc_sockaddr_t *srcaddr,
502 isc_sockaddr_t *destaddr, dns_dispatch_t **dispatchp)
505 isc_socket_t *socket = NULL;
508 isc_sockaddr_t bind_any;
510 result = isc_socket_create(requestmgr->socketmgr,
511 isc_sockaddr_pf(destaddr),
512 isc_sockettype_tcp, &socket);
513 if (result != ISC_R_SUCCESS)
515 if (srcaddr == NULL) {
516 isc_sockaddr_anyofpf(&bind_any,
517 isc_sockaddr_pf(destaddr));
518 result = isc_socket_bind(socket, &bind_any);
521 isc_sockaddr_setport(&src, 0);
522 result = isc_socket_bind(socket, &src);
524 if (result != ISC_R_SUCCESS)
527 attrs |= DNS_DISPATCHATTR_TCP;
528 attrs |= DNS_DISPATCHATTR_PRIVATE;
529 if (isc_sockaddr_pf(destaddr) == AF_INET)
530 attrs |= DNS_DISPATCHATTR_IPV4;
532 attrs |= DNS_DISPATCHATTR_IPV6;
533 attrs |= DNS_DISPATCHATTR_MAKEQUERY;
534 result = dns_dispatch_createtcp(requestmgr->dispatchmgr,
535 socket, requestmgr->taskmgr,
536 4096, 2, 1, 1, 3, attrs,
539 isc_socket_detach(&socket);
544 find_udp_dispatch(dns_requestmgr_t *requestmgr, isc_sockaddr_t *srcaddr,
545 isc_sockaddr_t *destaddr, dns_dispatch_t **dispatchp)
547 dns_dispatch_t *disp = NULL;
548 unsigned int attrs, attrmask;
550 if (srcaddr == NULL) {
551 switch (isc_sockaddr_pf(destaddr)) {
553 disp = requestmgr->dispatchv4;
557 disp = requestmgr->dispatchv6;
561 return (ISC_R_NOTIMPLEMENTED);
564 return (ISC_R_FAMILYNOSUPPORT);
565 dns_dispatch_attach(disp, dispatchp);
566 return (ISC_R_SUCCESS);
569 attrs |= DNS_DISPATCHATTR_UDP;
570 switch (isc_sockaddr_pf(srcaddr)) {
572 attrs |= DNS_DISPATCHATTR_IPV4;
576 attrs |= DNS_DISPATCHATTR_IPV6;
580 return (ISC_R_NOTIMPLEMENTED);
583 attrmask |= DNS_DISPATCHATTR_UDP;
584 attrmask |= DNS_DISPATCHATTR_TCP;
585 attrmask |= DNS_DISPATCHATTR_IPV4;
586 attrmask |= DNS_DISPATCHATTR_IPV6;
587 return (dns_dispatch_getudp(requestmgr->dispatchmgr,
588 requestmgr->socketmgr,
591 1000, 32768, 16411, 16433,
597 get_dispatch(isc_boolean_t tcp, dns_requestmgr_t *requestmgr,
598 isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
599 dns_dispatch_t **dispatchp)
603 result = create_tcp_dispatch(requestmgr, srcaddr,
604 destaddr, dispatchp);
606 result = find_udp_dispatch(requestmgr, srcaddr,
607 destaddr, dispatchp);
612 set_timer(isc_timer_t *timer, unsigned int timeout, unsigned int udpresend) {
614 isc_interval_t interval;
616 isc_timertype_t timertype;
618 isc_interval_set(&interval, timeout, 0);
619 result = isc_time_nowplusinterval(&expires, &interval);
620 isc_interval_set(&interval, udpresend, 0);
622 timertype = udpresend != 0 ? isc_timertype_limited : isc_timertype_once;
623 if (result == ISC_R_SUCCESS)
624 result = isc_timer_reset(timer, timertype, &expires,
625 &interval, ISC_FALSE);
630 dns_request_createraw(dns_requestmgr_t *requestmgr, isc_buffer_t *msgbuf,
631 isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
632 unsigned int options, unsigned int timeout,
633 isc_task_t *task, isc_taskaction_t action, void *arg,
634 dns_request_t **requestp)
636 return(dns_request_createraw3(requestmgr, msgbuf, srcaddr, destaddr,
637 options, timeout, 0, 0, task, action,
642 dns_request_createraw2(dns_requestmgr_t *requestmgr, isc_buffer_t *msgbuf,
643 isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
644 unsigned int options, unsigned int timeout,
645 unsigned int udptimeout, isc_task_t *task,
646 isc_taskaction_t action, void *arg,
647 dns_request_t **requestp)
649 unsigned int udpretries = 0;
652 udpretries = timeout / udptimeout;
654 return (dns_request_createraw3(requestmgr, msgbuf, srcaddr, destaddr,
655 options, timeout, udptimeout,
656 udpretries, task, action, arg,
661 dns_request_createraw3(dns_requestmgr_t *requestmgr, isc_buffer_t *msgbuf,
662 isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
663 unsigned int options, unsigned int timeout,
664 unsigned int udptimeout, unsigned int udpretries,
665 isc_task_t *task, isc_taskaction_t action, void *arg,
666 dns_request_t **requestp)
668 dns_request_t *request = NULL;
669 isc_task_t *tclone = NULL;
670 isc_socket_t *socket = NULL;
674 isc_boolean_t tcp = ISC_FALSE;
677 REQUIRE(VALID_REQUESTMGR(requestmgr));
678 REQUIRE(msgbuf != NULL);
679 REQUIRE(destaddr != NULL);
680 REQUIRE(task != NULL);
681 REQUIRE(action != NULL);
682 REQUIRE(requestp != NULL && *requestp == NULL);
683 REQUIRE(timeout > 0);
685 REQUIRE(isc_sockaddr_pf(srcaddr) == isc_sockaddr_pf(destaddr));
687 mctx = requestmgr->mctx;
689 req_log(ISC_LOG_DEBUG(3), "dns_request_createraw");
691 if (isblackholed(requestmgr->dispatchmgr, destaddr))
692 return (DNS_R_BLACKHOLED);
695 result = new_request(mctx, &request);
696 if (result != ISC_R_SUCCESS)
699 if (udptimeout == 0 && udpretries != 0) {
700 udptimeout = timeout / (udpretries + 1);
706 * Create timer now. We will set it below once.
708 result = isc_timer_create(requestmgr->timermgr, isc_timertype_inactive,
709 NULL, NULL, task, req_timeout, request,
711 if (result != ISC_R_SUCCESS)
714 request->event = (dns_requestevent_t *)
715 isc_event_allocate(mctx, task, DNS_EVENT_REQUESTDONE,
716 action, arg, sizeof(dns_requestevent_t));
717 if (request->event == NULL) {
718 result = ISC_R_NOMEMORY;
721 isc_task_attach(task, &tclone);
722 request->event->ev_sender = task;
723 request->event->request = request;
724 request->event->result = ISC_R_FAILURE;
726 isc_buffer_usedregion(msgbuf, &r);
727 if (r.length < DNS_MESSAGE_HEADERLEN || r.length > 65535) {
728 result = DNS_R_FORMERR;
732 if ((options & DNS_REQUESTOPT_TCP) != 0 || r.length > 512)
735 result = get_dispatch(tcp, requestmgr, srcaddr, destaddr,
737 if (result != ISC_R_SUCCESS)
740 socket = dns_dispatch_getsocket(request->dispatch);
741 INSIST(socket != NULL);
742 result = dns_dispatch_addresponse(request->dispatch, destaddr, task,
743 req_response, request, &id,
744 &request->dispentry);
745 if (result != ISC_R_SUCCESS)
748 result = isc_buffer_allocate(mctx, &request->query,
749 r.length + (tcp ? 2 : 0));
750 if (result != ISC_R_SUCCESS)
753 isc_buffer_putuint16(request->query, (isc_uint16_t)r.length);
754 result = isc_buffer_copyregion(request->query, &r);
755 if (result != ISC_R_SUCCESS)
758 /* Add message ID. */
759 isc_buffer_usedregion(request->query, &r);
761 isc_region_consume(&r, 2);
762 r.base[0] = (id>>8) & 0xff;
763 r.base[1] = id & 0xff;
765 LOCK(&requestmgr->lock);
766 if (requestmgr->exiting) {
767 UNLOCK(&requestmgr->lock);
768 result = ISC_R_SHUTTINGDOWN;
771 requestmgr_attach(requestmgr, &request->requestmgr);
772 request->hash = mgr_gethash(requestmgr);
773 ISC_LIST_APPEND(requestmgr->requests, request, link);
774 UNLOCK(&requestmgr->lock);
776 result = set_timer(request->timer, timeout, tcp ? 0 : udptimeout);
777 if (result != ISC_R_SUCCESS)
780 request->destaddr = *destaddr;
782 result = isc_socket_connect(socket, destaddr, task,
783 req_connected, request);
784 if (result != ISC_R_SUCCESS)
786 request->flags |= DNS_REQUEST_F_CONNECTING|DNS_REQUEST_F_TCP;
788 result = req_send(request, task, destaddr);
789 if (result != ISC_R_SUCCESS)
793 req_log(ISC_LOG_DEBUG(3), "dns_request_createraw: request %p",
796 return (ISC_R_SUCCESS);
799 LOCK(&requestmgr->lock);
800 ISC_LIST_UNLINK(requestmgr->requests, request, link);
801 UNLOCK(&requestmgr->lock);
805 isc_task_detach(&tclone);
806 req_destroy(request);
807 req_log(ISC_LOG_DEBUG(3), "dns_request_createraw: failed %s",
808 dns_result_totext(result));
813 dns_request_create(dns_requestmgr_t *requestmgr, dns_message_t *message,
814 isc_sockaddr_t *address, unsigned int options,
816 unsigned int timeout, isc_task_t *task,
817 isc_taskaction_t action, void *arg,
818 dns_request_t **requestp)
820 return (dns_request_createvia3(requestmgr, message, NULL, address,
821 options, key, timeout, 0, 0, task,
822 action, arg, requestp));
826 dns_request_createvia(dns_requestmgr_t *requestmgr, dns_message_t *message,
827 isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
828 unsigned int options, dns_tsigkey_t *key,
829 unsigned int timeout, isc_task_t *task,
830 isc_taskaction_t action, void *arg,
831 dns_request_t **requestp)
833 return(dns_request_createvia3(requestmgr, message, srcaddr, destaddr,
834 options, key, timeout, 0, 0, task,
835 action, arg, requestp));
839 dns_request_createvia2(dns_requestmgr_t *requestmgr, dns_message_t *message,
840 isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
841 unsigned int options, dns_tsigkey_t *key,
842 unsigned int timeout, unsigned int udptimeout,
843 isc_task_t *task, isc_taskaction_t action, void *arg,
844 dns_request_t **requestp)
846 unsigned int udpretries = 0;
849 udpretries = timeout / udptimeout;
850 return (dns_request_createvia3(requestmgr, message, srcaddr, destaddr,
851 options, key, timeout, udptimeout,
852 udpretries, task, action, arg,
857 dns_request_createvia3(dns_requestmgr_t *requestmgr, dns_message_t *message,
858 isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
859 unsigned int options, dns_tsigkey_t *key,
860 unsigned int timeout, unsigned int udptimeout,
861 unsigned int udpretries, isc_task_t *task,
862 isc_taskaction_t action, void *arg,
863 dns_request_t **requestp)
865 dns_request_t *request = NULL;
866 isc_task_t *tclone = NULL;
867 isc_socket_t *socket = NULL;
872 isc_boolean_t setkey = ISC_TRUE;
874 REQUIRE(VALID_REQUESTMGR(requestmgr));
875 REQUIRE(message != NULL);
876 REQUIRE(destaddr != NULL);
877 REQUIRE(task != NULL);
878 REQUIRE(action != NULL);
879 REQUIRE(requestp != NULL && *requestp == NULL);
880 REQUIRE(timeout > 0);
882 REQUIRE(isc_sockaddr_pf(srcaddr) == isc_sockaddr_pf(destaddr));
884 mctx = requestmgr->mctx;
886 req_log(ISC_LOG_DEBUG(3), "dns_request_createvia");
888 if (isblackholed(requestmgr->dispatchmgr, destaddr))
889 return (DNS_R_BLACKHOLED);
892 result = new_request(mctx, &request);
893 if (result != ISC_R_SUCCESS)
896 if (udptimeout == 0 && udpretries != 0) {
897 udptimeout = timeout / (udpretries + 1);
903 * Create timer now. We will set it below once.
905 result = isc_timer_create(requestmgr->timermgr, isc_timertype_inactive,
906 NULL, NULL, task, req_timeout, request,
908 if (result != ISC_R_SUCCESS)
911 request->event = (dns_requestevent_t *)
912 isc_event_allocate(mctx, task, DNS_EVENT_REQUESTDONE,
913 action, arg, sizeof(dns_requestevent_t));
914 if (request->event == NULL) {
915 result = ISC_R_NOMEMORY;
918 isc_task_attach(task, &tclone);
919 request->event->ev_sender = task;
920 request->event->request = request;
921 request->event->result = ISC_R_FAILURE;
923 dns_tsigkey_attach(key, &request->tsigkey);
926 tcp = ISC_TF((options & DNS_REQUESTOPT_TCP) != 0);
927 result = get_dispatch(tcp, requestmgr, srcaddr, destaddr,
929 if (result != ISC_R_SUCCESS)
932 socket = dns_dispatch_getsocket(request->dispatch);
933 INSIST(socket != NULL);
934 result = dns_dispatch_addresponse(request->dispatch, destaddr, task,
935 req_response, request, &id,
936 &request->dispentry);
937 if (result != ISC_R_SUCCESS)
942 result = dns_message_settsigkey(message, request->tsigkey);
943 if (result != ISC_R_SUCCESS)
946 result = req_render(message, &request->query, options, mctx);
947 if (result == DNS_R_USETCP &&
948 (options & DNS_REQUESTOPT_TCP) == 0) {
950 * Try again using TCP.
952 dns_message_renderreset(message);
953 dns_dispatch_removeresponse(&request->dispentry, NULL);
954 dns_dispatch_detach(&request->dispatch);
956 options |= DNS_REQUESTOPT_TCP;
960 if (result != ISC_R_SUCCESS)
963 result = dns_message_getquerytsig(message, mctx, &request->tsig);
964 if (result != ISC_R_SUCCESS)
967 LOCK(&requestmgr->lock);
968 if (requestmgr->exiting) {
969 UNLOCK(&requestmgr->lock);
970 result = ISC_R_SHUTTINGDOWN;
973 requestmgr_attach(requestmgr, &request->requestmgr);
974 request->hash = mgr_gethash(requestmgr);
975 ISC_LIST_APPEND(requestmgr->requests, request, link);
976 UNLOCK(&requestmgr->lock);
978 result = set_timer(request->timer, timeout, tcp ? 0 : udptimeout);
979 if (result != ISC_R_SUCCESS)
982 request->destaddr = *destaddr;
984 result = isc_socket_connect(socket, destaddr, task,
985 req_connected, request);
986 if (result != ISC_R_SUCCESS)
988 request->flags |= DNS_REQUEST_F_CONNECTING|DNS_REQUEST_F_TCP;
990 result = req_send(request, task, destaddr);
991 if (result != ISC_R_SUCCESS)
995 req_log(ISC_LOG_DEBUG(3), "dns_request_createvia: request %p",
998 return (ISC_R_SUCCESS);
1001 LOCK(&requestmgr->lock);
1002 ISC_LIST_UNLINK(requestmgr->requests, request, link);
1003 UNLOCK(&requestmgr->lock);
1007 isc_task_detach(&tclone);
1008 req_destroy(request);
1009 req_log(ISC_LOG_DEBUG(3), "dns_request_createvia: failed %s",
1010 dns_result_totext(result));
1015 req_render(dns_message_t *message, isc_buffer_t **bufferp,
1016 unsigned int options, isc_mem_t *mctx)
1018 isc_buffer_t *buf1 = NULL;
1019 isc_buffer_t *buf2 = NULL;
1020 isc_result_t result;
1022 isc_boolean_t tcp = ISC_FALSE;
1023 dns_compress_t cctx;
1024 isc_boolean_t cleanup_cctx = ISC_FALSE;
1026 REQUIRE(bufferp != NULL && *bufferp == NULL);
1028 req_log(ISC_LOG_DEBUG(3), "request_render");
1031 * Create buffer able to hold largest possible message.
1033 result = isc_buffer_allocate(mctx, &buf1, 65535);
1034 if (result != ISC_R_SUCCESS)
1037 result = dns_compress_init(&cctx, -1, mctx);
1038 if (result != ISC_R_SUCCESS)
1040 cleanup_cctx = ISC_TRUE;
1045 result = dns_message_renderbegin(message, &cctx, buf1);
1046 if (result != ISC_R_SUCCESS)
1048 result = dns_message_rendersection(message, DNS_SECTION_QUESTION, 0);
1049 if (result != ISC_R_SUCCESS)
1051 result = dns_message_rendersection(message, DNS_SECTION_ANSWER, 0);
1052 if (result != ISC_R_SUCCESS)
1054 result = dns_message_rendersection(message, DNS_SECTION_AUTHORITY, 0);
1055 if (result != ISC_R_SUCCESS)
1057 result = dns_message_rendersection(message, DNS_SECTION_ADDITIONAL, 0);
1058 if (result != ISC_R_SUCCESS)
1060 result = dns_message_renderend(message);
1061 if (result != ISC_R_SUCCESS)
1064 dns_compress_invalidate(&cctx);
1065 cleanup_cctx = ISC_FALSE;
1068 * Copy rendered message to exact sized buffer.
1070 isc_buffer_usedregion(buf1, &r);
1071 if ((options & DNS_REQUESTOPT_TCP) != 0) {
1073 } else if (r.length > 512) {
1074 result = DNS_R_USETCP;
1077 result = isc_buffer_allocate(mctx, &buf2, r.length + (tcp ? 2 : 0));
1078 if (result != ISC_R_SUCCESS)
1081 isc_buffer_putuint16(buf2, (isc_uint16_t)r.length);
1082 result = isc_buffer_copyregion(buf2, &r);
1083 if (result != ISC_R_SUCCESS)
1087 * Cleanup and return.
1089 isc_buffer_free(&buf1);
1091 return (ISC_R_SUCCESS);
1094 dns_message_renderreset(message);
1096 isc_buffer_free(&buf1);
1098 isc_buffer_free(&buf2);
1100 dns_compress_invalidate(&cctx);
1106 * If this request is no longer waiting for events,
1107 * send the completion event. This will ultimately
1108 * cause the request to be destroyed.
1111 * 'request' is locked by the caller.
1114 send_if_done(dns_request_t *request, isc_result_t result) {
1115 if (!DNS_REQUEST_CONNECTING(request) &&
1116 !DNS_REQUEST_SENDING(request) &&
1117 !request->canceling)
1118 req_sendevent(request, result);
1122 * Handle the control event.
1125 do_cancel(isc_task_t *task, isc_event_t *event) {
1126 dns_request_t *request = event->ev_arg;
1128 INSIST(event->ev_type == DNS_EVENT_REQUESTCONTROL);
1129 LOCK(&request->requestmgr->locks[request->hash]);
1130 request->canceling = ISC_FALSE;
1131 if (!DNS_REQUEST_CANCELED(request))
1132 req_cancel(request);
1133 send_if_done(request, ISC_R_CANCELED);
1134 UNLOCK(&request->requestmgr->locks[request->hash]);
1138 dns_request_cancel(dns_request_t *request) {
1139 REQUIRE(VALID_REQUEST(request));
1141 req_log(ISC_LOG_DEBUG(3), "dns_request_cancel: request %p", request);
1143 REQUIRE(VALID_REQUEST(request));
1145 LOCK(&request->requestmgr->locks[request->hash]);
1146 if (!request->canceling && !DNS_REQUEST_CANCELED(request)) {
1147 isc_event_t *ev = &request->ctlevent;
1148 isc_task_send(request->event->ev_sender, &ev);
1149 request->canceling = ISC_TRUE;
1151 UNLOCK(&request->requestmgr->locks[request->hash]);
1155 dns_request_getresponse(dns_request_t *request, dns_message_t *message,
1156 unsigned int options)
1158 isc_result_t result;
1160 REQUIRE(VALID_REQUEST(request));
1161 REQUIRE(request->answer != NULL);
1163 req_log(ISC_LOG_DEBUG(3), "dns_request_getresponse: request %p",
1166 result = dns_message_setquerytsig(message, request->tsig);
1167 if (result != ISC_R_SUCCESS)
1169 result = dns_message_settsigkey(message, request->tsigkey);
1170 if (result != ISC_R_SUCCESS)
1172 result = dns_message_parse(message, request->answer, options);
1173 if (result != ISC_R_SUCCESS)
1175 if (request->tsigkey != NULL)
1176 result = dns_tsig_verify(request->answer, message, NULL, NULL);
1181 dns_request_usedtcp(dns_request_t *request) {
1182 REQUIRE(VALID_REQUEST(request));
1184 return (ISC_TF((request->flags & DNS_REQUEST_F_TCP) != 0));
1188 dns_request_destroy(dns_request_t **requestp) {
1189 dns_request_t *request;
1191 REQUIRE(requestp != NULL && VALID_REQUEST(*requestp));
1193 request = *requestp;
1195 req_log(ISC_LOG_DEBUG(3), "dns_request_destroy: request %p", request);
1197 LOCK(&request->requestmgr->lock);
1198 LOCK(&request->requestmgr->locks[request->hash]);
1199 ISC_LIST_UNLINK(request->requestmgr->requests, request, link);
1200 INSIST(!DNS_REQUEST_CONNECTING(request));
1201 INSIST(!DNS_REQUEST_SENDING(request));
1202 UNLOCK(&request->requestmgr->locks[request->hash]);
1203 UNLOCK(&request->requestmgr->lock);
1206 * These should have been cleaned up by req_cancel() before
1207 * the completion event was sent.
1209 INSIST(!ISC_LINK_LINKED(request, link));
1210 INSIST(request->dispentry == NULL);
1211 INSIST(request->dispatch == NULL);
1212 INSIST(request->timer == NULL);
1214 req_destroy(request);
1220 *** Private: request.
1224 req_connected(isc_task_t *task, isc_event_t *event) {
1225 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1226 isc_result_t result;
1227 dns_request_t *request = event->ev_arg;
1229 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
1230 REQUIRE(VALID_REQUEST(request));
1231 REQUIRE(DNS_REQUEST_CONNECTING(request));
1233 req_log(ISC_LOG_DEBUG(3), "req_connected: request %p", request);
1235 LOCK(&request->requestmgr->locks[request->hash]);
1236 request->flags &= ~DNS_REQUEST_F_CONNECTING;
1238 if (DNS_REQUEST_CANCELED(request)) {
1240 * Send delayed event.
1242 if (DNS_REQUEST_TIMEDOUT(request))
1243 send_if_done(request, ISC_R_TIMEDOUT);
1245 send_if_done(request, ISC_R_CANCELED);
1247 dns_dispatch_starttcp(request->dispatch);
1248 result = sevent->result;
1249 if (result == ISC_R_SUCCESS)
1250 result = req_send(request, task, NULL);
1252 if (result != ISC_R_SUCCESS) {
1253 req_cancel(request);
1254 send_if_done(request, ISC_R_CANCELED);
1257 UNLOCK(&request->requestmgr->locks[request->hash]);
1258 isc_event_free(&event);
1262 req_senddone(isc_task_t *task, isc_event_t *event) {
1263 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1264 dns_request_t *request = event->ev_arg;
1266 REQUIRE(event->ev_type == ISC_SOCKEVENT_SENDDONE);
1267 REQUIRE(VALID_REQUEST(request));
1268 REQUIRE(DNS_REQUEST_SENDING(request));
1270 req_log(ISC_LOG_DEBUG(3), "req_senddone: request %p", request);
1274 LOCK(&request->requestmgr->locks[request->hash]);
1275 request->flags &= ~DNS_REQUEST_F_SENDING;
1277 if (DNS_REQUEST_CANCELED(request)) {
1279 * Send delayed event.
1281 if (DNS_REQUEST_TIMEDOUT(request))
1282 send_if_done(request, ISC_R_TIMEDOUT);
1284 send_if_done(request, ISC_R_CANCELED);
1285 } else if (sevent->result != ISC_R_SUCCESS) {
1286 req_cancel(request);
1287 send_if_done(request, ISC_R_CANCELED);
1289 UNLOCK(&request->requestmgr->locks[request->hash]);
1291 isc_event_free(&event);
1295 req_response(isc_task_t *task, isc_event_t *event) {
1296 isc_result_t result;
1297 dns_request_t *request = event->ev_arg;
1298 dns_dispatchevent_t *devent = (dns_dispatchevent_t *)event;
1301 REQUIRE(VALID_REQUEST(request));
1302 REQUIRE(event->ev_type == DNS_EVENT_DISPATCH);
1306 req_log(ISC_LOG_DEBUG(3), "req_response: request %p: %s", request,
1307 dns_result_totext(devent->result));
1309 LOCK(&request->requestmgr->locks[request->hash]);
1310 result = devent->result;
1311 if (result != ISC_R_SUCCESS)
1315 * Copy buffer to request.
1317 isc_buffer_usedregion(&devent->buffer, &r);
1318 result = isc_buffer_allocate(request->mctx, &request->answer,
1320 if (result != ISC_R_SUCCESS)
1322 result = isc_buffer_copyregion(request->answer, &r);
1323 if (result != ISC_R_SUCCESS)
1324 isc_buffer_free(&request->answer);
1329 dns_dispatch_removeresponse(&request->dispentry, &devent);
1330 req_cancel(request);
1332 * Send completion event.
1334 send_if_done(request, result);
1335 UNLOCK(&request->requestmgr->locks[request->hash]);
1339 req_timeout(isc_task_t *task, isc_event_t *event) {
1340 dns_request_t *request = event->ev_arg;
1341 isc_result_t result;
1343 REQUIRE(VALID_REQUEST(request));
1345 req_log(ISC_LOG_DEBUG(3), "req_timeout: request %p", request);
1348 LOCK(&request->requestmgr->locks[request->hash]);
1349 if (event->ev_type == ISC_TIMEREVENT_TICK &&
1350 request->udpcount-- != 0) {
1351 if (! DNS_REQUEST_SENDING(request)) {
1352 result = req_send(request, task, &request->destaddr);
1353 if (result != ISC_R_SUCCESS) {
1354 req_cancel(request);
1355 send_if_done(request, result);
1359 request->flags |= DNS_REQUEST_F_TIMEDOUT;
1360 req_cancel(request);
1361 send_if_done(request, ISC_R_TIMEDOUT);
1363 UNLOCK(&request->requestmgr->locks[request->hash]);
1364 isc_event_free(&event);
1368 req_sendevent(dns_request_t *request, isc_result_t result) {
1371 REQUIRE(VALID_REQUEST(request));
1373 req_log(ISC_LOG_DEBUG(3), "req_sendevent: request %p", request);
1376 * Lock held by caller.
1378 task = request->event->ev_sender;
1379 request->event->ev_sender = request;
1380 request->event->result = result;
1381 isc_task_sendanddetach(&task, (isc_event_t **)&request->event);
1385 req_destroy(dns_request_t *request) {
1388 REQUIRE(VALID_REQUEST(request));
1390 req_log(ISC_LOG_DEBUG(3), "req_destroy: request %p", request);
1393 if (request->query != NULL)
1394 isc_buffer_free(&request->query);
1395 if (request->answer != NULL)
1396 isc_buffer_free(&request->answer);
1397 if (request->event != NULL)
1398 isc_event_free((isc_event_t **)&request->event);
1399 if (request->dispentry != NULL)
1400 dns_dispatch_removeresponse(&request->dispentry, NULL);
1401 if (request->dispatch != NULL)
1402 dns_dispatch_detach(&request->dispatch);
1403 if (request->timer != NULL)
1404 isc_timer_detach(&request->timer);
1405 if (request->tsig != NULL)
1406 isc_buffer_free(&request->tsig);
1407 if (request->tsigkey != NULL)
1408 dns_tsigkey_detach(&request->tsigkey);
1409 if (request->requestmgr != NULL)
1410 requestmgr_detach(&request->requestmgr);
1411 mctx = request->mctx;
1412 isc_mem_put(mctx, request, sizeof(*request));
1413 isc_mem_detach(&mctx);
1417 * Stop the current request. Must be called from the request's task.
1420 req_cancel(dns_request_t *request) {
1421 isc_socket_t *socket;
1423 REQUIRE(VALID_REQUEST(request));
1425 req_log(ISC_LOG_DEBUG(3), "req_cancel: request %p", request);
1428 * Lock held by caller.
1430 request->flags |= DNS_REQUEST_F_CANCELED;
1432 if (request->timer != NULL)
1433 isc_timer_detach(&request->timer);
1434 if (request->dispentry != NULL)
1435 dns_dispatch_removeresponse(&request->dispentry, NULL);
1436 if (DNS_REQUEST_CONNECTING(request)) {
1437 socket = dns_dispatch_getsocket(request->dispatch);
1438 isc_socket_cancel(socket, NULL, ISC_SOCKCANCEL_CONNECT);
1440 if (DNS_REQUEST_SENDING(request)) {
1441 socket = dns_dispatch_getsocket(request->dispatch);
1442 isc_socket_cancel(socket, NULL, ISC_SOCKCANCEL_SEND);
1444 dns_dispatch_detach(&request->dispatch);
1448 req_log(int level, const char *fmt, ...) {
1452 isc_log_vwrite(dns_lctx, DNS_LOGCATEGORY_GENERAL,
1453 DNS_LOGMODULE_REQUEST, level, fmt, ap);