2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996-1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
10 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
12 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
19 #if !defined(_LIBC) && !defined(lint)
20 static const char rcsid
[] = "$BINDId: ns_print.c,v 8.18 2000/02/29 05:48:12 vixie Exp $";
25 #include <sys/types.h>
26 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/nameser.h>
30 #include <arpa/inet.h>
38 #define SPRINTF(x) ((size_t)sprintf x)
42 static size_t prune_origin(const char *name
, const char *origin
);
43 static int charstr(const u_char
*rdata
, const u_char
*edata
,
44 char **buf
, size_t *buflen
);
45 static int addname(const u_char
*msg
, size_t msglen
,
46 const u_char
**p
, const char *origin
,
47 char **buf
, size_t *buflen
);
48 static void addlen(size_t len
, char **buf
, size_t *buflen
);
49 static int addstr(const char *src
, size_t len
,
50 char **buf
, size_t *buflen
);
51 static int addtab(size_t len
, size_t target
, int spaced
,
52 char **buf
, size_t *buflen
);
54 static u_int16_t
dst_s_dns_key_id(const u_char
*, const int);
67 * Convert an RR to presentation format.
70 *\li Number of characters written to buf, or -1 (check errno).
73 ns_sprintrr(const ns_msg
*handle
, const ns_rr
*rr
,
74 const char *name_ctx
, const char *origin
,
75 char *buf
, size_t buflen
)
79 n
= ns_sprintrrf(ns_msg_base(*handle
), ns_msg_size(*handle
),
80 ns_rr_name(*rr
), ns_rr_class(*rr
), ns_rr_type(*rr
),
81 ns_rr_ttl(*rr
), ns_rr_rdata(*rr
), ns_rr_rdlen(*rr
),
82 name_ctx
, origin
, buf
, buflen
);
85 libresolv_hidden_def (ns_sprintrr
)
88 * Convert the fields of an RR into presentation format.
91 *\li Number of characters written to buf, or -1 (check errno).
94 ns_sprintrrf(const u_char
*msg
, size_t msglen
,
95 const char *name
, ns_class
class, ns_type type
,
96 u_long ttl
, const u_char
*rdata
, size_t rdlen
,
97 const char *name_ctx
, const char *origin
,
98 char *buf
, size_t buflen
)
100 const char *obuf
= buf
;
101 const u_char
*edata
= rdata
+ rdlen
;
112 if (name_ctx
!= NULL
&& ns_samename(name_ctx
, name
) == 1) {
113 T(addstr("\t\t\t", 3, &buf
, &buflen
));
115 len
= prune_origin(name
, origin
);
118 } else if (len
== 0) {
119 T(addstr("@\t\t\t", 4, &buf
, &buflen
));
121 T(addstr(name
, len
, &buf
, &buflen
));
122 /* Origin not used or not root, and no trailing dot? */
123 if (((origin
== NULL
|| origin
[0] == '\0') ||
124 (origin
[0] != '.' && origin
[1] != '\0' &&
125 name
[len
] == '\0')) && name
[len
- 1] != '.') {
127 T(addstr(".", 1, &buf
, &buflen
));
130 T(spaced
= addtab(len
, 24, spaced
, &buf
, &buflen
));
137 T(x
= ns_format_ttl(ttl
, buf
, buflen
));
138 addlen(x
, &buf
, &buflen
);
139 len
= SPRINTF((tmp
, " %s %s", p_class(class), p_type(type
)));
140 T(addstr(tmp
, len
, &buf
, &buflen
));
141 T(spaced
= addtab(x
+ len
, 16, spaced
, &buf
, &buflen
));
148 if (rdlen
!= (size_t)NS_INADDRSZ
)
150 (void) inet_ntop(AF_INET
, rdata
, buf
, buflen
);
151 addlen(strlen(buf
), &buf
, &buflen
);
161 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
167 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
171 T(addstr(" ", 1, &buf
, &buflen
));
174 /* Second word, optional in ISDN records. */
175 if (type
== ns_t_isdn
&& rdata
== edata
)
178 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
188 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
189 T(addstr(" ", 1, &buf
, &buflen
));
191 /* Administrator name. */
192 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
193 T(addstr(" (\n", 3, &buf
, &buflen
));
196 if ((edata
- rdata
) != 5*NS_INT32SZ
)
200 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
201 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
202 len
= SPRINTF((tmp
, "%lu", t
));
203 T(addstr(tmp
, len
, &buf
, &buflen
));
204 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
205 T(addstr("; serial\n", 9, &buf
, &buflen
));
208 /* Refresh interval. */
209 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
210 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
211 T(len
= ns_format_ttl(t
, buf
, buflen
));
212 addlen(len
, &buf
, &buflen
);
213 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
214 T(addstr("; refresh\n", 10, &buf
, &buflen
));
217 /* Retry 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("; retry\n", 8, &buf
, &buflen
));
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("; expiry\n", 9, &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(addstr(" )", 2, &buf
, &buflen
));
241 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
242 T(addstr("; minimum\n", 10, &buf
, &buflen
));
252 if (rdlen
< (size_t)NS_INT16SZ
)
258 len
= SPRINTF((tmp
, "%u ", t
));
259 T(addstr(tmp
, len
, &buf
, &buflen
));
262 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
270 if (rdlen
< (size_t)NS_INT16SZ
)
276 len
= SPRINTF((tmp
, "%u ", t
));
277 T(addstr(tmp
, len
, &buf
, &buflen
));
280 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
281 T(addstr(" ", 1, &buf
, &buflen
));
284 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
290 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
297 while (rdata
< edata
) {
298 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
303 T(addstr(" ", 1, &buf
, &buflen
));
310 (void) inet_nsap_ntoa(rdlen
, rdata
, t
);
311 T(addstr(t
, strlen(t
), &buf
, &buflen
));
316 if (rdlen
!= (size_t)NS_IN6ADDRSZ
)
318 (void) inet_ntop(AF_INET6
, rdata
, buf
, buflen
);
319 addlen(strlen(buf
), &buf
, &buflen
);
325 /* XXX protocol format checking? */
326 (void) loc_ntoa(rdata
, t
);
327 T(addstr(t
, strlen(t
), &buf
, &buflen
));
332 u_int order
, preference
;
335 if (rdlen
< 2U*NS_INT16SZ
)
338 /* Order, Precedence. */
339 order
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
340 preference
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
341 len
= SPRINTF((t
, "%u %u ", order
, preference
));
342 T(addstr(t
, len
, &buf
, &buflen
));
345 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
349 T(addstr(" ", 1, &buf
, &buflen
));
352 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
356 T(addstr(" ", 1, &buf
, &buflen
));
359 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
365 T(addstr(" ", 1, &buf
, &buflen
));
368 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
373 u_int priority
, weight
, port
;
376 if (rdlen
< 3U*NS_INT16SZ
)
379 /* Priority, Weight, Port. */
380 priority
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
381 weight
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
382 port
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
383 len
= SPRINTF((t
, "%u %u %u ", priority
, weight
, port
));
384 T(addstr(t
, len
, &buf
, &buflen
));
387 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
394 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
395 T(addstr(" ", 1, &buf
, &buflen
));
398 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
405 if (rdlen
< 1U + NS_INT32SZ
)
409 (void) inet_ntop(AF_INET
, rdata
, buf
, buflen
);
410 addlen(strlen(buf
), &buf
, &buflen
);
411 rdata
+= NS_INADDRSZ
;
414 len
= SPRINTF((tmp
, " %u ( ", *rdata
));
415 T(addstr(tmp
, len
, &buf
, &buflen
));
421 while (rdata
< edata
) {
426 T(addstr("\n\t\t\t\t", 5,
431 len
= SPRINTF((tmp
, "%d ", n
));
432 T(addstr(tmp
, len
, &buf
, &buflen
));
438 T(addstr(")", 1, &buf
, &buflen
));
444 char base64_key
[NS_MD5RSA_MAX_BASE64
];
445 u_int keyflags
, protocol
, algorithm
, key_id
;
449 if (rdlen
< 0U + NS_INT16SZ
+ NS_INT8SZ
+ NS_INT8SZ
)
452 /* Key flags, Protocol, Algorithm. */
453 key_id
= dst_s_dns_key_id(rdata
, edata
-rdata
);
454 keyflags
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
456 algorithm
= *rdata
++;
457 len
= SPRINTF((tmp
, "0x%04x %u %u",
458 keyflags
, protocol
, algorithm
));
459 T(addstr(tmp
, len
, &buf
, &buflen
));
461 /* Public key data. */
462 len
= b64_ntop(rdata
, edata
- rdata
,
463 base64_key
, sizeof base64_key
);
467 T(addstr(" (", 2, &buf
, &buflen
));
472 for (n
= 0; n
< len
; n
+= 48) {
473 T(addstr(leader
, strlen(leader
), &buf
, &buflen
));
474 T(addstr(base64_key
+ n
, MIN(len
- n
, 48),
478 T(addstr(" )", 2, &buf
, &buflen
));
479 n
= SPRINTF((tmp
, " ; key_tag= %u", key_id
));
480 T(addstr(tmp
, n
, &buf
, &buflen
));
486 char base64_key
[NS_MD5RSA_MAX_BASE64
];
487 u_int type
, algorithm
, labels
, footprint
;
495 /* Type covered, Algorithm, Label count, Original TTL. */
496 type
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
497 algorithm
= *rdata
++;
499 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
500 len
= SPRINTF((tmp
, "%s %d %d %lu ",
501 p_type(type
), algorithm
, labels
, t
));
502 T(addstr(tmp
, len
, &buf
, &buflen
));
503 if (labels
> (u_int
)dn_count_labels(name
))
506 /* Signature expiry. */
507 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
508 len
= SPRINTF((tmp
, "%s ", p_secstodate(t
)));
509 T(addstr(tmp
, len
, &buf
, &buflen
));
512 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
513 len
= SPRINTF((tmp
, "%s ", p_secstodate(t
)));
514 T(addstr(tmp
, len
, &buf
, &buflen
));
516 /* Signature Footprint. */
517 footprint
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
518 len
= SPRINTF((tmp
, "%u ", footprint
));
519 T(addstr(tmp
, len
, &buf
, &buflen
));
522 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
525 len
= b64_ntop(rdata
, edata
- rdata
,
526 base64_key
, sizeof base64_key
);
528 T(addstr(" (", 2, &buf
, &buflen
));
535 for (n
= 0; n
< len
; n
+= 48) {
536 T(addstr(leader
, strlen(leader
), &buf
, &buflen
));
537 T(addstr(base64_key
+ n
, MIN(len
- n
, 48),
541 T(addstr(" )", 2, &buf
, &buflen
));
548 /* Next domain name. */
549 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
553 for (c
= 0; c
< n
*8; c
++)
554 if (NS_NXT_BIT_ISSET(c
, rdata
)) {
555 len
= SPRINTF((tmp
, " %s", p_type(c
)));
556 T(addstr(tmp
, len
, &buf
, &buflen
));
562 u_int c_type
, key_tag
, alg
;
565 char base64_cert
[8192], tmp
[40];
568 c_type
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
569 key_tag
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
570 alg
= (u_int
) *rdata
++;
572 len
= SPRINTF((tmp
, "%d %d %d ", c_type
, key_tag
, alg
));
573 T(addstr(tmp
, len
, &buf
, &buflen
));
574 siz
= (edata
-rdata
)*4/3 + 4; /* "+4" accounts for trailing \0 */
575 if (siz
> sizeof(base64_cert
) * 3/4) {
576 const char *str
= "record too long to print";
577 T(addstr(str
, strlen(str
), &buf
, &buflen
));
580 len
= b64_ntop(rdata
, edata
-rdata
, base64_cert
, siz
);
585 T(addstr(" (", 2, &buf
, &buflen
));
592 for (n
= 0; n
< len
; n
+= 48) {
593 T(addstr(leader
, strlen(leader
),
595 T(addstr(base64_cert
+ n
, MIN(len
- n
, 48),
599 T(addstr(" )", 2, &buf
, &buflen
));
605 /* KJD - need to complete this */
607 int mode
, err
, keysize
;
609 /* Algorithm name. */
610 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
611 T(addstr(" ", 1, &buf
, &buflen
));
614 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
615 len
= SPRINTF((tmp
, "%s ", p_secstodate(t
)));
616 T(addstr(tmp
, len
, &buf
, &buflen
));
619 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
620 len
= SPRINTF((tmp
, "%s ", p_secstodate(t
)));
621 T(addstr(tmp
, len
, &buf
, &buflen
));
623 /* Mode , Error, Key Size. */
624 /* Priority, Weight, Port. */
625 mode
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
626 err
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
627 keysize
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
628 len
= SPRINTF((tmp
, "%u %u %u ", mode
, err
, keysize
));
629 T(addstr(tmp
, len
, &buf
, &buflen
));
631 /* XXX need to dump key, print otherdata length & other data */
636 /* BEW - need to complete this */
639 T(len
= addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
640 T(addstr(" ", 1, &buf
, &buflen
));
641 rdata
+= 8; /*%< time */
642 n
= ns_get16(rdata
); rdata
+= INT16SZ
;
643 rdata
+= n
; /*%< sig */
644 n
= ns_get16(rdata
); rdata
+= INT16SZ
; /*%< original id */
645 sprintf(buf
, "%d", ns_get16(rdata
));
647 addlen(strlen(buf
), &buf
, &buflen
);
656 if (rdlen
== 0U) goto formerr
;
657 len
= SPRINTF((tmp
, "%d ", *rdata
));
658 T(addstr(tmp
, len
, &buf
, &buflen
));
660 if (pbit
> 128) goto formerr
;
661 pbyte
= (pbit
& ~7) / 8;
664 /* address suffix: provided only when prefix len != 128 */
666 if (rdata
+ pbyte
>= edata
) goto formerr
;
667 memset(&a
, 0, sizeof(a
));
668 memcpy(&a
.s6_addr
[pbyte
], rdata
, sizeof(a
) - pbyte
);
669 (void) inet_ntop(AF_INET6
, &a
, buf
, buflen
);
670 addlen(strlen(buf
), &buf
, &buflen
);
671 rdata
+= sizeof(a
) - pbyte
;
674 /* prefix name: provided only when prefix len > 0 */
677 if (rdata
>= edata
) goto formerr
;
678 T(addstr(" ", 1, &buf
, &buflen
));
679 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
685 len
= SPRINTF((tmp
, "%u bytes", class));
686 T(addstr(tmp
, len
, &buf
, &buflen
));
691 snprintf (errbuf
, sizeof (errbuf
), "unknown RR type %d", type
);
697 comment
= "RR format error";
702 len
= SPRINTF((tmp
, "\\# %u%s\t; %s", (unsigned)(edata
- rdata
),
703 rdlen
!= 0U ? " (" : "", comment
));
704 T(addstr(tmp
, len
, &buf
, &buflen
));
705 while (rdata
< edata
) {
707 p
+= SPRINTF((p
, "\n\t"));
709 n
= MIN(16, edata
- rdata
);
710 for (m
= 0; m
< n
; m
++)
711 p
+= SPRINTF((p
, "%02x ", rdata
[m
]));
712 T(addstr(tmp
, p
- tmp
, &buf
, &buflen
));
714 T(addstr(")", 1, &buf
, &buflen
));
715 T(addtab(p
- tmp
+ 1, 48, spaced
, &buf
, &buflen
));
718 p
+= SPRINTF((p
, "; "));
719 for (m
= 0; m
< n
; m
++)
720 *p
++ = (isascii(rdata
[m
]) && isprint(rdata
[m
]))
723 T(addstr(tmp
, p
- tmp
, &buf
, &buflen
));
729 libresolv_hidden_def (ns_sprintrrf
)
735 * prune_origin(name, origin)
736 * Find out if the name is at or under the current origin.
738 * Number of characters in name before start of origin,
739 * or length of name if origin does not match.
741 * This function should share code with samedomain().
744 prune_origin(const char *name
, const char *origin
) {
745 const char *oname
= name
;
747 while (*name
!= '\0') {
748 if (origin
!= NULL
&& ns_samename(name
, origin
) == 1)
749 return (name
- oname
- (name
> oname
));
750 while (*name
!= '\0') {
753 /* XXX need to handle \nnn form. */
756 } else if (*name
== '.') {
763 return (name
- oname
);
768 * charstr(rdata, edata, buf, buflen)
769 * Format a <character-string> into the presentation buffer.
771 * Number of rdata octets consumed
772 * 0 for protocol format error
773 * -1 for output buffer error
775 * buffer is advanced on success.
778 charstr(const u_char
*rdata
, const u_char
*edata
, char **buf
, size_t *buflen
) {
779 const u_char
*odata
= rdata
;
780 size_t save_buflen
= *buflen
;
781 char *save_buf
= *buf
;
783 if (addstr("\"", 1, buf
, buflen
) < 0)
788 if (rdata
+ 1 + n
<= edata
) {
791 if (strchr("\n\"\\", *rdata
) != NULL
)
792 if (addstr("\\", 1, buf
, buflen
) < 0)
794 if (addstr((const char *)rdata
, 1,
801 if (addstr("\"", 1, buf
, buflen
) < 0)
803 return (rdata
- odata
);
805 __set_errno (ENOSPC
);
807 *buflen
= save_buflen
;
812 addname(const u_char
*msg
, size_t msglen
,
813 const u_char
**pp
, const char *origin
,
814 char **buf
, size_t *buflen
)
816 size_t newlen
, save_buflen
= *buflen
;
817 char *save_buf
= *buf
;
820 n
= dn_expand(msg
, msg
+ msglen
, *pp
, *buf
, *buflen
);
822 goto enospc
; /*%< Guess. */
823 newlen
= prune_origin(*buf
, origin
);
826 } else if (newlen
== 0U) {
827 /* Use "@" instead of name. */
828 if (newlen
+ 2 > *buflen
)
829 goto enospc
; /* No room for "@\0". */
830 (*buf
)[newlen
++] = '@';
831 (*buf
)[newlen
] = '\0';
833 if (((origin
== NULL
|| origin
[0] == '\0') ||
834 (origin
[0] != '.' && origin
[1] != '\0' &&
835 (*buf
)[newlen
] == '\0')) && (*buf
)[newlen
- 1] != '.') {
836 /* No trailing dot. */
838 if (newlen
+ 2 > *buflen
)
839 goto enospc
; /* No room for ".\0". */
840 (*buf
)[newlen
++] = '.';
841 (*buf
)[newlen
] = '\0';
845 addlen(newlen
, buf
, buflen
);
849 __set_errno (ENOSPC
);
851 *buflen
= save_buflen
;
856 addlen(size_t len
, char **buf
, size_t *buflen
) {
857 assert(len
<= *buflen
);
863 addstr(const char *src
, size_t len
, char **buf
, size_t *buflen
) {
864 if (len
>= *buflen
) {
865 __set_errno (ENOSPC
);
868 memcpy(*buf
, src
, len
);
869 addlen(len
, buf
, buflen
);
875 addtab(size_t len
, size_t target
, int spaced
, char **buf
, size_t *buflen
) {
876 size_t save_buflen
= *buflen
;
877 char *save_buf
= *buf
;
880 if (spaced
|| len
>= target
- 1) {
881 T(addstr(" ", 2, buf
, buflen
));
884 for (t
= (target
- len
- 1) / 8; t
>= 0; t
--)
885 if (addstr("\t", 1, buf
, buflen
) < 0) {
886 *buflen
= save_buflen
;
895 /* DST algorithm codes */
897 #define KEY_HMAC_MD5 157
900 * calculates a checksum used in dst for an id.
901 * takes an array of bytes and a length.
902 * returns a 16 bit checksum.
905 dst_s_id_calc(const u_char
*key
, const int keysize
)
908 const u_char
*kp
= key
;
911 if (!key
|| (keysize
<= 0))
914 for (ac
= 0; size
> 1; size
-= 2, kp
+= 2)
915 ac
+= ((*kp
) << 8) + *(kp
+ 1);
919 ac
+= (ac
>> 16) & 0xffff;
921 return (ac
& 0xffff);
926 * This routine extracts a 16 bit integer from a two byte character
927 * string. The character string is assumed to be in network byte
928 * order and may be unaligned. The number returned is in host order.
930 * buf A two byte character string.
932 * The converted integer value.
936 dst_s_get_int16(const u_char
*buf
)
938 register u_int16_t a
= 0;
939 a
= ((u_int16_t
)(buf
[0] << 8)) | ((u_int16_t
)(buf
[1]));
944 * dst_s_dns_key_id() Function to calculate DNSSEC footprint from KEY record
947 * dns_key_rdata: the raw data in wire format
948 * rdata_len: the size of the input data
950 * the key footprint/id calculated from the key data
953 dst_s_dns_key_id(const u_char
*dns_key_rdata
, const int rdata_len
)
959 if (dns_key_rdata
[3] == KEY_RSA
) /*%< Algorithm RSA */
960 return dst_s_get_int16((const u_char
*)
961 &dns_key_rdata
[rdata_len
- 3]);
962 else if (dns_key_rdata
[3] == KEY_HMAC_MD5
)
966 /* compute a checksum on the key part of the key rr */
967 return dst_s_id_calc(dns_key_rdata
, rdata_len
);