From d87cbeeb2fb152b5f377758c68c655e09fe177f3 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Thu, 7 Feb 2019 22:02:51 +0100 Subject: wintun: Detect if a foreign interface with the same name exists Signed-off-by: Simon Rozman --- tun/tun_windows.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'tun/tun_windows.go') diff --git a/tun/tun_windows.go b/tun/tun_windows.go index 5f4f22a..5c231e7 100644 --- a/tun/tun_windows.go +++ b/tun/tun_windows.go @@ -55,16 +55,26 @@ type nativeTun struct { func CreateTUN(ifname string) (TUNDevice, error) { // Does an interface with this name already exist? wt, err := wintun.GetInterface(ifname, 0) - if wt == nil || err != nil { + if wt == nil { // Interface does not exist or an error occured. Create one. wt, _, err = wintun.CreateInterface("WireGuard Tunnel Adapter", 0) if err != nil { return nil, 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 + // proces die without deleting this interface first, the interface would remain + // orphaned. + return nil, err + } - // Set interface name. (Ignore errors.) - wt.SetInterfaceName(ifname) + err = wt.SetInterfaceName(ifname) + if err != nil { + wt.DeleteInterface(0) + return nil, err } + err = wt.FlushInterface() if err != nil { wt.DeleteInterface(0) -- cgit v1.2.3