From 36dc8b6994514e42fcdf00ba91774148afbefc1c Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 27 Oct 2020 14:39:33 +0100 Subject: [PATCH] tun: use GetsockoptString in (*NativeTun).Name on macOS Direct syscalls using unix.Syscall(unix.SYS_*, ...) are discouraged on macOS and might not be supported in future versions. Instead, use the existing unix.GetsockoptString wrapper to get the interface name. Signed-off-by: Tobias Klauser Signed-off-by: Jason A. Donenfeld --- tun/tun_darwin.go | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/tun/tun_darwin.go b/tun/tun_darwin.go index 52b4070..f04ace2 100644 --- a/tun/tun_darwin.go +++ b/tun/tun_darwin.go @@ -230,27 +230,19 @@ func CreateTUNFromFile(file *os.File, mtu int) (Device, error) { } func (tun *NativeTun) Name() (string, error) { - var ifName struct { - name [16]byte - } - ifNameSize := uintptr(16) - - var errno syscall.Errno + var err error tun.operateOnFd(func(fd uintptr) { - _, _, errno = unix.Syscall6( - unix.SYS_GETSOCKOPT, - fd, + tun.name, err = unix.GetsockoptString( + int(fd), 2, /* #define SYSPROTO_CONTROL 2 */ 2, /* #define UTUN_OPT_IFNAME 2 */ - uintptr(unsafe.Pointer(&ifName)), - uintptr(unsafe.Pointer(&ifNameSize)), 0) + ) }) - if errno != 0 { - return "", fmt.Errorf("SYS_GETSOCKOPT: %v", errno) + if err != nil { + return "", fmt.Errorf("GetSockoptString: %w", err) } - tun.name = string(ifName.name[:ifNameSize-1]) return tun.name, nil }