Allow customizing MTU

This commit is contained in:
Jason A. Donenfeld 2018-10-27 00:57:58 +02:00
parent aed643ff7e
commit ded8ec065e
4 changed files with 16 additions and 8 deletions

View File

@ -14,14 +14,14 @@ static struct {
static bool is_closed = false; static bool is_closed = false;
ssize_t do_read(const void *ctx, const unsigned char *buf, size_t len) 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); printf("Reading from instance with ctx %p into buffer %p of length %zu\n", ctx, buf, len);
sleep(1); sleep(1);
return is_closed ? -1 : 0; return is_closed ? -1 : 0;
} }
ssize_t do_write(const void *ctx, const unsigned char *buf, size_t len) 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); printf("Writing from instance with ctx %p into buffer %p of length %zu\n", ctx, buf, len);
return len; return len;
@ -38,7 +38,7 @@ int main(int argc, char *argv[])
printf("WireGuard Go Version %s\n", wgVersion()); printf("WireGuard Go Version %s\n", wgVersion());
wgSetLogger(do_log); wgSetLogger(do_log);
handle = wgTurnOn((gostring_t){ .p = "test", .n = 4 }, (gostring_t){ .p = "", .n = 0 }, do_read, do_write, &ctx); handle = wgTurnOn((gostring_t){ .p = "test", .n = 4 }, (gostring_t){ .p = "", .n = 0 }, 0, do_read, do_write, &ctx);
sleep(5); sleep(5);
is_closed = true; is_closed = true;
wgTurnOff(handle); wgTurnOff(handle);

View File

@ -79,7 +79,7 @@ func wgSetLogger(loggerFn uintptr) {
} }
//export wgTurnOn //export wgTurnOn
func wgTurnOn(ifnameRef string, settings string, readFn uintptr, writeFn uintptr, ctx uintptr) int32 { func wgTurnOn(ifnameRef string, settings string, mtu uint16, readFn uintptr, writeFn uintptr, ctx uintptr) int32 {
interfaceName := string([]byte(ifnameRef)) interfaceName := string([]byte(ifnameRef))
logger := &Logger{ logger := &Logger{
@ -90,7 +90,7 @@ func wgTurnOn(ifnameRef string, settings string, readFn uintptr, writeFn uintptr
logger.Debug.Println("Debug log enabled") logger.Debug.Println("Debug log enabled")
tun := tun.CreateTUN(1280, unsafe.Pointer(readFn), unsafe.Pointer(writeFn), unsafe.Pointer(ctx)) tun := tun.CreateTUN(mtu, unsafe.Pointer(readFn), unsafe.Pointer(writeFn), unsafe.Pointer(ctx))
logger.Info.Println("Attaching to interface") logger.Info.Println("Attaching to interface")
device := NewDevice(tun, logger) device := NewDevice(tun, logger)

View File

@ -26,10 +26,17 @@ type nativeTun struct {
ctx unsafe.Pointer ctx unsafe.Pointer
} }
func CreateTUN(mtu int, readFn unsafe.Pointer, writeFn unsafe.Pointer, ctx unsafe.Pointer) TUNDevice { func CreateTUN(mtu uint16, readFn unsafe.Pointer, writeFn unsafe.Pointer, ctx unsafe.Pointer) TUNDevice {
if mtu == 0 {
/* 0 means automatic MTU, which iOS makes outerMTU-80-15. The 80 is for
* WireGuard and the 15 ensures our padding will work. Therefore, it's
* safe to have this code assume a massive MTU.
*/
mtu = ^mtu
}
tun := &nativeTun{ tun := &nativeTun{
events: make(chan TUNEvent, 10), events: make(chan TUNEvent, 10),
mtu: mtu, mtu: int(mtu),
readFn: readFn, readFn: readFn,
writeFn: writeFn, writeFn: writeFn,
ctx: ctx, ctx: ctx,

View File

@ -7,12 +7,13 @@
#define WIREGUARD_H #define WIREGUARD_H
#include <sys/types.h> #include <sys/types.h>
#include <stdint.h>
typedef struct { const char *p; size_t n; } gostring_t; typedef struct { const char *p; size_t n; } gostring_t;
typedef ssize_t(*read_write_fn_t)(void *ctx, unsigned char *buf, size_t len); typedef ssize_t(*read_write_fn_t)(void *ctx, unsigned char *buf, size_t len);
typedef void(*logger_fn_t)(int level, const char *tag, const char *msg); typedef void(*logger_fn_t)(int level, const char *tag, const char *msg);
extern void wgSetLogger(logger_fn_t logger_fn); extern void wgSetLogger(logger_fn_t logger_fn);
extern int wgTurnOn(gostring_t ifname, gostring_t settings, read_write_fn_t read_fn, read_write_fn_t write_fn, void *ctx); extern int wgTurnOn(gostring_t ifname, gostring_t settings, uint16_t mtu, read_write_fn_t read_fn, read_write_fn_t write_fn, void *ctx);
extern void wgTurnOff(int handle); extern void wgTurnOff(int handle);
extern char *wgVersion(); extern char *wgVersion();