adb.c revision 5f1f24822fabe7b78af5f81553bfc11a170326c3
c110d61b173a68420d19858abb80285be0dc1120Tinderbox User * Copyright (C) 1999-2001 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* $Id: adb.c,v 1.209 2003/10/16 05:46:19 marka Exp $ */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Implementation notes
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * --------------------
6b7257f756eb0530cdf54df9a7fab8d51a5001c3David Lawrence * In finds, if task == NULL, no events will be generated, and no events
6b7257f756eb0530cdf54df9a7fab8d51a5001c3David Lawrence * have been sent. If task != NULL but taskaction == NULL, an event has been
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * posted but not yet freed. If neither are NULL, no event was posted.
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence * After we have cleaned all buckets, dump the database contents.
669e9657c731176df235832367f61435f7b83ddfAndreas Gustafsson#include <isc/string.h> /* Required for HP/UX (and others?) */
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington#define DNS_ADB_MAGIC ISC_MAGIC('D', 'a', 'd', 'b')
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
3f96cf3e4f96b36cc1ad2ec7edc5b8e285fced8fBrian Wellington#define DNS_ADBNAME_MAGIC ISC_MAGIC('a', 'd', 'b', 'N')
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington#define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
7318a964ece83f748bc7e9814d8c3a61c2b4d946Mark Andrews#define DNS_ADBNAMEHOOK_MAGIC ISC_MAGIC('a', 'd', 'N', 'H')
4cd765650776027d05fe7fca248478918e02e63bDavid Lawrence#define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
4cd765650776027d05fe7fca248478918e02e63bDavid Lawrence#define DNS_ADBZONEINFO_MAGIC ISC_MAGIC('a', 'd', 'b', 'Z')
80b67b3a4f2d9fc7fdd32a50edc67ff189894da2Danny Mayer#define DNS_ADBZONEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBZONEINFO_MAGIC)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews#define DNS_ADBENTRY_MAGIC ISC_MAGIC('a', 'd', 'b', 'E')
87983da955bf63128de85d180359bdc418516c3cDavid Lawrence#define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#define DNS_ADBFETCH_MAGIC ISC_MAGIC('a', 'd', 'F', '4')
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington#define DNS_ADBFETCH_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews#define DNS_ADBFETCH6_MAGIC ISC_MAGIC('a', 'd', 'F', '6')
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews#define DNS_ADBFETCH6_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH6_MAGIC)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * The number of buckets needs to be a prime (for good hashing).
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt * XXXRTH How many buckets do we need?
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt#define NBUCKETS 1009 /* how many buckets for names/addrs */
4144efb39046963989ad002cf88a0c195401100aJeremy Reed * For type 3 negative cache entries, we will remember that the address is
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt * broken for this long. XXXMLG This is also used for actual addresses, too.
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington * The intent is to keep us from constantly asking about A/AAAA records
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence * if the zone has extremely low TTLs.
2ba574f329c14376d26d7c0f22c89d7a978a2625Mark Andrews#define ADB_CACHE_MAXIMUM 86400 /* seconds (86400 = 24 hours) */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Wake up every CLEAN_SECONDS and clean CLEAN_BUCKETS buckets, so that all
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington * buckets are cleaned in CLEAN_PERIOD seconds.
debd489a44363870f96f75818e89ec27d3cab736Francis Dupont#define CLEAN_BUCKETS ((NBUCKETS * CLEAN_SECONDS) / CLEAN_PERIOD)
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence#define FREE_ITEMS 64 /* free count for memory pools */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#define FILL_COUNT 16 /* fill count for memory pools */
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt#define DNS_ADB_INVALIDBUCKET (-1) /* invalid bucket address */
f7c21e46c4b5fdae516b91374c24a87671f83ea3Andreas Gustafsson#define DNS_ADB_MINADBSIZE (1024*1024) /* 1 Megabyte */
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrewstypedef ISC_LIST(dns_adbname_t) dns_adbnamelist_t;
fc39b6a96109b78154ec148d20eaf29e8abc14b6Mukund Sivaramantypedef struct dns_adbnamehook dns_adbnamehook_t;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrewstypedef ISC_LIST(dns_adbnamehook_t) dns_adbnamehooklist_t;
fc39b6a96109b78154ec148d20eaf29e8abc14b6Mukund Sivaramantypedef struct dns_adbzoneinfo dns_adbzoneinfo_t;
1479200aa05414b2acf33607dbd1682c16f58c51Evan Hunttypedef ISC_LIST(dns_adbentry_t) dns_adbentrylist_t;
5455f30a7532738d750252c00e649890c694ee30Brian Wellingtontypedef struct dns_adbfetch6 dns_adbfetch6_t;
c30d291128e099a284fa6272b91b2bd64519a209Mark Andrews unsigned int magic;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_mutex_t reflock; /* Covers irefcnt, erefcnt */
ecaed3593cd14f2491d1bd81fc98cb940e12f8bbMark Andrews unsigned int irefcnt;
ecaed3593cd14f2491d1bd81fc98cb940e12f8bbMark Andrews unsigned int erefcnt;
547411428e467f2a2848886eaac0a8b3e136a9abEvan Hunt * Bucketized locks and lists for names.
547411428e467f2a2848886eaac0a8b3e136a9abEvan Hunt * XXXRTH Have a per-bucket structure that contains all of these?
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews * Bucketized locks for entries.
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews * XXXRTH Have a per-bucket structure that contains all of these?
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews isc_boolean_t entry_sd[NBUCKETS]; /* shutting down */
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt * XXXMLG Document these structures.
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews unsigned int magic;
9bd876a683709be588f6fac6781a76fdd57b2f08Mark Andrews unsigned int flags;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews unsigned int chains;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews unsigned int magic;
d96f74a3cb6212ac9e4a7a0fa8924f850348eae9Mark Andrews * dns_adbnamehook_t
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence * This is a small widget that dangles off a dns_adbname_t. It contains a
87983da955bf63128de85d180359bdc418516c3cDavid Lawrence * pointer to the address information about this host, and a link to the next
87983da955bf63128de85d180359bdc418516c3cDavid Lawrence * namehook that will contain the next address this host has.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence unsigned int magic;
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * dns_adbzoneinfo_t
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews * This is a small widget that holds zone-specific information about an
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews * address. Currently limited to lameness, but could just as easily be
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews * extended to other types of information about zones.
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews unsigned int magic;
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington * An address entry. It holds quite a bit of information about addresses,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * including edns state (in "flags"), rtt, and of course the address of
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington unsigned int srtt;
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews * A nonzero 'expires' field indicates that the entry should
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrews * persist until that time. This allows entries found
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * using dns_adb_findaddrinfo() to persist for a limited time
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * even though they are not necessarily associated with a
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * Internal functions (and prototypes).
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellingtonstatic inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrencestatic inline void free_adbname(dns_adb_t *, dns_adbname_t **);
e1d05d323526e7e65df13a6d3dfbec30f6ddb500Brian Wellingtonstatic inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrewsstatic inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrewsstatic inline dns_adbzoneinfo_t *new_adbzoneinfo(dns_adb_t *, dns_name_t *);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrewsstatic inline void free_adbzoneinfo(dns_adb_t *, dns_adbzoneinfo_t **);
287910778c57d4836a52b03b697c2ef342d0eaa9Francis Dupontstatic inline dns_adbentry_t *new_adbentry(dns_adb_t *);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrewsstatic inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic inline dns_adbfind_t *new_adbfind(dns_adb_t *);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic inline isc_boolean_t free_adbfind(dns_adb_t *, dns_adbfind_t **);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *, dns_adbentry_t *,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic inline dns_adbfetch_t *new_adbfetch(dns_adb_t *);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic inline void free_adbfetch(dns_adb_t *, dns_adbfetch_t **);
73a691c373488e4f70387a62462cd8ce0d991705David Lawrencestatic inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt unsigned int, int *);
4eb998928b9aef0ceda42d7529980d658138698aEvan Huntstatic inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic void dump_adb(dns_adb_t *, FILE *, isc_boolean_t debug);
a2b15b3305acd52179e6f3dc7d073b07fbc40b8eMark Andrewsstatic void print_dns_name(FILE *, dns_name_t *);
a2b15b3305acd52179e6f3dc7d073b07fbc40b8eMark Andrewsstatic void print_namehook_list(FILE *, const char *legend,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic void print_find_list(FILE *, dns_adbname_t *);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic void print_fetch_list(FILE *, dns_adbname_t *);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic inline isc_boolean_t dec_adb_irefcnt(dns_adb_t *);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic inline void inc_adb_irefcnt(dns_adb_t *);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic inline void inc_adb_erefcnt(dns_adb_t *);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellingtonstatic inline isc_boolean_t dec_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
e7c0d42b11358f08e04316d31c67c23261dcdf36Evan Huntstatic inline void violate_locking_hierarchy(isc_mutex_t *, isc_mutex_t *);
e7c0d42b11358f08e04316d31c67c23261dcdf36Evan Huntstatic isc_boolean_t clean_namehooks(dns_adb_t *, dns_adbnamehooklist_t *);
927e4c9fecf448bf3894c68fcaf9dc2f89557f3aEvan Huntstatic void clean_target(dns_adb_t *, dns_name_t *);
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellingtonstatic void clean_finds_at_name(dns_adbname_t *, isc_eventtype_t,
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington unsigned int);
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellingtonstatic isc_boolean_t check_expire_namehooks(dns_adbname_t *, isc_stdtime_t,
9e804040a29b9c3066c8471b43835f30707039b7Evan Huntstatic void cancel_fetches_at_name(dns_adbname_t *);
9e804040a29b9c3066c8471b43835f30707039b7Evan Huntstatic isc_result_t dbfind_name(dns_adbname_t *, isc_stdtime_t,
9e804040a29b9c3066c8471b43835f30707039b7Evan Huntstatic isc_result_t fetch_name(dns_adbname_t *, isc_boolean_t,
9e804040a29b9c3066c8471b43835f30707039b7Evan Huntstatic void timer_cleanup(isc_task_t *, isc_event_t *);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic isc_boolean_t shutdown_entries(dns_adb_t *);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic inline void link_name(dns_adb_t *, int, dns_adbname_t *);
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrewsstatic inline isc_boolean_t unlink_name(dns_adb_t *, dns_adbname_t *);
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrewsstatic inline void link_entry(dns_adb_t *, int, dns_adbentry_t *);
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrewsstatic inline isc_boolean_t unlink_entry(dns_adb_t *, dns_adbentry_t *);
f07fe5a1ac9d1345eb7a36a0bc38716a03e25f61Mark Andrewsstatic isc_boolean_t kill_name(dns_adbname_t **, isc_eventtype_t);
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * MUST NOT overlap DNS_ADBFIND_* flags!
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#define FIND_EVENTSENT(h) (((h)->flags & FIND_EVENT_SENT) != 0)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#define FIND_EVENTFREED(h) (((h)->flags & FIND_EVENT_FREED) != 0)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#define NAME_STARTATZONE DNS_ADBFIND_STARTATZONE
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#define NAME_DEAD(n) (((n)->flags & NAME_IS_DEAD) != 0)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#define NAME_NEEDSPOKE(n) (((n)->flags & NAME_NEEDS_POKE) != 0)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#define NAME_GLUEOK(n) (((n)->flags & NAME_GLUE_OK) != 0)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#define NAME_HINTOK(n) (((n)->flags & NAME_HINT_OK) != 0)
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews * To the name, address classes are all that really exist. If it has a
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews * V6 address it doesn't care if it came from a AAAA query.
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews#define NAME_HAS_ADDRS(n) (NAME_HAS_V4(n) || NAME_HAS_V6(n))
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * Fetches are broken out into A and AAAA types. In some cases,
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * however, it makes more sense to test for a particular class of fetches,
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * like V4 or V6 above.
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * Note: since we have removed the support of A6 in adb, FETCH_A and FETCH_AAAA
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * are now equal to FETCH_V4 and FETCH_V6, respectively.
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#define NAME_FETCH_AAAA(n) ((n)->fetch_aaaa != NULL)
a2b15b3305acd52179e6f3dc7d073b07fbc40b8eMark Andrews#define NAME_FETCH(n) (NAME_FETCH_V4(n) || NAME_FETCH_V6(n))
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * Find options and tests to see if there are addresses on the list.
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#define FIND_WANTEVENT(fn) (((fn)->options & DNS_ADBFIND_WANTEVENT) != 0)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#define FIND_WANTEMPTYEVENT(fn) (((fn)->options & DNS_ADBFIND_EMPTYEVENT) != 0)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#define FIND_AVOIDFETCHES(fn) (((fn)->options & DNS_ADBFIND_AVOIDFETCHES) \
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#define FIND_STARTATZONE(fn) (((fn)->options & DNS_ADBFIND_STARTATZONE) \
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#define FIND_HINTOK(fn) (((fn)->options & DNS_ADBFIND_HINTOK) != 0)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#define FIND_GLUEOK(fn) (((fn)->options & DNS_ADBFIND_GLUEOK) != 0)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#define FIND_HAS_ADDRS(fn) (!ISC_LIST_EMPTY((fn)->list))
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#define FIND_RETURNLAME(fn) (((fn)->options & DNS_ADBFIND_RETURNLAME) != 0)
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * These are currently used on simple unsigned ints, so they are
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * not really associated with any particular type.
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#define WANT_INET(x) (((x) & DNS_ADBFIND_INET) != 0)
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt#define WANT_INET6(x) (((x) & DNS_ADBFIND_INET6) != 0)
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt#define EXPIRE_OK(exp, now) ((exp == INT_MAX) || (exp < now))
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt * Find out if the flags on a name (nf) indicate if it is a hint or
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt * glue, and compare this to the appropriate bits set in o, to see if
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt * this is ok.
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt#define GLUE_OK(nf, o) (!NAME_GLUEOK(nf) || (((o) & DNS_ADBFIND_GLUEOK) != 0))
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt#define HINT_OK(nf, o) (!NAME_HINTOK(nf) || (((o) & DNS_ADBFIND_HINTOK) != 0))
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt#define GLUEHINT_OK(nf, o) (GLUE_OK(nf, o) || HINT_OK(nf, o))
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt#define STARTATZONE_MATCHES(nf, o) (((nf)->flags & NAME_STARTATZONE) == \
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#define NCACHE_RESULT(r) ((r) == DNS_R_NCACHENXDOMAIN || \
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews#define NXDOMAIN_RESULT(r) ((r) == DNS_R_NXDOMAIN || \
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#define NXRRSET_RESULT(r) ((r) == DNS_R_NCACHENXRRSET || \
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * Error state rankings.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#define FIND_ERR_SUCCESS 0 /* highest rank */
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrewsstatic const char *errnames[] = {
bcdf37e0ff7d73310b7bf247d755194a5718ba38Mark Andrews "unexpected",
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington#define NEWERR(old, new) (ISC_MIN((old), (new)))
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic isc_result_t find_err_map[FIND_ERR_MAX] = {
e32d354f754a5d7847a0862bcd6302827ea225bfEvan HuntDP(int level, const char *format, ...) ISC_FORMAT_PRINTF(2, 3);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * Requires the adbname bucket be locked and that no entry buckets be locked.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * This code handles A and AAAA rdatasets only.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonimport_rdataset(dns_adbname_t *adbname, dns_rdataset_t *rdataset,
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews INSIST((rdtype == dns_rdatatype_a) || (rdtype == dns_rdatatype_aaaa));
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews DP(NCACHE_LEVEL, "expire_v4 set to MIN(%u,%u) import_rdataset",
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews adbname->expire_v4 = ISC_MIN(adbname->expire_v4,
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) import_rdataset",
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews adbname->expire_v6 = ISC_MIN(adbname->expire_v6,
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * Lie a little here. This is more or less so code that cares
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * can find out if any new information was added or not.
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * Requires the name's bucket be locked.
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrewskill_name(dns_adbname_t **n, isc_eventtype_t ev) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * If we're dead already, just check to see if we should go
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * away now or not.
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt * Clean up the name's various lists. These two are destructive
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt * in that they will always empty the list.
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt clean_finds_at_name(name, ev, DNS_ADBFIND_ADDRESSMASK);
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt * If fetches are running, cancel them. If none are running, we can
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt * just kill the name here.
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * Requires the name's bucket be locked and no entry buckets be locked.
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrewscheck_expire_namehooks(dns_adbname_t *name, isc_stdtime_t now,
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews * Check to see if we need to remove the v4 addresses
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews DP(DEF_LEVEL, "expiring v4 for name %p", name);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews * Check to see if we need to remove the v6 addresses
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews DP(DEF_LEVEL, "expiring v6 for name %p", name);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews * Check to see if we need to remove the alias target.
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews if (expire || EXPIRE_OK(name->expire_target, now)) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews * Requires the name's bucket be locked.
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrewsstatic inline void
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrewslink_name(dns_adb_t *adb, int bucket, dns_adbname_t *name) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews INSIST(name->lock_bucket == DNS_ADB_INVALIDBUCKET);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews ISC_LIST_PREPEND(adb->names[bucket], name, plink);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews * Requires the name's bucket be locked.
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrewsunlink_name(dns_adb_t *adb, dns_adbname_t *name) {
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews ISC_LIST_UNLINK(adb->names[bucket], name, plink);
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews * Requires the entry's bucket be locked.
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrewsstatic inline void
73a691c373488e4f70387a62462cd8ce0d991705David Lawrencelink_entry(dns_adb_t *adb, int bucket, dns_adbentry_t *entry) {
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * Requires the entry's bucket be locked.
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellingtonunlink_entry(dns_adb_t *adb, dns_adbentry_t *entry) {
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
bfafdac0616107ff32389532e7040567cd84b8aaBrian Wellington if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrewsstatic inline void
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrewsviolate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews * The ADB _MUST_ be locked before calling. Also, exit conditions must be
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews * checked after calling this function.
c4f9e613e12f03795bee18cf2ca8e6a9d39d6468Mark Andrews for (bucket = 0; bucket < NBUCKETS; bucket++) {
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence * This bucket has no names. We must decrement the
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence * irefcnt ourselves, since it will not be
4144efb39046963989ad002cf88a0c195401100aJeremy Reed * automatically triggered by a name being unlinked.
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * Run through the list. For each name, clean up finds
5096958739769958dd7a6b69356bf41260033873David Lawrence * found there, and cancel any fetches running. When
b39ad8a69bc2859b99c9f5a63d916789b566e470Andreas Gustafsson * all the fetches are canceled, the name will destroy
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt * The ADB _MUST_ be locked before calling. Also, exit conditions must be
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt * checked after calling this function.
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence for (bucket = 0; bucket < NBUCKETS; bucket++) {
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews * This bucket has no entries. We must decrement the
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews * irefcnt ourselves, since it will not be
0f8c9b5eed7e8714ceb7d6d3675555df9c5f6350Mark Andrews * automatically triggered by an entry being unlinked.
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence * Run through the list. Cleanup any entries not
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * associated with names, and which are not in use.
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt * Name bucket must be locked
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellingtoncancel_fetches_at_name(dns_adbname_t *name) {
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington dns_resolver_cancelfetch(name->fetch_a->fetch);
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington dns_resolver_cancelfetch(name->fetch_aaaa->fetch);
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington * Assumes the name bucket is locked.
d7ba3622ffa20c653ef6c8cfae42d8cd26465b7fBrian Wellingtonclean_namehooks(dns_adb_t *adb, dns_adbnamehooklist_t *namehooks) {
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson * Clean up the entry if needed.
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson if (addr_bucket != DNS_ADB_INVALIDBUCKET)
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson result = dec_entry_refcnt(adb, entry, ISC_FALSE);
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson * Free the namehook
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence ISC_LIST_UNLINK(*namehooks, namehook, plink);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafssonclean_target(dns_adb_t *adb, dns_name_t *target) {
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellingtonset_target(dns_adb_t *adb, dns_name_t *name, dns_name_t *fname,
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington dns_rdataset_t *rdataset, dns_name_t *target)
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt * Copy the CNAME's target into the target name.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence result = dns_rdata_tostruct(&rdata, &cname, NULL);
return (result);
return (result);
return (result);
return (result);
return (result);
NULL);
return (result);
return (result);
return (ISC_R_SUCCESS);
unsigned int addrs)
switch (evtype) {
if ((notify) != 0) {
if (wanted == 0)
if (process) {
static inline isc_boolean_t
return (result);
int bucket;
if (lock)
if (lock)
static inline isc_boolean_t
int bucket;
if (lock)
if (lock)
if (!destroy_entry)
return (result);
if (result)
return (result);
static inline dns_adbname_t *
return (NULL);
return (NULL);
return (name);
dns_adbname_t *n;
n = *name;
n->magic = 0;
static inline dns_adbnamehook_t *
return (NULL);
return (nh);
static inline dns_adbzoneinfo_t *
return (NULL);
return (NULL);
return (zi);
static inline dns_adbentry_t *
dns_adbentry_t *e;
isc_uint32_t r;
if (e == NULL)
return (NULL);
e->refcnt = 0;
e->flags = 0;
isc_random_get(&r);
e->expires = 0;
dns_adbentry_t *e;
e = *entry;
e->magic = 0;
static inline dns_adbfind_t *
dns_adbfind_t *h;
if (h == NULL)
return (NULL);
h->magic = 0;
h->partial_result = 0;
h->options = 0;
h->flags = 0;
return (NULL);
static inline dns_adbfetch_t *
dns_adbfetch_t *f;
if (f == NULL)
return (NULL);
f->magic = 0;
goto err;
goto err;
err:
return (NULL);
dns_adbfetch_t *f;
f = *fetch;
f->magic = 0;
static inline isc_boolean_t
static inline dns_adbaddrinfo_t *
return (NULL);
return (ai);
static inline dns_adbname_t *
int bucket;
return (adbname);
return (NULL);
static inline dns_adbentry_t *
int bucket;
return (entry);
return (NULL);
static isc_boolean_t
return (ISC_FALSE);
return (is_bad);
int bucket;
goto nextv4;
goto out;
goto nextv6;
goto out;
out:
static isc_boolean_t
return (result);
return (result);
return (result);
return (result);
return (result);
return (result);
static isc_boolean_t
return (result);
return (result);
if (result)
return (result);
static isc_boolean_t
return (result);
if (!result)
return (result);
static isc_boolean_t
return (result);
for (i = 0; i < CLEAN_BUCKETS; i++) {
== ISC_FALSE);
#ifdef DUMP_ADB_AFTER_CLEANING
return (ISC_R_NOMEMORY);
goto fail0b;
goto fail0c;
goto fail0d;
goto fail1;
for (i = 0; i < NBUCKETS; i++) {
for (i = 0; i < NBUCKETS; i++) {
goto fail2;
#define MPINIT(t, p, n) do { \
goto fail3; \
isc_mempool_setname((p), n); \
goto fail3;
goto fail3;
return (ISC_R_SUCCESS);
return (result);
if (need_exit_check) {
if (!need_check_exit)
if (need_check_exit)
int bucket;
unsigned int wanted_addresses;
unsigned int wanted_fetches;
unsigned int query_pending;
wanted_fetches = 0;
query_pending = 0;
if (now == 0)
return (ISC_R_NOMEMORY);
goto out;
goto out;
adbname);
goto post_copy;
adbname);
goto v6;
adbname);
goto post_copy;
goto fetch;
goto v6;
v6:
adbname);
goto fetch;
adbname);
goto post_copy;
goto fetch;
if (wanted_fetches != 0 &&
adbname);
adbname);
if (alias)
if (want_event) {
if (alias) {
goto out;
out:
if (want_event) {
return (result);
int bucket;
int bucket;
int unlock_bucket;
goto cleanup;
if (debug)
for (i = 0; i < NBUCKETS; i++)
for (i = 0; i < NBUCKETS; i++)
for (i = 0; i < NBUCKETS; i++) {
if (debug)
if (debug)
if (debug)
if (debug)
for (i = 0; i < NBUCKETS; i++)
for (i = 0; i < NBUCKETS; i++)
if (debug)
const char *tmpp;
case AF_INET:
case AF_INET6:
if (debug)
if (NAME_FETCH_A(n))
if (NAME_FETCH_AAAA(n))
static isc_result_t
switch (result) {
case DNS_R_GLUE:
case DNS_R_HINT:
case ISC_R_SUCCESS:
case DNS_R_NXDOMAIN:
case DNS_R_NXRRSET:
adbname);
adbname);
case DNS_R_NCACHENXDOMAIN:
case DNS_R_NCACHENXRRSET:
case DNS_R_CNAME:
case DNS_R_DNAME:
adbname);
return (result);
int bucket;
unsigned int address_type;
address_type = 0;
if (want_check_exit) {
goto out;
name);
goto check_result;
goto out;
out:
static isc_result_t
unsigned int options;
options = 0;
if (start_at_zone) {
adbname);
goto cleanup;
goto cleanup;
goto cleanup;
return (result);
int bucket;
return (ISC_R_NOMEMORY);
return (ISC_R_SUCCESS);
int bucket;
unsigned int new_srtt;
int bucket;
int bucket;
goto unlock;
goto unlock;
return (result);
int bucket;
if (want_check_exit) {
for (i = 0; i < NBUCKETS; i++) {
#ifdef DUMP_ADB_AFTER_CLEANING
int bucket;
if (overmem) {