6
6
package tun
7
7
8
8
import (
9
- "errors"
10
9
"fmt"
11
10
"io"
12
11
"net"
13
12
"os"
14
13
"sync"
15
14
"syscall"
16
- "time"
17
15
"unsafe"
18
16
19
17
"golang.org/x/sys/unix"
@@ -30,18 +28,6 @@ type NativeTun struct {
30
28
closeOnce sync.Once
31
29
}
32
30
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
-
45
31
func (tun * NativeTun ) routineRouteListener (tunIfindex int ) {
46
32
var (
47
33
statusUp bool
@@ -62,26 +48,23 @@ func (tun *NativeTun) routineRouteListener(tunIfindex int) {
62
48
return
63
49
}
64
50
65
- if n < 14 {
51
+ //sizeof(if_msghdr) = 112
52
+ if n < 112 {
66
53
continue
67
54
}
68
55
69
- if data [3 /* type */ ] != unix .RTM_IFINFO {
56
+ if data [3 /* ifm_type */ ] != unix .RTM_IFINFO {
70
57
continue
71
58
}
72
- ifindex := int (* (* uint16 )(unsafe .Pointer (& data [12 /* ifindex */ ])))
59
+ ifindex := int (* (* uint16 )(unsafe .Pointer (& data [12 /* ifm_index */ ])))
73
60
if ifindex != tunIfindex {
74
61
continue
75
62
}
76
63
77
- iface , err := retryInterfaceByIndex (ifindex )
78
- if err != nil {
79
- tun .errors <- err
80
- return
81
- }
64
+ flags := int (* (* uint32 )(unsafe .Pointer (& data [8 /* ifm_flags */ ])))
82
65
83
66
// Up / Down event
84
- up := (iface . Flags & net . FlagUp ) != 0
67
+ up := (flags & syscall . IFF_UP ) != 0
85
68
if up != statusUp && up {
86
69
tun .events <- EventUp
87
70
}
@@ -90,11 +73,13 @@ func (tun *NativeTun) routineRouteListener(tunIfindex int) {
90
73
}
91
74
statusUp = up
92
75
76
+ mtu := int (* (* uint32 )(unsafe .Pointer (& data [24 /* ifm_data.ifi_mtu */ ])))
77
+
93
78
// MTU changes
94
- if iface . MTU != statusMTU {
79
+ if mtu != statusMTU {
95
80
tun .events <- EventMTUUpdate
96
81
}
97
- statusMTU = iface . MTU
82
+ statusMTU = mtu
98
83
}
99
84
}
100
85
0 commit comments