]>
git.saurik.com Git - apple/libinfo.git/blob - dns.subproj/res_debug.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * ++Copyright++ 1985, 1990, 1993
27 * Copyright (c) 1985, 1990, 1993
28 * The Regents of the University of California. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
60 * Permission to use, copy, modify, and distribute this software for any
61 * purpose with or without fee is hereby granted, provided that the above
62 * copyright notice and this permission notice appear in all copies, and that
63 * the name of Digital Equipment Corporation not be used in advertising or
64 * publicity pertaining to distribution of the document or software without
65 * specific, written prior permission.
67 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
68 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
69 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
70 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
71 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
72 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
73 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
79 #if defined(LIBC_SCCS) && !defined(lint)
80 static char sccsid
[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93";
81 static char rcsid
[] = "$Id: res_debug.c,v 1.3 2002/02/19 20:36:12 epeyton Exp $";
82 #endif /* LIBC_SCCS and not lint */
84 #include <sys/param.h>
85 #include <netinet/in.h>
86 #include <arpa/inet.h>
87 #include <arpa/nameser.h>
92 #if defined(BSD) && (BSD >= 199103)
95 # include "portability.h"
98 #if defined(USE_OPTIONS_H)
102 const char *_res_opcodes
[] = {
106 "CQUERYU", /* experimental */
107 "NOTIFY", /* experimental */
121 const char *_res_resultcodes
[] = {
140 /* XXX: we should use getservbyport() instead. */
145 static char nbuf
[20];
148 case 5: return "rje";
149 case 7: return "echo";
150 case 9: return "discard";
151 case 11: return "systat";
152 case 13: return "daytime";
153 case 15: return "netstat";
154 case 17: return "qotd";
155 case 19: return "chargen";
156 case 20: return "ftp-data";
157 case 21: return "ftp";
158 case 23: return "telnet";
159 case 25: return "smtp";
160 case 37: return "time";
161 case 39: return "rlp";
162 case 42: return "name";
163 case 43: return "whois";
164 case 53: return "domain";
165 case 57: return "apts";
166 case 59: return "apfs";
167 case 67: return "bootps";
168 case 68: return "bootpc";
169 case 69: return "tftp";
170 case 77: return "rje";
171 case 79: return "finger";
172 case 87: return "link";
173 case 95: return "supdup";
174 case 100: return "newacct";
175 case 101: return "hostnames";
176 case 102: return "iso-tsap";
177 case 103: return "x400";
178 case 104: return "x400-snd";
179 case 105: return "csnet-ns";
180 case 109: return "pop-2";
181 case 111: return "sunrpc";
182 case 113: return "auth";
183 case 115: return "sftp";
184 case 117: return "uucp-path";
185 case 119: return "nntp";
186 case 121: return "erpc";
187 case 123: return "ntp";
188 case 133: return "statsrv";
189 case 136: return "profile";
190 case 144: return "NeWS";
191 case 161: return "snmp";
192 case 162: return "snmp-trap";
193 case 170: return "print-srv";
194 default: (void) sprintf(nbuf
, "%d", wks
); return (nbuf
);
198 /* XXX: we should use getprotobynumber() instead. */
203 static char nbuf
[20];
206 case 1: return "icmp";
207 case 2: return "igmp";
208 case 3: return "ggp";
210 case 6: return "tcp";
211 case 7: return "ucl";
212 case 8: return "egp";
213 case 9: return "igp";
214 case 11: return "nvp-II";
215 case 12: return "pup";
216 case 16: return "chaos";
217 case 17: return "udp";
218 default: (void) sprintf(nbuf
, "%d", protonum
); return (nbuf
);
222 static const u_char
*
223 do_rrset(msg
, len
, cp
, cnt
, pflag
, file
, hs
)
225 const u_char
*cp
, *msg
;
233 * Print answer records.
235 sflag
= (_res
.pfcode
& pflag
);
236 if ((n
= ntohs(cnt
))) {
237 if ((!_res
.pfcode
) ||
238 ((sflag
) && (_res
.pfcode
& RES_PRF_HEAD1
)))
241 if ((!_res
.pfcode
) || sflag
) {
242 cp
= p_rr(cp
, msg
, file
);
245 cp
+= __dn_skipname(cp
, cp
+ MAXCDNAME
);
249 dlen
= _getshort((u_char
*)cp
);
253 if ((cp
- msg
) > len
)
256 if ((!_res
.pfcode
) ||
257 ((sflag
) && (_res
.pfcode
& RES_PRF_HEAD1
)))
267 __fp_query(msg
, stdout
);
271 /* ultrix 4.0's packaging has some icky packaging. alias for it here.
272 * there is more junk of this kind over in res_comp.c.
283 * Print the current options.
284 * This is intended to be primarily a debugging routine.
287 __fp_resstat(statp
, file
)
288 struct __res_state
*statp
;
291 register u_long mask
;
293 fprintf(file
, ";; res options:");
296 for (mask
= 1; mask
!= 0; mask
<<= 1)
297 if (statp
->options
& mask
)
298 fprintf(file
, " %s", p_option(mask
));
303 * Print the contents of a query.
304 * This is intended to be primarily a debugging routine.
307 __fp_nquery(msg
, len
, file
)
312 register const u_char
*cp
, *endMark
;
313 register const HEADER
*hp
;
316 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1)
319 #define TruncTest(x) if (x >= endMark) goto trunc
320 #define ErrorTest(x) if (x == NULL) goto error
323 * Print header fields.
328 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_HEADX
) || hp
->rcode
) {
329 fprintf(file
, ";; ->>HEADER<<- opcode: %s, status: %s, id: %d",
330 _res_opcodes
[hp
->opcode
],
331 _res_resultcodes
[hp
->rcode
],
336 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_HEAD2
)) {
337 fprintf(file
, "; flags:");
339 fprintf(file
, " qr");
341 fprintf(file
, " aa");
343 fprintf(file
, " tc");
345 fprintf(file
, " rd");
347 fprintf(file
, " ra");
349 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_HEAD1
)) {
350 fprintf(file
, "; Ques: %d", ntohs(hp
->qdcount
));
351 fprintf(file
, ", Ans: %d", ntohs(hp
->ancount
));
352 fprintf(file
, ", Auth: %d", ntohs(hp
->nscount
));
353 fprintf(file
, ", Addit: %d", ntohs(hp
->arcount
));
355 if ((!_res
.pfcode
) || (_res
.pfcode
&
356 (RES_PRF_HEADX
| RES_PRF_HEAD2
| RES_PRF_HEAD1
))) {
360 * Print question records.
362 if ((n
= ntohs(hp
->qdcount
))) {
363 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_QUES
))
364 fprintf(file
, ";; QUESTIONS:\n");
366 fprintf(file
, ";;\t");
368 cp
= p_cdnname(cp
, msg
, len
, file
);
371 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_QUES
))
372 fprintf(file
, ", type = %s",
373 __p_type(_getshort((u_char
*)cp
)));
376 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_QUES
))
377 fprintf(file
, ", class = %s\n",
378 __p_class(_getshort((u_char
*)cp
)));
384 * Print authoritative answer records
387 cp
= do_rrset(msg
, len
, cp
, hp
->ancount
, RES_PRF_ANS
, file
,
392 * print name server records
395 cp
= do_rrset(msg
, len
, cp
, hp
->nscount
, RES_PRF_AUTH
, file
,
396 ";; AUTHORITY RECORDS:\n");
401 * print additional records
403 cp
= do_rrset(msg
, len
, cp
, hp
->arcount
, RES_PRF_ADD
, file
,
404 ";; ADDITIONAL RECORDS:\n");
408 fprintf(file
, "\n;; ...truncated\n");
411 fprintf(file
, "\n;; ...malformed\n");
415 __fp_query(msg
, file
)
419 fp_nquery(msg
, PACKETSZ
, file
);
423 __p_cdnname(cp
, msg
, len
, file
)
424 const u_char
*cp
, *msg
;
431 if ((n
= dn_expand(msg
, msg
+ len
, cp
, name
, sizeof name
)) < 0)
441 __p_cdname(cp
, msg
, file
)
442 const u_char
*cp
, *msg
;
445 return (p_cdnname(cp
, msg
, PACKETSZ
, file
));
448 /* XXX: the rest of these functions need to become length-limited, too. (vix)
452 __p_fqname(cp
, msg
, file
)
453 const u_char
*cp
, *msg
;
459 if ((n
= dn_expand(msg
, cp
+ MAXCDNAME
, cp
, name
, sizeof name
)) < 0)
461 if (name
[0] == '\0') {
465 if (name
[strlen(name
) - 1] != '.')
472 * Print resource record fields in human readable form.
475 __p_rr(cp
, msg
, file
)
476 const u_char
*cp
, *msg
;
479 int type
, class, dlen
, n
, c
;
480 struct in_addr inaddr
;
481 const u_char
*cp1
, *cp2
;
485 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
486 h_errno
= NETDB_INTERNAL
;
489 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
490 return (NULL
); /* compression error */
491 type
= _getshort((u_char
*)cp
);
493 class = _getshort((u_char
*)cp
);
495 tmpttl
= _getlong((u_char
*)cp
);
497 dlen
= _getshort((u_char
*)cp
);
500 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_TTLID
))
501 fprintf(file
, "\t%lu", (u_long
)tmpttl
);
502 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_CLASS
))
503 fprintf(file
, "\t%s", __p_class(class));
504 fprintf(file
, "\t%s", __p_type(type
));
506 * Print type specific data, if appropriate
513 bcopy(cp
, (char *)&inaddr
, INADDRSZ
);
515 fprintf(file
, "\t%s", inet_ntoa(inaddr
));
517 } else if (dlen
== 7) {
522 address
= inet_ntoa(inaddr
);
524 protocol
= *(u_char
*)cp
;
525 cp
+= sizeof(u_char
);
526 port
= _getshort((u_char
*)cp
);
528 fprintf(file
, "\t%s\t; proto %d, port %d",
529 address
, protocol
, port
);
543 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
551 fprintf(file
, "\t%.*s", n
, cp
);
554 if ((cp
< cp2
) && (n
= *cp
++)) {
555 fprintf(file
, "\t%.*s", n
, cp
);
557 } else if (type
== T_HINFO
)
558 fprintf(file
, "\n;; *** Warning *** OS-type missing");
563 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
566 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
569 t
= _getlong((u_char
*)cp
); cp
+= INT32SZ
;
570 fprintf(file
, "\t\t\t%lu\t; serial\n", (u_long
)t
);
571 t
= _getlong((u_char
*)cp
); cp
+= INT32SZ
;
572 fprintf(file
, "\t\t\t%lu\t; refresh (%s)\n",
573 (u_long
)t
, __p_time(t
));
574 t
= _getlong((u_char
*)cp
); cp
+= INT32SZ
;
575 fprintf(file
, "\t\t\t%lu\t; retry (%s)\n",
576 (u_long
)t
, __p_time(t
));
577 t
= _getlong((u_char
*)cp
); cp
+= INT32SZ
;
578 fprintf(file
, "\t\t\t%lu\t; expire (%s)\n",
579 (u_long
)t
, __p_time(t
));
580 t
= _getlong((u_char
*)cp
); cp
+= INT32SZ
;
581 fprintf(file
, "\t\t\t%lu )\t; minimum (%s)",
582 (u_long
)t
, __p_time(t
));
588 fprintf(file
, "\t%d ", _getshort((u_char
*)cp
));
590 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
595 fprintf(file
, "\t%d ", _getshort((u_char
*)cp
));
597 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
600 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
606 (void) fputs("\t\"", file
);
609 if ((n
= (unsigned char) *cp
++)) {
610 for (c
= n
; c
> 0 && cp
< cp2
; c
--)
611 if ((*cp
== '\n') || (*cp
== '"')) {
612 (void) putc('\\', file
);
613 (void) putc(*cp
++, file
);
615 (void) putc(*cp
++, file
);
622 (void) fprintf(file
, "\t%s", inet_nsap_ntoa(dlen
, cp
, NULL
));
629 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
632 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
638 fputs((char *)cp
, file
);
645 fprintf(file
, "\t%u", _getlong((u_char
*)cp
));
651 if (dlen
< INT32SZ
+ 1)
653 bcopy(cp
, (char *)&inaddr
, INADDRSZ
);
655 fprintf(file
, "\t%s %s ( ",
658 cp
+= sizeof(u_char
);
661 while (cp
< cp1
+ dlen
) {
666 fputs("\n\t\t\t", file
);
669 fputs(dewks(n
), file
);
679 #ifdef ALLOW_T_UNSPEC
686 if (dlen
< NumBytes
) NumBytes
= dlen
;
687 fprintf(file
, "\tFirst %d bytes of hex data:",
689 for (i
= 0, DataPtr
= cp
; i
< NumBytes
; i
++, DataPtr
++)
690 fprintf(file
, " %x", *DataPtr
);
694 #endif /* ALLOW_T_UNSPEC */
697 fprintf(file
, "\t?%d?", type
);
701 fprintf(file
, "\t; dlen=%d, ttl %s\n", dlen
, __p_time(tmpttl
));
705 if (cp
- cp1
!= dlen
) {
706 fprintf(file
, ";; packet size error (found %d, dlen was %d)\n",
714 * Return a string for the type
720 static char nbuf
[20];
723 case T_A
: return "A";
724 case T_NS
: return "NS";
725 case T_CNAME
: return "CNAME";
726 case T_SOA
: return "SOA";
727 case T_MB
: return "MB";
728 case T_MG
: return "MG";
729 case T_MR
: return "MR";
730 case T_NULL
: return "NULL";
731 case T_WKS
: return "WKS";
732 case T_PTR
: return "PTR";
733 case T_HINFO
: return "HINFO";
734 case T_MINFO
: return "MINFO";
735 case T_MX
: return "MX";
736 case T_TXT
: return "TXT";
737 case T_RP
: return "RP";
738 case T_AFSDB
: return "AFSDB";
739 case T_X25
: return "X25";
740 case T_ISDN
: return "ISDN";
741 case T_RT
: return "RT";
742 case T_NSAP
: return "NSAP";
743 case T_NSAP_PTR
: return "NSAP_PTR";
744 case T_SIG
: return "SIG";
745 case T_KEY
: return "KEY";
746 case T_PX
: return "PX";
747 case T_GPOS
: return "GPOS";
748 case T_AAAA
: return "AAAA";
749 case T_LOC
: return "LOC";
750 case T_AXFR
: return "AXFR";
751 case T_MAILB
: return "MAILB";
752 case T_MAILA
: return "MAILA";
753 case T_ANY
: return "ANY";
754 case T_UINFO
: return "UINFO";
755 case T_UID
: return "UID";
756 case T_GID
: return "GID";
757 #ifdef ALLOW_T_UNSPEC
758 case T_UNSPEC
: return "UNSPEC";
759 #endif /* ALLOW_T_UNSPEC */
760 default: (void)sprintf(nbuf
, "%d", type
); return (nbuf
);
765 * Return a mnemonic for class
771 static char nbuf
[20];
774 case C_IN
: return "IN";
775 case C_HS
: return "HS";
776 case C_ANY
: return "ANY";
777 default: (void)sprintf(nbuf
, "%d", class); return (nbuf
);
782 * Return a mnemonic for an option
788 static char nbuf
[40];
791 case RES_INIT
: return "init";
792 case RES_DEBUG
: return "debug";
793 case RES_AAONLY
: return "aaonly(unimpl)";
794 case RES_USEVC
: return "usevc";
795 case RES_PRIMARY
: return "primry(unimpl)";
796 case RES_IGNTC
: return "igntc";
797 case RES_RECURSE
: return "recurs";
798 case RES_DEFNAMES
: return "defnam";
799 case RES_STAYOPEN
: return "styopn";
800 case RES_DNSRCH
: return "dnsrch";
801 case RES_INSECURE1
: return "insecure1";
802 case RES_INSECURE2
: return "insecure2";
803 default: sprintf(nbuf
, "?0x%lx?", (u_long
)option
);
809 * Return a mnemonic for a time to live
815 static char nbuf
[40];
816 int secs
, mins
, hours
, days
;
820 strcpy(nbuf
, "0 secs");
833 #define PLURALIZE(x) x, (x == 1) ? "" : "s"
836 (void)sprintf(p
, "%d day%s", PLURALIZE(days
));
842 (void)sprintf(p
, "%d hour%s", PLURALIZE(hours
));
848 (void)sprintf(p
, "%d min%s", PLURALIZE(mins
));
851 if (secs
|| ! (days
|| hours
|| mins
)) {
852 if (days
|| hours
|| mins
)
854 (void)sprintf(p
, "%d sec%s", PLURALIZE(secs
));