Path.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. _addr(),
  94. _localAddress(),
  95. _ipScope(InetAddress::IP_SCOPE_NONE)
  96. {
  97. }
  98. Path(const InetAddress &localAddress,const InetAddress &addr) :
  99. _lastOut(0),
  100. _lastIn(0),
  101. _addr(addr),
  102. _localAddress(localAddress),
  103. _ipScope(addr.ipScope())
  104. {
  105. }
  106. /**
  107. * Called when a packet is received from this remote path, regardless of content
  108. *
  109. * @param t Time of receive
  110. */
  111. inline void received(const uint64_t t) { _lastIn = t; }
  112. /**
  113. * Send a packet via this path (last out time is also updated)
  114. *
  115. * @param RR Runtime environment
  116. * @param data Packet data
  117. * @param len Packet length
  118. * @param now Current time
  119. * @return True if transport reported success
  120. */
  121. bool send(const RuntimeEnvironment *RR,const void *data,unsigned int len,uint64_t now);
  122. /**
  123. * Manually update last sent time
  124. *
  125. * @param t Time of send
  126. */
  127. inline void sent(const uint64_t t) { _lastOut = t; }
  128. /**
  129. * @return Address of local side of this path or NULL if unspecified
  130. */
  131. inline const InetAddress &localAddress() const { return _localAddress; }
  132. /**
  133. * @return Physical address
  134. */
  135. inline const InetAddress &address() const { return _addr; }
  136. /**
  137. * @return IP scope -- faster shortcut for address().ipScope()
  138. */
  139. inline InetAddress::IpScope ipScope() const { return _ipScope; }
  140. /**
  141. * @return Preference rank, higher == better
  142. */
  143. inline unsigned int preferenceRank() const
  144. {
  145. // This causes us to rank paths in order of IP scope rank (see InetAdddress.hpp) but
  146. // within each IP scope class to prefer IPv6 over IPv4.
  147. return ( ((unsigned int)_ipScope << 1) | (unsigned int)(_addr.ss_family == AF_INET6) );
  148. }
  149. /**
  150. * Check whether this address is valid for a ZeroTier path
  151. *
  152. * This checks the address type and scope against address types and scopes
  153. * that we currently support for ZeroTier communication.
  154. *
  155. * @param a Address to check
  156. * @return True if address is good for ZeroTier path use
  157. */
  158. static inline bool isAddressValidForPath(const InetAddress &a)
  159. {
  160. if ((a.ss_family == AF_INET)||(a.ss_family == AF_INET6)) {
  161. switch(a.ipScope()) {
  162. /* Note: we don't do link-local at the moment. Unfortunately these
  163. * cause several issues. The first is that they usually require a
  164. * device qualifier, which we don't handle yet and can't portably
  165. * push in PUSH_DIRECT_PATHS. The second is that some OSes assign
  166. * these very ephemerally or otherwise strangely. So we'll use
  167. * private, pseudo-private, shared (e.g. carrier grade NAT), or
  168. * global IP addresses. */
  169. case InetAddress::IP_SCOPE_PRIVATE:
  170. case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
  171. case InetAddress::IP_SCOPE_SHARED:
  172. case InetAddress::IP_SCOPE_GLOBAL:
  173. if (a.ss_family == AF_INET6) {
  174. // TEMPORARY HACK: for now, we are going to blacklist he.net IPv6
  175. // tunnels due to very spotty performance and low MTU issues over
  176. // these IPv6 tunnel links.
  177. const uint8_t *ipd = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_addr.s6_addr);
  178. if ((ipd[0] == 0x20)&&(ipd[1] == 0x01)&&(ipd[2] == 0x04)&&(ipd[3] == 0x70))
  179. return false;
  180. }
  181. return true;
  182. default:
  183. return false;
  184. }
  185. }
  186. return false;
  187. }
  188. /**
  189. * @return True if path appears alive
  190. */
  191. inline bool alive(const uint64_t now) const { return ((now - _lastIn) <= ZT_PATH_ALIVE_TIMEOUT); }
  192. /**
  193. * @return True if this path needs a heartbeat
  194. */
  195. inline bool needsHeartbeat(const uint64_t now) const { return ((now - _lastOut) >= ZT_PATH_HEARTBEAT_PERIOD); }
  196. /**
  197. * @return Last time we sent something
  198. */
  199. inline uint64_t lastOut() const { return _lastOut; }
  200. /**
  201. * @return Last time we received anything
  202. */
  203. inline uint64_t lastIn() const { return _lastIn; }
  204. private:
  205. uint64_t _lastOut;
  206. uint64_t _lastIn;
  207. InetAddress _addr;
  208. InetAddress _localAddress;
  209. InetAddress::IpScope _ipScope; // memoize this since it's a computed value checked often
  210. AtomicCounter __refCount;
  211. };
  212. } // namespace ZeroTier
  213. #endif