adb.c revision 3c5148c4d98af51d6dcb449c6dbd45fe8c645f61
1633838b8255282d10af15c5c84cee5a51466712Bob Halley/*
ca41b452ede6feaa9d8739ec3cae19389a7b0d03Bob Halley * Copyright (C) 1999 Internet Software Consortium.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley *
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * Permission to use, copy, modify, and distribute this software for any
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * purpose with or without fee is hereby granted, provided that the above
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * copyright notice and this permission notice appear in all copies.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley *
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * SOFTWARE.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley */
94e25967cda41b886e33ec254b917d21df21a187Bob Halley
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley/*
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley * Implementation notes
94e25967cda41b886e33ec254b917d21df21a187Bob Halley * --------------------
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence *
94e25967cda41b886e33ec254b917d21df21a187Bob Halley * In handles, if task == NULL, no events will be generated, and no events
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * have been sent. If task != NULL but taskaction == NULL, an event has been
94e25967cda41b886e33ec254b917d21df21a187Bob Halley * posted but not yet freed. If neigher are NULL, no event was posted.
364a82f7c25b62967678027043425201a5e5171aBob Halley *
94e25967cda41b886e33ec254b917d21df21a187Bob Halley */
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#include <config.h>
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#include <isc/assertions.h>
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley#include <isc/magic.h>
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley#include <isc/mutex.h>
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley#include <isc/mutexblock.h>
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#include <isc/event.h>
3740b569ae76295b941d57a724a43beb75b533baBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#include <dns/address.h>
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#include <dns/name.h>
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#include "../isc/util.h"
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#define DNS_ADB_MAGIC 0x44616462 /* Dadb. */
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#define DNS_ADBNAME_MAGIC 0x6164624e /* adbN. */
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#define DNS_ADBNAMEHOOK_MAGIC 0x61644e48 /* adNH. */
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#define DNS_ADBZONEINFO_MAGIC 0x6164625a /* adbZ. */
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#define DNS_ADBZONEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBZONEINFO_MAGIC)
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#define DNS_ADBENTRY_MAGIC 0x61646245 /* adbE. */
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
3740b569ae76295b941d57a724a43beb75b533baBob Halley/*
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley * Lengths of lists needs to be powers of two.
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley */
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#define DNS_ADBNAMELIST_LENGTH 16 /* how many buckets for names */
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#define DNS_ADBENTRYLIST_LENGTH 16 /* how many buckets for addresses */
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#define FREE_ITEMS 16 /* free count for memory pools */
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#define FILL_COUNT 8 /* fill count for memory pools */
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halleytypedef struct dns_adbname dns_adbname_t;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halleytypedef ISC_LIST(dns_adbname_t) dns_adbnamelist_t;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halleytypedef struct dns_adbnamehook dns_adbnamehook_t;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halleytypedef struct dns_adbzoneinfo dns_adbzoneinfo_t;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halleytypedef ISC_LIST(dns_adbentry_t) dns_adbentrylist_t;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halleystruct dns_adb {
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley unsigned int magic;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley isc_mutex_t lock;
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley isc_mem_t *mctx;
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley isc_mutex_t mplock;
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence isc_mempool_t *nmp; /* dns_adbname_t */
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence isc_mempool_t *nhmp; /* dns_adbnamehook_t */
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence isc_mempool_t *zimp; /* dns_adbzoneinfo_t */
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence isc_mempool_t *emp; /* dns_adbentry_t */
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence isc_mempool_t *ahmp; /* dns_adbhandle_t */
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence isc_mempool_t *aimp; /* dns_adbaddrinfo_t */
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence /*
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence * Bucketized locks and lists for names.
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence */
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence dns_adbnamelist_t names[DNS_ADBNAMELIST_LENGTH];
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence isc_mutex_t namelocks[DNS_ADBNAMELIST_LENGTH];
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
3740b569ae76295b941d57a724a43beb75b533baBob Halley /*
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley * Bucketized locks for entries.
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley */
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley dns_adbentrylist_t entries[DNS_ADBENTRYLIST_LENGTH];
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley isc_mutex_t entrylocks[DNS_ADBENTRYLIST_LENGTH];
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley /*
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley * List of running and idle handles.
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley */
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley ISC_LIST(dns_adbhandle_t) running_handles;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley ISC_LIST(dns_adbhandle_t) idle_handles;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley};
3740b569ae76295b941d57a724a43beb75b533baBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halleystruct dns_adbname {
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley unsigned int magic;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley dns_name_t name;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley ISC_LIST(dns_adbnamehook_t) namehooks;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley ISC_LINK(dns_adbname_t) link;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley};
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley/*
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley * dns_adbnamehook_t
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley *
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley * This is a small widget that dangles off a dns_adbname_t. It contains a
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley * pointer to the address information about this host, and a link to the next
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley * namehook that will contain the next address this host has.
4d6964d70a114b53a11a3bd778d9b8f5179a7934Bob Halley */
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halleystruct dns_adbnamehook {
94e25967cda41b886e33ec254b917d21df21a187Bob Halley unsigned int magic;
94e25967cda41b886e33ec254b917d21df21a187Bob Halley dns_adbentry_t *entry;
94e25967cda41b886e33ec254b917d21df21a187Bob Halley ISC_LINK(dns_adbnamehook_t) link;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley};
94e25967cda41b886e33ec254b917d21df21a187Bob Halley
94e25967cda41b886e33ec254b917d21df21a187Bob Halley/*
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley * dns_adbzoneinfo_t
94e25967cda41b886e33ec254b917d21df21a187Bob Halley *
94e25967cda41b886e33ec254b917d21df21a187Bob Halley * This is a small widget that holds zone-specific information about an
f47bb8d1f3a2e3edae11942d0d7e62e96caa5720Bob Halley * address. Currently limited to lameness, but could just as easily be
94e25967cda41b886e33ec254b917d21df21a187Bob Halley * extended to other types of information about zones.
94e25967cda41b886e33ec254b917d21df21a187Bob Halley */
94e25967cda41b886e33ec254b917d21df21a187Bob Halleystruct dns_adbzoneinfo {
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley unsigned int magic;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
94e25967cda41b886e33ec254b917d21df21a187Bob Halley dns_name_t *zone;
94e25967cda41b886e33ec254b917d21df21a187Bob Halley unsigned int lame_timer;
94e25967cda41b886e33ec254b917d21df21a187Bob Halley
94e25967cda41b886e33ec254b917d21df21a187Bob Halley ISC_LINK(dns_adbzoneinfo_t) link;
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley};
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley/*
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley * An address entry. It holds quite a bit of information about addresses,
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley * including edns state, rtt, and of course the address of the host.
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley */
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halleystruct dns_adbentry {
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley unsigned int magic;
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley int lock_bucket;
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley unsigned int refcount;
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley unsigned int flags;
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley int goodness; /* bad <= 0 < good */
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley unsigned int srtt;
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley isc_sockaddr_t sockaddr;
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley ISC_LIST(dns_adbzoneinfo_t) zoneinfo;
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley ISC_LINK(dns_adbentry_t) link;
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley};
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley/*
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley * Internal functions (and prototypes).
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley */
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halleystatic dns_adbname_t *new_adbname(dns_adb_t *);
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halleystatic dns_adbnamehook_t *new_adbnamehook(dns_adb_t *, dns_adbentry_t *);
94e25967cda41b886e33ec254b917d21df21a187Bob Halleystatic dns_adbzoneinfo_t *new_adbzoneinfo(dns_adb_t *);
3740b569ae76295b941d57a724a43beb75b533baBob Halleystatic dns_adbentry_t *new_adbentry(dns_adb_t *);
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halleystatic dns_adbhandle_t *new_adbhandle(dns_adb_t *);
94e25967cda41b886e33ec254b917d21df21a187Bob Halleystatic dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *, dns_adbentry_t *);
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halleystatic dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *, int *);
94e25967cda41b886e33ec254b917d21df21a187Bob Halleystatic dns_adbentry_t *find_entry_and_lock(dns_adb_t *, isc_sockaddr_t *,
94e25967cda41b886e33ec254b917d21df21a187Bob Halley int *);
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley
94e25967cda41b886e33ec254b917d21df21a187Bob Halleystatic dns_adbname_t *
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halleynew_adbname(dns_adb_t *adb)
94e25967cda41b886e33ec254b917d21df21a187Bob Halley{
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley dns_adbname_t *name;
94e25967cda41b886e33ec254b917d21df21a187Bob Halley
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley name = isc_mempool_get(adb->nmp);
94e25967cda41b886e33ec254b917d21df21a187Bob Halley if (name == NULL)
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley return (NULL);
94e25967cda41b886e33ec254b917d21df21a187Bob Halley
94e25967cda41b886e33ec254b917d21df21a187Bob Halley name->magic = DNS_ADBNAME_MAGIC;
94e25967cda41b886e33ec254b917d21df21a187Bob Halley dns_name_init(&name->name, NULL);
94e25967cda41b886e33ec254b917d21df21a187Bob Halley ISC_LIST_INIT(name->namehooks);
94e25967cda41b886e33ec254b917d21df21a187Bob Halley ISC_LINK_INIT(name, link);
3740b569ae76295b941d57a724a43beb75b533baBob Halley
94e25967cda41b886e33ec254b917d21df21a187Bob Halley return (name);
94e25967cda41b886e33ec254b917d21df21a187Bob Halley}
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
94e25967cda41b886e33ec254b917d21df21a187Bob Halleystatic dns_adbnamehook_t *
94e25967cda41b886e33ec254b917d21df21a187Bob Halleynew_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry)
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley{
94e25967cda41b886e33ec254b917d21df21a187Bob Halley dns_adbnamehook_t *nh;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley nh = isc_mempool_get(adb->nhmp);
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley if (nh == NULL)
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley return (NULL);
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
94e25967cda41b886e33ec254b917d21df21a187Bob Halley nh->magic = DNS_ADBNAMEHOOK_MAGIC;
94e25967cda41b886e33ec254b917d21df21a187Bob Halley nh->entry = entry;
94e25967cda41b886e33ec254b917d21df21a187Bob Halley ISC_LINK_INIT(nh, link);
94e25967cda41b886e33ec254b917d21df21a187Bob Halley
3740b569ae76295b941d57a724a43beb75b533baBob Halley return (nh);
94e25967cda41b886e33ec254b917d21df21a187Bob Halley}
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
94e25967cda41b886e33ec254b917d21df21a187Bob Halleystatic dns_adbzoneinfo_t *
94e25967cda41b886e33ec254b917d21df21a187Bob Halleynew_adbzoneinfo(dns_adb_t *adb)
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley{
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence dns_adbzoneinfo_t *zi;
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence zi = isc_mempool_get(adb->zimp);
94e25967cda41b886e33ec254b917d21df21a187Bob Halley if (zi == NULL)
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley return (NULL);
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley zi->magic = DNS_ADBZONEINFO_MAGIC;
94e25967cda41b886e33ec254b917d21df21a187Bob Halley zi->zone = NULL;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley zi->lame_timer = 0;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley ISC_LINK_INIT(zi, link);
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
94e25967cda41b886e33ec254b917d21df21a187Bob Halley return (zi);
94e25967cda41b886e33ec254b917d21df21a187Bob Halley}
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrencestatic dns_adbentry_t *
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrencenew_adbentry(dns_adb_t *adb)
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence{
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence dns_adbentry_t *e;
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence e = isc_mempool_get(adb->emp);
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence if (e == NULL)
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence return (NULL);
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence e->magic = DNS_ADBENTRY_MAGIC;
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence e->lock_bucket = -1;
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence e->refcount = 0;
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence e->flags = 0;
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence e->goodness = 0;
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence e->srtt = 0;
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence ISC_LIST_INIT(e->zoneinfo);
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence ISC_LINK_INIT(e, link);
f924db5119ce01b332c28bfa06e279ab40a39f52Bob Halley
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence return (e);
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence}
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrencestatic dns_adbhandle_t *
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrencenew_adbhandle(dns_adb_t *adb)
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence{
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence dns_adbhandle_t *h;
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence isc_result_t result;
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence h = isc_mempool_get(adb->ahmp);
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence if (h == NULL)
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence return (NULL);
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence /*
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence * public members
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence */
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence h->magic = 0;
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence h->adb = adb;
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence ISC_LIST_INIT(h->list);
/*
* private members
*/
result = isc_mutex_init(&h->lock);
if (result != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_mutex_init failed in new_adbhandle()");
isc_mempool_put(adb->ahmp, h);
return (NULL);
}
h->task = NULL;
h->taskaction = NULL;
h->arg = NULL;
ISC_LINK_INIT(h, link);
ISC_EVENT_INIT(&h->event, sizeof (isc_event_t), 0, 0, 0, NULL, NULL,
NULL, NULL, h);
h->magic = DNS_ADBHANDLE_MAGIC;
return (h);
}
/*
* Copy bits from the entry into the newly allocated addrinfo. The entry
* must be locked, and the reference count must be bumped up by one
* if this function returns a valid pointer.
*/
static dns_adbaddrinfo_t *
new_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry)
{
dns_adbaddrinfo_t *ai;
ai = isc_mempool_get(adb->aimp);
if (ai == NULL)
return (NULL);
ai->magic = DNS_ADBADDRINFO_MAGIC;
ai->sockaddr = &entry->sockaddr;
ai->goodness = entry->goodness;
ai->srtt = entry->srtt;
ai->flags = entry->flags;
ai->entry = entry;
ISC_LINK_INIT(ai, link);
return (ai);
}
/*
* Search for the name. NOTE: The bucket is kept locked on both
* success and failure, so it must always be unlocked by the caller!
*/
static dns_adbname_t *
find_name_and_lock(dns_adb_t *adb, dns_name_t *name, int *bucketp)
{
dns_adbname_t *adbname;
unsigned int bucket;
bucket = dns_name_hash(name, ISC_FALSE);
bucket &= (DNS_ADBNAMELIST_LENGTH - 1);
LOCK(&adb->namelocks[bucket]);
*bucketp = (int)bucket;
adbname = ISC_LIST_HEAD(adb->names[bucket]);
while (adbname != NULL) {
if (dns_name_equal(name, &adbname->name))
return (adbname);
adbname = ISC_LIST_NEXT(adbname, link);
}
return (NULL);
}
/*
* Search for the address. NOTE: The bucket is kept locked on both
* success and failure, so it must always be unlocked by the caller!
*/
static dns_adbentry_t *
find_entry_and_lock(dns_adb_t *adb, isc_sockaddr_t *addr, int *bucketp)
{
dns_adbentry_t *entry;
unsigned int bucket;
bucket = isc_sockaddr_hash(addr, ISC_TRUE);
bucket &= (DNS_ADBENTRYLIST_LENGTH - 1);
LOCK(&adb->entrylocks[bucket]);
*bucketp = (int)bucket;
entry = ISC_LIST_HEAD(adb->entries[bucket]);
while (entry != NULL) {
if (isc_sockaddr_equal(addr, &entry->sockaddr))
return (entry);
entry = ISC_LIST_NEXT(entry, link);
}
return (NULL);
}
static void
destroy(dns_adb_t *adb)
{
adb->magic = 0;
isc_mempool_destroy(&adb->nmp);
isc_mempool_destroy(&adb->nhmp);
isc_mempool_destroy(&adb->zimp);
isc_mempool_destroy(&adb->emp);
isc_mempool_destroy(&adb->ahmp);
isc_mempool_destroy(&adb->aimp);
isc_mutexblock_destroy(adb->entrylocks, DNS_ADBENTRYLIST_LENGTH);
isc_mutexblock_destroy(adb->namelocks, DNS_ADBNAMELIST_LENGTH);
isc_mutex_destroy(&adb->lock);
isc_mutex_destroy(&adb->mplock);
isc_mem_put(adb->mctx, adb, sizeof (dns_adb_t));
}
/*
* Public functions.
*/
isc_result_t
dns_adb_create(isc_mem_t *mem, dns_adb_t **newadb)
{
dns_adb_t *adb;
isc_result_t result;
int i;
REQUIRE(mem != NULL);
REQUIRE(newadb != NULL && *newadb == NULL);
adb = isc_mem_get(mem, sizeof (dns_adb_t));
if (adb == NULL)
return (ISC_R_NOMEMORY);
/*
* Initialize things here that cannot fail, and especially things
* that must be NULL for the error return to work properly.
*/
adb->magic = 0;
adb->nmp = NULL;
adb->nhmp = NULL;
adb->zimp = NULL;
adb->emp = NULL;
adb->ahmp = NULL;
adb->aimp = NULL;
result = isc_mutex_init(&adb->lock);
if (result != ISC_R_SUCCESS)
goto fail1;
result = isc_mutex_init(&adb->mplock);
if (result != ISC_R_SUCCESS)
goto fail1;
/*
* Initialize the bucket locks for names and elements.
* May as well initialize the list heads, too.
*/
result = isc_mutexblock_init(adb->namelocks, DNS_ADBNAMELIST_LENGTH);
if (result != ISC_R_SUCCESS)
goto fail1;
for (i = 0 ; i < DNS_ADBNAMELIST_LENGTH ; i++)
ISC_LIST_INIT(adb->names[i]);
for (i = 0 ; i < DNS_ADBENTRYLIST_LENGTH ; i++)
ISC_LIST_INIT(adb->entries[i]);
result = isc_mutexblock_init(adb->entrylocks, DNS_ADBENTRYLIST_LENGTH);
if (result != ISC_R_SUCCESS)
goto fail2;
/*
* Memory pools
*/
#define MPINIT(t, p) do { \
result = isc_mempool_create(mem, sizeof (t), &(p)); \
if (result != ISC_R_SUCCESS) \
goto fail3; \
isc_mempool_setfreemax((p), FREE_ITEMS); \
isc_mempool_setfillcount((p), FILL_COUNT); \
} while (0)
#define MPINIT_LOCKED(t, p) do { \
MPINIT(t, p); \
isc_mempool_associatelock((p), &adb->mplock); \
} while (0)
MPINIT_LOCKED(dns_adbname_t, adb->nmp);
MPINIT_LOCKED(dns_adbnamehook_t, adb->nhmp);
MPINIT_LOCKED(dns_adbzoneinfo_t, adb->zimp);
MPINIT_LOCKED(dns_adbentry_t, adb->emp);
MPINIT_LOCKED(dns_adbhandle_t, adb->ahmp);
MPINIT_LOCKED(dns_adbaddrinfo_t, adb->aimp);
#undef MPINIT
#undef MPINIT_LOCKED
/*
* Normal return.
*/
adb->mctx = mem;
adb->magic = DNS_ADB_MAGIC;
*newadb = adb;
return (ISC_R_SUCCESS);
fail3: /* clean up entrylocks */
isc_mutexblock_destroy(adb->entrylocks, DNS_ADBENTRYLIST_LENGTH);
fail2: /* clean up namelocks */
isc_mutexblock_destroy(adb->namelocks, DNS_ADBNAMELIST_LENGTH);
fail1: /* clean up only allocated memory */
if (adb->nmp != NULL)
isc_mempool_destroy(&adb->nmp);
if (adb->nhmp != NULL)
isc_mempool_destroy(&adb->nhmp);
if (adb->zimp != NULL)
isc_mempool_destroy(&adb->zimp);
if (adb->emp != NULL)
isc_mempool_destroy(&adb->emp);
if (adb->ahmp != NULL)
isc_mempool_destroy(&adb->ahmp);
if (adb->aimp != NULL)
isc_mempool_destroy(&adb->aimp);
isc_mutex_destroy(&adb->lock);
isc_mutex_destroy(&adb->mplock);
isc_mem_put(mem, adb, sizeof (dns_adb_t));
return (result);
}
void
dns_adb_destroy(dns_adb_t **adbx)
{
dns_adb_t *adb;
REQUIRE(adbx != NULL && DNS_ADB_VALID(*adbx));
adb = *adbx;
*adbx = NULL;
/*
* XXX Need to wait here until the adb is fully shut down.
*/
/*
* If all lists are empty, destroy the memory used by this
* adb. XXX Need to implement this.
*/
destroy(adb);
}
isc_result_t
dns_adb_lookup(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t *action,
void *arg, dns_rdataset_t *nsrdataset, dns_name_t *zone,
dns_adbhandle_t **handle)
{
REQUIRE(DNS_ADB_VALID(adb));
if (task != NULL) {
REQUIRE(action != NULL);
}
REQUIRE(nsrdataset != NULL);
REQUIRE(zone != NULL);
REQUIRE(handle != NULL && *handle == NULL);
return (ISC_R_NOTIMPLEMENTED);
}
isc_result_t
dns_adb_refresh(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t *action,
void *arg, dns_rdataset_t *nsrdataset, dns_name_t *zone,
dns_adbhandle_t *handle)
{
REQUIRE(DNS_ADB_VALID(adb));
if (task != NULL) {
REQUIRE(action != NULL);
}
REQUIRE(nsrdataset != NULL);
REQUIRE(zone != NULL);
REQUIRE(DNS_ADBHANDLE_VALID(handle));
return (ISC_R_NOTIMPLEMENTED);
}
isc_result_t
dns_adb_insert(dns_adb_t *adb, dns_name_t *host, isc_sockaddr_t *addr)
{
dns_adbname_t *name;
isc_boolean_t free_name, free_namedata;
dns_adbentry_t *entry;
isc_boolean_t free_entry;
dns_adbnamehook_t *namehook;
isc_boolean_t free_namehook;
int name_bucket, addr_bucket; /* unlock if != -1 */
isc_result_t result;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(host != NULL);
REQUIRE(addr != NULL);
name = NULL;
free_name = ISC_FALSE;
free_namedata = ISC_FALSE;
entry = NULL;
free_entry = ISC_FALSE;
namehook = NULL;
free_namehook = ISC_FALSE;
name_bucket = -1;
addr_bucket = -1;
result = ISC_R_UNEXPECTED;
/*
* First, see if the host is already in the database. If it is,
* don't make a new host entry. If not, copy the name and name's
* contents into our structure and allocate what we'll need
* to attach things together.
*/
name = find_name_and_lock(adb, host, &name_bucket);
if (name == NULL) {
name = new_adbname(adb);
if (name == NULL) {
result = ISC_R_NOMEMORY;
goto out;
}
free_name = ISC_TRUE;
result = dns_name_dup(host, adb->mctx, &name->name);
if (result != ISC_R_SUCCESS)
goto out;
free_namedata = ISC_TRUE;
}
/*
* Now, while keeping the name locked, search for the address.
* Three possibilities: One, the address doesn't exist.
* Two, the address exists, but we aren't linked to it.
* Three, the address exists and we are linked to it.
* (1) causes a new entry and namehook to be created.
* (2) causes only a new namehook.
* (3) is an error.
*/
entry = find_entry_and_lock(adb, addr, &addr_bucket);
/*
* Case (1): new entry and namehook.
*/
if (entry == NULL) {
entry = new_adbentry(adb);
if (entry == NULL) {
result = ISC_R_NOMEMORY;
goto out;
}
free_entry = ISC_TRUE;
}
/*
* Case (3): entry exists, we're linked.
*/
namehook = ISC_LIST_HEAD(name->namehooks);
while (namehook != NULL) {
if (namehook->entry == entry) {
result = ISC_R_EXISTS;
goto out;
}
}
/*
* Case (2): New namehook, link to entry from above.
*/
namehook = new_adbnamehook(adb, entry);
if (namehook == NULL) {
result = ISC_R_NOMEMORY;
goto out;
}
free_namehook = ISC_TRUE;
ISC_LIST_APPEND(name->namehooks, namehook, link);
entry->refcount++;
/*
* If needed, string up the name and entry. Do the name last, since
* adding multiple addresses is simplified in that case.
*/
if (!ISC_LINK_LINKED(name, link))
ISC_LIST_PREPEND(adb->names[name_bucket], name, link);
if (!ISC_LINK_LINKED(entry, link))
ISC_LIST_PREPEND(adb->entries[addr_bucket], entry, link);
UNLOCK(&adb->namelocks[name_bucket]);
UNLOCK(&adb->entrylocks[addr_bucket]);
return (ISC_R_SUCCESS);
out:
if (free_namedata)
dns_name_free(&name->name, adb->mctx);
if (free_name)
isc_mempool_put(adb->nmp, name);
if (free_entry)
isc_mempool_put(adb->emp, entry);
if (free_namehook)
isc_mempool_put(adb->nhmp, namehook);
if (name_bucket != -1)
UNLOCK(&adb->namelocks[name_bucket]);
if (addr_bucket != -1)
UNLOCK(&adb->entrylocks[addr_bucket]);
return (result);
}
void
dns_adb_cancel(dns_adb_t *adb, dns_adbhandle_t **handle)
{
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(handle != NULL && DNS_ADBHANDLE_VALID(*handle));
INSIST(1 == 0);
}
void
dns_adb_done(dns_adb_t *adb, dns_adbhandle_t **handle)
{
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(handle != NULL && DNS_ADBHANDLE_VALID(*handle));
INSIST(1 == 0);
}