1 /* 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. 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 static const char copyright[] = 36 "@(#) Copyright (c) 1980, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)whois.c 8.1 (Berkeley) 6/6/93"; 43 #endif 44 static const char rcsid[] = 45 "$FreeBSD$"; 46 #endif /* not lint */ 47 48 #include <sys/types.h> 49 #include <sys/socket.h> 50 #include <netinet/in.h> 51 #include <arpa/inet.h> 52 #include <err.h> 53 #include <netdb.h> 54 #include <stdio.h> 55 #include <string.h> 56 #include <sysexits.h> 57 #include <unistd.h> 58 59 #define NICHOST "whois.internic.net" 60 #define DNICHOST "whois.nic.mil" 61 #define GNICHOST "whois.nic.gov" 62 #define ANICHOST "whois.arin.net" 63 #define RNICHOST "whois.ripe.net" 64 #define PNICHOST "whois.apnic.net" 65 #define RUNICHOST "whois.ripn.net" 66 #define WHOIS_PORT 43 67 68 static void usage __P((void)); 69 70 int 71 main(argc, argv) 72 int argc; 73 char **argv; 74 { 75 register FILE *sfi, *sfo; 76 register int ch; 77 struct sockaddr_in sin; 78 struct hostent *hp; 79 struct servent *sp; 80 int s; 81 char *host; 82 83 #ifdef SOCKS 84 SOCKSinit(argv[0]); 85 #endif 86 87 host = NICHOST; 88 while ((ch = getopt(argc, argv, "adgh:prR")) != -1) 89 switch((char)ch) { 90 case 'a': 91 host = ANICHOST; 92 break; 93 case 'd': 94 host = DNICHOST; 95 break; 96 case 'g': 97 host = GNICHOST; 98 break; 99 case 'h': 100 host = optarg; 101 break; 102 case 'p': 103 host = PNICHOST; 104 break; 105 case 'r': 106 host = RNICHOST; 107 break; 108 case 'R': 109 host = RUNICHOST; 110 break; 111 case '?': 112 default: 113 usage(); 114 } 115 argc -= optind; 116 argv += optind; 117 118 if (!argc) 119 usage(); 120 121 s = socket(PF_INET, SOCK_STREAM, 0); 122 if (s < 0) 123 err(EX_OSERR, "socket"); 124 125 memset(&sin, 0, sizeof sin); 126 sin.sin_len = sizeof sin; 127 sin.sin_family = AF_INET; 128 129 if (inet_aton(host, &sin.sin_addr) == 0) { 130 hp = gethostbyname2(host, AF_INET); 131 if (hp == NULL) 132 errx(EX_NOHOST, "%s: %s", host, hstrerror(h_errno)); 133 host = hp->h_name; 134 sin.sin_addr = *(struct in_addr *)hp->h_addr_list[0]; 135 } 136 137 sp = getservbyname("whois", "tcp"); 138 if (sp == NULL) 139 sin.sin_port = htons(WHOIS_PORT); 140 else 141 sin.sin_port = sp->s_port; 142 143 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) 144 err(EX_OSERR, "connect"); 145 146 sfi = fdopen(s, "r"); 147 sfo = fdopen(s, "w"); 148 if (sfi == NULL || sfo == NULL) 149 err(EX_OSERR, "fdopen"); 150 while (argc-- > 1) 151 (void)fprintf(sfo, "%s ", *argv++); 152 (void)fprintf(sfo, "%s\r\n", *argv); 153 (void)fflush(sfo); 154 while ((ch = getc(sfi)) != EOF) 155 putchar(ch); 156 exit(0); 157 } 158 159 static void 160 usage() 161 { 162 fprintf(stderr, "usage: whois [-adgprR] [-h hostname] name ...\n"); 163 exit(EX_USAGE); 164 } 165