]>
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.5 2004/06/11 23:16:00 majka 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/nameser8_compat.h>
91 #include <resolv8_compat.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, n
, c
;
481 struct in_addr inaddr
;
482 const u_char
*cp1
, *cp2
;
486 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
487 h_errno
= NETDB_INTERNAL
;
490 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
491 return (NULL
); /* compression error */
492 type
= _getshort((u_char
*)cp
);
494 class = _getshort((u_char
*)cp
);
496 tmpttl
= _getlong((u_char
*)cp
);
498 dlen
= _getshort((u_char
*)cp
);
501 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_TTLID
))
502 fprintf(file
, "\t%lu", (u_long
)tmpttl
);
503 if ((!_res
.pfcode
) || (_res
.pfcode
& RES_PRF_CLASS
))
504 fprintf(file
, "\t%s", __p_class(class));
505 fprintf(file
, "\t%s", __p_type(type
));
507 * Print type specific data, if appropriate
514 bcopy(cp
, (char *)&inaddr
, INADDRSZ
);
516 fprintf(file
, "\t%s", inet_ntoa(inaddr
));
518 } else if (dlen
== 7) {
523 address
= inet_ntoa(inaddr
);
525 protocol
= *(u_char
*)cp
;
526 cp
+= sizeof(u_char
);
527 port
= _getshort((u_char
*)cp
);
529 fprintf(file
, "\t%s\t; proto %d, port %d",
530 address
, protocol
, port
);
544 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
552 fprintf(file
, "\t%.*s", n
, cp
);
555 if ((cp
< cp2
) && (n
= *cp
++)) {
556 fprintf(file
, "\t%.*s", n
, cp
);
558 } else if (type
== T_HINFO
)
559 fprintf(file
, "\n;; *** Warning *** OS-type missing");
564 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
567 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
570 t
= _getlong((u_char
*)cp
); cp
+= INT32SZ
;
571 fprintf(file
, "\t\t\t%lu\t; serial\n", (u_long
)t
);
572 t
= _getlong((u_char
*)cp
); cp
+= INT32SZ
;
573 fprintf(file
, "\t\t\t%lu\t; refresh (%s)\n",
574 (u_long
)t
, __p_time(t
));
575 t
= _getlong((u_char
*)cp
); cp
+= INT32SZ
;
576 fprintf(file
, "\t\t\t%lu\t; retry (%s)\n",
577 (u_long
)t
, __p_time(t
));
578 t
= _getlong((u_char
*)cp
); cp
+= INT32SZ
;
579 fprintf(file
, "\t\t\t%lu\t; expire (%s)\n",
580 (u_long
)t
, __p_time(t
));
581 t
= _getlong((u_char
*)cp
); cp
+= INT32SZ
;
582 fprintf(file
, "\t\t\t%lu )\t; minimum (%s)",
583 (u_long
)t
, __p_time(t
));
589 fprintf(file
, "\t%d ", _getshort((u_char
*)cp
));
591 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
596 fprintf(file
, "\t%d ", _getshort((u_char
*)cp
));
598 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
601 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
607 (void) fputs("\t\"", file
);
610 if ((n
= (unsigned char) *cp
++)) {
611 for (c
= n
; c
> 0 && cp
< cp2
; c
--)
612 if ((*cp
== '\n') || (*cp
== '"')) {
613 (void) putc('\\', file
);
614 (void) putc(*cp
++, file
);
616 (void) putc(*cp
++, file
);
623 (void) fprintf(file
, "\t%s", inet_nsap_ntoa(dlen
, cp
, NULL
));
630 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
633 if ((cp
= p_fqname(cp
, msg
, file
)) == NULL
)
639 fputs((char *)cp
, file
);
646 fprintf(file
, "\t%u", _getlong((u_char
*)cp
));
652 if (dlen
< INT32SZ
+ 1)
654 bcopy(cp
, (char *)&inaddr
, INADDRSZ
);
656 fprintf(file
, "\t%s %s ( ",
659 cp
+= sizeof(u_char
);
662 while (cp
< cp1
+ dlen
) {
667 fputs("\n\t\t\t", file
);
670 fputs(dewks(n
), file
);
680 #ifdef ALLOW_T_UNSPEC
687 if (dlen
< NumBytes
) NumBytes
= dlen
;
688 fprintf(file
, "\tFirst %d bytes of hex data:",
690 for (i
= 0, DataPtr
= cp
; i
< NumBytes
; i
++, DataPtr
++)
691 fprintf(file
, " %x", *DataPtr
);
695 #endif /* ALLOW_T_UNSPEC */
698 fprintf(file
, "\t?%d?", type
);
702 fprintf(file
, "\t; dlen=%d, ttl %s\n", dlen
, __p_time(tmpttl
));
706 if (cp
- cp1
!= dlen
) {
707 fprintf(file
, ";; packet size error (found %ld, dlen was %ld)\n",
715 * Return a string for the type
721 static char nbuf
[20];
724 case T_A
: return "A";
725 case T_NS
: return "NS";
726 case T_CNAME
: return "CNAME";
727 case T_SOA
: return "SOA";
728 case T_MB
: return "MB";
729 case T_MG
: return "MG";
730 case T_MR
: return "MR";
731 case T_NULL
: return "NULL";
732 case T_WKS
: return "WKS";
733 case T_PTR
: return "PTR";
734 case T_HINFO
: return "HINFO";
735 case T_MINFO
: return "MINFO";
736 case T_MX
: return "MX";
737 case T_TXT
: return "TXT";
738 case T_RP
: return "RP";
739 case T_AFSDB
: return "AFSDB";
740 case T_X25
: return "X25";
741 case T_ISDN
: return "ISDN";
742 case T_RT
: return "RT";
743 case T_NSAP
: return "NSAP";
744 case T_NSAP_PTR
: return "NSAP_PTR";
745 case T_SIG
: return "SIG";
746 case T_KEY
: return "KEY";
747 case T_PX
: return "PX";
748 case T_GPOS
: return "GPOS";
749 case T_AAAA
: return "AAAA";
750 case T_LOC
: return "LOC";
751 case T_AXFR
: return "AXFR";
752 case T_MAILB
: return "MAILB";
753 case T_MAILA
: return "MAILA";
754 case T_ANY
: return "ANY";
755 case T_UINFO
: return "UINFO";
756 case T_UID
: return "UID";
757 case T_GID
: return "GID";
758 #ifdef ALLOW_T_UNSPEC
759 case T_UNSPEC
: return "UNSPEC";
760 #endif /* ALLOW_T_UNSPEC */
761 default: (void)sprintf(nbuf
, "%d", type
); return (nbuf
);
766 * Return a mnemonic for class
772 static char nbuf
[20];
775 case C_IN
: return "IN";
776 case C_HS
: return "HS";
777 case C_ANY
: return "ANY";
778 default: (void)sprintf(nbuf
, "%d", class); return (nbuf
);
783 * Return a mnemonic for an option
789 static char nbuf
[40];
792 case RES_INIT
: return "init";
793 case RES_DEBUG
: return "debug";
794 case RES_AAONLY
: return "aaonly(unimpl)";
795 case RES_USEVC
: return "usevc";
796 case RES_PRIMARY
: return "primry(unimpl)";
797 case RES_IGNTC
: return "igntc";
798 case RES_RECURSE
: return "recurs";
799 case RES_DEFNAMES
: return "defnam";
800 case RES_STAYOPEN
: return "styopn";
801 case RES_DNSRCH
: return "dnsrch";
802 case RES_INSECURE1
: return "insecure1";
803 case RES_INSECURE2
: return "insecure2";
804 default: sprintf(nbuf
, "?0x%lx?", (u_long
)option
);
810 * Return a mnemonic for a time to live
816 static char nbuf
[40];
817 int secs
, mins
, hours
, days
;
821 strcpy(nbuf
, "0 secs");
834 #define PLURALIZE(x) x, (x == 1) ? "" : "s"
837 (void)sprintf(p
, "%d day%s", PLURALIZE(days
));
843 (void)sprintf(p
, "%d hour%s", PLURALIZE(hours
));
849 (void)sprintf(p
, "%d min%s", PLURALIZE(mins
));
852 if (secs
|| ! (days
|| hours
|| mins
)) {
853 if (days
|| hours
|| mins
)
855 (void)sprintf(p
, "%d sec%s", PLURALIZE(secs
));