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 extern const char *_res_opcodes
[];
80 extern const char *_res_resultcodes
[];
82 /* XXX: we should use getservbyport() instead. */
91 case 7: return "echo";
92 case 9: return "discard";
93 case 11: return "systat";
94 case 13: return "daytime";
95 case 15: return "netstat";
96 case 17: return "qotd";
97 case 19: return "chargen";
98 case 20: return "ftp-data";
99 case 21: return "ftp";
100 case 23: return "telnet";
101 case 25: return "smtp";
102 case 37: return "time";
103 case 39: return "rlp";
104 case 42: return "name";
105 case 43: return "whois";
106 case 53: return "domain";
107 case 57: return "apts";
108 case 59: return "apfs";
109 case 67: return "bootps";
110 case 68: return "bootpc";
111 case 69: return "tftp";
112 case 77: return "rje";
113 case 79: return "finger";
114 case 87: return "link";
115 case 95: return "supdup";
116 case 100: return "newacct";
117 case 101: return "hostnames";
118 case 102: return "iso-tsap";
119 case 103: return "x400";
120 case 104: return "x400-snd";
121 case 105: return "csnet-ns";
122 case 109: return "pop-2";
123 case 111: return "sunrpc";
124 case 113: return "auth";
125 case 115: return "sftp";
126 case 117: return "uucp-path";
127 case 119: return "nntp";
128 case 121: return "erpc";
129 case 123: return "ntp";
130 case 133: return "statsrv";
131 case 136: return "profile";
132 case 144: return "NeWS";
133 case 161: return "snmp";
134 case 162: return "snmp-trap";
135 case 170: return "print-srv";
136 default: (void) sprintf(nbuf
, "%d", wks
); return (nbuf
);
140 /* XXX: we should use getprotobynumber() instead. */
145 static char nbuf
[20];
148 case 1: return "icmp";
149 case 2: return "igmp";
150 case 3: return "ggp";
152 case 6: return "tcp";
153 case 7: return "ucl";
154 case 8: return "egp";
155 case 9: return "igp";
156 case 11: return "nvp-II";
157 case 12: return "pup";
158 case 16: return "chaos";
159 case 17: return "udp";
160 default: (void) sprintf(nbuf
, "%d", protonum
); return (nbuf
);
164 static const u_char
*
165 do_rrset(msg
, len
, cp
, cnt
, pflag
, file
, hs
)
167 const u_char
*cp
, *msg
;
175 * Print answer records.
177 sflag
= (_res
.pfcode
& pflag
);
178 if (n
= ntohs(cnt
)) {
179 if ((!_res
.pfcode
) ||
180 ((sflag
) && (_res
.pfcode
& RES_PRF_HEAD1
)))
183 if ((!_res
.pfcode
) || sflag
) {
184 cp
= p_rr(cp
, msg
, file
);
187 cp
+= __dn_skipname(cp
, cp
+ MAXCDNAME
);
191 dlen
= _getshort((u_char
*)cp
);
195 if ((cp
- msg
) > len
)
198 if ((!_res
.pfcode
) ||
199 ((sflag
) && (_res
.pfcode
& RES_PRF_HEAD1
)))
209 __fp_query(msg
, stdout
);
213 /* ultrix 4.0's packaging has some icky packaging. alias for it here.
214 * there is more junk of this kind over in res_comp.c.
225 * Print the current options.
226 * This is intended to be primarily a debugging routine.
229 __fp_resstat(statp
, file
)
230 struct __res_state
*statp
;
233 register u_long mask
;
235 fprintf(file
, ";; res options:");
238 for (mask
= 1; mask
!= 0; mask
<<= 1)
239 if (statp
->options
& mask
)
240 fprintf(file
, " %s", p_option(mask
));
245 * Print the contents of a query.
246 * This is intended to be primarily a debugging routine.
249 __fp_nquery(msg
, len
, file
)
254 register const u_char
*cp
, *endMark
;
255 register const HEADER
*hp
;
258 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1)
261 #define TruncTest(x) if (x >= endMark) goto trunc
262 #define ErrorTest(x) if (x == NULL) goto error
265 * Print header fields.
270 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_HEADX
) || hp
->rcode
) {
271 fprintf(file
, ";; ->>HEADER<<- opcode: %s, status: %s, id: %d",
272 _res_opcodes
[hp
->opcode
],
273 _res_resultcodes
[hp
->rcode
],
277 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_HEADX
))
279 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_HEAD2
)) {
280 fprintf(file
, "; flags:");
282 fprintf(file
, " qr");
284 fprintf(file
, " aa");
286 fprintf(file
, " tc");
288 fprintf(file
, " rd");
290 fprintf(file
, " ra");
292 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_HEAD1
)) {
293 fprintf(file
, "; Ques: %d", ntohs(hp
->qdcount
));
294 fprintf(file
, ", Ans: %d", ntohs(hp
->ancount
));
295 fprintf(file
, ", Auth: %d", ntohs(hp
->nscount
));
296 fprintf(file
, ", Addit: %d", ntohs(hp
->arcount
));
298 if ((!_res
.pfcode
) || (_res
.pfcode
&
299 (RES_PRF_HEADX
| RES_PRF_HEAD2
| RES_PRF_HEAD1
))) {
303 * Print question records.
305 if (n
= ntohs(hp
->qdcount
)) {
306 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_QUES
))
307 fprintf(file
, ";; QUESTIONS:\n");
309 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_QUES
))
310 fprintf(file
, ";;\t");
312 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_QUES
))
313 cp
= p_cdnname(cp
, msg
, len
, file
);
318 if ((n
= dn_expand(msg
, msg
+len
, cp
, name
,
326 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_QUES
))
327 fprintf(file
, ", type = %s",
328 __p_type(_getshort((u_char
*)cp
)));
331 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_QUES
))
332 fprintf(file
, ", class = %s\n",
333 __p_class(_getshort((u_char
*)cp
)));
335 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_QUES
))
340 * Print authoritative answer records
343 cp
= do_rrset(msg
, len
, cp
, hp
->ancount
, RES_PRF_ANS
, file
,
348 * print name server records
351 cp
= do_rrset(msg
, len
, cp
, hp
->nscount
, RES_PRF_AUTH
, file
,
352 ";; AUTHORITY RECORDS:\n");
357 * print additional records
359 cp
= do_rrset(msg
, len
, cp
, hp
->arcount
, RES_PRF_ADD
, file
,
360 ";; ADDITIONAL RECORDS:\n");
364 fprintf(file
, "\n;; ...truncated\n");
367 fprintf(file
, "\n;; ...malformed\n");
371 __fp_query(msg
, file
)
375 fp_nquery(msg
, PACKETSZ
, file
);
379 __p_cdnname(cp
, msg
, len
, file
)
380 const u_char
*cp
, *msg
;
387 if ((n
= dn_expand(msg
, msg
+ len
, cp
, name
, sizeof name
)) < 0)
397 __p_cdname(cp
, msg
, file
)
398 const u_char
*cp
, *msg
;
401 return (p_cdnname(cp
, msg
, PACKETSZ
, file
));
404 /* XXX: the rest of these functions need to become length-limited, too. (vix)
408 __p_fqname(cp
, msg
, file
)
409 const u_char
*cp
, *msg
;
415 if ((n
= dn_expand(msg
, cp
+ MAXCDNAME
, cp
, name
, sizeof name
)) < 0)
417 if (name
[0] == '\0') {
421 if (name
[strlen(name
) - 1] != '.')
428 * Print resource record fields in human readable form.
431 __p_rr(cp
, msg
, file
)
432 const u_char
*cp
, *msg
;
435 int type
, class, dlen
, n
, c
;
436 struct in_addr inaddr
;
437 const u_char
*cp1
, *cp2
;
441 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
442 h_errno
= NETDB_INTERNAL
;
445 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
446 return (NULL
); /* compression error */
447 type
= _getshort((u_char
*)cp
);
449 class = _getshort((u_char
*)cp
);
451 tmpttl
= _getlong((u_char
*)cp
);
453 dlen
= _getshort((u_char
*)cp
);
456 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_TTLID
))
457 fprintf(file
, "\t%lu", (u_long
)tmpttl
);
458 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_CLASS
))
459 fprintf(file
, "\t%s", __p_class(class));
460 fprintf(file
, "\t%s", __p_type(type
));
462 * Print type specific data, if appropriate
469 bcopy(cp
, (char *)&inaddr
, INADDRSZ
);
471 fprintf(file
, "\t%s", inet_ntoa(inaddr
));
473 } else if (dlen
== 7) {
478 address
= inet_ntoa(inaddr
);
480 protocol
= *(u_char
*)cp
;
481 cp
+= sizeof(u_char
);
482 port
= _getshort((u_char
*)cp
);
484 fprintf(file
, "\t%s\t; proto %d, port %d",
485 address
, protocol
, port
);
499 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
507 fprintf(file
, "\t%.*s", n
, cp
);
510 if ((cp
< cp2
) && (n
= *cp
++)) {
511 fprintf(file
, "\t%.*s", n
, cp
);
513 } else if (type
== T_HINFO
)
514 fprintf(file
, "\n;; *** Warning *** OS-type missing");
519 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
522 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
525 t
= _getlong((u_char
*)cp
); cp
+= INT32SZ
;
526 fprintf(file
, "\t\t\t%lu\t; serial\n", (u_long
)t
);
527 t
= _getlong((u_char
*)cp
); cp
+= INT32SZ
;
528 fprintf(file
, "\t\t\t%lu\t; refresh (%s)\n",
529 (u_long
)t
, __p_time(t
));
530 t
= _getlong((u_char
*)cp
); cp
+= INT32SZ
;
531 fprintf(file
, "\t\t\t%lu\t; retry (%s)\n",
532 (u_long
)t
, __p_time(t
));
533 t
= _getlong((u_char
*)cp
); cp
+= INT32SZ
;
534 fprintf(file
, "\t\t\t%lu\t; expire (%s)\n",
535 (u_long
)t
, __p_time(t
));
536 t
= _getlong((u_char
*)cp
); cp
+= INT32SZ
;
537 fprintf(file
, "\t\t\t%lu )\t; minimum (%s)",
538 (u_long
)t
, __p_time(t
));
544 fprintf(file
, "\t%d ", _getshort((u_char
*)cp
));
546 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
551 fprintf(file
, "\t%d ", _getshort((u_char
*)cp
));
553 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
556 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
562 (void) fputs("\t\"", file
);
565 if (n
= (unsigned char) *cp
++) {
566 for (c
= n
; c
> 0 && cp
< cp2
; c
--)
567 if ((*cp
== '\n') || (*cp
== '"')) {
568 (void) putc('\\', file
);
569 (void) putc(*cp
++, file
);
571 (void) putc(*cp
++, file
);
578 (void) fprintf(file
, "\t%s", inet_nsap_ntoa(dlen
, cp
, NULL
));
585 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
588 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
594 fputs((char *)cp
, file
);
601 fprintf(file
, "\t%u", _getlong((u_char
*)cp
));
607 if (dlen
< INT32SZ
+ 1)
609 bcopy(cp
, (char *)&inaddr
, INADDRSZ
);
611 fprintf(file
, "\t%s %s ( ",
614 cp
+= sizeof(u_char
);
617 while (cp
< cp1
+ dlen
) {
622 fputs("\n\t\t\t", file
);
625 fputs(dewks(n
), file
);
635 #ifdef ALLOW_T_UNSPEC
642 if (dlen
< NumBytes
) NumBytes
= dlen
;
643 fprintf(file
, "\tFirst %d bytes of hex data:",
645 for (i
= 0, DataPtr
= cp
; i
< NumBytes
; i
++, DataPtr
++)
646 fprintf(file
, " %x", *DataPtr
);
650 #endif /* ALLOW_T_UNSPEC */
653 fprintf(file
, "\t?%d?", type
);
657 fprintf(file
, "\t; dlen=%d, ttl %s\n", dlen
, __p_time(tmpttl
));
661 if (cp
- cp1
!= dlen
) {
662 fprintf(file
, ";; packet size error (found %d, dlen was %d)\n",
670 * Return a string for the type
676 static char nbuf
[20];
679 case T_A
: return "A";
680 case T_NS
: return "NS";
681 case T_CNAME
: return "CNAME";
682 case T_SOA
: return "SOA";
683 case T_MB
: return "MB";
684 case T_MG
: return "MG";
685 case T_MR
: return "MR";
686 case T_NULL
: return "NULL";
687 case T_WKS
: return "WKS";
688 case T_PTR
: return "PTR";
689 case T_HINFO
: return "HINFO";
690 case T_MINFO
: return "MINFO";
691 case T_MX
: return "MX";
692 case T_TXT
: return "TXT";
693 case T_RP
: return "RP";
694 case T_AFSDB
: return "AFSDB";
695 case T_X25
: return "X25";
696 case T_ISDN
: return "ISDN";
697 case T_RT
: return "RT";
698 case T_NSAP
: return "NSAP";
699 case T_NSAP_PTR
: return "NSAP_PTR";
700 case T_SIG
: return "SIG";
701 case T_KEY
: return "KEY";
702 case T_PX
: return "PX";
703 case T_GPOS
: return "GPOS";
704 case T_AAAA
: return "AAAA";
705 case T_LOC
: return "LOC";
706 case T_AXFR
: return "AXFR";
707 case T_MAILB
: return "MAILB";
708 case T_MAILA
: return "MAILA";
709 case T_ANY
: return "ANY";
710 case T_UINFO
: return "UINFO";
711 case T_UID
: return "UID";
712 case T_GID
: return "GID";
713 #ifdef ALLOW_T_UNSPEC
714 case T_UNSPEC
: return "UNSPEC";
715 #endif /* ALLOW_T_UNSPEC */
716 default: (void)sprintf(nbuf
, "%d", type
); return (nbuf
);
721 * Return a mnemonic for class
727 static char nbuf
[20];
730 case C_IN
: return "IN";
731 case C_HS
: return "HS";
732 case C_ANY
: return "ANY";
733 default: (void)sprintf(nbuf
, "%d", class); return (nbuf
);
738 * Return a mnemonic for an option
744 static char nbuf
[40];
747 case RES_INIT
: return "init";
748 case RES_DEBUG
: return "debug";
749 case RES_AAONLY
: return "aaonly(unimpl)";
750 case RES_USEVC
: return "usevc";
751 case RES_PRIMARY
: return "primry(unimpl)";
752 case RES_IGNTC
: return "igntc";
753 case RES_RECURSE
: return "recurs";
754 case RES_DEFNAMES
: return "defnam";
755 case RES_STAYOPEN
: return "styopn";
756 case RES_DNSRCH
: return "dnsrch";
757 case RES_INSECURE1
: return "insecure1";
758 case RES_INSECURE2
: return "insecure2";
759 default: sprintf(nbuf
, "?0x%lx?", (u_long
)option
);
765 * Return a mnemonic for a time to live
771 static char nbuf
[40];
772 int secs
, mins
, hours
, days
;
776 strcpy(nbuf
, "0 secs");
789 #define PLURALIZE(x) x, (x == 1) ? "" : "s"
792 (void)sprintf(p
, "%d day%s", PLURALIZE(days
));
798 (void)sprintf(p
, "%d hour%s", PLURALIZE(hours
));
804 (void)sprintf(p
, "%d min%s", PLURALIZE(mins
));
807 if (secs
|| ! (days
|| hours
|| mins
)) {
808 if (days
|| hours
|| mins
)
810 (void)sprintf(p
, "%d sec%s", PLURALIZE(secs
));