From 26fb615b11a581e399771448c5ae19a01e7be7f2 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Tue, 27 Aug 2019 10:54:49 -0600 Subject: wintun: cleanup earlier --- tun/wintun/wintun_windows.go | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) (limited to 'tun/wintun') diff --git a/tun/wintun/wintun_windows.go b/tun/wintun/wintun_windows.go index f5d42eb..3566a76 100644 --- a/tun/wintun/wintun_windows.go +++ b/tun/wintun/wintun_windows.go @@ -257,6 +257,26 @@ func CreateInterface(requestedGUID *windows.GUID) (wintun *Wintun, rebootRequire return } + defer func() { + if err != nil { + // The interface failed to install, or the interface ID was unobtainable. Clean-up. + removeDeviceParams := setupapi.RemoveDeviceParams{ + ClassInstallHeader: *setupapi.MakeClassInstallHeader(setupapi.DIF_REMOVE), + Scope: setupapi.DI_REMOVEDEVICE_GLOBAL, + } + + // Set class installer parameters for DIF_REMOVE. + if devInfoList.SetClassInstallParams(deviceData, &removeDeviceParams.ClassInstallHeader, uint32(unsafe.Sizeof(removeDeviceParams))) == nil { + // Call appropriate class installer. + if devInfoList.CallClassInstaller(setupapi.DIF_REMOVE, deviceData) == nil { + rebootRequired = rebootRequired || checkReboot(devInfoList, deviceData) + } + } + + wintun = nil + } + }() + // Call appropriate class installer. err = devInfoList.CallClassInstaller(setupapi.DIF_REGISTERDEVICE, deviceData) if err != nil { @@ -293,25 +313,6 @@ func CreateInterface(requestedGUID *windows.GUID) (wintun *Wintun, rebootRequire // Install interfaces if any. (Ignore errors) devInfoList.CallClassInstaller(setupapi.DIF_INSTALLINTERFACES, deviceData) - defer func() { - if err != nil { - // The interface failed to install, or the interface ID was unobtainable. Clean-up. - removeDeviceParams := setupapi.RemoveDeviceParams{ - ClassInstallHeader: *setupapi.MakeClassInstallHeader(setupapi.DIF_REMOVE), - Scope: setupapi.DI_REMOVEDEVICE_GLOBAL, - } - - // Set class installer parameters for DIF_REMOVE. - if devInfoList.SetClassInstallParams(deviceData, &removeDeviceParams.ClassInstallHeader, uint32(unsafe.Sizeof(removeDeviceParams))) == nil { - // Call appropriate class installer. - if devInfoList.CallClassInstaller(setupapi.DIF_REMOVE, deviceData) == nil { - rebootRequired = rebootRequired || checkReboot(devInfoList, deviceData) - } - } - - wintun = nil - } - }() // Install the device. err = devInfoList.CallClassInstaller(setupapi.DIF_INSTALLDEVICE, deviceData) -- cgit v1.2.3