rdataslab.c revision 1bbd36c4db2c86762b582db4b0504e6e68a183f6
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington/*
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * Copyright (C) 2004-2012 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Copyright (C) 1999-2003 Internet Software Consortium.
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington *
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * Permission to use, copy, modify, and/or distribute this software for any
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * purpose with or without fee is hereby granted, provided that the above
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * copyright notice and this permission notice appear in all copies.
15a44745412679c30a6d022733925af70a38b715David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
15a44745412679c30a6d022733925af70a38b715David Lawrence * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15a44745412679c30a6d022733925af70a38b715David Lawrence * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15a44745412679c30a6d022733925af70a38b715David Lawrence * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15a44745412679c30a6d022733925af70a38b715David Lawrence * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15a44745412679c30a6d022733925af70a38b715David Lawrence * PERFORMANCE OF THIS SOFTWARE.
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington */
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
50980039820700108f1f5eef1e42aa998a44f087Brian Wellington/* $Id$ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington/*! \file */
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#include <config.h>
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#include <stdlib.h>
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#include <isc/mem.h>
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff#include <isc/region.h>
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#include <isc/string.h> /* Required for HP/UX (and others?) */
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#include <isc/util.h>
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#include <dns/result.h>
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff#include <dns/rdata.h>
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#include <dns/rdataset.h>
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#include <dns/rdataslab.h>
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff/*
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * The rdataslab structure allows iteration to occur in both load order
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * and DNSSEC order. The structure is as follows:
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff *
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * header (reservelen bytes)
9a6bbb206efa528373c31928ebd0b7216c747a13Brian Wellington * record count (2 bytes)
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * offset table (4 x record count bytes in load order)
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * data records
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * data length (2 bytes)
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * order (2 bytes)
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * meta data (1 byte for RRSIG's)
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * data (data length bytes)
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington *
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * If DNS_RDATASET_FIXED is defined to be zero (0) the format of a
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * rdataslab is as follows:
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff *
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * header (reservelen bytes)
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * record count (2 bytes)
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * data records
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * data length (2 bytes)
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * meta data (1 byte for RRSIG's)
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * data (data length bytes)
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff *
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * Offsets are from the end of the header.
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff *
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * Load order traversal is performed by walking the offset table to find
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * the start of the record (DNS_RDATASET_FIXED = 1).
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff *
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * DNSSEC order traversal is performed by walking the data records.
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff *
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * The order is stored with record to allow for efficient reconstruction
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * of the offset table following a merge or subtraction.
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff *
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * The iterator methods here currently only support DNSSEC order iteration.
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff *
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * The iterator methods in rbtdb support both load order and DNSSEC order
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * iteration.
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff *
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * WARNING:
8a23742754e4640a298acb0d6bd7ed4da0c11798Brian Wellington * rbtdb.c directly interacts with the slab's raw structures. If the
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * structure changes then rbtdb.c also needs to be updated to reflect
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * the changes. See the areas tagged with "RDATASLAB".
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff */
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graffstruct xrdata {
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff dns_rdata_t rdata;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff unsigned int order;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff};
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff/*% Note: the "const void *" are just to make qsort happy. */
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graffstatic int
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graffcompare_rdata(const void *p1, const void *p2) {
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff const struct xrdata *x1 = p1;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff const struct xrdata *x2 = p2;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff return (dns_rdata_compare(&x1->rdata, &x2->rdata));
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff}
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff#if DNS_RDATASET_FIXED
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graffstatic void
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Grafffillin_offsets(unsigned char *offsetbase, unsigned int *offsettable,
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff unsigned length)
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff{
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff unsigned int i, j;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff unsigned char *raw;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff for (i = 0, j = 0; i < length; i++) {
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff if (offsettable[i] == 0)
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff continue;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff /*
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * Fill in offset table.
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff */
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff raw = &offsetbase[j*4 + 2];
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff *raw++ = (offsettable[i] & 0xff000000) >> 24;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff *raw++ = (offsettable[i] & 0xff0000) >> 16;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff *raw++ = (offsettable[i] & 0xff00) >> 8;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff *raw = offsettable[i] & 0xff;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff /*
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * Fill in table index.
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff */
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff raw = offsetbase + offsettable[i] + 2;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff *raw++ = (j & 0xff00) >> 8;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff *raw = j++ & 0xff;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff }
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff}
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff#endif
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graffisc_result_t
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graffdns_rdataslab_fromrdataset(dns_rdataset_t *rdataset, isc_mem_t *mctx,
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff isc_region_t *region, unsigned int reservelen)
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff{
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff /*
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * Use &removed as a sentinal pointer for duplicate
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * rdata as rdata.data == NULL is valid.
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington */
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff static unsigned char removed;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff struct xrdata *x;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff unsigned char *rawbuf;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff#if DNS_RDATASET_FIXED
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff unsigned char *offsetbase;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#endif
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington unsigned int buflen;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff isc_result_t result;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff unsigned int nitems;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff unsigned int nalloc;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff unsigned int i;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff#if DNS_RDATASET_FIXED
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff unsigned int *offsettable;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff#endif
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff unsigned int length;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff buflen = reservelen + 2;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff nalloc = dns_rdataset_count(rdataset);
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff nitems = nalloc;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff if (nitems == 0 && rdataset->type != 0)
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff return (ISC_R_FAILURE);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington if (nalloc > 0xffff)
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington return (ISC_R_NOSPACE);
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington if (nalloc != 0) {
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff x = isc_mem_get(mctx, nalloc * sizeof(struct xrdata));
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff if (x == NULL)
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff return (ISC_R_NOMEMORY);
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff } else
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff x = NULL;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff /*
8a23742754e4640a298acb0d6bd7ed4da0c11798Brian Wellington * Save all of the rdata members into an array.
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington */
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington result = dns_rdataset_first(rdataset);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington if (result != ISC_R_SUCCESS && result != ISC_R_NOMORE)
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington goto free_rdatas;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington for (i = 0; i < nalloc && result == ISC_R_SUCCESS; i++) {
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington INSIST(result == ISC_R_SUCCESS);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington dns_rdata_init(&x[i].rdata);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington dns_rdataset_current(rdataset, &x[i].rdata);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington INSIST(x[i].rdata.data != &removed);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#if DNS_RDATASET_FIXED
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington x[i].order = i;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#endif
9a6bbb206efa528373c31928ebd0b7216c747a13Brian Wellington result = dns_rdataset_next(rdataset);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington }
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington if (result != ISC_R_NOMORE)
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington goto free_rdatas;
9a6bbb206efa528373c31928ebd0b7216c747a13Brian Wellington if (i != nalloc) {
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington /*
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * Somehow we iterated over fewer rdatas than
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * dns_rdataset_count() said there were!
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington */
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington result = ISC_R_FAILURE;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington goto free_rdatas;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington }
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington /*
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * Put into DNSSEC order.
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington */
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington qsort(x, nalloc, sizeof(struct xrdata), compare_rdata);
9a6bbb206efa528373c31928ebd0b7216c747a13Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington /*
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * Remove duplicates and compute the total storage required.
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington *
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * If an rdata is not a duplicate, accumulate the storage size
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * required for the rdata. We do not store the class, type, etc,
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * just the rdata, so our overhead is 2 bytes for the number of
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * records, and 8 for each rdata, (length(2), offset(4) and order(2))
9a6bbb206efa528373c31928ebd0b7216c747a13Brian Wellington * and then the rdata itself.
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington */
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington for (i = 1; i < nalloc; i++) {
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington if (compare_rdata(&x[i-1].rdata, &x[i].rdata) == 0) {
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington x[i-1].rdata.data = &removed;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#if DNS_RDATASET_FIXED
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington /*
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * Preserve the least order so A, B, A -> A, B
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * after duplicate removal.
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington */
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington if (x[i-1].order < x[i].order)
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington x[i].order = x[i-1].order;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#endif
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington nitems--;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington } else {
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#if DNS_RDATASET_FIXED
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington buflen += (8 + x[i-1].rdata.length);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#else
9a6bbb206efa528373c31928ebd0b7216c747a13Brian Wellington buflen += (2 + x[i-1].rdata.length);
9a6bbb206efa528373c31928ebd0b7216c747a13Brian Wellington#endif
9a6bbb206efa528373c31928ebd0b7216c747a13Brian Wellington /*
9a6bbb206efa528373c31928ebd0b7216c747a13Brian Wellington * Provide space to store the per RR meta data.
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington */
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington if (rdataset->type == dns_rdatatype_rrsig)
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington buflen++;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington }
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington }
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington /*
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * Don't forget the last item!
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington */
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington if (nalloc != 0) {
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#if DNS_RDATASET_FIXED
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington buflen += (8 + x[i-1].rdata.length);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#else
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington buflen += (2 + x[i-1].rdata.length);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#endif
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington }
50980039820700108f1f5eef1e42aa998a44f087Brian Wellington
50980039820700108f1f5eef1e42aa998a44f087Brian Wellington /*
50980039820700108f1f5eef1e42aa998a44f087Brian Wellington * Provide space to store the per RR meta data.
50980039820700108f1f5eef1e42aa998a44f087Brian Wellington */
50980039820700108f1f5eef1e42aa998a44f087Brian Wellington if (rdataset->type == dns_rdatatype_rrsig)
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington buflen++;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington /*
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * Ensure that singleton types are actually singletons.
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington */
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington if (nitems > 1 && dns_rdatatype_issingleton(rdataset->type)) {
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington /*
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * We have a singleton type, but there's more than one
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * RR in the rdataset.
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington */
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington result = DNS_R_SINGLETON;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington goto free_rdatas;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington }
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington /*
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * Allocate the memory, set up a buffer, start copying in
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * data.
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff */
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff rawbuf = isc_mem_get(mctx, buflen);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington if (rawbuf == NULL) {
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff result = ISC_R_NOMEMORY;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington goto free_rdatas;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington }
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#if DNS_RDATASET_FIXED
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington /* Allocate temporary offset table. */
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington offsettable = isc_mem_get(mctx, nalloc * sizeof(unsigned int));
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington if (offsettable == NULL) {
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington isc_mem_put(mctx, rawbuf, buflen);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington result = ISC_R_NOMEMORY;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington goto free_rdatas;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington }
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington memset(offsettable, 0, nalloc * sizeof(unsigned int));
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#endif
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington region->base = rawbuf;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington region->length = buflen;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington rawbuf += reservelen;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#if DNS_RDATASET_FIXED
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington offsetbase = rawbuf;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#endif
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington *rawbuf++ = (nitems & 0xff00) >> 8;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington *rawbuf++ = (nitems & 0x00ff);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#if DNS_RDATASET_FIXED
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington /* Skip load order table. Filled in later. */
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington rawbuf += nitems * 4;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#endif
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington for (i = 0; i < nalloc; i++) {
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington if (x[i].rdata.data == &removed)
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington continue;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#if DNS_RDATASET_FIXED
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington offsettable[x[i].order] = rawbuf - offsetbase;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#endif
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington length = x[i].rdata.length;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington if (rdataset->type == dns_rdatatype_rrsig)
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington length++;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington INSIST(length <= 0xffff);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington *rawbuf++ = (length & 0xff00) >> 8;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff *rawbuf++ = (length & 0x00ff);
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff#if DNS_RDATASET_FIXED
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington rawbuf += 2; /* filled in later */
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff#endif
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff /*
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff * Store the per RR meta data.
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington */
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff if (rdataset->type == dns_rdatatype_rrsig) {
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff *rawbuf++ |= (x[i].rdata.flags & DNS_RDATA_OFFLINE) ?
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington DNS_RDATASLAB_OFFLINE : 0;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff }
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff memcpy(rawbuf, x[i].rdata.data, x[i].rdata.length);
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff rawbuf += x[i].rdata.length;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff }
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff#if DNS_RDATASET_FIXED
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff fillin_offsets(offsetbase, offsettable, nalloc);
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff isc_mem_put(mctx, offsettable, nalloc * sizeof(unsigned int));
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff#endif
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff result = ISC_R_SUCCESS;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff free_rdatas:
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington if (x != NULL)
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington isc_mem_put(mctx, x, nalloc * sizeof(struct xrdata));
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington return (result);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington}
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellingtonstatic void
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellingtonrdataset_disassociate(dns_rdataset_t *rdataset) {
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington UNUSED(rdataset);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington}
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graffstatic isc_result_t
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graffrdataset_first(dns_rdataset_t *rdataset) {
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff unsigned char *raw = rdataset->private3;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff unsigned int count;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff count = raw[0] * 256 + raw[1];
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff if (count == 0) {
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff rdataset->private5 = NULL;
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff return (ISC_R_NOMORE);
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff }
4e96d1fc0646de4d879e7a0bc1e7aba449a67596Michael Graff#if DNS_RDATASET_FIXED
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington raw += 2 + (4 * count);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#else
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington raw += 2;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#endif
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington /*
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * The privateuint4 field is the number of rdata beyond the cursor
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * position, so we decrement the total count by one before storing
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * it.
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington */
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington count--;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington rdataset->privateuint4 = count;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington rdataset->private5 = raw;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington return (ISC_R_SUCCESS);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington}
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellingtonstatic isc_result_t
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellingtonrdataset_next(dns_rdataset_t *rdataset) {
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington unsigned int count;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington unsigned int length;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington unsigned char *raw;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington count = rdataset->privateuint4;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington if (count == 0)
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington return (ISC_R_NOMORE);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington count--;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington rdataset->privateuint4 = count;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington raw = rdataset->private5;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington length = raw[0] * 256 + raw[1];
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#if DNS_RDATASET_FIXED
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington raw += length + 4;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#else
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington raw += length + 2;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#endif
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington rdataset->private5 = raw;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington return (ISC_R_SUCCESS);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington}
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellingtonstatic void
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellingtonrdataset_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington unsigned char *raw = rdataset->private5;
9a6bbb206efa528373c31928ebd0b7216c747a13Brian Wellington isc_region_t r;
9a6bbb206efa528373c31928ebd0b7216c747a13Brian Wellington unsigned int length;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington unsigned int flags = 0;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington REQUIRE(raw != NULL);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington length = raw[0] * 256 + raw[1];
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#if DNS_RDATASET_FIXED
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington raw += 4;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#else
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington raw += 2;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington#endif
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington if (rdataset->type == dns_rdatatype_rrsig) {
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington if (*raw & DNS_RDATASLAB_OFFLINE)
50980039820700108f1f5eef1e42aa998a44f087Brian Wellington flags |= DNS_RDATA_OFFLINE;
50980039820700108f1f5eef1e42aa998a44f087Brian Wellington length--;
50980039820700108f1f5eef1e42aa998a44f087Brian Wellington raw++;
50980039820700108f1f5eef1e42aa998a44f087Brian Wellington }
50980039820700108f1f5eef1e42aa998a44f087Brian Wellington r.length = length;
50980039820700108f1f5eef1e42aa998a44f087Brian Wellington r.base = raw;
50980039820700108f1f5eef1e42aa998a44f087Brian Wellington dns_rdata_fromregion(rdata, rdataset->rdclass, rdataset->type, &r);
50980039820700108f1f5eef1e42aa998a44f087Brian Wellington rdata->flags |= flags;
50980039820700108f1f5eef1e42aa998a44f087Brian Wellington}
50980039820700108f1f5eef1e42aa998a44f087Brian Wellington
50980039820700108f1f5eef1e42aa998a44f087Brian Wellingtonstatic void
50980039820700108f1f5eef1e42aa998a44f087Brian Wellingtonrdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target) {
50980039820700108f1f5eef1e42aa998a44f087Brian Wellington *target = *source;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington /*
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington * Reset iterator state.
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington */
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington target->privateuint4 = 0;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington target->private5 = NULL;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington}
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellingtonstatic unsigned int
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellingtonrdataset_count(dns_rdataset_t *rdataset) {
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington unsigned char *raw = rdataset->private3;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington unsigned int count;
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington count = raw[0] * 256 + raw[1];
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington return (count);
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington}
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellingtonstatic dns_rdatasetmethods_t rdataset_methods = {
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington rdataset_disassociate,
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington rdataset_first,
50980039820700108f1f5eef1e42aa998a44f087Brian Wellington rdataset_next,
50980039820700108f1f5eef1e42aa998a44f087Brian Wellington rdataset_current,
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington rdataset_clone,
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington rdataset_count,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
void
dns_rdataslab_tordataset(unsigned char *slab, unsigned int reservelen,
dns_rdataclass_t rdclass, dns_rdatatype_t rdtype,
dns_rdatatype_t covers, dns_ttl_t ttl,
dns_rdataset_t *rdataset)
{
REQUIRE(slab != NULL);
REQUIRE(!dns_rdataset_isassociated(rdataset));
rdataset->methods = &rdataset_methods;
rdataset->rdclass = rdclass;
rdataset->type = rdtype;
rdataset->covers = covers;
rdataset->ttl = ttl;
rdataset->trust = 0;
rdataset->private1 = NULL;
rdataset->private2 = NULL;
rdataset->private3 = slab + reservelen;
/*
* Reset iterator state.
*/
rdataset->privateuint4 = 0;
rdataset->private5 = NULL;
}
unsigned int
dns_rdataslab_size(unsigned char *slab, unsigned int reservelen) {
unsigned int count, length;
unsigned char *current;
REQUIRE(slab != NULL);
current = slab + reservelen;
count = *current++ * 256;
count += *current++;
#if DNS_RDATASET_FIXED
current += (4 * count);
#endif
while (count > 0) {
count--;
length = *current++ * 256;
length += *current++;
#if DNS_RDATASET_FIXED
current += length + 2;
#else
current += length;
#endif
}
return ((unsigned int)(current - slab));
}
/*
* Make the dns_rdata_t 'rdata' refer to the slab item
* beginning at '*current', which is part of a slab of type
* 'type' and class 'rdclass', and advance '*current' to
* point to the next item in the slab.
*/
static inline void
rdata_from_slab(unsigned char **current,
dns_rdataclass_t rdclass, dns_rdatatype_t type,
dns_rdata_t *rdata)
{
unsigned char *tcurrent = *current;
isc_region_t region;
unsigned int length;
isc_boolean_t offline = ISC_FALSE;
length = *tcurrent++ * 256;
length += *tcurrent++;
if (type == dns_rdatatype_rrsig) {
if ((*tcurrent & DNS_RDATASLAB_OFFLINE) != 0)
offline = ISC_TRUE;
length--;
tcurrent++;
}
region.length = length;
#if DNS_RDATASET_FIXED
tcurrent += 2;
#endif
region.base = tcurrent;
tcurrent += region.length;
dns_rdata_fromregion(rdata, rdclass, type, &region);
if (offline)
rdata->flags |= DNS_RDATA_OFFLINE;
*current = tcurrent;
}
/*
* Return true iff 'slab' (slab data of type 'type' and class 'rdclass')
* contains an rdata identical to 'rdata'. This does case insensitive
* comparisons per DNSSEC.
*/
static inline isc_boolean_t
rdata_in_slab(unsigned char *slab, unsigned int reservelen,
dns_rdataclass_t rdclass, dns_rdatatype_t type,
dns_rdata_t *rdata)
{
unsigned int count, i;
unsigned char *current;
dns_rdata_t trdata = DNS_RDATA_INIT;
int n;
current = slab + reservelen;
count = *current++ * 256;
count += *current++;
#if DNS_RDATASET_FIXED
current += (4 * count);
#endif
for (i = 0; i < count; i++) {
rdata_from_slab(&current, rdclass, type, &trdata);
n = dns_rdata_compare(&trdata, rdata);
if (n == 0)
return (ISC_TRUE);
if (n > 0) /* In DNSSEC order. */
break;
dns_rdata_reset(&trdata);
}
return (ISC_FALSE);
}
isc_result_t
dns_rdataslab_merge(unsigned char *oslab, unsigned char *nslab,
unsigned int reservelen, isc_mem_t *mctx,
dns_rdataclass_t rdclass, dns_rdatatype_t type,
unsigned int flags, unsigned char **tslabp)
{
unsigned char *ocurrent, *ostart, *ncurrent, *tstart, *tcurrent, *data;
unsigned int ocount, ncount, count, olength, tlength, tcount, length;
dns_rdata_t ordata = DNS_RDATA_INIT;
dns_rdata_t nrdata = DNS_RDATA_INIT;
isc_boolean_t added_something = ISC_FALSE;
unsigned int oadded = 0;
unsigned int nadded = 0;
unsigned int nncount = 0;
#if DNS_RDATASET_FIXED
unsigned int oncount;
unsigned int norder = 0;
unsigned int oorder = 0;
unsigned char *offsetbase;
unsigned int *offsettable;
#endif
/*
* XXX Need parameter to allow "delete rdatasets in nslab" merge,
* or perhaps another merge routine for this purpose.
*/
REQUIRE(tslabp != NULL && *tslabp == NULL);
REQUIRE(oslab != NULL && nslab != NULL);
ocurrent = oslab + reservelen;
ocount = *ocurrent++ * 256;
ocount += *ocurrent++;
#if DNS_RDATASET_FIXED
ocurrent += (4 * ocount);
#endif
ostart = ocurrent;
ncurrent = nslab + reservelen;
ncount = *ncurrent++ * 256;
ncount += *ncurrent++;
#if DNS_RDATASET_FIXED
ncurrent += (4 * ncount);
#endif
INSIST(ocount > 0 && ncount > 0);
#if DNS_RDATASET_FIXED
oncount = ncount;
#endif
/*
* Yes, this is inefficient!
*/
/*
* Figure out the length of the old slab's data.
*/
olength = 0;
for (count = 0; count < ocount; count++) {
length = *ocurrent++ * 256;
length += *ocurrent++;
#if DNS_RDATASET_FIXED
olength += length + 8;
ocurrent += length + 2;
#else
olength += length + 2;
ocurrent += length;
#endif
}
/*
* Start figuring out the target length and count.
*/
tlength = reservelen + 2 + olength;
tcount = ocount;
/*
* Add in the length of rdata in the new slab that aren't in
* the old slab.
*/
do {
dns_rdata_init(&nrdata);
rdata_from_slab(&ncurrent, rdclass, type, &nrdata);
if (!rdata_in_slab(oslab, reservelen, rdclass, type, &nrdata))
{
/*
* This rdata isn't in the old slab.
*/
#if DNS_RDATASET_FIXED
tlength += nrdata.length + 8;
#else
tlength += nrdata.length + 2;
#endif
if (type == dns_rdatatype_rrsig)
tlength++;
tcount++;
nncount++;
added_something = ISC_TRUE;
}
ncount--;
} while (ncount > 0);
ncount = nncount;
if (((flags & DNS_RDATASLAB_EXACT) != 0) &&
(tcount != ncount + ocount))
return (DNS_R_NOTEXACT);
if (!added_something && (flags & DNS_RDATASLAB_FORCE) == 0)
return (DNS_R_UNCHANGED);
/*
* Ensure that singleton types are actually singletons.
*/
if (tcount > 1 && dns_rdatatype_issingleton(type)) {
/*
* We have a singleton type, but there's more than one
* RR in the rdataset.
*/
return (DNS_R_SINGLETON);
}
if (tcount > 0xffff)
return (ISC_R_NOSPACE);
/*
* Copy the reserved area from the new slab.
*/
tstart = isc_mem_get(mctx, tlength);
if (tstart == NULL)
return (ISC_R_NOMEMORY);
memcpy(tstart, nslab, reservelen);
tcurrent = tstart + reservelen;
#if DNS_RDATASET_FIXED
offsetbase = tcurrent;
#endif
/*
* Write the new count.
*/
*tcurrent++ = (tcount & 0xff00) >> 8;
*tcurrent++ = (tcount & 0x00ff);
#if DNS_RDATASET_FIXED
/*
* Skip offset table.
*/
tcurrent += (tcount * 4);
offsettable = isc_mem_get(mctx,
(ocount + oncount) * sizeof(unsigned int));
if (offsettable == NULL) {
isc_mem_put(mctx, tstart, tlength);
return (ISC_R_NOMEMORY);
}
memset(offsettable, 0, (ocount + oncount) * sizeof(unsigned int));
#endif
/*
* Merge the two slabs.
*/
ocurrent = ostart;
INSIST(ocount != 0);
#if DNS_RDATASET_FIXED
oorder = ocurrent[2] * 256 + ocurrent[3];
INSIST(oorder < ocount);
#endif
rdata_from_slab(&ocurrent, rdclass, type, &ordata);
ncurrent = nslab + reservelen + 2;
#if DNS_RDATASET_FIXED
ncurrent += (4 * oncount);
#endif
if (ncount > 0) {
do {
dns_rdata_reset(&nrdata);
#if DNS_RDATASET_FIXED
norder = ncurrent[2] * 256 + ncurrent[3];
INSIST(norder < oncount);
#endif
rdata_from_slab(&ncurrent, rdclass, type, &nrdata);
} while (rdata_in_slab(oslab, reservelen, rdclass,
type, &nrdata));
}
while (oadded < ocount || nadded < ncount) {
isc_boolean_t fromold;
if (oadded == ocount)
fromold = ISC_FALSE;
else if (nadded == ncount)
fromold = ISC_TRUE;
else
fromold = ISC_TF(compare_rdata(&ordata, &nrdata) < 0);
if (fromold) {
#if DNS_RDATASET_FIXED
offsettable[oorder] = tcurrent - offsetbase;
#endif
length = ordata.length;
data = ordata.data;
if (type == dns_rdatatype_rrsig) {
length++;
data--;
}
*tcurrent++ = (length & 0xff00) >> 8;
*tcurrent++ = (length & 0x00ff);
#if DNS_RDATASET_FIXED
tcurrent += 2; /* fill in later */
#endif
memcpy(tcurrent, data, length);
tcurrent += length;
oadded++;
if (oadded < ocount) {
dns_rdata_reset(&ordata);
#if DNS_RDATASET_FIXED
oorder = ocurrent[2] * 256 + ocurrent[3];
INSIST(oorder < ocount);
#endif
rdata_from_slab(&ocurrent, rdclass, type,
&ordata);
}
} else {
#if DNS_RDATASET_FIXED
offsettable[ocount + norder] = tcurrent - offsetbase;
#endif
length = nrdata.length;
data = nrdata.data;
if (type == dns_rdatatype_rrsig) {
length++;
data--;
}
*tcurrent++ = (length & 0xff00) >> 8;
*tcurrent++ = (length & 0x00ff);
#if DNS_RDATASET_FIXED
tcurrent += 2; /* fill in later */
#endif
memcpy(tcurrent, data, length);
tcurrent += length;
nadded++;
if (nadded < ncount) {
do {
dns_rdata_reset(&nrdata);
#if DNS_RDATASET_FIXED
norder = ncurrent[2] * 256 + ncurrent[3];
INSIST(norder < oncount);
#endif
rdata_from_slab(&ncurrent, rdclass,
type, &nrdata);
} while (rdata_in_slab(oslab, reservelen,
rdclass, type,
&nrdata));
}
}
}
#if DNS_RDATASET_FIXED
fillin_offsets(offsetbase, offsettable, ocount + oncount);
isc_mem_put(mctx, offsettable,
(ocount + oncount) * sizeof(unsigned int));
#endif
INSIST(tcurrent == tstart + tlength);
*tslabp = tstart;
return (ISC_R_SUCCESS);
}
isc_result_t
dns_rdataslab_subtract(unsigned char *mslab, unsigned char *sslab,
unsigned int reservelen, isc_mem_t *mctx,
dns_rdataclass_t rdclass, dns_rdatatype_t type,
unsigned int flags, unsigned char **tslabp)
{
unsigned char *mcurrent, *sstart, *scurrent, *tstart, *tcurrent;
unsigned int mcount, scount, rcount ,count, tlength, tcount, i;
dns_rdata_t srdata = DNS_RDATA_INIT;
dns_rdata_t mrdata = DNS_RDATA_INIT;
#if DNS_RDATASET_FIXED
unsigned char *offsetbase;
unsigned int *offsettable;
unsigned int order;
#endif
REQUIRE(tslabp != NULL && *tslabp == NULL);
REQUIRE(mslab != NULL && sslab != NULL);
mcurrent = mslab + reservelen;
mcount = *mcurrent++ * 256;
mcount += *mcurrent++;
scurrent = sslab + reservelen;
scount = *scurrent++ * 256;
scount += *scurrent++;
INSIST(mcount > 0 && scount > 0);
/*
* Yes, this is inefficient!
*/
/*
* Start figuring out the target length and count.
*/
tlength = reservelen + 2;
tcount = 0;
rcount = 0;
#if DNS_RDATASET_FIXED
mcurrent += 4 * mcount;
scurrent += 4 * scount;
#endif
sstart = scurrent;
/*
* Add in the length of rdata in the mslab that aren't in
* the sslab.
*/
for (i = 0; i < mcount; i++) {
unsigned char *mrdatabegin = mcurrent;
rdata_from_slab(&mcurrent, rdclass, type, &mrdata);
scurrent = sstart;
for (count = 0; count < scount; count++) {
dns_rdata_reset(&srdata);
rdata_from_slab(&scurrent, rdclass, type, &srdata);
if (dns_rdata_compare(&mrdata, &srdata) == 0)
break;
}
if (count == scount) {
/*
* This rdata isn't in the sslab, and thus isn't
* being subtracted.
*/
tlength += mcurrent - mrdatabegin;
tcount++;
} else
rcount++;
dns_rdata_reset(&mrdata);
}
#if DNS_RDATASET_FIXED
tlength += (4 * tcount);
#endif
/*
* Check that all the records originally existed. The numeric
* check only works as rdataslabs do not contain duplicates.
*/
if (((flags & DNS_RDATASLAB_EXACT) != 0) && (rcount != scount))
return (DNS_R_NOTEXACT);
/*
* Don't continue if the new rdataslab would be empty.
*/
if (tcount == 0)
return (DNS_R_NXRRSET);
/*
* If nothing is going to change, we can stop.
*/
if (rcount == 0)
return (DNS_R_UNCHANGED);
/*
* Copy the reserved area from the mslab.
*/
tstart = isc_mem_get(mctx, tlength);
if (tstart == NULL)
return (ISC_R_NOMEMORY);
memcpy(tstart, mslab, reservelen);
tcurrent = tstart + reservelen;
#if DNS_RDATASET_FIXED
offsetbase = tcurrent;
offsettable = isc_mem_get(mctx, mcount * sizeof(unsigned int));
if (offsettable == NULL) {
isc_mem_put(mctx, tstart, tlength);
return (ISC_R_NOMEMORY);
}
memset(offsettable, 0, mcount * sizeof(unsigned int));
#endif
/*
* Write the new count.
*/
*tcurrent++ = (tcount & 0xff00) >> 8;
*tcurrent++ = (tcount & 0x00ff);
#if DNS_RDATASET_FIXED
tcurrent += (4 * tcount);
#endif
/*
* Copy the parts of mslab not in sslab.
*/
mcurrent = mslab + reservelen;
mcount = *mcurrent++ * 256;
mcount += *mcurrent++;
#if DNS_RDATASET_FIXED
mcurrent += (4 * mcount);
#endif
for (i = 0; i < mcount; i++) {
unsigned char *mrdatabegin = mcurrent;
#if DNS_RDATASET_FIXED
order = mcurrent[2] * 256 + mcurrent[3];
INSIST(order < mcount);
#endif
rdata_from_slab(&mcurrent, rdclass, type, &mrdata);
scurrent = sstart;
for (count = 0; count < scount; count++) {
dns_rdata_reset(&srdata);
rdata_from_slab(&scurrent, rdclass, type, &srdata);
if (dns_rdata_compare(&mrdata, &srdata) == 0)
break;
}
if (count == scount) {
/*
* This rdata isn't in the sslab, and thus should be
* copied to the tslab.
*/
unsigned int length = mcurrent - mrdatabegin;
#if DNS_RDATASET_FIXED
offsettable[order] = tcurrent - offsetbase;
#endif
memcpy(tcurrent, mrdatabegin, length);
tcurrent += length;
}
dns_rdata_reset(&mrdata);
}
#if DNS_RDATASET_FIXED
fillin_offsets(offsetbase, offsettable, mcount);
isc_mem_put(mctx, offsettable, mcount * sizeof(unsigned int));
#endif
INSIST(tcurrent == tstart + tlength);
*tslabp = tstart;
return (ISC_R_SUCCESS);
}
isc_boolean_t
dns_rdataslab_equal(unsigned char *slab1, unsigned char *slab2,
unsigned int reservelen)
{
unsigned char *current1, *current2;
unsigned int count1, count2;
unsigned int length1, length2;
current1 = slab1 + reservelen;
count1 = *current1++ * 256;
count1 += *current1++;
current2 = slab2 + reservelen;
count2 = *current2++ * 256;
count2 += *current2++;
if (count1 != count2)
return (ISC_FALSE);
#if DNS_RDATASET_FIXED
current1 += (4 * count1);
current2 += (4 * count2);
#endif
while (count1 > 0) {
length1 = *current1++ * 256;
length1 += *current1++;
length2 = *current2++ * 256;
length2 += *current2++;
#if DNS_RDATASET_FIXED
current1 += 2;
current2 += 2;
#endif
if (length1 != length2 ||
memcmp(current1, current2, length1) != 0)
return (ISC_FALSE);
current1 += length1;
current2 += length1;
count1--;
}
return (ISC_TRUE);
}
isc_boolean_t
dns_rdataslab_equalx(unsigned char *slab1, unsigned char *slab2,
unsigned int reservelen, dns_rdataclass_t rdclass,
dns_rdatatype_t type)
{
unsigned char *current1, *current2;
unsigned int count1, count2;
dns_rdata_t rdata1 = DNS_RDATA_INIT;
dns_rdata_t rdata2 = DNS_RDATA_INIT;
current1 = slab1 + reservelen;
count1 = *current1++ * 256;
count1 += *current1++;
current2 = slab2 + reservelen;
count2 = *current2++ * 256;
count2 += *current2++;
if (count1 != count2)
return (ISC_FALSE);
#if DNS_RDATASET_FIXED
current1 += (4 * count1);
current2 += (4 * count2);
#endif
while (count1-- > 0) {
rdata_from_slab(&current1, rdclass, type, &rdata1);
rdata_from_slab(&current2, rdclass, type, &rdata2);
if (dns_rdata_compare(&rdata1, &rdata2) != 0)
return (ISC_FALSE);
dns_rdata_reset(&rdata1);
dns_rdata_reset(&rdata2);
}
return (ISC_TRUE);
}