Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / dns / zt.c
blob9efdee7caffd5a159cdbbcd850d7526644300d3c
1 /*
2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: zt.c,v 1.33.2.3 2004/04/15 01:38:09 marka Exp $ */
20 #include <config.h>
22 #include <isc/magic.h>
23 #include <isc/mem.h>
24 #include <isc/util.h>
26 #include <dns/rbt.h>
27 #include <dns/result.h>
28 #include <dns/zone.h>
29 #include <dns/zt.h>
31 struct dns_zt {
32 /* Unlocked. */
33 unsigned int magic;
34 isc_mem_t *mctx;
35 dns_rdataclass_t rdclass;
36 isc_rwlock_t rwlock;
37 /* Locked by lock. */
38 isc_uint32_t references;
39 dns_rbt_t *table;
42 #define ZTMAGIC ISC_MAGIC('Z', 'T', 'b', 'l')
43 #define VALID_ZT(zt) ISC_MAGIC_VALID(zt, ZTMAGIC)
45 static void
46 auto_detach(void *, void *);
48 static isc_result_t
49 load(dns_zone_t *zone, void *uap);
51 static isc_result_t
52 loadnew(dns_zone_t *zone, void *uap);
54 isc_result_t
55 dns_zt_create(isc_mem_t *mctx, dns_rdataclass_t rdclass, dns_zt_t **ztp) {
56 dns_zt_t *zt;
57 isc_result_t result;
59 REQUIRE(ztp != NULL && *ztp == NULL);
61 zt = isc_mem_get(mctx, sizeof *zt);
62 if (zt == NULL)
63 return (ISC_R_NOMEMORY);
65 zt->table = NULL;
66 result = dns_rbt_create(mctx, auto_detach, zt, &zt->table);
67 if (result != ISC_R_SUCCESS)
68 goto cleanup_zt;
70 result = isc_rwlock_init(&zt->rwlock, 0, 0);
71 if (result != ISC_R_SUCCESS) {
72 UNEXPECTED_ERROR(__FILE__, __LINE__,
73 "isc_rwlock_init() failed: %s",
74 isc_result_totext(result));
75 result = ISC_R_UNEXPECTED;
76 goto cleanup_rbt;
79 zt->mctx = mctx;
80 zt->references = 1;
81 zt->rdclass = rdclass;
82 zt->magic = ZTMAGIC;
83 *ztp = zt;
85 return (ISC_R_SUCCESS);
87 cleanup_rbt:
88 dns_rbt_destroy(&zt->table);
90 cleanup_zt:
91 isc_mem_put(mctx, zt, sizeof *zt);
93 return (result);
96 isc_result_t
97 dns_zt_mount(dns_zt_t *zt, dns_zone_t *zone) {
98 isc_result_t result;
99 dns_zone_t *dummy = NULL;
100 dns_name_t *name;
102 REQUIRE(VALID_ZT(zt));
104 name = dns_zone_getorigin(zone);
106 RWLOCK(&zt->rwlock, isc_rwlocktype_write);
108 result = dns_rbt_addname(zt->table, name, zone);
109 if (result == ISC_R_SUCCESS)
110 dns_zone_attach(zone, &dummy);
112 RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
114 return (result);
117 isc_result_t
118 dns_zt_unmount(dns_zt_t *zt, dns_zone_t *zone) {
119 isc_result_t result;
120 dns_name_t *name;
122 REQUIRE(VALID_ZT(zt));
124 name = dns_zone_getorigin(zone);
126 RWLOCK(&zt->rwlock, isc_rwlocktype_write);
128 result = dns_rbt_deletename(zt->table, name, ISC_FALSE);
130 RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
132 return (result);
135 isc_result_t
136 dns_zt_find(dns_zt_t *zt, dns_name_t *name, unsigned int options,
137 dns_name_t *foundname, dns_zone_t **zonep)
139 isc_result_t result;
140 dns_zone_t *dummy = NULL;
141 unsigned int rbtoptions = 0;
143 REQUIRE(VALID_ZT(zt));
145 if ((options & DNS_ZTFIND_NOEXACT) != 0)
146 rbtoptions |= DNS_RBTFIND_NOEXACT;
148 RWLOCK(&zt->rwlock, isc_rwlocktype_read);
150 result = dns_rbt_findname(zt->table, name, rbtoptions, foundname,
151 (void **) (void*)&dummy);
152 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
153 dns_zone_attach(dummy, zonep);
155 RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
157 return (result);
160 void
161 dns_zt_attach(dns_zt_t *zt, dns_zt_t **ztp) {
163 REQUIRE(VALID_ZT(zt));
164 REQUIRE(ztp != NULL && *ztp == NULL);
166 RWLOCK(&zt->rwlock, isc_rwlocktype_write);
168 INSIST(zt->references > 0);
169 zt->references++;
170 INSIST(zt->references != 0);
172 RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
174 *ztp = zt;
177 static isc_result_t
178 flush(dns_zone_t *zone, void *uap) {
179 UNUSED(uap);
180 return (dns_zone_flush(zone));
183 static void
184 zt_flushanddetach(dns_zt_t **ztp, isc_boolean_t need_flush) {
185 isc_boolean_t destroy = ISC_FALSE;
186 dns_zt_t *zt;
188 REQUIRE(ztp != NULL && VALID_ZT(*ztp));
190 zt = *ztp;
192 RWLOCK(&zt->rwlock, isc_rwlocktype_write);
194 INSIST(zt->references > 0);
195 zt->references--;
196 if (zt->references == 0)
197 destroy = ISC_TRUE;
199 RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
201 if (destroy) {
202 if (need_flush)
203 (void)dns_zt_apply(zt, ISC_FALSE, flush, NULL);
204 dns_rbt_destroy(&zt->table);
205 isc_rwlock_destroy(&zt->rwlock);
206 zt->magic = 0;
207 isc_mem_put(zt->mctx, zt, sizeof *zt);
210 *ztp = NULL;
213 void
214 dns_zt_flushanddetach(dns_zt_t **ztp) {
215 zt_flushanddetach(ztp, ISC_TRUE);
218 void
219 dns_zt_detach(dns_zt_t **ztp) {
220 zt_flushanddetach(ztp, ISC_FALSE);
223 isc_result_t
224 dns_zt_load(dns_zt_t *zt, isc_boolean_t stop) {
225 isc_result_t result;
227 REQUIRE(VALID_ZT(zt));
229 RWLOCK(&zt->rwlock, isc_rwlocktype_read);
230 result = dns_zt_apply(zt, stop, load, NULL);
231 RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
232 return (result);
235 static isc_result_t
236 load(dns_zone_t *zone, void *uap) {
237 UNUSED(uap);
238 return (dns_zone_load(zone));
241 isc_result_t
242 dns_zt_loadnew(dns_zt_t *zt, isc_boolean_t stop) {
243 isc_result_t result;
245 REQUIRE(VALID_ZT(zt));
247 RWLOCK(&zt->rwlock, isc_rwlocktype_read);
248 result = dns_zt_apply(zt, stop, loadnew, NULL);
249 RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
250 return (result);
253 static isc_result_t
254 loadnew(dns_zone_t *zone, void *uap) {
255 isc_result_t result;
256 UNUSED(uap);
257 result = dns_zone_loadnew(zone);
258 if (result == DNS_R_CONTINUE || result == DNS_R_UPTODATE ||
259 result == DNS_R_DYNAMIC)
260 result = ISC_R_SUCCESS;
261 return (result);
264 isc_result_t
265 dns_zt_apply(dns_zt_t *zt, isc_boolean_t stop,
266 isc_result_t (*action)(dns_zone_t *, void *), void *uap)
268 dns_rbtnode_t *node;
269 dns_rbtnodechain_t chain;
270 isc_result_t result;
271 dns_zone_t *zone;
273 REQUIRE(VALID_ZT(zt));
274 REQUIRE(action != NULL);
276 dns_rbtnodechain_init(&chain, zt->mctx);
277 result = dns_rbtnodechain_first(&chain, zt->table, NULL, NULL);
278 if (result == ISC_R_NOTFOUND) {
280 * The tree is empty.
282 result = ISC_R_NOMORE;
284 while (result == DNS_R_NEWORIGIN || result == ISC_R_SUCCESS) {
285 result = dns_rbtnodechain_current(&chain, NULL, NULL,
286 &node);
287 if (result == ISC_R_SUCCESS) {
288 zone = node->data;
289 if (zone != NULL)
290 result = (action)(zone, uap);
291 if (result != ISC_R_SUCCESS && stop)
292 goto cleanup; /* don't break */
294 result = dns_rbtnodechain_next(&chain, NULL, NULL);
296 if (result == ISC_R_NOMORE)
297 result = ISC_R_SUCCESS;
299 cleanup:
300 dns_rbtnodechain_invalidate(&chain);
302 return (result);
305 /***
306 *** Private
307 ***/
309 static void
310 auto_detach(void *data, void *arg) {
311 dns_zone_t *zone = data;
313 UNUSED(arg);
315 dns_zone_detach(&zone);