diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-07-24 14:53:43 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-07-24 17:57:56 +0200 |
commit | 8028d708cbf7d19f4bb6312eac731ad8c16182f0 (patch) | |
tree | ce35cddb5f898e4839547cfcfd58d15225ce4fd8 /app/tools/libwg-go/src/git.zx2c4.com | |
parent | 7689905c78e9312dc43b9927d523bbe14e707698 (diff) |
tools: let wg(8) play with userspace implementation
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/tools/libwg-go/src/git.zx2c4.com')
-rw-r--r-- | app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/api-android.go | 48 |
1 files changed, 39 insertions, 9 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 index d1b53849..4d87acc8 100644 --- 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 @@ -16,6 +16,7 @@ import ( "io/ioutil" "log" "math" + "net" "os" "os/signal" "runtime" @@ -33,11 +34,16 @@ func (l AndroidLogger) Write(p []byte) (int, error) { return len(p), nil } -var tunnelHandles map[int32]*Device +type TunnelHandle struct { + device *Device + uapi net.Listener +} + +var tunnelHandles map[int32]TunnelHandle func init() { roamingDisabled = true - tunnelHandles = make(map[int32]*Device) + tunnelHandles = make(map[int32]TunnelHandle) signals := make(chan os.Signal) signal.Notify(signals, unix.SIGUSR2) go func() { @@ -85,6 +91,29 @@ func wgTurnOn(ifnameRef string, tun_fd int32, settings string) int32 { return -1 } + uapiFile, err := UAPIOpen(name) + if err != nil { + unix.Close(int(tun_fd)) + logger.Error.Println(err) + return -1 + } + uapi, err := UAPIListen(name, uapiFile) + if err != nil { + uapiFile.Close() + unix.Close(int(tun_fd)) + logger.Error.Println(err) + return -1 + } + go func() { + for { + conn, err := uapi.Accept() + if err != nil { + return + } + go ipcHandle(device, conn) + } + }() + device.Up() logger.Info.Println("Device started") @@ -98,27 +127,28 @@ func wgTurnOn(ifnameRef string, tun_fd int32, settings string) int32 { unix.Close(int(tun_fd)) return -1 } - tunnelHandles[i] = device + tunnelHandles[i] = TunnelHandle{device: device, uapi: uapi} return i } //export wgTurnOff func wgTurnOff(tunnelHandle int32) { - device, ok := tunnelHandles[tunnelHandle] + handle, ok := tunnelHandles[tunnelHandle] if !ok { return } delete(tunnelHandles, tunnelHandle) - device.Close() + handle.uapi.Close() + handle.device.Close() } //export wgGetSocketV4 func wgGetSocketV4(tunnelHandle int32) int32 { - device, ok := tunnelHandles[tunnelHandle] + handle, ok := tunnelHandles[tunnelHandle] if !ok { return -1 } - native, ok := device.net.bind.(*NativeBind) + native, ok := handle.device.net.bind.(*NativeBind) if !ok { return -1 } @@ -138,11 +168,11 @@ func wgGetSocketV4(tunnelHandle int32) int32 { //export wgGetSocketV6 func wgGetSocketV6(tunnelHandle int32) int32 { - device, ok := tunnelHandles[tunnelHandle] + handle, ok := tunnelHandles[tunnelHandle] if !ok { return -1 } - native, ok := device.net.bind.(*NativeBind) + native, ok := handle.device.net.bind.(*NativeBind) if !ok { return -1 } |