naptr_35.c 15.3 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1
/*
Automatic Updater's avatar
Automatic Updater committed
2
 * Copyright (C) 2004, 2005, 2007, 2008  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 1999-2001, 2003  Internet Software Consortium.
4
 *
Automatic Updater's avatar
Automatic Updater committed
5
 * Permission to use, copy, modify, and/or distribute this software for any
Mark Andrews's avatar
Mark Andrews committed
6 7
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
8
 *
Mark Andrews's avatar
Mark Andrews committed
9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC 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
16 17
 */

18
/* $Id: naptr_35.c,v 1.54 2009/01/21 05:36:45 marka Exp $ */
Mark Andrews's avatar
Mark Andrews committed
19

20 21
/* Reviewed: Thu Mar 16 16:52:50 PST 2000 by bwelling */

22
/* RFC2915 */
Mark Andrews's avatar
Mark Andrews committed
23

24 25
#ifndef RDATA_IN_1_NAPTR_35_C
#define RDATA_IN_1_NAPTR_35_C
Mark Andrews's avatar
Mark Andrews committed
26

27
#define RRTYPE_NAPTR_ATTRIBUTES (0)
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
#ifdef HAVE_REGEX_H
#include <regex.h>
#endif

/*
 * Check the wire format of the Regexp field.
 * Don't allow embeded NUL's.
 */
static inline isc_result_t
txt_valid_regex(const unsigned char *txt) {
#ifdef HAVE_REGEX_H
	regex_t preg;
	unsigned int regflags = REG_EXTENDED;
	unsigned int nsub = 0;
	char regex[256];
	char *cp;
#endif
	isc_boolean_t flags = ISC_FALSE;
	isc_boolean_t replace = ISC_FALSE;
	unsigned char c;
	unsigned char delim;
	unsigned int len;

	len = *txt++;
	if (len == 0U)
		return (ISC_R_SUCCESS);

	delim = *txt++;
	len--;

	/*
	 * Digits, backslash and flags can't be delimiters.
	 */
	switch (delim) {
	case '0': case '1': case '2': case '3': case '4':
	case '5': case '6': case '7': case '8': case '9':
	case '\\': case 'i': case 0:
		return (DNS_R_SYNTAX);
	}

#ifdef HAVE_REGEX_H
	memset(&preg, 0, sizeof(preg));
	cp = regex;
#endif

	while (len-- > 0) {
		c = *txt++;
		if (c == 0)
			return (DNS_R_SYNTAX);
		if (c == delim && !replace) {
			replace = ISC_TRUE;
			continue;
		} else if (c == delim && !flags) {
			flags = ISC_TRUE;
			continue;
		} else if (c == delim)
			return (DNS_R_SYNTAX);
		/*
		 * Flags are not escaped.
		 */
		if (flags) {
			switch (c) {
			case 'i':
#ifdef HAVE_REGEX_H
				regflags |= REG_ICASE;
#endif
				continue;
			default:
				return (DNS_R_SYNTAX);
			}
		}
#ifdef HAVE_REGEX_H
		if (!replace)
			*cp++ = c;
#endif
		if (c == '\\') {
			if (len == 0)
				return (DNS_R_SYNTAX);
			c = *txt++;
			if (c == 0)
				return (DNS_R_SYNTAX);
			len--;
			if (replace)
				switch (c) {
				case '0': return (DNS_R_SYNTAX);
#ifdef HAVE_REGEX_H
				case '1': if (nsub < 1) nsub = 1; break;
				case '2': if (nsub < 2) nsub = 2; break;
				case '3': if (nsub < 3) nsub = 3; break;
				case '4': if (nsub < 4) nsub = 4; break;
				case '5': if (nsub < 5) nsub = 5; break;
				case '6': if (nsub < 6) nsub = 6; break;
				case '7': if (nsub < 7) nsub = 7; break;
				case '8': if (nsub < 8) nsub = 8; break;
				case '9': if (nsub < 9) nsub = 9; break;
#endif
				}
#ifdef HAVE_REGEX_H
			if (!replace)
				*cp++ = c;
#endif
		}
	}
	if (!flags)
		return (DNS_R_SYNTAX);
#ifdef HAVE_REGEX_H
	*cp = '\0';
	if (regcomp(&preg, regex, regflags))
		return (DNS_R_SYNTAX);
	/*
	 * Check that substitutions in the replacement string are consistant
	 * with the regular expression.
	 */
	if (preg.re_nsub < nsub) {
		regfree(&preg);
		return (DNS_R_SYNTAX);
	}
	regfree(&preg);
#endif
	return (ISC_R_SUCCESS);
}
149

150
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
151
fromtext_in_naptr(ARGS_FROMTEXT) {
Mark Andrews's avatar
Mark Andrews committed
152 153 154
	isc_token_t token;
	dns_name_t name;
	isc_buffer_t buffer;
155
	unsigned char *regex;
Mark Andrews's avatar
Mark Andrews committed
156 157

	REQUIRE(type == 35);
158
	REQUIRE(rdclass == 1);
Mark Andrews's avatar
Mark Andrews committed
159

160
	UNUSED(type);
161
	UNUSED(rdclass);
162
	UNUSED(callbacks);
163

David Lawrence's avatar
David Lawrence committed
164 165 166
	/*
	 * Order.
	 */
167 168
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
				      ISC_FALSE));
169
	if (token.value.as_ulong > 0xffffU)
170
		RETTOK(ISC_R_RANGE);
Mark Andrews's avatar
Mark Andrews committed
171 172
	RETERR(uint16_tobuffer(token.value.as_ulong, target));

David Lawrence's avatar
David Lawrence committed
173 174 175
	/*
	 * Preference.
	 */
176 177
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
				      ISC_FALSE));
178
	if (token.value.as_ulong > 0xffffU)
179
		RETTOK(ISC_R_RANGE);
Mark Andrews's avatar
Mark Andrews committed
180 181
	RETERR(uint16_tobuffer(token.value.as_ulong, target));

David Lawrence's avatar
David Lawrence committed
182 183 184
	/*
	 * Flags.
	 */
185 186
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_qstring,
				      ISC_FALSE));
187
	RETTOK(txt_fromtext(&token.value.as_textregion, target));
Mark Andrews's avatar
Mark Andrews committed
188

David Lawrence's avatar
David Lawrence committed
189 190 191
	/*
	 * Service.
	 */
192 193
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_qstring,
				      ISC_FALSE));
194
	RETTOK(txt_fromtext(&token.value.as_textregion, target));
Mark Andrews's avatar
Mark Andrews committed
195

David Lawrence's avatar
David Lawrence committed
196 197 198
	/*
	 * Regexp.
	 */
199
	regex = isc_buffer_used(target);
200 201
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_qstring,
				      ISC_FALSE));
202
	RETTOK(txt_fromtext(&token.value.as_textregion, target));
203
	RETTOK(txt_valid_regex(regex));
Mark Andrews's avatar
Mark Andrews committed
204

David Lawrence's avatar
David Lawrence committed
205 206 207
	/*
	 * Replacement.
	 */
208 209
	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
				      ISC_FALSE));
Mark Andrews's avatar
Mark Andrews committed
210
	dns_name_init(&name, NULL);
211
	buffer_fromregion(&buffer, &token.value.as_region);
Mark Andrews's avatar
Mark Andrews committed
212
	origin = (origin != NULL) ? origin : dns_rootname;
213
	RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
214
	return (ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
215 216
}

217
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
218
totext_in_naptr(ARGS_TOTEXT) {
Mark Andrews's avatar
Mark Andrews committed
219 220 221 222
	isc_region_t region;
	dns_name_t name;
	dns_name_t prefix;
	isc_boolean_t sub;
Andreas Gustafsson's avatar
Andreas Gustafsson committed
223
	char buf[sizeof("64000")];
Mark Andrews's avatar
Mark Andrews committed
224 225 226
	unsigned short num;

	REQUIRE(rdata->type == 35);
227
	REQUIRE(rdata->rdclass == 1);
228
	REQUIRE(rdata->length != 0);
Mark Andrews's avatar
Mark Andrews committed
229 230 231 232 233

	dns_name_init(&name, NULL);
	dns_name_init(&prefix, NULL);

	dns_rdata_toregion(rdata, &region);
234

David Lawrence's avatar
David Lawrence committed
235 236 237
	/*
	 * Order.
	 */
Mark Andrews's avatar
Mark Andrews committed
238 239 240 241 242 243
	num = uint16_fromregion(&region);
	isc_region_consume(&region, 2);
	sprintf(buf, "%u", num);
	RETERR(str_totext(buf, target));
	RETERR(str_totext(" ", target));

David Lawrence's avatar
David Lawrence committed
244 245 246
	/*
	 * Preference.
	 */
Mark Andrews's avatar
Mark Andrews committed
247 248 249 250 251 252
	num = uint16_fromregion(&region);
	isc_region_consume(&region, 2);
	sprintf(buf, "%u", num);
	RETERR(str_totext(buf, target));
	RETERR(str_totext(" ", target));

David Lawrence's avatar
David Lawrence committed
253 254 255
	/*
	 * Flags.
	 */
Mark Andrews's avatar
Mark Andrews committed
256 257 258
	RETERR(txt_totext(&region, target));
	RETERR(str_totext(" ", target));

David Lawrence's avatar
David Lawrence committed
259 260 261
	/*
	 * Service.
	 */
Mark Andrews's avatar
Mark Andrews committed
262 263 264
	RETERR(txt_totext(&region, target));
	RETERR(str_totext(" ", target));

David Lawrence's avatar
David Lawrence committed
265 266 267
	/*
	 * Regexp.
	 */
Mark Andrews's avatar
Mark Andrews committed
268 269 270
	RETERR(txt_totext(&region, target));
	RETERR(str_totext(" ", target));

David Lawrence's avatar
David Lawrence committed
271 272 273
	/*
	 * Replacement.
	 */
Mark Andrews's avatar
Mark Andrews committed
274
	dns_name_fromregion(&name, &region);
275
	sub = name_prefix(&name, tctx->origin, &prefix);
276
	return (dns_name_totext(&prefix, sub, target));
Mark Andrews's avatar
Mark Andrews committed
277 278
}

279
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
280
fromwire_in_naptr(ARGS_FROMWIRE) {
Automatic Updater's avatar
Automatic Updater committed
281
	dns_name_t name;
Mark Andrews's avatar
Mark Andrews committed
282
	isc_region_t sr;
283
	unsigned char *regex;
Mark Andrews's avatar
Mark Andrews committed
284 285

	REQUIRE(type == 35);
286
	REQUIRE(rdclass == 1);
Mark Andrews's avatar
Mark Andrews committed
287

288
	UNUSED(type);
289 290
	UNUSED(rdclass);

291
	dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
292

Automatic Updater's avatar
Automatic Updater committed
293
	dns_name_init(&name, NULL);
Mark Andrews's avatar
Mark Andrews committed
294

David Lawrence's avatar
David Lawrence committed
295 296 297
	/*
	 * Order, preference.
	 */
298
	isc_buffer_activeregion(source, &sr);
Mark Andrews's avatar
Mark Andrews committed
299
	if (sr.length < 4)
300
		return (ISC_R_UNEXPECTEDEND);
Mark Andrews's avatar
Mark Andrews committed
301 302 303
	RETERR(mem_tobuffer(target, sr.base, 4));
	isc_buffer_forward(source, 4);

David Lawrence's avatar
David Lawrence committed
304 305 306
	/*
	 * Flags.
	 */
Mark Andrews's avatar
Mark Andrews committed
307 308
	RETERR(txt_fromwire(source, target));

David Lawrence's avatar
David Lawrence committed
309 310 311
	/*
	 * Service.
	 */
Mark Andrews's avatar
Mark Andrews committed
312 313
	RETERR(txt_fromwire(source, target));

David Lawrence's avatar
David Lawrence committed
314 315 316
	/*
	 * Regexp.
	 */
317
	regex = isc_buffer_used(target);
Mark Andrews's avatar
Mark Andrews committed
318
	RETERR(txt_fromwire(source, target));
319
	RETERR(txt_valid_regex(regex));
Mark Andrews's avatar
Mark Andrews committed
320

David Lawrence's avatar
David Lawrence committed
321 322 323
	/*
	 * Replacement.
	 */
324
	return (dns_name_fromwire(&name, source, dctx, options, target));
Mark Andrews's avatar
Mark Andrews committed
325 326
}

327
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
328
towire_in_naptr(ARGS_TOWIRE) {
Mark Andrews's avatar
Mark Andrews committed
329
	dns_name_t name;
330
	dns_offsets_t offsets;
Mark Andrews's avatar
Mark Andrews committed
331 332 333
	isc_region_t sr;

	REQUIRE(rdata->type == 35);
334
	REQUIRE(rdata->rdclass == 1);
335
	REQUIRE(rdata->length != 0);
Mark Andrews's avatar
Mark Andrews committed
336

337
	dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
David Lawrence's avatar
David Lawrence committed
338 339 340
	/*
	 * Order, preference.
	 */
Mark Andrews's avatar
Mark Andrews committed
341 342 343
	dns_rdata_toregion(rdata, &sr);
	RETERR(mem_tobuffer(target, sr.base, 4));
	isc_region_consume(&sr, 4);
344

David Lawrence's avatar
David Lawrence committed
345 346 347
	/*
	 * Flags.
	 */
Mark Andrews's avatar
Mark Andrews committed
348 349 350
	RETERR(mem_tobuffer(target, sr.base, sr.base[0] + 1));
	isc_region_consume(&sr, sr.base[0] + 1);

David Lawrence's avatar
David Lawrence committed
351 352 353
	/*
	 * Service.
	 */
Mark Andrews's avatar
Mark Andrews committed
354 355 356
	RETERR(mem_tobuffer(target, sr.base, sr.base[0] + 1));
	isc_region_consume(&sr, sr.base[0] + 1);

David Lawrence's avatar
David Lawrence committed
357 358 359
	/*
	 * Regexp.
	 */
Mark Andrews's avatar
Mark Andrews committed
360 361 362
	RETERR(mem_tobuffer(target, sr.base, sr.base[0] + 1));
	isc_region_consume(&sr, sr.base[0] + 1);

David Lawrence's avatar
David Lawrence committed
363 364 365
	/*
	 * Replacement.
	 */
366
	dns_name_init(&name, offsets);
Mark Andrews's avatar
Mark Andrews committed
367 368 369 370
	dns_name_fromregion(&name, &sr);
	return (dns_name_towire(&name, cctx, target));
}

371
static inline int
David Lawrence's avatar
David Lawrence committed
372
compare_in_naptr(ARGS_COMPARE) {
Mark Andrews's avatar
Mark Andrews committed
373 374 375 376
	dns_name_t name1;
	dns_name_t name2;
	isc_region_t region1;
	isc_region_t region2;
377
	int order, len;
Mark Andrews's avatar
Mark Andrews committed
378 379

	REQUIRE(rdata1->type == rdata2->type);
380
	REQUIRE(rdata1->rdclass == rdata2->rdclass);
Mark Andrews's avatar
Mark Andrews committed
381
	REQUIRE(rdata1->type == 35);
382
	REQUIRE(rdata1->rdclass == 1);
383 384
	REQUIRE(rdata1->length != 0);
	REQUIRE(rdata2->length != 0);
Mark Andrews's avatar
Mark Andrews committed
385 386 387 388

	dns_rdata_toregion(rdata1, &region1);
	dns_rdata_toregion(rdata2, &region2);

David Lawrence's avatar
David Lawrence committed
389 390 391
	/*
	 * Order, preference.
	 */
392 393 394
	order = memcmp(region1.base, region2.base, 4);
	if (order != 0)
		return (order < 0 ? -1 : 1);
Mark Andrews's avatar
Mark Andrews committed
395 396 397
	isc_region_consume(&region1, 4);
	isc_region_consume(&region2, 4);

David Lawrence's avatar
David Lawrence committed
398 399 400
	/*
	 * Flags.
	 */
401
	len = ISC_MIN(region1.base[0], region2.base[0]);
402 403 404
	order = memcmp(region1.base, region2.base, len + 1);
	if (order != 0)
		return (order < 0 ? -1 : 1);
Mark Andrews's avatar
Mark Andrews committed
405 406 407
	isc_region_consume(&region1, region1.base[0] + 1);
	isc_region_consume(&region2, region2.base[0] + 1);

David Lawrence's avatar
David Lawrence committed
408 409 410
	/*
	 * Service.
	 */
411
	len = ISC_MIN(region1.base[0], region2.base[0]);
412 413 414
	order = memcmp(region1.base, region2.base, len + 1);
	if (order != 0)
		return (order < 0 ? -1 : 1);
Mark Andrews's avatar
Mark Andrews committed
415 416 417
	isc_region_consume(&region1, region1.base[0] + 1);
	isc_region_consume(&region2, region2.base[0] + 1);

David Lawrence's avatar
David Lawrence committed
418 419 420
	/*
	 * Regexp.
	 */
421
	len = ISC_MIN(region1.base[0], region2.base[0]);
422 423 424
	order = memcmp(region1.base, region2.base, len + 1);
	if (order != 0)
		return (order < 0 ? -1 : 1);
Mark Andrews's avatar
Mark Andrews committed
425 426 427
	isc_region_consume(&region1, region1.base[0] + 1);
	isc_region_consume(&region2, region2.base[0] + 1);

David Lawrence's avatar
David Lawrence committed
428 429 430
	/*
	 * Replacement.
	 */
Mark Andrews's avatar
Mark Andrews committed
431 432 433 434 435 436
	dns_name_init(&name1, NULL);
	dns_name_init(&name2, NULL);

	dns_name_fromregion(&name1, &region1);
	dns_name_fromregion(&name2, &region2);

437
	return (dns_name_rdatacompare(&name1, &name2));
Mark Andrews's avatar
Mark Andrews committed
438 439
}

440
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
441
fromstruct_in_naptr(ARGS_FROMSTRUCT) {
442 443
	dns_rdata_in_naptr_t *naptr = source;
	isc_region_t region;
Mark Andrews's avatar
Mark Andrews committed
444 445

	REQUIRE(type == 35);
446
	REQUIRE(rdclass == 1);
447 448 449
	REQUIRE(source != NULL);
	REQUIRE(naptr->common.rdtype == type);
	REQUIRE(naptr->common.rdclass == rdclass);
450
	REQUIRE(naptr->flags != NULL || naptr->flags_len == 0);
451 452
	REQUIRE(naptr->service != NULL || naptr->service_len == 0);
	REQUIRE(naptr->regexp != NULL || naptr->regexp_len == 0);
453

454
	UNUSED(type);
455 456
	UNUSED(rdclass);

457 458 459 460 461 462 463 464 465 466
	RETERR(uint16_tobuffer(naptr->order, target));
	RETERR(uint16_tobuffer(naptr->preference, target));
	RETERR(uint8_tobuffer(naptr->flags_len, target));
	RETERR(mem_tobuffer(target, naptr->flags, naptr->flags_len));
	RETERR(uint8_tobuffer(naptr->service_len, target));
	RETERR(mem_tobuffer(target, naptr->service, naptr->service_len));
	RETERR(uint8_tobuffer(naptr->regexp_len, target));
	RETERR(mem_tobuffer(target, naptr->regexp, naptr->regexp_len));
	dns_name_toregion(&naptr->replacement, &region);
	return (isc_buffer_copyregion(target, &region));
Mark Andrews's avatar
Mark Andrews committed
467 468
}

469
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
470
tostruct_in_naptr(ARGS_TOSTRUCT) {
471 472 473 474 475
	dns_rdata_in_naptr_t *naptr = target;
	isc_region_t r;
	isc_result_t result;
	dns_name_t name;

Mark Andrews's avatar
Mark Andrews committed
476
	REQUIRE(rdata->type == 35);
477
	REQUIRE(rdata->rdclass == 1);
478
	REQUIRE(target != NULL);
479
	REQUIRE(rdata->length != 0);
Mark Andrews's avatar
Mark Andrews committed
480

481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
	naptr->common.rdclass = rdata->rdclass;
	naptr->common.rdtype = rdata->type;
	ISC_LINK_INIT(&naptr->common, link);

	naptr->flags = NULL;
	naptr->service = NULL;
	naptr->regexp = NULL;

	dns_rdata_toregion(rdata, &r);

	naptr->order = uint16_fromregion(&r);
	isc_region_consume(&r, 2);

	naptr->preference = uint16_fromregion(&r);
	isc_region_consume(&r, 2);

	naptr->flags_len = uint8_fromregion(&r);
	isc_region_consume(&r, 1);
499 500 501 502 503
	INSIST(naptr->flags_len <= r.length);
	naptr->flags = mem_maybedup(mctx, r.base, naptr->flags_len);
	if (naptr->flags == NULL)
		goto cleanup;
	isc_region_consume(&r, naptr->flags_len);
504

505 506
	naptr->service_len = uint8_fromregion(&r);
	isc_region_consume(&r, 1);
507 508 509 510 511
	INSIST(naptr->service_len <= r.length);
	naptr->service = mem_maybedup(mctx, r.base, naptr->service_len);
	if (naptr->service == NULL)
		goto cleanup;
	isc_region_consume(&r, naptr->service_len);
512 513 514

	naptr->regexp_len = uint8_fromregion(&r);
	isc_region_consume(&r, 1);
515 516 517 518 519
	INSIST(naptr->regexp_len <= r.length);
	naptr->regexp = mem_maybedup(mctx, r.base, naptr->regexp_len);
	if (naptr->regexp == NULL)
		goto cleanup;
	isc_region_consume(&r, naptr->regexp_len);
520

521 522 523
	dns_name_init(&name, NULL);
	dns_name_fromregion(&name, &r);
	dns_name_init(&naptr->replacement, NULL);
524 525 526 527
	result = name_duporclone(&name, mctx, &naptr->replacement);
	if (result != ISC_R_SUCCESS)
		goto cleanup;
	naptr->mctx = mctx;
528
	return (ISC_R_SUCCESS);
529 530 531 532 533 534 535 536 537

 cleanup:
	if (mctx != NULL && naptr->flags != NULL)
		isc_mem_free(mctx, naptr->flags);
	if (mctx != NULL && naptr->service != NULL)
		isc_mem_free(mctx, naptr->service);
	if (mctx != NULL && naptr->regexp != NULL)
		isc_mem_free(mctx, naptr->regexp);
	return (ISC_R_NOMEMORY);
Mark Andrews's avatar
Mark Andrews committed
538
}
539

540
static inline void
David Lawrence's avatar
David Lawrence committed
541
freestruct_in_naptr(ARGS_FREESTRUCT) {
542
	dns_rdata_in_naptr_t *naptr = source;
543

544
	REQUIRE(source != NULL);
545 546 547 548 549 550 551 552 553 554 555 556 557 558
	REQUIRE(naptr->common.rdclass == 1);
	REQUIRE(naptr->common.rdtype == 35);

	if (naptr->mctx == NULL)
		return;

	if (naptr->flags != NULL)
		isc_mem_free(naptr->mctx, naptr->flags);
	if (naptr->service != NULL)
		isc_mem_free(naptr->mctx, naptr->service);
	if (naptr->regexp != NULL)
		isc_mem_free(naptr->mctx, naptr->regexp);
	dns_name_free(&naptr->replacement, naptr->mctx);
	naptr->mctx = NULL;
559
}
560

561
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
562
additionaldata_in_naptr(ARGS_ADDLDATA) {
Bob Halley's avatar
Bob Halley committed
563
	dns_name_t name;
564
	dns_offsets_t offsets;
Bob Halley's avatar
Bob Halley committed
565 566 567 568 569
	isc_region_t sr;
	dns_rdatatype_t atype;
	unsigned int i, flagslen;
	char *cp;

570 571 572
	REQUIRE(rdata->type == 35);
	REQUIRE(rdata->rdclass == 1);

David Lawrence's avatar
David Lawrence committed
573 574 575
	/*
	 * Order, preference.
	 */
Bob Halley's avatar
Bob Halley committed
576 577
	dns_rdata_toregion(rdata, &sr);
	isc_region_consume(&sr, 4);
578

David Lawrence's avatar
David Lawrence committed
579 580 581
	/*
	 * Flags.
	 */
Bob Halley's avatar
Bob Halley committed
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
	atype = 0;
	flagslen = sr.base[0];
	cp = (char *)&sr.base[1];
	for (i = 0; i < flagslen; i++, cp++) {
		if (*cp == 'S' || *cp == 's') {
			atype = dns_rdatatype_srv;
			break;
		}
		if (*cp == 'A' || *cp == 'a') {
			atype = dns_rdatatype_a;
			break;
		}
	}
	isc_region_consume(&sr, flagslen + 1);

David Lawrence's avatar
David Lawrence committed
597 598 599
	/*
	 * Service.
	 */
Bob Halley's avatar
Bob Halley committed
600 601
	isc_region_consume(&sr, sr.base[0] + 1);

David Lawrence's avatar
David Lawrence committed
602 603 604
	/*
	 * Regexp.
	 */
Bob Halley's avatar
Bob Halley committed
605 606
	isc_region_consume(&sr, sr.base[0] + 1);

David Lawrence's avatar
David Lawrence committed
607 608 609
	/*
	 * Replacement.
	 */
610
	dns_name_init(&name, offsets);
Bob Halley's avatar
Bob Halley committed
611 612 613 614
	dns_name_fromregion(&name, &sr);

	if (atype != 0)
		return ((add)(arg, &name, atype));
615

616
	return (ISC_R_SUCCESS);
617 618
}

619
static inline isc_result_t
David Lawrence's avatar
David Lawrence committed
620
digest_in_naptr(ARGS_DIGEST) {
621 622
	isc_region_t r1, r2;
	unsigned int length, n;
623
	isc_result_t result;
624 625 626 627 628 629 630 631 632
	dns_name_t name;

	REQUIRE(rdata->type == 35);
	REQUIRE(rdata->rdclass == 1);

	dns_rdata_toregion(rdata, &r1);
	r2 = r1;
	length = 0;

David Lawrence's avatar
David Lawrence committed
633 634 635
	/*
	 * Order, preference.
	 */
636 637 638
	length += 4;
	isc_region_consume(&r2, 4);

David Lawrence's avatar
David Lawrence committed
639 640 641
	/*
	 * Flags.
	 */
642 643 644 645
	n = r2.base[0] + 1;
	length += n;
	isc_region_consume(&r2, n);

David Lawrence's avatar
David Lawrence committed
646 647 648
	/*
	 * Service.
	 */
649 650 651 652
	n = r2.base[0] + 1;
	length += n;
	isc_region_consume(&r2, n);

David Lawrence's avatar
David Lawrence committed
653 654 655
	/*
	 * Regexp.
	 */
656 657 658 659 660 661 662 663 664
	n = r2.base[0] + 1;
	length += n;
	isc_region_consume(&r2, n);

	/*
	 * Digest the RR up to the replacement name.
	 */
	r1.length = length;
	result = (digest)(arg, &r1);
665
	if (result != ISC_R_SUCCESS)
666 667
		return (result);

David Lawrence's avatar
David Lawrence committed
668 669 670
	/*
	 * Replacement.
	 */
671 672 673

	dns_name_init(&name, NULL);
	dns_name_fromregion(&name, &r2);
674

675 676 677
	return (dns_name_digest(&name, digest, arg));
}

678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
static inline isc_boolean_t
checkowner_in_naptr(ARGS_CHECKOWNER) {

	REQUIRE(type == 35);
	REQUIRE(rdclass == 1);

	UNUSED(name);
	UNUSED(type);
	UNUSED(rdclass);
	UNUSED(wildcard);

	return (ISC_TRUE);
}

static inline isc_boolean_t
checknames_in_naptr(ARGS_CHECKNAMES) {

	REQUIRE(rdata->type == 35);
	REQUIRE(rdata->rdclass == 1);

	UNUSED(rdata);
	UNUSED(owner);
	UNUSED(bad);

	return (ISC_TRUE);
}

705
#endif	/* RDATA_IN_1_NAPTR_35_C */