1 /* $NetBSD: ns_print.c,v 1.9 2009/04/12 17:07:17 christos Exp $ */
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996-1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and 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
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/cdefs.h>
23 static const char rcsid
[] = "Id: ns_print.c,v 1.12 2009/03/03 05:29:58 each Exp";
25 __RCSID("$NetBSD: ns_print.c,v 1.9 2009/04/12 17:07:17 christos Exp $");
31 #include "port_before.h"
33 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/nameser.h>
38 #include <arpa/inet.h>
40 #include <isc/assertions.h>
47 #include "port_after.h"
50 # define SPRINTF(x) strlen(sprintf/**/x)
52 # define SPRINTF(x) ((size_t)sprintf x)
57 static size_t prune_origin(const char *name
, const char *origin
);
58 static int charstr(const u_char
*rdata
, const u_char
*edata
,
59 char **buf
, size_t *buflen
);
60 static int addname(const u_char
*msg
, size_t msglen
,
61 const u_char
**p
, const char *origin
,
62 char **buf
, size_t *buflen
);
63 static void addlen(size_t len
, char **buf
, size_t *buflen
);
64 static int addstr(const char *src
, size_t len
,
65 char **buf
, size_t *buflen
);
66 static int addtab(size_t len
, size_t target
, int spaced
,
67 char **buf
, size_t *buflen
);
75 } while (/*CONSTCOND*/0)
77 static const char base32hex
[] =
78 "0123456789ABCDEFGHIJKLMNOPQRSTUV=0123456789abcdefghijklmnopqrstuv";
83 * Convert an RR to presentation format.
86 *\li Number of characters written to buf, or -1 (check errno).
89 ns_sprintrr(const ns_msg
*handle
, const ns_rr
*rr
,
90 const char *name_ctx
, const char *origin
,
91 char *buf
, size_t buflen
)
95 n
= ns_sprintrrf(ns_msg_base(*handle
), ns_msg_size(*handle
),
96 ns_rr_name(*rr
), ns_rr_class(*rr
), ns_rr_type(*rr
),
97 ns_rr_ttl(*rr
), ns_rr_rdata(*rr
), ns_rr_rdlen(*rr
),
98 name_ctx
, origin
, buf
, buflen
);
103 * Convert the fields of an RR into presentation format.
106 *\li Number of characters written to buf, or -1 (check errno).
109 ns_sprintrrf(const u_char
*msg
, size_t msglen
,
110 const char *name
, ns_class
class, ns_type type
,
111 u_long ttl
, const u_char
*rdata
, size_t rdlen
,
112 const char *name_ctx
, const char *origin
,
113 char *buf
, size_t buflen
)
115 const char *obuf
= buf
;
116 const u_char
*edata
= rdata
+ rdlen
;
126 if (name_ctx
!= NULL
&& ns_samename(name_ctx
, name
) == 1) {
127 T(addstr("\t\t\t", (size_t)3, &buf
, &buflen
));
129 len
= prune_origin(name
, origin
);
132 } else if (len
== 0) {
133 T(addstr("@\t\t\t", (size_t)4, &buf
, &buflen
));
135 T(addstr(name
, (size_t)len
, &buf
, &buflen
));
136 /* Origin not used or not root, and no trailing dot? */
137 if (((origin
== NULL
|| origin
[0] == '\0') ||
138 (origin
[0] != '.' && origin
[1] != '\0' &&
139 name
[len
] == '\0')) && name
[len
- 1] != '.') {
141 T(addstr(".", (size_t)1, &buf
, &buflen
));
144 T(spaced
= addtab((size_t)len
, 24, spaced
, &buf
, &buflen
));
151 T(x
= ns_format_ttl(ttl
, buf
, buflen
));
152 addlen((size_t)x
, &buf
, &buflen
);
153 len
= SPRINTF((tmp
, " %s %s", p_class(class), p_type(type
)));
154 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
155 T(spaced
= addtab((size_t)(x
+ len
), (size_t)16, spaced
, &buf
, &buflen
));
162 if (rdlen
!= (size_t)NS_INADDRSZ
)
164 (void) inet_ntop(AF_INET
, rdata
, buf
, buflen
);
165 addlen(strlen(buf
), &buf
, &buflen
);
175 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
181 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
185 T(addstr(" ", (size_t)1, &buf
, &buflen
));
188 /* Second word, optional in ISDN records. */
189 if (type
== ns_t_isdn
&& rdata
== edata
)
192 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
202 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
203 T(addstr(" ", (size_t)1, &buf
, &buflen
));
205 /* Administrator name. */
206 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
207 T(addstr(" (\n", (size_t)3, &buf
, &buflen
));
210 if ((edata
- rdata
) != 5*NS_INT32SZ
)
214 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
215 T(addstr("\t\t\t\t\t", (size_t)5, &buf
, &buflen
));
216 len
= SPRINTF((tmp
, "%lu", t
));
217 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
218 T(spaced
= addtab((size_t)len
, (size_t)16, spaced
, &buf
, &buflen
));
219 T(addstr("; serial\n", (size_t)9, &buf
, &buflen
));
222 /* Refresh interval. */
223 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
224 T(addstr("\t\t\t\t\t", (size_t)5, &buf
, &buflen
));
225 T(len
= ns_format_ttl(t
, buf
, buflen
));
226 addlen((size_t)len
, &buf
, &buflen
);
227 T(spaced
= addtab((size_t)len
, (size_t)16, spaced
, &buf
, &buflen
));
228 T(addstr("; refresh\n", (size_t)10, &buf
, &buflen
));
231 /* Retry interval. */
232 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
233 T(addstr("\t\t\t\t\t", (size_t)5, &buf
, &buflen
));
234 T(len
= ns_format_ttl(t
, buf
, buflen
));
235 addlen((size_t)len
, &buf
, &buflen
);
236 T(spaced
= addtab((size_t)len
, (size_t)16, spaced
, &buf
, &buflen
));
237 T(addstr("; retry\n", (size_t)8, &buf
, &buflen
));
241 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
242 T(addstr("\t\t\t\t\t", (size_t)5, &buf
, &buflen
));
243 T(len
= ns_format_ttl(t
, buf
, buflen
));
244 addlen((size_t)len
, &buf
, &buflen
);
245 T(spaced
= addtab((size_t)len
, (size_t)16, spaced
, &buf
, &buflen
));
246 T(addstr("; expiry\n", (size_t)9, &buf
, &buflen
));
250 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
251 T(addstr("\t\t\t\t\t", (size_t)5, &buf
, &buflen
));
252 T(len
= ns_format_ttl(t
, buf
, buflen
));
253 addlen((size_t)len
, &buf
, &buflen
);
254 T(addstr(" )", (size_t)2, &buf
, &buflen
));
255 T(spaced
= addtab((size_t)len
, (size_t)16, spaced
, &buf
, &buflen
));
256 T(addstr("; minimum\n", (size_t)10, &buf
, &buflen
));
267 if (rdlen
< (size_t)NS_INT16SZ
)
273 len
= SPRINTF((tmp
, "%u ", t
));
274 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
277 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
285 if (rdlen
< (size_t)NS_INT16SZ
)
291 len
= SPRINTF((tmp
, "%u ", t
));
292 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
295 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
296 T(addstr(" ", (size_t)1, &buf
, &buflen
));
299 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
305 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
313 while (rdata
< edata
) {
314 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
319 T(addstr(" ", (size_t)1, &buf
, &buflen
));
326 (void) inet_nsap_ntoa((int)rdlen
, rdata
, t
);
327 T(addstr(t
, strlen(t
), &buf
, &buflen
));
332 if (rdlen
!= (size_t)NS_IN6ADDRSZ
)
334 (void) inet_ntop(AF_INET6
, rdata
, buf
, buflen
);
335 addlen(strlen(buf
), &buf
, &buflen
);
341 /* XXX protocol format checking? */
342 (void) loc_ntoa(rdata
, t
);
343 T(addstr(t
, strlen(t
), &buf
, &buflen
));
348 u_int order
, preference
;
351 if (rdlen
< 2U*NS_INT16SZ
)
354 /* Order, Precedence. */
355 order
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
356 preference
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
357 len
= SPRINTF((t
, "%u %u ", order
, preference
));
358 T(addstr(t
, (size_t)len
, &buf
, &buflen
));
361 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
365 T(addstr(" ", (size_t)1, &buf
, &buflen
));
368 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
372 T(addstr(" ", (size_t)1, &buf
, &buflen
));
375 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
381 T(addstr(" ", (size_t)1, &buf
, &buflen
));
384 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
389 u_int priority
, weight
, port
;
392 if (rdlen
< 3U*NS_INT16SZ
)
395 /* Priority, Weight, Port. */
396 priority
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
397 weight
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
398 port
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
399 len
= SPRINTF((t
, "%u %u %u ", priority
, weight
, port
));
400 T(addstr(t
, (size_t)len
, &buf
, &buflen
));
403 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
410 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
411 T(addstr(" ", (size_t)1, &buf
, &buflen
));
414 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
421 if (rdlen
< 1U + NS_INT32SZ
)
425 (void) inet_ntop(AF_INET
, rdata
, buf
, buflen
);
426 addlen(strlen(buf
), &buf
, &buflen
);
427 rdata
+= NS_INADDRSZ
;
430 len
= SPRINTF((tmp
, " %u ( ", *rdata
));
431 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
437 while (rdata
< edata
) {
442 T(addstr("\n\t\t\t\t", (size_t)5,
447 len
= SPRINTF((tmp
, "%d ", n
));
448 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
454 T(addstr(")", (size_t)1, &buf
, &buflen
));
461 char base64_key
[NS_MD5RSA_MAX_BASE64
];
462 u_int keyflags
, protocol
, algorithm
, key_id
;
466 if (rdlen
< 0U + NS_INT16SZ
+ NS_INT8SZ
+ NS_INT8SZ
)
469 /* Key flags, Protocol, Algorithm. */
471 key_id
= dst_s_dns_key_id(rdata
, edata
-rdata
);
475 keyflags
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
477 algorithm
= *rdata
++;
478 len
= SPRINTF((tmp
, "0x%04x %u %u",
479 keyflags
, protocol
, algorithm
));
480 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
482 /* Public key data. */
483 len
= b64_ntop(rdata
, (size_t)(edata
- rdata
),
484 base64_key
, sizeof base64_key
);
488 T(addstr(" (", (size_t)2, &buf
, &buflen
));
493 for (n
= 0; n
< len
; n
+= 48) {
494 T(addstr(leader
, strlen(leader
), &buf
, &buflen
));
495 T(addstr(base64_key
+ n
, (size_t)MIN(len
- n
, 48),
499 T(addstr(" )", (size_t)2, &buf
, &buflen
));
500 n
= SPRINTF((tmp
, " ; key_tag= %u", key_id
));
501 T(addstr(tmp
, (size_t)n
, &buf
, &buflen
));
508 char base64_key
[NS_MD5RSA_MAX_BASE64
];
509 u_int typ
, algorithm
, labels
, footprint
;
517 /* Type covered, Algorithm, Label count, Original TTL. */
518 typ
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
519 algorithm
= *rdata
++;
521 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
522 len
= SPRINTF((tmp
, "%s %d %d %lu ",
523 p_type((int)typ
), algorithm
, labels
, t
));
524 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
525 if (labels
> (u_int
)dn_count_labels(name
))
528 /* Signature expiry. */
529 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
530 len
= SPRINTF((tmp
, "%s ", p_secstodate(t
)));
531 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
534 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
535 len
= SPRINTF((tmp
, "%s ", p_secstodate(t
)));
536 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
538 /* Signature Footprint. */
539 footprint
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
540 len
= SPRINTF((tmp
, "%u ", footprint
));
541 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
544 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
547 len
= b64_ntop(rdata
, (size_t)(edata
- rdata
),
548 base64_key
, sizeof base64_key
);
550 T(addstr(" (", (size_t)2, &buf
, &buflen
));
557 for (n
= 0; n
< len
; n
+= 48) {
558 T(addstr(leader
, strlen(leader
), &buf
, &buflen
));
559 T(addstr(base64_key
+ n
, (size_t)MIN(len
- n
, 48),
563 T(addstr(" )", (size_t)2, &buf
, &buflen
));
570 /* Next domain name. */
571 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
575 for (c
= 0; c
< n
*8; c
++)
576 if (NS_NXT_BIT_ISSET(c
, rdata
)) {
577 len
= SPRINTF((tmp
, " %s", p_type(c
)));
578 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
584 u_int c_type
, key_tag
, alg
;
587 char base64_cert
[8192], tmp1
[40];
590 c_type
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
591 key_tag
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
592 alg
= (u_int
) *rdata
++;
594 len
= SPRINTF((tmp1
, "%d %d %d ", c_type
, key_tag
, alg
));
595 T(addstr(tmp1
, (size_t)len
, &buf
, &buflen
));
596 siz
= (edata
-rdata
)*4/3 + 4; /* "+4" accounts for trailing \0 */
597 if (siz
> sizeof(base64_cert
) * 3/4) {
598 const char *str
= "record too long to print";
599 T(addstr(str
, strlen(str
), &buf
, &buflen
));
602 len
= b64_ntop(rdata
, (size_t)(edata
-rdata
),
608 T(addstr(" (", (size_t)2, &buf
, &buflen
));
615 for (n
= 0; n
< len
; n
+= 48) {
616 T(addstr(leader
, strlen(leader
),
618 T(addstr(base64_cert
+ n
, (size_t)MIN(len
- n
, 48),
622 T(addstr(" )", (size_t)2, &buf
, &buflen
));
628 /* KJD - need to complete this */
630 int mode
, err
, keysize
;
632 /* Algorithm name. */
633 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
634 T(addstr(" ", (size_t)1, &buf
, &buflen
));
637 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
638 len
= SPRINTF((tmp
, "%s ", p_secstodate(t
)));
639 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
642 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
643 len
= SPRINTF((tmp
, "%s ", p_secstodate(t
)));
644 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
646 /* Mode , Error, Key Size. */
647 /* Priority, Weight, Port. */
648 mode
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
649 err
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
650 keysize
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
651 len
= SPRINTF((tmp
, "%u %u %u ", mode
, err
, keysize
));
652 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
654 /* XXX need to dump key, print otherdata length & other data */
659 /* BEW - need to complete this */
662 T(len
= addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
663 T(addstr(" ", (size_t)1, &buf
, &buflen
));
664 rdata
+= 8; /*%< time */
665 n
= ns_get16(rdata
); rdata
+= INT16SZ
;
666 rdata
+= n
; /*%< sig */
667 n
= ns_get16(rdata
); rdata
+= INT16SZ
; /*%< original id */
668 sprintf(buf
, "%d", ns_get16(rdata
));
670 addlen(strlen(buf
), &buf
, &buflen
);
679 if (rdlen
== 0U) goto formerr
;
680 len
= SPRINTF((tmp
, "%d ", *rdata
));
681 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
683 if (pbit
> 128) goto formerr
;
684 pbyte
= (pbit
& ~7) / 8;
687 /* address suffix: provided only when prefix len != 128 */
689 if (rdata
+ pbyte
>= edata
) goto formerr
;
690 memset(&a
, 0, sizeof(a
));
691 memcpy(&a
.s6_addr
[pbyte
], rdata
, sizeof(a
) - pbyte
);
692 (void) inet_ntop(AF_INET6
, &a
, buf
, buflen
);
693 addlen(strlen(buf
), &buf
, &buflen
);
694 rdata
+= sizeof(a
) - pbyte
;
697 /* prefix name: provided only when prefix len > 0 */
700 if (rdata
>= edata
) goto formerr
;
701 T(addstr(" ", (size_t)1, &buf
, &buflen
));
702 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
708 len
= SPRINTF((tmp
, "%u bytes", class));
709 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
718 if (type
== ns_t_ds
|| type
== ns_t_dlv
) {
719 if (rdlen
< 4U) goto formerr
;
722 len
= SPRINTF((tmp
, "%u ", t
));
723 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
725 if (rdlen
< 2U) goto formerr
;
727 len
= SPRINTF((tmp
, "%u ", *rdata
));
728 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
731 len
= SPRINTF((tmp
, "%u ", *rdata
));
732 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
735 while (rdata
< edata
) {
736 len
= SPRINTF((tmp
, "%02X", *rdata
));
737 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
744 case ns_t_nsec3param
: {
745 u_int t
, w
, l
, j
, k
, c
;
747 len
= SPRINTF((tmp
, "%u ", *rdata
));
748 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
751 len
= SPRINTF((tmp
, "%u ", *rdata
));
752 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
757 len
= SPRINTF((tmp
, "%u ", t
));
758 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
762 T(addstr("-", 1, &buf
, &buflen
));
765 len
= SPRINTF((tmp
, "%02X", *rdata
));
766 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
770 if (type
== ns_t_nsec3param
)
772 T(addstr(" ", 1, &buf
, &buflen
));
778 tmp
[0] = base32hex
[(((uint32_t)rdata
[0]>>3)&0x1f)];
779 tmp
[1] = base32hex
[(((uint32_t)rdata
[0]<<2)&0x1c)];
780 tmp
[2] = tmp
[3] = tmp
[4] = '=';
781 tmp
[5] = tmp
[6] = tmp
[7] = '=';
784 tmp
[0] = base32hex
[(((uint32_t)rdata
[0]>>3)&0x1f)];
785 tmp
[1] = base32hex
[(((uint32_t)rdata
[0]<<2)&0x1c)|
786 (((uint32_t)rdata
[1]>>6)&0x03)];
787 tmp
[2] = base32hex
[(((uint32_t)rdata
[1]>>1)&0x1f)];
788 tmp
[3] = base32hex
[(((uint32_t)rdata
[1]<<4)&0x10)];
789 tmp
[4] = tmp
[5] = tmp
[6] = tmp
[7] = '=';
792 tmp
[0] = base32hex
[(((uint32_t)rdata
[0]>>3)&0x1f)];
793 tmp
[1] = base32hex
[(((uint32_t)rdata
[0]<<2)&0x1c)|
794 (((uint32_t)rdata
[1]>>6)&0x03)];
795 tmp
[2] = base32hex
[(((uint32_t)rdata
[1]>>1)&0x1f)];
796 tmp
[3] = base32hex
[(((uint32_t)rdata
[1]<<4)&0x10)|
797 (((uint32_t)rdata
[2]>>4)&0x0f)];
798 tmp
[4] = base32hex
[(((uint32_t)rdata
[2]<<1)&0x1e)];
799 tmp
[5] = tmp
[6] = tmp
[7] = '=';
802 tmp
[0] = base32hex
[(((uint32_t)rdata
[0]>>3)&0x1f)];
803 tmp
[1] = base32hex
[(((uint32_t)rdata
[0]<<2)&0x1c)|
804 (((uint32_t)rdata
[1]>>6)&0x03)];
805 tmp
[2] = base32hex
[(((uint32_t)rdata
[1]>>1)&0x1f)];
806 tmp
[3] = base32hex
[(((uint32_t)rdata
[1]<<4)&0x10)|
807 (((uint32_t)rdata
[2]>>4)&0x0f)];
808 tmp
[4] = base32hex
[(((uint32_t)rdata
[2]<<1)&0x1e)|
809 (((uint32_t)rdata
[3]>>7)&0x01)];
810 tmp
[5] = base32hex
[(((uint32_t)rdata
[3]>>2)&0x1f)];
811 tmp
[6] = base32hex
[((uint32_t)rdata
[3]<<3)&0x18];
815 tmp
[0] = base32hex
[(((uint32_t)rdata
[0]>>3)&0x1f)];
816 tmp
[1] = base32hex
[(((uint32_t)rdata
[0]<<2)&0x1c)|
817 (((uint32_t)rdata
[1]>>6)&0x03)];
818 tmp
[2] = base32hex
[(((uint32_t)rdata
[1]>>1)&0x1f)];
819 tmp
[3] = base32hex
[(((uint32_t)rdata
[1]<<4)&0x10)|
820 (((uint32_t)rdata
[2]>>4)&0x0f)];
821 tmp
[4] = base32hex
[(((uint32_t)rdata
[2]<<1)&0x1e)|
822 (((uint32_t)rdata
[3]>>7)&0x01)];
823 tmp
[5] = base32hex
[(((uint32_t)rdata
[3]>>2)&0x1f)];
824 tmp
[6] = base32hex
[(((uint32_t)rdata
[3]<<3)&0x18)|
825 (((uint32_t)rdata
[4]>>5)&0x07)];
826 tmp
[7] = base32hex
[(rdata
[4]&0x1f)];
829 T(addstr(tmp
, 8, &buf
, &buflen
));
839 while (rdata
< edata
) {
842 for (j
= 0; j
< l
; j
++) {
845 for (k
= 0; k
< 8; k
++) {
846 if ((rdata
[j
] & (0x80 >> k
)) == 0)
848 c
= w
* 256 + j
* 8 + k
;
849 len
= SPRINTF((tmp
, " %s", p_type((ns_type
)c
)));
850 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
861 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
863 while (rdata
< edata
) {
866 for (j
= 0; j
< l
; j
++) {
869 for (k
= 0; k
< 8; k
++) {
870 if ((rdata
[j
] & (0x80 >> k
)) == 0)
872 c
= w
* 256 + j
* 8 + k
;
873 len
= SPRINTF((tmp
, " %s", p_type((ns_type
)c
)));
874 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
885 char base64_dhcid
[8192];
888 siz
= (edata
-rdata
)*4/3 + 4; /* "+4" accounts for trailing \0 */
889 if (siz
> sizeof(base64_dhcid
) * 3/4) {
890 const char *str
= "record too long to print";
891 T(addstr(str
, strlen(str
), &buf
, &buflen
));
893 len
= b64_ntop(rdata
, (size_t)(edata
-rdata
),
900 T(addstr(" (", 2, &buf
, &buflen
));
907 for (n
= 0; n
< len
; n
+= 48) {
908 T(addstr(leader
, strlen(leader
),
910 T(addstr(base64_dhcid
+ n
,
911 (size_t)MIN(len
- n
, 48), &buf
, &buflen
));
914 T(addstr(" )", 2, &buf
, &buflen
));
919 case ns_t_ipseckey
: {
922 char base64_key
[8192];
943 comment
= "unknown IPSECKEY gateway type";
947 len
= SPRINTF((tmp
, "%u ", *rdata
));
948 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
951 len
= SPRINTF((tmp
, "%u ", *rdata
));
952 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
955 len
= SPRINTF((tmp
, "%u ", *rdata
));
956 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
961 T(addstr(".", 1, &buf
, &buflen
));
964 (void) inet_ntop(AF_INET
, rdata
, buf
, buflen
);
965 addlen(strlen(buf
), &buf
, &buflen
);
969 (void) inet_ntop(AF_INET6
, rdata
, buf
, buflen
);
970 addlen(strlen(buf
), &buf
, &buflen
);
974 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
981 siz
= (edata
-rdata
)*4/3 + 4; /* "+4" accounts for trailing \0 */
982 if (siz
> sizeof(base64_key
) * 3/4) {
983 const char *str
= "record too long to print";
984 T(addstr(str
, strlen(str
), &buf
, &buflen
));
986 len
= b64_ntop(rdata
, (size_t)(edata
-rdata
),
993 T(addstr(" (", 2, &buf
, &buflen
));
1000 for (n
= 0; n
< len
; n
+= 48) {
1001 T(addstr(leader
, strlen(leader
),
1003 T(addstr(base64_key
+ n
,
1004 (size_t)MIN(len
- n
, 48), &buf
, &buflen
));
1007 T(addstr(" )", 2, &buf
, &buflen
));
1013 unsigned int i
, hip_len
, algorithm
, key_len
;
1014 char base64_key
[NS_MD5RSA_MAX_BASE64
];
1016 const char *leader
= "\n\t\t\t\t\t";
1019 algorithm
= *rdata
++;
1020 key_len
= ns_get16(rdata
);
1021 rdata
+= NS_INT16SZ
;
1023 siz
= key_len
*4/3 + 4; /* "+4" accounts for trailing \0 */
1024 if (siz
> sizeof(base64_key
) * 3/4) {
1025 const char *str
= "record too long to print";
1026 T(addstr(str
, strlen(str
), &buf
, &buflen
));
1028 len
= sprintf(tmp
, "( %u ", algorithm
);
1029 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
1031 for (i
= 0; i
< hip_len
; i
++) {
1032 len
= sprintf(tmp
, "%02X", *rdata
);
1033 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
1036 T(addstr(leader
, strlen(leader
), &buf
, &buflen
));
1038 len
= b64_ntop(rdata
, key_len
, base64_key
, siz
);
1042 T(addstr(base64_key
, (size_t)len
, &buf
, &buflen
));
1045 while (rdata
< edata
) {
1046 T(addstr(leader
, strlen(leader
), &buf
, &buflen
));
1047 T(addname(msg
, msglen
, &rdata
, origin
,
1050 T(addstr(" )", 2, &buf
, &buflen
));
1056 comment
= "unknown RR type";
1059 return (buf
- obuf
);
1061 comment
= "RR format error";
1066 len
= SPRINTF((tmp
, "\\# %u%s\t; %s", (unsigned)(edata
- rdata
),
1067 rdlen
!= 0U ? " (" : "", comment
));
1068 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
1069 while (rdata
< edata
) {
1071 p
+= SPRINTF((p
, "\n\t"));
1073 n
= MIN(16, edata
- rdata
);
1074 for (m
= 0; m
< n
; m
++)
1075 p
+= SPRINTF((p
, "%02x ", rdata
[m
]));
1076 T(addstr(tmp
, (size_t)(p
- tmp
), &buf
, &buflen
));
1078 T(addstr(")", (size_t)1, &buf
, &buflen
));
1079 T(addtab((size_t)(p
- tmp
+ 1), (size_t)48, spaced
, &buf
, &buflen
));
1082 p
+= SPRINTF((p
, "; "));
1083 for (m
= 0; m
< n
; m
++)
1084 *p
++ = (isascii(rdata
[m
]) && isprint(rdata
[m
]))
1087 T(addstr(tmp
, (size_t)(p
- tmp
), &buf
, &buflen
));
1090 return (buf
- obuf
);
1098 * prune_origin(name, origin)
1099 * Find out if the name is at or under the current origin.
1101 * Number of characters in name before start of origin,
1102 * or length of name if origin does not match.
1104 * This function should share code with samedomain().
1107 prune_origin(const char *name
, const char *origin
) {
1108 const char *oname
= name
;
1110 while (*name
!= '\0') {
1111 if (origin
!= NULL
&& ns_samename(name
, origin
) == 1)
1112 return (name
- oname
- (name
> oname
));
1113 while (*name
!= '\0') {
1114 if (*name
== '\\') {
1116 /* XXX need to handle \nnn form. */
1119 } else if (*name
== '.') {
1126 return (name
- oname
);
1131 * charstr(rdata, edata, buf, buflen)
1132 * Format a <character-string> into the presentation buffer.
1134 * Number of rdata octets consumed
1135 * 0 for protocol format error
1136 * -1 for output buffer error
1138 * buffer is advanced on success.
1141 charstr(const u_char
*rdata
, const u_char
*edata
, char **buf
, size_t *buflen
) {
1142 const u_char
*odata
= rdata
;
1143 size_t save_buflen
= *buflen
;
1144 char *save_buf
= *buf
;
1146 if (addstr("\"", (size_t)1, buf
, buflen
) < 0)
1148 if (rdata
< edata
) {
1151 if (rdata
+ 1 + n
<= edata
) {
1154 if (strchr("\n\"\\", *rdata
) != NULL
)
1155 if (addstr("\\", (size_t)1, buf
, buflen
) < 0)
1157 if (addstr((const char *)rdata
, (size_t)1,
1164 if (addstr("\"", (size_t)1, buf
, buflen
) < 0)
1166 return (rdata
- odata
);
1170 *buflen
= save_buflen
;
1175 addname(const u_char
*msg
, size_t msglen
,
1176 const u_char
**pp
, const char *origin
,
1177 char **buf
, size_t *buflen
)
1179 size_t newlen
, save_buflen
= *buflen
;
1180 char *save_buf
= *buf
;
1183 n
= dn_expand(msg
, msg
+ msglen
, *pp
, *buf
, (int)*buflen
);
1185 goto enospc
; /*%< Guess. */
1186 newlen
= prune_origin(*buf
, origin
);
1187 if (**buf
== '\0') {
1189 } else if (newlen
== 0U) {
1190 /* Use "@" instead of name. */
1191 if (newlen
+ 2 > *buflen
)
1192 goto enospc
; /* No room for "@\0". */
1193 (*buf
)[newlen
++] = '@';
1194 (*buf
)[newlen
] = '\0';
1196 if (((origin
== NULL
|| origin
[0] == '\0') ||
1197 (origin
[0] != '.' && origin
[1] != '\0' &&
1198 (*buf
)[newlen
] == '\0')) && (*buf
)[newlen
- 1] != '.') {
1199 /* No trailing dot. */
1201 if (newlen
+ 2 > *buflen
)
1202 goto enospc
; /* No room for ".\0". */
1203 (*buf
)[newlen
++] = '.';
1204 (*buf
)[newlen
] = '\0';
1208 addlen(newlen
, buf
, buflen
);
1214 *buflen
= save_buflen
;
1219 addlen(size_t len
, char **buf
, size_t *buflen
) {
1220 INSIST(len
<= *buflen
);
1226 addstr(const char *src
, size_t len
, char **buf
, size_t *buflen
) {
1227 if (len
>= *buflen
) {
1231 memcpy(*buf
, src
, len
);
1232 addlen(len
, buf
, buflen
);
1238 addtab(size_t len
, size_t target
, int spaced
, char **buf
, size_t *buflen
) {
1239 size_t save_buflen
= *buflen
;
1240 char *save_buf
= *buf
;
1243 if (spaced
|| len
>= target
- 1) {
1244 T(addstr(" ", (size_t)2, buf
, buflen
));
1247 for (t
= (target
- len
- 1) / 8; t
>= 0; t
--)
1248 if (addstr("\t", (size_t)1, buf
, buflen
) < 0) {
1249 *buflen
= save_buflen
;