]>
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 - 2014
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.
60 #include <sys/types.h>
61 #include <sys/socket.h>
63 #include <netinet/in.h>
64 #include <arpa/nameser.h>
65 #include <arpa/inet.h>
73 #define SPRINTF(x) (sprintf x)
77 static size_t prune_origin(const char *name
, const char *origin
);
78 static int charstr(const u_char
*rdata
, const u_char
*edata
,
79 char **buf
, size_t *buflen
);
80 static int addname(const u_char
*msg
, size_t msglen
,
81 const u_char
**p
, const char *origin
,
82 char **buf
, size_t *buflen
);
83 static void addlen(size_t len
, char **buf
, size_t *buflen
);
84 static int addstr(const char *src
, size_t len
,
85 char **buf
, size_t *buflen
);
86 static int addtab(size_t len
, size_t target
, int spaced
,
87 char **buf
, size_t *buflen
);
93 if ((ssize_t)(x) < 0) \
101 * ns_sprintrr(handle, rr, name_ctx, origin, buf, buflen)
102 * Convert an RR to presentation format.
104 * Number of characters written to buf, or -1 (check errno).
107 ns_sprintrr(const ns_msg
*handle
, const ns_rr
*rr
,
108 const char *name_ctx
, const char *origin
,
109 char *buf
, size_t buflen
)
113 n
= ns_sprintrrf(ns_msg_base(*handle
), ns_msg_size(*handle
),
114 ns_rr_name(*rr
), ns_rr_class(*rr
), ns_rr_type(*rr
),
115 ns_rr_ttl(*rr
), ns_rr_rdata(*rr
), ns_rr_rdlen(*rr
),
116 name_ctx
, origin
, buf
, buflen
);
122 * ns_sprintrrf(msg, msglen, name, class, type, ttl, rdata, rdlen,
123 * name_ctx, origin, buf, buflen)
124 * Convert the fields of an RR into presentation format.
126 * Number of characters written to buf, or -1 (check errno).
129 ns_sprintrrf(const u_char
*msg
, size_t msglen
,
130 const char *name
, ns_class
class, ns_type type
,
131 u_long ttl
, const u_char
*rdata
, size_t rdlen
,
132 const char *name_ctx
, const char *origin
,
133 char *buf
, size_t buflen
)
135 const char *obuf
= buf
;
136 const u_char
*edata
= rdata
+ rdlen
;
144 static char base64_key
[NS_MD5RSA_MAX_BASE64
];
145 static char t
[255*3];
150 if (name_ctx
!= NULL
&& strcasecmp(name_ctx
, name
) == 0) {
151 T(addstr("\t\t\t", 3, &buf
, &buflen
));
153 len
= prune_origin(name
, origin
);
155 T(addstr("@\t\t\t", 4, &buf
, &buflen
));
157 T(addstr(name
, len
, &buf
, &buflen
));
158 /* Origin not used and no trailing dot? */
159 if ((!origin
|| !origin
[0] || name
[len
] == '\0') &&
160 name
[len
- 1] != '.') {
161 T(addstr(".", 1, &buf
, &buflen
));
164 T(spaced
= addtab(len
, 24, spaced
, &buf
, &buflen
));
171 T(x
= ns_format_ttl(ttl
, buf
, buflen
));
172 addlen(x
, &buf
, &buflen
);
173 len
= SPRINTF((tmp
, " %s %s", p_class(class), p_type(type
)));
174 T(addstr(tmp
, len
, &buf
, &buflen
));
175 T(spaced
= addtab(x
+ len
, 16, spaced
, &buf
, &buflen
));
182 if (rdlen
!= NS_INADDRSZ
)
184 (void) inet_ntop(AF_INET
, rdata
, buf
, (socklen_t
)buflen
);
185 addlen(strlen(buf
), &buf
, &buflen
);
194 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
200 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
204 T(addstr(" ", 1, &buf
, &buflen
));
207 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
217 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
218 T(addstr(" ", 1, &buf
, &buflen
));
220 /* Administrator name. */
221 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
222 T(addstr(" (\n", 3, &buf
, &buflen
));
225 if ((edata
- rdata
) != 5*NS_INT32SZ
)
229 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
230 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
231 len
= SPRINTF((tmp
, "%lu", (unsigned long)t
));
232 T(addstr(tmp
, len
, &buf
, &buflen
));
233 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
234 T(addstr("; serial\n", 9, &buf
, &buflen
));
237 /* Refresh interval. */
238 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
239 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
240 T(len
= ns_format_ttl(t
, buf
, buflen
));
241 addlen(len
, &buf
, &buflen
);
242 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
243 T(addstr("; refresh\n", 10, &buf
, &buflen
));
246 /* Retry interval. */
247 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
248 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
249 T(len
= ns_format_ttl(t
, buf
, buflen
));
250 addlen(len
, &buf
, &buflen
);
251 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
252 T(addstr("; retry\n", 8, &buf
, &buflen
));
256 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
257 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
258 T(len
= ns_format_ttl(t
, buf
, buflen
));
259 addlen(len
, &buf
, &buflen
);
260 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
261 T(addstr("; expiry\n", 9, &buf
, &buflen
));
265 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
266 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
267 T(len
= ns_format_ttl(t
, buf
, buflen
));
268 addlen(len
, &buf
, &buflen
);
269 T(addstr(" )", 2, &buf
, &buflen
));
270 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
271 T(addstr("; minimum\n", 10, &buf
, &buflen
));
281 if (rdlen
< NS_INT16SZ
)
287 len
= SPRINTF((tmp
, "%u ", (unsigned int)t
));
288 T(addstr(tmp
, len
, &buf
, &buflen
));
291 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
299 if (rdlen
< NS_INT16SZ
)
305 len
= SPRINTF((tmp
, "%u ", (unsigned int)t
));
306 T(addstr(tmp
, len
, &buf
, &buflen
));
309 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
310 T(addstr(" ", 1, &buf
, &buflen
));
313 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
319 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
326 while (rdata
< edata
) {
327 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
332 T(addstr(" ", 1, &buf
, &buflen
));
338 (void) inet_nsap_ntoa((int)rdlen
, rdata
, t
);
339 T(addstr(t
, strlen(t
), &buf
, &buflen
));
344 if (rdlen
!= NS_IN6ADDRSZ
)
346 (void) inet_ntop(AF_INET6
, rdata
, buf
, (socklen_t
)buflen
);
347 addlen(strlen(buf
), &buf
, &buflen
);
351 /* XXX protocol format checking? */
352 (void) loc_ntoa(rdata
, t
);
353 T(addstr(t
, strlen(t
), &buf
, &buflen
));
358 u_int order
, preference
;
360 if (rdlen
< 2*NS_INT16SZ
)
363 /* Order, Precedence. */
364 order
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
365 preference
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
366 len
= SPRINTF((t
, "%u %u ", (unsigned int)order
, (unsigned int)preference
));
367 T(addstr(t
, len
, &buf
, &buflen
));
370 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
374 T(addstr(" ", 1, &buf
, &buflen
));
377 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
381 T(addstr(" ", 1, &buf
, &buflen
));
384 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
385 if ((ssize_t
)len
< 0)
390 T(addstr(" ", 1, &buf
, &buflen
));
393 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
398 u_int priority
, weight
, port
;
400 if (rdlen
< NS_INT16SZ
*3)
403 /* Priority, Weight, Port. */
404 priority
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
405 weight
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
406 port
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
407 len
= SPRINTF((t
, "%u %u %u ", (unsigned int)priority
, (unsigned int)weight
, (unsigned int)port
));
408 T(addstr(t
, len
, &buf
, &buflen
));
411 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
418 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
419 T(addstr(" ", 1, &buf
, &buflen
));
422 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
429 if (rdlen
< NS_INT32SZ
+ 1)
433 (void) inet_ntop(AF_INET
, rdata
, buf
, (socklen_t
)buflen
);
434 addlen(strlen(buf
), &buf
, &buflen
);
435 rdata
+= NS_INADDRSZ
;
438 len
= SPRINTF((tmp
, " %u ( ", (unsigned int)*rdata
));
439 T(addstr(tmp
, len
, &buf
, &buflen
));
445 while (rdata
< edata
) {
450 T(addstr("\n\t\t\t\t", 5,
455 len
= SPRINTF((tmp
, "%d ", n
));
456 T(addstr(tmp
, len
, &buf
, &buflen
));
462 T(addstr(")", 1, &buf
, &buflen
));
468 u_int keyflags
, protocol
, algorithm
;
472 if (rdlen
< NS_INT16SZ
+ NS_INT8SZ
+ NS_INT8SZ
)
475 /* Key flags, Protocol, Algorithm. */
476 keyflags
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
478 algorithm
= *rdata
++;
479 len
= SPRINTF((tmp
, "0x%04x %u %u",
480 (unsigned int)keyflags
, (unsigned int)protocol
, (unsigned int)algorithm
));
481 T(addstr(tmp
, len
, &buf
, &buflen
));
483 /* Public key data. */
484 len
= b64_ntop(rdata
, edata
- rdata
,
485 base64_key
, sizeof base64_key
);
486 if ((ssize_t
)len
< 0)
489 T(addstr(" (", 2, &buf
, &buflen
));
494 for (n
= 0; n
< (int)len
; n
+= 48) {
495 T(addstr(leader
, strlen(leader
), &buf
, &buflen
));
496 T(addstr(base64_key
+ n
, MIN(len
- n
, 48),
500 T(addstr(" )", 2, &buf
, &buflen
));
506 u_int type
, algorithm
, labels
, footprint
;
514 /* Type covered, Algorithm, Label count, Original TTL. */
515 type
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
516 algorithm
= *rdata
++;
518 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
519 len
= SPRINTF((tmp
, " %s %d %lu ",
520 p_type((int)type
), (int)algorithm
, (unsigned long)t
));
521 T(addstr(tmp
, len
, &buf
, &buflen
));
522 if (labels
!= (u_int
)dn_count_labels(name
))
525 /* Signature expiry. */
526 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
527 len
= SPRINTF((tmp
, "%s ", p_secstodate(t
)));
528 T(addstr(tmp
, len
, &buf
, &buflen
));
531 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
532 len
= SPRINTF((tmp
, "%s ", p_secstodate(t
)));
533 T(addstr(tmp
, len
, &buf
, &buflen
));
535 /* Signature Footprint. */
536 footprint
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
537 len
= SPRINTF((tmp
, "%u ", (unsigned int)footprint
));
538 T(addstr(tmp
, len
, &buf
, &buflen
));
541 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
544 len
= b64_ntop(rdata
, edata
- rdata
,
545 base64_key
, sizeof base64_key
);
547 T(addstr(" (", 2, &buf
, &buflen
));
552 if ((ssize_t
)len
< 0)
554 for (n
= 0; n
< (int)len
; n
+= 48) {
555 T(addstr(leader
, strlen(leader
), &buf
, &buflen
));
556 T(addstr(base64_key
+ n
, MIN(len
- n
, 48),
560 T(addstr(" )", 2, &buf
, &buflen
));
568 /* Next domain name. */
569 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
572 n
= (int)(edata
- rdata
);
573 for (c
= 0; c
< n
*8; c
++)
574 if (NS_NXT_BIT_ISSET(c
, rdata
)) {
575 len
= SPRINTF((tmp
, " %s", p_type(c
)));
576 T(addstr(tmp
, len
, &buf
, &buflen
));
582 comment
= "unknown RR type";
585 return ((int)(buf
- obuf
));
587 comment
= "RR format error";
592 len
= SPRINTF((tmp
, "\\#(\t\t; %s", comment
));
593 T(addstr(tmp
, len
, &buf
, &buflen
));
594 while (rdata
< edata
) {
596 p
+= SPRINTF((p
, "\n\t"));
598 n
= MIN(16, (int)(edata
- rdata
));
599 for (m
= 0; m
< n
; m
++)
600 p
+= SPRINTF((p
, "%02x ", rdata
[m
]));
601 T(addstr(tmp
, (u_int
)(p
- tmp
), &buf
, &buflen
));
603 T(addstr(")", 1, &buf
, &buflen
));
604 T(addtab((u_int
)(p
- tmp
) + 1, 48, spaced
, &buf
, &buflen
));
607 p
+= SPRINTF((p
, "; "));
608 for (m
= 0; m
< n
; m
++)
609 *p
++ = (isascii(rdata
[m
]) && isprint(rdata
[m
]))
612 T(addstr(tmp
, (u_int
)(p
- tmp
), &buf
, &buflen
));
615 return ((int)(buf
- obuf
));
623 * prune_origin(name, origin)
624 * Find out if the name is at or under the current origin.
626 * Number of characters in name before start of origin,
627 * or length of name if origin does not match.
629 * This function should share code with samedomain().
632 prune_origin(const char *name
, const char *origin
) {
633 const char *oname
= name
;
635 while (*name
!= '\0') {
636 if (origin
!= NULL
&& strcasecmp(name
, origin
) == 0)
637 return ((size_t)(name
- oname
) - (name
> oname
));
638 while (*name
!= '\0') {
641 /* XXX need to handle \nnn form. */
644 } else if (*name
== '.') {
651 return ((size_t)(name
- oname
));
656 * charstr(rdata, edata, buf, buflen)
657 * Format a <character-string> into the presentation buffer.
659 * Number of rdata octets consumed
660 * 0 for protocol format error
661 * -1 for output buffer error
663 * buffer is advanced on success.
666 charstr(const u_char
*rdata
, const u_char
*edata
, char **buf
, size_t *buflen
) {
667 const u_char
*odata
= rdata
;
668 size_t save_buflen
= *buflen
;
669 char *save_buf
= *buf
;
671 if (addstr("\"", 1, buf
, buflen
) < 0)
676 if (rdata
+ 1 + n
<= edata
) {
679 if (strchr("\n\"\\", *rdata
) != NULL
)
680 if (addstr("\\", 1, buf
, buflen
) < 0)
682 if (addstr((const char *)rdata
, 1,
689 if (addstr("\"", 1, buf
, buflen
) < 0)
691 return ((int)(rdata
- odata
));
695 *buflen
= save_buflen
;
700 addname(const u_char
*msg
, size_t msglen
,
701 const u_char
**pp
, const char *origin
,
702 char **buf
, size_t *buflen
)
704 size_t newlen
, save_buflen
= *buflen
;
705 char *save_buf
= *buf
;
708 n
= dn_expand(msg
, msg
+ msglen
, *pp
, *buf
, (int)(*buflen
));
710 goto enospc
; /* Guess. */
711 newlen
= prune_origin(*buf
, origin
);
712 if ((origin
== NULL
|| origin
[0] == '\0' || (*buf
)[newlen
] == '\0') &&
713 (newlen
== 0 || (*buf
)[newlen
- 1] != '.')) {
714 /* No trailing dot. */
715 if (newlen
+ 2 > *buflen
)
716 goto enospc
; /* No room for ".\0". */
717 (*buf
)[newlen
++] = '.';
718 (*buf
)[newlen
] = '\0';
721 /* Use "@" instead of name. */
722 if (newlen
+ 2 > *buflen
)
723 goto enospc
; /* No room for "@\0". */
724 (*buf
)[newlen
++] = '@';
725 (*buf
)[newlen
] = '\0';
728 addlen(newlen
, buf
, buflen
);
730 return ((int)newlen
);
734 *buflen
= save_buflen
;
739 addlen(size_t len
, char **buf
, size_t *buflen
) {
740 assert(len
<= *buflen
);
746 addstr(const char *src
, size_t len
, char **buf
, size_t *buflen
) {
751 memcpy(*buf
, src
, len
);
752 addlen(len
, buf
, buflen
);
758 addtab(size_t len
, size_t target
, int spaced
, char **buf
, size_t *buflen
) {
759 size_t save_buflen
= *buflen
;
760 char *save_buf
= *buf
;
763 if (spaced
|| len
>= target
- 1) {
764 T(addstr(" ", 2, buf
, buflen
));
767 for (t
= (int)(target
- len
- 1) / 8; t
>= 0; t
--)
768 if (addstr("\t", 1, buf
, buflen
) < 0) {
769 *buflen
= save_buflen
;