Unify interface-specific network registry key open

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2019-03-04 11:58:02 +01:00 committed by Jason A. Donenfeld
parent 68f0721c6a
commit cddfd9a0d8

View File

@ -13,10 +13,10 @@ import (
"time" "time"
"unsafe" "unsafe"
"golang.zx2c4.com/wireguard/tun/wintun/guid"
"golang.zx2c4.com/wireguard/tun/wintun/setupapi"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry" "golang.org/x/sys/windows/registry"
"golang.zx2c4.com/wireguard/tun/wintun/guid"
"golang.zx2c4.com/wireguard/tun/wintun/setupapi"
) )
type Wintun windows.GUID type Wintun windows.GUID
@ -422,11 +422,9 @@ func getInterfaceId(deviceInfoSet setupapi.DevInfo, deviceInfoData *setupapi.Dev
// GetInterfaceName returns network interface name. // GetInterfaceName returns network interface name.
// //
func (wintun *Wintun) GetInterfaceName() (string, error) { func (wintun *Wintun) GetInterfaceName() (string, error) {
ifid := (*windows.GUID)(wintun) key, err := wintun.openNetRegKey(registry.QUERY_VALUE)
// Open network interface registry key.
key, err := registry.OpenKey(registry.LOCAL_MACHINE, fmt.Sprintf("SYSTEM\\CurrentControlSet\\Control\\Network\\%v\\%v\\Connection", guid.ToString(&deviceClassNetGUID), guid.ToString(ifid)), registry.QUERY_VALUE)
if err != nil { if err != nil {
return "", errors.New("Network-specific registry key open failed: " + err.Error()) return "", err
} }
defer key.Close() defer key.Close()
@ -438,11 +436,9 @@ func (wintun *Wintun) GetInterfaceName() (string, error) {
// SetInterfaceName sets network interface name. // SetInterfaceName sets network interface name.
// //
func (wintun *Wintun) SetInterfaceName(ifname string) error { func (wintun *Wintun) SetInterfaceName(ifname string) error {
ifid := (*windows.GUID)(wintun) key, err := wintun.openNetRegKey(registry.SET_VALUE)
// Open network interface registry key.
key, err := registry.OpenKey(registry.LOCAL_MACHINE, fmt.Sprintf("SYSTEM\\CurrentControlSet\\Control\\Network\\%v\\%v\\Connection", guid.ToString(&deviceClassNetGUID), guid.ToString(ifid)), registry.SET_VALUE)
if err != nil { if err != nil {
return errors.New("Network-specific registry key open failed: " + err.Error()) return err
} }
defer key.Close() defer key.Close()
@ -450,6 +446,19 @@ func (wintun *Wintun) SetInterfaceName(ifname string) error {
return key.SetStringValue("Name", ifname) return key.SetStringValue("Name", ifname)
} }
//
// openNetRegKey opens interface-specific network registry key.
//
func (wintun *Wintun) openNetRegKey(access uint32) (registry.Key, error) {
ifid := (*windows.GUID)(wintun)
key, err := registry.OpenKey(registry.LOCAL_MACHINE, fmt.Sprintf("SYSTEM\\CurrentControlSet\\Control\\Network\\%v\\%v\\Connection", guid.ToString(&deviceClassNetGUID), guid.ToString(ifid)), access)
if err != nil {
return 0, errors.New("Network-specific registry key open failed: " + err.Error())
}
return key, nil
}
// //
// getRegStringValue function reads a string value from registry. // getRegStringValue function reads a string value from registry.
// //