diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-08-27 10:09:16 -0600 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-08-27 11:59:15 -0600 |
commit | 7fbb24afaac9c89c98db89f7773ce82c919c520c (patch) | |
tree | 7da9824aab58da9394aa4b9bc7996d649222c72c /tun/wintun/wintun_windows.go | |
parent | d9008ac35cc9eff988680895a81445ad6b049d65 (diff) |
wintun: rename duplicate adapters instead of ourselves
Diffstat (limited to 'tun/wintun/wintun_windows.go')
-rw-r--r-- | tun/wintun/wintun_windows.go | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/tun/wintun/wintun_windows.go b/tun/wintun/wintun_windows.go index 81da251..f5d42eb 100644 --- a/tun/wintun/wintun_windows.go +++ b/tun/wintun/wintun_windows.go @@ -15,6 +15,7 @@ import ( "golang.org/x/sys/windows" "golang.org/x/sys/windows/registry" + "golang.zx2c4.com/wireguard/tun/wintun/iphlpapi" "golang.zx2c4.com/wireguard/tun/wintun/nci" registryEx "golang.zx2c4.com/wireguard/tun/wintun/registry" "golang.zx2c4.com/wireguard/tun/wintun/setupapi" @@ -528,11 +529,35 @@ func (wintun *Wintun) InterfaceName() (string, error) { func (wintun *Wintun) SetInterfaceName(ifname string) error { const maxSuffix = 1000 availableIfname := ifname +setloop: for i := 0; ; i++ { err := nci.SetConnectionName(&wintun.cfgInstanceID, availableIfname) if err == nil { break } + if err == windows.ERROR_DUP_NAME { + duplicateGuid, err2 := iphlpapi.InterfaceGUIDFromAlias(availableIfname) + if err2 == nil { + for j := 0; j < 1000; j++ { + proposal := fmt.Sprintf("%s %d", ifname, j+1) + if proposal == availableIfname { + continue + } + err2 = nci.SetConnectionName(duplicateGuid, proposal) + if err2 == windows.ERROR_DUP_NAME { + continue + } + if err2 == nil { + err = nci.SetConnectionName(&wintun.cfgInstanceID, availableIfname) + if err == nil { + break setloop + } + } + break + } + } + } + if i > maxSuffix || err != windows.ERROR_DUP_NAME { return fmt.Errorf("NciSetConnectionName failed: %v", err) } |