2 * Copyright (C) 2004-2011 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.32 2011-06-09 00:16:36 each 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 isc_sockaddrlist_t bad_edns;
207 dns_validator_t *validator;
208 ISC_LIST(dns_validator_t) validators;
213 * The number of events we're waiting for.
215 unsigned int pending;
218 * The number of times we've "restarted" the current
219 * nameserver set. This acts as a failsafe to prevent
220 * us from pounding constantly on a particular set of
221 * servers that, for whatever reason, are not giving
222 * us useful responses, but are responding in such a
223 * way that they are not marked "bad".
225 unsigned int restarts;
228 * The number of timeouts that have occurred since we
229 * last successfully received a response packet. This
230 * is used for EDNS0 black hole detection.
232 unsigned int timeouts;
235 * Look aside state for DS lookups.
238 dns_fetch_t * nsfetch;
239 dns_rdataset_t nsrrset;
242 * Number of queries that reference this context.
244 unsigned int nqueries;
247 * The reason to print when logging a successful
248 * response to a query.
253 * Random numbers to use for mixing up server addresses.
255 isc_uint32_t rand_buf;
256 isc_uint32_t rand_bits;
259 * Fetch-local statistics for detailed logging.
261 isc_result_t result; /*%< fetch result */
262 isc_result_t vresult; /*%< validation result */
265 isc_uint64_t duration;
266 isc_boolean_t logged;
267 unsigned int querysent;
268 unsigned int referrals;
269 unsigned int lamecount;
271 unsigned int badresp;
273 unsigned int findfail;
274 unsigned int valfail;
275 isc_boolean_t timeout;
278 #define FCTX_MAGIC ISC_MAGIC('F', '!', '!', '!')
279 #define VALID_FCTX(fctx) ISC_MAGIC_VALID(fctx, FCTX_MAGIC)
281 #define FCTX_ATTR_HAVEANSWER 0x0001
282 #define FCTX_ATTR_GLUING 0x0002
283 #define FCTX_ATTR_ADDRWAIT 0x0004
284 #define FCTX_ATTR_SHUTTINGDOWN 0x0008
285 #define FCTX_ATTR_WANTCACHE 0x0010
286 #define FCTX_ATTR_WANTNCACHE 0x0020
287 #define FCTX_ATTR_NEEDEDNS0 0x0040
288 #define FCTX_ATTR_TRIEDFIND 0x0080
289 #define FCTX_ATTR_TRIEDALT 0x0100
291 #define HAVE_ANSWER(f) (((f)->attributes & FCTX_ATTR_HAVEANSWER) != \
293 #define GLUING(f) (((f)->attributes & FCTX_ATTR_GLUING) != \
295 #define ADDRWAIT(f) (((f)->attributes & FCTX_ATTR_ADDRWAIT) != \
297 #define SHUTTINGDOWN(f) (((f)->attributes & FCTX_ATTR_SHUTTINGDOWN) \
299 #define WANTCACHE(f) (((f)->attributes & FCTX_ATTR_WANTCACHE) != 0)
300 #define WANTNCACHE(f) (((f)->attributes & FCTX_ATTR_WANTNCACHE) != 0)
301 #define NEEDEDNS0(f) (((f)->attributes & FCTX_ATTR_NEEDEDNS0) != 0)
302 #define TRIEDFIND(f) (((f)->attributes & FCTX_ATTR_TRIEDFIND) != 0)
303 #define TRIEDALT(f) (((f)->attributes & FCTX_ATTR_TRIEDALT) != 0)
306 dns_adbaddrinfo_t * addrinfo;
312 fetchctx_t * private;
315 #define DNS_FETCH_MAGIC ISC_MAGIC('F', 't', 'c', 'h')
316 #define DNS_FETCH_VALID(fetch) ISC_MAGIC_VALID(fetch, DNS_FETCH_MAGIC)
318 typedef struct fctxbucket {
321 ISC_LIST(fetchctx_t) fctxs;
322 isc_boolean_t exiting;
326 typedef struct alternate {
327 isc_boolean_t isaddress;
335 ISC_LINK(struct alternate) link;
338 typedef struct dns_badcache dns_badcache_t;
339 struct dns_badcache {
340 dns_badcache_t * next;
341 dns_rdatatype_t type;
343 unsigned int hashval;
346 #define DNS_BADCACHE_SIZE 1021
347 #define DNS_BADCACHE_TTL(fctx) \
348 (((fctx)->res->lame_ttl > 30 ) ? (fctx)->res->lame_ttl : 30)
350 struct dns_resolver {
356 isc_mutex_t primelock;
357 dns_rdataclass_t rdclass;
358 isc_socketmgr_t * socketmgr;
359 isc_timermgr_t * timermgr;
360 isc_taskmgr_t * taskmgr;
362 isc_boolean_t frozen;
363 unsigned int options;
364 dns_dispatchmgr_t * dispatchmgr;
365 dns_dispatch_t * dispatchv4;
366 isc_boolean_t exclusivev4;
367 dns_dispatch_t * dispatchv6;
368 isc_boolean_t exclusivev6;
370 unsigned int nbuckets;
371 fctxbucket_t * buckets;
372 isc_uint32_t lame_ttl;
373 ISC_LIST(alternate_t) alternates;
374 isc_uint16_t udpsize;
376 isc_rwlock_t alglock;
378 dns_rbt_t * algorithms;
380 isc_rwlock_t mbslock;
382 dns_rbt_t * mustbesecure;
383 unsigned int spillatmax;
384 unsigned int spillatmin;
385 isc_timer_t * spillattimer;
386 isc_boolean_t zero_no_soa_ttl;
388 /* Locked by lock. */
389 unsigned int references;
390 isc_boolean_t exiting;
391 isc_eventlist_t whenshutdown;
392 unsigned int activebuckets;
393 isc_boolean_t priming;
394 unsigned int spillat; /* clients-per-query */
395 unsigned int nextdisp;
398 dns_badcache_t ** badcache;
399 unsigned int badcount;
400 unsigned int badhash;
401 unsigned int badsweep;
403 /* Locked by primelock. */
404 dns_fetch_t * primefetch;
405 /* Locked by nlock. */
409 #define RES_MAGIC ISC_MAGIC('R', 'e', 's', '!')
410 #define VALID_RESOLVER(res) ISC_MAGIC_VALID(res, RES_MAGIC)
413 * Private addrinfo flags. These must not conflict with DNS_FETCHOPT_NOEDNS0,
414 * which we also use as an addrinfo flag.
416 #define FCTX_ADDRINFO_MARK 0x0001
417 #define FCTX_ADDRINFO_FORWARDER 0x1000
418 #define FCTX_ADDRINFO_TRIED 0x2000
419 #define UNMARKED(a) (((a)->flags & FCTX_ADDRINFO_MARK) \
421 #define ISFORWARDER(a) (((a)->flags & \
422 FCTX_ADDRINFO_FORWARDER) != 0)
423 #define TRIED(a) (((a)->flags & \
424 FCTX_ADDRINFO_TRIED) != 0)
426 #define NXDOMAIN(r) (((r)->attributes & DNS_RDATASETATTR_NXDOMAIN) != 0)
427 #define NEGATIVE(r) (((r)->attributes & DNS_RDATASETATTR_NEGATIVE) != 0)
429 static void destroy(dns_resolver_t *res);
430 static void empty_bucket(dns_resolver_t *res);
431 static isc_result_t resquery_send(resquery_t *query);
432 static void resquery_response(isc_task_t *task, isc_event_t *event);
433 static void resquery_connected(isc_task_t *task, isc_event_t *event);
434 static void fctx_try(fetchctx_t *fctx, isc_boolean_t retrying,
435 isc_boolean_t badcache);
436 static isc_boolean_t fctx_destroy(fetchctx_t *fctx);
437 static isc_result_t ncache_adderesult(dns_message_t *message,
438 dns_db_t *cache, dns_dbnode_t *node,
439 dns_rdatatype_t covers,
440 isc_stdtime_t now, dns_ttl_t maxttl,
441 isc_boolean_t optout,
442 dns_rdataset_t *ardataset,
443 isc_result_t *eresultp);
444 static void validated(isc_task_t *task, isc_event_t *event);
445 static void maybe_destroy(fetchctx_t *fctx);
446 static void add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
447 isc_result_t reason, badnstype_t badtype);
450 * Increment resolver-related statistics counters.
453 inc_stats(dns_resolver_t *res, isc_statscounter_t counter) {
454 if (res->view->resstats != NULL)
455 isc_stats_increment(res->view->resstats, counter);
459 valcreate(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, dns_name_t *name,
460 dns_rdatatype_t type, dns_rdataset_t *rdataset,
461 dns_rdataset_t *sigrdataset, unsigned int valoptions,
464 dns_validator_t *validator = NULL;
465 dns_valarg_t *valarg;
468 valarg = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
471 return (ISC_R_NOMEMORY);
474 valarg->addrinfo = addrinfo;
476 if (!ISC_LIST_EMPTY(fctx->validators))
477 INSIST((valoptions & DNS_VALIDATOR_DEFER) != 0);
479 result = dns_validator_create(fctx->res->view, name, type, rdataset,
480 sigrdataset, fctx->rmessage,
481 valoptions, task, validated, valarg,
483 if (result == ISC_R_SUCCESS) {
484 inc_stats(fctx->res, dns_resstatscounter_val);
485 if ((valoptions & DNS_VALIDATOR_DEFER) == 0) {
486 INSIST(fctx->validator == NULL);
487 fctx->validator = validator;
489 ISC_LIST_APPEND(fctx->validators, validator, link);
491 isc_mem_put(fctx->res->buckets[fctx->bucketnum].mctx,
492 valarg, sizeof(*valarg));
497 rrsig_fromchildzone(fetchctx_t *fctx, dns_rdataset_t *rdataset) {
498 dns_namereln_t namereln;
499 dns_rdata_rrsig_t rrsig;
500 dns_rdata_t rdata = DNS_RDATA_INIT;
505 for (result = dns_rdataset_first(rdataset);
506 result == ISC_R_SUCCESS;
507 result = dns_rdataset_next(rdataset)) {
508 dns_rdataset_current(rdataset, &rdata);
509 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
510 RUNTIME_CHECK(result == ISC_R_SUCCESS);
511 namereln = dns_name_fullcompare(&rrsig.signer, &fctx->domain,
513 if (namereln == dns_namereln_subdomain)
515 dns_rdata_reset(&rdata);
521 fix_mustbedelegationornxdomain(dns_message_t *message, fetchctx_t *fctx) {
523 dns_name_t *domain = &fctx->domain;
524 dns_rdataset_t *rdataset;
525 dns_rdatatype_t type;
527 isc_boolean_t keep_auth = ISC_FALSE;
529 if (message->rcode == dns_rcode_nxdomain)
533 * A DS RRset can appear anywhere in a zone, even for a delegation-only
534 * zone. So a response to an explicit query for this type should be
535 * excluded from delegation-only fixup.
537 * SOA, NS, and DNSKEY can only exist at a zone apex, so a postive
538 * response to a query for these types can never violate the
539 * delegation-only assumption: if the query name is below a
540 * zone cut, the response should normally be a referral, which should
541 * be accepted; if the query name is below a zone cut but the server
542 * happens to have authority for the zone of the query name, the
543 * response is a (non-referral) answer. But this does not violate
544 * delegation-only because the query name must be in a different zone
545 * due to the "apex-only" nature of these types. Note that if the
546 * remote server happens to have authority for a child zone of a
547 * delegation-only zone, we may still incorrectly "fix" the response
548 * with NXDOMAIN for queries for other types. Unfortunately it's
549 * generally impossible to differentiate this case from violation of
550 * the delegation-only assumption. Once the resolver learns the
551 * correct zone cut, possibly via a separate query for an "apex-only"
552 * type, queries for other types will be resolved correctly.
554 * A query for type ANY will be accepted if it hits an exceptional
555 * type above in the answer section as it should be from a child
558 * Also accept answers with RRSIG records from the child zone.
559 * Direct queries for RRSIG records should not be answered from
563 if (message->counts[DNS_SECTION_ANSWER] != 0 &&
564 (fctx->type == dns_rdatatype_ns ||
565 fctx->type == dns_rdatatype_ds ||
566 fctx->type == dns_rdatatype_soa ||
567 fctx->type == dns_rdatatype_any ||
568 fctx->type == dns_rdatatype_rrsig ||
569 fctx->type == dns_rdatatype_dnskey)) {
570 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
571 while (result == ISC_R_SUCCESS) {
573 dns_message_currentname(message, DNS_SECTION_ANSWER,
575 for (rdataset = ISC_LIST_HEAD(name->list);
577 rdataset = ISC_LIST_NEXT(rdataset, link)) {
578 if (!dns_name_equal(name, &fctx->name))
580 type = rdataset->type;
584 if (type == dns_rdatatype_rrsig &&
585 rrsig_fromchildzone(fctx, rdataset))
588 * Direct query for apex records or DS.
590 if (fctx->type == type &&
591 (type == dns_rdatatype_ds ||
592 type == dns_rdatatype_ns ||
593 type == dns_rdatatype_soa ||
594 type == dns_rdatatype_dnskey))
597 * Indirect query for apex records or DS.
599 if (fctx->type == dns_rdatatype_any &&
600 (type == dns_rdatatype_ns ||
601 type == dns_rdatatype_ds ||
602 type == dns_rdatatype_soa ||
603 type == dns_rdatatype_dnskey))
606 result = dns_message_nextname(message,
612 * A NODATA response to a DS query?
614 if (fctx->type == dns_rdatatype_ds &&
615 message->counts[DNS_SECTION_ANSWER] == 0)
618 /* Look for referral or indication of answer from child zone? */
619 if (message->counts[DNS_SECTION_AUTHORITY] == 0)
622 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
623 while (result == ISC_R_SUCCESS) {
625 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
626 for (rdataset = ISC_LIST_HEAD(name->list);
628 rdataset = ISC_LIST_NEXT(rdataset, link)) {
629 type = rdataset->type;
630 if (type == dns_rdatatype_soa &&
631 dns_name_equal(name, domain))
632 keep_auth = ISC_TRUE;
634 if (type != dns_rdatatype_ns &&
635 type != dns_rdatatype_soa &&
636 type != dns_rdatatype_rrsig)
639 if (type == dns_rdatatype_rrsig) {
640 if (rrsig_fromchildzone(fctx, rdataset))
646 /* NS or SOA records. */
647 if (dns_name_equal(name, domain)) {
649 * If a query for ANY causes a negative
650 * response, we can be sure that this is
651 * an empty node. For other type of queries
652 * we cannot differentiate an empty node
653 * from a node that just doesn't have that
654 * type of record. We only accept the former
657 if (message->counts[DNS_SECTION_ANSWER] == 0 &&
658 fctx->type == dns_rdatatype_any)
660 } else if (dns_name_issubdomain(name, domain)) {
661 /* Referral or answer from child zone. */
665 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
669 message->rcode = dns_rcode_nxdomain;
670 message->counts[DNS_SECTION_ANSWER] = 0;
672 message->counts[DNS_SECTION_AUTHORITY] = 0;
673 message->counts[DNS_SECTION_ADDITIONAL] = 0;
677 static inline isc_result_t
678 fctx_starttimer(fetchctx_t *fctx) {
680 * Start the lifetime timer for fctx.
682 * This is also used for stopping the idle timer; in that
683 * case we must purge events already posted to ensure that
684 * no further idle events are delivered.
686 return (isc_timer_reset(fctx->timer, isc_timertype_once,
687 &fctx->expires, NULL, ISC_TRUE));
691 fctx_stoptimer(fetchctx_t *fctx) {
695 * We don't return a result if resetting the timer to inactive fails
696 * since there's nothing to be done about it. Resetting to inactive
697 * should never fail anyway, since the code as currently written
698 * cannot fail in that case.
700 result = isc_timer_reset(fctx->timer, isc_timertype_inactive,
701 NULL, NULL, ISC_TRUE);
702 if (result != ISC_R_SUCCESS) {
703 UNEXPECTED_ERROR(__FILE__, __LINE__,
704 "isc_timer_reset(): %s",
705 isc_result_totext(result));
710 static inline isc_result_t
711 fctx_startidletimer(fetchctx_t *fctx, isc_interval_t *interval) {
713 * Start the idle timer for fctx. The lifetime timer continues
716 return (isc_timer_reset(fctx->timer, isc_timertype_once,
717 &fctx->expires, interval, ISC_FALSE));
721 * Stopping the idle timer is equivalent to calling fctx_starttimer(), but
722 * we use fctx_stopidletimer for readability in the code below.
724 #define fctx_stopidletimer fctx_starttimer
728 resquery_destroy(resquery_t **queryp) {
731 REQUIRE(queryp != NULL);
733 REQUIRE(!ISC_LINK_LINKED(query, link));
735 INSIST(query->tcpsocket == NULL);
737 query->fctx->nqueries--;
738 if (SHUTTINGDOWN(query->fctx))
739 maybe_destroy(query->fctx); /* Locks bucket. */
741 isc_mem_put(query->mctx, query, sizeof(*query));
746 fctx_cancelquery(resquery_t **queryp, dns_dispatchevent_t **deventp,
747 isc_time_t *finish, isc_boolean_t no_response)
751 unsigned int rtt, rttms;
754 dns_adbaddrinfo_t *addrinfo;
755 isc_socket_t *socket;
760 FCTXTRACE("cancelquery");
762 REQUIRE(!RESQUERY_CANCELED(query));
764 query->attributes |= RESQUERY_ATTR_CANCELED;
767 * Should we update the RTT?
769 if (finish != NULL || no_response) {
770 if (finish != NULL) {
772 * We have both the start and finish times for this
773 * packet, so we can compute a real RTT.
775 rtt = (unsigned int)isc_time_microdiff(finish,
777 factor = DNS_ADB_RTTADJDEFAULT;
780 if (rttms < DNS_RESOLVER_QRYRTTCLASS0) {
782 dns_resstatscounter_queryrtt0);
783 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS1) {
785 dns_resstatscounter_queryrtt1);
786 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS2) {
788 dns_resstatscounter_queryrtt2);
789 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS3) {
791 dns_resstatscounter_queryrtt3);
792 } else if (rttms < DNS_RESOLVER_QRYRTTCLASS4) {
794 dns_resstatscounter_queryrtt4);
797 dns_resstatscounter_queryrtt5);
801 * We don't have an RTT for this query. Maybe the
802 * packet was lost, or maybe this server is very
803 * slow. We don't know. Increase the RTT.
806 rtt = query->addrinfo->srtt + 200000;
810 * Replace the current RTT with our value.
812 factor = DNS_ADB_RTTADJREPLACE;
814 dns_adb_adjustsrtt(fctx->adb, query->addrinfo, rtt, factor);
817 /* Remember that the server has been tried. */
818 if (!TRIED(query->addrinfo)) {
819 dns_adb_changeflags(fctx->adb, query->addrinfo,
820 FCTX_ADDRINFO_TRIED, FCTX_ADDRINFO_TRIED);
824 * Age RTTs of servers not tried.
826 factor = DNS_ADB_RTTADJAGE;
828 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
830 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
831 if (UNMARKED(addrinfo))
832 dns_adb_adjustsrtt(fctx->adb, addrinfo,
835 if (finish != NULL && TRIEDFIND(fctx))
836 for (find = ISC_LIST_HEAD(fctx->finds);
838 find = ISC_LIST_NEXT(find, publink))
839 for (addrinfo = ISC_LIST_HEAD(find->list);
841 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
842 if (UNMARKED(addrinfo))
843 dns_adb_adjustsrtt(fctx->adb, addrinfo,
846 if (finish != NULL && TRIEDALT(fctx)) {
847 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
849 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
850 if (UNMARKED(addrinfo))
851 dns_adb_adjustsrtt(fctx->adb, addrinfo,
853 for (find = ISC_LIST_HEAD(fctx->altfinds);
855 find = ISC_LIST_NEXT(find, publink))
856 for (addrinfo = ISC_LIST_HEAD(find->list);
858 addrinfo = ISC_LIST_NEXT(addrinfo, publink))
859 if (UNMARKED(addrinfo))
860 dns_adb_adjustsrtt(fctx->adb, addrinfo,
865 * Check for any outstanding socket events. If they exist, cancel
866 * them and let the event handlers finish the cleanup. The resolver
867 * only needs to worry about managing the connect and send events;
868 * the dispatcher manages the recv events.
870 if (RESQUERY_CONNECTING(query)) {
872 * Cancel the connect.
874 if (query->tcpsocket != NULL) {
875 isc_socket_cancel(query->tcpsocket, NULL,
876 ISC_SOCKCANCEL_CONNECT);
877 } else if (query->dispentry != NULL) {
878 INSIST(query->exclusivesocket);
879 socket = dns_dispatch_getentrysocket(query->dispentry);
881 isc_socket_cancel(socket, NULL,
882 ISC_SOCKCANCEL_CONNECT);
884 } else if (RESQUERY_SENDING(query)) {
886 * Cancel the pending send.
888 if (query->exclusivesocket && query->dispentry != NULL)
889 socket = dns_dispatch_getentrysocket(query->dispentry);
891 socket = dns_dispatch_getsocket(query->dispatch);
893 isc_socket_cancel(socket, NULL, ISC_SOCKCANCEL_SEND);
896 if (query->dispentry != NULL)
897 dns_dispatch_removeresponse(&query->dispentry, deventp);
899 ISC_LIST_UNLINK(fctx->queries, query, link);
901 if (query->tsig != NULL)
902 isc_buffer_free(&query->tsig);
904 if (query->tsigkey != NULL)
905 dns_tsigkey_detach(&query->tsigkey);
907 if (query->dispatch != NULL)
908 dns_dispatch_detach(&query->dispatch);
910 if (! (RESQUERY_CONNECTING(query) || RESQUERY_SENDING(query)))
912 * It's safe to destroy the query now.
914 resquery_destroy(&query);
918 fctx_cancelqueries(fetchctx_t *fctx, isc_boolean_t no_response) {
919 resquery_t *query, *next_query;
921 FCTXTRACE("cancelqueries");
923 for (query = ISC_LIST_HEAD(fctx->queries);
925 query = next_query) {
926 next_query = ISC_LIST_NEXT(query, link);
927 fctx_cancelquery(&query, NULL, NULL, no_response);
932 fctx_cleanupfinds(fetchctx_t *fctx) {
933 dns_adbfind_t *find, *next_find;
935 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
937 for (find = ISC_LIST_HEAD(fctx->finds);
940 next_find = ISC_LIST_NEXT(find, publink);
941 ISC_LIST_UNLINK(fctx->finds, find, publink);
942 dns_adb_destroyfind(&find);
948 fctx_cleanupaltfinds(fetchctx_t *fctx) {
949 dns_adbfind_t *find, *next_find;
951 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
953 for (find = ISC_LIST_HEAD(fctx->altfinds);
956 next_find = ISC_LIST_NEXT(find, publink);
957 ISC_LIST_UNLINK(fctx->altfinds, find, publink);
958 dns_adb_destroyfind(&find);
960 fctx->altfind = NULL;
964 fctx_cleanupforwaddrs(fetchctx_t *fctx) {
965 dns_adbaddrinfo_t *addr, *next_addr;
967 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
969 for (addr = ISC_LIST_HEAD(fctx->forwaddrs);
972 next_addr = ISC_LIST_NEXT(addr, publink);
973 ISC_LIST_UNLINK(fctx->forwaddrs, addr, publink);
974 dns_adb_freeaddrinfo(fctx->adb, &addr);
979 fctx_cleanupaltaddrs(fetchctx_t *fctx) {
980 dns_adbaddrinfo_t *addr, *next_addr;
982 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
984 for (addr = ISC_LIST_HEAD(fctx->altaddrs);
987 next_addr = ISC_LIST_NEXT(addr, publink);
988 ISC_LIST_UNLINK(fctx->altaddrs, addr, publink);
989 dns_adb_freeaddrinfo(fctx->adb, &addr);
994 fctx_stopeverything(fetchctx_t *fctx, isc_boolean_t no_response) {
995 FCTXTRACE("stopeverything");
996 fctx_cancelqueries(fctx, no_response);
997 fctx_cleanupfinds(fctx);
998 fctx_cleanupaltfinds(fctx);
999 fctx_cleanupforwaddrs(fctx);
1000 fctx_cleanupaltaddrs(fctx);
1001 fctx_stoptimer(fctx);
1005 fctx_sendevents(fetchctx_t *fctx, isc_result_t result, int line) {
1006 dns_fetchevent_t *event, *next_event;
1008 unsigned int count = 0;
1010 isc_boolean_t logit = ISC_FALSE;
1012 unsigned int old_spillat;
1013 unsigned int new_spillat = 0; /* initialized to silence
1014 compiler warnings */
1017 * Caller must be holding the appropriate bucket lock.
1019 REQUIRE(fctx->state == fetchstate_done);
1021 FCTXTRACE("sendevents");
1024 * Keep some record of fetch result for logging later (if required).
1026 fctx->result = result;
1027 fctx->exitline = line;
1029 fctx->duration = isc_time_microdiff(&now, &fctx->start);
1031 for (event = ISC_LIST_HEAD(fctx->events);
1033 event = next_event) {
1034 next_event = ISC_LIST_NEXT(event, ev_link);
1035 ISC_LIST_UNLINK(fctx->events, event, ev_link);
1036 task = event->ev_sender;
1037 event->ev_sender = fctx;
1038 if (!HAVE_ANSWER(fctx))
1039 event->result = result;
1041 INSIST(result != ISC_R_SUCCESS ||
1042 dns_rdataset_isassociated(event->rdataset) ||
1043 fctx->type == dns_rdatatype_any ||
1044 fctx->type == dns_rdatatype_rrsig ||
1045 fctx->type == dns_rdatatype_sig);
1048 * Negative results must be indicated in event->result.
1050 if (dns_rdataset_isassociated(event->rdataset) &&
1051 NEGATIVE(event->rdataset)) {
1052 INSIST(event->result == DNS_R_NCACHENXDOMAIN ||
1053 event->result == DNS_R_NCACHENXRRSET);
1056 isc_task_sendanddetach(&task, ISC_EVENT_PTR(&event));
1060 if ((fctx->attributes & FCTX_ATTR_HAVEANSWER) != 0 &&
1062 (count < fctx->res->spillatmax || fctx->res->spillatmax == 0)) {
1063 LOCK(&fctx->res->lock);
1064 if (count == fctx->res->spillat && !fctx->res->exiting) {
1065 old_spillat = fctx->res->spillat;
1066 fctx->res->spillat += 5;
1067 if (fctx->res->spillat > fctx->res->spillatmax &&
1068 fctx->res->spillatmax != 0)
1069 fctx->res->spillat = fctx->res->spillatmax;
1070 new_spillat = fctx->res->spillat;
1071 if (new_spillat != old_spillat) {
1074 isc_interval_set(&i, 20 * 60, 0);
1075 result = isc_timer_reset(fctx->res->spillattimer,
1076 isc_timertype_ticker, NULL,
1078 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1080 UNLOCK(&fctx->res->lock);
1082 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
1083 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
1084 "clients-per-query increased to %u",
1090 log_edns(fetchctx_t *fctx) {
1091 char domainbuf[DNS_NAME_FORMATSIZE];
1093 if (fctx->reason == NULL)
1096 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
1097 isc_log_write(dns_lctx, DNS_LOGCATEGORY_EDNS_DISABLED,
1098 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
1099 "success resolving '%s' (in '%s'?) after %s",
1100 fctx->info, domainbuf, fctx->reason);
1102 fctx->reason = NULL;
1106 fctx_done(fetchctx_t *fctx, isc_result_t result, int line) {
1107 dns_resolver_t *res;
1108 isc_boolean_t no_response;
1116 if (result == ISC_R_SUCCESS) {
1118 * Log any deferred EDNS timeout messages.
1121 no_response = ISC_TRUE;
1123 no_response = ISC_FALSE;
1125 fctx->reason = NULL;
1126 fctx_stopeverything(fctx, no_response);
1128 LOCK(&res->buckets[fctx->bucketnum].lock);
1130 fctx->state = fetchstate_done;
1131 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1132 fctx_sendevents(fctx, result, line);
1134 UNLOCK(&res->buckets[fctx->bucketnum].lock);
1138 process_sendevent(resquery_t *query, isc_event_t *event) {
1139 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1140 isc_boolean_t retry = ISC_FALSE;
1141 isc_result_t result;
1146 if (RESQUERY_CANCELED(query)) {
1147 if (query->sends == 0 && query->connects == 0) {
1149 * This query was canceled while the
1150 * isc_socket_sendto/connect() was in progress.
1152 if (query->tcpsocket != NULL)
1153 isc_socket_detach(&query->tcpsocket);
1154 resquery_destroy(&query);
1157 switch (sevent->result) {
1161 case ISC_R_HOSTUNREACH:
1162 case ISC_R_NETUNREACH:
1164 case ISC_R_ADDRNOTAVAIL:
1165 case ISC_R_CONNREFUSED:
1168 * No route to remote.
1170 add_bad(fctx, query->addrinfo, sevent->result,
1172 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
1177 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
1182 isc_event_free(&event);
1186 * Behave as if the idle timer has expired. For TCP
1187 * this may not actually reflect the latest timer.
1189 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1190 result = fctx_stopidletimer(fctx);
1191 if (result != ISC_R_SUCCESS)
1192 fctx_done(fctx, result, __LINE__);
1194 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
1199 resquery_udpconnected(isc_task_t *task, isc_event_t *event) {
1200 resquery_t *query = event->ev_arg;
1202 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
1204 QTRACE("udpconnected");
1208 INSIST(RESQUERY_CONNECTING(query));
1212 process_sendevent(query, event);
1216 resquery_senddone(isc_task_t *task, isc_event_t *event) {
1217 resquery_t *query = event->ev_arg;
1219 REQUIRE(event->ev_type == ISC_SOCKEVENT_SENDDONE);
1226 * Currently we don't wait for the senddone event before retrying
1227 * a query. This means that if we get really behind, we may end
1228 * up doing extra work!
1233 INSIST(RESQUERY_SENDING(query));
1237 process_sendevent(query, event);
1240 static inline isc_result_t
1241 fctx_addopt(dns_message_t *message, unsigned int version,
1242 isc_uint16_t udpsize, isc_boolean_t request_nsid)
1244 dns_rdataset_t *rdataset;
1245 dns_rdatalist_t *rdatalist;
1247 isc_result_t result;
1250 result = dns_message_gettemprdatalist(message, &rdatalist);
1251 if (result != ISC_R_SUCCESS)
1254 result = dns_message_gettemprdata(message, &rdata);
1255 if (result != ISC_R_SUCCESS)
1258 result = dns_message_gettemprdataset(message, &rdataset);
1259 if (result != ISC_R_SUCCESS)
1261 dns_rdataset_init(rdataset);
1263 rdatalist->type = dns_rdatatype_opt;
1264 rdatalist->covers = 0;
1267 * Set Maximum UDP buffer size.
1269 rdatalist->rdclass = udpsize;
1272 * Set EXTENDED-RCODE and Z to 0, DO to 1.
1274 rdatalist->ttl = (version << 16);
1275 rdatalist->ttl |= DNS_MESSAGEEXTFLAG_DO;
1278 * Set EDNS options if applicable
1281 /* Send empty NSID option (RFC5001) */
1282 unsigned char data[4];
1285 isc_buffer_init(&buf, data, sizeof(data));
1286 isc_buffer_putuint16(&buf, DNS_OPT_NSID);
1287 isc_buffer_putuint16(&buf, 0);
1289 rdata->length = sizeof(data);
1295 rdata->rdclass = rdatalist->rdclass;
1296 rdata->type = rdatalist->type;
1299 ISC_LIST_INIT(rdatalist->rdata);
1300 ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
1301 RUNTIME_CHECK(dns_rdatalist_tordataset(rdatalist, rdataset) == ISC_R_SUCCESS);
1303 return (dns_message_setopt(message, rdataset));
1307 fctx_setretryinterval(fetchctx_t *fctx, unsigned int rtt) {
1308 unsigned int seconds;
1312 * We retry every .8 seconds the first two times through the address
1313 * list, and then we do exponential back-off.
1315 if (fctx->restarts < 3)
1318 us = (800000 << (fctx->restarts - 2));
1321 * Double the round-trip time.
1326 * Always wait for at least the doubled round-trip time.
1332 * But don't ever wait for more than 10 seconds.
1337 seconds = us / 1000000;
1338 us -= seconds * 1000000;
1339 isc_interval_set(&fctx->interval, seconds, us * 1000);
1343 fctx_query(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
1344 unsigned int options)
1346 dns_resolver_t *res;
1348 isc_result_t result;
1350 isc_sockaddr_t addr;
1351 isc_boolean_t have_addr = ISC_FALSE;
1357 task = res->buckets[fctx->bucketnum].task;
1359 srtt = addrinfo->srtt;
1360 if (ISFORWARDER(addrinfo) && srtt < 1000000)
1363 fctx_setretryinterval(fctx, srtt);
1364 result = fctx_startidletimer(fctx, &fctx->interval);
1365 if (result != ISC_R_SUCCESS)
1368 INSIST(ISC_LIST_EMPTY(fctx->validators));
1370 dns_message_reset(fctx->rmessage, DNS_MESSAGE_INTENTPARSE);
1372 query = isc_mem_get(res->buckets[fctx->bucketnum].mctx,
1374 if (query == NULL) {
1375 result = ISC_R_NOMEMORY;
1376 goto stop_idle_timer;
1378 query->mctx = res->buckets[fctx->bucketnum].mctx;
1379 query->options = options;
1380 query->attributes = 0;
1382 query->connects = 0;
1384 * Note that the caller MUST guarantee that 'addrinfo' will remain
1385 * valid until this query is canceled.
1387 query->addrinfo = addrinfo;
1388 TIME_NOW(&query->start);
1391 * If this is a TCP query, then we need to make a socket and
1392 * a dispatch for it here. Otherwise we use the resolver's
1395 query->dispatchmgr = res->dispatchmgr;
1396 query->dispatch = NULL;
1397 query->exclusivesocket = ISC_FALSE;
1398 query->tcpsocket = NULL;
1399 if (res->view->peers != NULL) {
1400 dns_peer_t *peer = NULL;
1401 isc_netaddr_t dstip;
1402 isc_netaddr_fromsockaddr(&dstip, &addrinfo->sockaddr);
1403 result = dns_peerlist_peerbyaddr(res->view->peers,
1405 if (result == ISC_R_SUCCESS) {
1406 result = dns_peer_getquerysource(peer, &addr);
1407 if (result == ISC_R_SUCCESS)
1408 have_addr = ISC_TRUE;
1412 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1415 pf = isc_sockaddr_pf(&addrinfo->sockaddr);
1420 dns_dispatch_getlocaladdress(res->dispatchv4,
1425 dns_dispatch_getlocaladdress(res->dispatchv6,
1429 result = ISC_R_NOTIMPLEMENTED;
1432 if (result != ISC_R_SUCCESS)
1435 isc_sockaddr_setport(&addr, 0);
1437 result = isc_socket_create(res->socketmgr, pf,
1440 if (result != ISC_R_SUCCESS)
1443 #ifndef BROKEN_TCP_BIND_BEFORE_CONNECT
1444 result = isc_socket_bind(query->tcpsocket, &addr, 0);
1445 if (result != ISC_R_SUCCESS)
1446 goto cleanup_socket;
1450 * A dispatch will be created once the connect succeeds.
1454 unsigned int attrs, attrmask;
1455 attrs = DNS_DISPATCHATTR_UDP;
1456 switch (isc_sockaddr_pf(&addr)) {
1458 attrs |= DNS_DISPATCHATTR_IPV4;
1461 attrs |= DNS_DISPATCHATTR_IPV6;
1464 result = ISC_R_NOTIMPLEMENTED;
1467 attrmask = DNS_DISPATCHATTR_UDP;
1468 attrmask |= DNS_DISPATCHATTR_TCP;
1469 attrmask |= DNS_DISPATCHATTR_IPV4;
1470 attrmask |= DNS_DISPATCHATTR_IPV6;
1471 result = dns_dispatch_getudp(res->dispatchmgr,
1473 res->taskmgr, &addr,
1474 4096, 1000, 32768, 16411,
1475 16433, attrs, attrmask,
1477 if (result != ISC_R_SUCCESS)
1480 switch (isc_sockaddr_pf(&addrinfo->sockaddr)) {
1482 dns_dispatch_attach(res->dispatchv4,
1484 query->exclusivesocket = res->exclusivev4;
1487 dns_dispatch_attach(res->dispatchv6,
1489 query->exclusivesocket = res->exclusivev6;
1492 result = ISC_R_NOTIMPLEMENTED;
1497 * We should always have a valid dispatcher here. If we
1498 * don't support a protocol family, then its dispatcher
1499 * will be NULL, but we shouldn't be finding addresses for
1500 * protocol types we don't support, so the dispatcher
1501 * we found should never be NULL.
1503 INSIST(query->dispatch != NULL);
1506 query->dispentry = NULL;
1509 query->tsigkey = NULL;
1510 ISC_LINK_INIT(query, link);
1511 query->magic = QUERY_MAGIC;
1513 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1515 * Connect to the remote server.
1517 * XXXRTH Should we attach to the socket?
1519 result = isc_socket_connect(query->tcpsocket,
1520 &addrinfo->sockaddr, task,
1521 resquery_connected, query);
1522 if (result != ISC_R_SUCCESS)
1523 goto cleanup_socket;
1525 QTRACE("connecting via TCP");
1527 result = resquery_send(query);
1528 if (result != ISC_R_SUCCESS)
1529 goto cleanup_dispatch;
1533 ISC_LIST_APPEND(fctx->queries, query, link);
1534 query->fctx->nqueries++;
1535 if (isc_sockaddr_pf(&addrinfo->sockaddr) == PF_INET)
1536 inc_stats(res, dns_resstatscounter_queryv4);
1538 inc_stats(res, dns_resstatscounter_queryv6);
1539 if (res->view->resquerystats != NULL)
1540 dns_rdatatypestats_increment(res->view->resquerystats,
1543 return (ISC_R_SUCCESS);
1546 isc_socket_detach(&query->tcpsocket);
1549 if (query->dispatch != NULL)
1550 dns_dispatch_detach(&query->dispatch);
1554 isc_mem_put(res->buckets[fctx->bucketnum].mctx,
1555 query, sizeof(*query));
1558 RUNTIME_CHECK(fctx_stopidletimer(fctx) == ISC_R_SUCCESS);
1563 static isc_boolean_t
1564 bad_edns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1567 for (sa = ISC_LIST_HEAD(fctx->bad_edns);
1569 sa = ISC_LIST_NEXT(sa, link)) {
1570 if (isc_sockaddr_equal(sa, address))
1578 add_bad_edns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1581 if (bad_edns(fctx, address))
1584 sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
1590 ISC_LIST_INITANDAPPEND(fctx->bad_edns, sa, link);
1593 static isc_boolean_t
1594 triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1597 for (sa = ISC_LIST_HEAD(fctx->edns);
1599 sa = ISC_LIST_NEXT(sa, link)) {
1600 if (isc_sockaddr_equal(sa, address))
1608 add_triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1611 if (triededns(fctx, address))
1614 sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
1620 ISC_LIST_INITANDAPPEND(fctx->edns, sa, link);
1623 static isc_boolean_t
1624 triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1627 for (sa = ISC_LIST_HEAD(fctx->edns512);
1629 sa = ISC_LIST_NEXT(sa, link)) {
1630 if (isc_sockaddr_equal(sa, address))
1638 add_triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1641 if (triededns512(fctx, address))
1644 sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
1650 ISC_LIST_INITANDAPPEND(fctx->edns512, sa, link);
1654 resquery_send(resquery_t *query) {
1656 isc_result_t result;
1657 dns_name_t *qname = NULL;
1658 dns_rdataset_t *qrdataset = NULL;
1660 dns_resolver_t *res;
1662 isc_socket_t *socket;
1663 isc_buffer_t tcpbuffer;
1664 isc_sockaddr_t *address;
1665 isc_buffer_t *buffer;
1666 isc_netaddr_t ipaddr;
1667 dns_tsigkey_t *tsigkey = NULL;
1668 dns_peer_t *peer = NULL;
1669 isc_boolean_t useedns;
1670 dns_compress_t cctx;
1671 isc_boolean_t cleanup_cctx = ISC_FALSE;
1672 isc_boolean_t secure_domain;
1678 task = res->buckets[fctx->bucketnum].task;
1681 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1683 * Reserve space for the TCP message length.
1685 isc_buffer_init(&tcpbuffer, query->data, sizeof(query->data));
1686 isc_buffer_init(&query->buffer, query->data + 2,
1687 sizeof(query->data) - 2);
1688 buffer = &tcpbuffer;
1690 isc_buffer_init(&query->buffer, query->data,
1691 sizeof(query->data));
1692 buffer = &query->buffer;
1695 result = dns_message_gettempname(fctx->qmessage, &qname);
1696 if (result != ISC_R_SUCCESS)
1698 result = dns_message_gettemprdataset(fctx->qmessage, &qrdataset);
1699 if (result != ISC_R_SUCCESS)
1703 * Get a query id from the dispatch.
1705 result = dns_dispatch_addresponse2(query->dispatch,
1706 &query->addrinfo->sockaddr,
1713 if (result != ISC_R_SUCCESS)
1716 fctx->qmessage->opcode = dns_opcode_query;
1721 dns_name_init(qname, NULL);
1722 dns_name_clone(&fctx->name, qname);
1723 dns_rdataset_init(qrdataset);
1724 dns_rdataset_makequestion(qrdataset, res->rdclass, fctx->type);
1725 ISC_LIST_APPEND(qname->list, qrdataset, link);
1726 dns_message_addname(fctx->qmessage, qname, DNS_SECTION_QUESTION);
1731 * Set RD if the client has requested that we do a recursive query,
1732 * or if we're sending to a forwarder.
1734 if ((query->options & DNS_FETCHOPT_RECURSIVE) != 0 ||
1735 ISFORWARDER(query->addrinfo))
1736 fctx->qmessage->flags |= DNS_MESSAGEFLAG_RD;
1739 * Set CD if the client says don't validate or the question is
1740 * under a secure entry point.
1742 if ((query->options & DNS_FETCHOPT_NOVALIDATE) != 0) {
1743 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1744 } else if (res->view->enablevalidation) {
1745 result = dns_keytable_issecuredomain(res->view->secroots,
1748 if (result != ISC_R_SUCCESS)
1749 secure_domain = ISC_FALSE;
1750 if (res->view->dlv != NULL)
1751 secure_domain = ISC_TRUE;
1753 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1757 * We don't have to set opcode because it defaults to query.
1759 fctx->qmessage->id = query->id;
1762 * Convert the question to wire format.
1764 result = dns_compress_init(&cctx, -1, fctx->res->mctx);
1765 if (result != ISC_R_SUCCESS)
1766 goto cleanup_message;
1767 cleanup_cctx = ISC_TRUE;
1769 result = dns_message_renderbegin(fctx->qmessage, &cctx,
1771 if (result != ISC_R_SUCCESS)
1772 goto cleanup_message;
1774 result = dns_message_rendersection(fctx->qmessage,
1775 DNS_SECTION_QUESTION, 0);
1776 if (result != ISC_R_SUCCESS)
1777 goto cleanup_message;
1780 isc_netaddr_fromsockaddr(&ipaddr, &query->addrinfo->sockaddr);
1781 (void) dns_peerlist_peerbyaddr(fctx->res->view->peers, &ipaddr, &peer);
1784 * The ADB does not know about servers with "edns no". Check this,
1785 * and then inform the ADB for future use.
1787 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0 &&
1789 dns_peer_getsupportedns(peer, &useedns) == ISC_R_SUCCESS &&
1792 query->options |= DNS_FETCHOPT_NOEDNS0;
1793 dns_adb_changeflags(fctx->adb, query->addrinfo,
1794 DNS_FETCHOPT_NOEDNS0,
1795 DNS_FETCHOPT_NOEDNS0);
1798 /* Sync NOEDNS0 flag in addrinfo->flags and options now. */
1799 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) != 0)
1800 query->options |= DNS_FETCHOPT_NOEDNS0;
1803 * Handle timeouts by reducing the UDP response size to 512 bytes
1804 * then if that doesn't work disabling EDNS (includes DO) and CD.
1806 * These timeout can be due to:
1807 * * broken nameservers that don't respond to EDNS queries.
1808 * * broken/misconfigured firewalls and NAT implementations
1809 * that don't handle IP fragmentation.
1810 * * broken/misconfigured firewalls that don't handle responses
1811 * greater than 512 bytes.
1812 * * broken/misconfigured firewalls that don't handle EDNS, DO
1814 * * packet loss / link outage.
1816 if (fctx->timeout) {
1817 if ((triededns512(fctx, &query->addrinfo->sockaddr) ||
1818 fctx->timeouts >= (MAX_EDNS0_TIMEOUTS * 2)) &&
1819 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1820 query->options |= DNS_FETCHOPT_NOEDNS0;
1821 fctx->reason = "disabling EDNS";
1822 } else if ((triededns(fctx, &query->addrinfo->sockaddr) ||
1823 fctx->timeouts >= MAX_EDNS0_TIMEOUTS) &&
1824 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1825 query->options |= DNS_FETCHOPT_EDNS512;
1826 fctx->reason = "reducing the advertised EDNS UDP "
1827 "packet size to 512 octets";
1829 fctx->timeout = ISC_FALSE;
1833 * Use EDNS0, unless the caller doesn't want it, or we know that
1834 * the remote server doesn't like it.
1836 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1837 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0) {
1838 unsigned int version = 0; /* Default version. */
1840 isc_uint16_t udpsize = res->udpsize;
1841 isc_boolean_t reqnsid = res->view->requestnsid;
1843 flags = query->addrinfo->flags;
1844 if ((flags & DNS_FETCHOPT_EDNSVERSIONSET) != 0) {
1845 version = flags & DNS_FETCHOPT_EDNSVERSIONMASK;
1846 version >>= DNS_FETCHOPT_EDNSVERSIONSHIFT;
1848 if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
1850 else if (peer != NULL)
1851 (void)dns_peer_getudpsize(peer, &udpsize);
1853 /* request NSID for current view or peer? */
1855 (void) dns_peer_getrequestnsid(peer, &reqnsid);
1856 result = fctx_addopt(fctx->qmessage, version,
1858 if (reqnsid && result == ISC_R_SUCCESS) {
1859 query->options |= DNS_FETCHOPT_WANTNSID;
1860 } else if (result != ISC_R_SUCCESS) {
1862 * We couldn't add the OPT, but we'll press on.
1863 * We're not using EDNS0, so set the NOEDNS0
1866 query->options |= DNS_FETCHOPT_NOEDNS0;
1870 * We know this server doesn't like EDNS0, so we
1871 * won't use it. Set the NOEDNS0 bit since we're
1874 query->options |= DNS_FETCHOPT_NOEDNS0;
1879 * If we need EDNS0 to do this query and aren't using it, we lose.
1881 if (NEEDEDNS0(fctx) && (query->options & DNS_FETCHOPT_NOEDNS0) != 0) {
1882 result = DNS_R_SERVFAIL;
1883 goto cleanup_message;
1886 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0)
1887 add_triededns(fctx, &query->addrinfo->sockaddr);
1889 if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
1890 add_triededns512(fctx, &query->addrinfo->sockaddr);
1893 * Clear CD if EDNS is not in use.
1895 if ((query->options & DNS_FETCHOPT_NOEDNS0) != 0)
1896 fctx->qmessage->flags &= ~DNS_MESSAGEFLAG_CD;
1899 * Add TSIG record tailored to the current recipient.
1901 result = dns_view_getpeertsig(fctx->res->view, &ipaddr, &tsigkey);
1902 if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND)
1903 goto cleanup_message;
1905 if (tsigkey != NULL) {
1906 result = dns_message_settsigkey(fctx->qmessage, tsigkey);
1907 dns_tsigkey_detach(&tsigkey);
1908 if (result != ISC_R_SUCCESS)
1909 goto cleanup_message;
1912 result = dns_message_rendersection(fctx->qmessage,
1913 DNS_SECTION_ADDITIONAL, 0);
1914 if (result != ISC_R_SUCCESS)
1915 goto cleanup_message;
1917 result = dns_message_renderend(fctx->qmessage);
1918 if (result != ISC_R_SUCCESS)
1919 goto cleanup_message;
1921 dns_compress_invalidate(&cctx);
1922 cleanup_cctx = ISC_FALSE;
1924 if (dns_message_gettsigkey(fctx->qmessage) != NULL) {
1925 dns_tsigkey_attach(dns_message_gettsigkey(fctx->qmessage),
1927 result = dns_message_getquerytsig(fctx->qmessage,
1930 if (result != ISC_R_SUCCESS)
1931 goto cleanup_message;
1935 * If using TCP, write the length of the message at the beginning
1938 if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1939 isc_buffer_usedregion(&query->buffer, &r);
1940 isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t)r.length);
1941 isc_buffer_add(&tcpbuffer, r.length);
1945 * We're now done with the query message.
1947 dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
1949 if (query->exclusivesocket)
1950 socket = dns_dispatch_getentrysocket(query->dispentry);
1952 socket = dns_dispatch_getsocket(query->dispatch);
1956 if ((query->options & DNS_FETCHOPT_TCP) == 0) {
1957 address = &query->addrinfo->sockaddr;
1958 if (query->exclusivesocket) {
1959 result = isc_socket_connect(socket, address, task,
1960 resquery_udpconnected,
1962 if (result != ISC_R_SUCCESS)
1963 goto cleanup_message;
1967 isc_buffer_usedregion(buffer, &r);
1970 * XXXRTH Make sure we don't send to ourselves! We should probably
1971 * prune out these addresses when we get them from the ADB.
1973 result = isc_socket_sendto(socket, &r, task, resquery_senddone,
1974 query, address, NULL);
1975 if (result != ISC_R_SUCCESS)
1976 goto cleanup_message;
1982 return (ISC_R_SUCCESS);
1986 dns_compress_invalidate(&cctx);
1988 dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
1991 * Stop the dispatcher from listening.
1993 dns_dispatch_removeresponse(&query->dispentry, NULL);
1997 dns_message_puttempname(fctx->qmessage, &qname);
1998 if (qrdataset != NULL)
1999 dns_message_puttemprdataset(fctx->qmessage, &qrdataset);
2005 resquery_connected(isc_task_t *task, isc_event_t *event) {
2006 isc_socketevent_t *sevent = (isc_socketevent_t *)event;
2007 resquery_t *query = event->ev_arg;
2008 isc_boolean_t retry = ISC_FALSE;
2009 isc_interval_t interval;
2010 isc_result_t result;
2014 REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
2015 REQUIRE(VALID_QUERY(query));
2017 QTRACE("connected");
2024 * Currently we don't wait for the connect event before retrying
2025 * a query. This means that if we get really behind, we may end
2026 * up doing extra work!
2032 if (RESQUERY_CANCELED(query)) {
2034 * This query was canceled while the connect() was in
2037 isc_socket_detach(&query->tcpsocket);
2038 resquery_destroy(&query);
2040 switch (sevent->result) {
2044 * Extend the idle timer for TCP. 20 seconds
2045 * should be long enough for a TCP connection to be
2046 * established, a single DNS request to be sent,
2047 * and the response received.
2049 isc_interval_set(&interval, 20, 0);
2050 result = fctx_startidletimer(query->fctx, &interval);
2051 if (result != ISC_R_SUCCESS) {
2052 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2053 fctx_done(fctx, result, __LINE__);
2057 * We are connected. Create a dispatcher and
2061 attrs |= DNS_DISPATCHATTR_TCP;
2062 attrs |= DNS_DISPATCHATTR_PRIVATE;
2063 attrs |= DNS_DISPATCHATTR_CONNECTED;
2064 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) ==
2066 attrs |= DNS_DISPATCHATTR_IPV4;
2068 attrs |= DNS_DISPATCHATTR_IPV6;
2069 attrs |= DNS_DISPATCHATTR_MAKEQUERY;
2071 result = dns_dispatch_createtcp(query->dispatchmgr,
2073 query->fctx->res->taskmgr,
2074 4096, 2, 1, 1, 3, attrs,
2078 * Regardless of whether dns_dispatch_create()
2079 * succeeded or not, we don't need our reference
2080 * to the socket anymore.
2082 isc_socket_detach(&query->tcpsocket);
2084 if (result == ISC_R_SUCCESS)
2085 result = resquery_send(query);
2087 if (result != ISC_R_SUCCESS) {
2088 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2089 fctx_done(fctx, result, __LINE__);
2093 case ISC_R_NETUNREACH:
2094 case ISC_R_HOSTUNREACH:
2095 case ISC_R_CONNREFUSED:
2097 case ISC_R_ADDRNOTAVAIL:
2098 case ISC_R_CONNECTIONRESET:
2100 * No route to remote.
2102 isc_socket_detach(&query->tcpsocket);
2103 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
2108 isc_socket_detach(&query->tcpsocket);
2109 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2114 isc_event_free(&event);
2118 * Behave as if the idle timer has expired. For TCP
2119 * connections this may not actually reflect the latest timer.
2121 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2122 result = fctx_stopidletimer(fctx);
2123 if (result != ISC_R_SUCCESS)
2124 fctx_done(fctx, result, __LINE__);
2126 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
2131 fctx_finddone(isc_task_t *task, isc_event_t *event) {
2133 dns_adbfind_t *find;
2134 dns_resolver_t *res;
2135 isc_boolean_t want_try = ISC_FALSE;
2136 isc_boolean_t want_done = ISC_FALSE;
2137 isc_boolean_t bucket_empty = ISC_FALSE;
2138 unsigned int bucketnum;
2140 find = event->ev_sender;
2141 fctx = event->ev_arg;
2142 REQUIRE(VALID_FCTX(fctx));
2147 FCTXTRACE("finddone");
2149 INSIST(fctx->pending > 0);
2152 if (ADDRWAIT(fctx)) {
2154 * The fetch is waiting for a name to be found.
2156 INSIST(!SHUTTINGDOWN(fctx));
2157 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2158 if (event->ev_type == DNS_EVENT_ADBMOREADDRESSES)
2159 want_try = ISC_TRUE;
2162 if (fctx->pending == 0) {
2164 * We've got nothing else to wait for and don't
2165 * know the answer. There's nothing to do but
2168 want_done = ISC_TRUE;
2171 } else if (SHUTTINGDOWN(fctx) && fctx->pending == 0 &&
2172 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators)) {
2173 bucketnum = fctx->bucketnum;
2174 LOCK(&res->buckets[bucketnum].lock);
2176 * Note that we had to wait until we had the lock before
2177 * looking at fctx->references.
2179 if (fctx->references == 0)
2180 bucket_empty = fctx_destroy(fctx);
2181 UNLOCK(&res->buckets[bucketnum].lock);
2184 isc_event_free(&event);
2185 dns_adb_destroyfind(&find);
2188 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
2190 fctx_done(fctx, ISC_R_FAILURE, __LINE__);
2191 else if (bucket_empty)
2196 static inline isc_boolean_t
2197 bad_server(fetchctx_t *fctx, isc_sockaddr_t *address) {
2200 for (sa = ISC_LIST_HEAD(fctx->bad);
2202 sa = ISC_LIST_NEXT(sa, link)) {
2203 if (isc_sockaddr_equal(sa, address))
2210 static inline isc_boolean_t
2211 mark_bad(fetchctx_t *fctx) {
2212 dns_adbfind_t *curr;
2213 dns_adbaddrinfo_t *addrinfo;
2214 isc_boolean_t all_bad = ISC_TRUE;
2217 * Mark all known bad servers, so we don't try to talk to them
2222 * Mark any bad nameservers.
2224 for (curr = ISC_LIST_HEAD(fctx->finds);
2226 curr = ISC_LIST_NEXT(curr, publink)) {
2227 for (addrinfo = ISC_LIST_HEAD(curr->list);
2229 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2230 if (bad_server(fctx, &addrinfo->sockaddr))
2231 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2233 all_bad = ISC_FALSE;
2238 * Mark any bad forwarders.
2240 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2242 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2243 if (bad_server(fctx, &addrinfo->sockaddr))
2244 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2246 all_bad = ISC_FALSE;
2250 * Mark any bad alternates.
2252 for (curr = ISC_LIST_HEAD(fctx->altfinds);
2254 curr = ISC_LIST_NEXT(curr, publink)) {
2255 for (addrinfo = ISC_LIST_HEAD(curr->list);
2257 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2258 if (bad_server(fctx, &addrinfo->sockaddr))
2259 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2261 all_bad = ISC_FALSE;
2265 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
2267 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2268 if (bad_server(fctx, &addrinfo->sockaddr))
2269 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2271 all_bad = ISC_FALSE;
2278 add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_result_t reason,
2279 badnstype_t badtype)
2281 char namebuf[DNS_NAME_FORMATSIZE];
2282 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
2288 const char *sep1, *sep2;
2289 isc_sockaddr_t *address = &addrinfo->sockaddr;
2291 if (reason == DNS_R_LAME)
2295 case badns_unreachable:
2298 case badns_response:
2301 case badns_validation:
2302 break; /* counted as 'valfail' */
2306 if (bad_server(fctx, address)) {
2308 * We already know this server is bad.
2313 FCTXTRACE("add_bad");
2315 sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
2320 ISC_LIST_INITANDAPPEND(fctx->bad, sa, link);
2322 if (reason == DNS_R_LAME) /* already logged */
2325 if (reason == DNS_R_UNEXPECTEDRCODE &&
2326 fctx->rmessage->rcode == dns_rcode_servfail &&
2327 ISFORWARDER(addrinfo))
2330 if (reason == DNS_R_UNEXPECTEDRCODE) {
2331 isc_buffer_init(&b, code, sizeof(code) - 1);
2332 dns_rcode_totext(fctx->rmessage->rcode, &b);
2333 code[isc_buffer_usedlength(&b)] = '\0';
2336 } else if (reason == DNS_R_UNEXPECTEDOPCODE) {
2337 isc_buffer_init(&b, code, sizeof(code) - 1);
2338 dns_opcode_totext((dns_opcode_t)fctx->rmessage->opcode, &b);
2339 code[isc_buffer_usedlength(&b)] = '\0';
2347 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
2348 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
2349 dns_rdataclass_format(fctx->res->rdclass, classbuf, sizeof(classbuf));
2350 isc_sockaddr_format(address, addrbuf, sizeof(addrbuf));
2351 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
2352 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
2353 "%s %s%s%sresolving '%s/%s/%s': %s",
2354 dns_result_totext(reason), sep1, code, sep2,
2355 namebuf, typebuf, classbuf, addrbuf);
2359 * Return 'bits' bits of random entropy from fctx->rand_buf,
2360 * refreshing it by calling isc_random_get() whenever the requested
2361 * number of bits is greater than the number in the buffer.
2363 static inline isc_uint32_t
2364 random_bits(fetchctx_t *fctx, isc_uint32_t bits) {
2365 isc_uint32_t ret = 0;
2367 REQUIRE(VALID_FCTX(fctx));
2368 REQUIRE(bits <= 32);
2372 if (bits >= fctx->rand_bits) {
2373 /* if rand_bits == 0, this is unnecessary but harmless */
2374 bits -= fctx->rand_bits;
2375 ret = fctx->rand_buf << bits;
2377 /* refresh random buffer now */
2378 isc_random_get(&fctx->rand_buf);
2379 fctx->rand_bits = sizeof(fctx->rand_buf) * CHAR_BIT;
2383 isc_uint32_t mask = 0xffffffff;
2385 mask = (1 << bits) - 1;
2388 ret |= fctx->rand_buf & mask;
2389 fctx->rand_buf >>= bits;
2390 fctx->rand_bits -= bits;
2397 * Add some random jitter to a server's RTT value so that the
2398 * order of queries will be unpredictable.
2400 * RTT values of servers which have been tried are fuzzed by 128 ms.
2401 * Servers that haven't been tried yet have their RTT set to a random
2402 * value between 0 ms and 7 ms; they should get to go first, but in
2403 * unpredictable order.
2406 randomize_srtt(fetchctx_t *fctx, dns_adbaddrinfo_t *ai) {
2408 ai->srtt >>= 10; /* convert to milliseconds, near enough */
2409 ai->srtt |= (ai->srtt & 0x80) | random_bits(fctx, 7);
2410 ai->srtt <<= 10; /* now back to microseconds */
2412 ai->srtt = random_bits(fctx, 3) << 10;
2416 * Sort addrinfo list by RTT (with random jitter)
2419 sort_adbfind(fetchctx_t *fctx, dns_adbfind_t *find) {
2420 dns_adbaddrinfo_t *best, *curr;
2421 dns_adbaddrinfolist_t sorted;
2423 /* Add jitter to SRTT values */
2424 curr = ISC_LIST_HEAD(find->list);
2425 while (curr != NULL) {
2426 randomize_srtt(fctx, curr);
2427 curr = ISC_LIST_NEXT(curr, publink);
2430 /* Lame N^2 bubble sort. */
2431 ISC_LIST_INIT(sorted);
2432 while (!ISC_LIST_EMPTY(find->list)) {
2433 best = ISC_LIST_HEAD(find->list);
2434 curr = ISC_LIST_NEXT(best, publink);
2435 while (curr != NULL) {
2436 if (curr->srtt < best->srtt)
2438 curr = ISC_LIST_NEXT(curr, publink);
2440 ISC_LIST_UNLINK(find->list, best, publink);
2441 ISC_LIST_APPEND(sorted, best, publink);
2443 find->list = sorted;
2447 * Sort a list of finds by server RTT (with random jitter)
2450 sort_finds(fetchctx_t *fctx, dns_adbfindlist_t *findlist) {
2451 dns_adbfind_t *best, *curr;
2452 dns_adbfindlist_t sorted;
2453 dns_adbaddrinfo_t *addrinfo, *bestaddrinfo;
2455 /* Sort each find's addrinfo list by SRTT (after adding jitter) */
2456 for (curr = ISC_LIST_HEAD(*findlist);
2458 curr = ISC_LIST_NEXT(curr, publink))
2459 sort_adbfind(fctx, curr);
2461 /* Lame N^2 bubble sort. */
2462 ISC_LIST_INIT(sorted);
2463 while (!ISC_LIST_EMPTY(*findlist)) {
2464 best = ISC_LIST_HEAD(*findlist);
2465 bestaddrinfo = ISC_LIST_HEAD(best->list);
2466 INSIST(bestaddrinfo != NULL);
2467 curr = ISC_LIST_NEXT(best, publink);
2468 while (curr != NULL) {
2469 addrinfo = ISC_LIST_HEAD(curr->list);
2470 INSIST(addrinfo != NULL);
2471 if (addrinfo->srtt < bestaddrinfo->srtt) {
2473 bestaddrinfo = addrinfo;
2475 curr = ISC_LIST_NEXT(curr, publink);
2477 ISC_LIST_UNLINK(*findlist, best, publink);
2478 ISC_LIST_APPEND(sorted, best, publink);
2484 findname(fetchctx_t *fctx, dns_name_t *name, in_port_t port,
2485 unsigned int options, unsigned int flags, isc_stdtime_t now,
2486 isc_boolean_t *need_alternate)
2488 dns_adbaddrinfo_t *ai;
2489 dns_adbfind_t *find;
2490 dns_resolver_t *res;
2491 isc_boolean_t unshared;
2492 isc_result_t result;
2495 unshared = ISC_TF((fctx->options | DNS_FETCHOPT_UNSHARED) != 0);
2497 * If this name is a subdomain of the query domain, tell
2498 * the ADB to start looking using zone/hint data. This keeps us
2499 * from getting stuck if the nameserver is beneath the zone cut
2500 * and we don't know its address (e.g. because the A record has
2503 if (dns_name_issubdomain(name, &fctx->domain))
2504 options |= DNS_ADBFIND_STARTATZONE;
2505 options |= DNS_ADBFIND_GLUEOK;
2506 options |= DNS_ADBFIND_HINTOK;
2509 * See what we know about this address.
2512 result = dns_adb_createfind(fctx->adb,
2513 res->buckets[fctx->bucketnum].task,
2514 fctx_finddone, fctx, name,
2515 &fctx->name, fctx->type,
2517 res->view->dstport, &find);
2518 if (result != ISC_R_SUCCESS) {
2519 if (result == DNS_R_ALIAS) {
2521 * XXXRTH Follow the CNAME/DNAME chain?
2523 dns_adb_destroyfind(&find);
2526 } else if (!ISC_LIST_EMPTY(find->list)) {
2528 * We have at least some of the addresses for the
2531 INSIST((find->options & DNS_ADBFIND_WANTEVENT) == 0);
2532 if (flags != 0 || port != 0) {
2533 for (ai = ISC_LIST_HEAD(find->list);
2535 ai = ISC_LIST_NEXT(ai, publink)) {
2538 isc_sockaddr_setport(&ai->sockaddr,
2542 if ((flags & FCTX_ADDRINFO_FORWARDER) != 0)
2543 ISC_LIST_APPEND(fctx->altfinds, find, publink);
2545 ISC_LIST_APPEND(fctx->finds, find, publink);
2548 * We don't know any of the addresses for this
2551 if ((find->options & DNS_ADBFIND_WANTEVENT) != 0) {
2553 * We're looking for them and will get an
2554 * event about it later.
2560 if (need_alternate != NULL &&
2561 !*need_alternate && unshared &&
2562 ((res->dispatchv4 == NULL &&
2563 find->result_v6 != DNS_R_NXDOMAIN) ||
2564 (res->dispatchv6 == NULL &&
2565 find->result_v4 != DNS_R_NXDOMAIN)))
2566 *need_alternate = ISC_TRUE;
2568 if ((find->options & DNS_ADBFIND_LAMEPRUNED) != 0)
2569 fctx->lamecount++; /* cached lame server */
2571 fctx->adberr++; /* unreachable server, etc. */
2574 * If we know there are no addresses for
2575 * the family we are using then try to add
2576 * an alternative server.
2578 if (need_alternate != NULL && !*need_alternate &&
2579 ((res->dispatchv4 == NULL &&
2580 find->result_v6 == DNS_R_NXRRSET) ||
2581 (res->dispatchv6 == NULL &&
2582 find->result_v4 == DNS_R_NXRRSET)))
2583 *need_alternate = ISC_TRUE;
2584 dns_adb_destroyfind(&find);
2589 static isc_boolean_t
2590 isstrictsubdomain(dns_name_t *name1, dns_name_t *name2) {
2592 unsigned int nlabels;
2593 dns_namereln_t namereln;
2595 namereln = dns_name_fullcompare(name1, name2, &order, &nlabels);
2596 return (ISC_TF(namereln == dns_namereln_subdomain));
2600 fctx_getaddresses(fetchctx_t *fctx, isc_boolean_t badcache) {
2601 dns_rdata_t rdata = DNS_RDATA_INIT;
2602 isc_result_t result;
2603 dns_resolver_t *res;
2605 unsigned int stdoptions = 0;
2607 dns_adbaddrinfo_t *ai;
2608 isc_boolean_t all_bad;
2610 isc_boolean_t need_alternate = ISC_FALSE;
2612 FCTXTRACE("getaddresses");
2615 * Don't pound on remote servers. (Failsafe!)
2618 if (fctx->restarts > 10) {
2619 FCTXTRACE("too many restarts");
2620 return (DNS_R_SERVFAIL);
2629 INSIST(ISC_LIST_EMPTY(fctx->forwaddrs));
2630 INSIST(ISC_LIST_EMPTY(fctx->altaddrs));
2633 * If this fctx has forwarders, use them; otherwise use any
2634 * selective forwarders specified in the view; otherwise use the
2635 * resolver's forwarders (if any).
2637 sa = ISC_LIST_HEAD(fctx->forwarders);
2639 dns_forwarders_t *forwarders = NULL;
2640 dns_name_t *name = &fctx->name;
2642 unsigned int labels;
2643 dns_fixedname_t fixed;
2647 * DS records are found in the parent server.
2648 * Strip label to get the correct forwarder (if any).
2650 if (dns_rdatatype_atparent(fctx->type) &&
2651 dns_name_countlabels(name) > 1) {
2652 dns_name_init(&suffix, NULL);
2653 labels = dns_name_countlabels(name);
2654 dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
2658 dns_fixedname_init(&fixed);
2659 domain = dns_fixedname_name(&fixed);
2660 result = dns_fwdtable_find2(fctx->res->view->fwdtable, name,
2661 domain, &forwarders);
2662 if (result == ISC_R_SUCCESS) {
2663 sa = ISC_LIST_HEAD(forwarders->addrs);
2664 fctx->fwdpolicy = forwarders->fwdpolicy;
2665 if (fctx->fwdpolicy == dns_fwdpolicy_only &&
2666 isstrictsubdomain(domain, &fctx->domain)) {
2669 mctx = res->buckets[fctx->bucketnum].mctx;
2670 dns_name_free(&fctx->domain, mctx);
2671 dns_name_init(&fctx->domain, NULL);
2672 result = dns_name_dup(domain, mctx,
2674 if (result != ISC_R_SUCCESS)
2680 while (sa != NULL) {
2681 if ((isc_sockaddr_pf(sa) == AF_INET &&
2682 fctx->res->dispatchv4 == NULL) ||
2683 (isc_sockaddr_pf(sa) == AF_INET6 &&
2684 fctx->res->dispatchv6 == NULL)) {
2685 sa = ISC_LIST_NEXT(sa, link);
2689 result = dns_adb_findaddrinfo(fctx->adb,
2690 sa, &ai, 0); /* XXXMLG */
2691 if (result == ISC_R_SUCCESS) {
2692 dns_adbaddrinfo_t *cur;
2693 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2694 cur = ISC_LIST_HEAD(fctx->forwaddrs);
2695 while (cur != NULL && cur->srtt < ai->srtt)
2696 cur = ISC_LIST_NEXT(cur, publink);
2698 ISC_LIST_INSERTBEFORE(fctx->forwaddrs, cur,
2701 ISC_LIST_APPEND(fctx->forwaddrs, ai, publink);
2703 sa = ISC_LIST_NEXT(sa, link);
2707 * If the forwarding policy is "only", we don't need the addresses
2708 * of the nameservers.
2710 if (fctx->fwdpolicy == dns_fwdpolicy_only)
2714 * Normal nameservers.
2717 stdoptions = DNS_ADBFIND_WANTEVENT | DNS_ADBFIND_EMPTYEVENT;
2718 if (fctx->restarts == 1) {
2720 * To avoid sending out a flood of queries likely to
2721 * result in NXRRSET, we suppress fetches for address
2722 * families we don't have the first time through,
2723 * provided that we have addresses in some family we
2726 * We don't want to set this option all the time, since
2727 * if fctx->restarts > 1, we've clearly been having trouble
2728 * with the addresses we had, so getting more could help.
2730 stdoptions |= DNS_ADBFIND_AVOIDFETCHES;
2732 if (res->dispatchv4 != NULL)
2733 stdoptions |= DNS_ADBFIND_INET;
2734 if (res->dispatchv6 != NULL)
2735 stdoptions |= DNS_ADBFIND_INET6;
2736 isc_stdtime_get(&now);
2738 INSIST(ISC_LIST_EMPTY(fctx->finds));
2739 INSIST(ISC_LIST_EMPTY(fctx->altfinds));
2741 for (result = dns_rdataset_first(&fctx->nameservers);
2742 result == ISC_R_SUCCESS;
2743 result = dns_rdataset_next(&fctx->nameservers))
2745 dns_rdataset_current(&fctx->nameservers, &rdata);
2747 * Extract the name from the NS record.
2749 result = dns_rdata_tostruct(&rdata, &ns, NULL);
2750 if (result != ISC_R_SUCCESS)
2753 findname(fctx, &ns.name, 0, stdoptions, 0, now,
2755 dns_rdata_reset(&rdata);
2756 dns_rdata_freestruct(&ns);
2758 if (result != ISC_R_NOMORE)
2762 * Do we need to use 6 to 4?
2764 if (need_alternate) {
2767 family = (res->dispatchv6 != NULL) ? AF_INET6 : AF_INET;
2768 for (a = ISC_LIST_HEAD(fctx->res->alternates);
2770 a = ISC_LIST_NEXT(a, link)) {
2771 if (!a->isaddress) {
2772 findname(fctx, &a->_u._n.name, a->_u._n.port,
2773 stdoptions, FCTX_ADDRINFO_FORWARDER,
2777 if (isc_sockaddr_pf(&a->_u.addr) != family)
2780 result = dns_adb_findaddrinfo(fctx->adb, &a->_u.addr,
2782 if (result == ISC_R_SUCCESS) {
2783 dns_adbaddrinfo_t *cur;
2784 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2785 cur = ISC_LIST_HEAD(fctx->altaddrs);
2786 while (cur != NULL && cur->srtt < ai->srtt)
2787 cur = ISC_LIST_NEXT(cur, publink);
2789 ISC_LIST_INSERTBEFORE(fctx->altaddrs,
2792 ISC_LIST_APPEND(fctx->altaddrs, ai,
2800 * Mark all known bad servers.
2802 all_bad = mark_bad(fctx);
2809 * We've got no addresses.
2811 if (fctx->pending > 0) {
2813 * We're fetching the addresses, but don't have any
2814 * yet. Tell the caller to wait for an answer.
2816 result = DNS_R_WAIT;
2821 * We've lost completely. We don't know any
2822 * addresses, and the ADB has told us it can't get
2825 FCTXTRACE("no addresses");
2826 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
2827 result = isc_time_nowplusinterval(&expire, &i);
2829 (fctx->type == dns_rdatatype_dnskey ||
2830 fctx->type == dns_rdatatype_dlv ||
2831 fctx->type == dns_rdatatype_ds) &&
2832 result == ISC_R_SUCCESS)
2833 dns_resolver_addbadcache(fctx->res,
2835 fctx->type, &expire);
2836 result = ISC_R_FAILURE;
2840 * We've found some addresses. We might still be looking
2841 * for more addresses.
2843 sort_finds(fctx, &fctx->finds);
2844 sort_finds(fctx, &fctx->altfinds);
2845 result = ISC_R_SUCCESS;
2852 possibly_mark(fetchctx_t *fctx, dns_adbaddrinfo_t *addr)
2855 char buf[ISC_NETADDR_FORMATSIZE];
2857 isc_boolean_t aborted = ISC_FALSE;
2858 isc_boolean_t bogus;
2859 dns_acl_t *blackhole;
2860 isc_netaddr_t ipaddr;
2861 dns_peer_t *peer = NULL;
2862 dns_resolver_t *res;
2863 const char *msg = NULL;
2865 sa = &addr->sockaddr;
2868 isc_netaddr_fromsockaddr(&ipaddr, sa);
2869 blackhole = dns_dispatchmgr_getblackhole(res->dispatchmgr);
2870 (void) dns_peerlist_peerbyaddr(res->view->peers, &ipaddr, &peer);
2872 if (blackhole != NULL) {
2875 if (dns_acl_match(&ipaddr, NULL, blackhole,
2877 &match, NULL) == ISC_R_SUCCESS &&
2883 dns_peer_getbogus(peer, &bogus) == ISC_R_SUCCESS &&
2888 addr->flags |= FCTX_ADDRINFO_MARK;
2889 msg = "ignoring blackholed / bogus server: ";
2890 } else if (isc_sockaddr_ismulticast(sa)) {
2891 addr->flags |= FCTX_ADDRINFO_MARK;
2892 msg = "ignoring multicast address: ";
2893 } else if (isc_sockaddr_isexperimental(sa)) {
2894 addr->flags |= FCTX_ADDRINFO_MARK;
2895 msg = "ignoring experimental address: ";
2896 } else if (sa->type.sa.sa_family != AF_INET6) {
2898 } else if (IN6_IS_ADDR_V4MAPPED(&sa->type.sin6.sin6_addr)) {
2899 addr->flags |= FCTX_ADDRINFO_MARK;
2900 msg = "ignoring IPv6 mapped IPV4 address: ";
2901 } else if (IN6_IS_ADDR_V4COMPAT(&sa->type.sin6.sin6_addr)) {
2902 addr->flags |= FCTX_ADDRINFO_MARK;
2903 msg = "ignoring IPv6 compatibility IPV4 address: ";
2907 if (!isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(3)))
2910 isc_netaddr_fromsockaddr(&na, sa);
2911 isc_netaddr_format(&na, buf, sizeof(buf));
2912 FCTXTRACE2(msg, buf);
2915 static inline dns_adbaddrinfo_t *
2916 fctx_nextaddress(fetchctx_t *fctx) {
2917 dns_adbfind_t *find, *start;
2918 dns_adbaddrinfo_t *addrinfo;
2919 dns_adbaddrinfo_t *faddrinfo;
2922 * Return the next untried address, if any.
2926 * Find the first unmarked forwarder (if any).
2928 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2930 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2931 if (!UNMARKED(addrinfo))
2933 possibly_mark(fctx, addrinfo);
2934 if (UNMARKED(addrinfo)) {
2935 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2942 * No forwarders. Move to the next find.
2945 fctx->attributes |= FCTX_ATTR_TRIEDFIND;
2949 find = ISC_LIST_HEAD(fctx->finds);
2951 find = ISC_LIST_NEXT(find, publink);
2953 find = ISC_LIST_HEAD(fctx->finds);
2957 * Find the first unmarked addrinfo.
2963 for (addrinfo = ISC_LIST_HEAD(find->list);
2965 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2966 if (!UNMARKED(addrinfo))
2968 possibly_mark(fctx, addrinfo);
2969 if (UNMARKED(addrinfo)) {
2970 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2974 if (addrinfo != NULL)
2976 find = ISC_LIST_NEXT(find, publink);
2978 find = ISC_LIST_HEAD(fctx->finds);
2979 } while (find != start);
2983 if (addrinfo != NULL)
2987 * No nameservers left. Try alternates.
2990 fctx->attributes |= FCTX_ATTR_TRIEDALT;
2992 find = fctx->altfind;
2994 find = ISC_LIST_HEAD(fctx->altfinds);
2996 find = ISC_LIST_NEXT(find, publink);
2998 find = ISC_LIST_HEAD(fctx->altfinds);
3002 * Find the first unmarked addrinfo.
3008 for (addrinfo = ISC_LIST_HEAD(find->list);
3010 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
3011 if (!UNMARKED(addrinfo))
3013 possibly_mark(fctx, addrinfo);
3014 if (UNMARKED(addrinfo)) {
3015 addrinfo->flags |= FCTX_ADDRINFO_MARK;
3019 if (addrinfo != NULL)
3021 find = ISC_LIST_NEXT(find, publink);
3023 find = ISC_LIST_HEAD(fctx->altfinds);
3024 } while (find != start);
3027 faddrinfo = addrinfo;
3030 * See if we have a better alternate server by address.
3033 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
3035 addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
3036 if (!UNMARKED(addrinfo))
3038 possibly_mark(fctx, addrinfo);
3039 if (UNMARKED(addrinfo) &&
3040 (faddrinfo == NULL ||
3041 addrinfo->srtt < faddrinfo->srtt)) {
3042 if (faddrinfo != NULL)
3043 faddrinfo->flags &= ~FCTX_ADDRINFO_MARK;
3044 addrinfo->flags |= FCTX_ADDRINFO_MARK;
3049 if (addrinfo == NULL) {
3050 addrinfo = faddrinfo;
3051 fctx->altfind = find;
3058 fctx_try(fetchctx_t *fctx, isc_boolean_t retrying, isc_boolean_t badcache) {
3059 isc_result_t result;
3060 dns_adbaddrinfo_t *addrinfo;
3064 REQUIRE(!ADDRWAIT(fctx));
3066 addrinfo = fctx_nextaddress(fctx);
3067 if (addrinfo == NULL) {
3069 * We have no more addresses. Start over.
3071 fctx_cancelqueries(fctx, ISC_TRUE);
3072 fctx_cleanupfinds(fctx);
3073 fctx_cleanupaltfinds(fctx);
3074 fctx_cleanupforwaddrs(fctx);
3075 fctx_cleanupaltaddrs(fctx);
3076 result = fctx_getaddresses(fctx, badcache);
3077 if (result == DNS_R_WAIT) {
3079 * Sleep waiting for addresses.
3081 FCTXTRACE("addrwait");
3082 fctx->attributes |= FCTX_ATTR_ADDRWAIT;
3084 } else if (result != ISC_R_SUCCESS) {
3086 * Something bad happened.
3088 fctx_done(fctx, result, __LINE__);
3092 addrinfo = fctx_nextaddress(fctx);
3094 * While we may have addresses from the ADB, they
3095 * might be bad ones. In this case, return SERVFAIL.
3097 if (addrinfo == NULL) {
3098 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
3103 result = fctx_query(fctx, addrinfo, fctx->options);
3104 if (result != ISC_R_SUCCESS)
3105 fctx_done(fctx, result, __LINE__);
3107 inc_stats(fctx->res, dns_resstatscounter_retry);
3110 static isc_boolean_t
3111 fctx_destroy(fetchctx_t *fctx) {
3112 dns_resolver_t *res;
3113 unsigned int bucketnum;
3114 isc_sockaddr_t *sa, *next_sa;
3117 * Caller must be holding the bucket lock.
3120 REQUIRE(VALID_FCTX(fctx));
3121 REQUIRE(fctx->state == fetchstate_done ||
3122 fctx->state == fetchstate_init);
3123 REQUIRE(ISC_LIST_EMPTY(fctx->events));
3124 REQUIRE(ISC_LIST_EMPTY(fctx->queries));
3125 REQUIRE(ISC_LIST_EMPTY(fctx->finds));
3126 REQUIRE(ISC_LIST_EMPTY(fctx->altfinds));
3127 REQUIRE(fctx->pending == 0);
3128 REQUIRE(fctx->references == 0);
3129 REQUIRE(ISC_LIST_EMPTY(fctx->validators));
3131 FCTXTRACE("destroy");
3134 bucketnum = fctx->bucketnum;
3136 ISC_LIST_UNLINK(res->buckets[bucketnum].fctxs, fctx, link);
3141 for (sa = ISC_LIST_HEAD(fctx->bad);
3144 next_sa = ISC_LIST_NEXT(sa, link);
3145 ISC_LIST_UNLINK(fctx->bad, sa, link);
3146 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3149 for (sa = ISC_LIST_HEAD(fctx->edns);
3152 next_sa = ISC_LIST_NEXT(sa, link);
3153 ISC_LIST_UNLINK(fctx->edns, sa, link);
3154 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3157 for (sa = ISC_LIST_HEAD(fctx->edns512);
3160 next_sa = ISC_LIST_NEXT(sa, link);
3161 ISC_LIST_UNLINK(fctx->edns512, sa, link);
3162 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3165 for (sa = ISC_LIST_HEAD(fctx->bad_edns);
3168 next_sa = ISC_LIST_NEXT(sa, link);
3169 ISC_LIST_UNLINK(fctx->bad_edns, sa, link);
3170 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3173 isc_timer_detach(&fctx->timer);
3174 dns_message_destroy(&fctx->rmessage);
3175 dns_message_destroy(&fctx->qmessage);
3176 if (dns_name_countlabels(&fctx->domain) > 0)
3177 dns_name_free(&fctx->domain, res->buckets[bucketnum].mctx);
3178 if (dns_rdataset_isassociated(&fctx->nameservers))
3179 dns_rdataset_disassociate(&fctx->nameservers);
3180 dns_name_free(&fctx->name, res->buckets[bucketnum].mctx);
3181 dns_db_detach(&fctx->cache);
3182 dns_adb_detach(&fctx->adb);
3183 isc_mem_free(res->buckets[bucketnum].mctx, fctx->info);
3184 isc_mem_put(res->buckets[bucketnum].mctx, fctx, sizeof(*fctx));
3188 UNLOCK(&res->nlock);
3190 if (res->buckets[bucketnum].exiting &&
3191 ISC_LIST_EMPTY(res->buckets[bucketnum].fctxs))
3198 * Fetch event handlers.
3202 fctx_timeout(isc_task_t *task, isc_event_t *event) {
3203 fetchctx_t *fctx = event->ev_arg;
3204 isc_timerevent_t *tevent = (isc_timerevent_t *)event;
3207 REQUIRE(VALID_FCTX(fctx));
3211 FCTXTRACE("timeout");
3213 inc_stats(fctx->res, dns_resstatscounter_querytimeout);
3215 if (event->ev_type == ISC_TIMEREVENT_LIFE) {
3216 fctx->reason = NULL;
3217 fctx_done(fctx, ISC_R_TIMEDOUT, __LINE__);
3219 isc_result_t result;
3222 fctx->timeout = ISC_TRUE;
3224 * We could cancel the running queries here, or we could let
3225 * them keep going. Since we normally use separate sockets for
3226 * different queries, we adopt the former approach to reduce
3227 * the number of open sockets: cancel the oldest query if it
3228 * expired after the query had started (this is usually the
3229 * case but is not always so, depending on the task schedule
3232 query = ISC_LIST_HEAD(fctx->queries);
3233 if (query != NULL &&
3234 isc_time_compare(&tevent->due, &query->start) >= 0) {
3235 fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
3237 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3239 * Our timer has triggered. Reestablish the fctx lifetime
3242 result = fctx_starttimer(fctx);
3243 if (result != ISC_R_SUCCESS)
3244 fctx_done(fctx, result, __LINE__);
3249 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
3252 isc_event_free(&event);
3256 fctx_shutdown(fetchctx_t *fctx) {
3257 isc_event_t *cevent;
3260 * Start the shutdown process for fctx, if it isn't already underway.
3263 FCTXTRACE("shutdown");
3266 * The caller must be holding the appropriate bucket lock.
3269 if (fctx->want_shutdown)
3272 fctx->want_shutdown = ISC_TRUE;
3275 * Unless we're still initializing (in which case the
3276 * control event is still outstanding), we need to post
3277 * the control event to tell the fetch we want it to
3280 if (fctx->state != fetchstate_init) {
3281 cevent = &fctx->control_event;
3282 isc_task_send(fctx->res->buckets[fctx->bucketnum].task,
3288 fctx_doshutdown(isc_task_t *task, isc_event_t *event) {
3289 fetchctx_t *fctx = event->ev_arg;
3290 isc_boolean_t bucket_empty = ISC_FALSE;
3291 dns_resolver_t *res;
3292 unsigned int bucketnum;
3293 dns_validator_t *validator;
3295 REQUIRE(VALID_FCTX(fctx));
3300 bucketnum = fctx->bucketnum;
3302 FCTXTRACE("doshutdown");
3305 * An fctx that is shutting down is no longer in ADDRWAIT mode.
3307 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3310 * Cancel all pending validators. Note that this must be done
3311 * without the bucket lock held, since that could cause deadlock.
3313 validator = ISC_LIST_HEAD(fctx->validators);
3314 while (validator != NULL) {
3315 dns_validator_cancel(validator);
3316 validator = ISC_LIST_NEXT(validator, link);
3319 if (fctx->nsfetch != NULL)
3320 dns_resolver_cancelfetch(fctx->nsfetch);
3323 * Shut down anything that is still running on behalf of this
3324 * fetch. To avoid deadlock with the ADB, we must do this
3325 * before we lock the bucket lock.
3327 fctx_stopeverything(fctx, ISC_FALSE);
3329 LOCK(&res->buckets[bucketnum].lock);
3331 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3333 INSIST(fctx->state == fetchstate_active ||
3334 fctx->state == fetchstate_done);
3335 INSIST(fctx->want_shutdown);
3337 if (fctx->state != fetchstate_done) {
3338 fctx->state = fetchstate_done;
3339 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3342 if (fctx->references == 0 && fctx->pending == 0 &&
3343 fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators))
3344 bucket_empty = fctx_destroy(fctx);
3346 UNLOCK(&res->buckets[bucketnum].lock);
3353 fctx_start(isc_task_t *task, isc_event_t *event) {
3354 fetchctx_t *fctx = event->ev_arg;
3355 isc_boolean_t done = ISC_FALSE, bucket_empty = ISC_FALSE;
3356 dns_resolver_t *res;
3357 unsigned int bucketnum;
3359 REQUIRE(VALID_FCTX(fctx));
3364 bucketnum = fctx->bucketnum;
3368 LOCK(&res->buckets[bucketnum].lock);
3370 INSIST(fctx->state == fetchstate_init);
3371 if (fctx->want_shutdown) {
3373 * We haven't started this fctx yet, and we've been requested
3376 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3377 fctx->state = fetchstate_done;
3378 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3380 * Since we haven't started, we INSIST that we have no
3381 * pending ADB finds and no pending validations.
3383 INSIST(fctx->pending == 0);
3384 INSIST(fctx->nqueries == 0);
3385 INSIST(ISC_LIST_EMPTY(fctx->validators));
3386 if (fctx->references == 0) {
3388 * It's now safe to destroy this fctx.
3390 bucket_empty = fctx_destroy(fctx);
3395 * Normal fctx startup.
3397 fctx->state = fetchstate_active;
3399 * Reset the control event for later use in shutting down
3402 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
3403 DNS_EVENT_FETCHCONTROL, fctx_doshutdown, fctx,
3407 UNLOCK(&res->buckets[bucketnum].lock);
3410 isc_result_t result;
3413 * All is well. Start working on the fetch.
3415 result = fctx_starttimer(fctx);
3416 if (result != ISC_R_SUCCESS)
3417 fctx_done(fctx, result, __LINE__);
3419 fctx_try(fctx, ISC_FALSE, ISC_FALSE);
3420 } else if (bucket_empty)
3425 * Fetch Creation, Joining, and Cancelation.
3428 static inline isc_result_t
3429 fctx_join(fetchctx_t *fctx, isc_task_t *task, isc_sockaddr_t *client,
3430 dns_messageid_t id, isc_taskaction_t action, void *arg,
3431 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset,
3435 dns_fetchevent_t *event;
3440 * We store the task we're going to send this event to in the
3441 * sender field. We'll make the fetch the sender when we actually
3445 isc_task_attach(task, &clone);
3446 event = (dns_fetchevent_t *)
3447 isc_event_allocate(fctx->res->mctx, clone, DNS_EVENT_FETCHDONE,
3448 action, arg, sizeof(*event));
3449 if (event == NULL) {
3450 isc_task_detach(&clone);
3451 return (ISC_R_NOMEMORY);
3453 event->result = DNS_R_SERVFAIL;
3454 event->qtype = fctx->type;
3457 event->rdataset = rdataset;
3458 event->sigrdataset = sigrdataset;
3459 event->fetch = fetch;
3460 event->client = client;
3462 dns_fixedname_init(&event->foundname);
3465 * Make sure that we can store the sigrdataset in the
3466 * first event if it is needed by any of the events.
3468 if (event->sigrdataset != NULL)
3469 ISC_LIST_PREPEND(fctx->events, event, ev_link);
3471 ISC_LIST_APPEND(fctx->events, event, ev_link);
3474 fetch->magic = DNS_FETCH_MAGIC;
3475 fetch->private = fctx;
3477 return (ISC_R_SUCCESS);
3481 fctx_create(dns_resolver_t *res, dns_name_t *name, dns_rdatatype_t type,
3482 dns_name_t *domain, dns_rdataset_t *nameservers,
3483 unsigned int options, unsigned int bucketnum, fetchctx_t **fctxp)
3486 isc_result_t result;
3487 isc_result_t iresult;
3488 isc_interval_t interval;
3489 dns_fixedname_t fixed;
3490 unsigned int findoptions = 0;
3491 char buf[DNS_NAME_FORMATSIZE + DNS_RDATATYPE_FORMATSIZE];
3492 char typebuf[DNS_RDATATYPE_FORMATSIZE];
3496 * Caller must be holding the lock for bucket number 'bucketnum'.
3498 REQUIRE(fctxp != NULL && *fctxp == NULL);
3500 fctx = isc_mem_get(res->buckets[bucketnum].mctx, sizeof(*fctx));
3502 return (ISC_R_NOMEMORY);
3503 dns_name_format(name, buf, sizeof(buf));
3504 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
3505 strcat(buf, "/"); /* checked */
3506 strcat(buf, typebuf); /* checked */
3507 fctx->info = isc_mem_strdup(res->buckets[bucketnum].mctx, buf);
3508 if (fctx->info == NULL) {
3509 result = ISC_R_NOMEMORY;
3512 FCTXTRACE("create");
3513 dns_name_init(&fctx->name, NULL);
3514 result = dns_name_dup(name, res->buckets[bucketnum].mctx, &fctx->name);
3515 if (result != ISC_R_SUCCESS)
3517 dns_name_init(&fctx->domain, NULL);
3518 dns_rdataset_init(&fctx->nameservers);
3521 fctx->options = options;
3523 * Note! We do not attach to the task. We are relying on the
3524 * resolver to ensure that this task doesn't go away while we are
3528 fctx->references = 0;
3529 fctx->bucketnum = bucketnum;
3530 fctx->state = fetchstate_init;
3531 fctx->want_shutdown = ISC_FALSE;
3532 fctx->cloned = ISC_FALSE;
3533 ISC_LIST_INIT(fctx->queries);
3534 ISC_LIST_INIT(fctx->finds);
3535 ISC_LIST_INIT(fctx->altfinds);
3536 ISC_LIST_INIT(fctx->forwaddrs);
3537 ISC_LIST_INIT(fctx->altaddrs);
3538 ISC_LIST_INIT(fctx->forwarders);
3539 fctx->fwdpolicy = dns_fwdpolicy_none;
3540 ISC_LIST_INIT(fctx->bad);
3541 ISC_LIST_INIT(fctx->edns);
3542 ISC_LIST_INIT(fctx->edns512);
3543 ISC_LIST_INIT(fctx->bad_edns);
3544 ISC_LIST_INIT(fctx->validators);
3545 fctx->validator = NULL;
3547 fctx->altfind = NULL;
3550 fctx->querysent = 0;
3551 fctx->referrals = 0;
3552 TIME_NOW(&fctx->start);
3554 fctx->lamecount = 0;
3560 fctx->result = ISC_R_FAILURE;
3561 fctx->vresult = ISC_R_SUCCESS;
3562 fctx->exitline = -1; /* sentinel */
3563 fctx->logged = ISC_FALSE;
3564 fctx->attributes = 0;
3565 fctx->spilled = ISC_FALSE;
3567 fctx->reason = NULL;
3569 fctx->rand_bits = 0;
3570 fctx->timeout = ISC_FALSE;
3572 dns_name_init(&fctx->nsname, NULL);
3573 fctx->nsfetch = NULL;
3574 dns_rdataset_init(&fctx->nsrrset);
3576 if (domain == NULL) {
3577 dns_forwarders_t *forwarders = NULL;
3578 unsigned int labels;
3579 dns_name_t *fwdname = name;
3582 * DS records are found in the parent server.
3583 * Strip label to get the correct forwarder (if any).
3585 if (dns_rdatatype_atparent(fctx->type) &&
3586 dns_name_countlabels(name) > 1) {
3587 dns_name_init(&suffix, NULL);
3588 labels = dns_name_countlabels(name);
3589 dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
3592 dns_fixedname_init(&fixed);
3593 domain = dns_fixedname_name(&fixed);
3594 result = dns_fwdtable_find2(fctx->res->view->fwdtable, fwdname,
3595 domain, &forwarders);
3596 if (result == ISC_R_SUCCESS)
3597 fctx->fwdpolicy = forwarders->fwdpolicy;
3599 if (fctx->fwdpolicy != dns_fwdpolicy_only) {
3601 * The caller didn't supply a query domain and
3602 * nameservers, and we're not in forward-only mode,
3603 * so find the best nameservers to use.
3605 if (dns_rdatatype_atparent(fctx->type))
3606 findoptions |= DNS_DBFIND_NOEXACT;
3607 result = dns_view_findzonecut(res->view, name, domain,
3608 0, findoptions, ISC_TRUE,
3611 if (result != ISC_R_SUCCESS)
3613 result = dns_name_dup(domain,
3614 res->buckets[bucketnum].mctx,
3616 if (result != ISC_R_SUCCESS) {
3617 dns_rdataset_disassociate(&fctx->nameservers);
3622 * We're in forward-only mode. Set the query domain.
3624 result = dns_name_dup(domain,
3625 res->buckets[bucketnum].mctx,
3627 if (result != ISC_R_SUCCESS)
3631 result = dns_name_dup(domain,
3632 res->buckets[bucketnum].mctx,
3634 if (result != ISC_R_SUCCESS)
3636 dns_rdataset_clone(nameservers, &fctx->nameservers);
3639 INSIST(dns_name_issubdomain(&fctx->name, &fctx->domain));
3641 fctx->qmessage = NULL;
3642 result = dns_message_create(res->buckets[bucketnum].mctx,
3643 DNS_MESSAGE_INTENTRENDER,
3646 if (result != ISC_R_SUCCESS)
3647 goto cleanup_domain;
3649 fctx->rmessage = NULL;
3650 result = dns_message_create(res->buckets[bucketnum].mctx,
3651 DNS_MESSAGE_INTENTPARSE,
3654 if (result != ISC_R_SUCCESS)
3655 goto cleanup_qmessage;
3658 * Compute an expiration time for the entire fetch.
3660 isc_interval_set(&interval, 30, 0); /* XXXRTH constant */
3661 iresult = isc_time_nowplusinterval(&fctx->expires, &interval);
3662 if (iresult != ISC_R_SUCCESS) {
3663 UNEXPECTED_ERROR(__FILE__, __LINE__,
3664 "isc_time_nowplusinterval: %s",
3665 isc_result_totext(iresult));
3666 result = ISC_R_UNEXPECTED;
3667 goto cleanup_rmessage;
3671 * Default retry interval initialization. We set the interval now
3672 * mostly so it won't be uninitialized. It will be set to the
3673 * correct value before a query is issued.
3675 isc_interval_set(&fctx->interval, 2, 0);
3678 * Create an inactive timer. It will be made active when the fetch
3679 * is actually started.
3682 iresult = isc_timer_create(res->timermgr, isc_timertype_inactive,
3684 res->buckets[bucketnum].task, fctx_timeout,
3685 fctx, &fctx->timer);
3686 if (iresult != ISC_R_SUCCESS) {
3687 UNEXPECTED_ERROR(__FILE__, __LINE__,
3688 "isc_timer_create: %s",
3689 isc_result_totext(iresult));
3690 result = ISC_R_UNEXPECTED;
3691 goto cleanup_rmessage;
3695 * Attach to the view's cache and adb.
3698 dns_db_attach(res->view->cachedb, &fctx->cache);
3700 dns_adb_attach(res->view->adb, &fctx->adb);
3702 ISC_LIST_INIT(fctx->events);
3703 ISC_LINK_INIT(fctx, link);
3704 fctx->magic = FCTX_MAGIC;
3706 ISC_LIST_APPEND(res->buckets[bucketnum].fctxs, fctx, link);
3710 UNLOCK(&res->nlock);
3714 return (ISC_R_SUCCESS);
3717 dns_message_destroy(&fctx->rmessage);
3720 dns_message_destroy(&fctx->qmessage);
3723 if (dns_name_countlabels(&fctx->domain) > 0)
3724 dns_name_free(&fctx->domain, res->buckets[bucketnum].mctx);
3725 if (dns_rdataset_isassociated(&fctx->nameservers))
3726 dns_rdataset_disassociate(&fctx->nameservers);
3729 dns_name_free(&fctx->name, res->buckets[bucketnum].mctx);
3732 isc_mem_free(res->buckets[bucketnum].mctx, fctx->info);
3735 isc_mem_put(res->buckets[bucketnum].mctx, fctx, sizeof(*fctx));
3743 static inline isc_boolean_t
3744 is_lame(fetchctx_t *fctx) {
3745 dns_message_t *message = fctx->rmessage;
3747 dns_rdataset_t *rdataset;
3748 isc_result_t result;
3750 if (message->rcode != dns_rcode_noerror &&
3751 message->rcode != dns_rcode_nxdomain)
3754 if (message->counts[DNS_SECTION_ANSWER] != 0)
3757 if (message->counts[DNS_SECTION_AUTHORITY] == 0)
3760 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
3761 while (result == ISC_R_SUCCESS) {
3763 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
3764 for (rdataset = ISC_LIST_HEAD(name->list);
3766 rdataset = ISC_LIST_NEXT(rdataset, link)) {
3767 dns_namereln_t namereln;
3769 unsigned int labels;
3770 if (rdataset->type != dns_rdatatype_ns)
3772 namereln = dns_name_fullcompare(name, &fctx->domain,
3774 if (namereln == dns_namereln_equal &&
3775 (message->flags & DNS_MESSAGEFLAG_AA) != 0)
3777 if (namereln == dns_namereln_subdomain)
3781 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
3788 log_lame(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo) {
3789 char namebuf[DNS_NAME_FORMATSIZE];
3790 char domainbuf[DNS_NAME_FORMATSIZE];
3791 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
3793 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
3794 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
3795 isc_sockaddr_format(&addrinfo->sockaddr, addrbuf, sizeof(addrbuf));
3796 isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
3797 DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
3798 "lame server resolving '%s' (in '%s'?): %s",
3799 namebuf, domainbuf, addrbuf);
3802 static inline isc_result_t
3803 same_question(fetchctx_t *fctx) {
3804 isc_result_t result;
3805 dns_message_t *message = fctx->rmessage;
3807 dns_rdataset_t *rdataset;
3810 * Caller must be holding the fctx lock.
3814 * XXXRTH Currently we support only one question.
3816 if (message->counts[DNS_SECTION_QUESTION] != 1)
3817 return (DNS_R_FORMERR);
3819 result = dns_message_firstname(message, DNS_SECTION_QUESTION);
3820 if (result != ISC_R_SUCCESS)
3823 dns_message_currentname(message, DNS_SECTION_QUESTION, &name);
3824 rdataset = ISC_LIST_HEAD(name->list);
3825 INSIST(rdataset != NULL);
3826 INSIST(ISC_LIST_NEXT(rdataset, link) == NULL);
3827 if (fctx->type != rdataset->type ||
3828 fctx->res->rdclass != rdataset->rdclass ||
3829 !dns_name_equal(&fctx->name, name))
3830 return (DNS_R_FORMERR);
3832 return (ISC_R_SUCCESS);
3836 clone_results(fetchctx_t *fctx) {
3837 dns_fetchevent_t *event, *hevent;
3838 isc_result_t result;
3839 dns_name_t *name, *hname;
3841 FCTXTRACE("clone_results");
3844 * Set up any other events to have the same data as the first
3847 * Caller must be holding the appropriate lock.
3850 fctx->cloned = ISC_TRUE;
3851 hevent = ISC_LIST_HEAD(fctx->events);
3854 hname = dns_fixedname_name(&hevent->foundname);
3855 for (event = ISC_LIST_NEXT(hevent, ev_link);
3857 event = ISC_LIST_NEXT(event, ev_link)) {
3858 name = dns_fixedname_name(&event->foundname);
3859 result = dns_name_copy(hname, name, NULL);
3860 if (result != ISC_R_SUCCESS)
3861 event->result = result;
3863 event->result = hevent->result;
3864 dns_db_attach(hevent->db, &event->db);
3865 dns_db_attachnode(hevent->db, hevent->node, &event->node);
3866 INSIST(hevent->rdataset != NULL);
3867 INSIST(event->rdataset != NULL);
3868 if (dns_rdataset_isassociated(hevent->rdataset))
3869 dns_rdataset_clone(hevent->rdataset, event->rdataset);
3870 INSIST(! (hevent->sigrdataset == NULL &&
3871 event->sigrdataset != NULL));
3872 if (hevent->sigrdataset != NULL &&
3873 dns_rdataset_isassociated(hevent->sigrdataset) &&
3874 event->sigrdataset != NULL)
3875 dns_rdataset_clone(hevent->sigrdataset,
3876 event->sigrdataset);
3880 #define CACHE(r) (((r)->attributes & DNS_RDATASETATTR_CACHE) != 0)
3881 #define ANSWER(r) (((r)->attributes & DNS_RDATASETATTR_ANSWER) != 0)
3882 #define ANSWERSIG(r) (((r)->attributes & DNS_RDATASETATTR_ANSWERSIG) != 0)
3883 #define EXTERNAL(r) (((r)->attributes & DNS_RDATASETATTR_EXTERNAL) != 0)
3884 #define CHAINING(r) (((r)->attributes & DNS_RDATASETATTR_CHAINING) != 0)
3885 #define CHASE(r) (((r)->attributes & DNS_RDATASETATTR_CHASE) != 0)
3886 #define CHECKNAMES(r) (((r)->attributes & DNS_RDATASETATTR_CHECKNAMES) != 0)
3890 * Destroy '*fctx' if it is ready to be destroyed (i.e., if it has
3891 * no references and is no longer waiting for any events). If this
3892 * was the last fctx in the resolver, destroy the resolver.
3895 * '*fctx' is shutting down.
3898 maybe_destroy(fetchctx_t *fctx) {
3899 unsigned int bucketnum;
3900 isc_boolean_t bucket_empty = ISC_FALSE;
3901 dns_resolver_t *res = fctx->res;
3902 dns_validator_t *validator, *next_validator;
3904 REQUIRE(SHUTTINGDOWN(fctx));
3906 if (fctx->pending != 0 || fctx->nqueries != 0)
3909 for (validator = ISC_LIST_HEAD(fctx->validators);
3910 validator != NULL; validator = next_validator) {
3911 next_validator = ISC_LIST_NEXT(validator, link);
3912 dns_validator_cancel(validator);
3915 bucketnum = fctx->bucketnum;
3916 LOCK(&res->buckets[bucketnum].lock);
3917 if (fctx->references == 0 && ISC_LIST_EMPTY(fctx->validators))
3918 bucket_empty = fctx_destroy(fctx);
3919 UNLOCK(&res->buckets[bucketnum].lock);
3926 * The validator has finished.
3929 validated(isc_task_t *task, isc_event_t *event) {
3930 isc_result_t result = ISC_R_SUCCESS;
3931 isc_result_t eresult = ISC_R_SUCCESS;
3934 dns_validatorevent_t *vevent;
3935 dns_fetchevent_t *hevent;
3936 dns_rdataset_t *ardataset = NULL;
3937 dns_rdataset_t *asigrdataset = NULL;
3938 dns_dbnode_t *node = NULL;
3939 isc_boolean_t negative;
3940 isc_boolean_t chaining;
3941 isc_boolean_t sentresponse;
3943 dns_dbnode_t *nsnode = NULL;
3945 dns_rdataset_t *rdataset;
3946 dns_rdataset_t *sigrdataset;
3947 dns_valarg_t *valarg;
3948 dns_adbaddrinfo_t *addrinfo;
3950 UNUSED(task); /* for now */
3952 REQUIRE(event->ev_type == DNS_EVENT_VALIDATORDONE);
3953 valarg = event->ev_arg;
3954 fctx = valarg->fctx;
3955 addrinfo = valarg->addrinfo;
3956 REQUIRE(VALID_FCTX(fctx));
3957 REQUIRE(!ISC_LIST_EMPTY(fctx->validators));
3959 vevent = (dns_validatorevent_t *)event;
3961 FCTXTRACE("received validation completion event");
3963 ISC_LIST_UNLINK(fctx->validators, vevent->validator, link);
3964 fctx->validator = NULL;
3967 * Destroy the validator early so that we can
3968 * destroy the fctx if necessary.
3970 dns_validator_destroy(&vevent->validator);
3971 isc_mem_put(fctx->res->buckets[fctx->bucketnum].mctx,
3972 valarg, sizeof(*valarg));
3974 negative = ISC_TF(vevent->rdataset == NULL);
3976 sentresponse = ISC_TF((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0);
3979 * If shutting down, ignore the results. Check to see if we're
3980 * done waiting for validator completions and ADB pending events; if
3981 * so, destroy the fctx.
3983 if (SHUTTINGDOWN(fctx) && !sentresponse) {
3984 maybe_destroy(fctx); /* Locks bucket. */
3988 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
3990 isc_stdtime_get(&now);
3993 * If chaining, we need to make sure that the right result code is
3994 * returned, and that the rdatasets are bound.
3996 if (vevent->result == ISC_R_SUCCESS &&
3998 vevent->rdataset != NULL &&
3999 CHAINING(vevent->rdataset))
4001 if (vevent->rdataset->type == dns_rdatatype_cname)
4002 eresult = DNS_R_CNAME;
4004 INSIST(vevent->rdataset->type == dns_rdatatype_dname);
4005 eresult = DNS_R_DNAME;
4007 chaining = ISC_TRUE;
4009 chaining = ISC_FALSE;
4012 * Either we're not shutting down, or we are shutting down but want
4013 * to cache the result anyway (if this was a validation started by
4014 * a query with cd set)
4017 hevent = ISC_LIST_HEAD(fctx->events);
4018 if (hevent != NULL) {
4019 if (!negative && !chaining &&
4020 (fctx->type == dns_rdatatype_any ||
4021 fctx->type == dns_rdatatype_rrsig ||
4022 fctx->type == dns_rdatatype_sig)) {
4024 * Don't bind rdatasets; the caller
4025 * will iterate the node.
4028 ardataset = hevent->rdataset;
4029 asigrdataset = hevent->sigrdataset;
4033 if (vevent->result != ISC_R_SUCCESS) {
4034 FCTXTRACE("validation failed");
4035 inc_stats(fctx->res, dns_resstatscounter_valfail);
4037 fctx->vresult = vevent->result;
4038 if (fctx->vresult != DNS_R_BROKENCHAIN) {
4039 result = ISC_R_NOTFOUND;
4040 if (vevent->rdataset != NULL)
4041 result = dns_db_findnode(fctx->cache,
4044 if (result == ISC_R_SUCCESS)
4045 (void)dns_db_deleterdataset(fctx->cache, node,
4048 if (result == ISC_R_SUCCESS &&
4049 vevent->sigrdataset != NULL)
4050 (void)dns_db_deleterdataset(fctx->cache, node,
4052 dns_rdatatype_rrsig,
4054 if (result == ISC_R_SUCCESS)
4055 dns_db_detachnode(fctx->cache, &node);
4057 if (fctx->vresult == DNS_R_BROKENCHAIN && !negative) {
4059 * Cache the data as pending for later validation.
4061 result = ISC_R_NOTFOUND;
4062 if (vevent->rdataset != NULL)
4063 result = dns_db_findnode(fctx->cache,
4066 if (result == ISC_R_SUCCESS) {
4067 (void)dns_db_addrdataset(fctx->cache, node,
4069 vevent->rdataset, 0,
4072 if (result == ISC_R_SUCCESS &&
4073 vevent->sigrdataset != NULL)
4074 (void)dns_db_addrdataset(fctx->cache, node,
4076 vevent->sigrdataset,
4078 if (result == ISC_R_SUCCESS)
4079 dns_db_detachnode(fctx->cache, &node);
4081 result = fctx->vresult;
4082 add_bad(fctx, addrinfo, result, badns_validation);
4083 isc_event_free(&event);
4084 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4085 INSIST(fctx->validator == NULL);
4086 fctx->validator = ISC_LIST_HEAD(fctx->validators);
4087 if (fctx->validator != NULL)
4088 dns_validator_send(fctx->validator);
4089 else if (sentresponse)
4090 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4091 else if (result == DNS_R_BROKENCHAIN) {
4092 isc_result_t tresult;
4096 isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
4097 tresult = isc_time_nowplusinterval(&expire, &i);
4099 (fctx->type == dns_rdatatype_dnskey ||
4100 fctx->type == dns_rdatatype_dlv ||
4101 fctx->type == dns_rdatatype_ds) &&
4102 tresult == ISC_R_SUCCESS)
4103 dns_resolver_addbadcache(fctx->res,
4105 fctx->type, &expire);
4106 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4108 fctx_try(fctx, ISC_TRUE, ISC_TRUE); /* Locks bucket. */
4114 dns_rdatatype_t covers;
4115 FCTXTRACE("nonexistence validation OK");
4117 inc_stats(fctx->res, dns_resstatscounter_valnegsuccess);
4119 if (fctx->rmessage->rcode == dns_rcode_nxdomain)
4120 covers = dns_rdatatype_any;
4122 covers = fctx->type;
4124 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE,
4126 if (result != ISC_R_SUCCESS)
4127 goto noanswer_response;
4130 * If we are asking for a SOA record set the cache time
4131 * to zero to facilitate locating the containing zone of
4134 ttl = fctx->res->view->maxncachettl;
4135 if (fctx->type == dns_rdatatype_soa &&
4136 covers == dns_rdatatype_any &&
4137 fctx->res->zero_no_soa_ttl)
4140 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
4141 covers, now, ttl, vevent->optout,
4142 ardataset, &eresult);
4143 if (result != ISC_R_SUCCESS)
4144 goto noanswer_response;
4145 goto answer_response;
4147 inc_stats(fctx->res, dns_resstatscounter_valsuccess);
4149 FCTXTRACE("validation OK");
4151 if (vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF] != NULL) {
4153 result = dns_rdataset_addnoqname(vevent->rdataset,
4154 vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF]);
4155 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4156 INSIST(vevent->sigrdataset != NULL);
4157 vevent->sigrdataset->ttl = vevent->rdataset->ttl;
4158 if (vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER] != NULL) {
4159 result = dns_rdataset_addclosest(vevent->rdataset,
4160 vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER]);
4161 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4166 * The data was already cached as pending data.
4167 * Re-cache it as secure and bind the cached
4168 * rdatasets to the first event on the fetch
4171 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE, &node);
4172 if (result != ISC_R_SUCCESS)
4173 goto noanswer_response;
4175 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4176 vevent->rdataset, 0, ardataset);
4177 if (result != ISC_R_SUCCESS &&
4178 result != DNS_R_UNCHANGED)
4179 goto noanswer_response;
4180 if (ardataset != NULL && NEGATIVE(ardataset)) {
4181 if (NXDOMAIN(ardataset))
4182 eresult = DNS_R_NCACHENXDOMAIN;
4184 eresult = DNS_R_NCACHENXRRSET;
4185 } else if (vevent->sigrdataset != NULL) {
4186 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4187 vevent->sigrdataset, 0,
4189 if (result != ISC_R_SUCCESS &&
4190 result != DNS_R_UNCHANGED)
4191 goto noanswer_response;
4196 * If we only deferred the destroy because we wanted to cache
4197 * the data, destroy now.
4199 dns_db_detachnode(fctx->cache, &node);
4200 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4201 if (SHUTTINGDOWN(fctx))
4202 maybe_destroy(fctx); /* Locks bucket. */
4206 if (!ISC_LIST_EMPTY(fctx->validators)) {
4208 INSIST(fctx->type == dns_rdatatype_any ||
4209 fctx->type == dns_rdatatype_rrsig ||
4210 fctx->type == dns_rdatatype_sig);
4212 * Don't send a response yet - we have
4213 * more rdatasets that still need to
4216 dns_db_detachnode(fctx->cache, &node);
4217 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4218 dns_validator_send(ISC_LIST_HEAD(fctx->validators));
4224 * Cache any NS/NSEC records that happened to be validated.
4226 result = dns_message_firstname(fctx->rmessage, DNS_SECTION_AUTHORITY);
4227 while (result == ISC_R_SUCCESS) {
4229 dns_message_currentname(fctx->rmessage, DNS_SECTION_AUTHORITY,
4231 for (rdataset = ISC_LIST_HEAD(name->list);
4233 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4234 if ((rdataset->type != dns_rdatatype_ns &&
4235 rdataset->type != dns_rdatatype_nsec) ||
4236 rdataset->trust != dns_trust_secure)
4238 for (sigrdataset = ISC_LIST_HEAD(name->list);
4239 sigrdataset != NULL;
4240 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4241 if (sigrdataset->type != dns_rdatatype_rrsig ||
4242 sigrdataset->covers != rdataset->type)
4246 if (sigrdataset == NULL ||
4247 sigrdataset->trust != dns_trust_secure)
4249 result = dns_db_findnode(fctx->cache, name, ISC_TRUE,
4251 if (result != ISC_R_SUCCESS)
4254 result = dns_db_addrdataset(fctx->cache, nsnode, NULL,
4255 now, rdataset, 0, NULL);
4256 if (result == ISC_R_SUCCESS)
4257 result = dns_db_addrdataset(fctx->cache, nsnode,
4261 dns_db_detachnode(fctx->cache, &nsnode);
4262 if (result != ISC_R_SUCCESS)
4265 result = dns_message_nextname(fctx->rmessage,
4266 DNS_SECTION_AUTHORITY);
4269 result = ISC_R_SUCCESS;
4272 * Respond with an answer, positive or negative,
4273 * as opposed to an error. 'node' must be non-NULL.
4276 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4278 if (hevent != NULL) {
4279 hevent->result = eresult;
4280 RUNTIME_CHECK(dns_name_copy(vevent->name,
4281 dns_fixedname_name(&hevent->foundname), NULL)
4283 dns_db_attach(fctx->cache, &hevent->db);
4284 dns_db_transfernode(fctx->cache, &node, &hevent->node);
4285 clone_results(fctx);
4290 dns_db_detachnode(fctx->cache, &node);
4292 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4294 fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4297 INSIST(node == NULL);
4298 isc_event_free(&event);
4301 static inline isc_result_t
4302 cache_name(fetchctx_t *fctx, dns_name_t *name, dns_adbaddrinfo_t *addrinfo,
4305 dns_rdataset_t *rdataset, *sigrdataset;
4306 dns_rdataset_t *addedrdataset, *ardataset, *asigrdataset;
4307 dns_rdataset_t *valrdataset = NULL, *valsigrdataset = NULL;
4308 dns_dbnode_t *node, **anodep;
4311 dns_resolver_t *res;
4312 isc_boolean_t need_validation, secure_domain, have_answer;
4313 isc_result_t result, eresult;
4314 dns_fetchevent_t *event;
4315 unsigned int options;
4318 unsigned int valoptions = 0;
4321 * The appropriate bucket lock must be held.
4325 need_validation = ISC_FALSE;
4326 POST(need_validation);
4327 secure_domain = ISC_FALSE;
4328 have_answer = ISC_FALSE;
4329 eresult = ISC_R_SUCCESS;
4330 task = res->buckets[fctx->bucketnum].task;
4333 * Is DNSSEC validation required for this name?
4335 if (res->view->enablevalidation) {
4336 result = dns_keytable_issecuredomain(res->view->secroots, name,
4338 if (result != ISC_R_SUCCESS)
4341 if (!secure_domain && res->view->dlv != NULL) {
4342 valoptions = DNS_VALIDATOR_DLV;
4343 secure_domain = ISC_TRUE;
4347 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
4348 need_validation = ISC_FALSE;
4350 need_validation = secure_domain;
4356 asigrdataset = NULL;
4358 if ((name->attributes & DNS_NAMEATTR_ANSWER) != 0 &&
4360 have_answer = ISC_TRUE;
4361 event = ISC_LIST_HEAD(fctx->events);
4362 if (event != NULL) {
4364 aname = dns_fixedname_name(&event->foundname);
4365 result = dns_name_copy(name, aname, NULL);
4366 if (result != ISC_R_SUCCESS)
4368 anodep = &event->node;
4370 * If this is an ANY, SIG or RRSIG query, we're not
4371 * going to return any rdatasets, unless we encountered
4372 * a CNAME or DNAME as "the answer". In this case,
4373 * we're going to return DNS_R_CNAME or DNS_R_DNAME
4374 * and we must set up the rdatasets.
4376 if ((fctx->type != dns_rdatatype_any &&
4377 fctx->type != dns_rdatatype_rrsig &&
4378 fctx->type != dns_rdatatype_sig) ||
4379 (name->attributes & DNS_NAMEATTR_CHAINING) != 0) {
4380 ardataset = event->rdataset;
4381 asigrdataset = event->sigrdataset;
4387 * Find or create the cache node.
4390 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
4391 if (result != ISC_R_SUCCESS)
4395 * Cache or validate each cacheable rdataset.
4397 fail = ISC_TF((fctx->res->options & DNS_RESOLVER_CHECKNAMESFAIL) != 0);
4398 for (rdataset = ISC_LIST_HEAD(name->list);
4400 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4401 if (!CACHE(rdataset))
4403 if (CHECKNAMES(rdataset)) {
4404 char namebuf[DNS_NAME_FORMATSIZE];
4405 char typebuf[DNS_RDATATYPE_FORMATSIZE];
4406 char classbuf[DNS_RDATATYPE_FORMATSIZE];
4408 dns_name_format(name, namebuf, sizeof(namebuf));
4409 dns_rdatatype_format(rdataset->type, typebuf,
4411 dns_rdataclass_format(rdataset->rdclass, classbuf,
4413 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
4414 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
4415 "check-names %s %s/%s/%s",
4416 fail ? "failure" : "warning",
4417 namebuf, typebuf, classbuf);
4419 if (ANSWER(rdataset)) {
4420 dns_db_detachnode(fctx->cache, &node);
4421 return (DNS_R_BADNAME);
4428 * Enforce the configure maximum cache TTL.
4430 if (rdataset->ttl > res->view->maxcachettl)
4431 rdataset->ttl = res->view->maxcachettl;
4434 * If this RRset is in a secure domain, is in bailiwick,
4435 * and is not glue, attempt DNSSEC validation. (We do not
4436 * attempt to validate glue or out-of-bailiwick data--even
4437 * though there might be some performance benefit to doing
4438 * so--because it makes it simpler and safer to ensure that
4439 * records from a secure domain are only cached if validated
4440 * within the context of a query to the domain that owns
4443 if (secure_domain && rdataset->trust != dns_trust_glue &&
4444 !EXTERNAL(rdataset)) {
4448 * RRSIGs are validated as part of validating the
4451 if (rdataset->type == dns_rdatatype_rrsig)
4454 * Find the SIG for this rdataset, if we have it.
4456 for (sigrdataset = ISC_LIST_HEAD(name->list);
4457 sigrdataset != NULL;
4458 sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4459 if (sigrdataset->type == dns_rdatatype_rrsig &&
4460 sigrdataset->covers == rdataset->type)
4463 if (sigrdataset == NULL) {
4464 if (!ANSWER(rdataset) && need_validation) {
4466 * Ignore non-answer rdatasets that
4467 * are missing signatures.
4474 * Normalize the rdataset and sigrdataset TTLs.
4476 if (sigrdataset != NULL) {
4477 rdataset->ttl = ISC_MIN(rdataset->ttl,
4479 sigrdataset->ttl = rdataset->ttl;
4483 * Cache this rdataset/sigrdataset pair as
4484 * pending data. Track whether it was additional
4487 if (rdataset->trust == dns_trust_additional)
4488 trust = dns_trust_pending_additional;
4490 trust = dns_trust_pending_answer;
4492 rdataset->trust = trust;
4493 if (sigrdataset != NULL)
4494 sigrdataset->trust = trust;
4495 if (!need_validation || !ANSWER(rdataset)) {
4496 addedrdataset = ardataset;
4497 result = dns_db_addrdataset(fctx->cache, node,
4498 NULL, now, rdataset,
4500 if (result == DNS_R_UNCHANGED) {
4501 result = ISC_R_SUCCESS;
4502 if (!need_validation &&
4503 ardataset != NULL &&
4504 NEGATIVE(ardataset)) {
4506 * The answer in the cache is
4507 * better than the answer we
4508 * found, and is a negative
4509 * cache entry, so we must set
4510 * eresult appropriately.
4512 if (NXDOMAIN(ardataset))
4514 DNS_R_NCACHENXDOMAIN;
4517 DNS_R_NCACHENXRRSET;
4519 * We have a negative response
4520 * from the cache so don't
4521 * attempt to add the RRSIG
4527 if (result != ISC_R_SUCCESS)
4529 if (sigrdataset != NULL) {
4530 addedrdataset = asigrdataset;
4531 result = dns_db_addrdataset(fctx->cache,
4535 if (result == DNS_R_UNCHANGED)
4536 result = ISC_R_SUCCESS;
4537 if (result != ISC_R_SUCCESS)
4539 } else if (!ANSWER(rdataset))
4543 if (ANSWER(rdataset) && need_validation) {
4544 if (fctx->type != dns_rdatatype_any &&
4545 fctx->type != dns_rdatatype_rrsig &&
4546 fctx->type != dns_rdatatype_sig) {
4548 * This is The Answer. We will
4549 * validate it, but first we cache
4550 * the rest of the response - it may
4551 * contain useful keys.
4553 INSIST(valrdataset == NULL &&
4554 valsigrdataset == NULL);
4555 valrdataset = rdataset;
4556 valsigrdataset = sigrdataset;
4559 * This is one of (potentially)
4560 * multiple answers to an ANY
4561 * or SIG query. To keep things
4562 * simple, we just start the
4563 * validator right away rather
4564 * than caching first and
4565 * having to remember which
4566 * rdatasets needed validation.
4568 result = valcreate(fctx, addrinfo,
4569 name, rdataset->type,
4574 * Defer any further validations.
4575 * This prevents multiple validators
4576 * from manipulating fctx->rmessage
4579 valoptions |= DNS_VALIDATOR_DEFER;
4581 } else if (CHAINING(rdataset)) {
4582 if (rdataset->type == dns_rdatatype_cname)
4583 eresult = DNS_R_CNAME;
4585 INSIST(rdataset->type ==
4586 dns_rdatatype_dname);
4587 eresult = DNS_R_DNAME;
4590 } else if (!EXTERNAL(rdataset)) {
4592 * It's OK to cache this rdataset now.
4594 if (ANSWER(rdataset))
4595 addedrdataset = ardataset;
4596 else if (ANSWERSIG(rdataset))
4597 addedrdataset = asigrdataset;
4599 addedrdataset = NULL;
4600 if (CHAINING(rdataset)) {
4601 if (rdataset->type == dns_rdatatype_cname)
4602 eresult = DNS_R_CNAME;
4604 INSIST(rdataset->type ==
4605 dns_rdatatype_dname);
4606 eresult = DNS_R_DNAME;
4609 if (rdataset->trust == dns_trust_glue &&
4610 (rdataset->type == dns_rdatatype_ns ||
4611 (rdataset->type == dns_rdatatype_rrsig &&
4612 rdataset->covers == dns_rdatatype_ns))) {
4614 * If the trust level is 'dns_trust_glue'
4615 * then we are adding data from a referral
4616 * we got while executing the search algorithm.
4617 * New referral data always takes precedence
4618 * over the existing cache contents.
4620 options = DNS_DBADD_FORCE;
4624 * Now we can add the rdataset.
4626 result = dns_db_addrdataset(fctx->cache,
4631 if (result == DNS_R_UNCHANGED) {
4632 if (ANSWER(rdataset) &&
4633 ardataset != NULL &&
4634 NEGATIVE(ardataset)) {
4636 * The answer in the cache is better
4637 * than the answer we found, and is
4638 * a negative cache entry, so we
4639 * must set eresult appropriately.
4641 if (NXDOMAIN(ardataset))
4642 eresult = DNS_R_NCACHENXDOMAIN;
4644 eresult = DNS_R_NCACHENXRRSET;
4646 result = ISC_R_SUCCESS;
4647 } else if (result != ISC_R_SUCCESS)
4652 if (valrdataset != NULL)
4653 result = valcreate(fctx, addrinfo, name, fctx->type,
4654 valrdataset, valsigrdataset, valoptions,
4657 if (result == ISC_R_SUCCESS && have_answer) {
4658 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4659 if (event != NULL) {
4661 * Negative results must be indicated in event->result.
4663 if (dns_rdataset_isassociated(event->rdataset) &&
4664 NEGATIVE(event->rdataset)) {
4665 INSIST(eresult == DNS_R_NCACHENXDOMAIN ||
4666 eresult == DNS_R_NCACHENXRRSET);
4668 event->result = eresult;
4669 dns_db_attach(fctx->cache, adbp);
4670 dns_db_transfernode(fctx->cache, &node, anodep);
4671 clone_results(fctx);
4676 dns_db_detachnode(fctx->cache, &node);
4681 static inline isc_result_t
4682 cache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_stdtime_t now)
4684 isc_result_t result;
4685 dns_section_t section;
4688 FCTXTRACE("cache_message");
4690 fctx->attributes &= ~FCTX_ATTR_WANTCACHE;
4692 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4694 for (section = DNS_SECTION_ANSWER;
4695 section <= DNS_SECTION_ADDITIONAL;
4697 result = dns_message_firstname(fctx->rmessage, section);
4698 while (result == ISC_R_SUCCESS) {
4700 dns_message_currentname(fctx->rmessage, section,
4702 if ((name->attributes & DNS_NAMEATTR_CACHE) != 0) {
4703 result = cache_name(fctx, name, addrinfo, now);
4704 if (result != ISC_R_SUCCESS)
4707 result = dns_message_nextname(fctx->rmessage, section);
4709 if (result != ISC_R_NOMORE)
4712 if (result == ISC_R_NOMORE)
4713 result = ISC_R_SUCCESS;
4715 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4721 * Do what dns_ncache_addoptout() does, and then compute an appropriate eresult.
4724 ncache_adderesult(dns_message_t *message, dns_db_t *cache, dns_dbnode_t *node,
4725 dns_rdatatype_t covers, isc_stdtime_t now, dns_ttl_t maxttl,
4726 isc_boolean_t optout, dns_rdataset_t *ardataset,
4727 isc_result_t *eresultp)
4729 isc_result_t result;
4730 dns_rdataset_t rdataset;
4732 if (ardataset == NULL) {
4733 dns_rdataset_init(&rdataset);
4734 ardataset = &rdataset;
4736 result = dns_ncache_addoptout(message, cache, node, covers, now,
4737 maxttl, optout, ardataset);
4738 if (result == DNS_R_UNCHANGED || result == ISC_R_SUCCESS) {
4740 * If the cache now contains a negative entry and we
4741 * care about whether it is DNS_R_NCACHENXDOMAIN or
4742 * DNS_R_NCACHENXRRSET then extract it.
4744 if (NEGATIVE(ardataset)) {
4746 * The cache data is a negative cache entry.
4748 if (NXDOMAIN(ardataset))
4749 *eresultp = DNS_R_NCACHENXDOMAIN;
4751 *eresultp = DNS_R_NCACHENXRRSET;
4754 * Either we don't care about the nature of the
4755 * cache rdataset (because no fetch is interested
4756 * in the outcome), or the cache rdataset is not
4757 * a negative cache entry. Whichever case it is,
4758 * we can return success.
4760 * XXXRTH There's a CNAME/DNAME problem here.
4762 *eresultp = ISC_R_SUCCESS;
4764 result = ISC_R_SUCCESS;
4766 if (ardataset == &rdataset && dns_rdataset_isassociated(ardataset))
4767 dns_rdataset_disassociate(ardataset);
4772 static inline isc_result_t
4773 ncache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
4774 dns_rdatatype_t covers, isc_stdtime_t now)
4776 isc_result_t result, eresult;
4778 dns_resolver_t *res;
4780 dns_dbnode_t *node, **anodep;
4781 dns_rdataset_t *ardataset;
4782 isc_boolean_t need_validation, secure_domain;
4784 dns_fetchevent_t *event;
4786 unsigned int valoptions = 0;
4788 FCTXTRACE("ncache_message");
4790 fctx->attributes &= ~FCTX_ATTR_WANTNCACHE;
4793 need_validation = ISC_FALSE;
4794 POST(need_validation);
4795 secure_domain = ISC_FALSE;
4796 eresult = ISC_R_SUCCESS;
4801 * XXXMPA remove when we follow cnames and adjust the setting
4802 * of FCTX_ATTR_WANTNCACHE in noanswer_response().
4804 INSIST(fctx->rmessage->counts[DNS_SECTION_ANSWER] == 0);
4807 * Is DNSSEC validation required for this name?
4809 if (fctx->res->view->enablevalidation) {
4810 result = dns_keytable_issecuredomain(res->view->secroots, name,
4812 if (result != ISC_R_SUCCESS)
4815 if (!secure_domain && res->view->dlv != NULL) {
4816 valoptions = DNS_VALIDATOR_DLV;
4817 secure_domain = ISC_TRUE;
4821 if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
4822 need_validation = ISC_FALSE;
4824 need_validation = secure_domain;
4826 if (secure_domain) {
4828 * Mark all rdatasets as pending.
4830 dns_rdataset_t *trdataset;
4833 result = dns_message_firstname(fctx->rmessage,
4834 DNS_SECTION_AUTHORITY);
4835 while (result == ISC_R_SUCCESS) {
4837 dns_message_currentname(fctx->rmessage,
4838 DNS_SECTION_AUTHORITY,
4840 for (trdataset = ISC_LIST_HEAD(tname->list);
4842 trdataset = ISC_LIST_NEXT(trdataset, link))
4843 trdataset->trust = dns_trust_pending_answer;
4844 result = dns_message_nextname(fctx->rmessage,
4845 DNS_SECTION_AUTHORITY);
4847 if (result != ISC_R_NOMORE)
4852 if (need_validation) {
4854 * Do negative response validation.
4856 result = valcreate(fctx, addrinfo, name, fctx->type,
4857 NULL, NULL, valoptions,
4858 res->buckets[fctx->bucketnum].task);
4860 * If validation is necessary, return now. Otherwise continue
4861 * to process the message, letting the validation complete
4862 * in its own good time.
4867 LOCK(&res->buckets[fctx->bucketnum].lock);
4873 if (!HAVE_ANSWER(fctx)) {
4874 event = ISC_LIST_HEAD(fctx->events);
4875 if (event != NULL) {
4877 aname = dns_fixedname_name(&event->foundname);
4878 result = dns_name_copy(name, aname, NULL);
4879 if (result != ISC_R_SUCCESS)
4881 anodep = &event->node;
4882 ardataset = event->rdataset;
4887 result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
4888 if (result != ISC_R_SUCCESS)
4892 * If we are asking for a SOA record set the cache time
4893 * to zero to facilitate locating the containing zone of
4896 ttl = fctx->res->view->maxncachettl;
4897 if (fctx->type == dns_rdatatype_soa &&
4898 covers == dns_rdatatype_any &&
4899 fctx->res->zero_no_soa_ttl)
4902 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
4903 covers, now, ttl, ISC_FALSE,
4904 ardataset, &eresult);
4905 if (result != ISC_R_SUCCESS)
4908 if (!HAVE_ANSWER(fctx)) {
4909 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4910 if (event != NULL) {
4911 event->result = eresult;
4912 dns_db_attach(fctx->cache, adbp);
4913 dns_db_transfernode(fctx->cache, &node, anodep);
4914 clone_results(fctx);
4919 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4922 dns_db_detachnode(fctx->cache, &node);
4928 mark_related(dns_name_t *name, dns_rdataset_t *rdataset,
4929 isc_boolean_t external, isc_boolean_t gluing)
4931 name->attributes |= DNS_NAMEATTR_CACHE;
4933 rdataset->trust = dns_trust_glue;
4935 * Glue with 0 TTL causes problems. We force the TTL to
4936 * 1 second to prevent this.
4938 if (rdataset->ttl == 0)
4941 rdataset->trust = dns_trust_additional;
4943 * Avoid infinite loops by only marking new rdatasets.
4945 if (!CACHE(rdataset)) {
4946 name->attributes |= DNS_NAMEATTR_CHASE;
4947 rdataset->attributes |= DNS_RDATASETATTR_CHASE;
4949 rdataset->attributes |= DNS_RDATASETATTR_CACHE;
4951 rdataset->attributes |= DNS_RDATASETATTR_EXTERNAL;
4955 check_related(void *arg, dns_name_t *addname, dns_rdatatype_t type) {
4956 fetchctx_t *fctx = arg;
4957 isc_result_t result;
4959 dns_rdataset_t *rdataset;
4960 isc_boolean_t external;
4961 dns_rdatatype_t rtype;
4962 isc_boolean_t gluing;
4964 REQUIRE(VALID_FCTX(fctx));
4972 result = dns_message_findname(fctx->rmessage, DNS_SECTION_ADDITIONAL,
4973 addname, dns_rdatatype_any, 0, &name,
4975 if (result == ISC_R_SUCCESS) {
4976 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
4977 if (type == dns_rdatatype_a) {
4978 for (rdataset = ISC_LIST_HEAD(name->list);
4980 rdataset = ISC_LIST_NEXT(rdataset, link)) {
4981 if (rdataset->type == dns_rdatatype_rrsig)
4982 rtype = rdataset->covers;
4984 rtype = rdataset->type;
4985 if (rtype == dns_rdatatype_a ||
4986 rtype == dns_rdatatype_aaaa)
4987 mark_related(name, rdataset, external,
4991 result = dns_message_findtype(name, type, 0,
4993 if (result == ISC_R_SUCCESS) {
4994 mark_related(name, rdataset, external, gluing);
4996 * Do we have its SIG too?
4999 result = dns_message_findtype(name,
5000 dns_rdatatype_rrsig,
5002 if (result == ISC_R_SUCCESS)
5003 mark_related(name, rdataset, external,
5009 return (ISC_R_SUCCESS);
5013 chase_additional(fetchctx_t *fctx) {
5014 isc_boolean_t rescan;
5015 dns_section_t section = DNS_SECTION_ADDITIONAL;
5016 isc_result_t result;
5021 for (result = dns_message_firstname(fctx->rmessage, section);
5022 result == ISC_R_SUCCESS;
5023 result = dns_message_nextname(fctx->rmessage, section)) {
5024 dns_name_t *name = NULL;
5025 dns_rdataset_t *rdataset;
5026 dns_message_currentname(fctx->rmessage, DNS_SECTION_ADDITIONAL,
5028 if ((name->attributes & DNS_NAMEATTR_CHASE) == 0)
5030 name->attributes &= ~DNS_NAMEATTR_CHASE;
5031 for (rdataset = ISC_LIST_HEAD(name->list);
5033 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5034 if (CHASE(rdataset)) {
5035 rdataset->attributes &= ~DNS_RDATASETATTR_CHASE;
5036 (void)dns_rdataset_additionaldata(rdataset,
5047 static inline isc_result_t
5048 cname_target(dns_rdataset_t *rdataset, dns_name_t *tname) {
5049 isc_result_t result;
5050 dns_rdata_t rdata = DNS_RDATA_INIT;
5051 dns_rdata_cname_t cname;
5053 result = dns_rdataset_first(rdataset);
5054 if (result != ISC_R_SUCCESS)
5056 dns_rdataset_current(rdataset, &rdata);
5057 result = dns_rdata_tostruct(&rdata, &cname, NULL);
5058 if (result != ISC_R_SUCCESS)
5060 dns_name_init(tname, NULL);
5061 dns_name_clone(&cname.cname, tname);
5062 dns_rdata_freestruct(&cname);
5064 return (ISC_R_SUCCESS);
5067 static inline isc_result_t
5068 dname_target(dns_rdataset_t *rdataset, dns_name_t *qname, dns_name_t *oname,
5069 dns_fixedname_t *fixeddname)
5071 isc_result_t result;
5072 dns_rdata_t rdata = DNS_RDATA_INIT;
5073 unsigned int nlabels;
5075 dns_namereln_t namereln;
5076 dns_rdata_dname_t dname;
5077 dns_fixedname_t prefix;
5080 * Get the target name of the DNAME.
5083 result = dns_rdataset_first(rdataset);
5084 if (result != ISC_R_SUCCESS)
5086 dns_rdataset_current(rdataset, &rdata);
5087 result = dns_rdata_tostruct(&rdata, &dname, NULL);
5088 if (result != ISC_R_SUCCESS)
5092 * Get the prefix of qname.
5094 namereln = dns_name_fullcompare(qname, oname, &order, &nlabels);
5095 if (namereln != dns_namereln_subdomain) {
5096 dns_rdata_freestruct(&dname);
5097 return (DNS_R_FORMERR);
5099 dns_fixedname_init(&prefix);
5100 dns_name_split(qname, nlabels, dns_fixedname_name(&prefix), NULL);
5101 dns_fixedname_init(fixeddname);
5102 result = dns_name_concatenate(dns_fixedname_name(&prefix),
5104 dns_fixedname_name(fixeddname), NULL);
5105 dns_rdata_freestruct(&dname);
5110 * Handle a no-answer response (NXDOMAIN, NXRRSET, or referral).
5111 * If bind8_ns_resp is ISC_TRUE, this is a suspected BIND 8
5112 * response to an NS query that should be treated as a referral
5113 * even though the NS records occur in the answer section
5114 * rather than the authority section.
5117 noanswer_response(fetchctx_t *fctx, dns_name_t *oqname,
5118 isc_boolean_t bind8_ns_resp)
5120 isc_result_t result;
5121 dns_message_t *message;
5122 dns_name_t *name, *qname, *ns_name, *soa_name, *ds_name;
5123 dns_rdataset_t *rdataset, *ns_rdataset;
5124 isc_boolean_t aa, negative_response;
5125 dns_rdatatype_t type;
5126 dns_section_t section =
5127 bind8_ns_resp ? DNS_SECTION_ANSWER : DNS_SECTION_AUTHORITY;
5129 FCTXTRACE("noanswer_response");
5131 message = fctx->rmessage;
5136 if (oqname == NULL) {
5138 * We have a normal, non-chained negative response or
5141 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
5145 qname = &fctx->name;
5148 * We're being invoked by answer_response() after it has
5149 * followed a CNAME/DNAME chain.
5154 * If the current qname is not a subdomain of the query
5155 * domain, there's no point in looking at the authority
5156 * section without doing DNSSEC validation.
5158 * Until we do that validation, we'll just return success
5161 if (!dns_name_issubdomain(qname, &fctx->domain))
5162 return (ISC_R_SUCCESS);
5166 * We have to figure out if this is a negative response, or a
5171 * Sometimes we can tell if its a negative response by looking at
5172 * the message header.
5174 negative_response = ISC_FALSE;
5175 if (message->rcode == dns_rcode_nxdomain ||
5176 (message->counts[DNS_SECTION_ANSWER] == 0 &&
5177 message->counts[DNS_SECTION_AUTHORITY] == 0))
5178 negative_response = ISC_TRUE;
5181 * Process the authority section.
5187 result = dns_message_firstname(message, section);
5188 while (result == ISC_R_SUCCESS) {
5190 dns_message_currentname(message, section, &name);
5191 if (dns_name_issubdomain(name, &fctx->domain)) {
5193 * Look for NS/SOA RRsets first.
5195 for (rdataset = ISC_LIST_HEAD(name->list);
5197 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5198 type = rdataset->type;
5199 if (type == dns_rdatatype_rrsig)
5200 type = rdataset->covers;
5201 if (((type == dns_rdatatype_ns ||
5202 type == dns_rdatatype_soa) &&
5203 !dns_name_issubdomain(qname, name)))
5204 return (DNS_R_FORMERR);
5205 if (type == dns_rdatatype_ns) {
5209 * Only one set of NS RRs is allowed.
5211 if (rdataset->type ==
5213 if (ns_name != NULL &&
5215 return (DNS_R_FORMERR);
5217 ns_rdataset = rdataset;
5221 rdataset->attributes |=
5222 DNS_RDATASETATTR_CACHE;
5223 rdataset->trust = dns_trust_glue;
5225 if (type == dns_rdatatype_soa) {
5227 * SOA, or RRSIG SOA.
5229 * Only one SOA is allowed.
5231 if (rdataset->type ==
5232 dns_rdatatype_soa) {
5233 if (soa_name != NULL &&
5235 return (DNS_R_FORMERR);
5239 DNS_NAMEATTR_NCACHE;
5240 rdataset->attributes |=
5241 DNS_RDATASETATTR_NCACHE;
5244 dns_trust_authauthority;
5247 dns_trust_additional;
5251 result = dns_message_nextname(message, section);
5252 if (result == ISC_R_NOMORE)
5254 else if (result != ISC_R_SUCCESS)
5259 * A negative response has a SOA record (Type 2)
5260 * and a optional NS RRset (Type 1) or it has neither
5261 * a SOA or a NS RRset (Type 3, handled above) or
5262 * rcode is NXDOMAIN (handled above) in which case
5263 * the NS RRset is allowed (Type 4).
5265 if (soa_name != NULL)
5266 negative_response = ISC_TRUE;
5268 result = dns_message_firstname(message, section);
5269 while (result == ISC_R_SUCCESS) {
5271 dns_message_currentname(message, section, &name);
5272 if (dns_name_issubdomain(name, &fctx->domain)) {
5273 for (rdataset = ISC_LIST_HEAD(name->list);
5275 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5276 type = rdataset->type;
5277 if (type == dns_rdatatype_rrsig)
5278 type = rdataset->covers;
5279 if (type == dns_rdatatype_nsec ||
5280 type == dns_rdatatype_nsec3) {
5282 * NSEC or RRSIG NSEC.
5284 if (negative_response) {
5286 DNS_NAMEATTR_NCACHE;
5287 rdataset->attributes |=
5288 DNS_RDATASETATTR_NCACHE;
5289 } else if (type == dns_rdatatype_nsec) {
5292 rdataset->attributes |=
5293 DNS_RDATASETATTR_CACHE;
5297 dns_trust_authauthority;
5300 dns_trust_additional;
5302 * No additional data needs to be
5305 } else if (type == dns_rdatatype_ds) {
5309 * These should only be here if
5310 * this is a referral, and there
5311 * should only be one DS.
5313 if (ns_name == NULL)
5314 return (DNS_R_FORMERR);
5315 if (rdataset->type ==
5317 if (ds_name != NULL &&
5319 return (DNS_R_FORMERR);
5324 rdataset->attributes |=
5325 DNS_RDATASETATTR_CACHE;
5328 dns_trust_authauthority;
5331 dns_trust_additional;
5335 result = dns_message_nextname(message, section);
5336 if (result == ISC_R_NOMORE)
5338 else if (result != ISC_R_SUCCESS)
5343 * Trigger lookups for DNS nameservers.
5345 if (negative_response && message->rcode == dns_rcode_noerror &&
5346 fctx->type == dns_rdatatype_ds && soa_name != NULL &&
5347 dns_name_equal(soa_name, qname) &&
5348 !dns_name_equal(qname, dns_rootname))
5349 return (DNS_R_CHASEDSSERVERS);
5352 * Did we find anything?
5354 if (!negative_response && ns_name == NULL) {
5358 if (oqname != NULL) {
5360 * We've already got a partial CNAME/DNAME chain,
5361 * and haven't found else anything useful here, but
5362 * no error has occurred since we have an answer.
5364 return (ISC_R_SUCCESS);
5367 * The responder is insane.
5369 return (DNS_R_FORMERR);
5374 * If we found both NS and SOA, they should be the same name.
5376 if (ns_name != NULL && soa_name != NULL && ns_name != soa_name)
5377 return (DNS_R_FORMERR);
5380 * Do we have a referral? (We only want to follow a referral if
5381 * we're not following a chain.)
5383 if (!negative_response && ns_name != NULL && oqname == NULL) {
5385 * We already know ns_name is a subdomain of fctx->domain.
5386 * If ns_name is equal to fctx->domain, we're not making
5387 * progress. We return DNS_R_FORMERR so that we'll keep
5388 * trying other servers.
5390 if (dns_name_equal(ns_name, &fctx->domain))
5391 return (DNS_R_FORMERR);
5394 * If the referral name is not a parent of the query
5395 * name, consider the responder insane.
5397 if (! dns_name_issubdomain(&fctx->name, ns_name)) {
5398 FCTXTRACE("referral to non-parent");
5399 return (DNS_R_FORMERR);
5403 * Mark any additional data related to this rdataset.
5404 * It's important that we do this before we change the
5407 INSIST(ns_rdataset != NULL);
5408 fctx->attributes |= FCTX_ATTR_GLUING;
5409 (void)dns_rdataset_additionaldata(ns_rdataset, check_related,
5411 fctx->attributes &= ~FCTX_ATTR_GLUING;
5413 * NS rdatasets with 0 TTL cause problems.
5414 * dns_view_findzonecut() will not find them when we
5415 * try to follow the referral, and we'll SERVFAIL
5416 * because the best nameservers are now above QDOMAIN.
5417 * We force the TTL to 1 second to prevent this.
5419 if (ns_rdataset->ttl == 0)
5420 ns_rdataset->ttl = 1;
5422 * Set the current query domain to the referral name.
5424 * XXXRTH We should check if we're in forward-only mode, and
5425 * if so we should bail out.
5427 INSIST(dns_name_countlabels(&fctx->domain) > 0);
5428 dns_name_free(&fctx->domain,
5429 fctx->res->buckets[fctx->bucketnum].mctx);
5430 if (dns_rdataset_isassociated(&fctx->nameservers))
5431 dns_rdataset_disassociate(&fctx->nameservers);
5432 dns_name_init(&fctx->domain, NULL);
5433 result = dns_name_dup(ns_name,
5434 fctx->res->buckets[fctx->bucketnum].mctx,
5436 if (result != ISC_R_SUCCESS)
5438 fctx->attributes |= FCTX_ATTR_WANTCACHE;
5439 return (DNS_R_DELEGATION);
5443 * Since we're not doing a referral, we don't want to cache any
5444 * NS RRs we may have found.
5446 if (ns_name != NULL)
5447 ns_name->attributes &= ~DNS_NAMEATTR_CACHE;
5449 if (negative_response && oqname == NULL)
5450 fctx->attributes |= FCTX_ATTR_WANTNCACHE;
5452 return (ISC_R_SUCCESS);
5456 answer_response(fetchctx_t *fctx) {
5457 isc_result_t result;
5458 dns_message_t *message;
5459 dns_name_t *name, *qname, tname;
5460 dns_rdataset_t *rdataset;
5461 isc_boolean_t done, external, chaining, aa, found, want_chaining;
5462 isc_boolean_t have_answer, found_cname, found_type, wanted_chaining;
5464 dns_rdatatype_t type;
5465 dns_fixedname_t dname, fqname;
5467 FCTXTRACE("answer_response");
5469 message = fctx->rmessage;
5472 * Examine the answer section, marking those rdatasets which are
5473 * part of the answer and should be cached.
5477 found_cname = ISC_FALSE;
5478 found_type = ISC_FALSE;
5479 chaining = ISC_FALSE;
5480 have_answer = ISC_FALSE;
5481 want_chaining = ISC_FALSE;
5482 POST(want_chaining);
5483 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
5487 qname = &fctx->name;
5489 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
5490 while (!done && result == ISC_R_SUCCESS) {
5492 dns_message_currentname(message, DNS_SECTION_ANSWER, &name);
5493 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
5494 if (dns_name_equal(name, qname)) {
5495 wanted_chaining = ISC_FALSE;
5496 for (rdataset = ISC_LIST_HEAD(name->list);
5498 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5500 want_chaining = ISC_FALSE;
5502 if (rdataset->type == dns_rdatatype_nsec3) {
5504 * NSEC3 records are not allowed to
5505 * appear in the answer section.
5507 return (DNS_R_FORMERR);
5509 if (rdataset->type == type && !found_cname) {
5511 * We've found an ordinary answer.
5514 found_type = ISC_TRUE;
5516 aflag = DNS_RDATASETATTR_ANSWER;
5517 } else if (type == dns_rdatatype_any) {
5519 * We've found an answer matching
5520 * an ANY query. There may be
5524 aflag = DNS_RDATASETATTR_ANSWER;
5525 } else if (rdataset->type == dns_rdatatype_rrsig
5526 && rdataset->covers == type
5529 * We've found a signature that
5530 * covers the type we're looking for.
5533 found_type = ISC_TRUE;
5534 aflag = DNS_RDATASETATTR_ANSWERSIG;
5535 } else if (rdataset->type ==
5539 * We're looking for something else,
5540 * but we found a CNAME.
5542 * Getting a CNAME response for some
5543 * query types is an error.
5545 if (type == dns_rdatatype_rrsig ||
5546 type == dns_rdatatype_dnskey ||
5547 type == dns_rdatatype_nsec)
5548 return (DNS_R_FORMERR);
5550 found_cname = ISC_TRUE;
5551 want_chaining = ISC_TRUE;
5552 aflag = DNS_RDATASETATTR_ANSWER;
5553 result = cname_target(rdataset,
5555 if (result != ISC_R_SUCCESS)
5557 } else if (rdataset->type == dns_rdatatype_rrsig
5558 && rdataset->covers ==
5562 * We're looking for something else,
5563 * but we found a SIG CNAME.
5566 found_cname = ISC_TRUE;
5567 aflag = DNS_RDATASETATTR_ANSWERSIG;
5572 * We've found an answer to our
5577 rdataset->attributes |=
5578 DNS_RDATASETATTR_CACHE;
5579 rdataset->trust = dns_trust_answer;
5582 * This data is "the" answer
5583 * to our question only if
5584 * we're not chaining (i.e.
5585 * if we haven't followed
5586 * a CNAME or DNAME).
5590 DNS_RDATASETATTR_ANSWER)
5591 have_answer = ISC_TRUE;
5593 DNS_NAMEATTR_ANSWER;
5594 rdataset->attributes |= aflag;
5597 dns_trust_authanswer;
5598 } else if (external) {
5600 * This data is outside of
5601 * our query domain, and
5602 * may not be cached.
5604 rdataset->attributes |=
5605 DNS_RDATASETATTR_EXTERNAL;
5609 * Mark any additional data related
5612 (void)dns_rdataset_additionaldata(
5620 if (want_chaining) {
5621 wanted_chaining = ISC_TRUE;
5623 DNS_NAMEATTR_CHAINING;
5624 rdataset->attributes |=
5625 DNS_RDATASETATTR_CHAINING;
5630 * We could add an "else" clause here and
5631 * log that we're ignoring this rdataset.
5635 * If wanted_chaining is true, we've done
5636 * some chaining as the result of processing
5637 * this node, and thus we need to set
5640 * We don't set chaining inside of the
5641 * rdataset loop because doing that would
5642 * cause us to ignore the signatures of
5645 if (wanted_chaining)
5646 chaining = ISC_TRUE;
5649 * Look for a DNAME (or its SIG). Anything else is
5652 wanted_chaining = ISC_FALSE;
5653 for (rdataset = ISC_LIST_HEAD(name->list);
5655 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5656 isc_boolean_t found_dname = ISC_FALSE;
5659 if (rdataset->type == dns_rdatatype_dname) {
5661 * We're looking for something else,
5662 * but we found a DNAME.
5664 * If we're not chaining, then the
5665 * DNAME should not be external.
5667 if (!chaining && external)
5668 return (DNS_R_FORMERR);
5670 want_chaining = ISC_TRUE;
5671 POST(want_chaining);
5672 aflag = DNS_RDATASETATTR_ANSWER;
5673 result = dname_target(rdataset,
5676 if (result == ISC_R_NOSPACE) {
5678 * We can't construct the
5679 * DNAME target. Do not
5682 want_chaining = ISC_FALSE;
5683 POST(want_chaining);
5684 } else if (result != ISC_R_SUCCESS)
5687 found_dname = ISC_TRUE;
5688 } else if (rdataset->type == dns_rdatatype_rrsig
5689 && rdataset->covers ==
5690 dns_rdatatype_dname) {
5692 * We've found a signature that
5696 aflag = DNS_RDATASETATTR_ANSWERSIG;
5701 * We've found an answer to our
5706 rdataset->attributes |=
5707 DNS_RDATASETATTR_CACHE;
5708 rdataset->trust = dns_trust_answer;
5711 * This data is "the" answer
5712 * to our question only if
5713 * we're not chaining.
5717 DNS_RDATASETATTR_ANSWER)
5718 have_answer = ISC_TRUE;
5720 DNS_NAMEATTR_ANSWER;
5721 rdataset->attributes |= aflag;
5724 dns_trust_authanswer;
5725 } else if (external) {
5726 rdataset->attributes |=
5727 DNS_RDATASETATTR_EXTERNAL;
5735 * Copy the dname into the
5738 * Although we check for
5739 * failure of the copy
5740 * operation, in practice it
5741 * should never fail since
5742 * we already know that the
5743 * result fits in a fixedname.
5745 dns_fixedname_init(&fqname);
5746 result = dns_name_copy(
5747 dns_fixedname_name(&dname),
5748 dns_fixedname_name(&fqname),
5750 if (result != ISC_R_SUCCESS)
5752 wanted_chaining = ISC_TRUE;
5754 DNS_NAMEATTR_CHAINING;
5755 rdataset->attributes |=
5756 DNS_RDATASETATTR_CHAINING;
5757 qname = dns_fixedname_name(
5762 if (wanted_chaining)
5763 chaining = ISC_TRUE;
5765 result = dns_message_nextname(message, DNS_SECTION_ANSWER);
5767 if (result == ISC_R_NOMORE)
5768 result = ISC_R_SUCCESS;
5769 if (result != ISC_R_SUCCESS)
5773 * We should have found an answer.
5776 return (DNS_R_FORMERR);
5779 * This response is now potentially cacheable.
5781 fctx->attributes |= FCTX_ATTR_WANTCACHE;
5784 * Did chaining end before we got the final answer?
5788 * Yes. This may be a negative reply, so hand off
5789 * authority section processing to the noanswer code.
5790 * If it isn't a noanswer response, no harm will be
5793 return (noanswer_response(fctx, qname, ISC_FALSE));
5797 * We didn't end with an incomplete chain, so the rcode should be
5800 if (message->rcode != dns_rcode_noerror)
5801 return (DNS_R_FORMERR);
5804 * Examine the authority section (if there is one).
5806 * We expect there to be only one owner name for all the rdatasets
5807 * in this section, and we expect that it is not external.
5810 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
5811 while (!done && result == ISC_R_SUCCESS) {
5813 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
5814 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
5817 * We expect to find NS or SIG NS rdatasets, and
5820 for (rdataset = ISC_LIST_HEAD(name->list);
5822 rdataset = ISC_LIST_NEXT(rdataset, link)) {
5823 if (rdataset->type == dns_rdatatype_ns ||
5824 (rdataset->type == dns_rdatatype_rrsig &&
5825 rdataset->covers == dns_rdatatype_ns)) {
5828 rdataset->attributes |=
5829 DNS_RDATASETATTR_CACHE;
5830 if (aa && !chaining)
5832 dns_trust_authauthority;
5835 dns_trust_additional;
5838 * Mark any additional data related
5841 (void)dns_rdataset_additionaldata(
5849 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
5851 if (result == ISC_R_NOMORE)
5852 result = ISC_R_SUCCESS;
5857 static isc_boolean_t
5858 fctx_decreference(fetchctx_t *fctx) {
5859 isc_boolean_t bucket_empty = ISC_FALSE;
5861 INSIST(fctx->references > 0);
5863 if (fctx->references == 0) {
5865 * No one cares about the result of this fetch anymore.
5867 if (fctx->pending == 0 && fctx->nqueries == 0 &&
5868 ISC_LIST_EMPTY(fctx->validators) && SHUTTINGDOWN(fctx)) {
5870 * This fctx is already shutdown; we were just
5871 * waiting for the last reference to go away.
5873 bucket_empty = fctx_destroy(fctx);
5876 * Initiate shutdown.
5878 fctx_shutdown(fctx);
5881 return (bucket_empty);
5885 resume_dslookup(isc_task_t *task, isc_event_t *event) {
5886 dns_fetchevent_t *fevent;
5887 dns_resolver_t *res;
5889 isc_result_t result;
5890 isc_boolean_t bucket_empty;
5891 isc_boolean_t locked = ISC_FALSE;
5892 unsigned int bucketnum;
5893 dns_rdataset_t nameservers;
5894 dns_fixedname_t fixed;
5897 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
5898 fevent = (dns_fetchevent_t *)event;
5899 fctx = event->ev_arg;
5900 REQUIRE(VALID_FCTX(fctx));
5904 FCTXTRACE("resume_dslookup");
5906 if (fevent->node != NULL)
5907 dns_db_detachnode(fevent->db, &fevent->node);
5908 if (fevent->db != NULL)
5909 dns_db_detach(&fevent->db);
5911 dns_rdataset_init(&nameservers);
5913 bucketnum = fctx->bucketnum;
5914 if (fevent->result == ISC_R_CANCELED) {
5915 dns_resolver_destroyfetch(&fctx->nsfetch);
5916 fctx_done(fctx, ISC_R_CANCELED, __LINE__);
5917 } else if (fevent->result == ISC_R_SUCCESS) {
5919 FCTXTRACE("resuming DS lookup");
5921 dns_resolver_destroyfetch(&fctx->nsfetch);
5922 if (dns_rdataset_isassociated(&fctx->nameservers))
5923 dns_rdataset_disassociate(&fctx->nameservers);
5924 dns_rdataset_clone(fevent->rdataset, &fctx->nameservers);
5925 dns_name_free(&fctx->domain,
5926 fctx->res->buckets[bucketnum].mctx);
5927 dns_name_init(&fctx->domain, NULL);
5928 result = dns_name_dup(&fctx->nsname,
5929 fctx->res->buckets[bucketnum].mctx,
5931 if (result != ISC_R_SUCCESS) {
5932 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
5938 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
5941 dns_rdataset_t *nsrdataset = NULL;
5944 * Retrieve state from fctx->nsfetch before we destroy it.
5946 dns_fixedname_init(&fixed);
5947 domain = dns_fixedname_name(&fixed);
5948 dns_name_copy(&fctx->nsfetch->private->domain, domain, NULL);
5949 if (dns_name_equal(&fctx->nsname, domain)) {
5950 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
5951 dns_resolver_destroyfetch(&fctx->nsfetch);
5954 if (dns_rdataset_isassociated(
5955 &fctx->nsfetch->private->nameservers)) {
5957 &fctx->nsfetch->private->nameservers,
5959 nsrdataset = &nameservers;
5962 dns_resolver_destroyfetch(&fctx->nsfetch);
5963 n = dns_name_countlabels(&fctx->nsname);
5964 dns_name_getlabelsequence(&fctx->nsname, 1, n - 1,
5967 if (dns_rdataset_isassociated(fevent->rdataset))
5968 dns_rdataset_disassociate(fevent->rdataset);
5969 FCTXTRACE("continuing to look for parent's NS records");
5970 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
5971 dns_rdatatype_ns, domain,
5972 nsrdataset, NULL, 0, task,
5973 resume_dslookup, fctx,
5974 &fctx->nsrrset, NULL,
5976 if (result != ISC_R_SUCCESS)
5977 fctx_done(fctx, result, __LINE__);
5979 LOCK(&res->buckets[bucketnum].lock);
5986 if (dns_rdataset_isassociated(&nameservers))
5987 dns_rdataset_disassociate(&nameservers);
5988 if (dns_rdataset_isassociated(fevent->rdataset))
5989 dns_rdataset_disassociate(fevent->rdataset);
5990 INSIST(fevent->sigrdataset == NULL);
5991 isc_event_free(&event);
5993 LOCK(&res->buckets[bucketnum].lock);
5994 bucket_empty = fctx_decreference(fctx);
5995 UNLOCK(&res->buckets[bucketnum].lock);
6001 checknamessection(dns_message_t *message, dns_section_t section) {
6002 isc_result_t result;
6004 dns_rdata_t rdata = DNS_RDATA_INIT;
6005 dns_rdataset_t *rdataset;
6007 for (result = dns_message_firstname(message, section);
6008 result == ISC_R_SUCCESS;
6009 result = dns_message_nextname(message, section))
6012 dns_message_currentname(message, section, &name);
6013 for (rdataset = ISC_LIST_HEAD(name->list);
6015 rdataset = ISC_LIST_NEXT(rdataset, link)) {
6016 for (result = dns_rdataset_first(rdataset);
6017 result == ISC_R_SUCCESS;
6018 result = dns_rdataset_next(rdataset)) {
6019 dns_rdataset_current(rdataset, &rdata);
6020 if (!dns_rdata_checkowner(name, rdata.rdclass,
6023 !dns_rdata_checknames(&rdata, name, NULL))
6025 rdataset->attributes |=
6026 DNS_RDATASETATTR_CHECKNAMES;
6028 dns_rdata_reset(&rdata);
6035 checknames(dns_message_t *message) {
6037 checknamessection(message, DNS_SECTION_ANSWER);
6038 checknamessection(message, DNS_SECTION_AUTHORITY);
6039 checknamessection(message, DNS_SECTION_ADDITIONAL);
6043 * Log server NSID at log level 'level'
6046 log_nsid(dns_rdataset_t *opt, resquery_t *query, int level, isc_mem_t *mctx)
6048 static const char hex[17] = "0123456789abcdef";
6049 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
6050 isc_uint16_t optcode, nsid_len, buflen, i;
6051 isc_result_t result;
6052 isc_buffer_t nsidbuf;
6054 unsigned char *p, *buf, *nsid;
6056 /* Extract rdata from OPT rdataset */
6057 result = dns_rdataset_first(opt);
6058 if (result != ISC_R_SUCCESS)
6059 return (ISC_R_FAILURE);
6061 dns_rdata_init(&rdata);
6062 dns_rdataset_current(opt, &rdata);
6063 if (rdata.length < 4)
6064 return (ISC_R_FAILURE);
6066 /* Check for NSID */
6067 isc_buffer_init(&nsidbuf, rdata.data, rdata.length);
6068 isc_buffer_add(&nsidbuf, rdata.length);
6069 optcode = isc_buffer_getuint16(&nsidbuf);
6070 nsid_len = isc_buffer_getuint16(&nsidbuf);
6071 if (optcode != DNS_OPT_NSID || nsid_len == 0)
6072 return (ISC_R_FAILURE);
6074 /* Allocate buffer for storing hex version of the NSID */
6075 buflen = nsid_len * 2 + 1;
6076 buf = isc_mem_get(mctx, buflen);
6078 return (ISC_R_NOSPACE);
6080 /* Convert to hex */
6082 nsid = rdata.data + 4;
6083 for (i = 0; i < nsid_len; i++) {
6084 *p++ = hex[(nsid[0] >> 4) & 0xf];
6085 *p++ = hex[nsid[0] & 0xf];
6090 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
6092 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6093 DNS_LOGMODULE_RESOLVER, level,
6094 "received NSID '%s' from %s", buf, addrbuf);
6097 isc_mem_put(mctx, buf, buflen);
6098 return (ISC_R_SUCCESS);
6102 log_packet(dns_message_t *message, int level, isc_mem_t *mctx) {
6103 isc_buffer_t buffer;
6106 isc_result_t result;
6108 if (! isc_log_wouldlog(dns_lctx, level))
6112 * Note that these are multiline debug messages. We want a newline
6113 * to appear in the log after each message.
6117 buf = isc_mem_get(mctx, len);
6120 isc_buffer_init(&buffer, buf, len);
6121 result = dns_message_totext(message, &dns_master_style_debug,
6123 if (result == ISC_R_NOSPACE) {
6124 isc_mem_put(mctx, buf, len);
6126 } else if (result == ISC_R_SUCCESS)
6127 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6128 DNS_LOGMODULE_RESOLVER, level,
6129 "received packet:\n%.*s",
6130 (int)isc_buffer_usedlength(&buffer),
6132 } while (result == ISC_R_NOSPACE);
6135 isc_mem_put(mctx, buf, len);
6139 resquery_response(isc_task_t *task, isc_event_t *event) {
6140 isc_result_t result = ISC_R_SUCCESS;
6141 resquery_t *query = event->ev_arg;
6142 dns_dispatchevent_t *devent = (dns_dispatchevent_t *)event;
6143 isc_boolean_t keep_trying, get_nameservers, resend;
6144 isc_boolean_t truncated;
6145 dns_message_t *message;
6146 dns_rdataset_t *opt;
6149 dns_fixedname_t foundname;
6151 isc_time_t tnow, *finish;
6152 dns_adbaddrinfo_t *addrinfo;
6153 unsigned int options;
6154 unsigned int findoptions;
6155 isc_result_t broken_server;
6156 badnstype_t broken_type = badns_response;
6157 isc_boolean_t no_response;
6159 REQUIRE(VALID_QUERY(query));
6161 options = query->options;
6162 REQUIRE(VALID_FCTX(fctx));
6163 REQUIRE(event->ev_type == DNS_EVENT_DISPATCH);
6167 if (isc_sockaddr_pf(&query->addrinfo->sockaddr) == PF_INET)
6168 inc_stats(fctx->res, dns_resstatscounter_responsev4);
6170 inc_stats(fctx->res, dns_resstatscounter_responsev6);
6172 (void)isc_timer_touch(fctx->timer);
6174 keep_trying = ISC_FALSE;
6175 broken_server = ISC_R_SUCCESS;
6176 get_nameservers = ISC_FALSE;
6178 truncated = ISC_FALSE;
6180 no_response = ISC_FALSE;
6182 if (fctx->res->exiting) {
6183 result = ISC_R_SHUTTINGDOWN;
6188 fctx->timeout = ISC_FALSE;
6191 * XXXRTH We should really get the current time just once. We
6192 * need a routine to convert from an isc_time_t to an
6197 isc_stdtime_get(&now);
6200 * Did the dispatcher have a problem?
6202 if (devent->result != ISC_R_SUCCESS) {
6203 if (devent->result == ISC_R_EOF &&
6204 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6206 * The problem might be that they
6207 * don't understand EDNS0. Turn it
6208 * off and try again.
6210 options |= DNS_FETCHOPT_NOEDNS0;
6213 * Remember that they don't like EDNS0.
6215 dns_adb_changeflags(fctx->adb,
6217 DNS_FETCHOPT_NOEDNS0,
6218 DNS_FETCHOPT_NOEDNS0);
6221 * There's no hope for this query.
6223 keep_trying = ISC_TRUE;
6226 * If this is a network error on an exclusive query
6227 * socket, mark the server as bad so that we won't try
6228 * it for this fetch again. Also adjust finish and
6229 * no_response so that we penalize this address in SRTT
6232 if (query->exclusivesocket &&
6233 (devent->result == ISC_R_HOSTUNREACH ||
6234 devent->result == ISC_R_NETUNREACH ||
6235 devent->result == ISC_R_CONNREFUSED ||
6236 devent->result == ISC_R_CANCELED)) {
6237 broken_server = devent->result;
6238 broken_type = badns_unreachable;
6240 no_response = ISC_TRUE;
6246 message = fctx->rmessage;
6248 if (query->tsig != NULL) {
6249 result = dns_message_setquerytsig(message, query->tsig);
6250 if (result != ISC_R_SUCCESS)
6254 if (query->tsigkey) {
6255 result = dns_message_settsigkey(message, query->tsigkey);
6256 if (result != ISC_R_SUCCESS)
6260 result = dns_message_parse(message, &devent->buffer, 0);
6261 if (result != ISC_R_SUCCESS) {
6263 case ISC_R_UNEXPECTEDEND:
6264 if (!message->question_ok ||
6265 (message->flags & DNS_MESSAGEFLAG_TC) == 0 ||
6266 (options & DNS_FETCHOPT_TCP) != 0) {
6268 * Either the message ended prematurely,
6269 * and/or wasn't marked as being truncated,
6270 * and/or this is a response to a query we
6271 * sent over TCP. In all of these cases,
6272 * something is wrong with the remote
6273 * server and we don't want to retry using
6276 if ((query->options & DNS_FETCHOPT_NOEDNS0)
6279 * The problem might be that they
6280 * don't understand EDNS0. Turn it
6281 * off and try again.
6283 options |= DNS_FETCHOPT_NOEDNS0;
6286 * Remember that they don't like EDNS0.
6288 dns_adb_changeflags(
6291 DNS_FETCHOPT_NOEDNS0,
6292 DNS_FETCHOPT_NOEDNS0);
6293 inc_stats(fctx->res,
6294 dns_resstatscounter_edns0fail);
6296 broken_server = result;
6297 keep_trying = ISC_TRUE;
6302 * We defer retrying via TCP for a bit so we can
6303 * check out this message further.
6305 truncated = ISC_TRUE;
6308 if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6310 * The problem might be that they
6311 * don't understand EDNS0. Turn it
6312 * off and try again.
6314 options |= DNS_FETCHOPT_NOEDNS0;
6317 * Remember that they don't like EDNS0.
6319 dns_adb_changeflags(fctx->adb,
6321 DNS_FETCHOPT_NOEDNS0,
6322 DNS_FETCHOPT_NOEDNS0);
6323 inc_stats(fctx->res,
6324 dns_resstatscounter_edns0fail);
6326 broken_server = DNS_R_UNEXPECTEDRCODE;
6327 keep_trying = ISC_TRUE;
6332 * Something bad has happened.
6340 * Log the incoming packet.
6342 log_packet(message, ISC_LOG_DEBUG(10), fctx->res->mctx);
6345 * Did we request NSID? If so, and if the response contains
6346 * NSID data, log it at INFO level.
6348 opt = dns_message_getopt(message);
6349 if (opt != NULL && (query->options & DNS_FETCHOPT_WANTNSID) != 0)
6350 log_nsid(opt, query, ISC_LOG_INFO, fctx->res->mctx);
6353 * If the message is signed, check the signature. If not, this
6354 * returns success anyway.
6356 result = dns_message_checksig(message, fctx->res->view);
6357 if (result != ISC_R_SUCCESS)
6361 * The dispatcher should ensure we only get responses with QR set.
6363 INSIST((message->flags & DNS_MESSAGEFLAG_QR) != 0);
6365 * INSIST() that the message comes from the place we sent it to,
6366 * since the dispatch code should ensure this.
6368 * INSIST() that the message id is correct (this should also be
6369 * ensured by the dispatch code).
6373 * We have an affirmative response to the query and we have
6374 * previously got a response from this server which indicated
6375 * EDNS may not be supported so we can now cache the lack of
6379 (message->rcode == dns_rcode_noerror ||
6380 message->rcode == dns_rcode_nxdomain ||
6381 message->rcode == dns_rcode_refused ||
6382 message->rcode == dns_rcode_yxdomain) &&
6383 bad_edns(fctx, &query->addrinfo->sockaddr)) {
6384 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
6385 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
6387 dns_adb_changeflags(fctx->adb, query->addrinfo,
6388 DNS_FETCHOPT_NOEDNS0,
6389 DNS_FETCHOPT_NOEDNS0);
6393 * Deal with truncated responses by retrying using TCP.
6395 if ((message->flags & DNS_MESSAGEFLAG_TC) != 0)
6396 truncated = ISC_TRUE;
6399 inc_stats(fctx->res, dns_resstatscounter_truncated);
6400 if ((options & DNS_FETCHOPT_TCP) != 0) {
6401 broken_server = DNS_R_TRUNCATEDTCP;
6402 keep_trying = ISC_TRUE;
6404 options |= DNS_FETCHOPT_TCP;
6411 * Is it a query response?
6413 if (message->opcode != dns_opcode_query) {
6415 broken_server = DNS_R_UNEXPECTEDOPCODE;
6416 keep_trying = ISC_TRUE;
6421 * Update statistics about erroneous responses.
6423 if (message->rcode != dns_rcode_noerror) {
6424 switch (message->rcode) {
6425 case dns_rcode_nxdomain:
6426 inc_stats(fctx->res, dns_resstatscounter_nxdomain);
6428 case dns_rcode_servfail:
6429 inc_stats(fctx->res, dns_resstatscounter_servfail);
6431 case dns_rcode_formerr:
6432 inc_stats(fctx->res, dns_resstatscounter_formerr);
6435 inc_stats(fctx->res, dns_resstatscounter_othererror);
6441 * Is the remote server broken, or does it dislike us?
6443 if (message->rcode != dns_rcode_noerror &&
6444 message->rcode != dns_rcode_nxdomain) {
6445 if (((message->rcode == dns_rcode_formerr ||
6446 message->rcode == dns_rcode_notimp) ||
6447 (message->rcode == dns_rcode_servfail &&
6448 dns_message_getopt(message) == NULL)) &&
6449 (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6451 * It's very likely they don't like EDNS0.
6452 * If the response code is SERVFAIL, also check if the
6453 * response contains an OPT RR and don't cache the
6454 * failure since it can be returned for various other
6457 * XXXRTH We should check if the question
6458 * we're asking requires EDNS0, and
6459 * if so, we should bail out.
6461 options |= DNS_FETCHOPT_NOEDNS0;
6464 * Remember that they may not like EDNS0.
6466 add_bad_edns(fctx, &query->addrinfo->sockaddr);
6467 inc_stats(fctx->res, dns_resstatscounter_edns0fail);
6468 } else if (message->rcode == dns_rcode_formerr) {
6469 if (ISFORWARDER(query->addrinfo)) {
6471 * This forwarder doesn't understand us,
6472 * but other forwarders might. Keep trying.
6474 broken_server = DNS_R_REMOTEFORMERR;
6475 keep_trying = ISC_TRUE;
6478 * The server doesn't understand us. Since
6479 * all servers for a zone need similar
6480 * capabilities, we assume that we will get
6481 * FORMERR from all servers, and thus we
6482 * cannot make any more progress with this
6485 result = DNS_R_FORMERR;
6487 } else if (message->rcode == dns_rcode_yxdomain) {
6489 * DNAME mapping failed because the new name
6490 * was too long. There's no chance of success
6493 result = DNS_R_YXDOMAIN;
6494 } else if (message->rcode == dns_rcode_badvers) {
6495 unsigned int flags, mask;
6496 unsigned int version;
6499 INSIST(opt != NULL);
6500 version = (opt->ttl >> 16) & 0xff;
6501 flags = (version << DNS_FETCHOPT_EDNSVERSIONSHIFT) |
6502 DNS_FETCHOPT_EDNSVERSIONSET;
6503 mask = DNS_FETCHOPT_EDNSVERSIONMASK |
6504 DNS_FETCHOPT_EDNSVERSIONSET;
6507 dns_adb_changeflags(fctx->adb, query->addrinfo,
6511 broken_server = DNS_R_BADVERS;
6512 keep_trying = ISC_TRUE;
6519 broken_server = DNS_R_UNEXPECTEDRCODE;
6520 INSIST(broken_server != ISC_R_SUCCESS);
6521 keep_trying = ISC_TRUE;
6527 * Is the question the same as the one we asked?
6529 result = same_question(fctx);
6530 if (result != ISC_R_SUCCESS) {
6532 if (result == DNS_R_FORMERR)
6533 keep_trying = ISC_TRUE;
6538 * Is the server lame?
6540 if (fctx->res->lame_ttl != 0 && !ISFORWARDER(query->addrinfo) &&
6542 inc_stats(fctx->res, dns_resstatscounter_lame);
6543 log_lame(fctx, query->addrinfo);
6544 result = dns_adb_marklame(fctx->adb, query->addrinfo,
6545 &fctx->name, fctx->type,
6546 now + fctx->res->lame_ttl);
6547 if (result != ISC_R_SUCCESS)
6548 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6549 DNS_LOGMODULE_RESOLVER, ISC_LOG_ERROR,
6550 "could not mark server as lame: %s",
6551 isc_result_totext(result));
6552 broken_server = DNS_R_LAME;
6553 keep_trying = ISC_TRUE;
6558 * Enforce delegations only zones like NET and COM.
6560 if (!ISFORWARDER(query->addrinfo) &&
6561 dns_view_isdelegationonly(fctx->res->view, &fctx->domain) &&
6562 !dns_name_equal(&fctx->domain, &fctx->name) &&
6563 fix_mustbedelegationornxdomain(message, fctx)) {
6564 char namebuf[DNS_NAME_FORMATSIZE];
6565 char domainbuf[DNS_NAME_FORMATSIZE];
6566 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
6570 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
6571 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
6572 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
6573 dns_rdataclass_format(fctx->res->rdclass, classbuf,
6575 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
6578 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DELEGATION_ONLY,
6579 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
6580 "enforced delegation-only for '%s' (%s/%s/%s) "
6582 domainbuf, namebuf, typebuf, classbuf, addrbuf);
6585 if ((fctx->res->options & DNS_RESOLVER_CHECKNAMES) != 0)
6586 checknames(message);
6591 fctx->attributes &= ~(FCTX_ATTR_WANTNCACHE | FCTX_ATTR_WANTCACHE);
6594 * Did we get any answers?
6596 if (message->counts[DNS_SECTION_ANSWER] > 0 &&
6597 (message->rcode == dns_rcode_noerror ||
6598 message->rcode == dns_rcode_nxdomain)) {
6600 * We've got answers. However, if we sent
6601 * a BIND 8 server an NS query, it may have
6602 * incorrectly responded with a non-authoritative
6603 * answer instead of a referral. Since this
6604 * answer lacks the SIGs necessary to do DNSSEC
6605 * validation, we must invoke the following special
6606 * kludge to treat it as a referral.
6608 if (fctx->type == dns_rdatatype_ns &&
6609 (message->flags & DNS_MESSAGEFLAG_AA) == 0 &&
6610 !ISFORWARDER(query->addrinfo))
6612 result = noanswer_response(fctx, NULL, ISC_TRUE);
6613 if (result != DNS_R_DELEGATION) {
6615 * The answer section must have contained
6616 * something other than the NS records
6617 * we asked for. Since AA is not set
6618 * and the server is not a forwarder,
6619 * it is technically lame and it's easier
6620 * to treat it as such than to figure out
6621 * some more elaborate course of action.
6623 broken_server = DNS_R_LAME;
6624 keep_trying = ISC_TRUE;
6627 goto force_referral;
6629 result = answer_response(fctx);
6630 if (result != ISC_R_SUCCESS) {
6631 if (result == DNS_R_FORMERR)
6632 keep_trying = ISC_TRUE;
6635 } else if (message->counts[DNS_SECTION_AUTHORITY] > 0 ||
6636 message->rcode == dns_rcode_noerror ||
6637 message->rcode == dns_rcode_nxdomain) {
6639 * NXDOMAIN, NXRDATASET, or referral.
6641 result = noanswer_response(fctx, NULL, ISC_FALSE);
6642 if (result == DNS_R_CHASEDSSERVERS) {
6643 } else if (result == DNS_R_DELEGATION) {
6646 * We don't have the answer, but we know a better
6649 get_nameservers = ISC_TRUE;
6650 keep_trying = ISC_TRUE;
6652 * We have a new set of name servers, and it
6653 * has not experienced any restarts yet.
6658 * Update local statistics counters collected for each
6662 fctx->querysent = 0;
6663 fctx->lamecount = 0;
6668 result = ISC_R_SUCCESS;
6669 } else if (result != ISC_R_SUCCESS) {
6671 * Something has gone wrong.
6673 if (result == DNS_R_FORMERR)
6674 keep_trying = ISC_TRUE;
6679 * The server is insane.
6682 broken_server = DNS_R_UNEXPECTEDRCODE;
6683 keep_trying = ISC_TRUE;
6688 * Follow additional section data chains.
6690 chase_additional(fctx);
6693 * Cache the cacheable parts of the message. This may also cause
6694 * work to be queued to the DNSSEC validator.
6696 if (WANTCACHE(fctx)) {
6697 result = cache_message(fctx, query->addrinfo, now);
6698 if (result != ISC_R_SUCCESS)
6703 * Ncache the negatively cacheable parts of the message. This may
6704 * also cause work to be queued to the DNSSEC validator.
6706 if (WANTNCACHE(fctx)) {
6707 dns_rdatatype_t covers;
6708 if (message->rcode == dns_rcode_nxdomain)
6709 covers = dns_rdatatype_any;
6711 covers = fctx->type;
6714 * Cache any negative cache entries in the message.
6716 result = ncache_message(fctx, query->addrinfo, covers, now);
6721 * Remember the query's addrinfo, in case we need to mark the
6724 addrinfo = query->addrinfo;
6729 * XXXRTH Don't cancel the query if waiting for validation?
6731 fctx_cancelquery(&query, &devent, finish, no_response);
6734 if (result == DNS_R_FORMERR)
6735 broken_server = DNS_R_FORMERR;
6736 if (broken_server != ISC_R_SUCCESS) {
6738 * Add this server to the list of bad servers for
6741 add_bad(fctx, addrinfo, broken_server, broken_type);
6744 if (get_nameservers) {
6746 dns_fixedname_init(&foundname);
6747 fname = dns_fixedname_name(&foundname);
6748 if (result != ISC_R_SUCCESS) {
6749 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6753 if (dns_rdatatype_atparent(fctx->type))
6754 findoptions |= DNS_DBFIND_NOEXACT;
6755 if ((options & DNS_FETCHOPT_UNSHARED) == 0)
6758 name = &fctx->domain;
6759 result = dns_view_findzonecut(fctx->res->view,
6765 if (result != ISC_R_SUCCESS) {
6766 FCTXTRACE("couldn't find a zonecut");
6767 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6770 if (!dns_name_issubdomain(fname, &fctx->domain)) {
6772 * The best nameservers are now above our
6775 FCTXTRACE("nameservers now above QDOMAIN");
6776 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6779 dns_name_free(&fctx->domain,
6780 fctx->res->buckets[fctx->bucketnum].mctx);
6781 dns_name_init(&fctx->domain, NULL);
6782 result = dns_name_dup(fname,
6783 fctx->res->buckets[fctx->bucketnum].mctx,
6785 if (result != ISC_R_SUCCESS) {
6786 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6789 fctx_cancelqueries(fctx, ISC_TRUE);
6790 fctx_cleanupfinds(fctx);
6791 fctx_cleanupaltfinds(fctx);
6792 fctx_cleanupforwaddrs(fctx);
6793 fctx_cleanupaltaddrs(fctx);
6798 fctx_try(fctx, !get_nameservers, ISC_FALSE);
6799 } else if (resend) {
6801 * Resend (probably with changed options).
6803 FCTXTRACE("resend");
6804 inc_stats(fctx->res, dns_resstatscounter_retry);
6805 result = fctx_query(fctx, addrinfo, options);
6806 if (result != ISC_R_SUCCESS)
6807 fctx_done(fctx, result, __LINE__);
6808 } else if (result == ISC_R_SUCCESS && !HAVE_ANSWER(fctx)) {
6810 * All has gone well so far, but we are waiting for the
6811 * DNSSEC validator to validate the answer.
6813 FCTXTRACE("wait for validator");
6814 fctx_cancelqueries(fctx, ISC_TRUE);
6816 * We must not retransmit while the validator is working;
6817 * it has references to the current rmessage.
6819 result = fctx_stopidletimer(fctx);
6820 if (result != ISC_R_SUCCESS)
6821 fctx_done(fctx, result, __LINE__);
6822 } else if (result == DNS_R_CHASEDSSERVERS) {
6824 add_bad(fctx, addrinfo, result, broken_type);
6825 fctx_cancelqueries(fctx, ISC_TRUE);
6826 fctx_cleanupfinds(fctx);
6827 fctx_cleanupforwaddrs(fctx);
6829 n = dns_name_countlabels(&fctx->name);
6830 dns_name_getlabelsequence(&fctx->name, 1, n - 1, &fctx->nsname);
6832 FCTXTRACE("suspending DS lookup to find parent's NS records");
6834 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
6836 NULL, NULL, NULL, 0, task,
6837 resume_dslookup, fctx,
6838 &fctx->nsrrset, NULL,
6840 if (result != ISC_R_SUCCESS)
6841 fctx_done(fctx, result, __LINE__);
6843 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
6845 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
6846 result = fctx_stopidletimer(fctx);
6847 if (result != ISC_R_SUCCESS)
6848 fctx_done(fctx, result, __LINE__);
6854 fctx_done(fctx, result, __LINE__);
6860 *** Resolver Methods
6863 destroy_badcache(dns_resolver_t *res) {
6864 dns_badcache_t *bad, *next;
6867 if (res->badcache != NULL) {
6868 for (i = 0; i < res->badhash; i++)
6869 for (bad = res->badcache[i]; bad != NULL;
6872 isc_mem_put(res->mctx, bad, sizeof(*bad) +
6876 isc_mem_put(res->mctx, res->badcache,
6877 sizeof(*res->badcache) * res->badhash);
6878 res->badcache = NULL;
6880 INSIST(res->badcount == 0);
6885 destroy(dns_resolver_t *res) {
6889 REQUIRE(res->references == 0);
6890 REQUIRE(!res->priming);
6891 REQUIRE(res->primefetch == NULL);
6895 INSIST(res->nfctx == 0);
6897 DESTROYLOCK(&res->primelock);
6898 DESTROYLOCK(&res->nlock);
6899 DESTROYLOCK(&res->lock);
6900 for (i = 0; i < res->nbuckets; i++) {
6901 INSIST(ISC_LIST_EMPTY(res->buckets[i].fctxs));
6902 isc_task_shutdown(res->buckets[i].task);
6903 isc_task_detach(&res->buckets[i].task);
6904 DESTROYLOCK(&res->buckets[i].lock);
6905 isc_mem_detach(&res->buckets[i].mctx);
6907 isc_mem_put(res->mctx, res->buckets,
6908 res->nbuckets * sizeof(fctxbucket_t));
6909 if (res->dispatchv4 != NULL)
6910 dns_dispatch_detach(&res->dispatchv4);
6911 if (res->dispatchv6 != NULL)
6912 dns_dispatch_detach(&res->dispatchv6);
6913 while ((a = ISC_LIST_HEAD(res->alternates)) != NULL) {
6914 ISC_LIST_UNLINK(res->alternates, a, link);
6916 dns_name_free(&a->_u._n.name, res->mctx);
6917 isc_mem_put(res->mctx, a, sizeof(*a));
6919 dns_resolver_reset_algorithms(res);
6920 destroy_badcache(res);
6921 dns_resolver_resetmustbesecure(res);
6923 isc_rwlock_destroy(&res->alglock);
6926 isc_rwlock_destroy(&res->mbslock);
6928 isc_timer_detach(&res->spillattimer);
6930 isc_mem_put(res->mctx, res, sizeof(*res));
6934 send_shutdown_events(dns_resolver_t *res) {
6935 isc_event_t *event, *next_event;
6939 * Caller must be holding the resolver lock.
6942 for (event = ISC_LIST_HEAD(res->whenshutdown);
6944 event = next_event) {
6945 next_event = ISC_LIST_NEXT(event, ev_link);
6946 ISC_LIST_UNLINK(res->whenshutdown, event, ev_link);
6947 etask = event->ev_sender;
6948 event->ev_sender = res;
6949 isc_task_sendanddetach(&etask, &event);
6954 empty_bucket(dns_resolver_t *res) {
6955 RTRACE("empty_bucket");
6959 INSIST(res->activebuckets > 0);
6960 res->activebuckets--;
6961 if (res->activebuckets == 0)
6962 send_shutdown_events(res);
6968 spillattimer_countdown(isc_task_t *task, isc_event_t *event) {
6969 dns_resolver_t *res = event->ev_arg;
6970 isc_result_t result;
6972 isc_boolean_t logit = ISC_FALSE;
6974 REQUIRE(VALID_RESOLVER(res));
6979 INSIST(!res->exiting);
6980 if (res->spillat > res->spillatmin) {
6984 if (res->spillat <= res->spillatmin) {
6985 result = isc_timer_reset(res->spillattimer,
6986 isc_timertype_inactive, NULL,
6988 RUNTIME_CHECK(result == ISC_R_SUCCESS);
6990 count = res->spillat;
6993 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6994 DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
6995 "clients-per-query decreased to %u", count);
6997 isc_event_free(&event);
7001 dns_resolver_create(dns_view_t *view,
7002 isc_taskmgr_t *taskmgr, unsigned int ntasks,
7003 isc_socketmgr_t *socketmgr,
7004 isc_timermgr_t *timermgr,
7005 unsigned int options,
7006 dns_dispatchmgr_t *dispatchmgr,
7007 dns_dispatch_t *dispatchv4,
7008 dns_dispatch_t *dispatchv6,
7009 dns_resolver_t **resp)
7011 dns_resolver_t *res;
7012 isc_result_t result = ISC_R_SUCCESS;
7013 unsigned int i, buckets_created = 0;
7014 isc_task_t *task = NULL;
7019 * Create a resolver.
7022 REQUIRE(DNS_VIEW_VALID(view));
7023 REQUIRE(ntasks > 0);
7024 REQUIRE(resp != NULL && *resp == NULL);
7025 REQUIRE(dispatchmgr != NULL);
7026 REQUIRE(dispatchv4 != NULL || dispatchv6 != NULL);
7028 res = isc_mem_get(view->mctx, sizeof(*res));
7030 return (ISC_R_NOMEMORY);
7032 res->mctx = view->mctx;
7033 res->rdclass = view->rdclass;
7034 res->socketmgr = socketmgr;
7035 res->timermgr = timermgr;
7036 res->taskmgr = taskmgr;
7037 res->dispatchmgr = dispatchmgr;
7039 res->options = options;
7041 ISC_LIST_INIT(res->alternates);
7042 res->udpsize = RECV_BUFFER_SIZE;
7043 res->algorithms = NULL;
7044 res->badcache = NULL;
7048 res->mustbesecure = NULL;
7049 res->spillatmin = res->spillat = 10;
7050 res->spillatmax = 100;
7051 res->spillattimer = NULL;
7052 res->zero_no_soa_ttl = ISC_FALSE;
7054 res->nextdisp = 0; /* meaningless at this point, but init it */
7055 res->nbuckets = ntasks;
7056 res->activebuckets = ntasks;
7057 res->buckets = isc_mem_get(view->mctx,
7058 ntasks * sizeof(fctxbucket_t));
7059 if (res->buckets == NULL) {
7060 result = ISC_R_NOMEMORY;
7063 for (i = 0; i < ntasks; i++) {
7064 result = isc_mutex_init(&res->buckets[i].lock);
7065 if (result != ISC_R_SUCCESS)
7066 goto cleanup_buckets;
7067 res->buckets[i].task = NULL;
7068 result = isc_task_create(taskmgr, 0, &res->buckets[i].task);
7069 if (result != ISC_R_SUCCESS) {
7070 DESTROYLOCK(&res->buckets[i].lock);
7071 goto cleanup_buckets;
7073 res->buckets[i].mctx = NULL;
7074 snprintf(name, sizeof(name), "res%u", i);
7075 #ifdef ISC_PLATFORM_USETHREADS
7077 * Use a separate memory context for each bucket to reduce
7078 * contention among multiple threads. Do this only when
7079 * enabling threads because it will be require more memory.
7081 result = isc_mem_create(0, 0, &res->buckets[i].mctx);
7082 if (result != ISC_R_SUCCESS) {
7083 isc_task_detach(&res->buckets[i].task);
7084 DESTROYLOCK(&res->buckets[i].lock);
7085 goto cleanup_buckets;
7087 isc_mem_setname(res->buckets[i].mctx, name, NULL);
7089 isc_mem_attach(view->mctx, &res->buckets[i].mctx);
7091 isc_task_setname(res->buckets[i].task, name, res);
7092 ISC_LIST_INIT(res->buckets[i].fctxs);
7093 res->buckets[i].exiting = ISC_FALSE;
7097 res->dispatchv4 = NULL;
7098 if (dispatchv4 != NULL) {
7099 dns_dispatch_attach(dispatchv4, &res->dispatchv4);
7100 dispattr = dns_dispatch_getattributes(dispatchv4);
7102 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
7105 res->dispatchv6 = NULL;
7106 if (dispatchv6 != NULL) {
7107 dns_dispatch_attach(dispatchv6, &res->dispatchv6);
7108 dispattr = dns_dispatch_getattributes(dispatchv6);
7110 ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
7113 res->references = 1;
7114 res->exiting = ISC_FALSE;
7115 res->frozen = ISC_FALSE;
7116 ISC_LIST_INIT(res->whenshutdown);
7117 res->priming = ISC_FALSE;
7118 res->primefetch = NULL;
7121 result = isc_mutex_init(&res->lock);
7122 if (result != ISC_R_SUCCESS)
7123 goto cleanup_dispatches;
7125 result = isc_mutex_init(&res->nlock);
7126 if (result != ISC_R_SUCCESS)
7129 result = isc_mutex_init(&res->primelock);
7130 if (result != ISC_R_SUCCESS)
7134 result = isc_task_create(taskmgr, 0, &task);
7135 if (result != ISC_R_SUCCESS)
7136 goto cleanup_primelock;
7138 result = isc_timer_create(timermgr, isc_timertype_inactive, NULL, NULL,
7139 task, spillattimer_countdown, res,
7140 &res->spillattimer);
7141 isc_task_detach(&task);
7142 if (result != ISC_R_SUCCESS)
7143 goto cleanup_primelock;
7146 result = isc_rwlock_init(&res->alglock, 0, 0);
7147 if (result != ISC_R_SUCCESS)
7148 goto cleanup_spillattimer;
7151 result = isc_rwlock_init(&res->mbslock, 0, 0);
7152 if (result != ISC_R_SUCCESS)
7153 goto cleanup_alglock;
7156 res->magic = RES_MAGIC;
7160 return (ISC_R_SUCCESS);
7165 isc_rwlock_destroy(&res->alglock);
7168 #if USE_ALGLOCK || USE_MBSLOCK
7169 cleanup_spillattimer:
7170 isc_timer_detach(&res->spillattimer);
7174 DESTROYLOCK(&res->primelock);
7177 DESTROYLOCK(&res->nlock);
7180 DESTROYLOCK(&res->lock);
7183 if (res->dispatchv6 != NULL)
7184 dns_dispatch_detach(&res->dispatchv6);
7185 if (res->dispatchv4 != NULL)
7186 dns_dispatch_detach(&res->dispatchv4);
7189 for (i = 0; i < buckets_created; i++) {
7190 isc_mem_detach(&res->buckets[i].mctx);
7191 DESTROYLOCK(&res->buckets[i].lock);
7192 isc_task_shutdown(res->buckets[i].task);
7193 isc_task_detach(&res->buckets[i].task);
7195 isc_mem_put(view->mctx, res->buckets,
7196 res->nbuckets * sizeof(fctxbucket_t));
7199 isc_mem_put(view->mctx, res, sizeof(*res));
7205 prime_done(isc_task_t *task, isc_event_t *event) {
7206 dns_resolver_t *res;
7207 dns_fetchevent_t *fevent;
7209 dns_db_t *db = NULL;
7211 REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
7212 fevent = (dns_fetchevent_t *)event;
7213 res = event->ev_arg;
7214 REQUIRE(VALID_RESOLVER(res));
7220 INSIST(res->priming);
7221 res->priming = ISC_FALSE;
7222 LOCK(&res->primelock);
7223 fetch = res->primefetch;
7224 res->primefetch = NULL;
7225 UNLOCK(&res->primelock);
7229 if (fevent->result == ISC_R_SUCCESS &&
7230 res->view->cache != NULL && res->view->hints != NULL) {
7231 dns_cache_attachdb(res->view->cache, &db);
7232 dns_root_checkhints(res->view, res->view->hints, db);
7236 if (fevent->node != NULL)
7237 dns_db_detachnode(fevent->db, &fevent->node);
7238 if (fevent->db != NULL)
7239 dns_db_detach(&fevent->db);
7240 if (dns_rdataset_isassociated(fevent->rdataset))
7241 dns_rdataset_disassociate(fevent->rdataset);
7242 INSIST(fevent->sigrdataset == NULL);
7244 isc_mem_put(res->mctx, fevent->rdataset, sizeof(*fevent->rdataset));
7246 isc_event_free(&event);
7247 dns_resolver_destroyfetch(&fetch);
7251 dns_resolver_prime(dns_resolver_t *res) {
7252 isc_boolean_t want_priming = ISC_FALSE;
7253 dns_rdataset_t *rdataset;
7254 isc_result_t result;
7256 REQUIRE(VALID_RESOLVER(res));
7257 REQUIRE(res->frozen);
7259 RTRACE("dns_resolver_prime");
7263 if (!res->exiting && !res->priming) {
7264 INSIST(res->primefetch == NULL);
7265 res->priming = ISC_TRUE;
7266 want_priming = ISC_TRUE;
7273 * To avoid any possible recursive locking problems, we
7274 * start the priming fetch like any other fetch, and holding
7275 * no resolver locks. No one else will try to start it
7276 * because we're the ones who set res->priming to true.
7277 * Any other callers of dns_resolver_prime() while we're
7278 * running will see that res->priming is already true and
7282 rdataset = isc_mem_get(res->mctx, sizeof(*rdataset));
7283 if (rdataset == NULL) {
7285 INSIST(res->priming);
7286 INSIST(res->primefetch == NULL);
7287 res->priming = ISC_FALSE;
7291 dns_rdataset_init(rdataset);
7292 LOCK(&res->primelock);
7293 result = dns_resolver_createfetch(res, dns_rootname,
7295 NULL, NULL, NULL, 0,
7296 res->buckets[0].task,
7298 res, rdataset, NULL,
7300 UNLOCK(&res->primelock);
7301 if (result != ISC_R_SUCCESS) {
7303 INSIST(res->priming);
7304 res->priming = ISC_FALSE;
7311 dns_resolver_freeze(dns_resolver_t *res) {
7317 REQUIRE(VALID_RESOLVER(res));
7318 REQUIRE(!res->frozen);
7320 res->frozen = ISC_TRUE;
7324 dns_resolver_attach(dns_resolver_t *source, dns_resolver_t **targetp) {
7325 REQUIRE(VALID_RESOLVER(source));
7326 REQUIRE(targetp != NULL && *targetp == NULL);
7328 RRTRACE(source, "attach");
7329 LOCK(&source->lock);
7330 REQUIRE(!source->exiting);
7332 INSIST(source->references > 0);
7333 source->references++;
7334 INSIST(source->references != 0);
7335 UNLOCK(&source->lock);
7341 dns_resolver_whenshutdown(dns_resolver_t *res, isc_task_t *task,
7342 isc_event_t **eventp)
7347 REQUIRE(VALID_RESOLVER(res));
7348 REQUIRE(eventp != NULL);
7355 if (res->exiting && res->activebuckets == 0) {
7357 * We're already shutdown. Send the event.
7359 event->ev_sender = res;
7360 isc_task_send(task, &event);
7363 isc_task_attach(task, &clone);
7364 event->ev_sender = clone;
7365 ISC_LIST_APPEND(res->whenshutdown, event, ev_link);
7372 dns_resolver_shutdown(dns_resolver_t *res) {
7376 isc_result_t result;
7378 REQUIRE(VALID_RESOLVER(res));
7384 if (!res->exiting) {
7386 res->exiting = ISC_TRUE;
7388 for (i = 0; i < res->nbuckets; i++) {
7389 LOCK(&res->buckets[i].lock);
7390 for (fctx = ISC_LIST_HEAD(res->buckets[i].fctxs);
7392 fctx = ISC_LIST_NEXT(fctx, link))
7393 fctx_shutdown(fctx);
7394 if (res->dispatchv4 != NULL && !res->exclusivev4) {
7395 sock = dns_dispatch_getsocket(res->dispatchv4);
7396 isc_socket_cancel(sock, res->buckets[i].task,
7397 ISC_SOCKCANCEL_ALL);
7399 if (res->dispatchv6 != NULL && !res->exclusivev6) {
7400 sock = dns_dispatch_getsocket(res->dispatchv6);
7401 isc_socket_cancel(sock, res->buckets[i].task,
7402 ISC_SOCKCANCEL_ALL);
7404 res->buckets[i].exiting = ISC_TRUE;
7405 if (ISC_LIST_EMPTY(res->buckets[i].fctxs)) {
7406 INSIST(res->activebuckets > 0);
7407 res->activebuckets--;
7409 UNLOCK(&res->buckets[i].lock);
7411 if (res->activebuckets == 0)
7412 send_shutdown_events(res);
7413 result = isc_timer_reset(res->spillattimer,
7414 isc_timertype_inactive, NULL,
7416 RUNTIME_CHECK(result == ISC_R_SUCCESS);
7423 dns_resolver_detach(dns_resolver_t **resp) {
7424 dns_resolver_t *res;
7425 isc_boolean_t need_destroy = ISC_FALSE;
7427 REQUIRE(resp != NULL);
7429 REQUIRE(VALID_RESOLVER(res));
7435 INSIST(res->references > 0);
7437 if (res->references == 0) {
7438 INSIST(res->exiting && res->activebuckets == 0);
7439 need_destroy = ISC_TRUE;
7450 static inline isc_boolean_t
7451 fctx_match(fetchctx_t *fctx, dns_name_t *name, dns_rdatatype_t type,
7452 unsigned int options)
7455 * Don't match fetch contexts that are shutting down.
7457 if (fctx->cloned || fctx->state == fetchstate_done ||
7458 ISC_LIST_EMPTY(fctx->events))
7461 if (fctx->type != type || fctx->options != options)
7463 return (dns_name_equal(&fctx->name, name));
7467 log_fetch(dns_name_t *name, dns_rdatatype_t type) {
7468 char namebuf[DNS_NAME_FORMATSIZE];
7469 char typebuf[DNS_RDATATYPE_FORMATSIZE];
7470 int level = ISC_LOG_DEBUG(1);
7472 if (! isc_log_wouldlog(dns_lctx, level))
7475 dns_name_format(name, namebuf, sizeof(namebuf));
7476 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
7478 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7479 DNS_LOGMODULE_RESOLVER, level,
7480 "createfetch: %s %s", namebuf, typebuf);
7484 dns_resolver_createfetch(dns_resolver_t *res, dns_name_t *name,
7485 dns_rdatatype_t type,
7486 dns_name_t *domain, dns_rdataset_t *nameservers,
7487 dns_forwarders_t *forwarders,
7488 unsigned int options, isc_task_t *task,
7489 isc_taskaction_t action, void *arg,
7490 dns_rdataset_t *rdataset,
7491 dns_rdataset_t *sigrdataset,
7492 dns_fetch_t **fetchp)
7494 return (dns_resolver_createfetch2(res, name, type, domain,
7495 nameservers, forwarders, NULL, 0,
7496 options, task, action, arg,
7497 rdataset, sigrdataset, fetchp));
7501 dns_resolver_createfetch2(dns_resolver_t *res, dns_name_t *name,
7502 dns_rdatatype_t type,
7503 dns_name_t *domain, dns_rdataset_t *nameservers,
7504 dns_forwarders_t *forwarders,
7505 isc_sockaddr_t *client, dns_messageid_t id,
7506 unsigned int options, isc_task_t *task,
7507 isc_taskaction_t action, void *arg,
7508 dns_rdataset_t *rdataset,
7509 dns_rdataset_t *sigrdataset,
7510 dns_fetch_t **fetchp)
7513 fetchctx_t *fctx = NULL;
7514 isc_result_t result = ISC_R_SUCCESS;
7515 unsigned int bucketnum;
7516 isc_boolean_t new_fctx = ISC_FALSE;
7518 unsigned int count = 0;
7519 unsigned int spillat;
7520 unsigned int spillatmin;
7524 REQUIRE(VALID_RESOLVER(res));
7525 REQUIRE(res->frozen);
7526 /* XXXRTH Check for meta type */
7527 if (domain != NULL) {
7528 REQUIRE(DNS_RDATASET_VALID(nameservers));
7529 REQUIRE(nameservers->type == dns_rdatatype_ns);
7531 REQUIRE(nameservers == NULL);
7532 REQUIRE(forwarders == NULL);
7533 REQUIRE(!dns_rdataset_isassociated(rdataset));
7534 REQUIRE(sigrdataset == NULL ||
7535 !dns_rdataset_isassociated(sigrdataset));
7536 REQUIRE(fetchp != NULL && *fetchp == NULL);
7538 log_fetch(name, type);
7541 * XXXRTH use a mempool?
7543 fetch = isc_mem_get(res->mctx, sizeof(*fetch));
7545 return (ISC_R_NOMEMORY);
7547 bucketnum = dns_name_fullhash(name, ISC_FALSE) % res->nbuckets;
7550 spillat = res->spillat;
7551 spillatmin = res->spillatmin;
7553 LOCK(&res->buckets[bucketnum].lock);
7555 if (res->buckets[bucketnum].exiting) {
7556 result = ISC_R_SHUTTINGDOWN;
7560 if ((options & DNS_FETCHOPT_UNSHARED) == 0) {
7561 for (fctx = ISC_LIST_HEAD(res->buckets[bucketnum].fctxs);
7563 fctx = ISC_LIST_NEXT(fctx, link)) {
7564 if (fctx_match(fctx, name, type, options))
7570 * Is this a duplicate?
7572 if (fctx != NULL && client != NULL) {
7573 dns_fetchevent_t *fevent;
7574 for (fevent = ISC_LIST_HEAD(fctx->events);
7576 fevent = ISC_LIST_NEXT(fevent, ev_link)) {
7577 if (fevent->client != NULL && fevent->id == id &&
7578 isc_sockaddr_equal(fevent->client, client)) {
7579 result = DNS_R_DUPLICATE;
7585 if (count >= spillatmin && spillatmin != 0) {
7586 INSIST(fctx != NULL);
7587 if (count >= spillat)
7588 fctx->spilled = ISC_TRUE;
7589 if (fctx->spilled) {
7590 result = DNS_R_DROP;
7596 result = fctx_create(res, name, type, domain, nameservers,
7597 options, bucketnum, &fctx);
7598 if (result != ISC_R_SUCCESS)
7600 new_fctx = ISC_TRUE;
7603 result = fctx_join(fctx, task, client, id, action, arg,
7604 rdataset, sigrdataset, fetch);
7606 if (result == ISC_R_SUCCESS) {
7610 event = &fctx->control_event;
7611 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
7612 DNS_EVENT_FETCHCONTROL,
7613 fctx_start, fctx, NULL,
7615 isc_task_send(res->buckets[bucketnum].task, &event);
7618 * We don't care about the result of fctx_destroy()
7619 * since we know we're not exiting.
7621 (void)fctx_destroy(fctx);
7626 UNLOCK(&res->buckets[bucketnum].lock);
7628 if (result == ISC_R_SUCCESS) {
7632 isc_mem_put(res->mctx, fetch, sizeof(*fetch));
7638 dns_resolver_cancelfetch(dns_fetch_t *fetch) {
7640 dns_resolver_t *res;
7641 dns_fetchevent_t *event, *next_event;
7644 REQUIRE(DNS_FETCH_VALID(fetch));
7645 fctx = fetch->private;
7646 REQUIRE(VALID_FCTX(fctx));
7649 FTRACE("cancelfetch");
7651 LOCK(&res->buckets[fctx->bucketnum].lock);
7654 * Find the completion event for this fetch (as opposed
7655 * to those for other fetches that have joined the same
7656 * fctx) and send it with result = ISC_R_CANCELED.
7659 if (fctx->state != fetchstate_done) {
7660 for (event = ISC_LIST_HEAD(fctx->events);
7662 event = next_event) {
7663 next_event = ISC_LIST_NEXT(event, ev_link);
7664 if (event->fetch == fetch) {
7665 ISC_LIST_UNLINK(fctx->events, event, ev_link);
7670 if (event != NULL) {
7671 etask = event->ev_sender;
7672 event->ev_sender = fctx;
7673 event->result = ISC_R_CANCELED;
7674 isc_task_sendanddetach(&etask, ISC_EVENT_PTR(&event));
7677 * The fctx continues running even if no fetches remain;
7678 * the answer is still cached.
7681 UNLOCK(&res->buckets[fctx->bucketnum].lock);
7685 dns_resolver_destroyfetch(dns_fetch_t **fetchp) {
7687 dns_resolver_t *res;
7688 dns_fetchevent_t *event, *next_event;
7690 unsigned int bucketnum;
7691 isc_boolean_t bucket_empty;
7693 REQUIRE(fetchp != NULL);
7695 REQUIRE(DNS_FETCH_VALID(fetch));
7696 fctx = fetch->private;
7697 REQUIRE(VALID_FCTX(fctx));
7700 FTRACE("destroyfetch");
7702 bucketnum = fctx->bucketnum;
7703 LOCK(&res->buckets[bucketnum].lock);
7706 * Sanity check: the caller should have gotten its event before
7707 * trying to destroy the fetch.
7710 if (fctx->state != fetchstate_done) {
7711 for (event = ISC_LIST_HEAD(fctx->events);
7713 event = next_event) {
7714 next_event = ISC_LIST_NEXT(event, ev_link);
7715 RUNTIME_CHECK(event->fetch != fetch);
7719 bucket_empty = fctx_decreference(fctx);
7721 UNLOCK(&res->buckets[bucketnum].lock);
7723 isc_mem_put(res->mctx, fetch, sizeof(*fetch));
7731 dns_resolver_logfetch(dns_fetch_t *fetch, isc_log_t *lctx,
7732 isc_logcategory_t *category, isc_logmodule_t *module,
7733 int level, isc_boolean_t duplicateok)
7736 dns_resolver_t *res;
7737 char domainbuf[DNS_NAME_FORMATSIZE];
7739 REQUIRE(DNS_FETCH_VALID(fetch));
7740 fctx = fetch->private;
7741 REQUIRE(VALID_FCTX(fctx));
7744 LOCK(&res->buckets[fctx->bucketnum].lock);
7746 INSIST(fctx->exitline >= 0);
7747 if (!fctx->logged || duplicateok) {
7748 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
7749 isc_log_write(lctx, category, module, level,
7750 "fetch completed at %s:%d for %s in "
7751 "%" ISC_PRINT_QUADFORMAT "u."
7752 "%06" ISC_PRINT_QUADFORMAT "u: %s/%s "
7753 "[domain:%s,referral:%u,restart:%u,qrysent:%u,"
7754 "timeout:%u,lame:%u,neterr:%u,badresp:%u,"
7755 "adberr:%u,findfail:%u,valfail:%u]",
7756 __FILE__, fctx->exitline, fctx->info,
7757 fctx->duration / 1000000,
7758 fctx->duration % 1000000,
7759 isc_result_totext(fctx->result),
7760 isc_result_totext(fctx->vresult), domainbuf,
7761 fctx->referrals, fctx->restarts,
7762 fctx->querysent, fctx->timeouts, fctx->lamecount,
7763 fctx->neterr, fctx->badresp, fctx->adberr,
7764 fctx->findfail, fctx->valfail);
7765 fctx->logged = ISC_TRUE;
7768 UNLOCK(&res->buckets[fctx->bucketnum].lock);
7772 dns_resolver_dispatchmgr(dns_resolver_t *resolver) {
7773 REQUIRE(VALID_RESOLVER(resolver));
7774 return (resolver->dispatchmgr);
7778 dns_resolver_dispatchv4(dns_resolver_t *resolver) {
7779 REQUIRE(VALID_RESOLVER(resolver));
7780 return (resolver->dispatchv4);
7784 dns_resolver_dispatchv6(dns_resolver_t *resolver) {
7785 REQUIRE(VALID_RESOLVER(resolver));
7786 return (resolver->dispatchv6);
7790 dns_resolver_socketmgr(dns_resolver_t *resolver) {
7791 REQUIRE(VALID_RESOLVER(resolver));
7792 return (resolver->socketmgr);
7796 dns_resolver_taskmgr(dns_resolver_t *resolver) {
7797 REQUIRE(VALID_RESOLVER(resolver));
7798 return (resolver->taskmgr);
7802 dns_resolver_getlamettl(dns_resolver_t *resolver) {
7803 REQUIRE(VALID_RESOLVER(resolver));
7804 return (resolver->lame_ttl);
7808 dns_resolver_setlamettl(dns_resolver_t *resolver, isc_uint32_t lame_ttl) {
7809 REQUIRE(VALID_RESOLVER(resolver));
7810 resolver->lame_ttl = lame_ttl;
7814 dns_resolver_nrunning(dns_resolver_t *resolver) {
7816 LOCK(&resolver->nlock);
7817 n = resolver->nfctx;
7818 UNLOCK(&resolver->nlock);
7823 dns_resolver_addalternate(dns_resolver_t *resolver, isc_sockaddr_t *alt,
7824 dns_name_t *name, in_port_t port) {
7826 isc_result_t result;
7828 REQUIRE(VALID_RESOLVER(resolver));
7829 REQUIRE(!resolver->frozen);
7830 REQUIRE((alt == NULL) ^ (name == NULL));
7832 a = isc_mem_get(resolver->mctx, sizeof(*a));
7834 return (ISC_R_NOMEMORY);
7836 a->isaddress = ISC_TRUE;
7839 a->isaddress = ISC_FALSE;
7840 a->_u._n.port = port;
7841 dns_name_init(&a->_u._n.name, NULL);
7842 result = dns_name_dup(name, resolver->mctx, &a->_u._n.name);
7843 if (result != ISC_R_SUCCESS) {
7844 isc_mem_put(resolver->mctx, a, sizeof(*a));
7848 ISC_LINK_INIT(a, link);
7849 ISC_LIST_APPEND(resolver->alternates, a, link);
7851 return (ISC_R_SUCCESS);
7855 dns_resolver_setudpsize(dns_resolver_t *resolver, isc_uint16_t udpsize) {
7856 REQUIRE(VALID_RESOLVER(resolver));
7857 resolver->udpsize = udpsize;
7861 dns_resolver_getudpsize(dns_resolver_t *resolver) {
7862 REQUIRE(VALID_RESOLVER(resolver));
7863 return (resolver->udpsize);
7867 dns_resolver_flushbadcache(dns_resolver_t *resolver, dns_name_t *name) {
7869 dns_badcache_t *bad, *prev, *next;
7871 REQUIRE(VALID_RESOLVER(resolver));
7873 LOCK(&resolver->lock);
7874 if (resolver->badcache == NULL)
7879 isc_result_t result;
7880 result = isc_time_now(&now);
7881 if (result != ISC_R_SUCCESS)
7882 isc_time_settoepoch(&now);
7883 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
7885 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
7888 n = isc_time_compare(&bad->expire, &now);
7889 if (n < 0 || dns_name_equal(name, &bad->name)) {
7891 resolver->badcache[i] = bad->next;
7893 prev->next = bad->next;
7894 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
7896 resolver->badcount--;
7901 destroy_badcache(resolver);
7904 UNLOCK(&resolver->lock);
7909 resizehash(dns_resolver_t *resolver, isc_time_t *now, isc_boolean_t grow) {
7910 unsigned int newsize;
7911 dns_badcache_t **new, *bad, *next;
7915 newsize = resolver->badhash * 2 + 1;
7917 newsize = (resolver->badhash - 1) / 2;
7919 new = isc_mem_get(resolver->mctx,
7920 sizeof(*resolver->badcache) * newsize);
7923 memset(new, 0, sizeof(*resolver->badcache) * newsize);
7924 for (i = 0; i < resolver->badhash; i++) {
7925 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
7927 if (isc_time_compare(&bad->expire, now) < 0) {
7928 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
7930 resolver->badcount--;
7932 bad->next = new[bad->hashval % newsize];
7933 new[bad->hashval % newsize] = bad;
7937 isc_mem_put(resolver->mctx, resolver->badcache,
7938 sizeof(*resolver->badcache) * resolver->badhash);
7939 resolver->badhash = newsize;
7940 resolver->badcache = new;
7944 dns_resolver_addbadcache(dns_resolver_t *resolver, dns_name_t *name,
7945 dns_rdatatype_t type, isc_time_t *expire)
7948 isc_result_t result = ISC_R_SUCCESS;
7949 unsigned int i, hashval;
7950 dns_badcache_t *bad, *prev, *next;
7952 REQUIRE(VALID_RESOLVER(resolver));
7954 LOCK(&resolver->lock);
7955 if (resolver->badcache == NULL) {
7956 resolver->badcache = isc_mem_get(resolver->mctx,
7957 sizeof(*resolver->badcache) *
7959 if (resolver->badcache == NULL)
7961 resolver->badhash = DNS_BADCACHE_SIZE;
7962 memset(resolver->badcache, 0, sizeof(*resolver->badcache) *
7966 result = isc_time_now(&now);
7967 if (result != ISC_R_SUCCESS)
7968 isc_time_settoepoch(&now);
7969 hashval = dns_name_hash(name, ISC_FALSE);
7970 i = hashval % resolver->badhash;
7972 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
7974 if (bad->type == type && dns_name_equal(name, &bad->name))
7976 if (isc_time_compare(&bad->expire, &now) < 0) {
7978 resolver->badcache[i] = bad->next;
7980 prev->next = bad->next;
7981 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
7983 resolver->badcount--;
7988 isc_buffer_t buffer;
7989 bad = isc_mem_get(resolver->mctx, sizeof(*bad) + name->length);
7993 bad->hashval = hashval;
7994 isc_buffer_init(&buffer, bad + 1, name->length);
7995 dns_name_init(&bad->name, NULL);
7996 dns_name_copy(name, &bad->name, &buffer);
7997 bad->next = resolver->badcache[i];
7998 resolver->badcache[i] = bad;
7999 resolver->badcount++;
8000 if (resolver->badcount > resolver->badhash * 8)
8001 resizehash(resolver, &now, ISC_TRUE);
8002 if (resolver->badcount < resolver->badhash * 2 &&
8003 resolver->badhash > DNS_BADCACHE_SIZE)
8004 resizehash(resolver, &now, ISC_FALSE);
8006 bad->expire = *expire;
8008 UNLOCK(&resolver->lock);
8012 dns_resolver_getbadcache(dns_resolver_t *resolver, dns_name_t *name,
8013 dns_rdatatype_t type, isc_time_t *now)
8015 dns_badcache_t *bad, *prev, *next;
8016 isc_boolean_t answer = ISC_FALSE;
8019 REQUIRE(VALID_RESOLVER(resolver));
8021 LOCK(&resolver->lock);
8022 if (resolver->badcache == NULL)
8025 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
8027 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8030 * Search the hash list. Clean out expired records as we go.
8032 if (isc_time_compare(&bad->expire, now) < 0) {
8034 prev->next = bad->next;
8036 resolver->badcache[i] = bad->next;
8037 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8039 resolver->badcount--;
8042 if (bad->type == type && dns_name_equal(name, &bad->name)) {
8050 * Slow sweep to clean out stale records.
8052 i = resolver->badsweep++ % resolver->badhash;
8053 bad = resolver->badcache[i];
8054 if (bad != NULL && isc_time_compare(&bad->expire, now) < 0) {
8055 resolver->badcache[i] = bad->next;
8056 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8058 resolver->badcount--;
8062 UNLOCK(&resolver->lock);
8067 dns_resolver_printbadcache(dns_resolver_t *resolver, FILE *fp) {
8068 char namebuf[DNS_NAME_FORMATSIZE];
8069 char typebuf[DNS_RDATATYPE_FORMATSIZE];
8070 dns_badcache_t *bad, *next, *prev;
8075 LOCK(&resolver->lock);
8076 fprintf(fp, ";\n; Bad cache\n;\n");
8078 if (resolver->badcache == NULL)
8082 for (i = 0; i < resolver->badhash; i++) {
8084 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8086 if (isc_time_compare(&bad->expire, &now) < 0) {
8088 prev->next = bad->next;
8090 resolver->badcache[i] = bad->next;
8091 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8093 resolver->badcount--;
8097 dns_name_format(&bad->name, namebuf, sizeof(namebuf));
8098 dns_rdatatype_format(bad->type, typebuf,
8100 t = isc_time_microdiff(&bad->expire, &now);
8102 fprintf(fp, "; %s/%s [ttl "
8103 "%" ISC_PLATFORM_QUADFORMAT "u]\n",
8104 namebuf, typebuf, t);
8109 UNLOCK(&resolver->lock);
8113 free_algorithm(void *node, void *arg) {
8114 unsigned char *algorithms = node;
8115 isc_mem_t *mctx = arg;
8117 isc_mem_put(mctx, algorithms, *algorithms);
8121 dns_resolver_reset_algorithms(dns_resolver_t *resolver) {
8123 REQUIRE(VALID_RESOLVER(resolver));
8126 RWLOCK(&resolver->alglock, isc_rwlocktype_write);
8128 if (resolver->algorithms != NULL)
8129 dns_rbt_destroy(&resolver->algorithms);
8131 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
8136 dns_resolver_disable_algorithm(dns_resolver_t *resolver, dns_name_t *name,
8139 unsigned int len, mask;
8141 unsigned char *algorithms;
8142 isc_result_t result;
8143 dns_rbtnode_t *node = NULL;
8145 REQUIRE(VALID_RESOLVER(resolver));
8147 return (ISC_R_RANGE);
8150 RWLOCK(&resolver->alglock, isc_rwlocktype_write);
8152 if (resolver->algorithms == NULL) {
8153 result = dns_rbt_create(resolver->mctx, free_algorithm,
8154 resolver->mctx, &resolver->algorithms);
8155 if (result != ISC_R_SUCCESS)
8160 mask = 1 << (alg%8);
8162 result = dns_rbt_addnode(resolver->algorithms, name, &node);
8164 if (result == ISC_R_SUCCESS || result == ISC_R_EXISTS) {
8165 algorithms = node->data;
8166 if (algorithms == NULL || len > *algorithms) {
8167 new = isc_mem_get(resolver->mctx, len);
8169 result = ISC_R_NOMEMORY;
8172 memset(new, 0, len);
8173 if (algorithms != NULL)
8174 memcpy(new, algorithms, *algorithms);
8178 if (algorithms != NULL)
8179 isc_mem_put(resolver->mctx, algorithms,
8182 algorithms[len-1] |= mask;
8184 result = ISC_R_SUCCESS;
8187 RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
8193 dns_resolver_algorithm_supported(dns_resolver_t *resolver, dns_name_t *name,
8196 unsigned int len, mask;
8197 unsigned char *algorithms;
8199 isc_result_t result;
8200 isc_boolean_t found = ISC_FALSE;
8202 REQUIRE(VALID_RESOLVER(resolver));
8205 RWLOCK(&resolver->alglock, isc_rwlocktype_read);
8207 if (resolver->algorithms == NULL)
8209 result = dns_rbt_findname(resolver->algorithms, name, 0, NULL, &data);
8210 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
8212 mask = 1 << (alg%8);
8214 if (len <= *algorithms && (algorithms[len-1] & mask) != 0)
8219 RWUNLOCK(&resolver->alglock, isc_rwlocktype_read);
8223 return (dst_algorithm_supported(alg));
8227 dns_resolver_digest_supported(dns_resolver_t *resolver, unsigned int digest) {
8230 return (dns_ds_digest_supported(digest));
8234 dns_resolver_resetmustbesecure(dns_resolver_t *resolver) {
8236 REQUIRE(VALID_RESOLVER(resolver));
8239 RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
8241 if (resolver->mustbesecure != NULL)
8242 dns_rbt_destroy(&resolver->mustbesecure);
8244 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
8248 static isc_boolean_t yes = ISC_TRUE, no = ISC_FALSE;
8251 dns_resolver_setmustbesecure(dns_resolver_t *resolver, dns_name_t *name,
8252 isc_boolean_t value)
8254 isc_result_t result;
8256 REQUIRE(VALID_RESOLVER(resolver));
8259 RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
8261 if (resolver->mustbesecure == NULL) {
8262 result = dns_rbt_create(resolver->mctx, NULL, NULL,
8263 &resolver->mustbesecure);
8264 if (result != ISC_R_SUCCESS)
8267 result = dns_rbt_addname(resolver->mustbesecure, name,
8268 value ? &yes : &no);
8271 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
8277 dns_resolver_getmustbesecure(dns_resolver_t *resolver, dns_name_t *name) {
8279 isc_boolean_t value = ISC_FALSE;
8280 isc_result_t result;
8282 REQUIRE(VALID_RESOLVER(resolver));
8285 RWLOCK(&resolver->mbslock, isc_rwlocktype_read);
8287 if (resolver->mustbesecure == NULL)
8289 result = dns_rbt_findname(resolver->mustbesecure, name, 0, NULL, &data);
8290 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
8291 value = *(isc_boolean_t*)data;
8294 RWUNLOCK(&resolver->mbslock, isc_rwlocktype_read);
8300 dns_resolver_getclientsperquery(dns_resolver_t *resolver, isc_uint32_t *cur,
8301 isc_uint32_t *min, isc_uint32_t *max)
8303 REQUIRE(VALID_RESOLVER(resolver));
8305 LOCK(&resolver->lock);
8307 *cur = resolver->spillat;
8309 *min = resolver->spillatmin;
8311 *max = resolver->spillatmax;
8312 UNLOCK(&resolver->lock);
8316 dns_resolver_setclientsperquery(dns_resolver_t *resolver, isc_uint32_t min,
8319 REQUIRE(VALID_RESOLVER(resolver));
8321 LOCK(&resolver->lock);
8322 resolver->spillatmin = resolver->spillat = min;
8323 resolver->spillatmax = max;
8324 UNLOCK(&resolver->lock);
8328 dns_resolver_getzeronosoattl(dns_resolver_t *resolver) {
8329 REQUIRE(VALID_RESOLVER(resolver));
8331 return (resolver->zero_no_soa_ttl);
8335 dns_resolver_setzeronosoattl(dns_resolver_t *resolver, isc_boolean_t state) {
8336 REQUIRE(VALID_RESOLVER(resolver));
8338 resolver->zero_no_soa_ttl = state;
8342 dns_resolver_getoptions(dns_resolver_t *resolver) {
8343 REQUIRE(VALID_RESOLVER(resolver));
8345 return (resolver->options);