diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-11-06 15:46:44 +0100 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-11-06 16:27:25 +0100 |
commit | 0a55a284d5edcb792848448b5415e4faea437038 (patch) | |
tree | 7c12f8a044410ffeabf31fe217359ceab847c45f /wireguard-go-bridge/src/tun/api-ios.go | |
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 'wireguard-go-bridge/src/tun/api-ios.go')
-rw-r--r-- | wireguard-go-bridge/src/tun/api-ios.go | 52 |
1 files changed, 52 insertions, 0 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 +} |