Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / dns / rdataset.c
blobf77765805cf6eb098f852a6bfd5fb4d2dd6de907
1 /*
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 $ */
20 #include <config.h>
22 #include <stdlib.h>
24 #include <isc/buffer.h>
25 #include <isc/mem.h>
26 #include <isc/random.h>
27 #include <isc/util.h>
29 #include <dns/name.h>
30 #include <dns/ncache.h>
31 #include <dns/rdata.h>
32 #include <dns/rdataset.h>
33 #include <dns/compress.h>
35 void
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;
48 rdataset->type = 0;
49 rdataset->ttl = 0;
50 rdataset->trust = 0;
51 rdataset->covers = 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;
60 void
61 dns_rdataset_invalidate(dns_rdataset_t *rdataset) {
64 * Invalidate 'rdataset'.
67 REQUIRE(DNS_RDATASET_VALID(rdataset));
68 REQUIRE(rdataset->methods == NULL);
70 rdataset->magic = 0;
71 ISC_LINK_INIT(rdataset, link);
72 rdataset->rdclass = 0;
73 rdataset->type = 0;
74 rdataset->ttl = 0;
75 rdataset->trust = 0;
76 rdataset->covers = 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;
85 void
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;
99 rdataset->type = 0;
100 rdataset->ttl = 0;
101 rdataset->trust = 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;
111 isc_boolean_t
112 dns_rdataset_isassociated(dns_rdataset_t *rdataset) {
114 * Is 'rdataset' associated?
117 REQUIRE(DNS_RDATASET_VALID(rdataset));
119 if (rdataset->methods != NULL)
120 return (ISC_TRUE);
122 return (ISC_FALSE);
125 static void
126 question_disassociate(dns_rdataset_t *rdataset) {
127 UNUSED(rdataset);
130 static isc_result_t
131 question_cursor(dns_rdataset_t *rdataset) {
132 UNUSED(rdataset);
134 return (ISC_R_NOMORE);
137 static void
138 question_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {
140 * This routine should never be called.
142 UNUSED(rdataset);
143 UNUSED(rdata);
145 REQUIRE(0);
148 static void
149 question_clone(dns_rdataset_t *source, dns_rdataset_t *target) {
150 *target = *source;
153 static unsigned int
154 question_count(dns_rdataset_t *rdataset) {
156 * This routine should never be called.
158 UNUSED(rdataset);
159 REQUIRE(0);
161 return (0);
164 static dns_rdatasetmethods_t question_methods = {
165 question_disassociate,
166 question_cursor,
167 question_cursor,
168 question_current,
169 question_clone,
170 question_count
173 void
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;
192 unsigned int
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));
205 void
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);
220 isc_result_t
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));
233 isc_result_t
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));
246 void
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)
262 struct towire_sort {
263 int key;
264 dns_rdata_t *rdata;
267 static int
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);
274 static isc_result_t
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,
279 void **state)
281 dns_rdata_t rdata = DNS_RDATA_INIT;
282 isc_region_t r;
283 isc_result_t result;
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];
292 UNUSED(state);
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);
304 count = 0;
305 if ((rdataset->attributes & DNS_RDATASETATTR_QUESTION) != 0) {
306 question = ISC_TRUE;
307 count = 1;
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));
315 } else {
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)
321 return (result);
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)
330 shuffle = ISC_TRUE;
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)
336 shuffle = ISC_FALSE;
337 } else {
338 shuffled = shuffled_fixed;
339 sorted = sorted_fixed;
342 if (shuffle) {
344 * First we get handles to all of the rdata.
346 i = 0;
347 do {
348 INSIST(i < count);
349 dns_rdata_init(&shuffled[i]);
350 dns_rdataset_current(rdataset, &shuffled[i]);
351 i++;
352 result = dns_rdataset_next(rdataset);
353 } while (result == ISC_R_SUCCESS);
354 if (result != ISC_R_NOMORE)
355 goto cleanup;
356 INSIST(i == count);
358 * Now we shuffle.
360 if (order != NULL) {
362 * Sorted order.
364 for (i = 0; i < count; i++) {
365 sorted[i].key = (*order)(&shuffled[i],
366 order_arg);
367 sorted[i].rdata = &shuffled[i];
369 qsort(sorted, count, sizeof(sorted[0]),
370 towire_compare);
371 } else {
373 * "Cyclic" order.
375 isc_uint32_t val;
376 unsigned int j;
378 isc_random_get(&val);
379 j = val % count;
380 for (i = 0; i < count; i++) {
381 sorted[j].key = 0; /* Unused */
382 sorted[j].rdata = &shuffled[i];
383 j++;
384 if (j == count)
385 j = 0; /* Wrap around. */
390 savedbuffer = *target;
391 i = 0;
392 added = 0;
394 do {
396 * Copy out the name, type, class, ttl.
399 rrbuffer = *target;
400 dns_compress_setmethods(cctx, DNS_COMPRESS_GLOBAL14);
401 result = dns_name_towire(owner_name, cctx, target);
402 if (result != ISC_R_SUCCESS)
403 goto rollback;
404 headlen = sizeof(dns_rdataclass_t) + sizeof(dns_rdatatype_t);
405 if (!question)
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;
411 goto rollback;
413 isc_buffer_putuint16(target, rdataset->type);
414 isc_buffer_putuint16(target, rdataset->rdclass);
415 if (!question) {
416 isc_buffer_putuint32(target, rdataset->ttl);
419 * Save space for rdlen.
421 rdlen = *target;
422 isc_buffer_add(target, 2);
425 * Copy out the rdata
427 if (shuffle)
428 rdata = *(sorted[i].rdata);
429 else {
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)
435 goto rollback;
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 -
440 rdlen.used - 2));
441 added++;
444 if (shuffle) {
445 i++;
446 if (i == count)
447 result = ISC_R_NOMORE;
448 else
449 result = ISC_R_SUCCESS;
450 } else {
451 result = dns_rdataset_next(rdataset);
453 } while (result == ISC_R_SUCCESS);
455 if (result != ISC_R_NOMORE)
456 goto rollback;
458 *countp += count;
460 result = ISC_R_SUCCESS;
461 goto cleanup;
463 rollback:
464 if (partial && result == ISC_R_NOSPACE) {
465 INSIST(rrbuffer.used < 65536);
466 dns_compress_rollback(cctx, (isc_uint16_t)rrbuffer.used);
467 *countp += added;
468 *target = rrbuffer;
469 goto cleanup;
471 INSIST(savedbuffer.used < 65536);
472 dns_compress_rollback(cctx, (isc_uint16_t)savedbuffer.used);
473 *countp = 0;
474 *target = savedbuffer;
476 cleanup:
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));
481 return (result);
484 isc_result_t
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,
490 void *order_arg,
491 unsigned int *countp)
493 return (towiresorted(rdataset, owner_name, cctx, target,
494 order, order_arg, ISC_FALSE, countp, NULL));
497 isc_result_t
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,
503 void *order_arg,
504 unsigned int *countp,
505 void **state)
507 REQUIRE(state == NULL); /* XXX remove when implemented */
508 return (towiresorted(rdataset, owner_name, cctx, target,
509 order, order_arg, ISC_TRUE, countp, state));
512 isc_result_t
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));
523 isc_result_t
524 dns_rdataset_additionaldata(dns_rdataset_t *rdataset,
525 dns_additionaldatafunc_t add, void *arg)
527 dns_rdata_t rdata = DNS_RDATA_INIT;
528 isc_result_t result;
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)
540 return (result);
542 do {
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)
551 return (result);
553 return (ISC_R_SUCCESS);