2 * Copyright (C) 2004-2009, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or 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.
25 #include <isc/base64.h>
29 #include <isc/parseint.h>
30 #include <isc/print.h>
31 #include <isc/string.h>
32 #include <isc/stdlib.h>
35 #include <dns/callbacks.h>
37 #include <dns/compress.h>
38 #include <dns/enumtype.h>
39 #include <dns/keyflags.h>
40 #include <dns/keyvalues.h>
41 #include <dns/rcode.h>
42 #include <dns/rdata.h>
43 #include <dns/rdataclass.h>
44 #include <dns/rdatastruct.h>
45 #include <dns/rdatatype.h>
46 #include <dns/result.h>
47 #include <dns/secalg.h>
48 #include <dns/secproto.h>
54 isc_result_t _r = (x); \
55 if (_r != ISC_R_SUCCESS) \
61 isc_result_t _r = (x); \
62 if (_r != ISC_R_SUCCESS) { \
63 isc_lex_ungettoken(lexer, &token); \
68 #define DNS_AS_STR(t) ((t).value.as_textregion.base)
70 #define ARGS_FROMTEXT int rdclass, dns_rdatatype_t type, \
71 isc_lex_t *lexer, dns_name_t *origin, \
72 unsigned int options, isc_buffer_t *target, \
73 dns_rdatacallbacks_t *callbacks
75 #define ARGS_TOTEXT dns_rdata_t *rdata, dns_rdata_textctx_t *tctx, \
78 #define ARGS_FROMWIRE int rdclass, dns_rdatatype_t type, \
79 isc_buffer_t *source, dns_decompress_t *dctx, \
80 unsigned int options, isc_buffer_t *target
82 #define ARGS_TOWIRE dns_rdata_t *rdata, dns_compress_t *cctx, \
85 #define ARGS_COMPARE const dns_rdata_t *rdata1, const dns_rdata_t *rdata2
87 #define ARGS_FROMSTRUCT int rdclass, dns_rdatatype_t type, \
88 void *source, isc_buffer_t *target
90 #define ARGS_TOSTRUCT dns_rdata_t *rdata, void *target, isc_mem_t *mctx
92 #define ARGS_FREESTRUCT void *source
94 #define ARGS_ADDLDATA dns_rdata_t *rdata, dns_additionaldatafunc_t add, \
97 #define ARGS_DIGEST dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg
99 #define ARGS_CHECKOWNER dns_name_t *name, dns_rdataclass_t rdclass, \
100 dns_rdatatype_t type, isc_boolean_t wildcard
102 #define ARGS_CHECKNAMES dns_rdata_t *rdata, dns_name_t *owner, dns_name_t *bad
106 * Context structure for the totext_ functions.
107 * Contains formatting options for rdata-to-text
110 typedef struct dns_rdata_textctx {
111 dns_name_t *origin; /*%< Current origin, or NULL. */
112 unsigned int flags; /*%< DNS_STYLEFLAG_* */
113 unsigned int width; /*%< Width of rdata column. */
114 const char *linebreak; /*%< Line break string. */
115 } dns_rdata_textctx_t;
118 txt_totext(isc_region_t *source, isc_buffer_t *target);
121 txt_fromtext(isc_textregion_t *source, isc_buffer_t *target);
124 txt_fromwire(isc_buffer_t *source, isc_buffer_t *target);
127 name_prefix(dns_name_t *name, dns_name_t *origin, dns_name_t *target);
130 name_length(dns_name_t *name);
133 str_totext(const char *source, isc_buffer_t *target);
136 inet_totext(int af, isc_region_t *src, isc_buffer_t *target);
139 buffer_empty(isc_buffer_t *source);
142 buffer_fromregion(isc_buffer_t *buffer, isc_region_t *region);
145 uint32_tobuffer(isc_uint32_t, isc_buffer_t *target);
148 uint16_tobuffer(isc_uint32_t, isc_buffer_t *target);
151 uint8_tobuffer(isc_uint32_t, isc_buffer_t *target);
154 name_tobuffer(dns_name_t *name, isc_buffer_t *target);
157 uint32_fromregion(isc_region_t *region);
160 uint16_fromregion(isc_region_t *region);
163 uint8_fromregion(isc_region_t *region);
166 uint8_consume_fromregion(isc_region_t *region);
169 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);
172 hexvalue(char value);
175 decvalue(char value);
178 btoa_totext(unsigned char *inbuf, int inbuflen, isc_buffer_t *target);
181 atob_tobuffer(isc_lex_t *lexer, isc_buffer_t *target);
184 default_fromtext_callback(dns_rdatacallbacks_t *callbacks, const char *, ...)
185 ISC_FORMAT_PRINTF(2, 3);
188 fromtext_error(void (*callback)(dns_rdatacallbacks_t *, const char *, ...),
189 dns_rdatacallbacks_t *callbacks, const char *name,
190 unsigned long line, isc_token_t *token, isc_result_t result);
193 fromtext_warneof(isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks);
196 rdata_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
197 isc_buffer_t *target);
200 warn_badname(dns_name_t *name, isc_lex_t *lexer,
201 dns_rdatacallbacks_t *callbacks);
204 warn_badmx(isc_token_t *token, isc_lex_t *lexer,
205 dns_rdatacallbacks_t *callbacks);
208 uint16_consume_fromregion(isc_region_t *region);
211 unknown_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
212 isc_buffer_t *target);
215 getquad(const void *src, struct in_addr *dst,
216 isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks)
221 result = inet_aton(src, dst);
222 if (result == 1 && callbacks != NULL &&
223 inet_pton(AF_INET, src, &tmp) != 1) {
224 const char *name = isc_lex_getsourcename(lexer);
227 (*callbacks->warn)(callbacks, "%s:%lu: \"%s\" "
228 "is not a decimal dotted quad", name,
229 isc_lex_getsourceline(lexer), src);
234 static inline isc_result_t
235 name_duporclone(dns_name_t *source, isc_mem_t *mctx, dns_name_t *target) {
238 return (dns_name_dup(source, mctx, target));
239 dns_name_clone(source, target);
240 return (ISC_R_SUCCESS);
244 mem_maybedup(isc_mem_t *mctx, void *source, size_t length) {
249 new = isc_mem_allocate(mctx, length);
251 memcpy(new, source, length);
256 static const char hexdigits[] = "0123456789abcdef";
257 static const char decdigits[] = "0123456789";
262 #define RESERVED 0x0002
269 dns_rdata_init(dns_rdata_t *rdata) {
271 REQUIRE(rdata != NULL);
278 ISC_LINK_INIT(rdata, link);
279 /* ISC_LIST_INIT(rdata->list); */
283 #define DNS_RDATA_INITIALIZED(rdata) \
284 ((rdata)->data == NULL && (rdata)->length == 0 && \
285 (rdata)->rdclass == 0 && (rdata)->type == 0 && (rdata)->flags == 0 && \
286 !ISC_LINK_LINKED((rdata), link))
288 #ifdef ISC_LIST_CHECKINIT
289 #define DNS_RDATA_INITIALIZED(rdata) \
290 (!ISC_LINK_LINKED((rdata), link))
292 #define DNS_RDATA_INITIALIZED(rdata) ISC_TRUE
296 #define DNS_RDATA_VALIDFLAGS(rdata) \
297 (((rdata)->flags & ~(DNS_RDATA_UPDATE|DNS_RDATA_OFFLINE)) == 0)
300 dns_rdata_reset(dns_rdata_t *rdata) {
302 REQUIRE(rdata != NULL);
304 REQUIRE(!ISC_LINK_LINKED(rdata, link));
305 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
319 dns_rdata_clone(const dns_rdata_t *src, dns_rdata_t *target) {
321 REQUIRE(src != NULL);
322 REQUIRE(target != NULL);
324 REQUIRE(DNS_RDATA_INITIALIZED(target));
326 REQUIRE(DNS_RDATA_VALIDFLAGS(src));
327 REQUIRE(DNS_RDATA_VALIDFLAGS(target));
329 target->data = src->data;
330 target->length = src->length;
331 target->rdclass = src->rdclass;
332 target->type = src->type;
333 target->flags = src->flags;
342 dns_rdata_compare(const dns_rdata_t *rdata1, const dns_rdata_t *rdata2) {
344 isc_boolean_t use_default = ISC_FALSE;
346 REQUIRE(rdata1 != NULL);
347 REQUIRE(rdata2 != NULL);
348 REQUIRE(rdata1->length == 0 || rdata1->data != NULL);
349 REQUIRE(rdata2->length == 0 || rdata2->data != NULL);
350 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata1));
351 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata2));
353 if (rdata1->rdclass != rdata2->rdclass)
354 return (rdata1->rdclass < rdata2->rdclass ? -1 : 1);
356 if (rdata1->type != rdata2->type)
357 return (rdata1->type < rdata2->type ? -1 : 1);
365 dns_rdata_toregion(rdata1, &r1);
366 dns_rdata_toregion(rdata2, &r2);
367 result = isc_region_compare(&r1, &r2);
377 dns_rdata_fromregion(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
378 dns_rdatatype_t type, isc_region_t *r)
381 REQUIRE(rdata != NULL);
382 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
385 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
387 rdata->data = r->base;
388 rdata->length = r->length;
389 rdata->rdclass = rdclass;
395 dns_rdata_toregion(const dns_rdata_t *rdata, isc_region_t *r) {
397 REQUIRE(rdata != NULL);
399 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
401 r->base = rdata->data;
402 r->length = rdata->length;
406 dns_rdata_fromwire(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
407 dns_rdatatype_t type, isc_buffer_t *source,
408 dns_decompress_t *dctx, unsigned int options,
409 isc_buffer_t *target)
411 isc_result_t result = ISC_R_NOTIMPLEMENTED;
415 isc_boolean_t use_default = ISC_FALSE;
416 isc_uint32_t activelength;
418 REQUIRE(dctx != NULL);
420 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
421 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
425 return (DNS_R_FORMERR);
430 activelength = isc_buffer_activelength(source);
431 INSIST(activelength < 65536);
436 if (activelength > isc_buffer_availablelength(target))
437 result = ISC_R_NOSPACE;
439 isc_buffer_putmem(target, isc_buffer_current(source),
441 isc_buffer_forward(source, activelength);
442 result = ISC_R_SUCCESS;
447 * We should have consumed all of our buffer.
449 if (result == ISC_R_SUCCESS && !buffer_empty(source))
450 result = DNS_R_EXTRADATA;
452 if (rdata != NULL && result == ISC_R_SUCCESS) {
453 region.base = isc_buffer_used(&st);
454 region.length = isc_buffer_usedlength(target) -
455 isc_buffer_usedlength(&st);
456 dns_rdata_fromregion(rdata, rdclass, type, ®ion);
459 if (result != ISC_R_SUCCESS) {
467 dns_rdata_towire(dns_rdata_t *rdata, dns_compress_t *cctx,
468 isc_buffer_t *target)
470 isc_result_t result = ISC_R_NOTIMPLEMENTED;
471 isc_boolean_t use_default = ISC_FALSE;
475 REQUIRE(rdata != NULL);
476 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
479 * Some DynDNS meta-RRs have empty rdata.
481 if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
482 INSIST(rdata->length == 0);
483 return (ISC_R_SUCCESS);
491 isc_buffer_availableregion(target, &tr);
492 if (tr.length < rdata->length)
493 return (ISC_R_NOSPACE);
494 memcpy(tr.base, rdata->data, rdata->length);
495 isc_buffer_add(target, rdata->length);
496 return (ISC_R_SUCCESS);
498 if (result != ISC_R_SUCCESS) {
500 INSIST(target->used < 65536);
501 dns_compress_rollback(cctx, (isc_uint16_t)target->used);
507 * If the binary data in 'src' is valid uncompressed wire format
508 * rdata of class 'rdclass' and type 'type', return ISC_R_SUCCESS
509 * and copy the validated rdata to 'dest'. Otherwise return an error.
512 rdata_validate(isc_buffer_t *src, isc_buffer_t *dest, dns_rdataclass_t rdclass,
513 dns_rdatatype_t type)
515 dns_decompress_t dctx;
516 dns_rdata_t rdata = DNS_RDATA_INIT;
519 dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_NONE);
520 isc_buffer_setactive(src, isc_buffer_usedlength(src));
521 result = dns_rdata_fromwire(&rdata, rdclass, type, src,
523 dns_decompress_invalidate(&dctx);
529 unknown_fromtext(dns_rdataclass_t rdclass, dns_rdatatype_t type,
530 isc_lex_t *lexer, isc_mem_t *mctx, isc_buffer_t *target)
533 isc_buffer_t *buf = NULL;
536 if (type == 0 || dns_rdatatype_ismeta(type))
537 return (DNS_R_METATYPE);
539 result = isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
541 if (result == ISC_R_SUCCESS && token.value.as_ulong > 65535U)
542 return (ISC_R_RANGE);
543 result = isc_buffer_allocate(mctx, &buf, token.value.as_ulong);
544 if (result != ISC_R_SUCCESS)
547 result = isc_hex_tobuffer(lexer, buf,
548 (unsigned int)token.value.as_ulong);
549 if (result != ISC_R_SUCCESS)
551 if (isc_buffer_usedlength(buf) != token.value.as_ulong) {
552 result = ISC_R_UNEXPECTEDEND;
556 if (dns_rdatatype_isknown(type)) {
557 result = rdata_validate(buf, target, rdclass, type);
560 isc_buffer_usedregion(buf, &r);
561 result = isc_buffer_copyregion(target, &r);
563 if (result != ISC_R_SUCCESS)
566 isc_buffer_free(&buf);
567 return (ISC_R_SUCCESS);
570 isc_buffer_free(&buf);
575 dns_rdata_fromtext(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
576 dns_rdatatype_t type, isc_lex_t *lexer,
577 dns_name_t *origin, unsigned int options, isc_mem_t *mctx,
578 isc_buffer_t *target, dns_rdatacallbacks_t *callbacks)
580 isc_result_t result = ISC_R_NOTIMPLEMENTED;
584 unsigned int lexoptions = ISC_LEXOPT_EOL | ISC_LEXOPT_EOF |
585 ISC_LEXOPT_DNSMULTILINE | ISC_LEXOPT_ESCAPE;
588 void (*callback)(dns_rdatacallbacks_t *, const char *, ...);
589 isc_result_t tresult;
591 REQUIRE(origin == NULL || dns_name_isabsolute(origin) == ISC_TRUE);
593 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
594 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
596 if (callbacks != NULL) {
597 REQUIRE(callbacks->warn != NULL);
598 REQUIRE(callbacks->error != NULL);
603 if (callbacks != NULL)
604 callback = callbacks->error;
606 callback = default_fromtext_callback;
608 result = isc_lex_getmastertoken(lexer, &token, isc_tokentype_qstring,
610 if (result != ISC_R_SUCCESS) {
611 name = isc_lex_getsourcename(lexer);
612 line = isc_lex_getsourceline(lexer);
613 fromtext_error(callback, callbacks, name, line, NULL, result);
617 if (strcmp(DNS_AS_STR(token), "\\#") == 0)
618 result = unknown_fromtext(rdclass, type, lexer, mctx, target);
620 isc_lex_ungettoken(lexer, &token);
626 * Consume to end of line / file.
627 * If not at end of line initially set error code.
628 * Call callback via fromtext_error once if there was an error.
631 name = isc_lex_getsourcename(lexer);
632 line = isc_lex_getsourceline(lexer);
633 tresult = isc_lex_gettoken(lexer, lexoptions, &token);
634 if (tresult != ISC_R_SUCCESS) {
635 if (result == ISC_R_SUCCESS)
637 if (callback != NULL)
638 fromtext_error(callback, callbacks, name,
641 } else if (token.type != isc_tokentype_eol &&
642 token.type != isc_tokentype_eof) {
643 if (result == ISC_R_SUCCESS)
644 result = DNS_R_EXTRATOKEN;
645 if (callback != NULL) {
646 fromtext_error(callback, callbacks, name,
647 line, &token, result);
650 } else if (result != ISC_R_SUCCESS && callback != NULL) {
651 fromtext_error(callback, callbacks, name, line,
655 if (token.type == isc_tokentype_eof)
656 fromtext_warneof(lexer, callbacks);
661 if (rdata != NULL && result == ISC_R_SUCCESS) {
662 region.base = isc_buffer_used(&st);
663 region.length = isc_buffer_usedlength(target) -
664 isc_buffer_usedlength(&st);
665 dns_rdata_fromregion(rdata, rdclass, type, ®ion);
667 if (result != ISC_R_SUCCESS) {
674 unknown_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
675 isc_buffer_t *target)
678 char buf[sizeof("65535")];
681 strlcpy(buf, "\\# ", sizeof(buf));
682 result = str_totext(buf, target);
683 if (result != ISC_R_SUCCESS)
686 dns_rdata_toregion(rdata, &sr);
687 INSIST(sr.length < 65536);
688 snprintf(buf, sizeof(buf), "%u", sr.length);
689 result = str_totext(buf, target);
690 if (result != ISC_R_SUCCESS)
693 if (sr.length != 0U) {
694 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
695 result = str_totext(" ( ", target);
697 result = str_totext(" ", target);
699 if (result != ISC_R_SUCCESS)
702 if (tctx->width == 0) /* No splitting */
703 result = isc_hex_totext(&sr, 0, "", target);
705 result = isc_hex_totext(&sr, tctx->width - 2,
708 if (result == ISC_R_SUCCESS &&
709 (tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
710 result = str_totext(" )", target);
716 rdata_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
717 isc_buffer_t *target)
719 isc_result_t result = ISC_R_NOTIMPLEMENTED;
720 isc_boolean_t use_default = ISC_FALSE;
722 REQUIRE(rdata != NULL);
723 REQUIRE(tctx->origin == NULL ||
724 dns_name_isabsolute(tctx->origin) == ISC_TRUE);
727 * Some DynDNS meta-RRs have empty rdata.
729 if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
730 INSIST(rdata->length == 0);
731 return (ISC_R_SUCCESS);
737 result = unknown_totext(rdata, tctx, target);
743 dns_rdata_totext(dns_rdata_t *rdata, dns_name_t *origin, isc_buffer_t *target)
745 dns_rdata_textctx_t tctx;
747 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
750 * Set up formatting options for single-line output.
752 tctx.origin = origin;
755 tctx.linebreak = " ";
756 return (rdata_totext(rdata, &tctx, target));
760 dns_rdata_tofmttext(dns_rdata_t *rdata, dns_name_t *origin,
761 unsigned int flags, unsigned int width,
762 const char *linebreak, isc_buffer_t *target)
764 dns_rdata_textctx_t tctx;
766 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
769 * Set up formatting options for formatted output.
771 tctx.origin = origin;
773 if ((flags & DNS_STYLEFLAG_MULTILINE) != 0) {
775 tctx.linebreak = linebreak;
777 tctx.width = 60; /* Used for hex word length only. */
778 tctx.linebreak = " ";
780 return (rdata_totext(rdata, &tctx, target));
784 dns_rdata_fromstruct(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
785 dns_rdatatype_t type, void *source,
786 isc_buffer_t *target)
788 isc_result_t result = ISC_R_NOTIMPLEMENTED;
791 isc_boolean_t use_default = ISC_FALSE;
793 REQUIRE(source != NULL);
795 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
796 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
806 if (rdata != NULL && result == ISC_R_SUCCESS) {
807 region.base = isc_buffer_used(&st);
808 region.length = isc_buffer_usedlength(target) -
809 isc_buffer_usedlength(&st);
810 dns_rdata_fromregion(rdata, rdclass, type, ®ion);
812 if (result != ISC_R_SUCCESS)
818 dns_rdata_tostruct(dns_rdata_t *rdata, void *target, isc_mem_t *mctx) {
819 isc_result_t result = ISC_R_NOTIMPLEMENTED;
820 isc_boolean_t use_default = ISC_FALSE;
822 REQUIRE(rdata != NULL);
823 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
834 dns_rdata_freestruct(void *source) {
835 dns_rdatacommon_t *common = source;
836 REQUIRE(source != NULL);
842 dns_rdata_additionaldata(dns_rdata_t *rdata, dns_additionaldatafunc_t add,
845 isc_result_t result = ISC_R_NOTIMPLEMENTED;
846 isc_boolean_t use_default = ISC_FALSE;
849 * Call 'add' for each name and type from 'rdata' which is subject to
850 * additional section processing.
853 REQUIRE(rdata != NULL);
854 REQUIRE(add != NULL);
855 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
859 /* No additional processing for unknown types */
861 result = ISC_R_SUCCESS;
867 dns_rdata_digest(dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg) {
868 isc_result_t result = ISC_R_NOTIMPLEMENTED;
869 isc_boolean_t use_default = ISC_FALSE;
873 * Send 'rdata' in DNSSEC canonical form to 'digest'.
876 REQUIRE(rdata != NULL);
877 REQUIRE(digest != NULL);
878 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
883 dns_rdata_toregion(rdata, &r);
884 result = (digest)(arg, &r);
891 dns_rdata_checkowner(dns_name_t *name, dns_rdataclass_t rdclass,
892 dns_rdatatype_t type, isc_boolean_t wildcard)
894 isc_boolean_t result;
901 dns_rdata_checknames(dns_rdata_t *rdata, dns_name_t *owner, dns_name_t *bad)
903 isc_boolean_t result;
910 dns_rdatatype_attributes(dns_rdatatype_t type)
912 RDATATYPE_ATTRIBUTE_SW
913 if (type >= (dns_rdatatype_t)128 && type < (dns_rdatatype_t)255)
914 return (DNS_RDATATYPEATTR_UNKNOWN | DNS_RDATATYPEATTR_META);
915 return (DNS_RDATATYPEATTR_UNKNOWN);
919 dns_rdatatype_fromtext(dns_rdatatype_t *typep, isc_textregion_t *source) {
927 return (DNS_R_UNKNOWN);
929 a = tolower((unsigned char)source->base[0]);
930 b = tolower((unsigned char)source->base[n - 1]);
932 hash = ((a + n) * b) % 256;
935 * This switch block is inlined via \#define, and will use "return"
936 * to return a result to the caller if it is a valid (known)
939 RDATATYPE_FROMTEXT_SW(hash, source->base, n, typep);
941 if (source->length > 4 && source->length < (4 + sizeof("65000")) &&
942 strncasecmp("type", source->base, 4) == 0) {
943 char buf[sizeof("65000")];
947 strncpy(buf, source->base + 4, source->length - 4);
948 buf[source->length - 4] = '\0';
949 val = strtoul(buf, &endp, 10);
950 if (*endp == '\0' && val <= 0xffff) {
951 *typep = (dns_rdatatype_t)val;
952 return (ISC_R_SUCCESS);
956 return (DNS_R_UNKNOWN);
960 dns_rdatatype_totext(dns_rdatatype_t type, isc_buffer_t *target) {
961 char buf[sizeof("TYPE65535")];
964 snprintf(buf, sizeof(buf), "TYPE%u", type);
965 return (str_totext(buf, target));
969 dns_rdatatype_format(dns_rdatatype_t rdtype,
970 char *array, unsigned int size)
978 isc_buffer_init(&buf, array, size);
979 result = dns_rdatatype_totext(rdtype, &buf);
983 if (result == ISC_R_SUCCESS) {
984 if (isc_buffer_availablelength(&buf) >= 1)
985 isc_buffer_putuint8(&buf, 0);
987 result = ISC_R_NOSPACE;
989 if (result != ISC_R_SUCCESS)
990 strlcpy(array, "<unknown>", size);
998 name_length(dns_name_t *name) {
999 return (name->length);
1003 txt_totext(isc_region_t *source, isc_buffer_t *target) {
1008 isc_region_t region;
1010 isc_buffer_availableregion(target, ®ion);
1012 tp = (char *)region.base;
1017 REQUIRE(n + 1 <= source->length);
1020 return (ISC_R_NOSPACE);
1024 if (*sp < 0x20 || *sp >= 0x7f) {
1026 return (ISC_R_NOSPACE);
1028 *tp++ = 0x30 + ((*sp / 100) % 10);
1029 *tp++ = 0x30 + ((*sp / 10) % 10);
1030 *tp++ = 0x30 + (*sp % 10);
1035 /* double quote, semi-colon, backslash */
1036 if (*sp == 0x22 || *sp == 0x3b || *sp == 0x5c) {
1038 return (ISC_R_NOSPACE);
1043 return (ISC_R_NOSPACE);
1048 return (ISC_R_NOSPACE);
1051 isc_buffer_add(target, tp - (char *)region.base);
1052 isc_region_consume(source, *source->base + 1);
1053 return (ISC_R_SUCCESS);
1057 txt_fromtext(isc_textregion_t *source, isc_buffer_t *target) {
1058 isc_region_t tregion;
1059 isc_boolean_t escape;
1060 unsigned int n, nrem;
1066 isc_buffer_availableregion(target, &tregion);
1070 nrem = tregion.length;
1073 return (ISC_R_NOSPACE);
1080 * Maximum text string length.
1086 if (escape && (d = decvalue((char)c)) != -1) {
1089 return (DNS_R_SYNTAX);
1091 if ((d = decvalue(*s++)) != -1)
1094 return (DNS_R_SYNTAX);
1096 return (DNS_R_SYNTAX);
1098 if ((d = decvalue(*s++)) != -1)
1101 return (DNS_R_SYNTAX);
1103 return (DNS_R_SYNTAX);
1104 } else if (!escape && c == '\\') {
1110 return ((tregion.length <= 256U) ?
1111 ISC_R_NOSPACE : DNS_R_SYNTAX);
1116 return (DNS_R_SYNTAX);
1117 *tregion.base = t - tregion.base - 1;
1118 isc_buffer_add(target, *tregion.base + 1);
1119 return (ISC_R_SUCCESS);
1123 txt_fromwire(isc_buffer_t *source, isc_buffer_t *target) {
1125 isc_region_t sregion;
1126 isc_region_t tregion;
1128 isc_buffer_activeregion(source, &sregion);
1129 if (sregion.length == 0)
1130 return(ISC_R_UNEXPECTEDEND);
1131 n = *sregion.base + 1;
1132 if (n > sregion.length)
1133 return (ISC_R_UNEXPECTEDEND);
1135 isc_buffer_availableregion(target, &tregion);
1136 if (n > tregion.length)
1137 return (ISC_R_NOSPACE);
1139 memcpy(tregion.base, sregion.base, n);
1140 isc_buffer_forward(source, n);
1141 isc_buffer_add(target, n);
1142 return (ISC_R_SUCCESS);
1145 static isc_boolean_t
1146 name_prefix(dns_name_t *name, dns_name_t *origin, dns_name_t *target) {
1152 if (dns_name_compare(origin, dns_rootname) == 0)
1155 if (!dns_name_issubdomain(name, origin))
1158 l1 = dns_name_countlabels(name);
1159 l2 = dns_name_countlabels(origin);
1164 /* Master files should be case preserving. */
1165 dns_name_getlabelsequence(name, l1 - l2, l2, target);
1166 if (!dns_name_caseequal(origin, target))
1169 dns_name_getlabelsequence(name, 0, l1 - l2, target);
1178 str_totext(const char *source, isc_buffer_t *target) {
1180 isc_region_t region;
1182 isc_buffer_availableregion(target, ®ion);
1185 if (l > region.length)
1186 return (ISC_R_NOSPACE);
1188 memcpy(region.base, source, l);
1189 isc_buffer_add(target, l);
1190 return (ISC_R_SUCCESS);
1194 inet_totext(int af, isc_region_t *src, isc_buffer_t *target) {
1197 /* Note - inet_ntop doesn't do size checking on its input. */
1198 if (inet_ntop(af, src->base, tmpbuf, sizeof(tmpbuf)) == NULL)
1199 return (ISC_R_NOSPACE);
1200 if (strlen(tmpbuf) > isc_buffer_availablelength(target))
1201 return (ISC_R_NOSPACE);
1202 isc_buffer_putstr(target, tmpbuf);
1203 return (ISC_R_SUCCESS);
1206 static isc_boolean_t
1207 buffer_empty(isc_buffer_t *source) {
1208 return((source->current == source->active) ? ISC_TRUE : ISC_FALSE);
1212 buffer_fromregion(isc_buffer_t *buffer, isc_region_t *region) {
1213 isc_buffer_init(buffer, region->base, region->length);
1214 isc_buffer_add(buffer, region->length);
1215 isc_buffer_setactive(buffer, region->length);
1219 uint32_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1220 isc_region_t region;
1222 isc_buffer_availableregion(target, ®ion);
1223 if (region.length < 4)
1224 return (ISC_R_NOSPACE);
1225 isc_buffer_putuint32(target, value);
1226 return (ISC_R_SUCCESS);
1230 uint16_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1231 isc_region_t region;
1234 return (ISC_R_RANGE);
1235 isc_buffer_availableregion(target, ®ion);
1236 if (region.length < 2)
1237 return (ISC_R_NOSPACE);
1238 isc_buffer_putuint16(target, (isc_uint16_t)value);
1239 return (ISC_R_SUCCESS);
1243 uint8_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1244 isc_region_t region;
1247 return (ISC_R_RANGE);
1248 isc_buffer_availableregion(target, ®ion);
1249 if (region.length < 1)
1250 return (ISC_R_NOSPACE);
1251 isc_buffer_putuint8(target, (isc_uint8_t)value);
1252 return (ISC_R_SUCCESS);
1256 name_tobuffer(dns_name_t *name, isc_buffer_t *target) {
1258 dns_name_toregion(name, &r);
1259 return (isc_buffer_copyregion(target, &r));
1263 uint32_fromregion(isc_region_t *region) {
1266 REQUIRE(region->length >= 4);
1267 value = region->base[0] << 24;
1268 value |= region->base[1] << 16;
1269 value |= region->base[2] << 8;
1270 value |= region->base[3];
1275 uint16_consume_fromregion(isc_region_t *region) {
1276 isc_uint16_t r = uint16_fromregion(region);
1278 isc_region_consume(region, 2);
1283 uint16_fromregion(isc_region_t *region) {
1285 REQUIRE(region->length >= 2);
1287 return ((region->base[0] << 8) | region->base[1]);
1291 uint8_fromregion(isc_region_t *region) {
1293 REQUIRE(region->length >= 1);
1295 return (region->base[0]);
1299 uint8_consume_fromregion(isc_region_t *region) {
1300 isc_uint8_t r = uint8_fromregion(region);
1302 isc_region_consume(region, 1);
1307 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
1310 isc_buffer_availableregion(target, &tr);
1311 if (length > tr.length)
1312 return (ISC_R_NOSPACE);
1313 memcpy(tr.base, base, length);
1314 isc_buffer_add(target, length);
1315 return (ISC_R_SUCCESS);
1319 hexvalue(char value) {
1323 c = (unsigned char)value;
1329 if ((s = strchr(hexdigits, c)) == NULL)
1331 return (s - hexdigits);
1335 decvalue(char value) {
1339 * isascii() is valid for full range of int values, no need to
1342 if (!isascii(value))
1344 if ((s = strchr(decdigits, value)) == NULL)
1346 return (s - decdigits);
1349 static const char atob_digits[86] =
1350 "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`" \
1351 "abcdefghijklmnopqrstu";
1353 * Subroutines to convert between 8 bit binary bytes and printable ASCII.
1354 * Computes the number of bytes, and three kinds of simple checksums.
1355 * Incoming bytes are collected into 32-bit words, then printed in base 85:
1356 * exp(85,5) > exp(2,32)
1357 * The ASCII characters used are between '!' and 'u';
1358 * 'z' encodes 32-bit zero; 'x' is used to mark the end of encoded data.
1360 * Originally by Paul Rutter (philabs!per) and Joe Orost (petsd!joe) for
1361 * the atob/btoa programs, released with the compress program, in mod.sources.
1362 * Modified by Mike Schwartz 8/19/86 for use in BIND.
1363 * Modified to be re-entrant 3/2/99.
1375 #define Ceor state->Ceor
1376 #define Csum state->Csum
1377 #define Crot state->Crot
1378 #define word state->word
1379 #define bcount state->bcount
1381 #define times85(x) ((((((x<<2)+x)<<2)+x)<<2)+x)
1383 static isc_result_t byte_atob(int c, isc_buffer_t *target,
1384 struct state *state);
1385 static isc_result_t putbyte(int c, isc_buffer_t *, struct state *state);
1386 static isc_result_t byte_btoa(int c, isc_buffer_t *, struct state *state);
1389 * Decode ASCII-encoded byte c into binary representation and
1390 * place into *bufp, advancing bufp.
1393 byte_atob(int c, isc_buffer_t *target, struct state *state) {
1397 return(DNS_R_SYNTAX);
1399 RETERR(putbyte(0, target, state));
1400 RETERR(putbyte(0, target, state));
1401 RETERR(putbyte(0, target, state));
1402 RETERR(putbyte(0, target, state));
1404 } else if ((s = strchr(atob_digits, c)) != NULL) {
1406 word = s - atob_digits;
1408 } else if (bcount < 4) {
1409 word = times85(word);
1410 word += s - atob_digits;
1413 word = times85(word);
1414 word += s - atob_digits;
1415 RETERR(putbyte((word >> 24) & 0xff, target, state));
1416 RETERR(putbyte((word >> 16) & 0xff, target, state));
1417 RETERR(putbyte((word >> 8) & 0xff, target, state));
1418 RETERR(putbyte(word & 0xff, target, state));
1423 return(DNS_R_SYNTAX);
1424 return(ISC_R_SUCCESS);
1428 * Compute checksum info and place c into target.
1431 putbyte(int c, isc_buffer_t *target, struct state *state) {
1437 if ((Crot & 0x80000000)) {
1444 isc_buffer_availableregion(target, &tr);
1446 return (ISC_R_NOSPACE);
1448 isc_buffer_add(target, 1);
1449 return (ISC_R_SUCCESS);
1453 * Read the ASCII-encoded data from inbuf, of length inbuflen, and convert
1454 * it into T_UNSPEC (binary data) in outbuf, not to exceed outbuflen bytes;
1455 * outbuflen must be divisible by 4. (Note: this is because outbuf is filled
1456 * in 4 bytes at a time. If the actual data doesn't end on an even 4-byte
1457 * boundary, there will be no problem...it will be padded with 0 bytes, and
1458 * numbytes will indicate the correct number of bytes. The main point is
1459 * that since the buffer is filled in 4 bytes at a time, even if there is
1460 * not a full 4 bytes of data at the end, there has to be room to 0-pad the
1461 * data, so the buffer must be of size divisible by 4). Place the number of
1462 * output bytes in numbytes, and return a failure/success status.
1466 atob_tobuffer(isc_lex_t *lexer, isc_buffer_t *target) {
1467 long oeor, osum, orot;
1468 struct state statebuf, *state= &statebuf;
1473 Ceor = Csum = Crot = word = bcount = 0;
1475 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1477 while (token.value.as_textregion.length != 0) {
1478 if ((c = token.value.as_textregion.base[0]) == 'x') {
1481 RETERR(byte_atob(c, target, state));
1482 isc_textregion_consume(&token.value.as_textregion, 1);
1488 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
1490 if ((token.value.as_ulong % 4) != 0U)
1491 isc_buffer_subtract(target, 4 - (token.value.as_ulong % 4));
1496 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1498 oeor = strtol(DNS_AS_STR(token), &e, 16);
1500 return (DNS_R_SYNTAX);
1505 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1507 osum = strtol(DNS_AS_STR(token), &e, 16);
1509 return (DNS_R_SYNTAX);
1514 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1516 orot = strtol(DNS_AS_STR(token), &e, 16);
1518 return (DNS_R_SYNTAX);
1520 if ((oeor != Ceor) || (osum != Csum) || (orot != Crot))
1521 return(DNS_R_BADCKSUM);
1522 return (ISC_R_SUCCESS);
1526 * Encode binary byte c into ASCII representation and place into *bufp,
1530 byte_btoa(int c, isc_buffer_t *target, struct state *state) {
1533 isc_buffer_availableregion(target, &tr);
1537 if ((Crot & 0x80000000)) {
1550 return (ISC_R_NOSPACE);
1552 isc_buffer_add(target, 1);
1554 register int tmp = 0;
1555 register isc_int32_t tmpword = word;
1559 * Because some don't support u_long.
1562 tmpword -= (isc_int32_t)(85 * 85 * 85 * 85 * 32);
1566 tmpword -= (isc_int32_t)(85 * 85 * 85 * 85 * 32);
1569 return (ISC_R_NOSPACE);
1570 tr.base[0] = atob_digits[(tmpword /
1571 (isc_int32_t)(85 * 85 * 85 * 85))
1573 tmpword %= (isc_int32_t)(85 * 85 * 85 * 85);
1574 tr.base[1] = atob_digits[tmpword / (85 * 85 * 85)];
1575 tmpword %= (85 * 85 * 85);
1576 tr.base[2] = atob_digits[tmpword / (85 * 85)];
1577 tmpword %= (85 * 85);
1578 tr.base[3] = atob_digits[tmpword / 85];
1580 tr.base[4] = atob_digits[tmpword];
1581 isc_buffer_add(target, 5);
1587 return (ISC_R_SUCCESS);
1592 * Encode the binary data from inbuf, of length inbuflen, into a
1593 * target. Return success/failure status
1596 btoa_totext(unsigned char *inbuf, int inbuflen, isc_buffer_t *target) {
1598 struct state statebuf, *state = &statebuf;
1599 char buf[sizeof("x 2000000000 ffffffff ffffffff ffffffff")];
1601 Ceor = Csum = Crot = word = bcount = 0;
1602 for (inc = 0; inc < inbuflen; inbuf++, inc++)
1603 RETERR(byte_btoa(*inbuf, target, state));
1606 RETERR(byte_btoa(0, target, state));
1609 * Put byte count and checksum information at end of buffer,
1612 snprintf(buf, sizeof(buf), "x %d %x %x %x", inbuflen, Ceor, Csum, Crot);
1613 return (str_totext(buf, target));
1618 default_fromtext_callback(dns_rdatacallbacks_t *callbacks, const char *fmt,
1626 vfprintf(stderr, fmt, ap);
1628 fprintf(stderr, "\n");
1632 fromtext_warneof(isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks) {
1633 if (isc_lex_isfile(lexer) && callbacks != NULL) {
1634 const char *name = isc_lex_getsourcename(lexer);
1637 (*callbacks->warn)(callbacks,
1638 "%s:%lu: file does not end with newline",
1639 name, isc_lex_getsourceline(lexer));
1644 warn_badmx(isc_token_t *token, isc_lex_t *lexer,
1645 dns_rdatacallbacks_t *callbacks)
1650 if (lexer != NULL) {
1651 file = isc_lex_getsourcename(lexer);
1652 line = isc_lex_getsourceline(lexer);
1653 (*callbacks->warn)(callbacks, "%s:%u: warning: '%s': %s",
1654 file, line, DNS_AS_STR(*token),
1655 dns_result_totext(DNS_R_MXISADDRESS));
1660 warn_badname(dns_name_t *name, isc_lex_t *lexer,
1661 dns_rdatacallbacks_t *callbacks)
1665 char namebuf[DNS_NAME_FORMATSIZE];
1667 if (lexer != NULL) {
1668 file = isc_lex_getsourcename(lexer);
1669 line = isc_lex_getsourceline(lexer);
1670 dns_name_format(name, namebuf, sizeof(namebuf));
1671 (*callbacks->warn)(callbacks, "%s:%u: warning: %s: %s",
1672 file, line, namebuf,
1673 dns_result_totext(DNS_R_BADNAME));
1678 fromtext_error(void (*callback)(dns_rdatacallbacks_t *, const char *, ...),
1679 dns_rdatacallbacks_t *callbacks, const char *name,
1680 unsigned long line, isc_token_t *token, isc_result_t result)
1685 if (token != NULL) {
1686 switch (token->type) {
1687 case isc_tokentype_eol:
1688 (*callback)(callbacks, "%s: %s:%lu: near eol: %s",
1689 "dns_rdata_fromtext", name, line,
1690 dns_result_totext(result));
1692 case isc_tokentype_eof:
1693 (*callback)(callbacks, "%s: %s:%lu: near eof: %s",
1694 "dns_rdata_fromtext", name, line,
1695 dns_result_totext(result));
1697 case isc_tokentype_number:
1698 (*callback)(callbacks, "%s: %s:%lu: near %lu: %s",
1699 "dns_rdata_fromtext", name, line,
1700 token->value.as_ulong,
1701 dns_result_totext(result));
1703 case isc_tokentype_string:
1704 case isc_tokentype_qstring:
1705 (*callback)(callbacks, "%s: %s:%lu: near '%s': %s",
1706 "dns_rdata_fromtext", name, line,
1708 dns_result_totext(result));
1711 (*callback)(callbacks, "%s: %s:%lu: %s",
1712 "dns_rdata_fromtext", name, line,
1713 dns_result_totext(result));
1717 (*callback)(callbacks, "dns_rdata_fromtext: %s:%lu: %s",
1718 name, line, dns_result_totext(result));
1723 dns_rdata_covers(dns_rdata_t *rdata) {
1724 if (rdata->type == 46)
1725 return (covers_rrsig(rdata));
1726 return (covers_sig(rdata));
1730 dns_rdatatype_ismeta(dns_rdatatype_t type) {
1731 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_META) != 0)
1737 dns_rdatatype_issingleton(dns_rdatatype_t type) {
1738 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_SINGLETON)
1745 dns_rdatatype_notquestion(dns_rdatatype_t type) {
1746 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_NOTQUESTION)
1753 dns_rdatatype_questiononly(dns_rdatatype_t type) {
1754 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_QUESTIONONLY)
1761 dns_rdatatype_atparent(dns_rdatatype_t type) {
1762 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_ATPARENT) != 0)
1768 dns_rdataclass_ismeta(dns_rdataclass_t rdclass) {
1770 if (rdclass == dns_rdataclass_reserved0
1771 || rdclass == dns_rdataclass_none
1772 || rdclass == dns_rdataclass_any)
1775 return (ISC_FALSE); /* Assume it is not a meta class. */
1779 dns_rdatatype_isdnssec(dns_rdatatype_t type) {
1780 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_DNSSEC) != 0)
1786 dns_rdatatype_iszonecutauth(dns_rdatatype_t type) {
1787 if ((dns_rdatatype_attributes(type)
1788 & (DNS_RDATATYPEATTR_DNSSEC | DNS_RDATATYPEATTR_ZONECUTAUTH))
1795 dns_rdatatype_isknown(dns_rdatatype_t type) {
1796 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_UNKNOWN)