From a2249449d60f69c1878612e2c2c0b9b37b10ce1b Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Tue, 23 Jul 2019 14:58:46 +0200 Subject: wintun: get interface path properly with cfgmgr --- tun/wintun/setupapi/setupapi_windows.go | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'tun/wintun/setupapi/setupapi_windows.go') diff --git a/tun/wintun/setupapi/setupapi_windows.go b/tun/wintun/setupapi/setupapi_windows.go index 764b3e6..60a8eb7 100644 --- a/tun/wintun/setupapi/setupapi_windows.go +++ b/tun/wintun/setupapi/setupapi_windows.go @@ -469,3 +469,38 @@ func (deviceInfoSet DevInfo) SelectedDevice() (*DevInfoData, error) { func (deviceInfoSet DevInfo) SetSelectedDevice(deviceInfoData *DevInfoData) error { return SetupDiSetSelectedDevice(deviceInfoSet, deviceInfoData) } + +//sys cm_Get_Device_Interface_List_Size(len *uint32, interfaceClass *windows.GUID, deviceID *uint16, flags uint32) (ret uint32) = CfgMgr32.CM_Get_Device_Interface_List_SizeW +//sys cm_Get_Device_Interface_List(interfaceClass *windows.GUID, deviceID *uint16, buffer *uint16, bufferLen uint32, flags uint32) (ret uint32) = CfgMgr32.CM_Get_Device_Interface_ListW + +func CM_Get_Device_Interface_List(deviceID string, interfaceClass *windows.GUID, flags uint32) ([]string, error) { + deviceID16, err := windows.UTF16PtrFromString(deviceID) + if err != nil { + return nil, err + } + var buf []uint16 + var buflen uint32 + for { + if ret := cm_Get_Device_Interface_List_Size(&buflen, interfaceClass, deviceID16, flags); ret != CR_SUCCESS { + return nil, fmt.Errorf("CfgMgr error: 0x%x", ret) + } + buf = make([]uint16, buflen) + if ret := cm_Get_Device_Interface_List(interfaceClass, deviceID16, &buf[0], buflen, flags); ret == CR_SUCCESS { + break + } else if ret != CR_BUFFER_SMALL { + return nil, fmt.Errorf("CfgMgr error: 0x%x", ret) + } + } + var interfaces []string + for i := 0; i < len(buf); { + j := i + wcslen(buf[i:]) + if i < j { + interfaces = append(interfaces, windows.UTF16ToString(buf[i:j])) + } + i = j + 1 + } + if interfaces == nil { + return nil, fmt.Errorf("no interfaces found") + } + return interfaces, nil +} -- cgit v1.2.3