summaryrefslogtreecommitdiff
path: root/tests/unit_tests/openvpn/test_networking.c
blob: e7c148f0e9cc55a24841a3c741809eefc0ec8832 (plain)
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
#include "config.h"
#include "syshead.h"
#include "networking.h"


static char *iface = "dummy0";

static int
net__iface_up(bool up)
{
    printf("CMD: ip link set %s %s\n", iface, up ? "up" : "down");

    return net_iface_up(NULL, iface, up);
}

static int
net__iface_mtu_set(int mtu)
{
    printf("CMD: ip link set %s mtu %d\n", iface, mtu);

    return net_iface_mtu_set(NULL, iface, mtu);
}

static int
net__addr_v4_add(const char *addr_str, int prefixlen)
{
    in_addr_t addr;
    int ret;

    ret = inet_pton(AF_INET, addr_str, &addr);
    if (ret != 1)
    {
        return -1;
    }

    addr = ntohl(addr);

    printf("CMD: ip addr add %s/%d dev %s\n", addr_str, prefixlen, iface);

    return net_addr_v4_add(NULL, iface, &addr, prefixlen);
}

static int
net__addr_v6_add(const char *addr_str, int prefixlen)
{
    struct in6_addr addr;
    int ret;

    ret = inet_pton(AF_INET6, addr_str, &addr);
    if (ret != 1)
    {
        return -1;
    }

    printf("CMD: ip -6 addr add %s/%d dev %s\n", addr_str, prefixlen, iface);

    return net_addr_v6_add(NULL, iface, &addr, prefixlen);
}

static int
net__route_v4_add(const char *dst_str, int prefixlen, int metric)
{
    in_addr_t dst;
    int ret;

    if (!dst_str)
    {
        return -1;
    }

    ret = inet_pton(AF_INET, dst_str, &dst);
    if (ret != 1)
    {
        return -1;
    }

    dst = ntohl(dst);

    printf("CMD: ip route add %s/%d dev %s", dst_str, prefixlen, iface);
    if (metric > 0)
    {
        printf(" metric %d", metric);
    }
    printf("\n");

    return net_route_v4_add(NULL, &dst, prefixlen, NULL, iface, 0, metric);

}

static int
net__route_v4_add_gw(const char *dst_str, int prefixlen, const char *gw_str,
                     int metric)
{
    in_addr_t dst, gw;
    int ret;

    if (!dst_str || !gw_str)
    {
        return -1;
    }

    ret = inet_pton(AF_INET, dst_str, &dst);
    if (ret != 1)
    {
        return -1;
    }

    ret = inet_pton(AF_INET, gw_str, &gw);
    if (ret != 1)
    {
        return -1;
    }

    dst = ntohl(dst);
    gw = ntohl(gw);

    printf("CMD: ip route add %s/%d dev %s via %s", dst_str, prefixlen, iface,
           gw_str);
    if (metric > 0)
    {
        printf(" metric %d", metric);
    }
    printf("\n");

    return net_route_v4_add(NULL, &dst, prefixlen, &gw, iface, 0, metric);
}

static int
net__route_v6_add(const char *dst_str, int prefixlen, int metric)
{
    struct in6_addr dst;
    int ret;

    if (!dst_str)
    {
        return -1;
    }

    ret = inet_pton(AF_INET6, dst_str, &dst);
    if (ret != 1)
    {
        return -1;
    }

    printf("CMD: ip -6 route add %s/%d dev %s", dst_str, prefixlen, iface);
    if (metric > 0)
    {
        printf(" metric %d", metric);
    }
    printf("\n");

    return net_route_v6_add(NULL, &dst, prefixlen, NULL, iface, 0, metric);

}

static int
net__route_v6_add_gw(const char *dst_str, int prefixlen, const char *gw_str,
                     int metric)
{
    struct in6_addr dst, gw;
    int ret;

    if (!dst_str || !gw_str)
    {
        return -1;
    }

    ret = inet_pton(AF_INET6, dst_str, &dst);
    if (ret != 1)
    {
        return -1;
    }

    ret = inet_pton(AF_INET6, gw_str, &gw);
    if (ret != 1)
    {
        return -1;
    }

    printf("CMD: ip -6 route add %s/%d dev %s via %s", dst_str, prefixlen,
           iface, gw_str);
    if (metric > 0)
    {
        printf(" metric %d", metric);
    }
    printf("\n");

    return net_route_v6_add(NULL, &dst, prefixlen, &gw, iface, 0, metric);
}

static void
usage(char *name)
{
    printf("Usage: %s <0-7>\n", name);
}

int
main(int argc, char *argv[])
{
    int test;

    if (argc < 2)
    {
        usage(argv[0]);
        return -1;
    }

    /* the t_net script can use this command to perform a dry-run test */
    if (strcmp(argv[1], "test") == 0)
    {
        return 0;
    }

    if (argc > 3)
    {
        iface = argv[2];
    }

    test = atoi(argv[1]);
    switch (test)
    {
        case 0:
            return net__iface_up(true);

        case 1:
            return net__iface_mtu_set(1281);

        case 2:
            return net__addr_v4_add("10.255.255.1", 24);

        case 3:
            return net__addr_v6_add("2001::1", 64);

        case 4:
            return net__route_v4_add("11.11.11.0", 24, 0);

        case 5:
            return net__route_v4_add_gw("11.11.12.0", 24, "10.255.255.2", 0);

        case 6:
            return net__route_v6_add("2001:babe:cafe:babe::", 64, 600);

        case 7:
            return net__route_v6_add_gw("2001:cafe:babe::", 48, "2001::2", 600);

        default:
            printf("invalid test: %d\n", test);
            break;
    }

    usage(argv[0]);
    return -1;
}