Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / rdata.c
blobfc2a1b1966358e03dae2a831f29fb7463fc37434
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: rdata.c,v 1.204 2009/12/04 21:09:33 marka Exp */
22 /*! \file */
24 #include <config.h>
25 #include <ctype.h>
27 #include <isc/base64.h>
28 #include <isc/hex.h>
29 #include <isc/lex.h>
30 #include <isc/mem.h>
31 #include <isc/parseint.h>
32 #include <isc/print.h>
33 #include <isc/string.h>
34 #include <isc/stdlib.h>
35 #include <isc/util.h>
37 #include <dns/callbacks.h>
38 #include <dns/cert.h>
39 #include <dns/compress.h>
40 #include <dns/enumtype.h>
41 #include <dns/keyflags.h>
42 #include <dns/keyvalues.h>
43 #include <dns/message.h>
44 #include <dns/rcode.h>
45 #include <dns/rdata.h>
46 #include <dns/rdataclass.h>
47 #include <dns/rdatastruct.h>
48 #include <dns/rdatatype.h>
49 #include <dns/result.h>
50 #include <dns/secalg.h>
51 #include <dns/secproto.h>
52 #include <dns/time.h>
53 #include <dns/ttl.h>
55 #define RETERR(x) \
56 do { \
57 isc_result_t _r = (x); \
58 if (_r != ISC_R_SUCCESS) \
59 return (_r); \
60 } while (0)
62 #define RETTOK(x) \
63 do { \
64 isc_result_t _r = (x); \
65 if (_r != ISC_R_SUCCESS) { \
66 isc_lex_ungettoken(lexer, &token); \
67 return (_r); \
68 } \
69 } while (0)
71 #define DNS_AS_STR(t) ((t).value.as_textregion.base)
73 #define ARGS_FROMTEXT int rdclass, dns_rdatatype_t type, \
74 isc_lex_t *lexer, dns_name_t *origin, \
75 unsigned int options, isc_buffer_t *target, \
76 dns_rdatacallbacks_t *callbacks
78 #define ARGS_TOTEXT dns_rdata_t *rdata, dns_rdata_textctx_t *tctx, \
79 isc_buffer_t *target
81 #define ARGS_FROMWIRE int rdclass, dns_rdatatype_t type, \
82 isc_buffer_t *source, dns_decompress_t *dctx, \
83 unsigned int options, isc_buffer_t *target
85 #define ARGS_TOWIRE dns_rdata_t *rdata, dns_compress_t *cctx, \
86 isc_buffer_t *target
88 #define ARGS_COMPARE const dns_rdata_t *rdata1, const dns_rdata_t *rdata2
90 #define ARGS_FROMSTRUCT int rdclass, dns_rdatatype_t type, \
91 void *source, isc_buffer_t *target
93 #define ARGS_TOSTRUCT dns_rdata_t *rdata, void *target, isc_mem_t *mctx
95 #define ARGS_FREESTRUCT void *source
97 #define ARGS_ADDLDATA dns_rdata_t *rdata, dns_additionaldatafunc_t add, \
98 void *arg
100 #define ARGS_DIGEST dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg
102 #define ARGS_CHECKOWNER dns_name_t *name, dns_rdataclass_t rdclass, \
103 dns_rdatatype_t type, isc_boolean_t wildcard
105 #define ARGS_CHECKNAMES dns_rdata_t *rdata, dns_name_t *owner, dns_name_t *bad
109 * Context structure for the totext_ functions.
110 * Contains formatting options for rdata-to-text
111 * conversion.
113 typedef struct dns_rdata_textctx {
114 dns_name_t *origin; /*%< Current origin, or NULL. */
115 unsigned int flags; /*%< DNS_STYLEFLAG_* */
116 unsigned int width; /*%< Width of rdata column. */
117 const char *linebreak; /*%< Line break string. */
118 } dns_rdata_textctx_t;
120 static isc_result_t
121 txt_totext(isc_region_t *source, isc_buffer_t *target);
123 static isc_result_t
124 txt_fromtext(isc_textregion_t *source, isc_buffer_t *target);
126 static isc_result_t
127 txt_fromwire(isc_buffer_t *source, isc_buffer_t *target);
129 static isc_boolean_t
130 name_prefix(dns_name_t *name, dns_name_t *origin, dns_name_t *target);
132 static unsigned int
133 name_length(dns_name_t *name);
135 static isc_result_t
136 str_totext(const char *source, isc_buffer_t *target);
138 static isc_result_t
139 inet_totext(int af, isc_region_t *src, isc_buffer_t *target);
141 static isc_boolean_t
142 buffer_empty(isc_buffer_t *source);
144 static void
145 buffer_fromregion(isc_buffer_t *buffer, isc_region_t *region);
147 static isc_result_t
148 uint32_tobuffer(isc_uint32_t, isc_buffer_t *target);
150 static isc_result_t
151 uint16_tobuffer(isc_uint32_t, isc_buffer_t *target);
153 static isc_result_t
154 uint8_tobuffer(isc_uint32_t, isc_buffer_t *target);
156 static isc_result_t
157 name_tobuffer(dns_name_t *name, isc_buffer_t *target);
159 static isc_uint32_t
160 uint32_fromregion(isc_region_t *region);
162 static isc_uint16_t
163 uint16_fromregion(isc_region_t *region);
165 static isc_uint8_t
166 uint8_fromregion(isc_region_t *region);
168 static isc_uint8_t
169 uint8_consume_fromregion(isc_region_t *region);
171 static isc_result_t
172 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);
174 static int
175 hexvalue(char value);
177 static int
178 decvalue(char value);
180 static isc_result_t
181 btoa_totext(unsigned char *inbuf, int inbuflen, isc_buffer_t *target);
183 static isc_result_t
184 atob_tobuffer(isc_lex_t *lexer, isc_buffer_t *target);
186 static void
187 default_fromtext_callback(dns_rdatacallbacks_t *callbacks, const char *, ...)
188 ISC_FORMAT_PRINTF(2, 3);
190 static void
191 fromtext_error(void (*callback)(dns_rdatacallbacks_t *, const char *, ...),
192 dns_rdatacallbacks_t *callbacks, const char *name,
193 unsigned long line, isc_token_t *token, isc_result_t result);
195 static void
196 fromtext_warneof(isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks);
198 static isc_result_t
199 rdata_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
200 isc_buffer_t *target);
202 static void
203 warn_badname(dns_name_t *name, isc_lex_t *lexer,
204 dns_rdatacallbacks_t *callbacks);
206 static void
207 warn_badmx(isc_token_t *token, isc_lex_t *lexer,
208 dns_rdatacallbacks_t *callbacks);
210 static isc_uint16_t
211 uint16_consume_fromregion(isc_region_t *region);
213 static inline int
214 getquad(const void *src, struct in_addr *dst,
215 isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks)
217 int result;
218 struct in_addr *tmp;
220 result = inet_aton(src, dst);
221 if (result == 1 && callbacks != NULL &&
222 inet_pton(AF_INET, src, &tmp) != 1) {
223 const char *name = isc_lex_getsourcename(lexer);
224 if (name == NULL)
225 name = "UNKNOWN";
226 (*callbacks->warn)(callbacks, "%s:%lu: \"%s\" "
227 "is not a decimal dotted quad", name,
228 isc_lex_getsourceline(lexer), src);
230 return (result);
233 static inline isc_result_t
234 name_duporclone(dns_name_t *source, isc_mem_t *mctx, dns_name_t *target) {
236 if (mctx != NULL)
237 return (dns_name_dup(source, mctx, target));
238 dns_name_clone(source, target);
239 return (ISC_R_SUCCESS);
242 static inline void *
243 mem_maybedup(isc_mem_t *mctx, void *source, size_t length) {
244 void *new;
246 if (mctx == NULL)
247 return (source);
248 new = isc_mem_allocate(mctx, length);
249 if (new != NULL)
250 memcpy(new, source, length);
252 return (new);
255 static const char hexdigits[] = "0123456789abcdef";
256 static const char decdigits[] = "0123456789";
258 #include "code.h"
260 #define META 0x0001
261 #define RESERVED 0x0002
263 /***
264 *** Initialization
265 ***/
267 void
268 dns_rdata_init(dns_rdata_t *rdata) {
270 REQUIRE(rdata != NULL);
272 rdata->data = NULL;
273 rdata->length = 0;
274 rdata->rdclass = 0;
275 rdata->type = 0;
276 rdata->flags = 0;
277 ISC_LINK_INIT(rdata, link);
278 /* ISC_LIST_INIT(rdata->list); */
281 void
282 dns_rdata_reset(dns_rdata_t *rdata) {
284 REQUIRE(rdata != NULL);
286 REQUIRE(!ISC_LINK_LINKED(rdata, link));
287 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
289 rdata->data = NULL;
290 rdata->length = 0;
291 rdata->rdclass = 0;
292 rdata->type = 0;
293 rdata->flags = 0;
296 /***
298 ***/
300 void
301 dns_rdata_clone(const dns_rdata_t *src, dns_rdata_t *target) {
303 REQUIRE(src != NULL);
304 REQUIRE(target != NULL);
306 REQUIRE(DNS_RDATA_INITIALIZED(target));
308 REQUIRE(DNS_RDATA_VALIDFLAGS(src));
309 REQUIRE(DNS_RDATA_VALIDFLAGS(target));
311 target->data = src->data;
312 target->length = src->length;
313 target->rdclass = src->rdclass;
314 target->type = src->type;
315 target->flags = src->flags;
319 /***
320 *** Comparisons
321 ***/
324 dns_rdata_compare(const dns_rdata_t *rdata1, const dns_rdata_t *rdata2) {
325 int result = 0;
326 isc_boolean_t use_default = ISC_FALSE;
328 REQUIRE(rdata1 != NULL);
329 REQUIRE(rdata2 != NULL);
330 REQUIRE(rdata1->data != NULL);
331 REQUIRE(rdata2->data != NULL);
332 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata1));
333 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata2));
335 if (rdata1->rdclass != rdata2->rdclass)
336 return (rdata1->rdclass < rdata2->rdclass ? -1 : 1);
338 if (rdata1->type != rdata2->type)
339 return (rdata1->type < rdata2->type ? -1 : 1);
341 COMPARESWITCH
343 if (use_default) {
344 isc_region_t r1;
345 isc_region_t r2;
347 dns_rdata_toregion(rdata1, &r1);
348 dns_rdata_toregion(rdata2, &r2);
349 result = isc_region_compare(&r1, &r2);
351 return (result);
355 dns_rdata_casecompare(const dns_rdata_t *rdata1, const dns_rdata_t *rdata2) {
356 int result = 0;
357 isc_boolean_t use_default = ISC_FALSE;
359 REQUIRE(rdata1 != NULL);
360 REQUIRE(rdata2 != NULL);
361 REQUIRE(rdata1->data != NULL);
362 REQUIRE(rdata2->data != NULL);
363 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata1));
364 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata2));
366 if (rdata1->rdclass != rdata2->rdclass)
367 return (rdata1->rdclass < rdata2->rdclass ? -1 : 1);
369 if (rdata1->type != rdata2->type)
370 return (rdata1->type < rdata2->type ? -1 : 1);
372 CASECOMPARESWITCH
374 if (use_default) {
375 isc_region_t r1;
376 isc_region_t r2;
378 dns_rdata_toregion(rdata1, &r1);
379 dns_rdata_toregion(rdata2, &r2);
380 result = isc_region_compare(&r1, &r2);
382 return (result);
385 /***
386 *** Conversions
387 ***/
389 void
390 dns_rdata_fromregion(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
391 dns_rdatatype_t type, isc_region_t *r)
394 REQUIRE(rdata != NULL);
395 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
396 REQUIRE(r != NULL);
398 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
400 rdata->data = r->base;
401 rdata->length = r->length;
402 rdata->rdclass = rdclass;
403 rdata->type = type;
404 rdata->flags = 0;
407 void
408 dns_rdata_toregion(const dns_rdata_t *rdata, isc_region_t *r) {
410 REQUIRE(rdata != NULL);
411 REQUIRE(r != NULL);
412 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
414 r->base = rdata->data;
415 r->length = rdata->length;
418 isc_result_t
419 dns_rdata_fromwire(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
420 dns_rdatatype_t type, isc_buffer_t *source,
421 dns_decompress_t *dctx, unsigned int options,
422 isc_buffer_t *target)
424 isc_result_t result = ISC_R_NOTIMPLEMENTED;
425 isc_region_t region;
426 isc_buffer_t ss;
427 isc_buffer_t st;
428 isc_boolean_t use_default = ISC_FALSE;
429 isc_uint32_t activelength;
431 REQUIRE(dctx != NULL);
432 if (rdata != NULL) {
433 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
434 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
437 if (type == 0)
438 return (DNS_R_FORMERR);
440 ss = *source;
441 st = *target;
443 activelength = isc_buffer_activelength(source);
444 INSIST(activelength < 65536);
446 FROMWIRESWITCH
448 if (use_default) {
449 if (activelength > isc_buffer_availablelength(target))
450 result = ISC_R_NOSPACE;
451 else {
452 isc_buffer_putmem(target, isc_buffer_current(source),
453 activelength);
454 isc_buffer_forward(source, activelength);
455 result = ISC_R_SUCCESS;
460 * We should have consumed all of our buffer.
462 if (result == ISC_R_SUCCESS && !buffer_empty(source))
463 result = DNS_R_EXTRADATA;
465 if (rdata != NULL && result == ISC_R_SUCCESS) {
466 region.base = isc_buffer_used(&st);
467 region.length = isc_buffer_usedlength(target) -
468 isc_buffer_usedlength(&st);
469 dns_rdata_fromregion(rdata, rdclass, type, &region);
472 if (result != ISC_R_SUCCESS) {
473 *source = ss;
474 *target = st;
476 return (result);
479 isc_result_t
480 dns_rdata_towire(dns_rdata_t *rdata, dns_compress_t *cctx,
481 isc_buffer_t *target)
483 isc_result_t result = ISC_R_NOTIMPLEMENTED;
484 isc_boolean_t use_default = ISC_FALSE;
485 isc_region_t tr;
486 isc_buffer_t st;
488 REQUIRE(rdata != NULL);
489 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
492 * Some DynDNS meta-RRs have empty rdata.
494 if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
495 INSIST(rdata->length == 0);
496 return (ISC_R_SUCCESS);
499 st = *target;
501 TOWIRESWITCH
503 if (use_default) {
504 isc_buffer_availableregion(target, &tr);
505 if (tr.length < rdata->length)
506 return (ISC_R_NOSPACE);
507 memcpy(tr.base, rdata->data, rdata->length);
508 isc_buffer_add(target, rdata->length);
509 return (ISC_R_SUCCESS);
511 if (result != ISC_R_SUCCESS) {
512 *target = st;
513 INSIST(target->used < 65536);
514 dns_compress_rollback(cctx, (isc_uint16_t)target->used);
516 return (result);
520 * If the binary data in 'src' is valid uncompressed wire format
521 * rdata of class 'rdclass' and type 'type', return ISC_R_SUCCESS
522 * and copy the validated rdata to 'dest'. Otherwise return an error.
524 static isc_result_t
525 rdata_validate(isc_buffer_t *src, isc_buffer_t *dest, dns_rdataclass_t rdclass,
526 dns_rdatatype_t type)
528 dns_decompress_t dctx;
529 dns_rdata_t rdata = DNS_RDATA_INIT;
530 isc_result_t result;
532 dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_NONE);
533 isc_buffer_setactive(src, isc_buffer_usedlength(src));
534 result = dns_rdata_fromwire(&rdata, rdclass, type, src,
535 &dctx, 0, dest);
536 dns_decompress_invalidate(&dctx);
538 return (result);
541 static isc_result_t
542 unknown_fromtext(dns_rdataclass_t rdclass, dns_rdatatype_t type,
543 isc_lex_t *lexer, isc_mem_t *mctx, isc_buffer_t *target)
545 isc_result_t result;
546 isc_buffer_t *buf = NULL;
547 isc_token_t token;
549 if (type == 0 || dns_rdatatype_ismeta(type))
550 return (DNS_R_METATYPE);
552 result = isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
553 ISC_FALSE);
554 if (result == ISC_R_SUCCESS && token.value.as_ulong > 65535U)
555 return (ISC_R_RANGE);
556 result = isc_buffer_allocate(mctx, &buf, token.value.as_ulong);
557 if (result != ISC_R_SUCCESS)
558 return (result);
560 result = isc_hex_tobuffer(lexer, buf,
561 (unsigned int)token.value.as_ulong);
562 if (result != ISC_R_SUCCESS)
563 goto failure;
564 if (isc_buffer_usedlength(buf) != token.value.as_ulong) {
565 result = ISC_R_UNEXPECTEDEND;
566 goto failure;
569 if (dns_rdatatype_isknown(type)) {
570 result = rdata_validate(buf, target, rdclass, type);
571 } else {
572 isc_region_t r;
573 isc_buffer_usedregion(buf, &r);
574 result = isc_buffer_copyregion(target, &r);
576 if (result != ISC_R_SUCCESS)
577 goto failure;
579 isc_buffer_free(&buf);
580 return (ISC_R_SUCCESS);
582 failure:
583 isc_buffer_free(&buf);
584 return (result);
587 isc_result_t
588 dns_rdata_fromtext(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
589 dns_rdatatype_t type, isc_lex_t *lexer,
590 dns_name_t *origin, unsigned int options, isc_mem_t *mctx,
591 isc_buffer_t *target, dns_rdatacallbacks_t *callbacks)
593 isc_result_t result = ISC_R_NOTIMPLEMENTED;
594 isc_region_t region;
595 isc_buffer_t st;
596 isc_token_t token;
597 unsigned int lexoptions = ISC_LEXOPT_EOL | ISC_LEXOPT_EOF |
598 ISC_LEXOPT_DNSMULTILINE | ISC_LEXOPT_ESCAPE;
599 char *name;
600 unsigned long line;
601 void (*callback)(dns_rdatacallbacks_t *, const char *, ...);
602 isc_result_t tresult;
604 REQUIRE(origin == NULL || dns_name_isabsolute(origin) == ISC_TRUE);
605 if (rdata != NULL) {
606 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
607 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
609 if (callbacks != NULL) {
610 REQUIRE(callbacks->warn != NULL);
611 REQUIRE(callbacks->error != NULL);
614 st = *target;
616 if (callbacks != NULL)
617 callback = callbacks->error;
618 else
619 callback = default_fromtext_callback;
621 result = isc_lex_getmastertoken(lexer, &token, isc_tokentype_qstring,
622 ISC_FALSE);
623 if (result != ISC_R_SUCCESS) {
624 name = isc_lex_getsourcename(lexer);
625 line = isc_lex_getsourceline(lexer);
626 fromtext_error(callback, callbacks, name, line,
627 &token, result);
628 return (result);
631 if (strcmp(DNS_AS_STR(token), "\\#") == 0)
632 result = unknown_fromtext(rdclass, type, lexer, mctx, target);
633 else {
634 isc_lex_ungettoken(lexer, &token);
636 FROMTEXTSWITCH
640 * Consume to end of line / file.
641 * If not at end of line initially set error code.
642 * Call callback via fromtext_error once if there was an error.
644 do {
645 name = isc_lex_getsourcename(lexer);
646 line = isc_lex_getsourceline(lexer);
647 tresult = isc_lex_gettoken(lexer, lexoptions, &token);
648 if (tresult != ISC_R_SUCCESS) {
649 if (result == ISC_R_SUCCESS)
650 result = tresult;
651 if (callback != NULL)
652 fromtext_error(callback, callbacks, name,
653 line, NULL, result);
654 break;
655 } else if (token.type != isc_tokentype_eol &&
656 token.type != isc_tokentype_eof) {
657 if (result == ISC_R_SUCCESS)
658 result = DNS_R_EXTRATOKEN;
659 if (callback != NULL) {
660 fromtext_error(callback, callbacks, name,
661 line, &token, result);
662 callback = NULL;
664 } else if (result != ISC_R_SUCCESS && callback != NULL) {
665 fromtext_error(callback, callbacks, name, line,
666 &token, result);
667 break;
668 } else {
669 if (token.type == isc_tokentype_eof)
670 fromtext_warneof(lexer, callbacks);
671 break;
673 } while (1);
675 if (rdata != NULL && result == ISC_R_SUCCESS) {
676 region.base = isc_buffer_used(&st);
677 region.length = isc_buffer_usedlength(target) -
678 isc_buffer_usedlength(&st);
679 dns_rdata_fromregion(rdata, rdclass, type, &region);
681 if (result != ISC_R_SUCCESS) {
682 *target = st;
684 return (result);
687 static isc_result_t
688 rdata_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
689 isc_buffer_t *target)
691 isc_result_t result = ISC_R_NOTIMPLEMENTED;
692 isc_boolean_t use_default = ISC_FALSE;
693 char buf[sizeof("65535")];
694 isc_region_t sr;
696 REQUIRE(rdata != NULL);
697 REQUIRE(tctx->origin == NULL ||
698 dns_name_isabsolute(tctx->origin) == ISC_TRUE);
701 * Some DynDNS meta-RRs have empty rdata.
703 if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
704 INSIST(rdata->length == 0);
705 return (ISC_R_SUCCESS);
708 TOTEXTSWITCH
710 if (use_default) {
711 strlcpy(buf, "\\# ", sizeof(buf));
712 result = str_totext(buf, target);
713 dns_rdata_toregion(rdata, &sr);
714 INSIST(sr.length < 65536);
715 snprintf(buf, sizeof(buf), "%u", sr.length);
716 result = str_totext(buf, target);
717 if (sr.length != 0 && result == ISC_R_SUCCESS) {
718 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
719 result = str_totext(" ( ", target);
720 else
721 result = str_totext(" ", target);
722 if (result == ISC_R_SUCCESS)
723 result = isc_hex_totext(&sr, tctx->width - 2,
724 tctx->linebreak,
725 target);
726 if (result == ISC_R_SUCCESS &&
727 (tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
728 result = str_totext(" )", target);
732 return (result);
735 isc_result_t
736 dns_rdata_totext(dns_rdata_t *rdata, dns_name_t *origin, isc_buffer_t *target)
738 dns_rdata_textctx_t tctx;
740 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
743 * Set up formatting options for single-line output.
745 tctx.origin = origin;
746 tctx.flags = 0;
747 tctx.width = 60;
748 tctx.linebreak = " ";
749 return (rdata_totext(rdata, &tctx, target));
752 isc_result_t
753 dns_rdata_tofmttext(dns_rdata_t *rdata, dns_name_t *origin,
754 unsigned int flags, unsigned int width,
755 const char *linebreak, isc_buffer_t *target)
757 dns_rdata_textctx_t tctx;
759 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
762 * Set up formatting options for formatted output.
764 tctx.origin = origin;
765 tctx.flags = flags;
766 if ((flags & DNS_STYLEFLAG_MULTILINE) != 0) {
767 tctx.width = width;
768 tctx.linebreak = linebreak;
769 } else {
770 tctx.width = 60; /* Used for hex word length only. */
771 tctx.linebreak = " ";
773 return (rdata_totext(rdata, &tctx, target));
776 isc_result_t
777 dns_rdata_fromstruct(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
778 dns_rdatatype_t type, void *source,
779 isc_buffer_t *target)
781 isc_result_t result = ISC_R_NOTIMPLEMENTED;
782 isc_buffer_t st;
783 isc_region_t region;
784 isc_boolean_t use_default = ISC_FALSE;
786 REQUIRE(source != NULL);
787 if (rdata != NULL) {
788 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
789 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
792 st = *target;
794 FROMSTRUCTSWITCH
796 if (use_default)
797 (void)NULL;
799 if (rdata != NULL && result == ISC_R_SUCCESS) {
800 region.base = isc_buffer_used(&st);
801 region.length = isc_buffer_usedlength(target) -
802 isc_buffer_usedlength(&st);
803 dns_rdata_fromregion(rdata, rdclass, type, &region);
805 if (result != ISC_R_SUCCESS)
806 *target = st;
807 return (result);
810 isc_result_t
811 dns_rdata_tostruct(dns_rdata_t *rdata, void *target, isc_mem_t *mctx) {
812 isc_result_t result = ISC_R_NOTIMPLEMENTED;
813 isc_boolean_t use_default = ISC_FALSE;
815 REQUIRE(rdata != NULL);
816 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
818 TOSTRUCTSWITCH
820 if (use_default)
821 (void)NULL;
823 return (result);
826 void
827 dns_rdata_freestruct(void *source) {
828 dns_rdatacommon_t *common = source;
829 REQUIRE(source != NULL);
831 FREESTRUCTSWITCH
834 isc_result_t
835 dns_rdata_additionaldata(dns_rdata_t *rdata, dns_additionaldatafunc_t add,
836 void *arg)
838 isc_result_t result = ISC_R_NOTIMPLEMENTED;
839 isc_boolean_t use_default = ISC_FALSE;
842 * Call 'add' for each name and type from 'rdata' which is subject to
843 * additional section processing.
846 REQUIRE(rdata != NULL);
847 REQUIRE(add != NULL);
848 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
850 ADDITIONALDATASWITCH
852 /* No additional processing for unknown types */
853 if (use_default)
854 result = ISC_R_SUCCESS;
856 return (result);
859 isc_result_t
860 dns_rdata_digest(dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg) {
861 isc_result_t result = ISC_R_NOTIMPLEMENTED;
862 isc_boolean_t use_default = ISC_FALSE;
863 isc_region_t r;
866 * Send 'rdata' in DNSSEC canonical form to 'digest'.
869 REQUIRE(rdata != NULL);
870 REQUIRE(digest != NULL);
871 REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
873 DIGESTSWITCH
875 if (use_default) {
876 dns_rdata_toregion(rdata, &r);
877 result = (digest)(arg, &r);
880 return (result);
883 isc_boolean_t
884 dns_rdata_checkowner(dns_name_t *name, dns_rdataclass_t rdclass,
885 dns_rdatatype_t type, isc_boolean_t wildcard)
887 isc_boolean_t result;
889 CHECKOWNERSWITCH
890 return (result);
893 isc_boolean_t
894 dns_rdata_checknames(dns_rdata_t *rdata, dns_name_t *owner, dns_name_t *bad)
896 isc_boolean_t result;
898 CHECKNAMESSWITCH
899 return (result);
902 unsigned int
903 dns_rdatatype_attributes(dns_rdatatype_t type)
905 RDATATYPE_ATTRIBUTE_SW
906 if (type >= (dns_rdatatype_t)128 && type < (dns_rdatatype_t)255)
907 return (DNS_RDATATYPEATTR_UNKNOWN | DNS_RDATATYPEATTR_META);
908 return (DNS_RDATATYPEATTR_UNKNOWN);
911 isc_result_t
912 dns_rdatatype_fromtext(dns_rdatatype_t *typep, isc_textregion_t *source) {
913 unsigned int hash;
914 unsigned int n;
915 unsigned char a, b;
917 n = source->length;
919 if (n == 0)
920 return (DNS_R_UNKNOWN);
922 a = tolower((unsigned char)source->base[0]);
923 b = tolower((unsigned char)source->base[n - 1]);
925 hash = ((a + n) * b) % 256;
928 * This switch block is inlined via \#define, and will use "return"
929 * to return a result to the caller if it is a valid (known)
930 * rdatatype name.
932 RDATATYPE_FROMTEXT_SW(hash, source->base, n, typep);
934 if (source->length > 4 && source->length < (4 + sizeof("65000")) &&
935 strncasecmp("type", source->base, 4) == 0) {
936 char buf[sizeof("65000")];
937 char *endp;
938 unsigned int val;
940 strncpy(buf, source->base + 4, source->length - 4);
941 buf[source->length - 4] = '\0';
942 val = strtoul(buf, &endp, 10);
943 if (*endp == '\0' && val <= 0xffff) {
944 *typep = (dns_rdatatype_t)val;
945 return (ISC_R_SUCCESS);
949 return (DNS_R_UNKNOWN);
952 isc_result_t
953 dns_rdatatype_totext(dns_rdatatype_t type, isc_buffer_t *target) {
954 char buf[sizeof("TYPE65535")];
956 RDATATYPE_TOTEXT_SW
957 snprintf(buf, sizeof(buf), "TYPE%u", type);
958 return (str_totext(buf, target));
961 void
962 dns_rdatatype_format(dns_rdatatype_t rdtype,
963 char *array, unsigned int size)
965 isc_result_t result;
966 isc_buffer_t buf;
968 isc_buffer_init(&buf, array, size);
969 result = dns_rdatatype_totext(rdtype, &buf);
971 * Null terminate.
973 if (result == ISC_R_SUCCESS) {
974 if (isc_buffer_availablelength(&buf) >= 1)
975 isc_buffer_putuint8(&buf, 0);
976 else
977 result = ISC_R_NOSPACE;
979 if (result != ISC_R_SUCCESS) {
980 snprintf(array, size, "<unknown>");
981 array[size - 1] = '\0';
986 * Private function.
989 static unsigned int
990 name_length(dns_name_t *name) {
991 return (name->length);
994 static isc_result_t
995 txt_totext(isc_region_t *source, isc_buffer_t *target) {
996 unsigned int tl;
997 unsigned int n;
998 unsigned char *sp;
999 char *tp;
1000 isc_region_t region;
1002 isc_buffer_availableregion(target, &region);
1003 sp = source->base;
1004 tp = (char *)region.base;
1005 tl = region.length;
1007 n = *sp++;
1009 REQUIRE(n + 1 <= source->length);
1011 if (tl < 1)
1012 return (ISC_R_NOSPACE);
1013 *tp++ = '"';
1014 tl--;
1015 while (n--) {
1016 if (*sp < 0x20 || *sp >= 0x7f) {
1017 if (tl < 4)
1018 return (ISC_R_NOSPACE);
1019 *tp++ = 0x5c;
1020 *tp++ = 0x30 + ((*sp / 100) % 10);
1021 *tp++ = 0x30 + ((*sp / 10) % 10);
1022 *tp++ = 0x30 + (*sp % 10);
1023 sp++;
1024 tl -= 4;
1025 continue;
1027 /* double quote, semi-colon, backslash */
1028 if (*sp == 0x22 || *sp == 0x3b || *sp == 0x5c) {
1029 if (tl < 2)
1030 return (ISC_R_NOSPACE);
1031 *tp++ = '\\';
1032 tl--;
1034 if (tl < 1)
1035 return (ISC_R_NOSPACE);
1036 *tp++ = *sp++;
1037 tl--;
1039 if (tl < 1)
1040 return (ISC_R_NOSPACE);
1041 *tp++ = '"';
1042 tl--;
1043 isc_buffer_add(target, tp - (char *)region.base);
1044 isc_region_consume(source, *source->base + 1);
1045 return (ISC_R_SUCCESS);
1048 static isc_result_t
1049 txt_fromtext(isc_textregion_t *source, isc_buffer_t *target) {
1050 isc_region_t tregion;
1051 isc_boolean_t escape;
1052 unsigned int n, nrem;
1053 char *s;
1054 unsigned char *t;
1055 int d;
1056 int c;
1058 isc_buffer_availableregion(target, &tregion);
1059 s = source->base;
1060 n = source->length;
1061 t = tregion.base;
1062 nrem = tregion.length;
1063 escape = ISC_FALSE;
1064 if (nrem < 1)
1065 return (ISC_R_NOSPACE);
1067 * Length byte.
1069 nrem--;
1070 t++;
1072 * Maximum text string length.
1074 if (nrem > 255)
1075 nrem = 255;
1076 while (n-- != 0) {
1077 c = (*s++) & 0xff;
1078 if (escape && (d = decvalue((char)c)) != -1) {
1079 c = d;
1080 if (n == 0)
1081 return (DNS_R_SYNTAX);
1082 n--;
1083 if ((d = decvalue(*s++)) != -1)
1084 c = c * 10 + d;
1085 else
1086 return (DNS_R_SYNTAX);
1087 if (n == 0)
1088 return (DNS_R_SYNTAX);
1089 n--;
1090 if ((d = decvalue(*s++)) != -1)
1091 c = c * 10 + d;
1092 else
1093 return (DNS_R_SYNTAX);
1094 if (c > 255)
1095 return (DNS_R_SYNTAX);
1096 } else if (!escape && c == '\\') {
1097 escape = ISC_TRUE;
1098 continue;
1100 escape = ISC_FALSE;
1101 if (nrem == 0)
1102 return (ISC_R_NOSPACE);
1103 *t++ = c;
1104 nrem--;
1106 if (escape)
1107 return (DNS_R_SYNTAX);
1108 *tregion.base = t - tregion.base - 1;
1109 isc_buffer_add(target, *tregion.base + 1);
1110 return (ISC_R_SUCCESS);
1113 static isc_result_t
1114 txt_fromwire(isc_buffer_t *source, isc_buffer_t *target) {
1115 unsigned int n;
1116 isc_region_t sregion;
1117 isc_region_t tregion;
1119 isc_buffer_activeregion(source, &sregion);
1120 if (sregion.length == 0)
1121 return(ISC_R_UNEXPECTEDEND);
1122 n = *sregion.base + 1;
1123 if (n > sregion.length)
1124 return (ISC_R_UNEXPECTEDEND);
1126 isc_buffer_availableregion(target, &tregion);
1127 if (n > tregion.length)
1128 return (ISC_R_NOSPACE);
1130 memcpy(tregion.base, sregion.base, n);
1131 isc_buffer_forward(source, n);
1132 isc_buffer_add(target, n);
1133 return (ISC_R_SUCCESS);
1136 static isc_boolean_t
1137 name_prefix(dns_name_t *name, dns_name_t *origin, dns_name_t *target) {
1138 int l1, l2;
1140 if (origin == NULL)
1141 goto return_false;
1143 if (dns_name_compare(origin, dns_rootname) == 0)
1144 goto return_false;
1146 if (!dns_name_issubdomain(name, origin))
1147 goto return_false;
1149 l1 = dns_name_countlabels(name);
1150 l2 = dns_name_countlabels(origin);
1152 if (l1 == l2)
1153 goto return_false;
1155 dns_name_getlabelsequence(name, 0, l1 - l2, target);
1156 return (ISC_TRUE);
1158 return_false:
1159 *target = *name;
1160 return (ISC_FALSE);
1163 static isc_result_t
1164 str_totext(const char *source, isc_buffer_t *target) {
1165 unsigned int l;
1166 isc_region_t region;
1168 isc_buffer_availableregion(target, &region);
1169 l = strlen(source);
1171 if (l > region.length)
1172 return (ISC_R_NOSPACE);
1174 memcpy(region.base, source, l);
1175 isc_buffer_add(target, l);
1176 return (ISC_R_SUCCESS);
1179 static isc_result_t
1180 inet_totext(int af, isc_region_t *src, isc_buffer_t *target) {
1181 char tmpbuf[64];
1183 /* Note - inet_ntop doesn't do size checking on its input. */
1184 if (inet_ntop(af, src->base, tmpbuf, sizeof(tmpbuf)) == NULL)
1185 return (ISC_R_NOSPACE);
1186 if (strlen(tmpbuf) > isc_buffer_availablelength(target))
1187 return (ISC_R_NOSPACE);
1188 isc_buffer_putstr(target, tmpbuf);
1189 return (ISC_R_SUCCESS);
1192 static isc_boolean_t
1193 buffer_empty(isc_buffer_t *source) {
1194 return((source->current == source->active) ? ISC_TRUE : ISC_FALSE);
1197 static void
1198 buffer_fromregion(isc_buffer_t *buffer, isc_region_t *region) {
1199 isc_buffer_init(buffer, region->base, region->length);
1200 isc_buffer_add(buffer, region->length);
1201 isc_buffer_setactive(buffer, region->length);
1204 static isc_result_t
1205 uint32_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1206 isc_region_t region;
1208 isc_buffer_availableregion(target, &region);
1209 if (region.length < 4)
1210 return (ISC_R_NOSPACE);
1211 isc_buffer_putuint32(target, value);
1212 return (ISC_R_SUCCESS);
1215 static isc_result_t
1216 uint16_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1217 isc_region_t region;
1219 if (value > 0xffff)
1220 return (ISC_R_RANGE);
1221 isc_buffer_availableregion(target, &region);
1222 if (region.length < 2)
1223 return (ISC_R_NOSPACE);
1224 isc_buffer_putuint16(target, (isc_uint16_t)value);
1225 return (ISC_R_SUCCESS);
1228 static isc_result_t
1229 uint8_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
1230 isc_region_t region;
1232 if (value > 0xff)
1233 return (ISC_R_RANGE);
1234 isc_buffer_availableregion(target, &region);
1235 if (region.length < 1)
1236 return (ISC_R_NOSPACE);
1237 isc_buffer_putuint8(target, (isc_uint8_t)value);
1238 return (ISC_R_SUCCESS);
1241 static isc_result_t
1242 name_tobuffer(dns_name_t *name, isc_buffer_t *target) {
1243 isc_region_t r;
1244 dns_name_toregion(name, &r);
1245 return (isc_buffer_copyregion(target, &r));
1248 static isc_uint32_t
1249 uint32_fromregion(isc_region_t *region) {
1250 isc_uint32_t value;
1252 REQUIRE(region->length >= 4);
1253 value = region->base[0] << 24;
1254 value |= region->base[1] << 16;
1255 value |= region->base[2] << 8;
1256 value |= region->base[3];
1257 return(value);
1260 static isc_uint16_t
1261 uint16_consume_fromregion(isc_region_t *region) {
1262 isc_uint16_t r = uint16_fromregion(region);
1264 isc_region_consume(region, 2);
1265 return r;
1268 static isc_uint16_t
1269 uint16_fromregion(isc_region_t *region) {
1271 REQUIRE(region->length >= 2);
1273 return ((region->base[0] << 8) | region->base[1]);
1276 static isc_uint8_t
1277 uint8_fromregion(isc_region_t *region) {
1279 REQUIRE(region->length >= 1);
1281 return (region->base[0]);
1284 static isc_uint8_t
1285 uint8_consume_fromregion(isc_region_t *region) {
1286 isc_uint8_t r = uint8_fromregion(region);
1288 isc_region_consume(region, 1);
1289 return r;
1292 static isc_result_t
1293 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
1294 isc_region_t tr;
1296 isc_buffer_availableregion(target, &tr);
1297 if (length > tr.length)
1298 return (ISC_R_NOSPACE);
1299 memcpy(tr.base, base, length);
1300 isc_buffer_add(target, length);
1301 return (ISC_R_SUCCESS);
1304 static int
1305 hexvalue(char value) {
1306 char *s;
1307 unsigned char c;
1309 c = (unsigned char)value;
1311 if (!isascii(c))
1312 return (-1);
1313 if (isupper(c))
1314 c = tolower(c);
1315 if ((s = strchr(hexdigits, c)) == NULL)
1316 return (-1);
1317 return (s - hexdigits);
1320 static int
1321 decvalue(char value) {
1322 char *s;
1325 * isascii() is valid for full range of int values, no need to
1326 * mask or cast.
1328 if (!isascii(value))
1329 return (-1);
1330 if ((s = strchr(decdigits, value)) == NULL)
1331 return (-1);
1332 return (s - decdigits);
1335 static const char atob_digits[86] =
1336 "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`" \
1337 "abcdefghijklmnopqrstu";
1339 * Subroutines to convert between 8 bit binary bytes and printable ASCII.
1340 * Computes the number of bytes, and three kinds of simple checksums.
1341 * Incoming bytes are collected into 32-bit words, then printed in base 85:
1342 * exp(85,5) > exp(2,32)
1343 * The ASCII characters used are between '!' and 'u';
1344 * 'z' encodes 32-bit zero; 'x' is used to mark the end of encoded data.
1346 * Originally by Paul Rutter (philabs!per) and Joe Orost (petsd!joe) for
1347 * the atob/btoa programs, released with the compress program, in mod.sources.
1348 * Modified by Mike Schwartz 8/19/86 for use in BIND.
1349 * Modified to be re-entrant 3/2/99.
1353 struct state {
1354 isc_int32_t Ceor;
1355 isc_int32_t Csum;
1356 isc_int32_t Crot;
1357 isc_int32_t word;
1358 isc_int32_t bcount;
1361 #define Ceor state->Ceor
1362 #define Csum state->Csum
1363 #define Crot state->Crot
1364 #define word state->word
1365 #define bcount state->bcount
1367 #define times85(x) ((((((x<<2)+x)<<2)+x)<<2)+x)
1369 static isc_result_t byte_atob(int c, isc_buffer_t *target,
1370 struct state *state);
1371 static isc_result_t putbyte(int c, isc_buffer_t *, struct state *state);
1372 static isc_result_t byte_btoa(int c, isc_buffer_t *, struct state *state);
1375 * Decode ASCII-encoded byte c into binary representation and
1376 * place into *bufp, advancing bufp.
1378 static isc_result_t
1379 byte_atob(int c, isc_buffer_t *target, struct state *state) {
1380 char *s;
1381 if (c == 'z') {
1382 if (bcount != 0)
1383 return(DNS_R_SYNTAX);
1384 else {
1385 RETERR(putbyte(0, target, state));
1386 RETERR(putbyte(0, target, state));
1387 RETERR(putbyte(0, target, state));
1388 RETERR(putbyte(0, target, state));
1390 } else if ((s = strchr(atob_digits, c)) != NULL) {
1391 if (bcount == 0) {
1392 word = s - atob_digits;
1393 ++bcount;
1394 } else if (bcount < 4) {
1395 word = times85(word);
1396 word += s - atob_digits;
1397 ++bcount;
1398 } else {
1399 word = times85(word);
1400 word += s - atob_digits;
1401 RETERR(putbyte((word >> 24) & 0xff, target, state));
1402 RETERR(putbyte((word >> 16) & 0xff, target, state));
1403 RETERR(putbyte((word >> 8) & 0xff, target, state));
1404 RETERR(putbyte(word & 0xff, target, state));
1405 word = 0;
1406 bcount = 0;
1408 } else
1409 return(DNS_R_SYNTAX);
1410 return(ISC_R_SUCCESS);
1414 * Compute checksum info and place c into target.
1416 static isc_result_t
1417 putbyte(int c, isc_buffer_t *target, struct state *state) {
1418 isc_region_t tr;
1420 Ceor ^= c;
1421 Csum += c;
1422 Csum += 1;
1423 if ((Crot & 0x80000000)) {
1424 Crot <<= 1;
1425 Crot += 1;
1426 } else {
1427 Crot <<= 1;
1429 Crot += c;
1430 isc_buffer_availableregion(target, &tr);
1431 if (tr.length < 1)
1432 return (ISC_R_NOSPACE);
1433 tr.base[0] = c;
1434 isc_buffer_add(target, 1);
1435 return (ISC_R_SUCCESS);
1439 * Read the ASCII-encoded data from inbuf, of length inbuflen, and convert
1440 * it into T_UNSPEC (binary data) in outbuf, not to exceed outbuflen bytes;
1441 * outbuflen must be divisible by 4. (Note: this is because outbuf is filled
1442 * in 4 bytes at a time. If the actual data doesn't end on an even 4-byte
1443 * boundary, there will be no problem...it will be padded with 0 bytes, and
1444 * numbytes will indicate the correct number of bytes. The main point is
1445 * that since the buffer is filled in 4 bytes at a time, even if there is
1446 * not a full 4 bytes of data at the end, there has to be room to 0-pad the
1447 * data, so the buffer must be of size divisible by 4). Place the number of
1448 * output bytes in numbytes, and return a failure/success status.
1451 static isc_result_t
1452 atob_tobuffer(isc_lex_t *lexer, isc_buffer_t *target) {
1453 long oeor, osum, orot;
1454 struct state statebuf, *state= &statebuf;
1455 isc_token_t token;
1456 char c;
1457 char *e;
1459 Ceor = Csum = Crot = word = bcount = 0;
1461 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1462 ISC_FALSE));
1463 while (token.value.as_textregion.length != 0) {
1464 if ((c = token.value.as_textregion.base[0]) == 'x') {
1465 break;
1466 } else
1467 RETERR(byte_atob(c, target, state));
1468 isc_textregion_consume(&token.value.as_textregion, 1);
1472 * Number of bytes.
1474 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
1475 ISC_FALSE));
1476 if ((token.value.as_ulong % 4) != 0U)
1477 isc_buffer_subtract(target, 4 - (token.value.as_ulong % 4));
1480 * Checksum.
1482 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1483 ISC_FALSE));
1484 oeor = strtol(DNS_AS_STR(token), &e, 16);
1485 if (*e != 0)
1486 return (DNS_R_SYNTAX);
1489 * Checksum.
1491 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1492 ISC_FALSE));
1493 osum = strtol(DNS_AS_STR(token), &e, 16);
1494 if (*e != 0)
1495 return (DNS_R_SYNTAX);
1498 * Checksum.
1500 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
1501 ISC_FALSE));
1502 orot = strtol(DNS_AS_STR(token), &e, 16);
1503 if (*e != 0)
1504 return (DNS_R_SYNTAX);
1506 if ((oeor != Ceor) || (osum != Csum) || (orot != Crot))
1507 return(DNS_R_BADCKSUM);
1508 return (ISC_R_SUCCESS);
1512 * Encode binary byte c into ASCII representation and place into *bufp,
1513 * advancing bufp.
1515 static isc_result_t
1516 byte_btoa(int c, isc_buffer_t *target, struct state *state) {
1517 isc_region_t tr;
1519 isc_buffer_availableregion(target, &tr);
1520 Ceor ^= c;
1521 Csum += c;
1522 Csum += 1;
1523 if ((Crot & 0x80000000)) {
1524 Crot <<= 1;
1525 Crot += 1;
1526 } else {
1527 Crot <<= 1;
1529 Crot += c;
1531 word <<= 8;
1532 word |= c;
1533 if (bcount == 3) {
1534 if (word == 0) {
1535 if (tr.length < 1)
1536 return (ISC_R_NOSPACE);
1537 tr.base[0] = 'z';
1538 isc_buffer_add(target, 1);
1539 } else {
1540 register int tmp = 0;
1541 register isc_int32_t tmpword = word;
1543 if (tmpword < 0) {
1545 * Because some don't support u_long.
1547 tmp = 32;
1548 tmpword -= (isc_int32_t)(85 * 85 * 85 * 85 * 32);
1550 if (tmpword < 0) {
1551 tmp = 64;
1552 tmpword -= (isc_int32_t)(85 * 85 * 85 * 85 * 32);
1554 if (tr.length < 5)
1555 return (ISC_R_NOSPACE);
1556 tr.base[0] = atob_digits[(tmpword /
1557 (isc_int32_t)(85 * 85 * 85 * 85))
1558 + tmp];
1559 tmpword %= (isc_int32_t)(85 * 85 * 85 * 85);
1560 tr.base[1] = atob_digits[tmpword / (85 * 85 * 85)];
1561 tmpword %= (85 * 85 * 85);
1562 tr.base[2] = atob_digits[tmpword / (85 * 85)];
1563 tmpword %= (85 * 85);
1564 tr.base[3] = atob_digits[tmpword / 85];
1565 tmpword %= 85;
1566 tr.base[4] = atob_digits[tmpword];
1567 isc_buffer_add(target, 5);
1569 bcount = 0;
1570 } else {
1571 bcount += 1;
1573 return (ISC_R_SUCCESS);
1578 * Encode the binary data from inbuf, of length inbuflen, into a
1579 * target. Return success/failure status
1581 static isc_result_t
1582 btoa_totext(unsigned char *inbuf, int inbuflen, isc_buffer_t *target) {
1583 int inc;
1584 struct state statebuf, *state = &statebuf;
1585 char buf[sizeof("x 2000000000 ffffffff ffffffff ffffffff")];
1587 Ceor = Csum = Crot = word = bcount = 0;
1588 for (inc = 0; inc < inbuflen; inbuf++, inc++)
1589 RETERR(byte_btoa(*inbuf, target, state));
1591 while (bcount != 0)
1592 RETERR(byte_btoa(0, target, state));
1595 * Put byte count and checksum information at end of buffer,
1596 * delimited by 'x'
1598 snprintf(buf, sizeof(buf), "x %d %x %x %x", inbuflen, Ceor, Csum, Crot);
1599 return (str_totext(buf, target));
1603 static void
1604 default_fromtext_callback(dns_rdatacallbacks_t *callbacks, const char *fmt,
1605 ...)
1607 va_list ap;
1609 UNUSED(callbacks);
1611 va_start(ap, fmt);
1612 vfprintf(stderr, fmt, ap);
1613 va_end(ap);
1614 fprintf(stderr, "\n");
1617 static void
1618 fromtext_warneof(isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks) {
1619 if (isc_lex_isfile(lexer) && callbacks != NULL) {
1620 const char *name = isc_lex_getsourcename(lexer);
1621 if (name == NULL)
1622 name = "UNKNOWN";
1623 (*callbacks->warn)(callbacks,
1624 "%s:%lu: file does not end with newline",
1625 name, isc_lex_getsourceline(lexer));
1629 static void
1630 warn_badmx(isc_token_t *token, isc_lex_t *lexer,
1631 dns_rdatacallbacks_t *callbacks)
1633 const char *file;
1634 unsigned long line;
1636 if (lexer != NULL) {
1637 file = isc_lex_getsourcename(lexer);
1638 line = isc_lex_getsourceline(lexer);
1639 (*callbacks->warn)(callbacks, "%s:%u: warning: '%s': %s",
1640 file, line, DNS_AS_STR(*token),
1641 dns_result_totext(DNS_R_MXISADDRESS));
1645 static void
1646 warn_badname(dns_name_t *name, isc_lex_t *lexer,
1647 dns_rdatacallbacks_t *callbacks)
1649 const char *file;
1650 unsigned long line;
1651 char namebuf[DNS_NAME_FORMATSIZE];
1653 if (lexer != NULL) {
1654 file = isc_lex_getsourcename(lexer);
1655 line = isc_lex_getsourceline(lexer);
1656 dns_name_format(name, namebuf, sizeof(namebuf));
1657 (*callbacks->warn)(callbacks, "%s:%u: warning: %s: %s",
1658 file, line, namebuf,
1659 dns_result_totext(DNS_R_BADNAME));
1663 static void
1664 fromtext_error(void (*callback)(dns_rdatacallbacks_t *, const char *, ...),
1665 dns_rdatacallbacks_t *callbacks, const char *name,
1666 unsigned long line, isc_token_t *token, isc_result_t result)
1668 if (name == NULL)
1669 name = "UNKNOWN";
1671 if (token != NULL) {
1672 switch (token->type) {
1673 case isc_tokentype_eol:
1674 (*callback)(callbacks, "%s: %s:%lu: near eol: %s",
1675 "dns_rdata_fromtext", name, line,
1676 dns_result_totext(result));
1677 break;
1678 case isc_tokentype_eof:
1679 (*callback)(callbacks, "%s: %s:%lu: near eof: %s",
1680 "dns_rdata_fromtext", name, line,
1681 dns_result_totext(result));
1682 break;
1683 case isc_tokentype_number:
1684 (*callback)(callbacks, "%s: %s:%lu: near %lu: %s",
1685 "dns_rdata_fromtext", name, line,
1686 token->value.as_ulong,
1687 dns_result_totext(result));
1688 break;
1689 case isc_tokentype_string:
1690 case isc_tokentype_qstring:
1691 (*callback)(callbacks, "%s: %s:%lu: near '%s': %s",
1692 "dns_rdata_fromtext", name, line,
1693 DNS_AS_STR(*token),
1694 dns_result_totext(result));
1695 break;
1696 default:
1697 (*callback)(callbacks, "%s: %s:%lu: %s",
1698 "dns_rdata_fromtext", name, line,
1699 dns_result_totext(result));
1700 break;
1702 } else {
1703 (*callback)(callbacks, "dns_rdata_fromtext: %s:%lu: %s",
1704 name, line, dns_result_totext(result));
1708 dns_rdatatype_t
1709 dns_rdata_covers(dns_rdata_t *rdata) {
1710 if (rdata->type == 46)
1711 return (covers_rrsig(rdata));
1712 return (covers_sig(rdata));
1715 isc_boolean_t
1716 dns_rdatatype_ismeta(dns_rdatatype_t type) {
1717 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_META) != 0)
1718 return (ISC_TRUE);
1719 return (ISC_FALSE);
1722 isc_boolean_t
1723 dns_rdatatype_issingleton(dns_rdatatype_t type) {
1724 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_SINGLETON)
1725 != 0)
1726 return (ISC_TRUE);
1727 return (ISC_FALSE);
1730 isc_boolean_t
1731 dns_rdatatype_notquestion(dns_rdatatype_t type) {
1732 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_NOTQUESTION)
1733 != 0)
1734 return (ISC_TRUE);
1735 return (ISC_FALSE);
1738 isc_boolean_t
1739 dns_rdatatype_questiononly(dns_rdatatype_t type) {
1740 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_QUESTIONONLY)
1741 != 0)
1742 return (ISC_TRUE);
1743 return (ISC_FALSE);
1746 isc_boolean_t
1747 dns_rdatatype_atparent(dns_rdatatype_t type) {
1748 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_ATPARENT) != 0)
1749 return (ISC_TRUE);
1750 return (ISC_FALSE);
1753 isc_boolean_t
1754 dns_rdataclass_ismeta(dns_rdataclass_t rdclass) {
1756 if (rdclass == dns_rdataclass_reserved0
1757 || rdclass == dns_rdataclass_none
1758 || rdclass == dns_rdataclass_any)
1759 return (ISC_TRUE);
1761 return (ISC_FALSE); /* Assume it is not a meta class. */
1764 isc_boolean_t
1765 dns_rdatatype_isdnssec(dns_rdatatype_t type) {
1766 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_DNSSEC) != 0)
1767 return (ISC_TRUE);
1768 return (ISC_FALSE);
1771 isc_boolean_t
1772 dns_rdatatype_iszonecutauth(dns_rdatatype_t type) {
1773 if ((dns_rdatatype_attributes(type)
1774 & (DNS_RDATATYPEATTR_DNSSEC | DNS_RDATATYPEATTR_ZONECUTAUTH))
1775 != 0)
1776 return (ISC_TRUE);
1777 return (ISC_FALSE);
1780 isc_boolean_t
1781 dns_rdatatype_isknown(dns_rdatatype_t type) {
1782 if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_UNKNOWN)
1783 == 0)
1784 return (ISC_TRUE);
1785 return (ISC_FALSE);
1788 void
1789 dns_rdata_exists(dns_rdata_t *rdata, dns_rdatatype_t type) {
1791 REQUIRE(rdata != NULL);
1792 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
1794 rdata->data = NULL;
1795 rdata->length = 0;
1796 rdata->flags = DNS_RDATA_UPDATE;
1797 rdata->type = type;
1798 rdata->rdclass = dns_rdataclass_any;
1801 void
1802 dns_rdata_notexist(dns_rdata_t *rdata, dns_rdatatype_t type) {
1804 REQUIRE(rdata != NULL);
1805 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
1807 rdata->data = NULL;
1808 rdata->length = 0;
1809 rdata->flags = DNS_RDATA_UPDATE;
1810 rdata->type = type;
1811 rdata->rdclass = dns_rdataclass_none;
1814 void
1815 dns_rdata_deleterrset(dns_rdata_t *rdata, dns_rdatatype_t type) {
1817 REQUIRE(rdata != NULL);
1818 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
1820 rdata->data = NULL;
1821 rdata->length = 0;
1822 rdata->flags = DNS_RDATA_UPDATE;
1823 rdata->type = type;
1824 rdata->rdclass = dns_rdataclass_any;
1827 void
1828 dns_rdata_makedelete(dns_rdata_t *rdata) {
1829 REQUIRE(rdata != NULL);
1831 rdata->rdclass = dns_rdataclass_none;
1834 const char *
1835 dns_rdata_updateop(dns_rdata_t *rdata, dns_section_t section) {
1837 REQUIRE(rdata != NULL);
1838 REQUIRE(DNS_RDATA_INITIALIZED(rdata));
1840 switch (section) {
1841 case DNS_SECTION_PREREQUISITE:
1842 switch (rdata->rdclass) {
1843 case dns_rdataclass_none:
1844 switch (rdata->type) {
1845 case dns_rdatatype_any:
1846 return ("domain doesn't exist");
1847 default:
1848 return ("rrset doesn't exist");
1850 case dns_rdataclass_any:
1851 switch (rdata->type) {
1852 case dns_rdatatype_any:
1853 return ("domain exists");
1854 default:
1855 return ("rrset exists (value independent)");
1857 default:
1858 return ("rrset exists (value dependent)");
1860 case DNS_SECTION_UPDATE:
1861 switch (rdata->rdclass) {
1862 case dns_rdataclass_none:
1863 return ("delete");
1864 case dns_rdataclass_any:
1865 switch (rdata->type) {
1866 case dns_rdatatype_any:
1867 return ("delete all rrsets");
1868 default:
1869 return ("delete rrset");
1871 default:
1872 return ("add");
1875 return ("invalid");