]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
tun: use GetsockoptString in (*NativeTun).Name on macOS
authorTobias Klauser <tklauser@distanz.ch>
Tue, 27 Oct 2020 13:39:33 +0000 (14:39 +0100)
committerJason A. Donenfeld <Jason@zx2c4.com>
Tue, 27 Oct 2020 15:20:09 +0000 (16:20 +0100)
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>
tun/tun_darwin.go

index 52b4070d21880c626d5341f5963d2b6bda9bcd2b..f04ace2e77871e91735b4af8294364022aa96fb1 100644 (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
 }