Root.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 none)
  123. */
  124. inline const Str dnsName() const
  125. {
  126. Mutex::Lock l(_lock);
  127. return _dnsName;
  128. }
  129. /**
  130. * @return Latest locator
  131. */
  132. inline Locator locator() const
  133. {
  134. Mutex::Lock l(_lock);
  135. return _lastFetchedLocator;
  136. }
  137. /**
  138. * @return Timestamp of latest retrieved locator
  139. */
  140. inline int64_t locatorTimestamp() const
  141. {
  142. Mutex::Lock l(_lock);
  143. return _lastFetchedLocator.timestamp();
  144. }
  145. /**
  146. * Pick a random physical address
  147. *
  148. * @return Physical address or InetAddress::NIL if none are available
  149. */
  150. inline const InetAddress randomPhysicalAddress() const
  151. {
  152. Mutex::Lock l(_lock);
  153. if (_lastFetchedLocator.phy().empty()) {
  154. if (_defaultAddresses.empty())
  155. return InetAddress::NIL;
  156. return _defaultAddresses[(unsigned long)Utils::random() % (unsigned long)_defaultAddresses.size()];
  157. }
  158. return _lastFetchedLocator.phy()[(unsigned long)Utils::random() % (unsigned long)_lastFetchedLocator.phy().size()];
  159. }
  160. /**
  161. * Update locator, returning true if new locator is valid and newer than existing
  162. */
  163. inline bool updateLocator(const Locator &loc)
  164. {
  165. if (!loc.verify())
  166. return false;
  167. Mutex::Lock l(_lock);
  168. if ((loc.phy().size() > 0)&&(loc.timestamp() > _lastFetchedLocator.timestamp())) {
  169. _lastFetchedLocator = loc;
  170. return true;
  171. }
  172. return false;
  173. }
  174. /**
  175. * Update this root's locator from a series of TXT records
  176. */
  177. template<typename I>
  178. inline bool updateLocatorFromTxt(I start,I end)
  179. {
  180. try {
  181. Mutex::Lock l(_lock);
  182. if (_dnsPublicKeySize != ZT_ECC384_PUBLIC_KEY_SIZE)
  183. return false;
  184. Locator loc;
  185. if (!loc.decodeTxtRecords(start,end,_dnsPublicKey))
  186. return false;
  187. if ((loc.phy().size() > 0)&&(loc.timestamp() > _lastFetchedLocator.timestamp())) {
  188. _lastFetchedLocator = loc;
  189. return true;
  190. }
  191. return false;
  192. } catch ( ... ) {}
  193. return false;
  194. }
  195. private:
  196. Identity _defaultIdentity;
  197. std::vector<InetAddress> _defaultAddresses;
  198. Str _dnsName;
  199. Locator _lastFetchedLocator;
  200. unsigned int _dnsPublicKeySize;
  201. uint8_t _dnsPublicKey[ZT_ECC384_PUBLIC_KEY_SIZE];
  202. Mutex _lock;
  203. };
  204. } // namespace ZeroTier
  205. #endif