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;
419 REQUIRE(dctx != NULL);
421 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
422 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
426 return (DNS_R_FORMERR);
431 activelength = isc_buffer_activelength(source);
432 INSIST(activelength < 65536);
437 if (activelength > isc_buffer_availablelength(target))
438 result = ISC_R_NOSPACE;
440 isc_buffer_putmem(target, isc_buffer_current(source),
442 isc_buffer_forward(source, activelength);
443 result = ISC_R_SUCCESS;
448 * Reject any rdata that expands out to more than DNS_RDATA_MAXLENGTH
449 * as we cannot transmit it.
451 length = isc_buffer_usedlength(target) - isc_buffer_usedlength(&st);
452 if (result == ISC_R_SUCCESS && length > DNS_RDATA_MAXLENGTH)
453 result = DNS_R_FORMERR;
456 * We should have consumed all of our buffer.
458 if (result == ISC_R_SUCCESS && !buffer_empty(source))
459 result = DNS_R_EXTRADATA;
461 if (rdata != NULL && result == ISC_R_SUCCESS) {
462 region.base = isc_buffer_used(&st);
463 region.length = length;
464 dns_rdata_fromregion(rdata, rdclass, type, ®ion);
467 if (result != ISC_R_SUCCESS) {
475 dns_rdata_towire(dns_rdata_t *rdata, dns_compress_t *cctx,
476 isc_buffer_t *target)
478 isc_result_t result = ISC_R_NOTIMPLEMENTED;
479 isc_boolean_t use_default = ISC_FALSE;
483 REQUIRE(rdata != NULL);
484 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
487 * Some DynDNS meta-RRs have empty rdata.
489 if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
490 INSIST(rdata->length == 0);
491 return (ISC_R_SUCCESS);
499 isc_buffer_availableregion(target, &tr);
500 if (tr.length < rdata->length)
501 return (ISC_R_NOSPACE);
502 memcpy(tr.base, rdata->data, rdata->length);
503 isc_buffer_add(target, rdata->length);
504 return (ISC_R_SUCCESS);
506 if (result != ISC_R_SUCCESS) {
508 INSIST(target->used < 65536);
509 dns_compress_rollback(cctx, (isc_uint16_t)target->used);
515 * If the binary data in 'src' is valid uncompressed wire format
516 * rdata of class 'rdclass' and type 'type', return ISC_R_SUCCESS
517 * and copy the validated rdata to 'dest'. Otherwise return an error.
520 rdata_validate(isc_buffer_t *src, isc_buffer_t *dest, dns_rdataclass_t rdclass,
521 dns_rdatatype_t type)
523 dns_decompress_t dctx;
524 dns_rdata_t rdata = DNS_RDATA_INIT;
527 dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_NONE);
528 isc_buffer_setactive(src, isc_buffer_usedlength(src));
529 result = dns_rdata_fromwire(&rdata, rdclass, type, src,
531 dns_decompress_invalidate(&dctx);
537 unknown_fromtext(dns_rdataclass_t rdclass, dns_rdatatype_t type,
538 isc_lex_t *lexer, isc_mem_t *mctx, isc_buffer_t *target)
541 isc_buffer_t *buf = NULL;
544 if (type == 0 || dns_rdatatype_ismeta(type))
545 return (DNS_R_METATYPE);
547 result = isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
549 if (result == ISC_R_SUCCESS && token.value.as_ulong > 65535U)
550 return (ISC_R_RANGE);
551 result = isc_buffer_allocate(mctx, &buf, token.value.as_ulong);
552 if (result != ISC_R_SUCCESS)
555 result = isc_hex_tobuffer(lexer, buf,
556 (unsigned int)token.value.as_ulong);
557 if (result != ISC_R_SUCCESS)
559 if (isc_buffer_usedlength(buf) != token.value.as_ulong) {
560 result = ISC_R_UNEXPECTEDEND;
564 if (dns_rdatatype_isknown(type)) {
565 result = rdata_validate(buf, target, rdclass, type);
568 isc_buffer_usedregion(buf, &r);
569 result = isc_buffer_copyregion(target, &r);
571 if (result != ISC_R_SUCCESS)
574 isc_buffer_free(&buf);
575 return (ISC_R_SUCCESS);
578 isc_buffer_free(&buf);
583 dns_rdata_fromtext(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
584 dns_rdatatype_t type, isc_lex_t *lexer,
585 dns_name_t *origin, unsigned int options, isc_mem_t *mctx,
586 isc_buffer_t *target, dns_rdatacallbacks_t *callbacks)
588 isc_result_t result = ISC_R_NOTIMPLEMENTED;
592 unsigned int lexoptions = ISC_LEXOPT_EOL | ISC_LEXOPT_EOF |
593 ISC_LEXOPT_DNSMULTILINE | ISC_LEXOPT_ESCAPE;
596 void (*callback)(dns_rdatacallbacks_t *, const char *, ...);
597 isc_result_t tresult;
600 REQUIRE(origin == NULL || dns_name_isabsolute(origin) == ISC_TRUE);
602 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
603 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
605 if (callbacks != NULL) {
606 REQUIRE(callbacks->warn != NULL);
607 REQUIRE(callbacks->error != NULL);
612 if (callbacks != NULL)
613 callback = callbacks->error;
615 callback = default_fromtext_callback;
617 result = isc_lex_getmastertoken(lexer, &token, isc_tokentype_qstring,
619 if (result != ISC_R_SUCCESS) {
620 name = isc_lex_getsourcename(lexer);
621 line = isc_lex_getsourceline(lexer);
622 fromtext_error(callback, callbacks, name, line, NULL, result);
626 if (strcmp(DNS_AS_STR(token), "\\#") == 0)
627 result = unknown_fromtext(rdclass, type, lexer, mctx, target);
629 isc_lex_ungettoken(lexer, &token);
635 * Consume to end of line / file.
636 * If not at end of line initially set error code.
637 * Call callback via fromtext_error once if there was an error.
640 name = isc_lex_getsourcename(lexer);
641 line = isc_lex_getsourceline(lexer);
642 tresult = isc_lex_gettoken(lexer, lexoptions, &token);
643 if (tresult != ISC_R_SUCCESS) {
644 if (result == ISC_R_SUCCESS)
646 if (callback != NULL)
647 fromtext_error(callback, callbacks, name,
650 } else if (token.type != isc_tokentype_eol &&
651 token.type != isc_tokentype_eof) {
652 if (result == ISC_R_SUCCESS)
653 result = DNS_R_EXTRATOKEN;
654 if (callback != NULL) {
655 fromtext_error(callback, callbacks, name,
656 line, &token, result);
659 } else if (result != ISC_R_SUCCESS && callback != NULL) {
660 fromtext_error(callback, callbacks, name, line,
664 if (token.type == isc_tokentype_eof)
665 fromtext_warneof(lexer, callbacks);
670 length = isc_buffer_usedlength(target) - isc_buffer_usedlength(&st);
671 if (result == ISC_R_SUCCESS && length > DNS_RDATA_MAXLENGTH)
672 result = ISC_R_NOSPACE;
674 if (rdata != NULL && result == ISC_R_SUCCESS) {
675 region.base = isc_buffer_used(&st);
676 region.length = length;
677 dns_rdata_fromregion(rdata, rdclass, type, ®ion);
679 if (result != ISC_R_SUCCESS) {
686 unknown_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
687 isc_buffer_t *target)
690 char buf[sizeof("65535")];
693 strlcpy(buf, "\\# ", sizeof(buf));
694 result = str_totext(buf, target);
695 if (result != ISC_R_SUCCESS)
698 dns_rdata_toregion(rdata, &sr);
699 INSIST(sr.length < 65536);
700 snprintf(buf, sizeof(buf), "%u", sr.length);
701 result = str_totext(buf, target);
702 if (result != ISC_R_SUCCESS)
705 if (sr.length != 0U) {
706 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
707 result = str_totext(" ( ", target);
709 result = str_totext(" ", target);
711 if (result != ISC_R_SUCCESS)
714 if (tctx->width == 0) /* No splitting */
715 result = isc_hex_totext(&sr, 0, "", target);
717 result = isc_hex_totext(&sr, tctx->width - 2,
720 if (result == ISC_R_SUCCESS &&
721 (tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
722 result = str_totext(" )", target);
728 rdata_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
729 isc_buffer_t *target)
731 isc_result_t result = ISC_R_NOTIMPLEMENTED;
732 isc_boolean_t use_default = ISC_FALSE;
734 REQUIRE(rdata != NULL);
735 REQUIRE(tctx->origin == NULL ||
736 dns_name_isabsolute(tctx->origin) == ISC_TRUE);
739 * Some DynDNS meta-RRs have empty rdata.
741 if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
742 INSIST(rdata->length == 0);
743 return (ISC_R_SUCCESS);
749 result = unknown_totext(rdata, tctx, target);
755 dns_rdata_totext(dns_rdata_t *rdata, dns_name_t *origin, isc_buffer_t *target)
757 dns_rdata_textctx_t tctx;
759 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
762 * Set up formatting options for single-line output.
764 tctx.origin = origin;
767 tctx.linebreak = " ";
768 return (rdata_totext(rdata, &tctx, target));
772 dns_rdata_tofmttext(dns_rdata_t *rdata, dns_name_t *origin,
773 unsigned int flags, unsigned int width,
774 const char *linebreak, isc_buffer_t *target)
776 dns_rdata_textctx_t tctx;
778 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
781 * Set up formatting options for formatted output.
783 tctx.origin = origin;
785 if ((flags & DNS_STYLEFLAG_MULTILINE) != 0) {
787 tctx.linebreak = linebreak;
789 tctx.width = 60; /* Used for hex word length only. */
790 tctx.linebreak = " ";
792 return (rdata_totext(rdata, &tctx, target));
796 dns_rdata_fromstruct(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
797 dns_rdatatype_t type, void *source,
798 isc_buffer_t *target)
800 isc_result_t result = ISC_R_NOTIMPLEMENTED;
803 isc_boolean_t use_default = ISC_FALSE;
806 REQUIRE(source != NULL);
808 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
809 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
819 length = isc_buffer_usedlength(target) - isc_buffer_usedlength(&st);
820 if (result == ISC_R_SUCCESS && length > DNS_RDATA_MAXLENGTH)
821 result = ISC_R_NOSPACE;
823 if (rdata != NULL && result == ISC_R_SUCCESS) {
824 region.base = isc_buffer_used(&st);
825 region.length = length;
826 dns_rdata_fromregion(rdata, rdclass, type, ®ion);
828 if (result != ISC_R_SUCCESS)
834 dns_rdata_tostruct(dns_rdata_t *rdata, void *target, isc_mem_t *mctx) {
835 isc_result_t result = ISC_R_NOTIMPLEMENTED;
836 isc_boolean_t use_default = ISC_FALSE;
838 REQUIRE(rdata != NULL);
839 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
850 dns_rdata_freestruct(void *source) {
851 dns_rdatacommon_t *common = source;
852 REQUIRE(source != NULL);
858 dns_rdata_additionaldata(dns_rdata_t *rdata, dns_additionaldatafunc_t add,
861 isc_result_t result = ISC_R_NOTIMPLEMENTED;
862 isc_boolean_t use_default = ISC_FALSE;
865 * Call 'add' for each name and type from 'rdata' which is subject to
866 * additional section processing.
869 REQUIRE(rdata != NULL);
870 REQUIRE(add != NULL);
871 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
875 /* No additional processing for unknown types */
877 result = ISC_R_SUCCESS;
883 dns_rdata_digest(dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg) {
884 isc_result_t result = ISC_R_NOTIMPLEMENTED;
885 isc_boolean_t use_default = ISC_FALSE;
889 * Send 'rdata' in DNSSEC canonical form to 'digest'.
892 REQUIRE(rdata != NULL);
893 REQUIRE(digest != NULL);
894 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
899 dns_rdata_toregion(rdata, &r);
900 result = (digest)(arg, &r);
907 dns_rdata_checkowner(dns_name_t *name, dns_rdataclass_t rdclass,
908 dns_rdatatype_t type, isc_boolean_t wildcard)
910 isc_boolean_t result;
917 dns_rdata_checknames(dns_rdata_t *rdata, dns_name_t *owner, dns_name_t *bad)
919 isc_boolean_t result;
926 dns_rdatatype_attributes(dns_rdatatype_t type)
928 RDATATYPE_ATTRIBUTE_SW
929 if (type >= (dns_rdatatype_t)128 && type < (dns_rdatatype_t)255)
930 return (DNS_RDATATYPEATTR_UNKNOWN | DNS_RDATATYPEATTR_META);
931 return (DNS_RDATATYPEATTR_UNKNOWN);
935 dns_rdatatype_fromtext(dns_rdatatype_t *typep, isc_textregion_t *source) {
943 return (DNS_R_UNKNOWN);
945 a = tolower((unsigned char)source->base[0]);
946 b = tolower((unsigned char)source->base[n - 1]);
948 hash = ((a + n) * b) % 256;
951 * This switch block is inlined via \#define, and will use "return"
952 * to return a result to the caller if it is a valid (known)
955 RDATATYPE_FROMTEXT_SW(hash, source->base, n, typep);
957 if (source->length > 4 && source->length < (4 + sizeof("65000")) &&
958 strncasecmp("type", source->base, 4) == 0) {
959 char buf[sizeof("65000")];
963 strncpy(buf, source->base + 4, source->length - 4);
964 buf[source->length - 4] = '\0';
965 val = strtoul(buf, &endp, 10);
966 if (*endp == '\0' && val <= 0xffff) {
967 *typep = (dns_rdatatype_t)val;
968 return (ISC_R_SUCCESS);
972 return (DNS_R_UNKNOWN);
976 dns_rdatatype_totext(dns_rdatatype_t type, isc_buffer_t *target) {
977 char buf[sizeof("TYPE65535")];
980 snprintf(buf, sizeof(buf), "TYPE%u", type);
981 return (str_totext(buf, target));
985 dns_rdatatype_format(dns_rdatatype_t rdtype,
986 char *array, unsigned int size)
994 isc_buffer_init(&buf, array, size);
995 result = dns_rdatatype_totext(rdtype, &buf);
999 if (result == ISC_R_SUCCESS) {
1000 if (isc_buffer_availablelength(&buf) >= 1)
1001 isc_buffer_putuint8(&buf, 0);
1003 result = ISC_R_NOSPACE;
1005 if (result != ISC_R_SUCCESS)
1006 strlcpy(array, "<unknown>", size);
1014 name_length(dns_name_t *name) {
1015 return (name->length);
1019 txt_totext(isc_region_t *source, isc_buffer_t *target) {
1024 isc_region_t region;
1026 isc_buffer_availableregion(target, ®ion);
1028 tp = (char *)region.base;
1033 REQUIRE(n + 1 <= source->length);
1036 return (ISC_R_NOSPACE);
1040 if (*sp < 0x20 || *sp >= 0x7f) {
1042 return (ISC_R_NOSPACE);
1044 *tp++ = 0x30 + ((*sp / 100) % 10);
1045 *tp++ = 0x30 + ((*sp / 10) % 10);
1046 *tp++ = 0x30 + (*sp % 10);
1051 /* double quote, semi-colon, backslash */
1052 if (*sp == 0x22 || *sp == 0x3b || *sp == 0x5c) {
1054 return (ISC_R_NOSPACE);
1059 return (ISC_R_NOSPACE);
1064 return (ISC_R_NOSPACE);
1067 isc_buffer_add(target, tp - (char *)region.base);
1068 isc_region_consume(source, *source->base + 1);
1069 return (ISC_R_SUCCESS);
1073 txt_fromtext(isc_textregion_t *source, isc_buffer_t *target) {
1074 isc_region_t tregion;
1075 isc_boolean_t escape;
1076 unsigned int n, nrem;
1082 isc_buffer_availableregion(target, &tregion);
1086 nrem = tregion.length;
1089 return (ISC_R_NOSPACE);
1096 * Maximum text string length.
1102 if (escape && (d = decvalue((char)c)) != -1) {
1105 return (DNS_R_SYNTAX);
1107 if ((d = decvalue(*s++)) != -1)
1110 return (DNS_R_SYNTAX);
1112 return (DNS_R_SYNTAX);
1114 if ((d = decvalue(*s++)) != -1)
1117 return (DNS_R_SYNTAX);
1119 return (DNS_R_SYNTAX);
1120 } else if (!escape && c == '\\') {
1126 return ((tregion.length <= 256U) ?
1127 ISC_R_NOSPACE : DNS_R_SYNTAX);
1132 return (DNS_R_SYNTAX);
1133 *tregion.base = t - tregion.base - 1;
1134 isc_buffer_add(target, *tregion.base + 1);
1135 return (ISC_R_SUCCESS);
1139 txt_fromwire(isc_buffer_t *source, isc_buffer_t *target) {
1141 isc_region_t sregion;
1142 isc_region_t tregion;
1144 isc_buffer_activeregion(source, &sregion);
1145 if (sregion.length == 0)
1146 return(ISC_R_UNEXPECTEDEND);
1147 n = *sregion.base + 1;
1148 if (n > sregion.length)
1149 return (ISC_R_UNEXPECTEDEND);
1151 isc_buffer_availableregion(target, &tregion);
1152 if (n > tregion.length)
1153 return (ISC_R_NOSPACE);
1155 memcpy(tregion.base, sregion.base, n);
1156 isc_buffer_forward(source, n);
1157 isc_buffer_add(target, n);
1158 return (ISC_R_SUCCESS);
1161 static isc_boolean_t
1162 name_prefix(dns_name_t *name, dns_name_t *origin, dns_name_t *target) {
1168 if (dns_name_compare(origin, dns_rootname) == 0)
1171 if (!dns_name_issubdomain(name, origin))
1174 l1 = dns_name_countlabels(name);
1175 l2 = dns_name_countlabels(origin);
1180 /* Master files should be case preserving. */
1181 dns_name_getlabelsequence(name, l1 - l2, l2, target);
1182 if (!dns_name_caseequal(origin, target))
1185 dns_name_getlabelsequence(name, 0, l1 - l2, target);
1194 str_totext(const char *source, isc_buffer_t *target) {
1196 isc_region_t region;
1198 isc_buffer_availableregion(target, ®ion);
1201 if (l > region.length)
1202 return (ISC_R_NOSPACE);
1204 memcpy(region.base, source, l);
1205 isc_buffer_add(target, l);
1206 return (ISC_R_SUCCESS);
1210 inet_totext(int af, isc_region_t *src, isc_buffer_t *target) {
1213 /* Note - inet_ntop doesn't do size checking on its input. */
1214 if (inet_ntop(af, src->base, tmpbuf, sizeof(tmpbuf)) == NULL)
1215 return (ISC_R_NOSPACE);
1216 if (strlen(tmpbuf) > isc_buffer_availablelength(target))
1217 return (ISC_R_NOSPACE);
1218 isc_buffer_putstr(target, tmpbuf);
1219 return (ISC_R_SUCCESS);
1222 static isc_boolean_t
1223 buffer_empty(isc_buffer_t *source) {
1224 return((source->current == source->active) ? ISC_TRUE : ISC_FALSE);
1228 buffer_fromregion(isc_buffer_t *buffer, isc_region_t *region) {
1229 isc_buffer_init(buffer, region->base, region->length);
1230 isc_buffer_add(buffer, region->length);
1231 isc_buffer_setactive(buffer, region->length);
1235 uint32_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1236 isc_region_t region;
1238 isc_buffer_availableregion(target, ®ion);
1239 if (region.length < 4)
1240 return (ISC_R_NOSPACE);
1241 isc_buffer_putuint32(target, value);
1242 return (ISC_R_SUCCESS);
1246 uint16_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1247 isc_region_t region;
1250 return (ISC_R_RANGE);
1251 isc_buffer_availableregion(target, ®ion);
1252 if (region.length < 2)
1253 return (ISC_R_NOSPACE);
1254 isc_buffer_putuint16(target, (isc_uint16_t)value);
1255 return (ISC_R_SUCCESS);
1259 uint8_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1260 isc_region_t region;
1263 return (ISC_R_RANGE);
1264 isc_buffer_availableregion(target, ®ion);
1265 if (region.length < 1)
1266 return (ISC_R_NOSPACE);
1267 isc_buffer_putuint8(target, (isc_uint8_t)value);
1268 return (ISC_R_SUCCESS);
1272 name_tobuffer(dns_name_t *name, isc_buffer_t *target) {
1274 dns_name_toregion(name, &r);
1275 return (isc_buffer_copyregion(target, &r));
1279 uint32_fromregion(isc_region_t *region) {
1282 REQUIRE(region->length >= 4);
1283 value = region->base[0] << 24;
1284 value |= region->base[1] << 16;
1285 value |= region->base[2] << 8;
1286 value |= region->base[3];
1291 uint16_consume_fromregion(isc_region_t *region) {
1292 isc_uint16_t r = uint16_fromregion(region);
1294 isc_region_consume(region, 2);
1299 uint16_fromregion(isc_region_t *region) {
1301 REQUIRE(region->length >= 2);
1303 return ((region->base[0] << 8) | region->base[1]);
1307 uint8_fromregion(isc_region_t *region) {
1309 REQUIRE(region->length >= 1);
1311 return (region->base[0]);
1315 uint8_consume_fromregion(isc_region_t *region) {
1316 isc_uint8_t r = uint8_fromregion(region);
1318 isc_region_consume(region, 1);
1323 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
1326 isc_buffer_availableregion(target, &tr);
1327 if (length > tr.length)
1328 return (ISC_R_NOSPACE);
1329 memcpy(tr.base, base, length);
1330 isc_buffer_add(target, length);
1331 return (ISC_R_SUCCESS);
1335 hexvalue(char value) {
1339 c = (unsigned char)value;
1345 if ((s = strchr(hexdigits, c)) == NULL)
1347 return (s - hexdigits);
1351 decvalue(char value) {
1355 * isascii() is valid for full range of int values, no need to
1358 if (!isascii(value))
1360 if ((s = strchr(decdigits, value)) == NULL)
1362 return (s - decdigits);
1365 static const char atob_digits[86] =
1366 "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`" \
1367 "abcdefghijklmnopqrstu";
1369 * Subroutines to convert between 8 bit binary bytes and printable ASCII.
1370 * Computes the number of bytes, and three kinds of simple checksums.
1371 * Incoming bytes are collected into 32-bit words, then printed in base 85:
1372 * exp(85,5) > exp(2,32)
1373 * The ASCII characters used are between '!' and 'u';
1374 * 'z' encodes 32-bit zero; 'x' is used to mark the end of encoded data.
1376 * Originally by Paul Rutter (philabs!per) and Joe Orost (petsd!joe) for
1377 * the atob/btoa programs, released with the compress program, in mod.sources.
1378 * Modified by Mike Schwartz 8/19/86 for use in BIND.
1379 * Modified to be re-entrant 3/2/99.
1391 #define Ceor state->Ceor
1392 #define Csum state->Csum
1393 #define Crot state->Crot
1394 #define word state->word
1395 #define bcount state->bcount
1397 #define times85(x) ((((((x<<2)+x)<<2)+x)<<2)+x)
1399 static isc_result_t byte_atob(int c, isc_buffer_t *target,
1400 struct state *state);
1401 static isc_result_t putbyte(int c, isc_buffer_t *, struct state *state);
1402 static isc_result_t byte_btoa(int c, isc_buffer_t *, struct state *state);
1405 * Decode ASCII-encoded byte c into binary representation and
1406 * place into *bufp, advancing bufp.
1409 byte_atob(int c, isc_buffer_t *target, struct state *state) {
1413 return(DNS_R_SYNTAX);
1415 RETERR(putbyte(0, target, state));
1416 RETERR(putbyte(0, target, state));
1417 RETERR(putbyte(0, target, state));
1418 RETERR(putbyte(0, target, state));
1420 } else if ((s = strchr(atob_digits, c)) != NULL) {
1422 word = s - atob_digits;
1424 } else if (bcount < 4) {
1425 word = times85(word);
1426 word += s - atob_digits;
1429 word = times85(word);
1430 word += s - atob_digits;
1431 RETERR(putbyte((word >> 24) & 0xff, target, state));
1432 RETERR(putbyte((word >> 16) & 0xff, target, state));
1433 RETERR(putbyte((word >> 8) & 0xff, target, state));
1434 RETERR(putbyte(word & 0xff, target, state));
1439 return(DNS_R_SYNTAX);
1440 return(ISC_R_SUCCESS);
1444 * Compute checksum info and place c into target.
1447 putbyte(int c, isc_buffer_t *target, struct state *state) {
1453 if ((Crot & 0x80000000)) {
1460 isc_buffer_availableregion(target, &tr);
1462 return (ISC_R_NOSPACE);
1464 isc_buffer_add(target, 1);
1465 return (ISC_R_SUCCESS);
1469 * Read the ASCII-encoded data from inbuf, of length inbuflen, and convert
1470 * it into T_UNSPEC (binary data) in outbuf, not to exceed outbuflen bytes;
1471 * outbuflen must be divisible by 4. (Note: this is because outbuf is filled
1472 * in 4 bytes at a time. If the actual data doesn't end on an even 4-byte
1473 * boundary, there will be no problem...it will be padded with 0 bytes, and
1474 * numbytes will indicate the correct number of bytes. The main point is
1475 * that since the buffer is filled in 4 bytes at a time, even if there is
1476 * not a full 4 bytes of data at the end, there has to be room to 0-pad the
1477 * data, so the buffer must be of size divisible by 4). Place the number of
1478 * output bytes in numbytes, and return a failure/success status.
1482 atob_tobuffer(isc_lex_t *lexer, isc_buffer_t *target) {
1483 long oeor, osum, orot;
1484 struct state statebuf, *state= &statebuf;
1489 Ceor = Csum = Crot = word = bcount = 0;
1491 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1493 while (token.value.as_textregion.length != 0) {
1494 if ((c = token.value.as_textregion.base[0]) == 'x') {
1497 RETERR(byte_atob(c, target, state));
1498 isc_textregion_consume(&token.value.as_textregion, 1);
1504 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
1506 if ((token.value.as_ulong % 4) != 0U)
1507 isc_buffer_subtract(target, 4 - (token.value.as_ulong % 4));
1512 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1514 oeor = strtol(DNS_AS_STR(token), &e, 16);
1516 return (DNS_R_SYNTAX);
1521 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1523 osum = strtol(DNS_AS_STR(token), &e, 16);
1525 return (DNS_R_SYNTAX);
1530 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1532 orot = strtol(DNS_AS_STR(token), &e, 16);
1534 return (DNS_R_SYNTAX);
1536 if ((oeor != Ceor) || (osum != Csum) || (orot != Crot))
1537 return(DNS_R_BADCKSUM);
1538 return (ISC_R_SUCCESS);
1542 * Encode binary byte c into ASCII representation and place into *bufp,
1546 byte_btoa(int c, isc_buffer_t *target, struct state *state) {
1549 isc_buffer_availableregion(target, &tr);
1553 if ((Crot & 0x80000000)) {
1566 return (ISC_R_NOSPACE);
1568 isc_buffer_add(target, 1);
1570 register int tmp = 0;
1571 register isc_int32_t tmpword = word;
1575 * Because some don't support u_long.
1578 tmpword -= (isc_int32_t)(85 * 85 * 85 * 85 * 32);
1582 tmpword -= (isc_int32_t)(85 * 85 * 85 * 85 * 32);
1585 return (ISC_R_NOSPACE);
1586 tr.base[0] = atob_digits[(tmpword /
1587 (isc_int32_t)(85 * 85 * 85 * 85))
1589 tmpword %= (isc_int32_t)(85 * 85 * 85 * 85);
1590 tr.base[1] = atob_digits[tmpword / (85 * 85 * 85)];
1591 tmpword %= (85 * 85 * 85);
1592 tr.base[2] = atob_digits[tmpword / (85 * 85)];
1593 tmpword %= (85 * 85);
1594 tr.base[3] = atob_digits[tmpword / 85];
1596 tr.base[4] = atob_digits[tmpword];
1597 isc_buffer_add(target, 5);
1603 return (ISC_R_SUCCESS);
1608 * Encode the binary data from inbuf, of length inbuflen, into a
1609 * target. Return success/failure status
1612 btoa_totext(unsigned char *inbuf, int inbuflen, isc_buffer_t *target) {
1614 struct state statebuf, *state = &statebuf;
1615 char buf[sizeof("x 2000000000 ffffffff ffffffff ffffffff")];
1617 Ceor = Csum = Crot = word = bcount = 0;
1618 for (inc = 0; inc < inbuflen; inbuf++, inc++)
1619 RETERR(byte_btoa(*inbuf, target, state));
1622 RETERR(byte_btoa(0, target, state));
1625 * Put byte count and checksum information at end of buffer,
1628 snprintf(buf, sizeof(buf), "x %d %x %x %x", inbuflen, Ceor, Csum, Crot);
1629 return (str_totext(buf, target));
1634 default_fromtext_callback(dns_rdatacallbacks_t *callbacks, const char *fmt,
1642 vfprintf(stderr, fmt, ap);
1644 fprintf(stderr, "\n");
1648 fromtext_warneof(isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks) {
1649 if (isc_lex_isfile(lexer) && callbacks != NULL) {
1650 const char *name = isc_lex_getsourcename(lexer);
1653 (*callbacks->warn)(callbacks,
1654 "%s:%lu: file does not end with newline",
1655 name, isc_lex_getsourceline(lexer));
1660 warn_badmx(isc_token_t *token, isc_lex_t *lexer,
1661 dns_rdatacallbacks_t *callbacks)
1666 if (lexer != NULL) {
1667 file = isc_lex_getsourcename(lexer);
1668 line = isc_lex_getsourceline(lexer);
1669 (*callbacks->warn)(callbacks, "%s:%u: warning: '%s': %s",
1670 file, line, DNS_AS_STR(*token),
1671 dns_result_totext(DNS_R_MXISADDRESS));
1676 warn_badname(dns_name_t *name, isc_lex_t *lexer,
1677 dns_rdatacallbacks_t *callbacks)
1681 char namebuf[DNS_NAME_FORMATSIZE];
1683 if (lexer != NULL) {
1684 file = isc_lex_getsourcename(lexer);
1685 line = isc_lex_getsourceline(lexer);
1686 dns_name_format(name, namebuf, sizeof(namebuf));
1687 (*callbacks->warn)(callbacks, "%s:%u: warning: %s: %s",
1688 file, line, namebuf,
1689 dns_result_totext(DNS_R_BADNAME));
1694 fromtext_error(void (*callback)(dns_rdatacallbacks_t *, const char *, ...),
1695 dns_rdatacallbacks_t *callbacks, const char *name,
1696 unsigned long line, isc_token_t *token, isc_result_t result)
1701 if (token != NULL) {
1702 switch (token->type) {
1703 case isc_tokentype_eol:
1704 (*callback)(callbacks, "%s: %s:%lu: near eol: %s",
1705 "dns_rdata_fromtext", name, line,
1706 dns_result_totext(result));
1708 case isc_tokentype_eof:
1709 (*callback)(callbacks, "%s: %s:%lu: near eof: %s",
1710 "dns_rdata_fromtext", name, line,
1711 dns_result_totext(result));
1713 case isc_tokentype_number:
1714 (*callback)(callbacks, "%s: %s:%lu: near %lu: %s",
1715 "dns_rdata_fromtext", name, line,
1716 token->value.as_ulong,
1717 dns_result_totext(result));
1719 case isc_tokentype_string:
1720 case isc_tokentype_qstring:
1721 (*callback)(callbacks, "%s: %s:%lu: near '%s': %s",
1722 "dns_rdata_fromtext", name, line,
1724 dns_result_totext(result));
1727 (*callback)(callbacks, "%s: %s:%lu: %s",
1728 "dns_rdata_fromtext", name, line,
1729 dns_result_totext(result));
1733 (*callback)(callbacks, "dns_rdata_fromtext: %s:%lu: %s",
1734 name, line, dns_result_totext(result));
1739 dns_rdata_covers(dns_rdata_t *rdata) {
1740 if (rdata->type == 46)
1741 return (covers_rrsig(rdata));
1742 return (covers_sig(rdata));
1746 dns_rdatatype_ismeta(dns_rdatatype_t type) {
1747 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_META) != 0)
1753 dns_rdatatype_issingleton(dns_rdatatype_t type) {
1754 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_SINGLETON)
1761 dns_rdatatype_notquestion(dns_rdatatype_t type) {
1762 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_NOTQUESTION)
1769 dns_rdatatype_questiononly(dns_rdatatype_t type) {
1770 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_QUESTIONONLY)
1777 dns_rdatatype_atparent(dns_rdatatype_t type) {
1778 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_ATPARENT) != 0)
1784 dns_rdataclass_ismeta(dns_rdataclass_t rdclass) {
1786 if (rdclass == dns_rdataclass_reserved0
1787 || rdclass == dns_rdataclass_none
1788 || rdclass == dns_rdataclass_any)
1791 return (ISC_FALSE); /* Assume it is not a meta class. */
1795 dns_rdatatype_isdnssec(dns_rdatatype_t type) {
1796 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_DNSSEC) != 0)
1802 dns_rdatatype_iszonecutauth(dns_rdatatype_t type) {
1803 if ((dns_rdatatype_attributes(type)
1804 & (DNS_RDATATYPEATTR_DNSSEC | DNS_RDATATYPEATTR_ZONECUTAUTH))
1811 dns_rdatatype_isknown(dns_rdatatype_t type) {
1812 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_UNKNOWN)