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}
115
116static int
117net__route_v4_add_gw(const char *dst_str, int prefixlen, const char *gw_str,
118 int metric)
119{
120 in_addr_t dst, gw;
121 int ret;
122
123 if (!dst_str || !gw_str)
124 {
125 return -1;
126 }
127
128 ret = inet_pton(AF_INET, dst_str, &dst);
129 if (ret != 1)
130 {
131 return -1;
132 }
133
134 ret = inet_pton(AF_INET, gw_str, &gw);
135 if (ret != 1)
136 {
137 return -1;
138 }
139
140 dst = ntohl(dst);
141 gw = ntohl(gw);
142
143 printf("CMD: ip route add %s/%d dev %s via %s", dst_str, prefixlen, iface,
144 gw_str);
145 if (metric > 0)
146 {
147 printf(" metric %d", metric);
148 }
149 printf("\n");
150
151 return net_route_v4_add(NULL, &dst, prefixlen, &gw, iface, 0, metric);
152}
153
154static int
155net__route_v6_add(const char *dst_str, int prefixlen, int metric)
156{
157 struct in6_addr dst;
158 int ret;
159
160 if (!dst_str)
161 {
162 return -1;
163 }
164
165 ret = inet_pton(AF_INET6, dst_str, &dst);
166 if (ret != 1)
167 {
168 return -1;
169 }
170
171 printf("CMD: ip -6 route add %s/%d dev %s", dst_str, prefixlen, iface);
172 if (metric > 0)
173 {
174 printf(" metric %d", metric);
175 }
176 printf("\n");
177
178 return net_route_v6_add(NULL, &dst, prefixlen, NULL, iface, 0, metric);
179
180}
181
182static int
183net__route_v6_add_gw(const char *dst_str, int prefixlen, const char *gw_str,
184 int metric)
185{
186 struct in6_addr dst, gw;
187 int ret;
188
189 if (!dst_str || !gw_str)
190 {
191 return -1;
192 }
193
194 ret = inet_pton(AF_INET6, dst_str, &dst);
195 if (ret != 1)
196 {
197 return -1;
198 }
199
200 ret = inet_pton(AF_INET6, gw_str, &gw);
201 if (ret != 1)
202 {
203 return -1;
204 }
205
206 printf("CMD: ip -6 route add %s/%d dev %s via %s", dst_str, prefixlen,
207 iface, gw_str);
208 if (metric > 0)
209 {
210 printf(" metric %d", metric);
211 }
212 printf("\n");
213
214 return net_route_v6_add(NULL, &dst, prefixlen, &gw, iface, 0, metric);
215}
216
217static void
218usage(char *name)
219{
220 printf("Usage: %s <0-8>\n", name);
221}
222
223int
224main(int argc, char *argv[])
225{
226 int test;
227
228 if (argc < 2)
229 {
230 usage(argv[0]);
231 return -1;
232 }
233
234 /* the t_net script can use this command to perform a dry-run test */
235 if (strcmp(argv[1], "test") == 0)
236 {
237 return 0;
238 }
239
240 if (argc > 3)
241 {
242 iface = argv[2];
243 }
244
245 test = atoi(argv[1]);
246 switch (test)
247 {
248 case 0:
249 return net__iface_up(true);
250
251 case 1:
252 return net__iface_mtu_set(1281);
253
254 case 2:
255 return net__addr_v4_add("10.255.255.1", 24);
256
257 case 3:
258 return net__addr_v6_add("2001::1", 64);
259
260 case 4:
261 return net__route_v4_add("11.11.11.0", 24, 0);
262
263 case 5:
264 return net__route_v4_add_gw("11.11.12.0", 24, "10.255.255.2", 0);
265
266 case 6:
267 return net__route_v6_add("2001:babe:cafe:babe::", 64, 600);
268
269 case 7:
270 return net__route_v6_add_gw("2001:cafe:babe::", 48, "2001::2", 600);
271
272 /* following tests are standalone and do not print any CMD= */
273 case 8:
274 assert(net__iface_new("dummy0815", "dummy") == 0);
275 assert(net__iface_type("dummy0815", "dummy") == 0);
276 assert(net__iface_del("dummy0815") == 0);
277 assert(net__iface_type("dummy0815", NULL) == -ENODEV);
278 return 0;
279
280 default:
281 printf("invalid test: %d\n", test);
282 break;
283 }
284
285 usage(argv[0]);
286 return -1;
287}
#define IFACE_TYPE_LEN_MAX
Definition networking.h:26
int main(void)
static void usage(void)
Definition options.c:4934
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)