[BACK]Return to whois.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / whois

Diff for /src/usr.bin/whois/whois.c between version 1.10 and 1.11

version 1.10, 1999/11/19 03:57:14 version 1.11, 2000/07/12 18:01:57
Line 76 
Line 76 
 #define WHOIS_QUICK             0x04  #define WHOIS_QUICK             0x04
   
 static void usage       __P((void));  static void usage       __P((void));
 static void whois       __P((char *, struct sockaddr_in *, int));  static void whois       __P((char *, struct addrinfo *, int));
   
 int  int
 main(argc, argv)  main(argc, argv)
         int argc;          int argc;
         char **argv;          char **argv;
 {  {
         int ch, i, j;          int ch, i, j, error;
         int use_qnichost, flags;          int use_qnichost, flags;
         char *host;          char *host;
         char *qnichost;          char *qnichost;
         struct servent *sp;          struct addrinfo hints, *res;
         struct hostent *hp;  
         struct sockaddr_in sin;  
   
 #ifdef SOCKS  #ifdef SOCKS
         SOCKSinit(argv[0]);          SOCKSinit(argv[0]);
Line 144 
Line 142 
         if (!argc)          if (!argc)
                 usage();                  usage();
   
         memset(&sin, 0, sizeof sin);  
         sin.sin_len = sizeof(sin);  
         sin.sin_family = AF_INET;  
         sp = getservbyname("whois", "tcp");  
         if (sp == NULL)  
                 sin.sin_port = htons(WHOIS_PORT);  
         else  
                 sin.sin_port = sp->s_port;  
   
         /*          /*
          * If no nic host is specified, use whois-servers.net           * If no nic host is specified, use whois-servers.net
          * if there is a '.' in the name, else fall back to NICHOST.           * if there is a '.' in the name, else fall back to NICHOST.
Line 179 
Line 168 
                                         err(1, "malloc");                                          err(1, "malloc");
                                 strcpy(qnichost, *argv + j + 1);                                  strcpy(qnichost, *argv + j + 1);
                                 strcat(qnichost, QNICHOST_TAIL);                                  strcat(qnichost, QNICHOST_TAIL);
                                   memset(&hints, 0, sizeof(hints));
                                 if (inet_aton(qnichost, &sin.sin_addr) == 0) {                                  hints.ai_flags = 0;
                                         hp = gethostbyname2(qnichost, AF_INET);                                  hints.ai_family = AF_UNSPEC;
                                         if (hp == NULL) {                                  hints.ai_socktype = SOCK_STREAM;
                                                 free(qnichost);                                  error = getaddrinfo(qnichost, "whois",
                                                 qnichost = NULL;                                                  &hints, &res);
                                         } else {                                  if (error != 0)
                                                 sin.sin_addr = *(struct in_addr *)hp->h_addr_list[0];                                          errx(EX_NOHOST, "%s: %s", qnichost,
                                         }                                                  gai_strerror(error));
                                 }  
                         }                          }
                 }                  }
                 if (!qnichost && inet_aton(host, &sin.sin_addr) == 0) {                  if (!qnichost) {
                         hp = gethostbyname2(host, AF_INET);                          memset(&hints, 0, sizeof(hints));
                         if (hp == NULL)                          hints.ai_flags = 0;
                           hints.ai_family = AF_UNSPEC;
                           hints.ai_socktype = SOCK_STREAM;
                           error = getaddrinfo(host, "whois", &hints, &res);
                           if (error != 0)
                                 errx(EX_NOHOST, "%s: %s", host,                                  errx(EX_NOHOST, "%s: %s", host,
                                     hstrerror(h_errno));                                          gai_strerror(error));
                         host = hp->h_name;  
                         sin.sin_addr = *(struct in_addr *)hp->h_addr_list[0];  
                 }                  }
   
                 whois(*argv++, &sin, flags);                  whois(*argv++, res, flags);
                   freeaddrinfo(res);
         }          }
         exit(0);          exit(0);
 }  }
   
 static void  static void
 whois(name, sinp, flags)  whois(name, res, flags)
         char *name;          char *name;
         struct sockaddr_in *sinp;          struct addrinfo *res;
         int flags;          int flags;
 {  {
         FILE *sfi, *sfo;          FILE *sfi, *sfo;
         char *buf, *p, *nhost;          char *buf, *p, *nhost;
         size_t len;          size_t len;
         int s, nomatch;          int s, nomatch;
           const char *reason = NULL;
   
         s = socket(PF_INET, SOCK_STREAM, 0);          s = -1;
         if (s < 0)          for (/*nothing*/; res; res = res->ai_next) {
                 err(EX_OSERR, "socket");                  s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
                   if (s < 0) {
                           reason = "socket";
                           continue;
                   }
                   if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
                           reason = "connect";
                           close(s);
                           s = -1;
                           continue;
                   }
   
         if (connect(s, (struct sockaddr *)sinp, sizeof(*sinp)) < 0)                  break;  /*okay*/
                 err(EX_OSERR, "connect");          }
           if (s < 0) {
                   if (reason)
                           err(EX_OSERR, "%s", reason);
                   else
                           errx(EX_OSERR, "unknown error in connection attempt");
           }
   
         sfi = fdopen(s, "r");          sfi = fdopen(s, "r");
         sfo = fdopen(s, "w");          sfo = fdopen(s, "w");
Line 263 
Line 271 
                 nhost = INICHOST;                  nhost = INICHOST;
         }          }
         if (nhost) {          if (nhost) {
                 if (inet_aton(nhost, &sinp->sin_addr) == 0) {                  struct addrinfo hints, *res2;
                         struct hostent *hp = gethostbyname2(nhost, AF_INET);                  int error;
                         if (hp == NULL)  
                                 return;                  memset(&hints, 0, sizeof(hints));
                         sinp->sin_addr = *(struct in_addr *)hp->h_addr_list[0];                  hints.ai_flags = 0;
                   hints.ai_family = AF_UNSPEC;
                   hints.ai_socktype = SOCK_STREAM;
                   error = getaddrinfo(nhost, "whois", &hints, &res2);
                   if (error != 0) {
                           warnx("%s: %s", nhost, gai_strerror(error));
                           return;
                 }                  }
                 if (!nomatch)                  if (!nomatch)
                         free(nhost);                          free(nhost);
                 whois(name, sinp, 0);                  whois(name, res2, 0);
                   freeaddrinfo(res2);
         }          }
 }  }
   
Line 280 
Line 295 
 {  {
   
         (void)fprintf(stderr,          (void)fprintf(stderr,
             "usage: whois [-adgimpqQrR] [-h hostname] name ...\n");              "usage: whois [-adgimpqQrR6] [-h hostname] name ...\n");
         exit(EX_USAGE);          exit(EX_USAGE);
 }  }

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.11