home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / comm / amitcp-3.0ß2.lha / AmiTCP / src / amitcp / api / res_debug.c < prev    next >
C/C++ Source or Header  |  1994-01-20  |  11KB  |  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,