summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2020-10-27 14:39:36 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2020-10-27 16:20:09 +0100
commit3b490f30aa4d8762d89a51d90f330b39f0be5fef (patch)
tree9ba6250e78d971a46b5b26c9c4c07adf62922bf0
parente6b7c4eef3f914c8657ca02676e48f61e8bf3ed8 (diff)
tun: use SockaddrCtl from golang.org/x/sys/unix on macOS
Direct syscalls using unix.Syscall(unix.SYS_*, ...) are discouraged on macOS and might not be supported in future versions. Switch to use unix.Connect with unix.SockaddrCtl instead. Signed-off-by: Tobias Klauser <tklauser@distanz.ch> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r--tun/tun_darwin.go35
1 files changed, 6 insertions, 29 deletions
diff --git a/tun/tun_darwin.go b/tun/tun_darwin.go
index 54021c5..7a06af5 100644
--- a/tun/tun_darwin.go
+++ b/tun/tun_darwin.go
@@ -20,16 +20,6 @@ import (
const utunControlName = "com.apple.net.utun_control"
-// sockaddr_ctl specifeid in /usr/include/sys/kern_control.h
-type sockaddrCtl struct {
- scLen uint8
- scFamily uint8
- ssSysaddr uint16
- scID uint32
- scUnit uint32
- scReserved [5]uint32
-}
-
type NativeTun struct {
name string
tunFile *os.File
@@ -38,8 +28,6 @@ type NativeTun struct {
routeSocket int
}
-var sockaddrCtlSize uintptr = 32
-
func retryInterfaceByIndex(index int) (iface *net.Interface, err error) {
for i := 0; i < 20; i++ {
iface, err = net.InterfaceByIndex(index)
@@ -134,25 +122,14 @@ func CreateTUN(name string, mtu int) (Device, error) {
return nil, fmt.Errorf("IoctlGetCtlInfo: %w", err)
}
- sc := sockaddrCtl{
- scLen: uint8(sockaddrCtlSize),
- scFamily: unix.AF_SYSTEM,
- ssSysaddr: 2,
- scID: ctlInfo.Id,
- scUnit: uint32(ifIndex) + 1,
+ sc := &unix.SockaddrCtl{
+ ID: ctlInfo.Id,
+ Unit: uint32(ifIndex) + 1,
}
- scPointer := unsafe.Pointer(&sc)
-
- _, _, errno = unix.RawSyscall(
- unix.SYS_CONNECT,
- uintptr(fd),
- uintptr(scPointer),
- uintptr(sockaddrCtlSize),
- )
-
- if errno != 0 {
- return nil, fmt.Errorf("SYS_CONNECT: %v", errno)
+ err = unix.Connect(fd, sc)
+ if err != nil {
+ return nil, err
}
err = syscall.SetNonblock(fd, true)