view.c revision 113b8ef9731a0c4fb47f3ad41cb9d07d97e022a8
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff/*
ca41b452ede6feaa9d8739ec3cae19389a7b0d03Bob Halley * Copyright (C) 1999, 2000 Internet Software Consortium.
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff *
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff * Permission to use, copy, modify, and distribute this software for any
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff * purpose with or without fee is hereby granted, provided that the above
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff * copyright notice and this permission notice appear in all copies.
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff *
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff */
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff/* $Id: view.c,v 1.87 2000/12/15 21:11:35 gson Exp $ */
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff#include <config.h>
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff#include <isc/task.h>
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff#include <isc/string.h> /* Required for HP/UX (and others?) */
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff#include <isc/util.h>
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff#include <dns/acl.h>
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff#include <dns/adb.h>
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff#include <dns/cache.h>
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff#include <dns/db.h>
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff#include <dns/events.h>
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff#include <dns/forward.h>
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff#include <dns/keytable.h>
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff#include <dns/master.h>
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff#include <dns/masterdump.h>
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff#include <dns/peer.h>
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff#include <dns/rdataset.h>
364a82f7c25b62967678027043425201a5e5171aBob Halley#include <dns/request.h>
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff#include <dns/resolver.h>
e45d323a2a0f4ca08d4b139546e60a5fa7bd3f0cMichael Graff#include <dns/result.h>
fd15c8e32ed0c1cfd3ed737858a81966e7fbaeacAndreas Gustafsson#include <dns/tsig.h>
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff#include <dns/zone.h>
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff#include <dns/zt.h>
41ac1e406f1be4ec2eba2d8bae673e762d54ea74Andreas Gustafsson
efa4ebbff3c9f6f38ab8b55540fb696243c1172cAndreas Gustafsson#define RESSHUTDOWN(v) (((v)->attributes & DNS_VIEWATTR_RESSHUTDOWN) != 0)
efa4ebbff3c9f6f38ab8b55540fb696243c1172cAndreas Gustafsson#define ADBSHUTDOWN(v) (((v)->attributes & DNS_VIEWATTR_ADBSHUTDOWN) != 0)
c90f5e8d1edbd5c277f2ee320167a12a30ba7c7bMichael Graff#define REQSHUTDOWN(v) (((v)->attributes & DNS_VIEWATTR_REQSHUTDOWN) != 0)
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graffstatic void resolver_shutdown(isc_task_t *task, isc_event_t *event);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graffstatic void adb_shutdown(isc_task_t *task, isc_event_t *event);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graffstatic void req_shutdown(isc_task_t *task, isc_event_t *event);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
c803787146cadcb2d7e10cbf4491f3be513dfa1aMichael Graffisc_result_t
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graffdns_view_create(isc_mem_t *mctx, dns_rdataclass_t rdclass,
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff const char *name, dns_view_t **viewp)
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff{
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff dns_view_t *view;
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff isc_result_t result;
e198cb953c1a5bc189ae21dc3f8d622f5a08bc34Bob Halley
3ac63b472022ff92691d1fe69ac715a729671965Michael Graff /*
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff * Create a view.
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff */
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff REQUIRE(name != NULL);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff REQUIRE(viewp != NULL && *viewp == NULL);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff view = isc_mem_get(mctx, sizeof *view);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff if (view == NULL)
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff return (ISC_R_NOMEMORY);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff view->name = isc_mem_strdup(mctx, name);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff if (view->name == NULL) {
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff result = ISC_R_NOMEMORY;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff goto cleanup_view;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff }
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff result = isc_mutex_init(&view->lock);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff if (result != ISC_R_SUCCESS) {
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff UNEXPECTED_ERROR(__FILE__, __LINE__,
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff "isc_mutex_init() failed: %s",
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff isc_result_totext(result));
fc4fe6e5e61dbc916707ab2e071ffd22aef9de7fMichael Graff result = ISC_R_UNEXPECTED;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff goto cleanup_name;
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff }
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff result = isc_rwlock_init(&view->conflock, 1, 1);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff if (result != ISC_R_SUCCESS) {
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff UNEXPECTED_ERROR(__FILE__, __LINE__,
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff "isc_rwlock_init() failed: %s",
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff isc_result_totext(result));
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff result = ISC_R_UNEXPECTED;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff goto cleanup_mutex;
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff }
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff view->zonetable = NULL;
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff result = dns_zt_create(mctx, rdclass, &view->zonetable);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff if (result != ISC_R_SUCCESS) {
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff UNEXPECTED_ERROR(__FILE__, __LINE__,
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff "dns_zt_create() failed: %s",
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff isc_result_totext(result));
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff result = ISC_R_UNEXPECTED;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff goto cleanup_rwlock;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff }
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff view->secroots = NULL;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff result = dns_keytable_create(mctx, &view->secroots);
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff if (result != ISC_R_SUCCESS) {
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff UNEXPECTED_ERROR(__FILE__, __LINE__,
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff "dns_keytable_create() failed: %s",
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff isc_result_totext(result));
c803787146cadcb2d7e10cbf4491f3be513dfa1aMichael Graff result = ISC_R_UNEXPECTED;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff goto cleanup_zt;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff }
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff view->trustedkeys = NULL;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff result = dns_keytable_create(mctx, &view->trustedkeys);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff if (result != ISC_R_SUCCESS) {
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff UNEXPECTED_ERROR(__FILE__, __LINE__,
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff "dns_keytable_create() failed: %s",
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff isc_result_totext(result));
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff result = ISC_R_UNEXPECTED;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff goto cleanup_secroots;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff }
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff view->fwdtable = NULL;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff result = dns_fwdtable_create(mctx, &view->fwdtable);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff if (result != ISC_R_SUCCESS) {
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff UNEXPECTED_ERROR(__FILE__, __LINE__,
c803787146cadcb2d7e10cbf4491f3be513dfa1aMichael Graff "dns_fwdtable_create() failed: %s",
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff isc_result_totext(result));
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff result = ISC_R_UNEXPECTED;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff goto cleanup_trustedkeys;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff }
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff view->cache = NULL;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff view->cachedb = NULL;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff view->hints = NULL;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff view->resolver = NULL;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff view->adb = NULL;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff view->loadmgr = NULL;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff view->requestmgr = NULL;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff view->mctx = mctx;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff view->rdclass = rdclass;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff view->frozen = ISC_FALSE;
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff view->task = NULL;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff view->references = 1;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff view->weakrefs = 0;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff view->attributes = (DNS_VIEWATTR_RESSHUTDOWN|DNS_VIEWATTR_ADBSHUTDOWN|
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff DNS_VIEWATTR_REQSHUTDOWN);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff view->statickeys = NULL;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff view->dynamickeys = NULL;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff view->matchclients = NULL;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff result = dns_tsigkeyring_create(view->mctx, &view->dynamickeys);
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff if (result != ISC_R_SUCCESS)
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff goto cleanup_fwdtable;
897c9ddb4d745b2bfecf98b17e5487bb6656299aMichael Graff view->peers = NULL;
897c9ddb4d745b2bfecf98b17e5487bb6656299aMichael Graff
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff /*
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff * Initialize configuration data with default values.
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff */
c803787146cadcb2d7e10cbf4491f3be513dfa1aMichael Graff view->recursion = ISC_TRUE;
c803787146cadcb2d7e10cbf4491f3be513dfa1aMichael Graff view->auth_nxdomain = ISC_FALSE; /* Was true in BIND 8 */
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff view->additionalfromcache = ISC_TRUE;
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff view->additionalfromauth = ISC_TRUE;
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff view->transfer_format = dns_one_answer;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff view->queryacl = NULL;
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff view->recursionacl = NULL;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff view->sortlist = NULL;
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff view->requestixfr = ISC_TRUE;
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff view->provideixfr = ISC_TRUE;
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff view->maxcachettl = 7 * 24 * 3600;
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff view->maxncachettl = 3 * 3600;
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff view->dstport = 53;
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff view->cachefile = NULL;
3ac63b472022ff92691d1fe69ac715a729671965Michael Graff
3ac63b472022ff92691d1fe69ac715a729671965Michael Graff result = dns_peerlist_new(view->mctx, &view->peers);
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff if (result != ISC_R_SUCCESS)
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff goto cleanup_dynkeys;
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff ISC_LINK_INIT(view, link);
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff ISC_EVENT_INIT(&view->resevent, sizeof view->resevent, 0, NULL,
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff DNS_EVENT_VIEWRESSHUTDOWN, resolver_shutdown,
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff view, NULL, NULL, NULL);
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff ISC_EVENT_INIT(&view->adbevent, sizeof view->adbevent, 0, NULL,
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff DNS_EVENT_VIEWADBSHUTDOWN, adb_shutdown,
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff view, NULL, NULL, NULL);
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff ISC_EVENT_INIT(&view->reqevent, sizeof view->reqevent, 0, NULL,
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff DNS_EVENT_VIEWREQSHUTDOWN, req_shutdown,
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff view, NULL, NULL, NULL);
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff view->magic = DNS_VIEW_MAGIC;
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
fd15c8e32ed0c1cfd3ed737858a81966e7fbaeacAndreas Gustafsson *viewp = view;
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff return (ISC_R_SUCCESS);
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff cleanup_dynkeys:
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff dns_tsigkeyring_destroy(&view->dynamickeys);
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff cleanup_fwdtable:
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff dns_fwdtable_destroy(&view->fwdtable);
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff cleanup_trustedkeys:
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff dns_keytable_detach(&view->trustedkeys);
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
fd15c8e32ed0c1cfd3ed737858a81966e7fbaeacAndreas Gustafsson cleanup_secroots:
fd15c8e32ed0c1cfd3ed737858a81966e7fbaeacAndreas Gustafsson dns_keytable_detach(&view->secroots);
fd15c8e32ed0c1cfd3ed737858a81966e7fbaeacAndreas Gustafsson
fd15c8e32ed0c1cfd3ed737858a81966e7fbaeacAndreas Gustafsson cleanup_zt:
fd15c8e32ed0c1cfd3ed737858a81966e7fbaeacAndreas Gustafsson dns_zt_detach(&view->zonetable);
fd15c8e32ed0c1cfd3ed737858a81966e7fbaeacAndreas Gustafsson
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff cleanup_rwlock:
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff isc_rwlock_destroy(&view->conflock);
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff cleanup_mutex:
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff DESTROYLOCK(&view->lock);
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff cleanup_name:
3ac63b472022ff92691d1fe69ac715a729671965Michael Graff isc_mem_free(mctx, view->name);
5bd937c666375d03ee6a366a13d0bdb31aa3f07fBob Halley
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff cleanup_view:
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff isc_mem_put(mctx, view, sizeof *view);
efa4ebbff3c9f6f38ab8b55540fb696243c1172cAndreas Gustafsson
e592dd7c344052ee51eb707cd744b48b34f4c74eBob Halley return (result);
efa4ebbff3c9f6f38ab8b55540fb696243c1172cAndreas Gustafsson}
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
36ca83769dbba29a3d8670eef9acd95c7a71a7f6Michael Graffstatic inline void
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graffdestroy(dns_view_t *view) {
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff REQUIRE(!ISC_LINK_LINKED(view, link));
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff REQUIRE(view->references == 0);
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff REQUIRE(view->weakrefs == 0);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff REQUIRE(RESSHUTDOWN(view));
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff REQUIRE(ADBSHUTDOWN(view));
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff REQUIRE(REQSHUTDOWN(view));
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff if (view->peers != NULL)
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff dns_peerlist_detach(&view->peers);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff if (view->dynamickeys != NULL)
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff dns_tsigkeyring_destroy(&view->dynamickeys);
528829aa8ad69238e674cd81078bc14d4199691bMichael Graff if (view->statickeys != NULL)
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff dns_tsigkeyring_destroy(&view->statickeys);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff if (view->adb != NULL)
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff dns_adb_detach(&view->adb);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff if (view->resolver != NULL)
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence dns_resolver_detach(&view->resolver);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff if (view->loadmgr != NULL)
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence dns_loadmgr_detach(&view->loadmgr);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff if (view->requestmgr != NULL)
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff dns_requestmgr_detach(&view->requestmgr);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff if (view->task != NULL)
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff isc_task_detach(&view->task);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff if (view->hints != NULL)
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff dns_db_detach(&view->hints);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff if (view->cachedb != NULL)
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff dns_db_detach(&view->cachedb);
cebd4498636d3d480f6f2a7aa2eb72bd2ed64010Michael Graff if (view->cache != NULL)
cebd4498636d3d480f6f2a7aa2eb72bd2ed64010Michael Graff dns_cache_detach(&view->cache);
cebd4498636d3d480f6f2a7aa2eb72bd2ed64010Michael Graff if (view->matchclients != NULL)
cebd4498636d3d480f6f2a7aa2eb72bd2ed64010Michael Graff dns_acl_detach(&view->matchclients);
1a0e33bc2044e1902493111db14cbf793083ac47Michael Graff if (view->queryacl != NULL)
1a0e33bc2044e1902493111db14cbf793083ac47Michael Graff dns_acl_detach(&view->queryacl);
897c9ddb4d745b2bfecf98b17e5487bb6656299aMichael Graff if (view->recursionacl != NULL)
528829aa8ad69238e674cd81078bc14d4199691bMichael Graff dns_acl_detach(&view->recursionacl);
6a68df334f6dd02429d3e7a8866e552e9e9d84ecBob Halley if (view->sortlist != NULL)
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff dns_acl_detach(&view->sortlist);
c803787146cadcb2d7e10cbf4491f3be513dfa1aMichael Graff if (view->cachefile != NULL)
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff isc_mem_free(view->mctx, view->cachefile);
c803787146cadcb2d7e10cbf4491f3be513dfa1aMichael Graff dns_keytable_detach(&view->trustedkeys);
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff dns_keytable_detach(&view->secroots);
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff dns_fwdtable_destroy(&view->fwdtable);
c803787146cadcb2d7e10cbf4491f3be513dfa1aMichael Graff isc_rwlock_destroy(&view->conflock);
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff DESTROYLOCK(&view->lock);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff isc_mem_free(view->mctx, view->name);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff isc_mem_put(view->mctx, view, sizeof *view);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff}
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff/*
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff * Return true iff 'view' may be freed.
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff * The caller must be holding the view lock.
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff */
910df98b0efcbe8380b952887f4071051cc39a25Michael Graffstatic isc_boolean_t
da547174e2b7beb6d6119d58197ad0bc85b91179Michael Graffall_done(dns_view_t *view) {
da547174e2b7beb6d6119d58197ad0bc85b91179Michael Graff
da547174e2b7beb6d6119d58197ad0bc85b91179Michael Graff if (view->references == 0 && view->weakrefs == 0 &&
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff RESSHUTDOWN(view) && ADBSHUTDOWN(view) && REQSHUTDOWN(view))
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff return (ISC_TRUE);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff return (ISC_FALSE);
58aaab3687aac838542ee4ef65a9c094a5d34ab0Michael Graff}
f00d96a15cdd11e764437f9359e67328631caaeaMichael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graffvoid
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graffdns_view_attach(dns_view_t *source, dns_view_t **targetp) {
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff REQUIRE(DNS_VIEW_VALID(source));
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff REQUIRE(targetp != NULL && *targetp == NULL);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff LOCK(&source->lock);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff INSIST(source->references > 0);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff source->references++;
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff INSIST(source->references != 0);
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff UNLOCK(&source->lock);
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff *targetp = source;
e198cb953c1a5bc189ae21dc3f8d622f5a08bc34Bob Halley}
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff
c90f5e8d1edbd5c277f2ee320167a12a30ba7c7bMichael Graffstatic void
e198cb953c1a5bc189ae21dc3f8d622f5a08bc34Bob Halleyview_flushanddetach(dns_view_t **viewp, isc_boolean_t flush) {
c90f5e8d1edbd5c277f2ee320167a12a30ba7c7bMichael Graff dns_view_t *view;
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff isc_boolean_t done = ISC_FALSE;
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff REQUIRE(viewp != NULL);
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff view = *viewp;
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff REQUIRE(DNS_VIEW_VALID(view));
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff LOCK(&view->lock);
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff
e198cb953c1a5bc189ae21dc3f8d622f5a08bc34Bob Halley INSIST(view->references > 0);
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff view->references--;
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff if (view->references == 0) {
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff if (!RESSHUTDOWN(view))
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff dns_resolver_shutdown(view->resolver);
e198cb953c1a5bc189ae21dc3f8d622f5a08bc34Bob Halley if (!ADBSHUTDOWN(view))
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff dns_adb_shutdown(view->adb);
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff if (!REQSHUTDOWN(view))
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff dns_requestmgr_shutdown(view->requestmgr);
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff if (flush)
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff dns_zt_flushanddetach(&view->zonetable);
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff else
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff dns_zt_detach(&view->zonetable);
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff done = all_done(view);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff }
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff UNLOCK(&view->lock);
738b9aa3ded1ef724922d6695cb04ec2e721bdd1Bob Halley
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff *viewp = NULL;
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
738b9aa3ded1ef724922d6695cb04ec2e721bdd1Bob Halley if (done)
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff destroy(view);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff}
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graffvoid
910df98b0efcbe8380b952887f4071051cc39a25Michael Graffdns_view_flushanddetach(dns_view_t **viewp) {
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff view_flushanddetach(viewp, ISC_TRUE);
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff}
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graffvoid
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graffdns_view_detach(dns_view_t **viewp) {
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff view_flushanddetach(viewp, ISC_FALSE);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff}
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graffstatic isc_result_t
cebd4498636d3d480f6f2a7aa2eb72bd2ed64010Michael Graffdialup(dns_zone_t *zone, void *dummy) {
cebd4498636d3d480f6f2a7aa2eb72bd2ed64010Michael Graff UNUSED(dummy);
cebd4498636d3d480f6f2a7aa2eb72bd2ed64010Michael Graff dns_zone_dialup(zone);
cebd4498636d3d480f6f2a7aa2eb72bd2ed64010Michael Graff return (ISC_R_SUCCESS);
1c3bc66ada38236cc81c41b7174a9f0a872c9ab6Michael Graff}
1c3bc66ada38236cc81c41b7174a9f0a872c9ab6Michael Graff
1c3bc66ada38236cc81c41b7174a9f0a872c9ab6Michael Graffvoid
528829aa8ad69238e674cd81078bc14d4199691bMichael Graffdns_view_dialup(dns_view_t *view) {
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff REQUIRE(DNS_VIEW_VALID(view));
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff dns_zt_apply(view->zonetable, ISC_FALSE, dialup, NULL);
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff}
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graffvoid
1a0e33bc2044e1902493111db14cbf793083ac47Michael Graffdns_view_weakattach(dns_view_t *source, dns_view_t **targetp) {
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff REQUIRE(DNS_VIEW_VALID(source));
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff REQUIRE(targetp != NULL && *targetp == NULL);
1a0e33bc2044e1902493111db14cbf793083ac47Michael Graff
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff LOCK(&source->lock);
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff source->weakrefs++;
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff UNLOCK(&source->lock);
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff *targetp = source;
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff}
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graffvoid
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graffdns_view_weakdetach(dns_view_t **viewp) {
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff dns_view_t *view;
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff isc_boolean_t done = ISC_FALSE;
f942258b6380ba1f2c2f057a79ffc37bc3436488Michael Graff
78854e02c127f31ab90f56da0531542004b45377Michael Graff REQUIRE(viewp != NULL);
970cccf46e0f949a1a9edbcd8302dd2a112b43c2Michael Graff view = *viewp;
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff REQUIRE(DNS_VIEW_VALID(view));
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff
29f28fe573d4b3b318b3b026d567c1eb86738015Michael Graff LOCK(&view->lock);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff INSIST(view->weakrefs > 0);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff view->weakrefs--;
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff done = all_done(view);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff UNLOCK(&view->lock);
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff
30251e07d1705d1a85b0e1d5a969496e1aed612eMichael Graff *viewp = NULL;
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff
1c3bc66ada38236cc81c41b7174a9f0a872c9ab6Michael Graff if (done)
d4d2a13916a114879763562db6a19b70b1444ec1Michael Graff destroy(view);
ff9bb3fc5453bbf310b67c560fbf04a5c0fb60daMichael Graff}
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graff
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graffstatic void
efc8a09e19dfcfafa92fd2ad113073a4f5295e9bMichael Graffresolver_shutdown(isc_task_t *task, isc_event_t *event) {
a9ece9973c35d4d780338e89e288fb6a59575324Michael Graff dns_view_t *view = event->ev_arg;
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff isc_boolean_t done;
3ac63b472022ff92691d1fe69ac715a729671965Michael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff REQUIRE(event->ev_type == DNS_EVENT_VIEWRESSHUTDOWN);
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff REQUIRE(DNS_VIEW_VALID(view));
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff REQUIRE(view->task == task);
2bcb48cfcae36398454c98e40c563e2cde748e07Michael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff UNUSED(task);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff LOCK(&view->lock);
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff
86944a4c8002e80ae9b6eb5a5e29b797879be45fMichael Graff view->attributes |= DNS_VIEWATTR_RESSHUTDOWN;
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff done = all_done(view);
910df98b0efcbe8380b952887f4071051cc39a25Michael Graff
UNLOCK(&view->lock);
isc_event_free(&event);
if (done)
destroy(view);
}
static void
adb_shutdown(isc_task_t *task, isc_event_t *event) {
dns_view_t *view = event->ev_arg;
isc_boolean_t done;
REQUIRE(event->ev_type == DNS_EVENT_VIEWADBSHUTDOWN);
REQUIRE(DNS_VIEW_VALID(view));
REQUIRE(view->task == task);
UNUSED(task);
LOCK(&view->lock);
view->attributes |= DNS_VIEWATTR_ADBSHUTDOWN;
done = all_done(view);
UNLOCK(&view->lock);
isc_event_free(&event);
if (done)
destroy(view);
}
static void
req_shutdown(isc_task_t *task, isc_event_t *event) {
dns_view_t *view = event->ev_arg;
isc_boolean_t done;
REQUIRE(event->ev_type == DNS_EVENT_VIEWREQSHUTDOWN);
REQUIRE(DNS_VIEW_VALID(view));
REQUIRE(view->task == task);
UNUSED(task);
LOCK(&view->lock);
view->attributes |= DNS_VIEWATTR_REQSHUTDOWN;
done = all_done(view);
UNLOCK(&view->lock);
isc_event_free(&event);
if (done)
destroy(view);
}
isc_result_t
dns_view_createresolver(dns_view_t *view,
isc_taskmgr_t *taskmgr, unsigned int ntasks,
isc_socketmgr_t *socketmgr,
isc_timermgr_t *timermgr,
unsigned int options,
dns_dispatchmgr_t *dispatchmgr,
dns_dispatch_t *dispatchv4,
dns_dispatch_t *dispatchv6)
{
isc_result_t result;
isc_event_t *event;
REQUIRE(DNS_VIEW_VALID(view));
REQUIRE(!view->frozen);
REQUIRE(view->resolver == NULL);
result = isc_task_create(taskmgr, 0, &view->task);
if (result != ISC_R_SUCCESS)
return (result);
isc_task_setname(view->task, "view", view);
result = dns_resolver_create(view, taskmgr, ntasks, socketmgr,
timermgr, options, dispatchmgr,
dispatchv4, dispatchv6,
&view->resolver);
if (result != ISC_R_SUCCESS) {
isc_task_detach(&view->task);
return (result);
}
event = &view->resevent;
dns_resolver_whenshutdown(view->resolver, view->task, &event);
view->attributes &= ~DNS_VIEWATTR_RESSHUTDOWN;
result = dns_adb_create(view->mctx, view, timermgr, taskmgr,
&view->adb);
if (result != ISC_R_SUCCESS) {
dns_resolver_shutdown(view->resolver);
return (result);
}
event = &view->adbevent;
dns_adb_whenshutdown(view->adb, view->task, &event);
view->attributes &= ~DNS_VIEWATTR_ADBSHUTDOWN;
result = dns_requestmgr_create(view->mctx, timermgr, socketmgr,
dns_resolver_taskmgr(view->resolver),
dns_resolver_dispatchmgr(view->resolver),
dns_resolver_dispatchv4(view->resolver),
dns_resolver_dispatchv6(view->resolver),
&view->requestmgr);
if (result != ISC_R_SUCCESS) {
dns_adb_shutdown(view->adb);
dns_resolver_shutdown(view->resolver);
return (result);
}
event = &view->reqevent;
dns_requestmgr_whenshutdown(view->requestmgr, view->task, &event);
view->attributes &= ~DNS_VIEWATTR_REQSHUTDOWN;
return (ISC_R_SUCCESS);
}
void
dns_view_setcache(dns_view_t *view, dns_cache_t *cache) {
REQUIRE(DNS_VIEW_VALID(view));
REQUIRE(!view->frozen);
if (view->cache != NULL) {
dns_db_detach(&view->cachedb);
dns_cache_detach(&view->cache);
}
dns_cache_attach(cache, &view->cache);
dns_cache_attachdb(cache, &view->cachedb);
INSIST(DNS_DB_VALID(view->cachedb));
}
void
dns_view_sethints(dns_view_t *view, dns_db_t *hints) {
REQUIRE(DNS_VIEW_VALID(view));
REQUIRE(!view->frozen);
REQUIRE(view->hints == NULL);
REQUIRE(dns_db_iszone(hints));
dns_db_attach(hints, &view->hints);
}
void
dns_view_setkeyring(dns_view_t *view, dns_tsig_keyring_t *ring) {
REQUIRE(DNS_VIEW_VALID(view));
REQUIRE(ring != NULL);
if (view->statickeys != NULL)
dns_tsigkeyring_destroy(&view->statickeys);
view->statickeys = ring;
}
void
dns_view_setdstport(dns_view_t *view, in_port_t dstport) {
REQUIRE(DNS_VIEW_VALID(view));
view->dstport = dstport;
}
void
dns_view_setloadmgr(dns_view_t *view, dns_loadmgr_t *loadmgr) {
REQUIRE(DNS_VIEW_VALID(view));
if (view->loadmgr != NULL)
dns_loadmgr_detach(&view->loadmgr);
dns_loadmgr_attach(loadmgr, &view->loadmgr);
}
isc_result_t
dns_view_addzone(dns_view_t *view, dns_zone_t *zone) {
isc_result_t result;
REQUIRE(DNS_VIEW_VALID(view));
REQUIRE(!view->frozen);
result = dns_zt_mount(view->zonetable, zone);
return (result);
}
void
dns_view_freeze(dns_view_t *view) {
REQUIRE(DNS_VIEW_VALID(view));
REQUIRE(!view->frozen);
if (view->resolver != NULL) {
INSIST(view->cachedb != NULL);
dns_resolver_freeze(view->resolver);
}
view->frozen = ISC_TRUE;
}
isc_result_t
dns_view_findzone(dns_view_t *view, dns_name_t *name, dns_zone_t **zonep) {
isc_result_t result;
REQUIRE(DNS_VIEW_VALID(view));
result = dns_zt_find(view->zonetable, name, 0, NULL, zonep);
if (result == DNS_R_PARTIALMATCH) {
dns_zone_detach(zonep);
result = ISC_R_NOTFOUND;
}
return (result);
}
isc_result_t
dns_view_find(dns_view_t *view, dns_name_t *name, dns_rdatatype_t type,
isc_stdtime_t now, unsigned int options,
isc_boolean_t use_hints, dns_name_t *foundname,
dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
{
isc_result_t result;
dns_db_t *db;
isc_boolean_t is_cache;
dns_rdataset_t zrdataset, zsigrdataset;
dns_zone_t *zone;
/*
* Find an rdataset whose owner name is 'name', and whose type is
* 'type'.
*/
REQUIRE(DNS_VIEW_VALID(view));
REQUIRE(view->frozen);
REQUIRE(type != dns_rdatatype_any && type != dns_rdatatype_sig);
/*
* Initialize.
*/
dns_rdataset_init(&zrdataset);
dns_rdataset_init(&zsigrdataset);
/*
* Find a database to answer the query.
*/
zone = NULL;
db = NULL;
result = dns_zt_find(view->zonetable, name, 0, NULL, &zone);
if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
result = dns_zone_getdb(zone, &db);
if (result != ISC_R_SUCCESS && view->cachedb != NULL)
dns_db_attach(view->cachedb, &db);
else if (result != ISC_R_SUCCESS)
goto cleanup;
} else if (result == ISC_R_NOTFOUND && view->cachedb != NULL)
dns_db_attach(view->cachedb, &db);
else
goto cleanup;
is_cache = dns_db_iscache(db);
db_find:
/*
* Now look for an answer in the database.
*/
result = dns_db_find(db, name, NULL, type, options,
now, NULL, foundname, rdataset, sigrdataset);
if (result == DNS_R_DELEGATION ||
result == ISC_R_NOTFOUND) {
if (dns_rdataset_isassociated(rdataset))
dns_rdataset_disassociate(rdataset);
if (sigrdataset != NULL &&
dns_rdataset_isassociated(sigrdataset))
dns_rdataset_disassociate(sigrdataset);
if (!is_cache) {
if (view->cachedb != NULL) {
/*
* Either the answer is in the cache, or we
* don't know it.
*/
is_cache = ISC_TRUE;
dns_db_detach(&db);
dns_db_attach(view->cachedb, &db);
goto db_find;
}
} else {
/*
* We don't have the data in the cache. If we've got
* glue from the zone, use it.
*/
if (dns_rdataset_isassociated(&zrdataset)) {
dns_rdataset_clone(&zrdataset, rdataset);
if (sigrdataset != NULL &&
dns_rdataset_isassociated(&zsigrdataset))
dns_rdataset_clone(&zsigrdataset,
sigrdataset);
result = DNS_R_GLUE;
goto cleanup;
}
}
/*
* We don't know the answer.
*/
result = ISC_R_NOTFOUND;
} else if (result == DNS_R_GLUE) {
if (view->cachedb != NULL) {
/*
* We found an answer, but the cache may be better.
* Remember what we've got and go look in the cache.
*/
is_cache = ISC_TRUE;
dns_rdataset_clone(rdataset, &zrdataset);
dns_rdataset_disassociate(rdataset);
if (sigrdataset != NULL &&
dns_rdataset_isassociated(sigrdataset)) {
dns_rdataset_clone(sigrdataset, &zsigrdataset);
dns_rdataset_disassociate(sigrdataset);
}
dns_db_detach(&db);
dns_db_attach(view->cachedb, &db);
goto db_find;
}
/*
* Otherwise, the glue is the best answer.
*/
result = ISC_R_SUCCESS;
}
if (result == ISC_R_NOTFOUND && use_hints && view->hints != NULL) {
if (dns_rdataset_isassociated(rdataset))
dns_rdataset_disassociate(rdataset);
if (sigrdataset != NULL &&
dns_rdataset_isassociated(sigrdataset))
dns_rdataset_disassociate(sigrdataset);
result = dns_db_find(view->hints, name, NULL, type, options,
now, NULL, foundname,
rdataset, sigrdataset);
if (result == ISC_R_SUCCESS || result == DNS_R_GLUE) {
/*
* We just used a hint. Let the resolver know it
* should consider priming.
*/
dns_resolver_prime(view->resolver);
result = DNS_R_HINT;
} else if (result == DNS_R_NXDOMAIN ||
result == DNS_R_NXRRSET)
result = ISC_R_NOTFOUND;
}
cleanup:
if (result == DNS_R_NXDOMAIN || result == DNS_R_NXRRSET) {
/*
* We don't care about any DNSSEC proof data in these cases.
*/
if (dns_rdataset_isassociated(rdataset))
dns_rdataset_disassociate(rdataset);
if (sigrdataset != NULL &&
dns_rdataset_isassociated(sigrdataset))
dns_rdataset_disassociate(sigrdataset);
}
if (dns_rdataset_isassociated(&zrdataset)) {
dns_rdataset_disassociate(&zrdataset);
if (dns_rdataset_isassociated(&zsigrdataset))
dns_rdataset_disassociate(&zsigrdataset);
}
if (db != NULL)
dns_db_detach(&db);
if (zone != NULL)
dns_zone_detach(&zone);
return (result);
}
isc_result_t
dns_view_simplefind(dns_view_t *view, dns_name_t *name, dns_rdatatype_t type,
isc_stdtime_t now, unsigned int options,
isc_boolean_t use_hints,
dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
{
isc_result_t result;
dns_fixedname_t foundname;
dns_fixedname_init(&foundname);
result = dns_view_find(view, name, type, now, options, use_hints,
dns_fixedname_name(&foundname),
rdataset, sigrdataset);
if (result == DNS_R_NXDOMAIN) {
/*
* The rdataset and sigrdataset of the relevant NXT record
* may be returned, but the caller cannot use them because
* foundname is not returned by this simplified API. We
* disassociate them here to prevent any misuse by the caller.
*/
if (dns_rdataset_isassociated(rdataset))
dns_rdataset_disassociate(rdataset);
if (sigrdataset != NULL &&
dns_rdataset_isassociated(sigrdataset))
dns_rdataset_disassociate(sigrdataset);
} else if (result != ISC_R_SUCCESS &&
result != DNS_R_GLUE &&
result != DNS_R_HINT &&
result != DNS_R_NCACHENXDOMAIN &&
result != DNS_R_NCACHENXRRSET &&
result != DNS_R_NXRRSET &&
result != ISC_R_NOTFOUND) {
if (dns_rdataset_isassociated(rdataset))
dns_rdataset_disassociate(rdataset);
if (sigrdataset != NULL &&
dns_rdataset_isassociated(sigrdataset))
dns_rdataset_disassociate(sigrdataset);
result = ISC_R_NOTFOUND;
}
return (result);
}
isc_result_t
dns_view_findzonecut(dns_view_t *view, dns_name_t *name, dns_name_t *fname,
isc_stdtime_t now, unsigned int options,
isc_boolean_t use_hints,
dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
{
isc_result_t result;
dns_db_t *db;
isc_boolean_t is_cache, use_zone, try_hints;
dns_zone_t *zone;
dns_name_t *zfname;
dns_rdataset_t zrdataset, zsigrdataset;
dns_fixedname_t zfixedname;
REQUIRE(DNS_VIEW_VALID(view));
REQUIRE(view->frozen);
db = NULL;
zone = NULL;
use_zone = ISC_FALSE;
try_hints = ISC_FALSE;
zfname = NULL;
/*
* Initialize.
*/
dns_fixedname_init(&zfixedname);
dns_rdataset_init(&zrdataset);
dns_rdataset_init(&zsigrdataset);
/*
* Find the right database.
*/
result = dns_zt_find(view->zonetable, name, 0, NULL, &zone);
if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
result = dns_zone_getdb(zone, &db);
if (result == ISC_R_NOTFOUND) {
/*
* We're not directly authoritative for this query name, nor
* is it a subdomain of any zone for which we're
* authoritative.
*/
if (view->cachedb != NULL) {
/*
* We have a cache; try it.
*/
dns_db_attach(view->cachedb, &db);
} else {
/*
* Maybe we have hints...
*/
try_hints = ISC_TRUE;
goto finish;
}
} else if (result != ISC_R_SUCCESS) {
/*
* Something is broken.
*/
goto cleanup;
}
is_cache = dns_db_iscache(db);
db_find:
/*
* Look for the zonecut.
*/
if (!is_cache) {
result = dns_db_find(db, name, NULL, dns_rdatatype_ns, options,
now, NULL, fname, rdataset, sigrdataset);
if (result == DNS_R_DELEGATION)
result = ISC_R_SUCCESS;
else if (result != ISC_R_SUCCESS)
goto cleanup;
if (view->cachedb != NULL && db != view->hints) {
/*
* We found an answer, but the cache may be better.
*/
zfname = dns_fixedname_name(&zfixedname);
result = dns_name_concatenate(fname, NULL, zfname,
NULL);
if (result != ISC_R_SUCCESS)
goto cleanup;
dns_rdataset_clone(rdataset, &zrdataset);
dns_rdataset_disassociate(rdataset);
if (sigrdataset != NULL &&
dns_rdataset_isassociated(sigrdataset)) {
dns_rdataset_clone(sigrdataset, &zsigrdataset);
dns_rdataset_disassociate(sigrdataset);
}
dns_db_detach(&db);
dns_db_attach(view->cachedb, &db);
is_cache = ISC_TRUE;
goto db_find;
}
} else {
result = dns_db_findzonecut(db, name, options, now, NULL,
fname, rdataset, sigrdataset);
if (result == ISC_R_SUCCESS) {
if (zfname != NULL &&
!dns_name_issubdomain(fname, zfname)) {
/*
* We found a zonecut in the cache, but our
* zone delegation is better.
*/
use_zone = ISC_TRUE;
}
} else if (result == ISC_R_NOTFOUND) {
if (zfname != NULL) {
/*
* We didn't find anything in the cache, but we
* have a zone delegation, so use it.
*/
use_zone = ISC_TRUE;
} else {
/*
* Maybe we have hints...
*/
try_hints = ISC_TRUE;
}
} else {
/*
* Something bad happened.
*/
goto cleanup;
}
}
finish:
if (use_zone) {
if (dns_rdataset_isassociated(rdataset)) {
dns_rdataset_disassociate(rdataset);
if (sigrdataset != NULL &&
dns_rdataset_isassociated(sigrdataset))
dns_rdataset_disassociate(sigrdataset);
}
result = dns_name_concatenate(zfname, NULL, fname, NULL);
if (result != ISC_R_SUCCESS)
goto cleanup;
dns_rdataset_clone(&zrdataset, rdataset);
if (sigrdataset != NULL &&
dns_rdataset_isassociated(&zrdataset))
dns_rdataset_clone(&zsigrdataset, sigrdataset);
} else if (try_hints && use_hints && view->hints != NULL) {
/*
* We've found nothing so far, but we have hints.
*/
result = dns_db_find(view->hints, dns_rootname, NULL,
dns_rdatatype_ns, 0, now, NULL, fname,
rdataset, NULL);
if (result != ISC_R_SUCCESS) {
/*
* We can't even find the hints for the root
* nameservers!
*/
result = ISC_R_NOTFOUND;
}
}
cleanup:
if (dns_rdataset_isassociated(&zrdataset)) {
dns_rdataset_disassociate(&zrdataset);
if (dns_rdataset_isassociated(&zsigrdataset))
dns_rdataset_disassociate(&zsigrdataset);
}
if (db != NULL)
dns_db_detach(&db);
if (zone != NULL)
dns_zone_detach(&zone);
return (result);
}
isc_result_t
dns_viewlist_find(dns_viewlist_t *list, const char *name,
dns_rdataclass_t rdclass, dns_view_t **viewp)
{
dns_view_t *view;
REQUIRE(list != NULL);
for (view = ISC_LIST_HEAD(*list);
view != NULL;
view = ISC_LIST_NEXT(view, link)) {
if (strcmp(view->name, name) == 0 && view->rdclass == rdclass)
break;
}
if (view == NULL)
return (ISC_R_NOTFOUND);
dns_view_attach(view, viewp);
return (ISC_R_SUCCESS);
}
isc_result_t
dns_view_load(dns_view_t *view, isc_boolean_t stop) {
REQUIRE(DNS_VIEW_VALID(view));
return (dns_zt_load(view->zonetable, stop));
}
isc_result_t
dns_view_gettsig(dns_view_t *view, dns_name_t *keyname, dns_tsigkey_t **keyp)
{
isc_result_t result;
REQUIRE(keyp != NULL && *keyp == NULL);
result = dns_tsigkey_find(keyp, keyname, NULL,
view->statickeys);
if (result == ISC_R_NOTFOUND)
result = dns_tsigkey_find(keyp, keyname, NULL,
view->dynamickeys);
return (result);
}
isc_result_t
dns_view_getpeertsig(dns_view_t *view, isc_netaddr_t *peeraddr,
dns_tsigkey_t **keyp)
{
isc_result_t result;
dns_name_t *keyname = NULL;
dns_peer_t *peer = NULL;
result = dns_peerlist_peerbyaddr(view->peers, peeraddr, &peer);
if (result != ISC_R_SUCCESS)
return (result);
result = dns_peer_getkey(peer, &keyname);
if (result != ISC_R_SUCCESS)
return (result);
return (dns_view_gettsig(view, keyname, keyp));
}
isc_result_t
dns_view_checksig(dns_view_t *view, isc_buffer_t *source, dns_message_t *msg) {
REQUIRE(DNS_VIEW_VALID(view));
REQUIRE(source != NULL);
return (dns_tsig_verify(source, msg, view->statickeys,
view->dynamickeys));
}
isc_result_t
dns_view_dumpcache(dns_view_t *view) {
REQUIRE(DNS_VIEW_VALID(view));
if (view->cachefile == NULL)
return (ISC_R_IGNORE);
return (dns_master_dump(view->mctx, view->cachedb, NULL,
&dns_master_style_default, view->cachefile));
}
isc_result_t
dns_view_dumpdbtostream(dns_view_t *view, FILE *fp) {
isc_result_t result;
REQUIRE(DNS_VIEW_VALID(view));
(void)fprintf(fp, ";\n; Cache dump of view '%s'\n;\n", view->name);
result = dns_master_dumptostream(view->mctx, view->cachedb, NULL,
&dns_master_style_explicitttl, fp);
if (result != ISC_R_SUCCESS)
return (result);
#ifdef notyet /* clean up adb dump format first */
dns_adb_dump(view->adb, fp);
#endif
return (ISC_R_SUCCESS);
}