adb.c revision da318480f3c527e48647f4531225b259dffc8cb8
5fa46bc91672ef5737aee6f99763161511566c24Tinderbox 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
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * 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.
28a8f5b0de57d269cf2845c69cb6abe18cbd3b3aMark Andrews/* $Id: adb.c,v 1.206 2003/10/10 00:13:50 marka Exp $ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence * Implementation notes
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley * --------------------
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * In finds, if task == NULL, no events will be generated, and no events
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * 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.
fca5f81ad69098ea8abba130c7f841c951ef91c2Bob Halley * After we have cleaned all buckets, dump the database contents.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/string.h> /* Required for HP/UX (and others?) */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADB_MAGIC ISC_MAGIC('D', 'a', 'd', 'b')
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBNAME_MAGIC ISC_MAGIC('a', 'd', 'b', 'N')
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBNAMEHOOK_MAGIC ISC_MAGIC('a', 'd', 'N', 'H')
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBZONEINFO_MAGIC ISC_MAGIC('a', 'd', 'b', 'Z')
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBZONEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBZONEINFO_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBENTRY_MAGIC ISC_MAGIC('a', 'd', 'b', 'E')
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBFETCH_MAGIC ISC_MAGIC('a', 'd', 'F', '4')
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBFETCH_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBFETCH6_MAGIC ISC_MAGIC('a', 'd', 'F', '6')
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#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/AAAA records
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * if the zone has extremely low TTLs.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define ADB_CACHE_MAXIMUM 86400 /* seconds (86400 = 24 hours) */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Wake up every CLEAN_SECONDS and clean CLEAN_BUCKETS buckets, so that all
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * buckets are cleaned in CLEAN_PERIOD seconds.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define CLEAN_BUCKETS ((NBUCKETS * CLEAN_SECONDS) / CLEAN_PERIOD)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FREE_ITEMS 64 /* free count for memory pools */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FILL_COUNT 16 /* fill count for memory pools */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADB_INVALIDBUCKET (-1) /* invalid bucket address */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADB_MINADBSIZE (1024*1024) /* 1 Megabyte */
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;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley unsigned int magic;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_mutex_t reflock; /* Covers irefcnt, erefcnt */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley unsigned int irefcnt;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein unsigned int erefcnt;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * 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.
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * XXXRTH Have a per-bucket structure that contains all of these?
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater isc_boolean_t entry_sd[NBUCKETS]; /* shutting down */
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * XXXMLG Document these structures.
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley unsigned int chains;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews unsigned int magic;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews * dns_adbnamehook_t
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * This is a small widget that dangles off a dns_adbname_t. It contains a
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * pointer to the address information about this host, and a link to the next
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * namehook that will contain the next address this host has.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int magic;
da527e4ff6a013364826637963e7ac372e024f33David Lawrence * dns_adbzoneinfo_t
da527e4ff6a013364826637963e7ac372e024f33David Lawrence * This is a small widget that holds zone-specific information about an
da527e4ff6a013364826637963e7ac372e024f33David Lawrence * address. Currently limited to lameness, but could just as easily be
da527e4ff6a013364826637963e7ac372e024f33David Lawrence * extended to other types of information about zones.
da527e4ff6a013364826637963e7ac372e024f33David Lawrence unsigned int magic;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * An address entry. It holds quite a bit of information about addresses,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * including edns state (in "flags"), rtt, and of course the address of
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley unsigned int magic;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int refcnt;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley unsigned int flags;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley unsigned int srtt;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * A nonzero 'expires' field indicates that the entry should
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence * persist until that time. This allows entries found
5fc1b54cc6134bd70f4e22df90a2e5631aaea77aBob Halley * using dns_adb_findaddrinfo() to persist for a limited time
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * even though they are not necessarily associated with a
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * Internal functions (and prototypes).
ce3761f64d3d734cc94605026985898900ecc474Bob Halleystatic inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
ce3761f64d3d734cc94605026985898900ecc474Bob Halleystatic inline void free_adbname(dns_adb_t *, dns_adbname_t **);
ce3761f64d3d734cc94605026985898900ecc474Bob Halleystatic inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
ce3761f64d3d734cc94605026985898900ecc474Bob Halleystatic inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
ce3761f64d3d734cc94605026985898900ecc474Bob Halleystatic inline dns_adbzoneinfo_t *new_adbzoneinfo(dns_adb_t *, dns_name_t *);
ce3761f64d3d734cc94605026985898900ecc474Bob Halleystatic inline void free_adbzoneinfo(dns_adb_t *, dns_adbzoneinfo_t **);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halleystatic inline dns_adbentry_t *new_adbentry(dns_adb_t *);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencestatic inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleystatic inline dns_adbfind_t *new_adbfind(dns_adb_t *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline isc_boolean_t free_adbfind(dns_adb_t *, dns_adbfind_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *, dns_adbentry_t *,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbfetch_t *new_adbfetch(dns_adb_t *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void free_adbfetch(dns_adb_t *, dns_adbfetch_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley unsigned int, int *);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halleystatic inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void dump_adb(dns_adb_t *, FILE *, isc_boolean_t debug);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void print_namehook_list(FILE *, const char *legend,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void print_find_list(FILE *, dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void print_fetch_list(FILE *, dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline isc_boolean_t dec_adb_irefcnt(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void inc_adb_irefcnt(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void inc_adb_erefcnt(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline isc_boolean_t dec_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void violate_locking_hierarchy(isc_mutex_t *, isc_mutex_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_boolean_t 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 isc_boolean_t check_expire_namehooks(dns_adbname_t *, isc_stdtime_t,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark 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(dns_adbname_t *, isc_boolean_t,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void timer_cleanup(isc_task_t *, isc_event_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_boolean_t shutdown_names(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_boolean_t shutdown_entries(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void link_name(dns_adb_t *, int, dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline isc_boolean_t unlink_name(dns_adb_t *, dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void link_entry(dns_adb_t *, int, dns_adbentry_t *);
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updaterstatic inline isc_boolean_t unlink_entry(dns_adb_t *, dns_adbentry_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_boolean_t kill_name(dns_adbname_t **, isc_eventtype_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * MUST NOT overlap DNS_ADBFIND_* flags!
86548554f6180bbe051c8cd8f03c93fc9b6a7825Mark 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_STARTATZONE DNS_ADBFIND_STARTATZONE
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#define NAME_GLUEOK(n) (((n)->flags & NAME_GLUE_OK) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_HINTOK(n) (((n)->flags & NAME_HINT_OK) != 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 a 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))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * Fetches are broken out into A and AAAA 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 * Note: since we have removed the support of A6 in adb, FETCH_A and FETCH_AAAA
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater * are now equal to FETCH_V4 and FETCH_V6, respectively.
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_FETCH_AAAA(n) ((n)->fetch_aaaa != NULL)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_FETCH(n) (NAME_FETCH_V4(n) || NAME_FETCH_V6(n))
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_STARTATZONE(fn) (((fn)->options & DNS_ADBFIND_STARTATZONE) \
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_HINTOK(fn) (((fn)->options & DNS_ADBFIND_HINTOK) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_GLUEOK(fn) (((fn)->options & DNS_ADBFIND_GLUEOK) != 0)
86548554f6180bbe051c8cd8f03c93fc9b6a7825Mark Andrews#define FIND_HAS_ADDRS(fn) (!ISC_LIST_EMPTY((fn)->list))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_RETURNLAME(fn) (((fn)->options & DNS_ADBFIND_RETURNLAME) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * These are currently used on simple unsigned ints, so they are
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * not really associated with any particular type.
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define WANT_INET(x) (((x) & DNS_ADBFIND_INET) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define WANT_INET6(x) (((x) & DNS_ADBFIND_INET6) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define EXPIRE_OK(exp, now) ((exp == INT_MAX) || (exp < now))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * Find out if the flags on a name (nf) indicate if it is a hint or
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * glue, and compare this to the appropriate bits set in o, to see if
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * this is ok.
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define GLUE_OK(nf, o) (!NAME_GLUEOK(nf) || (((o) & DNS_ADBFIND_GLUEOK) != 0))
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define HINT_OK(nf, o) (!NAME_HINTOK(nf) || (((o) & DNS_ADBFIND_HINTOK) != 0))
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define GLUEHINT_OK(nf, o) (GLUE_OK(nf, o) || HINT_OK(nf, o))
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define STARTATZONE_MATCHES(nf, o) (((nf)->flags & NAME_STARTATZONE) == \
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define NCACHE_RESULT(r) ((r) == DNS_R_NCACHENXDOMAIN || \
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define NXDOMAIN_RESULT(r) ((r) == DNS_R_NXDOMAIN || \
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define NXRRSET_RESULT(r) ((r) == DNS_R_NCACHENXRRSET || \
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews * Error state rankings.
508f61f8d699c46f962b682f388e54b446a7194dMark Andrewsstatic const char *errnames[] = {
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews "unexpected",
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews#define NEWERR(old, new) (ISC_MIN((old), (new)))
508f61f8d699c46f962b682f388e54b446a7194dMark Andrewsstatic isc_result_t find_err_map[FIND_ERR_MAX] = {
f0bbac2c0f1afa74b88cab902daf11202ebe7cbdBob HalleyDP(int level, const char *format, ...) ISC_FORMAT_PRINTF(2, 3);
1366b7833c86343de278480b9abd71754e418bfaBob Halley * Requires the adbname bucket be locked and that no entry buckets be locked.
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley * This code handles A and AAAA rdatasets only.
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleyimport_rdataset(dns_adbname_t *adbname, dns_rdataset_t *rdataset,
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington dns_adbentry_t *foundentry; /* NO CLEAN UP! */
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews INSIST((rdtype == dns_rdatatype_a) || (rdtype == dns_rdatatype_aaaa));
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley DP(NCACHE_LEVEL, "expire_v4 set to MIN(%u,%u) import_rdataset",
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) import_rdataset",
611dc8876869036ab5e981e53ae7a446145d9354Mark Andrews adbname->expire_v6 = ISC_MIN(adbname->expire_v6,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Lie a little here. This is more or less so code that cares
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * can find out if any new information was added or not.
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews * Requires the name's bucket be locked.
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewskill_name(dns_adbname_t **n, isc_eventtype_t ev) {
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews * If we're dead already, just check to see if we should go
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews * 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);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * 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.
29b487b0a458d655f0aad9257ca46021f4903d08Bob 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 * Check to see if we need to remove the v6 addresses
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley * Check to see if we need to remove the alias target.
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley if (expire || EXPIRE_OK(name->expire_target, now)) {
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews * Requires the name's bucket be locked.
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrewsstatic inline void
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrewslink_name(dns_adb_t *adb, int bucket, dns_adbname_t *name) {
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews INSIST(name->lock_bucket == DNS_ADB_INVALIDBUCKET);
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews ISC_LIST_PREPEND(adb->names[bucket], name, plink);
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews * Requires the name's bucket be locked.
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrewsunlink_name(dns_adb_t *adb, dns_adbname_t *name) {
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews ISC_LIST_UNLINK(adb->names[bucket], name, plink);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews * Requires the entry's bucket be locked.
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrewsstatic inline void
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrewslink_entry(dns_adb_t *adb, int bucket, dns_adbentry_t *entry) {
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews * Requires the entry's bucket be locked.
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrewsunlink_entry(dns_adb_t *adb, dns_adbentry_t *entry) {
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewsstatic inline void
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewsviolate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want) {
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson * The ADB _MUST_ be locked before calling. Also, exit conditions must be
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson * checked after calling this function.
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews for (bucket = 0; bucket < NBUCKETS; bucket++) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * This bucket has no names. We must decrement the
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * irefcnt ourselves, since it will not be
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * automatically triggered by a name being unlinked.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Run through the list. For each name, clean up finds
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * found there, and cancel any fetches running. When
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 * all the fetches are canceled, the name will destroy
a8e4c27d2c00e831d1eb7260e3f455d97907d799Bob Halley * The ADB _MUST_ be locked before calling. Also, exit conditions must be
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington * checked after calling this function.
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington entry = ISC_LIST_HEAD(adb->entries[bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * This bucket has no entries. We must decrement the
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * irefcnt ourselves, since it will not be
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * automatically triggered by an entry being unlinked.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Run through the list. Cleanup any entries not
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * associated with names, and which are not in use.
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley * Name bucket must be locked
3740b569ae76295b941d57a724a43beb75b533baBob Halley dns_resolver_cancelfetch(name->fetch_aaaa->fetch);
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson * 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 result = dec_entry_refcnt(adb, entry, ISC_FALSE);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley * Free the namehook
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewsclean_target(dns_adb_t *adb, dns_name_t *target) {
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halleyset_target(dns_adb_t *adb, dns_name_t *name, dns_name_t *fname,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Copy the CNAME's target into the target name.
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley result = dns_rdata_tostruct(&rdata, &cname, NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dns_name_dup(&cname.cname, adb->mctx, target);
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews namereln = dns_name_fullcompare(name, fname, &order,
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer * Get the target name of the DNAME.
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews result = dns_rdata_tostruct(&rdata, &dname, NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Construct the new target name.
f257e9369c27578eb87077923dc010a6614e2a7aMark Andrews result = dns_name_split(name, nlabels, nbits, prefix, NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dns_name_concatenate(prefix, &dname.dname, new_target,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dns_name_dup(new_target, adb->mctx, target);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Assumes nothing is locked, since this is called by the client.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Assumes the name bucket is locked.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyclean_finds_at_name(dns_adbname_t *name, isc_eventtype_t evtype,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int addrs)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews "ENTER clean_finds_at_name, name %p, evtype %08x, addrs %08x",
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBMOREADDRESSES");
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley if ((notify) != 0) {
82a30bf0a0d1a8120e8c021966a5275eacb9ed35David Lawrence DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBNOMOREADDRESSES");
82a30bf0a0d1a8120e8c021966a5275eacb9ed35David Lawrence wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Unlink the find from the name, letting the caller
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * call dns_adb_destroyfind() on it to clean it up
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley find->result_v6 = find_err_map[name->fetch6_err];
cee7525336d4710a64368875d92eb439d4d3efb1Mark Andrews "sending event %p to task %p for find %p",
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_task_sendanddetach(&task, (isc_event_t **)&ev);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(ENTER_LEVEL, "EXIT clean_finds_at_name, name %p", name);
0ad5cb4782cd419b089bcab28d2fd9e140dbcc59Mark Andrewsstatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * The caller must be holding the adb lock.
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley * If there aren't any external references either, we're
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley * done. Send the control event to initiate shutdown.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_LIST_UNLINK(adb->whenshutdown, event, ev_link);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleyinc_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleydec_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley (adb->entry_sd[bucket] || entry->expires == 0)) {
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewsnew_adbname(dns_adb_t *adb, dns_name_t *dnsname) {
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley if (dns_name_dup(dnsname, adb->mctx, &name->name) != ISC_R_SUCCESS) {
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrewsstatic inline void
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark 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);
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrewsnew_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry) {
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrewsstatic inline void
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrewsfree_adbnamehook(dns_adb_t *adb, dns_adbnamehook_t **namehook) {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews INSIST(namehook != NULL && DNS_ADBNAMEHOOK_VALID(*namehook));
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Huntnew_adbzoneinfo(dns_adb_t *adb, dns_name_t *zone) {
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt if (dns_name_dup(zone, adb->mctx, &zi->zone) != ISC_R_SUCCESS) {
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrewsstatic inline void
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrewsfree_adbzoneinfo(dns_adb_t *adb, dns_adbzoneinfo_t **zoneinfo) {
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews 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));
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence INSIST(e->lock_bucket == DNS_ADB_INVALIDBUCKET);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley * Public members.
cedd0ab1e812ec7cf05d57c3e602db41b79f0a2aAndreas Gustafsson * private members
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff "isc_mutex_init failed in new_adbfind()");
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_EVENT_INIT(&h->event, sizeof(isc_event_t), 0, 0, 0, NULL, NULL,
b9a2c5ddf99250e851f45a606cdbe3e3fc4beccaBob Halleystatic inline dns_adbfetch_t *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyfree_adbfetch(dns_adb_t *adb, dns_adbfetch_t **fetch) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff INSIST(fetch != NULL && DNS_ADBFETCH_VALID(*fetch));
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtonfree_adbfind(dns_adb_t *adb, dns_adbfind_t **findp) {
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington INSIST(findp != NULL && DNS_ADBFIND_VALID(*findp));
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington INSIST(find->name_bucket == DNS_ADB_INVALIDBUCKET);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * Copy bits from the entry into the newly allocated addrinfo. The entry
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * must be locked, and the reference count must be bumped up by one
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * if this function returns a valid pointer.
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtonnew_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry, in_port_t port) {
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtonstatic inline void
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtonfree_adbaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **ainfo) {
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington INSIST(ainfo != NULL && DNS_ADBADDRINFO_VALID(*ainfo));
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * Search for the name. NOTE: The bucket is kept locked on both
45e22378fc8fc87fb96391ffd23d4402fe003dc0Brian Wellington * success and failure, so it must always be unlocked by the caller!
45e22378fc8fc87fb96391ffd23d4402fe003dc0Brian Wellington * On the first call to this function, *bucketp must be set to
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * DNS_ADB_INVALIDBUCKET.
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtonfind_name_and_lock(dns_adb_t *adb, dns_name_t *name,
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington bucket = dns_fullname_hash(name, ISC_FALSE) % NBUCKETS;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington adbname = ISC_LIST_HEAD(adb->names[bucket]);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * Search for the address. NOTE: The bucket is kept locked on both
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * success and failure, so it must always be unlocked by the caller.
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * On the first call to this function, *bucketp must be set to
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * DNS_ADB_INVALIDBUCKET. This will cause a lock to occur. On
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * later calls (within the same "lock path") it can be left alone, so
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * if this function is called multiple times locking is only done if
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * the bucket changes.
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halleyfind_entry_and_lock(dns_adb_t *adb, isc_sockaddr_t *addr, int *bucketp) {
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley bucket = isc_sockaddr_hash(addr, ISC_TRUE) % NBUCKETS;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley * Entry bucket MUST be locked!
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halleyentry_is_bad_for_zone(dns_adb_t *adb, dns_adbentry_t *entry, dns_name_t *zone,
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington * Has the entry expired?
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington ISC_LIST_UNLINK(entry->zoneinfo, zi, plink);
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington * Order tests from least to most expensive.
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halleycopy_namehook_lists(dns_adb_t *adb, dns_adbfind_t *find, dns_name_t *zone,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley && entry_is_bad_for_zone(adb, entry, zone, now)) {
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley addrinfo = new_adbaddrinfo(adb, entry, find->port);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews * Found a valid entry. Add it to the find's list.
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews ISC_LIST_APPEND(find->list, addrinfo, publink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (entry_is_bad_for_zone(adb, entry, zone, now))
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley addrinfo = new_adbaddrinfo(adb, entry, find->port);
514aeac2acbbe2b77ff3c4e310617523cf5651c5Mark Andrews * Found a valid entry. Add it to the find's list.
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrewsshutdown_task(isc_task_t *task, isc_event_t *ev) {
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley * Kill the timer, and then the ADB itself. Note that this implies
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley * 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()
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley * this) badness will occur.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * 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));
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson if (!EXPIRE_OK(name->expire_target, now))
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * The name is empty. Delete it.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Our caller, or one of its callers, will be calling check_exit() at
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * some point, so we don't need to do it here.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Entry bucket must be locked; adb may be locked; no other locks held.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleycheck_expire_entry(dns_adb_t *adb, dns_adbentry_t **entryp, isc_stdtime_t now)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(entryp != NULL && DNS_ADBENTRY_VALID(*entryp));
ecb6c5782ea248307e86c4bceac6c371d27576a6David Lawrence if (entry->expires == 0 || (! expire && entry->expires > now))
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley * The entry is not in use. Delete it.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * ADB must be locked, and no other locks held.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleycleanup_names(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley DP(CLEAN_LEVEL, "cleaning name bucket %d", bucket);
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews result = check_expire_namehooks(name, now, adb->overmem);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff * ADB must be locked, and no other locks held.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleycleanup_entries(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley DP(CLEAN_LEVEL, "cleaning entry bucket %d", bucket);
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson next_entry = ISC_LIST_NEXT(entry, plink);
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson result = check_expire_entry(adb, &entry, now);
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafssontimer_cleanup(isc_task_t *task, isc_event_t *ev) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley unsigned int i;
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley for (i = 0; i < CLEAN_BUCKETS; i++) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Call our cleanup routines.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley RUNTIME_CHECK(cleanup_names(adb, adb->next_cleanbucket, now) ==
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley RUNTIME_CHECK(cleanup_entries(adb, adb->next_cleanbucket, now)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Set the next bucket to be cleaned.
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley * Reset the timer.
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley * XXXDCL isc_timer_reset might return ISC_R_UNEXPECTED or
0c264c909424f855e7e3b7ce7bb21f650609e768Brian Wellington * ISC_R_NOMEMORY, but it isn't clear what could be done here
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * if either one of those things happened.
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews (void)isc_timer_reset(adb->timer, isc_timertype_once, NULL,
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews * The timer is already dead, from the task's shutdown callback.
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
1c33761bcff783815a952e6e1fb9d5e07a3e1363Brian Wellington * Public functions.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleydns_adb_create(isc_mem_t *mem, dns_view_t *view, isc_timermgr_t *timermgr,
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * Initialize things here that cannot fail, and especially things
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * that must be NULL for the error return to work properly.
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley ISC_EVENT_INIT(&adb->cevent, sizeof(adb->cevent), 0, NULL,
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * Initialize the bucket locks for names and elements.
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer * May as well initialize the list heads, too.
425028583845247a2e09532a88e58165e82924f4Brian Wellington result = isc_mutexblock_init(adb->namelocks, NBUCKETS);
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley for (i = 0; i < NBUCKETS; i++) {
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley for (i = 0; i < NBUCKETS; i++) {
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley result = isc_mutexblock_init(adb->entrylocks, NBUCKETS);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * Memory pools
ce3761f64d3d734cc94605026985898900ecc474Bob Halley#define MPINIT(t, p, n) do { \
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley result = isc_mempool_create(mem, sizeof(t), &(p)); \
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews MPINIT(dns_adbnamehook_t, adb->nhmp, "adbnamehook");
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews MPINIT(dns_adbzoneinfo_t, adb->zimp, "adbzoneinfo");
ce3761f64d3d734cc94605026985898900ecc474Bob Halley MPINIT(dns_adbaddrinfo_t, adb->aimp, "adbaddrinfo");
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * Allocate a timer and a task for our periodic cleanup.
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 result = isc_task_create(adb->taskmgr, 0, &adb->task);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * XXXMLG When this is changed to be a config file option,
72ce8db9183a39f393dbc6ff15259dbf7ea55275David Lawrence isc_interval_set(&adb->tick_interval, CLEAN_SECONDS, 0);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews result = isc_timer_create(adb->timermgr, isc_timertype_once,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence DP(ISC_LOG_DEBUG(5), "cleaning interval for adb: "
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence "%u buckets every %u seconds, %u buckets in system, %u cl.interval",
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence CLEAN_BUCKETS, CLEAN_SECONDS, NBUCKETS, CLEAN_PERIOD);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * Normal return.
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff /* clean up entrylocks */
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencedns_adb_attach(dns_adb_t *adb, dns_adb_t **adbx) {
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley need_exit_check = ISC_TF(adb->erefcnt == 0 && adb->irefcnt == 0);
63c8c8f2a1c1e490305fde095321798f0342739dBob Halleydns_adb_whenshutdown(dns_adb_t *adb, isc_task_t *task, isc_event_t **eventp) {
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley * Send '*eventp' to 'task' when 'adb' has shutdown.
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley * We're already shutdown. Send the event.
8326257468615966b10820260beb3ee96eee94b5Bob Halley ISC_LIST_APPEND(adb->whenshutdown, event, ev_link);
8326257468615966b10820260beb3ee96eee94b5Bob Halley * Shutdown 'adb'.
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrewsdns_adb_createfind(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews unsigned int options, isc_stdtime_t now, dns_name_t *target,
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews isc_boolean_t want_event, start_at_zone, alias, have_address;
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley REQUIRE(target == NULL || dns_name_hasbuffer(target));
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley REQUIRE((options & DNS_ADBFIND_ADDRESSMASK) != 0);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley wanted_addresses = (options & DNS_ADBFIND_ADDRESSMASK);
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews * XXXMLG Move this comment somewhere else!
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * Look up the name in our internal database.
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews * Possibilities: Note that these are not always exclusive.
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews * No name found. In this case, allocate a new name header and
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews * an initial namehook or two. If any of these allocations
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * fail, clean up and return ISC_R_NOMEMORY.
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * Name found, valid addresses present. Allocate one addrinfo
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * structure for each found and append it to the linked list
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * of addresses for this header.
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * Name found, queries pending. In this case, if a task was
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * passed in, allocate a job id, attach it to the name's job
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * list and remember to tell the caller that there will be
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * more info coming later.
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * Remember what types of addresses we are interested in.
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews * Try to see if we know anything about this name at all.
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews adbname = find_name_and_lock(adb, name, find->options, &bucket);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence "dns_adb_createfind: returning ISC_R_SHUTTINGDOWN");
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson INSIST(free_adbfind(adb, &find) == ISC_FALSE);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence * Nothing found. Allocate a new adbname structure for this name.
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence INSIST(free_adbfind(adb, &find) == ISC_FALSE);
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * Expire old entries, etc.
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt INSIST(check_expire_namehooks(adbname, now, adb->overmem) == ISC_FALSE);
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * Do we know that the name is an alias?
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt "dns_adb_createfind: name %p is an alias (cached)",
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * Try to populate the name from the database and/or
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * start fetches. First try looking for an A record
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * in the database.
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt if (!NAME_HAS_V4(adbname) && EXPIRE_OK(adbname->expire_v4, now)
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt result = dbfind_name(adbname, now, dns_rdatatype_a);
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt "dns_adb_createfind: found A for name %p in db",
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Did we get a CNAME or DNAME?
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews "dns_adb_createfind: name %p is an alias",
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * If the name doesn't exist at all, don't bother with
44de0b1f7d9997aaf6092589c4c7da4a1df908dbTatuya JINMEI 神明達哉 * v6 queries; they won't work.
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * If the name does exist but we didn't get our data, go
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * ahead and try AAAA.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * If the result is neither of these, try a fetch for A.
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt if (!NAME_HAS_V6(adbname) && EXPIRE_OK(adbname->expire_v6, now)
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt result = dbfind_name(adbname, now, dns_rdatatype_aaaa);
b7dca533e041adcacafb9369892b19db9231937bBrian Wellington "dns_adb_createfind: found AAAA for name %p",
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington * Did we get a CNAME or DNAME?
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington "dns_adb_createfind: name %p is an alias",
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington * Listen to negative cache hints, and don't start
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington * another query.
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington if (NCACHE_RESULT(result) || AUTH_NX(result))
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington if ((WANT_INET(wanted_addresses) && NAME_HAS_V4(adbname)) ||
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington (WANT_INET6(wanted_addresses) && NAME_HAS_V6(adbname)))
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington ! (FIND_AVOIDFETCHES(find) && have_address)) {
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington * We're missing at least one address family. Either the
b7dca533e041adcacafb9369892b19db9231937bBrian Wellington * caller hasn't instructed us to avoid fetches, or we don't
b7dca533e041adcacafb9369892b19db9231937bBrian Wellington * know anything about any of the address families that would
b7dca533e041adcacafb9369892b19db9231937bBrian Wellington * be acceptable so we have to launch fetches.
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews "dns_adb_createfind: started A fetch for name %p",
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews "dns_adb_createfind: "
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;
== ISC_FALSE);
if (overmem) {