You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

281 lines
8.0 KiB

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