adb.c revision 4d727d86b245063f00c5bfd98d138a63e0b7de6d
/*
* Copyright (C) 1999 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
/*
* Implementation notes
* --------------------
*
* In handles, if task == NULL, no events will be generated, and no events
* have been sent. If task != NULL but taskaction == NULL, an event has been
* posted but not yet freed. If neigher are NULL, no event was posted.
*
*/
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <isc/assertions.h>
#include <isc/condition.h>
#include <isc/mutexblock.h>
#include <dns/rdataset.h>
/*
* Lengths of lists needs to be powers of two.
*/
typedef struct dns_adbnamehook dns_adbnamehook_t;
typedef struct dns_adbzoneinfo dns_adbzoneinfo_t;
struct dns_adb {
unsigned int magic;
unsigned int irefcnt;
unsigned int erefcnt;
/*
* Bucketized locks and lists for names.
*/
unsigned int name_refcnt[DNS_ADBNAMELIST_LENGTH];
/*
* Bucketized locks for entries.
*/
};
struct dns_adbname {
unsigned int magic;
};
/*
* dns_adbnamehook_t
*
* This is a small widget that dangles off a dns_adbname_t. It contains a
* pointer to the address information about this host, and a link to the next
* namehook that will contain the next address this host has.
*/
struct dns_adbnamehook {
unsigned int magic;
};
/*
* dns_adbzoneinfo_t
*
* This is a small widget that holds zone-specific information about an
* address. Currently limited to lameness, but could just as easily be
* extended to other types of information about zones.
*/
struct dns_adbzoneinfo {
unsigned int magic;
};
/*
* An address entry. It holds quite a bit of information about addresses,
* including edns state, rtt, and of course the address of the host.
*/
struct dns_adbentry {
unsigned int magic;
int lock_bucket;
unsigned int refcnt;
unsigned int flags;
int edns_level; /* must be int! */
int goodness; /* bad < 0 <= good */
unsigned int srtt;
};
/*
* Internal functions (and prototypes).
*/
dns_adbentry_t *);
dns_adbentry_t *);
int *);
isc_sockaddr_t *, int *);
dns_name_t *, dns_name_t *,
dns_adbname_t *, int, isc_stdtime_t);
static inline void
{
}
}
/*
* The ADB _MUST_ be locked before calling.
*/
static void
{
int bucket;
(void)kill_fetches; /* XXX need to implement */
/*
* Run through the list. For each name, clean up handles
* found there, and cancel any fetches running. When
* all the fetches are canceled, the name will destroy
* itself.
*/
/* XXX TODO: cancel fetches */
/* if no fetches, kill! */
}
}
}
/*
* Assumes the name bucket is locked.
*/
static void
{
int addr_bucket;
/*
* Clean up the entry if needed.
*/
if (addr_bucket != DNS_ADB_INVALIDBUCKET)
}
}
/*
* Free the namehook
*/
}
if (addr_bucket != DNS_ADB_INVALIDBUCKET)
}
/*
* Assumes the name bucket is locked.
*/
static void
{
/*
* Unlink the handle from the name, letting the caller
* call dns_adb_done() on it to clean it up later.
*/
}
}
static inline void
{
if (lock)
if (lock)
}
static inline void
{
if (lock)
if (lock)
}
static inline void
{
if (lock)
if (lock)
}
static inline void
{
if (lock)
if (lock)
}
static inline void
{
int bucket;
if (lock)
if (lock)
}
static inline void
{
int bucket;
if (lock)
}
if (lock)
if (!destroy_entry)
return;
}
static inline dns_adbname_t *
{
return (NULL);
return (NULL);
}
name->expire_time = 0;
return (name);
}
static inline void
{
dns_adbname_t *n;
n = *name;
n->magic = 0;
}
static inline dns_adbnamehook_t *
{
return (NULL);
return (nh);
}
static inline void
{
}
static inline dns_adbzoneinfo_t *
{
return (NULL);
return (NULL);
}
zi->lame_timer = 0;
return (zi);
}
static inline void
{
}
static inline dns_adbentry_t *
{
dns_adbentry_t *e;
isc_uint32_t r;
if (e == NULL)
return (NULL);
e->magic = DNS_ADBENTRY_MAGIC;
e->refcnt = 0;
e->flags = 0;
e->goodness = 0;
ISC_LIST_INIT(e->zoneinfo);
ISC_LINK_INIT(e, link);
return (e);
}
static inline void
{
dns_adbentry_t *e;
e = *entry;
e->magic = 0;
}
}
static inline dns_adbhandle_t *
{
dns_adbhandle_t *h;
if (h == NULL)
return (NULL);
/*
* public members
*/
h->magic = 0;
h->query_pending = ISC_FALSE;
h->partial_result = ISC_FALSE;
h->result = ISC_R_UNEXPECTED;
ISC_LIST_INIT(h->list);
ISC_LINK_INIT(h, next);
/*
* private members
*/
if (result != ISC_R_SUCCESS) {
"isc_mutex_init failed in new_adbhandle()");
return (NULL);
}
ISC_LINK_INIT(h, link);
h->magic = DNS_ADBHANDLE_MAGIC;
return (h);
}
static inline void
{
}
/*
* Copy bits from the entry into the newly allocated addrinfo. The entry
* must be locked, and the reference count must be bumped up by one
* if this function returns a valid pointer.
*/
static inline dns_adbaddrinfo_t *
{
return (NULL);
return (ai);
}
static inline void
{
}
/*
* Search for the name. NOTE: The bucket is kept locked on both
* success and failure, so it must always be unlocked by the caller!
*
* On the first call to this function, *bucketp must be set to
* DNS_ADB_INVALIDBUCKET.
*/
static inline dns_adbname_t *
{
int bucket;
if (*bucketp == DNS_ADB_INVALIDBUCKET) {
}
return (adbname);
}
return (NULL);
}
/*
* Search for the address. NOTE: The bucket is kept locked on both
* success and failure, so it must always be unlocked by the caller.
*
* On the first call to this function, *bucketp must be set to
* DNS_ADB_INVALIDBUCKET. This will cause a lock to occur. On
* later calls (within the same "lock path") it can be left alone, so
* if this function is called multiple times locking is only done if
* the bucket changes.
*/
static inline dns_adbentry_t *
{
int bucket;
if (*bucketp == DNS_ADB_INVALIDBUCKET) {
}
return (entry);
}
return (NULL);
}
/*
* Entry bucket MUST be locked!
*/
static isc_boolean_t
{
return (ISC_FALSE);
/*
* Has the entry expired?
*/
}
/*
* Order tests from least to most expensive.
*/
}
}
return (is_bad);
}
static void
{
int bucket;
goto next;
goto out;
}
/*
* Found a valid entry. Add it to the handle's list.
*/
next:
}
out:
if (bucket != DNS_ADB_INVALIDBUCKET)
}
static void
{
}
/*
* Public functions.
*/
{
int i;
return (ISC_R_NOMEMORY);
/*
* Initialize things here that cannot fail, and especially things
* that must be NULL for the error return to work properly.
*/
if (result != ISC_R_SUCCESS)
goto fail0a;
if (result != ISC_R_SUCCESS)
goto fail0b;
if (result != ISC_R_SUCCESS)
goto fail0c;
if (result != ISC_R_SUCCESS)
goto fail0d;
/*
* Initialize the bucket locks for names and elements.
* May as well initialize the list heads, too.
*/
if (result != ISC_R_SUCCESS)
goto fail1;
for (i = 0 ; i < DNS_ADBNAMELIST_LENGTH ; i++) {
adb->name_refcnt[i] = 0;
}
for (i = 0 ; i < DNS_ADBENTRYLIST_LENGTH ; i++)
if (result != ISC_R_SUCCESS)
goto fail2;
/*
* Memory pools
*/
#define MPINIT(t, p, l, n) do { \
if (result != ISC_R_SUCCESS) \
goto fail3; \
isc_mempool_setfreemax((p), FREE_ITEMS); \
isc_mempool_setfillcount((p), FILL_COUNT); \
isc_mempool_setname((p), n); \
if (l) \
} while (0)
/*
* Normal return.
*/
return (ISC_R_SUCCESS);
fail3: /* clean up entrylocks */
fail2: /* clean up namelocks */
fail1: /* clean up only allocated memory */
return (result);
}
void
{
/*
* If all lists are empty, destroy the memory used by this
* adb.
*/
do {
if (!done)
} while (!done);
}
{
int bucket;
}
if (now == 0) {
if (result != ISC_R_SUCCESS)
return (result);
}
/*
* Look up the name in our internal database.
*
* Possibilities: Note that these are not always exclusive.
*
* No name found. In this case, allocate a new name header and
* an initial namehook or two. If any of these allocations
* fail, clean up and return ISC_R_NOMEMORY.
*
* Name found, valid addresses present. Allocate one addrinfo
* structure for each found and append it to the linked list
* of addresses for this header.
*
* Name found, queries pending. In this case, if a task was
* passed in, allocate a job id, attach it to the name's job
* list and remember to tell the caller that there will be
* more info coming later.
*/
goto out;
}
/*
* Look things up in our database first.
*/
goto fail;
}
/*
* Found! Run through the name and copy out the bits we are
* interested in. If we cannot copy at least one address, return
* ISC_R_NOMEMORY, otherwise copy out what we can and set the
* missing_data bit in the header.
*/
goto fail;
}
/*
* Attach to the name's query list if there are queries
* already running.
*/
}
goto out;
}
/*
* Nothing found. Allocate a new adbname structure for this name
* and look in the database for details. If the database has
* nothing useful, start a fetch if we can.
*/
goto fail;
}
/*
* If this function returns ISC_R_SUCCESS at least ONE new bit
* can ever be found it will return DNS_R_NOMEMORY more than likely.
*/
if (result == ISC_R_SUCCESS) {
if (adbname->partial_result)
goto again;
}
/*
* If anything other than success is returned, free the name
* (since it will have nothing useful in it) and return via the
* failure return.
*/
fail:
/*
* If the name isn't on a list it means we allocated it here, and it
* should be killed.
*/
}
/*
* "goto out" if the handle will be returned to the caller. This
* is a non-fatal return, since it will give the caller a handle
* at the very least.
*/
out:
if (attach_to_task) {
}
}
if (bucket != DNS_ADB_INVALIDBUCKET)
return (result);
}
{
int name_bucket;
/*
* Find the name.
*/
return (ISC_R_NOTFOUND);
}
/*
* If any handles are attached to this name, notify them that things
* are going away by canceling their requests.
*/
/* XXX TODO:
* If fetches are running for this name, cancel them all.
*/
/*
* Loop through the name and kill any namehooks and entries they
* point to.
*/
if (name_bucket != DNS_ADB_INVALIDBUCKET)
if (decr_adbrefcnt)
return (DNS_R_SUCCESS);
}
{
/*
* 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.
*/
goto out;
}
}
/*
* 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.
*/
/*
* Case (1): new entry and namehook.
*/
goto out;
}
}
/*
* Case (3): entry exists, we're linked.
*/
goto out;
}
}
/*
* Case (2): New namehook, link to entry from above.
*/
goto out;
}
/*
* If needed, string up the name and entry.
*/
}
return (ISC_R_SUCCESS);
out:
if (free_name)
if (free_entry)
if (free_namehook)
if (name_bucket != DNS_ADB_INVALIDBUCKET)
if (addr_bucket != DNS_ADB_INVALIDBUCKET)
return (result);
}
void
{
int bucket;
if (bucket == DNS_ADB_INVALIDBUCKET)
goto cleanup;
/*
* We need to get the adbname's lock to unlink the handle.
*/
if (bucket != DNS_ADB_INVALIDBUCKET) {
}
/*
* The handle doesn't exist on any list, and nothing is locked.
* Return the handle to the memory pool, and decrement the adb's
* reference count.
*/
}
}
void
{
int i;
char tmp[512];
const char *tmpp;
/*
* Lock the adb itself, lock all the name buckets, then lock all
* the entry buckets. This should put the adb into a state where
* nothing can change, so we can iterate through everything and
* print at our leasure.
*/
for (i = 0 ; i < DNS_ADBNAMELIST_LENGTH ; i++)
for (i = 0 ; i < DNS_ADBENTRYLIST_LENGTH ; i++)
/*
* Dump the names
*/
fprintf(f, "Names:\n");
for (i = 0 ; i < DNS_ADBNAMELIST_LENGTH ; i++) {
continue;
fprintf(f, "Name bucket %d:\n", i);
if (!DNS_ADBNAME_VALID(name))
fprintf(f, "\t");
fprintf(f, "\n");
print_namehook_list(f, name);
print_handle_list(f, name);
fprintf(f, "\n");
}
}
/*
* Dump the entries
*/
fprintf(f, "Entries:\n");
for (i = 0 ; i < DNS_ADBENTRYLIST_LENGTH ; i++) {
continue;
fprintf(f, "Entry bucket %d:\n", i);
if (!DNS_ADBENTRY_VALID(entry))
if (entry->lock_bucket != i)
fprintf(f, "\tWRONG BUCKET! lock_bucket %d\n",
entry->lock_bucket);
case AF_INET:
break;
case AF_INET6:
break;
default:
tmpp = "UnkFamily";
}
tmpp = "CANNOT TRANSLATE ADDRESS!";
fprintf(f, "\trefcnt %u flags %08x goodness %d"
" srtt %u addr %s\n",
}
}
/*
* Unlock everything
*/
for (i = 0 ; i < DNS_ADBENTRYLIST_LENGTH ; i++)
for (i = 0 ; i < DNS_ADBNAMELIST_LENGTH ; i++)
}
void
{
char tmp[512];
const char *tmpp;
/*
* Not used currently, in the API Just In Case we
*/
(void)adb;
fprintf(f, "\tquery_pending %d, result %d (%s)\n",
fprintf(f, "\tname_bucket %d, name %p, event sender %p\n",
fprintf(f, "\tAddresses:\n");
case AF_INET:
break;
case AF_INET6:
break;
default:
tmpp = "UnkFamily";
}
tmpp = "CANNOT TRANSLATE ADDRESS!";
fprintf(f, "\t\tentry %p, flags %08x goodness %d"
" srtt %u addr %s\n",
}
}
static void
{
char buf[257];
isc_buffer_t b;
}
static void
{
}
}
static void
{
}
}
/*
* On entry, "bucket" refers to a locked name bucket, "handle" is not NULL,
* and "name" is the name we are looking for. We will allocate an adbname
* and return a pointer to it in *adbnamep.
*
* If we return ISC_R_SUCCESS, the new name will have been allocated, and
* perhaps some namehooks will have been filled in with valid entries, and
* perhaps some fetches have been started.
*/
static isc_result_t
{
int addr_bucket;
return (ISC_R_NOTIMPLEMENTED);
switch (result) {
case DNS_R_GLUE:
case DNS_R_HINT:
case DNS_R_SUCCESS:
while (result == ISC_R_SUCCESS) {
goto fail;
}
&addr_bucket);
if (foundentry == NULL) {
goto fail;
}
} else {
foundentry->refcnt++;
}
}
}
fail:
if (addr_bucket != DNS_ADB_INVALIDBUCKET)
if (return_success)
return (ISC_R_SUCCESS);
return (result);
}
{
int bucket;
return (ISC_R_NOMEMORY);
return (ISC_R_SUCCESS);
}
void
int goodness_adjustment)
{
int bucket;
int old_goodness, new_goodness;
if (goodness_adjustment == 0)
return;
if (goodness_adjustment > 0) {
else
} else {
else
}
}
void
{
int bucket;
unsigned int new_srtt;
if (factor == 0)
factor = 4;
}