home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / NETWORK / ISP / bind.4.8.3.lzh / BIND483 / TOOLS / NSLOOKUP / subr.c < prev    next >
C/C++ Source or Header  |  1994-09-23  |  12KB  |  513 lines

  1. /*
  2.  * Copyright (c) 1985,1989 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted provided
  6.  * that: (1) source distributions retain this entire copyright notice and
  7.  * comment, and (2) distributions including binaries display the following
  8.  * acknowledgement:  ``This product includes software developed by the
  9.  * University of California, Berkeley and its contributors'' in the
  10.  * documentation or other materials provided with the distribution and in
  11.  * all advertising materials mentioning features or use of this software.
  12.  * Neither the name of the University nor the names of its contributors may
  13.  * be used to endorse or promote products derived from this software without
  14.  * specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  16.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  17.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #ifndef lint
  21. static char sccsid[] = "@(#)subr.c    5.22 (Berkeley) 8/3/90";
  22. #endif /* not lint */
  23.  
  24. /*
  25.  *******************************************************************************
  26.  *
  27.  *  subr.c --
  28.  *
  29.  *    Miscellaneous subroutines for the name server
  30.  *    lookup program.
  31.  *
  32.  *    Copyright (c) 1985
  33.  *    Andrew Cherenson
  34.  *    U.C. Berkeley
  35.  *    CS298-26  Fall 1985
  36.  *
  37.  *******************************************************************************
  38.  */
  39.  
  40. #ifdef OSK
  41. #include <types.h>
  42. #include <netdb.h>
  43. #include <socket.h>
  44. #include <in.h>
  45. #else
  46. #include <sys/types.h>
  47. #include <netdb.h>
  48. #include <sys/socket.h>
  49. #include <netinet/in.h>
  50. #endif
  51. #include <arpa/nameser.h>
  52. #include <arpa/inet.h>
  53. #include <signal.h>
  54. #include <setjmp.h>
  55. #include <stdio.h>
  56. #ifndef OSK
  57. #include <stdlib.h>
  58. #endif
  59. #include <string.h>
  60. #include "res.h"
  61.  
  62.  
  63.  
  64. /*
  65.  *******************************************************************************
  66.  *
  67.  *  IntrHandler --
  68.  *
  69.  *    This routine is called whenever a control-C is typed.
  70.  *    It performs three main functions:
  71.  *     - closes an open socket connection,
  72.  *     - closes an open output file (used by LookupHost, et al.),
  73.  *     - jumps back to the main read-eval loop.
  74.  *
  75.  *    If a user types a ^C in the middle of a routine that uses a socket,
  76.  *    the routine would not be able to close the socket. To prevent an
  77.  *    overflow of the process's open file table, the socket and output
  78.  *    file descriptors are closed by the interrupt handler.
  79.  *
  80.  *  Side effects:
  81.  *    Open file descriptors are closed.
  82.  *    If filePtr is valid, it is closed.
  83.  *    Flow of control returns to the main() routine.
  84.  *
  85.  *******************************************************************************
  86.  */
  87.  
  88. #ifdef SVR3
  89. void
  90. #else
  91. int
  92. #endif
  93. IntrHandler()
  94. {
  95.     extern jmp_buf env;
  96. #if defined(BSD) && BSD >= 199006
  97.     extern FILE *yyin;        /* scanner input file */
  98.     extern void yyrestart();    /* routine to restart scanner after interrupt */
  99. #endif
  100.  
  101.     SendRequest_close();
  102.     ListHost_close();
  103.     if (filePtr != NULL && filePtr != stdout) {
  104.     fclose(filePtr);
  105.     filePtr = NULL;
  106.     }
  107.     printf("\n");
  108. #if defined(BSD) && BSD >= 199006
  109.     yyrestart(yyin);
  110. #endif
  111.     longjmp(env, 1);
  112. }
  113.  
  114.  
  115. /*
  116.  *******************************************************************************
  117.  *
  118.  *  Malloc --
  119.  *  Calloc --
  120.  *
  121.  *      Calls the malloc library routine with SIGINT blocked to prevent
  122.  *    corruption of malloc's data structures. We need to do this because
  123.  *    a control-C doesn't kill the program -- it causes a return to the
  124.  *    main command loop.
  125.  *
  126.  *    NOTE: This method doesn't prevent the pointer returned by malloc
  127.  *    from getting lost, so it is possible to get "core leaks".
  128.  *
  129.  *    If malloc fails, the program exits.
  130.  *
  131.  *  Results:
  132.  *    (address)    - address of new buffer.
  133.  *
  134.  *******************************************************************************
  135.  */
  136.  
  137. char *
  138. Malloc(size)
  139.     int size;
  140. {
  141.     char    *ptr;
  142.  
  143. #ifdef SYSV
  144. #ifdef SVR3
  145.     sighold(SIGINT);
  146.     ptr = malloc((unsigned) size);
  147.     sigrelse(SIGINT);
  148. #else
  149.     { int (*old)();
  150.       old = signal(SIGINT, SIG_IGN);
  151.       ptr = malloc((unsigned) size);
  152.       signal(SIGINT, old);
  153.     }
  154. #endif
  155. #else
  156.     { int saveMask;
  157.       saveMask = sigblock(sigmask(SIGINT));
  158.       ptr = malloc((unsigned) size);
  159.       (void) sigsetmask(saveMask);
  160.     }
  161. #endif
  162.     if (ptr == NULL) {
  163.     fflush(stdout);
  164.     fprintf(stderr, "*** Can't allocate memory\n");
  165.     fflush(stderr);
  166.     abort();
  167.     /*NOTREACHED*/
  168.     } else {
  169.     return(ptr);
  170.     }
  171. }
  172.  
  173. char *
  174. Calloc(num, size)
  175.     register int num, size;
  176. {
  177.     char *ptr = Malloc(num*size);
  178.     bzero(ptr, num*size);
  179.     return(ptr);
  180. }
  181.  
  182.  
  183. /*
  184.  *******************************************************************************
  185.  *
  186.  *  PrintHostInfo --
  187.  *
  188.  *    Prints out the HostInfo structure for a host.
  189.  *
  190.  *******************************************************************************
  191.  */
  192.  
  193. void
  194. PrintHostInfo(file, title, hp)
  195.     FILE    *file;
  196.     char    *title;
  197.     register HostInfo *hp;
  198. {
  199.     register char        **cp;
  200.     register ServerInfo    **sp;
  201.     char            comma;
  202.     int            i;
  203.  
  204. #ifdef OSK
  205.     if ( title == NULL )
  206.         fprintf(file, "<NULL>  %s", hp->name);
  207.     else
  208. #endif
  209.         fprintf(file, "%-7s  %s", title, hp->name);
  210.  
  211.     if (hp->addrList != NULL) {
  212.         if (hp->addrList[1] != NULL) {
  213.         fprintf(file, "\nAddresses:");
  214.         } else {
  215.         fprintf(file, "\nAddress:");
  216.         }
  217.         comma = ' ';
  218.         i = 0;
  219.         for (cp = hp->addrList; cp && *cp; cp++) {
  220.         i++;
  221.         if (i > 4) {
  222.             fprintf(file, "\n\t");
  223.             comma = ' ';
  224.             i = 0;
  225.         }
  226.         fprintf(file,"%c %s", comma, inet_ntoa(*(struct in_addr *)*cp));
  227.         comma = ',';
  228.         }
  229.     }
  230.  
  231.     if (hp->aliases != NULL) {
  232.         fprintf(file, "\nAliases:");
  233.         comma = ' ';
  234.         i = 10;
  235.         for (cp = hp->aliases; cp && *cp && **cp; cp++) {
  236.         i += strlen(*cp) + 2;
  237.         if (i > 75) {
  238.             fprintf(file, "\n\t");
  239.             comma = ' ';
  240.             i = 10;
  241.         }
  242.         fprintf(file, "%c %s", comma, *cp);
  243.         comma = ',';
  244.         }
  245.     }
  246.  
  247.     if (hp->servers != NULL) {
  248.         fprintf(file, "\nServed by:\n");
  249.         for (sp = hp->servers; *sp != NULL ; sp++) {
  250.  
  251.         fprintf(file, "- %s\n\t",  (*sp)->name);
  252.  
  253.         comma = ' ';
  254.         i = 0;
  255.         for (cp = (*sp)->addrList; cp && *cp && **cp; cp++) {
  256.             i++;
  257.             if (i > 4) {
  258.             fprintf(file, "\n\t");
  259.             comma = ' ';
  260.             i = 0;
  261.             }
  262.             fprintf(file,
  263.             "%c %s", comma, inet_ntoa(*(struct in_addr *)*cp));
  264.             comma = ',';
  265.         }
  266.         fprintf(file, "\n\t");
  267.  
  268.         comma = ' ';
  269.         i = 10;
  270.         for (cp = (*sp)->domains; cp && *cp && **cp; cp++) {
  271.             i += strlen(*cp) + 2;
  272.             if (i > 75) {
  273.             fprintf(file, "\n\t");
  274.             comma = ' ';
  275.             i = 10;
  276.             }
  277.             fprintf(file, "%c %s", comma, *cp);
  278.             comma = ',';
  279.         }
  280.         fprintf(file, "\n");
  281.         }
  282.     }
  283.  
  284.     fprintf(file, "\n\n");
  285. }
  286.  
  287. /*
  288.  *******************************************************************************
  289.  *
  290.  *  OpenFile --
  291.  *
  292.  *    Parses a command string for a file name and opens
  293.  *    the file.
  294.  *
  295.  *  Results:
  296.  *    file pointer    - the open was successful.
  297.  *    NULL        - there was an error opening the file or
  298.  *              the input string was invalid.
  299.  *
  300.  *******************************************************************************
  301.  */
  302.  
  303. FILE *
  304. OpenFile(string, file)
  305.     char *string;
  306.     char *file;
  307. {
  308.     char    *redirect;
  309.     FILE    *tmpPtr;
  310.  
  311.     /*
  312.      *  Open an output file if we see '>' or >>'.
  313.      *  Check for overwrite (">") or concatenation (">>").
  314.      */
  315.  
  316.     redirect = strchr(string, '>');
  317.     if (redirect == NULL) {
  318.         return(NULL);
  319.     }
  320.     if (redirect[1] == '>') {
  321.         sscanf(redirect, ">> %s", file);
  322.         tmpPtr = fopen(file, "a+");
  323.     } else {
  324.         sscanf(redirect, "> %s", file);
  325.         tmpPtr = fopen(file, "w");
  326.     }
  327.  
  328.     if (tmpPtr != NULL) {
  329.         redirect[0] = '\0';
  330.     }
  331.  
  332.     return(tmpPtr);
  333. }
  334.  
  335. /*
  336.  *******************************************************************************
  337.  *
  338.  *  DecodeError --
  339.  *
  340.  *    Converts an error code into a character string.
  341.  *
  342.  *******************************************************************************
  343.  */
  344.  
  345. char *
  346. DecodeError(result)
  347.     int result;
  348. {
  349.     switch (result) {
  350.         case NOERROR:    return("Success"); break;
  351.         case FORMERR:    return("Format error"); break;
  352.         case SERVFAIL:    return("Server failed"); break;
  353.         case NXDOMAIN:    return("Non-existent domain"); break;
  354.         case NOTIMP:    return("Not implemented"); break;
  355.         case REFUSED:    return("Query refused"); break;
  356.         case NOCHANGE:    return("No change"); break;
  357.         case TIME_OUT:    return("Timed out"); break;
  358.         case NO_INFO:    return("No information"); break;
  359.         case ERROR:        return("Unspecified error"); break;
  360.         case NONAUTH:    return("Non-authoritative answer"); break;
  361.         case NO_RESPONSE:    return("No response from server"); break;
  362.         default:        break;
  363.     }
  364.     return("BAD ERROR VALUE");
  365. }
  366.  
  367.  
  368. int
  369. StringToClass(class, dflt)
  370.     char *class;
  371.     int dflt;
  372. {
  373.     if (strcasecmp(class, "IN") == 0)
  374.         return(C_IN);
  375.     if (strcasecmp(class, "HESIOD") == 0 ||
  376.         strcasecmp(class, "HS") == 0)
  377.         return(C_HS);
  378.     if (strcasecmp(class, "CHAOS") == 0)
  379.         return(C_CHAOS);
  380.     if (strcasecmp(class, "ANY") == 0)
  381.         return(C_ANY);
  382.     fprintf(stderr, "unknown query class: %s\n", class);
  383.     return(dflt);
  384. }
  385.  
  386.  
  387. /*
  388.  *******************************************************************************
  389.  *
  390.  *  StringToType --
  391.  *
  392.  *    Converts a string form of a query type name to its
  393.  *    corresponding integer value.
  394.  *
  395.  *******************************************************************************
  396.  */
  397.  
  398. int
  399. StringToType(type, dflt)
  400.     char *type;
  401.     int dflt;
  402. {
  403.     if (strcasecmp(type, "A") == 0)
  404.         return(T_A);
  405.     if (strcasecmp(type, "NS") == 0)
  406.         return(T_NS);            /* authoritative server */
  407.     if (strcasecmp(type, "MX") == 0)
  408.         return(T_MX);            /* mail exchanger */
  409.     if (strcasecmp(type, "CNAME") == 0)
  410.         return(T_CNAME);        /* canonical name */
  411.     if (strcasecmp(type, "SOA") == 0)
  412.         return(T_SOA);            /* start of authority zone */
  413.     if (strcasecmp(type, "MB") == 0)
  414.         return(T_MB);            /* mailbox domain name */
  415.     if (strcasecmp(type, "MG") == 0)
  416.         return(T_MG);            /* mail group member */
  417.     if (strcasecmp(type, "MR") == 0)
  418.         return(T_MR);            /* mail rename name */
  419.     if (strcasecmp(type, "WKS") == 0)
  420.         return(T_WKS);            /* well known service */
  421.     if (strcasecmp(type, "PTR") == 0)
  422.         return(T_PTR);            /* domain name pointer */
  423.     if (strcasecmp(type, "HINFO") == 0)
  424.         return(T_HINFO);        /* host information */
  425.     if (strcasecmp(type, "MINFO") == 0)
  426.         return(T_MINFO);        /* mailbox information */
  427.     if (strcasecmp(type, "AXFR") == 0)
  428.         return(T_AXFR);            /* zone transfer */
  429.     if (strcasecmp(type, "MAILA") == 0)
  430.         return(T_MAILA);        /* mail agent */
  431.     if (strcasecmp(type, "MAILB") == 0)
  432.         return(T_MAILB);        /* mail box */
  433.     if (strcasecmp(type, "ANY") == 0)
  434.         return(T_ANY);            /* matches any type */
  435.     if (strcasecmp(type, "UINFO") == 0)
  436.         return(T_UINFO);        /* user info */
  437.     if (strcasecmp(type, "UID") == 0)
  438.         return(T_UID);            /* user id */
  439.     if (strcasecmp(type, "GID") == 0)
  440.         return(T_GID);            /* group id */
  441.     if (strcasecmp(type, "TXT") == 0)
  442.         return(T_TXT);            /* text */
  443.     fprintf(stderr, "unknown query type: %s\n", type);
  444.     return(dflt);
  445. }
  446.  
  447. /*
  448.  *******************************************************************************
  449.  *
  450.  *  DecodeType --
  451.  *
  452.  *    Converts a query type to a descriptive name.
  453.  *    (A more verbose form of p_type.)
  454.  *
  455.  *
  456.  *******************************************************************************
  457.  */
  458.  
  459. static  char nbuf[20];
  460.  
  461. char *
  462. DecodeType(type)
  463.     int type;
  464. {
  465.     switch (type) {
  466.     case T_A:
  467.         return("address");
  468.     case T_NS:
  469.         return("name server");
  470.     case T_CNAME:
  471.         return("canonical name");
  472.     case T_SOA:
  473.         return("start of authority");
  474.     case T_MB:
  475.         return("mailbox");
  476.     case T_MG:
  477.         return("mail group member");
  478.     case T_MR:
  479.         return("mail rename");
  480.     case T_NULL:
  481.         return("null");
  482.     case T_WKS:
  483.         return("well-known service");
  484.     case T_PTR:
  485.         return("domain name pointer");
  486.     case T_HINFO:
  487.         return("host information");
  488.     case T_MINFO:
  489.         return("mailbox information");
  490.     case T_MX:
  491.         return("mail exchanger");
  492.     case T_TXT:
  493.         return("text");
  494.     case T_UINFO:
  495.         return("user information");
  496.     case T_UID:
  497.         return("user ID");
  498.     case T_GID:
  499.         return("group ID");
  500.     case T_AXFR:
  501.         return("zone transfer");
  502.     case T_MAILB:
  503.         return("mailbox-related data");
  504.     case T_MAILA:
  505.         return("mail agent");
  506.     case T_ANY:
  507.         return("\"any\"");
  508.     default:
  509.         (void) sprintf(nbuf, "%d", type);
  510.         return (nbuf);
  511.     }
  512. }
  513.