rdataslab.c revision 122d284b6112bae486400466769fe474771947c7
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews/*
f428e385a4f7a42196b53de8e134909e8c488258Automatic Updater * Copyright (C) 1999, 2000 Internet Software Consortium.
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews *
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews * copyright notice and this permission notice appear in all copies.
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews *
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews */
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews
f428e385a4f7a42196b53de8e134909e8c488258Automatic Updater/* $Id: rdataslab.c,v 1.18 2000/09/01 16:39:07 gson Exp $ */
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews#include <config.h>
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews#include <stdlib.h>
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews#include <isc/mem.h>
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews#include <isc/region.h>
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews#include <isc/string.h> /* Required for HP/UX (and others?) */
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews#include <isc/util.h>
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews#include <dns/result.h>
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews#include <dns/rdata.h>
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews#include <dns/rdataset.h>
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews#include <dns/rdataslab.h>
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews/* Note: the "const void *" are just to make qsort happy. */
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrewsstatic int
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrewscompare_rdata(const void *p1, const void *p2) {
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews const dns_rdata_t *rdata1 = p1;
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews const dns_rdata_t *rdata2 = p2;
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews int len = ISC_MIN(rdata1->length, rdata2->length);
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews int n;
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews n = memcmp(rdata1->data, rdata2->data, len);
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews if (n != 0)
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews return (n);
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews return (rdata1->length - rdata2->length);
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews}
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrewsisc_result_t
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrewsdns_rdataslab_fromrdataset(dns_rdataset_t *rdataset, isc_mem_t *mctx,
f428e385a4f7a42196b53de8e134909e8c488258Automatic Updater isc_region_t *region, unsigned int reservelen)
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews{
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews dns_rdata_t *rdatas;
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews unsigned char *rawbuf;
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews unsigned int buflen;
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews isc_result_t result;
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews unsigned int nitems;
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews unsigned int nalloc;
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews unsigned int i;
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews buflen = reservelen + 2;
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews nalloc = dns_rdataset_count(rdataset);
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews nitems = nalloc;
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews rdatas = isc_mem_get(mctx, nalloc * sizeof(dns_rdata_t));
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews if (rdatas == NULL)
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews return (ISC_R_NOMEMORY);
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews /*
10f9e687f5824b41083c497adb4d0c54f965ebd6Mark Andrews * Save all of the rdata members into an array.
10f9e687f5824b41083c497adb4d0c54f965ebd6Mark Andrews */
10f9e687f5824b41083c497adb4d0c54f965ebd6Mark Andrews result = dns_rdataset_first(rdataset);
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews INSIST(result == ISC_R_SUCCESS);
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews for (i = 0; i < nalloc; i++) {
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews INSIST(result == ISC_R_SUCCESS);
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews dns_rdataset_current(rdataset, &rdatas[i]);
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews result = dns_rdataset_next(rdataset);
10f9e687f5824b41083c497adb4d0c54f965ebd6Mark Andrews }
10f9e687f5824b41083c497adb4d0c54f965ebd6Mark Andrews INSIST(result == ISC_R_NOMORE);
10f9e687f5824b41083c497adb4d0c54f965ebd6Mark Andrews
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews qsort(rdatas, nalloc, sizeof(dns_rdata_t), compare_rdata);
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews /*
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews * Remove duplicates.
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews */
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews for (i = 1; i < nalloc; i++) {
10f9e687f5824b41083c497adb4d0c54f965ebd6Mark Andrews if (compare_rdata(&rdatas[i-1], &rdatas[i]) == 0) {
10f9e687f5824b41083c497adb4d0c54f965ebd6Mark Andrews rdatas[i-1].data = NULL;
10f9e687f5824b41083c497adb4d0c54f965ebd6Mark Andrews rdatas[i-1].length = 0;
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews nitems--;
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews }
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews }
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews /*
16a68807e13caea3183a41a5292f1b3f48b81a26Mark Andrews * Run through the rdataset list once, counting up the size
* of all the rdata members within it. We do not store the
* class, type, etc, just the rdata, so our overhead is 2 bytes
* for the number of records, and 2 for each rdata length, and
* then the rdata itself.
*/
for (i = 0; i < nalloc; i++) {
if (rdatas[i].data != NULL)
buflen += (2 + rdatas[i].length);
result = dns_rdataset_next(rdataset);
}
if (result != ISC_R_NOMORE) {
isc_mem_put(mctx, rdatas, nalloc * sizeof(dns_rdata_t));
return (result);
}
/*
* Allocate the memory, set up a buffer, start copying in
* data.
*/
rawbuf = isc_mem_get(mctx, buflen);
if (rawbuf == NULL) {
isc_mem_put(mctx, rdatas, nalloc * sizeof(dns_rdata_t));
return (ISC_R_NOMEMORY);
}
region->base = rawbuf;
region->length = buflen;
rawbuf += reservelen;
*rawbuf++ = (nitems & 0xff00) >> 8;
*rawbuf++ = (nitems & 0x00ff);
for (i = 0; i < nalloc; i++) {
if (rdatas[i].data == NULL)
continue;
*rawbuf++ = (rdatas[i].length & 0xff00) >> 8;
*rawbuf++ = (rdatas[i].length & 0x00ff);
memcpy(rawbuf, rdatas[i].data, rdatas[i].length);
rawbuf += rdatas[i].length;
}
isc_mem_put(mctx, rdatas, nalloc * sizeof(dns_rdata_t));
return (ISC_R_SUCCESS);
}
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++;
while (count > 0) {
count--;
length = *current++ * 256;
length += *current++;
current += length;
}
return ((unsigned int)(current - slab));
}
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;
isc_region_t region;
unsigned char *current;
dns_rdata_t trdata;
current = slab + reservelen;
count = *current++ * 256;
count += *current++;
dns_rdata_init(&trdata);
for (i = 0; i < count; i++) {
region.length = *current++ * 256;
region.length += *current++;
region.base = current;
dns_rdata_fromregion(&trdata, rdclass, type, &region);
current += region.length;
if (dns_rdata_compare(&trdata, rdata) == 0)
return (ISC_TRUE);
}
return (ISC_FALSE);
}
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;
region.length = *tcurrent++ * 256;
region.length += *tcurrent++;
region.base = tcurrent;
tcurrent += region.length;
dns_rdata_fromregion(rdata, rdclass, type, &region);
*current = tcurrent;
}
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,
isc_boolean_t force, unsigned char **tslabp)
{
unsigned char *ocurrent, *ostart, *ncurrent, *tstart, *tcurrent;
unsigned int ocount, ncount, count, olength, tlength, tcount, length;
isc_region_t nregion;
dns_rdata_t ordata, nrdata;
isc_boolean_t added_something = ISC_FALSE;
unsigned int oadded = 0;
unsigned int nadded = 0;
unsigned int nncount = 0;
/*
* Merge 'oslab' and 'nslab'.
*/
/*
* 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++;
ostart = ocurrent;
ncurrent = nslab + reservelen;
ncount = *ncurrent++ * 256;
ncount += *ncurrent++;
INSIST(ocount > 0 && ncount > 0);
/*
* 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++;
olength += length + 2;
ocurrent += length;
}
/*
* 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 {
nregion.length = *ncurrent++ * 256;
nregion.length += *ncurrent++;
nregion.base = ncurrent;
dns_rdata_fromregion(&nrdata, rdclass, type, &nregion);
if (!rdata_in_slab(oslab, reservelen, rdclass, type, &nrdata))
{
/*
* This rdata isn't in the old slab.
*/
tlength += nregion.length + 2;
tcount++;
nncount++;
added_something = ISC_TRUE;
}
ncurrent += nregion.length;
ncount--;
} while (ncount > 0);
ncount = nncount;
if (!added_something && !force)
return (DNS_R_UNCHANGED);
/*
* 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;
/*
* Write the new count.
*/
*tcurrent++ = (tcount & 0xff00) >> 8;
*tcurrent++ = (tcount & 0x00ff);
/*
* Merge the two slabs.
*/
ocurrent = ostart;
if (ocount > 0)
rdata_from_slab(&ocurrent, rdclass, type, &ordata);
ncurrent = nslab + reservelen + 2;
if (ncount > 0) {
do {
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) {
length = ordata.length;
*tcurrent++ = (length & 0xff00) >> 8;
*tcurrent++ = (length & 0x00ff);
memcpy(tcurrent, ordata.data, length);
tcurrent += length;
oadded++;
if (oadded < ocount) {
rdata_from_slab(&ocurrent, rdclass, type,
&ordata);
}
} else {
length = nrdata.length;
*tcurrent++ = (length & 0xff00) >> 8;
*tcurrent++ = (length & 0x00ff);
memcpy(tcurrent, nrdata.data, length);
tcurrent += length;
nadded++;
if (nadded < ncount) {
do {
rdata_from_slab(&ncurrent, rdclass,
type, &nrdata);
} while (rdata_in_slab(oslab, reservelen,
rdclass, type,
&nrdata));
}
}
}
*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 char **tslabp)
{
unsigned char *mcurrent, *sstart, *scurrent, *tstart, *tcurrent;
unsigned int mcount, scount, count, tlength, tcount;
isc_region_t mregion, sregion;
dns_rdata_t srdata, mrdata;
isc_boolean_t removed_something = ISC_FALSE;
/*
* Subtract 'sslab' from 'mslab'.
*/
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++;
sstart = scurrent;
INSIST(mcount > 0 && scount > 0);
/*
* Yes, this is inefficient!
*/
/*
* Start figuring out the target length and count.
*/
tlength = reservelen + 2;
tcount = 0;
/*
* Add in the length of rdata in the mslab that aren't in
* the sslab.
*/
do {
mregion.length = *mcurrent++ * 256;
mregion.length += *mcurrent++;
mregion.base = mcurrent;
dns_rdata_fromregion(&mrdata, rdclass, type, &mregion);
scurrent = sstart;
for (count = 0; count < scount; count++) {
sregion.length = *scurrent++ * 256;
sregion.length += *scurrent++;
sregion.base = scurrent;
dns_rdata_fromregion(&srdata, rdclass, type, &sregion);
scurrent += sregion.length;
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 += mregion.length + 2;
tcount++;
} else
removed_something = ISC_TRUE;
mcurrent += mregion.length;
mcount--;
} while (mcount > 0);
/*
* 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 (!removed_something)
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;
/*
* Write the new count.
*/
*tcurrent++ = (tcount & 0xff00) >> 8;
*tcurrent++ = (tcount & 0x00ff);
/*
* Copy the parts of mslab not in sslab.
*/
mcurrent = mslab + reservelen;
mcount = *mcurrent++ * 256;
mcount += *mcurrent++;
do {
mregion.length = *mcurrent++ * 256;
mregion.length += *mcurrent++;
mregion.base = mcurrent;
dns_rdata_fromregion(&mrdata, rdclass, type, &mregion);
scurrent = sstart;
for (count = 0; count < scount; count++) {
sregion.length = *scurrent++ * 256;
sregion.length += *scurrent++;
sregion.base = scurrent;
dns_rdata_fromregion(&srdata, rdclass, type, &sregion);
scurrent += sregion.length;
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.
*/
*tcurrent++ = (mregion.length & 0xff00) >> 8;
*tcurrent++ = (mregion.length & 0x00ff);
memcpy(tcurrent, mregion.base, mregion.length);
tcurrent += mregion.length;
}
mcurrent += mregion.length;
mcount--;
} while (mcount > 0);
*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);
while (count1 > 0) {
length1 = *current1++ * 256;
length1 += *current1++;
length2 = *current2++ * 256;
length2 += *current2++;
if (length1 != length2 ||
memcmp(current1, current2, length1) != 0)
return (ISC_FALSE);
current1 += length1;
current2 += length1;
count1--;
}
return (ISC_TRUE);
}