adb.c revision fe197676cc48d9b81940a0031a340bb5d23e1dcb
5fa46bc91672ef5737aee6f99763161511566c24Tinderbox User * Copyright (C) 1999 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
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * Implementation notes
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * --------------------
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley * In finds, if task == NULL, no events will be generated, and no events
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley * have been sent. If task != NULL but taskaction == NULL, an event has been
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * posted but not yet freed. If neither are NULL, no event was posted.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * After we have cleaned all buckets, dump the database contents.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBNAMEHOOK_MAGIC 0x61644e48 /* adNH. */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBZONEINFO_MAGIC 0x6164625a /* adbZ. */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBZONEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBZONEINFO_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBFETCH_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBFETCH6_MAGIC 0x61644636 /* adF6. */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBFETCH6_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH6_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * The number of buckets needs to be a prime (for good hashing).
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * XXXRTH How many buckets do we need?
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * This value must be coordinated with CLEAN_SECONDS (below).
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define NBUCKETS 1009 /* how many buckets for names/addrs */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * For type 3 negative cache entries, we will remember that the address is
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * broken for this long. XXXMLG This is also used for actual addresses, too.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * The intent is to keep us from constantly asking about A/A6/AAAA records
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * if the zone has extremely low TTLs.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Clean one bucket every CLEAN_SECONDS.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FREE_ITEMS 16 /* free count for memory pools */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FILL_COUNT 8 /* fill count for memory pools */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADB_INVALIDBUCKET (-1) /* invalid bucket address */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleytypedef ISC_LIST(dns_adbname_t) dns_adbnamelist_t;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleytypedef ISC_LIST(dns_adbnamehook_t) dns_adbnamehooklist_t;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleytypedef ISC_LIST(dns_adbentry_t) dns_adbentrylist_t;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley unsigned int magic;
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley unsigned int irefcnt;
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley unsigned int erefcnt;
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley * Bucketized locks and lists for names.
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley * XXXRTH Have a per-bucket structure that contains all of these?
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson * Bucketized locks for entries.
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson * XXXRTH Have a per-bucket structure that contains all of these?
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson * XXXMLG Document these structures.
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews unsigned int chains;
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews unsigned int magic;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence unsigned int magic;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int flags;
da527e4ff6a013364826637963e7ac372e024f33David Lawrence * dns_adbnamehook_t
da527e4ff6a013364826637963e7ac372e024f33David Lawrence * This is a small widget that dangles off a dns_adbname_t. It contains a
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington * pointer to the address information about this host, and a link to the next
da527e4ff6a013364826637963e7ac372e024f33David Lawrence * namehook that will contain the next address this host has.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley unsigned int magic;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * dns_adbzoneinfo_t
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * This is a small widget that holds zone-specific information about an
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * address. Currently limited to lameness, but could just as easily be
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * extended to other types of information about zones.
ce3761f64d3d734cc94605026985898900ecc474Bob Halley unsigned int magic;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * An address entry. It holds quite a bit of information about addresses,
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * including edns state, rtt, and of course the address of the host.
ce3761f64d3d734cc94605026985898900ecc474Bob Halley unsigned int magic;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley unsigned int refcnt;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence unsigned int flags;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley unsigned int srtt;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * Internal functions (and prototypes).
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencestatic inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleystatic inline void free_adbname(dns_adb_t *, dns_adbname_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbzoneinfo_t *new_adbzoneinfo(dns_adb_t *, dns_name_t *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void free_adbzoneinfo(dns_adb_t *, dns_adbzoneinfo_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbentry_t *new_adbentry(dns_adb_t *);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halleystatic inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halleystatic inline dns_adbfind_t *new_adbfind(dns_adb_t *);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halleystatic inline void free_adbfind(dns_adb_t *, dns_adbfind_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline dns_adbfetch_t *new_adbfetch(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void free_adbfetch(dns_adb_t *, dns_adbfetch_t **);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline dns_adbfetch6_t *new_adbfetch6(dns_adb_t *, dns_adbname_t *,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void free_adbfetch6(dns_adb_t *, dns_adbfetch6_t **);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void print_dns_name(FILE *, dns_name_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void print_namehook_list(FILE *, dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void print_find_list(FILE *, dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void print_fetch_list(FILE *, dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void dec_adb_irefcnt(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void inc_adb_erefcnt(dns_adb_t *, isc_boolean_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void dec_adb_erefcnt(dns_adb_t *, isc_boolean_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updaterstatic inline void dec_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void violate_locking_hierarchy(isc_mutex_t *, isc_mutex_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void clean_namehooks(dns_adb_t *, dns_adbnamehooklist_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void clean_target(dns_adb_t *, dns_name_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void clean_finds_at_name(dns_adbname_t *, isc_eventtype_t,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews unsigned int);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void check_expire_namehooks(dns_adbname_t *, isc_stdtime_t);
86548554f6180bbe051c8cd8f03c93fc9b6a7825Mark Andrewsstatic void cancel_fetches_at_name(dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_result_t dbfind_name(dns_adbname_t *, isc_stdtime_t,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_result_t fetch_name_v4(dns_adbname_t *, isc_boolean_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_result_t fetch_name_aaaa(dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_result_t fetch_name_a6(dns_adbname_t *, isc_boolean_t,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void timer_cleanup(isc_task_t *, isc_event_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void link_name(dns_adb_t *, int, dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void unlink_name(dns_adb_t *, dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void link_entry(dns_adb_t *, int, dns_adbentry_t *);
86548554f6180bbe051c8cd8f03c93fc9b6a7825Mark Andrewsstatic inline void unlink_entry(dns_adb_t *, dns_adbentry_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void kill_name(dns_adbname_t **, isc_eventtype_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void fetch_callback_a6(isc_task_t *, isc_event_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_result_t dbfind_a6(dns_adbname_t *, isc_stdtime_t, isc_boolean_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * MUST NOT overlap DNS_ADBFIND_* flags!
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_EVENTSENT(h) (((h)->flags & FIND_EVENT_SENT) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_EVENTFREED(h) (((h)->flags & FIND_EVENT_FREED) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_DEAD(n) (((n)->flags & NAME_IS_DEAD) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_NEEDSPOKE(n) (((n)->flags & NAME_NEEDS_POKE) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * To the name, address classes are all that really exist. If it has a
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * V6 address it doesn't care if it came from an A6 chain or an AAAA query.
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_HAS_V4(n) (!ISC_LIST_EMPTY((n)->v4))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_HAS_V6(n) (!ISC_LIST_EMPTY((n)->v6))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_HAS_ADDRS(n) (NAME_HAS_V4(n) || NAME_HAS_V6(n))
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater * Fetches are broken out into A, AAAA, and A6 types. In some cases,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * however, it makes more sense to test for a particular class of fetches,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * like V4 or V6 above.
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_FETCH_AAAA(n) ((n)->fetch_aaaa != NULL)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_FETCH_A6(n) (!ISC_LIST_EMPTY((n)->fetches_a6))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_FETCH_V6(n) (NAME_FETCH_AAAA(n) || NAME_FETCH_A6(n))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_FETCH(n) (NAME_FETCH_V4(n) || NAME_FETCH_V6(n))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * Was this fetch started using the hints database?
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * Was this the initial fetch for the A6 record? If so, we might want to
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * start AAAA queries if it fails.
86548554f6180bbe051c8cd8f03c93fc9b6a7825Mark Andrews#define FETCH_USEHINTS(f) (((f)->flags & FETCH_USE_HINTS) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FETCH_FIRSTA6(f) (((f)->flags & FETCH_FIRST_A6) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * Find options and tests to see if there are addresses on the list.
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_WANTEVENT(fn) (((fn)->options & DNS_ADBFIND_WANTEVENT) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_WANTEMPTYEVENT(fn) (((fn)->options & DNS_ADBFIND_EMPTYEVENT) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_AVOIDFETCHES(fn) (((fn)->options & DNS_ADBFIND_AVOIDFETCHES) \
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_STARTATROOT(fn) (((fn)->options & DNS_ADBFIND_STARTATROOT) \
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_HAS_ADDRS(fn) (!ISC_LIST_EMPTY((fn)->list))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * These are currently used on simple unsigned ints, so they are
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley * not really associated with any particular type.
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define WANT_INET(x) (((x) & DNS_ADBFIND_INET) != 0)
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define WANT_INET6(x) (((x) & DNS_ADBFIND_INET6) != 0)
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define EXPIRE_OK(exp, now) ((exp == INT_MAX) || (exp < now))
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define NCACHE_RESULT(r) ((r) == DNS_R_NCACHENXDOMAIN || \
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews * Requires the adbname bucket be locked and that no entry buckets be locked.
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews * This code handles A and AAAA rdatasets only.
508f61f8d699c46f962b682f388e54b446a7194dMark Andrewsimport_rdataset(dns_adbname_t *adbname, dns_rdataset_t *rdataset,
f0bbac2c0f1afa74b88cab902daf11202ebe7cbdBob Halley INSIST((rdtype == dns_rdatatype_a) || (rdtype == dns_rdatatype_aaaa));
1366b7833c86343de278480b9abd71754e418bfaBob Halley if (IN6_IS_ADDR_V4MAPPED(&sockaddr.type.sin6.sin6_addr)
1366b7833c86343de278480b9abd71754e418bfaBob Halley || IN6_IS_ADDR_V4COMPAT(&sockaddr.type.sin6.sin6_addr)) {
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington rdataset->ttl = ISC_MAX(rdataset->ttl, ADB_CACHE_MINIMUM);
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington DP(NCACHE_LEVEL, "expire_v4 set to MIN(%u,%u) import_rdataset",
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington adbname->expire_v4 = ISC_MIN(adbname->expire_v4,
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) import_rdataset",
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington adbname->expire_v6 = ISC_MIN(adbname->expire_v6,
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington * Lie a little here. This is more or less so code that cares
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington * can find out if any new information was added or not.
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley DP(ENTER_LEVEL, "ENTER: import_a6() name %p", name);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->partial_result |= DNS_ADBFIND_INET6; /* clear for AAAA */
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley isc_sockaddr_fromin6(&sockaddr, &a6ctx->in6addr, 53);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley if (IN6_IS_ADDR_V4MAPPED(&sockaddr.type.sin6.sin6_addr)
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley || IN6_IS_ADDR_V4COMPAT(&sockaddr.type.sin6.sin6_addr)) {
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) in import_v6",
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews name->expire_v6 = ISC_MIN(name->expire_v6, a6ctx->expiration);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews * Requires the name's bucket be locked.
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewskill_name(dns_adbname_t **n, isc_eventtype_t ev)
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * If we're dead already, just check to see if we should go
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * away now or not.
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * Clean up the name's various lists. These two are destructive
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * in that they will always empty the list.
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley clean_finds_at_name(name, ev, DNS_ADBFIND_ADDRESSMASK);
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 * If fetches are running, cancel them. If none are running, we can
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * just kill the name here.
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * Requires the name's bucket be locked and no entry buckets be locked.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleycheck_expire_namehooks(dns_adbname_t *name, isc_stdtime_t now)
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley * Check to see if we need to remove the v4 addresses
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley if (!NAME_FETCH_V4(name) && EXPIRE_OK(name->expire_v4, now)) {
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley * Check to see if we need to remove the v6 addresses
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley if (!NAME_FETCH_V6(name) && EXPIRE_OK(name->expire_v6, now)) {
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley * Check to see if we need to remove the alias target.
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews * Requires the name's bucket be locked.
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewsstatic inline void
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewslink_name(dns_adb_t *adb, int bucket, dns_adbname_t *name)
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley INSIST(name->lock_bucket == DNS_ADB_INVALIDBUCKET);
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley ISC_LIST_PREPEND(adb->names[bucket], name, plink);
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews * Requires the name's bucket be locked.
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrewsstatic inline void
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrewsunlink_name(dns_adb_t *adb, dns_adbname_t *name)
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews ISC_LIST_UNLINK(adb->names[bucket], name, plink);
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews * Requires the entry's bucket be locked.
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrewsstatic inline void
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrewslink_entry(dns_adb_t *adb, int bucket, dns_adbentry_t *entry)
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews * Requires the entry's bucket be locked.
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrewsstatic inline void
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrewsunlink_entry(dns_adb_t *adb, dns_adbentry_t *entry)
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrewsstatic inline void
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrewsviolate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want)
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews * The ADB _MUST_ be locked before calling. Also, exit conditions must be
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews * checked after calling this function.
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews for (bucket = 0 ; bucket < NBUCKETS ; bucket++) {
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson name = ISC_LIST_HEAD(adb->names[bucket]);
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson * This bucket has no names. We must decrement the
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson * irefcnt ourselves, since it will not be
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson * automatically triggered by a name being unlinked.
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews * Run through the list. For each name, clean up finds
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews * found there, and cancel any fetches running. When
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley * all the fetches are canceled, the name will destroy
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 * The ADB _MUST_ be locked before calling. Also, exit conditions must be
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * checked after calling this function.
5d661f0bde49c68d33eb1146d60058782aca50a7Bob Halley for (bucket = 0 ; bucket < NBUCKETS ; bucket++) {
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington entry = ISC_LIST_HEAD(adb->entries[bucket]);
a8e4c27d2c00e831d1eb7260e3f455d97907d799Bob Halley * This bucket has no entries. We must decrement the
a8e4c27d2c00e831d1eb7260e3f455d97907d799Bob Halley * irefcnt ourselves, since it will not be
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington * automatically triggered by an entry being unlinked.
5d661f0bde49c68d33eb1146d60058782aca50a7Bob Halley * Run through the list. Cleanup any entries not
5d661f0bde49c68d33eb1146d60058782aca50a7Bob Halley * associated with names, and which are not in use.
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * Name bucket must be locked
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_resolver_cancelfetch(name->fetch_aaaa->fetch);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * Assumes the name bucket is locked.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyclean_namehooks(dns_adb_t *adb, dns_adbnamehooklist_t *namehooks)
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson * Clean up the entry if needed.
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson if (addr_bucket != DNS_ADB_INVALIDBUCKET)
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson * Free the namehook
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson ISC_LIST_UNLINK(*namehooks, namehook, plink);
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halleyclean_target(dns_adb_t *adb, dns_name_t *target) {
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halleyset_target(dns_adb_t *adb, dns_name_t *name, dns_name_t *fname,
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley * Copy the CNAME's target into the target name.
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley result = dns_name_dup(&tname, adb->mctx, target);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley namereln = dns_name_fullcompare(name, fname, &order,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Get the target name of the DNAME.
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews * Construct the new target name.
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer result = dns_name_split(name, nlabels, nbits, prefix, NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dns_name_concatenate(prefix, &tname, new_target,
f257e9369c27578eb87077923dc010a6614e2a7aMark Andrews result = dns_name_dup(new_target, adb->mctx, target);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Assumes nothing is locked, since this is called by the client.
216030f2849b0812910fbc1817ca17208a112663Mark Andrews * Assumes the name bucket is locked.
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrewsclean_finds_at_name(dns_adbname_t *name, isc_eventtype_t evtype,
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews unsigned int addrs)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int wanted;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "ENTER clean_finds_at_name, name %p, evtype %08x, addrs %08x",
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley * Unlink the find from the name, letting the caller
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * call dns_adb_destroyfind() on it to clean it up
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "Sending event %p to task %p for find %p",
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(ENTER_LEVEL, "EXIT clean_finds_at_name, name %p", name);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * The caller must be holding the adb lock.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * We're now shutdown. Send any whenshutdown events.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * If there aren't any external references either, we're
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley * done. Send the control event to initiate shutdown.
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewsstatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyinc_adb_erefcnt(dns_adb_t *adb, isc_boolean_t lock)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleydec_adb_erefcnt(dns_adb_t *adb, isc_boolean_t lock)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyinc_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graffstatic inline void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleydec_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (adb->entry_sd[bucket] || entry->expires == 0)) {
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halleystatic inline dns_adbname_t *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (dns_name_dup(dnsname, adb->mctx, &name->name) != ISC_R_SUCCESS) {
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrewsstatic inline void
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrewsfree_adbname(dns_adb_t *adb, dns_adbname_t **name)
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews INSIST(name != NULL && DNS_ADBNAME_VALID(*name));
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews INSIST(n->lock_bucket == DNS_ADB_INVALIDBUCKET);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrewsnew_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry)
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Huntstatic inline void
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Huntfree_adbnamehook(dns_adb_t *adb, dns_adbnamehook_t **namehook)
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt INSIST(namehook != NULL && DNS_ADBNAMEHOOK_VALID(*namehook));
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews if (dns_name_dup(zone, adb->mctx, &zi->zone) != ISC_R_SUCCESS) {
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrewsstatic inline void
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrewsfree_adbzoneinfo(dns_adb_t *adb, dns_adbzoneinfo_t **zoneinfo)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(zoneinfo != NULL && DNS_ADBZONEINFO_VALID(*zoneinfo));
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrencestatic inline void
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrencefree_adbentry(dns_adb_t *adb, dns_adbentry_t **entry)
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence INSIST(entry != NULL && DNS_ADBENTRY_VALID(*entry));
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Huntstatic inline dns_adbfind_t *
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff * public members
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * private members
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "isc_mutex_init failed in new_adbfind()");
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews ISC_EVENT_INIT(&h->event, sizeof (isc_event_t), 0, 0, 0, NULL, NULL,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbfetch_t *
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graffstatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyfree_adbfetch(dns_adb_t *adb, dns_adbfetch_t **fetch)
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington INSIST(fetch != NULL && DNS_ADBFETCH_VALID(*fetch));
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington if (dns_rdataset_isassociated(&f->rdataset))
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * Caller must be holding the name lock.
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtona6find(void *arg, dns_name_t *a6name, dns_rdatatype_t type, isc_stdtime_t now,
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington return (dns_view_simplefind(adb->view, a6name, type, now,
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * Caller must be holding the name lock.
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtona6missing(dns_a6context_t *a6ctx, dns_name_t *a6name) {
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington result = dns_resolver_createfetch(adb->view->resolver, a6name,
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington ISC_LIST_APPEND(name->fetches_a6, fetch, plink);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtonnew_adbfetch6(dns_adb_t *adb, dns_adbname_t *name, dns_a6context_t *a6ctx)
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington dns_a6_init(&f->a6ctx, a6find, NULL, import_a6,
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halleystatic inline void
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halleyfree_adbfetch6(dns_adb_t *adb, dns_adbfetch6_t **fetch)
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley INSIST(fetch != NULL && DNS_ADBFETCH6_VALID(*fetch));
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington if (dns_rdataset_isassociated(&f->rdataset))
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halleystatic inline void
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Grafffree_adbfind(dns_adb_t *adb, dns_adbfind_t **findp)
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington INSIST(findp != NULL && DNS_ADBFIND_VALID(*findp));
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley INSIST(find->name_bucket == DNS_ADB_INVALIDBUCKET);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley * Copy bits from the entry into the newly allocated addrinfo. The entry
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley * must be locked, and the reference count must be bumped up by one
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley * if this function returns a valid pointer.
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halleynew_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyfree_adbaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **ainfo)
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley INSIST(ainfo != NULL && DNS_ADBADDRINFO_VALID(*ainfo));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Search for the name. NOTE: The bucket is kept locked on both
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * success and failure, so it must always be unlocked by the caller!
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * On the first call to this function, *bucketp must be set to
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * DNS_ADB_INVALIDBUCKET.
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halleystatic inline dns_adbname_t *
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencefind_name_and_lock(dns_adb_t *adb, dns_name_t *name, int *bucketp)
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halley bucket = dns_name_hash(name, ISC_FALSE) % NBUCKETS;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Search for the address. NOTE: The bucket is kept locked on both
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * success and failure, so it must always be unlocked by the caller.
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrews * On the first call to this function, *bucketp must be set to
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrews * DNS_ADB_INVALIDBUCKET. This will cause a lock to occur. On
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence * later calls (within the same "lock path") it can be left alone, so
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * if this function is called multiple times locking is only done if
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * the bucket changes.
ce3761f64d3d734cc94605026985898900ecc474Bob Halleystatic inline dns_adbentry_t *
ce3761f64d3d734cc94605026985898900ecc474Bob Halleyfind_entry_and_lock(dns_adb_t *adb, isc_sockaddr_t *addr, int *bucketp)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley bucket = isc_sockaddr_hash(addr, ISC_TRUE) % NBUCKETS;
514aeac2acbbe2b77ff3c4e310617523cf5651c5Mark Andrews * Entry bucket MUST be locked!
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafssonentry_is_bad_for_zone(dns_adb_t *adb, dns_adbentry_t *entry, dns_name_t *zone,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Has the entry expired?
514aeac2acbbe2b77ff3c4e310617523cf5651c5Mark Andrews * Order tests from least to most expensive.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleycopy_namehook_lists(dns_adb_t *adb, dns_adbfind_t *find, dns_name_t *zone,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley addrinfo = new_adbaddrinfo(adb, namehook->entry);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Found a valid entry. Add it to the find's list.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley inc_entry_refcnt(adb, namehook->entry, ISC_FALSE);
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews if (entry_is_bad_for_zone(adb, namehook->entry,
514aeac2acbbe2b77ff3c4e310617523cf5651c5Mark Andrews addrinfo = new_adbaddrinfo(adb, namehook->entry);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Found a valid entry. Add it to the find's list.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley inc_entry_refcnt(adb, namehook->entry, ISC_FALSE);
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyershutdown_task(isc_task_t *task, isc_event_t *ev)
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson * Kill the timer, and then the ADB itself. Note that this implies
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson * that this task was the one scheduled to get timer events. If
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * this is not true (and it is unfortunate there is no way to INSIST()
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * this) badness will occur.
0c264c909424f855e7e3b7ce7bb21f650609e768Brian Wellington * name bucket must be locked; adb may be locked; no other locks held.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleycheck_expire_name(dns_adbname_t **namep, isc_stdtime_t now)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(namep != NULL && DNS_ADBNAME_VALID(*namep));
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * The name is empty. Delete it.
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews * Our caller, or one of its callers, will be calling check_exit() at
29daf5bc7738f1cdab7914562269e1129c81acdcBrian Wellington * some point, so we don't need to do it here.
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington * entry bucket must be locked; adb may be locked; no other locks held.
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellingtoncheck_expire_entry(dns_adb_t *adb, dns_adbentry_t **entryp, isc_stdtime_t now)
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews INSIST(entryp != NULL && DNS_ADBENTRY_VALID(*entryp));
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews if (entry->expires == 0 || entry->expires > now)
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * The entry is not in use. Delete it.
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff * ADB must be locked, and no other locks held.
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrewscleanup_names(dns_adb_t *adb, int bucket, isc_stdtime_t now)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley DP(CLEAN_LEVEL, "cleaning name bucket %d", bucket);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * ADB must be locked, and no other locks held.
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halleycleanup_entries(dns_adb_t *adb, int bucket, isc_stdtime_t now)
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley DP(CLEAN_LEVEL, "cleaning entry bucket %d", bucket);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * Call our cleanup routines.
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley cleanup_entries(adb, adb->next_cleanbucket, now);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * Set the next bucket to be cleaned.
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * Reset the timer.
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley result = isc_timer_reset(adb->timer, isc_timertype_once, NULL,
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * The timer is already dead, from the task's shutdown callback.
ce3761f64d3d734cc94605026985898900ecc474Bob Halley isc_mutexblock_destroy(adb->entrylocks, NBUCKETS);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_mutexblock_destroy(adb->namelocks, NBUCKETS);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_mem_put(adb->mctx, adb, sizeof (dns_adb_t));
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * Public functions.
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrencedns_adb_create(isc_mem_t *mem, dns_view_t *view, isc_timermgr_t *timermgr,
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 * Initialize things here that cannot fail, and especially things
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley * that must be NULL for the error return to work properly.
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley ISC_EVENT_INIT(&adb->cevent, sizeof adb->cevent, 0, NULL,
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley * Initialize the bucket locks for names and elements.
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley * May as well initialize the list heads, too.
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley result = isc_mutexblock_init(adb->namelocks, NBUCKETS);
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley for (i = 0 ; i < NBUCKETS ; i++) {
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley for (i = 0 ; i < NBUCKETS ; i++) {
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley result = isc_mutexblock_init(adb->entrylocks, NBUCKETS);
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley * Memory pools
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley#define MPINIT(t, p, l, n) do { \
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley result = isc_mempool_create(mem, sizeof (t), &(p)); \
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff MPINIT(dns_adbname_t, adb->nmp, ISC_TRUE, "adbname");
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley MPINIT(dns_adbnamehook_t, adb->nhmp, ISC_TRUE, "adbnamehook");
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley MPINIT(dns_adbzoneinfo_t, adb->zimp, ISC_TRUE, "adbzoneinfo");
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley MPINIT(dns_adbentry_t, adb->emp, ISC_TRUE, "adbentry");
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley MPINIT(dns_adbfind_t, adb->ahmp, ISC_TRUE, "adbfind");
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley MPINIT(dns_adbaddrinfo_t, adb->aimp, ISC_TRUE, "adbaddrinfo");
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley MPINIT(dns_adbfetch_t, adb->afmp, ISC_TRUE, "adbfetch");
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley MPINIT(dns_adbfetch6_t, adb->af6mp, ISC_TRUE, "adbfetch6");
8326257468615966b10820260beb3ee96eee94b5Bob Halley * Allocate a timer and a task for our periodic cleanup.
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley result = isc_task_create(adb->taskmgr, adb->mctx, 0, &adb->task);
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley * XXXMLG When this is changed to be a config file option,
8326257468615966b10820260beb3ee96eee94b5Bob Halley isc_interval_set(&adb->tick_interval, CLEAN_SECONDS, 0);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff result = isc_timer_create(adb->timermgr, isc_timertype_once,
8326257468615966b10820260beb3ee96eee94b5Bob Halley * Normal return.
8326257468615966b10820260beb3ee96eee94b5Bob Halley /* clean up entrylocks */
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff isc_mutexblock_destroy(adb->entrylocks, NBUCKETS);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_mutexblock_destroy(adb->namelocks, NBUCKETS);
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrewsdns_adb_whenshutdown(dns_adb_t *adb, isc_task_t *task, isc_event_t **eventp)
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * Send '*eventp' to 'task' when 'adb' has shutdown.
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews * We're already shutdown. Send the event.
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson ISC_LIST_APPEND(adb->whenshutdown, event, link);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence * Shutdown 'adb'.
3f8be559f0871022c78a229bad0eb09560b90909Evan Huntdns_adb_createfind(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt unsigned int options, isc_stdtime_t now, dns_name_t *target,
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt isc_boolean_t use_hints, want_event, start_at_root, alias;
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt REQUIRE(target == NULL || dns_name_hasbuffer(target));
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt REQUIRE((options & DNS_ADBFIND_ADDRESSMASK) != 0);
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt wanted_addresses = (options & DNS_ADBFIND_ADDRESSMASK);
44de0b1f7d9997aaf6092589c4c7da4a1df908dbTatuya JINMEI 神明達哉 * XXXMLG Move this comment somewhere else!
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * Look up the name in our internal database.
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * Possibilities: Note that these are not always exclusive.
44de0b1f7d9997aaf6092589c4c7da4a1df908dbTatuya JINMEI 神明達哉 * No name found. In this case, allocate a new name header and
44de0b1f7d9997aaf6092589c4c7da4a1df908dbTatuya JINMEI 神明達哉 * an initial namehook or two. If any of these allocations
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * fail, clean up and return ISC_R_NOMEMORY.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Name found, valid addresses present. Allocate one addrinfo
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * structure for each found and append it to the linked list
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * of addresses for this header.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Name found, queries pending. In this case, if a task was
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * passed in, allocate a job id, attach it to the name's job
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * list and remember to tell the caller that there will be
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * more info coming later.
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * Remember what types of addresses we are interested in.
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington * Try to see if we know anything about this name at all.
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington adbname = find_name_and_lock(adb, name, &bucket);
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington "dns_adb_createfind: returning ISC_R_SHUTTINGDOWN");
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington * Nothing found. Allocate a new adbname structure for this name.
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington * Expire old entries, etc.
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington use_hints = dns_name_equal(zone, dns_rootname);
b7dca533e041adcacafb9369892b19db9231937bBrian Wellington * Do we know that the name is an alias?
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington if (!EXPIRE_OK(adbname->expire_target, now)) {
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington * Yes, it is.
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington "dns_adb_createfind: name %p is an alias (cached)",
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews * Try to populate the name from the database and/or
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews * start fetches.
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews if (!NAME_HAS_V4(adbname) && !NAME_FETCH_V4(adbname)
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews result = dbfind_name(adbname, now, use_hints, dns_rdatatype_a);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews "dns_adb_createfind: Found A for name %p in db",
goto v6;
adbname);
goto post_copy;
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 name_bucket;
return (ISC_R_NOTFOUND);
if (want_check_exit) {
return (DNS_R_SUCCESS);
if (now == 0)
goto out;
goto out;
goto out;
goto out;
return (ISC_R_SUCCESS);
out:
if (free_name)
if (free_entry)
if (free_namehook)
return (result);
int bucket;
int bucket;
int unlock_bucket;
goto cleanup;
const char *tmpp;
for (i = 0 ; i < NBUCKETS ; i++)
for (i = 0 ; i < NBUCKETS ; i++)
for (i = 0 ; i < NBUCKETS ; i++) {
for (i = 0 ; i < NBUCKETS ; i++) {
case AF_INET:
case AF_INET6:
for (i = 0 ; i < NBUCKETS ; i++)
for (i = 0 ; i < NBUCKETS ; i++)
const char *tmpp;
case AF_INET:
case AF_INET6:
isc_buffer_t b;
isc_region_t r;
isc_buffer_used(&b, &r);
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 DNS_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);
static isc_result_t
switch (result) {
case DNS_R_GLUE:
case DNS_R_HINT:
case DNS_R_SUCCESS:
case DNS_R_NXDOMAIN:
case DNS_R_NXRRSET:
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;
(void)task;
address_type = 0;
if (want_check_exit) {
goto out;
name);
goto check_result;
goto out;
out:
int bucket;
(void)task;
if (want_check_exit) {
name);
goto out;
name);
goto out;
goto out;
name);
goto out;
goto out;
out:
static isc_result_t
if (start_at_root) {
adbname);
goto cleanup;
goto cleanup;
return (result);
static isc_result_t
goto cleanup;
goto cleanup;
return (result);
static isc_result_t
if (start_at_root) {
adbname);
goto cleanup;
if (use_hints)
goto cleanup;
return (result);
int bucket;
return (ISC_R_NOMEMORY);
return (ISC_R_SUCCESS);
int goodness_adjustment)
int bucket;
if (goodness_adjustment == 0)
if (goodness_adjustment > 0) {
int bucket;
unsigned int new_srtt;
int bucket;
int bucket;
goto unlock;
goto unlock;
return (result);
int bucket;
if (want_check_exit) {