Repository navigation
Expand file tree
/
Copy pathadd_route.c
More file actions
executable file
·347 lines (315 loc) · 6.74 KB
/
Copy pathadd_route.c
File metadata and controls
executable file
·347 lines (315 loc) · 6.74 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
add_route.c
Created August 7, 1991 by Philip Homburg
*/
#define _POSIX_C_SOURCE 2
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/hton.h>
#include <net/netlib.h>
#include <net/gen/netdb.h>
#include <net/gen/in.h>
#include <net/gen/inet.h>
#include <net/gen/route.h>
#include <net/gen/socket.h>
#include <net/gen/ip_io.h>
static char *prog_name;
static enum { ADD, DEL } action;
static void usage(void);
static int name_to_ip(char *name, ipaddr_t *addr);
static int parse_cidr(char *cidr, ipaddr_t *addr, ipaddr_t *mask);
int main(int argc, char *argv[])
{
struct netent *netent;
ipaddr_t gateway, destination, netmask, defaultmask=0;
u8_t high_byte;
nwio_route_t route;
int ip_fd, itab;
int r;
int metric;
char *check;
char *ip_device;
char *netmask_str, *metric_str, *destination_str, *gateway_str;
int c;
char *d_arg, *g_arg, *m_arg, *n_arg, *I_arg;
int i_flag, o_flag, D_flag, v_flag;
int cidr;
prog_name= strrchr(argv[0], '/');
if (prog_name == NULL) prog_nam
10BC8
e= argv[0]; else prog_name++;
if (strcmp(prog_name, "add_route") == 0)
action= ADD;
else if (strcmp(prog_name, "del_route") == 0)
action= DEL;
else
{
fprintf(stderr, "Don't know what to do when named '%s'\n",
prog_name);
exit(1);
}
i_flag= 0;
o_flag= 0;
D_flag= 0;
v_flag= 0;
g_arg= NULL;
d_arg= NULL;
m_arg= NULL;
n_arg= NULL;
I_arg= NULL;
while ((c= getopt(argc, argv, "iovDg:d:m:n:I:?")) != -1)
{
switch(c)
{
case 'i':
if (i_flag)
usage();
i_flag= 1;
break;
case 'o':
if (o_flag)
usage();
o_flag= 1;
break;
case 'v':
if (v_flag)
usage();
v_flag= 1;
break;
case 'D':
if (D_flag)
usage();
D_flag= 1;
break;
case 'g':
if (g_arg)
usage();
g_arg= optarg;
break;
case 'd':
if (d_arg)
usage();
d_arg= optarg;
break;
case 'm':
if (m_arg)
usage();
m_arg= optarg;
break;
case 'n':
if (n_arg)
usage();
n_arg= optarg;
break;
case 'I':
if (I_arg)
usage();
I_arg= optarg;
break;
case '?':
usage();
default:
fprintf(stderr, "%s: getopt failed\n", prog_name);
exit(1);
}
}
if (optind != argc)
usage();
if (i_flag && o_flag)
usage();
itab= i_flag;
if (i_flag)
{
if (g_arg == NULL || d_arg == NULL || m_arg == NULL)
usage();
}
else
{
if (g_arg == NULL || (d_arg == NULL && n_arg != NULL))
{
usage();
}
}
gateway_str= g_arg;
destination_str= d_arg;
metric_str= m_arg;
netmask_str= n_arg;
ip_device= I_arg;
if (!name_to_ip(gateway_str, &gateway))
{
fprintf(stderr, "%s: unknown host '%s'\n", prog_name,
gateway_str);
exit(1);
}
destination= 0;
netmask= 0;
cidr= 0;
if (destination_str)
{
if (parse_cidr(destination_str, &destination, &netmask))
cidr= 1;
else if (inet_aton(destination_str, &destination))
;
else if ((netent= getnetbyname(destination_str)) != NULL)
destination= netent->n_net;
else if (!name_to_ip(destination_str, &destination))
{
fprintf(stderr, "%s: unknown network/host '%s'\n",
prog_name, destination_str);
exit(1);
}
high_byte= *(u8_t *)&destination;
if (!(high_byte & 0x80)) /* class A or 0 */
{
if (destination)
defaultmask= HTONL(0xff000000);
}
else if (!(high_byte & 0x40)) /* class B */
{
defaultmask= HTONL(0xffff0000);
}
else if (!(high_byte & 0x20)) /* class C */
{
defaultmask= HTONL(0xffffff00);
}
else /* class D is multicast ... */
{
fprintf(stderr, "%s: Warning: Martian address '%s'\n",
prog_name, inet_ntoa(destination));
defaultmask= HTONL(0xffffffff);
}
if (destination & ~defaultmask)
{
/* host route */
defaultmask= HTONL(0xffffffff);
}
if (!cidr)
netmask= defaultmask;
}
if (netmask_str)
{
if (cidr)
usage();
if (inet_aton(netmask_str, &netmask) == 0)
{
fprintf(stderr, "%s: illegal netmask'%s'\n", prog_name,
netmask_str);
exit(1);
}
}
if (metric_str)
{
metric= strtol(metric_str, &check, 0);
if (check[0] != '\0' || metric < 1)
{
fprintf(stderr, "%s: illegal metric %s\n",
prog_name, metric_str);
}
}
else
metric= 1;
if (!ip_device)
ip_device= getenv("IP_DEVICE");
if (!ip_device)
ip_device= IP_DEVICE;
ip_fd= open(ip_device, O_RDWR);
if (ip_fd == -1)
{
fprintf(stderr, "%s: unable to open('%s'): %s\n",
prog_name, ip_device, strerror(errno));
exit(1);
}
if (v_flag)
{
printf("%s %s route to %s ",
action == ADD ? "adding" : "deleting",
itab ? "input" : "output",
inet_ntoa(destination));
printf("with netmask %s ", inet_ntoa(netmask));
printf("using gateway %s", inet_ntoa(gateway));
if (itab && action == ADD)
printf(" at distance %d", metric);
printf("\n");
}
route.nwr_ent_no= 0;
route.nwr_dest= destination;
route.nwr_netmask= netmask;
route.nwr_gateway= gateway;
route.nwr_dist= action == ADD ? metric : 0;
route.nwr_flags= (action == DEL && D_flag) ? 0 : NWRF_STATIC;
route.nwr_pref= 0;
route.nwr_mtu= 0;
if (action == ADD)
r= ioctl(ip_fd, itab ? NWIOSIPIROUTE : NWIOSIPOROUTE, &route);
else
r= ioctl(ip_fd, itab ? NWIODIPIROUTE : NWIODIPOROUTE, &route);
if (r == -1)
{
fprintf(stderr, "%s: NWIO%cIP%cROUTE: %s\n",
prog_name,
action == ADD ? 'S' : 'D',
itab ? 'I' : 'O',
strerror(errno));
exit(1);
}
return(0);
}
static void usage(void)
{
fprintf(stderr,
"Usage: %s\n"
"\t[-o] %s-g gw [-d dst [-n netmask]] %s[-I ipdev] [-v]\n"
"\t-i %s-g gw -d dst [-n netmask] %s[-I ipdev] [-v]\n"
"Note: <dst> may be in CIDR notation\n",
prog_name,
action == DEL ? "[-D] " : "",
action == ADD ? "[-m metric] " : "",
action == DEL ? "[-D] " : "",
action == ADD ? "-m metric " : ""
);
exit(1);
}
static int name_to_ip(char *name, ipaddr_t *addr)
{
/* Translate a name to an IP address. Try first with inet_aton(), then
* with gethostbyname(). (The latter can also recognize an IP address,
* but only decimals with at least one dot).)
*/
struct hostent *hostent;
if (!inet_aton(name, addr)) {
if ((hostent= gethostbyname(name)) == NULL) return 0;
if (hostent->h_addrtype != AF_INET) return 0;
if (hostent->h_length != sizeof(*addr)) return 0;
memcpy(addr, hostent->h_addr, sizeof(*addr));
}
return 1;
}
static int parse_cidr(char *cidr, ipaddr_t *addr, ipaddr_t *mask)
{
char *slash, *check;
ipaddr_t a;
int ok;
unsigned long len;
if ((slash= strchr(cidr, '/')) == NULL)
return 0;
*slash++= 0;
ok= 1;
if (!inet_aton(cidr, &a))
ok= 0;
len= strtoul(slash, &check, 10);
if (check == slash || *check != 0 || len > 32)
ok= 0;
*--slash= '/';
if (!ok)
return 0;
*addr= a;
*mask= htonl(len == 0 ? 0 : (0xFFFFFFFF << (32-len)) & 0xFFFFFFFF);
return 1;
}
/*
* $PchId: add_route.c,v 1.6 2001/04/20 10:45:07 philip Exp $
*/