OpenVPN
test_networking.c
Go to the documentation of this file.
1#include "config.h"
2#include "syshead.h"
3#include "networking.h"
4
5#include <assert.h>
6
7static char *iface = "ovpn-dummy0";
8
9static int
11{
12 printf("CMD: ip link set %s %s\n", iface, up ? "up" : "down");
13
14 return net_iface_up(NULL, iface, up);
15}
16
17static int
18net__iface_new(const char *name, const char *type)
19{
20 return net_iface_new(NULL, name, type, NULL);
21}
22
23static int
24net__iface_type(const char *name, const char *type)
25{
26 char ret_type[IFACE_TYPE_LEN_MAX];
27 int ret = net_iface_type(NULL, name, ret_type);
28 if (ret == 0)
29 {
30 assert(strcmp(type, ret_type) == 0);
31 }
32
33 return ret;
34}
35
36static int
37net__iface_del(const char *name)
38{
39 return net_iface_del(NULL, name);
40}
41
42static int
44{
45 printf("CMD: ip link set %s mtu %d\n", iface, mtu);
46
47 return net_iface_mtu_set(NULL, iface, mtu);
48}
49
50static int
51net__addr_v4_add(const char *addr_str, int prefixlen)
52{
53 in_addr_t addr;
54 int ret;
55
56 ret = inet_pton(AF_INET, addr_str, &addr);
57 if (ret != 1)
58 {
59 return -1;
60 }
61
62 addr = ntohl(addr);
63
64 printf("CMD: ip addr add %s/%d dev %s\n", addr_str, prefixlen, iface);
65
66 return net_addr_v4_add(NULL, iface, &addr, prefixlen);
67}
68
69static int
70net__addr_v6_add(const char *addr_str, int prefixlen)
71{
72 struct in6_addr addr;
73 int ret;
74
75 ret = inet_pton(AF_INET6, addr_str, &addr);
76 if (ret != 1)
77 {
78 return -1;
79 }
80
81 printf("CMD: ip -6 addr add %s/%d dev %s\n", addr_str, prefixlen, iface);
82
83 return net_addr_v6_add(NULL, iface, &addr, prefixlen);
84}
85
86static int
87net__route_v4_add(const char *dst_str, int prefixlen, int metric)
88{
89 in_addr_t dst;
90 int ret;
91
92 if (!dst_str)
93 {
94 return -1;
95 }
96
97 ret = inet_pton(AF_INET, dst_str, &dst);
98 if (ret != 1)
99 {
100 return -1;
101 }
102
103 dst = ntohl(dst);
104
105 printf("CMD: ip route add %s/%d dev %s", dst_str, prefixlen, iface);
106 if (metric > 0)
107 {
108 printf(" metric %d", metric);
109 }
110 printf("\n");
111
112 return net_route_v4_add(NULL, &dst, prefixlen, NULL, iface, 0, metric);
113}
114
115static int
116net__route_v4_add_gw(const char *dst_str, int prefixlen, const char *gw_str, int metric)
117{
118 in_addr_t dst, gw;
119 int ret;
120
121 if (!dst_str || !gw_str)
122 {
123 return -1;
124 }
125
126 ret = inet_pton(AF_INET, dst_str, &dst);
127 if (ret != 1)
128 {
129 return -1;
130 }
131
132 ret = inet_pton(AF_INET, gw_str, &gw);
133 if (ret != 1)
134 {
135 return -1;
136 }
137
138 dst = ntohl(dst);
139 gw = ntohl(gw);
140
141 printf("CMD: ip route add %s/%d dev %s via %s", dst_str, prefixlen, iface, gw_str);
142 if (metric > 0)
143 {
144 printf(" metric %d", metric);
145 }
146 printf("\n");
147
148 return net_route_v4_add(NULL, &dst, prefixlen, &gw, iface, 0, metric);
149}
150
151static int
152net__route_v6_add(const char *dst_str, int prefixlen, int metric)
153{
154 struct in6_addr dst;
155 int ret;
156
157 if (!dst_str)
158 {
159 return -1;
160 }
161
162 ret = inet_pton(AF_INET6, dst_str, &dst);
163 if (ret != 1)
164 {
165 return -1;
166 }
167
168 printf("CMD: ip -6 route add %s/%d dev %s", dst_str, prefixlen, iface);
169 if (metric > 0)
170 {
171 printf(" metric %d", metric);
172 }
173 printf("\n");
174
175 return net_route_v6_add(NULL, &dst, prefixlen, NULL, iface, 0, metric);
176}
177
178static int
179net__route_v6_add_gw(const char *dst_str, int prefixlen, const char *gw_str, int metric)
180{
181 struct in6_addr dst, gw;
182 int ret;
183
184 if (!dst_str || !gw_str)
185 {
186 return -1;
187 }
188
189 ret = inet_pton(AF_INET6, dst_str, &dst);
190 if (ret != 1)
191 {
192 return -1;
193 }
194
195 ret = inet_pton(AF_INET6, gw_str, &gw);
196 if (ret != 1)
197 {
198 return -1;
199 }
200
201 printf("CMD: ip -6 route add %s/%d dev %s via %s", dst_str, prefixlen, iface, gw_str);
202 if (metric > 0)
203 {
204 printf(" metric %d", metric);
205 }
206 printf("\n");
207
208 return net_route_v6_add(NULL, &dst, prefixlen, &gw, iface, 0, metric);
209}
210
211static void
212usage(char *name)
213{
214 printf("Usage: %s <0-8>\n", name);
215}
216
217int
218main(int argc, char *argv[])
219{
220 int test;
221
222 if (argc < 2)
223 {
224 usage(argv[0]);
225 return -1;
226 }
227
228 /* the t_net script can use this command to perform a dry-run test */
229 if (strcmp(argv[1], "test") == 0)
230 {
231 return 0;
232 }
233
234 if (argc > 3)
235 {
236 iface = argv[2];
237 }
238
239 test = atoi(argv[1]);
240 switch (test)
241 {
242 case 0:
243 return net__iface_up(true);
244
245 case 1:
246 return net__iface_mtu_set(1281);
247
248 case 2:
249 return net__addr_v4_add("10.255.255.1", 24);
250
251 case 3:
252 return net__addr_v6_add("2001::1", 64);
253
254 case 4:
255 return net__route_v4_add("11.11.11.0", 24, 0);
256
257 case 5:
258 return net__route_v4_add_gw("11.11.12.0", 24, "10.255.255.2", 0);
259
260 case 6:
261 return net__route_v6_add("2001:babe:cafe:babe::", 64, 600);
262
263 case 7:
264 return net__route_v6_add_gw("2001:cafe:babe::", 48, "2001::2", 600);
265
266 /* following tests are standalone and do not print any CMD= */
267 case 8:
268 assert(net__iface_new("dummy0815", "dummy") == 0);
269 assert(net__iface_type("dummy0815", "dummy") == 0);
270 assert(net__iface_del("dummy0815") == 0);
271 assert(net__iface_type("dummy0815", NULL) == -ENODEV);
272 return 0;
273
274 default:
275 printf("invalid test: %d\n", test);
276 break;
277 }
278
279 usage(argv[0]);
280 return -1;
281}
#define IFACE_TYPE_LEN_MAX
Definition networking.h:25
int main(void)
static void usage(void)
Definition options.c:4833
Definition argv.h:35
static int net__route_v6_add_gw(const char *dst_str, int prefixlen, const char *gw_str, int metric)
static int net__addr_v6_add(const char *addr_str, int prefixlen)
static int net__iface_mtu_set(int mtu)
static int net__route_v4_add(const char *dst_str, int prefixlen, int metric)
static int net__iface_del(const char *name)
static int net__iface_new(const char *name, const char *type)
static int net__route_v4_add_gw(const char *dst_str, int prefixlen, const char *gw_str, int metric)
static int net__route_v6_add(const char *dst_str, int prefixlen, int metric)
static int net__addr_v4_add(const char *addr_str, int prefixlen)
static int net__iface_type(const char *name, const char *type)
static char * iface
static int net__iface_up(bool up)