OSDN Git Service

Merge pull request #41 from Bytom/dev
[bytom/vapor.git] / vendor / github.com / btcsuite / btcd / addrmgr / network.go
1 // Copyright (c) 2013-2014 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
4
5 package addrmgr
6
7 import (
8         "fmt"
9         "net"
10
11         "github.com/btcsuite/btcd/wire"
12 )
13
14 var (
15         // rfc1918Nets specifies the IPv4 private address blocks as defined by
16         // by RFC1918 (10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16).
17         rfc1918Nets = []net.IPNet{
18                 ipNet("10.0.0.0", 8, 32),
19                 ipNet("172.16.0.0", 12, 32),
20                 ipNet("192.168.0.0", 16, 32),
21         }
22
23         // rfc2544Net specifies the the IPv4 block as defined by RFC2544
24         // (198.18.0.0/15)
25         rfc2544Net = ipNet("198.18.0.0", 15, 32)
26
27         // rfc3849Net specifies the IPv6 documentation address block as defined
28         // by RFC3849 (2001:DB8::/32).
29         rfc3849Net = ipNet("2001:DB8::", 32, 128)
30
31         // rfc3927Net specifies the IPv4 auto configuration address block as
32         // defined by RFC3927 (169.254.0.0/16).
33         rfc3927Net = ipNet("169.254.0.0", 16, 32)
34
35         // rfc3964Net specifies the IPv6 to IPv4 encapsulation address block as
36         // defined by RFC3964 (2002::/16).
37         rfc3964Net = ipNet("2002::", 16, 128)
38
39         // rfc4193Net specifies the IPv6 unique local address block as defined
40         // by RFC4193 (FC00::/7).
41         rfc4193Net = ipNet("FC00::", 7, 128)
42
43         // rfc4380Net specifies the IPv6 teredo tunneling over UDP address block
44         // as defined by RFC4380 (2001::/32).
45         rfc4380Net = ipNet("2001::", 32, 128)
46
47         // rfc4843Net specifies the IPv6 ORCHID address block as defined by
48         // RFC4843 (2001:10::/28).
49         rfc4843Net = ipNet("2001:10::", 28, 128)
50
51         // rfc4862Net specifies the IPv6 stateless address autoconfiguration
52         // address block as defined by RFC4862 (FE80::/64).
53         rfc4862Net = ipNet("FE80::", 64, 128)
54
55         // rfc5737Net specifies the IPv4 documentation address blocks as defined
56         // by RFC5737 (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
57         rfc5737Net = []net.IPNet{
58                 ipNet("192.0.2.0", 24, 32),
59                 ipNet("198.51.100.0", 24, 32),
60                 ipNet("203.0.113.0", 24, 32),
61         }
62
63         // rfc6052Net specifies the IPv6 well-known prefix address block as
64         // defined by RFC6052 (64:FF9B::/96).
65         rfc6052Net = ipNet("64:FF9B::", 96, 128)
66
67         // rfc6145Net specifies the IPv6 to IPv4 translated address range as
68         // defined by RFC6145 (::FFFF:0:0:0/96).
69         rfc6145Net = ipNet("::FFFF:0:0:0", 96, 128)
70
71         // rfc6598Net specifies the IPv4 block as defined by RFC6598 (100.64.0.0/10)
72         rfc6598Net = ipNet("100.64.0.0", 10, 32)
73
74         // onionCatNet defines the IPv6 address block used to support Tor.
75         // bitcoind encodes a .onion address as a 16 byte number by decoding the
76         // address prior to the .onion (i.e. the key hash) base32 into a ten
77         // byte number. It then stores the first 6 bytes of the address as
78         // 0xfd, 0x87, 0xd8, 0x7e, 0xeb, 0x43.
79         //
80         // This is the same range used by OnionCat, which is part part of the
81         // RFC4193 unique local IPv6 range.
82         //
83         // In summary the format is:
84         // { magic 6 bytes, 10 bytes base32 decode of key hash }
85         onionCatNet = ipNet("fd87:d87e:eb43::", 48, 128)
86
87         // zero4Net defines the IPv4 address block for address staring with 0
88         // (0.0.0.0/8).
89         zero4Net = ipNet("0.0.0.0", 8, 32)
90
91         // heNet defines the Hurricane Electric IPv6 address block.
92         heNet = ipNet("2001:470::", 32, 128)
93 )
94
95 // ipNet returns a net.IPNet struct given the passed IP address string, number
96 // of one bits to include at the start of the mask, and the total number of bits
97 // for the mask.
98 func ipNet(ip string, ones, bits int) net.IPNet {
99         return net.IPNet{IP: net.ParseIP(ip), Mask: net.CIDRMask(ones, bits)}
100 }
101
102 // IsIPv4 returns whether or not the given address is an IPv4 address.
103 func IsIPv4(na *wire.NetAddress) bool {
104         return na.IP.To4() != nil
105 }
106
107 // IsLocal returns whether or not the given address is a local address.
108 func IsLocal(na *wire.NetAddress) bool {
109         return na.IP.IsLoopback() || zero4Net.Contains(na.IP)
110 }
111
112 // IsOnionCatTor returns whether or not the passed address is in the IPv6 range
113 // used by bitcoin to support Tor (fd87:d87e:eb43::/48).  Note that this range
114 // is the same range used by OnionCat, which is part of the RFC4193 unique local
115 // IPv6 range.
116 func IsOnionCatTor(na *wire.NetAddress) bool {
117         return onionCatNet.Contains(na.IP)
118 }
119
120 // IsRFC1918 returns whether or not the passed address is part of the IPv4
121 // private network address space as defined by RFC1918 (10.0.0.0/8,
122 // 172.16.0.0/12, or 192.168.0.0/16).
123 func IsRFC1918(na *wire.NetAddress) bool {
124         for _, rfc := range rfc1918Nets {
125                 if rfc.Contains(na.IP) {
126                         return true
127                 }
128         }
129         return false
130 }
131
132 // IsRFC2544 returns whether or not the passed address is part of the IPv4
133 // address space as defined by RFC2544 (198.18.0.0/15)
134 func IsRFC2544(na *wire.NetAddress) bool {
135         return rfc2544Net.Contains(na.IP)
136 }
137
138 // IsRFC3849 returns whether or not the passed address is part of the IPv6
139 // documentation range as defined by RFC3849 (2001:DB8::/32).
140 func IsRFC3849(na *wire.NetAddress) bool {
141         return rfc3849Net.Contains(na.IP)
142 }
143
144 // IsRFC3927 returns whether or not the passed address is part of the IPv4
145 // autoconfiguration range as defined by RFC3927 (169.254.0.0/16).
146 func IsRFC3927(na *wire.NetAddress) bool {
147         return rfc3927Net.Contains(na.IP)
148 }
149
150 // IsRFC3964 returns whether or not the passed address is part of the IPv6 to
151 // IPv4 encapsulation range as defined by RFC3964 (2002::/16).
152 func IsRFC3964(na *wire.NetAddress) bool {
153         return rfc3964Net.Contains(na.IP)
154 }
155
156 // IsRFC4193 returns whether or not the passed address is part of the IPv6
157 // unique local range as defined by RFC4193 (FC00::/7).
158 func IsRFC4193(na *wire.NetAddress) bool {
159         return rfc4193Net.Contains(na.IP)
160 }
161
162 // IsRFC4380 returns whether or not the passed address is part of the IPv6
163 // teredo tunneling over UDP range as defined by RFC4380 (2001::/32).
164 func IsRFC4380(na *wire.NetAddress) bool {
165         return rfc4380Net.Contains(na.IP)
166 }
167
168 // IsRFC4843 returns whether or not the passed address is part of the IPv6
169 // ORCHID range as defined by RFC4843 (2001:10::/28).
170 func IsRFC4843(na *wire.NetAddress) bool {
171         return rfc4843Net.Contains(na.IP)
172 }
173
174 // IsRFC4862 returns whether or not the passed address is part of the IPv6
175 // stateless address autoconfiguration range as defined by RFC4862 (FE80::/64).
176 func IsRFC4862(na *wire.NetAddress) bool {
177         return rfc4862Net.Contains(na.IP)
178 }
179
180 // IsRFC5737 returns whether or not the passed address is part of the IPv4
181 // documentation address space as defined by RFC5737 (192.0.2.0/24,
182 // 198.51.100.0/24, 203.0.113.0/24)
183 func IsRFC5737(na *wire.NetAddress) bool {
184         for _, rfc := range rfc5737Net {
185                 if rfc.Contains(na.IP) {
186                         return true
187                 }
188         }
189
190         return false
191 }
192
193 // IsRFC6052 returns whether or not the passed address is part of the IPv6
194 // well-known prefix range as defined by RFC6052 (64:FF9B::/96).
195 func IsRFC6052(na *wire.NetAddress) bool {
196         return rfc6052Net.Contains(na.IP)
197 }
198
199 // IsRFC6145 returns whether or not the passed address is part of the IPv6 to
200 // IPv4 translated address range as defined by RFC6145 (::FFFF:0:0:0/96).
201 func IsRFC6145(na *wire.NetAddress) bool {
202         return rfc6145Net.Contains(na.IP)
203 }
204
205 // IsRFC6598 returns whether or not the passed address is part of the IPv4
206 // shared address space specified by RFC6598 (100.64.0.0/10)
207 func IsRFC6598(na *wire.NetAddress) bool {
208         return rfc6598Net.Contains(na.IP)
209 }
210
211 // IsValid returns whether or not the passed address is valid.  The address is
212 // considered invalid under the following circumstances:
213 // IPv4: It is either a zero or all bits set address.
214 // IPv6: It is either a zero or RFC3849 documentation address.
215 func IsValid(na *wire.NetAddress) bool {
216         // IsUnspecified returns if address is 0, so only all bits set, and
217         // RFC3849 need to be explicitly checked.
218         return na.IP != nil && !(na.IP.IsUnspecified() ||
219                 na.IP.Equal(net.IPv4bcast))
220 }
221
222 // IsRoutable returns whether or not the passed address is routable over
223 // the public internet.  This is true as long as the address is valid and is not
224 // in any reserved ranges.
225 func IsRoutable(na *wire.NetAddress) bool {
226         return IsValid(na) && !(IsRFC1918(na) || IsRFC2544(na) ||
227                 IsRFC3927(na) || IsRFC4862(na) || IsRFC3849(na) ||
228                 IsRFC4843(na) || IsRFC5737(na) || IsRFC6598(na) ||
229                 IsLocal(na) || (IsRFC4193(na) && !IsOnionCatTor(na)))
230 }
231
232 // GroupKey returns a string representing the network group an address is part
233 // of.  This is the /16 for IPv4, the /32 (/36 for he.net) for IPv6, the string
234 // "local" for a local address, the string "tor:key" where key is the /4 of the
235 // onion address for Tor address, and the string "unroutable" for an unroutable
236 // address.
237 func GroupKey(na *wire.NetAddress) string {
238         if IsLocal(na) {
239                 return "local"
240         }
241         if !IsRoutable(na) {
242                 return "unroutable"
243         }
244         if IsIPv4(na) {
245                 return na.IP.Mask(net.CIDRMask(16, 32)).String()
246         }
247         if IsRFC6145(na) || IsRFC6052(na) {
248                 // last four bytes are the ip address
249                 ip := na.IP[12:16]
250                 return ip.Mask(net.CIDRMask(16, 32)).String()
251         }
252
253         if IsRFC3964(na) {
254                 ip := na.IP[2:6]
255                 return ip.Mask(net.CIDRMask(16, 32)).String()
256
257         }
258         if IsRFC4380(na) {
259                 // teredo tunnels have the last 4 bytes as the v4 address XOR
260                 // 0xff.
261                 ip := net.IP(make([]byte, 4))
262                 for i, byte := range na.IP[12:16] {
263                         ip[i] = byte ^ 0xff
264                 }
265                 return ip.Mask(net.CIDRMask(16, 32)).String()
266         }
267         if IsOnionCatTor(na) {
268                 // group is keyed off the first 4 bits of the actual onion key.
269                 return fmt.Sprintf("tor:%d", na.IP[6]&((1<<4)-1))
270         }
271
272         // OK, so now we know ourselves to be a IPv6 address.
273         // bitcoind uses /32 for everything, except for Hurricane Electric's
274         // (he.net) IP range, which it uses /36 for.
275         bits := 32
276         if heNet.Contains(na.IP) {
277                 bits = 36
278         }
279
280         return na.IP.Mask(net.CIDRMask(bits, 128)).String()
281 }