1999-06-23 22:28:27 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
1999-06-23 22:28:27 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
1999-06-23 22:28:27 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2000-08-30 01:24:20 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2017-09-13 00:02:53 -07:00
|
|
|
#include <isc/entropy.h>
|
2009-09-01 00:22:28 +00:00
|
|
|
#include <isc/hash.h>
|
|
|
|
#include <isc/mem.h>
|
1999-06-23 22:28:27 +00:00
|
|
|
#include <isc/msgcat.h>
|
2009-09-01 00:22:28 +00:00
|
|
|
#include <isc/mutex.h>
|
|
|
|
#include <isc/once.h>
|
2000-04-28 01:12:23 +00:00
|
|
|
#include <isc/util.h>
|
1999-06-23 22:28:27 +00:00
|
|
|
|
2009-09-01 00:22:28 +00:00
|
|
|
#include <dns/db.h>
|
|
|
|
#include <dns/ecdb.h>
|
1999-06-23 22:28:27 +00:00
|
|
|
#include <dns/lib.h>
|
2009-09-01 00:22:28 +00:00
|
|
|
#include <dns/result.h>
|
|
|
|
|
|
|
|
#include <dst/dst.h>
|
|
|
|
|
1999-06-23 22:28:27 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
*** Globals
|
|
|
|
***/
|
|
|
|
|
2005-08-15 01:21:07 +00:00
|
|
|
LIBDNS_EXTERNAL_DATA unsigned int dns_pps = 0U;
|
2001-11-19 03:08:44 +00:00
|
|
|
LIBDNS_EXTERNAL_DATA isc_msgcat_t * dns_msgcat = NULL;
|
1999-06-23 22:28:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
*** Private
|
|
|
|
***/
|
|
|
|
|
|
|
|
static isc_once_t msgcat_once = ISC_ONCE_INIT;
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
*** Functions
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
|
|
|
open_msgcat(void) {
|
|
|
|
isc_msgcat_open("libdns.cat", &dns_msgcat);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dns_lib_initmsgcat(void) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the DNS library's message catalog, dns_msgcat, if it
|
|
|
|
* has not already been initialized.
|
|
|
|
*/
|
|
|
|
|
|
|
|
RUNTIME_CHECK(isc_once_do(&msgcat_once, open_msgcat) == ISC_R_SUCCESS);
|
|
|
|
}
|
2009-09-01 00:22:28 +00:00
|
|
|
|
|
|
|
static isc_once_t init_once = ISC_ONCE_INIT;
|
|
|
|
static isc_mem_t *dns_g_mctx = NULL;
|
|
|
|
static dns_dbimplementation_t *dbimp = NULL;
|
|
|
|
static isc_boolean_t initialize_done = ISC_FALSE;
|
|
|
|
static isc_mutex_t reflock;
|
|
|
|
static unsigned int references = 0;
|
|
|
|
|
|
|
|
static void
|
2009-09-03 00:12:23 +00:00
|
|
|
initialize(void) {
|
2009-09-01 00:22:28 +00:00
|
|
|
isc_result_t result;
|
2017-09-12 23:03:49 -07:00
|
|
|
isc_entropy_t *ectx = NULL;
|
2009-09-01 00:22:28 +00:00
|
|
|
|
|
|
|
REQUIRE(initialize_done == ISC_FALSE);
|
|
|
|
|
|
|
|
result = isc_mem_create(0, 0, &dns_g_mctx);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return;
|
|
|
|
dns_result_register();
|
|
|
|
result = dns_ecdb_register(dns_g_mctx, &dbimp);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_mctx;
|
2017-09-12 23:03:49 -07:00
|
|
|
result = isc_entropy_create(dns_g_mctx, &ectx);
|
2009-09-01 00:22:28 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_db;
|
|
|
|
|
2018-04-04 09:44:50 +02:00
|
|
|
result = dst_lib_init(dns_g_mctx, ectx, NULL, 0);
|
2009-09-01 00:22:28 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
2018-03-27 16:23:13 +02:00
|
|
|
goto cleanup_ectx;
|
2009-09-01 00:22:28 +00:00
|
|
|
|
|
|
|
result = isc_mutex_init(&reflock);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_dst;
|
|
|
|
|
2017-09-12 23:03:49 -07:00
|
|
|
isc_entropy_detach(&ectx);
|
2017-09-12 22:49:35 -07:00
|
|
|
|
2009-09-01 00:22:28 +00:00
|
|
|
initialize_done = ISC_TRUE;
|
|
|
|
return;
|
|
|
|
|
|
|
|
cleanup_dst:
|
|
|
|
dst_lib_destroy();
|
2017-09-12 23:03:49 -07:00
|
|
|
cleanup_ectx:
|
|
|
|
if (ectx != NULL)
|
|
|
|
isc_entropy_detach(&ectx);
|
2009-09-01 00:22:28 +00:00
|
|
|
cleanup_db:
|
2013-04-10 13:49:57 -07:00
|
|
|
if (dbimp != NULL)
|
|
|
|
dns_ecdb_unregister(&dbimp);
|
2009-09-01 00:22:28 +00:00
|
|
|
cleanup_mctx:
|
2013-04-10 13:49:57 -07:00
|
|
|
if (dns_g_mctx != NULL)
|
|
|
|
isc_mem_detach(&dns_g_mctx);
|
2009-09-01 00:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
dns_lib_init(void) {
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since this routine is expected to be used by a normal application,
|
|
|
|
* it should be better to return an error, instead of an emergency
|
|
|
|
* abort, on any failure.
|
|
|
|
*/
|
|
|
|
result = isc_once_do(&init_once, initialize);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
|
|
|
|
if (!initialize_done)
|
|
|
|
return (ISC_R_FAILURE);
|
|
|
|
|
|
|
|
LOCK(&reflock);
|
|
|
|
references++;
|
|
|
|
UNLOCK(&reflock);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dns_lib_shutdown(void) {
|
|
|
|
isc_boolean_t cleanup_ok = ISC_FALSE;
|
|
|
|
|
|
|
|
LOCK(&reflock);
|
|
|
|
if (--references == 0)
|
|
|
|
cleanup_ok = ISC_TRUE;
|
|
|
|
UNLOCK(&reflock);
|
|
|
|
|
|
|
|
if (!cleanup_ok)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dst_lib_destroy();
|
2015-09-28 23:12:35 -07:00
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
if (dbimp != NULL)
|
|
|
|
dns_ecdb_unregister(&dbimp);
|
|
|
|
if (dns_g_mctx != NULL)
|
|
|
|
isc_mem_detach(&dns_g_mctx);
|
2009-09-01 00:22:28 +00:00
|
|
|
}
|