aboutsummaryrefslogtreecommitdiffstats
path: root/wireguard-go-bridge/example.c
diff options
context:
space:
mode:
Diffstat (limited to 'wireguard-go-bridge/example.c')
-rw-r--r--wireguard-go-bridge/example.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/wireguard-go-bridge/example.c b/wireguard-go-bridge/example.c
new file mode 100644
index 0000000..912d181
--- /dev/null
+++ b/wireguard-go-bridge/example.c
@@ -0,0 +1,46 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
+ */
+
+#include "wireguard.h"
+#include <stdio.h>
+#include <stdbool.h>
+#include <unistd.h>
+
+static struct {
+ int something;
+} ctx;
+
+static bool is_closed = false;
+
+ssize_t do_read(const void *ctx, const unsigned char *buf, size_t len)
+{
+ printf("Reading from instance with ctx %p into buffer %p of length %zu\n", ctx, buf, len);
+ sleep(1);
+ return is_closed ? -1 : 0;
+}
+
+ssize_t do_write(const void *ctx, const unsigned char *buf, size_t len)
+{
+ printf("Writing from instance with ctx %p into buffer %p of length %zu\n", ctx, buf, len);
+ return len;
+}
+
+void do_log(int level, const char *tag, const char *msg)
+{
+ printf("Log level %d for %s: %s", level, tag, msg);
+}
+
+int main(int argc, char *argv[])
+{
+ int handle;
+
+ printf("WireGuard Go Version %s\n", wgVersion());
+ wgSetLogger(do_log);
+ handle = wgTurnOn((gostring_t){ .p = "test", .n = 4 }, (gostring_t){ .p = "", .n = 0 }, do_read, do_write, &ctx);
+ sleep(5);
+ is_closed = true;
+ wgTurnOff(handle);
+ return 0;
+}