home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / mint / netlib / lib / rdebug.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-27  |  11.7 KB  |  525 lines

  1. /*
  2.  * Adopted to Mint-Net 1994, Kay Roemer
  3.  */
  4.  
  5. /*-
  6.  * Copyright (c) 1985, 1990 Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  *
  37.  *    @(#)res_debug.c    5.36 (Berkeley) 3/6/91
  38.  */
  39.  
  40. #if defined(LIBC_SCCS) && !defined(lint)
  41. static char sccsid[] = "@(#)res_debug.c    5.36 (Berkeley) 3/6/91";
  42. #endif /* LIBC_SCCS and not lint */
  43.  
  44. #include "socklib.h"
  45. #include <sys/param.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <arpa/nameser.h>
  49. #include <resolv.h>
  50. #include <stdio.h>
  51. #include <string.h>
  52.  
  53. void __fp_query();
  54. char *__p_class(), *__p_time(), *__p_type();
  55. static char *p_cdname(), *p_rr();
  56.  
  57. char *_res_opcodes[] = {
  58.     "QUERY",
  59.     "IQUERY",
  60.     "CQUERYM",
  61.     "CQUERYU",
  62.     "4",
  63.     "5",
  64.     "6",
  65.     "7",
  66.     "8",
  67.     "UPDATEA",
  68.     "UPDATED",
  69.     "UPDATEDA",
  70.     "UPDATEM",
  71.     "UPDATEMA",
  72.     "ZONEINIT",
  73.     "ZONEREF",
  74. };
  75.  
  76. char *_res_resultcodes[] = {
  77.     "NOERROR",
  78.     "FORMERR",
  79.     "SERVFAIL",
  80.     "NXDOMAIN",
  81.     "NOTIMP",
  82.     "REFUSED",
  83.     "6",
  84.     "7",
  85.     "8",
  86.     "9",
  87.     "10",
  88.     "11",
  89.     "12",
  90.     "13",
  91.     "14",
  92.     "NOCHANGE",
  93. };
  94.  
  95. void
  96. __p_query(msg)
  97.     char *msg;
  98. {
  99.     __fp_query(msg,stdout);
  100. }
  101.  
  102. /*
  103.  * Print the contents of a query.
  104.  * This is intended to be primarily a debugging routine.
  105.  */
  106. void
  107. __fp_query(msg,file)
  108.     char *msg;
  109.     FILE *file;
  110. {
  111.     register char *cp;
  112.     register HEADER *hp;
  113.     register int n;
  114.  
  115.     /*
  116.      * Print header fields.
  117.      */
  118.     hp = (HEADER *)msg;
  119.     cp = msg + sizeof(HEADER);
  120.     fprintf(file,"HEADER:\n");
  121.     fprintf(file,"\topcode = %s", _res_opcodes[hp->opcode]);
  122.     fprintf(file,", id = %d", ntohs(hp->id));
  123.     fprintf(file,", rcode = %s\n", _res_resultcodes[hp->rcode]);
  124.     fprintf(file,"\theader flags: ");
  125.     if (hp->qr)
  126.         fprintf(file," qr");
  127.     if (hp->aa)
  128.         fprintf(file," aa");
  129.     if (hp->tc)
  130.         fprintf(file," tc");
  131.     if (hp->rd)
  132.         fprintf(file," rd");
  133.     if (hp->ra)
  134.         fprintf(file," ra");
  135.     if (hp->pr)
  136.         fprintf(file," pr");
  137.     fprintf(file,"\n\tqdcount = %d", ntohs(hp->qdcount));
  138.     fprintf(file,", ancount = %d", ntohs(hp->ancount));
  139.     fprintf(file,", nscount = %d", ntohs(hp->nscount));
  140.     fprintf(file,", arcount = %d\n\n", ntohs(hp->arcount));
  141.     /*
  142.      * Print question records.
  143.      */
  144.     if (n = ntohs(hp->qdcount)) {
  145.         fprintf(file,"QUESTIONS:\n");
  146.         while (--n >= 0) {
  147.             fprintf(file,"\t");
  148.             cp = p_cdname(cp, msg, file);
  149.             if (cp == NULL)
  150.                 return;
  151.             fprintf(file,", type = %s", __p_type(_getshort(cp)));
  152.             cp += sizeof(u_short);
  153.             fprintf(file,
  154.                 ", class = %s\n\n", __p_class(_getshort(cp)));
  155.             cp += sizeof(u_short);
  156.         }
  157.     }
  158.     /*
  159.      * Print authoritative answer records
  160.      */
  161.     if (n = ntohs(hp->ancount)) {
  162.         fprintf(file,"ANSWERS:\n");
  163.         while (--n >= 0) {
  164.             fprintf(file,"\t");
  165.             cp = p_rr(cp, msg, file);
  166.             if (cp == NULL)
  167.                 return;
  168.         }
  169.     }
  170.     /*
  171.      * print name server records
  172.      */
  173.     if (n = ntohs(hp->nscount)) {
  174.         fprintf(file,"NAME SERVERS:\n");
  175.         while (--n >= 0) {
  176.             fprintf(file,"\t");
  177.             cp = p_rr(cp, msg, file);
  178.             if (cp == NULL)
  179.                 return;
  180.         }
  181.     }
  182.     /*
  183.      * print additional records
  184.      */
  185.     if (n = ntohs(hp->arcount)) {
  186.         fprintf(file,"ADDITIONAL RECORDS:\n");
  187.         while (--n >= 0) {
  188.             fprintf(file,"\t");
  189.             cp = p_rr(cp, msg, file);
  190.             if (cp == NULL)
  191.                 return;
  192.         }
  193.     }
  194. }
  195.  
  196. static char *
  197. p_cdname(cp, msg, file)
  198.     char *cp, *msg;
  199.     FILE *file;
  200. {
  201.     char name[MAXDNAME];
  202.     int n;
  203.  
  204.     if ((n = dn_expand((u_char *)msg, (u_char *)msg + 512, (u_char *)cp,
  205.         (u_char *)name, sizeof(name))) < 0)
  206.         return (NULL);
  207.     if (name[0] == '\0') {
  208.         name[0] = '.';
  209.         name[1] = '\0';
  210.     }
  211.     fputs(name, file);
  212.     return (cp + n);
  213. }
  214.  
  215. /*
  216.  * Print resource record fields in human readable form.
  217.  */
  218. static char *
  219. p_rr(cp, msg, file)
  220.     char *cp, *msg;
  221.     FILE *file;
  222. {
  223.     int type, class, dlen, n, c;
  224.     struct in_addr inaddr;
  225.     char *cp1, *cp2;
  226.  
  227.     if ((cp = p_cdname(cp, msg, file)) == NULL)
  228.         return (NULL);            /* compression error */
  229.     fprintf(file,"\n\ttype = %s", __p_type(type = _getshort(cp)));
  230.     cp += sizeof(u_short);
  231.     fprintf(file,", class = %s", __p_class(class = _getshort(cp)));
  232.     cp += sizeof(u_short);
  233.     fprintf(file,", ttl = %s", __p_time(_getlong(cp)));
  234.     cp += sizeof(u_long);
  235.     fprintf(file,", dlen = %d\n", dlen = _getshort(cp));
  236.     cp += sizeof(u_short);
  237.     cp1 = cp;
  238.     /*
  239.      * Print type specific data, if appropriate
  240.      */
  241.     switch (type) {
  242.     case T_A:
  243.         switch (class) {
  244.         case C_IN:
  245.         case C_HS:
  246.             bcopy(cp, (char *)&inaddr, sizeof(inaddr));
  247.             if (dlen == 4) {
  248.                 fprintf(file,"\tinternet address = %s\n",
  249.                     inet_ntoa(inaddr));
  250.                 cp += dlen;
  251.             } else if (dlen == 7) {
  252.                 fprintf(file,"\tinternet address = %s",
  253.                     inet_ntoa(inaddr));
  254.                 fprintf(file,", protocol = %d", cp[4]);
  255.                 fprintf(file,", port = %d\n",
  256.                     (cp[5] << 8) + cp[6]);
  257.                 cp += dlen;
  258.             }
  259.             break;
  260.         default:
  261.             cp += dlen;
  262.         }
  263.         break;
  264.     case T_CNAME:
  265.     case T_MB:
  266.     case T_MG:
  267.     case T_MR:
  268.     case T_NS:
  269.     case T_PTR:
  270.         fprintf(file,"\tdomain name = ");
  271.         cp = p_cdname(cp, msg, file);
  272.         fprintf(file,"\n");
  273.         break;
  274.  
  275.     case T_HINFO:
  276.         if (n = *cp++) {
  277.             fprintf(file,"\tCPU=%.*s\n", n, cp);
  278.             cp += n;
  279.         }
  280.         if (n = *cp++) {
  281.             fprintf(file,"\tOS=%.*s\n", n, cp);
  282.             cp += n;
  283.         }
  284.         break;
  285.  
  286.     case T_SOA:
  287.         fprintf(file,"\torigin = ");
  288.         cp = p_cdname(cp, msg, file);
  289.         fprintf(file,"\n\tmail addr = ");
  290.         cp = p_cdname(cp, msg, file);
  291.         fprintf(file,"\n\tserial = %ld", _getlong(cp));
  292.         cp += sizeof(u_long);
  293.         fprintf(file,"\n\trefresh = %s", __p_time(_getlong(cp)));
  294.         cp += sizeof(u_long);
  295.         fprintf(file,"\n\tretry = %s", __p_time(_getlong(cp)));
  296.         cp += sizeof(u_long);
  297.         fprintf(file,"\n\texpire = %s", __p_time(_getlong(cp)));
  298.         cp += sizeof(u_long);
  299.         fprintf(file,"\n\tmin = %s\n", __p_time(_getlong(cp)));
  300.         cp += sizeof(u_long);
  301.         break;
  302.  
  303.     case T_MX:
  304.         fprintf(file,"\tpreference = %ld,",_getshort(cp));
  305.         cp += sizeof(u_short);
  306.         fprintf(file," name = ");
  307.         cp = p_cdname(cp, msg, file);
  308.         break;
  309.  
  310.       case T_TXT:
  311.         (void) fputs("\t\"", file);
  312.         cp2 = cp1 + dlen;
  313.         while (cp < cp2) {
  314.             if (n = (unsigned char) *cp++) {
  315.                 for (c = n; c > 0 && cp < cp2; c--)
  316.                     if (*cp == '\n') {
  317.                         (void) putc('\\', file);
  318.                         (void) putc(*cp++, file);
  319.                     } else
  320.                         (void) putc(*cp++, file);
  321.             }
  322.         }
  323.         (void) fputs("\"\n", file);
  324.           break;
  325.  
  326.     case T_MINFO:
  327.         fprintf(file,"\trequests = ");
  328.         cp = p_cdname(cp, msg, file);
  329.         fprintf(file,"\n\terrors = ");
  330.         cp = p_cdname(cp, msg, file);
  331.         break;
  332.  
  333.     case T_UINFO:
  334.         fprintf(file,"\t%s\n", cp);
  335.         cp += dlen;
  336.         break;
  337.  
  338.     case T_UID:
  339.     case T_GID:
  340.         if (dlen == 4) {
  341.             fprintf(file,"\t%ld\n", _getlong(cp));
  342.             cp += sizeof(int);
  343.         }
  344.         break;
  345.  
  346.     case T_WKS:
  347.         if (dlen < sizeof(u_long) + 1)
  348.             break;
  349.         bcopy(cp, (char *)&inaddr, sizeof(inaddr));
  350.         cp += sizeof(u_long);
  351.         fprintf(file,"\tinternet address = %s, protocol = %d\n\t",
  352.             inet_ntoa(inaddr), *cp++);
  353.         n = 0;
  354.         while (cp < cp1 + dlen) {
  355.             c = *cp++;
  356.             do {
  357.                  if (c & 0200)
  358.                     fprintf(file," %d", n);
  359.                  c <<= 1;
  360.             } while (++n & 07);
  361.         }
  362.         putc('\n',file);
  363.         break;
  364.  
  365. #ifdef ALLOW_T_UNSPEC
  366.     case T_UNSPEC:
  367.         {
  368.             int NumBytes = 8;
  369.             char *DataPtr;
  370.             int i;
  371.  
  372.             if (dlen < NumBytes) NumBytes = dlen;
  373.             fprintf(file, "\tFirst %d bytes of hex data:",
  374.                 NumBytes);
  375.             for (i = 0, DataPtr = cp; i < NumBytes; i++, DataPtr++)
  376.                 fprintf(file, " %x", *DataPtr);
  377.             fputs("\n", file);
  378.             cp += dlen;
  379.         }
  380.         break;
  381. #endif /* ALLOW_T_UNSPEC */
  382.  
  383.     default:
  384.         fprintf(file,"\t???\n");
  385.         cp += dlen;
  386.     }
  387.     if (cp != cp1 + dlen) {
  388.         fprintf(file,"packet size error (%#lx != %#lx)\n", cp, cp1+dlen);
  389.         cp = NULL;
  390.     }
  391.     fprintf(file,"\n");
  392.     return (cp);
  393. }
  394.  
  395. static    char nbuf[40];
  396.  
  397. /*
  398.  * Return a string for the type
  399.  */
  400. char *
  401. __p_type(type)
  402.     int type;
  403. {
  404.     switch (type) {
  405.     case T_A:
  406.         return("A");
  407.     case T_NS:        /* authoritative server */
  408.         return("NS");
  409.     case T_CNAME:        /* canonical name */
  410.         return("CNAME");
  411.     case T_SOA:        /* start of authority zone */
  412.         return("SOA");
  413.     case T_MB:        /* mailbox domain name */
  414.         return("MB");
  415.     case T_MG:        /* mail group member */
  416.         return("MG");
  417.     case T_MR:        /* mail rename name */
  418.         return("MR");
  419.     case T_NULL:        /* null resource record */
  420.         return("NULL");
  421.     case T_WKS:        /* well known service */
  422.         return("WKS");
  423.     case T_PTR:        /* domain name pointer */
  424.         return("PTR");
  425.     case T_HINFO:        /* host information */
  426.         return("HINFO");
  427.     case T_MINFO:        /* mailbox information */
  428.         return("MINFO");
  429.     case T_MX:        /* mail routing info */
  430.         return("MX");
  431.     case T_TXT:        /* text */
  432.         return("TXT");
  433.     case T_AXFR:        /* zone transfer */
  434.         return("AXFR");
  435.     case T_MAILB:        /* mail box */
  436.         return("MAILB");
  437.     case T_MAILA:        /* mail address */
  438.         return("MAILA");
  439.     case T_ANY:        /* matches any type */
  440.         return("ANY");
  441.     case T_UINFO:
  442.         return("UINFO");
  443.     case T_UID:
  444.         return("UID");
  445.     case T_GID:
  446.         return("GID");
  447. #ifdef ALLOW_T_UNSPEC
  448.     case T_UNSPEC:
  449.         return("UNSPEC");
  450. #endif /* ALLOW_T_UNSPEC */
  451.     default:
  452.         (void)sprintf(nbuf, "%d", type);
  453.         return(nbuf);
  454.     }
  455. }
  456.  
  457. /*
  458.  * Return a mnemonic for class
  459.  */
  460. char *
  461. __p_class(class)
  462.     int class;
  463. {
  464.  
  465.     switch (class) {
  466.     case C_IN:        /* internet class */
  467.         return("IN");
  468.     case C_HS:        /* hesiod class */
  469.         return("HS");
  470.     case C_ANY:        /* matches any class */
  471.         return("ANY");
  472.     default:
  473.         (void)sprintf(nbuf, "%d", class);
  474.         return(nbuf);
  475.     }
  476. }
  477.  
  478. /*
  479.  * Return a mnemonic for a time to live
  480.  */
  481. char *
  482. __p_time(value)
  483.     u_long value;
  484. {
  485.     int secs, mins, hours;
  486.     register char *p;
  487.  
  488.     if (value == 0) {
  489.         strcpy(nbuf, "0 secs");
  490.         return(nbuf);
  491.     }
  492.  
  493.     secs = value % 60;
  494.     value /= 60;
  495.     mins = value % 60;
  496.     value /= 60;
  497.     hours = value % 24;
  498.     value /= 24;
  499.  
  500. #define    PLURALIZE(x)    x, (x == 1) ? "" : "s"
  501.     p = nbuf;
  502.     if (value) {
  503.         (void)sprintf(p, "%ld day%s", PLURALIZE(value));
  504.         while (*++p);
  505.     }
  506.     if (hours) {
  507.         if (value)
  508.             *p++ = ' ';
  509.         (void)sprintf(p, "%d hour%s", PLURALIZE(hours));
  510.         while (*++p);
  511.     }
  512.     if (mins) {
  513.         if (value || hours)
  514.             *p++ = ' ';
  515.         (void)sprintf(p, "%d min%s", PLURALIZE(mins));
  516.         while (*++p);
  517.     }
  518.     if (secs || ! (value || hours || mins)) {
  519.         if (value || hours || mins)
  520.             *p++ = ' ';
  521.         (void)sprintf(p, "%d sec%s", PLURALIZE(secs));
  522.     }
  523.     return(nbuf);
  524. }
  525.