diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-08-22 08:52:59 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-08-22 08:52:59 +0200 |
commit | 3cedc22d7b49be8ca00dc549c79de9e4a2d3df5b (patch) | |
tree | 8a3e9d09299a303883e04a793985a842c8f2dd10 /tun/wintun/wintun_windows.go | |
parent | 68fea631d8e97aa7395b944916e569cf66553e73 (diff) |
wintun: try multiple names until one isn't a duplicate
Diffstat (limited to 'tun/wintun/wintun_windows.go')
-rw-r--r-- | tun/wintun/wintun_windows.go | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/tun/wintun/wintun_windows.go b/tun/wintun/wintun_windows.go index f6a230a..1caa4a1 100644 --- a/tun/wintun/wintun_windows.go +++ b/tun/wintun/wintun_windows.go @@ -373,7 +373,7 @@ func CreateInterface(description string, requestedGUID *windows.GUID) (wintun *W // Wait for TCP/IP interface registry key to emerge. tcpipInterfaceRegKey, err := registryEx.OpenKeyWait( registry.LOCAL_MACHINE, - tcpipInterfaceRegKeyName, registry.QUERY_VALUE | registry.SET_VALUE, + tcpipInterfaceRegKeyName, registry.QUERY_VALUE|registry.SET_VALUE, waitForRegistryTimeout) if err != nil { err = fmt.Errorf("OpenKeyWait(HKLM\\%s) failed: %v", tcpipInterfaceRegKeyName, err) @@ -522,9 +522,17 @@ func (wintun *Wintun) InterfaceName() (string, error) { // SetInterfaceName sets name of the Wintun interface. func (wintun *Wintun) SetInterfaceName(ifname string) error { - err := nci.SetConnectionName(&wintun.cfgInstanceID, ifname) - if err != nil { - return fmt.Errorf("NciSetConnectionName failed: %v", err) + const maxSuffix = 1000 + availableIfname := ifname + for i := 0; ; i++ { + err := nci.SetConnectionName(&wintun.cfgInstanceID, availableIfname) + if err == nil { + break + } + if i > maxSuffix || err != windows.ERROR_DUP_NAME { + return fmt.Errorf("NciSetConnectionName failed: %v", err) + } + availableIfname = fmt.Sprintf("%s %d", ifname, i+1) } // TODO: This should use NetSetup2 so that it doesn't get unset. |