wintun: defer requires unique variable

This commit is contained in:
Jason A. Donenfeld 2019-08-19 09:11:21 +02:00
parent d20459dc69
commit 333de75370

View File

@ -262,13 +262,13 @@ func CreateInterface(description string, requestedGUID *windows.GUID) (wintun *W
// Register device co-installers if any. (Ignore errors) // Register device co-installers if any. (Ignore errors)
devInfoList.CallClassInstaller(setupapi.DIF_REGISTER_COINSTALLERS, deviceData) devInfoList.CallClassInstaller(setupapi.DIF_REGISTER_COINSTALLERS, deviceData)
var key registry.Key var netDevRegKey registry.Key
const pollTimeout = time.Millisecond * 50 const pollTimeout = time.Millisecond * 50
for i := 0; i < int(waitForRegistryTimeout/pollTimeout); i++ { for i := 0; i < int(waitForRegistryTimeout/pollTimeout); i++ {
if i != 0 { if i != 0 {
time.Sleep(pollTimeout) time.Sleep(pollTimeout)
} }
key, err = devInfoList.OpenDevRegKey(deviceData, setupapi.DICS_FLAG_GLOBAL, 0, setupapi.DIREG_DRV, registry.SET_VALUE|registry.QUERY_VALUE|registry.NOTIFY) netDevRegKey, err = devInfoList.OpenDevRegKey(deviceData, setupapi.DICS_FLAG_GLOBAL, 0, setupapi.DIREG_DRV, registry.SET_VALUE|registry.QUERY_VALUE|registry.NOTIFY)
if err == nil { if err == nil {
break break
} }
@ -277,9 +277,9 @@ func CreateInterface(description string, requestedGUID *windows.GUID) (wintun *W
err = fmt.Errorf("SetupDiOpenDevRegKey failed: %v", err) err = fmt.Errorf("SetupDiOpenDevRegKey failed: %v", err)
return return
} }
defer key.Close() defer netDevRegKey.Close()
if requestedGUID != nil { if requestedGUID != nil {
err = key.SetStringValue("NetSetupAnticipatedInstanceId", requestedGUID.String()) err = netDevRegKey.SetStringValue("NetSetupAnticipatedInstanceId", requestedGUID.String())
if err != nil { if err != nil {
err = fmt.Errorf("SetStringValue(NetSetupAnticipatedInstanceId) failed: %v", err) err = fmt.Errorf("SetStringValue(NetSetupAnticipatedInstanceId) failed: %v", err)
return return
@ -319,17 +319,17 @@ func CreateInterface(description string, requestedGUID *windows.GUID) (wintun *W
// DIF_INSTALLDEVICE returns almost immediately, while the device installation // DIF_INSTALLDEVICE returns almost immediately, while the device installation
// continues in the background. It might take a while, before all registry // continues in the background. It might take a while, before all registry
// keys and values are populated. // keys and values are populated.
_, err = registryEx.GetStringValueWait(key, "NetCfgInstanceId", waitForRegistryTimeout) _, err = registryEx.GetStringValueWait(netDevRegKey, "NetCfgInstanceId", waitForRegistryTimeout)
if err != nil { if err != nil {
err = fmt.Errorf("GetStringValueWait(NetCfgInstanceId) failed: %v", err) err = fmt.Errorf("GetStringValueWait(NetCfgInstanceId) failed: %v", err)
return return
} }
_, err = registryEx.GetIntegerValueWait(key, "NetLuidIndex", waitForRegistryTimeout) _, err = registryEx.GetIntegerValueWait(netDevRegKey, "NetLuidIndex", waitForRegistryTimeout)
if err != nil { if err != nil {
err = fmt.Errorf("GetIntegerValueWait(NetLuidIndex) failed: %v", err) err = fmt.Errorf("GetIntegerValueWait(NetLuidIndex) failed: %v", err)
return return
} }
_, err = registryEx.GetIntegerValueWait(key, "*IfType", waitForRegistryTimeout) _, err = registryEx.GetIntegerValueWait(netDevRegKey, "*IfType", waitForRegistryTimeout)
if err != nil { if err != nil {
err = fmt.Errorf("GetIntegerValueWait(*IfType) failed: %v", err) err = fmt.Errorf("GetIntegerValueWait(*IfType) failed: %v", err)
return return
@ -343,25 +343,25 @@ func CreateInterface(description string, requestedGUID *windows.GUID) (wintun *W
} }
// Name ourselves. // Name ourselves.
deviceKey, err := registry.OpenKey(registry.LOCAL_MACHINE, wintun.deviceRegKeyName(), registry.SET_VALUE) deviceRegKey, err := registry.OpenKey(registry.LOCAL_MACHINE, wintun.deviceRegKeyName(), registry.SET_VALUE)
if err != nil { if err != nil {
err = fmt.Errorf("Device-level registry key open failed: %v", err) err = fmt.Errorf("Device-level registry key open failed: %v", err)
return return
} }
defer deviceKey.Close() defer deviceRegKey.Close()
err = deviceKey.SetStringValue("DeviceDesc", deviceTypeName) err = deviceRegKey.SetStringValue("DeviceDesc", deviceTypeName)
if err != nil { if err != nil {
err = fmt.Errorf("SetStringValue(DeviceDesc) failed: %v", err) err = fmt.Errorf("SetStringValue(DeviceDesc) failed: %v", err)
return return
} }
err = deviceKey.SetStringValue("FriendlyName", deviceTypeName) err = deviceRegKey.SetStringValue("FriendlyName", deviceTypeName)
if err != nil { if err != nil {
err = fmt.Errorf("SetStringValue(FriendlyName) failed: %v", err) err = fmt.Errorf("SetStringValue(FriendlyName) failed: %v", err)
return return
} }
// Wait for network registry key to emerge and populate. // Wait for network registry key to emerge and populate.
key, err = registryEx.OpenKeyWait( netRegKey, err := registryEx.OpenKeyWait(
registry.LOCAL_MACHINE, registry.LOCAL_MACHINE,
wintun.netRegKeyName(), wintun.netRegKeyName(),
registry.QUERY_VALUE|registry.NOTIFY, registry.QUERY_VALUE|registry.NOTIFY,
@ -370,20 +370,20 @@ func CreateInterface(description string, requestedGUID *windows.GUID) (wintun *W
err = fmt.Errorf("makeWintun failed: %v", err) err = fmt.Errorf("makeWintun failed: %v", err)
return return
} }
defer key.Close() defer netRegKey.Close()
_, err = registryEx.GetStringValueWait(key, "Name", waitForRegistryTimeout) _, err = registryEx.GetStringValueWait(netRegKey, "Name", waitForRegistryTimeout)
if err != nil { if err != nil {
err = fmt.Errorf("GetStringValueWait(Name) failed: %v", err) err = fmt.Errorf("GetStringValueWait(Name) failed: %v", err)
return return
} }
_, err = registryEx.GetStringValueWait(key, "PnPInstanceId", waitForRegistryTimeout) _, err = registryEx.GetStringValueWait(netRegKey, "PnPInstanceId", waitForRegistryTimeout)
if err != nil { if err != nil {
err = fmt.Errorf("GetStringValueWait(PnPInstanceId) failed: %v", err) err = fmt.Errorf("GetStringValueWait(PnPInstanceId) failed: %v", err)
return return
} }
// Wait for TCP/IP adapter registry key to emerge and populate. // Wait for TCP/IP adapter registry key to emerge and populate.
key, err = registryEx.OpenKeyWait( tcpipAdapterRegKey, err := registryEx.OpenKeyWait(
registry.LOCAL_MACHINE, registry.LOCAL_MACHINE,
wintun.tcpipAdapterRegKeyName(), registry.QUERY_VALUE|registry.NOTIFY, wintun.tcpipAdapterRegKeyName(), registry.QUERY_VALUE|registry.NOTIFY,
waitForRegistryTimeout) waitForRegistryTimeout)
@ -391,8 +391,8 @@ func CreateInterface(description string, requestedGUID *windows.GUID) (wintun *W
err = fmt.Errorf("OpenKeyWait(HKLM\\%s) failed: %v", wintun.tcpipAdapterRegKeyName(), err) err = fmt.Errorf("OpenKeyWait(HKLM\\%s) failed: %v", wintun.tcpipAdapterRegKeyName(), err)
return return
} }
defer key.Close() defer tcpipAdapterRegKey.Close()
_, err = registryEx.GetStringValueWait(key, "IpConfig", waitForRegistryTimeout) _, err = registryEx.GetStringValueWait(tcpipAdapterRegKey, "IpConfig", waitForRegistryTimeout)
if err != nil { if err != nil {
err = fmt.Errorf("GetStringValueWait(IpConfig) failed: %v", err) err = fmt.Errorf("GetStringValueWait(IpConfig) failed: %v", err)
return return
@ -405,28 +405,17 @@ func CreateInterface(description string, requestedGUID *windows.GUID) (wintun *W
} }
// Wait for TCP/IP interface registry key to emerge. // Wait for TCP/IP interface registry key to emerge.
key, err = registryEx.OpenKeyWait( tcpipInterfaceRegKey, err := registryEx.OpenKeyWait(
registry.LOCAL_MACHINE, registry.LOCAL_MACHINE,
tcpipInterfaceRegKeyName, registry.QUERY_VALUE, tcpipInterfaceRegKeyName, registry.QUERY_VALUE | registry.SET_VALUE,
waitForRegistryTimeout) waitForRegistryTimeout)
if err != nil { if err != nil {
err = fmt.Errorf("OpenKeyWait(HKLM\\%s) failed: %v", tcpipInterfaceRegKeyName, err) err = fmt.Errorf("OpenKeyWait(HKLM\\%s) failed: %v", tcpipInterfaceRegKeyName, err)
return return
} }
key.Close() defer tcpipInterfaceRegKey.Close()
//
// All the registry keys and values we're relying on are present now.
//
// Disable dead gateway detection on our interface. // Disable dead gateway detection on our interface.
key, err = registry.OpenKey(registry.LOCAL_MACHINE, tcpipInterfaceRegKeyName, registry.SET_VALUE) tcpipInterfaceRegKey.SetDWordValue("EnableDeadGWDetect", 0)
if err != nil {
err = fmt.Errorf("Error opening interface-specific TCP/IP network registry key: %v", err)
return
}
key.SetDWordValue("EnableDeadGWDetect", 0)
key.Close()
return return
} }