rdataset.c revision e7857b5ee05414961bb11f9e57f654163fae6acb
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley/*
7d98a1783f222964bcde7d56dab77b822706204dBob Halley * Copyright (C) 2004-2012 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Copyright (C) 1999-2003 Internet Software Consortium.
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley *
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * Permission to use, copy, modify, and/or distribute this software for any
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * purpose with or without fee is hereby granted, provided that the above
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * copyright notice and this permission notice appear in all copies.
15a44745412679c30a6d022733925af70a38b715David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
15a44745412679c30a6d022733925af70a38b715David Lawrence * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15a44745412679c30a6d022733925af70a38b715David Lawrence * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15a44745412679c30a6d022733925af70a38b715David Lawrence * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15a44745412679c30a6d022733925af70a38b715David Lawrence * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15a44745412679c30a6d022733925af70a38b715David Lawrence * PERFORMANCE OF THIS SOFTWARE.
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley */
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
8f7cae3d7b0c122c3b17e8409bbb80005433acd2Brian Wellington/* $Id$ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley/*! \file */
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
8f7cae3d7b0c122c3b17e8409bbb80005433acd2Brian Wellington#include <config.h>
8f7cae3d7b0c122c3b17e8409bbb80005433acd2Brian Wellington
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley#include <stdlib.h>
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley#include <isc/buffer.h>
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley#include <isc/mem.h>
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley#include <isc/random.h>
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley#include <isc/serial.h>
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley#include <isc/util.h>
5fe5a0c02634eaadfcbc3528bf2c184557110a3bAndreas Gustafsson
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley#include <dns/name.h>
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley#include <dns/ncache.h>
8327c62a49a2487d29a37acbed6b602e629fc0eeAndreas Gustafsson#include <dns/rdata.h>
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley#include <dns/rdataset.h>
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley#include <dns/compress.h>
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halleystatic const char *trustnames[] = {
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley "none",
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley "pending-additional",
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley "pending-answer",
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley "additional",
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley "glue",
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley "answer",
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley "authauthority",
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley "authanswer",
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley "secure",
8327c62a49a2487d29a37acbed6b602e629fc0eeAndreas Gustafsson "local" /* aka ultimate */
8327c62a49a2487d29a37acbed6b602e629fc0eeAndreas Gustafsson};
8327c62a49a2487d29a37acbed6b602e629fc0eeAndreas Gustafsson
608f870f4821972313eadc5388a42fa55b6279d1Mark Andrewsconst char *
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellingtondns_trust_totext(dns_trust_t trust) {
878d3073b13833ee1a50dfeabf8e400b6fdfc754Brian Wellington if (trust >= sizeof(trustnames)/sizeof(*trustnames))
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley return ("bad");
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley return (trustnames[trust]);
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley}
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafssonvoid
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halleydns_rdataset_init(dns_rdataset_t *rdataset) {
edcd1247ad7e81bb8b430e610d9718f64c70f05dDavid Lawrence
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley /*
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * Make 'rdataset' a valid, disassociated rdataset.
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley */
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
8582a1e113c13886ccbd1b534d6c240315767be6Bob Halley REQUIRE(rdataset != NULL);
8582a1e113c13886ccbd1b534d6c240315767be6Bob Halley
8582a1e113c13886ccbd1b534d6c240315767be6Bob Halley rdataset->magic = DNS_RDATASET_MAGIC;
edcd1247ad7e81bb8b430e610d9718f64c70f05dDavid Lawrence rdataset->methods = NULL;
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley ISC_LINK_INIT(rdataset, link);
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley rdataset->rdclass = 0;
edcd1247ad7e81bb8b430e610d9718f64c70f05dDavid Lawrence rdataset->type = 0;
edcd1247ad7e81bb8b430e610d9718f64c70f05dDavid Lawrence rdataset->ttl = 0;
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley rdataset->trust = 0;
c90f5e8d1edbd5c277f2ee320167a12a30ba7c7bMichael Graff rdataset->covers = 0;
edcd1247ad7e81bb8b430e610d9718f64c70f05dDavid Lawrence rdataset->attributes = 0;
c90f5e8d1edbd5c277f2ee320167a12a30ba7c7bMichael Graff rdataset->count = ISC_UINT32_MAX;
8582a1e113c13886ccbd1b534d6c240315767be6Bob Halley rdataset->private1 = NULL;
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson rdataset->private2 = NULL;
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson rdataset->private3 = NULL;
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson rdataset->privateuint4 = 0;
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson rdataset->private5 = NULL;
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson rdataset->private6 = NULL;
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson rdataset->resign = 0;
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson}
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafssonvoid
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafssondns_rdataset_invalidate(dns_rdataset_t *rdataset) {
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson /*
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson * Invalidate 'rdataset'.
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson */
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson REQUIRE(DNS_RDATASET_VALID(rdataset));
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson REQUIRE(rdataset->methods == NULL);
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson rdataset->magic = 0;
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson ISC_LINK_INIT(rdataset, link);
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson rdataset->rdclass = 0;
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson rdataset->type = 0;
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson rdataset->ttl = 0;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence rdataset->trust = 0;
8582a1e113c13886ccbd1b534d6c240315767be6Bob Halley rdataset->covers = 0;
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson rdataset->attributes = 0;
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson rdataset->count = ISC_UINT32_MAX;
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson rdataset->private1 = NULL;
8582a1e113c13886ccbd1b534d6c240315767be6Bob Halley rdataset->private2 = NULL;
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson rdataset->private3 = NULL;
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson rdataset->privateuint4 = 0;
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson rdataset->private5 = NULL;
44fee668021c7ceef4ee1c848031d883a508b359James Brister}
44fee668021c7ceef4ee1c848031d883a508b359James Brister
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafssonvoid
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafssondns_rdataset_disassociate(dns_rdataset_t *rdataset) {
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson /*
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson * Disassociate 'rdataset' from its rdata, allowing it to be reused.
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson */
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson REQUIRE(DNS_RDATASET_VALID(rdataset));
44fee668021c7ceef4ee1c848031d883a508b359James Brister REQUIRE(rdataset->methods != NULL);
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson (rdataset->methods->disassociate)(rdataset);
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson rdataset->methods = NULL;
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson ISC_LINK_INIT(rdataset, link);
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson rdataset->rdclass = 0;
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson rdataset->type = 0;
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson rdataset->ttl = 0;
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson rdataset->trust = 0;
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson rdataset->covers = 0;
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson rdataset->attributes = 0;
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson rdataset->count = ISC_UINT32_MAX;
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson rdataset->private1 = NULL;
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson rdataset->private2 = NULL;
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson rdataset->private3 = NULL;
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson rdataset->privateuint4 = 0;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence rdataset->private5 = NULL;
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson rdataset->private6 = NULL;
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson}
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafssonisc_boolean_t
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafssondns_rdataset_isassociated(dns_rdataset_t *rdataset) {
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson /*
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson * Is 'rdataset' associated?
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson */
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson REQUIRE(DNS_RDATASET_VALID(rdataset));
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson if (rdataset->methods != NULL)
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson return (ISC_TRUE);
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson return (ISC_FALSE);
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson}
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafssonstatic void
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafssonquestion_disassociate(dns_rdataset_t *rdataset) {
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson UNUSED(rdataset);
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson}
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafssonstatic isc_result_t
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafssonquestion_cursor(dns_rdataset_t *rdataset) {
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson UNUSED(rdataset);
5542df09597c479be604da0ece8271cbc6fd9c4aDavid Lawrence
5542df09597c479be604da0ece8271cbc6fd9c4aDavid Lawrence return (ISC_R_NOMORE);
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley}
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafssonstatic void
5542df09597c479be604da0ece8271cbc6fd9c4aDavid Lawrencequestion_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {
5542df09597c479be604da0ece8271cbc6fd9c4aDavid Lawrence /*
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * This routine should never be called.
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley */
8582a1e113c13886ccbd1b534d6c240315767be6Bob Halley UNUSED(rdataset);
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson UNUSED(rdata);
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley REQUIRE(0);
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley}
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halleystatic void
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halleyquestion_clone(dns_rdataset_t *source, dns_rdataset_t *target) {
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley *target = *source;
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley}
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley
static unsigned int
question_count(dns_rdataset_t *rdataset) {
/*
* This routine should never be called.
*/
UNUSED(rdataset);
REQUIRE(0);
return (0);
}
static dns_rdatasetmethods_t question_methods = {
question_disassociate,
question_cursor,
question_cursor,
question_current,
question_clone,
question_count,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
void
dns_rdataset_makequestion(dns_rdataset_t *rdataset, dns_rdataclass_t rdclass,
dns_rdatatype_t type)
{
/*
* Make 'rdataset' a valid, associated, question rdataset, with a
* question class of 'rdclass' and type 'type'.
*/
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE(rdataset->methods == NULL);
rdataset->methods = &question_methods;
rdataset->rdclass = rdclass;
rdataset->type = type;
rdataset->attributes |= DNS_RDATASETATTR_QUESTION;
}
unsigned int
dns_rdataset_count(dns_rdataset_t *rdataset) {
/*
* Return the number of records in 'rdataset'.
*/
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE(rdataset->methods != NULL);
return ((rdataset->methods->count)(rdataset));
}
void
dns_rdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target) {
/*
* Make 'target' refer to the same rdataset as 'source'.
*/
REQUIRE(DNS_RDATASET_VALID(source));
REQUIRE(source->methods != NULL);
REQUIRE(DNS_RDATASET_VALID(target));
REQUIRE(target->methods == NULL);
(source->methods->clone)(source, target);
}
isc_result_t
dns_rdataset_first(dns_rdataset_t *rdataset) {
/*
* Move the rdata cursor to the first rdata in the rdataset (if any).
*/
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE(rdataset->methods != NULL);
return ((rdataset->methods->first)(rdataset));
}
isc_result_t
dns_rdataset_next(dns_rdataset_t *rdataset) {
/*
* Move the rdata cursor to the next rdata in the rdataset (if any).
*/
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE(rdataset->methods != NULL);
return ((rdataset->methods->next)(rdataset));
}
void
dns_rdataset_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {
/*
* Make 'rdata' refer to the current rdata.
*/
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE(rdataset->methods != NULL);
(rdataset->methods->current)(rdataset, rdata);
}
#define MAX_SHUFFLE 32
#define WANT_FIXED(r) (((r)->attributes & DNS_RDATASETATTR_FIXEDORDER) != 0)
#define WANT_RANDOM(r) (((r)->attributes & DNS_RDATASETATTR_RANDOMIZE) != 0)
struct towire_sort {
int key;
dns_rdata_t *rdata;
};
static int
towire_compare(const void *av, const void *bv) {
const struct towire_sort *a = (const struct towire_sort *) av;
const struct towire_sort *b = (const struct towire_sort *) bv;
return (a->key - b->key);
}
static isc_result_t
towiresorted(dns_rdataset_t *rdataset, const dns_name_t *owner_name,
dns_compress_t *cctx, isc_buffer_t *target,
dns_rdatasetorderfunc_t order, const void *order_arg,
isc_boolean_t partial, unsigned int options,
unsigned int *countp, void **state)
{
dns_rdata_t rdata = DNS_RDATA_INIT;
isc_region_t r;
isc_result_t result;
unsigned int i, count = 0, added, choice;
isc_buffer_t savedbuffer, rdlen, rrbuffer;
unsigned int headlen;
isc_boolean_t question = ISC_FALSE;
isc_boolean_t shuffle = ISC_FALSE;
dns_rdata_t *shuffled = NULL, shuffled_fixed[MAX_SHUFFLE];
struct towire_sort *sorted = NULL, sorted_fixed[MAX_SHUFFLE];
UNUSED(state);
/*
* Convert 'rdataset' to wire format, compressing names as specified
* in cctx, and storing the result in 'target'.
*/
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE(countp != NULL);
REQUIRE((order == NULL) == (order_arg == NULL));
REQUIRE(cctx != NULL && cctx->mctx != NULL);
if ((rdataset->attributes & DNS_RDATASETATTR_QUESTION) != 0) {
question = ISC_TRUE;
count = 1;
result = dns_rdataset_first(rdataset);
INSIST(result == ISC_R_NOMORE);
} else if ((rdataset->attributes & DNS_RDATASETATTR_NEGATIVE) != 0) {
/*
* This is a negative caching rdataset.
*/
unsigned int ncache_opts = 0;
if ((options & DNS_RDATASETTOWIRE_OMITDNSSEC) != 0)
ncache_opts |= DNS_NCACHETOWIRE_OMITDNSSEC;
return (dns_ncache_towire(rdataset, cctx, target, ncache_opts,
countp));
} else {
count = (rdataset->methods->count)(rdataset);
result = dns_rdataset_first(rdataset);
if (result == ISC_R_NOMORE)
return (ISC_R_SUCCESS);
if (result != ISC_R_SUCCESS)
return (result);
}
/*
* Do we want to shuffle this answer?
*/
if (!question && count > 1 &&
(!WANT_FIXED(rdataset) || order != NULL) &&
rdataset->type != dns_rdatatype_rrsig)
shuffle = ISC_TRUE;
if (shuffle && count > MAX_SHUFFLE) {
shuffled = isc_mem_get(cctx->mctx, count * sizeof(*shuffled));
sorted = isc_mem_get(cctx->mctx, count * sizeof(*sorted));
if (shuffled == NULL || sorted == NULL)
shuffle = ISC_FALSE;
} else {
shuffled = shuffled_fixed;
sorted = sorted_fixed;
}
if (shuffle) {
/*
* First we get handles to all of the rdata.
*/
i = 0;
do {
INSIST(i < count);
dns_rdata_init(&shuffled[i]);
dns_rdataset_current(rdataset, &shuffled[i]);
i++;
result = dns_rdataset_next(rdataset);
} while (result == ISC_R_SUCCESS);
if (result != ISC_R_NOMORE)
goto cleanup;
INSIST(i == count);
/*
* Now we shuffle.
*/
if (WANT_FIXED(rdataset)) {
/*
* 'Fixed' order.
*/
INSIST(order != NULL);
for (i = 0; i < count; i++) {
sorted[i].key = (*order)(&shuffled[i],
order_arg);
sorted[i].rdata = &shuffled[i];
}
} else if (WANT_RANDOM(rdataset)) {
/*
* 'Random' order.
*/
for (i = 0; i < count; i++) {
dns_rdata_t rdata;
isc_uint32_t val;
isc_random_get(&val);
choice = i + (val % (count - i));
rdata = shuffled[i];
shuffled[i] = shuffled[choice];
shuffled[choice] = rdata;
if (order != NULL)
sorted[i].key = (*order)(&shuffled[i],
order_arg);
else
sorted[i].key = 0; /* Unused */
sorted[i].rdata = &shuffled[i];
}
} else {
/*
* "Cyclic" order.
*/
isc_uint32_t val;
unsigned int j;
val = rdataset->count;
if (val == ISC_UINT32_MAX)
isc_random_get(&val);
j = val % count;
for (i = 0; i < count; i++) {
if (order != NULL)
sorted[i].key = (*order)(&shuffled[j],
order_arg);
else
sorted[i].key = 0; /* Unused */
sorted[i].rdata = &shuffled[j];
j++;
if (j == count)
j = 0; /* Wrap around. */
}
}
/*
* Sorted order.
*/
if (order != NULL)
qsort(sorted, count, sizeof(sorted[0]),
towire_compare);
}
savedbuffer = *target;
i = 0;
added = 0;
do {
/*
* Copy out the name, type, class, ttl.
*/
rrbuffer = *target;
dns_compress_setmethods(cctx, DNS_COMPRESS_GLOBAL14);
result = dns_name_towire(owner_name, cctx, target);
if (result != ISC_R_SUCCESS)
goto rollback;
headlen = sizeof(dns_rdataclass_t) + sizeof(dns_rdatatype_t);
if (!question)
headlen += sizeof(dns_ttl_t)
+ 2; /* XXX 2 for rdata len */
isc_buffer_availableregion(target, &r);
if (r.length < headlen) {
result = ISC_R_NOSPACE;
goto rollback;
}
isc_buffer_putuint16(target, rdataset->type);
isc_buffer_putuint16(target, rdataset->rdclass);
if (!question) {
isc_buffer_putuint32(target, rdataset->ttl);
/*
* Save space for rdlen.
*/
rdlen = *target;
isc_buffer_add(target, 2);
/*
* Copy out the rdata
*/
if (shuffle)
rdata = *(sorted[i].rdata);
else {
dns_rdata_reset(&rdata);
dns_rdataset_current(rdataset, &rdata);
}
result = dns_rdata_towire(&rdata, cctx, target);
if (result != ISC_R_SUCCESS)
goto rollback;
INSIST((target->used >= rdlen.used + 2) &&
(target->used - rdlen.used - 2 < 65536));
isc_buffer_putuint16(&rdlen,
(isc_uint16_t)(target->used -
rdlen.used - 2));
added++;
}
if (shuffle) {
i++;
if (i == count)
result = ISC_R_NOMORE;
else
result = ISC_R_SUCCESS;
} else {
result = dns_rdataset_next(rdataset);
}
} while (result == ISC_R_SUCCESS);
if (result != ISC_R_NOMORE)
goto rollback;
*countp += count;
result = ISC_R_SUCCESS;
goto cleanup;
rollback:
if (partial && result == ISC_R_NOSPACE) {
INSIST(rrbuffer.used < 65536);
dns_compress_rollback(cctx, (isc_uint16_t)rrbuffer.used);
*countp += added;
*target = rrbuffer;
goto cleanup;
}
INSIST(savedbuffer.used < 65536);
dns_compress_rollback(cctx, (isc_uint16_t)savedbuffer.used);
*countp = 0;
*target = savedbuffer;
cleanup:
if (sorted != NULL && sorted != sorted_fixed)
isc_mem_put(cctx->mctx, sorted, count * sizeof(*sorted));
if (shuffled != NULL && shuffled != shuffled_fixed)
isc_mem_put(cctx->mctx, shuffled, count * sizeof(*shuffled));
return (result);
}
isc_result_t
dns_rdataset_towiresorted(dns_rdataset_t *rdataset,
const dns_name_t *owner_name,
dns_compress_t *cctx,
isc_buffer_t *target,
dns_rdatasetorderfunc_t order,
const void *order_arg,
unsigned int options,
unsigned int *countp)
{
return (towiresorted(rdataset, owner_name, cctx, target,
order, order_arg, ISC_FALSE, options,
countp, NULL));
}
isc_result_t
dns_rdataset_towirepartial(dns_rdataset_t *rdataset,
const dns_name_t *owner_name,
dns_compress_t *cctx,
isc_buffer_t *target,
dns_rdatasetorderfunc_t order,
const void *order_arg,
unsigned int options,
unsigned int *countp,
void **state)
{
REQUIRE(state == NULL); /* XXX remove when implemented */
return (towiresorted(rdataset, owner_name, cctx, target,
order, order_arg, ISC_TRUE, options,
countp, state));
}
isc_result_t
dns_rdataset_towire(dns_rdataset_t *rdataset,
dns_name_t *owner_name,
dns_compress_t *cctx,
isc_buffer_t *target,
unsigned int options,
unsigned int *countp)
{
return (towiresorted(rdataset, owner_name, cctx, target,
NULL, NULL, ISC_FALSE, options, countp, NULL));
}
isc_result_t
dns_rdataset_additionaldata(dns_rdataset_t *rdataset,
dns_additionaldatafunc_t add, void *arg)
{
dns_rdata_t rdata = DNS_RDATA_INIT;
isc_result_t result;
/*
* For each rdata in rdataset, call 'add' for each name and type in the
* rdata which is subject to additional section processing.
*/
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE((rdataset->attributes & DNS_RDATASETATTR_QUESTION) == 0);
result = dns_rdataset_first(rdataset);
if (result != ISC_R_SUCCESS)
return (result);
do {
dns_rdataset_current(rdataset, &rdata);
result = dns_rdata_additionaldata(&rdata, add, arg);
if (result == ISC_R_SUCCESS)
result = dns_rdataset_next(rdataset);
dns_rdata_reset(&rdata);
} while (result == ISC_R_SUCCESS);
if (result != ISC_R_NOMORE)
return (result);
return (ISC_R_SUCCESS);
}
isc_result_t
dns_rdataset_addnoqname(dns_rdataset_t *rdataset, dns_name_t *name) {
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE(rdataset->methods != NULL);
if (rdataset->methods->addnoqname == NULL)
return (ISC_R_NOTIMPLEMENTED);
return((rdataset->methods->addnoqname)(rdataset, name));
}
isc_result_t
dns_rdataset_getnoqname(dns_rdataset_t *rdataset, dns_name_t *name,
dns_rdataset_t *neg, dns_rdataset_t *negsig)
{
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE(rdataset->methods != NULL);
if (rdataset->methods->getnoqname == NULL)
return (ISC_R_NOTIMPLEMENTED);
return((rdataset->methods->getnoqname)(rdataset, name, neg, negsig));
}
isc_result_t
dns_rdataset_addclosest(dns_rdataset_t *rdataset, dns_name_t *name) {
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE(rdataset->methods != NULL);
if (rdataset->methods->addclosest == NULL)
return (ISC_R_NOTIMPLEMENTED);
return((rdataset->methods->addclosest)(rdataset, name));
}
isc_result_t
dns_rdataset_getclosest(dns_rdataset_t *rdataset, dns_name_t *name,
dns_rdataset_t *neg, dns_rdataset_t *negsig)
{
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE(rdataset->methods != NULL);
if (rdataset->methods->getclosest == NULL)
return (ISC_R_NOTIMPLEMENTED);
return((rdataset->methods->getclosest)(rdataset, name, neg, negsig));
}
/*
* Additional cache stuff
*/
isc_result_t
dns_rdataset_getadditional(dns_rdataset_t *rdataset,
dns_rdatasetadditional_t type,
dns_rdatatype_t qtype,
dns_acache_t *acache,
dns_zone_t **zonep,
dns_db_t **dbp,
dns_dbversion_t **versionp,
dns_dbnode_t **nodep,
dns_name_t *fname,
dns_message_t *msg,
isc_stdtime_t now)
{
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE(rdataset->methods != NULL);
REQUIRE(zonep == NULL || *zonep == NULL);
REQUIRE(dbp != NULL && *dbp == NULL);
REQUIRE(versionp != NULL && *versionp == NULL);
REQUIRE(nodep != NULL && *nodep == NULL);
REQUIRE(fname != NULL);
REQUIRE(msg != NULL);
if (acache != NULL && rdataset->methods->getadditional != NULL) {
return ((rdataset->methods->getadditional)(rdataset, type,
qtype, acache,
zonep, dbp,
versionp, nodep,
fname, msg, now));
}
return (ISC_R_FAILURE);
}
isc_result_t
dns_rdataset_setadditional(dns_rdataset_t *rdataset,
dns_rdatasetadditional_t type,
dns_rdatatype_t qtype,
dns_acache_t *acache,
dns_zone_t *zone,
dns_db_t *db,
dns_dbversion_t *version,
dns_dbnode_t *node,
dns_name_t *fname)
{
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE(rdataset->methods != NULL);
if (acache != NULL && rdataset->methods->setadditional != NULL) {
return ((rdataset->methods->setadditional)(rdataset, type,
qtype, acache, zone,
db, version,
node, fname));
}
return (ISC_R_FAILURE);
}
isc_result_t
dns_rdataset_putadditional(dns_acache_t *acache,
dns_rdataset_t *rdataset,
dns_rdatasetadditional_t type,
dns_rdatatype_t qtype)
{
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE(rdataset->methods != NULL);
if (acache != NULL && rdataset->methods->putadditional != NULL) {
return ((rdataset->methods->putadditional)(acache, rdataset,
type, qtype));
}
return (ISC_R_FAILURE);
}
void
dns_rdataset_settrust(dns_rdataset_t *rdataset, dns_trust_t trust) {
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE(rdataset->methods != NULL);
if (rdataset->methods->settrust != NULL)
(rdataset->methods->settrust)(rdataset, trust);
else
rdataset->trust = trust;
}
void
dns_rdataset_expire(dns_rdataset_t *rdataset) {
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE(rdataset->methods != NULL);
if (rdataset->methods->expire != NULL)
(rdataset->methods->expire)(rdataset);
}
void
dns_rdataset_trimttl(dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset,
dns_rdata_rrsig_t *rrsig, isc_stdtime_t now,
isc_boolean_t acceptexpired)
{
isc_uint32_t ttl = 0;
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE(DNS_RDATASET_VALID(sigrdataset));
REQUIRE(rrsig != NULL);
/*
* If we accept expired RRsets keep them for no more than 120 seconds.
*/
if (acceptexpired &&
(isc_serial_le(rrsig->timeexpire, ((now + 120) & 0xffffffff)) ||
isc_serial_le(rrsig->timeexpire, now)))
ttl = 120;
else if (isc_serial_ge(rrsig->timeexpire, now))
ttl = rrsig->timeexpire - now;
ttl = ISC_MIN(ISC_MIN(rdataset->ttl, sigrdataset->ttl),
ISC_MIN(rrsig->originalttl, ttl));
rdataset->ttl = ttl;
sigrdataset->ttl = ttl;
}