From 83edde34494264e0917f134086cb49ae05d72d82 Mon Sep 17 00:00:00 2001 From: Owen Date: Wed, 31 Dec 2025 18:01:25 -0500 Subject: [PATCH] Fix build on darwin Former-commit-id: fbeb5be88d9a2c126c232e0df78efef4fa51ead8 --- device/tun_darwin.go | 44 ++++++++++++++++++++++++++++ device/{tun_unix.go => tun_linux.go} | 2 +- 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 device/tun_darwin.go rename device/{tun_unix.go => tun_linux.go} (98%) diff --git a/device/tun_darwin.go b/device/tun_darwin.go new file mode 100644 index 0000000..f763f74 --- /dev/null +++ b/device/tun_darwin.go @@ -0,0 +1,44 @@ +//go:build darwin + +package device + +import ( + "net" + "os" + + "github.com/fosrl/newt/logger" + "golang.org/x/sys/unix" + "golang.zx2c4.com/wireguard/ipc" + "golang.zx2c4.com/wireguard/tun" +) + +func CreateTUNFromFD(tunFd uint32, mtuInt int) (tun.Device, error) { + dupTunFd, err := unix.Dup(int(tunFd)) + if err != nil { + logger.Error("Unable to dup tun fd: %v", err) + return nil, err + } + + err = unix.SetNonblock(dupTunFd, true) + if err != nil { + unix.Close(dupTunFd) + return nil, err + } + + file := os.NewFile(uintptr(dupTunFd), "/dev/tun") + device, err := tun.CreateTUNFromFile(file, mtuInt) + if err != nil { + file.Close() + return nil, err + } + + return device, nil +} + +func UapiOpen(interfaceName string) (*os.File, error) { + return ipc.UAPIOpen(interfaceName) +} + +func UapiListen(interfaceName string, fileUAPI *os.File) (net.Listener, error) { + return ipc.UAPIListen(interfaceName, fileUAPI) +} diff --git a/device/tun_unix.go b/device/tun_linux.go similarity index 98% rename from device/tun_unix.go rename to device/tun_linux.go index 22cec13..902f269 100644 --- a/device/tun_unix.go +++ b/device/tun_linux.go @@ -1,4 +1,4 @@ -//go:build !windows +//go:build linux package device