2 * Copyright (C) 2004-2009 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.
18 /* $Id: rdata.c,v 1.204 2009/12/04 21:09:33 marka Exp $ */
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/message.h>
42 #include <dns/rcode.h>
43 #include <dns/rdata.h>
44 #include <dns/rdataclass.h>
45 #include <dns/rdatastruct.h>
46 #include <dns/rdatatype.h>
47 #include <dns/result.h>
48 #include <dns/secalg.h>
49 #include <dns/secproto.h>
55 isc_result_t _r = (x); \
56 if (_r != ISC_R_SUCCESS) \
62 isc_result_t _r = (x); \
63 if (_r != ISC_R_SUCCESS) { \
64 isc_lex_ungettoken(lexer, &token); \
69 #define DNS_AS_STR(t) ((t).value.as_textregion.base)
71 #define ARGS_FROMTEXT int rdclass, dns_rdatatype_t type, \
72 isc_lex_t *lexer, dns_name_t *origin, \
73 unsigned int options, isc_buffer_t *target, \
74 dns_rdatacallbacks_t *callbacks
76 #define ARGS_TOTEXT dns_rdata_t *rdata, dns_rdata_textctx_t *tctx, \
79 #define ARGS_FROMWIRE int rdclass, dns_rdatatype_t type, \
80 isc_buffer_t *source, dns_decompress_t *dctx, \
81 unsigned int options, isc_buffer_t *target
83 #define ARGS_TOWIRE dns_rdata_t *rdata, dns_compress_t *cctx, \
86 #define ARGS_COMPARE const dns_rdata_t *rdata1, const dns_rdata_t *rdata2
88 #define ARGS_FROMSTRUCT int rdclass, dns_rdatatype_t type, \
89 void *source, isc_buffer_t *target
91 #define ARGS_TOSTRUCT dns_rdata_t *rdata, void *target, isc_mem_t *mctx
93 #define ARGS_FREESTRUCT void *source
95 #define ARGS_ADDLDATA dns_rdata_t *rdata, dns_additionaldatafunc_t add, \
98 #define ARGS_DIGEST dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg
100 #define ARGS_CHECKOWNER dns_name_t *name, dns_rdataclass_t rdclass, \
101 dns_rdatatype_t type, isc_boolean_t wildcard
103 #define ARGS_CHECKNAMES dns_rdata_t *rdata, dns_name_t *owner, dns_name_t *bad
107 * Context structure for the totext_ functions.
108 * Contains formatting options for rdata-to-text
111 typedef struct dns_rdata_textctx {
112 dns_name_t *origin; /*%< Current origin, or NULL. */
113 unsigned int flags; /*%< DNS_STYLEFLAG_* */
114 unsigned int width; /*%< Width of rdata column. */
115 const char *linebreak; /*%< Line break string. */
116 } dns_rdata_textctx_t;
119 txt_totext(isc_region_t *source, isc_buffer_t *target);
122 txt_fromtext(isc_textregion_t *source, isc_buffer_t *target);
125 txt_fromwire(isc_buffer_t *source, isc_buffer_t *target);
128 name_prefix(dns_name_t *name, dns_name_t *origin, dns_name_t *target);
131 name_length(dns_name_t *name);
134 str_totext(const char *source, isc_buffer_t *target);
137 inet_totext(int af, isc_region_t *src, isc_buffer_t *target);
140 buffer_empty(isc_buffer_t *source);
143 buffer_fromregion(isc_buffer_t *buffer, isc_region_t *region);
146 uint32_tobuffer(isc_uint32_t, isc_buffer_t *target);
149 uint16_tobuffer(isc_uint32_t, isc_buffer_t *target);
152 uint8_tobuffer(isc_uint32_t, isc_buffer_t *target);
155 name_tobuffer(dns_name_t *name, isc_buffer_t *target);
158 uint32_fromregion(isc_region_t *region);
161 uint16_fromregion(isc_region_t *region);
164 uint8_fromregion(isc_region_t *region);
167 uint8_consume_fromregion(isc_region_t *region);
170 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);
173 hexvalue(char value);
176 decvalue(char value);
179 btoa_totext(unsigned char *inbuf, int inbuflen, isc_buffer_t *target);
182 atob_tobuffer(isc_lex_t *lexer, isc_buffer_t *target);
185 default_fromtext_callback(dns_rdatacallbacks_t *callbacks, const char *, ...)
186 ISC_FORMAT_PRINTF(2, 3);
189 fromtext_error(void (*callback)(dns_rdatacallbacks_t *, const char *, ...),
190 dns_rdatacallbacks_t *callbacks, const char *name,
191 unsigned long line, isc_token_t *token, isc_result_t result);
194 fromtext_warneof(isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks);
197 rdata_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
198 isc_buffer_t *target);
201 warn_badname(dns_name_t *name, isc_lex_t *lexer,
202 dns_rdatacallbacks_t *callbacks);
205 warn_badmx(isc_token_t *token, isc_lex_t *lexer,
206 dns_rdatacallbacks_t *callbacks);
209 uint16_consume_fromregion(isc_region_t *region);
212 getquad(const void *src, struct in_addr *dst,
213 isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks)
218 result = inet_aton(src, dst);
219 if (result == 1 && callbacks != NULL &&
220 inet_pton(AF_INET, src, &tmp) != 1) {
221 const char *name = isc_lex_getsourcename(lexer);
224 (*callbacks->warn)(callbacks, "%s:%lu: \"%s\" "
225 "is not a decimal dotted quad", name,
226 isc_lex_getsourceline(lexer), src);
231 static inline isc_result_t
232 name_duporclone(dns_name_t *source, isc_mem_t *mctx, dns_name_t *target) {
235 return (dns_name_dup(source, mctx, target));
236 dns_name_clone(source, target);
237 return (ISC_R_SUCCESS);
241 mem_maybedup(isc_mem_t *mctx, void *source, size_t length) {
246 new = isc_mem_allocate(mctx, length);
248 memcpy(new, source, length);
253 static const char hexdigits[] = "0123456789abcdef";
254 static const char decdigits[] = "0123456789";
259 #define RESERVED 0x0002
266 dns_rdata_init(dns_rdata_t *rdata) {
268 REQUIRE(rdata != NULL);
275 ISC_LINK_INIT(rdata, link);
276 /* ISC_LIST_INIT(rdata->list); */
280 dns_rdata_reset(dns_rdata_t *rdata) {
282 REQUIRE(rdata != NULL);
284 REQUIRE(!ISC_LINK_LINKED(rdata, link));
285 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
299 dns_rdata_clone(const dns_rdata_t *src, dns_rdata_t *target) {
301 REQUIRE(src != NULL);
302 REQUIRE(target != NULL);
304 REQUIRE(DNS_RDATA_INITIALIZED(target));
306 REQUIRE(DNS_RDATA_VALIDFLAGS(src));
307 REQUIRE(DNS_RDATA_VALIDFLAGS(target));
309 target->data = src->data;
310 target->length = src->length;
311 target->rdclass = src->rdclass;
312 target->type = src->type;
313 target->flags = src->flags;
322 dns_rdata_compare(const dns_rdata_t *rdata1, const dns_rdata_t *rdata2) {
324 isc_boolean_t use_default = ISC_FALSE;
326 REQUIRE(rdata1 != NULL);
327 REQUIRE(rdata2 != NULL);
328 REQUIRE(rdata1->data != NULL);
329 REQUIRE(rdata2->data != NULL);
330 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata1));
331 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata2));
333 if (rdata1->rdclass != rdata2->rdclass)
334 return (rdata1->rdclass < rdata2->rdclass ? -1 : 1);
336 if (rdata1->type != rdata2->type)
337 return (rdata1->type < rdata2->type ? -1 : 1);
345 dns_rdata_toregion(rdata1, &r1);
346 dns_rdata_toregion(rdata2, &r2);
347 result = isc_region_compare(&r1, &r2);
353 dns_rdata_casecompare(const dns_rdata_t *rdata1, const dns_rdata_t *rdata2) {
355 isc_boolean_t use_default = ISC_FALSE;
357 REQUIRE(rdata1 != NULL);
358 REQUIRE(rdata2 != NULL);
359 REQUIRE(rdata1->data != NULL);
360 REQUIRE(rdata2->data != NULL);
361 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata1));
362 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata2));
364 if (rdata1->rdclass != rdata2->rdclass)
365 return (rdata1->rdclass < rdata2->rdclass ? -1 : 1);
367 if (rdata1->type != rdata2->type)
368 return (rdata1->type < rdata2->type ? -1 : 1);
376 dns_rdata_toregion(rdata1, &r1);
377 dns_rdata_toregion(rdata2, &r2);
378 result = isc_region_compare(&r1, &r2);
388 dns_rdata_fromregion(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
389 dns_rdatatype_t type, isc_region_t *r)
392 REQUIRE(rdata != NULL);
393 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
396 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
398 rdata->data = r->base;
399 rdata->length = r->length;
400 rdata->rdclass = rdclass;
406 dns_rdata_toregion(const dns_rdata_t *rdata, isc_region_t *r) {
408 REQUIRE(rdata != NULL);
410 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
412 r->base = rdata->data;
413 r->length = rdata->length;
417 dns_rdata_fromwire(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
418 dns_rdatatype_t type, isc_buffer_t *source,
419 dns_decompress_t *dctx, unsigned int options,
420 isc_buffer_t *target)
422 isc_result_t result = ISC_R_NOTIMPLEMENTED;
426 isc_boolean_t use_default = ISC_FALSE;
427 isc_uint32_t activelength;
429 REQUIRE(dctx != NULL);
431 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
432 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
436 return (DNS_R_FORMERR);
441 activelength = isc_buffer_activelength(source);
442 INSIST(activelength < 65536);
447 if (activelength > isc_buffer_availablelength(target))
448 result = ISC_R_NOSPACE;
450 isc_buffer_putmem(target, isc_buffer_current(source),
452 isc_buffer_forward(source, activelength);
453 result = ISC_R_SUCCESS;
458 * We should have consumed all of our buffer.
460 if (result == ISC_R_SUCCESS && !buffer_empty(source))
461 result = DNS_R_EXTRADATA;
463 if (rdata != NULL && result == ISC_R_SUCCESS) {
464 region.base = isc_buffer_used(&st);
465 region.length = isc_buffer_usedlength(target) -
466 isc_buffer_usedlength(&st);
467 dns_rdata_fromregion(rdata, rdclass, type, ®ion);
470 if (result != ISC_R_SUCCESS) {
478 dns_rdata_towire(dns_rdata_t *rdata, dns_compress_t *cctx,
479 isc_buffer_t *target)
481 isc_result_t result = ISC_R_NOTIMPLEMENTED;
482 isc_boolean_t use_default = ISC_FALSE;
486 REQUIRE(rdata != NULL);
487 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
490 * Some DynDNS meta-RRs have empty rdata.
492 if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
493 INSIST(rdata->length == 0);
494 return (ISC_R_SUCCESS);
502 isc_buffer_availableregion(target, &tr);
503 if (tr.length < rdata->length)
504 return (ISC_R_NOSPACE);
505 memcpy(tr.base, rdata->data, rdata->length);
506 isc_buffer_add(target, rdata->length);
507 return (ISC_R_SUCCESS);
509 if (result != ISC_R_SUCCESS) {
511 INSIST(target->used < 65536);
512 dns_compress_rollback(cctx, (isc_uint16_t)target->used);
518 * If the binary data in 'src' is valid uncompressed wire format
519 * rdata of class 'rdclass' and type 'type', return ISC_R_SUCCESS
520 * and copy the validated rdata to 'dest'. Otherwise return an error.
523 rdata_validate(isc_buffer_t *src, isc_buffer_t *dest, dns_rdataclass_t rdclass,
524 dns_rdatatype_t type)
526 dns_decompress_t dctx;
527 dns_rdata_t rdata = DNS_RDATA_INIT;
530 dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_NONE);
531 isc_buffer_setactive(src, isc_buffer_usedlength(src));
532 result = dns_rdata_fromwire(&rdata, rdclass, type, src,
534 dns_decompress_invalidate(&dctx);
540 unknown_fromtext(dns_rdataclass_t rdclass, dns_rdatatype_t type,
541 isc_lex_t *lexer, isc_mem_t *mctx, isc_buffer_t *target)
544 isc_buffer_t *buf = NULL;
547 if (type == 0 || dns_rdatatype_ismeta(type))
548 return (DNS_R_METATYPE);
550 result = isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
552 if (result == ISC_R_SUCCESS && token.value.as_ulong > 65535U)
553 return (ISC_R_RANGE);
554 result = isc_buffer_allocate(mctx, &buf, token.value.as_ulong);
555 if (result != ISC_R_SUCCESS)
558 result = isc_hex_tobuffer(lexer, buf,
559 (unsigned int)token.value.as_ulong);
560 if (result != ISC_R_SUCCESS)
562 if (isc_buffer_usedlength(buf) != token.value.as_ulong) {
563 result = ISC_R_UNEXPECTEDEND;
567 if (dns_rdatatype_isknown(type)) {
568 result = rdata_validate(buf, target, rdclass, type);
571 isc_buffer_usedregion(buf, &r);
572 result = isc_buffer_copyregion(target, &r);
574 if (result != ISC_R_SUCCESS)
577 isc_buffer_free(&buf);
578 return (ISC_R_SUCCESS);
581 isc_buffer_free(&buf);
586 dns_rdata_fromtext(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
587 dns_rdatatype_t type, isc_lex_t *lexer,
588 dns_name_t *origin, unsigned int options, isc_mem_t *mctx,
589 isc_buffer_t *target, dns_rdatacallbacks_t *callbacks)
591 isc_result_t result = ISC_R_NOTIMPLEMENTED;
595 unsigned int lexoptions = ISC_LEXOPT_EOL | ISC_LEXOPT_EOF |
596 ISC_LEXOPT_DNSMULTILINE | ISC_LEXOPT_ESCAPE;
599 void (*callback)(dns_rdatacallbacks_t *, const char *, ...);
600 isc_result_t tresult;
602 REQUIRE(origin == NULL || dns_name_isabsolute(origin) == ISC_TRUE);
604 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
605 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
607 if (callbacks != NULL) {
608 REQUIRE(callbacks->warn != NULL);
609 REQUIRE(callbacks->error != NULL);
614 if (callbacks != NULL)
615 callback = callbacks->error;
617 callback = default_fromtext_callback;
619 result = isc_lex_getmastertoken(lexer, &token, isc_tokentype_qstring,
621 if (result != ISC_R_SUCCESS) {
622 name = isc_lex_getsourcename(lexer);
623 line = isc_lex_getsourceline(lexer);
624 fromtext_error(callback, callbacks, name, line,
629 if (strcmp(DNS_AS_STR(token), "\\#") == 0)
630 result = unknown_fromtext(rdclass, type, lexer, mctx, target);
632 isc_lex_ungettoken(lexer, &token);
638 * Consume to end of line / file.
639 * If not at end of line initially set error code.
640 * Call callback via fromtext_error once if there was an error.
643 name = isc_lex_getsourcename(lexer);
644 line = isc_lex_getsourceline(lexer);
645 tresult = isc_lex_gettoken(lexer, lexoptions, &token);
646 if (tresult != ISC_R_SUCCESS) {
647 if (result == ISC_R_SUCCESS)
649 if (callback != NULL)
650 fromtext_error(callback, callbacks, name,
653 } else if (token.type != isc_tokentype_eol &&
654 token.type != isc_tokentype_eof) {
655 if (result == ISC_R_SUCCESS)
656 result = DNS_R_EXTRATOKEN;
657 if (callback != NULL) {
658 fromtext_error(callback, callbacks, name,
659 line, &token, result);
662 } else if (result != ISC_R_SUCCESS && callback != NULL) {
663 fromtext_error(callback, callbacks, name, line,
667 if (token.type == isc_tokentype_eof)
668 fromtext_warneof(lexer, callbacks);
673 if (rdata != NULL && result == ISC_R_SUCCESS) {
674 region.base = isc_buffer_used(&st);
675 region.length = isc_buffer_usedlength(target) -
676 isc_buffer_usedlength(&st);
677 dns_rdata_fromregion(rdata, rdclass, type, ®ion);
679 if (result != ISC_R_SUCCESS) {
686 rdata_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
687 isc_buffer_t *target)
689 isc_result_t result = ISC_R_NOTIMPLEMENTED;
690 isc_boolean_t use_default = ISC_FALSE;
691 char buf[sizeof("65535")];
694 REQUIRE(rdata != NULL);
695 REQUIRE(tctx->origin == NULL ||
696 dns_name_isabsolute(tctx->origin) == ISC_TRUE);
699 * Some DynDNS meta-RRs have empty rdata.
701 if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
702 INSIST(rdata->length == 0);
703 return (ISC_R_SUCCESS);
709 strlcpy(buf, "\\# ", sizeof(buf));
710 result = str_totext(buf, target);
711 dns_rdata_toregion(rdata, &sr);
712 INSIST(sr.length < 65536);
713 snprintf(buf, sizeof(buf), "%u", sr.length);
714 result = str_totext(buf, target);
715 if (sr.length != 0 && result == ISC_R_SUCCESS) {
716 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
717 result = str_totext(" ( ", target);
719 result = str_totext(" ", target);
720 if (result == ISC_R_SUCCESS)
721 result = isc_hex_totext(&sr, tctx->width - 2,
724 if (result == ISC_R_SUCCESS &&
725 (tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
726 result = str_totext(" )", target);
734 dns_rdata_totext(dns_rdata_t *rdata, dns_name_t *origin, isc_buffer_t *target)
736 dns_rdata_textctx_t tctx;
738 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
741 * Set up formatting options for single-line output.
743 tctx.origin = origin;
746 tctx.linebreak = " ";
747 return (rdata_totext(rdata, &tctx, target));
751 dns_rdata_tofmttext(dns_rdata_t *rdata, dns_name_t *origin,
752 unsigned int flags, unsigned int width,
753 const char *linebreak, isc_buffer_t *target)
755 dns_rdata_textctx_t tctx;
757 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
760 * Set up formatting options for formatted output.
762 tctx.origin = origin;
764 if ((flags & DNS_STYLEFLAG_MULTILINE) != 0) {
766 tctx.linebreak = linebreak;
768 tctx.width = 60; /* Used for hex word length only. */
769 tctx.linebreak = " ";
771 return (rdata_totext(rdata, &tctx, target));
775 dns_rdata_fromstruct(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
776 dns_rdatatype_t type, void *source,
777 isc_buffer_t *target)
779 isc_result_t result = ISC_R_NOTIMPLEMENTED;
782 isc_boolean_t use_default = ISC_FALSE;
784 REQUIRE(source != NULL);
786 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
787 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
797 if (rdata != NULL && result == ISC_R_SUCCESS) {
798 region.base = isc_buffer_used(&st);
799 region.length = isc_buffer_usedlength(target) -
800 isc_buffer_usedlength(&st);
801 dns_rdata_fromregion(rdata, rdclass, type, ®ion);
803 if (result != ISC_R_SUCCESS)
809 dns_rdata_tostruct(dns_rdata_t *rdata, void *target, isc_mem_t *mctx) {
810 isc_result_t result = ISC_R_NOTIMPLEMENTED;
811 isc_boolean_t use_default = ISC_FALSE;
813 REQUIRE(rdata != NULL);
814 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
825 dns_rdata_freestruct(void *source) {
826 dns_rdatacommon_t *common = source;
827 REQUIRE(source != NULL);
833 dns_rdata_additionaldata(dns_rdata_t *rdata, dns_additionaldatafunc_t add,
836 isc_result_t result = ISC_R_NOTIMPLEMENTED;
837 isc_boolean_t use_default = ISC_FALSE;
840 * Call 'add' for each name and type from 'rdata' which is subject to
841 * additional section processing.
844 REQUIRE(rdata != NULL);
845 REQUIRE(add != NULL);
846 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
850 /* No additional processing for unknown types */
852 result = ISC_R_SUCCESS;
858 dns_rdata_digest(dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg) {
859 isc_result_t result = ISC_R_NOTIMPLEMENTED;
860 isc_boolean_t use_default = ISC_FALSE;
864 * Send 'rdata' in DNSSEC canonical form to 'digest'.
867 REQUIRE(rdata != NULL);
868 REQUIRE(digest != NULL);
869 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
874 dns_rdata_toregion(rdata, &r);
875 result = (digest)(arg, &r);
882 dns_rdata_checkowner(dns_name_t *name, dns_rdataclass_t rdclass,
883 dns_rdatatype_t type, isc_boolean_t wildcard)
885 isc_boolean_t result;
892 dns_rdata_checknames(dns_rdata_t *rdata, dns_name_t *owner, dns_name_t *bad)
894 isc_boolean_t result;
901 dns_rdatatype_attributes(dns_rdatatype_t type)
903 RDATATYPE_ATTRIBUTE_SW
904 if (type >= (dns_rdatatype_t)128 && type < (dns_rdatatype_t)255)
905 return (DNS_RDATATYPEATTR_UNKNOWN | DNS_RDATATYPEATTR_META);
906 return (DNS_RDATATYPEATTR_UNKNOWN);
910 dns_rdatatype_fromtext(dns_rdatatype_t *typep, isc_textregion_t *source) {
918 return (DNS_R_UNKNOWN);
920 a = tolower((unsigned char)source->base[0]);
921 b = tolower((unsigned char)source->base[n - 1]);
923 hash = ((a + n) * b) % 256;
926 * This switch block is inlined via \#define, and will use "return"
927 * to return a result to the caller if it is a valid (known)
930 RDATATYPE_FROMTEXT_SW(hash, source->base, n, typep);
932 if (source->length > 4 && source->length < (4 + sizeof("65000")) &&
933 strncasecmp("type", source->base, 4) == 0) {
934 char buf[sizeof("65000")];
938 strncpy(buf, source->base + 4, source->length - 4);
939 buf[source->length - 4] = '\0';
940 val = strtoul(buf, &endp, 10);
941 if (*endp == '\0' && val <= 0xffff) {
942 *typep = (dns_rdatatype_t)val;
943 return (ISC_R_SUCCESS);
947 return (DNS_R_UNKNOWN);
951 dns_rdatatype_totext(dns_rdatatype_t type, isc_buffer_t *target) {
952 char buf[sizeof("TYPE65535")];
955 snprintf(buf, sizeof(buf), "TYPE%u", type);
956 return (str_totext(buf, target));
960 dns_rdatatype_format(dns_rdatatype_t rdtype,
961 char *array, unsigned int size)
966 isc_buffer_init(&buf, array, size);
967 result = dns_rdatatype_totext(rdtype, &buf);
971 if (result == ISC_R_SUCCESS) {
972 if (isc_buffer_availablelength(&buf) >= 1)
973 isc_buffer_putuint8(&buf, 0);
975 result = ISC_R_NOSPACE;
977 if (result != ISC_R_SUCCESS) {
978 snprintf(array, size, "<unknown>");
979 array[size - 1] = '\0';
988 name_length(dns_name_t *name) {
989 return (name->length);
993 txt_totext(isc_region_t *source, isc_buffer_t *target) {
1000 isc_buffer_availableregion(target, ®ion);
1002 tp = (char *)region.base;
1007 REQUIRE(n + 1 <= source->length);
1010 return (ISC_R_NOSPACE);
1014 if (*sp < 0x20 || *sp >= 0x7f) {
1016 return (ISC_R_NOSPACE);
1018 *tp++ = 0x30 + ((*sp / 100) % 10);
1019 *tp++ = 0x30 + ((*sp / 10) % 10);
1020 *tp++ = 0x30 + (*sp % 10);
1025 /* double quote, semi-colon, backslash */
1026 if (*sp == 0x22 || *sp == 0x3b || *sp == 0x5c) {
1028 return (ISC_R_NOSPACE);
1033 return (ISC_R_NOSPACE);
1038 return (ISC_R_NOSPACE);
1041 isc_buffer_add(target, tp - (char *)region.base);
1042 isc_region_consume(source, *source->base + 1);
1043 return (ISC_R_SUCCESS);
1047 txt_fromtext(isc_textregion_t *source, isc_buffer_t *target) {
1048 isc_region_t tregion;
1049 isc_boolean_t escape;
1050 unsigned int n, nrem;
1056 isc_buffer_availableregion(target, &tregion);
1060 nrem = tregion.length;
1063 return (ISC_R_NOSPACE);
1070 * Maximum text string length.
1076 if (escape && (d = decvalue((char)c)) != -1) {
1079 return (DNS_R_SYNTAX);
1081 if ((d = decvalue(*s++)) != -1)
1084 return (DNS_R_SYNTAX);
1086 return (DNS_R_SYNTAX);
1088 if ((d = decvalue(*s++)) != -1)
1091 return (DNS_R_SYNTAX);
1093 return (DNS_R_SYNTAX);
1094 } else if (!escape && c == '\\') {
1100 return (ISC_R_NOSPACE);
1105 return (DNS_R_SYNTAX);
1106 *tregion.base = t - tregion.base - 1;
1107 isc_buffer_add(target, *tregion.base + 1);
1108 return (ISC_R_SUCCESS);
1112 txt_fromwire(isc_buffer_t *source, isc_buffer_t *target) {
1114 isc_region_t sregion;
1115 isc_region_t tregion;
1117 isc_buffer_activeregion(source, &sregion);
1118 if (sregion.length == 0)
1119 return(ISC_R_UNEXPECTEDEND);
1120 n = *sregion.base + 1;
1121 if (n > sregion.length)
1122 return (ISC_R_UNEXPECTEDEND);
1124 isc_buffer_availableregion(target, &tregion);
1125 if (n > tregion.length)
1126 return (ISC_R_NOSPACE);
1128 memcpy(tregion.base, sregion.base, n);
1129 isc_buffer_forward(source, n);
1130 isc_buffer_add(target, n);
1131 return (ISC_R_SUCCESS);
1134 static isc_boolean_t
1135 name_prefix(dns_name_t *name, dns_name_t *origin, dns_name_t *target) {
1141 if (dns_name_compare(origin, dns_rootname) == 0)
1144 if (!dns_name_issubdomain(name, origin))
1147 l1 = dns_name_countlabels(name);
1148 l2 = dns_name_countlabels(origin);
1153 dns_name_getlabelsequence(name, 0, l1 - l2, target);
1162 str_totext(const char *source, isc_buffer_t *target) {
1164 isc_region_t region;
1166 isc_buffer_availableregion(target, ®ion);
1169 if (l > region.length)
1170 return (ISC_R_NOSPACE);
1172 memcpy(region.base, source, l);
1173 isc_buffer_add(target, l);
1174 return (ISC_R_SUCCESS);
1178 inet_totext(int af, isc_region_t *src, isc_buffer_t *target) {
1181 /* Note - inet_ntop doesn't do size checking on its input. */
1182 if (inet_ntop(af, src->base, tmpbuf, sizeof(tmpbuf)) == NULL)
1183 return (ISC_R_NOSPACE);
1184 if (strlen(tmpbuf) > isc_buffer_availablelength(target))
1185 return (ISC_R_NOSPACE);
1186 isc_buffer_putstr(target, tmpbuf);
1187 return (ISC_R_SUCCESS);
1190 static isc_boolean_t
1191 buffer_empty(isc_buffer_t *source) {
1192 return((source->current == source->active) ? ISC_TRUE : ISC_FALSE);
1196 buffer_fromregion(isc_buffer_t *buffer, isc_region_t *region) {
1197 isc_buffer_init(buffer, region->base, region->length);
1198 isc_buffer_add(buffer, region->length);
1199 isc_buffer_setactive(buffer, region->length);
1203 uint32_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1204 isc_region_t region;
1206 isc_buffer_availableregion(target, ®ion);
1207 if (region.length < 4)
1208 return (ISC_R_NOSPACE);
1209 isc_buffer_putuint32(target, value);
1210 return (ISC_R_SUCCESS);
1214 uint16_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1215 isc_region_t region;
1218 return (ISC_R_RANGE);
1219 isc_buffer_availableregion(target, ®ion);
1220 if (region.length < 2)
1221 return (ISC_R_NOSPACE);
1222 isc_buffer_putuint16(target, (isc_uint16_t)value);
1223 return (ISC_R_SUCCESS);
1227 uint8_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1228 isc_region_t region;
1231 return (ISC_R_RANGE);
1232 isc_buffer_availableregion(target, ®ion);
1233 if (region.length < 1)
1234 return (ISC_R_NOSPACE);
1235 isc_buffer_putuint8(target, (isc_uint8_t)value);
1236 return (ISC_R_SUCCESS);
1240 name_tobuffer(dns_name_t *name, isc_buffer_t *target) {
1242 dns_name_toregion(name, &r);
1243 return (isc_buffer_copyregion(target, &r));
1247 uint32_fromregion(isc_region_t *region) {
1250 REQUIRE(region->length >= 4);
1251 value = region->base[0] << 24;
1252 value |= region->base[1] << 16;
1253 value |= region->base[2] << 8;
1254 value |= region->base[3];
1259 uint16_consume_fromregion(isc_region_t *region) {
1260 isc_uint16_t r = uint16_fromregion(region);
1262 isc_region_consume(region, 2);
1267 uint16_fromregion(isc_region_t *region) {
1269 REQUIRE(region->length >= 2);
1271 return ((region->base[0] << 8) | region->base[1]);
1275 uint8_fromregion(isc_region_t *region) {
1277 REQUIRE(region->length >= 1);
1279 return (region->base[0]);
1283 uint8_consume_fromregion(isc_region_t *region) {
1284 isc_uint8_t r = uint8_fromregion(region);
1286 isc_region_consume(region, 1);
1291 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
1294 isc_buffer_availableregion(target, &tr);
1295 if (length > tr.length)
1296 return (ISC_R_NOSPACE);
1297 memcpy(tr.base, base, length);
1298 isc_buffer_add(target, length);
1299 return (ISC_R_SUCCESS);
1303 hexvalue(char value) {
1307 c = (unsigned char)value;
1313 if ((s = strchr(hexdigits, c)) == NULL)
1315 return (s - hexdigits);
1319 decvalue(char value) {
1323 * isascii() is valid for full range of int values, no need to
1326 if (!isascii(value))
1328 if ((s = strchr(decdigits, value)) == NULL)
1330 return (s - decdigits);
1333 static const char atob_digits[86] =
1334 "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`" \
1335 "abcdefghijklmnopqrstu";
1337 * Subroutines to convert between 8 bit binary bytes and printable ASCII.
1338 * Computes the number of bytes, and three kinds of simple checksums.
1339 * Incoming bytes are collected into 32-bit words, then printed in base 85:
1340 * exp(85,5) > exp(2,32)
1341 * The ASCII characters used are between '!' and 'u';
1342 * 'z' encodes 32-bit zero; 'x' is used to mark the end of encoded data.
1344 * Originally by Paul Rutter (philabs!per) and Joe Orost (petsd!joe) for
1345 * the atob/btoa programs, released with the compress program, in mod.sources.
1346 * Modified by Mike Schwartz 8/19/86 for use in BIND.
1347 * Modified to be re-entrant 3/2/99.
1359 #define Ceor state->Ceor
1360 #define Csum state->Csum
1361 #define Crot state->Crot
1362 #define word state->word
1363 #define bcount state->bcount
1365 #define times85(x) ((((((x<<2)+x)<<2)+x)<<2)+x)
1367 static isc_result_t byte_atob(int c, isc_buffer_t *target,
1368 struct state *state);
1369 static isc_result_t putbyte(int c, isc_buffer_t *, struct state *state);
1370 static isc_result_t byte_btoa(int c, isc_buffer_t *, struct state *state);
1373 * Decode ASCII-encoded byte c into binary representation and
1374 * place into *bufp, advancing bufp.
1377 byte_atob(int c, isc_buffer_t *target, struct state *state) {
1381 return(DNS_R_SYNTAX);
1383 RETERR(putbyte(0, target, state));
1384 RETERR(putbyte(0, target, state));
1385 RETERR(putbyte(0, target, state));
1386 RETERR(putbyte(0, target, state));
1388 } else if ((s = strchr(atob_digits, c)) != NULL) {
1390 word = s - atob_digits;
1392 } else if (bcount < 4) {
1393 word = times85(word);
1394 word += s - atob_digits;
1397 word = times85(word);
1398 word += s - atob_digits;
1399 RETERR(putbyte((word >> 24) & 0xff, target, state));
1400 RETERR(putbyte((word >> 16) & 0xff, target, state));
1401 RETERR(putbyte((word >> 8) & 0xff, target, state));
1402 RETERR(putbyte(word & 0xff, target, state));
1407 return(DNS_R_SYNTAX);
1408 return(ISC_R_SUCCESS);
1412 * Compute checksum info and place c into target.
1415 putbyte(int c, isc_buffer_t *target, struct state *state) {
1421 if ((Crot & 0x80000000)) {
1428 isc_buffer_availableregion(target, &tr);
1430 return (ISC_R_NOSPACE);
1432 isc_buffer_add(target, 1);
1433 return (ISC_R_SUCCESS);
1437 * Read the ASCII-encoded data from inbuf, of length inbuflen, and convert
1438 * it into T_UNSPEC (binary data) in outbuf, not to exceed outbuflen bytes;
1439 * outbuflen must be divisible by 4. (Note: this is because outbuf is filled
1440 * in 4 bytes at a time. If the actual data doesn't end on an even 4-byte
1441 * boundary, there will be no problem...it will be padded with 0 bytes, and
1442 * numbytes will indicate the correct number of bytes. The main point is
1443 * that since the buffer is filled in 4 bytes at a time, even if there is
1444 * not a full 4 bytes of data at the end, there has to be room to 0-pad the
1445 * data, so the buffer must be of size divisible by 4). Place the number of
1446 * output bytes in numbytes, and return a failure/success status.
1450 atob_tobuffer(isc_lex_t *lexer, isc_buffer_t *target) {
1451 long oeor, osum, orot;
1452 struct state statebuf, *state= &statebuf;
1457 Ceor = Csum = Crot = word = bcount = 0;
1459 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1461 while (token.value.as_textregion.length != 0) {
1462 if ((c = token.value.as_textregion.base[0]) == 'x') {
1465 RETERR(byte_atob(c, target, state));
1466 isc_textregion_consume(&token.value.as_textregion, 1);
1472 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
1474 if ((token.value.as_ulong % 4) != 0U)
1475 isc_buffer_subtract(target, 4 - (token.value.as_ulong % 4));
1480 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1482 oeor = strtol(DNS_AS_STR(token), &e, 16);
1484 return (DNS_R_SYNTAX);
1489 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1491 osum = strtol(DNS_AS_STR(token), &e, 16);
1493 return (DNS_R_SYNTAX);
1498 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1500 orot = strtol(DNS_AS_STR(token), &e, 16);
1502 return (DNS_R_SYNTAX);
1504 if ((oeor != Ceor) || (osum != Csum) || (orot != Crot))
1505 return(DNS_R_BADCKSUM);
1506 return (ISC_R_SUCCESS);
1510 * Encode binary byte c into ASCII representation and place into *bufp,
1514 byte_btoa(int c, isc_buffer_t *target, struct state *state) {
1517 isc_buffer_availableregion(target, &tr);
1521 if ((Crot & 0x80000000)) {
1534 return (ISC_R_NOSPACE);
1536 isc_buffer_add(target, 1);
1538 register int tmp = 0;
1539 register isc_int32_t tmpword = word;
1543 * Because some don't support u_long.
1546 tmpword -= (isc_int32_t)(85 * 85 * 85 * 85 * 32);
1550 tmpword -= (isc_int32_t)(85 * 85 * 85 * 85 * 32);
1553 return (ISC_R_NOSPACE);
1554 tr.base[0] = atob_digits[(tmpword /
1555 (isc_int32_t)(85 * 85 * 85 * 85))
1557 tmpword %= (isc_int32_t)(85 * 85 * 85 * 85);
1558 tr.base[1] = atob_digits[tmpword / (85 * 85 * 85)];
1559 tmpword %= (85 * 85 * 85);
1560 tr.base[2] = atob_digits[tmpword / (85 * 85)];
1561 tmpword %= (85 * 85);
1562 tr.base[3] = atob_digits[tmpword / 85];
1564 tr.base[4] = atob_digits[tmpword];
1565 isc_buffer_add(target, 5);
1571 return (ISC_R_SUCCESS);
1576 * Encode the binary data from inbuf, of length inbuflen, into a
1577 * target. Return success/failure status
1580 btoa_totext(unsigned char *inbuf, int inbuflen, isc_buffer_t *target) {
1582 struct state statebuf, *state = &statebuf;
1583 char buf[sizeof("x 2000000000 ffffffff ffffffff ffffffff")];
1585 Ceor = Csum = Crot = word = bcount = 0;
1586 for (inc = 0; inc < inbuflen; inbuf++, inc++)
1587 RETERR(byte_btoa(*inbuf, target, state));
1590 RETERR(byte_btoa(0, target, state));
1593 * Put byte count and checksum information at end of buffer,
1596 snprintf(buf, sizeof(buf), "x %d %x %x %x", inbuflen, Ceor, Csum, Crot);
1597 return (str_totext(buf, target));
1602 default_fromtext_callback(dns_rdatacallbacks_t *callbacks, const char *fmt,
1610 vfprintf(stderr, fmt, ap);
1612 fprintf(stderr, "\n");
1616 fromtext_warneof(isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks) {
1617 if (isc_lex_isfile(lexer) && callbacks != NULL) {
1618 const char *name = isc_lex_getsourcename(lexer);
1621 (*callbacks->warn)(callbacks,
1622 "%s:%lu: file does not end with newline",
1623 name, isc_lex_getsourceline(lexer));
1628 warn_badmx(isc_token_t *token, isc_lex_t *lexer,
1629 dns_rdatacallbacks_t *callbacks)
1634 if (lexer != NULL) {
1635 file = isc_lex_getsourcename(lexer);
1636 line = isc_lex_getsourceline(lexer);
1637 (*callbacks->warn)(callbacks, "%s:%u: warning: '%s': %s",
1638 file, line, DNS_AS_STR(*token),
1639 dns_result_totext(DNS_R_MXISADDRESS));
1644 warn_badname(dns_name_t *name, isc_lex_t *lexer,
1645 dns_rdatacallbacks_t *callbacks)
1649 char namebuf[DNS_NAME_FORMATSIZE];
1651 if (lexer != NULL) {
1652 file = isc_lex_getsourcename(lexer);
1653 line = isc_lex_getsourceline(lexer);
1654 dns_name_format(name, namebuf, sizeof(namebuf));
1655 (*callbacks->warn)(callbacks, "%s:%u: warning: %s: %s",
1656 file, line, namebuf,
1657 dns_result_totext(DNS_R_BADNAME));
1662 fromtext_error(void (*callback)(dns_rdatacallbacks_t *, const char *, ...),
1663 dns_rdatacallbacks_t *callbacks, const char *name,
1664 unsigned long line, isc_token_t *token, isc_result_t result)
1669 if (token != NULL) {
1670 switch (token->type) {
1671 case isc_tokentype_eol:
1672 (*callback)(callbacks, "%s: %s:%lu: near eol: %s",
1673 "dns_rdata_fromtext", name, line,
1674 dns_result_totext(result));
1676 case isc_tokentype_eof:
1677 (*callback)(callbacks, "%s: %s:%lu: near eof: %s",
1678 "dns_rdata_fromtext", name, line,
1679 dns_result_totext(result));
1681 case isc_tokentype_number:
1682 (*callback)(callbacks, "%s: %s:%lu: near %lu: %s",
1683 "dns_rdata_fromtext", name, line,
1684 token->value.as_ulong,
1685 dns_result_totext(result));
1687 case isc_tokentype_string:
1688 case isc_tokentype_qstring:
1689 (*callback)(callbacks, "%s: %s:%lu: near '%s': %s",
1690 "dns_rdata_fromtext", name, line,
1692 dns_result_totext(result));
1695 (*callback)(callbacks, "%s: %s:%lu: %s",
1696 "dns_rdata_fromtext", name, line,
1697 dns_result_totext(result));
1701 (*callback)(callbacks, "dns_rdata_fromtext: %s:%lu: %s",
1702 name, line, dns_result_totext(result));
1707 dns_rdata_covers(dns_rdata_t *rdata) {
1708 if (rdata->type == 46)
1709 return (covers_rrsig(rdata));
1710 return (covers_sig(rdata));
1714 dns_rdatatype_ismeta(dns_rdatatype_t type) {
1715 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_META) != 0)
1721 dns_rdatatype_issingleton(dns_rdatatype_t type) {
1722 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_SINGLETON)
1729 dns_rdatatype_notquestion(dns_rdatatype_t type) {
1730 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_NOTQUESTION)
1737 dns_rdatatype_questiononly(dns_rdatatype_t type) {
1738 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_QUESTIONONLY)
1745 dns_rdatatype_atparent(dns_rdatatype_t type) {
1746 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_ATPARENT) != 0)
1752 dns_rdataclass_ismeta(dns_rdataclass_t rdclass) {
1754 if (rdclass == dns_rdataclass_reserved0
1755 || rdclass == dns_rdataclass_none
1756 || rdclass == dns_rdataclass_any)
1759 return (ISC_FALSE); /* Assume it is not a meta class. */
1763 dns_rdatatype_isdnssec(dns_rdatatype_t type) {
1764 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_DNSSEC) != 0)
1770 dns_rdatatype_iszonecutauth(dns_rdatatype_t type) {
1771 if ((dns_rdatatype_attributes(type)
1772 & (DNS_RDATATYPEATTR_DNSSEC | DNS_RDATATYPEATTR_ZONECUTAUTH))
1779 dns_rdatatype_isknown(dns_rdatatype_t type) {
1780 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_UNKNOWN)
1787 dns_rdata_exists(dns_rdata_t *rdata, dns_rdatatype_t type) {
1789 REQUIRE(rdata != NULL);
1790 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
1794 rdata->flags = DNS_RDATA_UPDATE;
1796 rdata->rdclass = dns_rdataclass_any;
1800 dns_rdata_notexist(dns_rdata_t *rdata, dns_rdatatype_t type) {
1802 REQUIRE(rdata != NULL);
1803 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
1807 rdata->flags = DNS_RDATA_UPDATE;
1809 rdata->rdclass = dns_rdataclass_none;
1813 dns_rdata_deleterrset(dns_rdata_t *rdata, dns_rdatatype_t type) {
1815 REQUIRE(rdata != NULL);
1816 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
1820 rdata->flags = DNS_RDATA_UPDATE;
1822 rdata->rdclass = dns_rdataclass_any;
1826 dns_rdata_makedelete(dns_rdata_t *rdata) {
1827 REQUIRE(rdata != NULL);
1829 rdata->rdclass = dns_rdataclass_none;
1833 dns_rdata_updateop(dns_rdata_t *rdata, dns_section_t section) {
1835 REQUIRE(rdata != NULL);
1836 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
1839 case DNS_SECTION_PREREQUISITE:
1840 switch (rdata->rdclass) {
1841 case dns_rdataclass_none:
1842 switch (rdata->type) {
1843 case dns_rdatatype_any:
1844 return ("domain doesn't exist");
1846 return ("rrset doesn't exist");
1848 case dns_rdataclass_any:
1849 switch (rdata->type) {
1850 case dns_rdatatype_any:
1851 return ("domain exists");
1853 return ("rrset exists (value independent)");
1856 return ("rrset exists (value dependent)");
1858 case DNS_SECTION_UPDATE:
1859 switch (rdata->rdclass) {
1860 case dns_rdataclass_none:
1862 case dns_rdataclass_any:
1863 switch (rdata->type) {
1864 case dns_rdatatype_any:
1865 return ("delete all rrsets");
1867 return ("delete rrset");