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.

210 lines
5.8 KiB

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