66 "context"
77 "errors"
88 "fmt"
9+ "net/netip"
910 "syscall"
1011 "unsafe"
1112
@@ -50,7 +51,6 @@ func checkChange(ctx context.Context, nexthopv4, nexthopv6 systemops.Nexthop, ca
5051 continue
5152 }
5253 if n < unix .SizeofRtMsghdr {
53- log .Debugf ("Network monitor: read from routing socket returned less than expected: %d bytes" , n )
5454 continue
5555 }
5656
@@ -65,23 +65,25 @@ func checkChange(ctx context.Context, nexthopv4, nexthopv6 systemops.Nexthop, ca
6565 continue
6666 }
6767
68+ // Gateway route was modified
6869 if route .Dst .Bits () != 0 {
6970 continue
7071 }
7172
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 ()
7580 }
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 .Intf != nil && actualNextHopV4 .Intf != nil && (nexthopv4 .IP .Compare (actualNextHopV4 .IP ) != 0 || nexthopv4 .Intf .Name != actualNextHopV4 .Intf .Name )
82+ hasV6HopChanged := nexthopv6 .Intf != nil && actualNextHopV6 .Intf != nil && (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 )
7986 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- }
8587 }
8688 }
8789 }
0 commit comments