Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / view.c
blob20616eb692a39159367eca942199c88ea759fe76
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2003 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.
20 /* Id: view.c,v 1.159 2009/11/28 15:57:37 vjs Exp */
22 /*! \file */
24 #include <config.h>
26 #include <isc/hash.h>
27 #include <isc/stats.h>
28 #include <isc/string.h> /* Required for HP/UX (and others?) */
29 #include <isc/task.h>
30 #include <isc/util.h>
32 #include <dns/acache.h>
33 #include <dns/acl.h>
34 #include <dns/adb.h>
35 #include <dns/cache.h>
36 #include <dns/db.h>
37 #include <dns/dlz.h>
38 #include <dns/events.h>
39 #include <dns/forward.h>
40 #include <dns/keytable.h>
41 #include <dns/master.h>
42 #include <dns/masterdump.h>
43 #include <dns/order.h>
44 #include <dns/peer.h>
45 #include <dns/rbt.h>
46 #include <dns/rdataset.h>
47 #include <dns/request.h>
48 #include <dns/resolver.h>
49 #include <dns/result.h>
50 #include <dns/stats.h>
51 #include <dns/tsig.h>
52 #include <dns/zone.h>
53 #include <dns/zt.h>
55 #define RESSHUTDOWN(v) (((v)->attributes & DNS_VIEWATTR_RESSHUTDOWN) != 0)
56 #define ADBSHUTDOWN(v) (((v)->attributes & DNS_VIEWATTR_ADBSHUTDOWN) != 0)
57 #define REQSHUTDOWN(v) (((v)->attributes & DNS_VIEWATTR_REQSHUTDOWN) != 0)
59 #define DNS_VIEW_DELONLYHASH 111
61 static void resolver_shutdown(isc_task_t *task, isc_event_t *event);
62 static void adb_shutdown(isc_task_t *task, isc_event_t *event);
63 static void req_shutdown(isc_task_t *task, isc_event_t *event);
65 isc_result_t
66 dns_view_create(isc_mem_t *mctx, dns_rdataclass_t rdclass,
67 const char *name, dns_view_t **viewp)
69 dns_view_t *view;
70 isc_result_t result;
73 * Create a view.
76 REQUIRE(name != NULL);
77 REQUIRE(viewp != NULL && *viewp == NULL);
79 view = isc_mem_get(mctx, sizeof(*view));
80 if (view == NULL)
81 return (ISC_R_NOMEMORY);
82 view->name = isc_mem_strdup(mctx, name);
83 if (view->name == NULL) {
84 result = ISC_R_NOMEMORY;
85 goto cleanup_view;
87 result = isc_mutex_init(&view->lock);
88 if (result != ISC_R_SUCCESS)
89 goto cleanup_name;
91 #ifdef BIND9
92 view->zonetable = NULL;
93 result = dns_zt_create(mctx, rdclass, &view->zonetable);
94 if (result != ISC_R_SUCCESS) {
95 UNEXPECTED_ERROR(__FILE__, __LINE__,
96 "dns_zt_create() failed: %s",
97 isc_result_totext(result));
98 result = ISC_R_UNEXPECTED;
99 goto cleanup_mutex;
101 #endif
102 view->secroots_priv = NULL;
103 view->fwdtable = NULL;
104 result = dns_fwdtable_create(mctx, &view->fwdtable);
105 if (result != ISC_R_SUCCESS) {
106 UNEXPECTED_ERROR(__FILE__, __LINE__,
107 "dns_fwdtable_create() failed: %s",
108 isc_result_totext(result));
109 result = ISC_R_UNEXPECTED;
110 goto cleanup_zt;
113 view->acache = NULL;
114 view->cache = NULL;
115 view->cachedb = NULL;
116 view->dlzdatabase = NULL;
117 view->hints = NULL;
118 view->resolver = NULL;
119 view->adb = NULL;
120 view->requestmgr = NULL;
121 view->mctx = mctx;
122 view->rdclass = rdclass;
123 view->frozen = ISC_FALSE;
124 view->task = NULL;
125 result = isc_refcount_init(&view->references, 1);
126 if (result != ISC_R_SUCCESS)
127 goto cleanup_fwdtable;
128 view->weakrefs = 0;
129 view->attributes = (DNS_VIEWATTR_RESSHUTDOWN|DNS_VIEWATTR_ADBSHUTDOWN|
130 DNS_VIEWATTR_REQSHUTDOWN);
131 view->statickeys = NULL;
132 view->dynamickeys = NULL;
133 view->matchclients = NULL;
134 view->matchdestinations = NULL;
135 view->matchrecursiveonly = ISC_FALSE;
136 result = dns_tsigkeyring_create(view->mctx, &view->dynamickeys);
137 if (result != ISC_R_SUCCESS)
138 goto cleanup_references;
139 view->peers = NULL;
140 view->order = NULL;
141 view->delonly = NULL;
142 view->rootdelonly = ISC_FALSE;
143 view->rootexclude = NULL;
144 view->resstats = NULL;
145 view->resquerystats = NULL;
146 view->cacheshared = ISC_FALSE;
149 * Initialize configuration data with default values.
151 view->recursion = ISC_TRUE;
152 view->auth_nxdomain = ISC_FALSE; /* Was true in BIND 8 */
153 view->additionalfromcache = ISC_TRUE;
154 view->additionalfromauth = ISC_TRUE;
155 view->enablednssec = ISC_TRUE;
156 view->enablevalidation = ISC_TRUE;
157 view->acceptexpired = ISC_FALSE;
158 view->minimalresponses = ISC_FALSE;
159 view->transfer_format = dns_one_answer;
160 view->queryacl = NULL;
161 view->queryonacl = NULL;
162 view->recursionacl = NULL;
163 view->recursiononacl = NULL;
164 view->sortlist = NULL;
165 view->transferacl = NULL;
166 view->notifyacl = NULL;
167 view->updateacl = NULL;
168 view->upfwdacl = NULL;
169 view->denyansweracl = NULL;
170 view->answeracl_exclude = NULL;
171 view->denyanswernames = NULL;
172 view->answernames_exclude = NULL;
173 view->requestixfr = ISC_TRUE;
174 view->provideixfr = ISC_TRUE;
175 view->maxcachettl = 7 * 24 * 3600;
176 view->maxncachettl = 3 * 3600;
177 view->dstport = 53;
178 view->preferred_glue = 0;
179 view->flush = ISC_FALSE;
180 view->dlv = NULL;
181 view->maxudp = 0;
182 #ifdef ALLOW_FILTER_AAAA_ON_V4
183 view->v4_aaaa = dns_v4_aaaa_ok;
184 #endif
185 dns_fixedname_init(&view->dlv_fixed);
187 #ifdef BIND9
188 result = dns_order_create(view->mctx, &view->order);
189 if (result != ISC_R_SUCCESS)
190 goto cleanup_dynkeys;
191 #endif
193 result = dns_peerlist_new(view->mctx, &view->peers);
194 if (result != ISC_R_SUCCESS)
195 goto cleanup_order;
197 result = dns_aclenv_init(view->mctx, &view->aclenv);
198 if (result != ISC_R_SUCCESS)
199 goto cleanup_peerlist;
201 ISC_LINK_INIT(view, link);
202 ISC_EVENT_INIT(&view->resevent, sizeof(view->resevent), 0, NULL,
203 DNS_EVENT_VIEWRESSHUTDOWN, resolver_shutdown,
204 view, NULL, NULL, NULL);
205 ISC_EVENT_INIT(&view->adbevent, sizeof(view->adbevent), 0, NULL,
206 DNS_EVENT_VIEWADBSHUTDOWN, adb_shutdown,
207 view, NULL, NULL, NULL);
208 ISC_EVENT_INIT(&view->reqevent, sizeof(view->reqevent), 0, NULL,
209 DNS_EVENT_VIEWREQSHUTDOWN, req_shutdown,
210 view, NULL, NULL, NULL);
211 view->magic = DNS_VIEW_MAGIC;
213 *viewp = view;
215 return (ISC_R_SUCCESS);
217 cleanup_peerlist:
218 dns_peerlist_detach(&view->peers);
220 cleanup_order:
221 #ifdef BIND9
222 dns_order_detach(&view->order);
224 cleanup_dynkeys:
225 #endif
226 dns_tsigkeyring_destroy(&view->dynamickeys);
228 cleanup_references:
229 isc_refcount_destroy(&view->references);
231 cleanup_fwdtable:
232 dns_fwdtable_destroy(&view->fwdtable);
234 cleanup_zt:
235 #ifdef BIND9
236 dns_zt_detach(&view->zonetable);
238 cleanup_mutex:
239 #endif
240 DESTROYLOCK(&view->lock);
242 cleanup_name:
243 isc_mem_free(mctx, view->name);
245 cleanup_view:
246 isc_mem_put(mctx, view, sizeof(*view));
248 return (result);
251 static inline void
252 destroy(dns_view_t *view) {
253 REQUIRE(!ISC_LINK_LINKED(view, link));
254 REQUIRE(isc_refcount_current(&view->references) == 0);
255 REQUIRE(view->weakrefs == 0);
256 REQUIRE(RESSHUTDOWN(view));
257 REQUIRE(ADBSHUTDOWN(view));
258 REQUIRE(REQSHUTDOWN(view));
260 #ifdef BIND9
261 if (view->order != NULL)
262 dns_order_detach(&view->order);
263 #endif
264 if (view->peers != NULL)
265 dns_peerlist_detach(&view->peers);
266 if (view->dynamickeys != NULL)
267 dns_tsigkeyring_destroy(&view->dynamickeys);
268 if (view->statickeys != NULL)
269 dns_tsigkeyring_destroy(&view->statickeys);
270 if (view->adb != NULL)
271 dns_adb_detach(&view->adb);
272 if (view->resolver != NULL)
273 dns_resolver_detach(&view->resolver);
274 #ifdef BIND9
275 if (view->acache != NULL) {
276 if (view->cachedb != NULL)
277 dns_acache_putdb(view->acache, view->cachedb);
278 dns_acache_detach(&view->acache);
280 #endif
281 if (view->requestmgr != NULL)
282 dns_requestmgr_detach(&view->requestmgr);
283 if (view->task != NULL)
284 isc_task_detach(&view->task);
285 if (view->hints != NULL)
286 dns_db_detach(&view->hints);
287 if (view->dlzdatabase != NULL)
288 dns_dlzdestroy(&view->dlzdatabase);
289 if (view->cachedb != NULL)
290 dns_db_detach(&view->cachedb);
291 if (view->cache != NULL)
292 dns_cache_detach(&view->cache);
293 if (view->matchclients != NULL)
294 dns_acl_detach(&view->matchclients);
295 if (view->matchdestinations != NULL)
296 dns_acl_detach(&view->matchdestinations);
297 if (view->queryacl != NULL)
298 dns_acl_detach(&view->queryacl);
299 if (view->queryonacl != NULL)
300 dns_acl_detach(&view->queryonacl);
301 if (view->recursionacl != NULL)
302 dns_acl_detach(&view->recursionacl);
303 if (view->recursiononacl != NULL)
304 dns_acl_detach(&view->recursiononacl);
305 if (view->sortlist != NULL)
306 dns_acl_detach(&view->sortlist);
307 if (view->transferacl != NULL)
308 dns_acl_detach(&view->transferacl);
309 if (view->notifyacl != NULL)
310 dns_acl_detach(&view->notifyacl);
311 if (view->updateacl != NULL)
312 dns_acl_detach(&view->updateacl);
313 if (view->upfwdacl != NULL)
314 dns_acl_detach(&view->upfwdacl);
315 if (view->denyansweracl != NULL)
316 dns_acl_detach(&view->denyansweracl);
317 if (view->answeracl_exclude != NULL)
318 dns_rbt_destroy(&view->answeracl_exclude);
319 if (view->denyanswernames != NULL)
320 dns_rbt_destroy(&view->denyanswernames);
321 if (view->answernames_exclude != NULL)
322 dns_rbt_destroy(&view->answernames_exclude);
323 if (view->delonly != NULL) {
324 dns_name_t *name;
325 int i;
327 for (i = 0; i < DNS_VIEW_DELONLYHASH; i++) {
328 name = ISC_LIST_HEAD(view->delonly[i]);
329 while (name != NULL) {
330 ISC_LIST_UNLINK(view->delonly[i], name, link);
331 dns_name_free(name, view->mctx);
332 isc_mem_put(view->mctx, name, sizeof(*name));
333 name = ISC_LIST_HEAD(view->delonly[i]);
336 isc_mem_put(view->mctx, view->delonly, sizeof(dns_namelist_t) *
337 DNS_VIEW_DELONLYHASH);
338 view->delonly = NULL;
340 if (view->rootexclude != NULL) {
341 dns_name_t *name;
342 int i;
344 for (i = 0; i < DNS_VIEW_DELONLYHASH; i++) {
345 name = ISC_LIST_HEAD(view->rootexclude[i]);
346 while (name != NULL) {
347 ISC_LIST_UNLINK(view->rootexclude[i],
348 name, link);
349 dns_name_free(name, view->mctx);
350 isc_mem_put(view->mctx, name, sizeof(*name));
351 name = ISC_LIST_HEAD(view->rootexclude[i]);
354 isc_mem_put(view->mctx, view->rootexclude,
355 sizeof(dns_namelist_t) * DNS_VIEW_DELONLYHASH);
356 view->rootexclude = NULL;
358 if (view->resstats != NULL)
359 isc_stats_detach(&view->resstats);
360 if (view->resquerystats != NULL)
361 dns_stats_detach(&view->resquerystats);
362 if (view->secroots_priv != NULL)
363 dns_keytable_detach(&view->secroots_priv);
364 dns_fwdtable_destroy(&view->fwdtable);
365 dns_aclenv_destroy(&view->aclenv);
366 DESTROYLOCK(&view->lock);
367 isc_refcount_destroy(&view->references);
368 isc_mem_free(view->mctx, view->name);
369 isc_mem_put(view->mctx, view, sizeof(*view));
373 * Return true iff 'view' may be freed.
374 * The caller must be holding the view lock.
376 static isc_boolean_t
377 all_done(dns_view_t *view) {
379 if (isc_refcount_current(&view->references) == 0 &&
380 view->weakrefs == 0 &&
381 RESSHUTDOWN(view) && ADBSHUTDOWN(view) && REQSHUTDOWN(view))
382 return (ISC_TRUE);
384 return (ISC_FALSE);
387 void
388 dns_view_attach(dns_view_t *source, dns_view_t **targetp) {
390 REQUIRE(DNS_VIEW_VALID(source));
391 REQUIRE(targetp != NULL && *targetp == NULL);
393 isc_refcount_increment(&source->references, NULL);
395 *targetp = source;
398 static void
399 view_flushanddetach(dns_view_t **viewp, isc_boolean_t flush) {
400 dns_view_t *view;
401 unsigned int refs;
402 isc_boolean_t done = ISC_FALSE;
404 REQUIRE(viewp != NULL);
405 view = *viewp;
406 REQUIRE(DNS_VIEW_VALID(view));
408 if (flush)
409 view->flush = ISC_TRUE;
410 isc_refcount_decrement(&view->references, &refs);
411 if (refs == 0) {
412 LOCK(&view->lock);
413 if (!RESSHUTDOWN(view))
414 dns_resolver_shutdown(view->resolver);
415 if (!ADBSHUTDOWN(view))
416 dns_adb_shutdown(view->adb);
417 if (!REQSHUTDOWN(view))
418 dns_requestmgr_shutdown(view->requestmgr);
419 #ifdef BIND9
420 if (view->acache != NULL)
421 dns_acache_shutdown(view->acache);
422 if (view->flush)
423 dns_zt_flushanddetach(&view->zonetable);
424 else
425 dns_zt_detach(&view->zonetable);
426 #endif
427 done = all_done(view);
428 UNLOCK(&view->lock);
431 *viewp = NULL;
433 if (done)
434 destroy(view);
437 void
438 dns_view_flushanddetach(dns_view_t **viewp) {
439 view_flushanddetach(viewp, ISC_TRUE);
442 void
443 dns_view_detach(dns_view_t **viewp) {
444 view_flushanddetach(viewp, ISC_FALSE);
447 #ifdef BIND9
448 static isc_result_t
449 dialup(dns_zone_t *zone, void *dummy) {
450 UNUSED(dummy);
451 dns_zone_dialup(zone);
452 return (ISC_R_SUCCESS);
455 void
456 dns_view_dialup(dns_view_t *view) {
457 REQUIRE(DNS_VIEW_VALID(view));
458 (void)dns_zt_apply(view->zonetable, ISC_FALSE, dialup, NULL);
460 #endif
462 void
463 dns_view_weakattach(dns_view_t *source, dns_view_t **targetp) {
465 REQUIRE(DNS_VIEW_VALID(source));
466 REQUIRE(targetp != NULL && *targetp == NULL);
468 LOCK(&source->lock);
469 source->weakrefs++;
470 UNLOCK(&source->lock);
472 *targetp = source;
475 void
476 dns_view_weakdetach(dns_view_t **viewp) {
477 dns_view_t *view;
478 isc_boolean_t done = ISC_FALSE;
480 REQUIRE(viewp != NULL);
481 view = *viewp;
482 REQUIRE(DNS_VIEW_VALID(view));
484 LOCK(&view->lock);
486 INSIST(view->weakrefs > 0);
487 view->weakrefs--;
488 done = all_done(view);
490 UNLOCK(&view->lock);
492 *viewp = NULL;
494 if (done)
495 destroy(view);
498 static void
499 resolver_shutdown(isc_task_t *task, isc_event_t *event) {
500 dns_view_t *view = event->ev_arg;
501 isc_boolean_t done;
503 REQUIRE(event->ev_type == DNS_EVENT_VIEWRESSHUTDOWN);
504 REQUIRE(DNS_VIEW_VALID(view));
505 REQUIRE(view->task == task);
507 UNUSED(task);
509 LOCK(&view->lock);
511 view->attributes |= DNS_VIEWATTR_RESSHUTDOWN;
512 done = all_done(view);
514 UNLOCK(&view->lock);
516 isc_event_free(&event);
518 if (done)
519 destroy(view);
522 static void
523 adb_shutdown(isc_task_t *task, isc_event_t *event) {
524 dns_view_t *view = event->ev_arg;
525 isc_boolean_t done;
527 REQUIRE(event->ev_type == DNS_EVENT_VIEWADBSHUTDOWN);
528 REQUIRE(DNS_VIEW_VALID(view));
529 REQUIRE(view->task == task);
531 UNUSED(task);
533 LOCK(&view->lock);
535 view->attributes |= DNS_VIEWATTR_ADBSHUTDOWN;
536 done = all_done(view);
538 UNLOCK(&view->lock);
540 isc_event_free(&event);
542 if (done)
543 destroy(view);
546 static void
547 req_shutdown(isc_task_t *task, isc_event_t *event) {
548 dns_view_t *view = event->ev_arg;
549 isc_boolean_t done;
551 REQUIRE(event->ev_type == DNS_EVENT_VIEWREQSHUTDOWN);
552 REQUIRE(DNS_VIEW_VALID(view));
553 REQUIRE(view->task == task);
555 UNUSED(task);
557 LOCK(&view->lock);
559 view->attributes |= DNS_VIEWATTR_REQSHUTDOWN;
560 done = all_done(view);
562 UNLOCK(&view->lock);
564 isc_event_free(&event);
566 if (done)
567 destroy(view);
570 isc_result_t
571 dns_view_createresolver(dns_view_t *view,
572 isc_taskmgr_t *taskmgr, unsigned int ntasks,
573 isc_socketmgr_t *socketmgr,
574 isc_timermgr_t *timermgr,
575 unsigned int options,
576 dns_dispatchmgr_t *dispatchmgr,
577 dns_dispatch_t *dispatchv4,
578 dns_dispatch_t *dispatchv6)
580 isc_result_t result;
581 isc_event_t *event;
582 isc_mem_t *mctx = NULL;
584 REQUIRE(DNS_VIEW_VALID(view));
585 REQUIRE(!view->frozen);
586 REQUIRE(view->resolver == NULL);
588 result = isc_task_create(taskmgr, 0, &view->task);
589 if (result != ISC_R_SUCCESS)
590 return (result);
591 isc_task_setname(view->task, "view", view);
593 result = dns_resolver_create(view, taskmgr, ntasks, socketmgr,
594 timermgr, options, dispatchmgr,
595 dispatchv4, dispatchv6,
596 &view->resolver);
597 if (result != ISC_R_SUCCESS) {
598 isc_task_detach(&view->task);
599 return (result);
601 event = &view->resevent;
602 dns_resolver_whenshutdown(view->resolver, view->task, &event);
603 view->attributes &= ~DNS_VIEWATTR_RESSHUTDOWN;
605 result = isc_mem_create(0, 0, &mctx);
606 if (result != ISC_R_SUCCESS) {
607 dns_resolver_shutdown(view->resolver);
608 return (result);
611 result = dns_adb_create(mctx, view, timermgr, taskmgr, &view->adb);
612 isc_mem_setname(mctx, "ADB", NULL);
613 isc_mem_detach(&mctx);
614 if (result != ISC_R_SUCCESS) {
615 dns_resolver_shutdown(view->resolver);
616 return (result);
618 event = &view->adbevent;
619 dns_adb_whenshutdown(view->adb, view->task, &event);
620 view->attributes &= ~DNS_VIEWATTR_ADBSHUTDOWN;
622 result = dns_requestmgr_create(view->mctx, timermgr, socketmgr,
623 dns_resolver_taskmgr(view->resolver),
624 dns_resolver_dispatchmgr(view->resolver),
625 dns_resolver_dispatchv4(view->resolver),
626 dns_resolver_dispatchv6(view->resolver),
627 &view->requestmgr);
628 if (result != ISC_R_SUCCESS) {
629 dns_adb_shutdown(view->adb);
630 dns_resolver_shutdown(view->resolver);
631 return (result);
633 event = &view->reqevent;
634 dns_requestmgr_whenshutdown(view->requestmgr, view->task, &event);
635 view->attributes &= ~DNS_VIEWATTR_REQSHUTDOWN;
637 return (ISC_R_SUCCESS);
640 void
641 dns_view_setcache(dns_view_t *view, dns_cache_t *cache) {
642 dns_view_setcache2(view, cache, ISC_FALSE);
645 void
646 dns_view_setcache2(dns_view_t *view, dns_cache_t *cache, isc_boolean_t shared) {
647 REQUIRE(DNS_VIEW_VALID(view));
648 REQUIRE(!view->frozen);
650 view->cacheshared = shared;
651 if (view->cache != NULL) {
652 #ifdef BIND9
653 if (view->acache != NULL)
654 dns_acache_putdb(view->acache, view->cachedb);
655 #endif
656 dns_db_detach(&view->cachedb);
657 dns_cache_detach(&view->cache);
659 dns_cache_attach(cache, &view->cache);
660 dns_cache_attachdb(cache, &view->cachedb);
661 INSIST(DNS_DB_VALID(view->cachedb));
663 #ifdef BIND9
664 if (view->acache != NULL)
665 dns_acache_setdb(view->acache, view->cachedb);
666 #endif
669 isc_boolean_t
670 dns_view_iscacheshared(dns_view_t *view) {
671 REQUIRE(DNS_VIEW_VALID(view));
673 return (view->cacheshared);
676 void
677 dns_view_sethints(dns_view_t *view, dns_db_t *hints) {
678 REQUIRE(DNS_VIEW_VALID(view));
679 REQUIRE(!view->frozen);
680 REQUIRE(view->hints == NULL);
681 REQUIRE(dns_db_iszone(hints));
683 dns_db_attach(hints, &view->hints);
686 void
687 dns_view_setkeyring(dns_view_t *view, dns_tsig_keyring_t *ring) {
688 REQUIRE(DNS_VIEW_VALID(view));
689 REQUIRE(ring != NULL);
690 if (view->statickeys != NULL)
691 dns_tsigkeyring_destroy(&view->statickeys);
692 view->statickeys = ring;
695 void
696 dns_view_setdstport(dns_view_t *view, in_port_t dstport) {
697 REQUIRE(DNS_VIEW_VALID(view));
698 view->dstport = dstport;
701 #ifdef BIND9
702 isc_result_t
703 dns_view_addzone(dns_view_t *view, dns_zone_t *zone) {
704 isc_result_t result;
706 REQUIRE(DNS_VIEW_VALID(view));
707 REQUIRE(!view->frozen);
709 result = dns_zt_mount(view->zonetable, zone);
711 return (result);
713 #endif
715 void
716 dns_view_freeze(dns_view_t *view) {
717 REQUIRE(DNS_VIEW_VALID(view));
718 REQUIRE(!view->frozen);
720 if (view->resolver != NULL) {
721 INSIST(view->cachedb != NULL);
722 dns_resolver_freeze(view->resolver);
724 view->frozen = ISC_TRUE;
727 #ifdef BIND9
728 isc_result_t
729 dns_view_findzone(dns_view_t *view, dns_name_t *name, dns_zone_t **zonep) {
730 isc_result_t result;
732 REQUIRE(DNS_VIEW_VALID(view));
734 result = dns_zt_find(view->zonetable, name, 0, NULL, zonep);
735 if (result == DNS_R_PARTIALMATCH) {
736 dns_zone_detach(zonep);
737 result = ISC_R_NOTFOUND;
740 return (result);
742 #endif
744 isc_result_t
745 dns_view_find(dns_view_t *view, dns_name_t *name, dns_rdatatype_t type,
746 isc_stdtime_t now, unsigned int options, isc_boolean_t use_hints,
747 dns_db_t **dbp, dns_dbnode_t **nodep, dns_name_t *foundname,
748 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
750 isc_result_t result;
751 dns_db_t *db, *zdb;
752 dns_dbnode_t *node, *znode;
753 isc_boolean_t is_cache;
754 dns_rdataset_t zrdataset, zsigrdataset;
755 dns_zone_t *zone;
757 #ifndef BIND9
758 UNUSED(use_hints);
759 #endif
762 * Find an rdataset whose owner name is 'name', and whose type is
763 * 'type'.
766 REQUIRE(DNS_VIEW_VALID(view));
767 REQUIRE(view->frozen);
768 REQUIRE(type != dns_rdatatype_rrsig);
769 REQUIRE(rdataset != NULL); /* XXXBEW - remove this */
770 REQUIRE(nodep == NULL || *nodep == NULL);
773 * Initialize.
775 dns_rdataset_init(&zrdataset);
776 dns_rdataset_init(&zsigrdataset);
777 zdb = NULL;
778 znode = NULL;
781 * Find a database to answer the query.
783 zone = NULL;
784 db = NULL;
785 node = NULL;
786 #ifdef BIND9
787 result = dns_zt_find(view->zonetable, name, 0, NULL, &zone);
788 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
789 result = dns_zone_getdb(zone, &db);
790 if (result != ISC_R_SUCCESS && view->cachedb != NULL)
791 dns_db_attach(view->cachedb, &db);
792 else if (result != ISC_R_SUCCESS)
793 goto cleanup;
794 } else if (result == ISC_R_NOTFOUND && view->cachedb != NULL)
795 dns_db_attach(view->cachedb, &db);
796 #else
797 result = ISC_R_NOTFOUND;
798 if (view->cachedb != NULL)
799 dns_db_attach(view->cachedb, &db);
800 #endif /* BIND9 */
801 else
802 goto cleanup;
804 is_cache = dns_db_iscache(db);
806 db_find:
808 * Now look for an answer in the database.
810 result = dns_db_find(db, name, NULL, type, options,
811 now, &node, foundname, rdataset, sigrdataset);
813 if (result == DNS_R_DELEGATION ||
814 result == ISC_R_NOTFOUND) {
815 if (dns_rdataset_isassociated(rdataset))
816 dns_rdataset_disassociate(rdataset);
817 if (sigrdataset != NULL &&
818 dns_rdataset_isassociated(sigrdataset))
819 dns_rdataset_disassociate(sigrdataset);
820 if (node != NULL)
821 dns_db_detachnode(db, &node);
822 if (!is_cache) {
823 dns_db_detach(&db);
824 if (view->cachedb != NULL) {
826 * Either the answer is in the cache, or we
827 * don't know it.
829 is_cache = ISC_TRUE;
830 dns_db_attach(view->cachedb, &db);
831 goto db_find;
833 } else {
835 * We don't have the data in the cache. If we've got
836 * glue from the zone, use it.
838 if (dns_rdataset_isassociated(&zrdataset)) {
839 dns_rdataset_clone(&zrdataset, rdataset);
840 if (sigrdataset != NULL &&
841 dns_rdataset_isassociated(&zsigrdataset))
842 dns_rdataset_clone(&zsigrdataset,
843 sigrdataset);
844 result = DNS_R_GLUE;
845 if (db != NULL)
846 dns_db_detach(&db);
847 dns_db_attach(zdb, &db);
848 dns_db_attachnode(db, znode, &node);
849 goto cleanup;
853 * We don't know the answer.
855 result = ISC_R_NOTFOUND;
856 } else if (result == DNS_R_GLUE) {
857 if (view->cachedb != NULL) {
859 * We found an answer, but the cache may be better.
860 * Remember what we've got and go look in the cache.
862 is_cache = ISC_TRUE;
863 dns_rdataset_clone(rdataset, &zrdataset);
864 dns_rdataset_disassociate(rdataset);
865 if (sigrdataset != NULL &&
866 dns_rdataset_isassociated(sigrdataset)) {
867 dns_rdataset_clone(sigrdataset, &zsigrdataset);
868 dns_rdataset_disassociate(sigrdataset);
870 dns_db_attach(db, &zdb);
871 dns_db_attachnode(zdb, node, &znode);
872 dns_db_detachnode(db, &node);
873 dns_db_detach(&db);
874 dns_db_attach(view->cachedb, &db);
875 goto db_find;
878 * Otherwise, the glue is the best answer.
880 result = ISC_R_SUCCESS;
883 #ifdef BIND9
884 if (result == ISC_R_NOTFOUND && use_hints && view->hints != NULL) {
885 if (dns_rdataset_isassociated(rdataset))
886 dns_rdataset_disassociate(rdataset);
887 if (sigrdataset != NULL &&
888 dns_rdataset_isassociated(sigrdataset))
889 dns_rdataset_disassociate(sigrdataset);
890 if (db != NULL) {
891 if (node != NULL)
892 dns_db_detachnode(db, &node);
893 dns_db_detach(&db);
895 result = dns_db_find(view->hints, name, NULL, type, options,
896 now, &node, foundname,
897 rdataset, sigrdataset);
898 if (result == ISC_R_SUCCESS || result == DNS_R_GLUE) {
900 * We just used a hint. Let the resolver know it
901 * should consider priming.
903 dns_resolver_prime(view->resolver);
904 dns_db_attach(view->hints, &db);
905 result = DNS_R_HINT;
906 } else if (result == DNS_R_NXRRSET) {
907 dns_db_attach(view->hints, &db);
908 result = DNS_R_HINTNXRRSET;
909 } else if (result == DNS_R_NXDOMAIN)
910 result = ISC_R_NOTFOUND;
913 * Cleanup if non-standard hints are used.
915 if (db == NULL && node != NULL)
916 dns_db_detachnode(view->hints, &node);
918 #endif /* BIND9 */
920 cleanup:
921 if (dns_rdataset_isassociated(&zrdataset)) {
922 dns_rdataset_disassociate(&zrdataset);
923 if (dns_rdataset_isassociated(&zsigrdataset))
924 dns_rdataset_disassociate(&zsigrdataset);
927 if (zdb != NULL) {
928 if (znode != NULL)
929 dns_db_detachnode(zdb, &znode);
930 dns_db_detach(&zdb);
933 if (db != NULL) {
934 if (node != NULL) {
935 if (nodep != NULL)
936 *nodep = node;
937 else
938 dns_db_detachnode(db, &node);
940 if (dbp != NULL)
941 *dbp = db;
942 else
943 dns_db_detach(&db);
944 } else
945 INSIST(node == NULL);
947 #ifdef BIND9
948 if (zone != NULL)
949 dns_zone_detach(&zone);
950 #endif
952 return (result);
955 isc_result_t
956 dns_view_simplefind(dns_view_t *view, dns_name_t *name, dns_rdatatype_t type,
957 isc_stdtime_t now, unsigned int options,
958 isc_boolean_t use_hints,
959 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
961 isc_result_t result;
962 dns_fixedname_t foundname;
964 dns_fixedname_init(&foundname);
965 result = dns_view_find(view, name, type, now, options, use_hints,
966 NULL, NULL, dns_fixedname_name(&foundname),
967 rdataset, sigrdataset);
968 if (result == DNS_R_NXDOMAIN) {
970 * The rdataset and sigrdataset of the relevant NSEC record
971 * may be returned, but the caller cannot use them because
972 * foundname is not returned by this simplified API. We
973 * disassociate them here to prevent any misuse by the caller.
975 if (dns_rdataset_isassociated(rdataset))
976 dns_rdataset_disassociate(rdataset);
977 if (sigrdataset != NULL &&
978 dns_rdataset_isassociated(sigrdataset))
979 dns_rdataset_disassociate(sigrdataset);
980 } else if (result != ISC_R_SUCCESS &&
981 result != DNS_R_GLUE &&
982 result != DNS_R_HINT &&
983 result != DNS_R_NCACHENXDOMAIN &&
984 result != DNS_R_NCACHENXRRSET &&
985 result != DNS_R_NXRRSET &&
986 result != DNS_R_HINTNXRRSET &&
987 result != ISC_R_NOTFOUND) {
988 if (dns_rdataset_isassociated(rdataset))
989 dns_rdataset_disassociate(rdataset);
990 if (sigrdataset != NULL &&
991 dns_rdataset_isassociated(sigrdataset))
992 dns_rdataset_disassociate(sigrdataset);
993 result = ISC_R_NOTFOUND;
996 return (result);
999 isc_result_t
1000 dns_view_findzonecut(dns_view_t *view, dns_name_t *name, dns_name_t *fname,
1001 isc_stdtime_t now, unsigned int options,
1002 isc_boolean_t use_hints,
1003 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
1005 return(dns_view_findzonecut2(view, name, fname, now, options,
1006 use_hints, ISC_TRUE,
1007 rdataset, sigrdataset));
1010 isc_result_t
1011 dns_view_findzonecut2(dns_view_t *view, dns_name_t *name, dns_name_t *fname,
1012 isc_stdtime_t now, unsigned int options,
1013 isc_boolean_t use_hints, isc_boolean_t use_cache,
1014 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
1016 isc_result_t result;
1017 dns_db_t *db;
1018 isc_boolean_t is_cache, use_zone, try_hints;
1019 dns_zone_t *zone;
1020 dns_name_t *zfname;
1021 dns_rdataset_t zrdataset, zsigrdataset;
1022 dns_fixedname_t zfixedname;
1024 REQUIRE(DNS_VIEW_VALID(view));
1025 REQUIRE(view->frozen);
1027 db = NULL;
1028 zone = NULL;
1029 use_zone = ISC_FALSE;
1030 try_hints = ISC_FALSE;
1031 zfname = NULL;
1034 * Initialize.
1036 dns_fixedname_init(&zfixedname);
1037 dns_rdataset_init(&zrdataset);
1038 dns_rdataset_init(&zsigrdataset);
1041 * Find the right database.
1043 #ifdef BIND9
1044 result = dns_zt_find(view->zonetable, name, 0, NULL, &zone);
1045 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
1046 result = dns_zone_getdb(zone, &db);
1047 #else
1048 result = ISC_R_NOTFOUND;
1049 #endif
1050 if (result == ISC_R_NOTFOUND) {
1052 * We're not directly authoritative for this query name, nor
1053 * is it a subdomain of any zone for which we're
1054 * authoritative.
1056 if (use_cache && view->cachedb != NULL) {
1058 * We have a cache; try it.
1060 dns_db_attach(view->cachedb, &db);
1061 } else {
1063 * Maybe we have hints...
1065 try_hints = ISC_TRUE;
1066 goto finish;
1068 } else if (result != ISC_R_SUCCESS) {
1070 * Something is broken.
1072 goto cleanup;
1074 is_cache = dns_db_iscache(db);
1076 db_find:
1078 * Look for the zonecut.
1080 if (!is_cache) {
1081 result = dns_db_find(db, name, NULL, dns_rdatatype_ns, options,
1082 now, NULL, fname, rdataset, sigrdataset);
1083 if (result == DNS_R_DELEGATION)
1084 result = ISC_R_SUCCESS;
1085 else if (result != ISC_R_SUCCESS)
1086 goto cleanup;
1087 if (use_cache && view->cachedb != NULL && db != view->hints) {
1089 * We found an answer, but the cache may be better.
1091 zfname = dns_fixedname_name(&zfixedname);
1092 result = dns_name_copy(fname, zfname, NULL);
1093 if (result != ISC_R_SUCCESS)
1094 goto cleanup;
1095 dns_rdataset_clone(rdataset, &zrdataset);
1096 dns_rdataset_disassociate(rdataset);
1097 if (sigrdataset != NULL &&
1098 dns_rdataset_isassociated(sigrdataset)) {
1099 dns_rdataset_clone(sigrdataset, &zsigrdataset);
1100 dns_rdataset_disassociate(sigrdataset);
1102 dns_db_detach(&db);
1103 dns_db_attach(view->cachedb, &db);
1104 is_cache = ISC_TRUE;
1105 goto db_find;
1107 } else {
1108 result = dns_db_findzonecut(db, name, options, now, NULL,
1109 fname, rdataset, sigrdataset);
1110 if (result == ISC_R_SUCCESS) {
1111 if (zfname != NULL &&
1112 !dns_name_issubdomain(fname, zfname)) {
1114 * We found a zonecut in the cache, but our
1115 * zone delegation is better.
1117 use_zone = ISC_TRUE;
1119 } else if (result == ISC_R_NOTFOUND) {
1120 if (zfname != NULL) {
1122 * We didn't find anything in the cache, but we
1123 * have a zone delegation, so use it.
1125 use_zone = ISC_TRUE;
1126 } else {
1128 * Maybe we have hints...
1130 try_hints = ISC_TRUE;
1132 } else {
1134 * Something bad happened.
1136 goto cleanup;
1140 finish:
1141 if (use_zone) {
1142 if (dns_rdataset_isassociated(rdataset)) {
1143 dns_rdataset_disassociate(rdataset);
1144 if (sigrdataset != NULL &&
1145 dns_rdataset_isassociated(sigrdataset))
1146 dns_rdataset_disassociate(sigrdataset);
1148 result = dns_name_copy(zfname, fname, NULL);
1149 if (result != ISC_R_SUCCESS)
1150 goto cleanup;
1151 dns_rdataset_clone(&zrdataset, rdataset);
1152 if (sigrdataset != NULL &&
1153 dns_rdataset_isassociated(&zrdataset))
1154 dns_rdataset_clone(&zsigrdataset, sigrdataset);
1155 } else if (try_hints && use_hints && view->hints != NULL) {
1157 * We've found nothing so far, but we have hints.
1159 result = dns_db_find(view->hints, dns_rootname, NULL,
1160 dns_rdatatype_ns, 0, now, NULL, fname,
1161 rdataset, NULL);
1162 if (result != ISC_R_SUCCESS) {
1164 * We can't even find the hints for the root
1165 * nameservers!
1167 if (dns_rdataset_isassociated(rdataset))
1168 dns_rdataset_disassociate(rdataset);
1169 result = ISC_R_NOTFOUND;
1173 cleanup:
1174 if (dns_rdataset_isassociated(&zrdataset)) {
1175 dns_rdataset_disassociate(&zrdataset);
1176 if (dns_rdataset_isassociated(&zsigrdataset))
1177 dns_rdataset_disassociate(&zsigrdataset);
1179 if (db != NULL)
1180 dns_db_detach(&db);
1181 #ifdef BIND9
1182 if (zone != NULL)
1183 dns_zone_detach(&zone);
1184 #endif
1186 return (result);
1189 isc_result_t
1190 dns_viewlist_find(dns_viewlist_t *list, const char *name,
1191 dns_rdataclass_t rdclass, dns_view_t **viewp)
1193 dns_view_t *view;
1195 REQUIRE(list != NULL);
1197 for (view = ISC_LIST_HEAD(*list);
1198 view != NULL;
1199 view = ISC_LIST_NEXT(view, link)) {
1200 if (strcmp(view->name, name) == 0 && view->rdclass == rdclass)
1201 break;
1203 if (view == NULL)
1204 return (ISC_R_NOTFOUND);
1206 dns_view_attach(view, viewp);
1208 return (ISC_R_SUCCESS);
1211 #ifdef BIND9
1212 isc_result_t
1213 dns_viewlist_findzone(dns_viewlist_t *list, dns_name_t *name,
1214 isc_boolean_t allclasses, dns_rdataclass_t rdclass,
1215 dns_zone_t **zonep)
1217 dns_view_t *view;
1218 isc_result_t result;
1219 dns_zone_t *zone1 = NULL, *zone2 = NULL;
1220 dns_zone_t **zp = NULL;;
1222 REQUIRE(list != NULL);
1223 for (view = ISC_LIST_HEAD(*list);
1224 view != NULL;
1225 view = ISC_LIST_NEXT(view, link)) {
1226 if (allclasses == ISC_FALSE && view->rdclass != rdclass)
1227 continue;
1230 * If the zone is defined in more than one view,
1231 * treat it as not found.
1233 zp = (zone1 == NULL) ? &zone1 : &zone2;
1234 result = dns_zt_find(view->zonetable, name, 0, NULL, zp);
1235 INSIST(result == ISC_R_SUCCESS ||
1236 result == ISC_R_NOTFOUND ||
1237 result == DNS_R_PARTIALMATCH);
1239 /* Treat a partial match as no match */
1240 if (result == DNS_R_PARTIALMATCH) {
1241 dns_zone_detach(zp);
1242 result = ISC_R_NOTFOUND;
1245 if (zone2 != NULL) {
1246 dns_zone_detach(&zone1);
1247 dns_zone_detach(&zone2);
1248 return (ISC_R_NOTFOUND);
1252 if (zone1 != NULL) {
1253 dns_zone_attach(zone1, zonep);
1254 dns_zone_detach(&zone1);
1255 return (ISC_R_SUCCESS);
1258 return (ISC_R_NOTFOUND);
1261 isc_result_t
1262 dns_view_load(dns_view_t *view, isc_boolean_t stop) {
1264 REQUIRE(DNS_VIEW_VALID(view));
1266 return (dns_zt_load(view->zonetable, stop));
1269 isc_result_t
1270 dns_view_loadnew(dns_view_t *view, isc_boolean_t stop) {
1272 REQUIRE(DNS_VIEW_VALID(view));
1274 return (dns_zt_loadnew(view->zonetable, stop));
1276 #endif /* BIND9 */
1278 isc_result_t
1279 dns_view_gettsig(dns_view_t *view, dns_name_t *keyname, dns_tsigkey_t **keyp)
1281 isc_result_t result;
1282 REQUIRE(keyp != NULL && *keyp == NULL);
1284 result = dns_tsigkey_find(keyp, keyname, NULL,
1285 view->statickeys);
1286 if (result == ISC_R_NOTFOUND)
1287 result = dns_tsigkey_find(keyp, keyname, NULL,
1288 view->dynamickeys);
1289 return (result);
1292 isc_result_t
1293 dns_view_getpeertsig(dns_view_t *view, isc_netaddr_t *peeraddr,
1294 dns_tsigkey_t **keyp)
1296 isc_result_t result;
1297 dns_name_t *keyname = NULL;
1298 dns_peer_t *peer = NULL;
1300 result = dns_peerlist_peerbyaddr(view->peers, peeraddr, &peer);
1301 if (result != ISC_R_SUCCESS)
1302 return (result);
1304 result = dns_peer_getkey(peer, &keyname);
1305 if (result != ISC_R_SUCCESS)
1306 return (result);
1308 result = dns_view_gettsig(view, keyname, keyp);
1309 return ((result == ISC_R_NOTFOUND) ? ISC_R_FAILURE : result);
1312 isc_result_t
1313 dns_view_checksig(dns_view_t *view, isc_buffer_t *source, dns_message_t *msg) {
1314 REQUIRE(DNS_VIEW_VALID(view));
1315 REQUIRE(source != NULL);
1317 return (dns_tsig_verify(source, msg, view->statickeys,
1318 view->dynamickeys));
1321 #ifdef BIND9
1322 isc_result_t
1323 dns_view_dumpdbtostream(dns_view_t *view, FILE *fp) {
1324 isc_result_t result;
1326 REQUIRE(DNS_VIEW_VALID(view));
1328 (void)fprintf(fp, ";\n; Cache dump of view '%s'\n;\n", view->name);
1329 result = dns_master_dumptostream(view->mctx, view->cachedb, NULL,
1330 &dns_master_style_cache, fp);
1331 if (result != ISC_R_SUCCESS)
1332 return (result);
1333 dns_adb_dump(view->adb, fp);
1334 return (ISC_R_SUCCESS);
1336 #endif
1338 isc_result_t
1339 dns_view_flushcache(dns_view_t *view) {
1340 return (dns_view_flushcache2(view, ISC_FALSE));
1343 isc_result_t
1344 dns_view_flushcache2(dns_view_t *view, isc_boolean_t fixuponly) {
1345 isc_result_t result;
1347 REQUIRE(DNS_VIEW_VALID(view));
1349 if (view->cachedb == NULL)
1350 return (ISC_R_SUCCESS);
1351 if (!fixuponly) {
1352 result = dns_cache_flush(view->cache);
1353 if (result != ISC_R_SUCCESS)
1354 return (result);
1356 #ifdef BIND9
1357 if (view->acache != NULL)
1358 dns_acache_putdb(view->acache, view->cachedb);
1359 #endif
1360 dns_db_detach(&view->cachedb);
1361 dns_cache_attachdb(view->cache, &view->cachedb);
1362 #ifdef BIND9
1363 if (view->acache != NULL)
1364 dns_acache_setdb(view->acache, view->cachedb);
1365 #endif
1367 dns_adb_flush(view->adb);
1368 return (ISC_R_SUCCESS);
1371 isc_result_t
1372 dns_view_flushname(dns_view_t *view, dns_name_t *name) {
1374 REQUIRE(DNS_VIEW_VALID(view));
1376 if (view->adb != NULL)
1377 dns_adb_flushname(view->adb, name);
1378 if (view->cache == NULL)
1379 return (ISC_R_SUCCESS);
1380 return (dns_cache_flushname(view->cache, name));
1383 isc_result_t
1384 dns_view_adddelegationonly(dns_view_t *view, dns_name_t *name) {
1385 isc_result_t result;
1386 dns_name_t *new;
1387 isc_uint32_t hash;
1389 REQUIRE(DNS_VIEW_VALID(view));
1391 if (view->delonly == NULL) {
1392 view->delonly = isc_mem_get(view->mctx,
1393 sizeof(dns_namelist_t) *
1394 DNS_VIEW_DELONLYHASH);
1395 if (view->delonly == NULL)
1396 return (ISC_R_NOMEMORY);
1397 for (hash = 0; hash < DNS_VIEW_DELONLYHASH; hash++)
1398 ISC_LIST_INIT(view->delonly[hash]);
1400 hash = dns_name_hash(name, ISC_FALSE) % DNS_VIEW_DELONLYHASH;
1401 new = ISC_LIST_HEAD(view->delonly[hash]);
1402 while (new != NULL && !dns_name_equal(new, name))
1403 new = ISC_LIST_NEXT(new, link);
1404 if (new != NULL)
1405 return (ISC_R_SUCCESS);
1406 new = isc_mem_get(view->mctx, sizeof(*new));
1407 if (new == NULL)
1408 return (ISC_R_NOMEMORY);
1409 dns_name_init(new, NULL);
1410 result = dns_name_dup(name, view->mctx, new);
1411 if (result == ISC_R_SUCCESS)
1412 ISC_LIST_APPEND(view->delonly[hash], new, link);
1413 else
1414 isc_mem_put(view->mctx, new, sizeof(*new));
1415 return (result);
1418 isc_result_t
1419 dns_view_excludedelegationonly(dns_view_t *view, dns_name_t *name) {
1420 isc_result_t result;
1421 dns_name_t *new;
1422 isc_uint32_t hash;
1424 REQUIRE(DNS_VIEW_VALID(view));
1426 if (view->rootexclude == NULL) {
1427 view->rootexclude = isc_mem_get(view->mctx,
1428 sizeof(dns_namelist_t) *
1429 DNS_VIEW_DELONLYHASH);
1430 if (view->rootexclude == NULL)
1431 return (ISC_R_NOMEMORY);
1432 for (hash = 0; hash < DNS_VIEW_DELONLYHASH; hash++)
1433 ISC_LIST_INIT(view->rootexclude[hash]);
1435 hash = dns_name_hash(name, ISC_FALSE) % DNS_VIEW_DELONLYHASH;
1436 new = ISC_LIST_HEAD(view->rootexclude[hash]);
1437 while (new != NULL && !dns_name_equal(new, name))
1438 new = ISC_LIST_NEXT(new, link);
1439 if (new != NULL)
1440 return (ISC_R_SUCCESS);
1441 new = isc_mem_get(view->mctx, sizeof(*new));
1442 if (new == NULL)
1443 return (ISC_R_NOMEMORY);
1444 dns_name_init(new, NULL);
1445 result = dns_name_dup(name, view->mctx, new);
1446 if (result == ISC_R_SUCCESS)
1447 ISC_LIST_APPEND(view->rootexclude[hash], new, link);
1448 else
1449 isc_mem_put(view->mctx, new, sizeof(*new));
1450 return (result);
1453 isc_boolean_t
1454 dns_view_isdelegationonly(dns_view_t *view, dns_name_t *name) {
1455 dns_name_t *new;
1456 isc_uint32_t hash;
1458 REQUIRE(DNS_VIEW_VALID(view));
1460 if (!view->rootdelonly && view->delonly == NULL)
1461 return (ISC_FALSE);
1463 hash = dns_name_hash(name, ISC_FALSE) % DNS_VIEW_DELONLYHASH;
1464 if (view->rootdelonly && dns_name_countlabels(name) <= 2) {
1465 if (view->rootexclude == NULL)
1466 return (ISC_TRUE);
1467 new = ISC_LIST_HEAD(view->rootexclude[hash]);
1468 while (new != NULL && !dns_name_equal(new, name))
1469 new = ISC_LIST_NEXT(new, link);
1470 if (new == NULL)
1471 return (ISC_TRUE);
1474 if (view->delonly == NULL)
1475 return (ISC_FALSE);
1477 new = ISC_LIST_HEAD(view->delonly[hash]);
1478 while (new != NULL && !dns_name_equal(new, name))
1479 new = ISC_LIST_NEXT(new, link);
1480 if (new == NULL)
1481 return (ISC_FALSE);
1482 return (ISC_TRUE);
1485 void
1486 dns_view_setrootdelonly(dns_view_t *view, isc_boolean_t value) {
1487 REQUIRE(DNS_VIEW_VALID(view));
1488 view->rootdelonly = value;
1491 isc_boolean_t
1492 dns_view_getrootdelonly(dns_view_t *view) {
1493 REQUIRE(DNS_VIEW_VALID(view));
1494 return (view->rootdelonly);
1497 #ifdef BIND9
1498 isc_result_t
1499 dns_view_freezezones(dns_view_t *view, isc_boolean_t value) {
1500 REQUIRE(DNS_VIEW_VALID(view));
1501 return (dns_zt_freezezones(view->zonetable, value));
1503 #endif
1505 void
1506 dns_view_setresstats(dns_view_t *view, isc_stats_t *stats) {
1507 REQUIRE(DNS_VIEW_VALID(view));
1508 REQUIRE(!view->frozen);
1509 REQUIRE(view->resstats == NULL);
1511 isc_stats_attach(stats, &view->resstats);
1514 void
1515 dns_view_getresstats(dns_view_t *view, isc_stats_t **statsp) {
1516 REQUIRE(DNS_VIEW_VALID(view));
1517 REQUIRE(statsp != NULL && *statsp == NULL);
1519 if (view->resstats != NULL)
1520 isc_stats_attach(view->resstats, statsp);
1523 void
1524 dns_view_setresquerystats(dns_view_t *view, dns_stats_t *stats) {
1525 REQUIRE(DNS_VIEW_VALID(view));
1526 REQUIRE(!view->frozen);
1527 REQUIRE(view->resquerystats == NULL);
1529 dns_stats_attach(stats, &view->resquerystats);
1532 void
1533 dns_view_getresquerystats(dns_view_t *view, dns_stats_t **statsp) {
1534 REQUIRE(DNS_VIEW_VALID(view));
1535 REQUIRE(statsp != NULL && *statsp == NULL);
1537 if (view->resquerystats != NULL)
1538 dns_stats_attach(view->resquerystats, statsp);
1541 isc_result_t
1542 dns_view_initsecroots(dns_view_t *view, isc_mem_t *mctx) {
1543 REQUIRE(DNS_VIEW_VALID(view));
1544 if (view->secroots_priv != NULL)
1545 dns_keytable_detach(&view->secroots_priv);
1546 return (dns_keytable_create(mctx, &view->secroots_priv));
1549 isc_result_t
1550 dns_view_getsecroots(dns_view_t *view, dns_keytable_t **ktp) {
1551 REQUIRE(DNS_VIEW_VALID(view));
1552 REQUIRE(ktp != NULL && *ktp == NULL);
1553 if (view->secroots_priv == NULL)
1554 return (ISC_R_NOTFOUND);
1555 dns_keytable_attach(view->secroots_priv, ktp);
1556 return (ISC_R_SUCCESS);
1559 isc_result_t
1560 dns_view_issecuredomain(dns_view_t *view, dns_name_t *name,
1561 isc_boolean_t *secure_domain) {
1562 REQUIRE(DNS_VIEW_VALID(view));
1563 return (dns_keytable_issecuredomain(view->secroots_priv, name,
1564 secure_domain));