2 * Copyright (C) 2004-2009, 2011 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.82.72.4 2011-03-12 04:57:27 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;
432 req_log(ISC_LOG_DEBUG(3), "req_send: request %p", request);
434 REQUIRE(VALID_REQUEST(request));
435 socket = req_getsocket(request);
436 isc_buffer_usedregion(request->query, &r);
438 * We could connect the socket when we are using an exclusive dispatch
439 * as we do in resolver.c, but we prefer implementation simplicity
442 result = isc_socket_sendto(socket, &r, task, req_senddone,
443 request, address, NULL);
444 if (result == ISC_R_SUCCESS)
445 request->flags |= DNS_REQUEST_F_SENDING;
450 new_request(isc_mem_t *mctx, dns_request_t **requestp) {
451 dns_request_t *request;
453 request = isc_mem_get(mctx, sizeof(*request));
455 return (ISC_R_NOMEMORY);
461 request->mctx = NULL;
463 ISC_LINK_INIT(request, link);
464 request->query = NULL;
465 request->answer = NULL;
466 request->event = NULL;
467 request->dispatch = NULL;
468 request->dispentry = NULL;
469 request->timer = NULL;
470 request->requestmgr = NULL;
471 request->tsig = NULL;
472 request->tsigkey = NULL;
473 ISC_EVENT_INIT(&request->ctlevent, sizeof(request->ctlevent), 0, NULL,
474 DNS_EVENT_REQUESTCONTROL, do_cancel, request, NULL,
476 request->canceling = ISC_FALSE;
477 request->udpcount = 0;
479 isc_mem_attach(mctx, &request->mctx);
481 request->magic = REQUEST_MAGIC;
483 return (ISC_R_SUCCESS);
488 isblackholed(dns_dispatchmgr_t *dispatchmgr, isc_sockaddr_t *destaddr) {
489 dns_acl_t *blackhole;
490 isc_netaddr_t netaddr;
492 isc_boolean_t drop = ISC_FALSE;
493 char netaddrstr[ISC_NETADDR_FORMATSIZE];
495 blackhole = dns_dispatchmgr_getblackhole(dispatchmgr);
496 if (blackhole != NULL) {
497 isc_netaddr_fromsockaddr(&netaddr, destaddr);
498 if (dns_acl_match(&netaddr, NULL, blackhole,
499 NULL, &match, NULL) == ISC_R_SUCCESS &&
504 isc_netaddr_format(&netaddr, netaddrstr, sizeof(netaddrstr));
505 req_log(ISC_LOG_DEBUG(10), "blackholed address %s", netaddrstr);
511 create_tcp_dispatch(dns_requestmgr_t *requestmgr, isc_sockaddr_t *srcaddr,
512 isc_sockaddr_t *destaddr, dns_dispatch_t **dispatchp)
515 isc_socket_t *socket = NULL;
518 isc_sockaddr_t bind_any;
520 result = isc_socket_create(requestmgr->socketmgr,
521 isc_sockaddr_pf(destaddr),
522 isc_sockettype_tcp, &socket);
523 if (result != ISC_R_SUCCESS)
525 #ifndef BROKEN_TCP_BIND_BEFORE_CONNECT
526 if (srcaddr == NULL) {
527 isc_sockaddr_anyofpf(&bind_any,
528 isc_sockaddr_pf(destaddr));
529 result = isc_socket_bind(socket, &bind_any, 0);
532 isc_sockaddr_setport(&src, 0);
533 result = isc_socket_bind(socket, &src, 0);
535 if (result != ISC_R_SUCCESS)
539 attrs |= DNS_DISPATCHATTR_TCP;
540 attrs |= DNS_DISPATCHATTR_PRIVATE;
541 if (isc_sockaddr_pf(destaddr) == AF_INET)
542 attrs |= DNS_DISPATCHATTR_IPV4;
544 attrs |= DNS_DISPATCHATTR_IPV6;
545 attrs |= DNS_DISPATCHATTR_MAKEQUERY;
546 result = dns_dispatch_createtcp(requestmgr->dispatchmgr,
547 socket, requestmgr->taskmgr,
548 4096, 2, 1, 1, 3, attrs,
551 isc_socket_detach(&socket);
556 find_udp_dispatch(dns_requestmgr_t *requestmgr, isc_sockaddr_t *srcaddr,
557 isc_sockaddr_t *destaddr, dns_dispatch_t **dispatchp)
559 dns_dispatch_t *disp = NULL;
560 unsigned int attrs, attrmask;
562 if (srcaddr == NULL) {
563 switch (isc_sockaddr_pf(destaddr)) {
565 disp = requestmgr->dispatchv4;
569 disp = requestmgr->dispatchv6;
573 return (ISC_R_NOTIMPLEMENTED);
576 return (ISC_R_FAMILYNOSUPPORT);
577 dns_dispatch_attach(disp, dispatchp);
578 return (ISC_R_SUCCESS);
581 attrs |= DNS_DISPATCHATTR_UDP;
582 switch (isc_sockaddr_pf(srcaddr)) {
584 attrs |= DNS_DISPATCHATTR_IPV4;
588 attrs |= DNS_DISPATCHATTR_IPV6;
592 return (ISC_R_NOTIMPLEMENTED);
595 attrmask |= DNS_DISPATCHATTR_UDP;
596 attrmask |= DNS_DISPATCHATTR_TCP;
597 attrmask |= DNS_DISPATCHATTR_IPV4;
598 attrmask |= DNS_DISPATCHATTR_IPV6;
599 return (dns_dispatch_getudp(requestmgr->dispatchmgr,
600 requestmgr->socketmgr,
603 1000, 32768, 16411, 16433,
609 get_dispatch(isc_boolean_t tcp, dns_requestmgr_t *requestmgr,
610 isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
611 dns_dispatch_t **dispatchp)
615 result = create_tcp_dispatch(requestmgr, srcaddr,
616 destaddr, dispatchp);
618 result = find_udp_dispatch(requestmgr, srcaddr,
619 destaddr, dispatchp);
624 set_timer(isc_timer_t *timer, unsigned int timeout, unsigned int udpresend) {
626 isc_interval_t interval;
628 isc_timertype_t timertype;
630 isc_interval_set(&interval, timeout, 0);
631 result = isc_time_nowplusinterval(&expires, &interval);
632 isc_interval_set(&interval, udpresend, 0);
634 timertype = udpresend != 0 ? isc_timertype_limited : isc_timertype_once;
635 if (result == ISC_R_SUCCESS)
636 result = isc_timer_reset(timer, timertype, &expires,
637 &interval, ISC_FALSE);
642 dns_request_createraw(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 isc_task_t *task, isc_taskaction_t action, void *arg,
646 dns_request_t **requestp)
648 return(dns_request_createraw3(requestmgr, msgbuf, srcaddr, destaddr,
649 options, timeout, 0, 0, task, action,
654 dns_request_createraw2(dns_requestmgr_t *requestmgr, isc_buffer_t *msgbuf,
655 isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
656 unsigned int options, unsigned int timeout,
657 unsigned int udptimeout, isc_task_t *task,
658 isc_taskaction_t action, void *arg,
659 dns_request_t **requestp)
661 unsigned int udpretries = 0;
664 udpretries = timeout / udptimeout;
666 return (dns_request_createraw3(requestmgr, msgbuf, srcaddr, destaddr,
667 options, timeout, udptimeout,
668 udpretries, task, action, arg,
673 dns_request_createraw3(dns_requestmgr_t *requestmgr, isc_buffer_t *msgbuf,
674 isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
675 unsigned int options, unsigned int timeout,
676 unsigned int udptimeout, unsigned int udpretries,
677 isc_task_t *task, isc_taskaction_t action, void *arg,
678 dns_request_t **requestp)
680 dns_request_t *request = NULL;
681 isc_task_t *tclone = NULL;
682 isc_socket_t *socket = NULL;
686 isc_boolean_t tcp = ISC_FALSE;
689 REQUIRE(VALID_REQUESTMGR(requestmgr));
690 REQUIRE(msgbuf != NULL);
691 REQUIRE(destaddr != NULL);
692 REQUIRE(task != NULL);
693 REQUIRE(action != NULL);
694 REQUIRE(requestp != NULL && *requestp == NULL);
695 REQUIRE(timeout > 0);
697 REQUIRE(isc_sockaddr_pf(srcaddr) == isc_sockaddr_pf(destaddr));
699 mctx = requestmgr->mctx;
701 req_log(ISC_LOG_DEBUG(3), "dns_request_createraw");
703 if (isblackholed(requestmgr->dispatchmgr, destaddr))
704 return (DNS_R_BLACKHOLED);
707 result = new_request(mctx, &request);
708 if (result != ISC_R_SUCCESS)
711 if (udptimeout == 0 && udpretries != 0) {
712 udptimeout = timeout / (udpretries + 1);
716 request->udpcount = udpretries;
719 * Create timer now. We will set it below once.
721 result = isc_timer_create(requestmgr->timermgr, isc_timertype_inactive,
722 NULL, NULL, task, req_timeout, request,
724 if (result != ISC_R_SUCCESS)
727 request->event = (dns_requestevent_t *)
728 isc_event_allocate(mctx, task, DNS_EVENT_REQUESTDONE,
729 action, arg, sizeof(dns_requestevent_t));
730 if (request->event == NULL) {
731 result = ISC_R_NOMEMORY;
734 isc_task_attach(task, &tclone);
735 request->event->ev_sender = task;
736 request->event->request = request;
737 request->event->result = ISC_R_FAILURE;
739 isc_buffer_usedregion(msgbuf, &r);
740 if (r.length < DNS_MESSAGE_HEADERLEN || r.length > 65535) {
741 result = DNS_R_FORMERR;
745 if ((options & DNS_REQUESTOPT_TCP) != 0 || r.length > 512)
748 result = get_dispatch(tcp, requestmgr, srcaddr, destaddr,
750 if (result != ISC_R_SUCCESS)
753 result = dns_dispatch_addresponse2(request->dispatch, destaddr, task,
754 req_response, request, &id,
756 requestmgr->socketmgr);
757 if (result != ISC_R_SUCCESS)
760 socket = req_getsocket(request);
761 INSIST(socket != NULL);
763 result = isc_buffer_allocate(mctx, &request->query,
764 r.length + (tcp ? 2 : 0));
765 if (result != ISC_R_SUCCESS)
768 isc_buffer_putuint16(request->query, (isc_uint16_t)r.length);
769 result = isc_buffer_copyregion(request->query, &r);
770 if (result != ISC_R_SUCCESS)
773 /* Add message ID. */
774 isc_buffer_usedregion(request->query, &r);
776 isc_region_consume(&r, 2);
777 r.base[0] = (id>>8) & 0xff;
778 r.base[1] = id & 0xff;
780 LOCK(&requestmgr->lock);
781 if (requestmgr->exiting) {
782 UNLOCK(&requestmgr->lock);
783 result = ISC_R_SHUTTINGDOWN;
786 requestmgr_attach(requestmgr, &request->requestmgr);
787 request->hash = mgr_gethash(requestmgr);
788 ISC_LIST_APPEND(requestmgr->requests, request, link);
789 UNLOCK(&requestmgr->lock);
791 result = set_timer(request->timer, timeout, tcp ? 0 : udptimeout);
792 if (result != ISC_R_SUCCESS)
795 request->destaddr = *destaddr;
797 result = isc_socket_connect(socket, destaddr, task,
798 req_connected, request);
799 if (result != ISC_R_SUCCESS)
801 request->flags |= DNS_REQUEST_F_CONNECTING|DNS_REQUEST_F_TCP;
803 result = req_send(request, task, destaddr);
804 if (result != ISC_R_SUCCESS)
808 req_log(ISC_LOG_DEBUG(3), "dns_request_createraw: request %p",
811 return (ISC_R_SUCCESS);
814 LOCK(&requestmgr->lock);
815 ISC_LIST_UNLINK(requestmgr->requests, request, link);
816 UNLOCK(&requestmgr->lock);
820 isc_task_detach(&tclone);
821 req_destroy(request);
822 req_log(ISC_LOG_DEBUG(3), "dns_request_createraw: failed %s",
823 dns_result_totext(result));
828 dns_request_create(dns_requestmgr_t *requestmgr, dns_message_t *message,
829 isc_sockaddr_t *address, unsigned int options,
831 unsigned int timeout, isc_task_t *task,
832 isc_taskaction_t action, void *arg,
833 dns_request_t **requestp)
835 return (dns_request_createvia3(requestmgr, message, NULL, address,
836 options, key, timeout, 0, 0, task,
837 action, arg, requestp));
841 dns_request_createvia(dns_requestmgr_t *requestmgr, dns_message_t *message,
842 isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
843 unsigned int options, dns_tsigkey_t *key,
844 unsigned int timeout, isc_task_t *task,
845 isc_taskaction_t action, void *arg,
846 dns_request_t **requestp)
848 return(dns_request_createvia3(requestmgr, message, srcaddr, destaddr,
849 options, key, timeout, 0, 0, task,
850 action, arg, requestp));
854 dns_request_createvia2(dns_requestmgr_t *requestmgr, dns_message_t *message,
855 isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
856 unsigned int options, dns_tsigkey_t *key,
857 unsigned int timeout, unsigned int udptimeout,
858 isc_task_t *task, isc_taskaction_t action, void *arg,
859 dns_request_t **requestp)
861 unsigned int udpretries = 0;
864 udpretries = timeout / udptimeout;
865 return (dns_request_createvia3(requestmgr, message, srcaddr, destaddr,
866 options, key, timeout, udptimeout,
867 udpretries, task, action, arg,
872 dns_request_createvia3(dns_requestmgr_t *requestmgr, dns_message_t *message,
873 isc_sockaddr_t *srcaddr, isc_sockaddr_t *destaddr,
874 unsigned int options, dns_tsigkey_t *key,
875 unsigned int timeout, unsigned int udptimeout,
876 unsigned int udpretries, isc_task_t *task,
877 isc_taskaction_t action, void *arg,
878 dns_request_t **requestp)
880 dns_request_t *request = NULL;
881 isc_task_t *tclone = NULL;
882 isc_socket_t *socket = NULL;
887 isc_boolean_t setkey = ISC_TRUE;
889 REQUIRE(VALID_REQUESTMGR(requestmgr));
890 REQUIRE(message != NULL);
891 REQUIRE(destaddr != NULL);
892 REQUIRE(task != NULL);
893 REQUIRE(action != NULL);
894 REQUIRE(requestp != NULL && *requestp == NULL);
895 REQUIRE(timeout > 0);
897 REQUIRE(isc_sockaddr_pf(srcaddr) == isc_sockaddr_pf(destaddr));
899 mctx = requestmgr->mctx;
901 req_log(ISC_LOG_DEBUG(3), "dns_request_createvia");
903 if (isblackholed(requestmgr->dispatchmgr, destaddr))
904 return (DNS_R_BLACKHOLED);
907 result = new_request(mctx, &request);
908 if (result != ISC_R_SUCCESS)
911 if (udptimeout == 0 && udpretries != 0) {
912 udptimeout = timeout / (udpretries + 1);
916 request->udpcount = udpretries;
919 * Create timer now. We will set it below once.
921 result = isc_timer_create(requestmgr->timermgr, isc_timertype_inactive,
922 NULL, NULL, task, req_timeout, request,
924 if (result != ISC_R_SUCCESS)
927 request->event = (dns_requestevent_t *)
928 isc_event_allocate(mctx, task, DNS_EVENT_REQUESTDONE,
929 action, arg, sizeof(dns_requestevent_t));
930 if (request->event == NULL) {
931 result = ISC_R_NOMEMORY;
934 isc_task_attach(task, &tclone);
935 request->event->ev_sender = task;
936 request->event->request = request;
937 request->event->result = ISC_R_FAILURE;
939 dns_tsigkey_attach(key, &request->tsigkey);
942 tcp = ISC_TF((options & DNS_REQUESTOPT_TCP) != 0);
943 result = get_dispatch(tcp, requestmgr, srcaddr, destaddr,
945 if (result != ISC_R_SUCCESS)
948 result = dns_dispatch_addresponse2(request->dispatch, destaddr, task,
949 req_response, request, &id,
951 requestmgr->socketmgr);
952 if (result != ISC_R_SUCCESS)
954 socket = req_getsocket(request);
955 INSIST(socket != NULL);
959 result = dns_message_settsigkey(message, request->tsigkey);
960 if (result != ISC_R_SUCCESS)
963 result = req_render(message, &request->query, options, mctx);
964 if (result == DNS_R_USETCP &&
965 (options & DNS_REQUESTOPT_TCP) == 0) {
967 * Try again using TCP.
969 dns_message_renderreset(message);
970 dns_dispatch_removeresponse(&request->dispentry, NULL);
971 dns_dispatch_detach(&request->dispatch);
973 options |= DNS_REQUESTOPT_TCP;
977 if (result != ISC_R_SUCCESS)
980 result = dns_message_getquerytsig(message, mctx, &request->tsig);
981 if (result != ISC_R_SUCCESS)
984 LOCK(&requestmgr->lock);
985 if (requestmgr->exiting) {
986 UNLOCK(&requestmgr->lock);
987 result = ISC_R_SHUTTINGDOWN;
990 requestmgr_attach(requestmgr, &request->requestmgr);
991 request->hash = mgr_gethash(requestmgr);
992 ISC_LIST_APPEND(requestmgr->requests, request, link);
993 UNLOCK(&requestmgr->lock);
995 result = set_timer(request->timer, timeout, tcp ? 0 : udptimeout);
996 if (result != ISC_R_SUCCESS)
999 request->destaddr = *destaddr;
1001 result = isc_socket_connect(socket, destaddr, task,
1002 req_connected, request);
1003 if (result != ISC_R_SUCCESS)
1005 request->flags |= DNS_REQUEST_F_CONNECTING|DNS_REQUEST_F_TCP;
1007 result = req_send(request, task, destaddr);
1008 if (result != ISC_R_SUCCESS)
1012 req_log(ISC_LOG_DEBUG(3), "dns_request_createvia: request %p",
1014 *requestp = request;
1015 return (ISC_R_SUCCESS);
1018 LOCK(&requestmgr->lock);
1019 ISC_LIST_UNLINK(requestmgr->requests, request, link);
1020 UNLOCK(&requestmgr->lock);
1024 isc_task_detach(&tclone);
1025 req_destroy(request);
1026 req_log(ISC_LOG_DEBUG(3), "dns_request_createvia: failed %s",
1027 dns_result_totext(result));
1032 req_render(dns_message_t *message, isc_buffer_t **bufferp,
1033 unsigned int options, isc_mem_t *mctx)
1035 isc_buffer_t *buf1 = NULL;
1036 isc_buffer_t *buf2 = NULL;
1037 isc_result_t result;
1039 isc_boolean_t tcp = ISC_FALSE;
1040 dns_compress_t cctx;
1041 isc_boolean_t cleanup_cctx = ISC_FALSE;
1043 REQUIRE(bufferp != NULL && *bufferp == NULL);
1045 req_log(ISC_LOG_DEBUG(3), "request_render");
1048 * Create buffer able to hold largest possible message.
1050 result = isc_buffer_allocate(mctx, &buf1, 65535);
1051 if (result != ISC_R_SUCCESS)
1054 result = dns_compress_init(&cctx, -1, mctx);
1055 if (result != ISC_R_SUCCESS)
1057 cleanup_cctx = ISC_TRUE;
1062 result = dns_message_renderbegin(message, &cctx, buf1);
1063 if (result != ISC_R_SUCCESS)
1065 result = dns_message_rendersection(message, DNS_SECTION_QUESTION, 0);
1066 if (result != ISC_R_SUCCESS)
1068 result = dns_message_rendersection(message, DNS_SECTION_ANSWER, 0);
1069 if (result != ISC_R_SUCCESS)
1071 result = dns_message_rendersection(message, DNS_SECTION_AUTHORITY, 0);
1072 if (result != ISC_R_SUCCESS)
1074 result = dns_message_rendersection(message, DNS_SECTION_ADDITIONAL, 0);
1075 if (result != ISC_R_SUCCESS)
1077 result = dns_message_renderend(message);
1078 if (result != ISC_R_SUCCESS)
1081 dns_compress_invalidate(&cctx);
1082 cleanup_cctx = ISC_FALSE;
1085 * Copy rendered message to exact sized buffer.
1087 isc_buffer_usedregion(buf1, &r);
1088 if ((options & DNS_REQUESTOPT_TCP) != 0) {
1090 } else if (r.length > 512) {
1091 result = DNS_R_USETCP;
1094 result = isc_buffer_allocate(mctx, &buf2, r.length + (tcp ? 2 : 0));
1095 if (result != ISC_R_SUCCESS)
1098 isc_buffer_putuint16(buf2, (isc_uint16_t)r.length);
1099 result = isc_buffer_copyregion(buf2, &r);
1100 if (result != ISC_R_SUCCESS)
1104 * Cleanup and return.
1106 isc_buffer_free(&buf1);
1108 return (ISC_R_SUCCESS);
1111 dns_message_renderreset(message);
1113 isc_buffer_free(&buf1);
1115 isc_buffer_free(&buf2);
1117 dns_compress_invalidate(&cctx);
1123 * If this request is no longer waiting for events,
1124 * send the completion event. This will ultimately
1125 * cause the request to be destroyed.
1128 * 'request' is locked by the caller.
1131 send_if_done(dns_request_t *request, isc_result_t result) {
1132 if (!DNS_REQUEST_CONNECTING(request) &&
1133 !DNS_REQUEST_SENDING(request) &&
1134 !request->canceling)
1135 req_sendevent(request, result);
1139 * Handle the control event.
1142 do_cancel(isc_task_t *task, isc_event_t *event) {
1143 dns_request_t *request = event->ev_arg;
1145 INSIST(event->ev_type == DNS_EVENT_REQUESTCONTROL);
1146 LOCK(&request->requestmgr->locks[request->hash]);
1147 request->canceling = ISC_FALSE;
1148 if (!DNS_REQUEST_CANCELED(request))
1149 req_cancel(request);
1150 send_if_done(request, ISC_R_CANCELED);
1151 UNLOCK(&request->requestmgr->locks[request->hash]);
1155 dns_request_cancel(dns_request_t *request) {
1156 REQUIRE(VALID_REQUEST(request));
1158 req_log(ISC_LOG_DEBUG(3), "dns_request_cancel: request %p", request);
1160 REQUIRE(VALID_REQUEST(request));
1162 LOCK(&request->requestmgr->locks[request->hash]);
1163 if (!request->canceling && !DNS_REQUEST_CANCELED(request)) {
1164 isc_event_t *ev = &request->ctlevent;
1165 isc_task_send(request->event->ev_sender, &ev);
1166 request->canceling = ISC_TRUE;
1168 UNLOCK(&request->requestmgr->locks[request->hash]);
1172 dns_request_getresponse(dns_request_t *request, dns_message_t *message,
1173 unsigned int options)
1175 isc_result_t result;
1177 REQUIRE(VALID_REQUEST(request));
1178 REQUIRE(request->answer != NULL);
1180 req_log(ISC_LOG_DEBUG(3), "dns_request_getresponse: request %p",
1183 result = dns_message_setquerytsig(message, request->tsig);
1184 if (result != ISC_R_SUCCESS)
1186 result = dns_message_settsigkey(message, request->tsigkey);
1187 if (result != ISC_R_SUCCESS)
1189 result = dns_message_parse(message, request->answer, options);
1190 if (result != ISC_R_SUCCESS)
1192 if (request->tsigkey != NULL)
1193 result = dns_tsig_verify(request->answer, message, NULL, NULL);
1198 dns_request_usedtcp(dns_request_t *request) {
1199 REQUIRE(VALID_REQUEST(request));
1201 return (ISC_TF((request->flags & DNS_REQUEST_F_TCP) != 0));
1205 dns_request_destroy(dns_request_t **requestp) {
1206 dns_request_t *request;
1208 REQUIRE(requestp != NULL && VALID_REQUEST(*requestp));
1210 request = *requestp;
1212 req_log(ISC_LOG_DEBUG(3), "dns_request_destroy: request %p", request);
1214 LOCK(&request->requestmgr->lock);
1215 LOCK(&request->requestmgr->locks[request->hash]);
1216 ISC_LIST_UNLINK(request->requestmgr->requests, request, link);
1217 INSIST(!DNS_REQUEST_CONNECTING(request));
1218 INSIST(!DNS_REQUEST_SENDING(request));
1219 UNLOCK(&request->requestmgr->locks[request->hash]);
1220 UNLOCK(&request->requestmgr->lock);
1223 * These should have been cleaned up by req_cancel() before
1224 * the completion event was sent.
1226 INSIST(!ISC_LINK_LINKED(request, link));
1227 INSIST(request->dispentry == NULL);
1228 INSIST(request->dispatch == NULL);
1229 INSIST(request->timer == NULL);
1231 req_destroy(request);
1237 *** Private: request.
1240 static isc_socket_t *
1241 req_getsocket(dns_request_t *request) {
1242 unsigned int dispattr;
1243 isc_socket_t *socket;
1245 dispattr = dns_dispatch_getattributes(request->dispatch);
1246 if ((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0) {
1247 INSIST(request->dispentry != NULL);
1248 socket = dns_dispatch_getentrysocket(request->dispentry);
1250 socket = dns_dispatch_getsocket(request->dispatch);
1256 req_connected(isc_task_t *task, isc_event_t *event) {
1257 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1258 isc_result_t result;
1259 dns_request_t *request = event->ev_arg;
1261 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
1262 REQUIRE(VALID_REQUEST(request));
1263 REQUIRE(DNS_REQUEST_CONNECTING(request));
1265 req_log(ISC_LOG_DEBUG(3), "req_connected: request %p", request);
1267 LOCK(&request->requestmgr->locks[request->hash]);
1268 request->flags &= ~DNS_REQUEST_F_CONNECTING;
1270 if (DNS_REQUEST_CANCELED(request)) {
1272 * Send delayed event.
1274 if (DNS_REQUEST_TIMEDOUT(request))
1275 send_if_done(request, ISC_R_TIMEDOUT);
1277 send_if_done(request, ISC_R_CANCELED);
1279 dns_dispatch_starttcp(request->dispatch);
1280 result = sevent->result;
1281 if (result == ISC_R_SUCCESS)
1282 result = req_send(request, task, NULL);
1284 if (result != ISC_R_SUCCESS) {
1285 req_cancel(request);
1286 send_if_done(request, ISC_R_CANCELED);
1289 UNLOCK(&request->requestmgr->locks[request->hash]);
1290 isc_event_free(&event);
1294 req_senddone(isc_task_t *task, isc_event_t *event) {
1295 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1296 dns_request_t *request = event->ev_arg;
1298 REQUIRE(event->ev_type == ISC_SOCKEVENT_SENDDONE);
1299 REQUIRE(VALID_REQUEST(request));
1300 REQUIRE(DNS_REQUEST_SENDING(request));
1302 req_log(ISC_LOG_DEBUG(3), "req_senddone: request %p", request);
1306 LOCK(&request->requestmgr->locks[request->hash]);
1307 request->flags &= ~DNS_REQUEST_F_SENDING;
1309 if (DNS_REQUEST_CANCELED(request)) {
1311 * Send delayed event.
1313 if (DNS_REQUEST_TIMEDOUT(request))
1314 send_if_done(request, ISC_R_TIMEDOUT);
1316 send_if_done(request, ISC_R_CANCELED);
1317 } else if (sevent->result != ISC_R_SUCCESS) {
1318 req_cancel(request);
1319 send_if_done(request, ISC_R_CANCELED);
1321 UNLOCK(&request->requestmgr->locks[request->hash]);
1323 isc_event_free(&event);
1327 req_response(isc_task_t *task, isc_event_t *event) {
1328 isc_result_t result;
1329 dns_request_t *request = event->ev_arg;
1330 dns_dispatchevent_t *devent = (dns_dispatchevent_t *)event;
1333 REQUIRE(VALID_REQUEST(request));
1334 REQUIRE(event->ev_type == DNS_EVENT_DISPATCH);
1338 req_log(ISC_LOG_DEBUG(3), "req_response: request %p: %s", request,
1339 dns_result_totext(devent->result));
1341 LOCK(&request->requestmgr->locks[request->hash]);
1342 result = devent->result;
1343 if (result != ISC_R_SUCCESS)
1347 * Copy buffer to request.
1349 isc_buffer_usedregion(&devent->buffer, &r);
1350 result = isc_buffer_allocate(request->mctx, &request->answer,
1352 if (result != ISC_R_SUCCESS)
1354 result = isc_buffer_copyregion(request->answer, &r);
1355 if (result != ISC_R_SUCCESS)
1356 isc_buffer_free(&request->answer);
1361 dns_dispatch_removeresponse(&request->dispentry, &devent);
1362 req_cancel(request);
1364 * Send completion event.
1366 send_if_done(request, result);
1367 UNLOCK(&request->requestmgr->locks[request->hash]);
1371 req_timeout(isc_task_t *task, isc_event_t *event) {
1372 dns_request_t *request = event->ev_arg;
1373 isc_result_t result;
1375 REQUIRE(VALID_REQUEST(request));
1377 req_log(ISC_LOG_DEBUG(3), "req_timeout: request %p", request);
1380 LOCK(&request->requestmgr->locks[request->hash]);
1381 if (event->ev_type == ISC_TIMEREVENT_TICK &&
1382 request->udpcount-- != 0) {
1383 if (! DNS_REQUEST_SENDING(request)) {
1384 result = req_send(request, task, &request->destaddr);
1385 if (result != ISC_R_SUCCESS) {
1386 req_cancel(request);
1387 send_if_done(request, result);
1391 request->flags |= DNS_REQUEST_F_TIMEDOUT;
1392 req_cancel(request);
1393 send_if_done(request, ISC_R_TIMEDOUT);
1395 UNLOCK(&request->requestmgr->locks[request->hash]);
1396 isc_event_free(&event);
1400 req_sendevent(dns_request_t *request, isc_result_t result) {
1403 REQUIRE(VALID_REQUEST(request));
1405 req_log(ISC_LOG_DEBUG(3), "req_sendevent: request %p", request);
1408 * Lock held by caller.
1410 task = request->event->ev_sender;
1411 request->event->ev_sender = request;
1412 request->event->result = result;
1413 isc_task_sendanddetach(&task, (isc_event_t **)&request->event);
1417 req_destroy(dns_request_t *request) {
1420 REQUIRE(VALID_REQUEST(request));
1422 req_log(ISC_LOG_DEBUG(3), "req_destroy: request %p", request);
1425 if (request->query != NULL)
1426 isc_buffer_free(&request->query);
1427 if (request->answer != NULL)
1428 isc_buffer_free(&request->answer);
1429 if (request->event != NULL)
1430 isc_event_free((isc_event_t **)&request->event);
1431 if (request->dispentry != NULL)
1432 dns_dispatch_removeresponse(&request->dispentry, NULL);
1433 if (request->dispatch != NULL)
1434 dns_dispatch_detach(&request->dispatch);
1435 if (request->timer != NULL)
1436 isc_timer_detach(&request->timer);
1437 if (request->tsig != NULL)
1438 isc_buffer_free(&request->tsig);
1439 if (request->tsigkey != NULL)
1440 dns_tsigkey_detach(&request->tsigkey);
1441 if (request->requestmgr != NULL)
1442 requestmgr_detach(&request->requestmgr);
1443 mctx = request->mctx;
1444 isc_mem_put(mctx, request, sizeof(*request));
1445 isc_mem_detach(&mctx);
1449 * Stop the current request. Must be called from the request's task.
1452 req_cancel(dns_request_t *request) {
1453 isc_socket_t *socket;
1454 unsigned int dispattr;
1456 REQUIRE(VALID_REQUEST(request));
1458 req_log(ISC_LOG_DEBUG(3), "req_cancel: request %p", request);
1461 * Lock held by caller.
1463 request->flags |= DNS_REQUEST_F_CANCELED;
1465 if (request->timer != NULL)
1466 isc_timer_detach(&request->timer);
1467 dispattr = dns_dispatch_getattributes(request->dispatch);
1469 if (DNS_REQUEST_CONNECTING(request) || DNS_REQUEST_SENDING(request)) {
1470 if ((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0) {
1471 if (request->dispentry != NULL) {
1472 socket = dns_dispatch_getentrysocket(
1473 request->dispentry);
1476 socket = dns_dispatch_getsocket(request->dispatch);
1477 if (DNS_REQUEST_CONNECTING(request) && socket != NULL)
1478 isc_socket_cancel(socket, NULL, ISC_SOCKCANCEL_CONNECT);
1479 if (DNS_REQUEST_SENDING(request) && socket != NULL)
1480 isc_socket_cancel(socket, NULL, ISC_SOCKCANCEL_SEND);
1482 if (request->dispentry != NULL)
1483 dns_dispatch_removeresponse(&request->dispentry, NULL);
1484 dns_dispatch_detach(&request->dispatch);
1488 req_log(int level, const char *fmt, ...) {
1492 isc_log_vwrite(dns_lctx, DNS_LOGCATEGORY_GENERAL,
1493 DNS_LOGMODULE_REQUEST, level, fmt, ap);