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.

253 lines
7.3 KiB

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