change retry loop to decrement after test
[unix-history] / usr / src / lib / libc / net / res_debug.c
index fde92cb..e2f5a00 100644 (file)
@@ -4,9 +4,13 @@
  * specifies the terms and conditions for redistribution.
  */
 
  * specifies the terms and conditions for redistribution.
  */
 
-#ifndef lint
-static char sccsid[] = "@(#)res_debug.c        5.2 (Berkeley) %G%";
-#endif not lint
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)res_debug.c        5.13 (Berkeley) %G%";
+#endif LIBC_SCCS and not lint
+
+#if defined(lint) && !defined(DEBUG)
+#define DEBUG
+#endif
 
 #include <sys/types.h>
 #include <netinet/in.h>
 
 #include <sys/types.h>
 #include <netinet/in.h>
@@ -54,13 +58,23 @@ char *rcodes[] = {
        "NOCHANGE",
 };
 
        "NOCHANGE",
 };
 
+p_query(msg)
+       char *msg;
+{
+#ifdef DEBUG
+       fp_query(msg,stdout);
+#endif
+}
+
 /*
  * 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)
+fp_query(msg,file)
        char *msg;
        char *msg;
+       FILE *file;
 {
 {
+#ifdef DEBUG
        register char *cp;
        register HEADER *hp;
        register int n;
        register char *cp;
        register HEADER *hp;
        register int n;
@@ -70,40 +84,40 @@ 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", opcodes[hp->opcode]);
+       fprintf(file,", id = %d", ntohs(hp->id));
+       fprintf(file,", rcode = %s\n", rcodes[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 +125,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 +137,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 +149,59 @@ 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;
                }
        }
+#endif
 }
 
 char *
 }
 
 char *
-p_cdname(cp, msg)
+p_cdname(cp, msg, file)
        char *cp, *msg;
        char *cp, *msg;
+       FILE *file;
 {
 {
+#ifdef DEBUG
        char name[MAXDNAME];
        int n;
 
        char name[MAXDNAME];
        int n;
 
-       if ((n = dn_expand(msg, 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);
+#endif
 }
 
 /*
  * Print resource record fields in human readable form.
  */
 char *
 }
 
 /*
  * Print resource record fields in human readable form.
  */
 char *
-p_rr(cp, msg)
+p_rr(cp, msg, file)
        char *cp, *msg;
        char *cp, *msg;
+       FILE *file;
 {
 {
+#ifdef DEBUG
        int type, class, dlen, n, c;
        struct in_addr inaddr;
        char *cp1;
 
        int type, class, dlen, n, c;
        struct in_addr inaddr;
        char *cp1;
 
-       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 = %u", 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;
        /*
@@ -193,14 +213,14 @@ p_rr(cp, msg)
                case C_IN:
                        bcopy(cp, (char *)&inaddr, sizeof(inaddr));
                        if (dlen == 4) {
                case C_IN:
                        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;
                        }
                                        (cp[5] << 8) + cp[6]);
                                cp += dlen;
                        }
@@ -209,61 +229,70 @@ p_rr(cp, msg)
                break;
        case T_CNAME:
        case T_MB:
                break;
        case T_CNAME:
        case T_MB:
+#ifdef OLDRR
        case T_MD:
        case T_MF:
        case T_MD:
        case T_MF:
+#endif /* OLDRR */
        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,", refresh=%ld", getlong(cp));
                cp += sizeof(u_long);
                cp += sizeof(u_long);
-               printf(", retry=%ld", getlong(cp));
+               fprintf(file,", retry=%ld", getlong(cp));
                cp += sizeof(u_long);
                cp += sizeof(u_long);
-               printf(", expire=%ld", getlong(cp));
+               fprintf(file,", expire=%ld", getlong(cp));
                cp += sizeof(u_long);
                cp += sizeof(u_long);
-               printf(", min=%ld\n", getlong(cp));
+               fprintf(file,", min=%ld\n", 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_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,28 +302,29 @@ 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;
 
        default:
                break;
 
        default:
-               printf("\t???\n");
+               fprintf(file,"\t???\n");
                cp += dlen;
        }
        if (cp != cp1 + dlen)
                cp += dlen;
        }
        if (cp != cp1 + dlen)
-               printf("packet size error (%#x != %#x)\n", cp, cp1+dlen);
-       printf("\n");
+               fprintf(file,"packet size error (%#x != %#x)\n", cp, cp1+dlen);
+       fprintf(file,"\n");
        return (cp);
        return (cp);
+#endif
 }
 
 static char nbuf[20];
 }
 
 static char nbuf[20];
@@ -307,16 +337,17 @@ 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");
+#ifdef OLDRR
        case T_MD:              /* mail destination */
                return("MD");
        case T_MF:              /* mail forwarder */
                return("MF");
        case T_MD:              /* mail destination */
                return("MD");
        case T_MF:              /* mail forwarder */
                return("MF");
+#endif /* OLDRR */
        case T_CNAME:           /* connonical name */
                return("CNAME");
        case T_SOA:             /* start of authority zone */
        case T_CNAME:           /* connonical name */
                return("CNAME");
        case T_SOA:             /* start of authority zone */
@@ -325,6 +356,8 @@ p_type(type)
                return("MB");
        case T_MG:              /* mail group member */
                return("MG");
                return("MB");
        case T_MG:              /* mail group member */
                return("MG");
+       case T_MX:              /* mail routing info */
+               return("MX");
        case T_MR:              /* mail rename name */
                return("MR");
        case T_NULL:            /* null resource record */
        case T_MR:              /* mail rename name */
                return("MR");
        case T_NULL:            /* null resource record */
@@ -367,8 +400,6 @@ 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_ANY:             /* matches any class */
                return("ANY");
        default:
        case C_ANY:             /* matches any class */
                return("ANY");
        default: