1 // Package netutil contains extensions to the net package.
13 var lan4, lan6, special4, special6 Netlist
16 // Lists from RFC 5735, RFC 5156,
17 // https://www.iana.org/assignments/iana-ipv4-special-registry/
18 lan4.Add("0.0.0.0/8") // "This" network
19 lan4.Add("10.0.0.0/8") // Private Use
20 lan4.Add("172.16.0.0/12") // Private Use
21 lan4.Add("192.168.0.0/16") // Private Use
22 lan6.Add("fe80::/10") // Link-Local
23 lan6.Add("fc00::/7") // Unique-Local
24 special4.Add("192.0.0.0/29") // IPv4 Service Continuity
25 special4.Add("192.0.0.9/32") // PCP Anycast
26 special4.Add("192.0.0.170/32") // NAT64/DNS64 Discovery
27 special4.Add("192.0.0.171/32") // NAT64/DNS64 Discovery
28 special4.Add("192.0.2.0/24") // TEST-NET-1
29 special4.Add("192.31.196.0/24") // AS112
30 special4.Add("192.52.193.0/24") // AMT
31 special4.Add("192.88.99.0/24") // 6to4 Relay Anycast
32 special4.Add("192.175.48.0/24") // AS112
33 special4.Add("198.18.0.0/15") // Device Benchmark Testing
34 special4.Add("198.51.100.0/24") // TEST-NET-2
35 special4.Add("203.0.113.0/24") // TEST-NET-3
36 special4.Add("255.255.255.255/32") // Limited Broadcast
38 // http://www.iana.org/assignments/iana-ipv6-special-registry/
39 special6.Add("100::/64")
40 special6.Add("2001::/32")
41 special6.Add("2001:1::1/128")
42 special6.Add("2001:2::/48")
43 special6.Add("2001:3::/32")
44 special6.Add("2001:4:112::/48")
45 special6.Add("2001:5::/32")
46 special6.Add("2001:10::/28")
47 special6.Add("2001:20::/28")
48 special6.Add("2001:db8::/32")
49 special6.Add("2002::/16")
52 // Netlist is a list of IP networks.
53 type Netlist []net.IPNet
55 // ParseNetlist parses a comma-separated list of CIDR masks.
56 // Whitespace and extra commas are ignored.
57 func ParseNetlist(s string) (*Netlist, error) {
58 ws := strings.NewReplacer(" ", "", "\n", "", "\t", "")
59 masks := strings.Split(ws.Replace(s), ",")
61 for _, mask := range masks {
65 _, n, err := net.ParseCIDR(mask)
74 // MarshalTOML implements toml.MarshalerRec.
75 func (l Netlist) MarshalTOML() interface{} {
76 list := make([]string, 0, len(l))
77 for _, net := range l {
78 list = append(list, net.String())
83 // UnmarshalTOML implements toml.UnmarshalerRec.
84 func (l *Netlist) UnmarshalTOML(fn func(interface{}) error) error {
86 if err := fn(&masks); err != nil {
89 for _, mask := range masks {
90 _, n, err := net.ParseCIDR(mask)
99 // Add parses a CIDR mask and appends it to the list. It panics for invalid masks and is
100 // intended to be used for setting up static lists.
101 func (l *Netlist) Add(cidr string) {
102 _, n, err := net.ParseCIDR(cidr)
109 // Contains reports whether the given IP is contained in the list.
110 func (l *Netlist) Contains(ip net.IP) bool {
114 for _, net := range *l {
115 if net.Contains(ip) {
122 // IsLAN reports whether an IP is a local network address.
123 func IsLAN(ip net.IP) bool {
127 if v4 := ip.To4(); v4 != nil {
128 return lan4.Contains(v4)
130 return lan6.Contains(ip)
133 // IsSpecialNetwork reports whether an IP is located in a special-use network range
134 // This includes broadcast, multicast and documentation addresses.
135 func IsSpecialNetwork(ip net.IP) bool {
136 if ip.IsMulticast() {
139 if v4 := ip.To4(); v4 != nil {
140 return special4.Contains(v4)
142 return special6.Contains(ip)
146 errInvalid = errors.New("invalid IP")
147 errUnspecified = errors.New("zero address")
148 errSpecial = errors.New("special network")
149 errLoopback = errors.New("loopback address from non-loopback host")
150 errLAN = errors.New("LAN address from WAN host")
153 // CheckRelayIP reports whether an IP relayed from the given sender IP
154 // is a valid connection target.
156 // There are four rules:
157 // - Special network addresses are never valid.
158 // - Loopback addresses are OK if relayed by a loopback host.
159 // - LAN addresses are OK if relayed by a LAN host.
160 // - All other addresses are always acceptable.
161 func CheckRelayIP(sender, addr net.IP) error {
162 if len(addr) != net.IPv4len && len(addr) != net.IPv6len {
165 if addr.IsUnspecified() {
166 return errUnspecified
168 if IsSpecialNetwork(addr) {
171 if addr.IsLoopback() && !sender.IsLoopback() {
174 if IsLAN(addr) && !IsLAN(sender) {
180 // SameNet reports whether two IP addresses have an equal prefix of the given bit length.
181 func SameNet(bits uint, ip, other net.IP) bool {
182 ip4, other4 := ip.To4(), other.To4()
184 case (ip4 == nil) != (other4 == nil):
187 return sameNet(bits, ip4, other4)
189 return sameNet(bits, ip.To16(), other.To16())
193 func sameNet(bits uint, ip, other net.IP) bool {
195 mask := ^byte(0xFF >> (bits % 8))
196 if mask != 0 && nb < len(ip) && ip[nb]&mask != other[nb]&mask {
199 return nb <= len(ip) && bytes.Equal(ip[:nb], other[:nb])
202 // DistinctNetSet tracks IPs, ensuring that at most N of them
203 // fall into the same network range.
204 type DistinctNetSet struct {
205 Subnet uint // number of common prefix bits
206 Limit uint // maximum number of IPs in each subnet
208 members map[string]uint
212 // Add adds an IP address to the set. It returns false (and doesn't add the IP) if the
213 // number of existing IPs in the defined range exceeds the limit.
214 func (s *DistinctNetSet) Add(ip net.IP) bool {
216 n := s.members[string(key)]
218 s.members[string(key)] = n + 1
224 // Remove removes an IP from the set.
225 func (s *DistinctNetSet) Remove(ip net.IP) {
227 if n, ok := s.members[string(key)]; ok {
229 delete(s.members, string(key))
231 s.members[string(key)] = n - 1
236 // Contains whether the given IP is contained in the set.
237 func (s DistinctNetSet) Contains(ip net.IP) bool {
239 _, ok := s.members[string(key)]
243 // Len returns the number of tracked IPs.
244 func (s DistinctNetSet) Len() int {
246 for _, i := range s.members {
252 // key encodes the map key for an address into a temporary buffer.
254 // The first byte of key is '4' or '6' to distinguish IPv4/IPv6 address types.
255 // The remainder of the key is the IP, truncated to the number of bits.
256 func (s *DistinctNetSet) key(ip net.IP) net.IP {
257 // Lazily initialize storage.
258 if s.members == nil {
259 s.members = make(map[string]uint)
260 s.buf = make(net.IP, 17)
262 // Canonicalize ip and bits.
264 if ip4 := ip.To4(); ip4 != nil {
268 if bits > uint(len(ip)*8) {
269 bits = uint(len(ip) * 8)
271 // Encode the prefix into s.buf.
273 mask := ^byte(0xFF >> (bits % 8))
275 buf := append(s.buf[:1], ip[:nb]...)
276 if nb < len(ip) && mask != 0 {
277 buf = append(buf, ip[nb]&mask)
282 // String implements fmt.Stringer
283 func (s DistinctNetSet) String() string {
286 keys := make([]string, 0, len(s.members))
287 for k := range s.members {
288 keys = append(keys, k)
291 for i, k := range keys {
296 ip = make(net.IP, 16)
299 fmt.Fprintf(&buf, "%vĂ—%d", ip, s.members[k])
300 if i != len(keys)-1 {