aboutsummaryrefslogblamecommitdiffstats
path: root/wireguard-go-bridge/example.c
blob: 7ba0c42e40039b23a7572b44ed8fbd1ad0b6c615 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16















                                                                                
                                                          





                                                                                                  
                                                           




                                                                                                  
                                       
 
                                               







                                                         
                                                                                                                            




                          
/* 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(void *ctx, 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(void *ctx, 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 *msg)
{
	printf("Log level %d: %s", level, 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 }, 0, do_read, do_write, &ctx);
	sleep(5);
	is_closed = true;
	wgTurnOff(handle);
	return 0;
}