2 * Copyright (C) 2004-2010 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: resolver.c,v 1.384.14.20.8.2 2010/02/25 10:57:12 tbox Exp $ */
24 #include <isc/platform.h>
25 #include <isc/print.h>
26 #include <isc/string.h>
27 #include <isc/random.h>
29 #include <isc/stats.h>
30 #include <isc/timer.h>
35 #include <dns/cache.h>
37 #include <dns/dispatch.h>
39 #include <dns/events.h>
40 #include <dns/forward.h>
41 #include <dns/keytable.h>
43 #include <dns/message.h>
44 #include <dns/ncache.h>
45 #include <dns/opcode.h>
48 #include <dns/rcode.h>
49 #include <dns/rdata.h>
50 #include <dns/rdataclass.h>
51 #include <dns/rdatalist.h>
52 #include <dns/rdataset.h>
53 #include <dns/rdatastruct.h>
54 #include <dns/rdatatype.h>
55 #include <dns/resolver.h>
56 #include <dns/result.h>
57 #include <dns/rootns.h>
58 #include <dns/stats.h>
60 #include <dns/validator.h>
62 #define DNS_RESOLVER_TRACE
63 #ifdef DNS_RESOLVER_TRACE
64 #define RTRACE(m) isc_log_write(dns_lctx, \
65 DNS_LOGCATEGORY_RESOLVER, \
66 DNS_LOGMODULE_RESOLVER, \
68 "res %p: %s", res, (m))
69 #define RRTRACE(r, m) isc_log_write(dns_lctx, \
70 DNS_LOGCATEGORY_RESOLVER, \
71 DNS_LOGMODULE_RESOLVER, \
73 "res %p: %s", (r), (m))
74 #define FCTXTRACE(m) isc_log_write(dns_lctx, \
75 DNS_LOGCATEGORY_RESOLVER, \
76 DNS_LOGMODULE_RESOLVER, \
78 "fctx %p(%s'): %s", fctx, fctx->info, (m))
79 #define FCTXTRACE2(m1, m2) \
80 isc_log_write(dns_lctx, \
81 DNS_LOGCATEGORY_RESOLVER, \
82 DNS_LOGMODULE_RESOLVER, \
84 "fctx %p(%s): %s %s", \
85 fctx, fctx->info, (m1), (m2))
86 #define FTRACE(m) isc_log_write(dns_lctx, \
87 DNS_LOGCATEGORY_RESOLVER, \
88 DNS_LOGMODULE_RESOLVER, \
90 "fetch %p (fctx %p(%s)): %s", \
91 fetch, fetch->private, \
92 fetch->private->info, (m))
93 #define QTRACE(m) isc_log_write(dns_lctx, \
94 DNS_LOGCATEGORY_RESOLVER, \
95 DNS_LOGMODULE_RESOLVER, \
97 "resquery %p (fctx %p(%s)): %s", \
99 query->fctx->info, (m))
102 #define RRTRACE(r, m)
109 * Maximum EDNS0 input packet size.
111 #define RECV_BUFFER_SIZE 4096 /* XXXRTH Constant. */
114 * This defines the maximum number of timeouts we will permit before we
115 * disable EDNS0 on the query.
117 #define MAX_EDNS0_TIMEOUTS 3
119 typedef struct fetchctx fetchctx_t;
121 typedef struct query {
122 /* Locked by task event serialization. */
126 dns_dispatchmgr_t * dispatchmgr;
127 dns_dispatch_t * dispatch;
128 isc_boolean_t exclusivesocket;
129 dns_adbaddrinfo_t * addrinfo;
130 isc_socket_t * tcpsocket;
133 dns_dispentry_t * dispentry;
134 ISC_LINK(struct query) link;
137 dns_tsigkey_t *tsigkey;
138 unsigned int options;
139 unsigned int attributes;
141 unsigned int connects;
142 unsigned char data[512];
145 #define QUERY_MAGIC ISC_MAGIC('Q', '!', '!', '!')
146 #define VALID_QUERY(query) ISC_MAGIC_VALID(query, QUERY_MAGIC)
148 #define RESQUERY_ATTR_CANCELED 0x02
150 #define RESQUERY_CONNECTING(q) ((q)->connects > 0)
151 #define RESQUERY_CANCELED(q) (((q)->attributes & \
152 RESQUERY_ATTR_CANCELED) != 0)
153 #define RESQUERY_SENDING(q) ((q)->sends > 0)
156 fetchstate_init = 0, /*%< Start event has not run yet. */
158 fetchstate_done /*%< FETCHDONE events posted. */
162 badns_unreachable = 0,
170 dns_resolver_t * res;
172 dns_rdatatype_t type;
173 unsigned int options;
174 unsigned int bucketnum;
176 /*% Locked by appropriate bucket lock. */
178 isc_boolean_t want_shutdown;
179 isc_boolean_t cloned;
180 isc_boolean_t spilled;
181 unsigned int references;
182 isc_event_t control_event;
183 ISC_LINK(struct fetchctx) link;
184 ISC_LIST(dns_fetchevent_t) events;
185 /*% Locked by task event serialization. */
187 dns_rdataset_t nameservers;
188 unsigned int attributes;
191 isc_interval_t interval;
192 dns_message_t * qmessage;
193 dns_message_t * rmessage;
194 ISC_LIST(resquery_t) queries;
195 dns_adbfindlist_t finds;
196 dns_adbfind_t * find;
197 dns_adbfindlist_t altfinds;
198 dns_adbfind_t * altfind;
199 dns_adbaddrinfolist_t forwaddrs;
200 dns_adbaddrinfolist_t altaddrs;
201 isc_sockaddrlist_t forwarders;
202 dns_fwdpolicy_t fwdpolicy;
203 isc_sockaddrlist_t bad;
204 isc_sockaddrlist_t edns;
205 isc_sockaddrlist_t edns512;
206 dns_validator_t *validator;
207 ISC_LIST(dns_validator_t) validators;
212 * The number of events we're waiting for.
214 unsigned int pending;
217 * The number of times we've "restarted" the current
218 * nameserver set. This acts as a failsafe to prevent
219 * us from pounding constantly on a particular set of
220 * servers that, for whatever reason, are not giving
221 * us useful responses, but are responding in such a
222 * way that they are not marked "bad".
224 unsigned int restarts;
227 * The number of timeouts that have occurred since we
228 * last successfully received a response packet. This
229 * is used for EDNS0 black hole detection.
231 unsigned int timeouts;
234 * Look aside state for DS lookups.
237 dns_fetch_t * nsfetch;
238 dns_rdataset_t nsrrset;
241 * Number of queries that reference this context.
243 unsigned int nqueries;
246 * The reason to print when logging a successful
247 * response to a query.
252 * Random numbers to use for mixing up server addresses.
254 isc_uint32_t rand_buf;
255 isc_uint32_t rand_bits;
258 * Fetch-local statistics for detailed logging.
260 isc_result_t result; /*%< fetch result */
261 isc_result_t vresult; /*%< validation result */
264 isc_uint64_t duration;
265 isc_boolean_t logged;
266 unsigned int querysent;
267 unsigned int referrals;
268 unsigned int lamecount;
270 unsigned int badresp;
272 unsigned int findfail;
273 unsigned int valfail;
274 isc_boolean_t timeout;
277 #define FCTX_MAGIC ISC_MAGIC('F', '!', '!', '!')
278 #define VALID_FCTX(fctx) ISC_MAGIC_VALID(fctx, FCTX_MAGIC)
280 #define FCTX_ATTR_HAVEANSWER 0x0001
281 #define FCTX_ATTR_GLUING 0x0002
282 #define FCTX_ATTR_ADDRWAIT 0x0004
283 #define FCTX_ATTR_SHUTTINGDOWN 0x0008
284 #define FCTX_ATTR_WANTCACHE 0x0010
285 #define FCTX_ATTR_WANTNCACHE 0x0020
286 #define FCTX_ATTR_NEEDEDNS0 0x0040
287 #define FCTX_ATTR_TRIEDFIND 0x0080
288 #define FCTX_ATTR_TRIEDALT 0x0100
290 #define HAVE_ANSWER(f) (((f)->attributes & FCTX_ATTR_HAVEANSWER) != \
292 #define GLUING(f) (((f)->attributes & FCTX_ATTR_GLUING) != \
294 #define ADDRWAIT(f) (((f)->attributes & FCTX_ATTR_ADDRWAIT) != \
296 #define SHUTTINGDOWN(f) (((f)->attributes & FCTX_ATTR_SHUTTINGDOWN) \
298 #define WANTCACHE(f) (((f)->attributes & FCTX_ATTR_WANTCACHE) != 0)
299 #define WANTNCACHE(f) (((f)->attributes & FCTX_ATTR_WANTNCACHE) != 0)
300 #define NEEDEDNS0(f) (((f)->attributes & FCTX_ATTR_NEEDEDNS0) != 0)
301 #define TRIEDFIND(f) (((f)->attributes & FCTX_ATTR_TRIEDFIND) != 0)
302 #define TRIEDALT(f) (((f)->attributes & FCTX_ATTR_TRIEDALT) != 0)
305 dns_adbaddrinfo_t * addrinfo;
311 fetchctx_t * private;
314 #define DNS_FETCH_MAGIC ISC_MAGIC('F', 't', 'c', 'h')
315 #define DNS_FETCH_VALID(fetch) ISC_MAGIC_VALID(fetch, DNS_FETCH_MAGIC)
317 typedef struct fctxbucket {
320 ISC_LIST(fetchctx_t) fctxs;
321 isc_boolean_t exiting;
325 typedef struct alternate {
326 isc_boolean_t isaddress;
334 ISC_LINK(struct alternate) link;
337 typedef struct dns_badcache dns_badcache_t;
338 struct dns_badcache {
339 dns_badcache_t * next;
340 dns_rdatatype_t type;
342 unsigned int hashval;
345 #define DNS_BADCACHE_SIZE 1021
346 #define DNS_BADCACHE_TTL(fctx) \
347 (((fctx)->res->lame_ttl > 30 ) ? (fctx)->res->lame_ttl : 30)
349 struct dns_resolver {
355 isc_mutex_t primelock;
356 dns_rdataclass_t rdclass;
357 isc_socketmgr_t * socketmgr;
358 isc_timermgr_t * timermgr;
359 isc_taskmgr_t * taskmgr;
361 isc_boolean_t frozen;
362 unsigned int options;
363 dns_dispatchmgr_t * dispatchmgr;
364 dns_dispatch_t * dispatchv4;
365 isc_boolean_t exclusivev4;
366 dns_dispatch_t * dispatchv6;
367 isc_boolean_t exclusivev6;
369 unsigned int nbuckets;
370 fctxbucket_t * buckets;
371 isc_uint32_t lame_ttl;
372 ISC_LIST(alternate_t) alternates;
373 isc_uint16_t udpsize;
375 isc_rwlock_t alglock;
377 dns_rbt_t * algorithms;
379 isc_rwlock_t mbslock;
381 dns_rbt_t * mustbesecure;
382 unsigned int spillatmax;
383 unsigned int spillatmin;
384 isc_timer_t * spillattimer;
385 isc_boolean_t zero_no_soa_ttl;
387 /* Locked by lock. */
388 unsigned int references;
389 isc_boolean_t exiting;
390 isc_eventlist_t whenshutdown;
391 unsigned int activebuckets;
392 isc_boolean_t priming;
393 unsigned int spillat; /* clients-per-query */
394 unsigned int nextdisp;
397 dns_badcache_t ** badcache;
398 unsigned int badcount;
399 unsigned int badhash;
400 unsigned int badsweep;
402 /* Locked by primelock. */
403 dns_fetch_t * primefetch;
404 /* Locked by nlock. */
408 #define RES_MAGIC ISC_MAGIC('R', 'e', 's', '!')
409 #define VALID_RESOLVER(res) ISC_MAGIC_VALID(res, RES_MAGIC)
412 * Private addrinfo flags. These must not conflict with DNS_FETCHOPT_NOEDNS0,
413 * which we also use as an addrinfo flag.
415 #define FCTX_ADDRINFO_MARK 0x0001
416 #define FCTX_ADDRINFO_FORWARDER 0x1000
417 #define FCTX_ADDRINFO_TRIED 0x2000
418 #define UNMARKED(a) (((a)->flags & FCTX_ADDRINFO_MARK) \
420 #define ISFORWARDER(a) (((a)->flags & \
421 FCTX_ADDRINFO_FORWARDER) != 0)
422 #define TRIED(a) (((a)->flags & \
423 FCTX_ADDRINFO_TRIED) != 0)
425 #define NXDOMAIN(r) (((r)->attributes & DNS_RDATASETATTR_NXDOMAIN) != 0)
427 static void destroy(dns_resolver_t *res);
428 static void empty_bucket(dns_resolver_t *res);
429 static isc_result_t resquery_send(resquery_t *query);
430 static void resquery_response(isc_task_t *task, isc_event_t *event);
431 static void resquery_connected(isc_task_t *task, isc_event_t *event);
432 static void fctx_try(fetchctx_t *fctx, isc_boolean_t retrying,
433 isc_boolean_t badcache);
434 static isc_boolean_t fctx_destroy(fetchctx_t *fctx);
435 static isc_result_t ncache_adderesult(dns_message_t *message,
436 dns_db_t *cache, dns_dbnode_t *node,
437 dns_rdatatype_t covers,
438 isc_stdtime_t now, dns_ttl_t maxttl,
439 isc_boolean_t optout,
440 dns_rdataset_t *ardataset,
441 isc_result_t *eresultp);
442 static void validated(isc_task_t *task, isc_event_t *event);
443 static void maybe_destroy(fetchctx_t *fctx);
444 static void add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
445 isc_result_t reason, badnstype_t badtype);
448 * Increment resolver-related statistics counters.
451 inc_stats(dns_resolver_t *res, isc_statscounter_t counter) {
452 if (res->view->resstats != NULL)
453 isc_stats_increment(res->view->resstats, counter);
457 valcreate(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, dns_name_t *name,
458 dns_rdatatype_t type, dns_rdataset_t *rdataset,
459 dns_rdataset_t *sigrdataset, unsigned int valoptions,
462 dns_validator_t *validator = NULL;
463 dns_valarg_t *valarg;
466 valarg = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
469 return (ISC_R_NOMEMORY);
472 valarg->addrinfo = addrinfo;
474 if (!ISC_LIST_EMPTY(fctx->validators))
475 INSIST((valoptions & DNS_VALIDATOR_DEFER) != 0);
477 result = dns_validator_create(fctx->res->view, name, type, rdataset,
478 sigrdataset, fctx->rmessage,
479 valoptions, task, validated, valarg,
481 if (result == ISC_R_SUCCESS) {
482 inc_stats(fctx->res, dns_resstatscounter_val);
483 if ((valoptions & DNS_VALIDATOR_DEFER) == 0) {
484 INSIST(fctx->validator == NULL);
485 fctx->validator = validator;
487 ISC_LIST_APPEND(fctx->validators, validator, link);
489 isc_mem_put(fctx->res->buckets[fctx->bucketnum].mctx,
490 valarg, sizeof(*valarg));
495 rrsig_fromchildzone(fetchctx_t *fctx, dns_rdataset_t *rdataset) {
496 dns_namereln_t namereln;
497 dns_rdata_rrsig_t rrsig;
498 dns_rdata_t rdata = DNS_RDATA_INIT;
503 for (result = dns_rdataset_first(rdataset);
504 result == ISC_R_SUCCESS;
505 result = dns_rdataset_next(rdataset)) {
506 dns_rdataset_current(rdataset, &rdata);
507 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
508 RUNTIME_CHECK(result == ISC_R_SUCCESS);
509 namereln = dns_name_fullcompare(&rrsig.signer, &fctx->domain,
511 if (namereln == dns_namereln_subdomain)
513 dns_rdata_reset(&rdata);
519 fix_mustbedelegationornxdomain(dns_message_t *message, fetchctx_t *fctx) {
521 dns_name_t *domain = &fctx->domain;
522 dns_rdataset_t *rdataset;
523 dns_rdatatype_t type;
525 isc_boolean_t keep_auth = ISC_FALSE;
527 if (message->rcode == dns_rcode_nxdomain)
531 * A DS RRset can appear anywhere in a zone, even for a delegation-only
532 * zone. So a response to an explicit query for this type should be
533 * excluded from delegation-only fixup.
535 * SOA, NS, and DNSKEY can only exist at a zone apex, so a postive
536 * response to a query for these types can never violate the
537 * delegation-only assumption: if the query name is below a
538 * zone cut, the response should normally be a referral, which should
539 * be accepted; if the query name is below a zone cut but the server
540 * happens to have authority for the zone of the query name, the
541 * response is a (non-referral) answer. But this does not violate
542 * delegation-only because the query name must be in a different zone
543 * due to the "apex-only" nature of these types. Note that if the
544 * remote server happens to have authority for a child zone of a
545 * delegation-only zone, we may still incorrectly "fix" the response
546 * with NXDOMAIN for queries for other types. Unfortunately it's
547 * generally impossible to differentiate this case from violation of
548 * the delegation-only assumption. Once the resolver learns the
549 * correct zone cut, possibly via a separate query for an "apex-only"
550 * type, queries for other types will be resolved correctly.
552 * A query for type ANY will be accepted if it hits an exceptional
553 * type above in the answer section as it should be from a child
556 * Also accept answers with RRSIG records from the child zone.
557 * Direct queries for RRSIG records should not be answered from
561 if (message->counts[DNS_SECTION_ANSWER] != 0 &&
562 (fctx->type == dns_rdatatype_ns ||
563 fctx->type == dns_rdatatype_ds ||
564 fctx->type == dns_rdatatype_soa ||
565 fctx->type == dns_rdatatype_any ||
566 fctx->type == dns_rdatatype_rrsig ||
567 fctx->type == dns_rdatatype_dnskey)) {
568 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
569 while (result == ISC_R_SUCCESS) {
571 dns_message_currentname(message, DNS_SECTION_ANSWER,
573 for (rdataset = ISC_LIST_HEAD(name->list);
575 rdataset = ISC_LIST_NEXT(rdataset, link)) {
576 if (!dns_name_equal(name, &fctx->name))
578 type = rdataset->type;
582 if (type == dns_rdatatype_rrsig &&
583 rrsig_fromchildzone(fctx, rdataset))
586 * Direct query for apex records or DS.
588 if (fctx->type == type &&
589 (type == dns_rdatatype_ds ||
590 type == dns_rdatatype_ns ||
591 type == dns_rdatatype_soa ||
592 type == dns_rdatatype_dnskey))
595 * Indirect query for apex records or DS.
597 if (fctx->type == dns_rdatatype_any &&
598 (type == dns_rdatatype_ns ||
599 type == dns_rdatatype_ds ||
600 type == dns_rdatatype_soa ||
601 type == dns_rdatatype_dnskey))
604 result = dns_message_nextname(message,
610 * A NODATA response to a DS query?
612 if (fctx->type == dns_rdatatype_ds &&
613 message->counts[DNS_SECTION_ANSWER] == 0)
616 /* Look for referral or indication of answer from child zone? */
617 if (message->counts[DNS_SECTION_AUTHORITY] == 0)
620 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
621 while (result == ISC_R_SUCCESS) {
623 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
624 for (rdataset = ISC_LIST_HEAD(name->list);
626 rdataset = ISC_LIST_NEXT(rdataset, link)) {
627 type = rdataset->type;
628 if (type == dns_rdatatype_soa &&
629 dns_name_equal(name, domain))
630 keep_auth = ISC_TRUE;
632 if (type != dns_rdatatype_ns &&
633 type != dns_rdatatype_soa &&
634 type != dns_rdatatype_rrsig)
637 if (type == dns_rdatatype_rrsig) {
638 if (rrsig_fromchildzone(fctx, rdataset))
644 /* NS or SOA records. */
645 if (dns_name_equal(name, domain)) {
647 * If a query for ANY causes a negative
648 * response, we can be sure that this is
649 * an empty node. For other type of queries
650 * we cannot differentiate an empty node
651 * from a node that just doesn't have that
652 * type of record. We only accept the former
655 if (message->counts[DNS_SECTION_ANSWER] == 0 &&
656 fctx->type == dns_rdatatype_any)
658 } else if (dns_name_issubdomain(name, domain)) {
659 /* Referral or answer from child zone. */
663 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
667 message->rcode = dns_rcode_nxdomain;
668 message->counts[DNS_SECTION_ANSWER] = 0;
670 message->counts[DNS_SECTION_AUTHORITY] = 0;
671 message->counts[DNS_SECTION_ADDITIONAL] = 0;
675 static inline isc_result_t
676 fctx_starttimer(fetchctx_t *fctx) {
678 * Start the lifetime timer for fctx.
680 * This is also used for stopping the idle timer; in that
681 * case we must purge events already posted to ensure that
682 * no further idle events are delivered.
684 return (isc_timer_reset(fctx->timer, isc_timertype_once,
685 &fctx->expires, NULL, ISC_TRUE));
689 fctx_stoptimer(fetchctx_t *fctx) {
693 * We don't return a result if resetting the timer to inactive fails
694 * since there's nothing to be done about it. Resetting to inactive
695 * should never fail anyway, since the code as currently written
696 * cannot fail in that case.
698 result = isc_timer_reset(fctx->timer, isc_timertype_inactive,
699 NULL, NULL, ISC_TRUE);
700 if (result != ISC_R_SUCCESS) {
701 UNEXPECTED_ERROR(__FILE__, __LINE__,
702 "isc_timer_reset(): %s",
703 isc_result_totext(result));
708 static inline isc_result_t
709 fctx_startidletimer(fetchctx_t *fctx, isc_interval_t *interval) {
711 * Start the idle timer for fctx. The lifetime timer continues
714 return (isc_timer_reset(fctx->timer, isc_timertype_once,
715 &fctx->expires, interval, ISC_FALSE));
719 * Stopping the idle timer is equivalent to calling fctx_starttimer(), but
720 * we use fctx_stopidletimer for readability in the code below.
722 #define fctx_stopidletimer fctx_starttimer
726 resquery_destroy(resquery_t **queryp) {
729 REQUIRE(queryp != NULL);
731 REQUIRE(!ISC_LINK_LINKED(query, link));
733 INSIST(query->tcpsocket == NULL);
735 query->fctx->nqueries--;
736 if (SHUTTINGDOWN(query->fctx))
737 maybe_destroy(query->fctx); /* Locks bucket. */
739 isc_mem_put(query->mctx, query, sizeof(*query));
744 fctx_cancelquery(resquery_t **queryp, dns_dispatchevent_t **deventp,
745 isc_time_t *finish, isc_boolean_t no_response)
749 unsigned int rtt, rttms;
752 dns_adbaddrinfo_t *addrinfo;
753 isc_socket_t *socket;
758 FCTXTRACE("cancelquery");
760 REQUIRE(!RESQUERY_CANCELED(query));
762 query->attributes |= RESQUERY_ATTR_CANCELED;
765 * Should we update the RTT?
767 if (finish != NULL || no_response) {
768 if (finish != NULL) {
770 * We have both the start and finish times for this
771 * packet, so we can compute a real RTT.
773 rtt = (unsigned int)isc_time_microdiff(finish,
775 factor = DNS_ADB_RTTADJDEFAULT;
778 if (rttms < DNS_RESOLVER_QRYRTTCLASS0) {
780 dns_resstatscounter_queryrtt0);
781 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS1) {
783 dns_resstatscounter_queryrtt1);
784 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS2) {
786 dns_resstatscounter_queryrtt2);
787 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS3) {
789 dns_resstatscounter_queryrtt3);
790 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS4) {
792 dns_resstatscounter_queryrtt4);
795 dns_resstatscounter_queryrtt5);
799 * We don't have an RTT for this query. Maybe the
800 * packet was lost, or maybe this server is very
801 * slow. We don't know. Increase the RTT.
804 rtt = query->addrinfo->srtt + 200000;
808 * Replace the current RTT with our value.
810 factor = DNS_ADB_RTTADJREPLACE;
812 dns_adb_adjustsrtt(fctx->adb, query->addrinfo, rtt, factor);
815 /* Remember that the server has been tried. */
816 if (!TRIED(query->addrinfo)) {
817 dns_adb_changeflags(fctx->adb, query->addrinfo,
818 FCTX_ADDRINFO_TRIED, FCTX_ADDRINFO_TRIED);
822 * Age RTTs of servers not tried.
824 factor = DNS_ADB_RTTADJAGE;
826 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
828 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
829 if (UNMARKED(addrinfo))
830 dns_adb_adjustsrtt(fctx->adb, addrinfo,
833 if (finish != NULL && TRIEDFIND(fctx))
834 for (find = ISC_LIST_HEAD(fctx->finds);
836 find = ISC_LIST_NEXT(find, publink))
837 for (addrinfo = ISC_LIST_HEAD(find->list);
839 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
840 if (UNMARKED(addrinfo))
841 dns_adb_adjustsrtt(fctx->adb, addrinfo,
844 if (finish != NULL && TRIEDALT(fctx)) {
845 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
847 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
848 if (UNMARKED(addrinfo))
849 dns_adb_adjustsrtt(fctx->adb, addrinfo,
851 for (find = ISC_LIST_HEAD(fctx->altfinds);
853 find = ISC_LIST_NEXT(find, publink))
854 for (addrinfo = ISC_LIST_HEAD(find->list);
856 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
857 if (UNMARKED(addrinfo))
858 dns_adb_adjustsrtt(fctx->adb, addrinfo,
863 * Check for any outstanding socket events. If they exist, cancel
864 * them and let the event handlers finish the cleanup. The resolver
865 * only needs to worry about managing the connect and send events;
866 * the dispatcher manages the recv events.
868 if (RESQUERY_CONNECTING(query)) {
870 * Cancel the connect.
872 if (query->tcpsocket != NULL) {
873 isc_socket_cancel(query->tcpsocket, NULL,
874 ISC_SOCKCANCEL_CONNECT);
875 } else if (query->dispentry != NULL) {
876 INSIST(query->exclusivesocket);
877 socket = dns_dispatch_getentrysocket(query->dispentry);
879 isc_socket_cancel(socket, NULL,
880 ISC_SOCKCANCEL_CONNECT);
882 } else if (RESQUERY_SENDING(query)) {
884 * Cancel the pending send.
886 if (query->exclusivesocket && query->dispentry != NULL)
887 socket = dns_dispatch_getentrysocket(query->dispentry);
889 socket = dns_dispatch_getsocket(query->dispatch);
891 isc_socket_cancel(socket, NULL, ISC_SOCKCANCEL_SEND);
894 if (query->dispentry != NULL)
895 dns_dispatch_removeresponse(&query->dispentry, deventp);
897 ISC_LIST_UNLINK(fctx->queries, query, link);
899 if (query->tsig != NULL)
900 isc_buffer_free(&query->tsig);
902 if (query->tsigkey != NULL)
903 dns_tsigkey_detach(&query->tsigkey);
905 if (query->dispatch != NULL)
906 dns_dispatch_detach(&query->dispatch);
908 if (! (RESQUERY_CONNECTING(query) || RESQUERY_SENDING(query)))
910 * It's safe to destroy the query now.
912 resquery_destroy(&query);
916 fctx_cancelqueries(fetchctx_t *fctx, isc_boolean_t no_response) {
917 resquery_t *query, *next_query;
919 FCTXTRACE("cancelqueries");
921 for (query = ISC_LIST_HEAD(fctx->queries);
923 query = next_query) {
924 next_query = ISC_LIST_NEXT(query, link);
925 fctx_cancelquery(&query, NULL, NULL, no_response);
930 fctx_cleanupfinds(fetchctx_t *fctx) {
931 dns_adbfind_t *find, *next_find;
933 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
935 for (find = ISC_LIST_HEAD(fctx->finds);
938 next_find = ISC_LIST_NEXT(find, publink);
939 ISC_LIST_UNLINK(fctx->finds, find, publink);
940 dns_adb_destroyfind(&find);
946 fctx_cleanupaltfinds(fetchctx_t *fctx) {
947 dns_adbfind_t *find, *next_find;
949 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
951 for (find = ISC_LIST_HEAD(fctx->altfinds);
954 next_find = ISC_LIST_NEXT(find, publink);
955 ISC_LIST_UNLINK(fctx->altfinds, find, publink);
956 dns_adb_destroyfind(&find);
958 fctx->altfind = NULL;
962 fctx_cleanupforwaddrs(fetchctx_t *fctx) {
963 dns_adbaddrinfo_t *addr, *next_addr;
965 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
967 for (addr = ISC_LIST_HEAD(fctx->forwaddrs);
970 next_addr = ISC_LIST_NEXT(addr, publink);
971 ISC_LIST_UNLINK(fctx->forwaddrs, addr, publink);
972 dns_adb_freeaddrinfo(fctx->adb, &addr);
977 fctx_cleanupaltaddrs(fetchctx_t *fctx) {
978 dns_adbaddrinfo_t *addr, *next_addr;
980 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
982 for (addr = ISC_LIST_HEAD(fctx->altaddrs);
985 next_addr = ISC_LIST_NEXT(addr, publink);
986 ISC_LIST_UNLINK(fctx->altaddrs, addr, publink);
987 dns_adb_freeaddrinfo(fctx->adb, &addr);
992 fctx_stopeverything(fetchctx_t *fctx, isc_boolean_t no_response) {
993 FCTXTRACE("stopeverything");
994 fctx_cancelqueries(fctx, no_response);
995 fctx_cleanupfinds(fctx);
996 fctx_cleanupaltfinds(fctx);
997 fctx_cleanupforwaddrs(fctx);
998 fctx_cleanupaltaddrs(fctx);
999 fctx_stoptimer(fctx);
1003 fctx_sendevents(fetchctx_t *fctx, isc_result_t result, int line) {
1004 dns_fetchevent_t *event, *next_event;
1006 unsigned int count = 0;
1008 isc_boolean_t logit = ISC_FALSE;
1010 unsigned int old_spillat;
1011 unsigned int new_spillat = 0; /* initialized to silence
1012 compiler warnings */
1015 * Caller must be holding the appropriate bucket lock.
1017 REQUIRE(fctx->state == fetchstate_done);
1019 FCTXTRACE("sendevents");
1022 * Keep some record of fetch result for logging later (if required).
1024 fctx->result = result;
1025 fctx->exitline = line;
1027 fctx->duration = isc_time_microdiff(&now, &fctx->start);
1029 for (event = ISC_LIST_HEAD(fctx->events);
1031 event = next_event) {
1032 next_event = ISC_LIST_NEXT(event, ev_link);
1033 ISC_LIST_UNLINK(fctx->events, event, ev_link);
1034 task = event->ev_sender;
1035 event->ev_sender = fctx;
1036 if (!HAVE_ANSWER(fctx))
1037 event->result = result;
1039 INSIST(result != ISC_R_SUCCESS ||
1040 dns_rdataset_isassociated(event->rdataset) ||
1041 fctx->type == dns_rdatatype_any ||
1042 fctx->type == dns_rdatatype_rrsig ||
1043 fctx->type == dns_rdatatype_sig);
1046 * Negative results must be indicated in event->result.
1048 if (dns_rdataset_isassociated(event->rdataset) &&
1049 event->rdataset->type == dns_rdatatype_none) {
1050 INSIST(event->result == DNS_R_NCACHENXDOMAIN ||
1051 event->result == DNS_R_NCACHENXRRSET);
1054 isc_task_sendanddetach(&task, ISC_EVENT_PTR(&event));
1058 if ((fctx->attributes & FCTX_ATTR_HAVEANSWER) != 0 &&
1060 (count < fctx->res->spillatmax || fctx->res->spillatmax == 0)) {
1061 LOCK(&fctx->res->lock);
1062 if (count == fctx->res->spillat && !fctx->res->exiting) {
1063 old_spillat = fctx->res->spillat;
1064 fctx->res->spillat += 5;
1065 if (fctx->res->spillat > fctx->res->spillatmax &&
1066 fctx->res->spillatmax != 0)
1067 fctx->res->spillat = fctx->res->spillatmax;
1068 new_spillat = fctx->res->spillat;
1069 if (new_spillat != old_spillat) {
1072 isc_interval_set(&i, 20 * 60, 0);
1073 result = isc_timer_reset(fctx->res->spillattimer,
1074 isc_timertype_ticker, NULL,
1076 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1078 UNLOCK(&fctx->res->lock);
1080 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
1081 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
1082 "clients-per-query increased to %u",
1088 log_edns(fetchctx_t *fctx) {
1089 char domainbuf[DNS_NAME_FORMATSIZE];
1091 if (fctx->reason == NULL)
1094 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
1095 isc_log_write(dns_lctx, DNS_LOGCATEGORY_EDNS_DISABLED,
1096 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
1097 "success resolving '%s' (in '%s'?) after %s",
1098 fctx->info, domainbuf, fctx->reason);
1100 fctx->reason = NULL;
1104 fctx_done(fetchctx_t *fctx, isc_result_t result, int line) {
1105 dns_resolver_t *res;
1106 isc_boolean_t no_response;
1114 if (result == ISC_R_SUCCESS) {
1116 * Log any deferred EDNS timeout messages.
1119 no_response = ISC_TRUE;
1121 no_response = ISC_FALSE;
1123 fctx->reason = NULL;
1124 fctx_stopeverything(fctx, no_response);
1126 LOCK(&res->buckets[fctx->bucketnum].lock);
1128 fctx->state = fetchstate_done;
1129 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1130 fctx_sendevents(fctx, result, line);
1132 UNLOCK(&res->buckets[fctx->bucketnum].lock);
1136 process_sendevent(resquery_t *query, isc_event_t *event) {
1137 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1138 isc_boolean_t retry = ISC_FALSE;
1139 isc_result_t result;
1144 if (RESQUERY_CANCELED(query)) {
1145 if (query->sends == 0 && query->connects == 0) {
1147 * This query was canceled while the
1148 * isc_socket_sendto/connect() was in progress.
1150 if (query->tcpsocket != NULL)
1151 isc_socket_detach(&query->tcpsocket);
1152 resquery_destroy(&query);
1155 switch (sevent->result) {
1159 case ISC_R_HOSTUNREACH:
1160 case ISC_R_NETUNREACH:
1162 case ISC_R_ADDRNOTAVAIL:
1163 case ISC_R_CONNREFUSED:
1166 * No route to remote.
1168 add_bad(fctx, query->addrinfo, sevent->result,
1170 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
1175 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
1180 isc_event_free(&event);
1184 * Behave as if the idle timer has expired. For TCP
1185 * this may not actually reflect the latest timer.
1187 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1188 result = fctx_stopidletimer(fctx);
1189 if (result != ISC_R_SUCCESS)
1190 fctx_done(fctx, result, __LINE__);
1192 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
1197 resquery_udpconnected(isc_task_t *task, isc_event_t *event) {
1198 resquery_t *query = event->ev_arg;
1200 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
1202 QTRACE("udpconnected");
1206 INSIST(RESQUERY_CONNECTING(query));
1210 process_sendevent(query, event);
1214 resquery_senddone(isc_task_t *task, isc_event_t *event) {
1215 resquery_t *query = event->ev_arg;
1217 REQUIRE(event->ev_type == ISC_SOCKEVENT_SENDDONE);
1224 * Currently we don't wait for the senddone event before retrying
1225 * a query. This means that if we get really behind, we may end
1226 * up doing extra work!
1231 INSIST(RESQUERY_SENDING(query));
1235 process_sendevent(query, event);
1238 static inline isc_result_t
1239 fctx_addopt(dns_message_t *message, unsigned int version,
1240 isc_uint16_t udpsize, isc_boolean_t request_nsid)
1242 dns_rdataset_t *rdataset;
1243 dns_rdatalist_t *rdatalist;
1245 isc_result_t result;
1248 result = dns_message_gettemprdatalist(message, &rdatalist);
1249 if (result != ISC_R_SUCCESS)
1252 result = dns_message_gettemprdata(message, &rdata);
1253 if (result != ISC_R_SUCCESS)
1256 result = dns_message_gettemprdataset(message, &rdataset);
1257 if (result != ISC_R_SUCCESS)
1259 dns_rdataset_init(rdataset);
1261 rdatalist->type = dns_rdatatype_opt;
1262 rdatalist->covers = 0;
1265 * Set Maximum UDP buffer size.
1267 rdatalist->rdclass = udpsize;
1270 * Set EXTENDED-RCODE and Z to 0, DO to 1.
1272 rdatalist->ttl = (version << 16);
1273 rdatalist->ttl |= DNS_MESSAGEEXTFLAG_DO;
1276 * Set EDNS options if applicable
1279 /* Send empty NSID option (RFC5001) */
1280 unsigned char data[4];
1283 isc_buffer_init(&buf, data, sizeof(data));
1284 isc_buffer_putuint16(&buf, DNS_OPT_NSID);
1285 isc_buffer_putuint16(&buf, 0);
1287 rdata->length = sizeof(data);
1293 rdata->rdclass = rdatalist->rdclass;
1294 rdata->type = rdatalist->type;
1297 ISC_LIST_INIT(rdatalist->rdata);
1298 ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
1299 RUNTIME_CHECK(dns_rdatalist_tordataset(rdatalist, rdataset) == ISC_R_SUCCESS);
1301 return (dns_message_setopt(message, rdataset));
1305 fctx_setretryinterval(fetchctx_t *fctx, unsigned int rtt) {
1306 unsigned int seconds;
1310 * We retry every .8 seconds the first two times through the address
1311 * list, and then we do exponential back-off.
1313 if (fctx->restarts < 3)
1316 us = (800000 << (fctx->restarts - 2));
1319 * Double the round-trip time.
1324 * Always wait for at least the doubled round-trip time.
1330 * But don't ever wait for more than 10 seconds.
1335 seconds = us / 1000000;
1336 us -= seconds * 1000000;
1337 isc_interval_set(&fctx->interval, seconds, us * 1000);
1341 fctx_query(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
1342 unsigned int options)
1344 dns_resolver_t *res;
1346 isc_result_t result;
1348 isc_sockaddr_t addr;
1349 isc_boolean_t have_addr = ISC_FALSE;
1355 task = res->buckets[fctx->bucketnum].task;
1357 srtt = addrinfo->srtt;
1358 if (ISFORWARDER(addrinfo) && srtt < 1000000)
1361 fctx_setretryinterval(fctx, srtt);
1362 result = fctx_startidletimer(fctx, &fctx->interval);
1363 if (result != ISC_R_SUCCESS)
1366 INSIST(ISC_LIST_EMPTY(fctx->validators));
1368 dns_message_reset(fctx->rmessage, DNS_MESSAGE_INTENTPARSE);
1370 query = isc_mem_get(res->buckets[fctx->bucketnum].mctx,
1372 if (query == NULL) {
1373 result = ISC_R_NOMEMORY;
1374 goto stop_idle_timer;
1376 query->mctx = res->buckets[fctx->bucketnum].mctx;
1377 query->options = options;
1378 query->attributes = 0;
1380 query->connects = 0;
1382 * Note that the caller MUST guarantee that 'addrinfo' will remain
1383 * valid until this query is canceled.
1385 query->addrinfo = addrinfo;
1386 TIME_NOW(&query->start);
1389 * If this is a TCP query, then we need to make a socket and
1390 * a dispatch for it here. Otherwise we use the resolver's
1393 query->dispatchmgr = res->dispatchmgr;
1394 query->dispatch = NULL;
1395 query->exclusivesocket = ISC_FALSE;
1396 query->tcpsocket = NULL;
1397 if (res->view->peers != NULL) {
1398 dns_peer_t *peer = NULL;
1399 isc_netaddr_t dstip;
1400 isc_netaddr_fromsockaddr(&dstip, &addrinfo->sockaddr);
1401 result = dns_peerlist_peerbyaddr(res->view->peers,
1403 if (result == ISC_R_SUCCESS) {
1404 result = dns_peer_getquerysource(peer, &addr);
1405 if (result == ISC_R_SUCCESS)
1406 have_addr = ISC_TRUE;
1410 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1413 pf = isc_sockaddr_pf(&addrinfo->sockaddr);
1418 dns_dispatch_getlocaladdress(res->dispatchv4,
1423 dns_dispatch_getlocaladdress(res->dispatchv6,
1427 result = ISC_R_NOTIMPLEMENTED;
1430 if (result != ISC_R_SUCCESS)
1433 isc_sockaddr_setport(&addr, 0);
1435 result = isc_socket_create(res->socketmgr, pf,
1438 if (result != ISC_R_SUCCESS)
1441 #ifndef BROKEN_TCP_BIND_BEFORE_CONNECT
1442 result = isc_socket_bind(query->tcpsocket, &addr, 0);
1443 if (result != ISC_R_SUCCESS)
1444 goto cleanup_socket;
1448 * A dispatch will be created once the connect succeeds.
1452 unsigned int attrs, attrmask;
1453 attrs = DNS_DISPATCHATTR_UDP;
1454 switch (isc_sockaddr_pf(&addr)) {
1456 attrs |= DNS_DISPATCHATTR_IPV4;
1459 attrs |= DNS_DISPATCHATTR_IPV6;
1462 result = ISC_R_NOTIMPLEMENTED;
1465 attrmask = DNS_DISPATCHATTR_UDP;
1466 attrmask |= DNS_DISPATCHATTR_TCP;
1467 attrmask |= DNS_DISPATCHATTR_IPV4;
1468 attrmask |= DNS_DISPATCHATTR_IPV6;
1469 result = dns_dispatch_getudp(res->dispatchmgr,
1471 res->taskmgr, &addr,
1472 4096, 1000, 32768, 16411,
1473 16433, attrs, attrmask,
1475 if (result != ISC_R_SUCCESS)
1478 switch (isc_sockaddr_pf(&addrinfo->sockaddr)) {
1480 dns_dispatch_attach(res->dispatchv4,
1482 query->exclusivesocket = res->exclusivev4;
1485 dns_dispatch_attach(res->dispatchv6,
1487 query->exclusivesocket = res->exclusivev6;
1490 result = ISC_R_NOTIMPLEMENTED;
1495 * We should always have a valid dispatcher here. If we
1496 * don't support a protocol family, then its dispatcher
1497 * will be NULL, but we shouldn't be finding addresses for
1498 * protocol types we don't support, so the dispatcher
1499 * we found should never be NULL.
1501 INSIST(query->dispatch != NULL);
1504 query->dispentry = NULL;
1507 query->tsigkey = NULL;
1508 ISC_LINK_INIT(query, link);
1509 query->magic = QUERY_MAGIC;
1511 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1513 * Connect to the remote server.
1515 * XXXRTH Should we attach to the socket?
1517 result = isc_socket_connect(query->tcpsocket,
1518 &addrinfo->sockaddr, task,
1519 resquery_connected, query);
1520 if (result != ISC_R_SUCCESS)
1521 goto cleanup_socket;
1523 QTRACE("connecting via TCP");
1525 result = resquery_send(query);
1526 if (result != ISC_R_SUCCESS)
1527 goto cleanup_dispatch;
1531 ISC_LIST_APPEND(fctx->queries, query, link);
1532 query->fctx->nqueries++;
1533 if (isc_sockaddr_pf(&addrinfo->sockaddr) == PF_INET)
1534 inc_stats(res, dns_resstatscounter_queryv4);
1536 inc_stats(res, dns_resstatscounter_queryv6);
1537 if (res->view->resquerystats != NULL)
1538 dns_rdatatypestats_increment(res->view->resquerystats,
1541 return (ISC_R_SUCCESS);
1544 isc_socket_detach(&query->tcpsocket);
1547 if (query->dispatch != NULL)
1548 dns_dispatch_detach(&query->dispatch);
1552 isc_mem_put(res->buckets[fctx->bucketnum].mctx,
1553 query, sizeof(*query));
1556 RUNTIME_CHECK(fctx_stopidletimer(fctx) == ISC_R_SUCCESS);
1561 static isc_boolean_t
1562 triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1565 for (sa = ISC_LIST_HEAD(fctx->edns);
1567 sa = ISC_LIST_NEXT(sa, link)) {
1568 if (isc_sockaddr_equal(sa, address))
1576 add_triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1579 if (triededns(fctx, address))
1582 sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
1588 ISC_LIST_INITANDAPPEND(fctx->edns, sa, link);
1591 static isc_boolean_t
1592 triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1595 for (sa = ISC_LIST_HEAD(fctx->edns512);
1597 sa = ISC_LIST_NEXT(sa, link)) {
1598 if (isc_sockaddr_equal(sa, address))
1606 add_triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1609 if (triededns512(fctx, address))
1612 sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
1618 ISC_LIST_INITANDAPPEND(fctx->edns512, sa, link);
1622 resquery_send(resquery_t *query) {
1624 isc_result_t result;
1625 dns_name_t *qname = NULL;
1626 dns_rdataset_t *qrdataset = NULL;
1628 dns_resolver_t *res;
1630 isc_socket_t *socket;
1631 isc_buffer_t tcpbuffer;
1632 isc_sockaddr_t *address;
1633 isc_buffer_t *buffer;
1634 isc_netaddr_t ipaddr;
1635 dns_tsigkey_t *tsigkey = NULL;
1636 dns_peer_t *peer = NULL;
1637 isc_boolean_t useedns;
1638 dns_compress_t cctx;
1639 isc_boolean_t cleanup_cctx = ISC_FALSE;
1640 isc_boolean_t secure_domain;
1646 task = res->buckets[fctx->bucketnum].task;
1649 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1651 * Reserve space for the TCP message length.
1653 isc_buffer_init(&tcpbuffer, query->data, sizeof(query->data));
1654 isc_buffer_init(&query->buffer, query->data + 2,
1655 sizeof(query->data) - 2);
1656 buffer = &tcpbuffer;
1658 isc_buffer_init(&query->buffer, query->data,
1659 sizeof(query->data));
1660 buffer = &query->buffer;
1663 result = dns_message_gettempname(fctx->qmessage, &qname);
1664 if (result != ISC_R_SUCCESS)
1666 result = dns_message_gettemprdataset(fctx->qmessage, &qrdataset);
1667 if (result != ISC_R_SUCCESS)
1671 * Get a query id from the dispatch.
1673 result = dns_dispatch_addresponse2(query->dispatch,
1674 &query->addrinfo->sockaddr,
1681 if (result != ISC_R_SUCCESS)
1684 fctx->qmessage->opcode = dns_opcode_query;
1689 dns_name_init(qname, NULL);
1690 dns_name_clone(&fctx->name, qname);
1691 dns_rdataset_init(qrdataset);
1692 dns_rdataset_makequestion(qrdataset, res->rdclass, fctx->type);
1693 ISC_LIST_APPEND(qname->list, qrdataset, link);
1694 dns_message_addname(fctx->qmessage, qname, DNS_SECTION_QUESTION);
1699 * Set RD if the client has requested that we do a recursive query,
1700 * or if we're sending to a forwarder.
1702 if ((query->options & DNS_FETCHOPT_RECURSIVE) != 0 ||
1703 ISFORWARDER(query->addrinfo))
1704 fctx->qmessage->flags |= DNS_MESSAGEFLAG_RD;
1707 * Set CD if the client says don't validate or the question is
1708 * under a secure entry point.
1710 if ((query->options & DNS_FETCHOPT_NOVALIDATE) != 0) {
1711 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1712 } else if (res->view->enablevalidation) {
1713 result = dns_keytable_issecuredomain(res->view->secroots,
1716 if (result != ISC_R_SUCCESS)
1717 secure_domain = ISC_FALSE;
1718 if (res->view->dlv != NULL)
1719 secure_domain = ISC_TRUE;
1721 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1725 * We don't have to set opcode because it defaults to query.
1727 fctx->qmessage->id = query->id;
1730 * Convert the question to wire format.
1732 result = dns_compress_init(&cctx, -1, fctx->res->mctx);
1733 if (result != ISC_R_SUCCESS)
1734 goto cleanup_message;
1735 cleanup_cctx = ISC_TRUE;
1737 result = dns_message_renderbegin(fctx->qmessage, &cctx,
1739 if (result != ISC_R_SUCCESS)
1740 goto cleanup_message;
1742 result = dns_message_rendersection(fctx->qmessage,
1743 DNS_SECTION_QUESTION, 0);
1744 if (result != ISC_R_SUCCESS)
1745 goto cleanup_message;
1748 isc_netaddr_fromsockaddr(&ipaddr, &query->addrinfo->sockaddr);
1749 (void) dns_peerlist_peerbyaddr(fctx->res->view->peers, &ipaddr, &peer);
1752 * The ADB does not know about servers with "edns no". Check this,
1753 * and then inform the ADB for future use.
1755 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0 &&
1757 dns_peer_getsupportedns(peer, &useedns) == ISC_R_SUCCESS &&
1760 query->options |= DNS_FETCHOPT_NOEDNS0;
1761 dns_adb_changeflags(fctx->adb, query->addrinfo,
1762 DNS_FETCHOPT_NOEDNS0,
1763 DNS_FETCHOPT_NOEDNS0);
1766 /* Sync NOEDNS0 flag in addrinfo->flags and options now. */
1767 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) != 0)
1768 query->options |= DNS_FETCHOPT_NOEDNS0;
1771 * Handle timeouts by reducing the UDP response size to 512 bytes
1772 * then if that doesn't work disabling EDNS (includes DO) and CD.
1774 * These timeout can be due to:
1775 * * broken nameservers that don't respond to EDNS queries.
1776 * * broken/misconfigured firewalls and NAT implementations
1777 * that don't handle IP fragmentation.
1778 * * broken/misconfigured firewalls that don't handle responses
1779 * greater than 512 bytes.
1780 * * broken/misconfigured firewalls that don't handle EDNS, DO
1782 * * packet loss / link outage.
1784 if (fctx->timeout) {
1785 if ((triededns512(fctx, &query->addrinfo->sockaddr) ||
1786 fctx->timeouts >= (MAX_EDNS0_TIMEOUTS * 2)) &&
1787 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1788 query->options |= DNS_FETCHOPT_NOEDNS0;
1789 fctx->reason = "disabling EDNS";
1790 } else if ((triededns(fctx, &query->addrinfo->sockaddr) ||
1791 fctx->timeouts >= MAX_EDNS0_TIMEOUTS) &&
1792 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1793 query->options |= DNS_FETCHOPT_EDNS512;
1794 fctx->reason = "reducing the advertised EDNS UDP "
1795 "packet size to 512 octets";
1797 fctx->timeout = ISC_FALSE;
1801 * Use EDNS0, unless the caller doesn't want it, or we know that
1802 * the remote server doesn't like it.
1804 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1805 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0) {
1806 unsigned int version = 0; /* Default version. */
1808 isc_uint16_t udpsize = res->udpsize;
1809 isc_boolean_t reqnsid = res->view->requestnsid;
1811 flags = query->addrinfo->flags;
1812 if ((flags & DNS_FETCHOPT_EDNSVERSIONSET) != 0) {
1813 version = flags & DNS_FETCHOPT_EDNSVERSIONMASK;
1814 version >>= DNS_FETCHOPT_EDNSVERSIONSHIFT;
1816 if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
1818 else if (peer != NULL)
1819 (void)dns_peer_getudpsize(peer, &udpsize);
1821 /* request NSID for current view or peer? */
1823 (void) dns_peer_getrequestnsid(peer, &reqnsid);
1824 result = fctx_addopt(fctx->qmessage, version,
1826 if (reqnsid && result == ISC_R_SUCCESS) {
1827 query->options |= DNS_FETCHOPT_WANTNSID;
1828 } else if (result != ISC_R_SUCCESS) {
1830 * We couldn't add the OPT, but we'll press on.
1831 * We're not using EDNS0, so set the NOEDNS0
1834 query->options |= DNS_FETCHOPT_NOEDNS0;
1838 * We know this server doesn't like EDNS0, so we
1839 * won't use it. Set the NOEDNS0 bit since we're
1842 query->options |= DNS_FETCHOPT_NOEDNS0;
1847 * If we need EDNS0 to do this query and aren't using it, we lose.
1849 if (NEEDEDNS0(fctx) && (query->options & DNS_FETCHOPT_NOEDNS0) != 0) {
1850 result = DNS_R_SERVFAIL;
1851 goto cleanup_message;
1854 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0)
1855 add_triededns(fctx, &query->addrinfo->sockaddr);
1857 if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
1858 add_triededns512(fctx, &query->addrinfo->sockaddr);
1861 * Clear CD if EDNS is not in use.
1863 if ((query->options & DNS_FETCHOPT_NOEDNS0) != 0)
1864 fctx->qmessage->flags &= ~DNS_MESSAGEFLAG_CD;
1867 * Add TSIG record tailored to the current recipient.
1869 result = dns_view_getpeertsig(fctx->res->view, &ipaddr, &tsigkey);
1870 if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND)
1871 goto cleanup_message;
1873 if (tsigkey != NULL) {
1874 result = dns_message_settsigkey(fctx->qmessage, tsigkey);
1875 dns_tsigkey_detach(&tsigkey);
1876 if (result != ISC_R_SUCCESS)
1877 goto cleanup_message;
1880 result = dns_message_rendersection(fctx->qmessage,
1881 DNS_SECTION_ADDITIONAL, 0);
1882 if (result != ISC_R_SUCCESS)
1883 goto cleanup_message;
1885 result = dns_message_renderend(fctx->qmessage);
1886 if (result != ISC_R_SUCCESS)
1887 goto cleanup_message;
1889 dns_compress_invalidate(&cctx);
1890 cleanup_cctx = ISC_FALSE;
1892 if (dns_message_gettsigkey(fctx->qmessage) != NULL) {
1893 dns_tsigkey_attach(dns_message_gettsigkey(fctx->qmessage),
1895 result = dns_message_getquerytsig(fctx->qmessage,
1898 if (result != ISC_R_SUCCESS)
1899 goto cleanup_message;
1903 * If using TCP, write the length of the message at the beginning
1906 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1907 isc_buffer_usedregion(&query->buffer, &r);
1908 isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t)r.length);
1909 isc_buffer_add(&tcpbuffer, r.length);
1913 * We're now done with the query message.
1915 dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
1917 if (query->exclusivesocket)
1918 socket = dns_dispatch_getentrysocket(query->dispentry);
1920 socket = dns_dispatch_getsocket(query->dispatch);
1924 if ((query->options & DNS_FETCHOPT_TCP) == 0) {
1925 address = &query->addrinfo->sockaddr;
1926 if (query->exclusivesocket) {
1927 result = isc_socket_connect(socket, address, task,
1928 resquery_udpconnected,
1930 if (result != ISC_R_SUCCESS)
1931 goto cleanup_message;
1935 isc_buffer_usedregion(buffer, &r);
1938 * XXXRTH Make sure we don't send to ourselves! We should probably
1939 * prune out these addresses when we get them from the ADB.
1941 result = isc_socket_sendto(socket, &r, task, resquery_senddone,
1942 query, address, NULL);
1943 if (result != ISC_R_SUCCESS)
1944 goto cleanup_message;
1950 return (ISC_R_SUCCESS);
1954 dns_compress_invalidate(&cctx);
1956 dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
1959 * Stop the dispatcher from listening.
1961 dns_dispatch_removeresponse(&query->dispentry, NULL);
1965 dns_message_puttempname(fctx->qmessage, &qname);
1966 if (qrdataset != NULL)
1967 dns_message_puttemprdataset(fctx->qmessage, &qrdataset);
1973 resquery_connected(isc_task_t *task, isc_event_t *event) {
1974 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1975 resquery_t *query = event->ev_arg;
1976 isc_boolean_t retry = ISC_FALSE;
1977 isc_interval_t interval;
1978 isc_result_t result;
1982 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
1983 REQUIRE(VALID_QUERY(query));
1985 QTRACE("connected");
1992 * Currently we don't wait for the connect event before retrying
1993 * a query. This means that if we get really behind, we may end
1994 * up doing extra work!
2000 if (RESQUERY_CANCELED(query)) {
2002 * This query was canceled while the connect() was in
2005 isc_socket_detach(&query->tcpsocket);
2006 resquery_destroy(&query);
2008 switch (sevent->result) {
2012 * Extend the idle timer for TCP. 20 seconds
2013 * should be long enough for a TCP connection to be
2014 * established, a single DNS request to be sent,
2015 * and the response received.
2017 isc_interval_set(&interval, 20, 0);
2018 result = fctx_startidletimer(query->fctx, &interval);
2019 if (result != ISC_R_SUCCESS) {
2020 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2021 fctx_done(fctx, result, __LINE__);
2025 * We are connected. Create a dispatcher and
2029 attrs |= DNS_DISPATCHATTR_TCP;
2030 attrs |= DNS_DISPATCHATTR_PRIVATE;
2031 attrs |= DNS_DISPATCHATTR_CONNECTED;
2032 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) ==
2034 attrs |= DNS_DISPATCHATTR_IPV4;
2036 attrs |= DNS_DISPATCHATTR_IPV6;
2037 attrs |= DNS_DISPATCHATTR_MAKEQUERY;
2039 result = dns_dispatch_createtcp(query->dispatchmgr,
2041 query->fctx->res->taskmgr,
2042 4096, 2, 1, 1, 3, attrs,
2046 * Regardless of whether dns_dispatch_create()
2047 * succeeded or not, we don't need our reference
2048 * to the socket anymore.
2050 isc_socket_detach(&query->tcpsocket);
2052 if (result == ISC_R_SUCCESS)
2053 result = resquery_send(query);
2055 if (result != ISC_R_SUCCESS) {
2056 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2057 fctx_done(fctx, result, __LINE__);
2061 case ISC_R_NETUNREACH:
2062 case ISC_R_HOSTUNREACH:
2063 case ISC_R_CONNREFUSED:
2065 case ISC_R_ADDRNOTAVAIL:
2066 case ISC_R_CONNECTIONRESET:
2068 * No route to remote.
2070 isc_socket_detach(&query->tcpsocket);
2071 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
2076 isc_socket_detach(&query->tcpsocket);
2077 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2082 isc_event_free(&event);
2086 * Behave as if the idle timer has expired. For TCP
2087 * connections this may not actually reflect the latest timer.
2089 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2090 result = fctx_stopidletimer(fctx);
2091 if (result != ISC_R_SUCCESS)
2092 fctx_done(fctx, result, __LINE__);
2094 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
2099 fctx_finddone(isc_task_t *task, isc_event_t *event) {
2101 dns_adbfind_t *find;
2102 dns_resolver_t *res;
2103 isc_boolean_t want_try = ISC_FALSE;
2104 isc_boolean_t want_done = ISC_FALSE;
2105 isc_boolean_t bucket_empty = ISC_FALSE;
2106 unsigned int bucketnum;
2108 find = event->ev_sender;
2109 fctx = event->ev_arg;
2110 REQUIRE(VALID_FCTX(fctx));
2115 FCTXTRACE("finddone");
2117 INSIST(fctx->pending > 0);
2120 if (ADDRWAIT(fctx)) {
2122 * The fetch is waiting for a name to be found.
2124 INSIST(!SHUTTINGDOWN(fctx));
2125 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2126 if (event->ev_type == DNS_EVENT_ADBMOREADDRESSES)
2127 want_try = ISC_TRUE;
2130 if (fctx->pending == 0) {
2132 * We've got nothing else to wait for and don't
2133 * know the answer. There's nothing to do but
2136 want_done = ISC_TRUE;
2139 } else if (SHUTTINGDOWN(fctx) && fctx->pending == 0 &&
2140 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators)) {
2141 bucketnum = fctx->bucketnum;
2142 LOCK(&res->buckets[bucketnum].lock);
2144 * Note that we had to wait until we had the lock before
2145 * looking at fctx->references.
2147 if (fctx->references == 0)
2148 bucket_empty = fctx_destroy(fctx);
2149 UNLOCK(&res->buckets[bucketnum].lock);
2152 isc_event_free(&event);
2153 dns_adb_destroyfind(&find);
2156 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
2158 fctx_done(fctx, ISC_R_FAILURE, __LINE__);
2159 else if (bucket_empty)
2164 static inline isc_boolean_t
2165 bad_server(fetchctx_t *fctx, isc_sockaddr_t *address) {
2168 for (sa = ISC_LIST_HEAD(fctx->bad);
2170 sa = ISC_LIST_NEXT(sa, link)) {
2171 if (isc_sockaddr_equal(sa, address))
2178 static inline isc_boolean_t
2179 mark_bad(fetchctx_t *fctx) {
2180 dns_adbfind_t *curr;
2181 dns_adbaddrinfo_t *addrinfo;
2182 isc_boolean_t all_bad = ISC_TRUE;
2185 * Mark all known bad servers, so we don't try to talk to them
2190 * Mark any bad nameservers.
2192 for (curr = ISC_LIST_HEAD(fctx->finds);
2194 curr = ISC_LIST_NEXT(curr, publink)) {
2195 for (addrinfo = ISC_LIST_HEAD(curr->list);
2197 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2198 if (bad_server(fctx, &addrinfo->sockaddr))
2199 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2201 all_bad = ISC_FALSE;
2206 * Mark any bad forwarders.
2208 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2210 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2211 if (bad_server(fctx, &addrinfo->sockaddr))
2212 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2214 all_bad = ISC_FALSE;
2218 * Mark any bad alternates.
2220 for (curr = ISC_LIST_HEAD(fctx->altfinds);
2222 curr = ISC_LIST_NEXT(curr, publink)) {
2223 for (addrinfo = ISC_LIST_HEAD(curr->list);
2225 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2226 if (bad_server(fctx, &addrinfo->sockaddr))
2227 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2229 all_bad = ISC_FALSE;
2233 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
2235 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2236 if (bad_server(fctx, &addrinfo->sockaddr))
2237 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2239 all_bad = ISC_FALSE;
2246 add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_result_t reason,
2247 badnstype_t badtype)
2249 char namebuf[DNS_NAME_FORMATSIZE];
2250 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
2256 const char *sep1, *sep2;
2257 isc_sockaddr_t *address = &addrinfo->sockaddr;
2259 if (reason == DNS_R_LAME)
2263 case badns_unreachable:
2266 case badns_response:
2269 case badns_validation:
2270 break; /* counted as 'valfail' */
2274 if (bad_server(fctx, address)) {
2276 * We already know this server is bad.
2281 FCTXTRACE("add_bad");
2283 sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
2288 ISC_LIST_INITANDAPPEND(fctx->bad, sa, link);
2290 if (reason == DNS_R_LAME) /* already logged */
2293 if (reason == DNS_R_UNEXPECTEDRCODE &&
2294 fctx->rmessage->rcode == dns_rcode_servfail &&
2295 ISFORWARDER(addrinfo))
2298 if (reason == DNS_R_UNEXPECTEDRCODE) {
2299 isc_buffer_init(&b, code, sizeof(code) - 1);
2300 dns_rcode_totext(fctx->rmessage->rcode, &b);
2301 code[isc_buffer_usedlength(&b)] = '\0';
2304 } else if (reason == DNS_R_UNEXPECTEDOPCODE) {
2305 isc_buffer_init(&b, code, sizeof(code) - 1);
2306 dns_opcode_totext((dns_opcode_t)fctx->rmessage->opcode, &b);
2307 code[isc_buffer_usedlength(&b)] = '\0';
2315 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
2316 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
2317 dns_rdataclass_format(fctx->res->rdclass, classbuf, sizeof(classbuf));
2318 isc_sockaddr_format(address, addrbuf, sizeof(addrbuf));
2319 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
2320 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
2321 "%s %s%s%sresolving '%s/%s/%s': %s",
2322 dns_result_totext(reason), sep1, code, sep2,
2323 namebuf, typebuf, classbuf, addrbuf);
2327 * Return 'bits' bits of random entropy from fctx->rand_buf,
2328 * refreshing it by calling isc_random_get() whenever the requested
2329 * number of bits is greater than the number in the buffer.
2331 static inline isc_uint32_t
2332 random_bits(fetchctx_t *fctx, isc_uint32_t bits) {
2333 isc_uint32_t ret = 0;
2335 REQUIRE(VALID_FCTX(fctx));
2336 REQUIRE(bits <= 32);
2340 if (bits >= fctx->rand_bits) {
2341 /* if rand_bits == 0, this is unnecessary but harmless */
2342 bits -= fctx->rand_bits;
2343 ret = fctx->rand_buf << bits;
2345 /* refresh random buffer now */
2346 isc_random_get(&fctx->rand_buf);
2347 fctx->rand_bits = sizeof(fctx->rand_buf) * CHAR_BIT;
2351 isc_uint32_t mask = 0xffffffff;
2353 mask = (1 << bits) - 1;
2356 ret |= fctx->rand_buf & mask;
2357 fctx->rand_buf >>= bits;
2358 fctx->rand_bits -= bits;
2365 * Add some random jitter to a server's RTT value so that the
2366 * order of queries will be unpredictable.
2368 * RTT values of servers which have been tried are fuzzed by 128 ms.
2369 * Servers that haven't been tried yet have their RTT set to a random
2370 * value between 0 ms and 7 ms; they should get to go first, but in
2371 * unpredictable order.
2374 randomize_srtt(fetchctx_t *fctx, dns_adbaddrinfo_t *ai) {
2376 ai->srtt >>= 10; /* convert to milliseconds, near enough */
2377 ai->srtt |= (ai->srtt & 0x80) | random_bits(fctx, 7);
2378 ai->srtt <<= 10; /* now back to microseconds */
2380 ai->srtt = random_bits(fctx, 3) << 10;
2384 * Sort addrinfo list by RTT (with random jitter)
2387 sort_adbfind(fetchctx_t *fctx, dns_adbfind_t *find) {
2388 dns_adbaddrinfo_t *best, *curr;
2389 dns_adbaddrinfolist_t sorted;
2391 /* Add jitter to SRTT values */
2392 curr = ISC_LIST_HEAD(find->list);
2393 while (curr != NULL) {
2394 randomize_srtt(fctx, curr);
2395 curr = ISC_LIST_NEXT(curr, publink);
2398 /* Lame N^2 bubble sort. */
2399 ISC_LIST_INIT(sorted);
2400 while (!ISC_LIST_EMPTY(find->list)) {
2401 best = ISC_LIST_HEAD(find->list);
2402 curr = ISC_LIST_NEXT(best, publink);
2403 while (curr != NULL) {
2404 if (curr->srtt < best->srtt)
2406 curr = ISC_LIST_NEXT(curr, publink);
2408 ISC_LIST_UNLINK(find->list, best, publink);
2409 ISC_LIST_APPEND(sorted, best, publink);
2411 find->list = sorted;
2415 * Sort a list of finds by server RTT (with random jitter)
2418 sort_finds(fetchctx_t *fctx, dns_adbfindlist_t *findlist) {
2419 dns_adbfind_t *best, *curr;
2420 dns_adbfindlist_t sorted;
2421 dns_adbaddrinfo_t *addrinfo, *bestaddrinfo;
2423 /* Sort each find's addrinfo list by SRTT (after adding jitter) */
2424 for (curr = ISC_LIST_HEAD(*findlist);
2426 curr = ISC_LIST_NEXT(curr, publink))
2427 sort_adbfind(fctx, curr);
2429 /* Lame N^2 bubble sort. */
2430 ISC_LIST_INIT(sorted);
2431 while (!ISC_LIST_EMPTY(*findlist)) {
2432 best = ISC_LIST_HEAD(*findlist);
2433 bestaddrinfo = ISC_LIST_HEAD(best->list);
2434 INSIST(bestaddrinfo != NULL);
2435 curr = ISC_LIST_NEXT(best, publink);
2436 while (curr != NULL) {
2437 addrinfo = ISC_LIST_HEAD(curr->list);
2438 INSIST(addrinfo != NULL);
2439 if (addrinfo->srtt < bestaddrinfo->srtt) {
2441 bestaddrinfo = addrinfo;
2443 curr = ISC_LIST_NEXT(curr, publink);
2445 ISC_LIST_UNLINK(*findlist, best, publink);
2446 ISC_LIST_APPEND(sorted, best, publink);
2452 findname(fetchctx_t *fctx, dns_name_t *name, in_port_t port,
2453 unsigned int options, unsigned int flags, isc_stdtime_t now,
2454 isc_boolean_t *need_alternate)
2456 dns_adbaddrinfo_t *ai;
2457 dns_adbfind_t *find;
2458 dns_resolver_t *res;
2459 isc_boolean_t unshared;
2460 isc_result_t result;
2463 unshared = ISC_TF((fctx->options | DNS_FETCHOPT_UNSHARED) != 0);
2465 * If this name is a subdomain of the query domain, tell
2466 * the ADB to start looking using zone/hint data. This keeps us
2467 * from getting stuck if the nameserver is beneath the zone cut
2468 * and we don't know its address (e.g. because the A record has
2471 if (dns_name_issubdomain(name, &fctx->domain))
2472 options |= DNS_ADBFIND_STARTATZONE;
2473 options |= DNS_ADBFIND_GLUEOK;
2474 options |= DNS_ADBFIND_HINTOK;
2477 * See what we know about this address.
2480 result = dns_adb_createfind(fctx->adb,
2481 res->buckets[fctx->bucketnum].task,
2482 fctx_finddone, fctx, name,
2483 &fctx->name, fctx->type,
2485 res->view->dstport, &find);
2486 if (result != ISC_R_SUCCESS) {
2487 if (result == DNS_R_ALIAS) {
2489 * XXXRTH Follow the CNAME/DNAME chain?
2491 dns_adb_destroyfind(&find);
2494 } else if (!ISC_LIST_EMPTY(find->list)) {
2496 * We have at least some of the addresses for the
2499 INSIST((find->options & DNS_ADBFIND_WANTEVENT) == 0);
2500 if (flags != 0 || port != 0) {
2501 for (ai = ISC_LIST_HEAD(find->list);
2503 ai = ISC_LIST_NEXT(ai, publink)) {
2506 isc_sockaddr_setport(&ai->sockaddr,
2510 if ((flags & FCTX_ADDRINFO_FORWARDER) != 0)
2511 ISC_LIST_APPEND(fctx->altfinds, find, publink);
2513 ISC_LIST_APPEND(fctx->finds, find, publink);
2516 * We don't know any of the addresses for this
2519 if ((find->options & DNS_ADBFIND_WANTEVENT) != 0) {
2521 * We're looking for them and will get an
2522 * event about it later.
2528 if (need_alternate != NULL &&
2529 !*need_alternate && unshared &&
2530 ((res->dispatchv4 == NULL &&
2531 find->result_v6 != DNS_R_NXDOMAIN) ||
2532 (res->dispatchv6 == NULL &&
2533 find->result_v4 != DNS_R_NXDOMAIN)))
2534 *need_alternate = ISC_TRUE;
2536 if ((find->options & DNS_ADBFIND_LAMEPRUNED) != 0)
2537 fctx->lamecount++; /* cached lame server */
2539 fctx->adberr++; /* unreachable server, etc. */
2542 * If we know there are no addresses for
2543 * the family we are using then try to add
2544 * an alternative server.
2546 if (need_alternate != NULL && !*need_alternate &&
2547 ((res->dispatchv4 == NULL &&
2548 find->result_v6 == DNS_R_NXRRSET) ||
2549 (res->dispatchv6 == NULL &&
2550 find->result_v4 == DNS_R_NXRRSET)))
2551 *need_alternate = ISC_TRUE;
2552 dns_adb_destroyfind(&find);
2557 static isc_boolean_t
2558 isstrictsubdomain(dns_name_t *name1, dns_name_t *name2) {
2560 unsigned int nlabels;
2561 dns_namereln_t namereln;
2563 namereln = dns_name_fullcompare(name1, name2, &order, &nlabels);
2564 return (ISC_TF(namereln == dns_namereln_subdomain));
2568 fctx_getaddresses(fetchctx_t *fctx, isc_boolean_t badcache) {
2569 dns_rdata_t rdata = DNS_RDATA_INIT;
2570 isc_result_t result;
2571 dns_resolver_t *res;
2573 unsigned int stdoptions;
2575 dns_adbaddrinfo_t *ai;
2576 isc_boolean_t all_bad;
2578 isc_boolean_t need_alternate = ISC_FALSE;
2580 FCTXTRACE("getaddresses");
2583 * Don't pound on remote servers. (Failsafe!)
2586 if (fctx->restarts > 10) {
2587 FCTXTRACE("too many restarts");
2588 return (DNS_R_SERVFAIL);
2592 stdoptions = 0; /* Keep compiler happy. */
2598 INSIST(ISC_LIST_EMPTY(fctx->forwaddrs));
2599 INSIST(ISC_LIST_EMPTY(fctx->altaddrs));
2602 * If this fctx has forwarders, use them; otherwise use any
2603 * selective forwarders specified in the view; otherwise use the
2604 * resolver's forwarders (if any).
2606 sa = ISC_LIST_HEAD(fctx->forwarders);
2608 dns_forwarders_t *forwarders = NULL;
2609 dns_name_t *name = &fctx->name;
2611 unsigned int labels;
2612 dns_fixedname_t fixed;
2616 * DS records are found in the parent server.
2617 * Strip label to get the correct forwarder (if any).
2619 if (dns_rdatatype_atparent(fctx->type) &&
2620 dns_name_countlabels(name) > 1) {
2621 dns_name_init(&suffix, NULL);
2622 labels = dns_name_countlabels(name);
2623 dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
2627 dns_fixedname_init(&fixed);
2628 domain = dns_fixedname_name(&fixed);
2629 result = dns_fwdtable_find2(fctx->res->view->fwdtable, name,
2630 domain, &forwarders);
2631 if (result == ISC_R_SUCCESS) {
2632 sa = ISC_LIST_HEAD(forwarders->addrs);
2633 fctx->fwdpolicy = forwarders->fwdpolicy;
2634 if (fctx->fwdpolicy == dns_fwdpolicy_only &&
2635 isstrictsubdomain(domain, &fctx->domain)) {
2638 mctx = res->buckets[fctx->bucketnum].mctx;
2639 dns_name_free(&fctx->domain, mctx);
2640 dns_name_init(&fctx->domain, NULL);
2641 result = dns_name_dup(domain, mctx,
2643 if (result != ISC_R_SUCCESS)
2649 while (sa != NULL) {
2650 if ((isc_sockaddr_pf(sa) == AF_INET &&
2651 fctx->res->dispatchv4 == NULL) ||
2652 (isc_sockaddr_pf(sa) == AF_INET6 &&
2653 fctx->res->dispatchv6 == NULL)) {
2654 sa = ISC_LIST_NEXT(sa, link);
2658 result = dns_adb_findaddrinfo(fctx->adb,
2659 sa, &ai, 0); /* XXXMLG */
2660 if (result == ISC_R_SUCCESS) {
2661 dns_adbaddrinfo_t *cur;
2662 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2663 cur = ISC_LIST_HEAD(fctx->forwaddrs);
2664 while (cur != NULL && cur->srtt < ai->srtt)
2665 cur = ISC_LIST_NEXT(cur, publink);
2667 ISC_LIST_INSERTBEFORE(fctx->forwaddrs, cur,
2670 ISC_LIST_APPEND(fctx->forwaddrs, ai, publink);
2672 sa = ISC_LIST_NEXT(sa, link);
2676 * If the forwarding policy is "only", we don't need the addresses
2677 * of the nameservers.
2679 if (fctx->fwdpolicy == dns_fwdpolicy_only)
2683 * Normal nameservers.
2686 stdoptions = DNS_ADBFIND_WANTEVENT | DNS_ADBFIND_EMPTYEVENT;
2687 if (fctx->restarts == 1) {
2689 * To avoid sending out a flood of queries likely to
2690 * result in NXRRSET, we suppress fetches for address
2691 * families we don't have the first time through,
2692 * provided that we have addresses in some family we
2695 * We don't want to set this option all the time, since
2696 * if fctx->restarts > 1, we've clearly been having trouble
2697 * with the addresses we had, so getting more could help.
2699 stdoptions |= DNS_ADBFIND_AVOIDFETCHES;
2701 if (res->dispatchv4 != NULL)
2702 stdoptions |= DNS_ADBFIND_INET;
2703 if (res->dispatchv6 != NULL)
2704 stdoptions |= DNS_ADBFIND_INET6;
2705 isc_stdtime_get(&now);
2707 INSIST(ISC_LIST_EMPTY(fctx->finds));
2708 INSIST(ISC_LIST_EMPTY(fctx->altfinds));
2710 for (result = dns_rdataset_first(&fctx->nameservers);
2711 result == ISC_R_SUCCESS;
2712 result = dns_rdataset_next(&fctx->nameservers))
2714 dns_rdataset_current(&fctx->nameservers, &rdata);
2716 * Extract the name from the NS record.
2718 result = dns_rdata_tostruct(&rdata, &ns, NULL);
2719 if (result != ISC_R_SUCCESS)
2722 findname(fctx, &ns.name, 0, stdoptions, 0, now,
2724 dns_rdata_reset(&rdata);
2725 dns_rdata_freestruct(&ns);
2727 if (result != ISC_R_NOMORE)
2731 * Do we need to use 6 to 4?
2733 if (need_alternate) {
2736 family = (res->dispatchv6 != NULL) ? AF_INET6 : AF_INET;
2737 for (a = ISC_LIST_HEAD(fctx->res->alternates);
2739 a = ISC_LIST_NEXT(a, link)) {
2740 if (!a->isaddress) {
2741 findname(fctx, &a->_u._n.name, a->_u._n.port,
2742 stdoptions, FCTX_ADDRINFO_FORWARDER,
2746 if (isc_sockaddr_pf(&a->_u.addr) != family)
2749 result = dns_adb_findaddrinfo(fctx->adb, &a->_u.addr,
2751 if (result == ISC_R_SUCCESS) {
2752 dns_adbaddrinfo_t *cur;
2753 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2754 cur = ISC_LIST_HEAD(fctx->altaddrs);
2755 while (cur != NULL && cur->srtt < ai->srtt)
2756 cur = ISC_LIST_NEXT(cur, publink);
2758 ISC_LIST_INSERTBEFORE(fctx->altaddrs,
2761 ISC_LIST_APPEND(fctx->altaddrs, ai,
2769 * Mark all known bad servers.
2771 all_bad = mark_bad(fctx);
2778 * We've got no addresses.
2780 if (fctx->pending > 0) {
2782 * We're fetching the addresses, but don't have any
2783 * yet. Tell the caller to wait for an answer.
2785 result = DNS_R_WAIT;
2790 * We've lost completely. We don't know any
2791 * addresses, and the ADB has told us it can't get
2794 FCTXTRACE("no addresses");
2795 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
2796 result = isc_time_nowplusinterval(&expire, &i);
2798 (fctx->type == dns_rdatatype_dnskey ||
2799 fctx->type == dns_rdatatype_dlv ||
2800 fctx->type == dns_rdatatype_ds) &&
2801 result == ISC_R_SUCCESS)
2802 dns_resolver_addbadcache(fctx->res,
2804 fctx->type, &expire);
2805 result = ISC_R_FAILURE;
2809 * We've found some addresses. We might still be looking
2810 * for more addresses.
2812 sort_finds(fctx, &fctx->finds);
2813 sort_finds(fctx, &fctx->altfinds);
2814 result = ISC_R_SUCCESS;
2821 possibly_mark(fetchctx_t *fctx, dns_adbaddrinfo_t *addr)
2824 char buf[ISC_NETADDR_FORMATSIZE];
2826 isc_boolean_t aborted = ISC_FALSE;
2827 isc_boolean_t bogus;
2828 dns_acl_t *blackhole;
2829 isc_netaddr_t ipaddr;
2830 dns_peer_t *peer = NULL;
2831 dns_resolver_t *res;
2832 const char *msg = NULL;
2834 sa = &addr->sockaddr;
2837 isc_netaddr_fromsockaddr(&ipaddr, sa);
2838 blackhole = dns_dispatchmgr_getblackhole(res->dispatchmgr);
2839 (void) dns_peerlist_peerbyaddr(res->view->peers, &ipaddr, &peer);
2841 if (blackhole != NULL) {
2844 if (dns_acl_match(&ipaddr, NULL, blackhole,
2846 &match, NULL) == ISC_R_SUCCESS &&
2852 dns_peer_getbogus(peer, &bogus) == ISC_R_SUCCESS &&
2857 addr->flags |= FCTX_ADDRINFO_MARK;
2858 msg = "ignoring blackholed / bogus server: ";
2859 } else if (isc_sockaddr_ismulticast(sa)) {
2860 addr->flags |= FCTX_ADDRINFO_MARK;
2861 msg = "ignoring multicast address: ";
2862 } else if (isc_sockaddr_isexperimental(sa)) {
2863 addr->flags |= FCTX_ADDRINFO_MARK;
2864 msg = "ignoring experimental address: ";
2865 } else if (sa->type.sa.sa_family != AF_INET6) {
2867 } else if (IN6_IS_ADDR_V4MAPPED(&sa->type.sin6.sin6_addr)) {
2868 addr->flags |= FCTX_ADDRINFO_MARK;
2869 msg = "ignoring IPv6 mapped IPV4 address: ";
2870 } else if (IN6_IS_ADDR_V4COMPAT(&sa->type.sin6.sin6_addr)) {
2871 addr->flags |= FCTX_ADDRINFO_MARK;
2872 msg = "ignoring IPv6 compatibility IPV4 address: ";
2876 if (!isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(3)))
2879 isc_netaddr_fromsockaddr(&na, sa);
2880 isc_netaddr_format(&na, buf, sizeof(buf));
2881 FCTXTRACE2(msg, buf);
2884 static inline dns_adbaddrinfo_t *
2885 fctx_nextaddress(fetchctx_t *fctx) {
2886 dns_adbfind_t *find, *start;
2887 dns_adbaddrinfo_t *addrinfo;
2888 dns_adbaddrinfo_t *faddrinfo;
2891 * Return the next untried address, if any.
2895 * Find the first unmarked forwarder (if any).
2897 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2899 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2900 if (!UNMARKED(addrinfo))
2902 possibly_mark(fctx, addrinfo);
2903 if (UNMARKED(addrinfo)) {
2904 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2911 * No forwarders. Move to the next find.
2914 fctx->attributes |= FCTX_ATTR_TRIEDFIND;
2918 find = ISC_LIST_HEAD(fctx->finds);
2920 find = ISC_LIST_NEXT(find, publink);
2922 find = ISC_LIST_HEAD(fctx->finds);
2926 * Find the first unmarked addrinfo.
2932 for (addrinfo = ISC_LIST_HEAD(find->list);
2934 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2935 if (!UNMARKED(addrinfo))
2937 possibly_mark(fctx, addrinfo);
2938 if (UNMARKED(addrinfo)) {
2939 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2943 if (addrinfo != NULL)
2945 find = ISC_LIST_NEXT(find, publink);
2947 find = ISC_LIST_HEAD(fctx->finds);
2948 } while (find != start);
2952 if (addrinfo != NULL)
2956 * No nameservers left. Try alternates.
2959 fctx->attributes |= FCTX_ATTR_TRIEDALT;
2961 find = fctx->altfind;
2963 find = ISC_LIST_HEAD(fctx->altfinds);
2965 find = ISC_LIST_NEXT(find, publink);
2967 find = ISC_LIST_HEAD(fctx->altfinds);
2971 * Find the first unmarked addrinfo.
2977 for (addrinfo = ISC_LIST_HEAD(find->list);
2979 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2980 if (!UNMARKED(addrinfo))
2982 possibly_mark(fctx, addrinfo);
2983 if (UNMARKED(addrinfo)) {
2984 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2988 if (addrinfo != NULL)
2990 find = ISC_LIST_NEXT(find, publink);
2992 find = ISC_LIST_HEAD(fctx->altfinds);
2993 } while (find != start);
2996 faddrinfo = addrinfo;
2999 * See if we have a better alternate server by address.
3002 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
3004 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
3005 if (!UNMARKED(addrinfo))
3007 possibly_mark(fctx, addrinfo);
3008 if (UNMARKED(addrinfo) &&
3009 (faddrinfo == NULL ||
3010 addrinfo->srtt < faddrinfo->srtt)) {
3011 if (faddrinfo != NULL)
3012 faddrinfo->flags &= ~FCTX_ADDRINFO_MARK;
3013 addrinfo->flags |= FCTX_ADDRINFO_MARK;
3018 if (addrinfo == NULL) {
3019 addrinfo = faddrinfo;
3020 fctx->altfind = find;
3027 fctx_try(fetchctx_t *fctx, isc_boolean_t retrying, isc_boolean_t badcache) {
3028 isc_result_t result;
3029 dns_adbaddrinfo_t *addrinfo;
3033 REQUIRE(!ADDRWAIT(fctx));
3035 addrinfo = fctx_nextaddress(fctx);
3036 if (addrinfo == NULL) {
3038 * We have no more addresses. Start over.
3040 fctx_cancelqueries(fctx, ISC_TRUE);
3041 fctx_cleanupfinds(fctx);
3042 fctx_cleanupaltfinds(fctx);
3043 fctx_cleanupforwaddrs(fctx);
3044 fctx_cleanupaltaddrs(fctx);
3045 result = fctx_getaddresses(fctx, badcache);
3046 if (result == DNS_R_WAIT) {
3048 * Sleep waiting for addresses.
3050 FCTXTRACE("addrwait");
3051 fctx->attributes |= FCTX_ATTR_ADDRWAIT;
3053 } else if (result != ISC_R_SUCCESS) {
3055 * Something bad happened.
3057 fctx_done(fctx, result, __LINE__);
3061 addrinfo = fctx_nextaddress(fctx);
3063 * While we may have addresses from the ADB, they
3064 * might be bad ones. In this case, return SERVFAIL.
3066 if (addrinfo == NULL) {
3067 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
3072 result = fctx_query(fctx, addrinfo, fctx->options);
3073 if (result != ISC_R_SUCCESS)
3074 fctx_done(fctx, result, __LINE__);
3076 inc_stats(fctx->res, dns_resstatscounter_retry);
3079 static isc_boolean_t
3080 fctx_destroy(fetchctx_t *fctx) {
3081 dns_resolver_t *res;
3082 unsigned int bucketnum;
3083 isc_sockaddr_t *sa, *next_sa;
3086 * Caller must be holding the bucket lock.
3089 REQUIRE(VALID_FCTX(fctx));
3090 REQUIRE(fctx->state == fetchstate_done ||
3091 fctx->state == fetchstate_init);
3092 REQUIRE(ISC_LIST_EMPTY(fctx->events));
3093 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
3094 REQUIRE(ISC_LIST_EMPTY(fctx->finds));
3095 REQUIRE(ISC_LIST_EMPTY(fctx->altfinds));
3096 REQUIRE(fctx->pending == 0);
3097 REQUIRE(fctx->references == 0);
3098 REQUIRE(ISC_LIST_EMPTY(fctx->validators));
3100 FCTXTRACE("destroy");
3103 bucketnum = fctx->bucketnum;
3105 ISC_LIST_UNLINK(res->buckets[bucketnum].fctxs, fctx, link);
3110 for (sa = ISC_LIST_HEAD(fctx->bad);
3113 next_sa = ISC_LIST_NEXT(sa, link);
3114 ISC_LIST_UNLINK(fctx->bad, sa, link);
3115 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3118 for (sa = ISC_LIST_HEAD(fctx->edns);
3121 next_sa = ISC_LIST_NEXT(sa, link);
3122 ISC_LIST_UNLINK(fctx->edns, sa, link);
3123 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3126 for (sa = ISC_LIST_HEAD(fctx->edns512);
3129 next_sa = ISC_LIST_NEXT(sa, link);
3130 ISC_LIST_UNLINK(fctx->edns512, sa, link);
3131 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3134 isc_timer_detach(&fctx->timer);
3135 dns_message_destroy(&fctx->rmessage);
3136 dns_message_destroy(&fctx->qmessage);
3137 if (dns_name_countlabels(&fctx->domain) > 0)
3138 dns_name_free(&fctx->domain, res->buckets[bucketnum].mctx);
3139 if (dns_rdataset_isassociated(&fctx->nameservers))
3140 dns_rdataset_disassociate(&fctx->nameservers);
3141 dns_name_free(&fctx->name, res->buckets[bucketnum].mctx);
3142 dns_db_detach(&fctx->cache);
3143 dns_adb_detach(&fctx->adb);
3144 isc_mem_free(res->buckets[bucketnum].mctx, fctx->info);
3145 isc_mem_put(res->buckets[bucketnum].mctx, fctx, sizeof(*fctx));
3149 UNLOCK(&res->nlock);
3151 if (res->buckets[bucketnum].exiting &&
3152 ISC_LIST_EMPTY(res->buckets[bucketnum].fctxs))
3159 * Fetch event handlers.
3163 fctx_timeout(isc_task_t *task, isc_event_t *event) {
3164 fetchctx_t *fctx = event->ev_arg;
3165 isc_timerevent_t *tevent = (isc_timerevent_t *)event;
3168 REQUIRE(VALID_FCTX(fctx));
3172 FCTXTRACE("timeout");
3174 inc_stats(fctx->res, dns_resstatscounter_querytimeout);
3176 if (event->ev_type == ISC_TIMEREVENT_LIFE) {
3177 fctx->reason = NULL;
3178 fctx_done(fctx, ISC_R_TIMEDOUT, __LINE__);
3180 isc_result_t result;
3183 fctx->timeout = ISC_TRUE;
3185 * We could cancel the running queries here, or we could let
3186 * them keep going. Since we normally use separate sockets for
3187 * different queries, we adopt the former approach to reduce
3188 * the number of open sockets: cancel the oldest query if it
3189 * expired after the query had started (this is usually the
3190 * case but is not always so, depending on the task schedule
3193 query = ISC_LIST_HEAD(fctx->queries);
3194 if (query != NULL &&
3195 isc_time_compare(&tevent->due, &query->start) >= 0) {
3196 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
3198 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3200 * Our timer has triggered. Reestablish the fctx lifetime
3203 result = fctx_starttimer(fctx);
3204 if (result != ISC_R_SUCCESS)
3205 fctx_done(fctx, result, __LINE__);
3210 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
3213 isc_event_free(&event);
3217 fctx_shutdown(fetchctx_t *fctx) {
3218 isc_event_t *cevent;
3221 * Start the shutdown process for fctx, if it isn't already underway.
3224 FCTXTRACE("shutdown");
3227 * The caller must be holding the appropriate bucket lock.
3230 if (fctx->want_shutdown)
3233 fctx->want_shutdown = ISC_TRUE;
3236 * Unless we're still initializing (in which case the
3237 * control event is still outstanding), we need to post
3238 * the control event to tell the fetch we want it to
3241 if (fctx->state != fetchstate_init) {
3242 cevent = &fctx->control_event;
3243 isc_task_send(fctx->res->buckets[fctx->bucketnum].task,
3249 fctx_doshutdown(isc_task_t *task, isc_event_t *event) {
3250 fetchctx_t *fctx = event->ev_arg;
3251 isc_boolean_t bucket_empty = ISC_FALSE;
3252 dns_resolver_t *res;
3253 unsigned int bucketnum;
3254 dns_validator_t *validator;
3256 REQUIRE(VALID_FCTX(fctx));
3261 bucketnum = fctx->bucketnum;
3263 FCTXTRACE("doshutdown");
3266 * An fctx that is shutting down is no longer in ADDRWAIT mode.
3268 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3271 * Cancel all pending validators. Note that this must be done
3272 * without the bucket lock held, since that could cause deadlock.
3274 validator = ISC_LIST_HEAD(fctx->validators);
3275 while (validator != NULL) {
3276 dns_validator_cancel(validator);
3277 validator = ISC_LIST_NEXT(validator, link);
3280 if (fctx->nsfetch != NULL)
3281 dns_resolver_cancelfetch(fctx->nsfetch);
3284 * Shut down anything that is still running on behalf of this
3285 * fetch. To avoid deadlock with the ADB, we must do this
3286 * before we lock the bucket lock.
3288 fctx_stopeverything(fctx, ISC_FALSE);
3290 LOCK(&res->buckets[bucketnum].lock);
3292 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3294 INSIST(fctx->state == fetchstate_active ||
3295 fctx->state == fetchstate_done);
3296 INSIST(fctx->want_shutdown);
3298 if (fctx->state != fetchstate_done) {
3299 fctx->state = fetchstate_done;
3300 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3303 if (fctx->references == 0 && fctx->pending == 0 &&
3304 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators))
3305 bucket_empty = fctx_destroy(fctx);
3307 UNLOCK(&res->buckets[bucketnum].lock);
3314 fctx_start(isc_task_t *task, isc_event_t *event) {
3315 fetchctx_t *fctx = event->ev_arg;
3316 isc_boolean_t done = ISC_FALSE, bucket_empty = ISC_FALSE;
3317 dns_resolver_t *res;
3318 unsigned int bucketnum;
3320 REQUIRE(VALID_FCTX(fctx));
3325 bucketnum = fctx->bucketnum;
3329 LOCK(&res->buckets[bucketnum].lock);
3331 INSIST(fctx->state == fetchstate_init);
3332 if (fctx->want_shutdown) {
3334 * We haven't started this fctx yet, and we've been requested
3337 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3338 fctx->state = fetchstate_done;
3339 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3341 * Since we haven't started, we INSIST that we have no
3342 * pending ADB finds and no pending validations.
3344 INSIST(fctx->pending == 0);
3345 INSIST(fctx->nqueries == 0);
3346 INSIST(ISC_LIST_EMPTY(fctx->validators));
3347 if (fctx->references == 0) {
3349 * It's now safe to destroy this fctx.
3351 bucket_empty = fctx_destroy(fctx);
3356 * Normal fctx startup.
3358 fctx->state = fetchstate_active;
3360 * Reset the control event for later use in shutting down
3363 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
3364 DNS_EVENT_FETCHCONTROL, fctx_doshutdown, fctx,
3368 UNLOCK(&res->buckets[bucketnum].lock);
3371 isc_result_t result;
3374 * All is well. Start working on the fetch.
3376 result = fctx_starttimer(fctx);
3377 if (result != ISC_R_SUCCESS)
3378 fctx_done(fctx, result, __LINE__);
3380 fctx_try(fctx, ISC_FALSE, ISC_FALSE);
3381 } else if (bucket_empty)
3386 * Fetch Creation, Joining, and Cancelation.
3389 static inline isc_result_t
3390 fctx_join(fetchctx_t *fctx, isc_task_t *task, isc_sockaddr_t *client,
3391 dns_messageid_t id, isc_taskaction_t action, void *arg,
3392 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset,
3396 dns_fetchevent_t *event;
3401 * We store the task we're going to send this event to in the
3402 * sender field. We'll make the fetch the sender when we actually
3406 isc_task_attach(task, &clone);
3407 event = (dns_fetchevent_t *)
3408 isc_event_allocate(fctx->res->mctx, clone, DNS_EVENT_FETCHDONE,
3409 action, arg, sizeof(*event));
3410 if (event == NULL) {
3411 isc_task_detach(&clone);
3412 return (ISC_R_NOMEMORY);
3414 event->result = DNS_R_SERVFAIL;
3415 event->qtype = fctx->type;
3418 event->rdataset = rdataset;
3419 event->sigrdataset = sigrdataset;
3420 event->fetch = fetch;
3421 event->client = client;
3423 dns_fixedname_init(&event->foundname);
3426 * Make sure that we can store the sigrdataset in the
3427 * first event if it is needed by any of the events.
3429 if (event->sigrdataset != NULL)
3430 ISC_LIST_PREPEND(fctx->events, event, ev_link);
3432 ISC_LIST_APPEND(fctx->events, event, ev_link);
3435 fetch->magic = DNS_FETCH_MAGIC;
3436 fetch->private = fctx;
3438 return (ISC_R_SUCCESS);
3442 fctx_create(dns_resolver_t *res, dns_name_t *name, dns_rdatatype_t type,
3443 dns_name_t *domain, dns_rdataset_t *nameservers,
3444 unsigned int options, unsigned int bucketnum, fetchctx_t **fctxp)
3447 isc_result_t result;
3448 isc_result_t iresult;
3449 isc_interval_t interval;
3450 dns_fixedname_t fixed;
3451 unsigned int findoptions = 0;
3452 char buf[DNS_NAME_FORMATSIZE + DNS_RDATATYPE_FORMATSIZE];
3453 char typebuf[DNS_RDATATYPE_FORMATSIZE];
3457 * Caller must be holding the lock for bucket number 'bucketnum'.
3459 REQUIRE(fctxp != NULL && *fctxp == NULL);
3461 fctx = isc_mem_get(res->buckets[bucketnum].mctx, sizeof(*fctx));
3463 return (ISC_R_NOMEMORY);
3464 dns_name_format(name, buf, sizeof(buf));
3465 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
3466 strcat(buf, "/"); /* checked */
3467 strcat(buf, typebuf); /* checked */
3468 fctx->info = isc_mem_strdup(res->buckets[bucketnum].mctx, buf);
3469 if (fctx->info == NULL) {
3470 result = ISC_R_NOMEMORY;
3473 FCTXTRACE("create");
3474 dns_name_init(&fctx->name, NULL);
3475 result = dns_name_dup(name, res->buckets[bucketnum].mctx, &fctx->name);
3476 if (result != ISC_R_SUCCESS)
3478 dns_name_init(&fctx->domain, NULL);
3479 dns_rdataset_init(&fctx->nameservers);
3482 fctx->options = options;
3484 * Note! We do not attach to the task. We are relying on the
3485 * resolver to ensure that this task doesn't go away while we are
3489 fctx->references = 0;
3490 fctx->bucketnum = bucketnum;
3491 fctx->state = fetchstate_init;
3492 fctx->want_shutdown = ISC_FALSE;
3493 fctx->cloned = ISC_FALSE;
3494 ISC_LIST_INIT(fctx->queries);
3495 ISC_LIST_INIT(fctx->finds);
3496 ISC_LIST_INIT(fctx->altfinds);
3497 ISC_LIST_INIT(fctx->forwaddrs);
3498 ISC_LIST_INIT(fctx->altaddrs);
3499 ISC_LIST_INIT(fctx->forwarders);
3500 fctx->fwdpolicy = dns_fwdpolicy_none;
3501 ISC_LIST_INIT(fctx->bad);
3502 ISC_LIST_INIT(fctx->edns);
3503 ISC_LIST_INIT(fctx->edns512);
3504 ISC_LIST_INIT(fctx->validators);
3505 fctx->validator = NULL;
3507 fctx->altfind = NULL;
3510 fctx->querysent = 0;
3511 fctx->referrals = 0;
3512 TIME_NOW(&fctx->start);
3514 fctx->lamecount = 0;
3520 fctx->result = ISC_R_FAILURE;
3521 fctx->vresult = ISC_R_SUCCESS;
3522 fctx->exitline = -1; /* sentinel */
3523 fctx->logged = ISC_FALSE;
3524 fctx->attributes = 0;
3525 fctx->spilled = ISC_FALSE;
3527 fctx->reason = NULL;
3529 fctx->rand_bits = 0;
3530 fctx->timeout = ISC_FALSE;
3532 dns_name_init(&fctx->nsname, NULL);
3533 fctx->nsfetch = NULL;
3534 dns_rdataset_init(&fctx->nsrrset);
3536 if (domain == NULL) {
3537 dns_forwarders_t *forwarders = NULL;
3538 unsigned int labels;
3539 dns_name_t *fwdname = name;
3542 * DS records are found in the parent server.
3543 * Strip label to get the correct forwarder (if any).
3545 if (dns_rdatatype_atparent(fctx->type) &&
3546 dns_name_countlabels(name) > 1) {
3547 dns_name_init(&suffix, NULL);
3548 labels = dns_name_countlabels(name);
3549 dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
3552 dns_fixedname_init(&fixed);
3553 domain = dns_fixedname_name(&fixed);
3554 result = dns_fwdtable_find2(fctx->res->view->fwdtable, fwdname,
3555 domain, &forwarders);
3556 if (result == ISC_R_SUCCESS)
3557 fctx->fwdpolicy = forwarders->fwdpolicy;
3559 if (fctx->fwdpolicy != dns_fwdpolicy_only) {
3561 * The caller didn't supply a query domain and
3562 * nameservers, and we're not in forward-only mode,
3563 * so find the best nameservers to use.
3565 if (dns_rdatatype_atparent(fctx->type))
3566 findoptions |= DNS_DBFIND_NOEXACT;
3567 result = dns_view_findzonecut(res->view, name, domain,
3568 0, findoptions, ISC_TRUE,
3571 if (result != ISC_R_SUCCESS)
3573 result = dns_name_dup(domain,
3574 res->buckets[bucketnum].mctx,
3576 if (result != ISC_R_SUCCESS) {
3577 dns_rdataset_disassociate(&fctx->nameservers);
3582 * We're in forward-only mode. Set the query domain.
3584 result = dns_name_dup(domain,
3585 res->buckets[bucketnum].mctx,
3587 if (result != ISC_R_SUCCESS)
3591 result = dns_name_dup(domain,
3592 res->buckets[bucketnum].mctx,
3594 if (result != ISC_R_SUCCESS)
3596 dns_rdataset_clone(nameservers, &fctx->nameservers);
3599 INSIST(dns_name_issubdomain(&fctx->name, &fctx->domain));
3601 fctx->qmessage = NULL;
3602 result = dns_message_create(res->buckets[bucketnum].mctx,
3603 DNS_MESSAGE_INTENTRENDER,
3606 if (result != ISC_R_SUCCESS)
3607 goto cleanup_domain;
3609 fctx->rmessage = NULL;
3610 result = dns_message_create(res->buckets[bucketnum].mctx,
3611 DNS_MESSAGE_INTENTPARSE,
3614 if (result != ISC_R_SUCCESS)
3615 goto cleanup_qmessage;
3618 * Compute an expiration time for the entire fetch.
3620 isc_interval_set(&interval, 30, 0); /* XXXRTH constant */
3621 iresult = isc_time_nowplusinterval(&fctx->expires, &interval);
3622 if (iresult != ISC_R_SUCCESS) {
3623 UNEXPECTED_ERROR(__FILE__, __LINE__,
3624 "isc_time_nowplusinterval: %s",
3625 isc_result_totext(iresult));
3626 result = ISC_R_UNEXPECTED;
3627 goto cleanup_rmessage;
3631 * Default retry interval initialization. We set the interval now
3632 * mostly so it won't be uninitialized. It will be set to the
3633 * correct value before a query is issued.
3635 isc_interval_set(&fctx->interval, 2, 0);
3638 * Create an inactive timer. It will be made active when the fetch
3639 * is actually started.
3642 iresult = isc_timer_create(res->timermgr, isc_timertype_inactive,
3644 res->buckets[bucketnum].task, fctx_timeout,
3645 fctx, &fctx->timer);
3646 if (iresult != ISC_R_SUCCESS) {
3647 UNEXPECTED_ERROR(__FILE__, __LINE__,
3648 "isc_timer_create: %s",
3649 isc_result_totext(iresult));
3650 result = ISC_R_UNEXPECTED;
3651 goto cleanup_rmessage;
3655 * Attach to the view's cache and adb.
3658 dns_db_attach(res->view->cachedb, &fctx->cache);
3660 dns_adb_attach(res->view->adb, &fctx->adb);
3662 ISC_LIST_INIT(fctx->events);
3663 ISC_LINK_INIT(fctx, link);
3664 fctx->magic = FCTX_MAGIC;
3666 ISC_LIST_APPEND(res->buckets[bucketnum].fctxs, fctx, link);
3670 UNLOCK(&res->nlock);
3674 return (ISC_R_SUCCESS);
3677 dns_message_destroy(&fctx->rmessage);
3680 dns_message_destroy(&fctx->qmessage);
3683 if (dns_name_countlabels(&fctx->domain) > 0)
3684 dns_name_free(&fctx->domain, res->buckets[bucketnum].mctx);
3685 if (dns_rdataset_isassociated(&fctx->nameservers))
3686 dns_rdataset_disassociate(&fctx->nameservers);
3689 dns_name_free(&fctx->name, res->buckets[bucketnum].mctx);
3692 isc_mem_free(res->buckets[bucketnum].mctx, fctx->info);
3695 isc_mem_put(res->buckets[bucketnum].mctx, fctx, sizeof(*fctx));
3703 static inline isc_boolean_t
3704 is_lame(fetchctx_t *fctx) {
3705 dns_message_t *message = fctx->rmessage;
3707 dns_rdataset_t *rdataset;
3708 isc_result_t result;
3710 if (message->rcode != dns_rcode_noerror &&
3711 message->rcode != dns_rcode_nxdomain)
3714 if (message->counts[DNS_SECTION_ANSWER] != 0)
3717 if (message->counts[DNS_SECTION_AUTHORITY] == 0)
3720 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
3721 while (result == ISC_R_SUCCESS) {
3723 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
3724 for (rdataset = ISC_LIST_HEAD(name->list);
3726 rdataset = ISC_LIST_NEXT(rdataset, link)) {
3727 dns_namereln_t namereln;
3729 unsigned int labels;
3730 if (rdataset->type != dns_rdatatype_ns)
3732 namereln = dns_name_fullcompare(name, &fctx->domain,
3734 if (namereln == dns_namereln_equal &&
3735 (message->flags & DNS_MESSAGEFLAG_AA) != 0)
3737 if (namereln == dns_namereln_subdomain)
3741 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
3748 log_lame(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo) {
3749 char namebuf[DNS_NAME_FORMATSIZE];
3750 char domainbuf[DNS_NAME_FORMATSIZE];
3751 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
3753 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
3754 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
3755 isc_sockaddr_format(&addrinfo->sockaddr, addrbuf, sizeof(addrbuf));
3756 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
3757 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
3758 "lame server resolving '%s' (in '%s'?): %s",
3759 namebuf, domainbuf, addrbuf);
3762 static inline isc_result_t
3763 same_question(fetchctx_t *fctx) {
3764 isc_result_t result;
3765 dns_message_t *message = fctx->rmessage;
3767 dns_rdataset_t *rdataset;
3770 * Caller must be holding the fctx lock.
3774 * XXXRTH Currently we support only one question.
3776 if (message->counts[DNS_SECTION_QUESTION] != 1)
3777 return (DNS_R_FORMERR);
3779 result = dns_message_firstname(message, DNS_SECTION_QUESTION);
3780 if (result != ISC_R_SUCCESS)
3783 dns_message_currentname(message, DNS_SECTION_QUESTION, &name);
3784 rdataset = ISC_LIST_HEAD(name->list);
3785 INSIST(rdataset != NULL);
3786 INSIST(ISC_LIST_NEXT(rdataset, link) == NULL);
3787 if (fctx->type != rdataset->type ||
3788 fctx->res->rdclass != rdataset->rdclass ||
3789 !dns_name_equal(&fctx->name, name))
3790 return (DNS_R_FORMERR);
3792 return (ISC_R_SUCCESS);
3796 clone_results(fetchctx_t *fctx) {
3797 dns_fetchevent_t *event, *hevent;
3798 isc_result_t result;
3799 dns_name_t *name, *hname;
3801 FCTXTRACE("clone_results");
3804 * Set up any other events to have the same data as the first
3807 * Caller must be holding the appropriate lock.
3810 fctx->cloned = ISC_TRUE;
3811 hevent = ISC_LIST_HEAD(fctx->events);
3814 hname = dns_fixedname_name(&hevent->foundname);
3815 for (event = ISC_LIST_NEXT(hevent, ev_link);
3817 event = ISC_LIST_NEXT(event, ev_link)) {
3818 name = dns_fixedname_name(&event->foundname);
3819 result = dns_name_copy(hname, name, NULL);
3820 if (result != ISC_R_SUCCESS)
3821 event->result = result;
3823 event->result = hevent->result;
3824 dns_db_attach(hevent->db, &event->db);
3825 dns_db_attachnode(hevent->db, hevent->node, &event->node);
3826 INSIST(hevent->rdataset != NULL);
3827 INSIST(event->rdataset != NULL);
3828 if (dns_rdataset_isassociated(hevent->rdataset))
3829 dns_rdataset_clone(hevent->rdataset, event->rdataset);
3830 INSIST(! (hevent->sigrdataset == NULL &&
3831 event->sigrdataset != NULL));
3832 if (hevent->sigrdataset != NULL &&
3833 dns_rdataset_isassociated(hevent->sigrdataset) &&
3834 event->sigrdataset != NULL)
3835 dns_rdataset_clone(hevent->sigrdataset,
3836 event->sigrdataset);
3840 #define CACHE(r) (((r)->attributes & DNS_RDATASETATTR_CACHE) != 0)
3841 #define ANSWER(r) (((r)->attributes & DNS_RDATASETATTR_ANSWER) != 0)
3842 #define ANSWERSIG(r) (((r)->attributes & DNS_RDATASETATTR_ANSWERSIG) != 0)
3843 #define EXTERNAL(r) (((r)->attributes & DNS_RDATASETATTR_EXTERNAL) != 0)
3844 #define CHAINING(r) (((r)->attributes & DNS_RDATASETATTR_CHAINING) != 0)
3845 #define CHASE(r) (((r)->attributes & DNS_RDATASETATTR_CHASE) != 0)
3846 #define CHECKNAMES(r) (((r)->attributes & DNS_RDATASETATTR_CHECKNAMES) != 0)
3850 * Destroy '*fctx' if it is ready to be destroyed (i.e., if it has
3851 * no references and is no longer waiting for any events). If this
3852 * was the last fctx in the resolver, destroy the resolver.
3855 * '*fctx' is shutting down.
3858 maybe_destroy(fetchctx_t *fctx) {
3859 unsigned int bucketnum;
3860 isc_boolean_t bucket_empty = ISC_FALSE;
3861 dns_resolver_t *res = fctx->res;
3862 dns_validator_t *validator, *next_validator;
3864 REQUIRE(SHUTTINGDOWN(fctx));
3866 if (fctx->pending != 0 || fctx->nqueries != 0)
3869 for (validator = ISC_LIST_HEAD(fctx->validators);
3870 validator != NULL; validator = next_validator) {
3871 next_validator = ISC_LIST_NEXT(validator, link);
3872 dns_validator_cancel(validator);
3874 * If this is a active validator wait for the cancel
3875 * to complete before calling dns_validator_destroy().
3877 if (validator == fctx->validator)
3879 ISC_LIST_UNLINK(fctx->validators, validator, link);
3880 dns_validator_destroy(&validator);
3883 bucketnum = fctx->bucketnum;
3884 LOCK(&res->buckets[bucketnum].lock);
3885 if (fctx->references == 0 && ISC_LIST_EMPTY(fctx->validators))
3886 bucket_empty = fctx_destroy(fctx);
3887 UNLOCK(&res->buckets[bucketnum].lock);
3894 * The validator has finished.
3897 validated(isc_task_t *task, isc_event_t *event) {
3898 isc_result_t result = ISC_R_SUCCESS;
3899 isc_result_t eresult = ISC_R_SUCCESS;
3902 dns_validatorevent_t *vevent;
3903 dns_fetchevent_t *hevent;
3904 dns_rdataset_t *ardataset = NULL;
3905 dns_rdataset_t *asigrdataset = NULL;
3906 dns_dbnode_t *node = NULL;
3907 isc_boolean_t negative;
3908 isc_boolean_t chaining;
3909 isc_boolean_t sentresponse;
3911 dns_dbnode_t *nsnode = NULL;
3913 dns_rdataset_t *rdataset;
3914 dns_rdataset_t *sigrdataset;
3915 dns_valarg_t *valarg;
3916 dns_adbaddrinfo_t *addrinfo;
3918 UNUSED(task); /* for now */
3920 REQUIRE(event->ev_type == DNS_EVENT_VALIDATORDONE);
3921 valarg = event->ev_arg;
3922 fctx = valarg->fctx;
3923 addrinfo = valarg->addrinfo;
3924 REQUIRE(VALID_FCTX(fctx));
3925 REQUIRE(!ISC_LIST_EMPTY(fctx->validators));
3927 vevent = (dns_validatorevent_t *)event;
3929 FCTXTRACE("received validation completion event");
3931 ISC_LIST_UNLINK(fctx->validators, vevent->validator, link);
3932 fctx->validator = NULL;
3935 * Destroy the validator early so that we can
3936 * destroy the fctx if necessary.
3938 dns_validator_destroy(&vevent->validator);
3939 isc_mem_put(fctx->res->buckets[fctx->bucketnum].mctx,
3940 valarg, sizeof(*valarg));
3942 negative = ISC_TF(vevent->rdataset == NULL);
3944 sentresponse = ISC_TF((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0);
3947 * If shutting down, ignore the results. Check to see if we're
3948 * done waiting for validator completions and ADB pending events; if
3949 * so, destroy the fctx.
3951 if (SHUTTINGDOWN(fctx) && !sentresponse) {
3952 maybe_destroy(fctx); /* Locks bucket. */
3956 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
3958 isc_stdtime_get(&now);
3961 * If chaining, we need to make sure that the right result code is
3962 * returned, and that the rdatasets are bound.
3964 if (vevent->result == ISC_R_SUCCESS &&
3966 vevent->rdataset != NULL &&
3967 CHAINING(vevent->rdataset))
3969 if (vevent->rdataset->type == dns_rdatatype_cname)
3970 eresult = DNS_R_CNAME;
3972 INSIST(vevent->rdataset->type == dns_rdatatype_dname);
3973 eresult = DNS_R_DNAME;
3975 chaining = ISC_TRUE;
3977 chaining = ISC_FALSE;
3980 * Either we're not shutting down, or we are shutting down but want
3981 * to cache the result anyway (if this was a validation started by
3982 * a query with cd set)
3985 hevent = ISC_LIST_HEAD(fctx->events);
3986 if (hevent != NULL) {
3987 if (!negative && !chaining &&
3988 (fctx->type == dns_rdatatype_any ||
3989 fctx->type == dns_rdatatype_rrsig ||
3990 fctx->type == dns_rdatatype_sig)) {
3992 * Don't bind rdatasets; the caller
3993 * will iterate the node.
3996 ardataset = hevent->rdataset;
3997 asigrdataset = hevent->sigrdataset;
4001 if (vevent->result != ISC_R_SUCCESS) {
4002 FCTXTRACE("validation failed");
4003 inc_stats(fctx->res, dns_resstatscounter_valfail);
4005 fctx->vresult = vevent->result;
4006 if (fctx->vresult != DNS_R_BROKENCHAIN) {
4007 result = ISC_R_NOTFOUND;
4008 if (vevent->rdataset != NULL)
4009 result = dns_db_findnode(fctx->cache,
4012 if (result == ISC_R_SUCCESS)
4013 (void)dns_db_deleterdataset(fctx->cache, node,
4016 if (result == ISC_R_SUCCESS &&
4017 vevent->sigrdataset != NULL)
4018 (void)dns_db_deleterdataset(fctx->cache, node,
4020 dns_rdatatype_rrsig,
4022 if (result == ISC_R_SUCCESS)
4023 dns_db_detachnode(fctx->cache, &node);
4025 if (fctx->vresult == DNS_R_BROKENCHAIN && !negative) {
4027 * Cache the data as pending for later validation.
4029 result = ISC_R_NOTFOUND;
4030 if (vevent->rdataset != NULL)
4031 result = dns_db_findnode(fctx->cache,
4034 if (result == ISC_R_SUCCESS) {
4035 (void)dns_db_addrdataset(fctx->cache, node,
4037 vevent->rdataset, 0,
4040 if (result == ISC_R_SUCCESS &&
4041 vevent->sigrdataset != NULL)
4042 (void)dns_db_addrdataset(fctx->cache, node,
4044 vevent->sigrdataset,
4046 if (result == ISC_R_SUCCESS)
4047 dns_db_detachnode(fctx->cache, &node);
4049 result = fctx->vresult;
4050 add_bad(fctx, addrinfo, result, badns_validation);
4051 isc_event_free(&event);
4052 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4053 INSIST(fctx->validator == NULL);
4054 fctx->validator = ISC_LIST_HEAD(fctx->validators);
4055 if (fctx->validator != NULL)
4056 dns_validator_send(fctx->validator);
4057 else if (sentresponse)
4058 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4059 else if (result == DNS_R_BROKENCHAIN) {
4060 isc_result_t tresult;
4064 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
4065 tresult = isc_time_nowplusinterval(&expire, &i);
4067 (fctx->type == dns_rdatatype_dnskey ||
4068 fctx->type == dns_rdatatype_dlv ||
4069 fctx->type == dns_rdatatype_ds) &&
4070 tresult == ISC_R_SUCCESS)
4071 dns_resolver_addbadcache(fctx->res,
4073 fctx->type, &expire);
4074 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4076 fctx_try(fctx, ISC_TRUE, ISC_TRUE); /* Locks bucket. */
4082 dns_rdatatype_t covers;
4083 FCTXTRACE("nonexistence validation OK");
4085 inc_stats(fctx->res, dns_resstatscounter_valnegsuccess);
4087 if (fctx->rmessage->rcode == dns_rcode_nxdomain)
4088 covers = dns_rdatatype_any;
4090 covers = fctx->type;
4092 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE,
4094 if (result != ISC_R_SUCCESS)
4095 goto noanswer_response;
4098 * If we are asking for a SOA record set the cache time
4099 * to zero to facilitate locating the containing zone of
4102 ttl = fctx->res->view->maxncachettl;
4103 if (fctx->type == dns_rdatatype_soa &&
4104 covers == dns_rdatatype_any &&
4105 fctx->res->zero_no_soa_ttl)
4108 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
4109 covers, now, ttl, vevent->optout,
4110 ardataset, &eresult);
4111 if (result != ISC_R_SUCCESS)
4112 goto noanswer_response;
4113 goto answer_response;
4115 inc_stats(fctx->res, dns_resstatscounter_valsuccess);
4117 FCTXTRACE("validation OK");
4119 if (vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF] != NULL) {
4121 result = dns_rdataset_addnoqname(vevent->rdataset,
4122 vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF]);
4123 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4124 INSIST(vevent->sigrdataset != NULL);
4125 vevent->sigrdataset->ttl = vevent->rdataset->ttl;
4126 if (vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER] != NULL) {
4127 result = dns_rdataset_addclosest(vevent->rdataset,
4128 vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER]);
4129 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4134 * The data was already cached as pending data.
4135 * Re-cache it as secure and bind the cached
4136 * rdatasets to the first event on the fetch
4139 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE, &node);
4140 if (result != ISC_R_SUCCESS)
4141 goto noanswer_response;
4143 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4144 vevent->rdataset, 0, ardataset);
4145 if (result != ISC_R_SUCCESS &&
4146 result != DNS_R_UNCHANGED)
4147 goto noanswer_response;
4148 if (ardataset != NULL && ardataset->type == 0) {
4149 if (NXDOMAIN(ardataset))
4150 eresult = DNS_R_NCACHENXDOMAIN;
4152 eresult = DNS_R_NCACHENXRRSET;
4153 } else if (vevent->sigrdataset != NULL) {
4154 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4155 vevent->sigrdataset, 0,
4157 if (result != ISC_R_SUCCESS &&
4158 result != DNS_R_UNCHANGED)
4159 goto noanswer_response;
4164 * If we only deferred the destroy because we wanted to cache
4165 * the data, destroy now.
4167 dns_db_detachnode(fctx->cache, &node);
4168 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4169 if (SHUTTINGDOWN(fctx))
4170 maybe_destroy(fctx); /* Locks bucket. */
4174 if (!ISC_LIST_EMPTY(fctx->validators)) {
4176 INSIST(fctx->type == dns_rdatatype_any ||
4177 fctx->type == dns_rdatatype_rrsig ||
4178 fctx->type == dns_rdatatype_sig);
4180 * Don't send a response yet - we have
4181 * more rdatasets that still need to
4184 dns_db_detachnode(fctx->cache, &node);
4185 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4186 dns_validator_send(ISC_LIST_HEAD(fctx->validators));
4192 * Cache any NS/NSEC records that happened to be validated.
4194 result = dns_message_firstname(fctx->rmessage, DNS_SECTION_AUTHORITY);
4195 while (result == ISC_R_SUCCESS) {
4197 dns_message_currentname(fctx->rmessage, DNS_SECTION_AUTHORITY,
4199 for (rdataset = ISC_LIST_HEAD(name->list);
4201 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4202 if ((rdataset->type != dns_rdatatype_ns &&
4203 rdataset->type != dns_rdatatype_nsec) ||
4204 rdataset->trust != dns_trust_secure)
4206 for (sigrdataset = ISC_LIST_HEAD(name->list);
4207 sigrdataset != NULL;
4208 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4209 if (sigrdataset->type != dns_rdatatype_rrsig ||
4210 sigrdataset->covers != rdataset->type)
4214 if (sigrdataset == NULL ||
4215 sigrdataset->trust != dns_trust_secure)
4217 result = dns_db_findnode(fctx->cache, name, ISC_TRUE,
4219 if (result != ISC_R_SUCCESS)
4222 result = dns_db_addrdataset(fctx->cache, nsnode, NULL,
4223 now, rdataset, 0, NULL);
4224 if (result == ISC_R_SUCCESS)
4225 result = dns_db_addrdataset(fctx->cache, nsnode,
4229 dns_db_detachnode(fctx->cache, &nsnode);
4231 result = dns_message_nextname(fctx->rmessage,
4232 DNS_SECTION_AUTHORITY);
4235 result = ISC_R_SUCCESS;
4238 * Respond with an answer, positive or negative,
4239 * as opposed to an error. 'node' must be non-NULL.
4242 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4244 if (hevent != NULL) {
4245 hevent->result = eresult;
4246 RUNTIME_CHECK(dns_name_copy(vevent->name,
4247 dns_fixedname_name(&hevent->foundname), NULL)
4249 dns_db_attach(fctx->cache, &hevent->db);
4250 dns_db_transfernode(fctx->cache, &node, &hevent->node);
4251 clone_results(fctx);
4256 dns_db_detachnode(fctx->cache, &node);
4258 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4260 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4263 INSIST(node == NULL);
4264 isc_event_free(&event);
4267 static inline isc_result_t
4268 cache_name(fetchctx_t *fctx, dns_name_t *name, dns_adbaddrinfo_t *addrinfo,
4271 dns_rdataset_t *rdataset, *sigrdataset;
4272 dns_rdataset_t *addedrdataset, *ardataset, *asigrdataset;
4273 dns_rdataset_t *valrdataset = NULL, *valsigrdataset = NULL;
4274 dns_dbnode_t *node, **anodep;
4277 dns_resolver_t *res;
4278 isc_boolean_t need_validation, secure_domain, have_answer;
4279 isc_result_t result, eresult;
4280 dns_fetchevent_t *event;
4281 unsigned int options;
4284 unsigned int valoptions = 0;
4287 * The appropriate bucket lock must be held.
4291 need_validation = ISC_FALSE;
4292 secure_domain = ISC_FALSE;
4293 have_answer = ISC_FALSE;
4294 eresult = ISC_R_SUCCESS;
4295 task = res->buckets[fctx->bucketnum].task;
4298 * Is DNSSEC validation required for this name?
4300 if (res->view->enablevalidation) {
4301 result = dns_keytable_issecuredomain(res->view->secroots, name,
4303 if (result != ISC_R_SUCCESS)
4306 if (!secure_domain && res->view->dlv != NULL) {
4307 valoptions = DNS_VALIDATOR_DLV;
4308 secure_domain = ISC_TRUE;
4312 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
4313 need_validation = ISC_FALSE;
4315 need_validation = secure_domain;
4321 asigrdataset = NULL;
4323 if ((name->attributes & DNS_NAMEATTR_ANSWER) != 0 &&
4325 have_answer = ISC_TRUE;
4326 event = ISC_LIST_HEAD(fctx->events);
4327 if (event != NULL) {
4329 aname = dns_fixedname_name(&event->foundname);
4330 result = dns_name_copy(name, aname, NULL);
4331 if (result != ISC_R_SUCCESS)
4333 anodep = &event->node;
4335 * If this is an ANY, SIG or RRSIG query, we're not
4336 * going to return any rdatasets, unless we encountered
4337 * a CNAME or DNAME as "the answer". In this case,
4338 * we're going to return DNS_R_CNAME or DNS_R_DNAME
4339 * and we must set up the rdatasets.
4341 if ((fctx->type != dns_rdatatype_any &&
4342 fctx->type != dns_rdatatype_rrsig &&
4343 fctx->type != dns_rdatatype_sig) ||
4344 (name->attributes & DNS_NAMEATTR_CHAINING) != 0) {
4345 ardataset = event->rdataset;
4346 asigrdataset = event->sigrdataset;
4352 * Find or create the cache node.
4355 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
4356 if (result != ISC_R_SUCCESS)
4360 * Cache or validate each cacheable rdataset.
4362 fail = ISC_TF((fctx->res->options & DNS_RESOLVER_CHECKNAMESFAIL) != 0);
4363 for (rdataset = ISC_LIST_HEAD(name->list);
4365 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4366 if (!CACHE(rdataset))
4368 if (CHECKNAMES(rdataset)) {
4369 char namebuf[DNS_NAME_FORMATSIZE];
4370 char typebuf[DNS_RDATATYPE_FORMATSIZE];
4371 char classbuf[DNS_RDATATYPE_FORMATSIZE];
4373 dns_name_format(name, namebuf, sizeof(namebuf));
4374 dns_rdatatype_format(rdataset->type, typebuf,
4376 dns_rdataclass_format(rdataset->rdclass, classbuf,
4378 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
4379 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
4380 "check-names %s %s/%s/%s",
4381 fail ? "failure" : "warning",
4382 namebuf, typebuf, classbuf);
4384 if (ANSWER(rdataset)) {
4385 dns_db_detachnode(fctx->cache, &node);
4386 return (DNS_R_BADNAME);
4393 * Enforce the configure maximum cache TTL.
4395 if (rdataset->ttl > res->view->maxcachettl)
4396 rdataset->ttl = res->view->maxcachettl;
4399 * If this RRset is in a secure domain, is in bailiwick,
4400 * and is not glue, attempt DNSSEC validation. (We do not
4401 * attempt to validate glue or out-of-bailiwick data--even
4402 * though there might be some performance benefit to doing
4403 * so--because it makes it simpler and safer to ensure that
4404 * records from a secure domain are only cached if validated
4405 * within the context of a query to the domain that owns
4408 if (secure_domain && rdataset->trust != dns_trust_glue &&
4409 !EXTERNAL(rdataset)) {
4413 * RRSIGs are validated as part of validating the
4416 if (rdataset->type == dns_rdatatype_rrsig)
4419 * Find the SIG for this rdataset, if we have it.
4421 for (sigrdataset = ISC_LIST_HEAD(name->list);
4422 sigrdataset != NULL;
4423 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4424 if (sigrdataset->type == dns_rdatatype_rrsig &&
4425 sigrdataset->covers == rdataset->type)
4428 if (sigrdataset == NULL) {
4429 if (!ANSWER(rdataset) && need_validation) {
4431 * Ignore non-answer rdatasets that
4432 * are missing signatures.
4439 * Normalize the rdataset and sigrdataset TTLs.
4441 if (sigrdataset != NULL) {
4442 rdataset->ttl = ISC_MIN(rdataset->ttl,
4444 sigrdataset->ttl = rdataset->ttl;
4448 * Cache this rdataset/sigrdataset pair as
4449 * pending data. Track whether it was additional
4452 if (rdataset->trust == dns_trust_additional)
4453 trust = dns_trust_pending_additional;
4455 trust = dns_trust_pending_answer;
4457 rdataset->trust = trust;
4458 if (sigrdataset != NULL)
4459 sigrdataset->trust = trust;
4460 if (!need_validation || !ANSWER(rdataset)) {
4461 addedrdataset = ardataset;
4462 result = dns_db_addrdataset(fctx->cache, node,
4463 NULL, now, rdataset,
4465 if (result == DNS_R_UNCHANGED) {
4466 result = ISC_R_SUCCESS;
4467 if (!need_validation &&
4468 ardataset != NULL &&
4469 ardataset->type == 0) {
4471 * The answer in the cache is
4472 * better than the answer we
4473 * found, and is a negative
4474 * cache entry, so we must set
4475 * eresult appropriately.
4477 if (NXDOMAIN(ardataset))
4479 DNS_R_NCACHENXDOMAIN;
4482 DNS_R_NCACHENXRRSET;
4484 * We have a negative response
4485 * from the cache so don't
4486 * attempt to add the RRSIG
4492 if (result != ISC_R_SUCCESS)
4494 if (sigrdataset != NULL) {
4495 addedrdataset = asigrdataset;
4496 result = dns_db_addrdataset(fctx->cache,
4500 if (result == DNS_R_UNCHANGED)
4501 result = ISC_R_SUCCESS;
4502 if (result != ISC_R_SUCCESS)
4504 } else if (!ANSWER(rdataset))
4508 if (ANSWER(rdataset) && need_validation) {
4509 if (fctx->type != dns_rdatatype_any &&
4510 fctx->type != dns_rdatatype_rrsig &&
4511 fctx->type != dns_rdatatype_sig) {
4513 * This is The Answer. We will
4514 * validate it, but first we cache
4515 * the rest of the response - it may
4516 * contain useful keys.
4518 INSIST(valrdataset == NULL &&
4519 valsigrdataset == NULL);
4520 valrdataset = rdataset;
4521 valsigrdataset = sigrdataset;
4524 * This is one of (potentially)
4525 * multiple answers to an ANY
4526 * or SIG query. To keep things
4527 * simple, we just start the
4528 * validator right away rather
4529 * than caching first and
4530 * having to remember which
4531 * rdatasets needed validation.
4533 result = valcreate(fctx, addrinfo,
4534 name, rdataset->type,
4539 * Defer any further validations.
4540 * This prevents multiple validators
4541 * from manipulating fctx->rmessage
4544 valoptions |= DNS_VALIDATOR_DEFER;
4546 } else if (CHAINING(rdataset)) {
4547 if (rdataset->type == dns_rdatatype_cname)
4548 eresult = DNS_R_CNAME;
4550 INSIST(rdataset->type ==
4551 dns_rdatatype_dname);
4552 eresult = DNS_R_DNAME;
4555 } else if (!EXTERNAL(rdataset)) {
4557 * It's OK to cache this rdataset now.
4559 if (ANSWER(rdataset))
4560 addedrdataset = ardataset;
4561 else if (ANSWERSIG(rdataset))
4562 addedrdataset = asigrdataset;
4564 addedrdataset = NULL;
4565 if (CHAINING(rdataset)) {
4566 if (rdataset->type == dns_rdatatype_cname)
4567 eresult = DNS_R_CNAME;
4569 INSIST(rdataset->type ==
4570 dns_rdatatype_dname);
4571 eresult = DNS_R_DNAME;
4574 if (rdataset->trust == dns_trust_glue &&
4575 (rdataset->type == dns_rdatatype_ns ||
4576 (rdataset->type == dns_rdatatype_rrsig &&
4577 rdataset->covers == dns_rdatatype_ns))) {
4579 * If the trust level is 'dns_trust_glue'
4580 * then we are adding data from a referral
4581 * we got while executing the search algorithm.
4582 * New referral data always takes precedence
4583 * over the existing cache contents.
4585 options = DNS_DBADD_FORCE;
4589 * Now we can add the rdataset.
4591 result = dns_db_addrdataset(fctx->cache,
4596 if (result == DNS_R_UNCHANGED) {
4597 if (ANSWER(rdataset) &&
4598 ardataset != NULL &&
4599 ardataset->type == 0) {
4601 * The answer in the cache is better
4602 * than the answer we found, and is
4603 * a negative cache entry, so we
4604 * must set eresult appropriately.
4606 if (NXDOMAIN(ardataset))
4607 eresult = DNS_R_NCACHENXDOMAIN;
4609 eresult = DNS_R_NCACHENXRRSET;
4611 result = ISC_R_SUCCESS;
4612 } else if (result != ISC_R_SUCCESS)
4617 if (valrdataset != NULL)
4618 result = valcreate(fctx, addrinfo, name, fctx->type,
4619 valrdataset, valsigrdataset, valoptions,
4622 if (result == ISC_R_SUCCESS && have_answer) {
4623 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4624 if (event != NULL) {
4626 * Negative results must be indicated in event->result.
4628 if (dns_rdataset_isassociated(event->rdataset) &&
4629 event->rdataset->type == dns_rdatatype_none) {
4630 INSIST(eresult == DNS_R_NCACHENXDOMAIN ||
4631 eresult == DNS_R_NCACHENXRRSET);
4633 event->result = eresult;
4634 dns_db_attach(fctx->cache, adbp);
4635 dns_db_transfernode(fctx->cache, &node, anodep);
4636 clone_results(fctx);
4641 dns_db_detachnode(fctx->cache, &node);
4646 static inline isc_result_t
4647 cache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_stdtime_t now)
4649 isc_result_t result;
4650 dns_section_t section;
4653 FCTXTRACE("cache_message");
4655 fctx->attributes &= ~FCTX_ATTR_WANTCACHE;
4657 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4659 for (section = DNS_SECTION_ANSWER;
4660 section <= DNS_SECTION_ADDITIONAL;
4662 result = dns_message_firstname(fctx->rmessage, section);
4663 while (result == ISC_R_SUCCESS) {
4665 dns_message_currentname(fctx->rmessage, section,
4667 if ((name->attributes & DNS_NAMEATTR_CACHE) != 0) {
4668 result = cache_name(fctx, name, addrinfo, now);
4669 if (result != ISC_R_SUCCESS)
4672 result = dns_message_nextname(fctx->rmessage, section);
4674 if (result != ISC_R_NOMORE)
4677 if (result == ISC_R_NOMORE)
4678 result = ISC_R_SUCCESS;
4680 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4686 * Do what dns_ncache_addoptout() does, and then compute an appropriate eresult.
4689 ncache_adderesult(dns_message_t *message, dns_db_t *cache, dns_dbnode_t *node,
4690 dns_rdatatype_t covers, isc_stdtime_t now, dns_ttl_t maxttl,
4691 isc_boolean_t optout, dns_rdataset_t *ardataset,
4692 isc_result_t *eresultp)
4694 isc_result_t result;
4695 dns_rdataset_t rdataset;
4697 if (ardataset == NULL) {
4698 dns_rdataset_init(&rdataset);
4699 ardataset = &rdataset;
4701 result = dns_ncache_addoptout(message, cache, node, covers, now,
4702 maxttl, optout, ardataset);
4703 if (result == DNS_R_UNCHANGED || result == ISC_R_SUCCESS) {
4705 * If the cache now contains a negative entry and we
4706 * care about whether it is DNS_R_NCACHENXDOMAIN or
4707 * DNS_R_NCACHENXRRSET then extract it.
4709 if (ardataset->type == 0) {
4711 * The cache data is a negative cache entry.
4713 if (NXDOMAIN(ardataset))
4714 *eresultp = DNS_R_NCACHENXDOMAIN;
4716 *eresultp = DNS_R_NCACHENXRRSET;
4719 * Either we don't care about the nature of the
4720 * cache rdataset (because no fetch is interested
4721 * in the outcome), or the cache rdataset is not
4722 * a negative cache entry. Whichever case it is,
4723 * we can return success.
4725 * XXXRTH There's a CNAME/DNAME problem here.
4727 *eresultp = ISC_R_SUCCESS;
4729 result = ISC_R_SUCCESS;
4731 if (ardataset == &rdataset && dns_rdataset_isassociated(ardataset))
4732 dns_rdataset_disassociate(ardataset);
4737 static inline isc_result_t
4738 ncache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
4739 dns_rdatatype_t covers, isc_stdtime_t now)
4741 isc_result_t result, eresult;
4743 dns_resolver_t *res;
4745 dns_dbnode_t *node, **anodep;
4746 dns_rdataset_t *ardataset;
4747 isc_boolean_t need_validation, secure_domain;
4749 dns_fetchevent_t *event;
4751 unsigned int valoptions = 0;
4753 FCTXTRACE("ncache_message");
4755 fctx->attributes &= ~FCTX_ATTR_WANTNCACHE;
4758 need_validation = ISC_FALSE;
4759 secure_domain = ISC_FALSE;
4760 eresult = ISC_R_SUCCESS;
4765 * XXXMPA remove when we follow cnames and adjust the setting
4766 * of FCTX_ATTR_WANTNCACHE in noanswer_response().
4768 INSIST(fctx->rmessage->counts[DNS_SECTION_ANSWER] == 0);
4771 * Is DNSSEC validation required for this name?
4773 if (fctx->res->view->enablevalidation) {
4774 result = dns_keytable_issecuredomain(res->view->secroots, name,
4776 if (result != ISC_R_SUCCESS)
4779 if (!secure_domain && res->view->dlv != NULL) {
4780 valoptions = DNS_VALIDATOR_DLV;
4781 secure_domain = ISC_TRUE;
4785 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
4786 need_validation = ISC_FALSE;
4788 need_validation = secure_domain;
4790 if (secure_domain) {
4792 * Mark all rdatasets as pending.
4794 dns_rdataset_t *trdataset;
4797 result = dns_message_firstname(fctx->rmessage,
4798 DNS_SECTION_AUTHORITY);
4799 while (result == ISC_R_SUCCESS) {
4801 dns_message_currentname(fctx->rmessage,
4802 DNS_SECTION_AUTHORITY,
4804 for (trdataset = ISC_LIST_HEAD(tname->list);
4806 trdataset = ISC_LIST_NEXT(trdataset, link))
4807 trdataset->trust = dns_trust_pending_answer;
4808 result = dns_message_nextname(fctx->rmessage,
4809 DNS_SECTION_AUTHORITY);
4811 if (result != ISC_R_NOMORE)
4816 if (need_validation) {
4818 * Do negative response validation.
4820 result = valcreate(fctx, addrinfo, name, fctx->type,
4821 NULL, NULL, valoptions,
4822 res->buckets[fctx->bucketnum].task);
4824 * If validation is necessary, return now. Otherwise continue
4825 * to process the message, letting the validation complete
4826 * in its own good time.
4831 LOCK(&res->buckets[fctx->bucketnum].lock);
4837 if (!HAVE_ANSWER(fctx)) {
4838 event = ISC_LIST_HEAD(fctx->events);
4839 if (event != NULL) {
4841 aname = dns_fixedname_name(&event->foundname);
4842 result = dns_name_copy(name, aname, NULL);
4843 if (result != ISC_R_SUCCESS)
4845 anodep = &event->node;
4846 ardataset = event->rdataset;
4851 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
4852 if (result != ISC_R_SUCCESS)
4856 * If we are asking for a SOA record set the cache time
4857 * to zero to facilitate locating the containing zone of
4860 ttl = fctx->res->view->maxncachettl;
4861 if (fctx->type == dns_rdatatype_soa &&
4862 covers == dns_rdatatype_any &&
4863 fctx->res->zero_no_soa_ttl)
4866 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
4867 covers, now, ttl, ISC_FALSE,
4868 ardataset, &eresult);
4869 if (result != ISC_R_SUCCESS)
4872 if (!HAVE_ANSWER(fctx)) {
4873 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4874 if (event != NULL) {
4875 event->result = eresult;
4876 dns_db_attach(fctx->cache, adbp);
4877 dns_db_transfernode(fctx->cache, &node, anodep);
4878 clone_results(fctx);
4883 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4886 dns_db_detachnode(fctx->cache, &node);
4892 mark_related(dns_name_t *name, dns_rdataset_t *rdataset,
4893 isc_boolean_t external, isc_boolean_t gluing)
4895 name->attributes |= DNS_NAMEATTR_CACHE;
4897 rdataset->trust = dns_trust_glue;
4899 * Glue with 0 TTL causes problems. We force the TTL to
4900 * 1 second to prevent this.
4902 if (rdataset->ttl == 0)
4905 rdataset->trust = dns_trust_additional;
4907 * Avoid infinite loops by only marking new rdatasets.
4909 if (!CACHE(rdataset)) {
4910 name->attributes |= DNS_NAMEATTR_CHASE;
4911 rdataset->attributes |= DNS_RDATASETATTR_CHASE;
4913 rdataset->attributes |= DNS_RDATASETATTR_CACHE;
4915 rdataset->attributes |= DNS_RDATASETATTR_EXTERNAL;
4919 check_related(void *arg, dns_name_t *addname, dns_rdatatype_t type) {
4920 fetchctx_t *fctx = arg;
4921 isc_result_t result;
4923 dns_rdataset_t *rdataset;
4924 isc_boolean_t external;
4925 dns_rdatatype_t rtype;
4926 isc_boolean_t gluing;
4928 REQUIRE(VALID_FCTX(fctx));
4936 result = dns_message_findname(fctx->rmessage, DNS_SECTION_ADDITIONAL,
4937 addname, dns_rdatatype_any, 0, &name,
4939 if (result == ISC_R_SUCCESS) {
4940 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
4941 if (type == dns_rdatatype_a) {
4942 for (rdataset = ISC_LIST_HEAD(name->list);
4944 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4945 if (rdataset->type == dns_rdatatype_rrsig)
4946 rtype = rdataset->covers;
4948 rtype = rdataset->type;
4949 if (rtype == dns_rdatatype_a ||
4950 rtype == dns_rdatatype_aaaa)
4951 mark_related(name, rdataset, external,
4955 result = dns_message_findtype(name, type, 0,
4957 if (result == ISC_R_SUCCESS) {
4958 mark_related(name, rdataset, external, gluing);
4960 * Do we have its SIG too?
4963 result = dns_message_findtype(name,
4964 dns_rdatatype_rrsig,
4966 if (result == ISC_R_SUCCESS)
4967 mark_related(name, rdataset, external,
4973 return (ISC_R_SUCCESS);
4977 chase_additional(fetchctx_t *fctx) {
4978 isc_boolean_t rescan;
4979 dns_section_t section = DNS_SECTION_ADDITIONAL;
4980 isc_result_t result;
4985 for (result = dns_message_firstname(fctx->rmessage, section);
4986 result == ISC_R_SUCCESS;
4987 result = dns_message_nextname(fctx->rmessage, section)) {
4988 dns_name_t *name = NULL;
4989 dns_rdataset_t *rdataset;
4990 dns_message_currentname(fctx->rmessage, DNS_SECTION_ADDITIONAL,
4992 if ((name->attributes & DNS_NAMEATTR_CHASE) == 0)
4994 name->attributes &= ~DNS_NAMEATTR_CHASE;
4995 for (rdataset = ISC_LIST_HEAD(name->list);
4997 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4998 if (CHASE(rdataset)) {
4999 rdataset->attributes &= ~DNS_RDATASETATTR_CHASE;
5000 (void)dns_rdataset_additionaldata(rdataset,
5011 static inline isc_result_t
5012 cname_target(dns_rdataset_t *rdataset, dns_name_t *tname) {
5013 isc_result_t result;
5014 dns_rdata_t rdata = DNS_RDATA_INIT;
5015 dns_rdata_cname_t cname;
5017 result = dns_rdataset_first(rdataset);
5018 if (result != ISC_R_SUCCESS)
5020 dns_rdataset_current(rdataset, &rdata);
5021 result = dns_rdata_tostruct(&rdata, &cname, NULL);
5022 if (result != ISC_R_SUCCESS)
5024 dns_name_init(tname, NULL);
5025 dns_name_clone(&cname.cname, tname);
5026 dns_rdata_freestruct(&cname);
5028 return (ISC_R_SUCCESS);
5031 static inline isc_result_t
5032 dname_target(dns_rdataset_t *rdataset, dns_name_t *qname, dns_name_t *oname,
5033 dns_fixedname_t *fixeddname)
5035 isc_result_t result;
5036 dns_rdata_t rdata = DNS_RDATA_INIT;
5037 unsigned int nlabels;
5039 dns_namereln_t namereln;
5040 dns_rdata_dname_t dname;
5041 dns_fixedname_t prefix;
5044 * Get the target name of the DNAME.
5047 result = dns_rdataset_first(rdataset);
5048 if (result != ISC_R_SUCCESS)
5050 dns_rdataset_current(rdataset, &rdata);
5051 result = dns_rdata_tostruct(&rdata, &dname, NULL);
5052 if (result != ISC_R_SUCCESS)
5056 * Get the prefix of qname.
5058 namereln = dns_name_fullcompare(qname, oname, &order, &nlabels);
5059 if (namereln != dns_namereln_subdomain) {
5060 dns_rdata_freestruct(&dname);
5061 return (DNS_R_FORMERR);
5063 dns_fixedname_init(&prefix);
5064 dns_name_split(qname, nlabels, dns_fixedname_name(&prefix), NULL);
5065 dns_fixedname_init(fixeddname);
5066 result = dns_name_concatenate(dns_fixedname_name(&prefix),
5068 dns_fixedname_name(fixeddname), NULL);
5069 dns_rdata_freestruct(&dname);
5074 * Handle a no-answer response (NXDOMAIN, NXRRSET, or referral).
5075 * If bind8_ns_resp is ISC_TRUE, this is a suspected BIND 8
5076 * response to an NS query that should be treated as a referral
5077 * even though the NS records occur in the answer section
5078 * rather than the authority section.
5081 noanswer_response(fetchctx_t *fctx, dns_name_t *oqname,
5082 isc_boolean_t bind8_ns_resp)
5084 isc_result_t result;
5085 dns_message_t *message;
5086 dns_name_t *name, *qname, *ns_name, *soa_name, *ds_name;
5087 dns_rdataset_t *rdataset, *ns_rdataset;
5088 isc_boolean_t aa, negative_response;
5089 dns_rdatatype_t type;
5090 dns_section_t section =
5091 bind8_ns_resp ? DNS_SECTION_ANSWER : DNS_SECTION_AUTHORITY;
5093 FCTXTRACE("noanswer_response");
5095 message = fctx->rmessage;
5100 if (oqname == NULL) {
5102 * We have a normal, non-chained negative response or
5105 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
5109 qname = &fctx->name;
5112 * We're being invoked by answer_response() after it has
5113 * followed a CNAME/DNAME chain.
5118 * If the current qname is not a subdomain of the query
5119 * domain, there's no point in looking at the authority
5120 * section without doing DNSSEC validation.
5122 * Until we do that validation, we'll just return success
5125 if (!dns_name_issubdomain(qname, &fctx->domain))
5126 return (ISC_R_SUCCESS);
5130 * We have to figure out if this is a negative response, or a
5135 * Sometimes we can tell if its a negative response by looking at
5136 * the message header.
5138 negative_response = ISC_FALSE;
5139 if (message->rcode == dns_rcode_nxdomain ||
5140 (message->counts[DNS_SECTION_ANSWER] == 0 &&
5141 message->counts[DNS_SECTION_AUTHORITY] == 0))
5142 negative_response = ISC_TRUE;
5145 * Process the authority section.
5151 result = dns_message_firstname(message, section);
5152 while (result == ISC_R_SUCCESS) {
5154 dns_message_currentname(message, section, &name);
5155 if (dns_name_issubdomain(name, &fctx->domain)) {
5157 * Look for NS/SOA RRsets first.
5159 for (rdataset = ISC_LIST_HEAD(name->list);
5161 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5162 type = rdataset->type;
5163 if (type == dns_rdatatype_rrsig)
5164 type = rdataset->covers;
5165 if (((type == dns_rdatatype_ns ||
5166 type == dns_rdatatype_soa) &&
5167 !dns_name_issubdomain(qname, name)))
5168 return (DNS_R_FORMERR);
5169 if (type == dns_rdatatype_ns) {
5173 * Only one set of NS RRs is allowed.
5175 if (rdataset->type ==
5177 if (ns_name != NULL &&
5179 return (DNS_R_FORMERR);
5181 ns_rdataset = rdataset;
5185 rdataset->attributes |=
5186 DNS_RDATASETATTR_CACHE;
5187 rdataset->trust = dns_trust_glue;
5189 if (type == dns_rdatatype_soa) {
5191 * SOA, or RRSIG SOA.
5193 * Only one SOA is allowed.
5195 if (rdataset->type ==
5196 dns_rdatatype_soa) {
5197 if (soa_name != NULL &&
5199 return (DNS_R_FORMERR);
5203 DNS_NAMEATTR_NCACHE;
5204 rdataset->attributes |=
5205 DNS_RDATASETATTR_NCACHE;
5208 dns_trust_authauthority;
5211 dns_trust_additional;
5215 result = dns_message_nextname(message, section);
5216 if (result == ISC_R_NOMORE)
5218 else if (result != ISC_R_SUCCESS)
5223 * A negative response has a SOA record (Type 2)
5224 * and a optional NS RRset (Type 1) or it has neither
5225 * a SOA or a NS RRset (Type 3, handled above) or
5226 * rcode is NXDOMAIN (handled above) in which case
5227 * the NS RRset is allowed (Type 4).
5229 if (soa_name != NULL)
5230 negative_response = ISC_TRUE;
5232 result = dns_message_firstname(message, section);
5233 while (result == ISC_R_SUCCESS) {
5235 dns_message_currentname(message, section, &name);
5236 if (dns_name_issubdomain(name, &fctx->domain)) {
5237 for (rdataset = ISC_LIST_HEAD(name->list);
5239 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5240 type = rdataset->type;
5241 if (type == dns_rdatatype_rrsig)
5242 type = rdataset->covers;
5243 if (type == dns_rdatatype_nsec ||
5244 type == dns_rdatatype_nsec3) {
5246 * NSEC or RRSIG NSEC.
5248 if (negative_response) {
5250 DNS_NAMEATTR_NCACHE;
5251 rdataset->attributes |=
5252 DNS_RDATASETATTR_NCACHE;
5253 } else if (type == dns_rdatatype_nsec) {
5256 rdataset->attributes |=
5257 DNS_RDATASETATTR_CACHE;
5261 dns_trust_authauthority;
5264 dns_trust_additional;
5266 * No additional data needs to be
5269 } else if (type == dns_rdatatype_ds) {
5273 * These should only be here if
5274 * this is a referral, and there
5275 * should only be one DS.
5277 if (ns_name == NULL)
5278 return (DNS_R_FORMERR);
5279 if (rdataset->type ==
5281 if (ds_name != NULL &&
5283 return (DNS_R_FORMERR);
5288 rdataset->attributes |=
5289 DNS_RDATASETATTR_CACHE;
5292 dns_trust_authauthority;
5295 dns_trust_additional;
5299 result = dns_message_nextname(message, section);
5300 if (result == ISC_R_NOMORE)
5302 else if (result != ISC_R_SUCCESS)
5307 * Trigger lookups for DNS nameservers.
5309 if (negative_response && message->rcode == dns_rcode_noerror &&
5310 fctx->type == dns_rdatatype_ds && soa_name != NULL &&
5311 dns_name_equal(soa_name, qname) &&
5312 !dns_name_equal(qname, dns_rootname))
5313 return (DNS_R_CHASEDSSERVERS);
5316 * Did we find anything?
5318 if (!negative_response && ns_name == NULL) {
5322 if (oqname != NULL) {
5324 * We've already got a partial CNAME/DNAME chain,
5325 * and haven't found else anything useful here, but
5326 * no error has occurred since we have an answer.
5328 return (ISC_R_SUCCESS);
5331 * The responder is insane.
5333 return (DNS_R_FORMERR);
5338 * If we found both NS and SOA, they should be the same name.
5340 if (ns_name != NULL && soa_name != NULL && ns_name != soa_name)
5341 return (DNS_R_FORMERR);
5344 * Do we have a referral? (We only want to follow a referral if
5345 * we're not following a chain.)
5347 if (!negative_response && ns_name != NULL && oqname == NULL) {
5349 * We already know ns_name is a subdomain of fctx->domain.
5350 * If ns_name is equal to fctx->domain, we're not making
5351 * progress. We return DNS_R_FORMERR so that we'll keep
5352 * trying other servers.
5354 if (dns_name_equal(ns_name, &fctx->domain))
5355 return (DNS_R_FORMERR);
5358 * If the referral name is not a parent of the query
5359 * name, consider the responder insane.
5361 if (! dns_name_issubdomain(&fctx->name, ns_name)) {
5362 FCTXTRACE("referral to non-parent");
5363 return (DNS_R_FORMERR);
5367 * Mark any additional data related to this rdataset.
5368 * It's important that we do this before we change the
5371 INSIST(ns_rdataset != NULL);
5372 fctx->attributes |= FCTX_ATTR_GLUING;
5373 (void)dns_rdataset_additionaldata(ns_rdataset, check_related,
5375 fctx->attributes &= ~FCTX_ATTR_GLUING;
5377 * NS rdatasets with 0 TTL cause problems.
5378 * dns_view_findzonecut() will not find them when we
5379 * try to follow the referral, and we'll SERVFAIL
5380 * because the best nameservers are now above QDOMAIN.
5381 * We force the TTL to 1 second to prevent this.
5383 if (ns_rdataset->ttl == 0)
5384 ns_rdataset->ttl = 1;
5386 * Set the current query domain to the referral name.
5388 * XXXRTH We should check if we're in forward-only mode, and
5389 * if so we should bail out.
5391 INSIST(dns_name_countlabels(&fctx->domain) > 0);
5392 dns_name_free(&fctx->domain,
5393 fctx->res->buckets[fctx->bucketnum].mctx);
5394 if (dns_rdataset_isassociated(&fctx->nameservers))
5395 dns_rdataset_disassociate(&fctx->nameservers);
5396 dns_name_init(&fctx->domain, NULL);
5397 result = dns_name_dup(ns_name,
5398 fctx->res->buckets[fctx->bucketnum].mctx,
5400 if (result != ISC_R_SUCCESS)
5402 fctx->attributes |= FCTX_ATTR_WANTCACHE;
5403 return (DNS_R_DELEGATION);
5407 * Since we're not doing a referral, we don't want to cache any
5408 * NS RRs we may have found.
5410 if (ns_name != NULL)
5411 ns_name->attributes &= ~DNS_NAMEATTR_CACHE;
5413 if (negative_response && oqname == NULL)
5414 fctx->attributes |= FCTX_ATTR_WANTNCACHE;
5416 return (ISC_R_SUCCESS);
5420 answer_response(fetchctx_t *fctx) {
5421 isc_result_t result;
5422 dns_message_t *message;
5423 dns_name_t *name, *qname, tname;
5424 dns_rdataset_t *rdataset;
5425 isc_boolean_t done, external, chaining, aa, found, want_chaining;
5426 isc_boolean_t have_answer, found_cname, found_type, wanted_chaining;
5428 dns_rdatatype_t type;
5429 dns_fixedname_t dname, fqname;
5431 FCTXTRACE("answer_response");
5433 message = fctx->rmessage;
5436 * Examine the answer section, marking those rdatasets which are
5437 * part of the answer and should be cached.
5441 found_cname = ISC_FALSE;
5442 found_type = ISC_FALSE;
5443 chaining = ISC_FALSE;
5444 have_answer = ISC_FALSE;
5445 want_chaining = ISC_FALSE;
5446 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
5450 qname = &fctx->name;
5452 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
5453 while (!done && result == ISC_R_SUCCESS) {
5455 dns_message_currentname(message, DNS_SECTION_ANSWER, &name);
5456 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
5457 if (dns_name_equal(name, qname)) {
5458 wanted_chaining = ISC_FALSE;
5459 for (rdataset = ISC_LIST_HEAD(name->list);
5461 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5463 want_chaining = ISC_FALSE;
5465 if (rdataset->type == dns_rdatatype_nsec3) {
5467 * NSEC3 records are not allowed to
5468 * appear in the answer section.
5470 return (DNS_R_FORMERR);
5472 if (rdataset->type == type && !found_cname) {
5474 * We've found an ordinary answer.
5477 found_type = ISC_TRUE;
5479 aflag = DNS_RDATASETATTR_ANSWER;
5480 } else if (type == dns_rdatatype_any) {
5482 * We've found an answer matching
5483 * an ANY query. There may be
5487 aflag = DNS_RDATASETATTR_ANSWER;
5488 } else if (rdataset->type == dns_rdatatype_rrsig
5489 && rdataset->covers == type
5492 * We've found a signature that
5493 * covers the type we're looking for.
5496 found_type = ISC_TRUE;
5497 aflag = DNS_RDATASETATTR_ANSWERSIG;
5498 } else if (rdataset->type ==
5502 * We're looking for something else,
5503 * but we found a CNAME.
5505 * Getting a CNAME response for some
5506 * query types is an error.
5508 if (type == dns_rdatatype_rrsig ||
5509 type == dns_rdatatype_dnskey ||
5510 type == dns_rdatatype_nsec)
5511 return (DNS_R_FORMERR);
5513 found_cname = ISC_TRUE;
5514 want_chaining = ISC_TRUE;
5515 aflag = DNS_RDATASETATTR_ANSWER;
5516 result = cname_target(rdataset,
5518 if (result != ISC_R_SUCCESS)
5520 } else if (rdataset->type == dns_rdatatype_rrsig
5521 && rdataset->covers ==
5525 * We're looking for something else,
5526 * but we found a SIG CNAME.
5529 found_cname = ISC_TRUE;
5530 aflag = DNS_RDATASETATTR_ANSWERSIG;
5535 * We've found an answer to our
5540 rdataset->attributes |=
5541 DNS_RDATASETATTR_CACHE;
5542 rdataset->trust = dns_trust_answer;
5545 * This data is "the" answer
5546 * to our question only if
5547 * we're not chaining (i.e.
5548 * if we haven't followed
5549 * a CNAME or DNAME).
5553 DNS_RDATASETATTR_ANSWER)
5554 have_answer = ISC_TRUE;
5556 DNS_NAMEATTR_ANSWER;
5557 rdataset->attributes |= aflag;
5560 dns_trust_authanswer;
5561 } else if (external) {
5563 * This data is outside of
5564 * our query domain, and
5565 * may not be cached.
5567 rdataset->attributes |=
5568 DNS_RDATASETATTR_EXTERNAL;
5572 * Mark any additional data related
5575 (void)dns_rdataset_additionaldata(
5583 if (want_chaining) {
5584 wanted_chaining = ISC_TRUE;
5586 DNS_NAMEATTR_CHAINING;
5587 rdataset->attributes |=
5588 DNS_RDATASETATTR_CHAINING;
5593 * We could add an "else" clause here and
5594 * log that we're ignoring this rdataset.
5598 * If wanted_chaining is true, we've done
5599 * some chaining as the result of processing
5600 * this node, and thus we need to set
5603 * We don't set chaining inside of the
5604 * rdataset loop because doing that would
5605 * cause us to ignore the signatures of
5608 if (wanted_chaining)
5609 chaining = ISC_TRUE;
5612 * Look for a DNAME (or its SIG). Anything else is
5615 wanted_chaining = ISC_FALSE;
5616 for (rdataset = ISC_LIST_HEAD(name->list);
5618 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5619 isc_boolean_t found_dname = ISC_FALSE;
5622 if (rdataset->type == dns_rdatatype_dname) {
5624 * We're looking for something else,
5625 * but we found a DNAME.
5627 * If we're not chaining, then the
5628 * DNAME should not be external.
5630 if (!chaining && external)
5631 return (DNS_R_FORMERR);
5633 want_chaining = ISC_TRUE;
5634 aflag = DNS_RDATASETATTR_ANSWER;
5635 result = dname_target(rdataset,
5638 if (result == ISC_R_NOSPACE) {
5640 * We can't construct the
5641 * DNAME target. Do not
5644 want_chaining = ISC_FALSE;
5645 } else if (result != ISC_R_SUCCESS)
5648 found_dname = ISC_TRUE;
5649 } else if (rdataset->type == dns_rdatatype_rrsig
5650 && rdataset->covers ==
5651 dns_rdatatype_dname) {
5653 * We've found a signature that
5657 aflag = DNS_RDATASETATTR_ANSWERSIG;
5662 * We've found an answer to our
5667 rdataset->attributes |=
5668 DNS_RDATASETATTR_CACHE;
5669 rdataset->trust = dns_trust_answer;
5672 * This data is "the" answer
5673 * to our question only if
5674 * we're not chaining.
5678 DNS_RDATASETATTR_ANSWER)
5679 have_answer = ISC_TRUE;
5681 DNS_NAMEATTR_ANSWER;
5682 rdataset->attributes |= aflag;
5685 dns_trust_authanswer;
5686 } else if (external) {
5687 rdataset->attributes |=
5688 DNS_RDATASETATTR_EXTERNAL;
5696 * Copy the dname into the
5699 * Although we check for
5700 * failure of the copy
5701 * operation, in practice it
5702 * should never fail since
5703 * we already know that the
5704 * result fits in a fixedname.
5706 dns_fixedname_init(&fqname);
5707 result = dns_name_copy(
5708 dns_fixedname_name(&dname),
5709 dns_fixedname_name(&fqname),
5711 if (result != ISC_R_SUCCESS)
5713 wanted_chaining = ISC_TRUE;
5715 DNS_NAMEATTR_CHAINING;
5716 rdataset->attributes |=
5717 DNS_RDATASETATTR_CHAINING;
5718 qname = dns_fixedname_name(
5723 if (wanted_chaining)
5724 chaining = ISC_TRUE;
5726 result = dns_message_nextname(message, DNS_SECTION_ANSWER);
5728 if (result == ISC_R_NOMORE)
5729 result = ISC_R_SUCCESS;
5730 if (result != ISC_R_SUCCESS)
5734 * We should have found an answer.
5737 return (DNS_R_FORMERR);
5740 * This response is now potentially cacheable.
5742 fctx->attributes |= FCTX_ATTR_WANTCACHE;
5745 * Did chaining end before we got the final answer?
5749 * Yes. This may be a negative reply, so hand off
5750 * authority section processing to the noanswer code.
5751 * If it isn't a noanswer response, no harm will be
5754 return (noanswer_response(fctx, qname, ISC_FALSE));
5758 * We didn't end with an incomplete chain, so the rcode should be
5761 if (message->rcode != dns_rcode_noerror)
5762 return (DNS_R_FORMERR);
5765 * Examine the authority section (if there is one).
5767 * We expect there to be only one owner name for all the rdatasets
5768 * in this section, and we expect that it is not external.
5771 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
5772 while (!done && result == ISC_R_SUCCESS) {
5774 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
5775 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
5778 * We expect to find NS or SIG NS rdatasets, and
5781 for (rdataset = ISC_LIST_HEAD(name->list);
5783 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5784 if (rdataset->type == dns_rdatatype_ns ||
5785 (rdataset->type == dns_rdatatype_rrsig &&
5786 rdataset->covers == dns_rdatatype_ns)) {
5789 rdataset->attributes |=
5790 DNS_RDATASETATTR_CACHE;
5791 if (aa && !chaining)
5793 dns_trust_authauthority;
5796 dns_trust_additional;
5799 * Mark any additional data related
5802 (void)dns_rdataset_additionaldata(
5810 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
5812 if (result == ISC_R_NOMORE)
5813 result = ISC_R_SUCCESS;
5819 resume_dslookup(isc_task_t *task, isc_event_t *event) {
5820 dns_fetchevent_t *fevent;
5821 dns_resolver_t *res;
5823 isc_result_t result;
5824 isc_boolean_t bucket_empty = ISC_FALSE;
5825 isc_boolean_t locked = ISC_FALSE;
5826 unsigned int bucketnum;
5827 dns_rdataset_t nameservers;
5828 dns_fixedname_t fixed;
5831 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
5832 fevent = (dns_fetchevent_t *)event;
5833 fctx = event->ev_arg;
5834 REQUIRE(VALID_FCTX(fctx));
5838 FCTXTRACE("resume_dslookup");
5840 if (fevent->node != NULL)
5841 dns_db_detachnode(fevent->db, &fevent->node);
5842 if (fevent->db != NULL)
5843 dns_db_detach(&fevent->db);
5845 dns_rdataset_init(&nameservers);
5847 bucketnum = fctx->bucketnum;
5848 if (fevent->result == ISC_R_CANCELED) {
5849 dns_resolver_destroyfetch(&fctx->nsfetch);
5850 fctx_done(fctx, ISC_R_CANCELED, __LINE__);
5851 } else if (fevent->result == ISC_R_SUCCESS) {
5853 FCTXTRACE("resuming DS lookup");
5855 dns_resolver_destroyfetch(&fctx->nsfetch);
5856 if (dns_rdataset_isassociated(&fctx->nameservers))
5857 dns_rdataset_disassociate(&fctx->nameservers);
5858 dns_rdataset_clone(fevent->rdataset, &fctx->nameservers);
5859 dns_name_free(&fctx->domain,
5860 fctx->res->buckets[bucketnum].mctx);
5861 dns_name_init(&fctx->domain, NULL);
5862 result = dns_name_dup(&fctx->nsname,
5863 fctx->res->buckets[bucketnum].mctx,
5865 if (result != ISC_R_SUCCESS) {
5866 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
5872 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
5875 dns_rdataset_t *nsrdataset = NULL;
5878 * Retrieve state from fctx->nsfetch before we destroy it.
5880 dns_fixedname_init(&fixed);
5881 domain = dns_fixedname_name(&fixed);
5882 dns_name_copy(&fctx->nsfetch->private->domain, domain, NULL);
5883 if (dns_name_equal(&fctx->nsname, domain)) {
5884 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
5885 dns_resolver_destroyfetch(&fctx->nsfetch);
5888 if (dns_rdataset_isassociated(
5889 &fctx->nsfetch->private->nameservers)) {
5891 &fctx->nsfetch->private->nameservers,
5893 nsrdataset = &nameservers;
5896 dns_resolver_destroyfetch(&fctx->nsfetch);
5897 n = dns_name_countlabels(&fctx->nsname);
5898 dns_name_getlabelsequence(&fctx->nsname, 1, n - 1,
5901 if (dns_rdataset_isassociated(fevent->rdataset))
5902 dns_rdataset_disassociate(fevent->rdataset);
5903 FCTXTRACE("continuing to look for parent's NS records");
5904 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
5905 dns_rdatatype_ns, domain,
5906 nsrdataset, NULL, 0, task,
5907 resume_dslookup, fctx,
5908 &fctx->nsrrset, NULL,
5910 if (result != ISC_R_SUCCESS)
5911 fctx_done(fctx, result, __LINE__);
5913 LOCK(&res->buckets[bucketnum].lock);
5920 if (dns_rdataset_isassociated(&nameservers))
5921 dns_rdataset_disassociate(&nameservers);
5922 if (dns_rdataset_isassociated(fevent->rdataset))
5923 dns_rdataset_disassociate(fevent->rdataset);
5924 INSIST(fevent->sigrdataset == NULL);
5925 isc_event_free(&event);
5927 LOCK(&res->buckets[bucketnum].lock);
5929 if (fctx->references == 0)
5930 bucket_empty = fctx_destroy(fctx);
5931 UNLOCK(&res->buckets[bucketnum].lock);
5937 checknamessection(dns_message_t *message, dns_section_t section) {
5938 isc_result_t result;
5940 dns_rdata_t rdata = DNS_RDATA_INIT;
5941 dns_rdataset_t *rdataset;
5943 for (result = dns_message_firstname(message, section);
5944 result == ISC_R_SUCCESS;
5945 result = dns_message_nextname(message, section))
5948 dns_message_currentname(message, section, &name);
5949 for (rdataset = ISC_LIST_HEAD(name->list);
5951 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5952 for (result = dns_rdataset_first(rdataset);
5953 result == ISC_R_SUCCESS;
5954 result = dns_rdataset_next(rdataset)) {
5955 dns_rdataset_current(rdataset, &rdata);
5956 if (!dns_rdata_checkowner(name, rdata.rdclass,
5959 !dns_rdata_checknames(&rdata, name, NULL))
5961 rdataset->attributes |=
5962 DNS_RDATASETATTR_CHECKNAMES;
5964 dns_rdata_reset(&rdata);
5971 checknames(dns_message_t *message) {
5973 checknamessection(message, DNS_SECTION_ANSWER);
5974 checknamessection(message, DNS_SECTION_AUTHORITY);
5975 checknamessection(message, DNS_SECTION_ADDITIONAL);
5979 * Log server NSID at log level 'level'
5982 log_nsid(dns_rdataset_t *opt, resquery_t *query, int level, isc_mem_t *mctx)
5984 static const char hex[17] = "0123456789abcdef";
5985 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
5986 isc_uint16_t optcode, nsid_len, buflen, i;
5987 isc_result_t result;
5988 isc_buffer_t nsidbuf;
5990 unsigned char *p, *buf, *nsid;
5992 /* Extract rdata from OPT rdataset */
5993 result = dns_rdataset_first(opt);
5994 if (result != ISC_R_SUCCESS)
5995 return (ISC_R_FAILURE);
5997 dns_rdata_init(&rdata);
5998 dns_rdataset_current(opt, &rdata);
5999 if (rdata.length < 4)
6000 return (ISC_R_FAILURE);
6002 /* Check for NSID */
6003 isc_buffer_init(&nsidbuf, rdata.data, rdata.length);
6004 isc_buffer_add(&nsidbuf, rdata.length);
6005 optcode = isc_buffer_getuint16(&nsidbuf);
6006 nsid_len = isc_buffer_getuint16(&nsidbuf);
6007 if (optcode != DNS_OPT_NSID || nsid_len == 0)
6008 return (ISC_R_FAILURE);
6010 /* Allocate buffer for storing hex version of the NSID */
6011 buflen = nsid_len * 2 + 1;
6012 buf = isc_mem_get(mctx, buflen);
6014 return (ISC_R_NOSPACE);
6016 /* Convert to hex */
6018 nsid = rdata.data + 4;
6019 for (i = 0; i < nsid_len; i++) {
6020 *p++ = hex[(nsid[0] >> 4) & 0xf];
6021 *p++ = hex[nsid[0] & 0xf];
6026 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
6028 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6029 DNS_LOGMODULE_RESOLVER, level,
6030 "received NSID '%s' from %s", buf, addrbuf);
6033 isc_mem_put(mctx, buf, buflen);
6034 return (ISC_R_SUCCESS);
6038 log_packet(dns_message_t *message, int level, isc_mem_t *mctx) {
6039 isc_buffer_t buffer;
6042 isc_result_t result;
6044 if (! isc_log_wouldlog(dns_lctx, level))
6048 * Note that these are multiline debug messages. We want a newline
6049 * to appear in the log after each message.
6053 buf = isc_mem_get(mctx, len);
6056 isc_buffer_init(&buffer, buf, len);
6057 result = dns_message_totext(message, &dns_master_style_debug,
6059 if (result == ISC_R_NOSPACE) {
6060 isc_mem_put(mctx, buf, len);
6062 } else if (result == ISC_R_SUCCESS)
6063 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6064 DNS_LOGMODULE_RESOLVER, level,
6065 "received packet:\n%.*s",
6066 (int)isc_buffer_usedlength(&buffer),
6068 } while (result == ISC_R_NOSPACE);
6071 isc_mem_put(mctx, buf, len);
6075 resquery_response(isc_task_t *task, isc_event_t *event) {
6076 isc_result_t result = ISC_R_SUCCESS;
6077 resquery_t *query = event->ev_arg;
6078 dns_dispatchevent_t *devent = (dns_dispatchevent_t *)event;
6079 isc_boolean_t keep_trying, get_nameservers, resend;
6080 isc_boolean_t truncated;
6081 dns_message_t *message;
6082 dns_rdataset_t *opt;
6085 dns_fixedname_t foundname;
6087 isc_time_t tnow, *finish;
6088 dns_adbaddrinfo_t *addrinfo;
6089 unsigned int options;
6090 unsigned int findoptions;
6091 isc_result_t broken_server;
6092 badnstype_t broken_type = badns_response;
6094 REQUIRE(VALID_QUERY(query));
6096 options = query->options;
6097 REQUIRE(VALID_FCTX(fctx));
6098 REQUIRE(event->ev_type == DNS_EVENT_DISPATCH);
6102 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) == PF_INET)
6103 inc_stats(fctx->res, dns_resstatscounter_responsev4);
6105 inc_stats(fctx->res, dns_resstatscounter_responsev6);
6107 (void)isc_timer_touch(fctx->timer);
6109 keep_trying = ISC_FALSE;
6110 broken_server = ISC_R_SUCCESS;
6111 get_nameservers = ISC_FALSE;
6113 truncated = ISC_FALSE;
6116 if (fctx->res->exiting) {
6117 result = ISC_R_SHUTTINGDOWN;
6122 fctx->timeout = ISC_FALSE;
6125 * XXXRTH We should really get the current time just once. We
6126 * need a routine to convert from an isc_time_t to an
6131 isc_stdtime_get(&now);
6134 * Did the dispatcher have a problem?
6136 if (devent->result != ISC_R_SUCCESS) {
6137 if (devent->result == ISC_R_EOF &&
6138 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6140 * The problem might be that they
6141 * don't understand EDNS0. Turn it
6142 * off and try again.
6144 options |= DNS_FETCHOPT_NOEDNS0;
6147 * Remember that they don't like EDNS0.
6149 dns_adb_changeflags(fctx->adb,
6151 DNS_FETCHOPT_NOEDNS0,
6152 DNS_FETCHOPT_NOEDNS0);
6155 * There's no hope for this query.
6157 keep_trying = ISC_TRUE;
6160 * If this is a network error on an exclusive query
6161 * socket, mark the server as bad so that we won't try
6162 * it for this fetch again.
6164 if (query->exclusivesocket &&
6165 (devent->result == ISC_R_HOSTUNREACH ||
6166 devent->result == ISC_R_NETUNREACH ||
6167 devent->result == ISC_R_CONNREFUSED ||
6168 devent->result == ISC_R_CANCELED)) {
6169 broken_server = devent->result;
6170 broken_type = badns_unreachable;
6176 message = fctx->rmessage;
6178 if (query->tsig != NULL) {
6179 result = dns_message_setquerytsig(message, query->tsig);
6180 if (result != ISC_R_SUCCESS)
6184 if (query->tsigkey) {
6185 result = dns_message_settsigkey(message, query->tsigkey);
6186 if (result != ISC_R_SUCCESS)
6190 result = dns_message_parse(message, &devent->buffer, 0);
6191 if (result != ISC_R_SUCCESS) {
6193 case ISC_R_UNEXPECTEDEND:
6194 if (!message->question_ok ||
6195 (message->flags & DNS_MESSAGEFLAG_TC) == 0 ||
6196 (options & DNS_FETCHOPT_TCP) != 0) {
6198 * Either the message ended prematurely,
6199 * and/or wasn't marked as being truncated,
6200 * and/or this is a response to a query we
6201 * sent over TCP. In all of these cases,
6202 * something is wrong with the remote
6203 * server and we don't want to retry using
6206 if ((query->options & DNS_FETCHOPT_NOEDNS0)
6209 * The problem might be that they
6210 * don't understand EDNS0. Turn it
6211 * off and try again.
6213 options |= DNS_FETCHOPT_NOEDNS0;
6216 * Remember that they don't like EDNS0.
6218 dns_adb_changeflags(
6221 DNS_FETCHOPT_NOEDNS0,
6222 DNS_FETCHOPT_NOEDNS0);
6223 inc_stats(fctx->res,
6224 dns_resstatscounter_edns0fail);
6226 broken_server = result;
6227 keep_trying = ISC_TRUE;
6232 * We defer retrying via TCP for a bit so we can
6233 * check out this message further.
6235 truncated = ISC_TRUE;
6238 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6240 * The problem might be that they
6241 * don't understand EDNS0. Turn it
6242 * off and try again.
6244 options |= DNS_FETCHOPT_NOEDNS0;
6247 * Remember that they don't like EDNS0.
6249 dns_adb_changeflags(fctx->adb,
6251 DNS_FETCHOPT_NOEDNS0,
6252 DNS_FETCHOPT_NOEDNS0);
6253 inc_stats(fctx->res,
6254 dns_resstatscounter_edns0fail);
6256 broken_server = DNS_R_UNEXPECTEDRCODE;
6257 keep_trying = ISC_TRUE;
6262 * Something bad has happened.
6270 * Log the incoming packet.
6272 log_packet(message, ISC_LOG_DEBUG(10), fctx->res->mctx);
6275 * Did we request NSID? If so, and if the response contains
6276 * NSID data, log it at INFO level.
6278 opt = dns_message_getopt(message);
6279 if (opt != NULL && (query->options & DNS_FETCHOPT_WANTNSID) != 0)
6280 log_nsid(opt, query, ISC_LOG_INFO, fctx->res->mctx);
6283 * If the message is signed, check the signature. If not, this
6284 * returns success anyway.
6286 result = dns_message_checksig(message, fctx->res->view);
6287 if (result != ISC_R_SUCCESS)
6291 * The dispatcher should ensure we only get responses with QR set.
6293 INSIST((message->flags & DNS_MESSAGEFLAG_QR) != 0);
6295 * INSIST() that the message comes from the place we sent it to,
6296 * since the dispatch code should ensure this.
6298 * INSIST() that the message id is correct (this should also be
6299 * ensured by the dispatch code).
6304 * Deal with truncated responses by retrying using TCP.
6306 if ((message->flags & DNS_MESSAGEFLAG_TC) != 0)
6307 truncated = ISC_TRUE;
6310 inc_stats(fctx->res, dns_resstatscounter_truncated);
6311 if ((options & DNS_FETCHOPT_TCP) != 0) {
6312 broken_server = DNS_R_TRUNCATEDTCP;
6313 keep_trying = ISC_TRUE;
6315 options |= DNS_FETCHOPT_TCP;
6322 * Is it a query response?
6324 if (message->opcode != dns_opcode_query) {
6326 broken_server = DNS_R_UNEXPECTEDOPCODE;
6327 keep_trying = ISC_TRUE;
6332 * Update statistics about erroneous responses.
6334 if (message->rcode != dns_rcode_noerror) {
6335 switch (message->rcode) {
6336 case dns_rcode_nxdomain:
6337 inc_stats(fctx->res, dns_resstatscounter_nxdomain);
6339 case dns_rcode_servfail:
6340 inc_stats(fctx->res, dns_resstatscounter_servfail);
6342 case dns_rcode_formerr:
6343 inc_stats(fctx->res, dns_resstatscounter_formerr);
6346 inc_stats(fctx->res, dns_resstatscounter_othererror);
6352 * Is the remote server broken, or does it dislike us?
6354 if (message->rcode != dns_rcode_noerror &&
6355 message->rcode != dns_rcode_nxdomain) {
6356 if (((message->rcode == dns_rcode_formerr ||
6357 message->rcode == dns_rcode_notimp) ||
6358 (message->rcode == dns_rcode_servfail &&
6359 dns_message_getopt(message) == NULL)) &&
6360 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6362 * It's very likely they don't like EDNS0.
6363 * If the response code is SERVFAIL, also check if the
6364 * response contains an OPT RR and don't cache the
6365 * failure since it can be returned for various other
6368 * XXXRTH We should check if the question
6369 * we're asking requires EDNS0, and
6370 * if so, we should bail out.
6372 options |= DNS_FETCHOPT_NOEDNS0;
6375 * Remember that they don't like EDNS0.
6377 if (message->rcode != dns_rcode_servfail)
6378 dns_adb_changeflags(fctx->adb, query->addrinfo,
6379 DNS_FETCHOPT_NOEDNS0,
6380 DNS_FETCHOPT_NOEDNS0);
6381 inc_stats(fctx->res, dns_resstatscounter_edns0fail);
6382 } else if (message->rcode == dns_rcode_formerr) {
6383 if (ISFORWARDER(query->addrinfo)) {
6385 * This forwarder doesn't understand us,
6386 * but other forwarders might. Keep trying.
6388 broken_server = DNS_R_REMOTEFORMERR;
6389 keep_trying = ISC_TRUE;
6392 * The server doesn't understand us. Since
6393 * all servers for a zone need similar
6394 * capabilities, we assume that we will get
6395 * FORMERR from all servers, and thus we
6396 * cannot make any more progress with this
6399 result = DNS_R_FORMERR;
6401 } else if (message->rcode == dns_rcode_yxdomain) {
6403 * DNAME mapping failed because the new name
6404 * was too long. There's no chance of success
6407 result = DNS_R_YXDOMAIN;
6408 } else if (message->rcode == dns_rcode_badvers) {
6409 unsigned int flags, mask;
6410 unsigned int version;
6413 version = (opt->ttl >> 16) & 0xff;
6414 flags = (version << DNS_FETCHOPT_EDNSVERSIONSHIFT) |
6415 DNS_FETCHOPT_EDNSVERSIONSET;
6416 mask = DNS_FETCHOPT_EDNSVERSIONMASK |
6417 DNS_FETCHOPT_EDNSVERSIONSET;
6420 dns_adb_changeflags(fctx->adb, query->addrinfo,
6424 broken_server = DNS_R_BADVERS;
6425 keep_trying = ISC_TRUE;
6432 broken_server = DNS_R_UNEXPECTEDRCODE;
6433 INSIST(broken_server != ISC_R_SUCCESS);
6434 keep_trying = ISC_TRUE;
6440 * Is the question the same as the one we asked?
6442 result = same_question(fctx);
6443 if (result != ISC_R_SUCCESS) {
6445 if (result == DNS_R_FORMERR)
6446 keep_trying = ISC_TRUE;
6451 * Is the server lame?
6453 if (fctx->res->lame_ttl != 0 && !ISFORWARDER(query->addrinfo) &&
6455 inc_stats(fctx->res, dns_resstatscounter_lame);
6456 log_lame(fctx, query->addrinfo);
6457 result = dns_adb_marklame(fctx->adb, query->addrinfo,
6458 &fctx->name, fctx->type,
6459 now + fctx->res->lame_ttl);
6460 if (result != ISC_R_SUCCESS)
6461 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6462 DNS_LOGMODULE_RESOLVER, ISC_LOG_ERROR,
6463 "could not mark server as lame: %s",
6464 isc_result_totext(result));
6465 broken_server = DNS_R_LAME;
6466 keep_trying = ISC_TRUE;
6471 * Enforce delegations only zones like NET and COM.
6473 if (!ISFORWARDER(query->addrinfo) &&
6474 dns_view_isdelegationonly(fctx->res->view, &fctx->domain) &&
6475 !dns_name_equal(&fctx->domain, &fctx->name) &&
6476 fix_mustbedelegationornxdomain(message, fctx)) {
6477 char namebuf[DNS_NAME_FORMATSIZE];
6478 char domainbuf[DNS_NAME_FORMATSIZE];
6479 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
6483 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
6484 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
6485 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
6486 dns_rdataclass_format(fctx->res->rdclass, classbuf,
6488 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
6491 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DELEGATION_ONLY,
6492 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
6493 "enforced delegation-only for '%s' (%s/%s/%s) "
6495 domainbuf, namebuf, typebuf, classbuf, addrbuf);
6498 if ((fctx->res->options & DNS_RESOLVER_CHECKNAMES) != 0)
6499 checknames(message);
6504 fctx->attributes &= ~(FCTX_ATTR_WANTNCACHE | FCTX_ATTR_WANTCACHE);
6507 * Did we get any answers?
6509 if (message->counts[DNS_SECTION_ANSWER] > 0 &&
6510 (message->rcode == dns_rcode_noerror ||
6511 message->rcode == dns_rcode_nxdomain)) {
6513 * We've got answers. However, if we sent
6514 * a BIND 8 server an NS query, it may have
6515 * incorrectly responded with a non-authoritative
6516 * answer instead of a referral. Since this
6517 * answer lacks the SIGs necessary to do DNSSEC
6518 * validation, we must invoke the following special
6519 * kludge to treat it as a referral.
6521 if (fctx->type == dns_rdatatype_ns &&
6522 (message->flags & DNS_MESSAGEFLAG_AA) == 0 &&
6523 !ISFORWARDER(query->addrinfo))
6525 result = noanswer_response(fctx, NULL, ISC_TRUE);
6526 if (result != DNS_R_DELEGATION) {
6528 * The answer section must have contained
6529 * something other than the NS records
6530 * we asked for. Since AA is not set
6531 * and the server is not a forwarder,
6532 * it is technically lame and it's easier
6533 * to treat it as such than to figure out
6534 * some more elaborate course of action.
6536 broken_server = DNS_R_LAME;
6537 keep_trying = ISC_TRUE;
6540 goto force_referral;
6542 result = answer_response(fctx);
6543 if (result != ISC_R_SUCCESS) {
6544 if (result == DNS_R_FORMERR)
6545 keep_trying = ISC_TRUE;
6548 } else if (message->counts[DNS_SECTION_AUTHORITY] > 0 ||
6549 message->rcode == dns_rcode_noerror ||
6550 message->rcode == dns_rcode_nxdomain) {
6552 * NXDOMAIN, NXRDATASET, or referral.
6554 result = noanswer_response(fctx, NULL, ISC_FALSE);
6555 if (result == DNS_R_CHASEDSSERVERS) {
6556 } else if (result == DNS_R_DELEGATION) {
6559 * We don't have the answer, but we know a better
6562 get_nameservers = ISC_TRUE;
6563 keep_trying = ISC_TRUE;
6565 * We have a new set of name servers, and it
6566 * has not experienced any restarts yet.
6571 * Update local statistics counters collected for each
6575 fctx->querysent = 0;
6576 fctx->lamecount = 0;
6581 result = ISC_R_SUCCESS;
6582 } else if (result != ISC_R_SUCCESS) {
6584 * Something has gone wrong.
6586 if (result == DNS_R_FORMERR)
6587 keep_trying = ISC_TRUE;
6592 * The server is insane.
6595 broken_server = DNS_R_UNEXPECTEDRCODE;
6596 keep_trying = ISC_TRUE;
6601 * Follow additional section data chains.
6603 chase_additional(fctx);
6606 * Cache the cacheable parts of the message. This may also cause
6607 * work to be queued to the DNSSEC validator.
6609 if (WANTCACHE(fctx)) {
6610 result = cache_message(fctx, query->addrinfo, now);
6611 if (result != ISC_R_SUCCESS)
6616 * Ncache the negatively cacheable parts of the message. This may
6617 * also cause work to be queued to the DNSSEC validator.
6619 if (WANTNCACHE(fctx)) {
6620 dns_rdatatype_t covers;
6621 if (message->rcode == dns_rcode_nxdomain)
6622 covers = dns_rdatatype_any;
6624 covers = fctx->type;
6627 * Cache any negative cache entries in the message.
6629 result = ncache_message(fctx, query->addrinfo, covers, now);
6634 * Remember the query's addrinfo, in case we need to mark the
6637 addrinfo = query->addrinfo;
6642 * XXXRTH Don't cancel the query if waiting for validation?
6644 fctx_cancelquery(&query, &devent, finish, ISC_FALSE);
6647 if (result == DNS_R_FORMERR)
6648 broken_server = DNS_R_FORMERR;
6649 if (broken_server != ISC_R_SUCCESS) {
6651 * Add this server to the list of bad servers for
6654 add_bad(fctx, addrinfo, broken_server, broken_type);
6657 if (get_nameservers) {
6659 dns_fixedname_init(&foundname);
6660 fname = dns_fixedname_name(&foundname);
6661 if (result != ISC_R_SUCCESS) {
6662 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6666 if (dns_rdatatype_atparent(fctx->type))
6667 findoptions |= DNS_DBFIND_NOEXACT;
6668 if ((options & DNS_FETCHOPT_UNSHARED) == 0)
6671 name = &fctx->domain;
6672 result = dns_view_findzonecut(fctx->res->view,
6678 if (result != ISC_R_SUCCESS) {
6679 FCTXTRACE("couldn't find a zonecut");
6680 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6683 if (!dns_name_issubdomain(fname, &fctx->domain)) {
6685 * The best nameservers are now above our
6688 FCTXTRACE("nameservers now above QDOMAIN");
6689 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6692 dns_name_free(&fctx->domain,
6693 fctx->res->buckets[fctx->bucketnum].mctx);
6694 dns_name_init(&fctx->domain, NULL);
6695 result = dns_name_dup(fname,
6696 fctx->res->buckets[fctx->bucketnum].mctx,
6698 if (result != ISC_R_SUCCESS) {
6699 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6702 fctx_cancelqueries(fctx, ISC_TRUE);
6703 fctx_cleanupfinds(fctx);
6704 fctx_cleanupaltfinds(fctx);
6705 fctx_cleanupforwaddrs(fctx);
6706 fctx_cleanupaltaddrs(fctx);
6711 fctx_try(fctx, !get_nameservers, ISC_FALSE);
6712 } else if (resend) {
6714 * Resend (probably with changed options).
6716 FCTXTRACE("resend");
6717 inc_stats(fctx->res, dns_resstatscounter_retry);
6718 result = fctx_query(fctx, addrinfo, options);
6719 if (result != ISC_R_SUCCESS)
6720 fctx_done(fctx, result, __LINE__);
6721 } else if (result == ISC_R_SUCCESS && !HAVE_ANSWER(fctx)) {
6723 * All has gone well so far, but we are waiting for the
6724 * DNSSEC validator to validate the answer.
6726 FCTXTRACE("wait for validator");
6727 fctx_cancelqueries(fctx, ISC_TRUE);
6729 * We must not retransmit while the validator is working;
6730 * it has references to the current rmessage.
6732 result = fctx_stopidletimer(fctx);
6733 if (result != ISC_R_SUCCESS)
6734 fctx_done(fctx, result, __LINE__);
6735 } else if (result == DNS_R_CHASEDSSERVERS) {
6737 add_bad(fctx, addrinfo, result, broken_type);
6738 fctx_cancelqueries(fctx, ISC_TRUE);
6739 fctx_cleanupfinds(fctx);
6740 fctx_cleanupforwaddrs(fctx);
6742 n = dns_name_countlabels(&fctx->name);
6743 dns_name_getlabelsequence(&fctx->name, 1, n - 1, &fctx->nsname);
6745 FCTXTRACE("suspending DS lookup to find parent's NS records");
6747 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
6749 NULL, NULL, NULL, 0, task,
6750 resume_dslookup, fctx,
6751 &fctx->nsrrset, NULL,
6753 if (result != ISC_R_SUCCESS)
6754 fctx_done(fctx, result, __LINE__);
6755 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
6757 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
6758 result = fctx_stopidletimer(fctx);
6759 if (result != ISC_R_SUCCESS)
6760 fctx_done(fctx, result, __LINE__);
6765 fctx_done(fctx, result, __LINE__);
6771 *** Resolver Methods
6774 destroy_badcache(dns_resolver_t *res) {
6775 dns_badcache_t *bad, *next;
6778 if (res->badcache != NULL) {
6779 for (i = 0; i < res->badhash; i++)
6780 for (bad = res->badcache[i]; bad != NULL;
6783 isc_mem_put(res->mctx, bad, sizeof(*bad) +
6787 isc_mem_put(res->mctx, res->badcache,
6788 sizeof(*res->badcache) * res->badhash);
6789 res->badcache = NULL;
6791 INSIST(res->badcount == 0);
6796 destroy(dns_resolver_t *res) {
6800 REQUIRE(res->references == 0);
6801 REQUIRE(!res->priming);
6802 REQUIRE(res->primefetch == NULL);
6806 INSIST(res->nfctx == 0);
6808 DESTROYLOCK(&res->primelock);
6809 DESTROYLOCK(&res->nlock);
6810 DESTROYLOCK(&res->lock);
6811 for (i = 0; i < res->nbuckets; i++) {
6812 INSIST(ISC_LIST_EMPTY(res->buckets[i].fctxs));
6813 isc_task_shutdown(res->buckets[i].task);
6814 isc_task_detach(&res->buckets[i].task);
6815 DESTROYLOCK(&res->buckets[i].lock);
6816 isc_mem_detach(&res->buckets[i].mctx);
6818 isc_mem_put(res->mctx, res->buckets,
6819 res->nbuckets * sizeof(fctxbucket_t));
6820 if (res->dispatchv4 != NULL)
6821 dns_dispatch_detach(&res->dispatchv4);
6822 if (res->dispatchv6 != NULL)
6823 dns_dispatch_detach(&res->dispatchv6);
6824 while ((a = ISC_LIST_HEAD(res->alternates)) != NULL) {
6825 ISC_LIST_UNLINK(res->alternates, a, link);
6827 dns_name_free(&a->_u._n.name, res->mctx);
6828 isc_mem_put(res->mctx, a, sizeof(*a));
6830 dns_resolver_reset_algorithms(res);
6831 destroy_badcache(res);
6832 dns_resolver_resetmustbesecure(res);
6834 isc_rwlock_destroy(&res->alglock);
6837 isc_rwlock_destroy(&res->mbslock);
6839 isc_timer_detach(&res->spillattimer);
6841 isc_mem_put(res->mctx, res, sizeof(*res));
6845 send_shutdown_events(dns_resolver_t *res) {
6846 isc_event_t *event, *next_event;
6850 * Caller must be holding the resolver lock.
6853 for (event = ISC_LIST_HEAD(res->whenshutdown);
6855 event = next_event) {
6856 next_event = ISC_LIST_NEXT(event, ev_link);
6857 ISC_LIST_UNLINK(res->whenshutdown, event, ev_link);
6858 etask = event->ev_sender;
6859 event->ev_sender = res;
6860 isc_task_sendanddetach(&etask, &event);
6865 empty_bucket(dns_resolver_t *res) {
6866 RTRACE("empty_bucket");
6870 INSIST(res->activebuckets > 0);
6871 res->activebuckets--;
6872 if (res->activebuckets == 0)
6873 send_shutdown_events(res);
6879 spillattimer_countdown(isc_task_t *task, isc_event_t *event) {
6880 dns_resolver_t *res = event->ev_arg;
6881 isc_result_t result;
6883 isc_boolean_t logit = ISC_FALSE;
6885 REQUIRE(VALID_RESOLVER(res));
6890 INSIST(!res->exiting);
6891 if (res->spillat > res->spillatmin) {
6895 if (res->spillat <= res->spillatmin) {
6896 result = isc_timer_reset(res->spillattimer,
6897 isc_timertype_inactive, NULL,
6899 RUNTIME_CHECK(result == ISC_R_SUCCESS);
6901 count = res->spillat;
6904 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6905 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
6906 "clients-per-query decreased to %u", count);
6908 isc_event_free(&event);
6912 dns_resolver_create(dns_view_t *view,
6913 isc_taskmgr_t *taskmgr, unsigned int ntasks,
6914 isc_socketmgr_t *socketmgr,
6915 isc_timermgr_t *timermgr,
6916 unsigned int options,
6917 dns_dispatchmgr_t *dispatchmgr,
6918 dns_dispatch_t *dispatchv4,
6919 dns_dispatch_t *dispatchv6,
6920 dns_resolver_t **resp)
6922 dns_resolver_t *res;
6923 isc_result_t result = ISC_R_SUCCESS;
6924 unsigned int i, buckets_created = 0;
6925 isc_task_t *task = NULL;
6930 * Create a resolver.
6933 REQUIRE(DNS_VIEW_VALID(view));
6934 REQUIRE(ntasks > 0);
6935 REQUIRE(resp != NULL && *resp == NULL);
6936 REQUIRE(dispatchmgr != NULL);
6937 REQUIRE(dispatchv4 != NULL || dispatchv6 != NULL);
6939 res = isc_mem_get(view->mctx, sizeof(*res));
6941 return (ISC_R_NOMEMORY);
6943 res->mctx = view->mctx;
6944 res->rdclass = view->rdclass;
6945 res->socketmgr = socketmgr;
6946 res->timermgr = timermgr;
6947 res->taskmgr = taskmgr;
6948 res->dispatchmgr = dispatchmgr;
6950 res->options = options;
6952 ISC_LIST_INIT(res->alternates);
6953 res->udpsize = RECV_BUFFER_SIZE;
6954 res->algorithms = NULL;
6955 res->badcache = NULL;
6959 res->mustbesecure = NULL;
6960 res->spillatmin = res->spillat = 10;
6961 res->spillatmax = 100;
6962 res->spillattimer = NULL;
6963 res->zero_no_soa_ttl = ISC_FALSE;
6965 res->nextdisp = 0; /* meaningless at this point, but init it */
6966 res->nbuckets = ntasks;
6967 res->activebuckets = ntasks;
6968 res->buckets = isc_mem_get(view->mctx,
6969 ntasks * sizeof(fctxbucket_t));
6970 if (res->buckets == NULL) {
6971 result = ISC_R_NOMEMORY;
6974 for (i = 0; i < ntasks; i++) {
6975 result = isc_mutex_init(&res->buckets[i].lock);
6976 if (result != ISC_R_SUCCESS)
6977 goto cleanup_buckets;
6978 res->buckets[i].task = NULL;
6979 result = isc_task_create(taskmgr, 0, &res->buckets[i].task);
6980 if (result != ISC_R_SUCCESS) {
6981 DESTROYLOCK(&res->buckets[i].lock);
6982 goto cleanup_buckets;
6984 res->buckets[i].mctx = NULL;
6985 snprintf(name, sizeof(name), "res%u", i);
6986 #ifdef ISC_PLATFORM_USETHREADS
6988 * Use a separate memory context for each bucket to reduce
6989 * contention among multiple threads. Do this only when
6990 * enabling threads because it will be require more memory.
6992 result = isc_mem_create(0, 0, &res->buckets[i].mctx);
6993 if (result != ISC_R_SUCCESS) {
6994 isc_task_detach(&res->buckets[i].task);
6995 DESTROYLOCK(&res->buckets[i].lock);
6996 goto cleanup_buckets;
6998 isc_mem_setname(res->buckets[i].mctx, name, NULL);
7000 isc_mem_attach(view->mctx, &res->buckets[i].mctx);
7002 isc_task_setname(res->buckets[i].task, name, res);
7003 ISC_LIST_INIT(res->buckets[i].fctxs);
7004 res->buckets[i].exiting = ISC_FALSE;
7008 res->dispatchv4 = NULL;
7009 if (dispatchv4 != NULL) {
7010 dns_dispatch_attach(dispatchv4, &res->dispatchv4);
7011 dispattr = dns_dispatch_getattributes(dispatchv4);
7013 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
7016 res->dispatchv6 = NULL;
7017 if (dispatchv6 != NULL) {
7018 dns_dispatch_attach(dispatchv6, &res->dispatchv6);
7019 dispattr = dns_dispatch_getattributes(dispatchv6);
7021 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
7024 res->references = 1;
7025 res->exiting = ISC_FALSE;
7026 res->frozen = ISC_FALSE;
7027 ISC_LIST_INIT(res->whenshutdown);
7028 res->priming = ISC_FALSE;
7029 res->primefetch = NULL;
7032 result = isc_mutex_init(&res->lock);
7033 if (result != ISC_R_SUCCESS)
7034 goto cleanup_dispatches;
7036 result = isc_mutex_init(&res->nlock);
7037 if (result != ISC_R_SUCCESS)
7040 result = isc_mutex_init(&res->primelock);
7041 if (result != ISC_R_SUCCESS)
7045 result = isc_task_create(taskmgr, 0, &task);
7046 if (result != ISC_R_SUCCESS)
7047 goto cleanup_primelock;
7049 result = isc_timer_create(timermgr, isc_timertype_inactive, NULL, NULL,
7050 task, spillattimer_countdown, res,
7051 &res->spillattimer);
7052 isc_task_detach(&task);
7053 if (result != ISC_R_SUCCESS)
7054 goto cleanup_primelock;
7057 result = isc_rwlock_init(&res->alglock, 0, 0);
7058 if (result != ISC_R_SUCCESS)
7059 goto cleanup_spillattimer;
7062 result = isc_rwlock_init(&res->mbslock, 0, 0);
7063 if (result != ISC_R_SUCCESS)
7064 goto cleanup_alglock;
7067 res->magic = RES_MAGIC;
7071 return (ISC_R_SUCCESS);
7076 isc_rwlock_destroy(&res->alglock);
7079 #if USE_ALGLOCK || USE_MBSLOCK
7080 cleanup_spillattimer:
7081 isc_timer_detach(&res->spillattimer);
7085 DESTROYLOCK(&res->primelock);
7088 DESTROYLOCK(&res->nlock);
7091 DESTROYLOCK(&res->lock);
7094 if (res->dispatchv6 != NULL)
7095 dns_dispatch_detach(&res->dispatchv6);
7096 if (res->dispatchv4 != NULL)
7097 dns_dispatch_detach(&res->dispatchv4);
7100 for (i = 0; i < buckets_created; i++) {
7101 isc_mem_detach(&res->buckets[i].mctx);
7102 DESTROYLOCK(&res->buckets[i].lock);
7103 isc_task_shutdown(res->buckets[i].task);
7104 isc_task_detach(&res->buckets[i].task);
7106 isc_mem_put(view->mctx, res->buckets,
7107 res->nbuckets * sizeof(fctxbucket_t));
7110 isc_mem_put(view->mctx, res, sizeof(*res));
7116 prime_done(isc_task_t *task, isc_event_t *event) {
7117 dns_resolver_t *res;
7118 dns_fetchevent_t *fevent;
7120 dns_db_t *db = NULL;
7122 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
7123 fevent = (dns_fetchevent_t *)event;
7124 res = event->ev_arg;
7125 REQUIRE(VALID_RESOLVER(res));
7131 INSIST(res->priming);
7132 res->priming = ISC_FALSE;
7133 LOCK(&res->primelock);
7134 fetch = res->primefetch;
7135 res->primefetch = NULL;
7136 UNLOCK(&res->primelock);
7140 if (fevent->result == ISC_R_SUCCESS &&
7141 res->view->cache != NULL && res->view->hints != NULL) {
7142 dns_cache_attachdb(res->view->cache, &db);
7143 dns_root_checkhints(res->view, res->view->hints, db);
7147 if (fevent->node != NULL)
7148 dns_db_detachnode(fevent->db, &fevent->node);
7149 if (fevent->db != NULL)
7150 dns_db_detach(&fevent->db);
7151 if (dns_rdataset_isassociated(fevent->rdataset))
7152 dns_rdataset_disassociate(fevent->rdataset);
7153 INSIST(fevent->sigrdataset == NULL);
7155 isc_mem_put(res->mctx, fevent->rdataset, sizeof(*fevent->rdataset));
7157 isc_event_free(&event);
7158 dns_resolver_destroyfetch(&fetch);
7162 dns_resolver_prime(dns_resolver_t *res) {
7163 isc_boolean_t want_priming = ISC_FALSE;
7164 dns_rdataset_t *rdataset;
7165 isc_result_t result;
7167 REQUIRE(VALID_RESOLVER(res));
7168 REQUIRE(res->frozen);
7170 RTRACE("dns_resolver_prime");
7174 if (!res->exiting && !res->priming) {
7175 INSIST(res->primefetch == NULL);
7176 res->priming = ISC_TRUE;
7177 want_priming = ISC_TRUE;
7184 * To avoid any possible recursive locking problems, we
7185 * start the priming fetch like any other fetch, and holding
7186 * no resolver locks. No one else will try to start it
7187 * because we're the ones who set res->priming to true.
7188 * Any other callers of dns_resolver_prime() while we're
7189 * running will see that res->priming is already true and
7193 rdataset = isc_mem_get(res->mctx, sizeof(*rdataset));
7194 if (rdataset == NULL) {
7196 INSIST(res->priming);
7197 INSIST(res->primefetch == NULL);
7198 res->priming = ISC_FALSE;
7202 dns_rdataset_init(rdataset);
7203 LOCK(&res->primelock);
7204 result = dns_resolver_createfetch(res, dns_rootname,
7206 NULL, NULL, NULL, 0,
7207 res->buckets[0].task,
7209 res, rdataset, NULL,
7211 UNLOCK(&res->primelock);
7212 if (result != ISC_R_SUCCESS) {
7214 INSIST(res->priming);
7215 res->priming = ISC_FALSE;
7222 dns_resolver_freeze(dns_resolver_t *res) {
7228 REQUIRE(VALID_RESOLVER(res));
7229 REQUIRE(!res->frozen);
7231 res->frozen = ISC_TRUE;
7235 dns_resolver_attach(dns_resolver_t *source, dns_resolver_t **targetp) {
7236 REQUIRE(VALID_RESOLVER(source));
7237 REQUIRE(targetp != NULL && *targetp == NULL);
7239 RRTRACE(source, "attach");
7240 LOCK(&source->lock);
7241 REQUIRE(!source->exiting);
7243 INSIST(source->references > 0);
7244 source->references++;
7245 INSIST(source->references != 0);
7246 UNLOCK(&source->lock);
7252 dns_resolver_whenshutdown(dns_resolver_t *res, isc_task_t *task,
7253 isc_event_t **eventp)
7258 REQUIRE(VALID_RESOLVER(res));
7259 REQUIRE(eventp != NULL);
7266 if (res->exiting && res->activebuckets == 0) {
7268 * We're already shutdown. Send the event.
7270 event->ev_sender = res;
7271 isc_task_send(task, &event);
7274 isc_task_attach(task, &clone);
7275 event->ev_sender = clone;
7276 ISC_LIST_APPEND(res->whenshutdown, event, ev_link);
7283 dns_resolver_shutdown(dns_resolver_t *res) {
7287 isc_result_t result;
7289 REQUIRE(VALID_RESOLVER(res));
7295 if (!res->exiting) {
7297 res->exiting = ISC_TRUE;
7299 for (i = 0; i < res->nbuckets; i++) {
7300 LOCK(&res->buckets[i].lock);
7301 for (fctx = ISC_LIST_HEAD(res->buckets[i].fctxs);
7303 fctx = ISC_LIST_NEXT(fctx, link))
7304 fctx_shutdown(fctx);
7305 if (res->dispatchv4 != NULL && !res->exclusivev4) {
7306 sock = dns_dispatch_getsocket(res->dispatchv4);
7307 isc_socket_cancel(sock, res->buckets[i].task,
7308 ISC_SOCKCANCEL_ALL);
7310 if (res->dispatchv6 != NULL && !res->exclusivev6) {
7311 sock = dns_dispatch_getsocket(res->dispatchv6);
7312 isc_socket_cancel(sock, res->buckets[i].task,
7313 ISC_SOCKCANCEL_ALL);
7315 res->buckets[i].exiting = ISC_TRUE;
7316 if (ISC_LIST_EMPTY(res->buckets[i].fctxs)) {
7317 INSIST(res->activebuckets > 0);
7318 res->activebuckets--;
7320 UNLOCK(&res->buckets[i].lock);
7322 if (res->activebuckets == 0)
7323 send_shutdown_events(res);
7324 result = isc_timer_reset(res->spillattimer,
7325 isc_timertype_inactive, NULL,
7327 RUNTIME_CHECK(result == ISC_R_SUCCESS);
7334 dns_resolver_detach(dns_resolver_t **resp) {
7335 dns_resolver_t *res;
7336 isc_boolean_t need_destroy = ISC_FALSE;
7338 REQUIRE(resp != NULL);
7340 REQUIRE(VALID_RESOLVER(res));
7346 INSIST(res->references > 0);
7348 if (res->references == 0) {
7349 INSIST(res->exiting && res->activebuckets == 0);
7350 need_destroy = ISC_TRUE;
7361 static inline isc_boolean_t
7362 fctx_match(fetchctx_t *fctx, dns_name_t *name, dns_rdatatype_t type,
7363 unsigned int options)
7365 if (fctx->type != type || fctx->options != options)
7367 return (dns_name_equal(&fctx->name, name));
7371 log_fetch(dns_name_t *name, dns_rdatatype_t type) {
7372 char namebuf[DNS_NAME_FORMATSIZE];
7373 char typebuf[DNS_RDATATYPE_FORMATSIZE];
7374 int level = ISC_LOG_DEBUG(1);
7376 if (! isc_log_wouldlog(dns_lctx, level))
7379 dns_name_format(name, namebuf, sizeof(namebuf));
7380 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
7382 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7383 DNS_LOGMODULE_RESOLVER, level,
7384 "createfetch: %s %s", namebuf, typebuf);
7388 dns_resolver_createfetch(dns_resolver_t *res, dns_name_t *name,
7389 dns_rdatatype_t type,
7390 dns_name_t *domain, dns_rdataset_t *nameservers,
7391 dns_forwarders_t *forwarders,
7392 unsigned int options, isc_task_t *task,
7393 isc_taskaction_t action, void *arg,
7394 dns_rdataset_t *rdataset,
7395 dns_rdataset_t *sigrdataset,
7396 dns_fetch_t **fetchp)
7398 return (dns_resolver_createfetch2(res, name, type, domain,
7399 nameservers, forwarders, NULL, 0,
7400 options, task, action, arg,
7401 rdataset, sigrdataset, fetchp));
7405 dns_resolver_createfetch2(dns_resolver_t *res, dns_name_t *name,
7406 dns_rdatatype_t type,
7407 dns_name_t *domain, dns_rdataset_t *nameservers,
7408 dns_forwarders_t *forwarders,
7409 isc_sockaddr_t *client, dns_messageid_t id,
7410 unsigned int options, isc_task_t *task,
7411 isc_taskaction_t action, void *arg,
7412 dns_rdataset_t *rdataset,
7413 dns_rdataset_t *sigrdataset,
7414 dns_fetch_t **fetchp)
7417 fetchctx_t *fctx = NULL;
7418 isc_result_t result = ISC_R_SUCCESS;
7419 unsigned int bucketnum;
7420 isc_boolean_t new_fctx = ISC_FALSE;
7422 unsigned int count = 0;
7423 unsigned int spillat;
7424 unsigned int spillatmin;
7428 REQUIRE(VALID_RESOLVER(res));
7429 REQUIRE(res->frozen);
7430 /* XXXRTH Check for meta type */
7431 if (domain != NULL) {
7432 REQUIRE(DNS_RDATASET_VALID(nameservers));
7433 REQUIRE(nameservers->type == dns_rdatatype_ns);
7435 REQUIRE(nameservers == NULL);
7436 REQUIRE(forwarders == NULL);
7437 REQUIRE(!dns_rdataset_isassociated(rdataset));
7438 REQUIRE(sigrdataset == NULL ||
7439 !dns_rdataset_isassociated(sigrdataset));
7440 REQUIRE(fetchp != NULL && *fetchp == NULL);
7442 log_fetch(name, type);
7445 * XXXRTH use a mempool?
7447 fetch = isc_mem_get(res->mctx, sizeof(*fetch));
7449 return (ISC_R_NOMEMORY);
7451 bucketnum = dns_name_fullhash(name, ISC_FALSE) % res->nbuckets;
7454 spillat = res->spillat;
7455 spillatmin = res->spillatmin;
7457 LOCK(&res->buckets[bucketnum].lock);
7459 if (res->buckets[bucketnum].exiting) {
7460 result = ISC_R_SHUTTINGDOWN;
7464 if ((options & DNS_FETCHOPT_UNSHARED) == 0) {
7465 for (fctx = ISC_LIST_HEAD(res->buckets[bucketnum].fctxs);
7467 fctx = ISC_LIST_NEXT(fctx, link)) {
7468 if (fctx_match(fctx, name, type, options))
7474 * Is this a duplicate?
7476 if (fctx != NULL && client != NULL) {
7477 dns_fetchevent_t *fevent;
7478 for (fevent = ISC_LIST_HEAD(fctx->events);
7480 fevent = ISC_LIST_NEXT(fevent, ev_link)) {
7481 if (fevent->client != NULL && fevent->id == id &&
7482 isc_sockaddr_equal(fevent->client, client)) {
7483 result = DNS_R_DUPLICATE;
7489 if (count >= spillatmin && spillatmin != 0) {
7490 INSIST(fctx != NULL);
7491 if (count >= spillat)
7492 fctx->spilled = ISC_TRUE;
7493 if (fctx->spilled) {
7494 result = DNS_R_DROP;
7500 * If we didn't have a fetch, would attach to a done fetch, this
7501 * fetch has already cloned its results, or if the fetch has gone
7502 * "idle" (no one was interested in it), we need to start a new
7503 * fetch instead of joining with the existing one.
7506 fctx->state == fetchstate_done ||
7508 ISC_LIST_EMPTY(fctx->events)) {
7510 result = fctx_create(res, name, type, domain, nameservers,
7511 options, bucketnum, &fctx);
7512 if (result != ISC_R_SUCCESS)
7514 new_fctx = ISC_TRUE;
7517 result = fctx_join(fctx, task, client, id, action, arg,
7518 rdataset, sigrdataset, fetch);
7520 if (result == ISC_R_SUCCESS) {
7524 event = &fctx->control_event;
7525 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
7526 DNS_EVENT_FETCHCONTROL,
7527 fctx_start, fctx, NULL,
7529 isc_task_send(res->buckets[bucketnum].task, &event);
7532 * We don't care about the result of fctx_destroy()
7533 * since we know we're not exiting.
7535 (void)fctx_destroy(fctx);
7540 UNLOCK(&res->buckets[bucketnum].lock);
7542 if (result == ISC_R_SUCCESS) {
7546 isc_mem_put(res->mctx, fetch, sizeof(*fetch));
7552 dns_resolver_cancelfetch(dns_fetch_t *fetch) {
7554 dns_resolver_t *res;
7555 dns_fetchevent_t *event, *next_event;
7558 REQUIRE(DNS_FETCH_VALID(fetch));
7559 fctx = fetch->private;
7560 REQUIRE(VALID_FCTX(fctx));
7563 FTRACE("cancelfetch");
7565 LOCK(&res->buckets[fctx->bucketnum].lock);
7568 * Find the completion event for this fetch (as opposed
7569 * to those for other fetches that have joined the same
7570 * fctx) and send it with result = ISC_R_CANCELED.
7573 if (fctx->state != fetchstate_done) {
7574 for (event = ISC_LIST_HEAD(fctx->events);
7576 event = next_event) {
7577 next_event = ISC_LIST_NEXT(event, ev_link);
7578 if (event->fetch == fetch) {
7579 ISC_LIST_UNLINK(fctx->events, event, ev_link);
7584 if (event != NULL) {
7585 etask = event->ev_sender;
7586 event->ev_sender = fctx;
7587 event->result = ISC_R_CANCELED;
7588 isc_task_sendanddetach(&etask, ISC_EVENT_PTR(&event));
7591 * The fctx continues running even if no fetches remain;
7592 * the answer is still cached.
7595 UNLOCK(&res->buckets[fctx->bucketnum].lock);
7599 dns_resolver_destroyfetch(dns_fetch_t **fetchp) {
7601 dns_resolver_t *res;
7602 dns_fetchevent_t *event, *next_event;
7604 unsigned int bucketnum;
7605 isc_boolean_t bucket_empty = ISC_FALSE;
7607 REQUIRE(fetchp != NULL);
7609 REQUIRE(DNS_FETCH_VALID(fetch));
7610 fctx = fetch->private;
7611 REQUIRE(VALID_FCTX(fctx));
7614 FTRACE("destroyfetch");
7616 bucketnum = fctx->bucketnum;
7617 LOCK(&res->buckets[bucketnum].lock);
7620 * Sanity check: the caller should have gotten its event before
7621 * trying to destroy the fetch.
7624 if (fctx->state != fetchstate_done) {
7625 for (event = ISC_LIST_HEAD(fctx->events);
7627 event = next_event) {
7628 next_event = ISC_LIST_NEXT(event, ev_link);
7629 RUNTIME_CHECK(event->fetch != fetch);
7633 INSIST(fctx->references > 0);
7635 if (fctx->references == 0) {
7637 * No one cares about the result of this fetch anymore.
7639 if (fctx->pending == 0 && fctx->nqueries == 0 &&
7640 ISC_LIST_EMPTY(fctx->validators) &&
7641 SHUTTINGDOWN(fctx)) {
7643 * This fctx is already shutdown; we were just
7644 * waiting for the last reference to go away.
7646 bucket_empty = fctx_destroy(fctx);
7649 * Initiate shutdown.
7651 fctx_shutdown(fctx);
7655 UNLOCK(&res->buckets[bucketnum].lock);
7657 isc_mem_put(res->mctx, fetch, sizeof(*fetch));
7665 dns_resolver_logfetch(dns_fetch_t *fetch, isc_log_t *lctx,
7666 isc_logcategory_t *category, isc_logmodule_t *module,
7667 int level, isc_boolean_t duplicateok)
7670 dns_resolver_t *res;
7671 char domainbuf[DNS_NAME_FORMATSIZE];
7673 REQUIRE(DNS_FETCH_VALID(fetch));
7674 fctx = fetch->private;
7675 REQUIRE(VALID_FCTX(fctx));
7678 LOCK(&res->buckets[fctx->bucketnum].lock);
7680 INSIST(fctx->exitline >= 0);
7681 if (!fctx->logged || duplicateok) {
7682 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
7683 isc_log_write(lctx, category, module, level,
7684 "fetch completed at %s:%d for %s in "
7685 "%" ISC_PRINT_QUADFORMAT "u."
7686 "%06" ISC_PRINT_QUADFORMAT "u: %s/%s "
7687 "[domain:%s,referral:%u,restart:%u,qrysent:%u,"
7688 "timeout:%u,lame:%u,neterr:%u,badresp:%u,"
7689 "adberr:%u,findfail:%u,valfail:%u]",
7690 __FILE__, fctx->exitline, fctx->info,
7691 fctx->duration / 1000000,
7692 fctx->duration % 1000000,
7693 isc_result_totext(fctx->result),
7694 isc_result_totext(fctx->vresult), domainbuf,
7695 fctx->referrals, fctx->restarts,
7696 fctx->querysent, fctx->timeouts, fctx->lamecount,
7697 fctx->neterr, fctx->badresp, fctx->adberr,
7698 fctx->findfail, fctx->valfail);
7699 fctx->logged = ISC_TRUE;
7702 UNLOCK(&res->buckets[fctx->bucketnum].lock);
7706 dns_resolver_dispatchmgr(dns_resolver_t *resolver) {
7707 REQUIRE(VALID_RESOLVER(resolver));
7708 return (resolver->dispatchmgr);
7712 dns_resolver_dispatchv4(dns_resolver_t *resolver) {
7713 REQUIRE(VALID_RESOLVER(resolver));
7714 return (resolver->dispatchv4);
7718 dns_resolver_dispatchv6(dns_resolver_t *resolver) {
7719 REQUIRE(VALID_RESOLVER(resolver));
7720 return (resolver->dispatchv6);
7724 dns_resolver_socketmgr(dns_resolver_t *resolver) {
7725 REQUIRE(VALID_RESOLVER(resolver));
7726 return (resolver->socketmgr);
7730 dns_resolver_taskmgr(dns_resolver_t *resolver) {
7731 REQUIRE(VALID_RESOLVER(resolver));
7732 return (resolver->taskmgr);
7736 dns_resolver_getlamettl(dns_resolver_t *resolver) {
7737 REQUIRE(VALID_RESOLVER(resolver));
7738 return (resolver->lame_ttl);
7742 dns_resolver_setlamettl(dns_resolver_t *resolver, isc_uint32_t lame_ttl) {
7743 REQUIRE(VALID_RESOLVER(resolver));
7744 resolver->lame_ttl = lame_ttl;
7748 dns_resolver_nrunning(dns_resolver_t *resolver) {
7750 LOCK(&resolver->nlock);
7751 n = resolver->nfctx;
7752 UNLOCK(&resolver->nlock);
7757 dns_resolver_addalternate(dns_resolver_t *resolver, isc_sockaddr_t *alt,
7758 dns_name_t *name, in_port_t port) {
7760 isc_result_t result;
7762 REQUIRE(VALID_RESOLVER(resolver));
7763 REQUIRE(!resolver->frozen);
7764 REQUIRE((alt == NULL) ^ (name == NULL));
7766 a = isc_mem_get(resolver->mctx, sizeof(*a));
7768 return (ISC_R_NOMEMORY);
7770 a->isaddress = ISC_TRUE;
7773 a->isaddress = ISC_FALSE;
7774 a->_u._n.port = port;
7775 dns_name_init(&a->_u._n.name, NULL);
7776 result = dns_name_dup(name, resolver->mctx, &a->_u._n.name);
7777 if (result != ISC_R_SUCCESS) {
7778 isc_mem_put(resolver->mctx, a, sizeof(*a));
7782 ISC_LINK_INIT(a, link);
7783 ISC_LIST_APPEND(resolver->alternates, a, link);
7785 return (ISC_R_SUCCESS);
7789 dns_resolver_setudpsize(dns_resolver_t *resolver, isc_uint16_t udpsize) {
7790 REQUIRE(VALID_RESOLVER(resolver));
7791 resolver->udpsize = udpsize;
7795 dns_resolver_getudpsize(dns_resolver_t *resolver) {
7796 REQUIRE(VALID_RESOLVER(resolver));
7797 return (resolver->udpsize);
7801 dns_resolver_flushbadcache(dns_resolver_t *resolver, dns_name_t *name) {
7803 dns_badcache_t *bad, *prev, *next;
7805 REQUIRE(VALID_RESOLVER(resolver));
7807 LOCK(&resolver->lock);
7808 if (resolver->badcache == NULL)
7813 isc_result_t result;
7814 result = isc_time_now(&now);
7815 if (result != ISC_R_SUCCESS)
7816 isc_time_settoepoch(&now);
7817 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
7819 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
7822 n = isc_time_compare(&bad->expire, &now);
7823 if (n < 0 || dns_name_equal(name, &bad->name)) {
7825 resolver->badcache[i] = bad->next;
7827 prev->next = bad->next;
7828 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
7830 resolver->badcount--;
7835 destroy_badcache(resolver);
7838 UNLOCK(&resolver->lock);
7843 resizehash(dns_resolver_t *resolver, isc_time_t *now, isc_boolean_t grow) {
7844 unsigned int newsize;
7845 dns_badcache_t **new, *bad, *next;
7849 newsize = resolver->badhash * 2 + 1;
7851 newsize = (resolver->badhash - 1) / 2;
7853 new = isc_mem_get(resolver->mctx,
7854 sizeof(*resolver->badcache) * newsize);
7857 memset(new, 0, sizeof(*resolver->badcache) * newsize);
7858 for (i = 0; i < resolver->badhash; i++) {
7859 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
7861 if (isc_time_compare(&bad->expire, now) < 0) {
7862 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
7864 resolver->badcount--;
7866 bad->next = new[bad->hashval % newsize];
7867 new[bad->hashval % newsize] = bad;
7871 isc_mem_put(resolver->mctx, resolver->badcache,
7872 sizeof(*resolver->badcache) * resolver->badhash);
7873 resolver->badhash = newsize;
7874 resolver->badcache = new;
7878 dns_resolver_addbadcache(dns_resolver_t *resolver, dns_name_t *name,
7879 dns_rdatatype_t type, isc_time_t *expire)
7882 isc_result_t result = ISC_R_SUCCESS;
7883 unsigned int i, hashval;
7884 dns_badcache_t *bad, *prev, *next;
7886 REQUIRE(VALID_RESOLVER(resolver));
7888 LOCK(&resolver->lock);
7889 if (resolver->badcache == NULL) {
7890 resolver->badcache = isc_mem_get(resolver->mctx,
7891 sizeof(*resolver->badcache) *
7893 if (resolver->badcache == NULL) {
7894 result = ISC_R_NOMEMORY;
7897 resolver->badhash = DNS_BADCACHE_SIZE;
7898 memset(resolver->badcache, 0, sizeof(*resolver->badcache) *
7902 result = isc_time_now(&now);
7903 if (result != ISC_R_SUCCESS)
7904 isc_time_settoepoch(&now);
7905 hashval = dns_name_hash(name, ISC_FALSE);
7906 i = hashval % resolver->badhash;
7908 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
7910 if (bad->type == type && dns_name_equal(name, &bad->name))
7912 if (isc_time_compare(&bad->expire, &now) < 0) {
7914 resolver->badcache[i] = bad->next;
7916 prev->next = bad->next;
7917 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
7919 resolver->badcount--;
7924 isc_buffer_t buffer;
7925 bad = isc_mem_get(resolver->mctx, sizeof(*bad) + name->length);
7927 result = ISC_R_NOMEMORY;
7931 bad->hashval = hashval;
7932 isc_buffer_init(&buffer, bad + 1, name->length);
7933 dns_name_init(&bad->name, NULL);
7934 dns_name_copy(name, &bad->name, &buffer);
7935 bad->next = resolver->badcache[i];
7936 resolver->badcache[i] = bad;
7937 resolver->badcount++;
7938 if (resolver->badcount > resolver->badhash * 8)
7939 resizehash(resolver, &now, ISC_TRUE);
7940 if (resolver->badcount < resolver->badhash * 2 &&
7941 resolver->badhash > DNS_BADCACHE_SIZE)
7942 resizehash(resolver, &now, ISC_FALSE);
7944 bad->expire = *expire;
7946 UNLOCK(&resolver->lock);
7950 dns_resolver_getbadcache(dns_resolver_t *resolver, dns_name_t *name,
7951 dns_rdatatype_t type, isc_time_t *now)
7953 dns_badcache_t *bad, *prev, *next;
7954 isc_boolean_t answer = ISC_FALSE;
7957 REQUIRE(VALID_RESOLVER(resolver));
7959 LOCK(&resolver->lock);
7960 if (resolver->badcache == NULL)
7963 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
7965 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
7968 * Search the hash list. Clean out expired records as we go.
7970 if (isc_time_compare(&bad->expire, now) < 0) {
7972 prev->next = bad->next;
7974 resolver->badcache[i] = bad->next;
7975 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
7977 resolver->badcount--;
7980 if (bad->type == type && dns_name_equal(name, &bad->name)) {
7988 * Slow sweep to clean out stale records.
7990 i = resolver->badsweep++ % resolver->badhash;
7991 bad = resolver->badcache[i];
7992 if (bad != NULL && isc_time_compare(&bad->expire, now) < 0) {
7993 resolver->badcache[i] = bad->next;
7994 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
7996 resolver->badcount--;
8000 UNLOCK(&resolver->lock);
8005 dns_resolver_printbadcache(dns_resolver_t *resolver, FILE *fp) {
8006 char namebuf[DNS_NAME_FORMATSIZE];
8007 char typebuf[DNS_RDATATYPE_FORMATSIZE];
8008 dns_badcache_t *bad, *next, *prev;
8013 LOCK(&resolver->lock);
8014 fprintf(fp, ";\n; Bad cache\n;\n");
8016 if (resolver->badcache == NULL)
8020 for (i = 0; i < resolver->badhash; i++) {
8022 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8024 if (isc_time_compare(&bad->expire, &now) < 0) {
8026 prev->next = bad->next;
8028 resolver->badcache[i] = bad->next;
8029 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8031 resolver->badcount--;
8035 dns_name_format(&bad->name, namebuf, sizeof(namebuf));
8036 dns_rdatatype_format(bad->type, typebuf,
8038 t = isc_time_microdiff(&bad->expire, &now);
8040 fprintf(fp, "; %s/%s [ttl "
8041 "%" ISC_PLATFORM_QUADFORMAT "u]\n",
8042 namebuf, typebuf, t);
8047 UNLOCK(&resolver->lock);
8051 free_algorithm(void *node, void *arg) {
8052 unsigned char *algorithms = node;
8053 isc_mem_t *mctx = arg;
8055 isc_mem_put(mctx, algorithms, *algorithms);
8059 dns_resolver_reset_algorithms(dns_resolver_t *resolver) {
8061 REQUIRE(VALID_RESOLVER(resolver));
8064 RWLOCK(&resolver->alglock, isc_rwlocktype_write);
8066 if (resolver->algorithms != NULL)
8067 dns_rbt_destroy(&resolver->algorithms);
8069 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
8074 dns_resolver_disable_algorithm(dns_resolver_t *resolver, dns_name_t *name,
8077 unsigned int len, mask;
8079 unsigned char *algorithms;
8080 isc_result_t result;
8081 dns_rbtnode_t *node = NULL;
8083 REQUIRE(VALID_RESOLVER(resolver));
8085 return (ISC_R_RANGE);
8088 RWLOCK(&resolver->alglock, isc_rwlocktype_write);
8090 if (resolver->algorithms == NULL) {
8091 result = dns_rbt_create(resolver->mctx, free_algorithm,
8092 resolver->mctx, &resolver->algorithms);
8093 if (result != ISC_R_SUCCESS)
8098 mask = 1 << (alg%8);
8100 result = dns_rbt_addnode(resolver->algorithms, name, &node);
8102 if (result == ISC_R_SUCCESS || result == ISC_R_EXISTS) {
8103 algorithms = node->data;
8104 if (algorithms == NULL || len > *algorithms) {
8105 new = isc_mem_get(resolver->mctx, len);
8107 result = ISC_R_NOMEMORY;
8110 memset(new, 0, len);
8111 if (algorithms != NULL)
8112 memcpy(new, algorithms, *algorithms);
8116 if (algorithms != NULL)
8117 isc_mem_put(resolver->mctx, algorithms,
8120 algorithms[len-1] |= mask;
8122 result = ISC_R_SUCCESS;
8125 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
8131 dns_resolver_algorithm_supported(dns_resolver_t *resolver, dns_name_t *name,
8134 unsigned int len, mask;
8135 unsigned char *algorithms;
8137 isc_result_t result;
8138 isc_boolean_t found = ISC_FALSE;
8140 REQUIRE(VALID_RESOLVER(resolver));
8143 RWLOCK(&resolver->alglock, isc_rwlocktype_read);
8145 if (resolver->algorithms == NULL)
8147 result = dns_rbt_findname(resolver->algorithms, name, 0, NULL, &data);
8148 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
8150 mask = 1 << (alg%8);
8152 if (len <= *algorithms && (algorithms[len-1] & mask) != 0)
8157 RWUNLOCK(&resolver->alglock, isc_rwlocktype_read);
8161 return (dst_algorithm_supported(alg));
8165 dns_resolver_digest_supported(dns_resolver_t *resolver, unsigned int digest) {
8168 return (dns_ds_digest_supported(digest));
8172 dns_resolver_resetmustbesecure(dns_resolver_t *resolver) {
8174 REQUIRE(VALID_RESOLVER(resolver));
8177 RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
8179 if (resolver->mustbesecure != NULL)
8180 dns_rbt_destroy(&resolver->mustbesecure);
8182 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
8186 static isc_boolean_t yes = ISC_TRUE, no = ISC_FALSE;
8189 dns_resolver_setmustbesecure(dns_resolver_t *resolver, dns_name_t *name,
8190 isc_boolean_t value)
8192 isc_result_t result;
8194 REQUIRE(VALID_RESOLVER(resolver));
8197 RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
8199 if (resolver->mustbesecure == NULL) {
8200 result = dns_rbt_create(resolver->mctx, NULL, NULL,
8201 &resolver->mustbesecure);
8202 if (result != ISC_R_SUCCESS)
8205 result = dns_rbt_addname(resolver->mustbesecure, name,
8206 value ? &yes : &no);
8209 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
8215 dns_resolver_getmustbesecure(dns_resolver_t *resolver, dns_name_t *name) {
8217 isc_boolean_t value = ISC_FALSE;
8218 isc_result_t result;
8220 REQUIRE(VALID_RESOLVER(resolver));
8223 RWLOCK(&resolver->mbslock, isc_rwlocktype_read);
8225 if (resolver->mustbesecure == NULL)
8227 result = dns_rbt_findname(resolver->mustbesecure, name, 0, NULL, &data);
8228 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
8229 value = *(isc_boolean_t*)data;
8232 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_read);
8238 dns_resolver_getclientsperquery(dns_resolver_t *resolver, isc_uint32_t *cur,
8239 isc_uint32_t *min, isc_uint32_t *max)
8241 REQUIRE(VALID_RESOLVER(resolver));
8243 LOCK(&resolver->lock);
8245 *cur = resolver->spillat;
8247 *min = resolver->spillatmin;
8249 *max = resolver->spillatmax;
8250 UNLOCK(&resolver->lock);
8254 dns_resolver_setclientsperquery(dns_resolver_t *resolver, isc_uint32_t min,
8257 REQUIRE(VALID_RESOLVER(resolver));
8259 LOCK(&resolver->lock);
8260 resolver->spillatmin = resolver->spillat = min;
8261 resolver->spillatmax = max;
8262 UNLOCK(&resolver->lock);
8266 dns_resolver_getzeronosoattl(dns_resolver_t *resolver) {
8267 REQUIRE(VALID_RESOLVER(resolver));
8269 return (resolver->zero_no_soa_ttl);
8273 dns_resolver_setzeronosoattl(dns_resolver_t *resolver, isc_boolean_t state) {
8274 REQUIRE(VALID_RESOLVER(resolver));
8276 resolver->zero_no_soa_ttl = state;
8280 dns_resolver_getoptions(dns_resolver_t *resolver) {
8281 REQUIRE(VALID_RESOLVER(resolver));
8283 return (resolver->options);