2 * Copyright (C) 2004-2010 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2002 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: request.c,v 1.87 2010-03-04 23:50:34 tbox Exp $ */
24 #include <isc/magic.h>
27 #include <isc/timer.h>
31 #include <dns/compress.h>
32 #include <dns/dispatch.h>
33 #include <dns/events.h>
35 #include <dns/message.h>
36 #include <dns/rdata.h>
37 #include <dns/rdatastruct.h>
38 #include <dns/request.h>
39 #include <dns/result.h>
42 #define REQUESTMGR_MAGIC ISC_MAGIC('R', 'q', 'u', 'M')
43 #define VALID_REQUESTMGR(mgr) ISC_MAGIC_VALID(mgr, REQUESTMGR_MAGIC)
45 #define REQUEST_MAGIC ISC_MAGIC('R', 'q', 'u', '!')
46 #define VALID_REQUEST(request) ISC_MAGIC_VALID(request, REQUEST_MAGIC)
48 typedef ISC_LIST(dns_request_t) dns_requestlist_t;
50 #define DNS_REQUEST_NLOCKS 7
52 struct dns_requestmgr {
60 isc_timermgr_t *timermgr;
61 isc_socketmgr_t *socketmgr;
62 isc_taskmgr_t *taskmgr;
63 dns_dispatchmgr_t *dispatchmgr;
64 dns_dispatch_t *dispatchv4;
65 dns_dispatch_t *dispatchv6;
66 isc_boolean_t exiting;
67 isc_eventlist_t whenshutdown;
69 isc_mutex_t locks[DNS_REQUEST_NLOCKS];
70 dns_requestlist_t requests;
78 ISC_LINK(dns_request_t) link;
81 dns_requestevent_t *event;
82 dns_dispatch_t *dispatch;
83 dns_dispentry_t *dispentry;
85 dns_requestmgr_t *requestmgr;
87 dns_tsigkey_t *tsigkey;
89 isc_boolean_t canceling; /* ctlevent outstanding */
90 isc_sockaddr_t destaddr;
91 unsigned int udpcount;
94 #define DNS_REQUEST_F_CONNECTING 0x0001
95 #define DNS_REQUEST_F_SENDING 0x0002
96 #define DNS_REQUEST_F_CANCELED 0x0004 /*%< ctlevent received, or otherwise
97 synchronously canceled */
98 #define DNS_REQUEST_F_TIMEDOUT 0x0008 /*%< canceled due to a timeout */
99 #define DNS_REQUEST_F_TCP 0x0010 /*%< This request used TCP */
100 #define DNS_REQUEST_CANCELED(r) \
101 (((r)->flags & DNS_REQUEST_F_CANCELED) != 0)
102 #define DNS_REQUEST_CONNECTING(r) \
103 (((r)->flags & DNS_REQUEST_F_CONNECTING) != 0)
104 #define DNS_REQUEST_SENDING(r) \
105 (((r)->flags & DNS_REQUEST_F_SENDING) != 0)
106 #define DNS_REQUEST_TIMEDOUT(r) \
107 (((r)->flags & DNS_REQUEST_F_TIMEDOUT) != 0)
114 static void mgr_destroy(dns_requestmgr_t *requestmgr);
115 static void mgr_shutdown(dns_requestmgr_t *requestmgr);
116 static unsigned int mgr_gethash(dns_requestmgr_t *requestmgr);
117 static void send_shutdown_events(dns_requestmgr_t *requestmgr);
119 static isc_result_t req_render(dns_message_t *message, isc_buffer_t **buffer,
120 unsigned int options, isc_mem_t *mctx);
121 static void req_senddone(isc_task_t *task, isc_event_t *event);
122 static void req_response(isc_task_t *task, isc_event_t *event);
123 static void req_timeout(isc_task_t *task, isc_event_t *event);
124 static isc_socket_t * req_getsocket(dns_request_t *request);
125 static void req_connected(isc_task_t *task, isc_event_t *event);
126 static void req_sendevent(dns_request_t *request, isc_result_t result);
127 static void req_cancel(dns_request_t *request);
128 static void req_destroy(dns_request_t *request);
129 static void req_log(int level, const char *fmt, ...) ISC_FORMAT_PRINTF(2, 3);
130 static void do_cancel(isc_task_t *task, isc_event_t *event);
137 dns_requestmgr_create(isc_mem_t *mctx,
138 isc_timermgr_t *timermgr,
139 isc_socketmgr_t *socketmgr,
140 isc_taskmgr_t *taskmgr,
141 dns_dispatchmgr_t *dispatchmgr,
142 dns_dispatch_t *dispatchv4,
143 dns_dispatch_t *dispatchv6,
144 dns_requestmgr_t **requestmgrp)
146 dns_requestmgr_t *requestmgr;
147 isc_socket_t *socket;
150 unsigned int dispattr;
152 req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_create");
154 REQUIRE(requestmgrp != NULL && *requestmgrp == NULL);
155 REQUIRE(timermgr != NULL);
156 REQUIRE(socketmgr != NULL);
157 REQUIRE(taskmgr != NULL);
158 REQUIRE(dispatchmgr != NULL);
160 if (dispatchv4 != NULL) {
161 dispattr = dns_dispatch_getattributes(dispatchv4);
162 REQUIRE((dispattr & DNS_DISPATCHATTR_UDP) != 0);
164 if (dispatchv6 != NULL) {
165 dispattr = dns_dispatch_getattributes(dispatchv6);
166 REQUIRE((dispattr & DNS_DISPATCHATTR_UDP) != 0);
169 requestmgr = isc_mem_get(mctx, sizeof(*requestmgr));
170 if (requestmgr == NULL)
171 return (ISC_R_NOMEMORY);
173 result = isc_mutex_init(&requestmgr->lock);
174 if (result != ISC_R_SUCCESS) {
175 isc_mem_put(mctx, requestmgr, sizeof(*requestmgr));
178 for (i = 0; i < DNS_REQUEST_NLOCKS; i++) {
179 result = isc_mutex_init(&requestmgr->locks[i]);
180 if (result != ISC_R_SUCCESS) {
182 DESTROYLOCK(&requestmgr->locks[i]);
183 DESTROYLOCK(&requestmgr->lock);
184 isc_mem_put(mctx, requestmgr, sizeof(*requestmgr));
188 requestmgr->timermgr = timermgr;
189 requestmgr->socketmgr = socketmgr;
190 requestmgr->taskmgr = taskmgr;
191 requestmgr->dispatchmgr = dispatchmgr;
192 requestmgr->dispatchv4 = NULL;
193 if (dispatchv4 != NULL)
194 dns_dispatch_attach(dispatchv4, &requestmgr->dispatchv4);
195 requestmgr->dispatchv6 = NULL;
196 if (dispatchv6 != NULL)
197 dns_dispatch_attach(dispatchv6, &requestmgr->dispatchv6);
198 requestmgr->mctx = NULL;
199 isc_mem_attach(mctx, &requestmgr->mctx);
200 requestmgr->eref = 1; /* implicit attach */
201 requestmgr->iref = 0;
202 ISC_LIST_INIT(requestmgr->whenshutdown);
203 ISC_LIST_INIT(requestmgr->requests);
204 requestmgr->exiting = ISC_FALSE;
205 requestmgr->hash = 0;
206 requestmgr->magic = REQUESTMGR_MAGIC;
208 req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_create: %p", requestmgr);
210 *requestmgrp = requestmgr;
211 return (ISC_R_SUCCESS);
215 dns_requestmgr_whenshutdown(dns_requestmgr_t *requestmgr, isc_task_t *task,
216 isc_event_t **eventp)
221 req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_whenshutdown");
223 REQUIRE(VALID_REQUESTMGR(requestmgr));
224 REQUIRE(eventp != NULL);
229 LOCK(&requestmgr->lock);
231 if (requestmgr->exiting) {
233 * We're already shutdown. Send the event.
235 event->ev_sender = requestmgr;
236 isc_task_send(task, &event);
239 isc_task_attach(task, &clone);
240 event->ev_sender = clone;
241 ISC_LIST_APPEND(requestmgr->whenshutdown, event, ev_link);
243 UNLOCK(&requestmgr->lock);
247 dns_requestmgr_shutdown(dns_requestmgr_t *requestmgr) {
249 REQUIRE(VALID_REQUESTMGR(requestmgr));
251 req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_shutdown: %p", requestmgr);
253 LOCK(&requestmgr->lock);
254 mgr_shutdown(requestmgr);
255 UNLOCK(&requestmgr->lock);
259 mgr_shutdown(dns_requestmgr_t *requestmgr) {
260 dns_request_t *request;
265 if (!requestmgr->exiting) {
266 requestmgr->exiting = ISC_TRUE;
267 for (request = ISC_LIST_HEAD(requestmgr->requests);
269 request = ISC_LIST_NEXT(request, link)) {
270 dns_request_cancel(request);
272 if (requestmgr->iref == 0) {
273 INSIST(ISC_LIST_EMPTY(requestmgr->requests));
274 send_shutdown_events(requestmgr);
280 requestmgr_attach(dns_requestmgr_t *source, dns_requestmgr_t **targetp) {
286 REQUIRE(VALID_REQUESTMGR(source));
287 REQUIRE(targetp != NULL && *targetp == NULL);
289 REQUIRE(!source->exiting);
294 req_log(ISC_LOG_DEBUG(3), "requestmgr_attach: %p: eref %d iref %d",
295 source, source->eref, source->iref);
299 requestmgr_detach(dns_requestmgr_t **requestmgrp) {
300 dns_requestmgr_t *requestmgr;
301 isc_boolean_t need_destroy = ISC_FALSE;
303 REQUIRE(requestmgrp != NULL);
304 requestmgr = *requestmgrp;
305 REQUIRE(VALID_REQUESTMGR(requestmgr));
308 LOCK(&requestmgr->lock);
309 INSIST(requestmgr->iref > 0);
312 req_log(ISC_LOG_DEBUG(3), "requestmgr_detach: %p: eref %d iref %d",
313 requestmgr, requestmgr->eref, requestmgr->iref);
315 if (requestmgr->iref == 0 && requestmgr->exiting) {
316 INSIST(ISC_LIST_HEAD(requestmgr->requests) == NULL);
317 send_shutdown_events(requestmgr);
318 if (requestmgr->eref == 0)
319 need_destroy = ISC_TRUE;
321 UNLOCK(&requestmgr->lock);
324 mgr_destroy(requestmgr);
328 dns_requestmgr_attach(dns_requestmgr_t *source, dns_requestmgr_t **targetp) {
330 REQUIRE(VALID_REQUESTMGR(source));
331 REQUIRE(targetp != NULL && *targetp == NULL);
332 REQUIRE(!source->exiting);
337 UNLOCK(&source->lock);
339 req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_attach: %p: eref %d iref %d",
340 source, source->eref, source->iref);
344 dns_requestmgr_detach(dns_requestmgr_t **requestmgrp) {
345 dns_requestmgr_t *requestmgr;
346 isc_boolean_t need_destroy = ISC_FALSE;
348 REQUIRE(requestmgrp != NULL);
349 requestmgr = *requestmgrp;
350 REQUIRE(VALID_REQUESTMGR(requestmgr));
352 LOCK(&requestmgr->lock);
353 INSIST(requestmgr->eref > 0);
356 req_log(ISC_LOG_DEBUG(3), "dns_requestmgr_detach: %p: eref %d iref %d",
357 requestmgr, requestmgr->eref, requestmgr->iref);
359 if (requestmgr->eref == 0 && requestmgr->iref == 0) {
360 INSIST(requestmgr->exiting &&
361 ISC_LIST_HEAD(requestmgr->requests) == NULL);
362 need_destroy = ISC_TRUE;
364 UNLOCK(&requestmgr->lock);
367 mgr_destroy(requestmgr);
373 send_shutdown_events(dns_requestmgr_t *requestmgr) {
374 isc_event_t *event, *next_event;
377 req_log(ISC_LOG_DEBUG(3), "send_shutdown_events: %p", requestmgr);
380 * Caller must be holding the manager lock.
382 for (event = ISC_LIST_HEAD(requestmgr->whenshutdown);
384 event = next_event) {
385 next_event = ISC_LIST_NEXT(event, ev_link);
386 ISC_LIST_UNLINK(requestmgr->whenshutdown, event, ev_link);
387 etask = event->ev_sender;
388 event->ev_sender = requestmgr;
389 isc_task_sendanddetach(&etask, &event);
394 mgr_destroy(dns_requestmgr_t *requestmgr) {
398 req_log(ISC_LOG_DEBUG(3), "mgr_destroy");
400 REQUIRE(requestmgr->eref == 0);
401 REQUIRE(requestmgr->iref == 0);
403 DESTROYLOCK(&requestmgr->lock);
404 for (i = 0; i < DNS_REQUEST_NLOCKS; i++)
405 DESTROYLOCK(&requestmgr->locks[i]);
406 if (requestmgr->dispatchv4 != NULL)
407 dns_dispatch_detach(&requestmgr->dispatchv4);
408 if (requestmgr->dispatchv6 != NULL)
409 dns_dispatch_detach(&requestmgr->dispatchv6);
410 requestmgr->magic = 0;
411 mctx = requestmgr->mctx;
412 isc_mem_put(mctx, requestmgr, sizeof(*requestmgr));
413 isc_mem_detach(&mctx);
417 mgr_gethash(dns_requestmgr_t *requestmgr) {
418 req_log(ISC_LOG_DEBUG(3), "mgr_gethash");
423 return (requestmgr->hash % DNS_REQUEST_NLOCKS);
426 static inline isc_result_t
427 req_send(dns_request_t *request, isc_task_t *task, isc_sockaddr_t *address) {
429 isc_socket_t *socket;
431 unsigned int dispattr;
433 req_log(ISC_LOG_DEBUG(3), "req_send: request %p", request);
435 REQUIRE(VALID_REQUEST(request));
436 dispattr = dns_dispatch_getattributes(request->dispatch);
437 socket = req_getsocket(request);
438 isc_buffer_usedregion(request->query, &r);
440 * We could connect the socket when we are using an exclusive dispatch
441 * as we do in resolver.c, but we prefer implementation simplicity
444 result = isc_socket_sendto(socket, &r, task, req_senddone,
445 request, address, NULL);
446 if (result == ISC_R_SUCCESS)
447 request->flags |= DNS_REQUEST_F_SENDING;
452 new_request(isc_mem_t *mctx, dns_request_t **requestp)
454 dns_request_t *request;
456 request = isc_mem_get(mctx, sizeof(*request));
458 return (ISC_R_NOMEMORY);
464 request->mctx = NULL;
466 ISC_LINK_INIT(request, link);
467 request->query = NULL;
468 request->answer = NULL;
469 request->event = NULL;
470 request->dispatch = NULL;
471 request->dispentry = NULL;
472 request->timer = NULL;
473 request->requestmgr = NULL;
474 request->tsig = NULL;
475 request->tsigkey = NULL;
476 ISC_EVENT_INIT(&request->ctlevent, sizeof(request->ctlevent), 0, NULL,
477 DNS_EVENT_REQUESTCONTROL, do_cancel, request, NULL,
479 request->canceling = ISC_FALSE;
480 request->udpcount = 0;
482 isc_mem_attach(mctx, &request->mctx);
484 request->magic = REQUEST_MAGIC;
486 return (ISC_R_SUCCESS);
491 isblackholed(dns_dispatchmgr_t *dispatchmgr, isc_sockaddr_t *destaddr) {
492 dns_acl_t *blackhole;
493 isc_netaddr_t netaddr;
495 isc_boolean_t drop = ISC_FALSE;
496 char netaddrstr[ISC_NETADDR_FORMATSIZE];
498 blackhole = dns_dispatchmgr_getblackhole(dispatchmgr);
499 if (blackhole != NULL) {
500 isc_netaddr_fromsockaddr(&netaddr, destaddr);
501 if (dns_acl_match(&netaddr, NULL, blackhole,
502 NULL, &match, NULL) == ISC_R_SUCCESS &&
507 isc_netaddr_format(&netaddr, netaddrstr, sizeof(netaddrstr));
508 req_log(ISC_LOG_DEBUG(10), "blackholed address %s", netaddrstr);
514 create_tcp_dispatch(dns_requestmgr_t *requestmgr, isc_sockaddr_t *srcaddr,
515 isc_sockaddr_t *destaddr, dns_dispatch_t **dispatchp)
518 isc_socket_t *socket = NULL;
521 isc_sockaddr_t bind_any;
523 result = isc_socket_create(requestmgr->socketmgr,
524 isc_sockaddr_pf(destaddr),
525 isc_sockettype_tcp, &socket);
526 if (result != ISC_R_SUCCESS)
528 #ifndef BROKEN_TCP_BIND_BEFORE_CONNECT
529 if (srcaddr == NULL) {
530 isc_sockaddr_anyofpf(&bind_any,
531 isc_sockaddr_pf(destaddr));
532 result = isc_socket_bind(socket, &bind_any, 0);
535 isc_sockaddr_setport(&src, 0);
536 result = isc_socket_bind(socket, &src, 0);
538 if (result != ISC_R_SUCCESS)
542 attrs |= DNS_DISPATCHATTR_TCP;
543 attrs |= DNS_DISPATCHATTR_PRIVATE;
544 if (isc_sockaddr_pf(destaddr) == AF_INET)
545 attrs |= DNS_DISPATCHATTR_IPV4;
547 attrs |= DNS_DISPATCHATTR_IPV6;
548 attrs |= DNS_DISPATCHATTR_MAKEQUERY;
549 result = dns_dispatch_createtcp(requestmgr->dispatchmgr,
550 socket, requestmgr->taskmgr,
551 4096, 2, 1, 1, 3, attrs,
554 isc_socket_detach(&socket);
559 find_udp_dispatch(dns_requestmgr_t *requestmgr, isc_sockaddr_t *srcaddr,
560 isc_sockaddr_t *destaddr, dns_dispatch_t **dispatchp)
562 dns_dispatch_t *disp = NULL;
563 unsigned int attrs, attrmask;
565 if (srcaddr == NULL) {
566 switch (isc_sockaddr_pf(destaddr)) {
568 disp = requestmgr->dispatchv4;
572 disp = requestmgr->dispatchv6;
576 return (ISC_R_NOTIMPLEMENTED);
579 return (ISC_R_FAMILYNOSUPPORT);
580 dns_dispatch_attach(disp, dispatchp);
581 return (ISC_R_SUCCESS);
584 attrs |= DNS_DISPATCHATTR_UDP;
585 switch (isc_sockaddr_pf(srcaddr)) {
587 attrs |= DNS_DISPATCHATTR_IPV4;
591 attrs |= DNS_DISPATCHATTR_IPV6;
595 return (ISC_R_NOTIMPLEMENTED);
598 attrmask |= DNS_DISPATCHATTR_UDP;
599 attrmask |= DNS_DISPATCHATTR_TCP;
600 attrmask |= DNS_DISPATCHATTR_IPV4;
601 attrmask |= DNS_DISPATCHATTR_IPV6;
602 return (dns_dispatch_getudp(requestmgr->dispatchmgr,
603 requestmgr->socketmgr,
606 1000, 32768, 16411, 16433,
612 get_dispatch(isc_boolean_t tcp, dns_requestmgr_t *requestmgr,
613 isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
614 dns_dispatch_t **dispatchp)
618 result = create_tcp_dispatch(requestmgr, srcaddr,
619 destaddr, dispatchp);
621 result = find_udp_dispatch(requestmgr, srcaddr,
622 destaddr, dispatchp);
627 set_timer(isc_timer_t *timer, unsigned int timeout, unsigned int udpresend) {
629 isc_interval_t interval;
631 isc_timertype_t timertype;
633 isc_interval_set(&interval, timeout, 0);
634 result = isc_time_nowplusinterval(&expires, &interval);
635 isc_interval_set(&interval, udpresend, 0);
637 timertype = udpresend != 0 ? isc_timertype_limited : isc_timertype_once;
638 if (result == ISC_R_SUCCESS)
639 result = isc_timer_reset(timer, timertype, &expires,
640 &interval, ISC_FALSE);
645 dns_request_createraw(dns_requestmgr_t *requestmgr, isc_buffer_t *msgbuf,
646 isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
647 unsigned int options, unsigned int timeout,
648 isc_task_t *task, isc_taskaction_t action, void *arg,
649 dns_request_t **requestp)
651 return(dns_request_createraw3(requestmgr, msgbuf, srcaddr, destaddr,
652 options, timeout, 0, 0, task, action,
657 dns_request_createraw2(dns_requestmgr_t *requestmgr, isc_buffer_t *msgbuf,
658 isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
659 unsigned int options, unsigned int timeout,
660 unsigned int udptimeout, isc_task_t *task,
661 isc_taskaction_t action, void *arg,
662 dns_request_t **requestp)
664 unsigned int udpretries = 0;
667 udpretries = timeout / udptimeout;
669 return (dns_request_createraw3(requestmgr, msgbuf, srcaddr, destaddr,
670 options, timeout, udptimeout,
671 udpretries, task, action, arg,
676 dns_request_createraw3(dns_requestmgr_t *requestmgr, isc_buffer_t *msgbuf,
677 isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
678 unsigned int options, unsigned int timeout,
679 unsigned int udptimeout, unsigned int udpretries,
680 isc_task_t *task, isc_taskaction_t action, void *arg,
681 dns_request_t **requestp)
683 dns_request_t *request = NULL;
684 isc_task_t *tclone = NULL;
685 isc_socket_t *socket = NULL;
689 isc_boolean_t tcp = ISC_FALSE;
692 REQUIRE(VALID_REQUESTMGR(requestmgr));
693 REQUIRE(msgbuf != NULL);
694 REQUIRE(destaddr != NULL);
695 REQUIRE(task != NULL);
696 REQUIRE(action != NULL);
697 REQUIRE(requestp != NULL && *requestp == NULL);
698 REQUIRE(timeout > 0);
700 REQUIRE(isc_sockaddr_pf(srcaddr) == isc_sockaddr_pf(destaddr));
702 mctx = requestmgr->mctx;
704 req_log(ISC_LOG_DEBUG(3), "dns_request_createraw");
706 if (isblackholed(requestmgr->dispatchmgr, destaddr))
707 return (DNS_R_BLACKHOLED);
710 result = new_request(mctx, &request);
711 if (result != ISC_R_SUCCESS)
714 if (udptimeout == 0 && udpretries != 0) {
715 udptimeout = timeout / (udpretries + 1);
719 request->udpcount = udpretries;
722 * Create timer now. We will set it below once.
724 result = isc_timer_create(requestmgr->timermgr, isc_timertype_inactive,
725 NULL, NULL, task, req_timeout, request,
727 if (result != ISC_R_SUCCESS)
730 request->event = (dns_requestevent_t *)
731 isc_event_allocate(mctx, task, DNS_EVENT_REQUESTDONE,
732 action, arg, sizeof(dns_requestevent_t));
733 if (request->event == NULL) {
734 result = ISC_R_NOMEMORY;
737 isc_task_attach(task, &tclone);
738 request->event->ev_sender = task;
739 request->event->request = request;
740 request->event->result = ISC_R_FAILURE;
742 isc_buffer_usedregion(msgbuf, &r);
743 if (r.length < DNS_MESSAGE_HEADERLEN || r.length > 65535) {
744 result = DNS_R_FORMERR;
748 if ((options & DNS_REQUESTOPT_TCP) != 0 || r.length > 512)
751 result = get_dispatch(tcp, requestmgr, srcaddr, destaddr,
753 if (result != ISC_R_SUCCESS)
756 result = dns_dispatch_addresponse2(request->dispatch, destaddr, task,
757 req_response, request, &id,
759 requestmgr->socketmgr);
760 if (result != ISC_R_SUCCESS)
763 socket = req_getsocket(request);
764 INSIST(socket != NULL);
766 result = isc_buffer_allocate(mctx, &request->query,
767 r.length + (tcp ? 2 : 0));
768 if (result != ISC_R_SUCCESS)
771 isc_buffer_putuint16(request->query, (isc_uint16_t)r.length);
772 result = isc_buffer_copyregion(request->query, &r);
773 if (result != ISC_R_SUCCESS)
776 /* Add message ID. */
777 isc_buffer_usedregion(request->query, &r);
779 isc_region_consume(&r, 2);
780 r.base[0] = (id>>8) & 0xff;
781 r.base[1] = id & 0xff;
783 LOCK(&requestmgr->lock);
784 if (requestmgr->exiting) {
785 UNLOCK(&requestmgr->lock);
786 result = ISC_R_SHUTTINGDOWN;
789 requestmgr_attach(requestmgr, &request->requestmgr);
790 request->hash = mgr_gethash(requestmgr);
791 ISC_LIST_APPEND(requestmgr->requests, request, link);
792 UNLOCK(&requestmgr->lock);
794 result = set_timer(request->timer, timeout, tcp ? 0 : udptimeout);
795 if (result != ISC_R_SUCCESS)
798 request->destaddr = *destaddr;
800 result = isc_socket_connect(socket, destaddr, task,
801 req_connected, request);
802 if (result != ISC_R_SUCCESS)
804 request->flags |= DNS_REQUEST_F_CONNECTING|DNS_REQUEST_F_TCP;
806 result = req_send(request, task, destaddr);
807 if (result != ISC_R_SUCCESS)
811 req_log(ISC_LOG_DEBUG(3), "dns_request_createraw: request %p",
814 return (ISC_R_SUCCESS);
817 LOCK(&requestmgr->lock);
818 ISC_LIST_UNLINK(requestmgr->requests, request, link);
819 UNLOCK(&requestmgr->lock);
823 isc_task_detach(&tclone);
824 req_destroy(request);
825 req_log(ISC_LOG_DEBUG(3), "dns_request_createraw: failed %s",
826 dns_result_totext(result));
831 dns_request_create(dns_requestmgr_t *requestmgr, dns_message_t *message,
832 isc_sockaddr_t *address, unsigned int options,
834 unsigned int timeout, isc_task_t *task,
835 isc_taskaction_t action, void *arg,
836 dns_request_t **requestp)
838 return (dns_request_createvia3(requestmgr, message, NULL, address,
839 options, key, timeout, 0, 0, task,
840 action, arg, requestp));
844 dns_request_createvia(dns_requestmgr_t *requestmgr, dns_message_t *message,
845 isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
846 unsigned int options, dns_tsigkey_t *key,
847 unsigned int timeout, isc_task_t *task,
848 isc_taskaction_t action, void *arg,
849 dns_request_t **requestp)
851 return(dns_request_createvia3(requestmgr, message, srcaddr, destaddr,
852 options, key, timeout, 0, 0, task,
853 action, arg, requestp));
857 dns_request_createvia2(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 isc_task_t *task, isc_taskaction_t action, void *arg,
862 dns_request_t **requestp)
864 unsigned int udpretries = 0;
867 udpretries = timeout / udptimeout;
868 return (dns_request_createvia3(requestmgr, message, srcaddr, destaddr,
869 options, key, timeout, udptimeout,
870 udpretries, task, action, arg,
875 dns_request_createvia3(dns_requestmgr_t *requestmgr, dns_message_t *message,
876 isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
877 unsigned int options, dns_tsigkey_t *key,
878 unsigned int timeout, unsigned int udptimeout,
879 unsigned int udpretries, isc_task_t *task,
880 isc_taskaction_t action, void *arg,
881 dns_request_t **requestp)
883 dns_request_t *request = NULL;
884 isc_task_t *tclone = NULL;
885 isc_socket_t *socket = NULL;
890 isc_boolean_t setkey = ISC_TRUE;
892 REQUIRE(VALID_REQUESTMGR(requestmgr));
893 REQUIRE(message != NULL);
894 REQUIRE(destaddr != NULL);
895 REQUIRE(task != NULL);
896 REQUIRE(action != NULL);
897 REQUIRE(requestp != NULL && *requestp == NULL);
898 REQUIRE(timeout > 0);
900 REQUIRE(isc_sockaddr_pf(srcaddr) == isc_sockaddr_pf(destaddr));
902 mctx = requestmgr->mctx;
904 req_log(ISC_LOG_DEBUG(3), "dns_request_createvia");
906 if (isblackholed(requestmgr->dispatchmgr, destaddr))
907 return (DNS_R_BLACKHOLED);
910 result = new_request(mctx, &request);
911 if (result != ISC_R_SUCCESS)
914 if (udptimeout == 0 && udpretries != 0) {
915 udptimeout = timeout / (udpretries + 1);
919 request->udpcount = udpretries;
922 * Create timer now. We will set it below once.
924 result = isc_timer_create(requestmgr->timermgr, isc_timertype_inactive,
925 NULL, NULL, task, req_timeout, request,
927 if (result != ISC_R_SUCCESS)
930 request->event = (dns_requestevent_t *)
931 isc_event_allocate(mctx, task, DNS_EVENT_REQUESTDONE,
932 action, arg, sizeof(dns_requestevent_t));
933 if (request->event == NULL) {
934 result = ISC_R_NOMEMORY;
937 isc_task_attach(task, &tclone);
938 request->event->ev_sender = task;
939 request->event->request = request;
940 request->event->result = ISC_R_FAILURE;
942 dns_tsigkey_attach(key, &request->tsigkey);
945 tcp = ISC_TF((options & DNS_REQUESTOPT_TCP) != 0);
946 result = get_dispatch(tcp, requestmgr, srcaddr, destaddr,
948 if (result != ISC_R_SUCCESS)
951 result = dns_dispatch_addresponse2(request->dispatch, destaddr, task,
952 req_response, request, &id,
954 requestmgr->socketmgr);
955 if (result != ISC_R_SUCCESS)
957 socket = req_getsocket(request);
958 INSIST(socket != NULL);
962 result = dns_message_settsigkey(message, request->tsigkey);
963 if (result != ISC_R_SUCCESS)
966 result = req_render(message, &request->query, options, mctx);
967 if (result == DNS_R_USETCP &&
968 (options & DNS_REQUESTOPT_TCP) == 0) {
970 * Try again using TCP.
972 dns_message_renderreset(message);
973 dns_dispatch_removeresponse(&request->dispentry, NULL);
974 dns_dispatch_detach(&request->dispatch);
976 options |= DNS_REQUESTOPT_TCP;
980 if (result != ISC_R_SUCCESS)
983 result = dns_message_getquerytsig(message, mctx, &request->tsig);
984 if (result != ISC_R_SUCCESS)
987 LOCK(&requestmgr->lock);
988 if (requestmgr->exiting) {
989 UNLOCK(&requestmgr->lock);
990 result = ISC_R_SHUTTINGDOWN;
993 requestmgr_attach(requestmgr, &request->requestmgr);
994 request->hash = mgr_gethash(requestmgr);
995 ISC_LIST_APPEND(requestmgr->requests, request, link);
996 UNLOCK(&requestmgr->lock);
998 result = set_timer(request->timer, timeout, tcp ? 0 : udptimeout);
999 if (result != ISC_R_SUCCESS)
1002 request->destaddr = *destaddr;
1004 result = isc_socket_connect(socket, destaddr, task,
1005 req_connected, request);
1006 if (result != ISC_R_SUCCESS)
1008 request->flags |= DNS_REQUEST_F_CONNECTING|DNS_REQUEST_F_TCP;
1010 result = req_send(request, task, destaddr);
1011 if (result != ISC_R_SUCCESS)
1015 req_log(ISC_LOG_DEBUG(3), "dns_request_createvia: request %p",
1017 *requestp = request;
1018 return (ISC_R_SUCCESS);
1021 LOCK(&requestmgr->lock);
1022 ISC_LIST_UNLINK(requestmgr->requests, request, link);
1023 UNLOCK(&requestmgr->lock);
1027 isc_task_detach(&tclone);
1028 req_destroy(request);
1029 req_log(ISC_LOG_DEBUG(3), "dns_request_createvia: failed %s",
1030 dns_result_totext(result));
1035 req_render(dns_message_t *message, isc_buffer_t **bufferp,
1036 unsigned int options, isc_mem_t *mctx)
1038 isc_buffer_t *buf1 = NULL;
1039 isc_buffer_t *buf2 = NULL;
1040 isc_result_t result;
1042 isc_boolean_t tcp = ISC_FALSE;
1043 dns_compress_t cctx;
1044 isc_boolean_t cleanup_cctx = ISC_FALSE;
1046 REQUIRE(bufferp != NULL && *bufferp == NULL);
1048 req_log(ISC_LOG_DEBUG(3), "request_render");
1051 * Create buffer able to hold largest possible message.
1053 result = isc_buffer_allocate(mctx, &buf1, 65535);
1054 if (result != ISC_R_SUCCESS)
1057 result = dns_compress_init(&cctx, -1, mctx);
1058 if (result != ISC_R_SUCCESS)
1060 cleanup_cctx = ISC_TRUE;
1062 if ((options & DNS_REQUESTOPT_CASE) != 0)
1063 dns_compress_setsensitive(&cctx, ISC_TRUE);
1068 result = dns_message_renderbegin(message, &cctx, buf1);
1069 if (result != ISC_R_SUCCESS)
1071 result = dns_message_rendersection(message, DNS_SECTION_QUESTION, 0);
1072 if (result != ISC_R_SUCCESS)
1074 result = dns_message_rendersection(message, DNS_SECTION_ANSWER, 0);
1075 if (result != ISC_R_SUCCESS)
1077 result = dns_message_rendersection(message, DNS_SECTION_AUTHORITY, 0);
1078 if (result != ISC_R_SUCCESS)
1080 result = dns_message_rendersection(message, DNS_SECTION_ADDITIONAL, 0);
1081 if (result != ISC_R_SUCCESS)
1083 result = dns_message_renderend(message);
1084 if (result != ISC_R_SUCCESS)
1087 dns_compress_invalidate(&cctx);
1088 cleanup_cctx = ISC_FALSE;
1091 * Copy rendered message to exact sized buffer.
1093 isc_buffer_usedregion(buf1, &r);
1094 if ((options & DNS_REQUESTOPT_TCP) != 0) {
1096 } else if (r.length > 512) {
1097 result = DNS_R_USETCP;
1100 result = isc_buffer_allocate(mctx, &buf2, r.length + (tcp ? 2 : 0));
1101 if (result != ISC_R_SUCCESS)
1104 isc_buffer_putuint16(buf2, (isc_uint16_t)r.length);
1105 result = isc_buffer_copyregion(buf2, &r);
1106 if (result != ISC_R_SUCCESS)
1110 * Cleanup and return.
1112 isc_buffer_free(&buf1);
1114 return (ISC_R_SUCCESS);
1117 dns_message_renderreset(message);
1119 isc_buffer_free(&buf1);
1121 isc_buffer_free(&buf2);
1123 dns_compress_invalidate(&cctx);
1129 * If this request is no longer waiting for events,
1130 * send the completion event. This will ultimately
1131 * cause the request to be destroyed.
1134 * 'request' is locked by the caller.
1137 send_if_done(dns_request_t *request, isc_result_t result) {
1138 if (!DNS_REQUEST_CONNECTING(request) &&
1139 !DNS_REQUEST_SENDING(request) &&
1140 !request->canceling)
1141 req_sendevent(request, result);
1145 * Handle the control event.
1148 do_cancel(isc_task_t *task, isc_event_t *event) {
1149 dns_request_t *request = event->ev_arg;
1151 INSIST(event->ev_type == DNS_EVENT_REQUESTCONTROL);
1152 LOCK(&request->requestmgr->locks[request->hash]);
1153 request->canceling = ISC_FALSE;
1154 if (!DNS_REQUEST_CANCELED(request))
1155 req_cancel(request);
1156 send_if_done(request, ISC_R_CANCELED);
1157 UNLOCK(&request->requestmgr->locks[request->hash]);
1161 dns_request_cancel(dns_request_t *request) {
1162 REQUIRE(VALID_REQUEST(request));
1164 req_log(ISC_LOG_DEBUG(3), "dns_request_cancel: request %p", request);
1166 REQUIRE(VALID_REQUEST(request));
1168 LOCK(&request->requestmgr->locks[request->hash]);
1169 if (!request->canceling && !DNS_REQUEST_CANCELED(request)) {
1170 isc_event_t *ev = &request->ctlevent;
1171 isc_task_send(request->event->ev_sender, &ev);
1172 request->canceling = ISC_TRUE;
1174 UNLOCK(&request->requestmgr->locks[request->hash]);
1178 dns_request_getresponse(dns_request_t *request, dns_message_t *message,
1179 unsigned int options)
1181 isc_result_t result;
1183 REQUIRE(VALID_REQUEST(request));
1184 REQUIRE(request->answer != NULL);
1186 req_log(ISC_LOG_DEBUG(3), "dns_request_getresponse: request %p",
1189 result = dns_message_setquerytsig(message, request->tsig);
1190 if (result != ISC_R_SUCCESS)
1192 result = dns_message_settsigkey(message, request->tsigkey);
1193 if (result != ISC_R_SUCCESS)
1195 result = dns_message_parse(message, request->answer, options);
1196 if (result != ISC_R_SUCCESS)
1198 if (request->tsigkey != NULL)
1199 result = dns_tsig_verify(request->answer, message, NULL, NULL);
1204 dns_request_usedtcp(dns_request_t *request) {
1205 REQUIRE(VALID_REQUEST(request));
1207 return (ISC_TF((request->flags & DNS_REQUEST_F_TCP) != 0));
1211 dns_request_destroy(dns_request_t **requestp) {
1212 dns_request_t *request;
1214 REQUIRE(requestp != NULL && VALID_REQUEST(*requestp));
1216 request = *requestp;
1218 req_log(ISC_LOG_DEBUG(3), "dns_request_destroy: request %p", request);
1220 LOCK(&request->requestmgr->lock);
1221 LOCK(&request->requestmgr->locks[request->hash]);
1222 ISC_LIST_UNLINK(request->requestmgr->requests, request, link);
1223 INSIST(!DNS_REQUEST_CONNECTING(request));
1224 INSIST(!DNS_REQUEST_SENDING(request));
1225 UNLOCK(&request->requestmgr->locks[request->hash]);
1226 UNLOCK(&request->requestmgr->lock);
1229 * These should have been cleaned up by req_cancel() before
1230 * the completion event was sent.
1232 INSIST(!ISC_LINK_LINKED(request, link));
1233 INSIST(request->dispentry == NULL);
1234 INSIST(request->dispatch == NULL);
1235 INSIST(request->timer == NULL);
1237 req_destroy(request);
1243 *** Private: request.
1246 static isc_socket_t *
1247 req_getsocket(dns_request_t *request) {
1248 unsigned int dispattr;
1249 isc_socket_t *socket;
1251 dispattr = dns_dispatch_getattributes(request->dispatch);
1252 if ((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0) {
1253 INSIST(request->dispentry != NULL);
1254 socket = dns_dispatch_getentrysocket(request->dispentry);
1256 socket = dns_dispatch_getsocket(request->dispatch);
1262 req_connected(isc_task_t *task, isc_event_t *event) {
1263 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1264 isc_result_t result;
1265 dns_request_t *request = event->ev_arg;
1267 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
1268 REQUIRE(VALID_REQUEST(request));
1269 REQUIRE(DNS_REQUEST_CONNECTING(request));
1271 req_log(ISC_LOG_DEBUG(3), "req_connected: request %p", request);
1273 LOCK(&request->requestmgr->locks[request->hash]);
1274 request->flags &= ~DNS_REQUEST_F_CONNECTING;
1276 if (DNS_REQUEST_CANCELED(request)) {
1278 * Send delayed event.
1280 if (DNS_REQUEST_TIMEDOUT(request))
1281 send_if_done(request, ISC_R_TIMEDOUT);
1283 send_if_done(request, ISC_R_CANCELED);
1285 dns_dispatch_starttcp(request->dispatch);
1286 result = sevent->result;
1287 if (result == ISC_R_SUCCESS)
1288 result = req_send(request, task, NULL);
1290 if (result != ISC_R_SUCCESS) {
1291 req_cancel(request);
1292 send_if_done(request, ISC_R_CANCELED);
1295 UNLOCK(&request->requestmgr->locks[request->hash]);
1296 isc_event_free(&event);
1300 req_senddone(isc_task_t *task, isc_event_t *event) {
1301 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1302 dns_request_t *request = event->ev_arg;
1304 REQUIRE(event->ev_type == ISC_SOCKEVENT_SENDDONE);
1305 REQUIRE(VALID_REQUEST(request));
1306 REQUIRE(DNS_REQUEST_SENDING(request));
1308 req_log(ISC_LOG_DEBUG(3), "req_senddone: request %p", request);
1312 LOCK(&request->requestmgr->locks[request->hash]);
1313 request->flags &= ~DNS_REQUEST_F_SENDING;
1315 if (DNS_REQUEST_CANCELED(request)) {
1317 * Send delayed event.
1319 if (DNS_REQUEST_TIMEDOUT(request))
1320 send_if_done(request, ISC_R_TIMEDOUT);
1322 send_if_done(request, ISC_R_CANCELED);
1323 } else if (sevent->result != ISC_R_SUCCESS) {
1324 req_cancel(request);
1325 send_if_done(request, ISC_R_CANCELED);
1327 UNLOCK(&request->requestmgr->locks[request->hash]);
1329 isc_event_free(&event);
1333 req_response(isc_task_t *task, isc_event_t *event) {
1334 isc_result_t result;
1335 dns_request_t *request = event->ev_arg;
1336 dns_dispatchevent_t *devent = (dns_dispatchevent_t *)event;
1339 REQUIRE(VALID_REQUEST(request));
1340 REQUIRE(event->ev_type == DNS_EVENT_DISPATCH);
1344 req_log(ISC_LOG_DEBUG(3), "req_response: request %p: %s", request,
1345 dns_result_totext(devent->result));
1347 LOCK(&request->requestmgr->locks[request->hash]);
1348 result = devent->result;
1349 if (result != ISC_R_SUCCESS)
1353 * Copy buffer to request.
1355 isc_buffer_usedregion(&devent->buffer, &r);
1356 result = isc_buffer_allocate(request->mctx, &request->answer,
1358 if (result != ISC_R_SUCCESS)
1360 result = isc_buffer_copyregion(request->answer, &r);
1361 if (result != ISC_R_SUCCESS)
1362 isc_buffer_free(&request->answer);
1367 dns_dispatch_removeresponse(&request->dispentry, &devent);
1368 req_cancel(request);
1370 * Send completion event.
1372 send_if_done(request, result);
1373 UNLOCK(&request->requestmgr->locks[request->hash]);
1377 req_timeout(isc_task_t *task, isc_event_t *event) {
1378 dns_request_t *request = event->ev_arg;
1379 isc_result_t result;
1381 REQUIRE(VALID_REQUEST(request));
1383 req_log(ISC_LOG_DEBUG(3), "req_timeout: request %p", request);
1386 LOCK(&request->requestmgr->locks[request->hash]);
1387 if (event->ev_type == ISC_TIMEREVENT_TICK &&
1388 request->udpcount-- != 0) {
1389 if (! DNS_REQUEST_SENDING(request)) {
1390 result = req_send(request, task, &request->destaddr);
1391 if (result != ISC_R_SUCCESS) {
1392 req_cancel(request);
1393 send_if_done(request, result);
1397 request->flags |= DNS_REQUEST_F_TIMEDOUT;
1398 req_cancel(request);
1399 send_if_done(request, ISC_R_TIMEDOUT);
1401 UNLOCK(&request->requestmgr->locks[request->hash]);
1402 isc_event_free(&event);
1406 req_sendevent(dns_request_t *request, isc_result_t result) {
1409 REQUIRE(VALID_REQUEST(request));
1411 req_log(ISC_LOG_DEBUG(3), "req_sendevent: request %p", request);
1414 * Lock held by caller.
1416 task = request->event->ev_sender;
1417 request->event->ev_sender = request;
1418 request->event->result = result;
1419 isc_task_sendanddetach(&task, (isc_event_t **)&request->event);
1423 req_destroy(dns_request_t *request) {
1426 REQUIRE(VALID_REQUEST(request));
1428 req_log(ISC_LOG_DEBUG(3), "req_destroy: request %p", request);
1431 if (request->query != NULL)
1432 isc_buffer_free(&request->query);
1433 if (request->answer != NULL)
1434 isc_buffer_free(&request->answer);
1435 if (request->event != NULL)
1436 isc_event_free((isc_event_t **)&request->event);
1437 if (request->dispentry != NULL)
1438 dns_dispatch_removeresponse(&request->dispentry, NULL);
1439 if (request->dispatch != NULL)
1440 dns_dispatch_detach(&request->dispatch);
1441 if (request->timer != NULL)
1442 isc_timer_detach(&request->timer);
1443 if (request->tsig != NULL)
1444 isc_buffer_free(&request->tsig);
1445 if (request->tsigkey != NULL)
1446 dns_tsigkey_detach(&request->tsigkey);
1447 if (request->requestmgr != NULL)
1448 requestmgr_detach(&request->requestmgr);
1449 mctx = request->mctx;
1450 isc_mem_put(mctx, request, sizeof(*request));
1451 isc_mem_detach(&mctx);
1455 * Stop the current request. Must be called from the request's task.
1458 req_cancel(dns_request_t *request) {
1459 isc_socket_t *socket;
1460 unsigned int dispattr;
1462 REQUIRE(VALID_REQUEST(request));
1464 req_log(ISC_LOG_DEBUG(3), "req_cancel: request %p", request);
1467 * Lock held by caller.
1469 request->flags |= DNS_REQUEST_F_CANCELED;
1471 if (request->timer != NULL)
1472 isc_timer_detach(&request->timer);
1473 dispattr = dns_dispatch_getattributes(request->dispatch);
1475 if (DNS_REQUEST_CONNECTING(request) || DNS_REQUEST_SENDING(request)) {
1476 if ((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0) {
1477 if (request->dispentry != NULL) {
1478 socket = dns_dispatch_getentrysocket(
1479 request->dispentry);
1482 socket = dns_dispatch_getsocket(request->dispatch);
1483 if (DNS_REQUEST_CONNECTING(request) && socket != NULL)
1484 isc_socket_cancel(socket, NULL, ISC_SOCKCANCEL_CONNECT);
1485 if (DNS_REQUEST_SENDING(request) && socket != NULL)
1486 isc_socket_cancel(socket, NULL, ISC_SOCKCANCEL_SEND);
1488 if (request->dispentry != NULL)
1489 dns_dispatch_removeresponse(&request->dispentry, NULL);
1490 dns_dispatch_detach(&request->dispatch);
1494 req_log(int level, const char *fmt, ...) {
1498 isc_log_vwrite(dns_lctx, DNS_LOGCATEGORY_GENERAL,
1499 DNS_LOGMODULE_REQUEST, level, fmt, ap);