Documentation
¶
Overview ¶
Package netutil contains extensions to the net package.
Index ¶
- func AddrIP(addr net.Addr) net.IP
- func CheckRelayIP(sender, addr net.IP) error
- func IsLAN(ip net.IP) bool
- func IsSpecialNetwork(ip net.IP) bool
- func IsTemporaryError(err error) bool
- func IsTimeout(err error) bool
- func SameNet(bits uint, ip, other net.IP) bool
- type DistinctNetSet
- type IPTracker
- type Netlist
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddrIP ¶
AddrIP gets the IP address contained in addr. It returns nil if no address is present. 从net.Addr对象解析出来net.IP对象
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.
sender是消息的来源地址,addr是sender返回的节点的地址 判断sender返回的addr是不是合法
得到的节点地址不能是特殊地址 得到的节点地址是回环地址,只能是来源地址也是回环地址 得到的节点地址是私网地址,来源地址也必须是私网地址 得到的节点地址不是以上地址都接受
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 SameNet ¶
SameNet reports whether two IP addresses have an equal prefix of the given bit length. 判断ip和other是不是同一个子网
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 ¶
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. 用来限制来自同一个子网的ip个数 一个DistinctNetSet对象可以管理多个前缀长度相同的子网
func (*DistinctNetSet) Add ¶
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. 添加成功返回true并且让对应子网的个数加一,如果达到了上限返回false
func (DistinctNetSet) Contains ¶
func (s DistinctNetSet) Contains(ip net.IP) bool
Contains whether the given IP is contained in the set. 判断指定的ip是不是在DistinctNetSet已经保存的各种子网中
func (DistinctNetSet) Len ¶
func (s DistinctNetSet) Len() int
Len returns the number of tracked IPs. 保存的ip个数就是members保存的数的和
func (*DistinctNetSet) Remove ¶
func (s *DistinctNetSet) Remove(ip net.IP)
Remove removes an IP from the set. 让该ip所在子网的计数器减去1
func (DistinctNetSet) String ¶
func (s DistinctNetSet) String() string
String implements fmt.Stringer
type IPTracker ¶
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. IPTracker用来预测本地节点的外部ip和端口 预测是根据其他节点认为的本地ip和端口
func NewIPTracker ¶
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 ¶
AddContact records that a packet containing our endpoint information has been sent to a certain host. 本地主动向外部发起连接通过AddContact添加
func (*IPTracker) AddStatement ¶
AddStatement records that a certain host thinks our external endpoint is the one given. 添加外部节点认为的本地ip的记录 host是外部的地址,endpoint是外部认为的本地地址
func (*IPTracker) PredictEndpoint ¶
PredictEndpoint returns the current prediction of the external endpoint. 预测本地节点在其他节点眼里以为的ip 就是计算statements里面保存的哪个endpoint最多
func (*IPTracker) PredictFullConeNAT ¶
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. 判断是不是Full Cone类型的NAT 检测方法是判断有没有从外部主动建立的连接
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) Contains ¶
Contains reports whether the given IP is contained in the list. 判断输入的ip是否在这一组中的任意子网中
func (Netlist) MarshalTOML ¶
func (l Netlist) MarshalTOML() interface{}
MarshalTOML implements toml.MarshalerRec.
func (*Netlist) UnmarshalTOML ¶
UnmarshalTOML implements toml.UnmarshalerRec.