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.

252 lines
7.3 KiB

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