From 8c057bf9282fad72536ecd4401079291d2b0571c Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Thu, 5 Nov 2020 17:14:16 +0100 Subject: [PATCH] go-bridge: Add context support for wgSetLogger Cherry picked cda99bf45c3cb95ca56204549689a0ae91ff4813 from jd/loggerCtx with the fix for wgSetLogger signature in the C header file. Signed-off-by: Andrej Mihajlov --- wireguard-go-bridge/api-ios.go | 12 +++++++----- wireguard-go-bridge/wireguard.h | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/wireguard-go-bridge/api-ios.go b/wireguard-go-bridge/api-ios.go index d6eccd8..0f80384 100644 --- a/wireguard-go-bridge/api-ios.go +++ b/wireguard-go-bridge/api-ios.go @@ -7,9 +7,9 @@ package main // #include // #include -// static void callLogger(void *func, int level, const char *msg) +// static void callLogger(void *func, void *ctx, int level, const char *msg) // { -// ((void(*)(int, const char *))func)(level, msg); +// ((void(*)(void *, int, const char *))func)(ctx, level, msg); // } import "C" @@ -30,6 +30,7 @@ import ( ) var loggerFunc unsafe.Pointer +var loggerCtx unsafe.Pointer var versionString *C.char type CLogger struct { @@ -41,7 +42,7 @@ func (l *CLogger) Write(p []byte) (int, error) { return 0, errors.New("No logger initialized") } message := C.CString(string(p)) - C.callLogger(loggerFunc, l.level, message) + C.callLogger(loggerFunc, loggerCtx, l.level, message) C.free(unsafe.Pointer(message)) return len(p), nil } @@ -66,7 +67,7 @@ func init() { n := runtime.Stack(buf, true) buf[n] = 0 if uintptr(loggerFunc) != 0 { - C.callLogger(loggerFunc, 0, (*C.char)(unsafe.Pointer(&buf[0]))) + C.callLogger(loggerFunc, loggerCtx, 0, (*C.char)(unsafe.Pointer(&buf[0]))) } } } @@ -79,7 +80,8 @@ func wgEnableRoaming(enabled bool) { } //export wgSetLogger -func wgSetLogger(loggerFn uintptr) { +func wgSetLogger(context, loggerFn uintptr) { + loggerCtx = unsafe.Pointer(context) loggerFunc = unsafe.Pointer(loggerFn) } diff --git a/wireguard-go-bridge/wireguard.h b/wireguard-go-bridge/wireguard.h index 5c30ee9..18bf6bd 100644 --- a/wireguard-go-bridge/wireguard.h +++ b/wireguard-go-bridge/wireguard.h @@ -10,9 +10,9 @@ #include #include -typedef void(*logger_fn_t)(int level, const char *msg); +typedef void(*logger_fn_t)(void *context, int level, const char *msg); extern void wgEnableRoaming(bool enabled); -extern void wgSetLogger(logger_fn_t logger_fn); +extern void wgSetLogger(void *context, logger_fn_t logger_fn); extern int wgTurnOn(const char *settings, int32_t tun_fd); extern void wgTurnOff(int handle); extern int64_t wgSetConfig(int handle, const char *settings);