2 * Copyright (C) 2004-2012 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.
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;
122 isc_interval_t tick_interval;
123 int next_cleanbucket;
125 unsigned int irefcnt;
126 unsigned int erefcnt;
129 isc_mempool_t *nmp; /*%< dns_adbname_t */
130 isc_mempool_t *nhmp; /*%< dns_adbnamehook_t */
131 isc_mempool_t *limp; /*%< dns_adblameinfo_t */
132 isc_mempool_t *emp; /*%< dns_adbentry_t */
133 isc_mempool_t *ahmp; /*%< dns_adbfind_t */
134 isc_mempool_t *aimp; /*%< dns_adbaddrinfo_t */
135 isc_mempool_t *afmp; /*%< dns_adbfetch_t */
138 * Bucketized locks and lists for names.
140 * XXXRTH Have a per-bucket structure that contains all of these?
142 dns_adbnamelist_t names[NBUCKETS];
143 dns_adbnamelist_t deadnames[NBUCKETS];
144 /*% See dns_adbnamelist_t */
145 isc_mutex_t namelocks[NBUCKETS];
146 /*% See dns_adbnamelist_t */
147 isc_boolean_t name_sd[NBUCKETS];
148 /*% See dns_adbnamelist_t */
149 unsigned int name_refcnt[NBUCKETS];
152 * Bucketized locks for entries.
154 * XXXRTH Have a per-bucket structure that contains all of these?
156 dns_adbentrylist_t entries[NBUCKETS];
157 dns_adbentrylist_t deadentries[NBUCKETS];
158 isc_mutex_t entrylocks[NBUCKETS];
159 isc_boolean_t entry_sd[NBUCKETS]; /*%< shutting down */
160 unsigned int entry_refcnt[NBUCKETS];
163 isc_boolean_t cevent_sent;
164 isc_boolean_t shutting_down;
165 isc_eventlist_t whenshutdown;
169 * XXXMLG Document these structures.
172 /*% dns_adbname structure */
177 unsigned int partial_result;
181 isc_stdtime_t expire_target;
182 isc_stdtime_t expire_v4;
183 isc_stdtime_t expire_v6;
185 dns_adbnamehooklist_t v4;
186 dns_adbnamehooklist_t v6;
187 dns_adbfetch_t *fetch_a;
188 dns_adbfetch_t *fetch_aaaa;
189 unsigned int fetch_err;
190 unsigned int fetch6_err;
191 dns_adbfindlist_t finds;
192 /* for LRU-based management */
193 isc_stdtime_t last_used;
195 ISC_LINK(dns_adbname_t) plink;
198 /*% The adbfetch structure */
199 struct dns_adbfetch {
202 dns_rdataset_t rdataset;
206 * This is a small widget that dangles off a dns_adbname_t. It contains a
207 * pointer to the address information about this host, and a link to the next
208 * namehook that will contain the next address this host has.
210 struct dns_adbnamehook {
212 dns_adbentry_t *entry;
213 ISC_LINK(dns_adbnamehook_t) plink;
217 * This is a small widget that holds qname-specific information about an
218 * address. Currently limited to lameness, but could just as easily be
219 * extended to other types of information about zones.
221 struct dns_adblameinfo {
225 dns_rdatatype_t qtype;
226 isc_stdtime_t lame_timer;
228 ISC_LINK(dns_adblameinfo_t) plink;
232 * An address entry. It holds quite a bit of information about addresses,
233 * including edns state (in "flags"), rtt, and of course the address of
236 struct dns_adbentry {
244 isc_sockaddr_t sockaddr;
246 isc_stdtime_t expires;
248 * A nonzero 'expires' field indicates that the entry should
249 * persist until that time. This allows entries found
250 * using dns_adb_findaddrinfo() to persist for a limited time
251 * even though they are not necessarily associated with a
255 ISC_LIST(dns_adblameinfo_t) lameinfo;
256 ISC_LINK(dns_adbentry_t) plink;
260 * Internal functions (and prototypes).
262 static inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
263 static inline void free_adbname(dns_adb_t *, dns_adbname_t **);
264 static inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
266 static inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
267 static inline dns_adblameinfo_t *new_adblameinfo(dns_adb_t *, dns_name_t *,
269 static inline void free_adblameinfo(dns_adb_t *, dns_adblameinfo_t **);
270 static inline dns_adbentry_t *new_adbentry(dns_adb_t *);
271 static inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
272 static inline dns_adbfind_t *new_adbfind(dns_adb_t *);
273 static inline isc_boolean_t free_adbfind(dns_adb_t *, dns_adbfind_t **);
274 static inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *, dns_adbentry_t *,
276 static inline dns_adbfetch_t *new_adbfetch(dns_adb_t *);
277 static inline void free_adbfetch(dns_adb_t *, dns_adbfetch_t **);
278 static inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
279 unsigned int, int *);
280 static inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
281 isc_sockaddr_t *, int *,
283 static void dump_adb(dns_adb_t *, FILE *, isc_boolean_t debug, isc_stdtime_t);
284 static void print_dns_name(FILE *, dns_name_t *);
285 static void print_namehook_list(FILE *, const char *legend,
286 dns_adbnamehooklist_t *list,
289 static void print_find_list(FILE *, dns_adbname_t *);
290 static void print_fetch_list(FILE *, dns_adbname_t *);
291 static inline isc_boolean_t dec_adb_irefcnt(dns_adb_t *);
292 static inline void inc_adb_irefcnt(dns_adb_t *);
293 static inline void inc_adb_erefcnt(dns_adb_t *);
294 static inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
296 static inline isc_boolean_t dec_entry_refcnt(dns_adb_t *, isc_boolean_t,
297 dns_adbentry_t *, isc_boolean_t);
298 static inline void violate_locking_hierarchy(isc_mutex_t *, isc_mutex_t *);
299 static isc_boolean_t clean_namehooks(dns_adb_t *, dns_adbnamehooklist_t *);
300 static void clean_target(dns_adb_t *, dns_name_t *);
301 static void clean_finds_at_name(dns_adbname_t *, isc_eventtype_t,
303 static isc_boolean_t check_expire_namehooks(dns_adbname_t *, isc_stdtime_t);
304 static isc_boolean_t check_expire_entry(dns_adb_t *, dns_adbentry_t **,
306 static void cancel_fetches_at_name(dns_adbname_t *);
307 static isc_result_t dbfind_name(dns_adbname_t *, isc_stdtime_t,
309 static isc_result_t fetch_name(dns_adbname_t *, isc_boolean_t,
311 static inline void check_exit(dns_adb_t *);
312 static void destroy(dns_adb_t *);
313 static isc_boolean_t shutdown_names(dns_adb_t *);
314 static isc_boolean_t shutdown_entries(dns_adb_t *);
315 static inline void link_name(dns_adb_t *, int, dns_adbname_t *);
316 static inline isc_boolean_t unlink_name(dns_adb_t *, dns_adbname_t *);
317 static inline void link_entry(dns_adb_t *, int, dns_adbentry_t *);
318 static inline isc_boolean_t unlink_entry(dns_adb_t *, dns_adbentry_t *);
319 static isc_boolean_t kill_name(dns_adbname_t **, isc_eventtype_t);
320 static void water(void *, int);
321 static void dump_entry(FILE *, dns_adbentry_t *, isc_boolean_t, isc_stdtime_t);
324 * MUST NOT overlap DNS_ADBFIND_* flags!
326 #define FIND_EVENT_SENT 0x40000000
327 #define FIND_EVENT_FREED 0x80000000
328 #define FIND_EVENTSENT(h) (((h)->flags & FIND_EVENT_SENT) != 0)
329 #define FIND_EVENTFREED(h) (((h)->flags & FIND_EVENT_FREED) != 0)
331 #define NAME_NEEDS_POKE 0x80000000
332 #define NAME_IS_DEAD 0x40000000
333 #define NAME_HINT_OK DNS_ADBFIND_HINTOK
334 #define NAME_GLUE_OK DNS_ADBFIND_GLUEOK
335 #define NAME_STARTATZONE DNS_ADBFIND_STARTATZONE
336 #define NAME_DEAD(n) (((n)->flags & NAME_IS_DEAD) != 0)
337 #define NAME_NEEDSPOKE(n) (((n)->flags & NAME_NEEDS_POKE) != 0)
338 #define NAME_GLUEOK(n) (((n)->flags & NAME_GLUE_OK) != 0)
339 #define NAME_HINTOK(n) (((n)->flags & NAME_HINT_OK) != 0)
342 * Private flag(s) for entries.
343 * MUST NOT overlap FCTX_ADDRINFO_xxx and DNS_FETCHOPT_NOEDNS0.
345 #define ENTRY_IS_DEAD 0x80000000
348 * To the name, address classes are all that really exist. If it has a
349 * V6 address it doesn't care if it came from a AAAA query.
351 #define NAME_HAS_V4(n) (!ISC_LIST_EMPTY((n)->v4))
352 #define NAME_HAS_V6(n) (!ISC_LIST_EMPTY((n)->v6))
353 #define NAME_HAS_ADDRS(n) (NAME_HAS_V4(n) || NAME_HAS_V6(n))
356 * Fetches are broken out into A and AAAA types. In some cases,
357 * however, it makes more sense to test for a particular class of fetches,
358 * like V4 or V6 above.
359 * Note: since we have removed the support of A6 in adb, FETCH_A and FETCH_AAAA
360 * are now equal to FETCH_V4 and FETCH_V6, respectively.
362 #define NAME_FETCH_A(n) ((n)->fetch_a != NULL)
363 #define NAME_FETCH_AAAA(n) ((n)->fetch_aaaa != NULL)
364 #define NAME_FETCH_V4(n) (NAME_FETCH_A(n))
365 #define NAME_FETCH_V6(n) (NAME_FETCH_AAAA(n))
366 #define NAME_FETCH(n) (NAME_FETCH_V4(n) || NAME_FETCH_V6(n))
369 * Find options and tests to see if there are addresses on the list.
371 #define FIND_WANTEVENT(fn) (((fn)->options & DNS_ADBFIND_WANTEVENT) != 0)
372 #define FIND_WANTEMPTYEVENT(fn) (((fn)->options & DNS_ADBFIND_EMPTYEVENT) != 0)
373 #define FIND_AVOIDFETCHES(fn) (((fn)->options & DNS_ADBFIND_AVOIDFETCHES) \
375 #define FIND_STARTATZONE(fn) (((fn)->options & DNS_ADBFIND_STARTATZONE) \
377 #define FIND_HINTOK(fn) (((fn)->options & DNS_ADBFIND_HINTOK) != 0)
378 #define FIND_GLUEOK(fn) (((fn)->options & DNS_ADBFIND_GLUEOK) != 0)
379 #define FIND_HAS_ADDRS(fn) (!ISC_LIST_EMPTY((fn)->list))
380 #define FIND_RETURNLAME(fn) (((fn)->options & DNS_ADBFIND_RETURNLAME) != 0)
383 * These are currently used on simple unsigned ints, so they are
384 * not really associated with any particular type.
386 #define WANT_INET(x) (((x) & DNS_ADBFIND_INET) != 0)
387 #define WANT_INET6(x) (((x) & DNS_ADBFIND_INET6) != 0)
389 #define EXPIRE_OK(exp, now) ((exp == INT_MAX) || (exp < now))
392 * Find out if the flags on a name (nf) indicate if it is a hint or
393 * glue, and compare this to the appropriate bits set in o, to see if
396 #define GLUE_OK(nf, o) (!NAME_GLUEOK(nf) || (((o) & DNS_ADBFIND_GLUEOK) != 0))
397 #define HINT_OK(nf, o) (!NAME_HINTOK(nf) || (((o) & DNS_ADBFIND_HINTOK) != 0))
398 #define GLUEHINT_OK(nf, o) (GLUE_OK(nf, o) || HINT_OK(nf, o))
399 #define STARTATZONE_MATCHES(nf, o) (((nf)->flags & NAME_STARTATZONE) == \
400 ((o) & DNS_ADBFIND_STARTATZONE))
402 #define ENTER_LEVEL ISC_LOG_DEBUG(50)
403 #define EXIT_LEVEL ENTER_LEVEL
404 #define CLEAN_LEVEL ISC_LOG_DEBUG(100)
405 #define DEF_LEVEL ISC_LOG_DEBUG(5)
406 #define NCACHE_LEVEL ISC_LOG_DEBUG(20)
408 #define NCACHE_RESULT(r) ((r) == DNS_R_NCACHENXDOMAIN || \
409 (r) == DNS_R_NCACHENXRRSET)
410 #define AUTH_NX(r) ((r) == DNS_R_NXDOMAIN || \
411 (r) == DNS_R_NXRRSET)
412 #define NXDOMAIN_RESULT(r) ((r) == DNS_R_NXDOMAIN || \
413 (r) == DNS_R_NCACHENXDOMAIN)
414 #define NXRRSET_RESULT(r) ((r) == DNS_R_NCACHENXRRSET || \
415 (r) == DNS_R_NXRRSET || \
416 (r) == DNS_R_HINTNXRRSET)
419 * Error state rankings.
422 #define FIND_ERR_SUCCESS 0 /* highest rank */
423 #define FIND_ERR_CANCELED 1
424 #define FIND_ERR_FAILURE 2
425 #define FIND_ERR_NXDOMAIN 3
426 #define FIND_ERR_NXRRSET 4
427 #define FIND_ERR_UNEXPECTED 5
428 #define FIND_ERR_NOTFOUND 6
429 #define FIND_ERR_MAX 7
431 static const char *errnames[] = {
441 #define NEWERR(old, new) (ISC_MIN((old), (new)))
443 static isc_result_t find_err_map[FIND_ERR_MAX] = {
450 ISC_R_NOTFOUND /* not YET found */
454 DP(int level, const char *format, ...) ISC_FORMAT_PRINTF(2, 3);
457 DP(int level, const char *format, ...) {
460 va_start(args, format);
461 isc_log_vwrite(dns_lctx,
462 DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ADB,
463 level, format, args);
468 * Increment resolver-related statistics counters.
471 inc_stats(dns_adb_t *adb, isc_statscounter_t counter) {
472 if (adb->view->resstats != NULL)
473 isc_stats_increment(adb->view->resstats, counter);
476 static inline dns_ttl_t
477 ttlclamp(dns_ttl_t ttl) {
478 if (ttl < ADB_CACHE_MINIMUM)
479 ttl = ADB_CACHE_MINIMUM;
480 if (ttl > ADB_CACHE_MAXIMUM)
481 ttl = ADB_CACHE_MAXIMUM;
487 * Requires the adbname bucket be locked and that no entry buckets be locked.
489 * This code handles A and AAAA rdatasets only.
492 import_rdataset(dns_adbname_t *adbname, dns_rdataset_t *rdataset,
497 dns_adbnamehook_t *nh;
498 dns_adbnamehook_t *anh;
499 dns_rdata_t rdata = DNS_RDATA_INIT;
501 struct in6_addr in6a;
502 isc_sockaddr_t sockaddr;
503 dns_adbentry_t *foundentry; /* NO CLEAN UP! */
505 isc_boolean_t new_addresses_added;
506 dns_rdatatype_t rdtype;
507 unsigned int findoptions;
508 dns_adbnamehooklist_t *hookhead;
510 INSIST(DNS_ADBNAME_VALID(adbname));
512 INSIST(DNS_ADB_VALID(adb));
514 rdtype = rdataset->type;
515 INSIST((rdtype == dns_rdatatype_a) || (rdtype == dns_rdatatype_aaaa));
516 if (rdtype == dns_rdatatype_a)
517 findoptions = DNS_ADBFIND_INET;
519 findoptions = DNS_ADBFIND_INET6;
521 addr_bucket = DNS_ADB_INVALIDBUCKET;
522 new_addresses_added = ISC_FALSE;
525 result = dns_rdataset_first(rdataset);
526 while (result == ISC_R_SUCCESS) {
527 dns_rdata_reset(&rdata);
528 dns_rdataset_current(rdataset, &rdata);
529 if (rdtype == dns_rdatatype_a) {
530 INSIST(rdata.length == 4);
531 memcpy(&ina.s_addr, rdata.data, 4);
532 isc_sockaddr_fromin(&sockaddr, &ina, 0);
533 hookhead = &adbname->v4;
535 INSIST(rdata.length == 16);
536 memcpy(in6a.s6_addr, rdata.data, 16);
537 isc_sockaddr_fromin6(&sockaddr, &in6a, 0);
538 hookhead = &adbname->v6;
542 nh = new_adbnamehook(adb, NULL);
544 adbname->partial_result |= findoptions;
545 result = ISC_R_NOMEMORY;
549 foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket,
551 if (foundentry == NULL) {
552 dns_adbentry_t *entry;
554 entry = new_adbentry(adb);
556 adbname->partial_result |= findoptions;
557 result = ISC_R_NOMEMORY;
561 entry->sockaddr = sockaddr;
566 link_entry(adb, addr_bucket, entry);
568 for (anh = ISC_LIST_HEAD(*hookhead);
570 anh = ISC_LIST_NEXT(anh, plink))
571 if (anh->entry == foundentry)
574 foundentry->refcnt++;
575 nh->entry = foundentry;
577 free_adbnamehook(adb, &nh);
580 new_addresses_added = ISC_TRUE;
582 ISC_LIST_APPEND(*hookhead, nh, plink);
584 result = dns_rdataset_next(rdataset);
589 free_adbnamehook(adb, &nh);
591 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
592 UNLOCK(&adb->entrylocks[addr_bucket]);
594 if (rdataset->trust == dns_trust_glue ||
595 rdataset->trust == dns_trust_additional)
596 rdataset->ttl = ADB_CACHE_MINIMUM;
597 else if (rdataset->trust == dns_trust_ultimate)
600 rdataset->ttl = ttlclamp(rdataset->ttl);
602 if (rdtype == dns_rdatatype_a) {
603 DP(NCACHE_LEVEL, "expire_v4 set to MIN(%u,%u) import_rdataset",
604 adbname->expire_v4, now + rdataset->ttl);
605 adbname->expire_v4 = ISC_MIN(adbname->expire_v4,
606 now + rdataset->ttl);
608 DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) import_rdataset",
609 adbname->expire_v6, now + rdataset->ttl);
610 adbname->expire_v6 = ISC_MIN(adbname->expire_v6,
611 now + rdataset->ttl);
614 if (new_addresses_added) {
616 * Lie a little here. This is more or less so code that cares
617 * can find out if any new information was added or not.
619 return (ISC_R_SUCCESS);
626 * Requires the name's bucket be locked.
629 kill_name(dns_adbname_t **n, isc_eventtype_t ev) {
631 isc_boolean_t result = ISC_FALSE;
632 isc_boolean_t result4, result6;
639 INSIST(DNS_ADBNAME_VALID(name));
641 INSIST(DNS_ADB_VALID(adb));
643 DP(DEF_LEVEL, "killing name %p", name);
646 * If we're dead already, just check to see if we should go
649 if (NAME_DEAD(name) && !NAME_FETCH(name)) {
650 result = unlink_name(adb, name);
651 free_adbname(adb, &name);
653 result = dec_adb_irefcnt(adb);
658 * Clean up the name's various lists. These two are destructive
659 * in that they will always empty the list.
661 clean_finds_at_name(name, ev, DNS_ADBFIND_ADDRESSMASK);
662 result4 = clean_namehooks(adb, &name->v4);
663 result6 = clean_namehooks(adb, &name->v6);
664 clean_target(adb, &name->target);
665 result = ISC_TF(result4 || result6);
668 * If fetches are running, cancel them. If none are running, we can
669 * just kill the name here.
671 if (!NAME_FETCH(name)) {
672 INSIST(result == ISC_FALSE);
673 result = unlink_name(adb, name);
674 free_adbname(adb, &name);
676 result = dec_adb_irefcnt(adb);
678 cancel_fetches_at_name(name);
679 if (!NAME_DEAD(name)) {
680 bucket = name->lock_bucket;
681 ISC_LIST_UNLINK(adb->names[bucket], name, plink);
682 ISC_LIST_APPEND(adb->deadnames[bucket], name, plink);
683 name->flags |= NAME_IS_DEAD;
690 * Requires the name's bucket be locked and no entry buckets be locked.
693 check_expire_namehooks(dns_adbname_t *name, isc_stdtime_t now) {
695 isc_boolean_t result4 = ISC_FALSE;
696 isc_boolean_t result6 = ISC_FALSE;
698 INSIST(DNS_ADBNAME_VALID(name));
700 INSIST(DNS_ADB_VALID(adb));
703 * Check to see if we need to remove the v4 addresses
705 if (!NAME_FETCH_V4(name) && EXPIRE_OK(name->expire_v4, now)) {
706 if (NAME_HAS_V4(name)) {
707 DP(DEF_LEVEL, "expiring v4 for name %p", name);
708 result4 = clean_namehooks(adb, &name->v4);
709 name->partial_result &= ~DNS_ADBFIND_INET;
711 name->expire_v4 = INT_MAX;
712 name->fetch_err = FIND_ERR_UNEXPECTED;
716 * Check to see if we need to remove the v6 addresses
718 if (!NAME_FETCH_V6(name) && EXPIRE_OK(name->expire_v6, now)) {
719 if (NAME_HAS_V6(name)) {
720 DP(DEF_LEVEL, "expiring v6 for name %p", name);
721 result6 = clean_namehooks(adb, &name->v6);
722 name->partial_result &= ~DNS_ADBFIND_INET6;
724 name->expire_v6 = INT_MAX;
725 name->fetch6_err = FIND_ERR_UNEXPECTED;
729 * Check to see if we need to remove the alias target.
731 if (EXPIRE_OK(name->expire_target, now)) {
732 clean_target(adb, &name->target);
733 name->expire_target = INT_MAX;
735 return (ISC_TF(result4 || result6));
739 * Requires the name's bucket be locked.
742 link_name(dns_adb_t *adb, int bucket, dns_adbname_t *name) {
743 INSIST(name->lock_bucket == DNS_ADB_INVALIDBUCKET);
745 ISC_LIST_PREPEND(adb->names[bucket], name, plink);
746 name->lock_bucket = bucket;
747 adb->name_refcnt[bucket]++;
751 * Requires the name's bucket be locked.
753 static inline isc_boolean_t
754 unlink_name(dns_adb_t *adb, dns_adbname_t *name) {
756 isc_boolean_t result = ISC_FALSE;
758 bucket = name->lock_bucket;
759 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
762 ISC_LIST_UNLINK(adb->deadnames[bucket], name, plink);
764 ISC_LIST_UNLINK(adb->names[bucket], name, plink);
765 name->lock_bucket = DNS_ADB_INVALIDBUCKET;
766 INSIST(adb->name_refcnt[bucket] > 0);
767 adb->name_refcnt[bucket]--;
768 if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
774 * Requires the entry's bucket be locked.
777 link_entry(dns_adb_t *adb, int bucket, dns_adbentry_t *entry) {
781 if (isc_mem_isovermem(adb->mctx)) {
782 for (i = 0; i < 2; i++) {
783 e = ISC_LIST_TAIL(adb->entries[bucket]);
786 if (e->refcnt == 0) {
787 unlink_entry(adb, e);
788 free_adbentry(adb, &e);
791 INSIST((e->flags & ENTRY_IS_DEAD) == 0);
792 e->flags |= ENTRY_IS_DEAD;
793 ISC_LIST_UNLINK(adb->entries[bucket], e, plink);
794 ISC_LIST_PREPEND(adb->deadentries[bucket], e, plink);
798 ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
799 entry->lock_bucket = bucket;
800 adb->entry_refcnt[bucket]++;
804 * Requires the entry's bucket be locked.
806 static inline isc_boolean_t
807 unlink_entry(dns_adb_t *adb, dns_adbentry_t *entry) {
809 isc_boolean_t result = ISC_FALSE;
811 bucket = entry->lock_bucket;
812 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
814 if ((entry->flags & ENTRY_IS_DEAD) != 0)
815 ISC_LIST_UNLINK(adb->deadentries[bucket], entry, plink);
817 ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
818 entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
819 INSIST(adb->entry_refcnt[bucket] > 0);
820 adb->entry_refcnt[bucket]--;
821 if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
827 violate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want) {
828 if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
836 * The ADB _MUST_ be locked before calling. Also, exit conditions must be
837 * checked after calling this function.
840 shutdown_names(dns_adb_t *adb) {
842 isc_boolean_t result = ISC_FALSE;
844 dns_adbname_t *next_name;
846 for (bucket = 0; bucket < NBUCKETS; bucket++) {
847 LOCK(&adb->namelocks[bucket]);
848 adb->name_sd[bucket] = ISC_TRUE;
850 name = ISC_LIST_HEAD(adb->names[bucket]);
853 * This bucket has no names. We must decrement the
854 * irefcnt ourselves, since it will not be
855 * automatically triggered by a name being unlinked.
857 INSIST(result == ISC_FALSE);
858 result = dec_adb_irefcnt(adb);
861 * Run through the list. For each name, clean up finds
862 * found there, and cancel any fetches running. When
863 * all the fetches are canceled, the name will destroy
866 while (name != NULL) {
867 next_name = ISC_LIST_NEXT(name, plink);
868 INSIST(result == ISC_FALSE);
869 result = kill_name(&name,
870 DNS_EVENT_ADBSHUTDOWN);
875 UNLOCK(&adb->namelocks[bucket]);
881 * The ADB _MUST_ be locked before calling. Also, exit conditions must be
882 * checked after calling this function.
885 shutdown_entries(dns_adb_t *adb) {
887 isc_boolean_t result = ISC_FALSE;
888 dns_adbentry_t *entry;
889 dns_adbentry_t *next_entry;
891 for (bucket = 0; bucket < NBUCKETS; bucket++) {
892 LOCK(&adb->entrylocks[bucket]);
893 adb->entry_sd[bucket] = ISC_TRUE;
895 entry = ISC_LIST_HEAD(adb->entries[bucket]);
896 if (adb->entry_refcnt[bucket] == 0) {
898 * This bucket has no entries. We must decrement the
899 * irefcnt ourselves, since it will not be
900 * automatically triggered by an entry being unlinked.
902 result = dec_adb_irefcnt(adb);
905 * Run through the list. Cleanup any entries not
906 * associated with names, and which are not in use.
908 while (entry != NULL) {
909 next_entry = ISC_LIST_NEXT(entry, plink);
910 if (entry->refcnt == 0 &&
911 entry->expires != 0) {
912 result = unlink_entry(adb, entry);
913 free_adbentry(adb, &entry);
915 result = dec_adb_irefcnt(adb);
921 UNLOCK(&adb->entrylocks[bucket]);
927 * Name bucket must be locked
930 cancel_fetches_at_name(dns_adbname_t *name) {
931 if (NAME_FETCH_A(name))
932 dns_resolver_cancelfetch(name->fetch_a->fetch);
934 if (NAME_FETCH_AAAA(name))
935 dns_resolver_cancelfetch(name->fetch_aaaa->fetch);
939 * Assumes the name bucket is locked.
942 clean_namehooks(dns_adb_t *adb, dns_adbnamehooklist_t *namehooks) {
943 dns_adbentry_t *entry;
944 dns_adbnamehook_t *namehook;
946 isc_boolean_t result = ISC_FALSE;
947 isc_boolean_t overmem = isc_mem_isovermem(adb->mctx);
949 addr_bucket = DNS_ADB_INVALIDBUCKET;
950 namehook = ISC_LIST_HEAD(*namehooks);
951 while (namehook != NULL) {
952 INSIST(DNS_ADBNAMEHOOK_VALID(namehook));
955 * Clean up the entry if needed.
957 entry = namehook->entry;
959 INSIST(DNS_ADBENTRY_VALID(entry));
961 if (addr_bucket != entry->lock_bucket) {
962 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
963 UNLOCK(&adb->entrylocks[addr_bucket]);
964 addr_bucket = entry->lock_bucket;
965 LOCK(&adb->entrylocks[addr_bucket]);
968 result = dec_entry_refcnt(adb, overmem, entry,
975 namehook->entry = NULL;
976 ISC_LIST_UNLINK(*namehooks, namehook, plink);
977 free_adbnamehook(adb, &namehook);
979 namehook = ISC_LIST_HEAD(*namehooks);
982 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
983 UNLOCK(&adb->entrylocks[addr_bucket]);
988 clean_target(dns_adb_t *adb, dns_name_t *target) {
989 if (dns_name_countlabels(target) > 0) {
990 dns_name_free(target, adb->mctx);
991 dns_name_init(target, NULL);
996 set_target(dns_adb_t *adb, dns_name_t *name, dns_name_t *fname,
997 dns_rdataset_t *rdataset, dns_name_t *target)
1000 dns_namereln_t namereln;
1001 unsigned int nlabels;
1003 dns_rdata_t rdata = DNS_RDATA_INIT;
1004 dns_fixedname_t fixed1, fixed2;
1005 dns_name_t *prefix, *new_target;
1007 REQUIRE(dns_name_countlabels(target) == 0);
1009 if (rdataset->type == dns_rdatatype_cname) {
1010 dns_rdata_cname_t cname;
1013 * Copy the CNAME's target into the target name.
1015 result = dns_rdataset_first(rdataset);
1016 if (result != ISC_R_SUCCESS)
1018 dns_rdataset_current(rdataset, &rdata);
1019 result = dns_rdata_tostruct(&rdata, &cname, NULL);
1020 if (result != ISC_R_SUCCESS)
1022 result = dns_name_dup(&cname.cname, adb->mctx, target);
1023 dns_rdata_freestruct(&cname);
1024 if (result != ISC_R_SUCCESS)
1027 dns_rdata_dname_t dname;
1029 INSIST(rdataset->type == dns_rdatatype_dname);
1030 namereln = dns_name_fullcompare(name, fname, &order, &nlabels);
1031 INSIST(namereln == dns_namereln_subdomain);
1033 * Get the target name of the DNAME.
1035 result = dns_rdataset_first(rdataset);
1036 if (result != ISC_R_SUCCESS)
1038 dns_rdataset_current(rdataset, &rdata);
1039 result = dns_rdata_tostruct(&rdata, &dname, NULL);
1040 if (result != ISC_R_SUCCESS)
1043 * Construct the new target name.
1045 dns_fixedname_init(&fixed1);
1046 prefix = dns_fixedname_name(&fixed1);
1047 dns_fixedname_init(&fixed2);
1048 new_target = dns_fixedname_name(&fixed2);
1049 dns_name_split(name, nlabels, prefix, NULL);
1050 result = dns_name_concatenate(prefix, &dname.dname, new_target,
1052 dns_rdata_freestruct(&dname);
1053 if (result != ISC_R_SUCCESS)
1055 result = dns_name_dup(new_target, adb->mctx, target);
1056 if (result != ISC_R_SUCCESS)
1060 return (ISC_R_SUCCESS);
1064 * Assumes nothing is locked, since this is called by the client.
1067 event_free(isc_event_t *event) {
1068 dns_adbfind_t *find;
1070 INSIST(event != NULL);
1071 find = event->ev_destroy_arg;
1072 INSIST(DNS_ADBFIND_VALID(find));
1075 find->flags |= FIND_EVENT_FREED;
1076 event->ev_destroy_arg = NULL;
1077 UNLOCK(&find->lock);
1081 * Assumes the name bucket is locked.
1084 clean_finds_at_name(dns_adbname_t *name, isc_eventtype_t evtype,
1089 dns_adbfind_t *find;
1090 dns_adbfind_t *next_find;
1091 isc_boolean_t process;
1092 unsigned int wanted, notify;
1095 "ENTER clean_finds_at_name, name %p, evtype %08x, addrs %08x",
1096 name, evtype, addrs);
1098 find = ISC_LIST_HEAD(name->finds);
1099 while (find != NULL) {
1101 next_find = ISC_LIST_NEXT(find, plink);
1103 process = ISC_FALSE;
1104 wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
1105 notify = wanted & addrs;
1108 case DNS_EVENT_ADBMOREADDRESSES:
1109 DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBMOREADDRESSES");
1110 if ((notify) != 0) {
1111 find->flags &= ~addrs;
1115 case DNS_EVENT_ADBNOMOREADDRESSES:
1116 DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBNOMOREADDRESSES");
1117 find->flags &= ~addrs;
1118 wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
1123 find->flags &= ~addrs;
1128 DP(DEF_LEVEL, "cfan: processing find %p", find);
1130 * Unlink the find from the name, letting the caller
1131 * call dns_adb_destroyfind() on it to clean it up
1134 ISC_LIST_UNLINK(name->finds, find, plink);
1135 find->adbname = NULL;
1136 find->name_bucket = DNS_ADB_INVALIDBUCKET;
1138 INSIST(!FIND_EVENTSENT(find));
1141 task = ev->ev_sender;
1142 ev->ev_sender = find;
1143 find->result_v4 = find_err_map[name->fetch_err];
1144 find->result_v6 = find_err_map[name->fetch6_err];
1145 ev->ev_type = evtype;
1146 ev->ev_destroy = event_free;
1147 ev->ev_destroy_arg = find;
1150 "sending event %p to task %p for find %p",
1153 isc_task_sendanddetach(&task, (isc_event_t **)&ev);
1155 DP(DEF_LEVEL, "cfan: skipping find %p", find);
1158 UNLOCK(&find->lock);
1162 DP(ENTER_LEVEL, "EXIT clean_finds_at_name, name %p", name);
1166 check_exit(dns_adb_t *adb) {
1169 * The caller must be holding the adb lock.
1171 if (adb->shutting_down) {
1173 * If there aren't any external references either, we're
1174 * done. Send the control event to initiate shutdown.
1176 INSIST(!adb->cevent_sent); /* Sanity check. */
1177 event = &adb->cevent;
1178 isc_task_send(adb->task, &event);
1179 adb->cevent_sent = ISC_TRUE;
1183 static inline isc_boolean_t
1184 dec_adb_irefcnt(dns_adb_t *adb) {
1187 isc_boolean_t result = ISC_FALSE;
1189 LOCK(&adb->reflock);
1191 INSIST(adb->irefcnt > 0);
1194 if (adb->irefcnt == 0) {
1195 event = ISC_LIST_HEAD(adb->whenshutdown);
1196 while (event != NULL) {
1197 ISC_LIST_UNLINK(adb->whenshutdown, event, ev_link);
1198 etask = event->ev_sender;
1199 event->ev_sender = adb;
1200 isc_task_sendanddetach(&etask, &event);
1201 event = ISC_LIST_HEAD(adb->whenshutdown);
1205 if (adb->irefcnt == 0 && adb->erefcnt == 0)
1207 UNLOCK(&adb->reflock);
1212 inc_adb_irefcnt(dns_adb_t *adb) {
1213 LOCK(&adb->reflock);
1215 UNLOCK(&adb->reflock);
1219 inc_adb_erefcnt(dns_adb_t *adb) {
1220 LOCK(&adb->reflock);
1222 UNLOCK(&adb->reflock);
1226 inc_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
1229 bucket = entry->lock_bucket;
1232 LOCK(&adb->entrylocks[bucket]);
1237 UNLOCK(&adb->entrylocks[bucket]);
1240 static inline isc_boolean_t
1241 dec_entry_refcnt(dns_adb_t *adb, isc_boolean_t overmem, dns_adbentry_t *entry,
1245 isc_boolean_t destroy_entry;
1246 isc_boolean_t result = ISC_FALSE;
1248 bucket = entry->lock_bucket;
1251 LOCK(&adb->entrylocks[bucket]);
1253 INSIST(entry->refcnt > 0);
1256 destroy_entry = ISC_FALSE;
1257 if (entry->refcnt == 0 &&
1258 (adb->entry_sd[bucket] || entry->expires == 0 || overmem ||
1259 (entry->flags & ENTRY_IS_DEAD) != 0)) {
1260 destroy_entry = ISC_TRUE;
1261 result = unlink_entry(adb, entry);
1265 UNLOCK(&adb->entrylocks[bucket]);
1270 entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
1272 free_adbentry(adb, &entry);
1274 result = dec_adb_irefcnt(adb);
1279 static inline dns_adbname_t *
1280 new_adbname(dns_adb_t *adb, dns_name_t *dnsname) {
1281 dns_adbname_t *name;
1283 name = isc_mempool_get(adb->nmp);
1287 dns_name_init(&name->name, NULL);
1288 if (dns_name_dup(dnsname, adb->mctx, &name->name) != ISC_R_SUCCESS) {
1289 isc_mempool_put(adb->nmp, name);
1292 dns_name_init(&name->target, NULL);
1293 name->magic = DNS_ADBNAME_MAGIC;
1295 name->partial_result = 0;
1297 name->expire_v4 = INT_MAX;
1298 name->expire_v6 = INT_MAX;
1299 name->expire_target = INT_MAX;
1301 name->lock_bucket = DNS_ADB_INVALIDBUCKET;
1302 ISC_LIST_INIT(name->v4);
1303 ISC_LIST_INIT(name->v6);
1304 name->fetch_a = NULL;
1305 name->fetch_aaaa = NULL;
1306 name->fetch_err = FIND_ERR_UNEXPECTED;
1307 name->fetch6_err = FIND_ERR_UNEXPECTED;
1308 ISC_LIST_INIT(name->finds);
1309 ISC_LINK_INIT(name, plink);
1315 free_adbname(dns_adb_t *adb, dns_adbname_t **name) {
1318 INSIST(name != NULL && DNS_ADBNAME_VALID(*name));
1322 INSIST(!NAME_HAS_V4(n));
1323 INSIST(!NAME_HAS_V6(n));
1324 INSIST(!NAME_FETCH(n));
1325 INSIST(ISC_LIST_EMPTY(n->finds));
1326 INSIST(!ISC_LINK_LINKED(n, plink));
1327 INSIST(n->lock_bucket == DNS_ADB_INVALIDBUCKET);
1328 INSIST(n->adb == adb);
1331 dns_name_free(&n->name, adb->mctx);
1333 isc_mempool_put(adb->nmp, n);
1336 static inline dns_adbnamehook_t *
1337 new_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry) {
1338 dns_adbnamehook_t *nh;
1340 nh = isc_mempool_get(adb->nhmp);
1344 nh->magic = DNS_ADBNAMEHOOK_MAGIC;
1346 ISC_LINK_INIT(nh, plink);
1352 free_adbnamehook(dns_adb_t *adb, dns_adbnamehook_t **namehook) {
1353 dns_adbnamehook_t *nh;
1355 INSIST(namehook != NULL && DNS_ADBNAMEHOOK_VALID(*namehook));
1359 INSIST(nh->entry == NULL);
1360 INSIST(!ISC_LINK_LINKED(nh, plink));
1363 isc_mempool_put(adb->nhmp, nh);
1366 static inline dns_adblameinfo_t *
1367 new_adblameinfo(dns_adb_t *adb, dns_name_t *qname, dns_rdatatype_t qtype) {
1368 dns_adblameinfo_t *li;
1370 li = isc_mempool_get(adb->limp);
1374 dns_name_init(&li->qname, NULL);
1375 if (dns_name_dup(qname, adb->mctx, &li->qname) != ISC_R_SUCCESS) {
1376 isc_mempool_put(adb->limp, li);
1379 li->magic = DNS_ADBLAMEINFO_MAGIC;
1382 ISC_LINK_INIT(li, plink);
1388 free_adblameinfo(dns_adb_t *adb, dns_adblameinfo_t **lameinfo) {
1389 dns_adblameinfo_t *li;
1391 INSIST(lameinfo != NULL && DNS_ADBLAMEINFO_VALID(*lameinfo));
1395 INSIST(!ISC_LINK_LINKED(li, plink));
1397 dns_name_free(&li->qname, adb->mctx);
1401 isc_mempool_put(adb->limp, li);
1404 static inline dns_adbentry_t *
1405 new_adbentry(dns_adb_t *adb) {
1409 e = isc_mempool_get(adb->emp);
1413 e->magic = DNS_ADBENTRY_MAGIC;
1414 e->lock_bucket = DNS_ADB_INVALIDBUCKET;
1418 e->srtt = (r & 0x1f) + 1;
1420 ISC_LIST_INIT(e->lameinfo);
1421 ISC_LINK_INIT(e, plink);
1427 free_adbentry(dns_adb_t *adb, dns_adbentry_t **entry) {
1429 dns_adblameinfo_t *li;
1431 INSIST(entry != NULL && DNS_ADBENTRY_VALID(*entry));
1435 INSIST(e->lock_bucket == DNS_ADB_INVALIDBUCKET);
1436 INSIST(e->refcnt == 0);
1437 INSIST(!ISC_LINK_LINKED(e, plink));
1441 li = ISC_LIST_HEAD(e->lameinfo);
1442 while (li != NULL) {
1443 ISC_LIST_UNLINK(e->lameinfo, li, plink);
1444 free_adblameinfo(adb, &li);
1445 li = ISC_LIST_HEAD(e->lameinfo);
1448 isc_mempool_put(adb->emp, e);
1451 static inline dns_adbfind_t *
1452 new_adbfind(dns_adb_t *adb) {
1454 isc_result_t result;
1456 h = isc_mempool_get(adb->ahmp);
1465 h->partial_result = 0;
1468 h->result_v4 = ISC_R_UNEXPECTED;
1469 h->result_v6 = ISC_R_UNEXPECTED;
1470 ISC_LINK_INIT(h, publink);
1471 ISC_LINK_INIT(h, plink);
1472 ISC_LIST_INIT(h->list);
1474 h->name_bucket = DNS_ADB_INVALIDBUCKET;
1479 result = isc_mutex_init(&h->lock);
1480 if (result != ISC_R_SUCCESS) {
1481 isc_mempool_put(adb->ahmp, h);
1485 ISC_EVENT_INIT(&h->event, sizeof(isc_event_t), 0, 0, 0, NULL, NULL,
1488 inc_adb_irefcnt(adb);
1489 h->magic = DNS_ADBFIND_MAGIC;
1493 static inline dns_adbfetch_t *
1494 new_adbfetch(dns_adb_t *adb) {
1497 f = isc_mempool_get(adb->afmp);
1504 dns_rdataset_init(&f->rdataset);
1506 f->magic = DNS_ADBFETCH_MAGIC;
1512 free_adbfetch(dns_adb_t *adb, dns_adbfetch_t **fetch) {
1515 INSIST(fetch != NULL && DNS_ADBFETCH_VALID(*fetch));
1521 if (dns_rdataset_isassociated(&f->rdataset))
1522 dns_rdataset_disassociate(&f->rdataset);
1524 isc_mempool_put(adb->afmp, f);
1527 static inline isc_boolean_t
1528 free_adbfind(dns_adb_t *adb, dns_adbfind_t **findp) {
1529 dns_adbfind_t *find;
1531 INSIST(findp != NULL && DNS_ADBFIND_VALID(*findp));
1535 INSIST(!FIND_HAS_ADDRS(find));
1536 INSIST(!ISC_LINK_LINKED(find, publink));
1537 INSIST(!ISC_LINK_LINKED(find, plink));
1538 INSIST(find->name_bucket == DNS_ADB_INVALIDBUCKET);
1539 INSIST(find->adbname == NULL);
1543 DESTROYLOCK(&find->lock);
1544 isc_mempool_put(adb->ahmp, find);
1545 return (dec_adb_irefcnt(adb));
1549 * Copy bits from the entry into the newly allocated addrinfo. The entry
1550 * must be locked, and the reference count must be bumped up by one
1551 * if this function returns a valid pointer.
1553 static inline dns_adbaddrinfo_t *
1554 new_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry, in_port_t port) {
1555 dns_adbaddrinfo_t *ai;
1557 ai = isc_mempool_get(adb->aimp);
1561 ai->magic = DNS_ADBADDRINFO_MAGIC;
1562 ai->sockaddr = entry->sockaddr;
1563 isc_sockaddr_setport(&ai->sockaddr, port);
1564 ai->srtt = entry->srtt;
1565 ai->flags = entry->flags;
1567 ISC_LINK_INIT(ai, publink);
1573 free_adbaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **ainfo) {
1574 dns_adbaddrinfo_t *ai;
1576 INSIST(ainfo != NULL && DNS_ADBADDRINFO_VALID(*ainfo));
1580 INSIST(ai->entry == NULL);
1581 INSIST(!ISC_LINK_LINKED(ai, publink));
1585 isc_mempool_put(adb->aimp, ai);
1589 * Search for the name. NOTE: The bucket is kept locked on both
1590 * success and failure, so it must always be unlocked by the caller!
1592 * On the first call to this function, *bucketp must be set to
1593 * DNS_ADB_INVALIDBUCKET.
1595 static inline dns_adbname_t *
1596 find_name_and_lock(dns_adb_t *adb, dns_name_t *name,
1597 unsigned int options, int *bucketp)
1599 dns_adbname_t *adbname;
1602 bucket = dns_name_fullhash(name, ISC_FALSE) % NBUCKETS;
1604 if (*bucketp == DNS_ADB_INVALIDBUCKET) {
1605 LOCK(&adb->namelocks[bucket]);
1607 } else if (*bucketp != bucket) {
1608 UNLOCK(&adb->namelocks[*bucketp]);
1609 LOCK(&adb->namelocks[bucket]);
1613 adbname = ISC_LIST_HEAD(adb->names[bucket]);
1614 while (adbname != NULL) {
1615 if (!NAME_DEAD(adbname)) {
1616 if (dns_name_equal(name, &adbname->name)
1617 && GLUEHINT_OK(adbname, options)
1618 && STARTATZONE_MATCHES(adbname, options))
1621 adbname = ISC_LIST_NEXT(adbname, plink);
1628 * Search for the address. NOTE: The bucket is kept locked on both
1629 * success and failure, so it must always be unlocked by the caller.
1631 * On the first call to this function, *bucketp must be set to
1632 * DNS_ADB_INVALIDBUCKET. This will cause a lock to occur. On
1633 * later calls (within the same "lock path") it can be left alone, so
1634 * if this function is called multiple times locking is only done if
1635 * the bucket changes.
1637 static inline dns_adbentry_t *
1638 find_entry_and_lock(dns_adb_t *adb, isc_sockaddr_t *addr, int *bucketp,
1641 dns_adbentry_t *entry, *entry_next;
1644 bucket = isc_sockaddr_hash(addr, ISC_TRUE) % NBUCKETS;
1646 if (*bucketp == DNS_ADB_INVALIDBUCKET) {
1647 LOCK(&adb->entrylocks[bucket]);
1649 } else if (*bucketp != bucket) {
1650 UNLOCK(&adb->entrylocks[*bucketp]);
1651 LOCK(&adb->entrylocks[bucket]);
1655 /* Search the list, while cleaning up expired entries. */
1656 for (entry = ISC_LIST_HEAD(adb->entries[bucket]);
1658 entry = entry_next) {
1659 entry_next = ISC_LIST_NEXT(entry, plink);
1660 (void)check_expire_entry(adb, &entry, now);
1661 if (entry != NULL &&
1662 isc_sockaddr_equal(addr, &entry->sockaddr)) {
1663 ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
1664 ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
1673 * Entry bucket MUST be locked!
1675 static isc_boolean_t
1676 entry_is_lame(dns_adb_t *adb, dns_adbentry_t *entry, dns_name_t *qname,
1677 dns_rdatatype_t qtype, isc_stdtime_t now)
1679 dns_adblameinfo_t *li, *next_li;
1680 isc_boolean_t is_bad;
1684 li = ISC_LIST_HEAD(entry->lameinfo);
1687 while (li != NULL) {
1688 next_li = ISC_LIST_NEXT(li, plink);
1691 * Has the entry expired?
1693 if (li->lame_timer < now) {
1694 ISC_LIST_UNLINK(entry->lameinfo, li, plink);
1695 free_adblameinfo(adb, &li);
1699 * Order tests from least to most expensive.
1701 * We do not break out of the main loop here as
1702 * we use the loop for house keeping.
1704 if (li != NULL && !is_bad && li->qtype == qtype &&
1705 dns_name_equal(qname, &li->qname))
1715 copy_namehook_lists(dns_adb_t *adb, dns_adbfind_t *find, dns_name_t *qname,
1716 dns_rdatatype_t qtype, dns_adbname_t *name,
1719 dns_adbnamehook_t *namehook;
1720 dns_adbaddrinfo_t *addrinfo;
1721 dns_adbentry_t *entry;
1724 bucket = DNS_ADB_INVALIDBUCKET;
1726 if (find->options & DNS_ADBFIND_INET) {
1727 namehook = ISC_LIST_HEAD(name->v4);
1728 while (namehook != NULL) {
1729 entry = namehook->entry;
1730 bucket = entry->lock_bucket;
1731 LOCK(&adb->entrylocks[bucket]);
1733 if (!FIND_RETURNLAME(find)
1734 && entry_is_lame(adb, entry, qname, qtype, now)) {
1735 find->options |= DNS_ADBFIND_LAMEPRUNED;
1738 addrinfo = new_adbaddrinfo(adb, entry, find->port);
1739 if (addrinfo == NULL) {
1740 find->partial_result |= DNS_ADBFIND_INET;
1744 * Found a valid entry. Add it to the find's list.
1746 inc_entry_refcnt(adb, entry, ISC_FALSE);
1747 ISC_LIST_APPEND(find->list, addrinfo, publink);
1750 UNLOCK(&adb->entrylocks[bucket]);
1751 bucket = DNS_ADB_INVALIDBUCKET;
1752 namehook = ISC_LIST_NEXT(namehook, plink);
1756 if (find->options & DNS_ADBFIND_INET6) {
1757 namehook = ISC_LIST_HEAD(name->v6);
1758 while (namehook != NULL) {
1759 entry = namehook->entry;
1760 bucket = entry->lock_bucket;
1761 LOCK(&adb->entrylocks[bucket]);
1763 if (!FIND_RETURNLAME(find)
1764 && entry_is_lame(adb, entry, qname, qtype, now)) {
1765 find->options |= DNS_ADBFIND_LAMEPRUNED;
1768 addrinfo = new_adbaddrinfo(adb, entry, find->port);
1769 if (addrinfo == NULL) {
1770 find->partial_result |= DNS_ADBFIND_INET6;
1774 * Found a valid entry. Add it to the find's list.
1776 inc_entry_refcnt(adb, entry, ISC_FALSE);
1777 ISC_LIST_APPEND(find->list, addrinfo, publink);
1780 UNLOCK(&adb->entrylocks[bucket]);
1781 bucket = DNS_ADB_INVALIDBUCKET;
1782 namehook = ISC_LIST_NEXT(namehook, plink);
1787 if (bucket != DNS_ADB_INVALIDBUCKET)
1788 UNLOCK(&adb->entrylocks[bucket]);
1792 shutdown_task(isc_task_t *task, isc_event_t *ev) {
1798 INSIST(DNS_ADB_VALID(adb));
1800 isc_event_free(&ev);
1802 * Wait for lock around check_exit() call to be released.
1810 * Name bucket must be locked; adb may be locked; no other locks held.
1812 static isc_boolean_t
1813 check_expire_name(dns_adbname_t **namep, isc_stdtime_t now) {
1814 dns_adbname_t *name;
1815 isc_boolean_t result = ISC_FALSE;
1817 INSIST(namep != NULL && DNS_ADBNAME_VALID(*namep));
1820 if (NAME_HAS_V4(name) || NAME_HAS_V6(name))
1822 if (NAME_FETCH(name))
1824 if (!EXPIRE_OK(name->expire_v4, now))
1826 if (!EXPIRE_OK(name->expire_v6, now))
1828 if (!EXPIRE_OK(name->expire_target, now))
1832 * The name is empty. Delete it.
1834 result = kill_name(&name, DNS_EVENT_ADBEXPIRED);
1838 * Our caller, or one of its callers, will be calling check_exit() at
1839 * some point, so we don't need to do it here.
1845 * Examine the tail entry of the LRU list to see if it expires or is stale
1846 * (unused for some period); if so, the name entry will be freed. If the ADB
1847 * is in the overmem condition, the tail and the next to tail entries
1848 * will be unconditionally removed (unless they have an outstanding fetch).
1849 * We don't care about a race on 'overmem' at the risk of causing some
1850 * collateral damage or a small delay in starting cleanup, so we don't bother
1851 * to lock ADB (if it's not locked).
1853 * Name bucket must be locked; adb may be locked; no other locks held.
1856 check_stale_name(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
1857 int victims, max_victims;
1858 dns_adbname_t *victim, *next_victim;
1859 isc_boolean_t overmem = isc_mem_isovermem(adb->mctx);
1862 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
1864 max_victims = overmem ? 2 : 1;
1867 * We limit the number of scanned entries to 10 (arbitrary choice)
1868 * in order to avoid examining too many entries when there are many
1869 * tail entries that have fetches (this should be rare, but could
1872 victim = ISC_LIST_TAIL(adb->names[bucket]);
1874 victim != NULL && victims < max_victims && scans < 10;
1875 victim = next_victim) {
1876 INSIST(!NAME_DEAD(victim));
1878 next_victim = ISC_LIST_PREV(victim, plink);
1879 (void)check_expire_name(&victim, now);
1880 if (victim == NULL) {
1885 if (!NAME_FETCH(victim) &&
1886 (overmem || victim->last_used + ADB_STALE_MARGIN <= now)) {
1887 RUNTIME_CHECK(kill_name(&victim,
1888 DNS_EVENT_ADBCANCELED) ==
1900 * Entry bucket must be locked; adb may be locked; no other locks held.
1902 static isc_boolean_t
1903 check_expire_entry(dns_adb_t *adb, dns_adbentry_t **entryp, isc_stdtime_t now)
1905 dns_adbentry_t *entry;
1906 isc_boolean_t result = ISC_FALSE;
1908 INSIST(entryp != NULL && DNS_ADBENTRY_VALID(*entryp));
1911 if (entry->refcnt != 0)
1914 if (entry->expires == 0 || entry->expires > now)
1918 * The entry is not in use. Delete it.
1920 DP(DEF_LEVEL, "killing entry %p", entry);
1921 INSIST(ISC_LINK_LINKED(entry, plink));
1922 result = unlink_entry(adb, entry);
1923 free_adbentry(adb, &entry);
1925 dec_adb_irefcnt(adb);
1931 * ADB must be locked, and no other locks held.
1933 static isc_boolean_t
1934 cleanup_names(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
1935 dns_adbname_t *name;
1936 dns_adbname_t *next_name;
1937 isc_boolean_t result = ISC_FALSE;
1939 DP(CLEAN_LEVEL, "cleaning name bucket %d", bucket);
1941 LOCK(&adb->namelocks[bucket]);
1942 if (adb->name_sd[bucket]) {
1943 UNLOCK(&adb->namelocks[bucket]);
1947 name = ISC_LIST_HEAD(adb->names[bucket]);
1948 while (name != NULL) {
1949 next_name = ISC_LIST_NEXT(name, plink);
1950 INSIST(result == ISC_FALSE);
1951 result = check_expire_namehooks(name, now);
1953 result = check_expire_name(&name, now);
1956 UNLOCK(&adb->namelocks[bucket]);
1961 * ADB must be locked, and no other locks held.
1963 static isc_boolean_t
1964 cleanup_entries(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
1965 dns_adbentry_t *entry, *next_entry;
1966 isc_boolean_t result = ISC_FALSE;
1968 DP(CLEAN_LEVEL, "cleaning entry bucket %d", bucket);
1970 LOCK(&adb->entrylocks[bucket]);
1971 entry = ISC_LIST_HEAD(adb->entries[bucket]);
1972 while (entry != NULL) {
1973 next_entry = ISC_LIST_NEXT(entry, plink);
1974 INSIST(result == ISC_FALSE);
1975 result = check_expire_entry(adb, &entry, now);
1978 UNLOCK(&adb->entrylocks[bucket]);
1983 destroy(dns_adb_t *adb) {
1986 isc_task_detach(&adb->task);
1988 isc_mempool_destroy(&adb->nmp);
1989 isc_mempool_destroy(&adb->nhmp);
1990 isc_mempool_destroy(&adb->limp);
1991 isc_mempool_destroy(&adb->emp);
1992 isc_mempool_destroy(&adb->ahmp);
1993 isc_mempool_destroy(&adb->aimp);
1994 isc_mempool_destroy(&adb->afmp);
1996 DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
1997 DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
1999 DESTROYLOCK(&adb->reflock);
2000 DESTROYLOCK(&adb->lock);
2001 DESTROYLOCK(&adb->mplock);
2002 DESTROYLOCK(&adb->overmemlock);
2004 isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
2013 dns_adb_create(isc_mem_t *mem, dns_view_t *view, isc_timermgr_t *timermgr,
2014 isc_taskmgr_t *taskmgr, dns_adb_t **newadb)
2017 isc_result_t result;
2020 REQUIRE(mem != NULL);
2021 REQUIRE(view != NULL);
2022 REQUIRE(timermgr != NULL); /* this is actually unused */
2023 REQUIRE(taskmgr != NULL);
2024 REQUIRE(newadb != NULL && *newadb == NULL);
2028 adb = isc_mem_get(mem, sizeof(dns_adb_t));
2030 return (ISC_R_NOMEMORY);
2033 * Initialize things here that cannot fail, and especially things
2034 * that must be NULL for the error return to work properly.
2049 adb->taskmgr = taskmgr;
2050 adb->next_cleanbucket = 0;
2051 ISC_EVENT_INIT(&adb->cevent, sizeof(adb->cevent), 0, NULL,
2052 DNS_EVENT_ADBCONTROL, shutdown_task, adb,
2054 adb->cevent_sent = ISC_FALSE;
2055 adb->shutting_down = ISC_FALSE;
2056 ISC_LIST_INIT(adb->whenshutdown);
2058 isc_mem_attach(mem, &adb->mctx);
2060 result = isc_mutex_init(&adb->lock);
2061 if (result != ISC_R_SUCCESS)
2064 result = isc_mutex_init(&adb->mplock);
2065 if (result != ISC_R_SUCCESS)
2068 result = isc_mutex_init(&adb->reflock);
2069 if (result != ISC_R_SUCCESS)
2072 result = isc_mutex_init(&adb->overmemlock);
2073 if (result != ISC_R_SUCCESS)
2077 * Initialize the bucket locks for names and elements.
2078 * May as well initialize the list heads, too.
2080 result = isc_mutexblock_init(adb->namelocks, NBUCKETS);
2081 if (result != ISC_R_SUCCESS)
2083 for (i = 0; i < NBUCKETS; i++) {
2084 ISC_LIST_INIT(adb->names[i]);
2085 ISC_LIST_INIT(adb->deadnames[i]);
2086 adb->name_sd[i] = ISC_FALSE;
2087 adb->name_refcnt[i] = 0;
2090 for (i = 0; i < NBUCKETS; i++) {
2091 ISC_LIST_INIT(adb->entries[i]);
2092 ISC_LIST_INIT(adb->deadentries[i]);
2093 adb->entry_sd[i] = ISC_FALSE;
2094 adb->entry_refcnt[i] = 0;
2097 result = isc_mutexblock_init(adb->entrylocks, NBUCKETS);
2098 if (result != ISC_R_SUCCESS)
2104 #define MPINIT(t, p, n) do { \
2105 result = isc_mempool_create(mem, sizeof(t), &(p)); \
2106 if (result != ISC_R_SUCCESS) \
2108 isc_mempool_setfreemax((p), FREE_ITEMS); \
2109 isc_mempool_setfillcount((p), FILL_COUNT); \
2110 isc_mempool_setname((p), n); \
2111 isc_mempool_associatelock((p), &adb->mplock); \
2114 MPINIT(dns_adbname_t, adb->nmp, "adbname");
2115 MPINIT(dns_adbnamehook_t, adb->nhmp, "adbnamehook");
2116 MPINIT(dns_adblameinfo_t, adb->limp, "adblameinfo");
2117 MPINIT(dns_adbentry_t, adb->emp, "adbentry");
2118 MPINIT(dns_adbfind_t, adb->ahmp, "adbfind");
2119 MPINIT(dns_adbaddrinfo_t, adb->aimp, "adbaddrinfo");
2120 MPINIT(dns_adbfetch_t, adb->afmp, "adbfetch");
2125 * Allocate an internal task.
2127 result = isc_task_create(adb->taskmgr, 0, &adb->task);
2128 if (result != ISC_R_SUCCESS)
2130 isc_task_setname(adb->task, "ADB", adb);
2135 adb->magic = DNS_ADB_MAGIC;
2137 return (ISC_R_SUCCESS);
2140 if (adb->task != NULL)
2141 isc_task_detach(&adb->task);
2143 /* clean up entrylocks */
2144 DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
2146 fail2: /* clean up namelocks */
2147 DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
2149 fail1: /* clean up only allocated memory */
2150 if (adb->nmp != NULL)
2151 isc_mempool_destroy(&adb->nmp);
2152 if (adb->nhmp != NULL)
2153 isc_mempool_destroy(&adb->nhmp);
2154 if (adb->limp != NULL)
2155 isc_mempool_destroy(&adb->limp);
2156 if (adb->emp != NULL)
2157 isc_mempool_destroy(&adb->emp);
2158 if (adb->ahmp != NULL)
2159 isc_mempool_destroy(&adb->ahmp);
2160 if (adb->aimp != NULL)
2161 isc_mempool_destroy(&adb->aimp);
2162 if (adb->afmp != NULL)
2163 isc_mempool_destroy(&adb->afmp);
2165 DESTROYLOCK(&adb->overmemlock);
2167 DESTROYLOCK(&adb->reflock);
2169 DESTROYLOCK(&adb->mplock);
2171 DESTROYLOCK(&adb->lock);
2173 isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
2179 dns_adb_attach(dns_adb_t *adb, dns_adb_t **adbx) {
2181 REQUIRE(DNS_ADB_VALID(adb));
2182 REQUIRE(adbx != NULL && *adbx == NULL);
2184 inc_adb_erefcnt(adb);
2189 dns_adb_detach(dns_adb_t **adbx) {
2191 isc_boolean_t need_exit_check;
2193 REQUIRE(adbx != NULL && DNS_ADB_VALID(*adbx));
2198 INSIST(adb->erefcnt > 0);
2200 LOCK(&adb->reflock);
2202 need_exit_check = ISC_TF(adb->erefcnt == 0 && adb->irefcnt == 0);
2203 UNLOCK(&adb->reflock);
2205 if (need_exit_check) {
2207 INSIST(adb->shutting_down);
2214 dns_adb_whenshutdown(dns_adb_t *adb, isc_task_t *task, isc_event_t **eventp) {
2217 isc_boolean_t zeroirefcnt = ISC_FALSE;
2220 * Send '*eventp' to 'task' when 'adb' has shutdown.
2223 REQUIRE(DNS_ADB_VALID(adb));
2224 REQUIRE(eventp != NULL);
2231 LOCK(&adb->reflock);
2232 zeroirefcnt = ISC_TF(adb->irefcnt == 0);
2234 if (adb->shutting_down && zeroirefcnt &&
2235 isc_mempool_getallocated(adb->ahmp) == 0) {
2237 * We're already shutdown. Send the event.
2239 event->ev_sender = adb;
2240 isc_task_send(task, &event);
2243 isc_task_attach(task, &clone);
2244 event->ev_sender = clone;
2245 ISC_LIST_APPEND(adb->whenshutdown, event, ev_link);
2248 UNLOCK(&adb->reflock);
2253 dns_adb_shutdown(dns_adb_t *adb) {
2254 isc_boolean_t need_check_exit;
2262 if (!adb->shutting_down) {
2263 adb->shutting_down = ISC_TRUE;
2264 isc_mem_setwater(adb->mctx, water, adb, 0, 0);
2265 need_check_exit = shutdown_names(adb);
2266 if (!need_check_exit)
2267 need_check_exit = shutdown_entries(adb);
2268 if (need_check_exit)
2276 dns_adb_createfind(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
2277 void *arg, dns_name_t *name, dns_name_t *qname,
2278 dns_rdatatype_t qtype, unsigned int options,
2279 isc_stdtime_t now, dns_name_t *target,
2280 in_port_t port, dns_adbfind_t **findp)
2282 dns_adbfind_t *find;
2283 dns_adbname_t *adbname;
2285 isc_boolean_t want_event, start_at_zone, alias, have_address;
2286 isc_result_t result;
2287 unsigned int wanted_addresses;
2288 unsigned int wanted_fetches;
2289 unsigned int query_pending;
2291 REQUIRE(DNS_ADB_VALID(adb));
2293 REQUIRE(action != NULL);
2295 REQUIRE(name != NULL);
2296 REQUIRE(qname != NULL);
2297 REQUIRE(findp != NULL && *findp == NULL);
2298 REQUIRE(target == NULL || dns_name_hasbuffer(target));
2300 REQUIRE((options & DNS_ADBFIND_ADDRESSMASK) != 0);
2302 result = ISC_R_UNEXPECTED;
2304 wanted_addresses = (options & DNS_ADBFIND_ADDRESSMASK);
2307 want_event = ISC_FALSE;
2308 start_at_zone = ISC_FALSE;
2312 isc_stdtime_get(&now);
2315 * XXXMLG Move this comment somewhere else!
2317 * Look up the name in our internal database.
2319 * Possibilities: Note that these are not always exclusive.
2321 * No name found. In this case, allocate a new name header and
2322 * an initial namehook or two. If any of these allocations
2323 * fail, clean up and return ISC_R_NOMEMORY.
2325 * Name found, valid addresses present. Allocate one addrinfo
2326 * structure for each found and append it to the linked list
2327 * of addresses for this header.
2329 * Name found, queries pending. In this case, if a task was
2330 * passed in, allocate a job id, attach it to the name's job
2331 * list and remember to tell the caller that there will be
2332 * more info coming later.
2335 find = new_adbfind(adb);
2337 return (ISC_R_NOMEMORY);
2342 * Remember what types of addresses we are interested in.
2344 find->options = options;
2345 find->flags |= wanted_addresses;
2346 if (FIND_WANTEVENT(find)) {
2347 REQUIRE(task != NULL);
2351 * Try to see if we know anything about this name at all.
2353 bucket = DNS_ADB_INVALIDBUCKET;
2354 adbname = find_name_and_lock(adb, name, find->options, &bucket);
2355 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
2356 if (adb->name_sd[bucket]) {
2358 "dns_adb_createfind: returning ISC_R_SHUTTINGDOWN");
2359 RUNTIME_CHECK(free_adbfind(adb, &find) == ISC_FALSE);
2360 result = ISC_R_SHUTTINGDOWN;
2365 * Nothing found. Allocate a new adbname structure for this name.
2367 if (adbname == NULL) {
2369 * See if there is any stale name at the end of list, and purge
2372 check_stale_name(adb, bucket, now);
2374 adbname = new_adbname(adb, name);
2375 if (adbname == NULL) {
2376 RUNTIME_CHECK(free_adbfind(adb, &find) == ISC_FALSE);
2377 result = ISC_R_NOMEMORY;
2380 link_name(adb, bucket, adbname);
2381 if (FIND_HINTOK(find))
2382 adbname->flags |= NAME_HINT_OK;
2383 if (FIND_GLUEOK(find))
2384 adbname->flags |= NAME_GLUE_OK;
2385 if (FIND_STARTATZONE(find))
2386 adbname->flags |= NAME_STARTATZONE;
2388 /* Move this name forward in the LRU list */
2389 ISC_LIST_UNLINK(adb->names[bucket], adbname, plink);
2390 ISC_LIST_PREPEND(adb->names[bucket], adbname, plink);
2392 adbname->last_used = now;
2395 * Expire old entries, etc.
2397 RUNTIME_CHECK(check_expire_namehooks(adbname, now) == ISC_FALSE);
2400 * Do we know that the name is an alias?
2402 if (!EXPIRE_OK(adbname->expire_target, now)) {
2407 "dns_adb_createfind: name %p is an alias (cached)",
2414 * Try to populate the name from the database and/or
2415 * start fetches. First try looking for an A record
2418 if (!NAME_HAS_V4(adbname) && EXPIRE_OK(adbname->expire_v4, now)
2419 && WANT_INET(wanted_addresses)) {
2420 result = dbfind_name(adbname, now, dns_rdatatype_a);
2421 if (result == ISC_R_SUCCESS) {
2423 "dns_adb_createfind: found A for name %p in db",
2429 * Did we get a CNAME or DNAME?
2431 if (result == DNS_R_ALIAS) {
2433 "dns_adb_createfind: name %p is an alias",
2440 * If the name doesn't exist at all, don't bother with
2441 * v6 queries; they won't work.
2443 * If the name does exist but we didn't get our data, go
2444 * ahead and try AAAA.
2446 * If the result is neither of these, try a fetch for A.
2448 if (NXDOMAIN_RESULT(result))
2450 else if (NXRRSET_RESULT(result))
2453 if (!NAME_FETCH_V4(adbname))
2454 wanted_fetches |= DNS_ADBFIND_INET;
2458 if (!NAME_HAS_V6(adbname) && EXPIRE_OK(adbname->expire_v6, now)
2459 && WANT_INET6(wanted_addresses)) {
2460 result = dbfind_name(adbname, now, dns_rdatatype_aaaa);
2461 if (result == ISC_R_SUCCESS) {
2463 "dns_adb_createfind: found AAAA for name %p",
2469 * Did we get a CNAME or DNAME?
2471 if (result == DNS_R_ALIAS) {
2473 "dns_adb_createfind: name %p is an alias",
2480 * Listen to negative cache hints, and don't start
2483 if (NCACHE_RESULT(result) || AUTH_NX(result))
2486 if (!NAME_FETCH_V6(adbname))
2487 wanted_fetches |= DNS_ADBFIND_INET6;
2491 if ((WANT_INET(wanted_addresses) && NAME_HAS_V4(adbname)) ||
2492 (WANT_INET6(wanted_addresses) && NAME_HAS_V6(adbname)))
2493 have_address = ISC_TRUE;
2495 have_address = ISC_FALSE;
2496 if (wanted_fetches != 0 &&
2497 ! (FIND_AVOIDFETCHES(find) && have_address)) {
2499 * We're missing at least one address family. Either the
2500 * caller hasn't instructed us to avoid fetches, or we don't
2501 * know anything about any of the address families that would
2502 * be acceptable so we have to launch fetches.
2505 if (FIND_STARTATZONE(find))
2506 start_at_zone = ISC_TRUE;
2511 if (WANT_INET(wanted_fetches) &&
2512 fetch_name(adbname, start_at_zone,
2513 dns_rdatatype_a) == ISC_R_SUCCESS) {
2515 "dns_adb_createfind: started A fetch for name %p",
2522 if (WANT_INET6(wanted_fetches) &&
2523 fetch_name(adbname, start_at_zone,
2524 dns_rdatatype_aaaa) == ISC_R_SUCCESS) {
2526 "dns_adb_createfind: "
2527 "started AAAA fetch for name %p",
2533 * Run through the name and copy out the bits we are
2536 copy_namehook_lists(adb, find, qname, qtype, adbname, now);
2539 if (NAME_FETCH_V4(adbname))
2540 query_pending |= DNS_ADBFIND_INET;
2541 if (NAME_FETCH_V6(adbname))
2542 query_pending |= DNS_ADBFIND_INET6;
2545 * Attach to the name's query list if there are queries
2546 * already running, and we have been asked to.
2548 want_event = ISC_TRUE;
2549 if (!FIND_WANTEVENT(find))
2550 want_event = ISC_FALSE;
2551 if (FIND_WANTEMPTYEVENT(find) && FIND_HAS_ADDRS(find))
2552 want_event = ISC_FALSE;
2553 if ((wanted_addresses & query_pending) == 0)
2554 want_event = ISC_FALSE;
2556 want_event = ISC_FALSE;
2558 find->adbname = adbname;
2559 find->name_bucket = bucket;
2560 ISC_LIST_APPEND(adbname->finds, find, plink);
2561 find->query_pending = (query_pending & wanted_addresses);
2562 find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
2563 find->flags |= (find->query_pending & DNS_ADBFIND_ADDRESSMASK);
2564 DP(DEF_LEVEL, "createfind: attaching find %p to adbname %p",
2568 * Remove the flag so the caller knows there will never
2569 * be an event, and set internal flags to fake that
2570 * the event was sent and freed, so dns_adb_destroyfind() will
2571 * do the right thing.
2573 find->query_pending = (query_pending & wanted_addresses);
2574 find->options &= ~DNS_ADBFIND_WANTEVENT;
2575 find->flags |= (FIND_EVENT_SENT | FIND_EVENT_FREED);
2576 find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
2579 find->partial_result |= (adbname->partial_result & wanted_addresses);
2581 if (target != NULL) {
2582 result = dns_name_copy(&adbname->target, target, NULL);
2583 if (result != ISC_R_SUCCESS)
2586 result = DNS_R_ALIAS;
2588 result = ISC_R_SUCCESS;
2591 * Copy out error flags from the name structure into the find.
2593 find->result_v4 = find_err_map[adbname->fetch_err];
2594 find->result_v6 = find_err_map[adbname->fetch6_err];
2603 INSIST((find->flags & DNS_ADBFIND_ADDRESSMASK) != 0);
2605 isc_task_attach(task, &taskp);
2606 find->event.ev_sender = taskp;
2607 find->event.ev_action = action;
2608 find->event.ev_arg = arg;
2612 UNLOCK(&adb->namelocks[bucket]);
2618 dns_adb_destroyfind(dns_adbfind_t **findp) {
2619 dns_adbfind_t *find;
2620 dns_adbentry_t *entry;
2621 dns_adbaddrinfo_t *ai;
2624 isc_boolean_t overmem;
2626 REQUIRE(findp != NULL && DNS_ADBFIND_VALID(*findp));
2632 DP(DEF_LEVEL, "dns_adb_destroyfind on find %p", find);
2635 REQUIRE(DNS_ADB_VALID(adb));
2637 REQUIRE(FIND_EVENTFREED(find));
2639 bucket = find->name_bucket;
2640 INSIST(bucket == DNS_ADB_INVALIDBUCKET);
2642 UNLOCK(&find->lock);
2645 * The find doesn't exist on any list, and nothing is locked.
2646 * Return the find to the memory pool, and decrement the adb's
2649 overmem = isc_mem_isovermem(adb->mctx);
2650 ai = ISC_LIST_HEAD(find->list);
2651 while (ai != NULL) {
2652 ISC_LIST_UNLINK(find->list, ai, publink);
2655 INSIST(DNS_ADBENTRY_VALID(entry));
2656 RUNTIME_CHECK(dec_entry_refcnt(adb, overmem, entry, ISC_TRUE) ==
2658 free_adbaddrinfo(adb, &ai);
2659 ai = ISC_LIST_HEAD(find->list);
2663 * WARNING: The find is freed with the adb locked. This is done
2664 * to avoid a race condition where we free the find, some other
2665 * thread tests to see if it should be destroyed, detects it should
2666 * be, destroys it, and then we try to lock it for our check, but the
2667 * lock is destroyed.
2670 if (free_adbfind(adb, &find))
2676 dns_adb_cancelfind(dns_adbfind_t *find) {
2685 DP(DEF_LEVEL, "dns_adb_cancelfind on find %p", find);
2688 REQUIRE(DNS_ADB_VALID(adb));
2690 REQUIRE(!FIND_EVENTFREED(find));
2691 REQUIRE(FIND_WANTEVENT(find));
2693 bucket = find->name_bucket;
2694 if (bucket == DNS_ADB_INVALIDBUCKET)
2698 * We need to get the adbname's lock to unlink the find.
2700 unlock_bucket = bucket;
2701 violate_locking_hierarchy(&find->lock, &adb->namelocks[unlock_bucket]);
2702 bucket = find->name_bucket;
2703 if (bucket != DNS_ADB_INVALIDBUCKET) {
2704 ISC_LIST_UNLINK(find->adbname->finds, find, plink);
2705 find->adbname = NULL;
2706 find->name_bucket = DNS_ADB_INVALIDBUCKET;
2708 UNLOCK(&adb->namelocks[unlock_bucket]);
2709 bucket = DNS_ADB_INVALIDBUCKET;
2714 if (!FIND_EVENTSENT(find)) {
2716 task = ev->ev_sender;
2717 ev->ev_sender = find;
2718 ev->ev_type = DNS_EVENT_ADBCANCELED;
2719 ev->ev_destroy = event_free;
2720 ev->ev_destroy_arg = find;
2721 find->result_v4 = ISC_R_CANCELED;
2722 find->result_v6 = ISC_R_CANCELED;
2724 DP(DEF_LEVEL, "sending event %p to task %p for find %p",
2727 isc_task_sendanddetach(&task, (isc_event_t **)&ev);
2730 UNLOCK(&find->lock);
2734 dns_adb_dump(dns_adb_t *adb, FILE *f) {
2738 REQUIRE(DNS_ADB_VALID(adb));
2742 * Lock the adb itself, lock all the name buckets, then lock all
2743 * the entry buckets. This should put the adb into a state where
2744 * nothing can change, so we can iterate through everything and
2745 * print at our leisure.
2749 isc_stdtime_get(&now);
2751 for (i = 0; i < NBUCKETS; i++)
2752 RUNTIME_CHECK(cleanup_names(adb, i, now) == ISC_FALSE);
2753 for (i = 0; i < NBUCKETS; i++)
2754 RUNTIME_CHECK(cleanup_entries(adb, i, now) == ISC_FALSE);
2756 dump_adb(adb, f, ISC_FALSE, now);
2761 dump_ttl(FILE *f, const char *legend, isc_stdtime_t value, isc_stdtime_t now) {
2762 if (value == INT_MAX)
2764 fprintf(f, " [%s TTL %d]", legend, value - now);
2768 dump_adb(dns_adb_t *adb, FILE *f, isc_boolean_t debug, isc_stdtime_t now) {
2770 dns_adbname_t *name;
2771 dns_adbentry_t *entry;
2773 fprintf(f, ";\n; Address database dump\n;\n");
2775 fprintf(f, "; addr %p, erefcnt %u, irefcnt %u, finds out %u\n",
2776 adb, adb->erefcnt, adb->irefcnt,
2777 isc_mempool_getallocated(adb->nhmp));
2779 for (i = 0; i < NBUCKETS; i++)
2780 LOCK(&adb->namelocks[i]);
2781 for (i = 0; i < NBUCKETS; i++)
2782 LOCK(&adb->entrylocks[i]);
2787 for (i = 0; i < NBUCKETS; i++) {
2788 name = ISC_LIST_HEAD(adb->names[i]);
2792 fprintf(f, "; bucket %d\n", i);
2795 name = ISC_LIST_NEXT(name, plink))
2798 fprintf(f, "; name %p (flags %08x)\n",
2802 print_dns_name(f, &name->name);
2803 if (dns_name_countlabels(&name->target) > 0) {
2804 fprintf(f, " alias ");
2805 print_dns_name(f, &name->target);
2808 dump_ttl(f, "v4", name->expire_v4, now);
2809 dump_ttl(f, "v6", name->expire_v6, now);
2810 dump_ttl(f, "target", name->expire_target, now);
2812 fprintf(f, " [v4 %s] [v6 %s]",
2813 errnames[name->fetch_err],
2814 errnames[name->fetch6_err]);
2818 print_namehook_list(f, "v4", &name->v4, debug, now);
2819 print_namehook_list(f, "v6", &name->v6, debug, now);
2822 print_fetch_list(f, name);
2824 print_find_list(f, name);
2829 fprintf(f, ";\n; Unassociated entries\n;\n");
2831 for (i = 0; i < NBUCKETS; i++) {
2832 entry = ISC_LIST_HEAD(adb->entries[i]);
2833 while (entry != NULL) {
2834 if (entry->refcnt == 0)
2835 dump_entry(f, entry, debug, now);
2836 entry = ISC_LIST_NEXT(entry, plink);
2843 for (i = 0; i < NBUCKETS; i++)
2844 UNLOCK(&adb->entrylocks[i]);
2845 for (i = 0; i < NBUCKETS; i++)
2846 UNLOCK(&adb->namelocks[i]);
2850 dump_entry(FILE *f, dns_adbentry_t *entry, isc_boolean_t debug,
2853 char addrbuf[ISC_NETADDR_FORMATSIZE];
2854 char typebuf[DNS_RDATATYPE_FORMATSIZE];
2855 isc_netaddr_t netaddr;
2856 dns_adblameinfo_t *li;
2858 isc_netaddr_fromsockaddr(&netaddr, &entry->sockaddr);
2859 isc_netaddr_format(&netaddr, addrbuf, sizeof(addrbuf));
2862 fprintf(f, ";\t%p: refcnt %u\n", entry, entry->refcnt);
2864 fprintf(f, ";\t%s [srtt %u] [flags %08x]",
2865 addrbuf, entry->srtt, entry->flags);
2866 if (entry->expires != 0)
2867 fprintf(f, " [ttl %d]", entry->expires - now);
2869 for (li = ISC_LIST_HEAD(entry->lameinfo);
2871 li = ISC_LIST_NEXT(li, plink)) {
2872 fprintf(f, ";\t\t");
2873 print_dns_name(f, &li->qname);
2874 dns_rdatatype_format(li->qtype, typebuf, sizeof(typebuf));
2875 fprintf(f, " %s [lame TTL %d]\n", typebuf,
2876 li->lame_timer - now);
2881 dns_adb_dumpfind(dns_adbfind_t *find, FILE *f) {
2884 dns_adbaddrinfo_t *ai;
2888 * Not used currently, in the API Just In Case we
2889 * want to dump out the name and/or entries too.
2894 fprintf(f, ";Find %p\n", find);
2895 fprintf(f, ";\tqpending %08x partial %08x options %08x flags %08x\n",
2896 find->query_pending, find->partial_result,
2897 find->options, find->flags);
2898 fprintf(f, ";\tname_bucket %d, name %p, event sender %p\n",
2899 find->name_bucket, find->adbname, find->event.ev_sender);
2901 ai = ISC_LIST_HEAD(find->list);
2903 fprintf(f, "\tAddresses:\n");
2904 while (ai != NULL) {
2906 switch (sa->type.sa.sa_family) {
2908 tmpp = inet_ntop(AF_INET, &sa->type.sin.sin_addr,
2912 tmpp = inet_ntop(AF_INET6, &sa->type.sin6.sin6_addr,
2920 tmpp = "BadAddress";
2922 fprintf(f, "\t\tentry %p, flags %08x"
2923 " srtt %u addr %s\n",
2924 ai->entry, ai->flags, ai->srtt, tmpp);
2926 ai = ISC_LIST_NEXT(ai, publink);
2929 UNLOCK(&find->lock);
2933 print_dns_name(FILE *f, dns_name_t *name) {
2934 char buf[DNS_NAME_FORMATSIZE];
2938 dns_name_format(name, buf, sizeof(buf));
2939 fprintf(f, "%s", buf);
2943 print_namehook_list(FILE *f, const char *legend, dns_adbnamehooklist_t *list,
2944 isc_boolean_t debug, isc_stdtime_t now)
2946 dns_adbnamehook_t *nh;
2948 for (nh = ISC_LIST_HEAD(*list);
2950 nh = ISC_LIST_NEXT(nh, plink))
2953 fprintf(f, ";\tHook(%s) %p\n", legend, nh);
2954 dump_entry(f, nh->entry, debug, now);
2959 print_fetch(FILE *f, dns_adbfetch_t *ft, const char *type) {
2960 fprintf(f, "\t\tFetch(%s): %p -> { fetch %p }\n",
2961 type, ft, ft->fetch);
2965 print_fetch_list(FILE *f, dns_adbname_t *n) {
2966 if (NAME_FETCH_A(n))
2967 print_fetch(f, n->fetch_a, "A");
2968 if (NAME_FETCH_AAAA(n))
2969 print_fetch(f, n->fetch_aaaa, "AAAA");
2973 print_find_list(FILE *f, dns_adbname_t *name) {
2974 dns_adbfind_t *find;
2976 find = ISC_LIST_HEAD(name->finds);
2977 while (find != NULL) {
2978 dns_adb_dumpfind(find, f);
2979 find = ISC_LIST_NEXT(find, plink);
2984 dbfind_name(dns_adbname_t *adbname, isc_stdtime_t now, dns_rdatatype_t rdtype)
2986 isc_result_t result;
2987 dns_rdataset_t rdataset;
2989 dns_fixedname_t foundname;
2992 INSIST(DNS_ADBNAME_VALID(adbname));
2994 INSIST(DNS_ADB_VALID(adb));
2995 INSIST(rdtype == dns_rdatatype_a || rdtype == dns_rdatatype_aaaa);
2997 dns_fixedname_init(&foundname);
2998 fname = dns_fixedname_name(&foundname);
2999 dns_rdataset_init(&rdataset);
3001 if (rdtype == dns_rdatatype_a)
3002 adbname->fetch_err = FIND_ERR_UNEXPECTED;
3004 adbname->fetch6_err = FIND_ERR_UNEXPECTED;
3006 result = dns_view_find(adb->view, &adbname->name, rdtype, now,
3007 NAME_GLUEOK(adbname) ? DNS_DBFIND_GLUEOK : 0,
3008 ISC_TF(NAME_HINTOK(adbname)),
3009 NULL, NULL, fname, &rdataset, NULL);
3011 /* XXXVIX this switch statement is too sparse to gen a jump table. */
3017 * Found in the database. Even if we can't copy out
3018 * any information, return success, or else a fetch
3019 * will be made, which will only make things worse.
3021 if (rdtype == dns_rdatatype_a)
3022 adbname->fetch_err = FIND_ERR_SUCCESS;
3024 adbname->fetch6_err = FIND_ERR_SUCCESS;
3025 result = import_rdataset(adbname, &rdataset, now);
3027 case DNS_R_NXDOMAIN:
3030 * We're authoritative and the data doesn't exist.
3031 * Make up a negative cache entry so we don't ask again
3034 * XXXRTH What time should we use? I'm putting in 30 seconds
3037 if (rdtype == dns_rdatatype_a) {
3038 adbname->expire_v4 = now + 30;
3040 "adb name %p: Caching auth negative entry for A",
3042 if (result == DNS_R_NXDOMAIN)
3043 adbname->fetch_err = FIND_ERR_NXDOMAIN;
3045 adbname->fetch_err = FIND_ERR_NXRRSET;
3048 "adb name %p: Caching auth negative entry for AAAA",
3050 adbname->expire_v6 = now + 30;
3051 if (result == DNS_R_NXDOMAIN)
3052 adbname->fetch6_err = FIND_ERR_NXDOMAIN;
3054 adbname->fetch6_err = FIND_ERR_NXRRSET;
3057 case DNS_R_NCACHENXDOMAIN:
3058 case DNS_R_NCACHENXRRSET:
3060 * We found a negative cache entry. Pull the TTL from it
3061 * so we won't ask again for a while.
3063 rdataset.ttl = ttlclamp(rdataset.ttl);
3064 if (rdtype == dns_rdatatype_a) {
3065 adbname->expire_v4 = rdataset.ttl + now;
3066 if (result == DNS_R_NCACHENXDOMAIN)
3067 adbname->fetch_err = FIND_ERR_NXDOMAIN;
3069 adbname->fetch_err = FIND_ERR_NXRRSET;
3071 "adb name %p: Caching negative entry for A (ttl %u)",
3072 adbname, rdataset.ttl);
3075 "adb name %p: Caching negative entry for AAAA (ttl %u)",
3076 adbname, rdataset.ttl);
3077 adbname->expire_v6 = rdataset.ttl + now;
3078 if (result == DNS_R_NCACHENXDOMAIN)
3079 adbname->fetch6_err = FIND_ERR_NXDOMAIN;
3081 adbname->fetch6_err = FIND_ERR_NXRRSET;
3087 * Clear the hint and glue flags, so this will match
3090 adbname->flags &= ~(DNS_ADBFIND_GLUEOK | DNS_ADBFIND_HINTOK);
3092 rdataset.ttl = ttlclamp(rdataset.ttl);
3093 clean_target(adb, &adbname->target);
3094 adbname->expire_target = INT_MAX;
3095 result = set_target(adb, &adbname->name, fname, &rdataset,
3097 if (result == ISC_R_SUCCESS) {
3098 result = DNS_R_ALIAS;
3100 "adb name %p: caching alias target",
3102 adbname->expire_target = rdataset.ttl + now;
3104 if (rdtype == dns_rdatatype_a)
3105 adbname->fetch_err = FIND_ERR_SUCCESS;
3107 adbname->fetch6_err = FIND_ERR_SUCCESS;
3111 if (dns_rdataset_isassociated(&rdataset))
3112 dns_rdataset_disassociate(&rdataset);
3118 fetch_callback(isc_task_t *task, isc_event_t *ev) {
3119 dns_fetchevent_t *dev;
3120 dns_adbname_t *name;
3122 dns_adbfetch_t *fetch;
3124 isc_eventtype_t ev_status;
3126 isc_result_t result;
3127 unsigned int address_type;
3128 isc_boolean_t want_check_exit = ISC_FALSE;
3132 INSIST(ev->ev_type == DNS_EVENT_FETCHDONE);
3133 dev = (dns_fetchevent_t *)ev;
3135 INSIST(DNS_ADBNAME_VALID(name));
3137 INSIST(DNS_ADB_VALID(adb));
3139 bucket = name->lock_bucket;
3140 LOCK(&adb->namelocks[bucket]);
3142 INSIST(NAME_FETCH_A(name) || NAME_FETCH_AAAA(name));
3144 if (NAME_FETCH_A(name) && (name->fetch_a->fetch == dev->fetch)) {
3145 address_type = DNS_ADBFIND_INET;
3146 fetch = name->fetch_a;
3147 name->fetch_a = NULL;
3148 } else if (NAME_FETCH_AAAA(name)
3149 && (name->fetch_aaaa->fetch == dev->fetch)) {
3150 address_type = DNS_ADBFIND_INET6;
3151 fetch = name->fetch_aaaa;
3152 name->fetch_aaaa = NULL;
3156 INSIST(address_type != 0 && fetch != NULL);
3158 dns_resolver_destroyfetch(&fetch->fetch);
3161 ev_status = DNS_EVENT_ADBNOMOREADDRESSES;
3164 * Cleanup things we don't care about.
3166 if (dev->node != NULL)
3167 dns_db_detachnode(dev->db, &dev->node);
3168 if (dev->db != NULL)
3169 dns_db_detach(&dev->db);
3172 * If this name is marked as dead, clean up, throwing away
3173 * potentially good data.
3175 if (NAME_DEAD(name)) {
3176 free_adbfetch(adb, &fetch);
3177 isc_event_free(&ev);
3179 want_check_exit = kill_name(&name, DNS_EVENT_ADBCANCELED);
3181 UNLOCK(&adb->namelocks[bucket]);
3183 if (want_check_exit) {
3192 isc_stdtime_get(&now);
3195 * If we got a negative cache response, remember it.
3197 if (NCACHE_RESULT(dev->result)) {
3198 dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
3199 if (address_type == DNS_ADBFIND_INET) {
3200 DP(NCACHE_LEVEL, "adb fetch name %p: "
3201 "caching negative entry for A (ttl %u)",
3202 name, dev->rdataset->ttl);
3203 name->expire_v4 = ISC_MIN(name->expire_v4,
3204 dev->rdataset->ttl + now);
3205 if (dev->result == DNS_R_NCACHENXDOMAIN)
3206 name->fetch_err = FIND_ERR_NXDOMAIN;
3208 name->fetch_err = FIND_ERR_NXRRSET;
3209 inc_stats(adb, dns_resstatscounter_gluefetchv4fail);
3211 DP(NCACHE_LEVEL, "adb fetch name %p: "
3212 "caching negative entry for AAAA (ttl %u)",
3213 name, dev->rdataset->ttl);
3214 name->expire_v6 = ISC_MIN(name->expire_v6,
3215 dev->rdataset->ttl + now);
3216 if (dev->result == DNS_R_NCACHENXDOMAIN)
3217 name->fetch6_err = FIND_ERR_NXDOMAIN;
3219 name->fetch6_err = FIND_ERR_NXRRSET;
3220 inc_stats(adb, dns_resstatscounter_gluefetchv6fail);
3226 * Handle CNAME/DNAME.
3228 if (dev->result == DNS_R_CNAME || dev->result == DNS_R_DNAME) {
3229 dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
3230 clean_target(adb, &name->target);
3231 name->expire_target = INT_MAX;
3232 result = set_target(adb, &name->name,
3233 dns_fixedname_name(&dev->foundname),
3236 if (result == ISC_R_SUCCESS) {
3238 "adb fetch name %p: caching alias target",
3240 name->expire_target = dev->rdataset->ttl + now;
3246 * Did we get back junk? If so, and there are no more fetches
3247 * sitting out there, tell all the finds about it.
3249 if (dev->result != ISC_R_SUCCESS) {
3250 char buf[DNS_NAME_FORMATSIZE];
3252 dns_name_format(&name->name, buf, sizeof(buf));
3253 DP(DEF_LEVEL, "adb: fetch of '%s' %s failed: %s",
3254 buf, address_type == DNS_ADBFIND_INET ? "A" : "AAAA",
3255 dns_result_totext(dev->result));
3256 /* XXXMLG Don't pound on bad servers. */
3257 if (address_type == DNS_ADBFIND_INET) {
3258 name->expire_v4 = ISC_MIN(name->expire_v4, now + 300);
3259 name->fetch_err = FIND_ERR_FAILURE;
3260 inc_stats(adb, dns_resstatscounter_gluefetchv4fail);
3262 name->expire_v6 = ISC_MIN(name->expire_v6, now + 300);
3263 name->fetch6_err = FIND_ERR_FAILURE;
3264 inc_stats(adb, dns_resstatscounter_gluefetchv6fail);
3270 * We got something potentially useful.
3272 result = import_rdataset(name, &fetch->rdataset, now);
3275 if (result == ISC_R_SUCCESS) {
3276 ev_status = DNS_EVENT_ADBMOREADDRESSES;
3277 if (address_type == DNS_ADBFIND_INET)
3278 name->fetch_err = FIND_ERR_SUCCESS;
3280 name->fetch6_err = FIND_ERR_SUCCESS;
3284 free_adbfetch(adb, &fetch);
3285 isc_event_free(&ev);
3287 clean_finds_at_name(name, ev_status, address_type);
3289 UNLOCK(&adb->namelocks[bucket]);
3293 fetch_name(dns_adbname_t *adbname,
3294 isc_boolean_t start_at_zone,
3295 dns_rdatatype_t type)
3297 isc_result_t result;
3298 dns_adbfetch_t *fetch = NULL;
3300 dns_fixedname_t fixed;
3302 dns_rdataset_t rdataset;
3303 dns_rdataset_t *nameservers;
3304 unsigned int options;
3306 INSIST(DNS_ADBNAME_VALID(adbname));
3308 INSIST(DNS_ADB_VALID(adb));
3310 INSIST((type == dns_rdatatype_a && !NAME_FETCH_V4(adbname)) ||
3311 (type == dns_rdatatype_aaaa && !NAME_FETCH_V6(adbname)));
3313 adbname->fetch_err = FIND_ERR_NOTFOUND;
3317 dns_rdataset_init(&rdataset);
3319 options = DNS_FETCHOPT_NOVALIDATE;
3320 if (start_at_zone) {
3322 "fetch_name: starting at zone for name %p",
3324 dns_fixedname_init(&fixed);
3325 name = dns_fixedname_name(&fixed);
3326 result = dns_view_findzonecut2(adb->view, &adbname->name, name,
3327 0, 0, ISC_TRUE, ISC_FALSE,
3329 if (result != ISC_R_SUCCESS && result != DNS_R_HINT)
3331 nameservers = &rdataset;
3332 options |= DNS_FETCHOPT_UNSHARED;
3335 fetch = new_adbfetch(adb);
3336 if (fetch == NULL) {
3337 result = ISC_R_NOMEMORY;
3341 result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
3342 type, name, nameservers, NULL,
3343 options, adb->task, fetch_callback,
3344 adbname, &fetch->rdataset, NULL,
3346 if (result != ISC_R_SUCCESS)
3349 if (type == dns_rdatatype_a) {
3350 adbname->fetch_a = fetch;
3351 inc_stats(adb, dns_resstatscounter_gluefetchv4);
3353 adbname->fetch_aaaa = fetch;
3354 inc_stats(adb, dns_resstatscounter_gluefetchv6);
3356 fetch = NULL; /* Keep us from cleaning this up below. */
3360 free_adbfetch(adb, &fetch);
3361 if (dns_rdataset_isassociated(&rdataset))
3362 dns_rdataset_disassociate(&rdataset);
3368 * XXXMLG Needs to take a find argument and an address info, no zone or adb,
3369 * since these can be extracted from the find itself.
3372 dns_adb_marklame(dns_adb_t *adb, dns_adbaddrinfo_t *addr, dns_name_t *qname,
3373 dns_rdatatype_t qtype, isc_stdtime_t expire_time)
3375 dns_adblameinfo_t *li;
3377 isc_result_t result = ISC_R_SUCCESS;
3379 REQUIRE(DNS_ADB_VALID(adb));
3380 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3381 REQUIRE(qname != NULL);
3383 bucket = addr->entry->lock_bucket;
3384 LOCK(&adb->entrylocks[bucket]);
3385 li = ISC_LIST_HEAD(addr->entry->lameinfo);
3386 while (li != NULL &&
3387 (li->qtype != qtype || !dns_name_equal(qname, &li->qname)))
3388 li = ISC_LIST_NEXT(li, plink);
3390 if (expire_time > li->lame_timer)
3391 li->lame_timer = expire_time;
3394 li = new_adblameinfo(adb, qname, qtype);
3396 result = ISC_R_NOMEMORY;
3400 li->lame_timer = expire_time;
3402 ISC_LIST_PREPEND(addr->entry->lameinfo, li, plink);
3404 UNLOCK(&adb->entrylocks[bucket]);
3410 dns_adb_adjustsrtt(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
3411 unsigned int rtt, unsigned int factor)
3414 unsigned int new_srtt;
3417 REQUIRE(DNS_ADB_VALID(adb));
3418 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3419 REQUIRE(factor <= 10);
3421 bucket = addr->entry->lock_bucket;
3422 LOCK(&adb->entrylocks[bucket]);
3424 if (factor == DNS_ADB_RTTADJAGE)
3425 new_srtt = addr->entry->srtt * 98 / 100;
3427 new_srtt = (addr->entry->srtt / 10 * factor)
3428 + (rtt / 10 * (10 - factor));
3430 addr->entry->srtt = new_srtt;
3431 addr->srtt = new_srtt;
3433 if (addr->entry->expires == 0) {
3434 isc_stdtime_get(&now);
3435 addr->entry->expires = now + ADB_ENTRY_WINDOW;
3438 UNLOCK(&adb->entrylocks[bucket]);
3442 dns_adb_changeflags(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
3443 unsigned int bits, unsigned int mask)
3448 REQUIRE(DNS_ADB_VALID(adb));
3449 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3451 bucket = addr->entry->lock_bucket;
3452 LOCK(&adb->entrylocks[bucket]);
3454 addr->entry->flags = (addr->entry->flags & ~mask) | (bits & mask);
3455 if (addr->entry->expires == 0) {
3456 isc_stdtime_get(&now);
3457 addr->entry->expires = now + ADB_ENTRY_WINDOW;
3461 * Note that we do not update the other bits in addr->flags with
3462 * the most recent values from addr->entry->flags.
3464 addr->flags = (addr->flags & ~mask) | (bits & mask);
3466 UNLOCK(&adb->entrylocks[bucket]);
3470 dns_adb_findaddrinfo(dns_adb_t *adb, isc_sockaddr_t *sa,
3471 dns_adbaddrinfo_t **addrp, isc_stdtime_t now)
3474 dns_adbentry_t *entry;
3475 dns_adbaddrinfo_t *addr;
3476 isc_result_t result;
3479 REQUIRE(DNS_ADB_VALID(adb));
3480 REQUIRE(addrp != NULL && *addrp == NULL);
3484 result = ISC_R_SUCCESS;
3485 bucket = DNS_ADB_INVALIDBUCKET;
3486 entry = find_entry_and_lock(adb, sa, &bucket, now);
3487 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
3488 if (adb->entry_sd[bucket]) {
3489 result = ISC_R_SHUTTINGDOWN;
3492 if (entry == NULL) {
3494 * We don't know anything about this address.
3496 entry = new_adbentry(adb);
3497 if (entry == NULL) {
3498 result = ISC_R_NOMEMORY;
3501 entry->sockaddr = *sa;
3502 link_entry(adb, bucket, entry);
3503 DP(ENTER_LEVEL, "findaddrinfo: new entry %p", entry);
3505 DP(ENTER_LEVEL, "findaddrinfo: found entry %p", entry);
3507 port = isc_sockaddr_getport(sa);
3508 addr = new_adbaddrinfo(adb, entry, port);
3510 result = ISC_R_NOMEMORY;
3512 inc_entry_refcnt(adb, entry, ISC_FALSE);
3517 UNLOCK(&adb->entrylocks[bucket]);
3523 dns_adb_freeaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **addrp) {
3524 dns_adbaddrinfo_t *addr;
3525 dns_adbentry_t *entry;
3528 isc_boolean_t want_check_exit = ISC_FALSE;
3529 isc_boolean_t overmem;
3531 REQUIRE(DNS_ADB_VALID(adb));
3532 REQUIRE(addrp != NULL);
3534 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3535 entry = addr->entry;
3536 REQUIRE(DNS_ADBENTRY_VALID(entry));
3539 overmem = isc_mem_isovermem(adb->mctx);
3541 bucket = addr->entry->lock_bucket;
3542 LOCK(&adb->entrylocks[bucket]);
3544 if (entry->expires == 0) {
3545 isc_stdtime_get(&now);
3546 entry->expires = now + ADB_ENTRY_WINDOW;
3549 want_check_exit = dec_entry_refcnt(adb, overmem, entry, ISC_FALSE);
3551 UNLOCK(&adb->entrylocks[bucket]);
3554 free_adbaddrinfo(adb, &addr);
3556 if (want_check_exit) {
3564 dns_adb_flush(dns_adb_t *adb) {
3567 INSIST(DNS_ADB_VALID(adb));
3572 * Call our cleanup routines.
3574 for (i = 0; i < NBUCKETS; i++)
3575 RUNTIME_CHECK(cleanup_names(adb, i, INT_MAX) == ISC_FALSE);
3576 for (i = 0; i < NBUCKETS; i++)
3577 RUNTIME_CHECK(cleanup_entries(adb, i, INT_MAX) == ISC_FALSE);
3579 #ifdef DUMP_ADB_AFTER_CLEANING
3580 dump_adb(adb, stdout, ISC_TRUE, INT_MAX);
3587 dns_adb_flushname(dns_adb_t *adb, dns_name_t *name) {
3588 dns_adbname_t *adbname;
3589 dns_adbname_t *nextname;
3592 INSIST(DNS_ADB_VALID(adb));
3595 bucket = dns_name_hash(name, ISC_FALSE) % NBUCKETS;
3596 LOCK(&adb->namelocks[bucket]);
3597 adbname = ISC_LIST_HEAD(adb->names[bucket]);
3598 while (adbname != NULL) {
3599 nextname = ISC_LIST_NEXT(adbname, plink);
3600 if (!NAME_DEAD(adbname) &&
3601 dns_name_equal(name, &adbname->name)) {
3602 RUNTIME_CHECK(kill_name(&adbname,
3603 DNS_EVENT_ADBCANCELED) ==
3608 UNLOCK(&adb->namelocks[bucket]);
3613 water(void *arg, int mark) {
3615 * We're going to change the way to handle overmem condition: use
3616 * isc_mem_isovermem() instead of storing the state via this callback,
3617 * since the latter way tends to cause race conditions.
3618 * To minimize the change, and in case we re-enable the callback
3619 * approach, however, keep this function at the moment.
3622 dns_adb_t *adb = arg;
3623 isc_boolean_t overmem = ISC_TF(mark == ISC_MEM_HIWATER);
3625 REQUIRE(DNS_ADB_VALID(adb));
3627 DP(ISC_LOG_DEBUG(1),
3628 "adb reached %s water mark", overmem ? "high" : "low");
3632 dns_adb_setadbsize(dns_adb_t *adb, isc_uint32_t size) {
3633 isc_uint32_t hiwater;
3634 isc_uint32_t lowater;
3636 INSIST(DNS_ADB_VALID(adb));
3638 if (size != 0 && size < DNS_ADB_MINADBSIZE)
3639 size = DNS_ADB_MINADBSIZE;
3641 hiwater = size - (size >> 3); /* Approximately 7/8ths. */
3642 lowater = size - (size >> 2); /* Approximately 3/4ths. */
3644 if (size == 0 || hiwater == 0 || lowater == 0)
3645 isc_mem_setwater(adb->mctx, water, adb, 0, 0);
3647 isc_mem_setwater(adb->mctx, water, adb, hiwater, lowater);