4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
21 * Id: dbtable.c,v 1.33 2007/06/19 23:47:16 tbox Exp
26 * Principal Author: DCL
32 #include <isc/rwlock.h>
35 #include <dns/dbtable.h>
38 #include <dns/result.h>
44 dns_rdataclass_t rdclass
;
46 isc_rwlock_t tree_lock
;
48 unsigned int references
;
49 /* Locked by tree_lock. */
51 dns_db_t
* default_db
;
54 #define DBTABLE_MAGIC ISC_MAGIC('D', 'B', '-', '-')
55 #define VALID_DBTABLE(dbtable) ISC_MAGIC_VALID(dbtable, DBTABLE_MAGIC)
58 dbdetach(void *data
, void *arg
) {
67 dns_dbtable_create(isc_mem_t
*mctx
, dns_rdataclass_t rdclass
,
68 dns_dbtable_t
**dbtablep
)
70 dns_dbtable_t
*dbtable
;
73 REQUIRE(mctx
!= NULL
);
74 REQUIRE(dbtablep
!= NULL
&& *dbtablep
== NULL
);
76 dbtable
= (dns_dbtable_t
*)isc_mem_get(mctx
, sizeof(*dbtable
));
78 return (ISC_R_NOMEMORY
);
81 result
= dns_rbt_create(mctx
, dbdetach
, NULL
, &dbtable
->rbt
);
82 if (result
!= ISC_R_SUCCESS
)
85 result
= isc_mutex_init(&dbtable
->lock
);
86 if (result
!= ISC_R_SUCCESS
)
89 result
= isc_rwlock_init(&dbtable
->tree_lock
, 0, 0);
90 if (result
!= ISC_R_SUCCESS
)
93 dbtable
->default_db
= NULL
;
95 dbtable
->rdclass
= rdclass
;
96 dbtable
->magic
= DBTABLE_MAGIC
;
97 dbtable
->references
= 1;
101 return (ISC_R_SUCCESS
);
104 DESTROYLOCK(&dbtable
->lock
);
107 dns_rbt_destroy(&dbtable
->rbt
);
110 isc_mem_put(mctx
, dbtable
, sizeof(*dbtable
));
116 dbtable_free(dns_dbtable_t
*dbtable
) {
118 * Caller must ensure that it is safe to call.
121 RWLOCK(&dbtable
->tree_lock
, isc_rwlocktype_write
);
123 if (dbtable
->default_db
!= NULL
)
124 dns_db_detach(&dbtable
->default_db
);
126 dns_rbt_destroy(&dbtable
->rbt
);
128 RWUNLOCK(&dbtable
->tree_lock
, isc_rwlocktype_write
);
130 isc_rwlock_destroy(&dbtable
->tree_lock
);
134 isc_mem_put(dbtable
->mctx
, dbtable
, sizeof(*dbtable
));
138 dns_dbtable_attach(dns_dbtable_t
*source
, dns_dbtable_t
**targetp
) {
139 REQUIRE(VALID_DBTABLE(source
));
140 REQUIRE(targetp
!= NULL
&& *targetp
== NULL
);
144 INSIST(source
->references
> 0);
145 source
->references
++;
146 INSIST(source
->references
!= 0);
148 UNLOCK(&source
->lock
);
154 dns_dbtable_detach(dns_dbtable_t
**dbtablep
) {
155 dns_dbtable_t
*dbtable
;
156 isc_boolean_t free_dbtable
= ISC_FALSE
;
158 REQUIRE(dbtablep
!= NULL
);
160 REQUIRE(VALID_DBTABLE(dbtable
));
162 LOCK(&dbtable
->lock
);
164 INSIST(dbtable
->references
> 0);
165 dbtable
->references
--;
166 if (dbtable
->references
== 0)
167 free_dbtable
= ISC_TRUE
;
169 UNLOCK(&dbtable
->lock
);
172 dbtable_free(dbtable
);
178 dns_dbtable_add(dns_dbtable_t
*dbtable
, dns_db_t
*db
) {
182 REQUIRE(VALID_DBTABLE(dbtable
));
183 REQUIRE(dns_db_class(db
) == dbtable
->rdclass
);
186 dns_db_attach(db
, &clone
);
188 RWLOCK(&dbtable
->tree_lock
, isc_rwlocktype_write
);
189 result
= dns_rbt_addname(dbtable
->rbt
, dns_db_origin(clone
), clone
);
190 RWUNLOCK(&dbtable
->tree_lock
, isc_rwlocktype_write
);
196 dns_dbtable_remove(dns_dbtable_t
*dbtable
, dns_db_t
*db
) {
197 dns_db_t
*stored_data
= NULL
;
201 REQUIRE(VALID_DBTABLE(dbtable
));
203 name
= dns_db_origin(db
);
206 * There is a requirement that the association of name with db
207 * be verified. With the current rbt.c this is expensive to do,
208 * because effectively two find operations are being done, but
209 * deletion is relatively infrequent.
210 * XXXDCL ... this could be cheaper now with dns_rbt_deletenode.
213 RWLOCK(&dbtable
->tree_lock
, isc_rwlocktype_write
);
215 result
= dns_rbt_findname(dbtable
->rbt
, name
, 0, NULL
,
216 (void **) (void *)&stored_data
);
218 if (result
== ISC_R_SUCCESS
) {
219 INSIST(stored_data
== db
);
221 (void)dns_rbt_deletename(dbtable
->rbt
, name
, ISC_FALSE
);
224 RWUNLOCK(&dbtable
->tree_lock
, isc_rwlocktype_write
);
228 dns_dbtable_adddefault(dns_dbtable_t
*dbtable
, dns_db_t
*db
) {
229 REQUIRE(VALID_DBTABLE(dbtable
));
230 REQUIRE(dbtable
->default_db
== NULL
);
231 REQUIRE(dns_name_compare(dns_db_origin(db
), dns_rootname
) == 0);
233 RWLOCK(&dbtable
->tree_lock
, isc_rwlocktype_write
);
235 dbtable
->default_db
= NULL
;
236 dns_db_attach(db
, &dbtable
->default_db
);
238 RWUNLOCK(&dbtable
->tree_lock
, isc_rwlocktype_write
);
242 dns_dbtable_getdefault(dns_dbtable_t
*dbtable
, dns_db_t
**dbp
) {
243 REQUIRE(VALID_DBTABLE(dbtable
));
244 REQUIRE(dbp
!= NULL
&& *dbp
== NULL
);
246 RWLOCK(&dbtable
->tree_lock
, isc_rwlocktype_read
);
248 dns_db_attach(dbtable
->default_db
, dbp
);
250 RWUNLOCK(&dbtable
->tree_lock
, isc_rwlocktype_read
);
254 dns_dbtable_removedefault(dns_dbtable_t
*dbtable
) {
255 REQUIRE(VALID_DBTABLE(dbtable
));
257 RWLOCK(&dbtable
->tree_lock
, isc_rwlocktype_write
);
259 dns_db_detach(&dbtable
->default_db
);
261 RWUNLOCK(&dbtable
->tree_lock
, isc_rwlocktype_write
);
265 dns_dbtable_find(dns_dbtable_t
*dbtable
, dns_name_t
*name
,
266 unsigned int options
, dns_db_t
**dbp
)
268 dns_db_t
*stored_data
= NULL
;
270 unsigned int rbtoptions
= 0;
272 REQUIRE(dbp
!= NULL
&& *dbp
== NULL
);
274 if ((options
& DNS_DBTABLEFIND_NOEXACT
) != 0)
275 rbtoptions
|= DNS_RBTFIND_NOEXACT
;
277 RWLOCK(&dbtable
->tree_lock
, isc_rwlocktype_read
);
279 result
= dns_rbt_findname(dbtable
->rbt
, name
, rbtoptions
, NULL
,
280 (void **) (void *)&stored_data
);
282 if (result
== ISC_R_SUCCESS
|| result
== DNS_R_PARTIALMATCH
)
283 dns_db_attach(stored_data
, dbp
);
284 else if (dbtable
->default_db
!= NULL
) {
285 dns_db_attach(dbtable
->default_db
, dbp
);
286 result
= DNS_R_PARTIALMATCH
;
288 result
= ISC_R_NOTFOUND
;
290 RWUNLOCK(&dbtable
->tree_lock
, isc_rwlocktype_read
);