1 /* 2 * Copyright (c) 1980 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18 #ifndef lint 19 char copyright[] = 20 "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 21 All rights reserved.\n"; 22 #endif /* not lint */ 23 24 #ifndef lint 25 static char sccsid[] = "@(#)whois.c 5.8 (Berkeley) 09/10/89"; 26 #endif /* not lint */ 27 28 #include <sys/types.h> 29 #include <sys/socket.h> 30 #include <netinet/in.h> 31 #include <netdb.h> 32 #include <stdio.h> 33 34 #define NICHOST "nic.ddn.mil" 35 36 main(argc, argv) 37 int argc; 38 char **argv; 39 { 40 extern char *optarg; 41 extern int optind; 42 register FILE *sfi, *sfo; 43 register int ch; 44 struct sockaddr_in sin; 45 struct hostent *hp; 46 struct servent *sp; 47 int s; 48 char *host; 49 50 host = NICHOST; 51 while ((ch = getopt(argc, argv, "h:")) != EOF) 52 switch((char)ch) { 53 case 'h': 54 host = optarg; 55 break; 56 case '?': 57 default: 58 usage(); 59 } 60 argc -= optind; 61 argv += optind; 62 63 if (argc != 1) 64 usage(); 65 66 hp = gethostbyname(host); 67 if (hp == NULL) { 68 (void)fprintf(stderr, "whois: %s: ", host); 69 herror((char *)NULL); 70 exit(1); 71 } 72 host = hp->h_name; 73 s = socket(hp->h_addrtype, SOCK_STREAM, 0); 74 if (s < 0) { 75 perror("whois: socket"); 76 exit(2); 77 } 78 bzero((caddr_t)&sin, sizeof (sin)); 79 sin.sin_family = hp->h_addrtype; 80 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 81 perror("whois: bind"); 82 exit(3); 83 } 84 bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length); 85 sp = getservbyname("whois", "tcp"); 86 if (sp == NULL) { 87 (void)fprintf(stderr, "whois: whois/tcp: unknown service\n"); 88 exit(4); 89 } 90 sin.sin_port = sp->s_port; 91 if (connect(s, &sin, sizeof(sin)) < 0) { 92 perror("whois: connect"); 93 exit(5); 94 } 95 sfi = fdopen(s, "r"); 96 sfo = fdopen(s, "w"); 97 if (sfi == NULL || sfo == NULL) { 98 perror("whois: fdopen"); 99 (void)close(s); 100 exit(1); 101 } 102 (void)fprintf(sfo, "%s\r\n", *argv); 103 (void)fflush(sfo); 104 while ((ch = getc(sfi)) != EOF) 105 putchar(ch); 106 exit(0); 107 } 108 109 static 110 usage() 111 { 112 (void)fprintf(stderr, "usage: whois [-h host] name\n"); 113 exit(1); 114 } 115