1999-03-18 19:37:30 +00:00
|
|
|
/*
|
2000-02-03 23:50:32 +00:00
|
|
|
* Copyright (C) 1999, 2000 Internet Software Consortium.
|
1999-03-18 19:37:30 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
/*
|
2000-04-19 18:27:24 +00:00
|
|
|
* $Id: dbtable.c,v 1.15 2000/04/19 18:23:30 halley Exp $
|
1999-04-14 02:37:08 +00:00
|
|
|
*/
|
1999-03-18 21:21:31 +00:00
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
/*
|
|
|
|
* Principal Author: DCL
|
|
|
|
*/
|
1999-03-18 21:21:31 +00:00
|
|
|
|
1999-03-18 19:37:30 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <isc/assertions.h>
|
|
|
|
#include <isc/rwlock.h>
|
1999-12-16 22:24:22 +00:00
|
|
|
#include <isc/util.h>
|
1999-03-18 19:37:30 +00:00
|
|
|
|
|
|
|
#include <dns/dbtable.h>
|
1999-04-14 02:37:08 +00:00
|
|
|
#include <dns/db.h>
|
1999-03-18 19:37:30 +00:00
|
|
|
#include <dns/rbt.h>
|
|
|
|
|
|
|
|
struct dns_dbtable {
|
1999-03-18 20:06:26 +00:00
|
|
|
/* Unlocked. */
|
1999-03-18 19:37:30 +00:00
|
|
|
unsigned int magic;
|
|
|
|
isc_mem_t * mctx;
|
1999-04-20 22:26:50 +00:00
|
|
|
dns_rdataclass_t rdclass;
|
1999-05-11 23:18:37 +00:00
|
|
|
isc_mutex_t lock;
|
1999-03-18 19:37:30 +00:00
|
|
|
isc_rwlock_t tree_lock;
|
1999-05-11 23:18:37 +00:00
|
|
|
/* Locked by lock. */
|
|
|
|
unsigned int references;
|
1999-03-18 20:06:26 +00:00
|
|
|
/* Locked by tree_lock. */
|
1999-03-18 19:37:30 +00:00
|
|
|
dns_rbt_t * rbt;
|
1999-04-14 02:37:08 +00:00
|
|
|
dns_db_t * default_db;
|
1999-03-18 19:37:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define DBTABLE_MAGIC 0x44422D2DU /* DB--. */
|
|
|
|
#define VALID_DBTABLE(dbtable) ((dbtable) != NULL && \
|
|
|
|
(dbtable)->magic == DBTABLE_MAGIC)
|
|
|
|
|
1999-09-22 18:24:05 +00:00
|
|
|
static void
|
|
|
|
dbdetach(void *data, void *arg) {
|
|
|
|
dns_db_t *db = data;
|
|
|
|
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
dns_db_detach(&db);
|
|
|
|
}
|
|
|
|
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t
|
1999-04-20 22:26:50 +00:00
|
|
|
dns_dbtable_create(isc_mem_t *mctx, dns_rdataclass_t rdclass,
|
|
|
|
dns_dbtable_t **dbtablep)
|
|
|
|
{
|
1999-03-18 19:37:30 +00:00
|
|
|
dns_dbtable_t *dbtable;
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t result;
|
1999-05-11 23:18:37 +00:00
|
|
|
isc_result_t iresult;
|
1999-03-18 19:37:30 +00:00
|
|
|
|
|
|
|
REQUIRE(mctx != NULL);
|
|
|
|
REQUIRE(dbtablep != NULL && *dbtablep == NULL);
|
|
|
|
|
|
|
|
dbtable = (dns_dbtable_t *)isc_mem_get(mctx, sizeof(*dbtable));
|
|
|
|
if (dbtable == NULL)
|
2000-04-06 22:03:35 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-05-25 18:41:52 +00:00
|
|
|
dbtable->rbt = NULL;
|
1999-09-22 18:24:05 +00:00
|
|
|
result = dns_rbt_create(mctx, dbdetach, NULL, &dbtable->rbt);
|
2000-04-06 22:03:35 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
1999-05-11 23:18:37 +00:00
|
|
|
goto clean1;
|
|
|
|
|
|
|
|
iresult = isc_mutex_init(&dbtable->lock);
|
|
|
|
if (iresult != ISC_R_SUCCESS) {
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"isc_lock_init() failed: %s",
|
|
|
|
isc_result_totext(result));
|
2000-04-06 22:03:35 +00:00
|
|
|
result = ISC_R_UNEXPECTED;
|
1999-05-11 23:18:37 +00:00
|
|
|
goto clean2;
|
1999-03-18 19:37:30 +00:00
|
|
|
}
|
|
|
|
|
1999-05-11 23:18:37 +00:00
|
|
|
iresult = isc_rwlock_init(&dbtable->tree_lock, 0, 0);
|
|
|
|
if (iresult != ISC_R_SUCCESS) {
|
1999-03-18 19:37:30 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"isc_rwlock_init() failed: %s",
|
1999-04-20 22:26:50 +00:00
|
|
|
isc_result_totext(result));
|
2000-04-06 22:03:35 +00:00
|
|
|
result = ISC_R_UNEXPECTED;
|
1999-05-11 23:18:37 +00:00
|
|
|
goto clean3;
|
1999-03-18 19:37:30 +00:00
|
|
|
}
|
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
dbtable->default_db = NULL;
|
1999-03-18 19:37:30 +00:00
|
|
|
dbtable->mctx = mctx;
|
1999-04-20 22:26:50 +00:00
|
|
|
dbtable->rdclass = rdclass;
|
1999-03-18 19:37:30 +00:00
|
|
|
dbtable->magic = DBTABLE_MAGIC;
|
1999-05-11 23:18:37 +00:00
|
|
|
dbtable->references = 1;
|
1999-03-18 19:37:30 +00:00
|
|
|
|
|
|
|
*dbtablep = dbtable;
|
|
|
|
|
2000-04-06 22:03:35 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-05-11 23:18:37 +00:00
|
|
|
clean3:
|
|
|
|
(void)isc_mutex_destroy(&dbtable->lock);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-05-11 23:18:37 +00:00
|
|
|
clean2:
|
|
|
|
dns_rbt_destroy(&dbtable->rbt);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-05-11 23:18:37 +00:00
|
|
|
clean1:
|
|
|
|
isc_mem_put(mctx, dbtable, sizeof(*dbtable));
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
dbtable_free(dns_dbtable_t *dbtable) {
|
|
|
|
/*
|
|
|
|
* Caller must ensure that it is safe to
|
|
|
|
* call.
|
|
|
|
*/
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
RWLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
|
|
|
|
|
|
|
if (dbtable->default_db != NULL)
|
|
|
|
dns_db_detach(&dbtable->default_db);
|
|
|
|
|
1999-03-18 19:37:30 +00:00
|
|
|
dns_rbt_destroy(&dbtable->rbt);
|
1999-04-14 02:37:08 +00:00
|
|
|
|
|
|
|
RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
|
|
|
isc_rwlock_destroy(&dbtable->tree_lock);
|
|
|
|
|
|
|
|
dbtable->magic = 0;
|
|
|
|
|
|
|
|
isc_mem_put(dbtable->mctx, dbtable, sizeof(*dbtable));
|
1999-05-11 23:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dns_dbtable_attach(dns_dbtable_t *source, dns_dbtable_t **targetp) {
|
|
|
|
REQUIRE(VALID_DBTABLE(source));
|
|
|
|
REQUIRE(targetp != NULL && *targetp == NULL);
|
|
|
|
|
|
|
|
LOCK(&source->lock);
|
|
|
|
|
|
|
|
INSIST(source->references > 0);
|
|
|
|
source->references++;
|
|
|
|
INSIST(source->references != 0);
|
|
|
|
|
|
|
|
UNLOCK(&source->lock);
|
|
|
|
|
|
|
|
*targetp = source;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dns_dbtable_detach(dns_dbtable_t **dbtablep) {
|
|
|
|
dns_dbtable_t *dbtable;
|
|
|
|
isc_boolean_t free_dbtable = ISC_FALSE;
|
|
|
|
|
|
|
|
REQUIRE(dbtablep != NULL);
|
|
|
|
dbtable = *dbtablep;
|
|
|
|
REQUIRE(VALID_DBTABLE(dbtable));
|
|
|
|
|
|
|
|
LOCK(&dbtable->lock);
|
|
|
|
|
|
|
|
INSIST(dbtable->references > 0);
|
|
|
|
dbtable->references--;
|
|
|
|
if (dbtable->references == 0)
|
|
|
|
free_dbtable = ISC_TRUE;
|
|
|
|
|
|
|
|
UNLOCK(&dbtable->lock);
|
|
|
|
|
|
|
|
if (free_dbtable)
|
|
|
|
dbtable_free(dbtable);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
|
|
|
*dbtablep = NULL;
|
|
|
|
}
|
|
|
|
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t
|
1999-04-14 02:37:08 +00:00
|
|
|
dns_dbtable_add(dns_dbtable_t *dbtable, dns_db_t *db) {
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t result;
|
1999-04-14 02:37:08 +00:00
|
|
|
dns_db_t *clone;
|
1999-03-18 19:37:30 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_DBTABLE(dbtable));
|
1999-04-20 22:26:50 +00:00
|
|
|
REQUIRE(dns_db_class(db) == dbtable->rdclass);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
clone = NULL;
|
|
|
|
dns_db_attach(db, &clone);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
RWLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
|
|
|
result = dns_rbt_addname(dbtable->rbt, dns_db_origin(clone), clone);
|
|
|
|
RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
return (result);
|
1999-03-18 19:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-04-14 02:37:08 +00:00
|
|
|
dns_dbtable_remove(dns_dbtable_t *dbtable, dns_db_t *db) {
|
|
|
|
dns_db_t *stored_data = NULL;
|
1999-03-18 19:37:30 +00:00
|
|
|
isc_result_t result;
|
1999-04-14 02:37:08 +00:00
|
|
|
dns_name_t *name;
|
1999-03-18 19:37:30 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_DBTABLE(dbtable));
|
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
name = dns_db_origin(db);
|
|
|
|
|
1999-03-18 19:37:30 +00:00
|
|
|
/*
|
|
|
|
* There is a requirement that the association of name with db
|
|
|
|
* be verified. With the current rbt.c this is expensive to do,
|
|
|
|
* because effectively two find operations are being done, but
|
|
|
|
* deletion is relatively infrequent.
|
|
|
|
*/
|
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
RWLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
|
|
|
|
2000-04-19 18:27:24 +00:00
|
|
|
result = dns_rbt_findname(dbtable->rbt, name, 0, NULL,
|
1999-04-14 02:37:08 +00:00
|
|
|
(void **)&stored_data);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
2000-04-06 22:03:35 +00:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
1999-04-14 12:29:39 +00:00
|
|
|
INSIST(stored_data == db);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-04-14 12:29:39 +00:00
|
|
|
dns_rbt_deletename(dbtable->rbt, name, ISC_FALSE);
|
|
|
|
}
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
1999-03-18 19:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dns_dbtable_adddefault(dns_dbtable_t *dbtable, dns_db_t *db) {
|
|
|
|
REQUIRE(VALID_DBTABLE(dbtable));
|
1999-04-14 02:37:08 +00:00
|
|
|
REQUIRE(dbtable->default_db == NULL);
|
|
|
|
REQUIRE(dns_name_compare(dns_db_origin(db), dns_rootname) == 0);
|
|
|
|
|
|
|
|
RWLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
dbtable->default_db = NULL;
|
|
|
|
dns_db_attach(db, &dbtable->default_db);
|
|
|
|
|
|
|
|
RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
1999-03-18 19:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-04-14 02:37:08 +00:00
|
|
|
dns_dbtable_getdefault(dns_dbtable_t *dbtable, dns_db_t **dbp) {
|
1999-03-18 19:37:30 +00:00
|
|
|
REQUIRE(VALID_DBTABLE(dbtable));
|
1999-04-14 02:37:08 +00:00
|
|
|
REQUIRE(dbp != NULL && *dbp == NULL);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
RWLOCK(&dbtable->tree_lock, isc_rwlocktype_read);
|
|
|
|
|
|
|
|
dns_db_attach(dbtable->default_db, dbp);
|
|
|
|
|
|
|
|
RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_read);
|
1999-03-18 19:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-04-14 02:37:08 +00:00
|
|
|
dns_dbtable_removedefault(dns_dbtable_t *dbtable) {
|
1999-03-18 19:37:30 +00:00
|
|
|
REQUIRE(VALID_DBTABLE(dbtable));
|
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
RWLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
|
|
|
|
|
|
|
dns_db_detach(&dbtable->default_db);
|
|
|
|
|
|
|
|
RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
1999-03-18 19:37:30 +00:00
|
|
|
}
|
|
|
|
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t
|
1999-03-18 19:37:30 +00:00
|
|
|
dns_dbtable_find(dns_dbtable_t *dbtable, dns_name_t *name, dns_db_t **dbp) {
|
1999-04-14 02:37:08 +00:00
|
|
|
dns_db_t *stored_data = NULL;
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t result;
|
1999-03-18 19:37:30 +00:00
|
|
|
|
|
|
|
REQUIRE(dbp != NULL && *dbp == NULL);
|
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
RWLOCK(&dbtable->tree_lock, isc_rwlocktype_read);
|
|
|
|
|
2000-04-19 18:27:24 +00:00
|
|
|
result = dns_rbt_findname(dbtable->rbt, name, 0, NULL,
|
1999-04-14 02:37:08 +00:00
|
|
|
(void **)&stored_data);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
2000-04-06 22:03:35 +00:00
|
|
|
if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
|
1999-04-14 02:37:08 +00:00
|
|
|
dns_db_attach(stored_data, dbp);
|
|
|
|
else if (dbtable->default_db != NULL) {
|
|
|
|
dns_db_attach(dbtable->default_db, dbp);
|
|
|
|
result = DNS_R_PARTIALMATCH;
|
|
|
|
} else
|
2000-04-06 22:03:35 +00:00
|
|
|
result = ISC_R_NOTFOUND;
|
1999-04-14 02:37:08 +00:00
|
|
|
|
|
|
|
RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_read);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|