Path.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_PATH_HPP
  28. #define ZT_PATH_HPP
  29. #include <stdint.h>
  30. #include <string.h>
  31. #include <stdexcept>
  32. #include <algorithm>
  33. #include "Constants.hpp"
  34. #include "InetAddress.hpp"
  35. /**
  36. * Flag indicating that this path is suboptimal
  37. *
  38. * This is used in cluster mode to indicate that the peer has been directed
  39. * to a better path. This path can continue to be used but shouldn't be kept
  40. * or advertised to other cluster members. Not used if clustering is not
  41. * built and enabled.
  42. */
  43. #define ZT_PATH_FLAG_CLUSTER_SUBOPTIMAL 0x0001
  44. namespace ZeroTier {
  45. class RuntimeEnvironment;
  46. /**
  47. * Base class for paths
  48. *
  49. * The base Path class is an immutable value.
  50. */
  51. class Path
  52. {
  53. public:
  54. Path() :
  55. _lastSend(0),
  56. _lastReceived(0),
  57. _addr(),
  58. _localAddress(),
  59. _flags(0),
  60. _ipScope(InetAddress::IP_SCOPE_NONE)
  61. {
  62. }
  63. Path(const InetAddress &localAddress,const InetAddress &addr) :
  64. _lastSend(0),
  65. _lastReceived(0),
  66. _addr(addr),
  67. _localAddress(localAddress),
  68. _flags(0),
  69. _ipScope(addr.ipScope())
  70. {
  71. }
  72. inline Path &operator=(const Path &p)
  73. {
  74. if (this != &p)
  75. memcpy(this,&p,sizeof(Path));
  76. return *this;
  77. }
  78. /**
  79. * Called when a packet is sent to this remote path
  80. *
  81. * This is called automatically by Path::send().
  82. *
  83. * @param t Time of send
  84. */
  85. inline void sent(uint64_t t) { _lastSend = t; }
  86. /**
  87. * Called when a packet is received from this remote path
  88. *
  89. * @param t Time of receive
  90. */
  91. inline void received(uint64_t t) { _lastReceived = t; }
  92. /**
  93. * @param now Current time
  94. * @return True if this path appears active
  95. */
  96. inline bool active(uint64_t now) const
  97. throw()
  98. {
  99. return ((now - _lastReceived) < ZT_PEER_ACTIVITY_TIMEOUT);
  100. }
  101. /**
  102. * Send a packet via this path
  103. *
  104. * @param RR Runtime environment
  105. * @param data Packet data
  106. * @param len Packet length
  107. * @param now Current time
  108. * @return True if transport reported success
  109. */
  110. bool send(const RuntimeEnvironment *RR,const void *data,unsigned int len,uint64_t now);
  111. /**
  112. * @return Address of local side of this path or NULL if unspecified
  113. */
  114. inline const InetAddress &localAddress() const throw() { return _localAddress; }
  115. /**
  116. * @return Time of last send to this path
  117. */
  118. inline uint64_t lastSend() const throw() { return _lastSend; }
  119. /**
  120. * @return Time of last receive from this path
  121. */
  122. inline uint64_t lastReceived() const throw() { return _lastReceived; }
  123. /**
  124. * @return Physical address
  125. */
  126. inline const InetAddress &address() const throw() { return _addr; }
  127. /**
  128. * @return IP scope -- faster shortcut for address().ipScope()
  129. */
  130. inline InetAddress::IpScope ipScope() const throw() { return _ipScope; }
  131. /**
  132. * @return Preference rank, higher == better
  133. */
  134. inline int preferenceRank() const throw()
  135. {
  136. // First, since the scope enum values in InetAddress.hpp are in order of
  137. // use preference rank, we take that. Then we multiple by two, yielding
  138. // a sequence like 0, 2, 4, 6, etc. Then if it's IPv6 we add one. This
  139. // makes IPv6 addresses of a given scope outrank IPv4 addresses of the
  140. // same scope -- e.g. 1 outranks 0. This makes us prefer IPv6, but not
  141. // if the address scope/class is of a fundamentally lower rank.
  142. return ( ((int)_ipScope * 2) + ((_addr.ss_family == AF_INET6) ? 1 : 0) );
  143. }
  144. /**
  145. * @return True if path is considered reliable (no NAT keepalives etc. are needed)
  146. */
  147. inline bool reliable() const throw()
  148. {
  149. if (_addr.ss_family == AF_INET)
  150. return ((_ipScope != InetAddress::IP_SCOPE_GLOBAL)&&(_ipScope != InetAddress::IP_SCOPE_PSEUDOPRIVATE));
  151. return true;
  152. }
  153. /**
  154. * @return True if address is non-NULL
  155. */
  156. inline operator bool() const throw() { return (_addr); }
  157. /**
  158. * Check whether this address is valid for a ZeroTier path
  159. *
  160. * This checks the address type and scope against address types and scopes
  161. * that we currently support for ZeroTier communication.
  162. *
  163. * @param a Address to check
  164. * @return True if address is good for ZeroTier path use
  165. */
  166. static inline bool isAddressValidForPath(const InetAddress &a)
  167. throw()
  168. {
  169. if ((a.ss_family == AF_INET)||(a.ss_family == AF_INET6)) {
  170. switch(a.ipScope()) {
  171. /* Note: we don't do link-local at the moment. Unfortunately these
  172. * cause several issues. The first is that they usually require a
  173. * device qualifier, which we don't handle yet and can't portably
  174. * push in PUSH_DIRECT_PATHS. The second is that some OSes assign
  175. * these very ephemerally or otherwise strangely. So we'll use
  176. * private, pseudo-private, shared (e.g. carrier grade NAT), or
  177. * global IP addresses. */
  178. case InetAddress::IP_SCOPE_PRIVATE:
  179. case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
  180. case InetAddress::IP_SCOPE_SHARED:
  181. case InetAddress::IP_SCOPE_GLOBAL:
  182. return true;
  183. default:
  184. return false;
  185. }
  186. }
  187. return false;
  188. }
  189. #ifdef ZT_ENABLE_CLUSTER
  190. /**
  191. * @param f New value of ZT_PATH_FLAG_CLUSTER_SUBOPTIMAL
  192. */
  193. inline void setClusterSuboptimal(bool f) { _flags = ((f) ? (_flags | ZT_PATH_FLAG_CLUSTER_SUBOPTIMAL) : (_flags & (~ZT_PATH_FLAG_CLUSTER_SUBOPTIMAL))); }
  194. /**
  195. * @return True if ZT_PATH_FLAG_CLUSTER_SUBOPTIMAL is set
  196. */
  197. inline bool isClusterSuboptimal() const { return ((_flags & ZT_PATH_FLAG_CLUSTER_SUBOPTIMAL) != 0); }
  198. #endif
  199. template<unsigned int C>
  200. inline void serialize(Buffer<C> &b) const
  201. {
  202. b.append((uint8_t)0); // version
  203. b.append((uint64_t)_lastSend);
  204. b.append((uint64_t)_lastReceived);
  205. _addr.serialize(b);
  206. _localAddress.serialize(b);
  207. b.append((uint16_t)_flags);
  208. }
  209. template<unsigned int C>
  210. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  211. {
  212. unsigned int p = startAt;
  213. if (b[p++] != 0)
  214. throw std::invalid_argument("invalid serialized Path");
  215. _lastSend = b.template at<uint64_t>(p); p += 8;
  216. _lastReceived = b.template at<uint64_t>(p); p += 8;
  217. p += _addr.deserialize(b,p);
  218. p += _localAddress.deserialize(b,p);
  219. _flags = b.template at<uint16_t>(p); p += 2;
  220. _ipScope = _addr.ipScope();
  221. return (p - startAt);
  222. }
  223. private:
  224. uint64_t _lastSend;
  225. uint64_t _lastReceived;
  226. InetAddress _addr;
  227. InetAddress _localAddress;
  228. unsigned int _flags;
  229. InetAddress::IpScope _ipScope; // memoize this since it's a computed value checked often
  230. };
  231. } // namespace ZeroTier
  232. #endif