Annotation of src/usr.bin/whois/whois.c, Revision 1.5
1.5 ! art 1: /* $OpenBSD: whois.c,v 1.4 1998/02/24 10:09:50 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.5 ! art 47: static char rcsid[] = "$OpenBSD: whois.c,v 1.4 1998/02/24 10:09:50 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>
1.5 ! art 53: #include <arpa/inet.h>
1.1 deraadt 54: #include <netdb.h>
55: #include <stdio.h>
56: #include <stdlib.h>
57: #include <string.h>
1.4 deraadt 58: #include <sysexits.h>
1.1 deraadt 59: #include <unistd.h>
1.5 ! art 60: #include <err.h>
1.1 deraadt 61:
1.4 deraadt 62: #define NICHOST "whois.internic.net"
63: #define DNICHOST "nic.ddn.mil"
64: #define ANICHOST "whois.arin.net"
65: #define RNICHOST "whois.ripe.net"
66: #define PNICHOST "whois.apnic.net"
67: #define WHOIS_PORT 43
1.1 deraadt 68:
69: static void usage();
70:
71: int
72: main(argc, argv)
73: int argc;
74: char **argv;
75: {
76: extern char *optarg;
77: extern int optind;
78: register FILE *sfi, *sfo;
79: register int ch;
80: struct sockaddr_in sin;
81: struct hostent *hp;
82: struct servent *sp;
83: int s;
84: char *host;
85:
86: host = NICHOST;
1.4 deraadt 87: while ((ch = getopt(argc, argv, "adh:pr")) != -1)
1.1 deraadt 88: switch((char)ch) {
1.4 deraadt 89: case 'a':
90: host = ANICHOST;
91: break;
92: case 'd':
93: host = DNICHOST;
94: break;
1.1 deraadt 95: case 'h':
96: host = optarg;
97: break;
1.4 deraadt 98: case 'p':
99: host = PNICHOST;
100: break;
101: case 'r':
102: host = RNICHOST;
103: break;
1.1 deraadt 104: case '?':
105: default:
106: usage();
107: }
108: argc -= optind;
109: argv += optind;
110:
111: if (!argc)
112: usage();
113:
1.4 deraadt 114: s = socket(PF_INET, SOCK_STREAM, 0);
115: if (s < 0)
116: err(EX_OSERR, "socket");
117:
118: memset(&sin, 0, sizeof sin);
119: sin.sin_len = sizeof sin;
120: sin.sin_family = AF_INET;
121:
122: if (inet_aton(host, &sin.sin_addr) == 0) {
123: hp = gethostbyname2(host, AF_INET);
124: if (hp == NULL)
125: errx(EX_NOHOST, "%s: %s", host, hstrerror(h_errno));
126: host = hp->h_name;
127: sin.sin_addr = *(struct in_addr *)hp->h_addr_list[0];
1.1 deraadt 128: }
1.4 deraadt 129:
1.1 deraadt 130: sp = getservbyname("whois", "tcp");
1.4 deraadt 131: if (sp == NULL)
132: sin.sin_port = htons(WHOIS_PORT);
133: else
134: sin.sin_port = sp->s_port;
135:
136: if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
137: err(EX_OSERR, "connect");
138:
1.1 deraadt 139: sfi = fdopen(s, "r");
140: sfo = fdopen(s, "w");
1.4 deraadt 141: if (sfi == NULL || sfo == NULL)
142: err(EX_OSERR, "fdopen");
1.1 deraadt 143: while (argc-- > 1)
144: (void)fprintf(sfo, "%s ", *argv++);
145: (void)fprintf(sfo, "%s\r\n", *argv);
146: (void)fflush(sfo);
147: while ((ch = getc(sfi)) != EOF)
148: putchar(ch);
149: exit(0);
150: }
151:
152: static void
153: usage()
154: {
1.4 deraadt 155: (void)fprintf(stderr, "usage: whois [-adpr] [-h hostname] name ...\n");
156: exit(EX_USAGE);
1.1 deraadt 157: }