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.

262 lines
7.5 KiB

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