diff options
Diffstat (limited to 'tunnel/tools/libwg-go')
-rw-r--r-- | tunnel/tools/libwg-go/api-android.go | 35 | ||||
-rw-r--r-- | tunnel/tools/libwg-go/jni.c | 25 |
2 files changed, 60 insertions, 0 deletions
diff --git a/tunnel/tools/libwg-go/api-android.go b/tunnel/tools/libwg-go/api-android.go index 7a393cae..9ccd913f 100644 --- a/tunnel/tools/libwg-go/api-android.go +++ b/tunnel/tools/libwg-go/api-android.go @@ -11,6 +11,7 @@ import "C" import ( "bufio" + "bytes" "golang.org/x/sys/unix" "golang.zx2c4.com/wireguard/device" "golang.zx2c4.com/wireguard/ipc" @@ -143,6 +144,40 @@ func wgTurnOff(tunnelHandle int32) { handle.device.Close() } +//export wgGetOperation +func wgGetOperation(tunnelHandle int32) *C.char { + handle, ok := tunnelHandles[tunnelHandle] + if !ok { + return C.CString("(bad handle)") + } + + var buf bytes.Buffer + writer := bufio.NewWriter(&buf) + + getError := handle.device.IpcGetOperation(writer) + if getError != nil { + return C.CString(getError.Error()) + } + + writer.Flush() + return C.CString(buf.String()) +} + +//export wgSetOperation +func wgSetOperation(tunnelHandle int32, settings string) int32 { + handle, ok := tunnelHandles[tunnelHandle] + if !ok { + return -1 + } + + setError := handle.device.IpcSetOperation(bufio.NewReader(strings.NewReader(settings))) + if setError != nil { + return -1 + } + + return 0 +} + //export wgGetSocketV4 func wgGetSocketV4(tunnelHandle int32) int32 { handle, ok := tunnelHandles[tunnelHandle] diff --git a/tunnel/tools/libwg-go/jni.c b/tunnel/tools/libwg-go/jni.c index 3f877d47..1cc69343 100644 --- a/tunnel/tools/libwg-go/jni.c +++ b/tunnel/tools/libwg-go/jni.c @@ -10,6 +10,8 @@ struct go_string { const char *str; long n; }; extern int wgTurnOn(struct go_string ifname, int tun_fd, struct go_string settings); extern void wgTurnOff(int handle); +extern char *wgGetOperation(int handle); +extern int wgSetOperation(int handle, struct go_string settings); extern int wgGetSocketV4(int handle); extern int wgGetSocketV6(int handle); extern char *wgGetConfig(int handle); @@ -38,6 +40,29 @@ JNIEXPORT void JNICALL Java_com_wireguard_android_backend_GoBackend_wgTurnOff(JN wgTurnOff(handle); } +JNIEXPORT jstring JNICALL Java_com_wireguard_android_backend_GoBackend_wgGetOperation(JNIEnv *env, jclass c, jint handle) +{ + jstring ret; + char *settings = wgGetOperation(handle); + if (!settings) + return NULL; + ret = (*env)->NewStringUTF(env, settings); + free(settings); + return ret; +} + +JNIEXPORT jint JNICALL Java_com_wireguard_android_backend_GoBackend_wgSetOperation(JNIEnv *env, jclass c, jint handle, jstring settings) +{ + const char *settings_str = (*env)->GetStringUTFChars(env, settings, 0); + size_t settings_len = (*env)->GetStringUTFLength(env, settings); + int ret = wgSetOperation(handle, (struct go_string){ + .str = settings_str, + .n = settings_len + }); + (*env)->ReleaseStringUTFChars(env, settings, settings_str); + return ret; +} + JNIEXPORT jint JNICALL Java_com_wireguard_android_backend_GoBackend_wgGetSocketV4(JNIEnv *env, jclass c, jint handle) { return wgGetSocketV4(handle); |