From de7ecc571b9ea81d5cad97e339e84f8084352741 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Fri, 4 May 2018 21:11:38 +0200 Subject: [PATCH] tun: allow darwin to auto assign names --- main.go | 22 +++++++++++++++------- tun_darwin.go | 8 +++++--- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 41231bf..8562bf6 100644 --- a/main.go +++ b/main.go @@ -115,13 +115,6 @@ func main() { return LogLevelInfo }() - logger := NewLogger( - logLevel, - fmt.Sprintf("(%s) ", interfaceName), - ) - - logger.Debug.Println("Debug log enabled") - // open TUN device (or use supplied fd) tun, err := func() (TUNDevice, error) { @@ -141,6 +134,21 @@ func main() { return CreateTUNFromFile(file) }() + if err == nil { + realInterfaceName, err2 := tun.Name() + if err2 == nil { + interfaceName = realInterfaceName + } + } + + logger := NewLogger( + logLevel, + fmt.Sprintf("(%s) ", interfaceName), + ) + + logger.Debug.Println("Debug log enabled") + + if err != nil { logger.Error.Println("Failed to create TUN device:", err) os.Exit(ExitSetupFailed) diff --git a/tun_darwin.go b/tun_darwin.go index a03347f..4d9b06d 100644 --- a/tun_darwin.go +++ b/tun_darwin.go @@ -46,9 +46,11 @@ var sockaddrCtlSize uintptr = 32 func CreateTUN(name string) (TUNDevice, error) { ifIndex := -1 - fmt.Sscanf(name, "utun%d", &ifIndex) - if ifIndex < 0 { - return nil, fmt.Errorf("error parsing interface name %s, must be utun[0-9]+", name) + if (name != "utun") { + fmt.Sscanf(name, "utun%d", &ifIndex) + if ifIndex < 0 { + return nil, fmt.Errorf("Interface name must be utun[0-9]*") + } } fd, err := unix.Socket(unix.AF_SYSTEM, unix.SOCK_DGRAM, 2)