github.com/devfans/go-ethereum@v1.5.10-0.20170326212234-7419d0c38291/p2p/netutil/net.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum 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 go-ethereum 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 go-ethereum 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 // Add parses a CIDR mask and appends it to the list. It panics for invalid masks and is 88 // intended to be used for setting up static lists. 89 func (l *Netlist) Add(cidr string) { 90 _, n, err := net.ParseCIDR(cidr) 91 if err != nil { 92 panic(err) 93 } 94 *l = append(*l, *n) 95 } 96 97 // Contains reports whether the given IP is contained in the list. 98 func (l *Netlist) Contains(ip net.IP) bool { 99 if l == nil { 100 return false 101 } 102 for _, net := range *l { 103 if net.Contains(ip) { 104 return true 105 } 106 } 107 return false 108 } 109 110 // IsLAN reports whether an IP is a local network address. 111 func IsLAN(ip net.IP) bool { 112 if ip.IsLoopback() { 113 return true 114 } 115 if v4 := ip.To4(); v4 != nil { 116 return lan4.Contains(v4) 117 } 118 return lan6.Contains(ip) 119 } 120 121 // IsSpecialNetwork reports whether an IP is located in a special-use network range 122 // This includes broadcast, multicast and documentation addresses. 123 func IsSpecialNetwork(ip net.IP) bool { 124 if ip.IsMulticast() { 125 return true 126 } 127 if v4 := ip.To4(); v4 != nil { 128 return special4.Contains(v4) 129 } 130 return special6.Contains(ip) 131 } 132 133 var ( 134 errInvalid = errors.New("invalid IP") 135 errUnspecified = errors.New("zero address") 136 errSpecial = errors.New("special network") 137 errLoopback = errors.New("loopback address from non-loopback host") 138 errLAN = errors.New("LAN address from WAN host") 139 ) 140 141 // CheckRelayIP reports whether an IP relayed from the given sender IP 142 // is a valid connection target. 143 // 144 // There are four rules: 145 // - Special network addresses are never valid. 146 // - Loopback addresses are OK if relayed by a loopback host. 147 // - LAN addresses are OK if relayed by a LAN host. 148 // - All other addresses are always acceptable. 149 func CheckRelayIP(sender, addr net.IP) error { 150 if len(addr) != net.IPv4len && len(addr) != net.IPv6len { 151 return errInvalid 152 } 153 if addr.IsUnspecified() { 154 return errUnspecified 155 } 156 if IsSpecialNetwork(addr) { 157 return errSpecial 158 } 159 if addr.IsLoopback() && !sender.IsLoopback() { 160 return errLoopback 161 } 162 if IsLAN(addr) && !IsLAN(sender) { 163 return errLAN 164 } 165 return nil 166 }