Documentation ¶
Overview ¶
Package monitor provides facilities for monitoring network interface and route changes. It primarily exists to know when portable devices move between different networks.
Index ¶
- type ChangeDelta
- type ChangeFunc
- type Monitor
- func (m *Monitor) Close() error
- func (m *Monitor) GatewayAndSelfIP() (gw, myIP netip.Addr, ok bool)
- func (m *Monitor) InjectEvent()
- func (m *Monitor) InterfaceState() *interfaces.State
- func (m *Monitor) IsMajorChangeFrom(s1, s2 *interfaces.State) bool
- func (m *Monitor) Poll()
- func (m *Monitor) RegisterChangeCallback(callback ChangeFunc) (unregister func())
- func (m *Monitor) RegisterRuleDeleteCallback(callback RuleDeleteCallback) (unregister func())
- func (m *Monitor) SetTailscaleInterfaceName(ifName string)
- func (m *Monitor) Start()
- type RuleDeleteCallback
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChangeDelta ¶ added in v1.50.0
type ChangeDelta struct { // Monitor is the network monitor that sent this delta. Monitor *Monitor // Old is the old interface state, if known. // It's nil if the old state is unknown. // Do not mutate it. Old *interfaces.State // New is the new network state. // It is always non-nil. // Do not mutate it. New *interfaces.State // Major is our legacy boolean of whether the network changed in some major // way. // // Deprecated: do not remove. As of 2023-08-23 we're in a renewed effort to // remove it and ask specific qustions of ChangeDelta instead. Look at Old // and New (or add methods to ChangeDelta) instead of using Major. Major bool // TimeJumped is whether there was a big jump in wall time since the last // time we checked. This is a hint that a mobile sleeping device might have // come out of sleep. TimeJumped bool }
ChangeDelta describes the difference between two network states.
type ChangeFunc ¶
type ChangeFunc func(*ChangeDelta)
ChangeFunc is a callback function registered with Monitor that's called when the network changed.
type Monitor ¶
type Monitor struct {
// contains filtered or unexported fields
}
Monitor represents a monitoring instance.
func New ¶
New instantiates and starts a monitoring instance. The returned monitor is inactive until it's started by the Start method. Use RegisterChangeCallback to get notified of network changes.
func (*Monitor) GatewayAndSelfIP ¶
GatewayAndSelfIP returns the current network's default gateway, and the machine's default IP for that gateway.
It's the same as interfaces.LikelyHomeRouterIP, but it caches the result until the monitor detects a network change.
func (*Monitor) InjectEvent ¶
func (m *Monitor) InjectEvent()
InjectEvent forces the monitor to pretend there was a network change and re-check the state of the network. Any registered ChangeFunc callbacks will be called within the event coalescing period (under a fraction of a second).
func (*Monitor) InterfaceState ¶
func (m *Monitor) InterfaceState() *interfaces.State
InterfaceState returns the latest snapshot of the machine's network interfaces.
The returned value is owned by Mon; it must not be modified.
func (*Monitor) IsMajorChangeFrom ¶ added in v1.50.0
func (m *Monitor) IsMajorChangeFrom(s1, s2 *interfaces.State) bool
IsMajorChangeFrom reports whether the transition from s1 to s2 is a "major" change, where major roughly means it's worth tearing down a bunch of connections and rebinding.
TODO(bradiftz): tigten this definition.
func (*Monitor) Poll ¶ added in v1.50.0
func (m *Monitor) Poll()
Poll forces the monitor to pretend there was a network change and re-check the state of the network.
This is like InjectEvent but only fires ChangeFunc callbacks if the network state differed at all.
func (*Monitor) RegisterChangeCallback ¶
func (m *Monitor) RegisterChangeCallback(callback ChangeFunc) (unregister func())
RegisterChangeCallback adds callback to the set of parties to be notified (in their own goroutine) when the network state changes. To remove this callback, call unregister (or close the monitor).
func (*Monitor) RegisterRuleDeleteCallback ¶
func (m *Monitor) RegisterRuleDeleteCallback(callback RuleDeleteCallback) (unregister func())
RegisterRuleDeleteCallback adds callback to the set of parties to be notified (in their own goroutine) when a Linux ip rule is deleted. To remove this callback, call unregister (or close the monitor).
func (*Monitor) SetTailscaleInterfaceName ¶ added in v1.50.0
SetTailscaleInterfaceName sets the name of the Tailscale interface. For example, "tailscale0", "tun0", "utun3", etc.
This must be called only early in tailscaled startup before the monitor is used.
type RuleDeleteCallback ¶
RuleDeleteCallback is a callback when a Linux IP policy routing rule is deleted. The table is the table number (52, 253, 354) and priority is the priority order number (for Tailscale rules currently: 5210, 5230, 5250, 5270)