adb.c revision 4b36b9c1fff56d836feeaa1dc7eb1d4676d9c8bb
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * Copyright (C) 2004-2014 Internet Systems Consortium, Inc. ("ISC")
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * Copyright (C) 1999-2003 Internet Software Consortium.
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * Permission to use, copy, modify, and/or distribute this software for any
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * purpose with or without fee is hereby granted, provided that the above
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * copyright notice and this permission notice appear in all copies.
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * PERFORMANCE OF THIS SOFTWARE.
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * In finds, if task == NULL, no events will be generated, and no events
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * have been sent. If task != NULL but taskaction == NULL, an event has been
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * posted but not yet freed. If neither are NULL, no event was posted.
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff#include <isc/string.h> /* Required for HP/UX (and others?) */
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff#define DNS_ADB_MAGIC ISC_MAGIC('D', 'a', 'd', 'b')
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff#define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff#define DNS_ADBNAME_MAGIC ISC_MAGIC('a', 'd', 'b', 'N')
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff#define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff#define DNS_ADBNAMEHOOK_MAGIC ISC_MAGIC('a', 'd', 'N', 'H')
aed3b8cb4e1e9274df734b61844845ea0e33e79bBrian Wellington#define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
aed3b8cb4e1e9274df734b61844845ea0e33e79bBrian Wellington#define DNS_ADBLAMEINFO_MAGIC ISC_MAGIC('a', 'd', 'b', 'Z')
aed3b8cb4e1e9274df734b61844845ea0e33e79bBrian Wellington#define DNS_ADBLAMEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBLAMEINFO_MAGIC)
aed3b8cb4e1e9274df734b61844845ea0e33e79bBrian Wellington#define DNS_ADBENTRY_MAGIC ISC_MAGIC('a', 'd', 'b', 'E')
aed3b8cb4e1e9274df734b61844845ea0e33e79bBrian Wellington#define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
aed3b8cb4e1e9274df734b61844845ea0e33e79bBrian Wellington#define DNS_ADBFETCH_MAGIC ISC_MAGIC('a', 'd', 'F', '4')
aed3b8cb4e1e9274df734b61844845ea0e33e79bBrian Wellington#define DNS_ADBFETCH_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff#define DNS_ADBFETCH6_MAGIC ISC_MAGIC('a', 'd', 'F', '6')
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff#define DNS_ADBFETCH6_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH6_MAGIC)
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * For type 3 negative cache entries, we will remember that the address is
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * broken for this long. XXXMLG This is also used for actual addresses, too.
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * The intent is to keep us from constantly asking about A/AAAA records
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * if the zone has extremely low TTLs.
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff#define ADB_CACHE_MAXIMUM 86400 /*%< seconds (86400 = 24 hours) */
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * The period in seconds after which an ADB name entry is regarded as stale
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff * and forced to be cleaned up.
9f95b0199c3f1b0ef3d40c1854a7501d72112e5aMichael Graff * TODO: This should probably be configurable at run-time.
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff#define FREE_ITEMS 64 /*%< free count for memory pools */
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff#define FILL_COUNT 16 /*%< fill count for memory pools */
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff#define DNS_ADB_INVALIDBUCKET (-1) /*%< invalid bucket address */
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff#define DNS_ADB_MINADBSIZE (1024U*1024U) /*%< 1 Megabyte */
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Grafftypedef ISC_LIST(dns_adbname_t) dns_adbnamelist_t;
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Grafftypedef struct dns_adbnamehook dns_adbnamehook_t;
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Grafftypedef ISC_LIST(dns_adbnamehook_t) dns_adbnamehooklist_t;
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Grafftypedef struct dns_adblameinfo dns_adblameinfo_t;
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Grafftypedef ISC_LIST(dns_adbentry_t) dns_adbentrylist_t;
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff/*% dns adb structure */
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff unsigned int magic;
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff isc_mutex_t reflock; /*%< Covers irefcnt, erefcnt */
890fb60939f93161ca0c63e19c7154eaf3fed156Michael Graff isc_mutex_t overmemlock; /*%< Covers overmem */
int next_cleanbucket;
unsigned int irefcnt;
unsigned int erefcnt;
unsigned int nnames;
unsigned int namescnt;
unsigned int *name_refcnt;
unsigned int nentries;
unsigned int entriescnt;
unsigned int *entry_refcnt;
struct dns_adbname {
unsigned int magic;
unsigned int partial_result;
unsigned int flags;
int lock_bucket;
unsigned int chains;
unsigned int fetch_err;
unsigned int fetch6_err;
struct dns_adbfetch {
unsigned int magic;
unsigned int depth;
struct dns_adbnamehook {
unsigned int magic;
struct dns_adblameinfo {
unsigned int magic;
struct dns_adbentry {
unsigned int magic;
int lock_bucket;
unsigned int refcnt;
unsigned int flags;
unsigned int srtt;
unsigned char plain;
unsigned char plainto;
unsigned char edns;
unsigned char * sit;
dns_adbentry_t *);
isc_sockaddr_t *, int *,
static void water(void *, int);
((o) & DNS_ADBFIND_STARTATZONE))
(r) == DNS_R_NCACHENXRRSET)
(r) == DNS_R_NXRRSET)
(r) == DNS_R_NCACHENXDOMAIN)
(r) == DNS_R_NXRRSET || \
(r) == DNS_R_HINTNXRRSET)
static const char *errnames[] = {
static inline dns_ttl_t
return (ttl);
dns_adbentry_t *e;
unsigned int i, n, bucket;
goto check_exit;
if (nbuckets[i] != 0)
n = nbuckets[i];
goto done;
goto cleanup;
goto cleanup;
goto cleanup;
newentry_refcnt[i] = 0;
while (e != NULL) {
while (e != NULL) {
goto done;
sizeof(*newentries) * n);
sizeof(*newdeadentries) * n);
sizeof(*newentrylocks) * n);
sizeof(*newentry_sd) * n);
sizeof(*newentry_refcnt) * n);
done:
unsigned int i, n, bucket;
goto check_exit;
if (nbuckets[i] != 0)
n = nbuckets[i];
goto done;
goto cleanup;
goto cleanup;
goto cleanup;
newname_refcnt[i] = 0;
goto done;
sizeof(*newname_refcnt) * n);
done:
static isc_result_t
int addr_bucket;
unsigned int findoptions;
goto fail;
now);
goto fail;
fail:
if (new_addresses_added) {
return (ISC_R_SUCCESS);
return (result);
static isc_boolean_t
int bucket;
name = *n;
*n = NULL;
if (result)
return (result);
if (result)
return (result);
static isc_boolean_t
static inline isc_boolean_t
int bucket;
return (result);
dns_adbentry_t *e;
if (e == NULL)
if (e->refcnt == 0) {
static inline isc_boolean_t
int bucket;
return (result);
static isc_boolean_t
unsigned int bucket;
return (result);
static isc_boolean_t
unsigned int bucket;
if (result)
return (result);
static isc_boolean_t
int addr_bucket;
return (result);
static isc_result_t
unsigned int nlabels;
int order;
return (result);
return (result);
return (result);
return (result);
return (result);
NULL);
return (result);
return (result);
return (ISC_R_SUCCESS);
unsigned int addrs)
switch (evtype) {
if ((notify) != 0) {
if (wanted == 0)
if (process) {
static inline isc_boolean_t
return (result);
int bucket;
if (lock)
if (lock)
static inline isc_boolean_t
int bucket;
if (lock)
if (lock)
if (!destroy_entry)
return (result);
if (result)
return (result);
static inline dns_adbname_t *
return (NULL);
return (NULL);
return (name);
dns_adbname_t *n;
n = *name;
n->magic = 0;
static inline dns_adbnamehook_t *
return (NULL);
return (nh);
static inline dns_adblameinfo_t *
return (NULL);
return (NULL);
return (li);
static inline dns_adbentry_t *
dns_adbentry_t *e;
isc_uint32_t r;
if (e == NULL)
return (NULL);
e->refcnt = 0;
e->flags = 0;
e->udpsize = 0;
e->edns = 0;
e->plain = 0;
e->plainto = 0;
e->to4096 = 0;
e->to1432 = 0;
e->to1232 = 0;
e->to512 = 0;
e->sitlen = 0;
isc_random_get(&r);
e->lastage = 0;
e->expires = 0;
dns_adbentry_t *e;
e = *entry;
e->magic = 0;
static inline dns_adbfind_t *
dns_adbfind_t *h;
if (h == NULL)
return (NULL);
h->magic = 0;
h->partial_result = 0;
h->options = 0;
h->flags = 0;
return (NULL);
static inline dns_adbfetch_t *
dns_adbfetch_t *f;
if (f == NULL)
return (NULL);
f->magic = 0;
dns_adbfetch_t *f;
f = *fetch;
f->magic = 0;
static inline isc_boolean_t
static inline dns_adbaddrinfo_t *
return (NULL);
return (ai);
static inline dns_adbname_t *
int bucket;
return (adbname);
return (NULL);
static inline dns_adbentry_t *
int bucket;
return (entry);
return (NULL);
static isc_boolean_t
return (ISC_FALSE);
return (is_bad);
int bucket;
goto nextv4;
goto out;
goto nextv6;
goto out;
out:
static isc_boolean_t
return (result);
return (result);
return (result);
return (result);
return (result);
return (result);
int scans = 0;
for (victims = 0;
scans++;
victims++;
goto next;
victims++;
next:
if (!overmem)
static isc_boolean_t
return (result);
return (result);
if (result)
return (result);
static isc_boolean_t
return (result);
if (!result)
return (result);
static isc_boolean_t
return (result);
return (ISC_R_NOMEMORY);
goto fail0b;
goto fail0c;
goto fail0d;
goto fail0e;
goto fail0f;
goto fail0g;
goto fail1; \
goto fail1; \
goto fail1;
goto fail2;
#define MPINIT(t, p, n) do { \
goto fail3; \
isc_mempool_setname((p), n); \
goto fail3;
goto fail3;
return (ISC_R_SUCCESS);
return (result);
if (need_exit_check) {
int bucket;
unsigned int wanted_addresses;
unsigned int wanted_fetches;
unsigned int query_pending;
wanted_fetches = 0;
query_pending = 0;
if (now == 0)
return (ISC_R_NOMEMORY);
namebuf[0] = 0;
goto out;
goto out;
goto post_copy;
goto v6;
goto post_copy;
goto fetch;
goto v6;
v6:
goto fetch;
goto post_copy;
goto fetch;
if (wanted_fetches != 0 &&
if (alias)
if (want_event) {
if (alias) {
goto out;
out:
if (want_event) {
return (result);
int bucket;
int bucket;
int unlock_bucket;
goto cleanup;
if (debug)
if (debug)
if (debug)
if (debug)
if (debug)
if (debug)
#ifdef ISC_PLATFORM_USESIT
const char *tmpp;
case AF_INET:
case AF_INET6:
if (debug)
if (NAME_FETCH_A(n))
if (NAME_FETCH_AAAA(n))
static isc_result_t
switch (result) {
case DNS_R_GLUE:
case DNS_R_HINT:
case ISC_R_SUCCESS:
case DNS_R_NXDOMAIN:
case DNS_R_NXRRSET:
adbname);
adbname);
case DNS_R_NCACHENXDOMAIN:
case DNS_R_NCACHENXRRSET:
case DNS_R_CNAME:
case DNS_R_DNAME:
adbname);
return (result);
int bucket;
unsigned int address_type;
address_type = 0;
if (want_check_exit) {
goto out;
name);
goto check_result;
goto out;
goto out;
out:
static isc_result_t
unsigned int options;
if (start_at_zone) {
adbname);
goto cleanup;
goto cleanup;
goto cleanup;
return (result);
int bucket;
goto unlock;
goto unlock;
return (result);
int bucket;
int bucket;
int bucket;
int bucket;
return (noedns);
int bucket;
int bucket;
int bucket;
int bucket;
int bucket;
unsigned int size;
return (size);
int bucket;
unsigned int size;
if (lookups > 0 &&
return (size);
int bucket;
int bucket;
len = 0;
return (len);
int bucket;
goto unlock;
goto unlock;
return (result);
int bucket;
if (want_check_exit) {
#ifdef DUMP_ADB_AFTER_CLEANING
int bucket;