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.

300 lines
8.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
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. // including their ID, IP, and Port.
  117. func (na *NetAddress) Equals(other interface{}) bool {
  118. if o, ok := other.(*NetAddress); ok {
  119. return na.String() == o.String()
  120. }
  121. return false
  122. }
  123. // Same returns true is na has the same non-empty ID or DialString as other.
  124. func (na *NetAddress) Same(other interface{}) bool {
  125. if o, ok := other.(*NetAddress); ok {
  126. if na.DialString() == o.DialString() {
  127. return true
  128. }
  129. if na.ID != "" && na.ID == o.ID {
  130. return true
  131. }
  132. }
  133. return false
  134. }
  135. // String representation: <ID>@<IP>:<PORT>
  136. func (na *NetAddress) String() string {
  137. if na.str == "" {
  138. addrStr := na.DialString()
  139. if na.ID != "" {
  140. addrStr = IDAddressString(na.ID, addrStr)
  141. }
  142. na.str = addrStr
  143. }
  144. return na.str
  145. }
  146. func (na *NetAddress) DialString() string {
  147. return net.JoinHostPort(
  148. na.IP.String(),
  149. strconv.FormatUint(uint64(na.Port), 10),
  150. )
  151. }
  152. // Dial calls net.Dial on the address.
  153. func (na *NetAddress) Dial() (net.Conn, error) {
  154. conn, err := net.Dial("tcp", na.DialString())
  155. if err != nil {
  156. return nil, err
  157. }
  158. return conn, nil
  159. }
  160. // DialTimeout calls net.DialTimeout on the address.
  161. func (na *NetAddress) DialTimeout(timeout time.Duration) (net.Conn, error) {
  162. conn, err := net.DialTimeout("tcp", na.DialString(), timeout)
  163. if err != nil {
  164. return nil, err
  165. }
  166. return conn, nil
  167. }
  168. // Routable returns true if the address is routable.
  169. func (na *NetAddress) Routable() bool {
  170. // TODO(oga) bitcoind doesn't include RFC3849 here, but should we?
  171. return na.Valid() && !(na.RFC1918() || na.RFC3927() || na.RFC4862() ||
  172. na.RFC4193() || na.RFC4843() || na.Local())
  173. }
  174. // For IPv4 these are either a 0 or all bits set address. For IPv6 a zero
  175. // address or one that matches the RFC3849 documentation address format.
  176. func (na *NetAddress) Valid() bool {
  177. return na.IP != nil && !(na.IP.IsUnspecified() || na.RFC3849() ||
  178. na.IP.Equal(net.IPv4bcast))
  179. }
  180. // Local returns true if it is a local address.
  181. func (na *NetAddress) Local() bool {
  182. return na.IP.IsLoopback() || zero4.Contains(na.IP)
  183. }
  184. // ReachabilityTo checks whenever o can be reached from na.
  185. func (na *NetAddress) ReachabilityTo(o *NetAddress) int {
  186. const (
  187. Unreachable = 0
  188. Default = iota
  189. Teredo
  190. Ipv6_weak
  191. Ipv4
  192. Ipv6_strong
  193. )
  194. if !na.Routable() {
  195. return Unreachable
  196. } else if na.RFC4380() {
  197. if !o.Routable() {
  198. return Default
  199. } else if o.RFC4380() {
  200. return Teredo
  201. } else if o.IP.To4() != nil {
  202. return Ipv4
  203. } else { // ipv6
  204. return Ipv6_weak
  205. }
  206. } else if na.IP.To4() != nil {
  207. if o.Routable() && o.IP.To4() != nil {
  208. return Ipv4
  209. }
  210. return Default
  211. } else /* ipv6 */ {
  212. var tunnelled bool
  213. // Is our v6 is tunnelled?
  214. if o.RFC3964() || o.RFC6052() || o.RFC6145() {
  215. tunnelled = true
  216. }
  217. if !o.Routable() {
  218. return Default
  219. } else if o.RFC4380() {
  220. return Teredo
  221. } else if o.IP.To4() != nil {
  222. return Ipv4
  223. } else if tunnelled {
  224. // only prioritise ipv6 if we aren't tunnelling it.
  225. return Ipv6_weak
  226. }
  227. return Ipv6_strong
  228. }
  229. }
  230. // RFC1918: IPv4 Private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
  231. // RFC3849: IPv6 Documentation address (2001:0DB8::/32)
  232. // RFC3927: IPv4 Autoconfig (169.254.0.0/16)
  233. // RFC3964: IPv6 6to4 (2002::/16)
  234. // RFC4193: IPv6 unique local (FC00::/7)
  235. // RFC4380: IPv6 Teredo tunneling (2001::/32)
  236. // RFC4843: IPv6 ORCHID: (2001:10::/28)
  237. // RFC4862: IPv6 Autoconfig (FE80::/64)
  238. // RFC6052: IPv6 well known prefix (64:FF9B::/96)
  239. // RFC6145: IPv6 IPv4 translated address ::FFFF:0:0:0/96
  240. var rfc1918_10 = net.IPNet{IP: net.ParseIP("10.0.0.0"), Mask: net.CIDRMask(8, 32)}
  241. var rfc1918_192 = net.IPNet{IP: net.ParseIP("192.168.0.0"), Mask: net.CIDRMask(16, 32)}
  242. var rfc1918_172 = net.IPNet{IP: net.ParseIP("172.16.0.0"), Mask: net.CIDRMask(12, 32)}
  243. var rfc3849 = net.IPNet{IP: net.ParseIP("2001:0DB8::"), Mask: net.CIDRMask(32, 128)}
  244. var rfc3927 = net.IPNet{IP: net.ParseIP("169.254.0.0"), Mask: net.CIDRMask(16, 32)}
  245. var rfc3964 = net.IPNet{IP: net.ParseIP("2002::"), Mask: net.CIDRMask(16, 128)}
  246. var rfc4193 = net.IPNet{IP: net.ParseIP("FC00::"), Mask: net.CIDRMask(7, 128)}
  247. var rfc4380 = net.IPNet{IP: net.ParseIP("2001::"), Mask: net.CIDRMask(32, 128)}
  248. var rfc4843 = net.IPNet{IP: net.ParseIP("2001:10::"), Mask: net.CIDRMask(28, 128)}
  249. var rfc4862 = net.IPNet{IP: net.ParseIP("FE80::"), Mask: net.CIDRMask(64, 128)}
  250. var rfc6052 = net.IPNet{IP: net.ParseIP("64:FF9B::"), Mask: net.CIDRMask(96, 128)}
  251. var rfc6145 = net.IPNet{IP: net.ParseIP("::FFFF:0:0:0"), Mask: net.CIDRMask(96, 128)}
  252. var zero4 = net.IPNet{IP: net.ParseIP("0.0.0.0"), Mask: net.CIDRMask(8, 32)}
  253. func (na *NetAddress) RFC1918() bool {
  254. return rfc1918_10.Contains(na.IP) ||
  255. rfc1918_192.Contains(na.IP) ||
  256. rfc1918_172.Contains(na.IP)
  257. }
  258. func (na *NetAddress) RFC3849() bool { return rfc3849.Contains(na.IP) }
  259. func (na *NetAddress) RFC3927() bool { return rfc3927.Contains(na.IP) }
  260. func (na *NetAddress) RFC3964() bool { return rfc3964.Contains(na.IP) }
  261. func (na *NetAddress) RFC4193() bool { return rfc4193.Contains(na.IP) }
  262. func (na *NetAddress) RFC4380() bool { return rfc4380.Contains(na.IP) }
  263. func (na *NetAddress) RFC4843() bool { return rfc4843.Contains(na.IP) }
  264. func (na *NetAddress) RFC4862() bool { return rfc4862.Contains(na.IP) }
  265. func (na *NetAddress) RFC6052() bool { return rfc6052.Contains(na.IP) }
  266. func (na *NetAddress) RFC6145() bool { return rfc6145.Contains(na.IP) }
  267. func removeProtocolIfDefined(addr string) string {
  268. if strings.Contains(addr, "://") {
  269. return strings.Split(addr, "://")[1]
  270. } else {
  271. return addr
  272. }
  273. }