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.

217 lines
6.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 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. "fmt"
  7. "net"
  8. "strconv"
  9. "time"
  10. . "github.com/tendermint/tendermint/common"
  11. )
  12. type NetAddress struct {
  13. IP net.IP
  14. Port uint16
  15. str string
  16. }
  17. // TODO: socks proxies?
  18. func NewNetAddress(addr net.Addr) *NetAddress {
  19. tcpAddr, ok := addr.(*net.TCPAddr)
  20. if !ok {
  21. PanicSanity(fmt.Sprintf("Only TCPAddrs are supported. Got: %v", addr))
  22. }
  23. ip := tcpAddr.IP
  24. port := uint16(tcpAddr.Port)
  25. return NewNetAddressIPPort(ip, port)
  26. }
  27. // Also resolves the host if host is not an IP.
  28. func NewNetAddressString(addr string) *NetAddress {
  29. host, portStr, err := net.SplitHostPort(addr)
  30. if err != nil {
  31. PanicSanity(err)
  32. }
  33. ip := net.ParseIP(host)
  34. if ip == nil {
  35. if len(host) > 0 {
  36. ips, err := net.LookupIP(host)
  37. if err != nil {
  38. PanicSanity(err)
  39. }
  40. ip = ips[0]
  41. }
  42. }
  43. port, err := strconv.ParseUint(portStr, 10, 16)
  44. if err != nil {
  45. PanicSanity(err)
  46. }
  47. na := NewNetAddressIPPort(ip, uint16(port))
  48. return na
  49. }
  50. func NewNetAddressIPPort(ip net.IP, port uint16) *NetAddress {
  51. na := &NetAddress{
  52. IP: ip,
  53. Port: port,
  54. str: net.JoinHostPort(
  55. ip.String(),
  56. strconv.FormatUint(uint64(port), 10),
  57. ),
  58. }
  59. return na
  60. }
  61. func (na *NetAddress) Equals(other interface{}) bool {
  62. if o, ok := other.(*NetAddress); ok {
  63. return na.String() == o.String()
  64. } else {
  65. return false
  66. }
  67. }
  68. func (na *NetAddress) Less(other interface{}) bool {
  69. if o, ok := other.(*NetAddress); ok {
  70. return na.String() < o.String()
  71. } else {
  72. PanicSanity("Cannot compare unequal types")
  73. return false
  74. }
  75. }
  76. func (na *NetAddress) String() string {
  77. if na.str == "" {
  78. na.str = net.JoinHostPort(
  79. na.IP.String(),
  80. strconv.FormatUint(uint64(na.Port), 10),
  81. )
  82. }
  83. return na.str
  84. }
  85. func (na *NetAddress) Dial() (net.Conn, error) {
  86. conn, err := net.Dial("tcp", na.String())
  87. if err != nil {
  88. return nil, err
  89. }
  90. return conn, nil
  91. }
  92. func (na *NetAddress) DialTimeout(timeout time.Duration) (net.Conn, error) {
  93. conn, err := net.DialTimeout("tcp", na.String(), timeout)
  94. if err != nil {
  95. return nil, err
  96. }
  97. return conn, nil
  98. }
  99. func (na *NetAddress) Routable() bool {
  100. if config.GetBool("local_routing") {
  101. return na.Valid()
  102. }
  103. // TODO(oga) bitcoind doesn't include RFC3849 here, but should we?
  104. return na.Valid() && !(na.RFC1918() || na.RFC3927() || na.RFC4862() ||
  105. na.RFC4193() || na.RFC4843() || na.Local())
  106. }
  107. // For IPv4 these are either a 0 or all bits set address. For IPv6 a zero
  108. // address or one that matches the RFC3849 documentation address format.
  109. func (na *NetAddress) Valid() bool {
  110. return na.IP != nil && !(na.IP.IsUnspecified() || na.RFC3849() ||
  111. na.IP.Equal(net.IPv4bcast))
  112. }
  113. func (na *NetAddress) Local() bool {
  114. return na.IP.IsLoopback() || zero4.Contains(na.IP)
  115. }
  116. func (na *NetAddress) ReachabilityTo(o *NetAddress) int {
  117. const (
  118. Unreachable = 0
  119. Default = iota
  120. Teredo
  121. Ipv6_weak
  122. Ipv4
  123. Ipv6_strong
  124. Private
  125. )
  126. if !na.Routable() {
  127. return Unreachable
  128. } else if na.RFC4380() {
  129. if !o.Routable() {
  130. return Default
  131. } else if o.RFC4380() {
  132. return Teredo
  133. } else if o.IP.To4() != nil {
  134. return Ipv4
  135. } else { // ipv6
  136. return Ipv6_weak
  137. }
  138. } else if na.IP.To4() != nil {
  139. if o.Routable() && o.IP.To4() != nil {
  140. return Ipv4
  141. }
  142. return Default
  143. } else /* ipv6 */ {
  144. var tunnelled bool
  145. // Is our v6 is tunnelled?
  146. if o.RFC3964() || o.RFC6052() || o.RFC6145() {
  147. tunnelled = true
  148. }
  149. if !o.Routable() {
  150. return Default
  151. } else if o.RFC4380() {
  152. return Teredo
  153. } else if o.IP.To4() != nil {
  154. return Ipv4
  155. } else if tunnelled {
  156. // only prioritise ipv6 if we aren't tunnelling it.
  157. return Ipv6_weak
  158. }
  159. return Ipv6_strong
  160. }
  161. }
  162. // RFC1918: IPv4 Private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
  163. // RFC3849: IPv6 Documentation address (2001:0DB8::/32)
  164. // RFC3927: IPv4 Autoconfig (169.254.0.0/16)
  165. // RFC3964: IPv6 6to4 (2002::/16)
  166. // RFC4193: IPv6 unique local (FC00::/7)
  167. // RFC4380: IPv6 Teredo tunneling (2001::/32)
  168. // RFC4843: IPv6 ORCHID: (2001:10::/28)
  169. // RFC4862: IPv6 Autoconfig (FE80::/64)
  170. // RFC6052: IPv6 well known prefix (64:FF9B::/96)
  171. // RFC6145: IPv6 IPv4 translated address ::FFFF:0:0:0/96
  172. var rfc1918_10 = net.IPNet{IP: net.ParseIP("10.0.0.0"), Mask: net.CIDRMask(8, 32)}
  173. var rfc1918_192 = net.IPNet{IP: net.ParseIP("192.168.0.0"), Mask: net.CIDRMask(16, 32)}
  174. var rfc1918_172 = net.IPNet{IP: net.ParseIP("172.16.0.0"), Mask: net.CIDRMask(12, 32)}
  175. var rfc3849 = net.IPNet{IP: net.ParseIP("2001:0DB8::"), Mask: net.CIDRMask(32, 128)}
  176. var rfc3927 = net.IPNet{IP: net.ParseIP("169.254.0.0"), Mask: net.CIDRMask(16, 32)}
  177. var rfc3964 = net.IPNet{IP: net.ParseIP("2002::"), Mask: net.CIDRMask(16, 128)}
  178. var rfc4193 = net.IPNet{IP: net.ParseIP("FC00::"), Mask: net.CIDRMask(7, 128)}
  179. var rfc4380 = net.IPNet{IP: net.ParseIP("2001::"), Mask: net.CIDRMask(32, 128)}
  180. var rfc4843 = net.IPNet{IP: net.ParseIP("2001:10::"), Mask: net.CIDRMask(28, 128)}
  181. var rfc4862 = net.IPNet{IP: net.ParseIP("FE80::"), Mask: net.CIDRMask(64, 128)}
  182. var rfc6052 = net.IPNet{IP: net.ParseIP("64:FF9B::"), Mask: net.CIDRMask(96, 128)}
  183. var rfc6145 = net.IPNet{IP: net.ParseIP("::FFFF:0:0:0"), Mask: net.CIDRMask(96, 128)}
  184. var zero4 = net.IPNet{IP: net.ParseIP("0.0.0.0"), Mask: net.CIDRMask(8, 32)}
  185. func (na *NetAddress) RFC1918() bool {
  186. return rfc1918_10.Contains(na.IP) ||
  187. rfc1918_192.Contains(na.IP) ||
  188. rfc1918_172.Contains(na.IP)
  189. }
  190. func (na *NetAddress) RFC3849() bool { return rfc3849.Contains(na.IP) }
  191. func (na *NetAddress) RFC3927() bool { return rfc3927.Contains(na.IP) }
  192. func (na *NetAddress) RFC3964() bool { return rfc3964.Contains(na.IP) }
  193. func (na *NetAddress) RFC4193() bool { return rfc4193.Contains(na.IP) }
  194. func (na *NetAddress) RFC4380() bool { return rfc4380.Contains(na.IP) }
  195. func (na *NetAddress) RFC4843() bool { return rfc4843.Contains(na.IP) }
  196. func (na *NetAddress) RFC4862() bool { return rfc4862.Contains(na.IP) }
  197. func (na *NetAddress) RFC6052() bool { return rfc6052.Contains(na.IP) }
  198. func (na *NetAddress) RFC6145() bool { return rfc6145.Contains(na.IP) }