Root.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 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. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_ROOT_HPP
  27. #define ZT_ROOT_HPP
  28. #include "Constants.hpp"
  29. #include "Str.hpp"
  30. #include "ECC384.hpp"
  31. #include "Locator.hpp"
  32. #include "InetAddress.hpp"
  33. #include "Utils.hpp"
  34. #include "Identity.hpp"
  35. #include "Mutex.hpp"
  36. namespace ZeroTier {
  37. /**
  38. * A root entry pointing to a node capable of global identity lookup and indirect transit
  39. *
  40. * Root entries point to DNS records that contain TXT entries that decode to Locator objects
  41. * pointing to actual root nodes. A default root identity and static addresses can also be
  42. * provided as fallback if DNS is not available.
  43. *
  44. * Note that root identities can change if DNS returns a different result, but that DNS entries
  45. * are authenticated using their own signature scheme. This allows a root DNS name to serve
  46. * up different roots based on factors like location or relative load of different roots.
  47. *
  48. * It's also possible to create a root with no DNS and no DNS validator public key. This root
  49. * will be a static entry pointing to a single root identity and set of physical addresses.
  50. *
  51. * This object is thread-safe and may be concurrently accessed and updated.
  52. */
  53. class Root
  54. {
  55. public:
  56. inline Root() : _dnsPublicKeySize(0) {}
  57. inline Root(const Root &r) { *this = r; }
  58. /**
  59. * Create a new root entry
  60. *
  61. * @param dn DNS name
  62. * @param dnspk DNS public key for record validation
  63. * @param dnspksize Size of DNS public key (currently always the size of a NIST P-384 point compressed public key)
  64. * @param dflId Default identity if DNS is not available
  65. * @param dflAddrs Default IP addresses if DNS is not available
  66. */
  67. template<typename S>
  68. inline Root(S dn,const uint8_t *const dnspk,const unsigned int dnspksize,const Identity &dflId,const std::vector<InetAddress> &dflAddrs) :
  69. _defaultIdentity(dflId),
  70. _defaultAddresses(dflAddrs),
  71. _dnsName(dn),
  72. _dnsPublicKeySize(dnspksize)
  73. {
  74. if (dnspksize != 0) {
  75. if (dnspksize > sizeof(_dnsPublicKey))
  76. throw ZT_EXCEPTION_INVALID_ARGUMENT;
  77. memcpy(_dnsPublicKey,dnspk,dnspksize);
  78. }
  79. }
  80. inline Root &operator=(const Root &r)
  81. {
  82. Mutex::Lock l(_lock);
  83. Mutex::Lock rl(r._lock);
  84. _defaultIdentity = r._defaultIdentity;
  85. _defaultAddresses = r._defaultAddresses;
  86. _dnsName = r._dnsName;
  87. _lastFetchedLocator = r._lastFetchedLocator;
  88. _dnsPublicKeySize = r._dnsPublicKeySize;
  89. memcpy(_dnsPublicKey,r._dnsPublicKey,_dnsPublicKeySize);
  90. return *this;
  91. }
  92. /**
  93. * @return Current identity (either default or latest locator)
  94. */
  95. inline const Identity id() const
  96. {
  97. Mutex::Lock l(_lock);
  98. if (_lastFetchedLocator.id())
  99. return _lastFetchedLocator.id();
  100. return _defaultIdentity;
  101. }
  102. /**
  103. * @param id Identity to check
  104. * @return True if identity equals this root's current identity
  105. */
  106. inline bool is(const Identity &id) const
  107. {
  108. Mutex::Lock l(_lock);
  109. return ((_lastFetchedLocator.id()) ? (id == _lastFetchedLocator.id()) : (id == _defaultIdentity));
  110. }
  111. /**
  112. * @return Current ZeroTier address (either default or latest locator)
  113. */
  114. inline const Address address() const
  115. {
  116. Mutex::Lock l(_lock);
  117. if (_lastFetchedLocator.id())
  118. return _lastFetchedLocator.id().address();
  119. return _defaultIdentity.address();
  120. }
  121. /**
  122. * @return DNS name for this root or empty string if static entry with no DNS
  123. */
  124. inline const Str dnsName() const
  125. {
  126. Mutex::Lock l(_lock);
  127. return _dnsName;
  128. }
  129. /**
  130. * @return Latest locator or NIL locator object if none
  131. */
  132. inline Locator locator() const
  133. {
  134. Mutex::Lock l(_lock);
  135. return _lastFetchedLocator;
  136. }
  137. /**
  138. * @return Timestamp of latest retrieved locator or 0 if none
  139. */
  140. inline int64_t locatorTimestamp() const
  141. {
  142. Mutex::Lock l(_lock);
  143. return _lastFetchedLocator.timestamp();
  144. }
  145. /**
  146. * Update locator, returning true if new locator is valid and newer than existing
  147. */
  148. inline bool updateLocator(const Locator &loc)
  149. {
  150. if (!loc.verify())
  151. return false;
  152. Mutex::Lock l(_lock);
  153. if ((loc.phy().size() > 0)&&(loc.timestamp() > _lastFetchedLocator.timestamp())) {
  154. _lastFetchedLocator = loc;
  155. return true;
  156. }
  157. return false;
  158. }
  159. /**
  160. * Update this root's locator from a series of TXT records
  161. */
  162. template<typename I>
  163. inline bool updateLocatorFromTxt(I start,I end)
  164. {
  165. try {
  166. Mutex::Lock l(_lock);
  167. if (_dnsPublicKeySize != ZT_ECC384_PUBLIC_KEY_SIZE)
  168. return false;
  169. Locator loc;
  170. if (!loc.decodeTxtRecords(start,end,_dnsPublicKey)) // also does verify()
  171. return false;
  172. if ((loc.phy().size() > 0)&&(loc.timestamp() > _lastFetchedLocator.timestamp())) {
  173. _lastFetchedLocator = loc;
  174. return true;
  175. }
  176. return false;
  177. } catch ( ... ) {}
  178. return false;
  179. }
  180. /**
  181. * Pick random IPv4 and IPv6 addresses for this root
  182. *
  183. * @param v4 Filled with V4 address or NIL if none found
  184. * @param v6 Filled with V6 address or NIL if none found
  185. */
  186. inline void pickPhysical(InetAddress &v4,InetAddress &v6) const
  187. {
  188. v4.clear();
  189. v6.clear();
  190. std::vector<const InetAddress *> v4a,v6a;
  191. Mutex::Lock l(_lock);
  192. const std::vector<InetAddress> *const av = (_lastFetchedLocator) ? &(_lastFetchedLocator.phy()) : &_defaultAddresses;
  193. for(std::vector<InetAddress>::const_iterator i(av->begin());i!=av->end();++i) {
  194. switch(i->ss_family) {
  195. case AF_INET:
  196. v4a.push_back(&(*i));
  197. break;
  198. case AF_INET6:
  199. v6a.push_back(&(*i));
  200. break;
  201. }
  202. }
  203. if (v4a.size() == 1) {
  204. v4 = *v4a[0];
  205. } else if (v4a.size() > 1) {
  206. v4 = *v4a[(unsigned long)Utils::random() % (unsigned long)v4a.size()];
  207. }
  208. if (v6a.size() == 1) {
  209. v6 = *v6a[0];
  210. } else if (v6a.size() > 1) {
  211. v6 = *v6a[(unsigned long)Utils::random() % (unsigned long)v6a.size()];
  212. }
  213. }
  214. private:
  215. Identity _defaultIdentity;
  216. std::vector<InetAddress> _defaultAddresses;
  217. Str _dnsName;
  218. Locator _lastFetchedLocator;
  219. unsigned int _dnsPublicKeySize;
  220. uint8_t _dnsPublicKey[ZT_ECC384_PUBLIC_KEY_SIZE];
  221. Mutex _lock;
  222. };
  223. } // namespace ZeroTier
  224. #endif