diff options
-rw-r--r-- | tun/tun_openbsd.go | 54 |
1 files changed, 31 insertions, 23 deletions
diff --git a/tun/tun_openbsd.go b/tun/tun_openbsd.go index bbc0432..44cedaa 100644 --- a/tun/tun_openbsd.go +++ b/tun/tun_openbsd.go @@ -42,13 +42,41 @@ func (tun *NativeTun) routineRouteListener(tunIfindex int) { defer close(tun.events) + check := func() bool { + iface, err := net.InterfaceByIndex(tunIfindex) + if err != nil { + tun.errors <- err + return true + } + + // Up / Down event + up := (iface.Flags & net.FlagUp) != 0 + if up != statusUp && up { + tun.events <- EventUp + } + if up != statusUp && !up { + tun.events <- EventDown + } + statusUp = up + + // MTU changes + if iface.MTU != statusMTU { + tun.events <- EventMTUUpdate + } + statusMTU = iface.MTU + return false + } + + if check() { + return + } + data := make([]byte, os.Getpagesize()) for { - retry: n, err := unix.Read(tun.routeSocket, data) if err != nil { if errno, ok := err.(syscall.Errno); ok && errno == syscall.EINTR { - goto retry + continue } tun.errors <- err return @@ -65,28 +93,9 @@ func (tun *NativeTun) routineRouteListener(tunIfindex int) { if ifindex != tunIfindex { continue } - - iface, err := net.InterfaceByIndex(ifindex) - if err != nil { - tun.errors <- err + if check() { return } - - // Up / Down event - up := (iface.Flags & net.FlagUp) != 0 - if up != statusUp && up { - tun.events <- EventUp - } - if up != statusUp && !up { - tun.events <- EventDown - } - statusUp = up - - // MTU changes - if iface.MTU != statusMTU { - tun.events <- EventMTUUpdate - } - statusMTU = iface.MTU } } @@ -140,7 +149,6 @@ func CreateTUN(name string, mtu int) (Device, error) { } func CreateTUNFromFile(file *os.File, mtu int) (Device, error) { - tun := &NativeTun{ tunFile: file, events: make(chan Event, 10), |