4 * Copyright (C) 2004, 2005, 2007-2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001, 2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: naptr_35.c,v 1.56 2009/12/04 21:09:34 marka Exp */
22 /* Reviewed: Thu Mar 16 16:52:50 PST 2000 by bwelling */
26 #ifndef RDATA_IN_1_NAPTR_35_C
27 #define RDATA_IN_1_NAPTR_35_C
29 #define RRTYPE_NAPTR_ATTRIBUTES (0)
35 * Check the wire format of the Regexp field.
36 * Don't allow embeded NUL's.
38 static inline isc_result_t
39 txt_valid_regex(const unsigned char *txt
) {
42 unsigned int regflags
= REG_EXTENDED
;
43 unsigned int nsub
= 0;
47 isc_boolean_t flags
= ISC_FALSE
;
48 isc_boolean_t replace
= ISC_FALSE
;
55 return (ISC_R_SUCCESS
);
61 * Digits, backslash and flags can't be delimiters.
64 case '0': case '1': case '2': case '3': case '4':
65 case '5': case '6': case '7': case '8': case '9':
66 case '\\': case 'i': case 0:
67 return (DNS_R_SYNTAX
);
71 memset(&preg
, 0, sizeof(preg
));
78 return (DNS_R_SYNTAX
);
79 if (c
== delim
&& !replace
) {
82 } else if (c
== delim
&& !flags
) {
85 } else if (c
== delim
)
86 return (DNS_R_SYNTAX
);
88 * Flags are not escaped.
94 regflags
|= REG_ICASE
;
98 return (DNS_R_SYNTAX
);
107 return (DNS_R_SYNTAX
);
110 return (DNS_R_SYNTAX
);
114 case '0': return (DNS_R_SYNTAX
);
116 case '1': if (nsub
< 1) nsub
= 1; break;
117 case '2': if (nsub
< 2) nsub
= 2; break;
118 case '3': if (nsub
< 3) nsub
= 3; break;
119 case '4': if (nsub
< 4) nsub
= 4; break;
120 case '5': if (nsub
< 5) nsub
= 5; break;
121 case '6': if (nsub
< 6) nsub
= 6; break;
122 case '7': if (nsub
< 7) nsub
= 7; break;
123 case '8': if (nsub
< 8) nsub
= 8; break;
124 case '9': if (nsub
< 9) nsub
= 9; break;
134 return (DNS_R_SYNTAX
);
137 if (regcomp(&preg
, regex
, regflags
))
138 return (DNS_R_SYNTAX
);
140 * Check that substitutions in the replacement string are consistant
141 * with the regular expression.
143 if (preg
.re_nsub
< nsub
) {
145 return (DNS_R_SYNTAX
);
149 return (ISC_R_SUCCESS
);
152 static inline isc_result_t
153 fromtext_in_naptr(ARGS_FROMTEXT
) {
157 unsigned char *regex
;
160 REQUIRE(rdclass
== 1);
169 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
171 if (token
.value
.as_ulong
> 0xffffU
)
173 RETERR(uint16_tobuffer(token
.value
.as_ulong
, target
));
178 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
180 if (token
.value
.as_ulong
> 0xffffU
)
182 RETERR(uint16_tobuffer(token
.value
.as_ulong
, target
));
187 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_qstring
,
189 RETTOK(txt_fromtext(&token
.value
.as_textregion
, target
));
194 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_qstring
,
196 RETTOK(txt_fromtext(&token
.value
.as_textregion
, target
));
201 regex
= isc_buffer_used(target
);
202 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_qstring
,
204 RETTOK(txt_fromtext(&token
.value
.as_textregion
, target
));
205 RETTOK(txt_valid_regex(regex
));
210 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
212 dns_name_init(&name
, NULL
);
213 buffer_fromregion(&buffer
, &token
.value
.as_region
);
214 origin
= (origin
!= NULL
) ? origin
: dns_rootname
;
215 RETTOK(dns_name_fromtext(&name
, &buffer
, origin
, options
, target
));
216 return (ISC_R_SUCCESS
);
219 static inline isc_result_t
220 totext_in_naptr(ARGS_TOTEXT
) {
225 char buf
[sizeof("64000")];
228 REQUIRE(rdata
->type
== 35);
229 REQUIRE(rdata
->rdclass
== 1);
230 REQUIRE(rdata
->length
!= 0);
232 dns_name_init(&name
, NULL
);
233 dns_name_init(&prefix
, NULL
);
235 dns_rdata_toregion(rdata
, ®ion
);
240 num
= uint16_fromregion(®ion
);
241 isc_region_consume(®ion
, 2);
242 sprintf(buf
, "%u", num
);
243 RETERR(str_totext(buf
, target
));
244 RETERR(str_totext(" ", target
));
249 num
= uint16_fromregion(®ion
);
250 isc_region_consume(®ion
, 2);
251 sprintf(buf
, "%u", num
);
252 RETERR(str_totext(buf
, target
));
253 RETERR(str_totext(" ", target
));
258 RETERR(txt_totext(®ion
, target
));
259 RETERR(str_totext(" ", target
));
264 RETERR(txt_totext(®ion
, target
));
265 RETERR(str_totext(" ", target
));
270 RETERR(txt_totext(®ion
, target
));
271 RETERR(str_totext(" ", target
));
276 dns_name_fromregion(&name
, ®ion
);
277 sub
= name_prefix(&name
, tctx
->origin
, &prefix
);
278 return (dns_name_totext(&prefix
, sub
, target
));
281 static inline isc_result_t
282 fromwire_in_naptr(ARGS_FROMWIRE
) {
285 unsigned char *regex
;
288 REQUIRE(rdclass
== 1);
293 dns_decompress_setmethods(dctx
, DNS_COMPRESS_NONE
);
295 dns_name_init(&name
, NULL
);
300 isc_buffer_activeregion(source
, &sr
);
302 return (ISC_R_UNEXPECTEDEND
);
303 RETERR(mem_tobuffer(target
, sr
.base
, 4));
304 isc_buffer_forward(source
, 4);
309 RETERR(txt_fromwire(source
, target
));
314 RETERR(txt_fromwire(source
, target
));
319 regex
= isc_buffer_used(target
);
320 RETERR(txt_fromwire(source
, target
));
321 RETERR(txt_valid_regex(regex
));
326 return (dns_name_fromwire(&name
, source
, dctx
, options
, target
));
329 static inline isc_result_t
330 towire_in_naptr(ARGS_TOWIRE
) {
332 dns_offsets_t offsets
;
335 REQUIRE(rdata
->type
== 35);
336 REQUIRE(rdata
->rdclass
== 1);
337 REQUIRE(rdata
->length
!= 0);
339 dns_compress_setmethods(cctx
, DNS_COMPRESS_NONE
);
343 dns_rdata_toregion(rdata
, &sr
);
344 RETERR(mem_tobuffer(target
, sr
.base
, 4));
345 isc_region_consume(&sr
, 4);
350 RETERR(mem_tobuffer(target
, sr
.base
, sr
.base
[0] + 1));
351 isc_region_consume(&sr
, sr
.base
[0] + 1);
356 RETERR(mem_tobuffer(target
, sr
.base
, sr
.base
[0] + 1));
357 isc_region_consume(&sr
, sr
.base
[0] + 1);
362 RETERR(mem_tobuffer(target
, sr
.base
, sr
.base
[0] + 1));
363 isc_region_consume(&sr
, sr
.base
[0] + 1);
368 dns_name_init(&name
, offsets
);
369 dns_name_fromregion(&name
, &sr
);
370 return (dns_name_towire(&name
, cctx
, target
));
374 compare_in_naptr(ARGS_COMPARE
) {
377 isc_region_t region1
;
378 isc_region_t region2
;
381 REQUIRE(rdata1
->type
== rdata2
->type
);
382 REQUIRE(rdata1
->rdclass
== rdata2
->rdclass
);
383 REQUIRE(rdata1
->type
== 35);
384 REQUIRE(rdata1
->rdclass
== 1);
385 REQUIRE(rdata1
->length
!= 0);
386 REQUIRE(rdata2
->length
!= 0);
388 dns_rdata_toregion(rdata1
, ®ion1
);
389 dns_rdata_toregion(rdata2
, ®ion2
);
394 order
= memcmp(region1
.base
, region2
.base
, 4);
396 return (order
< 0 ? -1 : 1);
397 isc_region_consume(®ion1
, 4);
398 isc_region_consume(®ion2
, 4);
403 len
= ISC_MIN(region1
.base
[0], region2
.base
[0]);
404 order
= memcmp(region1
.base
, region2
.base
, len
+ 1);
406 return (order
< 0 ? -1 : 1);
407 isc_region_consume(®ion1
, region1
.base
[0] + 1);
408 isc_region_consume(®ion2
, region2
.base
[0] + 1);
413 len
= ISC_MIN(region1
.base
[0], region2
.base
[0]);
414 order
= memcmp(region1
.base
, region2
.base
, len
+ 1);
416 return (order
< 0 ? -1 : 1);
417 isc_region_consume(®ion1
, region1
.base
[0] + 1);
418 isc_region_consume(®ion2
, region2
.base
[0] + 1);
423 len
= ISC_MIN(region1
.base
[0], region2
.base
[0]);
424 order
= memcmp(region1
.base
, region2
.base
, len
+ 1);
426 return (order
< 0 ? -1 : 1);
427 isc_region_consume(®ion1
, region1
.base
[0] + 1);
428 isc_region_consume(®ion2
, region2
.base
[0] + 1);
433 dns_name_init(&name1
, NULL
);
434 dns_name_init(&name2
, NULL
);
436 dns_name_fromregion(&name1
, ®ion1
);
437 dns_name_fromregion(&name2
, ®ion2
);
439 return (dns_name_rdatacompare(&name1
, &name2
));
442 static inline isc_result_t
443 fromstruct_in_naptr(ARGS_FROMSTRUCT
) {
444 dns_rdata_in_naptr_t
*naptr
= source
;
448 REQUIRE(rdclass
== 1);
449 REQUIRE(source
!= NULL
);
450 REQUIRE(naptr
->common
.rdtype
== type
);
451 REQUIRE(naptr
->common
.rdclass
== rdclass
);
452 REQUIRE(naptr
->flags
!= NULL
|| naptr
->flags_len
== 0);
453 REQUIRE(naptr
->service
!= NULL
|| naptr
->service_len
== 0);
454 REQUIRE(naptr
->regexp
!= NULL
|| naptr
->regexp_len
== 0);
459 RETERR(uint16_tobuffer(naptr
->order
, target
));
460 RETERR(uint16_tobuffer(naptr
->preference
, target
));
461 RETERR(uint8_tobuffer(naptr
->flags_len
, target
));
462 RETERR(mem_tobuffer(target
, naptr
->flags
, naptr
->flags_len
));
463 RETERR(uint8_tobuffer(naptr
->service_len
, target
));
464 RETERR(mem_tobuffer(target
, naptr
->service
, naptr
->service_len
));
465 RETERR(uint8_tobuffer(naptr
->regexp_len
, target
));
466 RETERR(mem_tobuffer(target
, naptr
->regexp
, naptr
->regexp_len
));
467 dns_name_toregion(&naptr
->replacement
, ®ion
);
468 return (isc_buffer_copyregion(target
, ®ion
));
471 static inline isc_result_t
472 tostruct_in_naptr(ARGS_TOSTRUCT
) {
473 dns_rdata_in_naptr_t
*naptr
= target
;
478 REQUIRE(rdata
->type
== 35);
479 REQUIRE(rdata
->rdclass
== 1);
480 REQUIRE(target
!= NULL
);
481 REQUIRE(rdata
->length
!= 0);
483 naptr
->common
.rdclass
= rdata
->rdclass
;
484 naptr
->common
.rdtype
= rdata
->type
;
485 ISC_LINK_INIT(&naptr
->common
, link
);
488 naptr
->service
= NULL
;
489 naptr
->regexp
= NULL
;
491 dns_rdata_toregion(rdata
, &r
);
493 naptr
->order
= uint16_fromregion(&r
);
494 isc_region_consume(&r
, 2);
496 naptr
->preference
= uint16_fromregion(&r
);
497 isc_region_consume(&r
, 2);
499 naptr
->flags_len
= uint8_fromregion(&r
);
500 isc_region_consume(&r
, 1);
501 INSIST(naptr
->flags_len
<= r
.length
);
502 naptr
->flags
= mem_maybedup(mctx
, r
.base
, naptr
->flags_len
);
503 if (naptr
->flags
== NULL
)
505 isc_region_consume(&r
, naptr
->flags_len
);
507 naptr
->service_len
= uint8_fromregion(&r
);
508 isc_region_consume(&r
, 1);
509 INSIST(naptr
->service_len
<= r
.length
);
510 naptr
->service
= mem_maybedup(mctx
, r
.base
, naptr
->service_len
);
511 if (naptr
->service
== NULL
)
513 isc_region_consume(&r
, naptr
->service_len
);
515 naptr
->regexp_len
= uint8_fromregion(&r
);
516 isc_region_consume(&r
, 1);
517 INSIST(naptr
->regexp_len
<= r
.length
);
518 naptr
->regexp
= mem_maybedup(mctx
, r
.base
, naptr
->regexp_len
);
519 if (naptr
->regexp
== NULL
)
521 isc_region_consume(&r
, naptr
->regexp_len
);
523 dns_name_init(&name
, NULL
);
524 dns_name_fromregion(&name
, &r
);
525 dns_name_init(&naptr
->replacement
, NULL
);
526 result
= name_duporclone(&name
, mctx
, &naptr
->replacement
);
527 if (result
!= ISC_R_SUCCESS
)
530 return (ISC_R_SUCCESS
);
533 if (mctx
!= NULL
&& naptr
->flags
!= NULL
)
534 isc_mem_free(mctx
, naptr
->flags
);
535 if (mctx
!= NULL
&& naptr
->service
!= NULL
)
536 isc_mem_free(mctx
, naptr
->service
);
537 if (mctx
!= NULL
&& naptr
->regexp
!= NULL
)
538 isc_mem_free(mctx
, naptr
->regexp
);
539 return (ISC_R_NOMEMORY
);
543 freestruct_in_naptr(ARGS_FREESTRUCT
) {
544 dns_rdata_in_naptr_t
*naptr
= source
;
546 REQUIRE(source
!= NULL
);
547 REQUIRE(naptr
->common
.rdclass
== 1);
548 REQUIRE(naptr
->common
.rdtype
== 35);
550 if (naptr
->mctx
== NULL
)
553 if (naptr
->flags
!= NULL
)
554 isc_mem_free(naptr
->mctx
, naptr
->flags
);
555 if (naptr
->service
!= NULL
)
556 isc_mem_free(naptr
->mctx
, naptr
->service
);
557 if (naptr
->regexp
!= NULL
)
558 isc_mem_free(naptr
->mctx
, naptr
->regexp
);
559 dns_name_free(&naptr
->replacement
, naptr
->mctx
);
563 static inline isc_result_t
564 additionaldata_in_naptr(ARGS_ADDLDATA
) {
566 dns_offsets_t offsets
;
568 dns_rdatatype_t atype
;
569 unsigned int i
, flagslen
;
572 REQUIRE(rdata
->type
== 35);
573 REQUIRE(rdata
->rdclass
== 1);
578 dns_rdata_toregion(rdata
, &sr
);
579 isc_region_consume(&sr
, 4);
585 flagslen
= sr
.base
[0];
586 cp
= (char *)&sr
.base
[1];
587 for (i
= 0; i
< flagslen
; i
++, cp
++) {
588 if (*cp
== 'S' || *cp
== 's') {
589 atype
= dns_rdatatype_srv
;
592 if (*cp
== 'A' || *cp
== 'a') {
593 atype
= dns_rdatatype_a
;
597 isc_region_consume(&sr
, flagslen
+ 1);
602 isc_region_consume(&sr
, sr
.base
[0] + 1);
607 isc_region_consume(&sr
, sr
.base
[0] + 1);
612 dns_name_init(&name
, offsets
);
613 dns_name_fromregion(&name
, &sr
);
616 return ((add
)(arg
, &name
, atype
));
618 return (ISC_R_SUCCESS
);
621 static inline isc_result_t
622 digest_in_naptr(ARGS_DIGEST
) {
624 unsigned int length
, n
;
628 REQUIRE(rdata
->type
== 35);
629 REQUIRE(rdata
->rdclass
== 1);
631 dns_rdata_toregion(rdata
, &r1
);
639 isc_region_consume(&r2
, 4);
646 isc_region_consume(&r2
, n
);
653 isc_region_consume(&r2
, n
);
660 isc_region_consume(&r2
, n
);
663 * Digest the RR up to the replacement name.
666 result
= (digest
)(arg
, &r1
);
667 if (result
!= ISC_R_SUCCESS
)
674 dns_name_init(&name
, NULL
);
675 dns_name_fromregion(&name
, &r2
);
677 return (dns_name_digest(&name
, digest
, arg
));
680 static inline isc_boolean_t
681 checkowner_in_naptr(ARGS_CHECKOWNER
) {
684 REQUIRE(rdclass
== 1);
694 static inline isc_boolean_t
695 checknames_in_naptr(ARGS_CHECKNAMES
) {
697 REQUIRE(rdata
->type
== 35);
698 REQUIRE(rdata
->rdclass
== 1);
708 casecompare_in_naptr(ARGS_COMPARE
) {
709 return (compare_in_naptr(rdata1
, rdata2
));
712 #endif /* RDATA_IN_1_NAPTR_35_C */