summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2020-10-27 14:39:33 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2020-10-27 16:20:09 +0100
commit36dc8b6994514e42fcdf00ba91774148afbefc1c (patch)
tree1b2cefda0dac8db4f27b3ec7d72454a68fd7378e
parent2057f19a61cedbb0fef88823c832b04165325ab1 (diff)
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>
-rw-r--r--tun/tun_darwin.go20
1 files 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
}