1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/*
* Copyright (C) 1999, 2000 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.
*/
/* $Id: zt.c,v 1.22 2000/06/22 21:54:58 tale Exp $ */
#include <config.h>
#include <isc/magic.h>
#include <isc/mem.h>
#include <isc/util.h>
#include <dns/rbt.h>
#include <dns/result.h>
#include <dns/zone.h>
#include <dns/zt.h>
struct dns_zt {
/* Unlocked. */
unsigned int magic;
isc_mem_t *mctx;
dns_rdataclass_t rdclass;
isc_rwlock_t rwlock;
/* Locked by lock. */
isc_uint32_t references;
dns_rbt_t *table;
};
#define ZTMAGIC 0x5a54626cU /* ZTbl */
#define VALID_ZT(zt) ISC_MAGIC_VALID(zt, ZTMAGIC)
static void
auto_detach(void *, void *);
static isc_result_t
load(dns_zone_t *zone, void *uap);
isc_result_t
dns_zt_create(isc_mem_t *mctx, dns_rdataclass_t rdclass, dns_zt_t **ztp) {
dns_zt_t *zt;
isc_result_t result;
REQUIRE(ztp != NULL && *ztp == NULL);
zt = isc_mem_get(mctx, sizeof *zt);
if (zt == NULL)
return (ISC_R_NOMEMORY);
zt->table = NULL;
result = dns_rbt_create(mctx, auto_detach, zt, &zt->table);
if (result != ISC_R_SUCCESS)
goto cleanup_zt;
result = isc_rwlock_init(&zt->rwlock, 0, 0);
if (result != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_rwlock_init() failed: %s",
isc_result_totext(result));
result = ISC_R_UNEXPECTED;
goto cleanup_rbt;
}
zt->mctx = mctx;
zt->references = 1;
zt->rdclass = rdclass;
zt->magic = ZTMAGIC;
*ztp = zt;
return (ISC_R_SUCCESS);
cleanup_rbt:
dns_rbt_destroy(&zt->table);
cleanup_zt:
isc_mem_put(mctx, zt, sizeof *zt);
return (result);
}
isc_result_t
dns_zt_mount(dns_zt_t *zt, dns_zone_t *zone) {
isc_result_t result;
dns_zone_t *dummy = NULL;
dns_name_t *name;
REQUIRE(VALID_ZT(zt));
name = dns_zone_getorigin(zone);
RWLOCK(&zt->rwlock, isc_rwlocktype_write);
result = dns_rbt_addname(zt->table, name, zone);
if (result == ISC_R_SUCCESS)
dns_zone_attach(zone, &dummy);
RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
return (result);
}
isc_result_t
dns_zt_unmount(dns_zt_t *zt, dns_zone_t *zone) {
isc_result_t result;
dns_name_t *name;
REQUIRE(VALID_ZT(zt));
name = dns_zone_getorigin(zone);
RWLOCK(&zt->rwlock, isc_rwlocktype_write);
result = dns_rbt_deletename(zt->table, name, ISC_FALSE);
RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
return (result);
}
isc_result_t
dns_zt_find(dns_zt_t *zt, dns_name_t *name, unsigned int options,
dns_name_t *foundname, dns_zone_t **zonep)
{
isc_result_t result;
dns_zone_t *dummy = NULL;
unsigned int rbtoptions = 0;
REQUIRE(VALID_ZT(zt));
if ((options & DNS_ZTFIND_NOEXACT) != 0)
rbtoptions |= DNS_RBTFIND_NOEXACT;
RWLOCK(&zt->rwlock, isc_rwlocktype_read);
result = dns_rbt_findname(zt->table, name, rbtoptions, foundname,
(void **)&dummy);
if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
dns_zone_attach(dummy, zonep);
RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
return (result);
}
void
dns_zt_attach(dns_zt_t *zt, dns_zt_t **ztp) {
REQUIRE(VALID_ZT(zt));
REQUIRE(ztp != NULL && *ztp == NULL);
RWLOCK(&zt->rwlock, isc_rwlocktype_write);
INSIST(zt->references > 0);
zt->references++;
INSIST(zt->references != 0);
RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
*ztp = zt;
}
void
dns_zt_detach(dns_zt_t **ztp) {
isc_boolean_t destroy = ISC_FALSE;
dns_zt_t *zt;
REQUIRE(ztp != NULL && VALID_ZT(*ztp));
zt = *ztp;
RWLOCK(&zt->rwlock, isc_rwlocktype_write);
INSIST(zt->references > 0);
zt->references--;
if (zt->references == 0)
destroy = ISC_TRUE;
RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
if (destroy) {
dns_rbt_destroy(&zt->table);
isc_rwlock_destroy(&zt->rwlock);
zt->magic = 0;
isc_mem_put(zt->mctx, zt, sizeof *zt);
}
*ztp = NULL;
}
void
dns_zt_print(dns_zt_t *zt) {
dns_rbtnode_t *node;
dns_rbtnodechain_t chain;
isc_result_t result;
dns_zone_t *zone;
REQUIRE(VALID_ZT(zt));
RWLOCK(&zt->rwlock, isc_rwlocktype_read);
dns_rbtnodechain_init(&chain, zt->mctx);
result = dns_rbtnodechain_first(&chain, zt->table, NULL, NULL);
while (result == DNS_R_NEWORIGIN || result == ISC_R_SUCCESS) {
result = dns_rbtnodechain_current(&chain, NULL, NULL,
&node);
if (result == ISC_R_SUCCESS) {
zone = node->data;
if (zone != NULL)
(void)dns_zone_print(zone);
}
result = dns_rbtnodechain_next(&chain, NULL, NULL);
}
dns_rbtnodechain_invalidate(&chain);
RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
}
isc_result_t
dns_zt_load(dns_zt_t *zt, isc_boolean_t stop) {
isc_result_t result;
RWLOCK(&zt->rwlock, isc_rwlocktype_read);
result = dns_zt_apply(zt, stop, load, NULL);
RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
return (result);
}
static isc_result_t
load(dns_zone_t *zone, void *uap) {
UNUSED(uap);
return (dns_zone_load(zone));
}
isc_result_t
dns_zt_apply(dns_zt_t *zt, isc_boolean_t stop,
isc_result_t (*action)(dns_zone_t *, void *), void *uap)
{
dns_rbtnode_t *node;
dns_rbtnodechain_t chain;
isc_result_t result;
dns_zone_t *zone;
REQUIRE(VALID_ZT(zt));
REQUIRE(action != NULL);
dns_rbtnodechain_init(&chain, zt->mctx);
result = dns_rbtnodechain_first(&chain, zt->table, NULL, NULL);
if (result == ISC_R_NOTFOUND) {
/*
* The tree is empty.
*/
result = ISC_R_NOMORE;
}
while (result == DNS_R_NEWORIGIN || result == ISC_R_SUCCESS) {
result = dns_rbtnodechain_current(&chain, NULL, NULL,
&node);
if (result == ISC_R_SUCCESS) {
zone = node->data;
if (zone != NULL)
result = (action)(zone, uap);
if (result != ISC_R_SUCCESS && stop)
goto cleanup; /* don't break */
}
result = dns_rbtnodechain_next(&chain, NULL, NULL);
}
if (result == ISC_R_NOMORE)
result = ISC_R_SUCCESS;
cleanup:
dns_rbtnodechain_invalidate(&chain);
return (result);
}
/***
*** Private
***/
static void
auto_detach(void *data, void *arg) {
dns_zone_t *zone = data;
UNUSED(arg);
dns_zone_detach(&zone);
}
|