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
21 #include <sys/types.h>
22 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/nameser.h>
26 #include <arpa/inet.h>
34 #define SPRINTF(x) ((size_t)sprintf x)
38 static size_t prune_origin(const char *name
, const char *origin
);
39 static int charstr(const u_char
*rdata
, const u_char
*edata
,
40 char **buf
, size_t *buflen
);
41 static int addname(const u_char
*msg
, size_t msglen
,
42 const u_char
**p
, const char *origin
,
43 char **buf
, size_t *buflen
);
44 static void addlen(size_t len
, char **buf
, size_t *buflen
);
45 static int addstr(const char *src
, size_t len
,
46 char **buf
, size_t *buflen
);
47 static int addtab(size_t len
, size_t target
, int spaced
,
48 char **buf
, size_t *buflen
);
61 * Convert an RR to presentation format.
64 *\li Number of characters written to buf, or -1 (check errno).
67 ns_sprintrr(const ns_msg
*handle
, const ns_rr
*rr
,
68 const char *name_ctx
, const char *origin
,
69 char *buf
, size_t buflen
)
73 n
= ns_sprintrrf(ns_msg_base(*handle
), ns_msg_size(*handle
),
74 ns_rr_name(*rr
), ns_rr_class(*rr
), ns_rr_type(*rr
),
75 ns_rr_ttl(*rr
), ns_rr_rdata(*rr
), ns_rr_rdlen(*rr
),
76 name_ctx
, origin
, buf
, buflen
);
79 libresolv_hidden_def (ns_sprintrr
)
82 * Convert the fields of an RR into presentation format.
85 *\li Number of characters written to buf, or -1 (check errno).
88 ns_sprintrrf(const u_char
*msg
, size_t msglen
,
89 const char *name
, ns_class
class, ns_type type
,
90 u_long ttl
, const u_char
*rdata
, size_t rdlen
,
91 const char *name_ctx
, const char *origin
,
92 char *buf
, size_t buflen
)
94 const char *obuf
= buf
;
95 const u_char
*edata
= rdata
+ rdlen
;
106 if (name_ctx
!= NULL
&& __libc_ns_samename (name_ctx
, name
) == 1)
107 T(addstr("\t\t\t", 3, &buf
, &buflen
));
109 len
= prune_origin(name
, origin
);
112 } else if (len
== 0) {
113 T(addstr("@\t\t\t", 4, &buf
, &buflen
));
115 T(addstr(name
, len
, &buf
, &buflen
));
116 /* Origin not used or not root, and no trailing dot? */
117 if (((origin
== NULL
|| origin
[0] == '\0') ||
118 (origin
[0] != '.' && origin
[1] != '\0' &&
119 name
[len
] == '\0')) && 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
!= (size_t)NS_INADDRSZ
)
144 (void) inet_ntop(AF_INET
, rdata
, buf
, buflen
);
145 addlen(strlen(buf
), &buf
, &buflen
);
155 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
161 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
165 T(addstr(" ", 1, &buf
, &buflen
));
168 /* Second word, optional in ISDN records. */
169 if (type
== ns_t_isdn
&& rdata
== edata
)
172 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
182 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
183 T(addstr(" ", 1, &buf
, &buflen
));
185 /* Administrator name. */
186 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
187 T(addstr(" (\n", 3, &buf
, &buflen
));
190 if ((edata
- rdata
) != 5*NS_INT32SZ
)
194 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
195 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
196 len
= SPRINTF((tmp
, "%lu", t
));
197 T(addstr(tmp
, len
, &buf
, &buflen
));
198 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
199 T(addstr("; serial\n", 9, &buf
, &buflen
));
202 /* Refresh interval. */
203 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
204 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
205 T(len
= ns_format_ttl(t
, buf
, buflen
));
206 addlen(len
, &buf
, &buflen
);
207 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
208 T(addstr("; refresh\n", 10, &buf
, &buflen
));
211 /* Retry interval. */
212 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
213 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
214 T(len
= ns_format_ttl(t
, buf
, buflen
));
215 addlen(len
, &buf
, &buflen
);
216 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
217 T(addstr("; retry\n", 8, &buf
, &buflen
));
221 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
222 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
223 T(len
= ns_format_ttl(t
, buf
, buflen
));
224 addlen(len
, &buf
, &buflen
);
225 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
226 T(addstr("; expiry\n", 9, &buf
, &buflen
));
230 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
231 T(addstr("\t\t\t\t\t", 5, &buf
, &buflen
));
232 T(len
= ns_format_ttl(t
, buf
, buflen
));
233 addlen(len
, &buf
, &buflen
);
234 T(addstr(" )", 2, &buf
, &buflen
));
235 T(spaced
= addtab(len
, 16, spaced
, &buf
, &buflen
));
236 T(addstr("; minimum\n", 10, &buf
, &buflen
));
246 if (rdlen
< (size_t)NS_INT16SZ
)
252 len
= SPRINTF((tmp
, "%u ", t
));
253 T(addstr(tmp
, len
, &buf
, &buflen
));
256 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
264 if (rdlen
< (size_t)NS_INT16SZ
)
270 len
= SPRINTF((tmp
, "%u ", t
));
271 T(addstr(tmp
, len
, &buf
, &buflen
));
274 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
275 T(addstr(" ", 1, &buf
, &buflen
));
278 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
284 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
291 while (rdata
< edata
) {
292 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
297 T(addstr(" ", 1, &buf
, &buflen
));
304 (void) inet_nsap_ntoa(rdlen
, rdata
, t
);
305 T(addstr(t
, strlen(t
), &buf
, &buflen
));
310 if (rdlen
!= (size_t)NS_IN6ADDRSZ
)
312 (void) inet_ntop(AF_INET6
, rdata
, buf
, buflen
);
313 addlen(strlen(buf
), &buf
, &buflen
);
319 /* XXX protocol format checking? */
320 (void) loc_ntoa(rdata
, t
);
321 T(addstr(t
, strlen(t
), &buf
, &buflen
));
326 u_int order
, preference
;
329 if (rdlen
< 2U*NS_INT16SZ
)
332 /* Order, Precedence. */
333 order
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
334 preference
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
335 len
= SPRINTF((t
, "%u %u ", order
, preference
));
336 T(addstr(t
, len
, &buf
, &buflen
));
339 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
343 T(addstr(" ", 1, &buf
, &buflen
));
346 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
350 T(addstr(" ", 1, &buf
, &buflen
));
353 T(len
= charstr(rdata
, edata
, &buf
, &buflen
));
359 T(addstr(" ", 1, &buf
, &buflen
));
362 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
367 u_int priority
, weight
, port
;
370 if (rdlen
< 3U*NS_INT16SZ
)
373 /* Priority, Weight, Port. */
374 priority
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
375 weight
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
376 port
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
377 len
= SPRINTF((t
, "%u %u %u ", priority
, weight
, port
));
378 T(addstr(t
, len
, &buf
, &buflen
));
381 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
388 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
389 T(addstr(" ", 1, &buf
, &buflen
));
392 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
399 if (rdlen
< 1U + NS_INT32SZ
)
403 (void) inet_ntop(AF_INET
, rdata
, buf
, buflen
);
404 addlen(strlen(buf
), &buf
, &buflen
);
405 rdata
+= NS_INADDRSZ
;
408 len
= SPRINTF((tmp
, " %u ( ", *rdata
));
409 T(addstr(tmp
, len
, &buf
, &buflen
));
415 while (rdata
< edata
) {
420 T(addstr("\n\t\t\t\t", 5,
425 len
= SPRINTF((tmp
, "%d ", n
));
426 T(addstr(tmp
, len
, &buf
, &buflen
));
432 T(addstr(")", 1, &buf
, &buflen
));
438 u_int c_type
, key_tag
, alg
;
441 char base64_cert
[8192], tmp
[40];
444 c_type
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
445 key_tag
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
446 alg
= (u_int
) *rdata
++;
448 len
= SPRINTF((tmp
, "%d %d %d ", c_type
, key_tag
, alg
));
449 T(addstr(tmp
, len
, &buf
, &buflen
));
450 siz
= (edata
-rdata
)*4/3 + 4; /* "+4" accounts for trailing \0 */
451 if (siz
> sizeof(base64_cert
) * 3/4) {
452 const char *str
= "record too long to print";
453 T(addstr(str
, strlen(str
), &buf
, &buflen
));
456 len
= b64_ntop(rdata
, edata
-rdata
, base64_cert
, siz
);
461 T(addstr(" (", 2, &buf
, &buflen
));
468 for (n
= 0; n
< len
; n
+= 48) {
469 T(addstr(leader
, strlen(leader
),
471 T(addstr(base64_cert
+ n
, MIN(len
- n
, 48),
475 T(addstr(" )", 2, &buf
, &buflen
));
481 /* KJD - need to complete this */
483 int mode
, err
, keysize
;
485 /* Algorithm name. */
486 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
487 T(addstr(" ", 1, &buf
, &buflen
));
490 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
491 len
= SPRINTF((tmp
, "%lu ", t
));
492 T(addstr(tmp
, len
, &buf
, &buflen
));
495 t
= ns_get32(rdata
); rdata
+= NS_INT32SZ
;
496 len
= SPRINTF((tmp
, "%lu ", t
));
497 T(addstr(tmp
, len
, &buf
, &buflen
));
499 /* Mode , Error, Key Size. */
500 /* Priority, Weight, Port. */
501 mode
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
502 err
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
503 keysize
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
504 len
= SPRINTF((tmp
, "%u %u %u ", mode
, err
, keysize
));
505 T(addstr(tmp
, len
, &buf
, &buflen
));
507 /* XXX need to dump key, print otherdata length & other data */
512 /* BEW - need to complete this */
515 T(len
= addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
516 T(addstr(" ", 1, &buf
, &buflen
));
517 rdata
+= 8; /*%< time */
518 n
= ns_get16(rdata
); rdata
+= INT16SZ
;
519 rdata
+= n
; /*%< sig */
520 n
= ns_get16(rdata
); rdata
+= INT16SZ
; /*%< original id */
521 sprintf(buf
, "%d", ns_get16(rdata
));
523 addlen(strlen(buf
), &buf
, &buflen
);
532 if (rdlen
== 0U) goto formerr
;
533 len
= SPRINTF((tmp
, "%d ", *rdata
));
534 T(addstr(tmp
, len
, &buf
, &buflen
));
536 if (pbit
> 128) goto formerr
;
537 pbyte
= (pbit
& ~7) / 8;
540 /* address suffix: provided only when prefix len != 128 */
542 if (rdata
+ pbyte
>= edata
) goto formerr
;
543 memset(&a
, 0, sizeof(a
));
544 memcpy(&a
.s6_addr
[pbyte
], rdata
, sizeof(a
) - pbyte
);
545 (void) inet_ntop(AF_INET6
, &a
, buf
, buflen
);
546 addlen(strlen(buf
), &buf
, &buflen
);
547 rdata
+= sizeof(a
) - pbyte
;
550 /* prefix name: provided only when prefix len > 0 */
553 if (rdata
>= edata
) goto formerr
;
554 T(addstr(" ", 1, &buf
, &buflen
));
555 T(addname(msg
, msglen
, &rdata
, origin
, &buf
, &buflen
));
561 len
= SPRINTF((tmp
, "%u bytes", class));
562 T(addstr(tmp
, len
, &buf
, &buflen
));
567 snprintf (errbuf
, sizeof (errbuf
), "unknown RR type %d", type
);
573 comment
= "RR format error";
578 len
= SPRINTF((tmp
, "\\# %u%s\t; %s", (unsigned)(edata
- rdata
),
579 rdlen
!= 0U ? " (" : "", comment
));
580 T(addstr(tmp
, len
, &buf
, &buflen
));
581 while (rdata
< edata
) {
583 p
+= SPRINTF((p
, "\n\t"));
585 n
= MIN(16, edata
- rdata
);
586 for (m
= 0; m
< n
; m
++)
587 p
+= SPRINTF((p
, "%02x ", rdata
[m
]));
588 T(addstr(tmp
, p
- tmp
, &buf
, &buflen
));
590 T(addstr(")", 1, &buf
, &buflen
));
591 T(addtab(p
- tmp
+ 1, 48, spaced
, &buf
, &buflen
));
594 p
+= SPRINTF((p
, "; "));
595 for (m
= 0; m
< n
; m
++)
596 *p
++ = (isascii(rdata
[m
]) && isprint(rdata
[m
]))
599 T(addstr(tmp
, p
- tmp
, &buf
, &buflen
));
605 libresolv_hidden_def (ns_sprintrrf
)
611 * prune_origin(name, origin)
612 * Find out if the name is at or under the current origin.
614 * Number of characters in name before start of origin,
615 * or length of name if origin does not match.
617 * This function should share code with samedomain().
620 prune_origin(const char *name
, const char *origin
) {
621 const char *oname
= name
;
623 while (*name
!= '\0') {
624 if (origin
!= NULL
&& __libc_ns_samename (name
, origin
) == 1)
625 return (name
- oname
- (name
> oname
));
626 while (*name
!= '\0') {
629 /* XXX need to handle \nnn form. */
632 } else if (*name
== '.') {
639 return (name
- oname
);
644 * charstr(rdata, edata, buf, buflen)
645 * Format a <character-string> into the presentation buffer.
647 * Number of rdata octets consumed
648 * 0 for protocol format error
649 * -1 for output buffer error
651 * buffer is advanced on success.
654 charstr(const u_char
*rdata
, const u_char
*edata
, char **buf
, size_t *buflen
) {
655 const u_char
*odata
= rdata
;
656 size_t save_buflen
= *buflen
;
657 char *save_buf
= *buf
;
659 if (addstr("\"", 1, buf
, buflen
) < 0)
664 if (rdata
+ 1 + n
<= edata
) {
667 if (strchr("\n\"\\", *rdata
) != NULL
)
668 if (addstr("\\", 1, buf
, buflen
) < 0)
670 if (addstr((const char *)rdata
, 1,
677 if (addstr("\"", 1, buf
, buflen
) < 0)
679 return (rdata
- odata
);
681 __set_errno (ENOSPC
);
683 *buflen
= save_buflen
;
688 addname(const u_char
*msg
, size_t msglen
,
689 const u_char
**pp
, const char *origin
,
690 char **buf
, size_t *buflen
)
692 size_t newlen
, save_buflen
= *buflen
;
693 char *save_buf
= *buf
;
696 n
= __libc_dn_expand (msg
, msg
+ msglen
, *pp
, *buf
, *buflen
);
698 goto enospc
; /*%< Guess. */
699 newlen
= prune_origin(*buf
, origin
);
702 } else if (newlen
== 0U) {
703 /* Use "@" instead of name. */
704 if (newlen
+ 2 > *buflen
)
705 goto enospc
; /* No room for "@\0". */
706 (*buf
)[newlen
++] = '@';
707 (*buf
)[newlen
] = '\0';
709 if (((origin
== NULL
|| origin
[0] == '\0') ||
710 (origin
[0] != '.' && origin
[1] != '\0' &&
711 (*buf
)[newlen
] == '\0')) && (*buf
)[newlen
- 1] != '.') {
712 /* No trailing dot. */
714 if (newlen
+ 2 > *buflen
)
715 goto enospc
; /* No room for ".\0". */
716 (*buf
)[newlen
++] = '.';
717 (*buf
)[newlen
] = '\0';
721 addlen(newlen
, buf
, buflen
);
725 __set_errno (ENOSPC
);
727 *buflen
= save_buflen
;
732 addlen(size_t len
, char **buf
, size_t *buflen
) {
733 assert(len
<= *buflen
);
739 addstr(const char *src
, size_t len
, char **buf
, size_t *buflen
) {
740 if (len
>= *buflen
) {
741 __set_errno (ENOSPC
);
744 memcpy(*buf
, src
, len
);
745 addlen(len
, buf
, buflen
);
751 addtab(size_t len
, size_t target
, int spaced
, char **buf
, size_t *buflen
) {
752 size_t save_buflen
= *buflen
;
753 char *save_buf
= *buf
;
756 if (spaced
|| len
>= target
- 1) {
757 T(addstr(" ", 2, buf
, buflen
));
760 for (t
= (target
- len
- 1) / 8; t
>= 0; t
--)
761 if (addstr("\t", 1, buf
, buflen
) < 0) {
762 *buflen
= save_buflen
;