wintun: normalize variable names for their types

This commit is contained in:
Jason A. Donenfeld 2019-10-17 15:30:56 +02:00
parent c85e4a410f
commit 95fbfccf60

View file

@ -40,9 +40,9 @@ const (
) )
// makeWintun creates a Wintun interface handle and populates it from the device's registry key. // makeWintun creates a Wintun interface handle and populates it from the device's registry key.
func makeWintun(deviceInfoSet setupapi.DevInfo, deviceInfoData *setupapi.DevInfoData, pool Pool) (*Interface, error) { func makeWintun(devInfo setupapi.DevInfo, devInfoData *setupapi.DevInfoData, pool Pool) (*Interface, error) {
// Open HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\<class>\<id> registry key. // Open HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\<class>\<id> registry key.
key, err := deviceInfoSet.OpenDevRegKey(deviceInfoData, setupapi.DICS_FLAG_GLOBAL, 0, setupapi.DIREG_DRV, registry.QUERY_VALUE) key, err := devInfo.OpenDevRegKey(devInfoData, setupapi.DICS_FLAG_GLOBAL, 0, setupapi.DIREG_DRV, registry.QUERY_VALUE)
if err != nil { if err != nil {
return nil, fmt.Errorf("Device-specific registry key open failed: %v", err) return nil, fmt.Errorf("Device-specific registry key open failed: %v", err)
} }
@ -72,7 +72,7 @@ func makeWintun(deviceInfoSet setupapi.DevInfo, deviceInfoData *setupapi.DevInfo
return nil, fmt.Errorf("RegQueryValue(\"*IfType\") failed: %v", err) return nil, fmt.Errorf("RegQueryValue(\"*IfType\") failed: %v", err)
} }
instanceID, err := deviceInfoSet.DeviceInstanceID(deviceInfoData) instanceID, err := devInfo.DeviceInstanceID(devInfoData)
if err != nil { if err != nil {
return nil, fmt.Errorf("DeviceInstanceID failed: %v", err) return nil, fmt.Errorf("DeviceInstanceID failed: %v", err)
} }
@ -109,11 +109,11 @@ func (pool Pool) GetInterface(ifname string) (*Interface, error) {
}() }()
// Create a list of network devices. // Create a list of network devices.
devInfoList, err := setupapi.SetupDiGetClassDevsEx(&deviceClassNetGUID, "", 0, setupapi.DIGCF_PRESENT, setupapi.DevInfo(0), "") devInfo, err := setupapi.SetupDiGetClassDevsEx(&deviceClassNetGUID, "", 0, setupapi.DIGCF_PRESENT, setupapi.DevInfo(0), "")
if err != nil { if err != nil {
return nil, fmt.Errorf("SetupDiGetClassDevsEx(%v) failed: %v", deviceClassNetGUID, err) return nil, fmt.Errorf("SetupDiGetClassDevsEx(%v) failed: %v", deviceClassNetGUID, err)
} }
defer devInfoList.Close() defer devInfo.Close()
// Windows requires each interface to have a different name. When // Windows requires each interface to have a different name. When
// enforcing this, Windows treats interface names case-insensitive. If an // enforcing this, Windows treats interface names case-insensitive. If an
@ -123,7 +123,7 @@ func (pool Pool) GetInterface(ifname string) (*Interface, error) {
ifname = strings.ToLower(ifname) ifname = strings.ToLower(ifname)
for index := 0; ; index++ { for index := 0; ; index++ {
deviceData, err := devInfoList.EnumDeviceInfo(index) devInfoData, err := devInfo.EnumDeviceInfo(index)
if err != nil { if err != nil {
if err == windows.ERROR_NO_MORE_ITEMS { if err == windows.ERROR_NO_MORE_ITEMS {
break break
@ -132,7 +132,7 @@ func (pool Pool) GetInterface(ifname string) (*Interface, error) {
} }
// Check the Hardware ID to make sure it's a real Wintun device first. This avoids doing slow operations on non-Wintun devices. // Check the Hardware ID to make sure it's a real Wintun device first. This avoids doing slow operations on non-Wintun devices.
property, err := devInfoList.DeviceRegistryProperty(deviceData, setupapi.SPDRP_HARDWAREID) property, err := devInfo.DeviceRegistryProperty(devInfoData, setupapi.SPDRP_HARDWAREID)
if err != nil { if err != nil {
continue continue
} }
@ -140,7 +140,7 @@ func (pool Pool) GetInterface(ifname string) (*Interface, error) {
continue continue
} }
wintun, err := makeWintun(devInfoList, deviceData, pool) wintun, err := makeWintun(devInfo, devInfoData, pool)
if err != nil { if err != nil {
continue continue
} }
@ -154,14 +154,14 @@ func (pool Pool) GetInterface(ifname string) (*Interface, error) {
ifname3 := removeNumberedSuffix(ifname2) ifname3 := removeNumberedSuffix(ifname2)
if ifname == ifname2 || ifname == ifname3 { if ifname == ifname2 || ifname == ifname3 {
err = devInfoList.BuildDriverInfoList(deviceData, setupapi.SPDIT_COMPATDRIVER) err = devInfo.BuildDriverInfoList(devInfoData, setupapi.SPDIT_COMPATDRIVER)
if err != nil { if err != nil {
return nil, fmt.Errorf("SetupDiBuildDriverInfoList failed: %v", err) return nil, fmt.Errorf("SetupDiBuildDriverInfoList failed: %v", err)
} }
defer devInfoList.DestroyDriverInfoList(deviceData, setupapi.SPDIT_COMPATDRIVER) defer devInfo.DestroyDriverInfoList(devInfoData, setupapi.SPDIT_COMPATDRIVER)
for index := 0; ; index++ { for index := 0; ; index++ {
driverData, err := devInfoList.EnumDriverInfo(deviceData, setupapi.SPDIT_COMPATDRIVER, index) driverData, err := devInfo.EnumDriverInfo(devInfoData, setupapi.SPDIT_COMPATDRIVER, index)
if err != nil { if err != nil {
if err == windows.ERROR_NO_MORE_ITEMS { if err == windows.ERROR_NO_MORE_ITEMS {
break break
@ -170,13 +170,13 @@ func (pool Pool) GetInterface(ifname string) (*Interface, error) {
} }
// Get driver info details. // Get driver info details.
driverDetailData, err := devInfoList.DriverInfoDetail(deviceData, driverData) driverDetailData, err := devInfo.DriverInfoDetail(devInfoData, driverData)
if err != nil { if err != nil {
continue continue
} }
if driverDetailData.IsCompatible(hardwareID) { if driverDetailData.IsCompatible(hardwareID) {
isMember, err := pool.isMember(devInfoList, deviceData) isMember, err := pool.isMember(devInfo, devInfoData)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -215,12 +215,12 @@ func (pool Pool) CreateInterface(ifname string, requestedGUID *windows.GUID) (wi
}() }()
// Create an empty device info set for network adapter device class. // Create an empty device info set for network adapter device class.
devInfoList, err := setupapi.SetupDiCreateDeviceInfoListEx(&deviceClassNetGUID, 0, "") devInfo, err := setupapi.SetupDiCreateDeviceInfoListEx(&deviceClassNetGUID, 0, "")
if err != nil { if err != nil {
err = fmt.Errorf("SetupDiCreateDeviceInfoListEx(%v) failed: %v", deviceClassNetGUID, err) err = fmt.Errorf("SetupDiCreateDeviceInfoListEx(%v) failed: %v", deviceClassNetGUID, err)
return return
} }
defer devInfoList.Close() defer devInfo.Close()
// Get the device class name from GUID. // Get the device class name from GUID.
className, err := setupapi.SetupDiClassNameFromGuidEx(&deviceClassNetGUID, "") className, err := setupapi.SetupDiClassNameFromGuidEx(&deviceClassNetGUID, "")
@ -231,43 +231,43 @@ func (pool Pool) CreateInterface(ifname string, requestedGUID *windows.GUID) (wi
// Create a new device info element and add it to the device info set. // Create a new device info element and add it to the device info set.
deviceTypeName := pool.deviceTypeName() deviceTypeName := pool.deviceTypeName()
deviceData, err := devInfoList.CreateDeviceInfo(className, &deviceClassNetGUID, deviceTypeName, 0, setupapi.DICD_GENERATE_ID) devInfoData, err := devInfo.CreateDeviceInfo(className, &deviceClassNetGUID, deviceTypeName, 0, setupapi.DICD_GENERATE_ID)
if err != nil { if err != nil {
err = fmt.Errorf("SetupDiCreateDeviceInfo failed: %v", err) err = fmt.Errorf("SetupDiCreateDeviceInfo failed: %v", err)
return return
} }
err = setQuietInstall(devInfoList, deviceData) err = setQuietInstall(devInfo, devInfoData)
if err != nil { if err != nil {
err = fmt.Errorf("Setting quiet installation failed: %v", err) err = fmt.Errorf("Setting quiet installation failed: %v", err)
return return
} }
// Set a device information element as the selected member of a device information set. // Set a device information element as the selected member of a device information set.
err = devInfoList.SetSelectedDevice(deviceData) err = devInfo.SetSelectedDevice(devInfoData)
if err != nil { if err != nil {
err = fmt.Errorf("SetupDiSetSelectedDevice failed: %v", err) err = fmt.Errorf("SetupDiSetSelectedDevice failed: %v", err)
return return
} }
// Set Plug&Play device hardware ID property. // Set Plug&Play device hardware ID property.
err = devInfoList.SetDeviceRegistryPropertyString(deviceData, setupapi.SPDRP_HARDWAREID, hardwareID) err = devInfo.SetDeviceRegistryPropertyString(devInfoData, setupapi.SPDRP_HARDWAREID, hardwareID)
if err != nil { if err != nil {
err = fmt.Errorf("SetupDiSetDeviceRegistryProperty(SPDRP_HARDWAREID) failed: %v", err) err = fmt.Errorf("SetupDiSetDeviceRegistryProperty(SPDRP_HARDWAREID) failed: %v", err)
return return
} }
err = devInfoList.BuildDriverInfoList(deviceData, setupapi.SPDIT_COMPATDRIVER) // TODO: This takes ~510ms err = devInfo.BuildDriverInfoList(devInfoData, setupapi.SPDIT_COMPATDRIVER) // TODO: This takes ~510ms
if err != nil { if err != nil {
err = fmt.Errorf("SetupDiBuildDriverInfoList failed: %v", err) err = fmt.Errorf("SetupDiBuildDriverInfoList failed: %v", err)
return return
} }
defer devInfoList.DestroyDriverInfoList(deviceData, setupapi.SPDIT_COMPATDRIVER) defer devInfo.DestroyDriverInfoList(devInfoData, setupapi.SPDIT_COMPATDRIVER)
driverDate := windows.Filetime{} driverDate := windows.Filetime{}
driverVersion := uint64(0) driverVersion := uint64(0)
for index := 0; ; index++ { // TODO: This loop takes ~600ms for index := 0; ; index++ { // TODO: This loop takes ~600ms
driverData, err := devInfoList.EnumDriverInfo(deviceData, setupapi.SPDIT_COMPATDRIVER, index) driverData, err := devInfo.EnumDriverInfo(devInfoData, setupapi.SPDIT_COMPATDRIVER, index)
if err != nil { if err != nil {
if err == windows.ERROR_NO_MORE_ITEMS { if err == windows.ERROR_NO_MORE_ITEMS {
break break
@ -277,13 +277,13 @@ func (pool Pool) CreateInterface(ifname string, requestedGUID *windows.GUID) (wi
// Check the driver version first, since the check is trivial and will save us iterating over hardware IDs for any driver versioned prior our best match. // Check the driver version first, since the check is trivial and will save us iterating over hardware IDs for any driver versioned prior our best match.
if driverData.IsNewer(driverDate, driverVersion) { if driverData.IsNewer(driverDate, driverVersion) {
driverDetailData, err := devInfoList.DriverInfoDetail(deviceData, driverData) driverDetailData, err := devInfo.DriverInfoDetail(devInfoData, driverData)
if err != nil { if err != nil {
continue continue
} }
if driverDetailData.IsCompatible(hardwareID) { if driverDetailData.IsCompatible(hardwareID) {
err := devInfoList.SetSelectedDriver(deviceData, driverData) err := devInfo.SetSelectedDriver(devInfoData, driverData)
if err != nil { if err != nil {
continue continue
} }
@ -308,10 +308,10 @@ func (pool Pool) CreateInterface(ifname string, requestedGUID *windows.GUID) (wi
} }
// Set class installer parameters for DIF_REMOVE. // Set class installer parameters for DIF_REMOVE.
if devInfoList.SetClassInstallParams(deviceData, &removeDeviceParams.ClassInstallHeader, uint32(unsafe.Sizeof(removeDeviceParams))) == nil { if devInfo.SetClassInstallParams(devInfoData, &removeDeviceParams.ClassInstallHeader, uint32(unsafe.Sizeof(removeDeviceParams))) == nil {
// Call appropriate class installer. // Call appropriate class installer.
if devInfoList.CallClassInstaller(setupapi.DIF_REMOVE, deviceData) == nil { if devInfo.CallClassInstaller(setupapi.DIF_REMOVE, devInfoData) == nil {
rebootRequired = rebootRequired || checkReboot(devInfoList, deviceData) rebootRequired = rebootRequired || checkReboot(devInfo, devInfoData)
} }
} }
@ -320,14 +320,14 @@ func (pool Pool) CreateInterface(ifname string, requestedGUID *windows.GUID) (wi
}() }()
// Call appropriate class installer. // Call appropriate class installer.
err = devInfoList.CallClassInstaller(setupapi.DIF_REGISTERDEVICE, deviceData) err = devInfo.CallClassInstaller(setupapi.DIF_REGISTERDEVICE, devInfoData)
if err != nil { if err != nil {
err = fmt.Errorf("SetupDiCallClassInstaller(DIF_REGISTERDEVICE) failed: %v", err) err = fmt.Errorf("SetupDiCallClassInstaller(DIF_REGISTERDEVICE) failed: %v", err)
return return
} }
// Register device co-installers if any. (Ignore errors) // Register device co-installers if any. (Ignore errors)
devInfoList.CallClassInstaller(setupapi.DIF_REGISTER_COINSTALLERS, deviceData) devInfo.CallClassInstaller(setupapi.DIF_REGISTER_COINSTALLERS, devInfoData)
var netDevRegKey registry.Key var netDevRegKey registry.Key
const pollTimeout = time.Millisecond * 50 const pollTimeout = time.Millisecond * 50
@ -335,7 +335,7 @@ func (pool Pool) CreateInterface(ifname string, requestedGUID *windows.GUID) (wi
if i != 0 { if i != 0 {
time.Sleep(pollTimeout) time.Sleep(pollTimeout)
} }
netDevRegKey, err = devInfoList.OpenDevRegKey(deviceData, setupapi.DICS_FLAG_GLOBAL, 0, setupapi.DIREG_DRV, registry.SET_VALUE|registry.QUERY_VALUE|registry.NOTIFY) netDevRegKey, err = devInfo.OpenDevRegKey(devInfoData, setupapi.DICS_FLAG_GLOBAL, 0, setupapi.DIREG_DRV, registry.SET_VALUE|registry.QUERY_VALUE|registry.NOTIFY)
if err == nil { if err == nil {
break break
} }
@ -354,17 +354,17 @@ func (pool Pool) CreateInterface(ifname string, requestedGUID *windows.GUID) (wi
} }
// Install interfaces if any. (Ignore errors) // Install interfaces if any. (Ignore errors)
devInfoList.CallClassInstaller(setupapi.DIF_INSTALLINTERFACES, deviceData) devInfo.CallClassInstaller(setupapi.DIF_INSTALLINTERFACES, devInfoData)
// Install the device. // Install the device.
err = devInfoList.CallClassInstaller(setupapi.DIF_INSTALLDEVICE, deviceData) err = devInfo.CallClassInstaller(setupapi.DIF_INSTALLDEVICE, devInfoData)
if err != nil { if err != nil {
err = fmt.Errorf("SetupDiCallClassInstaller(DIF_INSTALLDEVICE) failed: %v", err) err = fmt.Errorf("SetupDiCallClassInstaller(DIF_INSTALLDEVICE) failed: %v", err)
return return
} }
rebootRequired = checkReboot(devInfoList, deviceData) rebootRequired = checkReboot(devInfo, devInfoData)
err = devInfoList.SetDeviceRegistryPropertyString(deviceData, setupapi.SPDRP_DEVICEDESC, deviceTypeName) err = devInfo.SetDeviceRegistryPropertyString(devInfoData, setupapi.SPDRP_DEVICEDESC, deviceTypeName)
if err != nil { if err != nil {
err = fmt.Errorf("SetDeviceRegistryPropertyString(SPDRP_DEVICEDESC) failed: %v", err) err = fmt.Errorf("SetDeviceRegistryPropertyString(SPDRP_DEVICEDESC) failed: %v", err)
return return
@ -390,7 +390,7 @@ func (pool Pool) CreateInterface(ifname string, requestedGUID *windows.GUID) (wi
} }
// Get network interface. // Get network interface.
wintun, err = makeWintun(devInfoList, deviceData, pool) wintun, err = makeWintun(devInfo, devInfoData, pool)
if err != nil { if err != nil {
err = fmt.Errorf("makeWintun failed: %v", err) err = fmt.Errorf("makeWintun failed: %v", err)
return return
@ -444,14 +444,14 @@ func (pool Pool) CreateInterface(ifname string, requestedGUID *windows.GUID) (wi
// if the interface was not found. It returns a bool indicating whether // if the interface was not found. It returns a bool indicating whether
// a reboot is required. // a reboot is required.
func (wintun *Interface) DeleteInterface() (rebootRequired bool, err error) { func (wintun *Interface) DeleteInterface() (rebootRequired bool, err error) {
devInfoList, deviceData, err := wintun.deviceData() devInfo, devInfoData, err := wintun.devInfoData()
if err == windows.ERROR_OBJECT_NOT_FOUND { if err == windows.ERROR_OBJECT_NOT_FOUND {
return false, nil return false, nil
} }
if err != nil { if err != nil {
return false, err return false, err
} }
defer devInfoList.Close() defer devInfo.Close()
// Remove the device. // Remove the device.
removeDeviceParams := setupapi.RemoveDeviceParams{ removeDeviceParams := setupapi.RemoveDeviceParams{
@ -460,18 +460,18 @@ func (wintun *Interface) DeleteInterface() (rebootRequired bool, err error) {
} }
// Set class installer parameters for DIF_REMOVE. // Set class installer parameters for DIF_REMOVE.
err = devInfoList.SetClassInstallParams(deviceData, &removeDeviceParams.ClassInstallHeader, uint32(unsafe.Sizeof(removeDeviceParams))) err = devInfo.SetClassInstallParams(devInfoData, &removeDeviceParams.ClassInstallHeader, uint32(unsafe.Sizeof(removeDeviceParams)))
if err != nil { if err != nil {
return false, fmt.Errorf("SetupDiSetClassInstallParams failed: %v", err) return false, fmt.Errorf("SetupDiSetClassInstallParams failed: %v", err)
} }
// Call appropriate class installer. // Call appropriate class installer.
err = devInfoList.CallClassInstaller(setupapi.DIF_REMOVE, deviceData) err = devInfo.CallClassInstaller(setupapi.DIF_REMOVE, devInfoData)
if err != nil { if err != nil {
return false, fmt.Errorf("SetupDiCallClassInstaller failed: %v", err) return false, fmt.Errorf("SetupDiCallClassInstaller failed: %v", err)
} }
return checkReboot(devInfoList, deviceData), nil return checkReboot(devInfo, devInfoData), nil
} }
// DeleteMatchingInterfaces deletes all Wintun interfaces, which match // DeleteMatchingInterfaces deletes all Wintun interfaces, which match
@ -488,14 +488,14 @@ func (pool Pool) DeleteMatchingInterfaces(matches func(wintun *Interface) bool)
windows.CloseHandle(mutex) windows.CloseHandle(mutex)
}() }()
devInfoList, err := setupapi.SetupDiGetClassDevsEx(&deviceClassNetGUID, "", 0, setupapi.DIGCF_PRESENT, setupapi.DevInfo(0), "") devInfo, err := setupapi.SetupDiGetClassDevsEx(&deviceClassNetGUID, "", 0, setupapi.DIGCF_PRESENT, setupapi.DevInfo(0), "")
if err != nil { if err != nil {
return nil, false, []error{fmt.Errorf("SetupDiGetClassDevsEx(%v) failed: %v", deviceClassNetGUID, err.Error())} return nil, false, []error{fmt.Errorf("SetupDiGetClassDevsEx(%v) failed: %v", deviceClassNetGUID, err.Error())}
} }
defer devInfoList.Close() defer devInfo.Close()
for i := 0; ; i++ { for i := 0; ; i++ {
deviceData, err := devInfoList.EnumDeviceInfo(i) devInfoData, err := devInfo.EnumDeviceInfo(i)
if err != nil { if err != nil {
if err == windows.ERROR_NO_MORE_ITEMS { if err == windows.ERROR_NO_MORE_ITEMS {
break break
@ -504,7 +504,7 @@ func (pool Pool) DeleteMatchingInterfaces(matches func(wintun *Interface) bool)
} }
// Check the Hardware ID to make sure it's a real Wintun device first. This avoids doing slow operations on non-Wintun devices. // Check the Hardware ID to make sure it's a real Wintun device first. This avoids doing slow operations on non-Wintun devices.
property, err := devInfoList.DeviceRegistryProperty(deviceData, setupapi.SPDRP_HARDWAREID) property, err := devInfo.DeviceRegistryProperty(devInfoData, setupapi.SPDRP_HARDWAREID)
if err != nil { if err != nil {
continue continue
} }
@ -512,22 +512,22 @@ func (pool Pool) DeleteMatchingInterfaces(matches func(wintun *Interface) bool)
continue continue
} }
err = devInfoList.BuildDriverInfoList(deviceData, setupapi.SPDIT_COMPATDRIVER) err = devInfo.BuildDriverInfoList(devInfoData, setupapi.SPDIT_COMPATDRIVER)
if err != nil { if err != nil {
continue continue
} }
defer devInfoList.DestroyDriverInfoList(deviceData, setupapi.SPDIT_COMPATDRIVER) defer devInfo.DestroyDriverInfoList(devInfoData, setupapi.SPDIT_COMPATDRIVER)
isWintun := false isWintun := false
for j := 0; ; j++ { for j := 0; ; j++ {
driverData, err := devInfoList.EnumDriverInfo(deviceData, setupapi.SPDIT_COMPATDRIVER, j) driverData, err := devInfo.EnumDriverInfo(devInfoData, setupapi.SPDIT_COMPATDRIVER, j)
if err != nil { if err != nil {
if err == windows.ERROR_NO_MORE_ITEMS { if err == windows.ERROR_NO_MORE_ITEMS {
break break
} }
continue continue
} }
driverDetailData, err := devInfoList.DriverInfoDetail(deviceData, driverData) driverDetailData, err := devInfo.DriverInfoDetail(devInfoData, driverData)
if err != nil { if err != nil {
continue continue
} }
@ -540,7 +540,7 @@ func (pool Pool) DeleteMatchingInterfaces(matches func(wintun *Interface) bool)
continue continue
} }
isMember, err := pool.isMember(devInfoList, deviceData) isMember, err := pool.isMember(devInfo, devInfoData)
if err != nil { if err != nil {
errors = append(errors, err) errors = append(errors, err)
continue continue
@ -549,7 +549,7 @@ func (pool Pool) DeleteMatchingInterfaces(matches func(wintun *Interface) bool)
continue continue
} }
wintun, err := makeWintun(devInfoList, deviceData, pool) wintun, err := makeWintun(devInfo, devInfoData, pool)
if err != nil { if err != nil {
errors = append(errors, fmt.Errorf("Unable to make Wintun interface object: %v", err)) errors = append(errors, fmt.Errorf("Unable to make Wintun interface object: %v", err))
continue continue
@ -558,41 +558,41 @@ func (pool Pool) DeleteMatchingInterfaces(matches func(wintun *Interface) bool)
continue continue
} }
err = setQuietInstall(devInfoList, deviceData) err = setQuietInstall(devInfo, devInfoData)
if err != nil { if err != nil {
errors = append(errors, err) errors = append(errors, err)
continue continue
} }
inst := deviceData.DevInst inst := devInfoData.DevInst
removeDeviceParams := setupapi.RemoveDeviceParams{ removeDeviceParams := setupapi.RemoveDeviceParams{
ClassInstallHeader: *setupapi.MakeClassInstallHeader(setupapi.DIF_REMOVE), ClassInstallHeader: *setupapi.MakeClassInstallHeader(setupapi.DIF_REMOVE),
Scope: setupapi.DI_REMOVEDEVICE_GLOBAL, Scope: setupapi.DI_REMOVEDEVICE_GLOBAL,
} }
err = devInfoList.SetClassInstallParams(deviceData, &removeDeviceParams.ClassInstallHeader, uint32(unsafe.Sizeof(removeDeviceParams))) err = devInfo.SetClassInstallParams(devInfoData, &removeDeviceParams.ClassInstallHeader, uint32(unsafe.Sizeof(removeDeviceParams)))
if err != nil { if err != nil {
errors = append(errors, err) errors = append(errors, err)
continue continue
} }
err = devInfoList.CallClassInstaller(setupapi.DIF_REMOVE, deviceData) err = devInfo.CallClassInstaller(setupapi.DIF_REMOVE, devInfoData)
if err != nil { if err != nil {
errors = append(errors, err) errors = append(errors, err)
continue continue
} }
rebootRequired = rebootRequired || checkReboot(devInfoList, deviceData) rebootRequired = rebootRequired || checkReboot(devInfo, devInfoData)
deviceInstancesDeleted = append(deviceInstancesDeleted, inst) deviceInstancesDeleted = append(deviceInstancesDeleted, inst)
} }
return return
} }
// isMember checks if SPDRP_DEVICEDESC or SPDRP_FRIENDLYNAME match device type name. // isMember checks if SPDRP_DEVICEDESC or SPDRP_FRIENDLYNAME match device type name.
func (pool Pool) isMember(deviceInfoSet setupapi.DevInfo, deviceInfoData *setupapi.DevInfoData) (bool, error) { func (pool Pool) isMember(devInfo setupapi.DevInfo, devInfoData *setupapi.DevInfoData) (bool, error) {
deviceDescVal, err := deviceInfoSet.DeviceRegistryProperty(deviceInfoData, setupapi.SPDRP_DEVICEDESC) deviceDescVal, err := devInfo.DeviceRegistryProperty(devInfoData, setupapi.SPDRP_DEVICEDESC)
if err != nil { if err != nil {
return false, fmt.Errorf("DeviceRegistryPropertyString(SPDRP_DEVICEDESC) failed: %v", err) return false, fmt.Errorf("DeviceRegistryPropertyString(SPDRP_DEVICEDESC) failed: %v", err)
} }
deviceDesc, _ := deviceDescVal.(string) deviceDesc, _ := deviceDescVal.(string)
friendlyNameVal, err := deviceInfoSet.DeviceRegistryProperty(deviceInfoData, setupapi.SPDRP_FRIENDLYNAME) friendlyNameVal, err := devInfo.DeviceRegistryProperty(devInfoData, setupapi.SPDRP_FRIENDLYNAME)
if err != nil { if err != nil {
return false, fmt.Errorf("DeviceRegistryPropertyString(SPDRP_FRIENDLYNAME) failed: %v", err) return false, fmt.Errorf("DeviceRegistryPropertyString(SPDRP_FRIENDLYNAME) failed: %v", err)
} }
@ -603,8 +603,8 @@ func (pool Pool) isMember(deviceInfoSet setupapi.DevInfo, deviceInfoData *setupa
} }
// checkReboot checks device install parameters if a system reboot is required. // checkReboot checks device install parameters if a system reboot is required.
func checkReboot(deviceInfoSet setupapi.DevInfo, deviceInfoData *setupapi.DevInfoData) bool { func checkReboot(devInfo setupapi.DevInfo, devInfoData *setupapi.DevInfoData) bool {
devInstallParams, err := deviceInfoSet.DeviceInstallParams(deviceInfoData) devInstallParams, err := devInfo.DeviceInstallParams(devInfoData)
if err != nil { if err != nil {
return false return false
} }
@ -613,14 +613,14 @@ func checkReboot(deviceInfoSet setupapi.DevInfo, deviceInfoData *setupapi.DevInf
} }
// setQuietInstall sets device install parameters for a quiet installation // setQuietInstall sets device install parameters for a quiet installation
func setQuietInstall(deviceInfoSet setupapi.DevInfo, deviceInfoData *setupapi.DevInfoData) error { func setQuietInstall(devInfo setupapi.DevInfo, devInfoData *setupapi.DevInfoData) error {
devInstallParams, err := deviceInfoSet.DeviceInstallParams(deviceInfoData) devInstallParams, err := devInfo.DeviceInstallParams(devInfoData)
if err != nil { if err != nil {
return err return err
} }
devInstallParams.Flags |= setupapi.DI_QUIETINSTALL devInstallParams.Flags |= setupapi.DI_QUIETINSTALL
return deviceInfoSet.SetDeviceInstallParams(deviceInfoData, devInstallParams) return devInfo.SetDeviceInstallParams(devInfoData, devInstallParams)
} }
// deviceTypeName returns pool-specific device type name. // deviceTypeName returns pool-specific device type name.
@ -739,18 +739,18 @@ func (wintun *Interface) tcpipInterfaceRegKeyName() (path string, err error) {
return fmt.Sprintf("SYSTEM\\CurrentControlSet\\Services\\%s", paths[0]), nil return fmt.Sprintf("SYSTEM\\CurrentControlSet\\Services\\%s", paths[0]), nil
} }
// deviceData returns TUN device info list handle and interface device info // devInfoData returns TUN device info list handle and interface device info
// data. The device info list handle must be closed after use. In case the // data. The device info list handle must be closed after use. In case the
// device is not found, windows.ERROR_OBJECT_NOT_FOUND is returned. // device is not found, windows.ERROR_OBJECT_NOT_FOUND is returned.
func (wintun *Interface) deviceData() (setupapi.DevInfo, *setupapi.DevInfoData, error) { func (wintun *Interface) devInfoData() (setupapi.DevInfo, *setupapi.DevInfoData, error) {
// Create a list of network devices. // Create a list of network devices.
devInfoList, err := setupapi.SetupDiGetClassDevsEx(&deviceClassNetGUID, "", 0, setupapi.DIGCF_PRESENT, setupapi.DevInfo(0), "") devInfo, err := setupapi.SetupDiGetClassDevsEx(&deviceClassNetGUID, "", 0, setupapi.DIGCF_PRESENT, setupapi.DevInfo(0), "")
if err != nil { if err != nil {
return 0, nil, fmt.Errorf("SetupDiGetClassDevsEx(%v) failed: %v", deviceClassNetGUID, err.Error()) return 0, nil, fmt.Errorf("SetupDiGetClassDevsEx(%v) failed: %v", deviceClassNetGUID, err.Error())
} }
for index := 0; ; index++ { for index := 0; ; index++ {
deviceData, err := devInfoList.EnumDeviceInfo(index) devInfoData, err := devInfo.EnumDeviceInfo(index)
if err != nil { if err != nil {
if err == windows.ERROR_NO_MORE_ITEMS { if err == windows.ERROR_NO_MORE_ITEMS {
break break
@ -760,22 +760,22 @@ func (wintun *Interface) deviceData() (setupapi.DevInfo, *setupapi.DevInfoData,
// Get interface ID. // Get interface ID.
// TODO: Store some ID in the Wintun object such that this call isn't required. // TODO: Store some ID in the Wintun object such that this call isn't required.
wintun2, err := makeWintun(devInfoList, deviceData, wintun.pool) wintun2, err := makeWintun(devInfo, devInfoData, wintun.pool)
if err != nil { if err != nil {
continue continue
} }
if wintun.cfgInstanceID == wintun2.cfgInstanceID { if wintun.cfgInstanceID == wintun2.cfgInstanceID {
err = setQuietInstall(devInfoList, deviceData) err = setQuietInstall(devInfo, devInfoData)
if err != nil { if err != nil {
devInfoList.Close() devInfo.Close()
return 0, nil, fmt.Errorf("Setting quiet installation failed: %v", err) return 0, nil, fmt.Errorf("Setting quiet installation failed: %v", err)
} }
return devInfoList, deviceData, nil return devInfo, devInfoData, nil
} }
} }
devInfoList.Close() devInfo.Close()
return 0, nil, windows.ERROR_OBJECT_NOT_FOUND return 0, nil, windows.ERROR_OBJECT_NOT_FOUND
} }