World.hpp 7.9 KB

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