Documentation ¶
Overview ¶
Package netutil contains extensions to the net package.
Index ¶
- func AddrAddr(addr net.Addr) netip.Addr
- func AddrIsLAN(ip netip.Addr) bool
- func AddrIsSpecialNetwork(ip netip.Addr) bool
- func CheckRelayAddr(sender, addr netip.Addr) error
- func CheckRelayIP(sender, addr net.IP) error
- func IPToAddr(ip net.IP) netip.Addr
- func IsLAN(ip net.IP) bool
- func IsSpecialNetwork(ip net.IP) bool
- func IsTemporaryError(err error) bool
- func IsTimeout(err error) bool
- func RandomAddr(rng *rand.Rand, ipv4 bool) netip.Addr
- func SameNet(bits uint, ip, other net.IP) bool
- type DistinctNetSet
- func (s *DistinctNetSet) Add(ip net.IP) bool
- func (s *DistinctNetSet) AddAddr(ip netip.Addr) bool
- func (s DistinctNetSet) Contains(ip net.IP) bool
- func (s DistinctNetSet) ContainsAddr(ip netip.Addr) bool
- func (s DistinctNetSet) Len() int
- func (s *DistinctNetSet) Remove(ip net.IP)
- func (s *DistinctNetSet) RemoveAddr(ip netip.Addr)
- func (s DistinctNetSet) String() string
- type IPTracker
- type Netlist
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddrAddr ¶ added in v1.14.5
AddrAddr gets the IP address contained in addr. The result will be invalid if the address type is unsupported.
func AddrIsSpecialNetwork ¶ added in v1.14.5
AddrIsSpecialNetwork reports whether an IP is located in a special-use network range This includes broadcast, multicast and documentation addresses.
func CheckRelayAddr ¶ added in v1.14.5
CheckRelayAddr reports whether an IP relayed from the given sender IP is a valid connection target.
There are four rules:
- Special network addresses are never valid.
- Loopback addresses are OK if relayed by a loopback host.
- LAN addresses are OK if relayed by a LAN host.
- All other addresses are always acceptable.
func CheckRelayIP ¶
CheckRelayIP reports whether an IP relayed from the given sender IP is a valid connection target.
There are four rules:
- Special network addresses are never valid.
- Loopback addresses are OK if relayed by a loopback host.
- LAN addresses are OK if relayed by a LAN host.
- All other addresses are always acceptable.
func IPToAddr ¶ added in v1.14.5
IPToAddr converts net.IP to netip.Addr. Note that unlike netip.AddrFromSlice, this function will always ensure that the resulting Addr is IPv4 when the input is.
func IsSpecialNetwork ¶
IsSpecialNetwork reports whether an IP is located in a special-use network range This includes broadcast, multicast and documentation addresses.
func IsTemporaryError ¶
IsTemporaryError checks whether the given error should be considered temporary.
func RandomAddr ¶ added in v1.14.5
RandomAddr creates a random IP address.
func SameNet ¶ added in v1.8.0
SameNet reports whether two IP addresses have an equal prefix of the given bit length.
Example ¶
// This returns true because the IPs are in the same /24 network: fmt.Println(SameNet(24, net.IP{127, 0, 0, 1}, net.IP{127, 0, 0, 3})) // This call returns false: fmt.Println(SameNet(24, net.IP{127, 3, 0, 1}, net.IP{127, 5, 0, 3}))
Output: true false
Types ¶
type DistinctNetSet ¶ added in v1.8.0
type DistinctNetSet struct { Subnet uint // number of common prefix bits Limit uint // maximum number of IPs in each subnet // contains filtered or unexported fields }
DistinctNetSet tracks IPs, ensuring that at most N of them fall into the same network range.
func (*DistinctNetSet) Add ¶ added in v1.8.0
func (s *DistinctNetSet) Add(ip net.IP) bool
Add adds an IP address to the set. It returns false (and doesn't add the IP) if the number of existing IPs in the defined range exceeds the limit.
func (*DistinctNetSet) AddAddr ¶ added in v1.14.5
func (s *DistinctNetSet) AddAddr(ip netip.Addr) bool
AddAddr adds an IP address to the set. It returns false (and doesn't add the IP) if the number of existing IPs in the defined range exceeds the limit.
func (DistinctNetSet) Contains ¶ added in v1.8.0
func (s DistinctNetSet) Contains(ip net.IP) bool
Contains reports whether the given IP is contained in the set.
func (DistinctNetSet) ContainsAddr ¶ added in v1.14.5
func (s DistinctNetSet) ContainsAddr(ip netip.Addr) bool
ContainsAddr reports whether the given IP is contained in the set.
func (DistinctNetSet) Len ¶ added in v1.8.0
func (s DistinctNetSet) Len() int
Len returns the number of tracked IPs.
func (*DistinctNetSet) Remove ¶ added in v1.8.0
func (s *DistinctNetSet) Remove(ip net.IP)
Remove removes an IP from the set.
func (*DistinctNetSet) RemoveAddr ¶ added in v1.14.5
func (s *DistinctNetSet) RemoveAddr(ip netip.Addr)
RemoveAddr removes an IP from the set.
func (DistinctNetSet) String ¶ added in v1.8.0
func (s DistinctNetSet) String() string
String implements fmt.Stringer
type IPTracker ¶ added in v1.8.18
type IPTracker struct {
// contains filtered or unexported fields
}
IPTracker predicts the external endpoint, i.e. IP address and port, of the local host based on statements made by other hosts.
func NewIPTracker ¶ added in v1.8.18
NewIPTracker creates an IP tracker.
The window parameters configure the amount of past network events which are kept. The minStatements parameter enforces a minimum number of statements which must be recorded before any prediction is made. Higher values for these parameters decrease 'flapping' of predictions as network conditions change. Window duration values should typically be in the range of minutes.
func (*IPTracker) AddContact ¶ added in v1.8.18
AddContact records that a packet containing our endpoint information has been sent to a certain host.
func (*IPTracker) AddStatement ¶ added in v1.8.18
AddStatement records that a certain host thinks our external endpoint is the one given.
func (*IPTracker) PredictEndpoint ¶ added in v1.8.18
PredictEndpoint returns the current prediction of the external endpoint.
func (*IPTracker) PredictFullConeNAT ¶ added in v1.8.18
PredictFullConeNAT checks whether the local host is behind full cone NAT. It predicts by checking whether any statement has been received from a node we didn't contact before the statement was made.
type Netlist ¶
Netlist is a list of IP networks.
func ParseNetlist ¶
ParseNetlist parses a comma-separated list of CIDR masks. Whitespace and extra commas are ignored.
func (*Netlist) Add ¶
Add parses a CIDR mask and appends it to the list. It panics for invalid masks and is intended to be used for setting up static lists.
func (*Netlist) ContainsAddr ¶ added in v1.14.5
ContainsAddr reports whether the given IP is contained in the list.
func (Netlist) MarshalTOML ¶ added in v1.6.0
func (l Netlist) MarshalTOML() interface{}
MarshalTOML implements toml.MarshalerRec.
func (*Netlist) UnmarshalTOML ¶ added in v1.6.0
UnmarshalTOML implements toml.UnmarshalerRec.