aboutsummaryrefslogtreecommitdiffstats
path: root/netlink_race_test.c
diff options
context:
space:
mode:
authorThomas Gschwantner <tharre3@gmail.com>2019-09-29 11:58:56 +0200
committerThomas Gschwantner <tharre3@gmail.com>2019-09-29 11:58:56 +0200
commit798e822030728c56bbb07f4444bdf74771ebeb5b (patch)
treec724162f26f6b950c12e150e7f30866a4b151772 /netlink_race_test.c
parentRename struct ip_pool to be more descriptive (diff)
downloadwg-dynamic-798e822030728c56bbb07f4444bdf74771ebeb5b.tar.xz
wg-dynamic-798e822030728c56bbb07f4444bdf74771ebeb5b.zip
Netlink race testtg/netlink_race
Diffstat (limited to 'netlink_race_test.c')
-rw-r--r--netlink_race_test.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/netlink_race_test.c b/netlink_race_test.c
new file mode 100644
index 0000000..8ffe445
--- /dev/null
+++ b/netlink_race_test.c
@@ -0,0 +1,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;
+}