flag fields are u_int's
[unix-history] / usr / src / lib / libc / net / res_debug.c
index 7b283d9..bac8d0b 100644 (file)
@@ -1,22 +1,29 @@
-/*
- * Copyright (c) 1985 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+/*-
+ * Copyright (c) 1985, 1990 Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
+ *
+ *     @(#)res_debug.c 5.36 (Berkeley) %G%
  */
 
  */
 
-#ifndef lint
-static char sccsid[] = "@(#)res_debug.c        5.1 (Berkeley) %G%";
-#endif not lint
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)res_debug.c        5.36 (Berkeley) %G%";
+#endif /* LIBC_SCCS and not lint */
 
 
-#include <sys/types.h>
+#include <sys/param.h>
 #include <netinet/in.h>
 #include <netinet/in.h>
+#include <arpa/inet.h>
+#include <arpa/nameser.h>
+#include <resolv.h>
 #include <stdio.h>
 #include <stdio.h>
-#include <nameser.h>
+#include <string.h>
 
 
-extern char *p_cdname(), *p_rr(), *p_type(), *p_class();
-extern char *inet_ntoa();
+void __fp_query();
+char *__p_class(), *__p_time(), *__p_type();
+static char *p_cdname(), *p_rr();
 
 
-char *opcodes[] = {
+char *_res_opcodes[] = {
        "QUERY",
        "IQUERY",
        "CQUERYM",
        "QUERY",
        "IQUERY",
        "CQUERYM",
@@ -26,16 +33,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",
@@ -54,12 +61,20 @@ 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(msg)
+void
+__fp_query(msg,file)
        char *msg;
        char *msg;
+       FILE *file;
 {
        register char *cp;
        register HEADER *hp;
 {
        register char *cp;
        register HEADER *hp;
@@ -70,40 +85,41 @@ p_query(msg)
         */
        hp = (HEADER *)msg;
        cp = msg + sizeof(HEADER);
         */
        hp = (HEADER *)msg;
        cp = msg + 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: ");
+       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, msg);
+                       fprintf(file,"\t");
+                       cp = p_cdname(cp, msg, file);
                        if (cp == NULL)
                                return;
                        if (cp == NULL)
                                return;
-                       printf(", type = %s", p_type(getshort(cp)));
+                       fprintf(file,", type = %s", __p_type(_getshort(cp)));
                        cp += sizeof(u_short);
                        cp += sizeof(u_short);
-                       printf(", class = %s\n\n", p_class(getshort(cp)));
+                       fprintf(file,
+                           ", class = %s\n\n", __p_class(_getshort(cp)));
                        cp += sizeof(u_short);
                }
        }
                        cp += sizeof(u_short);
                }
        }
@@ -111,10 +127,10 @@ p_query(msg)
         * 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, msg);
+                       fprintf(file,"\t");
+                       cp = p_rr(cp, msg, file);
                        if (cp == NULL)
                                return;
                }
                        if (cp == NULL)
                                return;
                }
@@ -123,10 +139,10 @@ p_query(msg)
         * 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, msg);
+                       fprintf(file,"\t");
+                       cp = p_rr(cp, msg, file);
                        if (cp == NULL)
                                return;
                }
                        if (cp == NULL)
                                return;
                }
@@ -135,53 +151,56 @@ p_query(msg)
         * 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, msg);
+                       fprintf(file,"\t");
+                       cp = p_rr(cp, msg, file);
                        if (cp == NULL)
                                return;
                }
        }
 }
 
                        if (cp == NULL)
                                return;
                }
        }
 }
 
-char *
-p_cdname(cp, msg)
+static char *
+p_cdname(cp, msg, file)
        char *cp, *msg;
        char *cp, *msg;
+       FILE *file;
 {
        char name[MAXDNAME];
        int n;
 
 {
        char name[MAXDNAME];
        int n;
 
-       if ((n = dn_expand(msg, cp, name, sizeof(name))) < 0)
+       if ((n = dn_expand((u_char *)msg, (u_char *)msg + 512, (u_char *)cp,
+           (u_char *)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);
 }
 
 /*
  * Print resource record fields in human readable form.
  */
        return (cp + n);
 }
 
 /*
  * Print resource record fields in human readable form.
  */
-char *
-p_rr(cp, msg)
+static char *
+p_rr(cp, msg, file)
        char *cp, *msg;
        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, msg)) == NULL)
+       if ((cp = p_cdname(cp, msg, file)) == NULL)
                return (NULL);                  /* compression error */
                return (NULL);                  /* compression error */
-       printf("\n\ttype = %s", p_type(type = getshort(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 = getshort(cp)));
+       fprintf(file,", class = %s", __p_class(class = _getshort(cp)));
        cp += sizeof(u_short);
        cp += sizeof(u_short);
-       printf(", ttl = %ld", getlong(cp));
+       fprintf(file,", ttl = %s", __p_time(_getlong(cp)));
        cp += sizeof(u_long);
        cp += sizeof(u_long);
-       printf(", dlen = %d\n", dlen = getshort(cp));
+       fprintf(file,", dlen = %d\n", dlen = _getshort(cp));
        cp += sizeof(u_short);
        cp1 = cp;
        /*
        cp += sizeof(u_short);
        cp1 = cp;
        /*
@@ -191,79 +210,103 @@ p_rr(cp, msg)
        case T_A:
                switch (class) {
                case C_IN:
        case T_A:
                switch (class) {
                case C_IN:
+               case C_HS:
                        bcopy(cp, (char *)&inaddr, sizeof(inaddr));
                        if (dlen == 4) {
                        bcopy(cp, (char *)&inaddr, sizeof(inaddr));
                        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, msg);
-               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, msg);
-               printf("\n\tmail addr = ");
-               cp = p_cdname(cp, msg);
-               printf("\n\tserial=%ld", getlong(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=%ld", getlong(cp));
+               fprintf(file,"\n\trefresh = %s", __p_time(_getlong(cp)));
                cp += sizeof(u_long);
                cp += sizeof(u_long);
-               printf(", retry=%ld", getlong(cp));
+               fprintf(file,"\n\tretry = %s", __p_time(_getlong(cp)));
                cp += sizeof(u_long);
                cp += sizeof(u_long);
-               printf(", expire=%ld", getlong(cp));
+               fprintf(file,"\n\texpire = %s", __p_time(_getlong(cp)));
                cp += sizeof(u_long);
                cp += sizeof(u_long);
-               printf(", min=%ld\n", getlong(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:
        case T_MINFO:
-               printf("\trequests = ");
-               cp = p_cdname(cp, msg);
-               printf("\n\terrors = ");
-               cp = p_cdname(cp, msg);
+               fprintf(file,"\trequests = ");
+               cp = p_cdname(cp, msg, file);
+               fprintf(file,"\n\terrors = ");
+               cp = p_cdname(cp, msg, file);
                break;
 
        case T_UINFO:
                break;
 
        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%ld\n", getlong(cp));
+                       fprintf(file,"\t%ld\n", _getlong(cp));
                        cp += sizeof(int);
                }
                break;
                        cp += sizeof(int);
                }
                break;
@@ -273,51 +316,65 @@ p_rr(cp, msg)
                        break;
                bcopy(cp, (char *)&inaddr, sizeof(inaddr));
                cp += sizeof(u_long);
                        break;
                bcopy(cp, (char *)&inaddr, sizeof(inaddr));
                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;
+
+#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;
                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
  */
 char *
 
 /*
  * Return a string for the type
  */
 char *
-p_type(type)
+__p_type(type)
        int 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");
@@ -337,6 +394,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 */
@@ -351,8 +412,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);
        }
 }
 
        }
 }
 
@@ -360,18 +426,67 @@ p_type(type)
  * Return a mnemonic for class
  */
 char *
  * Return a mnemonic for class
  */
 char *
-p_class(class)
+__p_class(class)
        int class;
 {
 
        switch (class) {
        case C_IN:              /* internet class */
                return("IN");
        int class;
 {
 
        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;
+
+       if (value == 0) {
+               strcpy(nbuf, "0 secs");
+               return(nbuf);
+       }
+
+       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);
 }
 }