]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
wintun: work around GetInterface staleness bug
authorJason A. Donenfeld <Jason@zx2c4.com>
Thu, 2 May 2019 22:11:59 +0000 (00:11 +0200)
committerJason A. Donenfeld <Jason@zx2c4.com>
Thu, 2 May 2019 22:42:36 +0000 (00:42 +0200)
tun/tun_windows.go
tun/wintun/registryhacks_windows.go

index 2fa690123f18cd40bffc04be40906ec3d701cb47..b319c2723946b8ff8668b20b4abff615315e2263 100644 (file)
@@ -7,6 +7,7 @@ package tun
 
 import (
        "errors"
+       "fmt"
        "os"
        "sync"
        "time"
@@ -59,32 +60,43 @@ func packetAlign(size uint32) uint32 {
 // adapter with the same name exist, it is reused.
 //
 func CreateTUN(ifname string) (TUNDevice, error) {
-       // 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)
+       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
                if err != nil {
-                       return nil, errors.New("Creating Wintun adapter failed: " + err.Error())
+                       wt.DeleteInterface(0)
+                       wt = nil
+                       err = fmt.Errorf("wintun.SetInterfaceName: %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
-               // proces die without deleting this interface first, the interface would remain
-               // orphaned.
-               return nil, err
        }
-
-       err = wt.SetInterfaceName(ifname)
        if err != nil {
-               wt.DeleteInterface(0)
                return nil, err
        }
 
        err = wt.FlushInterface()
        if err != nil {
                wt.DeleteInterface(0)
-               return nil, errors.New("Flushing interface failed: " + err.Error())
+               return nil, fmt.Errorf("wintun.FlushInterface: %v", err)
        }
 
        return &NativeTun{
index 94cc2f324e38d847e4f1c45ad92b6530a1fa9261..bf72f926bab7ade697d484230ea7ab3ed1a68558 100644 (file)
@@ -11,8 +11,8 @@ import (
 )
 
 const (
-       numRetries   = 100
-       retryTimeout = 150 * time.Millisecond
+       numRetries   = 50
+       retryTimeout = 100 * time.Millisecond
 )
 
 func registryOpenKeyRetry(k registry.Key, path string, access uint32) (key registry.Key, err error) {