tun: allow darwin to auto assign names

This commit is contained in:
Jason A. Donenfeld 2018-05-04 21:11:38 +02:00
parent 0f322f83f5
commit de7ecc571b
2 changed files with 20 additions and 10 deletions

22
main.go
View File

@ -115,13 +115,6 @@ func main() {
return LogLevelInfo return LogLevelInfo
}() }()
logger := NewLogger(
logLevel,
fmt.Sprintf("(%s) ", interfaceName),
)
logger.Debug.Println("Debug log enabled")
// open TUN device (or use supplied fd) // open TUN device (or use supplied fd)
tun, err := func() (TUNDevice, error) { tun, err := func() (TUNDevice, error) {
@ -141,6 +134,21 @@ func main() {
return CreateTUNFromFile(file) 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 { if err != nil {
logger.Error.Println("Failed to create TUN device:", err) logger.Error.Println("Failed to create TUN device:", err)
os.Exit(ExitSetupFailed) os.Exit(ExitSetupFailed)

View File

@ -46,9 +46,11 @@ var sockaddrCtlSize uintptr = 32
func CreateTUN(name string) (TUNDevice, error) { func CreateTUN(name string) (TUNDevice, error) {
ifIndex := -1 ifIndex := -1
fmt.Sscanf(name, "utun%d", &ifIndex) if (name != "utun") {
if ifIndex < 0 { fmt.Sscanf(name, "utun%d", &ifIndex)
return nil, fmt.Errorf("error parsing interface name %s, must be utun[0-9]+", name) 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) fd, err := unix.Socket(unix.AF_SYSTEM, unix.SOCK_DGRAM, 2)