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.

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