aboutsummaryrefslogtreecommitdiffstats
path: root/wireguard-go-bridge/example.c
blob: 912d181d017a2e83485d08608504ddbca7e75c76 (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
/* 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;
}