Path.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef ZT_PATH_HPP
  19. #define ZT_PATH_HPP
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include <stdexcept>
  23. #include <algorithm>
  24. #include "Constants.hpp"
  25. #include "InetAddress.hpp"
  26. #include "SharedPtr.hpp"
  27. #include "AtomicCounter.hpp"
  28. /**
  29. * Maximum return value of preferenceRank()
  30. */
  31. #define ZT_PATH_MAX_PREFERENCE_RANK ((ZT_INETADDRESS_MAX_SCOPE << 1) | 1)
  32. namespace ZeroTier {
  33. class RuntimeEnvironment;
  34. /**
  35. * A path across the physical network
  36. */
  37. class Path
  38. {
  39. friend class SharedPtr<Path>;
  40. public:
  41. /**
  42. * Efficient unique key for paths in a Hashtable
  43. */
  44. class HashKey
  45. {
  46. public:
  47. HashKey() {}
  48. HashKey(const InetAddress &l,const InetAddress &r)
  49. {
  50. // This is an ad-hoc bit packing algorithm to yield unique keys for
  51. // remote addresses and their local-side counterparts if defined.
  52. // Portability across runtimes is not needed.
  53. if (r.ss_family == AF_INET) {
  54. _k[0] = (uint64_t)reinterpret_cast<const struct sockaddr_in *>(&r)->sin_addr.s_addr;
  55. _k[1] = (uint64_t)reinterpret_cast<const struct sockaddr_in *>(&r)->sin_port;
  56. if (l.ss_family == AF_INET) {
  57. _k[2] = (uint64_t)reinterpret_cast<const struct sockaddr_in *>(&l)->sin_addr.s_addr;
  58. _k[3] = (uint64_t)reinterpret_cast<const struct sockaddr_in *>(&r)->sin_port;
  59. } else {
  60. _k[2] = 0;
  61. _k[3] = 0;
  62. }
  63. } else if (r.ss_family == AF_INET6) {
  64. const uint8_t *a = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&r)->sin6_addr.s6_addr);
  65. uint8_t *b = reinterpret_cast<uint8_t *>(_k);
  66. for(unsigned int i=0;i<16;++i) b[i] = a[i];
  67. _k[2] = ~((uint64_t)reinterpret_cast<const struct sockaddr_in6 *>(&r)->sin6_port);
  68. if (l.ss_family == AF_INET6) {
  69. _k[2] ^= ((uint64_t)reinterpret_cast<const struct sockaddr_in6 *>(&r)->sin6_port) << 32;
  70. a = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&l)->sin6_addr.s6_addr);
  71. b += 24;
  72. for(unsigned int i=0;i<8;++i) b[i] = a[i];
  73. a += 8;
  74. for(unsigned int i=0;i<8;++i) b[i] ^= a[i];
  75. }
  76. } else {
  77. _k[0] = 0;
  78. _k[1] = 0;
  79. _k[2] = 0;
  80. _k[3] = 0;
  81. }
  82. }
  83. inline unsigned long hashCode() const { return (unsigned long)(_k[0] + _k[1] + _k[2] + _k[3]); }
  84. inline bool operator==(const HashKey &k) const { return ( (_k[0] == k._k[0]) && (_k[1] == k._k[1]) && (_k[2] == k._k[2]) && (_k[3] == k._k[3]) ); }
  85. inline bool operator!=(const HashKey &k) const { return (!(*this == k)); }
  86. private:
  87. uint64_t _k[4];
  88. };
  89. Path() :
  90. _lastOut(0),
  91. _lastIn(0),
  92. _addr(),
  93. _localAddress(),
  94. _ipScope(InetAddress::IP_SCOPE_NONE)
  95. {
  96. }
  97. Path(const InetAddress &localAddress,const InetAddress &addr) :
  98. _lastOut(0),
  99. _lastIn(0),
  100. _addr(addr),
  101. _localAddress(localAddress),
  102. _ipScope(addr.ipScope())
  103. {
  104. }
  105. /**
  106. * Called when a packet is received from this remote path, regardless of content
  107. *
  108. * @param t Time of receive
  109. */
  110. inline void received(const uint64_t t) { _lastIn = t; }
  111. /**
  112. * Send a packet via this path (last out time is also updated)
  113. *
  114. * @param RR Runtime environment
  115. * @param data Packet data
  116. * @param len Packet length
  117. * @param now Current time
  118. * @return True if transport reported success
  119. */
  120. bool send(const RuntimeEnvironment *RR,const void *data,unsigned int len,uint64_t now);
  121. /**
  122. * @return Address of local side of this path or NULL if unspecified
  123. */
  124. inline const InetAddress &localAddress() const { return _localAddress; }
  125. /**
  126. * @return Physical address
  127. */
  128. inline const InetAddress &address() const { return _addr; }
  129. /**
  130. * @return IP scope -- faster shortcut for address().ipScope()
  131. */
  132. inline InetAddress::IpScope ipScope() const { return _ipScope; }
  133. /**
  134. * @return Preference rank, higher == better (will be less than 255)
  135. */
  136. inline unsigned int preferenceRank() const
  137. {
  138. /* First, since the scope enum values in InetAddress.hpp are in order of
  139. * use preference rank, we take that. Then we multiple by two, yielding
  140. * a sequence like 0, 2, 4, 6, etc. Then if it's IPv6 we add one. This
  141. * makes IPv6 addresses of a given scope outrank IPv4 addresses of the
  142. * same scope -- e.g. 1 outranks 0. This makes us prefer IPv6, but not
  143. * if the address scope/class is of a fundamentally lower rank. */
  144. return ( ((unsigned int)_ipScope << 1) | (unsigned int)(_addr.ss_family == AF_INET6) );
  145. }
  146. /**
  147. * @return This path's overall quality score (higher is better)
  148. */
  149. inline uint64_t score() const
  150. {
  151. return (_lastIn + (preferenceRank() * (ZT_PEER_PING_PERIOD / ZT_PATH_MAX_PREFERENCE_RANK)));
  152. }
  153. /**
  154. * Check whether this address is valid for a ZeroTier path
  155. *
  156. * This checks the address type and scope against address types and scopes
  157. * that we currently support for ZeroTier communication.
  158. *
  159. * @param a Address to check
  160. * @return True if address is good for ZeroTier path use
  161. */
  162. static inline bool isAddressValidForPath(const InetAddress &a)
  163. {
  164. if ((a.ss_family == AF_INET)||(a.ss_family == AF_INET6)) {
  165. switch(a.ipScope()) {
  166. /* Note: we don't do link-local at the moment. Unfortunately these
  167. * cause several issues. The first is that they usually require a
  168. * device qualifier, which we don't handle yet and can't portably
  169. * push in PUSH_DIRECT_PATHS. The second is that some OSes assign
  170. * these very ephemerally or otherwise strangely. So we'll use
  171. * private, pseudo-private, shared (e.g. carrier grade NAT), or
  172. * global IP addresses. */
  173. case InetAddress::IP_SCOPE_PRIVATE:
  174. case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
  175. case InetAddress::IP_SCOPE_SHARED:
  176. case InetAddress::IP_SCOPE_GLOBAL:
  177. if (a.ss_family == AF_INET6) {
  178. // TEMPORARY HACK: for now, we are going to blacklist he.net IPv6
  179. // tunnels due to very spotty performance and low MTU issues over
  180. // these IPv6 tunnel links.
  181. const uint8_t *ipd = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_addr.s6_addr);
  182. if ((ipd[0] == 0x20)&&(ipd[1] == 0x01)&&(ipd[2] == 0x04)&&(ipd[3] == 0x70))
  183. return false;
  184. }
  185. return true;
  186. default:
  187. return false;
  188. }
  189. }
  190. return false;
  191. }
  192. /**
  193. * @return True if path appears alive
  194. */
  195. inline bool alive(const uint64_t now) const { return ((now - _lastIn) <= ZT_PATH_ALIVE_TIMEOUT); }
  196. /**
  197. * @return True if this path needs a heartbeat
  198. */
  199. inline bool needsHeartbeat(const uint64_t now) const { return ((now - _lastOut) > ZT_PATH_HEARTBEAT_PERIOD); }
  200. /**
  201. * @return Last time we sent something
  202. */
  203. inline uint64_t lastOut() const { return _lastOut; }
  204. /**
  205. * @return Last time we received anything
  206. */
  207. inline uint64_t lastIn() const { return _lastIn; }
  208. private:
  209. uint64_t _lastOut;
  210. uint64_t _lastIn;
  211. InetAddress _addr;
  212. InetAddress _localAddress;
  213. InetAddress::IpScope _ipScope; // memoize this since it's a computed value checked often
  214. AtomicCounter __refCount;
  215. };
  216. } // namespace ZeroTier
  217. #endif