Path.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. namespace ZeroTier {
  36. class RuntimeEnvironment;
  37. /**
  38. * Base class for paths
  39. *
  40. * The base Path class is an immutable value.
  41. */
  42. class Path
  43. {
  44. public:
  45. Path() :
  46. _lastSend(0),
  47. _lastReceived(0),
  48. _addr(),
  49. _localAddress(),
  50. _ipScope(InetAddress::IP_SCOPE_NONE)
  51. {
  52. }
  53. Path(const InetAddress &localAddress,const InetAddress &addr) :
  54. _lastSend(0),
  55. _lastReceived(0),
  56. _addr(addr),
  57. _localAddress(localAddress),
  58. _ipScope(addr.ipScope())
  59. {
  60. }
  61. inline Path &operator=(const Path &p)
  62. throw()
  63. {
  64. if (this != &p)
  65. memcpy(this,&p,sizeof(Path));
  66. return *this;
  67. }
  68. /**
  69. * Called when a packet is sent to this remote path
  70. *
  71. * This is called automatically by Path::send().
  72. *
  73. * @param t Time of send
  74. */
  75. inline void sent(uint64_t t)
  76. throw()
  77. {
  78. _lastSend = t;
  79. }
  80. /**
  81. * Called when a packet is received from this remote path
  82. *
  83. * @param t Time of receive
  84. */
  85. inline void received(uint64_t t)
  86. throw()
  87. {
  88. _lastReceived = t;
  89. }
  90. /**
  91. * @param now Current time
  92. * @return True if this path appears active
  93. */
  94. inline bool active(uint64_t now) const
  95. throw()
  96. {
  97. return ((now - _lastReceived) < ZT_PEER_ACTIVITY_TIMEOUT);
  98. }
  99. /**
  100. * Send a packet via this path
  101. *
  102. * @param RR Runtime environment
  103. * @param data Packet data
  104. * @param len Packet length
  105. * @param now Current time
  106. * @return True if transport reported success
  107. */
  108. bool send(const RuntimeEnvironment *RR,const void *data,unsigned int len,uint64_t now);
  109. /**
  110. * @return Address of local side of this path or NULL if unspecified
  111. */
  112. inline const InetAddress &localAddress() const throw() { return _localAddress; }
  113. /**
  114. * @return Time of last send to this path
  115. */
  116. inline uint64_t lastSend() const throw() { return _lastSend; }
  117. /**
  118. * @return Time of last receive from this path
  119. */
  120. inline uint64_t lastReceived() const throw() { return _lastReceived; }
  121. /**
  122. * @return Physical address
  123. */
  124. inline const InetAddress &address() const throw() { return _addr; }
  125. /**
  126. * @return IP scope -- faster shortcut for address().ipScope()
  127. */
  128. inline InetAddress::IpScope ipScope() const throw() { return _ipScope; }
  129. /**
  130. * @return Preference rank, higher == better
  131. */
  132. inline int preferenceRank() const throw()
  133. {
  134. // First, since the scope enum values in InetAddress.hpp are in order of
  135. // use preference rank, we take that. Then we multiple by two, yielding
  136. // a sequence like 0, 2, 4, 6, etc. Then if it's IPv6 we add one. This
  137. // makes IPv6 addresses of a given scope outrank IPv4 addresses of the
  138. // same scope -- e.g. 1 outranks 0. This makes us prefer IPv6, but not
  139. // if the address scope/class is of a fundamentally lower rank.
  140. return ( ((int)_ipScope * 2) + ((_addr.ss_family == AF_INET6) ? 1 : 0) );
  141. }
  142. /**
  143. * @return True if path is considered reliable (no NAT keepalives etc. are needed)
  144. */
  145. inline bool reliable() const throw()
  146. {
  147. if (_addr.ss_family == AF_INET)
  148. return ((_ipScope != InetAddress::IP_SCOPE_GLOBAL)&&(_ipScope != InetAddress::IP_SCOPE_PSEUDOPRIVATE));
  149. return true;
  150. }
  151. /**
  152. * @return True if address is non-NULL
  153. */
  154. inline operator bool() const throw() { return (_addr); }
  155. /**
  156. * Check whether this address is valid for a ZeroTier path
  157. *
  158. * This checks the address type and scope against address types and scopes
  159. * that we currently support for ZeroTier communication.
  160. *
  161. * @param a Address to check
  162. * @return True if address is good for ZeroTier path use
  163. */
  164. static inline bool isAddressValidForPath(const InetAddress &a)
  165. throw()
  166. {
  167. if ((a.ss_family == AF_INET)||(a.ss_family == AF_INET6)) {
  168. switch(a.ipScope()) {
  169. /* Note: we don't do link-local at the moment. Unfortunately these
  170. * cause several issues. The first is that they usually require a
  171. * device qualifier, which we don't handle yet and can't portably
  172. * push in PUSH_DIRECT_PATHS. The second is that some OSes assign
  173. * these very ephemerally or otherwise strangely. So we'll use
  174. * private, pseudo-private, shared (e.g. carrier grade NAT), or
  175. * global IP addresses. */
  176. case InetAddress::IP_SCOPE_PRIVATE:
  177. case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
  178. case InetAddress::IP_SCOPE_SHARED:
  179. case InetAddress::IP_SCOPE_GLOBAL:
  180. return true;
  181. default:
  182. return false;
  183. }
  184. }
  185. return false;
  186. }
  187. template<unsigned int C>
  188. inline void serialize(Buffer<C> &b) const
  189. {
  190. b.append((uint8_t)1); // version
  191. b.append((uint64_t)_lastSend);
  192. b.append((uint64_t)_lastReceived);
  193. _addr.serialize(b);
  194. _localAddress.serialize(b);
  195. }
  196. template<unsigned int C>
  197. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  198. {
  199. unsigned int p = startAt;
  200. if (b[p++] != 1)
  201. throw std::invalid_argument("invalid serialized Path");
  202. _lastSend = b.template at<uint64_t>(p); p += 8;
  203. _lastReceived = b.template at<uint64_t>(p); p += 8;
  204. p += _addr.deserialize(b,p);
  205. p += _localAddress.deserialize(b,p);
  206. _ipScope = _addr.ipScope();
  207. return (p - startAt);
  208. }
  209. private:
  210. uint64_t _lastSend;
  211. uint64_t _lastReceived;
  212. InetAddress _addr;
  213. InetAddress _localAddress;
  214. InetAddress::IpScope _ipScope; // memoize this since it's a computed value checked often
  215. };
  216. } // namespace ZeroTier
  217. #endif