adb.c revision fbcf7bc351f2d42d338cb1fa29e59babd9bee94e
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff/*
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff * Copyright (C) 1999 Internet Software Consortium.
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff *
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff * Permission to use, copy, modify, and distribute this software for any
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff * purpose with or without fee is hereby granted, provided that the above
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff * copyright notice and this permission notice appear in all copies.
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff *
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff * SOFTWARE.
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff */
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff/*
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * Implementation notes
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * --------------------
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff *
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * In handles, if task == NULL, no events will be generated, and no events
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * have been sent. If task != NULL but taskaction == NULL, an event has been
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * posted but not yet freed. If neigher are NULL, no event was posted.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff *
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff */
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff#include <config.h>
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff#include <stdio.h>
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff#include <string.h>
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff#include <isc/assertions.h>
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff#include <isc/condition.h>
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff#include <isc/event.h>
31fab17bcdbe302592a6c0dc5374ef56333ee879Michael Graff#include <isc/magic.h>
213973a334f92d4aef4ef62b4538fc2e4d0e8082Michael Graff#include <isc/mutex.h>
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff#include <isc/mutexblock.h>
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff#include <isc/random.h>
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
e45d323a2a0f4ca08d4b139546e60a5fa7bd3f0cMichael Graff#include <dns/adb.h>
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff#include <dns/db.h>
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff#include <dns/events.h>
11efdeb076d65fa9f0c5fc067dc040e7c99dfba6Michael Graff#include <dns/fixedname.h>
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff#include <dns/name.h>
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff#include <dns/rdata.h>
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff#include <dns/rdataset.h>
11efdeb076d65fa9f0c5fc067dc040e7c99dfba6Michael Graff#include <dns/resolver.h>
11efdeb076d65fa9f0c5fc067dc040e7c99dfba6Michael Graff#include <dns/types.h>
439c0011e642fb1d26011116144af698125262dbMichael Graff#include <dns/view.h>
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff#include "../isc/util.h"
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff
31fab17bcdbe302592a6c0dc5374ef56333ee879Michael Graff#define DNS_ADB_MAGIC 0x44616462 /* Dadb. */
31fab17bcdbe302592a6c0dc5374ef56333ee879Michael Graff#define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
31fab17bcdbe302592a6c0dc5374ef56333ee879Michael Graff#define DNS_ADBNAME_MAGIC 0x6164624e /* adbN. */
31fab17bcdbe302592a6c0dc5374ef56333ee879Michael Graff#define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
31fab17bcdbe302592a6c0dc5374ef56333ee879Michael Graff#define DNS_ADBNAMEHOOK_MAGIC 0x61644e48 /* adNH. */
31fab17bcdbe302592a6c0dc5374ef56333ee879Michael Graff#define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
31fab17bcdbe302592a6c0dc5374ef56333ee879Michael Graff#define DNS_ADBZONEINFO_MAGIC 0x6164625a /* adbZ. */
31fab17bcdbe302592a6c0dc5374ef56333ee879Michael Graff#define DNS_ADBZONEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBZONEINFO_MAGIC)
31fab17bcdbe302592a6c0dc5374ef56333ee879Michael Graff#define DNS_ADBENTRY_MAGIC 0x61646245 /* adbE. */
31fab17bcdbe302592a6c0dc5374ef56333ee879Michael Graff#define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff#define DNS_ADBFETCH_MAGIC 0x61646246 /* adbF. */
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff#define DNS_ADBFETCH_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff/*
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * Lengths of lists needs to be powers of two.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff */
f00d96a15cdd11e764437f9359e67328631caaeaMichael Graff#define DNS_ADBNAMELIST_LENGTH 32 /* how many buckets for names */
f00d96a15cdd11e764437f9359e67328631caaeaMichael Graff#define DNS_ADBENTRYLIST_LENGTH 32 /* how many buckets for addresses */
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff#define FREE_ITEMS 16 /* free count for memory pools */
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff#define FILL_COUNT 8 /* fill count for memory pools */
213973a334f92d4aef4ef62b4538fc2e4d0e8082Michael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff#define DNS_ADB_INVALIDBUCKET (-1) /* invalid bucket address */
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
213973a334f92d4aef4ef62b4538fc2e4d0e8082Michael Grafftypedef ISC_LIST(dns_adbname_t) dns_adbnamelist_t;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Grafftypedef struct dns_adbnamehook dns_adbnamehook_t;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Grafftypedef struct dns_adbzoneinfo dns_adbzoneinfo_t;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Grafftypedef ISC_LIST(dns_adbentry_t) dns_adbentrylist_t;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Grafftypedef struct dns_adbfetch dns_adbfetch_t;
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graffstruct dns_adb {
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff unsigned int magic;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff isc_mutex_t lock;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff isc_condition_t shutdown_cond;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff isc_mem_t *mctx;
439c0011e642fb1d26011116144af698125262dbMichael Graff dns_view_t *view;
11efdeb076d65fa9f0c5fc067dc040e7c99dfba6Michael Graff isc_timermgr_t *timermgr;
11efdeb076d65fa9f0c5fc067dc040e7c99dfba6Michael Graff isc_taskmgr_t *taskmgr;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff unsigned int irefcnt;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff unsigned int erefcnt;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff isc_mutex_t mplock;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff isc_mempool_t *nmp; /* dns_adbname_t */
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff isc_mempool_t *nhmp; /* dns_adbnamehook_t */
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff isc_mempool_t *zimp; /* dns_adbzoneinfo_t */
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff isc_mempool_t *emp; /* dns_adbentry_t */
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff isc_mempool_t *ahmp; /* dns_adbhandle_t */
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff isc_mempool_t *aimp; /* dns_adbaddrinfo_t */
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff isc_mempool_t *afmp; /* dns_adbfetch_t */
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff isc_random_t rand;
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff
213973a334f92d4aef4ef62b4538fc2e4d0e8082Michael Graff /*
213973a334f92d4aef4ef62b4538fc2e4d0e8082Michael Graff * Bucketized locks and lists for names.
213973a334f92d4aef4ef62b4538fc2e4d0e8082Michael Graff */
213973a334f92d4aef4ef62b4538fc2e4d0e8082Michael Graff dns_adbnamelist_t names[DNS_ADBNAMELIST_LENGTH];
213973a334f92d4aef4ef62b4538fc2e4d0e8082Michael Graff isc_mutex_t namelocks[DNS_ADBNAMELIST_LENGTH];
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff isc_boolean_t name_sd[DNS_ADBNAMELIST_LENGTH];
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff unsigned int name_refcnt[DNS_ADBNAMELIST_LENGTH];
213973a334f92d4aef4ef62b4538fc2e4d0e8082Michael Graff
213973a334f92d4aef4ef62b4538fc2e4d0e8082Michael Graff /*
213973a334f92d4aef4ef62b4538fc2e4d0e8082Michael Graff * Bucketized locks for entries.
213973a334f92d4aef4ef62b4538fc2e4d0e8082Michael Graff */
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_adbentrylist_t entries[DNS_ADBENTRYLIST_LENGTH];
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff isc_mutex_t entrylocks[DNS_ADBENTRYLIST_LENGTH];
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff};
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graffstruct dns_adbname {
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff unsigned int magic;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_name_t name;
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff isc_boolean_t partial_result;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff isc_stdtime_t expire_time;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff ISC_LIST(dns_adbnamehook_t) namehooks;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff ISC_LIST(dns_adbfetch_t) fetches;
439c0011e642fb1d26011116144af698125262dbMichael Graff ISC_LIST(dns_adbhandle_t) handles;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_LINK(dns_adbname_t) link;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff};
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graffstruct dns_adbfetch {
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff unsigned int magic;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff dns_adbnamehook_t *namehook;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff dns_adbentry_t *entry;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff dns_fetch_t *fetch;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff dns_rdataset_t rdataset;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff ISC_LINK(dns_adbfetch_t) link;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff};
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff/*
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff * dns_adbnamehook_t
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff *
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff * This is a small widget that dangles off a dns_adbname_t. It contains a
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff * pointer to the address information about this host, and a link to the next
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff * namehook that will contain the next address this host has.
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff */
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graffstruct dns_adbnamehook {
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff unsigned int magic;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_adbentry_t *entry;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff ISC_LINK(dns_adbnamehook_t) link;
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff};
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff/*
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff * dns_adbzoneinfo_t
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff *
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff * This is a small widget that holds zone-specific information about an
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff * address. Currently limited to lameness, but could just as easily be
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff * extended to other types of information about zones.
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff */
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graffstruct dns_adbzoneinfo {
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff unsigned int magic;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff dns_name_t zone;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff isc_stdtime_t lame_timer;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff ISC_LINK(dns_adbzoneinfo_t) link;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff};
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff/*
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff * An address entry. It holds quite a bit of information about addresses,
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff * including edns state, rtt, and of course the address of the host.
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff */
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graffstruct dns_adbentry {
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff unsigned int magic;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff int lock_bucket;
439c0011e642fb1d26011116144af698125262dbMichael Graff unsigned int refcnt;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff unsigned int flags;
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff int edns_level; /* must be int! */
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff int goodness; /* bad < 0 <= good */
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff unsigned int srtt;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff isc_sockaddr_t sockaddr;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff ISC_LIST(dns_adbzoneinfo_t) zoneinfo;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_LINK(dns_adbentry_t) link;
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff};
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff/*
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * Internal functions (and prototypes).
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff */
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graffstatic inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
439c0011e642fb1d26011116144af698125262dbMichael Graffstatic inline void free_adbname(dns_adb_t *, dns_adbname_t **);
439c0011e642fb1d26011116144af698125262dbMichael Graffstatic inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
439c0011e642fb1d26011116144af698125262dbMichael Graff dns_adbentry_t *);
439c0011e642fb1d26011116144af698125262dbMichael Graffstatic inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graffstatic inline dns_adbzoneinfo_t *new_adbzoneinfo(dns_adb_t *, dns_name_t *);
439c0011e642fb1d26011116144af698125262dbMichael Graffstatic inline void free_adbzoneinfo(dns_adb_t *, dns_adbzoneinfo_t **);
439c0011e642fb1d26011116144af698125262dbMichael Graffstatic inline dns_adbentry_t *new_adbentry(dns_adb_t *);
439c0011e642fb1d26011116144af698125262dbMichael Graffstatic inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
439c0011e642fb1d26011116144af698125262dbMichael Graffstatic inline dns_adbhandle_t *new_adbhandle(dns_adb_t *);
439c0011e642fb1d26011116144af698125262dbMichael Graffstatic inline void free_adbhandle(dns_adb_t *, dns_adbhandle_t **);
439c0011e642fb1d26011116144af698125262dbMichael Graffstatic inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *,
439c0011e642fb1d26011116144af698125262dbMichael Graff dns_adbentry_t *);
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graffstatic inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
439c0011e642fb1d26011116144af698125262dbMichael Graff int *);
439c0011e642fb1d26011116144af698125262dbMichael Graffstatic inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
439c0011e642fb1d26011116144af698125262dbMichael Graff isc_sockaddr_t *, int *);
439c0011e642fb1d26011116144af698125262dbMichael Graffstatic void print_dns_name(FILE *, dns_name_t *);
439c0011e642fb1d26011116144af698125262dbMichael Graffstatic void print_namehook_list(FILE *, dns_adbname_t *);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graffstatic void print_handle_list(FILE *, dns_adbname_t *);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graffstatic inline void inc_adb_irefcnt(dns_adb_t *, isc_boolean_t);
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graffstatic inline isc_boolean_t dec_adb_irefcnt(dns_adb_t *, isc_boolean_t);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graffstatic inline void inc_adb_erefcnt(dns_adb_t *, isc_boolean_t);
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graffstatic inline isc_boolean_t dec_adb_erefcnt(dns_adb_t *, isc_boolean_t);
439c0011e642fb1d26011116144af698125262dbMichael Graffstatic inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
439c0011e642fb1d26011116144af698125262dbMichael Graff isc_boolean_t);
439c0011e642fb1d26011116144af698125262dbMichael Graffstatic inline void dec_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
439c0011e642fb1d26011116144af698125262dbMichael Graff isc_boolean_t);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graffstatic inline void violate_locking_hierarchy(isc_mutex_t *, isc_mutex_t *);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graffstatic void clean_namehooks_at_name(dns_adb_t *, dns_adbname_t *);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graffstatic void clean_handles_at_name(dns_adbname_t *, isc_eventtype_t);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graffstatic isc_result_t construct_name(dns_adb_t *, dns_adbhandle_t *,
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff dns_name_t *, dns_name_t *,
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff dns_adbname_t *, int, isc_stdtime_t);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graffstatic inline isc_boolean_t check_exit(dns_adb_t *);
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graffstatic inline void
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graffviolate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want)
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff{
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff UNLOCK(have);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff LOCK(want);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff LOCK(have);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff }
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff}
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff/*
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff * The ADB _MUST_ be locked before calling. Also, exit conditions must be
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff * checked after calling this function.
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff */
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graffstatic void
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graffshutdown_names(dns_adb_t *adb, isc_boolean_t kill_fetches)
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff{
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff int bucket;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff dns_adbname_t *name;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff (void)kill_fetches; /* XXX need to implement */
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff for (bucket = 0 ; bucket < DNS_ADBNAMELIST_LENGTH ; bucket++) {
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff LOCK(&adb->namelocks[bucket]);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff adb->name_sd[bucket] = ISC_TRUE;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff /*
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff * Run through the list. For each name, clean up handles
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff * found there, and cancel any fetches running. When
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff * all the fetches are canceled, the name will destroy
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff * itself.
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff */
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff name = ISC_LIST_HEAD(adb->names[bucket]);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff while (name != NULL) {
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff clean_handles_at_name(name, DNS_EVENT_ADBSHUTDOWN);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff clean_namehooks_at_name(adb, name);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff /* XXX TODO: cancel fetches */
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff /* if no fetches, kill! */
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff ISC_LIST_UNLINK(adb->names[bucket], name, link);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff free_adbname(adb, &name);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff INSIST(adb->name_refcnt[bucket] > 0);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff adb->name_refcnt[bucket]--;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff name = ISC_LIST_HEAD(adb->names[bucket]);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff }
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff if (adb->name_refcnt[bucket] == 0)
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff dec_adb_irefcnt(adb, ISC_FALSE);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff UNLOCK(&adb->namelocks[bucket]);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff }
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff}
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff/*
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff * Assumes the name bucket is locked.
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff */
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graffstatic void
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graffclean_namehooks_at_name(dns_adb_t *adb, dns_adbname_t *name)
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff{
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff dns_adbentry_t *entry;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff dns_adbnamehook_t *namehook;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff int addr_bucket;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff addr_bucket = DNS_ADB_INVALIDBUCKET;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff namehook = ISC_LIST_HEAD(name->namehooks);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff while (namehook != NULL) {
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff INSIST(DNS_ADBNAMEHOOK_VALID(namehook));
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff /*
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff * Clean up the entry if needed.
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff */
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff entry = namehook->entry;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff if (entry != NULL) {
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff INSIST(DNS_ADBENTRY_VALID(entry));
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff if (addr_bucket != entry->lock_bucket) {
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff if (addr_bucket != DNS_ADB_INVALIDBUCKET)
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff UNLOCK(&adb->entrylocks[addr_bucket]);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff addr_bucket = entry->lock_bucket;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff LOCK(&adb->entrylocks[addr_bucket]);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff }
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff dec_entry_refcnt(adb, entry, ISC_FALSE);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff }
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff /*
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff * Free the namehook
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff */
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff namehook->entry = NULL;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff ISC_LIST_UNLINK(name->namehooks, namehook, link);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff free_adbnamehook(adb, &namehook);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff namehook = ISC_LIST_HEAD(name->namehooks);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff }
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff if (addr_bucket != DNS_ADB_INVALIDBUCKET)
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff UNLOCK(&adb->entrylocks[addr_bucket]);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff}
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff/*
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff * Assumes the name bucket is locked.
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff */
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graffstatic void
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graffclean_handles_at_name(dns_adbname_t *name, isc_eventtype_t evtype)
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff{
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff isc_event_t *ev;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff isc_task_t *task;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff dns_adbhandle_t *handle;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff handle = ISC_LIST_HEAD(name->handles);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff while (handle != NULL) {
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff LOCK(&handle->lock);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff ev = &handle->event;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff task = ev->sender;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff ev->sender = handle;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff ev->type = evtype;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff isc_task_sendanddetach(&task, &ev);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff /*
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff * Unlink the handle from the name, letting the caller
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff * call dns_adb_done() on it to clean it up later.
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff */
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff ISC_LIST_UNLINK(name->handles, handle, link);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff handle->adbname = NULL;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff handle->name_bucket = DNS_ADB_INVALIDBUCKET;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff UNLOCK(&handle->lock);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff handle = ISC_LIST_HEAD(name->handles);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff }
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff}
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graffstatic inline isc_boolean_t
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graffcheck_exit(dns_adb_t *adb)
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff{
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff if ((adb->irefcnt == 0)
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff && (adb->erefcnt == 0)
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff && (isc_mempool_getallocated(adb->ahmp) == 0))
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff return (ISC_TRUE);
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff return (ISC_FALSE);
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff}
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graffstatic inline void
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graffinc_adb_irefcnt(dns_adb_t *adb, isc_boolean_t lock)
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff{
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff if (lock)
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff LOCK(&adb->lock);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff adb->irefcnt++;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff if (lock)
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff UNLOCK(&adb->lock);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff}
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graffstatic inline isc_boolean_t
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graffdec_adb_irefcnt(dns_adb_t *adb, isc_boolean_t lock)
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff{
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff isc_boolean_t kill;
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff if (lock)
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff LOCK(&adb->lock);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff INSIST(adb->irefcnt > 0);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff adb->irefcnt--;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff kill = check_exit(adb);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff if (lock)
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff UNLOCK(&adb->lock);
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff return (kill);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff}
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline void
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graffinc_adb_erefcnt(dns_adb_t *adb, isc_boolean_t lock)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff{
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff if (lock)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff LOCK(&adb->lock);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff adb->erefcnt++;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff if (lock)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff UNLOCK(&adb->lock);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff}
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graffstatic inline isc_boolean_t
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graffdec_adb_erefcnt(dns_adb_t *adb, isc_boolean_t lock)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff{
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff isc_boolean_t kill;
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff if (lock)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff LOCK(&adb->lock);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff INSIST(adb->erefcnt > 0);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff adb->erefcnt--;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff kill = check_exit(adb);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff if (lock)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff UNLOCK(&adb->lock);
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff return (kill);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff}
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graffstatic inline void
439c0011e642fb1d26011116144af698125262dbMichael Graffinc_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock)
439c0011e642fb1d26011116144af698125262dbMichael Graff{
439c0011e642fb1d26011116144af698125262dbMichael Graff int bucket;
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff bucket = entry->lock_bucket;
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff if (lock)
439c0011e642fb1d26011116144af698125262dbMichael Graff LOCK(&adb->entrylocks[bucket]);
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff entry->refcnt++;
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff if (lock)
439c0011e642fb1d26011116144af698125262dbMichael Graff UNLOCK(&adb->entrylocks[bucket]);
439c0011e642fb1d26011116144af698125262dbMichael Graff}
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graffstatic inline void
439c0011e642fb1d26011116144af698125262dbMichael Graffdec_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock)
439c0011e642fb1d26011116144af698125262dbMichael Graff{
439c0011e642fb1d26011116144af698125262dbMichael Graff int bucket;
439c0011e642fb1d26011116144af698125262dbMichael Graff isc_boolean_t destroy_entry;
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff bucket = entry->lock_bucket;
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff if (lock)
439c0011e642fb1d26011116144af698125262dbMichael Graff LOCK(&adb->entrylocks[bucket]);
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff INSIST(entry->refcnt > 0);
439c0011e642fb1d26011116144af698125262dbMichael Graff entry->refcnt--;
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff destroy_entry = ISC_FALSE;
439c0011e642fb1d26011116144af698125262dbMichael Graff if (entry->refcnt == 0) {
439c0011e642fb1d26011116144af698125262dbMichael Graff destroy_entry = ISC_TRUE;
439c0011e642fb1d26011116144af698125262dbMichael Graff ISC_LIST_UNLINK(adb->entries[bucket], entry, link);
439c0011e642fb1d26011116144af698125262dbMichael Graff }
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff if (lock)
439c0011e642fb1d26011116144af698125262dbMichael Graff UNLOCK(&adb->entrylocks[bucket]);
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff if (!destroy_entry)
439c0011e642fb1d26011116144af698125262dbMichael Graff return;
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff free_adbentry(adb, &entry);
439c0011e642fb1d26011116144af698125262dbMichael Graff}
439c0011e642fb1d26011116144af698125262dbMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline dns_adbname_t *
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graffnew_adbname(dns_adb_t *adb, dns_name_t *dnsname)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff{
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_adbname_t *name;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff name = isc_mempool_get(adb->nmp);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (name == NULL)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (NULL);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_name_init(&name->name, NULL);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff if (dns_name_dup(dnsname, adb->mctx, &name->name) != ISC_R_SUCCESS) {
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff isc_mempool_put(adb->nmp, name);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff return (NULL);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff }
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff name->magic = DNS_ADBNAME_MAGIC;
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff name->partial_result = ISC_FALSE;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff name->expire_time = 0;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_LIST_INIT(name->namehooks);
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff ISC_LIST_INIT(name->fetches);
439c0011e642fb1d26011116144af698125262dbMichael Graff ISC_LIST_INIT(name->handles);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_LINK_INIT(name, link);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (name);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff}
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline void
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Grafffree_adbname(dns_adb_t *adb, dns_adbname_t **name)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff{
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff dns_adbname_t *n;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff INSIST(name != NULL && DNS_ADBNAME_VALID(*name));
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff n = *name;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff *name = NULL;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff INSIST(ISC_LIST_EMPTY(n->namehooks));
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff INSIST(ISC_LIST_EMPTY(n->fetches));
439c0011e642fb1d26011116144af698125262dbMichael Graff INSIST(ISC_LIST_EMPTY(n->handles));
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff INSIST(!ISC_LINK_LINKED(n, link));
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff n->magic = 0;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff dns_name_free(&n->name, adb->mctx);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff isc_mempool_put(adb->nmp, n);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff}
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline dns_adbnamehook_t *
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graffnew_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff{
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_adbnamehook_t *nh;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff nh = isc_mempool_get(adb->nhmp);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (nh == NULL)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (NULL);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff nh->magic = DNS_ADBNAMEHOOK_MAGIC;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff nh->entry = entry;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_LINK_INIT(nh, link);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (nh);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff}
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline void
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Grafffree_adbnamehook(dns_adb_t *adb, dns_adbnamehook_t **namehook)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff{
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff dns_adbnamehook_t *nh;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff INSIST(namehook != NULL && DNS_ADBNAMEHOOK_VALID(*namehook));
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff nh = *namehook;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff *namehook = NULL;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff INSIST(nh->entry == NULL);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff INSIST(!ISC_LINK_LINKED(nh, link));
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff nh->magic = 0;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff isc_mempool_put(adb->nhmp, nh);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff}
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline dns_adbzoneinfo_t *
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graffnew_adbzoneinfo(dns_adb_t *adb, dns_name_t *zone)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff{
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_adbzoneinfo_t *zi;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff zi = isc_mempool_get(adb->zimp);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (zi == NULL)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (NULL);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff dns_name_init(&zi->zone, NULL);
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graff if (dns_name_dup(zone, adb->mctx, &zi->zone) != ISC_R_SUCCESS) {
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graff isc_mempool_put(adb->zimp, zi);
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graff return (NULL);
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graff }
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graff
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graff zi->magic = DNS_ADBZONEINFO_MAGIC;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff zi->lame_timer = 0;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_LINK_INIT(zi, link);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (zi);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff}
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graffstatic inline void
439c0011e642fb1d26011116144af698125262dbMichael Grafffree_adbzoneinfo(dns_adb_t *adb, dns_adbzoneinfo_t **zoneinfo)
439c0011e642fb1d26011116144af698125262dbMichael Graff{
439c0011e642fb1d26011116144af698125262dbMichael Graff dns_adbzoneinfo_t *zi;
439c0011e642fb1d26011116144af698125262dbMichael Graff
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff INSIST(zoneinfo != NULL && DNS_ADBZONEINFO_VALID(*zoneinfo));
439c0011e642fb1d26011116144af698125262dbMichael Graff zi = *zoneinfo;
439c0011e642fb1d26011116144af698125262dbMichael Graff *zoneinfo = NULL;
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff INSIST(!ISC_LINK_LINKED(zi, link));
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff dns_name_free(&zi->zone, adb->mctx);
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff zi->magic = 0;
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff isc_mempool_put(adb->zimp, zi);
439c0011e642fb1d26011116144af698125262dbMichael Graff}
439c0011e642fb1d26011116144af698125262dbMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline dns_adbentry_t *
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graffnew_adbentry(dns_adb_t *adb)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff{
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_adbentry_t *e;
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff isc_uint32_t r;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff e = isc_mempool_get(adb->emp);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (e == NULL)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (NULL);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff e->magic = DNS_ADBENTRY_MAGIC;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff e->lock_bucket = DNS_ADB_INVALIDBUCKET;
439c0011e642fb1d26011116144af698125262dbMichael Graff e->refcnt = 0;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff e->flags = 0;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff e->goodness = 0;
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff isc_random_get(&adb->rand, &r);
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff e->srtt = (r & 0x1f) + 1;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_LIST_INIT(e->zoneinfo);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_LINK_INIT(e, link);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (e);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff}
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline void
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Grafffree_adbentry(dns_adb_t *adb, dns_adbentry_t **entry)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff{
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff dns_adbentry_t *e;
3f6a66689410910ef601a4d26f10a24f331ef83cMichael Graff dns_adbzoneinfo_t *zi;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff INSIST(entry != NULL && DNS_ADBENTRY_VALID(*entry));
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff e = *entry;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff *entry = NULL;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff INSIST(e->lock_bucket == DNS_ADB_INVALIDBUCKET);
439c0011e642fb1d26011116144af698125262dbMichael Graff INSIST(e->refcnt == 0);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff INSIST(!ISC_LINK_LINKED(e, link));
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff e->magic = 0;
3f6a66689410910ef601a4d26f10a24f331ef83cMichael Graff
3f6a66689410910ef601a4d26f10a24f331ef83cMichael Graff zi = ISC_LIST_HEAD(e->zoneinfo);
3f6a66689410910ef601a4d26f10a24f331ef83cMichael Graff while (zi != NULL) {
3f6a66689410910ef601a4d26f10a24f331ef83cMichael Graff ISC_LIST_UNLINK(e->zoneinfo, zi, link);
3f6a66689410910ef601a4d26f10a24f331ef83cMichael Graff free_adbzoneinfo(adb, &zi);
3f6a66689410910ef601a4d26f10a24f331ef83cMichael Graff zi = ISC_LIST_HEAD(e->zoneinfo);
3f6a66689410910ef601a4d26f10a24f331ef83cMichael Graff }
3f6a66689410910ef601a4d26f10a24f331ef83cMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff isc_mempool_put(adb->emp, e);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff}
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline dns_adbhandle_t *
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graffnew_adbhandle(dns_adb_t *adb)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff{
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_adbhandle_t *h;
519b4a1a27c8b767a57a981dda69a3c6394bd49dMichael Graff isc_result_t result;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff h = isc_mempool_get(adb->ahmp);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (h == NULL)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (NULL);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
519b4a1a27c8b767a57a981dda69a3c6394bd49dMichael Graff /*
519b4a1a27c8b767a57a981dda69a3c6394bd49dMichael Graff * public members
519b4a1a27c8b767a57a981dda69a3c6394bd49dMichael Graff */
519b4a1a27c8b767a57a981dda69a3c6394bd49dMichael Graff h->magic = 0;
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff h->adb = adb;
439c0011e642fb1d26011116144af698125262dbMichael Graff h->query_pending = ISC_FALSE;
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff h->partial_result = ISC_FALSE;
439c0011e642fb1d26011116144af698125262dbMichael Graff h->result = ISC_R_UNEXPECTED;
519b4a1a27c8b767a57a981dda69a3c6394bd49dMichael Graff ISC_LIST_INIT(h->list);
439c0011e642fb1d26011116144af698125262dbMichael Graff ISC_LINK_INIT(h, next);
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff h->name_bucket = DNS_ADB_INVALIDBUCKET;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff h->adbname = NULL;
519b4a1a27c8b767a57a981dda69a3c6394bd49dMichael Graff
519b4a1a27c8b767a57a981dda69a3c6394bd49dMichael Graff /*
519b4a1a27c8b767a57a981dda69a3c6394bd49dMichael Graff * private members
519b4a1a27c8b767a57a981dda69a3c6394bd49dMichael Graff */
519b4a1a27c8b767a57a981dda69a3c6394bd49dMichael Graff result = isc_mutex_init(&h->lock);
519b4a1a27c8b767a57a981dda69a3c6394bd49dMichael Graff if (result != ISC_R_SUCCESS) {
519b4a1a27c8b767a57a981dda69a3c6394bd49dMichael Graff UNEXPECTED_ERROR(__FILE__, __LINE__,
519b4a1a27c8b767a57a981dda69a3c6394bd49dMichael Graff "isc_mutex_init failed in new_adbhandle()");
519b4a1a27c8b767a57a981dda69a3c6394bd49dMichael Graff isc_mempool_put(adb->ahmp, h);
519b4a1a27c8b767a57a981dda69a3c6394bd49dMichael Graff return (NULL);
519b4a1a27c8b767a57a981dda69a3c6394bd49dMichael Graff }
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_LINK_INIT(h, link);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_EVENT_INIT(&h->event, sizeof (isc_event_t), 0, 0, 0, NULL, NULL,
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff NULL, NULL, h);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
519b4a1a27c8b767a57a981dda69a3c6394bd49dMichael Graff h->magic = DNS_ADBHANDLE_MAGIC;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (h);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff}
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graffstatic inline dns_adbfetch_t *
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graffnew_adbfetch(dns_adb_t *adb)
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff{
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff dns_adbfetch_t *f;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff f = isc_mempool_get(adb->afmp);
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff if (f == NULL)
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff return (NULL);
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff f->magic = 0;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff f->namehook = NULL;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff f->entry = NULL;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff f->fetch = NULL;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff f->namehook = new_adbnamehook(adb, NULL);
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff if (f->namehook == NULL)
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff goto err;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff f->entry = new_adbentry(adb);
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff if (f->entry == NULL)
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff goto err;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff dns_rdataset_init(&f->rdataset);
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff ISC_LINK_INIT(f, link);
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff f->magic = DNS_ADBFETCH_MAGIC;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff return (f);
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff err:
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff if (f->namehook != NULL)
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff free_adbnamehook(adb, &f->namehook);
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff if (f->entry != NULL)
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff free_adbentry(adb, &f->entry);
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff isc_mempool_put(adb->afmp, f);
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff return (NULL);
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff}
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graffstatic inline void
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Grafffree_adbfetch(dns_adb_t *adb, dns_adbfetch_t **fetch)
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff{
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff dns_adbfetch_t *f;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff INSIST(fetch != NULL && DNS_ADBFETCH_VALID(*fetch));
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff f = *fetch;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff *fetch = NULL;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff INSIST(!ISC_LINK_LINKED(f, link));
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff f->magic = 0;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff if (f->namehook != NULL)
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff free_adbnamehook(adb, &f->namehook);
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff if (f->entry != NULL)
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff free_adbentry(adb, &f->entry);
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff if (dns_rdataset_isassociated(&f->rdataset))
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff dns_rdataset_disassociate(&f->rdataset);
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff isc_mempool_put(adb->afmp, f);
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff}
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graffstatic inline void
439c0011e642fb1d26011116144af698125262dbMichael Grafffree_adbhandle(dns_adb_t *adb, dns_adbhandle_t **handlep)
439c0011e642fb1d26011116144af698125262dbMichael Graff{
439c0011e642fb1d26011116144af698125262dbMichael Graff dns_adbhandle_t *handle;
439c0011e642fb1d26011116144af698125262dbMichael Graff
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff INSIST(handlep != NULL && DNS_ADBHANDLE_VALID(*handlep));
439c0011e642fb1d26011116144af698125262dbMichael Graff handle = *handlep;
439c0011e642fb1d26011116144af698125262dbMichael Graff *handlep = NULL;
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff INSIST(ISC_LIST_EMPTY(handle->list));
439c0011e642fb1d26011116144af698125262dbMichael Graff INSIST(!ISC_LINK_LINKED(handle, next));
439c0011e642fb1d26011116144af698125262dbMichael Graff INSIST(!ISC_LINK_LINKED(handle, link));
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff INSIST(handle->name_bucket == DNS_ADB_INVALIDBUCKET);
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff INSIST(handle->adbname == NULL);
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff handle->magic = 0;
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff isc_mutex_destroy(&handle->lock);
439c0011e642fb1d26011116144af698125262dbMichael Graff isc_mempool_put(adb->ahmp, handle);
439c0011e642fb1d26011116144af698125262dbMichael Graff}
439c0011e642fb1d26011116144af698125262dbMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff/*
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * Copy bits from the entry into the newly allocated addrinfo. The entry
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * must be locked, and the reference count must be bumped up by one
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * if this function returns a valid pointer.
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff */
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline dns_adbaddrinfo_t *
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graffnew_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff{
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_adbaddrinfo_t *ai;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ai = isc_mempool_get(adb->aimp);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (ai == NULL)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (NULL);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ai->magic = DNS_ADBADDRINFO_MAGIC;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ai->sockaddr = &entry->sockaddr;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ai->goodness = entry->goodness;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ai->srtt = entry->srtt;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ai->flags = entry->flags;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ai->entry = entry;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_LINK_INIT(ai, link);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (ai);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff}
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graffstatic inline void
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Grafffree_adbaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **ainfo)
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff{
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff dns_adbaddrinfo_t *ai;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff INSIST(ainfo != NULL && DNS_ADBADDRINFO_VALID(*ainfo));
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff ai = *ainfo;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff *ainfo = NULL;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff INSIST(ai->sockaddr == NULL);
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff INSIST(ai->entry == NULL);
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff INSIST(!ISC_LINK_LINKED(ai, link));
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff ai->magic = 0;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff isc_mempool_put(adb->aimp, ai);
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff}
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff/*
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * Search for the name. NOTE: The bucket is kept locked on both
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * success and failure, so it must always be unlocked by the caller!
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff *
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * On the first call to this function, *bucketp must be set to
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * DNS_ADB_INVALIDBUCKET.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff */
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline dns_adbname_t *
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Grafffind_name_and_lock(dns_adb_t *adb, dns_name_t *name, int *bucketp)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff{
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_adbname_t *adbname;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff int bucket;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff bucket = dns_name_hash(name, ISC_FALSE);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff bucket &= (DNS_ADBNAMELIST_LENGTH - 1);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff if (*bucketp == DNS_ADB_INVALIDBUCKET) {
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff LOCK(&adb->namelocks[bucket]);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff *bucketp = bucket;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff } else if (*bucketp != bucket) {
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff UNLOCK(&adb->namelocks[*bucketp]);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff LOCK(&adb->namelocks[bucket]);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff *bucketp = bucket;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff }
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff adbname = ISC_LIST_HEAD(adb->names[bucket]);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff while (adbname != NULL) {
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (dns_name_equal(name, &adbname->name))
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (adbname);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff adbname = ISC_LIST_NEXT(adbname, link);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff }
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (NULL);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff}
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff/*
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * Search for the address. NOTE: The bucket is kept locked on both
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * success and failure, so it must always be unlocked by the caller.
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff *
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * On the first call to this function, *bucketp must be set to
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * DNS_ADB_INVALIDBUCKET. This will cause a lock to occur. On
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * later calls (within the same "lock path") it can be left alone, so
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * if this function is called multiple times locking is only done if
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * the bucket changes.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff */
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline dns_adbentry_t *
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Grafffind_entry_and_lock(dns_adb_t *adb, isc_sockaddr_t *addr, int *bucketp)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff{
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_adbentry_t *entry;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff int bucket;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff bucket = isc_sockaddr_hash(addr, ISC_TRUE);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff bucket &= (DNS_ADBENTRYLIST_LENGTH - 1);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff if (*bucketp == DNS_ADB_INVALIDBUCKET) {
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff LOCK(&adb->entrylocks[bucket]);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff *bucketp = bucket;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff } else if (*bucketp != bucket) {
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff UNLOCK(&adb->entrylocks[*bucketp]);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff LOCK(&adb->entrylocks[bucket]);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff *bucketp = bucket;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff }
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff entry = ISC_LIST_HEAD(adb->entries[bucket]);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff while (entry != NULL) {
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (isc_sockaddr_equal(addr, &entry->sockaddr))
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (entry);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff entry = ISC_LIST_NEXT(entry, link);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff }
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (NULL);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff}
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff/*
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * Entry bucket MUST be locked!
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff */
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graffstatic isc_boolean_t
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graffentry_is_bad_for_zone(dns_adb_t *adb, dns_adbentry_t *entry, dns_name_t *zone,
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff isc_stdtime_t now)
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff{
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff dns_adbzoneinfo_t *zi, *next_zi;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff isc_boolean_t is_bad;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff is_bad = ISC_FALSE;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff zi = ISC_LIST_HEAD(entry->zoneinfo);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff if (zi == NULL)
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff return (ISC_FALSE);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff while (zi != NULL) {
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff next_zi = ISC_LIST_NEXT(zi, link);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff /*
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * Has the entry expired?
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff */
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff if (zi->lame_timer < now) {
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff ISC_LIST_UNLINK(entry->zoneinfo, zi, link);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff free_adbzoneinfo(adb, &zi);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff }
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff /*
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff * Order tests from least to most expensive.
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff */
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff if (zi != NULL && !is_bad) {
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff if (dns_name_equal(zone, &zi->zone))
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff is_bad = ISC_TRUE;
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff }
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff zi = next_zi;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff }
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff return (is_bad);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff}
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graffstatic void
439c0011e642fb1d26011116144af698125262dbMichael Graffcopy_namehook_list(dns_adb_t *adb, dns_adbhandle_t *handle,
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff dns_adbname_t *name, dns_name_t *zone, isc_stdtime_t now)
439c0011e642fb1d26011116144af698125262dbMichael Graff{
439c0011e642fb1d26011116144af698125262dbMichael Graff dns_adbnamehook_t *namehook;
439c0011e642fb1d26011116144af698125262dbMichael Graff dns_adbaddrinfo_t *addrinfo;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff int bucket;
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff handle->query_pending = ISC_FALSE;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff bucket = DNS_ADB_INVALIDBUCKET;
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff namehook = ISC_LIST_HEAD(name->namehooks);
439c0011e642fb1d26011116144af698125262dbMichael Graff while (namehook != NULL) {
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff bucket = namehook->entry->lock_bucket;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff LOCK(&adb->entrylocks[bucket]);
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff if (entry_is_bad_for_zone(adb, namehook->entry, zone, now))
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff goto next;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff addrinfo = new_adbaddrinfo(adb, namehook->entry);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff if (addrinfo == NULL) {
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff handle->partial_result = ISC_TRUE;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff handle->result = ISC_R_NOMEMORY;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff goto out;
439c0011e642fb1d26011116144af698125262dbMichael Graff }
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff /*
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * Found a valid entry. Add it to the handle's list.
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff */
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff inc_entry_refcnt(adb, namehook->entry, ISC_FALSE);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff ISC_LIST_APPEND(handle->list, addrinfo, link);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff addrinfo = NULL;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff next:
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff UNLOCK(&adb->entrylocks[bucket]);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff bucket = DNS_ADB_INVALIDBUCKET;
439c0011e642fb1d26011116144af698125262dbMichael Graff namehook = ISC_LIST_NEXT(namehook, link);
439c0011e642fb1d26011116144af698125262dbMichael Graff }
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff handle->result = ISC_R_SUCCESS; /* all were copied */
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff out:
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff if (bucket != DNS_ADB_INVALIDBUCKET)
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff UNLOCK(&adb->entrylocks[bucket]);
439c0011e642fb1d26011116144af698125262dbMichael Graff}
439c0011e642fb1d26011116144af698125262dbMichael Graff
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graffstatic void
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graffdestroy(dns_adb_t *adb)
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff{
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff adb->magic = 0;
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff isc_mempool_destroy(&adb->nmp);
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff isc_mempool_destroy(&adb->nhmp);
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff isc_mempool_destroy(&adb->zimp);
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff isc_mempool_destroy(&adb->emp);
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff isc_mempool_destroy(&adb->ahmp);
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff isc_mempool_destroy(&adb->aimp);
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff isc_mempool_destroy(&adb->afmp);
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff isc_mutexblock_destroy(adb->entrylocks, DNS_ADBENTRYLIST_LENGTH);
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff isc_mutexblock_destroy(adb->namelocks, DNS_ADBNAMELIST_LENGTH);
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff isc_mutex_destroy(&adb->lock);
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff isc_mutex_destroy(&adb->mplock);
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff isc_random_invalidate(&adb->rand);
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff isc_mem_put(adb->mctx, adb, sizeof (dns_adb_t));
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff}
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff/*
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff * Public functions.
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff */
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graffisc_result_t
fbcf7bc351f2d42d338cb1fa29e59babd9bee94eMichael Graffdns_adb_create(isc_mem_t *mem, dns_view_t *view, isc_timermgr_t *timermgr,
11efdeb076d65fa9f0c5fc067dc040e7c99dfba6Michael Graff isc_taskmgr_t *taskmgr, dns_adb_t **newadb)
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff{
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_adb_t *adb;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff isc_result_t result;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff int i;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff REQUIRE(mem != NULL);
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff REQUIRE(newadb != NULL && *newadb == NULL);
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff adb = isc_mem_get(mem, sizeof (dns_adb_t));
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (adb == NULL)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (ISC_R_NOMEMORY);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff /*
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * Initialize things here that cannot fail, and especially things
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * that must be NULL for the error return to work properly.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff */
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff adb->magic = 0;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff adb->erefcnt = 1;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff adb->irefcnt = 0;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff adb->nmp = NULL;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff adb->nhmp = NULL;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff adb->zimp = NULL;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff adb->emp = NULL;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff adb->ahmp = NULL;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff adb->aimp = NULL;
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff adb->afmp = NULL;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff result = isc_random_init(&adb->rand);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (result != ISC_R_SUCCESS)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff goto fail0a;
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff result = isc_mutex_init(&adb->lock);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (result != ISC_R_SUCCESS)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff goto fail0b;
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff result = isc_mutex_init(&adb->mplock);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff if (result != ISC_R_SUCCESS)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff goto fail0c;
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff result = isc_condition_init(&adb->shutdown_cond);
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff if (result != ISC_R_SUCCESS)
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff goto fail0d;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff /*
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff * Initialize the bucket locks for names and elements.
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff * May as well initialize the list heads, too.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff */
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff result = isc_mutexblock_init(adb->namelocks, DNS_ADBNAMELIST_LENGTH);
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff if (result != ISC_R_SUCCESS)
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff goto fail1;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff for (i = 0 ; i < DNS_ADBNAMELIST_LENGTH ; i++) {
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_LIST_INIT(adb->names[i]);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff adb->name_sd[i] = ISC_FALSE;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff adb->name_refcnt[i] = 0;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff adb->irefcnt++;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff }
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff for (i = 0 ; i < DNS_ADBENTRYLIST_LENGTH ; i++)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_LIST_INIT(adb->entries[i]);
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff result = isc_mutexblock_init(adb->entrylocks, DNS_ADBENTRYLIST_LENGTH);
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff if (result != ISC_R_SUCCESS)
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff goto fail2;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff /*
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * Memory pools
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff */
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff#define MPINIT(t, p, l, n) do { \
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff result = isc_mempool_create(mem, sizeof (t), &(p)); \
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (result != ISC_R_SUCCESS) \
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff goto fail3; \
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff isc_mempool_setfreemax((p), FREE_ITEMS); \
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff isc_mempool_setfillcount((p), FILL_COUNT); \
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff isc_mempool_setname((p), n); \
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff if (l) \
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff isc_mempool_associatelock((p), &adb->mplock); \
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff} while (0)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff MPINIT(dns_adbname_t, adb->nmp, ISC_TRUE, "adbname");
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff MPINIT(dns_adbnamehook_t, adb->nhmp, ISC_TRUE, "adbnamehook");
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff MPINIT(dns_adbzoneinfo_t, adb->zimp, ISC_TRUE, "adbzoneinfo");
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff MPINIT(dns_adbentry_t, adb->emp, ISC_TRUE, "adbentry");
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff MPINIT(dns_adbhandle_t, adb->ahmp, ISC_TRUE, "adbhandle");
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff MPINIT(dns_adbaddrinfo_t, adb->aimp, ISC_TRUE, "adbaddrinfo");
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff MPINIT(dns_adbfetch_t, adb->afmp, ISC_TRUE, "adbfetch");
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff#undef MPINIT
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff /*
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * Normal return.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff */
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff adb->mctx = mem;
439c0011e642fb1d26011116144af698125262dbMichael Graff adb->view = view;
11efdeb076d65fa9f0c5fc067dc040e7c99dfba6Michael Graff adb->timermgr = timermgr;
11efdeb076d65fa9f0c5fc067dc040e7c99dfba6Michael Graff adb->taskmgr = taskmgr;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff adb->magic = DNS_ADB_MAGIC;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff *newadb = adb;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (ISC_R_SUCCESS);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff fail3: /* clean up entrylocks */
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff isc_mutexblock_destroy(adb->entrylocks, DNS_ADBENTRYLIST_LENGTH);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff fail2: /* clean up namelocks */
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff isc_mutexblock_destroy(adb->namelocks, DNS_ADBNAMELIST_LENGTH);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff fail1: /* clean up only allocated memory */
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (adb->nmp != NULL)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff isc_mempool_destroy(&adb->nmp);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (adb->nhmp != NULL)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff isc_mempool_destroy(&adb->nhmp);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (adb->zimp != NULL)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff isc_mempool_destroy(&adb->zimp);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (adb->emp != NULL)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff isc_mempool_destroy(&adb->emp);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (adb->ahmp != NULL)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff isc_mempool_destroy(&adb->ahmp);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (adb->aimp != NULL)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff isc_mempool_destroy(&adb->aimp);
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff if (adb->aimp != NULL)
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff isc_mempool_destroy(&adb->afmp);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff isc_condition_destroy(&adb->shutdown_cond);
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff fail0d:
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff isc_mutex_destroy(&adb->mplock);
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff fail0c:
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff isc_mutex_destroy(&adb->lock);
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff fail0b:
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff isc_random_invalidate(&adb->rand);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff fail0a:
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff isc_mem_put(mem, adb, sizeof (dns_adb_t));
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (result);
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff}
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graffvoid
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graffdns_adb_detach(dns_adb_t **adbx)
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff{
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_adb_t *adb;
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff isc_boolean_t kill;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff REQUIRE(adbx != NULL && DNS_ADB_VALID(*adbx));
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff adb = *adbx;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff *adbx = NULL;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff LOCK(&adb->lock);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff dec_adb_erefcnt(adb, ISC_FALSE);
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff if (adb->erefcnt == 0)
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff shutdown_names(adb, ISC_TRUE);
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff kill = check_exit(adb);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff UNLOCK(&adb->lock);
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff if (kill)
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff destroy(adb);
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff}
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graffisc_result_t
439c0011e642fb1d26011116144af698125262dbMichael Graffdns_adb_lookup(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
439c0011e642fb1d26011116144af698125262dbMichael Graff void *arg, dns_name_t *name, dns_name_t *zone,
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff isc_stdtime_t now, dns_adbhandle_t **handlep)
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff{
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff dns_adbhandle_t *handle;
439c0011e642fb1d26011116144af698125262dbMichael Graff dns_adbname_t *adbname;
439c0011e642fb1d26011116144af698125262dbMichael Graff int bucket;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff isc_result_t result;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff isc_boolean_t attach_to_task;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff REQUIRE(DNS_ADB_VALID(adb));
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff if (task != NULL) {
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff REQUIRE(action != NULL);
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff }
439c0011e642fb1d26011116144af698125262dbMichael Graff REQUIRE(name != NULL);
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff REQUIRE(zone != NULL);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff REQUIRE(handlep != NULL && *handlep == NULL);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff attach_to_task = ISC_FALSE;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff result = ISC_R_UNEXPECTED;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff if (now == 0) {
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff result = isc_stdtime_get(&now);
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff if (result != ISC_R_SUCCESS)
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff return (result);
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff }
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff /*
439c0011e642fb1d26011116144af698125262dbMichael Graff * Look up the name in our internal database.
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff *
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff * Possibilities: Note that these are not always exclusive.
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff *
439c0011e642fb1d26011116144af698125262dbMichael Graff * No name found. In this case, allocate a new name header and
439c0011e642fb1d26011116144af698125262dbMichael Graff * an initial namehook or two. If any of these allocations
439c0011e642fb1d26011116144af698125262dbMichael Graff * fail, clean up and return ISC_R_NOMEMORY.
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff *
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff * Name found, valid addresses present. Allocate one addrinfo
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff * structure for each found and append it to the linked list
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff * of addresses for this header.
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff *
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff * Name found, queries pending. In this case, if a task was
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff * passed in, allocate a job id, attach it to the name's job
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff * list and remember to tell the caller that there will be
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff * more info coming later.
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff */
3c5148c4d98af51d6dcb449c6dbd45fe8c645f61Michael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff handle = new_adbhandle(adb);
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff if (handle == NULL) {
439c0011e642fb1d26011116144af698125262dbMichael Graff result = ISC_R_NOMEMORY;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff goto out;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff }
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff /*
439c0011e642fb1d26011116144af698125262dbMichael Graff * Look things up in our database first.
439c0011e642fb1d26011116144af698125262dbMichael Graff */
439c0011e642fb1d26011116144af698125262dbMichael Graff bucket = DNS_ADB_INVALIDBUCKET;
439c0011e642fb1d26011116144af698125262dbMichael Graff adbname = find_name_and_lock(adb, name, &bucket);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff if (adb->name_sd[bucket]) {
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff result = ISC_R_SHUTTINGDOWN;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff goto fail;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff }
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff /*
439c0011e642fb1d26011116144af698125262dbMichael Graff * Found! Run through the name and copy out the bits we are
439c0011e642fb1d26011116144af698125262dbMichael Graff * interested in. If we cannot copy at least one address, return
439c0011e642fb1d26011116144af698125262dbMichael Graff * ISC_R_NOMEMORY, otherwise copy out what we can and set the
439c0011e642fb1d26011116144af698125262dbMichael Graff * missing_data bit in the header.
439c0011e642fb1d26011116144af698125262dbMichael Graff */
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff again:
439c0011e642fb1d26011116144af698125262dbMichael Graff if (adbname != NULL) {
f181f94ec8da8b1dbcc6353e8be965ea4a5ea282Michael Graff copy_namehook_list(adb, handle, adbname, zone, now);
439c0011e642fb1d26011116144af698125262dbMichael Graff if (handle->result == ISC_R_NOMEMORY
439c0011e642fb1d26011116144af698125262dbMichael Graff && ISC_LIST_EMPTY(handle->list)) {
439c0011e642fb1d26011116144af698125262dbMichael Graff result = ISC_R_NOMEMORY;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff goto fail;
439c0011e642fb1d26011116144af698125262dbMichael Graff }
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff /*
439c0011e642fb1d26011116144af698125262dbMichael Graff * Attach to the name's query list if there are queries
439c0011e642fb1d26011116144af698125262dbMichael Graff * already running.
439c0011e642fb1d26011116144af698125262dbMichael Graff */
bb143613cf26e0f27dfd9caf1a7336065d064b26Michael Graff if (!ISC_LIST_EMPTY(adbname->fetches) && (task != NULL)) {
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff handle->adbname = adbname;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff handle->name_bucket = bucket;
439c0011e642fb1d26011116144af698125262dbMichael Graff ISC_LIST_APPEND(adbname->handles, handle, link);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff attach_to_task = ISC_TRUE;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff }
dec90b6caef758fe2890ee50db148cff6acffb90Michael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff result = ISC_R_SUCCESS;
439c0011e642fb1d26011116144af698125262dbMichael Graff goto out;
439c0011e642fb1d26011116144af698125262dbMichael Graff }
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff /*
439c0011e642fb1d26011116144af698125262dbMichael Graff * Nothing found. Allocate a new adbname structure for this name
439c0011e642fb1d26011116144af698125262dbMichael Graff * and look in the database for details. If the database has
439c0011e642fb1d26011116144af698125262dbMichael Graff * nothing useful, start a fetch if we can.
439c0011e642fb1d26011116144af698125262dbMichael Graff */
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff adbname = new_adbname(adb, name);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff if (adbname == NULL) {
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff result = ISC_R_NOMEMORY;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff goto fail;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff }
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff /*
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * Try to populate the name from the database and/or start fetches.
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * If this function returns ISC_R_SUCCESS at least ONE new bit
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * of data was added, and/or fetches were started. If nothing new
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * can ever be found it will return DNS_R_NOMEMORY more than likely.
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff */
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff result = construct_name(adb, handle, name, zone, adbname, bucket, now);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff if (result == ISC_R_SUCCESS) {
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff ISC_LIST_PREPEND(adb->names[bucket], adbname, link);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff adb->name_refcnt[bucket]++;
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff if (adbname->partial_result)
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff handle->partial_result = ISC_TRUE;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff goto again;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff }
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff /*
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * If anything other than success is returned, free the name
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * (since it will have nothing useful in it) and return via the
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * failure return.
439c0011e642fb1d26011116144af698125262dbMichael Graff */
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff fail:
439c0011e642fb1d26011116144af698125262dbMichael Graff free_adbhandle(adb, &handle);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff /*
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * If the name isn't on a list it means we allocated it here, and it
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * should be killed.
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff */
dec90b6caef758fe2890ee50db148cff6acffb90Michael Graff if (adbname != NULL) {
dec90b6caef758fe2890ee50db148cff6acffb90Michael Graff INSIST(!ISC_LINK_LINKED(adbname, link));
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff free_adbname(adb, &adbname);
dec90b6caef758fe2890ee50db148cff6acffb90Michael Graff }
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff /*
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * "goto out" if the handle will be returned to the caller. This
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * is a non-fatal return, since it will give the caller a handle
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * at the very least.
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff */
439c0011e642fb1d26011116144af698125262dbMichael Graff out:
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff if (handle != NULL) {
439c0011e642fb1d26011116144af698125262dbMichael Graff *handlep = handle;
439c0011e642fb1d26011116144af698125262dbMichael Graff
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff if (attach_to_task) {
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff isc_task_t *taskp;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff taskp = NULL;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff isc_task_attach(task, &taskp);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff handle->event.sender = taskp;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff handle->event.action = action;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff handle->event.arg = arg;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff }
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff }
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff if (bucket != DNS_ADB_INVALIDBUCKET)
439c0011e642fb1d26011116144af698125262dbMichael Graff UNLOCK(&adb->namelocks[bucket]);
439c0011e642fb1d26011116144af698125262dbMichael Graff
439c0011e642fb1d26011116144af698125262dbMichael Graff return (result);
3c5148c4d98af51d6dcb449c6dbd45fe8c645f61Michael Graff}
3c5148c4d98af51d6dcb449c6dbd45fe8c645f61Michael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graffisc_result_t
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graffdns_adb_deletename(dns_adb_t *adb, dns_name_t *host)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff{
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff int name_bucket;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff dns_adbname_t *name;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff isc_boolean_t decr_adbrefcnt;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff REQUIRE(DNS_ADB_VALID(adb));
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff REQUIRE(host != NULL);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff name = NULL;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff /*
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * Find the name.
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff */
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff name_bucket = DNS_ADB_INVALIDBUCKET;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff name = find_name_and_lock(adb, host, &name_bucket);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff if (name == NULL) {
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff UNLOCK(&adb->namelocks[name_bucket]);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff return (ISC_R_NOTFOUND);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff }
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff /*
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff * If any handles are attached to this name, notify them that things
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * are going away by canceling their requests.
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff */
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff clean_handles_at_name(name, DNS_EVENT_ADBNAMEDELETED);
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff /* XXX TODO:
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff * If fetches are running for this name, cancel them all.
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff */
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff /*
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * Loop through the name and kill any namehooks and entries they
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * point to.
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff */
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff clean_namehooks_at_name(adb, name);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff ISC_LIST_UNLINK(adb->names[name_bucket], name, link);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff free_adbname(adb, &name);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff INSIST(adb->name_refcnt[name_bucket] > 0);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff adb->name_refcnt[name_bucket]--;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff decr_adbrefcnt = ISC_FALSE;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff if (adb->name_sd[name_bucket] && (adb->name_refcnt[name_bucket] == 0))
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff decr_adbrefcnt = ISC_TRUE;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff if (name_bucket != DNS_ADB_INVALIDBUCKET)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff UNLOCK(&adb->namelocks[name_bucket]);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff if (decr_adbrefcnt)
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff dec_adb_irefcnt(adb, ISC_TRUE);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff return (DNS_R_SUCCESS);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff}
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graffisc_result_t
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graffdns_adb_insert(dns_adb_t *adb, dns_name_t *host, isc_sockaddr_t *addr)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff{
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_adbname_t *name;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff isc_boolean_t free_name;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_adbentry_t *entry;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff isc_boolean_t free_entry;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_adbnamehook_t *namehook;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff isc_boolean_t free_namehook;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff int name_bucket, addr_bucket; /* unlock if != DNS_ADB_INVALIDBUCKET */
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff isc_result_t result;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff REQUIRE(DNS_ADB_VALID(adb));
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff REQUIRE(host != NULL);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff REQUIRE(addr != NULL);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff name = NULL;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff free_name = ISC_FALSE;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff entry = NULL;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff free_entry = ISC_FALSE;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff namehook = NULL;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff free_namehook = ISC_FALSE;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff result = ISC_R_UNEXPECTED;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff /*
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * First, see if the host is already in the database. If it is,
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * don't make a new host entry. If not, copy the name and name's
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * contents into our structure and allocate what we'll need
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * to attach things together.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff */
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff name_bucket = DNS_ADB_INVALIDBUCKET;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff name = find_name_and_lock(adb, host, &name_bucket);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (name == NULL) {
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff name = new_adbname(adb, host);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (name == NULL) {
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff result = ISC_R_NOMEMORY;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff goto out;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff }
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff free_name = ISC_TRUE;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff }
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff /*
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * Now, while keeping the name locked, search for the address.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * Three possibilities: One, the address doesn't exist.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * Two, the address exists, but we aren't linked to it.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * Three, the address exists and we are linked to it.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * (1) causes a new entry and namehook to be created.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * (2) causes only a new namehook.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * (3) is an error.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff */
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff addr_bucket = DNS_ADB_INVALIDBUCKET;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff entry = find_entry_and_lock(adb, addr, &addr_bucket);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff /*
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * Case (1): new entry and namehook.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff */
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (entry == NULL) {
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff entry = new_adbentry(adb);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (entry == NULL) {
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff result = ISC_R_NOMEMORY;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff goto out;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff }
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff free_entry = ISC_TRUE;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff }
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff /*
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * Case (3): entry exists, we're linked.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff */
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff namehook = ISC_LIST_HEAD(name->namehooks);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff while (namehook != NULL) {
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (namehook->entry == entry) {
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff result = ISC_R_EXISTS;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff goto out;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff }
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff namehook = ISC_LIST_NEXT(namehook, link);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff }
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff /*
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * Case (2): New namehook, link to entry from above.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff */
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff namehook = new_adbnamehook(adb, entry);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (namehook == NULL) {
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff result = ISC_R_NOMEMORY;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff goto out;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff }
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff free_namehook = ISC_TRUE;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_LIST_APPEND(name->namehooks, namehook, link);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff entry->lock_bucket = addr_bucket;
439c0011e642fb1d26011116144af698125262dbMichael Graff inc_entry_refcnt(adb, entry, ISC_FALSE);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff entry->sockaddr = *addr;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff /*
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff * If needed, string up the name and entry.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff */
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff if (!ISC_LINK_LINKED(name, link)) {
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_LIST_PREPEND(adb->names[name_bucket], name, link);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff adb->name_refcnt[name_bucket]++;
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff }
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (!ISC_LINK_LINKED(entry, link))
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_LIST_PREPEND(adb->entries[addr_bucket], entry, link);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff UNLOCK(&adb->namelocks[name_bucket]);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff name_bucket = DNS_ADB_INVALIDBUCKET;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff UNLOCK(&adb->entrylocks[addr_bucket]);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff addr_bucket = DNS_ADB_INVALIDBUCKET;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (ISC_R_SUCCESS);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff out:
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (free_name)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff free_adbname(adb, &name);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (free_entry)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff isc_mempool_put(adb->emp, entry);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (free_namehook)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff isc_mempool_put(adb->nhmp, namehook);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff if (name_bucket != DNS_ADB_INVALIDBUCKET)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff UNLOCK(&adb->namelocks[name_bucket]);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff if (addr_bucket != DNS_ADB_INVALIDBUCKET)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff UNLOCK(&adb->entrylocks[addr_bucket]);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (result);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff}
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graffvoid
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graffdns_adb_done(dns_adbhandle_t **handlep)
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff{
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff dns_adbhandle_t *handle;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff dns_adbentry_t *entry;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff dns_adbaddrinfo_t *ai;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff int bucket;
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff dns_adb_t *adb;
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff isc_boolean_t kill;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff REQUIRE(handlep != NULL && DNS_ADBHANDLE_VALID(*handlep));
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff handle = *handlep;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff *handlep = NULL;
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff LOCK(&handle->lock);
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff adb = handle->adb;
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff REQUIRE(DNS_ADB_VALID(adb));
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff REQUIRE(!ISC_LINK_LINKED(handle, next));
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff bucket = handle->name_bucket;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff if (bucket == DNS_ADB_INVALIDBUCKET)
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff goto cleanup;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff /*
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * We need to get the adbname's lock to unlink the handle.
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff */
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff violate_locking_hierarchy(&handle->lock, &adb->namelocks[bucket]);
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff bucket = handle->name_bucket;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff if (bucket != DNS_ADB_INVALIDBUCKET) {
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff ISC_LIST_UNLINK(handle->adbname->handles, handle, link);
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff handle->adbname = NULL;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff handle->name_bucket = DNS_ADB_INVALIDBUCKET;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff }
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff UNLOCK(&adb->namelocks[bucket]);
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff bucket = DNS_ADB_INVALIDBUCKET;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff cleanup:
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff UNLOCK(&handle->lock);
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff /*
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff * The handle doesn't exist on any list, and nothing is locked.
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff * Return the handle to the memory pool, and decrement the adb's
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff * reference count.
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff */
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff ai = ISC_LIST_HEAD(handle->list);
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff while (ai != NULL) {
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff ISC_LIST_UNLINK(handle->list, ai, link);
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff entry = ai->entry;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff ai->entry = NULL;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff ai->sockaddr = NULL;
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff INSIST(DNS_ADBENTRY_VALID(entry));
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff dec_entry_refcnt(adb, entry, ISC_TRUE);
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff free_adbaddrinfo(adb, &ai);
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff ai = ISC_LIST_HEAD(handle->list);
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff }
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff LOCK(&adb->lock);
6d14fe95e9ea5bbc5e863e5aab4618f7b3dbcc0fMichael Graff free_adbhandle(adb, &handle);
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff kill = check_exit(adb);
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff UNLOCK(&adb->lock);
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff if (kill)
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff destroy(adb);
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff}
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graffvoid
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graffdns_adb_dump(dns_adb_t *adb, FILE *f)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff{
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff int i;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff isc_sockaddr_t *sa;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff dns_adbname_t *name;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff dns_adbentry_t *entry;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff char tmp[512];
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff const char *tmpp;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff REQUIRE(DNS_ADB_VALID(adb));
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff REQUIRE(f != NULL);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff /*
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * Lock the adb itself, lock all the name buckets, then lock all
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * the entry buckets. This should put the adb into a state where
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * nothing can change, so we can iterate through everything and
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * print at our leasure.
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff */
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff LOCK(&adb->lock);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff fprintf(f, "ADB %p DUMP:\n", adb);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff fprintf(f, "erefcnt %u, irefcnt %u\n", adb->erefcnt, adb->irefcnt);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff for (i = 0 ; i < DNS_ADBNAMELIST_LENGTH ; i++)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff LOCK(&adb->namelocks[i]);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff for (i = 0 ; i < DNS_ADBENTRYLIST_LENGTH ; i++)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff LOCK(&adb->entrylocks[i]);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff /*
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * Dump the names
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff */
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff fprintf(f, "Names:\n");
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff for (i = 0 ; i < DNS_ADBNAMELIST_LENGTH ; i++) {
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff name = ISC_LIST_HEAD(adb->names[i]);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff if (name == NULL)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff continue;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff fprintf(f, "Name bucket %d:\n", i);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff while (name != NULL) {
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff fprintf(f, "name %p\n", name);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff if (!DNS_ADBNAME_VALID(name))
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff fprintf(f, "\tMAGIC %08x\n", name->magic);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff fprintf(f, "\t");
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff print_dns_name(f, &name->name);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff fprintf(f, "\n");
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff print_namehook_list(f, name);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff print_handle_list(f, name);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff fprintf(f, "\n");
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff name = ISC_LIST_NEXT(name, link);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff }
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff }
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff /*
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * Dump the entries
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff */
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff fprintf(f, "Entries:\n");
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff for (i = 0 ; i < DNS_ADBENTRYLIST_LENGTH ; i++) {
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff entry = ISC_LIST_HEAD(adb->entries[i]);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff if (entry == NULL)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff continue;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff fprintf(f, "Entry bucket %d:\n", i);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff while (entry != NULL) {
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff if (!DNS_ADBENTRY_VALID(entry))
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff fprintf(f, "\tMAGIC %08x\n", entry->magic);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff if (entry->lock_bucket != i)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff fprintf(f, "\tWRONG BUCKET! lock_bucket %d\n",
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff entry->lock_bucket);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff sa = &entry->sockaddr;
a9ece9973c35d4d780338e89e288fb6a59575324Michael Graff switch (sa->type.sa.sa_family) {
a9ece9973c35d4d780338e89e288fb6a59575324Michael Graff case AF_INET:
a9ece9973c35d4d780338e89e288fb6a59575324Michael Graff tmpp = inet_ntop(AF_INET,
a9ece9973c35d4d780338e89e288fb6a59575324Michael Graff &sa->type.sin.sin_addr,
a9ece9973c35d4d780338e89e288fb6a59575324Michael Graff tmp, sizeof tmp);
a9ece9973c35d4d780338e89e288fb6a59575324Michael Graff break;
a9ece9973c35d4d780338e89e288fb6a59575324Michael Graff case AF_INET6:
a9ece9973c35d4d780338e89e288fb6a59575324Michael Graff tmpp = inet_ntop(AF_INET6,
a9ece9973c35d4d780338e89e288fb6a59575324Michael Graff &sa->type.sin6.sin6_addr,
a9ece9973c35d4d780338e89e288fb6a59575324Michael Graff tmp, sizeof tmp);
a9ece9973c35d4d780338e89e288fb6a59575324Michael Graff break;
a9ece9973c35d4d780338e89e288fb6a59575324Michael Graff default:
a9ece9973c35d4d780338e89e288fb6a59575324Michael Graff tmpp = "UnkFamily";
a9ece9973c35d4d780338e89e288fb6a59575324Michael Graff }
a9ece9973c35d4d780338e89e288fb6a59575324Michael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff if (tmpp == NULL)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff tmpp = "CANNOT TRANSLATE ADDRESS!";
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff fprintf(f, "\t%p: refcnt %u flags %08x goodness %d"
a9ece9973c35d4d780338e89e288fb6a59575324Michael Graff " srtt %u addr %s\n",
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff entry, entry->refcnt, entry->flags,
a385f150bb21b8b81f70ed7df545357a83f1da82Michael Graff entry->goodness, entry->srtt, tmpp);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff entry = ISC_LIST_NEXT(entry, link);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff }
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff }
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff /*
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * Unlock everything
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff */
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff for (i = 0 ; i < DNS_ADBENTRYLIST_LENGTH ; i++)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff UNLOCK(&adb->entrylocks[i]);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff for (i = 0 ; i < DNS_ADBNAMELIST_LENGTH ; i++)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff UNLOCK(&adb->namelocks[i]);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff UNLOCK(&adb->lock);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff}
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graffvoid
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graffdns_adb_dumphandle(dns_adb_t *adb, dns_adbhandle_t *handle, FILE *f)
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff{
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff char tmp[512];
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff const char *tmpp;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff dns_adbaddrinfo_t *ai;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff isc_sockaddr_t *sa;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff /*
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff * Not used currently, in the API Just In Case we
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff * want to dump out the name and/or entries too.
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff */
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff (void)adb;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff LOCK(&handle->lock);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff fprintf(f, "Handle %p\n", handle);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff fprintf(f, "\tquery_pending %d, result %d (%s)\n",
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff handle->query_pending, handle->result,
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff isc_result_totext(handle->result));
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff fprintf(f, "\tname_bucket %d, name %p, event sender %p\n",
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff handle->name_bucket, handle->adbname, handle->event.sender);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff ai = ISC_LIST_HEAD(handle->list);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff if (ai != NULL)
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff fprintf(f, "\tAddresses:\n");
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff while (ai != NULL) {
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff sa = ai->sockaddr;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff switch (sa->type.sa.sa_family) {
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff case AF_INET:
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff tmpp = inet_ntop(AF_INET, &sa->type.sin.sin_addr,
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff tmp, sizeof tmp);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff break;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff case AF_INET6:
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff tmpp = inet_ntop(AF_INET6, &sa->type.sin6.sin6_addr,
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff tmp, sizeof tmp);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff break;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff default:
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff tmpp = "UnkFamily";
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff }
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff if (tmpp == NULL)
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff tmpp = "CANNOT TRANSLATE ADDRESS!";
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff fprintf(f, "\t\tentry %p, flags %08x goodness %d"
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff " srtt %u addr %s\n",
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff ai->entry, ai->flags, ai->goodness, ai->srtt, tmpp);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff ai = ISC_LIST_NEXT(ai, link);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff }
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff UNLOCK(&handle->lock);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff}
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graffstatic void
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graffprint_dns_name(FILE *f, dns_name_t *name)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff{
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff char buf[257];
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff isc_buffer_t b;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff INSIST(f != NULL);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff memset(buf, 0, sizeof (buf));
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff isc_buffer_init(&b, buf, sizeof (buf) - 1, ISC_BUFFERTYPE_TEXT);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff dns_name_totext(name, ISC_FALSE, &b);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff fprintf(f, buf); /* safe, since names < 256 chars, and we memset */
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff}
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graffstatic void
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graffprint_namehook_list(FILE *f, dns_adbname_t *n)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff{
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff dns_adbnamehook_t *nh;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff nh = ISC_LIST_HEAD(n->namehooks);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff while (nh != NULL) {
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff fprintf(f, "\t\tHook %p -> entry %p\n", nh, nh->entry);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff nh = ISC_LIST_NEXT(nh, link);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff }
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff}
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graffstatic void
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graffprint_handle_list(FILE *f, dns_adbname_t *name)
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff{
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff dns_adbhandle_t *handle;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff handle = ISC_LIST_HEAD(name->handles);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff while (handle != NULL) {
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff fprintf(f, "\t\tHandle %p\n", handle);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff handle = ISC_LIST_NEXT(handle, link);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff }
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff}
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff/*
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * On entry, "bucket" refers to a locked name bucket, "handle" is not NULL,
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * and "name" is the name we are looking for. We will allocate an adbname
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * and return a pointer to it in *adbnamep.
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff *
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * If we return ISC_R_SUCCESS, the new name will have been allocated, and
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * perhaps some namehooks will have been filled in with valid entries, and
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff * perhaps some fetches have been started.
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff */
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graffstatic isc_result_t
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graffconstruct_name(dns_adb_t *adb, dns_adbhandle_t *handle, dns_name_t *name,
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff dns_name_t *zone, dns_adbname_t *adbname, int bucket,
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff isc_stdtime_t now)
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff{
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff dns_adbnamehook_t *nh;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff isc_result_t result;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff int addr_bucket;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff isc_boolean_t return_success;
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff isc_boolean_t use_hints;
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff dns_rdataset_t rdataset;
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff dns_rdata_t rdata;
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff struct in_addr ina;
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff isc_sockaddr_t sockaddr;
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff dns_adbentry_t *foundentry; /* NO CLEAN UP! */
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff INSIST(DNS_ADB_VALID(adb));
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff INSIST(DNS_ADBHANDLE_VALID(handle));
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff INSIST(name != NULL);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff INSIST(DNS_ADBNAME_VALID(adbname));
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff INSIST(bucket != DNS_ADB_INVALIDBUCKET);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff if (adb->view == NULL)
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff return (ISC_R_NOTIMPLEMENTED);
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff result = ISC_R_UNEXPECTED;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff addr_bucket = DNS_ADB_INVALIDBUCKET;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff return_success = ISC_FALSE;
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff nh = NULL;
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff use_hints = dns_name_equal(zone, dns_rootname);
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff dns_rdataset_init(&rdataset);
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff adbname->partial_result = ISC_FALSE;
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff result = dns_view_find(adb->view, name, dns_rdatatype_a,
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff now, DNS_DBFIND_GLUEOK, use_hints,
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff &rdataset, NULL);
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff switch (result) {
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff case DNS_R_GLUE:
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff case DNS_R_HINT:
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff case DNS_R_SUCCESS:
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff result = dns_rdataset_first(&rdataset);
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff while (result == ISC_R_SUCCESS) {
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff nh = new_adbnamehook(adb, NULL);
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff if (nh == NULL) {
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff adbname->partial_result = ISC_TRUE;
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff result = ISC_R_NOMEMORY;
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff goto fail;
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff }
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff dns_rdataset_current(&rdataset, &rdata);
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff INSIST(rdata.length == 4);
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff memcpy(&ina.s_addr, rdata.data, 4);
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff isc_sockaddr_fromin(&sockaddr, &ina, 53);
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff foundentry = find_entry_and_lock(adb, &sockaddr,
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff &addr_bucket);
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff if (foundentry == NULL) {
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff dns_adbentry_t *entry;
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff entry = new_adbentry(adb);
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff if (entry == NULL) {
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff adbname->partial_result = ISC_TRUE;
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff result = ISC_R_NOMEMORY;
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff goto fail;
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff }
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff entry->sockaddr = sockaddr;
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff entry->refcnt = 1;
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff entry->lock_bucket = addr_bucket;
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff nh->entry = entry;
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff ISC_LIST_APPEND(adb->entries[addr_bucket],
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff entry, link);
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff } else {
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff foundentry->refcnt++;
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff nh->entry = foundentry;
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff }
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff ISC_LIST_APPEND(adbname->namehooks, nh, link);
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff nh = NULL;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff return_success = ISC_TRUE;
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff result = dns_rdataset_next(&rdataset);
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff }
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff }
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff fail:
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff if (dns_rdataset_isassociated(&rdataset))
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff dns_rdataset_disassociate(&rdataset);
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff if (nh != NULL)
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff free_adbnamehook(adb, &nh);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff if (addr_bucket != DNS_ADB_INVALIDBUCKET)
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff UNLOCK(&adb->entrylocks[addr_bucket]);
4d727d86b245063f00c5bfd98d138a63e0b7de6dMichael Graff
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff if (return_success)
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff return (ISC_R_SUCCESS);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff return (result);
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff}
d8590892d10fc9528b0dde7e2781935e7b8d7a87Michael Graff
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graffisc_result_t
f00d96a15cdd11e764437f9359e67328631caaeaMichael Graffdns_adb_marklame(dns_adb_t *adb, dns_adbaddrinfo_t *addr, dns_name_t *zone,
f00d96a15cdd11e764437f9359e67328631caaeaMichael Graff isc_stdtime_t expire_time)
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graff{
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graff dns_adbzoneinfo_t *zi;
f00d96a15cdd11e764437f9359e67328631caaeaMichael Graff int bucket;
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graff
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graff REQUIRE(DNS_ADB_VALID(adb));
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graff REQUIRE(DNS_ADBADDRINFO_VALID(addr));
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graff REQUIRE(zone != NULL);
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graff
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graff zi = new_adbzoneinfo(adb, zone);
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graff if (zi == NULL)
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graff return (ISC_R_NOMEMORY);
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graff
f00d96a15cdd11e764437f9359e67328631caaeaMichael Graff zi->lame_timer = expire_time;
f00d96a15cdd11e764437f9359e67328631caaeaMichael Graff
f00d96a15cdd11e764437f9359e67328631caaeaMichael Graff bucket = addr->entry->lock_bucket;
f00d96a15cdd11e764437f9359e67328631caaeaMichael Graff LOCK(&adb->entrylocks[bucket]);
f00d96a15cdd11e764437f9359e67328631caaeaMichael Graff ISC_LIST_PREPEND(addr->entry->zoneinfo, zi, link);
f00d96a15cdd11e764437f9359e67328631caaeaMichael Graff UNLOCK(&adb->entrylocks[bucket]);
f00d96a15cdd11e764437f9359e67328631caaeaMichael Graff
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graff return (ISC_R_SUCCESS);
f36a81c88493985ee2d1c53cc6fe88f4b00dbbc8Michael Graff}
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff
ca924f706b53b895a662de8345d88437b23b8f01Michael Graffvoid
ca924f706b53b895a662de8345d88437b23b8f01Michael Graffdns_adb_adjustgoodness(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff int goodness_adjustment)
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff{
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff int bucket;
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff int old_goodness, new_goodness;
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff REQUIRE(DNS_ADB_VALID(adb));
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff REQUIRE(DNS_ADBADDRINFO_VALID(addr));
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff if (goodness_adjustment == 0)
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff return;
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff bucket = addr->entry->lock_bucket;
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff LOCK(&adb->entrylocks[bucket]);
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff old_goodness = addr->entry->goodness;
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff if (goodness_adjustment > 0) {
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff if (old_goodness > INT_MAX - goodness_adjustment)
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff new_goodness = INT_MAX;
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff else
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff new_goodness = old_goodness + goodness_adjustment;
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff } else {
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff if (old_goodness < INT_MIN - goodness_adjustment)
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff new_goodness = INT_MAX;
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff else
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff new_goodness = old_goodness + goodness_adjustment;
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff }
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff addr->entry->goodness = new_goodness;
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff addr->goodness = new_goodness;
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff UNLOCK(&adb->entrylocks[bucket]);
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff}
ca924f706b53b895a662de8345d88437b23b8f01Michael Graff
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graffvoid
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graffdns_adb_adjustsrtt(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff unsigned int rtt, unsigned int factor)
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff{
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff int bucket;
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff unsigned int new_srtt;
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff REQUIRE(DNS_ADB_VALID(adb));
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff REQUIRE(DNS_ADBADDRINFO_VALID(addr));
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff if (factor == 0)
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff factor = 4;
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff bucket = addr->entry->lock_bucket;
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff LOCK(&adb->entrylocks[bucket]);
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff new_srtt = (addr->entry->srtt * (factor - 1) + rtt) / factor;
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff addr->entry->srtt = new_srtt;
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff addr->srtt = new_srtt;
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff UNLOCK(&adb->entrylocks[bucket]);
beb2b4f19624af46230a8e3df97e54d6c596573eMichael Graff}