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.

216 lines
6.1 KiB

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. "net"
  7. "strconv"
  8. "time"
  9. . "github.com/tendermint/go-common"
  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. log.Warn(`Only TCPAddrs are supported. If used for anything but testing,
  21. may result in undefined behaviour!`, "addr", addr)
  22. return NewNetAddressIPPort(net.IP("0.0.0.0"), 0)
  23. // NOTE: it would be nice to only not panic if we're in testing ...
  24. // PanicSanity(Fmt("Only TCPAddrs are supported. Got: %v", addr))
  25. }
  26. ip := tcpAddr.IP
  27. port := uint16(tcpAddr.Port)
  28. return NewNetAddressIPPort(ip, port)
  29. }
  30. // Also resolves the host if host is not an IP.
  31. func NewNetAddressString(addr string) *NetAddress {
  32. host, portStr, err := net.SplitHostPort(addr)
  33. if err != nil {
  34. PanicSanity(err)
  35. }
  36. ip := net.ParseIP(host)
  37. if ip == nil {
  38. if len(host) > 0 {
  39. ips, err := net.LookupIP(host)
  40. if err != nil {
  41. PanicSanity(err)
  42. }
  43. ip = ips[0]
  44. }
  45. }
  46. port, err := strconv.ParseUint(portStr, 10, 16)
  47. if err != nil {
  48. PanicSanity(err)
  49. }
  50. na := NewNetAddressIPPort(ip, uint16(port))
  51. return na
  52. }
  53. func NewNetAddressIPPort(ip net.IP, port uint16) *NetAddress {
  54. na := &NetAddress{
  55. IP: ip,
  56. Port: port,
  57. str: net.JoinHostPort(
  58. ip.String(),
  59. strconv.FormatUint(uint64(port), 10),
  60. ),
  61. }
  62. return na
  63. }
  64. func (na *NetAddress) Equals(other interface{}) bool {
  65. if o, ok := other.(*NetAddress); ok {
  66. return na.String() == o.String()
  67. } else {
  68. return false
  69. }
  70. }
  71. func (na *NetAddress) Less(other interface{}) bool {
  72. if o, ok := other.(*NetAddress); ok {
  73. return na.String() < o.String()
  74. } else {
  75. PanicSanity("Cannot compare unequal types")
  76. return false
  77. }
  78. }
  79. func (na *NetAddress) String() string {
  80. if na.str == "" {
  81. na.str = net.JoinHostPort(
  82. na.IP.String(),
  83. strconv.FormatUint(uint64(na.Port), 10),
  84. )
  85. }
  86. return na.str
  87. }
  88. func (na *NetAddress) Dial() (net.Conn, error) {
  89. conn, err := net.Dial("tcp", na.String())
  90. if err != nil {
  91. return nil, err
  92. }
  93. return conn, nil
  94. }
  95. func (na *NetAddress) DialTimeout(timeout time.Duration) (net.Conn, error) {
  96. conn, err := net.DialTimeout("tcp", na.String(), timeout)
  97. if err != nil {
  98. return nil, err
  99. }
  100. return conn, nil
  101. }
  102. func (na *NetAddress) Routable() bool {
  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) }