xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.bin/whois.c (revision 8914:283ac0b2ec39)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57448Sderekmorr@psu.edu  * Common Development and Distribution License (the "License").
67448Sderekmorr@psu.edu  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*8914SMilan.Jurik@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  * The Regents of the University of California
320Sstevel@tonic-gate  * All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  * contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <sys/socket.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <netinet/in.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <stdio.h>
450Sstevel@tonic-gate #include <netdb.h>
467448Sderekmorr@psu.edu #include <string.h>
477448Sderekmorr@psu.edu #include <stdlib.h>
487448Sderekmorr@psu.edu #include <unistd.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #define	NICHOST	"whois.internic.net"
510Sstevel@tonic-gate 
52473Sbw int
main(int argc,char * argv[])537448Sderekmorr@psu.edu main(int argc, char *argv[])
540Sstevel@tonic-gate {
557448Sderekmorr@psu.edu 	int s, rv;
560Sstevel@tonic-gate 	register FILE *sfi, *sfo;
570Sstevel@tonic-gate 	register int c;
580Sstevel@tonic-gate 	char *host = NICHOST;
597448Sderekmorr@psu.edu 	struct addrinfo *ai_head, *ai;
607448Sderekmorr@psu.edu 	struct addrinfo hints;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	argc--, argv++;
630Sstevel@tonic-gate 	if (argc > 2 && strcmp(*argv, "-h") == 0) {
640Sstevel@tonic-gate 		argv++, argc--;
650Sstevel@tonic-gate 		host = *argv++;
660Sstevel@tonic-gate 		argc--;
670Sstevel@tonic-gate 	}
680Sstevel@tonic-gate 	if (argc != 1) {
69*8914SMilan.Jurik@Sun.COM 		(void) fprintf(stderr, "usage: whois [ -h host ] name\n");
700Sstevel@tonic-gate 		exit(1);
710Sstevel@tonic-gate 	}
727448Sderekmorr@psu.edu 
737448Sderekmorr@psu.edu 	memset(&hints, 0, sizeof (hints));
747448Sderekmorr@psu.edu 	hints.ai_socktype = SOCK_STREAM;
757448Sderekmorr@psu.edu 	hints.ai_protocol = IPPROTO_TCP;
767448Sderekmorr@psu.edu 	hints.ai_flags = AI_ADDRCONFIG;
777448Sderekmorr@psu.edu 	rv = getaddrinfo(host, "whois", &hints, &ai_head);
787448Sderekmorr@psu.edu 	if (rv != 0) {
79*8914SMilan.Jurik@Sun.COM 		(void) fprintf(stderr, "whois: %s: %s\n", host,
80*8914SMilan.Jurik@Sun.COM 		    gai_strerror(rv));
817448Sderekmorr@psu.edu 		exit(1);
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate 
847448Sderekmorr@psu.edu 	for (ai = ai_head; ai != NULL; ai = ai->ai_next) {
857448Sderekmorr@psu.edu 		s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
867448Sderekmorr@psu.edu 		if (s >= 0) {
877448Sderekmorr@psu.edu 			rv = connect(s, ai->ai_addr, ai->ai_addrlen);
887448Sderekmorr@psu.edu 			if (rv < 0)
89*8914SMilan.Jurik@Sun.COM 				(void) close(s);
907448Sderekmorr@psu.edu 			else
917448Sderekmorr@psu.edu 				break;
927448Sderekmorr@psu.edu 		}
937448Sderekmorr@psu.edu 	}
94*8914SMilan.Jurik@Sun.COM 	if (ai_head != NULL)
95*8914SMilan.Jurik@Sun.COM 		freeaddrinfo(ai_head);
967448Sderekmorr@psu.edu 
970Sstevel@tonic-gate 	if (s < 0) {
980Sstevel@tonic-gate 		perror("whois: socket");
990Sstevel@tonic-gate 		exit(2);
1007448Sderekmorr@psu.edu 	} else if (rv < 0) {
1010Sstevel@tonic-gate 		perror("whois: connect");
1020Sstevel@tonic-gate 		exit(5);
1030Sstevel@tonic-gate 	}
1047448Sderekmorr@psu.edu 
1050Sstevel@tonic-gate 	sfi = fdopen(s, "r");
1060Sstevel@tonic-gate 	sfo = fdopen(s, "w");
1070Sstevel@tonic-gate 	if (sfi == NULL || sfo == NULL) {
1080Sstevel@tonic-gate 		perror("fdopen");
109*8914SMilan.Jurik@Sun.COM 		(void) close(s);
1100Sstevel@tonic-gate 		exit(1);
1110Sstevel@tonic-gate 	}
112*8914SMilan.Jurik@Sun.COM 	(void) fprintf(sfo, "%s\r\n", *argv);
113*8914SMilan.Jurik@Sun.COM 	(void) fflush(sfo);
1140Sstevel@tonic-gate 	while ((c = getc(sfi)) != EOF)
115*8914SMilan.Jurik@Sun.COM 		(void) putchar(c);
116473Sbw 	return (0);
1170Sstevel@tonic-gate }
118