-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalidateconf.c
233 lines (203 loc) · 6.46 KB
/
validateconf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
VALIDATECONF - Part of the tnat64 package
This utility can be used to validate the tnat64.conf
configuration file
Copyright (C) 2000 Shaun Clowes
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
/* Global configuration variables */
static const char *progname = "tnat64-validateconf"; /* Name for error msgs */
/* Header Files */
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <common.h>
#include <parser.h>
void show_prefix(struct parsedfile *, struct prefixent *, int);
void show_conf(struct parsedfile *config);
void test_host(struct parsedfile *config, char *);
int main(int argc, char *argv[])
{
char *usage = "Usage: [-f conf file] [-t hostname/ip[:port]]";
char *filename = NULL;
char *testhost = NULL;
struct parsedfile config;
int i;
set_log_options(MSGERR, progname, NULL, 0);
if ((argc > 5) || (((argc - 1) % 2) != 0))
{
show_msg(MSGERR, "Invalid number of arguments\n");
show_msg(MSGERR, "%s\n", usage);
exit(1);
}
for (i = 1; i < argc; i = i + 2)
{
if (!strcmp(argv[i], "-f"))
{
filename = argv[(i + 1)];
}
else if (!strcmp(argv[i], "-t"))
{
testhost = argv[(i + 1)];
}
else
{
show_msg(MSGERR, "Unknown option %s\n", argv[i]);
show_msg(MSGERR, "%s\n", usage);
exit(1);
}
}
if (!filename)
filename = strdup(CONF_FILE);
printf("Reading configuration file %s...\n", filename);
if (read_config(filename, &config) == 0)
printf("... Read complete\n\n");
else
exit(1);
/* If they specified a test host, test it, otherwise */
/* dump the configuration */
if (!testhost)
show_conf(&config);
else
test_host(&config, testhost);
return (0);
}
void test_host(struct parsedfile *config, char *host)
{
struct in_addr hostaddr;
struct prefixent *path;
char *hostname, *port;
char separator;
unsigned long portno = 0;
/* See if a port has been specified */
hostname = strsplit(&separator, &host, ": \t\n");
if (separator == ':')
{
port = strsplit(NULL, &host, " \t\n");
if (port)
portno = strtol(port, NULL, 0);
}
/* First resolve the host to an ip */
if ((hostaddr.s_addr = resolve_ip(hostname, 0, 1)) == -1)
{
fprintf(stderr, "Error: Cannot resolve %s\n", host);
return;
}
else
{
printf("Finding path for %s...\n", inet_ntoa(hostaddr));
if (!(is_local(config, &(hostaddr))))
{
printf("Path is local\n");
}
else
{
pick_prefix(config, &path, &hostaddr, portno);
if (path == &(config->defaultprefix))
{
printf("Path is reached via default NAT64 prefix:\n");
show_prefix(config, path, 1);
}
else
{
printf("Host is reached via this path:\n");
show_prefix(config, path, 0);
}
}
}
return;
}
void show_conf(struct parsedfile *config)
{
struct netent *net;
struct prefixent *prefix;
/* Show the local networks */
printf("=== Local networks (no NAT64 needed) ===\n");
net = (config->localnets);
while (net != NULL)
{
printf("Network: %15s ", inet_ntoa(net->localip));
printf("Netmask: %15s\n", inet_ntoa(net->localnet));
net = net->next;
}
printf("\n");
/* If we have a default prefix configuration show it */
printf("=== Default NAT64 prefix configuration ===\n");
if ((config->defaultprefix).address != NULL)
{
show_prefix(config, &(config->defaultprefix), 1);
}
else
{
printf("No default NAT64 prefix specified, this is rarely a " "good idea\n");
}
printf("\n");
/* Now show paths */
if ((config->paths) != NULL)
{
prefix = (config->paths);
while (prefix != NULL)
{
printf("=== Path (line no %d in configuration file)" " ===\n", prefix->lineno);
show_prefix(config, prefix, 0);
printf("\n");
prefix = prefix->next;
}
}
return;
}
void show_prefix(struct parsedfile *config, struct prefixent *prefix, int def)
{
struct netent *net;
/* Show address */
if (prefix->address != NULL)
printf("NAT64 prefix: %s\n", prefix->address);
else
printf("NAT64 prefix: ERROR! None specified\n");
/* If this is the default servers and it has reachnets, thats stupid */
if (def)
{
if (prefix->reachnets != NULL)
{
fprintf(stderr, "Error: The default NAT64 prefix has "
"specified networks it can be used to reach (subnet statements), "
"these statements are ignored since the " "default NAT64 prefix will be used for any network " "which is not specified in a subnet statement " "for other prefixes\n");
}
}
else if (prefix->reachnets == NULL)
{
fprintf(stderr, "Error: No subnet statements specified for " "this NAT64 prefix, it will never be used\n");
}
else
{
printf("This NAT64 prefix can be used to reach:\n");
net = prefix->reachnets;
while (net != NULL)
{
printf("Network: %15s ", inet_ntoa(net->localip));
printf("Netmask: %15s ", inet_ntoa(net->localnet));
if (net->startport)
printf("Ports: %5lu - %5lu", net->startport, net->endport);
printf("\n");
net = net->next;
}
}
}