Path.hpp 6.9 KB

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