1 /* $NetBSD: ns_print.c,v 1.11 2012/03/13 21:13:39 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.11 2012/03/13 21:13:39 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>
49 #include "port_after.h"
52 # define SPRINTF(x) ((int)strlen(sprintf/**/x))
54 # define SPRINTF(x) (sprintf x)
59 static size_t prune_origin(const char *name
, const char *origin
);
60 static int charstr(const u_char
*rdata
, const u_char
*edata
,
61 char **buf
, size_t *buflen
);
62 static int addname(const u_char
*msg
, size_t msglen
,
63 const u_char
**p
, const char *origin
,
64 char **buf
, size_t *buflen
);
65 static void addlen(size_t len
, char **buf
, size_t *buflen
);
66 static int addstr(const char *src
, size_t len
,
67 char **buf
, size_t *buflen
);
68 static int addtab(size_t len
, size_t target
, int spaced
,
69 char **buf
, size_t *buflen
);
77 } while (/*CONSTCOND*/0)
79 static const char base32hex
[] =
80 "0123456789ABCDEFGHIJKLMNOPQRSTUV=0123456789abcdefghijklmnopqrstuv";
85 * Convert an RR to presentation format.
88 *\li Number of characters written to buf, or -1 (check errno).
91 ns_sprintrr(const ns_msg
*handle
, const ns_rr
*rr
,
92 const char *name_ctx
, const char *origin
,
93 char *buf
, size_t buflen
)
97 n
= ns_sprintrrf(ns_msg_base(*handle
), ns_msg_size(*handle
),
98 ns_rr_name(*rr
), ns_rr_class(*rr
), ns_rr_type(*rr
),
99 ns_rr_ttl(*rr
), ns_rr_rdata(*rr
), ns_rr_rdlen(*rr
),
100 name_ctx
, origin
, buf
, buflen
);
105 * Convert the fields of an RR into presentation format.
108 *\li Number of characters written to buf, or -1 (check errno).
111 ns_sprintrrf(const u_char
*msg
, size_t msglen
,
112 const char *name
, ns_class
class, ns_type type
,
113 u_long ttl
, const u_char
*rdata
, size_t rdlen
,
114 const char *name_ctx
, const char *origin
,
115 char *buf
, size_t buflen
)
117 const char *obuf
= buf
;
118 const u_char
*edata
= rdata
+ rdlen
;
128 if (name_ctx
!= NULL
&& ns_samename(name_ctx
, name
) == 1) {
129 T(addstr("\t\t\t", (size_t)3, &buf
, &buflen
));
131 len
= (int)prune_origin(name
, origin
);
134 } else if (len
== 0) {
135 T(addstr("@\t\t\t", (size_t)4, &buf
, &buflen
));
137 T(addstr(name
, (size_t)len
, &buf
, &buflen
));
138 /* Origin not used or not root, and no trailing dot? */
139 if (((origin
== NULL
|| origin
[0] == '\0') ||
140 (origin
[0] != '.' && origin
[1] != '\0' &&
141 name
[len
] == '\0')) && name
[len
- 1] != '.') {
143 T(addstr(".", (size_t)1, &buf
, &buflen
));
146 T(spaced
= addtab((size_t)len
, 24, spaced
, &buf
, &buflen
));
153 T(x
= ns_format_ttl(ttl
, buf
, buflen
));
154 addlen((size_t)x
, &buf
, &buflen
);
155 len
= SPRINTF((tmp
, " %s %s", p_class(class), p_type(type
)));
156 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
157 T(spaced
= addtab((size_t)(x
+ len
), (size_t)16, spaced
, &buf
, &buflen
));
164 if (rdlen
!= (size_t)NS_INADDRSZ
)
166 (void) inet_ntop(AF_INET
, rdata
, buf
, (socklen_t
)buflen
);
167 addlen(strlen(buf
), &buf
, &buflen
);
177 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
183 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
187 T(addstr(" ", (size_t)1, &buf
, &buflen
));
190 /* Second word, optional in ISDN records. */
191 if (type
== ns_t_isdn
&& rdata
== edata
)
194 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
204 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
205 T(addstr(" ", (size_t)1, &buf
, &buflen
));
207 /* Administrator name. */
208 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
209 T(addstr(" (\n", (size_t)3, &buf
, &buflen
));
212 if ((edata
- rdata
) != 5*NS_INT32SZ
)
216 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
217 T(addstr("\t\t\t\t\t", (size_t)5, &buf
, &buflen
));
218 len
= SPRINTF((tmp
, "%lu", t
));
219 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
220 T(spaced
= addtab((size_t)len
, (size_t)16, spaced
, &buf
, &buflen
));
221 T(addstr("; serial\n", (size_t)9, &buf
, &buflen
));
224 /* Refresh interval. */
225 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
226 T(addstr("\t\t\t\t\t", (size_t)5, &buf
, &buflen
));
227 T(len
= ns_format_ttl(t
, buf
, buflen
));
228 addlen((size_t)len
, &buf
, &buflen
);
229 T(spaced
= addtab((size_t)len
, (size_t)16, spaced
, &buf
, &buflen
));
230 T(addstr("; refresh\n", (size_t)10, &buf
, &buflen
));
233 /* Retry interval. */
234 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
235 T(addstr("\t\t\t\t\t", (size_t)5, &buf
, &buflen
));
236 T(len
= ns_format_ttl(t
, buf
, buflen
));
237 addlen((size_t)len
, &buf
, &buflen
);
238 T(spaced
= addtab((size_t)len
, (size_t)16, spaced
, &buf
, &buflen
));
239 T(addstr("; retry\n", (size_t)8, &buf
, &buflen
));
243 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
244 T(addstr("\t\t\t\t\t", (size_t)5, &buf
, &buflen
));
245 T(len
= ns_format_ttl(t
, buf
, buflen
));
246 addlen((size_t)len
, &buf
, &buflen
);
247 T(spaced
= addtab((size_t)len
, (size_t)16, spaced
, &buf
, &buflen
));
248 T(addstr("; expiry\n", (size_t)9, &buf
, &buflen
));
252 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
253 T(addstr("\t\t\t\t\t", (size_t)5, &buf
, &buflen
));
254 T(len
= ns_format_ttl(t
, buf
, buflen
));
255 addlen((size_t)len
, &buf
, &buflen
);
256 T(addstr(" )", (size_t)2, &buf
, &buflen
));
257 T(spaced
= addtab((size_t)len
, (size_t)16, spaced
, &buf
, &buflen
));
258 T(addstr("; minimum\n", (size_t)10, &buf
, &buflen
));
269 if (rdlen
< (size_t)NS_INT16SZ
)
275 len
= SPRINTF((tmp
, "%u ", t
));
276 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
279 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
287 if (rdlen
< (size_t)NS_INT16SZ
)
293 len
= SPRINTF((tmp
, "%u ", t
));
294 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
297 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
298 T(addstr(" ", (size_t)1, &buf
, &buflen
));
301 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
307 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
315 while (rdata
< edata
) {
316 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
321 T(addstr(" ", (size_t)1, &buf
, &buflen
));
328 (void) inet_nsap_ntoa((int)rdlen
, rdata
, t
);
329 T(addstr(t
, strlen(t
), &buf
, &buflen
));
334 if (rdlen
!= (size_t)NS_IN6ADDRSZ
)
336 (void) inet_ntop(AF_INET6
, rdata
, buf
, (socklen_t
)buflen
);
337 addlen(strlen(buf
), &buf
, &buflen
);
343 /* XXX protocol format checking? */
344 (void) loc_ntoa(rdata
, t
);
345 T(addstr(t
, strlen(t
), &buf
, &buflen
));
350 u_int order
, preference
;
353 if (rdlen
< 2U*NS_INT16SZ
)
356 /* Order, Precedence. */
357 order
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
358 preference
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
359 len
= SPRINTF((t
, "%u %u ", order
, preference
));
360 T(addstr(t
, (size_t)len
, &buf
, &buflen
));
363 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
367 T(addstr(" ", (size_t)1, &buf
, &buflen
));
370 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
374 T(addstr(" ", (size_t)1, &buf
, &buflen
));
377 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
383 T(addstr(" ", (size_t)1, &buf
, &buflen
));
386 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
391 u_int priority
, weight
, port
;
394 if (rdlen
< 3U*NS_INT16SZ
)
397 /* Priority, Weight, Port. */
398 priority
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
399 weight
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
400 port
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
401 len
= SPRINTF((t
, "%u %u %u ", priority
, weight
, port
));
402 T(addstr(t
, (size_t)len
, &buf
, &buflen
));
405 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
412 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
413 T(addstr(" ", (size_t)1, &buf
, &buflen
));
416 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
423 if (rdlen
< 1U + NS_INT32SZ
)
427 (void) inet_ntop(AF_INET
, rdata
, buf
, (socklen_t
)buflen
);
428 addlen(strlen(buf
), &buf
, &buflen
);
429 rdata
+= NS_INADDRSZ
;
432 len
= SPRINTF((tmp
, " %u ( ", *rdata
));
433 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
439 while (rdata
< edata
) {
444 T(addstr("\n\t\t\t\t", (size_t)5,
449 len
= SPRINTF((tmp
, "%d ", n
));
450 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
456 T(addstr(")", (size_t)1, &buf
, &buflen
));
463 char base64_key
[NS_MD5RSA_MAX_BASE64
];
464 u_int keyflags
, protocol
, algorithm
, key_id
;
468 if (rdlen
< 0U + NS_INT16SZ
+ NS_INT8SZ
+ NS_INT8SZ
)
471 /* Key flags, Protocol, Algorithm. */
473 key_id
= dst_s_dns_key_id(rdata
, edata
-rdata
);
477 keyflags
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
479 algorithm
= *rdata
++;
480 len
= SPRINTF((tmp
, "0x%04x %u %u",
481 keyflags
, protocol
, algorithm
));
482 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
484 /* Public key data. */
485 len
= b64_ntop(rdata
, (size_t)(edata
- rdata
),
486 base64_key
, sizeof base64_key
);
490 T(addstr(" (", (size_t)2, &buf
, &buflen
));
495 for (n
= 0; n
< len
; n
+= 48) {
496 T(addstr(leader
, strlen(leader
), &buf
, &buflen
));
497 T(addstr(base64_key
+ n
, (size_t)MIN(len
- n
, 48),
501 T(addstr(" )", (size_t)2, &buf
, &buflen
));
502 n
= SPRINTF((tmp
, " ; key_tag= %u", key_id
));
503 T(addstr(tmp
, (size_t)n
, &buf
, &buflen
));
510 char base64_key
[NS_MD5RSA_MAX_BASE64
];
511 u_int typ
, algorithm
, labels
, footprint
;
519 /* Type covered, Algorithm, Label count, Original TTL. */
520 typ
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
521 algorithm
= *rdata
++;
523 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
524 len
= SPRINTF((tmp
, "%s %d %d %lu ",
525 p_type((int)typ
), algorithm
, labels
, t
));
526 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
527 if (labels
> (u_int
)dn_count_labels(name
))
530 /* Signature expiry. */
531 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
532 len
= SPRINTF((tmp
, "%s ", p_secstodate(t
)));
533 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
536 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
537 len
= SPRINTF((tmp
, "%s ", p_secstodate(t
)));
538 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
540 /* Signature Footprint. */
541 footprint
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
542 len
= SPRINTF((tmp
, "%u ", footprint
));
543 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
546 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
549 len
= b64_ntop(rdata
, (size_t)(edata
- rdata
),
550 base64_key
, sizeof base64_key
);
552 T(addstr(" (", (size_t)2, &buf
, &buflen
));
559 for (n
= 0; n
< len
; n
+= 48) {
560 T(addstr(leader
, strlen(leader
), &buf
, &buflen
));
561 T(addstr(base64_key
+ n
, (size_t)MIN(len
- n
, 48),
565 T(addstr(" )", (size_t)2, &buf
, &buflen
));
572 /* Next domain name. */
573 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
577 for (c
= 0; c
< n
*8; c
++)
578 if (NS_NXT_BIT_ISSET(c
, rdata
)) {
579 len
= SPRINTF((tmp
, " %s", p_type((int)c
)));
580 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
586 u_int c_type
, key_tag
, alg
;
589 char base64_cert
[8192], tmp1
[40];
592 c_type
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
593 key_tag
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
594 alg
= (u_int
) *rdata
++;
596 len
= SPRINTF((tmp1
, "%d %d %d ", c_type
, key_tag
, alg
));
597 T(addstr(tmp1
, (size_t)len
, &buf
, &buflen
));
598 siz
= (edata
-rdata
)*4/3 + 4; /* "+4" accounts for trailing \0 */
599 if (siz
> sizeof(base64_cert
) * 3/4) {
600 const char *str
= "record too long to print";
601 T(addstr(str
, strlen(str
), &buf
, &buflen
));
604 len
= b64_ntop(rdata
, (size_t)(edata
-rdata
),
610 T(addstr(" (", (size_t)2, &buf
, &buflen
));
617 for (n
= 0; n
< len
; n
+= 48) {
618 T(addstr(leader
, strlen(leader
),
620 T(addstr(base64_cert
+ n
, (size_t)MIN(len
- n
, 48),
624 T(addstr(" )", (size_t)2, &buf
, &buflen
));
630 /* KJD - need to complete this */
632 int mode
, err
, keysize
;
634 /* Algorithm name. */
635 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
636 T(addstr(" ", (size_t)1, &buf
, &buflen
));
639 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
640 len
= SPRINTF((tmp
, "%s ", p_secstodate(t
)));
641 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
644 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
645 len
= SPRINTF((tmp
, "%s ", p_secstodate(t
)));
646 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
648 /* Mode , Error, Key Size. */
649 /* Priority, Weight, Port. */
650 mode
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
651 err
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
652 keysize
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
653 len
= SPRINTF((tmp
, "%u %u %u ", mode
, err
, keysize
));
654 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
656 /* XXX need to dump key, print otherdata length & other data */
661 /* BEW - need to complete this */
664 T(len
= addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
665 T(addstr(" ", (size_t)1, &buf
, &buflen
));
666 rdata
+= 8; /*%< time */
667 n
= ns_get16(rdata
); rdata
+= INT16SZ
;
668 rdata
+= n
; /*%< sig */
669 n
= ns_get16(rdata
); rdata
+= INT16SZ
; /*%< original id */
670 sprintf(buf
, "%d", ns_get16(rdata
));
672 addlen(strlen(buf
), &buf
, &buflen
);
681 if (rdlen
== 0U) goto formerr
;
682 len
= SPRINTF((tmp
, "%d ", *rdata
));
683 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
685 if (pbit
> 128) goto formerr
;
686 pbyte
= (pbit
& ~7) / 8;
689 /* address suffix: provided only when prefix len != 128 */
691 if (rdata
+ pbyte
>= edata
) goto formerr
;
692 memset(&a
, 0, sizeof(a
));
693 memcpy(&a
.s6_addr
[pbyte
], rdata
, sizeof(a
) - pbyte
);
694 (void) inet_ntop(AF_INET6
, &a
, buf
, (socklen_t
)buflen
);
695 addlen(strlen(buf
), &buf
, &buflen
);
696 rdata
+= sizeof(a
) - pbyte
;
699 /* prefix name: provided only when prefix len > 0 */
702 if (rdata
>= edata
) goto formerr
;
703 T(addstr(" ", (size_t)1, &buf
, &buflen
));
704 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
710 len
= SPRINTF((tmp
, "%u bytes", class));
711 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
720 if (type
== ns_t_ds
|| type
== ns_t_dlv
) {
721 if (rdlen
< 4U) goto formerr
;
724 len
= SPRINTF((tmp
, "%u ", t
));
725 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
727 if (rdlen
< 2U) goto formerr
;
729 len
= SPRINTF((tmp
, "%u ", *rdata
));
730 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
733 len
= SPRINTF((tmp
, "%u ", *rdata
));
734 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
737 while (rdata
< edata
) {
738 len
= SPRINTF((tmp
, "%02X", *rdata
));
739 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
746 case ns_t_nsec3param
: {
747 u_int t
, w
, l
, j
, k
, c
;
749 len
= SPRINTF((tmp
, "%u ", *rdata
));
750 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
753 len
= SPRINTF((tmp
, "%u ", *rdata
));
754 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
759 len
= SPRINTF((tmp
, "%u ", t
));
760 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
764 T(addstr("-", 1, &buf
, &buflen
));
767 len
= SPRINTF((tmp
, "%02X", *rdata
));
768 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
772 if (type
== ns_t_nsec3param
)
774 T(addstr(" ", 1, &buf
, &buflen
));
780 tmp
[0] = base32hex
[(((uint32_t)rdata
[0]>>3)&0x1f)];
781 tmp
[1] = base32hex
[(((uint32_t)rdata
[0]<<2)&0x1c)];
782 tmp
[2] = tmp
[3] = tmp
[4] = '=';
783 tmp
[5] = tmp
[6] = tmp
[7] = '=';
786 tmp
[0] = base32hex
[(((uint32_t)rdata
[0]>>3)&0x1f)];
787 tmp
[1] = base32hex
[(((uint32_t)rdata
[0]<<2)&0x1c)|
788 (((uint32_t)rdata
[1]>>6)&0x03)];
789 tmp
[2] = base32hex
[(((uint32_t)rdata
[1]>>1)&0x1f)];
790 tmp
[3] = base32hex
[(((uint32_t)rdata
[1]<<4)&0x10)];
791 tmp
[4] = tmp
[5] = tmp
[6] = tmp
[7] = '=';
794 tmp
[0] = base32hex
[(((uint32_t)rdata
[0]>>3)&0x1f)];
795 tmp
[1] = base32hex
[(((uint32_t)rdata
[0]<<2)&0x1c)|
796 (((uint32_t)rdata
[1]>>6)&0x03)];
797 tmp
[2] = base32hex
[(((uint32_t)rdata
[1]>>1)&0x1f)];
798 tmp
[3] = base32hex
[(((uint32_t)rdata
[1]<<4)&0x10)|
799 (((uint32_t)rdata
[2]>>4)&0x0f)];
800 tmp
[4] = base32hex
[(((uint32_t)rdata
[2]<<1)&0x1e)];
801 tmp
[5] = tmp
[6] = tmp
[7] = '=';
804 tmp
[0] = base32hex
[(((uint32_t)rdata
[0]>>3)&0x1f)];
805 tmp
[1] = base32hex
[(((uint32_t)rdata
[0]<<2)&0x1c)|
806 (((uint32_t)rdata
[1]>>6)&0x03)];
807 tmp
[2] = base32hex
[(((uint32_t)rdata
[1]>>1)&0x1f)];
808 tmp
[3] = base32hex
[(((uint32_t)rdata
[1]<<4)&0x10)|
809 (((uint32_t)rdata
[2]>>4)&0x0f)];
810 tmp
[4] = base32hex
[(((uint32_t)rdata
[2]<<1)&0x1e)|
811 (((uint32_t)rdata
[3]>>7)&0x01)];
812 tmp
[5] = base32hex
[(((uint32_t)rdata
[3]>>2)&0x1f)];
813 tmp
[6] = base32hex
[((uint32_t)rdata
[3]<<3)&0x18];
817 tmp
[0] = base32hex
[(((uint32_t)rdata
[0]>>3)&0x1f)];
818 tmp
[1] = base32hex
[(((uint32_t)rdata
[0]<<2)&0x1c)|
819 (((uint32_t)rdata
[1]>>6)&0x03)];
820 tmp
[2] = base32hex
[(((uint32_t)rdata
[1]>>1)&0x1f)];
821 tmp
[3] = base32hex
[(((uint32_t)rdata
[1]<<4)&0x10)|
822 (((uint32_t)rdata
[2]>>4)&0x0f)];
823 tmp
[4] = base32hex
[(((uint32_t)rdata
[2]<<1)&0x1e)|
824 (((uint32_t)rdata
[3]>>7)&0x01)];
825 tmp
[5] = base32hex
[(((uint32_t)rdata
[3]>>2)&0x1f)];
826 tmp
[6] = base32hex
[(((uint32_t)rdata
[3]<<3)&0x18)|
827 (((uint32_t)rdata
[4]>>5)&0x07)];
828 tmp
[7] = base32hex
[(rdata
[4]&0x1f)];
831 T(addstr(tmp
, 8, &buf
, &buflen
));
841 while (rdata
< edata
) {
844 for (j
= 0; j
< l
; j
++) {
847 for (k
= 0; k
< 8; k
++) {
848 if ((rdata
[j
] & (0x80 >> k
)) == 0)
850 c
= w
* 256 + j
* 8 + k
;
851 len
= SPRINTF((tmp
, " %s", p_type((ns_type
)c
)));
852 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
863 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
865 while (rdata
< edata
) {
868 for (j
= 0; j
< l
; j
++) {
871 for (k
= 0; k
< 8; k
++) {
872 if ((rdata
[j
] & (0x80 >> k
)) == 0)
874 c
= w
* 256 + j
* 8 + k
;
875 len
= SPRINTF((tmp
, " %s", p_type((ns_type
)c
)));
876 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
887 char base64_dhcid
[8192];
890 siz
= (int)(edata
-rdata
)*4/3 + 4; /* "+4" accounts for trailing \0 */
891 if (siz
> sizeof(base64_dhcid
) * 3/4) {
892 const char *str
= "record too long to print";
893 T(addstr(str
, strlen(str
), &buf
, &buflen
));
895 len
= b64_ntop(rdata
, (size_t)(edata
-rdata
),
902 T(addstr(" (", 2, &buf
, &buflen
));
909 for (n
= 0; n
< len
; n
+= 48) {
910 T(addstr(leader
, strlen(leader
),
912 T(addstr(base64_dhcid
+ n
,
913 (size_t)MIN(len
- n
, 48), &buf
, &buflen
));
916 T(addstr(" )", 2, &buf
, &buflen
));
921 case ns_t_ipseckey
: {
924 char base64_key
[8192];
945 comment
= "unknown IPSECKEY gateway type";
949 len
= SPRINTF((tmp
, "%u ", *rdata
));
950 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
953 len
= SPRINTF((tmp
, "%u ", *rdata
));
954 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
957 len
= SPRINTF((tmp
, "%u ", *rdata
));
958 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
963 T(addstr(".", 1, &buf
, &buflen
));
966 (void) inet_ntop(AF_INET
, rdata
, buf
, (socklen_t
)buflen
);
967 addlen(strlen(buf
), &buf
, &buflen
);
971 (void) inet_ntop(AF_INET6
, rdata
, buf
, (socklen_t
)buflen
);
972 addlen(strlen(buf
), &buf
, &buflen
);
976 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
983 siz
= (int)(edata
-rdata
)*4/3 + 4; /* "+4" accounts for trailing \0 */
984 if (siz
> sizeof(base64_key
) * 3/4) {
985 const char *str
= "record too long to print";
986 T(addstr(str
, strlen(str
), &buf
, &buflen
));
988 len
= b64_ntop(rdata
, (size_t)(edata
-rdata
),
995 T(addstr(" (", 2, &buf
, &buflen
));
1002 for (n
= 0; n
< len
; n
+= 48) {
1003 T(addstr(leader
, strlen(leader
),
1005 T(addstr(base64_key
+ n
,
1006 (size_t)MIN(len
- n
, 48), &buf
, &buflen
));
1009 T(addstr(" )", 2, &buf
, &buflen
));
1015 unsigned int i
, hip_len
, algorithm
, key_len
;
1016 char base64_key
[NS_MD5RSA_MAX_BASE64
];
1018 const char *leader
= "\n\t\t\t\t\t";
1021 algorithm
= *rdata
++;
1022 key_len
= ns_get16(rdata
);
1023 rdata
+= NS_INT16SZ
;
1025 siz
= key_len
*4/3 + 4; /* "+4" accounts for trailing \0 */
1026 if (siz
> sizeof(base64_key
) * 3/4) {
1027 const char *str
= "record too long to print";
1028 T(addstr(str
, strlen(str
), &buf
, &buflen
));
1030 len
= sprintf(tmp
, "( %u ", algorithm
);
1031 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
1033 for (i
= 0; i
< hip_len
; i
++) {
1034 len
= sprintf(tmp
, "%02X", *rdata
);
1035 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
1038 T(addstr(leader
, strlen(leader
), &buf
, &buflen
));
1040 len
= b64_ntop(rdata
, key_len
, base64_key
, siz
);
1044 T(addstr(base64_key
, (size_t)len
, &buf
, &buflen
));
1047 while (rdata
< edata
) {
1048 T(addstr(leader
, strlen(leader
), &buf
, &buflen
));
1049 T(addname(msg
, msglen
, &rdata
, origin
,
1052 T(addstr(" )", 2, &buf
, &buflen
));
1058 comment
= "unknown RR type";
1061 _DIAGASSERT(__type_fit(int, buf
- obuf
));
1062 return (int)(buf
- obuf
);
1064 comment
= "RR format error";
1069 len
= SPRINTF((tmp
, "\\# %u%s\t; %s", (unsigned)(edata
- rdata
),
1070 rdlen
!= 0U ? " (" : "", comment
));
1071 T(addstr(tmp
, (size_t)len
, &buf
, &buflen
));
1072 while (rdata
< edata
) {
1074 p
+= SPRINTF((p
, "\n\t"));
1076 n
= MIN(16, (int)(edata
- rdata
));
1077 for (m
= 0; m
< n
; m
++)
1078 p
+= SPRINTF((p
, "%02x ", rdata
[m
]));
1079 T(addstr(tmp
, (size_t)(p
- tmp
), &buf
, &buflen
));
1081 T(addstr(")", (size_t)1, &buf
, &buflen
));
1082 T(addtab((size_t)(p
- tmp
+ 1), (size_t)48, spaced
, &buf
, &buflen
));
1085 p
+= SPRINTF((p
, "; "));
1086 for (m
= 0; m
< n
; m
++)
1087 *p
++ = (isascii(rdata
[m
]) && isprint(rdata
[m
]))
1090 T(addstr(tmp
, (size_t)(p
- tmp
), &buf
, &buflen
));
1093 _DIAGASSERT(__type_fit(int, buf
- obuf
));
1094 return (int)(buf
- obuf
);
1102 * prune_origin(name, origin)
1103 * Find out if the name is at or under the current origin.
1105 * Number of characters in name before start of origin,
1106 * or length of name if origin does not match.
1108 * This function should share code with samedomain().
1111 prune_origin(const char *name
, const char *origin
) {
1112 const char *oname
= name
;
1114 while (*name
!= '\0') {
1115 if (origin
!= NULL
&& ns_samename(name
, origin
) == 1)
1116 return (name
- oname
- (name
> oname
));
1117 while (*name
!= '\0') {
1118 if (*name
== '\\') {
1120 /* XXX need to handle \nnn form. */
1123 } else if (*name
== '.') {
1130 return (name
- oname
);
1135 * charstr(rdata, edata, buf, buflen)
1136 * Format a <character-string> into the presentation buffer.
1138 * Number of rdata octets consumed
1139 * 0 for protocol format error
1140 * -1 for output buffer error
1142 * buffer is advanced on success.
1145 charstr(const u_char
*rdata
, const u_char
*edata
, char **buf
, size_t *buflen
) {
1146 const u_char
*odata
= rdata
;
1147 size_t save_buflen
= *buflen
;
1148 char *save_buf
= *buf
;
1150 if (addstr("\"", (size_t)1, buf
, buflen
) < 0)
1152 if (rdata
< edata
) {
1155 if (rdata
+ 1 + n
<= edata
) {
1158 if (strchr("\n\"\\", *rdata
) != NULL
)
1159 if (addstr("\\", (size_t)1, buf
, buflen
) < 0)
1161 if (addstr((const char *)rdata
, (size_t)1,
1168 if (addstr("\"", (size_t)1, buf
, buflen
) < 0)
1170 _DIAGASSERT(__type_fit(int, rdata
- odata
));
1171 return (int)(rdata
- odata
);
1175 *buflen
= save_buflen
;
1180 addname(const u_char
*msg
, size_t msglen
,
1181 const u_char
**pp
, const char *origin
,
1182 char **buf
, size_t *buflen
)
1184 size_t newlen
, save_buflen
= *buflen
;
1185 char *save_buf
= *buf
;
1188 n
= dn_expand(msg
, msg
+ msglen
, *pp
, *buf
, (int)*buflen
);
1190 goto enospc
; /*%< Guess. */
1191 newlen
= prune_origin(*buf
, origin
);
1192 if (**buf
== '\0') {
1194 } else if (newlen
== 0U) {
1195 /* Use "@" instead of name. */
1196 if (newlen
+ 2 > *buflen
)
1197 goto enospc
; /* No room for "@\0". */
1198 (*buf
)[newlen
++] = '@';
1199 (*buf
)[newlen
] = '\0';
1201 if (((origin
== NULL
|| origin
[0] == '\0') ||
1202 (origin
[0] != '.' && origin
[1] != '\0' &&
1203 (*buf
)[newlen
] == '\0')) && (*buf
)[newlen
- 1] != '.') {
1204 /* No trailing dot. */
1206 if (newlen
+ 2 > *buflen
)
1207 goto enospc
; /* No room for ".\0". */
1208 (*buf
)[newlen
++] = '.';
1209 (*buf
)[newlen
] = '\0';
1213 addlen(newlen
, buf
, buflen
);
1215 _DIAGASSERT(__type_fit(int, newlen
));
1220 *buflen
= save_buflen
;
1225 addlen(size_t len
, char **buf
, size_t *buflen
) {
1226 INSIST(len
<= *buflen
);
1232 addstr(const char *src
, size_t len
, char **buf
, size_t *buflen
) {
1233 if (len
>= *buflen
) {
1237 memcpy(*buf
, src
, len
);
1238 addlen(len
, buf
, buflen
);
1244 addtab(size_t len
, size_t target
, int spaced
, char **buf
, size_t *buflen
) {
1245 size_t save_buflen
= *buflen
;
1246 char *save_buf
= *buf
;
1249 if (spaced
|| len
>= target
- 1) {
1250 T(addstr(" ", (size_t)2, buf
, buflen
));
1253 for (t
= (target
- len
- 1) / 8; t
>= 0; t
--)
1254 if (addstr("\t", (size_t)1, buf
, buflen
) < 0) {
1255 *buflen
= save_buflen
;