from the ANSI standard
[unix-history] / usr / src / lib / libc / net / res_debug.c
index 141225f..8b2c9e3 100644 (file)
@@ -1,16 +1,33 @@
-#ifndef lint
-static char sccsid[] = "@(#)res_debug.c        4.1 (Berkeley) %G%";
-#endif
+/*
+ * Copyright (c) 1985, 1990 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)res_debug.c        5.29 (Berkeley) %G%";
+#endif /* LIBC_SCCS and not lint */
 
 #include <sys/types.h>
 #include <netinet/in.h>
 #include <stdio.h>
 
 #include <sys/types.h>
 #include <netinet/in.h>
 #include <stdio.h>
-#include <nameser.h>
+#include <arpa/nameser.h>
 
 
-extern char *p_cdname(), *p_rr(), *p_type(), *p_class();
+extern char *p_cdname(), *p_rr(), *p_type(), *p_class(), *p_time();
 extern char *inet_ntoa();
 
 extern char *inet_ntoa();
 
-char *opcodes[] = {
+char *_res_opcodes[] = {
        "QUERY",
        "IQUERY",
        "CQUERYM",
        "QUERY",
        "IQUERY",
        "CQUERYM",
@@ -20,16 +37,16 @@ char *opcodes[] = {
        "6",
        "7",
        "8",
        "6",
        "7",
        "8",
-       "9",
-       "10",
        "UPDATEA",
        "UPDATED",
        "UPDATEA",
        "UPDATED",
+       "UPDATEDA",
        "UPDATEM",
        "UPDATEM",
+       "UPDATEMA",
        "ZONEINIT",
        "ZONEREF",
 };
 
        "ZONEINIT",
        "ZONEREF",
 };
 
-char *rcodes[] = {
+char *_res_resultcodes[] = {
        "NOERROR",
        "FORMERR",
        "SERVFAIL",
        "NOERROR",
        "FORMERR",
        "SERVFAIL",
@@ -48,12 +65,19 @@ char *rcodes[] = {
        "NOCHANGE",
 };
 
        "NOCHANGE",
 };
 
+p_query(msg)
+       char *msg;
+{
+       fp_query(msg,stdout);
+}
+
 /*
  * Print the contents of a query.
  * This is intended to be primarily a debugging routine.
  */
 /*
  * Print the contents of a query.
  * This is intended to be primarily a debugging routine.
  */
-p_query(buf)
-       char *buf;
+fp_query(msg,file)
+       char *msg;
+       FILE *file;
 {
        register char *cp;
        register HEADER *hp;
 {
        register char *cp;
        register HEADER *hp;
@@ -62,42 +86,42 @@ p_query(buf)
        /*
         * Print header fields.
         */
        /*
         * Print header fields.
         */
-       hp = (HEADER *)buf;
-       cp = buf + sizeof(HEADER);
-       printf("HEADER:\n");
-       printf("\topcode = %s", opcodes[hp->opcode]);
-       printf(", id = %d", ntohs(hp->id));
-       printf(", rcode = %s\n", rcodes[hp->rcode]);
-       printf("\theader flags: ");
+       hp = (HEADER *)msg;
+       cp = msg + sizeof(HEADER);
+       fprintf(file,"HEADER:\n");
+       fprintf(file,"\topcode = %s", _res_opcodes[hp->opcode]);
+       fprintf(file,", id = %d", ntohs(hp->id));
+       fprintf(file,", rcode = %s\n", _res_resultcodes[hp->rcode]);
+       fprintf(file,"\theader flags: ");
        if (hp->qr)
        if (hp->qr)
-               printf(" qr");
+               fprintf(file," qr");
        if (hp->aa)
        if (hp->aa)
-               printf(" aa");
+               fprintf(file," aa");
        if (hp->tc)
        if (hp->tc)
-               printf(" tc");
+               fprintf(file," tc");
        if (hp->rd)
        if (hp->rd)
-               printf(" rd");
+               fprintf(file," rd");
        if (hp->ra)
        if (hp->ra)
-               printf(" ra");
+               fprintf(file," ra");
        if (hp->pr)
        if (hp->pr)
-               printf(" pr");
-       printf("\n\tqdcount = %d", ntohs(hp->qdcount));
-       printf(", ancount = %d", ntohs(hp->ancount));
-       printf(", nscount = %d", ntohs(hp->nscount));
-       printf(", arcount = %d\n\n", ntohs(hp->arcount));
+               fprintf(file," pr");
+       fprintf(file,"\n\tqdcount = %d", ntohs(hp->qdcount));
+       fprintf(file,", ancount = %d", ntohs(hp->ancount));
+       fprintf(file,", nscount = %d", ntohs(hp->nscount));
+       fprintf(file,", arcount = %d\n\n", ntohs(hp->arcount));
        /*
         * Print question records.
         */
        if (n = ntohs(hp->qdcount)) {
        /*
         * Print question records.
         */
        if (n = ntohs(hp->qdcount)) {
-               printf("QUESTIONS:\n");
+               fprintf(file,"QUESTIONS:\n");
                while (--n >= 0) {
                while (--n >= 0) {
-                       printf("\t");
-                       cp = p_cdname(cp, buf);
+                       fprintf(file,"\t");
+                       cp = p_cdname(cp, msg, file);
                        if (cp == NULL)
                                return;
                        if (cp == NULL)
                                return;
-                       printf(", type = %s", p_type(ntohs(*(u_short *)cp)));
+                       fprintf(file,", type = %s", p_type(_getshort(cp)));
                        cp += sizeof(u_short);
                        cp += sizeof(u_short);
-                       printf(", class = %s\n\n", p_class(ntohs(*(u_short *)cp)));
+                       fprintf(file,", class = %s\n\n", p_class(_getshort(cp)));
                        cp += sizeof(u_short);
                }
        }
                        cp += sizeof(u_short);
                }
        }
@@ -105,10 +129,10 @@ p_query(buf)
         * Print authoritative answer records
         */
        if (n = ntohs(hp->ancount)) {
         * Print authoritative answer records
         */
        if (n = ntohs(hp->ancount)) {
-               printf("ANSWERS:\n");
+               fprintf(file,"ANSWERS:\n");
                while (--n >= 0) {
                while (--n >= 0) {
-                       printf("\t");
-                       cp = p_rr(cp, buf);
+                       fprintf(file,"\t");
+                       cp = p_rr(cp, msg, file);
                        if (cp == NULL)
                                return;
                }
                        if (cp == NULL)
                                return;
                }
@@ -117,10 +141,10 @@ p_query(buf)
         * print name server records
         */
        if (n = ntohs(hp->nscount)) {
         * print name server records
         */
        if (n = ntohs(hp->nscount)) {
-               printf("NAME SERVERS:\n");
+               fprintf(file,"NAME SERVERS:\n");
                while (--n >= 0) {
                while (--n >= 0) {
-                       printf("\t");
-                       cp = p_rr(cp, buf);
+                       fprintf(file,"\t");
+                       cp = p_rr(cp, msg, file);
                        if (cp == NULL)
                                return;
                }
                        if (cp == NULL)
                                return;
                }
@@ -129,10 +153,10 @@ p_query(buf)
         * print additional records
         */
        if (n = ntohs(hp->arcount)) {
         * print additional records
         */
        if (n = ntohs(hp->arcount)) {
-               printf("ADDITIONAL RECORDS:\n");
+               fprintf(file,"ADDITIONAL RECORDS:\n");
                while (--n >= 0) {
                while (--n >= 0) {
-                       printf("\t");
-                       cp = p_rr(cp, buf);
+                       fprintf(file,"\t");
+                       cp = p_rr(cp, msg, file);
                        if (cp == NULL)
                                return;
                }
                        if (cp == NULL)
                                return;
                }
@@ -140,19 +164,20 @@ p_query(buf)
 }
 
 char *
 }
 
 char *
-p_cdname(cp, buf)
-       char *cp, *buf;
+p_cdname(cp, msg, file)
+       char *cp, *msg;
+       FILE *file;
 {
        char name[MAXDNAME];
        int n;
 
 {
        char name[MAXDNAME];
        int n;
 
-       if ((n = dn_expand(buf, cp, name, sizeof(name))) < 0)
+       if ((n = dn_expand(msg, msg + 512, cp, name, sizeof(name))) < 0)
                return (NULL);
        if (name[0] == '\0') {
                name[0] = '.';
                name[1] = '\0';
        }
                return (NULL);
        if (name[0] == '\0') {
                name[0] = '.';
                name[1] = '\0';
        }
-       fputs(name, stdout);
+       fputs(name, file);
        return (cp + n);
 }
 
        return (cp + n);
 }
 
@@ -160,22 +185,23 @@ p_cdname(cp, buf)
  * Print resource record fields in human readable form.
  */
 char *
  * Print resource record fields in human readable form.
  */
 char *
-p_rr(cp, buf)
-       char *cp, *buf;
+p_rr(cp, msg, file)
+       char *cp, *msg;
+       FILE *file;
 {
        int type, class, dlen, n, c;
        struct in_addr inaddr;
 {
        int type, class, dlen, n, c;
        struct in_addr inaddr;
-       char *cp1;
+       char *cp1, *cp2;
 
 
-       if ((cp = p_cdname(cp, buf)) == NULL)
+       if ((cp = p_cdname(cp, msg, file)) == NULL)
                return (NULL);                  /* compression error */
                return (NULL);                  /* compression error */
-       printf("\n\ttype = %s", p_type(type = ntohs(*(u_short *)cp)));
+       fprintf(file,"\n\ttype = %s", p_type(type = _getshort(cp)));
        cp += sizeof(u_short);
        cp += sizeof(u_short);
-       printf(", class = %s", p_class(class = ntohs(*(u_short *)cp)));
+       fprintf(file,", class = %s", p_class(class = _getshort(cp)));
        cp += sizeof(u_short);
        cp += sizeof(u_short);
-       printf(", ttl = %d", ntohl(*(u_int *)cp));
+       fprintf(file,", ttl = %s", p_time(_getlong(cp)));
        cp += sizeof(u_long);
        cp += sizeof(u_long);
-       printf(", dlen = %d\n", dlen = ntohs(*(u_short *)cp));
+       fprintf(file,", dlen = %d\n", dlen = _getshort(cp));
        cp += sizeof(u_short);
        cp1 = cp;
        /*
        cp += sizeof(u_short);
        cp1 = cp;
        /*
@@ -185,72 +211,103 @@ p_rr(cp, buf)
        case T_A:
                switch (class) {
                case C_IN:
        case T_A:
                switch (class) {
                case C_IN:
-                       inaddr.s_addr = *(u_long *)cp;
+               case C_HS:
+                       bcopy(cp, (char *)&inaddr, sizeof(inaddr));
                        if (dlen == 4) {
                        if (dlen == 4) {
-                               printf("\tinternet address = %s\n",
+                               fprintf(file,"\tinternet address = %s\n",
                                        inet_ntoa(inaddr));
                                cp += dlen;
                        } else if (dlen == 7) {
                                        inet_ntoa(inaddr));
                                cp += dlen;
                        } else if (dlen == 7) {
-                               printf("\tinternet address = %s",
+                               fprintf(file,"\tinternet address = %s",
                                        inet_ntoa(inaddr));
                                        inet_ntoa(inaddr));
-                               printf(", protocol = %d", cp[4]);
-                               printf(", port = %d\n",
+                               fprintf(file,", protocol = %d", cp[4]);
+                               fprintf(file,", port = %d\n",
                                        (cp[5] << 8) + cp[6]);
                                cp += dlen;
                        }
                        break;
                                        (cp[5] << 8) + cp[6]);
                                cp += dlen;
                        }
                        break;
+               default:
+                       cp += dlen;
                }
                break;
        case T_CNAME:
        case T_MB:
                }
                break;
        case T_CNAME:
        case T_MB:
-       case T_MD:
-       case T_MF:
        case T_MG:
        case T_MR:
        case T_NS:
        case T_PTR:
        case T_MG:
        case T_MR:
        case T_NS:
        case T_PTR:
-               printf("\tdomain name = ");
-               cp = p_cdname(cp, buf);
-               printf("\n");
+               fprintf(file,"\tdomain name = ");
+               cp = p_cdname(cp, msg, file);
+               fprintf(file,"\n");
                break;
 
        case T_HINFO:
                if (n = *cp++) {
                break;
 
        case T_HINFO:
                if (n = *cp++) {
-                       printf("\tCPU=%.*s\n", n, cp);
+                       fprintf(file,"\tCPU=%.*s\n", n, cp);
                        cp += n;
                }
                if (n = *cp++) {
                        cp += n;
                }
                if (n = *cp++) {
-                       printf("\tOS=%.*s\n", n, cp);
+                       fprintf(file,"\tOS=%.*s\n", n, cp);
                        cp += n;
                }
                break;
 
        case T_SOA:
                        cp += n;
                }
                break;
 
        case T_SOA:
-               printf("\torigin = ");
-               cp = p_cdname(cp, buf);
-               printf("\n\tmail addr = ");
-               cp = p_cdname(cp, buf);
-               printf("\n\tserial=%d", ntohl(*(u_long *)cp));
+               fprintf(file,"\torigin = ");
+               cp = p_cdname(cp, msg, file);
+               fprintf(file,"\n\tmail addr = ");
+               cp = p_cdname(cp, msg, file);
+               fprintf(file,"\n\tserial = %ld", _getlong(cp));
                cp += sizeof(u_long);
                cp += sizeof(u_long);
-               printf(", refresh=%d", ntohl(*(u_long *)cp));
+               fprintf(file,"\n\trefresh = %s", p_time(_getlong(cp)));
                cp += sizeof(u_long);
                cp += sizeof(u_long);
-               printf(", retry=%d", ntohl(*(u_long *)cp));
+               fprintf(file,"\n\tretry = %s", p_time(_getlong(cp)));
                cp += sizeof(u_long);
                cp += sizeof(u_long);
-               printf(", expire=%d", ntohl(*(u_long *)cp));
+               fprintf(file,"\n\texpire = %s", p_time(_getlong(cp)));
                cp += sizeof(u_long);
                cp += sizeof(u_long);
-               printf(", min=%d\n", ntohl(*(u_long *)cp));
+               fprintf(file,"\n\tmin = %s\n", p_time(_getlong(cp)));
                cp += sizeof(u_long);
                break;
 
                cp += sizeof(u_long);
                break;
 
+       case T_MX:
+               fprintf(file,"\tpreference = %ld,",_getshort(cp));
+               cp += sizeof(u_short);
+               fprintf(file," name = ");
+               cp = p_cdname(cp, msg, file);
+               break;
+
+       case T_TXT:
+               (void) fputs("\t\"", file);
+               cp2 = cp1 + dlen;
+               while (cp < cp2) {
+                       if (n = (unsigned char) *cp++) {
+                               for (c = n; c > 0 && cp < cp2; c--)
+                                       if (*cp == '\n') {
+                                           (void) putc('\\', file);
+                                           (void) putc(*cp++, file);
+                                       } else
+                                           (void) putc(*cp++, file);
+                       }
+               }
+               (void) fputs("\"\n", file);
+               break;
+
+       case T_MINFO:
+               fprintf(file,"\trequests = ");
+               cp = p_cdname(cp, msg, file);
+               fprintf(file,"\n\terrors = ");
+               cp = p_cdname(cp, msg, file);
+               break;
+
        case T_UINFO:
        case T_UINFO:
-               printf("\t%s\n", cp);
+               fprintf(file,"\t%s\n", cp);
                cp += dlen;
                break;
 
        case T_UID:
        case T_GID:
                if (dlen == 4) {
                cp += dlen;
                break;
 
        case T_UID:
        case T_GID:
                if (dlen == 4) {
-                       printf("\t%d\n", ntohl(*(int *)cp));
+                       fprintf(file,"\t%ld\n", _getlong(cp));
                        cp += sizeof(int);
                }
                break;
                        cp += sizeof(int);
                }
                break;
@@ -258,34 +315,53 @@ p_rr(cp, buf)
        case T_WKS:
                if (dlen < sizeof(u_long) + 1)
                        break;
        case T_WKS:
                if (dlen < sizeof(u_long) + 1)
                        break;
-               inaddr.s_addr = *(u_long *)cp;
+               bcopy(cp, (char *)&inaddr, sizeof(inaddr));
                cp += sizeof(u_long);
                cp += sizeof(u_long);
-               printf("\tinternet address = %s, protocol = %d\n\t",
+               fprintf(file,"\tinternet address = %s, protocol = %d\n\t",
                        inet_ntoa(inaddr), *cp++);
                n = 0;
                while (cp < cp1 + dlen) {
                        c = *cp++;
                        do {
                        inet_ntoa(inaddr), *cp++);
                n = 0;
                while (cp < cp1 + dlen) {
                        c = *cp++;
                        do {
-                               if (c & 1)
-                                       printf(" %d", n);
-                               c >>= 1;
+                               if (c & 0200)
+                                       fprintf(file," %d", n);
+                               c <<= 1;
                        } while (++n & 07);
                }
                        } while (++n & 07);
                }
-               putchar('\n');
+               putc('\n',file);
                break;
 
                break;
 
+#ifdef ALLOW_T_UNSPEC
+       case T_UNSPEC:
+               {
+                       int NumBytes = 8;
+                       char *DataPtr;
+                       int i;
+
+                       if (dlen < NumBytes) NumBytes = dlen;
+                       fprintf(file, "\tFirst %d bytes of hex data:",
+                               NumBytes);
+                       for (i = 0, DataPtr = cp; i < NumBytes; i++, DataPtr++)
+                               fprintf(file, " %x", *DataPtr);
+                       fputs("\n", file);
+                       cp += dlen;
+               }
+               break;
+#endif /* ALLOW_T_UNSPEC */
+
        default:
        default:
-               printf("\t???\n");
+               fprintf(file,"\t???\n");
                cp += dlen;
        }
                cp += dlen;
        }
-       if (cp != cp1 + dlen)
-               printf("packet size error (%#x != %#x)\n", cp, cp1+dlen);
-       printf("\n");
+       if (cp != cp1 + dlen) {
+               fprintf(file,"packet size error (%#x != %#x)\n", cp, cp1+dlen);
+               cp = NULL;
+       }
+       fprintf(file,"\n");
        return (cp);
 }
 
        return (cp);
 }
 
-static char nbuf[20];
-extern char *sprintf();
+static char nbuf[40];
 
 /*
  * Return a string for the type
 
 /*
  * Return a string for the type
@@ -294,17 +370,12 @@ char *
 p_type(type)
        int type;
 {
 p_type(type)
        int type;
 {
-
        switch (type) {
        case T_A:
                return("A");
        case T_NS:              /* authoritative server */
                return("NS");
        switch (type) {
        case T_A:
                return("A");
        case T_NS:              /* authoritative server */
                return("NS");
-       case T_MD:              /* mail destination */
-               return("MD");
-       case T_MF:              /* mail forwarder */
-               return("MF");
-       case T_CNAME:           /* connonical name */
+       case T_CNAME:           /* canonical name */
                return("CNAME");
        case T_SOA:             /* start of authority zone */
                return("SOA");
                return("CNAME");
        case T_SOA:             /* start of authority zone */
                return("SOA");
@@ -324,6 +395,10 @@ p_type(type)
                return("HINFO");
        case T_MINFO:           /* mailbox information */
                return("MINFO");
                return("HINFO");
        case T_MINFO:           /* mailbox information */
                return("MINFO");
+       case T_MX:              /* mail routing info */
+               return("MX");
+       case T_TXT:             /* text */
+               return("TXT");
        case T_AXFR:            /* zone transfer */
                return("AXFR");
        case T_MAILB:           /* mail box */
        case T_AXFR:            /* zone transfer */
                return("AXFR");
        case T_MAILB:           /* mail box */
@@ -338,8 +413,13 @@ p_type(type)
                return("UID");
        case T_GID:
                return("GID");
                return("UID");
        case T_GID:
                return("GID");
+#ifdef ALLOW_T_UNSPEC
+       case T_UNSPEC:
+               return("UNSPEC");
+#endif /* ALLOW_T_UNSPEC */
        default:
        default:
-               return (sprintf(nbuf, "%d", type));
+               (void)sprintf(nbuf, "%d", type);
+               return(nbuf);
        }
 }
 
        }
 }
 
@@ -354,11 +434,55 @@ p_class(class)
        switch (class) {
        case C_IN:              /* internet class */
                return("IN");
        switch (class) {
        case C_IN:              /* internet class */
                return("IN");
-       case C_CS:              /* csnet class */
-               return("CS");
+       case C_HS:              /* hesiod class */
+               return("HS");
        case C_ANY:             /* matches any class */
                return("ANY");
        default:
        case C_ANY:             /* matches any class */
                return("ANY");
        default:
-               return (sprintf(nbuf, "%d", class));
+               (void)sprintf(nbuf, "%d", class);
+               return(nbuf);
+       }
+}
+
+/*
+ * Return a mnemonic for a time to live
+ */
+char *
+p_time(value)
+       u_long value;
+{
+       int secs, mins, hours;
+       register char *p;
+
+       secs = value % 60;
+       value /= 60;
+       mins = value % 60;
+       value /= 60;
+       hours = value % 24;
+       value /= 24;
+
+#define        PLURALIZE(x)    x, (x == 1) ? "" : "s"
+       p = nbuf;
+       if (value) {
+               (void)sprintf(p, "%d day%s", PLURALIZE(value));
+               while (*++p);
+       }
+       if (hours) {
+               if (value)
+                       *p++ = ' ';
+               (void)sprintf(p, "%d hour%s", PLURALIZE(hours));
+               while (*++p);
+       }
+       if (mins) {
+               if (value || hours)
+                       *p++ = ' ';
+               (void)sprintf(p, "%d min%s", PLURALIZE(mins));
+               while (*++p);
+       }
+       if (secs || ! (value || hours || mins)) {
+               if (value || hours || mins)
+                       *p++ = ' ';
+               (void)sprintf(p, "%d sec%s", PLURALIZE(secs));
        }
        }
+       return(nbuf);
 }
 }