6
6
"context"
7
7
"errors"
8
8
"fmt"
9
+ "net/netip"
9
10
"syscall"
10
11
"unsafe"
11
12
@@ -50,7 +51,6 @@ func checkChange(ctx context.Context, nexthopv4, nexthopv6 systemops.Nexthop, ca
50
51
continue
51
52
}
52
53
if n < unix .SizeofRtMsghdr {
53
- log .Debugf ("Network monitor: read from routing socket returned less than expected: %d bytes" , n )
54
54
continue
55
55
}
56
56
@@ -65,23 +65,25 @@ func checkChange(ctx context.Context, nexthopv4, nexthopv6 systemops.Nexthop, ca
65
65
continue
66
66
}
67
67
68
+ // Gateway route was modified
68
69
if route .Dst .Bits () != 0 {
69
70
continue
70
71
}
71
72
72
- intf := "<nil>"
73
- if route .Interface != nil {
74
- intf = route .Interface .Name
73
+ // Compare current with saved netxhop
74
+ actualNextHopV4 , errv4 := systemops .GetNextHop (netip .IPv4Unspecified ())
75
+ actualNextHopV6 , errv6 := systemops .GetNextHop (netip .IPv6Unspecified ())
76
+ if errv4 != nil || errv6 != nil {
77
+ err := errors .Join (errv4 , errv6 )
78
+ log .Infof ("Network monitor: failed to check next hop, assuming no network connection available: %s" , err )
79
+ go callback ()
75
80
}
76
- switch msg .Type {
77
- case unix .RTM_ADD :
78
- log .Infof ("Network monitor: default route changed: via %s, interface %s" , route .Gw , intf )
81
+ hasV4HopChanged := nexthopv4 .IP .Compare (actualNextHopV4 .IP ) != 0 || nexthopv4 .Intf .Name != actualNextHopV4 .Intf .Name
82
+ hasV6HopChanged := nexthopv6 .IP .Compare (actualNextHopV6 .IP ) != 0 || nexthopv6 .Intf .Name != actualNextHopV6 .Intf .Name
83
+
84
+ if hasV4HopChanged || hasV6HopChanged {
85
+ log .Infof ("Network monitor: default route changed, IPv4: %t, IPv6: %t" , hasV4HopChanged , hasV6HopChanged )
79
86
go callback ()
80
- case unix .RTM_DELETE :
81
- if nexthopv4 .Intf != nil && route .Gw .Compare (nexthopv4 .IP ) == 0 || nexthopv6 .Intf != nil && route .Gw .Compare (nexthopv6 .IP ) == 0 {
82
- log .Infof ("Network monitor: default route removed: via %s, interface %s" , route .Gw , intf )
83
- go callback ()
84
- }
85
87
}
86
88
}
87
89
}
0 commit comments