rdataslab.c revision cd82aa8fa32698240ecd6634f11e3052ca7a2448
d76ed813a51465e5c47d521ab09ea20c06f1428dMark Andrews/*
d76ed813a51465e5c47d521ab09ea20c06f1428dMark Andrews * Copyright (C) 1999 Internet Software Consortium.
f3cbd0e0299338f95a97e73ac9c2b0fcdca2cbfbTinderbox User *
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * Permission to use, copy, modify, and distribute this software for any
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * purpose with or without fee is hereby granted, provided that the above
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * copyright notice and this permission notice appear in all copies.
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews *
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews * SOFTWARE.
d76ed813a51465e5c47d521ab09ea20c06f1428dMark Andrews */
d76ed813a51465e5c47d521ab09ea20c06f1428dMark Andrews
d76ed813a51465e5c47d521ab09ea20c06f1428dMark Andrews/* $Id: rdataslab.c,v 1.1 1999/01/27 08:44:10 explorer Exp $ */
d76ed813a51465e5c47d521ab09ea20c06f1428dMark Andrews
68116c5a5fd36fef812fc207de3b7714db2994d5Evan Hunt#include <config.h>
68116c5a5fd36fef812fc207de3b7714db2994d5Evan Hunt
68116c5a5fd36fef812fc207de3b7714db2994d5Evan Hunt#include <isc/region.h>
d76ed813a51465e5c47d521ab09ea20c06f1428dMark Andrews#include <isc/buffer.h>
d76ed813a51465e5c47d521ab09ea20c06f1428dMark Andrews#include <isc/assertions.h>
95de316a5d99b09804eda3223d1a41623d7ed611Evan Hunt
66e50468dde42a9757ac489e738d8b2db8fd7f80Evan Hunt#include <dns/types.h>
66e50468dde42a9757ac489e738d8b2db8fd7f80Evan Hunt#include <dns/result.h>
66e50468dde42a9757ac489e738d8b2db8fd7f80Evan Hunt#include <dns/rdata.h>
66e50468dde42a9757ac489e738d8b2db8fd7f80Evan Hunt#include <dns/rdataclass.h>
66e50468dde42a9757ac489e738d8b2db8fd7f80Evan Hunt#include <dns/rdatatype.h>
66e50468dde42a9757ac489e738d8b2db8fd7f80Evan Hunt#include <dns/rdataset.h>
d76ed813a51465e5c47d521ab09ea20c06f1428dMark Andrews#include <dns/rdataslab.h>
d76ed813a51465e5c47d521ab09ea20c06f1428dMark Andrews
d76ed813a51465e5c47d521ab09ea20c06f1428dMark Andrewsdns_result_t
66e50468dde42a9757ac489e738d8b2db8fd7f80Evan Huntdns_rdataslab_fromrdataset(dns_rdataset_t *rdataset, isc_mem_t *mctx,
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt isc_region_t *region, unsigned int reservelen)
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt{
66e50468dde42a9757ac489e738d8b2db8fd7f80Evan Hunt dns_rdata_t rdata;
66e50468dde42a9757ac489e738d8b2db8fd7f80Evan Hunt unsigned char *rawbuf;
68116c5a5fd36fef812fc207de3b7714db2994d5Evan Hunt unsigned int buflen;
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt dns_result_t result;
66e50468dde42a9757ac489e738d8b2db8fd7f80Evan Hunt unsigned int nitems;
66e50468dde42a9757ac489e738d8b2db8fd7f80Evan Hunt
66e50468dde42a9757ac489e738d8b2db8fd7f80Evan Hunt result = dns_rdataset_first(rdataset);
89740699cd2191d9b84e67716c281b2dfeba5e56Evan Hunt REQUIRE(result == DNS_R_SUCCESS);
89740699cd2191d9b84e67716c281b2dfeba5e56Evan Hunt
89740699cd2191d9b84e67716c281b2dfeba5e56Evan Hunt buflen = reservelen + 2;
ab6fd5e89266a29efdafee3784d2cb06f8624b1bMark Andrews nitems = 0;
89740699cd2191d9b84e67716c281b2dfeba5e56Evan Hunt
89740699cd2191d9b84e67716c281b2dfeba5e56Evan Hunt /*
89740699cd2191d9b84e67716c281b2dfeba5e56Evan Hunt * Run through the rdataset list once, counting up the size
68116c5a5fd36fef812fc207de3b7714db2994d5Evan Hunt * of all the rdata members within it. We do not store the
89740699cd2191d9b84e67716c281b2dfeba5e56Evan Hunt * class, type, etc, just the rdata, so our overhead is 2 bytes
ab6fd5e89266a29efdafee3784d2cb06f8624b1bMark Andrews * for the number of records, and 2 for each rdata length, and
89740699cd2191d9b84e67716c281b2dfeba5e56Evan Hunt * then the rdata itself.
89740699cd2191d9b84e67716c281b2dfeba5e56Evan Hunt */
89740699cd2191d9b84e67716c281b2dfeba5e56Evan Hunt do {
89740699cd2191d9b84e67716c281b2dfeba5e56Evan Hunt dns_rdataset_current(rdataset, &rdata);
89740699cd2191d9b84e67716c281b2dfeba5e56Evan Hunt buflen += (2 + rdata.length);
89740699cd2191d9b84e67716c281b2dfeba5e56Evan Hunt
89740699cd2191d9b84e67716c281b2dfeba5e56Evan Hunt nitems++;
89740699cd2191d9b84e67716c281b2dfeba5e56Evan Hunt
89740699cd2191d9b84e67716c281b2dfeba5e56Evan Hunt result = dns_rdataset_next(rdataset);
89740699cd2191d9b84e67716c281b2dfeba5e56Evan Hunt } while (result == DNS_R_SUCCESS);
89740699cd2191d9b84e67716c281b2dfeba5e56Evan Hunt
89740699cd2191d9b84e67716c281b2dfeba5e56Evan Hunt if (result != DNS_R_NOMORE)
89740699cd2191d9b84e67716c281b2dfeba5e56Evan Hunt return (result);
66e50468dde42a9757ac489e738d8b2db8fd7f80Evan Hunt
d76ed813a51465e5c47d521ab09ea20c06f1428dMark Andrews /*
d76ed813a51465e5c47d521ab09ea20c06f1428dMark Andrews * Allocate the memory, set up a buffer, start copying in
d76ed813a51465e5c47d521ab09ea20c06f1428dMark Andrews * data.
d76ed813a51465e5c47d521ab09ea20c06f1428dMark Andrews */
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt rawbuf = isc_mem_get(mctx, buflen);
d76ed813a51465e5c47d521ab09ea20c06f1428dMark Andrews if (rawbuf == NULL)
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt return (DNS_R_NOMEMORY);
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt
6643b0dd91249ace16218ef667967c87b291992cMark Andrews region->base = rawbuf;
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt region->length = buflen;
68116c5a5fd36fef812fc207de3b7714db2994d5Evan Hunt
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt rawbuf += reservelen;
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt *rawbuf++ = (nitems & 0xff00) >> 8;
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt *rawbuf++ = (nitems & 0x00ff);
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt result = dns_rdataset_first(rdataset);
6643b0dd91249ace16218ef667967c87b291992cMark Andrews REQUIRE(result == DNS_R_SUCCESS);
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt do {
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt dns_rdataset_current(rdataset, &rdata);
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt *rawbuf++ = (rdata.length & 0xff00) >> 8;
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt *rawbuf++ = (rdata.length & 0x00ff);
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt memcpy(rawbuf, rdata.data, rdata.length);
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt rawbuf += rdata.length;
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt result = dns_rdataset_next(rdataset);
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt } while (result == DNS_R_SUCCESS);
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt if (result != DNS_R_NOMORE) {
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt isc_mem_put(mctx, region->base, region->length);
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt region->base = NULL;
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt region->length = 0;
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt return (result);
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt }
6643b0dd91249ace16218ef667967c87b291992cMark Andrews
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt return (DNS_R_SUCCESS);
68116c5a5fd36fef812fc207de3b7714db2994d5Evan Hunt}
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Huntdns_result_t
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Huntdns_rdataslab_tordataset(dns_rdataset_t *rdataset, isc_region_t *region,
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt unsigned int reservelen)
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt{
d76ed813a51465e5c47d521ab09ea20c06f1428dMark Andrews /* XXX shut up compiler */
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt rdataset = rdataset;
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt region = region;
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt reservelen = reservelen;
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt return (DNS_R_NOTIMPLEMENTED);
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt}
56c9fcf07580457442b80ac32bdb7c07aa0df870Evan Hunt