home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / n / bind / bind-4.001 / bind-4~ / bind-4.9.3-BETA9 / contrib / rfc1101 / main.c next >
C/C++ Source or Header  |  1993-07-06  |  5KB  |  208 lines

  1. /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
  2.  *    Dep. Matematica Universidade de Coimbra, Portugal, Europe
  3.  */
  4. /*
  5.  * Copyright (c) 1983 Regents of the University of California.
  6.  * All rights reserved.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char    sccsid[] = "@(#)main.c    1.4 (Coimbra) 93/06/03";
  39. #endif /* LIBC_SCCS and not lint */
  40.  
  41. #include <sys/types.h>
  42. #include <netdb.h>
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <sys/socket.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <ctype.h>
  49.  
  50. #include <arpa/nameser.h>
  51. #include <resolv.h>
  52.  
  53. #define    ADDR 1
  54. #define    NAME 2
  55. #define    UNDEF -1
  56.  
  57. int    h_errno;
  58. void    herror();
  59.  
  60. char    *progname;
  61. char    *use = "%s [-d] [-a addr | -n name | string]  \n";
  62. int    name_or_addr = UNDEF;
  63.  
  64. main(argc, argv)
  65. int    argc;
  66. char    *argv[];
  67.     char    **ch, c, *net_name_or_addr, *s;
  68.     struct netent *net_entry;
  69.     register unsigned long    net2, nn;
  70.     unsigned    netbr[4];
  71.  
  72.     progname = argv[0];
  73.     net_name_or_addr = NULL;
  74.  
  75.     while (--argc > 0 && (*++argv)[0] == '-') {
  76.         for (s = argv[0] + 1; *s != '\0'; s++) {
  77.             switch (*s) {
  78.             case 'd':
  79.                 _res.options |= RES_DEBUG;
  80.                 break;
  81.             case 'a':
  82.                 if (--argc > 0) {
  83.                     name_or_addr = ADDR;
  84.                     net_name_or_addr = *++argv;
  85.                 } else 
  86.                     terminate();
  87.                 break;
  88.             case 'n':
  89.                 if (--argc > 0) {
  90.                     name_or_addr = NAME;
  91.                     net_name_or_addr = *++argv;
  92.                 } else 
  93.                     terminate();
  94.                 break;
  95.             default: 
  96.                 terminate();
  97.                 break;
  98.             }
  99.         }
  100.     }
  101.  
  102.     if (name_or_addr == UNDEF) {
  103.         if (argc > 0) {
  104.             c = **argv;
  105.             if (isalpha(c)) {
  106.                 name_or_addr = NAME;
  107.             } else if (isdigit(c)) {
  108.                 name_or_addr = ADDR;
  109.             } else {
  110.                 terminate();
  111.             }
  112.             net_name_or_addr = *argv++;
  113.             --argc;
  114.         } else 
  115.             terminate();
  116.     }
  117.  
  118.     if (argc > 0) {
  119.         fprintf(stderr, "extra arguments ignored:");
  120.         while (argc-- > 0 )
  121.             fprintf(stderr, " %s", *argv++);
  122.         fprintf(stderr, "\n");
  123.     }
  124.  
  125. /*    fprintf(stderr, "name_or_addr = %d, net_name_or_addr = %s\n",
  126.         name_or_addr , net_name_or_addr);
  127. */
  128.     switch (name_or_addr) {
  129.     case ADDR:
  130.         net_entry = getnetbyaddr(inet_network(net_name_or_addr ),
  131.             AF_INET);
  132.         break;
  133.     case NAME:
  134.         net_entry = getnetbyname(net_name_or_addr );
  135.         break;
  136.     default: /* can't happen */
  137.         terminate();
  138.     }
  139.  
  140.     if (net_entry) {
  141.         printf(" Official name: %s \n", net_entry->n_name);
  142.         printf(" Net addr. type: %d \n", net_entry->n_addrtype);
  143.         printf(" Network :  %u ----> ", net2 = (unsigned long)net_entry->n_net);
  144.         for (nn = 4; net2; netbr[--nn] = net2 & 0xff, net2 >>= 8)
  145.             ;
  146.         printf("  ");
  147.         switch (nn) {
  148.         case 0: 
  149.             printf("%u.%u.%u.%u", netbr[0], netbr[1], netbr[2], netbr[3]);
  150.             break;
  151.         case 1: 
  152.             printf("%u.%u.%u", netbr[1], netbr[2], netbr[3]);
  153.             break;
  154.         case 2: 
  155.             printf("%u.%u", netbr[2], netbr[3]);
  156.             break;
  157.         case 3: 
  158.             printf("%u", netbr[3]);
  159.             break;
  160.         }
  161.         printf("  \n");
  162.         printf(" Aliases : \n");
  163.         for (ch = net_entry->n_aliases; *ch != NULL; ch++)
  164.             printf("             %s \n", *ch);
  165.         return 0;
  166.     } else {
  167.         herror(progname);
  168.         return 2;
  169.     }
  170. }
  171.  
  172.  
  173. terminate()
  174. {
  175.     fprintf(stderr, use, progname);
  176.     exit (1);
  177. }
  178.  
  179.  
  180. #ifdef sun
  181. char    *h_errlist[] = {
  182.     "Error 0",
  183.     "Unknown host",             /* 1 HOST_NOT_FOUND */
  184.     "Host name lookup failure",         /* 2 TRY_AGAIN */
  185.     "Unknown server error",         /* 3 NO_RECOVERY */
  186.     "No address associated with name",     /* 4 NO_ADDRESS */
  187. };
  188.  
  189. int    h_nerr = { 
  190.     sizeof(h_errlist) / sizeof(h_errlist[0]) };
  191.  
  192. extern int    h_errno;
  193.  
  194. /*
  195.  * herror --
  196.  *    print the error indicated by the h_errno value.
  197.  */
  198. void
  199. herror(s)
  200. char    *s;
  201. {
  202.     fprintf(stderr, "%s: %s\n", s,
  203.         (u_int) h_errno < h_nerr ?  h_errlist[h_errno] : "Unknown error");
  204. }
  205.  
  206. #endif
  207.