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