2 * Copyright (C) 2004-2009 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: adb.c,v 1.243.42.4 2009/02/03 22:34:28 jinmei Exp $ */
23 * In finds, if task == NULL, no events will be generated, and no events
24 * have been sent. If task != NULL but taskaction == NULL, an event has been
25 * posted but not yet freed. If neither are NULL, no event was posted.
33 #include <isc/mutexblock.h>
34 #include <isc/netaddr.h>
35 #include <isc/random.h>
36 #include <isc/stats.h>
37 #include <isc/string.h> /* Required for HP/UX (and others?) */
43 #include <dns/events.h>
45 #include <dns/rdata.h>
46 #include <dns/rdataset.h>
47 #include <dns/rdatastruct.h>
48 #include <dns/rdatatype.h>
49 #include <dns/resolver.h>
50 #include <dns/result.h>
51 #include <dns/stats.h>
53 #define DNS_ADB_MAGIC ISC_MAGIC('D', 'a', 'd', 'b')
54 #define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
55 #define DNS_ADBNAME_MAGIC ISC_MAGIC('a', 'd', 'b', 'N')
56 #define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
57 #define DNS_ADBNAMEHOOK_MAGIC ISC_MAGIC('a', 'd', 'N', 'H')
58 #define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
59 #define DNS_ADBLAMEINFO_MAGIC ISC_MAGIC('a', 'd', 'b', 'Z')
60 #define DNS_ADBLAMEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBLAMEINFO_MAGIC)
61 #define DNS_ADBENTRY_MAGIC ISC_MAGIC('a', 'd', 'b', 'E')
62 #define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
63 #define DNS_ADBFETCH_MAGIC ISC_MAGIC('a', 'd', 'F', '4')
64 #define DNS_ADBFETCH_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
65 #define DNS_ADBFETCH6_MAGIC ISC_MAGIC('a', 'd', 'F', '6')
66 #define DNS_ADBFETCH6_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH6_MAGIC)
69 * The number of buckets needs to be a prime (for good hashing).
71 * XXXRTH How many buckets do we need?
73 #define NBUCKETS 1009 /*%< how many buckets for names/addrs */
76 * For type 3 negative cache entries, we will remember that the address is
77 * broken for this long. XXXMLG This is also used for actual addresses, too.
78 * The intent is to keep us from constantly asking about A/AAAA records
79 * if the zone has extremely low TTLs.
81 #define ADB_CACHE_MINIMUM 10 /*%< seconds */
82 #define ADB_CACHE_MAXIMUM 86400 /*%< seconds (86400 = 24 hours) */
83 #define ADB_ENTRY_WINDOW 1800 /*%< seconds */
86 * The period in seconds after which an ADB name entry is regarded as stale
87 * and forced to be cleaned up.
88 * TODO: This should probably be configurable at run-time.
90 #ifndef ADB_STALE_MARGIN
91 #define ADB_STALE_MARGIN 1800
94 #define FREE_ITEMS 64 /*%< free count for memory pools */
95 #define FILL_COUNT 16 /*%< fill count for memory pools */
97 #define DNS_ADB_INVALIDBUCKET (-1) /*%< invalid bucket address */
99 #define DNS_ADB_MINADBSIZE (1024*1024) /*%< 1 Megabyte */
101 typedef ISC_LIST(dns_adbname_t) dns_adbnamelist_t;
102 typedef struct dns_adbnamehook dns_adbnamehook_t;
103 typedef ISC_LIST(dns_adbnamehook_t) dns_adbnamehooklist_t;
104 typedef struct dns_adblameinfo dns_adblameinfo_t;
105 typedef ISC_LIST(dns_adbentry_t) dns_adbentrylist_t;
106 typedef struct dns_adbfetch dns_adbfetch_t;
107 typedef struct dns_adbfetch6 dns_adbfetch6_t;
109 /*% dns adb structure */
114 isc_mutex_t reflock; /*%< Covers irefcnt, erefcnt */
115 isc_mutex_t overmemlock; /*%< Covers overmem */
119 isc_taskmgr_t *taskmgr;
121 isc_boolean_t overmem;
123 isc_interval_t tick_interval;
124 int next_cleanbucket;
126 unsigned int irefcnt;
127 unsigned int erefcnt;
130 isc_mempool_t *nmp; /*%< dns_adbname_t */
131 isc_mempool_t *nhmp; /*%< dns_adbnamehook_t */
132 isc_mempool_t *limp; /*%< dns_adblameinfo_t */
133 isc_mempool_t *emp; /*%< dns_adbentry_t */
134 isc_mempool_t *ahmp; /*%< dns_adbfind_t */
135 isc_mempool_t *aimp; /*%< dns_adbaddrinfo_t */
136 isc_mempool_t *afmp; /*%< dns_adbfetch_t */
139 * Bucketized locks and lists for names.
141 * XXXRTH Have a per-bucket structure that contains all of these?
143 dns_adbnamelist_t names[NBUCKETS];
144 dns_adbnamelist_t deadnames[NBUCKETS];
145 /*% See dns_adbnamelist_t */
146 isc_mutex_t namelocks[NBUCKETS];
147 /*% See dns_adbnamelist_t */
148 isc_boolean_t name_sd[NBUCKETS];
149 /*% See dns_adbnamelist_t */
150 unsigned int name_refcnt[NBUCKETS];
153 * Bucketized locks for entries.
155 * XXXRTH Have a per-bucket structure that contains all of these?
157 dns_adbentrylist_t entries[NBUCKETS];
158 dns_adbentrylist_t deadentries[NBUCKETS];
159 isc_mutex_t entrylocks[NBUCKETS];
160 isc_boolean_t entry_sd[NBUCKETS]; /*%< shutting down */
161 unsigned int entry_refcnt[NBUCKETS];
164 isc_boolean_t cevent_sent;
165 isc_boolean_t shutting_down;
166 isc_eventlist_t whenshutdown;
170 * XXXMLG Document these structures.
173 /*% dns_adbname structure */
178 unsigned int partial_result;
182 isc_stdtime_t expire_target;
183 isc_stdtime_t expire_v4;
184 isc_stdtime_t expire_v6;
186 dns_adbnamehooklist_t v4;
187 dns_adbnamehooklist_t v6;
188 dns_adbfetch_t *fetch_a;
189 dns_adbfetch_t *fetch_aaaa;
190 unsigned int fetch_err;
191 unsigned int fetch6_err;
192 dns_adbfindlist_t finds;
193 /* for LRU-based management */
194 isc_stdtime_t last_used;
196 ISC_LINK(dns_adbname_t) plink;
199 /*% The adbfetch structure */
200 struct dns_adbfetch {
203 dns_rdataset_t rdataset;
207 * This is a small widget that dangles off a dns_adbname_t. It contains a
208 * pointer to the address information about this host, and a link to the next
209 * namehook that will contain the next address this host has.
211 struct dns_adbnamehook {
213 dns_adbentry_t *entry;
214 ISC_LINK(dns_adbnamehook_t) plink;
218 * This is a small widget that holds qname-specific information about an
219 * address. Currently limited to lameness, but could just as easily be
220 * extended to other types of information about zones.
222 struct dns_adblameinfo {
226 dns_rdatatype_t qtype;
227 isc_stdtime_t lame_timer;
229 ISC_LINK(dns_adblameinfo_t) plink;
233 * An address entry. It holds quite a bit of information about addresses,
234 * including edns state (in "flags"), rtt, and of course the address of
237 struct dns_adbentry {
245 isc_sockaddr_t sockaddr;
247 isc_stdtime_t expires;
249 * A nonzero 'expires' field indicates that the entry should
250 * persist until that time. This allows entries found
251 * using dns_adb_findaddrinfo() to persist for a limited time
252 * even though they are not necessarily associated with a
256 ISC_LIST(dns_adblameinfo_t) lameinfo;
257 ISC_LINK(dns_adbentry_t) plink;
261 * Internal functions (and prototypes).
263 static inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
264 static inline void free_adbname(dns_adb_t *, dns_adbname_t **);
265 static inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
267 static inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
268 static inline dns_adblameinfo_t *new_adblameinfo(dns_adb_t *, dns_name_t *,
270 static inline void free_adblameinfo(dns_adb_t *, dns_adblameinfo_t **);
271 static inline dns_adbentry_t *new_adbentry(dns_adb_t *);
272 static inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
273 static inline dns_adbfind_t *new_adbfind(dns_adb_t *);
274 static inline isc_boolean_t free_adbfind(dns_adb_t *, dns_adbfind_t **);
275 static inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *, dns_adbentry_t *,
277 static inline dns_adbfetch_t *new_adbfetch(dns_adb_t *);
278 static inline void free_adbfetch(dns_adb_t *, dns_adbfetch_t **);
279 static inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
280 unsigned int, int *);
281 static inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
282 isc_sockaddr_t *, int *,
284 static void dump_adb(dns_adb_t *, FILE *, isc_boolean_t debug, isc_stdtime_t);
285 static void print_dns_name(FILE *, dns_name_t *);
286 static void print_namehook_list(FILE *, const char *legend,
287 dns_adbnamehooklist_t *list,
290 static void print_find_list(FILE *, dns_adbname_t *);
291 static void print_fetch_list(FILE *, dns_adbname_t *);
292 static inline isc_boolean_t dec_adb_irefcnt(dns_adb_t *);
293 static inline void inc_adb_irefcnt(dns_adb_t *);
294 static inline void inc_adb_erefcnt(dns_adb_t *);
295 static inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
297 static inline isc_boolean_t dec_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
299 static inline void violate_locking_hierarchy(isc_mutex_t *, isc_mutex_t *);
300 static isc_boolean_t clean_namehooks(dns_adb_t *, dns_adbnamehooklist_t *);
301 static void clean_target(dns_adb_t *, dns_name_t *);
302 static void clean_finds_at_name(dns_adbname_t *, isc_eventtype_t,
304 static isc_boolean_t check_expire_namehooks(dns_adbname_t *, isc_stdtime_t);
305 static isc_boolean_t check_expire_entry(dns_adb_t *, dns_adbentry_t **,
307 static void cancel_fetches_at_name(dns_adbname_t *);
308 static isc_result_t dbfind_name(dns_adbname_t *, isc_stdtime_t,
310 static isc_result_t fetch_name(dns_adbname_t *, isc_boolean_t,
312 static inline void check_exit(dns_adb_t *);
313 static void destroy(dns_adb_t *);
314 static isc_boolean_t shutdown_names(dns_adb_t *);
315 static isc_boolean_t shutdown_entries(dns_adb_t *);
316 static inline void link_name(dns_adb_t *, int, dns_adbname_t *);
317 static inline isc_boolean_t unlink_name(dns_adb_t *, dns_adbname_t *);
318 static inline void link_entry(dns_adb_t *, int, dns_adbentry_t *);
319 static inline isc_boolean_t unlink_entry(dns_adb_t *, dns_adbentry_t *);
320 static isc_boolean_t kill_name(dns_adbname_t **, isc_eventtype_t);
321 static void water(void *, int);
322 static void dump_entry(FILE *, dns_adbentry_t *, isc_boolean_t, isc_stdtime_t);
325 * MUST NOT overlap DNS_ADBFIND_* flags!
327 #define FIND_EVENT_SENT 0x40000000
328 #define FIND_EVENT_FREED 0x80000000
329 #define FIND_EVENTSENT(h) (((h)->flags & FIND_EVENT_SENT) != 0)
330 #define FIND_EVENTFREED(h) (((h)->flags & FIND_EVENT_FREED) != 0)
332 #define NAME_NEEDS_POKE 0x80000000
333 #define NAME_IS_DEAD 0x40000000
334 #define NAME_HINT_OK DNS_ADBFIND_HINTOK
335 #define NAME_GLUE_OK DNS_ADBFIND_GLUEOK
336 #define NAME_STARTATZONE DNS_ADBFIND_STARTATZONE
337 #define NAME_DEAD(n) (((n)->flags & NAME_IS_DEAD) != 0)
338 #define NAME_NEEDSPOKE(n) (((n)->flags & NAME_NEEDS_POKE) != 0)
339 #define NAME_GLUEOK(n) (((n)->flags & NAME_GLUE_OK) != 0)
340 #define NAME_HINTOK(n) (((n)->flags & NAME_HINT_OK) != 0)
343 * Private flag(s) for entries.
344 * MUST NOT overlap FCTX_ADDRINFO_xxx and DNS_FETCHOPT_NOEDNS0.
346 #define ENTRY_IS_DEAD 0x80000000
349 * To the name, address classes are all that really exist. If it has a
350 * V6 address it doesn't care if it came from a AAAA query.
352 #define NAME_HAS_V4(n) (!ISC_LIST_EMPTY((n)->v4))
353 #define NAME_HAS_V6(n) (!ISC_LIST_EMPTY((n)->v6))
354 #define NAME_HAS_ADDRS(n) (NAME_HAS_V4(n) || NAME_HAS_V6(n))
357 * Fetches are broken out into A and AAAA types. In some cases,
358 * however, it makes more sense to test for a particular class of fetches,
359 * like V4 or V6 above.
360 * Note: since we have removed the support of A6 in adb, FETCH_A and FETCH_AAAA
361 * are now equal to FETCH_V4 and FETCH_V6, respectively.
363 #define NAME_FETCH_A(n) ((n)->fetch_a != NULL)
364 #define NAME_FETCH_AAAA(n) ((n)->fetch_aaaa != NULL)
365 #define NAME_FETCH_V4(n) (NAME_FETCH_A(n))
366 #define NAME_FETCH_V6(n) (NAME_FETCH_AAAA(n))
367 #define NAME_FETCH(n) (NAME_FETCH_V4(n) || NAME_FETCH_V6(n))
370 * Find options and tests to see if there are addresses on the list.
372 #define FIND_WANTEVENT(fn) (((fn)->options & DNS_ADBFIND_WANTEVENT) != 0)
373 #define FIND_WANTEMPTYEVENT(fn) (((fn)->options & DNS_ADBFIND_EMPTYEVENT) != 0)
374 #define FIND_AVOIDFETCHES(fn) (((fn)->options & DNS_ADBFIND_AVOIDFETCHES) \
376 #define FIND_STARTATZONE(fn) (((fn)->options & DNS_ADBFIND_STARTATZONE) \
378 #define FIND_HINTOK(fn) (((fn)->options & DNS_ADBFIND_HINTOK) != 0)
379 #define FIND_GLUEOK(fn) (((fn)->options & DNS_ADBFIND_GLUEOK) != 0)
380 #define FIND_HAS_ADDRS(fn) (!ISC_LIST_EMPTY((fn)->list))
381 #define FIND_RETURNLAME(fn) (((fn)->options & DNS_ADBFIND_RETURNLAME) != 0)
384 * These are currently used on simple unsigned ints, so they are
385 * not really associated with any particular type.
387 #define WANT_INET(x) (((x) & DNS_ADBFIND_INET) != 0)
388 #define WANT_INET6(x) (((x) & DNS_ADBFIND_INET6) != 0)
390 #define EXPIRE_OK(exp, now) ((exp == INT_MAX) || (exp < now))
393 * Find out if the flags on a name (nf) indicate if it is a hint or
394 * glue, and compare this to the appropriate bits set in o, to see if
397 #define GLUE_OK(nf, o) (!NAME_GLUEOK(nf) || (((o) & DNS_ADBFIND_GLUEOK) != 0))
398 #define HINT_OK(nf, o) (!NAME_HINTOK(nf) || (((o) & DNS_ADBFIND_HINTOK) != 0))
399 #define GLUEHINT_OK(nf, o) (GLUE_OK(nf, o) || HINT_OK(nf, o))
400 #define STARTATZONE_MATCHES(nf, o) (((nf)->flags & NAME_STARTATZONE) == \
401 ((o) & DNS_ADBFIND_STARTATZONE))
403 #define ENTER_LEVEL ISC_LOG_DEBUG(50)
404 #define EXIT_LEVEL ENTER_LEVEL
405 #define CLEAN_LEVEL ISC_LOG_DEBUG(100)
406 #define DEF_LEVEL ISC_LOG_DEBUG(5)
407 #define NCACHE_LEVEL ISC_LOG_DEBUG(20)
409 #define NCACHE_RESULT(r) ((r) == DNS_R_NCACHENXDOMAIN || \
410 (r) == DNS_R_NCACHENXRRSET)
411 #define AUTH_NX(r) ((r) == DNS_R_NXDOMAIN || \
412 (r) == DNS_R_NXRRSET)
413 #define NXDOMAIN_RESULT(r) ((r) == DNS_R_NXDOMAIN || \
414 (r) == DNS_R_NCACHENXDOMAIN)
415 #define NXRRSET_RESULT(r) ((r) == DNS_R_NCACHENXRRSET || \
416 (r) == DNS_R_NXRRSET || \
417 (r) == DNS_R_HINTNXRRSET)
420 * Error state rankings.
423 #define FIND_ERR_SUCCESS 0 /* highest rank */
424 #define FIND_ERR_CANCELED 1
425 #define FIND_ERR_FAILURE 2
426 #define FIND_ERR_NXDOMAIN 3
427 #define FIND_ERR_NXRRSET 4
428 #define FIND_ERR_UNEXPECTED 5
429 #define FIND_ERR_NOTFOUND 6
430 #define FIND_ERR_MAX 7
432 static const char *errnames[] = {
442 #define NEWERR(old, new) (ISC_MIN((old), (new)))
444 static isc_result_t find_err_map[FIND_ERR_MAX] = {
451 ISC_R_NOTFOUND /* not YET found */
455 DP(int level, const char *format, ...) ISC_FORMAT_PRINTF(2, 3);
458 DP(int level, const char *format, ...) {
461 va_start(args, format);
462 isc_log_vwrite(dns_lctx,
463 DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ADB,
464 level, format, args);
469 * Increment resolver-related statistics counters.
472 inc_stats(dns_adb_t *adb, isc_statscounter_t counter) {
473 if (adb->view->resstats != NULL)
474 isc_stats_increment(adb->view->resstats, counter);
477 static inline dns_ttl_t
478 ttlclamp(dns_ttl_t ttl) {
479 if (ttl < ADB_CACHE_MINIMUM)
480 ttl = ADB_CACHE_MINIMUM;
481 if (ttl > ADB_CACHE_MAXIMUM)
482 ttl = ADB_CACHE_MAXIMUM;
488 * Requires the adbname bucket be locked and that no entry buckets be locked.
490 * This code handles A and AAAA rdatasets only.
493 import_rdataset(dns_adbname_t *adbname, dns_rdataset_t *rdataset,
498 dns_adbnamehook_t *nh;
499 dns_adbnamehook_t *anh;
500 dns_rdata_t rdata = DNS_RDATA_INIT;
502 struct in6_addr in6a;
503 isc_sockaddr_t sockaddr;
504 dns_adbentry_t *foundentry; /* NO CLEAN UP! */
506 isc_boolean_t new_addresses_added;
507 dns_rdatatype_t rdtype;
508 unsigned int findoptions;
509 dns_adbnamehooklist_t *hookhead;
511 INSIST(DNS_ADBNAME_VALID(adbname));
513 INSIST(DNS_ADB_VALID(adb));
515 rdtype = rdataset->type;
516 INSIST((rdtype == dns_rdatatype_a) || (rdtype == dns_rdatatype_aaaa));
517 if (rdtype == dns_rdatatype_a)
518 findoptions = DNS_ADBFIND_INET;
520 findoptions = DNS_ADBFIND_INET6;
522 addr_bucket = DNS_ADB_INVALIDBUCKET;
523 new_addresses_added = ISC_FALSE;
526 result = dns_rdataset_first(rdataset);
527 while (result == ISC_R_SUCCESS) {
528 dns_rdata_reset(&rdata);
529 dns_rdataset_current(rdataset, &rdata);
530 if (rdtype == dns_rdatatype_a) {
531 INSIST(rdata.length == 4);
532 memcpy(&ina.s_addr, rdata.data, 4);
533 isc_sockaddr_fromin(&sockaddr, &ina, 0);
534 hookhead = &adbname->v4;
536 INSIST(rdata.length == 16);
537 memcpy(in6a.s6_addr, rdata.data, 16);
538 isc_sockaddr_fromin6(&sockaddr, &in6a, 0);
539 hookhead = &adbname->v6;
543 nh = new_adbnamehook(adb, NULL);
545 adbname->partial_result |= findoptions;
546 result = ISC_R_NOMEMORY;
550 foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket,
552 if (foundentry == NULL) {
553 dns_adbentry_t *entry;
555 entry = new_adbentry(adb);
557 adbname->partial_result |= findoptions;
558 result = ISC_R_NOMEMORY;
562 entry->sockaddr = sockaddr;
567 link_entry(adb, addr_bucket, entry);
569 for (anh = ISC_LIST_HEAD(*hookhead);
571 anh = ISC_LIST_NEXT(anh, plink))
572 if (anh->entry == foundentry)
575 foundentry->refcnt++;
576 nh->entry = foundentry;
578 free_adbnamehook(adb, &nh);
581 new_addresses_added = ISC_TRUE;
583 ISC_LIST_APPEND(*hookhead, nh, plink);
585 result = dns_rdataset_next(rdataset);
590 free_adbnamehook(adb, &nh);
592 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
593 UNLOCK(&adb->entrylocks[addr_bucket]);
595 if (rdataset->trust == dns_trust_glue ||
596 rdataset->trust == dns_trust_additional)
597 rdataset->ttl = ADB_CACHE_MINIMUM;
599 rdataset->ttl = ttlclamp(rdataset->ttl);
601 if (rdtype == dns_rdatatype_a) {
602 DP(NCACHE_LEVEL, "expire_v4 set to MIN(%u,%u) import_rdataset",
603 adbname->expire_v4, now + rdataset->ttl);
604 adbname->expire_v4 = ISC_MIN(adbname->expire_v4,
605 now + rdataset->ttl);
607 DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) import_rdataset",
608 adbname->expire_v6, now + rdataset->ttl);
609 adbname->expire_v6 = ISC_MIN(adbname->expire_v6,
610 now + rdataset->ttl);
613 if (new_addresses_added) {
615 * Lie a little here. This is more or less so code that cares
616 * can find out if any new information was added or not.
618 return (ISC_R_SUCCESS);
625 * Requires the name's bucket be locked.
628 kill_name(dns_adbname_t **n, isc_eventtype_t ev) {
630 isc_boolean_t result = ISC_FALSE;
631 isc_boolean_t result4, result6;
638 INSIST(DNS_ADBNAME_VALID(name));
640 INSIST(DNS_ADB_VALID(adb));
642 DP(DEF_LEVEL, "killing name %p", name);
645 * If we're dead already, just check to see if we should go
648 if (NAME_DEAD(name) && !NAME_FETCH(name)) {
649 result = unlink_name(adb, name);
650 free_adbname(adb, &name);
652 result = dec_adb_irefcnt(adb);
657 * Clean up the name's various lists. These two are destructive
658 * in that they will always empty the list.
660 clean_finds_at_name(name, ev, DNS_ADBFIND_ADDRESSMASK);
661 result4 = clean_namehooks(adb, &name->v4);
662 result6 = clean_namehooks(adb, &name->v6);
663 clean_target(adb, &name->target);
664 result = ISC_TF(result4 || result6);
667 * If fetches are running, cancel them. If none are running, we can
668 * just kill the name here.
670 if (!NAME_FETCH(name)) {
671 INSIST(result == ISC_FALSE);
672 result = unlink_name(adb, name);
673 free_adbname(adb, &name);
675 result = dec_adb_irefcnt(adb);
677 cancel_fetches_at_name(name);
678 if (!NAME_DEAD(name)) {
679 bucket = name->lock_bucket;
680 ISC_LIST_UNLINK(adb->names[bucket], name, plink);
681 ISC_LIST_APPEND(adb->deadnames[bucket], name, plink);
682 name->flags |= NAME_IS_DEAD;
689 * Requires the name's bucket be locked and no entry buckets be locked.
692 check_expire_namehooks(dns_adbname_t *name, isc_stdtime_t now) {
694 isc_boolean_t result4 = ISC_FALSE;
695 isc_boolean_t result6 = ISC_FALSE;
697 INSIST(DNS_ADBNAME_VALID(name));
699 INSIST(DNS_ADB_VALID(adb));
702 * Check to see if we need to remove the v4 addresses
704 if (!NAME_FETCH_V4(name) && EXPIRE_OK(name->expire_v4, now)) {
705 if (NAME_HAS_V4(name)) {
706 DP(DEF_LEVEL, "expiring v4 for name %p", name);
707 result4 = clean_namehooks(adb, &name->v4);
708 name->partial_result &= ~DNS_ADBFIND_INET;
710 name->expire_v4 = INT_MAX;
711 name->fetch_err = FIND_ERR_UNEXPECTED;
715 * Check to see if we need to remove the v6 addresses
717 if (!NAME_FETCH_V6(name) && EXPIRE_OK(name->expire_v6, now)) {
718 if (NAME_HAS_V6(name)) {
719 DP(DEF_LEVEL, "expiring v6 for name %p", name);
720 result6 = clean_namehooks(adb, &name->v6);
721 name->partial_result &= ~DNS_ADBFIND_INET6;
723 name->expire_v6 = INT_MAX;
724 name->fetch6_err = FIND_ERR_UNEXPECTED;
728 * Check to see if we need to remove the alias target.
730 if (EXPIRE_OK(name->expire_target, now)) {
731 clean_target(adb, &name->target);
732 name->expire_target = INT_MAX;
734 return (ISC_TF(result4 || result6));
738 * Requires the name's bucket be locked.
741 link_name(dns_adb_t *adb, int bucket, dns_adbname_t *name) {
742 INSIST(name->lock_bucket == DNS_ADB_INVALIDBUCKET);
744 ISC_LIST_PREPEND(adb->names[bucket], name, plink);
745 name->lock_bucket = bucket;
746 adb->name_refcnt[bucket]++;
750 * Requires the name's bucket be locked.
752 static inline isc_boolean_t
753 unlink_name(dns_adb_t *adb, dns_adbname_t *name) {
755 isc_boolean_t result = ISC_FALSE;
757 bucket = name->lock_bucket;
758 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
761 ISC_LIST_UNLINK(adb->deadnames[bucket], name, plink);
763 ISC_LIST_UNLINK(adb->names[bucket], name, plink);
764 name->lock_bucket = DNS_ADB_INVALIDBUCKET;
765 INSIST(adb->name_refcnt[bucket] > 0);
766 adb->name_refcnt[bucket]--;
767 if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
773 * Requires the entry's bucket be locked.
776 link_entry(dns_adb_t *adb, int bucket, dns_adbentry_t *entry) {
781 for (i = 0; i < 2; i++) {
782 e = ISC_LIST_TAIL(adb->entries[bucket]);
785 if (e->refcnt == 0) {
786 unlink_entry(adb, e);
787 free_adbentry(adb, &e);
790 INSIST((e->flags & ENTRY_IS_DEAD) == 0);
791 e->flags |= ENTRY_IS_DEAD;
792 ISC_LIST_UNLINK(adb->entries[bucket], e, plink);
793 ISC_LIST_PREPEND(adb->deadentries[bucket], e, plink);
797 ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
798 entry->lock_bucket = bucket;
799 adb->entry_refcnt[bucket]++;
803 * Requires the entry's bucket be locked.
805 static inline isc_boolean_t
806 unlink_entry(dns_adb_t *adb, dns_adbentry_t *entry) {
808 isc_boolean_t result = ISC_FALSE;
810 bucket = entry->lock_bucket;
811 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
813 if ((entry->flags & ENTRY_IS_DEAD) != 0)
814 ISC_LIST_UNLINK(adb->deadentries[bucket], entry, plink);
816 ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
817 entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
818 INSIST(adb->entry_refcnt[bucket] > 0);
819 adb->entry_refcnt[bucket]--;
820 if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
826 violate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want) {
827 if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
835 * The ADB _MUST_ be locked before calling. Also, exit conditions must be
836 * checked after calling this function.
839 shutdown_names(dns_adb_t *adb) {
841 isc_boolean_t result = ISC_FALSE;
843 dns_adbname_t *next_name;
845 for (bucket = 0; bucket < NBUCKETS; bucket++) {
846 LOCK(&adb->namelocks[bucket]);
847 adb->name_sd[bucket] = ISC_TRUE;
849 name = ISC_LIST_HEAD(adb->names[bucket]);
852 * This bucket has no names. We must decrement the
853 * irefcnt ourselves, since it will not be
854 * automatically triggered by a name being unlinked.
856 INSIST(result == ISC_FALSE);
857 result = dec_adb_irefcnt(adb);
860 * Run through the list. For each name, clean up finds
861 * found there, and cancel any fetches running. When
862 * all the fetches are canceled, the name will destroy
865 while (name != NULL) {
866 next_name = ISC_LIST_NEXT(name, plink);
867 INSIST(result == ISC_FALSE);
868 result = kill_name(&name,
869 DNS_EVENT_ADBSHUTDOWN);
874 UNLOCK(&adb->namelocks[bucket]);
880 * The ADB _MUST_ be locked before calling. Also, exit conditions must be
881 * checked after calling this function.
884 shutdown_entries(dns_adb_t *adb) {
886 isc_boolean_t result = ISC_FALSE;
887 dns_adbentry_t *entry;
888 dns_adbentry_t *next_entry;
890 for (bucket = 0; bucket < NBUCKETS; bucket++) {
891 LOCK(&adb->entrylocks[bucket]);
892 adb->entry_sd[bucket] = ISC_TRUE;
894 entry = ISC_LIST_HEAD(adb->entries[bucket]);
895 if (adb->entry_refcnt[bucket] == 0) {
897 * This bucket has no entries. We must decrement the
898 * irefcnt ourselves, since it will not be
899 * automatically triggered by an entry being unlinked.
901 result = dec_adb_irefcnt(adb);
904 * Run through the list. Cleanup any entries not
905 * associated with names, and which are not in use.
907 while (entry != NULL) {
908 next_entry = ISC_LIST_NEXT(entry, plink);
909 if (entry->refcnt == 0 &&
910 entry->expires != 0) {
911 result = unlink_entry(adb, entry);
912 free_adbentry(adb, &entry);
914 result = dec_adb_irefcnt(adb);
920 UNLOCK(&adb->entrylocks[bucket]);
926 * Name bucket must be locked
929 cancel_fetches_at_name(dns_adbname_t *name) {
930 if (NAME_FETCH_A(name))
931 dns_resolver_cancelfetch(name->fetch_a->fetch);
933 if (NAME_FETCH_AAAA(name))
934 dns_resolver_cancelfetch(name->fetch_aaaa->fetch);
938 * Assumes the name bucket is locked.
941 clean_namehooks(dns_adb_t *adb, dns_adbnamehooklist_t *namehooks) {
942 dns_adbentry_t *entry;
943 dns_adbnamehook_t *namehook;
945 isc_boolean_t result = ISC_FALSE;
947 addr_bucket = DNS_ADB_INVALIDBUCKET;
948 namehook = ISC_LIST_HEAD(*namehooks);
949 while (namehook != NULL) {
950 INSIST(DNS_ADBNAMEHOOK_VALID(namehook));
953 * Clean up the entry if needed.
955 entry = namehook->entry;
957 INSIST(DNS_ADBENTRY_VALID(entry));
959 if (addr_bucket != entry->lock_bucket) {
960 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
961 UNLOCK(&adb->entrylocks[addr_bucket]);
962 addr_bucket = entry->lock_bucket;
963 LOCK(&adb->entrylocks[addr_bucket]);
966 result = dec_entry_refcnt(adb, entry, ISC_FALSE);
972 namehook->entry = NULL;
973 ISC_LIST_UNLINK(*namehooks, namehook, plink);
974 free_adbnamehook(adb, &namehook);
976 namehook = ISC_LIST_HEAD(*namehooks);
979 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
980 UNLOCK(&adb->entrylocks[addr_bucket]);
985 clean_target(dns_adb_t *adb, dns_name_t *target) {
986 if (dns_name_countlabels(target) > 0) {
987 dns_name_free(target, adb->mctx);
988 dns_name_init(target, NULL);
993 set_target(dns_adb_t *adb, dns_name_t *name, dns_name_t *fname,
994 dns_rdataset_t *rdataset, dns_name_t *target)
997 dns_namereln_t namereln;
998 unsigned int nlabels;
1000 dns_rdata_t rdata = DNS_RDATA_INIT;
1001 dns_fixedname_t fixed1, fixed2;
1002 dns_name_t *prefix, *new_target;
1004 REQUIRE(dns_name_countlabels(target) == 0);
1006 if (rdataset->type == dns_rdatatype_cname) {
1007 dns_rdata_cname_t cname;
1010 * Copy the CNAME's target into the target name.
1012 result = dns_rdataset_first(rdataset);
1013 if (result != ISC_R_SUCCESS)
1015 dns_rdataset_current(rdataset, &rdata);
1016 result = dns_rdata_tostruct(&rdata, &cname, NULL);
1017 if (result != ISC_R_SUCCESS)
1019 result = dns_name_dup(&cname.cname, adb->mctx, target);
1020 dns_rdata_freestruct(&cname);
1021 if (result != ISC_R_SUCCESS)
1024 dns_rdata_dname_t dname;
1026 INSIST(rdataset->type == dns_rdatatype_dname);
1027 namereln = dns_name_fullcompare(name, fname, &order, &nlabels);
1028 INSIST(namereln == dns_namereln_subdomain);
1030 * Get the target name of the DNAME.
1032 result = dns_rdataset_first(rdataset);
1033 if (result != ISC_R_SUCCESS)
1035 dns_rdataset_current(rdataset, &rdata);
1036 result = dns_rdata_tostruct(&rdata, &dname, NULL);
1037 if (result != ISC_R_SUCCESS)
1040 * Construct the new target name.
1042 dns_fixedname_init(&fixed1);
1043 prefix = dns_fixedname_name(&fixed1);
1044 dns_fixedname_init(&fixed2);
1045 new_target = dns_fixedname_name(&fixed2);
1046 dns_name_split(name, nlabels, prefix, NULL);
1047 result = dns_name_concatenate(prefix, &dname.dname, new_target,
1049 dns_rdata_freestruct(&dname);
1050 if (result != ISC_R_SUCCESS)
1052 result = dns_name_dup(new_target, adb->mctx, target);
1053 if (result != ISC_R_SUCCESS)
1057 return (ISC_R_SUCCESS);
1061 * Assumes nothing is locked, since this is called by the client.
1064 event_free(isc_event_t *event) {
1065 dns_adbfind_t *find;
1067 INSIST(event != NULL);
1068 find = event->ev_destroy_arg;
1069 INSIST(DNS_ADBFIND_VALID(find));
1072 find->flags |= FIND_EVENT_FREED;
1073 event->ev_destroy_arg = NULL;
1074 UNLOCK(&find->lock);
1078 * Assumes the name bucket is locked.
1081 clean_finds_at_name(dns_adbname_t *name, isc_eventtype_t evtype,
1086 dns_adbfind_t *find;
1087 dns_adbfind_t *next_find;
1088 isc_boolean_t process;
1089 unsigned int wanted, notify;
1092 "ENTER clean_finds_at_name, name %p, evtype %08x, addrs %08x",
1093 name, evtype, addrs);
1095 find = ISC_LIST_HEAD(name->finds);
1096 while (find != NULL) {
1098 next_find = ISC_LIST_NEXT(find, plink);
1100 process = ISC_FALSE;
1101 wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
1102 notify = wanted & addrs;
1105 case DNS_EVENT_ADBMOREADDRESSES:
1106 DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBMOREADDRESSES");
1107 if ((notify) != 0) {
1108 find->flags &= ~addrs;
1112 case DNS_EVENT_ADBNOMOREADDRESSES:
1113 DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBNOMOREADDRESSES");
1114 find->flags &= ~addrs;
1115 wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
1120 find->flags &= ~addrs;
1125 DP(DEF_LEVEL, "cfan: processing find %p", find);
1127 * Unlink the find from the name, letting the caller
1128 * call dns_adb_destroyfind() on it to clean it up
1131 ISC_LIST_UNLINK(name->finds, find, plink);
1132 find->adbname = NULL;
1133 find->name_bucket = DNS_ADB_INVALIDBUCKET;
1135 INSIST(!FIND_EVENTSENT(find));
1138 task = ev->ev_sender;
1139 ev->ev_sender = find;
1140 find->result_v4 = find_err_map[name->fetch_err];
1141 find->result_v6 = find_err_map[name->fetch6_err];
1142 ev->ev_type = evtype;
1143 ev->ev_destroy = event_free;
1144 ev->ev_destroy_arg = find;
1147 "sending event %p to task %p for find %p",
1150 isc_task_sendanddetach(&task, (isc_event_t **)&ev);
1152 DP(DEF_LEVEL, "cfan: skipping find %p", find);
1155 UNLOCK(&find->lock);
1159 DP(ENTER_LEVEL, "EXIT clean_finds_at_name, name %p", name);
1163 check_exit(dns_adb_t *adb) {
1166 * The caller must be holding the adb lock.
1168 if (adb->shutting_down) {
1170 * If there aren't any external references either, we're
1171 * done. Send the control event to initiate shutdown.
1173 INSIST(!adb->cevent_sent); /* Sanity check. */
1174 event = &adb->cevent;
1175 isc_task_send(adb->task, &event);
1176 adb->cevent_sent = ISC_TRUE;
1180 static inline isc_boolean_t
1181 dec_adb_irefcnt(dns_adb_t *adb) {
1184 isc_boolean_t result = ISC_FALSE;
1186 LOCK(&adb->reflock);
1188 INSIST(adb->irefcnt > 0);
1191 if (adb->irefcnt == 0) {
1192 event = ISC_LIST_HEAD(adb->whenshutdown);
1193 while (event != NULL) {
1194 ISC_LIST_UNLINK(adb->whenshutdown, event, ev_link);
1195 etask = event->ev_sender;
1196 event->ev_sender = adb;
1197 isc_task_sendanddetach(&etask, &event);
1198 event = ISC_LIST_HEAD(adb->whenshutdown);
1202 if (adb->irefcnt == 0 && adb->erefcnt == 0)
1204 UNLOCK(&adb->reflock);
1209 inc_adb_irefcnt(dns_adb_t *adb) {
1210 LOCK(&adb->reflock);
1212 UNLOCK(&adb->reflock);
1216 inc_adb_erefcnt(dns_adb_t *adb) {
1217 LOCK(&adb->reflock);
1219 UNLOCK(&adb->reflock);
1223 inc_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
1226 bucket = entry->lock_bucket;
1229 LOCK(&adb->entrylocks[bucket]);
1234 UNLOCK(&adb->entrylocks[bucket]);
1237 static inline isc_boolean_t
1238 dec_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
1240 isc_boolean_t destroy_entry;
1241 isc_boolean_t result = ISC_FALSE;
1243 bucket = entry->lock_bucket;
1246 LOCK(&adb->entrylocks[bucket]);
1248 INSIST(entry->refcnt > 0);
1251 destroy_entry = ISC_FALSE;
1252 if (entry->refcnt == 0 &&
1253 (adb->entry_sd[bucket] || entry->expires == 0 || adb->overmem ||
1254 (entry->flags & ENTRY_IS_DEAD) != 0)) {
1255 destroy_entry = ISC_TRUE;
1256 result = unlink_entry(adb, entry);
1260 UNLOCK(&adb->entrylocks[bucket]);
1265 entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
1267 free_adbentry(adb, &entry);
1269 result = dec_adb_irefcnt(adb);
1274 static inline dns_adbname_t *
1275 new_adbname(dns_adb_t *adb, dns_name_t *dnsname) {
1276 dns_adbname_t *name;
1278 name = isc_mempool_get(adb->nmp);
1282 dns_name_init(&name->name, NULL);
1283 if (dns_name_dup(dnsname, adb->mctx, &name->name) != ISC_R_SUCCESS) {
1284 isc_mempool_put(adb->nmp, name);
1287 dns_name_init(&name->target, NULL);
1288 name->magic = DNS_ADBNAME_MAGIC;
1290 name->partial_result = 0;
1292 name->expire_v4 = INT_MAX;
1293 name->expire_v6 = INT_MAX;
1294 name->expire_target = INT_MAX;
1296 name->lock_bucket = DNS_ADB_INVALIDBUCKET;
1297 ISC_LIST_INIT(name->v4);
1298 ISC_LIST_INIT(name->v6);
1299 name->fetch_a = NULL;
1300 name->fetch_aaaa = NULL;
1301 name->fetch_err = FIND_ERR_UNEXPECTED;
1302 name->fetch6_err = FIND_ERR_UNEXPECTED;
1303 ISC_LIST_INIT(name->finds);
1304 ISC_LINK_INIT(name, plink);
1310 free_adbname(dns_adb_t *adb, dns_adbname_t **name) {
1313 INSIST(name != NULL && DNS_ADBNAME_VALID(*name));
1317 INSIST(!NAME_HAS_V4(n));
1318 INSIST(!NAME_HAS_V6(n));
1319 INSIST(!NAME_FETCH(n));
1320 INSIST(ISC_LIST_EMPTY(n->finds));
1321 INSIST(!ISC_LINK_LINKED(n, plink));
1322 INSIST(n->lock_bucket == DNS_ADB_INVALIDBUCKET);
1323 INSIST(n->adb == adb);
1326 dns_name_free(&n->name, adb->mctx);
1328 isc_mempool_put(adb->nmp, n);
1331 static inline dns_adbnamehook_t *
1332 new_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry) {
1333 dns_adbnamehook_t *nh;
1335 nh = isc_mempool_get(adb->nhmp);
1339 nh->magic = DNS_ADBNAMEHOOK_MAGIC;
1341 ISC_LINK_INIT(nh, plink);
1347 free_adbnamehook(dns_adb_t *adb, dns_adbnamehook_t **namehook) {
1348 dns_adbnamehook_t *nh;
1350 INSIST(namehook != NULL && DNS_ADBNAMEHOOK_VALID(*namehook));
1354 INSIST(nh->entry == NULL);
1355 INSIST(!ISC_LINK_LINKED(nh, plink));
1358 isc_mempool_put(adb->nhmp, nh);
1361 static inline dns_adblameinfo_t *
1362 new_adblameinfo(dns_adb_t *adb, dns_name_t *qname, dns_rdatatype_t qtype) {
1363 dns_adblameinfo_t *li;
1365 li = isc_mempool_get(adb->limp);
1369 dns_name_init(&li->qname, NULL);
1370 if (dns_name_dup(qname, adb->mctx, &li->qname) != ISC_R_SUCCESS) {
1371 isc_mempool_put(adb->limp, li);
1374 li->magic = DNS_ADBLAMEINFO_MAGIC;
1377 ISC_LINK_INIT(li, plink);
1383 free_adblameinfo(dns_adb_t *adb, dns_adblameinfo_t **lameinfo) {
1384 dns_adblameinfo_t *li;
1386 INSIST(lameinfo != NULL && DNS_ADBLAMEINFO_VALID(*lameinfo));
1390 INSIST(!ISC_LINK_LINKED(li, plink));
1392 dns_name_free(&li->qname, adb->mctx);
1396 isc_mempool_put(adb->limp, li);
1399 static inline dns_adbentry_t *
1400 new_adbentry(dns_adb_t *adb) {
1404 e = isc_mempool_get(adb->emp);
1408 e->magic = DNS_ADBENTRY_MAGIC;
1409 e->lock_bucket = DNS_ADB_INVALIDBUCKET;
1413 e->srtt = (r & 0x1f) + 1;
1415 ISC_LIST_INIT(e->lameinfo);
1416 ISC_LINK_INIT(e, plink);
1422 free_adbentry(dns_adb_t *adb, dns_adbentry_t **entry) {
1424 dns_adblameinfo_t *li;
1426 INSIST(entry != NULL && DNS_ADBENTRY_VALID(*entry));
1430 INSIST(e->lock_bucket == DNS_ADB_INVALIDBUCKET);
1431 INSIST(e->refcnt == 0);
1432 INSIST(!ISC_LINK_LINKED(e, plink));
1436 li = ISC_LIST_HEAD(e->lameinfo);
1437 while (li != NULL) {
1438 ISC_LIST_UNLINK(e->lameinfo, li, plink);
1439 free_adblameinfo(adb, &li);
1440 li = ISC_LIST_HEAD(e->lameinfo);
1443 isc_mempool_put(adb->emp, e);
1446 static inline dns_adbfind_t *
1447 new_adbfind(dns_adb_t *adb) {
1449 isc_result_t result;
1451 h = isc_mempool_get(adb->ahmp);
1460 h->partial_result = 0;
1463 h->result_v4 = ISC_R_UNEXPECTED;
1464 h->result_v6 = ISC_R_UNEXPECTED;
1465 ISC_LINK_INIT(h, publink);
1466 ISC_LINK_INIT(h, plink);
1467 ISC_LIST_INIT(h->list);
1469 h->name_bucket = DNS_ADB_INVALIDBUCKET;
1474 result = isc_mutex_init(&h->lock);
1475 if (result != ISC_R_SUCCESS) {
1476 isc_mempool_put(adb->ahmp, h);
1480 ISC_EVENT_INIT(&h->event, sizeof(isc_event_t), 0, 0, 0, NULL, NULL,
1483 inc_adb_irefcnt(adb);
1484 h->magic = DNS_ADBFIND_MAGIC;
1488 static inline dns_adbfetch_t *
1489 new_adbfetch(dns_adb_t *adb) {
1492 f = isc_mempool_get(adb->afmp);
1499 dns_rdataset_init(&f->rdataset);
1501 f->magic = DNS_ADBFETCH_MAGIC;
1507 free_adbfetch(dns_adb_t *adb, dns_adbfetch_t **fetch) {
1510 INSIST(fetch != NULL && DNS_ADBFETCH_VALID(*fetch));
1516 if (dns_rdataset_isassociated(&f->rdataset))
1517 dns_rdataset_disassociate(&f->rdataset);
1519 isc_mempool_put(adb->afmp, f);
1522 static inline isc_boolean_t
1523 free_adbfind(dns_adb_t *adb, dns_adbfind_t **findp) {
1524 dns_adbfind_t *find;
1526 INSIST(findp != NULL && DNS_ADBFIND_VALID(*findp));
1530 INSIST(!FIND_HAS_ADDRS(find));
1531 INSIST(!ISC_LINK_LINKED(find, publink));
1532 INSIST(!ISC_LINK_LINKED(find, plink));
1533 INSIST(find->name_bucket == DNS_ADB_INVALIDBUCKET);
1534 INSIST(find->adbname == NULL);
1538 DESTROYLOCK(&find->lock);
1539 isc_mempool_put(adb->ahmp, find);
1540 return (dec_adb_irefcnt(adb));
1544 * Copy bits from the entry into the newly allocated addrinfo. The entry
1545 * must be locked, and the reference count must be bumped up by one
1546 * if this function returns a valid pointer.
1548 static inline dns_adbaddrinfo_t *
1549 new_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry, in_port_t port) {
1550 dns_adbaddrinfo_t *ai;
1552 ai = isc_mempool_get(adb->aimp);
1556 ai->magic = DNS_ADBADDRINFO_MAGIC;
1557 ai->sockaddr = entry->sockaddr;
1558 isc_sockaddr_setport(&ai->sockaddr, port);
1559 ai->srtt = entry->srtt;
1560 ai->flags = entry->flags;
1562 ISC_LINK_INIT(ai, publink);
1568 free_adbaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **ainfo) {
1569 dns_adbaddrinfo_t *ai;
1571 INSIST(ainfo != NULL && DNS_ADBADDRINFO_VALID(*ainfo));
1575 INSIST(ai->entry == NULL);
1576 INSIST(!ISC_LINK_LINKED(ai, publink));
1580 isc_mempool_put(adb->aimp, ai);
1584 * Search for the name. NOTE: The bucket is kept locked on both
1585 * success and failure, so it must always be unlocked by the caller!
1587 * On the first call to this function, *bucketp must be set to
1588 * DNS_ADB_INVALIDBUCKET.
1590 static inline dns_adbname_t *
1591 find_name_and_lock(dns_adb_t *adb, dns_name_t *name,
1592 unsigned int options, int *bucketp)
1594 dns_adbname_t *adbname;
1597 bucket = dns_name_fullhash(name, ISC_FALSE) % NBUCKETS;
1599 if (*bucketp == DNS_ADB_INVALIDBUCKET) {
1600 LOCK(&adb->namelocks[bucket]);
1602 } else if (*bucketp != bucket) {
1603 UNLOCK(&adb->namelocks[*bucketp]);
1604 LOCK(&adb->namelocks[bucket]);
1608 adbname = ISC_LIST_HEAD(adb->names[bucket]);
1609 while (adbname != NULL) {
1610 if (!NAME_DEAD(adbname)) {
1611 if (dns_name_equal(name, &adbname->name)
1612 && GLUEHINT_OK(adbname, options)
1613 && STARTATZONE_MATCHES(adbname, options))
1616 adbname = ISC_LIST_NEXT(adbname, plink);
1623 * Search for the address. NOTE: The bucket is kept locked on both
1624 * success and failure, so it must always be unlocked by the caller.
1626 * On the first call to this function, *bucketp must be set to
1627 * DNS_ADB_INVALIDBUCKET. This will cause a lock to occur. On
1628 * later calls (within the same "lock path") it can be left alone, so
1629 * if this function is called multiple times locking is only done if
1630 * the bucket changes.
1632 static inline dns_adbentry_t *
1633 find_entry_and_lock(dns_adb_t *adb, isc_sockaddr_t *addr, int *bucketp,
1636 dns_adbentry_t *entry, *entry_next;
1639 bucket = isc_sockaddr_hash(addr, ISC_TRUE) % NBUCKETS;
1641 if (*bucketp == DNS_ADB_INVALIDBUCKET) {
1642 LOCK(&adb->entrylocks[bucket]);
1644 } else if (*bucketp != bucket) {
1645 UNLOCK(&adb->entrylocks[*bucketp]);
1646 LOCK(&adb->entrylocks[bucket]);
1650 /* Search the list, while cleaning up expired entries. */
1651 for (entry = ISC_LIST_HEAD(adb->entries[bucket]);
1653 entry = entry_next) {
1654 entry_next = ISC_LIST_NEXT(entry, plink);
1655 (void)check_expire_entry(adb, &entry, now);
1656 if (entry != NULL &&
1657 isc_sockaddr_equal(addr, &entry->sockaddr)) {
1658 ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
1659 ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
1668 * Entry bucket MUST be locked!
1670 static isc_boolean_t
1671 entry_is_lame(dns_adb_t *adb, dns_adbentry_t *entry, dns_name_t *qname,
1672 dns_rdatatype_t qtype, isc_stdtime_t now)
1674 dns_adblameinfo_t *li, *next_li;
1675 isc_boolean_t is_bad;
1679 li = ISC_LIST_HEAD(entry->lameinfo);
1682 while (li != NULL) {
1683 next_li = ISC_LIST_NEXT(li, plink);
1686 * Has the entry expired?
1688 if (li->lame_timer < now) {
1689 ISC_LIST_UNLINK(entry->lameinfo, li, plink);
1690 free_adblameinfo(adb, &li);
1694 * Order tests from least to most expensive.
1696 * We do not break out of the main loop here as
1697 * we use the loop for house keeping.
1699 if (li != NULL && !is_bad && li->qtype == qtype &&
1700 dns_name_equal(qname, &li->qname))
1710 copy_namehook_lists(dns_adb_t *adb, dns_adbfind_t *find, dns_name_t *qname,
1711 dns_rdatatype_t qtype, dns_adbname_t *name,
1714 dns_adbnamehook_t *namehook;
1715 dns_adbaddrinfo_t *addrinfo;
1716 dns_adbentry_t *entry;
1719 bucket = DNS_ADB_INVALIDBUCKET;
1721 if (find->options & DNS_ADBFIND_INET) {
1722 namehook = ISC_LIST_HEAD(name->v4);
1723 while (namehook != NULL) {
1724 entry = namehook->entry;
1725 bucket = entry->lock_bucket;
1726 LOCK(&adb->entrylocks[bucket]);
1728 if (!FIND_RETURNLAME(find)
1729 && entry_is_lame(adb, entry, qname, qtype, now)) {
1730 find->options |= DNS_ADBFIND_LAMEPRUNED;
1733 addrinfo = new_adbaddrinfo(adb, entry, find->port);
1734 if (addrinfo == NULL) {
1735 find->partial_result |= DNS_ADBFIND_INET;
1739 * Found a valid entry. Add it to the find's list.
1741 inc_entry_refcnt(adb, entry, ISC_FALSE);
1742 ISC_LIST_APPEND(find->list, addrinfo, publink);
1745 UNLOCK(&adb->entrylocks[bucket]);
1746 bucket = DNS_ADB_INVALIDBUCKET;
1747 namehook = ISC_LIST_NEXT(namehook, plink);
1751 if (find->options & DNS_ADBFIND_INET6) {
1752 namehook = ISC_LIST_HEAD(name->v6);
1753 while (namehook != NULL) {
1754 entry = namehook->entry;
1755 bucket = entry->lock_bucket;
1756 LOCK(&adb->entrylocks[bucket]);
1758 if (!FIND_RETURNLAME(find)
1759 && entry_is_lame(adb, entry, qname, qtype, now)) {
1760 find->options |= DNS_ADBFIND_LAMEPRUNED;
1763 addrinfo = new_adbaddrinfo(adb, entry, find->port);
1764 if (addrinfo == NULL) {
1765 find->partial_result |= DNS_ADBFIND_INET6;
1769 * Found a valid entry. Add it to the find's list.
1771 inc_entry_refcnt(adb, entry, ISC_FALSE);
1772 ISC_LIST_APPEND(find->list, addrinfo, publink);
1775 UNLOCK(&adb->entrylocks[bucket]);
1776 bucket = DNS_ADB_INVALIDBUCKET;
1777 namehook = ISC_LIST_NEXT(namehook, plink);
1782 if (bucket != DNS_ADB_INVALIDBUCKET)
1783 UNLOCK(&adb->entrylocks[bucket]);
1787 shutdown_task(isc_task_t *task, isc_event_t *ev) {
1793 INSIST(DNS_ADB_VALID(adb));
1795 isc_event_free(&ev);
1797 * Wait for lock around check_exit() call to be released.
1805 * Name bucket must be locked; adb may be locked; no other locks held.
1807 static isc_boolean_t
1808 check_expire_name(dns_adbname_t **namep, isc_stdtime_t now) {
1809 dns_adbname_t *name;
1810 isc_boolean_t result = ISC_FALSE;
1812 INSIST(namep != NULL && DNS_ADBNAME_VALID(*namep));
1815 if (NAME_HAS_V4(name) || NAME_HAS_V6(name))
1817 if (NAME_FETCH(name))
1819 if (!EXPIRE_OK(name->expire_v4, now))
1821 if (!EXPIRE_OK(name->expire_v6, now))
1823 if (!EXPIRE_OK(name->expire_target, now))
1827 * The name is empty. Delete it.
1829 result = kill_name(&name, DNS_EVENT_ADBEXPIRED);
1833 * Our caller, or one of its callers, will be calling check_exit() at
1834 * some point, so we don't need to do it here.
1840 * Examine the tail entry of the LRU list to see if it expires or is stale
1841 * (unused for some period); if so, the name entry will be freed. If the ADB
1842 * is in the overmem condition, the tail and the next to tail entries
1843 * will be unconditionally removed (unless they have an outstanding fetch).
1844 * We don't care about a race on 'overmem' at the risk of causing some
1845 * collateral damage or a small delay in starting cleanup, so we don't bother
1846 * to lock ADB (if it's not locked).
1848 * Name bucket must be locked; adb may be locked; no other locks held.
1851 check_stale_name(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
1852 int victims, max_victims;
1853 isc_boolean_t result;
1854 dns_adbname_t *victim, *next_victim;
1855 isc_boolean_t overmem = adb->overmem;
1858 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
1860 max_victims = overmem ? 2 : 1;
1863 * We limit the number of scanned entries to 10 (arbitrary choice)
1864 * in order to avoid examining too many entries when there are many
1865 * tail entries that have fetches (this should be rare, but could
1868 victim = ISC_LIST_TAIL(adb->names[bucket]);
1870 victim != NULL && victims < max_victims && scans < 10;
1871 victim = next_victim) {
1872 INSIST(!NAME_DEAD(victim));
1874 next_victim = ISC_LIST_PREV(victim, plink);
1875 result = check_expire_name(&victim, now);
1876 if (victim == NULL) {
1881 if (!NAME_FETCH(victim) &&
1882 (overmem || victim->last_used + ADB_STALE_MARGIN <= now)) {
1883 RUNTIME_CHECK(kill_name(&victim,
1884 DNS_EVENT_ADBCANCELED) ==
1896 * Entry bucket must be locked; adb may be locked; no other locks held.
1898 static isc_boolean_t
1899 check_expire_entry(dns_adb_t *adb, dns_adbentry_t **entryp, isc_stdtime_t now)
1901 dns_adbentry_t *entry;
1902 isc_boolean_t result = ISC_FALSE;
1904 INSIST(entryp != NULL && DNS_ADBENTRY_VALID(*entryp));
1907 if (entry->refcnt != 0)
1910 if (entry->expires == 0 || entry->expires > now)
1914 * The entry is not in use. Delete it.
1916 DP(DEF_LEVEL, "killing entry %p", entry);
1917 INSIST(ISC_LINK_LINKED(entry, plink));
1918 result = unlink_entry(adb, entry);
1919 free_adbentry(adb, &entry);
1921 dec_adb_irefcnt(adb);
1927 * ADB must be locked, and no other locks held.
1929 static isc_boolean_t
1930 cleanup_names(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
1931 dns_adbname_t *name;
1932 dns_adbname_t *next_name;
1933 isc_boolean_t result = ISC_FALSE;
1935 DP(CLEAN_LEVEL, "cleaning name bucket %d", bucket);
1937 LOCK(&adb->namelocks[bucket]);
1938 if (adb->name_sd[bucket]) {
1939 UNLOCK(&adb->namelocks[bucket]);
1943 name = ISC_LIST_HEAD(adb->names[bucket]);
1944 while (name != NULL) {
1945 next_name = ISC_LIST_NEXT(name, plink);
1946 INSIST(result == ISC_FALSE);
1947 result = check_expire_namehooks(name, now);
1949 result = check_expire_name(&name, now);
1952 UNLOCK(&adb->namelocks[bucket]);
1957 * ADB must be locked, and no other locks held.
1959 static isc_boolean_t
1960 cleanup_entries(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
1961 dns_adbentry_t *entry, *next_entry;
1962 isc_boolean_t result = ISC_FALSE;
1964 DP(CLEAN_LEVEL, "cleaning entry bucket %d", bucket);
1966 LOCK(&adb->entrylocks[bucket]);
1967 entry = ISC_LIST_HEAD(adb->entries[bucket]);
1968 while (entry != NULL) {
1969 next_entry = ISC_LIST_NEXT(entry, plink);
1970 INSIST(result == ISC_FALSE);
1971 result = check_expire_entry(adb, &entry, now);
1974 UNLOCK(&adb->entrylocks[bucket]);
1979 destroy(dns_adb_t *adb) {
1982 isc_task_detach(&adb->task);
1984 isc_mempool_destroy(&adb->nmp);
1985 isc_mempool_destroy(&adb->nhmp);
1986 isc_mempool_destroy(&adb->limp);
1987 isc_mempool_destroy(&adb->emp);
1988 isc_mempool_destroy(&adb->ahmp);
1989 isc_mempool_destroy(&adb->aimp);
1990 isc_mempool_destroy(&adb->afmp);
1992 DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
1993 DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
1995 DESTROYLOCK(&adb->reflock);
1996 DESTROYLOCK(&adb->lock);
1997 DESTROYLOCK(&adb->mplock);
1998 DESTROYLOCK(&adb->overmemlock);
2000 isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
2009 dns_adb_create(isc_mem_t *mem, dns_view_t *view, isc_timermgr_t *timermgr,
2010 isc_taskmgr_t *taskmgr, dns_adb_t **newadb)
2013 isc_result_t result;
2016 REQUIRE(mem != NULL);
2017 REQUIRE(view != NULL);
2018 REQUIRE(timermgr != NULL); /* this is actually unused */
2019 REQUIRE(taskmgr != NULL);
2020 REQUIRE(newadb != NULL && *newadb == NULL);
2024 adb = isc_mem_get(mem, sizeof(dns_adb_t));
2026 return (ISC_R_NOMEMORY);
2029 * Initialize things here that cannot fail, and especially things
2030 * that must be NULL for the error return to work properly.
2045 adb->taskmgr = taskmgr;
2046 adb->next_cleanbucket = 0;
2047 ISC_EVENT_INIT(&adb->cevent, sizeof(adb->cevent), 0, NULL,
2048 DNS_EVENT_ADBCONTROL, shutdown_task, adb,
2050 adb->cevent_sent = ISC_FALSE;
2051 adb->shutting_down = ISC_FALSE;
2052 adb->overmem = ISC_FALSE;
2053 ISC_LIST_INIT(adb->whenshutdown);
2055 isc_mem_attach(mem, &adb->mctx);
2057 result = isc_mutex_init(&adb->lock);
2058 if (result != ISC_R_SUCCESS)
2061 result = isc_mutex_init(&adb->mplock);
2062 if (result != ISC_R_SUCCESS)
2065 result = isc_mutex_init(&adb->reflock);
2066 if (result != ISC_R_SUCCESS)
2069 result = isc_mutex_init(&adb->overmemlock);
2070 if (result != ISC_R_SUCCESS)
2074 * Initialize the bucket locks for names and elements.
2075 * May as well initialize the list heads, too.
2077 result = isc_mutexblock_init(adb->namelocks, NBUCKETS);
2078 if (result != ISC_R_SUCCESS)
2080 for (i = 0; i < NBUCKETS; i++) {
2081 ISC_LIST_INIT(adb->names[i]);
2082 ISC_LIST_INIT(adb->deadnames[i]);
2083 adb->name_sd[i] = ISC_FALSE;
2084 adb->name_refcnt[i] = 0;
2087 for (i = 0; i < NBUCKETS; i++) {
2088 ISC_LIST_INIT(adb->entries[i]);
2089 ISC_LIST_INIT(adb->deadentries[i]);
2090 adb->entry_sd[i] = ISC_FALSE;
2091 adb->entry_refcnt[i] = 0;
2094 result = isc_mutexblock_init(adb->entrylocks, NBUCKETS);
2095 if (result != ISC_R_SUCCESS)
2101 #define MPINIT(t, p, n) do { \
2102 result = isc_mempool_create(mem, sizeof(t), &(p)); \
2103 if (result != ISC_R_SUCCESS) \
2105 isc_mempool_setfreemax((p), FREE_ITEMS); \
2106 isc_mempool_setfillcount((p), FILL_COUNT); \
2107 isc_mempool_setname((p), n); \
2108 isc_mempool_associatelock((p), &adb->mplock); \
2111 MPINIT(dns_adbname_t, adb->nmp, "adbname");
2112 MPINIT(dns_adbnamehook_t, adb->nhmp, "adbnamehook");
2113 MPINIT(dns_adblameinfo_t, adb->limp, "adblameinfo");
2114 MPINIT(dns_adbentry_t, adb->emp, "adbentry");
2115 MPINIT(dns_adbfind_t, adb->ahmp, "adbfind");
2116 MPINIT(dns_adbaddrinfo_t, adb->aimp, "adbaddrinfo");
2117 MPINIT(dns_adbfetch_t, adb->afmp, "adbfetch");
2122 * Allocate an internal task.
2124 result = isc_task_create(adb->taskmgr, 0, &adb->task);
2125 if (result != ISC_R_SUCCESS)
2127 isc_task_setname(adb->task, "ADB", adb);
2132 adb->magic = DNS_ADB_MAGIC;
2134 return (ISC_R_SUCCESS);
2137 if (adb->task != NULL)
2138 isc_task_detach(&adb->task);
2140 /* clean up entrylocks */
2141 DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
2143 fail2: /* clean up namelocks */
2144 DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
2146 fail1: /* clean up only allocated memory */
2147 if (adb->nmp != NULL)
2148 isc_mempool_destroy(&adb->nmp);
2149 if (adb->nhmp != NULL)
2150 isc_mempool_destroy(&adb->nhmp);
2151 if (adb->limp != NULL)
2152 isc_mempool_destroy(&adb->limp);
2153 if (adb->emp != NULL)
2154 isc_mempool_destroy(&adb->emp);
2155 if (adb->ahmp != NULL)
2156 isc_mempool_destroy(&adb->ahmp);
2157 if (adb->aimp != NULL)
2158 isc_mempool_destroy(&adb->aimp);
2159 if (adb->afmp != NULL)
2160 isc_mempool_destroy(&adb->afmp);
2162 DESTROYLOCK(&adb->overmemlock);
2164 DESTROYLOCK(&adb->reflock);
2166 DESTROYLOCK(&adb->mplock);
2168 DESTROYLOCK(&adb->lock);
2170 isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
2176 dns_adb_attach(dns_adb_t *adb, dns_adb_t **adbx) {
2178 REQUIRE(DNS_ADB_VALID(adb));
2179 REQUIRE(adbx != NULL && *adbx == NULL);
2181 inc_adb_erefcnt(adb);
2186 dns_adb_detach(dns_adb_t **adbx) {
2188 isc_boolean_t need_exit_check;
2190 REQUIRE(adbx != NULL && DNS_ADB_VALID(*adbx));
2195 INSIST(adb->erefcnt > 0);
2197 LOCK(&adb->reflock);
2199 need_exit_check = ISC_TF(adb->erefcnt == 0 && adb->irefcnt == 0);
2200 UNLOCK(&adb->reflock);
2202 if (need_exit_check) {
2204 INSIST(adb->shutting_down);
2211 dns_adb_whenshutdown(dns_adb_t *adb, isc_task_t *task, isc_event_t **eventp) {
2214 isc_boolean_t zeroirefcnt = ISC_FALSE;
2217 * Send '*eventp' to 'task' when 'adb' has shutdown.
2220 REQUIRE(DNS_ADB_VALID(adb));
2221 REQUIRE(eventp != NULL);
2228 LOCK(&adb->reflock);
2229 zeroirefcnt = ISC_TF(adb->irefcnt == 0);
2231 if (adb->shutting_down && zeroirefcnt &&
2232 isc_mempool_getallocated(adb->ahmp) == 0) {
2234 * We're already shutdown. Send the event.
2236 event->ev_sender = adb;
2237 isc_task_send(task, &event);
2240 isc_task_attach(task, &clone);
2241 event->ev_sender = clone;
2242 ISC_LIST_APPEND(adb->whenshutdown, event, ev_link);
2245 UNLOCK(&adb->reflock);
2250 dns_adb_shutdown(dns_adb_t *adb) {
2251 isc_boolean_t need_check_exit;
2259 if (!adb->shutting_down) {
2260 adb->shutting_down = ISC_TRUE;
2261 isc_mem_setwater(adb->mctx, water, adb, 0, 0);
2262 need_check_exit = shutdown_names(adb);
2263 if (!need_check_exit)
2264 need_check_exit = shutdown_entries(adb);
2265 if (need_check_exit)
2273 dns_adb_createfind(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
2274 void *arg, dns_name_t *name, dns_name_t *qname,
2275 dns_rdatatype_t qtype, unsigned int options,
2276 isc_stdtime_t now, dns_name_t *target,
2277 in_port_t port, dns_adbfind_t **findp)
2279 dns_adbfind_t *find;
2280 dns_adbname_t *adbname;
2282 isc_boolean_t want_event, start_at_zone, alias, have_address;
2283 isc_result_t result;
2284 unsigned int wanted_addresses;
2285 unsigned int wanted_fetches;
2286 unsigned int query_pending;
2288 REQUIRE(DNS_ADB_VALID(adb));
2290 REQUIRE(action != NULL);
2292 REQUIRE(name != NULL);
2293 REQUIRE(qname != NULL);
2294 REQUIRE(findp != NULL && *findp == NULL);
2295 REQUIRE(target == NULL || dns_name_hasbuffer(target));
2297 REQUIRE((options & DNS_ADBFIND_ADDRESSMASK) != 0);
2299 result = ISC_R_UNEXPECTED;
2300 wanted_addresses = (options & DNS_ADBFIND_ADDRESSMASK);
2303 want_event = ISC_FALSE;
2304 start_at_zone = ISC_FALSE;
2308 isc_stdtime_get(&now);
2311 * XXXMLG Move this comment somewhere else!
2313 * Look up the name in our internal database.
2315 * Possibilities: Note that these are not always exclusive.
2317 * No name found. In this case, allocate a new name header and
2318 * an initial namehook or two. If any of these allocations
2319 * fail, clean up and return ISC_R_NOMEMORY.
2321 * Name found, valid addresses present. Allocate one addrinfo
2322 * structure for each found and append it to the linked list
2323 * of addresses for this header.
2325 * Name found, queries pending. In this case, if a task was
2326 * passed in, allocate a job id, attach it to the name's job
2327 * list and remember to tell the caller that there will be
2328 * more info coming later.
2331 find = new_adbfind(adb);
2333 return (ISC_R_NOMEMORY);
2338 * Remember what types of addresses we are interested in.
2340 find->options = options;
2341 find->flags |= wanted_addresses;
2342 if (FIND_WANTEVENT(find)) {
2343 REQUIRE(task != NULL);
2347 * Try to see if we know anything about this name at all.
2349 bucket = DNS_ADB_INVALIDBUCKET;
2350 adbname = find_name_and_lock(adb, name, find->options, &bucket);
2351 if (adb->name_sd[bucket]) {
2353 "dns_adb_createfind: returning ISC_R_SHUTTINGDOWN");
2354 RUNTIME_CHECK(free_adbfind(adb, &find) == ISC_FALSE);
2355 result = ISC_R_SHUTTINGDOWN;
2360 * Nothing found. Allocate a new adbname structure for this name.
2362 if (adbname == NULL) {
2364 * See if there is any stale name at the end of list, and purge
2367 check_stale_name(adb, bucket, now);
2369 adbname = new_adbname(adb, name);
2370 if (adbname == NULL) {
2371 RUNTIME_CHECK(free_adbfind(adb, &find) == ISC_FALSE);
2372 result = ISC_R_NOMEMORY;
2375 link_name(adb, bucket, adbname);
2376 if (FIND_HINTOK(find))
2377 adbname->flags |= NAME_HINT_OK;
2378 if (FIND_GLUEOK(find))
2379 adbname->flags |= NAME_GLUE_OK;
2380 if (FIND_STARTATZONE(find))
2381 adbname->flags |= NAME_STARTATZONE;
2383 /* Move this name forward in the LRU list */
2384 ISC_LIST_UNLINK(adb->names[bucket], adbname, plink);
2385 ISC_LIST_PREPEND(adb->names[bucket], adbname, plink);
2387 adbname->last_used = now;
2390 * Expire old entries, etc.
2392 RUNTIME_CHECK(check_expire_namehooks(adbname, now) == ISC_FALSE);
2395 * Do we know that the name is an alias?
2397 if (!EXPIRE_OK(adbname->expire_target, now)) {
2402 "dns_adb_createfind: name %p is an alias (cached)",
2409 * Try to populate the name from the database and/or
2410 * start fetches. First try looking for an A record
2413 if (!NAME_HAS_V4(adbname) && EXPIRE_OK(adbname->expire_v4, now)
2414 && WANT_INET(wanted_addresses)) {
2415 result = dbfind_name(adbname, now, dns_rdatatype_a);
2416 if (result == ISC_R_SUCCESS) {
2418 "dns_adb_createfind: found A for name %p in db",
2424 * Did we get a CNAME or DNAME?
2426 if (result == DNS_R_ALIAS) {
2428 "dns_adb_createfind: name %p is an alias",
2435 * If the name doesn't exist at all, don't bother with
2436 * v6 queries; they won't work.
2438 * If the name does exist but we didn't get our data, go
2439 * ahead and try AAAA.
2441 * If the result is neither of these, try a fetch for A.
2443 if (NXDOMAIN_RESULT(result))
2445 else if (NXRRSET_RESULT(result))
2448 if (!NAME_FETCH_V4(adbname))
2449 wanted_fetches |= DNS_ADBFIND_INET;
2453 if (!NAME_HAS_V6(adbname) && EXPIRE_OK(adbname->expire_v6, now)
2454 && WANT_INET6(wanted_addresses)) {
2455 result = dbfind_name(adbname, now, dns_rdatatype_aaaa);
2456 if (result == ISC_R_SUCCESS) {
2458 "dns_adb_createfind: found AAAA for name %p",
2464 * Did we get a CNAME or DNAME?
2466 if (result == DNS_R_ALIAS) {
2468 "dns_adb_createfind: name %p is an alias",
2475 * Listen to negative cache hints, and don't start
2478 if (NCACHE_RESULT(result) || AUTH_NX(result))
2481 if (!NAME_FETCH_V6(adbname))
2482 wanted_fetches |= DNS_ADBFIND_INET6;
2486 if ((WANT_INET(wanted_addresses) && NAME_HAS_V4(adbname)) ||
2487 (WANT_INET6(wanted_addresses) && NAME_HAS_V6(adbname)))
2488 have_address = ISC_TRUE;
2490 have_address = ISC_FALSE;
2491 if (wanted_fetches != 0 &&
2492 ! (FIND_AVOIDFETCHES(find) && have_address)) {
2494 * We're missing at least one address family. Either the
2495 * caller hasn't instructed us to avoid fetches, or we don't
2496 * know anything about any of the address families that would
2497 * be acceptable so we have to launch fetches.
2500 if (FIND_STARTATZONE(find))
2501 start_at_zone = ISC_TRUE;
2506 if (WANT_INET(wanted_fetches) &&
2507 fetch_name(adbname, start_at_zone,
2508 dns_rdatatype_a) == ISC_R_SUCCESS) {
2510 "dns_adb_createfind: started A fetch for name %p",
2517 if (WANT_INET6(wanted_fetches) &&
2518 fetch_name(adbname, start_at_zone,
2519 dns_rdatatype_aaaa) == ISC_R_SUCCESS) {
2521 "dns_adb_createfind: "
2522 "started AAAA fetch for name %p",
2528 * Run through the name and copy out the bits we are
2531 copy_namehook_lists(adb, find, qname, qtype, adbname, now);
2534 if (NAME_FETCH_V4(adbname))
2535 query_pending |= DNS_ADBFIND_INET;
2536 if (NAME_FETCH_V6(adbname))
2537 query_pending |= DNS_ADBFIND_INET6;
2540 * Attach to the name's query list if there are queries
2541 * already running, and we have been asked to.
2543 want_event = ISC_TRUE;
2544 if (!FIND_WANTEVENT(find))
2545 want_event = ISC_FALSE;
2546 if (FIND_WANTEMPTYEVENT(find) && FIND_HAS_ADDRS(find))
2547 want_event = ISC_FALSE;
2548 if ((wanted_addresses & query_pending) == 0)
2549 want_event = ISC_FALSE;
2551 want_event = ISC_FALSE;
2553 find->adbname = adbname;
2554 find->name_bucket = bucket;
2555 ISC_LIST_APPEND(adbname->finds, find, plink);
2556 find->query_pending = (query_pending & wanted_addresses);
2557 find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
2558 find->flags |= (find->query_pending & DNS_ADBFIND_ADDRESSMASK);
2559 DP(DEF_LEVEL, "createfind: attaching find %p to adbname %p",
2563 * Remove the flag so the caller knows there will never
2564 * be an event, and set internal flags to fake that
2565 * the event was sent and freed, so dns_adb_destroyfind() will
2566 * do the right thing.
2568 find->query_pending = (query_pending & wanted_addresses);
2569 find->options &= ~DNS_ADBFIND_WANTEVENT;
2570 find->flags |= (FIND_EVENT_SENT | FIND_EVENT_FREED);
2571 find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
2574 find->partial_result |= (adbname->partial_result & wanted_addresses);
2576 if (target != NULL) {
2577 result = dns_name_copy(&adbname->target, target, NULL);
2578 if (result != ISC_R_SUCCESS)
2581 result = DNS_R_ALIAS;
2583 result = ISC_R_SUCCESS;
2586 * Copy out error flags from the name structure into the find.
2588 find->result_v4 = find_err_map[adbname->fetch_err];
2589 find->result_v6 = find_err_map[adbname->fetch6_err];
2598 INSIST((find->flags & DNS_ADBFIND_ADDRESSMASK) != 0);
2600 isc_task_attach(task, &taskp);
2601 find->event.ev_sender = taskp;
2602 find->event.ev_action = action;
2603 find->event.ev_arg = arg;
2607 UNLOCK(&adb->namelocks[bucket]);
2613 dns_adb_destroyfind(dns_adbfind_t **findp) {
2614 dns_adbfind_t *find;
2615 dns_adbentry_t *entry;
2616 dns_adbaddrinfo_t *ai;
2620 REQUIRE(findp != NULL && DNS_ADBFIND_VALID(*findp));
2626 DP(DEF_LEVEL, "dns_adb_destroyfind on find %p", find);
2629 REQUIRE(DNS_ADB_VALID(adb));
2631 REQUIRE(FIND_EVENTFREED(find));
2633 bucket = find->name_bucket;
2634 INSIST(bucket == DNS_ADB_INVALIDBUCKET);
2636 UNLOCK(&find->lock);
2639 * The find doesn't exist on any list, and nothing is locked.
2640 * Return the find to the memory pool, and decrement the adb's
2643 ai = ISC_LIST_HEAD(find->list);
2644 while (ai != NULL) {
2645 ISC_LIST_UNLINK(find->list, ai, publink);
2648 INSIST(DNS_ADBENTRY_VALID(entry));
2649 RUNTIME_CHECK(dec_entry_refcnt(adb, entry, ISC_TRUE) ==
2651 free_adbaddrinfo(adb, &ai);
2652 ai = ISC_LIST_HEAD(find->list);
2656 * WARNING: The find is freed with the adb locked. This is done
2657 * to avoid a race condition where we free the find, some other
2658 * thread tests to see if it should be destroyed, detects it should
2659 * be, destroys it, and then we try to lock it for our check, but the
2660 * lock is destroyed.
2663 if (free_adbfind(adb, &find))
2669 dns_adb_cancelfind(dns_adbfind_t *find) {
2678 DP(DEF_LEVEL, "dns_adb_cancelfind on find %p", find);
2681 REQUIRE(DNS_ADB_VALID(adb));
2683 REQUIRE(!FIND_EVENTFREED(find));
2684 REQUIRE(FIND_WANTEVENT(find));
2686 bucket = find->name_bucket;
2687 if (bucket == DNS_ADB_INVALIDBUCKET)
2691 * We need to get the adbname's lock to unlink the find.
2693 unlock_bucket = bucket;
2694 violate_locking_hierarchy(&find->lock, &adb->namelocks[unlock_bucket]);
2695 bucket = find->name_bucket;
2696 if (bucket != DNS_ADB_INVALIDBUCKET) {
2697 ISC_LIST_UNLINK(find->adbname->finds, find, plink);
2698 find->adbname = NULL;
2699 find->name_bucket = DNS_ADB_INVALIDBUCKET;
2701 UNLOCK(&adb->namelocks[unlock_bucket]);
2702 bucket = DNS_ADB_INVALIDBUCKET;
2706 if (!FIND_EVENTSENT(find)) {
2708 task = ev->ev_sender;
2709 ev->ev_sender = find;
2710 ev->ev_type = DNS_EVENT_ADBCANCELED;
2711 ev->ev_destroy = event_free;
2712 ev->ev_destroy_arg = find;
2713 find->result_v4 = ISC_R_CANCELED;
2714 find->result_v6 = ISC_R_CANCELED;
2716 DP(DEF_LEVEL, "sending event %p to task %p for find %p",
2719 isc_task_sendanddetach(&task, (isc_event_t **)&ev);
2722 UNLOCK(&find->lock);
2726 dns_adb_dump(dns_adb_t *adb, FILE *f) {
2730 REQUIRE(DNS_ADB_VALID(adb));
2734 * Lock the adb itself, lock all the name buckets, then lock all
2735 * the entry buckets. This should put the adb into a state where
2736 * nothing can change, so we can iterate through everything and
2737 * print at our leisure.
2741 isc_stdtime_get(&now);
2743 for (i = 0; i < NBUCKETS; i++)
2744 RUNTIME_CHECK(cleanup_names(adb, i, now) == ISC_FALSE);
2745 for (i = 0; i < NBUCKETS; i++)
2746 RUNTIME_CHECK(cleanup_entries(adb, i, now) == ISC_FALSE);
2748 dump_adb(adb, f, ISC_FALSE, now);
2753 dump_ttl(FILE *f, const char *legend, isc_stdtime_t value, isc_stdtime_t now) {
2754 if (value == INT_MAX)
2756 fprintf(f, " [%s TTL %d]", legend, value - now);
2760 dump_adb(dns_adb_t *adb, FILE *f, isc_boolean_t debug, isc_stdtime_t now) {
2762 dns_adbname_t *name;
2763 dns_adbentry_t *entry;
2765 fprintf(f, ";\n; Address database dump\n;\n");
2767 fprintf(f, "; addr %p, erefcnt %u, irefcnt %u, finds out %u\n",
2768 adb, adb->erefcnt, adb->irefcnt,
2769 isc_mempool_getallocated(adb->nhmp));
2771 for (i = 0; i < NBUCKETS; i++)
2772 LOCK(&adb->namelocks[i]);
2773 for (i = 0; i < NBUCKETS; i++)
2774 LOCK(&adb->entrylocks[i]);
2779 for (i = 0; i < NBUCKETS; i++) {
2780 name = ISC_LIST_HEAD(adb->names[i]);
2784 fprintf(f, "; bucket %d\n", i);
2787 name = ISC_LIST_NEXT(name, plink))
2790 fprintf(f, "; name %p (flags %08x)\n",
2794 print_dns_name(f, &name->name);
2795 if (dns_name_countlabels(&name->target) > 0) {
2796 fprintf(f, " alias ");
2797 print_dns_name(f, &name->target);
2800 dump_ttl(f, "v4", name->expire_v4, now);
2801 dump_ttl(f, "v6", name->expire_v6, now);
2802 dump_ttl(f, "target", name->expire_target, now);
2804 fprintf(f, " [v4 %s] [v6 %s]",
2805 errnames[name->fetch_err],
2806 errnames[name->fetch6_err]);
2810 print_namehook_list(f, "v4", &name->v4, debug, now);
2811 print_namehook_list(f, "v6", &name->v6, debug, now);
2814 print_fetch_list(f, name);
2816 print_find_list(f, name);
2821 fprintf(f, ";\n; Unassociated entries\n;\n");
2823 for (i = 0; i < NBUCKETS; i++) {
2824 entry = ISC_LIST_HEAD(adb->entries[i]);
2825 while (entry != NULL) {
2826 if (entry->refcnt == 0)
2827 dump_entry(f, entry, debug, now);
2828 entry = ISC_LIST_NEXT(entry, plink);
2835 for (i = 0; i < NBUCKETS; i++)
2836 UNLOCK(&adb->entrylocks[i]);
2837 for (i = 0; i < NBUCKETS; i++)
2838 UNLOCK(&adb->namelocks[i]);
2842 dump_entry(FILE *f, dns_adbentry_t *entry, isc_boolean_t debug,
2845 char addrbuf[ISC_NETADDR_FORMATSIZE];
2846 char typebuf[DNS_RDATATYPE_FORMATSIZE];
2847 isc_netaddr_t netaddr;
2848 dns_adblameinfo_t *li;
2850 isc_netaddr_fromsockaddr(&netaddr, &entry->sockaddr);
2851 isc_netaddr_format(&netaddr, addrbuf, sizeof(addrbuf));
2854 fprintf(f, ";\t%p: refcnt %u\n", entry, entry->refcnt);
2856 fprintf(f, ";\t%s [srtt %u] [flags %08x]",
2857 addrbuf, entry->srtt, entry->flags);
2858 if (entry->expires != 0)
2859 fprintf(f, " [ttl %d]", entry->expires - now);
2861 for (li = ISC_LIST_HEAD(entry->lameinfo);
2863 li = ISC_LIST_NEXT(li, plink)) {
2864 fprintf(f, ";\t\t");
2865 print_dns_name(f, &li->qname);
2866 dns_rdatatype_format(li->qtype, typebuf, sizeof(typebuf));
2867 fprintf(f, " %s [lame TTL %d]\n", typebuf,
2868 li->lame_timer - now);
2873 dns_adb_dumpfind(dns_adbfind_t *find, FILE *f) {
2876 dns_adbaddrinfo_t *ai;
2880 * Not used currently, in the API Just In Case we
2881 * want to dump out the name and/or entries too.
2886 fprintf(f, ";Find %p\n", find);
2887 fprintf(f, ";\tqpending %08x partial %08x options %08x flags %08x\n",
2888 find->query_pending, find->partial_result,
2889 find->options, find->flags);
2890 fprintf(f, ";\tname_bucket %d, name %p, event sender %p\n",
2891 find->name_bucket, find->adbname, find->event.ev_sender);
2893 ai = ISC_LIST_HEAD(find->list);
2895 fprintf(f, "\tAddresses:\n");
2896 while (ai != NULL) {
2898 switch (sa->type.sa.sa_family) {
2900 tmpp = inet_ntop(AF_INET, &sa->type.sin.sin_addr,
2904 tmpp = inet_ntop(AF_INET6, &sa->type.sin6.sin6_addr,
2912 tmpp = "BadAddress";
2914 fprintf(f, "\t\tentry %p, flags %08x"
2915 " srtt %u addr %s\n",
2916 ai->entry, ai->flags, ai->srtt, tmpp);
2918 ai = ISC_LIST_NEXT(ai, publink);
2921 UNLOCK(&find->lock);
2925 print_dns_name(FILE *f, dns_name_t *name) {
2926 char buf[DNS_NAME_FORMATSIZE];
2930 dns_name_format(name, buf, sizeof(buf));
2931 fprintf(f, "%s", buf);
2935 print_namehook_list(FILE *f, const char *legend, dns_adbnamehooklist_t *list,
2936 isc_boolean_t debug, isc_stdtime_t now)
2938 dns_adbnamehook_t *nh;
2940 for (nh = ISC_LIST_HEAD(*list);
2942 nh = ISC_LIST_NEXT(nh, plink))
2945 fprintf(f, ";\tHook(%s) %p\n", legend, nh);
2946 dump_entry(f, nh->entry, debug, now);
2951 print_fetch(FILE *f, dns_adbfetch_t *ft, const char *type) {
2952 fprintf(f, "\t\tFetch(%s): %p -> { fetch %p }\n",
2953 type, ft, ft->fetch);
2957 print_fetch_list(FILE *f, dns_adbname_t *n) {
2958 if (NAME_FETCH_A(n))
2959 print_fetch(f, n->fetch_a, "A");
2960 if (NAME_FETCH_AAAA(n))
2961 print_fetch(f, n->fetch_aaaa, "AAAA");
2965 print_find_list(FILE *f, dns_adbname_t *name) {
2966 dns_adbfind_t *find;
2968 find = ISC_LIST_HEAD(name->finds);
2969 while (find != NULL) {
2970 dns_adb_dumpfind(find, f);
2971 find = ISC_LIST_NEXT(find, plink);
2976 dbfind_name(dns_adbname_t *adbname, isc_stdtime_t now, dns_rdatatype_t rdtype)
2978 isc_result_t result;
2979 dns_rdataset_t rdataset;
2981 dns_fixedname_t foundname;
2984 INSIST(DNS_ADBNAME_VALID(adbname));
2986 INSIST(DNS_ADB_VALID(adb));
2987 INSIST(rdtype == dns_rdatatype_a || rdtype == dns_rdatatype_aaaa);
2989 dns_fixedname_init(&foundname);
2990 fname = dns_fixedname_name(&foundname);
2991 dns_rdataset_init(&rdataset);
2993 if (rdtype == dns_rdatatype_a)
2994 adbname->fetch_err = FIND_ERR_UNEXPECTED;
2996 adbname->fetch6_err = FIND_ERR_UNEXPECTED;
2998 result = dns_view_find(adb->view, &adbname->name, rdtype, now,
2999 NAME_GLUEOK(adbname) ? DNS_DBFIND_GLUEOK : 0,
3000 ISC_TF(NAME_HINTOK(adbname)),
3001 NULL, NULL, fname, &rdataset, NULL);
3003 /* XXXVIX this switch statement is too sparse to gen a jump table. */
3009 * Found in the database. Even if we can't copy out
3010 * any information, return success, or else a fetch
3011 * will be made, which will only make things worse.
3013 if (rdtype == dns_rdatatype_a)
3014 adbname->fetch_err = FIND_ERR_SUCCESS;
3016 adbname->fetch6_err = FIND_ERR_SUCCESS;
3017 result = import_rdataset(adbname, &rdataset, now);
3019 case DNS_R_NXDOMAIN:
3022 * We're authoritative and the data doesn't exist.
3023 * Make up a negative cache entry so we don't ask again
3026 * XXXRTH What time should we use? I'm putting in 30 seconds
3029 if (rdtype == dns_rdatatype_a) {
3030 adbname->expire_v4 = now + 30;
3032 "adb name %p: Caching auth negative entry for A",
3034 if (result == DNS_R_NXDOMAIN)
3035 adbname->fetch_err = FIND_ERR_NXDOMAIN;
3037 adbname->fetch_err = FIND_ERR_NXRRSET;
3040 "adb name %p: Caching auth negative entry for AAAA",
3042 adbname->expire_v6 = now + 30;
3043 if (result == DNS_R_NXDOMAIN)
3044 adbname->fetch6_err = FIND_ERR_NXDOMAIN;
3046 adbname->fetch6_err = FIND_ERR_NXRRSET;
3049 case DNS_R_NCACHENXDOMAIN:
3050 case DNS_R_NCACHENXRRSET:
3052 * We found a negative cache entry. Pull the TTL from it
3053 * so we won't ask again for a while.
3055 rdataset.ttl = ttlclamp(rdataset.ttl);
3056 if (rdtype == dns_rdatatype_a) {
3057 adbname->expire_v4 = rdataset.ttl + now;
3058 if (result == DNS_R_NCACHENXDOMAIN)
3059 adbname->fetch_err = FIND_ERR_NXDOMAIN;
3061 adbname->fetch_err = FIND_ERR_NXRRSET;
3063 "adb name %p: Caching negative entry for A (ttl %u)",
3064 adbname, rdataset.ttl);
3067 "adb name %p: Caching negative entry for AAAA (ttl %u)",
3068 adbname, rdataset.ttl);
3069 adbname->expire_v6 = rdataset.ttl + now;
3070 if (result == DNS_R_NCACHENXDOMAIN)
3071 adbname->fetch6_err = FIND_ERR_NXDOMAIN;
3073 adbname->fetch6_err = FIND_ERR_NXRRSET;
3079 * Clear the hint and glue flags, so this will match
3082 adbname->flags &= ~(DNS_ADBFIND_GLUEOK | DNS_ADBFIND_HINTOK);
3084 rdataset.ttl = ttlclamp(rdataset.ttl);
3085 clean_target(adb, &adbname->target);
3086 adbname->expire_target = INT_MAX;
3087 result = set_target(adb, &adbname->name, fname, &rdataset,
3089 if (result == ISC_R_SUCCESS) {
3090 result = DNS_R_ALIAS;
3092 "adb name %p: caching alias target",
3094 adbname->expire_target = rdataset.ttl + now;
3096 if (rdtype == dns_rdatatype_a)
3097 adbname->fetch_err = FIND_ERR_SUCCESS;
3099 adbname->fetch6_err = FIND_ERR_SUCCESS;
3103 if (dns_rdataset_isassociated(&rdataset))
3104 dns_rdataset_disassociate(&rdataset);
3110 fetch_callback(isc_task_t *task, isc_event_t *ev) {
3111 dns_fetchevent_t *dev;
3112 dns_adbname_t *name;
3114 dns_adbfetch_t *fetch;
3116 isc_eventtype_t ev_status;
3118 isc_result_t result;
3119 unsigned int address_type;
3120 isc_boolean_t want_check_exit = ISC_FALSE;
3124 INSIST(ev->ev_type == DNS_EVENT_FETCHDONE);
3125 dev = (dns_fetchevent_t *)ev;
3127 INSIST(DNS_ADBNAME_VALID(name));
3129 INSIST(DNS_ADB_VALID(adb));
3131 bucket = name->lock_bucket;
3132 LOCK(&adb->namelocks[bucket]);
3134 INSIST(NAME_FETCH_A(name) || NAME_FETCH_AAAA(name));
3136 if (NAME_FETCH_A(name) && (name->fetch_a->fetch == dev->fetch)) {
3137 address_type = DNS_ADBFIND_INET;
3138 fetch = name->fetch_a;
3139 name->fetch_a = NULL;
3140 } else if (NAME_FETCH_AAAA(name)
3141 && (name->fetch_aaaa->fetch == dev->fetch)) {
3142 address_type = DNS_ADBFIND_INET6;
3143 fetch = name->fetch_aaaa;
3144 name->fetch_aaaa = NULL;
3148 INSIST(address_type != 0 && fetch != NULL);
3150 dns_resolver_destroyfetch(&fetch->fetch);
3153 ev_status = DNS_EVENT_ADBNOMOREADDRESSES;
3156 * Cleanup things we don't care about.
3158 if (dev->node != NULL)
3159 dns_db_detachnode(dev->db, &dev->node);
3160 if (dev->db != NULL)
3161 dns_db_detach(&dev->db);
3164 * If this name is marked as dead, clean up, throwing away
3165 * potentially good data.
3167 if (NAME_DEAD(name)) {
3168 free_adbfetch(adb, &fetch);
3169 isc_event_free(&ev);
3171 want_check_exit = kill_name(&name, DNS_EVENT_ADBCANCELED);
3173 UNLOCK(&adb->namelocks[bucket]);
3175 if (want_check_exit) {
3184 isc_stdtime_get(&now);
3187 * If we got a negative cache response, remember it.
3189 if (NCACHE_RESULT(dev->result)) {
3190 dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
3191 if (address_type == DNS_ADBFIND_INET) {
3192 DP(NCACHE_LEVEL, "adb fetch name %p: "
3193 "caching negative entry for A (ttl %u)",
3194 name, dev->rdataset->ttl);
3195 name->expire_v4 = ISC_MIN(name->expire_v4,
3196 dev->rdataset->ttl + now);
3197 if (dev->result == DNS_R_NCACHENXDOMAIN)
3198 name->fetch_err = FIND_ERR_NXDOMAIN;
3200 name->fetch_err = FIND_ERR_NXRRSET;
3201 inc_stats(adb, dns_resstatscounter_gluefetchv4fail);
3203 DP(NCACHE_LEVEL, "adb fetch name %p: "
3204 "caching negative entry for AAAA (ttl %u)",
3205 name, dev->rdataset->ttl);
3206 name->expire_v6 = ISC_MIN(name->expire_v6,
3207 dev->rdataset->ttl + now);
3208 if (dev->result == DNS_R_NCACHENXDOMAIN)
3209 name->fetch6_err = FIND_ERR_NXDOMAIN;
3211 name->fetch6_err = FIND_ERR_NXRRSET;
3212 inc_stats(adb, dns_resstatscounter_gluefetchv6fail);
3218 * Handle CNAME/DNAME.
3220 if (dev->result == DNS_R_CNAME || dev->result == DNS_R_DNAME) {
3221 dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
3222 clean_target(adb, &name->target);
3223 name->expire_target = INT_MAX;
3224 result = set_target(adb, &name->name,
3225 dns_fixedname_name(&dev->foundname),
3228 if (result == ISC_R_SUCCESS) {
3230 "adb fetch name %p: caching alias target",
3232 name->expire_target = dev->rdataset->ttl + now;
3238 * Did we get back junk? If so, and there are no more fetches
3239 * sitting out there, tell all the finds about it.
3241 if (dev->result != ISC_R_SUCCESS) {
3242 char buf[DNS_NAME_FORMATSIZE];
3244 dns_name_format(&name->name, buf, sizeof(buf));
3245 DP(DEF_LEVEL, "adb: fetch of '%s' %s failed: %s",
3246 buf, address_type == DNS_ADBFIND_INET ? "A" : "AAAA",
3247 dns_result_totext(dev->result));
3248 /* XXXMLG Don't pound on bad servers. */
3249 if (address_type == DNS_ADBFIND_INET) {
3250 name->expire_v4 = ISC_MIN(name->expire_v4, now + 300);
3251 name->fetch_err = FIND_ERR_FAILURE;
3252 inc_stats(adb, dns_resstatscounter_gluefetchv4fail);
3254 name->expire_v6 = ISC_MIN(name->expire_v6, now + 300);
3255 name->fetch6_err = FIND_ERR_FAILURE;
3256 inc_stats(adb, dns_resstatscounter_gluefetchv6fail);
3262 * We got something potentially useful.
3264 result = import_rdataset(name, &fetch->rdataset, now);
3267 if (result == ISC_R_SUCCESS) {
3268 ev_status = DNS_EVENT_ADBMOREADDRESSES;
3269 if (address_type == DNS_ADBFIND_INET)
3270 name->fetch_err = FIND_ERR_SUCCESS;
3272 name->fetch6_err = FIND_ERR_SUCCESS;
3276 free_adbfetch(adb, &fetch);
3277 isc_event_free(&ev);
3279 clean_finds_at_name(name, ev_status, address_type);
3281 UNLOCK(&adb->namelocks[bucket]);
3285 fetch_name(dns_adbname_t *adbname,
3286 isc_boolean_t start_at_zone,
3287 dns_rdatatype_t type)
3289 isc_result_t result;
3290 dns_adbfetch_t *fetch = NULL;
3292 dns_fixedname_t fixed;
3294 dns_rdataset_t rdataset;
3295 dns_rdataset_t *nameservers;
3296 unsigned int options;
3298 INSIST(DNS_ADBNAME_VALID(adbname));
3300 INSIST(DNS_ADB_VALID(adb));
3302 INSIST((type == dns_rdatatype_a && !NAME_FETCH_V4(adbname)) ||
3303 (type == dns_rdatatype_aaaa && !NAME_FETCH_V6(adbname)));
3305 adbname->fetch_err = FIND_ERR_NOTFOUND;
3309 dns_rdataset_init(&rdataset);
3311 options = DNS_FETCHOPT_NOVALIDATE;
3312 if (start_at_zone) {
3314 "fetch_name: starting at zone for name %p",
3316 dns_fixedname_init(&fixed);
3317 name = dns_fixedname_name(&fixed);
3318 result = dns_view_findzonecut2(adb->view, &adbname->name, name,
3319 0, 0, ISC_TRUE, ISC_FALSE,
3321 if (result != ISC_R_SUCCESS && result != DNS_R_HINT)
3323 nameservers = &rdataset;
3324 options |= DNS_FETCHOPT_UNSHARED;
3327 fetch = new_adbfetch(adb);
3328 if (fetch == NULL) {
3329 result = ISC_R_NOMEMORY;
3333 result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
3334 type, name, nameservers, NULL,
3335 options, adb->task, fetch_callback,
3336 adbname, &fetch->rdataset, NULL,
3338 if (result != ISC_R_SUCCESS)
3341 if (type == dns_rdatatype_a) {
3342 adbname->fetch_a = fetch;
3343 inc_stats(adb, dns_resstatscounter_gluefetchv4);
3345 adbname->fetch_aaaa = fetch;
3346 inc_stats(adb, dns_resstatscounter_gluefetchv6);
3348 fetch = NULL; /* Keep us from cleaning this up below. */
3352 free_adbfetch(adb, &fetch);
3353 if (dns_rdataset_isassociated(&rdataset))
3354 dns_rdataset_disassociate(&rdataset);
3360 * XXXMLG Needs to take a find argument and an address info, no zone or adb,
3361 * since these can be extracted from the find itself.
3364 dns_adb_marklame(dns_adb_t *adb, dns_adbaddrinfo_t *addr, dns_name_t *qname,
3365 dns_rdatatype_t qtype, isc_stdtime_t expire_time)
3367 dns_adblameinfo_t *li;
3369 isc_result_t result = ISC_R_SUCCESS;
3371 REQUIRE(DNS_ADB_VALID(adb));
3372 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3373 REQUIRE(qname != NULL);
3375 bucket = addr->entry->lock_bucket;
3376 LOCK(&adb->entrylocks[bucket]);
3377 li = ISC_LIST_HEAD(addr->entry->lameinfo);
3378 while (li != NULL &&
3379 (li->qtype != qtype || !dns_name_equal(qname, &li->qname)))
3380 li = ISC_LIST_NEXT(li, plink);
3382 if (expire_time > li->lame_timer)
3383 li->lame_timer = expire_time;
3386 li = new_adblameinfo(adb, qname, qtype);
3388 result = ISC_R_NOMEMORY;
3392 li->lame_timer = expire_time;
3394 ISC_LIST_PREPEND(addr->entry->lameinfo, li, plink);
3396 UNLOCK(&adb->entrylocks[bucket]);
3402 dns_adb_adjustsrtt(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
3403 unsigned int rtt, unsigned int factor)
3406 unsigned int new_srtt;
3409 REQUIRE(DNS_ADB_VALID(adb));
3410 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3411 REQUIRE(factor <= 10);
3413 bucket = addr->entry->lock_bucket;
3414 LOCK(&adb->entrylocks[bucket]);
3416 if (factor == DNS_ADB_RTTADJAGE)
3417 new_srtt = addr->entry->srtt * 98 / 100;
3419 new_srtt = (addr->entry->srtt / 10 * factor)
3420 + (rtt / 10 * (10 - factor));
3422 addr->entry->srtt = new_srtt;
3423 addr->srtt = new_srtt;
3425 isc_stdtime_get(&now);
3426 addr->entry->expires = now + ADB_ENTRY_WINDOW;
3428 UNLOCK(&adb->entrylocks[bucket]);
3432 dns_adb_changeflags(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
3433 unsigned int bits, unsigned int mask)
3437 REQUIRE(DNS_ADB_VALID(adb));
3438 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3440 bucket = addr->entry->lock_bucket;
3441 LOCK(&adb->entrylocks[bucket]);
3443 addr->entry->flags = (addr->entry->flags & ~mask) | (bits & mask);
3445 * Note that we do not update the other bits in addr->flags with
3446 * the most recent values from addr->entry->flags.
3448 addr->flags = (addr->flags & ~mask) | (bits & mask);
3450 UNLOCK(&adb->entrylocks[bucket]);
3454 dns_adb_findaddrinfo(dns_adb_t *adb, isc_sockaddr_t *sa,
3455 dns_adbaddrinfo_t **addrp, isc_stdtime_t now)
3458 dns_adbentry_t *entry;
3459 dns_adbaddrinfo_t *addr;
3460 isc_result_t result;
3463 REQUIRE(DNS_ADB_VALID(adb));
3464 REQUIRE(addrp != NULL && *addrp == NULL);
3468 result = ISC_R_SUCCESS;
3469 bucket = DNS_ADB_INVALIDBUCKET;
3470 entry = find_entry_and_lock(adb, sa, &bucket, now);
3471 if (adb->entry_sd[bucket]) {
3472 result = ISC_R_SHUTTINGDOWN;
3475 if (entry == NULL) {
3477 * We don't know anything about this address.
3479 entry = new_adbentry(adb);
3480 if (entry == NULL) {
3481 result = ISC_R_NOMEMORY;
3484 entry->sockaddr = *sa;
3485 link_entry(adb, bucket, entry);
3486 DP(ENTER_LEVEL, "findaddrinfo: new entry %p", entry);
3488 DP(ENTER_LEVEL, "findaddrinfo: found entry %p", entry);
3490 port = isc_sockaddr_getport(sa);
3491 addr = new_adbaddrinfo(adb, entry, port);
3493 result = ISC_R_NOMEMORY;
3495 inc_entry_refcnt(adb, entry, ISC_FALSE);
3500 UNLOCK(&adb->entrylocks[bucket]);
3506 dns_adb_freeaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **addrp) {
3507 dns_adbaddrinfo_t *addr;
3508 dns_adbentry_t *entry;
3511 isc_boolean_t want_check_exit = ISC_FALSE;
3513 REQUIRE(DNS_ADB_VALID(adb));
3514 REQUIRE(addrp != NULL);
3516 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3517 entry = addr->entry;
3518 REQUIRE(DNS_ADBENTRY_VALID(entry));
3520 isc_stdtime_get(&now);
3524 bucket = addr->entry->lock_bucket;
3525 LOCK(&adb->entrylocks[bucket]);
3527 entry->expires = now + ADB_ENTRY_WINDOW;
3529 want_check_exit = dec_entry_refcnt(adb, entry, ISC_FALSE);
3531 UNLOCK(&adb->entrylocks[bucket]);
3534 free_adbaddrinfo(adb, &addr);
3536 if (want_check_exit) {
3544 dns_adb_flush(dns_adb_t *adb) {
3547 INSIST(DNS_ADB_VALID(adb));
3552 * Call our cleanup routines.
3554 for (i = 0; i < NBUCKETS; i++)
3555 RUNTIME_CHECK(cleanup_names(adb, i, INT_MAX) == ISC_FALSE);
3556 for (i = 0; i < NBUCKETS; i++)
3557 RUNTIME_CHECK(cleanup_entries(adb, i, INT_MAX) == ISC_FALSE);
3559 #ifdef DUMP_ADB_AFTER_CLEANING
3560 dump_adb(adb, stdout, ISC_TRUE, INT_MAX);
3567 dns_adb_flushname(dns_adb_t *adb, dns_name_t *name) {
3568 dns_adbname_t *adbname;
3569 dns_adbname_t *nextname;
3572 INSIST(DNS_ADB_VALID(adb));
3575 bucket = dns_name_hash(name, ISC_FALSE) % NBUCKETS;
3576 LOCK(&adb->namelocks[bucket]);
3577 adbname = ISC_LIST_HEAD(adb->names[bucket]);
3578 while (adbname != NULL) {
3579 nextname = ISC_LIST_NEXT(adbname, plink);
3580 if (!NAME_DEAD(adbname) &&
3581 dns_name_equal(name, &adbname->name)) {
3582 RUNTIME_CHECK(kill_name(&adbname,
3583 DNS_EVENT_ADBCANCELED) ==
3588 UNLOCK(&adb->namelocks[bucket]);
3593 water(void *arg, int mark) {
3594 dns_adb_t *adb = arg;
3595 isc_boolean_t overmem = ISC_TF(mark == ISC_MEM_HIWATER);
3597 REQUIRE(DNS_ADB_VALID(adb));
3599 DP(ISC_LOG_DEBUG(1),
3600 "adb reached %s water mark", overmem ? "high" : "low");
3603 * We can't use adb->lock as there is potential for water
3604 * to be called when adb->lock is held.
3606 LOCK(&adb->overmemlock);
3607 if (adb->overmem != overmem) {
3608 adb->overmem = overmem;
3609 isc_mem_waterack(adb->mctx, mark);
3611 UNLOCK(&adb->overmemlock);
3615 dns_adb_setadbsize(dns_adb_t *adb, isc_uint32_t size) {
3616 isc_uint32_t hiwater;
3617 isc_uint32_t lowater;
3619 INSIST(DNS_ADB_VALID(adb));
3621 if (size != 0 && size < DNS_ADB_MINADBSIZE)
3622 size = DNS_ADB_MINADBSIZE;
3624 hiwater = size - (size >> 3); /* Approximately 7/8ths. */
3625 lowater = size - (size >> 2); /* Approximately 3/4ths. */
3627 if (size == 0 || hiwater == 0 || lowater == 0)
3628 isc_mem_setwater(adb->mctx, water, adb, 0, 0);
3630 isc_mem_setwater(adb->mctx, water, adb, hiwater, lowater);