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.199.50.2 2009/02/16 23:47:15 tbox 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/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 getquad(const void *src, struct in_addr *dst,
212 isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks)
217 result = inet_aton(src, dst);
218 if (result == 1 && callbacks != NULL &&
219 inet_pton(AF_INET, src, &tmp) != 1) {
220 const char *name = isc_lex_getsourcename(lexer);
223 (*callbacks->warn)(callbacks, "%s:%lu: \"%s\" "
224 "is not a decimal dotted quad", name,
225 isc_lex_getsourceline(lexer), src);
230 static inline isc_result_t
231 name_duporclone(dns_name_t *source, isc_mem_t *mctx, dns_name_t *target) {
234 return (dns_name_dup(source, mctx, target));
235 dns_name_clone(source, target);
236 return (ISC_R_SUCCESS);
240 mem_maybedup(isc_mem_t *mctx, void *source, size_t length) {
245 new = isc_mem_allocate(mctx, length);
247 memcpy(new, source, length);
252 static const char hexdigits[] = "0123456789abcdef";
253 static const char decdigits[] = "0123456789";
258 #define RESERVED 0x0002
265 dns_rdata_init(dns_rdata_t *rdata) {
267 REQUIRE(rdata != NULL);
274 ISC_LINK_INIT(rdata, link);
275 /* ISC_LIST_INIT(rdata->list); */
279 #define DNS_RDATA_INITIALIZED(rdata) \
280 ((rdata)->data == NULL && (rdata)->length == 0 && \
281 (rdata)->rdclass == 0 && (rdata)->type == 0 && (rdata)->flags == 0 && \
282 !ISC_LINK_LINKED((rdata), link))
284 #ifdef ISC_LIST_CHECKINIT
285 #define DNS_RDATA_INITIALIZED(rdata) \
286 (!ISC_LINK_LINKED((rdata), link))
288 #define DNS_RDATA_INITIALIZED(rdata) ISC_TRUE
292 #define DNS_RDATA_VALIDFLAGS(rdata) \
293 (((rdata)->flags & ~(DNS_RDATA_UPDATE|DNS_RDATA_OFFLINE)) == 0)
296 dns_rdata_reset(dns_rdata_t *rdata) {
298 REQUIRE(rdata != NULL);
300 REQUIRE(!ISC_LINK_LINKED(rdata, link));
301 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
315 dns_rdata_clone(const dns_rdata_t *src, dns_rdata_t *target) {
317 REQUIRE(src != NULL);
318 REQUIRE(target != NULL);
320 REQUIRE(DNS_RDATA_INITIALIZED(target));
322 REQUIRE(DNS_RDATA_VALIDFLAGS(src));
323 REQUIRE(DNS_RDATA_VALIDFLAGS(target));
325 target->data = src->data;
326 target->length = src->length;
327 target->rdclass = src->rdclass;
328 target->type = src->type;
329 target->flags = src->flags;
338 dns_rdata_compare(const dns_rdata_t *rdata1, const dns_rdata_t *rdata2) {
340 isc_boolean_t use_default = ISC_FALSE;
342 REQUIRE(rdata1 != NULL);
343 REQUIRE(rdata2 != NULL);
344 REQUIRE(rdata1->data != NULL);
345 REQUIRE(rdata2->data != NULL);
346 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata1));
347 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata2));
349 if (rdata1->rdclass != rdata2->rdclass)
350 return (rdata1->rdclass < rdata2->rdclass ? -1 : 1);
352 if (rdata1->type != rdata2->type)
353 return (rdata1->type < rdata2->type ? -1 : 1);
361 dns_rdata_toregion(rdata1, &r1);
362 dns_rdata_toregion(rdata2, &r2);
363 result = isc_region_compare(&r1, &r2);
373 dns_rdata_fromregion(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
374 dns_rdatatype_t type, isc_region_t *r)
377 REQUIRE(rdata != NULL);
378 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
381 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
383 rdata->data = r->base;
384 rdata->length = r->length;
385 rdata->rdclass = rdclass;
391 dns_rdata_toregion(const dns_rdata_t *rdata, isc_region_t *r) {
393 REQUIRE(rdata != NULL);
395 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
397 r->base = rdata->data;
398 r->length = rdata->length;
402 dns_rdata_fromwire(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
403 dns_rdatatype_t type, isc_buffer_t *source,
404 dns_decompress_t *dctx, unsigned int options,
405 isc_buffer_t *target)
407 isc_result_t result = ISC_R_NOTIMPLEMENTED;
411 isc_boolean_t use_default = ISC_FALSE;
412 isc_uint32_t activelength;
414 REQUIRE(dctx != NULL);
416 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
417 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
421 return (DNS_R_FORMERR);
426 activelength = isc_buffer_activelength(source);
427 INSIST(activelength < 65536);
432 if (activelength > isc_buffer_availablelength(target))
433 result = ISC_R_NOSPACE;
435 isc_buffer_putmem(target, isc_buffer_current(source),
437 isc_buffer_forward(source, activelength);
438 result = ISC_R_SUCCESS;
443 * We should have consumed all of our buffer.
445 if (result == ISC_R_SUCCESS && !buffer_empty(source))
446 result = DNS_R_EXTRADATA;
448 if (rdata != NULL && result == ISC_R_SUCCESS) {
449 region.base = isc_buffer_used(&st);
450 region.length = isc_buffer_usedlength(target) -
451 isc_buffer_usedlength(&st);
452 dns_rdata_fromregion(rdata, rdclass, type, ®ion);
455 if (result != ISC_R_SUCCESS) {
463 dns_rdata_towire(dns_rdata_t *rdata, dns_compress_t *cctx,
464 isc_buffer_t *target)
466 isc_result_t result = ISC_R_NOTIMPLEMENTED;
467 isc_boolean_t use_default = ISC_FALSE;
471 REQUIRE(rdata != NULL);
472 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
475 * Some DynDNS meta-RRs have empty rdata.
477 if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
478 INSIST(rdata->length == 0);
479 return (ISC_R_SUCCESS);
487 isc_buffer_availableregion(target, &tr);
488 if (tr.length < rdata->length)
489 return (ISC_R_NOSPACE);
490 memcpy(tr.base, rdata->data, rdata->length);
491 isc_buffer_add(target, rdata->length);
492 return (ISC_R_SUCCESS);
494 if (result != ISC_R_SUCCESS) {
496 INSIST(target->used < 65536);
497 dns_compress_rollback(cctx, (isc_uint16_t)target->used);
503 * If the binary data in 'src' is valid uncompressed wire format
504 * rdata of class 'rdclass' and type 'type', return ISC_R_SUCCESS
505 * and copy the validated rdata to 'dest'. Otherwise return an error.
508 rdata_validate(isc_buffer_t *src, isc_buffer_t *dest, dns_rdataclass_t rdclass,
509 dns_rdatatype_t type)
511 dns_decompress_t dctx;
512 dns_rdata_t rdata = DNS_RDATA_INIT;
515 dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_NONE);
516 isc_buffer_setactive(src, isc_buffer_usedlength(src));
517 result = dns_rdata_fromwire(&rdata, rdclass, type, src,
519 dns_decompress_invalidate(&dctx);
525 unknown_fromtext(dns_rdataclass_t rdclass, dns_rdatatype_t type,
526 isc_lex_t *lexer, isc_mem_t *mctx, isc_buffer_t *target)
529 isc_buffer_t *buf = NULL;
532 if (type == 0 || dns_rdatatype_ismeta(type))
533 return (DNS_R_METATYPE);
535 result = isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
537 if (result == ISC_R_SUCCESS && token.value.as_ulong > 65535U)
538 return (ISC_R_RANGE);
539 result = isc_buffer_allocate(mctx, &buf, token.value.as_ulong);
540 if (result != ISC_R_SUCCESS)
543 result = isc_hex_tobuffer(lexer, buf,
544 (unsigned int)token.value.as_ulong);
545 if (result != ISC_R_SUCCESS)
547 if (isc_buffer_usedlength(buf) != token.value.as_ulong) {
548 result = ISC_R_UNEXPECTEDEND;
552 if (dns_rdatatype_isknown(type)) {
553 result = rdata_validate(buf, target, rdclass, type);
556 isc_buffer_usedregion(buf, &r);
557 result = isc_buffer_copyregion(target, &r);
559 if (result != ISC_R_SUCCESS)
562 isc_buffer_free(&buf);
563 return (ISC_R_SUCCESS);
566 isc_buffer_free(&buf);
571 dns_rdata_fromtext(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
572 dns_rdatatype_t type, isc_lex_t *lexer,
573 dns_name_t *origin, unsigned int options, isc_mem_t *mctx,
574 isc_buffer_t *target, dns_rdatacallbacks_t *callbacks)
576 isc_result_t result = ISC_R_NOTIMPLEMENTED;
580 unsigned int lexoptions = ISC_LEXOPT_EOL | ISC_LEXOPT_EOF |
581 ISC_LEXOPT_DNSMULTILINE | ISC_LEXOPT_ESCAPE;
584 void (*callback)(dns_rdatacallbacks_t *, const char *, ...);
585 isc_result_t tresult;
587 REQUIRE(origin == NULL || dns_name_isabsolute(origin) == ISC_TRUE);
589 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
590 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
592 if (callbacks != NULL) {
593 REQUIRE(callbacks->warn != NULL);
594 REQUIRE(callbacks->error != NULL);
599 if (callbacks != NULL)
600 callback = callbacks->error;
602 callback = default_fromtext_callback;
604 result = isc_lex_getmastertoken(lexer, &token, isc_tokentype_qstring,
606 if (result != ISC_R_SUCCESS) {
607 name = isc_lex_getsourcename(lexer);
608 line = isc_lex_getsourceline(lexer);
609 fromtext_error(callback, callbacks, name, line,
614 if (strcmp(DNS_AS_STR(token), "\\#") == 0)
615 result = unknown_fromtext(rdclass, type, lexer, mctx, target);
617 isc_lex_ungettoken(lexer, &token);
623 * Consume to end of line / file.
624 * If not at end of line initially set error code.
625 * Call callback via fromtext_error once if there was an error.
628 name = isc_lex_getsourcename(lexer);
629 line = isc_lex_getsourceline(lexer);
630 tresult = isc_lex_gettoken(lexer, lexoptions, &token);
631 if (tresult != ISC_R_SUCCESS) {
632 if (result == ISC_R_SUCCESS)
634 if (callback != NULL)
635 fromtext_error(callback, callbacks, name,
638 } else if (token.type != isc_tokentype_eol &&
639 token.type != isc_tokentype_eof) {
640 if (result == ISC_R_SUCCESS)
641 result = DNS_R_EXTRATOKEN;
642 if (callback != NULL) {
643 fromtext_error(callback, callbacks, name,
644 line, &token, result);
647 } else if (result != ISC_R_SUCCESS && callback != NULL) {
648 fromtext_error(callback, callbacks, name, line,
652 if (token.type == isc_tokentype_eof)
653 fromtext_warneof(lexer, callbacks);
658 if (rdata != NULL && result == ISC_R_SUCCESS) {
659 region.base = isc_buffer_used(&st);
660 region.length = isc_buffer_usedlength(target) -
661 isc_buffer_usedlength(&st);
662 dns_rdata_fromregion(rdata, rdclass, type, ®ion);
664 if (result != ISC_R_SUCCESS) {
671 rdata_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
672 isc_buffer_t *target)
674 isc_result_t result = ISC_R_NOTIMPLEMENTED;
675 isc_boolean_t use_default = ISC_FALSE;
676 char buf[sizeof("65535")];
679 REQUIRE(rdata != NULL);
680 REQUIRE(tctx->origin == NULL ||
681 dns_name_isabsolute(tctx->origin) == ISC_TRUE);
684 * Some DynDNS meta-RRs have empty rdata.
686 if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
687 INSIST(rdata->length == 0);
688 return (ISC_R_SUCCESS);
694 strlcpy(buf, "\\# ", sizeof(buf));
695 result = str_totext(buf, target);
696 dns_rdata_toregion(rdata, &sr);
697 INSIST(sr.length < 65536);
698 snprintf(buf, sizeof(buf), "%u", sr.length);
699 result = str_totext(buf, target);
700 if (sr.length != 0 && result == ISC_R_SUCCESS) {
701 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
702 result = str_totext(" ( ", target);
704 result = str_totext(" ", target);
705 if (result == ISC_R_SUCCESS)
706 result = isc_hex_totext(&sr, tctx->width - 2,
709 if (result == ISC_R_SUCCESS &&
710 (tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
711 result = str_totext(" )", target);
719 dns_rdata_totext(dns_rdata_t *rdata, dns_name_t *origin, isc_buffer_t *target)
721 dns_rdata_textctx_t tctx;
723 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
726 * Set up formatting options for single-line output.
728 tctx.origin = origin;
731 tctx.linebreak = " ";
732 return (rdata_totext(rdata, &tctx, target));
736 dns_rdata_tofmttext(dns_rdata_t *rdata, dns_name_t *origin,
737 unsigned int flags, unsigned int width,
738 const char *linebreak, isc_buffer_t *target)
740 dns_rdata_textctx_t tctx;
742 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
745 * Set up formatting options for formatted output.
747 tctx.origin = origin;
749 if ((flags & DNS_STYLEFLAG_MULTILINE) != 0) {
751 tctx.linebreak = linebreak;
753 tctx.width = 60; /* Used for hex word length only. */
754 tctx.linebreak = " ";
756 return (rdata_totext(rdata, &tctx, target));
760 dns_rdata_fromstruct(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
761 dns_rdatatype_t type, void *source,
762 isc_buffer_t *target)
764 isc_result_t result = ISC_R_NOTIMPLEMENTED;
767 isc_boolean_t use_default = ISC_FALSE;
769 REQUIRE(source != NULL);
771 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
772 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
782 if (rdata != NULL && result == ISC_R_SUCCESS) {
783 region.base = isc_buffer_used(&st);
784 region.length = isc_buffer_usedlength(target) -
785 isc_buffer_usedlength(&st);
786 dns_rdata_fromregion(rdata, rdclass, type, ®ion);
788 if (result != ISC_R_SUCCESS)
794 dns_rdata_tostruct(dns_rdata_t *rdata, void *target, isc_mem_t *mctx) {
795 isc_result_t result = ISC_R_NOTIMPLEMENTED;
796 isc_boolean_t use_default = ISC_FALSE;
798 REQUIRE(rdata != NULL);
799 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
810 dns_rdata_freestruct(void *source) {
811 dns_rdatacommon_t *common = source;
812 REQUIRE(source != NULL);
818 dns_rdata_additionaldata(dns_rdata_t *rdata, dns_additionaldatafunc_t add,
821 isc_result_t result = ISC_R_NOTIMPLEMENTED;
822 isc_boolean_t use_default = ISC_FALSE;
825 * Call 'add' for each name and type from 'rdata' which is subject to
826 * additional section processing.
829 REQUIRE(rdata != NULL);
830 REQUIRE(add != NULL);
831 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
835 /* No additional processing for unknown types */
837 result = ISC_R_SUCCESS;
843 dns_rdata_digest(dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg) {
844 isc_result_t result = ISC_R_NOTIMPLEMENTED;
845 isc_boolean_t use_default = ISC_FALSE;
849 * Send 'rdata' in DNSSEC canonical form to 'digest'.
852 REQUIRE(rdata != NULL);
853 REQUIRE(digest != NULL);
854 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
859 dns_rdata_toregion(rdata, &r);
860 result = (digest)(arg, &r);
867 dns_rdata_checkowner(dns_name_t *name, dns_rdataclass_t rdclass,
868 dns_rdatatype_t type, isc_boolean_t wildcard)
870 isc_boolean_t result;
877 dns_rdata_checknames(dns_rdata_t *rdata, dns_name_t *owner, dns_name_t *bad)
879 isc_boolean_t result;
886 dns_rdatatype_attributes(dns_rdatatype_t type)
888 RDATATYPE_ATTRIBUTE_SW
889 if (type >= (dns_rdatatype_t)128 && type < (dns_rdatatype_t)255)
890 return (DNS_RDATATYPEATTR_UNKNOWN | DNS_RDATATYPEATTR_META);
891 return (DNS_RDATATYPEATTR_UNKNOWN);
895 dns_rdatatype_fromtext(dns_rdatatype_t *typep, isc_textregion_t *source) {
903 return (DNS_R_UNKNOWN);
905 a = tolower((unsigned char)source->base[0]);
906 b = tolower((unsigned char)source->base[n - 1]);
908 hash = ((a + n) * b) % 256;
911 * This switch block is inlined via \#define, and will use "return"
912 * to return a result to the caller if it is a valid (known)
915 RDATATYPE_FROMTEXT_SW(hash, source->base, n, typep);
917 if (source->length > 4 && source->length < (4 + sizeof("65000")) &&
918 strncasecmp("type", source->base, 4) == 0) {
919 char buf[sizeof("65000")];
923 strncpy(buf, source->base + 4, source->length - 4);
924 buf[source->length - 4] = '\0';
925 val = strtoul(buf, &endp, 10);
926 if (*endp == '\0' && val <= 0xffff) {
927 *typep = (dns_rdatatype_t)val;
928 return (ISC_R_SUCCESS);
932 return (DNS_R_UNKNOWN);
936 dns_rdatatype_totext(dns_rdatatype_t type, isc_buffer_t *target) {
937 char buf[sizeof("TYPE65535")];
940 snprintf(buf, sizeof(buf), "TYPE%u", type);
941 return (str_totext(buf, target));
945 dns_rdatatype_format(dns_rdatatype_t rdtype,
946 char *array, unsigned int size)
951 isc_buffer_init(&buf, array, size);
952 result = dns_rdatatype_totext(rdtype, &buf);
956 if (result == ISC_R_SUCCESS) {
957 if (isc_buffer_availablelength(&buf) >= 1)
958 isc_buffer_putuint8(&buf, 0);
960 result = ISC_R_NOSPACE;
962 if (result != ISC_R_SUCCESS) {
963 snprintf(array, size, "<unknown>");
964 array[size - 1] = '\0';
973 name_length(dns_name_t *name) {
974 return (name->length);
978 txt_totext(isc_region_t *source, isc_buffer_t *target) {
985 isc_buffer_availableregion(target, ®ion);
987 tp = (char *)region.base;
992 REQUIRE(n + 1 <= source->length);
995 return (ISC_R_NOSPACE);
999 if (*sp < 0x20 || *sp >= 0x7f) {
1001 return (ISC_R_NOSPACE);
1003 *tp++ = 0x30 + ((*sp / 100) % 10);
1004 *tp++ = 0x30 + ((*sp / 10) % 10);
1005 *tp++ = 0x30 + (*sp % 10);
1010 /* double quote, semi-colon, backslash */
1011 if (*sp == 0x22 || *sp == 0x3b || *sp == 0x5c) {
1013 return (ISC_R_NOSPACE);
1018 return (ISC_R_NOSPACE);
1023 return (ISC_R_NOSPACE);
1026 isc_buffer_add(target, tp - (char *)region.base);
1027 isc_region_consume(source, *source->base + 1);
1028 return (ISC_R_SUCCESS);
1032 txt_fromtext(isc_textregion_t *source, isc_buffer_t *target) {
1033 isc_region_t tregion;
1034 isc_boolean_t escape;
1035 unsigned int n, nrem;
1041 isc_buffer_availableregion(target, &tregion);
1045 nrem = tregion.length;
1048 return (ISC_R_NOSPACE);
1055 * Maximum text string length.
1061 if (escape && (d = decvalue((char)c)) != -1) {
1064 return (DNS_R_SYNTAX);
1066 if ((d = decvalue(*s++)) != -1)
1069 return (DNS_R_SYNTAX);
1071 return (DNS_R_SYNTAX);
1073 if ((d = decvalue(*s++)) != -1)
1076 return (DNS_R_SYNTAX);
1078 return (DNS_R_SYNTAX);
1079 } else if (!escape && c == '\\') {
1085 return (ISC_R_NOSPACE);
1090 return (DNS_R_SYNTAX);
1091 *tregion.base = t - tregion.base - 1;
1092 isc_buffer_add(target, *tregion.base + 1);
1093 return (ISC_R_SUCCESS);
1097 txt_fromwire(isc_buffer_t *source, isc_buffer_t *target) {
1099 isc_region_t sregion;
1100 isc_region_t tregion;
1102 isc_buffer_activeregion(source, &sregion);
1103 if (sregion.length == 0)
1104 return(ISC_R_UNEXPECTEDEND);
1105 n = *sregion.base + 1;
1106 if (n > sregion.length)
1107 return (ISC_R_UNEXPECTEDEND);
1109 isc_buffer_availableregion(target, &tregion);
1110 if (n > tregion.length)
1111 return (ISC_R_NOSPACE);
1113 memcpy(tregion.base, sregion.base, n);
1114 isc_buffer_forward(source, n);
1115 isc_buffer_add(target, n);
1116 return (ISC_R_SUCCESS);
1119 static isc_boolean_t
1120 name_prefix(dns_name_t *name, dns_name_t *origin, dns_name_t *target) {
1126 if (dns_name_compare(origin, dns_rootname) == 0)
1129 if (!dns_name_issubdomain(name, origin))
1132 l1 = dns_name_countlabels(name);
1133 l2 = dns_name_countlabels(origin);
1138 dns_name_getlabelsequence(name, 0, l1 - l2, target);
1147 str_totext(const char *source, isc_buffer_t *target) {
1149 isc_region_t region;
1151 isc_buffer_availableregion(target, ®ion);
1154 if (l > region.length)
1155 return (ISC_R_NOSPACE);
1157 memcpy(region.base, source, l);
1158 isc_buffer_add(target, l);
1159 return (ISC_R_SUCCESS);
1163 inet_totext(int af, isc_region_t *src, isc_buffer_t *target) {
1166 /* Note - inet_ntop doesn't do size checking on its input. */
1167 if (inet_ntop(af, src->base, tmpbuf, sizeof(tmpbuf)) == NULL)
1168 return (ISC_R_NOSPACE);
1169 if (strlen(tmpbuf) > isc_buffer_availablelength(target))
1170 return (ISC_R_NOSPACE);
1171 isc_buffer_putstr(target, tmpbuf);
1172 return (ISC_R_SUCCESS);
1175 static isc_boolean_t
1176 buffer_empty(isc_buffer_t *source) {
1177 return((source->current == source->active) ? ISC_TRUE : ISC_FALSE);
1181 buffer_fromregion(isc_buffer_t *buffer, isc_region_t *region) {
1182 isc_buffer_init(buffer, region->base, region->length);
1183 isc_buffer_add(buffer, region->length);
1184 isc_buffer_setactive(buffer, region->length);
1188 uint32_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1189 isc_region_t region;
1191 isc_buffer_availableregion(target, ®ion);
1192 if (region.length < 4)
1193 return (ISC_R_NOSPACE);
1194 isc_buffer_putuint32(target, value);
1195 return (ISC_R_SUCCESS);
1199 uint16_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1200 isc_region_t region;
1203 return (ISC_R_RANGE);
1204 isc_buffer_availableregion(target, ®ion);
1205 if (region.length < 2)
1206 return (ISC_R_NOSPACE);
1207 isc_buffer_putuint16(target, (isc_uint16_t)value);
1208 return (ISC_R_SUCCESS);
1212 uint8_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1213 isc_region_t region;
1216 return (ISC_R_RANGE);
1217 isc_buffer_availableregion(target, ®ion);
1218 if (region.length < 1)
1219 return (ISC_R_NOSPACE);
1220 isc_buffer_putuint8(target, (isc_uint8_t)value);
1221 return (ISC_R_SUCCESS);
1225 name_tobuffer(dns_name_t *name, isc_buffer_t *target) {
1227 dns_name_toregion(name, &r);
1228 return (isc_buffer_copyregion(target, &r));
1232 uint32_fromregion(isc_region_t *region) {
1235 REQUIRE(region->length >= 4);
1236 value = region->base[0] << 24;
1237 value |= region->base[1] << 16;
1238 value |= region->base[2] << 8;
1239 value |= region->base[3];
1244 uint16_consume_fromregion(isc_region_t *region) {
1245 isc_uint16_t r = uint16_fromregion(region);
1247 isc_region_consume(region, 2);
1252 uint16_fromregion(isc_region_t *region) {
1254 REQUIRE(region->length >= 2);
1256 return ((region->base[0] << 8) | region->base[1]);
1260 uint8_fromregion(isc_region_t *region) {
1262 REQUIRE(region->length >= 1);
1264 return (region->base[0]);
1268 uint8_consume_fromregion(isc_region_t *region) {
1269 isc_uint8_t r = uint8_fromregion(region);
1271 isc_region_consume(region, 1);
1276 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
1279 isc_buffer_availableregion(target, &tr);
1280 if (length > tr.length)
1281 return (ISC_R_NOSPACE);
1282 memcpy(tr.base, base, length);
1283 isc_buffer_add(target, length);
1284 return (ISC_R_SUCCESS);
1288 hexvalue(char value) {
1292 c = (unsigned char)value;
1298 if ((s = strchr(hexdigits, c)) == NULL)
1300 return (s - hexdigits);
1304 decvalue(char value) {
1308 * isascii() is valid for full range of int values, no need to
1311 if (!isascii(value))
1313 if ((s = strchr(decdigits, value)) == NULL)
1315 return (s - decdigits);
1318 static const char atob_digits[86] =
1319 "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`" \
1320 "abcdefghijklmnopqrstu";
1322 * Subroutines to convert between 8 bit binary bytes and printable ASCII.
1323 * Computes the number of bytes, and three kinds of simple checksums.
1324 * Incoming bytes are collected into 32-bit words, then printed in base 85:
1325 * exp(85,5) > exp(2,32)
1326 * The ASCII characters used are between '!' and 'u';
1327 * 'z' encodes 32-bit zero; 'x' is used to mark the end of encoded data.
1329 * Originally by Paul Rutter (philabs!per) and Joe Orost (petsd!joe) for
1330 * the atob/btoa programs, released with the compress program, in mod.sources.
1331 * Modified by Mike Schwartz 8/19/86 for use in BIND.
1332 * Modified to be re-entrant 3/2/99.
1344 #define Ceor state->Ceor
1345 #define Csum state->Csum
1346 #define Crot state->Crot
1347 #define word state->word
1348 #define bcount state->bcount
1350 #define times85(x) ((((((x<<2)+x)<<2)+x)<<2)+x)
1352 static isc_result_t byte_atob(int c, isc_buffer_t *target,
1353 struct state *state);
1354 static isc_result_t putbyte(int c, isc_buffer_t *, struct state *state);
1355 static isc_result_t byte_btoa(int c, isc_buffer_t *, struct state *state);
1358 * Decode ASCII-encoded byte c into binary representation and
1359 * place into *bufp, advancing bufp.
1362 byte_atob(int c, isc_buffer_t *target, struct state *state) {
1366 return(DNS_R_SYNTAX);
1368 RETERR(putbyte(0, target, state));
1369 RETERR(putbyte(0, target, state));
1370 RETERR(putbyte(0, target, state));
1371 RETERR(putbyte(0, target, state));
1373 } else if ((s = strchr(atob_digits, c)) != NULL) {
1375 word = s - atob_digits;
1377 } else if (bcount < 4) {
1378 word = times85(word);
1379 word += s - atob_digits;
1382 word = times85(word);
1383 word += s - atob_digits;
1384 RETERR(putbyte((word >> 24) & 0xff, target, state));
1385 RETERR(putbyte((word >> 16) & 0xff, target, state));
1386 RETERR(putbyte((word >> 8) & 0xff, target, state));
1387 RETERR(putbyte(word & 0xff, target, state));
1392 return(DNS_R_SYNTAX);
1393 return(ISC_R_SUCCESS);
1397 * Compute checksum info and place c into target.
1400 putbyte(int c, isc_buffer_t *target, struct state *state) {
1406 if ((Crot & 0x80000000)) {
1413 isc_buffer_availableregion(target, &tr);
1415 return (ISC_R_NOSPACE);
1417 isc_buffer_add(target, 1);
1418 return (ISC_R_SUCCESS);
1422 * Read the ASCII-encoded data from inbuf, of length inbuflen, and convert
1423 * it into T_UNSPEC (binary data) in outbuf, not to exceed outbuflen bytes;
1424 * outbuflen must be divisible by 4. (Note: this is because outbuf is filled
1425 * in 4 bytes at a time. If the actual data doesn't end on an even 4-byte
1426 * boundary, there will be no problem...it will be padded with 0 bytes, and
1427 * numbytes will indicate the correct number of bytes. The main point is
1428 * that since the buffer is filled in 4 bytes at a time, even if there is
1429 * not a full 4 bytes of data at the end, there has to be room to 0-pad the
1430 * data, so the buffer must be of size divisible by 4). Place the number of
1431 * output bytes in numbytes, and return a failure/success status.
1435 atob_tobuffer(isc_lex_t *lexer, isc_buffer_t *target) {
1436 long oeor, osum, orot;
1437 struct state statebuf, *state= &statebuf;
1442 Ceor = Csum = Crot = word = bcount = 0;
1444 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1446 while (token.value.as_textregion.length != 0) {
1447 if ((c = token.value.as_textregion.base[0]) == 'x') {
1450 RETERR(byte_atob(c, target, state));
1451 isc_textregion_consume(&token.value.as_textregion, 1);
1457 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
1459 if ((token.value.as_ulong % 4) != 0U)
1460 isc_buffer_subtract(target, 4 - (token.value.as_ulong % 4));
1465 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1467 oeor = strtol(DNS_AS_STR(token), &e, 16);
1469 return (DNS_R_SYNTAX);
1474 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1476 osum = strtol(DNS_AS_STR(token), &e, 16);
1478 return (DNS_R_SYNTAX);
1483 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1485 orot = strtol(DNS_AS_STR(token), &e, 16);
1487 return (DNS_R_SYNTAX);
1489 if ((oeor != Ceor) || (osum != Csum) || (orot != Crot))
1490 return(DNS_R_BADCKSUM);
1491 return (ISC_R_SUCCESS);
1495 * Encode binary byte c into ASCII representation and place into *bufp,
1499 byte_btoa(int c, isc_buffer_t *target, struct state *state) {
1502 isc_buffer_availableregion(target, &tr);
1506 if ((Crot & 0x80000000)) {
1519 return (ISC_R_NOSPACE);
1521 isc_buffer_add(target, 1);
1523 register int tmp = 0;
1524 register isc_int32_t tmpword = word;
1528 * Because some don't support u_long.
1531 tmpword -= (isc_int32_t)(85 * 85 * 85 * 85 * 32);
1535 tmpword -= (isc_int32_t)(85 * 85 * 85 * 85 * 32);
1538 return (ISC_R_NOSPACE);
1539 tr.base[0] = atob_digits[(tmpword /
1540 (isc_int32_t)(85 * 85 * 85 * 85))
1542 tmpword %= (isc_int32_t)(85 * 85 * 85 * 85);
1543 tr.base[1] = atob_digits[tmpword / (85 * 85 * 85)];
1544 tmpword %= (85 * 85 * 85);
1545 tr.base[2] = atob_digits[tmpword / (85 * 85)];
1546 tmpword %= (85 * 85);
1547 tr.base[3] = atob_digits[tmpword / 85];
1549 tr.base[4] = atob_digits[tmpword];
1550 isc_buffer_add(target, 5);
1556 return (ISC_R_SUCCESS);
1561 * Encode the binary data from inbuf, of length inbuflen, into a
1562 * target. Return success/failure status
1565 btoa_totext(unsigned char *inbuf, int inbuflen, isc_buffer_t *target) {
1567 struct state statebuf, *state = &statebuf;
1568 char buf[sizeof("x 2000000000 ffffffff ffffffff ffffffff")];
1570 Ceor = Csum = Crot = word = bcount = 0;
1571 for (inc = 0; inc < inbuflen; inbuf++, inc++)
1572 RETERR(byte_btoa(*inbuf, target, state));
1575 RETERR(byte_btoa(0, target, state));
1578 * Put byte count and checksum information at end of buffer,
1581 snprintf(buf, sizeof(buf), "x %d %x %x %x", inbuflen, Ceor, Csum, Crot);
1582 return (str_totext(buf, target));
1587 default_fromtext_callback(dns_rdatacallbacks_t *callbacks, const char *fmt,
1595 vfprintf(stderr, fmt, ap);
1597 fprintf(stderr, "\n");
1601 fromtext_warneof(isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks) {
1602 if (isc_lex_isfile(lexer) && callbacks != NULL) {
1603 const char *name = isc_lex_getsourcename(lexer);
1606 (*callbacks->warn)(callbacks,
1607 "%s:%lu: file does not end with newline",
1608 name, isc_lex_getsourceline(lexer));
1613 warn_badmx(isc_token_t *token, isc_lex_t *lexer,
1614 dns_rdatacallbacks_t *callbacks)
1619 if (lexer != NULL) {
1620 file = isc_lex_getsourcename(lexer);
1621 line = isc_lex_getsourceline(lexer);
1622 (*callbacks->warn)(callbacks, "%s:%u: warning: '%s': %s",
1623 file, line, DNS_AS_STR(*token),
1624 dns_result_totext(DNS_R_MXISADDRESS));
1629 warn_badname(dns_name_t *name, isc_lex_t *lexer,
1630 dns_rdatacallbacks_t *callbacks)
1634 char namebuf[DNS_NAME_FORMATSIZE];
1636 if (lexer != NULL) {
1637 file = isc_lex_getsourcename(lexer);
1638 line = isc_lex_getsourceline(lexer);
1639 dns_name_format(name, namebuf, sizeof(namebuf));
1640 (*callbacks->warn)(callbacks, "%s:%u: warning: %s: %s",
1641 file, line, namebuf,
1642 dns_result_totext(DNS_R_BADNAME));
1647 fromtext_error(void (*callback)(dns_rdatacallbacks_t *, const char *, ...),
1648 dns_rdatacallbacks_t *callbacks, const char *name,
1649 unsigned long line, isc_token_t *token, isc_result_t result)
1654 if (token != NULL) {
1655 switch (token->type) {
1656 case isc_tokentype_eol:
1657 (*callback)(callbacks, "%s: %s:%lu: near eol: %s",
1658 "dns_rdata_fromtext", name, line,
1659 dns_result_totext(result));
1661 case isc_tokentype_eof:
1662 (*callback)(callbacks, "%s: %s:%lu: near eof: %s",
1663 "dns_rdata_fromtext", name, line,
1664 dns_result_totext(result));
1666 case isc_tokentype_number:
1667 (*callback)(callbacks, "%s: %s:%lu: near %lu: %s",
1668 "dns_rdata_fromtext", name, line,
1669 token->value.as_ulong,
1670 dns_result_totext(result));
1672 case isc_tokentype_string:
1673 case isc_tokentype_qstring:
1674 (*callback)(callbacks, "%s: %s:%lu: near '%s': %s",
1675 "dns_rdata_fromtext", name, line,
1677 dns_result_totext(result));
1680 (*callback)(callbacks, "%s: %s:%lu: %s",
1681 "dns_rdata_fromtext", name, line,
1682 dns_result_totext(result));
1686 (*callback)(callbacks, "dns_rdata_fromtext: %s:%lu: %s",
1687 name, line, dns_result_totext(result));
1692 dns_rdata_covers(dns_rdata_t *rdata) {
1693 if (rdata->type == 46)
1694 return (covers_rrsig(rdata));
1695 return (covers_sig(rdata));
1699 dns_rdatatype_ismeta(dns_rdatatype_t type) {
1700 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_META) != 0)
1706 dns_rdatatype_issingleton(dns_rdatatype_t type) {
1707 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_SINGLETON)
1714 dns_rdatatype_notquestion(dns_rdatatype_t type) {
1715 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_NOTQUESTION)
1722 dns_rdatatype_questiononly(dns_rdatatype_t type) {
1723 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_QUESTIONONLY)
1730 dns_rdatatype_atparent(dns_rdatatype_t type) {
1731 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_ATPARENT) != 0)
1737 dns_rdataclass_ismeta(dns_rdataclass_t rdclass) {
1739 if (rdclass == dns_rdataclass_reserved0
1740 || rdclass == dns_rdataclass_none
1741 || rdclass == dns_rdataclass_any)
1744 return (ISC_FALSE); /* Assume it is not a meta class. */
1748 dns_rdatatype_isdnssec(dns_rdatatype_t type) {
1749 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_DNSSEC) != 0)
1755 dns_rdatatype_iszonecutauth(dns_rdatatype_t type) {
1756 if ((dns_rdatatype_attributes(type)
1757 & (DNS_RDATATYPEATTR_DNSSEC | DNS_RDATATYPEATTR_ZONECUTAUTH))
1764 dns_rdatatype_isknown(dns_rdatatype_t type) {
1765 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_UNKNOWN)