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) {
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
}