diff options
author | Simon Rozman <simon@rozman.si> | 2019-03-04 11:58:02 +0100 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-03-04 16:37:11 +0100 |
commit | cddfd9a0d8d8c2dcdd778aa46ed32228d8f6cb93 (patch) | |
tree | 1f907b200f1cc7a47a32e51a32379a805a113431 /tun/wintun | |
parent | 68f0721c6a1aea6bb97320ec0cf6b8f7f723603d (diff) |
Unify interface-specific network registry key open
Signed-off-by: Simon Rozman <simon@rozman.si>
Diffstat (limited to 'tun/wintun')
-rw-r--r-- | tun/wintun/wintun_windows.go | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/tun/wintun/wintun_windows.go b/tun/wintun/wintun_windows.go index a8fe3fa..875ca35 100644 --- a/tun/wintun/wintun_windows.go +++ b/tun/wintun/wintun_windows.go @@ -13,10 +13,10 @@ import ( "time" "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/registry" + "golang.zx2c4.com/wireguard/tun/wintun/guid" + "golang.zx2c4.com/wireguard/tun/wintun/setupapi" ) type Wintun windows.GUID @@ -422,11 +422,9 @@ func getInterfaceId(deviceInfoSet setupapi.DevInfo, deviceInfoData *setupapi.Dev // GetInterfaceName returns network interface name. // func (wintun *Wintun) GetInterfaceName() (string, error) { - ifid := (*windows.GUID)(wintun) - // 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) + key, err := wintun.openNetRegKey(registry.QUERY_VALUE) if err != nil { - return "", errors.New("Network-specific registry key open failed: " + err.Error()) + return "", err } defer key.Close() @@ -438,11 +436,9 @@ func (wintun *Wintun) GetInterfaceName() (string, error) { // SetInterfaceName sets network interface name. // func (wintun *Wintun) SetInterfaceName(ifname string) error { - ifid := (*windows.GUID)(wintun) - // 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) + key, err := wintun.openNetRegKey(registry.SET_VALUE) if err != nil { - return errors.New("Network-specific registry key open failed: " + err.Error()) + return err } defer key.Close() @@ -451,6 +447,19 @@ func (wintun *Wintun) SetInterfaceName(ifname string) error { } // +// 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. // // If the value type is REG_EXPAND_SZ the environment variables are expanded. |