adb.c revision 3f6a66689410910ef601a4d26f10a24f331ef83c
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson/*
7d32c065c7bb56f281651ae3dd2888f32ce4f1d9Bob Halley * Copyright (C) 1999 Internet Software Consortium.
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson *
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * Permission to use, copy, modify, and distribute this software for any
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * purpose with or without fee is hereby granted, provided that the above
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * copyright notice and this permission notice appear in all copies.
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson *
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * SOFTWARE.
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson/*
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * Implementation notes
364a82f7c25b62967678027043425201a5e5171aBob Halley * --------------------
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson *
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * In handles, if task == NULL, no events will be generated, and no events
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * have been sent. If task != NULL but taskaction == NULL, an event has been
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * posted but not yet freed. If neigher are NULL, no event was posted.
6fcfd0c35d3fd6aea3d36ad002b68e59ac62fdc7Brian Wellington *
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#include <config.h>
bb56f556f520621bcab33688c31d655953774adcAndreas Gustafsson
bb56f556f520621bcab33688c31d655953774adcAndreas Gustafsson#include <stdio.h>
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#include <string.h>
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#include <isc/assertions.h>
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#include <isc/condition.h>
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#include <isc/magic.h>
a672bfbee46c261729811405685db409bd7c9285Andreas Gustafsson#include <isc/mutex.h>
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#include <isc/mutexblock.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/event.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/address.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/events.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/name.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/rdata.h>
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#include <dns/rdataset.h>
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#include <dns/view.h>
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#include "../isc/util.h"
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define DNS_ADB_MAGIC 0x44616462 /* Dadb. */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
2e91b1f3d417cd65e7ec9db1097dfa12e72d8e47Andreas Gustafsson#define DNS_ADBNAME_MAGIC 0x6164624e /* adbN. */
a672bfbee46c261729811405685db409bd7c9285Andreas Gustafsson#define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
a672bfbee46c261729811405685db409bd7c9285Andreas Gustafsson#define DNS_ADBNAMEHOOK_MAGIC 0x61644e48 /* adNH. */
a672bfbee46c261729811405685db409bd7c9285Andreas Gustafsson#define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
a672bfbee46c261729811405685db409bd7c9285Andreas Gustafsson#define DNS_ADBZONEINFO_MAGIC 0x6164625a /* adbZ. */
3eef7eaba00e9bd468d8036c709a296a0e5b76f1Andreas Gustafsson#define DNS_ADBZONEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBZONEINFO_MAGIC)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define DNS_ADBENTRY_MAGIC 0x61646245 /* adbE. */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson/*
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff * Lengths of lists needs to be powers of two.
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define DNS_ADBNAMELIST_LENGTH 32 /* how many buckets for names */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define DNS_ADBENTRYLIST_LENGTH 32 /* how many buckets for addresses */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define FREE_ITEMS 16 /* free count for memory pools */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define FILL_COUNT 8 /* fill count for memory pools */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define DNS_ADB_INVALIDBUCKET (-1) /* invalid bucket address */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafssontypedef ISC_LIST(dns_adbname_t) dns_adbnamelist_t;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafssontypedef struct dns_adbnamehook dns_adbnamehook_t;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafssontypedef struct dns_adbzoneinfo dns_adbzoneinfo_t;
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafssontypedef ISC_LIST(dns_adbentry_t) dns_adbentrylist_t;
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafssonstruct dns_adb {
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson unsigned int magic;
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson isc_mutex_t lock;
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson isc_condition_t shutdown_cond;
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson isc_mem_t *mctx;
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson dns_view_t *view;
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson unsigned int irefcnt;
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson unsigned int erefcnt;
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson isc_mutex_t mplock;
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson isc_mempool_t *nmp; /* dns_adbname_t */
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson isc_mempool_t *nhmp; /* dns_adbnamehook_t */
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson isc_mempool_t *zimp; /* dns_adbzoneinfo_t */
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson isc_mempool_t *emp; /* dns_adbentry_t */
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson isc_mempool_t *ahmp; /* dns_adbhandle_t */
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff isc_mempool_t *aimp; /* dns_adbaddrinfo_t */
a672bfbee46c261729811405685db409bd7c9285Andreas Gustafsson
a672bfbee46c261729811405685db409bd7c9285Andreas Gustafsson /*
a672bfbee46c261729811405685db409bd7c9285Andreas Gustafsson * Bucketized locks and lists for names.
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson dns_adbnamelist_t names[DNS_ADBNAMELIST_LENGTH];
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson isc_mutex_t namelocks[DNS_ADBNAMELIST_LENGTH];
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson isc_boolean_t name_sd[DNS_ADBNAMELIST_LENGTH];
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafsson unsigned int name_refcnt[DNS_ADBNAMELIST_LENGTH];
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafsson /*
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * Bucketized locks for entries.
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson dns_adbentrylist_t entries[DNS_ADBENTRYLIST_LENGTH];
e5a213d7ee3258524936f96191250c1edea2d6baDavid Lawrence isc_mutex_t entrylocks[DNS_ADBENTRYLIST_LENGTH];
a7e803adb0894257fbc0fc4560ff5d915c2b6194Andreas Gustafsson};
6fcfd0c35d3fd6aea3d36ad002b68e59ac62fdc7Brian Wellington
e5a213d7ee3258524936f96191250c1edea2d6baDavid Lawrencestruct dns_adbname {
c718c8aa60e77ed39da5a16d24798e46cfc74f8dAndreas Gustafsson unsigned int magic;
c718c8aa60e77ed39da5a16d24798e46cfc74f8dAndreas Gustafsson dns_name_t name;
a7e803adb0894257fbc0fc4560ff5d915c2b6194Andreas Gustafsson isc_boolean_t partial_results;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson isc_stdtime_t expire_time;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson ISC_LIST(dns_adbnamehook_t) namehooks;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson ISC_LIST(dns_adbnamehook_t) in_progress;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson ISC_LIST(dns_adbhandle_t) handles;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson ISC_LINK(dns_adbname_t) link;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff};
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson/*
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * dns_adbnamehook_t
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson *
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * This is a small widget that dangles off a dns_adbname_t. It contains a
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * pointer to the address information about this host, and a link to the next
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * namehook that will contain the next address this host has.
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafssonstruct dns_adbnamehook {
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson unsigned int magic;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff dns_adbentry_t *entry;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson ISC_LINK(dns_adbnamehook_t) link;
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafsson};
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson/*
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * dns_adbzoneinfo_t
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson *
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * This is a small widget that holds zone-specific information about an
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafsson * address. Currently limited to lameness, but could just as easily be
a672bfbee46c261729811405685db409bd7c9285Andreas Gustafsson * extended to other types of information about zones.
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson */
a672bfbee46c261729811405685db409bd7c9285Andreas Gustafssonstruct dns_adbzoneinfo {
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson unsigned int magic;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff dns_name_t zone;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson isc_stdtime_t lame_timer;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
a672bfbee46c261729811405685db409bd7c9285Andreas Gustafsson ISC_LINK(dns_adbzoneinfo_t) link;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson};
a672bfbee46c261729811405685db409bd7c9285Andreas Gustafsson
c47baa985c31bbcf396b52958654164afd1091c6James Brister/*
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * An address entry. It holds quite a bit of information about addresses,
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * including edns state, rtt, and of course the address of the host.
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafssonstruct dns_adbentry {
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson unsigned int magic;
a672bfbee46c261729811405685db409bd7c9285Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson int lock_bucket;
a672bfbee46c261729811405685db409bd7c9285Andreas Gustafsson unsigned int refcnt;
c47baa985c31bbcf396b52958654164afd1091c6James Brister
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson unsigned int flags;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson int goodness; /* bad <= 0 < good */
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff unsigned int srtt;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson isc_sockaddr_t sockaddr;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson ISC_LIST(dns_adbzoneinfo_t) zoneinfo;
ade06bd257e7c888588a89ddd8247c66cbf6c9a5Andreas Gustafsson ISC_LINK(dns_adbentry_t) link;
ade06bd257e7c888588a89ddd8247c66cbf6c9a5Andreas Gustafsson};
ade06bd257e7c888588a89ddd8247c66cbf6c9a5Andreas Gustafsson
ade06bd257e7c888588a89ddd8247c66cbf6c9a5Andreas Gustafsson/*
ade06bd257e7c888588a89ddd8247c66cbf6c9a5Andreas Gustafsson * Internal functions (and prototypes).
ade06bd257e7c888588a89ddd8247c66cbf6c9a5Andreas Gustafsson */
ade06bd257e7c888588a89ddd8247c66cbf6c9a5Andreas Gustafssonstatic inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
ade06bd257e7c888588a89ddd8247c66cbf6c9a5Andreas Gustafssonstatic inline void free_adbname(dns_adb_t *, dns_adbname_t **);
ade06bd257e7c888588a89ddd8247c66cbf6c9a5Andreas Gustafssonstatic inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson dns_adbentry_t *);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafssonstatic inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
ade06bd257e7c888588a89ddd8247c66cbf6c9a5Andreas Gustafssonstatic inline dns_adbzoneinfo_t *new_adbzoneinfo(dns_adb_t *, dns_name_t *);
ade06bd257e7c888588a89ddd8247c66cbf6c9a5Andreas Gustafssonstatic inline void free_adbzoneinfo(dns_adb_t *, dns_adbzoneinfo_t **);
ade06bd257e7c888588a89ddd8247c66cbf6c9a5Andreas Gustafssonstatic inline dns_adbentry_t *new_adbentry(dns_adb_t *);
ade06bd257e7c888588a89ddd8247c66cbf6c9a5Andreas Gustafssonstatic inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
ade06bd257e7c888588a89ddd8247c66cbf6c9a5Andreas Gustafssonstatic inline dns_adbhandle_t *new_adbhandle(dns_adb_t *);
ade06bd257e7c888588a89ddd8247c66cbf6c9a5Andreas Gustafssonstatic inline void free_adbhandle(dns_adb_t *, dns_adbhandle_t **);
ade06bd257e7c888588a89ddd8247c66cbf6c9a5Andreas Gustafssonstatic inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *,
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson dns_adbentry_t *);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrewsstatic inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrews int *);
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrewsstatic inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrews isc_sockaddr_t *, int *);
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrewsstatic void print_dns_name(FILE *, dns_name_t *);
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrewsstatic void print_namehook_list(FILE *, dns_adbname_t *);
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrewsstatic void print_handle_list(FILE *, dns_adbname_t *);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafssonstatic inline void inc_adb_irefcnt(dns_adb_t *, isc_boolean_t);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafssonstatic inline void dec_adb_irefcnt(dns_adb_t *, isc_boolean_t);
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafssonstatic inline void inc_adb_erefcnt(dns_adb_t *, isc_boolean_t);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencestatic inline void dec_adb_erefcnt(dns_adb_t *, isc_boolean_t);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencestatic inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson isc_boolean_t);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencestatic inline void dec_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence isc_boolean_t);
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafssonstatic inline void violate_locking_hierarchy(isc_mutex_t *, isc_mutex_t *);
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafssonstatic void clean_namehooks_at_name(dns_adb_t *, dns_adbname_t *);
22eeba5ec6b621eb81aec084a1a4e0776aee0f41Andreas Gustafssonstatic void clean_handles_at_name(dns_adbname_t *, isc_eventtype_t);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafssonstatic isc_result_t construct_name(dns_adb_t *, dns_adbhandle_t *,
22eeba5ec6b621eb81aec084a1a4e0776aee0f41Andreas Gustafsson dns_name_t *, dns_adbname_t *, int);
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencestatic inline void
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrenceviolate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want)
b78f21115c0ee4ae539a3d0d24e065f982a15b19Andreas Gustafsson{
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence UNLOCK(have);
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson LOCK(want);
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson LOCK(have);
22eeba5ec6b621eb81aec084a1a4e0776aee0f41Andreas Gustafsson }
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson}
6fcfd0c35d3fd6aea3d36ad002b68e59ac62fdc7Brian Wellington
6fcfd0c35d3fd6aea3d36ad002b68e59ac62fdc7Brian Wellington/*
6fcfd0c35d3fd6aea3d36ad002b68e59ac62fdc7Brian Wellington * The ADB _MUST_ be locked before calling.
6fcfd0c35d3fd6aea3d36ad002b68e59ac62fdc7Brian Wellington */
6fcfd0c35d3fd6aea3d36ad002b68e59ac62fdc7Brian Wellingtonstatic void
6fcfd0c35d3fd6aea3d36ad002b68e59ac62fdc7Brian Wellingtonshutdown_names(dns_adb_t *adb, isc_boolean_t kill_fetches)
6fcfd0c35d3fd6aea3d36ad002b68e59ac62fdc7Brian Wellington{
6fcfd0c35d3fd6aea3d36ad002b68e59ac62fdc7Brian Wellington int bucket;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson dns_adbname_t *name;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson (void)kill_fetches; /* XXX need to implement */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson for (bucket = 0 ; bucket < DNS_ADBNAMELIST_LENGTH ; bucket++) {
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence LOCK(&adb->namelocks[bucket]);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson adb->name_sd[bucket] = ISC_TRUE;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson /*
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * Run through the list. For each name, clean up handles
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * found there, and cancel any fetches running. When
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * all the fetches are canceled, the name will destroy
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * itself.
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson name = ISC_LIST_HEAD(adb->names[bucket]);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson while (name != NULL) {
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson clean_handles_at_name(name, DNS_EVENT_ADBSHUTDOWN);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson clean_namehooks_at_name(adb, name);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson /* XXX TODO: cancel fetches */
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson /* if no fetches, kill! */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson ISC_LIST_UNLINK(adb->names[bucket], name, link);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson free_adbname(adb, &name);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson INSIST(adb->name_refcnt[bucket] > 0);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson adb->name_refcnt[bucket]--;
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafsson
a672bfbee46c261729811405685db409bd7c9285Andreas Gustafsson name = ISC_LIST_HEAD(adb->names[bucket]);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson }
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (adb->name_refcnt[bucket] == 0)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson dec_adb_irefcnt(adb, ISC_FALSE);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff UNLOCK(&adb->namelocks[bucket]);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson }
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson}
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrewsstatic void
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrewsclean_namehooks_at_name(dns_adb_t *adb, dns_adbname_t *name)
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrews{
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrews dns_adbentry_t *entry;
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrews dns_adbnamehook_t *namehook;
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrews int addr_bucket;
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrews
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson addr_bucket = DNS_ADB_INVALIDBUCKET;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson namehook = ISC_LIST_HEAD(name->namehooks);
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson while (namehook != NULL) {
bb56f556f520621bcab33688c31d655953774adcAndreas Gustafsson INSIST(DNS_ADBNAMEHOOK_VALID(namehook));
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson /*
bb56f556f520621bcab33688c31d655953774adcAndreas Gustafsson * Clean up the entry if needed.
bb56f556f520621bcab33688c31d655953774adcAndreas Gustafsson */
bb56f556f520621bcab33688c31d655953774adcAndreas Gustafsson entry = namehook->entry;
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson if (entry != NULL) {
bb56f556f520621bcab33688c31d655953774adcAndreas Gustafsson INSIST(DNS_ADBENTRY_VALID(entry));
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson if (addr_bucket != entry->lock_bucket) {
bb56f556f520621bcab33688c31d655953774adcAndreas Gustafsson if (addr_bucket != DNS_ADB_INVALIDBUCKET)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson UNLOCK(&adb->entrylocks[addr_bucket]);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson addr_bucket = entry->lock_bucket;
cb284e7bd479a4309bfa9ccfb139a07ceb3edfd6Andreas Gustafsson LOCK(&adb->entrylocks[addr_bucket]);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence }
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
cb284e7bd479a4309bfa9ccfb139a07ceb3edfd6Andreas Gustafsson dec_entry_refcnt(adb, entry, ISC_FALSE);
c718c8aa60e77ed39da5a16d24798e46cfc74f8dAndreas Gustafsson }
cb284e7bd479a4309bfa9ccfb139a07ceb3edfd6Andreas Gustafsson
cb284e7bd479a4309bfa9ccfb139a07ceb3edfd6Andreas Gustafsson /*
c718c8aa60e77ed39da5a16d24798e46cfc74f8dAndreas Gustafsson * Free the namehook
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson */
a7e803adb0894257fbc0fc4560ff5d915c2b6194Andreas Gustafsson namehook->entry = NULL;
cb284e7bd479a4309bfa9ccfb139a07ceb3edfd6Andreas Gustafsson ISC_LIST_UNLINK(name->namehooks, namehook, link);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence free_adbnamehook(adb, &namehook);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
cb284e7bd479a4309bfa9ccfb139a07ceb3edfd6Andreas Gustafsson namehook = ISC_LIST_HEAD(name->namehooks);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence }
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
cb284e7bd479a4309bfa9ccfb139a07ceb3edfd6Andreas Gustafsson if (addr_bucket != DNS_ADB_INVALIDBUCKET)
cb284e7bd479a4309bfa9ccfb139a07ceb3edfd6Andreas Gustafsson UNLOCK(&adb->entrylocks[addr_bucket]);
a7e803adb0894257fbc0fc4560ff5d915c2b6194Andreas Gustafsson}
a7e803adb0894257fbc0fc4560ff5d915c2b6194Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafssonstatic void
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafssonclean_handles_at_name(dns_adbname_t *name, isc_eventtype_t evtype)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence{
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence isc_event_t *ev;
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson isc_task_t *task;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence dns_adbhandle_t *handle;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson handle = ISC_LIST_HEAD(name->handles);
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson while (handle != NULL) {
22eeba5ec6b621eb81aec084a1a4e0776aee0f41Andreas Gustafsson LOCK(&handle->lock);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
22eeba5ec6b621eb81aec084a1a4e0776aee0f41Andreas Gustafsson ev = &handle->event;
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson task = ev->sender;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ev->sender = handle;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ev->type = evtype;
b78f21115c0ee4ae539a3d0d24e065f982a15b19Andreas Gustafsson isc_task_sendanddetach(&task, &ev);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence /*
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson * Unlink the handle from the name, letting the caller
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson * call dns_adb_done() on it to clean it up later.
22eeba5ec6b621eb81aec084a1a4e0776aee0f41Andreas Gustafsson */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson ISC_LIST_UNLINK(name->handles, handle, link);
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrews handle->adbname = NULL;
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrews handle->name_bucket = DNS_ADB_INVALIDBUCKET;
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrews
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrews UNLOCK(&handle->lock);
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrews
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrews handle = ISC_LIST_HEAD(name->handles);
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrews }
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrews}
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrews
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrewsstatic inline void
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrewsinc_adb_irefcnt(dns_adb_t *adb, isc_boolean_t lock)
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrews{
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrews if (lock)
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrews LOCK(&adb->lock);
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrews
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrews adb->irefcnt++;
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrews
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrews if (lock)
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrews UNLOCK(&adb->lock);
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrews}
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrews
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrewsstatic inline void
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrewsdec_adb_irefcnt(dns_adb_t *adb, isc_boolean_t lock)
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrews{
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrews if (lock)
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrews LOCK(&adb->lock);
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrews
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrews INSIST(adb->irefcnt > 0);
3e8a8028a47e2a6a1303fb8c3483e781ac940478Mark Andrews adb->irefcnt--;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson if (adb->irefcnt == 0 && adb->erefcnt)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson SIGNAL(&adb->shutdown_cond);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson if (lock)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson UNLOCK(&adb->lock);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson}
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graffstatic inline void
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafssoninc_adb_erefcnt(dns_adb_t *adb, isc_boolean_t lock)
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafsson{
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson if (lock)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson LOCK(&adb->lock);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson adb->erefcnt++;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafsson if (lock)
a672bfbee46c261729811405685db409bd7c9285Andreas Gustafsson UNLOCK(&adb->lock);
a672bfbee46c261729811405685db409bd7c9285Andreas Gustafsson}
a672bfbee46c261729811405685db409bd7c9285Andreas Gustafsson
c47baa985c31bbcf396b52958654164afd1091c6James Bristerstatic inline void
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafssondec_adb_erefcnt(dns_adb_t *adb, isc_boolean_t lock)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson{
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (lock)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson LOCK(&adb->lock);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson INSIST(adb->erefcnt > 0);
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrews adb->erefcnt--;
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrews
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrews if (adb->irefcnt == 0 && adb->erefcnt)
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrews SIGNAL(&adb->shutdown_cond);
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrews
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrews if (lock)
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrews UNLOCK(&adb->lock);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson}
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafssonstatic inline void
bb56f556f520621bcab33688c31d655953774adcAndreas Gustafssoninc_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock)
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson{
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson int bucket;
bb56f556f520621bcab33688c31d655953774adcAndreas Gustafsson
bb56f556f520621bcab33688c31d655953774adcAndreas Gustafsson bucket = entry->lock_bucket;
bb56f556f520621bcab33688c31d655953774adcAndreas Gustafsson
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson if (lock)
bb56f556f520621bcab33688c31d655953774adcAndreas Gustafsson LOCK(&adb->entrylocks[bucket]);
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson
e82d19b0c77c8e408b3c1fb2e34488d4993f4b21Andreas Gustafsson entry->refcnt++;
bb56f556f520621bcab33688c31d655953774adcAndreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson if (lock)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson UNLOCK(&adb->entrylocks[bucket]);
cb284e7bd479a4309bfa9ccfb139a07ceb3edfd6Andreas Gustafsson}
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencestatic inline void
cb284e7bd479a4309bfa9ccfb139a07ceb3edfd6Andreas Gustafssondec_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock)
c718c8aa60e77ed39da5a16d24798e46cfc74f8dAndreas Gustafsson{
cb284e7bd479a4309bfa9ccfb139a07ceb3edfd6Andreas Gustafsson int bucket;
cb284e7bd479a4309bfa9ccfb139a07ceb3edfd6Andreas Gustafsson isc_boolean_t destroy_entry;
c718c8aa60e77ed39da5a16d24798e46cfc74f8dAndreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson bucket = entry->lock_bucket;
a7e803adb0894257fbc0fc4560ff5d915c2b6194Andreas Gustafsson
cb284e7bd479a4309bfa9ccfb139a07ceb3edfd6Andreas Gustafsson if (lock)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence LOCK(&adb->entrylocks[bucket]);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
cb284e7bd479a4309bfa9ccfb139a07ceb3edfd6Andreas Gustafsson INSIST(entry->refcnt > 0);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence entry->refcnt--;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
cb284e7bd479a4309bfa9ccfb139a07ceb3edfd6Andreas Gustafsson destroy_entry = ISC_FALSE;
cb284e7bd479a4309bfa9ccfb139a07ceb3edfd6Andreas Gustafsson if (entry->refcnt == 0) {
a7e803adb0894257fbc0fc4560ff5d915c2b6194Andreas Gustafsson destroy_entry = ISC_TRUE;
a7e803adb0894257fbc0fc4560ff5d915c2b6194Andreas Gustafsson ISC_LIST_UNLINK(adb->entries[bucket], entry, link);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson }
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson if (lock)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson UNLOCK(&adb->entrylocks[bucket]);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson if (!destroy_entry)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafsson entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson free_adbentry(adb, &entry);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson}
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafssonstatic inline dns_adbname_t *
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafssonnew_adbname(dns_adb_t *adb, dns_name_t *dnsname)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson{
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson dns_adbname_t *name;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson name = isc_mempool_get(adb->nmp);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (name == NULL)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson return (NULL);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson dns_name_init(&name->name, NULL);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (dns_name_dup(dnsname, adb->mctx, &name->name) != ISC_R_SUCCESS) {
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson isc_mempool_put(adb->nmp, name);
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson return (NULL);
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson }
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson name->magic = DNS_ADBNAME_MAGIC;
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson name->partial_results = ISC_FALSE;
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson name->expire_time = 0;
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson ISC_LIST_INIT(name->namehooks);
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson ISC_LIST_INIT(name->in_progress);
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson ISC_LIST_INIT(name->handles);
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson ISC_LINK_INIT(name, link);
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson return (name);
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson}
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafssonstatic inline void
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafssonfree_adbname(dns_adb_t *adb, dns_adbname_t **name)
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson{
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafsson dns_adbname_t *n;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafsson INSIST(name != NULL && DNS_ADBNAME_VALID(*name));
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafsson n = *name;
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafsson *name = NULL;
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafsson
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff INSIST(ISC_LIST_EMPTY(n->namehooks));
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafsson INSIST(ISC_LIST_EMPTY(n->in_progress));
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafsson INSIST(ISC_LIST_EMPTY(n->handles));
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafsson INSIST(!ISC_LINK_LINKED(n, link));
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafsson
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafsson n->magic = 0;
94a3e829e0b9e25660ec136742ad5c3694067c70Andreas Gustafsson dns_name_free(&n->name, adb->mctx);
isc_mempool_put(adb->nmp, n);
}
static inline dns_adbnamehook_t *
new_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry)
{
dns_adbnamehook_t *nh;
nh = isc_mempool_get(adb->nhmp);
if (nh == NULL)
return (NULL);
nh->magic = DNS_ADBNAMEHOOK_MAGIC;
nh->entry = entry;
ISC_LINK_INIT(nh, link);
return (nh);
}
static inline void
free_adbnamehook(dns_adb_t *adb, dns_adbnamehook_t **namehook)
{
dns_adbnamehook_t *nh;
INSIST(namehook != NULL && DNS_ADBNAMEHOOK_VALID(*namehook));
nh = *namehook;
*namehook = NULL;
INSIST(nh->entry == NULL);
INSIST(!ISC_LINK_LINKED(nh, link));
nh->magic = 0;
isc_mempool_put(adb->nhmp, nh);
}
static inline dns_adbzoneinfo_t *
new_adbzoneinfo(dns_adb_t *adb, dns_name_t *zone)
{
dns_adbzoneinfo_t *zi;
zi = isc_mempool_get(adb->zimp);
if (zi == NULL)
return (NULL);
dns_name_init(&zi->zone, NULL);
if (dns_name_dup(zone, adb->mctx, &zi->zone) != ISC_R_SUCCESS) {
isc_mempool_put(adb->zimp, zi);
return (NULL);
}
zi->magic = DNS_ADBZONEINFO_MAGIC;
zi->lame_timer = 0;
ISC_LINK_INIT(zi, link);
return (zi);
}
static inline void
free_adbzoneinfo(dns_adb_t *adb, dns_adbzoneinfo_t **zoneinfo)
{
dns_adbzoneinfo_t *zi;
INSIST(zoneinfo != NULL && DNS_ADBZONEINFO_VALID(*zoneinfo));
zi = *zoneinfo;
*zoneinfo = NULL;
INSIST(!ISC_LINK_LINKED(zi, link));
dns_name_free(&zi->zone, adb->mctx);
zi->magic = 0;
isc_mempool_put(adb->zimp, zi);
}
static inline dns_adbentry_t *
new_adbentry(dns_adb_t *adb)
{
dns_adbentry_t *e;
e = isc_mempool_get(adb->emp);
if (e == NULL)
return (NULL);
e->magic = DNS_ADBENTRY_MAGIC;
e->lock_bucket = DNS_ADB_INVALIDBUCKET;
e->refcnt = 0;
e->flags = 0;
e->goodness = 0;
e->srtt = 0;
ISC_LIST_INIT(e->zoneinfo);
ISC_LINK_INIT(e, link);
return (e);
}
static inline void
free_adbentry(dns_adb_t *adb, dns_adbentry_t **entry)
{
dns_adbentry_t *e;
dns_adbzoneinfo_t *zi;
INSIST(entry != NULL && DNS_ADBENTRY_VALID(*entry));
e = *entry;
*entry = NULL;
INSIST(e->lock_bucket == DNS_ADB_INVALIDBUCKET);
INSIST(e->refcnt == 0);
INSIST(ISC_LIST_EMPTY(e->zoneinfo));
INSIST(!ISC_LINK_LINKED(e, link));
e->magic = 0;
zi = ISC_LIST_HEAD(e->zoneinfo);
while (zi != NULL) {
ISC_LIST_UNLINK(e->zoneinfo, zi, link);
free_adbzoneinfo(adb, &zi);
zi = ISC_LIST_HEAD(e->zoneinfo);
}
isc_mempool_put(adb->emp, e);
}
static inline dns_adbhandle_t *
new_adbhandle(dns_adb_t *adb)
{
dns_adbhandle_t *h;
isc_result_t result;
h = isc_mempool_get(adb->ahmp);
if (h == NULL)
return (NULL);
/*
* public members
*/
h->magic = 0;
h->query_pending = ISC_FALSE;
h->result = ISC_R_UNEXPECTED;
ISC_LIST_INIT(h->list);
ISC_LINK_INIT(h, next);
h->name_bucket = DNS_ADB_INVALIDBUCKET;
h->adbname = NULL;
/*
* private members
*/
result = isc_mutex_init(&h->lock);
if (result != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_mutex_init failed in new_adbhandle()");
isc_mempool_put(adb->ahmp, h);
return (NULL);
}
ISC_LINK_INIT(h, link);
ISC_EVENT_INIT(&h->event, sizeof (isc_event_t), 0, 0, 0, NULL, NULL,
NULL, NULL, h);
h->magic = DNS_ADBHANDLE_MAGIC;
return (h);
}
static inline void
free_adbhandle(dns_adb_t *adb, dns_adbhandle_t **handlep)
{
dns_adbhandle_t *handle;
INSIST(handlep != NULL && DNS_ADBHANDLE_VALID(*handlep));
handle = *handlep;
*handlep = NULL;
INSIST(ISC_LIST_EMPTY(handle->list));
INSIST(!ISC_LINK_LINKED(handle, next));
INSIST(!ISC_LINK_LINKED(handle, link));
INSIST(handle->name_bucket == DNS_ADB_INVALIDBUCKET);
INSIST(handle->adbname == NULL);
handle->magic = 0;
isc_mutex_destroy(&handle->lock);
isc_mempool_put(adb->ahmp, handle);
}
/*
* Copy bits from the entry into the newly allocated addrinfo. The entry
* must be locked, and the reference count must be bumped up by one
* if this function returns a valid pointer.
*/
static inline dns_adbaddrinfo_t *
new_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry)
{
dns_adbaddrinfo_t *ai;
ai = isc_mempool_get(adb->aimp);
if (ai == NULL)
return (NULL);
ai->magic = DNS_ADBADDRINFO_MAGIC;
ai->sockaddr = &entry->sockaddr;
ai->goodness = entry->goodness;
ai->srtt = entry->srtt;
ai->flags = entry->flags;
ai->entry = entry;
ISC_LINK_INIT(ai, link);
return (ai);
}
static inline void
free_adbaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **ainfo)
{
dns_adbaddrinfo_t *ai;
INSIST(ainfo != NULL && DNS_ADBADDRINFO_VALID(*ainfo));
ai = *ainfo;
*ainfo = NULL;
INSIST(ai->sockaddr == NULL);
INSIST(ai->entry == NULL);
INSIST(!ISC_LINK_LINKED(ai, link));
ai->magic = 0;
isc_mempool_put(adb->aimp, ai);
}
/*
* Search for the name. NOTE: The bucket is kept locked on both
* success and failure, so it must always be unlocked by the caller!
*
* On the first call to this function, *bucketp must be set to
* DNS_ADB_INVALIDBUCKET.
*/
static inline dns_adbname_t *
find_name_and_lock(dns_adb_t *adb, dns_name_t *name, int *bucketp)
{
dns_adbname_t *adbname;
int bucket;
bucket = dns_name_hash(name, ISC_FALSE);
bucket &= (DNS_ADBNAMELIST_LENGTH - 1);
if (*bucketp == DNS_ADB_INVALIDBUCKET) {
LOCK(&adb->namelocks[bucket]);
*bucketp = bucket;
} else if (*bucketp != bucket) {
UNLOCK(&adb->namelocks[*bucketp]);
LOCK(&adb->namelocks[bucket]);
*bucketp = bucket;
}
adbname = ISC_LIST_HEAD(adb->names[bucket]);
while (adbname != NULL) {
if (dns_name_equal(name, &adbname->name))
return (adbname);
adbname = ISC_LIST_NEXT(adbname, link);
}
return (NULL);
}
/*
* Search for the address. NOTE: The bucket is kept locked on both
* success and failure, so it must always be unlocked by the caller.
*
* On the first call to this function, *bucketp must be set to
* DNS_ADB_INVALIDBUCKET. This will cause a lock to occur. On
* later calls (within the same "lock path") it can be left alone, so
* if this function is called multiple times locking is only done if
* the bucket changes.
*/
static inline dns_adbentry_t *
find_entry_and_lock(dns_adb_t *adb, isc_sockaddr_t *addr, int *bucketp)
{
dns_adbentry_t *entry;
int bucket;
bucket = isc_sockaddr_hash(addr, ISC_TRUE);
bucket &= (DNS_ADBENTRYLIST_LENGTH - 1);
if (*bucketp == DNS_ADB_INVALIDBUCKET) {
LOCK(&adb->entrylocks[bucket]);
*bucketp = bucket;
} else if (*bucketp != bucket) {
UNLOCK(&adb->entrylocks[*bucketp]);
LOCK(&adb->entrylocks[bucket]);
*bucketp = bucket;
}
entry = ISC_LIST_HEAD(adb->entries[bucket]);
while (entry != NULL) {
if (isc_sockaddr_equal(addr, &entry->sockaddr))
return (entry);
entry = ISC_LIST_NEXT(entry, link);
}
return (NULL);
}
/*
* Entry bucket MUST be locked!
*/
static isc_boolean_t
entry_is_bad_for_zone(dns_adb_t *adb, dns_adbentry_t *entry, dns_name_t *zone)
{
dns_adbzoneinfo_t *zi, *next_zi;
isc_stdtime_t now;
isc_boolean_t is_bad;
if (isc_stdtime_get(&now) != ISC_R_SUCCESS)
return (ISC_FALSE); /* XXXMLG: assume ok if this fails? */
is_bad = ISC_FALSE;
zi = ISC_LIST_HEAD(entry->zoneinfo);
if (zi == NULL)
return (ISC_FALSE);
while (zi != NULL) {
next_zi = ISC_LIST_NEXT(zi, link);
/*
* Has the entry expired?
*/
if (zi->lame_timer < now) {
ISC_LIST_UNLINK(entry->zoneinfo, zi, link);
free_adbzoneinfo(adb, &zi);
}
if (zi != NULL && !is_bad)
if (dns_name_equal(zone, &zi->zone))
is_bad = ISC_TRUE;
zi = next_zi;
}
return (is_bad);
}
static void
copy_namehook_list(dns_adb_t *adb, dns_adbhandle_t *handle,
dns_adbname_t *name, dns_name_t *zone)
{
dns_adbnamehook_t *namehook;
dns_adbaddrinfo_t *addrinfo;
int bucket;
handle->query_pending = ISC_FALSE;
handle->result = ISC_R_UNEXPECTED;
bucket = DNS_ADB_INVALIDBUCKET;
namehook = ISC_LIST_HEAD(name->namehooks);
while (namehook != NULL) {
bucket = namehook->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
if (entry_is_bad_for_zone(adb, namehook->entry, zone))
goto next;
addrinfo = new_adbaddrinfo(adb, namehook->entry);
if (addrinfo == NULL) {
handle->result = ISC_R_NOMEMORY;
goto out;
}
/*
* Found a valid entry. Add it to the handle's list.
*/
inc_entry_refcnt(adb, namehook->entry, ISC_FALSE);
ISC_LIST_APPEND(handle->list, addrinfo, link);
addrinfo = NULL;
next:
UNLOCK(&adb->entrylocks[bucket]);
bucket = DNS_ADB_INVALIDBUCKET;
namehook = ISC_LIST_NEXT(namehook, link);
}
handle->result = ISC_R_SUCCESS; /* all were copied */
out:
if (bucket != DNS_ADB_INVALIDBUCKET)
UNLOCK(&adb->entrylocks[bucket]);
}
static void
destroy(dns_adb_t *adb)
{
adb->magic = 0;
isc_mempool_destroy(&adb->nmp);
isc_mempool_destroy(&adb->nhmp);
isc_mempool_destroy(&adb->zimp);
isc_mempool_destroy(&adb->emp);
isc_mempool_destroy(&adb->ahmp);
isc_mempool_destroy(&adb->aimp);
isc_mutexblock_destroy(adb->entrylocks, DNS_ADBENTRYLIST_LENGTH);
isc_mutexblock_destroy(adb->namelocks, DNS_ADBNAMELIST_LENGTH);
isc_mutex_destroy(&adb->lock);
isc_mutex_destroy(&adb->mplock);
isc_mem_put(adb->mctx, adb, sizeof (dns_adb_t));
}
/*
* Public functions.
*/
isc_result_t
dns_adb_create(isc_mem_t *mem, dns_view_t *view, dns_adb_t **newadb)
{
dns_adb_t *adb;
isc_result_t result;
int i;
REQUIRE(mem != NULL);
REQUIRE(newadb != NULL && *newadb == NULL);
adb = isc_mem_get(mem, sizeof (dns_adb_t));
if (adb == NULL)
return (ISC_R_NOMEMORY);
/*
* Initialize things here that cannot fail, and especially things
* that must be NULL for the error return to work properly.
*/
adb->magic = 0;
adb->erefcnt = 1;
adb->irefcnt = 0;
adb->nmp = NULL;
adb->nhmp = NULL;
adb->zimp = NULL;
adb->emp = NULL;
adb->ahmp = NULL;
adb->aimp = NULL;
result = isc_mutex_init(&adb->lock);
if (result != ISC_R_SUCCESS)
goto fail0a;
result = isc_mutex_init(&adb->mplock);
if (result != ISC_R_SUCCESS)
goto fail0b;
result = isc_condition_init(&adb->shutdown_cond);
if (result != ISC_R_SUCCESS)
goto fail0c;
/*
* Initialize the bucket locks for names and elements.
* May as well initialize the list heads, too.
*/
result = isc_mutexblock_init(adb->namelocks, DNS_ADBNAMELIST_LENGTH);
if (result != ISC_R_SUCCESS)
goto fail1;
for (i = 0 ; i < DNS_ADBNAMELIST_LENGTH ; i++) {
ISC_LIST_INIT(adb->names[i]);
adb->name_sd[i] = ISC_FALSE;
adb->name_refcnt[i] = 0;
adb->irefcnt++;
}
for (i = 0 ; i < DNS_ADBENTRYLIST_LENGTH ; i++)
ISC_LIST_INIT(adb->entries[i]);
result = isc_mutexblock_init(adb->entrylocks, DNS_ADBENTRYLIST_LENGTH);
if (result != ISC_R_SUCCESS)
goto fail2;
/*
* Memory pools
*/
#define MPINIT(t, p, l, n) do { \
result = isc_mempool_create(mem, sizeof (t), &(p)); \
if (result != ISC_R_SUCCESS) \
goto fail3; \
isc_mempool_setfreemax((p), FREE_ITEMS); \
isc_mempool_setfillcount((p), FILL_COUNT); \
isc_mempool_setname((p), n); \
if (l) \
isc_mempool_associatelock((p), &adb->mplock); \
} while (0)
MPINIT(dns_adbname_t, adb->nmp, ISC_TRUE, "adbname");
MPINIT(dns_adbnamehook_t, adb->nhmp, ISC_TRUE, "adbnamehook");
MPINIT(dns_adbzoneinfo_t, adb->zimp, ISC_TRUE, "adbzoneinfo");
MPINIT(dns_adbentry_t, adb->emp, ISC_TRUE, "adbentry");
MPINIT(dns_adbhandle_t, adb->ahmp, ISC_TRUE, "adbhandle");
MPINIT(dns_adbaddrinfo_t, adb->aimp, ISC_TRUE, "adbaddrinfo");
#undef MPINIT
/*
* Normal return.
*/
adb->mctx = mem;
adb->view = view;
adb->magic = DNS_ADB_MAGIC;
*newadb = adb;
return (ISC_R_SUCCESS);
fail3: /* clean up entrylocks */
isc_mutexblock_destroy(adb->entrylocks, DNS_ADBENTRYLIST_LENGTH);
fail2: /* clean up namelocks */
isc_mutexblock_destroy(adb->namelocks, DNS_ADBNAMELIST_LENGTH);
fail1: /* clean up only allocated memory */
if (adb->nmp != NULL)
isc_mempool_destroy(&adb->nmp);
if (adb->nhmp != NULL)
isc_mempool_destroy(&adb->nhmp);
if (adb->zimp != NULL)
isc_mempool_destroy(&adb->zimp);
if (adb->emp != NULL)
isc_mempool_destroy(&adb->emp);
if (adb->ahmp != NULL)
isc_mempool_destroy(&adb->ahmp);
if (adb->aimp != NULL)
isc_mempool_destroy(&adb->aimp);
isc_condition_destroy(&adb->shutdown_cond);
fail0c:
isc_mutex_destroy(&adb->mplock);
fail0b:
isc_mutex_destroy(&adb->lock);
fail0a:
isc_mem_put(mem, adb, sizeof (dns_adb_t));
return (result);
}
void
dns_adb_destroy(dns_adb_t **adbx)
{
dns_adb_t *adb;
isc_boolean_t done;
REQUIRE(adbx != NULL && DNS_ADB_VALID(*adbx));
adb = *adbx;
*adbx = NULL;
/*
* If all lists are empty, destroy the memory used by this
* adb.
*/
LOCK(&adb->lock);
shutdown_names(adb, ISC_TRUE);
dec_adb_erefcnt(adb, ISC_FALSE);
do {
done = ISC_TRUE;
if (adb->erefcnt != 0 || adb->irefcnt != 0)
done = ISC_FALSE;
if (isc_mempool_getallocated(adb->ahmp) > 0)
done = ISC_FALSE;
if (!done)
WAIT(&adb->shutdown_cond, &adb->lock);
} while (!done);
UNLOCK(&adb->lock);
destroy(adb);
}
isc_result_t
dns_adb_lookup(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
void *arg, dns_name_t *name, dns_name_t *zone,
dns_adbhandle_t **handlep)
{
dns_adbhandle_t *handle;
dns_adbname_t *adbname;
int bucket;
isc_result_t result;
isc_boolean_t attach_to_task;
REQUIRE(DNS_ADB_VALID(adb));
if (task != NULL) {
REQUIRE(action != NULL);
}
REQUIRE(name != NULL);
REQUIRE(zone != NULL);
REQUIRE(handlep != NULL && *handlep == NULL);
attach_to_task = ISC_FALSE;
result = ISC_R_UNEXPECTED;
/*
* Look up the name in our internal database.
*
* Possibilities: Note that these are not always exclusive.
*
* No name found. In this case, allocate a new name header and
* an initial namehook or two. If any of these allocations
* fail, clean up and return ISC_R_NOMEMORY.
*
* Name found, valid addresses present. Allocate one addrinfo
* structure for each found and append it to the linked list
* of addresses for this header.
*
* Name found, queries pending. In this case, if a task was
* passed in, allocate a job id, attach it to the name's job
* list and remember to tell the caller that there will be
* more info coming later.
*/
handle = new_adbhandle(adb);
if (handle == NULL) {
result = ISC_R_NOMEMORY;
goto out;
}
/*
* Look things up in our database first.
*/
bucket = DNS_ADB_INVALIDBUCKET;
adbname = find_name_and_lock(adb, name, &bucket);
if (adb->name_sd[bucket]) {
result = ISC_R_SHUTTINGDOWN;
goto fail;
}
/*
* Found! Run through the name and copy out the bits we are
* interested in. If we cannot copy at least one address, return
* ISC_R_NOMEMORY, otherwise copy out what we can and set the
* missing_data bit in the header.
*/
again:
if (adbname != NULL) {
copy_namehook_list(adb, handle, adbname, zone);
if (handle->result == ISC_R_NOMEMORY
&& ISC_LIST_EMPTY(handle->list)) {
result = ISC_R_NOMEMORY;
goto fail;
}
/*
* Attach to the name's query list if there are queries
* already running.
*/
if (!ISC_LIST_EMPTY(adbname->in_progress) && (task != NULL)) {
handle->adbname = adbname;
handle->name_bucket = bucket;
ISC_LIST_APPEND(adbname->handles, handle, link);
attach_to_task = ISC_TRUE;
} else {
/*
* We are done with this name, so release the lock
* here. Then, lock the adb itself, and add the
* handle to the list of handles given out.
*/
UNLOCK(&adb->namelocks[bucket]);
bucket = DNS_ADB_INVALIDBUCKET;
}
result = ISC_R_SUCCESS;
goto out;
}
/*
* Nothing found. Allocate a new adbname structure for this name
* and look in the database for details. If the database has
* nothing useful, start a fetch if we can.
*/
adbname = new_adbname(adb, name);
if (adbname == NULL) {
result = ISC_R_NOMEMORY;
goto fail;
}
/*
* Try to populate the name from the database and/or start fetches.
* If this function returns ISC_R_SUCCESS at least ONE new bit
* of data was added, and/or fetches were started. If nothing new
* can ever be found it will return DNS_R_NOMEMORY more than likely.
*/
result = construct_name(adb, handle, name, adbname, bucket);
if (result == ISC_R_SUCCESS) {
ISC_LIST_PREPEND(adb->names[bucket], adbname, link);
adb->name_refcnt[bucket]++;
goto again;
}
/*
* If anything other than success is returned, free the name
* (since it will have nothing useful in it) and return via the
* failure return.
*/
free_adbname(adb, &adbname);
fail:
free_adbhandle(adb, &handle);
/*
* If the name isn't on a list it means we allocated it here, and it
* should be killed.
*/
if (!ISC_LINK_LINKED(adbname, link))
free_adbname(adb, &adbname);
/*
* "goto out" if the handle will be returned to the caller. This
* is a non-fatal return, since it will give the caller a handle
* at the very least.
*/
out:
if (handle != NULL) {
*handlep = handle;
if (attach_to_task) {
isc_task_t *taskp;
taskp = NULL;
isc_task_attach(task, &taskp);
handle->event.sender = taskp;
handle->event.action = action;
handle->event.arg = arg;
}
}
if (bucket != DNS_ADB_INVALIDBUCKET)
UNLOCK(&adb->namelocks[bucket]);
return (result);
}
isc_result_t
dns_adb_deletename(dns_adb_t *adb, dns_name_t *host)
{
int name_bucket;
dns_adbname_t *name;
isc_boolean_t decr_adbrefcnt;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(host != NULL);
name = NULL;
/*
* Find the name.
*/
name_bucket = DNS_ADB_INVALIDBUCKET;
name = find_name_and_lock(adb, host, &name_bucket);
if (name == NULL) {
UNLOCK(&adb->namelocks[name_bucket]);
return (ISC_R_NOTFOUND);
}
/*
* If any handles are attached to this name, notify them that things
* are going away by canceling their requests.
*/
clean_handles_at_name(name, DNS_EVENT_ADBNAMEDELETED);
/* XXX TODO:
* If fetches are running for this name, cancel them all.
*/
/*
* Loop through the name and kill any namehooks and entries they
* point to.
*/
clean_namehooks_at_name(adb, name);
ISC_LIST_UNLINK(adb->names[name_bucket], name, link);
free_adbname(adb, &name);
INSIST(adb->name_refcnt[name_bucket] > 0);
adb->name_refcnt[name_bucket]--;
decr_adbrefcnt = ISC_FALSE;
if (adb->name_sd[name_bucket] && (adb->name_refcnt[name_bucket] == 0))
decr_adbrefcnt = ISC_TRUE;
if (name_bucket != DNS_ADB_INVALIDBUCKET)
UNLOCK(&adb->namelocks[name_bucket]);
if (decr_adbrefcnt)
dec_adb_irefcnt(adb, ISC_TRUE);
return (DNS_R_SUCCESS);
}
isc_result_t
dns_adb_insert(dns_adb_t *adb, dns_name_t *host, isc_sockaddr_t *addr)
{
dns_adbname_t *name;
isc_boolean_t free_name;
dns_adbentry_t *entry;
isc_boolean_t free_entry;
dns_adbnamehook_t *namehook;
isc_boolean_t free_namehook;
int name_bucket, addr_bucket; /* unlock if != DNS_ADB_INVALIDBUCKET */
isc_result_t result;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(host != NULL);
REQUIRE(addr != NULL);
name = NULL;
free_name = ISC_FALSE;
entry = NULL;
free_entry = ISC_FALSE;
namehook = NULL;
free_namehook = ISC_FALSE;
result = ISC_R_UNEXPECTED;
/*
* First, see if the host is already in the database. If it is,
* don't make a new host entry. If not, copy the name and name's
* contents into our structure and allocate what we'll need
* to attach things together.
*/
name_bucket = DNS_ADB_INVALIDBUCKET;
name = find_name_and_lock(adb, host, &name_bucket);
if (name == NULL) {
name = new_adbname(adb, host);
if (name == NULL) {
result = ISC_R_NOMEMORY;
goto out;
}
free_name = ISC_TRUE;
}
/*
* Now, while keeping the name locked, search for the address.
* Three possibilities: One, the address doesn't exist.
* Two, the address exists, but we aren't linked to it.
* Three, the address exists and we are linked to it.
* (1) causes a new entry and namehook to be created.
* (2) causes only a new namehook.
* (3) is an error.
*/
addr_bucket = DNS_ADB_INVALIDBUCKET;
entry = find_entry_and_lock(adb, addr, &addr_bucket);
/*
* Case (1): new entry and namehook.
*/
if (entry == NULL) {
entry = new_adbentry(adb);
if (entry == NULL) {
result = ISC_R_NOMEMORY;
goto out;
}
free_entry = ISC_TRUE;
}
/*
* Case (3): entry exists, we're linked.
*/
namehook = ISC_LIST_HEAD(name->namehooks);
while (namehook != NULL) {
if (namehook->entry == entry) {
result = ISC_R_EXISTS;
goto out;
}
namehook = ISC_LIST_NEXT(namehook, link);
}
/*
* Case (2): New namehook, link to entry from above.
*/
namehook = new_adbnamehook(adb, entry);
if (namehook == NULL) {
result = ISC_R_NOMEMORY;
goto out;
}
free_namehook = ISC_TRUE;
ISC_LIST_APPEND(name->namehooks, namehook, link);
entry->lock_bucket = addr_bucket;
inc_entry_refcnt(adb, entry, ISC_FALSE);
entry->sockaddr = *addr;
/*
* If needed, string up the name and entry.
*/
if (!ISC_LINK_LINKED(name, link)) {
ISC_LIST_PREPEND(adb->names[name_bucket], name, link);
adb->name_refcnt[name_bucket]++;
}
if (!ISC_LINK_LINKED(entry, link))
ISC_LIST_PREPEND(adb->entries[addr_bucket], entry, link);
UNLOCK(&adb->namelocks[name_bucket]);
name_bucket = DNS_ADB_INVALIDBUCKET;
UNLOCK(&adb->entrylocks[addr_bucket]);
addr_bucket = DNS_ADB_INVALIDBUCKET;
return (ISC_R_SUCCESS);
out:
if (free_name)
free_adbname(adb, &name);
if (free_entry)
isc_mempool_put(adb->emp, entry);
if (free_namehook)
isc_mempool_put(adb->nhmp, namehook);
if (name_bucket != DNS_ADB_INVALIDBUCKET)
UNLOCK(&adb->namelocks[name_bucket]);
if (addr_bucket != DNS_ADB_INVALIDBUCKET)
UNLOCK(&adb->entrylocks[addr_bucket]);
return (result);
}
void
dns_adb_done(dns_adb_t *adb, dns_adbhandle_t **handlep)
{
dns_adbhandle_t *handle;
dns_adbentry_t *entry;
dns_adbaddrinfo_t *ai;
int bucket;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(handlep != NULL && DNS_ADBHANDLE_VALID(*handlep));
handle = *handlep;
*handlep = NULL;
REQUIRE(!ISC_LINK_LINKED(handle, next));
LOCK(&handle->lock);
bucket = handle->name_bucket;
if (bucket == DNS_ADB_INVALIDBUCKET)
goto cleanup;
/*
* We need to get the adbname's lock to unlink the handle.
*/
violate_locking_hierarchy(&handle->lock, &adb->namelocks[bucket]);
bucket = handle->name_bucket;
if (bucket != DNS_ADB_INVALIDBUCKET) {
ISC_LIST_UNLINK(handle->adbname->handles, handle, link);
handle->adbname = NULL;
handle->name_bucket = DNS_ADB_INVALIDBUCKET;
}
UNLOCK(&adb->namelocks[bucket]);
bucket = DNS_ADB_INVALIDBUCKET;
cleanup:
UNLOCK(&handle->lock);
/*
* The handle doesn't exist on any list, and nothing is locked.
* Return the handle to the memory pool, and decrement the adb's
* reference count.
*/
ai = ISC_LIST_HEAD(handle->list);
while (ai != NULL) {
ISC_LIST_UNLINK(handle->list, ai, link);
entry = ai->entry;
ai->entry = NULL;
ai->sockaddr = NULL;
INSIST(DNS_ADBENTRY_VALID(entry));
dec_entry_refcnt(adb, entry, ISC_TRUE);
free_adbaddrinfo(adb, &ai);
ai = ISC_LIST_HEAD(handle->list);
}
free_adbhandle(adb, &handle);
}
void
dns_adb_dump(dns_adb_t *adb, FILE *f)
{
int i;
isc_sockaddr_t *sa;
dns_adbname_t *name;
dns_adbentry_t *entry;
char tmp[512];
const char *tmpp;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(f != NULL);
/*
* Lock the adb itself, lock all the name buckets, then lock all
* the entry buckets. This should put the adb into a state where
* nothing can change, so we can iterate through everything and
* print at our leasure.
*/
LOCK(&adb->lock);
fprintf(f, "ADB %p DUMP:\n", adb);
fprintf(f, "erefcnt %u, irefcnt %u\n", adb->erefcnt, adb->irefcnt);
for (i = 0 ; i < DNS_ADBNAMELIST_LENGTH ; i++)
LOCK(&adb->namelocks[i]);
for (i = 0 ; i < DNS_ADBENTRYLIST_LENGTH ; i++)
LOCK(&adb->entrylocks[i]);
/*
* Dump the names
*/
fprintf(f, "Names:\n");
for (i = 0 ; i < DNS_ADBNAMELIST_LENGTH ; i++) {
name = ISC_LIST_HEAD(adb->names[i]);
if (name == NULL)
continue;
fprintf(f, "Name bucket %d:\n", i);
while (name != NULL) {
fprintf(f, "name %p\n", name);
if (!DNS_ADBNAME_VALID(name))
fprintf(f, "\tMAGIC %08x\n", name->magic);
fprintf(f, "\t");
print_dns_name(f, &name->name);
fprintf(f, "\n");
print_namehook_list(f, name);
print_handle_list(f, name);
fprintf(f, "\n");
name = ISC_LIST_NEXT(name, link);
}
}
/*
* Dump the entries
*/
fprintf(f, "Entries:\n");
for (i = 0 ; i < DNS_ADBENTRYLIST_LENGTH ; i++) {
entry = ISC_LIST_HEAD(adb->entries[i]);
if (entry == NULL)
continue;
fprintf(f, "Entry bucket %d:\n", i);
while (entry != NULL) {
if (!DNS_ADBENTRY_VALID(entry))
fprintf(f, "\tMAGIC %08x\n", entry->magic);
if (entry->lock_bucket != i)
fprintf(f, "\tWRONG BUCKET! lock_bucket %d\n",
entry->lock_bucket);
sa = &entry->sockaddr;
switch (sa->type.sa.sa_family) {
case AF_INET:
tmpp = inet_ntop(AF_INET,
&sa->type.sin.sin_addr,
tmp, sizeof tmp);
break;
case AF_INET6:
tmpp = inet_ntop(AF_INET6,
&sa->type.sin6.sin6_addr,
tmp, sizeof tmp);
break;
default:
tmpp = "UnkFamily";
}
if (tmpp == NULL)
tmpp = "CANNOT TRANSLATE ADDRESS!";
fprintf(f, "\trefcnt %u flags %08x goodness %d"
" srtt %u addr %s\n",
entry->refcnt, entry->flags, entry->goodness,
entry->srtt, tmpp);
entry = ISC_LIST_NEXT(entry, link);
}
}
/*
* Unlock everything
*/
for (i = 0 ; i < DNS_ADBENTRYLIST_LENGTH ; i++)
UNLOCK(&adb->entrylocks[i]);
for (i = 0 ; i < DNS_ADBNAMELIST_LENGTH ; i++)
UNLOCK(&adb->namelocks[i]);
UNLOCK(&adb->lock);
}
void
dns_adb_dumphandle(dns_adb_t *adb, dns_adbhandle_t *handle, FILE *f)
{
char tmp[512];
const char *tmpp;
dns_adbaddrinfo_t *ai;
isc_sockaddr_t *sa;
/*
* Not used currently, in the API Just In Case we
* want to dump out the name and/or entries too.
*/
(void)adb;
LOCK(&handle->lock);
fprintf(f, "Handle %p\n", handle);
fprintf(f, "\tquery_pending %d, result %d (%s)\n",
handle->query_pending, handle->result,
isc_result_totext(handle->result));
fprintf(f, "\tname_bucket %d, name %p, event sender %p\n",
handle->name_bucket, handle->adbname, handle->event.sender);
ai = ISC_LIST_HEAD(handle->list);
if (ai != NULL)
fprintf(f, "\tAddresses:\n");
while (ai != NULL) {
sa = ai->sockaddr;
switch (sa->type.sa.sa_family) {
case AF_INET:
tmpp = inet_ntop(AF_INET, &sa->type.sin.sin_addr,
tmp, sizeof tmp);
break;
case AF_INET6:
tmpp = inet_ntop(AF_INET6, &sa->type.sin6.sin6_addr,
tmp, sizeof tmp);
break;
default:
tmpp = "UnkFamily";
}
if (tmpp == NULL)
tmpp = "CANNOT TRANSLATE ADDRESS!";
fprintf(f, "\t\tentry %p, flags %08x goodness %d"
" srtt %u addr %s\n",
ai->entry, ai->flags, ai->goodness, ai->srtt, tmpp);
ai = ISC_LIST_NEXT(ai, link);
}
UNLOCK(&handle->lock);
}
static void
print_dns_name(FILE *f, dns_name_t *name)
{
char buf[257];
isc_buffer_t b;
INSIST(f != NULL);
memset(buf, 0, sizeof (buf));
isc_buffer_init(&b, buf, sizeof (buf) - 1, ISC_BUFFERTYPE_TEXT);
dns_name_totext(name, ISC_FALSE, &b);
fprintf(f, buf); /* safe, since names < 256 chars, and we memset */
}
static void
print_namehook_list(FILE *f, dns_adbname_t *n)
{
dns_adbnamehook_t *nh;
nh = ISC_LIST_HEAD(n->namehooks);
while (nh != NULL) {
fprintf(f, "\t\tHook %p -> entry %p\n", nh, nh->entry);
nh = ISC_LIST_NEXT(nh, link);
}
}
static void
print_handle_list(FILE *f, dns_adbname_t *name)
{
dns_adbhandle_t *handle;
handle = ISC_LIST_HEAD(name->handles);
while (handle != NULL) {
fprintf(f, "\t\tHandle %p\n", handle);
handle = ISC_LIST_NEXT(handle, link);
}
}
/*
* On entry, "bucket" refers to a locked name bucket, "handle" is not NULL,
* and "name" is the name we are looking for. We will allocate an adbname
* and return a pointer to it in *adbnamep.
*
* If we return ISC_R_SUCCESS, the new name will have been allocated, and
* perhaps some namehooks will have been filled in with valid entries, and
* perhaps some fetches have been started.
*/
static isc_result_t
construct_name(dns_adb_t *adb, dns_adbhandle_t *handle, dns_name_t *name,
dns_adbname_t *adbname, int bucket)
{
dns_adbentry_t *entry;
dns_adbnamehook_t *nh;
isc_result_t result;
int addr_bucket;
isc_boolean_t return_success;
INSIST(DNS_ADB_VALID(adb));
INSIST(DNS_ADBHANDLE_VALID(handle));
INSIST(name != NULL);
INSIST(DNS_ADBNAME_VALID(adbname));
INSIST(bucket != DNS_ADB_INVALIDBUCKET);
result = ISC_R_UNEXPECTED;
addr_bucket = DNS_ADB_INVALIDBUCKET;
return_success = ISC_FALSE;
/*
* Allocate an entry and a namehook, but don't string them up
* anywhere yet. If we need more, we will have to get more later.
* These are the first two that cannot fail or else we are broken.
* Later ones can fail, but it is still bad to have that happen.
*/
entry = new_adbentry(adb);
if (entry == NULL) {
result = ISC_R_NOMEMORY;
goto fail;
}
nh = new_adbnamehook(adb, NULL);
if (nh == NULL) {
result = ISC_R_NOMEMORY;
goto fail;
}
/*
* Look up the A record in the database, the cache, or in glue.
* If it isn't found in any of these, we will have to start a
* fetch for it.
*/
fail:
if (entry != NULL)
free_adbentry(adb, &entry);
if (nh != NULL)
free_adbnamehook(adb, &nh);
if (return_success)
return (ISC_R_SUCCESS);
adbname->partial_results = ISC_TRUE;
return (result);
}
isc_result_t
dns_adb_marklame(dns_adb_t *adb, dns_adbaddrinfo_t *addr, dns_name_t *zone,
isc_stdtime_t expire_time)
{
dns_adbzoneinfo_t *zi;
int bucket;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(DNS_ADBADDRINFO_VALID(addr));
REQUIRE(zone != NULL);
zi = new_adbzoneinfo(adb, zone);
if (zi == NULL)
return (ISC_R_NOMEMORY);
zi->lame_timer = expire_time;
bucket = addr->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
ISC_LIST_PREPEND(addr->entry->zoneinfo, zi, link);
UNLOCK(&adb->entrylocks[bucket]);
return (ISC_R_SUCCESS);
}