|
|
1.1 root 1: /*
2: * Copyright (c) 1983 Regents of the University of California.
3: * All rights reserved. The Berkeley software License Agreement
4: * specifies the terms and conditions for redistribution.
5: */
6:
7: #ifndef lint
8: static char sccsid[] = "@(#)gettable.c 5.2 (Berkeley) 4/30/86";
9: #endif not lint
10:
11: #include <sys/types.h>
12: #include <sys/socket.h>
13:
14: #include <netinet/in.h>
15:
16: #include <stdio.h>
17: #include <netdb.h>
18:
19: #define OUTFILE "hosts.txt" /* default output file */
20: #define VERFILE "hosts.ver" /* default version file */
21: #define QUERY "ALL\r\n" /* query to hostname server */
22: #define VERSION "VERSION\r\n" /* get version number */
23:
24: #define equaln(s1, s2, n) (!strncmp(s1, s2, n))
25:
26: struct sockaddr_in sin;
27: char buf[BUFSIZ];
28: char *outfile = OUTFILE;
29:
30: main(argc, argv)
31: int argc;
32: char *argv[];
33: {
34: int s;
35: register len;
36: register FILE *sfi, *sfo, *hf;
37: char *host;
38: register struct hostent *hp;
39: struct servent *sp;
40: int version = 0;
41: int beginseen = 0;
42: int endseen = 0;
43:
44: argv++, argc--;
45: if (**argv == '-') {
46: if (argv[0][1] != 'v')
47: fprintf(stderr, "unknown option %s ignored\n", *argv);
48: else
49: version++, outfile = VERFILE;
50: argv++, argc--;
51: }
52: if (argc < 1 || argc > 2) {
53: fprintf(stderr, "usage: gettable [-v] host [ file ]\n");
54: exit(1);
55: }
56: sp = getservbyname("hostnames", "tcp");
57: if (sp == NULL) {
58: fprintf(stderr, "gettable: hostnames/tcp: unknown service\n");
59: exit(3);
60: }
61: host = *argv;
62: argv++, argc--;
63: hp = gethostbyname(host);
64: if (hp == NULL) {
65: fprintf(stderr, "gettable: %s: host unknown\n", host);
66: exit(2);
67: }
68: host = hp->h_name;
69: if (argc > 0)
70: outfile = *argv;
71: sin.sin_family = hp->h_addrtype;
72: s = socket(hp->h_addrtype, SOCK_STREAM, 0);
73: if (s < 0) {
74: perror("gettable: socket");
75: exit(4);
76: }
77: if (bind(s, &sin, sizeof (sin)) < 0) {
78: perror("gettable: bind");
79: exit(5);
80: }
81: bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
82: sin.sin_port = sp->s_port;
83: if (connect(s, &sin, sizeof (sin)) < 0) {
84: perror("gettable: connect");
85: exit(6);
86: }
87: fprintf(stderr, "Connection to %s opened.\n", host);
88: sfi = fdopen(s, "r");
89: sfo = fdopen(s, "w");
90: if (sfi == NULL || sfo == NULL) {
91: perror("gettable: fdopen");
92: close(s);
93: exit(1);
94: }
95: hf = fopen(outfile, "w");
96: if (hf == NULL) {
97: fprintf(stderr, "gettable: "); perror(outfile);
98: close(s);
99: exit(1);
100: }
101: fprintf(sfo, version ? VERSION : QUERY);
102: fflush(sfo);
103: while (fgets(buf, sizeof(buf), sfi) != NULL) {
104: len = strlen(buf);
105: buf[len-2] = '\0';
106: if (!version && equaln(buf, "BEGIN", 5)) {
107: if (beginseen || endseen) {
108: fprintf(stderr,
109: "gettable: BEGIN sequence error\n");
110: exit(90);
111: }
112: beginseen++;
113: continue;
114: }
115: if (!version && equaln(buf, "END", 3)) {
116: if (!beginseen || endseen) {
117: fprintf(stderr,
118: "gettable: END sequence error\n");
119: exit(91);
120: }
121: endseen++;
122: continue;
123: }
124: if (equaln(buf, "ERR", 3)) {
125: fprintf(stderr,
126: "gettable: hostname service error: %s", buf);
127: exit(92);
128: }
129: fprintf(hf, "%s\n", buf);
130: }
131: fclose(hf);
132: if (!version) {
133: if (!beginseen) {
134: fprintf(stderr, "gettable: no BEGIN seen\n");
135: exit(93);
136: }
137: if (!endseen) {
138: fprintf(stderr, "gettable: no END seen\n");
139: exit(94);
140: }
141: fprintf(stderr, "Host table received.\n");
142: } else
143: fprintf(stderr, "Version number received.\n");
144: close(s);
145: fprintf(stderr, "Connection to %s closed\n", host);
146: exit(0);
147: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.