github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/p2p/netutil/net.go (about) 1 // Copyright 2016 The Spectrum Authors 2 // This file is part of the Spectrum library. 3 // 4 // The Spectrum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The Spectrum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the Spectrum library. If not, see <http://www.gnu.org/licenses/>. 16 17 // Package netutil contains extensions to the net package. 18 package netutil 19 20 import ( 21 "errors" 22 "net" 23 "strings" 24 ) 25 26 var lan4, lan6, special4, special6 Netlist 27 28 func init() { 29 // Lists from RFC 5735, RFC 5156, 30 // https://www.iana.org/assignments/iana-ipv4-special-registry/ 31 lan4.Add("0.0.0.0/8") // "This" network 32 lan4.Add("10.0.0.0/8") // Private Use 33 lan4.Add("172.16.0.0/12") // Private Use 34 lan4.Add("192.168.0.0/16") // Private Use 35 lan6.Add("fe80::/10") // Link-Local 36 lan6.Add("fc00::/7") // Unique-Local 37 special4.Add("192.0.0.0/29") // IPv4 Service Continuity 38 special4.Add("192.0.0.9/32") // PCP Anycast 39 special4.Add("192.0.0.170/32") // NAT64/DNS64 Discovery 40 special4.Add("192.0.0.171/32") // NAT64/DNS64 Discovery 41 special4.Add("192.0.2.0/24") // TEST-NET-1 42 special4.Add("192.31.196.0/24") // AS112 43 special4.Add("192.52.193.0/24") // AMT 44 special4.Add("192.88.99.0/24") // 6to4 Relay Anycast 45 special4.Add("192.175.48.0/24") // AS112 46 special4.Add("198.18.0.0/15") // Device Benchmark Testing 47 special4.Add("198.51.100.0/24") // TEST-NET-2 48 special4.Add("203.0.113.0/24") // TEST-NET-3 49 special4.Add("255.255.255.255/32") // Limited Broadcast 50 51 // http://www.iana.org/assignments/iana-ipv6-special-registry/ 52 special6.Add("100::/64") 53 special6.Add("2001::/32") 54 special6.Add("2001:1::1/128") 55 special6.Add("2001:2::/48") 56 special6.Add("2001:3::/32") 57 special6.Add("2001:4:112::/48") 58 special6.Add("2001:5::/32") 59 special6.Add("2001:10::/28") 60 special6.Add("2001:20::/28") 61 special6.Add("2001:db8::/32") 62 special6.Add("2002::/16") 63 } 64 65 // Netlist is a list of IP networks. 66 type Netlist []net.IPNet 67 68 // ParseNetlist parses a comma-separated list of CIDR masks. 69 // Whitespace and extra commas are ignored. 70 func ParseNetlist(s string) (*Netlist, error) { 71 ws := strings.NewReplacer(" ", "", "\n", "", "\t", "") 72 masks := strings.Split(ws.Replace(s), ",") 73 l := make(Netlist, 0) 74 for _, mask := range masks { 75 if mask == "" { 76 continue 77 } 78 _, n, err := net.ParseCIDR(mask) 79 if err != nil { 80 return nil, err 81 } 82 l = append(l, *n) 83 } 84 return &l, nil 85 } 86 87 // MarshalTOML implements toml.MarshalerRec. 88 func (l Netlist) MarshalTOML() interface{} { 89 list := make([]string, 0, len(l)) 90 for _, net := range l { 91 list = append(list, net.String()) 92 } 93 return list 94 } 95 96 // UnmarshalTOML implements toml.UnmarshalerRec. 97 func (l *Netlist) UnmarshalTOML(fn func(interface{}) error) error { 98 var masks []string 99 if err := fn(&masks); err != nil { 100 return err 101 } 102 for _, mask := range masks { 103 _, n, err := net.ParseCIDR(mask) 104 if err != nil { 105 return err 106 } 107 *l = append(*l, *n) 108 } 109 return nil 110 } 111 112 // Add parses a CIDR mask and appends it to the list. It panics for invalid masks and is 113 // intended to be used for setting up static lists. 114 func (l *Netlist) Add(cidr string) { 115 _, n, err := net.ParseCIDR(cidr) 116 if err != nil { 117 panic(err) 118 } 119 *l = append(*l, *n) 120 } 121 122 // Contains reports whether the given IP is contained in the list. 123 func (l *Netlist) Contains(ip net.IP) bool { 124 if l == nil { 125 return false 126 } 127 for _, net := range *l { 128 if net.Contains(ip) { 129 return true 130 } 131 } 132 return false 133 } 134 135 // IsLAN reports whether an IP is a local network address. 136 func IsLAN(ip net.IP) bool { 137 if ip.IsLoopback() { 138 return true 139 } 140 if v4 := ip.To4(); v4 != nil { 141 return lan4.Contains(v4) 142 } 143 return lan6.Contains(ip) 144 } 145 146 // IsSpecialNetwork reports whether an IP is located in a special-use network range 147 // This includes broadcast, multicast and documentation addresses. 148 func IsSpecialNetwork(ip net.IP) bool { 149 if ip.IsMulticast() { 150 return true 151 } 152 if v4 := ip.To4(); v4 != nil { 153 return special4.Contains(v4) 154 } 155 return special6.Contains(ip) 156 } 157 158 var ( 159 errInvalid = errors.New("invalid IP") 160 errUnspecified = errors.New("zero address") 161 errSpecial = errors.New("special network") 162 errLoopback = errors.New("loopback address from non-loopback host") 163 errLAN = errors.New("LAN address from WAN host") 164 ) 165 166 // CheckRelayIP reports whether an IP relayed from the given sender IP 167 // is a valid connection target. 168 // 169 // There are four rules: 170 // - Special network addresses are never valid. 171 // - Loopback addresses are OK if relayed by a loopback host. 172 // - LAN addresses are OK if relayed by a LAN host. 173 // - All other addresses are always acceptable. 174 func CheckRelayIP(sender, addr net.IP) error { 175 if len(addr) != net.IPv4len && len(addr) != net.IPv6len { 176 return errInvalid 177 } 178 if addr.IsUnspecified() { 179 return errUnspecified 180 } 181 if IsSpecialNetwork(addr) { 182 return errSpecial 183 } 184 if addr.IsLoopback() && !sender.IsLoopback() { 185 return errLoopback 186 } 187 if IsLAN(addr) && !IsLAN(sender) { 188 return errLAN 189 } 190 return nil 191 }