aboutsummaryrefslogtreecommitdiffstats
path: root/netlink_race_test.c
blob: 8ffe4452b0042cb295d678c86efd131e9c0b9e14 (plain) (blame)
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
#include <stdio.h>
#include <string.h>

#include "netlink.h"
#include "ip_util.h"

static void add_allowed_ips(wg_key pubkey, struct in_addr *ipv4,
			    struct in6_addr *ipv6)
{
	wg_allowedip allowed_v4, allowed_v6;
	wg_peer peer = { .flags = WGPEER_NO_CREATE };
	wg_device dev = { .first_peer = &peer };

	strcpy(dev.name, "wg0");
	memcpy(peer.public_key, pubkey, sizeof peer.public_key);
	wg_allowedip **cur = &peer.first_allowedip;

	if (ipv4) {
		allowed_v4 = (wg_allowedip){
			.family = AF_INET,
			.cidr = 32,
			.ip4 = *ipv4,
		};
		*cur = &allowed_v4;
		cur = &allowed_v4.next_allowedip;
	}

	if (ipv6) {
		allowed_v6 = (wg_allowedip){
			.family = AF_INET6,
			.cidr = 128,
			.ip6 = *ipv6,
		};
		*cur = &allowed_v6;
	}

	if (wg_set_device(&dev))
		perror("wg_set_device()");
}

int main(void)
{
	struct wg_device *device;
	if (wg_get_device(&device, "wg0")) {
		perror("Unable to access interface wg0");
		return 1;
	}

	if (!device->first_peer) {
		wg_free_device(device);
		return 1;
	}

	wg_key_b64_string str;
	wg_key_to_base64(str, device->first_peer->public_key);
	printf("Public key: %s\n", str);

	char cmd[4096];
	sprintf(cmd, "wg set wg0 peer %s remove", str);
	system(cmd);

	add_allowed_ips(device->first_peer->public_key, ip4_from("192.168.1.1"),
			NULL);

	wg_free_device(device);
	return 0;
}