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
17 * $FreeBSD: src/lib/libc/net/ns_print.c,v 1.2 1999/08/28 00:00:15 peter Exp $
18 * $DragonFly: src/lib/libc/net/ns_print.c,v 1.3 2005/11/13 02:04:47 swildner Exp $
23 #include <sys/types.h>
24 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/nameser.h>
28 #include <arpa/inet.h>
36 #define SPRINTF(x) ((size_t)sprintf x)
40 static size_t prune_origin(const char *name, const char *origin);
41 static int charstr(const u_char *rdata, const u_char *edata,
42 char **buf, size_t *buflen);
43 static int addname(const u_char *msg, size_t msglen,
44 const u_char **p, const char *origin,
45 char **buf, size_t *buflen);
46 static void addlen(size_t len, char **buf, size_t *buflen);
47 static int addstr(const char *src, size_t len,
48 char **buf, size_t *buflen);
49 static int addtab(size_t len, size_t target, int spaced,
50 char **buf, size_t *buflen);
64 * ns_sprintrr(handle, rr, name_ctx, origin, buf, buflen)
65 * Convert an RR to presentation format.
67 * Number of characters written to buf, or -1 (check errno).
70 ns_sprintrr(const ns_msg *handle, const ns_rr *rr,
71 const char *name_ctx, const char *origin,
72 char *buf, size_t buflen)
76 n = ns_sprintrrf(ns_msg_base(*handle), ns_msg_size(*handle),
77 ns_rr_name(*rr), ns_rr_class(*rr), ns_rr_type(*rr),
78 ns_rr_ttl(*rr), ns_rr_rdata(*rr), ns_rr_rdlen(*rr),
79 name_ctx, origin, buf, buflen);
85 * ns_sprintrrf(msg, msglen, name, class, type, ttl, rdata, rdlen,
86 * name_ctx, origin, buf, buflen)
87 * Convert the fields of an RR into presentation format.
89 * Number of characters written to buf, or -1 (check errno).
92 ns_sprintrrf(const u_char *msg, size_t msglen,
93 const char *name, ns_class class, ns_type type,
94 u_long ttl, const u_char *rdata, size_t rdlen,
95 const char *name_ctx, const char *origin,
96 char *buf, size_t buflen)
98 const char *obuf = buf;
99 const u_char *edata = rdata + rdlen;
109 if (name_ctx != NULL && strcasecmp(name_ctx, name) == 0) {
110 T(addstr("\t\t\t", 3, &buf, &buflen));
112 len = prune_origin(name, origin);
114 T(addstr("@\t\t\t", 4, &buf, &buflen));
116 T(addstr(name, len, &buf, &buflen));
117 /* Origin not used and no trailing dot? */
118 if ((!origin || !origin[0] || name[len] == '\0') &&
119 name[len - 1] != '.') {
120 T(addstr(".", 1, &buf, &buflen));
123 T(spaced = addtab(len, 24, spaced, &buf, &buflen));
130 T(x = ns_format_ttl(ttl, buf, buflen));
131 addlen(x, &buf, &buflen);
132 len = SPRINTF((tmp, " %s %s", p_class(class), p_type(type)));
133 T(addstr(tmp, len, &buf, &buflen));
134 T(spaced = addtab(x + len, 16, spaced, &buf, &buflen));
141 if (rdlen != NS_INADDRSZ)
143 inet_ntop(AF_INET, rdata, buf, buflen);
144 addlen(strlen(buf), &buf, &buflen);
153 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
159 T(len = charstr(rdata, edata, &buf, &buflen));
163 T(addstr(" ", 1, &buf, &buflen));
166 T(len = charstr(rdata, edata, &buf, &buflen));
176 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
177 T(addstr(" ", 1, &buf, &buflen));
179 /* Administrator name. */
180 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
181 T(addstr(" (\n", 3, &buf, &buflen));
184 if ((edata - rdata) != 5*NS_INT32SZ)
188 t = ns_get32(rdata); rdata += NS_INT32SZ;
189 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
190 len = SPRINTF((tmp, "%lu", t));
191 T(addstr(tmp, len, &buf, &buflen));
192 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
193 T(addstr("; serial\n", 9, &buf, &buflen));
196 /* Refresh interval. */
197 t = ns_get32(rdata); rdata += NS_INT32SZ;
198 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
199 T(len = ns_format_ttl(t, buf, buflen));
200 addlen(len, &buf, &buflen);
201 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
202 T(addstr("; refresh\n", 10, &buf, &buflen));
205 /* Retry interval. */
206 t = ns_get32(rdata); rdata += NS_INT32SZ;
207 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
208 T(len = ns_format_ttl(t, buf, buflen));
209 addlen(len, &buf, &buflen);
210 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
211 T(addstr("; retry\n", 8, &buf, &buflen));
215 t = ns_get32(rdata); rdata += NS_INT32SZ;
216 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
217 T(len = ns_format_ttl(t, buf, buflen));
218 addlen(len, &buf, &buflen);
219 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
220 T(addstr("; expiry\n", 9, &buf, &buflen));
224 t = ns_get32(rdata); rdata += NS_INT32SZ;
225 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
226 T(len = ns_format_ttl(t, buf, buflen));
227 addlen(len, &buf, &buflen);
228 T(addstr(" )", 2, &buf, &buflen));
229 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
230 T(addstr("; minimum\n", 10, &buf, &buflen));
240 if (rdlen < NS_INT16SZ)
246 len = SPRINTF((tmp, "%u ", t));
247 T(addstr(tmp, len, &buf, &buflen));
250 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
258 if (rdlen < NS_INT16SZ)
264 len = SPRINTF((tmp, "%u ", t));
265 T(addstr(tmp, len, &buf, &buflen));
268 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
269 T(addstr(" ", 1, &buf, &buflen));
272 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
278 T(len = charstr(rdata, edata, &buf, &buflen));
285 while (rdata < edata) {
286 T(len = charstr(rdata, edata, &buf, &buflen));
291 T(addstr(" ", 1, &buf, &buflen));
298 inet_nsap_ntoa(rdlen, rdata, t);
299 T(addstr(t, strlen(t), &buf, &buflen));
304 if (rdlen != NS_IN6ADDRSZ)
306 inet_ntop(AF_INET6, rdata, buf, buflen);
307 addlen(strlen(buf), &buf, &buflen);
313 /* XXX protocol format checking? */
315 T(addstr(t, strlen(t), &buf, &buflen));
320 u_int order, preference;
323 if (rdlen < 2*NS_INT16SZ)
326 /* Order, Precedence. */
327 order = ns_get16(rdata); rdata += NS_INT16SZ;
328 preference = ns_get16(rdata); rdata += NS_INT16SZ;
329 len = SPRINTF((t, "%u %u ", order, preference));
330 T(addstr(t, len, &buf, &buflen));
333 T(len = charstr(rdata, edata, &buf, &buflen));
337 T(addstr(" ", 1, &buf, &buflen));
340 T(len = charstr(rdata, edata, &buf, &buflen));
344 T(addstr(" ", 1, &buf, &buflen));
347 T(len = charstr(rdata, edata, &buf, &buflen));
353 T(addstr(" ", 1, &buf, &buflen));
356 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
361 u_int priority, weight, port;
364 if (rdlen < NS_INT16SZ*3)
367 /* Priority, Weight, Port. */
368 priority = ns_get16(rdata); rdata += NS_INT16SZ;
369 weight = ns_get16(rdata); rdata += NS_INT16SZ;
370 port = ns_get16(rdata); rdata += NS_INT16SZ;
371 len = SPRINTF((t, "%u %u %u ", priority, weight, port));
372 T(addstr(t, len, &buf, &buflen));
375 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
382 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
383 T(addstr(" ", 1, &buf, &buflen));
386 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
393 if (rdlen < NS_INT32SZ + 1)
397 inet_ntop(AF_INET, rdata, buf, buflen);
398 addlen(strlen(buf), &buf, &buflen);
399 rdata += NS_INADDRSZ;
402 len = SPRINTF((tmp, " %u ( ", *rdata));
403 T(addstr(tmp, len, &buf, &buflen));
409 while (rdata < edata) {
414 T(addstr("\n\t\t\t\t", 5,
419 len = SPRINTF((tmp, "%d ", n));
420 T(addstr(tmp, len, &buf, &buflen));
426 T(addstr(")", 1, &buf, &buflen));
432 char base64_key[NS_MD5RSA_MAX_BASE64];
433 u_int keyflags, protocol, algorithm;
437 if (rdlen < NS_INT16SZ + NS_INT8SZ + NS_INT8SZ)
440 /* Key flags, Protocol, Algorithm. */
441 keyflags = ns_get16(rdata); rdata += NS_INT16SZ;
443 algorithm = *rdata++;
444 len = SPRINTF((tmp, "0x%04x %u %u",
445 keyflags, protocol, algorithm));
446 T(addstr(tmp, len, &buf, &buflen));
448 /* Public key data. */
449 len = b64_ntop(rdata, edata - rdata,
450 base64_key, sizeof base64_key);
454 T(addstr(" (", 2, &buf, &buflen));
459 for (n = 0; n < len; n += 48) {
460 T(addstr(leader, strlen(leader), &buf, &buflen));
461 T(addstr(base64_key + n, MIN(len - n, 48),
465 T(addstr(" )", 2, &buf, &buflen));
471 char base64_key[NS_MD5RSA_MAX_BASE64];
472 u_int type, algorithm, labels, footprint;
480 /* Type covered, Algorithm, Label count, Original TTL. */
481 type = ns_get16(rdata); rdata += NS_INT16SZ;
482 algorithm = *rdata++;
484 t = ns_get32(rdata); rdata += NS_INT32SZ;
485 len = SPRINTF((tmp, " %s %d %lu ",
486 p_type(type), algorithm, t));
487 T(addstr(tmp, len, &buf, &buflen));
488 if (labels != (u_int)dn_count_labels(name))
491 /* Signature expiry. */
492 t = ns_get32(rdata); rdata += NS_INT32SZ;
493 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
494 T(addstr(tmp, len, &buf, &buflen));
497 t = ns_get32(rdata); rdata += NS_INT32SZ;
498 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
499 T(addstr(tmp, len, &buf, &buflen));
501 /* Signature Footprint. */
502 footprint = ns_get16(rdata); rdata += NS_INT16SZ;
503 len = SPRINTF((tmp, "%u ", footprint));
504 T(addstr(tmp, len, &buf, &buflen));
507 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
510 len = b64_ntop(rdata, edata - rdata,
511 base64_key, sizeof base64_key);
513 T(addstr(" (", 2, &buf, &buflen));
520 for (n = 0; n < len; n += 48) {
521 T(addstr(leader, strlen(leader), &buf, &buflen));
522 T(addstr(base64_key + n, MIN(len - n, 48),
526 T(addstr(" )", 2, &buf, &buflen));
534 /* Next domain name. */
535 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
539 for (c = 0; c < n*8; c++)
540 if (NS_NXT_BIT_ISSET(c, rdata)) {
541 len = SPRINTF((tmp, " %s", p_type(c)));
542 T(addstr(tmp, len, &buf, &buflen));
548 comment = "unknown RR type";
553 comment = "RR format error";
558 len = SPRINTF((tmp, "\\#(\t\t; %s", comment));
559 T(addstr(tmp, len, &buf, &buflen));
560 while (rdata < edata) {
562 p += SPRINTF((p, "\n\t"));
564 n = MIN(16, edata - rdata);
565 for (m = 0; m < n; m++)
566 p += SPRINTF((p, "%02x ", rdata[m]));
567 T(addstr(tmp, p - tmp, &buf, &buflen));
569 T(addstr(")", 1, &buf, &buflen));
570 T(addtab(p - tmp + 1, 48, spaced, &buf, &buflen));
573 p += SPRINTF((p, "; "));
574 for (m = 0; m < n; m++)
575 *p++ = (isascii(rdata[m]) && isprint(rdata[m]))
578 T(addstr(tmp, p - tmp, &buf, &buflen));
589 * prune_origin(name, origin)
590 * Find out if the name is at or under the current origin.
592 * Number of characters in name before start of origin,
593 * or length of name if origin does not match.
595 * This function should share code with samedomain().
598 prune_origin(const char *name, const char *origin) {
599 const char *oname = name;
601 while (*name != '\0') {
602 if (origin != NULL && strcasecmp(name, origin) == 0)
603 return (name - oname - (name > oname));
604 while (*name != '\0') {
607 /* XXX need to handle \nnn form. */
610 } else if (*name == '.') {
617 return (name - oname);
622 * charstr(rdata, edata, buf, buflen)
623 * Format a <character-string> into the presentation buffer.
625 * Number of rdata octets consumed
626 * 0 for protocol format error
627 * -1 for output buffer error
629 * buffer is advanced on success.
632 charstr(const u_char *rdata, const u_char *edata, char **buf, size_t *buflen) {
633 const u_char *odata = rdata;
634 size_t save_buflen = *buflen;
635 char *save_buf = *buf;
637 if (addstr("\"", 1, buf, buflen) < 0)
642 if (rdata + 1 + n <= edata) {
645 if (strchr("\n\"\\", *rdata) != NULL)
646 if (addstr("\\", 1, buf, buflen) < 0)
648 if (addstr((const char *)rdata, 1,
655 if (addstr("\"", 1, buf, buflen) < 0)
657 return (rdata - odata);
661 *buflen = save_buflen;
666 addname(const u_char *msg, size_t msglen,
667 const u_char **pp, const char *origin,
668 char **buf, size_t *buflen)
670 size_t newlen, save_buflen = *buflen;
671 char *save_buf = *buf;
674 n = dn_expand(msg, msg + msglen, *pp, *buf, *buflen);
676 goto enospc; /* Guess. */
677 newlen = prune_origin(*buf, origin);
678 if ((origin == NULL || origin[0] == '\0' || (*buf)[newlen] == '\0') &&
679 (newlen == 0 || (*buf)[newlen - 1] != '.')) {
680 /* No trailing dot. */
681 if (newlen + 2 > *buflen)
682 goto enospc; /* No room for ".\0". */
683 (*buf)[newlen++] = '.';
684 (*buf)[newlen] = '\0';
687 /* Use "@" instead of name. */
688 if (newlen + 2 > *buflen)
689 goto enospc; /* No room for "@\0". */
690 (*buf)[newlen++] = '@';
691 (*buf)[newlen] = '\0';
694 addlen(newlen, buf, buflen);
700 *buflen = save_buflen;
705 addlen(size_t len, char **buf, size_t *buflen) {
706 assert(len <= *buflen);
712 addstr(const char *src, size_t len, char **buf, size_t *buflen) {
717 memcpy(*buf, src, len);
718 addlen(len, buf, buflen);
724 addtab(size_t len, size_t target, int spaced, char **buf, size_t *buflen) {
725 size_t save_buflen = *buflen;
726 char *save_buf = *buf;
729 if (spaced || len >= target - 1) {
730 T(addstr(" ", 2, buf, buflen));
733 for (t = (target - len - 1) / 8; t >= 0; t--)
734 if (addstr("\t", 1, buf, buflen) < 0) {
735 *buflen = save_buflen;