OSDN Git Service

Merge pull request #935 from Bytom/dev
[bytom/bytom.git] / p2p / netaddress.go
1 // Modified for Tendermint
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 )
16
17 // NetAddress defines information about a peer on the network
18 // including its IP address, and port.
19 type NetAddress struct {
20         IP   net.IP
21         Port uint16
22         str  string
23 }
24
25 // NewNetAddress returns a new NetAddress using the provided TCP
26 // address. When testing, other net.Addr (except TCP) will result in
27 // using 0.0.0.0:0. When normal run, other net.Addr (except TCP) will
28 // panic.
29 // TODO: socks proxies?
30 func NewNetAddress(addr net.Addr) *NetAddress {
31         tcpAddr, ok := addr.(*net.TCPAddr)
32         if !ok {
33                 if flag.Lookup("test.v") == nil { // normal run
34                         cmn.PanicSanity(cmn.Fmt("Only TCPAddrs are supported. Got: %v", addr))
35                 } else { // in testing
36                         return NewNetAddressIPPort(net.IP("0.0.0.0"), 0)
37                 }
38         }
39         ip := tcpAddr.IP
40         port := uint16(tcpAddr.Port)
41         return NewNetAddressIPPort(ip, port)
42 }
43
44 // NewNetAddressString returns a new NetAddress using the provided
45 // address in the form of "IP:Port". Also resolves the host if host
46 // is not an IP.
47 func NewNetAddressString(addr string) (*NetAddress, error) {
48
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         na := &NetAddress{
92                 IP:   ip,
93                 Port: port,
94                 str: net.JoinHostPort(
95                         ip.String(),
96                         strconv.FormatUint(uint64(port), 10),
97                 ),
98         }
99         return na
100 }
101
102 // Equals reports whether na and other are the same addresses.
103 func (na *NetAddress) Equals(other interface{}) bool {
104         if o, ok := other.(*NetAddress); ok {
105                 return na.String() == o.String()
106         }
107
108         return false
109 }
110
111 func (na *NetAddress) Less(other interface{}) bool {
112         if o, ok := other.(*NetAddress); ok {
113                 return na.String() < o.String()
114         }
115
116         cmn.PanicSanity("Cannot compare unequal types")
117         return false
118 }
119
120 // String representation.
121 func (na *NetAddress) String() string {
122         if na.str == "" {
123                 na.str = net.JoinHostPort(
124                         na.IP.String(),
125                         strconv.FormatUint(uint64(na.Port), 10),
126                 )
127         }
128         return na.str
129 }
130
131 // Dial calls net.Dial on the address.
132 func (na *NetAddress) Dial() (net.Conn, error) {
133         conn, err := net.Dial("tcp", na.String())
134         if err != nil {
135                 return nil, err
136         }
137         return conn, nil
138 }
139
140 // DialTimeout calls net.DialTimeout on the address.
141 func (na *NetAddress) DialTimeout(timeout time.Duration) (net.Conn, error) {
142         conn, err := net.DialTimeout("tcp", na.String(), timeout)
143         if err != nil {
144                 return nil, err
145         }
146         return conn, nil
147 }
148
149 // Routable returns true if the address is routable.
150 func (na *NetAddress) Routable() bool {
151         // TODO(oga) bitcoind doesn't include RFC3849 here, but should we?
152         return na.Valid() && !(na.RFC1918() || na.RFC3927() || na.RFC4862() ||
153                 na.RFC4193() || na.RFC4843() || na.Local())
154 }
155
156 // For IPv4 these are either a 0 or all bits set address. For IPv6 a zero
157 // address or one that matches the RFC3849 documentation address format.
158 func (na *NetAddress) Valid() bool {
159         return na.IP != nil && !(na.IP.IsUnspecified() || na.RFC3849() ||
160                 na.IP.Equal(net.IPv4bcast))
161 }
162
163 // Local returns true if it is a local address.
164 func (na *NetAddress) Local() bool {
165         return na.IP.IsLoopback() || zero4.Contains(na.IP)
166 }
167
168 // ReachabilityTo checks whenever o can be reached from na.
169 func (na *NetAddress) ReachabilityTo(o *NetAddress) int {
170         const (
171                 Unreachable = 0
172                 Default     = iota
173                 Teredo
174                 Ipv6_weak
175                 Ipv4
176                 Ipv6_strong
177                 Private
178         )
179         if !na.Routable() {
180                 return Unreachable
181         } else if na.RFC4380() {
182                 if !o.Routable() {
183                         return Default
184                 } else if o.RFC4380() {
185                         return Teredo
186                 } else if o.IP.To4() != nil {
187                         return Ipv4
188                 } else { // ipv6
189                         return Ipv6_weak
190                 }
191         } else if na.IP.To4() != nil {
192                 if o.Routable() && o.IP.To4() != nil {
193                         return Ipv4
194                 }
195                 return Default
196         } else /* ipv6 */ {
197                 var tunnelled bool
198                 // Is our v6 is tunnelled?
199                 if o.RFC3964() || o.RFC6052() || o.RFC6145() {
200                         tunnelled = true
201                 }
202                 if !o.Routable() {
203                         return Default
204                 } else if o.RFC4380() {
205                         return Teredo
206                 } else if o.IP.To4() != nil {
207                         return Ipv4
208                 } else if tunnelled {
209                         // only prioritise ipv6 if we aren't tunnelling it.
210                         return Ipv6_weak
211                 }
212                 return Ipv6_strong
213         }
214 }
215
216 // RFC1918: IPv4 Private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
217 // RFC3849: IPv6 Documentation address  (2001:0DB8::/32)
218 // RFC3927: IPv4 Autoconfig (169.254.0.0/16)
219 // RFC3964: IPv6 6to4 (2002::/16)
220 // RFC4193: IPv6 unique local (FC00::/7)
221 // RFC4380: IPv6 Teredo tunneling (2001::/32)
222 // RFC4843: IPv6 ORCHID: (2001:10::/28)
223 // RFC4862: IPv6 Autoconfig (FE80::/64)
224 // RFC6052: IPv6 well known prefix (64:FF9B::/96)
225 // RFC6145: IPv6 IPv4 translated address ::FFFF:0:0:0/96
226 var rfc1918_10 = net.IPNet{IP: net.ParseIP("10.0.0.0"), Mask: net.CIDRMask(8, 32)}
227 var rfc1918_192 = net.IPNet{IP: net.ParseIP("192.168.0.0"), Mask: net.CIDRMask(16, 32)}
228 var rfc1918_172 = net.IPNet{IP: net.ParseIP("172.16.0.0"), Mask: net.CIDRMask(12, 32)}
229 var rfc3849 = net.IPNet{IP: net.ParseIP("2001:0DB8::"), Mask: net.CIDRMask(32, 128)}
230 var rfc3927 = net.IPNet{IP: net.ParseIP("169.254.0.0"), Mask: net.CIDRMask(16, 32)}
231 var rfc3964 = net.IPNet{IP: net.ParseIP("2002::"), Mask: net.CIDRMask(16, 128)}
232 var rfc4193 = net.IPNet{IP: net.ParseIP("FC00::"), Mask: net.CIDRMask(7, 128)}
233 var rfc4380 = net.IPNet{IP: net.ParseIP("2001::"), Mask: net.CIDRMask(32, 128)}
234 var rfc4843 = net.IPNet{IP: net.ParseIP("2001:10::"), Mask: net.CIDRMask(28, 128)}
235 var rfc4862 = net.IPNet{IP: net.ParseIP("FE80::"), Mask: net.CIDRMask(64, 128)}
236 var rfc6052 = net.IPNet{IP: net.ParseIP("64:FF9B::"), Mask: net.CIDRMask(96, 128)}
237 var rfc6145 = net.IPNet{IP: net.ParseIP("::FFFF:0:0:0"), Mask: net.CIDRMask(96, 128)}
238 var zero4 = net.IPNet{IP: net.ParseIP("0.0.0.0"), Mask: net.CIDRMask(8, 32)}
239
240 func (na *NetAddress) RFC1918() bool {
241         return rfc1918_10.Contains(na.IP) ||
242                 rfc1918_192.Contains(na.IP) ||
243                 rfc1918_172.Contains(na.IP)
244 }
245 func (na *NetAddress) RFC3849() bool { return rfc3849.Contains(na.IP) }
246 func (na *NetAddress) RFC3927() bool { return rfc3927.Contains(na.IP) }
247 func (na *NetAddress) RFC3964() bool { return rfc3964.Contains(na.IP) }
248 func (na *NetAddress) RFC4193() bool { return rfc4193.Contains(na.IP) }
249 func (na *NetAddress) RFC4380() bool { return rfc4380.Contains(na.IP) }
250 func (na *NetAddress) RFC4843() bool { return rfc4843.Contains(na.IP) }
251 func (na *NetAddress) RFC4862() bool { return rfc4862.Contains(na.IP) }
252 func (na *NetAddress) RFC6052() bool { return rfc6052.Contains(na.IP) }
253 func (na *NetAddress) RFC6145() bool { return rfc6145.Contains(na.IP) }