From 625e445b22afbb4dab20eed07640388ea36b332d Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Tue, 4 Jun 2019 14:54:56 +0200 Subject: setupapi, wintun: replace syscall with golang.org/x/sys/windows Signed-off-by: Simon Rozman --- tun/wintun/setupapi/setupapi_windows.go | 23 +++++++++++------------ tun/wintun/setupapi/setupapi_windows_test.go | 25 ++++++++++++------------- tun/wintun/setupapi/types_windows.go | 11 +++++------ tun/wintun/wintun_windows.go | 9 ++++----- 4 files changed, 32 insertions(+), 36 deletions(-) (limited to 'tun') diff --git a/tun/wintun/setupapi/setupapi_windows.go b/tun/wintun/setupapi/setupapi_windows.go index 2148a67..1ac1419 100644 --- a/tun/wintun/setupapi/setupapi_windows.go +++ b/tun/wintun/setupapi/setupapi_windows.go @@ -9,7 +9,6 @@ import ( "encoding/binary" "fmt" "runtime" - "syscall" "unsafe" "golang.org/x/sys/windows" @@ -22,7 +21,7 @@ import ( func SetupDiCreateDeviceInfoListEx(classGUID *windows.GUID, hwndParent uintptr, machineName string) (deviceInfoSet DevInfo, err error) { var machineNameUTF16 *uint16 if machineName != "" { - machineNameUTF16, err = syscall.UTF16PtrFromString(machineName) + machineNameUTF16, err = windows.UTF16PtrFromString(machineName) if err != nil { return } @@ -49,14 +48,14 @@ func (deviceInfoSet DevInfo) DeviceInfoListDetail() (*DevInfoListDetailData, err // SetupDiCreateDeviceInfo function creates a new device information element and adds it as a new member to the specified device information set. func SetupDiCreateDeviceInfo(deviceInfoSet DevInfo, deviceName string, classGUID *windows.GUID, deviceDescription string, hwndParent uintptr, creationFlags DICD) (deviceInfoData *DevInfoData, err error) { - deviceNameUTF16, err := syscall.UTF16PtrFromString(deviceName) + deviceNameUTF16, err := windows.UTF16PtrFromString(deviceName) if err != nil { return } var deviceDescriptionUTF16 *uint16 if deviceDescription != "" { - deviceDescriptionUTF16, err = syscall.UTF16PtrFromString(deviceDescription) + deviceDescriptionUTF16, err = windows.UTF16PtrFromString(deviceDescription) if err != nil { return } @@ -165,7 +164,7 @@ func SetupDiGetDriverInfoDetail(deviceInfoSet DevInfo, deviceInfoData *DevInfoDa return data, nil } - if errWin, ok := err.(syscall.Errno); ok && errWin == windows.ERROR_INSUFFICIENT_BUFFER { + if errWin, ok := err.(windows.Errno); ok && errWin == windows.ERROR_INSUFFICIENT_BUFFER { // The buffer was too small. Now that we got the required size, create another one big enough and retry. buf := make([]byte, bufLen) data := (*DrvInfoDetailData)(unsafe.Pointer(&buf[0])) @@ -199,14 +198,14 @@ func (deviceInfoSet DevInfo) DestroyDriverInfoList(deviceInfoData *DevInfoData, func SetupDiGetClassDevsEx(classGUID *windows.GUID, enumerator string, hwndParent uintptr, flags DIGCF, deviceInfoSet DevInfo, machineName string) (handle DevInfo, err error) { var enumeratorUTF16 *uint16 if enumerator != "" { - enumeratorUTF16, err = syscall.UTF16PtrFromString(enumerator) + enumeratorUTF16, err = windows.UTF16PtrFromString(enumerator) if err != nil { return } } var machineNameUTF16 *uint16 if machineName != "" { - machineNameUTF16, err = syscall.UTF16PtrFromString(machineName) + machineNameUTF16, err = windows.UTF16PtrFromString(machineName) if err != nil { return } @@ -247,7 +246,7 @@ func SetupDiGetDeviceRegistryProperty(deviceInfoSet DevInfo, deviceInfoData *Dev return getRegistryValue(buf[:bufLen], dataType) } - if errWin, ok := err.(syscall.Errno); ok && errWin == windows.ERROR_INSUFFICIENT_BUFFER { + if errWin, ok := err.(windows.Errno); ok && errWin == windows.ERROR_INSUFFICIENT_BUFFER { // The buffer was too small. Now that we got the required size, create another one big enough and retry. buf = make([]byte, bufLen) err = setupDiGetDeviceRegistryProperty(deviceInfoSet, deviceInfoData, property, &dataType, &buf[0], uint32(cap(buf)), &bufLen) @@ -396,7 +395,7 @@ func SetupDiClassNameFromGuidEx(classGUID *windows.GUID, machineName string) (cl var machineNameUTF16 *uint16 if machineName != "" { - machineNameUTF16, err = syscall.UTF16PtrFromString(machineName) + machineNameUTF16, err = windows.UTF16PtrFromString(machineName) if err != nil { return } @@ -415,7 +414,7 @@ func SetupDiClassNameFromGuidEx(classGUID *windows.GUID, machineName string) (cl // SetupDiClassGuidsFromNameEx function retrieves the GUIDs associated with the specified class name. This resulting list contains the classes currently installed on a local or remote computer. func SetupDiClassGuidsFromNameEx(className string, machineName string) ([]windows.GUID, error) { - classNameUTF16, err := syscall.UTF16PtrFromString(className) + classNameUTF16, err := windows.UTF16PtrFromString(className) if err != nil { return nil, err } @@ -426,7 +425,7 @@ func SetupDiClassGuidsFromNameEx(className string, machineName string) ([]window var machineNameUTF16 *uint16 if machineName != "" { - machineNameUTF16, err = syscall.UTF16PtrFromString(machineName) + machineNameUTF16, err = windows.UTF16PtrFromString(machineName) if err != nil { return nil, err } @@ -438,7 +437,7 @@ func SetupDiClassGuidsFromNameEx(className string, machineName string) ([]window return buf[:bufLen], nil } - if errWin, ok := err.(syscall.Errno); ok && errWin == windows.ERROR_INSUFFICIENT_BUFFER { + if errWin, ok := err.(windows.Errno); ok && errWin == windows.ERROR_INSUFFICIENT_BUFFER { // The GUID array was too small. Now that we got the required size, create another one big enough and retry. buf := make([]windows.GUID, bufLen) err = setupDiClassGuidsFromNameEx(classNameUTF16, &buf[0], bufLen, &bufLen, machineNameUTF16, 0) diff --git a/tun/wintun/setupapi/setupapi_windows_test.go b/tun/wintun/setupapi/setupapi_windows_test.go index ede1078..a2edab8 100644 --- a/tun/wintun/setupapi/setupapi_windows_test.go +++ b/tun/wintun/setupapi/setupapi_windows_test.go @@ -8,7 +8,6 @@ package setupapi import ( "runtime" "strings" - "syscall" "testing" "golang.org/x/sys/windows" @@ -114,7 +113,7 @@ func TestSetupDiCreateDeviceInfo(t *testing.T) { devInfoData, err := devInfoList.CreateDeviceInfo(deviceClassNetName, &deviceClassNetGUID, "This is a test device", 0, DICD_GENERATE_ID) if err != nil { // Access denied is expected, as the SetupDiCreateDeviceInfo() require elevation to succeed. - if errWin, ok := err.(syscall.Errno); !ok || errWin != windows.ERROR_ACCESS_DENIED { + if errWin, ok := err.(windows.Errno); !ok || errWin != windows.ERROR_ACCESS_DENIED { t.Errorf("Error calling SetupDiCreateDeviceInfo: %s", err.Error()) } } else if devInfoData.ClassGUID != deviceClassNetGUID { @@ -132,7 +131,7 @@ func TestSetupDiEnumDeviceInfo(t *testing.T) { for i := 0; true; i++ { data, err := devInfoList.EnumDeviceInfo(i) if err != nil { - if errWin, ok := err.(syscall.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { + if errWin, ok := err.(windows.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { break } continue @@ -154,7 +153,7 @@ func TestDevInfo_BuildDriverInfoList(t *testing.T) { for i := 0; true; i++ { deviceData, err := devInfoList.EnumDeviceInfo(i) if err != nil { - if errWin, ok := err.(syscall.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { + if errWin, ok := err.(windows.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { break } continue @@ -171,7 +170,7 @@ func TestDevInfo_BuildDriverInfoList(t *testing.T) { for j := 0; true; j++ { driverData, err := devInfoList.EnumDriverInfo(deviceData, driverType, j) if err != nil { - if errWin, ok := err.(syscall.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { + if errWin, ok := err.(windows.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { break } continue @@ -265,7 +264,7 @@ func TestSetupDiGetClassDevsEx(t *testing.T) { devInfoList.Close() t.Errorf("SetupDiGetClassDevsEx(nil, ...) should fail") } else { - if errWin, ok := err.(syscall.Errno); !ok || errWin != windows.ERROR_INVALID_PARAMETER { + if errWin, ok := err.(windows.Errno); !ok || errWin != windows.ERROR_INVALID_PARAMETER { t.Errorf("SetupDiGetClassDevsEx(nil, ...) should fail with ERROR_INVALID_PARAMETER") } } @@ -281,7 +280,7 @@ func TestSetupDiOpenDevRegKey(t *testing.T) { for i := 0; true; i++ { data, err := devInfoList.EnumDeviceInfo(i) if err != nil { - if errWin, ok := err.(syscall.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { + if errWin, ok := err.(windows.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { break } continue @@ -305,7 +304,7 @@ func TestSetupDiGetDeviceRegistryProperty(t *testing.T) { for i := 0; true; i++ { data, err := devInfoList.EnumDeviceInfo(i) if err != nil { - if errWin, ok := err.(syscall.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { + if errWin, ok := err.(windows.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { break } continue @@ -335,7 +334,7 @@ func TestSetupDiGetDeviceRegistryProperty(t *testing.T) { val, err = devInfoList.DeviceRegistryProperty(data, SPDRP_COMPATIBLEIDS) if err != nil { // Some devices have no SPDRP_COMPATIBLEIDS. - if errWin, ok := err.(syscall.Errno); !ok || errWin != windows.ERROR_INVALID_DATA { + if errWin, ok := err.(windows.Errno); !ok || errWin != windows.ERROR_INVALID_DATA { t.Errorf("Error calling SetupDiGetDeviceRegistryProperty(SPDRP_COMPATIBLEIDS): %s", err.Error()) } } @@ -362,7 +361,7 @@ func TestSetupDiGetDeviceInstallParams(t *testing.T) { for i := 0; true; i++ { data, err := devInfoList.EnumDeviceInfo(i) if err != nil { - if errWin, ok := err.(syscall.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { + if errWin, ok := err.(windows.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { break } continue @@ -400,7 +399,7 @@ func TestSetupDiClassNameFromGuidEx(t *testing.T) { if err == nil { t.Errorf("SetupDiClassNameFromGuidEx(nil) should fail") } else { - if errWin, ok := err.(syscall.Errno); !ok || errWin != windows.ERROR_INVALID_USER_BUFFER { + if errWin, ok := err.(windows.Errno); !ok || errWin != windows.ERROR_INVALID_USER_BUFFER { t.Errorf("SetupDiClassNameFromGuidEx(nil) should fail with ERROR_INVALID_USER_BUFFER") } } @@ -441,7 +440,7 @@ func TestSetupDiGetSelectedDevice(t *testing.T) { for i := 0; true; i++ { data, err := devInfoList.EnumDeviceInfo(i) if err != nil { - if errWin, ok := err.(syscall.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { + if errWin, ok := err.(windows.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { break } continue @@ -464,7 +463,7 @@ func TestSetupDiGetSelectedDevice(t *testing.T) { if err == nil { t.Errorf("SetupDiSetSelectedDevice(nil) should fail") } else { - if errWin, ok := err.(syscall.Errno); !ok || errWin != windows.ERROR_INVALID_PARAMETER { + if errWin, ok := err.(windows.Errno); !ok || errWin != windows.ERROR_INVALID_PARAMETER { t.Errorf("SetupDiSetSelectedDevice(nil) should fail with ERROR_INVALID_USER_BUFFER") } } diff --git a/tun/wintun/setupapi/types_windows.go b/tun/wintun/setupapi/types_windows.go index cf2b718..6ceda09 100644 --- a/tun/wintun/setupapi/types_windows.go +++ b/tun/wintun/setupapi/types_windows.go @@ -7,7 +7,6 @@ package setupapi import ( "strings" - "syscall" "unsafe" "golang.org/x/sys/windows" @@ -71,7 +70,7 @@ func (data *DevInfoListDetailData) RemoteMachineName() string { } func (data *DevInfoListDetailData) SetRemoteMachineName(remoteMachineName string) error { - str, err := syscall.UTF16FromString(remoteMachineName) + str, err := windows.UTF16FromString(remoteMachineName) if err != nil { return err } @@ -143,7 +142,7 @@ func (params *DevInstallParams) DriverPath() string { } func (params *DevInstallParams) SetDriverPath(driverPath string) error { - str, err := syscall.UTF16FromString(driverPath) + str, err := windows.UTF16FromString(driverPath) if err != nil { return err } @@ -328,7 +327,7 @@ func (data *DrvInfoData) Description() string { } func (data *DrvInfoData) SetDescription(description string) error { - str, err := syscall.UTF16FromString(description) + str, err := windows.UTF16FromString(description) if err != nil { return err } @@ -341,7 +340,7 @@ func (data *DrvInfoData) MfgName() string { } func (data *DrvInfoData) SetMfgName(mfgName string) error { - str, err := syscall.UTF16FromString(mfgName) + str, err := windows.UTF16FromString(mfgName) if err != nil { return err } @@ -354,7 +353,7 @@ func (data *DrvInfoData) ProviderName() string { } func (data *DrvInfoData) SetProviderName(providerName string) error { - str, err := syscall.UTF16FromString(providerName) + str, err := windows.UTF16FromString(providerName) if err != nil { return err } diff --git a/tun/wintun/wintun_windows.go b/tun/wintun/wintun_windows.go index 9dc4030..6daa4d7 100644 --- a/tun/wintun/wintun_windows.go +++ b/tun/wintun/wintun_windows.go @@ -9,7 +9,6 @@ import ( "errors" "fmt" "strings" - "syscall" "time" "unsafe" @@ -112,7 +111,7 @@ func GetInterface(ifname string, hwndParent uintptr) (*Wintun, error) { // Get the device from the list. Should anything be wrong with this device, continue with next. deviceData, err := devInfoList.EnumDeviceInfo(index) if err != nil { - if errWin, ok := err.(syscall.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { + if errWin, ok := err.(windows.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { break } continue @@ -144,7 +143,7 @@ func GetInterface(ifname string, hwndParent uintptr) (*Wintun, error) { // Get a driver from the list. driverData, err := devInfoList.EnumDriverInfo(deviceData, driverType, index) if err != nil { - if errWin, ok := err.(syscall.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { + if errWin, ok := err.(windows.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { break } // Something is wrong with this driver. Skip it. @@ -239,7 +238,7 @@ func CreateInterface(description string, hwndParent uintptr) (*Wintun, bool, err // Get a driver from the list. driverData, err := devInfoList.EnumDriverInfo(deviceData, driverType, index) if err != nil { - if errWin, ok := err.(syscall.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { + if errWin, ok := err.(windows.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { break } // Something is wrong with this driver. Skip it. @@ -434,7 +433,7 @@ func (wintun *Wintun) DeleteInterface(hwndParent uintptr) (bool, bool, error) { // Get the device from the list. Should anything be wrong with this device, continue with next. deviceData, err := devInfoList.EnumDeviceInfo(index) if err != nil { - if errWin, ok := err.(syscall.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { + if errWin, ok := err.(windows.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { break } continue -- cgit v1.2.3