home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / internet / amitcp3.0b / src.lha / src / amitcp / api / res_debug.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-08  |  11.2 KB  |  524 lines

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