xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/gettable.c (revision 473:9c6ea6da29de)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*473Sbw  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
320Sstevel@tonic-gate  * under license from the Regents of the University of California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/socket.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <netinet/in.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <stdio.h>
430Sstevel@tonic-gate #include <netdb.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #define	OUTFILE		"hosts.txt"	/* default output file */
460Sstevel@tonic-gate #define	VERFILE		"hosts.ver"	/* default version file */
470Sstevel@tonic-gate #define	QUERY		"ALL\r\n"	/* query to hostname server */
480Sstevel@tonic-gate #define	VERSION		"VERSION\r\n"	/* get version number */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #define	equaln(s1, s2, n)	(!strncmp(s1, s2, n))
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #ifdef SYSV
530Sstevel@tonic-gate #define bcopy(a,b,c)	  memcpy(b,a,c)
540Sstevel@tonic-gate #endif
550Sstevel@tonic-gate 
560Sstevel@tonic-gate struct	sockaddr_in sin;
570Sstevel@tonic-gate struct	sockaddr_in sintmp;
580Sstevel@tonic-gate char	buf[BUFSIZ];
590Sstevel@tonic-gate char	*outfile = OUTFILE;
600Sstevel@tonic-gate 
61*473Sbw int
main(argc,argv)620Sstevel@tonic-gate main(argc, argv)
630Sstevel@tonic-gate 	int argc;
640Sstevel@tonic-gate 	char *argv[];
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 	int s;
67*473Sbw 	register int len;
680Sstevel@tonic-gate 	register FILE *sfi, *sfo, *hf;
690Sstevel@tonic-gate 	char *host;
700Sstevel@tonic-gate 	register struct hostent *hp;
710Sstevel@tonic-gate 	struct servent *sp;
720Sstevel@tonic-gate 	int version = 0;
730Sstevel@tonic-gate 	int beginseen = 0;
740Sstevel@tonic-gate 	int endseen = 0;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	argv++, argc--;
770Sstevel@tonic-gate 	if (argc > 0 && **argv == '-') {
780Sstevel@tonic-gate 		if (argv[0][1] != 'v')
790Sstevel@tonic-gate 			fprintf(stderr, "unknown option %s ignored\n", *argv);
800Sstevel@tonic-gate 		else
810Sstevel@tonic-gate 			version++, outfile = VERFILE;
820Sstevel@tonic-gate 		argv++, argc--;
830Sstevel@tonic-gate 	}
840Sstevel@tonic-gate 	if (argc < 1 || argc > 2) {
850Sstevel@tonic-gate 		fprintf(stderr, "usage: gettable [-v] host [ file ]\n");
860Sstevel@tonic-gate 		exit(1);
870Sstevel@tonic-gate 	}
880Sstevel@tonic-gate 	sp = getservbyname("hostnames", "tcp");
890Sstevel@tonic-gate 	if (sp == NULL) {
900Sstevel@tonic-gate 		fprintf(stderr, "gettable: hostnames/tcp: unknown service\n");
910Sstevel@tonic-gate 		exit(3);
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 	host = *argv;
940Sstevel@tonic-gate 	argv++, argc--;
950Sstevel@tonic-gate 	sintmp.sin_addr.s_addr = inet_addr(host);
960Sstevel@tonic-gate 	if (sintmp.sin_addr.s_addr != -1 && sintmp.sin_addr.s_addr != 0) {
970Sstevel@tonic-gate 		sin.sin_family = AF_INET;
980Sstevel@tonic-gate 	} else {
990Sstevel@tonic-gate 		hp = gethostbyname(host);
1000Sstevel@tonic-gate 		if (hp == NULL) {
1010Sstevel@tonic-gate 			fprintf(stderr, "gettable: %s: host unknown\n", host);
1020Sstevel@tonic-gate 			exit(2);
1030Sstevel@tonic-gate 		} else {
1040Sstevel@tonic-gate 			sin.sin_family = hp->h_addrtype;
1050Sstevel@tonic-gate 			host = hp->h_name;
1060Sstevel@tonic-gate 		}
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 	if (argc > 0)
1090Sstevel@tonic-gate 		outfile = *argv;
1100Sstevel@tonic-gate 	s = socket(sin.sin_family, SOCK_STREAM, 0);
1110Sstevel@tonic-gate 	if (s < 0) {
1120Sstevel@tonic-gate 		perror("gettable: socket");
1130Sstevel@tonic-gate 		exit(4);
1140Sstevel@tonic-gate 	}
1150Sstevel@tonic-gate 	if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
1160Sstevel@tonic-gate 		perror("gettable: bind");
1170Sstevel@tonic-gate 		exit(5);
1180Sstevel@tonic-gate 	}
1190Sstevel@tonic-gate 	if (sintmp.sin_addr.s_addr != -1 && sintmp.sin_addr.s_addr != 0)
1200Sstevel@tonic-gate 		bcopy((char *)&sintmp.sin_addr, (char *)&sin.sin_addr,
1210Sstevel@tonic-gate 			sizeof(sintmp.sin_addr));
1220Sstevel@tonic-gate 	else
1230Sstevel@tonic-gate 		bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
1240Sstevel@tonic-gate 	sin.sin_port = sp->s_port;
1250Sstevel@tonic-gate 	if (connect(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
1260Sstevel@tonic-gate 		perror("gettable: connect");
1270Sstevel@tonic-gate 		exit(6);
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate 	fprintf(stderr, "Connection to %s opened.\n", host);
1300Sstevel@tonic-gate 	sfi = fdopen(s, "r");
1310Sstevel@tonic-gate 	sfo = fdopen(s, "w");
1320Sstevel@tonic-gate 	if (sfi == NULL || sfo == NULL) {
1330Sstevel@tonic-gate 		perror("gettable: fdopen");
1340Sstevel@tonic-gate 		close(s);
1350Sstevel@tonic-gate 		exit(1);
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 	hf = fopen(outfile, "w");
1380Sstevel@tonic-gate 	if (hf == NULL) {
1390Sstevel@tonic-gate 		fprintf(stderr, "gettable: "); perror(outfile);
1400Sstevel@tonic-gate 		close(s);
1410Sstevel@tonic-gate 		exit(1);
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 	fprintf(sfo, version ? VERSION : QUERY);
1440Sstevel@tonic-gate 	fflush(sfo);
1450Sstevel@tonic-gate 	while (fgets(buf, sizeof(buf), sfi) != NULL) {
1460Sstevel@tonic-gate 		len = strlen(buf);
1470Sstevel@tonic-gate 		buf[len-2] = '\0';
1480Sstevel@tonic-gate 		if (!version && equaln(buf, "BEGIN", 5)) {
1490Sstevel@tonic-gate 			if (beginseen || endseen) {
1500Sstevel@tonic-gate 				fprintf(stderr,
1510Sstevel@tonic-gate 				    "gettable: BEGIN sequence error\n");
1520Sstevel@tonic-gate 				exit(90);
1530Sstevel@tonic-gate 			}
1540Sstevel@tonic-gate 			beginseen++;
1550Sstevel@tonic-gate 			continue;
1560Sstevel@tonic-gate 		}
1570Sstevel@tonic-gate 		if (!version && equaln(buf, "END", 3)) {
1580Sstevel@tonic-gate 			if (!beginseen || endseen) {
1590Sstevel@tonic-gate 				fprintf(stderr,
1600Sstevel@tonic-gate 				    "gettable: END sequence error\n");
1610Sstevel@tonic-gate 				exit(91);
1620Sstevel@tonic-gate 			}
1630Sstevel@tonic-gate 			endseen++;
1640Sstevel@tonic-gate 			continue;
1650Sstevel@tonic-gate 		}
1660Sstevel@tonic-gate 		if (equaln(buf, "ERR", 3)) {
1670Sstevel@tonic-gate 			fprintf(stderr,
1680Sstevel@tonic-gate 			    "gettable: hostname service error: %s", buf);
1690Sstevel@tonic-gate 			exit(92);
1700Sstevel@tonic-gate 		}
1710Sstevel@tonic-gate 		fprintf(hf, "%s\n", buf);
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 	fclose(hf);
1740Sstevel@tonic-gate 	if (!version) {
1750Sstevel@tonic-gate 		if (!beginseen) {
1760Sstevel@tonic-gate 			fprintf(stderr, "gettable: no BEGIN seen\n");
1770Sstevel@tonic-gate 			exit(93);
1780Sstevel@tonic-gate 		}
1790Sstevel@tonic-gate 		if (!endseen) {
1800Sstevel@tonic-gate 			fprintf(stderr, "gettable: no END seen\n");
1810Sstevel@tonic-gate 			exit(94);
1820Sstevel@tonic-gate 		}
1830Sstevel@tonic-gate 		fprintf(stderr, "Host table received.\n");
1840Sstevel@tonic-gate 	} else
1850Sstevel@tonic-gate 		fprintf(stderr, "Version number received.\n");
1860Sstevel@tonic-gate 	close(s);
1870Sstevel@tonic-gate 	fprintf(stderr, "Connection to %s closed\n", host);
188*473Sbw 	return (0);
1890Sstevel@tonic-gate }
190