Path.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. /**
  46. * Path trust category
  47. *
  48. * Note that this is NOT peer trust and has nothing to do with root server
  49. * designations or other trust metrics. This indicates how much we trust
  50. * this path to be secure and/or private. A trust level of normal means
  51. * encrypt and authenticate all traffic. Privacy trust means we can send
  52. * traffic in the clear. Ultimate trust means we don't even need
  53. * authentication. Generally a private path would be a hard-wired local
  54. * LAN, while an ultimate trust path would be a physically isolated private
  55. * server backplane.
  56. *
  57. * Nearly all paths will be normal trust. The other levels are for high
  58. * performance local SDN use only.
  59. *
  60. * These values MUST match ZT_LocalInterfaceAddressTrust in ZeroTierOne.h
  61. */
  62. enum Trust // NOTE: max 255
  63. {
  64. TRUST_NORMAL = 0,
  65. TRUST_PRIVACY = 10,
  66. TRUST_ULTIMATE = 20
  67. };
  68. Path() :
  69. _lastSend(0),
  70. _lastReceived(0),
  71. _addr(),
  72. _localAddress(),
  73. _ipScope(InetAddress::IP_SCOPE_NONE),
  74. _trust(TRUST_NORMAL),
  75. _flags(0)
  76. {
  77. }
  78. Path(const InetAddress &localAddress,const InetAddress &addr,Trust trust) :
  79. _lastSend(0),
  80. _lastReceived(0),
  81. _addr(addr),
  82. _localAddress(localAddress),
  83. _ipScope(addr.ipScope()),
  84. _trust(trust),
  85. _flags(0)
  86. {
  87. }
  88. /**
  89. * Called when a packet is sent to this remote path
  90. *
  91. * This is called automatically by Path::send().
  92. *
  93. * @param t Time of send
  94. */
  95. inline void sent(uint64_t t)
  96. throw()
  97. {
  98. _lastSend = t;
  99. }
  100. /**
  101. * Called when a packet is received from this remote path
  102. *
  103. * @param t Time of receive
  104. */
  105. inline void received(uint64_t t)
  106. throw()
  107. {
  108. _lastReceived = t;
  109. }
  110. /**
  111. * @param now Current time
  112. * @return True if this path appears active
  113. */
  114. inline bool active(uint64_t now) const
  115. throw()
  116. {
  117. return ((now - _lastReceived) < ZT_PEER_ACTIVITY_TIMEOUT);
  118. }
  119. /**
  120. * Send a packet via this path
  121. *
  122. * @param RR Runtime environment
  123. * @param data Packet data
  124. * @param len Packet length
  125. * @param now Current time
  126. * @return True if transport reported success
  127. */
  128. bool send(const RuntimeEnvironment *RR,const void *data,unsigned int len,uint64_t now);
  129. /**
  130. * @return Address of local side of this path or NULL if unspecified
  131. */
  132. inline const InetAddress &localAddress() const throw() { return _localAddress; }
  133. /**
  134. * @return Time of last send to this path
  135. */
  136. inline uint64_t lastSend() const throw() { return _lastSend; }
  137. /**
  138. * @return Time of last receive from this path
  139. */
  140. inline uint64_t lastReceived() const throw() { return _lastReceived; }
  141. /**
  142. * @return Physical address
  143. */
  144. inline const InetAddress &address() const throw() { return _addr; }
  145. /**
  146. * @return IP scope -- faster shortcut for address().ipScope()
  147. */
  148. inline InetAddress::IpScope ipScope() const throw() { return _ipScope; }
  149. /**
  150. * @return Preference rank, higher == better
  151. */
  152. inline int preferenceRank() const throw()
  153. {
  154. // First, since the scope enum values in InetAddress.hpp are in order of
  155. // use preference rank, we take that. Then we multiple by two, yielding
  156. // a sequence like 0, 2, 4, 6, etc. Then if it's IPv6 we add one. This
  157. // makes IPv6 addresses of a given scope outrank IPv4 addresses of the
  158. // same scope -- e.g. 1 outranks 0. This makes us prefer IPv6, but not
  159. // if the address scope/class is of a fundamentally lower rank.
  160. return ( ((int)_ipScope * 2) + ((_addr.ss_family == AF_INET6) ? 1 : 0) );
  161. }
  162. /**
  163. * @return Path trust level
  164. */
  165. inline Trust trust() const throw() { return _trust; }
  166. /**
  167. * @return True if path is considered reliable (no NAT keepalives etc. are needed)
  168. */
  169. inline bool reliable() const throw()
  170. {
  171. if (_addr.ss_family == AF_INET)
  172. return ((_ipScope != InetAddress::IP_SCOPE_GLOBAL)&&(_ipScope != InetAddress::IP_SCOPE_PSEUDOPRIVATE));
  173. return true;
  174. }
  175. /**
  176. * @return True if address is non-NULL
  177. */
  178. inline operator bool() const throw() { return (_addr); }
  179. /**
  180. * Check whether this address is valid for a ZeroTier path
  181. *
  182. * This checks the address type and scope against address types and scopes
  183. * that we currently support for ZeroTier communication.
  184. *
  185. * @param a Address to check
  186. * @return True if address is good for ZeroTier path use
  187. */
  188. static inline bool isAddressValidForPath(const InetAddress &a)
  189. throw()
  190. {
  191. if ((a.ss_family == AF_INET)||(a.ss_family == AF_INET6)) {
  192. switch(a.ipScope()) {
  193. /* Note: we don't do link-local at the moment. Unfortunately these
  194. * cause several issues. The first is that they usually require a
  195. * device qualifier, which we don't handle yet and can't portably
  196. * push in PUSH_DIRECT_PATHS. The second is that some OSes assign
  197. * these very ephemerally or otherwise strangely. So we'll use
  198. * private, pseudo-private, shared (e.g. carrier grade NAT), or
  199. * global IP addresses. */
  200. case InetAddress::IP_SCOPE_PRIVATE:
  201. case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
  202. case InetAddress::IP_SCOPE_SHARED:
  203. case InetAddress::IP_SCOPE_GLOBAL:
  204. return true;
  205. default:
  206. return false;
  207. }
  208. }
  209. return false;
  210. }
  211. template<unsigned int C>
  212. inline void serialize(Buffer<C> &b) const
  213. {
  214. b.append((uint8_t)0); // version
  215. b.append((uint64_t)_lastSend);
  216. b.append((uint64_t)_lastReceived);
  217. _addr.serialize(b);
  218. _localAddress.serialize(b);
  219. b.append((uint8_t)_trust);
  220. b.append((uint16_t)_flags);
  221. }
  222. template<unsigned int C>
  223. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  224. {
  225. unsigned int p = startAt;
  226. if (b[p++] != 0)
  227. throw std::invalid_argument("invalid serialized Path");
  228. _lastSend = b.template at<uint64_t>(p); p += 8;
  229. _lastReceived = b.template at<uint64_t>(p); p += 8;
  230. p += _addr.deserialize(b,p);
  231. p += _localAddress.deserialize(b,p);
  232. _ipScope = _addr.ipScope();
  233. _trust = (Path::Trust)b[p++];
  234. _flags = b.template at<uint16_t>(p); p += 2;
  235. return (p - startAt);
  236. }
  237. private:
  238. uint64_t _lastSend;
  239. uint64_t _lastReceived;
  240. InetAddress _addr;
  241. InetAddress _localAddress;
  242. InetAddress::IpScope _ipScope; // memoize this since it's a computed value checked often
  243. Trust _trust;
  244. uint16_t _flags;
  245. };
  246. } // namespace ZeroTier
  247. #endif