rdata.c 15.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (C) 1998  Internet Software Consortium.
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

Mark Andrews's avatar
Mark Andrews committed
18
 /* $Id: rdata.c,v 1.13 1999/01/22 05:02:43 marka Exp $ */
19 20 21 22

#include <config.h>

#include <stdio.h>
Mark Andrews's avatar
Mark Andrews committed
23

24 25
#include <isc/buffer.h>
#include <isc/lex.h>
26 27
#include <isc/assertions.h>

28 29 30 31
#include <dns/types.h>
#include <dns/result.h>
#include <dns/rdata.h>
#include <dns/region.h>
32 33
#include <dns/rdataclass.h>
#include <dns/rdatatype.h>
34

35
#define RETERR(x) do { \
36 37 38
	dns_result_t __r = (x); \
	if (__r != DNS_R_SUCCESS) \
		return (__r); \
39
	} while (0)
40

41 42 43 44 45 46 47 48 49 50 51 52 53
static dns_result_t	txt_totext(isc_region_t *source, isc_buffer_t *target);
static dns_result_t	txt_fromtext(isc_textregion_t *source,
				     isc_buffer_t *target);
static dns_result_t	txt_fromwire(isc_buffer_t *source,
				    isc_buffer_t *target);
static isc_boolean_t	name_prefix(dns_name_t *name, dns_name_t *origin,
				    dns_name_t *target);
static unsigned int 	name_length(dns_name_t *name);
static dns_result_t	str_totext(char *source, isc_buffer_t *target);
static isc_boolean_t	buffer_empty(isc_buffer_t *source);
static void		buffer_fromregion(isc_buffer_t *buffer,
					  isc_region_t *region,
					  unsigned int type);
Mark Andrews's avatar
Mark Andrews committed
54
static isc_result_t	uint32_tobuffer(unsigned long value,
55
					isc_buffer_t *target);
Mark Andrews's avatar
Mark Andrews committed
56
static isc_result_t	uint16_tobuffer(unsigned long value,
57 58 59
					isc_buffer_t *target);
static unsigned long	uint32_fromregion(isc_region_t *region);
static unsigned short	uint16_fromregion(isc_region_t *region);
Mark Andrews's avatar
Mark Andrews committed
60 61 62 63 64
static dns_result_t	gettoken(isc_lex_t *lexer, isc_token_t *token,
				 isc_tokentype_t expect, isc_boolean_t eol);
static dns_result_t	mem_tobuffer(isc_buffer_t *target, void *base,
				     unsigned int length);
static int		compare_region(isc_region_t *r1, isc_region_t *r2);
65 66 67

#include "code.h"

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#define META 0x0001
#define RESERVED 0x0002

#define METATYPES \
	{ 0, "NONE", META }, \
	{ 100, "UINFO", RESERVED }, \
	{ 101, "UID", RESERVED }, \
	{ 102, "GID", RESERVED }, \
	{ 103, "UNSPEC", RESERVED }, \
	{ 249, "TKEY", META }, \
	{ 250, "TSIG", META }, \
	{ 251, "IXFR", META }, \
	{ 252, "AXFR", META }, \
	{ 253, "MAILB", META }, \
	{ 254, "MAILA", META }, \
	{ 255, "ANY", META },

#define METACLASSES \
	{ 0, "NONE", META }, \
	{ 255, "ANY", META },

89 90 91
struct tbl {
	int	value;
	char	*name;
92 93 94
	int	flags;
} types[] = { TYPENAMES METATYPES {0, NULL, 0} },
classes[] = { CLASSNAMES METACLASSES { 0, NULL, 0} };
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
/***
 *** Initialization
 ***/

void
dns_rdata_init(dns_rdata_t *rdata) {

	REQUIRE(rdata != NULL);

	rdata->data = NULL;
	rdata->length = 0;
	rdata->class = 0;
	rdata->type = 0;
	ISC_LINK_INIT(rdata, link);
	/* ISC_LIST_INIT(rdata->list); */
}

/***
 *** Comparisons
 ***/

int
dns_rdata_compare(dns_rdata_t *rdata1, dns_rdata_t *rdata2) {
Mark Andrews's avatar
Mark Andrews committed
118 119
	int result = 0;
	isc_boolean_t use_default = ISC_FALSE;
120 121 122 123 124 125 126 127 128 129 130 131 132 133

	REQUIRE(rdata1 != NULL);
	REQUIRE(rdata2 != NULL);
	REQUIRE(rdata1->data != NULL);
	REQUIRE(rdata2->data != NULL);

	if (rdata1->class != rdata2->class)
		return (rdata1->class < rdata2->class ? -1 : 1);

	if (rdata1->type != rdata2->type)
		return (rdata1->type < rdata2->type ? -1 : 1);

	COMPARESWITCH

Mark Andrews's avatar
Mark Andrews committed
134
	if (use_default) {
Mark Andrews's avatar
Mark Andrews committed
135 136 137 138 139 140
		isc_region_t r1;
		isc_region_t r2;

		dns_rdata_toregion(rdata1, &r1);
		dns_rdata_toregion(rdata2, &r2);
		result = compare_region(&r1, &r2);
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	}
	return (result);
}

/***
 *** Conversions
 ***/

void
dns_rdata_fromregion(dns_rdata_t *rdata,
			  dns_rdataclass_t class, dns_rdatatype_t type,
			  isc_region_t *r) {
			  
	rdata->data = r->base;
	rdata->length = r->length;
	rdata->class = class;
	rdata->type = type;
}

void
dns_rdata_toregion(dns_rdata_t *rdata, isc_region_t *r) {

	r->base = rdata->data;
	r->length = rdata->length;
}

dns_result_t
dns_rdata_fromwire(dns_rdata_t *rdata,
		   dns_rdataclass_t class, dns_rdatatype_t type,
		   isc_buffer_t *source,
		   dns_decompress_t *dctx,
		   isc_boolean_t downcase,
		   isc_buffer_t *target) {
Mark Andrews's avatar
Mark Andrews committed
174
	dns_result_t result = DNS_R_NOTIMPLEMENTED;
175 176 177
	isc_region_t region;
	isc_buffer_t ss;
	isc_buffer_t st;
Mark Andrews's avatar
Mark Andrews committed
178
	isc_boolean_t use_default = ISC_FALSE;
179 180 181

	ss = *source;
	st = *target;
182
	region.base = (unsigned char *)(target->base) + target->used;
183 184 185

	FROMWIRESWITCH

Mark Andrews's avatar
Mark Andrews committed
186 187
	if (use_default)
		(void)NULL;
188 189

	/* We should have consumed all out buffer */
Mark Andrews's avatar
Mark Andrews committed
190 191
	if (result == DNS_R_SUCCESS && !buffer_empty(source))
		result = DNS_R_EXTRADATA;
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

	if (rdata && result == DNS_R_SUCCESS) {
		region.length = target->used - st.used;
		dns_rdata_fromregion(rdata, class, type, &region);
	}

	if (result != DNS_R_SUCCESS) {
		*source = ss;
		*target = st;
	}
	return (result);
}

dns_result_t
dns_rdata_towire(dns_rdata_t *rdata, dns_compress_t *cctx,
	         isc_buffer_t *target) {
Mark Andrews's avatar
Mark Andrews committed
208 209
	dns_result_t result = DNS_R_NOTIMPLEMENTED;
	isc_boolean_t use_default = ISC_FALSE;
210 211 212

	TOWIRESWITCH
	
Mark Andrews's avatar
Mark Andrews committed
213
	if (use_default) {
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
		if (target->length < rdata->length) 
			return (DNS_R_NOSPACE);
		memcpy(target->base, rdata->data, rdata->length);
		target->length = rdata->length;
		return (DNS_R_SUCCESS);
	}
	return (result);
}

dns_result_t
dns_rdata_fromtext(dns_rdata_t *rdata,
		   dns_rdataclass_t class, dns_rdatatype_t type,
		   isc_lex_t *lexer, dns_name_t *origin,
		   isc_boolean_t downcase,
		   isc_buffer_t *target) {
Mark Andrews's avatar
Mark Andrews committed
229
	dns_result_t result = DNS_R_NOTIMPLEMENTED;
230 231
	isc_region_t region;
	isc_buffer_t st;
Mark Andrews's avatar
Mark Andrews committed
232 233 234
	isc_boolean_t use_default = ISC_FALSE;
	isc_token_t token;
	unsigned int options = ISC_LEXOPT_EOL | ISC_LEXOPT_EOF;
235 236

	st = *target;
237
	region.base = (unsigned char *)(target->base) + target->used;
238 239 240

	FROMTEXTSWITCH

Mark Andrews's avatar
Mark Andrews committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	if (use_default)
		(void)NULL;

	/*
	 * Consume to end of line / file.
	 * If not at end of line initially set error code.
	 */
	do {
		if (isc_lex_gettoken(lexer, options, &token)
		    != ISC_R_SUCCESS) {
			if (result == DNS_R_SUCCESS)
				result = DNS_R_UNEXPECTED;
			break;
		} else if (token.type != isc_tokentype_eol &&
			   token.type != isc_tokentype_eof) {
			fprintf(stderr, "token_type = %d\n", token.type);
			if (result == DNS_R_SUCCESS)
				result = DNS_R_EXTRATOKEN;
		} else
			break;
	} while (1);
262 263 264 265 266 267 268 269 270 271 272 273 274

	if (rdata != NULL && result == DNS_R_SUCCESS) {
		region.length = target->used - st.used;
		dns_rdata_fromregion(rdata, class, type, &region);
	}
	if (result != DNS_R_SUCCESS) {
		*target = st;
	}
	return (result);
}

dns_result_t
dns_rdata_totext(dns_rdata_t *rdata, isc_buffer_t *target) {
Mark Andrews's avatar
Mark Andrews committed
275
	dns_result_t result = DNS_R_NOTIMPLEMENTED;
276
	dns_name_t *origin = NULL;
Mark Andrews's avatar
Mark Andrews committed
277
	isc_boolean_t use_default = ISC_FALSE;
278 279 280
	
	TOTEXTSWITCH

Mark Andrews's avatar
Mark Andrews committed
281 282 283
	if (use_default)
		(void)NULL;

284 285 286 287 288 289 290 291
	return (result);
}

dns_result_t
dns_rdata_fromstruct(dns_rdata_t *rdata,
		     dns_rdataclass_t class, dns_rdatatype_t type,
		     void *source,
		     isc_buffer_t *target) {
Mark Andrews's avatar
Mark Andrews committed
292
	dns_result_t result = DNS_R_NOTIMPLEMENTED;
293 294
	isc_buffer_t st;
	isc_region_t region;
Mark Andrews's avatar
Mark Andrews committed
295
	isc_boolean_t use_default = ISC_FALSE;
296

297
	region.base = (unsigned char *)(target->base) + target->used;
298 299 300 301
	st = *target;

	FROMSTRUCTSWITCH

Mark Andrews's avatar
Mark Andrews committed
302 303
	if (use_default)
		(void)NULL;
304 305 306 307 308 309 310 311 312 313 314 315

	if (rdata != NULL && result == DNS_R_SUCCESS) {
		region.length = target->used - st.used;
		dns_rdata_fromregion(rdata, class, type, &region);
	}
	if (result != DNS_R_SUCCESS)
		*target = st;
	return (result);
}

dns_result_t
dns_rdata_tostruct(dns_rdata_t *rdata, void *target) {
Mark Andrews's avatar
Mark Andrews committed
316 317
	dns_result_t result = DNS_R_NOTIMPLEMENTED;
	isc_boolean_t use_default = ISC_FALSE;
318 319 320

	TOSTRUCTSWITCH

Mark Andrews's avatar
Mark Andrews committed
321 322 323
	if (use_default)
		(void)NULL;

324 325 326
	return (result);
}

327 328 329 330 331 332 333 334 335 336
dns_result_t
dns_rdataclass_fromtext(dns_rdataclass_t *classp, isc_textregion_t *source) {
	int i = 0;
	unsigned int n;

	while (classes[i].name != NULL) {
		n = strlen(classes[i].name);
		if (n == source->length &&
		    strncasecmp(source->base, classes[i].name, n) == 0) {
			*classp = classes[i].value;
337 338 339
			if ((classes[i].flags & RESERVED) != 0)
				return (DNS_R_NOTIMPLEMENTED);
			return (DNS_R_SUCCESS);
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
		}
		i++;
	}
	return (DNS_R_UNKNOWN);
}

dns_result_t
dns_rdataclass_totext(dns_rdataclass_t class, isc_buffer_t *target) {
	int i = 0;
	unsigned int n;
	isc_region_t region;

	while (classes[i].name != NULL) {
		if (classes[i].value == class) {
			isc_buffer_available(target, &region);
			if ((n = strlen(classes[i].name)) > region.length)
				return (DNS_R_NOSPACE);
			memcpy(region.base, classes[i].name, n);
			isc_buffer_add(target, n);
359
			return (DNS_R_SUCCESS);
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
		}
		i++;
	}
	return (DNS_R_UNKNOWN);
}

dns_result_t
dns_rdatatype_fromtext(dns_rdatatype_t *typep, isc_textregion_t *source) {
	int i = 0;
	unsigned int n;

	while (types[i].name != NULL) {
		n = strlen(types[i].name);
		if (n == source->length &&
		    strncasecmp(source->base, types[i].name, n) == 0) {
			*typep = types[i].value;
376 377 378
			if ((types[i].flags & RESERVED) != 0)
				return (DNS_R_NOTIMPLEMENTED);
			return (DNS_R_SUCCESS);
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
		}
		i++;
	}
	return (DNS_R_UNKNOWN);
}

dns_result_t
dns_rdatatype_totext(dns_rdatatype_t type, isc_buffer_t *target) {
	int i = 0;
	unsigned int n;
	isc_region_t region;

	while (types[i].name != NULL) {
		if (types[i].value == type) {
			isc_buffer_available(target, &region);
			if ((n = strlen(types[i].name)) > region.length)
				return (DNS_R_NOSPACE);
			memcpy(region.base, types[i].name, n);
			isc_buffer_add(target, n);
398
			return (DNS_R_SUCCESS);
399 400 401 402 403 404 405 406
		}
		i++;
	}
	return (DNS_R_UNKNOWN);
}

 /* Private function */

407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
static unsigned int
name_length(dns_name_t *name) {
	return (name->length);
}

static dns_result_t
txt_totext(isc_region_t *source, isc_buffer_t *target) {
	unsigned int tl;
	unsigned int n;
	unsigned char *sp;
	unsigned char *tp;
	isc_region_t region;

	isc_buffer_available(target, &region);
	sp = source->base;
	tp = region.base;
	tl = region.length;

	n = *sp++;

	INSIST(n + 1 <= source->length);

	if (tl < 1)
		return (DNS_R_NOSPACE);
	*tp++ = '"';
	tl--;
	while (n--) {
		if (*sp < 0x20 || *sp > 0x7f) {
			if (tl < 4)
				return (DNS_R_NOSPACE);
			sprintf(tp, "\\%03u", *sp++);
			tp += 4;
			tl -= 4;
			continue;
		}
		if (*sp == 0x22 || *sp == 0x3b || *sp == 0x5c) {
			if (tl < 2)
				return (DNS_R_NOSPACE);
			*tp++ = '\\';
			tl--;
		}
		if (tl < 1)
			return (DNS_R_NOSPACE);
		*tp++ = *sp++;
		tl--;
	}
	if (tl < 1)
		return (DNS_R_NOSPACE);
	*tp++ = '"';
	tl--;
	isc_buffer_add(target, tp - region.base);
	isc_region_consume(source, *source->base + 1);
	return (DNS_R_SUCCESS);
}

static dns_result_t
txt_fromtext(isc_textregion_t *source, isc_buffer_t *target) {
	isc_region_t tregion;

	isc_buffer_available(target, &tregion);
	if (tregion.length < source->length + 1)
		return (DNS_R_NOSPACE);
	if (source->length > 255)
Mark Andrews's avatar
Mark Andrews committed
470
		return (DNS_R_TEXTTOLONG);
471 472 473 474 475 476 477 478 479 480 481 482
	*tregion.base = source->length;
	memcpy(tregion.base + 1, source->base, source->length);
	isc_buffer_add(target, source->length + 1);
	return (DNS_R_SUCCESS);
}

static dns_result_t
txt_fromwire(isc_buffer_t *source, isc_buffer_t *target) {
	unsigned int n;
	isc_region_t sregion;
	isc_region_t tregion;

483
	isc_buffer_active(source, &sregion);
Mark Andrews's avatar
Mark Andrews committed
484 485
	if (sregion.length == 0)
		return(DNS_R_UNEXPECTEDEND);
486 487
	n = *sregion.base + 1;
	if (n > sregion.length)
Mark Andrews's avatar
Mark Andrews committed
488
		return (DNS_R_UNEXPECTEDEND);
489 490
	
	isc_buffer_available(target, &tregion);
491
	if (n > tregion.length)
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
		return (DNS_R_NOSPACE);

	memcpy(tregion.base, sregion.base, n);
	isc_buffer_forward(source, n);
	isc_buffer_add(target, n);
	return (DNS_R_SUCCESS);
}

static isc_boolean_t
name_prefix(dns_name_t *name, dns_name_t *origin, dns_name_t *target) {
	int l1, l2;

	if (origin == NULL)
		goto return_false;

	if (dns_name_compare(origin, dns_rootname) == 0)
		goto return_false;

	if (!dns_name_issubdomain(name, origin))
		goto return_false;

	l1 = dns_name_countlabels(name);
	l2 = dns_name_countlabels(origin);
	
	if (l1 == l2)
		goto return_false;

	dns_name_getlabelsequence(name, 0, l1 - l2, target);
	return (ISC_TRUE);

return_false:
	*target = *name;
	return (ISC_FALSE);
}

static dns_result_t
str_totext(char *source, isc_buffer_t *target) {
	unsigned int l;
	isc_region_t region;

	isc_buffer_available(target, &region);
	l = strlen(source);

	if (l > region.length)
		return (DNS_R_NOSPACE);

	memcpy(region.base, source, l);
	isc_buffer_add(target, l);
	return (DNS_R_SUCCESS);
}

static isc_boolean_t
buffer_empty(isc_buffer_t *source) {
545
	return((source->current == source->active) ? ISC_TRUE : ISC_FALSE);
546 547 548 549 550 551 552 553
}

static void
buffer_fromregion(isc_buffer_t *buffer, isc_region_t *region,
		  unsigned int type) {

	isc_buffer_init(buffer, region->base, region->length, type);
	isc_buffer_add(buffer, region->length);
Mark Andrews's avatar
Mark Andrews committed
554
	isc_buffer_setactive(buffer, region->length);
555 556 557
}

static isc_result_t
Mark Andrews's avatar
Mark Andrews committed
558
uint32_tobuffer(unsigned long value, isc_buffer_t *target) {
559 560 561 562 563 564 565 566 567 568 569 570 571 572
	isc_region_t region;

	isc_buffer_available(target, &region);
	if (region.length < 4)
		return (DNS_R_NOSPACE);
	region.base[0] = (value >> 24) & 0xff;
	region.base[1] = (value >> 16) & 0xff;
	region.base[2] = (value >> 8) & 0xff;
	region.base[3] = value & 0xff;
	isc_buffer_add(target, 4);
	return (DNS_R_SUCCESS);
}

static isc_result_t
Mark Andrews's avatar
Mark Andrews committed
573
uint16_tobuffer(unsigned long value, isc_buffer_t *target) {
574 575
	isc_region_t region;

Mark Andrews's avatar
Mark Andrews committed
576 577
	if (value > 0xffff)
		return (DNS_R_RANGE);
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
	isc_buffer_available(target, &region);
	if (region.length < 2)
		return (DNS_R_NOSPACE);
	region.base[0] = (value >> 8) & 0xff;
	region.base[1] = value & 0xff;
	isc_buffer_add(target, 2);
	return (DNS_R_SUCCESS);
}

static unsigned long
uint32_fromregion(isc_region_t *region) {
	unsigned long value;
	
	INSIST(region->length >= 4);
	value = region->base[0] << 24;
	value |= region->base[1] << 16;
	value |= region->base[2] << 8;
	value |= region->base[3];
	return(value);
}

static unsigned short
uint16_fromregion(isc_region_t *region) {
	
	INSIST(region->length >= 2);

	return ((region->base[0] << 8) | region->base[1]);
}
Mark Andrews's avatar
Mark Andrews committed
606 607 608 609 610 611

static dns_result_t
gettoken(isc_lex_t *lexer, isc_token_t *token, isc_tokentype_t expect,
	 isc_boolean_t eol) {
	unsigned int options = ISC_LEXOPT_EOL | ISC_LEXOPT_EOF;
	
612 613
	if (expect == isc_tokentype_qstring)
		options |= ISC_LEXOPT_QSTRING;
Mark Andrews's avatar
Mark Andrews committed
614 615 616 617 618 619 620
	if (expect == isc_tokentype_number)
		options |= ISC_LEXOPT_NUMBER;
        if (isc_lex_gettoken(lexer, options, token) != ISC_R_SUCCESS)
                return (DNS_R_UNEXPECTED);
	if (eol && ((token->type == isc_tokentype_eol) || 
		    (token->type == isc_tokentype_eof)))
		return (DNS_R_SUCCESS);
621 622 623
	if (token->type == isc_tokentype_string &&
	    expect == isc_tokentype_qstring)
		return (DNS_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
        if (token->type != expect) {
                isc_lex_ungettoken(lexer, token);
                if (token->type == isc_tokentype_eol ||
                    token->type == isc_tokentype_eof)
                        return(DNS_R_UNEXPECTEDEND);
                return (DNS_R_UNEXPECTED);
        }
	return (DNS_R_SUCCESS);
}

static dns_result_t
mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
	isc_region_t tr;

	isc_buffer_available(target, &tr);
        if (length > tr.length)
		return (DNS_R_NOSPACE);
	memcpy(tr.base, base, length);
	isc_buffer_add(target, length);
	return (DNS_R_SUCCESS);
}

static int
compare_region(isc_region_t *r1, isc_region_t *r2) {
	unsigned int l;
	int result;

	l = (r1->length < r2->length) ? r1->length : r2->length;

	if ((result = memcmp(r1->base, r2->base, l)) != 0)
		return ((result < 0) ? -1 : 1);
	else
		return ((r1->length < r2->length) ? -1 : 1);
}