2 * Copyright (C) 2004-2010 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: adb.c,v 1.243.42.4.24.2 2010/08/12 23:46:24 tbox 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;
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;
598 rdataset->ttl = ttlclamp(rdataset->ttl);
600 if (rdtype == dns_rdatatype_a) {
601 DP(NCACHE_LEVEL, "expire_v4 set to MIN(%u,%u) import_rdataset",
602 adbname->expire_v4, now + rdataset->ttl);
603 adbname->expire_v4 = ISC_MIN(adbname->expire_v4,
604 now + rdataset->ttl);
606 DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) import_rdataset",
607 adbname->expire_v6, now + rdataset->ttl);
608 adbname->expire_v6 = ISC_MIN(adbname->expire_v6,
609 now + rdataset->ttl);
612 if (new_addresses_added) {
614 * Lie a little here. This is more or less so code that cares
615 * can find out if any new information was added or not.
617 return (ISC_R_SUCCESS);
624 * Requires the name's bucket be locked.
627 kill_name(dns_adbname_t **n, isc_eventtype_t ev) {
629 isc_boolean_t result = ISC_FALSE;
630 isc_boolean_t result4, result6;
637 INSIST(DNS_ADBNAME_VALID(name));
639 INSIST(DNS_ADB_VALID(adb));
641 DP(DEF_LEVEL, "killing name %p", name);
644 * If we're dead already, just check to see if we should go
647 if (NAME_DEAD(name) && !NAME_FETCH(name)) {
648 result = unlink_name(adb, name);
649 free_adbname(adb, &name);
651 result = dec_adb_irefcnt(adb);
656 * Clean up the name's various lists. These two are destructive
657 * in that they will always empty the list.
659 clean_finds_at_name(name, ev, DNS_ADBFIND_ADDRESSMASK);
660 result4 = clean_namehooks(adb, &name->v4);
661 result6 = clean_namehooks(adb, &name->v6);
662 clean_target(adb, &name->target);
663 result = ISC_TF(result4 || result6);
666 * If fetches are running, cancel them. If none are running, we can
667 * just kill the name here.
669 if (!NAME_FETCH(name)) {
670 INSIST(result == ISC_FALSE);
671 result = unlink_name(adb, name);
672 free_adbname(adb, &name);
674 result = dec_adb_irefcnt(adb);
676 cancel_fetches_at_name(name);
677 if (!NAME_DEAD(name)) {
678 bucket = name->lock_bucket;
679 ISC_LIST_UNLINK(adb->names[bucket], name, plink);
680 ISC_LIST_APPEND(adb->deadnames[bucket], name, plink);
681 name->flags |= NAME_IS_DEAD;
688 * Requires the name's bucket be locked and no entry buckets be locked.
691 check_expire_namehooks(dns_adbname_t *name, isc_stdtime_t now) {
693 isc_boolean_t result4 = ISC_FALSE;
694 isc_boolean_t result6 = ISC_FALSE;
696 INSIST(DNS_ADBNAME_VALID(name));
698 INSIST(DNS_ADB_VALID(adb));
701 * Check to see if we need to remove the v4 addresses
703 if (!NAME_FETCH_V4(name) && EXPIRE_OK(name->expire_v4, now)) {
704 if (NAME_HAS_V4(name)) {
705 DP(DEF_LEVEL, "expiring v4 for name %p", name);
706 result4 = clean_namehooks(adb, &name->v4);
707 name->partial_result &= ~DNS_ADBFIND_INET;
709 name->expire_v4 = INT_MAX;
710 name->fetch_err = FIND_ERR_UNEXPECTED;
714 * Check to see if we need to remove the v6 addresses
716 if (!NAME_FETCH_V6(name) && EXPIRE_OK(name->expire_v6, now)) {
717 if (NAME_HAS_V6(name)) {
718 DP(DEF_LEVEL, "expiring v6 for name %p", name);
719 result6 = clean_namehooks(adb, &name->v6);
720 name->partial_result &= ~DNS_ADBFIND_INET6;
722 name->expire_v6 = INT_MAX;
723 name->fetch6_err = FIND_ERR_UNEXPECTED;
727 * Check to see if we need to remove the alias target.
729 if (EXPIRE_OK(name->expire_target, now)) {
730 clean_target(adb, &name->target);
731 name->expire_target = INT_MAX;
733 return (ISC_TF(result4 || result6));
737 * Requires the name's bucket be locked.
740 link_name(dns_adb_t *adb, int bucket, dns_adbname_t *name) {
741 INSIST(name->lock_bucket == DNS_ADB_INVALIDBUCKET);
743 ISC_LIST_PREPEND(adb->names[bucket], name, plink);
744 name->lock_bucket = bucket;
745 adb->name_refcnt[bucket]++;
749 * Requires the name's bucket be locked.
751 static inline isc_boolean_t
752 unlink_name(dns_adb_t *adb, dns_adbname_t *name) {
754 isc_boolean_t result = ISC_FALSE;
756 bucket = name->lock_bucket;
757 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
760 ISC_LIST_UNLINK(adb->deadnames[bucket], name, plink);
762 ISC_LIST_UNLINK(adb->names[bucket], name, plink);
763 name->lock_bucket = DNS_ADB_INVALIDBUCKET;
764 INSIST(adb->name_refcnt[bucket] > 0);
765 adb->name_refcnt[bucket]--;
766 if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
772 * Requires the entry's bucket be locked.
775 link_entry(dns_adb_t *adb, int bucket, dns_adbentry_t *entry) {
779 if (isc_mem_isovermem(adb->mctx)) {
780 for (i = 0; i < 2; i++) {
781 e = ISC_LIST_TAIL(adb->entries[bucket]);
784 if (e->refcnt == 0) {
785 unlink_entry(adb, e);
786 free_adbentry(adb, &e);
789 INSIST((e->flags & ENTRY_IS_DEAD) == 0);
790 e->flags |= ENTRY_IS_DEAD;
791 ISC_LIST_UNLINK(adb->entries[bucket], e, plink);
792 ISC_LIST_PREPEND(adb->deadentries[bucket], e, plink);
796 ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
797 entry->lock_bucket = bucket;
798 adb->entry_refcnt[bucket]++;
802 * Requires the entry's bucket be locked.
804 static inline isc_boolean_t
805 unlink_entry(dns_adb_t *adb, dns_adbentry_t *entry) {
807 isc_boolean_t result = ISC_FALSE;
809 bucket = entry->lock_bucket;
810 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
812 if ((entry->flags & ENTRY_IS_DEAD) != 0)
813 ISC_LIST_UNLINK(adb->deadentries[bucket], entry, plink);
815 ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
816 entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
817 INSIST(adb->entry_refcnt[bucket] > 0);
818 adb->entry_refcnt[bucket]--;
819 if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
825 violate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want) {
826 if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
834 * The ADB _MUST_ be locked before calling. Also, exit conditions must be
835 * checked after calling this function.
838 shutdown_names(dns_adb_t *adb) {
840 isc_boolean_t result = ISC_FALSE;
842 dns_adbname_t *next_name;
844 for (bucket = 0; bucket < NBUCKETS; bucket++) {
845 LOCK(&adb->namelocks[bucket]);
846 adb->name_sd[bucket] = ISC_TRUE;
848 name = ISC_LIST_HEAD(adb->names[bucket]);
851 * This bucket has no names. We must decrement the
852 * irefcnt ourselves, since it will not be
853 * automatically triggered by a name being unlinked.
855 INSIST(result == ISC_FALSE);
856 result = dec_adb_irefcnt(adb);
859 * Run through the list. For each name, clean up finds
860 * found there, and cancel any fetches running. When
861 * all the fetches are canceled, the name will destroy
864 while (name != NULL) {
865 next_name = ISC_LIST_NEXT(name, plink);
866 INSIST(result == ISC_FALSE);
867 result = kill_name(&name,
868 DNS_EVENT_ADBSHUTDOWN);
873 UNLOCK(&adb->namelocks[bucket]);
879 * The ADB _MUST_ be locked before calling. Also, exit conditions must be
880 * checked after calling this function.
883 shutdown_entries(dns_adb_t *adb) {
885 isc_boolean_t result = ISC_FALSE;
886 dns_adbentry_t *entry;
887 dns_adbentry_t *next_entry;
889 for (bucket = 0; bucket < NBUCKETS; bucket++) {
890 LOCK(&adb->entrylocks[bucket]);
891 adb->entry_sd[bucket] = ISC_TRUE;
893 entry = ISC_LIST_HEAD(adb->entries[bucket]);
894 if (adb->entry_refcnt[bucket] == 0) {
896 * This bucket has no entries. We must decrement the
897 * irefcnt ourselves, since it will not be
898 * automatically triggered by an entry being unlinked.
900 result = dec_adb_irefcnt(adb);
903 * Run through the list. Cleanup any entries not
904 * associated with names, and which are not in use.
906 while (entry != NULL) {
907 next_entry = ISC_LIST_NEXT(entry, plink);
908 if (entry->refcnt == 0 &&
909 entry->expires != 0) {
910 result = unlink_entry(adb, entry);
911 free_adbentry(adb, &entry);
913 result = dec_adb_irefcnt(adb);
919 UNLOCK(&adb->entrylocks[bucket]);
925 * Name bucket must be locked
928 cancel_fetches_at_name(dns_adbname_t *name) {
929 if (NAME_FETCH_A(name))
930 dns_resolver_cancelfetch(name->fetch_a->fetch);
932 if (NAME_FETCH_AAAA(name))
933 dns_resolver_cancelfetch(name->fetch_aaaa->fetch);
937 * Assumes the name bucket is locked.
940 clean_namehooks(dns_adb_t *adb, dns_adbnamehooklist_t *namehooks) {
941 dns_adbentry_t *entry;
942 dns_adbnamehook_t *namehook;
944 isc_boolean_t result = ISC_FALSE;
945 isc_boolean_t overmem = isc_mem_isovermem(adb->mctx);
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, overmem, entry,
973 namehook->entry = NULL;
974 ISC_LIST_UNLINK(*namehooks, namehook, plink);
975 free_adbnamehook(adb, &namehook);
977 namehook = ISC_LIST_HEAD(*namehooks);
980 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
981 UNLOCK(&adb->entrylocks[addr_bucket]);
986 clean_target(dns_adb_t *adb, dns_name_t *target) {
987 if (dns_name_countlabels(target) > 0) {
988 dns_name_free(target, adb->mctx);
989 dns_name_init(target, NULL);
994 set_target(dns_adb_t *adb, dns_name_t *name, dns_name_t *fname,
995 dns_rdataset_t *rdataset, dns_name_t *target)
998 dns_namereln_t namereln;
999 unsigned int nlabels;
1001 dns_rdata_t rdata = DNS_RDATA_INIT;
1002 dns_fixedname_t fixed1, fixed2;
1003 dns_name_t *prefix, *new_target;
1005 REQUIRE(dns_name_countlabels(target) == 0);
1007 if (rdataset->type == dns_rdatatype_cname) {
1008 dns_rdata_cname_t cname;
1011 * Copy the CNAME's target into the target name.
1013 result = dns_rdataset_first(rdataset);
1014 if (result != ISC_R_SUCCESS)
1016 dns_rdataset_current(rdataset, &rdata);
1017 result = dns_rdata_tostruct(&rdata, &cname, NULL);
1018 if (result != ISC_R_SUCCESS)
1020 result = dns_name_dup(&cname.cname, adb->mctx, target);
1021 dns_rdata_freestruct(&cname);
1022 if (result != ISC_R_SUCCESS)
1025 dns_rdata_dname_t dname;
1027 INSIST(rdataset->type == dns_rdatatype_dname);
1028 namereln = dns_name_fullcompare(name, fname, &order, &nlabels);
1029 INSIST(namereln == dns_namereln_subdomain);
1031 * Get the target name of the DNAME.
1033 result = dns_rdataset_first(rdataset);
1034 if (result != ISC_R_SUCCESS)
1036 dns_rdataset_current(rdataset, &rdata);
1037 result = dns_rdata_tostruct(&rdata, &dname, NULL);
1038 if (result != ISC_R_SUCCESS)
1041 * Construct the new target name.
1043 dns_fixedname_init(&fixed1);
1044 prefix = dns_fixedname_name(&fixed1);
1045 dns_fixedname_init(&fixed2);
1046 new_target = dns_fixedname_name(&fixed2);
1047 dns_name_split(name, nlabels, prefix, NULL);
1048 result = dns_name_concatenate(prefix, &dname.dname, new_target,
1050 dns_rdata_freestruct(&dname);
1051 if (result != ISC_R_SUCCESS)
1053 result = dns_name_dup(new_target, adb->mctx, target);
1054 if (result != ISC_R_SUCCESS)
1058 return (ISC_R_SUCCESS);
1062 * Assumes nothing is locked, since this is called by the client.
1065 event_free(isc_event_t *event) {
1066 dns_adbfind_t *find;
1068 INSIST(event != NULL);
1069 find = event->ev_destroy_arg;
1070 INSIST(DNS_ADBFIND_VALID(find));
1073 find->flags |= FIND_EVENT_FREED;
1074 event->ev_destroy_arg = NULL;
1075 UNLOCK(&find->lock);
1079 * Assumes the name bucket is locked.
1082 clean_finds_at_name(dns_adbname_t *name, isc_eventtype_t evtype,
1087 dns_adbfind_t *find;
1088 dns_adbfind_t *next_find;
1089 isc_boolean_t process;
1090 unsigned int wanted, notify;
1093 "ENTER clean_finds_at_name, name %p, evtype %08x, addrs %08x",
1094 name, evtype, addrs);
1096 find = ISC_LIST_HEAD(name->finds);
1097 while (find != NULL) {
1099 next_find = ISC_LIST_NEXT(find, plink);
1101 process = ISC_FALSE;
1102 wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
1103 notify = wanted & addrs;
1106 case DNS_EVENT_ADBMOREADDRESSES:
1107 DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBMOREADDRESSES");
1108 if ((notify) != 0) {
1109 find->flags &= ~addrs;
1113 case DNS_EVENT_ADBNOMOREADDRESSES:
1114 DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBNOMOREADDRESSES");
1115 find->flags &= ~addrs;
1116 wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
1121 find->flags &= ~addrs;
1126 DP(DEF_LEVEL, "cfan: processing find %p", find);
1128 * Unlink the find from the name, letting the caller
1129 * call dns_adb_destroyfind() on it to clean it up
1132 ISC_LIST_UNLINK(name->finds, find, plink);
1133 find->adbname = NULL;
1134 find->name_bucket = DNS_ADB_INVALIDBUCKET;
1136 INSIST(!FIND_EVENTSENT(find));
1139 task = ev->ev_sender;
1140 ev->ev_sender = find;
1141 find->result_v4 = find_err_map[name->fetch_err];
1142 find->result_v6 = find_err_map[name->fetch6_err];
1143 ev->ev_type = evtype;
1144 ev->ev_destroy = event_free;
1145 ev->ev_destroy_arg = find;
1148 "sending event %p to task %p for find %p",
1151 isc_task_sendanddetach(&task, (isc_event_t **)&ev);
1153 DP(DEF_LEVEL, "cfan: skipping find %p", find);
1156 UNLOCK(&find->lock);
1160 DP(ENTER_LEVEL, "EXIT clean_finds_at_name, name %p", name);
1164 check_exit(dns_adb_t *adb) {
1167 * The caller must be holding the adb lock.
1169 if (adb->shutting_down) {
1171 * If there aren't any external references either, we're
1172 * done. Send the control event to initiate shutdown.
1174 INSIST(!adb->cevent_sent); /* Sanity check. */
1175 event = &adb->cevent;
1176 isc_task_send(adb->task, &event);
1177 adb->cevent_sent = ISC_TRUE;
1181 static inline isc_boolean_t
1182 dec_adb_irefcnt(dns_adb_t *adb) {
1185 isc_boolean_t result = ISC_FALSE;
1187 LOCK(&adb->reflock);
1189 INSIST(adb->irefcnt > 0);
1192 if (adb->irefcnt == 0) {
1193 event = ISC_LIST_HEAD(adb->whenshutdown);
1194 while (event != NULL) {
1195 ISC_LIST_UNLINK(adb->whenshutdown, event, ev_link);
1196 etask = event->ev_sender;
1197 event->ev_sender = adb;
1198 isc_task_sendanddetach(&etask, &event);
1199 event = ISC_LIST_HEAD(adb->whenshutdown);
1203 if (adb->irefcnt == 0 && adb->erefcnt == 0)
1205 UNLOCK(&adb->reflock);
1210 inc_adb_irefcnt(dns_adb_t *adb) {
1211 LOCK(&adb->reflock);
1213 UNLOCK(&adb->reflock);
1217 inc_adb_erefcnt(dns_adb_t *adb) {
1218 LOCK(&adb->reflock);
1220 UNLOCK(&adb->reflock);
1224 inc_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
1227 bucket = entry->lock_bucket;
1230 LOCK(&adb->entrylocks[bucket]);
1235 UNLOCK(&adb->entrylocks[bucket]);
1238 static inline isc_boolean_t
1239 dec_entry_refcnt(dns_adb_t *adb, isc_boolean_t overmem, dns_adbentry_t *entry,
1243 isc_boolean_t destroy_entry;
1244 isc_boolean_t result = ISC_FALSE;
1246 bucket = entry->lock_bucket;
1249 LOCK(&adb->entrylocks[bucket]);
1251 INSIST(entry->refcnt > 0);
1254 destroy_entry = ISC_FALSE;
1255 if (entry->refcnt == 0 &&
1256 (adb->entry_sd[bucket] || entry->expires == 0 || overmem ||
1257 (entry->flags & ENTRY_IS_DEAD) != 0)) {
1258 destroy_entry = ISC_TRUE;
1259 result = unlink_entry(adb, entry);
1263 UNLOCK(&adb->entrylocks[bucket]);
1268 entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
1270 free_adbentry(adb, &entry);
1272 result = dec_adb_irefcnt(adb);
1277 static inline dns_adbname_t *
1278 new_adbname(dns_adb_t *adb, dns_name_t *dnsname) {
1279 dns_adbname_t *name;
1281 name = isc_mempool_get(adb->nmp);
1285 dns_name_init(&name->name, NULL);
1286 if (dns_name_dup(dnsname, adb->mctx, &name->name) != ISC_R_SUCCESS) {
1287 isc_mempool_put(adb->nmp, name);
1290 dns_name_init(&name->target, NULL);
1291 name->magic = DNS_ADBNAME_MAGIC;
1293 name->partial_result = 0;
1295 name->expire_v4 = INT_MAX;
1296 name->expire_v6 = INT_MAX;
1297 name->expire_target = INT_MAX;
1299 name->lock_bucket = DNS_ADB_INVALIDBUCKET;
1300 ISC_LIST_INIT(name->v4);
1301 ISC_LIST_INIT(name->v6);
1302 name->fetch_a = NULL;
1303 name->fetch_aaaa = NULL;
1304 name->fetch_err = FIND_ERR_UNEXPECTED;
1305 name->fetch6_err = FIND_ERR_UNEXPECTED;
1306 ISC_LIST_INIT(name->finds);
1307 ISC_LINK_INIT(name, plink);
1313 free_adbname(dns_adb_t *adb, dns_adbname_t **name) {
1316 INSIST(name != NULL && DNS_ADBNAME_VALID(*name));
1320 INSIST(!NAME_HAS_V4(n));
1321 INSIST(!NAME_HAS_V6(n));
1322 INSIST(!NAME_FETCH(n));
1323 INSIST(ISC_LIST_EMPTY(n->finds));
1324 INSIST(!ISC_LINK_LINKED(n, plink));
1325 INSIST(n->lock_bucket == DNS_ADB_INVALIDBUCKET);
1326 INSIST(n->adb == adb);
1329 dns_name_free(&n->name, adb->mctx);
1331 isc_mempool_put(adb->nmp, n);
1334 static inline dns_adbnamehook_t *
1335 new_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry) {
1336 dns_adbnamehook_t *nh;
1338 nh = isc_mempool_get(adb->nhmp);
1342 nh->magic = DNS_ADBNAMEHOOK_MAGIC;
1344 ISC_LINK_INIT(nh, plink);
1350 free_adbnamehook(dns_adb_t *adb, dns_adbnamehook_t **namehook) {
1351 dns_adbnamehook_t *nh;
1353 INSIST(namehook != NULL && DNS_ADBNAMEHOOK_VALID(*namehook));
1357 INSIST(nh->entry == NULL);
1358 INSIST(!ISC_LINK_LINKED(nh, plink));
1361 isc_mempool_put(adb->nhmp, nh);
1364 static inline dns_adblameinfo_t *
1365 new_adblameinfo(dns_adb_t *adb, dns_name_t *qname, dns_rdatatype_t qtype) {
1366 dns_adblameinfo_t *li;
1368 li = isc_mempool_get(adb->limp);
1372 dns_name_init(&li->qname, NULL);
1373 if (dns_name_dup(qname, adb->mctx, &li->qname) != ISC_R_SUCCESS) {
1374 isc_mempool_put(adb->limp, li);
1377 li->magic = DNS_ADBLAMEINFO_MAGIC;
1380 ISC_LINK_INIT(li, plink);
1386 free_adblameinfo(dns_adb_t *adb, dns_adblameinfo_t **lameinfo) {
1387 dns_adblameinfo_t *li;
1389 INSIST(lameinfo != NULL && DNS_ADBLAMEINFO_VALID(*lameinfo));
1393 INSIST(!ISC_LINK_LINKED(li, plink));
1395 dns_name_free(&li->qname, adb->mctx);
1399 isc_mempool_put(adb->limp, li);
1402 static inline dns_adbentry_t *
1403 new_adbentry(dns_adb_t *adb) {
1407 e = isc_mempool_get(adb->emp);
1411 e->magic = DNS_ADBENTRY_MAGIC;
1412 e->lock_bucket = DNS_ADB_INVALIDBUCKET;
1416 e->srtt = (r & 0x1f) + 1;
1418 ISC_LIST_INIT(e->lameinfo);
1419 ISC_LINK_INIT(e, plink);
1425 free_adbentry(dns_adb_t *adb, dns_adbentry_t **entry) {
1427 dns_adblameinfo_t *li;
1429 INSIST(entry != NULL && DNS_ADBENTRY_VALID(*entry));
1433 INSIST(e->lock_bucket == DNS_ADB_INVALIDBUCKET);
1434 INSIST(e->refcnt == 0);
1435 INSIST(!ISC_LINK_LINKED(e, plink));
1439 li = ISC_LIST_HEAD(e->lameinfo);
1440 while (li != NULL) {
1441 ISC_LIST_UNLINK(e->lameinfo, li, plink);
1442 free_adblameinfo(adb, &li);
1443 li = ISC_LIST_HEAD(e->lameinfo);
1446 isc_mempool_put(adb->emp, e);
1449 static inline dns_adbfind_t *
1450 new_adbfind(dns_adb_t *adb) {
1452 isc_result_t result;
1454 h = isc_mempool_get(adb->ahmp);
1463 h->partial_result = 0;
1466 h->result_v4 = ISC_R_UNEXPECTED;
1467 h->result_v6 = ISC_R_UNEXPECTED;
1468 ISC_LINK_INIT(h, publink);
1469 ISC_LINK_INIT(h, plink);
1470 ISC_LIST_INIT(h->list);
1472 h->name_bucket = DNS_ADB_INVALIDBUCKET;
1477 result = isc_mutex_init(&h->lock);
1478 if (result != ISC_R_SUCCESS) {
1479 isc_mempool_put(adb->ahmp, h);
1483 ISC_EVENT_INIT(&h->event, sizeof(isc_event_t), 0, 0, 0, NULL, NULL,
1486 inc_adb_irefcnt(adb);
1487 h->magic = DNS_ADBFIND_MAGIC;
1491 static inline dns_adbfetch_t *
1492 new_adbfetch(dns_adb_t *adb) {
1495 f = isc_mempool_get(adb->afmp);
1502 dns_rdataset_init(&f->rdataset);
1504 f->magic = DNS_ADBFETCH_MAGIC;
1510 free_adbfetch(dns_adb_t *adb, dns_adbfetch_t **fetch) {
1513 INSIST(fetch != NULL && DNS_ADBFETCH_VALID(*fetch));
1519 if (dns_rdataset_isassociated(&f->rdataset))
1520 dns_rdataset_disassociate(&f->rdataset);
1522 isc_mempool_put(adb->afmp, f);
1525 static inline isc_boolean_t
1526 free_adbfind(dns_adb_t *adb, dns_adbfind_t **findp) {
1527 dns_adbfind_t *find;
1529 INSIST(findp != NULL && DNS_ADBFIND_VALID(*findp));
1533 INSIST(!FIND_HAS_ADDRS(find));
1534 INSIST(!ISC_LINK_LINKED(find, publink));
1535 INSIST(!ISC_LINK_LINKED(find, plink));
1536 INSIST(find->name_bucket == DNS_ADB_INVALIDBUCKET);
1537 INSIST(find->adbname == NULL);
1541 DESTROYLOCK(&find->lock);
1542 isc_mempool_put(adb->ahmp, find);
1543 return (dec_adb_irefcnt(adb));
1547 * Copy bits from the entry into the newly allocated addrinfo. The entry
1548 * must be locked, and the reference count must be bumped up by one
1549 * if this function returns a valid pointer.
1551 static inline dns_adbaddrinfo_t *
1552 new_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry, in_port_t port) {
1553 dns_adbaddrinfo_t *ai;
1555 ai = isc_mempool_get(adb->aimp);
1559 ai->magic = DNS_ADBADDRINFO_MAGIC;
1560 ai->sockaddr = entry->sockaddr;
1561 isc_sockaddr_setport(&ai->sockaddr, port);
1562 ai->srtt = entry->srtt;
1563 ai->flags = entry->flags;
1565 ISC_LINK_INIT(ai, publink);
1571 free_adbaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **ainfo) {
1572 dns_adbaddrinfo_t *ai;
1574 INSIST(ainfo != NULL && DNS_ADBADDRINFO_VALID(*ainfo));
1578 INSIST(ai->entry == NULL);
1579 INSIST(!ISC_LINK_LINKED(ai, publink));
1583 isc_mempool_put(adb->aimp, ai);
1587 * Search for the name. NOTE: The bucket is kept locked on both
1588 * success and failure, so it must always be unlocked by the caller!
1590 * On the first call to this function, *bucketp must be set to
1591 * DNS_ADB_INVALIDBUCKET.
1593 static inline dns_adbname_t *
1594 find_name_and_lock(dns_adb_t *adb, dns_name_t *name,
1595 unsigned int options, int *bucketp)
1597 dns_adbname_t *adbname;
1600 bucket = dns_name_fullhash(name, ISC_FALSE) % NBUCKETS;
1602 if (*bucketp == DNS_ADB_INVALIDBUCKET) {
1603 LOCK(&adb->namelocks[bucket]);
1605 } else if (*bucketp != bucket) {
1606 UNLOCK(&adb->namelocks[*bucketp]);
1607 LOCK(&adb->namelocks[bucket]);
1611 adbname = ISC_LIST_HEAD(adb->names[bucket]);
1612 while (adbname != NULL) {
1613 if (!NAME_DEAD(adbname)) {
1614 if (dns_name_equal(name, &adbname->name)
1615 && GLUEHINT_OK(adbname, options)
1616 && STARTATZONE_MATCHES(adbname, options))
1619 adbname = ISC_LIST_NEXT(adbname, plink);
1626 * Search for the address. NOTE: The bucket is kept locked on both
1627 * success and failure, so it must always be unlocked by the caller.
1629 * On the first call to this function, *bucketp must be set to
1630 * DNS_ADB_INVALIDBUCKET. This will cause a lock to occur. On
1631 * later calls (within the same "lock path") it can be left alone, so
1632 * if this function is called multiple times locking is only done if
1633 * the bucket changes.
1635 static inline dns_adbentry_t *
1636 find_entry_and_lock(dns_adb_t *adb, isc_sockaddr_t *addr, int *bucketp,
1639 dns_adbentry_t *entry, *entry_next;
1642 bucket = isc_sockaddr_hash(addr, ISC_TRUE) % NBUCKETS;
1644 if (*bucketp == DNS_ADB_INVALIDBUCKET) {
1645 LOCK(&adb->entrylocks[bucket]);
1647 } else if (*bucketp != bucket) {
1648 UNLOCK(&adb->entrylocks[*bucketp]);
1649 LOCK(&adb->entrylocks[bucket]);
1653 /* Search the list, while cleaning up expired entries. */
1654 for (entry = ISC_LIST_HEAD(adb->entries[bucket]);
1656 entry = entry_next) {
1657 entry_next = ISC_LIST_NEXT(entry, plink);
1658 (void)check_expire_entry(adb, &entry, now);
1659 if (entry != NULL &&
1660 isc_sockaddr_equal(addr, &entry->sockaddr)) {
1661 ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
1662 ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
1671 * Entry bucket MUST be locked!
1673 static isc_boolean_t
1674 entry_is_lame(dns_adb_t *adb, dns_adbentry_t *entry, dns_name_t *qname,
1675 dns_rdatatype_t qtype, isc_stdtime_t now)
1677 dns_adblameinfo_t *li, *next_li;
1678 isc_boolean_t is_bad;
1682 li = ISC_LIST_HEAD(entry->lameinfo);
1685 while (li != NULL) {
1686 next_li = ISC_LIST_NEXT(li, plink);
1689 * Has the entry expired?
1691 if (li->lame_timer < now) {
1692 ISC_LIST_UNLINK(entry->lameinfo, li, plink);
1693 free_adblameinfo(adb, &li);
1697 * Order tests from least to most expensive.
1699 * We do not break out of the main loop here as
1700 * we use the loop for house keeping.
1702 if (li != NULL && !is_bad && li->qtype == qtype &&
1703 dns_name_equal(qname, &li->qname))
1713 copy_namehook_lists(dns_adb_t *adb, dns_adbfind_t *find, dns_name_t *qname,
1714 dns_rdatatype_t qtype, dns_adbname_t *name,
1717 dns_adbnamehook_t *namehook;
1718 dns_adbaddrinfo_t *addrinfo;
1719 dns_adbentry_t *entry;
1722 bucket = DNS_ADB_INVALIDBUCKET;
1724 if (find->options & DNS_ADBFIND_INET) {
1725 namehook = ISC_LIST_HEAD(name->v4);
1726 while (namehook != NULL) {
1727 entry = namehook->entry;
1728 bucket = entry->lock_bucket;
1729 LOCK(&adb->entrylocks[bucket]);
1731 if (!FIND_RETURNLAME(find)
1732 && entry_is_lame(adb, entry, qname, qtype, now)) {
1733 find->options |= DNS_ADBFIND_LAMEPRUNED;
1736 addrinfo = new_adbaddrinfo(adb, entry, find->port);
1737 if (addrinfo == NULL) {
1738 find->partial_result |= DNS_ADBFIND_INET;
1742 * Found a valid entry. Add it to the find's list.
1744 inc_entry_refcnt(adb, entry, ISC_FALSE);
1745 ISC_LIST_APPEND(find->list, addrinfo, publink);
1748 UNLOCK(&adb->entrylocks[bucket]);
1749 bucket = DNS_ADB_INVALIDBUCKET;
1750 namehook = ISC_LIST_NEXT(namehook, plink);
1754 if (find->options & DNS_ADBFIND_INET6) {
1755 namehook = ISC_LIST_HEAD(name->v6);
1756 while (namehook != NULL) {
1757 entry = namehook->entry;
1758 bucket = entry->lock_bucket;
1759 LOCK(&adb->entrylocks[bucket]);
1761 if (!FIND_RETURNLAME(find)
1762 && entry_is_lame(adb, entry, qname, qtype, now)) {
1763 find->options |= DNS_ADBFIND_LAMEPRUNED;
1766 addrinfo = new_adbaddrinfo(adb, entry, find->port);
1767 if (addrinfo == NULL) {
1768 find->partial_result |= DNS_ADBFIND_INET6;
1772 * Found a valid entry. Add it to the find's list.
1774 inc_entry_refcnt(adb, entry, ISC_FALSE);
1775 ISC_LIST_APPEND(find->list, addrinfo, publink);
1778 UNLOCK(&adb->entrylocks[bucket]);
1779 bucket = DNS_ADB_INVALIDBUCKET;
1780 namehook = ISC_LIST_NEXT(namehook, plink);
1785 if (bucket != DNS_ADB_INVALIDBUCKET)
1786 UNLOCK(&adb->entrylocks[bucket]);
1790 shutdown_task(isc_task_t *task, isc_event_t *ev) {
1796 INSIST(DNS_ADB_VALID(adb));
1798 isc_event_free(&ev);
1800 * Wait for lock around check_exit() call to be released.
1808 * Name bucket must be locked; adb may be locked; no other locks held.
1810 static isc_boolean_t
1811 check_expire_name(dns_adbname_t **namep, isc_stdtime_t now) {
1812 dns_adbname_t *name;
1813 isc_boolean_t result = ISC_FALSE;
1815 INSIST(namep != NULL && DNS_ADBNAME_VALID(*namep));
1818 if (NAME_HAS_V4(name) || NAME_HAS_V6(name))
1820 if (NAME_FETCH(name))
1822 if (!EXPIRE_OK(name->expire_v4, now))
1824 if (!EXPIRE_OK(name->expire_v6, now))
1826 if (!EXPIRE_OK(name->expire_target, now))
1830 * The name is empty. Delete it.
1832 result = kill_name(&name, DNS_EVENT_ADBEXPIRED);
1836 * Our caller, or one of its callers, will be calling check_exit() at
1837 * some point, so we don't need to do it here.
1843 * Examine the tail entry of the LRU list to see if it expires or is stale
1844 * (unused for some period); if so, the name entry will be freed. If the ADB
1845 * is in the overmem condition, the tail and the next to tail entries
1846 * will be unconditionally removed (unless they have an outstanding fetch).
1847 * We don't care about a race on 'overmem' at the risk of causing some
1848 * collateral damage or a small delay in starting cleanup, so we don't bother
1849 * to lock ADB (if it's not locked).
1851 * Name bucket must be locked; adb may be locked; no other locks held.
1854 check_stale_name(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
1855 int victims, max_victims;
1856 isc_boolean_t result;
1857 dns_adbname_t *victim, *next_victim;
1858 isc_boolean_t overmem = isc_mem_isovermem(adb->mctx);
1861 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
1863 max_victims = overmem ? 2 : 1;
1866 * We limit the number of scanned entries to 10 (arbitrary choice)
1867 * in order to avoid examining too many entries when there are many
1868 * tail entries that have fetches (this should be rare, but could
1871 victim = ISC_LIST_TAIL(adb->names[bucket]);
1873 victim != NULL && victims < max_victims && scans < 10;
1874 victim = next_victim) {
1875 INSIST(!NAME_DEAD(victim));
1877 next_victim = ISC_LIST_PREV(victim, plink);
1878 result = check_expire_name(&victim, now);
1879 if (victim == NULL) {
1884 if (!NAME_FETCH(victim) &&
1885 (overmem || victim->last_used + ADB_STALE_MARGIN <= now)) {
1886 RUNTIME_CHECK(kill_name(&victim,
1887 DNS_EVENT_ADBCANCELED) ==
1899 * Entry bucket must be locked; adb may be locked; no other locks held.
1901 static isc_boolean_t
1902 check_expire_entry(dns_adb_t *adb, dns_adbentry_t **entryp, isc_stdtime_t now)
1904 dns_adbentry_t *entry;
1905 isc_boolean_t result = ISC_FALSE;
1907 INSIST(entryp != NULL && DNS_ADBENTRY_VALID(*entryp));
1910 if (entry->refcnt != 0)
1913 if (entry->expires == 0 || entry->expires > now)
1917 * The entry is not in use. Delete it.
1919 DP(DEF_LEVEL, "killing entry %p", entry);
1920 INSIST(ISC_LINK_LINKED(entry, plink));
1921 result = unlink_entry(adb, entry);
1922 free_adbentry(adb, &entry);
1924 dec_adb_irefcnt(adb);
1930 * ADB must be locked, and no other locks held.
1932 static isc_boolean_t
1933 cleanup_names(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
1934 dns_adbname_t *name;
1935 dns_adbname_t *next_name;
1936 isc_boolean_t result = ISC_FALSE;
1938 DP(CLEAN_LEVEL, "cleaning name bucket %d", bucket);
1940 LOCK(&adb->namelocks[bucket]);
1941 if (adb->name_sd[bucket]) {
1942 UNLOCK(&adb->namelocks[bucket]);
1946 name = ISC_LIST_HEAD(adb->names[bucket]);
1947 while (name != NULL) {
1948 next_name = ISC_LIST_NEXT(name, plink);
1949 INSIST(result == ISC_FALSE);
1950 result = check_expire_namehooks(name, now);
1952 result = check_expire_name(&name, now);
1955 UNLOCK(&adb->namelocks[bucket]);
1960 * ADB must be locked, and no other locks held.
1962 static isc_boolean_t
1963 cleanup_entries(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
1964 dns_adbentry_t *entry, *next_entry;
1965 isc_boolean_t result = ISC_FALSE;
1967 DP(CLEAN_LEVEL, "cleaning entry bucket %d", bucket);
1969 LOCK(&adb->entrylocks[bucket]);
1970 entry = ISC_LIST_HEAD(adb->entries[bucket]);
1971 while (entry != NULL) {
1972 next_entry = ISC_LIST_NEXT(entry, plink);
1973 INSIST(result == ISC_FALSE);
1974 result = check_expire_entry(adb, &entry, now);
1977 UNLOCK(&adb->entrylocks[bucket]);
1982 destroy(dns_adb_t *adb) {
1985 isc_task_detach(&adb->task);
1987 isc_mempool_destroy(&adb->nmp);
1988 isc_mempool_destroy(&adb->nhmp);
1989 isc_mempool_destroy(&adb->limp);
1990 isc_mempool_destroy(&adb->emp);
1991 isc_mempool_destroy(&adb->ahmp);
1992 isc_mempool_destroy(&adb->aimp);
1993 isc_mempool_destroy(&adb->afmp);
1995 DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
1996 DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
1998 DESTROYLOCK(&adb->reflock);
1999 DESTROYLOCK(&adb->lock);
2000 DESTROYLOCK(&adb->mplock);
2001 DESTROYLOCK(&adb->overmemlock);
2003 isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
2012 dns_adb_create(isc_mem_t *mem, dns_view_t *view, isc_timermgr_t *timermgr,
2013 isc_taskmgr_t *taskmgr, dns_adb_t **newadb)
2016 isc_result_t result;
2019 REQUIRE(mem != NULL);
2020 REQUIRE(view != NULL);
2021 REQUIRE(timermgr != NULL); /* this is actually unused */
2022 REQUIRE(taskmgr != NULL);
2023 REQUIRE(newadb != NULL && *newadb == NULL);
2027 adb = isc_mem_get(mem, sizeof(dns_adb_t));
2029 return (ISC_R_NOMEMORY);
2032 * Initialize things here that cannot fail, and especially things
2033 * that must be NULL for the error return to work properly.
2048 adb->taskmgr = taskmgr;
2049 adb->next_cleanbucket = 0;
2050 ISC_EVENT_INIT(&adb->cevent, sizeof(adb->cevent), 0, NULL,
2051 DNS_EVENT_ADBCONTROL, shutdown_task, adb,
2053 adb->cevent_sent = ISC_FALSE;
2054 adb->shutting_down = ISC_FALSE;
2055 ISC_LIST_INIT(adb->whenshutdown);
2057 isc_mem_attach(mem, &adb->mctx);
2059 result = isc_mutex_init(&adb->lock);
2060 if (result != ISC_R_SUCCESS)
2063 result = isc_mutex_init(&adb->mplock);
2064 if (result != ISC_R_SUCCESS)
2067 result = isc_mutex_init(&adb->reflock);
2068 if (result != ISC_R_SUCCESS)
2071 result = isc_mutex_init(&adb->overmemlock);
2072 if (result != ISC_R_SUCCESS)
2076 * Initialize the bucket locks for names and elements.
2077 * May as well initialize the list heads, too.
2079 result = isc_mutexblock_init(adb->namelocks, NBUCKETS);
2080 if (result != ISC_R_SUCCESS)
2082 for (i = 0; i < NBUCKETS; i++) {
2083 ISC_LIST_INIT(adb->names[i]);
2084 ISC_LIST_INIT(adb->deadnames[i]);
2085 adb->name_sd[i] = ISC_FALSE;
2086 adb->name_refcnt[i] = 0;
2089 for (i = 0; i < NBUCKETS; i++) {
2090 ISC_LIST_INIT(adb->entries[i]);
2091 ISC_LIST_INIT(adb->deadentries[i]);
2092 adb->entry_sd[i] = ISC_FALSE;
2093 adb->entry_refcnt[i] = 0;
2096 result = isc_mutexblock_init(adb->entrylocks, NBUCKETS);
2097 if (result != ISC_R_SUCCESS)
2103 #define MPINIT(t, p, n) do { \
2104 result = isc_mempool_create(mem, sizeof(t), &(p)); \
2105 if (result != ISC_R_SUCCESS) \
2107 isc_mempool_setfreemax((p), FREE_ITEMS); \
2108 isc_mempool_setfillcount((p), FILL_COUNT); \
2109 isc_mempool_setname((p), n); \
2110 isc_mempool_associatelock((p), &adb->mplock); \
2113 MPINIT(dns_adbname_t, adb->nmp, "adbname");
2114 MPINIT(dns_adbnamehook_t, adb->nhmp, "adbnamehook");
2115 MPINIT(dns_adblameinfo_t, adb->limp, "adblameinfo");
2116 MPINIT(dns_adbentry_t, adb->emp, "adbentry");
2117 MPINIT(dns_adbfind_t, adb->ahmp, "adbfind");
2118 MPINIT(dns_adbaddrinfo_t, adb->aimp, "adbaddrinfo");
2119 MPINIT(dns_adbfetch_t, adb->afmp, "adbfetch");
2124 * Allocate an internal task.
2126 result = isc_task_create(adb->taskmgr, 0, &adb->task);
2127 if (result != ISC_R_SUCCESS)
2129 isc_task_setname(adb->task, "ADB", adb);
2134 adb->magic = DNS_ADB_MAGIC;
2136 return (ISC_R_SUCCESS);
2139 if (adb->task != NULL)
2140 isc_task_detach(&adb->task);
2142 /* clean up entrylocks */
2143 DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
2145 fail2: /* clean up namelocks */
2146 DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
2148 fail1: /* clean up only allocated memory */
2149 if (adb->nmp != NULL)
2150 isc_mempool_destroy(&adb->nmp);
2151 if (adb->nhmp != NULL)
2152 isc_mempool_destroy(&adb->nhmp);
2153 if (adb->limp != NULL)
2154 isc_mempool_destroy(&adb->limp);
2155 if (adb->emp != NULL)
2156 isc_mempool_destroy(&adb->emp);
2157 if (adb->ahmp != NULL)
2158 isc_mempool_destroy(&adb->ahmp);
2159 if (adb->aimp != NULL)
2160 isc_mempool_destroy(&adb->aimp);
2161 if (adb->afmp != NULL)
2162 isc_mempool_destroy(&adb->afmp);
2164 DESTROYLOCK(&adb->overmemlock);
2166 DESTROYLOCK(&adb->reflock);
2168 DESTROYLOCK(&adb->mplock);
2170 DESTROYLOCK(&adb->lock);
2172 isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
2178 dns_adb_attach(dns_adb_t *adb, dns_adb_t **adbx) {
2180 REQUIRE(DNS_ADB_VALID(adb));
2181 REQUIRE(adbx != NULL && *adbx == NULL);
2183 inc_adb_erefcnt(adb);
2188 dns_adb_detach(dns_adb_t **adbx) {
2190 isc_boolean_t need_exit_check;
2192 REQUIRE(adbx != NULL && DNS_ADB_VALID(*adbx));
2197 INSIST(adb->erefcnt > 0);
2199 LOCK(&adb->reflock);
2201 need_exit_check = ISC_TF(adb->erefcnt == 0 && adb->irefcnt == 0);
2202 UNLOCK(&adb->reflock);
2204 if (need_exit_check) {
2206 INSIST(adb->shutting_down);
2213 dns_adb_whenshutdown(dns_adb_t *adb, isc_task_t *task, isc_event_t **eventp) {
2216 isc_boolean_t zeroirefcnt = ISC_FALSE;
2219 * Send '*eventp' to 'task' when 'adb' has shutdown.
2222 REQUIRE(DNS_ADB_VALID(adb));
2223 REQUIRE(eventp != NULL);
2230 LOCK(&adb->reflock);
2231 zeroirefcnt = ISC_TF(adb->irefcnt == 0);
2233 if (adb->shutting_down && zeroirefcnt &&
2234 isc_mempool_getallocated(adb->ahmp) == 0) {
2236 * We're already shutdown. Send the event.
2238 event->ev_sender = adb;
2239 isc_task_send(task, &event);
2242 isc_task_attach(task, &clone);
2243 event->ev_sender = clone;
2244 ISC_LIST_APPEND(adb->whenshutdown, event, ev_link);
2247 UNLOCK(&adb->reflock);
2252 dns_adb_shutdown(dns_adb_t *adb) {
2253 isc_boolean_t need_check_exit;
2261 if (!adb->shutting_down) {
2262 adb->shutting_down = ISC_TRUE;
2263 isc_mem_setwater(adb->mctx, water, adb, 0, 0);
2264 need_check_exit = shutdown_names(adb);
2265 if (!need_check_exit)
2266 need_check_exit = shutdown_entries(adb);
2267 if (need_check_exit)
2275 dns_adb_createfind(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
2276 void *arg, dns_name_t *name, dns_name_t *qname,
2277 dns_rdatatype_t qtype, unsigned int options,
2278 isc_stdtime_t now, dns_name_t *target,
2279 in_port_t port, dns_adbfind_t **findp)
2281 dns_adbfind_t *find;
2282 dns_adbname_t *adbname;
2284 isc_boolean_t want_event, start_at_zone, alias, have_address;
2285 isc_result_t result;
2286 unsigned int wanted_addresses;
2287 unsigned int wanted_fetches;
2288 unsigned int query_pending;
2290 REQUIRE(DNS_ADB_VALID(adb));
2292 REQUIRE(action != NULL);
2294 REQUIRE(name != NULL);
2295 REQUIRE(qname != NULL);
2296 REQUIRE(findp != NULL && *findp == NULL);
2297 REQUIRE(target == NULL || dns_name_hasbuffer(target));
2299 REQUIRE((options & DNS_ADBFIND_ADDRESSMASK) != 0);
2301 result = ISC_R_UNEXPECTED;
2302 wanted_addresses = (options & DNS_ADBFIND_ADDRESSMASK);
2305 want_event = ISC_FALSE;
2306 start_at_zone = ISC_FALSE;
2310 isc_stdtime_get(&now);
2313 * XXXMLG Move this comment somewhere else!
2315 * Look up the name in our internal database.
2317 * Possibilities: Note that these are not always exclusive.
2319 * No name found. In this case, allocate a new name header and
2320 * an initial namehook or two. If any of these allocations
2321 * fail, clean up and return ISC_R_NOMEMORY.
2323 * Name found, valid addresses present. Allocate one addrinfo
2324 * structure for each found and append it to the linked list
2325 * of addresses for this header.
2327 * Name found, queries pending. In this case, if a task was
2328 * passed in, allocate a job id, attach it to the name's job
2329 * list and remember to tell the caller that there will be
2330 * more info coming later.
2333 find = new_adbfind(adb);
2335 return (ISC_R_NOMEMORY);
2340 * Remember what types of addresses we are interested in.
2342 find->options = options;
2343 find->flags |= wanted_addresses;
2344 if (FIND_WANTEVENT(find)) {
2345 REQUIRE(task != NULL);
2349 * Try to see if we know anything about this name at all.
2351 bucket = DNS_ADB_INVALIDBUCKET;
2352 adbname = find_name_and_lock(adb, name, find->options, &bucket);
2353 if (adb->name_sd[bucket]) {
2355 "dns_adb_createfind: returning ISC_R_SHUTTINGDOWN");
2356 RUNTIME_CHECK(free_adbfind(adb, &find) == ISC_FALSE);
2357 result = ISC_R_SHUTTINGDOWN;
2362 * Nothing found. Allocate a new adbname structure for this name.
2364 if (adbname == NULL) {
2366 * See if there is any stale name at the end of list, and purge
2369 check_stale_name(adb, bucket, now);
2371 adbname = new_adbname(adb, name);
2372 if (adbname == NULL) {
2373 RUNTIME_CHECK(free_adbfind(adb, &find) == ISC_FALSE);
2374 result = ISC_R_NOMEMORY;
2377 link_name(adb, bucket, adbname);
2378 if (FIND_HINTOK(find))
2379 adbname->flags |= NAME_HINT_OK;
2380 if (FIND_GLUEOK(find))
2381 adbname->flags |= NAME_GLUE_OK;
2382 if (FIND_STARTATZONE(find))
2383 adbname->flags |= NAME_STARTATZONE;
2385 /* Move this name forward in the LRU list */
2386 ISC_LIST_UNLINK(adb->names[bucket], adbname, plink);
2387 ISC_LIST_PREPEND(adb->names[bucket], adbname, plink);
2389 adbname->last_used = now;
2392 * Expire old entries, etc.
2394 RUNTIME_CHECK(check_expire_namehooks(adbname, now) == ISC_FALSE);
2397 * Do we know that the name is an alias?
2399 if (!EXPIRE_OK(adbname->expire_target, now)) {
2404 "dns_adb_createfind: name %p is an alias (cached)",
2411 * Try to populate the name from the database and/or
2412 * start fetches. First try looking for an A record
2415 if (!NAME_HAS_V4(adbname) && EXPIRE_OK(adbname->expire_v4, now)
2416 && WANT_INET(wanted_addresses)) {
2417 result = dbfind_name(adbname, now, dns_rdatatype_a);
2418 if (result == ISC_R_SUCCESS) {
2420 "dns_adb_createfind: found A for name %p in db",
2426 * Did we get a CNAME or DNAME?
2428 if (result == DNS_R_ALIAS) {
2430 "dns_adb_createfind: name %p is an alias",
2437 * If the name doesn't exist at all, don't bother with
2438 * v6 queries; they won't work.
2440 * If the name does exist but we didn't get our data, go
2441 * ahead and try AAAA.
2443 * If the result is neither of these, try a fetch for A.
2445 if (NXDOMAIN_RESULT(result))
2447 else if (NXRRSET_RESULT(result))
2450 if (!NAME_FETCH_V4(adbname))
2451 wanted_fetches |= DNS_ADBFIND_INET;
2455 if (!NAME_HAS_V6(adbname) && EXPIRE_OK(adbname->expire_v6, now)
2456 && WANT_INET6(wanted_addresses)) {
2457 result = dbfind_name(adbname, now, dns_rdatatype_aaaa);
2458 if (result == ISC_R_SUCCESS) {
2460 "dns_adb_createfind: found AAAA for name %p",
2466 * Did we get a CNAME or DNAME?
2468 if (result == DNS_R_ALIAS) {
2470 "dns_adb_createfind: name %p is an alias",
2477 * Listen to negative cache hints, and don't start
2480 if (NCACHE_RESULT(result) || AUTH_NX(result))
2483 if (!NAME_FETCH_V6(adbname))
2484 wanted_fetches |= DNS_ADBFIND_INET6;
2488 if ((WANT_INET(wanted_addresses) && NAME_HAS_V4(adbname)) ||
2489 (WANT_INET6(wanted_addresses) && NAME_HAS_V6(adbname)))
2490 have_address = ISC_TRUE;
2492 have_address = ISC_FALSE;
2493 if (wanted_fetches != 0 &&
2494 ! (FIND_AVOIDFETCHES(find) && have_address)) {
2496 * We're missing at least one address family. Either the
2497 * caller hasn't instructed us to avoid fetches, or we don't
2498 * know anything about any of the address families that would
2499 * be acceptable so we have to launch fetches.
2502 if (FIND_STARTATZONE(find))
2503 start_at_zone = ISC_TRUE;
2508 if (WANT_INET(wanted_fetches) &&
2509 fetch_name(adbname, start_at_zone,
2510 dns_rdatatype_a) == ISC_R_SUCCESS) {
2512 "dns_adb_createfind: started A fetch for name %p",
2519 if (WANT_INET6(wanted_fetches) &&
2520 fetch_name(adbname, start_at_zone,
2521 dns_rdatatype_aaaa) == ISC_R_SUCCESS) {
2523 "dns_adb_createfind: "
2524 "started AAAA fetch for name %p",
2530 * Run through the name and copy out the bits we are
2533 copy_namehook_lists(adb, find, qname, qtype, adbname, now);
2536 if (NAME_FETCH_V4(adbname))
2537 query_pending |= DNS_ADBFIND_INET;
2538 if (NAME_FETCH_V6(adbname))
2539 query_pending |= DNS_ADBFIND_INET6;
2542 * Attach to the name's query list if there are queries
2543 * already running, and we have been asked to.
2545 want_event = ISC_TRUE;
2546 if (!FIND_WANTEVENT(find))
2547 want_event = ISC_FALSE;
2548 if (FIND_WANTEMPTYEVENT(find) && FIND_HAS_ADDRS(find))
2549 want_event = ISC_FALSE;
2550 if ((wanted_addresses & query_pending) == 0)
2551 want_event = ISC_FALSE;
2553 want_event = ISC_FALSE;
2555 find->adbname = adbname;
2556 find->name_bucket = bucket;
2557 ISC_LIST_APPEND(adbname->finds, find, plink);
2558 find->query_pending = (query_pending & wanted_addresses);
2559 find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
2560 find->flags |= (find->query_pending & DNS_ADBFIND_ADDRESSMASK);
2561 DP(DEF_LEVEL, "createfind: attaching find %p to adbname %p",
2565 * Remove the flag so the caller knows there will never
2566 * be an event, and set internal flags to fake that
2567 * the event was sent and freed, so dns_adb_destroyfind() will
2568 * do the right thing.
2570 find->query_pending = (query_pending & wanted_addresses);
2571 find->options &= ~DNS_ADBFIND_WANTEVENT;
2572 find->flags |= (FIND_EVENT_SENT | FIND_EVENT_FREED);
2573 find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
2576 find->partial_result |= (adbname->partial_result & wanted_addresses);
2578 if (target != NULL) {
2579 result = dns_name_copy(&adbname->target, target, NULL);
2580 if (result != ISC_R_SUCCESS)
2583 result = DNS_R_ALIAS;
2585 result = ISC_R_SUCCESS;
2588 * Copy out error flags from the name structure into the find.
2590 find->result_v4 = find_err_map[adbname->fetch_err];
2591 find->result_v6 = find_err_map[adbname->fetch6_err];
2600 INSIST((find->flags & DNS_ADBFIND_ADDRESSMASK) != 0);
2602 isc_task_attach(task, &taskp);
2603 find->event.ev_sender = taskp;
2604 find->event.ev_action = action;
2605 find->event.ev_arg = arg;
2609 UNLOCK(&adb->namelocks[bucket]);
2615 dns_adb_destroyfind(dns_adbfind_t **findp) {
2616 dns_adbfind_t *find;
2617 dns_adbentry_t *entry;
2618 dns_adbaddrinfo_t *ai;
2621 isc_boolean_t overmem;
2623 REQUIRE(findp != NULL && DNS_ADBFIND_VALID(*findp));
2629 DP(DEF_LEVEL, "dns_adb_destroyfind on find %p", find);
2632 REQUIRE(DNS_ADB_VALID(adb));
2634 REQUIRE(FIND_EVENTFREED(find));
2636 bucket = find->name_bucket;
2637 INSIST(bucket == DNS_ADB_INVALIDBUCKET);
2639 UNLOCK(&find->lock);
2642 * The find doesn't exist on any list, and nothing is locked.
2643 * Return the find to the memory pool, and decrement the adb's
2646 overmem = isc_mem_isovermem(adb->mctx);
2647 ai = ISC_LIST_HEAD(find->list);
2648 while (ai != NULL) {
2649 ISC_LIST_UNLINK(find->list, ai, publink);
2652 INSIST(DNS_ADBENTRY_VALID(entry));
2653 RUNTIME_CHECK(dec_entry_refcnt(adb, overmem, entry, ISC_TRUE) ==
2655 free_adbaddrinfo(adb, &ai);
2656 ai = ISC_LIST_HEAD(find->list);
2660 * WARNING: The find is freed with the adb locked. This is done
2661 * to avoid a race condition where we free the find, some other
2662 * thread tests to see if it should be destroyed, detects it should
2663 * be, destroys it, and then we try to lock it for our check, but the
2664 * lock is destroyed.
2667 if (free_adbfind(adb, &find))
2673 dns_adb_cancelfind(dns_adbfind_t *find) {
2682 DP(DEF_LEVEL, "dns_adb_cancelfind on find %p", find);
2685 REQUIRE(DNS_ADB_VALID(adb));
2687 REQUIRE(!FIND_EVENTFREED(find));
2688 REQUIRE(FIND_WANTEVENT(find));
2690 bucket = find->name_bucket;
2691 if (bucket == DNS_ADB_INVALIDBUCKET)
2695 * We need to get the adbname's lock to unlink the find.
2697 unlock_bucket = bucket;
2698 violate_locking_hierarchy(&find->lock, &adb->namelocks[unlock_bucket]);
2699 bucket = find->name_bucket;
2700 if (bucket != DNS_ADB_INVALIDBUCKET) {
2701 ISC_LIST_UNLINK(find->adbname->finds, find, plink);
2702 find->adbname = NULL;
2703 find->name_bucket = DNS_ADB_INVALIDBUCKET;
2705 UNLOCK(&adb->namelocks[unlock_bucket]);
2706 bucket = DNS_ADB_INVALIDBUCKET;
2710 if (!FIND_EVENTSENT(find)) {
2712 task = ev->ev_sender;
2713 ev->ev_sender = find;
2714 ev->ev_type = DNS_EVENT_ADBCANCELED;
2715 ev->ev_destroy = event_free;
2716 ev->ev_destroy_arg = find;
2717 find->result_v4 = ISC_R_CANCELED;
2718 find->result_v6 = ISC_R_CANCELED;
2720 DP(DEF_LEVEL, "sending event %p to task %p for find %p",
2723 isc_task_sendanddetach(&task, (isc_event_t **)&ev);
2726 UNLOCK(&find->lock);
2730 dns_adb_dump(dns_adb_t *adb, FILE *f) {
2734 REQUIRE(DNS_ADB_VALID(adb));
2738 * Lock the adb itself, lock all the name buckets, then lock all
2739 * the entry buckets. This should put the adb into a state where
2740 * nothing can change, so we can iterate through everything and
2741 * print at our leisure.
2745 isc_stdtime_get(&now);
2747 for (i = 0; i < NBUCKETS; i++)
2748 RUNTIME_CHECK(cleanup_names(adb, i, now) == ISC_FALSE);
2749 for (i = 0; i < NBUCKETS; i++)
2750 RUNTIME_CHECK(cleanup_entries(adb, i, now) == ISC_FALSE);
2752 dump_adb(adb, f, ISC_FALSE, now);
2757 dump_ttl(FILE *f, const char *legend, isc_stdtime_t value, isc_stdtime_t now) {
2758 if (value == INT_MAX)
2760 fprintf(f, " [%s TTL %d]", legend, value - now);
2764 dump_adb(dns_adb_t *adb, FILE *f, isc_boolean_t debug, isc_stdtime_t now) {
2766 dns_adbname_t *name;
2767 dns_adbentry_t *entry;
2769 fprintf(f, ";\n; Address database dump\n;\n");
2771 fprintf(f, "; addr %p, erefcnt %u, irefcnt %u, finds out %u\n",
2772 adb, adb->erefcnt, adb->irefcnt,
2773 isc_mempool_getallocated(adb->nhmp));
2775 for (i = 0; i < NBUCKETS; i++)
2776 LOCK(&adb->namelocks[i]);
2777 for (i = 0; i < NBUCKETS; i++)
2778 LOCK(&adb->entrylocks[i]);
2783 for (i = 0; i < NBUCKETS; i++) {
2784 name = ISC_LIST_HEAD(adb->names[i]);
2788 fprintf(f, "; bucket %d\n", i);
2791 name = ISC_LIST_NEXT(name, plink))
2794 fprintf(f, "; name %p (flags %08x)\n",
2798 print_dns_name(f, &name->name);
2799 if (dns_name_countlabels(&name->target) > 0) {
2800 fprintf(f, " alias ");
2801 print_dns_name(f, &name->target);
2804 dump_ttl(f, "v4", name->expire_v4, now);
2805 dump_ttl(f, "v6", name->expire_v6, now);
2806 dump_ttl(f, "target", name->expire_target, now);
2808 fprintf(f, " [v4 %s] [v6 %s]",
2809 errnames[name->fetch_err],
2810 errnames[name->fetch6_err]);
2814 print_namehook_list(f, "v4", &name->v4, debug, now);
2815 print_namehook_list(f, "v6", &name->v6, debug, now);
2818 print_fetch_list(f, name);
2820 print_find_list(f, name);
2825 fprintf(f, ";\n; Unassociated entries\n;\n");
2827 for (i = 0; i < NBUCKETS; i++) {
2828 entry = ISC_LIST_HEAD(adb->entries[i]);
2829 while (entry != NULL) {
2830 if (entry->refcnt == 0)
2831 dump_entry(f, entry, debug, now);
2832 entry = ISC_LIST_NEXT(entry, plink);
2839 for (i = 0; i < NBUCKETS; i++)
2840 UNLOCK(&adb->entrylocks[i]);
2841 for (i = 0; i < NBUCKETS; i++)
2842 UNLOCK(&adb->namelocks[i]);
2846 dump_entry(FILE *f, dns_adbentry_t *entry, isc_boolean_t debug,
2849 char addrbuf[ISC_NETADDR_FORMATSIZE];
2850 char typebuf[DNS_RDATATYPE_FORMATSIZE];
2851 isc_netaddr_t netaddr;
2852 dns_adblameinfo_t *li;
2854 isc_netaddr_fromsockaddr(&netaddr, &entry->sockaddr);
2855 isc_netaddr_format(&netaddr, addrbuf, sizeof(addrbuf));
2858 fprintf(f, ";\t%p: refcnt %u\n", entry, entry->refcnt);
2860 fprintf(f, ";\t%s [srtt %u] [flags %08x]",
2861 addrbuf, entry->srtt, entry->flags);
2862 if (entry->expires != 0)
2863 fprintf(f, " [ttl %d]", entry->expires - now);
2865 for (li = ISC_LIST_HEAD(entry->lameinfo);
2867 li = ISC_LIST_NEXT(li, plink)) {
2868 fprintf(f, ";\t\t");
2869 print_dns_name(f, &li->qname);
2870 dns_rdatatype_format(li->qtype, typebuf, sizeof(typebuf));
2871 fprintf(f, " %s [lame TTL %d]\n", typebuf,
2872 li->lame_timer - now);
2877 dns_adb_dumpfind(dns_adbfind_t *find, FILE *f) {
2880 dns_adbaddrinfo_t *ai;
2884 * Not used currently, in the API Just In Case we
2885 * want to dump out the name and/or entries too.
2890 fprintf(f, ";Find %p\n", find);
2891 fprintf(f, ";\tqpending %08x partial %08x options %08x flags %08x\n",
2892 find->query_pending, find->partial_result,
2893 find->options, find->flags);
2894 fprintf(f, ";\tname_bucket %d, name %p, event sender %p\n",
2895 find->name_bucket, find->adbname, find->event.ev_sender);
2897 ai = ISC_LIST_HEAD(find->list);
2899 fprintf(f, "\tAddresses:\n");
2900 while (ai != NULL) {
2902 switch (sa->type.sa.sa_family) {
2904 tmpp = inet_ntop(AF_INET, &sa->type.sin.sin_addr,
2908 tmpp = inet_ntop(AF_INET6, &sa->type.sin6.sin6_addr,
2916 tmpp = "BadAddress";
2918 fprintf(f, "\t\tentry %p, flags %08x"
2919 " srtt %u addr %s\n",
2920 ai->entry, ai->flags, ai->srtt, tmpp);
2922 ai = ISC_LIST_NEXT(ai, publink);
2925 UNLOCK(&find->lock);
2929 print_dns_name(FILE *f, dns_name_t *name) {
2930 char buf[DNS_NAME_FORMATSIZE];
2934 dns_name_format(name, buf, sizeof(buf));
2935 fprintf(f, "%s", buf);
2939 print_namehook_list(FILE *f, const char *legend, dns_adbnamehooklist_t *list,
2940 isc_boolean_t debug, isc_stdtime_t now)
2942 dns_adbnamehook_t *nh;
2944 for (nh = ISC_LIST_HEAD(*list);
2946 nh = ISC_LIST_NEXT(nh, plink))
2949 fprintf(f, ";\tHook(%s) %p\n", legend, nh);
2950 dump_entry(f, nh->entry, debug, now);
2955 print_fetch(FILE *f, dns_adbfetch_t *ft, const char *type) {
2956 fprintf(f, "\t\tFetch(%s): %p -> { fetch %p }\n",
2957 type, ft, ft->fetch);
2961 print_fetch_list(FILE *f, dns_adbname_t *n) {
2962 if (NAME_FETCH_A(n))
2963 print_fetch(f, n->fetch_a, "A");
2964 if (NAME_FETCH_AAAA(n))
2965 print_fetch(f, n->fetch_aaaa, "AAAA");
2969 print_find_list(FILE *f, dns_adbname_t *name) {
2970 dns_adbfind_t *find;
2972 find = ISC_LIST_HEAD(name->finds);
2973 while (find != NULL) {
2974 dns_adb_dumpfind(find, f);
2975 find = ISC_LIST_NEXT(find, plink);
2980 dbfind_name(dns_adbname_t *adbname, isc_stdtime_t now, dns_rdatatype_t rdtype)
2982 isc_result_t result;
2983 dns_rdataset_t rdataset;
2985 dns_fixedname_t foundname;
2988 INSIST(DNS_ADBNAME_VALID(adbname));
2990 INSIST(DNS_ADB_VALID(adb));
2991 INSIST(rdtype == dns_rdatatype_a || rdtype == dns_rdatatype_aaaa);
2993 dns_fixedname_init(&foundname);
2994 fname = dns_fixedname_name(&foundname);
2995 dns_rdataset_init(&rdataset);
2997 if (rdtype == dns_rdatatype_a)
2998 adbname->fetch_err = FIND_ERR_UNEXPECTED;
3000 adbname->fetch6_err = FIND_ERR_UNEXPECTED;
3002 result = dns_view_find(adb->view, &adbname->name, rdtype, now,
3003 NAME_GLUEOK(adbname) ? DNS_DBFIND_GLUEOK : 0,
3004 ISC_TF(NAME_HINTOK(adbname)),
3005 NULL, NULL, fname, &rdataset, NULL);
3007 /* XXXVIX this switch statement is too sparse to gen a jump table. */
3013 * Found in the database. Even if we can't copy out
3014 * any information, return success, or else a fetch
3015 * will be made, which will only make things worse.
3017 if (rdtype == dns_rdatatype_a)
3018 adbname->fetch_err = FIND_ERR_SUCCESS;
3020 adbname->fetch6_err = FIND_ERR_SUCCESS;
3021 result = import_rdataset(adbname, &rdataset, now);
3023 case DNS_R_NXDOMAIN:
3026 * We're authoritative and the data doesn't exist.
3027 * Make up a negative cache entry so we don't ask again
3030 * XXXRTH What time should we use? I'm putting in 30 seconds
3033 if (rdtype == dns_rdatatype_a) {
3034 adbname->expire_v4 = now + 30;
3036 "adb name %p: Caching auth negative entry for A",
3038 if (result == DNS_R_NXDOMAIN)
3039 adbname->fetch_err = FIND_ERR_NXDOMAIN;
3041 adbname->fetch_err = FIND_ERR_NXRRSET;
3044 "adb name %p: Caching auth negative entry for AAAA",
3046 adbname->expire_v6 = now + 30;
3047 if (result == DNS_R_NXDOMAIN)
3048 adbname->fetch6_err = FIND_ERR_NXDOMAIN;
3050 adbname->fetch6_err = FIND_ERR_NXRRSET;
3053 case DNS_R_NCACHENXDOMAIN:
3054 case DNS_R_NCACHENXRRSET:
3056 * We found a negative cache entry. Pull the TTL from it
3057 * so we won't ask again for a while.
3059 rdataset.ttl = ttlclamp(rdataset.ttl);
3060 if (rdtype == dns_rdatatype_a) {
3061 adbname->expire_v4 = rdataset.ttl + now;
3062 if (result == DNS_R_NCACHENXDOMAIN)
3063 adbname->fetch_err = FIND_ERR_NXDOMAIN;
3065 adbname->fetch_err = FIND_ERR_NXRRSET;
3067 "adb name %p: Caching negative entry for A (ttl %u)",
3068 adbname, rdataset.ttl);
3071 "adb name %p: Caching negative entry for AAAA (ttl %u)",
3072 adbname, rdataset.ttl);
3073 adbname->expire_v6 = rdataset.ttl + now;
3074 if (result == DNS_R_NCACHENXDOMAIN)
3075 adbname->fetch6_err = FIND_ERR_NXDOMAIN;
3077 adbname->fetch6_err = FIND_ERR_NXRRSET;
3083 * Clear the hint and glue flags, so this will match
3086 adbname->flags &= ~(DNS_ADBFIND_GLUEOK | DNS_ADBFIND_HINTOK);
3088 rdataset.ttl = ttlclamp(rdataset.ttl);
3089 clean_target(adb, &adbname->target);
3090 adbname->expire_target = INT_MAX;
3091 result = set_target(adb, &adbname->name, fname, &rdataset,
3093 if (result == ISC_R_SUCCESS) {
3094 result = DNS_R_ALIAS;
3096 "adb name %p: caching alias target",
3098 adbname->expire_target = rdataset.ttl + now;
3100 if (rdtype == dns_rdatatype_a)
3101 adbname->fetch_err = FIND_ERR_SUCCESS;
3103 adbname->fetch6_err = FIND_ERR_SUCCESS;
3107 if (dns_rdataset_isassociated(&rdataset))
3108 dns_rdataset_disassociate(&rdataset);
3114 fetch_callback(isc_task_t *task, isc_event_t *ev) {
3115 dns_fetchevent_t *dev;
3116 dns_adbname_t *name;
3118 dns_adbfetch_t *fetch;
3120 isc_eventtype_t ev_status;
3122 isc_result_t result;
3123 unsigned int address_type;
3124 isc_boolean_t want_check_exit = ISC_FALSE;
3128 INSIST(ev->ev_type == DNS_EVENT_FETCHDONE);
3129 dev = (dns_fetchevent_t *)ev;
3131 INSIST(DNS_ADBNAME_VALID(name));
3133 INSIST(DNS_ADB_VALID(adb));
3135 bucket = name->lock_bucket;
3136 LOCK(&adb->namelocks[bucket]);
3138 INSIST(NAME_FETCH_A(name) || NAME_FETCH_AAAA(name));
3140 if (NAME_FETCH_A(name) && (name->fetch_a->fetch == dev->fetch)) {
3141 address_type = DNS_ADBFIND_INET;
3142 fetch = name->fetch_a;
3143 name->fetch_a = NULL;
3144 } else if (NAME_FETCH_AAAA(name)
3145 && (name->fetch_aaaa->fetch == dev->fetch)) {
3146 address_type = DNS_ADBFIND_INET6;
3147 fetch = name->fetch_aaaa;
3148 name->fetch_aaaa = NULL;
3152 INSIST(address_type != 0 && fetch != NULL);
3154 dns_resolver_destroyfetch(&fetch->fetch);
3157 ev_status = DNS_EVENT_ADBNOMOREADDRESSES;
3160 * Cleanup things we don't care about.
3162 if (dev->node != NULL)
3163 dns_db_detachnode(dev->db, &dev->node);
3164 if (dev->db != NULL)
3165 dns_db_detach(&dev->db);
3168 * If this name is marked as dead, clean up, throwing away
3169 * potentially good data.
3171 if (NAME_DEAD(name)) {
3172 free_adbfetch(adb, &fetch);
3173 isc_event_free(&ev);
3175 want_check_exit = kill_name(&name, DNS_EVENT_ADBCANCELED);
3177 UNLOCK(&adb->namelocks[bucket]);
3179 if (want_check_exit) {
3188 isc_stdtime_get(&now);
3191 * If we got a negative cache response, remember it.
3193 if (NCACHE_RESULT(dev->result)) {
3194 dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
3195 if (address_type == DNS_ADBFIND_INET) {
3196 DP(NCACHE_LEVEL, "adb fetch name %p: "
3197 "caching negative entry for A (ttl %u)",
3198 name, dev->rdataset->ttl);
3199 name->expire_v4 = ISC_MIN(name->expire_v4,
3200 dev->rdataset->ttl + now);
3201 if (dev->result == DNS_R_NCACHENXDOMAIN)
3202 name->fetch_err = FIND_ERR_NXDOMAIN;
3204 name->fetch_err = FIND_ERR_NXRRSET;
3205 inc_stats(adb, dns_resstatscounter_gluefetchv4fail);
3207 DP(NCACHE_LEVEL, "adb fetch name %p: "
3208 "caching negative entry for AAAA (ttl %u)",
3209 name, dev->rdataset->ttl);
3210 name->expire_v6 = ISC_MIN(name->expire_v6,
3211 dev->rdataset->ttl + now);
3212 if (dev->result == DNS_R_NCACHENXDOMAIN)
3213 name->fetch6_err = FIND_ERR_NXDOMAIN;
3215 name->fetch6_err = FIND_ERR_NXRRSET;
3216 inc_stats(adb, dns_resstatscounter_gluefetchv6fail);
3222 * Handle CNAME/DNAME.
3224 if (dev->result == DNS_R_CNAME || dev->result == DNS_R_DNAME) {
3225 dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
3226 clean_target(adb, &name->target);
3227 name->expire_target = INT_MAX;
3228 result = set_target(adb, &name->name,
3229 dns_fixedname_name(&dev->foundname),
3232 if (result == ISC_R_SUCCESS) {
3234 "adb fetch name %p: caching alias target",
3236 name->expire_target = dev->rdataset->ttl + now;
3242 * Did we get back junk? If so, and there are no more fetches
3243 * sitting out there, tell all the finds about it.
3245 if (dev->result != ISC_R_SUCCESS) {
3246 char buf[DNS_NAME_FORMATSIZE];
3248 dns_name_format(&name->name, buf, sizeof(buf));
3249 DP(DEF_LEVEL, "adb: fetch of '%s' %s failed: %s",
3250 buf, address_type == DNS_ADBFIND_INET ? "A" : "AAAA",
3251 dns_result_totext(dev->result));
3252 /* XXXMLG Don't pound on bad servers. */
3253 if (address_type == DNS_ADBFIND_INET) {
3254 name->expire_v4 = ISC_MIN(name->expire_v4, now + 300);
3255 name->fetch_err = FIND_ERR_FAILURE;
3256 inc_stats(adb, dns_resstatscounter_gluefetchv4fail);
3258 name->expire_v6 = ISC_MIN(name->expire_v6, now + 300);
3259 name->fetch6_err = FIND_ERR_FAILURE;
3260 inc_stats(adb, dns_resstatscounter_gluefetchv6fail);
3266 * We got something potentially useful.
3268 result = import_rdataset(name, &fetch->rdataset, now);
3271 if (result == ISC_R_SUCCESS) {
3272 ev_status = DNS_EVENT_ADBMOREADDRESSES;
3273 if (address_type == DNS_ADBFIND_INET)
3274 name->fetch_err = FIND_ERR_SUCCESS;
3276 name->fetch6_err = FIND_ERR_SUCCESS;
3280 free_adbfetch(adb, &fetch);
3281 isc_event_free(&ev);
3283 clean_finds_at_name(name, ev_status, address_type);
3285 UNLOCK(&adb->namelocks[bucket]);
3289 fetch_name(dns_adbname_t *adbname,
3290 isc_boolean_t start_at_zone,
3291 dns_rdatatype_t type)
3293 isc_result_t result;
3294 dns_adbfetch_t *fetch = NULL;
3296 dns_fixedname_t fixed;
3298 dns_rdataset_t rdataset;
3299 dns_rdataset_t *nameservers;
3300 unsigned int options;
3302 INSIST(DNS_ADBNAME_VALID(adbname));
3304 INSIST(DNS_ADB_VALID(adb));
3306 INSIST((type == dns_rdatatype_a && !NAME_FETCH_V4(adbname)) ||
3307 (type == dns_rdatatype_aaaa && !NAME_FETCH_V6(adbname)));
3309 adbname->fetch_err = FIND_ERR_NOTFOUND;
3313 dns_rdataset_init(&rdataset);
3315 options = DNS_FETCHOPT_NOVALIDATE;
3316 if (start_at_zone) {
3318 "fetch_name: starting at zone for name %p",
3320 dns_fixedname_init(&fixed);
3321 name = dns_fixedname_name(&fixed);
3322 result = dns_view_findzonecut2(adb->view, &adbname->name, name,
3323 0, 0, ISC_TRUE, ISC_FALSE,
3325 if (result != ISC_R_SUCCESS && result != DNS_R_HINT)
3327 nameservers = &rdataset;
3328 options |= DNS_FETCHOPT_UNSHARED;
3331 fetch = new_adbfetch(adb);
3332 if (fetch == NULL) {
3333 result = ISC_R_NOMEMORY;
3337 result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
3338 type, name, nameservers, NULL,
3339 options, adb->task, fetch_callback,
3340 adbname, &fetch->rdataset, NULL,
3342 if (result != ISC_R_SUCCESS)
3345 if (type == dns_rdatatype_a) {
3346 adbname->fetch_a = fetch;
3347 inc_stats(adb, dns_resstatscounter_gluefetchv4);
3349 adbname->fetch_aaaa = fetch;
3350 inc_stats(adb, dns_resstatscounter_gluefetchv6);
3352 fetch = NULL; /* Keep us from cleaning this up below. */
3356 free_adbfetch(adb, &fetch);
3357 if (dns_rdataset_isassociated(&rdataset))
3358 dns_rdataset_disassociate(&rdataset);
3364 * XXXMLG Needs to take a find argument and an address info, no zone or adb,
3365 * since these can be extracted from the find itself.
3368 dns_adb_marklame(dns_adb_t *adb, dns_adbaddrinfo_t *addr, dns_name_t *qname,
3369 dns_rdatatype_t qtype, isc_stdtime_t expire_time)
3371 dns_adblameinfo_t *li;
3373 isc_result_t result = ISC_R_SUCCESS;
3375 REQUIRE(DNS_ADB_VALID(adb));
3376 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3377 REQUIRE(qname != NULL);
3379 bucket = addr->entry->lock_bucket;
3380 LOCK(&adb->entrylocks[bucket]);
3381 li = ISC_LIST_HEAD(addr->entry->lameinfo);
3382 while (li != NULL &&
3383 (li->qtype != qtype || !dns_name_equal(qname, &li->qname)))
3384 li = ISC_LIST_NEXT(li, plink);
3386 if (expire_time > li->lame_timer)
3387 li->lame_timer = expire_time;
3390 li = new_adblameinfo(adb, qname, qtype);
3392 result = ISC_R_NOMEMORY;
3396 li->lame_timer = expire_time;
3398 ISC_LIST_PREPEND(addr->entry->lameinfo, li, plink);
3400 UNLOCK(&adb->entrylocks[bucket]);
3406 dns_adb_adjustsrtt(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
3407 unsigned int rtt, unsigned int factor)
3410 unsigned int new_srtt;
3413 REQUIRE(DNS_ADB_VALID(adb));
3414 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3415 REQUIRE(factor <= 10);
3417 bucket = addr->entry->lock_bucket;
3418 LOCK(&adb->entrylocks[bucket]);
3420 if (factor == DNS_ADB_RTTADJAGE)
3421 new_srtt = addr->entry->srtt * 98 / 100;
3423 new_srtt = (addr->entry->srtt / 10 * factor)
3424 + (rtt / 10 * (10 - factor));
3426 addr->entry->srtt = new_srtt;
3427 addr->srtt = new_srtt;
3429 isc_stdtime_get(&now);
3430 addr->entry->expires = now + ADB_ENTRY_WINDOW;
3432 UNLOCK(&adb->entrylocks[bucket]);
3436 dns_adb_changeflags(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
3437 unsigned int bits, unsigned int mask)
3441 REQUIRE(DNS_ADB_VALID(adb));
3442 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3444 bucket = addr->entry->lock_bucket;
3445 LOCK(&adb->entrylocks[bucket]);
3447 addr->entry->flags = (addr->entry->flags & ~mask) | (bits & mask);
3449 * Note that we do not update the other bits in addr->flags with
3450 * the most recent values from addr->entry->flags.
3452 addr->flags = (addr->flags & ~mask) | (bits & mask);
3454 UNLOCK(&adb->entrylocks[bucket]);
3458 dns_adb_findaddrinfo(dns_adb_t *adb, isc_sockaddr_t *sa,
3459 dns_adbaddrinfo_t **addrp, isc_stdtime_t now)
3462 dns_adbentry_t *entry;
3463 dns_adbaddrinfo_t *addr;
3464 isc_result_t result;
3467 REQUIRE(DNS_ADB_VALID(adb));
3468 REQUIRE(addrp != NULL && *addrp == NULL);
3472 result = ISC_R_SUCCESS;
3473 bucket = DNS_ADB_INVALIDBUCKET;
3474 entry = find_entry_and_lock(adb, sa, &bucket, now);
3475 if (adb->entry_sd[bucket]) {
3476 result = ISC_R_SHUTTINGDOWN;
3479 if (entry == NULL) {
3481 * We don't know anything about this address.
3483 entry = new_adbentry(adb);
3484 if (entry == NULL) {
3485 result = ISC_R_NOMEMORY;
3488 entry->sockaddr = *sa;
3489 link_entry(adb, bucket, entry);
3490 DP(ENTER_LEVEL, "findaddrinfo: new entry %p", entry);
3492 DP(ENTER_LEVEL, "findaddrinfo: found entry %p", entry);
3494 port = isc_sockaddr_getport(sa);
3495 addr = new_adbaddrinfo(adb, entry, port);
3497 result = ISC_R_NOMEMORY;
3499 inc_entry_refcnt(adb, entry, ISC_FALSE);
3504 UNLOCK(&adb->entrylocks[bucket]);
3510 dns_adb_freeaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **addrp) {
3511 dns_adbaddrinfo_t *addr;
3512 dns_adbentry_t *entry;
3515 isc_boolean_t want_check_exit = ISC_FALSE;
3516 isc_boolean_t overmem;
3518 REQUIRE(DNS_ADB_VALID(adb));
3519 REQUIRE(addrp != NULL);
3521 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3522 entry = addr->entry;
3523 REQUIRE(DNS_ADBENTRY_VALID(entry));
3525 isc_stdtime_get(&now);
3528 overmem = isc_mem_isovermem(adb->mctx);
3530 bucket = addr->entry->lock_bucket;
3531 LOCK(&adb->entrylocks[bucket]);
3533 entry->expires = now + ADB_ENTRY_WINDOW;
3535 want_check_exit = dec_entry_refcnt(adb, overmem, entry, ISC_FALSE);
3537 UNLOCK(&adb->entrylocks[bucket]);
3540 free_adbaddrinfo(adb, &addr);
3542 if (want_check_exit) {
3550 dns_adb_flush(dns_adb_t *adb) {
3553 INSIST(DNS_ADB_VALID(adb));
3558 * Call our cleanup routines.
3560 for (i = 0; i < NBUCKETS; i++)
3561 RUNTIME_CHECK(cleanup_names(adb, i, INT_MAX) == ISC_FALSE);
3562 for (i = 0; i < NBUCKETS; i++)
3563 RUNTIME_CHECK(cleanup_entries(adb, i, INT_MAX) == ISC_FALSE);
3565 #ifdef DUMP_ADB_AFTER_CLEANING
3566 dump_adb(adb, stdout, ISC_TRUE, INT_MAX);
3573 dns_adb_flushname(dns_adb_t *adb, dns_name_t *name) {
3574 dns_adbname_t *adbname;
3575 dns_adbname_t *nextname;
3578 INSIST(DNS_ADB_VALID(adb));
3581 bucket = dns_name_hash(name, ISC_FALSE) % NBUCKETS;
3582 LOCK(&adb->namelocks[bucket]);
3583 adbname = ISC_LIST_HEAD(adb->names[bucket]);
3584 while (adbname != NULL) {
3585 nextname = ISC_LIST_NEXT(adbname, plink);
3586 if (!NAME_DEAD(adbname) &&
3587 dns_name_equal(name, &adbname->name)) {
3588 RUNTIME_CHECK(kill_name(&adbname,
3589 DNS_EVENT_ADBCANCELED) ==
3594 UNLOCK(&adb->namelocks[bucket]);
3599 water(void *arg, int mark) {
3601 * We're going to change the way to handle overmem condition: use
3602 * isc_mem_isovermem() instead of storing the state via this callback,
3603 * since the latter way tends to cause race conditions.
3604 * To minimize the change, and in case we re-enable the callback
3605 * approach, however, keep this function at the moment.
3608 dns_adb_t *adb = arg;
3609 isc_boolean_t overmem = ISC_TF(mark == ISC_MEM_HIWATER);
3611 REQUIRE(DNS_ADB_VALID(adb));
3613 DP(ISC_LOG_DEBUG(1),
3614 "adb reached %s water mark", overmem ? "high" : "low");
3618 dns_adb_setadbsize(dns_adb_t *adb, isc_uint32_t size) {
3619 isc_uint32_t hiwater;
3620 isc_uint32_t lowater;
3622 INSIST(DNS_ADB_VALID(adb));
3624 if (size != 0 && size < DNS_ADB_MINADBSIZE)
3625 size = DNS_ADB_MINADBSIZE;
3627 hiwater = size - (size >> 3); /* Approximately 7/8ths. */
3628 lowater = size - (size >> 2); /* Approximately 3/4ths. */
3630 if (size == 0 || hiwater == 0 || lowater == 0)
3631 isc_mem_setwater(adb->mctx, water, adb, 0, 0);
3633 isc_mem_setwater(adb->mctx, water, adb, hiwater, lowater);