2 * Copyright (C) 2004-2006 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-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: rdata.c,v 1.147.2.11.2.22 2006/07/21 02:05:56 marka Exp $ */
23 #include <isc/base64.h>
27 #include <isc/parseint.h>
28 #include <isc/print.h>
29 #include <isc/string.h>
30 #include <isc/stdlib.h>
33 #include <dns/callbacks.h>
35 #include <dns/compress.h>
36 #include <dns/enumtype.h>
37 #include <dns/keyflags.h>
38 #include <dns/keyvalues.h>
39 #include <dns/rcode.h>
40 #include <dns/rdata.h>
41 #include <dns/rdataclass.h>
42 #include <dns/rdatastruct.h>
43 #include <dns/rdatatype.h>
44 #include <dns/result.h>
45 #include <dns/secalg.h>
46 #include <dns/secproto.h>
52 isc_result_t _r = (x); \
53 if (_r != ISC_R_SUCCESS) \
59 isc_result_t _r = (x); \
60 if (_r != ISC_R_SUCCESS) { \
61 isc_lex_ungettoken(lexer, &token); \
66 #define DNS_AS_STR(t) ((t).value.as_textregion.base)
68 #define ARGS_FROMTEXT int rdclass, dns_rdatatype_t type, \
69 isc_lex_t *lexer, dns_name_t *origin, \
70 unsigned int options, isc_buffer_t *target, \
71 dns_rdatacallbacks_t *callbacks
73 #define ARGS_TOTEXT dns_rdata_t *rdata, dns_rdata_textctx_t *tctx, \
76 #define ARGS_FROMWIRE int rdclass, dns_rdatatype_t type, \
77 isc_buffer_t *source, dns_decompress_t *dctx, \
78 unsigned int options, isc_buffer_t *target
80 #define ARGS_TOWIRE dns_rdata_t *rdata, dns_compress_t *cctx, \
83 #define ARGS_COMPARE const dns_rdata_t *rdata1, const dns_rdata_t *rdata2
85 #define ARGS_FROMSTRUCT int rdclass, dns_rdatatype_t type, \
86 void *source, isc_buffer_t *target
88 #define ARGS_TOSTRUCT dns_rdata_t *rdata, void *target, isc_mem_t *mctx
90 #define ARGS_FREESTRUCT void *source
92 #define ARGS_ADDLDATA dns_rdata_t *rdata, dns_additionaldatafunc_t add, \
95 #define ARGS_DIGEST dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg
97 #define ARGS_CHECKOWNER dns_name_t *name, dns_rdataclass_t rdclass, \
98 dns_rdatatype_t type, isc_boolean_t wildcard
100 #define ARGS_CHECKNAMES dns_rdata_t *rdata, dns_name_t *owner, dns_name_t *bad
104 * Context structure for the totext_ functions.
105 * Contains formatting options for rdata-to-text
108 typedef struct dns_rdata_textctx
{
109 dns_name_t
*origin
; /* Current origin, or NULL. */
110 unsigned int flags
; /* DNS_STYLEFLAG_* */
111 unsigned int width
; /* Width of rdata column. */
112 const char *linebreak
; /* Line break string. */
113 } dns_rdata_textctx_t
;
116 txt_totext(isc_region_t
*source
, isc_buffer_t
*target
);
119 txt_fromtext(isc_textregion_t
*source
, isc_buffer_t
*target
);
122 txt_fromwire(isc_buffer_t
*source
, isc_buffer_t
*target
);
125 name_prefix(dns_name_t
*name
, dns_name_t
*origin
, dns_name_t
*target
);
128 name_length(dns_name_t
*name
);
131 str_totext(const char *source
, isc_buffer_t
*target
);
134 inet_totext(int af
, isc_region_t
*src
, isc_buffer_t
*target
);
137 buffer_empty(isc_buffer_t
*source
);
140 buffer_fromregion(isc_buffer_t
*buffer
, isc_region_t
*region
);
143 uint32_tobuffer(isc_uint32_t
, isc_buffer_t
*target
);
146 uint16_tobuffer(isc_uint32_t
, isc_buffer_t
*target
);
149 uint8_tobuffer(isc_uint32_t
, isc_buffer_t
*target
);
152 name_tobuffer(dns_name_t
*name
, isc_buffer_t
*target
);
155 uint32_fromregion(isc_region_t
*region
);
158 uint16_fromregion(isc_region_t
*region
);
161 uint8_fromregion(isc_region_t
*region
);
164 mem_tobuffer(isc_buffer_t
*target
, void *base
, unsigned int length
);
167 hexvalue(char value
);
170 decvalue(char value
);
173 btoa_totext(unsigned char *inbuf
, int inbuflen
, isc_buffer_t
*target
);
176 atob_tobuffer(isc_lex_t
*lexer
, isc_buffer_t
*target
);
179 default_fromtext_callback(dns_rdatacallbacks_t
*callbacks
, const char *, ...)
180 ISC_FORMAT_PRINTF(2, 3);
183 fromtext_error(void (*callback
)(dns_rdatacallbacks_t
*, const char *, ...),
184 dns_rdatacallbacks_t
*callbacks
, const char *name
,
185 unsigned long line
, isc_token_t
*token
, isc_result_t result
);
188 fromtext_warneof(isc_lex_t
*lexer
, dns_rdatacallbacks_t
*callbacks
);
191 rdata_totext(dns_rdata_t
*rdata
, dns_rdata_textctx_t
*tctx
,
192 isc_buffer_t
*target
);
195 warn_badname(dns_name_t
*name
, isc_lex_t
*lexer
,
196 dns_rdatacallbacks_t
*callbacks
);
199 getquad(const void *src
, struct in_addr
*dst
,
200 isc_lex_t
*lexer
, dns_rdatacallbacks_t
*callbacks
)
205 result
= inet_aton(src
, dst
);
206 if (result
== 1 && callbacks
!= NULL
&&
207 inet_pton(AF_INET
, src
, &tmp
) != 1) {
208 const char *name
= isc_lex_getsourcename(lexer
);
211 (*callbacks
->warn
)(callbacks
, "%s:%lu: \"%s\" "
212 "is not a decimal dotted quad", name
,
213 isc_lex_getsourceline(lexer
), src
);
218 static inline isc_result_t
219 name_duporclone(dns_name_t
*source
, isc_mem_t
*mctx
, dns_name_t
*target
) {
222 return (dns_name_dup(source
, mctx
, target
));
223 dns_name_clone(source
, target
);
224 return (ISC_R_SUCCESS
);
228 mem_maybedup(isc_mem_t
*mctx
, void *source
, size_t length
) {
233 new = isc_mem_allocate(mctx
, length
);
235 memcpy(new, source
, length
);
240 static const char hexdigits
[] = "0123456789abcdef";
241 static const char decdigits
[] = "0123456789";
246 #define RESERVED 0x0002
253 dns_rdata_init(dns_rdata_t
*rdata
) {
255 REQUIRE(rdata
!= NULL
);
262 ISC_LINK_INIT(rdata
, link
);
263 /* ISC_LIST_INIT(rdata->list); */
267 #define DNS_RDATA_INITIALIZED(rdata) \
268 ((rdata)->data == NULL && (rdata)->length == 0 && \
269 (rdata)->rdclass == 0 && (rdata)->type == 0 && (rdata)->flags == 0 && \
270 !ISC_LINK_LINKED((rdata), link))
272 #ifdef ISC_LIST_CHECKINIT
273 #define DNS_RDATA_INITIALIZED(rdata) \
274 (!ISC_LINK_LINKED((rdata), link))
276 #define DNS_RDATA_INITIALIZED(rdata) ISC_TRUE
279 #define DNS_RDATA_VALIDFLAGS(rdata) \
280 (((rdata)->flags & ~DNS_RDATA_UPDATE) == 0)
283 dns_rdata_reset(dns_rdata_t
*rdata
) {
285 REQUIRE(rdata
!= NULL
);
287 REQUIRE(!ISC_LINK_LINKED(rdata
, link
));
288 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata
));
302 dns_rdata_clone(const dns_rdata_t
*src
, dns_rdata_t
*target
) {
304 REQUIRE(src
!= NULL
);
305 REQUIRE(target
!= NULL
);
307 REQUIRE(DNS_RDATA_INITIALIZED(target
));
309 REQUIRE(DNS_RDATA_VALIDFLAGS(src
));
310 REQUIRE(DNS_RDATA_VALIDFLAGS(target
));
312 target
->data
= src
->data
;
313 target
->length
= src
->length
;
314 target
->rdclass
= src
->rdclass
;
315 target
->type
= src
->type
;
316 target
->flags
= src
->flags
;
325 dns_rdata_compare(const dns_rdata_t
*rdata1
, const dns_rdata_t
*rdata2
) {
327 isc_boolean_t use_default
= ISC_FALSE
;
329 REQUIRE(rdata1
!= NULL
);
330 REQUIRE(rdata2
!= NULL
);
331 REQUIRE(rdata1
->data
!= NULL
);
332 REQUIRE(rdata2
->data
!= NULL
);
333 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata1
));
334 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata2
));
336 if (rdata1
->rdclass
!= rdata2
->rdclass
)
337 return (rdata1
->rdclass
< rdata2
->rdclass
? -1 : 1);
339 if (rdata1
->type
!= rdata2
->type
)
340 return (rdata1
->type
< rdata2
->type
? -1 : 1);
348 dns_rdata_toregion(rdata1
, &r1
);
349 dns_rdata_toregion(rdata2
, &r2
);
350 result
= isc_region_compare(&r1
, &r2
);
360 dns_rdata_fromregion(dns_rdata_t
*rdata
, dns_rdataclass_t rdclass
,
361 dns_rdatatype_t type
, isc_region_t
*r
)
364 REQUIRE(rdata
!= NULL
);
365 REQUIRE(DNS_RDATA_INITIALIZED(rdata
));
368 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata
));
370 rdata
->data
= r
->base
;
371 rdata
->length
= r
->length
;
372 rdata
->rdclass
= rdclass
;
378 dns_rdata_toregion(const dns_rdata_t
*rdata
, isc_region_t
*r
) {
380 REQUIRE(rdata
!= NULL
);
382 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata
));
384 r
->base
= rdata
->data
;
385 r
->length
= rdata
->length
;
389 dns_rdata_fromwire(dns_rdata_t
*rdata
, dns_rdataclass_t rdclass
,
390 dns_rdatatype_t type
, isc_buffer_t
*source
,
391 dns_decompress_t
*dctx
, unsigned int options
,
392 isc_buffer_t
*target
)
394 isc_result_t result
= ISC_R_NOTIMPLEMENTED
;
398 isc_boolean_t use_default
= ISC_FALSE
;
399 isc_uint32_t activelength
;
401 REQUIRE(dctx
!= NULL
);
403 REQUIRE(DNS_RDATA_INITIALIZED(rdata
));
404 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata
));
408 return (DNS_R_FORMERR
);
413 activelength
= isc_buffer_activelength(source
);
414 INSIST(activelength
< 65536);
419 if (activelength
> isc_buffer_availablelength(target
))
420 result
= ISC_R_NOSPACE
;
422 isc_buffer_putmem(target
, isc_buffer_current(source
),
424 isc_buffer_forward(source
, activelength
);
425 result
= ISC_R_SUCCESS
;
430 * We should have consumed all of our buffer.
432 if (result
== ISC_R_SUCCESS
&& !buffer_empty(source
))
433 result
= DNS_R_EXTRADATA
;
435 if (rdata
!= NULL
&& result
== ISC_R_SUCCESS
) {
436 region
.base
= isc_buffer_used(&st
);
437 region
.length
= isc_buffer_usedlength(target
) -
438 isc_buffer_usedlength(&st
);
439 dns_rdata_fromregion(rdata
, rdclass
, type
, ®ion
);
442 if (result
!= ISC_R_SUCCESS
) {
450 dns_rdata_towire(dns_rdata_t
*rdata
, dns_compress_t
*cctx
,
451 isc_buffer_t
*target
)
453 isc_result_t result
= ISC_R_NOTIMPLEMENTED
;
454 isc_boolean_t use_default
= ISC_FALSE
;
458 REQUIRE(rdata
!= NULL
);
459 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata
));
462 * Some DynDNS meta-RRs have empty rdata.
464 if ((rdata
->flags
& DNS_RDATA_UPDATE
) != 0) {
465 INSIST(rdata
->length
== 0);
466 return (ISC_R_SUCCESS
);
474 isc_buffer_availableregion(target
, &tr
);
475 if (tr
.length
< rdata
->length
)
476 return (ISC_R_NOSPACE
);
477 memcpy(tr
.base
, rdata
->data
, rdata
->length
);
478 isc_buffer_add(target
, rdata
->length
);
479 return (ISC_R_SUCCESS
);
481 if (result
!= ISC_R_SUCCESS
) {
483 INSIST(target
->used
< 65536);
484 dns_compress_rollback(cctx
, (isc_uint16_t
)target
->used
);
490 * If the binary data in 'src' is valid uncompressed wire format
491 * rdata of class 'rdclass' and type 'type', return ISC_R_SUCCESS
492 * and copy the validated rdata to 'dest'. Otherwise return an error.
495 rdata_validate(isc_buffer_t
*src
, isc_buffer_t
*dest
, dns_rdataclass_t rdclass
,
496 dns_rdatatype_t type
)
498 dns_decompress_t dctx
;
499 dns_rdata_t rdata
= DNS_RDATA_INIT
;
502 dns_decompress_init(&dctx
, -1, DNS_DECOMPRESS_NONE
);
503 isc_buffer_setactive(src
, isc_buffer_usedlength(src
));
504 result
= dns_rdata_fromwire(&rdata
, rdclass
, type
, src
,
506 dns_decompress_invalidate(&dctx
);
512 unknown_fromtext(dns_rdataclass_t rdclass
, dns_rdatatype_t type
,
513 isc_lex_t
*lexer
, isc_mem_t
*mctx
, isc_buffer_t
*target
)
516 isc_buffer_t
*buf
= NULL
;
519 if (type
== 0 || dns_rdatatype_ismeta(type
))
520 return (DNS_R_METATYPE
);
522 result
= isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
524 if (result
== ISC_R_SUCCESS
&& token
.value
.as_ulong
> 65535U)
525 return (ISC_R_RANGE
);
526 result
= isc_buffer_allocate(mctx
, &buf
, token
.value
.as_ulong
);
527 if (result
!= ISC_R_SUCCESS
)
530 result
= isc_hex_tobuffer(lexer
, buf
,
531 (unsigned int)token
.value
.as_ulong
);
532 if (result
!= ISC_R_SUCCESS
)
534 if (isc_buffer_usedlength(buf
) != token
.value
.as_ulong
) {
535 result
= ISC_R_UNEXPECTEDEND
;
539 if (dns_rdatatype_isknown(type
)) {
540 result
= rdata_validate(buf
, target
, rdclass
, type
);
543 isc_buffer_usedregion(buf
, &r
);
544 result
= isc_buffer_copyregion(target
, &r
);
546 if (result
!= ISC_R_SUCCESS
)
549 isc_buffer_free(&buf
);
550 return (ISC_R_SUCCESS
);
553 isc_buffer_free(&buf
);
558 dns_rdata_fromtext(dns_rdata_t
*rdata
, dns_rdataclass_t rdclass
,
559 dns_rdatatype_t type
, isc_lex_t
*lexer
,
560 dns_name_t
*origin
, unsigned int options
, isc_mem_t
*mctx
,
561 isc_buffer_t
*target
, dns_rdatacallbacks_t
*callbacks
)
563 isc_result_t result
= ISC_R_NOTIMPLEMENTED
;
567 unsigned int lexoptions
= ISC_LEXOPT_EOL
| ISC_LEXOPT_EOF
|
568 ISC_LEXOPT_DNSMULTILINE
| ISC_LEXOPT_ESCAPE
;
571 void (*callback
)(dns_rdatacallbacks_t
*, const char *, ...);
572 isc_result_t tresult
;
574 REQUIRE(origin
== NULL
|| dns_name_isabsolute(origin
) == ISC_TRUE
);
576 REQUIRE(DNS_RDATA_INITIALIZED(rdata
));
577 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata
));
579 if (callbacks
!= NULL
) {
580 REQUIRE(callbacks
->warn
!= NULL
);
581 REQUIRE(callbacks
->error
!= NULL
);
586 if (callbacks
!= NULL
)
587 callback
= callbacks
->error
;
589 callback
= default_fromtext_callback
;
591 result
= isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_qstring
,
593 if (result
!= ISC_R_SUCCESS
) {
594 name
= isc_lex_getsourcename(lexer
);
595 line
= isc_lex_getsourceline(lexer
);
596 fromtext_error(callback
, callbacks
, name
, line
,
601 if (strcmp(DNS_AS_STR(token
), "\\#") == 0)
602 result
= unknown_fromtext(rdclass
, type
, lexer
, mctx
, target
);
604 isc_lex_ungettoken(lexer
, &token
);
610 * Consume to end of line / file.
611 * If not at end of line initially set error code.
612 * Call callback via fromtext_error once if there was an error.
615 name
= isc_lex_getsourcename(lexer
);
616 line
= isc_lex_getsourceline(lexer
);
617 tresult
= isc_lex_gettoken(lexer
, lexoptions
, &token
);
618 if (tresult
!= ISC_R_SUCCESS
) {
619 if (result
== ISC_R_SUCCESS
)
621 if (callback
!= NULL
)
622 fromtext_error(callback
, callbacks
, name
,
625 } else if (token
.type
!= isc_tokentype_eol
&&
626 token
.type
!= isc_tokentype_eof
) {
627 if (result
== ISC_R_SUCCESS
)
628 result
= DNS_R_EXTRATOKEN
;
629 if (callback
!= NULL
) {
630 fromtext_error(callback
, callbacks
, name
,
631 line
, &token
, result
);
634 } else if (result
!= ISC_R_SUCCESS
&& callback
!= NULL
) {
635 fromtext_error(callback
, callbacks
, name
, line
,
639 if (token
.type
== isc_tokentype_eof
)
640 fromtext_warneof(lexer
, callbacks
);
645 if (rdata
!= NULL
&& result
== ISC_R_SUCCESS
) {
646 region
.base
= isc_buffer_used(&st
);
647 region
.length
= isc_buffer_usedlength(target
) -
648 isc_buffer_usedlength(&st
);
649 dns_rdata_fromregion(rdata
, rdclass
, type
, ®ion
);
651 if (result
!= ISC_R_SUCCESS
) {
658 rdata_totext(dns_rdata_t
*rdata
, dns_rdata_textctx_t
*tctx
,
659 isc_buffer_t
*target
)
661 isc_result_t result
= ISC_R_NOTIMPLEMENTED
;
662 isc_boolean_t use_default
= ISC_FALSE
;
663 char buf
[sizeof("65535")];
666 REQUIRE(rdata
!= NULL
);
667 REQUIRE(tctx
->origin
== NULL
||
668 dns_name_isabsolute(tctx
->origin
) == ISC_TRUE
);
671 * Some DynDNS meta-RRs have empty rdata.
673 if ((rdata
->flags
& DNS_RDATA_UPDATE
) != 0) {
674 INSIST(rdata
->length
== 0);
675 return (ISC_R_SUCCESS
);
681 strlcpy(buf
, "\\# ", sizeof(buf
));
682 result
= str_totext(buf
, target
);
683 dns_rdata_toregion(rdata
, &sr
);
684 INSIST(sr
.length
< 65536);
685 snprintf(buf
, sizeof(buf
), "%u", sr
.length
);
686 result
= str_totext(buf
, target
);
687 if (sr
.length
!= 0 && result
== ISC_R_SUCCESS
) {
688 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
689 result
= str_totext(" ( ", target
);
691 result
= str_totext(" ", target
);
692 if (result
== ISC_R_SUCCESS
)
693 result
= isc_hex_totext(&sr
, tctx
->width
- 2,
696 if (result
== ISC_R_SUCCESS
&&
697 (tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
698 result
= str_totext(" )", target
);
706 dns_rdata_totext(dns_rdata_t
*rdata
, dns_name_t
*origin
, isc_buffer_t
*target
)
708 dns_rdata_textctx_t tctx
;
710 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata
));
713 * Set up formatting options for single-line output.
715 tctx
.origin
= origin
;
718 tctx
.linebreak
= " ";
719 return (rdata_totext(rdata
, &tctx
, target
));
723 dns_rdata_tofmttext(dns_rdata_t
*rdata
, dns_name_t
*origin
,
724 unsigned int flags
, unsigned int width
,
725 char *linebreak
, isc_buffer_t
*target
)
727 dns_rdata_textctx_t tctx
;
729 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata
));
732 * Set up formatting options for formatted output.
734 tctx
.origin
= origin
;
736 if ((flags
& DNS_STYLEFLAG_MULTILINE
) != 0) {
738 tctx
.linebreak
= linebreak
;
740 tctx
.width
= 60; /* Used for hex word length only. */
741 tctx
.linebreak
= " ";
743 return (rdata_totext(rdata
, &tctx
, target
));
747 dns_rdata_fromstruct(dns_rdata_t
*rdata
, dns_rdataclass_t rdclass
,
748 dns_rdatatype_t type
, void *source
,
749 isc_buffer_t
*target
)
751 isc_result_t result
= ISC_R_NOTIMPLEMENTED
;
754 isc_boolean_t use_default
= ISC_FALSE
;
756 REQUIRE(source
!= NULL
);
758 REQUIRE(DNS_RDATA_INITIALIZED(rdata
));
759 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata
));
769 if (rdata
!= NULL
&& result
== ISC_R_SUCCESS
) {
770 region
.base
= isc_buffer_used(&st
);
771 region
.length
= isc_buffer_usedlength(target
) -
772 isc_buffer_usedlength(&st
);
773 dns_rdata_fromregion(rdata
, rdclass
, type
, ®ion
);
775 if (result
!= ISC_R_SUCCESS
)
781 dns_rdata_tostruct(dns_rdata_t
*rdata
, void *target
, isc_mem_t
*mctx
) {
782 isc_result_t result
= ISC_R_NOTIMPLEMENTED
;
783 isc_boolean_t use_default
= ISC_FALSE
;
785 REQUIRE(rdata
!= NULL
);
786 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata
));
797 dns_rdata_freestruct(void *source
) {
798 dns_rdatacommon_t
*common
= source
;
799 REQUIRE(source
!= NULL
);
805 dns_rdata_additionaldata(dns_rdata_t
*rdata
, dns_additionaldatafunc_t add
,
808 isc_result_t result
= ISC_R_NOTIMPLEMENTED
;
809 isc_boolean_t use_default
= ISC_FALSE
;
812 * Call 'add' for each name and type from 'rdata' which is subject to
813 * additional section processing.
816 REQUIRE(rdata
!= NULL
);
817 REQUIRE(add
!= NULL
);
818 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata
));
822 /* No additional processing for unknown types */
824 result
= ISC_R_SUCCESS
;
830 dns_rdata_digest(dns_rdata_t
*rdata
, dns_digestfunc_t digest
, void *arg
) {
831 isc_result_t result
= ISC_R_NOTIMPLEMENTED
;
832 isc_boolean_t use_default
= ISC_FALSE
;
836 * Send 'rdata' in DNSSEC canonical form to 'digest'.
839 REQUIRE(rdata
!= NULL
);
840 REQUIRE(digest
!= NULL
);
841 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata
));
846 dns_rdata_toregion(rdata
, &r
);
847 result
= (digest
)(arg
, &r
);
854 dns_rdata_checkowner(dns_name_t
*name
, dns_rdataclass_t rdclass
,
855 dns_rdatatype_t type
, isc_boolean_t wildcard
)
857 isc_boolean_t result
;
864 dns_rdata_checknames(dns_rdata_t
*rdata
, dns_name_t
*owner
, dns_name_t
*bad
)
866 isc_boolean_t result
;
873 dns_rdatatype_attributes(dns_rdatatype_t type
)
875 RDATATYPE_ATTRIBUTE_SW
876 if (type
>= (dns_rdatatype_t
)128 && type
< (dns_rdatatype_t
)255)
877 return (DNS_RDATATYPEATTR_UNKNOWN
| DNS_RDATATYPEATTR_META
);
878 return (DNS_RDATATYPEATTR_UNKNOWN
);
882 dns_rdatatype_fromtext(dns_rdatatype_t
*typep
, isc_textregion_t
*source
) {
890 return (DNS_R_UNKNOWN
);
892 a
= tolower((unsigned char)source
->base
[0]);
893 b
= tolower((unsigned char)source
->base
[n
- 1]);
895 hash
= ((a
+ n
) * b
) % 256;
898 * This switch block is inlined via #define, and will use "return"
899 * to return a result to the caller if it is a valid (known)
902 RDATATYPE_FROMTEXT_SW(hash
, source
->base
, n
, typep
);
904 if (source
->length
> 4 && source
->length
< (4 + sizeof("65000")) &&
905 strncasecmp("type", source
->base
, 4) == 0) {
906 char buf
[sizeof("65000")];
910 strncpy(buf
, source
->base
+ 4, source
->length
- 4);
911 buf
[source
->length
- 4] = '\0';
912 val
= strtoul(buf
, &endp
, 10);
913 if (*endp
== '\0' && val
<= 0xffff) {
914 *typep
= (dns_rdatatype_t
)val
;
915 return (ISC_R_SUCCESS
);
919 return (DNS_R_UNKNOWN
);
923 dns_rdatatype_totext(dns_rdatatype_t type
, isc_buffer_t
*target
) {
924 char buf
[sizeof("TYPE65535")];
927 snprintf(buf
, sizeof(buf
), "TYPE%u", type
);
928 return (str_totext(buf
, target
));
932 dns_rdatatype_format(dns_rdatatype_t rdtype
,
933 char *array
, unsigned int size
)
938 isc_buffer_init(&buf
, array
, size
);
939 result
= dns_rdatatype_totext(rdtype
, &buf
);
943 if (result
== ISC_R_SUCCESS
) {
944 if (isc_buffer_availablelength(&buf
) >= 1)
945 isc_buffer_putuint8(&buf
, 0);
947 result
= ISC_R_NOSPACE
;
949 if (result
!= ISC_R_SUCCESS
) {
950 snprintf(array
, size
, "<unknown>");
951 array
[size
- 1] = '\0';
960 name_length(dns_name_t
*name
) {
961 return (name
->length
);
965 txt_totext(isc_region_t
*source
, isc_buffer_t
*target
) {
972 isc_buffer_availableregion(target
, ®ion
);
974 tp
= (char *)region
.base
;
979 REQUIRE(n
+ 1 <= source
->length
);
982 return (ISC_R_NOSPACE
);
986 if (*sp
< 0x20 || *sp
>= 0x7f) {
988 return (ISC_R_NOSPACE
);
990 *tp
++ = 0x30 + ((*sp
/ 100) % 10);
991 *tp
++ = 0x30 + ((*sp
/ 10) % 10);
992 *tp
++ = 0x30 + (*sp
% 10);
997 /* double quote, semi-colon, backslash */
998 if (*sp
== 0x22 || *sp
== 0x3b || *sp
== 0x5c) {
1000 return (ISC_R_NOSPACE
);
1005 return (ISC_R_NOSPACE
);
1010 return (ISC_R_NOSPACE
);
1013 isc_buffer_add(target
, tp
- (char *)region
.base
);
1014 isc_region_consume(source
, *source
->base
+ 1);
1015 return (ISC_R_SUCCESS
);
1019 txt_fromtext(isc_textregion_t
*source
, isc_buffer_t
*target
) {
1020 isc_region_t tregion
;
1021 isc_boolean_t escape
;
1022 unsigned int n
, nrem
;
1028 isc_buffer_availableregion(target
, &tregion
);
1032 nrem
= tregion
.length
;
1035 return (ISC_R_NOSPACE
);
1042 * Maximum text string length.
1048 if (escape
&& (d
= decvalue((char)c
)) != -1) {
1051 return (DNS_R_SYNTAX
);
1053 if ((d
= decvalue(*s
++)) != -1)
1056 return (DNS_R_SYNTAX
);
1058 return (DNS_R_SYNTAX
);
1060 if ((d
= decvalue(*s
++)) != -1)
1063 return (DNS_R_SYNTAX
);
1065 return (DNS_R_SYNTAX
);
1066 } else if (!escape
&& c
== '\\') {
1072 return (ISC_R_NOSPACE
);
1077 return (DNS_R_SYNTAX
);
1078 *tregion
.base
= t
- tregion
.base
- 1;
1079 isc_buffer_add(target
, *tregion
.base
+ 1);
1080 return (ISC_R_SUCCESS
);
1084 txt_fromwire(isc_buffer_t
*source
, isc_buffer_t
*target
) {
1086 isc_region_t sregion
;
1087 isc_region_t tregion
;
1089 isc_buffer_activeregion(source
, &sregion
);
1090 if (sregion
.length
== 0)
1091 return(ISC_R_UNEXPECTEDEND
);
1092 n
= *sregion
.base
+ 1;
1093 if (n
> sregion
.length
)
1094 return (ISC_R_UNEXPECTEDEND
);
1096 isc_buffer_availableregion(target
, &tregion
);
1097 if (n
> tregion
.length
)
1098 return (ISC_R_NOSPACE
);
1100 memcpy(tregion
.base
, sregion
.base
, n
);
1101 isc_buffer_forward(source
, n
);
1102 isc_buffer_add(target
, n
);
1103 return (ISC_R_SUCCESS
);
1106 static isc_boolean_t
1107 name_prefix(dns_name_t
*name
, dns_name_t
*origin
, dns_name_t
*target
) {
1113 if (dns_name_compare(origin
, dns_rootname
) == 0)
1116 if (!dns_name_issubdomain(name
, origin
))
1119 l1
= dns_name_countlabels(name
);
1120 l2
= dns_name_countlabels(origin
);
1125 dns_name_getlabelsequence(name
, 0, l1
- l2
, target
);
1134 str_totext(const char *source
, isc_buffer_t
*target
) {
1136 isc_region_t region
;
1138 isc_buffer_availableregion(target
, ®ion
);
1141 if (l
> region
.length
)
1142 return (ISC_R_NOSPACE
);
1144 memcpy(region
.base
, source
, l
);
1145 isc_buffer_add(target
, l
);
1146 return (ISC_R_SUCCESS
);
1150 inet_totext(int af
, isc_region_t
*src
, isc_buffer_t
*target
) {
1153 /* Note - inet_ntop doesn't do size checking on its input. */
1154 if (inet_ntop(af
, src
->base
, tmpbuf
, sizeof(tmpbuf
)) == NULL
)
1155 return (ISC_R_NOSPACE
);
1156 if (strlen(tmpbuf
) > isc_buffer_availablelength(target
))
1157 return (ISC_R_NOSPACE
);
1158 isc_buffer_putstr(target
, tmpbuf
);
1159 return (ISC_R_SUCCESS
);
1162 static isc_boolean_t
1163 buffer_empty(isc_buffer_t
*source
) {
1164 return((source
->current
== source
->active
) ? ISC_TRUE
: ISC_FALSE
);
1168 buffer_fromregion(isc_buffer_t
*buffer
, isc_region_t
*region
) {
1169 isc_buffer_init(buffer
, region
->base
, region
->length
);
1170 isc_buffer_add(buffer
, region
->length
);
1171 isc_buffer_setactive(buffer
, region
->length
);
1175 uint32_tobuffer(isc_uint32_t value
, isc_buffer_t
*target
) {
1176 isc_region_t region
;
1178 isc_buffer_availableregion(target
, ®ion
);
1179 if (region
.length
< 4)
1180 return (ISC_R_NOSPACE
);
1181 isc_buffer_putuint32(target
, value
);
1182 return (ISC_R_SUCCESS
);
1186 uint16_tobuffer(isc_uint32_t value
, isc_buffer_t
*target
) {
1187 isc_region_t region
;
1190 return (ISC_R_RANGE
);
1191 isc_buffer_availableregion(target
, ®ion
);
1192 if (region
.length
< 2)
1193 return (ISC_R_NOSPACE
);
1194 isc_buffer_putuint16(target
, (isc_uint16_t
)value
);
1195 return (ISC_R_SUCCESS
);
1199 uint8_tobuffer(isc_uint32_t value
, isc_buffer_t
*target
) {
1200 isc_region_t region
;
1203 return (ISC_R_RANGE
);
1204 isc_buffer_availableregion(target
, ®ion
);
1205 if (region
.length
< 1)
1206 return (ISC_R_NOSPACE
);
1207 isc_buffer_putuint8(target
, (isc_uint8_t
)value
);
1208 return (ISC_R_SUCCESS
);
1212 name_tobuffer(dns_name_t
*name
, isc_buffer_t
*target
) {
1214 dns_name_toregion(name
, &r
);
1215 return (isc_buffer_copyregion(target
, &r
));
1219 uint32_fromregion(isc_region_t
*region
) {
1222 REQUIRE(region
->length
>= 4);
1223 value
= region
->base
[0] << 24;
1224 value
|= region
->base
[1] << 16;
1225 value
|= region
->base
[2] << 8;
1226 value
|= region
->base
[3];
1231 uint16_fromregion(isc_region_t
*region
) {
1233 REQUIRE(region
->length
>= 2);
1235 return ((region
->base
[0] << 8) | region
->base
[1]);
1239 uint8_fromregion(isc_region_t
*region
) {
1241 REQUIRE(region
->length
>= 1);
1243 return (region
->base
[0]);
1247 mem_tobuffer(isc_buffer_t
*target
, void *base
, unsigned int length
) {
1250 isc_buffer_availableregion(target
, &tr
);
1251 if (length
> tr
.length
)
1252 return (ISC_R_NOSPACE
);
1253 memcpy(tr
.base
, base
, length
);
1254 isc_buffer_add(target
, length
);
1255 return (ISC_R_SUCCESS
);
1259 hexvalue(char value
) {
1263 c
= (unsigned char)value
;
1269 if ((s
= strchr(hexdigits
, c
)) == NULL
)
1271 return (s
- hexdigits
);
1275 decvalue(char value
) {
1279 * isascii() is valid for full range of int values, no need to
1282 if (!isascii(value
))
1284 if ((s
= strchr(decdigits
, value
)) == NULL
)
1286 return (s
- decdigits
);
1289 static const char atob_digits
[86] =
1290 "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`" \
1291 "abcdefghijklmnopqrstu";
1293 * Subroutines to convert between 8 bit binary bytes and printable ASCII.
1294 * Computes the number of bytes, and three kinds of simple checksums.
1295 * Incoming bytes are collected into 32-bit words, then printed in base 85:
1296 * exp(85,5) > exp(2,32)
1297 * The ASCII characters used are between '!' and 'u';
1298 * 'z' encodes 32-bit zero; 'x' is used to mark the end of encoded data.
1300 * Originally by Paul Rutter (philabs!per) and Joe Orost (petsd!joe) for
1301 * the atob/btoa programs, released with the compress program, in mod.sources.
1302 * Modified by Mike Schwartz 8/19/86 for use in BIND.
1303 * Modified to be re-entrant 3/2/99.
1315 #define Ceor state->Ceor
1316 #define Csum state->Csum
1317 #define Crot state->Crot
1318 #define word state->word
1319 #define bcount state->bcount
1321 #define times85(x) ((((((x<<2)+x)<<2)+x)<<2)+x)
1323 static isc_result_t
byte_atob(int c
, isc_buffer_t
*target
,
1324 struct state
*state
);
1325 static isc_result_t
putbyte(int c
, isc_buffer_t
*, struct state
*state
);
1326 static isc_result_t
byte_btoa(int c
, isc_buffer_t
*, struct state
*state
);
1329 * Decode ASCII-encoded byte c into binary representation and
1330 * place into *bufp, advancing bufp.
1333 byte_atob(int c
, isc_buffer_t
*target
, struct state
*state
) {
1337 return(DNS_R_SYNTAX
);
1339 RETERR(putbyte(0, target
, state
));
1340 RETERR(putbyte(0, target
, state
));
1341 RETERR(putbyte(0, target
, state
));
1342 RETERR(putbyte(0, target
, state
));
1344 } else if ((s
= strchr(atob_digits
, c
)) != NULL
) {
1346 word
= s
- atob_digits
;
1348 } else if (bcount
< 4) {
1349 word
= times85(word
);
1350 word
+= s
- atob_digits
;
1353 word
= times85(word
);
1354 word
+= s
- atob_digits
;
1355 RETERR(putbyte((word
>> 24) & 0xff, target
, state
));
1356 RETERR(putbyte((word
>> 16) & 0xff, target
, state
));
1357 RETERR(putbyte((word
>> 8) & 0xff, target
, state
));
1358 RETERR(putbyte(word
& 0xff, target
, state
));
1363 return(DNS_R_SYNTAX
);
1364 return(ISC_R_SUCCESS
);
1368 * Compute checksum info and place c into target.
1371 putbyte(int c
, isc_buffer_t
*target
, struct state
*state
) {
1377 if ((Crot
& 0x80000000)) {
1384 isc_buffer_availableregion(target
, &tr
);
1386 return (ISC_R_NOSPACE
);
1388 isc_buffer_add(target
, 1);
1389 return (ISC_R_SUCCESS
);
1393 * Read the ASCII-encoded data from inbuf, of length inbuflen, and convert
1394 * it into T_UNSPEC (binary data) in outbuf, not to exceed outbuflen bytes;
1395 * outbuflen must be divisible by 4. (Note: this is because outbuf is filled
1396 * in 4 bytes at a time. If the actual data doesn't end on an even 4-byte
1397 * boundary, there will be no problem...it will be padded with 0 bytes, and
1398 * numbytes will indicate the correct number of bytes. The main point is
1399 * that since the buffer is filled in 4 bytes at a time, even if there is
1400 * not a full 4 bytes of data at the end, there has to be room to 0-pad the
1401 * data, so the buffer must be of size divisible by 4). Place the number of
1402 * output bytes in numbytes, and return a failure/success status.
1406 atob_tobuffer(isc_lex_t
*lexer
, isc_buffer_t
*target
) {
1407 long oeor
, osum
, orot
;
1408 struct state statebuf
, *state
= &statebuf
;
1413 Ceor
= Csum
= Crot
= word
= bcount
= 0;
1415 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
1417 while (token
.value
.as_textregion
.length
!= 0) {
1418 if ((c
= token
.value
.as_textregion
.base
[0]) == 'x') {
1421 RETERR(byte_atob(c
, target
, state
));
1422 isc_textregion_consume(&token
.value
.as_textregion
, 1);
1428 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
1430 if ((token
.value
.as_ulong
% 4) != 0U)
1431 isc_buffer_subtract(target
, 4 - (token
.value
.as_ulong
% 4));
1436 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
1438 oeor
= strtol(DNS_AS_STR(token
), &e
, 16);
1440 return (DNS_R_SYNTAX
);
1445 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
1447 osum
= strtol(DNS_AS_STR(token
), &e
, 16);
1449 return (DNS_R_SYNTAX
);
1454 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
1456 orot
= strtol(DNS_AS_STR(token
), &e
, 16);
1458 return (DNS_R_SYNTAX
);
1460 if ((oeor
!= Ceor
) || (osum
!= Csum
) || (orot
!= Crot
))
1461 return(DNS_R_BADCKSUM
);
1462 return (ISC_R_SUCCESS
);
1466 * Encode binary byte c into ASCII representation and place into *bufp,
1470 byte_btoa(int c
, isc_buffer_t
*target
, struct state
*state
) {
1473 isc_buffer_availableregion(target
, &tr
);
1477 if ((Crot
& 0x80000000)) {
1490 return (ISC_R_NOSPACE
);
1492 isc_buffer_add(target
, 1);
1494 register int tmp
= 0;
1495 register isc_int32_t tmpword
= word
;
1499 * Because some don't support u_long.
1502 tmpword
-= (isc_int32_t
)(85 * 85 * 85 * 85 * 32);
1506 tmpword
-= (isc_int32_t
)(85 * 85 * 85 * 85 * 32);
1509 return (ISC_R_NOSPACE
);
1510 tr
.base
[0] = atob_digits
[(tmpword
/
1511 (isc_int32_t
)(85 * 85 * 85 * 85))
1513 tmpword
%= (isc_int32_t
)(85 * 85 * 85 * 85);
1514 tr
.base
[1] = atob_digits
[tmpword
/ (85 * 85 * 85)];
1515 tmpword
%= (85 * 85 * 85);
1516 tr
.base
[2] = atob_digits
[tmpword
/ (85 * 85)];
1517 tmpword
%= (85 * 85);
1518 tr
.base
[3] = atob_digits
[tmpword
/ 85];
1520 tr
.base
[4] = atob_digits
[tmpword
];
1521 isc_buffer_add(target
, 5);
1527 return (ISC_R_SUCCESS
);
1532 * Encode the binary data from inbuf, of length inbuflen, into a
1533 * target. Return success/failure status
1536 btoa_totext(unsigned char *inbuf
, int inbuflen
, isc_buffer_t
*target
) {
1538 struct state statebuf
, *state
= &statebuf
;
1539 char buf
[sizeof("x 2000000000 ffffffff ffffffff ffffffff")];
1541 Ceor
= Csum
= Crot
= word
= bcount
= 0;
1542 for (inc
= 0; inc
< inbuflen
; inbuf
++, inc
++)
1543 RETERR(byte_btoa(*inbuf
, target
, state
));
1546 RETERR(byte_btoa(0, target
, state
));
1549 * Put byte count and checksum information at end of buffer,
1552 snprintf(buf
, sizeof(buf
), "x %d %x %x %x", inbuflen
, Ceor
, Csum
, Crot
);
1553 return (str_totext(buf
, target
));
1558 default_fromtext_callback(dns_rdatacallbacks_t
*callbacks
, const char *fmt
,
1566 vfprintf(stderr
, fmt
, ap
);
1568 fprintf(stderr
, "\n");
1572 fromtext_warneof(isc_lex_t
*lexer
, dns_rdatacallbacks_t
*callbacks
) {
1573 if (isc_lex_isfile(lexer
) && callbacks
!= NULL
) {
1574 const char *name
= isc_lex_getsourcename(lexer
);
1577 (*callbacks
->warn
)(callbacks
,
1578 "%s:%lu: file does not end with newline",
1579 name
, isc_lex_getsourceline(lexer
));
1584 warn_badname(dns_name_t
*name
, isc_lex_t
*lexer
,
1585 dns_rdatacallbacks_t
*callbacks
)
1589 char namebuf
[DNS_NAME_FORMATSIZE
];
1591 if (lexer
!= NULL
) {
1592 file
= isc_lex_getsourcename(lexer
);
1593 line
= isc_lex_getsourceline(lexer
);
1594 dns_name_format(name
, namebuf
, sizeof(namebuf
));
1595 (*callbacks
->warn
)(callbacks
, "%s:%u: warning: %s: %s",
1596 file
, line
, namebuf
,
1597 dns_result_totext(DNS_R_BADNAME
));
1602 fromtext_error(void (*callback
)(dns_rdatacallbacks_t
*, const char *, ...),
1603 dns_rdatacallbacks_t
*callbacks
, const char *name
,
1604 unsigned long line
, isc_token_t
*token
, isc_result_t result
)
1609 if (token
!= NULL
) {
1610 switch (token
->type
) {
1611 case isc_tokentype_eol
:
1612 (*callback
)(callbacks
, "%s: %s:%lu: near eol: %s",
1613 "dns_rdata_fromtext", name
, line
,
1614 dns_result_totext(result
));
1616 case isc_tokentype_eof
:
1617 (*callback
)(callbacks
, "%s: %s:%lu: near eof: %s",
1618 "dns_rdata_fromtext", name
, line
,
1619 dns_result_totext(result
));
1621 case isc_tokentype_number
:
1622 (*callback
)(callbacks
, "%s: %s:%lu: near %lu: %s",
1623 "dns_rdata_fromtext", name
, line
,
1624 token
->value
.as_ulong
,
1625 dns_result_totext(result
));
1627 case isc_tokentype_string
:
1628 case isc_tokentype_qstring
:
1629 (*callback
)(callbacks
, "%s: %s:%lu: near '%s': %s",
1630 "dns_rdata_fromtext", name
, line
,
1632 dns_result_totext(result
));
1635 (*callback
)(callbacks
, "%s: %s:%lu: %s",
1636 "dns_rdata_fromtext", name
, line
,
1637 dns_result_totext(result
));
1641 (*callback
)(callbacks
, "dns_rdata_fromtext: %s:%lu: %s",
1642 name
, line
, dns_result_totext(result
));
1647 dns_rdata_covers(dns_rdata_t
*rdata
) {
1648 if (rdata
->type
== 46)
1649 return (covers_rrsig(rdata
));
1650 return (covers_sig(rdata
));
1654 dns_rdatatype_ismeta(dns_rdatatype_t type
) {
1655 if ((dns_rdatatype_attributes(type
) & DNS_RDATATYPEATTR_META
) != 0)
1661 dns_rdatatype_issingleton(dns_rdatatype_t type
) {
1662 if ((dns_rdatatype_attributes(type
) & DNS_RDATATYPEATTR_SINGLETON
)
1669 dns_rdatatype_notquestion(dns_rdatatype_t type
) {
1670 if ((dns_rdatatype_attributes(type
) & DNS_RDATATYPEATTR_NOTQUESTION
)
1677 dns_rdatatype_questiononly(dns_rdatatype_t type
) {
1678 if ((dns_rdatatype_attributes(type
) & DNS_RDATATYPEATTR_QUESTIONONLY
)
1685 dns_rdatatype_atparent(dns_rdatatype_t type
) {
1686 if ((dns_rdatatype_attributes(type
) & DNS_RDATATYPEATTR_ATPARENT
) != 0)
1692 dns_rdataclass_ismeta(dns_rdataclass_t rdclass
) {
1694 if (rdclass
== dns_rdataclass_reserved0
1695 || rdclass
== dns_rdataclass_none
1696 || rdclass
== dns_rdataclass_any
)
1699 return (ISC_FALSE
); /* Assume it is not a meta class. */
1703 dns_rdatatype_isdnssec(dns_rdatatype_t type
) {
1704 if ((dns_rdatatype_attributes(type
) & DNS_RDATATYPEATTR_DNSSEC
) != 0)
1710 dns_rdatatype_iszonecutauth(dns_rdatatype_t type
) {
1711 if ((dns_rdatatype_attributes(type
)
1712 & (DNS_RDATATYPEATTR_DNSSEC
| DNS_RDATATYPEATTR_ZONECUTAUTH
))
1719 dns_rdatatype_isknown(dns_rdatatype_t type
) {
1720 if ((dns_rdatatype_attributes(type
) & DNS_RDATATYPEATTR_UNKNOWN
)