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.

329 lines
9.5 KiB

  1. // Modified for Tendermint
  2. // Originally Copyright (c) 2013-2014 Conformal Systems LLC.
  3. // https://github.com/conformal/btcd/blob/master/LICENSE
  4. package types
  5. import (
  6. "errors"
  7. "flag"
  8. "fmt"
  9. "net"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // EmptyNetAddress defines the string representation of an empty NetAddress
  15. const EmptyNetAddress = "<nil-NetAddress>"
  16. // NetAddress defines information about a peer on the network
  17. // including its ID, IP address, and port.
  18. type NetAddress struct {
  19. ID NodeID `json:"id"`
  20. IP net.IP `json:"ip"`
  21. Port uint16 `json:"port"`
  22. }
  23. // NewNetAddress returns a new NetAddress using the provided TCP
  24. // address. When testing, other net.Addr (except TCP) will result in
  25. // using 0.0.0.0:0. When normal run, other net.Addr (except TCP) will
  26. // panic. Panics if ID is invalid.
  27. // TODO: socks proxies?
  28. func NewNetAddress(id NodeID, addr net.Addr) *NetAddress {
  29. tcpAddr, ok := addr.(*net.TCPAddr)
  30. if !ok {
  31. if flag.Lookup("test.v") == nil { // normal run
  32. panic(fmt.Sprintf("Only TCPAddrs are supported. Got: %v", addr))
  33. } else { // in testing
  34. netAddr := NewNetAddressIPPort(net.IP("127.0.0.1"), 0)
  35. netAddr.ID = id
  36. return netAddr
  37. }
  38. }
  39. if err := id.Validate(); err != nil {
  40. panic(fmt.Sprintf("Invalid ID %v: %v (addr: %v)", id, err, addr))
  41. }
  42. ip := tcpAddr.IP
  43. port := uint16(tcpAddr.Port)
  44. na := NewNetAddressIPPort(ip, port)
  45. na.ID = id
  46. return na
  47. }
  48. // NewNetAddressIPPort returns a new NetAddress using the provided IP
  49. // and port number.
  50. func NewNetAddressIPPort(ip net.IP, port uint16) *NetAddress {
  51. return &NetAddress{
  52. IP: ip,
  53. Port: port,
  54. }
  55. }
  56. // NewNetAddressString returns a new NetAddress using the provided address in
  57. // the form of "ID@IP:Port".
  58. // Also resolves the host if host is not an IP.
  59. // Errors are of type ErrNetAddressXxx where Xxx is in (NoID, Invalid, Lookup)
  60. func NewNetAddressString(addr string) (*NetAddress, error) {
  61. addrWithoutProtocol := removeProtocolIfDefined(addr)
  62. spl := strings.Split(addrWithoutProtocol, "@")
  63. if len(spl) != 2 {
  64. return nil, ErrNetAddressNoID{addr}
  65. }
  66. id, err := NewNodeID(spl[0])
  67. if err != nil {
  68. return nil, ErrNetAddressInvalid{addrWithoutProtocol, err}
  69. }
  70. if err := id.Validate(); err != nil {
  71. return nil, ErrNetAddressInvalid{addrWithoutProtocol, err}
  72. }
  73. addrWithoutProtocol = 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. // Equals reports whether na and other are the same addresses,
  101. // including their ID, IP, and Port.
  102. func (na *NetAddress) Equals(other interface{}) bool {
  103. if o, ok := other.(*NetAddress); ok {
  104. return na.String() == o.String()
  105. }
  106. return false
  107. }
  108. // Same returns true is na has the same non-empty ID or DialString as other.
  109. func (na *NetAddress) Same(other interface{}) bool {
  110. if o, ok := other.(*NetAddress); ok {
  111. if na.DialString() == o.DialString() {
  112. return true
  113. }
  114. if na.ID != "" && na.ID == o.ID {
  115. return true
  116. }
  117. }
  118. return false
  119. }
  120. // String representation: <ID>@<IP>:<PORT>
  121. func (na *NetAddress) String() string {
  122. if na == nil {
  123. return EmptyNetAddress
  124. }
  125. addrStr := na.DialString()
  126. if na.ID != "" {
  127. addrStr = na.ID.AddressString(addrStr)
  128. }
  129. return addrStr
  130. }
  131. func (na *NetAddress) DialString() string {
  132. if na == nil {
  133. return "<nil-NetAddress>"
  134. }
  135. return net.JoinHostPort(
  136. na.IP.String(),
  137. strconv.FormatUint(uint64(na.Port), 10),
  138. )
  139. }
  140. // Dial calls net.Dial on the address.
  141. func (na *NetAddress) Dial() (net.Conn, error) {
  142. conn, err := net.Dial("tcp", na.DialString())
  143. if err != nil {
  144. return nil, err
  145. }
  146. return conn, nil
  147. }
  148. // DialTimeout calls net.DialTimeout on the address.
  149. func (na *NetAddress) DialTimeout(timeout time.Duration) (net.Conn, error) {
  150. conn, err := net.DialTimeout("tcp", na.DialString(), timeout)
  151. if err != nil {
  152. return nil, err
  153. }
  154. return conn, nil
  155. }
  156. // Routable returns true if the address is routable.
  157. func (na *NetAddress) Routable() bool {
  158. if err := na.Valid(); err != nil {
  159. return false
  160. }
  161. // TODO(oga) bitcoind doesn't include RFC3849 here, but should we?
  162. return !(na.RFC1918() || na.RFC3927() || na.RFC4862() ||
  163. na.RFC4193() || na.RFC4843() || na.Local())
  164. }
  165. // For IPv4 these are either a 0 or all bits set address. For IPv6 a zero
  166. // address or one that matches the RFC3849 documentation address format.
  167. func (na *NetAddress) Valid() error {
  168. if err := na.ID.Validate(); err != nil {
  169. return fmt.Errorf("invalid ID: %w", err)
  170. }
  171. if na.IP == nil {
  172. return errors.New("no IP")
  173. }
  174. if na.IP.IsUnspecified() || na.RFC3849() || na.IP.Equal(net.IPv4bcast) {
  175. return errors.New("invalid IP")
  176. }
  177. return nil
  178. }
  179. // Local returns true if it is a local address.
  180. func (na *NetAddress) Local() bool {
  181. return na.IP.IsLoopback() || zero4.Contains(na.IP)
  182. }
  183. // ReachabilityTo checks whenever o can be reached from na.
  184. func (na *NetAddress) ReachabilityTo(o *NetAddress) int {
  185. const (
  186. Unreachable = 0
  187. Default = iota
  188. Teredo
  189. Ipv6Weak
  190. Ipv4
  191. Ipv6Strong
  192. )
  193. switch {
  194. case !na.Routable():
  195. return Unreachable
  196. case na.RFC4380():
  197. switch {
  198. case !o.Routable():
  199. return Default
  200. case o.RFC4380():
  201. return Teredo
  202. case o.IP.To4() != nil:
  203. return Ipv4
  204. default: // ipv6
  205. return Ipv6Weak
  206. }
  207. case na.IP.To4() != nil:
  208. if o.Routable() && o.IP.To4() != nil {
  209. return Ipv4
  210. }
  211. return Default
  212. default: /* ipv6 */
  213. var tunneled bool
  214. // Is our v6 is tunneled?
  215. if o.RFC3964() || o.RFC6052() || o.RFC6145() {
  216. tunneled = true
  217. }
  218. switch {
  219. case !o.Routable():
  220. return Default
  221. case o.RFC4380():
  222. return Teredo
  223. case o.IP.To4() != nil:
  224. return Ipv4
  225. case tunneled:
  226. // only prioritize ipv6 if we aren't tunneling it.
  227. return Ipv6Weak
  228. }
  229. return Ipv6Strong
  230. }
  231. }
  232. // RFC1918: IPv4 Private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
  233. // RFC3849: IPv6 Documentation address (2001:0DB8::/32)
  234. // RFC3927: IPv4 Autoconfig (169.254.0.0/16)
  235. // RFC3964: IPv6 6to4 (2002::/16)
  236. // RFC4193: IPv6 unique local (FC00::/7)
  237. // RFC4380: IPv6 Teredo tunneling (2001::/32)
  238. // RFC4843: IPv6 ORCHID: (2001:10::/28)
  239. // RFC4862: IPv6 Autoconfig (FE80::/64)
  240. // RFC6052: IPv6 well known prefix (64:FF9B::/96)
  241. // RFC6145: IPv6 IPv4 translated address ::FFFF:0:0:0/96
  242. var rfc1918_10 = net.IPNet{IP: net.ParseIP("10.0.0.0"), Mask: net.CIDRMask(8, 32)}
  243. var rfc1918_192 = net.IPNet{IP: net.ParseIP("192.168.0.0"), Mask: net.CIDRMask(16, 32)}
  244. var rfc1918_172 = net.IPNet{IP: net.ParseIP("172.16.0.0"), Mask: net.CIDRMask(12, 32)}
  245. var rfc3849 = net.IPNet{IP: net.ParseIP("2001:0DB8::"), Mask: net.CIDRMask(32, 128)}
  246. var rfc3927 = net.IPNet{IP: net.ParseIP("169.254.0.0"), Mask: net.CIDRMask(16, 32)}
  247. var rfc3964 = net.IPNet{IP: net.ParseIP("2002::"), Mask: net.CIDRMask(16, 128)}
  248. var rfc4193 = net.IPNet{IP: net.ParseIP("FC00::"), Mask: net.CIDRMask(7, 128)}
  249. var rfc4380 = net.IPNet{IP: net.ParseIP("2001::"), Mask: net.CIDRMask(32, 128)}
  250. var rfc4843 = net.IPNet{IP: net.ParseIP("2001:10::"), Mask: net.CIDRMask(28, 128)}
  251. var rfc4862 = net.IPNet{IP: net.ParseIP("FE80::"), Mask: net.CIDRMask(64, 128)}
  252. var rfc6052 = net.IPNet{IP: net.ParseIP("64:FF9B::"), Mask: net.CIDRMask(96, 128)}
  253. var rfc6145 = net.IPNet{IP: net.ParseIP("::FFFF:0:0:0"), Mask: net.CIDRMask(96, 128)}
  254. var zero4 = net.IPNet{IP: net.ParseIP("0.0.0.0"), Mask: net.CIDRMask(8, 32)}
  255. var (
  256. // onionCatNet defines the IPv6 address block used to support Tor.
  257. // bitcoind encodes a .onion address as a 16 byte number by decoding the
  258. // address prior to the .onion (i.e. the key hash) base32 into a ten
  259. // byte number. It then stores the first 6 bytes of the address as
  260. // 0xfd, 0x87, 0xd8, 0x7e, 0xeb, 0x43.
  261. //
  262. // This is the same range used by OnionCat, which is part part of the
  263. // RFC4193 unique local IPv6 range.
  264. //
  265. // In summary the format is:
  266. // { magic 6 bytes, 10 bytes base32 decode of key hash }
  267. onionCatNet = ipNet("fd87:d87e:eb43::", 48, 128)
  268. )
  269. func (na *NetAddress) RFC1918() bool {
  270. return rfc1918_10.Contains(na.IP) ||
  271. rfc1918_192.Contains(na.IP) ||
  272. rfc1918_172.Contains(na.IP)
  273. }
  274. func (na *NetAddress) RFC3849() bool { return rfc3849.Contains(na.IP) }
  275. func (na *NetAddress) RFC3927() bool { return rfc3927.Contains(na.IP) }
  276. func (na *NetAddress) RFC3964() bool { return rfc3964.Contains(na.IP) }
  277. func (na *NetAddress) RFC4193() bool { return rfc4193.Contains(na.IP) }
  278. func (na *NetAddress) RFC4380() bool { return rfc4380.Contains(na.IP) }
  279. func (na *NetAddress) RFC4843() bool { return rfc4843.Contains(na.IP) }
  280. func (na *NetAddress) RFC4862() bool { return rfc4862.Contains(na.IP) }
  281. func (na *NetAddress) RFC6052() bool { return rfc6052.Contains(na.IP) }
  282. func (na *NetAddress) RFC6145() bool { return rfc6145.Contains(na.IP) }
  283. func (na *NetAddress) OnionCatTor() bool { return onionCatNet.Contains(na.IP) }
  284. func removeProtocolIfDefined(addr string) string {
  285. if strings.Contains(addr, "://") {
  286. return strings.Split(addr, "://")[1]
  287. }
  288. return addr
  289. }
  290. // ipNet returns a net.IPNet struct given the passed IP address string, number
  291. // of one bits to include at the start of the mask, and the total number of bits
  292. // for the mask.
  293. func ipNet(ip string, ones, bits int) net.IPNet {
  294. return net.IPNet{IP: net.ParseIP(ip), Mask: net.CIDRMask(ones, bits)}
  295. }