]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
wintun: revise GetInterface()
authorSimon Rozman <simon@rozman.si>
Fri, 24 May 2019 07:28:50 +0000 (09:28 +0200)
committerSimon Rozman <simon@rozman.si>
Fri, 24 May 2019 07:29:57 +0000 (09:29 +0200)
- Make foreign interface found error numeric to ease condition
  detection.
- Update GetInterface() documentation.
- Make tun.CreateTUN() quit when foreign interface found before
  attempting to create a Wintun interface with a duplicate name.
  Creation is futile.

Signed-off-by: Simon Rozman <simon@rozman.si>
tun/tun_windows.go
tun/wintun/wintun_windows.go

index c17f6d1285373fff1cf0d0e2ef85594143b025c6..b352cd1110073198692f1b300f1d1f42342a6a47 100644 (file)
@@ -71,6 +71,8 @@ func CreateTUN(ifname string) (TUNDevice, error) {
                if err != nil {
                        return nil, fmt.Errorf("Unable to delete already existing Wintun interface: %v", err)
                }
+       } else if err == windows.ERROR_ALREADY_EXISTS {
+               return nil, fmt.Errorf("Foreign network interface with the same name exists")
        }
        wt, _, err = wintun.CreateInterface("WireGuard Tunnel Adapter", 0)
        if err != nil {
index aea4ad1e7b27bbb04ed91f1cb79f685534c52aed..7b99a5006a427c1e66c9450dcdf0d60603a85723 100644 (file)
@@ -89,8 +89,9 @@ func makeWintun(deviceInfoSet setupapi.DevInfo, deviceInfoData *setupapi.DevInfo
 // optional and can be 0. If a specific top-level window is not required, set
 // hwndParent to 0.
 //
-// Function returns interface if found, or nil otherwise. If the interface is
-// found but not Wintun-class, the function returns interface and an error.
+// Function returns interface if found, or windows.ERROR_OBJECT_NOT_FOUND
+// otherwise. If the interface is found but not Wintun-class, the function
+// returns windows.ERROR_ALREADY_EXISTS.
 //
 func GetInterface(ifname string, hwndParent uintptr) (*Wintun, error) {
        // Create a list of network devices.
@@ -164,7 +165,7 @@ func GetInterface(ifname string, hwndParent uintptr) (*Wintun, error) {
                        }
 
                        // This interface is not using Wintun driver.
-                       return nil, errors.New("Foreign network interface with the same name exists")
+                       return nil, windows.ERROR_ALREADY_EXISTS
                }
        }