Home | History | Annotate | Line # | Download | only in whois
whois.c revision 1.20
      1  1.20  itojun /*	$NetBSD: whois.c,v 1.20 2003/07/14 09:21:03 itojun Exp $	*/
      2   1.5     jtc 
      3   1.1     cgd /*
      4   1.9    tron  * RIPE version marten (at) ripe.net
      5   1.9    tron  * many changes & networkupdate by david (at) ripe.net
      6   1.9    tron  * cosmetics by steven (at) dante.org.uk --	gcc stopped complaining mostly,
      7   1.9    tron  *					code is still messy, though.
      8   1.9    tron  *
      9   1.9    tron  * 1.15 94/09/07
     10   1.9    tron  *
     11   1.9    tron  * 1.2  9705/02
     12   1.9    tron  * "-v" option added; ambrose (at) ripe.net
     13   1.9    tron  * "whois.ripe.net" replaced by "bsdbase.ripe.net";  ambrose (at) ripe.net
     14   1.9    tron  * "bsdbase.ripe.net" replaced by "joshua.ripe.net"; marek (at) ripe.net
     15   1.9    tron  * "joshua.ripe.net" replaced by "whois.ripe.net"; roman (at) ripe.net 981105
     16   1.9    tron  *
     17   1.9    tron  * Copyright (c) 1980 Regents of the University of California.
     18   1.9    tron  * All rights reserved.
     19   1.1     cgd  *
     20   1.1     cgd  * Redistribution and use in source and binary forms, with or without
     21   1.1     cgd  * modification, are permitted provided that the following conditions
     22   1.1     cgd  * are met:
     23   1.1     cgd  * 1. Redistributions of source code must retain the above copyright
     24   1.1     cgd  *    notice, this list of conditions and the following disclaimer.
     25   1.1     cgd  * 2. Redistributions in binary form must reproduce the above copyright
     26   1.1     cgd  *    notice, this list of conditions and the following disclaimer in the
     27   1.1     cgd  *    documentation and/or other materials provided with the distribution.
     28   1.1     cgd  * 3. All advertising materials mentioning features or use of this software
     29   1.1     cgd  *    must display the following acknowledgement:
     30   1.1     cgd  *	This product includes software developed by the University of
     31   1.1     cgd  *	California, Berkeley and its contributors.
     32   1.1     cgd  * 4. Neither the name of the University nor the names of its contributors
     33   1.1     cgd  *    may be used to endorse or promote products derived from this software
     34   1.1     cgd  *    without specific prior written permission.
     35   1.1     cgd  *
     36   1.1     cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     37   1.1     cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     38   1.1     cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     39   1.1     cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     40   1.1     cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     41   1.1     cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     42   1.1     cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     43   1.1     cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     44   1.1     cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     45   1.1     cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     46   1.1     cgd  * SUCH DAMAGE.
     47   1.1     cgd  */
     48   1.1     cgd 
     49   1.6     mrg #include <sys/cdefs.h>
     50   1.9    tron 
     51   1.1     cgd #ifndef lint
     52   1.6     mrg __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
     53   1.6     mrg 	The Regents of the University of California.  All rights reserved.\n");
     54   1.1     cgd #endif /* not lint */
     55   1.1     cgd 
     56   1.9    tron #ifndef RIPE
     57   1.1     cgd #ifndef lint
     58   1.5     jtc #if 0
     59   1.5     jtc static char sccsid[] = "@(#)whois.c	8.1 (Berkeley) 6/6/93";
     60   1.6     mrg #else
     61  1.20  itojun __RCSID("$NetBSD: whois.c,v 1.20 2003/07/14 09:21:03 itojun Exp $");
     62   1.5     jtc #endif
     63   1.1     cgd #endif /* not lint */
     64   1.9    tron #endif /* not RIPE */
     65   1.9    tron 
     66   1.9    tron #ifdef RIPE
     67   1.9    tron #ifndef lint
     68   1.9    tron char sccsid[] =
     69   1.9    tron     "@(#)whois.c 5.11 (Berkeley) 3/2/91 - RIPE 1.15 94/09/07 marten (at) ripe.net";
     70   1.9    tron #endif /* not lint */
     71  1.19     agc #ifndef lint
     72  1.20  itojun __RCSID("$NetBSD: whois.c,v 1.20 2003/07/14 09:21:03 itojun Exp $");
     73  1.19     agc #endif
     74  1.19     agc 
     75   1.9    tron #endif /* RIPE */
     76   1.1     cgd 
     77   1.1     cgd #include <sys/types.h>
     78   1.1     cgd #include <sys/socket.h>
     79   1.9    tron #include <sys/param.h>
     80   1.1     cgd #include <netinet/in.h>
     81   1.7   lukem #include <err.h>
     82   1.1     cgd #include <netdb.h>
     83   1.1     cgd #include <stdio.h>
     84   1.4     jtc #include <stdlib.h>
     85   1.9    tron #include <ctype.h>
     86   1.4     jtc #include <string.h>
     87   1.9    tron #include <errno.h>
     88   1.4     jtc #include <unistd.h>
     89   1.9    tron #include <pwd.h>
     90   1.9    tron #include <signal.h>
     91   1.9    tron 
     92  1.18     wiz #define		index(s,c)	strchr((const char*)(s),(int)(c))
     93  1.18     wiz #define		rindex(s,c)	strrchr((const char*)(s),(int)(c))
     94  1.18     wiz #define		bzero(s,n)	memset((void*)s,0,(size_t)n)
     95   1.9    tron 
     96   1.9    tron #ifdef HASMEMMOVE
     97   1.9    tron # define	bcopy(s,d,n)	memmove((void*)(d),(void*)(s),(size_t)(n))
     98   1.9    tron #else
     99   1.9    tron # define	bcopy(s,d,n)	memcpy((void*)(d),(void*)(s),(size_t)(n))
    100   1.9    tron #endif /* HASMEMMOVE */
    101   1.1     cgd 
    102   1.9    tron /*
    103   1.9    tron 
    104   1.9    tron # the following defines can be used but are not fully functional anymore...
    105   1.9    tron #
    106   1.9    tron # CLEVER- Use a educated guess of the whereabouts of the nearest server
    107   1.9    tron #	  This is done by taking the top-level domain of the current
    108   1.9    tron #	  machine, and looking for a CNAME record for a server with name
    109   1.9    tron #	  <top-level-domain>-whois.ripe.net
    110   1.9    tron #	  If this machine does not exsist or the current machine's top-level
    111   1.9    tron #	  domain could not be found,it will fall back to whois.ripe.net
    112   1.9    tron #	  the default for this RIPE version of whois
    113   1.9    tron #	  The CLEVER option implies the RIPE option.
    114   1.9    tron 
    115   1.9    tron # TOPDOMAIN=\"<top-level-domain>\"
    116   1.9    tron #	- This option will fix the default host to be
    117   1.9    tron #	  <top-level-domain>-whois.ripe.net, which may point to a secondary
    118   1.9    tron #	  server inside your top-level domain. If there is no such secondary
    119   1.9    tron #	  server, it will point to whois.ripe.net, the default. This option
    120   1.9    tron #	  overrules the CLEVER option.
    121   1.9    tron #	  The TOPDOMAIN option implies the RIPE option.
    122   1.9    tron 
    123   1.9    tron */
    124   1.9    tron 
    125   1.9    tron #if defined(TOPDOMAIN) || defined(CLEVER)
    126   1.9    tron #ifndef RIPE
    127   1.9    tron #define RIPE
    128   1.9    tron #endif /* !RIPE */
    129   1.9    tron #endif /* TOPDOMAIN || CLEVER */
    130   1.9    tron 
    131  1.14   jhawk #define NICHOST "whois.internic.net"
    132   1.1     cgd 
    133  1.18     wiz int main(int, char **);
    134  1.18     wiz static void usage(void);
    135  1.18     wiz static void closesocket(int, int);
    136  1.18     wiz static void termhandler(int);
    137   1.9    tron 
    138  1.18     wiz void
    139  1.18     wiz usage(void)
    140   1.9    tron {
    141   1.9    tron #ifdef RIPE
    142   1.9    tron #ifdef NETWORKUPDATE
    143  1.10  itojun   (void)fprintf(stderr, "\nUsage: networkupdate [-46] [-h hostname] [-p port]");
    144   1.9    tron #else
    145  1.10  itojun   (void)fprintf(stderr, "\nUsage: whois [-46aFLmMrSvR] [-h hostname] [-s sources] [-T types] [-i attr] keys\n");
    146   1.9    tron   (void)fprintf(stderr, "       whois -t type");
    147   1.9    tron   (void)fprintf(stderr, "       whois -v type");
    148   1.9    tron #endif
    149   1.9    tron #else
    150  1.10  itojun   (void)fprintf(stderr, "\nUsage: whois [-46] [-h hostname] [-p port] name ...");
    151   1.9    tron #endif
    152   1.9    tron   (void)fprintf(stderr, "\n\nWhere:\n\n");
    153  1.10  itojun   (void)fprintf(stderr, "-4                         Use IPv4 Only\n");
    154  1.10  itojun   (void)fprintf(stderr, "-6                         Use IPv6 Only\n");
    155   1.9    tron #ifdef RIPE
    156   1.9    tron #ifndef NETWORKUPDATE
    157   1.9    tron   (void)fprintf(stderr, "-a                         search all databases\n");
    158   1.9    tron   (void)fprintf(stderr, "-F                         fast raw output\n");
    159   1.9    tron #endif
    160   1.9    tron #endif
    161   1.9    tron   (void)fprintf(stderr, "-h hostname                search alternate server\n");
    162   1.9    tron #ifdef RIPE
    163   1.9    tron #ifndef NETWORKUPDATE
    164   1.9    tron   (void)fprintf(stderr, "-i [attr][[,attr] ... ]    do an inverse lookup for specified attributes\n");
    165   1.9    tron   (void)fprintf(stderr, "-L                         find all Less specific matches\n");
    166   1.9    tron   (void)fprintf(stderr, "-m                         find first level more specific matches\n");
    167   1.9    tron   (void)fprintf(stderr, "-M                         find all More specific matches\n");
    168   1.9    tron #endif
    169   1.9    tron #endif
    170   1.9    tron   (void)fprintf(stderr, "-p port                    port to connect to\n");
    171   1.9    tron #ifdef RIPE
    172   1.9    tron #ifndef NETWORKUPDATE
    173   1.9    tron   (void)fprintf(stderr, "-r                         turn off recursive lookups\n");
    174   1.9    tron   (void)fprintf(stderr, "-s source[[,source] ... ]  search databases with source 'source'\n");
    175   1.9    tron   (void)fprintf(stderr, "-S                         tell server to leave out 'syntactic sugar'\n");
    176   1.9    tron   (void)fprintf(stderr, "-t type                    requests template for object of type 'type'\n");
    177   1.9    tron   (void)fprintf(stderr, "-v type                    requests verbose template for object of type 'type'\n");
    178   1.9    tron   (void)fprintf(stderr, "-R                         force to show local copy of the domain object even if it contains referral\n");
    179   1.9    tron   (void)fprintf(stderr, "-T type[[,type] ... ]      only look for objects of type 'type'\n\n");
    180   1.9    tron   (void)fprintf(stderr, "Please note that most of these flags are NOT understood by\n");
    181   1.9    tron   (void)fprintf(stderr, "non RIPE whois servers\n");
    182   1.9    tron #endif
    183   1.9    tron #endif
    184   1.9    tron   (void)fprintf(stderr, "\n");
    185   1.9    tron 
    186   1.9    tron   exit(1);
    187   1.9    tron }
    188   1.4     jtc 
    189   1.9    tron int s;
    190   1.9    tron 
    191  1.18     wiz void
    192  1.18     wiz closesocket(int s, int child)
    193   1.9    tron {
    194   1.9    tron   /* printf("close connection child=%i\n", child);  */
    195   1.9    tron 
    196   1.9    tron   close(s);
    197   1.9    tron 
    198   1.9    tron #ifdef NETWORKUPDATE
    199   1.9    tron   if (child==0) {
    200   1.9    tron      kill(getppid(), SIGTERM);
    201   1.9    tron   }
    202   1.9    tron #endif
    203   1.9    tron 
    204   1.9    tron   exit(0);
    205   1.9    tron 
    206   1.9    tron }
    207   1.9    tron 
    208  1.18     wiz void
    209  1.18     wiz termhandler(int sig)
    210   1.1     cgd {
    211   1.9    tron   closesocket(s,1);
    212   1.9    tron }
    213   1.9    tron 
    214   1.9    tron 
    215   1.9    tron #ifdef RIPE
    216   1.9    tron #define occurs(str,pat)		((int) strstr((str),(pat)))
    217   1.9    tron #endif
    218   1.1     cgd 
    219  1.18     wiz int
    220  1.18     wiz main(int argc, char *argv[])
    221   1.1     cgd {
    222   1.9    tron   extern char *optarg;
    223   1.9    tron   extern int optind;
    224   1.9    tron   FILE *sfi;
    225   1.9    tron   FILE *sfo;
    226   1.9    tron   int ch;
    227  1.10  itojun   struct addrinfo *dst, hints;
    228  1.11  itojun   int af=PF_UNSPEC;
    229  1.10  itojun   int error;
    230   1.9    tron   char *host, *whoishost;
    231  1.10  itojun   int optp=0;
    232  1.10  itojun   char *optport="whois";
    233  1.10  itojun #ifdef DEBUG
    234  1.10  itojun   int verb=1;
    235  1.10  itojun #else /*DEBUG */
    236  1.10  itojun   int verb=0;
    237  1.10  itojun #endif
    238   1.9    tron #ifdef RIPE
    239  1.10  itojun   int opthost=0;
    240   1.9    tron #ifndef NETWORKUPDATE
    241   1.9    tron   /* normal whois client */
    242   1.9    tron   char *string;
    243   1.9    tron   int alldatabases=0;
    244   1.9    tron   int optsource=0, optrecur=0, optfast=0, opttempl=0, optverbose=0;
    245   1.9    tron   int optobjtype=0, optsugar=0, optinverselookup=0, optgetupdates=0;
    246   1.9    tron   int optL=0, optm=0, optM=0, optchanged=0, optnonreferral=0;
    247   1.9    tron   char	*source=NULL, *templ=NULL, *verbose=NULL, *objtype=NULL,
    248   1.9    tron 	*inverselookup=NULL, *getupdates=NULL;
    249   1.9    tron #else /* NETWORKUPDATE */
    250   1.9    tron   /* networkupdate client */
    251   1.9    tron   int prev;
    252   1.9    tron   char domainname[64]; /* that's what sys/param.h says */
    253   1.9    tron   struct passwd *passwdentry;
    254   1.9    tron   int child;
    255   1.9    tron #endif
    256   1.9    tron #ifdef CLEVER
    257   1.9    tron   int  myerror;
    258   1.9    tron   char *mytoplevel;
    259   1.9    tron   char *myhost;
    260   1.9    tron #endif
    261   1.9    tron #endif
    262   1.9    tron 
    263   1.9    tron #ifdef TOPDOMAIN
    264  1.16    joda   char topdomain[] = TOPDOMAIN "-whois.ripe.net";
    265  1.16    joda   host = topdomain;
    266   1.9    tron #else
    267   1.9    tron   host = NICHOST;
    268   1.9    tron #endif
    269   1.9    tron 
    270   1.9    tron #ifdef RIPE
    271   1.9    tron #ifdef NETWORKUPDATE
    272  1.15     mjl     while ((ch = getopt(argc, argv, "46h:p:")) != -1)
    273   1.9    tron #else
    274  1.15     mjl   while ((ch = getopt(argc, argv, "46acFg:h:i:LmMp:rs:SRt:T:v:")) != -1)
    275   1.9    tron #endif
    276   1.9    tron #else
    277  1.15     mjl     while ((ch = getopt(argc, argv, "46h:p:")) != -1)
    278   1.9    tron #endif
    279   1.9    tron       switch((char)ch) {
    280  1.10  itojun       case '4':
    281  1.11  itojun 	af = PF_INET;
    282  1.10  itojun 	break;
    283  1.10  itojun       case '6':
    284  1.11  itojun 	af = PF_INET6;
    285  1.10  itojun 	break;
    286   1.9    tron       case 'h':
    287   1.9    tron 	host = optarg;
    288   1.9    tron 	opthost = 1;
    289   1.9    tron 	break;
    290   1.9    tron       case 'p':
    291  1.10  itojun 	optport=optarg;
    292   1.9    tron         optp =1;
    293   1.9    tron         break;
    294   1.9    tron #ifdef RIPE
    295   1.9    tron #ifndef NETWORKUPDATE
    296   1.9    tron       case 'a':
    297   1.9    tron 	alldatabases=1;
    298   1.9    tron 	break;
    299   1.9    tron       case 'c':
    300   1.9    tron         optchanged=1;
    301   1.9    tron         break;
    302   1.9    tron       case 'F':
    303   1.9    tron 	optfast = 1;
    304   1.9    tron 	break;
    305   1.9    tron       case 'g':
    306   1.9    tron         getupdates=optarg;
    307   1.9    tron 	optgetupdates=1;
    308   1.9    tron 	break;
    309   1.9    tron       case 'i':
    310   1.9    tron         inverselookup=optarg;
    311   1.9    tron 	optinverselookup = 1;
    312   1.9    tron 	break;
    313   1.9    tron       case 'L':
    314   1.9    tron 	if (optM || optm) {
    315   1.9    tron           fprintf(stderr, "Only one of -L, -m or -M allowed\n\n");
    316   1.9    tron           usage();
    317   1.9    tron         }
    318   1.9    tron 	optL=1;
    319   1.9    tron 	break;
    320   1.9    tron       case 'm':
    321   1.9    tron 	if (optM || optL) {
    322   1.9    tron 	  fprintf(stderr, "Only one of -L, -m or -M allowed\n\n");
    323   1.9    tron           usage();
    324   1.9    tron 	}
    325   1.9    tron 	optm=1;
    326   1.9    tron 	break;
    327   1.9    tron       case 'M':
    328   1.9    tron 	if (optL || optm) {
    329   1.9    tron 	  fprintf(stderr, "Only one of -L, -m or -M allowed\n\n");
    330   1.9    tron 	  usage();
    331   1.9    tron 	}
    332   1.9    tron 	optM=1;
    333   1.9    tron 	break;
    334   1.9    tron 
    335   1.9    tron       case 's':
    336   1.9    tron 	source = optarg;
    337   1.9    tron 	optsource=1;
    338   1.9    tron 	break;
    339   1.9    tron       case 'S':
    340   1.9    tron 	optsugar=1;
    341   1.9    tron 	break;
    342   1.9    tron       case 'R':
    343   1.9    tron 	optnonreferral=1;
    344   1.9    tron 	break;
    345   1.9    tron       case 'r':
    346   1.9    tron 	optrecur=1;
    347   1.9    tron 	break;
    348   1.9    tron       case 't':
    349   1.9    tron 	 templ=optarg;
    350   1.9    tron 	 opttempl=1;
    351   1.9    tron 	 break;
    352   1.9    tron       case 'v':
    353   1.9    tron 	 verbose=optarg;
    354   1.9    tron 	 optverbose=1;
    355   1.9    tron 	 break;
    356   1.9    tron       case 'T':
    357   1.9    tron 	objtype=optarg;
    358   1.9    tron 	optobjtype=1;
    359   1.9    tron 	break;
    360   1.9    tron 
    361   1.9    tron #endif
    362   1.9    tron #endif
    363   1.9    tron       case '?':
    364   1.9    tron       default:
    365   1.9    tron 	usage();
    366   1.9    tron       }
    367   1.9    tron   argc -= optind;
    368   1.9    tron   argv += optind;
    369   1.9    tron 
    370   1.9    tron #ifdef RIPE
    371   1.9    tron #ifdef NETWORKUPDATE
    372   1.9    tron   if (argc>0)
    373   1.9    tron     usage();
    374   1.9    tron #else
    375   1.9    tron   if ((argc<=0) && !opttempl && !optverbose && !optgetupdates && (!(opttempl && optgetupdates)))
    376   1.9    tron     usage();
    377   1.9    tron #endif
    378   1.9    tron #else
    379   1.9    tron   if (argc<=0)
    380   1.9    tron     usage();
    381   1.9    tron #endif
    382   1.9    tron 
    383   1.9    tron   if (!opthost) {
    384   1.9    tron 
    385   1.9    tron #ifdef CLEVER
    386   1.9    tron     whoishost=(char *)calloc(MAXHOSTNAMELEN, sizeof(char));
    387  1.20  itojun     if (!whoishost)
    388  1.20  itojun       err(1, "malloc");
    389   1.9    tron     myhost =(char *)calloc(MAXHOSTNAMELEN, sizeof(char));
    390  1.20  itojun     if (!myhost)
    391  1.20  itojun       err(1, "malloc");
    392   1.9    tron     myerror = gethostname(myhost, MAXHOSTNAMELEN);
    393   1.9    tron     if (myerror >= 0) {
    394   1.9    tron       if (occurs(myhost, ".")) {
    395   1.9    tron 	mytoplevel = rindex(myhost,'.');
    396   1.9    tron 	mytoplevel++;
    397  1.20  itojun 	(void) snprintf(whoishost, MAXHOSTNAMELEN, "%s-whois.ripe.net",
    398  1.20  itojun 	    mytoplevel);
    399   1.9    tron 	if (verb) fprintf(stderr, "Clever guess: %s\n", whoishost);
    400   1.9    tron       }
    401   1.9    tron     }
    402  1.10  itojun 
    403  1.10  itojun     memset(&hints, 0, sizeof(hints));
    404  1.10  itojun     hints.ai_flags = AI_CANONNAME;
    405  1.11  itojun     hints.ai_family = af;
    406  1.10  itojun     hints.ai_socktype = SOCK_STREAM;
    407  1.10  itojun     hints.ai_protocol = 0;
    408  1.10  itojun     error = getaddrinfo(host, optport, &hints, &dst);
    409  1.10  itojun     if ((error) && (verb))
    410   1.9    tron       fprintf(stderr,"No such host: %s\n", whoishost);
    411  1.10  itojun     if (error) {
    412   1.9    tron #endif
    413   1.9    tron 
    414   1.9    tron       whoishost=NICHOST;
    415   1.9    tron 
    416  1.11  itojun       if (verb)
    417  1.11  itojun 	fprintf(stderr, "Default host: %s\n\n", whoishost);
    418  1.10  itojun       memset(&hints, 0, sizeof(hints));
    419  1.10  itojun       hints.ai_flags = AI_CANONNAME;
    420  1.11  itojun       hints.ai_family = af;
    421  1.10  itojun       hints.ai_socktype = SOCK_STREAM;
    422  1.10  itojun       hints.ai_protocol = 0;
    423  1.10  itojun       error = getaddrinfo(host, optport , &hints, &dst);
    424  1.10  itojun       if (error) {
    425   1.9    tron 	fprintf(stderr,"No such host: %s\n", whoishost);
    426   1.9    tron 	if (verb) fprintf(stderr, "Now I give up ...\n");
    427   1.9    tron 	perror("Unknown host");
    428  1.10  itojun 	exit(1);
    429   1.9    tron       }
    430   1.9    tron 
    431   1.9    tron #ifdef CLEVER
    432   1.9    tron     }
    433   1.9    tron #endif
    434   1.9    tron   }
    435   1.9    tron   else {
    436  1.11  itojun     if (verb)
    437  1.11  itojun       fprintf(stderr, "Trying: %s\n\n", host);
    438  1.10  itojun     memset(&hints, 0, sizeof(hints));
    439  1.10  itojun     hints.ai_flags = AI_CANONNAME;
    440  1.11  itojun     hints.ai_family = af;
    441  1.10  itojun     hints.ai_socktype = SOCK_STREAM;
    442  1.10  itojun     hints.ai_protocol = 0;
    443  1.10  itojun     error = getaddrinfo(host, optport, &hints, &dst);
    444  1.10  itojun     if (error) {
    445   1.9    tron       (void)fprintf(stderr, "whois: %s: ", host);
    446   1.9    tron       perror("Unknown host");
    447   1.9    tron       exit(1);
    448   1.9    tron     }
    449   1.9    tron   }
    450   1.9    tron 
    451  1.11  itojun   for (/*nothing*/; dst; dst = dst->ai_next) {
    452  1.10  itojun     s = socket(dst->ai_family, dst->ai_socktype, dst->ai_protocol);
    453  1.11  itojun     if (s < 0)
    454  1.11  itojun       continue;
    455  1.10  itojun     if (connect(s, dst->ai_addr, dst->ai_addrlen) < 0) {
    456  1.11  itojun       close(s);
    457  1.10  itojun       if (verb) (void)fprintf(stderr, "whois: connect miss\n");
    458  1.10  itojun       continue;
    459  1.10  itojun     }
    460  1.11  itojun     /*okay*/
    461  1.10  itojun     break;
    462  1.11  itojun   }
    463  1.11  itojun   if (dst == NULL) {
    464  1.11  itojun     perror("whois: connect");
    465  1.11  itojun     exit(1);
    466   1.9    tron   }
    467  1.10  itojun   if (verb) (void)fprintf(stderr, "whois: connect success\n");
    468   1.9    tron 
    469   1.9    tron #ifndef NETWORKUPDATE
    470   1.9    tron   sfi = fdopen(s, "r");
    471   1.9    tron   sfo = fdopen(s, "w");
    472   1.9    tron   if (sfi == NULL || sfo == NULL) {
    473   1.9    tron     perror("whois: fdopen");
    474   1.9    tron     (void)close(s);
    475   1.9    tron     exit(1);
    476   1.9    tron   }
    477   1.9    tron #endif
    478   1.9    tron 
    479   1.9    tron   signal(SIGTERM, termhandler);
    480   1.9    tron 
    481   1.9    tron #ifdef RIPE
    482   1.9    tron #ifdef NETWORKUPDATE
    483   1.9    tron 
    484   1.9    tron   if ((child=fork())==0) {
    485   1.9    tron 
    486   1.9    tron      sfo = fdopen(s, "w");
    487   1.9    tron      if (sfo == NULL) {
    488   1.9    tron        perror("whois: fdopen");
    489   1.9    tron        (void)close(s);
    490   1.9    tron        exit(1);
    491   1.9    tron      }
    492   1.9    tron 
    493   1.9    tron      if (gethostname(domainname, sizeof(domainname))) {
    494   1.9    tron         fprintf(stderr, "error when doing gethostname()");
    495  1.17     wiz         exit(1);
    496   1.9    tron      }
    497   1.9    tron 
    498   1.9    tron      passwdentry=getpwuid(getuid());
    499   1.9    tron 
    500   1.9    tron      fprintf(sfo, "-Vnc2.0 -U %s %s\n", passwdentry->pw_name, domainname);
    501   1.9    tron      fflush(sfo);
    502   1.9    tron 
    503   1.9    tron      prev='\0';
    504   1.9    tron 
    505   1.9    tron      while ((ch=getchar()) != EOF) {
    506   1.9    tron 
    507   1.9    tron         fputc(ch, sfo);
    508   1.9    tron 
    509   1.9    tron         if (ch=='\n') fflush(sfo);
    510   1.9    tron         if (feof(sfo)) closesocket(s, child);
    511   1.9    tron         if ((ch=='.') && (prev=='\n')) closesocket(s, child);
    512   1.9    tron         if (!isspace(ch) || ((!isspace(prev)) && (ch=='\n'))) prev=ch;
    513   1.9    tron      }
    514   1.9    tron 
    515   1.9    tron      closesocket(s, child);
    516   1.9    tron 
    517   1.9    tron   }
    518   1.9    tron 
    519   1.9    tron   sfi = fdopen(s, "r");
    520   1.9    tron   if (sfi == NULL) {
    521   1.9    tron        perror("whois: fdopen");
    522   1.9    tron        (void)close(s);
    523   1.9    tron        exit(1);
    524   1.9    tron   }
    525   1.9    tron 
    526   1.9    tron #else
    527   1.9    tron 
    528   1.9    tron   if (alldatabases)
    529   1.9    tron     (void)fprintf(sfo, "-a ");
    530   1.9    tron   if (optchanged)
    531   1.9    tron     (void)fprintf(sfo, "-c ");
    532   1.9    tron   if (optfast)
    533   1.9    tron     (void)fprintf(sfo, "-F ");
    534   1.9    tron   if (optgetupdates)
    535   1.9    tron     (void)fprintf(sfo, "-g %s ", getupdates);
    536   1.9    tron   if (optinverselookup)
    537   1.9    tron     (void)fprintf(sfo, "-i %s ", inverselookup);
    538   1.9    tron   if (optL)
    539   1.9    tron     (void)fprintf(sfo, "-L ");
    540   1.9    tron   if (optm)
    541   1.9    tron     (void)fprintf(sfo, "-m ");
    542   1.9    tron   if (optM)
    543   1.9    tron     (void)fprintf(sfo, "-M ");
    544   1.9    tron   if (optrecur)
    545   1.9    tron     (void)fprintf(sfo, "-r ");
    546   1.9    tron   if (optsource)
    547   1.9    tron     (void)fprintf(sfo, "-s %s ", source);
    548   1.9    tron   if (optsugar)
    549   1.9    tron     (void)fprintf(sfo, "-S ");
    550   1.9    tron   if (optnonreferral)
    551   1.9    tron     (void)fprintf(sfo, "-R ");
    552   1.9    tron   if (opttempl)
    553   1.9    tron     (void)fprintf(sfo, "-t %s ", templ);
    554   1.9    tron   if (optverbose)
    555   1.9    tron     (void)fprintf(sfo, "-v %s ", verbose);
    556   1.9    tron   if (optobjtype)
    557   1.9    tron     (void)fprintf(sfo, "-T %s ", objtype);
    558   1.9    tron 
    559   1.9    tron   /* we can only send the -V when we are sure that we are dealing with
    560   1.9    tron      a RIPE whois server :-( */
    561   1.9    tron 
    562  1.20  itojun   whoishost = strdup(host);
    563  1.20  itojun   if (!whoishost)
    564  1.20  itojun     err(1, "malloc");
    565   1.9    tron   for (string=whoishost;(*string=(char)tolower(*string));string++);
    566   1.9    tron 
    567   1.9    tron   if (strstr(whoishost, "ripe.net") ||
    568   1.9    tron       strstr(whoishost, "ra.net") ||
    569   1.9    tron       strstr(whoishost, "apnic.net") ||
    570   1.9    tron       strstr(whoishost, "mci.net") ||
    571   1.9    tron       strstr(whoishost, "isi.edu") ||
    572   1.9    tron       strstr(whoishost, "garr.it") ||
    573   1.9    tron       strstr(whoishost, "ans.net") ||
    574   1.9    tron       alldatabases || optfast || optgetupdates || optinverselookup ||
    575   1.9    tron       optL || optm || optM || optrecur || optsugar || optsource ||
    576   1.9    tron       opttempl || optverbose || optobjtype)
    577   1.9    tron     (void)fprintf(sfo, "-VwC2.0 ");
    578   1.9    tron #endif
    579   1.9    tron #endif
    580   1.9    tron 
    581   1.9    tron #ifndef NETWORKUPDATE
    582   1.9    tron   while (argc-- > 1)
    583   1.9    tron     (void)fprintf(sfo, "%s ", *argv++);
    584   1.9    tron    if (*argv) (void)fputs(*argv, sfo);
    585   1.9    tron    (void)fputs("\r\n", sfo);
    586   1.9    tron   (void)fflush(sfo);
    587   1.9    tron #endif
    588   1.9    tron 
    589   1.9    tron   while ((ch = getc(sfi)) != EOF)
    590   1.9    tron     putchar(ch);
    591   1.9    tron 
    592   1.9    tron   closesocket(s, 1);
    593   1.9    tron 
    594   1.9    tron   exit(0);
    595   1.1     cgd }
    596