update from main archive 960904
[glibc.git] / resolv / res_debug.c
blobb47667626fd7bd8c435df8f9aac0d75b04c15cd9
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 /* ultrix 4.0's packaging has some icky packaging. alias for it here.
242 * there is more junk of this kind over in res_comp.c.
244 void
245 p_query(msg)
246 const u_char *msg;
248 __p_query(msg);
250 #endif
253 * Print the current options.
254 * This is intended to be primarily a debugging routine.
256 void
257 __fp_resstat(statp, file)
258 struct __res_state *statp;
259 FILE *file;
261 register u_long mask;
263 fprintf(file, ";; res options:");
264 if (!statp)
265 statp = &_res;
266 for (mask = 1; mask != 0; mask <<= 1)
267 if (statp->options & mask)
268 fprintf(file, " %s", p_option(mask));
269 putc('\n', file);
273 * Print the contents of a query.
274 * This is intended to be primarily a debugging routine.
276 void
277 __fp_nquery(msg, len, file)
278 const u_char *msg;
279 int len;
280 FILE *file;
282 register const u_char *cp, *endMark;
283 register const HEADER *hp;
284 register int n;
286 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
287 return;
289 #define TruncTest(x) if (x >= endMark) goto trunc
290 #define ErrorTest(x) if (x == NULL) goto error
293 * Print header fields.
295 hp = (HEADER *)msg;
296 cp = msg + HFIXEDSZ;
297 endMark = cp + len;
298 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEADX) || hp->rcode) {
299 fprintf(file, ";; ->>HEADER<<- opcode: %s, status: %s, id: %d",
300 _res_opcodes[hp->opcode],
301 _res_resultcodes[hp->rcode],
302 ntohs(hp->id));
303 putc('\n', file);
305 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEADX))
306 putc(';', file);
307 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEAD2)) {
308 fprintf(file, "; flags:");
309 if (hp->qr)
310 fprintf(file, " qr");
311 if (hp->aa)
312 fprintf(file, " aa");
313 if (hp->tc)
314 fprintf(file, " tc");
315 if (hp->rd)
316 fprintf(file, " rd");
317 if (hp->ra)
318 fprintf(file, " ra");
319 if (hp->unused)
320 fprintf(file, " UNUSED-BIT-ON");
321 if (hp->ad)
322 fprintf(file, " ad");
323 if (hp->cd)
324 fprintf(file, " cd");
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))) {
334 putc('\n',file);
337 * Print question records.
339 if (n = ntohs(hp->qdcount)) {
340 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
341 fprintf(file, ";; QUESTIONS:\n");
342 while (--n >= 0) {
343 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
344 fprintf(file, ";;\t");
345 TruncTest(cp);
346 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
347 cp = p_cdnname(cp, msg, len, file);
348 else {
349 int n;
350 char name[MAXDNAME];
352 if ((n = dn_expand(msg, msg+len, cp, name,
353 sizeof name)) < 0)
354 cp = NULL;
355 else
356 cp += n;
358 ErrorTest(cp);
359 TruncTest(cp);
360 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
361 fprintf(file, ", type = %s",
362 __p_type(_getshort((u_char*)cp)));
363 cp += INT16SZ;
364 TruncTest(cp);
365 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
366 fprintf(file, ", class = %s\n",
367 __p_class(_getshort((u_char*)cp)));
368 cp += INT16SZ;
369 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
370 putc('\n', file);
374 * Print authoritative answer records
376 TruncTest(cp);
377 cp = do_rrset(msg, len, cp, hp->ancount, RES_PRF_ANS, file,
378 ";; ANSWERS:\n");
379 ErrorTest(cp);
382 * print name server records
384 TruncTest(cp);
385 cp = do_rrset(msg, len, cp, hp->nscount, RES_PRF_AUTH, file,
386 ";; AUTHORITY RECORDS:\n");
387 ErrorTest(cp);
389 TruncTest(cp);
391 * print additional records
393 cp = do_rrset(msg, len, cp, hp->arcount, RES_PRF_ADD, file,
394 ";; ADDITIONAL RECORDS:\n");
395 ErrorTest(cp);
396 return;
397 trunc:
398 fprintf(file, "\n;; ...truncated\n");
399 return;
400 error:
401 fprintf(file, "\n;; ...malformed\n");
404 void
405 __fp_query(msg, file)
406 const u_char *msg;
407 FILE *file;
409 fp_nquery(msg, PACKETSZ, file);
412 const u_char *
413 __p_cdnname(cp, msg, len, file)
414 const u_char *cp, *msg;
415 int len;
416 FILE *file;
418 char name[MAXDNAME];
419 int n;
421 if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0)
422 return (NULL);
423 if (name[0] == '\0')
424 putc('.', file);
425 else
426 fputs(name, file);
427 return (cp + n);
430 const u_char *
431 __p_cdname(cp, msg, file)
432 const u_char *cp, *msg;
433 FILE *file;
435 return (p_cdnname(cp, msg, PACKETSZ, file));
439 /* Return a fully-qualified domain name from a compressed name (with
440 length supplied). */
442 const u_char *
443 __p_fqnname(cp, msg, msglen, name, namelen)
444 const u_char *cp, *msg;
445 int msglen;
446 char *name;
447 int namelen;
449 int n, newlen;
451 if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)
452 return (NULL);
453 newlen = strlen (name);
454 if (newlen == 0 || name[newlen - 1] != '.')
455 if (newlen+1 >= namelen) /* Lack space for final dot */
456 return (NULL);
457 else
458 strcpy(name + newlen, ".");
459 return (cp + n);
462 /* XXX: the rest of these functions need to become length-limited, too. (vix)
465 const u_char *
466 __p_fqname(cp, msg, file)
467 const u_char *cp, *msg;
468 FILE *file;
470 char name[MAXDNAME];
471 const u_char *n;
473 n = __p_fqnname(cp, msg, MAXCDNAME, name, sizeof name);
474 if (n == NULL)
475 return (NULL);
476 fputs(name, file);
477 return (n);
481 * Print resource record fields in human readable form.
483 const u_char *
484 __p_rr(cp, msg, file)
485 const u_char *cp, *msg;
486 FILE *file;
488 int type, class, dlen, n, c;
489 struct in_addr inaddr;
490 const u_char *cp1, *cp2;
491 u_int32_t tmpttl, t;
492 int lcnt;
493 u_int16_t keyflags;
494 char rrname[MAXDNAME]; /* The fqdn of this RR */
495 char base64_key[MAX_KEY_BASE64];
497 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
498 h_errno = NETDB_INTERNAL;
499 return (NULL);
501 cp = __p_fqnname(cp, msg, MAXCDNAME, rrname, sizeof rrname);
502 if (!cp)
503 return (NULL); /* compression error */
504 fputs(rrname, file);
506 type = _getshort((u_char*)cp);
507 cp += INT16SZ;
508 class = _getshort((u_char*)cp);
509 cp += INT16SZ;
510 tmpttl = _getlong((u_char*)cp);
511 cp += INT32SZ;
512 dlen = _getshort((u_char*)cp);
513 cp += INT16SZ;
514 cp1 = cp;
515 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_TTLID))
516 fprintf(file, "\t%lu", (u_long)tmpttl);
517 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_CLASS))
518 fprintf(file, "\t%s", __p_class(class));
519 fprintf(file, "\t%s", __p_type(type));
521 * Print type specific data, if appropriate
523 switch (type) {
524 case T_A:
525 switch (class) {
526 case C_IN:
527 case C_HS:
528 bcopy(cp, (char *)&inaddr, INADDRSZ);
529 if (dlen == 4) {
530 fprintf(file, "\t%s", inet_ntoa(inaddr));
531 cp += dlen;
532 } else if (dlen == 7) {
533 char *address;
534 u_char protocol;
535 u_short port;
537 address = inet_ntoa(inaddr);
538 cp += INADDRSZ;
539 protocol = *(u_char*)cp;
540 cp += sizeof (u_char);
541 port = _getshort((u_char*)cp);
542 cp += INT16SZ;
543 fprintf(file, "\t%s\t; proto %d, port %d",
544 address, protocol, port);
546 break;
547 default:
548 cp += dlen;
550 break;
551 case T_CNAME:
552 case T_MB:
553 case T_MG:
554 case T_MR:
555 case T_NS:
556 case T_PTR:
557 putc('\t', file);
558 if ((cp = p_fqname(cp, msg, file)) == NULL)
559 return (NULL);
560 break;
562 case T_HINFO:
563 case T_ISDN:
564 cp2 = cp + dlen;
565 (void) fputs("\t\"", file);
566 if ((n = (unsigned char) *cp++) != 0) {
567 for (c = n; c > 0 && cp < cp2; c--) {
568 if (strchr("\n\"\\", *cp))
569 (void) putc('\\', file);
570 (void) putc(*cp++, file);
573 putc('"', file);
574 if (cp < cp2 && (n = (unsigned char) *cp++) != 0) {
575 (void) fputs ("\t\"", file);
576 for (c = n; c > 0 && cp < cp2; c--) {
577 if (strchr("\n\"\\", *cp))
578 (void) putc('\\', file);
579 (void) putc(*cp++, file);
581 putc('"', file);
582 } else if (type == T_HINFO) {
583 (void) fputs("\"?\"", file);
584 fprintf(file, "\n;; *** Warning *** OS-type missing");
586 break;
588 case T_SOA:
589 putc('\t', file);
590 if ((cp = p_fqname(cp, msg, file)) == NULL)
591 return (NULL);
592 putc(' ', file);
593 if ((cp = p_fqname(cp, msg, file)) == NULL)
594 return (NULL);
595 fputs(" (\n", file);
596 t = _getlong((u_char*)cp); cp += INT32SZ;
597 fprintf(file, "\t\t\t%lu\t; serial\n", (u_long)t);
598 t = _getlong((u_char*)cp); cp += INT32SZ;
599 fprintf(file, "\t\t\t%lu\t; refresh (%s)\n",
600 (u_long)t, __p_time(t));
601 t = _getlong((u_char*)cp); cp += INT32SZ;
602 fprintf(file, "\t\t\t%lu\t; retry (%s)\n",
603 (u_long)t, __p_time(t));
604 t = _getlong((u_char*)cp); cp += INT32SZ;
605 fprintf(file, "\t\t\t%lu\t; expire (%s)\n",
606 (u_long)t, __p_time(t));
607 t = _getlong((u_char*)cp); cp += INT32SZ;
608 fprintf(file, "\t\t\t%lu )\t; minimum (%s)",
609 (u_long)t, __p_time(t));
610 break;
612 case T_MX:
613 case T_AFSDB:
614 case T_RT:
615 fprintf(file, "\t%d ", _getshort((u_char*)cp));
616 cp += INT16SZ;
617 if ((cp = p_fqname(cp, msg, file)) == NULL)
618 return (NULL);
619 break;
621 case T_PX:
622 fprintf(file, "\t%d ", _getshort((u_char*)cp));
623 cp += INT16SZ;
624 if ((cp = p_fqname(cp, msg, file)) == NULL)
625 return (NULL);
626 putc(' ', file);
627 if ((cp = p_fqname(cp, msg, file)) == NULL)
628 return (NULL);
629 break;
631 case T_X25:
632 cp2 = cp + dlen;
633 (void) fputs("\t\"", file);
634 if ((n = (unsigned char) *cp++) != 0) {
635 for (c = n; c > 0 && cp < cp2; c--) {
636 if (strchr("\n\"\\", *cp))
637 (void) putc('\\', file);
638 (void) putc(*cp++, file);
641 putc('"', file);
642 break;
644 case T_TXT:
645 (void) putc('\t', file);
646 cp2 = cp1 + dlen;
647 while (cp < cp2) {
648 putc('"', file);
649 if (n = (unsigned char) *cp++) {
650 for (c = n; c > 0 && cp < cp2; c--) {
651 if (strchr("\n\"\\", *cp))
652 (void) putc('\\', file);
653 (void) putc(*cp++, file);
656 putc('"', file);
657 if (cp < cp2)
658 putc(' ', file);
660 break;
662 case T_NSAP:
663 (void) fprintf(file, "\t%s", inet_nsap_ntoa(dlen, cp, NULL));
664 cp += dlen;
665 break;
667 case T_AAAA: {
668 char t[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
670 fprintf(file, "\t%s", inet_ntop(AF_INET6, cp, t, sizeof t));
671 cp += dlen;
672 break;
675 case T_LOC: {
676 char t[255];
678 (void) fprintf(file, "\t%s", loc_ntoa(cp, t));
679 cp += dlen;
680 break;
683 case T_SRV: {
684 u_int priority, weight, port;
686 priority = _getshort(cp); cp += INT16SZ;
687 weight = _getshort(cp); cp += INT16SZ;
688 port = _getshort(cp); cp += INT16SZ;
689 fprintf(file, "\t%u %u %u ", priority, weight, port);
690 if ((cp = p_fqname(cp, msg, file)) == NULL)
691 return (NULL);
692 break;
695 case T_MINFO:
696 case T_RP:
697 putc('\t', file);
698 if ((cp = p_fqname(cp, msg, file)) == NULL)
699 return (NULL);
700 putc(' ', file);
701 if ((cp = p_fqname(cp, msg, file)) == NULL)
702 return (NULL);
703 break;
705 case T_UINFO:
706 putc('\t', file);
707 fputs((char *)cp, file);
708 cp += dlen;
709 break;
711 case T_UID:
712 case T_GID:
713 if (dlen == 4) {
714 fprintf(file, "\t%u", _getlong((u_char*)cp));
715 cp += INT32SZ;
717 break;
719 case T_WKS:
720 if (dlen < INT32SZ + 1)
721 break;
722 bcopy(cp, (char *)&inaddr, INADDRSZ);
723 cp += INT32SZ;
724 fprintf(file, "\t%s %s ( ",
725 inet_ntoa(inaddr),
726 deproto((int) *cp));
727 cp += sizeof (u_char);
728 n = 0;
729 lcnt = 0;
730 while (cp < cp1 + dlen) {
731 c = *cp++;
732 do {
733 if (c & 0200) {
734 if (lcnt == 0) {
735 fputs("\n\t\t\t", file);
736 lcnt = 5;
738 fputs(dewks(n), file);
739 putc(' ', file);
740 lcnt--;
742 c <<= 1;
743 } while (++n & 07);
745 putc(')', file);
746 break;
748 case T_KEY:
749 putc('\t', file);
750 keyflags = _getshort(cp);
751 cp += 2;
752 fprintf(file,"0x%04x", keyflags ); /* flags */
753 fprintf(file," %u", *cp++); /* protocol */
754 fprintf(file," %u (", *cp++); /* algorithm */
756 n = b64_ntop(cp, (cp1 + dlen) - cp,
757 base64_key, sizeof base64_key);
758 for (c = 0; c < n; ++c) {
759 if (0 == (c & 0x3F))
760 fprintf(file, "\n\t");
761 putc(base64_key[c], file); /* public key data */
764 fprintf(file, " )");
765 if (n < 0)
766 fprintf(file, "\t; BAD BASE64");
767 fflush(file);
768 cp = cp1 + dlen;
769 break;
771 case T_SIG:
772 type = _getshort((u_char*)cp);
773 cp += INT16SZ;
774 fprintf(file, " %s", p_type(type));
775 fprintf(file, "\t%d", *cp++); /* algorithm */
776 /* Check label value and print error if wrong. */
777 n = *cp++;
778 c = dn_count_labels (rrname);
779 if (n != c)
780 fprintf(file, "\t; LABELS WRONG (%d should be %d)\n\t",
781 n, c);
782 /* orig ttl */
783 n = _getlong((u_char*)cp);
784 if (n != tmpttl)
785 fprintf(file, " %u", n);
786 cp += INT32SZ;
787 /* sig expire */
788 fprintf(file, " (\n\t%s",
789 __p_secstodate(_getlong((u_char*)cp)));
790 cp += INT32SZ;
791 /* time signed */
792 fprintf(file, " %s", __p_secstodate(_getlong((u_char*)cp)));
793 cp += INT32SZ;
794 /* sig footprint */
795 fprintf(file," %u ", _getshort((u_char*)cp));
796 cp += INT16SZ;
797 /* signer's name */
798 cp = p_fqname(cp, msg, file);
799 n = b64_ntop(cp, (cp1 + dlen) - cp,
800 base64_key, sizeof base64_key);
801 for (c = 0; c < n; c++) {
802 if (0 == (c & 0x3F))
803 fprintf (file, "\n\t");
804 putc(base64_key[c], file); /* signature */
806 /* Clean up... */
807 fprintf(file, " )");
808 if (n < 0)
809 fprintf(file, "\t; BAD BASE64");
810 fflush(file);
811 cp = cp1+dlen;
812 break;
814 #ifdef ALLOW_T_UNSPEC
815 case T_UNSPEC:
817 int NumBytes = 8;
818 u_char *DataPtr;
819 int i;
821 if (dlen < NumBytes) NumBytes = dlen;
822 fprintf(file, "\tFirst %d bytes of hex data:",
823 NumBytes);
824 for (i = 0, DataPtr = cp; i < NumBytes; i++, DataPtr++)
825 fprintf(file, " %x", *DataPtr);
826 cp += dlen;
828 break;
829 #endif /* ALLOW_T_UNSPEC */
831 default:
832 fprintf(file, "\t?%d?", type);
833 cp += dlen;
835 #if 0
836 fprintf(file, "\t; dlen=%d, ttl %s\n", dlen, __p_time(tmpttl));
837 #else
838 putc('\n', file);
839 #endif
840 if (cp - cp1 != dlen) {
841 fprintf(file,
842 ";; packet size error (found %lu, dlen was %d)\n",
843 (unsigned long) (cp - cp1), dlen);
844 cp = NULL;
846 return (cp);
850 * Names of RR classes and qclasses. Classes and qclasses are the same, except
851 * that C_ANY is a qclass but not a class. (You can ask for records of class
852 * C_ANY, but you can't have any records of that class in the database.)
854 const struct res_sym __p_class_syms[] = {
855 {C_IN, "IN"},
856 {C_CHAOS, "CHAOS"},
857 {C_HS, "HS"},
858 {C_HS, "HESIOD"},
859 {C_ANY, "ANY"},
860 {C_IN, (char *)0}
864 * Names of RR types and qtypes. Types and qtypes are the same, except
865 * that T_ANY is a qtype but not a type. (You can ask for records of type
866 * T_ANY, but you can't have any records of that type in the database.)
868 const struct res_sym __p_type_syms[] = {
869 {T_A, "A", "address"},
870 {T_NS, "NS", "name server"},
871 {T_MD, "MD", "mail destination (deprecated)"},
872 {T_MF, "MF", "mail forwarder (deprecated)"},
873 {T_CNAME, "CNAME", "canonical name"},
874 {T_SOA, "SOA", "start of authority"},
875 {T_MB, "MB", "mailbox"},
876 {T_MG, "MG", "mail group member"},
877 {T_MR, "MR", "mail rename"},
878 {T_NULL, "NULL", "null"},
879 {T_WKS, "WKS", "well-known service (deprecated)"},
880 {T_PTR, "PTR", "domain name pointer"},
881 {T_HINFO, "HINFO", "host information"},
882 {T_MINFO, "MINFO", "mailbox information"},
883 {T_MX, "MX", "mail exchanger"},
884 {T_TXT, "TXT", "text"},
885 {T_RP, "RP", "responsible person"},
886 {T_AFSDB, "AFSDB", "DCE or AFS server"},
887 {T_X25, "X25", "X25 address"},
888 {T_ISDN, "ISDN", "ISDN address"},
889 {T_RT, "RT", "router"},
890 {T_NSAP, "NSAP", "nsap address"},
891 {T_NSAP_PTR, "NSAP_PTR", "domain name pointer"},
892 {T_SIG, "SIG", "signature"},
893 {T_KEY, "KEY", "key"},
894 {T_PX, "PX", "mapping information"},
895 {T_GPOS, "GPOS", "geographical position (withdrawn)"},
896 {T_AAAA, "AAAA", "IPv6 address"},
897 {T_LOC, "LOC", "location"},
898 {T_NXT, "NXT", "next valid name (unimplemented)"},
899 {T_EID, "EID", "endpoint identifier (unimplemented)"},
900 {T_NIMLOC, "NIMLOC", "NIMROD locator (unimplemented)"},
901 {T_SRV, "SRV", "server selection"},
902 {T_ATMA, "ATMA", "ATM address (unimplemented)"},
903 {T_IXFR, "IXFR", "incremental zone transfer"},
904 {T_AXFR, "AXFR", "zone transfer"},
905 {T_MAILB, "MAILB", "mailbox-related data (deprecated)"},
906 {T_MAILA, "MAILA", "mail agent (deprecated)"},
907 {T_UINFO, "UINFO", "user information (nonstandard)"},
908 {T_UID, "UID", "user ID (nonstandard)"},
909 {T_GID, "GID", "group ID (nonstandard)"},
910 #ifdef ALLOW_T_UNSPEC
911 {T_UNSPEC, "UNSPEC", "unspecified data (nonstandard)"},
912 #endif /* ALLOW_T_UNSPEC */
913 {T_ANY, "ANY", "\"any\""},
914 {0, NULL, NULL}
918 __sym_ston(syms, name, success)
919 const struct res_sym *syms;
920 char *name;
921 int *success;
923 #ifdef _LIBC
924 /* Changed to prevent warning. --drepper@gnu */
925 for (; syms->name != 0; syms++) {
926 #else
927 for (NULL; syms->name != 0; syms++) {
928 #endif
929 if (strcasecmp (name, syms->name) == 0) {
930 if (success)
931 *success = 1;
932 return (syms->number);
935 if (success)
936 *success = 0;
937 return (syms->number); /* The default value. */
940 const char *
941 __sym_ntos(syms, number, success)
942 const struct res_sym *syms;
943 int number;
944 int *success;
946 static char unname[20];
948 #ifdef _LIBC
949 /* Changed to prevent warning. --drepper@gnu */
950 for (; syms->name != 0; syms++) {
951 #else
952 for (NULL; syms->name != 0; syms++) {
953 #endif
954 if (number == syms->number) {
955 if (success)
956 *success = 1;
957 return (syms->name);
961 sprintf (unname, "%d", number);
962 if (success)
963 *success = 0;
964 return (unname);
968 const char *
969 __sym_ntop(syms, number, success)
970 const struct res_sym *syms;
971 int number;
972 int *success;
974 static char unname[20];
976 #ifdef _LIBC
977 /* Changed to prevent warning. --drepper@gnu */
978 for (; syms->name != 0; syms++) {
979 #else
980 for (NULL; syms->name != 0; syms++) {
981 #endif
982 if (number == syms->number) {
983 if (success)
984 *success = 1;
985 return (syms->humanname);
988 sprintf(unname, "%d", number);
989 if (success)
990 *success = 0;
991 return (unname);
995 * Return a string for the type
997 const char *
998 __p_type(type)
999 int type;
1001 return (__sym_ntos (__p_type_syms, type, (int *)0));
1005 * Return a mnemonic for class
1007 const char *
1008 __p_class(class)
1009 int class;
1011 return (__sym_ntos (__p_class_syms, class, (int *)0));
1015 * Return a mnemonic for an option
1017 const char *
1018 __p_option(option)
1019 u_long option;
1021 static char nbuf[40];
1023 switch (option) {
1024 case RES_INIT: return "init";
1025 case RES_DEBUG: return "debug";
1026 case RES_AAONLY: return "aaonly(unimpl)";
1027 case RES_USEVC: return "usevc";
1028 case RES_PRIMARY: return "primry(unimpl)";
1029 case RES_IGNTC: return "igntc";
1030 case RES_RECURSE: return "recurs";
1031 case RES_DEFNAMES: return "defnam";
1032 case RES_STAYOPEN: return "styopn";
1033 case RES_DNSRCH: return "dnsrch";
1034 case RES_INSECURE1: return "insecure1";
1035 case RES_INSECURE2: return "insecure2";
1036 default: sprintf(nbuf, "?0x%lx?", (u_long)option);
1037 return (nbuf);
1042 * Return a mnemonic for a time to live
1044 char *
1045 __p_time(value)
1046 u_int32_t value;
1048 static char nbuf[40];
1049 int secs, mins, hours, days;
1050 register char *p;
1052 if (value == 0) {
1053 strcpy(nbuf, "0 secs");
1054 return (nbuf);
1057 secs = value % 60;
1058 value /= 60;
1059 mins = value % 60;
1060 value /= 60;
1061 hours = value % 24;
1062 value /= 24;
1063 days = value;
1064 value = 0;
1066 #define PLURALIZE(x) x, (x == 1) ? "" : "s"
1067 p = nbuf;
1068 if (days) {
1069 (void)sprintf(p, "%d day%s", PLURALIZE(days));
1070 while (*++p);
1072 if (hours) {
1073 if (days)
1074 *p++ = ' ';
1075 (void)sprintf(p, "%d hour%s", PLURALIZE(hours));
1076 while (*++p);
1078 if (mins) {
1079 if (days || hours)
1080 *p++ = ' ';
1081 (void)sprintf(p, "%d min%s", PLURALIZE(mins));
1082 while (*++p);
1084 if (secs || ! (days || hours || mins)) {
1085 if (days || hours || mins)
1086 *p++ = ' ';
1087 (void)sprintf(p, "%d sec%s", PLURALIZE(secs));
1089 return (nbuf);
1093 * routines to convert between on-the-wire RR format and zone file format.
1094 * Does not contain conversion to/from decimal degrees; divide or multiply
1095 * by 60*60*1000 for that.
1098 static unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000,
1099 1000000,10000000,100000000,1000000000};
1101 /* takes an XeY precision/size value, returns a string representation. */
1102 static const char *
1103 precsize_ntoa(prec)
1104 u_int8_t prec;
1106 static char retbuf[sizeof "90000000.00"];
1107 unsigned long val;
1108 int mantissa, exponent;
1110 mantissa = (int)((prec >> 4) & 0x0f) % 10;
1111 exponent = (int)((prec >> 0) & 0x0f) % 10;
1113 val = mantissa * poweroften[exponent];
1115 (void) sprintf(retbuf, "%ld.%.2ld", val/100, val%100);
1116 return (retbuf);
1119 /* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
1120 static u_int8_t
1121 precsize_aton(strptr)
1122 char **strptr;
1124 unsigned int mval = 0, cmval = 0;
1125 u_int8_t retval = 0;
1126 register char *cp;
1127 register int exponent;
1128 register int mantissa;
1130 cp = *strptr;
1132 while (isdigit(*cp))
1133 mval = mval * 10 + (*cp++ - '0');
1135 if (*cp == '.') { /* centimeters */
1136 cp++;
1137 if (isdigit(*cp)) {
1138 cmval = (*cp++ - '0') * 10;
1139 if (isdigit(*cp)) {
1140 cmval += (*cp++ - '0');
1144 cmval = (mval * 100) + cmval;
1146 for (exponent = 0; exponent < 9; exponent++)
1147 if (cmval < poweroften[exponent+1])
1148 break;
1150 mantissa = cmval / poweroften[exponent];
1151 if (mantissa > 9)
1152 mantissa = 9;
1154 retval = (mantissa << 4) | exponent;
1156 *strptr = cp;
1158 return (retval);
1161 /* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
1162 static u_int32_t
1163 latlon2ul(latlonstrptr,which)
1164 char **latlonstrptr;
1165 int *which;
1167 register char *cp;
1168 u_int32_t retval;
1169 int deg = 0, min = 0, secs = 0, secsfrac = 0;
1171 cp = *latlonstrptr;
1173 while (isdigit(*cp))
1174 deg = deg * 10 + (*cp++ - '0');
1176 while (isspace(*cp))
1177 cp++;
1179 if (!(isdigit(*cp)))
1180 goto fndhemi;
1182 while (isdigit(*cp))
1183 min = min * 10 + (*cp++ - '0');
1185 while (isspace(*cp))
1186 cp++;
1188 if (!(isdigit(*cp)))
1189 goto fndhemi;
1191 while (isdigit(*cp))
1192 secs = secs * 10 + (*cp++ - '0');
1194 if (*cp == '.') { /* decimal seconds */
1195 cp++;
1196 if (isdigit(*cp)) {
1197 secsfrac = (*cp++ - '0') * 100;
1198 if (isdigit(*cp)) {
1199 secsfrac += (*cp++ - '0') * 10;
1200 if (isdigit(*cp)) {
1201 secsfrac += (*cp++ - '0');
1207 while (!isspace(*cp)) /* if any trailing garbage */
1208 cp++;
1210 while (isspace(*cp))
1211 cp++;
1213 fndhemi:
1214 switch (*cp) {
1215 case 'N': case 'n':
1216 case 'E': case 'e':
1217 retval = ((unsigned)1<<31)
1218 + (((((deg * 60) + min) * 60) + secs) * 1000)
1219 + secsfrac;
1220 break;
1221 case 'S': case 's':
1222 case 'W': case 'w':
1223 retval = ((unsigned)1<<31)
1224 - (((((deg * 60) + min) * 60) + secs) * 1000)
1225 - secsfrac;
1226 break;
1227 default:
1228 retval = 0; /* invalid value -- indicates error */
1229 break;
1232 switch (*cp) {
1233 case 'N': case 'n':
1234 case 'S': case 's':
1235 *which = 1; /* latitude */
1236 break;
1237 case 'E': case 'e':
1238 case 'W': case 'w':
1239 *which = 2; /* longitude */
1240 break;
1241 default:
1242 *which = 0; /* error */
1243 break;
1246 cp++; /* skip the hemisphere */
1248 while (!isspace(*cp)) /* if any trailing garbage */
1249 cp++;
1251 while (isspace(*cp)) /* move to next field */
1252 cp++;
1254 *latlonstrptr = cp;
1256 return (retval);
1259 /* converts a zone file representation in a string to an RDATA on-the-wire
1260 * representation. */
1262 loc_aton(ascii, binary)
1263 const char *ascii;
1264 u_char *binary;
1266 const char *cp, *maxcp;
1267 u_char *bcp;
1269 u_int32_t latit = 0, longit = 0, alt = 0;
1270 u_int32_t lltemp1 = 0, lltemp2 = 0;
1271 int altmeters = 0, altfrac = 0, altsign = 1;
1272 u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */
1273 u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */
1274 u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */
1275 int which1 = 0, which2 = 0;
1277 cp = ascii;
1278 maxcp = cp + strlen(ascii);
1280 lltemp1 = latlon2ul(&cp, &which1);
1282 lltemp2 = latlon2ul(&cp, &which2);
1284 switch (which1 + which2) {
1285 case 3: /* 1 + 2, the only valid combination */
1286 if ((which1 == 1) && (which2 == 2)) { /* normal case */
1287 latit = lltemp1;
1288 longit = lltemp2;
1289 } else if ((which1 == 2) && (which2 == 1)) { /* reversed */
1290 longit = lltemp1;
1291 latit = lltemp2;
1292 } else { /* some kind of brokenness */
1293 return (0);
1295 break;
1296 default: /* we didn't get one of each */
1297 return (0);
1300 /* altitude */
1301 if (*cp == '-') {
1302 altsign = -1;
1303 cp++;
1306 if (*cp == '+')
1307 cp++;
1309 while (isdigit(*cp))
1310 altmeters = altmeters * 10 + (*cp++ - '0');
1312 if (*cp == '.') { /* decimal meters */
1313 cp++;
1314 if (isdigit(*cp)) {
1315 altfrac = (*cp++ - '0') * 10;
1316 if (isdigit(*cp)) {
1317 altfrac += (*cp++ - '0');
1322 alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
1324 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
1325 cp++;
1327 while (isspace(*cp) && (cp < maxcp))
1328 cp++;
1330 if (cp >= maxcp)
1331 goto defaults;
1333 siz = precsize_aton(&cp);
1335 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
1336 cp++;
1338 while (isspace(*cp) && (cp < maxcp))
1339 cp++;
1341 if (cp >= maxcp)
1342 goto defaults;
1344 hp = precsize_aton(&cp);
1346 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
1347 cp++;
1349 while (isspace(*cp) && (cp < maxcp))
1350 cp++;
1352 if (cp >= maxcp)
1353 goto defaults;
1355 vp = precsize_aton(&cp);
1357 defaults:
1359 bcp = binary;
1360 *bcp++ = (u_int8_t) 0; /* version byte */
1361 *bcp++ = siz;
1362 *bcp++ = hp;
1363 *bcp++ = vp;
1364 PUTLONG(latit,bcp);
1365 PUTLONG(longit,bcp);
1366 PUTLONG(alt,bcp);
1368 return (16); /* size of RR in octets */
1371 /* takes an on-the-wire LOC RR and formats it in a human readable format. */
1372 char *
1373 loc_ntoa(binary, ascii)
1374 const u_char *binary;
1375 char *ascii;
1377 static char *error = "?";
1378 register const u_char *cp = binary;
1380 int latdeg, latmin, latsec, latsecfrac;
1381 int longdeg, longmin, longsec, longsecfrac;
1382 char northsouth, eastwest;
1383 int altmeters, altfrac, altsign;
1385 const int referencealt = 100000 * 100;
1387 int32_t latval, longval, altval;
1388 u_int32_t templ;
1389 u_int8_t sizeval, hpval, vpval, versionval;
1391 char *sizestr, *hpstr, *vpstr;
1393 versionval = *cp++;
1395 if (versionval) {
1396 sprintf(ascii, "; error: unknown LOC RR version");
1397 return (ascii);
1400 sizeval = *cp++;
1402 hpval = *cp++;
1403 vpval = *cp++;
1405 GETLONG(templ, cp);
1406 latval = (templ - ((unsigned)1<<31));
1408 GETLONG(templ, cp);
1409 longval = (templ - ((unsigned)1<<31));
1411 GETLONG(templ, cp);
1412 if (templ < referencealt) { /* below WGS 84 spheroid */
1413 altval = referencealt - templ;
1414 altsign = -1;
1415 } else {
1416 altval = templ - referencealt;
1417 altsign = 1;
1420 if (latval < 0) {
1421 northsouth = 'S';
1422 latval = -latval;
1423 } else
1424 northsouth = 'N';
1426 latsecfrac = latval % 1000;
1427 latval = latval / 1000;
1428 latsec = latval % 60;
1429 latval = latval / 60;
1430 latmin = latval % 60;
1431 latval = latval / 60;
1432 latdeg = latval;
1434 if (longval < 0) {
1435 eastwest = 'W';
1436 longval = -longval;
1437 } else
1438 eastwest = 'E';
1440 longsecfrac = longval % 1000;
1441 longval = longval / 1000;
1442 longsec = longval % 60;
1443 longval = longval / 60;
1444 longmin = longval % 60;
1445 longval = longval / 60;
1446 longdeg = longval;
1448 altfrac = altval % 100;
1449 altmeters = (altval / 100) * altsign;
1451 if ((sizestr = strdup(precsize_ntoa(sizeval))) == NULL)
1452 sizestr = error;
1453 if ((hpstr = strdup(precsize_ntoa(hpval))) == NULL)
1454 hpstr = error;
1455 if ((vpstr = strdup(precsize_ntoa(vpval))) == NULL)
1456 vpstr = error;
1458 sprintf(ascii,
1459 "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %d.%.2dm %sm %sm %sm",
1460 latdeg, latmin, latsec, latsecfrac, northsouth,
1461 longdeg, longmin, longsec, longsecfrac, eastwest,
1462 altmeters, altfrac, sizestr, hpstr, vpstr);
1464 if (sizestr != error)
1465 free(sizestr);
1466 if (hpstr != error)
1467 free(hpstr);
1468 if (vpstr != error)
1469 free(vpstr);
1471 return (ascii);
1475 /* Return the number of DNS hierarchy levels in the name. */
1477 __dn_count_labels(name)
1478 char *name;
1480 int i, len, count;
1482 len = strlen(name);
1484 for(i = 0, count = 0; i < len; i++) {
1485 if (name[i] == '.')
1486 count++;
1489 /* don't count initial wildcard */
1490 if (name[0] == '*')
1491 if (count)
1492 count--;
1494 /* don't count the null label for root. */
1495 /* if terminating '.' not found, must adjust */
1496 /* count to include last label */
1497 if (len > 0 && name[len-1] != '.')
1498 count++;
1499 return (count);
1504 * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
1505 * SIG records are required to be printed like this, by the Secure DNS RFC.
1507 char *
1508 __p_secstodate (secs)
1509 unsigned long secs;
1511 static char output[15]; /* YYYYMMDDHHMMSS and null */
1512 time_t clock = secs;
1513 struct tm time;
1515 __gmtime_r(&clock, &time);
1516 time.tm_year += 1900;
1517 time.tm_mon += 1;
1518 sprintf(output, "%04d%02d%02d%02d%02d%02d",
1519 time.tm_year, time.tm_mon, time.tm_mday,
1520 time.tm_hour, time.tm_min, time.tm_sec);
1521 return (output);