Path.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. inline Path &operator=(const Path &p)
  89. throw()
  90. {
  91. if (this != &p)
  92. memcpy(this,&p,sizeof(Path));
  93. return *this;
  94. }
  95. /**
  96. * Called when a packet is sent to this remote path
  97. *
  98. * This is called automatically by Path::send().
  99. *
  100. * @param t Time of send
  101. */
  102. inline void sent(uint64_t t)
  103. throw()
  104. {
  105. _lastSend = t;
  106. }
  107. /**
  108. * Called when a packet is received from this remote path
  109. *
  110. * @param t Time of receive
  111. */
  112. inline void received(uint64_t t)
  113. throw()
  114. {
  115. _lastReceived = t;
  116. }
  117. /**
  118. * @param now Current time
  119. * @return True if this path appears active
  120. */
  121. inline bool active(uint64_t now) const
  122. throw()
  123. {
  124. return ((now - _lastReceived) < ZT_PEER_ACTIVITY_TIMEOUT);
  125. }
  126. /**
  127. * Send a packet via this path
  128. *
  129. * @param RR Runtime environment
  130. * @param data Packet data
  131. * @param len Packet length
  132. * @param now Current time
  133. * @return True if transport reported success
  134. */
  135. bool send(const RuntimeEnvironment *RR,const void *data,unsigned int len,uint64_t now);
  136. /**
  137. * @return Address of local side of this path or NULL if unspecified
  138. */
  139. inline const InetAddress &localAddress() const throw() { return _localAddress; }
  140. /**
  141. * @return Time of last send to this path
  142. */
  143. inline uint64_t lastSend() const throw() { return _lastSend; }
  144. /**
  145. * @return Time of last receive from this path
  146. */
  147. inline uint64_t lastReceived() const throw() { return _lastReceived; }
  148. /**
  149. * @return Physical address
  150. */
  151. inline const InetAddress &address() const throw() { return _addr; }
  152. /**
  153. * @return IP scope -- faster shortcut for address().ipScope()
  154. */
  155. inline InetAddress::IpScope ipScope() const throw() { return _ipScope; }
  156. /**
  157. * @return Preference rank, higher == better
  158. */
  159. inline int preferenceRank() const throw()
  160. {
  161. // First, since the scope enum values in InetAddress.hpp are in order of
  162. // use preference rank, we take that. Then we multiple by two, yielding
  163. // a sequence like 0, 2, 4, 6, etc. Then if it's IPv6 we add one. This
  164. // makes IPv6 addresses of a given scope outrank IPv4 addresses of the
  165. // same scope -- e.g. 1 outranks 0. This makes us prefer IPv6, but not
  166. // if the address scope/class is of a fundamentally lower rank.
  167. return ( ((int)_ipScope * 2) + ((_addr.ss_family == AF_INET6) ? 1 : 0) );
  168. }
  169. /**
  170. * @return Path trust level
  171. */
  172. inline Trust trust() const throw() { return _trust; }
  173. /**
  174. * @return True if path is considered reliable (no NAT keepalives etc. are needed)
  175. */
  176. inline bool reliable() const throw()
  177. {
  178. if (_addr.ss_family == AF_INET)
  179. return ((_ipScope != InetAddress::IP_SCOPE_GLOBAL)&&(_ipScope != InetAddress::IP_SCOPE_PSEUDOPRIVATE));
  180. return true;
  181. }
  182. /**
  183. * @return True if address is non-NULL
  184. */
  185. inline operator bool() const throw() { return (_addr); }
  186. /**
  187. * Check whether this address is valid for a ZeroTier path
  188. *
  189. * This checks the address type and scope against address types and scopes
  190. * that we currently support for ZeroTier communication.
  191. *
  192. * @param a Address to check
  193. * @return True if address is good for ZeroTier path use
  194. */
  195. static inline bool isAddressValidForPath(const InetAddress &a)
  196. throw()
  197. {
  198. if ((a.ss_family == AF_INET)||(a.ss_family == AF_INET6)) {
  199. switch(a.ipScope()) {
  200. /* Note: we don't do link-local at the moment. Unfortunately these
  201. * cause several issues. The first is that they usually require a
  202. * device qualifier, which we don't handle yet and can't portably
  203. * push in PUSH_DIRECT_PATHS. The second is that some OSes assign
  204. * these very ephemerally or otherwise strangely. So we'll use
  205. * private, pseudo-private, shared (e.g. carrier grade NAT), or
  206. * global IP addresses. */
  207. case InetAddress::IP_SCOPE_PRIVATE:
  208. case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
  209. case InetAddress::IP_SCOPE_SHARED:
  210. case InetAddress::IP_SCOPE_GLOBAL:
  211. return true;
  212. default:
  213. return false;
  214. }
  215. }
  216. return false;
  217. }
  218. template<unsigned int C>
  219. inline void serialize(Buffer<C> &b) const
  220. {
  221. b.append((uint8_t)0); // version
  222. b.append((uint64_t)_lastSend);
  223. b.append((uint64_t)_lastReceived);
  224. _addr.serialize(b);
  225. _localAddress.serialize(b);
  226. b.append((uint8_t)_trust);
  227. b.append((uint16_t)_flags);
  228. }
  229. template<unsigned int C>
  230. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  231. {
  232. unsigned int p = startAt;
  233. if (b[p++] != 0)
  234. throw std::invalid_argument("invalid serialized Path");
  235. _lastSend = b.template at<uint64_t>(p); p += 8;
  236. _lastReceived = b.template at<uint64_t>(p); p += 8;
  237. p += _addr.deserialize(b,p);
  238. p += _localAddress.deserialize(b,p);
  239. _ipScope = _addr.ipScope();
  240. _trust = (Path::Trust)b[p++];
  241. _flags = b.template at<uint16_t>(p); p += 2;
  242. return (p - startAt);
  243. }
  244. private:
  245. uint64_t _lastSend;
  246. uint64_t _lastReceived;
  247. InetAddress _addr;
  248. InetAddress _localAddress;
  249. InetAddress::IpScope _ipScope; // memoize this since it's a computed value checked often
  250. Trust _trust;
  251. uint16_t _flags;
  252. };
  253. } // namespace ZeroTier
  254. #endif