Root.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. class Root
  52. {
  53. public:
  54. inline Root() : _dnsPublicKeySize(0) {}
  55. /**
  56. * Create a new root entry
  57. *
  58. * @param dn DNS name
  59. * @param dnspk DNS public key for record validation
  60. * @param dnspksize Size of DNS public key (currently always the size of a NIST P-384 point compressed public key)
  61. * @param dflId Default identity if DNS is not available
  62. * @param dflAddrs Default IP addresses if DNS is not available
  63. */
  64. template<typename S>
  65. inline Root(S dn,const uint8_t *const dnspk,const unsigned int dnspksize,const Identity &dflId,const std::vector<InetAddress> &dflAddrs) :
  66. _defaultIdentity(dflId),
  67. _defaultAddresses(dflAddrs),
  68. _dnsName(dn),
  69. _dnsPublicKeySize(dnspksize)
  70. {
  71. if (dnspksize != 0) {
  72. if (dnspksize > sizeof(_dnsPublicKey))
  73. throw ZT_EXCEPTION_INVALID_ARGUMENT;
  74. memcpy(_dnsPublicKey,dnspk,dnspksize);
  75. }
  76. }
  77. /**
  78. * @return Current identity (either default or latest locator)
  79. */
  80. inline const Identity id() const
  81. {
  82. if (_lastFetchedLocator.id())
  83. return _lastFetchedLocator.id();
  84. return _defaultIdentity;
  85. }
  86. /**
  87. * @param id Identity to check
  88. * @return True if identity equals this root's current identity
  89. */
  90. inline bool is(const Identity &id) const
  91. {
  92. return ((_lastFetchedLocator.id()) ? (id == _lastFetchedLocator.id()) : (id == _defaultIdentity));
  93. }
  94. /**
  95. * @return Current ZeroTier address (either default or latest locator)
  96. */
  97. inline const Address address() const
  98. {
  99. if (_lastFetchedLocator.id())
  100. return _lastFetchedLocator.id().address();
  101. return _defaultIdentity.address();
  102. }
  103. /**
  104. * @return DNS name for this root or empty string if static entry with no DNS
  105. */
  106. inline const Str dnsName() const
  107. {
  108. return _dnsName;
  109. }
  110. /**
  111. * @return Latest locator or NIL locator object if none
  112. */
  113. inline Locator locator() const
  114. {
  115. return _lastFetchedLocator;
  116. }
  117. /**
  118. * @return Timestamp of latest retrieved locator or 0 if none
  119. */
  120. inline int64_t locatorTimestamp() const
  121. {
  122. return _lastFetchedLocator.timestamp();
  123. }
  124. /**
  125. * Update locator, returning true if new locator is valid and newer than existing
  126. */
  127. inline bool updateLocator(const Locator &loc)
  128. {
  129. if (!loc.verify())
  130. return false;
  131. if ((loc.phy().size() > 0)&&(loc.timestamp() > _lastFetchedLocator.timestamp())) {
  132. _lastFetchedLocator = loc;
  133. return true;
  134. }
  135. return false;
  136. }
  137. /**
  138. * Update this root's locator from a series of TXT records
  139. */
  140. template<typename I>
  141. inline bool updateLocatorFromTxt(I start,I end)
  142. {
  143. try {
  144. if (_dnsPublicKeySize != ZT_ECC384_PUBLIC_KEY_SIZE)
  145. return false;
  146. Locator loc;
  147. if (!loc.decodeTxtRecords(start,end,_dnsPublicKey)) // also does verify()
  148. return false;
  149. if ((loc.phy().size() > 0)&&(loc.timestamp() > _lastFetchedLocator.timestamp())) {
  150. _lastFetchedLocator = loc;
  151. return true;
  152. }
  153. return false;
  154. } catch ( ... ) {}
  155. return false;
  156. }
  157. /**
  158. * Pick a random physical IP for this root with the given address family
  159. *
  160. * @param addressFamily AF_INET or AF_INET6
  161. * @return Address or InetAddress::NIL if no addresses exist for the given family
  162. */
  163. inline const InetAddress &pickPhysical(const int addressFamily) const
  164. {
  165. std::vector<const InetAddress *> pickList;
  166. const std::vector<InetAddress> *const av = (_lastFetchedLocator) ? &(_lastFetchedLocator.phy()) : &_defaultAddresses;
  167. for(std::vector<InetAddress>::const_iterator i(av->begin());i!=av->end();++i) {
  168. if (addressFamily == (int)i->ss_family) {
  169. pickList.push_back(&(*i));
  170. }
  171. }
  172. if (pickList.size() == 1)
  173. return *pickList[0];
  174. else if (pickList.size() > 1)
  175. return *pickList[(unsigned long)Utils::random() % (unsigned long)pickList.size()];
  176. return InetAddress::NIL;
  177. }
  178. private:
  179. Identity _defaultIdentity;
  180. std::vector<InetAddress> _defaultAddresses;
  181. Str _dnsName;
  182. Locator _lastFetchedLocator;
  183. unsigned int _dnsPublicKeySize;
  184. uint8_t _dnsPublicKey[ZT_ECC384_PUBLIC_KEY_SIZE];
  185. };
  186. } // namespace ZeroTier
  187. #endif