World.hpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 for Earth
  46. *
  47. * This is the ID for the ZeroTier World used on planet Earth. It is unrelated
  48. * to the public network 8056c2e21c000001 of the same name. It was chosen
  49. * from Earth's approximate distance from the sun in kilometers.
  50. */
  51. #define ZT_WORLD_ID_EARTH 149604618
  52. /**
  53. * World ID for Mars -- for future use by SpaceX or others
  54. */
  55. #define ZT_WORLD_ID_MARS 227883110
  56. namespace ZeroTier {
  57. /**
  58. * A world definition (formerly known as a root topology)
  59. *
  60. * Think of a World as a single data center. Within this data center a set
  61. * of distributed fault tolerant root servers provide stable anchor points
  62. * for a peer to peer network that provides VLAN service. Updates to a world
  63. * definition can be published by signing them with the previous revision's
  64. * signing key, and should be very infrequent.
  65. *
  66. * The maximum data center size is approximately 2.5 cubic light seconds,
  67. * since many protocols have issues with >5s RTT latencies.
  68. *
  69. * ZeroTier operates a World for Earth capable of encompassing the planet, its
  70. * orbits, the Moon (about 1.3 light seconds), and nearby Lagrange points. A
  71. * world ID for Mars and nearby space is defined but not yet used, and a test
  72. * world ID is provided for testing purposes.
  73. */
  74. class World
  75. {
  76. public:
  77. /**
  78. * World type -- do not change IDs
  79. */
  80. enum Type
  81. {
  82. TYPE_NULL = 0,
  83. TYPE_PLANET = 1, // Planets, of which there is currently one (Earth)
  84. TYPE_MOON = 127 // Moons, which are user-created and many
  85. };
  86. /**
  87. * Upstream server definition in world/moon
  88. */
  89. struct Root
  90. {
  91. Identity identity;
  92. std::vector<InetAddress> stableEndpoints;
  93. inline bool operator==(const Root &r) const throw() { return ((identity == r.identity)&&(stableEndpoints == r.stableEndpoints)); }
  94. inline bool operator!=(const Root &r) const throw() { return (!(*this == r)); }
  95. inline bool operator<(const Root &r) const throw() { return (identity < r.identity); } // for sorting
  96. };
  97. /**
  98. * Construct an empty / null World
  99. */
  100. World() :
  101. _id(0),
  102. _ts(0),
  103. _type(TYPE_NULL) {}
  104. /**
  105. * @return Root servers for this world and their stable endpoints
  106. */
  107. inline const std::vector<World::Root> &roots() const { return _roots; }
  108. /**
  109. * @return World type: planet or moon
  110. */
  111. inline Type type() const { return _type; }
  112. /**
  113. * @return World unique identifier
  114. */
  115. inline uint64_t id() const { return _id; }
  116. /**
  117. * @return World definition timestamp
  118. */
  119. inline uint64_t timestamp() const { return _ts; }
  120. /**
  121. * Check whether a world update should replace this one
  122. *
  123. * @param update Candidate update
  124. * @return True if update is newer than current, matches its ID and type, and is properly signed (or if current is NULL)
  125. */
  126. inline bool shouldBeReplacedBy(const World &update)
  127. {
  128. if ((_id == 0)||(_type == TYPE_NULL))
  129. return true;
  130. if ((_id == update._id)&&(_ts < update._ts)&&(_type == update._type)) {
  131. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> tmp;
  132. update.serialize(tmp,true);
  133. return C25519::verify(_updatesMustBeSignedBy,tmp.data(),tmp.size(),update._signature);
  134. }
  135. return false;
  136. }
  137. /**
  138. * @return True if this World is non-empty
  139. */
  140. inline operator bool() const { return (_type != TYPE_NULL); }
  141. template<unsigned int C>
  142. inline void serialize(Buffer<C> &b,bool forSign = false) const
  143. {
  144. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  145. b.append((uint8_t)_type);
  146. b.append((uint64_t)_id);
  147. b.append((uint64_t)_ts);
  148. b.append(_updatesMustBeSignedBy.data,ZT_C25519_PUBLIC_KEY_LEN);
  149. if (!forSign)
  150. b.append(_signature.data,ZT_C25519_SIGNATURE_LEN);
  151. b.append((uint8_t)_roots.size());
  152. for(std::vector<Root>::const_iterator r(_roots.begin());r!=_roots.end();++r) {
  153. r->identity.serialize(b);
  154. b.append((uint8_t)r->stableEndpoints.size());
  155. for(std::vector<InetAddress>::const_iterator ep(r->stableEndpoints.begin());ep!=r->stableEndpoints.end();++ep)
  156. ep->serialize(b);
  157. }
  158. if (_type == TYPE_MOON)
  159. b.append((uint16_t)0); // no attached dictionary (for future use)
  160. if (forSign) b.append((uint64_t)0xf7f7f7f7f7f7f7f7ULL);
  161. }
  162. template<unsigned int C>
  163. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  164. {
  165. unsigned int p = startAt;
  166. _roots.clear();
  167. switch((Type)b[p++]) {
  168. case TYPE_NULL: _type = TYPE_NULL; break; // shouldn't ever really happen in serialized data but it's not invalid
  169. case TYPE_PLANET: _type = TYPE_PLANET; break;
  170. case TYPE_MOON: _type = TYPE_MOON; break;
  171. default:
  172. throw std::invalid_argument("invalid world type");
  173. }
  174. _id = b.template at<uint64_t>(p); p += 8;
  175. _ts = b.template at<uint64_t>(p); p += 8;
  176. memcpy(_updatesMustBeSignedBy.data,b.field(p,ZT_C25519_PUBLIC_KEY_LEN),ZT_C25519_PUBLIC_KEY_LEN); p += ZT_C25519_PUBLIC_KEY_LEN;
  177. memcpy(_signature.data,b.field(p,ZT_C25519_SIGNATURE_LEN),ZT_C25519_SIGNATURE_LEN); p += ZT_C25519_SIGNATURE_LEN;
  178. const unsigned int numRoots = (unsigned int)b[p++];
  179. if (numRoots > ZT_WORLD_MAX_ROOTS)
  180. throw std::invalid_argument("too many roots in World");
  181. for(unsigned int k=0;k<numRoots;++k) {
  182. _roots.push_back(Root());
  183. Root &r = _roots.back();
  184. p += r.identity.deserialize(b,p);
  185. unsigned int numStableEndpoints = b[p++];
  186. if (numStableEndpoints > ZT_WORLD_MAX_STABLE_ENDPOINTS_PER_ROOT)
  187. throw std::invalid_argument("too many stable endpoints in World/Root");
  188. for(unsigned int kk=0;kk<numStableEndpoints;++kk) {
  189. r.stableEndpoints.push_back(InetAddress());
  190. p += r.stableEndpoints.back().deserialize(b,p);
  191. }
  192. }
  193. if (_type == TYPE_MOON)
  194. p += b.template at<uint16_t>(p) + 2;
  195. return (p - startAt);
  196. }
  197. 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)); }
  198. inline bool operator!=(const World &w) const { return (!(*this == w)); }
  199. inline bool operator<(const World &w) const { return (((int)_type < (int)w._type) ? true : ((_type == w._type) ? (_id < w._id) : false)); }
  200. /**
  201. * Create a World object signed with a key pair
  202. *
  203. * @param t World type
  204. * @param id World ID
  205. * @param ts World timestamp / revision
  206. * @param sk Key that must be used to sign the next future update to this world
  207. * @param roots Roots and their stable endpoints
  208. * @param signWith Key to sign this World with (can have the same public as the next-update signing key, but doesn't have to)
  209. * @return Signed World object
  210. */
  211. 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)
  212. {
  213. World w;
  214. w._id = id;
  215. w._ts = ts;
  216. w._type = t;
  217. w._updatesMustBeSignedBy = sk;
  218. w._roots = roots;
  219. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> tmp;
  220. w.serialize(tmp,true);
  221. w._signature = C25519::sign(signWith,tmp.data(),tmp.size());
  222. return w;
  223. }
  224. protected:
  225. uint64_t _id;
  226. uint64_t _ts;
  227. Type _type;
  228. C25519::Public _updatesMustBeSignedBy;
  229. C25519::Signature _signature;
  230. std::vector<Root> _roots;
  231. };
  232. } // namespace ZeroTier
  233. #endif