1
0
mirror of https://git.zx2c4.com/wireguard-go synced 2024-11-15 01:05:15 +01:00

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>
This commit is contained in:
Tobias Klauser 2020-10-27 14:39:36 +01:00 committed by Jason A. Donenfeld
parent e6b7c4eef3
commit 3b490f30aa

View File

@ -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)