summaryrefslogtreecommitdiffhomepage
path: root/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-05-23 03:52:26 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-05-23 17:01:40 +0200
commit62dbeca7323e74d4e08a08a878f429ad0a050616 (patch)
tree75cd35a8fdd55f2890f02fd92c6b90c7e302f89d /app/tools/libwg-go/src/git.zx2c4.com/wireguard-go
parenta533be82e8d889216bcc25b2aad35ca55f2822ff (diff)
libwg-go: use gopath
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/tools/libwg-go/src/git.zx2c4.com/wireguard-go')
-rw-r--r--app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/api-android.go162
-rw-r--r--app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/jni.c45
-rw-r--r--app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/tun/api-android.go31
3 files changed, 238 insertions, 0 deletions
diff --git a/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/api-android.go b/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/api-android.go
new file mode 100644
index 00000000..24a0eaec
--- /dev/null
+++ b/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/api-android.go
@@ -0,0 +1,162 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Copyright (C) 2017-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
+ */
+
+package main
+
+// #cgo LDFLAGS: -llog
+// #include <android/log.h>
+import "C"
+
+import (
+ "bufio"
+ "git.zx2c4.com/wireguard-go/tun"
+ "golang.org/x/sys/unix"
+ "io/ioutil"
+ "log"
+ "math"
+ "os"
+ "os/signal"
+ "runtime"
+ "strings"
+ "unsafe"
+)
+
+type AndroidLogger struct {
+ level C.int
+ interfaceName string
+}
+
+func (l AndroidLogger) Write(p []byte) (int, error) {
+ C.__android_log_write(l.level, C.CString("WireGuard/GoBackend/"+l.interfaceName), C.CString(string(p)))
+ return len(p), nil
+}
+
+var tunnelHandles map[int32]*Device
+
+func init() {
+ tunnelHandles = make(map[int32]*Device)
+ signals := make(chan os.Signal)
+ signal.Notify(signals, unix.SIGUSR2)
+ go func() {
+ buf := make([]byte, os.Getpagesize())
+ for {
+ select {
+ case <-signals:
+ n := runtime.Stack(buf, true)
+ buf[n] = 0
+ C.__android_log_write(C.ANDROID_LOG_ERROR, C.CString("WireGuard/GoBackend/Stacktrace"), (*_Ctype_char)(unsafe.Pointer(&buf[0])))
+ }
+ }
+ }()
+}
+
+//export wgTurnOn
+func wgTurnOn(ifnameRef string, tun_fd int32, settings string) int32 {
+ interfaceName := string([]byte(ifnameRef))
+
+ logger := &Logger{
+ Debug: log.New(&AndroidLogger{level: C.ANDROID_LOG_DEBUG, interfaceName: interfaceName}, "", 0),
+ Info: log.New(&AndroidLogger{level: C.ANDROID_LOG_INFO, interfaceName: interfaceName}, "", 0),
+ Error: log.New(&AndroidLogger{level: C.ANDROID_LOG_ERROR, interfaceName: interfaceName}, "", 0),
+ }
+
+ logger.Debug.Println("Debug log enabled")
+
+ tun, name, err := tun.CreateTUNFromFD(int(tun_fd))
+ if err != nil {
+ unix.Close(int(tun_fd))
+ logger.Error.Println(err)
+ return -1
+ }
+
+ logger.Info.Println("Attaching to interface", name)
+ device := NewDevice(tun, logger)
+
+ logger.Debug.Println("Interface has MTU", device.tun.mtu)
+
+ bufferedSettings := bufio.NewReadWriter(bufio.NewReader(strings.NewReader(settings)), bufio.NewWriter(ioutil.Discard))
+ setError := ipcSetOperation(device, bufferedSettings)
+ if setError != nil {
+ unix.Close(int(tun_fd))
+ logger.Error.Println(setError)
+ return -1
+ }
+
+ device.Up()
+ logger.Info.Println("Device started")
+
+ var i int32
+ for i = 0; i < math.MaxInt32; i++ {
+ if _, exists := tunnelHandles[i]; !exists {
+ break
+ }
+ }
+ if i == math.MaxInt32 {
+ unix.Close(int(tun_fd))
+ return -1
+ }
+ tunnelHandles[i] = device
+ return i
+}
+
+//export wgTurnOff
+func wgTurnOff(tunnelHandle int32) {
+ device, ok := tunnelHandles[tunnelHandle]
+ if !ok {
+ return
+ }
+ delete(tunnelHandles, tunnelHandle)
+ device.Close()
+}
+
+//export wgGetSocketV4
+func wgGetSocketV4(tunnelHandle int32) int32 {
+ device, ok := tunnelHandles[tunnelHandle]
+ if !ok {
+ return -1
+ }
+ native, ok := device.net.bind.(*NativeBind)
+ if !ok {
+ return -1
+ }
+ fd := int32(-1)
+ conn, err := native.ipv4.SyscallConn()
+ if err != nil {
+ return -1
+ }
+ err = conn.Control(func(f uintptr) {
+ fd = int32(f)
+ })
+ if err != nil {
+ return -1
+ }
+ return fd
+}
+
+//export wgGetSocketV6
+func wgGetSocketV6(tunnelHandle int32) int32 {
+ device, ok := tunnelHandles[tunnelHandle]
+ if !ok {
+ return -1
+ }
+ native, ok := device.net.bind.(*NativeBind)
+ if !ok {
+ return -1
+ }
+ fd := int32(-1)
+ conn, err := native.ipv6.SyscallConn()
+ if err != nil {
+ return -1
+ }
+ err = conn.Control(func(f uintptr) {
+ fd = int32(f)
+ })
+ if err != nil {
+ return -1
+ }
+ return fd
+}
+
+func main() {}
diff --git a/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/jni.c b/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/jni.c
new file mode 100644
index 00000000..a0f3d0ba
--- /dev/null
+++ b/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/jni.c
@@ -0,0 +1,45 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Copyright © 2017-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
+ */
+
+#include <jni.h>
+
+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 int wgGetSocketV4(int handle);
+extern int wgGetSocketV6(int handle);
+
+JNIEXPORT jint JNICALL Java_com_wireguard_android_backend_GoBackend_wgTurnOn(JNIEnv *env, jclass c, jstring ifname, jint tun_fd, jstring settings)
+{
+ const char *ifname_str = (*env)->GetStringUTFChars(env, ifname, 0);
+ size_t ifname_len = (*env)->GetStringUTFLength(env, ifname);
+ const char *settings_str = (*env)->GetStringUTFChars(env, settings, 0);
+ size_t settings_len = (*env)->GetStringUTFLength(env, settings);
+ int ret = wgTurnOn((struct go_string){
+ .str = ifname_str,
+ .n = ifname_len
+ }, tun_fd, (struct go_string){
+ .str = settings_str,
+ .n = settings_len
+ });
+ (*env)->ReleaseStringUTFChars(env, ifname, ifname_str);
+ (*env)->ReleaseStringUTFChars(env, settings, settings_str);
+ return ret;
+}
+
+JNIEXPORT void JNICALL Java_com_wireguard_android_backend_GoBackend_wgTurnOff(JNIEnv *env, jclass c, jint handle)
+{
+ wgTurnOff(handle);
+}
+
+JNIEXPORT jint JNICALL Java_com_wireguard_android_backend_GoBackend_wgGetSocketV4(JNIEnv *env, jclass c, jint handle)
+{
+ return wgGetSocketV4(handle);
+}
+
+JNIEXPORT jint JNICALL Java_com_wireguard_android_backend_GoBackend_wgGetSocketV6(JNIEnv *env, jclass c, jint handle)
+{
+ return wgGetSocketV6(handle);
+}
diff --git a/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/tun/api-android.go b/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/tun/api-android.go
new file mode 100644
index 00000000..3cb5753e
--- /dev/null
+++ b/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/tun/api-android.go
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Copyright (C) 2017-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
+ */
+
+package tun
+
+import (
+ "git.zx2c4.com/wireguard-go/rwcancel"
+ "os"
+)
+
+func CreateTUNFromFD(tun_fd int) (TUNDevice, string, error) {
+ tun := &nativeTun{
+ fd: os.NewFile(uintptr(tun_fd), "/dev/tun"),
+ events: make(chan TUNEvent, 5),
+ errors: make(chan error, 5),
+ nopi: true,
+ }
+ var err error
+ tun.fdCancel, err = rwcancel.NewRWCancel(tun_fd)
+ if err != nil {
+ return nil, "", err
+ }
+ name, err := tun.Name()
+ if err != nil {
+ tun.fdCancel.Cancel()
+ return nil, "", err
+ }
+ return tun, name, nil
+}