Path.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #include "NonCopyable.hpp"
  29. /**
  30. * Maximum return value of preferenceRank()
  31. */
  32. #define ZT_PATH_MAX_PREFERENCE_RANK ((ZT_INETADDRESS_MAX_SCOPE << 1) | 1)
  33. namespace ZeroTier {
  34. class RuntimeEnvironment;
  35. /**
  36. * A path across the physical network
  37. */
  38. class Path : NonCopyable
  39. {
  40. friend class SharedPtr<Path>;
  41. public:
  42. /**
  43. * Efficient unique key for paths in a Hashtable
  44. */
  45. class HashKey
  46. {
  47. public:
  48. HashKey() {}
  49. HashKey(const InetAddress &l,const InetAddress &r)
  50. {
  51. // This is an ad-hoc bit packing algorithm to yield unique keys for
  52. // remote addresses and their local-side counterparts if defined.
  53. // Portability across runtimes is not needed.
  54. if (r.ss_family == AF_INET) {
  55. _k[0] = (uint64_t)reinterpret_cast<const struct sockaddr_in *>(&r)->sin_addr.s_addr;
  56. _k[1] = (uint64_t)reinterpret_cast<const struct sockaddr_in *>(&r)->sin_port;
  57. if (l.ss_family == AF_INET) {
  58. _k[2] = (uint64_t)reinterpret_cast<const struct sockaddr_in *>(&l)->sin_addr.s_addr;
  59. _k[3] = (uint64_t)reinterpret_cast<const struct sockaddr_in *>(&r)->sin_port;
  60. } else {
  61. _k[2] = 0;
  62. _k[3] = 0;
  63. }
  64. } else if (r.ss_family == AF_INET6) {
  65. const uint8_t *a = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&r)->sin6_addr.s6_addr);
  66. uint8_t *b = reinterpret_cast<uint8_t *>(_k);
  67. for(unsigned int i=0;i<16;++i) b[i] = a[i];
  68. _k[2] = ~((uint64_t)reinterpret_cast<const struct sockaddr_in6 *>(&r)->sin6_port);
  69. if (l.ss_family == AF_INET6) {
  70. _k[2] ^= ((uint64_t)reinterpret_cast<const struct sockaddr_in6 *>(&r)->sin6_port) << 32;
  71. a = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&l)->sin6_addr.s6_addr);
  72. b += 24;
  73. for(unsigned int i=0;i<8;++i) b[i] = a[i];
  74. a += 8;
  75. for(unsigned int i=0;i<8;++i) b[i] ^= a[i];
  76. }
  77. } else {
  78. _k[0] = 0;
  79. _k[1] = 0;
  80. _k[2] = 0;
  81. _k[3] = 0;
  82. }
  83. }
  84. inline unsigned long hashCode() const { return (unsigned long)(_k[0] + _k[1] + _k[2] + _k[3]); }
  85. 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]) ); }
  86. inline bool operator!=(const HashKey &k) const { return (!(*this == k)); }
  87. private:
  88. uint64_t _k[4];
  89. };
  90. Path() :
  91. _lastOut(0),
  92. _lastIn(0),
  93. _lastHello(0),
  94. _addr(),
  95. _localAddress(),
  96. _ipScope(InetAddress::IP_SCOPE_NONE)
  97. {
  98. }
  99. Path(const InetAddress &localAddress,const InetAddress &addr) :
  100. _lastOut(0),
  101. _lastIn(0),
  102. _lastHello(0),
  103. _addr(addr),
  104. _localAddress(localAddress),
  105. _ipScope(addr.ipScope())
  106. {
  107. }
  108. /**
  109. * Called when a packet is received from this remote path, regardless of content
  110. *
  111. * @param t Time of receive
  112. */
  113. inline void received(const uint64_t t) { _lastIn = t; }
  114. /**
  115. * Send a packet via this path (last out time is also updated)
  116. *
  117. * @param RR Runtime environment
  118. * @param data Packet data
  119. * @param len Packet length
  120. * @param now Current time
  121. * @return True if transport reported success
  122. */
  123. bool send(const RuntimeEnvironment *RR,const void *data,unsigned int len,uint64_t now);
  124. /**
  125. * Manually update last sent time
  126. *
  127. * @param t Time of send
  128. */
  129. inline void sent(const uint64_t t) { _lastOut = t; }
  130. /**
  131. * @return Address of local side of this path or NULL if unspecified
  132. */
  133. inline const InetAddress &localAddress() const { return _localAddress; }
  134. /**
  135. * @return Physical address
  136. */
  137. inline const InetAddress &address() const { return _addr; }
  138. /**
  139. * @return IP scope -- faster shortcut for address().ipScope()
  140. */
  141. inline InetAddress::IpScope ipScope() const { return _ipScope; }
  142. /**
  143. * @return Preference rank, higher == better
  144. */
  145. inline unsigned int preferenceRank() const
  146. {
  147. // This causes us to rank paths in order of IP scope rank (see InetAdddress.hpp) but
  148. // within each IP scope class to prefer IPv6 over IPv4.
  149. return ( ((unsigned int)_ipScope << 1) | (unsigned int)(_addr.ss_family == AF_INET6) );
  150. }
  151. /**
  152. * Check whether this address is valid for a ZeroTier path
  153. *
  154. * This checks the address type and scope against address types and scopes
  155. * that we currently support for ZeroTier communication.
  156. *
  157. * @param a Address to check
  158. * @return True if address is good for ZeroTier path use
  159. */
  160. static inline bool isAddressValidForPath(const InetAddress &a)
  161. {
  162. if ((a.ss_family == AF_INET)||(a.ss_family == AF_INET6)) {
  163. switch(a.ipScope()) {
  164. /* Note: we don't do link-local at the moment. Unfortunately these
  165. * cause several issues. The first is that they usually require a
  166. * device qualifier, which we don't handle yet and can't portably
  167. * push in PUSH_DIRECT_PATHS. The second is that some OSes assign
  168. * these very ephemerally or otherwise strangely. So we'll use
  169. * private, pseudo-private, shared (e.g. carrier grade NAT), or
  170. * global IP addresses. */
  171. case InetAddress::IP_SCOPE_PRIVATE:
  172. case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
  173. case InetAddress::IP_SCOPE_SHARED:
  174. case InetAddress::IP_SCOPE_GLOBAL:
  175. if (a.ss_family == AF_INET6) {
  176. // TEMPORARY HACK: for now, we are going to blacklist he.net IPv6
  177. // tunnels due to very spotty performance and low MTU issues over
  178. // these IPv6 tunnel links.
  179. const uint8_t *ipd = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_addr.s6_addr);
  180. if ((ipd[0] == 0x20)&&(ipd[1] == 0x01)&&(ipd[2] == 0x04)&&(ipd[3] == 0x70))
  181. return false;
  182. }
  183. return true;
  184. default:
  185. return false;
  186. }
  187. }
  188. return false;
  189. }
  190. /**
  191. * @return True if path appears alive
  192. */
  193. inline bool alive(const uint64_t now) const { return ((now - _lastIn) <= ZT_PATH_ALIVE_TIMEOUT); }
  194. /**
  195. * @return True if this path needs a heartbeat
  196. */
  197. inline bool needsHeartbeat(const uint64_t now) const { return ((now - _lastOut) >= ZT_PATH_HEARTBEAT_PERIOD); }
  198. /**
  199. * @return Last time we sent something
  200. */
  201. inline uint64_t lastOut() const { return _lastOut; }
  202. /**
  203. * @return Last time we received anything
  204. */
  205. inline uint64_t lastIn() const { return _lastIn; }
  206. /**
  207. * @return True if we should allow HELLO via this path
  208. */
  209. inline bool rateGateHello(const uint64_t now)
  210. {
  211. if ((now - _lastHello) >= ZT_PATH_HELLO_RATE_LIMIT) {
  212. _lastHello = now;
  213. return true;
  214. }
  215. return false;
  216. }
  217. private:
  218. uint64_t _lastOut;
  219. uint64_t _lastIn;
  220. uint64_t _lastHello;
  221. InetAddress _addr;
  222. InetAddress _localAddress;
  223. InetAddress::IpScope _ipScope; // memoize this since it's a computed value checked often
  224. AtomicCounter __refCount;
  225. };
  226. } // namespace ZeroTier
  227. #endif