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