]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
tun: use errors.Is for unwrapping
authorJason A. Donenfeld <Jason@zx2c4.com>
Tue, 9 Feb 2021 18:48:27 +0000 (19:48 +0100)
committerJason A. Donenfeld <Jason@zx2c4.com>
Tue, 9 Feb 2021 18:50:31 +0000 (19:50 +0100)
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
tun/tun_darwin.go
tun/tun_openbsd.go

index 7c7f3e28d9122ceaebbb36f626b934dae481e316..f513d03b86779197884825bf06a465964fa0bc11 100644 (file)
@@ -6,6 +6,7 @@
 package tun
 
 import (
+       "errors"
        "fmt"
        "io/ioutil"
        "net"
@@ -31,13 +32,9 @@ type NativeTun struct {
 func retryInterfaceByIndex(index int) (iface *net.Interface, err error) {
        for i := 0; i < 20; i++ {
                iface, err = net.InterfaceByIndex(index)
-               if err != nil {
-                       if opErr, ok := err.(*net.OpError); ok {
-                               if syscallErr, ok := opErr.Err.(*os.SyscallError); ok && syscallErr.Err == syscall.ENOMEM {
-                                       time.Sleep(time.Duration(i) * time.Second / 3)
-                                       continue
-                               }
-                       }
+               if err != nil && errors.Is(err, syscall.ENOMEM) {
+                       time.Sleep(time.Duration(i) * time.Second / 3)
+                       continue
                }
                return iface, err
        }
index ab4e99fcc3218aaa15e75e6ce69c4824a4b056da..b2815d7d7b9ced59755d15ac6678496a97651ac7 100644 (file)
@@ -6,6 +6,7 @@
 package tun
 
 import (
+       "errors"
        "fmt"
        "io/ioutil"
        "net"
@@ -99,16 +100,6 @@ func (tun *NativeTun) routineRouteListener(tunIfindex int) {
        }
 }
 
-func errorIsEBUSY(err error) bool {
-       if pe, ok := err.(*os.PathError); ok {
-               err = pe.Err
-       }
-       if errno, ok := err.(syscall.Errno); ok && errno == syscall.EBUSY {
-               return true
-       }
-       return false
-}
-
 func CreateTUN(name string, mtu int) (Device, error) {
        ifIndex := -1
        if name != "tun" {
@@ -126,7 +117,7 @@ func CreateTUN(name string, mtu int) (Device, error) {
        } else {
                for ifIndex = 0; ifIndex < 256; ifIndex++ {
                        tunfile, err = os.OpenFile(fmt.Sprintf("/dev/tun%d", ifIndex), unix.O_RDWR, 0)
-                       if err == nil || !errorIsEBUSY(err) {
+                       if err == nil || !errors.Is(err, syscall.EBUSY) {
                                break
                        }
                }