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:
parent
e6b7c4eef3
commit
3b490f30aa
|
@ -20,16 +20,6 @@ import (
|
||||||
|
|
||||||
const utunControlName = "com.apple.net.utun_control"
|
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 {
|
type NativeTun struct {
|
||||||
name string
|
name string
|
||||||
tunFile *os.File
|
tunFile *os.File
|
||||||
|
@ -38,8 +28,6 @@ type NativeTun struct {
|
||||||
routeSocket int
|
routeSocket int
|
||||||
}
|
}
|
||||||
|
|
||||||
var sockaddrCtlSize uintptr = 32
|
|
||||||
|
|
||||||
func retryInterfaceByIndex(index int) (iface *net.Interface, err error) {
|
func retryInterfaceByIndex(index int) (iface *net.Interface, err error) {
|
||||||
for i := 0; i < 20; i++ {
|
for i := 0; i < 20; i++ {
|
||||||
iface, err = net.InterfaceByIndex(index)
|
iface, err = net.InterfaceByIndex(index)
|
||||||
|
@ -134,25 +122,14 @@ func CreateTUN(name string, mtu int) (Device, error) {
|
||||||
return nil, fmt.Errorf("IoctlGetCtlInfo: %w", err)
|
return nil, fmt.Errorf("IoctlGetCtlInfo: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sc := sockaddrCtl{
|
sc := &unix.SockaddrCtl{
|
||||||
scLen: uint8(sockaddrCtlSize),
|
ID: ctlInfo.Id,
|
||||||
scFamily: unix.AF_SYSTEM,
|
Unit: uint32(ifIndex) + 1,
|
||||||
ssSysaddr: 2,
|
|
||||||
scID: ctlInfo.Id,
|
|
||||||
scUnit: uint32(ifIndex) + 1,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
scPointer := unsafe.Pointer(&sc)
|
err = unix.Connect(fd, sc)
|
||||||
|
if err != nil {
|
||||||
_, _, errno = unix.RawSyscall(
|
return nil, err
|
||||||
unix.SYS_CONNECT,
|
|
||||||
uintptr(fd),
|
|
||||||
uintptr(scPointer),
|
|
||||||
uintptr(sockaddrCtlSize),
|
|
||||||
)
|
|
||||||
|
|
||||||
if errno != 0 {
|
|
||||||
return nil, fmt.Errorf("SYS_CONNECT: %v", errno)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = syscall.SetNonblock(fd, true)
|
err = syscall.SetNonblock(fd, true)
|
||||||
|
|
Loading…
Reference in a new issue