This commit was generated by cvs2svn to track changes on a CVS vendor
[unix-history] / usr.sbin / gettable / gettable.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1983 The Regents of the University of California.
3 * 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
35char copyright[] =
36"@(#) Copyright (c) 1983 The Regents of the University of California.\n\
37 All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)gettable.c 5.6 (Berkeley) 3/2/91";
42#endif /* not lint */
43
44#include <sys/types.h>
45#include <sys/socket.h>
46
47#include <netinet/in.h>
48
49#include <stdio.h>
50#include <netdb.h>
51
52#define OUTFILE "hosts.txt" /* default output file */
53#define VERFILE "hosts.ver" /* default version file */
54#define QUERY "ALL\r\n" /* query to hostname server */
55#define VERSION "VERSION\r\n" /* get version number */
56
57#define equaln(s1, s2, n) (!strncmp(s1, s2, n))
58
89caf9d9 59struct sockaddr_in s_in;
15637ed4
RG
60char buf[BUFSIZ];
61char *outfile = OUTFILE;
62
63main(argc, argv)
64 int argc;
65 char *argv[];
66{
67 int s;
68 register len;
69 register FILE *sfi, *sfo, *hf;
70 char *host;
71 register struct hostent *hp;
72 struct servent *sp;
73 int version = 0;
74 int beginseen = 0;
75 int endseen = 0;
76
77 argv++, argc--;
78 if (**argv == '-') {
79 if (argv[0][1] != 'v')
80 fprintf(stderr, "unknown option %s ignored\n", *argv);
81 else
82 version++, outfile = VERFILE;
83 argv++, argc--;
84 }
85 if (argc < 1 || argc > 2) {
86 fprintf(stderr, "usage: gettable [-v] host [ file ]\n");
87 exit(1);
88 }
89 sp = getservbyname("hostnames", "tcp");
90 if (sp == NULL) {
91 fprintf(stderr, "gettable: hostnames/tcp: unknown service\n");
92 exit(3);
93 }
94 host = *argv;
95 argv++, argc--;
96 hp = gethostbyname(host);
97 if (hp == NULL) {
98 fprintf(stderr, "gettable: %s: ", host);
99 herror((char *)NULL);
100 exit(2);
101 }
102 host = hp->h_name;
103 if (argc > 0)
104 outfile = *argv;
89caf9d9 105 s_in.sin_family = hp->h_addrtype;
15637ed4
RG
106 s = socket(hp->h_addrtype, SOCK_STREAM, 0);
107 if (s < 0) {
108 perror("gettable: socket");
109 exit(4);
110 }
89caf9d9 111 if (bind(s, (struct sockaddr *)&s_in, sizeof (s_in)) < 0) {
15637ed4
RG
112 perror("gettable: bind");
113 exit(5);
114 }
89caf9d9
PR
115 bcopy(hp->h_addr, &s_in.sin_addr, hp->h_length);
116 s_in.sin_port = sp->s_port;
117 if (connect(s, (struct sockaddr *)&s_in, sizeof (s_in)) < 0) {
15637ed4
RG
118 perror("gettable: connect");
119 exit(6);
120 }
121 fprintf(stderr, "Connection to %s opened.\n", host);
122 sfi = fdopen(s, "r");
123 sfo = fdopen(s, "w");
124 if (sfi == NULL || sfo == NULL) {
125 perror("gettable: fdopen");
126 close(s);
127 exit(1);
128 }
129 hf = fopen(outfile, "w");
130 if (hf == NULL) {
131 fprintf(stderr, "gettable: "); perror(outfile);
132 close(s);
133 exit(1);
134 }
135 fprintf(sfo, version ? VERSION : QUERY);
136 fflush(sfo);
137 while (fgets(buf, sizeof(buf), sfi) != NULL) {
138 len = strlen(buf);
139 buf[len-2] = '\0';
140 if (!version && equaln(buf, "BEGIN", 5)) {
141 if (beginseen || endseen) {
142 fprintf(stderr,
143 "gettable: BEGIN sequence error\n");
144 exit(90);
145 }
146 beginseen++;
147 continue;
148 }
149 if (!version && equaln(buf, "END", 3)) {
150 if (!beginseen || endseen) {
151 fprintf(stderr,
152 "gettable: END sequence error\n");
153 exit(91);
154 }
155 endseen++;
156 continue;
157 }
158 if (equaln(buf, "ERR", 3)) {
159 fprintf(stderr,
160 "gettable: hostname service error: %s", buf);
161 exit(92);
162 }
163 fprintf(hf, "%s\n", buf);
164 }
165 fclose(hf);
166 if (!version) {
167 if (!beginseen) {
168 fprintf(stderr, "gettable: no BEGIN seen\n");
169 exit(93);
170 }
171 if (!endseen) {
172 fprintf(stderr, "gettable: no END seen\n");
173 exit(94);
174 }
175 fprintf(stderr, "Host table received.\n");
176 } else
177 fprintf(stderr, "Version number received.\n");
178 close(s);
179 fprintf(stderr, "Connection to %s closed\n", host);
180 exit(0);
181}