db.c revision 7f79448198139145cebc2540188b16b1861b98c5
ddad35552931651426ad70912d29d9ab7d2a8d80Automatic Updater * Database API implementation. The interface is defined in lib/dns/db.h.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * dns_db_*() calls on database instances backed by this driver use
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * struct sampledb_methods to find appropriate function implementation.
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * This example re-uses RBT DB implementation from original BIND and blindly
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * proxies most of dns_db_*() calls to this underlying RBT DB.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * See struct sampledb below.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * Copyright (C) 2009-2015 Red Hat ; see COPYRIGHT for license
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister#define SAMPLEDB_MAGIC ISC_MAGIC('S', 'M', 'D', 'B')
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister ((sampledb) != NULL && (sampledb)->common.impmagic == SAMPLEDB_MAGIC)
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister * Internal RBT database implementation provided by BIND.
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington * Most dns_db_* calls (find(), createiterator(), etc.)
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews * are blindly forwarded to this RBT DB.
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson * Get full DNS name from the node.
b3e77535185043f089b346166440402d092030c3David Lawrence * The code silently expects that "node" came from RBTDB and thus
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * assumption dns_dbnode_t (from RBTDB) == dns_rbtnode_t is correct.
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * This should work as long as we use only RBTDB and nothing else.
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafssonsample_name_fromnode(dns_dbnode_t *node, dns_name_t *name) {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_rbtnode_t *rbtnode = (dns_rbtnode_t *) node;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence return (dns_rbt_fullnamefromnode(rbtnode, name));
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_refcount_increment(&sampledb->refs, NULL);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_name_free(&sampledb->common.origin, sampledb->common.mctx);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_mem_putanddetach(&sampledb->common.mctx, sampledb, sizeof(*sampledb));
b3e77535185043f089b346166440402d092030c3David Lawrence unsigned int refs;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_refcount_decrement(&sampledb->refs, &refs);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * This method should never be called, because DB is "persistent".
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * See ispersistent() function. It means that database do not need to be
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * loaded in the usual sense.
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerbeginload(dns_db_t *db, dns_rdatacallbacks_t *callbacks) {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister fatal_error("current implementation should never call beginload()");
b3e77535185043f089b346166440402d092030c3David Lawrence /* Not reached */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * This method should never be called, because DB is "persistent".
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister * See ispersistent() function. It means that database do not need to be
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * loaded in the usual sense.
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerendload(dns_db_t *db, dns_rdatacallbacks_t *callbacks) {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister fatal_error("current implementation should never call endload()");
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister /* Not reached */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerserialize(dns_db_t *db, dns_dbversion_t *version, FILE *file) {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister return (dns_db_serialize(sampledb->rbtdb, version, file));
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrewsdump(dns_db_t *db, dns_dbversion_t *version, const char *filename,
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews fatal_error("current implementation should never call dump()");
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews /* Not reached */
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristercurrentversion(dns_db_t *db, dns_dbversion_t **versionp) {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_db_currentversion(sampledb->rbtdb, versionp);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristernewversion(dns_db_t *db, dns_dbversion_t **versionp) {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister return (dns_db_newversion(sampledb->rbtdb, versionp));
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerattachversion(dns_db_t *db, dns_dbversion_t *source,
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_db_attachversion(sampledb->rbtdb, source, targetp);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristercloseversion(dns_db_t *db, dns_dbversion_t **versionp, isc_boolean_t commit) {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_db_closeversion(sampledb->rbtdb, versionp, commit);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerfindnode(dns_db_t *db, dns_name_t *name, isc_boolean_t create,
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews return (dns_db_findnode(sampledb->rbtdb, name, create, nodep));
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrewsfind(dns_db_t *db, dns_name_t *name, dns_dbversion_t *version,
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews dns_rdatatype_t type, unsigned int options, isc_stdtime_t now,
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews dns_dbnode_t **nodep, dns_name_t *foundname, dns_rdataset_t *rdataset,
4844ed026a9b5a91044e76399cee80a6514cbf0dMark Andrews return (dns_db_find(sampledb->rbtdb, name, version, type,
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencefindzonecut(dns_db_t *db, dns_name_t *name, unsigned int options,
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_stdtime_t now, dns_dbnode_t **nodep, dns_name_t *foundname,
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson sampledb_t *sampledb = (sampledb_t *) db;
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister return (dns_db_findzonecut(sampledb->rbtdb, name, options,
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Bristerattachnode(dns_db_t *db, dns_dbnode_t *source, dns_dbnode_t **targetp) {
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews dns_db_attachnode(sampledb->rbtdb, source, targetp);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerdetachnode(dns_db_t *db, dns_dbnode_t **targetp) {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerexpirenode(dns_db_t *db, dns_dbnode_t *node, isc_stdtime_t now) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence return (dns_db_expirenode(sampledb->rbtdb, node, now));
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerprintnode(dns_db_t *db, dns_dbnode_t *node, FILE *out) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_db_printnode(sampledb->rbtdb, node, out);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristercreateiterator(dns_db_t *db, unsigned int options,
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister return (dns_db_createiterator(sampledb->rbtdb, options, iteratorp));
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerfindrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson dns_rdatatype_t type, dns_rdatatype_t covers, isc_stdtime_t now,
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence return (dns_db_findrdataset(sampledb->rbtdb, node, version, type,
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerallrdatasets(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister isc_stdtime_t now, dns_rdatasetiter_t **iteratorp)
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister return (dns_db_allrdatasets(sampledb->rbtdb, node, version,
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉addrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 isc_stdtime_t now, dns_rdataset_t *rdataset, unsigned int options,
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister CHECK(dns_db_addrdataset(sampledb->rbtdb, node, version, now,
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister CHECK(sample_name_fromnode(node, dns_fixedname_name(&name)));
b3e77535185043f089b346166440402d092030c3David Lawrence CHECK(syncptrs(sampledb->inst, dns_fixedname_name(&name),
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencesubtractrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
b3e77535185043f089b346166440402d092030c3David Lawrence dns_rdataset_t *rdataset, unsigned int options,
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister result = dns_db_subtractrdataset(sampledb->rbtdb, node, version,
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister if (result != ISC_R_SUCCESS && result != DNS_R_NXRRSET)
b3e77535185043f089b346166440402d092030c3David Lawrence CHECK(sample_name_fromnode(node, dns_fixedname_name(&name)));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence CHECK(syncptrs(sampledb->inst, dns_fixedname_name(&name),
b3e77535185043f089b346166440402d092030c3David Lawrence * deleterdataset() function is not used during DNS update processing so syncptr
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Brister * implementation is left as an exercise to the reader.
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Bristerdeleterdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence return (dns_db_deleterdataset(sampledb->rbtdb, node, version,
5475a2d296215b7a93bd89804dc33c36a6de7cb5James Bristerstatic unsigned int
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington * The database does not need to be loaded from disk or written to disk.
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellington * Always return ISC_TRUE.
d25dd5b0567f67ecf40b7ed1cb20e0dce7b41c49Brian Wellingtonovermem(dns_db_t *db, isc_boolean_t overmem) {
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrewsgetoriginnode(dns_db_t *db, dns_dbnode_t **nodep) {
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews return (dns_db_getoriginnode(sampledb->rbtdb, nodep));
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrewstransfernode(dns_db_t *db, dns_dbnode_t **sourcep, dns_dbnode_t **targetp) {
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews dns_db_transfernode(sampledb->rbtdb, sourcep, targetp);
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrewsgetnsec3parameters(dns_db_t *db, dns_dbversion_t *version,
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence return (dns_db_getnsec3parameters(sampledb->rbtdb, version,
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerfindnsec3node(dns_db_t *db, dns_name_t *name, isc_boolean_t create,
b3e77535185043f089b346166440402d092030c3David Lawrence return (dns_db_findnsec3node(sampledb->rbtdb, name, create, nodep));
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristersetsigningtime(dns_db_t *db, dns_rdataset_t *rdataset, isc_stdtime_t resign) {
b3e77535185043f089b346166440402d092030c3David Lawrence return (dns_db_setsigningtime(sampledb->rbtdb, rdataset, resign));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencegetsigningtime(dns_db_t *db, dns_rdataset_t *rdataset, dns_name_t *name) {
b3e77535185043f089b346166440402d092030c3David Lawrence return (dns_db_getsigningtime(sampledb->rbtdb, rdataset, name));
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerresigned(dns_db_t *db, dns_rdataset_t *rdataset, dns_dbversion_t *version) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_db_resigned(sampledb->rbtdb, rdataset, version);
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister return (dns_db_getrrsetstats(sampledb->rbtdb));
2cde028c51055c9fd4837337116cd4fdfe8ff623James Bristerrpz_attach(dns_db_t *db, dns_rpz_zones_t *rpzs, dns_rpz_num_t rpz_num) {
2cde028c51055c9fd4837337116cd4fdfe8ff623James Brister dns_db_rpz_attach(sampledb->rbtdb, rpzs, rpz_num);
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington return (dns_db_rpz_ready(sampledb->rbtdb));
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington isc_boolean_t create, dns_clientinfomethods_t *methods,
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington dns_clientinfo_t *clientinfo, dns_dbnode_t **nodep)
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington return (dns_db_findnodeext(sampledb->rbtdb, name, create,
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellingtonfindext(dns_db_t *db, dns_name_t *name, dns_dbversion_t *version,
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington dns_rdatatype_t type, unsigned int options, isc_stdtime_t now,
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington dns_dbnode_t **nodep, dns_name_t *foundname,
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington dns_clientinfomethods_t *methods, dns_clientinfo_t *clientinfo,
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
590f840d3484114576d9f8a7f7d73fbe31228888Brian Wellington return (dns_db_findext(sampledb->rbtdb, name, version, type,
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉setcachestats(dns_db_t *db, isc_stats_t *stats) {
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 return (dns_db_setcachestats(sampledb->rbtdb, stats));
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 sampledb_t *sampledb = (sampledb_t *) db;
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 return (dns_db_hashsize(sampledb->rbtdb));
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 * DB interface definition. Database driver uses this structure to
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉 * determine which implementation of dns_db_*() function to call.
600cbd1fcea3c9cc9706dc1ff8fc0d0034ebdeacTatuya JINMEI 神明達哉static dns_dbmethods_t sampledb_methods = {
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews/* Auxiliary driver functions. */
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews * Auxiliary functions add_*() create minimal database which can be loaded.
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews * This is necessary because this driver create empty 'fake' zone which
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews * is not loaded from disk so there is no way for user to supply SOA, NS and A
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews * Following functions were copied from BIND 9.10.2rc1 named/server.c,
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews * credit goes to ISC.
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrewsadd_soa(dns_db_t *db, dns_dbversion_t *version, dns_name_t *name,
7d4a465de03b26bf9f5ef131d03253b8f6afc169Mark Andrews CHECK(dns_soa_buildrdata(origin, contact, dns_db_class(db),
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews ISC_LIST_APPEND(rdatalist.rdata, &rdata, link);
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews CHECK(dns_rdatalist_tordataset(&rdatalist, &rdataset));
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews CHECK(dns_db_findnode(db, name, ISC_TRUE, &node));
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrews CHECK(dns_db_addrdataset(db, node, version, 0, &rdataset, 0, NULL));
1c153afce556ff3c687986fb7c4a0b0a7f5e7cd8Mark Andrewsadd_ns(dns_db_t *db, dns_dbversion_t *version, dns_name_t *name,
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews CHECK(dns_rdata_fromstruct(&rdata, dns_db_class(db), dns_rdatatype_ns,
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews ISC_LIST_APPEND(rdatalist.rdata, &rdata, link);
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews CHECK(dns_rdatalist_tordataset(&rdatalist, &rdataset));
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews CHECK(dns_db_findnode(db, name, ISC_TRUE, &node));
08c90261660649ca7d92065f6f13a61ec5a9a86dMark Andrews CHECK(dns_db_addrdataset(db, node, version, 0, &rdataset, 0, NULL));
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrewsadd_a(dns_db_t *db, dns_dbversion_t *version, dns_name_t *name,
isc_buffer_t b;
return (result);
return (ISC_R_SUCCESS);
sizeof(*sampledb));
return (result);