From 700860f8e697b187a7bbd7b226469a1de0818456 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Mon, 10 Jun 2019 11:10:49 +0200 Subject: wintun: simplify error matching and remove dumb comments --- tun/wintun/wintun_windows.go | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) (limited to 'tun') diff --git a/tun/wintun/wintun_windows.go b/tun/wintun/wintun_windows.go index e5b768e..7b7a889 100644 --- a/tun/wintun/wintun_windows.go +++ b/tun/wintun/wintun_windows.go @@ -94,23 +94,20 @@ func GetInterface(ifname string) (*Wintun, error) { ifname = strings.ToLower(ifname) for index := 0; ; index++ { - // 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.(windows.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { + if err == windows.ERROR_NO_MORE_ITEMS { break } continue } - // Get interface ID. wintun, err := makeWintun(devInfoList, deviceData) if err != nil { continue } // TODO: is there a better way than comparing ifnames? - // Get interface name. ifname2, err := wintun.InterfaceName() if err != nil { continue @@ -126,25 +123,21 @@ func GetInterface(ifname string) (*Wintun, error) { defer devInfoList.DestroyDriverInfoList(deviceData, driverType) for index := 0; ; index++ { - // Get a driver from the list. driverData, err := devInfoList.EnumDriverInfo(deviceData, driverType, index) if err != nil { - if errWin, ok := err.(windows.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { + if err == windows.ERROR_NO_MORE_ITEMS { break } - // Something is wrong with this driver. Skip it. continue } // Get driver info details. driverDetailData, err := devInfoList.DriverInfoDetail(deviceData, driverData) if err != nil { - // Something is wrong with this driver. Skip it. continue } if driverDetailData.IsCompatible(hardwareID) { - // Matching hardware ID found. return wintun, nil } } @@ -204,7 +197,6 @@ func CreateInterface(description string, requestedGUID *windows.GUID) (wintun *W return nil, false, fmt.Errorf("SetupDiSetDeviceRegistryProperty(SPDRP_HARDWAREID) failed: %v", err) } - // Search for the driver. const driverType = setupapi.SPDIT_COMPATDRIVER err = devInfoList.BuildDriverInfoList(deviceData, driverType) // TODO: This takes ~510ms if err != nil { @@ -215,30 +207,24 @@ func CreateInterface(description string, requestedGUID *windows.GUID) (wintun *W driverDate := windows.Filetime{} driverVersion := uint64(0) for index := 0; ; index++ { // TODO: This loop takes ~600ms - // Get a driver from the list. driverData, err := devInfoList.EnumDriverInfo(deviceData, driverType, index) if err != nil { - if errWin, ok := err.(windows.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { + if err == windows.ERROR_NO_MORE_ITEMS { break } - // Something is wrong with this driver. Skip it. continue } // Check the driver version first, since the check is trivial and will save us iterating over hardware IDs for any driver versioned prior our best match. if driverData.IsNewer(driverDate, driverVersion) { - // Get driver info details. driverDetailData, err := devInfoList.DriverInfoDetail(deviceData, driverData) if err != nil { - // Something is wrong with this driver. Skip it. continue } if driverDetailData.IsCompatible(hardwareID) { - // Matching hardware ID found. Select the driver. err := devInfoList.SetSelectedDriver(deviceData, driverData) if err != nil { - // Something is wrong with this driver. Skip it. continue } @@ -521,10 +507,9 @@ func (wintun *Wintun) deviceData() (setupapi.DevInfo, *setupapi.DevInfoData, err } for index := 0; ; index++ { - // 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.(windows.Errno); ok && errWin == windows.ERROR_NO_MORE_ITEMS { + if err == windows.ERROR_NO_MORE_ITEMS { break } continue -- cgit v1.2.3