netmonitor

package
v0.0.0-...-a6598b8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AddrChange

type AddrChange struct {
	IfIndex   int
	IfAddress *net.IPNet
	Deleted   bool
}

AddrChange : IP address was (un)assigned from/to interface.

type DHCPInfo

type DHCPInfo struct {
	Subnet     *net.IPNet
	NtpServers []net.IP
}

DHCPInfo : DHCP information associated with an interface.

type DNSInfo

type DNSInfo struct {
	ResolvConfPath string
	Domains        []string
	DNSServers     []net.IP
}

DNSInfo : DNS information associated with an interface.

type DNSInfoChange

type DNSInfoChange struct {
	IfIndex int
	Info    DNSInfo
}

DNSInfoChange : DNS information for interface has changed.

type Event

type Event interface {
	// contains filtered or unexported methods
}

Event received from the network stack.

type IfAttrs

type IfAttrs struct {
	// Index of the interface
	IfIndex int
	// Name of the interface.
	IfName string
	// IfType should be one of the link types as defined in ip-link(8).
	IfType string
	// True if interface is a loopback interface.
	IsLoopback bool
	// True if interface supports broadcast access capability.
	WithBroadcast bool
	// True if interface is administratively enabled.
	AdminUp bool
	// True if interface is ready to transmit data at the L1 layer.
	LowerUp bool
	// True if interface is a slave of another interface (e.g. a sub-interface).
	Enslaved bool
	// If interface is enslaved, this should contain index of the master interface.
	MasterIfIndex int
	// Maximum Transmission Unit configured on the interface.
	MTU uint16
}

IfAttrs : interface attributes.

func (IfAttrs) Equal

func (a IfAttrs) Equal(a2 IfAttrs) bool

Equal allows to compare two sets of interface attributes for equality.

type IfChange

type IfChange struct {
	Attrs IfAttrs
	// True if this is a newly added interface.
	Added bool
	// True if interface was removed.
	Deleted bool
}

IfChange : interface (dis)appeared or attributes changed.

func (IfChange) Equal

func (e IfChange) Equal(e2 IfChange) bool

Equal allows to compare two IfChange events for equality.

type LinuxNetworkMonitor

type LinuxNetworkMonitor struct {
	Log *base.LogObject
	// contains filtered or unexported fields
}

LinuxNetworkMonitor implements NetworkMonitor for the Linux network stack.

func (*LinuxNetworkMonitor) ClearCache

func (m *LinuxNetworkMonitor) ClearCache()

ClearCache clears cached state data.

func (*LinuxNetworkMonitor) GetInterfaceAddrs

func (m *LinuxNetworkMonitor) GetInterfaceAddrs(ifIndex int) ([]*net.IPNet, net.HardwareAddr, error)

GetInterfaceAddrs returns IP addresses and the HW address assigned to the interface.

func (*LinuxNetworkMonitor) GetInterfaceAttrs

func (m *LinuxNetworkMonitor) GetInterfaceAttrs(ifIndex int) (attrs IfAttrs, err error)

GetInterfaceAttrs returns interface attributes.

func (*LinuxNetworkMonitor) GetInterfaceDHCPInfo

func (m *LinuxNetworkMonitor) GetInterfaceDHCPInfo(ifIndex int) (info DHCPInfo, err error)

GetInterfaceDHCPInfo returns DHCP info for the interface obtained from dhcpcd.

func (*LinuxNetworkMonitor) GetInterfaceDNSInfo

func (m *LinuxNetworkMonitor) GetInterfaceDNSInfo(ifIndex int) (info DNSInfo, err error)

GetInterfaceDNSInfo returns DNS info for the interface obtained from resolv.conf file.

func (*LinuxNetworkMonitor) GetInterfaceDefaultGWs

func (m *LinuxNetworkMonitor) GetInterfaceDefaultGWs(ifIndex int) (gws []net.IP, err error)

GetInterfaceDefaultGWs return a list of IP addresses of default gateways used by the given interface. This is based on routes from the main routing table.

func (*LinuxNetworkMonitor) GetInterfaceIndex

func (m *LinuxNetworkMonitor) GetInterfaceIndex(ifName string) (ifIndex int, exists bool, err error)

GetInterfaceIndex returns index of the interface.

func (*LinuxNetworkMonitor) ListInterfaces

func (m *LinuxNetworkMonitor) ListInterfaces() (ifNames []string, err error)

ListInterfaces returns all interfaces present in the Linux network stack (in the namespaces of the caller process). ListInterfaces is not backed by the cache.

func (*LinuxNetworkMonitor) ListRoutes

func (m *LinuxNetworkMonitor) ListRoutes(filters RouteFilters) (routes []Route, err error)

ListRoutes returns routes currently present in the routing tables. The set of routes to list can be filtered. ListRoutes is not backed by the cache.

func (*LinuxNetworkMonitor) WatchEvents

func (m *LinuxNetworkMonitor) WatchEvents(ctx context.Context, subName string) <-chan Event

WatchEvents allows to subscribe to watch for events from the Linux network stack.

type MockInterface

type MockInterface struct {
	Attrs   IfAttrs
	IPAddrs []*net.IPNet
	HwAddr  net.HardwareAddr
	DNS     DNSInfo
	DHCP    DHCPInfo
}

MockInterface : a simulated network interface and its state.

type MockNetworkMonitor

type MockNetworkMonitor struct {
	sync.Mutex
	Log    *base.LogObject
	MainRT int // inject syscall.RT_TABLE_MAIN for Linux network stack
	// contains filtered or unexported fields
}

MockNetworkMonitor is used for unit testing.

func (*MockNetworkMonitor) AddOrUpdateInterface

func (m *MockNetworkMonitor) AddOrUpdateInterface(mockIf MockInterface)

AddOrUpdateInterface : allows to simulate an event of interface being added or updated.

func (*MockNetworkMonitor) ClearCache

func (m *MockNetworkMonitor) ClearCache()

ClearCache does nothing.

func (*MockNetworkMonitor) DelInterface

func (m *MockNetworkMonitor) DelInterface(ifName string)

DelInterface : allows to simulate an event of removed interface.

func (*MockNetworkMonitor) GetInterfaceAddrs

func (m *MockNetworkMonitor) GetInterfaceAddrs(ifIndex int) (
	[]*net.IPNet, net.HardwareAddr, error)

GetInterfaceAddrs returns addresses assigned to the mock interface.

func (*MockNetworkMonitor) GetInterfaceAttrs

func (m *MockNetworkMonitor) GetInterfaceAttrs(ifIndex int) (IfAttrs, error)

GetInterfaceAttrs returns attributes of the mock interface.

func (*MockNetworkMonitor) GetInterfaceDHCPInfo

func (m *MockNetworkMonitor) GetInterfaceDHCPInfo(ifIndex int) (DHCPInfo, error)

GetInterfaceDHCPInfo returns DHCP info associated with the mock interface.

func (*MockNetworkMonitor) GetInterfaceDNSInfo

func (m *MockNetworkMonitor) GetInterfaceDNSInfo(ifIndex int) (DNSInfo, error)

GetInterfaceDNSInfo returns DNS info associated with the mock interface.

func (*MockNetworkMonitor) GetInterfaceDefaultGWs

func (m *MockNetworkMonitor) GetInterfaceDefaultGWs(ifIndex int) (gws []net.IP, err error)

GetInterfaceDefaultGWs returns default gateways associated with the mock interface.

func (*MockNetworkMonitor) GetInterfaceIndex

func (m *MockNetworkMonitor) GetInterfaceIndex(ifName string) (
	ifIndex int, exists bool, err error)

GetInterfaceIndex returns index of the mock interface.

func (*MockNetworkMonitor) ListInterfaces

func (m *MockNetworkMonitor) ListInterfaces() (ifNames []string, err error)

ListInterfaces returns all mocked interfaces.

func (*MockNetworkMonitor) ListRoutes

func (m *MockNetworkMonitor) ListRoutes(filters RouteFilters) (rts []Route, err error)

ListRoutes lists all mock routes.

func (*MockNetworkMonitor) UpdateRoutes

func (m *MockNetworkMonitor) UpdateRoutes(routes []Route)

UpdateRoutes : allows to update the set of mock routes.

func (*MockNetworkMonitor) WatchEvents

func (m *MockNetworkMonitor) WatchEvents(ctx context.Context, subName string) <-chan Event

WatchEvents allows to watch for event created artificially using AddOrUpdateInterface, DelInterface and UpdateRoutes.

type NetworkMonitor

type NetworkMonitor interface {
	// ListInterfaces returns all available network interfaces.
	ListInterfaces() (ifNames []string, err error)
	// GetInterfaceIndex : get interface index. Both name and index are unique
	// interface identifiers, but while name is usually used in user-facing
	// contexts, index is preferred in APIs (may vary between different network stacks).
	// At least within NetworkMonitor, index is the preferred identifier and can be used
	// with the methods below to obtain interface attributes, assigned addresses, etc.
	// Do not keep the index for too long however, the mapping between the name and
	// the index may change as interfaces are added/deleted.
	GetInterfaceIndex(ifName string) (ifIndex int, exists bool, err error)
	// GetInterfaceAttrs : get interface attributes (not including assigned addresses).
	GetInterfaceAttrs(ifIndex int) (IfAttrs, error)
	// GetInterfaceAddrs : get MAC address and a list of IP addresses assigned
	// to the given interface.
	GetInterfaceAddrs(ifIndex int) ([]*net.IPNet, net.HardwareAddr, error)
	// GetInterfaceDNSInfo : get DNS information associated with the given interface.
	GetInterfaceDNSInfo(ifIndex int) (DNSInfo, error)
	// GetInterfaceDHCPInfo : get DHCP information associated with the given interface.
	// This information should be retrieved from the DHCP client.
	GetInterfaceDHCPInfo(ifIndex int) (DHCPInfo, error)
	// GetInterfaceDefaultGWs : return a list of IP addresses of default gateways
	// used by the given interface. Includes both statically configured GWs as well as
	// those assigned by DHCP.
	GetInterfaceDefaultGWs(ifIndex int) ([]net.IP, error)
	// ListRoutes returns routes currently present in the routing tables.
	// The set of routes to list can be filtered.
	ListRoutes(filters RouteFilters) ([]Route, error)
	// ClearCache : clear cached mappings between interface names, interface indexes,
	// attributes, assigned addresses, DNS info, DHCP info and default GWs.
	// It is reasonable to do this once in a while because the monitor can miss some
	// notification from the network stack and therefore the cache can get out-of-date
	// over time.
	ClearCache()
	// WatchEvents : subscribe to watch for changes happening inside the network stack
	// related to interfaces, routes, DNS, etc.
	// The returned channel should be reasonably buffered.
	WatchEvents(ctx context.Context, subName string) <-chan Event
}

NetworkMonitor should allow to: - list (and possibly cache) network interfaces - obtain (and possibly cache) interface index - obtain (and possibly cache) interface attributes, addresses, DNS info, etc. - allow to watch for interface/address/route changes There should be one implementation for every supported network stack. NetworkMonitor should be thread-safe.

type Route

type Route struct {
	IfIndex int
	Dst     *net.IPNet
	Gw      net.IP
	Table   int
	// Network-stack specific data.
	Data interface{}
}

Route : IP route.

func (Route) IsDefaultRoute

func (r Route) IsDefaultRoute() bool

IsDefaultRoute returns true if this is a default route, i.e. matches all destinations.

type RouteChange

type RouteChange struct {
	Route
	Added   bool
	Deleted bool
}

RouteChange : a route was added or removed.

type RouteFilters

type RouteFilters struct {
	// Enable to retrieve routes only from the given table.
	FilterByTable bool
	Table         int

	// Enable to retrieve only those routes which are associated
	// with the given interface.
	FilterByIf bool
	IfIndex    int
}

RouteFilters : used by ListRoutes() to limit the set of routes to list.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL