From 3341e2d4448e51152c2e919f0a694c5667fa91dd Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Fri, 19 Jul 2019 13:51:56 +0200 Subject: tun: windows: get rid of retry logic Things work fine on Windows 8. --- tun/wintun/wintun_windows.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'tun/wintun') diff --git a/tun/wintun/wintun_windows.go b/tun/wintun/wintun_windows.go index e8eadf5..88d565d 100644 --- a/tun/wintun/wintun_windows.go +++ b/tun/wintun/wintun_windows.go @@ -612,21 +612,25 @@ func (wintun *Wintun) deviceData() (setupapi.DevInfo, *setupapi.DevInfoData, err return 0, nil, windows.ERROR_OBJECT_NOT_FOUND } -// NdisFileName returns the Wintun NDIS device object name. -func (wintun *Wintun) NdisFileName() (string, error) { +// AdapterHandle returns a handle to the adapter device object. +func (wintun *Wintun) AdapterHandle() (windows.Handle, error) { key, err := registry.OpenKey(registry.LOCAL_MACHINE, wintun.netRegKeyName(), registry.QUERY_VALUE) if err != nil { - return "", fmt.Errorf("Network-specific registry key open failed: %v", err) + return windows.InvalidHandle, fmt.Errorf("Network-specific registry key open failed: %v", err) } defer key.Close() // Get the interface name. pnpInstanceID, err := registryEx.GetStringValue(key, "PnPInstanceId") if err != nil { - return "", fmt.Errorf("PnpInstanceId registry key read failed: %v", err) + return windows.InvalidHandle, fmt.Errorf("PnPInstanceId registry key read failed: %v", err) } mangledPnpNode := strings.ReplaceAll(fmt.Sprintf("%s\\{cac88484-7515-4c03-82e6-71a87abac361}", pnpInstanceID), "\\", "#") - return fmt.Sprintf("\\\\.\\Global\\%s", mangledPnpNode), nil + handle, err := windows.CreateFile(windows.StringToUTF16Ptr(fmt.Sprintf("\\\\.\\Global\\%s", mangledPnpNode)), windows.GENERIC_READ|windows.GENERIC_WRITE, 0, nil, windows.OPEN_EXISTING, 0, 0) + if err != nil { + return windows.InvalidHandle, fmt.Errorf("CreateFile on mangled PnPInstanceId path failed: %v", err) + } + return handle, nil } // GUID returns the GUID of the interface. -- cgit v1.2.3