Documentation ¶
Overview ¶
Package netstate implements utilities for retrieving and filtering network interface state.
There are routines to obtain the available set of of network addresess, for determining changes to those addresses, and for selecting from amongst them according to some set of policies. Polices are implemented by applying simple predicates (functions with names of the form Is<condition>) to filter or find the first matching address from a list of addresses. The intent is to make it easy to create policies that do things like 'find the first IPv4 unicast address that is globally routable, failing that use a private IPv4 address, and failing that, an IPv6 address'.
A simple usage would be:
state, _ := netstate.GetAccessibleIPs() ipv4 := state.Filter(netstate.IsPublicUnicastIPv4) // ipv4 will contain all of the public IPv4 addresses, if any.
The example policy described above would be implemented using a series of calls to Filter with appropriate predicates.
In some cases, it may be necessary to take IP routing information into account and hence the interface hosting the address. The interface hosting each address is provided via NetworkInterface. GetAllAddresses and GetAccessibleIPs return instances of Address that provide access to the network address and the network interface that hosts it.
GetAll and GetAccessibleIPs cache the state of the network interfaces and routing information. This cache is invalidated by the Invalidate function which must be called whenever the network changes state (e.g. in response to dhcp changes).
Although most commercial networking hardware supports IPv6, some consumer devices and more importantly many ISPs do not, so routines are provided to allow developers to easily distinguish between the two and to use whichever is appropriate for their product/situation.
The term 'accessible' is used to refer to any non-loopback IP address. The term 'public' is used to refer to any globally routable IP address.
All IPv6 addresses are intended to be 'public', but any starting with fc00::/7 (RFC4193) are reserved for private use, but the go net libraries do not appear to recognise this. Similarly fe80::/10 (RFC 4291) are reserved for 'site-local' usage, but again this is not implemented in the go libraries. Any developer who needs to distinguish these cases will need to write their own routines to test for them.
When using the go net package it is important to remember that IPv6 addresses subsume IPv4 and hence in many cases the same internal representation is used for both, thus testing for the length of the IP address is unreliable. The reliable test is to use the net.To4() which will return a non-nil result if can be used as an IPv4 one. Any address can be used as an IPv6 and hence the only reliable way to test for an IPv6 address that is not an IPv4 one also is for the To4 call to return nil for it.
Index ¶
- Variables
- func AsIP(a net.Addr) net.IP
- func AsIPAddr(a net.Addr) *net.IPAddr
- func InvalidateCache()
- func IsAccessibleIP(a Address) bool
- func IsDefaultRoute(r *route.IPRoute) bool
- func IsGloballyRoutableIP(ip net.IP) bool
- func IsIPProtocol(n string) bool
- func IsLoopbackIP(a Address) bool
- func IsOnDefaultRoute(a Address) bool
- func IsPublicUnicastIP(a Address) bool
- func IsPublicUnicastIPv4(a Address) bool
- func IsPublicUnicastIPv6(a Address) bool
- func IsUnicastIP(a Address) bool
- func IsUnicastIPv4(a Address) bool
- func IsUnicastIPv6(a Address) bool
- func IsUnspecifiedIP(a Address) bool
- func NewNetAddr(network, protocol string) net.Addr
- func PossibleAddresses(protocol, addr string, chooser AddressChooser) ([]net.Addr, bool, error)
- func SameMachine(addr net.Addr) (bool, error)
- type AddrList
- type Address
- type AddressChooser
- type AddressChooserFunc
- type AddressPredicate
- type IPNetworkInterface
- type IPRouteList
- type InterfaceList
- type Mapper
- type NetworkInterface
- type RoutePredicate
- type RouteTable
Constants ¶
This section is empty.
Variables ¶
var ( ErrUnsupportedProtocol = errors.New("unsupported protocol") ErrFailedToParseIPAddr = errors.New("failed to parse IP address") ErrUnspecifiedIPAddr = errors.New("unspecified (i.e. zero) IP address") ErrFailedToFindInterface = errors.New("failed to find a network interface") )
var ( // ErrNotAnIPProtocol is returned when the requested protocol is not from // theIP family. ErrNotAnIPProtocol = errors.New("requested protocol is not from the IP family") )
Functions ¶
func AsIPAddr ¶
AsIPAddr returns its argument as a net.IPAddr if that's possible. If the address is an IP in host:port notation it will use the host portion only.
func IsAccessibleIP ¶
IsAccessible returns true if its argument is an accessible (non-loopback) IP address.
func IsDefaultRoute ¶
IsDefaultRoute returns true if the supplied IPRoute is a default route.
func IsGloballyRoutableIP ¶
IsGloballyRoutableIP returns true if the argument is a globally routable IP address.
func IsIPProtocol ¶
IsIPProtocol returns true if its parameter is one of the allowed network/protocol values for IP. It considers the vanadium specific websockect protocols (wsh, wsh4, wsh6) as being IP.
func IsLoopbackIP ¶
IsLoopback returns true if its argument is a loopback IP address
func IsOnDefaultRoute ¶
IsOnDefaultRoute returns true for addresses that are on an interface that has a default route set for the supplied address.
func IsPublicUnicastIP ¶
IsPublicUnicastIP returns true if its argument is a global routable IPv4 or 6 address.
func IsPublicUnicastIPv4 ¶
IsPublicUnicastIPv4 returns true if its argument is a globally routable, public IPv4 unicast address.
func IsPublicUnicastIPv6 ¶
IsUnicastIPv6 returns true if its argument is a globally routable IP6 address
func IsUnicastIP ¶
IsUnicastIP returns true if its argument is a unicast IP address.
func IsUnicastIPv4 ¶
IsUnicastIPv4 returns true if its argument is a unicast IP4 address
func IsUnicastIPv6 ¶
IsUnicastIPv6 returns true if its argument is a unicast IPv6 address
func IsUnspecifiedIP ¶
IsUnspecified returns true if its argument is an unspecified IP address
func NewNetAddr ¶
NewNetAddr creates a net.Addr from the supplied network and protocol.
func PossibleAddresses ¶
PossibleAddresses returns the set of addresses that can be used to reach the specified host and that satisfy whatever policy is implemented by the supplied AddressChooser. It also returns an indication of whether the supplied host is unspecified or not. An unspecified host can be used over any network interface on the host. If the supplied address contains a port in then all of the returned addresses will also contain that port.
The returned net.Addr's need have the exact same protocol as that passed in as a parameter, rather, the chooser should return net.Addr's that can be used for that protocol. Using tcp as a parameter for example will generally result in net.Addr's whose Network method returns "ip" or "ip6".
If the chooser fails to find any appropriate addresses then the protocol, addr parameters will be returned as net.Addr (and if possible as a netstate.Address).
PossibleAddress currently only supports IP addresses.
Types ¶
type AddrList ¶
type AddrList []Address
AddrList is a slice of Addresses.
func ConvertToAddresses ¶
ConvertToAddresses attempts to convert a slice of net.Addr's into an AddrList. It does so as follows:
- using type assertion if the net.Addr instance is also an instance of Address.
- using AddressFromAddr.
- filling in just the address portion of Address without any interface information.
func FindAdded ¶
FindAdded returns the set addresses that are present in b, but not in a - i.e. have been added.
func FindRemoved ¶
FindRemoved returns the set of addresses that are present in a, but not in b - i.e. have been removed.
func GetAccessibleIPs ¶
GetAccessibleIPs returns all of the accessible IP addresses on the device - i.e. excluding loopback and unspecified addresses. The IP addresses returned will be host addresses.
func GetAllAddresses ¶
GetAllAddresses gets all of the available addresses on the device, including loopback addresses, non-IP protocols etc. The IP interface addresses returned are in CIDR form. GetAllAddressses caches the state of network interfaces and route tables to avoid expensive system calls (for routing information), the cache is invalidated by the Invalidate function which should be called whenever the network state may have changed (e.g. following a dhcp change). The returned chan is closed when the returned AddrList has become stale.
func (AddrList) AsNetAddrs ¶
AsNetAddrs returns al as a slice of net.Addrs by changing the type of the slice that contains them and not by copying them.
func (AddrList) Filter ¶
func (al AddrList) Filter(predicate AddressPredicate) AddrList
Filter returns all of the addresses for which the predicate function is true.
type Address ¶
type Address interface { // Address returns the network address this instance represents. net.Addr Interface() NetworkInterface DebugString() string }
Address represents a network address and the interface that hosts it.
func AddressFromAddr ¶
AddressFromAddr creates an instance of Address given the suppied net.Addr. It will search through the available network interfaces to find the interface that hosts this address. It currently supports only IP protocols.
func AddressFromIP ¶
AddressFromIP creates an instance of Address given the supplied IP address. It will search through the available network interfaces to find the interface that hosts this IP address.
func WithIPHost ¶
WithIPHost returns an instance of Address with the network address component being an instance with a net.Addr that contains an IP host address (as opposed to a network CIDR for example).
func WithIPHostAndPort ¶
WithIPHostAndPort returns an instance of Address with the network address component being an instance of net.Addr that contains an IP host and port in : notation.
type AddressChooser ¶
type AddressChooser interface {
ChooseAddresses(protocol string, candidates []net.Addr) ([]net.Addr, error)
}
AddressChooser determines the preferred addresses to publish with the mount table when one is not otherwise specified.
type AddressChooserFunc ¶
AddressChooserFunc is a convenience for implementations that wish to supply a function literal implementation of AddressChooser.
func (AddressChooserFunc) ChooseAddresses ¶
func (f AddressChooserFunc) ChooseAddresses(protocol string, candidates []net.Addr) ([]net.Addr, error)
ChooseAddresses applies AddressChooserFunc.
type AddressPredicate ¶
AddressPredicate defines the function signature for predicate functions to be used with AddrList
type IPNetworkInterface ¶
type IPNetworkInterface interface { NetworkInterface IPRoutes() IPRouteList }
IPNetworkInterface represents a network interface supporting IP protocols.
type IPRouteList ¶
IPRouteList is a slice of IPRoutes as returned by the netconfig package.
func (IPRouteList) Filter ¶
func (rl IPRouteList) Filter(predicate RoutePredicate) IPRouteList
Filter returns all of the routes for which the predicate function is true.
func (IPRouteList) String ¶
func (rl IPRouteList) String() string
type InterfaceList ¶
type InterfaceList []NetworkInterface
InterfaceList represents a list of network interfaces.
func GetAllInterfaces ¶
func GetAllInterfaces() (InterfaceList, error)
GetAllInterfaces returns a list of all of the network interfaces on this device. It uses the same cache as GetAllAddresses.
func (InterfaceList) String ¶
func (ifcl InterfaceList) String() string
type NetworkInterface ¶
type NetworkInterface interface { // Addrs returns the addresses hosted by this interface. Addrs() []net.Addr // Index returns the index of this interface. Index() int // Name returns the name of this interface, e.g., "en0", "lo0", "eth0.100" Name() string // MTU returns the maximum transmission unit MTU() int // HardwareAddr returns the hardware address in IEEE MAC-48, EUI-48 and EUI-64 form HardwareAddr() net.HardwareAddr // Flags returns the flags for the interface e.g., FlagUp, FlagLoopback, FlagMulticast Flags() net.Flags // Networks returns the set of networks accessible over this interface. Networks() []net.Addr // String returns a string representation of the interface. String() string }
Network interface represents a network interface.
type RoutePredicate ¶
RoutePredicate defines the function signature for predicate functions to be used with RouteList
type RouteTable ¶
type RouteTable map[int]IPRouteList
RouteTable represents the set of currently available network interfaces and the routes on each such interface. It is index by the index number of each interface.