World.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 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. #ifndef ZT_WORLD_HPP
  19. #define ZT_WORLD_HPP
  20. #include <vector>
  21. #include <string>
  22. #include "Constants.hpp"
  23. #include "InetAddress.hpp"
  24. #include "Identity.hpp"
  25. #include "Buffer.hpp"
  26. #include "C25519.hpp"
  27. /**
  28. * Maximum number of roots (sanity limit, okay to increase)
  29. *
  30. * A given root can (through multi-homing) be distributed across any number of
  31. * physical endpoints, but having more than one is good to permit total failure
  32. * of one root or its withdrawal due to compromise without taking the whole net
  33. * down.
  34. */
  35. #define ZT_WORLD_MAX_ROOTS 4
  36. /**
  37. * Maximum number of stable endpoints per root (sanity limit, okay to increase)
  38. */
  39. #define ZT_WORLD_MAX_STABLE_ENDPOINTS_PER_ROOT 32
  40. /**
  41. * The (more than) maximum length of a serialized World
  42. */
  43. #define ZT_WORLD_MAX_SERIALIZED_LENGTH (((1024 + (32 * ZT_WORLD_MAX_STABLE_ENDPOINTS_PER_ROOT)) * ZT_WORLD_MAX_ROOTS) + ZT_C25519_PUBLIC_KEY_LEN + ZT_C25519_SIGNATURE_LEN + 128)
  44. /**
  45. * World ID indicating null / empty World object
  46. */
  47. #define ZT_WORLD_ID_NULL 0
  48. /**
  49. * World ID for a test network with ephemeral or temporary roots
  50. */
  51. #define ZT_WORLD_ID_TESTNET 1
  52. /**
  53. * World ID for Earth
  54. *
  55. * This is the ID for the ZeroTier World used on planet Earth. It is unrelated
  56. * to the public network 8056c2e21c000001 of the same name. It was chosen
  57. * from Earth's approximate distance from the sun in kilometers.
  58. */
  59. #define ZT_WORLD_ID_EARTH 149604618
  60. /**
  61. * World ID for Mars -- for future use by SpaceX or others
  62. */
  63. #define ZT_WORLD_ID_MARS 227883110
  64. namespace ZeroTier {
  65. /**
  66. * A world definition (formerly known as a root topology)
  67. *
  68. * Think of a World as a single data center. Within this data center a set
  69. * of distributed fault tolerant root servers provide stable anchor points
  70. * for a peer to peer network that provides VLAN service. Updates to a world
  71. * definition can be published by signing them with the previous revision's
  72. * signing key, and should be very infrequent.
  73. *
  74. * The maximum data center size is approximately 2.5 cubic light seconds,
  75. * since many protocols have issues with >5s RTT latencies.
  76. *
  77. * ZeroTier operates a World for Earth capable of encompassing the planet, its
  78. * orbits, the Moon (about 1.3 light seconds), and nearby Lagrange points. A
  79. * world ID for Mars and nearby space is defined but not yet used, and a test
  80. * world ID is provided for testing purposes.
  81. *
  82. * If you absolutely must run your own "unofficial" ZeroTier network, please
  83. * define your world IDs above 0xffffffff (4294967295). Code to make a World
  84. * is in mkworld.cpp in the parent directory and must be edited to change
  85. * settings.
  86. */
  87. class World
  88. {
  89. public:
  90. struct Root
  91. {
  92. Identity identity;
  93. std::vector<InetAddress> stableEndpoints;
  94. inline bool operator==(const Root &r) const throw() { return ((identity == r.identity)&&(stableEndpoints == r.stableEndpoints)); }
  95. inline bool operator!=(const Root &r) const throw() { return (!(*this == r)); }
  96. inline bool operator<(const Root &r) const throw() { return (identity < r.identity); } // for sorting
  97. };
  98. /**
  99. * Construct an empty / null World
  100. */
  101. World() :
  102. _id(ZT_WORLD_ID_NULL),
  103. _ts(0) {}
  104. /**
  105. * @return Root servers for this world and their stable endpoints
  106. */
  107. inline const std::vector<World::Root> &roots() const throw() { return _roots; }
  108. /**
  109. * @return World unique identifier
  110. */
  111. inline uint64_t id() const throw() { return _id; }
  112. /**
  113. * @return World definition timestamp
  114. */
  115. inline uint64_t timestamp() const throw() { return _ts; }
  116. /**
  117. * Check whether a world update should replace this one
  118. *
  119. * A new world update is valid if it is for the same world ID, is newer,
  120. * and is signed by the current world's signing key. If this world object
  121. * is null, it can always be updated.
  122. *
  123. * @param update Candidate update
  124. * @param fullSignatureCheck Perform full cryptographic signature check (true == yes, false == skip)
  125. * @return True if update is newer than current and is properly signed
  126. */
  127. inline bool shouldBeReplacedBy(const World &update,bool fullSignatureCheck)
  128. {
  129. if (_id == ZT_WORLD_ID_NULL)
  130. return true;
  131. if ((_id == update._id)&&(_ts < update._ts)) {
  132. if (fullSignatureCheck) {
  133. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> tmp;
  134. update.serialize(tmp,true);
  135. return C25519::verify(_updateSigningKey,tmp.data(),tmp.size(),update._signature);
  136. } else return true;
  137. }
  138. return false;
  139. }
  140. /**
  141. * @return True if this World is non-empty
  142. */
  143. inline operator bool() const throw() { return (_id != ZT_WORLD_ID_NULL); }
  144. template<unsigned int C>
  145. inline void serialize(Buffer<C> &b,bool forSign = false) const
  146. {
  147. if (forSign)
  148. b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  149. b.append((uint8_t)0x01); // version -- only one valid value for now
  150. b.append((uint64_t)_id);
  151. b.append((uint64_t)_ts);
  152. b.append(_updateSigningKey.data,ZT_C25519_PUBLIC_KEY_LEN);
  153. if (!forSign)
  154. b.append(_signature.data,ZT_C25519_SIGNATURE_LEN);
  155. b.append((uint8_t)_roots.size());
  156. for(std::vector<Root>::const_iterator r(_roots.begin());r!=_roots.end();++r) {
  157. r->identity.serialize(b);
  158. b.append((uint8_t)r->stableEndpoints.size());
  159. for(std::vector<InetAddress>::const_iterator ep(r->stableEndpoints.begin());ep!=r->stableEndpoints.end();++ep)
  160. ep->serialize(b);
  161. }
  162. if (forSign)
  163. b.append((uint64_t)0xf7f7f7f7f7f7f7f7ULL);
  164. }
  165. template<unsigned int C>
  166. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  167. {
  168. unsigned int p = startAt;
  169. _roots.clear();
  170. if (b[p++] != 0x01)
  171. throw std::invalid_argument("invalid World serialized version");
  172. _id = b.template at<uint64_t>(p); p += 8;
  173. _ts = b.template at<uint64_t>(p); p += 8;
  174. memcpy(_updateSigningKey.data,b.field(p,ZT_C25519_PUBLIC_KEY_LEN),ZT_C25519_PUBLIC_KEY_LEN); p += ZT_C25519_PUBLIC_KEY_LEN;
  175. memcpy(_signature.data,b.field(p,ZT_C25519_SIGNATURE_LEN),ZT_C25519_SIGNATURE_LEN); p += ZT_C25519_SIGNATURE_LEN;
  176. unsigned int numRoots = b[p++];
  177. if (numRoots > ZT_WORLD_MAX_ROOTS)
  178. throw std::invalid_argument("too many roots in World");
  179. for(unsigned int k=0;k<numRoots;++k) {
  180. _roots.push_back(Root());
  181. Root &r = _roots.back();
  182. p += r.identity.deserialize(b,p);
  183. unsigned int numStableEndpoints = b[p++];
  184. if (numStableEndpoints > ZT_WORLD_MAX_STABLE_ENDPOINTS_PER_ROOT)
  185. throw std::invalid_argument("too many stable endpoints in World/Root");
  186. for(unsigned int kk=0;kk<numStableEndpoints;++kk) {
  187. r.stableEndpoints.push_back(InetAddress());
  188. p += r.stableEndpoints.back().deserialize(b,p);
  189. }
  190. }
  191. return (p - startAt);
  192. }
  193. inline bool operator==(const World &w) const throw() { return ((_id == w._id)&&(_ts == w._ts)&&(_updateSigningKey == w._updateSigningKey)&&(_signature == w._signature)&&(_roots == w._roots)); }
  194. inline bool operator!=(const World &w) const throw() { return (!(*this == w)); }
  195. protected:
  196. uint64_t _id;
  197. uint64_t _ts;
  198. C25519::Public _updateSigningKey;
  199. C25519::Signature _signature;
  200. std::vector<Root> _roots;
  201. };
  202. } // namespace ZeroTier
  203. #endif