Commit | Line | Data |
---|---|---|
629e75d6 DF |
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 | ||
b9feeb00 | 7 | #ifndef lint |
f5bcb151 | 8 | static char sccsid[] = "@(#)gettable.c 5.2 (Berkeley) %G%"; |
629e75d6 | 9 | #endif not lint |
b9feeb00 SL |
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 */ | |
329362bd | 20 | #define VERFILE "hosts.ver" /* default version file */ |
b9feeb00 | 21 | #define QUERY "ALL\r\n" /* query to hostname server */ |
329362bd | 22 | #define VERSION "VERSION\r\n" /* get version number */ |
b9feeb00 SL |
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; | |
b9feeb00 SL |
37 | char *host; |
38 | register struct hostent *hp; | |
39 | struct servent *sp; | |
329362bd | 40 | int version = 0; |
b949b1b9 SL |
41 | int beginseen = 0; |
42 | int endseen = 0; | |
b9feeb00 SL |
43 | |
44 | argv++, argc--; | |
329362bd RC |
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 | } | |
b9feeb00 | 52 | if (argc < 1 || argc > 2) { |
329362bd | 53 | fprintf(stderr, "usage: gettable [-v] host [ file ]\n"); |
b9feeb00 SL |
54 | exit(1); |
55 | } | |
e0754ba7 | 56 | sp = getservbyname("hostnames", "tcp"); |
b9feeb00 | 57 | if (sp == NULL) { |
e0754ba7 | 58 | fprintf(stderr, "gettable: hostnames/tcp: unknown service\n"); |
b9feeb00 SL |
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; | |
f5bcb151 | 72 | s = socket(hp->h_addrtype, SOCK_STREAM, 0); |
b9feeb00 SL |
73 | if (s < 0) { |
74 | perror("gettable: socket"); | |
75 | exit(4); | |
76 | } | |
f5bcb151 | 77 | if (bind(s, &sin, sizeof (sin)) < 0) { |
b9feeb00 SL |
78 | perror("gettable: bind"); |
79 | exit(5); | |
80 | } | |
f5bcb151 | 81 | bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length); |
b9feeb00 | 82 | sin.sin_port = sp->s_port; |
f5bcb151 | 83 | if (connect(s, &sin, sizeof (sin)) < 0) { |
b9feeb00 SL |
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 | } | |
329362bd | 101 | fprintf(sfo, version ? VERSION : QUERY); |
b9feeb00 SL |
102 | fflush(sfo); |
103 | while (fgets(buf, sizeof(buf), sfi) != NULL) { | |
104 | len = strlen(buf); | |
105 | buf[len-2] = '\0'; | |
b949b1b9 SL |
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++; | |
b9feeb00 SL |
113 | continue; |
114 | } | |
b949b1b9 SL |
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++; | |
b9feeb00 SL |
122 | continue; |
123 | } | |
b949b1b9 SL |
124 | if (equaln(buf, "ERR", 3)) { |
125 | fprintf(stderr, | |
126 | "gettable: hostname service error: %s", buf); | |
127 | exit(92); | |
128 | } | |
b9feeb00 SL |
129 | fprintf(hf, "%s\n", buf); |
130 | } | |
131 | fclose(hf); | |
b949b1b9 SL |
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 | } | |
329362bd | 141 | fprintf(stderr, "Host table received.\n"); |
b949b1b9 SL |
142 | } else |
143 | fprintf(stderr, "Version number received.\n"); | |
b9feeb00 SL |
144 | close(s); |
145 | fprintf(stderr, "Connection to %s closed\n", host); | |
329362bd | 146 | exit(0); |
b9feeb00 | 147 | } |