From 3b490f30aa4d8762d89a51d90f330b39f0be5fef Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 27 Oct 2020 14:39:36 +0100 Subject: [PATCH] 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 Signed-off-by: Jason A. Donenfeld --- tun/tun_darwin.go | 35 ++++++----------------------------- 1 file 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)