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 <tklauser@distanz.ch>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Tobias Klauser 2020-10-27 14:39:33 +01:00 committed by Jason A. Donenfeld
parent 2057f19a61
commit 36dc8b6994

View File

@ -230,27 +230,19 @@ func CreateTUNFromFile(file *os.File, mtu int) (Device, error) {
} }
func (tun *NativeTun) Name() (string, error) { func (tun *NativeTun) Name() (string, error) {
var ifName struct { var err error
name [16]byte
}
ifNameSize := uintptr(16)
var errno syscall.Errno
tun.operateOnFd(func(fd uintptr) { tun.operateOnFd(func(fd uintptr) {
_, _, errno = unix.Syscall6( tun.name, err = unix.GetsockoptString(
unix.SYS_GETSOCKOPT, int(fd),
fd,
2, /* #define SYSPROTO_CONTROL 2 */ 2, /* #define SYSPROTO_CONTROL 2 */
2, /* #define UTUN_OPT_IFNAME 2 */ 2, /* #define UTUN_OPT_IFNAME 2 */
uintptr(unsafe.Pointer(&ifName)), )
uintptr(unsafe.Pointer(&ifNameSize)), 0)
}) })
if errno != 0 { if err != nil {
return "", fmt.Errorf("SYS_GETSOCKOPT: %v", errno) return "", fmt.Errorf("GetSockoptString: %w", err)
} }
tun.name = string(ifName.name[:ifNameSize-1])
return tun.name, nil return tun.name, nil
} }