package tun
import (
+ "errors"
"fmt"
"io/ioutil"
"net"
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
}
package tun
import (
+ "errors"
"fmt"
"io/ioutil"
"net"
}
}
-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" {
} 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
}
}