Home | History | Annotate | Line # | Download | only in whois
whois.c revision 1.6
      1 /*	$NetBSD: whois.c,v 1.6 1997/10/19 14:46:19 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)whois.c	8.1 (Berkeley) 6/6/93";
     45 #else
     46 __RCSID("$NetBSD: whois.c,v 1.6 1997/10/19 14:46:19 mrg Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/types.h>
     51 #include <sys/socket.h>
     52 #include <netinet/in.h>
     53 #include <netdb.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 #include <unistd.h>
     58 
     59 #define	NICHOST	"whois.internic.net"
     60 
     61 int main __P((int, char **));
     62 static void usage __P((void));
     63 
     64 int
     65 main(argc, argv)
     66 	int argc;
     67 	char **argv;
     68 {
     69 	extern char *optarg;
     70 	extern int optind;
     71 	FILE *sfi, *sfo;
     72 	int ch;
     73 	struct sockaddr_in sin;
     74 	struct hostent *hp;
     75 	struct servent *sp;
     76 	int s;
     77 	char *host;
     78 
     79 	host = NICHOST;
     80 	while ((ch = getopt(argc, argv, "h:")) != EOF)
     81 		switch((char)ch) {
     82 		case 'h':
     83 			host = optarg;
     84 			break;
     85 		case '?':
     86 		default:
     87 			usage();
     88 		}
     89 	argc -= optind;
     90 	argv += optind;
     91 
     92 	if (!argc)
     93 		usage();
     94 
     95 	hp = gethostbyname(host);
     96 	if (hp == NULL) {
     97 		(void)fprintf(stderr, "whois: %s: ", host);
     98 		herror((char *)NULL);
     99 		exit(1);
    100 	}
    101 	host = hp->h_name;
    102 	s = socket(hp->h_addrtype, SOCK_STREAM, 0);
    103 	if (s < 0) {
    104 		perror("whois: socket");
    105 		exit(1);
    106 	}
    107 	bzero((caddr_t)&sin, sizeof (sin));
    108 	sin.sin_family = hp->h_addrtype;
    109 	if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    110 		perror("whois: bind");
    111 		exit(1);
    112 	}
    113 	bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
    114 	sp = getservbyname("whois", "tcp");
    115 	if (sp == NULL) {
    116 		(void)fprintf(stderr, "whois: whois/tcp: unknown service\n");
    117 		exit(1);
    118 	}
    119 	sin.sin_port = sp->s_port;
    120 	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    121 		perror("whois: connect");
    122 		exit(1);
    123 	}
    124 	sfi = fdopen(s, "r");
    125 	sfo = fdopen(s, "w");
    126 	if (sfi == NULL || sfo == NULL) {
    127 		perror("whois: fdopen");
    128 		(void)close(s);
    129 		exit(1);
    130 	}
    131 	while (argc-- > 1)
    132 		(void)fprintf(sfo, "%s ", *argv++);
    133 	(void)fprintf(sfo, "%s\r\n", *argv);
    134 	(void)fflush(sfo);
    135 	while ((ch = getc(sfi)) != EOF)
    136 		putchar(ch);
    137 	exit(0);
    138 }
    139 
    140 static void
    141 usage()
    142 {
    143 	(void)fprintf(stderr, "usage: whois [-h hostname] name ...\n");
    144 	exit(1);
    145 }
    146