xref: /titanic_50/usr/src/cmd/cmd-inet/usr.bin/whois.c (revision 43f842b839f57d4648918e13930041230790bbdf)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5f098c48bSDerek Morr  * Common Development and Distribution License (the "License").
6f098c48bSDerek Morr  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*43f842b8SMilan Jurik  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/socket.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <netinet/in.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <stdio.h>
457c478bd9Sstevel@tonic-gate #include <netdb.h>
46f098c48bSDerek Morr #include <string.h>
47f098c48bSDerek Morr #include <stdlib.h>
48f098c48bSDerek Morr #include <unistd.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #define	NICHOST	"whois.internic.net"
517c478bd9Sstevel@tonic-gate 
52740638c8Sbw int
53f098c48bSDerek Morr main(int argc, char *argv[])
547c478bd9Sstevel@tonic-gate {
55f098c48bSDerek Morr 	int s, rv;
567c478bd9Sstevel@tonic-gate 	register FILE *sfi, *sfo;
577c478bd9Sstevel@tonic-gate 	register int c;
587c478bd9Sstevel@tonic-gate 	char *host = NICHOST;
59f098c48bSDerek Morr 	struct addrinfo *ai_head, *ai;
60f098c48bSDerek Morr 	struct addrinfo hints;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	argc--, argv++;
637c478bd9Sstevel@tonic-gate 	if (argc > 2 && strcmp(*argv, "-h") == 0) {
647c478bd9Sstevel@tonic-gate 		argv++, argc--;
657c478bd9Sstevel@tonic-gate 		host = *argv++;
667c478bd9Sstevel@tonic-gate 		argc--;
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate 	if (argc != 1) {
69*43f842b8SMilan Jurik 		(void) fprintf(stderr, "usage: whois [ -h host ] name\n");
707c478bd9Sstevel@tonic-gate 		exit(1);
717c478bd9Sstevel@tonic-gate 	}
72f098c48bSDerek Morr 
73f098c48bSDerek Morr 	memset(&hints, 0, sizeof (hints));
74f098c48bSDerek Morr 	hints.ai_socktype = SOCK_STREAM;
75f098c48bSDerek Morr 	hints.ai_protocol = IPPROTO_TCP;
76f098c48bSDerek Morr 	hints.ai_flags = AI_ADDRCONFIG;
77f098c48bSDerek Morr 	rv = getaddrinfo(host, "whois", &hints, &ai_head);
78f098c48bSDerek Morr 	if (rv != 0) {
79*43f842b8SMilan Jurik 		(void) fprintf(stderr, "whois: %s: %s\n", host,
80*43f842b8SMilan Jurik 		    gai_strerror(rv));
817c478bd9Sstevel@tonic-gate 		exit(1);
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 
84f098c48bSDerek Morr 	for (ai = ai_head; ai != NULL; ai = ai->ai_next) {
85f098c48bSDerek Morr 		s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
86f098c48bSDerek Morr 		if (s >= 0) {
87f098c48bSDerek Morr 			rv = connect(s, ai->ai_addr, ai->ai_addrlen);
88f098c48bSDerek Morr 			if (rv < 0)
89*43f842b8SMilan Jurik 				(void) close(s);
90f098c48bSDerek Morr 			else
91f098c48bSDerek Morr 				break;
92f098c48bSDerek Morr 		}
93f098c48bSDerek Morr 	}
94*43f842b8SMilan Jurik 	if (ai_head != NULL)
95f098c48bSDerek Morr 		freeaddrinfo(ai_head);
96f098c48bSDerek Morr 
977c478bd9Sstevel@tonic-gate 	if (s < 0) {
987c478bd9Sstevel@tonic-gate 		perror("whois: socket");
997c478bd9Sstevel@tonic-gate 		exit(2);
100f098c48bSDerek Morr 	} else if (rv < 0) {
1017c478bd9Sstevel@tonic-gate 		perror("whois: connect");
1027c478bd9Sstevel@tonic-gate 		exit(5);
1037c478bd9Sstevel@tonic-gate 	}
104f098c48bSDerek Morr 
1057c478bd9Sstevel@tonic-gate 	sfi = fdopen(s, "r");
1067c478bd9Sstevel@tonic-gate 	sfo = fdopen(s, "w");
1077c478bd9Sstevel@tonic-gate 	if (sfi == NULL || sfo == NULL) {
1087c478bd9Sstevel@tonic-gate 		perror("fdopen");
109*43f842b8SMilan Jurik 		(void) close(s);
1107c478bd9Sstevel@tonic-gate 		exit(1);
1117c478bd9Sstevel@tonic-gate 	}
112*43f842b8SMilan Jurik 	(void) fprintf(sfo, "%s\r\n", *argv);
113*43f842b8SMilan Jurik 	(void) fflush(sfo);
1147c478bd9Sstevel@tonic-gate 	while ((c = getc(sfi)) != EOF)
115*43f842b8SMilan Jurik 		(void) putchar(c);
116740638c8Sbw 	return (0);
1177c478bd9Sstevel@tonic-gate }
118