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