2 // Originally Copyright (c) 2013-2014 Conformal Systems LLC.
3 // https://github.com/conformal/btcd/blob/master/LICENSE
14 cmn "github.com/tendermint/tmlibs/common"
15 "github.com/btcsuite/go-socks/socks"
18 // NetAddress defines information about a peer on the network
19 // including its IP address, and port.
20 type NetAddress struct {
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
30 // TODO: socks proxies?
31 func NewNetAddress(addr net.Addr) *NetAddress {
32 tcpAddr, ok := addr.(*net.TCPAddr)
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)
41 port := uint16(tcpAddr.Port)
42 return NewNetAddressIPPort(ip, port)
45 // NewNetAddressString returns a new NetAddress using the provided
46 // address in the form of "IP:Port". Also resolves the host if host
48 func NewNetAddressString(addr string) (*NetAddress, error) {
49 host, portStr, err := net.SplitHostPort(addr)
54 ip := net.ParseIP(host)
57 ips, err := net.LookupIP(host)
65 port, err := strconv.ParseUint(portStr, 10, 16)
70 na := NewNetAddressIPPort(ip, uint16(port))
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)
81 return nil, errors.New(cmn.Fmt("Error in address %s: %v", addr, err))
88 // NewNetAddressIPPort returns a new NetAddress using the provided IP
90 func NewNetAddressIPPort(ip net.IP, port uint16) *NetAddress {
94 str: net.JoinHostPort(
96 strconv.FormatUint(uint64(port), 10),
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()
109 // String representation.
110 func (na *NetAddress) String() string {
112 na.str = net.JoinHostPort(
114 strconv.FormatUint(uint64(na.Port), 10),
120 //DialString dial address string representation
121 func (na *NetAddress) DialString() string {
122 return net.JoinHostPort(
124 strconv.FormatUint(uint64(na.Port), 10),
128 // Dial calls net.Dial on the address.
129 func (na *NetAddress) Dial() (net.Conn, error) {
130 conn, err := net.Dial("tcp", na.DialString())
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)
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)
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())
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))
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)
174 // ReachabilityTo checks whenever o can be reached from na.
175 func (na *NetAddress) ReachabilityTo(o *NetAddress) int {
186 } else if na.RFC4380() {
189 } else if o.RFC4380() {
191 } else if o.IP.To4() != nil {
195 } else if na.IP.To4() != nil {
196 if o.Routable() && o.IP.To4() != nil {
203 // Is our v6 is tunnelled?
204 if o.RFC3964() || o.RFC6052() || o.RFC6145() {
209 } else if o.RFC4380() {
211 } else if o.IP.To4() != nil {
213 } else if tunnelled {
214 // only prioritise ipv6 if we aren't tunnelling it.
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)}
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)
239 // RFC3849 IPv6 Documentation address (2001:0DB8::/32)
240 func (na *NetAddress) RFC3849() bool {
241 return rfc3849.Contains(na.IP)
244 // RFC3927 IPv4 Autoconfig (169.254.0.0/16)
245 func (na *NetAddress) RFC3927() bool {
246 return rfc3927.Contains(na.IP)
249 // RFC3964 IPv6 6to4 (2002::/16)
250 func (na *NetAddress) RFC3964() bool {
251 return rfc3964.Contains(na.IP)
254 // RFC4193 IPv6 unique local (FC00::/7)
255 func (na *NetAddress) RFC4193() bool {
256 return rfc4193.Contains(na.IP)
259 // RFC4380 IPv6 Teredo tunneling (2001::/32)
260 func (na *NetAddress) RFC4380() bool {
261 return rfc4380.Contains(na.IP)
264 // RFC4843 IPv6 ORCHID: (2001:10::/28)
265 func (na *NetAddress) RFC4843() bool {
266 return rfc4843.Contains(na.IP)
269 // RFC4862 IPv6 Autoconfig (FE80::/64)
270 func (na *NetAddress) RFC4862() bool {
271 return rfc4862.Contains(na.IP)
274 // RFC6052 IPv6 well known prefix (64:FF9B::/96)
275 func (na *NetAddress) RFC6052() bool {
276 return rfc6052.Contains(na.IP)
279 // RFC6145 IPv6 IPv4 translated address ::FFFF:0:0:0/96
280 func (na *NetAddress) RFC6145() bool {
281 return rfc6145.Contains(na.IP)