From 7e962a9932667f4a161b20aba5ff1c75ab8e578a Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Thu, 9 May 2019 10:11:15 +0200 Subject: wintun: wait for interface registry key on device creation By using RegNotifyChangeKeyValue(). Also disable dead gateway detection. Signed-off-by: Simon Rozman --- tun/tun_windows.go | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) (limited to 'tun/tun_windows.go') diff --git a/tun/tun_windows.go b/tun/tun_windows.go index b319c27..8218fc3 100644 --- a/tun/tun_windows.go +++ b/tun/tun_windows.go @@ -62,35 +62,27 @@ func packetAlign(size uint32) uint32 { func CreateTUN(ifname string) (TUNDevice, error) { var err error var wt *wintun.Wintun - for i := 0; i < 3; i++ { - // Does an interface with this name already exist? - wt, err = wintun.GetInterface(ifname, 0) - if wt == nil { - // Interface does not exist or an error occured. Create one. - wt, _, err = wintun.CreateInterface("WireGuard Tunnel Adapter", 0) - if err != nil { - err = fmt.Errorf("wintun.CreateInterface: %v", err) - continue - } - } else if err != nil { - // Foreign interface with the same name found. - // We could create a Wintun interface under a temporary name. But, should our - // process die without deleting this interface first, the interface would remain - // orphaned. - err = fmt.Errorf("wintun.GetInterface: %v", err) - continue - } - err = wt.SetInterfaceName(ifname) //TODO: This is the function that most often fails + // Does an interface with this name already exist? + wt, err = wintun.GetInterface(ifname, 0) + if wt == nil { + // Interface does not exist or an error occurred. Create one. + wt, _, err = wintun.CreateInterface("WireGuard Tunnel Adapter", 0) if err != nil { - wt.DeleteInterface(0) - wt = nil - err = fmt.Errorf("wintun.SetInterfaceName: %v", err) - continue + return nil, fmt.Errorf("wintun.CreateInterface: %v", err) } + } else if err != nil { + // Foreign interface with the same name found. + // We could create a Wintun interface under a temporary name. But, should our + // process die without deleting this interface first, the interface would remain + // orphaned. + return nil, fmt.Errorf("wintun.GetInterface: %v", err) } + + err = wt.SetInterfaceName(ifname) if err != nil { - return nil, err + wt.DeleteInterface(0) + return nil, fmt.Errorf("wintun.SetInterfaceName: %v", err) } err = wt.FlushInterface() -- cgit v1.2.3-59-g8ed1b