@@ -42,7 +42,7 @@ func (m *networkUpdateMonitor) loopUpdate() {
4242 select {
4343 case <- m .done :
4444 return
45- case <- time . After ( time . Second ) :
45+ default :
4646 }
4747 err := m .loopUpdate0 ()
4848 if err != nil {
@@ -67,7 +67,16 @@ func (m *networkUpdateMonitor) loopUpdate1(routeSocketFile *os.File) {
6767 defer routeSocketFile .Close ()
6868 buffer := buf .NewPacket ()
6969 defer buffer .Release ()
70+ done := make (chan struct {})
71+ go func () {
72+ select {
73+ case <- m .done :
74+ routeSocketFile .Close ()
75+ case <- done :
76+ }
77+ }()
7078 n , err := routeSocketFile .Read (buffer .FreeBytes ())
79+ close (done )
7180 if err != nil {
7281 return
7382 }
@@ -92,57 +101,59 @@ func (m *networkUpdateMonitor) Close() error {
92101}
93102
94103func (m * defaultInterfaceMonitor ) checkUpdate () error {
95- ribMessage , err := route .FetchRIB (unix .AF_UNSPEC , route .RIBTypeRoute , 0 )
96- if err != nil {
97- return err
98- }
99- routeMessages , err := route .ParseRIB (route .RIBTypeRoute , ribMessage )
100- if err != nil {
101- return err
102- }
103- var defaultInterface * net.Interface
104- for _ , rawRouteMessage := range routeMessages {
105- routeMessage := rawRouteMessage .(* route.RouteMessage )
106- if len (routeMessage .Addrs ) <= unix .RTAX_NETMASK {
107- continue
108- }
109- destination , isIPv4Destination := routeMessage .Addrs [unix .RTAX_DST ].(* route.Inet4Addr )
110- if ! isIPv4Destination {
111- continue
112- }
113- if destination .IP != netip .IPv4Unspecified ().As4 () {
114- continue
115- }
116- mask , isIPv4Mask := routeMessage .Addrs [unix .RTAX_NETMASK ].(* route.Inet4Addr )
117- if ! isIPv4Mask {
118- continue
119- }
120- ones , _ := net .IPMask (mask .IP [:]).Size ()
121- if ones != 0 {
122- continue
123- }
124- routeInterface , err := net .InterfaceByIndex (routeMessage .Index )
104+ var (
105+ defaultInterface * net.Interface
106+ err error
107+ )
108+ if m .options .UnderNetworkExtension {
109+ defaultInterface , err = getDefaultInterfaceBySocket ()
125110 if err != nil {
126111 return err
127112 }
128- if routeMessage .Flags & unix .RTF_UP == 0 {
129- continue
130- }
131- if routeMessage .Flags & unix .RTF_GATEWAY == 0 {
132- continue
113+ } else {
114+ ribMessage , err := route .FetchRIB (unix .AF_UNSPEC , route .RIBTypeRoute , 0 )
115+ if err != nil {
116+ return err
133117 }
134- if routeMessage .Flags & unix .RTF_IFSCOPE != 0 {
135- // continue
118+ routeMessages , err := route .ParseRIB (route .RIBTypeRoute , ribMessage )
119+ if err != nil {
120+ return err
136121 }
137- defaultInterface = routeInterface
138- break
139- }
140- if defaultInterface == nil {
141- if m .options .UnderNetworkExtension {
142- defaultInterface , err = getDefaultInterfaceBySocket ()
122+ for _ , rawRouteMessage := range routeMessages {
123+ routeMessage := rawRouteMessage .(* route.RouteMessage )
124+ if len (routeMessage .Addrs ) <= unix .RTAX_NETMASK {
125+ continue
126+ }
127+ destination , isIPv4Destination := routeMessage .Addrs [unix .RTAX_DST ].(* route.Inet4Addr )
128+ if ! isIPv4Destination {
129+ continue
130+ }
131+ if destination .IP != netip .IPv4Unspecified ().As4 () {
132+ continue
133+ }
134+ mask , isIPv4Mask := routeMessage .Addrs [unix .RTAX_NETMASK ].(* route.Inet4Addr )
135+ if ! isIPv4Mask {
136+ continue
137+ }
138+ ones , _ := net .IPMask (mask .IP [:]).Size ()
139+ if ones != 0 {
140+ continue
141+ }
142+ routeInterface , err := net .InterfaceByIndex (routeMessage .Index )
143143 if err != nil {
144144 return err
145145 }
146+ if routeMessage .Flags & unix .RTF_UP == 0 {
147+ continue
148+ }
149+ if routeMessage .Flags & unix .RTF_GATEWAY == 0 {
150+ continue
151+ }
152+ if routeMessage .Flags & unix .RTF_IFSCOPE != 0 {
153+ // continue
154+ }
155+ defaultInterface = routeInterface
156+ break
146157 }
147158 }
148159 if defaultInterface == nil {
0 commit comments