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.

213 lines
5.9 KiB

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. "fmt"
  7. "net"
  8. "strconv"
  9. "time"
  10. . "github.com/tendermint/go-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. // TODO(oga) bitcoind doesn't include RFC3849 here, but should we?
  101. return na.Valid() && !(na.RFC1918() || na.RFC3927() || na.RFC4862() ||
  102. na.RFC4193() || na.RFC4843() || na.Local())
  103. }
  104. // For IPv4 these are either a 0 or all bits set address. For IPv6 a zero
  105. // address or one that matches the RFC3849 documentation address format.
  106. func (na *NetAddress) Valid() bool {
  107. return na.IP != nil && !(na.IP.IsUnspecified() || na.RFC3849() ||
  108. na.IP.Equal(net.IPv4bcast))
  109. }
  110. func (na *NetAddress) Local() bool {
  111. return na.IP.IsLoopback() || zero4.Contains(na.IP)
  112. }
  113. func (na *NetAddress) ReachabilityTo(o *NetAddress) int {
  114. const (
  115. Unreachable = 0
  116. Default = iota
  117. Teredo
  118. Ipv6_weak
  119. Ipv4
  120. Ipv6_strong
  121. Private
  122. )
  123. if !na.Routable() {
  124. return Unreachable
  125. } else if na.RFC4380() {
  126. if !o.Routable() {
  127. return Default
  128. } else if o.RFC4380() {
  129. return Teredo
  130. } else if o.IP.To4() != nil {
  131. return Ipv4
  132. } else { // ipv6
  133. return Ipv6_weak
  134. }
  135. } else if na.IP.To4() != nil {
  136. if o.Routable() && o.IP.To4() != nil {
  137. return Ipv4
  138. }
  139. return Default
  140. } else /* ipv6 */ {
  141. var tunnelled bool
  142. // Is our v6 is tunnelled?
  143. if o.RFC3964() || o.RFC6052() || o.RFC6145() {
  144. tunnelled = true
  145. }
  146. if !o.Routable() {
  147. return Default
  148. } else if o.RFC4380() {
  149. return Teredo
  150. } else if o.IP.To4() != nil {
  151. return Ipv4
  152. } else if tunnelled {
  153. // only prioritise ipv6 if we aren't tunnelling it.
  154. return Ipv6_weak
  155. }
  156. return Ipv6_strong
  157. }
  158. }
  159. // RFC1918: IPv4 Private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
  160. // RFC3849: IPv6 Documentation address (2001:0DB8::/32)
  161. // RFC3927: IPv4 Autoconfig (169.254.0.0/16)
  162. // RFC3964: IPv6 6to4 (2002::/16)
  163. // RFC4193: IPv6 unique local (FC00::/7)
  164. // RFC4380: IPv6 Teredo tunneling (2001::/32)
  165. // RFC4843: IPv6 ORCHID: (2001:10::/28)
  166. // RFC4862: IPv6 Autoconfig (FE80::/64)
  167. // RFC6052: IPv6 well known prefix (64:FF9B::/96)
  168. // RFC6145: IPv6 IPv4 translated address ::FFFF:0:0:0/96
  169. var rfc1918_10 = net.IPNet{IP: net.ParseIP("10.0.0.0"), Mask: net.CIDRMask(8, 32)}
  170. var rfc1918_192 = net.IPNet{IP: net.ParseIP("192.168.0.0"), Mask: net.CIDRMask(16, 32)}
  171. var rfc1918_172 = net.IPNet{IP: net.ParseIP("172.16.0.0"), Mask: net.CIDRMask(12, 32)}
  172. var rfc3849 = net.IPNet{IP: net.ParseIP("2001:0DB8::"), Mask: net.CIDRMask(32, 128)}
  173. var rfc3927 = net.IPNet{IP: net.ParseIP("169.254.0.0"), Mask: net.CIDRMask(16, 32)}
  174. var rfc3964 = net.IPNet{IP: net.ParseIP("2002::"), Mask: net.CIDRMask(16, 128)}
  175. var rfc4193 = net.IPNet{IP: net.ParseIP("FC00::"), Mask: net.CIDRMask(7, 128)}
  176. var rfc4380 = net.IPNet{IP: net.ParseIP("2001::"), Mask: net.CIDRMask(32, 128)}
  177. var rfc4843 = net.IPNet{IP: net.ParseIP("2001:10::"), Mask: net.CIDRMask(28, 128)}
  178. var rfc4862 = net.IPNet{IP: net.ParseIP("FE80::"), Mask: net.CIDRMask(64, 128)}
  179. var rfc6052 = net.IPNet{IP: net.ParseIP("64:FF9B::"), Mask: net.CIDRMask(96, 128)}
  180. var rfc6145 = net.IPNet{IP: net.ParseIP("::FFFF:0:0:0"), Mask: net.CIDRMask(96, 128)}
  181. var zero4 = net.IPNet{IP: net.ParseIP("0.0.0.0"), Mask: net.CIDRMask(8, 32)}
  182. func (na *NetAddress) RFC1918() bool {
  183. return rfc1918_10.Contains(na.IP) ||
  184. rfc1918_192.Contains(na.IP) ||
  185. rfc1918_172.Contains(na.IP)
  186. }
  187. func (na *NetAddress) RFC3849() bool { return rfc3849.Contains(na.IP) }
  188. func (na *NetAddress) RFC3927() bool { return rfc3927.Contains(na.IP) }
  189. func (na *NetAddress) RFC3964() bool { return rfc3964.Contains(na.IP) }
  190. func (na *NetAddress) RFC4193() bool { return rfc4193.Contains(na.IP) }
  191. func (na *NetAddress) RFC4380() bool { return rfc4380.Contains(na.IP) }
  192. func (na *NetAddress) RFC4843() bool { return rfc4843.Contains(na.IP) }
  193. func (na *NetAddress) RFC4862() bool { return rfc4862.Contains(na.IP) }
  194. func (na *NetAddress) RFC6052() bool { return rfc6052.Contains(na.IP) }
  195. func (na *NetAddress) RFC6145() bool { return rfc6145.Contains(na.IP) }