diff options
author | 2018-11-06 15:46:44 +0100 | |
---|---|---|
committer | 2018-11-06 16:27:25 +0100 | |
commit | 0a55a284d5edcb792848448b5415e4faea437038 (patch) | |
tree | 7c12f8a044410ffeabf31fe217359ceab847c45f /wireguard-go-bridge/src/tun | |
parent | Xcode: enable more warnings (diff) | |
download | wireguard-apple-0a55a284d5edcb792848448b5415e4faea437038.tar.xz wireguard-apple-0a55a284d5edcb792848448b5415e4faea437038.zip |
wireguard-go-bridge: take fd instead of fnptr
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to '')
-rw-r--r-- | wireguard-go-bridge/src/tun/api-ios.go | 52 | ||||
-rw-r--r-- | wireguard-go-bridge/src/tun/tun_ios.go | 90 |
2 files changed, 52 insertions, 90 deletions
diff --git a/wireguard-go-bridge/src/tun/api-ios.go b/wireguard-go-bridge/src/tun/api-ios.go new file mode 100644 index 0000000..476669f --- /dev/null +++ b/wireguard-go-bridge/src/tun/api-ios.go @@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: GPL-2.0 + * + * Copyright (C) 2017-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. + */ + +package tun + +import ( + "git.zx2c4.com/wireguard-go/rwcancel" + "golang.org/x/sys/unix" + "net" + "os" +) + +func CreateTUNFromFD(tunFd int) (TUNDevice, string, error) { + file := os.NewFile(uintptr(tunFd), "/dev/tun") + tun := &nativeTun{ + tunFile: file, + fd: file.Fd(), + events: make(chan TUNEvent, 5), + errors: make(chan error, 5), + } + var err error + tun.rwcancel, err = rwcancel.NewRWCancel(tunFd) + if err != nil { + return nil, "", err + } + name, err := tun.Name() + if err != nil { + tun.rwcancel.Cancel() + return nil, "", err + } + tunIfindex, err := func() (int, error) { + iface, err := net.InterfaceByName(name) + if err != nil { + return -1, err + } + return iface.Index, nil + }() + if err != nil { + tun.tunFile.Close() + return nil, "", err + } + tun.routeSocket, err = unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC) + if err != nil { + tun.tunFile.Close() + return nil, "", err + } + go tun.routineRouteListener(tunIfindex) + + return tun, name, nil +} diff --git a/wireguard-go-bridge/src/tun/tun_ios.go b/wireguard-go-bridge/src/tun/tun_ios.go deleted file mode 100644 index cafe33b..0000000 --- a/wireguard-go-bridge/src/tun/tun_ios.go +++ /dev/null @@ -1,90 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 - * - * Copyright (C) 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. - */ - -package tun - -// #include <sys/types.h> -// static ssize_t callFnWithCtx(const void *func, const void *ctx, const void *buffer, size_t len) -// { -// return ((ssize_t(*)(const void *, const unsigned char *, size_t))func)(ctx, buffer, len); -// } -import "C" - -import ( - "os" - "syscall" - "unsafe" -) - -type nativeTun struct { - events chan TUNEvent - mtu int - readFn unsafe.Pointer - writeFn unsafe.Pointer - ctx unsafe.Pointer -} - -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{ - events: make(chan TUNEvent, 10), - mtu: int(mtu), - readFn: readFn, - writeFn: writeFn, - ctx: ctx, - } - tun.events <- TUNEventUp - return tun -} - -func (tun *nativeTun) Name() (string, error) { - return "tun", nil -} - -func (tun *nativeTun) File() *os.File { - return nil -} - -func (tun *nativeTun) Events() chan TUNEvent { - return tun.events -} - -func (tun *nativeTun) Read(buff []byte, offset int) (int, error) { - ret := C.callFnWithCtx(tun.readFn, tun.ctx, unsafe.Pointer(&buff[offset]), C.size_t(len(buff) - offset)) - if ret < 0 { - return 0, syscall.Errno(-ret) - } - return int(ret), nil -} - -func (tun *nativeTun) Write(buff []byte, offset int) (int, error) { - ret := C.callFnWithCtx(tun.writeFn, tun.ctx, unsafe.Pointer(&buff[offset]), C.size_t(len(buff) - offset)) - if ret < 0 { - return 0, syscall.Errno(-ret) - } - return int(ret), nil -} - -func (tun *nativeTun) Close() error { - if tun.events != nil { - close(tun.events) - } - return nil -} - -func (tun *nativeTun) setMTU(n int) error { - tun.mtu = n - return nil -} - -func (tun *nativeTun) MTU() (int, error) { - return tun.mtu, nil -} |