rdataslab.c revision 87bff9accdd711bd02fe3043800d5edd10082521
/*
* Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
* Copyright (C) 1999-2003 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* 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: rdataslab.c,v 1.42 2007/03/14 23:46:54 tbox Exp $ */
/*! \file */
#include <config.h>
#include <stdlib.h>
#include <dns/rdataset.h>
#include <dns/rdataslab.h>
#ifndef DNS_RDATASET_FIXED
#define DNS_RDATASET_FIXED 1
#endif
/*
* 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)
* 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)
* 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 reconstuction of
* 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
{
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
if (nitems == 0)
return (ISC_R_FAILURE);
if (nalloc > 0xffff)
return (ISC_R_NOSPACE);
if (x == NULL)
return (ISC_R_NOMEMORY);
/*
* Save all of the rdata members into an array.
*/
if (result != ISC_R_SUCCESS)
goto free_rdatas;
dns_rdata_init(&x[i].rdata);
x[i].order = i;
#endif
}
if (result != ISC_R_NOMORE)
goto free_rdatas;
if (i != nalloc) {
/*
* Somehow we iterated over fewer rdatas than
* dns_rdataset_count() said there were!
*/
goto free_rdatas;
}
/*
* Put into DNSSEC order.
*/
/*
* 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
}
/*
* Don't forget the last item!
*/
#else
#endif
/*
* 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
#endif
}
#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;
raw += 4;
#else
raw += 2;
#endif
}
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,
};
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,
{
tcurrent += 2;
#endif
}
/*
* 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 {
#endif
{
/*
* This rdata isn't in the old slab.
*/
#else
#endif
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
#endif
oadded++;
#endif
&ordata);
}
} else {
#endif
#endif
nadded++;
do {
#endif
&nrdata));
}
}
}
#endif
return (ISC_R_SUCCESS);
}
{
unsigned char *offsetbase;
unsigned int *offsettable;
#endif
unsigned int order;
/*
* 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.
*/
#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);
}