2 * ++Copyright++ 1985, 1990, 1993
4 * Copyright (c) 1985, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies, and that
40 * the name of Digital Equipment Corporation not be used in advertising or
41 * publicity pertaining to distribution of the document or software without
42 * specific, written prior permission.
44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char sccsid
[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93";
58 static char rcsid
[] = "$Id$";
59 #endif /* LIBC_SCCS and not lint */
61 #include <sys/param.h>
62 #include <netinet/in.h>
63 #include <arpa/inet.h>
64 #include <arpa/nameser.h>
69 #if defined(BSD) && (BSD >= 199103)
72 # include "../conf/portability.h"
75 #if defined(USE_OPTIONS_H)
76 # include "../conf/options.h"
79 const char *_res_opcodes
[] = {
83 "CQUERYU", /* experimental */
84 "NOTIFY", /* experimental */
98 const char *_res_resultcodes
[] = {
117 /* XXX: we should use getservbyport() instead. */
122 static char nbuf
[20];
125 case 5: return "rje";
126 case 7: return "echo";
127 case 9: return "discard";
128 case 11: return "systat";
129 case 13: return "daytime";
130 case 15: return "netstat";
131 case 17: return "qotd";
132 case 19: return "chargen";
133 case 20: return "ftp-data";
134 case 21: return "ftp";
135 case 23: return "telnet";
136 case 25: return "smtp";
137 case 37: return "time";
138 case 39: return "rlp";
139 case 42: return "name";
140 case 43: return "whois";
141 case 53: return "domain";
142 case 57: return "apts";
143 case 59: return "apfs";
144 case 67: return "bootps";
145 case 68: return "bootpc";
146 case 69: return "tftp";
147 case 77: return "rje";
148 case 79: return "finger";
149 case 87: return "link";
150 case 95: return "supdup";
151 case 100: return "newacct";
152 case 101: return "hostnames";
153 case 102: return "iso-tsap";
154 case 103: return "x400";
155 case 104: return "x400-snd";
156 case 105: return "csnet-ns";
157 case 109: return "pop-2";
158 case 111: return "sunrpc";
159 case 113: return "auth";
160 case 115: return "sftp";
161 case 117: return "uucp-path";
162 case 119: return "nntp";
163 case 121: return "erpc";
164 case 123: return "ntp";
165 case 133: return "statsrv";
166 case 136: return "profile";
167 case 144: return "NeWS";
168 case 161: return "snmp";
169 case 162: return "snmp-trap";
170 case 170: return "print-srv";
171 default: (void) sprintf(nbuf
, "%d", wks
); return (nbuf
);
175 /* XXX: we should use getprotobynumber() instead. */
180 static char nbuf
[20];
183 case 1: return "icmp";
184 case 2: return "igmp";
185 case 3: return "ggp";
187 case 6: return "tcp";
188 case 7: return "ucl";
189 case 8: return "egp";
190 case 9: return "igp";
191 case 11: return "nvp-II";
192 case 12: return "pup";
193 case 16: return "chaos";
194 case 17: return "udp";
195 default: (void) sprintf(nbuf
, "%d", protonum
); return (nbuf
);
199 static const u_char
*
200 do_rrset(msg
, len
, cp
, cnt
, pflag
, file
, hs
)
202 const u_char
*cp
, *msg
;
210 * Print answer records.
212 sflag
= (_res
.pfcode
& pflag
);
213 if (n
= ntohs(cnt
)) {
214 if ((!_res
.pfcode
) ||
215 ((sflag
) && (_res
.pfcode
& RES_PRF_HEAD1
)))
218 if ((!_res
.pfcode
) || sflag
) {
219 cp
= p_rr(cp
, msg
, file
);
222 cp
+= __dn_skipname(cp
, cp
+ MAXCDNAME
);
226 dlen
= _getshort((u_char
*)cp
);
230 if ((cp
- msg
) > len
)
233 if ((!_res
.pfcode
) ||
234 ((sflag
) && (_res
.pfcode
& RES_PRF_HEAD1
)))
244 __fp_query(msg
, stdout
);
248 /* ultrix 4.0's packaging has some icky packaging. alias for it here.
249 * there is more junk of this kind over in res_comp.c.
260 * Print the current options.
261 * This is intended to be primarily a debugging routine.
264 __fp_resstat(statp
, file
)
265 struct __res_state
*statp
;
268 register u_long mask
;
270 fprintf(file
, ";; res options:");
273 for (mask
= 1; mask
!= 0; mask
<<= 1)
274 if (statp
->options
& mask
)
275 fprintf(file
, " %s", p_option(mask
));
280 * Print the contents of a query.
281 * This is intended to be primarily a debugging routine.
284 __fp_nquery(msg
, len
, file
)
289 register const u_char
*cp
, *endMark
;
290 register const HEADER
*hp
;
293 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1)
296 #define TruncTest(x) if (x >= endMark) goto trunc
297 #define ErrorTest(x) if (x == NULL) goto error
300 * Print header fields.
305 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_HEADX
) || hp
->rcode
) {
306 fprintf(file
, ";; ->>HEADER<<- opcode: %s, status: %s, id: %d",
307 _res_opcodes
[hp
->opcode
],
308 _res_resultcodes
[hp
->rcode
],
313 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_HEAD2
)) {
314 fprintf(file
, "; flags:");
316 fprintf(file
, " qr");
318 fprintf(file
, " aa");
320 fprintf(file
, " tc");
322 fprintf(file
, " rd");
324 fprintf(file
, " ra");
326 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_HEAD1
)) {
327 fprintf(file
, "; Ques: %d", ntohs(hp
->qdcount
));
328 fprintf(file
, ", Ans: %d", ntohs(hp
->ancount
));
329 fprintf(file
, ", Auth: %d", ntohs(hp
->nscount
));
330 fprintf(file
, ", Addit: %d", ntohs(hp
->arcount
));
332 if ((!_res
.pfcode
) || (_res
.pfcode
&
333 (RES_PRF_HEADX
| RES_PRF_HEAD2
| RES_PRF_HEAD1
))) {
337 * Print question records.
339 if (n
= ntohs(hp
->qdcount
)) {
340 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_QUES
))
341 fprintf(file
, ";; QUESTIONS:\n");
343 fprintf(file
, ";;\t");
345 cp
= p_cdnname(cp
, msg
, len
, file
);
348 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_QUES
))
349 fprintf(file
, ", type = %s",
350 __p_type(_getshort((u_char
*)cp
)));
353 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_QUES
))
354 fprintf(file
, ", class = %s\n",
355 __p_class(_getshort((u_char
*)cp
)));
361 * Print authoritative answer records
364 cp
= do_rrset(msg
, len
, cp
, hp
->ancount
, RES_PRF_ANS
, file
,
369 * print name server records
372 cp
= do_rrset(msg
, len
, cp
, hp
->nscount
, RES_PRF_AUTH
, file
,
373 ";; AUTHORITY RECORDS:\n");
378 * print additional records
380 cp
= do_rrset(msg
, len
, cp
, hp
->arcount
, RES_PRF_ADD
, file
,
381 ";; ADDITIONAL RECORDS:\n");
385 fprintf(file
, "\n;; ...truncated\n");
388 fprintf(file
, "\n;; ...malformed\n");
392 __fp_query(msg
, file
)
396 fp_nquery(msg
, PACKETSZ
, file
);
400 __p_cdnname(cp
, msg
, len
, file
)
401 const u_char
*cp
, *msg
;
408 if ((n
= dn_expand(msg
, msg
+ len
, cp
, name
, sizeof name
)) < 0)
418 __p_cdname(cp
, msg
, file
)
419 const u_char
*cp
, *msg
;
422 return (p_cdnname(cp
, msg
, PACKETSZ
, file
));
425 /* XXX: the rest of these functions need to become length-limited, too. (vix)
429 __p_fqname(cp
, msg
, file
)
430 const u_char
*cp
, *msg
;
436 if ((n
= dn_expand(msg
, cp
+ MAXCDNAME
, cp
, name
, sizeof name
)) < 0)
438 if (name
[0] == '\0') {
442 if (name
[strlen(name
) - 1] != '.')
449 * Print resource record fields in human readable form.
452 __p_rr(cp
, msg
, file
)
453 const u_char
*cp
, *msg
;
456 int type
, class, dlen
, n
, c
;
457 struct in_addr inaddr
;
458 const u_char
*cp1
, *cp2
;
462 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
463 h_errno
= NETDB_INTERNAL
;
466 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
467 return (NULL
); /* compression error */
468 type
= _getshort((u_char
*)cp
);
470 class = _getshort((u_char
*)cp
);
472 tmpttl
= _getlong((u_char
*)cp
);
474 dlen
= _getshort((u_char
*)cp
);
477 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_TTLID
))
478 fprintf(file
, "\t%lu", (u_long
)tmpttl
);
479 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_CLASS
))
480 fprintf(file
, "\t%s", __p_class(class));
481 fprintf(file
, "\t%s", __p_type(type
));
483 * Print type specific data, if appropriate
490 bcopy(cp
, (char *)&inaddr
, INADDRSZ
);
492 fprintf(file
, "\t%s", inet_ntoa(inaddr
));
494 } else if (dlen
== 7) {
499 address
= inet_ntoa(inaddr
);
501 protocol
= *(u_char
*)cp
;
502 cp
+= sizeof(u_char
);
503 port
= _getshort((u_char
*)cp
);
505 fprintf(file
, "\t%s\t; proto %d, port %d",
506 address
, protocol
, port
);
520 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
528 fprintf(file
, "\t%.*s", n
, cp
);
531 if ((cp
< cp2
) && (n
= *cp
++)) {
532 fprintf(file
, "\t%.*s", n
, cp
);
534 } else if (type
== T_HINFO
)
535 fprintf(file
, "\n;; *** Warning *** OS-type missing");
540 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
543 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
546 t
= _getlong((u_char
*)cp
); cp
+= INT32SZ
;
547 fprintf(file
, "\t\t\t%lu\t; serial\n", (u_long
)t
);
548 t
= _getlong((u_char
*)cp
); cp
+= INT32SZ
;
549 fprintf(file
, "\t\t\t%lu\t; refresh (%s)\n",
550 (u_long
)t
, __p_time(t
));
551 t
= _getlong((u_char
*)cp
); cp
+= INT32SZ
;
552 fprintf(file
, "\t\t\t%lu\t; retry (%s)\n",
553 (u_long
)t
, __p_time(t
));
554 t
= _getlong((u_char
*)cp
); cp
+= INT32SZ
;
555 fprintf(file
, "\t\t\t%lu\t; expire (%s)\n",
556 (u_long
)t
, __p_time(t
));
557 t
= _getlong((u_char
*)cp
); cp
+= INT32SZ
;
558 fprintf(file
, "\t\t\t%lu )\t; minimum (%s)",
559 (u_long
)t
, __p_time(t
));
565 fprintf(file
, "\t%d ", _getshort((u_char
*)cp
));
567 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
572 fprintf(file
, "\t%d ", _getshort((u_char
*)cp
));
574 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
577 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
583 (void) fputs("\t\"", file
);
586 if (n
= (unsigned char) *cp
++) {
587 for (c
= n
; c
> 0 && cp
< cp2
; c
--)
588 if ((*cp
== '\n') || (*cp
== '"')) {
589 (void) putc('\\', file
);
590 (void) putc(*cp
++, file
);
592 (void) putc(*cp
++, file
);
599 (void) fprintf(file
, "\t%s", inet_nsap_ntoa(dlen
, cp
, NULL
));
606 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
609 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
615 fputs((char *)cp
, file
);
622 fprintf(file
, "\t%u", _getlong((u_char
*)cp
));
628 if (dlen
< INT32SZ
+ 1)
630 bcopy(cp
, (char *)&inaddr
, INADDRSZ
);
632 fprintf(file
, "\t%s %s ( ",
635 cp
+= sizeof(u_char
);
638 while (cp
< cp1
+ dlen
) {
643 fputs("\n\t\t\t", file
);
646 fputs(dewks(n
), file
);
656 #ifdef ALLOW_T_UNSPEC
663 if (dlen
< NumBytes
) NumBytes
= dlen
;
664 fprintf(file
, "\tFirst %d bytes of hex data:",
666 for (i
= 0, DataPtr
= cp
; i
< NumBytes
; i
++, DataPtr
++)
667 fprintf(file
, " %x", *DataPtr
);
671 #endif /* ALLOW_T_UNSPEC */
674 fprintf(file
, "\t?%d?", type
);
678 fprintf(file
, "\t; dlen=%d, ttl %s\n", dlen
, __p_time(tmpttl
));
682 if (cp
- cp1
!= dlen
) {
683 fprintf(file
, ";; packet size error (found %d, dlen was %d)\n",
691 * Return a string for the type
697 static char nbuf
[20];
700 case T_A
: return "A";
701 case T_NS
: return "NS";
702 case T_CNAME
: return "CNAME";
703 case T_SOA
: return "SOA";
704 case T_MB
: return "MB";
705 case T_MG
: return "MG";
706 case T_MR
: return "MR";
707 case T_NULL
: return "NULL";
708 case T_WKS
: return "WKS";
709 case T_PTR
: return "PTR";
710 case T_HINFO
: return "HINFO";
711 case T_MINFO
: return "MINFO";
712 case T_MX
: return "MX";
713 case T_TXT
: return "TXT";
714 case T_RP
: return "RP";
715 case T_AFSDB
: return "AFSDB";
716 case T_X25
: return "X25";
717 case T_ISDN
: return "ISDN";
718 case T_RT
: return "RT";
719 case T_NSAP
: return "NSAP";
720 case T_NSAP_PTR
: return "NSAP_PTR";
721 case T_SIG
: return "SIG";
722 case T_KEY
: return "KEY";
723 case T_PX
: return "PX";
724 case T_GPOS
: return "GPOS";
725 case T_AAAA
: return "AAAA";
726 case T_LOC
: return "LOC";
727 case T_AXFR
: return "AXFR";
728 case T_MAILB
: return "MAILB";
729 case T_MAILA
: return "MAILA";
730 case T_ANY
: return "ANY";
731 case T_UINFO
: return "UINFO";
732 case T_UID
: return "UID";
733 case T_GID
: return "GID";
734 #ifdef ALLOW_T_UNSPEC
735 case T_UNSPEC
: return "UNSPEC";
736 #endif /* ALLOW_T_UNSPEC */
737 default: (void)sprintf(nbuf
, "%d", type
); return (nbuf
);
742 * Return a mnemonic for class
748 static char nbuf
[20];
751 case C_IN
: return "IN";
752 case C_HS
: return "HS";
753 case C_ANY
: return "ANY";
754 default: (void)sprintf(nbuf
, "%d", class); return (nbuf
);
759 * Return a mnemonic for an option
765 static char nbuf
[40];
768 case RES_INIT
: return "init";
769 case RES_DEBUG
: return "debug";
770 case RES_AAONLY
: return "aaonly(unimpl)";
771 case RES_USEVC
: return "usevc";
772 case RES_PRIMARY
: return "primry(unimpl)";
773 case RES_IGNTC
: return "igntc";
774 case RES_RECURSE
: return "recurs";
775 case RES_DEFNAMES
: return "defnam";
776 case RES_STAYOPEN
: return "styopn";
777 case RES_DNSRCH
: return "dnsrch";
778 case RES_INSECURE1
: return "insecure1";
779 case RES_INSECURE2
: return "insecure2";
780 default: sprintf(nbuf
, "?0x%lx?", (u_long
)option
);
786 * Return a mnemonic for a time to live
792 static char nbuf
[40];
793 int secs
, mins
, hours
, days
;
797 strcpy(nbuf
, "0 secs");
810 #define PLURALIZE(x) x, (x == 1) ? "" : "s"
813 (void)sprintf(p
, "%d day%s", PLURALIZE(days
));
819 (void)sprintf(p
, "%d hour%s", PLURALIZE(hours
));
825 (void)sprintf(p
, "%d min%s", PLURALIZE(mins
));
828 if (secs
|| ! (days
|| hours
|| mins
)) {
829 if (days
|| hours
|| mins
)
831 (void)sprintf(p
, "%d sec%s", PLURALIZE(secs
));