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.

233 lines
6.5 KiB

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