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.

424 lines
12 KiB

9 years ago
9 years ago
9 years ago
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
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. "errors"
  8. "flag"
  9. "fmt"
  10. "net"
  11. "strconv"
  12. "strings"
  13. "time"
  14. tmp2p "github.com/tendermint/tendermint/proto/p2p"
  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 `json:"id"`
  20. IP net.IP `json:"ip"`
  21. Port uint16 `json:"port"`
  22. // TODO:
  23. // Name string `json:"name"` // optional DNS name
  24. // memoize .String()
  25. str string
  26. }
  27. // IDAddressString returns id@hostPort. It strips the leading
  28. // protocol from protocolHostPort if it exists.
  29. func IDAddressString(id ID, protocolHostPort string) string {
  30. hostPort := removeProtocolIfDefined(protocolHostPort)
  31. return fmt.Sprintf("%s@%s", id, hostPort)
  32. }
  33. // NewNetAddress returns a new NetAddress using the provided TCP
  34. // address. When testing, other net.Addr (except TCP) will result in
  35. // using 0.0.0.0:0. When normal run, other net.Addr (except TCP) will
  36. // panic. Panics if ID is invalid.
  37. // TODO: socks proxies?
  38. func NewNetAddress(id ID, addr net.Addr) *NetAddress {
  39. tcpAddr, ok := addr.(*net.TCPAddr)
  40. if !ok {
  41. if flag.Lookup("test.v") == nil { // normal run
  42. panic(fmt.Sprintf("Only TCPAddrs are supported. Got: %v", addr))
  43. } else { // in testing
  44. netAddr := NewNetAddressIPPort(net.IP("127.0.0.1"), 0)
  45. netAddr.ID = id
  46. return netAddr
  47. }
  48. }
  49. if err := validateID(id); err != nil {
  50. panic(fmt.Sprintf("Invalid ID %v: %v (addr: %v)", id, err, addr))
  51. }
  52. ip := tcpAddr.IP
  53. port := uint16(tcpAddr.Port)
  54. na := NewNetAddressIPPort(ip, port)
  55. na.ID = id
  56. return na
  57. }
  58. // NewNetAddressString returns a new NetAddress using the provided address in
  59. // the form of "ID@IP:Port".
  60. // Also resolves the host if host is not an IP.
  61. // Errors are of type ErrNetAddressXxx where Xxx is in (NoID, Invalid, Lookup)
  62. func NewNetAddressString(addr string) (*NetAddress, error) {
  63. addrWithoutProtocol := removeProtocolIfDefined(addr)
  64. spl := strings.Split(addrWithoutProtocol, "@")
  65. if len(spl) != 2 {
  66. return nil, ErrNetAddressNoID{addr}
  67. }
  68. // get ID
  69. if err := validateID(ID(spl[0])); err != nil {
  70. return nil, ErrNetAddressInvalid{addrWithoutProtocol, err}
  71. }
  72. var id ID
  73. id, addrWithoutProtocol = ID(spl[0]), spl[1]
  74. // get host and port
  75. host, portStr, err := net.SplitHostPort(addrWithoutProtocol)
  76. if err != nil {
  77. return nil, ErrNetAddressInvalid{addrWithoutProtocol, err}
  78. }
  79. if len(host) == 0 {
  80. return nil, ErrNetAddressInvalid{
  81. addrWithoutProtocol,
  82. errors.New("host is empty")}
  83. }
  84. ip := net.ParseIP(host)
  85. if ip == nil {
  86. ips, err := net.LookupIP(host)
  87. if err != nil {
  88. return nil, ErrNetAddressLookup{host, err}
  89. }
  90. ip = ips[0]
  91. }
  92. port, err := strconv.ParseUint(portStr, 10, 16)
  93. if err != nil {
  94. return nil, ErrNetAddressInvalid{portStr, err}
  95. }
  96. na := NewNetAddressIPPort(ip, uint16(port))
  97. na.ID = id
  98. return na, nil
  99. }
  100. // NewNetAddressStrings returns an array of NetAddress'es build using
  101. // the provided strings.
  102. func NewNetAddressStrings(addrs []string) ([]*NetAddress, []error) {
  103. netAddrs := make([]*NetAddress, 0)
  104. errs := make([]error, 0)
  105. for _, addr := range addrs {
  106. netAddr, err := NewNetAddressString(addr)
  107. if err != nil {
  108. errs = append(errs, err)
  109. } else {
  110. netAddrs = append(netAddrs, netAddr)
  111. }
  112. }
  113. return netAddrs, errs
  114. }
  115. // NewNetAddressIPPort returns a new NetAddress using the provided IP
  116. // and port number.
  117. func NewNetAddressIPPort(ip net.IP, port uint16) *NetAddress {
  118. return &NetAddress{
  119. IP: ip,
  120. Port: port,
  121. }
  122. }
  123. // NetAddressFromProto converts a Protobuf NetAddress into a native struct.
  124. func NetAddressFromProto(pb tmp2p.NetAddress) (*NetAddress, error) {
  125. ip := net.ParseIP(pb.IP)
  126. if ip == nil {
  127. return nil, fmt.Errorf("invalid IP address %v", pb.IP)
  128. }
  129. if pb.Port >= 1<<16 {
  130. return nil, fmt.Errorf("invalid port number %v", pb.Port)
  131. }
  132. return &NetAddress{
  133. ID: ID(pb.ID),
  134. IP: ip,
  135. Port: uint16(pb.Port),
  136. }, nil
  137. }
  138. // NetAddressesFromProto converts a slice of Protobuf NetAddresses into a native slice.
  139. func NetAddressesFromProto(pbs []tmp2p.NetAddress) ([]*NetAddress, error) {
  140. nas := make([]*NetAddress, 0, len(pbs))
  141. for _, pb := range pbs {
  142. na, err := NetAddressFromProto(pb)
  143. if err != nil {
  144. return nil, err
  145. }
  146. nas = append(nas, na)
  147. }
  148. return nas, nil
  149. }
  150. // NetAddressesToProto converts a slice of NetAddresses into a Protobuf slice.
  151. func NetAddressesToProto(nas []*NetAddress) []tmp2p.NetAddress {
  152. pbs := make([]tmp2p.NetAddress, 0, len(nas))
  153. for _, na := range nas {
  154. if na != nil {
  155. pbs = append(pbs, na.ToProto())
  156. }
  157. }
  158. return pbs
  159. }
  160. // ToProto converts a NetAddress to Protobuf.
  161. func (na *NetAddress) ToProto() tmp2p.NetAddress {
  162. return tmp2p.NetAddress{
  163. ID: string(na.ID),
  164. IP: na.IP.String(),
  165. Port: uint32(na.Port),
  166. }
  167. }
  168. // Equals reports whether na and other are the same addresses,
  169. // including their ID, IP, and Port.
  170. func (na *NetAddress) Equals(other interface{}) bool {
  171. if o, ok := other.(*NetAddress); ok {
  172. return na.String() == o.String()
  173. }
  174. return false
  175. }
  176. // Same returns true is na has the same non-empty ID or DialString as other.
  177. func (na *NetAddress) Same(other interface{}) bool {
  178. if o, ok := other.(*NetAddress); ok {
  179. if na.DialString() == o.DialString() {
  180. return true
  181. }
  182. if na.ID != "" && na.ID == o.ID {
  183. return true
  184. }
  185. }
  186. return false
  187. }
  188. // String representation: <ID>@<IP>:<PORT>
  189. func (na *NetAddress) String() string {
  190. if na == nil {
  191. return "<nil-NetAddress>"
  192. }
  193. if na.str == "" {
  194. addrStr := na.DialString()
  195. if na.ID != "" {
  196. addrStr = IDAddressString(na.ID, addrStr)
  197. }
  198. na.str = addrStr
  199. }
  200. return na.str
  201. }
  202. func (na *NetAddress) DialString() string {
  203. if na == nil {
  204. return "<nil-NetAddress>"
  205. }
  206. return net.JoinHostPort(
  207. na.IP.String(),
  208. strconv.FormatUint(uint64(na.Port), 10),
  209. )
  210. }
  211. // Dial calls net.Dial on the address.
  212. func (na *NetAddress) Dial() (net.Conn, error) {
  213. conn, err := net.Dial("tcp", na.DialString())
  214. if err != nil {
  215. return nil, err
  216. }
  217. return conn, nil
  218. }
  219. // DialTimeout calls net.DialTimeout on the address.
  220. func (na *NetAddress) DialTimeout(timeout time.Duration) (net.Conn, error) {
  221. conn, err := net.DialTimeout("tcp", na.DialString(), timeout)
  222. if err != nil {
  223. return nil, err
  224. }
  225. return conn, nil
  226. }
  227. // Routable returns true if the address is routable.
  228. func (na *NetAddress) Routable() bool {
  229. if err := na.Valid(); err != nil {
  230. return false
  231. }
  232. // TODO(oga) bitcoind doesn't include RFC3849 here, but should we?
  233. return !(na.RFC1918() || na.RFC3927() || na.RFC4862() ||
  234. na.RFC4193() || na.RFC4843() || na.Local())
  235. }
  236. // For IPv4 these are either a 0 or all bits set address. For IPv6 a zero
  237. // address or one that matches the RFC3849 documentation address format.
  238. func (na *NetAddress) Valid() error {
  239. if err := validateID(na.ID); err != nil {
  240. return fmt.Errorf("invalid ID: %w", err)
  241. }
  242. if na.IP == nil {
  243. return errors.New("no IP")
  244. }
  245. if na.IP.IsUnspecified() || na.RFC3849() || na.IP.Equal(net.IPv4bcast) {
  246. return errors.New("invalid IP")
  247. }
  248. return nil
  249. }
  250. // HasID returns true if the address has an ID.
  251. // NOTE: It does not check whether the ID is valid or not.
  252. func (na *NetAddress) HasID() bool {
  253. return string(na.ID) != ""
  254. }
  255. // Local returns true if it is a local address.
  256. func (na *NetAddress) Local() bool {
  257. return na.IP.IsLoopback() || zero4.Contains(na.IP)
  258. }
  259. // ReachabilityTo checks whenever o can be reached from na.
  260. func (na *NetAddress) ReachabilityTo(o *NetAddress) int {
  261. const (
  262. Unreachable = 0
  263. Default = iota
  264. Teredo
  265. Ipv6Weak
  266. Ipv4
  267. Ipv6Strong
  268. )
  269. switch {
  270. case !na.Routable():
  271. return Unreachable
  272. case na.RFC4380():
  273. switch {
  274. case !o.Routable():
  275. return Default
  276. case o.RFC4380():
  277. return Teredo
  278. case o.IP.To4() != nil:
  279. return Ipv4
  280. default: // ipv6
  281. return Ipv6Weak
  282. }
  283. case na.IP.To4() != nil:
  284. if o.Routable() && o.IP.To4() != nil {
  285. return Ipv4
  286. }
  287. return Default
  288. default: /* ipv6 */
  289. var tunnelled bool
  290. // Is our v6 is tunnelled?
  291. if o.RFC3964() || o.RFC6052() || o.RFC6145() {
  292. tunnelled = true
  293. }
  294. switch {
  295. case !o.Routable():
  296. return Default
  297. case o.RFC4380():
  298. return Teredo
  299. case o.IP.To4() != nil:
  300. return Ipv4
  301. case tunnelled:
  302. // only prioritise ipv6 if we aren't tunnelling it.
  303. return Ipv6Weak
  304. }
  305. return Ipv6Strong
  306. }
  307. }
  308. // RFC1918: IPv4 Private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
  309. // RFC3849: IPv6 Documentation address (2001:0DB8::/32)
  310. // RFC3927: IPv4 Autoconfig (169.254.0.0/16)
  311. // RFC3964: IPv6 6to4 (2002::/16)
  312. // RFC4193: IPv6 unique local (FC00::/7)
  313. // RFC4380: IPv6 Teredo tunneling (2001::/32)
  314. // RFC4843: IPv6 ORCHID: (2001:10::/28)
  315. // RFC4862: IPv6 Autoconfig (FE80::/64)
  316. // RFC6052: IPv6 well known prefix (64:FF9B::/96)
  317. // RFC6145: IPv6 IPv4 translated address ::FFFF:0:0:0/96
  318. var rfc1918_10 = net.IPNet{IP: net.ParseIP("10.0.0.0"), Mask: net.CIDRMask(8, 32)}
  319. var rfc1918_192 = net.IPNet{IP: net.ParseIP("192.168.0.0"), Mask: net.CIDRMask(16, 32)}
  320. var rfc1918_172 = net.IPNet{IP: net.ParseIP("172.16.0.0"), Mask: net.CIDRMask(12, 32)}
  321. var rfc3849 = net.IPNet{IP: net.ParseIP("2001:0DB8::"), Mask: net.CIDRMask(32, 128)}
  322. var rfc3927 = net.IPNet{IP: net.ParseIP("169.254.0.0"), Mask: net.CIDRMask(16, 32)}
  323. var rfc3964 = net.IPNet{IP: net.ParseIP("2002::"), Mask: net.CIDRMask(16, 128)}
  324. var rfc4193 = net.IPNet{IP: net.ParseIP("FC00::"), Mask: net.CIDRMask(7, 128)}
  325. var rfc4380 = net.IPNet{IP: net.ParseIP("2001::"), Mask: net.CIDRMask(32, 128)}
  326. var rfc4843 = net.IPNet{IP: net.ParseIP("2001:10::"), Mask: net.CIDRMask(28, 128)}
  327. var rfc4862 = net.IPNet{IP: net.ParseIP("FE80::"), Mask: net.CIDRMask(64, 128)}
  328. var rfc6052 = net.IPNet{IP: net.ParseIP("64:FF9B::"), Mask: net.CIDRMask(96, 128)}
  329. var rfc6145 = net.IPNet{IP: net.ParseIP("::FFFF:0:0:0"), Mask: net.CIDRMask(96, 128)}
  330. var zero4 = net.IPNet{IP: net.ParseIP("0.0.0.0"), Mask: net.CIDRMask(8, 32)}
  331. var (
  332. // onionCatNet defines the IPv6 address block used to support Tor.
  333. // bitcoind encodes a .onion address as a 16 byte number by decoding the
  334. // address prior to the .onion (i.e. the key hash) base32 into a ten
  335. // byte number. It then stores the first 6 bytes of the address as
  336. // 0xfd, 0x87, 0xd8, 0x7e, 0xeb, 0x43.
  337. //
  338. // This is the same range used by OnionCat, which is part part of the
  339. // RFC4193 unique local IPv6 range.
  340. //
  341. // In summary the format is:
  342. // { magic 6 bytes, 10 bytes base32 decode of key hash }
  343. onionCatNet = ipNet("fd87:d87e:eb43::", 48, 128)
  344. )
  345. // ipNet returns a net.IPNet struct given the passed IP address string, number
  346. // of one bits to include at the start of the mask, and the total number of bits
  347. // for the mask.
  348. func ipNet(ip string, ones, bits int) net.IPNet {
  349. return net.IPNet{IP: net.ParseIP(ip), Mask: net.CIDRMask(ones, bits)}
  350. }
  351. func (na *NetAddress) RFC1918() bool {
  352. return rfc1918_10.Contains(na.IP) ||
  353. rfc1918_192.Contains(na.IP) ||
  354. rfc1918_172.Contains(na.IP)
  355. }
  356. func (na *NetAddress) RFC3849() bool { return rfc3849.Contains(na.IP) }
  357. func (na *NetAddress) RFC3927() bool { return rfc3927.Contains(na.IP) }
  358. func (na *NetAddress) RFC3964() bool { return rfc3964.Contains(na.IP) }
  359. func (na *NetAddress) RFC4193() bool { return rfc4193.Contains(na.IP) }
  360. func (na *NetAddress) RFC4380() bool { return rfc4380.Contains(na.IP) }
  361. func (na *NetAddress) RFC4843() bool { return rfc4843.Contains(na.IP) }
  362. func (na *NetAddress) RFC4862() bool { return rfc4862.Contains(na.IP) }
  363. func (na *NetAddress) RFC6052() bool { return rfc6052.Contains(na.IP) }
  364. func (na *NetAddress) RFC6145() bool { return rfc6145.Contains(na.IP) }
  365. func (na *NetAddress) OnionCatTor() bool { return onionCatNet.Contains(na.IP) }
  366. func removeProtocolIfDefined(addr string) string {
  367. if strings.Contains(addr, "://") {
  368. return strings.Split(addr, "://")[1]
  369. }
  370. return addr
  371. }
  372. func validateID(id ID) error {
  373. if len(id) == 0 {
  374. return errors.New("no ID")
  375. }
  376. idBytes, err := hex.DecodeString(string(id))
  377. if err != nil {
  378. return err
  379. }
  380. if len(idBytes) != IDByteLength {
  381. return fmt.Errorf("invalid hex length - got %d, expected %d", len(idBytes), IDByteLength)
  382. }
  383. return nil
  384. }