-
Notifications
You must be signed in to change notification settings - Fork 3
/
amprroute.c
79 lines (67 loc) · 1.49 KB
/
amprroute.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
/*
* A program to manually add or remove tunnel routes. route(8)
* is not sufficiently expressive to do this.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "dat.h"
#include "fns.h"
int
main(int argc, char *argv[])
{
Route route;
Tunnel tunnel;
struct in_addr addr;
char *slash, *net, *ifname;
unsigned int cidr;
uint32_t netmask;
int ch, rdomain;
rdomain = 0;
while ((ch = getopt(argc, argv, "D:?h")) != -1) {
switch (ch) {
case 'D':
rdomain = strnum(optarg);
break;
case '?':
case 'h':
default:
fatal("usage: amprroute network/cidr ifname");
break;
}
}
argc -= optind;
argv += optind;
initlog();
initsys(rdomain);
if (argc != 2)
fatal("usage: amprroute network/cidr ifname");
net = argv[0];
ifname = argv[1];
slash = strchr(net, '/');
if (slash == NULL)
fatal("network missing CIDR");
*slash++ = '\0';
cidr = strnum(slash);
netmask = cidr2netmask(cidr);
memset(&addr, 0, sizeof(addr));
if (inet_pton(AF_INET, net, &addr) <= 0)
fatal("cannot parse network: %s", net);
memset(&route, 0, sizeof(route));
route.subnetmask = netmask;
route.ipnet = ntohl(addr.s_addr);
memset(&tunnel, 0, sizeof(tunnel));
strlcpy(tunnel.ifname, ifname, sizeof(tunnel.ifname));
addroute(&route, &tunnel, rdomain);
return 0;
}