2 * Copyright (C) 2004-2006 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: rdataslab.c,v 1.35.18.5 2006/03/05 23:58:51 marka Exp $ */
27 #include <isc/region.h>
28 #include <isc/string.h> /* Required for HP/UX (and others?) */
31 #include <dns/result.h>
32 #include <dns/rdata.h>
33 #include <dns/rdataset.h>
34 #include <dns/rdataslab.h>
37 * The rdataslab structure allows iteration to occur in both load order
38 * and DNSSEC order. The structure is as follows:
40 * header (reservelen bytes)
41 * record count (2 bytes)
42 * offset table (4 x record count bytes in load order)
44 * data length (2 bytes)
46 * data (data length bytes)
48 * Offsets are from the end of the header.
50 * Load order traversal is performed by walking the offset table to find
51 * the start of the record.
53 * DNSSEC order traversal is performed by walking the data records.
55 * The order is stored with record to allow for efficient reconstuction of
56 * of the offset table following a merge or subtraction.
58 * The iterator methods here currently only support DNSSEC order iteration.
60 * The iterator methods in rbtdb support both load order and DNSSEC order
64 * rbtdb.c directly interacts with the slab's raw structures. If the
65 * structure changes then rbtdb.c also needs to be updated to reflect
66 * the changes. See the areas tagged with "RDATASLAB".
74 /*% Note: the "const void *" are just to make qsort happy. */
76 compare_rdata(const void *p1, const void *p2) {
77 const struct xrdata *x1 = p1;
78 const struct xrdata *x2 = p2;
79 return (dns_rdata_compare(&x1->rdata, &x2->rdata));
83 fillin_offsets(unsigned char *offsetbase, unsigned int *offsettable,
89 for (i = 0, j = 0; i < length; i++) {
91 if (offsettable[i] == 0)
95 * Fill in offset table.
97 raw = &offsetbase[j*4 + 2];
98 *raw++ = (offsettable[i] & 0xff000000) >> 24;
99 *raw++ = (offsettable[i] & 0xff0000) >> 16;
100 *raw++ = (offsettable[i] & 0xff00) >> 8;
101 *raw = offsettable[i] & 0xff;
104 * Fill in table index.
106 raw = offsetbase + offsettable[i] + 2;
107 *raw++ = (j & 0xff00) >> 8;
113 dns_rdataslab_fromrdataset(dns_rdataset_t *rdataset, isc_mem_t *mctx,
114 isc_region_t *region, unsigned int reservelen)
117 unsigned char *rawbuf;
118 unsigned char *offsetbase;
124 unsigned int *offsettable;
126 buflen = reservelen + 2;
128 nalloc = dns_rdataset_count(rdataset);
131 return (ISC_R_FAILURE);
134 return (ISC_R_NOSPACE);
136 x = isc_mem_get(mctx, nalloc * sizeof(struct xrdata));
138 return (ISC_R_NOMEMORY);
141 * Save all of the rdata members into an array.
143 result = dns_rdataset_first(rdataset);
144 if (result != ISC_R_SUCCESS)
146 for (i = 0; i < nalloc && result == ISC_R_SUCCESS; i++) {
147 INSIST(result == ISC_R_SUCCESS);
148 dns_rdata_init(&x[i].rdata);
149 dns_rdataset_current(rdataset, &x[i].rdata);
151 result = dns_rdataset_next(rdataset);
153 if (result != ISC_R_NOMORE)
157 * Somehow we iterated over fewer rdatas than
158 * dns_rdataset_count() said there were!
160 result = ISC_R_FAILURE;
165 * Put into DNSSEC order.
167 qsort(x, nalloc, sizeof(struct xrdata), compare_rdata);
170 * Remove duplicates and compute the total storage required.
172 * If an rdata is not a duplicate, accumulate the storage size
173 * required for the rdata. We do not store the class, type, etc,
174 * just the rdata, so our overhead is 2 bytes for the number of
175 * records, and 8 for each rdata, (length(2), offset(4) and order(2))
176 * and then the rdata itself.
178 for (i = 1; i < nalloc; i++) {
179 if (compare_rdata(&x[i-1].rdata, &x[i].rdata) == 0) {
180 x[i-1].rdata.data = NULL;
181 x[i-1].rdata.length = 0;
183 * Preserve the least order so A, B, A -> A, B
184 * after duplicate removal.
186 if (x[i-1].order < x[i].order)
187 x[i].order = x[i-1].order;
190 buflen += (8 + x[i-1].rdata.length);
193 * Don't forget the last item!
195 buflen += (8 + x[i-1].rdata.length);
198 * Ensure that singleton types are actually singletons.
200 if (nitems > 1 && dns_rdatatype_issingleton(rdataset->type)) {
202 * We have a singleton type, but there's more than one
203 * RR in the rdataset.
205 result = DNS_R_SINGLETON;
210 * Allocate the memory, set up a buffer, start copying in
213 rawbuf = isc_mem_get(mctx, buflen);
214 if (rawbuf == NULL) {
215 result = ISC_R_NOMEMORY;
219 /* Allocate temporary offset table. */
220 offsettable = isc_mem_get(mctx, nalloc * sizeof(unsigned int));
221 if (offsettable == NULL) {
222 isc_mem_put(mctx, rawbuf, buflen);
223 result = ISC_R_NOMEMORY;
226 memset(offsettable, 0, nalloc * sizeof(unsigned int));
228 region->base = rawbuf;
229 region->length = buflen;
231 rawbuf += reservelen;
234 *rawbuf++ = (nitems & 0xff00) >> 8;
235 *rawbuf++ = (nitems & 0x00ff);
237 /* Skip load order table. Filled in later. */
238 rawbuf += nitems * 4;
240 for (i = 0; i < nalloc; i++) {
241 if (x[i].rdata.data == NULL)
243 offsettable[x[i].order] = rawbuf - offsetbase;
244 *rawbuf++ = (x[i].rdata.length & 0xff00) >> 8;
245 *rawbuf++ = (x[i].rdata.length & 0x00ff);
246 rawbuf += 2; /* filled in later */
247 memcpy(rawbuf, x[i].rdata.data, x[i].rdata.length);
248 rawbuf += x[i].rdata.length;
251 fillin_offsets(offsetbase, offsettable, nalloc);
253 isc_mem_put(mctx, offsettable, nalloc * sizeof(unsigned int));
255 result = ISC_R_SUCCESS;
258 isc_mem_put(mctx, x, nalloc * sizeof(struct xrdata));
263 rdataset_disassociate(dns_rdataset_t *rdataset) {
268 rdataset_first(dns_rdataset_t *rdataset) {
269 unsigned char *raw = rdataset->private3;
272 count = raw[0] * 256 + raw[1];
274 rdataset->private5 = NULL;
275 return (ISC_R_NOMORE);
277 raw += 2 + (4 * count);
279 * The privateuint4 field is the number of rdata beyond the cursor
280 * position, so we decrement the total count by one before storing
284 rdataset->privateuint4 = count;
285 rdataset->private5 = raw;
287 return (ISC_R_SUCCESS);
291 rdataset_next(dns_rdataset_t *rdataset) {
296 count = rdataset->privateuint4;
298 return (ISC_R_NOMORE);
300 rdataset->privateuint4 = count;
301 raw = rdataset->private5;
302 length = raw[0] * 256 + raw[1];
304 rdataset->private5 = raw;
306 return (ISC_R_SUCCESS);
310 rdataset_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {
311 unsigned char *raw = rdataset->private5;
314 REQUIRE(raw != NULL);
316 r.length = raw[0] * 256 + raw[1];
319 dns_rdata_fromregion(rdata, rdataset->rdclass, rdataset->type, &r);
323 rdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target) {
327 * Reset iterator state.
329 target->privateuint4 = 0;
330 target->private5 = NULL;
334 rdataset_count(dns_rdataset_t *rdataset) {
335 unsigned char *raw = rdataset->private3;
338 count = raw[0] * 256 + raw[1];
343 static dns_rdatasetmethods_t rdataset_methods = {
344 rdataset_disassociate,
358 dns_rdataslab_tordataset(unsigned char *slab, unsigned int reservelen,
359 dns_rdataclass_t rdclass, dns_rdatatype_t rdtype,
360 dns_rdatatype_t covers, dns_ttl_t ttl,
361 dns_rdataset_t *rdataset)
363 REQUIRE(slab != NULL);
364 REQUIRE(!dns_rdataset_isassociated(rdataset));
366 rdataset->methods = &rdataset_methods;
367 rdataset->rdclass = rdclass;
368 rdataset->type = rdtype;
369 rdataset->covers = covers;
372 rdataset->private1 = NULL;
373 rdataset->private2 = NULL;
374 rdataset->private3 = slab + reservelen;
377 * Reset iterator state.
379 rdataset->privateuint4 = 0;
380 rdataset->private5 = NULL;
384 dns_rdataslab_size(unsigned char *slab, unsigned int reservelen) {
385 unsigned int count, length;
386 unsigned char *current;
388 REQUIRE(slab != NULL);
390 current = slab + reservelen;
391 count = *current++ * 256;
393 current += (4 * count);
396 length = *current++ * 256;
397 length += *current++;
398 current += length + 2;
401 return ((unsigned int)(current - slab));
405 * Make the dns_rdata_t 'rdata' refer to the slab item
406 * beginning at '*current', which is part of a slab of type
407 * 'type' and class 'rdclass', and advance '*current' to
408 * point to the next item in the slab.
411 rdata_from_slab(unsigned char **current,
412 dns_rdataclass_t rdclass, dns_rdatatype_t type,
415 unsigned char *tcurrent = *current;
418 region.length = *tcurrent++ * 256;
419 region.length += *tcurrent++;
421 region.base = tcurrent;
422 tcurrent += region.length;
423 dns_rdata_fromregion(rdata, rdclass, type, ®ion);
428 * Return true iff 'slab' (slab data of type 'type' and class 'rdclass')
429 * contains an rdata identical to 'rdata'. This does case insensitive
430 * comparisons per DNSSEC.
432 static inline isc_boolean_t
433 rdata_in_slab(unsigned char *slab, unsigned int reservelen,
434 dns_rdataclass_t rdclass, dns_rdatatype_t type,
437 unsigned int count, i;
438 unsigned char *current;
439 dns_rdata_t trdata = DNS_RDATA_INIT;
442 current = slab + reservelen;
443 count = *current++ * 256;
446 current += (4 * count);
448 for (i = 0; i < count; i++) {
449 rdata_from_slab(¤t, rdclass, type, &trdata);
451 n = dns_rdata_compare(&trdata, rdata);
454 if (n > 0) /* In DNSSEC order. */
456 dns_rdata_reset(&trdata);
462 dns_rdataslab_merge(unsigned char *oslab, unsigned char *nslab,
463 unsigned int reservelen, isc_mem_t *mctx,
464 dns_rdataclass_t rdclass, dns_rdatatype_t type,
465 unsigned int flags, unsigned char **tslabp)
467 unsigned char *ocurrent, *ostart, *ncurrent, *tstart, *tcurrent;
468 unsigned int ocount, ncount, count, olength, tlength, tcount, length;
469 isc_region_t nregion;
470 dns_rdata_t ordata = DNS_RDATA_INIT;
471 dns_rdata_t nrdata = DNS_RDATA_INIT;
472 isc_boolean_t added_something = ISC_FALSE;
473 unsigned int oadded = 0;
474 unsigned int nadded = 0;
475 unsigned int nncount = 0;
476 unsigned int oncount;
477 unsigned int norder = 0;
478 unsigned int oorder = 0;
479 unsigned char *offsetbase;
480 unsigned int *offsettable;
483 * XXX Need parameter to allow "delete rdatasets in nslab" merge,
484 * or perhaps another merge routine for this purpose.
487 REQUIRE(tslabp != NULL && *tslabp == NULL);
488 REQUIRE(oslab != NULL && nslab != NULL);
490 ocurrent = oslab + reservelen;
491 ocount = *ocurrent++ * 256;
492 ocount += *ocurrent++;
493 ocurrent += (4 * ocount);
495 ncurrent = nslab + reservelen;
496 ncount = *ncurrent++ * 256;
497 ncount += *ncurrent++;
498 ncurrent += (4 * ncount);
499 INSIST(ocount > 0 && ncount > 0);
504 * Yes, this is inefficient!
508 * Figure out the length of the old slab's data.
511 for (count = 0; count < ocount; count++) {
512 length = *ocurrent++ * 256;
513 length += *ocurrent++;
514 olength += length + 8;
515 ocurrent += length + 2;
519 * Start figuring out the target length and count.
521 tlength = reservelen + 2 + olength;
525 * Add in the length of rdata in the new slab that aren't in
529 nregion.length = *ncurrent++ * 256;
530 nregion.length += *ncurrent++;
532 nregion.base = ncurrent;
533 dns_rdata_init(&nrdata);
534 dns_rdata_fromregion(&nrdata, rdclass, type, &nregion);
535 if (!rdata_in_slab(oslab, reservelen, rdclass, type, &nrdata))
538 * This rdata isn't in the old slab.
540 tlength += nregion.length + 8;
543 added_something = ISC_TRUE;
545 ncurrent += nregion.length;
547 } while (ncount > 0);
550 if (((flags & DNS_RDATASLAB_EXACT) != 0) &&
551 (tcount != ncount + ocount))
552 return (DNS_R_NOTEXACT);
554 if (!added_something && (flags & DNS_RDATASLAB_FORCE) == 0)
555 return (DNS_R_UNCHANGED);
558 * Ensure that singleton types are actually singletons.
560 if (tcount > 1 && dns_rdatatype_issingleton(type)) {
562 * We have a singleton type, but there's more than one
563 * RR in the rdataset.
565 return (DNS_R_SINGLETON);
569 return (ISC_R_NOSPACE);
572 * Copy the reserved area from the new slab.
574 tstart = isc_mem_get(mctx, tlength);
576 return (ISC_R_NOMEMORY);
577 memcpy(tstart, nslab, reservelen);
578 tcurrent = tstart + reservelen;
579 offsetbase = tcurrent;
582 * Write the new count.
584 *tcurrent++ = (tcount & 0xff00) >> 8;
585 *tcurrent++ = (tcount & 0x00ff);
590 tcurrent += (tcount * 4);
592 offsettable = isc_mem_get(mctx,
593 (ocount + oncount) * sizeof(unsigned int));
594 if (offsettable == NULL) {
595 isc_mem_put(mctx, tstart, tlength);
596 return (ISC_R_NOMEMORY);
598 memset(offsettable, 0, (ocount + oncount) * sizeof(unsigned int));
601 * Merge the two slabs.
605 oorder = ocurrent[2] * 256 + ocurrent[3];
606 INSIST(oorder < ocount);
607 rdata_from_slab(&ocurrent, rdclass, type, &ordata);
609 ncurrent = nslab + reservelen + 2;
610 ncurrent += (4 * oncount);
614 dns_rdata_reset(&nrdata);
615 norder = ncurrent[2] * 256 + ncurrent[3];
616 INSIST(norder < oncount);
617 rdata_from_slab(&ncurrent, rdclass, type, &nrdata);
618 } while (rdata_in_slab(oslab, reservelen, rdclass,
622 while (oadded < ocount || nadded < ncount) {
623 isc_boolean_t fromold;
624 if (oadded == ocount)
626 else if (nadded == ncount)
629 fromold = ISC_TF(compare_rdata(&ordata, &nrdata) < 0);
631 offsettable[oorder] = tcurrent - offsetbase;
632 length = ordata.length;
633 *tcurrent++ = (length & 0xff00) >> 8;
634 *tcurrent++ = (length & 0x00ff);
635 tcurrent += 2; /* fill in later */
636 memcpy(tcurrent, ordata.data, length);
639 if (oadded < ocount) {
640 dns_rdata_reset(&ordata);
641 oorder = ocurrent[2] * 256 + ocurrent[3];
642 INSIST(oorder < ocount);
643 rdata_from_slab(&ocurrent, rdclass, type,
647 offsettable[ocount + norder] = tcurrent - offsetbase;
648 length = nrdata.length;
649 *tcurrent++ = (length & 0xff00) >> 8;
650 *tcurrent++ = (length & 0x00ff);
651 tcurrent += 2; /* fill in later */
652 memcpy(tcurrent, nrdata.data, length);
655 if (nadded < ncount) {
657 dns_rdata_reset(&nrdata);
658 norder = ncurrent[2] * 256 + ncurrent[3];
659 INSIST(norder < oncount);
660 rdata_from_slab(&ncurrent, rdclass,
662 } while (rdata_in_slab(oslab, reservelen,
669 fillin_offsets(offsetbase, offsettable, ocount + oncount);
671 isc_mem_put(mctx, offsettable,
672 (ocount + oncount) * sizeof(unsigned int));
674 INSIST(tcurrent == tstart + tlength);
678 return (ISC_R_SUCCESS);
682 dns_rdataslab_subtract(unsigned char *mslab, unsigned char *sslab,
683 unsigned int reservelen, isc_mem_t *mctx,
684 dns_rdataclass_t rdclass, dns_rdatatype_t type,
685 unsigned int flags, unsigned char **tslabp)
687 unsigned char *mcurrent, *sstart, *scurrent, *tstart, *tcurrent;
688 unsigned int mcount, scount, rcount ,count, tlength, tcount, i;
689 dns_rdata_t srdata = DNS_RDATA_INIT;
690 dns_rdata_t mrdata = DNS_RDATA_INIT;
691 unsigned char *offsetbase;
692 unsigned int *offsettable;
695 REQUIRE(tslabp != NULL && *tslabp == NULL);
696 REQUIRE(mslab != NULL && sslab != NULL);
698 mcurrent = mslab + reservelen;
699 mcount = *mcurrent++ * 256;
700 mcount += *mcurrent++;
701 scurrent = sslab + reservelen;
702 scount = *scurrent++ * 256;
703 scount += *scurrent++;
704 INSIST(mcount > 0 && scount > 0);
707 * Yes, this is inefficient!
711 * Start figuring out the target length and count.
713 tlength = reservelen + 2;
717 mcurrent += 4 * mcount;
718 scurrent += 4 * scount;
722 * Add in the length of rdata in the mslab that aren't in
725 for (i = 0; i < mcount; i++) {
726 unsigned char *mrdatabegin = mcurrent;
727 rdata_from_slab(&mcurrent, rdclass, type, &mrdata);
729 for (count = 0; count < scount; count++) {
730 dns_rdata_reset(&srdata);
731 rdata_from_slab(&scurrent, rdclass, type, &srdata);
732 if (dns_rdata_compare(&mrdata, &srdata) == 0)
735 if (count == scount) {
737 * This rdata isn't in the sslab, and thus isn't
740 tlength += mcurrent - mrdatabegin;
744 dns_rdata_reset(&mrdata);
747 tlength += (4 * tcount);
750 * Check that all the records originally existed. The numeric
751 * check only works as rdataslabs do not contain duplicates.
753 if (((flags & DNS_RDATASLAB_EXACT) != 0) && (rcount != scount))
754 return (DNS_R_NOTEXACT);
757 * Don't continue if the new rdataslab would be empty.
760 return (DNS_R_NXRRSET);
763 * If nothing is going to change, we can stop.
766 return (DNS_R_UNCHANGED);
769 * Copy the reserved area from the mslab.
771 tstart = isc_mem_get(mctx, tlength);
773 return (ISC_R_NOMEMORY);
774 memcpy(tstart, mslab, reservelen);
775 tcurrent = tstart + reservelen;
776 offsetbase = tcurrent;
778 offsettable = isc_mem_get(mctx, mcount * sizeof(unsigned int));
779 if (offsettable == NULL) {
780 isc_mem_put(mctx, tstart, tlength);
781 return (ISC_R_NOMEMORY);
783 memset(offsettable, 0, mcount * sizeof(unsigned int));
786 * Write the new count.
788 *tcurrent++ = (tcount & 0xff00) >> 8;
789 *tcurrent++ = (tcount & 0x00ff);
791 tcurrent += (4 * tcount);
794 * Copy the parts of mslab not in sslab.
796 mcurrent = mslab + reservelen;
797 mcount = *mcurrent++ * 256;
798 mcount += *mcurrent++;
799 mcurrent += (4 * mcount);
800 for (i = 0; i < mcount; i++) {
801 unsigned char *mrdatabegin = mcurrent;
802 order = mcurrent[2] * 256 + mcurrent[3];
803 INSIST(order < mcount);
804 rdata_from_slab(&mcurrent, rdclass, type, &mrdata);
806 for (count = 0; count < scount; count++) {
807 dns_rdata_reset(&srdata);
808 rdata_from_slab(&scurrent, rdclass, type, &srdata);
809 if (dns_rdata_compare(&mrdata, &srdata) == 0)
812 if (count == scount) {
814 * This rdata isn't in the sslab, and thus should be
815 * copied to the tslab.
817 unsigned int length = mcurrent - mrdatabegin;
818 offsettable[order] = tcurrent - offsetbase;
819 memcpy(tcurrent, mrdatabegin, length);
822 dns_rdata_reset(&mrdata);
825 fillin_offsets(offsetbase, offsettable, mcount);
827 isc_mem_put(mctx, offsettable, mcount * sizeof(unsigned int));
829 INSIST(tcurrent == tstart + tlength);
833 return (ISC_R_SUCCESS);
837 dns_rdataslab_equal(unsigned char *slab1, unsigned char *slab2,
838 unsigned int reservelen)
840 unsigned char *current1, *current2;
841 unsigned int count1, count2;
842 unsigned int length1, length2;
844 current1 = slab1 + reservelen;
845 count1 = *current1++ * 256;
846 count1 += *current1++;
848 current2 = slab2 + reservelen;
849 count2 = *current2++ * 256;
850 count2 += *current2++;
852 if (count1 != count2)
855 current1 += (4 * count1);
856 current2 += (4 * count2);
859 length1 = *current1++ * 256;
860 length1 += *current1++;
862 length2 = *current2++ * 256;
863 length2 += *current2++;
868 if (length1 != length2 ||
869 memcmp(current1, current2, length1) != 0)
881 dns_rdataslab_equalx(unsigned char *slab1, unsigned char *slab2,
882 unsigned int reservelen, dns_rdataclass_t rdclass,
883 dns_rdatatype_t type)
885 unsigned char *current1, *current2;
886 unsigned int count1, count2;
887 dns_rdata_t rdata1 = DNS_RDATA_INIT;
888 dns_rdata_t rdata2 = DNS_RDATA_INIT;
890 current1 = slab1 + reservelen;
891 count1 = *current1++ * 256;
892 count1 += *current1++;
894 current2 = slab2 + reservelen;
895 count2 = *current2++ * 256;
896 count2 += *current2++;
898 if (count1 != count2)
901 current1 += (4 * count1);
902 current2 += (4 * count2);
904 while (count1-- > 0) {
905 rdata_from_slab(¤t1, rdclass, type, &rdata1);
906 rdata_from_slab(¤t2, rdclass, type, &rdata2);
907 if (dns_rdata_compare(&rdata1, &rdata2) != 0)
909 dns_rdata_reset(&rdata1);
910 dns_rdata_reset(&rdata2);