adb.c revision e51511aa3281f8dc384eb1283115c7f8d5c402ae
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>
31fab17bcdbe302592a6c0dc5374ef56333ee879Michael Graff#include <isc/magic.h>
213973a334f92d4aef4ef62b4538fc2e4d0e8082Michael Graff#include <isc/mutex.h>
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff#include <isc/mutexblock.h>
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff#include <isc/event.h>
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff#include <dns/address.h>
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff#include <dns/name.h>
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff#include <dns/rdata.h>
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff#include <dns/rdataset.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)
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff/*
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * Lengths of lists needs to be powers of two.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff */
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff#define DNS_ADBNAMELIST_LENGTH 16 /* how many buckets for names */
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff#define DNS_ADBENTRYLIST_LENGTH 16 /* 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
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Grafftypedef struct dns_adbname dns_adbname_t;
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;
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;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff unsigned int refcnt;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff isc_boolean_t done;
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 */
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael 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];
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];
213973a334f92d4aef4ef62b4538fc2e4d0e8082Michael Graff
213973a334f92d4aef4ef62b4538fc2e4d0e8082Michael Graff /*
213973a334f92d4aef4ef62b4538fc2e4d0e8082Michael Graff * List of running and idle handles.
213973a334f92d4aef4ef62b4538fc2e4d0e8082Michael Graff */
213973a334f92d4aef4ef62b4538fc2e4d0e8082Michael Graff ISC_LIST(dns_adbhandle_t) running_handles;
213973a334f92d4aef4ef62b4538fc2e4d0e8082Michael Graff ISC_LIST(dns_adbhandle_t) idle_handles;
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff};
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graffstruct dns_adbname {
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff unsigned int magic;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_name_t name;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff ISC_LIST(dns_adbnamehook_t) namehooks;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_LINK(dns_adbname_t) link;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff};
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael 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
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff dns_name_t *zone;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff unsigned int 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;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff unsigned int refcount;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff unsigned int flags;
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael 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 */
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline dns_adbname_t *
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffnew_adbname(dns_adb_t *);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline dns_adbnamehook_t *
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffnew_adbnamehook(dns_adb_t *, dns_adbentry_t *);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline dns_adbzoneinfo_t *
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffnew_adbzoneinfo(dns_adb_t *);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline dns_adbentry_t *
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffnew_adbentry(dns_adb_t *);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline dns_adbhandle_t *
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffnew_adbhandle(dns_adb_t *);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline dns_adbaddrinfo_t *
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffnew_adbaddrinfo(dns_adb_t *, dns_adbentry_t *);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline dns_adbname_t *
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Grafffind_name_and_lock(dns_adb_t *, dns_name_t *, int *);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline dns_adbentry_t *
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Grafffind_entry_and_lock(dns_adb_t *, isc_sockaddr_t *, int *);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic void
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffprint_dns_name(FILE *, dns_name_t *);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic void
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffprint_namehook_list(FILE *, dns_adbname_t *);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline void
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffinc_adb_refcnt(dns_adb_t *, isc_boolean_t);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline void
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffdec_adb_refcnt(dns_adb_t *, isc_boolean_t);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline void
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffinc_adb_refcnt(dns_adb_t *adb, isc_boolean_t lock)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff{
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff if (lock)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff LOCK(&adb->lock);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff REQUIRE(!adb->done);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff adb->refcnt++;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff if (lock)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff UNLOCK(&adb->lock);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff}
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline void
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffdec_adb_refcnt(dns_adb_t *adb, isc_boolean_t lock)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff{
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff if (lock)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff LOCK(&adb->lock);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff INSIST(adb->refcnt > 0);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff adb->refcnt--;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff if (adb->done && adb->refcnt == 0)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff SIGNAL(&adb->shutdown_cond);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff if (lock)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff UNLOCK(&adb->lock);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff}
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline dns_adbname_t *
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graffnew_adbname(dns_adb_t *adb)
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 name->magic = DNS_ADBNAME_MAGIC;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_name_init(&name->name, NULL);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_LIST_INIT(name->namehooks);
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));
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 *
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graffnew_adbzoneinfo(dns_adb_t *adb)
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
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff zi->magic = DNS_ADBZONEINFO_MAGIC;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff zi->zone = NULL;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff zi->lame_timer = 0;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_LINK_INIT(zi, link);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff return (zi);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff}
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graffstatic inline dns_adbentry_t *
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graffnew_adbentry(dns_adb_t *adb)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff{
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_adbentry_t *e;
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;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff e->refcount = 0;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff e->flags = 0;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff e->goodness = 0;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff e->srtt = 0;
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;
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);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff INSIST(e->refcount == 0);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff INSIST(ISC_LIST_EMPTY(e->zoneinfo));
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff INSIST(!ISC_LINK_LINKED(e, link));
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff e->magic = 0;
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;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff h->adb = adb;
519b4a1a27c8b767a57a981dda69a3c6394bd49dMichael Graff ISC_LIST_INIT(h->list);
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 h->task = NULL;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff h->taskaction = NULL;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff h->arg = NULL;
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
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
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
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);
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
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
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graffdns_adb_create(isc_mem_t *mem, 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;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff adb->refcnt = 0;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff adb->done = ISC_FALSE;
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;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff result = isc_mutex_init(&adb->lock);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (result != ISC_R_SUCCESS)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff goto fail0a;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff result = isc_mutex_init(&adb->mplock);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (result != ISC_R_SUCCESS)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff goto fail0b;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff result = isc_condition_init(&adb->shutdown_cond);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff if (result != ISC_R_SUCCESS)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff goto fail0c;
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;
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff for (i = 0 ; i < DNS_ADBNAMELIST_LENGTH ; i++)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_LIST_INIT(adb->names[i]);
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 */
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff#define MPINIT(t, p) 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); \
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff} while (0)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff#define MPINIT_LOCKED(t, p) do { \
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff MPINIT(t, p); \
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff isc_mempool_associatelock((p), &adb->mplock); \
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff} while (0)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff MPINIT_LOCKED(dns_adbname_t, adb->nmp);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff MPINIT_LOCKED(dns_adbnamehook_t, adb->nhmp);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff MPINIT_LOCKED(dns_adbzoneinfo_t, adb->zimp);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff MPINIT_LOCKED(dns_adbentry_t, adb->emp);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff MPINIT_LOCKED(dns_adbhandle_t, adb->ahmp);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff MPINIT_LOCKED(dns_adbaddrinfo_t, adb->aimp);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff#undef MPINIT
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff#undef MPINIT_LOCKED
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff /*
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * Normal return.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff */
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff adb->mctx = mem;
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);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff isc_condition_destroy(&adb->shutdown_cond);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff fail0c:
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff isc_mutex_destroy(&adb->mplock);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff fail0b:
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff isc_mutex_destroy(&adb->lock);
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
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graffdns_adb_destroy(dns_adb_t **adbx)
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff{
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff dns_adb_t *adb;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff isc_boolean_t done;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff REQUIRE(adbx != NULL && DNS_ADB_VALID(*adbx));
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff adb = *adbx;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff *adbx = NULL;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff /*
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff * If all lists are empty, destroy the memory used by this
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff * adb.
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff */
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff LOCK(&adb->lock);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff REQUIRE(adb->done != ISC_TRUE);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff adb->done = ISC_TRUE;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff do {
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff done = ISC_TRUE;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff if (adb->refcnt != 0)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff done = ISC_FALSE;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff if (!done)
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff WAIT(&adb->shutdown_cond, &adb->lock);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff } while (!done);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff UNLOCK(&adb->lock);
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
3024dbecbac365171bc6de0f3fa04951d6558be3Michael Graff destroy(adb);
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff}
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graffisc_result_t
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graffdns_adb_lookup(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t *action,
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff void *arg, dns_rdataset_t *nsrdataset, dns_name_t *zone,
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff dns_adbhandle_t **handlep)
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff{
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff dns_adbhandle_t *handle;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff isc_boolean_t use_hints;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff dns_rdata_t rdata;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff isc_region_t r;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff dns_name_t name;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff dns_rdataset_t rdataset;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff isc_sockaddr_t *address;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff struct in_addr ina;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff isc_result_t result;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff REQUIRE(DNS_ADB_VALID(adb));
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff if (task != NULL) {
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff REQUIRE(action != NULL);
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff }
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff REQUIRE(nsrdataset != NULL);
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff REQUIRE(zone != NULL);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff REQUIRE(handlep != NULL && *handlep == NULL);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff REQUIRE(nsrdataset->type == dns_rdatatype_ns);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff /*
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff * Bump our reference count to the adb, so it cannot go away while
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff * we're using it.
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff */
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff LOCK(&adb->lock);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff REQUIRE(!adb->done);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff inc_adb_refcnt(adb, ISC_FALSE);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff UNLOCK(&adb->lock);
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff /*
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff * Iterate through the nsrdataset. For each name found, do a search
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff * for it in our database.
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff *
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff * Possibilities: Note that these are not always exclusive.
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff *
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff * No name found. In this case, allocate a new name header,
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff * an initial namehook or two, and a job id. If any of these
50b5857f1ad137624a18ce67b26b9941e316b007Michael Graff * allocations fail, clean up and simply skip this address.
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);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff if (handle == NULL) {
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff dec_adb_refcnt(adb, ISC_TRUE);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff return (ISC_R_NOMEMORY);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff }
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff use_hints = dns_name_equal(zone, dns_rootname);
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff
3c5148c4d98af51d6dcb449c6dbd45fe8c645f61Michael Graff return (ISC_R_NOTIMPLEMENTED);
3c5148c4d98af51d6dcb449c6dbd45fe8c645f61Michael Graff}
3c5148c4d98af51d6dcb449c6dbd45fe8c645f61Michael Graff
3c5148c4d98af51d6dcb449c6dbd45fe8c645f61Michael Graffisc_result_t
3c5148c4d98af51d6dcb449c6dbd45fe8c645f61Michael Graffdns_adb_refresh(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t *action,
3c5148c4d98af51d6dcb449c6dbd45fe8c645f61Michael Graff void *arg, dns_rdataset_t *nsrdataset, dns_name_t *zone,
3c5148c4d98af51d6dcb449c6dbd45fe8c645f61Michael Graff dns_adbhandle_t *handle)
3c5148c4d98af51d6dcb449c6dbd45fe8c645f61Michael Graff{
3c5148c4d98af51d6dcb449c6dbd45fe8c645f61Michael Graff REQUIRE(DNS_ADB_VALID(adb));
3c5148c4d98af51d6dcb449c6dbd45fe8c645f61Michael Graff if (task != NULL) {
3c5148c4d98af51d6dcb449c6dbd45fe8c645f61Michael Graff REQUIRE(action != NULL);
3c5148c4d98af51d6dcb449c6dbd45fe8c645f61Michael Graff }
3c5148c4d98af51d6dcb449c6dbd45fe8c645f61Michael Graff REQUIRE(nsrdataset != NULL);
3c5148c4d98af51d6dcb449c6dbd45fe8c645f61Michael Graff REQUIRE(zone != NULL);
3c5148c4d98af51d6dcb449c6dbd45fe8c645f61Michael Graff REQUIRE(DNS_ADBHANDLE_VALID(handle));
3c5148c4d98af51d6dcb449c6dbd45fe8c645f61Michael Graff
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff return (ISC_R_NOTIMPLEMENTED);
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff}
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graffisc_result_t
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graffdns_adb_deletename(dns_adb_t *adb, dns_name_t *host)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff{
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff int name_bucket, addr_bucket;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff dns_adbname_t *name;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff dns_adbentry_t *entry;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff dns_adbnamehook_t *namehook;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff REQUIRE(DNS_ADB_VALID(adb));
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff REQUIRE(host != NULL);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff name = NULL;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff entry = NULL;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff namehook = 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
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff /* XXX
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * If any jobs are attached to this name, notify them that things
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * are going away by canceling their requests.
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff */
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff /*
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * Loop through the name and kill any namehooks and entries they
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * point to.
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff */
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff addr_bucket = DNS_ADB_INVALIDBUCKET;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff namehook = ISC_LIST_HEAD(name->namehooks);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff while (namehook != NULL) {
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff INSIST(DNS_ADBNAMEHOOK_VALID(namehook));
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff /*
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * Clean up the entry if needed.
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff */
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff entry = namehook->entry;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff if (entry != NULL) {
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff INSIST(DNS_ADBENTRY_VALID(entry));
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff if (addr_bucket != entry->lock_bucket) {
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff if (addr_bucket != DNS_ADB_INVALIDBUCKET)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff UNLOCK(&adb->entrylocks[addr_bucket]);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff addr_bucket = entry->lock_bucket;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff LOCK(&adb->entrylocks[addr_bucket]);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff }
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff INSIST(entry->refcount > 0);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff entry->refcount--;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff if (entry->refcount == 0) {
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff /* XXX kill zoneinfo here */
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff ISC_LIST_UNLINK(adb->entries[addr_bucket],
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff entry, link);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff free_adbentry(adb, &entry);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff }
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff }
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff /*
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * Free the namehook
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff */
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff namehook->entry = NULL;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff ISC_LIST_UNLINK(name->namehooks, namehook, link);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff free_adbnamehook(adb, &namehook);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff namehook = ISC_LIST_HEAD(name->namehooks);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff }
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff /*
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff * And lastly, unlink and free the name.
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff */
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff ISC_LIST_UNLINK(adb->names[name_bucket], name, link);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff free_adbname(adb, &name);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff if (name_bucket != DNS_ADB_INVALIDBUCKET)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff UNLOCK(&adb->namelocks[name_bucket]);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff if (addr_bucket != DNS_ADB_INVALIDBUCKET)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff UNLOCK(&adb->entrylocks[addr_bucket]);
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;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff isc_boolean_t decr_refcnt;
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;
e51511aa3281f8dc384eb1283115c7f8d5c402aeMichael Graff decr_refcnt = 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) {
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff name = new_adbname(adb);
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 result = dns_name_dup(host, adb->mctx, &name->name);
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (result != ISC_R_SUCCESS)
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff goto out;
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;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff entry->refcount++;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff entry->sockaddr = *addr;
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff /*
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * If needed, string up the name and entry. Do the name last, since
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff * adding multiple addresses is simplified in that case.
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff */
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff if (!ISC_LINK_LINKED(name, link))
ad3a5c4b7e21af04d1b872f933c2e19e5c0a135bMichael Graff ISC_LIST_PREPEND(adb->names[name_bucket], name, link);
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
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graffdns_adb_cancel(dns_adb_t *adb, dns_adbhandle_t **handle)
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff{
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff REQUIRE(DNS_ADB_VALID(adb));
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff REQUIRE(handle != NULL && DNS_ADBHANDLE_VALID(*handle));
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff INSIST(1 == 0);
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff}
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graffvoid
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graffdns_adb_done(dns_adb_t *adb, dns_adbhandle_t **handle)
66bd3b3c6b171271c705b897823dcdcf29464698Michael Graff{
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff REQUIRE(DNS_ADB_VALID(adb));
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff REQUIRE(handle != NULL && DNS_ADBHANDLE_VALID(*handle));
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff
11fcc67616fac1bc6a28b3d4fed24641137888e7Michael Graff INSIST(1 == 0);
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
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);
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;
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff tmpp = inet_ntop(sa->type.sa.sa_family, &sa->type.sa,
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff tmp, sizeof tmp);
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff if (tmpp == NULL)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff tmpp = "CANNOT TRANSLATE ADDRESS!";
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff fprintf(f, "\trefcount %u flags %08x goodness %d"
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff " srtt %u host %s\n",
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff entry->refcount, entry->flags, entry->goodness,
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff 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
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 if (nh == NULL)
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff fprintf(f, "\t\tNo name hooks\n");
64828244e04e86dfa40f0a4f0c05f27923da499dMichael Graff
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}