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.

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