vendor/BIND: Update to 9.5.2-P3
[dragonfly.git] / contrib / bind / lib / dns / adb.c
blobcb45c82d01b4fe9b2e2c9ce113e74423fc0e4279
1 /*
2 * Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: adb.c,v 1.233.36.14 2009/02/03 22:35:00 jinmei Exp $ */
20 /*! \file
22 * \note
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.
29 #include <config.h>
31 #include <limits.h>
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?) */
38 #include <isc/task.h>
39 #include <isc/util.h>
41 #include <dns/adb.h>
42 #include <dns/db.h>
43 #include <dns/events.h>
44 #include <dns/log.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)
68 /*!
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 */
75 /*!
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 */
85 /*%
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
92 #endif
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 */
110 struct dns_adb {
111 unsigned int magic;
113 isc_mutex_t lock;
114 isc_mutex_t reflock; /*%< Covers irefcnt, erefcnt */
115 isc_mutex_t overmemlock; /*%< Covers overmem */
116 isc_mem_t *mctx;
117 dns_view_t *view;
119 isc_taskmgr_t *taskmgr;
120 isc_task_t *task;
121 isc_boolean_t overmem;
123 isc_interval_t tick_interval;
124 int next_cleanbucket;
126 unsigned int irefcnt;
127 unsigned int erefcnt;
129 isc_mutex_t mplock;
130 isc_mempool_t *nmp; /*%< dns_adbname_t */
131 isc_mempool_t *nhmp; /*%< dns_adbnamehook_t */
132 isc_mempool_t *limp; /*%< dns_adblameinfo_t */
133 isc_mempool_t *emp; /*%< dns_adbentry_t */
134 isc_mempool_t *ahmp; /*%< dns_adbfind_t */
135 isc_mempool_t *aimp; /*%< dns_adbaddrinfo_t */
136 isc_mempool_t *afmp; /*%< dns_adbfetch_t */
139 * Bucketized locks and lists for names.
141 * XXXRTH Have a per-bucket structure that contains all of these?
143 dns_adbnamelist_t names[NBUCKETS];
144 dns_adbnamelist_t deadnames[NBUCKETS];
145 /*% See dns_adbnamelist_t */
146 isc_mutex_t namelocks[NBUCKETS];
147 /*% See dns_adbnamelist_t */
148 isc_boolean_t name_sd[NBUCKETS];
149 /*% See dns_adbnamelist_t */
150 unsigned int name_refcnt[NBUCKETS];
153 * Bucketized locks for entries.
155 * XXXRTH Have a per-bucket structure that contains all of these?
157 dns_adbentrylist_t entries[NBUCKETS];
158 dns_adbentrylist_t deadentries[NBUCKETS];
159 isc_mutex_t entrylocks[NBUCKETS];
160 isc_boolean_t entry_sd[NBUCKETS]; /*%< shutting down */
161 unsigned int entry_refcnt[NBUCKETS];
163 isc_event_t cevent;
164 isc_boolean_t cevent_sent;
165 isc_boolean_t shutting_down;
166 isc_eventlist_t whenshutdown;
170 * XXXMLG Document these structures.
173 /*% dns_adbname structure */
174 struct dns_adbname {
175 unsigned int magic;
176 dns_name_t name;
177 dns_adb_t *adb;
178 unsigned int partial_result;
179 unsigned int flags;
180 int lock_bucket;
181 dns_name_t target;
182 isc_stdtime_t expire_target;
183 isc_stdtime_t expire_v4;
184 isc_stdtime_t expire_v6;
185 unsigned int chains;
186 dns_adbnamehooklist_t v4;
187 dns_adbnamehooklist_t v6;
188 dns_adbfetch_t *fetch_a;
189 dns_adbfetch_t *fetch_aaaa;
190 unsigned int fetch_err;
191 unsigned int fetch6_err;
192 dns_adbfindlist_t finds;
193 /* for LRU-based management */
194 isc_stdtime_t last_used;
196 ISC_LINK(dns_adbname_t) plink;
199 /*% The adbfetch structure */
200 struct dns_adbfetch {
201 unsigned int magic;
202 dns_fetch_t *fetch;
203 dns_rdataset_t rdataset;
207 * This is a small widget that dangles off a dns_adbname_t. It contains a
208 * pointer to the address information about this host, and a link to the next
209 * namehook that will contain the next address this host has.
211 struct dns_adbnamehook {
212 unsigned int magic;
213 dns_adbentry_t *entry;
214 ISC_LINK(dns_adbnamehook_t) plink;
218 * This is a small widget that holds qname-specific information about an
219 * address. Currently limited to lameness, but could just as easily be
220 * extended to other types of information about zones.
222 struct dns_adblameinfo {
223 unsigned int magic;
225 dns_name_t qname;
226 dns_rdatatype_t qtype;
227 isc_stdtime_t lame_timer;
229 ISC_LINK(dns_adblameinfo_t) plink;
233 * An address entry. It holds quite a bit of information about addresses,
234 * including edns state (in "flags"), rtt, and of course the address of
235 * the host.
237 struct dns_adbentry {
238 unsigned int magic;
240 int lock_bucket;
241 unsigned int refcnt;
243 unsigned int flags;
244 unsigned int srtt;
245 isc_sockaddr_t sockaddr;
247 isc_stdtime_t expires;
248 /*%<
249 * A nonzero 'expires' field indicates that the entry should
250 * persist until that time. This allows entries found
251 * using dns_adb_findaddrinfo() to persist for a limited time
252 * even though they are not necessarily associated with a
253 * name.
256 ISC_LIST(dns_adblameinfo_t) lameinfo;
257 ISC_LINK(dns_adbentry_t) plink;
261 * Internal functions (and prototypes).
263 static inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
264 static inline void free_adbname(dns_adb_t *, dns_adbname_t **);
265 static inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
266 dns_adbentry_t *);
267 static inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
268 static inline dns_adblameinfo_t *new_adblameinfo(dns_adb_t *, dns_name_t *,
269 dns_rdatatype_t);
270 static inline void free_adblameinfo(dns_adb_t *, dns_adblameinfo_t **);
271 static inline dns_adbentry_t *new_adbentry(dns_adb_t *);
272 static inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
273 static inline dns_adbfind_t *new_adbfind(dns_adb_t *);
274 static inline isc_boolean_t free_adbfind(dns_adb_t *, dns_adbfind_t **);
275 static inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *, dns_adbentry_t *,
276 in_port_t);
277 static inline dns_adbfetch_t *new_adbfetch(dns_adb_t *);
278 static inline void free_adbfetch(dns_adb_t *, dns_adbfetch_t **);
279 static inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
280 unsigned int, int *);
281 static inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
282 isc_sockaddr_t *, int *,
283 isc_stdtime_t);
284 static void dump_adb(dns_adb_t *, FILE *, isc_boolean_t debug, isc_stdtime_t);
285 static void print_dns_name(FILE *, dns_name_t *);
286 static void print_namehook_list(FILE *, const char *legend,
287 dns_adbnamehooklist_t *list,
288 isc_boolean_t debug,
289 isc_stdtime_t now);
290 static void print_find_list(FILE *, dns_adbname_t *);
291 static void print_fetch_list(FILE *, dns_adbname_t *);
292 static inline isc_boolean_t dec_adb_irefcnt(dns_adb_t *);
293 static inline void inc_adb_irefcnt(dns_adb_t *);
294 static inline void inc_adb_erefcnt(dns_adb_t *);
295 static inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
296 isc_boolean_t);
297 static inline isc_boolean_t dec_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
298 isc_boolean_t);
299 static inline void violate_locking_hierarchy(isc_mutex_t *, isc_mutex_t *);
300 static isc_boolean_t clean_namehooks(dns_adb_t *, dns_adbnamehooklist_t *);
301 static void clean_target(dns_adb_t *, dns_name_t *);
302 static void clean_finds_at_name(dns_adbname_t *, isc_eventtype_t,
303 unsigned int);
304 static isc_boolean_t check_expire_namehooks(dns_adbname_t *, isc_stdtime_t);
305 static isc_boolean_t check_expire_entry(dns_adb_t *, dns_adbentry_t **,
306 isc_stdtime_t);
307 static void cancel_fetches_at_name(dns_adbname_t *);
308 static isc_result_t dbfind_name(dns_adbname_t *, isc_stdtime_t,
309 dns_rdatatype_t);
310 static isc_result_t fetch_name(dns_adbname_t *, isc_boolean_t,
311 dns_rdatatype_t);
312 static inline void check_exit(dns_adb_t *);
313 static void destroy(dns_adb_t *);
314 static isc_boolean_t shutdown_names(dns_adb_t *);
315 static isc_boolean_t shutdown_entries(dns_adb_t *);
316 static inline void link_name(dns_adb_t *, int, dns_adbname_t *);
317 static inline isc_boolean_t unlink_name(dns_adb_t *, dns_adbname_t *);
318 static inline void link_entry(dns_adb_t *, int, dns_adbentry_t *);
319 static inline isc_boolean_t unlink_entry(dns_adb_t *, dns_adbentry_t *);
320 static isc_boolean_t kill_name(dns_adbname_t **, isc_eventtype_t);
321 static void water(void *, int);
322 static void dump_entry(FILE *, dns_adbentry_t *, isc_boolean_t, isc_stdtime_t);
325 * MUST NOT overlap DNS_ADBFIND_* flags!
327 #define FIND_EVENT_SENT 0x40000000
328 #define FIND_EVENT_FREED 0x80000000
329 #define FIND_EVENTSENT(h) (((h)->flags & FIND_EVENT_SENT) != 0)
330 #define FIND_EVENTFREED(h) (((h)->flags & FIND_EVENT_FREED) != 0)
332 #define NAME_NEEDS_POKE 0x80000000
333 #define NAME_IS_DEAD 0x40000000
334 #define NAME_HINT_OK DNS_ADBFIND_HINTOK
335 #define NAME_GLUE_OK DNS_ADBFIND_GLUEOK
336 #define NAME_STARTATZONE DNS_ADBFIND_STARTATZONE
337 #define NAME_DEAD(n) (((n)->flags & NAME_IS_DEAD) != 0)
338 #define NAME_NEEDSPOKE(n) (((n)->flags & NAME_NEEDS_POKE) != 0)
339 #define NAME_GLUEOK(n) (((n)->flags & NAME_GLUE_OK) != 0)
340 #define NAME_HINTOK(n) (((n)->flags & NAME_HINT_OK) != 0)
343 * Private flag(s) for entries.
344 * MUST NOT overlap FCTX_ADDRINFO_xxx and DNS_FETCHOPT_NOEDNS0.
346 #define ENTRY_IS_DEAD 0x80000000
349 * To the name, address classes are all that really exist. If it has a
350 * V6 address it doesn't care if it came from a AAAA query.
352 #define NAME_HAS_V4(n) (!ISC_LIST_EMPTY((n)->v4))
353 #define NAME_HAS_V6(n) (!ISC_LIST_EMPTY((n)->v6))
354 #define NAME_HAS_ADDRS(n) (NAME_HAS_V4(n) || NAME_HAS_V6(n))
357 * Fetches are broken out into A and AAAA types. In some cases,
358 * however, it makes more sense to test for a particular class of fetches,
359 * like V4 or V6 above.
360 * Note: since we have removed the support of A6 in adb, FETCH_A and FETCH_AAAA
361 * are now equal to FETCH_V4 and FETCH_V6, respectively.
363 #define NAME_FETCH_A(n) ((n)->fetch_a != NULL)
364 #define NAME_FETCH_AAAA(n) ((n)->fetch_aaaa != NULL)
365 #define NAME_FETCH_V4(n) (NAME_FETCH_A(n))
366 #define NAME_FETCH_V6(n) (NAME_FETCH_AAAA(n))
367 #define NAME_FETCH(n) (NAME_FETCH_V4(n) || NAME_FETCH_V6(n))
370 * Find options and tests to see if there are addresses on the list.
372 #define FIND_WANTEVENT(fn) (((fn)->options & DNS_ADBFIND_WANTEVENT) != 0)
373 #define FIND_WANTEMPTYEVENT(fn) (((fn)->options & DNS_ADBFIND_EMPTYEVENT) != 0)
374 #define FIND_AVOIDFETCHES(fn) (((fn)->options & DNS_ADBFIND_AVOIDFETCHES) \
375 != 0)
376 #define FIND_STARTATZONE(fn) (((fn)->options & DNS_ADBFIND_STARTATZONE) \
377 != 0)
378 #define FIND_HINTOK(fn) (((fn)->options & DNS_ADBFIND_HINTOK) != 0)
379 #define FIND_GLUEOK(fn) (((fn)->options & DNS_ADBFIND_GLUEOK) != 0)
380 #define FIND_HAS_ADDRS(fn) (!ISC_LIST_EMPTY((fn)->list))
381 #define FIND_RETURNLAME(fn) (((fn)->options & DNS_ADBFIND_RETURNLAME) != 0)
384 * These are currently used on simple unsigned ints, so they are
385 * not really associated with any particular type.
387 #define WANT_INET(x) (((x) & DNS_ADBFIND_INET) != 0)
388 #define WANT_INET6(x) (((x) & DNS_ADBFIND_INET6) != 0)
390 #define EXPIRE_OK(exp, now) ((exp == INT_MAX) || (exp < now))
393 * Find out if the flags on a name (nf) indicate if it is a hint or
394 * glue, and compare this to the appropriate bits set in o, to see if
395 * this is ok.
397 #define GLUE_OK(nf, o) (!NAME_GLUEOK(nf) || (((o) & DNS_ADBFIND_GLUEOK) != 0))
398 #define HINT_OK(nf, o) (!NAME_HINTOK(nf) || (((o) & DNS_ADBFIND_HINTOK) != 0))
399 #define GLUEHINT_OK(nf, o) (GLUE_OK(nf, o) || HINT_OK(nf, o))
400 #define STARTATZONE_MATCHES(nf, o) (((nf)->flags & NAME_STARTATZONE) == \
401 ((o) & DNS_ADBFIND_STARTATZONE))
403 #define ENTER_LEVEL ISC_LOG_DEBUG(50)
404 #define EXIT_LEVEL ENTER_LEVEL
405 #define CLEAN_LEVEL ISC_LOG_DEBUG(100)
406 #define DEF_LEVEL ISC_LOG_DEBUG(5)
407 #define NCACHE_LEVEL ISC_LOG_DEBUG(20)
409 #define NCACHE_RESULT(r) ((r) == DNS_R_NCACHENXDOMAIN || \
410 (r) == DNS_R_NCACHENXRRSET)
411 #define AUTH_NX(r) ((r) == DNS_R_NXDOMAIN || \
412 (r) == DNS_R_NXRRSET)
413 #define NXDOMAIN_RESULT(r) ((r) == DNS_R_NXDOMAIN || \
414 (r) == DNS_R_NCACHENXDOMAIN)
415 #define NXRRSET_RESULT(r) ((r) == DNS_R_NCACHENXRRSET || \
416 (r) == DNS_R_NXRRSET || \
417 (r) == DNS_R_HINTNXRRSET)
420 * Error state rankings.
423 #define FIND_ERR_SUCCESS 0 /* highest rank */
424 #define FIND_ERR_CANCELED 1
425 #define FIND_ERR_FAILURE 2
426 #define FIND_ERR_NXDOMAIN 3
427 #define FIND_ERR_NXRRSET 4
428 #define FIND_ERR_UNEXPECTED 5
429 #define FIND_ERR_NOTFOUND 6
430 #define FIND_ERR_MAX 7
432 static const char *errnames[] = {
433 "success",
434 "canceled",
435 "failure",
436 "nxdomain",
437 "nxrrset",
438 "unexpected",
439 "not_found"
442 #define NEWERR(old, new) (ISC_MIN((old), (new)))
444 static isc_result_t find_err_map[FIND_ERR_MAX] = {
445 ISC_R_SUCCESS,
446 ISC_R_CANCELED,
447 ISC_R_FAILURE,
448 DNS_R_NXDOMAIN,
449 DNS_R_NXRRSET,
450 ISC_R_UNEXPECTED,
451 ISC_R_NOTFOUND /* not YET found */
454 static void
455 DP(int level, const char *format, ...) ISC_FORMAT_PRINTF(2, 3);
457 static void
458 DP(int level, const char *format, ...) {
459 va_list args;
461 va_start(args, format);
462 isc_log_vwrite(dns_lctx,
463 DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ADB,
464 level, format, args);
465 va_end(args);
469 * Increment resolver-related statistics counters.
471 static inline void
472 inc_stats(dns_adb_t *adb, isc_statscounter_t counter) {
473 if (adb->view->resstats != NULL)
474 isc_stats_increment(adb->view->resstats, counter);
477 static inline dns_ttl_t
478 ttlclamp(dns_ttl_t ttl) {
479 if (ttl < ADB_CACHE_MINIMUM)
480 ttl = ADB_CACHE_MINIMUM;
481 if (ttl > ADB_CACHE_MAXIMUM)
482 ttl = ADB_CACHE_MAXIMUM;
484 return (ttl);
488 * Requires the adbname bucket be locked and that no entry buckets be locked.
490 * This code handles A and AAAA rdatasets only.
492 static isc_result_t
493 import_rdataset(dns_adbname_t *adbname, dns_rdataset_t *rdataset,
494 isc_stdtime_t now)
496 isc_result_t result;
497 dns_adb_t *adb;
498 dns_adbnamehook_t *nh;
499 dns_adbnamehook_t *anh;
500 dns_rdata_t rdata = DNS_RDATA_INIT;
501 struct in_addr ina;
502 struct in6_addr in6a;
503 isc_sockaddr_t sockaddr;
504 dns_adbentry_t *foundentry; /* NO CLEAN UP! */
505 int addr_bucket;
506 isc_boolean_t new_addresses_added;
507 dns_rdatatype_t rdtype;
508 unsigned int findoptions;
509 dns_adbnamehooklist_t *hookhead;
511 INSIST(DNS_ADBNAME_VALID(adbname));
512 adb = adbname->adb;
513 INSIST(DNS_ADB_VALID(adb));
515 rdtype = rdataset->type;
516 INSIST((rdtype == dns_rdatatype_a) || (rdtype == dns_rdatatype_aaaa));
517 if (rdtype == dns_rdatatype_a)
518 findoptions = DNS_ADBFIND_INET;
519 else
520 findoptions = DNS_ADBFIND_INET6;
522 addr_bucket = DNS_ADB_INVALIDBUCKET;
523 new_addresses_added = ISC_FALSE;
525 nh = NULL;
526 result = dns_rdataset_first(rdataset);
527 while (result == ISC_R_SUCCESS) {
528 dns_rdata_reset(&rdata);
529 dns_rdataset_current(rdataset, &rdata);
530 if (rdtype == dns_rdatatype_a) {
531 INSIST(rdata.length == 4);
532 memcpy(&ina.s_addr, rdata.data, 4);
533 isc_sockaddr_fromin(&sockaddr, &ina, 0);
534 hookhead = &adbname->v4;
535 } else {
536 INSIST(rdata.length == 16);
537 memcpy(in6a.s6_addr, rdata.data, 16);
538 isc_sockaddr_fromin6(&sockaddr, &in6a, 0);
539 hookhead = &adbname->v6;
542 INSIST(nh == NULL);
543 nh = new_adbnamehook(adb, NULL);
544 if (nh == NULL) {
545 adbname->partial_result |= findoptions;
546 result = ISC_R_NOMEMORY;
547 goto fail;
550 foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket,
551 now);
552 if (foundentry == NULL) {
553 dns_adbentry_t *entry;
555 entry = new_adbentry(adb);
556 if (entry == NULL) {
557 adbname->partial_result |= findoptions;
558 result = ISC_R_NOMEMORY;
559 goto fail;
562 entry->sockaddr = sockaddr;
563 entry->refcnt = 1;
565 nh->entry = entry;
567 link_entry(adb, addr_bucket, entry);
568 } else {
569 for (anh = ISC_LIST_HEAD(*hookhead);
570 anh != NULL;
571 anh = ISC_LIST_NEXT(anh, plink))
572 if (anh->entry == foundentry)
573 break;
574 if (anh == NULL) {
575 foundentry->refcnt++;
576 nh->entry = foundentry;
577 } else
578 free_adbnamehook(adb, &nh);
581 new_addresses_added = ISC_TRUE;
582 if (nh != NULL)
583 ISC_LIST_APPEND(*hookhead, nh, plink);
584 nh = NULL;
585 result = dns_rdataset_next(rdataset);
588 fail:
589 if (nh != NULL)
590 free_adbnamehook(adb, &nh);
592 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
593 UNLOCK(&adb->entrylocks[addr_bucket]);
595 if (rdataset->trust == dns_trust_glue ||
596 rdataset->trust == dns_trust_additional)
597 rdataset->ttl = ADB_CACHE_MINIMUM;
598 else
599 rdataset->ttl = ttlclamp(rdataset->ttl);
601 if (rdtype == dns_rdatatype_a) {
602 DP(NCACHE_LEVEL, "expire_v4 set to MIN(%u,%u) import_rdataset",
603 adbname->expire_v4, now + rdataset->ttl);
604 adbname->expire_v4 = ISC_MIN(adbname->expire_v4,
605 now + rdataset->ttl);
606 } else {
607 DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) import_rdataset",
608 adbname->expire_v6, now + rdataset->ttl);
609 adbname->expire_v6 = ISC_MIN(adbname->expire_v6,
610 now + rdataset->ttl);
613 if (new_addresses_added) {
615 * Lie a little here. This is more or less so code that cares
616 * can find out if any new information was added or not.
618 return (ISC_R_SUCCESS);
621 return (result);
625 * Requires the name's bucket be locked.
627 static isc_boolean_t
628 kill_name(dns_adbname_t **n, isc_eventtype_t ev) {
629 dns_adbname_t *name;
630 isc_boolean_t result = ISC_FALSE;
631 isc_boolean_t result4, result6;
632 int bucket;
633 dns_adb_t *adb;
635 INSIST(n != NULL);
636 name = *n;
637 *n = NULL;
638 INSIST(DNS_ADBNAME_VALID(name));
639 adb = name->adb;
640 INSIST(DNS_ADB_VALID(adb));
642 DP(DEF_LEVEL, "killing name %p", name);
645 * If we're dead already, just check to see if we should go
646 * away now or not.
648 if (NAME_DEAD(name) && !NAME_FETCH(name)) {
649 result = unlink_name(adb, name);
650 free_adbname(adb, &name);
651 if (result)
652 result = dec_adb_irefcnt(adb);
653 return (result);
657 * Clean up the name's various lists. These two are destructive
658 * in that they will always empty the list.
660 clean_finds_at_name(name, ev, DNS_ADBFIND_ADDRESSMASK);
661 result4 = clean_namehooks(adb, &name->v4);
662 result6 = clean_namehooks(adb, &name->v6);
663 clean_target(adb, &name->target);
664 result = ISC_TF(result4 || result6);
667 * If fetches are running, cancel them. If none are running, we can
668 * just kill the name here.
670 if (!NAME_FETCH(name)) {
671 INSIST(result == ISC_FALSE);
672 result = unlink_name(adb, name);
673 free_adbname(adb, &name);
674 if (result)
675 result = dec_adb_irefcnt(adb);
676 } else {
677 cancel_fetches_at_name(name);
678 if (!NAME_DEAD(name)) {
679 bucket = name->lock_bucket;
680 ISC_LIST_UNLINK(adb->names[bucket], name, plink);
681 ISC_LIST_APPEND(adb->deadnames[bucket], name, plink);
682 name->flags |= NAME_IS_DEAD;
685 return (result);
689 * Requires the name's bucket be locked and no entry buckets be locked.
691 static isc_boolean_t
692 check_expire_namehooks(dns_adbname_t *name, isc_stdtime_t now) {
693 dns_adb_t *adb;
694 isc_boolean_t result4 = ISC_FALSE;
695 isc_boolean_t result6 = ISC_FALSE;
697 INSIST(DNS_ADBNAME_VALID(name));
698 adb = name->adb;
699 INSIST(DNS_ADB_VALID(adb));
702 * Check to see if we need to remove the v4 addresses
704 if (!NAME_FETCH_V4(name) && EXPIRE_OK(name->expire_v4, now)) {
705 if (NAME_HAS_V4(name)) {
706 DP(DEF_LEVEL, "expiring v4 for name %p", name);
707 result4 = clean_namehooks(adb, &name->v4);
708 name->partial_result &= ~DNS_ADBFIND_INET;
710 name->expire_v4 = INT_MAX;
711 name->fetch_err = FIND_ERR_UNEXPECTED;
715 * Check to see if we need to remove the v6 addresses
717 if (!NAME_FETCH_V6(name) && EXPIRE_OK(name->expire_v6, now)) {
718 if (NAME_HAS_V6(name)) {
719 DP(DEF_LEVEL, "expiring v6 for name %p", name);
720 result6 = clean_namehooks(adb, &name->v6);
721 name->partial_result &= ~DNS_ADBFIND_INET6;
723 name->expire_v6 = INT_MAX;
724 name->fetch6_err = FIND_ERR_UNEXPECTED;
728 * Check to see if we need to remove the alias target.
730 if (EXPIRE_OK(name->expire_target, now)) {
731 clean_target(adb, &name->target);
732 name->expire_target = INT_MAX;
734 return (ISC_TF(result4 || result6));
738 * Requires the name's bucket be locked.
740 static inline void
741 link_name(dns_adb_t *adb, int bucket, dns_adbname_t *name) {
742 INSIST(name->lock_bucket == DNS_ADB_INVALIDBUCKET);
744 ISC_LIST_PREPEND(adb->names[bucket], name, plink);
745 name->lock_bucket = bucket;
746 adb->name_refcnt[bucket]++;
750 * Requires the name's bucket be locked.
752 static inline isc_boolean_t
753 unlink_name(dns_adb_t *adb, dns_adbname_t *name) {
754 int bucket;
755 isc_boolean_t result = ISC_FALSE;
757 bucket = name->lock_bucket;
758 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
760 if (NAME_DEAD(name))
761 ISC_LIST_UNLINK(adb->deadnames[bucket], name, plink);
762 else
763 ISC_LIST_UNLINK(adb->names[bucket], name, plink);
764 name->lock_bucket = DNS_ADB_INVALIDBUCKET;
765 INSIST(adb->name_refcnt[bucket] > 0);
766 adb->name_refcnt[bucket]--;
767 if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
768 result = ISC_TRUE;
769 return (result);
773 * Requires the entry's bucket be locked.
775 static inline void
776 link_entry(dns_adb_t *adb, int bucket, dns_adbentry_t *entry) {
777 int i;
778 dns_adbentry_t *e;
780 if (adb->overmem) {
781 for (i = 0; i < 2; i++) {
782 e = ISC_LIST_TAIL(adb->entries[bucket]);
783 if (e == NULL)
784 break;
785 if (e->refcnt == 0) {
786 unlink_entry(adb, e);
787 free_adbentry(adb, &e);
788 continue;
790 INSIST((e->flags & ENTRY_IS_DEAD) == 0);
791 e->flags |= ENTRY_IS_DEAD;
792 ISC_LIST_UNLINK(adb->entries[bucket], e, plink);
793 ISC_LIST_PREPEND(adb->deadentries[bucket], e, plink);
797 ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
798 entry->lock_bucket = bucket;
799 adb->entry_refcnt[bucket]++;
803 * Requires the entry's bucket be locked.
805 static inline isc_boolean_t
806 unlink_entry(dns_adb_t *adb, dns_adbentry_t *entry) {
807 int bucket;
808 isc_boolean_t result = ISC_FALSE;
810 bucket = entry->lock_bucket;
811 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
813 if ((entry->flags & ENTRY_IS_DEAD) != 0)
814 ISC_LIST_UNLINK(adb->deadentries[bucket], entry, plink);
815 else
816 ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
817 entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
818 INSIST(adb->entry_refcnt[bucket] > 0);
819 adb->entry_refcnt[bucket]--;
820 if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
821 result = ISC_TRUE;
822 return (result);
825 static inline void
826 violate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want) {
827 if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
828 UNLOCK(have);
829 LOCK(want);
830 LOCK(have);
835 * The ADB _MUST_ be locked before calling. Also, exit conditions must be
836 * checked after calling this function.
838 static isc_boolean_t
839 shutdown_names(dns_adb_t *adb) {
840 int bucket;
841 isc_boolean_t result = ISC_FALSE;
842 dns_adbname_t *name;
843 dns_adbname_t *next_name;
845 for (bucket = 0; bucket < NBUCKETS; bucket++) {
846 LOCK(&adb->namelocks[bucket]);
847 adb->name_sd[bucket] = ISC_TRUE;
849 name = ISC_LIST_HEAD(adb->names[bucket]);
850 if (name == NULL) {
852 * This bucket has no names. We must decrement the
853 * irefcnt ourselves, since it will not be
854 * automatically triggered by a name being unlinked.
856 INSIST(result == ISC_FALSE);
857 result = dec_adb_irefcnt(adb);
858 } else {
860 * Run through the list. For each name, clean up finds
861 * found there, and cancel any fetches running. When
862 * all the fetches are canceled, the name will destroy
863 * itself.
865 while (name != NULL) {
866 next_name = ISC_LIST_NEXT(name, plink);
867 INSIST(result == ISC_FALSE);
868 result = kill_name(&name,
869 DNS_EVENT_ADBSHUTDOWN);
870 name = next_name;
874 UNLOCK(&adb->namelocks[bucket]);
876 return (result);
880 * The ADB _MUST_ be locked before calling. Also, exit conditions must be
881 * checked after calling this function.
883 static isc_boolean_t
884 shutdown_entries(dns_adb_t *adb) {
885 int bucket;
886 isc_boolean_t result = ISC_FALSE;
887 dns_adbentry_t *entry;
888 dns_adbentry_t *next_entry;
890 for (bucket = 0; bucket < NBUCKETS; bucket++) {
891 LOCK(&adb->entrylocks[bucket]);
892 adb->entry_sd[bucket] = ISC_TRUE;
894 entry = ISC_LIST_HEAD(adb->entries[bucket]);
895 if (adb->entry_refcnt[bucket] == 0) {
897 * This bucket has no entries. We must decrement the
898 * irefcnt ourselves, since it will not be
899 * automatically triggered by an entry being unlinked.
901 result = dec_adb_irefcnt(adb);
902 } else {
904 * Run through the list. Cleanup any entries not
905 * associated with names, and which are not in use.
907 while (entry != NULL) {
908 next_entry = ISC_LIST_NEXT(entry, plink);
909 if (entry->refcnt == 0 &&
910 entry->expires != 0) {
911 result = unlink_entry(adb, entry);
912 free_adbentry(adb, &entry);
913 if (result)
914 result = dec_adb_irefcnt(adb);
916 entry = next_entry;
920 UNLOCK(&adb->entrylocks[bucket]);
922 return (result);
926 * Name bucket must be locked
928 static void
929 cancel_fetches_at_name(dns_adbname_t *name) {
930 if (NAME_FETCH_A(name))
931 dns_resolver_cancelfetch(name->fetch_a->fetch);
933 if (NAME_FETCH_AAAA(name))
934 dns_resolver_cancelfetch(name->fetch_aaaa->fetch);
938 * Assumes the name bucket is locked.
940 static isc_boolean_t
941 clean_namehooks(dns_adb_t *adb, dns_adbnamehooklist_t *namehooks) {
942 dns_adbentry_t *entry;
943 dns_adbnamehook_t *namehook;
944 int addr_bucket;
945 isc_boolean_t result = ISC_FALSE;
947 addr_bucket = DNS_ADB_INVALIDBUCKET;
948 namehook = ISC_LIST_HEAD(*namehooks);
949 while (namehook != NULL) {
950 INSIST(DNS_ADBNAMEHOOK_VALID(namehook));
953 * Clean up the entry if needed.
955 entry = namehook->entry;
956 if (entry != NULL) {
957 INSIST(DNS_ADBENTRY_VALID(entry));
959 if (addr_bucket != entry->lock_bucket) {
960 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
961 UNLOCK(&adb->entrylocks[addr_bucket]);
962 addr_bucket = entry->lock_bucket;
963 LOCK(&adb->entrylocks[addr_bucket]);
966 result = dec_entry_refcnt(adb, entry, ISC_FALSE);
970 * Free the namehook
972 namehook->entry = NULL;
973 ISC_LIST_UNLINK(*namehooks, namehook, plink);
974 free_adbnamehook(adb, &namehook);
976 namehook = ISC_LIST_HEAD(*namehooks);
979 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
980 UNLOCK(&adb->entrylocks[addr_bucket]);
981 return (result);
984 static void
985 clean_target(dns_adb_t *adb, dns_name_t *target) {
986 if (dns_name_countlabels(target) > 0) {
987 dns_name_free(target, adb->mctx);
988 dns_name_init(target, NULL);
992 static isc_result_t
993 set_target(dns_adb_t *adb, dns_name_t *name, dns_name_t *fname,
994 dns_rdataset_t *rdataset, dns_name_t *target)
996 isc_result_t result;
997 dns_namereln_t namereln;
998 unsigned int nlabels;
999 int order;
1000 dns_rdata_t rdata = DNS_RDATA_INIT;
1001 dns_fixedname_t fixed1, fixed2;
1002 dns_name_t *prefix, *new_target;
1004 REQUIRE(dns_name_countlabels(target) == 0);
1006 if (rdataset->type == dns_rdatatype_cname) {
1007 dns_rdata_cname_t cname;
1010 * Copy the CNAME's target into the target name.
1012 result = dns_rdataset_first(rdataset);
1013 if (result != ISC_R_SUCCESS)
1014 return (result);
1015 dns_rdataset_current(rdataset, &rdata);
1016 result = dns_rdata_tostruct(&rdata, &cname, NULL);
1017 if (result != ISC_R_SUCCESS)
1018 return (result);
1019 result = dns_name_dup(&cname.cname, adb->mctx, target);
1020 dns_rdata_freestruct(&cname);
1021 if (result != ISC_R_SUCCESS)
1022 return (result);
1023 } else {
1024 dns_rdata_dname_t dname;
1026 INSIST(rdataset->type == dns_rdatatype_dname);
1027 namereln = dns_name_fullcompare(name, fname, &order, &nlabels);
1028 INSIST(namereln == dns_namereln_subdomain);
1030 * Get the target name of the DNAME.
1032 result = dns_rdataset_first(rdataset);
1033 if (result != ISC_R_SUCCESS)
1034 return (result);
1035 dns_rdataset_current(rdataset, &rdata);
1036 result = dns_rdata_tostruct(&rdata, &dname, NULL);
1037 if (result != ISC_R_SUCCESS)
1038 return (result);
1040 * Construct the new target name.
1042 dns_fixedname_init(&fixed1);
1043 prefix = dns_fixedname_name(&fixed1);
1044 dns_fixedname_init(&fixed2);
1045 new_target = dns_fixedname_name(&fixed2);
1046 dns_name_split(name, nlabels, prefix, NULL);
1047 result = dns_name_concatenate(prefix, &dname.dname, new_target,
1048 NULL);
1049 dns_rdata_freestruct(&dname);
1050 if (result != ISC_R_SUCCESS)
1051 return (result);
1052 result = dns_name_dup(new_target, adb->mctx, target);
1053 if (result != ISC_R_SUCCESS)
1054 return (result);
1057 return (ISC_R_SUCCESS);
1061 * Assumes nothing is locked, since this is called by the client.
1063 static void
1064 event_free(isc_event_t *event) {
1065 dns_adbfind_t *find;
1067 INSIST(event != NULL);
1068 find = event->ev_destroy_arg;
1069 INSIST(DNS_ADBFIND_VALID(find));
1071 LOCK(&find->lock);
1072 find->flags |= FIND_EVENT_FREED;
1073 event->ev_destroy_arg = NULL;
1074 UNLOCK(&find->lock);
1078 * Assumes the name bucket is locked.
1080 static void
1081 clean_finds_at_name(dns_adbname_t *name, isc_eventtype_t evtype,
1082 unsigned int addrs)
1084 isc_event_t *ev;
1085 isc_task_t *task;
1086 dns_adbfind_t *find;
1087 dns_adbfind_t *next_find;
1088 isc_boolean_t process;
1089 unsigned int wanted, notify;
1091 DP(ENTER_LEVEL,
1092 "ENTER clean_finds_at_name, name %p, evtype %08x, addrs %08x",
1093 name, evtype, addrs);
1095 find = ISC_LIST_HEAD(name->finds);
1096 while (find != NULL) {
1097 LOCK(&find->lock);
1098 next_find = ISC_LIST_NEXT(find, plink);
1100 process = ISC_FALSE;
1101 wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
1102 notify = wanted & addrs;
1104 switch (evtype) {
1105 case DNS_EVENT_ADBMOREADDRESSES:
1106 DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBMOREADDRESSES");
1107 if ((notify) != 0) {
1108 find->flags &= ~addrs;
1109 process = ISC_TRUE;
1111 break;
1112 case DNS_EVENT_ADBNOMOREADDRESSES:
1113 DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBNOMOREADDRESSES");
1114 find->flags &= ~addrs;
1115 wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
1116 if (wanted == 0)
1117 process = ISC_TRUE;
1118 break;
1119 default:
1120 find->flags &= ~addrs;
1121 process = ISC_TRUE;
1124 if (process) {
1125 DP(DEF_LEVEL, "cfan: processing find %p", find);
1127 * Unlink the find from the name, letting the caller
1128 * call dns_adb_destroyfind() on it to clean it up
1129 * later.
1131 ISC_LIST_UNLINK(name->finds, find, plink);
1132 find->adbname = NULL;
1133 find->name_bucket = DNS_ADB_INVALIDBUCKET;
1135 INSIST(!FIND_EVENTSENT(find));
1137 ev = &find->event;
1138 task = ev->ev_sender;
1139 ev->ev_sender = find;
1140 find->result_v4 = find_err_map[name->fetch_err];
1141 find->result_v6 = find_err_map[name->fetch6_err];
1142 ev->ev_type = evtype;
1143 ev->ev_destroy = event_free;
1144 ev->ev_destroy_arg = find;
1146 DP(DEF_LEVEL,
1147 "sending event %p to task %p for find %p",
1148 ev, task, find);
1150 isc_task_sendanddetach(&task, (isc_event_t **)&ev);
1151 } else {
1152 DP(DEF_LEVEL, "cfan: skipping find %p", find);
1155 UNLOCK(&find->lock);
1156 find = next_find;
1159 DP(ENTER_LEVEL, "EXIT clean_finds_at_name, name %p", name);
1162 static inline void
1163 check_exit(dns_adb_t *adb) {
1164 isc_event_t *event;
1166 * The caller must be holding the adb lock.
1168 if (adb->shutting_down) {
1170 * If there aren't any external references either, we're
1171 * done. Send the control event to initiate shutdown.
1173 INSIST(!adb->cevent_sent); /* Sanity check. */
1174 event = &adb->cevent;
1175 isc_task_send(adb->task, &event);
1176 adb->cevent_sent = ISC_TRUE;
1180 static inline isc_boolean_t
1181 dec_adb_irefcnt(dns_adb_t *adb) {
1182 isc_event_t *event;
1183 isc_task_t *etask;
1184 isc_boolean_t result = ISC_FALSE;
1186 LOCK(&adb->reflock);
1188 INSIST(adb->irefcnt > 0);
1189 adb->irefcnt--;
1191 if (adb->irefcnt == 0) {
1192 event = ISC_LIST_HEAD(adb->whenshutdown);
1193 while (event != NULL) {
1194 ISC_LIST_UNLINK(adb->whenshutdown, event, ev_link);
1195 etask = event->ev_sender;
1196 event->ev_sender = adb;
1197 isc_task_sendanddetach(&etask, &event);
1198 event = ISC_LIST_HEAD(adb->whenshutdown);
1202 if (adb->irefcnt == 0 && adb->erefcnt == 0)
1203 result = ISC_TRUE;
1204 UNLOCK(&adb->reflock);
1205 return (result);
1208 static inline void
1209 inc_adb_irefcnt(dns_adb_t *adb) {
1210 LOCK(&adb->reflock);
1211 adb->irefcnt++;
1212 UNLOCK(&adb->reflock);
1215 static inline void
1216 inc_adb_erefcnt(dns_adb_t *adb) {
1217 LOCK(&adb->reflock);
1218 adb->erefcnt++;
1219 UNLOCK(&adb->reflock);
1222 static inline void
1223 inc_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
1224 int bucket;
1226 bucket = entry->lock_bucket;
1228 if (lock)
1229 LOCK(&adb->entrylocks[bucket]);
1231 entry->refcnt++;
1233 if (lock)
1234 UNLOCK(&adb->entrylocks[bucket]);
1237 static inline isc_boolean_t
1238 dec_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
1239 int bucket;
1240 isc_boolean_t destroy_entry;
1241 isc_boolean_t result = ISC_FALSE;
1243 bucket = entry->lock_bucket;
1245 if (lock)
1246 LOCK(&adb->entrylocks[bucket]);
1248 INSIST(entry->refcnt > 0);
1249 entry->refcnt--;
1251 destroy_entry = ISC_FALSE;
1252 if (entry->refcnt == 0 &&
1253 (adb->entry_sd[bucket] || entry->expires == 0 || adb->overmem ||
1254 (entry->flags & ENTRY_IS_DEAD) != 0)) {
1255 destroy_entry = ISC_TRUE;
1256 result = unlink_entry(adb, entry);
1259 if (lock)
1260 UNLOCK(&adb->entrylocks[bucket]);
1262 if (!destroy_entry)
1263 return (result);
1265 entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
1267 free_adbentry(adb, &entry);
1268 if (result)
1269 result = dec_adb_irefcnt(adb);
1271 return (result);
1274 static inline dns_adbname_t *
1275 new_adbname(dns_adb_t *adb, dns_name_t *dnsname) {
1276 dns_adbname_t *name;
1278 name = isc_mempool_get(adb->nmp);
1279 if (name == NULL)
1280 return (NULL);
1282 dns_name_init(&name->name, NULL);
1283 if (dns_name_dup(dnsname, adb->mctx, &name->name) != ISC_R_SUCCESS) {
1284 isc_mempool_put(adb->nmp, name);
1285 return (NULL);
1287 dns_name_init(&name->target, NULL);
1288 name->magic = DNS_ADBNAME_MAGIC;
1289 name->adb = adb;
1290 name->partial_result = 0;
1291 name->flags = 0;
1292 name->expire_v4 = INT_MAX;
1293 name->expire_v6 = INT_MAX;
1294 name->expire_target = INT_MAX;
1295 name->chains = 0;
1296 name->lock_bucket = DNS_ADB_INVALIDBUCKET;
1297 ISC_LIST_INIT(name->v4);
1298 ISC_LIST_INIT(name->v6);
1299 name->fetch_a = NULL;
1300 name->fetch_aaaa = NULL;
1301 name->fetch_err = FIND_ERR_UNEXPECTED;
1302 name->fetch6_err = FIND_ERR_UNEXPECTED;
1303 ISC_LIST_INIT(name->finds);
1304 ISC_LINK_INIT(name, plink);
1306 return (name);
1309 static inline void
1310 free_adbname(dns_adb_t *adb, dns_adbname_t **name) {
1311 dns_adbname_t *n;
1313 INSIST(name != NULL && DNS_ADBNAME_VALID(*name));
1314 n = *name;
1315 *name = NULL;
1317 INSIST(!NAME_HAS_V4(n));
1318 INSIST(!NAME_HAS_V6(n));
1319 INSIST(!NAME_FETCH(n));
1320 INSIST(ISC_LIST_EMPTY(n->finds));
1321 INSIST(!ISC_LINK_LINKED(n, plink));
1322 INSIST(n->lock_bucket == DNS_ADB_INVALIDBUCKET);
1323 INSIST(n->adb == adb);
1325 n->magic = 0;
1326 dns_name_free(&n->name, adb->mctx);
1328 isc_mempool_put(adb->nmp, n);
1331 static inline dns_adbnamehook_t *
1332 new_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry) {
1333 dns_adbnamehook_t *nh;
1335 nh = isc_mempool_get(adb->nhmp);
1336 if (nh == NULL)
1337 return (NULL);
1339 nh->magic = DNS_ADBNAMEHOOK_MAGIC;
1340 nh->entry = entry;
1341 ISC_LINK_INIT(nh, plink);
1343 return (nh);
1346 static inline void
1347 free_adbnamehook(dns_adb_t *adb, dns_adbnamehook_t **namehook) {
1348 dns_adbnamehook_t *nh;
1350 INSIST(namehook != NULL && DNS_ADBNAMEHOOK_VALID(*namehook));
1351 nh = *namehook;
1352 *namehook = NULL;
1354 INSIST(nh->entry == NULL);
1355 INSIST(!ISC_LINK_LINKED(nh, plink));
1357 nh->magic = 0;
1358 isc_mempool_put(adb->nhmp, nh);
1361 static inline dns_adblameinfo_t *
1362 new_adblameinfo(dns_adb_t *adb, dns_name_t *qname, dns_rdatatype_t qtype) {
1363 dns_adblameinfo_t *li;
1365 li = isc_mempool_get(adb->limp);
1366 if (li == NULL)
1367 return (NULL);
1369 dns_name_init(&li->qname, NULL);
1370 if (dns_name_dup(qname, adb->mctx, &li->qname) != ISC_R_SUCCESS) {
1371 isc_mempool_put(adb->limp, li);
1372 return (NULL);
1374 li->magic = DNS_ADBLAMEINFO_MAGIC;
1375 li->lame_timer = 0;
1376 li->qtype = qtype;
1377 ISC_LINK_INIT(li, plink);
1379 return (li);
1382 static inline void
1383 free_adblameinfo(dns_adb_t *adb, dns_adblameinfo_t **lameinfo) {
1384 dns_adblameinfo_t *li;
1386 INSIST(lameinfo != NULL && DNS_ADBLAMEINFO_VALID(*lameinfo));
1387 li = *lameinfo;
1388 *lameinfo = NULL;
1390 INSIST(!ISC_LINK_LINKED(li, plink));
1392 dns_name_free(&li->qname, adb->mctx);
1394 li->magic = 0;
1396 isc_mempool_put(adb->limp, li);
1399 static inline dns_adbentry_t *
1400 new_adbentry(dns_adb_t *adb) {
1401 dns_adbentry_t *e;
1402 isc_uint32_t r;
1404 e = isc_mempool_get(adb->emp);
1405 if (e == NULL)
1406 return (NULL);
1408 e->magic = DNS_ADBENTRY_MAGIC;
1409 e->lock_bucket = DNS_ADB_INVALIDBUCKET;
1410 e->refcnt = 0;
1411 e->flags = 0;
1412 isc_random_get(&r);
1413 e->srtt = (r & 0x1f) + 1;
1414 e->expires = 0;
1415 ISC_LIST_INIT(e->lameinfo);
1416 ISC_LINK_INIT(e, plink);
1418 return (e);
1421 static inline void
1422 free_adbentry(dns_adb_t *adb, dns_adbentry_t **entry) {
1423 dns_adbentry_t *e;
1424 dns_adblameinfo_t *li;
1426 INSIST(entry != NULL && DNS_ADBENTRY_VALID(*entry));
1427 e = *entry;
1428 *entry = NULL;
1430 INSIST(e->lock_bucket == DNS_ADB_INVALIDBUCKET);
1431 INSIST(e->refcnt == 0);
1432 INSIST(!ISC_LINK_LINKED(e, plink));
1434 e->magic = 0;
1436 li = ISC_LIST_HEAD(e->lameinfo);
1437 while (li != NULL) {
1438 ISC_LIST_UNLINK(e->lameinfo, li, plink);
1439 free_adblameinfo(adb, &li);
1440 li = ISC_LIST_HEAD(e->lameinfo);
1443 isc_mempool_put(adb->emp, e);
1446 static inline dns_adbfind_t *
1447 new_adbfind(dns_adb_t *adb) {
1448 dns_adbfind_t *h;
1449 isc_result_t result;
1451 h = isc_mempool_get(adb->ahmp);
1452 if (h == NULL)
1453 return (NULL);
1456 * Public members.
1458 h->magic = 0;
1459 h->adb = adb;
1460 h->partial_result = 0;
1461 h->options = 0;
1462 h->flags = 0;
1463 h->result_v4 = ISC_R_UNEXPECTED;
1464 h->result_v6 = ISC_R_UNEXPECTED;
1465 ISC_LINK_INIT(h, publink);
1466 ISC_LINK_INIT(h, plink);
1467 ISC_LIST_INIT(h->list);
1468 h->adbname = NULL;
1469 h->name_bucket = DNS_ADB_INVALIDBUCKET;
1472 * private members
1474 result = isc_mutex_init(&h->lock);
1475 if (result != ISC_R_SUCCESS) {
1476 isc_mempool_put(adb->ahmp, h);
1477 return (NULL);
1480 ISC_EVENT_INIT(&h->event, sizeof(isc_event_t), 0, 0, 0, NULL, NULL,
1481 NULL, NULL, h);
1483 inc_adb_irefcnt(adb);
1484 h->magic = DNS_ADBFIND_MAGIC;
1485 return (h);
1488 static inline dns_adbfetch_t *
1489 new_adbfetch(dns_adb_t *adb) {
1490 dns_adbfetch_t *f;
1492 f = isc_mempool_get(adb->afmp);
1493 if (f == NULL)
1494 return (NULL);
1496 f->magic = 0;
1497 f->fetch = NULL;
1499 dns_rdataset_init(&f->rdataset);
1501 f->magic = DNS_ADBFETCH_MAGIC;
1503 return (f);
1506 static inline void
1507 free_adbfetch(dns_adb_t *adb, dns_adbfetch_t **fetch) {
1508 dns_adbfetch_t *f;
1510 INSIST(fetch != NULL && DNS_ADBFETCH_VALID(*fetch));
1511 f = *fetch;
1512 *fetch = NULL;
1514 f->magic = 0;
1516 if (dns_rdataset_isassociated(&f->rdataset))
1517 dns_rdataset_disassociate(&f->rdataset);
1519 isc_mempool_put(adb->afmp, f);
1522 static inline isc_boolean_t
1523 free_adbfind(dns_adb_t *adb, dns_adbfind_t **findp) {
1524 dns_adbfind_t *find;
1526 INSIST(findp != NULL && DNS_ADBFIND_VALID(*findp));
1527 find = *findp;
1528 *findp = NULL;
1530 INSIST(!FIND_HAS_ADDRS(find));
1531 INSIST(!ISC_LINK_LINKED(find, publink));
1532 INSIST(!ISC_LINK_LINKED(find, plink));
1533 INSIST(find->name_bucket == DNS_ADB_INVALIDBUCKET);
1534 INSIST(find->adbname == NULL);
1536 find->magic = 0;
1538 DESTROYLOCK(&find->lock);
1539 isc_mempool_put(adb->ahmp, find);
1540 return (dec_adb_irefcnt(adb));
1544 * Copy bits from the entry into the newly allocated addrinfo. The entry
1545 * must be locked, and the reference count must be bumped up by one
1546 * if this function returns a valid pointer.
1548 static inline dns_adbaddrinfo_t *
1549 new_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry, in_port_t port) {
1550 dns_adbaddrinfo_t *ai;
1552 ai = isc_mempool_get(adb->aimp);
1553 if (ai == NULL)
1554 return (NULL);
1556 ai->magic = DNS_ADBADDRINFO_MAGIC;
1557 ai->sockaddr = entry->sockaddr;
1558 isc_sockaddr_setport(&ai->sockaddr, port);
1559 ai->srtt = entry->srtt;
1560 ai->flags = entry->flags;
1561 ai->entry = entry;
1562 ISC_LINK_INIT(ai, publink);
1564 return (ai);
1567 static inline void
1568 free_adbaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **ainfo) {
1569 dns_adbaddrinfo_t *ai;
1571 INSIST(ainfo != NULL && DNS_ADBADDRINFO_VALID(*ainfo));
1572 ai = *ainfo;
1573 *ainfo = NULL;
1575 INSIST(ai->entry == NULL);
1576 INSIST(!ISC_LINK_LINKED(ai, publink));
1578 ai->magic = 0;
1580 isc_mempool_put(adb->aimp, ai);
1584 * Search for the name. NOTE: The bucket is kept locked on both
1585 * success and failure, so it must always be unlocked by the caller!
1587 * On the first call to this function, *bucketp must be set to
1588 * DNS_ADB_INVALIDBUCKET.
1590 static inline dns_adbname_t *
1591 find_name_and_lock(dns_adb_t *adb, dns_name_t *name,
1592 unsigned int options, int *bucketp)
1594 dns_adbname_t *adbname;
1595 int bucket;
1597 bucket = dns_name_fullhash(name, ISC_FALSE) % NBUCKETS;
1599 if (*bucketp == DNS_ADB_INVALIDBUCKET) {
1600 LOCK(&adb->namelocks[bucket]);
1601 *bucketp = bucket;
1602 } else if (*bucketp != bucket) {
1603 UNLOCK(&adb->namelocks[*bucketp]);
1604 LOCK(&adb->namelocks[bucket]);
1605 *bucketp = bucket;
1608 adbname = ISC_LIST_HEAD(adb->names[bucket]);
1609 while (adbname != NULL) {
1610 if (!NAME_DEAD(adbname)) {
1611 if (dns_name_equal(name, &adbname->name)
1612 && GLUEHINT_OK(adbname, options)
1613 && STARTATZONE_MATCHES(adbname, options))
1614 return (adbname);
1616 adbname = ISC_LIST_NEXT(adbname, plink);
1619 return (NULL);
1623 * Search for the address. NOTE: The bucket is kept locked on both
1624 * success and failure, so it must always be unlocked by the caller.
1626 * On the first call to this function, *bucketp must be set to
1627 * DNS_ADB_INVALIDBUCKET. This will cause a lock to occur. On
1628 * later calls (within the same "lock path") it can be left alone, so
1629 * if this function is called multiple times locking is only done if
1630 * the bucket changes.
1632 static inline dns_adbentry_t *
1633 find_entry_and_lock(dns_adb_t *adb, isc_sockaddr_t *addr, int *bucketp,
1634 isc_stdtime_t now)
1636 dns_adbentry_t *entry, *entry_next;
1637 int bucket;
1639 bucket = isc_sockaddr_hash(addr, ISC_TRUE) % NBUCKETS;
1641 if (*bucketp == DNS_ADB_INVALIDBUCKET) {
1642 LOCK(&adb->entrylocks[bucket]);
1643 *bucketp = bucket;
1644 } else if (*bucketp != bucket) {
1645 UNLOCK(&adb->entrylocks[*bucketp]);
1646 LOCK(&adb->entrylocks[bucket]);
1647 *bucketp = bucket;
1650 /* Search the list, while cleaning up expired entries. */
1651 for (entry = ISC_LIST_HEAD(adb->entries[bucket]);
1652 entry != NULL;
1653 entry = entry_next) {
1654 entry_next = ISC_LIST_NEXT(entry, plink);
1655 (void)check_expire_entry(adb, &entry, now);
1656 if (entry != NULL &&
1657 isc_sockaddr_equal(addr, &entry->sockaddr)) {
1658 ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
1659 ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
1660 return (entry);
1664 return (NULL);
1668 * Entry bucket MUST be locked!
1670 static isc_boolean_t
1671 entry_is_lame(dns_adb_t *adb, dns_adbentry_t *entry, dns_name_t *qname,
1672 dns_rdatatype_t qtype, isc_stdtime_t now)
1674 dns_adblameinfo_t *li, *next_li;
1675 isc_boolean_t is_bad;
1677 is_bad = ISC_FALSE;
1679 li = ISC_LIST_HEAD(entry->lameinfo);
1680 if (li == NULL)
1681 return (ISC_FALSE);
1682 while (li != NULL) {
1683 next_li = ISC_LIST_NEXT(li, plink);
1686 * Has the entry expired?
1688 if (li->lame_timer < now) {
1689 ISC_LIST_UNLINK(entry->lameinfo, li, plink);
1690 free_adblameinfo(adb, &li);
1694 * Order tests from least to most expensive.
1696 * We do not break out of the main loop here as
1697 * we use the loop for house keeping.
1699 if (li != NULL && !is_bad && li->qtype == qtype &&
1700 dns_name_equal(qname, &li->qname))
1701 is_bad = ISC_TRUE;
1703 li = next_li;
1706 return (is_bad);
1709 static void
1710 copy_namehook_lists(dns_adb_t *adb, dns_adbfind_t *find, dns_name_t *qname,
1711 dns_rdatatype_t qtype, dns_adbname_t *name,
1712 isc_stdtime_t now)
1714 dns_adbnamehook_t *namehook;
1715 dns_adbaddrinfo_t *addrinfo;
1716 dns_adbentry_t *entry;
1717 int bucket;
1719 bucket = DNS_ADB_INVALIDBUCKET;
1721 if (find->options & DNS_ADBFIND_INET) {
1722 namehook = ISC_LIST_HEAD(name->v4);
1723 while (namehook != NULL) {
1724 entry = namehook->entry;
1725 bucket = entry->lock_bucket;
1726 LOCK(&adb->entrylocks[bucket]);
1728 if (!FIND_RETURNLAME(find)
1729 && entry_is_lame(adb, entry, qname, qtype, now)) {
1730 find->options |= DNS_ADBFIND_LAMEPRUNED;
1731 goto nextv4;
1733 addrinfo = new_adbaddrinfo(adb, entry, find->port);
1734 if (addrinfo == NULL) {
1735 find->partial_result |= DNS_ADBFIND_INET;
1736 goto out;
1739 * Found a valid entry. Add it to the find's list.
1741 inc_entry_refcnt(adb, entry, ISC_FALSE);
1742 ISC_LIST_APPEND(find->list, addrinfo, publink);
1743 addrinfo = NULL;
1744 nextv4:
1745 UNLOCK(&adb->entrylocks[bucket]);
1746 bucket = DNS_ADB_INVALIDBUCKET;
1747 namehook = ISC_LIST_NEXT(namehook, plink);
1751 if (find->options & DNS_ADBFIND_INET6) {
1752 namehook = ISC_LIST_HEAD(name->v6);
1753 while (namehook != NULL) {
1754 entry = namehook->entry;
1755 bucket = entry->lock_bucket;
1756 LOCK(&adb->entrylocks[bucket]);
1758 if (!FIND_RETURNLAME(find)
1759 && entry_is_lame(adb, entry, qname, qtype, now)) {
1760 find->options |= DNS_ADBFIND_LAMEPRUNED;
1761 goto nextv6;
1763 addrinfo = new_adbaddrinfo(adb, entry, find->port);
1764 if (addrinfo == NULL) {
1765 find->partial_result |= DNS_ADBFIND_INET6;
1766 goto out;
1769 * Found a valid entry. Add it to the find's list.
1771 inc_entry_refcnt(adb, entry, ISC_FALSE);
1772 ISC_LIST_APPEND(find->list, addrinfo, publink);
1773 addrinfo = NULL;
1774 nextv6:
1775 UNLOCK(&adb->entrylocks[bucket]);
1776 bucket = DNS_ADB_INVALIDBUCKET;
1777 namehook = ISC_LIST_NEXT(namehook, plink);
1781 out:
1782 if (bucket != DNS_ADB_INVALIDBUCKET)
1783 UNLOCK(&adb->entrylocks[bucket]);
1786 static void
1787 shutdown_task(isc_task_t *task, isc_event_t *ev) {
1788 dns_adb_t *adb;
1790 UNUSED(task);
1792 adb = ev->ev_arg;
1793 INSIST(DNS_ADB_VALID(adb));
1795 isc_event_free(&ev);
1797 * Wait for lock around check_exit() call to be released.
1799 LOCK(&adb->lock);
1800 UNLOCK(&adb->lock);
1801 destroy(adb);
1805 * Name bucket must be locked; adb may be locked; no other locks held.
1807 static isc_boolean_t
1808 check_expire_name(dns_adbname_t **namep, isc_stdtime_t now) {
1809 dns_adbname_t *name;
1810 isc_boolean_t result = ISC_FALSE;
1812 INSIST(namep != NULL && DNS_ADBNAME_VALID(*namep));
1813 name = *namep;
1815 if (NAME_HAS_V4(name) || NAME_HAS_V6(name))
1816 return (result);
1817 if (NAME_FETCH(name))
1818 return (result);
1819 if (!EXPIRE_OK(name->expire_v4, now))
1820 return (result);
1821 if (!EXPIRE_OK(name->expire_v6, now))
1822 return (result);
1823 if (!EXPIRE_OK(name->expire_target, now))
1824 return (result);
1827 * The name is empty. Delete it.
1829 result = kill_name(&name, DNS_EVENT_ADBEXPIRED);
1830 *namep = NULL;
1833 * Our caller, or one of its callers, will be calling check_exit() at
1834 * some point, so we don't need to do it here.
1836 return (result);
1840 * Examine the tail entry of the LRU list to see if it expires or is stale
1841 * (unused for some period); if so, the name entry will be freed. If the ADB
1842 * is in the overmem condition, the tail and the next to tail entries
1843 * will be unconditionally removed (unless they have an outstanding fetch).
1844 * We don't care about a race on 'overmem' at the risk of causing some
1845 * collateral damage or a small delay in starting cleanup, so we don't bother
1846 * to lock ADB (if it's not locked).
1848 * Name bucket must be locked; adb may be locked; no other locks held.
1850 static void
1851 check_stale_name(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
1852 int victims, max_victims;
1853 isc_boolean_t result;
1854 dns_adbname_t *victim, *next_victim;
1855 isc_boolean_t overmem = adb->overmem;
1856 int scans = 0;
1858 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
1860 max_victims = overmem ? 2 : 1;
1863 * We limit the number of scanned entries to 10 (arbitrary choice)
1864 * in order to avoid examining too many entries when there are many
1865 * tail entries that have fetches (this should be rare, but could
1866 * happen).
1868 victim = ISC_LIST_TAIL(adb->names[bucket]);
1869 for (victims = 0;
1870 victim != NULL && victims < max_victims && scans < 10;
1871 victim = next_victim) {
1872 INSIST(!NAME_DEAD(victim));
1873 scans++;
1874 next_victim = ISC_LIST_PREV(victim, plink);
1875 result = check_expire_name(&victim, now);
1876 if (victim == NULL) {
1877 victims++;
1878 goto next;
1881 if (!NAME_FETCH(victim) &&
1882 (overmem || victim->last_used + ADB_STALE_MARGIN <= now)) {
1883 RUNTIME_CHECK(kill_name(&victim,
1884 DNS_EVENT_ADBCANCELED) ==
1885 ISC_FALSE);
1886 victims++;
1889 next:
1890 if (!overmem)
1891 break;
1896 * Entry bucket must be locked; adb may be locked; no other locks held.
1898 static isc_boolean_t
1899 check_expire_entry(dns_adb_t *adb, dns_adbentry_t **entryp, isc_stdtime_t now)
1901 dns_adbentry_t *entry;
1902 isc_boolean_t result = ISC_FALSE;
1904 INSIST(entryp != NULL && DNS_ADBENTRY_VALID(*entryp));
1905 entry = *entryp;
1907 if (entry->refcnt != 0)
1908 return (result);
1910 if (entry->expires == 0 || entry->expires > now)
1911 return (result);
1914 * The entry is not in use. Delete it.
1916 DP(DEF_LEVEL, "killing entry %p", entry);
1917 INSIST(ISC_LINK_LINKED(entry, plink));
1918 result = unlink_entry(adb, entry);
1919 free_adbentry(adb, &entry);
1920 if (result)
1921 dec_adb_irefcnt(adb);
1922 *entryp = NULL;
1923 return (result);
1927 * ADB must be locked, and no other locks held.
1929 static isc_boolean_t
1930 cleanup_names(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
1931 dns_adbname_t *name;
1932 dns_adbname_t *next_name;
1933 isc_boolean_t result = ISC_FALSE;
1935 DP(CLEAN_LEVEL, "cleaning name bucket %d", bucket);
1937 LOCK(&adb->namelocks[bucket]);
1938 if (adb->name_sd[bucket]) {
1939 UNLOCK(&adb->namelocks[bucket]);
1940 return (result);
1943 name = ISC_LIST_HEAD(adb->names[bucket]);
1944 while (name != NULL) {
1945 next_name = ISC_LIST_NEXT(name, plink);
1946 INSIST(result == ISC_FALSE);
1947 result = check_expire_namehooks(name, now);
1948 if (!result)
1949 result = check_expire_name(&name, now);
1950 name = next_name;
1952 UNLOCK(&adb->namelocks[bucket]);
1953 return (result);
1957 * ADB must be locked, and no other locks held.
1959 static isc_boolean_t
1960 cleanup_entries(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
1961 dns_adbentry_t *entry, *next_entry;
1962 isc_boolean_t result = ISC_FALSE;
1964 DP(CLEAN_LEVEL, "cleaning entry bucket %d", bucket);
1966 LOCK(&adb->entrylocks[bucket]);
1967 entry = ISC_LIST_HEAD(adb->entries[bucket]);
1968 while (entry != NULL) {
1969 next_entry = ISC_LIST_NEXT(entry, plink);
1970 INSIST(result == ISC_FALSE);
1971 result = check_expire_entry(adb, &entry, now);
1972 entry = next_entry;
1974 UNLOCK(&adb->entrylocks[bucket]);
1975 return (result);
1978 static void
1979 destroy(dns_adb_t *adb) {
1980 adb->magic = 0;
1982 isc_task_detach(&adb->task);
1984 isc_mempool_destroy(&adb->nmp);
1985 isc_mempool_destroy(&adb->nhmp);
1986 isc_mempool_destroy(&adb->limp);
1987 isc_mempool_destroy(&adb->emp);
1988 isc_mempool_destroy(&adb->ahmp);
1989 isc_mempool_destroy(&adb->aimp);
1990 isc_mempool_destroy(&adb->afmp);
1992 DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
1993 DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
1995 DESTROYLOCK(&adb->reflock);
1996 DESTROYLOCK(&adb->lock);
1997 DESTROYLOCK(&adb->mplock);
1998 DESTROYLOCK(&adb->overmemlock);
2000 isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
2005 * Public functions.
2008 isc_result_t
2009 dns_adb_create(isc_mem_t *mem, dns_view_t *view, isc_timermgr_t *timermgr,
2010 isc_taskmgr_t *taskmgr, dns_adb_t **newadb)
2012 dns_adb_t *adb;
2013 isc_result_t result;
2014 int i;
2016 REQUIRE(mem != NULL);
2017 REQUIRE(view != NULL);
2018 REQUIRE(timermgr != NULL); /* this is actually unused */
2019 REQUIRE(taskmgr != NULL);
2020 REQUIRE(newadb != NULL && *newadb == NULL);
2022 UNUSED(timermgr);
2024 adb = isc_mem_get(mem, sizeof(dns_adb_t));
2025 if (adb == NULL)
2026 return (ISC_R_NOMEMORY);
2029 * Initialize things here that cannot fail, and especially things
2030 * that must be NULL for the error return to work properly.
2032 adb->magic = 0;
2033 adb->erefcnt = 1;
2034 adb->irefcnt = 0;
2035 adb->nmp = NULL;
2036 adb->nhmp = NULL;
2037 adb->limp = NULL;
2038 adb->emp = NULL;
2039 adb->ahmp = NULL;
2040 adb->aimp = NULL;
2041 adb->afmp = NULL;
2042 adb->task = NULL;
2043 adb->mctx = NULL;
2044 adb->view = view;
2045 adb->taskmgr = taskmgr;
2046 adb->next_cleanbucket = 0;
2047 ISC_EVENT_INIT(&adb->cevent, sizeof(adb->cevent), 0, NULL,
2048 DNS_EVENT_ADBCONTROL, shutdown_task, adb,
2049 adb, NULL, NULL);
2050 adb->cevent_sent = ISC_FALSE;
2051 adb->shutting_down = ISC_FALSE;
2052 adb->overmem = ISC_FALSE;
2053 ISC_LIST_INIT(adb->whenshutdown);
2055 isc_mem_attach(mem, &adb->mctx);
2057 result = isc_mutex_init(&adb->lock);
2058 if (result != ISC_R_SUCCESS)
2059 goto fail0b;
2061 result = isc_mutex_init(&adb->mplock);
2062 if (result != ISC_R_SUCCESS)
2063 goto fail0c;
2065 result = isc_mutex_init(&adb->reflock);
2066 if (result != ISC_R_SUCCESS)
2067 goto fail0d;
2069 result = isc_mutex_init(&adb->overmemlock);
2070 if (result != ISC_R_SUCCESS)
2071 goto fail0e;
2074 * Initialize the bucket locks for names and elements.
2075 * May as well initialize the list heads, too.
2077 result = isc_mutexblock_init(adb->namelocks, NBUCKETS);
2078 if (result != ISC_R_SUCCESS)
2079 goto fail1;
2080 for (i = 0; i < NBUCKETS; i++) {
2081 ISC_LIST_INIT(adb->names[i]);
2082 ISC_LIST_INIT(adb->deadnames[i]);
2083 adb->name_sd[i] = ISC_FALSE;
2084 adb->name_refcnt[i] = 0;
2085 adb->irefcnt++;
2087 for (i = 0; i < NBUCKETS; i++) {
2088 ISC_LIST_INIT(adb->entries[i]);
2089 ISC_LIST_INIT(adb->deadentries[i]);
2090 adb->entry_sd[i] = ISC_FALSE;
2091 adb->entry_refcnt[i] = 0;
2092 adb->irefcnt++;
2094 result = isc_mutexblock_init(adb->entrylocks, NBUCKETS);
2095 if (result != ISC_R_SUCCESS)
2096 goto fail2;
2099 * Memory pools
2101 #define MPINIT(t, p, n) do { \
2102 result = isc_mempool_create(mem, sizeof(t), &(p)); \
2103 if (result != ISC_R_SUCCESS) \
2104 goto fail3; \
2105 isc_mempool_setfreemax((p), FREE_ITEMS); \
2106 isc_mempool_setfillcount((p), FILL_COUNT); \
2107 isc_mempool_setname((p), n); \
2108 isc_mempool_associatelock((p), &adb->mplock); \
2109 } while (0)
2111 MPINIT(dns_adbname_t, adb->nmp, "adbname");
2112 MPINIT(dns_adbnamehook_t, adb->nhmp, "adbnamehook");
2113 MPINIT(dns_adblameinfo_t, adb->limp, "adblameinfo");
2114 MPINIT(dns_adbentry_t, adb->emp, "adbentry");
2115 MPINIT(dns_adbfind_t, adb->ahmp, "adbfind");
2116 MPINIT(dns_adbaddrinfo_t, adb->aimp, "adbaddrinfo");
2117 MPINIT(dns_adbfetch_t, adb->afmp, "adbfetch");
2119 #undef MPINIT
2122 * Allocate an internal task.
2124 result = isc_task_create(adb->taskmgr, 0, &adb->task);
2125 if (result != ISC_R_SUCCESS)
2126 goto fail3;
2127 isc_task_setname(adb->task, "ADB", adb);
2130 * Normal return.
2132 adb->magic = DNS_ADB_MAGIC;
2133 *newadb = adb;
2134 return (ISC_R_SUCCESS);
2136 fail3:
2137 if (adb->task != NULL)
2138 isc_task_detach(&adb->task);
2140 /* clean up entrylocks */
2141 DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
2143 fail2: /* clean up namelocks */
2144 DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
2146 fail1: /* clean up only allocated memory */
2147 if (adb->nmp != NULL)
2148 isc_mempool_destroy(&adb->nmp);
2149 if (adb->nhmp != NULL)
2150 isc_mempool_destroy(&adb->nhmp);
2151 if (adb->limp != NULL)
2152 isc_mempool_destroy(&adb->limp);
2153 if (adb->emp != NULL)
2154 isc_mempool_destroy(&adb->emp);
2155 if (adb->ahmp != NULL)
2156 isc_mempool_destroy(&adb->ahmp);
2157 if (adb->aimp != NULL)
2158 isc_mempool_destroy(&adb->aimp);
2159 if (adb->afmp != NULL)
2160 isc_mempool_destroy(&adb->afmp);
2162 DESTROYLOCK(&adb->overmemlock);
2163 fail0e:
2164 DESTROYLOCK(&adb->reflock);
2165 fail0d:
2166 DESTROYLOCK(&adb->mplock);
2167 fail0c:
2168 DESTROYLOCK(&adb->lock);
2169 fail0b:
2170 isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
2172 return (result);
2175 void
2176 dns_adb_attach(dns_adb_t *adb, dns_adb_t **adbx) {
2178 REQUIRE(DNS_ADB_VALID(adb));
2179 REQUIRE(adbx != NULL && *adbx == NULL);
2181 inc_adb_erefcnt(adb);
2182 *adbx = adb;
2185 void
2186 dns_adb_detach(dns_adb_t **adbx) {
2187 dns_adb_t *adb;
2188 isc_boolean_t need_exit_check;
2190 REQUIRE(adbx != NULL && DNS_ADB_VALID(*adbx));
2192 adb = *adbx;
2193 *adbx = NULL;
2195 INSIST(adb->erefcnt > 0);
2197 LOCK(&adb->reflock);
2198 adb->erefcnt--;
2199 need_exit_check = ISC_TF(adb->erefcnt == 0 && adb->irefcnt == 0);
2200 UNLOCK(&adb->reflock);
2202 if (need_exit_check) {
2203 LOCK(&adb->lock);
2204 INSIST(adb->shutting_down);
2205 check_exit(adb);
2206 UNLOCK(&adb->lock);
2210 void
2211 dns_adb_whenshutdown(dns_adb_t *adb, isc_task_t *task, isc_event_t **eventp) {
2212 isc_task_t *clone;
2213 isc_event_t *event;
2214 isc_boolean_t zeroirefcnt = ISC_FALSE;
2217 * Send '*eventp' to 'task' when 'adb' has shutdown.
2220 REQUIRE(DNS_ADB_VALID(adb));
2221 REQUIRE(eventp != NULL);
2223 event = *eventp;
2224 *eventp = NULL;
2226 LOCK(&adb->lock);
2228 LOCK(&adb->reflock);
2229 zeroirefcnt = ISC_TF(adb->irefcnt == 0);
2231 if (adb->shutting_down && zeroirefcnt &&
2232 isc_mempool_getallocated(adb->ahmp) == 0) {
2234 * We're already shutdown. Send the event.
2236 event->ev_sender = adb;
2237 isc_task_send(task, &event);
2238 } else {
2239 clone = NULL;
2240 isc_task_attach(task, &clone);
2241 event->ev_sender = clone;
2242 ISC_LIST_APPEND(adb->whenshutdown, event, ev_link);
2245 UNLOCK(&adb->reflock);
2246 UNLOCK(&adb->lock);
2249 void
2250 dns_adb_shutdown(dns_adb_t *adb) {
2251 isc_boolean_t need_check_exit;
2254 * Shutdown 'adb'.
2257 LOCK(&adb->lock);
2259 if (!adb->shutting_down) {
2260 adb->shutting_down = ISC_TRUE;
2261 isc_mem_setwater(adb->mctx, water, adb, 0, 0);
2262 need_check_exit = shutdown_names(adb);
2263 if (!need_check_exit)
2264 need_check_exit = shutdown_entries(adb);
2265 if (need_check_exit)
2266 check_exit(adb);
2269 UNLOCK(&adb->lock);
2272 isc_result_t
2273 dns_adb_createfind(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
2274 void *arg, dns_name_t *name, dns_name_t *qname,
2275 dns_rdatatype_t qtype, unsigned int options,
2276 isc_stdtime_t now, dns_name_t *target,
2277 in_port_t port, dns_adbfind_t **findp)
2279 dns_adbfind_t *find;
2280 dns_adbname_t *adbname;
2281 int bucket;
2282 isc_boolean_t want_event, start_at_zone, alias, have_address;
2283 isc_result_t result;
2284 unsigned int wanted_addresses;
2285 unsigned int wanted_fetches;
2286 unsigned int query_pending;
2288 REQUIRE(DNS_ADB_VALID(adb));
2289 if (task != NULL) {
2290 REQUIRE(action != NULL);
2292 REQUIRE(name != NULL);
2293 REQUIRE(qname != NULL);
2294 REQUIRE(findp != NULL && *findp == NULL);
2295 REQUIRE(target == NULL || dns_name_hasbuffer(target));
2297 REQUIRE((options & DNS_ADBFIND_ADDRESSMASK) != 0);
2299 result = ISC_R_UNEXPECTED;
2300 wanted_addresses = (options & DNS_ADBFIND_ADDRESSMASK);
2301 wanted_fetches = 0;
2302 query_pending = 0;
2303 want_event = ISC_FALSE;
2304 start_at_zone = ISC_FALSE;
2305 alias = ISC_FALSE;
2307 if (now == 0)
2308 isc_stdtime_get(&now);
2311 * XXXMLG Move this comment somewhere else!
2313 * Look up the name in our internal database.
2315 * Possibilities: Note that these are not always exclusive.
2317 * No name found. In this case, allocate a new name header and
2318 * an initial namehook or two. If any of these allocations
2319 * fail, clean up and return ISC_R_NOMEMORY.
2321 * Name found, valid addresses present. Allocate one addrinfo
2322 * structure for each found and append it to the linked list
2323 * of addresses for this header.
2325 * Name found, queries pending. In this case, if a task was
2326 * passed in, allocate a job id, attach it to the name's job
2327 * list and remember to tell the caller that there will be
2328 * more info coming later.
2331 find = new_adbfind(adb);
2332 if (find == NULL)
2333 return (ISC_R_NOMEMORY);
2335 find->port = port;
2338 * Remember what types of addresses we are interested in.
2340 find->options = options;
2341 find->flags |= wanted_addresses;
2342 if (FIND_WANTEVENT(find)) {
2343 REQUIRE(task != NULL);
2347 * Try to see if we know anything about this name at all.
2349 bucket = DNS_ADB_INVALIDBUCKET;
2350 adbname = find_name_and_lock(adb, name, find->options, &bucket);
2351 if (adb->name_sd[bucket]) {
2352 DP(DEF_LEVEL,
2353 "dns_adb_createfind: returning ISC_R_SHUTTINGDOWN");
2354 RUNTIME_CHECK(free_adbfind(adb, &find) == ISC_FALSE);
2355 result = ISC_R_SHUTTINGDOWN;
2356 goto out;
2360 * Nothing found. Allocate a new adbname structure for this name.
2362 if (adbname == NULL) {
2364 * See if there is any stale name at the end of list, and purge
2365 * it if so.
2367 check_stale_name(adb, bucket, now);
2369 adbname = new_adbname(adb, name);
2370 if (adbname == NULL) {
2371 RUNTIME_CHECK(free_adbfind(adb, &find) == ISC_FALSE);
2372 result = ISC_R_NOMEMORY;
2373 goto out;
2375 link_name(adb, bucket, adbname);
2376 if (FIND_HINTOK(find))
2377 adbname->flags |= NAME_HINT_OK;
2378 if (FIND_GLUEOK(find))
2379 adbname->flags |= NAME_GLUE_OK;
2380 if (FIND_STARTATZONE(find))
2381 adbname->flags |= NAME_STARTATZONE;
2382 } else {
2383 /* Move this name forward in the LRU list */
2384 ISC_LIST_UNLINK(adb->names[bucket], adbname, plink);
2385 ISC_LIST_PREPEND(adb->names[bucket], adbname, plink);
2387 adbname->last_used = now;
2390 * Expire old entries, etc.
2392 RUNTIME_CHECK(check_expire_namehooks(adbname, now) == ISC_FALSE);
2395 * Do we know that the name is an alias?
2397 if (!EXPIRE_OK(adbname->expire_target, now)) {
2399 * Yes, it is.
2401 DP(DEF_LEVEL,
2402 "dns_adb_createfind: name %p is an alias (cached)",
2403 adbname);
2404 alias = ISC_TRUE;
2405 goto post_copy;
2409 * Try to populate the name from the database and/or
2410 * start fetches. First try looking for an A record
2411 * in the database.
2413 if (!NAME_HAS_V4(adbname) && EXPIRE_OK(adbname->expire_v4, now)
2414 && WANT_INET(wanted_addresses)) {
2415 result = dbfind_name(adbname, now, dns_rdatatype_a);
2416 if (result == ISC_R_SUCCESS) {
2417 DP(DEF_LEVEL,
2418 "dns_adb_createfind: found A for name %p in db",
2419 adbname);
2420 goto v6;
2424 * Did we get a CNAME or DNAME?
2426 if (result == DNS_R_ALIAS) {
2427 DP(DEF_LEVEL,
2428 "dns_adb_createfind: name %p is an alias",
2429 adbname);
2430 alias = ISC_TRUE;
2431 goto post_copy;
2435 * If the name doesn't exist at all, don't bother with
2436 * v6 queries; they won't work.
2438 * If the name does exist but we didn't get our data, go
2439 * ahead and try AAAA.
2441 * If the result is neither of these, try a fetch for A.
2443 if (NXDOMAIN_RESULT(result))
2444 goto fetch;
2445 else if (NXRRSET_RESULT(result))
2446 goto v6;
2448 if (!NAME_FETCH_V4(adbname))
2449 wanted_fetches |= DNS_ADBFIND_INET;
2453 if (!NAME_HAS_V6(adbname) && EXPIRE_OK(adbname->expire_v6, now)
2454 && WANT_INET6(wanted_addresses)) {
2455 result = dbfind_name(adbname, now, dns_rdatatype_aaaa);
2456 if (result == ISC_R_SUCCESS) {
2457 DP(DEF_LEVEL,
2458 "dns_adb_createfind: found AAAA for name %p",
2459 adbname);
2460 goto fetch;
2464 * Did we get a CNAME or DNAME?
2466 if (result == DNS_R_ALIAS) {
2467 DP(DEF_LEVEL,
2468 "dns_adb_createfind: name %p is an alias",
2469 adbname);
2470 alias = ISC_TRUE;
2471 goto post_copy;
2475 * Listen to negative cache hints, and don't start
2476 * another query.
2478 if (NCACHE_RESULT(result) || AUTH_NX(result))
2479 goto fetch;
2481 if (!NAME_FETCH_V6(adbname))
2482 wanted_fetches |= DNS_ADBFIND_INET6;
2485 fetch:
2486 if ((WANT_INET(wanted_addresses) && NAME_HAS_V4(adbname)) ||
2487 (WANT_INET6(wanted_addresses) && NAME_HAS_V6(adbname)))
2488 have_address = ISC_TRUE;
2489 else
2490 have_address = ISC_FALSE;
2491 if (wanted_fetches != 0 &&
2492 ! (FIND_AVOIDFETCHES(find) && have_address)) {
2494 * We're missing at least one address family. Either the
2495 * caller hasn't instructed us to avoid fetches, or we don't
2496 * know anything about any of the address families that would
2497 * be acceptable so we have to launch fetches.
2500 if (FIND_STARTATZONE(find))
2501 start_at_zone = ISC_TRUE;
2504 * Start V4.
2506 if (WANT_INET(wanted_fetches) &&
2507 fetch_name(adbname, start_at_zone,
2508 dns_rdatatype_a) == ISC_R_SUCCESS) {
2509 DP(DEF_LEVEL,
2510 "dns_adb_createfind: started A fetch for name %p",
2511 adbname);
2515 * Start V6.
2517 if (WANT_INET6(wanted_fetches) &&
2518 fetch_name(adbname, start_at_zone,
2519 dns_rdatatype_aaaa) == ISC_R_SUCCESS) {
2520 DP(DEF_LEVEL,
2521 "dns_adb_createfind: "
2522 "started AAAA fetch for name %p",
2523 adbname);
2528 * Run through the name and copy out the bits we are
2529 * interested in.
2531 copy_namehook_lists(adb, find, qname, qtype, adbname, now);
2533 post_copy:
2534 if (NAME_FETCH_V4(adbname))
2535 query_pending |= DNS_ADBFIND_INET;
2536 if (NAME_FETCH_V6(adbname))
2537 query_pending |= DNS_ADBFIND_INET6;
2540 * Attach to the name's query list if there are queries
2541 * already running, and we have been asked to.
2543 want_event = ISC_TRUE;
2544 if (!FIND_WANTEVENT(find))
2545 want_event = ISC_FALSE;
2546 if (FIND_WANTEMPTYEVENT(find) && FIND_HAS_ADDRS(find))
2547 want_event = ISC_FALSE;
2548 if ((wanted_addresses & query_pending) == 0)
2549 want_event = ISC_FALSE;
2550 if (alias)
2551 want_event = ISC_FALSE;
2552 if (want_event) {
2553 find->adbname = adbname;
2554 find->name_bucket = bucket;
2555 ISC_LIST_APPEND(adbname->finds, find, plink);
2556 find->query_pending = (query_pending & wanted_addresses);
2557 find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
2558 find->flags |= (find->query_pending & DNS_ADBFIND_ADDRESSMASK);
2559 DP(DEF_LEVEL, "createfind: attaching find %p to adbname %p",
2560 find, adbname);
2561 } else {
2563 * Remove the flag so the caller knows there will never
2564 * be an event, and set internal flags to fake that
2565 * the event was sent and freed, so dns_adb_destroyfind() will
2566 * do the right thing.
2568 find->query_pending = (query_pending & wanted_addresses);
2569 find->options &= ~DNS_ADBFIND_WANTEVENT;
2570 find->flags |= (FIND_EVENT_SENT | FIND_EVENT_FREED);
2571 find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
2574 find->partial_result |= (adbname->partial_result & wanted_addresses);
2575 if (alias) {
2576 if (target != NULL) {
2577 result = dns_name_copy(&adbname->target, target, NULL);
2578 if (result != ISC_R_SUCCESS)
2579 goto out;
2581 result = DNS_R_ALIAS;
2582 } else
2583 result = ISC_R_SUCCESS;
2586 * Copy out error flags from the name structure into the find.
2588 find->result_v4 = find_err_map[adbname->fetch_err];
2589 find->result_v6 = find_err_map[adbname->fetch6_err];
2591 out:
2592 if (find != NULL) {
2593 *findp = find;
2595 if (want_event) {
2596 isc_task_t *taskp;
2598 INSIST((find->flags & DNS_ADBFIND_ADDRESSMASK) != 0);
2599 taskp = NULL;
2600 isc_task_attach(task, &taskp);
2601 find->event.ev_sender = taskp;
2602 find->event.ev_action = action;
2603 find->event.ev_arg = arg;
2607 UNLOCK(&adb->namelocks[bucket]);
2609 return (result);
2612 void
2613 dns_adb_destroyfind(dns_adbfind_t **findp) {
2614 dns_adbfind_t *find;
2615 dns_adbentry_t *entry;
2616 dns_adbaddrinfo_t *ai;
2617 int bucket;
2618 dns_adb_t *adb;
2620 REQUIRE(findp != NULL && DNS_ADBFIND_VALID(*findp));
2621 find = *findp;
2622 *findp = NULL;
2624 LOCK(&find->lock);
2626 DP(DEF_LEVEL, "dns_adb_destroyfind on find %p", find);
2628 adb = find->adb;
2629 REQUIRE(DNS_ADB_VALID(adb));
2631 REQUIRE(FIND_EVENTFREED(find));
2633 bucket = find->name_bucket;
2634 INSIST(bucket == DNS_ADB_INVALIDBUCKET);
2636 UNLOCK(&find->lock);
2639 * The find doesn't exist on any list, and nothing is locked.
2640 * Return the find to the memory pool, and decrement the adb's
2641 * reference count.
2643 ai = ISC_LIST_HEAD(find->list);
2644 while (ai != NULL) {
2645 ISC_LIST_UNLINK(find->list, ai, publink);
2646 entry = ai->entry;
2647 ai->entry = NULL;
2648 INSIST(DNS_ADBENTRY_VALID(entry));
2649 RUNTIME_CHECK(dec_entry_refcnt(adb, entry, ISC_TRUE) ==
2650 ISC_FALSE);
2651 free_adbaddrinfo(adb, &ai);
2652 ai = ISC_LIST_HEAD(find->list);
2656 * WARNING: The find is freed with the adb locked. This is done
2657 * to avoid a race condition where we free the find, some other
2658 * thread tests to see if it should be destroyed, detects it should
2659 * be, destroys it, and then we try to lock it for our check, but the
2660 * lock is destroyed.
2662 LOCK(&adb->lock);
2663 if (free_adbfind(adb, &find))
2664 check_exit(adb);
2665 UNLOCK(&adb->lock);
2668 void
2669 dns_adb_cancelfind(dns_adbfind_t *find) {
2670 isc_event_t *ev;
2671 isc_task_t *task;
2672 dns_adb_t *adb;
2673 int bucket;
2674 int unlock_bucket;
2676 LOCK(&find->lock);
2678 DP(DEF_LEVEL, "dns_adb_cancelfind on find %p", find);
2680 adb = find->adb;
2681 REQUIRE(DNS_ADB_VALID(adb));
2683 REQUIRE(!FIND_EVENTFREED(find));
2684 REQUIRE(FIND_WANTEVENT(find));
2686 bucket = find->name_bucket;
2687 if (bucket == DNS_ADB_INVALIDBUCKET)
2688 goto cleanup;
2691 * We need to get the adbname's lock to unlink the find.
2693 unlock_bucket = bucket;
2694 violate_locking_hierarchy(&find->lock, &adb->namelocks[unlock_bucket]);
2695 bucket = find->name_bucket;
2696 if (bucket != DNS_ADB_INVALIDBUCKET) {
2697 ISC_LIST_UNLINK(find->adbname->finds, find, plink);
2698 find->adbname = NULL;
2699 find->name_bucket = DNS_ADB_INVALIDBUCKET;
2701 UNLOCK(&adb->namelocks[unlock_bucket]);
2702 bucket = DNS_ADB_INVALIDBUCKET;
2704 cleanup:
2706 if (!FIND_EVENTSENT(find)) {
2707 ev = &find->event;
2708 task = ev->ev_sender;
2709 ev->ev_sender = find;
2710 ev->ev_type = DNS_EVENT_ADBCANCELED;
2711 ev->ev_destroy = event_free;
2712 ev->ev_destroy_arg = find;
2713 find->result_v4 = ISC_R_CANCELED;
2714 find->result_v6 = ISC_R_CANCELED;
2716 DP(DEF_LEVEL, "sending event %p to task %p for find %p",
2717 ev, task, find);
2719 isc_task_sendanddetach(&task, (isc_event_t **)&ev);
2722 UNLOCK(&find->lock);
2725 void
2726 dns_adb_dump(dns_adb_t *adb, FILE *f) {
2727 int i;
2728 isc_stdtime_t now;
2730 REQUIRE(DNS_ADB_VALID(adb));
2731 REQUIRE(f != NULL);
2734 * Lock the adb itself, lock all the name buckets, then lock all
2735 * the entry buckets. This should put the adb into a state where
2736 * nothing can change, so we can iterate through everything and
2737 * print at our leisure.
2740 LOCK(&adb->lock);
2741 isc_stdtime_get(&now);
2743 for (i = 0; i < NBUCKETS; i++)
2744 RUNTIME_CHECK(cleanup_names(adb, i, now) == ISC_FALSE);
2745 for (i = 0; i < NBUCKETS; i++)
2746 RUNTIME_CHECK(cleanup_entries(adb, i, now) == ISC_FALSE);
2748 dump_adb(adb, f, ISC_FALSE, now);
2749 UNLOCK(&adb->lock);
2752 static void
2753 dump_ttl(FILE *f, const char *legend, isc_stdtime_t value, isc_stdtime_t now) {
2754 if (value == INT_MAX)
2755 return;
2756 fprintf(f, " [%s TTL %d]", legend, value - now);
2759 static void
2760 dump_adb(dns_adb_t *adb, FILE *f, isc_boolean_t debug, isc_stdtime_t now) {
2761 int i;
2762 dns_adbname_t *name;
2763 dns_adbentry_t *entry;
2765 fprintf(f, ";\n; Address database dump\n;\n");
2766 if (debug)
2767 fprintf(f, "; addr %p, erefcnt %u, irefcnt %u, finds out %u\n",
2768 adb, adb->erefcnt, adb->irefcnt,
2769 isc_mempool_getallocated(adb->nhmp));
2771 for (i = 0; i < NBUCKETS; i++)
2772 LOCK(&adb->namelocks[i]);
2773 for (i = 0; i < NBUCKETS; i++)
2774 LOCK(&adb->entrylocks[i]);
2777 * Dump the names
2779 for (i = 0; i < NBUCKETS; i++) {
2780 name = ISC_LIST_HEAD(adb->names[i]);
2781 if (name == NULL)
2782 continue;
2783 if (debug)
2784 fprintf(f, "; bucket %d\n", i);
2785 for (;
2786 name != NULL;
2787 name = ISC_LIST_NEXT(name, plink))
2789 if (debug)
2790 fprintf(f, "; name %p (flags %08x)\n",
2791 name, name->flags);
2793 fprintf(f, "; ");
2794 print_dns_name(f, &name->name);
2795 if (dns_name_countlabels(&name->target) > 0) {
2796 fprintf(f, " alias ");
2797 print_dns_name(f, &name->target);
2800 dump_ttl(f, "v4", name->expire_v4, now);
2801 dump_ttl(f, "v6", name->expire_v6, now);
2802 dump_ttl(f, "target", name->expire_target, now);
2804 fprintf(f, " [v4 %s] [v6 %s]",
2805 errnames[name->fetch_err],
2806 errnames[name->fetch6_err]);
2808 fprintf(f, "\n");
2810 print_namehook_list(f, "v4", &name->v4, debug, now);
2811 print_namehook_list(f, "v6", &name->v6, debug, now);
2813 if (debug)
2814 print_fetch_list(f, name);
2815 if (debug)
2816 print_find_list(f, name);
2821 fprintf(f, ";\n; Unassociated entries\n;\n");
2823 for (i = 0; i < NBUCKETS; i++) {
2824 entry = ISC_LIST_HEAD(adb->entries[i]);
2825 while (entry != NULL) {
2826 if (entry->refcnt == 0)
2827 dump_entry(f, entry, debug, now);
2828 entry = ISC_LIST_NEXT(entry, plink);
2833 * Unlock everything
2835 for (i = 0; i < NBUCKETS; i++)
2836 UNLOCK(&adb->entrylocks[i]);
2837 for (i = 0; i < NBUCKETS; i++)
2838 UNLOCK(&adb->namelocks[i]);
2841 static void
2842 dump_entry(FILE *f, dns_adbentry_t *entry, isc_boolean_t debug,
2843 isc_stdtime_t now)
2845 char addrbuf[ISC_NETADDR_FORMATSIZE];
2846 char typebuf[DNS_RDATATYPE_FORMATSIZE];
2847 isc_netaddr_t netaddr;
2848 dns_adblameinfo_t *li;
2850 isc_netaddr_fromsockaddr(&netaddr, &entry->sockaddr);
2851 isc_netaddr_format(&netaddr, addrbuf, sizeof(addrbuf));
2853 if (debug)
2854 fprintf(f, ";\t%p: refcnt %u\n", entry, entry->refcnt);
2856 fprintf(f, ";\t%s [srtt %u] [flags %08x]",
2857 addrbuf, entry->srtt, entry->flags);
2858 if (entry->expires != 0)
2859 fprintf(f, " [ttl %d]", entry->expires - now);
2860 fprintf(f, "\n");
2861 for (li = ISC_LIST_HEAD(entry->lameinfo);
2862 li != NULL;
2863 li = ISC_LIST_NEXT(li, plink)) {
2864 fprintf(f, ";\t\t");
2865 print_dns_name(f, &li->qname);
2866 dns_rdatatype_format(li->qtype, typebuf, sizeof(typebuf));
2867 fprintf(f, " %s [lame TTL %d]\n", typebuf,
2868 li->lame_timer - now);
2872 void
2873 dns_adb_dumpfind(dns_adbfind_t *find, FILE *f) {
2874 char tmp[512];
2875 const char *tmpp;
2876 dns_adbaddrinfo_t *ai;
2877 isc_sockaddr_t *sa;
2880 * Not used currently, in the API Just In Case we
2881 * want to dump out the name and/or entries too.
2884 LOCK(&find->lock);
2886 fprintf(f, ";Find %p\n", find);
2887 fprintf(f, ";\tqpending %08x partial %08x options %08x flags %08x\n",
2888 find->query_pending, find->partial_result,
2889 find->options, find->flags);
2890 fprintf(f, ";\tname_bucket %d, name %p, event sender %p\n",
2891 find->name_bucket, find->adbname, find->event.ev_sender);
2893 ai = ISC_LIST_HEAD(find->list);
2894 if (ai != NULL)
2895 fprintf(f, "\tAddresses:\n");
2896 while (ai != NULL) {
2897 sa = &ai->sockaddr;
2898 switch (sa->type.sa.sa_family) {
2899 case AF_INET:
2900 tmpp = inet_ntop(AF_INET, &sa->type.sin.sin_addr,
2901 tmp, sizeof(tmp));
2902 break;
2903 case AF_INET6:
2904 tmpp = inet_ntop(AF_INET6, &sa->type.sin6.sin6_addr,
2905 tmp, sizeof(tmp));
2906 break;
2907 default:
2908 tmpp = "UnkFamily";
2911 if (tmpp == NULL)
2912 tmpp = "BadAddress";
2914 fprintf(f, "\t\tentry %p, flags %08x"
2915 " srtt %u addr %s\n",
2916 ai->entry, ai->flags, ai->srtt, tmpp);
2918 ai = ISC_LIST_NEXT(ai, publink);
2921 UNLOCK(&find->lock);
2924 static void
2925 print_dns_name(FILE *f, dns_name_t *name) {
2926 char buf[DNS_NAME_FORMATSIZE];
2928 INSIST(f != NULL);
2930 dns_name_format(name, buf, sizeof(buf));
2931 fprintf(f, "%s", buf);
2934 static void
2935 print_namehook_list(FILE *f, const char *legend, dns_adbnamehooklist_t *list,
2936 isc_boolean_t debug, isc_stdtime_t now)
2938 dns_adbnamehook_t *nh;
2940 for (nh = ISC_LIST_HEAD(*list);
2941 nh != NULL;
2942 nh = ISC_LIST_NEXT(nh, plink))
2944 if (debug)
2945 fprintf(f, ";\tHook(%s) %p\n", legend, nh);
2946 dump_entry(f, nh->entry, debug, now);
2950 static inline void
2951 print_fetch(FILE *f, dns_adbfetch_t *ft, const char *type) {
2952 fprintf(f, "\t\tFetch(%s): %p -> { fetch %p }\n",
2953 type, ft, ft->fetch);
2956 static void
2957 print_fetch_list(FILE *f, dns_adbname_t *n) {
2958 if (NAME_FETCH_A(n))
2959 print_fetch(f, n->fetch_a, "A");
2960 if (NAME_FETCH_AAAA(n))
2961 print_fetch(f, n->fetch_aaaa, "AAAA");
2964 static void
2965 print_find_list(FILE *f, dns_adbname_t *name) {
2966 dns_adbfind_t *find;
2968 find = ISC_LIST_HEAD(name->finds);
2969 while (find != NULL) {
2970 dns_adb_dumpfind(find, f);
2971 find = ISC_LIST_NEXT(find, plink);
2975 static isc_result_t
2976 dbfind_name(dns_adbname_t *adbname, isc_stdtime_t now, dns_rdatatype_t rdtype)
2978 isc_result_t result;
2979 dns_rdataset_t rdataset;
2980 dns_adb_t *adb;
2981 dns_fixedname_t foundname;
2982 dns_name_t *fname;
2984 INSIST(DNS_ADBNAME_VALID(adbname));
2985 adb = adbname->adb;
2986 INSIST(DNS_ADB_VALID(adb));
2987 INSIST(rdtype == dns_rdatatype_a || rdtype == dns_rdatatype_aaaa);
2989 dns_fixedname_init(&foundname);
2990 fname = dns_fixedname_name(&foundname);
2991 dns_rdataset_init(&rdataset);
2993 if (rdtype == dns_rdatatype_a)
2994 adbname->fetch_err = FIND_ERR_UNEXPECTED;
2995 else
2996 adbname->fetch6_err = FIND_ERR_UNEXPECTED;
2998 result = dns_view_find(adb->view, &adbname->name, rdtype, now,
2999 NAME_GLUEOK(adbname) ? DNS_DBFIND_GLUEOK : 0,
3000 ISC_TF(NAME_HINTOK(adbname)),
3001 NULL, NULL, fname, &rdataset, NULL);
3003 /* XXXVIX this switch statement is too sparse to gen a jump table. */
3004 switch (result) {
3005 case DNS_R_GLUE:
3006 case DNS_R_HINT:
3007 case ISC_R_SUCCESS:
3009 * Found in the database. Even if we can't copy out
3010 * any information, return success, or else a fetch
3011 * will be made, which will only make things worse.
3013 if (rdtype == dns_rdatatype_a)
3014 adbname->fetch_err = FIND_ERR_SUCCESS;
3015 else
3016 adbname->fetch6_err = FIND_ERR_SUCCESS;
3017 result = import_rdataset(adbname, &rdataset, now);
3018 break;
3019 case DNS_R_NXDOMAIN:
3020 case DNS_R_NXRRSET:
3022 * We're authoritative and the data doesn't exist.
3023 * Make up a negative cache entry so we don't ask again
3024 * for a while.
3026 * XXXRTH What time should we use? I'm putting in 30 seconds
3027 * for now.
3029 if (rdtype == dns_rdatatype_a) {
3030 adbname->expire_v4 = now + 30;
3031 DP(NCACHE_LEVEL,
3032 "adb name %p: Caching auth negative entry for A",
3033 adbname);
3034 if (result == DNS_R_NXDOMAIN)
3035 adbname->fetch_err = FIND_ERR_NXDOMAIN;
3036 else
3037 adbname->fetch_err = FIND_ERR_NXRRSET;
3038 } else {
3039 DP(NCACHE_LEVEL,
3040 "adb name %p: Caching auth negative entry for AAAA",
3041 adbname);
3042 adbname->expire_v6 = now + 30;
3043 if (result == DNS_R_NXDOMAIN)
3044 adbname->fetch6_err = FIND_ERR_NXDOMAIN;
3045 else
3046 adbname->fetch6_err = FIND_ERR_NXRRSET;
3048 break;
3049 case DNS_R_NCACHENXDOMAIN:
3050 case DNS_R_NCACHENXRRSET:
3052 * We found a negative cache entry. Pull the TTL from it
3053 * so we won't ask again for a while.
3055 rdataset.ttl = ttlclamp(rdataset.ttl);
3056 if (rdtype == dns_rdatatype_a) {
3057 adbname->expire_v4 = rdataset.ttl + now;
3058 if (result == DNS_R_NCACHENXDOMAIN)
3059 adbname->fetch_err = FIND_ERR_NXDOMAIN;
3060 else
3061 adbname->fetch_err = FIND_ERR_NXRRSET;
3062 DP(NCACHE_LEVEL,
3063 "adb name %p: Caching negative entry for A (ttl %u)",
3064 adbname, rdataset.ttl);
3065 } else {
3066 DP(NCACHE_LEVEL,
3067 "adb name %p: Caching negative entry for AAAA (ttl %u)",
3068 adbname, rdataset.ttl);
3069 adbname->expire_v6 = rdataset.ttl + now;
3070 if (result == DNS_R_NCACHENXDOMAIN)
3071 adbname->fetch6_err = FIND_ERR_NXDOMAIN;
3072 else
3073 adbname->fetch6_err = FIND_ERR_NXRRSET;
3075 break;
3076 case DNS_R_CNAME:
3077 case DNS_R_DNAME:
3079 * Clear the hint and glue flags, so this will match
3080 * more often.
3082 adbname->flags &= ~(DNS_ADBFIND_GLUEOK | DNS_ADBFIND_HINTOK);
3084 rdataset.ttl = ttlclamp(rdataset.ttl);
3085 clean_target(adb, &adbname->target);
3086 adbname->expire_target = INT_MAX;
3087 result = set_target(adb, &adbname->name, fname, &rdataset,
3088 &adbname->target);
3089 if (result == ISC_R_SUCCESS) {
3090 result = DNS_R_ALIAS;
3091 DP(NCACHE_LEVEL,
3092 "adb name %p: caching alias target",
3093 adbname);
3094 adbname->expire_target = rdataset.ttl + now;
3096 if (rdtype == dns_rdatatype_a)
3097 adbname->fetch_err = FIND_ERR_SUCCESS;
3098 else
3099 adbname->fetch6_err = FIND_ERR_SUCCESS;
3100 break;
3103 if (dns_rdataset_isassociated(&rdataset))
3104 dns_rdataset_disassociate(&rdataset);
3106 return (result);
3109 static void
3110 fetch_callback(isc_task_t *task, isc_event_t *ev) {
3111 dns_fetchevent_t *dev;
3112 dns_adbname_t *name;
3113 dns_adb_t *adb;
3114 dns_adbfetch_t *fetch;
3115 int bucket;
3116 isc_eventtype_t ev_status;
3117 isc_stdtime_t now;
3118 isc_result_t result;
3119 unsigned int address_type;
3120 isc_boolean_t want_check_exit = ISC_FALSE;
3122 UNUSED(task);
3124 INSIST(ev->ev_type == DNS_EVENT_FETCHDONE);
3125 dev = (dns_fetchevent_t *)ev;
3126 name = ev->ev_arg;
3127 INSIST(DNS_ADBNAME_VALID(name));
3128 adb = name->adb;
3129 INSIST(DNS_ADB_VALID(adb));
3131 bucket = name->lock_bucket;
3132 LOCK(&adb->namelocks[bucket]);
3134 INSIST(NAME_FETCH_A(name) || NAME_FETCH_AAAA(name));
3135 address_type = 0;
3136 if (NAME_FETCH_A(name) && (name->fetch_a->fetch == dev->fetch)) {
3137 address_type = DNS_ADBFIND_INET;
3138 fetch = name->fetch_a;
3139 name->fetch_a = NULL;
3140 } else if (NAME_FETCH_AAAA(name)
3141 && (name->fetch_aaaa->fetch == dev->fetch)) {
3142 address_type = DNS_ADBFIND_INET6;
3143 fetch = name->fetch_aaaa;
3144 name->fetch_aaaa = NULL;
3145 } else
3146 fetch = NULL;
3148 INSIST(address_type != 0 && fetch != NULL);
3150 dns_resolver_destroyfetch(&fetch->fetch);
3151 dev->fetch = NULL;
3153 ev_status = DNS_EVENT_ADBNOMOREADDRESSES;
3156 * Cleanup things we don't care about.
3158 if (dev->node != NULL)
3159 dns_db_detachnode(dev->db, &dev->node);
3160 if (dev->db != NULL)
3161 dns_db_detach(&dev->db);
3164 * If this name is marked as dead, clean up, throwing away
3165 * potentially good data.
3167 if (NAME_DEAD(name)) {
3168 free_adbfetch(adb, &fetch);
3169 isc_event_free(&ev);
3171 want_check_exit = kill_name(&name, DNS_EVENT_ADBCANCELED);
3173 UNLOCK(&adb->namelocks[bucket]);
3175 if (want_check_exit) {
3176 LOCK(&adb->lock);
3177 check_exit(adb);
3178 UNLOCK(&adb->lock);
3181 return;
3184 isc_stdtime_get(&now);
3187 * If we got a negative cache response, remember it.
3189 if (NCACHE_RESULT(dev->result)) {
3190 dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
3191 if (address_type == DNS_ADBFIND_INET) {
3192 DP(NCACHE_LEVEL, "adb fetch name %p: "
3193 "caching negative entry for A (ttl %u)",
3194 name, dev->rdataset->ttl);
3195 name->expire_v4 = ISC_MIN(name->expire_v4,
3196 dev->rdataset->ttl + now);
3197 if (dev->result == DNS_R_NCACHENXDOMAIN)
3198 name->fetch_err = FIND_ERR_NXDOMAIN;
3199 else
3200 name->fetch_err = FIND_ERR_NXRRSET;
3201 inc_stats(adb, dns_resstatscounter_gluefetchv4fail);
3202 } else {
3203 DP(NCACHE_LEVEL, "adb fetch name %p: "
3204 "caching negative entry for AAAA (ttl %u)",
3205 name, dev->rdataset->ttl);
3206 name->expire_v6 = ISC_MIN(name->expire_v6,
3207 dev->rdataset->ttl + now);
3208 if (dev->result == DNS_R_NCACHENXDOMAIN)
3209 name->fetch6_err = FIND_ERR_NXDOMAIN;
3210 else
3211 name->fetch6_err = FIND_ERR_NXRRSET;
3212 inc_stats(adb, dns_resstatscounter_gluefetchv6fail);
3214 goto out;
3218 * Handle CNAME/DNAME.
3220 if (dev->result == DNS_R_CNAME || dev->result == DNS_R_DNAME) {
3221 dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
3222 clean_target(adb, &name->target);
3223 name->expire_target = INT_MAX;
3224 result = set_target(adb, &name->name,
3225 dns_fixedname_name(&dev->foundname),
3226 dev->rdataset,
3227 &name->target);
3228 if (result == ISC_R_SUCCESS) {
3229 DP(NCACHE_LEVEL,
3230 "adb fetch name %p: caching alias target",
3231 name);
3232 name->expire_target = dev->rdataset->ttl + now;
3234 goto check_result;
3238 * Did we get back junk? If so, and there are no more fetches
3239 * sitting out there, tell all the finds about it.
3241 if (dev->result != ISC_R_SUCCESS) {
3242 char buf[DNS_NAME_FORMATSIZE];
3244 dns_name_format(&name->name, buf, sizeof(buf));
3245 DP(DEF_LEVEL, "adb: fetch of '%s' %s failed: %s",
3246 buf, address_type == DNS_ADBFIND_INET ? "A" : "AAAA",
3247 dns_result_totext(dev->result));
3248 /* XXXMLG Don't pound on bad servers. */
3249 if (address_type == DNS_ADBFIND_INET) {
3250 name->expire_v4 = ISC_MIN(name->expire_v4, now + 300);
3251 name->fetch_err = FIND_ERR_FAILURE;
3252 inc_stats(adb, dns_resstatscounter_gluefetchv4fail);
3253 } else {
3254 name->expire_v6 = ISC_MIN(name->expire_v6, now + 300);
3255 name->fetch6_err = FIND_ERR_FAILURE;
3256 inc_stats(adb, dns_resstatscounter_gluefetchv6fail);
3258 goto out;
3262 * We got something potentially useful.
3264 result = import_rdataset(name, &fetch->rdataset, now);
3266 check_result:
3267 if (result == ISC_R_SUCCESS) {
3268 ev_status = DNS_EVENT_ADBMOREADDRESSES;
3269 if (address_type == DNS_ADBFIND_INET)
3270 name->fetch_err = FIND_ERR_SUCCESS;
3271 else
3272 name->fetch6_err = FIND_ERR_SUCCESS;
3275 out:
3276 free_adbfetch(adb, &fetch);
3277 isc_event_free(&ev);
3279 clean_finds_at_name(name, ev_status, address_type);
3281 UNLOCK(&adb->namelocks[bucket]);
3284 static isc_result_t
3285 fetch_name(dns_adbname_t *adbname,
3286 isc_boolean_t start_at_zone,
3287 dns_rdatatype_t type)
3289 isc_result_t result;
3290 dns_adbfetch_t *fetch = NULL;
3291 dns_adb_t *adb;
3292 dns_fixedname_t fixed;
3293 dns_name_t *name;
3294 dns_rdataset_t rdataset;
3295 dns_rdataset_t *nameservers;
3296 unsigned int options;
3298 INSIST(DNS_ADBNAME_VALID(adbname));
3299 adb = adbname->adb;
3300 INSIST(DNS_ADB_VALID(adb));
3302 INSIST((type == dns_rdatatype_a && !NAME_FETCH_V4(adbname)) ||
3303 (type == dns_rdatatype_aaaa && !NAME_FETCH_V6(adbname)));
3305 adbname->fetch_err = FIND_ERR_NOTFOUND;
3307 name = NULL;
3308 nameservers = NULL;
3309 dns_rdataset_init(&rdataset);
3311 options = DNS_FETCHOPT_NOVALIDATE;
3312 if (start_at_zone) {
3313 DP(ENTER_LEVEL,
3314 "fetch_name: starting at zone for name %p",
3315 adbname);
3316 dns_fixedname_init(&fixed);
3317 name = dns_fixedname_name(&fixed);
3318 result = dns_view_findzonecut2(adb->view, &adbname->name, name,
3319 0, 0, ISC_TRUE, ISC_FALSE,
3320 &rdataset, NULL);
3321 if (result != ISC_R_SUCCESS && result != DNS_R_HINT)
3322 goto cleanup;
3323 nameservers = &rdataset;
3324 options |= DNS_FETCHOPT_UNSHARED;
3327 fetch = new_adbfetch(adb);
3328 if (fetch == NULL) {
3329 result = ISC_R_NOMEMORY;
3330 goto cleanup;
3333 result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
3334 type, name, nameservers, NULL,
3335 options, adb->task, fetch_callback,
3336 adbname, &fetch->rdataset, NULL,
3337 &fetch->fetch);
3338 if (result != ISC_R_SUCCESS)
3339 goto cleanup;
3341 if (type == dns_rdatatype_a) {
3342 adbname->fetch_a = fetch;
3343 inc_stats(adb, dns_resstatscounter_gluefetchv4);
3344 } else {
3345 adbname->fetch_aaaa = fetch;
3346 inc_stats(adb, dns_resstatscounter_gluefetchv6);
3348 fetch = NULL; /* Keep us from cleaning this up below. */
3350 cleanup:
3351 if (fetch != NULL)
3352 free_adbfetch(adb, &fetch);
3353 if (dns_rdataset_isassociated(&rdataset))
3354 dns_rdataset_disassociate(&rdataset);
3356 return (result);
3360 * XXXMLG Needs to take a find argument and an address info, no zone or adb,
3361 * since these can be extracted from the find itself.
3363 isc_result_t
3364 dns_adb_marklame(dns_adb_t *adb, dns_adbaddrinfo_t *addr, dns_name_t *qname,
3365 dns_rdatatype_t qtype, isc_stdtime_t expire_time)
3367 dns_adblameinfo_t *li;
3368 int bucket;
3369 isc_result_t result = ISC_R_SUCCESS;
3371 REQUIRE(DNS_ADB_VALID(adb));
3372 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3373 REQUIRE(qname != NULL);
3375 bucket = addr->entry->lock_bucket;
3376 LOCK(&adb->entrylocks[bucket]);
3377 li = ISC_LIST_HEAD(addr->entry->lameinfo);
3378 while (li != NULL &&
3379 (li->qtype != qtype || !dns_name_equal(qname, &li->qname)))
3380 li = ISC_LIST_NEXT(li, plink);
3381 if (li != NULL) {
3382 if (expire_time > li->lame_timer)
3383 li->lame_timer = expire_time;
3384 goto unlock;
3386 li = new_adblameinfo(adb, qname, qtype);
3387 if (li == NULL) {
3388 result = ISC_R_NOMEMORY;
3389 goto unlock;
3392 li->lame_timer = expire_time;
3394 ISC_LIST_PREPEND(addr->entry->lameinfo, li, plink);
3395 unlock:
3396 UNLOCK(&adb->entrylocks[bucket]);
3398 return (result);
3401 void
3402 dns_adb_adjustsrtt(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
3403 unsigned int rtt, unsigned int factor)
3405 int bucket;
3406 unsigned int new_srtt;
3407 isc_stdtime_t now;
3409 REQUIRE(DNS_ADB_VALID(adb));
3410 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3411 REQUIRE(factor <= 10);
3413 bucket = addr->entry->lock_bucket;
3414 LOCK(&adb->entrylocks[bucket]);
3416 if (factor == DNS_ADB_RTTADJAGE)
3417 new_srtt = addr->entry->srtt * 98 / 100;
3418 else
3419 new_srtt = (addr->entry->srtt / 10 * factor)
3420 + (rtt / 10 * (10 - factor));
3422 addr->entry->srtt = new_srtt;
3423 addr->srtt = new_srtt;
3425 isc_stdtime_get(&now);
3426 addr->entry->expires = now + ADB_ENTRY_WINDOW;
3428 UNLOCK(&adb->entrylocks[bucket]);
3431 void
3432 dns_adb_changeflags(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
3433 unsigned int bits, unsigned int mask)
3435 int bucket;
3437 REQUIRE(DNS_ADB_VALID(adb));
3438 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3440 bucket = addr->entry->lock_bucket;
3441 LOCK(&adb->entrylocks[bucket]);
3443 addr->entry->flags = (addr->entry->flags & ~mask) | (bits & mask);
3445 * Note that we do not update the other bits in addr->flags with
3446 * the most recent values from addr->entry->flags.
3448 addr->flags = (addr->flags & ~mask) | (bits & mask);
3450 UNLOCK(&adb->entrylocks[bucket]);
3453 isc_result_t
3454 dns_adb_findaddrinfo(dns_adb_t *adb, isc_sockaddr_t *sa,
3455 dns_adbaddrinfo_t **addrp, isc_stdtime_t now)
3457 int bucket;
3458 dns_adbentry_t *entry;
3459 dns_adbaddrinfo_t *addr;
3460 isc_result_t result;
3461 in_port_t port;
3463 REQUIRE(DNS_ADB_VALID(adb));
3464 REQUIRE(addrp != NULL && *addrp == NULL);
3466 UNUSED(now);
3468 result = ISC_R_SUCCESS;
3469 bucket = DNS_ADB_INVALIDBUCKET;
3470 entry = find_entry_and_lock(adb, sa, &bucket, now);
3471 if (adb->entry_sd[bucket]) {
3472 result = ISC_R_SHUTTINGDOWN;
3473 goto unlock;
3475 if (entry == NULL) {
3477 * We don't know anything about this address.
3479 entry = new_adbentry(adb);
3480 if (entry == NULL) {
3481 result = ISC_R_NOMEMORY;
3482 goto unlock;
3484 entry->sockaddr = *sa;
3485 link_entry(adb, bucket, entry);
3486 DP(ENTER_LEVEL, "findaddrinfo: new entry %p", entry);
3487 } else
3488 DP(ENTER_LEVEL, "findaddrinfo: found entry %p", entry);
3490 port = isc_sockaddr_getport(sa);
3491 addr = new_adbaddrinfo(adb, entry, port);
3492 if (addr == NULL) {
3493 result = ISC_R_NOMEMORY;
3494 } else {
3495 inc_entry_refcnt(adb, entry, ISC_FALSE);
3496 *addrp = addr;
3499 unlock:
3500 UNLOCK(&adb->entrylocks[bucket]);
3502 return (result);
3505 void
3506 dns_adb_freeaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **addrp) {
3507 dns_adbaddrinfo_t *addr;
3508 dns_adbentry_t *entry;
3509 int bucket;
3510 isc_stdtime_t now;
3511 isc_boolean_t want_check_exit = ISC_FALSE;
3513 REQUIRE(DNS_ADB_VALID(adb));
3514 REQUIRE(addrp != NULL);
3515 addr = *addrp;
3516 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3517 entry = addr->entry;
3518 REQUIRE(DNS_ADBENTRY_VALID(entry));
3520 isc_stdtime_get(&now);
3522 *addrp = NULL;
3524 bucket = addr->entry->lock_bucket;
3525 LOCK(&adb->entrylocks[bucket]);
3527 entry->expires = now + ADB_ENTRY_WINDOW;
3529 want_check_exit = dec_entry_refcnt(adb, entry, ISC_FALSE);
3531 UNLOCK(&adb->entrylocks[bucket]);
3533 addr->entry = NULL;
3534 free_adbaddrinfo(adb, &addr);
3536 if (want_check_exit) {
3537 LOCK(&adb->lock);
3538 check_exit(adb);
3539 UNLOCK(&adb->lock);
3543 void
3544 dns_adb_flush(dns_adb_t *adb) {
3545 unsigned int i;
3547 INSIST(DNS_ADB_VALID(adb));
3549 LOCK(&adb->lock);
3552 * Call our cleanup routines.
3554 for (i = 0; i < NBUCKETS; i++)
3555 RUNTIME_CHECK(cleanup_names(adb, i, INT_MAX) == ISC_FALSE);
3556 for (i = 0; i < NBUCKETS; i++)
3557 RUNTIME_CHECK(cleanup_entries(adb, i, INT_MAX) == ISC_FALSE);
3559 #ifdef DUMP_ADB_AFTER_CLEANING
3560 dump_adb(adb, stdout, ISC_TRUE, INT_MAX);
3561 #endif
3563 UNLOCK(&adb->lock);
3566 void
3567 dns_adb_flushname(dns_adb_t *adb, dns_name_t *name) {
3568 dns_adbname_t *adbname;
3569 dns_adbname_t *nextname;
3570 int bucket;
3572 INSIST(DNS_ADB_VALID(adb));
3574 LOCK(&adb->lock);
3575 bucket = dns_name_hash(name, ISC_FALSE) % NBUCKETS;
3576 LOCK(&adb->namelocks[bucket]);
3577 adbname = ISC_LIST_HEAD(adb->names[bucket]);
3578 while (adbname != NULL) {
3579 nextname = ISC_LIST_NEXT(adbname, plink);
3580 if (!NAME_DEAD(adbname) &&
3581 dns_name_equal(name, &adbname->name)) {
3582 RUNTIME_CHECK(kill_name(&adbname,
3583 DNS_EVENT_ADBCANCELED) ==
3584 ISC_FALSE);
3586 adbname = nextname;
3588 UNLOCK(&adb->namelocks[bucket]);
3589 UNLOCK(&adb->lock);
3592 static void
3593 water(void *arg, int mark) {
3594 dns_adb_t *adb = arg;
3595 isc_boolean_t overmem = ISC_TF(mark == ISC_MEM_HIWATER);
3597 REQUIRE(DNS_ADB_VALID(adb));
3599 DP(ISC_LOG_DEBUG(1),
3600 "adb reached %s water mark", overmem ? "high" : "low");
3603 * We can't use adb->lock as there is potential for water
3604 * to be called when adb->lock is held.
3606 LOCK(&adb->overmemlock);
3607 if (adb->overmem != overmem) {
3608 adb->overmem = overmem;
3609 isc_mem_waterack(adb->mctx, mark);
3611 UNLOCK(&adb->overmemlock);
3614 void
3615 dns_adb_setadbsize(dns_adb_t *adb, isc_uint32_t size) {
3616 isc_uint32_t hiwater;
3617 isc_uint32_t lowater;
3619 INSIST(DNS_ADB_VALID(adb));
3621 if (size != 0 && size < DNS_ADB_MINADBSIZE)
3622 size = DNS_ADB_MINADBSIZE;
3624 hiwater = size - (size >> 3); /* Approximately 7/8ths. */
3625 lowater = size - (size >> 2); /* Approximately 3/4ths. */
3627 if (size == 0 || hiwater == 0 || lowater == 0)
3628 isc_mem_setwater(adb->mctx, water, adb, 0, 0);
3629 else
3630 isc_mem_setwater(adb->mctx, water, adb, hiwater, lowater);