home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / whoiskg1.zip / whois.c < prev    next >
C/C++ Source or Header  |  1998-03-30  |  4KB  |  191 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. #ifdef __EMX__
  14. #define strncasecmp strnicmp
  15. #define  WHOIS_PATH (char *)getenv("ETC")
  16. #define  WHOIS_FILE "\\whohosts.txt"
  17. #else
  18. #define  WHOIS_PATH "/etc/"
  19. #define  WHOIS_FILE "whohosts.txt"
  20. #endif
  21. #define  SX 2048
  22.  
  23.  
  24. char *nichost(char *name);        /* Routine to parse /etc/whohosts */
  25.  
  26. char defhost[SX] = "";            /* Default whois host, or from -h */
  27. char lookuphost[SX] = "";        /* Whois host from /etc/whohosts.txt */
  28. char tld[SX];                        /* TLD component of argument */
  29.  
  30. main(argc, argv)
  31.     int argc;
  32.     char **argv;
  33. {
  34.     extern char *optarg;
  35.     extern int optind;
  36.     register FILE *sfi, *sfo;
  37.     register int ch;
  38.     struct sockaddr_in sin;
  39.     struct hostent *hp;
  40.     struct servent *sp;
  41.     int s;
  42.     char *host;
  43.     
  44.  
  45. #ifdef    SOCKS
  46.     SOCKSinit(argv[0]);
  47. #endif
  48.  
  49.     if (argc == 1)
  50.         usage();
  51.  
  52.     strcpy (defhost, NICHOST);
  53.     host = NICHOST;
  54. ;
  55.     while ((ch = getopt(argc, argv, "h:")) != EOF)
  56.         switch((char)ch) {
  57.         case 'h':
  58.             {
  59.             host = optarg; 
  60.             strcpy (defhost, optarg);
  61.             break;
  62.             }
  63.         case '?':
  64.         default:
  65.             usage();
  66.         }
  67.     argc -= optind;
  68.     argv += optind;
  69.  
  70.     strcpy (lookuphost, nichost(*argv));
  71.     host = lookuphost;
  72.  
  73.     hp = gethostbyname(host);
  74.     if (hp == NULL) {
  75.         (void)fprintf(stderr, "\nNuts, whois: %s: ", host);
  76.         herror((char *)NULL);
  77.         exit(1);
  78.     }
  79.     host = hp->h_name;
  80.     s = socket(hp->h_addrtype, SOCK_STREAM, 0);
  81.     if (s < 0) {
  82.         perror("whois: socket");
  83.         exit(1);
  84.     }
  85.     bzero((caddr_t)&sin, sizeof (sin));
  86.     sin.sin_family = hp->h_addrtype;
  87.     bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
  88.     sp = getservbyname("whois", "tcp");
  89. #ifndef __EMX__
  90.     if (sp == NULL) {
  91.         (void)fprintf(stderr, "whois: whois/tcp: unknown service\n");
  92.         exit(1);
  93.     }
  94. #else
  95.     if (sp == NULL)
  96.         sp = getservbyname("nicname", "tcp");
  97.     if (sp == NULL)
  98.         sin.sin_port = htons(43);
  99.     else
  100. #endif
  101.     sin.sin_port = sp->s_port;
  102.     if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
  103.         perror("whois: connect");
  104.         exit(1);
  105.     }
  106.     sfi = fdopen(s, "r");
  107.     sfo = fdopen(s, "w");
  108.     if (sfi == NULL || sfo == NULL) {
  109.         perror("whois: fdopen");
  110.         (void)close(s);
  111.         exit(1);
  112.     }
  113.     while (argc-- > 1)
  114.         {
  115.         (void)fprintf(sfo, "%s ", *argv++);
  116.         }
  117.     (void)fprintf(sfo, "%s\r\n", *argv);
  118.     (void)fflush(sfo);
  119.     while ((ch = getc(sfi)) != EOF)
  120.         putchar(ch);
  121.     exit(0);
  122. }
  123.  
  124. usage()
  125. {
  126.     (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);
  127.     exit(1);
  128. }
  129.  
  130.  
  131. char *nichost (char *name)
  132.     {
  133.     FILE *hosts;
  134.     char hostsfile[SX];
  135.     static char inbuf[SX];
  136.     char *s;
  137.  
  138.     strcpy (hostsfile, WHOIS_PATH);
  139.     strcat (hostsfile, WHOIS_FILE);
  140.  
  141.     hosts = fopen (hostsfile, "r"); 
  142.  
  143.     if (hosts == (FILE *)NULL)
  144.         {
  145.         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);
  146.  
  147.         return (defhost);
  148.         }
  149.     else
  150.         {
  151.         s = name + strlen (name);
  152.  
  153.         while ((*s != '.') && (s > name))
  154.             {
  155.             s--;
  156.             }
  157.  
  158.         if (s == name)
  159.             {
  160.             /* No TLD found */
  161.             return (defhost);
  162.             }
  163.         else
  164.             {
  165.             /* s points to .dom */
  166.  
  167.             if (*(s+1) != '\0') s++;        /* skip . */
  168.  
  169.             if (fgets (inbuf, SX - 1, hosts) != (char *)NULL)
  170.                 {
  171.                 do
  172.                     {
  173.                     if (strncasecmp (s, inbuf, strlen (s)) == 0)
  174.                         {
  175.                         /* Nuke trailing \n */
  176.  
  177.                         inbuf[strlen (inbuf) -1] = '\0';
  178.  
  179.                         return (inbuf + strlen (s) + 1);
  180.                         }
  181.                     } while (fgets (inbuf, SX - 1, hosts) != (char *)NULL);
  182.                 }
  183.             }
  184.  
  185.         }
  186.  
  187.     return (defhost);
  188.     }
  189.  
  190.     
  191.