]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/ns_print.c
2 * Copyright (c) 1996, 1998 by Internet Software Consortium.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
19 * Portions copyright (c) 1999, 2000
21 * All rights reserved.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
37 * This product includes software developed by Intel Corporation and
40 * 4. Neither the name of Intel Corporation or its contributors may be
41 * used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
45 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
48 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54 * THE POSSIBILITY OF SUCH DAMAGE.
59 static char rcsid
[] = "$Id: ns_print.c,v 1.1.1.1 2003/11/19 01:51:34 kyu3 Exp $";
64 #include <sys/types.h>
65 #include <sys/socket.h>
67 #include <netinet/in.h>
68 #include <arpa/nameser.h>
69 #include <arpa/inet.h>
77 #define SPRINTF(x) (sprintf x)
81 static size_t prune_origin(const char *name
, const char *origin
);
82 static int charstr(const u_char
*rdata
, const u_char
*edata
,
83 char **buf
, size_t *buflen
);
84 static int addname(const u_char
*msg
, size_t msglen
,
85 const u_char
**p
, const char *origin
,
86 char **buf
, size_t *buflen
);
87 static void addlen(size_t len
, char **buf
, size_t *buflen
);
88 static int addstr(const char *src
, size_t len
,
89 char **buf
, size_t *buflen
);
90 static int addtab(size_t len
, size_t target
, int spaced
,
91 char **buf
, size_t *buflen
);
97 if ((ssize_t)(x) < 0) \
105 * ns_sprintrr(handle, rr, name_ctx, origin, buf, buflen)
106 * Convert an RR to presentation format.
108 * Number of characters written to buf, or -1 (check errno).
111 ns_sprintrr(const ns_msg
*handle
, const ns_rr
*rr
,
112 const char *name_ctx
, const char *origin
,
113 char *buf
, size_t buflen
)
117 n
= ns_sprintrrf(ns_msg_base(*handle
), ns_msg_size(*handle
),
118 ns_rr_name(*rr
), ns_rr_class(*rr
), ns_rr_type(*rr
),
119 ns_rr_ttl(*rr
), ns_rr_rdata(*rr
), ns_rr_rdlen(*rr
),
120 name_ctx
, origin
, buf
, buflen
);
126 * ns_sprintrrf(msg, msglen, name, class, type, ttl, rdata, rdlen,
127 * name_ctx, origin, buf, buflen)
128 * Convert the fields of an RR into presentation format.
130 * Number of characters written to buf, or -1 (check errno).
133 ns_sprintrrf(const u_char
*msg
, size_t msglen
,
134 const char *name
, ns_class
class, ns_type type
,
135 u_long ttl
, const u_char
*rdata
, size_t rdlen
,
136 const char *name_ctx
, const char *origin
,
137 char *buf
, size_t buflen
)
139 const char *obuf
= buf
;
140 const u_char
*edata
= rdata
+ rdlen
;
148 static char base64_key
[NS_MD5RSA_MAX_BASE64
];
149 static char t
[255*3];
154 if (name_ctx
!= NULL
&& strcasecmp(name_ctx
, name
) == 0) {
155 T(addstr("\t\t\t", 3, &buf
, &buflen
));
157 len
= prune_origin(name
, origin
);
159 T(addstr("@\t\t\t", 4, &buf
, &buflen
));
161 T(addstr(name
, len
, &buf
, &buflen
));
162 /* Origin not used and no trailing dot? */
163 if ((!origin
|| !origin
[0] || name
[len
] == '\0') &&
164 name
[len
- 1] != '.') {
165 T(addstr(".", 1, &buf
, &buflen
));
168 T(spaced
= addtab(len
, 24, spaced
, &buf
, &buflen
));
175 T(x
= ns_format_ttl(ttl
, buf
, buflen
));
176 addlen(x
, &buf
, &buflen
);
177 len
= SPRINTF((tmp
, " %s %s", p_class(class), p_type(type
)));
178 T(addstr(tmp
, len
, &buf
, &buflen
));
179 T(spaced
= addtab(x
+ len
, 16, spaced
, &buf
, &buflen
));
186 if (rdlen
!= NS_INADDRSZ
)
188 (void) inet_ntop(AF_INET
, rdata
, buf
, (socklen_t
)buflen
);
189 addlen(strlen(buf
), &buf
, &buflen
);
198 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
204 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
208 T(addstr(" ", 1, &buf
, &buflen
));
211 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
221 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
222 T(addstr(" ", 1, &buf
, &buflen
));
224 /* Administrator name. */
225 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
226 T(addstr(" (\n", 3, &buf
, &buflen
));
229 if ((edata
- rdata
) != 5*NS_INT32SZ
)
233 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
234 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
235 len
= SPRINTF((tmp
, "%lu", t
));
236 T(addstr(tmp
, len
, &buf
, &buflen
));
237 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
238 T(addstr("; serial\n", 9, &buf
, &buflen
));
241 /* Refresh interval. */
242 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
243 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
244 T(len
= ns_format_ttl(t
, buf
, buflen
));
245 addlen(len
, &buf
, &buflen
);
246 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
247 T(addstr("; refresh\n", 10, &buf
, &buflen
));
250 /* Retry interval. */
251 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
252 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
253 T(len
= ns_format_ttl(t
, buf
, buflen
));
254 addlen(len
, &buf
, &buflen
);
255 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
256 T(addstr("; retry\n", 8, &buf
, &buflen
));
260 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
261 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
262 T(len
= ns_format_ttl(t
, buf
, buflen
));
263 addlen(len
, &buf
, &buflen
);
264 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
265 T(addstr("; expiry\n", 9, &buf
, &buflen
));
269 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
270 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
271 T(len
= ns_format_ttl(t
, buf
, buflen
));
272 addlen(len
, &buf
, &buflen
);
273 T(addstr(" )", 2, &buf
, &buflen
));
274 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
275 T(addstr("; minimum\n", 10, &buf
, &buflen
));
285 if (rdlen
< NS_INT16SZ
)
291 len
= SPRINTF((tmp
, "%u ", t
));
292 T(addstr(tmp
, len
, &buf
, &buflen
));
295 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
303 if (rdlen
< NS_INT16SZ
)
309 len
= SPRINTF((tmp
, "%u ", t
));
310 T(addstr(tmp
, len
, &buf
, &buflen
));
313 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
314 T(addstr(" ", 1, &buf
, &buflen
));
317 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
323 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
330 while (rdata
< edata
) {
331 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
336 T(addstr(" ", 1, &buf
, &buflen
));
342 (void) inet_nsap_ntoa((int)rdlen
, rdata
, t
);
343 T(addstr(t
, strlen(t
), &buf
, &buflen
));
348 if (rdlen
!= NS_IN6ADDRSZ
)
350 (void) inet_ntop(AF_INET6
, rdata
, buf
, (socklen_t
)buflen
);
351 addlen(strlen(buf
), &buf
, &buflen
);
355 /* XXX protocol format checking? */
356 (void) loc_ntoa(rdata
, t
);
357 T(addstr(t
, strlen(t
), &buf
, &buflen
));
362 u_int order
, preference
;
364 if (rdlen
< 2*NS_INT16SZ
)
367 /* Order, Precedence. */
368 order
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
369 preference
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
370 len
= SPRINTF((t
, "%u %u ", order
, preference
));
371 T(addstr(t
, len
, &buf
, &buflen
));
374 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
378 T(addstr(" ", 1, &buf
, &buflen
));
381 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
385 T(addstr(" ", 1, &buf
, &buflen
));
388 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
389 if ((ssize_t
)len
< 0)
394 T(addstr(" ", 1, &buf
, &buflen
));
397 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
402 u_int priority
, weight
, port
;
404 if (rdlen
< NS_INT16SZ
*3)
407 /* Priority, Weight, Port. */
408 priority
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
409 weight
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
410 port
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
411 len
= SPRINTF((t
, "%u %u %u ", priority
, weight
, port
));
412 T(addstr(t
, len
, &buf
, &buflen
));
415 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
422 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
423 T(addstr(" ", 1, &buf
, &buflen
));
426 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
433 if (rdlen
< NS_INT32SZ
+ 1)
437 (void) inet_ntop(AF_INET
, rdata
, buf
, (socklen_t
)buflen
);
438 addlen(strlen(buf
), &buf
, &buflen
);
439 rdata
+= NS_INADDRSZ
;
442 len
= SPRINTF((tmp
, " %u ( ", *rdata
));
443 T(addstr(tmp
, len
, &buf
, &buflen
));
449 while (rdata
< edata
) {
454 T(addstr("\n\t\t\t\t", 5,
459 len
= SPRINTF((tmp
, "%d ", n
));
460 T(addstr(tmp
, len
, &buf
, &buflen
));
466 T(addstr(")", 1, &buf
, &buflen
));
472 u_int keyflags
, protocol
, algorithm
;
476 if (rdlen
< NS_INT16SZ
+ NS_INT8SZ
+ NS_INT8SZ
)
479 /* Key flags, Protocol, Algorithm. */
480 keyflags
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
482 algorithm
= *rdata
++;
483 len
= SPRINTF((tmp
, "0x%04x %u %u",
484 keyflags
, protocol
, algorithm
));
485 T(addstr(tmp
, len
, &buf
, &buflen
));
487 /* Public key data. */
488 len
= b64_ntop(rdata
, edata
- rdata
,
489 base64_key
, sizeof base64_key
);
490 if ((ssize_t
)len
< 0)
493 T(addstr(" (", 2, &buf
, &buflen
));
498 for (n
= 0; n
< (int)len
; n
+= 48) {
499 T(addstr(leader
, strlen(leader
), &buf
, &buflen
));
500 T(addstr(base64_key
+ n
, MIN(len
- n
, 48),
504 T(addstr(" )", 2, &buf
, &buflen
));
510 u_int type
, algorithm
, labels
, footprint
;
518 /* Type covered, Algorithm, Label count, Original TTL. */
519 type
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
520 algorithm
= *rdata
++;
522 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
523 len
= SPRINTF((tmp
, " %s %d %lu ",
524 p_type((int)type
), algorithm
, t
));
525 T(addstr(tmp
, len
, &buf
, &buflen
));
526 if (labels
!= (u_int
)dn_count_labels(name
))
529 /* Signature expiry. */
530 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
531 len
= SPRINTF((tmp
, "%s ", p_secstodate(t
)));
532 T(addstr(tmp
, len
, &buf
, &buflen
));
535 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
536 len
= SPRINTF((tmp
, "%s ", p_secstodate(t
)));
537 T(addstr(tmp
, len
, &buf
, &buflen
));
539 /* Signature Footprint. */
540 footprint
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
541 len
= SPRINTF((tmp
, "%u ", footprint
));
542 T(addstr(tmp
, len
, &buf
, &buflen
));
545 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
548 len
= b64_ntop(rdata
, edata
- rdata
,
549 base64_key
, sizeof base64_key
);
551 T(addstr(" (", 2, &buf
, &buflen
));
556 if ((ssize_t
)len
< 0)
558 for (n
= 0; n
< (int)len
; n
+= 48) {
559 T(addstr(leader
, strlen(leader
), &buf
, &buflen
));
560 T(addstr(base64_key
+ n
, MIN(len
- n
, 48),
564 T(addstr(" )", 2, &buf
, &buflen
));
572 /* Next domain name. */
573 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
576 n
= (int)(edata
- rdata
);
577 for (c
= 0; c
< n
*8; c
++)
578 if (NS_NXT_BIT_ISSET(c
, rdata
)) {
579 len
= SPRINTF((tmp
, " %s", p_type(c
)));
580 T(addstr(tmp
, len
, &buf
, &buflen
));
586 comment
= "unknown RR type";
589 return ((int)(buf
- obuf
));
591 comment
= "RR format error";
596 len
= SPRINTF((tmp
, "\\#(\t\t; %s", comment
));
597 T(addstr(tmp
, len
, &buf
, &buflen
));
598 while (rdata
< edata
) {
600 p
+= SPRINTF((p
, "\n\t"));
602 n
= MIN(16, (int)(edata
- rdata
));
603 for (m
= 0; m
< n
; m
++)
604 p
+= SPRINTF((p
, "%02x ", rdata
[m
]));
605 T(addstr(tmp
, (u_int
)(p
- tmp
), &buf
, &buflen
));
607 T(addstr(")", 1, &buf
, &buflen
));
608 T(addtab((u_int
)(p
- tmp
) + 1, 48, spaced
, &buf
, &buflen
));
611 p
+= SPRINTF((p
, "; "));
612 for (m
= 0; m
< n
; m
++)
613 *p
++ = (isascii(rdata
[m
]) && isprint(rdata
[m
]))
616 T(addstr(tmp
, (u_int
)(p
- tmp
), &buf
, &buflen
));
619 return ((int)(buf
- obuf
));
627 * prune_origin(name, origin)
628 * Find out if the name is at or under the current origin.
630 * Number of characters in name before start of origin,
631 * or length of name if origin does not match.
633 * This function should share code with samedomain().
636 prune_origin(const char *name
, const char *origin
) {
637 const char *oname
= name
;
639 while (*name
!= '\0') {
640 if (origin
!= NULL
&& strcasecmp(name
, origin
) == 0)
641 return ((size_t)(name
- oname
) - (name
> oname
));
642 while (*name
!= '\0') {
645 /* XXX need to handle \nnn form. */
648 } else if (*name
== '.') {
655 return ((size_t)(name
- oname
));
660 * charstr(rdata, edata, buf, buflen)
661 * Format a <character-string> into the presentation buffer.
663 * Number of rdata octets consumed
664 * 0 for protocol format error
665 * -1 for output buffer error
667 * buffer is advanced on success.
670 charstr(const u_char
*rdata
, const u_char
*edata
, char **buf
, size_t *buflen
) {
671 const u_char
*odata
= rdata
;
672 size_t save_buflen
= *buflen
;
673 char *save_buf
= *buf
;
675 if (addstr("\"", 1, buf
, buflen
) < 0)
680 if (rdata
+ 1 + n
<= edata
) {
683 if (strchr("\n\"\\", *rdata
) != NULL
)
684 if (addstr("\\", 1, buf
, buflen
) < 0)
686 if (addstr((const char *)rdata
, 1,
693 if (addstr("\"", 1, buf
, buflen
) < 0)
695 return ((int)(rdata
- odata
));
699 *buflen
= save_buflen
;
704 addname(const u_char
*msg
, size_t msglen
,
705 const u_char
**pp
, const char *origin
,
706 char **buf
, size_t *buflen
)
708 size_t newlen
, save_buflen
= *buflen
;
709 char *save_buf
= *buf
;
712 n
= dn_expand(msg
, msg
+ msglen
, *pp
, *buf
, (int)(*buflen
));
714 goto enospc
; /* Guess. */
715 newlen
= prune_origin(*buf
, origin
);
716 if ((origin
== NULL
|| origin
[0] == '\0' || (*buf
)[newlen
] == '\0') &&
717 (newlen
== 0 || (*buf
)[newlen
- 1] != '.')) {
718 /* No trailing dot. */
719 if (newlen
+ 2 > *buflen
)
720 goto enospc
; /* No room for ".\0". */
721 (*buf
)[newlen
++] = '.';
722 (*buf
)[newlen
] = '\0';
725 /* Use "@" instead of name. */
726 if (newlen
+ 2 > *buflen
)
727 goto enospc
; /* No room for "@\0". */
728 (*buf
)[newlen
++] = '@';
729 (*buf
)[newlen
] = '\0';
732 addlen(newlen
, buf
, buflen
);
734 return ((int)newlen
);
738 *buflen
= save_buflen
;
743 addlen(size_t len
, char **buf
, size_t *buflen
) {
744 assert(len
<= *buflen
);
750 addstr(const char *src
, size_t len
, char **buf
, size_t *buflen
) {
755 memcpy(*buf
, src
, len
);
756 addlen(len
, buf
, buflen
);
762 addtab(size_t len
, size_t target
, int spaced
, char **buf
, size_t *buflen
) {
763 size_t save_buflen
= *buflen
;
764 char *save_buf
= *buf
;
767 if (spaced
|| len
>= target
- 1) {
768 T(addstr(" ", 2, buf
, buflen
));
771 for (t
= (int)(target
- len
- 1) / 8; t
>= 0; t
--)
772 if (addstr("\t", 1, buf
, buflen
) < 0) {
773 *buflen
= save_buflen
;