4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews/*
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * Copyright (C) 2007, 2016 Internet Systems Consortium, Inc. ("ISC")
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews *
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <stdlib.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <string.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <isc/buffer.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <isc/mem.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <isc/print.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <isc/result.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <dns/db.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <dns/dbiterator.h>
f94ec08c17d60ee519a4f46b6dbb3519989e5b9cMark Andrews#include <isc/entropy.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <dns/fixedname.h>
f94ec08c17d60ee519a4f46b6dbb3519989e5b9cMark Andrews#include <isc/hash.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <dns/name.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <dns/rdata.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <dns/rdataset.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <dns/rdatasetiter.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <dns/rdatatype.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <dns/result.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#include <sqlite3.h>
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#ifndef UNUSED
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#define UNUSED(x) (x) = (x)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#endif
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews/*
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews * Generate an SQLite table from a zone.
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewstypedef struct _dbinfo {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sqlite3 *db;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *filename;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *table;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews} dbinfo_t;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsdbinfo_t dbi = { NULL, NULL, NULL };
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsstatic void
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewscloseandexit(int status)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews{
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (dbi.db) {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sqlite3_close(dbi.db);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbi.db = NULL;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews }
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews exit(status);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews}
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsstatic void
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewscheck_result(isc_result_t result, const char *message)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews{
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (result != ISC_R_SUCCESS) {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews fprintf(stderr, "%s: %s\n", message,
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews isc_result_totext(result));
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews closeandexit(1);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews }
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews}
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsstatic isc_result_t
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsdb_connect(dbinfo_t *dbi)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews{
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (sqlite3_open(dbi->filename, &dbi->db) == SQLITE_OK) {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return (ISC_R_SUCCESS);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews } else {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews /* a connection is returned even if the open fails */
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sqlite3_close(dbi->db);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbi->db = NULL;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return (ISC_R_FAILURE);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews }
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews}
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsstatic int
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsadd_rdata_cb(void *parm, int cc, char **cv, char **cn)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews{
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews UNUSED(parm);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews UNUSED(cc);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews UNUSED(cv);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews UNUSED(cn);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews return 0;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews}
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsstatic void
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsaddrdata(dns_name_t *name, dns_ttl_t ttl, dns_rdata_t *rdata)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews{
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews unsigned char namearray[DNS_NAME_MAXTEXT + 1];
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews unsigned char typearray[20];
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews unsigned char dataarray[2048];
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews isc_buffer_t b;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews isc_result_t result;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *sql;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *errmsg = NULL;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews int res;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews isc_buffer_init(&b, namearray, sizeof(namearray) - 1);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews result = dns_name_totext(name, ISC_TRUE, &b);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews check_result(result, "dns_name_totext");
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews namearray[isc_buffer_usedlength(&b)] = 0;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews isc_buffer_init(&b, typearray, sizeof(typearray) - 1);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews result = dns_rdatatype_totext(rdata->type, &b);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews check_result(result, "dns_rdatatype_totext");
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews typearray[isc_buffer_usedlength(&b)] = 0;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews isc_buffer_init(&b, dataarray, sizeof(dataarray) - 1);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews result = dns_rdata_totext(rdata, NULL, &b);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews check_result(result, "dns_rdata_totext");
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dataarray[isc_buffer_usedlength(&b)] = 0;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sql = sqlite3_mprintf(
96a116178567d4eb22e57aba533073de9a2f9d84Evan Hunt "INSERT INTO %Q (NAME, TTL, RDTYPE, RDATA)"
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews " VALUES ('%q', %d, '%q', '%q') ",
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbi.table,
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews namearray, ttl, typearray, dataarray);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews printf("%s\n", sql);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews res = sqlite3_exec(dbi.db, sql, add_rdata_cb, NULL, &errmsg);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sqlite3_free(sql);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
58f1ac8dadf2c1f215343a0b2d1df2df954c4b19Mark Andrews if (res != SQLITE_OK) {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews fprintf(stderr, "INSERT failed: %s\n", errmsg);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews closeandexit(1);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews }
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews}
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsint
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrewsmain(int argc, char *argv[])
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews{
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *sql;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews int res;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *errmsg = NULL;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews char *porigin, *zonefile;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_fixedname_t forigin, fname;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_name_t *origin, *name;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_db_t *db = NULL;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_dbiterator_t *dbiter;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_dbnode_t *node;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_rdatasetiter_t *rdsiter;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_rdataset_t rdataset;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_rdata_t rdata = DNS_RDATA_INIT;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews isc_mem_t *mctx = NULL;
f94ec08c17d60ee519a4f46b6dbb3519989e5b9cMark Andrews isc_entropy_t *ectx = NULL;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews isc_buffer_t b;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews isc_result_t result;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (argc != 5) {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews printf("usage: %s <zone> <zonefile> <dbfile> <dbtable>\n", argv[0]);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews exit(1);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews }
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews porigin = argv[1];
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews zonefile = argv[2];
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbi.filename = argv[3];
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbi.table = argv[4];
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_result_register();
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews result = isc_mem_create(0, 0, &mctx);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews check_result(result, "isc_mem_create");
f94ec08c17d60ee519a4f46b6dbb3519989e5b9cMark Andrews result = isc_entropy_create(mctx, &ectx);
f94ec08c17d60ee519a4f46b6dbb3519989e5b9cMark Andrews check_result(result, "isc_entropy_create");
f94ec08c17d60ee519a4f46b6dbb3519989e5b9cMark Andrews result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
f94ec08c17d60ee519a4f46b6dbb3519989e5b9cMark Andrews check_result(result, "isc_hash_create");
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews isc_buffer_init(&b, porigin, strlen(porigin));
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews isc_buffer_add(&b, strlen(porigin));
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_fixedname_init(&forigin);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews origin = dns_fixedname_name(&forigin);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 result = dns_name_fromtext(origin, &b, dns_rootname, 0, NULL);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews check_result(result, "dns_name_fromtext");
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews db = NULL;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews result = dns_db_create(mctx, "rbt", origin, dns_dbtype_zone,
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_rdataclass_in, 0, NULL, &db);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews check_result(result, "dns_db_create");
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews result = dns_db_load(db, zonefile);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (result == DNS_R_SEENINCLUDE)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews result = ISC_R_SUCCESS;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews check_result(result, "dns_db_load");
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews printf("Connecting to '%s'\n", dbi.filename);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if ((result = db_connect(&dbi)) != ISC_R_SUCCESS) {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews fprintf(stderr, "Connection to database '%s' failed\n",
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbi.filename);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews closeandexit(1);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews }
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
96a116178567d4eb22e57aba533073de9a2f9d84Evan Hunt sql = sqlite3_mprintf("DROP TABLE %Q ", dbi.table);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews printf("%s\n", sql);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews res = sqlite3_exec(dbi.db, sql, NULL, NULL, &errmsg);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sqlite3_free(sql);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#if 0
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (res != SQLITE_OK) {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews fprintf(stderr, "DROP TABLE %s failed: %s\n",
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbi.table, errmsg);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews }
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#endif
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#if 0
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sql = sqlite3_mprintf(sql, "BEGIN TRANSACTION");
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews printf("%s\n", sql);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews res = sqlite3_exec(dbi.db, sql, NULL, NULL, &errmsg);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sqlite3_free(sql);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (res != SQLITE_OK) {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews fprintf(stderr, "BEGIN TRANSACTION failed: %s\n", errmsg);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews closeandexit(1);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews }
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#endif
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sql = sqlite3_mprintf(
96a116178567d4eb22e57aba533073de9a2f9d84Evan Hunt "CREATE TABLE %Q "
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews "(NAME TEXT, TTL INTEGER, RDTYPE TEXT, RDATA TEXT) ",
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbi.table);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews printf("%s\n", sql);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews res = sqlite3_exec(dbi.db, sql, NULL, NULL, &errmsg);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sqlite3_free(sql);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (res != SQLITE_OK) {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews fprintf(stderr, "CREATE TABLE %s failed: %s\n",
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbi.table, errmsg);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews closeandexit(1);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews }
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dbiter = NULL;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews result = dns_db_createiterator(db, 0, &dbiter);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews check_result(result, "dns_db_createiterator()");
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews result = dns_dbiterator_first(dbiter);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews check_result(result, "dns_dbiterator_first");
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_fixedname_init(&fname);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews name = dns_fixedname_name(&fname);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_rdataset_init(&rdataset);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_rdata_init(&rdata);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews while (result == ISC_R_SUCCESS) {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews node = NULL;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews result = dns_dbiterator_current(dbiter, &node, name);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (result == ISC_R_NOMORE)
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews break;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews check_result(result, "dns_dbiterator_current");
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews rdsiter = NULL;
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews result = dns_db_allrdatasets(db, node, NULL, 0, &rdsiter);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews check_result(result, "dns_db_allrdatasets");
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews result = dns_rdatasetiter_first(rdsiter);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews while (result == ISC_R_SUCCESS) {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_rdatasetiter_current(rdsiter, &rdataset);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews result = dns_rdataset_first(&rdataset);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews check_result(result, "dns_rdataset_first");
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews while (result == ISC_R_SUCCESS) {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_rdataset_current(&rdataset, &rdata);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews addrdata(name, rdataset.ttl, &rdata);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_rdata_reset(&rdata);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews result = dns_rdataset_next(&rdataset);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews }
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_rdataset_disassociate(&rdataset);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews result = dns_rdatasetiter_next(rdsiter);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews }
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_rdatasetiter_destroy(&rdsiter);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_db_detachnode(db, &node);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews result = dns_dbiterator_next(dbiter);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews }
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#if 0
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sql = sqlite3_mprintf(sql, "COMMIT TRANSACTION ");
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews printf("%s\n", sql);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews res = sqlite3_exec(dbi.db, sql, NULL, NULL, &errmsg);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews sqlite3_free(sql);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews if (res != SQLITE_OK) {
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews fprintf(stderr, "COMMIT TRANSACTION failed: %s\n", errmsg);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews closeandexit(1);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews }
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews#endif
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_dbiterator_destroy(&dbiter);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews dns_db_detach(&db);
f94ec08c17d60ee519a4f46b6dbb3519989e5b9cMark Andrews isc_hash_destroy();
f94ec08c17d60ee519a4f46b6dbb3519989e5b9cMark Andrews isc_entropy_detach(&ectx);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews isc_mem_destroy(&mctx);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews closeandexit(0);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews exit(0);
4259095c8058beb9b475f1884dbeda375979e6f6Mark Andrews}