World.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 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_WORLD_HPP
  27. #define ZT_WORLD_HPP
  28. #include <vector>
  29. #include <string>
  30. #include "Constants.hpp"
  31. #include "InetAddress.hpp"
  32. #include "Identity.hpp"
  33. #include "Buffer.hpp"
  34. #include "C25519.hpp"
  35. /**
  36. * Maximum number of roots (sanity limit, okay to increase)
  37. *
  38. * A given root can (through multi-homing) be distributed across any number of
  39. * physical endpoints, but having more than one is good to permit total failure
  40. * of one root or its withdrawal due to compromise without taking the whole net
  41. * down.
  42. */
  43. #define ZT_WORLD_MAX_ROOTS 4
  44. /**
  45. * Maximum number of stable endpoints per root (sanity limit, okay to increase)
  46. */
  47. #define ZT_WORLD_MAX_STABLE_ENDPOINTS_PER_ROOT 32
  48. /**
  49. * The (more than) maximum length of a serialized World
  50. */
  51. #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)
  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. class World
  83. {
  84. public:
  85. /**
  86. * World type -- do not change IDs
  87. */
  88. enum Type
  89. {
  90. TYPE_NULL = 0,
  91. TYPE_PLANET = 1, // Planets, of which there is currently one (Earth)
  92. TYPE_MOON = 127 // Moons, which are user-created and many
  93. };
  94. /**
  95. * Upstream server definition in world/moon
  96. */
  97. struct Root
  98. {
  99. Identity identity;
  100. std::vector<InetAddress> stableEndpoints;
  101. inline bool operator==(const Root &r) const throw() { return ((identity == r.identity)&&(stableEndpoints == r.stableEndpoints)); }
  102. inline bool operator!=(const Root &r) const throw() { return (!(*this == r)); }
  103. inline bool operator<(const Root &r) const throw() { return (identity < r.identity); } // for sorting
  104. };
  105. /**
  106. * Construct an empty / null World
  107. */
  108. World() :
  109. _id(0),
  110. _ts(0),
  111. _type(TYPE_NULL) {}
  112. /**
  113. * @return Root servers for this world and their stable endpoints
  114. */
  115. inline const std::vector<World::Root> &roots() const { return _roots; }
  116. /**
  117. * @return World type: planet or moon
  118. */
  119. inline Type type() const { return _type; }
  120. /**
  121. * @return World unique identifier
  122. */
  123. inline uint64_t id() const { return _id; }
  124. /**
  125. * @return World definition timestamp
  126. */
  127. inline uint64_t timestamp() const { return _ts; }
  128. /**
  129. * @return C25519 signature
  130. */
  131. inline const C25519::Signature &signature() const { return _signature; }
  132. /**
  133. * @return Public key that must sign next update
  134. */
  135. inline const C25519::Public &updatesMustBeSignedBy() const { return _updatesMustBeSignedBy; }
  136. /**
  137. * Check whether a world update should replace this one
  138. *
  139. * @param update Candidate update
  140. * @return True if update is newer than current, matches its ID and type, and is properly signed (or if current is NULL)
  141. */
  142. inline bool shouldBeReplacedBy(const World &update)
  143. {
  144. if ((_id == 0)||(_type == TYPE_NULL))
  145. return true;
  146. if ((_id == update._id)&&(_ts < update._ts)&&(_type == update._type)) {
  147. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> tmp;
  148. update.serialize(tmp,true);
  149. return C25519::verify(_updatesMustBeSignedBy,tmp.data(),tmp.size(),update._signature);
  150. }
  151. return false;
  152. }
  153. /**
  154. * @return True if this World is non-empty
  155. */
  156. inline operator bool() const { return (_type != TYPE_NULL); }
  157. template<unsigned int C>
  158. inline void serialize(Buffer<C> &b,bool forSign = false) const
  159. {
  160. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  161. b.append((uint8_t)_type);
  162. b.append((uint64_t)_id);
  163. b.append((uint64_t)_ts);
  164. b.append(_updatesMustBeSignedBy.data,ZT_C25519_PUBLIC_KEY_LEN);
  165. if (!forSign)
  166. b.append(_signature.data,ZT_C25519_SIGNATURE_LEN);
  167. b.append((uint8_t)_roots.size());
  168. for(std::vector<Root>::const_iterator r(_roots.begin());r!=_roots.end();++r) {
  169. r->identity.serialize(b);
  170. b.append((uint8_t)r->stableEndpoints.size());
  171. for(std::vector<InetAddress>::const_iterator ep(r->stableEndpoints.begin());ep!=r->stableEndpoints.end();++ep)
  172. ep->serialize(b);
  173. }
  174. if (_type == TYPE_MOON)
  175. b.append((uint16_t)0); // no attached dictionary (for future use)
  176. if (forSign) b.append((uint64_t)0xf7f7f7f7f7f7f7f7ULL);
  177. }
  178. template<unsigned int C>
  179. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  180. {
  181. unsigned int p = startAt;
  182. _roots.clear();
  183. switch((Type)b[p++]) {
  184. case TYPE_NULL: _type = TYPE_NULL; break; // shouldn't ever really happen in serialized data but it's not invalid
  185. case TYPE_PLANET: _type = TYPE_PLANET; break;
  186. case TYPE_MOON: _type = TYPE_MOON; break;
  187. default:
  188. throw std::invalid_argument("invalid world type");
  189. }
  190. _id = b.template at<uint64_t>(p); p += 8;
  191. _ts = b.template at<uint64_t>(p); p += 8;
  192. memcpy(_updatesMustBeSignedBy.data,b.field(p,ZT_C25519_PUBLIC_KEY_LEN),ZT_C25519_PUBLIC_KEY_LEN); p += ZT_C25519_PUBLIC_KEY_LEN;
  193. memcpy(_signature.data,b.field(p,ZT_C25519_SIGNATURE_LEN),ZT_C25519_SIGNATURE_LEN); p += ZT_C25519_SIGNATURE_LEN;
  194. const unsigned int numRoots = (unsigned int)b[p++];
  195. if (numRoots > ZT_WORLD_MAX_ROOTS)
  196. throw std::invalid_argument("too many roots in World");
  197. for(unsigned int k=0;k<numRoots;++k) {
  198. _roots.push_back(Root());
  199. Root &r = _roots.back();
  200. p += r.identity.deserialize(b,p);
  201. unsigned int numStableEndpoints = b[p++];
  202. if (numStableEndpoints > ZT_WORLD_MAX_STABLE_ENDPOINTS_PER_ROOT)
  203. throw std::invalid_argument("too many stable endpoints in World/Root");
  204. for(unsigned int kk=0;kk<numStableEndpoints;++kk) {
  205. r.stableEndpoints.push_back(InetAddress());
  206. p += r.stableEndpoints.back().deserialize(b,p);
  207. }
  208. }
  209. if (_type == TYPE_MOON)
  210. p += b.template at<uint16_t>(p) + 2;
  211. return (p - startAt);
  212. }
  213. inline bool operator==(const World &w) const { return ((_id == w._id)&&(_ts == w._ts)&&(_updatesMustBeSignedBy == w._updatesMustBeSignedBy)&&(_signature == w._signature)&&(_roots == w._roots)&&(_type == w._type)); }
  214. inline bool operator!=(const World &w) const { return (!(*this == w)); }
  215. inline bool operator<(const World &w) const { return (((int)_type < (int)w._type) ? true : ((_type == w._type) ? (_id < w._id) : false)); }
  216. /**
  217. * Create a World object signed with a key pair
  218. *
  219. * @param t World type
  220. * @param id World ID
  221. * @param ts World timestamp / revision
  222. * @param sk Key that must be used to sign the next future update to this world
  223. * @param roots Roots and their stable endpoints
  224. * @param signWith Key to sign this World with (can have the same public as the next-update signing key, but doesn't have to)
  225. * @return Signed World object
  226. */
  227. static inline World make(World::Type t,uint64_t id,uint64_t ts,const C25519::Public &sk,const std::vector<World::Root> &roots,const C25519::Pair &signWith)
  228. {
  229. World w;
  230. w._id = id;
  231. w._ts = ts;
  232. w._type = t;
  233. w._updatesMustBeSignedBy = sk;
  234. w._roots = roots;
  235. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> tmp;
  236. w.serialize(tmp,true);
  237. w._signature = C25519::sign(signWith,tmp.data(),tmp.size());
  238. return w;
  239. }
  240. protected:
  241. uint64_t _id;
  242. uint64_t _ts;
  243. Type _type;
  244. C25519::Public _updatesMustBeSignedBy;
  245. C25519::Signature _signature;
  246. std::vector<Root> _roots;
  247. };
  248. } // namespace ZeroTier
  249. #endif