diff options
author | Simon Rozman <simon@rozman.si> | 2019-02-07 22:02:51 +0100 |
---|---|---|
committer | Simon Rozman <simon@rozman.si> | 2019-02-07 22:02:51 +0100 |
commit | d87cbeeb2fb152b5f377758c68c655e09fe177f3 (patch) | |
tree | 1eb353b53e458b5b573fb2b2e552b15904ba5600 /tun/wintun/wintun_windows.go | |
parent | 043b7e8013674d58757fe3d3f4e4cacf89c46112 (diff) |
wintun: Detect if a foreign interface with the same name exists
Signed-off-by: Simon Rozman <simon@rozman.si>
Diffstat (limited to 'tun/wintun/wintun_windows.go')
-rw-r--r-- | tun/wintun/wintun_windows.go | 39 |
1 files changed, 36 insertions, 3 deletions
diff --git a/tun/wintun/wintun_windows.go b/tun/wintun/wintun_windows.go index 75180d5..f9fc309 100644 --- a/tun/wintun/wintun_windows.go +++ b/tun/wintun/wintun_windows.go @@ -35,7 +35,8 @@ const TUN_HWID = "Wintun" // hwndParent to 0. // // Function returns interface ID when the interface was found, or nil -// otherwise. +// otherwise. If the interface is found but not Wintun-class, the function +// returns interface ID with an error. // func GetInterface(ifname string, hwndParent uintptr) (*Wintun, error) { // Create a list of network devices. @@ -79,8 +80,40 @@ func GetInterface(ifname string, hwndParent uintptr) (*Wintun, error) { } if ifname == strings.ToLower(ifname2) { - // Interface name found. - return (*Wintun)(ifid), nil + // Interface name found. Check its driver. + const driverType = setupapi.SPDIT_COMPATDRIVER + err = devInfoList.BuildDriverInfoList(deviceData, driverType) + if err != nil { + return nil, err + } + defer devInfoList.DestroyDriverInfoList(deviceData, driverType) + + for index := 0; ; index++ { + // Get a driver from the list. + driverData, err := devInfoList.EnumDriverInfo(deviceData, driverType, index) + if err != nil { + if errWin, ok := err.(syscall.Errno); ok && errWin == 259 /*ERROR_NO_MORE_ITEMS*/ { + break + } + // Something is wrong with this driver. Skip it. + continue + } + + // Get driver info details. + driverDetailData, err := devInfoList.GetDriverInfoDetail(deviceData, driverData) + if err != nil { + // Something is wrong with this driver. Skip it. + continue + } + + if driverDetailData.IsCompatible(TUN_HWID) { + // Matching hardware ID found. + return (*Wintun)(ifid), nil + } + } + + // This interface is not using Wintun driver. + return (*Wintun)(ifid), errors.New("Foreign network interface with the same name exists") } } |