rdataslab.c revision a8da00ef95ba37b9d071c2b8db1a0c967e060106
/*
* Copyright (C) 2004-2014 Internet Systems Consortium, Inc. ("ISC")
* Copyright (C) 1999-2003 Internet Software Consortium.
*
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id$ */
/*! \file */
#include <config.h>
#include <stdlib.h>
#include <dns/rdataset.h>
#include <dns/rdataslab.h>
/*
* The rdataslab structure allows iteration to occur in both load order
* and DNSSEC order. The structure is as follows:
*
* header (reservelen bytes)
* record count (2 bytes)
* offset table (4 x record count bytes in load order)
* data records
* data length (2 bytes)
* order (2 bytes)
* meta data (1 byte for RRSIG's)
* data (data length bytes)
*
* If DNS_RDATASET_FIXED is defined to be zero (0) the format of a
* rdataslab is as follows:
*
* header (reservelen bytes)
* record count (2 bytes)
* data records
* data length (2 bytes)
* meta data (1 byte for RRSIG's)
* data (data length bytes)
*
* Offsets are from the end of the header.
*
* Load order traversal is performed by walking the offset table to find
* the start of the record (DNS_RDATASET_FIXED = 1).
*
* DNSSEC order traversal is performed by walking the data records.
*
* The order is stored with record to allow for efficient reconstruction
* of the offset table following a merge or subtraction.
*
* The iterator methods here currently only support DNSSEC order iteration.
*
* The iterator methods in rbtdb support both load order and DNSSEC order
* iteration.
*
* WARNING:
* rbtdb.c directly interacts with the slab's raw structures. If the
* structure changes then rbtdb.c also needs to be updated to reflect
* the changes. See the areas tagged with "RDATASLAB".
*/
struct xrdata {
unsigned int order;
};
/*% Note: the "const void *" are just to make qsort happy. */
static int
}
static void
unsigned length)
{
unsigned int i, j;
unsigned char *raw;
for (i = 0, j = 0; i < length; i++) {
if (offsettable[i] == 0)
continue;
/*
* Fill in offset table.
*/
/*
* Fill in table index.
*/
*raw = j++ & 0xff;
}
}
#endif
{
/*
* Use &removed as a sentinal pointer for duplicate
* rdata as rdata.data == NULL is valid.
*/
static unsigned char removed;
struct xrdata *x;
unsigned char *rawbuf;
unsigned char *offsetbase;
#endif
unsigned int buflen;
unsigned int nitems;
unsigned int nalloc;
unsigned int i;
unsigned int *offsettable;
#endif
unsigned int length;
/*
* If there are no rdata then we can just need to allocate a header
* with zero a record count.
*/
if (nitems == 0) {
return (ISC_R_FAILURE);
return (ISC_R_NOMEMORY);
rawbuf += reservelen;
*rawbuf++ = 0;
*rawbuf = 0;
return (ISC_R_SUCCESS);
}
if (nitems > 0xffff)
return (ISC_R_NOSPACE);
/*
* Remember the original number of items.
*/
if (x == NULL)
return (ISC_R_NOMEMORY);
/*
* Save all of the rdata members into an array.
*/
goto free_rdatas;
dns_rdata_init(&x[i].rdata);
x[i].order = i;
#endif
}
/*
* Somehow we iterated over fewer rdatas than
* dns_rdataset_count() said there were or there
* were more items than dns_rdataset_count said
* there were.
*/
goto free_rdatas;
}
/*
* Put into DNSSEC order.
*/
if (nalloc > 1U)
/*
* Remove duplicates and compute the total storage required.
*
* If an rdata is not a duplicate, accumulate the storage size
* required for the rdata. We do not store the class, type, etc,
* just the rdata, so our overhead is 2 bytes for the number of
* records, and 8 for each rdata, (length(2), offset(4) and order(2))
* and then the rdata itself.
*/
for (i = 1; i < nalloc; i++) {
/*
* Preserve the least order so A, B, A -> A, B
* after duplicate removal.
*/
#endif
nitems--;
} else {
#else
#endif
/*
* Provide space to store the per RR meta data.
*/
buflen++;
}
}
/*
* Don't forget the last item!
*/
#else
#endif
/*
* Provide space to store the per RR meta data.
*/
buflen++;
/*
* Ensure that singleton types are actually singletons.
*/
/*
* We have a singleton type, but there's more than one
* RR in the rdataset.
*/
goto free_rdatas;
}
/*
* Allocate the memory, set up a buffer, start copying in
* data.
*/
goto free_rdatas;
}
/* Allocate temporary offset table. */
if (offsettable == NULL) {
goto free_rdatas;
}
#endif
rawbuf += reservelen;
offsetbase = rawbuf;
#endif
/* Skip load order table. Filled in later. */
#endif
for (i = 0; i < nalloc; i++) {
continue;
#endif
length++;
#endif
/*
* Store the per RR meta data.
*/
}
}
#endif
return (result);
}
static void
}
static isc_result_t
unsigned int count;
if (count == 0) {
return (ISC_R_NOMORE);
}
#else
raw += 2;
#endif
/*
* The privateuint4 field is the number of rdata beyond the cursor
* position, so we decrement the total count by one before storing
* it.
*/
count--;
return (ISC_R_SUCCESS);
}
static isc_result_t
unsigned int count;
unsigned int length;
unsigned char *raw;
if (count == 0)
return (ISC_R_NOMORE);
count--;
#else
#endif
return (ISC_R_SUCCESS);
}
static void
isc_region_t r;
unsigned int length;
unsigned int flags = 0;
raw += 4;
#else
raw += 2;
#endif
if (*raw & DNS_RDATASLAB_OFFLINE)
length--;
raw++;
}
}
static void
/*
* Reset iterator state.
*/
target->privateuint4 = 0;
}
static unsigned int
unsigned int count;
return (count);
}
static dns_rdatasetmethods_t rdataset_methods = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
};
void
{
/*
* Reset iterator state.
*/
rdataset->privateuint4 = 0;
}
unsigned int
unsigned char *current;
#endif
while (count > 0) {
count--;
#else
#endif
}
}
/*
* 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,
{
unsigned int length;
if (type == dns_rdatatype_rrsig) {
if ((*tcurrent & DNS_RDATASLAB_OFFLINE) != 0)
length--;
tcurrent++;
}
tcurrent += 2;
#endif
if (offline)
}
/*
* 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
{
unsigned int count, i;
unsigned char *current;
int n;
#endif
for (i = 0; i < count; i++) {
if (n == 0)
return (ISC_TRUE);
if (n > 0) /* In DNSSEC order. */
break;
}
return (ISC_FALSE);
}
{
unsigned int oadded = 0;
unsigned int nadded = 0;
unsigned int nncount = 0;
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.
*/
#endif
#endif
#endif
/*
* Yes, this is inefficient!
*/
/*
* Figure out the length of the old slab's data.
*/
olength = 0;
#else
#endif
}
/*
* Start figuring out the target length and count.
*/
/*
* Add in the length of rdata in the new slab that aren't in
* the old slab.
*/
do {
{
/*
* This rdata isn't in the old slab.
*/
#else
#endif
if (type == dns_rdatatype_rrsig)
tlength++;
tcount++;
nncount++;
}
ncount--;
} while (ncount > 0);
if (((flags & DNS_RDATASLAB_EXACT) != 0) &&
return (DNS_R_NOTEXACT);
return (DNS_R_UNCHANGED);
/*
* Ensure that singleton types are actually singletons.
*/
/*
* 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.
*/
return (ISC_R_NOMEMORY);
#endif
/*
* Write the new count.
*/
/*
* Skip offset table.
*/
if (offsettable == NULL) {
return (ISC_R_NOMEMORY);
}
#endif
/*
* Merge the two slabs.
*/
#endif
#endif
if (ncount > 0) {
do {
#endif
}
else
if (fromold) {
#endif
if (type == dns_rdatatype_rrsig) {
length++;
data--;
}
#endif
oadded++;
#endif
&ordata);
}
} else {
#endif
if (type == dns_rdatatype_rrsig) {
length++;
data--;
}
#endif
nadded++;
do {
#endif
&nrdata));
}
}
}
#endif
return (ISC_R_SUCCESS);
}
{
unsigned char *offsetbase;
unsigned int *offsettable;
unsigned int order;
#endif
/*
* Yes, this is inefficient!
*/
/*
* Start figuring out the target length and count.
*/
tcount = 0;
rcount = 0;
#endif
/*
* 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;
break;
}
/*
* This rdata isn't in the sslab, and thus isn't
* being subtracted.
*/
tcount++;
} else
rcount++;
}
#endif
/*
* Check that all the records originally existed. The numeric
* check only works as rdataslabs do not contain duplicates.
*/
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.
*/
return (ISC_R_NOMEMORY);
if (offsettable == NULL) {
return (ISC_R_NOMEMORY);
}
#endif
/*
* Write the new count.
*/
#endif
/*
* Copy the parts of mslab not in sslab.
*/
#endif
for (i = 0; i < mcount; i++) {
unsigned char *mrdatabegin = mcurrent;
#endif
break;
}
/*
* This rdata isn't in the sslab, and thus should be
* copied to the tslab.
*/
unsigned int length;
#endif
}
}
#endif
return (ISC_R_SUCCESS);
}
unsigned int reservelen)
{
return (ISC_FALSE);
#endif
while (count1 > 0) {
current1 += 2;
current2 += 2;
#endif
return (ISC_FALSE);
count1--;
}
return (ISC_TRUE);
}
{
return (ISC_FALSE);
#endif
while (count1-- > 0) {
return (ISC_FALSE);
}
return (ISC_TRUE);
}