Path.hpp 7.1 KB

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