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 static char rcsid[] = "$FreeBSD: src/lib/libc/net/ns_print.c,v 1.2 1999/08/28 00:00:15 peter Exp $";
24 #include <sys/types.h>
25 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/nameser.h>
29 #include <arpa/inet.h>
37 #define SPRINTF(x) ((size_t)sprintf x)
41 static size_t prune_origin(const char *name, const char *origin);
42 static int charstr(const u_char *rdata, const u_char *edata,
43 char **buf, size_t *buflen);
44 static int addname(const u_char *msg, size_t msglen,
45 const u_char **p, const char *origin,
46 char **buf, size_t *buflen);
47 static void addlen(size_t len, char **buf, size_t *buflen);
48 static int addstr(const char *src, size_t len,
49 char **buf, size_t *buflen);
50 static int addtab(size_t len, size_t target, int spaced,
51 char **buf, size_t *buflen);
65 * ns_sprintrr(handle, rr, name_ctx, origin, buf, buflen)
66 * Convert an RR to presentation format.
68 * Number of characters written to buf, or -1 (check errno).
71 ns_sprintrr(const ns_msg *handle, const ns_rr *rr,
72 const char *name_ctx, const char *origin,
73 char *buf, size_t buflen)
77 n = ns_sprintrrf(ns_msg_base(*handle), ns_msg_size(*handle),
78 ns_rr_name(*rr), ns_rr_class(*rr), ns_rr_type(*rr),
79 ns_rr_ttl(*rr), ns_rr_rdata(*rr), ns_rr_rdlen(*rr),
80 name_ctx, origin, buf, buflen);
86 * ns_sprintrrf(msg, msglen, name, class, type, ttl, rdata, rdlen,
87 * name_ctx, origin, buf, buflen)
88 * Convert the fields of an RR into presentation format.
90 * Number of characters written to buf, or -1 (check errno).
93 ns_sprintrrf(const u_char *msg, size_t msglen,
94 const char *name, ns_class class, ns_type type,
95 u_long ttl, const u_char *rdata, size_t rdlen,
96 const char *name_ctx, const char *origin,
97 char *buf, size_t buflen)
99 const char *obuf = buf;
100 const u_char *edata = rdata + rdlen;
110 if (name_ctx != NULL && strcasecmp(name_ctx, name) == 0) {
111 T(addstr("\t\t\t", 3, &buf, &buflen));
113 len = prune_origin(name, origin);
115 T(addstr("@\t\t\t", 4, &buf, &buflen));
117 T(addstr(name, len, &buf, &buflen));
118 /* Origin not used and no trailing dot? */
119 if ((!origin || !origin[0] || name[len] == '\0') &&
120 name[len - 1] != '.') {
121 T(addstr(".", 1, &buf, &buflen));
124 T(spaced = addtab(len, 24, spaced, &buf, &buflen));
131 T(x = ns_format_ttl(ttl, buf, buflen));
132 addlen(x, &buf, &buflen);
133 len = SPRINTF((tmp, " %s %s", p_class(class), p_type(type)));
134 T(addstr(tmp, len, &buf, &buflen));
135 T(spaced = addtab(x + len, 16, spaced, &buf, &buflen));
142 if (rdlen != NS_INADDRSZ)
144 (void) inet_ntop(AF_INET, rdata, buf, buflen);
145 addlen(strlen(buf), &buf, &buflen);
154 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
160 T(len = charstr(rdata, edata, &buf, &buflen));
164 T(addstr(" ", 1, &buf, &buflen));
167 T(len = charstr(rdata, edata, &buf, &buflen));
177 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
178 T(addstr(" ", 1, &buf, &buflen));
180 /* Administrator name. */
181 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
182 T(addstr(" (\n", 3, &buf, &buflen));
185 if ((edata - rdata) != 5*NS_INT32SZ)
189 t = ns_get32(rdata); rdata += NS_INT32SZ;
190 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
191 len = SPRINTF((tmp, "%lu", t));
192 T(addstr(tmp, len, &buf, &buflen));
193 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
194 T(addstr("; serial\n", 9, &buf, &buflen));
197 /* Refresh interval. */
198 t = ns_get32(rdata); rdata += NS_INT32SZ;
199 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
200 T(len = ns_format_ttl(t, buf, buflen));
201 addlen(len, &buf, &buflen);
202 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
203 T(addstr("; refresh\n", 10, &buf, &buflen));
206 /* Retry interval. */
207 t = ns_get32(rdata); rdata += NS_INT32SZ;
208 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
209 T(len = ns_format_ttl(t, buf, buflen));
210 addlen(len, &buf, &buflen);
211 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
212 T(addstr("; retry\n", 8, &buf, &buflen));
216 t = ns_get32(rdata); rdata += NS_INT32SZ;
217 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
218 T(len = ns_format_ttl(t, buf, buflen));
219 addlen(len, &buf, &buflen);
220 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
221 T(addstr("; expiry\n", 9, &buf, &buflen));
225 t = ns_get32(rdata); rdata += NS_INT32SZ;
226 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
227 T(len = ns_format_ttl(t, buf, buflen));
228 addlen(len, &buf, &buflen);
229 T(addstr(" )", 2, &buf, &buflen));
230 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
231 T(addstr("; minimum\n", 10, &buf, &buflen));
241 if (rdlen < NS_INT16SZ)
247 len = SPRINTF((tmp, "%u ", t));
248 T(addstr(tmp, len, &buf, &buflen));
251 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
259 if (rdlen < NS_INT16SZ)
265 len = SPRINTF((tmp, "%u ", t));
266 T(addstr(tmp, len, &buf, &buflen));
269 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
270 T(addstr(" ", 1, &buf, &buflen));
273 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
279 T(len = charstr(rdata, edata, &buf, &buflen));
286 while (rdata < edata) {
287 T(len = charstr(rdata, edata, &buf, &buflen));
292 T(addstr(" ", 1, &buf, &buflen));
299 (void) inet_nsap_ntoa(rdlen, rdata, t);
300 T(addstr(t, strlen(t), &buf, &buflen));
305 if (rdlen != NS_IN6ADDRSZ)
307 (void) inet_ntop(AF_INET6, rdata, buf, buflen);
308 addlen(strlen(buf), &buf, &buflen);
314 /* XXX protocol format checking? */
315 (void) loc_ntoa(rdata, t);
316 T(addstr(t, strlen(t), &buf, &buflen));
321 u_int order, preference;
324 if (rdlen < 2*NS_INT16SZ)
327 /* Order, Precedence. */
328 order = ns_get16(rdata); rdata += NS_INT16SZ;
329 preference = ns_get16(rdata); rdata += NS_INT16SZ;
330 len = SPRINTF((t, "%u %u ", order, preference));
331 T(addstr(t, len, &buf, &buflen));
334 T(len = charstr(rdata, edata, &buf, &buflen));
338 T(addstr(" ", 1, &buf, &buflen));
341 T(len = charstr(rdata, edata, &buf, &buflen));
345 T(addstr(" ", 1, &buf, &buflen));
348 T(len = charstr(rdata, edata, &buf, &buflen));
354 T(addstr(" ", 1, &buf, &buflen));
357 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
362 u_int priority, weight, port;
365 if (rdlen < NS_INT16SZ*3)
368 /* Priority, Weight, Port. */
369 priority = ns_get16(rdata); rdata += NS_INT16SZ;
370 weight = ns_get16(rdata); rdata += NS_INT16SZ;
371 port = ns_get16(rdata); rdata += NS_INT16SZ;
372 len = SPRINTF((t, "%u %u %u ", priority, weight, port));
373 T(addstr(t, len, &buf, &buflen));
376 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
383 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
384 T(addstr(" ", 1, &buf, &buflen));
387 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
394 if (rdlen < NS_INT32SZ + 1)
398 (void) inet_ntop(AF_INET, rdata, buf, buflen);
399 addlen(strlen(buf), &buf, &buflen);
400 rdata += NS_INADDRSZ;
403 len = SPRINTF((tmp, " %u ( ", *rdata));
404 T(addstr(tmp, len, &buf, &buflen));
410 while (rdata < edata) {
415 T(addstr("\n\t\t\t\t", 5,
420 len = SPRINTF((tmp, "%d ", n));
421 T(addstr(tmp, len, &buf, &buflen));
427 T(addstr(")", 1, &buf, &buflen));
433 char base64_key[NS_MD5RSA_MAX_BASE64];
434 u_int keyflags, protocol, algorithm;
438 if (rdlen < NS_INT16SZ + NS_INT8SZ + NS_INT8SZ)
441 /* Key flags, Protocol, Algorithm. */
442 keyflags = ns_get16(rdata); rdata += NS_INT16SZ;
444 algorithm = *rdata++;
445 len = SPRINTF((tmp, "0x%04x %u %u",
446 keyflags, protocol, algorithm));
447 T(addstr(tmp, len, &buf, &buflen));
449 /* Public key data. */
450 len = b64_ntop(rdata, edata - rdata,
451 base64_key, sizeof base64_key);
455 T(addstr(" (", 2, &buf, &buflen));
460 for (n = 0; n < len; n += 48) {
461 T(addstr(leader, strlen(leader), &buf, &buflen));
462 T(addstr(base64_key + n, MIN(len - n, 48),
466 T(addstr(" )", 2, &buf, &buflen));
472 char base64_key[NS_MD5RSA_MAX_BASE64];
473 u_int type, algorithm, labels, footprint;
481 /* Type covered, Algorithm, Label count, Original TTL. */
482 type = ns_get16(rdata); rdata += NS_INT16SZ;
483 algorithm = *rdata++;
485 t = ns_get32(rdata); rdata += NS_INT32SZ;
486 len = SPRINTF((tmp, " %s %d %lu ",
487 p_type(type), algorithm, t));
488 T(addstr(tmp, len, &buf, &buflen));
489 if (labels != (u_int)dn_count_labels(name))
492 /* Signature expiry. */
493 t = ns_get32(rdata); rdata += NS_INT32SZ;
494 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
495 T(addstr(tmp, len, &buf, &buflen));
498 t = ns_get32(rdata); rdata += NS_INT32SZ;
499 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
500 T(addstr(tmp, len, &buf, &buflen));
502 /* Signature Footprint. */
503 footprint = ns_get16(rdata); rdata += NS_INT16SZ;
504 len = SPRINTF((tmp, "%u ", footprint));
505 T(addstr(tmp, len, &buf, &buflen));
508 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
511 len = b64_ntop(rdata, edata - rdata,
512 base64_key, sizeof base64_key);
514 T(addstr(" (", 2, &buf, &buflen));
521 for (n = 0; n < len; n += 48) {
522 T(addstr(leader, strlen(leader), &buf, &buflen));
523 T(addstr(base64_key + n, MIN(len - n, 48),
527 T(addstr(" )", 2, &buf, &buflen));
535 /* Next domain name. */
536 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
540 for (c = 0; c < n*8; c++)
541 if (NS_NXT_BIT_ISSET(c, rdata)) {
542 len = SPRINTF((tmp, " %s", p_type(c)));
543 T(addstr(tmp, len, &buf, &buflen));
549 comment = "unknown RR type";
554 comment = "RR format error";
559 len = SPRINTF((tmp, "\\#(\t\t; %s", comment));
560 T(addstr(tmp, len, &buf, &buflen));
561 while (rdata < edata) {
563 p += SPRINTF((p, "\n\t"));
565 n = MIN(16, edata - rdata);
566 for (m = 0; m < n; m++)
567 p += SPRINTF((p, "%02x ", rdata[m]));
568 T(addstr(tmp, p - tmp, &buf, &buflen));
570 T(addstr(")", 1, &buf, &buflen));
571 T(addtab(p - tmp + 1, 48, spaced, &buf, &buflen));
574 p += SPRINTF((p, "; "));
575 for (m = 0; m < n; m++)
576 *p++ = (isascii(rdata[m]) && isprint(rdata[m]))
579 T(addstr(tmp, p - tmp, &buf, &buflen));
590 * prune_origin(name, origin)
591 * Find out if the name is at or under the current origin.
593 * Number of characters in name before start of origin,
594 * or length of name if origin does not match.
596 * This function should share code with samedomain().
599 prune_origin(const char *name, const char *origin) {
600 const char *oname = name;
602 while (*name != '\0') {
603 if (origin != NULL && strcasecmp(name, origin) == 0)
604 return (name - oname - (name > oname));
605 while (*name != '\0') {
608 /* XXX need to handle \nnn form. */
611 } else if (*name == '.') {
618 return (name - oname);
623 * charstr(rdata, edata, buf, buflen)
624 * Format a <character-string> into the presentation buffer.
626 * Number of rdata octets consumed
627 * 0 for protocol format error
628 * -1 for output buffer error
630 * buffer is advanced on success.
633 charstr(const u_char *rdata, const u_char *edata, char **buf, size_t *buflen) {
634 const u_char *odata = rdata;
635 size_t save_buflen = *buflen;
636 char *save_buf = *buf;
638 if (addstr("\"", 1, buf, buflen) < 0)
643 if (rdata + 1 + n <= edata) {
646 if (strchr("\n\"\\", *rdata) != NULL)
647 if (addstr("\\", 1, buf, buflen) < 0)
649 if (addstr((const char *)rdata, 1,
656 if (addstr("\"", 1, buf, buflen) < 0)
658 return (rdata - odata);
662 *buflen = save_buflen;
667 addname(const u_char *msg, size_t msglen,
668 const u_char **pp, const char *origin,
669 char **buf, size_t *buflen)
671 size_t newlen, save_buflen = *buflen;
672 char *save_buf = *buf;
675 n = dn_expand(msg, msg + msglen, *pp, *buf, *buflen);
677 goto enospc; /* Guess. */
678 newlen = prune_origin(*buf, origin);
679 if ((origin == NULL || origin[0] == '\0' || (*buf)[newlen] == '\0') &&
680 (newlen == 0 || (*buf)[newlen - 1] != '.')) {
681 /* No trailing dot. */
682 if (newlen + 2 > *buflen)
683 goto enospc; /* No room for ".\0". */
684 (*buf)[newlen++] = '.';
685 (*buf)[newlen] = '\0';
688 /* Use "@" instead of name. */
689 if (newlen + 2 > *buflen)
690 goto enospc; /* No room for "@\0". */
691 (*buf)[newlen++] = '@';
692 (*buf)[newlen] = '\0';
695 addlen(newlen, buf, buflen);
701 *buflen = save_buflen;
706 addlen(size_t len, char **buf, size_t *buflen) {
707 assert(len <= *buflen);
713 addstr(const char *src, size_t len, char **buf, size_t *buflen) {
718 memcpy(*buf, src, len);
719 addlen(len, buf, buflen);
725 addtab(size_t len, size_t target, int spaced, char **buf, size_t *buflen) {
726 size_t save_buflen = *buflen;
727 char *save_buf = *buf;
730 if (spaced || len >= target - 1) {
731 T(addstr(" ", 2, buf, buflen));
734 for (t = (target - len - 1) / 8; t >= 0; t--)
735 if (addstr("\t", 1, buf, buflen) < 0) {
736 *buflen = save_buflen;