update from main archive 961001
[glibc.git] / resolv / res_debug.c
blobcc6f84fea1fe87a570aa96944979f27259e569d9
1 /*
2 * ++Copyright++ 1985, 1990, 1993
3 * -
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
9 * are met:
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
33 * SUCH DAMAGE.
34 * -
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
51 * SOFTWARE.
52 * -
53 * Portions Copyright (c) 1995 by International Business Machines, Inc.
55 * International Business Machines, Inc. (hereinafter called IBM) grants
56 * permission under its copyrights to use, copy, modify, and distribute this
57 * Software with or without fee, provided that the above copyright notice and
58 * all paragraphs of this notice appear in all copies, and that the name of IBM
59 * not be used in connection with the marketing of any product incorporating
60 * the Software or modifications thereof, without specific, written prior
61 * permission.
63 * To the extent it has a right to do so, IBM grants an immunity from suit
64 * under its patents, if any, for the use, sale or manufacture of products to
65 * the extent that such products are used for performing Domain Name System
66 * dynamic updates in TCP/IP networks by means of the Software. No immunity is
67 * granted for any product per se or for any other function of any product.
69 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
70 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
71 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
72 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
73 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
74 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
75 * --Copyright--
78 #if defined(LIBC_SCCS) && !defined(lint)
79 static char sccsid[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93";
80 static char rcsid[] = "$Id$";
81 #endif /* LIBC_SCCS and not lint */
83 #include <sys/param.h>
84 #include <sys/types.h>
85 #include <sys/socket.h>
86 #include <netinet/in.h>
87 #include <arpa/inet.h>
88 #include <arpa/nameser.h>
90 #include <ctype.h>
91 #include <netdb.h>
92 #include <resolv.h>
93 #include <stdio.h>
94 #include <time.h>
96 #if defined(BSD) && (BSD >= 199103) && defined(AF_INET6)
97 # include <stdlib.h>
98 # include <string.h>
99 #else
100 # include "../conf/portability.h"
101 #endif
103 #if defined(USE_OPTIONS_H)
104 # include "../conf/options.h"
105 #endif
107 extern const char *_res_opcodes[];
108 extern const char *_res_resultcodes[];
110 /* XXX: we should use getservbyport() instead. */
111 static const char *
112 dewks(wks)
113 int wks;
115 static char nbuf[20];
117 switch (wks) {
118 case 5: return "rje";
119 case 7: return "echo";
120 case 9: return "discard";
121 case 11: return "systat";
122 case 13: return "daytime";
123 case 15: return "netstat";
124 case 17: return "qotd";
125 case 19: return "chargen";
126 case 20: return "ftp-data";
127 case 21: return "ftp";
128 case 23: return "telnet";
129 case 25: return "smtp";
130 case 37: return "time";
131 case 39: return "rlp";
132 case 42: return "name";
133 case 43: return "whois";
134 case 53: return "domain";
135 case 57: return "apts";
136 case 59: return "apfs";
137 case 67: return "bootps";
138 case 68: return "bootpc";
139 case 69: return "tftp";
140 case 77: return "rje";
141 case 79: return "finger";
142 case 87: return "link";
143 case 95: return "supdup";
144 case 100: return "newacct";
145 case 101: return "hostnames";
146 case 102: return "iso-tsap";
147 case 103: return "x400";
148 case 104: return "x400-snd";
149 case 105: return "csnet-ns";
150 case 109: return "pop-2";
151 case 111: return "sunrpc";
152 case 113: return "auth";
153 case 115: return "sftp";
154 case 117: return "uucp-path";
155 case 119: return "nntp";
156 case 121: return "erpc";
157 case 123: return "ntp";
158 case 133: return "statsrv";
159 case 136: return "profile";
160 case 144: return "NeWS";
161 case 161: return "snmp";
162 case 162: return "snmp-trap";
163 case 170: return "print-srv";
164 default: (void) sprintf(nbuf, "%d", wks); return (nbuf);
168 /* XXX: we should use getprotobynumber() instead. */
169 static const char *
170 deproto(protonum)
171 int protonum;
173 static char nbuf[20];
175 switch (protonum) {
176 case 1: return "icmp";
177 case 2: return "igmp";
178 case 3: return "ggp";
179 case 5: return "st";
180 case 6: return "tcp";
181 case 7: return "ucl";
182 case 8: return "egp";
183 case 9: return "igp";
184 case 11: return "nvp-II";
185 case 12: return "pup";
186 case 16: return "chaos";
187 case 17: return "udp";
188 default: (void) sprintf(nbuf, "%d", protonum); return (nbuf);
192 static const u_char *
193 do_rrset(msg, len, cp, cnt, pflag, file, hs)
194 int cnt, pflag, len;
195 const u_char *cp, *msg;
196 const char *hs;
197 FILE *file;
199 int n;
200 int sflag;
203 * Print answer records.
205 sflag = (_res.pfcode & pflag);
206 if (n = ntohs(cnt)) {
207 if ((!_res.pfcode) ||
208 ((sflag) && (_res.pfcode & RES_PRF_HEAD1)))
209 fprintf(file, hs);
210 while (--n >= 0) {
211 if ((!_res.pfcode) || sflag) {
212 cp = p_rr(cp, msg, file);
213 } else {
214 unsigned int dlen;
215 cp += __dn_skipname(cp, cp + MAXCDNAME);
216 cp += INT16SZ;
217 cp += INT16SZ;
218 cp += INT32SZ;
219 dlen = _getshort((u_char*)cp);
220 cp += INT16SZ;
221 cp += dlen;
223 if ((cp - msg) > len)
224 return (NULL);
226 if ((!_res.pfcode) ||
227 ((sflag) && (_res.pfcode & RES_PRF_HEAD1)))
228 putc('\n', file);
230 return (cp);
233 void
234 __p_query(msg)
235 const u_char *msg;
237 __fp_query(msg, stdout);
240 #ifdef ultrix
241 #undef p_query
242 /* ultrix 4.0's packaging has some icky packaging. alias for it here.
243 * there is more junk of this kind over in res_comp.c.
245 void
246 p_query(msg)
247 const u_char *msg;
249 __p_query(msg);
251 #endif
254 * Print the current options.
255 * This is intended to be primarily a debugging routine.
257 void
258 __fp_resstat(statp, file)
259 struct __res_state *statp;
260 FILE *file;
262 register u_long mask;
264 fprintf(file, ";; res options:");
265 if (!statp)
266 statp = &_res;
267 for (mask = 1; mask != 0; mask <<= 1)
268 if (statp->options & mask)
269 fprintf(file, " %s", p_option(mask));
270 putc('\n', file);
274 * Print the contents of a query.
275 * This is intended to be primarily a debugging routine.
277 void
278 __fp_nquery(msg, len, file)
279 const u_char *msg;
280 int len;
281 FILE *file;
283 register const u_char *cp, *endMark;
284 register const HEADER *hp;
285 register int n;
287 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
288 return;
290 #define TruncTest(x) if (x >= endMark) goto trunc
291 #define ErrorTest(x) if (x == NULL) goto error
294 * Print header fields.
296 hp = (HEADER *)msg;
297 cp = msg + HFIXEDSZ;
298 endMark = cp + len;
299 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEADX) || hp->rcode) {
300 fprintf(file, ";; ->>HEADER<<- opcode: %s, status: %s, id: %d",
301 _res_opcodes[hp->opcode],
302 _res_resultcodes[hp->rcode],
303 ntohs(hp->id));
304 putc('\n', file);
306 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEADX))
307 putc(';', file);
308 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEAD2)) {
309 fprintf(file, "; flags:");
310 if (hp->qr)
311 fprintf(file, " qr");
312 if (hp->aa)
313 fprintf(file, " aa");
314 if (hp->tc)
315 fprintf(file, " tc");
316 if (hp->rd)
317 fprintf(file, " rd");
318 if (hp->ra)
319 fprintf(file, " ra");
320 if (hp->unused)
321 fprintf(file, " UNUSED-BIT-ON");
322 if (hp->ad)
323 fprintf(file, " ad");
324 if (hp->cd)
325 fprintf(file, " cd");
327 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEAD1)) {
328 fprintf(file, "; Ques: %d", ntohs(hp->qdcount));
329 fprintf(file, ", Ans: %d", ntohs(hp->ancount));
330 fprintf(file, ", Auth: %d", ntohs(hp->nscount));
331 fprintf(file, ", Addit: %d", ntohs(hp->arcount));
333 if ((!_res.pfcode) || (_res.pfcode &
334 (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
335 putc('\n',file);
338 * Print question records.
340 if (n = ntohs(hp->qdcount)) {
341 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
342 fprintf(file, ";; QUESTIONS:\n");
343 while (--n >= 0) {
344 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
345 fprintf(file, ";;\t");
346 TruncTest(cp);
347 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
348 cp = p_cdnname(cp, msg, len, file);
349 else {
350 int n;
351 char name[MAXDNAME];
353 if ((n = dn_expand(msg, msg+len, cp, name,
354 sizeof name)) < 0)
355 cp = NULL;
356 else
357 cp += n;
359 ErrorTest(cp);
360 TruncTest(cp);
361 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
362 fprintf(file, ", type = %s",
363 __p_type(_getshort((u_char*)cp)));
364 cp += INT16SZ;
365 TruncTest(cp);
366 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
367 fprintf(file, ", class = %s\n",
368 __p_class(_getshort((u_char*)cp)));
369 cp += INT16SZ;
370 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
371 putc('\n', file);
375 * Print authoritative answer records
377 TruncTest(cp);
378 cp = do_rrset(msg, len, cp, hp->ancount, RES_PRF_ANS, file,
379 ";; ANSWERS:\n");
380 ErrorTest(cp);
383 * print name server records
385 TruncTest(cp);
386 cp = do_rrset(msg, len, cp, hp->nscount, RES_PRF_AUTH, file,
387 ";; AUTHORITY RECORDS:\n");
388 ErrorTest(cp);
390 TruncTest(cp);
392 * print additional records
394 cp = do_rrset(msg, len, cp, hp->arcount, RES_PRF_ADD, file,
395 ";; ADDITIONAL RECORDS:\n");
396 ErrorTest(cp);
397 return;
398 trunc:
399 fprintf(file, "\n;; ...truncated\n");
400 return;
401 error:
402 fprintf(file, "\n;; ...malformed\n");
405 void
406 __fp_query(msg, file)
407 const u_char *msg;
408 FILE *file;
410 fp_nquery(msg, PACKETSZ, file);
413 const u_char *
414 __p_cdnname(cp, msg, len, file)
415 const u_char *cp, *msg;
416 int len;
417 FILE *file;
419 char name[MAXDNAME];
420 int n;
422 if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0)
423 return (NULL);
424 if (name[0] == '\0')
425 putc('.', file);
426 else
427 fputs(name, file);
428 return (cp + n);
431 const u_char *
432 __p_cdname(cp, msg, file)
433 const u_char *cp, *msg;
434 FILE *file;
436 return (p_cdnname(cp, msg, PACKETSZ, file));
440 /* Return a fully-qualified domain name from a compressed name (with
441 length supplied). */
443 const u_char *
444 __p_fqnname(cp, msg, msglen, name, namelen)
445 const u_char *cp, *msg;
446 int msglen;
447 char *name;
448 int namelen;
450 int n, newlen;
452 if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)
453 return (NULL);
454 newlen = strlen (name);
455 if (newlen == 0 || name[newlen - 1] != '.')
456 if (newlen+1 >= namelen) /* Lack space for final dot */
457 return (NULL);
458 else
459 strcpy(name + newlen, ".");
460 return (cp + n);
463 /* XXX: the rest of these functions need to become length-limited, too. (vix)
466 const u_char *
467 __p_fqname(cp, msg, file)
468 const u_char *cp, *msg;
469 FILE *file;
471 char name[MAXDNAME];
472 const u_char *n;
474 n = __p_fqnname(cp, msg, MAXCDNAME, name, sizeof name);
475 if (n == NULL)
476 return (NULL);
477 fputs(name, file);
478 return (n);
482 * Print resource record fields in human readable form.
484 const u_char *
485 __p_rr(cp, msg, file)
486 const u_char *cp, *msg;
487 FILE *file;
489 int type, class, dlen, n, c;
490 struct in_addr inaddr;
491 const u_char *cp1, *cp2;
492 u_int32_t tmpttl, t;
493 int lcnt;
494 u_int16_t keyflags;
495 char rrname[MAXDNAME]; /* The fqdn of this RR */
496 char base64_key[MAX_KEY_BASE64];
498 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
499 __set_h_errno (NETDB_INTERNAL);
500 return (NULL);
502 cp = __p_fqnname(cp, msg, MAXCDNAME, rrname, sizeof rrname);
503 if (!cp)
504 return (NULL); /* compression error */
505 fputs(rrname, file);
507 type = _getshort((u_char*)cp);
508 cp += INT16SZ;
509 class = _getshort((u_char*)cp);
510 cp += INT16SZ;
511 tmpttl = _getlong((u_char*)cp);
512 cp += INT32SZ;
513 dlen = _getshort((u_char*)cp);
514 cp += INT16SZ;
515 cp1 = cp;
516 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_TTLID))
517 fprintf(file, "\t%lu", (u_long)tmpttl);
518 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_CLASS))
519 fprintf(file, "\t%s", __p_class(class));
520 fprintf(file, "\t%s", __p_type(type));
522 * Print type specific data, if appropriate
524 switch (type) {
525 case T_A:
526 switch (class) {
527 case C_IN:
528 case C_HS:
529 bcopy(cp, (char *)&inaddr, INADDRSZ);
530 if (dlen == 4) {
531 fprintf(file, "\t%s", inet_ntoa(inaddr));
532 cp += dlen;
533 } else if (dlen == 7) {
534 char *address;
535 u_char protocol;
536 u_short port;
538 address = inet_ntoa(inaddr);
539 cp += INADDRSZ;
540 protocol = *(u_char*)cp;
541 cp += sizeof (u_char);
542 port = _getshort((u_char*)cp);
543 cp += INT16SZ;
544 fprintf(file, "\t%s\t; proto %d, port %d",
545 address, protocol, port);
547 break;
548 default:
549 cp += dlen;
551 break;
552 case T_CNAME:
553 case T_MB:
554 case T_MG:
555 case T_MR:
556 case T_NS:
557 case T_PTR:
558 putc('\t', file);
559 if ((cp = p_fqname(cp, msg, file)) == NULL)
560 return (NULL);
561 break;
563 case T_HINFO:
564 case T_ISDN:
565 cp2 = cp + dlen;
566 (void) fputs("\t\"", file);
567 if ((n = (unsigned char) *cp++) != 0) {
568 for (c = n; c > 0 && cp < cp2; c--) {
569 if (strchr("\n\"\\", *cp))
570 (void) putc('\\', file);
571 (void) putc(*cp++, file);
574 putc('"', file);
575 if (cp < cp2 && (n = (unsigned char) *cp++) != 0) {
576 (void) fputs ("\t\"", file);
577 for (c = n; c > 0 && cp < cp2; c--) {
578 if (strchr("\n\"\\", *cp))
579 (void) putc('\\', file);
580 (void) putc(*cp++, file);
582 putc('"', file);
583 } else if (type == T_HINFO) {
584 (void) fputs("\"?\"", file);
585 fprintf(file, "\n;; *** Warning *** OS-type missing");
587 break;
589 case T_SOA:
590 putc('\t', file);
591 if ((cp = p_fqname(cp, msg, file)) == NULL)
592 return (NULL);
593 putc(' ', file);
594 if ((cp = p_fqname(cp, msg, file)) == NULL)
595 return (NULL);
596 fputs(" (\n", file);
597 t = _getlong((u_char*)cp); cp += INT32SZ;
598 fprintf(file, "\t\t\t%lu\t; serial\n", (u_long)t);
599 t = _getlong((u_char*)cp); cp += INT32SZ;
600 fprintf(file, "\t\t\t%lu\t; refresh (%s)\n",
601 (u_long)t, __p_time(t));
602 t = _getlong((u_char*)cp); cp += INT32SZ;
603 fprintf(file, "\t\t\t%lu\t; retry (%s)\n",
604 (u_long)t, __p_time(t));
605 t = _getlong((u_char*)cp); cp += INT32SZ;
606 fprintf(file, "\t\t\t%lu\t; expire (%s)\n",
607 (u_long)t, __p_time(t));
608 t = _getlong((u_char*)cp); cp += INT32SZ;
609 fprintf(file, "\t\t\t%lu )\t; minimum (%s)",
610 (u_long)t, __p_time(t));
611 break;
613 case T_MX:
614 case T_AFSDB:
615 case T_RT:
616 fprintf(file, "\t%d ", _getshort((u_char*)cp));
617 cp += INT16SZ;
618 if ((cp = p_fqname(cp, msg, file)) == NULL)
619 return (NULL);
620 break;
622 case T_PX:
623 fprintf(file, "\t%d ", _getshort((u_char*)cp));
624 cp += INT16SZ;
625 if ((cp = p_fqname(cp, msg, file)) == NULL)
626 return (NULL);
627 putc(' ', file);
628 if ((cp = p_fqname(cp, msg, file)) == NULL)
629 return (NULL);
630 break;
632 case T_X25:
633 cp2 = cp + dlen;
634 (void) fputs("\t\"", file);
635 if ((n = (unsigned char) *cp++) != 0) {
636 for (c = n; c > 0 && cp < cp2; c--) {
637 if (strchr("\n\"\\", *cp))
638 (void) putc('\\', file);
639 (void) putc(*cp++, file);
642 putc('"', file);
643 break;
645 case T_TXT:
646 (void) putc('\t', file);
647 cp2 = cp1 + dlen;
648 while (cp < cp2) {
649 putc('"', file);
650 if (n = (unsigned char) *cp++) {
651 for (c = n; c > 0 && cp < cp2; c--) {
652 if (strchr("\n\"\\", *cp))
653 (void) putc('\\', file);
654 (void) putc(*cp++, file);
657 putc('"', file);
658 if (cp < cp2)
659 putc(' ', file);
661 break;
663 case T_NSAP:
664 (void) fprintf(file, "\t%s", inet_nsap_ntoa(dlen, cp, NULL));
665 cp += dlen;
666 break;
668 case T_AAAA: {
669 char t[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
671 fprintf(file, "\t%s", inet_ntop(AF_INET6, cp, t, sizeof t));
672 cp += dlen;
673 break;
676 case T_LOC: {
677 char t[255];
679 (void) fprintf(file, "\t%s", loc_ntoa(cp, t));
680 cp += dlen;
681 break;
684 case T_SRV: {
685 u_int priority, weight, port;
687 priority = _getshort(cp); cp += INT16SZ;
688 weight = _getshort(cp); cp += INT16SZ;
689 port = _getshort(cp); cp += INT16SZ;
690 fprintf(file, "\t%u %u %u ", priority, weight, port);
691 if ((cp = p_fqname(cp, msg, file)) == NULL)
692 return (NULL);
693 break;
696 case T_MINFO:
697 case T_RP:
698 putc('\t', file);
699 if ((cp = p_fqname(cp, msg, file)) == NULL)
700 return (NULL);
701 putc(' ', file);
702 if ((cp = p_fqname(cp, msg, file)) == NULL)
703 return (NULL);
704 break;
706 case T_UINFO:
707 putc('\t', file);
708 fputs((char *)cp, file);
709 cp += dlen;
710 break;
712 case T_UID:
713 case T_GID:
714 if (dlen == 4) {
715 fprintf(file, "\t%u", _getlong((u_char*)cp));
716 cp += INT32SZ;
718 break;
720 case T_WKS:
721 if (dlen < INT32SZ + 1)
722 break;
723 bcopy(cp, (char *)&inaddr, INADDRSZ);
724 cp += INT32SZ;
725 fprintf(file, "\t%s %s ( ",
726 inet_ntoa(inaddr),
727 deproto((int) *cp));
728 cp += sizeof (u_char);
729 n = 0;
730 lcnt = 0;
731 while (cp < cp1 + dlen) {
732 c = *cp++;
733 do {
734 if (c & 0200) {
735 if (lcnt == 0) {
736 fputs("\n\t\t\t", file);
737 lcnt = 5;
739 fputs(dewks(n), file);
740 putc(' ', file);
741 lcnt--;
743 c <<= 1;
744 } while (++n & 07);
746 putc(')', file);
747 break;
749 case T_KEY:
750 putc('\t', file);
751 keyflags = _getshort(cp);
752 cp += 2;
753 fprintf(file,"0x%04x", keyflags ); /* flags */
754 fprintf(file," %u", *cp++); /* protocol */
755 fprintf(file," %u (", *cp++); /* algorithm */
757 n = b64_ntop(cp, (cp1 + dlen) - cp,
758 base64_key, sizeof base64_key);
759 for (c = 0; c < n; ++c) {
760 if (0 == (c & 0x3F))
761 fprintf(file, "\n\t");
762 putc(base64_key[c], file); /* public key data */
765 fprintf(file, " )");
766 if (n < 0)
767 fprintf(file, "\t; BAD BASE64");
768 fflush(file);
769 cp = cp1 + dlen;
770 break;
772 case T_SIG:
773 type = _getshort((u_char*)cp);
774 cp += INT16SZ;
775 fprintf(file, " %s", p_type(type));
776 fprintf(file, "\t%d", *cp++); /* algorithm */
777 /* Check label value and print error if wrong. */
778 n = *cp++;
779 c = dn_count_labels (rrname);
780 if (n != c)
781 fprintf(file, "\t; LABELS WRONG (%d should be %d)\n\t",
782 n, c);
783 /* orig ttl */
784 n = _getlong((u_char*)cp);
785 if (n != tmpttl)
786 fprintf(file, " %u", n);
787 cp += INT32SZ;
788 /* sig expire */
789 fprintf(file, " (\n\t%s",
790 __p_secstodate(_getlong((u_char*)cp)));
791 cp += INT32SZ;
792 /* time signed */
793 fprintf(file, " %s", __p_secstodate(_getlong((u_char*)cp)));
794 cp += INT32SZ;
795 /* sig footprint */
796 fprintf(file," %u ", _getshort((u_char*)cp));
797 cp += INT16SZ;
798 /* signer's name */
799 cp = p_fqname(cp, msg, file);
800 n = b64_ntop(cp, (cp1 + dlen) - cp,
801 base64_key, sizeof base64_key);
802 for (c = 0; c < n; c++) {
803 if (0 == (c & 0x3F))
804 fprintf (file, "\n\t");
805 putc(base64_key[c], file); /* signature */
807 /* Clean up... */
808 fprintf(file, " )");
809 if (n < 0)
810 fprintf(file, "\t; BAD BASE64");
811 fflush(file);
812 cp = cp1+dlen;
813 break;
815 #ifdef ALLOW_T_UNSPEC
816 case T_UNSPEC:
818 int NumBytes = 8;
819 u_char *DataPtr;
820 int i;
822 if (dlen < NumBytes) NumBytes = dlen;
823 fprintf(file, "\tFirst %d bytes of hex data:",
824 NumBytes);
825 for (i = 0, DataPtr = cp; i < NumBytes; i++, DataPtr++)
826 fprintf(file, " %x", *DataPtr);
827 cp += dlen;
829 break;
830 #endif /* ALLOW_T_UNSPEC */
832 default:
833 fprintf(file, "\t?%d?", type);
834 cp += dlen;
836 #if 0
837 fprintf(file, "\t; dlen=%d, ttl %s\n", dlen, __p_time(tmpttl));
838 #else
839 putc('\n', file);
840 #endif
841 if (cp - cp1 != dlen) {
842 fprintf(file,
843 ";; packet size error (found %lu, dlen was %d)\n",
844 (unsigned long) (cp - cp1), dlen);
845 cp = NULL;
847 return (cp);
851 * Names of RR classes and qclasses. Classes and qclasses are the same, except
852 * that C_ANY is a qclass but not a class. (You can ask for records of class
853 * C_ANY, but you can't have any records of that class in the database.)
855 const struct res_sym __p_class_syms[] = {
856 {C_IN, "IN"},
857 {C_CHAOS, "CHAOS"},
858 {C_HS, "HS"},
859 {C_HS, "HESIOD"},
860 {C_ANY, "ANY"},
861 {C_IN, (char *)0}
865 * Names of RR types and qtypes. Types and qtypes are the same, except
866 * that T_ANY is a qtype but not a type. (You can ask for records of type
867 * T_ANY, but you can't have any records of that type in the database.)
869 const struct res_sym __p_type_syms[] = {
870 {T_A, "A", "address"},
871 {T_NS, "NS", "name server"},
872 {T_MD, "MD", "mail destination (deprecated)"},
873 {T_MF, "MF", "mail forwarder (deprecated)"},
874 {T_CNAME, "CNAME", "canonical name"},
875 {T_SOA, "SOA", "start of authority"},
876 {T_MB, "MB", "mailbox"},
877 {T_MG, "MG", "mail group member"},
878 {T_MR, "MR", "mail rename"},
879 {T_NULL, "NULL", "null"},
880 {T_WKS, "WKS", "well-known service (deprecated)"},
881 {T_PTR, "PTR", "domain name pointer"},
882 {T_HINFO, "HINFO", "host information"},
883 {T_MINFO, "MINFO", "mailbox information"},
884 {T_MX, "MX", "mail exchanger"},
885 {T_TXT, "TXT", "text"},
886 {T_RP, "RP", "responsible person"},
887 {T_AFSDB, "AFSDB", "DCE or AFS server"},
888 {T_X25, "X25", "X25 address"},
889 {T_ISDN, "ISDN", "ISDN address"},
890 {T_RT, "RT", "router"},
891 {T_NSAP, "NSAP", "nsap address"},
892 {T_NSAP_PTR, "NSAP_PTR", "domain name pointer"},
893 {T_SIG, "SIG", "signature"},
894 {T_KEY, "KEY", "key"},
895 {T_PX, "PX", "mapping information"},
896 {T_GPOS, "GPOS", "geographical position (withdrawn)"},
897 {T_AAAA, "AAAA", "IPv6 address"},
898 {T_LOC, "LOC", "location"},
899 {T_NXT, "NXT", "next valid name (unimplemented)"},
900 {T_EID, "EID", "endpoint identifier (unimplemented)"},
901 {T_NIMLOC, "NIMLOC", "NIMROD locator (unimplemented)"},
902 {T_SRV, "SRV", "server selection"},
903 {T_ATMA, "ATMA", "ATM address (unimplemented)"},
904 {T_IXFR, "IXFR", "incremental zone transfer"},
905 {T_AXFR, "AXFR", "zone transfer"},
906 {T_MAILB, "MAILB", "mailbox-related data (deprecated)"},
907 {T_MAILA, "MAILA", "mail agent (deprecated)"},
908 {T_UINFO, "UINFO", "user information (nonstandard)"},
909 {T_UID, "UID", "user ID (nonstandard)"},
910 {T_GID, "GID", "group ID (nonstandard)"},
911 #ifdef ALLOW_T_UNSPEC
912 {T_UNSPEC, "UNSPEC", "unspecified data (nonstandard)"},
913 #endif /* ALLOW_T_UNSPEC */
914 {T_ANY, "ANY", "\"any\""},
915 {0, NULL, NULL}
919 __sym_ston(syms, name, success)
920 const struct res_sym *syms;
921 char *name;
922 int *success;
924 #ifdef _LIBC
925 /* Changed to prevent warning. --drepper@gnu */
926 for (; syms->name != 0; syms++) {
927 #else
928 for (NULL; syms->name != 0; syms++) {
929 #endif
930 if (strcasecmp (name, syms->name) == 0) {
931 if (success)
932 *success = 1;
933 return (syms->number);
936 if (success)
937 *success = 0;
938 return (syms->number); /* The default value. */
941 const char *
942 __sym_ntos(syms, number, success)
943 const struct res_sym *syms;
944 int number;
945 int *success;
947 static char unname[20];
949 #ifdef _LIBC
950 /* Changed to prevent warning. --drepper@gnu */
951 for (; syms->name != 0; syms++) {
952 #else
953 for (NULL; syms->name != 0; syms++) {
954 #endif
955 if (number == syms->number) {
956 if (success)
957 *success = 1;
958 return (syms->name);
962 sprintf (unname, "%d", number);
963 if (success)
964 *success = 0;
965 return (unname);
969 const char *
970 __sym_ntop(syms, number, success)
971 const struct res_sym *syms;
972 int number;
973 int *success;
975 static char unname[20];
977 #ifdef _LIBC
978 /* Changed to prevent warning. --drepper@gnu */
979 for (; syms->name != 0; syms++) {
980 #else
981 for (NULL; syms->name != 0; syms++) {
982 #endif
983 if (number == syms->number) {
984 if (success)
985 *success = 1;
986 return (syms->humanname);
989 sprintf(unname, "%d", number);
990 if (success)
991 *success = 0;
992 return (unname);
996 * Return a string for the type
998 const char *
999 __p_type(type)
1000 int type;
1002 return (__sym_ntos (__p_type_syms, type, (int *)0));
1006 * Return a mnemonic for class
1008 const char *
1009 __p_class(class)
1010 int class;
1012 return (__sym_ntos (__p_class_syms, class, (int *)0));
1016 * Return a mnemonic for an option
1018 const char *
1019 __p_option(option)
1020 u_long option;
1022 static char nbuf[40];
1024 switch (option) {
1025 case RES_INIT: return "init";
1026 case RES_DEBUG: return "debug";
1027 case RES_AAONLY: return "aaonly(unimpl)";
1028 case RES_USEVC: return "usevc";
1029 case RES_PRIMARY: return "primry(unimpl)";
1030 case RES_IGNTC: return "igntc";
1031 case RES_RECURSE: return "recurs";
1032 case RES_DEFNAMES: return "defnam";
1033 case RES_STAYOPEN: return "styopn";
1034 case RES_DNSRCH: return "dnsrch";
1035 case RES_INSECURE1: return "insecure1";
1036 case RES_INSECURE2: return "insecure2";
1037 default: sprintf(nbuf, "?0x%lx?", (u_long)option);
1038 return (nbuf);
1043 * Return a mnemonic for a time to live
1045 const char *
1046 p_time(value)
1047 u_int32_t value;
1049 static char nbuf[60];
1050 int secs, mins, hours, days;
1051 register char *p;
1053 if (value == 0) {
1054 strcpy(nbuf, "0 secs");
1055 return (nbuf);
1058 secs = value % 60;
1059 value /= 60;
1060 mins = value % 60;
1061 value /= 60;
1062 hours = value % 24;
1063 value /= 24;
1064 days = value;
1065 value = 0;
1067 #define PLURALIZE(x) x, (x == 1) ? "" : "s"
1068 p = nbuf;
1069 if (days) {
1070 (void)sprintf(p, "%d day%s", PLURALIZE(days));
1071 while (*++p);
1073 if (hours) {
1074 if (days)
1075 *p++ = ' ';
1076 (void)sprintf(p, "%d hour%s", PLURALIZE(hours));
1077 while (*++p);
1079 if (mins) {
1080 if (days || hours)
1081 *p++ = ' ';
1082 (void)sprintf(p, "%d min%s", PLURALIZE(mins));
1083 while (*++p);
1085 if (secs || ! (days || hours || mins)) {
1086 if (days || hours || mins)
1087 *p++ = ' ';
1088 (void)sprintf(p, "%d sec%s", PLURALIZE(secs));
1090 return (nbuf);
1094 * routines to convert between on-the-wire RR format and zone file format.
1095 * Does not contain conversion to/from decimal degrees; divide or multiply
1096 * by 60*60*1000 for that.
1099 static unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000,
1100 1000000,10000000,100000000,1000000000};
1102 /* takes an XeY precision/size value, returns a string representation. */
1103 static const char *
1104 precsize_ntoa(prec)
1105 u_int8_t prec;
1107 static char retbuf[sizeof "90000000.00"];
1108 unsigned long val;
1109 int mantissa, exponent;
1111 mantissa = (int)((prec >> 4) & 0x0f) % 10;
1112 exponent = (int)((prec >> 0) & 0x0f) % 10;
1114 val = mantissa * poweroften[exponent];
1116 (void) sprintf(retbuf, "%ld.%.2ld", val/100, val%100);
1117 return (retbuf);
1120 /* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
1121 static u_int8_t
1122 precsize_aton(strptr)
1123 char **strptr;
1125 unsigned int mval = 0, cmval = 0;
1126 u_int8_t retval = 0;
1127 register char *cp;
1128 register int exponent;
1129 register int mantissa;
1131 cp = *strptr;
1133 while (isdigit(*cp))
1134 mval = mval * 10 + (*cp++ - '0');
1136 if (*cp == '.') { /* centimeters */
1137 cp++;
1138 if (isdigit(*cp)) {
1139 cmval = (*cp++ - '0') * 10;
1140 if (isdigit(*cp)) {
1141 cmval += (*cp++ - '0');
1145 cmval = (mval * 100) + cmval;
1147 for (exponent = 0; exponent < 9; exponent++)
1148 if (cmval < poweroften[exponent+1])
1149 break;
1151 mantissa = cmval / poweroften[exponent];
1152 if (mantissa > 9)
1153 mantissa = 9;
1155 retval = (mantissa << 4) | exponent;
1157 *strptr = cp;
1159 return (retval);
1162 /* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
1163 static u_int32_t
1164 latlon2ul(latlonstrptr,which)
1165 char **latlonstrptr;
1166 int *which;
1168 register char *cp;
1169 u_int32_t retval;
1170 int deg = 0, min = 0, secs = 0, secsfrac = 0;
1172 cp = *latlonstrptr;
1174 while (isdigit(*cp))
1175 deg = deg * 10 + (*cp++ - '0');
1177 while (isspace(*cp))
1178 cp++;
1180 if (!(isdigit(*cp)))
1181 goto fndhemi;
1183 while (isdigit(*cp))
1184 min = min * 10 + (*cp++ - '0');
1186 while (isspace(*cp))
1187 cp++;
1189 if (!(isdigit(*cp)))
1190 goto fndhemi;
1192 while (isdigit(*cp))
1193 secs = secs * 10 + (*cp++ - '0');
1195 if (*cp == '.') { /* decimal seconds */
1196 cp++;
1197 if (isdigit(*cp)) {
1198 secsfrac = (*cp++ - '0') * 100;
1199 if (isdigit(*cp)) {
1200 secsfrac += (*cp++ - '0') * 10;
1201 if (isdigit(*cp)) {
1202 secsfrac += (*cp++ - '0');
1208 while (!isspace(*cp)) /* if any trailing garbage */
1209 cp++;
1211 while (isspace(*cp))
1212 cp++;
1214 fndhemi:
1215 switch (*cp) {
1216 case 'N': case 'n':
1217 case 'E': case 'e':
1218 retval = ((unsigned)1<<31)
1219 + (((((deg * 60) + min) * 60) + secs) * 1000)
1220 + secsfrac;
1221 break;
1222 case 'S': case 's':
1223 case 'W': case 'w':
1224 retval = ((unsigned)1<<31)
1225 - (((((deg * 60) + min) * 60) + secs) * 1000)
1226 - secsfrac;
1227 break;
1228 default:
1229 retval = 0; /* invalid value -- indicates error */
1230 break;
1233 switch (*cp) {
1234 case 'N': case 'n':
1235 case 'S': case 's':
1236 *which = 1; /* latitude */
1237 break;
1238 case 'E': case 'e':
1239 case 'W': case 'w':
1240 *which = 2; /* longitude */
1241 break;
1242 default:
1243 *which = 0; /* error */
1244 break;
1247 cp++; /* skip the hemisphere */
1249 while (!isspace(*cp)) /* if any trailing garbage */
1250 cp++;
1252 while (isspace(*cp)) /* move to next field */
1253 cp++;
1255 *latlonstrptr = cp;
1257 return (retval);
1260 /* converts a zone file representation in a string to an RDATA on-the-wire
1261 * representation. */
1263 loc_aton(ascii, binary)
1264 const char *ascii;
1265 u_char *binary;
1267 const char *cp, *maxcp;
1268 u_char *bcp;
1270 u_int32_t latit = 0, longit = 0, alt = 0;
1271 u_int32_t lltemp1 = 0, lltemp2 = 0;
1272 int altmeters = 0, altfrac = 0, altsign = 1;
1273 u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */
1274 u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */
1275 u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */
1276 int which1 = 0, which2 = 0;
1278 cp = ascii;
1279 maxcp = cp + strlen(ascii);
1281 lltemp1 = latlon2ul(&cp, &which1);
1283 lltemp2 = latlon2ul(&cp, &which2);
1285 switch (which1 + which2) {
1286 case 3: /* 1 + 2, the only valid combination */
1287 if ((which1 == 1) && (which2 == 2)) { /* normal case */
1288 latit = lltemp1;
1289 longit = lltemp2;
1290 } else if ((which1 == 2) && (which2 == 1)) { /* reversed */
1291 longit = lltemp1;
1292 latit = lltemp2;
1293 } else { /* some kind of brokenness */
1294 return (0);
1296 break;
1297 default: /* we didn't get one of each */
1298 return (0);
1301 /* altitude */
1302 if (*cp == '-') {
1303 altsign = -1;
1304 cp++;
1307 if (*cp == '+')
1308 cp++;
1310 while (isdigit(*cp))
1311 altmeters = altmeters * 10 + (*cp++ - '0');
1313 if (*cp == '.') { /* decimal meters */
1314 cp++;
1315 if (isdigit(*cp)) {
1316 altfrac = (*cp++ - '0') * 10;
1317 if (isdigit(*cp)) {
1318 altfrac += (*cp++ - '0');
1323 alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
1325 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
1326 cp++;
1328 while (isspace(*cp) && (cp < maxcp))
1329 cp++;
1331 if (cp >= maxcp)
1332 goto defaults;
1334 siz = precsize_aton(&cp);
1336 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
1337 cp++;
1339 while (isspace(*cp) && (cp < maxcp))
1340 cp++;
1342 if (cp >= maxcp)
1343 goto defaults;
1345 hp = precsize_aton(&cp);
1347 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
1348 cp++;
1350 while (isspace(*cp) && (cp < maxcp))
1351 cp++;
1353 if (cp >= maxcp)
1354 goto defaults;
1356 vp = precsize_aton(&cp);
1358 defaults:
1360 bcp = binary;
1361 *bcp++ = (u_int8_t) 0; /* version byte */
1362 *bcp++ = siz;
1363 *bcp++ = hp;
1364 *bcp++ = vp;
1365 PUTLONG(latit,bcp);
1366 PUTLONG(longit,bcp);
1367 PUTLONG(alt,bcp);
1369 return (16); /* size of RR in octets */
1372 /* takes an on-the-wire LOC RR and formats it in a human readable format. */
1373 const char *
1374 loc_ntoa(binary, ascii)
1375 const u_char *binary;
1376 char *ascii;
1378 static char *error = "?";
1379 register const u_char *cp = binary;
1381 int latdeg, latmin, latsec, latsecfrac;
1382 int longdeg, longmin, longsec, longsecfrac;
1383 char northsouth, eastwest;
1384 int altmeters, altfrac, altsign;
1386 const int referencealt = 100000 * 100;
1388 int32_t latval, longval, altval;
1389 u_int32_t templ;
1390 u_int8_t sizeval, hpval, vpval, versionval;
1392 char *sizestr, *hpstr, *vpstr;
1394 versionval = *cp++;
1396 if (versionval) {
1397 sprintf(ascii, "; error: unknown LOC RR version");
1398 return (ascii);
1401 sizeval = *cp++;
1403 hpval = *cp++;
1404 vpval = *cp++;
1406 GETLONG(templ, cp);
1407 latval = (templ - ((unsigned)1<<31));
1409 GETLONG(templ, cp);
1410 longval = (templ - ((unsigned)1<<31));
1412 GETLONG(templ, cp);
1413 if (templ < referencealt) { /* below WGS 84 spheroid */
1414 altval = referencealt - templ;
1415 altsign = -1;
1416 } else {
1417 altval = templ - referencealt;
1418 altsign = 1;
1421 if (latval < 0) {
1422 northsouth = 'S';
1423 latval = -latval;
1424 } else
1425 northsouth = 'N';
1427 latsecfrac = latval % 1000;
1428 latval = latval / 1000;
1429 latsec = latval % 60;
1430 latval = latval / 60;
1431 latmin = latval % 60;
1432 latval = latval / 60;
1433 latdeg = latval;
1435 if (longval < 0) {
1436 eastwest = 'W';
1437 longval = -longval;
1438 } else
1439 eastwest = 'E';
1441 longsecfrac = longval % 1000;
1442 longval = longval / 1000;
1443 longsec = longval % 60;
1444 longval = longval / 60;
1445 longmin = longval % 60;
1446 longval = longval / 60;
1447 longdeg = longval;
1449 altfrac = altval % 100;
1450 altmeters = (altval / 100) * altsign;
1452 if ((sizestr = strdup(precsize_ntoa(sizeval))) == NULL)
1453 sizestr = error;
1454 if ((hpstr = strdup(precsize_ntoa(hpval))) == NULL)
1455 hpstr = error;
1456 if ((vpstr = strdup(precsize_ntoa(vpval))) == NULL)
1457 vpstr = error;
1459 sprintf(ascii,
1460 "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %d.%.2dm %sm %sm %sm",
1461 latdeg, latmin, latsec, latsecfrac, northsouth,
1462 longdeg, longmin, longsec, longsecfrac, eastwest,
1463 altmeters, altfrac, sizestr, hpstr, vpstr);
1465 if (sizestr != error)
1466 free(sizestr);
1467 if (hpstr != error)
1468 free(hpstr);
1469 if (vpstr != error)
1470 free(vpstr);
1472 return (ascii);
1476 /* Return the number of DNS hierarchy levels in the name. */
1478 __dn_count_labels(name)
1479 char *name;
1481 int i, len, count;
1483 len = strlen(name);
1485 for(i = 0, count = 0; i < len; i++) {
1486 if (name[i] == '.')
1487 count++;
1490 /* don't count initial wildcard */
1491 if (name[0] == '*')
1492 if (count)
1493 count--;
1495 /* don't count the null label for root. */
1496 /* if terminating '.' not found, must adjust */
1497 /* count to include last label */
1498 if (len > 0 && name[len-1] != '.')
1499 count++;
1500 return (count);
1505 * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
1506 * SIG records are required to be printed like this, by the Secure DNS RFC.
1508 char *
1509 __p_secstodate (secs)
1510 unsigned long secs;
1512 static char output[15]; /* YYYYMMDDHHMMSS and null */
1513 time_t clock = secs;
1514 struct tm time;
1516 __gmtime_r(&clock, &time);
1517 time.tm_year += 1900;
1518 time.tm_mon += 1;
1519 sprintf(output, "%04d%02d%02d%02d%02d%02d",
1520 time.tm_year, time.tm_mon, time.tm_mday,
1521 time.tm_hour, time.tm_min, time.tm_sec);
1522 return (output);