2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001, 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: rdataset.c,v 1.58.2.5 2004/03/09 06:11:06 marka Exp $ */
24 #include <isc/buffer.h>
26 #include <isc/random.h>
30 #include <dns/ncache.h>
31 #include <dns/rdata.h>
32 #include <dns/rdataset.h>
33 #include <dns/compress.h>
36 dns_rdataset_init(dns_rdataset_t *rdataset) {
39 * Make 'rdataset' a valid, disassociated rdataset.
42 REQUIRE(rdataset != NULL);
44 rdataset->magic = DNS_RDATASET_MAGIC;
45 rdataset->methods = NULL;
46 ISC_LINK_INIT(rdataset, link);
47 rdataset->rdclass = 0;
52 rdataset->attributes = 0;
53 rdataset->private1 = NULL;
54 rdataset->private2 = NULL;
55 rdataset->private3 = NULL;
56 rdataset->privateuint4 = 0;
57 rdataset->private5 = NULL;
61 dns_rdataset_invalidate(dns_rdataset_t *rdataset) {
64 * Invalidate 'rdataset'.
67 REQUIRE(DNS_RDATASET_VALID(rdataset));
68 REQUIRE(rdataset->methods == NULL);
71 ISC_LINK_INIT(rdataset, link);
72 rdataset->rdclass = 0;
77 rdataset->attributes = 0;
78 rdataset->private1 = NULL;
79 rdataset->private2 = NULL;
80 rdataset->private3 = NULL;
81 rdataset->privateuint4 = 0;
82 rdataset->private5 = NULL;
86 dns_rdataset_disassociate(dns_rdataset_t *rdataset) {
89 * Disassociate 'rdataset' from its rdata, allowing it to be reused.
92 REQUIRE(DNS_RDATASET_VALID(rdataset));
93 REQUIRE(rdataset->methods != NULL);
95 (rdataset->methods->disassociate)(rdataset);
96 rdataset->methods = NULL;
97 ISC_LINK_INIT(rdataset, link);
98 rdataset->rdclass = 0;
102 rdataset->covers = 0;
103 rdataset->attributes = 0;
104 rdataset->private1 = NULL;
105 rdataset->private2 = NULL;
106 rdataset->private3 = NULL;
107 rdataset->privateuint4 = 0;
108 rdataset->private5 = NULL;
112 dns_rdataset_isassociated(dns_rdataset_t *rdataset) {
114 * Is 'rdataset' associated?
117 REQUIRE(DNS_RDATASET_VALID(rdataset));
119 if (rdataset->methods != NULL)
126 question_disassociate(dns_rdataset_t *rdataset) {
131 question_cursor(dns_rdataset_t *rdataset) {
134 return (ISC_R_NOMORE);
138 question_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {
140 * This routine should never be called.
149 question_clone(dns_rdataset_t *source, dns_rdataset_t *target) {
154 question_count(dns_rdataset_t *rdataset) {
156 * This routine should never be called.
164 static dns_rdatasetmethods_t question_methods = {
165 question_disassociate,
174 dns_rdataset_makequestion(dns_rdataset_t *rdataset, dns_rdataclass_t rdclass,
175 dns_rdatatype_t type)
179 * Make 'rdataset' a valid, associated, question rdataset, with a
180 * question class of 'rdclass' and type 'type'.
183 REQUIRE(DNS_RDATASET_VALID(rdataset));
184 REQUIRE(rdataset->methods == NULL);
186 rdataset->methods = &question_methods;
187 rdataset->rdclass = rdclass;
188 rdataset->type = type;
189 rdataset->attributes |= DNS_RDATASETATTR_QUESTION;
193 dns_rdataset_count(dns_rdataset_t *rdataset) {
196 * Return the number of records in 'rdataset'.
199 REQUIRE(DNS_RDATASET_VALID(rdataset));
200 REQUIRE(rdataset->methods != NULL);
202 return ((rdataset->methods->count)(rdataset));
206 dns_rdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target) {
209 * Make 'target' refer to the same rdataset as 'source'.
212 REQUIRE(DNS_RDATASET_VALID(source));
213 REQUIRE(source->methods != NULL);
214 REQUIRE(DNS_RDATASET_VALID(target));
215 REQUIRE(target->methods == NULL);
217 (source->methods->clone)(source, target);
221 dns_rdataset_first(dns_rdataset_t *rdataset) {
224 * Move the rdata cursor to the first rdata in the rdataset (if any).
227 REQUIRE(DNS_RDATASET_VALID(rdataset));
228 REQUIRE(rdataset->methods != NULL);
230 return ((rdataset->methods->first)(rdataset));
234 dns_rdataset_next(dns_rdataset_t *rdataset) {
237 * Move the rdata cursor to the next rdata in the rdataset (if any).
240 REQUIRE(DNS_RDATASET_VALID(rdataset));
241 REQUIRE(rdataset->methods != NULL);
243 return ((rdataset->methods->next)(rdataset));
247 dns_rdataset_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {
250 * Make 'rdata' refer to the current rdata.
253 REQUIRE(DNS_RDATASET_VALID(rdataset));
254 REQUIRE(rdataset->methods != NULL);
256 (rdataset->methods->current)(rdataset, rdata);
259 #define MAX_SHUFFLE 32
260 #define WANT_FIXED(r) (((r)->attributes & DNS_RDATASETATTR_FIXEDORDER) != 0)
268 towire_compare(const void *av, const void *bv) {
269 const struct towire_sort *a = (const struct towire_sort *) av;
270 const struct towire_sort *b = (const struct towire_sort *) bv;
271 return (a->key - b->key);
275 towiresorted(dns_rdataset_t *rdataset, dns_name_t *owner_name,
276 dns_compress_t *cctx, isc_buffer_t *target,
277 dns_rdatasetorderfunc_t order, void *order_arg,
278 isc_boolean_t partial, unsigned int *countp,
281 dns_rdata_t rdata = DNS_RDATA_INIT;
284 unsigned int i, count, added;
285 isc_buffer_t savedbuffer, rdlen, rrbuffer;
286 unsigned int headlen;
287 isc_boolean_t question = ISC_FALSE;
288 isc_boolean_t shuffle = ISC_FALSE;
289 dns_rdata_t *shuffled = NULL, shuffled_fixed[MAX_SHUFFLE];
290 struct towire_sort *sorted = NULL, sorted_fixed[MAX_SHUFFLE];
295 * Convert 'rdataset' to wire format, compressing names as specified
296 * in cctx, and storing the result in 'target'.
299 REQUIRE(DNS_RDATASET_VALID(rdataset));
300 REQUIRE(countp != NULL);
301 REQUIRE((order == NULL) == (order_arg == NULL));
302 REQUIRE(cctx != NULL && cctx->mctx != NULL);
305 if ((rdataset->attributes & DNS_RDATASETATTR_QUESTION) != 0) {
308 result = dns_rdataset_first(rdataset);
309 INSIST(result == ISC_R_NOMORE);
310 } else if (rdataset->type == 0) {
312 * This is a negative caching rdataset.
314 return (dns_ncache_towire(rdataset, cctx, target, countp));
316 count = (rdataset->methods->count)(rdataset);
317 result = dns_rdataset_first(rdataset);
318 if (result == ISC_R_NOMORE)
319 return (ISC_R_SUCCESS);
320 if (result != ISC_R_SUCCESS)
325 * Do we want to shuffle this anwer?
327 if (!question && count > 1 &&
328 (!WANT_FIXED(rdataset) || order != NULL) &&
329 rdataset->type != dns_rdatatype_sig)
332 if (shuffle && count > MAX_SHUFFLE) {
333 shuffled = isc_mem_get(cctx->mctx, count * sizeof(*shuffled));
334 sorted = isc_mem_get(cctx->mctx, count * sizeof(*sorted));
335 if (shuffled == NULL || sorted == NULL)
338 shuffled = shuffled_fixed;
339 sorted = sorted_fixed;
344 * First we get handles to all of the rdata.
349 dns_rdata_init(&shuffled[i]);
350 dns_rdataset_current(rdataset, &shuffled[i]);
352 result = dns_rdataset_next(rdataset);
353 } while (result == ISC_R_SUCCESS);
354 if (result != ISC_R_NOMORE)
364 for (i = 0; i < count; i++) {
365 sorted[i].key = (*order)(&shuffled[i],
367 sorted[i].rdata = &shuffled[i];
369 qsort(sorted, count, sizeof(sorted[0]),
378 isc_random_get(&val);
380 for (i = 0; i < count; i++) {
381 sorted[j].key = 0; /* Unused */
382 sorted[j].rdata = &shuffled[i];
385 j = 0; /* Wrap around. */
390 savedbuffer = *target;
396 * Copy out the name, type, class, ttl.
400 dns_compress_setmethods(cctx, DNS_COMPRESS_GLOBAL14);
401 result = dns_name_towire(owner_name, cctx, target);
402 if (result != ISC_R_SUCCESS)
404 headlen = sizeof(dns_rdataclass_t) + sizeof(dns_rdatatype_t);
406 headlen += sizeof(dns_ttl_t)
407 + 2; /* XXX 2 for rdata len */
408 isc_buffer_availableregion(target, &r);
409 if (r.length < headlen) {
410 result = ISC_R_NOSPACE;
413 isc_buffer_putuint16(target, rdataset->type);
414 isc_buffer_putuint16(target, rdataset->rdclass);
416 isc_buffer_putuint32(target, rdataset->ttl);
419 * Save space for rdlen.
422 isc_buffer_add(target, 2);
428 rdata = *(sorted[i].rdata);
430 dns_rdata_reset(&rdata);
431 dns_rdataset_current(rdataset, &rdata);
433 result = dns_rdata_towire(&rdata, cctx, target);
434 if (result != ISC_R_SUCCESS)
436 INSIST((target->used >= rdlen.used + 2) &&
437 (target->used - rdlen.used - 2 < 65536));
438 isc_buffer_putuint16(&rdlen,
439 (isc_uint16_t)(target->used -
447 result = ISC_R_NOMORE;
449 result = ISC_R_SUCCESS;
451 result = dns_rdataset_next(rdataset);
453 } while (result == ISC_R_SUCCESS);
455 if (result != ISC_R_NOMORE)
460 result = ISC_R_SUCCESS;
464 if (partial && result == ISC_R_NOSPACE) {
465 INSIST(rrbuffer.used < 65536);
466 dns_compress_rollback(cctx, (isc_uint16_t)rrbuffer.used);
471 INSIST(savedbuffer.used < 65536);
472 dns_compress_rollback(cctx, (isc_uint16_t)savedbuffer.used);
474 *target = savedbuffer;
477 if (sorted != NULL && sorted != sorted_fixed)
478 isc_mem_put(cctx->mctx, sorted, count * sizeof(*sorted));
479 if (shuffled != NULL && shuffled != shuffled_fixed)
480 isc_mem_put(cctx->mctx, shuffled, count * sizeof(*shuffled));
485 dns_rdataset_towiresorted(dns_rdataset_t *rdataset,
486 dns_name_t *owner_name,
487 dns_compress_t *cctx,
488 isc_buffer_t *target,
489 dns_rdatasetorderfunc_t order,
491 unsigned int *countp)
493 return (towiresorted(rdataset, owner_name, cctx, target,
494 order, order_arg, ISC_FALSE, countp, NULL));
498 dns_rdataset_towirepartial(dns_rdataset_t *rdataset,
499 dns_name_t *owner_name,
500 dns_compress_t *cctx,
501 isc_buffer_t *target,
502 dns_rdatasetorderfunc_t order,
504 unsigned int *countp,
507 REQUIRE(state == NULL); /* XXX remove when implemented */
508 return (towiresorted(rdataset, owner_name, cctx, target,
509 order, order_arg, ISC_TRUE, countp, state));
513 dns_rdataset_towire(dns_rdataset_t *rdataset,
514 dns_name_t *owner_name,
515 dns_compress_t *cctx,
516 isc_buffer_t *target,
517 unsigned int *countp)
519 return (towiresorted(rdataset, owner_name, cctx, target,
520 NULL, NULL, ISC_FALSE, countp, NULL));
524 dns_rdataset_additionaldata(dns_rdataset_t *rdataset,
525 dns_additionaldatafunc_t add, void *arg)
527 dns_rdata_t rdata = DNS_RDATA_INIT;
531 * For each rdata in rdataset, call 'add' for each name and type in the
532 * rdata which is subject to additional section processing.
535 REQUIRE(DNS_RDATASET_VALID(rdataset));
536 REQUIRE((rdataset->attributes & DNS_RDATASETATTR_QUESTION) == 0);
538 result = dns_rdataset_first(rdataset);
539 if (result != ISC_R_SUCCESS)
543 dns_rdataset_current(rdataset, &rdata);
544 result = dns_rdata_additionaldata(&rdata, add, arg);
545 if (result == ISC_R_SUCCESS)
546 result = dns_rdataset_next(rdataset);
547 dns_rdata_reset(&rdata);
548 } while (result == ISC_R_SUCCESS);
550 if (result != ISC_R_NOMORE)
553 return (ISC_R_SUCCESS);