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.
21 static const char rcsid
[] = "Id: ns_print.c,v 1.12 2009/03/03 05:29:58 each Exp";
26 #include "port_before.h"
28 #include <sys/types.h>
29 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/nameser.h>
33 #include <arpa/inet.h>
35 #include <isc/assertions.h>
42 #include "port_after.h"
45 # define SPRINTF(x) strlen(sprintf/**/x)
47 # define SPRINTF(x) ((size_t)sprintf x)
52 static size_t prune_origin(const char *name
, const char *origin
);
53 static int charstr(const u_char
*rdata
, const u_char
*edata
,
54 char **buf
, size_t *buflen
);
55 static int addname(const u_char
*msg
, size_t msglen
,
56 const u_char
**p
, const char *origin
,
57 char **buf
, size_t *buflen
);
58 static void addlen(size_t len
, char **buf
, size_t *buflen
);
59 static int addstr(const char *src
, size_t len
,
60 char **buf
, size_t *buflen
);
61 static int addtab(size_t len
, size_t target
, int spaced
,
62 char **buf
, size_t *buflen
);
72 static const char base32hex
[] =
73 "0123456789ABCDEFGHIJKLMNOPQRSTUV=0123456789abcdefghijklmnopqrstuv";
78 * Convert an RR to presentation format.
81 *\li Number of characters written to buf, or -1 (check errno).
84 ns_sprintrr(const ns_msg
*handle
, const ns_rr
*rr
,
85 const char *name_ctx
, const char *origin
,
86 char *buf
, size_t buflen
)
90 n
= ns_sprintrrf(ns_msg_base(*handle
), ns_msg_size(*handle
),
91 ns_rr_name(*rr
), ns_rr_class(*rr
), ns_rr_type(*rr
),
92 ns_rr_ttl(*rr
), ns_rr_rdata(*rr
), ns_rr_rdlen(*rr
),
93 name_ctx
, origin
, buf
, buflen
);
98 * Convert the fields of an RR into presentation format.
101 *\li Number of characters written to buf, or -1 (check errno).
104 ns_sprintrrf(const u_char
*msg
, size_t msglen
,
105 const char *name
, ns_class
class, ns_type type
,
106 u_long ttl
, const u_char
*rdata
, size_t rdlen
,
107 const char *name_ctx
, const char *origin
,
108 char *buf
, size_t buflen
)
110 const char *obuf
= buf
;
111 const u_char
*edata
= rdata
+ rdlen
;
121 if (name_ctx
!= NULL
&& ns_samename(name_ctx
, name
) == 1) {
122 T(addstr("\t\t\t", 3, &buf
, &buflen
));
124 len
= prune_origin(name
, origin
);
127 } else if (len
== 0) {
128 T(addstr("@\t\t\t", 4, &buf
, &buflen
));
130 T(addstr(name
, len
, &buf
, &buflen
));
131 /* Origin not used or not root, and no trailing dot? */
132 if (((origin
== NULL
|| origin
[0] == '\0') ||
133 (origin
[0] != '.' && origin
[1] != '\0' &&
134 name
[len
] == '\0')) && name
[len
- 1] != '.') {
136 T(addstr(".", 1, &buf
, &buflen
));
139 T(spaced
= addtab(len
, 24, spaced
, &buf
, &buflen
));
146 T(x
= ns_format_ttl(ttl
, buf
, buflen
));
147 addlen(x
, &buf
, &buflen
);
148 len
= SPRINTF((tmp
, " %s %s", p_class(class), p_type(type
)));
149 T(addstr(tmp
, len
, &buf
, &buflen
));
150 T(spaced
= addtab(x
+ len
, 16, spaced
, &buf
, &buflen
));
157 if (rdlen
!= (size_t)NS_INADDRSZ
)
159 (void) inet_ntop(AF_INET
, rdata
, buf
, buflen
);
160 addlen(strlen(buf
), &buf
, &buflen
);
170 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
176 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
180 T(addstr(" ", 1, &buf
, &buflen
));
183 /* Second word, optional in ISDN records. */
184 if (type
== ns_t_isdn
&& rdata
== edata
)
187 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
197 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
198 T(addstr(" ", 1, &buf
, &buflen
));
200 /* Administrator name. */
201 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
202 T(addstr(" (\n", 3, &buf
, &buflen
));
205 if ((edata
- rdata
) != 5*NS_INT32SZ
)
209 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
210 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
211 len
= SPRINTF((tmp
, "%lu", t
));
212 T(addstr(tmp
, len
, &buf
, &buflen
));
213 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
214 T(addstr("; serial\n", 9, &buf
, &buflen
));
217 /* Refresh interval. */
218 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
219 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
220 T(len
= ns_format_ttl(t
, buf
, buflen
));
221 addlen(len
, &buf
, &buflen
);
222 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
223 T(addstr("; refresh\n", 10, &buf
, &buflen
));
226 /* Retry interval. */
227 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
228 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
229 T(len
= ns_format_ttl(t
, buf
, buflen
));
230 addlen(len
, &buf
, &buflen
);
231 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
232 T(addstr("; retry\n", 8, &buf
, &buflen
));
236 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
237 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
238 T(len
= ns_format_ttl(t
, buf
, buflen
));
239 addlen(len
, &buf
, &buflen
);
240 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
241 T(addstr("; expiry\n", 9, &buf
, &buflen
));
245 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
246 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
247 T(len
= ns_format_ttl(t
, buf
, buflen
));
248 addlen(len
, &buf
, &buflen
);
249 T(addstr(" )", 2, &buf
, &buflen
));
250 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
251 T(addstr("; minimum\n", 10, &buf
, &buflen
));
262 if (rdlen
< (size_t)NS_INT16SZ
)
268 len
= SPRINTF((tmp
, "%u ", t
));
269 T(addstr(tmp
, len
, &buf
, &buflen
));
272 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
280 if (rdlen
< (size_t)NS_INT16SZ
)
286 len
= SPRINTF((tmp
, "%u ", t
));
287 T(addstr(tmp
, len
, &buf
, &buflen
));
290 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
291 T(addstr(" ", 1, &buf
, &buflen
));
294 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
300 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
308 while (rdata
< edata
) {
309 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
314 T(addstr(" ", 1, &buf
, &buflen
));
321 (void) inet_nsap_ntoa(rdlen
, rdata
, t
);
322 T(addstr(t
, strlen(t
), &buf
, &buflen
));
327 if (rdlen
!= (size_t)NS_IN6ADDRSZ
)
329 (void) inet_ntop(AF_INET6
, rdata
, buf
, buflen
);
330 addlen(strlen(buf
), &buf
, &buflen
);
336 /* XXX protocol format checking? */
337 (void) loc_ntoa(rdata
, t
);
338 T(addstr(t
, strlen(t
), &buf
, &buflen
));
343 u_int order
, preference
;
346 if (rdlen
< 2U*NS_INT16SZ
)
349 /* Order, Precedence. */
350 order
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
351 preference
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
352 len
= SPRINTF((t
, "%u %u ", order
, preference
));
353 T(addstr(t
, len
, &buf
, &buflen
));
356 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
360 T(addstr(" ", 1, &buf
, &buflen
));
363 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
367 T(addstr(" ", 1, &buf
, &buflen
));
370 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
376 T(addstr(" ", 1, &buf
, &buflen
));
379 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
384 u_int priority
, weight
, port
;
387 if (rdlen
< 3U*NS_INT16SZ
)
390 /* Priority, Weight, Port. */
391 priority
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
392 weight
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
393 port
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
394 len
= SPRINTF((t
, "%u %u %u ", priority
, weight
, port
));
395 T(addstr(t
, len
, &buf
, &buflen
));
398 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
405 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
406 T(addstr(" ", 1, &buf
, &buflen
));
409 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
416 if (rdlen
< 1U + NS_INT32SZ
)
420 (void) inet_ntop(AF_INET
, rdata
, buf
, buflen
);
421 addlen(strlen(buf
), &buf
, &buflen
);
422 rdata
+= NS_INADDRSZ
;
425 len
= SPRINTF((tmp
, " %u ( ", *rdata
));
426 T(addstr(tmp
, len
, &buf
, &buflen
));
432 while (rdata
< edata
) {
437 T(addstr("\n\t\t\t\t", 5,
442 len
= SPRINTF((tmp
, "%d ", n
));
443 T(addstr(tmp
, len
, &buf
, &buflen
));
449 T(addstr(")", 1, &buf
, &buflen
));
456 char base64_key
[NS_MD5RSA_MAX_BASE64
];
457 u_int keyflags
, protocol
, algorithm
, key_id
;
461 if (rdlen
< 0U + NS_INT16SZ
+ NS_INT8SZ
+ NS_INT8SZ
)
464 /* Key flags, Protocol, Algorithm. */
465 key_id
= dst_s_dns_key_id(rdata
, edata
-rdata
);
466 keyflags
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
468 algorithm
= *rdata
++;
469 len
= SPRINTF((tmp
, "0x%04x %u %u",
470 keyflags
, protocol
, algorithm
));
471 T(addstr(tmp
, len
, &buf
, &buflen
));
473 /* Public key data. */
474 len
= b64_ntop(rdata
, edata
- rdata
,
475 base64_key
, sizeof base64_key
);
479 T(addstr(" (", 2, &buf
, &buflen
));
484 for (n
= 0; n
< len
; n
+= 48) {
485 T(addstr(leader
, strlen(leader
), &buf
, &buflen
));
486 T(addstr(base64_key
+ n
, MIN(len
- n
, 48),
490 T(addstr(" )", 2, &buf
, &buflen
));
491 n
= SPRINTF((tmp
, " ; key_tag= %u", key_id
));
492 T(addstr(tmp
, n
, &buf
, &buflen
));
499 char base64_key
[NS_MD5RSA_MAX_BASE64
];
500 u_int type
, algorithm
, labels
, footprint
;
508 /* Type covered, Algorithm, Label count, Original TTL. */
509 type
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
510 algorithm
= *rdata
++;
512 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
513 len
= SPRINTF((tmp
, "%s %d %d %lu ",
514 p_type(type
), algorithm
, labels
, t
));
515 T(addstr(tmp
, len
, &buf
, &buflen
));
516 if (labels
> (u_int
)dn_count_labels(name
))
519 /* Signature expiry. */
520 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
521 len
= SPRINTF((tmp
, "%s ", p_secstodate(t
)));
522 T(addstr(tmp
, len
, &buf
, &buflen
));
525 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
526 len
= SPRINTF((tmp
, "%s ", p_secstodate(t
)));
527 T(addstr(tmp
, len
, &buf
, &buflen
));
529 /* Signature Footprint. */
530 footprint
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
531 len
= SPRINTF((tmp
, "%u ", footprint
));
532 T(addstr(tmp
, len
, &buf
, &buflen
));
535 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
538 len
= b64_ntop(rdata
, edata
- rdata
,
539 base64_key
, sizeof base64_key
);
541 T(addstr(" (", 2, &buf
, &buflen
));
548 for (n
= 0; n
< len
; n
+= 48) {
549 T(addstr(leader
, strlen(leader
), &buf
, &buflen
));
550 T(addstr(base64_key
+ n
, MIN(len
- n
, 48),
554 T(addstr(" )", 2, &buf
, &buflen
));
561 /* Next domain name. */
562 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
566 for (c
= 0; c
< n
*8; c
++)
567 if (NS_NXT_BIT_ISSET(c
, rdata
)) {
568 len
= SPRINTF((tmp
, " %s", p_type(c
)));
569 T(addstr(tmp
, len
, &buf
, &buflen
));
575 u_int c_type
, key_tag
, alg
;
578 char base64_cert
[8192], tmp
[40];
581 c_type
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
582 key_tag
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
583 alg
= (u_int
) *rdata
++;
585 len
= SPRINTF((tmp
, "%d %d %d ", c_type
, key_tag
, alg
));
586 T(addstr(tmp
, len
, &buf
, &buflen
));
587 siz
= (edata
-rdata
)*4/3 + 4; /* "+4" accounts for trailing \0 */
588 if (siz
> sizeof(base64_cert
) * 3/4) {
589 const char *str
= "record too long to print";
590 T(addstr(str
, strlen(str
), &buf
, &buflen
));
593 len
= b64_ntop(rdata
, edata
-rdata
, base64_cert
, siz
);
598 T(addstr(" (", 2, &buf
, &buflen
));
605 for (n
= 0; n
< len
; n
+= 48) {
606 T(addstr(leader
, strlen(leader
),
608 T(addstr(base64_cert
+ n
, MIN(len
- n
, 48),
612 T(addstr(" )", 2, &buf
, &buflen
));
618 /* KJD - need to complete this */
620 int mode
, err
, keysize
;
622 /* Algorithm name. */
623 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
624 T(addstr(" ", 1, &buf
, &buflen
));
627 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
628 len
= SPRINTF((tmp
, "%s ", p_secstodate(t
)));
629 T(addstr(tmp
, len
, &buf
, &buflen
));
632 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
633 len
= SPRINTF((tmp
, "%s ", p_secstodate(t
)));
634 T(addstr(tmp
, len
, &buf
, &buflen
));
636 /* Mode , Error, Key Size. */
637 /* Priority, Weight, Port. */
638 mode
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
639 err
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
640 keysize
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
641 len
= SPRINTF((tmp
, "%u %u %u ", mode
, err
, keysize
));
642 T(addstr(tmp
, len
, &buf
, &buflen
));
644 /* XXX need to dump key, print otherdata length & other data */
649 /* BEW - need to complete this */
652 T(len
= addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
653 T(addstr(" ", 1, &buf
, &buflen
));
654 rdata
+= 8; /*%< time */
655 n
= ns_get16(rdata
); rdata
+= INT16SZ
;
656 rdata
+= n
; /*%< sig */
657 n
= ns_get16(rdata
); rdata
+= INT16SZ
; /*%< original id */
658 sprintf(buf
, "%d", ns_get16(rdata
));
660 addlen(strlen(buf
), &buf
, &buflen
);
669 if (rdlen
== 0U) goto formerr
;
670 len
= SPRINTF((tmp
, "%d ", *rdata
));
671 T(addstr(tmp
, len
, &buf
, &buflen
));
673 if (pbit
> 128) goto formerr
;
674 pbyte
= (pbit
& ~7) / 8;
677 /* address suffix: provided only when prefix len != 128 */
679 if (rdata
+ pbyte
>= edata
) goto formerr
;
680 memset(&a
, 0, sizeof(a
));
681 memcpy(&a
.s6_addr
[pbyte
], rdata
, sizeof(a
) - pbyte
);
682 (void) inet_ntop(AF_INET6
, &a
, buf
, buflen
);
683 addlen(strlen(buf
), &buf
, &buflen
);
684 rdata
+= sizeof(a
) - pbyte
;
687 /* prefix name: provided only when prefix len > 0 */
690 if (rdata
>= edata
) goto formerr
;
691 T(addstr(" ", 1, &buf
, &buflen
));
692 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
698 len
= SPRINTF((tmp
, "%u bytes", class));
699 T(addstr(tmp
, len
, &buf
, &buflen
));
708 if (type
== ns_t_ds
|| type
== ns_t_dlv
) {
709 if (rdlen
< 4U) goto formerr
;
712 len
= SPRINTF((tmp
, "%u ", t
));
713 T(addstr(tmp
, len
, &buf
, &buflen
));
715 if (rdlen
< 2U) goto formerr
;
717 len
= SPRINTF((tmp
, "%u ", *rdata
));
718 T(addstr(tmp
, len
, &buf
, &buflen
));
721 len
= SPRINTF((tmp
, "%u ", *rdata
));
722 T(addstr(tmp
, len
, &buf
, &buflen
));
725 while (rdata
< edata
) {
726 len
= SPRINTF((tmp
, "%02X", *rdata
));
727 T(addstr(tmp
, len
, &buf
, &buflen
));
734 case ns_t_nsec3param
: {
735 u_int t
, w
, l
, j
, k
, c
;
737 len
= SPRINTF((tmp
, "%u ", *rdata
));
738 T(addstr(tmp
, len
, &buf
, &buflen
));
741 len
= SPRINTF((tmp
, "%u ", *rdata
));
742 T(addstr(tmp
, len
, &buf
, &buflen
));
747 len
= SPRINTF((tmp
, "%u ", t
));
748 T(addstr(tmp
, len
, &buf
, &buflen
));
752 T(addstr("-", 1, &buf
, &buflen
));
755 len
= SPRINTF((tmp
, "%02X", *rdata
));
756 T(addstr(tmp
, len
, &buf
, &buflen
));
760 if (type
== ns_t_nsec3param
)
762 T(addstr(" ", 1, &buf
, &buflen
));
768 tmp
[0] = base32hex
[((rdata
[0]>>3)&0x1f)];
769 tmp
[1] = base32hex
[((rdata
[0]<<2)&0x1c)];
770 tmp
[2] = tmp
[3] = tmp
[4] = '=';
771 tmp
[5] = tmp
[6] = tmp
[7] = '=';
774 tmp
[0] = base32hex
[((rdata
[0]>>3)&0x1f)];
775 tmp
[1] = base32hex
[((rdata
[0]<<2)&0x1c)|
776 ((rdata
[1]>>6)&0x03)];
777 tmp
[2] = base32hex
[((rdata
[1]>>1)&0x1f)];
778 tmp
[3] = base32hex
[((rdata
[1]<<4)&0x10)];
779 tmp
[4] = tmp
[5] = tmp
[6] = tmp
[7] = '=';
782 tmp
[0] = base32hex
[((rdata
[0]>>3)&0x1f)];
783 tmp
[1] = base32hex
[((rdata
[0]<<2)&0x1c)|
784 ((rdata
[1]>>6)&0x03)];
785 tmp
[2] = base32hex
[((rdata
[1]>>1)&0x1f)];
786 tmp
[3] = base32hex
[((rdata
[1]<<4)&0x10)|
787 ((rdata
[2]>>4)&0x0f)];
788 tmp
[4] = base32hex
[((rdata
[2]<<1)&0x1e)];
789 tmp
[5] = tmp
[6] = tmp
[7] = '=';
792 tmp
[0] = base32hex
[((rdata
[0]>>3)&0x1f)];
793 tmp
[1] = base32hex
[((rdata
[0]<<2)&0x1c)|
794 ((rdata
[1]>>6)&0x03)];
795 tmp
[2] = base32hex
[((rdata
[1]>>1)&0x1f)];
796 tmp
[3] = base32hex
[((rdata
[1]<<4)&0x10)|
797 ((rdata
[2]>>4)&0x0f)];
798 tmp
[4] = base32hex
[((rdata
[2]<<1)&0x1e)|
799 ((rdata
[3]>>7)&0x01)];
800 tmp
[5] = base32hex
[((rdata
[3]>>2)&0x1f)];
801 tmp
[6] = base32hex
[(rdata
[3]<<3)&0x18];
805 tmp
[0] = base32hex
[((rdata
[0]>>3)&0x1f)];
806 tmp
[1] = base32hex
[((rdata
[0]<<2)&0x1c)|
807 ((rdata
[1]>>6)&0x03)];
808 tmp
[2] = base32hex
[((rdata
[1]>>1)&0x1f)];
809 tmp
[3] = base32hex
[((rdata
[1]<<4)&0x10)|
810 ((rdata
[2]>>4)&0x0f)];
811 tmp
[4] = base32hex
[((rdata
[2]<<1)&0x1e)|
812 ((rdata
[3]>>7)&0x01)];
813 tmp
[5] = base32hex
[((rdata
[3]>>2)&0x1f)];
814 tmp
[6] = base32hex
[((rdata
[3]<<3)&0x18)|
815 ((rdata
[4]>>5)&0x07)];
816 tmp
[7] = base32hex
[(rdata
[4]&0x1f)];
819 T(addstr(tmp
, 8, &buf
, &buflen
));
829 while (rdata
< edata
) {
832 for (j
= 0; j
< l
; j
++) {
835 for (k
= 0; k
< 8; k
++) {
836 if ((rdata
[j
] & (0x80 >> k
)) == 0)
838 c
= w
* 256 + j
* 8 + k
;
839 len
= SPRINTF((tmp
, " %s", p_type(c
)));
840 T(addstr(tmp
, len
, &buf
, &buflen
));
851 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
853 while (rdata
< edata
) {
856 for (j
= 0; j
< l
; j
++) {
859 for (k
= 0; k
< 8; k
++) {
860 if ((rdata
[j
] & (0x80 >> k
)) == 0)
862 c
= w
* 256 + j
* 8 + k
;
863 len
= SPRINTF((tmp
, " %s", p_type(c
)));
864 T(addstr(tmp
, len
, &buf
, &buflen
));
875 char base64_dhcid
[8192];
878 siz
= (edata
-rdata
)*4/3 + 4; /* "+4" accounts for trailing \0 */
879 if (siz
> sizeof(base64_dhcid
) * 3/4) {
880 const char *str
= "record too long to print";
881 T(addstr(str
, strlen(str
), &buf
, &buflen
));
883 len
= b64_ntop(rdata
, edata
-rdata
, base64_dhcid
, siz
);
889 T(addstr(" (", 2, &buf
, &buflen
));
896 for (n
= 0; n
< len
; n
+= 48) {
897 T(addstr(leader
, strlen(leader
),
899 T(addstr(base64_dhcid
+ n
, MIN(len
- n
, 48),
903 T(addstr(" )", 2, &buf
, &buflen
));
907 case ns_t_ipseckey
: {
910 char base64_key
[8192];
931 comment
= "unknown IPSECKEY gateway type";
935 len
= SPRINTF((tmp
, "%u ", *rdata
));
936 T(addstr(tmp
, len
, &buf
, &buflen
));
939 len
= SPRINTF((tmp
, "%u ", *rdata
));
940 T(addstr(tmp
, len
, &buf
, &buflen
));
943 len
= SPRINTF((tmp
, "%u ", *rdata
));
944 T(addstr(tmp
, len
, &buf
, &buflen
));
949 T(addstr(".", 1, &buf
, &buflen
));
952 (void) inet_ntop(AF_INET
, rdata
, buf
, buflen
);
953 addlen(strlen(buf
), &buf
, &buflen
);
957 (void) inet_ntop(AF_INET6
, rdata
, buf
, buflen
);
958 addlen(strlen(buf
), &buf
, &buflen
);
962 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
969 siz
= (edata
-rdata
)*4/3 + 4; /* "+4" accounts for trailing \0 */
970 if (siz
> sizeof(base64_key
) * 3/4) {
971 const char *str
= "record too long to print";
972 T(addstr(str
, strlen(str
), &buf
, &buflen
));
974 len
= b64_ntop(rdata
, edata
-rdata
, base64_key
, siz
);
980 T(addstr(" (", 2, &buf
, &buflen
));
987 for (n
= 0; n
< len
; n
+= 48) {
988 T(addstr(leader
, strlen(leader
),
990 T(addstr(base64_key
+ n
, MIN(len
- n
, 48),
994 T(addstr(" )", 2, &buf
, &buflen
));
999 unsigned int i
, hip_len
, algorithm
, key_len
;
1000 char base64_key
[NS_MD5RSA_MAX_BASE64
];
1002 const char *leader
= "\n\t\t\t\t\t";
1005 algorithm
= *rdata
++;
1006 key_len
= ns_get16(rdata
);
1007 rdata
+= NS_INT16SZ
;
1009 siz
= key_len
*4/3 + 4; /* "+4" accounts for trailing \0 */
1010 if (siz
> sizeof(base64_key
) * 3/4) {
1011 const char *str
= "record too long to print";
1012 T(addstr(str
, strlen(str
), &buf
, &buflen
));
1014 len
= sprintf(tmp
, "( %u ", algorithm
);
1015 T(addstr(tmp
, len
, &buf
, &buflen
));
1017 for (i
= 0; i
< hip_len
; i
++) {
1018 len
= sprintf(tmp
, "%02X", *rdata
);
1019 T(addstr(tmp
, len
, &buf
, &buflen
));
1022 T(addstr(leader
, strlen(leader
), &buf
, &buflen
));
1024 len
= b64_ntop(rdata
, key_len
, base64_key
, siz
);
1028 T(addstr(base64_key
, len
, &buf
, &buflen
));
1031 while (rdata
< edata
) {
1032 T(addstr(leader
, strlen(leader
), &buf
, &buflen
));
1033 T(addname(msg
, msglen
, &rdata
, origin
,
1036 T(addstr(" )", 2, &buf
, &buflen
));
1042 comment
= "unknown RR type";
1045 return (buf
- obuf
);
1047 comment
= "RR format error";
1052 len
= SPRINTF((tmp
, "\\# %u%s\t; %s", (unsigned)(edata
- rdata
),
1053 rdlen
!= 0U ? " (" : "", comment
));
1054 T(addstr(tmp
, len
, &buf
, &buflen
));
1055 while (rdata
< edata
) {
1057 p
+= SPRINTF((p
, "\n\t"));
1059 n
= MIN(16, edata
- rdata
);
1060 for (m
= 0; m
< n
; m
++)
1061 p
+= SPRINTF((p
, "%02x ", rdata
[m
]));
1062 T(addstr(tmp
, p
- tmp
, &buf
, &buflen
));
1064 T(addstr(")", 1, &buf
, &buflen
));
1065 T(addtab(p
- tmp
+ 1, 48, spaced
, &buf
, &buflen
));
1068 p
+= SPRINTF((p
, "; "));
1069 for (m
= 0; m
< n
; m
++)
1070 *p
++ = (isascii(rdata
[m
]) && isprint(rdata
[m
]))
1073 T(addstr(tmp
, p
- tmp
, &buf
, &buflen
));
1076 return (buf
- obuf
);
1084 * prune_origin(name, origin)
1085 * Find out if the name is at or under the current origin.
1087 * Number of characters in name before start of origin,
1088 * or length of name if origin does not match.
1090 * This function should share code with samedomain().
1093 prune_origin(const char *name
, const char *origin
) {
1094 const char *oname
= name
;
1096 while (*name
!= '\0') {
1097 if (origin
!= NULL
&& ns_samename(name
, origin
) == 1)
1098 return (name
- oname
- (name
> oname
));
1099 while (*name
!= '\0') {
1100 if (*name
== '\\') {
1102 /* XXX need to handle \nnn form. */
1105 } else if (*name
== '.') {
1112 return (name
- oname
);
1117 * charstr(rdata, edata, buf, buflen)
1118 * Format a <character-string> into the presentation buffer.
1120 * Number of rdata octets consumed
1121 * 0 for protocol format error
1122 * -1 for output buffer error
1124 * buffer is advanced on success.
1127 charstr(const u_char
*rdata
, const u_char
*edata
, char **buf
, size_t *buflen
) {
1128 const u_char
*odata
= rdata
;
1129 size_t save_buflen
= *buflen
;
1130 char *save_buf
= *buf
;
1132 if (addstr("\"", 1, buf
, buflen
) < 0)
1134 if (rdata
< edata
) {
1137 if (rdata
+ 1 + n
<= edata
) {
1140 if (strchr("\n\"\\", *rdata
) != NULL
)
1141 if (addstr("\\", 1, buf
, buflen
) < 0)
1143 if (addstr((const char *)rdata
, 1,
1150 if (addstr("\"", 1, buf
, buflen
) < 0)
1152 return (rdata
- odata
);
1156 *buflen
= save_buflen
;
1161 addname(const u_char
*msg
, size_t msglen
,
1162 const u_char
**pp
, const char *origin
,
1163 char **buf
, size_t *buflen
)
1165 size_t newlen
, save_buflen
= *buflen
;
1166 char *save_buf
= *buf
;
1169 n
= dn_expand(msg
, msg
+ msglen
, *pp
, *buf
, *buflen
);
1171 goto enospc
; /*%< Guess. */
1172 newlen
= prune_origin(*buf
, origin
);
1173 if (**buf
== '\0') {
1175 } else if (newlen
== 0U) {
1176 /* Use "@" instead of name. */
1177 if (newlen
+ 2 > *buflen
)
1178 goto enospc
; /* No room for "@\0". */
1179 (*buf
)[newlen
++] = '@';
1180 (*buf
)[newlen
] = '\0';
1182 if (((origin
== NULL
|| origin
[0] == '\0') ||
1183 (origin
[0] != '.' && origin
[1] != '\0' &&
1184 (*buf
)[newlen
] == '\0')) && (*buf
)[newlen
- 1] != '.') {
1185 /* No trailing dot. */
1187 if (newlen
+ 2 > *buflen
)
1188 goto enospc
; /* No room for ".\0". */
1189 (*buf
)[newlen
++] = '.';
1190 (*buf
)[newlen
] = '\0';
1194 addlen(newlen
, buf
, buflen
);
1200 *buflen
= save_buflen
;
1205 addlen(size_t len
, char **buf
, size_t *buflen
) {
1206 INSIST(len
<= *buflen
);
1212 addstr(const char *src
, size_t len
, char **buf
, size_t *buflen
) {
1213 if (len
>= *buflen
) {
1217 memcpy(*buf
, src
, len
);
1218 addlen(len
, buf
, buflen
);
1224 addtab(size_t len
, size_t target
, int spaced
, char **buf
, size_t *buflen
) {
1225 size_t save_buflen
= *buflen
;
1226 char *save_buf
= *buf
;
1229 if (spaced
|| len
>= target
- 1) {
1230 T(addstr(" ", 2, buf
, buflen
));
1233 for (t
= (target
- len
- 1) / 8; t
>= 0; t
--)
1234 if (addstr("\t", 1, buf
, buflen
) < 0) {
1235 *buflen
= save_buflen
;