xref: /titanic_41/usr/src/cmd/cmd-inet/usr.bin/whois.c (revision 74e20cfe817b82802b16fac8690dadcda76f54f5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 
45 #include <netinet/in.h>
46 
47 #include <stdio.h>
48 #include <netdb.h>
49 
50 #ifdef SYSV
51 #define	bcopy(a,b,c)	memcpy((b),(a),(c))
52 #endif /* SYSV */
53 
54 #define	NICHOST	"whois.internic.net"
55 
56 int
57 main(argc, argv)
58 	int argc;
59 	char *argv[];
60 {
61 	int s;
62 	register FILE *sfi, *sfo;
63 	register int c;
64 	char *host = NICHOST;
65 	struct sockaddr_in sin;
66 	struct hostent *hp;
67 	struct servent *sp;
68 	char hnamebuf[32];
69 	int addrtype;
70 
71 	argc--, argv++;
72 	if (argc > 2 && strcmp(*argv, "-h") == 0) {
73 		argv++, argc--;
74 		host = *argv++;
75 		argc--;
76 	}
77 	if (argc != 1) {
78 		fprintf(stderr, "usage: whois [ -h host ] name\n");
79 		exit(1);
80 	}
81 	sin.sin_addr.s_addr = inet_addr(host);
82 	if (sin.sin_addr.s_addr != -1 && sin.sin_addr.s_addr != 0) {
83 		addrtype = AF_INET;
84 	} else {
85 		hp = gethostbyname(host);
86 		if (hp == NULL) {
87 			fprintf(stderr, "whois: %s: host unknown\n", host);
88 			exit(1);
89 		}
90 		addrtype = hp->h_addrtype;
91 		host = hp->h_name;
92 		bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
93 	}
94 
95 	s = socket(addrtype, SOCK_STREAM, 0);
96 	if (s < 0) {
97 		perror("whois: socket");
98 		exit(2);
99 	}
100 	sin.sin_family = addrtype;
101 	sp = getservbyname("whois", "tcp");
102 	if (sp == NULL) {
103 		sin.sin_port = htons(IPPORT_WHOIS);
104 	}
105 	else sin.sin_port = sp->s_port;
106 	if (connect(s, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
107 		perror("whois: connect");
108 		exit(5);
109 	}
110 	sfi = fdopen(s, "r");
111 	sfo = fdopen(s, "w");
112 	if (sfi == NULL || sfo == NULL) {
113 		perror("fdopen");
114 		close(s);
115 		exit(1);
116 	}
117 	fprintf(sfo, "%s\r\n", *argv);
118 	fflush(sfo);
119 	while ((c = getc(sfi)) != EOF)
120 		putchar(c);
121 	return (0);
122 }
123