OSDN Git Service

Set theme jekyll-theme-leap-day
[bytom/vapor.git] / p2p / netaddress.go
1 // Modified for Bytom
2 // Originally Copyright (c) 2013-2014 Conformal Systems LLC.
3 // https://github.com/conformal/btcd/blob/master/LICENSE
4
5 package p2p
6
7 import (
8         "errors"
9         "flag"
10         "net"
11         "strconv"
12         "time"
13
14         cmn "github.com/tendermint/tmlibs/common"
15         "github.com/btcsuite/go-socks/socks"
16 )
17
18 // NetAddress defines information about a peer on the network
19 // including its IP address, and port.
20 type NetAddress struct {
21         IP   net.IP
22         Port uint16
23         str  string
24 }
25
26 // NewNetAddress returns a new NetAddress using the provided TCP
27 // address. When testing, other net.Addr (except TCP) will result in
28 // using 0.0.0.0:0. When normal run, other net.Addr (except TCP) will
29 // panic.
30 // TODO: socks proxies?
31 func NewNetAddress(addr net.Addr) *NetAddress {
32         tcpAddr, ok := addr.(*net.TCPAddr)
33         if !ok {
34                 if flag.Lookup("test.v") == nil { // normal run
35                         cmn.PanicSanity(cmn.Fmt("Only TCPAddrs are supported. Got: %v", addr))
36                 } else { // in testing
37                         return NewNetAddressIPPort(net.IP("0.0.0.0"), 0)
38                 }
39         }
40         ip := tcpAddr.IP
41         port := uint16(tcpAddr.Port)
42         return NewNetAddressIPPort(ip, port)
43 }
44
45 // NewNetAddressString returns a new NetAddress using the provided
46 // address in the form of "IP:Port". Also resolves the host if host
47 // is not an IP.
48 func NewNetAddressString(addr string) (*NetAddress, error) {
49         host, portStr, err := net.SplitHostPort(addr)
50         if err != nil {
51                 return nil, err
52         }
53
54         ip := net.ParseIP(host)
55         if ip == nil {
56                 if len(host) > 0 {
57                         ips, err := net.LookupIP(host)
58                         if err != nil {
59                                 return nil, err
60                         }
61                         ip = ips[0]
62                 }
63         }
64
65         port, err := strconv.ParseUint(portStr, 10, 16)
66         if err != nil {
67                 return nil, err
68         }
69
70         na := NewNetAddressIPPort(ip, uint16(port))
71         return na, nil
72 }
73
74 // NewNetAddressStrings returns an array of NetAddress'es build using
75 // the provided strings.
76 func NewNetAddressStrings(addrs []string) ([]*NetAddress, error) {
77         netAddrs := make([]*NetAddress, len(addrs))
78         for i, addr := range addrs {
79                 netAddr, err := NewNetAddressString(addr)
80                 if err != nil {
81                         return nil, errors.New(cmn.Fmt("Error in address %s: %v", addr, err))
82                 }
83                 netAddrs[i] = netAddr
84         }
85         return netAddrs, nil
86 }
87
88 // NewNetAddressIPPort returns a new NetAddress using the provided IP
89 // and port number.
90 func NewNetAddressIPPort(ip net.IP, port uint16) *NetAddress {
91         return &NetAddress{
92                 IP:   ip,
93                 Port: port,
94                 str: net.JoinHostPort(
95                         ip.String(),
96                         strconv.FormatUint(uint64(port), 10),
97                 ),
98         }
99 }
100
101 // Equals reports whether na and other are the same addresses.
102 func (na *NetAddress) Equals(other interface{}) bool {
103         if o, ok := other.(*NetAddress); ok {
104                 return na.String() == o.String()
105         }
106         return false
107 }
108
109 // String representation.
110 func (na *NetAddress) String() string {
111         if na.str == "" {
112                 na.str = net.JoinHostPort(
113                         na.IP.String(),
114                         strconv.FormatUint(uint64(na.Port), 10),
115                 )
116         }
117         return na.str
118 }
119
120 //DialString dial address string representation
121 func (na *NetAddress) DialString() string {
122         return net.JoinHostPort(
123                 na.IP.String(),
124                 strconv.FormatUint(uint64(na.Port), 10),
125         )
126 }
127
128 // Dial calls net.Dial on the address.
129 func (na *NetAddress) Dial() (net.Conn, error) {
130         conn, err := net.Dial("tcp", na.DialString())
131         if err != nil {
132                 return nil, err
133         }
134         return conn, nil
135 }
136
137 // DialTimeout calls net.DialTimeout on the address.
138 func (na *NetAddress) DialTimeout(timeout time.Duration) (net.Conn, error) {
139         conn, err := net.DialTimeout("tcp", na.DialString(), timeout)
140         if err != nil {
141                 return nil, err
142         }
143         return conn, nil
144 }
145
146 // DialTimeoutWithProxy calls socks.Proxy.DialTimeout on the address.
147 func (na *NetAddress) DialTimeoutWithProxy(proxy *socks.Proxy, timeout time.Duration) (net.Conn, error) {
148         conn, err := proxy.DialTimeout("tcp", na.DialString(), timeout)
149         if err != nil {
150                 return nil, err
151         }
152         return conn, nil
153 }
154
155 // Routable returns true if the address is routable.
156 func (na *NetAddress) Routable() bool {
157         // TODO(oga) bitcoind doesn't include RFC3849 here, but should we?
158         return na.Valid() && !(na.RFC1918() || na.RFC3927() || na.RFC4862() ||
159                 na.RFC4193() || na.RFC4843() || na.Local())
160 }
161
162 // Valid For IPv4 these are either a 0 or all bits set address. For IPv6 a zero
163 // address or one that matches the RFC3849 documentation address format.
164 func (na *NetAddress) Valid() bool {
165         return na.IP != nil && !(na.IP.IsUnspecified() || na.RFC3849() ||
166                 na.IP.Equal(net.IPv4bcast))
167 }
168
169 // Local returns true if it is a local address.
170 func (na *NetAddress) Local() bool {
171         return na.IP.IsLoopback() || zero4.Contains(na.IP)
172 }
173
174 // ReachabilityTo checks whenever o can be reached from na.
175 func (na *NetAddress) ReachabilityTo(o *NetAddress) int {
176         const (
177                 Unreachable = 0
178                 Default     = iota
179                 Teredo
180                 Ipv6Weak
181                 Ipv4
182                 Ipv6Strong
183         )
184         if !na.Routable() {
185                 return Unreachable
186         } else if na.RFC4380() {
187                 if !o.Routable() {
188                         return Default
189                 } else if o.RFC4380() {
190                         return Teredo
191                 } else if o.IP.To4() != nil {
192                         return Ipv4
193                 }
194                 return Ipv6Weak
195         } else if na.IP.To4() != nil {
196                 if o.Routable() && o.IP.To4() != nil {
197                         return Ipv4
198                 }
199                 return Default
200         }
201
202         var tunnelled bool
203         // Is our v6 is tunnelled?
204         if o.RFC3964() || o.RFC6052() || o.RFC6145() {
205                 tunnelled = true
206         }
207         if !o.Routable() {
208                 return Default
209         } else if o.RFC4380() {
210                 return Teredo
211         } else if o.IP.To4() != nil {
212                 return Ipv4
213         } else if tunnelled {
214                 // only prioritise ipv6 if we aren't tunnelling it.
215                 return Ipv6Weak
216         }
217         return Ipv6Strong
218 }
219
220 var rfc1918_10 = net.IPNet{IP: net.ParseIP("10.0.0.0"), Mask: net.CIDRMask(8, 32)}
221 var rfc1918_192 = net.IPNet{IP: net.ParseIP("192.168.0.0"), Mask: net.CIDRMask(16, 32)}
222 var rfc1918_172 = net.IPNet{IP: net.ParseIP("172.16.0.0"), Mask: net.CIDRMask(12, 32)}
223 var rfc3849 = net.IPNet{IP: net.ParseIP("2001:0DB8::"), Mask: net.CIDRMask(32, 128)}
224 var rfc3927 = net.IPNet{IP: net.ParseIP("169.254.0.0"), Mask: net.CIDRMask(16, 32)}
225 var rfc3964 = net.IPNet{IP: net.ParseIP("2002::"), Mask: net.CIDRMask(16, 128)}
226 var rfc4193 = net.IPNet{IP: net.ParseIP("FC00::"), Mask: net.CIDRMask(7, 128)}
227 var rfc4380 = net.IPNet{IP: net.ParseIP("2001::"), Mask: net.CIDRMask(32, 128)}
228 var rfc4843 = net.IPNet{IP: net.ParseIP("2001:10::"), Mask: net.CIDRMask(28, 128)}
229 var rfc4862 = net.IPNet{IP: net.ParseIP("FE80::"), Mask: net.CIDRMask(64, 128)}
230 var rfc6052 = net.IPNet{IP: net.ParseIP("64:FF9B::"), Mask: net.CIDRMask(96, 128)}
231 var rfc6145 = net.IPNet{IP: net.ParseIP("::FFFF:0:0:0"), Mask: net.CIDRMask(96, 128)}
232 var zero4 = net.IPNet{IP: net.ParseIP("0.0.0.0"), Mask: net.CIDRMask(8, 32)}
233
234 // RFC1918 IPv4 Private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
235 func (na *NetAddress) RFC1918() bool {
236         return rfc1918_10.Contains(na.IP) || rfc1918_192.Contains(na.IP) || rfc1918_172.Contains(na.IP)
237 }
238
239 // RFC3849 IPv6 Documentation address  (2001:0DB8::/32)
240 func (na *NetAddress) RFC3849() bool {
241         return rfc3849.Contains(na.IP)
242 }
243
244 // RFC3927 IPv4 Autoconfig (169.254.0.0/16)
245 func (na *NetAddress) RFC3927() bool {
246         return rfc3927.Contains(na.IP)
247 }
248
249 // RFC3964 IPv6 6to4 (2002::/16)
250 func (na *NetAddress) RFC3964() bool {
251         return rfc3964.Contains(na.IP)
252 }
253
254 // RFC4193 IPv6 unique local (FC00::/7)
255 func (na *NetAddress) RFC4193() bool {
256         return rfc4193.Contains(na.IP)
257 }
258
259 // RFC4380 IPv6 Teredo tunneling (2001::/32)
260 func (na *NetAddress) RFC4380() bool {
261         return rfc4380.Contains(na.IP)
262 }
263
264 // RFC4843 IPv6 ORCHID: (2001:10::/28)
265 func (na *NetAddress) RFC4843() bool {
266         return rfc4843.Contains(na.IP)
267 }
268
269 // RFC4862 IPv6 Autoconfig (FE80::/64)
270 func (na *NetAddress) RFC4862() bool {
271         return rfc4862.Contains(na.IP)
272 }
273
274 // RFC6052 IPv6 well known prefix (64:FF9B::/96)
275 func (na *NetAddress) RFC6052() bool {
276         return rfc6052.Contains(na.IP)
277 }
278
279 // RFC6145 IPv6 IPv4 translated address ::FFFF:0:0:0/96
280 func (na *NetAddress) RFC6145() bool {
281         return rfc6145.Contains(na.IP)
282 }