tun: use errors.Is for unwrapping

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2021-02-09 19:48:27 +01:00
parent 5cdb862f15
commit c040dea798
2 changed files with 6 additions and 18 deletions

View File

@ -6,6 +6,7 @@
package tun package tun
import ( import (
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net" "net"
@ -31,14 +32,10 @@ type NativeTun struct {
func retryInterfaceByIndex(index int) (iface *net.Interface, err error) { func retryInterfaceByIndex(index int) (iface *net.Interface, err error) {
for i := 0; i < 20; i++ { for i := 0; i < 20; i++ {
iface, err = net.InterfaceByIndex(index) iface, err = net.InterfaceByIndex(index)
if err != nil { if err != nil && errors.Is(err, syscall.ENOMEM) {
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) time.Sleep(time.Duration(i) * time.Second / 3)
continue continue
} }
}
}
return iface, err return iface, err
} }
return nil, err return nil, err

View File

@ -6,6 +6,7 @@
package tun package tun
import ( import (
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net" "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) { func CreateTUN(name string, mtu int) (Device, error) {
ifIndex := -1 ifIndex := -1
if name != "tun" { if name != "tun" {
@ -126,7 +117,7 @@ func CreateTUN(name string, mtu int) (Device, error) {
} else { } else {
for ifIndex = 0; ifIndex < 256; ifIndex++ { for ifIndex = 0; ifIndex < 256; ifIndex++ {
tunfile, err = os.OpenFile(fmt.Sprintf("/dev/tun%d", ifIndex), unix.O_RDWR, 0) 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 break
} }
} }