Home | History | Annotate | Line # | Download | only in whois
whois.c revision 1.5
      1 /*	$NetBSD: whois.c,v 1.5 1994/11/14 05:13:25 jtc 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 #ifndef lint
     37 static char copyright[] =
     38 "@(#) 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 #endif
     46 static char rcsid[] = "$NetBSD: whois.c,v 1.5 1994/11/14 05:13:25 jtc Exp $";
     47 #endif /* not lint */
     48 
     49 #include <sys/types.h>
     50 #include <sys/socket.h>
     51 #include <netinet/in.h>
     52 #include <netdb.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <unistd.h>
     57 
     58 #define	NICHOST	"whois.internic.net"
     59 
     60 static void usage();
     61 
     62 int
     63 main(argc, argv)
     64 	int argc;
     65 	char **argv;
     66 {
     67 	extern char *optarg;
     68 	extern int optind;
     69 	register FILE *sfi, *sfo;
     70 	register int ch;
     71 	struct sockaddr_in sin;
     72 	struct hostent *hp;
     73 	struct servent *sp;
     74 	int s;
     75 	char *host;
     76 
     77 	host = NICHOST;
     78 	while ((ch = getopt(argc, argv, "h:")) != EOF)
     79 		switch((char)ch) {
     80 		case 'h':
     81 			host = optarg;
     82 			break;
     83 		case '?':
     84 		default:
     85 			usage();
     86 		}
     87 	argc -= optind;
     88 	argv += optind;
     89 
     90 	if (!argc)
     91 		usage();
     92 
     93 	hp = gethostbyname(host);
     94 	if (hp == NULL) {
     95 		(void)fprintf(stderr, "whois: %s: ", host);
     96 		herror((char *)NULL);
     97 		exit(1);
     98 	}
     99 	host = hp->h_name;
    100 	s = socket(hp->h_addrtype, SOCK_STREAM, 0);
    101 	if (s < 0) {
    102 		perror("whois: socket");
    103 		exit(1);
    104 	}
    105 	bzero((caddr_t)&sin, sizeof (sin));
    106 	sin.sin_family = hp->h_addrtype;
    107 	if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    108 		perror("whois: bind");
    109 		exit(1);
    110 	}
    111 	bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
    112 	sp = getservbyname("whois", "tcp");
    113 	if (sp == NULL) {
    114 		(void)fprintf(stderr, "whois: whois/tcp: unknown service\n");
    115 		exit(1);
    116 	}
    117 	sin.sin_port = sp->s_port;
    118 	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    119 		perror("whois: connect");
    120 		exit(1);
    121 	}
    122 	sfi = fdopen(s, "r");
    123 	sfo = fdopen(s, "w");
    124 	if (sfi == NULL || sfo == NULL) {
    125 		perror("whois: fdopen");
    126 		(void)close(s);
    127 		exit(1);
    128 	}
    129 	while (argc-- > 1)
    130 		(void)fprintf(sfo, "%s ", *argv++);
    131 	(void)fprintf(sfo, "%s\r\n", *argv);
    132 	(void)fflush(sfo);
    133 	while ((ch = getc(sfi)) != EOF)
    134 		putchar(ch);
    135 	exit(0);
    136 }
    137 
    138 static void
    139 usage()
    140 {
    141 	(void)fprintf(stderr, "usage: whois [-h hostname] name ...\n");
    142 	exit(1);
    143 }
    144