Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / adb.c
blob69cfadd15c7ea9c8eb9839bc22ef1844a77fca64
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: adb.c,v 1.247 2009/02/03 22:33:13 jinmei Exp */
22 /*! \file
24 * \note
25 * In finds, if task == NULL, no events will be generated, and no events
26 * have been sent. If task != NULL but taskaction == NULL, an event has been
27 * posted but not yet freed. If neither are NULL, no event was posted.
31 #include <config.h>
33 #include <limits.h>
35 #include <isc/mutexblock.h>
36 #include <isc/netaddr.h>
37 #include <isc/random.h>
38 #include <isc/stats.h>
39 #include <isc/string.h> /* Required for HP/UX (and others?) */
40 #include <isc/task.h>
41 #include <isc/util.h>
43 #include <dns/adb.h>
44 #include <dns/db.h>
45 #include <dns/events.h>
46 #include <dns/log.h>
47 #include <dns/rdata.h>
48 #include <dns/rdataset.h>
49 #include <dns/rdatastruct.h>
50 #include <dns/rdatatype.h>
51 #include <dns/resolver.h>
52 #include <dns/result.h>
53 #include <dns/stats.h>
55 #define DNS_ADB_MAGIC ISC_MAGIC('D', 'a', 'd', 'b')
56 #define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
57 #define DNS_ADBNAME_MAGIC ISC_MAGIC('a', 'd', 'b', 'N')
58 #define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
59 #define DNS_ADBNAMEHOOK_MAGIC ISC_MAGIC('a', 'd', 'N', 'H')
60 #define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
61 #define DNS_ADBLAMEINFO_MAGIC ISC_MAGIC('a', 'd', 'b', 'Z')
62 #define DNS_ADBLAMEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBLAMEINFO_MAGIC)
63 #define DNS_ADBENTRY_MAGIC ISC_MAGIC('a', 'd', 'b', 'E')
64 #define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
65 #define DNS_ADBFETCH_MAGIC ISC_MAGIC('a', 'd', 'F', '4')
66 #define DNS_ADBFETCH_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
67 #define DNS_ADBFETCH6_MAGIC ISC_MAGIC('a', 'd', 'F', '6')
68 #define DNS_ADBFETCH6_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH6_MAGIC)
70 /*!
71 * The number of buckets needs to be a prime (for good hashing).
73 * XXXRTH How many buckets do we need?
75 #define NBUCKETS 1009 /*%< how many buckets for names/addrs */
77 /*!
78 * For type 3 negative cache entries, we will remember that the address is
79 * broken for this long. XXXMLG This is also used for actual addresses, too.
80 * The intent is to keep us from constantly asking about A/AAAA records
81 * if the zone has extremely low TTLs.
83 #define ADB_CACHE_MINIMUM 10 /*%< seconds */
84 #define ADB_CACHE_MAXIMUM 86400 /*%< seconds (86400 = 24 hours) */
85 #define ADB_ENTRY_WINDOW 1800 /*%< seconds */
87 /*%
88 * The period in seconds after which an ADB name entry is regarded as stale
89 * and forced to be cleaned up.
90 * TODO: This should probably be configurable at run-time.
92 #ifndef ADB_STALE_MARGIN
93 #define ADB_STALE_MARGIN 1800
94 #endif
96 #define FREE_ITEMS 64 /*%< free count for memory pools */
97 #define FILL_COUNT 16 /*%< fill count for memory pools */
99 #define DNS_ADB_INVALIDBUCKET (-1) /*%< invalid bucket address */
101 #define DNS_ADB_MINADBSIZE (1024*1024) /*%< 1 Megabyte */
103 typedef ISC_LIST(dns_adbname_t) dns_adbnamelist_t;
104 typedef struct dns_adbnamehook dns_adbnamehook_t;
105 typedef ISC_LIST(dns_adbnamehook_t) dns_adbnamehooklist_t;
106 typedef struct dns_adblameinfo dns_adblameinfo_t;
107 typedef ISC_LIST(dns_adbentry_t) dns_adbentrylist_t;
108 typedef struct dns_adbfetch dns_adbfetch_t;
109 typedef struct dns_adbfetch6 dns_adbfetch6_t;
111 /*% dns adb structure */
112 struct dns_adb {
113 unsigned int magic;
115 isc_mutex_t lock;
116 isc_mutex_t reflock; /*%< Covers irefcnt, erefcnt */
117 isc_mutex_t overmemlock; /*%< Covers overmem */
118 isc_mem_t *mctx;
119 dns_view_t *view;
121 isc_taskmgr_t *taskmgr;
122 isc_task_t *task;
123 isc_boolean_t overmem;
125 isc_interval_t tick_interval;
126 int next_cleanbucket;
128 unsigned int irefcnt;
129 unsigned int erefcnt;
131 isc_mutex_t mplock;
132 isc_mempool_t *nmp; /*%< dns_adbname_t */
133 isc_mempool_t *nhmp; /*%< dns_adbnamehook_t */
134 isc_mempool_t *limp; /*%< dns_adblameinfo_t */
135 isc_mempool_t *emp; /*%< dns_adbentry_t */
136 isc_mempool_t *ahmp; /*%< dns_adbfind_t */
137 isc_mempool_t *aimp; /*%< dns_adbaddrinfo_t */
138 isc_mempool_t *afmp; /*%< dns_adbfetch_t */
141 * Bucketized locks and lists for names.
143 * XXXRTH Have a per-bucket structure that contains all of these?
145 dns_adbnamelist_t names[NBUCKETS];
146 dns_adbnamelist_t deadnames[NBUCKETS];
147 /*% See dns_adbnamelist_t */
148 isc_mutex_t namelocks[NBUCKETS];
149 /*% See dns_adbnamelist_t */
150 isc_boolean_t name_sd[NBUCKETS];
151 /*% See dns_adbnamelist_t */
152 unsigned int name_refcnt[NBUCKETS];
155 * Bucketized locks for entries.
157 * XXXRTH Have a per-bucket structure that contains all of these?
159 dns_adbentrylist_t entries[NBUCKETS];
160 dns_adbentrylist_t deadentries[NBUCKETS];
161 isc_mutex_t entrylocks[NBUCKETS];
162 isc_boolean_t entry_sd[NBUCKETS]; /*%< shutting down */
163 unsigned int entry_refcnt[NBUCKETS];
165 isc_event_t cevent;
166 isc_boolean_t cevent_sent;
167 isc_boolean_t shutting_down;
168 isc_eventlist_t whenshutdown;
172 * XXXMLG Document these structures.
175 /*% dns_adbname structure */
176 struct dns_adbname {
177 unsigned int magic;
178 dns_name_t name;
179 dns_adb_t *adb;
180 unsigned int partial_result;
181 unsigned int flags;
182 int lock_bucket;
183 dns_name_t target;
184 isc_stdtime_t expire_target;
185 isc_stdtime_t expire_v4;
186 isc_stdtime_t expire_v6;
187 unsigned int chains;
188 dns_adbnamehooklist_t v4;
189 dns_adbnamehooklist_t v6;
190 dns_adbfetch_t *fetch_a;
191 dns_adbfetch_t *fetch_aaaa;
192 unsigned int fetch_err;
193 unsigned int fetch6_err;
194 dns_adbfindlist_t finds;
195 /* for LRU-based management */
196 isc_stdtime_t last_used;
198 ISC_LINK(dns_adbname_t) plink;
201 /*% The adbfetch structure */
202 struct dns_adbfetch {
203 unsigned int magic;
204 dns_fetch_t *fetch;
205 dns_rdataset_t rdataset;
209 * This is a small widget that dangles off a dns_adbname_t. It contains a
210 * pointer to the address information about this host, and a link to the next
211 * namehook that will contain the next address this host has.
213 struct dns_adbnamehook {
214 unsigned int magic;
215 dns_adbentry_t *entry;
216 ISC_LINK(dns_adbnamehook_t) plink;
220 * This is a small widget that holds qname-specific information about an
221 * address. Currently limited to lameness, but could just as easily be
222 * extended to other types of information about zones.
224 struct dns_adblameinfo {
225 unsigned int magic;
227 dns_name_t qname;
228 dns_rdatatype_t qtype;
229 isc_stdtime_t lame_timer;
231 ISC_LINK(dns_adblameinfo_t) plink;
235 * An address entry. It holds quite a bit of information about addresses,
236 * including edns state (in "flags"), rtt, and of course the address of
237 * the host.
239 struct dns_adbentry {
240 unsigned int magic;
242 int lock_bucket;
243 unsigned int refcnt;
245 unsigned int flags;
246 unsigned int srtt;
247 isc_sockaddr_t sockaddr;
249 isc_stdtime_t expires;
250 /*%<
251 * A nonzero 'expires' field indicates that the entry should
252 * persist until that time. This allows entries found
253 * using dns_adb_findaddrinfo() to persist for a limited time
254 * even though they are not necessarily associated with a
255 * name.
258 ISC_LIST(dns_adblameinfo_t) lameinfo;
259 ISC_LINK(dns_adbentry_t) plink;
263 * Internal functions (and prototypes).
265 static inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
266 static inline void free_adbname(dns_adb_t *, dns_adbname_t **);
267 static inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
268 dns_adbentry_t *);
269 static inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
270 static inline dns_adblameinfo_t *new_adblameinfo(dns_adb_t *, dns_name_t *,
271 dns_rdatatype_t);
272 static inline void free_adblameinfo(dns_adb_t *, dns_adblameinfo_t **);
273 static inline dns_adbentry_t *new_adbentry(dns_adb_t *);
274 static inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
275 static inline dns_adbfind_t *new_adbfind(dns_adb_t *);
276 static inline isc_boolean_t free_adbfind(dns_adb_t *, dns_adbfind_t **);
277 static inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *, dns_adbentry_t *,
278 in_port_t);
279 static inline dns_adbfetch_t *new_adbfetch(dns_adb_t *);
280 static inline void free_adbfetch(dns_adb_t *, dns_adbfetch_t **);
281 static inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
282 unsigned int, int *);
283 static inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
284 isc_sockaddr_t *, int *,
285 isc_stdtime_t);
286 static void dump_adb(dns_adb_t *, FILE *, isc_boolean_t debug, isc_stdtime_t);
287 static void print_dns_name(FILE *, dns_name_t *);
288 static void print_namehook_list(FILE *, const char *legend,
289 dns_adbnamehooklist_t *list,
290 isc_boolean_t debug,
291 isc_stdtime_t now);
292 static void print_find_list(FILE *, dns_adbname_t *);
293 static void print_fetch_list(FILE *, dns_adbname_t *);
294 static inline isc_boolean_t dec_adb_irefcnt(dns_adb_t *);
295 static inline void inc_adb_irefcnt(dns_adb_t *);
296 static inline void inc_adb_erefcnt(dns_adb_t *);
297 static inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
298 isc_boolean_t);
299 static inline isc_boolean_t dec_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
300 isc_boolean_t);
301 static inline void violate_locking_hierarchy(isc_mutex_t *, isc_mutex_t *);
302 static isc_boolean_t clean_namehooks(dns_adb_t *, dns_adbnamehooklist_t *);
303 static void clean_target(dns_adb_t *, dns_name_t *);
304 static void clean_finds_at_name(dns_adbname_t *, isc_eventtype_t,
305 unsigned int);
306 static isc_boolean_t check_expire_namehooks(dns_adbname_t *, isc_stdtime_t);
307 static isc_boolean_t check_expire_entry(dns_adb_t *, dns_adbentry_t **,
308 isc_stdtime_t);
309 static void cancel_fetches_at_name(dns_adbname_t *);
310 static isc_result_t dbfind_name(dns_adbname_t *, isc_stdtime_t,
311 dns_rdatatype_t);
312 static isc_result_t fetch_name(dns_adbname_t *, isc_boolean_t,
313 dns_rdatatype_t);
314 static inline void check_exit(dns_adb_t *);
315 static void destroy(dns_adb_t *);
316 static isc_boolean_t shutdown_names(dns_adb_t *);
317 static isc_boolean_t shutdown_entries(dns_adb_t *);
318 static inline void link_name(dns_adb_t *, int, dns_adbname_t *);
319 static inline isc_boolean_t unlink_name(dns_adb_t *, dns_adbname_t *);
320 static inline void link_entry(dns_adb_t *, int, dns_adbentry_t *);
321 static inline isc_boolean_t unlink_entry(dns_adb_t *, dns_adbentry_t *);
322 static isc_boolean_t kill_name(dns_adbname_t **, isc_eventtype_t);
323 static void water(void *, int);
324 static void dump_entry(FILE *, dns_adbentry_t *, isc_boolean_t, isc_stdtime_t);
327 * MUST NOT overlap DNS_ADBFIND_* flags!
329 #define FIND_EVENT_SENT 0x40000000
330 #define FIND_EVENT_FREED 0x80000000
331 #define FIND_EVENTSENT(h) (((h)->flags & FIND_EVENT_SENT) != 0)
332 #define FIND_EVENTFREED(h) (((h)->flags & FIND_EVENT_FREED) != 0)
334 #define NAME_NEEDS_POKE 0x80000000
335 #define NAME_IS_DEAD 0x40000000
336 #define NAME_HINT_OK DNS_ADBFIND_HINTOK
337 #define NAME_GLUE_OK DNS_ADBFIND_GLUEOK
338 #define NAME_STARTATZONE DNS_ADBFIND_STARTATZONE
339 #define NAME_DEAD(n) (((n)->flags & NAME_IS_DEAD) != 0)
340 #define NAME_NEEDSPOKE(n) (((n)->flags & NAME_NEEDS_POKE) != 0)
341 #define NAME_GLUEOK(n) (((n)->flags & NAME_GLUE_OK) != 0)
342 #define NAME_HINTOK(n) (((n)->flags & NAME_HINT_OK) != 0)
345 * Private flag(s) for entries.
346 * MUST NOT overlap FCTX_ADDRINFO_xxx and DNS_FETCHOPT_NOEDNS0.
348 #define ENTRY_IS_DEAD 0x80000000
351 * To the name, address classes are all that really exist. If it has a
352 * V6 address it doesn't care if it came from a AAAA query.
354 #define NAME_HAS_V4(n) (!ISC_LIST_EMPTY((n)->v4))
355 #define NAME_HAS_V6(n) (!ISC_LIST_EMPTY((n)->v6))
356 #define NAME_HAS_ADDRS(n) (NAME_HAS_V4(n) || NAME_HAS_V6(n))
359 * Fetches are broken out into A and AAAA types. In some cases,
360 * however, it makes more sense to test for a particular class of fetches,
361 * like V4 or V6 above.
362 * Note: since we have removed the support of A6 in adb, FETCH_A and FETCH_AAAA
363 * are now equal to FETCH_V4 and FETCH_V6, respectively.
365 #define NAME_FETCH_A(n) ((n)->fetch_a != NULL)
366 #define NAME_FETCH_AAAA(n) ((n)->fetch_aaaa != NULL)
367 #define NAME_FETCH_V4(n) (NAME_FETCH_A(n))
368 #define NAME_FETCH_V6(n) (NAME_FETCH_AAAA(n))
369 #define NAME_FETCH(n) (NAME_FETCH_V4(n) || NAME_FETCH_V6(n))
372 * Find options and tests to see if there are addresses on the list.
374 #define FIND_WANTEVENT(fn) (((fn)->options & DNS_ADBFIND_WANTEVENT) != 0)
375 #define FIND_WANTEMPTYEVENT(fn) (((fn)->options & DNS_ADBFIND_EMPTYEVENT) != 0)
376 #define FIND_AVOIDFETCHES(fn) (((fn)->options & DNS_ADBFIND_AVOIDFETCHES) \
377 != 0)
378 #define FIND_STARTATZONE(fn) (((fn)->options & DNS_ADBFIND_STARTATZONE) \
379 != 0)
380 #define FIND_HINTOK(fn) (((fn)->options & DNS_ADBFIND_HINTOK) != 0)
381 #define FIND_GLUEOK(fn) (((fn)->options & DNS_ADBFIND_GLUEOK) != 0)
382 #define FIND_HAS_ADDRS(fn) (!ISC_LIST_EMPTY((fn)->list))
383 #define FIND_RETURNLAME(fn) (((fn)->options & DNS_ADBFIND_RETURNLAME) != 0)
386 * These are currently used on simple unsigned ints, so they are
387 * not really associated with any particular type.
389 #define WANT_INET(x) (((x) & DNS_ADBFIND_INET) != 0)
390 #define WANT_INET6(x) (((x) & DNS_ADBFIND_INET6) != 0)
392 #define EXPIRE_OK(exp, now) ((exp == INT_MAX) || (exp < now))
395 * Find out if the flags on a name (nf) indicate if it is a hint or
396 * glue, and compare this to the appropriate bits set in o, to see if
397 * this is ok.
399 #define GLUE_OK(nf, o) (!NAME_GLUEOK(nf) || (((o) & DNS_ADBFIND_GLUEOK) != 0))
400 #define HINT_OK(nf, o) (!NAME_HINTOK(nf) || (((o) & DNS_ADBFIND_HINTOK) != 0))
401 #define GLUEHINT_OK(nf, o) (GLUE_OK(nf, o) || HINT_OK(nf, o))
402 #define STARTATZONE_MATCHES(nf, o) (((nf)->flags & NAME_STARTATZONE) == \
403 ((o) & DNS_ADBFIND_STARTATZONE))
405 #define ENTER_LEVEL ISC_LOG_DEBUG(50)
406 #define EXIT_LEVEL ENTER_LEVEL
407 #define CLEAN_LEVEL ISC_LOG_DEBUG(100)
408 #define DEF_LEVEL ISC_LOG_DEBUG(5)
409 #define NCACHE_LEVEL ISC_LOG_DEBUG(20)
411 #define NCACHE_RESULT(r) ((r) == DNS_R_NCACHENXDOMAIN || \
412 (r) == DNS_R_NCACHENXRRSET)
413 #define AUTH_NX(r) ((r) == DNS_R_NXDOMAIN || \
414 (r) == DNS_R_NXRRSET)
415 #define NXDOMAIN_RESULT(r) ((r) == DNS_R_NXDOMAIN || \
416 (r) == DNS_R_NCACHENXDOMAIN)
417 #define NXRRSET_RESULT(r) ((r) == DNS_R_NCACHENXRRSET || \
418 (r) == DNS_R_NXRRSET || \
419 (r) == DNS_R_HINTNXRRSET)
422 * Error state rankings.
425 #define FIND_ERR_SUCCESS 0 /* highest rank */
426 #define FIND_ERR_CANCELED 1
427 #define FIND_ERR_FAILURE 2
428 #define FIND_ERR_NXDOMAIN 3
429 #define FIND_ERR_NXRRSET 4
430 #define FIND_ERR_UNEXPECTED 5
431 #define FIND_ERR_NOTFOUND 6
432 #define FIND_ERR_MAX 7
434 static const char *errnames[] = {
435 "success",
436 "canceled",
437 "failure",
438 "nxdomain",
439 "nxrrset",
440 "unexpected",
441 "not_found"
444 #define NEWERR(old, new) (ISC_MIN((old), (new)))
446 static isc_result_t find_err_map[FIND_ERR_MAX] = {
447 ISC_R_SUCCESS,
448 ISC_R_CANCELED,
449 ISC_R_FAILURE,
450 DNS_R_NXDOMAIN,
451 DNS_R_NXRRSET,
452 ISC_R_UNEXPECTED,
453 ISC_R_NOTFOUND /* not YET found */
456 static void
457 DP(int level, const char *format, ...) ISC_FORMAT_PRINTF(2, 3);
459 static void
460 DP(int level, const char *format, ...) {
461 va_list args;
463 va_start(args, format);
464 isc_log_vwrite(dns_lctx,
465 DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ADB,
466 level, format, args);
467 va_end(args);
471 * Increment resolver-related statistics counters.
473 static inline void
474 inc_stats(dns_adb_t *adb, isc_statscounter_t counter) {
475 if (adb->view->resstats != NULL)
476 isc_stats_increment(adb->view->resstats, counter);
479 static inline dns_ttl_t
480 ttlclamp(dns_ttl_t ttl) {
481 if (ttl < ADB_CACHE_MINIMUM)
482 ttl = ADB_CACHE_MINIMUM;
483 if (ttl > ADB_CACHE_MAXIMUM)
484 ttl = ADB_CACHE_MAXIMUM;
486 return (ttl);
490 * Requires the adbname bucket be locked and that no entry buckets be locked.
492 * This code handles A and AAAA rdatasets only.
494 static isc_result_t
495 import_rdataset(dns_adbname_t *adbname, dns_rdataset_t *rdataset,
496 isc_stdtime_t now)
498 isc_result_t result;
499 dns_adb_t *adb;
500 dns_adbnamehook_t *nh;
501 dns_adbnamehook_t *anh;
502 dns_rdata_t rdata = DNS_RDATA_INIT;
503 struct in_addr ina;
504 struct in6_addr in6a;
505 isc_sockaddr_t sockaddr;
506 dns_adbentry_t *foundentry; /* NO CLEAN UP! */
507 int addr_bucket;
508 isc_boolean_t new_addresses_added;
509 dns_rdatatype_t rdtype;
510 unsigned int findoptions;
511 dns_adbnamehooklist_t *hookhead;
513 INSIST(DNS_ADBNAME_VALID(adbname));
514 adb = adbname->adb;
515 INSIST(DNS_ADB_VALID(adb));
517 rdtype = rdataset->type;
518 INSIST((rdtype == dns_rdatatype_a) || (rdtype == dns_rdatatype_aaaa));
519 if (rdtype == dns_rdatatype_a)
520 findoptions = DNS_ADBFIND_INET;
521 else
522 findoptions = DNS_ADBFIND_INET6;
524 addr_bucket = DNS_ADB_INVALIDBUCKET;
525 new_addresses_added = ISC_FALSE;
527 nh = NULL;
528 result = dns_rdataset_first(rdataset);
529 while (result == ISC_R_SUCCESS) {
530 dns_rdata_reset(&rdata);
531 dns_rdataset_current(rdataset, &rdata);
532 if (rdtype == dns_rdatatype_a) {
533 INSIST(rdata.length == 4);
534 memcpy(&ina.s_addr, rdata.data, 4);
535 isc_sockaddr_fromin(&sockaddr, &ina, 0);
536 hookhead = &adbname->v4;
537 } else {
538 INSIST(rdata.length == 16);
539 memcpy(in6a.s6_addr, rdata.data, 16);
540 isc_sockaddr_fromin6(&sockaddr, &in6a, 0);
541 hookhead = &adbname->v6;
544 INSIST(nh == NULL);
545 nh = new_adbnamehook(adb, NULL);
546 if (nh == NULL) {
547 adbname->partial_result |= findoptions;
548 result = ISC_R_NOMEMORY;
549 goto fail;
552 foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket,
553 now);
554 if (foundentry == NULL) {
555 dns_adbentry_t *entry;
557 entry = new_adbentry(adb);
558 if (entry == NULL) {
559 adbname->partial_result |= findoptions;
560 result = ISC_R_NOMEMORY;
561 goto fail;
564 entry->sockaddr = sockaddr;
565 entry->refcnt = 1;
567 nh->entry = entry;
569 link_entry(adb, addr_bucket, entry);
570 } else {
571 for (anh = ISC_LIST_HEAD(*hookhead);
572 anh != NULL;
573 anh = ISC_LIST_NEXT(anh, plink))
574 if (anh->entry == foundentry)
575 break;
576 if (anh == NULL) {
577 foundentry->refcnt++;
578 nh->entry = foundentry;
579 } else
580 free_adbnamehook(adb, &nh);
583 new_addresses_added = ISC_TRUE;
584 if (nh != NULL)
585 ISC_LIST_APPEND(*hookhead, nh, plink);
586 nh = NULL;
587 result = dns_rdataset_next(rdataset);
590 fail:
591 if (nh != NULL)
592 free_adbnamehook(adb, &nh);
594 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
595 UNLOCK(&adb->entrylocks[addr_bucket]);
597 if (rdataset->trust == dns_trust_glue ||
598 rdataset->trust == dns_trust_additional)
599 rdataset->ttl = ADB_CACHE_MINIMUM;
600 else
601 rdataset->ttl = ttlclamp(rdataset->ttl);
603 if (rdtype == dns_rdatatype_a) {
604 DP(NCACHE_LEVEL, "expire_v4 set to MIN(%u,%u) import_rdataset",
605 adbname->expire_v4, now + rdataset->ttl);
606 adbname->expire_v4 = ISC_MIN(adbname->expire_v4,
607 now + rdataset->ttl);
608 } else {
609 DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) import_rdataset",
610 adbname->expire_v6, now + rdataset->ttl);
611 adbname->expire_v6 = ISC_MIN(adbname->expire_v6,
612 now + rdataset->ttl);
615 if (new_addresses_added) {
617 * Lie a little here. This is more or less so code that cares
618 * can find out if any new information was added or not.
620 return (ISC_R_SUCCESS);
623 return (result);
627 * Requires the name's bucket be locked.
629 static isc_boolean_t
630 kill_name(dns_adbname_t **n, isc_eventtype_t ev) {
631 dns_adbname_t *name;
632 isc_boolean_t result = ISC_FALSE;
633 isc_boolean_t result4, result6;
634 int bucket;
635 dns_adb_t *adb;
637 INSIST(n != NULL);
638 name = *n;
639 *n = NULL;
640 INSIST(DNS_ADBNAME_VALID(name));
641 adb = name->adb;
642 INSIST(DNS_ADB_VALID(adb));
644 DP(DEF_LEVEL, "killing name %p", name);
647 * If we're dead already, just check to see if we should go
648 * away now or not.
650 if (NAME_DEAD(name) && !NAME_FETCH(name)) {
651 result = unlink_name(adb, name);
652 free_adbname(adb, &name);
653 if (result)
654 result = dec_adb_irefcnt(adb);
655 return (result);
659 * Clean up the name's various lists. These two are destructive
660 * in that they will always empty the list.
662 clean_finds_at_name(name, ev, DNS_ADBFIND_ADDRESSMASK);
663 result4 = clean_namehooks(adb, &name->v4);
664 result6 = clean_namehooks(adb, &name->v6);
665 clean_target(adb, &name->target);
666 result = ISC_TF(result4 || result6);
669 * If fetches are running, cancel them. If none are running, we can
670 * just kill the name here.
672 if (!NAME_FETCH(name)) {
673 INSIST(result == ISC_FALSE);
674 result = unlink_name(adb, name);
675 free_adbname(adb, &name);
676 if (result)
677 result = dec_adb_irefcnt(adb);
678 } else {
679 cancel_fetches_at_name(name);
680 if (!NAME_DEAD(name)) {
681 bucket = name->lock_bucket;
682 ISC_LIST_UNLINK(adb->names[bucket], name, plink);
683 ISC_LIST_APPEND(adb->deadnames[bucket], name, plink);
684 name->flags |= NAME_IS_DEAD;
687 return (result);
691 * Requires the name's bucket be locked and no entry buckets be locked.
693 static isc_boolean_t
694 check_expire_namehooks(dns_adbname_t *name, isc_stdtime_t now) {
695 dns_adb_t *adb;
696 isc_boolean_t result4 = ISC_FALSE;
697 isc_boolean_t result6 = ISC_FALSE;
699 INSIST(DNS_ADBNAME_VALID(name));
700 adb = name->adb;
701 INSIST(DNS_ADB_VALID(adb));
704 * Check to see if we need to remove the v4 addresses
706 if (!NAME_FETCH_V4(name) && EXPIRE_OK(name->expire_v4, now)) {
707 if (NAME_HAS_V4(name)) {
708 DP(DEF_LEVEL, "expiring v4 for name %p", name);
709 result4 = clean_namehooks(adb, &name->v4);
710 name->partial_result &= ~DNS_ADBFIND_INET;
712 name->expire_v4 = INT_MAX;
713 name->fetch_err = FIND_ERR_UNEXPECTED;
717 * Check to see if we need to remove the v6 addresses
719 if (!NAME_FETCH_V6(name) && EXPIRE_OK(name->expire_v6, now)) {
720 if (NAME_HAS_V6(name)) {
721 DP(DEF_LEVEL, "expiring v6 for name %p", name);
722 result6 = clean_namehooks(adb, &name->v6);
723 name->partial_result &= ~DNS_ADBFIND_INET6;
725 name->expire_v6 = INT_MAX;
726 name->fetch6_err = FIND_ERR_UNEXPECTED;
730 * Check to see if we need to remove the alias target.
732 if (EXPIRE_OK(name->expire_target, now)) {
733 clean_target(adb, &name->target);
734 name->expire_target = INT_MAX;
736 return (ISC_TF(result4 || result6));
740 * Requires the name's bucket be locked.
742 static inline void
743 link_name(dns_adb_t *adb, int bucket, dns_adbname_t *name) {
744 INSIST(name->lock_bucket == DNS_ADB_INVALIDBUCKET);
746 ISC_LIST_PREPEND(adb->names[bucket], name, plink);
747 name->lock_bucket = bucket;
748 adb->name_refcnt[bucket]++;
752 * Requires the name's bucket be locked.
754 static inline isc_boolean_t
755 unlink_name(dns_adb_t *adb, dns_adbname_t *name) {
756 int bucket;
757 isc_boolean_t result = ISC_FALSE;
759 bucket = name->lock_bucket;
760 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
762 if (NAME_DEAD(name))
763 ISC_LIST_UNLINK(adb->deadnames[bucket], name, plink);
764 else
765 ISC_LIST_UNLINK(adb->names[bucket], name, plink);
766 name->lock_bucket = DNS_ADB_INVALIDBUCKET;
767 INSIST(adb->name_refcnt[bucket] > 0);
768 adb->name_refcnt[bucket]--;
769 if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
770 result = ISC_TRUE;
771 return (result);
775 * Requires the entry's bucket be locked.
777 static inline void
778 link_entry(dns_adb_t *adb, int bucket, dns_adbentry_t *entry) {
779 int i;
780 dns_adbentry_t *e;
782 if (adb->overmem) {
783 for (i = 0; i < 2; i++) {
784 e = ISC_LIST_TAIL(adb->entries[bucket]);
785 if (e == NULL)
786 break;
787 if (e->refcnt == 0) {
788 unlink_entry(adb, e);
789 free_adbentry(adb, &e);
790 continue;
792 INSIST((e->flags & ENTRY_IS_DEAD) == 0);
793 e->flags |= ENTRY_IS_DEAD;
794 ISC_LIST_UNLINK(adb->entries[bucket], e, plink);
795 ISC_LIST_PREPEND(adb->deadentries[bucket], e, plink);
799 ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
800 entry->lock_bucket = bucket;
801 adb->entry_refcnt[bucket]++;
805 * Requires the entry's bucket be locked.
807 static inline isc_boolean_t
808 unlink_entry(dns_adb_t *adb, dns_adbentry_t *entry) {
809 int bucket;
810 isc_boolean_t result = ISC_FALSE;
812 bucket = entry->lock_bucket;
813 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
815 if ((entry->flags & ENTRY_IS_DEAD) != 0)
816 ISC_LIST_UNLINK(adb->deadentries[bucket], entry, plink);
817 else
818 ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
819 entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
820 INSIST(adb->entry_refcnt[bucket] > 0);
821 adb->entry_refcnt[bucket]--;
822 if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
823 result = ISC_TRUE;
824 return (result);
827 static inline void
828 violate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want) {
829 if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
830 UNLOCK(have);
831 LOCK(want);
832 LOCK(have);
837 * The ADB _MUST_ be locked before calling. Also, exit conditions must be
838 * checked after calling this function.
840 static isc_boolean_t
841 shutdown_names(dns_adb_t *adb) {
842 int bucket;
843 isc_boolean_t result = ISC_FALSE;
844 dns_adbname_t *name;
845 dns_adbname_t *next_name;
847 for (bucket = 0; bucket < NBUCKETS; bucket++) {
848 LOCK(&adb->namelocks[bucket]);
849 adb->name_sd[bucket] = ISC_TRUE;
851 name = ISC_LIST_HEAD(adb->names[bucket]);
852 if (name == NULL) {
854 * This bucket has no names. We must decrement the
855 * irefcnt ourselves, since it will not be
856 * automatically triggered by a name being unlinked.
858 INSIST(result == ISC_FALSE);
859 result = dec_adb_irefcnt(adb);
860 } else {
862 * Run through the list. For each name, clean up finds
863 * found there, and cancel any fetches running. When
864 * all the fetches are canceled, the name will destroy
865 * itself.
867 while (name != NULL) {
868 next_name = ISC_LIST_NEXT(name, plink);
869 INSIST(result == ISC_FALSE);
870 result = kill_name(&name,
871 DNS_EVENT_ADBSHUTDOWN);
872 name = next_name;
876 UNLOCK(&adb->namelocks[bucket]);
878 return (result);
882 * The ADB _MUST_ be locked before calling. Also, exit conditions must be
883 * checked after calling this function.
885 static isc_boolean_t
886 shutdown_entries(dns_adb_t *adb) {
887 int bucket;
888 isc_boolean_t result = ISC_FALSE;
889 dns_adbentry_t *entry;
890 dns_adbentry_t *next_entry;
892 for (bucket = 0; bucket < NBUCKETS; bucket++) {
893 LOCK(&adb->entrylocks[bucket]);
894 adb->entry_sd[bucket] = ISC_TRUE;
896 entry = ISC_LIST_HEAD(adb->entries[bucket]);
897 if (adb->entry_refcnt[bucket] == 0) {
899 * This bucket has no entries. We must decrement the
900 * irefcnt ourselves, since it will not be
901 * automatically triggered by an entry being unlinked.
903 result = dec_adb_irefcnt(adb);
904 } else {
906 * Run through the list. Cleanup any entries not
907 * associated with names, and which are not in use.
909 while (entry != NULL) {
910 next_entry = ISC_LIST_NEXT(entry, plink);
911 if (entry->refcnt == 0 &&
912 entry->expires != 0) {
913 result = unlink_entry(adb, entry);
914 free_adbentry(adb, &entry);
915 if (result)
916 result = dec_adb_irefcnt(adb);
918 entry = next_entry;
922 UNLOCK(&adb->entrylocks[bucket]);
924 return (result);
928 * Name bucket must be locked
930 static void
931 cancel_fetches_at_name(dns_adbname_t *name) {
932 if (NAME_FETCH_A(name))
933 dns_resolver_cancelfetch(name->fetch_a->fetch);
935 if (NAME_FETCH_AAAA(name))
936 dns_resolver_cancelfetch(name->fetch_aaaa->fetch);
940 * Assumes the name bucket is locked.
942 static isc_boolean_t
943 clean_namehooks(dns_adb_t *adb, dns_adbnamehooklist_t *namehooks) {
944 dns_adbentry_t *entry;
945 dns_adbnamehook_t *namehook;
946 int addr_bucket;
947 isc_boolean_t result = ISC_FALSE;
949 addr_bucket = DNS_ADB_INVALIDBUCKET;
950 namehook = ISC_LIST_HEAD(*namehooks);
951 while (namehook != NULL) {
952 INSIST(DNS_ADBNAMEHOOK_VALID(namehook));
955 * Clean up the entry if needed.
957 entry = namehook->entry;
958 if (entry != NULL) {
959 INSIST(DNS_ADBENTRY_VALID(entry));
961 if (addr_bucket != entry->lock_bucket) {
962 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
963 UNLOCK(&adb->entrylocks[addr_bucket]);
964 addr_bucket = entry->lock_bucket;
965 LOCK(&adb->entrylocks[addr_bucket]);
968 result = dec_entry_refcnt(adb, entry, ISC_FALSE);
972 * Free the namehook
974 namehook->entry = NULL;
975 ISC_LIST_UNLINK(*namehooks, namehook, plink);
976 free_adbnamehook(adb, &namehook);
978 namehook = ISC_LIST_HEAD(*namehooks);
981 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
982 UNLOCK(&adb->entrylocks[addr_bucket]);
983 return (result);
986 static void
987 clean_target(dns_adb_t *adb, dns_name_t *target) {
988 if (dns_name_countlabels(target) > 0) {
989 dns_name_free(target, adb->mctx);
990 dns_name_init(target, NULL);
994 static isc_result_t
995 set_target(dns_adb_t *adb, dns_name_t *name, dns_name_t *fname,
996 dns_rdataset_t *rdataset, dns_name_t *target)
998 isc_result_t result;
999 dns_namereln_t namereln;
1000 unsigned int nlabels;
1001 int order;
1002 dns_rdata_t rdata = DNS_RDATA_INIT;
1003 dns_fixedname_t fixed1, fixed2;
1004 dns_name_t *prefix, *new_target;
1006 REQUIRE(dns_name_countlabels(target) == 0);
1008 if (rdataset->type == dns_rdatatype_cname) {
1009 dns_rdata_cname_t cname;
1012 * Copy the CNAME's target into the target name.
1014 result = dns_rdataset_first(rdataset);
1015 if (result != ISC_R_SUCCESS)
1016 return (result);
1017 dns_rdataset_current(rdataset, &rdata);
1018 result = dns_rdata_tostruct(&rdata, &cname, NULL);
1019 if (result != ISC_R_SUCCESS)
1020 return (result);
1021 result = dns_name_dup(&cname.cname, adb->mctx, target);
1022 dns_rdata_freestruct(&cname);
1023 if (result != ISC_R_SUCCESS)
1024 return (result);
1025 } else {
1026 dns_rdata_dname_t dname;
1028 INSIST(rdataset->type == dns_rdatatype_dname);
1029 namereln = dns_name_fullcompare(name, fname, &order, &nlabels);
1030 INSIST(namereln == dns_namereln_subdomain);
1032 * Get the target name of the DNAME.
1034 result = dns_rdataset_first(rdataset);
1035 if (result != ISC_R_SUCCESS)
1036 return (result);
1037 dns_rdataset_current(rdataset, &rdata);
1038 result = dns_rdata_tostruct(&rdata, &dname, NULL);
1039 if (result != ISC_R_SUCCESS)
1040 return (result);
1042 * Construct the new target name.
1044 dns_fixedname_init(&fixed1);
1045 prefix = dns_fixedname_name(&fixed1);
1046 dns_fixedname_init(&fixed2);
1047 new_target = dns_fixedname_name(&fixed2);
1048 dns_name_split(name, nlabels, prefix, NULL);
1049 result = dns_name_concatenate(prefix, &dname.dname, new_target,
1050 NULL);
1051 dns_rdata_freestruct(&dname);
1052 if (result != ISC_R_SUCCESS)
1053 return (result);
1054 result = dns_name_dup(new_target, adb->mctx, target);
1055 if (result != ISC_R_SUCCESS)
1056 return (result);
1059 return (ISC_R_SUCCESS);
1063 * Assumes nothing is locked, since this is called by the client.
1065 static void
1066 event_free(isc_event_t *event) {
1067 dns_adbfind_t *find;
1069 INSIST(event != NULL);
1070 find = event->ev_destroy_arg;
1071 INSIST(DNS_ADBFIND_VALID(find));
1073 LOCK(&find->lock);
1074 find->flags |= FIND_EVENT_FREED;
1075 event->ev_destroy_arg = NULL;
1076 UNLOCK(&find->lock);
1080 * Assumes the name bucket is locked.
1082 static void
1083 clean_finds_at_name(dns_adbname_t *name, isc_eventtype_t evtype,
1084 unsigned int addrs)
1086 isc_event_t *ev;
1087 isc_task_t *task;
1088 dns_adbfind_t *find;
1089 dns_adbfind_t *next_find;
1090 isc_boolean_t process;
1091 unsigned int wanted, notify;
1093 DP(ENTER_LEVEL,
1094 "ENTER clean_finds_at_name, name %p, evtype %08x, addrs %08x",
1095 name, evtype, addrs);
1097 find = ISC_LIST_HEAD(name->finds);
1098 while (find != NULL) {
1099 LOCK(&find->lock);
1100 next_find = ISC_LIST_NEXT(find, plink);
1102 process = ISC_FALSE;
1103 wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
1104 notify = wanted & addrs;
1106 switch (evtype) {
1107 case DNS_EVENT_ADBMOREADDRESSES:
1108 DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBMOREADDRESSES");
1109 if ((notify) != 0) {
1110 find->flags &= ~addrs;
1111 process = ISC_TRUE;
1113 break;
1114 case DNS_EVENT_ADBNOMOREADDRESSES:
1115 DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBNOMOREADDRESSES");
1116 find->flags &= ~addrs;
1117 wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
1118 if (wanted == 0)
1119 process = ISC_TRUE;
1120 break;
1121 default:
1122 find->flags &= ~addrs;
1123 process = ISC_TRUE;
1126 if (process) {
1127 DP(DEF_LEVEL, "cfan: processing find %p", find);
1129 * Unlink the find from the name, letting the caller
1130 * call dns_adb_destroyfind() on it to clean it up
1131 * later.
1133 ISC_LIST_UNLINK(name->finds, find, plink);
1134 find->adbname = NULL;
1135 find->name_bucket = DNS_ADB_INVALIDBUCKET;
1137 INSIST(!FIND_EVENTSENT(find));
1139 ev = &find->event;
1140 task = ev->ev_sender;
1141 ev->ev_sender = find;
1142 find->result_v4 = find_err_map[name->fetch_err];
1143 find->result_v6 = find_err_map[name->fetch6_err];
1144 ev->ev_type = evtype;
1145 ev->ev_destroy = event_free;
1146 ev->ev_destroy_arg = find;
1148 DP(DEF_LEVEL,
1149 "sending event %p to task %p for find %p",
1150 ev, task, find);
1152 isc_task_sendanddetach(&task, (isc_event_t **)&ev);
1153 } else {
1154 DP(DEF_LEVEL, "cfan: skipping find %p", find);
1157 UNLOCK(&find->lock);
1158 find = next_find;
1161 DP(ENTER_LEVEL, "EXIT clean_finds_at_name, name %p", name);
1164 static inline void
1165 check_exit(dns_adb_t *adb) {
1166 isc_event_t *event;
1168 * The caller must be holding the adb lock.
1170 if (adb->shutting_down) {
1172 * If there aren't any external references either, we're
1173 * done. Send the control event to initiate shutdown.
1175 INSIST(!adb->cevent_sent); /* Sanity check. */
1176 event = &adb->cevent;
1177 isc_task_send(adb->task, &event);
1178 adb->cevent_sent = ISC_TRUE;
1182 static inline isc_boolean_t
1183 dec_adb_irefcnt(dns_adb_t *adb) {
1184 isc_event_t *event;
1185 isc_task_t *etask;
1186 isc_boolean_t result = ISC_FALSE;
1188 LOCK(&adb->reflock);
1190 INSIST(adb->irefcnt > 0);
1191 adb->irefcnt--;
1193 if (adb->irefcnt == 0) {
1194 event = ISC_LIST_HEAD(adb->whenshutdown);
1195 while (event != NULL) {
1196 ISC_LIST_UNLINK(adb->whenshutdown, event, ev_link);
1197 etask = event->ev_sender;
1198 event->ev_sender = adb;
1199 isc_task_sendanddetach(&etask, &event);
1200 event = ISC_LIST_HEAD(adb->whenshutdown);
1204 if (adb->irefcnt == 0 && adb->erefcnt == 0)
1205 result = ISC_TRUE;
1206 UNLOCK(&adb->reflock);
1207 return (result);
1210 static inline void
1211 inc_adb_irefcnt(dns_adb_t *adb) {
1212 LOCK(&adb->reflock);
1213 adb->irefcnt++;
1214 UNLOCK(&adb->reflock);
1217 static inline void
1218 inc_adb_erefcnt(dns_adb_t *adb) {
1219 LOCK(&adb->reflock);
1220 adb->erefcnt++;
1221 UNLOCK(&adb->reflock);
1224 static inline void
1225 inc_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
1226 int bucket;
1228 bucket = entry->lock_bucket;
1230 if (lock)
1231 LOCK(&adb->entrylocks[bucket]);
1233 entry->refcnt++;
1235 if (lock)
1236 UNLOCK(&adb->entrylocks[bucket]);
1239 static inline isc_boolean_t
1240 dec_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
1241 int bucket;
1242 isc_boolean_t destroy_entry;
1243 isc_boolean_t result = ISC_FALSE;
1245 bucket = entry->lock_bucket;
1247 if (lock)
1248 LOCK(&adb->entrylocks[bucket]);
1250 INSIST(entry->refcnt > 0);
1251 entry->refcnt--;
1253 destroy_entry = ISC_FALSE;
1254 if (entry->refcnt == 0 &&
1255 (adb->entry_sd[bucket] || entry->expires == 0 || adb->overmem ||
1256 (entry->flags & ENTRY_IS_DEAD) != 0)) {
1257 destroy_entry = ISC_TRUE;
1258 result = unlink_entry(adb, entry);
1261 if (lock)
1262 UNLOCK(&adb->entrylocks[bucket]);
1264 if (!destroy_entry)
1265 return (result);
1267 entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
1269 free_adbentry(adb, &entry);
1270 if (result)
1271 result = dec_adb_irefcnt(adb);
1273 return (result);
1276 static inline dns_adbname_t *
1277 new_adbname(dns_adb_t *adb, dns_name_t *dnsname) {
1278 dns_adbname_t *name;
1280 name = isc_mempool_get(adb->nmp);
1281 if (name == NULL)
1282 return (NULL);
1284 dns_name_init(&name->name, NULL);
1285 if (dns_name_dup(dnsname, adb->mctx, &name->name) != ISC_R_SUCCESS) {
1286 isc_mempool_put(adb->nmp, name);
1287 return (NULL);
1289 dns_name_init(&name->target, NULL);
1290 name->magic = DNS_ADBNAME_MAGIC;
1291 name->adb = adb;
1292 name->partial_result = 0;
1293 name->flags = 0;
1294 name->expire_v4 = INT_MAX;
1295 name->expire_v6 = INT_MAX;
1296 name->expire_target = INT_MAX;
1297 name->chains = 0;
1298 name->lock_bucket = DNS_ADB_INVALIDBUCKET;
1299 ISC_LIST_INIT(name->v4);
1300 ISC_LIST_INIT(name->v6);
1301 name->fetch_a = NULL;
1302 name->fetch_aaaa = NULL;
1303 name->fetch_err = FIND_ERR_UNEXPECTED;
1304 name->fetch6_err = FIND_ERR_UNEXPECTED;
1305 ISC_LIST_INIT(name->finds);
1306 ISC_LINK_INIT(name, plink);
1308 return (name);
1311 static inline void
1312 free_adbname(dns_adb_t *adb, dns_adbname_t **name) {
1313 dns_adbname_t *n;
1315 INSIST(name != NULL && DNS_ADBNAME_VALID(*name));
1316 n = *name;
1317 *name = NULL;
1319 INSIST(!NAME_HAS_V4(n));
1320 INSIST(!NAME_HAS_V6(n));
1321 INSIST(!NAME_FETCH(n));
1322 INSIST(ISC_LIST_EMPTY(n->finds));
1323 INSIST(!ISC_LINK_LINKED(n, plink));
1324 INSIST(n->lock_bucket == DNS_ADB_INVALIDBUCKET);
1325 INSIST(n->adb == adb);
1327 n->magic = 0;
1328 dns_name_free(&n->name, adb->mctx);
1330 isc_mempool_put(adb->nmp, n);
1333 static inline dns_adbnamehook_t *
1334 new_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry) {
1335 dns_adbnamehook_t *nh;
1337 nh = isc_mempool_get(adb->nhmp);
1338 if (nh == NULL)
1339 return (NULL);
1341 nh->magic = DNS_ADBNAMEHOOK_MAGIC;
1342 nh->entry = entry;
1343 ISC_LINK_INIT(nh, plink);
1345 return (nh);
1348 static inline void
1349 free_adbnamehook(dns_adb_t *adb, dns_adbnamehook_t **namehook) {
1350 dns_adbnamehook_t *nh;
1352 INSIST(namehook != NULL && DNS_ADBNAMEHOOK_VALID(*namehook));
1353 nh = *namehook;
1354 *namehook = NULL;
1356 INSIST(nh->entry == NULL);
1357 INSIST(!ISC_LINK_LINKED(nh, plink));
1359 nh->magic = 0;
1360 isc_mempool_put(adb->nhmp, nh);
1363 static inline dns_adblameinfo_t *
1364 new_adblameinfo(dns_adb_t *adb, dns_name_t *qname, dns_rdatatype_t qtype) {
1365 dns_adblameinfo_t *li;
1367 li = isc_mempool_get(adb->limp);
1368 if (li == NULL)
1369 return (NULL);
1371 dns_name_init(&li->qname, NULL);
1372 if (dns_name_dup(qname, adb->mctx, &li->qname) != ISC_R_SUCCESS) {
1373 isc_mempool_put(adb->limp, li);
1374 return (NULL);
1376 li->magic = DNS_ADBLAMEINFO_MAGIC;
1377 li->lame_timer = 0;
1378 li->qtype = qtype;
1379 ISC_LINK_INIT(li, plink);
1381 return (li);
1384 static inline void
1385 free_adblameinfo(dns_adb_t *adb, dns_adblameinfo_t **lameinfo) {
1386 dns_adblameinfo_t *li;
1388 INSIST(lameinfo != NULL && DNS_ADBLAMEINFO_VALID(*lameinfo));
1389 li = *lameinfo;
1390 *lameinfo = NULL;
1392 INSIST(!ISC_LINK_LINKED(li, plink));
1394 dns_name_free(&li->qname, adb->mctx);
1396 li->magic = 0;
1398 isc_mempool_put(adb->limp, li);
1401 static inline dns_adbentry_t *
1402 new_adbentry(dns_adb_t *adb) {
1403 dns_adbentry_t *e;
1404 isc_uint32_t r;
1406 e = isc_mempool_get(adb->emp);
1407 if (e == NULL)
1408 return (NULL);
1410 e->magic = DNS_ADBENTRY_MAGIC;
1411 e->lock_bucket = DNS_ADB_INVALIDBUCKET;
1412 e->refcnt = 0;
1413 e->flags = 0;
1414 isc_random_get(&r);
1415 e->srtt = (r & 0x1f) + 1;
1416 e->expires = 0;
1417 ISC_LIST_INIT(e->lameinfo);
1418 ISC_LINK_INIT(e, plink);
1420 return (e);
1423 static inline void
1424 free_adbentry(dns_adb_t *adb, dns_adbentry_t **entry) {
1425 dns_adbentry_t *e;
1426 dns_adblameinfo_t *li;
1428 INSIST(entry != NULL && DNS_ADBENTRY_VALID(*entry));
1429 e = *entry;
1430 *entry = NULL;
1432 INSIST(e->lock_bucket == DNS_ADB_INVALIDBUCKET);
1433 INSIST(e->refcnt == 0);
1434 INSIST(!ISC_LINK_LINKED(e, plink));
1436 e->magic = 0;
1438 li = ISC_LIST_HEAD(e->lameinfo);
1439 while (li != NULL) {
1440 ISC_LIST_UNLINK(e->lameinfo, li, plink);
1441 free_adblameinfo(adb, &li);
1442 li = ISC_LIST_HEAD(e->lameinfo);
1445 isc_mempool_put(adb->emp, e);
1448 static inline dns_adbfind_t *
1449 new_adbfind(dns_adb_t *adb) {
1450 dns_adbfind_t *h;
1451 isc_result_t result;
1453 h = isc_mempool_get(adb->ahmp);
1454 if (h == NULL)
1455 return (NULL);
1458 * Public members.
1460 h->magic = 0;
1461 h->adb = adb;
1462 h->partial_result = 0;
1463 h->options = 0;
1464 h->flags = 0;
1465 h->result_v4 = ISC_R_UNEXPECTED;
1466 h->result_v6 = ISC_R_UNEXPECTED;
1467 ISC_LINK_INIT(h, publink);
1468 ISC_LINK_INIT(h, plink);
1469 ISC_LIST_INIT(h->list);
1470 h->adbname = NULL;
1471 h->name_bucket = DNS_ADB_INVALIDBUCKET;
1474 * private members
1476 result = isc_mutex_init(&h->lock);
1477 if (result != ISC_R_SUCCESS) {
1478 isc_mempool_put(adb->ahmp, h);
1479 return (NULL);
1482 ISC_EVENT_INIT(&h->event, sizeof(isc_event_t), 0, 0, 0, NULL, NULL,
1483 NULL, NULL, h);
1485 inc_adb_irefcnt(adb);
1486 h->magic = DNS_ADBFIND_MAGIC;
1487 return (h);
1490 static inline dns_adbfetch_t *
1491 new_adbfetch(dns_adb_t *adb) {
1492 dns_adbfetch_t *f;
1494 f = isc_mempool_get(adb->afmp);
1495 if (f == NULL)
1496 return (NULL);
1498 f->magic = 0;
1499 f->fetch = NULL;
1501 dns_rdataset_init(&f->rdataset);
1503 f->magic = DNS_ADBFETCH_MAGIC;
1505 return (f);
1508 static inline void
1509 free_adbfetch(dns_adb_t *adb, dns_adbfetch_t **fetch) {
1510 dns_adbfetch_t *f;
1512 INSIST(fetch != NULL && DNS_ADBFETCH_VALID(*fetch));
1513 f = *fetch;
1514 *fetch = NULL;
1516 f->magic = 0;
1518 if (dns_rdataset_isassociated(&f->rdataset))
1519 dns_rdataset_disassociate(&f->rdataset);
1521 isc_mempool_put(adb->afmp, f);
1524 static inline isc_boolean_t
1525 free_adbfind(dns_adb_t *adb, dns_adbfind_t **findp) {
1526 dns_adbfind_t *find;
1528 INSIST(findp != NULL && DNS_ADBFIND_VALID(*findp));
1529 find = *findp;
1530 *findp = NULL;
1532 INSIST(!FIND_HAS_ADDRS(find));
1533 INSIST(!ISC_LINK_LINKED(find, publink));
1534 INSIST(!ISC_LINK_LINKED(find, plink));
1535 INSIST(find->name_bucket == DNS_ADB_INVALIDBUCKET);
1536 INSIST(find->adbname == NULL);
1538 find->magic = 0;
1540 DESTROYLOCK(&find->lock);
1541 isc_mempool_put(adb->ahmp, find);
1542 return (dec_adb_irefcnt(adb));
1546 * Copy bits from the entry into the newly allocated addrinfo. The entry
1547 * must be locked, and the reference count must be bumped up by one
1548 * if this function returns a valid pointer.
1550 static inline dns_adbaddrinfo_t *
1551 new_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry, in_port_t port) {
1552 dns_adbaddrinfo_t *ai;
1554 ai = isc_mempool_get(adb->aimp);
1555 if (ai == NULL)
1556 return (NULL);
1558 ai->magic = DNS_ADBADDRINFO_MAGIC;
1559 ai->sockaddr = entry->sockaddr;
1560 isc_sockaddr_setport(&ai->sockaddr, port);
1561 ai->srtt = entry->srtt;
1562 ai->flags = entry->flags;
1563 ai->entry = entry;
1564 ISC_LINK_INIT(ai, publink);
1566 return (ai);
1569 static inline void
1570 free_adbaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **ainfo) {
1571 dns_adbaddrinfo_t *ai;
1573 INSIST(ainfo != NULL && DNS_ADBADDRINFO_VALID(*ainfo));
1574 ai = *ainfo;
1575 *ainfo = NULL;
1577 INSIST(ai->entry == NULL);
1578 INSIST(!ISC_LINK_LINKED(ai, publink));
1580 ai->magic = 0;
1582 isc_mempool_put(adb->aimp, ai);
1586 * Search for the name. NOTE: The bucket is kept locked on both
1587 * success and failure, so it must always be unlocked by the caller!
1589 * On the first call to this function, *bucketp must be set to
1590 * DNS_ADB_INVALIDBUCKET.
1592 static inline dns_adbname_t *
1593 find_name_and_lock(dns_adb_t *adb, dns_name_t *name,
1594 unsigned int options, int *bucketp)
1596 dns_adbname_t *adbname;
1597 int bucket;
1599 bucket = dns_name_fullhash(name, ISC_FALSE) % NBUCKETS;
1601 if (*bucketp == DNS_ADB_INVALIDBUCKET) {
1602 LOCK(&adb->namelocks[bucket]);
1603 *bucketp = bucket;
1604 } else if (*bucketp != bucket) {
1605 UNLOCK(&adb->namelocks[*bucketp]);
1606 LOCK(&adb->namelocks[bucket]);
1607 *bucketp = bucket;
1610 adbname = ISC_LIST_HEAD(adb->names[bucket]);
1611 while (adbname != NULL) {
1612 if (!NAME_DEAD(adbname)) {
1613 if (dns_name_equal(name, &adbname->name)
1614 && GLUEHINT_OK(adbname, options)
1615 && STARTATZONE_MATCHES(adbname, options))
1616 return (adbname);
1618 adbname = ISC_LIST_NEXT(adbname, plink);
1621 return (NULL);
1625 * Search for the address. NOTE: The bucket is kept locked on both
1626 * success and failure, so it must always be unlocked by the caller.
1628 * On the first call to this function, *bucketp must be set to
1629 * DNS_ADB_INVALIDBUCKET. This will cause a lock to occur. On
1630 * later calls (within the same "lock path") it can be left alone, so
1631 * if this function is called multiple times locking is only done if
1632 * the bucket changes.
1634 static inline dns_adbentry_t *
1635 find_entry_and_lock(dns_adb_t *adb, isc_sockaddr_t *addr, int *bucketp,
1636 isc_stdtime_t now)
1638 dns_adbentry_t *entry, *entry_next;
1639 int bucket;
1641 bucket = isc_sockaddr_hash(addr, ISC_TRUE) % NBUCKETS;
1643 if (*bucketp == DNS_ADB_INVALIDBUCKET) {
1644 LOCK(&adb->entrylocks[bucket]);
1645 *bucketp = bucket;
1646 } else if (*bucketp != bucket) {
1647 UNLOCK(&adb->entrylocks[*bucketp]);
1648 LOCK(&adb->entrylocks[bucket]);
1649 *bucketp = bucket;
1652 /* Search the list, while cleaning up expired entries. */
1653 for (entry = ISC_LIST_HEAD(adb->entries[bucket]);
1654 entry != NULL;
1655 entry = entry_next) {
1656 entry_next = ISC_LIST_NEXT(entry, plink);
1657 (void)check_expire_entry(adb, &entry, now);
1658 if (entry != NULL &&
1659 isc_sockaddr_equal(addr, &entry->sockaddr)) {
1660 ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
1661 ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
1662 return (entry);
1666 return (NULL);
1670 * Entry bucket MUST be locked!
1672 static isc_boolean_t
1673 entry_is_lame(dns_adb_t *adb, dns_adbentry_t *entry, dns_name_t *qname,
1674 dns_rdatatype_t qtype, isc_stdtime_t now)
1676 dns_adblameinfo_t *li, *next_li;
1677 isc_boolean_t is_bad;
1679 is_bad = ISC_FALSE;
1681 li = ISC_LIST_HEAD(entry->lameinfo);
1682 if (li == NULL)
1683 return (ISC_FALSE);
1684 while (li != NULL) {
1685 next_li = ISC_LIST_NEXT(li, plink);
1688 * Has the entry expired?
1690 if (li->lame_timer < now) {
1691 ISC_LIST_UNLINK(entry->lameinfo, li, plink);
1692 free_adblameinfo(adb, &li);
1696 * Order tests from least to most expensive.
1698 * We do not break out of the main loop here as
1699 * we use the loop for house keeping.
1701 if (li != NULL && !is_bad && li->qtype == qtype &&
1702 dns_name_equal(qname, &li->qname))
1703 is_bad = ISC_TRUE;
1705 li = next_li;
1708 return (is_bad);
1711 static void
1712 copy_namehook_lists(dns_adb_t *adb, dns_adbfind_t *find, dns_name_t *qname,
1713 dns_rdatatype_t qtype, dns_adbname_t *name,
1714 isc_stdtime_t now)
1716 dns_adbnamehook_t *namehook;
1717 dns_adbaddrinfo_t *addrinfo;
1718 dns_adbentry_t *entry;
1719 int bucket;
1721 bucket = DNS_ADB_INVALIDBUCKET;
1723 if (find->options & DNS_ADBFIND_INET) {
1724 namehook = ISC_LIST_HEAD(name->v4);
1725 while (namehook != NULL) {
1726 entry = namehook->entry;
1727 bucket = entry->lock_bucket;
1728 LOCK(&adb->entrylocks[bucket]);
1730 if (!FIND_RETURNLAME(find)
1731 && entry_is_lame(adb, entry, qname, qtype, now)) {
1732 find->options |= DNS_ADBFIND_LAMEPRUNED;
1733 goto nextv4;
1735 addrinfo = new_adbaddrinfo(adb, entry, find->port);
1736 if (addrinfo == NULL) {
1737 find->partial_result |= DNS_ADBFIND_INET;
1738 goto out;
1741 * Found a valid entry. Add it to the find's list.
1743 inc_entry_refcnt(adb, entry, ISC_FALSE);
1744 ISC_LIST_APPEND(find->list, addrinfo, publink);
1745 addrinfo = NULL;
1746 nextv4:
1747 UNLOCK(&adb->entrylocks[bucket]);
1748 bucket = DNS_ADB_INVALIDBUCKET;
1749 namehook = ISC_LIST_NEXT(namehook, plink);
1753 if (find->options & DNS_ADBFIND_INET6) {
1754 namehook = ISC_LIST_HEAD(name->v6);
1755 while (namehook != NULL) {
1756 entry = namehook->entry;
1757 bucket = entry->lock_bucket;
1758 LOCK(&adb->entrylocks[bucket]);
1760 if (!FIND_RETURNLAME(find)
1761 && entry_is_lame(adb, entry, qname, qtype, now)) {
1762 find->options |= DNS_ADBFIND_LAMEPRUNED;
1763 goto nextv6;
1765 addrinfo = new_adbaddrinfo(adb, entry, find->port);
1766 if (addrinfo == NULL) {
1767 find->partial_result |= DNS_ADBFIND_INET6;
1768 goto out;
1771 * Found a valid entry. Add it to the find's list.
1773 inc_entry_refcnt(adb, entry, ISC_FALSE);
1774 ISC_LIST_APPEND(find->list, addrinfo, publink);
1775 addrinfo = NULL;
1776 nextv6:
1777 UNLOCK(&adb->entrylocks[bucket]);
1778 bucket = DNS_ADB_INVALIDBUCKET;
1779 namehook = ISC_LIST_NEXT(namehook, plink);
1783 out:
1784 if (bucket != DNS_ADB_INVALIDBUCKET)
1785 UNLOCK(&adb->entrylocks[bucket]);
1788 static void
1789 shutdown_task(isc_task_t *task, isc_event_t *ev) {
1790 dns_adb_t *adb;
1792 UNUSED(task);
1794 adb = ev->ev_arg;
1795 INSIST(DNS_ADB_VALID(adb));
1797 isc_event_free(&ev);
1799 * Wait for lock around check_exit() call to be released.
1801 LOCK(&adb->lock);
1802 UNLOCK(&adb->lock);
1803 destroy(adb);
1807 * Name bucket must be locked; adb may be locked; no other locks held.
1809 static isc_boolean_t
1810 check_expire_name(dns_adbname_t **namep, isc_stdtime_t now) {
1811 dns_adbname_t *name;
1812 isc_boolean_t result = ISC_FALSE;
1814 INSIST(namep != NULL && DNS_ADBNAME_VALID(*namep));
1815 name = *namep;
1817 if (NAME_HAS_V4(name) || NAME_HAS_V6(name))
1818 return (result);
1819 if (NAME_FETCH(name))
1820 return (result);
1821 if (!EXPIRE_OK(name->expire_v4, now))
1822 return (result);
1823 if (!EXPIRE_OK(name->expire_v6, now))
1824 return (result);
1825 if (!EXPIRE_OK(name->expire_target, now))
1826 return (result);
1829 * The name is empty. Delete it.
1831 result = kill_name(&name, DNS_EVENT_ADBEXPIRED);
1832 *namep = NULL;
1835 * Our caller, or one of its callers, will be calling check_exit() at
1836 * some point, so we don't need to do it here.
1838 return (result);
1842 * Examine the tail entry of the LRU list to see if it expires or is stale
1843 * (unused for some period); if so, the name entry will be freed. If the ADB
1844 * is in the overmem condition, the tail and the next to tail entries
1845 * will be unconditionally removed (unless they have an outstanding fetch).
1846 * We don't care about a race on 'overmem' at the risk of causing some
1847 * collateral damage or a small delay in starting cleanup, so we don't bother
1848 * to lock ADB (if it's not locked).
1850 * Name bucket must be locked; adb may be locked; no other locks held.
1852 static void
1853 check_stale_name(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
1854 int victims, max_victims;
1855 isc_boolean_t result;
1856 dns_adbname_t *victim, *next_victim;
1857 isc_boolean_t overmem = adb->overmem;
1858 int scans = 0;
1860 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
1862 max_victims = overmem ? 2 : 1;
1865 * We limit the number of scanned entries to 10 (arbitrary choice)
1866 * in order to avoid examining too many entries when there are many
1867 * tail entries that have fetches (this should be rare, but could
1868 * happen).
1870 victim = ISC_LIST_TAIL(adb->names[bucket]);
1871 for (victims = 0;
1872 victim != NULL && victims < max_victims && scans < 10;
1873 victim = next_victim) {
1874 INSIST(!NAME_DEAD(victim));
1875 scans++;
1876 next_victim = ISC_LIST_PREV(victim, plink);
1877 result = check_expire_name(&victim, now);
1878 if (victim == NULL) {
1879 victims++;
1880 goto next;
1883 if (!NAME_FETCH(victim) &&
1884 (overmem || victim->last_used + ADB_STALE_MARGIN <= now)) {
1885 RUNTIME_CHECK(kill_name(&victim,
1886 DNS_EVENT_ADBCANCELED) ==
1887 ISC_FALSE);
1888 victims++;
1891 next:
1892 if (!overmem)
1893 break;
1898 * Entry bucket must be locked; adb may be locked; no other locks held.
1900 static isc_boolean_t
1901 check_expire_entry(dns_adb_t *adb, dns_adbentry_t **entryp, isc_stdtime_t now)
1903 dns_adbentry_t *entry;
1904 isc_boolean_t result = ISC_FALSE;
1906 INSIST(entryp != NULL && DNS_ADBENTRY_VALID(*entryp));
1907 entry = *entryp;
1909 if (entry->refcnt != 0)
1910 return (result);
1912 if (entry->expires == 0 || entry->expires > now)
1913 return (result);
1916 * The entry is not in use. Delete it.
1918 DP(DEF_LEVEL, "killing entry %p", entry);
1919 INSIST(ISC_LINK_LINKED(entry, plink));
1920 result = unlink_entry(adb, entry);
1921 free_adbentry(adb, &entry);
1922 if (result)
1923 dec_adb_irefcnt(adb);
1924 *entryp = NULL;
1925 return (result);
1929 * ADB must be locked, and no other locks held.
1931 static isc_boolean_t
1932 cleanup_names(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
1933 dns_adbname_t *name;
1934 dns_adbname_t *next_name;
1935 isc_boolean_t result = ISC_FALSE;
1937 DP(CLEAN_LEVEL, "cleaning name bucket %d", bucket);
1939 LOCK(&adb->namelocks[bucket]);
1940 if (adb->name_sd[bucket]) {
1941 UNLOCK(&adb->namelocks[bucket]);
1942 return (result);
1945 name = ISC_LIST_HEAD(adb->names[bucket]);
1946 while (name != NULL) {
1947 next_name = ISC_LIST_NEXT(name, plink);
1948 INSIST(result == ISC_FALSE);
1949 result = check_expire_namehooks(name, now);
1950 if (!result)
1951 result = check_expire_name(&name, now);
1952 name = next_name;
1954 UNLOCK(&adb->namelocks[bucket]);
1955 return (result);
1959 * ADB must be locked, and no other locks held.
1961 static isc_boolean_t
1962 cleanup_entries(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
1963 dns_adbentry_t *entry, *next_entry;
1964 isc_boolean_t result = ISC_FALSE;
1966 DP(CLEAN_LEVEL, "cleaning entry bucket %d", bucket);
1968 LOCK(&adb->entrylocks[bucket]);
1969 entry = ISC_LIST_HEAD(adb->entries[bucket]);
1970 while (entry != NULL) {
1971 next_entry = ISC_LIST_NEXT(entry, plink);
1972 INSIST(result == ISC_FALSE);
1973 result = check_expire_entry(adb, &entry, now);
1974 entry = next_entry;
1976 UNLOCK(&adb->entrylocks[bucket]);
1977 return (result);
1980 static void
1981 destroy(dns_adb_t *adb) {
1982 adb->magic = 0;
1984 isc_task_detach(&adb->task);
1986 isc_mempool_destroy(&adb->nmp);
1987 isc_mempool_destroy(&adb->nhmp);
1988 isc_mempool_destroy(&adb->limp);
1989 isc_mempool_destroy(&adb->emp);
1990 isc_mempool_destroy(&adb->ahmp);
1991 isc_mempool_destroy(&adb->aimp);
1992 isc_mempool_destroy(&adb->afmp);
1994 DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
1995 DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
1997 DESTROYLOCK(&adb->reflock);
1998 DESTROYLOCK(&adb->lock);
1999 DESTROYLOCK(&adb->mplock);
2000 DESTROYLOCK(&adb->overmemlock);
2002 isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
2007 * Public functions.
2010 isc_result_t
2011 dns_adb_create(isc_mem_t *mem, dns_view_t *view, isc_timermgr_t *timermgr,
2012 isc_taskmgr_t *taskmgr, dns_adb_t **newadb)
2014 dns_adb_t *adb;
2015 isc_result_t result;
2016 int i;
2018 REQUIRE(mem != NULL);
2019 REQUIRE(view != NULL);
2020 REQUIRE(timermgr != NULL); /* this is actually unused */
2021 REQUIRE(taskmgr != NULL);
2022 REQUIRE(newadb != NULL && *newadb == NULL);
2024 UNUSED(timermgr);
2026 adb = isc_mem_get(mem, sizeof(dns_adb_t));
2027 if (adb == NULL)
2028 return (ISC_R_NOMEMORY);
2031 * Initialize things here that cannot fail, and especially things
2032 * that must be NULL for the error return to work properly.
2034 adb->magic = 0;
2035 adb->erefcnt = 1;
2036 adb->irefcnt = 0;
2037 adb->nmp = NULL;
2038 adb->nhmp = NULL;
2039 adb->limp = NULL;
2040 adb->emp = NULL;
2041 adb->ahmp = NULL;
2042 adb->aimp = NULL;
2043 adb->afmp = NULL;
2044 adb->task = NULL;
2045 adb->mctx = NULL;
2046 adb->view = view;
2047 adb->taskmgr = taskmgr;
2048 adb->next_cleanbucket = 0;
2049 ISC_EVENT_INIT(&adb->cevent, sizeof(adb->cevent), 0, NULL,
2050 DNS_EVENT_ADBCONTROL, shutdown_task, adb,
2051 adb, NULL, NULL);
2052 adb->cevent_sent = ISC_FALSE;
2053 adb->shutting_down = ISC_FALSE;
2054 adb->overmem = ISC_FALSE;
2055 ISC_LIST_INIT(adb->whenshutdown);
2057 isc_mem_attach(mem, &adb->mctx);
2059 result = isc_mutex_init(&adb->lock);
2060 if (result != ISC_R_SUCCESS)
2061 goto fail0b;
2063 result = isc_mutex_init(&adb->mplock);
2064 if (result != ISC_R_SUCCESS)
2065 goto fail0c;
2067 result = isc_mutex_init(&adb->reflock);
2068 if (result != ISC_R_SUCCESS)
2069 goto fail0d;
2071 result = isc_mutex_init(&adb->overmemlock);
2072 if (result != ISC_R_SUCCESS)
2073 goto fail0e;
2076 * Initialize the bucket locks for names and elements.
2077 * May as well initialize the list heads, too.
2079 result = isc_mutexblock_init(adb->namelocks, NBUCKETS);
2080 if (result != ISC_R_SUCCESS)
2081 goto fail1;
2082 for (i = 0; i < NBUCKETS; i++) {
2083 ISC_LIST_INIT(adb->names[i]);
2084 ISC_LIST_INIT(adb->deadnames[i]);
2085 adb->name_sd[i] = ISC_FALSE;
2086 adb->name_refcnt[i] = 0;
2087 adb->irefcnt++;
2089 for (i = 0; i < NBUCKETS; i++) {
2090 ISC_LIST_INIT(adb->entries[i]);
2091 ISC_LIST_INIT(adb->deadentries[i]);
2092 adb->entry_sd[i] = ISC_FALSE;
2093 adb->entry_refcnt[i] = 0;
2094 adb->irefcnt++;
2096 result = isc_mutexblock_init(adb->entrylocks, NBUCKETS);
2097 if (result != ISC_R_SUCCESS)
2098 goto fail2;
2101 * Memory pools
2103 #define MPINIT(t, p, n) do { \
2104 result = isc_mempool_create(mem, sizeof(t), &(p)); \
2105 if (result != ISC_R_SUCCESS) \
2106 goto fail3; \
2107 isc_mempool_setfreemax((p), FREE_ITEMS); \
2108 isc_mempool_setfillcount((p), FILL_COUNT); \
2109 isc_mempool_setname((p), n); \
2110 isc_mempool_associatelock((p), &adb->mplock); \
2111 } while (0)
2113 MPINIT(dns_adbname_t, adb->nmp, "adbname");
2114 MPINIT(dns_adbnamehook_t, adb->nhmp, "adbnamehook");
2115 MPINIT(dns_adblameinfo_t, adb->limp, "adblameinfo");
2116 MPINIT(dns_adbentry_t, adb->emp, "adbentry");
2117 MPINIT(dns_adbfind_t, adb->ahmp, "adbfind");
2118 MPINIT(dns_adbaddrinfo_t, adb->aimp, "adbaddrinfo");
2119 MPINIT(dns_adbfetch_t, adb->afmp, "adbfetch");
2121 #undef MPINIT
2124 * Allocate an internal task.
2126 result = isc_task_create(adb->taskmgr, 0, &adb->task);
2127 if (result != ISC_R_SUCCESS)
2128 goto fail3;
2129 isc_task_setname(adb->task, "ADB", adb);
2132 * Normal return.
2134 adb->magic = DNS_ADB_MAGIC;
2135 *newadb = adb;
2136 return (ISC_R_SUCCESS);
2138 fail3:
2139 if (adb->task != NULL)
2140 isc_task_detach(&adb->task);
2142 /* clean up entrylocks */
2143 DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
2145 fail2: /* clean up namelocks */
2146 DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
2148 fail1: /* clean up only allocated memory */
2149 if (adb->nmp != NULL)
2150 isc_mempool_destroy(&adb->nmp);
2151 if (adb->nhmp != NULL)
2152 isc_mempool_destroy(&adb->nhmp);
2153 if (adb->limp != NULL)
2154 isc_mempool_destroy(&adb->limp);
2155 if (adb->emp != NULL)
2156 isc_mempool_destroy(&adb->emp);
2157 if (adb->ahmp != NULL)
2158 isc_mempool_destroy(&adb->ahmp);
2159 if (adb->aimp != NULL)
2160 isc_mempool_destroy(&adb->aimp);
2161 if (adb->afmp != NULL)
2162 isc_mempool_destroy(&adb->afmp);
2164 DESTROYLOCK(&adb->overmemlock);
2165 fail0e:
2166 DESTROYLOCK(&adb->reflock);
2167 fail0d:
2168 DESTROYLOCK(&adb->mplock);
2169 fail0c:
2170 DESTROYLOCK(&adb->lock);
2171 fail0b:
2172 isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
2174 return (result);
2177 void
2178 dns_adb_attach(dns_adb_t *adb, dns_adb_t **adbx) {
2180 REQUIRE(DNS_ADB_VALID(adb));
2181 REQUIRE(adbx != NULL && *adbx == NULL);
2183 inc_adb_erefcnt(adb);
2184 *adbx = adb;
2187 void
2188 dns_adb_detach(dns_adb_t **adbx) {
2189 dns_adb_t *adb;
2190 isc_boolean_t need_exit_check;
2192 REQUIRE(adbx != NULL && DNS_ADB_VALID(*adbx));
2194 adb = *adbx;
2195 *adbx = NULL;
2197 INSIST(adb->erefcnt > 0);
2199 LOCK(&adb->reflock);
2200 adb->erefcnt--;
2201 need_exit_check = ISC_TF(adb->erefcnt == 0 && adb->irefcnt == 0);
2202 UNLOCK(&adb->reflock);
2204 if (need_exit_check) {
2205 LOCK(&adb->lock);
2206 INSIST(adb->shutting_down);
2207 check_exit(adb);
2208 UNLOCK(&adb->lock);
2212 void
2213 dns_adb_whenshutdown(dns_adb_t *adb, isc_task_t *task, isc_event_t **eventp) {
2214 isc_task_t *clone;
2215 isc_event_t *event;
2216 isc_boolean_t zeroirefcnt = ISC_FALSE;
2219 * Send '*eventp' to 'task' when 'adb' has shutdown.
2222 REQUIRE(DNS_ADB_VALID(adb));
2223 REQUIRE(eventp != NULL);
2225 event = *eventp;
2226 *eventp = NULL;
2228 LOCK(&adb->lock);
2230 LOCK(&adb->reflock);
2231 zeroirefcnt = ISC_TF(adb->irefcnt == 0);
2233 if (adb->shutting_down && zeroirefcnt &&
2234 isc_mempool_getallocated(adb->ahmp) == 0) {
2236 * We're already shutdown. Send the event.
2238 event->ev_sender = adb;
2239 isc_task_send(task, &event);
2240 } else {
2241 clone = NULL;
2242 isc_task_attach(task, &clone);
2243 event->ev_sender = clone;
2244 ISC_LIST_APPEND(adb->whenshutdown, event, ev_link);
2247 UNLOCK(&adb->reflock);
2248 UNLOCK(&adb->lock);
2251 void
2252 dns_adb_shutdown(dns_adb_t *adb) {
2253 isc_boolean_t need_check_exit;
2256 * Shutdown 'adb'.
2259 LOCK(&adb->lock);
2261 if (!adb->shutting_down) {
2262 adb->shutting_down = ISC_TRUE;
2263 isc_mem_setwater(adb->mctx, water, adb, 0, 0);
2264 need_check_exit = shutdown_names(adb);
2265 if (!need_check_exit)
2266 need_check_exit = shutdown_entries(adb);
2267 if (need_check_exit)
2268 check_exit(adb);
2271 UNLOCK(&adb->lock);
2274 isc_result_t
2275 dns_adb_createfind(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
2276 void *arg, dns_name_t *name, dns_name_t *qname,
2277 dns_rdatatype_t qtype, unsigned int options,
2278 isc_stdtime_t now, dns_name_t *target,
2279 in_port_t port, dns_adbfind_t **findp)
2281 dns_adbfind_t *find;
2282 dns_adbname_t *adbname;
2283 int bucket;
2284 isc_boolean_t want_event, start_at_zone, alias, have_address;
2285 isc_result_t result;
2286 unsigned int wanted_addresses;
2287 unsigned int wanted_fetches;
2288 unsigned int query_pending;
2290 REQUIRE(DNS_ADB_VALID(adb));
2291 if (task != NULL) {
2292 REQUIRE(action != NULL);
2294 REQUIRE(name != NULL);
2295 REQUIRE(qname != NULL);
2296 REQUIRE(findp != NULL && *findp == NULL);
2297 REQUIRE(target == NULL || dns_name_hasbuffer(target));
2299 REQUIRE((options & DNS_ADBFIND_ADDRESSMASK) != 0);
2301 result = ISC_R_UNEXPECTED;
2302 wanted_addresses = (options & DNS_ADBFIND_ADDRESSMASK);
2303 wanted_fetches = 0;
2304 query_pending = 0;
2305 want_event = ISC_FALSE;
2306 start_at_zone = ISC_FALSE;
2307 alias = ISC_FALSE;
2309 if (now == 0)
2310 isc_stdtime_get(&now);
2313 * XXXMLG Move this comment somewhere else!
2315 * Look up the name in our internal database.
2317 * Possibilities: Note that these are not always exclusive.
2319 * No name found. In this case, allocate a new name header and
2320 * an initial namehook or two. If any of these allocations
2321 * fail, clean up and return ISC_R_NOMEMORY.
2323 * Name found, valid addresses present. Allocate one addrinfo
2324 * structure for each found and append it to the linked list
2325 * of addresses for this header.
2327 * Name found, queries pending. In this case, if a task was
2328 * passed in, allocate a job id, attach it to the name's job
2329 * list and remember to tell the caller that there will be
2330 * more info coming later.
2333 find = new_adbfind(adb);
2334 if (find == NULL)
2335 return (ISC_R_NOMEMORY);
2337 find->port = port;
2340 * Remember what types of addresses we are interested in.
2342 find->options = options;
2343 find->flags |= wanted_addresses;
2344 if (FIND_WANTEVENT(find)) {
2345 REQUIRE(task != NULL);
2349 * Try to see if we know anything about this name at all.
2351 bucket = DNS_ADB_INVALIDBUCKET;
2352 adbname = find_name_and_lock(adb, name, find->options, &bucket);
2353 if (adb->name_sd[bucket]) {
2354 DP(DEF_LEVEL,
2355 "dns_adb_createfind: returning ISC_R_SHUTTINGDOWN");
2356 RUNTIME_CHECK(free_adbfind(adb, &find) == ISC_FALSE);
2357 result = ISC_R_SHUTTINGDOWN;
2358 goto out;
2362 * Nothing found. Allocate a new adbname structure for this name.
2364 if (adbname == NULL) {
2366 * See if there is any stale name at the end of list, and purge
2367 * it if so.
2369 check_stale_name(adb, bucket, now);
2371 adbname = new_adbname(adb, name);
2372 if (adbname == NULL) {
2373 RUNTIME_CHECK(free_adbfind(adb, &find) == ISC_FALSE);
2374 result = ISC_R_NOMEMORY;
2375 goto out;
2377 link_name(adb, bucket, adbname);
2378 if (FIND_HINTOK(find))
2379 adbname->flags |= NAME_HINT_OK;
2380 if (FIND_GLUEOK(find))
2381 adbname->flags |= NAME_GLUE_OK;
2382 if (FIND_STARTATZONE(find))
2383 adbname->flags |= NAME_STARTATZONE;
2384 } else {
2385 /* Move this name forward in the LRU list */
2386 ISC_LIST_UNLINK(adb->names[bucket], adbname, plink);
2387 ISC_LIST_PREPEND(adb->names[bucket], adbname, plink);
2389 adbname->last_used = now;
2392 * Expire old entries, etc.
2394 RUNTIME_CHECK(check_expire_namehooks(adbname, now) == ISC_FALSE);
2397 * Do we know that the name is an alias?
2399 if (!EXPIRE_OK(adbname->expire_target, now)) {
2401 * Yes, it is.
2403 DP(DEF_LEVEL,
2404 "dns_adb_createfind: name %p is an alias (cached)",
2405 adbname);
2406 alias = ISC_TRUE;
2407 goto post_copy;
2411 * Try to populate the name from the database and/or
2412 * start fetches. First try looking for an A record
2413 * in the database.
2415 if (!NAME_HAS_V4(adbname) && EXPIRE_OK(adbname->expire_v4, now)
2416 && WANT_INET(wanted_addresses)) {
2417 result = dbfind_name(adbname, now, dns_rdatatype_a);
2418 if (result == ISC_R_SUCCESS) {
2419 DP(DEF_LEVEL,
2420 "dns_adb_createfind: found A for name %p in db",
2421 adbname);
2422 goto v6;
2426 * Did we get a CNAME or DNAME?
2428 if (result == DNS_R_ALIAS) {
2429 DP(DEF_LEVEL,
2430 "dns_adb_createfind: name %p is an alias",
2431 adbname);
2432 alias = ISC_TRUE;
2433 goto post_copy;
2437 * If the name doesn't exist at all, don't bother with
2438 * v6 queries; they won't work.
2440 * If the name does exist but we didn't get our data, go
2441 * ahead and try AAAA.
2443 * If the result is neither of these, try a fetch for A.
2445 if (NXDOMAIN_RESULT(result))
2446 goto fetch;
2447 else if (NXRRSET_RESULT(result))
2448 goto v6;
2450 if (!NAME_FETCH_V4(adbname))
2451 wanted_fetches |= DNS_ADBFIND_INET;
2455 if (!NAME_HAS_V6(adbname) && EXPIRE_OK(adbname->expire_v6, now)
2456 && WANT_INET6(wanted_addresses)) {
2457 result = dbfind_name(adbname, now, dns_rdatatype_aaaa);
2458 if (result == ISC_R_SUCCESS) {
2459 DP(DEF_LEVEL,
2460 "dns_adb_createfind: found AAAA for name %p",
2461 adbname);
2462 goto fetch;
2466 * Did we get a CNAME or DNAME?
2468 if (result == DNS_R_ALIAS) {
2469 DP(DEF_LEVEL,
2470 "dns_adb_createfind: name %p is an alias",
2471 adbname);
2472 alias = ISC_TRUE;
2473 goto post_copy;
2477 * Listen to negative cache hints, and don't start
2478 * another query.
2480 if (NCACHE_RESULT(result) || AUTH_NX(result))
2481 goto fetch;
2483 if (!NAME_FETCH_V6(adbname))
2484 wanted_fetches |= DNS_ADBFIND_INET6;
2487 fetch:
2488 if ((WANT_INET(wanted_addresses) && NAME_HAS_V4(adbname)) ||
2489 (WANT_INET6(wanted_addresses) && NAME_HAS_V6(adbname)))
2490 have_address = ISC_TRUE;
2491 else
2492 have_address = ISC_FALSE;
2493 if (wanted_fetches != 0 &&
2494 ! (FIND_AVOIDFETCHES(find) && have_address)) {
2496 * We're missing at least one address family. Either the
2497 * caller hasn't instructed us to avoid fetches, or we don't
2498 * know anything about any of the address families that would
2499 * be acceptable so we have to launch fetches.
2502 if (FIND_STARTATZONE(find))
2503 start_at_zone = ISC_TRUE;
2506 * Start V4.
2508 if (WANT_INET(wanted_fetches) &&
2509 fetch_name(adbname, start_at_zone,
2510 dns_rdatatype_a) == ISC_R_SUCCESS) {
2511 DP(DEF_LEVEL,
2512 "dns_adb_createfind: started A fetch for name %p",
2513 adbname);
2517 * Start V6.
2519 if (WANT_INET6(wanted_fetches) &&
2520 fetch_name(adbname, start_at_zone,
2521 dns_rdatatype_aaaa) == ISC_R_SUCCESS) {
2522 DP(DEF_LEVEL,
2523 "dns_adb_createfind: "
2524 "started AAAA fetch for name %p",
2525 adbname);
2530 * Run through the name and copy out the bits we are
2531 * interested in.
2533 copy_namehook_lists(adb, find, qname, qtype, adbname, now);
2535 post_copy:
2536 if (NAME_FETCH_V4(adbname))
2537 query_pending |= DNS_ADBFIND_INET;
2538 if (NAME_FETCH_V6(adbname))
2539 query_pending |= DNS_ADBFIND_INET6;
2542 * Attach to the name's query list if there are queries
2543 * already running, and we have been asked to.
2545 want_event = ISC_TRUE;
2546 if (!FIND_WANTEVENT(find))
2547 want_event = ISC_FALSE;
2548 if (FIND_WANTEMPTYEVENT(find) && FIND_HAS_ADDRS(find))
2549 want_event = ISC_FALSE;
2550 if ((wanted_addresses & query_pending) == 0)
2551 want_event = ISC_FALSE;
2552 if (alias)
2553 want_event = ISC_FALSE;
2554 if (want_event) {
2555 find->adbname = adbname;
2556 find->name_bucket = bucket;
2557 ISC_LIST_APPEND(adbname->finds, find, plink);
2558 find->query_pending = (query_pending & wanted_addresses);
2559 find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
2560 find->flags |= (find->query_pending & DNS_ADBFIND_ADDRESSMASK);
2561 DP(DEF_LEVEL, "createfind: attaching find %p to adbname %p",
2562 find, adbname);
2563 } else {
2565 * Remove the flag so the caller knows there will never
2566 * be an event, and set internal flags to fake that
2567 * the event was sent and freed, so dns_adb_destroyfind() will
2568 * do the right thing.
2570 find->query_pending = (query_pending & wanted_addresses);
2571 find->options &= ~DNS_ADBFIND_WANTEVENT;
2572 find->flags |= (FIND_EVENT_SENT | FIND_EVENT_FREED);
2573 find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
2576 find->partial_result |= (adbname->partial_result & wanted_addresses);
2577 if (alias) {
2578 if (target != NULL) {
2579 result = dns_name_copy(&adbname->target, target, NULL);
2580 if (result != ISC_R_SUCCESS)
2581 goto out;
2583 result = DNS_R_ALIAS;
2584 } else
2585 result = ISC_R_SUCCESS;
2588 * Copy out error flags from the name structure into the find.
2590 find->result_v4 = find_err_map[adbname->fetch_err];
2591 find->result_v6 = find_err_map[adbname->fetch6_err];
2593 out:
2594 if (find != NULL) {
2595 *findp = find;
2597 if (want_event) {
2598 isc_task_t *taskp;
2600 INSIST((find->flags & DNS_ADBFIND_ADDRESSMASK) != 0);
2601 taskp = NULL;
2602 isc_task_attach(task, &taskp);
2603 find->event.ev_sender = taskp;
2604 find->event.ev_action = action;
2605 find->event.ev_arg = arg;
2609 UNLOCK(&adb->namelocks[bucket]);
2611 return (result);
2614 void
2615 dns_adb_destroyfind(dns_adbfind_t **findp) {
2616 dns_adbfind_t *find;
2617 dns_adbentry_t *entry;
2618 dns_adbaddrinfo_t *ai;
2619 int bucket;
2620 dns_adb_t *adb;
2622 REQUIRE(findp != NULL && DNS_ADBFIND_VALID(*findp));
2623 find = *findp;
2624 *findp = NULL;
2626 LOCK(&find->lock);
2628 DP(DEF_LEVEL, "dns_adb_destroyfind on find %p", find);
2630 adb = find->adb;
2631 REQUIRE(DNS_ADB_VALID(adb));
2633 REQUIRE(FIND_EVENTFREED(find));
2635 bucket = find->name_bucket;
2636 INSIST(bucket == DNS_ADB_INVALIDBUCKET);
2638 UNLOCK(&find->lock);
2641 * The find doesn't exist on any list, and nothing is locked.
2642 * Return the find to the memory pool, and decrement the adb's
2643 * reference count.
2645 ai = ISC_LIST_HEAD(find->list);
2646 while (ai != NULL) {
2647 ISC_LIST_UNLINK(find->list, ai, publink);
2648 entry = ai->entry;
2649 ai->entry = NULL;
2650 INSIST(DNS_ADBENTRY_VALID(entry));
2651 RUNTIME_CHECK(dec_entry_refcnt(adb, entry, ISC_TRUE) ==
2652 ISC_FALSE);
2653 free_adbaddrinfo(adb, &ai);
2654 ai = ISC_LIST_HEAD(find->list);
2658 * WARNING: The find is freed with the adb locked. This is done
2659 * to avoid a race condition where we free the find, some other
2660 * thread tests to see if it should be destroyed, detects it should
2661 * be, destroys it, and then we try to lock it for our check, but the
2662 * lock is destroyed.
2664 LOCK(&adb->lock);
2665 if (free_adbfind(adb, &find))
2666 check_exit(adb);
2667 UNLOCK(&adb->lock);
2670 void
2671 dns_adb_cancelfind(dns_adbfind_t *find) {
2672 isc_event_t *ev;
2673 isc_task_t *task;
2674 dns_adb_t *adb;
2675 int bucket;
2676 int unlock_bucket;
2678 LOCK(&find->lock);
2680 DP(DEF_LEVEL, "dns_adb_cancelfind on find %p", find);
2682 adb = find->adb;
2683 REQUIRE(DNS_ADB_VALID(adb));
2685 REQUIRE(!FIND_EVENTFREED(find));
2686 REQUIRE(FIND_WANTEVENT(find));
2688 bucket = find->name_bucket;
2689 if (bucket == DNS_ADB_INVALIDBUCKET)
2690 goto cleanup;
2693 * We need to get the adbname's lock to unlink the find.
2695 unlock_bucket = bucket;
2696 violate_locking_hierarchy(&find->lock, &adb->namelocks[unlock_bucket]);
2697 bucket = find->name_bucket;
2698 if (bucket != DNS_ADB_INVALIDBUCKET) {
2699 ISC_LIST_UNLINK(find->adbname->finds, find, plink);
2700 find->adbname = NULL;
2701 find->name_bucket = DNS_ADB_INVALIDBUCKET;
2703 UNLOCK(&adb->namelocks[unlock_bucket]);
2704 bucket = DNS_ADB_INVALIDBUCKET;
2706 cleanup:
2708 if (!FIND_EVENTSENT(find)) {
2709 ev = &find->event;
2710 task = ev->ev_sender;
2711 ev->ev_sender = find;
2712 ev->ev_type = DNS_EVENT_ADBCANCELED;
2713 ev->ev_destroy = event_free;
2714 ev->ev_destroy_arg = find;
2715 find->result_v4 = ISC_R_CANCELED;
2716 find->result_v6 = ISC_R_CANCELED;
2718 DP(DEF_LEVEL, "sending event %p to task %p for find %p",
2719 ev, task, find);
2721 isc_task_sendanddetach(&task, (isc_event_t **)&ev);
2724 UNLOCK(&find->lock);
2727 void
2728 dns_adb_dump(dns_adb_t *adb, FILE *f) {
2729 int i;
2730 isc_stdtime_t now;
2732 REQUIRE(DNS_ADB_VALID(adb));
2733 REQUIRE(f != NULL);
2736 * Lock the adb itself, lock all the name buckets, then lock all
2737 * the entry buckets. This should put the adb into a state where
2738 * nothing can change, so we can iterate through everything and
2739 * print at our leisure.
2742 LOCK(&adb->lock);
2743 isc_stdtime_get(&now);
2745 for (i = 0; i < NBUCKETS; i++)
2746 RUNTIME_CHECK(cleanup_names(adb, i, now) == ISC_FALSE);
2747 for (i = 0; i < NBUCKETS; i++)
2748 RUNTIME_CHECK(cleanup_entries(adb, i, now) == ISC_FALSE);
2750 dump_adb(adb, f, ISC_FALSE, now);
2751 UNLOCK(&adb->lock);
2754 static void
2755 dump_ttl(FILE *f, const char *legend, isc_stdtime_t value, isc_stdtime_t now) {
2756 if (value == INT_MAX)
2757 return;
2758 fprintf(f, " [%s TTL %d]", legend, value - now);
2761 static void
2762 dump_adb(dns_adb_t *adb, FILE *f, isc_boolean_t debug, isc_stdtime_t now) {
2763 int i;
2764 dns_adbname_t *name;
2765 dns_adbentry_t *entry;
2767 fprintf(f, ";\n; Address database dump\n;\n");
2768 if (debug)
2769 fprintf(f, "; addr %p, erefcnt %u, irefcnt %u, finds out %u\n",
2770 adb, adb->erefcnt, adb->irefcnt,
2771 isc_mempool_getallocated(adb->nhmp));
2773 for (i = 0; i < NBUCKETS; i++)
2774 LOCK(&adb->namelocks[i]);
2775 for (i = 0; i < NBUCKETS; i++)
2776 LOCK(&adb->entrylocks[i]);
2779 * Dump the names
2781 for (i = 0; i < NBUCKETS; i++) {
2782 name = ISC_LIST_HEAD(adb->names[i]);
2783 if (name == NULL)
2784 continue;
2785 if (debug)
2786 fprintf(f, "; bucket %d\n", i);
2787 for (;
2788 name != NULL;
2789 name = ISC_LIST_NEXT(name, plink))
2791 if (debug)
2792 fprintf(f, "; name %p (flags %08x)\n",
2793 name, name->flags);
2795 fprintf(f, "; ");
2796 print_dns_name(f, &name->name);
2797 if (dns_name_countlabels(&name->target) > 0) {
2798 fprintf(f, " alias ");
2799 print_dns_name(f, &name->target);
2802 dump_ttl(f, "v4", name->expire_v4, now);
2803 dump_ttl(f, "v6", name->expire_v6, now);
2804 dump_ttl(f, "target", name->expire_target, now);
2806 fprintf(f, " [v4 %s] [v6 %s]",
2807 errnames[name->fetch_err],
2808 errnames[name->fetch6_err]);
2810 fprintf(f, "\n");
2812 print_namehook_list(f, "v4", &name->v4, debug, now);
2813 print_namehook_list(f, "v6", &name->v6, debug, now);
2815 if (debug)
2816 print_fetch_list(f, name);
2817 if (debug)
2818 print_find_list(f, name);
2823 fprintf(f, ";\n; Unassociated entries\n;\n");
2825 for (i = 0; i < NBUCKETS; i++) {
2826 entry = ISC_LIST_HEAD(adb->entries[i]);
2827 while (entry != NULL) {
2828 if (entry->refcnt == 0)
2829 dump_entry(f, entry, debug, now);
2830 entry = ISC_LIST_NEXT(entry, plink);
2835 * Unlock everything
2837 for (i = 0; i < NBUCKETS; i++)
2838 UNLOCK(&adb->entrylocks[i]);
2839 for (i = 0; i < NBUCKETS; i++)
2840 UNLOCK(&adb->namelocks[i]);
2843 static void
2844 dump_entry(FILE *f, dns_adbentry_t *entry, isc_boolean_t debug,
2845 isc_stdtime_t now)
2847 char addrbuf[ISC_NETADDR_FORMATSIZE];
2848 char typebuf[DNS_RDATATYPE_FORMATSIZE];
2849 isc_netaddr_t netaddr;
2850 dns_adblameinfo_t *li;
2852 isc_netaddr_fromsockaddr(&netaddr, &entry->sockaddr);
2853 isc_netaddr_format(&netaddr, addrbuf, sizeof(addrbuf));
2855 if (debug)
2856 fprintf(f, ";\t%p: refcnt %u\n", entry, entry->refcnt);
2858 fprintf(f, ";\t%s [srtt %u] [flags %08x]",
2859 addrbuf, entry->srtt, entry->flags);
2860 if (entry->expires != 0)
2861 fprintf(f, " [ttl %d]", entry->expires - now);
2862 fprintf(f, "\n");
2863 for (li = ISC_LIST_HEAD(entry->lameinfo);
2864 li != NULL;
2865 li = ISC_LIST_NEXT(li, plink)) {
2866 fprintf(f, ";\t\t");
2867 print_dns_name(f, &li->qname);
2868 dns_rdatatype_format(li->qtype, typebuf, sizeof(typebuf));
2869 fprintf(f, " %s [lame TTL %d]\n", typebuf,
2870 li->lame_timer - now);
2874 void
2875 dns_adb_dumpfind(dns_adbfind_t *find, FILE *f) {
2876 char tmp[512];
2877 const char *tmpp;
2878 dns_adbaddrinfo_t *ai;
2879 isc_sockaddr_t *sa;
2882 * Not used currently, in the API Just In Case we
2883 * want to dump out the name and/or entries too.
2886 LOCK(&find->lock);
2888 fprintf(f, ";Find %p\n", find);
2889 fprintf(f, ";\tqpending %08x partial %08x options %08x flags %08x\n",
2890 find->query_pending, find->partial_result,
2891 find->options, find->flags);
2892 fprintf(f, ";\tname_bucket %d, name %p, event sender %p\n",
2893 find->name_bucket, find->adbname, find->event.ev_sender);
2895 ai = ISC_LIST_HEAD(find->list);
2896 if (ai != NULL)
2897 fprintf(f, "\tAddresses:\n");
2898 while (ai != NULL) {
2899 sa = &ai->sockaddr;
2900 switch (sa->type.sa.sa_family) {
2901 case AF_INET:
2902 tmpp = inet_ntop(AF_INET, &sa->type.sin.sin_addr,
2903 tmp, sizeof(tmp));
2904 break;
2905 case AF_INET6:
2906 tmpp = inet_ntop(AF_INET6, &sa->type.sin6.sin6_addr,
2907 tmp, sizeof(tmp));
2908 break;
2909 default:
2910 tmpp = "UnkFamily";
2913 if (tmpp == NULL)
2914 tmpp = "BadAddress";
2916 fprintf(f, "\t\tentry %p, flags %08x"
2917 " srtt %u addr %s\n",
2918 ai->entry, ai->flags, ai->srtt, tmpp);
2920 ai = ISC_LIST_NEXT(ai, publink);
2923 UNLOCK(&find->lock);
2926 static void
2927 print_dns_name(FILE *f, dns_name_t *name) {
2928 char buf[DNS_NAME_FORMATSIZE];
2930 INSIST(f != NULL);
2932 dns_name_format(name, buf, sizeof(buf));
2933 fprintf(f, "%s", buf);
2936 static void
2937 print_namehook_list(FILE *f, const char *legend, dns_adbnamehooklist_t *list,
2938 isc_boolean_t debug, isc_stdtime_t now)
2940 dns_adbnamehook_t *nh;
2942 for (nh = ISC_LIST_HEAD(*list);
2943 nh != NULL;
2944 nh = ISC_LIST_NEXT(nh, plink))
2946 if (debug)
2947 fprintf(f, ";\tHook(%s) %p\n", legend, nh);
2948 dump_entry(f, nh->entry, debug, now);
2952 static inline void
2953 print_fetch(FILE *f, dns_adbfetch_t *ft, const char *type) {
2954 fprintf(f, "\t\tFetch(%s): %p -> { fetch %p }\n",
2955 type, ft, ft->fetch);
2958 static void
2959 print_fetch_list(FILE *f, dns_adbname_t *n) {
2960 if (NAME_FETCH_A(n))
2961 print_fetch(f, n->fetch_a, "A");
2962 if (NAME_FETCH_AAAA(n))
2963 print_fetch(f, n->fetch_aaaa, "AAAA");
2966 static void
2967 print_find_list(FILE *f, dns_adbname_t *name) {
2968 dns_adbfind_t *find;
2970 find = ISC_LIST_HEAD(name->finds);
2971 while (find != NULL) {
2972 dns_adb_dumpfind(find, f);
2973 find = ISC_LIST_NEXT(find, plink);
2977 static isc_result_t
2978 dbfind_name(dns_adbname_t *adbname, isc_stdtime_t now, dns_rdatatype_t rdtype)
2980 isc_result_t result;
2981 dns_rdataset_t rdataset;
2982 dns_adb_t *adb;
2983 dns_fixedname_t foundname;
2984 dns_name_t *fname;
2986 INSIST(DNS_ADBNAME_VALID(adbname));
2987 adb = adbname->adb;
2988 INSIST(DNS_ADB_VALID(adb));
2989 INSIST(rdtype == dns_rdatatype_a || rdtype == dns_rdatatype_aaaa);
2991 dns_fixedname_init(&foundname);
2992 fname = dns_fixedname_name(&foundname);
2993 dns_rdataset_init(&rdataset);
2995 if (rdtype == dns_rdatatype_a)
2996 adbname->fetch_err = FIND_ERR_UNEXPECTED;
2997 else
2998 adbname->fetch6_err = FIND_ERR_UNEXPECTED;
3000 result = dns_view_find(adb->view, &adbname->name, rdtype, now,
3001 NAME_GLUEOK(adbname) ? DNS_DBFIND_GLUEOK : 0,
3002 ISC_TF(NAME_HINTOK(adbname)),
3003 NULL, NULL, fname, &rdataset, NULL);
3005 /* XXXVIX this switch statement is too sparse to gen a jump table. */
3006 switch (result) {
3007 case DNS_R_GLUE:
3008 case DNS_R_HINT:
3009 case ISC_R_SUCCESS:
3011 * Found in the database. Even if we can't copy out
3012 * any information, return success, or else a fetch
3013 * will be made, which will only make things worse.
3015 if (rdtype == dns_rdatatype_a)
3016 adbname->fetch_err = FIND_ERR_SUCCESS;
3017 else
3018 adbname->fetch6_err = FIND_ERR_SUCCESS;
3019 result = import_rdataset(adbname, &rdataset, now);
3020 break;
3021 case DNS_R_NXDOMAIN:
3022 case DNS_R_NXRRSET:
3024 * We're authoritative and the data doesn't exist.
3025 * Make up a negative cache entry so we don't ask again
3026 * for a while.
3028 * XXXRTH What time should we use? I'm putting in 30 seconds
3029 * for now.
3031 if (rdtype == dns_rdatatype_a) {
3032 adbname->expire_v4 = now + 30;
3033 DP(NCACHE_LEVEL,
3034 "adb name %p: Caching auth negative entry for A",
3035 adbname);
3036 if (result == DNS_R_NXDOMAIN)
3037 adbname->fetch_err = FIND_ERR_NXDOMAIN;
3038 else
3039 adbname->fetch_err = FIND_ERR_NXRRSET;
3040 } else {
3041 DP(NCACHE_LEVEL,
3042 "adb name %p: Caching auth negative entry for AAAA",
3043 adbname);
3044 adbname->expire_v6 = now + 30;
3045 if (result == DNS_R_NXDOMAIN)
3046 adbname->fetch6_err = FIND_ERR_NXDOMAIN;
3047 else
3048 adbname->fetch6_err = FIND_ERR_NXRRSET;
3050 break;
3051 case DNS_R_NCACHENXDOMAIN:
3052 case DNS_R_NCACHENXRRSET:
3054 * We found a negative cache entry. Pull the TTL from it
3055 * so we won't ask again for a while.
3057 rdataset.ttl = ttlclamp(rdataset.ttl);
3058 if (rdtype == dns_rdatatype_a) {
3059 adbname->expire_v4 = rdataset.ttl + now;
3060 if (result == DNS_R_NCACHENXDOMAIN)
3061 adbname->fetch_err = FIND_ERR_NXDOMAIN;
3062 else
3063 adbname->fetch_err = FIND_ERR_NXRRSET;
3064 DP(NCACHE_LEVEL,
3065 "adb name %p: Caching negative entry for A (ttl %u)",
3066 adbname, rdataset.ttl);
3067 } else {
3068 DP(NCACHE_LEVEL,
3069 "adb name %p: Caching negative entry for AAAA (ttl %u)",
3070 adbname, rdataset.ttl);
3071 adbname->expire_v6 = rdataset.ttl + now;
3072 if (result == DNS_R_NCACHENXDOMAIN)
3073 adbname->fetch6_err = FIND_ERR_NXDOMAIN;
3074 else
3075 adbname->fetch6_err = FIND_ERR_NXRRSET;
3077 break;
3078 case DNS_R_CNAME:
3079 case DNS_R_DNAME:
3081 * Clear the hint and glue flags, so this will match
3082 * more often.
3084 adbname->flags &= ~(DNS_ADBFIND_GLUEOK | DNS_ADBFIND_HINTOK);
3086 rdataset.ttl = ttlclamp(rdataset.ttl);
3087 clean_target(adb, &adbname->target);
3088 adbname->expire_target = INT_MAX;
3089 result = set_target(adb, &adbname->name, fname, &rdataset,
3090 &adbname->target);
3091 if (result == ISC_R_SUCCESS) {
3092 result = DNS_R_ALIAS;
3093 DP(NCACHE_LEVEL,
3094 "adb name %p: caching alias target",
3095 adbname);
3096 adbname->expire_target = rdataset.ttl + now;
3098 if (rdtype == dns_rdatatype_a)
3099 adbname->fetch_err = FIND_ERR_SUCCESS;
3100 else
3101 adbname->fetch6_err = FIND_ERR_SUCCESS;
3102 break;
3105 if (dns_rdataset_isassociated(&rdataset))
3106 dns_rdataset_disassociate(&rdataset);
3108 return (result);
3111 static void
3112 fetch_callback(isc_task_t *task, isc_event_t *ev) {
3113 dns_fetchevent_t *dev;
3114 dns_adbname_t *name;
3115 dns_adb_t *adb;
3116 dns_adbfetch_t *fetch;
3117 int bucket;
3118 isc_eventtype_t ev_status;
3119 isc_stdtime_t now;
3120 isc_result_t result;
3121 unsigned int address_type;
3122 isc_boolean_t want_check_exit = ISC_FALSE;
3124 UNUSED(task);
3126 INSIST(ev->ev_type == DNS_EVENT_FETCHDONE);
3127 dev = (dns_fetchevent_t *)ev;
3128 name = ev->ev_arg;
3129 INSIST(DNS_ADBNAME_VALID(name));
3130 adb = name->adb;
3131 INSIST(DNS_ADB_VALID(adb));
3133 bucket = name->lock_bucket;
3134 LOCK(&adb->namelocks[bucket]);
3136 INSIST(NAME_FETCH_A(name) || NAME_FETCH_AAAA(name));
3137 address_type = 0;
3138 if (NAME_FETCH_A(name) && (name->fetch_a->fetch == dev->fetch)) {
3139 address_type = DNS_ADBFIND_INET;
3140 fetch = name->fetch_a;
3141 name->fetch_a = NULL;
3142 } else if (NAME_FETCH_AAAA(name)
3143 && (name->fetch_aaaa->fetch == dev->fetch)) {
3144 address_type = DNS_ADBFIND_INET6;
3145 fetch = name->fetch_aaaa;
3146 name->fetch_aaaa = NULL;
3147 } else
3148 fetch = NULL;
3150 INSIST(address_type != 0 && fetch != NULL);
3152 dns_resolver_destroyfetch(&fetch->fetch);
3153 dev->fetch = NULL;
3155 ev_status = DNS_EVENT_ADBNOMOREADDRESSES;
3158 * Cleanup things we don't care about.
3160 if (dev->node != NULL)
3161 dns_db_detachnode(dev->db, &dev->node);
3162 if (dev->db != NULL)
3163 dns_db_detach(&dev->db);
3166 * If this name is marked as dead, clean up, throwing away
3167 * potentially good data.
3169 if (NAME_DEAD(name)) {
3170 free_adbfetch(adb, &fetch);
3171 isc_event_free(&ev);
3173 want_check_exit = kill_name(&name, DNS_EVENT_ADBCANCELED);
3175 UNLOCK(&adb->namelocks[bucket]);
3177 if (want_check_exit) {
3178 LOCK(&adb->lock);
3179 check_exit(adb);
3180 UNLOCK(&adb->lock);
3183 return;
3186 isc_stdtime_get(&now);
3189 * If we got a negative cache response, remember it.
3191 if (NCACHE_RESULT(dev->result)) {
3192 dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
3193 if (address_type == DNS_ADBFIND_INET) {
3194 DP(NCACHE_LEVEL, "adb fetch name %p: "
3195 "caching negative entry for A (ttl %u)",
3196 name, dev->rdataset->ttl);
3197 name->expire_v4 = ISC_MIN(name->expire_v4,
3198 dev->rdataset->ttl + now);
3199 if (dev->result == DNS_R_NCACHENXDOMAIN)
3200 name->fetch_err = FIND_ERR_NXDOMAIN;
3201 else
3202 name->fetch_err = FIND_ERR_NXRRSET;
3203 inc_stats(adb, dns_resstatscounter_gluefetchv4fail);
3204 } else {
3205 DP(NCACHE_LEVEL, "adb fetch name %p: "
3206 "caching negative entry for AAAA (ttl %u)",
3207 name, dev->rdataset->ttl);
3208 name->expire_v6 = ISC_MIN(name->expire_v6,
3209 dev->rdataset->ttl + now);
3210 if (dev->result == DNS_R_NCACHENXDOMAIN)
3211 name->fetch6_err = FIND_ERR_NXDOMAIN;
3212 else
3213 name->fetch6_err = FIND_ERR_NXRRSET;
3214 inc_stats(adb, dns_resstatscounter_gluefetchv6fail);
3216 goto out;
3220 * Handle CNAME/DNAME.
3222 if (dev->result == DNS_R_CNAME || dev->result == DNS_R_DNAME) {
3223 dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
3224 clean_target(adb, &name->target);
3225 name->expire_target = INT_MAX;
3226 result = set_target(adb, &name->name,
3227 dns_fixedname_name(&dev->foundname),
3228 dev->rdataset,
3229 &name->target);
3230 if (result == ISC_R_SUCCESS) {
3231 DP(NCACHE_LEVEL,
3232 "adb fetch name %p: caching alias target",
3233 name);
3234 name->expire_target = dev->rdataset->ttl + now;
3236 goto check_result;
3240 * Did we get back junk? If so, and there are no more fetches
3241 * sitting out there, tell all the finds about it.
3243 if (dev->result != ISC_R_SUCCESS) {
3244 char buf[DNS_NAME_FORMATSIZE];
3246 dns_name_format(&name->name, buf, sizeof(buf));
3247 DP(DEF_LEVEL, "adb: fetch of '%s' %s failed: %s",
3248 buf, address_type == DNS_ADBFIND_INET ? "A" : "AAAA",
3249 dns_result_totext(dev->result));
3250 /* XXXMLG Don't pound on bad servers. */
3251 if (address_type == DNS_ADBFIND_INET) {
3252 name->expire_v4 = ISC_MIN(name->expire_v4, now + 300);
3253 name->fetch_err = FIND_ERR_FAILURE;
3254 inc_stats(adb, dns_resstatscounter_gluefetchv4fail);
3255 } else {
3256 name->expire_v6 = ISC_MIN(name->expire_v6, now + 300);
3257 name->fetch6_err = FIND_ERR_FAILURE;
3258 inc_stats(adb, dns_resstatscounter_gluefetchv6fail);
3260 goto out;
3264 * We got something potentially useful.
3266 result = import_rdataset(name, &fetch->rdataset, now);
3268 check_result:
3269 if (result == ISC_R_SUCCESS) {
3270 ev_status = DNS_EVENT_ADBMOREADDRESSES;
3271 if (address_type == DNS_ADBFIND_INET)
3272 name->fetch_err = FIND_ERR_SUCCESS;
3273 else
3274 name->fetch6_err = FIND_ERR_SUCCESS;
3277 out:
3278 free_adbfetch(adb, &fetch);
3279 isc_event_free(&ev);
3281 clean_finds_at_name(name, ev_status, address_type);
3283 UNLOCK(&adb->namelocks[bucket]);
3286 static isc_result_t
3287 fetch_name(dns_adbname_t *adbname,
3288 isc_boolean_t start_at_zone,
3289 dns_rdatatype_t type)
3291 isc_result_t result;
3292 dns_adbfetch_t *fetch = NULL;
3293 dns_adb_t *adb;
3294 dns_fixedname_t fixed;
3295 dns_name_t *name;
3296 dns_rdataset_t rdataset;
3297 dns_rdataset_t *nameservers;
3298 unsigned int options;
3300 INSIST(DNS_ADBNAME_VALID(adbname));
3301 adb = adbname->adb;
3302 INSIST(DNS_ADB_VALID(adb));
3304 INSIST((type == dns_rdatatype_a && !NAME_FETCH_V4(adbname)) ||
3305 (type == dns_rdatatype_aaaa && !NAME_FETCH_V6(adbname)));
3307 adbname->fetch_err = FIND_ERR_NOTFOUND;
3309 name = NULL;
3310 nameservers = NULL;
3311 dns_rdataset_init(&rdataset);
3313 options = DNS_FETCHOPT_NOVALIDATE;
3314 if (start_at_zone) {
3315 DP(ENTER_LEVEL,
3316 "fetch_name: starting at zone for name %p",
3317 adbname);
3318 dns_fixedname_init(&fixed);
3319 name = dns_fixedname_name(&fixed);
3320 result = dns_view_findzonecut2(adb->view, &adbname->name, name,
3321 0, 0, ISC_TRUE, ISC_FALSE,
3322 &rdataset, NULL);
3323 if (result != ISC_R_SUCCESS && result != DNS_R_HINT)
3324 goto cleanup;
3325 nameservers = &rdataset;
3326 options |= DNS_FETCHOPT_UNSHARED;
3329 fetch = new_adbfetch(adb);
3330 if (fetch == NULL) {
3331 result = ISC_R_NOMEMORY;
3332 goto cleanup;
3335 result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
3336 type, name, nameservers, NULL,
3337 options, adb->task, fetch_callback,
3338 adbname, &fetch->rdataset, NULL,
3339 &fetch->fetch);
3340 if (result != ISC_R_SUCCESS)
3341 goto cleanup;
3343 if (type == dns_rdatatype_a) {
3344 adbname->fetch_a = fetch;
3345 inc_stats(adb, dns_resstatscounter_gluefetchv4);
3346 } else {
3347 adbname->fetch_aaaa = fetch;
3348 inc_stats(adb, dns_resstatscounter_gluefetchv6);
3350 fetch = NULL; /* Keep us from cleaning this up below. */
3352 cleanup:
3353 if (fetch != NULL)
3354 free_adbfetch(adb, &fetch);
3355 if (dns_rdataset_isassociated(&rdataset))
3356 dns_rdataset_disassociate(&rdataset);
3358 return (result);
3362 * XXXMLG Needs to take a find argument and an address info, no zone or adb,
3363 * since these can be extracted from the find itself.
3365 isc_result_t
3366 dns_adb_marklame(dns_adb_t *adb, dns_adbaddrinfo_t *addr, dns_name_t *qname,
3367 dns_rdatatype_t qtype, isc_stdtime_t expire_time)
3369 dns_adblameinfo_t *li;
3370 int bucket;
3371 isc_result_t result = ISC_R_SUCCESS;
3373 REQUIRE(DNS_ADB_VALID(adb));
3374 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3375 REQUIRE(qname != NULL);
3377 bucket = addr->entry->lock_bucket;
3378 LOCK(&adb->entrylocks[bucket]);
3379 li = ISC_LIST_HEAD(addr->entry->lameinfo);
3380 while (li != NULL &&
3381 (li->qtype != qtype || !dns_name_equal(qname, &li->qname)))
3382 li = ISC_LIST_NEXT(li, plink);
3383 if (li != NULL) {
3384 if (expire_time > li->lame_timer)
3385 li->lame_timer = expire_time;
3386 goto unlock;
3388 li = new_adblameinfo(adb, qname, qtype);
3389 if (li == NULL) {
3390 result = ISC_R_NOMEMORY;
3391 goto unlock;
3394 li->lame_timer = expire_time;
3396 ISC_LIST_PREPEND(addr->entry->lameinfo, li, plink);
3397 unlock:
3398 UNLOCK(&adb->entrylocks[bucket]);
3400 return (result);
3403 void
3404 dns_adb_adjustsrtt(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
3405 unsigned int rtt, unsigned int factor)
3407 int bucket;
3408 unsigned int new_srtt;
3409 isc_stdtime_t now;
3411 REQUIRE(DNS_ADB_VALID(adb));
3412 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3413 REQUIRE(factor <= 10);
3415 bucket = addr->entry->lock_bucket;
3416 LOCK(&adb->entrylocks[bucket]);
3418 if (factor == DNS_ADB_RTTADJAGE)
3419 new_srtt = addr->entry->srtt * 98 / 100;
3420 else
3421 new_srtt = (addr->entry->srtt / 10 * factor)
3422 + (rtt / 10 * (10 - factor));
3424 addr->entry->srtt = new_srtt;
3425 addr->srtt = new_srtt;
3427 isc_stdtime_get(&now);
3428 addr->entry->expires = now + ADB_ENTRY_WINDOW;
3430 UNLOCK(&adb->entrylocks[bucket]);
3433 void
3434 dns_adb_changeflags(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
3435 unsigned int bits, unsigned int mask)
3437 int bucket;
3439 REQUIRE(DNS_ADB_VALID(adb));
3440 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3442 bucket = addr->entry->lock_bucket;
3443 LOCK(&adb->entrylocks[bucket]);
3445 addr->entry->flags = (addr->entry->flags & ~mask) | (bits & mask);
3447 * Note that we do not update the other bits in addr->flags with
3448 * the most recent values from addr->entry->flags.
3450 addr->flags = (addr->flags & ~mask) | (bits & mask);
3452 UNLOCK(&adb->entrylocks[bucket]);
3455 isc_result_t
3456 dns_adb_findaddrinfo(dns_adb_t *adb, isc_sockaddr_t *sa,
3457 dns_adbaddrinfo_t **addrp, isc_stdtime_t now)
3459 int bucket;
3460 dns_adbentry_t *entry;
3461 dns_adbaddrinfo_t *addr;
3462 isc_result_t result;
3463 in_port_t port;
3465 REQUIRE(DNS_ADB_VALID(adb));
3466 REQUIRE(addrp != NULL && *addrp == NULL);
3468 UNUSED(now);
3470 result = ISC_R_SUCCESS;
3471 bucket = DNS_ADB_INVALIDBUCKET;
3472 entry = find_entry_and_lock(adb, sa, &bucket, now);
3473 if (adb->entry_sd[bucket]) {
3474 result = ISC_R_SHUTTINGDOWN;
3475 goto unlock;
3477 if (entry == NULL) {
3479 * We don't know anything about this address.
3481 entry = new_adbentry(adb);
3482 if (entry == NULL) {
3483 result = ISC_R_NOMEMORY;
3484 goto unlock;
3486 entry->sockaddr = *sa;
3487 link_entry(adb, bucket, entry);
3488 DP(ENTER_LEVEL, "findaddrinfo: new entry %p", entry);
3489 } else
3490 DP(ENTER_LEVEL, "findaddrinfo: found entry %p", entry);
3492 port = isc_sockaddr_getport(sa);
3493 addr = new_adbaddrinfo(adb, entry, port);
3494 if (addr == NULL) {
3495 result = ISC_R_NOMEMORY;
3496 } else {
3497 inc_entry_refcnt(adb, entry, ISC_FALSE);
3498 *addrp = addr;
3501 unlock:
3502 UNLOCK(&adb->entrylocks[bucket]);
3504 return (result);
3507 void
3508 dns_adb_freeaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **addrp) {
3509 dns_adbaddrinfo_t *addr;
3510 dns_adbentry_t *entry;
3511 int bucket;
3512 isc_stdtime_t now;
3513 isc_boolean_t want_check_exit = ISC_FALSE;
3515 REQUIRE(DNS_ADB_VALID(adb));
3516 REQUIRE(addrp != NULL);
3517 addr = *addrp;
3518 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3519 entry = addr->entry;
3520 REQUIRE(DNS_ADBENTRY_VALID(entry));
3522 isc_stdtime_get(&now);
3524 *addrp = NULL;
3526 bucket = addr->entry->lock_bucket;
3527 LOCK(&adb->entrylocks[bucket]);
3529 entry->expires = now + ADB_ENTRY_WINDOW;
3531 want_check_exit = dec_entry_refcnt(adb, entry, ISC_FALSE);
3533 UNLOCK(&adb->entrylocks[bucket]);
3535 addr->entry = NULL;
3536 free_adbaddrinfo(adb, &addr);
3538 if (want_check_exit) {
3539 LOCK(&adb->lock);
3540 check_exit(adb);
3541 UNLOCK(&adb->lock);
3545 void
3546 dns_adb_flush(dns_adb_t *adb) {
3547 unsigned int i;
3549 INSIST(DNS_ADB_VALID(adb));
3551 LOCK(&adb->lock);
3554 * Call our cleanup routines.
3556 for (i = 0; i < NBUCKETS; i++)
3557 RUNTIME_CHECK(cleanup_names(adb, i, INT_MAX) == ISC_FALSE);
3558 for (i = 0; i < NBUCKETS; i++)
3559 RUNTIME_CHECK(cleanup_entries(adb, i, INT_MAX) == ISC_FALSE);
3561 #ifdef DUMP_ADB_AFTER_CLEANING
3562 dump_adb(adb, stdout, ISC_TRUE, INT_MAX);
3563 #endif
3565 UNLOCK(&adb->lock);
3568 void
3569 dns_adb_flushname(dns_adb_t *adb, dns_name_t *name) {
3570 dns_adbname_t *adbname;
3571 dns_adbname_t *nextname;
3572 int bucket;
3574 INSIST(DNS_ADB_VALID(adb));
3576 LOCK(&adb->lock);
3577 bucket = dns_name_hash(name, ISC_FALSE) % NBUCKETS;
3578 LOCK(&adb->namelocks[bucket]);
3579 adbname = ISC_LIST_HEAD(adb->names[bucket]);
3580 while (adbname != NULL) {
3581 nextname = ISC_LIST_NEXT(adbname, plink);
3582 if (!NAME_DEAD(adbname) &&
3583 dns_name_equal(name, &adbname->name)) {
3584 RUNTIME_CHECK(kill_name(&adbname,
3585 DNS_EVENT_ADBCANCELED) ==
3586 ISC_FALSE);
3588 adbname = nextname;
3590 UNLOCK(&adb->namelocks[bucket]);
3591 UNLOCK(&adb->lock);
3594 static void
3595 water(void *arg, int mark) {
3596 dns_adb_t *adb = arg;
3597 isc_boolean_t overmem = ISC_TF(mark == ISC_MEM_HIWATER);
3599 REQUIRE(DNS_ADB_VALID(adb));
3601 DP(ISC_LOG_DEBUG(1),
3602 "adb reached %s water mark", overmem ? "high" : "low");
3605 * We can't use adb->lock as there is potential for water
3606 * to be called when adb->lock is held.
3608 LOCK(&adb->overmemlock);
3609 if (adb->overmem != overmem) {
3610 adb->overmem = overmem;
3611 isc_mem_waterack(adb->mctx, mark);
3613 UNLOCK(&adb->overmemlock);
3616 void
3617 dns_adb_setadbsize(dns_adb_t *adb, isc_uint32_t size) {
3618 isc_uint32_t hiwater;
3619 isc_uint32_t lowater;
3621 INSIST(DNS_ADB_VALID(adb));
3623 if (size != 0 && size < DNS_ADB_MINADBSIZE)
3624 size = DNS_ADB_MINADBSIZE;
3626 hiwater = size - (size >> 3); /* Approximately 7/8ths. */
3627 lowater = size - (size >> 2); /* Approximately 3/4ths. */
3629 if (size == 0 || hiwater == 0 || lowater == 0)
3630 isc_mem_setwater(adb->mctx, water, adb, 0, 0);
3631 else
3632 isc_mem_setwater(adb->mctx, water, adb, hiwater, lowater);