Home | History | Annotate | Line # | Download | only in whois
whois.c revision 1.10
      1  1.10  itojun /*	$NetBSD: whois.c,v 1.10 1999/09/03 13:51:28 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 #if defined(sun) && defined(solaris)
     51   1.9    tron #define SYSV
     52   1.9    tron #endif
     53   1.9    tron 
     54   1.1     cgd #ifndef lint
     55   1.6     mrg __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
     56   1.6     mrg 	The Regents of the University of California.  All rights reserved.\n");
     57   1.1     cgd #endif /* not lint */
     58   1.1     cgd 
     59   1.9    tron #ifndef RIPE
     60   1.1     cgd #ifndef lint
     61   1.5     jtc #if 0
     62   1.5     jtc static char sccsid[] = "@(#)whois.c	8.1 (Berkeley) 6/6/93";
     63   1.6     mrg #else
     64  1.10  itojun __RCSID("$NetBSD: whois.c,v 1.10 1999/09/03 13:51:28 itojun Exp $");
     65   1.5     jtc #endif
     66   1.1     cgd #endif /* not lint */
     67   1.9    tron #endif /* not RIPE */
     68   1.9    tron 
     69   1.9    tron #ifdef RIPE
     70   1.9    tron #ifndef lint
     71   1.9    tron char sccsid[] =
     72   1.9    tron     "@(#)whois.c 5.11 (Berkeley) 3/2/91 - RIPE 1.15 94/09/07 marten (at) ripe.net";
     73   1.9    tron #endif /* not lint */
     74   1.9    tron #endif /* RIPE */
     75   1.1     cgd 
     76   1.1     cgd #include <sys/types.h>
     77   1.1     cgd #include <sys/socket.h>
     78   1.9    tron #include <sys/param.h>
     79   1.1     cgd #include <netinet/in.h>
     80   1.7   lukem #include <err.h>
     81   1.1     cgd #include <netdb.h>
     82   1.1     cgd #include <stdio.h>
     83   1.4     jtc #include <stdlib.h>
     84   1.9    tron #include <ctype.h>
     85   1.4     jtc #include <string.h>
     86   1.9    tron #include <errno.h>
     87   1.4     jtc #include <unistd.h>
     88   1.9    tron #include <pwd.h>
     89   1.9    tron #include <signal.h>
     90   1.9    tron 
     91   1.9    tron #if defined(SYSV)
     92   1.9    tron #include	<crypt.h>
     93   1.9    tron #endif /* SYSV */
     94   1.9    tron 
     95   1.9    tron #ifndef __NetBSD__
     96   1.9    tron #ifdef __STDC__
     97   1.9    tron extern int	getopt(int argc, char * const *argv, const char *optstring);
     98   1.9    tron extern int	kill(pid_t pid, int sig);
     99   1.9    tron extern FILE	*fdopen(int fildes, const char *type);
    100   1.9    tron extern int	gethostname(char *name, int namelen);
    101   1.9    tron #else /* !__STDC__ */
    102   1.9    tron extern int	gethostname();
    103   1.9    tron #endif /* __STDC__ */
    104   1.9    tron #endif /* __NetBSD__ */
    105   1.9    tron 
    106   1.9    tron #if defined(SYSV) || defined(__STDC__)
    107   1.9    tron 
    108   1.9    tron #define		index(s,c)		strchr((const char*)(s),(int)(c))
    109   1.9    tron #define		rindex(s,c)		strrchr((const char*)(s),(int)(c))
    110   1.9    tron #define		bzero(s,n)		memset((void*)s,0,(size_t)n)
    111   1.9    tron 
    112   1.9    tron #ifdef HASMEMMOVE
    113   1.9    tron # define	bcopy(s,d,n)	memmove((void*)(d),(void*)(s),(size_t)(n))
    114   1.9    tron #else
    115   1.9    tron # define	bcopy(s,d,n)	memcpy((void*)(d),(void*)(s),(size_t)(n))
    116   1.9    tron #endif /* HASMEMMOVE */
    117   1.1     cgd 
    118   1.9    tron #endif /* SYSV || __STDC__ */
    119   1.9    tron 
    120   1.9    tron #ifdef GLIBC
    121   1.9    tron typedef __u_short u_short;
    122   1.9    tron typedef __caddr_t caddr_t;
    123   1.9    tron #endif /* GLIBC */
    124   1.9    tron 
    125   1.9    tron /*
    126   1.9    tron 
    127   1.9    tron # the following defines can be used but are not fully functional anymore...
    128   1.9    tron #
    129   1.9    tron # CLEVER- Use a educated guess of the whereabouts of the nearest server
    130   1.9    tron #	  This is done by taking the top-level domain of the current
    131   1.9    tron #	  machine, and looking for a CNAME record for a server with name
    132   1.9    tron #	  <top-level-domain>-whois.ripe.net
    133   1.9    tron #	  If this machine does not exsist or the current machine's top-level
    134   1.9    tron #	  domain could not be found,it will fall back to whois.ripe.net
    135   1.9    tron #	  the default for this RIPE version of whois
    136   1.9    tron #	  The CLEVER option implies the RIPE option.
    137   1.9    tron 
    138   1.9    tron # TOPDOMAIN=\"<top-level-domain>\"
    139   1.9    tron #	- This option will fix the default host to be
    140   1.9    tron #	  <top-level-domain>-whois.ripe.net, which may point to a secondary
    141   1.9    tron #	  server inside your top-level domain. If there is no such secondary
    142   1.9    tron #	  server, it will point to whois.ripe.net, the default. This option
    143   1.9    tron #	  overrules the CLEVER option.
    144   1.9    tron #	  The TOPDOMAIN option implies the RIPE option.
    145   1.9    tron 
    146   1.9    tron */
    147   1.9    tron 
    148   1.9    tron #if defined(TOPDOMAIN) || defined(CLEVER)
    149   1.9    tron #ifndef RIPE
    150   1.9    tron #define RIPE
    151   1.9    tron #endif /* !RIPE */
    152   1.9    tron #endif /* TOPDOMAIN || CLEVER */
    153   1.9    tron 
    154   1.9    tron #if defined(RIPE) && !defined(__NetBSD__)
    155   1.9    tron #include <sys/param.h>
    156   1.9    tron #define NICHOST "whois.ripe.net"
    157   1.9    tron #else
    158   1.9    tron #define NICHOST "whois.internic.net"
    159   1.9    tron #endif
    160   1.1     cgd 
    161   1.6     mrg int main __P((int, char **));
    162   1.6     mrg static void usage __P((void));
    163   1.9    tron static void closesocket __P((int, int));
    164   1.9    tron static void termhandler __P((int));
    165   1.9    tron 
    166   1.9    tron void usage()
    167   1.9    tron {
    168   1.9    tron #ifdef RIPE
    169   1.9    tron #ifdef NETWORKUPDATE
    170  1.10  itojun   (void)fprintf(stderr, "\nUsage: networkupdate [-46] [-h hostname] [-p port]");
    171   1.9    tron #else
    172  1.10  itojun   (void)fprintf(stderr, "\nUsage: whois [-46aFLmMrSvR] [-h hostname] [-s sources] [-T types] [-i attr] keys\n");
    173   1.9    tron   (void)fprintf(stderr, "       whois -t type");
    174   1.9    tron   (void)fprintf(stderr, "       whois -v type");
    175   1.9    tron #endif
    176   1.9    tron #else
    177  1.10  itojun   (void)fprintf(stderr, "\nUsage: whois [-46] [-h hostname] [-p port] name ...");
    178   1.9    tron #endif
    179   1.9    tron   (void)fprintf(stderr, "\n\nWhere:\n\n");
    180  1.10  itojun   (void)fprintf(stderr, "-4                         Use IPv4 Only\n");
    181  1.10  itojun   (void)fprintf(stderr, "-6                         Use IPv6 Only\n");
    182   1.9    tron #ifdef RIPE
    183   1.9    tron #ifndef NETWORKUPDATE
    184   1.9    tron   (void)fprintf(stderr, "-a                         search all databases\n");
    185   1.9    tron   (void)fprintf(stderr, "-F                         fast raw output\n");
    186   1.9    tron #endif
    187   1.9    tron #endif
    188   1.9    tron   (void)fprintf(stderr, "-h hostname                search alternate server\n");
    189   1.9    tron #ifdef RIPE
    190   1.9    tron #ifndef NETWORKUPDATE
    191   1.9    tron   (void)fprintf(stderr, "-i [attr][[,attr] ... ]    do an inverse lookup for specified attributes\n");
    192   1.9    tron   (void)fprintf(stderr, "-L                         find all Less specific matches\n");
    193   1.9    tron   (void)fprintf(stderr, "-m                         find first level more specific matches\n");
    194   1.9    tron   (void)fprintf(stderr, "-M                         find all More specific matches\n");
    195   1.9    tron #endif
    196   1.9    tron #endif
    197   1.9    tron   (void)fprintf(stderr, "-p port                    port to connect to\n");
    198   1.9    tron #ifdef RIPE
    199   1.9    tron #ifndef NETWORKUPDATE
    200   1.9    tron   (void)fprintf(stderr, "-r                         turn off recursive lookups\n");
    201   1.9    tron   (void)fprintf(stderr, "-s source[[,source] ... ]  search databases with source 'source'\n");
    202   1.9    tron   (void)fprintf(stderr, "-S                         tell server to leave out 'syntactic sugar'\n");
    203   1.9    tron   (void)fprintf(stderr, "-t type                    requests template for object of type 'type'\n");
    204   1.9    tron   (void)fprintf(stderr, "-v type                    requests verbose template for object of type 'type'\n");
    205   1.9    tron   (void)fprintf(stderr, "-R                         force to show local copy of the domain object even if it contains referral\n");
    206   1.9    tron   (void)fprintf(stderr, "-T type[[,type] ... ]      only look for objects of type 'type'\n\n");
    207   1.9    tron   (void)fprintf(stderr, "Please note that most of these flags are NOT understood by\n");
    208   1.9    tron   (void)fprintf(stderr, "non RIPE whois servers\n");
    209   1.9    tron #endif
    210   1.9    tron #endif
    211   1.9    tron   (void)fprintf(stderr, "\n");
    212   1.9    tron 
    213   1.9    tron   exit(1);
    214   1.9    tron }
    215   1.4     jtc 
    216   1.9    tron int s;
    217   1.9    tron 
    218   1.9    tron void closesocket(s, child)
    219   1.9    tron int s, child;
    220   1.9    tron {
    221   1.9    tron   /* printf("close connection child=%i\n", child);  */
    222   1.9    tron 
    223   1.9    tron   close(s);
    224   1.9    tron 
    225   1.9    tron #ifdef NETWORKUPDATE
    226   1.9    tron   if (child==0) {
    227   1.9    tron      kill(getppid(), SIGTERM);
    228   1.9    tron   }
    229   1.9    tron #endif
    230   1.9    tron 
    231   1.9    tron   exit(0);
    232   1.9    tron 
    233   1.9    tron }
    234   1.9    tron 
    235   1.9    tron void termhandler(sig)
    236   1.9    tron int sig;
    237   1.1     cgd {
    238   1.9    tron   closesocket(s,1);
    239   1.9    tron }
    240   1.9    tron 
    241   1.9    tron 
    242   1.9    tron #ifdef RIPE
    243   1.9    tron #if defined(__STDC__) || defined(SYSV)
    244   1.9    tron #define occurs(str,pat)		((int) strstr((str),(pat)))
    245   1.9    tron #else /* !__STDC__ && !SYSV */
    246   1.9    tron int occurs(str, pat)
    247   1.9    tron      char *str, *pat;
    248   1.9    tron {
    249   1.9    tron   register char *point = str;
    250   1.9    tron 
    251   1.9    tron   while ((point=index(point, *pat)))
    252   1.9    tron     {
    253   1.9    tron       if (strncmp(point, pat, strlen(pat)) == 0)
    254   1.9    tron 	return(1);
    255   1.9    tron       point++;
    256   1.9    tron     }
    257   1.9    tron   return(0);
    258   1.1     cgd }
    259   1.9    tron #endif
    260   1.9    tron #endif
    261   1.1     cgd 
    262   1.9    tron int main(argc, argv)
    263   1.9    tron      int argc;
    264   1.9    tron      char **argv;
    265   1.1     cgd {
    266   1.9    tron   extern char *optarg;
    267   1.9    tron   extern int optind;
    268   1.9    tron   FILE *sfi;
    269   1.9    tron   FILE *sfo;
    270   1.9    tron   int ch;
    271  1.10  itojun   struct addrinfo *dst, hints;
    272  1.10  itojun   int socktype=PF_UNSPEC;
    273  1.10  itojun   int error;
    274   1.9    tron   char *host, *whoishost;
    275  1.10  itojun   int optp=0;
    276  1.10  itojun   char *optport="whois";
    277  1.10  itojun #ifdef DEBUG
    278  1.10  itojun   int verb=1;
    279  1.10  itojun #else /*DEBUG */
    280  1.10  itojun   int verb=0;
    281  1.10  itojun #endif
    282   1.9    tron #ifdef RIPE
    283  1.10  itojun   int opthost=0;
    284   1.9    tron #ifndef NETWORKUPDATE
    285   1.9    tron   /* normal whois client */
    286   1.9    tron   char *string;
    287   1.9    tron   int alldatabases=0;
    288   1.9    tron   int optsource=0, optrecur=0, optfast=0, opttempl=0, optverbose=0;
    289   1.9    tron   int optobjtype=0, optsugar=0, optinverselookup=0, optgetupdates=0;
    290   1.9    tron   int optL=0, optm=0, optM=0, optchanged=0, optnonreferral=0;
    291   1.9    tron   char	*source=NULL, *templ=NULL, *verbose=NULL, *objtype=NULL,
    292   1.9    tron 	*inverselookup=NULL, *getupdates=NULL;
    293   1.9    tron #else /* NETWORKUPDATE */
    294   1.9    tron   /* networkupdate client */
    295   1.9    tron   int prev;
    296   1.9    tron   char domainname[64]; /* that's what sys/param.h says */
    297   1.9    tron   struct passwd *passwdentry;
    298   1.9    tron   int child;
    299   1.9    tron #endif
    300   1.9    tron #ifdef CLEVER
    301   1.9    tron   int  myerror;
    302   1.9    tron   char *mytoplevel;
    303   1.9    tron   char *myhost;
    304   1.9    tron #endif
    305   1.9    tron #endif
    306   1.9    tron 
    307   1.9    tron #ifdef TOPDOMAIN
    308   1.9    tron   host = strcat(TOPDOMAIN, "-whois.ripe.net");
    309   1.9    tron #else
    310   1.9    tron   host = NICHOST;
    311   1.9    tron #endif
    312   1.9    tron 
    313   1.9    tron #ifdef RIPE
    314   1.9    tron #ifdef NETWORKUPDATE
    315  1.10  itojun     while ((ch = getopt(argc, argv, "46h:p:")) != EOF)
    316   1.9    tron #else
    317  1.10  itojun   while ((ch = getopt(argc, argv, "46acFg:h:i:LmMp:rs:SRt:T:v:")) != EOF)
    318   1.9    tron #endif
    319   1.9    tron #else
    320  1.10  itojun     while ((ch = getopt(argc, argv, "46h:p:")) != EOF)
    321   1.9    tron #endif
    322   1.9    tron       switch((char)ch) {
    323  1.10  itojun       case '4':
    324  1.10  itojun 	socktype = PF_INET;
    325  1.10  itojun 	break;
    326  1.10  itojun       case '6':
    327  1.10  itojun 	socktype = PF_INET6;
    328  1.10  itojun 	break;
    329   1.9    tron       case 'h':
    330   1.9    tron 	host = optarg;
    331   1.9    tron 	opthost = 1;
    332   1.9    tron 	break;
    333   1.9    tron       case 'p':
    334  1.10  itojun 	optport=optarg;
    335   1.9    tron         optp =1;
    336   1.9    tron         break;
    337   1.9    tron #ifdef RIPE
    338   1.9    tron #ifndef NETWORKUPDATE
    339   1.9    tron       case 'a':
    340   1.9    tron 	alldatabases=1;
    341   1.9    tron 	break;
    342   1.9    tron       case 'c':
    343   1.9    tron         optchanged=1;
    344   1.9    tron         break;
    345   1.9    tron       case 'F':
    346   1.9    tron 	optfast = 1;
    347   1.9    tron 	break;
    348   1.9    tron       case 'g':
    349   1.9    tron         getupdates=optarg;
    350   1.9    tron 	optgetupdates=1;
    351   1.9    tron 	break;
    352   1.9    tron       case 'i':
    353   1.9    tron         inverselookup=optarg;
    354   1.9    tron 	optinverselookup = 1;
    355   1.9    tron 	break;
    356   1.9    tron       case 'L':
    357   1.9    tron 	if (optM || optm) {
    358   1.9    tron           fprintf(stderr, "Only one of -L, -m or -M allowed\n\n");
    359   1.9    tron           usage();
    360   1.9    tron         }
    361   1.9    tron 	optL=1;
    362   1.9    tron 	break;
    363   1.9    tron       case 'm':
    364   1.9    tron 	if (optM || optL) {
    365   1.9    tron 	  fprintf(stderr, "Only one of -L, -m or -M allowed\n\n");
    366   1.9    tron           usage();
    367   1.9    tron 	}
    368   1.9    tron 	optm=1;
    369   1.9    tron 	break;
    370   1.9    tron       case 'M':
    371   1.9    tron 	if (optL || optm) {
    372   1.9    tron 	  fprintf(stderr, "Only one of -L, -m or -M allowed\n\n");
    373   1.9    tron 	  usage();
    374   1.9    tron 	}
    375   1.9    tron 	optM=1;
    376   1.9    tron 	break;
    377   1.9    tron 
    378   1.9    tron       case 's':
    379   1.9    tron 	source = optarg;
    380   1.9    tron 	optsource=1;
    381   1.9    tron 	break;
    382   1.9    tron       case 'S':
    383   1.9    tron 	optsugar=1;
    384   1.9    tron 	break;
    385   1.9    tron       case 'R':
    386   1.9    tron 	optnonreferral=1;
    387   1.9    tron 	break;
    388   1.9    tron       case 'r':
    389   1.9    tron 	optrecur=1;
    390   1.9    tron 	break;
    391   1.9    tron       case 't':
    392   1.9    tron 	 templ=optarg;
    393   1.9    tron 	 opttempl=1;
    394   1.9    tron 	 break;
    395   1.9    tron       case 'v':
    396   1.9    tron 	 verbose=optarg;
    397   1.9    tron 	 optverbose=1;
    398   1.9    tron 	 break;
    399   1.9    tron       case 'T':
    400   1.9    tron 	objtype=optarg;
    401   1.9    tron 	optobjtype=1;
    402   1.9    tron 	break;
    403   1.9    tron 
    404   1.9    tron #endif
    405   1.9    tron #endif
    406   1.9    tron       case '?':
    407   1.9    tron       default:
    408   1.9    tron 	usage();
    409   1.9    tron       }
    410   1.9    tron   argc -= optind;
    411   1.9    tron   argv += optind;
    412   1.9    tron 
    413   1.9    tron #ifdef RIPE
    414   1.9    tron #ifdef NETWORKUPDATE
    415   1.9    tron   if (argc>0)
    416   1.9    tron     usage();
    417   1.9    tron #else
    418   1.9    tron   if ((argc<=0) && !opttempl && !optverbose && !optgetupdates && (!(opttempl && optgetupdates)))
    419   1.9    tron     usage();
    420   1.9    tron #endif
    421   1.9    tron #else
    422   1.9    tron   if (argc<=0)
    423   1.9    tron     usage();
    424   1.9    tron #endif
    425   1.9    tron 
    426   1.9    tron   if (!opthost) {
    427   1.9    tron 
    428   1.9    tron #ifdef CLEVER
    429   1.9    tron     whoishost=(char *)calloc(MAXHOSTNAMELEN, sizeof(char));
    430   1.9    tron     myhost =(char *)calloc(MAXHOSTNAMELEN, sizeof(char));
    431   1.9    tron     myerror = gethostname(myhost, MAXHOSTNAMELEN);
    432   1.9    tron     if (myerror >= 0) {
    433   1.9    tron       if (occurs(myhost, ".")) {
    434   1.9    tron 	mytoplevel = rindex(myhost,'.');
    435   1.9    tron 	mytoplevel++;
    436   1.9    tron 	(void) sprintf(whoishost, "%s-whois.ripe.net", mytoplevel);
    437   1.9    tron 	if (verb) fprintf(stderr, "Clever guess: %s\n", whoishost);
    438   1.9    tron       }
    439   1.9    tron     }
    440  1.10  itojun 
    441  1.10  itojun     memset(&hints, 0, sizeof(hints));
    442  1.10  itojun     hints.ai_flags = AI_CANONNAME;
    443  1.10  itojun     hints.ai_family = socktype;
    444  1.10  itojun     hints.ai_socktype = SOCK_STREAM;
    445  1.10  itojun     hints.ai_protocol = 0;
    446  1.10  itojun     error = getaddrinfo(host, optport, &hints, &dst);
    447  1.10  itojun     if ((error) && (verb))
    448   1.9    tron       fprintf(stderr,"No such host: %s\n", whoishost);
    449  1.10  itojun     if (error) {
    450   1.9    tron #endif
    451   1.9    tron 
    452   1.9    tron       whoishost=NICHOST;
    453   1.9    tron 
    454   1.9    tron       if (verb) fprintf(stderr, "Default host: %s\n\n", whoishost);
    455  1.10  itojun       memset(&hints, 0, sizeof(hints));
    456  1.10  itojun       hints.ai_flags = AI_CANONNAME;
    457  1.10  itojun       hints.ai_family = socktype;
    458  1.10  itojun       hints.ai_socktype = SOCK_STREAM;
    459  1.10  itojun       hints.ai_protocol = 0;
    460  1.10  itojun       error = getaddrinfo(host, optport , &hints, &dst);
    461  1.10  itojun       if (error) {
    462   1.9    tron 	fprintf(stderr,"No such host: %s\n", whoishost);
    463   1.9    tron 	if (verb) fprintf(stderr, "Now I give up ...\n");
    464   1.9    tron 	perror("Unknown host");
    465  1.10  itojun 	exit(1);
    466   1.9    tron       }
    467   1.9    tron 
    468   1.9    tron #ifdef CLEVER
    469   1.9    tron     }
    470   1.9    tron #endif
    471   1.9    tron   }
    472   1.9    tron   else {
    473   1.9    tron     if (verb) fprintf(stderr, "Trying: %s\n\n", host);
    474  1.10  itojun     memset(&hints, 0, sizeof(hints));
    475  1.10  itojun     hints.ai_flags = AI_CANONNAME;
    476  1.10  itojun     hints.ai_family = socktype;
    477  1.10  itojun     hints.ai_socktype = SOCK_STREAM;
    478  1.10  itojun     hints.ai_protocol = 0;
    479  1.10  itojun     error = getaddrinfo(host, optport, &hints, &dst);
    480  1.10  itojun     if (error) {
    481   1.9    tron       (void)fprintf(stderr, "whois: %s: ", host);
    482   1.9    tron       perror("Unknown host");
    483   1.9    tron       exit(1);
    484   1.9    tron     }
    485   1.9    tron   }
    486   1.9    tron 
    487  1.10  itojun   while (1) {
    488  1.10  itojun     s = socket(dst->ai_family, dst->ai_socktype, dst->ai_protocol);
    489  1.10  itojun     if (connect(s, dst->ai_addr, dst->ai_addrlen) < 0) {
    490  1.10  itojun       if (verb) (void)fprintf(stderr, "whois: connect miss\n");
    491  1.10  itojun       if (dst->ai_next == NULL) {
    492  1.10  itojun 	perror("whois: connect");
    493  1.10  itojun 	exit(1);
    494  1.10  itojun       } else {
    495  1.10  itojun 	dst = dst->ai_next;
    496  1.10  itojun       }
    497  1.10  itojun       continue;
    498  1.10  itojun     }
    499  1.10  itojun     break;
    500   1.9    tron   }
    501  1.10  itojun   if (verb) (void)fprintf(stderr, "whois: connect success\n");
    502   1.9    tron 
    503   1.9    tron #ifndef NETWORKUPDATE
    504   1.9    tron   sfi = fdopen(s, "r");
    505   1.9    tron   sfo = fdopen(s, "w");
    506   1.9    tron   if (sfi == NULL || sfo == NULL) {
    507   1.9    tron     perror("whois: fdopen");
    508   1.9    tron     (void)close(s);
    509   1.9    tron     exit(1);
    510   1.9    tron   }
    511   1.9    tron #endif
    512   1.9    tron 
    513   1.9    tron   signal(SIGTERM, termhandler);
    514   1.9    tron 
    515   1.9    tron #ifdef RIPE
    516   1.9    tron #ifdef NETWORKUPDATE
    517   1.9    tron 
    518   1.9    tron   if ((child=fork())==0) {
    519   1.9    tron 
    520   1.9    tron      sfo = fdopen(s, "w");
    521   1.9    tron      if (sfo == NULL) {
    522   1.9    tron        perror("whois: fdopen");
    523   1.9    tron        (void)close(s);
    524   1.9    tron        exit(1);
    525   1.9    tron      }
    526   1.9    tron 
    527   1.9    tron      if (gethostname(domainname, sizeof(domainname))) {
    528   1.9    tron         fprintf(stderr, "error when doing gethostname()");
    529   1.9    tron         exit(-1);
    530   1.9    tron      }
    531   1.9    tron 
    532   1.9    tron      passwdentry=getpwuid(getuid());
    533   1.9    tron 
    534   1.9    tron      fprintf(sfo, "-Vnc2.0 -U %s %s\n", passwdentry->pw_name, domainname);
    535   1.9    tron      fflush(sfo);
    536   1.9    tron 
    537   1.9    tron      prev='\0';
    538   1.9    tron 
    539   1.9    tron      while ((ch=getchar()) != EOF) {
    540   1.9    tron 
    541   1.9    tron         fputc(ch, sfo);
    542   1.9    tron 
    543   1.9    tron         if (ch=='\n') fflush(sfo);
    544   1.9    tron         if (feof(sfo)) closesocket(s, child);
    545   1.9    tron         if ((ch=='.') && (prev=='\n')) closesocket(s, child);
    546   1.9    tron         if (!isspace(ch) || ((!isspace(prev)) && (ch=='\n'))) prev=ch;
    547   1.9    tron      }
    548   1.9    tron 
    549   1.9    tron      closesocket(s, child);
    550   1.9    tron 
    551   1.9    tron   }
    552   1.9    tron 
    553   1.9    tron   sfi = fdopen(s, "r");
    554   1.9    tron   if (sfi == NULL) {
    555   1.9    tron        perror("whois: fdopen");
    556   1.9    tron        (void)close(s);
    557   1.9    tron        exit(1);
    558   1.9    tron   }
    559   1.9    tron 
    560   1.9    tron #else
    561   1.9    tron 
    562   1.9    tron   if (alldatabases)
    563   1.9    tron     (void)fprintf(sfo, "-a ");
    564   1.9    tron   if (optchanged)
    565   1.9    tron     (void)fprintf(sfo, "-c ");
    566   1.9    tron   if (optfast)
    567   1.9    tron     (void)fprintf(sfo, "-F ");
    568   1.9    tron   if (optgetupdates)
    569   1.9    tron     (void)fprintf(sfo, "-g %s ", getupdates);
    570   1.9    tron   if (optinverselookup)
    571   1.9    tron     (void)fprintf(sfo, "-i %s ", inverselookup);
    572   1.9    tron   if (optL)
    573   1.9    tron     (void)fprintf(sfo, "-L ");
    574   1.9    tron   if (optm)
    575   1.9    tron     (void)fprintf(sfo, "-m ");
    576   1.9    tron   if (optM)
    577   1.9    tron     (void)fprintf(sfo, "-M ");
    578   1.9    tron   if (optrecur)
    579   1.9    tron     (void)fprintf(sfo, "-r ");
    580   1.9    tron   if (optsource)
    581   1.9    tron     (void)fprintf(sfo, "-s %s ", source);
    582   1.9    tron   if (optsugar)
    583   1.9    tron     (void)fprintf(sfo, "-S ");
    584   1.9    tron   if (optnonreferral)
    585   1.9    tron     (void)fprintf(sfo, "-R ");
    586   1.9    tron   if (opttempl)
    587   1.9    tron     (void)fprintf(sfo, "-t %s ", templ);
    588   1.9    tron   if (optverbose)
    589   1.9    tron     (void)fprintf(sfo, "-v %s ", verbose);
    590   1.9    tron   if (optobjtype)
    591   1.9    tron     (void)fprintf(sfo, "-T %s ", objtype);
    592   1.9    tron 
    593   1.9    tron   /* we can only send the -V when we are sure that we are dealing with
    594   1.9    tron      a RIPE whois server :-( */
    595   1.9    tron 
    596   1.9    tron   whoishost=(char *)calloc(strlen(host)+1, sizeof(char));
    597   1.9    tron   strcpy(whoishost, host);
    598   1.9    tron   for (string=whoishost;(*string=(char)tolower(*string));string++);
    599   1.9    tron 
    600   1.9    tron   if (strstr(whoishost, "ripe.net") ||
    601   1.9    tron       strstr(whoishost, "ra.net") ||
    602   1.9    tron       strstr(whoishost, "apnic.net") ||
    603   1.9    tron       strstr(whoishost, "mci.net") ||
    604   1.9    tron       strstr(whoishost, "isi.edu") ||
    605   1.9    tron       strstr(whoishost, "garr.it") ||
    606   1.9    tron       strstr(whoishost, "ans.net") ||
    607   1.9    tron       alldatabases || optfast || optgetupdates || optinverselookup ||
    608   1.9    tron       optL || optm || optM || optrecur || optsugar || optsource ||
    609   1.9    tron       opttempl || optverbose || optobjtype)
    610   1.9    tron     (void)fprintf(sfo, "-VwC2.0 ");
    611   1.9    tron #endif
    612   1.9    tron #endif
    613   1.9    tron 
    614   1.9    tron #ifndef NETWORKUPDATE
    615   1.9    tron   while (argc-- > 1)
    616   1.9    tron     (void)fprintf(sfo, "%s ", *argv++);
    617   1.9    tron    if (*argv) (void)fputs(*argv, sfo);
    618   1.9    tron    (void)fputs("\r\n", sfo);
    619   1.9    tron   (void)fflush(sfo);
    620   1.9    tron #endif
    621   1.9    tron 
    622   1.9    tron   while ((ch = getc(sfi)) != EOF)
    623   1.9    tron     putchar(ch);
    624   1.9    tron 
    625   1.9    tron   closesocket(s, 1);
    626   1.9    tron 
    627   1.9    tron   exit(0);
    628   1.9    tron 
    629   1.1     cgd }
    630