home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / whoiskg1.zip / whois.c.orig < prev    next >
Text File  |  1998-03-30  |  3KB  |  177 lines

  1. /*
  2.  * whois.c, a kinder, gentler whois.
  3.  *
  4.  */
  5.  
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <netdb.h>
  10. #include <stdio.h>
  11.  
  12. #define    NICHOST    "whois.internic.net"
  13. #define  WHOIS_PATH "/etc/"
  14. #define  WHOIS_FILE "whohosts.txt"
  15. #define  SX 2048
  16.  
  17.  
  18. char *nichost(char *name);        /* Routine to parse /etc/whohosts */
  19.  
  20. char defhost[SX] = "";            /* Default whois host, or from -h */
  21. char lookuphost[SX] = "";        /* Whois host from /etc/whohosts.txt */
  22. char tld[SX];                        /* TLD component of argument */
  23.  
  24. main(argc, argv)
  25.     int argc;
  26.     char **argv;
  27. {
  28.     extern char *optarg;
  29.     extern int optind;
  30.     register FILE *sfi, *sfo;
  31.     register int ch;
  32.     struct sockaddr_in sin;
  33.     struct hostent *hp;
  34.     struct servent *sp;
  35.     int s;
  36.     char *host;
  37.     
  38.  
  39. #ifdef    SOCKS
  40.     SOCKSinit(argv[0]);
  41. #endif
  42.  
  43.     if (argc == 1)
  44.         usage();
  45.  
  46.     strcpy (defhost, NICHOST);
  47.     host = NICHOST;
  48. ;
  49.     while ((ch = getopt(argc, argv, "h:")) != EOF)
  50.         switch((char)ch) {
  51.         case 'h':
  52.             {
  53.             host = optarg; 
  54.             strcpy (defhost, optarg);
  55.             break;
  56.             }
  57.         case '?':
  58.         default:
  59.             usage();
  60.         }
  61.     argc -= optind;
  62.     argv += optind;
  63.  
  64.     strcpy (lookuphost, nichost(*argv));
  65.     host = lookuphost;
  66.  
  67.     hp = gethostbyname(host);
  68.     if (hp == NULL) {
  69.         (void)fprintf(stderr, "\nNuts, whois: %s: ", host);
  70.         herror((char *)NULL);
  71.         exit(1);
  72.     }
  73.     host = hp->h_name;
  74.     s = socket(hp->h_addrtype, SOCK_STREAM, 0);
  75.     if (s < 0) {
  76.         perror("whois: socket");
  77.         exit(1);
  78.     }
  79.     bzero((caddr_t)&sin, sizeof (sin));
  80.     sin.sin_family = hp->h_addrtype;
  81.     bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
  82.     sp = getservbyname("whois", "tcp");
  83.     if (sp == NULL) {
  84.         (void)fprintf(stderr, "whois: whois/tcp: unknown service\n");
  85.         exit(1);
  86.     }
  87.     sin.sin_port = sp->s_port;
  88.     if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
  89.         perror("whois: connect");
  90.         exit(1);
  91.     }
  92.     sfi = fdopen(s, "r");
  93.     sfo = fdopen(s, "w");
  94.     if (sfi == NULL || sfo == NULL) {
  95.         perror("whois: fdopen");
  96.         (void)close(s);
  97.         exit(1);
  98.     }
  99.     while (argc-- > 1)
  100.         {
  101.         (void)fprintf(sfo, "%s ", *argv++);
  102.         }
  103.     (void)fprintf(sfo, "%s\r\n", *argv);
  104.     (void)fflush(sfo);
  105.     while ((ch = getc(sfi)) != EOF)
  106.         putchar(ch);
  107.     exit(0);
  108. }
  109.  
  110. usage()
  111. {
  112.     (void)fprintf(stderr, "Usage1: whois [-h whois_server_hostname] name ...\n\nWhois will look in the file %s%s for a list of TLD and IP whois servers.\n\nThe latest copy of %s can be found at:\n\n                   http://dns.vrx.net/tech/rootzone/whohosts.txt\n\n", WHOIS_PATH, WHOIS_FILE, WHOIS_FILE);
  113.     exit(1);
  114. }
  115.  
  116.  
  117. char *nichost (char *name)
  118.     {
  119.     FILE *hosts;
  120.     char hostsfile[SX];
  121.     static char inbuf[SX];
  122.     char *s;
  123.  
  124.     strcpy (hostsfile, WHOIS_PATH);
  125.     strcat (hostsfile, WHOIS_FILE);
  126.  
  127.     hosts = fopen (hostsfile, "r"); 
  128.  
  129.     if (hosts == (FILE *)NULL)
  130.         {
  131.         fprintf (stderr, "\nCan't find whois hosts file as %s\nSee http://dns.vrx.net/tech/rootzone/whohosts.txt to get a copy.\n", hostsfile);
  132.  
  133.         return (defhost);
  134.         }
  135.     else
  136.         {
  137.         s = name + strlen (name);
  138.  
  139.         while ((*s != '.') && (s > name))
  140.             {
  141.             s--;
  142.             }
  143.  
  144.         if (s == name)
  145.             {
  146.             /* No TLD found */
  147.             return (defhost);
  148.             }
  149.         else
  150.             {
  151.             /* s points to .dom */
  152.  
  153.             if (*(s+1) != '\0') s++;        /* skip . */
  154.  
  155.             if (fgets (inbuf, SX - 1, hosts) != (char *)NULL)
  156.                 {
  157.                 do
  158.                     {
  159.                     if (strncasecmp (s, inbuf, strlen (s)) == 0)
  160.                         {
  161.                         /* Nuke trailing \n */
  162.  
  163.                         inbuf[strlen (inbuf) -1] = '\0';
  164.  
  165.                         return (inbuf + strlen (s) + 1);
  166.                         }
  167.                     } while (fgets (inbuf, SX - 1, hosts) != (char *)NULL);
  168.                 }
  169.             }
  170.  
  171.         }
  172.  
  173.     return (defhost);
  174.     }
  175.  
  176.     
  177.