diff options
author | Simon Rozman <simon@rozman.si> | 2019-08-28 11:39:01 +0200 |
---|---|---|
committer | Simon Rozman <simon@rozman.si> | 2019-08-28 11:39:01 +0200 |
commit | 7305b4ce935828da5c2081c69a6b554a8c15ed53 (patch) | |
tree | daf0777ac1f3e2c0ec79a05041efff17b32c5c74 | |
parent | 26fb615b11a581e399771448c5ae19a01e7be7f2 (diff) |
wintun: upgrade deleting all interfaces and make it reusable
DeleteAllInterfaces() didn't check if SPDRP_DEVICEDESC == "WireGuard
Tunnel". It deleted _all_ Wintun adapters, not just WireGuard's.
Furthermore, the DeleteAllInterfaces() was upgraded into a new function
called DeleteMatchingInterfaces() for selectively deletion. This will
be used by WireGuard to clean stale Wintun adapters.
Signed-off-by: Simon Rozman <simon@rozman.si>
-rw-r--r-- | tun/wintun/wintun_windows.go | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/tun/wintun/wintun_windows.go b/tun/wintun/wintun_windows.go index 3566a76..7286435 100644 --- a/tun/wintun/wintun_windows.go +++ b/tun/wintun/wintun_windows.go @@ -426,10 +426,10 @@ func (wintun *Wintun) DeleteInterface() (rebootRequired bool, err error) { return checkReboot(devInfoList, deviceData), nil } -// DeleteAllInterfaces deletes all Wintun interfaces, and returns which -// ones it deleted, whether a reboot is required after, and which errors -// occurred during the process. -func DeleteAllInterfaces() (deviceInstancesDeleted []uint32, rebootRequired bool, errors []error) { +// DeleteMatchingInterfaces deletes all Wintun interfaces, which match +// given criteria, and returns which ones it deleted, whether a reboot +// is required after, and which errors occurred during the process. +func DeleteMatchingInterfaces(matches func(wintun *Wintun) bool) (deviceInstancesDeleted []uint32, rebootRequired bool, errors []error) { devInfoList, err := setupapi.SetupDiGetClassDevsEx(&deviceClassNetGUID, "", 0, setupapi.DIGCF_PRESENT, setupapi.DevInfo(0), "") if err != nil { return nil, false, []error{fmt.Errorf("SetupDiGetClassDevsEx(%v) failed: %v", deviceClassNetGUID, err.Error())} @@ -472,6 +472,22 @@ func DeleteAllInterfaces() (deviceInstancesDeleted []uint32, rebootRequired bool if !isWintun { continue } + deviceDescVal, err := devInfoList.DeviceRegistryProperty(deviceData, setupapi.SPDRP_DEVICEDESC) + if err != nil { + errors = append(errors, fmt.Errorf("DeviceRegistryPropertyString(SPDRP_DEVICEDESC) failed: %v", err)) + continue + } + if deviceDesc, ok := deviceDescVal.(string); !ok || deviceDesc != deviceTypeName { + continue + } + wintun, err := makeWintun(devInfoList, deviceData) + if err != nil { + errors = append(errors, fmt.Errorf("makeWintun failed: %v", err)) + continue + } + if !matches(wintun) { + continue + } err = setQuietInstall(devInfoList, deviceData) if err != nil { @@ -500,6 +516,15 @@ func DeleteAllInterfaces() (deviceInstancesDeleted []uint32, rebootRequired bool return } +// DeleteAllInterfaces deletes all Wintun interfaces, and returns which +// ones it deleted, whether a reboot is required after, and which errors +// occurred during the process. +func DeleteAllInterfaces() (deviceInstancesDeleted []uint32, rebootRequired bool, errors []error) { + return DeleteMatchingInterfaces(func(wintun *Wintun) bool { + return true + }) +} + // checkReboot checks device install parameters if a system reboot is required. func checkReboot(deviceInfoSet setupapi.DevInfo, deviceInfoData *setupapi.DevInfoData) bool { devInstallParams, err := deviceInfoSet.DeviceInstallParams(deviceInfoData) |