zone2bdb.c revision 6098d364b690cb9dabf96e9664c4689c8559bd2e
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews/*
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * Copyright (C) 2002 Nuno M. Rodrigues.
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Permission to use, copy, modify, and distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * purpose with or without fee is hereby granted, provided that the above
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * copyright notice and this permission notice appear in all copies.
12178c86525332bb0ab66155feb61fbf32eca6acEvan Hunt *
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND NUNO M. RODRIGUES
44aae046c38e796e581110b7ecdf4478167d684dBob Halley * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
44aae046c38e796e581110b7ecdf4478167d684dBob Halley * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
44aae046c38e796e581110b7ecdf4478167d684dBob Halley * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
44aae046c38e796e581110b7ecdf4478167d684dBob Halley * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
c2bc56dc65b4b103a5600565680eb5f33fa4c90bMark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
634c61d2ddf514c3b1f5c8d49c2d2375df20a8b6Andreas Gustafsson * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
634c61d2ddf514c3b1f5c8d49c2d2375df20a8b6Andreas Gustafsson * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
12178c86525332bb0ab66155feb61fbf32eca6acEvan Hunt */
44aae046c38e796e581110b7ecdf4478167d684dBob Halley
44aae046c38e796e581110b7ecdf4478167d684dBob Halley/* $Id: zone2bdb.c,v 1.2 2008/09/24 02:46:21 marka Exp $ */
b6d52ee5bea1b9d9074698e693b49ce96edff47bMark Andrews
b6d52ee5bea1b9d9074698e693b49ce96edff47bMark Andrews#include <stdio.h>
b6d52ee5bea1b9d9074698e693b49ce96edff47bMark Andrews
b6d52ee5bea1b9d9074698e693b49ce96edff47bMark Andrews#include <isc/mem.h>
b6d52ee5bea1b9d9074698e693b49ce96edff47bMark Andrews#include <isc/result.h>
b6d52ee5bea1b9d9074698e693b49ce96edff47bMark Andrews#include <isc/types.h>
fd35453765c6494d077edca44f4d998af5fd4cfaDavid Lawrence#include <isc/util.h>
44aae046c38e796e581110b7ecdf4478167d684dBob Halley
56b40c0185bcbd4a04422acc1211b5e2d2ba8a3eBob Halley#include <dns/db.h>
56b40c0185bcbd4a04422acc1211b5e2d2ba8a3eBob Halley#include <dns/dbiterator.h>
44aae046c38e796e581110b7ecdf4478167d684dBob Halley#include <dns/fixedname.h>
44aae046c38e796e581110b7ecdf4478167d684dBob Halley#include <dns/name.h>
44aae046c38e796e581110b7ecdf4478167d684dBob Halley#include <dns/rdata.h>
44aae046c38e796e581110b7ecdf4478167d684dBob Halley#include <dns/rdataset.h>
44aae046c38e796e581110b7ecdf4478167d684dBob Halley#include <dns/rdatasetiter.h>
193738b819e3c699f9edd18864a6810fcfcec855Andreas Gustafsson#include <dns/rdatatype.h>
fd35453765c6494d077edca44f4d998af5fd4cfaDavid Lawrence#include <dns/ttl.h>
fd35453765c6494d077edca44f4d998af5fd4cfaDavid Lawrence#include <dns/types.h>
44aae046c38e796e581110b7ecdf4478167d684dBob Halley
634c61d2ddf514c3b1f5c8d49c2d2375df20a8b6Andreas Gustafsson#include <db.h>
74cb99072c4b0ebd2ccafcfa284288fa760f7a1aMark Andrews
9f7d51ee3290e2a064d71016a6bd555b47134a7cMark Andrews#define MAX_RDATATEXT 63 + 4 + 65535 + 2 /* ttl + type + rdata + sep */
6e1141e6e83b3907b8b187d97932f30fa82470efMark Andrews
634c61d2ddf514c3b1f5c8d49c2d2375df20a8b6Andreas Gustafsson/*
fd35453765c6494d077edca44f4d998af5fd4cfaDavid Lawrence * Returns a valid 'DB' handle.
44aae046c38e796e581110b7ecdf4478167d684dBob Halley *
44aae046c38e796e581110b7ecdf4478167d684dBob Halley * Requires:
44aae046c38e796e581110b7ecdf4478167d684dBob Halley * 'file' is a valid non-existant path.
fd35453765c6494d077edca44f4d998af5fd4cfaDavid Lawrence */
DB *
bdb_init(const char *file)
{
DB *db;
REQUIRE(db_create(&db, NULL, 0) == 0);
REQUIRE(db->set_flags(db, DB_DUP) == 0);
REQUIRE(db->open(db, file, NULL, DB_HASH, DB_CREATE | DB_EXCL, 0) == 0);
return db;
}
/*
* Puts resource record data on 'db'.
*/
isc_result_t
bdb_putrdata(DB *db, dns_name_t *name, dns_ttl_t ttl, dns_rdata_t *rdata)
{
static DBT key, data;
isc_buffer_t keybuf, databuf;
char nametext[DNS_NAME_MAXTEXT];
char rdatatext[MAX_RDATATEXT];
isc_buffer_init(&keybuf, nametext, DNS_NAME_MAXTEXT);
dns_name_totext(name, ISC_TRUE, &keybuf);
key.data = isc_buffer_base(&keybuf);
key.size = isc_buffer_usedlength(&keybuf);
isc_buffer_init(&databuf, rdatatext, MAX_RDATATEXT);
dns_ttl_totext(ttl, ISC_FALSE, &databuf);
*(char *)isc_buffer_used(&databuf) = ' ';
isc_buffer_add(&databuf, 1);
dns_rdatatype_totext(rdata->type, &databuf); /* XXX private data */
*(char *)isc_buffer_used(&databuf) = ' ';
isc_buffer_add(&databuf, 1);
dns_rdata_totext(rdata, NULL, &databuf);
data.data = isc_buffer_base(&databuf);
data.size = isc_buffer_usedlength(&databuf);
REQUIRE(db->put(db, NULL, &key, &data, 0) == 0);
return ISC_R_SUCCESS;
}
isc_result_t
bdb_destroy(DB *db)
{
return (db->close(db, 0) == 0) ? ISC_R_SUCCESS : ISC_R_FAILURE;
}
void
usage(const char *prog)
{
fprintf(stderr, "Usage: %s <origin> <zonefile> <db>\n", prog);
exit(1);
}
int
main(int argc, char *argv[])
{
isc_mem_t *mctx = NULL;
isc_buffer_t b;
int n;
dns_fixedname_t origin, name;
dns_db_t *db = NULL;
dns_dbiterator_t *dbiter = NULL;
isc_result_t res;
dns_dbnode_t *node = NULL;
dns_rdataset_t rdataset;
dns_rdatasetiter_t *rdatasetiter = NULL;
dns_rdata_t rdata;
DB *bdb;
if (argc != 4) usage(*argv);
REQUIRE(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
n = strlen(argv[1]);
isc_buffer_init(&b, argv[1], n);
isc_buffer_add(&b, n);
dns_fixedname_init(&origin);
REQUIRE(dns_name_fromtext(dns_fixedname_name(&origin), &b, dns_rootname,
ISC_FALSE, NULL) == ISC_R_SUCCESS);
REQUIRE(dns_db_create(mctx, "rbt", dns_fixedname_name(&origin),
dns_dbtype_zone, dns_rdataclass_in, 0, NULL,
&db) == ISC_R_SUCCESS);
REQUIRE(dns_db_load(db, argv[2]) == ISC_R_SUCCESS);
REQUIRE(dns_db_createiterator(db, 0, &dbiter) == ISC_R_SUCCESS);
dns_rdataset_init(&rdataset);
dns_rdata_init(&rdata);
dns_fixedname_init(&name);
bdb = bdb_init(argv[3]);
for (res = dns_dbiterator_first(dbiter); res == ISC_R_SUCCESS;
res = dns_dbiterator_next(dbiter)) {
dns_dbiterator_current(dbiter, &node, dns_fixedname_name(&name));
REQUIRE(dns_db_allrdatasets(db, node, NULL, 0, &rdatasetiter)
== ISC_R_SUCCESS);
for (res = dns_rdatasetiter_first(rdatasetiter);
res == ISC_R_SUCCESS;
res = dns_rdatasetiter_next(rdatasetiter)) {
dns_rdatasetiter_current(rdatasetiter, &rdataset);
res = dns_rdataset_first(&rdataset);
while (res == ISC_R_SUCCESS) {
dns_rdataset_current(&rdataset, &rdata);
REQUIRE(bdb_putrdata(bdb,
dns_fixedname_name(&name),
rdataset.ttl, &rdata)
== ISC_R_SUCCESS);
dns_rdata_reset(&rdata);
res = dns_rdataset_next(&rdataset);
}
dns_rdataset_disassociate(&rdataset);
}
dns_rdatasetiter_destroy(&rdatasetiter);
dns_db_detachnode(db, &node);
}
dns_dbiterator_destroy(&dbiter);
REQUIRE(bdb_destroy(bdb) == ISC_R_SUCCESS);
return 0;
}