journal.c revision 875e88e734b310976b178dc26a74d86fbebc935c
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews/*
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington * Copyright (C) 1999 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews * Permission to use, copy, modify, and distribute this software for any
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews * purpose with or without fee is hereby granted, provided that the above
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
15a44745412679c30a6d022733925af70a38b715David Lawrence * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15a44745412679c30a6d022733925af70a38b715David Lawrence * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
15a44745412679c30a6d022733925af70a38b715David Lawrence * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
15a44745412679c30a6d022733925af70a38b715David Lawrence * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15a44745412679c30a6d022733925af70a38b715David Lawrence * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15a44745412679c30a6d022733925af70a38b715David Lawrence * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15a44745412679c30a6d022733925af70a38b715David Lawrence * SOFTWARE.
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
f1b68725503ff3e46001eee5a1751e29a43a09d1Andreas Gustafsson#include <config.h>
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
6b7257f756eb0530cdf54df9a7fab8d51a5001c3David Lawrence#include <stdio.h>
6b7257f756eb0530cdf54df9a7fab8d51a5001c3David Lawrence#include <stdlib.h>
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#include <unistd.h>
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#include <fcntl.h>
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#include <errno.h>
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#include <string.h>
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff
35c842e05dc6382ce1d9161a658d3ff4b2c3d4c9Bob Halley#include <sys/types.h>
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff#include <isc/assertions.h>
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff#include <isc/error.h>
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff#include <isc/mem.h>
d0783e645b149fcea7e7f22cd43f87b5d188b055Mark Andrews#include <isc/net.h>
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#include <isc/result.h>
d0783e645b149fcea7e7f22cd43f87b5d188b055Mark Andrews#include <isc/buffer.h>
d0783e645b149fcea7e7f22cd43f87b5d188b055Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#include <dns/db.h>
d0783e645b149fcea7e7f22cd43f87b5d188b055Mark Andrews#include <dns/dbiterator.h>
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#include <dns/fixedname.h>
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence#include <dns/journal.h>
5236050c0f819b727dfc4850133f5194f346f76fDanny Mayer#include <dns/log.h>
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence#include <dns/name.h>
35c842e05dc6382ce1d9161a658d3ff4b2c3d4c9Bob Halley#include <dns/rdata.h>
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#include <dns/rdatalist.h>
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#include <dns/rdataset.h>
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence#include <dns/rdatasetiter.h>
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence#include <dns/result.h>
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#include <dns/types.h>
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * When true, accept IXFR difference sequences where the
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * SOA serial number does not change (BIND 8 sends such
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * sequences).
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsstatic isc_boolean_t bind8_compat = ISC_TRUE; /* XXX config */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/**************************************************************************/
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Miscellaneous utilities.
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence */
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#define JOURNAL_COMMON_LOGARGS \
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_JOURNAL
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#define JOURNAL_DEBUG_LOGARGS(n) \
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews JOURNAL_COMMON_LOGARGS, ISC_LOG_DEBUG(n)
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence#define FAIL(code) do { result = (code); goto failure; } while (0)
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence#define CHECK(op) \
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence do { result = (op); \
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence if (result != ISC_R_SUCCESS) goto failure; \
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence } while (0)
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence/* XXX should be macros */
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrencestatic isc_uint32_t
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsdecode_uint32(unsigned char *p) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return ((p[0] << 24) +
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews (p[1] << 16) +
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews (p[2] << 8) +
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews (p[3] << 0));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsstatic void
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsencode_uint32(isc_uint32_t val, unsigned char *p) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews p[0] = (isc_uint8_t)(val >> 24);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews p[1] = (isc_uint8_t)(val >> 16);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews p[2] = (isc_uint8_t)(val >> 8);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews p[3] = (isc_uint8_t)(val >> 0);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsstatic dns_rdatatype_t
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsrdata_covers(dns_rdata_t *rdata) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (rdata->type == dns_rdatatype_sig ?
af9cfbc64363b61aa5903dd916e9fbc152084d4cMark Andrews dns_rdata_covers(rdata) : 0);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_uint32_t
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsdns_soa_getserial(dns_rdata_t *rdata)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews{
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(rdata->type == dns_rdatatype_soa);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /*
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews * Locate the serial number within the SOA RDATA based
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * on its position relative to the end of the data.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * (it starts 20 bytes from the end). This is a bit of
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * a kludge, but the alternative approach of using
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * dns_rdata_tostruct() and dns_rdata_fromstruct()
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * would involve a lot of unnecessary work (like
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * building domain names and allocating temporary memory)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * when all we really want to do is to change 32 bits of
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * fixed-sized data. Besides, fromstruct_soa() is not
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * implemented yet.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(rdata->length > 20);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (decode_uint32(rdata->data + rdata->length - 20));
35c842e05dc6382ce1d9161a658d3ff4b2c3d4c9Bob Halley}
35c842e05dc6382ce1d9161a658d3ff4b2c3d4c9Bob Halley
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsvoid
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrencedns_soa_setserial(isc_uint32_t val, dns_rdata_t *rdata)
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence{
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence INSIST(rdata->type == dns_rdatatype_soa);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(rdata->length > 20);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews encode_uint32(val, rdata->data + rdata->length - 20);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsdns_result_t
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsdns_db_getsoaserial(dns_db_t *db, dns_dbversion_t *ver, isc_uint32_t *serialp)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews{
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_result_t result;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_dbnode_t *node = NULL;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rdataset_t rdataset;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rdata_t rdata;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence REQUIRE(dns_db_iszone(db));
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence result = dns_db_findnode(db, dns_db_origin(db), ISC_FALSE, &node);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (result != DNS_R_SUCCESS)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (result);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence dns_rdataset_init(&rdataset);
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence result = dns_db_findrdataset(db, node, ver, dns_rdatatype_soa, 0,
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence (isc_stdtime_t) 0, &rdataset, NULL);
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence if (result != DNS_R_SUCCESS)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews goto freenode;
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence result = dns_rdataset_first(&rdataset);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (result != DNS_R_SUCCESS)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews goto freerdataset;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rdataset_current(&rdataset, &rdata);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews *serialp = dns_soa_getserial(&rdata);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result = DNS_R_SUCCESS;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews freerdataset:
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rdataset_disassociate(&rdataset);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
efe2f579ba6fbec4229129015780f2d925085b04Michael Graff freenode:
6add3af8397580475ab9fa10ac8c1269465cff87Mark Andrews dns_db_detachnode(db, &node);
6add3af8397580475ab9fa10ac8c1269465cff87Mark Andrews return (result);
6add3af8397580475ab9fa10ac8c1269465cff87Mark Andrews}
6add3af8397580475ab9fa10ac8c1269465cff87Mark Andrews
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellingtondns_result_t
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsdns_db_createsoatuple(dns_db_t *db, dns_dbversion_t *ver, isc_mem_t *mctx,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_diffop_t op, dns_difftuple_t **tp)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence{
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_result_t result;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_dbnode_t *node;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rdataset_t rdataset;
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews dns_rdata_t rdata;
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews dns_name_t *zonename;
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews dns_rdataclass_t zoneclass;
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews zonename = dns_db_origin(db);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews zoneclass = dns_db_class(db);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews node = NULL;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result = dns_db_findnode(db, zonename, ISC_FALSE, &node);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (result != DNS_R_SUCCESS)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (result);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rdataset_init(&rdataset);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result = dns_db_findrdataset(db, node, ver, dns_rdatatype_soa, 0,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews (isc_stdtime_t) 0, &rdataset, NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (result != DNS_R_SUCCESS)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews goto freenode;
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews result = dns_rdataset_first(&rdataset);
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews if (result != DNS_R_SUCCESS)
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews goto freenode;
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rdataset_current(&rdataset, &rdata);
171d0db7f93bf796f870713d6208b21893401cfcBrian Wellington
171d0db7f93bf796f870713d6208b21893401cfcBrian Wellington result = dns_difftuple_create(mctx, op, zonename, rdataset.ttl,
171d0db7f93bf796f870713d6208b21893401cfcBrian Wellington &rdata, tp);
171d0db7f93bf796f870713d6208b21893401cfcBrian Wellington
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews dns_rdataset_disassociate(&rdataset);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence freenode:
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_db_detachnode(db, &node);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (result);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/**************************************************************************/
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/*
af9cfbc64363b61aa5903dd916e9fbc152084d4cMark Andrews * Diffs, aka Pending Journal Entries.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
af9cfbc64363b61aa5903dd916e9fbc152084d4cMark Andrews
03084608809afbaf570093a14d2dc4ad507336a0Brian Wellingtondns_result_t
03084608809afbaf570093a14d2dc4ad507336a0Brian Wellingtondns_difftuple_create(isc_mem_t *mctx,
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews dns_diffop_t op, dns_name_t *name, dns_ttl_t ttl,
03084608809afbaf570093a14d2dc4ad507336a0Brian Wellington dns_rdata_t *rdata, dns_difftuple_t **tp)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews{
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_difftuple_t *t;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned int size;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned char *datap;
af9cfbc64363b61aa5903dd916e9fbc152084d4cMark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(tp != NULL && *tp == NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews /*
347ccc2716b45b8c72a1021d7a74faab49d3eefaBrian Wellington * Create a new tuple. The variable-size wire-format name data and
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence * rdata immediately follow the dns_difftuple_t structure
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence * in memory.
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence */
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence size = sizeof(*t) + name->length + rdata->length;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t = isc_mem_allocate(mctx, size);
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence if (t == NULL)
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence return (DNS_R_NOMEMORY);
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence t->mctx = mctx;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->op = op;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews datap = (unsigned char *) (t + 1);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence memcpy(datap, name->ndata, name->length);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_name_init(&t->name, NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_name_clone(name, &t->name);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->name.ndata = datap;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews datap += name->length;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->ttl = ttl;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews memcpy(datap, rdata->data, rdata->length);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->rdata.data = datap;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->rdata.length = rdata->length;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->rdata.rdclass = rdata->rdclass;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->rdata.type = rdata->type;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews datap += rdata->length;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ISC_LINK_INIT(&t->rdata, link);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->magic = DNS_DIFFTUPLE_MAGIC;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(datap == (unsigned char *) t + size);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence *tp = t;
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence return (DNS_R_SUCCESS);
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence}
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrencevoid
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrencedns_difftuple_free(dns_difftuple_t **tp) {
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence dns_difftuple_t *t = *tp;
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence REQUIRE(DNS_DIFFTUPLE_VALID(t));
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence dns_name_invalidate(&t->name);
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence t->magic = 0;
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence isc_mem_free(t->mctx, t);
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence *tp = NULL;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrencedns_result_t
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsdns_difftuple_copy(dns_difftuple_t *orig, dns_difftuple_t **copyp)
ea023072092112a831ca7c2943387ccbb422a3f2Mark Andrews{
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (dns_difftuple_create(orig->mctx, orig->op, &orig->name,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews orig->ttl, &orig->rdata, copyp));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
efe2f579ba6fbec4229129015780f2d925085b04Michael Graff
03084608809afbaf570093a14d2dc4ad507336a0Brian Wellingtonvoid
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellingtondns_diff_init(isc_mem_t *mctx, dns_diff_t *diff)
347ccc2716b45b8c72a1021d7a74faab49d3eefaBrian Wellington{
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews diff->mctx = mctx;
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews ISC_LIST_INIT(diff->tuples);
347ccc2716b45b8c72a1021d7a74faab49d3eefaBrian Wellington diff->magic = DNS_DIFF_MAGIC;
03084608809afbaf570093a14d2dc4ad507336a0Brian Wellington}
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews
171d0db7f93bf796f870713d6208b21893401cfcBrian Wellingtonvoid
171d0db7f93bf796f870713d6208b21893401cfcBrian Wellingtondns_diff_clear(dns_diff_t *diff) {
171d0db7f93bf796f870713d6208b21893401cfcBrian Wellington dns_difftuple_t *t;
171d0db7f93bf796f870713d6208b21893401cfcBrian Wellington REQUIRE(DNS_DIFF_VALID(diff));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews while ((t = ISC_LIST_HEAD(diff->tuples)) != NULL) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ISC_LIST_UNLINK(diff->tuples, t, link);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_difftuple_free(&t);
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews }
03084608809afbaf570093a14d2dc4ad507336a0Brian Wellington ENSURE(ISC_LIST_EMPTY(diff->tuples));
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsvoid
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsdns_diff_append(dns_diff_t *diff, dns_difftuple_t **tuplep)
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence{
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence ISC_LIST_APPEND(diff->tuples, *tuplep, link);
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence *tuplep = NULL;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
af9cfbc64363b61aa5903dd916e9fbc152084d4cMark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/* XXX this is O(N) */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsvoid
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsdns_diff_appendminimal(dns_diff_t *diff, dns_difftuple_t **tuplep)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews{
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews dns_difftuple_t *ot, *next_ot;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(DNS_DIFF_VALID(diff));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(DNS_DIFFTUPLE_VALID(*tuplep));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Look for an existing tuple with the same owner name,
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence * rdata, and TTL. If we are doing an addition and find a
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * deletion or vice versa, remove both the old and the
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * new tuple since they cancel each other out (assuming
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * that we never delete nonexistent data or add existing
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * data).
efe2f579ba6fbec4229129015780f2d925085b04Michael Graff *
03084608809afbaf570093a14d2dc4ad507336a0Brian Wellington * If we find an old update of the same kind as
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington * the one we are doing, there must be a programming
347ccc2716b45b8c72a1021d7a74faab49d3eefaBrian Wellington * error. We report it but try to continue anyway.
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews for (ot = ISC_LIST_HEAD(diff->tuples); ot != NULL;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ot = next_ot)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews next_ot = ISC_LIST_NEXT(ot, link);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (dns_name_equal(&ot->name, &(*tuplep)->name) &&
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rdata_compare(&ot->rdata, &(*tuplep)->rdata) == 0 &&
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews ot->ttl == (*tuplep)->ttl)
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews {
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews ISC_LIST_UNLINK(diff->tuples, ot, link);
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews if ((*tuplep)->op == ot->op) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews "unexpected non-minimal diff");
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews } else {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_difftuple_free(tuplep);
af9cfbc64363b61aa5903dd916e9fbc152084d4cMark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_difftuple_free(&ot);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews break;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (*tuplep != NULL) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ISC_LIST_APPEND(diff->tuples, *tuplep, link);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews *tuplep = NULL;
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ENSURE(*tuplep == NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsdns_result_t
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsdns_diff_apply(dns_diff_t *diff, dns_db_t *db, dns_dbversion_t *ver)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews{
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews dns_difftuple_t *t;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_dbnode_t *node = NULL;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_result_t result;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(DNS_DIFF_VALID(diff));
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews REQUIRE(DNS_DB_VALID(db));
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews t = ISC_LIST_HEAD(diff->tuples);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews while (t != NULL) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_name_t *name;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(node == NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews name = &t->name;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Find the node.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * We create the node if it does not exist.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * This will cause an empty node to be created if the diff
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * contains a deletion of an RR at a nonexistent name,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * but such diffs should never be created in the first
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * place.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews node = NULL;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews CHECK(dns_db_findnode(db, name, ISC_TRUE, &node));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews while (t != NULL && dns_name_equal(&t->name, name)) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rdatatype_t type, covers;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_diffop_t op;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rdatalist_t rdl;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rdataset_t rds;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews op = t->op;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews type = t->rdata.type;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews covers = rdata_covers(&t->rdata);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Collect a contiguous set of updates with
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * the same operation (add/delete) and RR type
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * into a single rdatalist so that the
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * database rrset merging/subtraction code
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * can work more efficiently than if each
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * RR were merged into / subtracted from
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * the database separately.
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence *
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * This is done by linking rdata structures from the
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * diff into "rdatalist". This uses the rdata link
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * field, not the diff link field, so the structure
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * of the diff itself is not affected.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews rdl.type = type;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews rdl.covers = covers;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews rdl.rdclass = t->rdata.rdclass;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews rdl.ttl = t->ttl;
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews ISC_LIST_INIT(rdl.rdata);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ISC_LINK_INIT(&rdl, link);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews while (t != NULL &&
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_name_equal(&t->name, name) &&
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->op == op &&
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->rdata.type == type &&
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews rdata_covers(&t->rdata) == covers)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (t->ttl != rdl.ttl) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_log_write(JOURNAL_COMMON_LOGARGS,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ISC_LOG_WARNING,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews "TTL differs in rdataset, "
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews "adjusting %lu -> %lu",
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews (unsigned long) t->ttl,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews (unsigned long) rdl.ttl);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ISC_LIST_APPEND(rdl.rdata, &t->rdata, link);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence t = ISC_LIST_NEXT(t, link);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /* Convert the rdatalist into a rdataset. */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rdataset_init(&rds);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews CHECK(dns_rdatalist_tordataset(&rdl, &rds));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /* Merge the rdataset into the database. */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (op == DNS_DIFFOP_ADD) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result = dns_db_addrdataset(db, node, ver,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews 0, &rds,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ISC_TRUE, NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews } else if (op == DNS_DIFFOP_DEL) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result = dns_db_subtractrdataset(db, node, ver,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews &rds,
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews } else {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(0);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence if (result == DNS_R_UNCHANGED) {
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence /*
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * This will not happen when executing a
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * dynamic update, because that code will
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * generate strictly minimal diffs.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * It may happen when receiving an IXFR
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * from a server that is not as careful.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Issue a warning and continue.
fa5a42fbad42f4e033376d5d4624e29d018d97b7Brian Wellington */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_log_write(JOURNAL_COMMON_LOGARGS,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ISC_LOG_WARNING,
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews "update with no effect");
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews } else if (result == DNS_R_SUCCESS ||
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result == DNS_R_NXRDATASET) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /* OK */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews } else {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews CHECK(result);
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_db_detachnode(db, &node);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (DNS_R_SUCCESS);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews failure:
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (node != NULL)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_db_detachnode(db, &node);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (result);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
fa5a42fbad42f4e033376d5d4624e29d018d97b7Brian Wellington
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/* XXX this duplicates lots of code in dns_diff_apply(). */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsdns_result_t
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsdns_diff_load(dns_diff_t *diff, dns_addrdatasetfunc_t addfunc,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews void *add_private)
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence{
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence dns_difftuple_t *t;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_result_t result;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(DNS_DIFF_VALID(diff));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t = ISC_LIST_HEAD(diff->tuples);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews while (t != NULL) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_name_t *name;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence name = &t->name;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews while (t != NULL && dns_name_equal(&t->name, name)) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rdatatype_t type, covers;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_diffop_t op;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rdatalist_t rdl;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rdataset_t rds;
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence op = t->op;
ed71ea51c6ecb5d7d659b6e6a20f6b3f5c2678c6David Lawrence type = t->rdata.type;
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence covers = rdata_covers(&t->rdata);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews rdl.type = type;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews rdl.covers = covers;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews rdl.rdclass = t->rdata.rdclass;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews rdl.ttl = t->ttl;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ISC_LIST_INIT(rdl.rdata);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ISC_LINK_INIT(&rdl, link);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews while (t != NULL && dns_name_equal(&t->name, name) &&
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->op == op && t->rdata.type == type &&
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews rdata_covers(&t->rdata) == covers)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews {
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews ISC_LIST_APPEND(rdl.rdata, &t->rdata, link);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t = ISC_LIST_NEXT(t, link);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /* Convert the rdatalist into a rdataset. */
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews dns_rdataset_init(&rds);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews CHECK(dns_rdatalist_tordataset(&rdl, &rds));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(op == DNS_DIFFOP_ADD);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result = (*addfunc)(add_private, name, &rds);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (result == DNS_R_UNCHANGED) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_log_write(JOURNAL_COMMON_LOGARGS,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ISC_LOG_WARNING,
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence "update with no effect");
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence } else if (result == DNS_R_SUCCESS ||
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result == DNS_R_NXRDATASET) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /* OK */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews } else {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews CHECK(result);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence result = DNS_R_SUCCESS;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews failure:
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (result);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * XXX uses qsort(); a merge sort would be more natural for lists,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * and perhaps safer wrt thread stack overflow.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsdns_result_t
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsdns_diff_sort(dns_diff_t *diff, dns_diff_compare_func *compare) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned int length = 0;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned int i;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_difftuple_t **v;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_difftuple_t *p;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(DNS_DIFF_VALID(diff));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews for (p = ISC_LIST_HEAD(diff->tuples);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews p != NULL;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews p = ISC_LIST_NEXT(p, link))
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews length++;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (length == 0)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (ISC_R_SUCCESS);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews v = isc_mem_get(diff->mctx, length * sizeof(dns_difftuple_t *));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (v == NULL)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (DNS_R_NOMEMORY);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews i = 0;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews for (i = 0; i < length; i++) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews p = ISC_LIST_HEAD(diff->tuples);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews v[i] = p;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ISC_LIST_UNLINK(diff->tuples, p, link);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(ISC_LIST_HEAD(diff->tuples) == NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews qsort(v, length, sizeof(v[0]), compare);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews for (i = 0; i < length; i++) {
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews ISC_LIST_APPEND(diff->tuples, v[i], link);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_mem_put(diff->mctx, v, length * sizeof(dns_difftuple_t *));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (ISC_R_SUCCESS);
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Create an rdataset containing the single RR of the given
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * tuple. The caller must allocate both the rdataset and
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * an rdatalist structure for it to refer to.
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsstatic dns_result_t
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsdiff_tuple_tordataset(dns_difftuple_t *t, dns_rdatalist_t *rdl,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rdataset_t *rds)
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews{
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(DNS_DIFFTUPLE_VALID(t));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(rdl != NULL);
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence REQUIRE(rds != NULL);
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence rdl->type = t->rdata.type;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews rdl->rdclass = t->rdata.rdclass;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews rdl->ttl = t->ttl;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ISC_LIST_INIT(rdl->rdata);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ISC_LINK_INIT(rdl, link);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rdataset_init(rds);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ISC_LIST_APPEND(rdl->rdata, &t->rdata, link);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (dns_rdatalist_tordataset(rdl, rds));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsdns_result_t
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsdns_diff_print(dns_diff_t *diff, FILE *file) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_result_t result;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_difftuple_t *t;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews char *mem = NULL;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned int size = 2048;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(DNS_DIFF_VALID(diff));
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews mem = isc_mem_get(diff->mctx, size);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (mem == NULL)
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence return (DNS_R_NOMEMORY);
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence for (t = ISC_LIST_HEAD(diff->tuples); t != NULL;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t = ISC_LIST_NEXT(t, link))
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_buffer_t buf;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_region_t r;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rdatalist_t rdl;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_rdataset_t rds;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result = diff_tuple_tordataset(t, &rdl, &rds);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (result != DNS_R_SUCCESS) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews "diff_tuple_tordataset failed: %s",
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews dns_result_totext(result));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result = DNS_R_UNEXPECTED;
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews goto cleanup;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews again:
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_buffer_init(&buf, mem, size, ISC_BUFFERTYPE_TEXT);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result = dns_rdataset_totext(&rds, &t->name,
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence ISC_FALSE, ISC_FALSE, &buf);
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence /* Get rid of final newline. */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(buf.used >= 1 && ((char *) buf.base)[buf.used-1] == '\n');
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews buf.used--;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (result == DNS_R_NOSPACE) {
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews isc_mem_put(diff->mctx, mem, size);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews size += 1024;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews mem = isc_mem_get(diff->mctx, size);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (mem == NULL) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result = DNS_R_NOMEMORY;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews goto cleanup;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews goto again;
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence }
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence if (result == DNS_R_SUCCESS) {
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence isc_buffer_used(&buf, &r);
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews if (file != NULL)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews fprintf(file, "%s %.*s\n",
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->op == DNS_DIFFOP_ADD ?
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews "add" : "del", (int) r.length,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews (char *) r.base);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews else
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence isc_log_write(JOURNAL_COMMON_LOGARGS,
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence ISC_LOG_DEBUG(7),
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence "%s %.*s",
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->op == DNS_DIFFOP_ADD ?
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews "add" : "del",
5236050c0f819b727dfc4850133f5194f346f76fDanny Mayer (int) r.length, (char *) r.base);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews } else
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews goto cleanup;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result = DNS_R_SUCCESS;
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews cleanup:
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (mem != NULL)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_mem_put(diff->mctx, mem, size);
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews return (result);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/**************************************************************************/
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/*
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews * Journalling.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/*
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews * A journal file consists of
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews *
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * - A fixed-size header of type journal_rawheader_t.
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews *
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * - The index. This is an unordered array of index entries
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * of type journal_rawpos_t giving the locations
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * of some arbitrary subset of the journal's addressable
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * transactions. The index entries are used as hints to
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * speed up the process of locating a transaction with a given
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * serial number. Unused index entries have an "offset"
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * field of zero. The size of the index can vary between
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews * journal files, but does not change during the lifetime
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * of a file. The size can be zero.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews *
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * - The journal data. This consists of one or more transactions.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Each transaction begins with a transaction header of type
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * journal_rawxhdr_t. The transaction header is followed by a
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * sequence of RRs, similar in structure to an IXFR difference
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * sequence (RFC1995). That is, the pre-transaction SOA,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * zero or more other deleted RRs, the post-transaction SOA,
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * and zero or more other added RRs. Unlike in IXFR, each RR
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * is prefixed with a 32-bit length.
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence *
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * The journal data part grows as new transactions are
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * appended to the file. Only those transactions
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * whose serial number is current-(2^31-1) to current
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews * are considered "addressable" and may be pointed
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * to from the header or index. They may be preceded
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * by old transactions that are no longer addressable,
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews * and they may be followed by transactions that were
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * appended to the journal but never committed by updating
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * the "end" position in the header. The latter will
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * be overwritten when new transactions are added.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence/*
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * On-disk representation of a "pointer" to a journal entry.
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * These are used in the journal header to locate the beginning
88ed7381f16a72409061875ddeda598f477e5ef6Michael Graff * and end of the journal, and in the journal index to locate
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * other transactions.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewstypedef struct {
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence unsigned char serial[4]; /* SOA serial before update. */
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence /*
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * XXXRTH Should offset be 8 bytes?
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
347ccc2716b45b8c72a1021d7a74faab49d3eefaBrian Wellington unsigned char offset[4]; /* Offset from beginning of file. */
347ccc2716b45b8c72a1021d7a74faab49d3eefaBrian Wellington} journal_rawpos_t;
f1b68725503ff3e46001eee5a1751e29a43a09d1Andreas Gustafsson
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * The on-disk representation of the journal header.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * All numbers are stored in big-endian order.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/*
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * The header is of a fixed size, with some spare room for future
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * extensions.
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#define JOURNAL_HEADER_SIZE 64 /* Bytes. */
c4cc75482fd6846163cafa79e98abb11bcfaf3d3Mark Andrews
c4cc75482fd6846163cafa79e98abb11bcfaf3d3Mark Andrewstypedef union {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews struct {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /* File format version ID. */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned char format[16];
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /* Position of the first addressable transaction */
c4cc75482fd6846163cafa79e98abb11bcfaf3d3Mark Andrews journal_rawpos_t begin;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /* Position of the next (yet nonexistent) transaction. */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews journal_rawpos_t end;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /* Number of index entries following the header. */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned char index_size[4];
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews } h;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /* Pad the header to a fixed size. */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned char pad[JOURNAL_HEADER_SIZE];
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews} journal_rawheader_t;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * The on-disk representation of the transaction header.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * There is one of these at the beginning of each transaction.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewstypedef struct {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned char size[4]; /* In bytes, excluding header. */
347ccc2716b45b8c72a1021d7a74faab49d3eefaBrian Wellington unsigned char serial0[4]; /* SOA serial before update. */
347ccc2716b45b8c72a1021d7a74faab49d3eefaBrian Wellington unsigned char serial1[4]; /* SOA serial after update. */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews} journal_rawxhdr_t;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * The on-disk representation of the RR header.
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * There is one of these at the beginning of each RR.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewstypedef struct {
d736db6dc53e615e3f2d66d1ddbe28473694d107Michael Graff unsigned char size[4]; /* In bytes, excluding header. */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews} journal_rawrrhdr_t;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/* The in-core representation of the journal header. */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewstypedef struct {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_uint32_t serial;
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence /*
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * XXXRTH Should offset be 8 bytes?
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_uint32_t offset;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews} journal_pos_t;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews#define POS_VALID(pos) ((pos).offset != 0)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#define POS_INVALIDATE(pos) ((pos).offset = 0, (pos).serial = 0)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrewstypedef struct {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned char format[16];
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews journal_pos_t begin;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews journal_pos_t end;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_uint32_t index_size;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews} journal_header_t;
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence/* The in-core representation of the transaction header. */
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence
88ed7381f16a72409061875ddeda598f477e5ef6Michael Grafftypedef struct {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_uint32_t size;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_uint32_t serial0;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_uint32_t serial1;
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence} journal_xhdr_t;
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence/* The in-core representation of the RR header. */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
f1b68725503ff3e46001eee5a1751e29a43a09d1Andreas Gustafssontypedef struct {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_uint32_t size;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews} journal_rrhdr_t;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Initial contents to store in the header of a newly created
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * journal file.
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence *
ca81c3971cd0c9cfd830c0a18289d4d1b0f9de01David Lawrence * The header starts with the magic string ";BIND LOG V9\n"
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * to identify the file as a BIND 9 journal file. An ASCII
d736db6dc53e615e3f2d66d1ddbe28473694d107Michael Graff * identification string is used rather than a binary magic
d736db6dc53e615e3f2d66d1ddbe28473694d107Michael Graff * number to be consistent with BIND 8 (BIND 8 journal files
d736db6dc53e615e3f2d66d1ddbe28473694d107Michael Graff * are ASCII text files).
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsstatic journal_header_t
d736db6dc53e615e3f2d66d1ddbe28473694d107Michael Graffinitial_journal_header = { ";BIND LOG V9\n", { 0, 0 }, { 0, 0 }, 0 };
d736db6dc53e615e3f2d66d1ddbe28473694d107Michael Graff
d736db6dc53e615e3f2d66d1ddbe28473694d107Michael Graff#define JOURNAL_EMPTY(h) ((h)->begin.offset == (h)->end.offset)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewstypedef enum {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews JOURNAL_STATE_INVALID,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews JOURNAL_STATE_READ,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews JOURNAL_STATE_WRITE,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews JOURNAL_STATE_TRANSACTION
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews} journal_state_t;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * XXXRTH We use 'off_t' in the following structure. If we continue to
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * use stdio instead of creating an ISC file module, we'll convert
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * 'off_t' to 'long'.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsstruct dns_journal {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned int magic; /* JOUR */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_mem_t *mctx; /* Memory context */
347ccc2716b45b8c72a1021d7a74faab49d3eefaBrian Wellington journal_state_t state;
347ccc2716b45b8c72a1021d7a74faab49d3eefaBrian Wellington const char *filename; /* Journal file name */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews FILE * fp; /* File handle */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews off_t offset; /* Current file offset */
journal_header_t header; /* In-core journal header */
journal_pos_t *index; /* In-core journal index */
/* Current transaction state (when writing). */
struct {
unsigned int n_soa; /* Number of SOAs seen */
journal_pos_t pos[2]; /* Begin/end position */
} x;
/* Iteration state (when reading). */
struct {
/* These define the part of the journal we iterate over. */
journal_pos_t bpos; /* Position before first, */
journal_pos_t epos; /* and after last
transaction */
/* The rest is iterator state. */
isc_uint32_t current_serial; /* Current SOA serial */
isc_buffer_t source; /* Data from disk */
isc_buffer_t target; /* Data from _fromwire check */
dns_decompress_t dctx; /* Dummy decompression ctx */
dns_name_t name; /* Current domain name */
dns_rdata_t rdata; /* Current rdata */
isc_uint32_t ttl; /* Current TTL */
unsigned int xsize; /* Size of transaction data */
unsigned int xpos; /* Current position in it */
dns_result_t result; /* Result of last call */
} it;
};
#define DNS_JOURNAL_MAGIC 0x4a4f5552U /* JOUR. */
#define DNS_JOURNAL_VALID(t) ((t) != NULL && \
(t)->magic == DNS_JOURNAL_MAGIC)
static void
journal_pos_decode(journal_rawpos_t *raw, journal_pos_t *cooked)
{
cooked->serial = decode_uint32(raw->serial);
cooked->offset = decode_uint32(raw->offset);
}
static void
journal_pos_encode(journal_rawpos_t *raw, journal_pos_t *cooked)
{
encode_uint32(cooked->serial, raw->serial);
encode_uint32(cooked->offset, raw->offset);
}
static void
journal_header_decode(journal_rawheader_t *raw, journal_header_t *cooked)
{
INSIST(sizeof(cooked->format) == sizeof(raw->h.format));
memcpy(cooked->format, raw->h.format, sizeof(cooked->format));
journal_pos_decode(&raw->h.begin, &cooked->begin);
journal_pos_decode(&raw->h.end, &cooked->end);
cooked->index_size = decode_uint32(raw->h.index_size);
}
static void
journal_header_encode(journal_header_t *cooked, journal_rawheader_t *raw)
{
INSIST(sizeof(cooked->format) == sizeof(raw->h.format));
memset(raw->pad, 0, sizeof(raw->pad));
memcpy(raw->h.format, cooked->format, sizeof(raw->h.format));
journal_pos_encode(&raw->h.begin, &cooked->begin);
journal_pos_encode(&raw->h.end, &cooked->end);
encode_uint32(cooked->index_size, raw->h.index_size);
}
/* Journal file I/O subroutines, with error checking and reporting. */
/*
* XXXRTH You may not use UNIX I/O routines. Use fread()/fwrite()/fseek().
* Alternatively, we can create isc_file_t. We may need to do this
* anyway at some point, to provide "safe open" semantics (see
* write_open() in BIND 8's ns_config.c). We also need a portable
* "fsync()", so isc_file_t is looking more and more probable.
*/
static dns_result_t
journal_seek(dns_journal_t *j, isc_uint32_t offset) {
int seek_result;
seek_result = fseek(j->fp, (long) offset, SEEK_SET);
if (seek_result != 0) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: seek: %s",
j->filename, strerror(errno));
return (DNS_R_UNEXPECTED);
}
j->offset = offset;
return (DNS_R_SUCCESS);
}
static dns_result_t
journal_read(dns_journal_t *j, void *mem, size_t nbytes) {
size_t nread;
clearerr(j->fp);
nread = fread(mem, 1, nbytes, j->fp);
if (nread != nbytes) {
if (feof(j->fp))
return (DNS_R_NOMORE);
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: read: %s",
j->filename, strerror(errno));
return (DNS_R_UNEXPECTED);
}
j->offset += nbytes;
return (DNS_R_SUCCESS);
}
static dns_result_t
journal_write(dns_journal_t *j, void *mem, size_t nbytes) {
size_t nwritten;
clearerr(j->fp);
nwritten = fwrite(mem, 1, nbytes, j->fp);
if (nwritten != nbytes) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: write: %s",
j->filename, strerror(errno));
return (DNS_R_UNEXPECTED);
}
j->offset += nbytes;
return (DNS_R_SUCCESS);
}
static dns_result_t
journal_fsync(dns_journal_t *j) {
int r;
r = fflush(j->fp);
if (r < 0) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: fflush: %s",
j->filename,
strerror(errno));
return (DNS_R_UNEXPECTED);
}
r = fsync(fileno(j->fp));
if (r < 0) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: fsync: %s",
j->filename,
strerror(errno));
return (DNS_R_UNEXPECTED);
}
return (DNS_R_SUCCESS);
}
/* Read/write a transaction header at the current file position. */
static dns_result_t
journal_read_xhdr(dns_journal_t *j, journal_xhdr_t *xhdr) {
journal_rawxhdr_t raw;
dns_result_t result;
result = journal_read(j, &raw, sizeof(raw));
if (result != DNS_R_SUCCESS)
return (result);
xhdr->size = decode_uint32(raw.size);
xhdr->serial0 = decode_uint32(raw.serial0);
xhdr->serial1 = decode_uint32(raw.serial1);
return (DNS_R_SUCCESS);
}
static dns_result_t
journal_write_xhdr(dns_journal_t *j, isc_uint32_t size,
isc_uint32_t serial0, isc_uint32_t serial1)
{
journal_rawxhdr_t raw;
encode_uint32(size, raw.size);
encode_uint32(serial0, raw.serial0);
encode_uint32(serial1, raw.serial1);
return (journal_write(j, &raw, sizeof(raw)));
}
/* Read an RR header at the current file position. */
static dns_result_t
journal_read_rrhdr(dns_journal_t *j, journal_rrhdr_t *rrhdr) {
journal_rawrrhdr_t raw;
dns_result_t result;
result = journal_read(j, &raw, sizeof(raw));
if (result != DNS_R_SUCCESS)
return (result);
rrhdr->size = decode_uint32(raw.size);
return (DNS_R_SUCCESS);
}
static dns_result_t
journal_file_create(isc_mem_t *mctx, const char *filename) {
FILE *fp;
int r;
size_t nwritten;
journal_header_t header;
journal_rawheader_t rawheader;
int index_size = 56; /* XXX configurable */
int size;
void *mem; /* Memory for temporary index image. */
INSIST(sizeof(journal_rawheader_t) == JOURNAL_HEADER_SIZE);
fp = fopen(filename, "w");
if (fp == 0) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: create: %s",
filename, strerror(errno));
return (DNS_R_UNEXPECTED);
}
header = initial_journal_header;
header.index_size = index_size;
journal_header_encode(&header, &rawheader);
size = sizeof(journal_rawheader_t) +
index_size * sizeof(journal_rawpos_t);
mem = isc_mem_get(mctx, size);
if (mem == NULL) {
(void) fclose(fp);
(void) unlink(filename);
return (DNS_R_NOMEMORY);
}
memset(mem, 0, size);
memcpy(mem, &rawheader, sizeof(rawheader));
nwritten = fwrite(mem, 1, (size_t) size, fp);
if (nwritten != (size_t) size) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: write: %s",
filename, strerror(errno));
(void) fclose(fp);
(void) unlink(filename);
isc_mem_put(mctx, mem, size);
return (DNS_R_UNEXPECTED);
}
isc_mem_put(mctx, mem, size);
r = fclose(fp);
if (r != 0) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: close: %s",
filename, strerror(errno));
(void) unlink(filename);
return (DNS_R_UNEXPECTED);
}
return (DNS_R_SUCCESS);
}
dns_result_t
dns_journal_open(isc_mem_t *mctx, const char *filename, isc_boolean_t write,
dns_journal_t **journalp) {
FILE *fp;
dns_result_t result;
journal_rawheader_t rawheader;
dns_journal_t *j;
INSIST(journalp != NULL && *journalp == NULL);
j = isc_mem_get(mctx, sizeof(*j));
if (j == NULL)
return (ISC_R_NOMEMORY);
j->mctx = mctx;
j->state = JOURNAL_STATE_INVALID;
j->fp = 0;
j->filename = filename;
j->index = NULL;
/* XXX fopen() will need "b" (binary) on some platforms */
fp = fopen(j->filename, write ? "r+" : "r");
if (fp == 0 && errno == ENOENT) {
if (write) {
isc_log_write(JOURNAL_COMMON_LOGARGS,
ISC_LOG_INFO,
"journal file %s does not exist, "
"creating it",
j->filename);
CHECK(journal_file_create(mctx, filename));
/* Retry. */
fp = fopen(j->filename, "r+");
} else {
FAIL(ISC_R_NOTFOUND);
}
}
if (fp == 0) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: open: %s",
j->filename, strerror(errno));
FAIL(DNS_R_UNEXPECTED);
}
j->fp = fp;
/* Set magic early so that seek/read can succeed. */
j->magic = DNS_JOURNAL_MAGIC;
CHECK(journal_seek(j, 0));
CHECK(journal_read(j, &rawheader, sizeof(rawheader)));
if (memcmp(rawheader.h.format, initial_journal_header.format,
sizeof(initial_journal_header.format)) != 0) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: jornal format not recognized",
j->filename);
FAIL(DNS_R_UNEXPECTED);
}
journal_header_decode(&rawheader, &j->header);
/*
* When opening a journal for reading, it is not supposed to
* be empty - if there are no transactions, the file should
* not exist.
*/
if (! write && JOURNAL_EMPTY(&j->header)) {
isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
"journal file %s is empty", j->filename);
FAIL(DNS_R_NOTFOUND);
}
/*
* If there is an index, read it into dynamically allocated
* memory and byte swap it if necessary.
*/
if (j->header.index_size != 0) {
unsigned int i;
INSIST(sizeof(journal_rawpos_t) == sizeof(journal_pos_t));
j->index = isc_mem_get(mctx, j->header.index_size *
sizeof(journal_rawpos_t));
if (j->index == NULL)
FAIL(ISC_R_NOMEMORY);
CHECK(journal_read(j, j->index, j->header.index_size *
sizeof(journal_rawpos_t)));
for (i = 0; i < j->header.index_size; i++) {
j->index[i].serial = ntohl(j->index[i].serial);
j->index[i].offset = ntohl(j->index[i].offset);
}
}
j->offset = -1; /* Invalid, must seek explicitly. */
/* Initialize the iterator. */
dns_name_init(&j->it.name, NULL);
dns_rdata_init(&j->it.rdata);
/*
* Set up empty initial buffers for uncheched and checked
* wire format RR data. They will be reallocated
* later.
*/
isc_buffer_init(&j->it.source, NULL, 0, ISC_BUFFERTYPE_BINARY);
isc_buffer_init(&j->it.target, NULL, 0, ISC_BUFFERTYPE_BINARY);
dns_decompress_init(&j->it.dctx, -1, ISC_FALSE);
j->state =
write ? JOURNAL_STATE_WRITE : JOURNAL_STATE_READ;
*journalp = j;
return (DNS_R_SUCCESS);
failure:
j->magic = 0;
if (j->index != NULL) {
isc_mem_put(j->mctx, j->index, j->header.index_size *
sizeof(journal_rawpos_t));
j->index = NULL;
}
if (j->fp != NULL)
(void) fclose(j->fp);
isc_mem_put(j->mctx, j, sizeof(*j));
return (result);
}
/*
* A comparison function defining the sorting order for
* entries in the IXFR-style journal file.
*
* The IXFR format requires that deletions are sorted before
* additions, and within either one, SOA records are sorted
* before others.
*
* Also sort the non-SOA records by type as a courtesy to the
* server receiving the IXFR - it may help reduce the amount of
* rdataset merging it has to do.
*/
static int
ixfr_order(const void *av, const void *bv)
{
dns_difftuple_t * const *ap = av;
dns_difftuple_t * const *bp = bv;
dns_difftuple_t *a = *ap;
dns_difftuple_t *b = *bp;
int r;
r = (b->op == DNS_DIFFOP_DEL) - (a->op == DNS_DIFFOP_DEL);
if (r != 0)
return (r);
r = (b->rdata.type == dns_rdatatype_soa) -
(a->rdata.type == dns_rdatatype_soa);
if (r != 0)
return (r);
r = (a->rdata.type - b->rdata.type);
return (r);
}
/*
* Advance '*pos' to the next journal transaction.
*
* Requires:
* *pos refers to a valid journal transaction.
*
* Ensures:
* When DNS_R_SUCCESS is returned,
* *pos refers to the next journal transaction.
*
* Returns one of:
*
* DNS_R_SUCCESS
* DNS_R_NOMORE *pos pointed at the last transaction
* Other results due to file errors are possible.
*/
static dns_result_t
journal_next(dns_journal_t *j, journal_pos_t *pos) {
dns_result_t result;
journal_xhdr_t xhdr;
REQUIRE(DNS_JOURNAL_VALID(j));
result = journal_seek(j, pos->offset);
if (result != DNS_R_SUCCESS)
return (result);
/*
* Read the header of the current transaction.
* This will return DNS_R_NOMORE if we are at EOF.
*/
result = journal_read_xhdr(j, &xhdr);
if (result != DNS_R_SUCCESS)
return (result);
/* Check serial number consistency. */
if (xhdr.serial0 != pos->serial) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: journal corrupt: "
"expected serial %u, got %u",
j->filename,
pos->serial, xhdr.serial0);
return (DNS_R_UNEXPECTED);
}
/* Check for offset wraparound. */
if (pos->offset + xhdr.size < pos->offset) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: offset too large",
j->filename);
return (DNS_R_UNEXPECTED);
}
pos->offset += sizeof(journal_rawxhdr_t) + xhdr.size;
pos->serial = xhdr.serial1;
return (DNS_R_SUCCESS);
}
/*
* If the index of the journal 'j' contains an entry "better"
* than '*best_guess', replace '*best_guess' with it.
*
* "Better" means having a serial number closer to 'serial'
* but not greater than 'serial'.
*/
static void
index_find(dns_journal_t *j, isc_uint32_t serial, journal_pos_t *best_guess) {
unsigned int i;
if (j->index == NULL)
return;
for (i = 0; i < j->header.index_size; i++) {
if (POS_VALID(j->index[i]) &&
DNS_SERIAL_GE(serial, j->index[i].serial) &&
DNS_SERIAL_GT(j->index[i].serial, best_guess->serial))
*best_guess = j->index[i];
}
}
/*
* Add a new index entry. If there is no room, make room by removing
* the odd-numbered entries and compacting the others into the first
* half of the index. This decimates old index entries exponentially
* over time, so that the index always contains a much larger fraction
* of recent serial numbers than of old ones. This is deliberate -
* most index searches are for outgoing IXFR, and IXFR tends to request
* recent versions more often than old ones.
*/
static void
index_add(dns_journal_t *j, journal_pos_t *pos) {
unsigned int i;
if (j->index == NULL)
return;
/* Search for a vacant position. */
for (i = 0; i < j->header.index_size; i++) {
if (! POS_VALID(j->index[i]))
break;
}
if (i == j->header.index_size) {
unsigned int k = 0;
/* Found no vacant position. Make some room. */
for (i = 0; i < j->header.index_size; i += 2) {
j->index[k++] = j->index[i];
}
i = k; /* 'i' identifies the first vacant position. */
while (k < j->header.index_size) {
POS_INVALIDATE(j->index[k]);
k++;
}
}
INSIST(i < j->header.index_size);
INSIST(! POS_VALID(j->index[i]));
/* Store the new index entry. */
j->index[i] = *pos;
}
/*
* Invalidate any existing index entries that could become
* ambiguous when a new transaction with number 'serial' is added.
*/
static void
index_invalidate(dns_journal_t *j, isc_uint32_t serial)
{
unsigned int i;
if (j->index == NULL)
return;
for (i = 0; i < j->header.index_size; i++) {
if (! DNS_SERIAL_GT(serial, j->index[i].serial))
POS_INVALIDATE(j->index[i]);
}
}
/*
* Try to find a transaction with initial serial number 'serial'
* in the journal 'j'.
*
* If found, store its position at '*pos' and return DNS_R_SUCCESS.
*
* If 'serial' is current (= the ending serial number of the
* last transaction in the journal), set '*pos' to
* the position immediately following the last transaction and
* return DNS_R_SUCCESS.
*
* If 'serial' is within the range of addressable serial numbers
* covered by the journal but that particular serial number is missing
* (from the journal, not just from the index), return DNS_R_NOTFOUND.
*
* If 'serial' is outside the range of addressable serial numbers
* covered by the journal, return DNS_R_RANGE.
*
*/
static dns_result_t
journal_find(dns_journal_t *j, isc_uint32_t serial, journal_pos_t *pos) {
dns_result_t result;
journal_pos_t current_pos;
REQUIRE(DNS_JOURNAL_VALID(j));
if (DNS_SERIAL_GT(j->header.begin.serial, serial))
return (DNS_R_RANGE);
if (DNS_SERIAL_GT(serial, j->header.end.serial))
return (DNS_R_RANGE);
if (serial == j->header.end.serial) {
*pos = j->header.end;
return (DNS_R_SUCCESS);
}
current_pos = j->header.begin;
index_find(j, serial, &current_pos);
while (current_pos.serial != serial) {
if (DNS_SERIAL_GT(current_pos.serial, serial))
return (DNS_R_NOTFOUND);
result = journal_next(j, &current_pos);
if (result != DNS_R_SUCCESS)
return (result);
}
*pos = current_pos;
return (DNS_R_SUCCESS);
}
dns_result_t
dns_journal_begin_transaction(dns_journal_t *j) {
isc_uint32_t offset;
dns_result_t result;
journal_rawxhdr_t hdr;
REQUIRE(DNS_JOURNAL_VALID(j));
REQUIRE(j->state == JOURNAL_STATE_WRITE);
/*
* Find the file offset where the new transaction should
* be written, and seek there.
*/
if (JOURNAL_EMPTY(&j->header)) {
offset = sizeof(journal_rawheader_t) +
j->header.index_size * sizeof(journal_rawpos_t);
} else {
offset = j->header.end.offset;
}
j->x.pos[0].offset = offset;
j->x.pos[1].offset = offset; /* Initial value, will be incremented. */
j->x.n_soa = 0;
CHECK(journal_seek(j, offset));
/*
* Write a dummy transaction header of all zeroes to reserve
* space. It will be filled in when the transaction is
* finished.
*/
memset(&hdr, 0, sizeof(hdr));
CHECK(journal_write(j, &hdr, sizeof(hdr)));
j->x.pos[1].offset = j->offset;
j->state = JOURNAL_STATE_TRANSACTION;
result = DNS_R_SUCCESS;
failure:
return (result);
}
dns_result_t
dns_journal_writediff(dns_journal_t *j, dns_diff_t *diff) {
dns_difftuple_t *t;
isc_buffer_t buffer;
void *mem = NULL;
unsigned int size;
dns_result_t result;
isc_region_t used;
REQUIRE(DNS_DIFF_VALID(diff));
REQUIRE(j->state == JOURNAL_STATE_TRANSACTION);
isc_log_write(JOURNAL_DEBUG_LOGARGS(3), "writing to journal");
dns_diff_print(diff, NULL);
/*
* Pass 1: determine the buffer size needed, and
* keep track of SOA serial numbers.
*/
size = 0;
for (t = ISC_LIST_HEAD(diff->tuples); t != NULL;
t = ISC_LIST_NEXT(t, link))
{
if (t->rdata.type == dns_rdatatype_soa) {
if (j->x.n_soa < 2)
j->x.pos[j->x.n_soa].serial =
dns_soa_getserial(&t->rdata);
j->x.n_soa++;
}
size += sizeof(journal_rawrrhdr_t);
size += t->name.length; /* XXX should have access macro? */
size += 10;
size += t->rdata.length;
}
mem = isc_mem_get(j->mctx, size);
if (mem == NULL)
return (DNS_R_NOMEMORY);
isc_buffer_init(&buffer, mem, size, ISC_BUFFERTYPE_BINARY);
/*
* Pass 2. Write RRs to buffer.
*/
for (t = ISC_LIST_HEAD(diff->tuples); t != NULL;
t = ISC_LIST_NEXT(t, link))
{
isc_region_t avail;
/* Write the RR header */
isc_buffer_putuint32(&buffer, t->name.length + 10 +
t->rdata.length);
/* Write the owner name, RR header, and RR data. */
isc_buffer_putmem(&buffer, t->name.ndata, t->name.length);
isc_buffer_putuint16(&buffer, t->rdata.type);
isc_buffer_putuint16(&buffer, t->rdata.rdclass);
isc_buffer_putuint32(&buffer, t->ttl);
INSIST(t->rdata.length < 65536);
isc_buffer_putuint16(&buffer, (isc_uint16_t)t->rdata.length);
isc_buffer_available(&buffer, &avail);
isc_buffer_putmem(&buffer, t->rdata.data, t->rdata.length);
}
isc_buffer_used(&buffer, &used);
INSIST(used.length == size);
j->x.pos[1].offset += used.length;
/* Write the buffer contents to the journal file. */
CHECK(journal_write(j, used.base, used.length));
result = DNS_R_SUCCESS;
failure:
if (mem != NULL)
isc_mem_put(j->mctx, mem, size);
return (result);
}
dns_result_t
dns_journal_commit(dns_journal_t *j) {
dns_result_t result;
journal_rawheader_t rawheader;
unsigned int i;
REQUIRE(DNS_JOURNAL_VALID(j));
REQUIRE(j->state == JOURNAL_STATE_TRANSACTION);
/* Perform some basic consistency checks. */
if (j->x.n_soa != 2) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"malformed transaction: %d SOAs",
j->x.n_soa);
return (ISC_R_UNEXPECTED);
}
if (! (DNS_SERIAL_GT(j->x.pos[1].serial, j->x.pos[0].serial) ||
(bind8_compat &&
j->x.pos[1].serial == j->x.pos[0].serial)))
{
UNEXPECTED_ERROR(__FILE__, __LINE__,
"malformed transaction: serial number "
"would decrease");
return (ISC_R_UNEXPECTED);
}
if (! JOURNAL_EMPTY(&j->header)) {
if (j->x.pos[0].serial != j->header.end.serial) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"malformed transaction: "
"%s last serial %u != "
"transaction first serial %u",
j->filename,
j->header.end.serial,
j->x.pos[0].serial);
return (DNS_R_UNEXPECTED);
}
}
/*
* Some old journal entries may become non-addressable
* when we increment the current serial number. Purge them
* by stepping header.begin forward to the first addressable
* transaction. Also purge them from the index.
*/
if (! JOURNAL_EMPTY(&j->header)) {
while (! DNS_SERIAL_GT(j->x.pos[1].serial,
j->header.begin.serial)) {
CHECK(journal_next(j, &j->header.begin));
}
index_invalidate(j, j->x.pos[1].serial);
}
#ifdef notyet
if (DNS_SERIAL_GT(last_dumped_serial, j->x.pos[1].serial)) {
force_dump(...);
}
#endif
/* Commit the transaction data to stable storage. */
CHECK(journal_fsync(j));
/* Update the transaction header. */
CHECK(journal_seek(j, j->x.pos[0].offset));
CHECK(journal_write_xhdr(j, (j->x.pos[1].offset - j->x.pos[0].offset) -
sizeof(journal_rawxhdr_t),
j->x.pos[0].serial, j->x.pos[1].serial));
/* Update the journal header. */
if (JOURNAL_EMPTY(&j->header)) {
j->header.begin = j->x.pos[0];
}
j->header.end = j->x.pos[1];
journal_header_encode(&j->header, &rawheader);
CHECK(journal_seek(j, 0));
CHECK(journal_write(j, &rawheader, sizeof(rawheader)));
/* Update the index. Byte swap in-place if necessary. */
index_add(j, &j->x.pos[0]);
if (j->header.index_size != 0) {
unsigned int i;
for (i = 0; i < j->header.index_size; i++) {
j->index[i].serial = ntohl(j->index[i].serial);
j->index[i].offset = ntohl(j->index[i].offset);
}
CHECK(journal_write(j, j->index, j->header.index_size *
sizeof(journal_rawpos_t)));
}
/* Commit the header to stable storage. */
CHECK(journal_fsync(j));
/* Undo the byte swapping. */
for (i = 0; i < j->header.index_size; i++) {
j->index[i].serial = ntohl(j->index[i].serial);
j->index[i].offset = ntohl(j->index[i].offset);
}
/* We no longer have a transaction open. */
j->state = JOURNAL_STATE_WRITE;
result = DNS_R_SUCCESS;
failure:
return (result);
}
dns_result_t
dns_journal_write_transaction(dns_journal_t *j, dns_diff_t *diff) {
dns_result_t result;
CHECK(dns_diff_sort(diff, ixfr_order));
CHECK(dns_journal_begin_transaction(j));
CHECK(dns_journal_writediff(j, diff));
CHECK(dns_journal_commit(j));
result = DNS_R_SUCCESS;
failure:
return (result);
}
void
dns_journal_destroy(dns_journal_t **journalp) {
dns_journal_t *j = *journalp;
REQUIRE(DNS_JOURNAL_VALID(j));
j->it.result = ISC_R_FAILURE;
dns_name_invalidate(&j->it.name);
dns_decompress_invalidate(&j->it.dctx);
if (j->index != NULL)
isc_mem_put(j->mctx, j->index, j->header.index_size *
sizeof(journal_rawpos_t));
if (j->it.target.base != NULL)
isc_mem_put(j->mctx, j->it.target.base, j->it.target.length);
if (j->it.source.base != NULL)
isc_mem_put(j->mctx, j->it.source.base, j->it.source.length);
if (j->fp != NULL)
(void) fclose(j->fp);
j->magic = 0;
isc_mem_put(j->mctx, j, sizeof(*j));
*journalp = NULL;
}
/*
* Roll the open journal 'j' into the database 'db'.
* A new database version will be created.
*/
/* XXX Share code with incoming IXFR? */
static dns_result_t
roll_forward(dns_journal_t *j, dns_db_t *db) {
isc_buffer_t source; /* Transaction data from disk */
isc_buffer_t target; /* Ditto after _fromwire check */
isc_uint32_t db_serial; /* Database SOA serial */
isc_uint32_t end_serial; /* Last journal SOA serial */
dns_result_t result;
dns_dbversion_t *ver = NULL;
journal_pos_t pos;
dns_diff_t diff;
unsigned int n_soa = 0;
unsigned int n_put = 0;
REQUIRE(DNS_JOURNAL_VALID(j));
REQUIRE(DNS_DB_VALID(db));
dns_diff_init(j->mctx, &diff);
/*
* Set up empty initial buffers for uncheched and checked
* wire format transaction data. They will be reallocated
* later.
*/
isc_buffer_init(&source, NULL, 0, ISC_BUFFERTYPE_BINARY);
isc_buffer_init(&target, NULL, 0, ISC_BUFFERTYPE_BINARY);
/* Create the new database version. */
CHECK(dns_db_newversion(db, &ver));
/* Get the current database SOA serial number. */
CHECK(dns_db_getsoaserial(db, ver, &db_serial));
/* Locate a journal entry for the current database serial. */
CHECK(journal_find(j, db_serial, &pos));
/*
* XXX do more drastic things, like marking zone stale,
* if this fails?
*/
/*
* XXXRTH The zone code should probably mark the zone as bad and
* scream loudly into the log if this is a dynamic update
* log reply that failed.
*/
end_serial = dns_journal_last_serial(j);
if (db_serial == end_serial)
CHECK(DNS_R_UPTODATE);
CHECK(dns_journal_iter_init(j, db_serial, end_serial));
for (result = dns_journal_first_rr(j);
result == DNS_R_SUCCESS;
result = dns_journal_next_rr(j))
{
dns_name_t *name;
isc_uint32_t ttl;
dns_rdata_t *rdata;
dns_difftuple_t *tuple = NULL;
name = NULL;
rdata = NULL;
dns_journal_current_rr(j, &name, &ttl, &rdata);
if (rdata->type == dns_rdatatype_soa)
n_soa++;
if (n_soa == 3)
n_soa = 1;
if (n_soa == 0) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"journal corrupt: missing "
"initial SOA");
FAIL (DNS_R_UNEXPECTED);
}
CHECK(dns_difftuple_create(diff.mctx, n_soa == 1 ?
DNS_DIFFOP_DEL : DNS_DIFFOP_ADD,
name, ttl, rdata, &tuple));
dns_diff_append(&diff, &tuple);
if (++n_put > 100) {
isc_log_write(JOURNAL_DEBUG_LOGARGS(3),
"applying diff to database");
dns_diff_print(&diff, NULL);
CHECK(dns_diff_apply(&diff, db, ver));
dns_diff_clear(&diff);
n_put = 0;
}
}
if (result == DNS_R_NOMORE)
result = DNS_R_SUCCESS;
CHECK(result);
if (n_put != 0) {
isc_log_write(JOURNAL_DEBUG_LOGARGS(3),
"applying final diff to database");
dns_diff_print(&diff, NULL);
CHECK(dns_diff_apply(&diff, db, ver));
dns_diff_clear(&diff);
}
failure:
if (ver != NULL)
dns_db_closeversion(db, &ver, result == DNS_R_SUCCESS ?
ISC_TRUE : ISC_FALSE);
if (source.base != NULL)
isc_mem_put(j->mctx, source.base, source.length);
if (target.base != NULL)
isc_mem_put(j->mctx, target.base, target.length);
dns_diff_clear(&diff);
return (result);
}
dns_result_t
dns_journal_rollforward(isc_mem_t *mctx, dns_db_t *db, const char *filename) {
dns_journal_t *j;
dns_result_t result;
REQUIRE(DNS_DB_VALID(db));
REQUIRE(filename != NULL);
j = NULL;
result = dns_journal_open(mctx, filename, ISC_FALSE, &j);
if (result == DNS_R_NOTFOUND) {
isc_log_write(JOURNAL_DEBUG_LOGARGS(3),
"no journal file, but that's OK");
return (DNS_R_SUCCESS);
}
if (result != DNS_R_SUCCESS)
return (result);
result = roll_forward(j, db);
dns_journal_destroy(&j);
return (result);
}
dns_result_t
dns_journal_print(isc_mem_t *mctx, const char *filename, FILE *file) {
dns_journal_t *j;
isc_buffer_t source; /* Transaction data from disk */
isc_buffer_t target; /* Ditto after _fromwire check */
isc_uint32_t start_serial; /* Database SOA serial */
isc_uint32_t end_serial; /* Last journal SOA serial */
dns_result_t result;
dns_diff_t diff;
unsigned int n_soa = 0;
unsigned int n_put = 0;
REQUIRE(filename != NULL);
j = NULL;
result = dns_journal_open(mctx, filename, ISC_FALSE, &j);
if (result == DNS_R_NOTFOUND) {
isc_log_write(JOURNAL_DEBUG_LOGARGS(3), "no journal file");
return (DNS_R_NOTFOUND);
}
if (result != DNS_R_SUCCESS) {
isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
"journal open failure");
return (result);
}
dns_diff_init(j->mctx, &diff);
/*
* Set up empty initial buffers for uncheched and checked
* wire format transaction data. They will be reallocated
* later.
*/
isc_buffer_init(&source, NULL, 0, ISC_BUFFERTYPE_BINARY);
isc_buffer_init(&target, NULL, 0, ISC_BUFFERTYPE_BINARY);
start_serial = dns_journal_first_serial(j);
end_serial = dns_journal_last_serial(j);
CHECK(dns_journal_iter_init(j, start_serial, end_serial));
for (result = dns_journal_first_rr(j);
result == DNS_R_SUCCESS;
result = dns_journal_next_rr(j))
{
dns_name_t *name;
isc_uint32_t ttl;
dns_rdata_t *rdata;
dns_difftuple_t *tuple = NULL;
name = NULL;
rdata = NULL;
dns_journal_current_rr(j, &name, &ttl, &rdata);
if (rdata->type == dns_rdatatype_soa)
n_soa++;
if (n_soa == 3)
n_soa = 1;
if (n_soa == 0) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"journal corrupt: missing "
"initial SOA");
FAIL (DNS_R_UNEXPECTED);
}
CHECK(dns_difftuple_create(diff.mctx, n_soa == 1 ?
DNS_DIFFOP_DEL : DNS_DIFFOP_ADD,
name, ttl, rdata, &tuple));
dns_diff_append(&diff, &tuple);
if (++n_put > 100) {
result = dns_diff_print(&diff, file);
dns_diff_clear(&diff);
n_put = 0;
if (result != DNS_R_SUCCESS)
break;
}
}
if (result == DNS_R_NOMORE)
result = DNS_R_SUCCESS;
CHECK(result);
if (n_put != 0) {
result = dns_diff_print(&diff, file);
dns_diff_clear(&diff);
}
goto cleanup;
failure:
isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
"journal corrupt");
cleanup:
if (source.base != NULL)
isc_mem_put(j->mctx, source.base, source.length);
if (target.base != NULL)
isc_mem_put(j->mctx, target.base, target.length);
dns_diff_clear(&diff);
dns_journal_destroy(&j);
return (result);
}
/**************************************************************************/
/*
* Miscellaneous accessors.
*/
isc_uint32_t dns_journal_first_serial(dns_journal_t *j) {
return (j->header.begin.serial);
}
isc_uint32_t dns_journal_last_serial(dns_journal_t *j) {
return (j->header.end.serial);
}
/**************************************************************************/
/*
* Iteration support.
*
* When serving an outgoing IXFR, we transmit a part the journal starting
* at the serial number in the IXFR request and ending at the serial
* number that is current when the IXFR request arrives. The ending
* serial number is not necessarily at the end of the journal:
* the journal may grow while the IXFR is in progress, but we stop
* when we reach the serial number that was current when the IXFR started.
*/
static dns_result_t read_one_rr(dns_journal_t *j);
/*
* Make sure the buffer 'b' is has at least 'size' bytes
* allocated, and clear it.
*
* Requires:
* Either b->base is NULL, or it points to b->length bytes of memory
* previously allocated by isc_mem_get().
*/
static dns_result_t
size_buffer(isc_mem_t *mctx, isc_buffer_t *b, unsigned size) {
if (b->length < size) {
void *mem = isc_mem_get(mctx, size);
if (mem == NULL)
return (DNS_R_NOMEMORY);
if (b->base != NULL)
isc_mem_put(mctx, b->base, b->length);
b->base = mem;
b->length = size;
}
isc_buffer_clear(b);
return (DNS_R_SUCCESS);
}
dns_result_t
dns_journal_iter_init(dns_journal_t *j,
isc_uint32_t begin_serial, isc_uint32_t end_serial)
{
dns_result_t result;
CHECK(journal_find(j, begin_serial, &j->it.bpos));
INSIST(j->it.bpos.serial == begin_serial);
CHECK(journal_find(j, end_serial, &j->it.epos));
INSIST(j->it.epos.serial == end_serial);
result = DNS_R_SUCCESS;
failure:
j->it.result = result;
return (j->it.result);
}
dns_result_t
dns_journal_first_rr(dns_journal_t *j)
{
dns_result_t result;
/*
* Seek to the beginning of the first transaction we are
* interested in.
*/
CHECK(journal_seek(j, j->it.bpos.offset));
j->it.current_serial = j->it.bpos.serial;
j->it.xsize = 0; /* We have no transaction data yet... */
j->it.xpos = 0; /* ...and haven't used any of it. */
return (read_one_rr(j));
failure:
return (result);
}
static dns_result_t
read_one_rr(dns_journal_t *j) {
dns_result_t result;
dns_rdatatype_t rdtype;
dns_rdataclass_t rdclass;
unsigned int rdlen;
isc_uint32_t ttl;
journal_xhdr_t xhdr;
journal_rrhdr_t rrhdr;
isc_region_t r;
/*
* XXXRTH Need to resolve the comparison between int and unsigned
* int here, but we can defer this until we decide what type
* we want j->offset to be.
*/
INSIST(j->offset <= j->it.epos.offset);
if (j->offset == j->it.epos.offset)
return (DNS_R_NOMORE);
if (j->it.xpos == j->it.xsize) {
/*
* We are at a transaction boundary.
* Read another transaction header.
*/
CHECK(journal_read_xhdr(j, &xhdr));
if (xhdr.size == 0) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"journal corrupt: empty transaction");
FAIL(DNS_R_UNEXPECTED);
}
if (xhdr.serial0 != j->it.current_serial) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"%s: journal corrupt: "
"expected serial %u, got %u",
j->filename,
j->it.current_serial, xhdr.serial0);
FAIL(DNS_R_UNEXPECTED);
}
j->it.xsize = xhdr.size;
j->it.xpos = 0;
}
/* Read an RR. */
result = journal_read_rrhdr(j, &rrhdr);
/*
* Perform a sanity check on the journal RR size.
* The smallest possible RR has a 1-byte owner name
* and a 10-byte header. The largest possible
* RR has 65535 bytes of data, a header, and a maximum-
* size owner name, well below 70 k total.
*/
if (rrhdr.size < 1+10 || rrhdr.size > 70000) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"journal corrupt: impossible RR size "
"(%d bytes)", rrhdr.size);
FAIL(DNS_R_UNEXPECTED);
}
CHECK(size_buffer(j->mctx, &j->it.source, rrhdr.size));
CHECK(journal_read(j, j->it.source.base, rrhdr.size));
isc_buffer_add(&j->it.source, rrhdr.size);
/*
* The target buffer is made the same size
* as the source buffer, with the assumption that when
* no compression in present, the output of dns_*_fromwire()
* is no larger than the input.
*/
CHECK(size_buffer(j->mctx, &j->it.target, rrhdr.size));
/*
* Parse the owner name. We don't know where it
* ends yet, so we make the entire "remaining"
* part of the buffer "active".
*/
isc_buffer_setactive(&j->it.source,
j->it.source.used - j->it.source.current);
CHECK(dns_name_fromwire(&j->it.name, &j->it.source,
&j->it.dctx, ISC_FALSE, &j->it.target));
/* Check that the RR header is there, and parse it. */
isc_buffer_remaining(&j->it.source, &r);
if (r.length < 10)
FAIL(DNS_R_FORMERR);
rdtype = isc_buffer_getuint16(&j->it.source);
rdclass = isc_buffer_getuint16(&j->it.source);
ttl = isc_buffer_getuint32(&j->it.source);
rdlen = isc_buffer_getuint16(&j->it.source);
/* Parse the rdata. */
isc_buffer_setactive(&j->it.source, rdlen);
CHECK(dns_rdata_fromwire(&j->it.rdata, rdclass,
rdtype, &j->it.source, &j->it.dctx,
ISC_FALSE, &j->it.target));
j->it.ttl = ttl;
j->it.xpos += sizeof(journal_rawrrhdr_t) + rrhdr.size;
if (rdtype == dns_rdatatype_soa) {
/* XXX could do additional consistency checks here */
j->it.current_serial = dns_soa_getserial(&j->it.rdata);
}
result = DNS_R_SUCCESS;
failure:
j->it.result = result;
return (result);
}
dns_result_t
dns_journal_next_rr(dns_journal_t *j) {
j->it.result = read_one_rr(j);
return (j->it.result);
}
void
dns_journal_current_rr(dns_journal_t *j, dns_name_t **name, isc_uint32_t *ttl,
dns_rdata_t **rdata)
{
REQUIRE(j->it.result == DNS_R_SUCCESS);
*name = &j->it.name;
*ttl = j->it.ttl;
*rdata = &j->it.rdata;
}
/**************************************************************************/
/*
* Generating diffs from databases
*/
/*
* Construct a diff containing all the RRs at the current name of the
* database iterator 'dbit' in database 'db', version 'ver'.
* Set '*name' to the current name, and append the diff to 'diff'.
* All new tuples will have the operation 'op'.
*
* Requires: 'name' must have buffer large enough to hold the name.
* Typically, a dns_fixedname_t would be used.
*/
static dns_result_t
get_name_diff(dns_db_t *db, dns_dbversion_t *ver, isc_stdtime_t now,
dns_dbiterator_t *dbit, dns_name_t *name, dns_diffop_t op,
dns_diff_t *diff)
{
dns_result_t result;
dns_dbnode_t *node = NULL;
dns_rdatasetiter_t *rdsiter = NULL;
dns_difftuple_t *tuple = NULL;
result = dns_dbiterator_current(dbit, &node, name);
if (result != DNS_R_SUCCESS)
return (result);
result = dns_db_allrdatasets(db, node, ver, now, &rdsiter);
if (result != DNS_R_SUCCESS)
goto cleanup_node;
for (result = dns_rdatasetiter_first(rdsiter);
result == DNS_R_SUCCESS;
result = dns_rdatasetiter_next(rdsiter))
{
dns_rdataset_t rdataset;
dns_rdataset_init(&rdataset);
dns_rdatasetiter_current(rdsiter, &rdataset);
for (result = dns_rdataset_first(&rdataset);
result == DNS_R_SUCCESS;
result = dns_rdataset_next(&rdataset))
{
dns_rdata_t rdata;
dns_rdataset_current(&rdataset, &rdata);
result = dns_difftuple_create(diff->mctx, op, name,
rdataset.ttl, &rdata,
&tuple);
if (result != DNS_R_SUCCESS) {
dns_rdataset_disassociate(&rdataset);
goto cleanup_iterator;
}
dns_diff_append(diff, &tuple);
}
dns_rdataset_disassociate(&rdataset);
if (result != DNS_R_NOMORE)
goto cleanup_iterator;
}
if (result != DNS_R_NOMORE)
goto cleanup_iterator;
result = DNS_R_SUCCESS;
cleanup_iterator:
dns_rdatasetiter_destroy(&rdsiter);
cleanup_node:
dns_db_detachnode(db, &node);
return (result);
}
/*
* Comparison function for use by dns_diff_subtract when sorting
* the diffs to be subtracted. The sort keys are the rdata type
* and the rdata itself. The owner name is ignored, because
* it is known to be the same for all tuples.
*/
static int
rdata_order(const void *av, const void *bv)
{
dns_difftuple_t * const *ap = av;
dns_difftuple_t * const *bp = bv;
dns_difftuple_t *a = *ap;
dns_difftuple_t *b = *bp;
int r;
r = (b->rdata.type - a->rdata.type);
if (r != 0)
return (r);
r = dns_rdata_compare(&a->rdata, &b->rdata);
return (r);
}
static dns_result_t
dns_diff_subtract(dns_diff_t diff[2], dns_diff_t *r)
{
dns_result_t result;
dns_difftuple_t *p[2];
int i, t;
CHECK(dns_diff_sort(&diff[0], rdata_order));
CHECK(dns_diff_sort(&diff[1], rdata_order));
for (;;) {
p[0] = ISC_LIST_HEAD(diff[0].tuples);
p[1] = ISC_LIST_HEAD(diff[1].tuples);
if (p[0] == NULL && p[1] == NULL)
break;
for (i = 0; i < 2; i++)
if (p[!i] == NULL) {
ISC_LIST_UNLINK(diff[i].tuples, p[i], link);
ISC_LIST_APPEND(r->tuples, p[i], link);
goto next;
}
t = rdata_order(&p[0], &p[1]);
if (t < 0) {
ISC_LIST_UNLINK(diff[0].tuples, p[0], link);
ISC_LIST_APPEND(r->tuples, p[0], link);
goto next;
}
if (t > 0) {
ISC_LIST_UNLINK(diff[1].tuples, p[1], link);
ISC_LIST_APPEND(r->tuples, p[1], link);
goto next;
}
INSIST(t == 0);
/* Identical RRs in both databases; skip them both. */
for (i = 0; i < 2; i++) {
ISC_LIST_UNLINK(diff[i].tuples, p[i], link);
dns_difftuple_free(&p[i]);
}
next: ;
}
result = ISC_R_SUCCESS;
failure:
return (result);
}
/*
* Compare the databases 'dba' and 'dbb' and generate a journal
* entry containing the changes to make 'dba' from 'dbb' (note
* the order). This journal entry will consist of a single,
* possibly very large transaction.
*/
dns_result_t
dns_db_diff(isc_mem_t *mctx,
dns_db_t *dba, dns_dbversion_t *dbvera,
dns_db_t *dbb, dns_dbversion_t *dbverb,
const char *journal_filename)
{
dns_db_t *db[2];
dns_dbversion_t *ver[2];
dns_dbiterator_t *dbit[2] = { NULL, NULL };
isc_boolean_t have[2] = { ISC_FALSE, ISC_FALSE };
dns_fixedname_t fixname[2];
dns_result_t result, itresult[2];
dns_diff_t diff[2], resultdiff;
int i, t;
dns_journal_t *journal = NULL;
db[0] = dba, db[1] = dbb;
ver[0] = dbvera, ver[1] = dbverb;
dns_diff_init(mctx, &diff[0]);
dns_diff_init(mctx, &diff[1]);
dns_diff_init(mctx, &resultdiff);
dns_fixedname_init(&fixname[0]);
dns_fixedname_init(&fixname[1]);
CHECK(dns_journal_open(mctx, journal_filename, ISC_TRUE, &journal));
CHECK(dns_db_createiterator(db[0], ISC_FALSE, &dbit[0]));
CHECK(dns_db_createiterator(db[1], ISC_FALSE, &dbit[1]));
itresult[0] = dns_dbiterator_first(dbit[0]);
itresult[1] = dns_dbiterator_first(dbit[1]);
for (;;) {
for (i = 0; i < 2; i++) {
if (! have[i] && itresult[i] == DNS_R_SUCCESS) {
CHECK(get_name_diff(db[i], ver[i], 0, dbit[i],
dns_fixedname_name(&fixname[i]),
i == 0 ?
DNS_DIFFOP_ADD :
DNS_DIFFOP_DEL,
&diff[i]));
itresult[i] = dns_dbiterator_next(dbit[i]);
have[i] = ISC_TRUE;
}
}
if (! have[0] && ! have[1]) {
INSIST(ISC_LIST_EMPTY(diff[0].tuples));
INSIST(ISC_LIST_EMPTY(diff[1].tuples));
break;
}
for (i = 0; i < 2; i++) {
if (! have[!i]) {
ISC_LIST_APPENDLIST(resultdiff.tuples,
diff[i].tuples, link);
INSIST(ISC_LIST_EMPTY(diff[i].tuples));
have[i] = ISC_FALSE;
goto next;
}
}
t = dns_name_compare(dns_fixedname_name(&fixname[0]),
dns_fixedname_name(&fixname[1]));
if (t < 0) {
ISC_LIST_APPENDLIST(resultdiff.tuples,
diff[0].tuples, link);
INSIST(ISC_LIST_EMPTY(diff[0].tuples));
have[0] = ISC_FALSE;
continue;
}
if (t > 0) {
ISC_LIST_APPENDLIST(resultdiff.tuples,
diff[1].tuples, link);
INSIST(ISC_LIST_EMPTY(diff[1].tuples));
have[1] = ISC_FALSE;
continue;
}
INSIST(t == 0);
CHECK(dns_diff_subtract(diff, &resultdiff));
INSIST(ISC_LIST_EMPTY(diff[0].tuples));
INSIST(ISC_LIST_EMPTY(diff[1].tuples));
have[0] = have[1] = ISC_FALSE;
next: ;
}
if (itresult[0] != DNS_R_NOMORE)
FAIL(itresult[0]);
if (itresult[1] != DNS_R_NOMORE)
FAIL(itresult[1]);
if (ISC_LIST_EMPTY(resultdiff.tuples)) {
isc_log_write(JOURNAL_DEBUG_LOGARGS(3), "no changes");
} else {
CHECK(dns_journal_write_transaction(journal, &resultdiff));
}
INSIST(ISC_LIST_EMPTY(diff[0].tuples));
INSIST(ISC_LIST_EMPTY(diff[1].tuples));
dns_diff_clear(&resultdiff);
failure:
dns_dbiterator_destroy(&dbit[0]);
dns_dbiterator_destroy(&dbit[1]);
dns_journal_destroy(&journal);
return (result);
}