Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / db.c
blobb09ae3dbb0fd52a62c1f64cd34f1218c58f6c111
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007-2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001, 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: db.c,v 1.95 2009/10/08 23:13:06 marka Exp */
22 /*! \file */
24 /***
25 *** Imports
26 ***/
28 #include <config.h>
30 #include <isc/buffer.h>
31 #include <isc/mem.h>
32 #include <isc/once.h>
33 #include <isc/rwlock.h>
34 #include <isc/string.h>
35 #include <isc/util.h>
37 #include <dns/callbacks.h>
38 #include <dns/db.h>
39 #include <dns/dbiterator.h>
40 #include <dns/log.h>
41 #include <dns/master.h>
42 #include <dns/rdata.h>
43 #include <dns/rdataset.h>
44 #include <dns/rdatasetiter.h>
45 #include <dns/result.h>
47 /***
48 *** Private Types
49 ***/
51 struct dns_dbimplementation {
52 const char * name;
53 dns_dbcreatefunc_t create;
54 isc_mem_t * mctx;
55 void * driverarg;
56 ISC_LINK(dns_dbimplementation_t) link;
59 /***
60 *** Supported DB Implementations Registry
61 ***/
64 * Built in database implementations are registered here.
67 #include "rbtdb.h"
68 #ifdef BIND9
69 #include "rbtdb64.h"
70 #endif
72 static ISC_LIST(dns_dbimplementation_t) implementations;
73 static isc_rwlock_t implock;
74 static isc_once_t once = ISC_ONCE_INIT;
76 static dns_dbimplementation_t rbtimp;
77 #ifdef BIND9
78 static dns_dbimplementation_t rbt64imp;
79 #endif
81 static void
82 initialize(void) {
83 RUNTIME_CHECK(isc_rwlock_init(&implock, 0, 0) == ISC_R_SUCCESS);
85 rbtimp.name = "rbt";
86 rbtimp.create = dns_rbtdb_create;
87 rbtimp.mctx = NULL;
88 rbtimp.driverarg = NULL;
89 ISC_LINK_INIT(&rbtimp, link);
91 #ifdef BIND9
92 rbt64imp.name = "rbt64";
93 rbt64imp.create = dns_rbtdb64_create;
94 rbt64imp.mctx = NULL;
95 rbt64imp.driverarg = NULL;
96 ISC_LINK_INIT(&rbt64imp, link);
97 #endif
99 ISC_LIST_INIT(implementations);
100 ISC_LIST_APPEND(implementations, &rbtimp, link);
101 #ifdef BIND9
102 ISC_LIST_APPEND(implementations, &rbt64imp, link);
103 #endif
106 static inline dns_dbimplementation_t *
107 impfind(const char *name) {
108 dns_dbimplementation_t *imp;
110 for (imp = ISC_LIST_HEAD(implementations);
111 imp != NULL;
112 imp = ISC_LIST_NEXT(imp, link))
113 if (strcasecmp(name, imp->name) == 0)
114 return (imp);
115 return (NULL);
119 /***
120 *** Basic DB Methods
121 ***/
123 isc_result_t
124 dns_db_create(isc_mem_t *mctx, const char *db_type, dns_name_t *origin,
125 dns_dbtype_t type, dns_rdataclass_t rdclass,
126 unsigned int argc, char *argv[], dns_db_t **dbp)
128 dns_dbimplementation_t *impinfo;
130 RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
133 * Create a new database using implementation 'db_type'.
136 REQUIRE(dbp != NULL && *dbp == NULL);
137 REQUIRE(dns_name_isabsolute(origin));
139 RWLOCK(&implock, isc_rwlocktype_read);
140 impinfo = impfind(db_type);
141 if (impinfo != NULL) {
142 isc_result_t result;
143 result = ((impinfo->create)(mctx, origin, type,
144 rdclass, argc, argv,
145 impinfo->driverarg, dbp));
146 RWUNLOCK(&implock, isc_rwlocktype_read);
147 return (result);
150 RWUNLOCK(&implock, isc_rwlocktype_read);
152 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
153 DNS_LOGMODULE_DB, ISC_LOG_ERROR,
154 "unsupported database type '%s'", db_type);
156 return (ISC_R_NOTFOUND);
159 void
160 dns_db_attach(dns_db_t *source, dns_db_t **targetp) {
163 * Attach *targetp to source.
166 REQUIRE(DNS_DB_VALID(source));
167 REQUIRE(targetp != NULL && *targetp == NULL);
169 (source->methods->attach)(source, targetp);
171 ENSURE(*targetp == source);
174 void
175 dns_db_detach(dns_db_t **dbp) {
178 * Detach *dbp from its database.
181 REQUIRE(dbp != NULL);
182 REQUIRE(DNS_DB_VALID(*dbp));
184 ((*dbp)->methods->detach)(dbp);
186 ENSURE(*dbp == NULL);
189 isc_result_t
190 dns_db_ondestroy(dns_db_t *db, isc_task_t *task, isc_event_t **eventp)
192 REQUIRE(DNS_DB_VALID(db));
194 return (isc_ondestroy_register(&db->ondest, task, eventp));
198 isc_boolean_t
199 dns_db_iscache(dns_db_t *db) {
202 * Does 'db' have cache semantics?
205 REQUIRE(DNS_DB_VALID(db));
207 if ((db->attributes & DNS_DBATTR_CACHE) != 0)
208 return (ISC_TRUE);
210 return (ISC_FALSE);
213 isc_boolean_t
214 dns_db_iszone(dns_db_t *db) {
217 * Does 'db' have zone semantics?
220 REQUIRE(DNS_DB_VALID(db));
222 if ((db->attributes & (DNS_DBATTR_CACHE|DNS_DBATTR_STUB)) == 0)
223 return (ISC_TRUE);
225 return (ISC_FALSE);
228 isc_boolean_t
229 dns_db_isstub(dns_db_t *db) {
232 * Does 'db' have stub semantics?
235 REQUIRE(DNS_DB_VALID(db));
237 if ((db->attributes & DNS_DBATTR_STUB) != 0)
238 return (ISC_TRUE);
240 return (ISC_FALSE);
243 isc_boolean_t
244 dns_db_isdnssec(dns_db_t *db) {
247 * Is 'db' secure or partially secure?
250 REQUIRE(DNS_DB_VALID(db));
251 REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0);
253 if (db->methods->isdnssec != NULL)
254 return ((db->methods->isdnssec)(db));
255 return ((db->methods->issecure)(db));
258 isc_boolean_t
259 dns_db_issecure(dns_db_t *db) {
262 * Is 'db' secure?
265 REQUIRE(DNS_DB_VALID(db));
266 REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0);
268 return ((db->methods->issecure)(db));
271 isc_boolean_t
272 dns_db_ispersistent(dns_db_t *db) {
275 * Is 'db' persistent?
278 REQUIRE(DNS_DB_VALID(db));
280 return ((db->methods->ispersistent)(db));
283 dns_name_t *
284 dns_db_origin(dns_db_t *db) {
286 * The origin of the database.
289 REQUIRE(DNS_DB_VALID(db));
291 return (&db->origin);
294 dns_rdataclass_t
295 dns_db_class(dns_db_t *db) {
297 * The class of the database.
300 REQUIRE(DNS_DB_VALID(db));
302 return (db->rdclass);
305 #ifdef BIND9
306 isc_result_t
307 dns_db_beginload(dns_db_t *db, dns_addrdatasetfunc_t *addp,
308 dns_dbload_t **dbloadp) {
310 * Begin loading 'db'.
313 REQUIRE(DNS_DB_VALID(db));
314 REQUIRE(addp != NULL && *addp == NULL);
315 REQUIRE(dbloadp != NULL && *dbloadp == NULL);
317 return ((db->methods->beginload)(db, addp, dbloadp));
320 isc_result_t
321 dns_db_endload(dns_db_t *db, dns_dbload_t **dbloadp) {
323 * Finish loading 'db'.
326 REQUIRE(DNS_DB_VALID(db));
327 REQUIRE(dbloadp != NULL && *dbloadp != NULL);
329 return ((db->methods->endload)(db, dbloadp));
332 isc_result_t
333 dns_db_load(dns_db_t *db, const char *filename) {
334 return (dns_db_load3(db, filename, dns_masterformat_text, 0));
337 isc_result_t
338 dns_db_load2(dns_db_t *db, const char *filename, dns_masterformat_t format) {
339 return (dns_db_load3(db, filename, format, 0));
342 isc_result_t
343 dns_db_load3(dns_db_t *db, const char *filename, dns_masterformat_t format,
344 unsigned int options) {
345 isc_result_t result, eresult;
346 dns_rdatacallbacks_t callbacks;
349 * Load master file 'filename' into 'db'.
352 REQUIRE(DNS_DB_VALID(db));
354 if ((db->attributes & DNS_DBATTR_CACHE) != 0)
355 options |= DNS_MASTER_AGETTL;
357 dns_rdatacallbacks_init(&callbacks);
359 result = dns_db_beginload(db, &callbacks.add, &callbacks.add_private);
360 if (result != ISC_R_SUCCESS)
361 return (result);
362 result = dns_master_loadfile2(filename, &db->origin, &db->origin,
363 db->rdclass, options,
364 &callbacks, db->mctx, format);
365 eresult = dns_db_endload(db, &callbacks.add_private);
367 * We always call dns_db_endload(), but we only want to return its
368 * result if dns_master_loadfile() succeeded. If dns_master_loadfile()
369 * failed, we want to return the result code it gave us.
371 if (eresult != ISC_R_SUCCESS &&
372 (result == ISC_R_SUCCESS || result == DNS_R_SEENINCLUDE))
373 result = eresult;
375 return (result);
378 isc_result_t
379 dns_db_dump(dns_db_t *db, dns_dbversion_t *version, const char *filename) {
380 return ((db->methods->dump)(db, version, filename,
381 dns_masterformat_text));
384 isc_result_t
385 dns_db_dump2(dns_db_t *db, dns_dbversion_t *version, const char *filename,
386 dns_masterformat_t masterformat) {
388 * Dump 'db' into master file 'filename' in the 'masterformat' format.
389 * XXXJT: is it okay to modify the interface to the existing "dump"
390 * method?
393 REQUIRE(DNS_DB_VALID(db));
395 return ((db->methods->dump)(db, version, filename, masterformat));
397 #endif /* BIND9 */
399 /***
400 *** Version Methods
401 ***/
403 void
404 dns_db_currentversion(dns_db_t *db, dns_dbversion_t **versionp) {
407 * Open the current version for reading.
410 REQUIRE(DNS_DB_VALID(db));
411 REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0);
412 REQUIRE(versionp != NULL && *versionp == NULL);
414 (db->methods->currentversion)(db, versionp);
417 isc_result_t
418 dns_db_newversion(dns_db_t *db, dns_dbversion_t **versionp) {
421 * Open a new version for reading and writing.
424 REQUIRE(DNS_DB_VALID(db));
425 REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0);
426 REQUIRE(versionp != NULL && *versionp == NULL);
428 return ((db->methods->newversion)(db, versionp));
431 void
432 dns_db_attachversion(dns_db_t *db, dns_dbversion_t *source,
433 dns_dbversion_t **targetp)
436 * Attach '*targetp' to 'source'.
439 REQUIRE(DNS_DB_VALID(db));
440 REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0);
441 REQUIRE(source != NULL);
442 REQUIRE(targetp != NULL && *targetp == NULL);
444 (db->methods->attachversion)(db, source, targetp);
446 ENSURE(*targetp != NULL);
449 void
450 dns_db_closeversion(dns_db_t *db, dns_dbversion_t **versionp,
451 isc_boolean_t commit)
455 * Close version '*versionp'.
458 REQUIRE(DNS_DB_VALID(db));
459 REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0);
460 REQUIRE(versionp != NULL && *versionp != NULL);
462 (db->methods->closeversion)(db, versionp, commit);
464 ENSURE(*versionp == NULL);
467 /***
468 *** Node Methods
469 ***/
471 isc_result_t
472 dns_db_findnode(dns_db_t *db, dns_name_t *name,
473 isc_boolean_t create, dns_dbnode_t **nodep)
477 * Find the node with name 'name'.
480 REQUIRE(DNS_DB_VALID(db));
481 REQUIRE(nodep != NULL && *nodep == NULL);
483 return ((db->methods->findnode)(db, name, create, nodep));
486 isc_result_t
487 dns_db_findnsec3node(dns_db_t *db, dns_name_t *name,
488 isc_boolean_t create, dns_dbnode_t **nodep)
492 * Find the node with name 'name'.
495 REQUIRE(DNS_DB_VALID(db));
496 REQUIRE(nodep != NULL && *nodep == NULL);
498 return ((db->methods->findnsec3node)(db, name, create, nodep));
501 isc_result_t
502 dns_db_find(dns_db_t *db, dns_name_t *name, dns_dbversion_t *version,
503 dns_rdatatype_t type, unsigned int options, isc_stdtime_t now,
504 dns_dbnode_t **nodep, dns_name_t *foundname,
505 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
509 * Find the best match for 'name' and 'type' in version 'version'
510 * of 'db'.
513 REQUIRE(DNS_DB_VALID(db));
514 REQUIRE(type != dns_rdatatype_rrsig);
515 REQUIRE(nodep == NULL || (nodep != NULL && *nodep == NULL));
516 REQUIRE(dns_name_hasbuffer(foundname));
517 REQUIRE(rdataset == NULL ||
518 (DNS_RDATASET_VALID(rdataset) &&
519 ! dns_rdataset_isassociated(rdataset)));
520 REQUIRE(sigrdataset == NULL ||
521 (DNS_RDATASET_VALID(sigrdataset) &&
522 ! dns_rdataset_isassociated(sigrdataset)));
524 return ((db->methods->find)(db, name, version, type, options, now,
525 nodep, foundname, rdataset, sigrdataset));
528 isc_result_t
529 dns_db_findzonecut(dns_db_t *db, dns_name_t *name,
530 unsigned int options, isc_stdtime_t now,
531 dns_dbnode_t **nodep, dns_name_t *foundname,
532 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
535 * Find the deepest known zonecut which encloses 'name' in 'db'.
538 REQUIRE(DNS_DB_VALID(db));
539 REQUIRE((db->attributes & DNS_DBATTR_CACHE) != 0);
540 REQUIRE(nodep == NULL || (nodep != NULL && *nodep == NULL));
541 REQUIRE(dns_name_hasbuffer(foundname));
542 REQUIRE(sigrdataset == NULL ||
543 (DNS_RDATASET_VALID(sigrdataset) &&
544 ! dns_rdataset_isassociated(sigrdataset)));
546 return ((db->methods->findzonecut)(db, name, options, now, nodep,
547 foundname, rdataset, sigrdataset));
550 void
551 dns_db_attachnode(dns_db_t *db, dns_dbnode_t *source, dns_dbnode_t **targetp) {
554 * Attach *targetp to source.
557 REQUIRE(DNS_DB_VALID(db));
558 REQUIRE(source != NULL);
559 REQUIRE(targetp != NULL && *targetp == NULL);
561 (db->methods->attachnode)(db, source, targetp);
564 void
565 dns_db_detachnode(dns_db_t *db, dns_dbnode_t **nodep) {
568 * Detach *nodep from its node.
571 REQUIRE(DNS_DB_VALID(db));
572 REQUIRE(nodep != NULL && *nodep != NULL);
574 (db->methods->detachnode)(db, nodep);
576 ENSURE(*nodep == NULL);
579 void
580 dns_db_transfernode(dns_db_t *db, dns_dbnode_t **sourcep,
581 dns_dbnode_t **targetp)
583 REQUIRE(DNS_DB_VALID(db));
584 REQUIRE(targetp != NULL && *targetp == NULL);
586 * This doesn't check the implementation magic. If we find that
587 * we need such checks in future then this will be done in the
588 * method.
590 REQUIRE(sourcep != NULL && *sourcep != NULL);
592 UNUSED(db);
594 if (db->methods->transfernode == NULL) {
595 *targetp = *sourcep;
596 *sourcep = NULL;
597 } else
598 (db->methods->transfernode)(db, sourcep, targetp);
600 ENSURE(*sourcep == NULL);
603 isc_result_t
604 dns_db_expirenode(dns_db_t *db, dns_dbnode_t *node, isc_stdtime_t now) {
607 * Mark as stale all records at 'node' which expire at or before 'now'.
610 REQUIRE(DNS_DB_VALID(db));
611 REQUIRE((db->attributes & DNS_DBATTR_CACHE) != 0);
612 REQUIRE(node != NULL);
614 return ((db->methods->expirenode)(db, node, now));
617 void
618 dns_db_printnode(dns_db_t *db, dns_dbnode_t *node, FILE *out) {
620 * Print a textual representation of the contents of the node to
621 * 'out'.
624 REQUIRE(DNS_DB_VALID(db));
625 REQUIRE(node != NULL);
627 (db->methods->printnode)(db, node, out);
630 /***
631 *** DB Iterator Creation
632 ***/
634 isc_result_t
635 dns_db_createiterator(dns_db_t *db, unsigned int flags,
636 dns_dbiterator_t **iteratorp)
639 * Create an iterator for version 'version' of 'db'.
642 REQUIRE(DNS_DB_VALID(db));
643 REQUIRE(iteratorp != NULL && *iteratorp == NULL);
645 return (db->methods->createiterator(db, flags, iteratorp));
648 /***
649 *** Rdataset Methods
650 ***/
652 isc_result_t
653 dns_db_findrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
654 dns_rdatatype_t type, dns_rdatatype_t covers,
655 isc_stdtime_t now, dns_rdataset_t *rdataset,
656 dns_rdataset_t *sigrdataset)
659 * Search for an rdataset of type 'type' at 'node' that are in version
660 * 'version' of 'db'. If found, make 'rdataset' refer to it.
663 REQUIRE(DNS_DB_VALID(db));
664 REQUIRE(node != NULL);
665 REQUIRE(DNS_RDATASET_VALID(rdataset));
666 REQUIRE(! dns_rdataset_isassociated(rdataset));
667 REQUIRE(covers == 0 || type == dns_rdatatype_rrsig);
668 REQUIRE(type != dns_rdatatype_any);
669 REQUIRE(sigrdataset == NULL ||
670 (DNS_RDATASET_VALID(sigrdataset) &&
671 ! dns_rdataset_isassociated(sigrdataset)));
673 return ((db->methods->findrdataset)(db, node, version, type, covers,
674 now, rdataset, sigrdataset));
677 isc_result_t
678 dns_db_allrdatasets(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
679 isc_stdtime_t now, dns_rdatasetiter_t **iteratorp)
682 * Make '*iteratorp' an rdataset iteratator for all rdatasets at
683 * 'node' in version 'version' of 'db'.
686 REQUIRE(DNS_DB_VALID(db));
687 REQUIRE(iteratorp != NULL && *iteratorp == NULL);
689 return ((db->methods->allrdatasets)(db, node, version, now,
690 iteratorp));
693 isc_result_t
694 dns_db_addrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
695 isc_stdtime_t now, dns_rdataset_t *rdataset,
696 unsigned int options, dns_rdataset_t *addedrdataset)
699 * Add 'rdataset' to 'node' in version 'version' of 'db'.
702 REQUIRE(DNS_DB_VALID(db));
703 REQUIRE(node != NULL);
704 REQUIRE(((db->attributes & DNS_DBATTR_CACHE) == 0 && version != NULL)||
705 ((db->attributes & DNS_DBATTR_CACHE) != 0 &&
706 version == NULL && (options & DNS_DBADD_MERGE) == 0));
707 REQUIRE((options & DNS_DBADD_EXACT) == 0 ||
708 (options & DNS_DBADD_MERGE) != 0);
709 REQUIRE(DNS_RDATASET_VALID(rdataset));
710 REQUIRE(dns_rdataset_isassociated(rdataset));
711 REQUIRE(rdataset->rdclass == db->rdclass);
712 REQUIRE(addedrdataset == NULL ||
713 (DNS_RDATASET_VALID(addedrdataset) &&
714 ! dns_rdataset_isassociated(addedrdataset)));
716 return ((db->methods->addrdataset)(db, node, version, now, rdataset,
717 options, addedrdataset));
720 isc_result_t
721 dns_db_subtractrdataset(dns_db_t *db, dns_dbnode_t *node,
722 dns_dbversion_t *version, dns_rdataset_t *rdataset,
723 unsigned int options, dns_rdataset_t *newrdataset)
726 * Remove any rdata in 'rdataset' from 'node' in version 'version' of
727 * 'db'.
730 REQUIRE(DNS_DB_VALID(db));
731 REQUIRE(node != NULL);
732 REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0 && version != NULL);
733 REQUIRE(DNS_RDATASET_VALID(rdataset));
734 REQUIRE(dns_rdataset_isassociated(rdataset));
735 REQUIRE(rdataset->rdclass == db->rdclass);
736 REQUIRE(newrdataset == NULL ||
737 (DNS_RDATASET_VALID(newrdataset) &&
738 ! dns_rdataset_isassociated(newrdataset)));
740 return ((db->methods->subtractrdataset)(db, node, version, rdataset,
741 options, newrdataset));
744 isc_result_t
745 dns_db_deleterdataset(dns_db_t *db, dns_dbnode_t *node,
746 dns_dbversion_t *version, dns_rdatatype_t type,
747 dns_rdatatype_t covers)
750 * Make it so that no rdataset of type 'type' exists at 'node' in
751 * version version 'version' of 'db'.
754 REQUIRE(DNS_DB_VALID(db));
755 REQUIRE(node != NULL);
756 REQUIRE(((db->attributes & DNS_DBATTR_CACHE) == 0 && version != NULL)||
757 ((db->attributes & DNS_DBATTR_CACHE) != 0 && version == NULL));
759 return ((db->methods->deleterdataset)(db, node, version,
760 type, covers));
763 void
764 dns_db_overmem(dns_db_t *db, isc_boolean_t overmem) {
766 REQUIRE(DNS_DB_VALID(db));
768 (db->methods->overmem)(db, overmem);
771 isc_result_t
772 dns_db_getsoaserial(dns_db_t *db, dns_dbversion_t *ver, isc_uint32_t *serialp)
774 isc_result_t result;
775 dns_dbnode_t *node = NULL;
776 dns_rdataset_t rdataset;
777 dns_rdata_t rdata = DNS_RDATA_INIT;
778 isc_buffer_t buffer;
780 REQUIRE(dns_db_iszone(db) || dns_db_isstub(db));
782 result = dns_db_findnode(db, dns_db_origin(db), ISC_FALSE, &node);
783 if (result != ISC_R_SUCCESS)
784 return (result);
786 dns_rdataset_init(&rdataset);
787 result = dns_db_findrdataset(db, node, ver, dns_rdatatype_soa, 0,
788 (isc_stdtime_t)0, &rdataset, NULL);
789 if (result != ISC_R_SUCCESS)
790 goto freenode;
792 result = dns_rdataset_first(&rdataset);
793 if (result != ISC_R_SUCCESS)
794 goto freerdataset;
795 dns_rdataset_current(&rdataset, &rdata);
796 result = dns_rdataset_next(&rdataset);
797 INSIST(result == ISC_R_NOMORE);
799 INSIST(rdata.length > 20);
800 isc_buffer_init(&buffer, rdata.data, rdata.length);
801 isc_buffer_add(&buffer, rdata.length);
802 isc_buffer_forward(&buffer, rdata.length - 20);
803 *serialp = isc_buffer_getuint32(&buffer);
805 result = ISC_R_SUCCESS;
807 freerdataset:
808 dns_rdataset_disassociate(&rdataset);
810 freenode:
811 dns_db_detachnode(db, &node);
812 return (result);
815 unsigned int
816 dns_db_nodecount(dns_db_t *db) {
817 REQUIRE(DNS_DB_VALID(db));
819 return ((db->methods->nodecount)(db));
822 void
823 dns_db_settask(dns_db_t *db, isc_task_t *task) {
824 REQUIRE(DNS_DB_VALID(db));
826 (db->methods->settask)(db, task);
829 isc_result_t
830 dns_db_register(const char *name, dns_dbcreatefunc_t create, void *driverarg,
831 isc_mem_t *mctx, dns_dbimplementation_t **dbimp)
833 dns_dbimplementation_t *imp;
835 REQUIRE(name != NULL);
836 REQUIRE(dbimp != NULL && *dbimp == NULL);
838 RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
840 RWLOCK(&implock, isc_rwlocktype_write);
841 imp = impfind(name);
842 if (imp != NULL) {
843 RWUNLOCK(&implock, isc_rwlocktype_write);
844 return (ISC_R_EXISTS);
847 imp = isc_mem_get(mctx, sizeof(dns_dbimplementation_t));
848 if (imp == NULL) {
849 RWUNLOCK(&implock, isc_rwlocktype_write);
850 return (ISC_R_NOMEMORY);
852 imp->name = name;
853 imp->create = create;
854 imp->mctx = NULL;
855 imp->driverarg = driverarg;
856 isc_mem_attach(mctx, &imp->mctx);
857 ISC_LINK_INIT(imp, link);
858 ISC_LIST_APPEND(implementations, imp, link);
859 RWUNLOCK(&implock, isc_rwlocktype_write);
861 *dbimp = imp;
863 return (ISC_R_SUCCESS);
866 void
867 dns_db_unregister(dns_dbimplementation_t **dbimp) {
868 dns_dbimplementation_t *imp;
869 isc_mem_t *mctx;
871 REQUIRE(dbimp != NULL && *dbimp != NULL);
873 RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
875 imp = *dbimp;
876 *dbimp = NULL;
877 RWLOCK(&implock, isc_rwlocktype_write);
878 ISC_LIST_UNLINK(implementations, imp, link);
879 mctx = imp->mctx;
880 isc_mem_put(mctx, imp, sizeof(dns_dbimplementation_t));
881 isc_mem_detach(&mctx);
882 RWUNLOCK(&implock, isc_rwlocktype_write);
883 ENSURE(*dbimp == NULL);
886 isc_result_t
887 dns_db_getoriginnode(dns_db_t *db, dns_dbnode_t **nodep) {
888 REQUIRE(DNS_DB_VALID(db));
889 REQUIRE(dns_db_iszone(db) == ISC_TRUE);
890 REQUIRE(nodep != NULL && *nodep == NULL);
892 if (db->methods->getoriginnode != NULL)
893 return ((db->methods->getoriginnode)(db, nodep));
895 return (ISC_R_NOTFOUND);
898 dns_stats_t *
899 dns_db_getrrsetstats(dns_db_t *db) {
900 REQUIRE(DNS_DB_VALID(db));
902 if (db->methods->getrrsetstats != NULL)
903 return ((db->methods->getrrsetstats)(db));
905 return (NULL);
908 isc_result_t
909 dns_db_getnsec3parameters(dns_db_t *db, dns_dbversion_t *version,
910 dns_hash_t *hash, isc_uint8_t *flags,
911 isc_uint16_t *iterations,
912 unsigned char *salt, size_t *salt_length)
914 REQUIRE(DNS_DB_VALID(db));
915 REQUIRE(dns_db_iszone(db) == ISC_TRUE);
917 if (db->methods->getnsec3parameters != NULL)
918 return ((db->methods->getnsec3parameters)(db, version, hash,
919 flags, iterations,
920 salt, salt_length));
922 return (ISC_R_NOTFOUND);
925 isc_result_t
926 dns_db_setsigningtime(dns_db_t *db, dns_rdataset_t *rdataset,
927 isc_stdtime_t resign)
929 if (db->methods->setsigningtime != NULL)
930 return ((db->methods->setsigningtime)(db, rdataset, resign));
931 return (ISC_R_NOTIMPLEMENTED);
934 isc_result_t
935 dns_db_getsigningtime(dns_db_t *db, dns_rdataset_t *rdataset, dns_name_t *name)
937 if (db->methods->getsigningtime != NULL)
938 return ((db->methods->getsigningtime)(db, rdataset, name));
939 return (ISC_R_NOTFOUND);
942 void
943 dns_db_resigned(dns_db_t *db, dns_rdataset_t *rdataset,
944 dns_dbversion_t *version)
946 if (db->methods->resigned != NULL)
947 (db->methods->resigned)(db, rdataset, version);