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