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.

253 lines
7.3 KiB

9 years ago
9 years ago
7 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. "flag"
  7. "fmt"
  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, 0)
  67. errs := make([]error, 0)
  68. for _, addr := range addrs {
  69. netAddr, err := NewNetAddressString(addr)
  70. if err != nil {
  71. errs = append(errs, fmt.Errorf("Error in address %s: %v", addr, err))
  72. } else {
  73. netAddrs = append(netAddrs, netAddr)
  74. }
  75. }
  76. return netAddrs, errs
  77. }
  78. // NewNetAddressIPPort returns a new NetAddress using the provided IP
  79. // and port number.
  80. func NewNetAddressIPPort(ip net.IP, port uint16) *NetAddress {
  81. na := &NetAddress{
  82. IP: ip,
  83. Port: port,
  84. str: net.JoinHostPort(
  85. ip.String(),
  86. strconv.FormatUint(uint64(port), 10),
  87. ),
  88. }
  89. return na
  90. }
  91. // Equals reports whether na and other are the same addresses.
  92. func (na *NetAddress) Equals(other interface{}) bool {
  93. if o, ok := other.(*NetAddress); ok {
  94. return na.String() == o.String()
  95. }
  96. return false
  97. }
  98. func (na *NetAddress) Less(other interface{}) bool {
  99. if o, ok := other.(*NetAddress); ok {
  100. return na.String() < o.String()
  101. }
  102. cmn.PanicSanity("Cannot compare unequal types")
  103. return false
  104. }
  105. // String representation.
  106. func (na *NetAddress) String() string {
  107. if na.str == "" {
  108. na.str = net.JoinHostPort(
  109. na.IP.String(),
  110. strconv.FormatUint(uint64(na.Port), 10),
  111. )
  112. }
  113. return na.str
  114. }
  115. // Dial calls net.Dial on the address.
  116. func (na *NetAddress) Dial() (net.Conn, error) {
  117. conn, err := net.Dial("tcp", na.String())
  118. if err != nil {
  119. return nil, err
  120. }
  121. return conn, nil
  122. }
  123. // DialTimeout calls net.DialTimeout on the address.
  124. func (na *NetAddress) DialTimeout(timeout time.Duration) (net.Conn, error) {
  125. conn, err := net.DialTimeout("tcp", na.String(), timeout)
  126. if err != nil {
  127. return nil, err
  128. }
  129. return conn, nil
  130. }
  131. // Routable returns true if the address is routable.
  132. func (na *NetAddress) Routable() bool {
  133. // TODO(oga) bitcoind doesn't include RFC3849 here, but should we?
  134. return na.Valid() && !(na.RFC1918() || na.RFC3927() || na.RFC4862() ||
  135. na.RFC4193() || na.RFC4843() || na.Local())
  136. }
  137. // For IPv4 these are either a 0 or all bits set address. For IPv6 a zero
  138. // address or one that matches the RFC3849 documentation address format.
  139. func (na *NetAddress) Valid() bool {
  140. return na.IP != nil && !(na.IP.IsUnspecified() || na.RFC3849() ||
  141. na.IP.Equal(net.IPv4bcast))
  142. }
  143. // Local returns true if it is a local address.
  144. func (na *NetAddress) Local() bool {
  145. return na.IP.IsLoopback() || zero4.Contains(na.IP)
  146. }
  147. // ReachabilityTo checks whenever o can be reached from na.
  148. func (na *NetAddress) ReachabilityTo(o *NetAddress) int {
  149. const (
  150. Unreachable = 0
  151. Default = iota
  152. Teredo
  153. Ipv6_weak
  154. Ipv4
  155. Ipv6_strong
  156. )
  157. if !na.Routable() {
  158. return Unreachable
  159. } else if na.RFC4380() {
  160. if !o.Routable() {
  161. return Default
  162. } else if o.RFC4380() {
  163. return Teredo
  164. } else if o.IP.To4() != nil {
  165. return Ipv4
  166. } else { // ipv6
  167. return Ipv6_weak
  168. }
  169. } else if na.IP.To4() != nil {
  170. if o.Routable() && o.IP.To4() != nil {
  171. return Ipv4
  172. }
  173. return Default
  174. } else /* ipv6 */ {
  175. var tunnelled bool
  176. // Is our v6 is tunnelled?
  177. if o.RFC3964() || o.RFC6052() || o.RFC6145() {
  178. tunnelled = true
  179. }
  180. if !o.Routable() {
  181. return Default
  182. } else if o.RFC4380() {
  183. return Teredo
  184. } else if o.IP.To4() != nil {
  185. return Ipv4
  186. } else if tunnelled {
  187. // only prioritise ipv6 if we aren't tunnelling it.
  188. return Ipv6_weak
  189. }
  190. return Ipv6_strong
  191. }
  192. }
  193. // RFC1918: IPv4 Private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
  194. // RFC3849: IPv6 Documentation address (2001:0DB8::/32)
  195. // RFC3927: IPv4 Autoconfig (169.254.0.0/16)
  196. // RFC3964: IPv6 6to4 (2002::/16)
  197. // RFC4193: IPv6 unique local (FC00::/7)
  198. // RFC4380: IPv6 Teredo tunneling (2001::/32)
  199. // RFC4843: IPv6 ORCHID: (2001:10::/28)
  200. // RFC4862: IPv6 Autoconfig (FE80::/64)
  201. // RFC6052: IPv6 well known prefix (64:FF9B::/96)
  202. // RFC6145: IPv6 IPv4 translated address ::FFFF:0:0:0/96
  203. var rfc1918_10 = net.IPNet{IP: net.ParseIP("10.0.0.0"), Mask: net.CIDRMask(8, 32)}
  204. var rfc1918_192 = net.IPNet{IP: net.ParseIP("192.168.0.0"), Mask: net.CIDRMask(16, 32)}
  205. var rfc1918_172 = net.IPNet{IP: net.ParseIP("172.16.0.0"), Mask: net.CIDRMask(12, 32)}
  206. var rfc3849 = net.IPNet{IP: net.ParseIP("2001:0DB8::"), Mask: net.CIDRMask(32, 128)}
  207. var rfc3927 = net.IPNet{IP: net.ParseIP("169.254.0.0"), Mask: net.CIDRMask(16, 32)}
  208. var rfc3964 = net.IPNet{IP: net.ParseIP("2002::"), Mask: net.CIDRMask(16, 128)}
  209. var rfc4193 = net.IPNet{IP: net.ParseIP("FC00::"), Mask: net.CIDRMask(7, 128)}
  210. var rfc4380 = net.IPNet{IP: net.ParseIP("2001::"), Mask: net.CIDRMask(32, 128)}
  211. var rfc4843 = net.IPNet{IP: net.ParseIP("2001:10::"), Mask: net.CIDRMask(28, 128)}
  212. var rfc4862 = net.IPNet{IP: net.ParseIP("FE80::"), Mask: net.CIDRMask(64, 128)}
  213. var rfc6052 = net.IPNet{IP: net.ParseIP("64:FF9B::"), Mask: net.CIDRMask(96, 128)}
  214. var rfc6145 = net.IPNet{IP: net.ParseIP("::FFFF:0:0:0"), Mask: net.CIDRMask(96, 128)}
  215. var zero4 = net.IPNet{IP: net.ParseIP("0.0.0.0"), Mask: net.CIDRMask(8, 32)}
  216. func (na *NetAddress) RFC1918() bool {
  217. return rfc1918_10.Contains(na.IP) ||
  218. rfc1918_192.Contains(na.IP) ||
  219. rfc1918_172.Contains(na.IP)
  220. }
  221. func (na *NetAddress) RFC3849() bool { return rfc3849.Contains(na.IP) }
  222. func (na *NetAddress) RFC3927() bool { return rfc3927.Contains(na.IP) }
  223. func (na *NetAddress) RFC3964() bool { return rfc3964.Contains(na.IP) }
  224. func (na *NetAddress) RFC4193() bool { return rfc4193.Contains(na.IP) }
  225. func (na *NetAddress) RFC4380() bool { return rfc4380.Contains(na.IP) }
  226. func (na *NetAddress) RFC4843() bool { return rfc4843.Contains(na.IP) }
  227. func (na *NetAddress) RFC4862() bool { return rfc4862.Contains(na.IP) }
  228. func (na *NetAddress) RFC6052() bool { return rfc6052.Contains(na.IP) }
  229. func (na *NetAddress) RFC6145() bool { return rfc6145.Contains(na.IP) }