66package tun
77
88import (
9- "errors"
109 "fmt"
1110 "io"
1211 "net"
1312 "os"
1413 "sync"
1514 "syscall"
16- "time"
1715 "unsafe"
1816
1917 "golang.org/x/sys/unix"
@@ -30,18 +28,6 @@ type NativeTun struct {
3028 closeOnce sync.Once
3129}
3230
33- func retryInterfaceByIndex (index int ) (iface * net.Interface , err error ) {
34- for i := 0 ; i < 20 ; i ++ {
35- iface , err = net .InterfaceByIndex (index )
36- if err != nil && errors .Is (err , unix .ENOMEM ) {
37- time .Sleep (time .Duration (i ) * time .Second / 3 )
38- continue
39- }
40- return iface , err
41- }
42- return nil , err
43- }
44-
4531func (tun * NativeTun ) routineRouteListener (tunIfindex int ) {
4632 var (
4733 statusUp bool
@@ -62,26 +48,22 @@ func (tun *NativeTun) routineRouteListener(tunIfindex int) {
6248 return
6349 }
6450
65- if n < 14 {
51+ if n < 28 {
6652 continue
6753 }
6854
69- if data [3 /* type */ ] != unix .RTM_IFINFO {
55+ if data [3 /* ifm_type */ ] != unix .RTM_IFINFO {
7056 continue
7157 }
72- ifindex := int (* (* uint16 )(unsafe .Pointer (& data [12 /* ifindex */ ])))
58+ ifindex := int (* (* uint16 )(unsafe .Pointer (& data [12 /* ifm_index */ ])))
7359 if ifindex != tunIfindex {
7460 continue
7561 }
7662
77- iface , err := retryInterfaceByIndex (ifindex )
78- if err != nil {
79- tun .errors <- err
80- return
81- }
63+ flags := int (* (* uint32 )(unsafe .Pointer (& data [8 /* ifm_flags */ ])))
8264
8365 // Up / Down event
84- up := (iface . Flags & net . FlagUp ) != 0
66+ up := (flags & syscall . IFF_UP ) != 0
8567 if up != statusUp && up {
8668 tun .events <- EventUp
8769 }
@@ -90,11 +72,13 @@ func (tun *NativeTun) routineRouteListener(tunIfindex int) {
9072 }
9173 statusUp = up
9274
75+ mtu := int (* (* uint32 )(unsafe .Pointer (& data [24 /* ifm_data.ifi_mtu */ ])))
76+
9377 // MTU changes
94- if iface . MTU != statusMTU {
78+ if mtu != statusMTU {
9579 tun .events <- EventMTUUpdate
9680 }
97- statusMTU = iface . MTU
81+ statusMTU = mtu
9882 }
9983}
10084
0 commit comments