Topology.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include "Topology.hpp"
  14. #include "Buffer.hpp"
  15. #include "Constants.hpp"
  16. #include "Network.hpp"
  17. #include "NetworkConfig.hpp"
  18. #include "Node.hpp"
  19. #include "RuntimeEnvironment.hpp"
  20. #include "Switch.hpp"
  21. namespace ZeroTier {
  22. #define ZT_DEFAULT_WORLD_LENGTH 570
  23. static const unsigned char ZT_DEFAULT_WORLD[ZT_DEFAULT_WORLD_LENGTH] = {
  24. 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0xea, 0xc9, 0x0a, 0x00, 0x00, 0x01, 0x7e, 0xe9, 0x57, 0x60, 0xcd, 0xb8, 0xb3, 0x88, 0xa4, 0x69, 0x22, 0x14, 0x91, 0xaa, 0x9a, 0xcd, 0x66, 0xcc, 0x76, 0x4c, 0xde, 0xfd, 0x56, 0x03, 0x9f, 0x10,
  25. 0x67, 0xae, 0x15, 0xe6, 0x9c, 0x6f, 0xb4, 0x2d, 0x7b, 0x55, 0x33, 0x0e, 0x3f, 0xda, 0xac, 0x52, 0x9c, 0x07, 0x92, 0xfd, 0x73, 0x40, 0xa6, 0xaa, 0x21, 0xab, 0xa8, 0xa4, 0x89, 0xfd, 0xae, 0xa4, 0x4a, 0x39, 0xbf, 0x2d, 0x00, 0x65,
  26. 0x9a, 0xc9, 0xc8, 0x18, 0xeb, 0x36, 0x00, 0x92, 0x76, 0x37, 0xef, 0x4d, 0x14, 0x04, 0xa4, 0x4d, 0x54, 0x46, 0x84, 0x85, 0x13, 0x79, 0x75, 0x1f, 0xaa, 0x79, 0xb4, 0xc4, 0xea, 0x85, 0x04, 0x01, 0x75, 0xea, 0x06, 0x58, 0x60, 0x48,
  27. 0x24, 0x02, 0xe1, 0xeb, 0x34, 0x20, 0x52, 0x00, 0x0e, 0x62, 0x90, 0x06, 0x1a, 0x9b, 0xe0, 0xcd, 0x29, 0x3c, 0x8b, 0x55, 0xf1, 0xc3, 0xd2, 0x52, 0x48, 0x08, 0xaf, 0xc5, 0x49, 0x22, 0x08, 0x0e, 0x35, 0x39, 0xa7, 0x5a, 0xdd, 0xc3,
  28. 0xce, 0xf0, 0xf6, 0xad, 0x26, 0x0d, 0x58, 0x82, 0x93, 0xbb, 0x77, 0x86, 0xe7, 0x1e, 0xfa, 0x4b, 0x90, 0x57, 0xda, 0xd9, 0x86, 0x7a, 0xfe, 0x12, 0xdd, 0x04, 0xca, 0xfe, 0x9e, 0xfe, 0xb9, 0x00, 0xcc, 0xde, 0xf7, 0x6b, 0xc7, 0xb9,
  29. 0x7d, 0xed, 0x90, 0x4e, 0xab, 0xc5, 0xdf, 0x09, 0x88, 0x6d, 0x9c, 0x15, 0x14, 0xa6, 0x10, 0x03, 0x6c, 0xb9, 0x13, 0x9c, 0xc2, 0x14, 0x00, 0x1a, 0x29, 0x58, 0x97, 0x8e, 0xfc, 0xec, 0x15, 0x71, 0x2d, 0xd3, 0x94, 0x8c, 0x6e, 0x6b,
  30. 0x3a, 0x8e, 0x89, 0x3d, 0xf0, 0x1f, 0xf4, 0x93, 0xd1, 0xf8, 0xd9, 0x80, 0x6a, 0x86, 0x0c, 0x54, 0x20, 0x57, 0x1b, 0xf0, 0x00, 0x02, 0x04, 0x68, 0xc2, 0x08, 0x86, 0x27, 0x09, 0x06, 0x26, 0x05, 0x98, 0x80, 0x02, 0x00, 0x12, 0x00,
  31. 0x00, 0x30, 0x05, 0x71, 0x0e, 0x34, 0x00, 0x51, 0x27, 0x09, 0x77, 0x8c, 0xde, 0x71, 0x90, 0x00, 0x3f, 0x66, 0x81, 0xa9, 0x9e, 0x5a, 0xd1, 0x89, 0x5e, 0x9f, 0xba, 0x33, 0xe6, 0x21, 0x2d, 0x44, 0x54, 0xe1, 0x68, 0xbc, 0xec, 0x71,
  32. 0x12, 0x10, 0x1b, 0xf0, 0x00, 0x95, 0x6e, 0xd8, 0xe9, 0x2e, 0x42, 0x89, 0x2c, 0xb6, 0xf2, 0xec, 0x41, 0x08, 0x81, 0xa8, 0x4a, 0xb1, 0x9d, 0xa5, 0x0e, 0x12, 0x87, 0xba, 0x3d, 0x92, 0x6c, 0x3a, 0x1f, 0x75, 0x5c, 0xcc, 0xf2, 0x99,
  33. 0xa1, 0x20, 0x70, 0x55, 0x00, 0x02, 0x04, 0x67, 0xc3, 0x67, 0x42, 0x27, 0x09, 0x06, 0x26, 0x05, 0x98, 0x80, 0x04, 0x00, 0x00, 0xc3, 0x02, 0x54, 0xf2, 0xbc, 0xa1, 0xf7, 0x00, 0x19, 0x27, 0x09, 0x62, 0xf8, 0x65, 0xae, 0x71, 0x00,
  34. 0xe2, 0x07, 0x6c, 0x57, 0xde, 0x87, 0x0e, 0x62, 0x88, 0xd7, 0xd5, 0xe7, 0x40, 0x44, 0x08, 0xb1, 0x54, 0x5e, 0xfc, 0xa3, 0x7d, 0x67, 0xf7, 0x7b, 0x87, 0xe9, 0xe5, 0x41, 0x68, 0xc2, 0x5d, 0x3e, 0xf1, 0xa9, 0xab, 0xf2, 0x90, 0x5e,
  35. 0xa5, 0xe7, 0x85, 0xc0, 0x1d, 0xff, 0x23, 0x88, 0x7a, 0xd4, 0x23, 0x2d, 0x95, 0xc7, 0xa8, 0xfd, 0x2c, 0x27, 0x11, 0x1a, 0x72, 0xbd, 0x15, 0x93, 0x22, 0xdc, 0x00, 0x02, 0x04, 0x32, 0x07, 0xfc, 0x8a, 0x27, 0x09, 0x06, 0x20, 0x01,
  36. 0x49, 0xf0, 0xd0, 0xdb, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x27, 0x09, 0xca, 0xfe, 0x04, 0xeb, 0xa9, 0x00, 0x6c, 0x6a, 0x9d, 0x1d, 0xea, 0x55, 0xc1, 0x61, 0x6b, 0xfe, 0x2a, 0x2b, 0x8f, 0x0f, 0xf9, 0xa8,
  37. 0xca, 0xca, 0xf7, 0x03, 0x74, 0xfb, 0x1f, 0x39, 0xe3, 0xbe, 0xf8, 0x1c, 0xbf, 0xeb, 0xef, 0x17, 0xb7, 0x22, 0x82, 0x68, 0xa0, 0xa2, 0xa2, 0x9d, 0x34, 0x88, 0xc7, 0x52, 0x56, 0x5c, 0x6c, 0x96, 0x5c, 0xbd, 0x65, 0x06, 0xec, 0x24,
  38. 0x39, 0x7c, 0xc8, 0xa5, 0xd9, 0xd1, 0x52, 0x85, 0xa8, 0x7f, 0x00, 0x02, 0x04, 0x54, 0x11, 0x35, 0x9b, 0x27, 0x09, 0x06, 0x2a, 0x02, 0x6e, 0xa0, 0xd4, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x99, 0x93, 0x27, 0x09
  39. };
  40. Topology::Topology(const RuntimeEnvironment* renv, void* tPtr) : RR(renv), _numConfiguredPhysicalPaths(0), _amUpstream(false)
  41. {
  42. uint8_t tmp[ZT_WORLD_MAX_SERIALIZED_LENGTH];
  43. uint64_t idtmp[2];
  44. idtmp[0] = 0;
  45. idtmp[1] = 0;
  46. int n = RR->node->stateObjectGet(tPtr, ZT_STATE_OBJECT_PLANET, idtmp, tmp, sizeof(tmp));
  47. if (n > 0) {
  48. try {
  49. World cachedPlanet;
  50. cachedPlanet.deserialize(Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH>(tmp, (unsigned int)n), 0);
  51. addWorld(tPtr, cachedPlanet, false);
  52. }
  53. catch (...) {
  54. } // ignore invalid cached planets
  55. }
  56. World defaultPlanet;
  57. {
  58. Buffer<ZT_DEFAULT_WORLD_LENGTH> wtmp(ZT_DEFAULT_WORLD, ZT_DEFAULT_WORLD_LENGTH);
  59. defaultPlanet.deserialize(wtmp, 0); // throws on error, which would indicate a bad static variable up top
  60. }
  61. addWorld(tPtr, defaultPlanet, false);
  62. }
  63. Topology::~Topology()
  64. {
  65. Hashtable<Address, SharedPtr<Peer> >::Iterator i(_peers);
  66. Address* a = (Address*)0;
  67. SharedPtr<Peer>* p = (SharedPtr<Peer>*)0;
  68. while (i.next(a, p)) {
  69. _savePeer((void*)0, *p);
  70. }
  71. }
  72. SharedPtr<Peer> Topology::addPeer(void* tPtr, const SharedPtr<Peer>& peer)
  73. {
  74. SharedPtr<Peer> np;
  75. {
  76. Mutex::Lock _l(_peers_m);
  77. SharedPtr<Peer>& hp = _peers[peer->address()];
  78. if (! hp) {
  79. hp = peer;
  80. }
  81. np = hp;
  82. }
  83. return np;
  84. }
  85. SharedPtr<Peer> Topology::getPeer(void* tPtr, const Address& zta)
  86. {
  87. if (zta == RR->identity.address()) {
  88. return SharedPtr<Peer>();
  89. }
  90. {
  91. Mutex::Lock _l(_peers_m);
  92. const SharedPtr<Peer>* const ap = _peers.get(zta);
  93. if (ap) {
  94. return *ap;
  95. }
  96. }
  97. try {
  98. Buffer<ZT_PEER_MAX_SERIALIZED_STATE_SIZE> buf;
  99. uint64_t idbuf[2];
  100. idbuf[0] = zta.toInt();
  101. idbuf[1] = 0;
  102. int len = RR->node->stateObjectGet(tPtr, ZT_STATE_OBJECT_PEER, idbuf, buf.unsafeData(), ZT_PEER_MAX_SERIALIZED_STATE_SIZE);
  103. if (len > 0) {
  104. buf.setSize(len);
  105. Mutex::Lock _l(_peers_m);
  106. SharedPtr<Peer>& ap = _peers[zta];
  107. if (ap) {
  108. return ap;
  109. }
  110. ap = Peer::deserializeFromCache(RR->node->now(), tPtr, buf, RR);
  111. if (! ap) {
  112. _peers.erase(zta);
  113. }
  114. return SharedPtr<Peer>();
  115. }
  116. }
  117. catch (...) {
  118. } // ignore invalid identities or other strange failures
  119. return SharedPtr<Peer>();
  120. }
  121. Identity Topology::getIdentity(void* tPtr, const Address& zta)
  122. {
  123. if (zta == RR->identity.address()) {
  124. return RR->identity;
  125. }
  126. else {
  127. Mutex::Lock _l(_peers_m);
  128. const SharedPtr<Peer>* const ap = _peers.get(zta);
  129. if (ap) {
  130. return (*ap)->identity();
  131. }
  132. }
  133. return Identity();
  134. }
  135. SharedPtr<Peer> Topology::getUpstreamPeer()
  136. {
  137. const int64_t now = RR->node->now();
  138. unsigned int bestq = ~((unsigned int)0);
  139. const SharedPtr<Peer>* best = (const SharedPtr<Peer>*)0;
  140. Mutex::Lock _l2(_peers_m);
  141. Mutex::Lock _l1(_upstreams_m);
  142. for (std::vector<Address>::const_iterator a(_upstreamAddresses.begin()); a != _upstreamAddresses.end(); ++a) {
  143. const SharedPtr<Peer>* p = _peers.get(*a);
  144. if (p) {
  145. const unsigned int q = (*p)->relayQuality(now);
  146. if (q <= bestq) {
  147. bestq = q;
  148. best = p;
  149. }
  150. }
  151. }
  152. if (! best) {
  153. return SharedPtr<Peer>();
  154. }
  155. return *best;
  156. }
  157. bool Topology::isUpstream(const Identity& id) const
  158. {
  159. Mutex::Lock _l(_upstreams_m);
  160. return (std::find(_upstreamAddresses.begin(), _upstreamAddresses.end(), id.address()) != _upstreamAddresses.end());
  161. }
  162. bool Topology::shouldAcceptWorldUpdateFrom(const Address& addr) const
  163. {
  164. Mutex::Lock _l(_upstreams_m);
  165. if (std::find(_upstreamAddresses.begin(), _upstreamAddresses.end(), addr) != _upstreamAddresses.end()) {
  166. return true;
  167. }
  168. for (std::vector<std::pair<uint64_t, Address> >::const_iterator s(_moonSeeds.begin()); s != _moonSeeds.end(); ++s) {
  169. if (s->second == addr) {
  170. return true;
  171. }
  172. }
  173. return false;
  174. }
  175. ZT_PeerRole Topology::role(const Address& ztaddr) const
  176. {
  177. Mutex::Lock _l(_upstreams_m);
  178. if (std::find(_upstreamAddresses.begin(), _upstreamAddresses.end(), ztaddr) != _upstreamAddresses.end()) {
  179. for (std::vector<World::Root>::const_iterator i(_planet.roots().begin()); i != _planet.roots().end(); ++i) {
  180. if (i->identity.address() == ztaddr) {
  181. return ZT_PEER_ROLE_PLANET;
  182. }
  183. }
  184. return ZT_PEER_ROLE_MOON;
  185. }
  186. return ZT_PEER_ROLE_LEAF;
  187. }
  188. bool Topology::isProhibitedEndpoint(const Address& ztaddr, const InetAddress& ipaddr) const
  189. {
  190. Mutex::Lock _l(_upstreams_m);
  191. // For roots the only permitted addresses are those defined. This adds just a little
  192. // bit of extra security against spoofing, replaying, etc.
  193. if (std::find(_upstreamAddresses.begin(), _upstreamAddresses.end(), ztaddr) != _upstreamAddresses.end()) {
  194. for (std::vector<World::Root>::const_iterator r(_planet.roots().begin()); r != _planet.roots().end(); ++r) {
  195. if (r->identity.address() == ztaddr) {
  196. if (r->stableEndpoints.empty()) {
  197. return false; // no stable endpoints specified, so allow dynamic paths
  198. }
  199. for (std::vector<InetAddress>::const_iterator e(r->stableEndpoints.begin()); e != r->stableEndpoints.end(); ++e) {
  200. if (ipaddr.ipsEqual(*e)) {
  201. return false;
  202. }
  203. }
  204. }
  205. }
  206. for (std::vector<World>::const_iterator m(_moons.begin()); m != _moons.end(); ++m) {
  207. for (std::vector<World::Root>::const_iterator r(m->roots().begin()); r != m->roots().end(); ++r) {
  208. if (r->identity.address() == ztaddr) {
  209. if (r->stableEndpoints.empty()) {
  210. return false; // no stable endpoints specified, so allow dynamic paths
  211. }
  212. for (std::vector<InetAddress>::const_iterator e(r->stableEndpoints.begin()); e != r->stableEndpoints.end(); ++e) {
  213. if (ipaddr.ipsEqual(*e)) {
  214. return false;
  215. }
  216. }
  217. }
  218. }
  219. }
  220. return true;
  221. }
  222. return false;
  223. }
  224. bool Topology::addWorld(void* tPtr, const World& newWorld, bool alwaysAcceptNew)
  225. {
  226. if ((newWorld.type() != World::TYPE_PLANET) && (newWorld.type() != World::TYPE_MOON)) {
  227. return false;
  228. }
  229. Mutex::Lock _l2(_peers_m);
  230. Mutex::Lock _l1(_upstreams_m);
  231. World* existing = (World*)0;
  232. switch (newWorld.type()) {
  233. case World::TYPE_PLANET:
  234. existing = &_planet;
  235. break;
  236. case World::TYPE_MOON:
  237. for (std::vector<World>::iterator m(_moons.begin()); m != _moons.end(); ++m) {
  238. if (m->id() == newWorld.id()) {
  239. existing = &(*m);
  240. break;
  241. }
  242. }
  243. break;
  244. default:
  245. return false;
  246. }
  247. if (existing) {
  248. if (existing->shouldBeReplacedBy(newWorld)) {
  249. *existing = newWorld;
  250. }
  251. else {
  252. return false;
  253. }
  254. }
  255. else if (newWorld.type() == World::TYPE_MOON) {
  256. if (alwaysAcceptNew) {
  257. _moons.push_back(newWorld);
  258. existing = &(_moons.back());
  259. }
  260. else {
  261. for (std::vector<std::pair<uint64_t, Address> >::iterator m(_moonSeeds.begin()); m != _moonSeeds.end(); ++m) {
  262. if (m->first == newWorld.id()) {
  263. for (std::vector<World::Root>::const_iterator r(newWorld.roots().begin()); r != newWorld.roots().end(); ++r) {
  264. if (r->identity.address() == m->second) {
  265. _moonSeeds.erase(m);
  266. _moons.push_back(newWorld);
  267. existing = &(_moons.back());
  268. break;
  269. }
  270. }
  271. if (existing) {
  272. break;
  273. }
  274. }
  275. }
  276. }
  277. if (! existing) {
  278. return false;
  279. }
  280. }
  281. else {
  282. return false;
  283. }
  284. try {
  285. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> sbuf;
  286. existing->serialize(sbuf, false);
  287. uint64_t idtmp[2];
  288. idtmp[0] = existing->id();
  289. idtmp[1] = 0;
  290. RR->node->stateObjectPut(tPtr, (existing->type() == World::TYPE_PLANET) ? ZT_STATE_OBJECT_PLANET : ZT_STATE_OBJECT_MOON, idtmp, sbuf.data(), sbuf.size());
  291. }
  292. catch (...) {
  293. }
  294. _memoizeUpstreams(tPtr);
  295. return true;
  296. }
  297. void Topology::addMoon(void* tPtr, const uint64_t id, const Address& seed)
  298. {
  299. char tmp[ZT_WORLD_MAX_SERIALIZED_LENGTH];
  300. uint64_t idtmp[2];
  301. idtmp[0] = id;
  302. idtmp[1] = 0;
  303. int n = RR->node->stateObjectGet(tPtr, ZT_STATE_OBJECT_MOON, idtmp, tmp, sizeof(tmp));
  304. if (n > 0) {
  305. try {
  306. World w;
  307. w.deserialize(Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH>(tmp, (unsigned int)n));
  308. if ((w.type() == World::TYPE_MOON) && (w.id() == id)) {
  309. addWorld(tPtr, w, true);
  310. return;
  311. }
  312. }
  313. catch (...) {
  314. }
  315. }
  316. if (seed) {
  317. Mutex::Lock _l(_upstreams_m);
  318. if (std::find(_moonSeeds.begin(), _moonSeeds.end(), std::pair<uint64_t, Address>(id, seed)) == _moonSeeds.end()) {
  319. _moonSeeds.push_back(std::pair<uint64_t, Address>(id, seed));
  320. }
  321. }
  322. }
  323. void Topology::removeMoon(void* tPtr, const uint64_t id)
  324. {
  325. Mutex::Lock _l2(_peers_m);
  326. Mutex::Lock _l1(_upstreams_m);
  327. std::vector<World> nm;
  328. for (std::vector<World>::const_iterator m(_moons.begin()); m != _moons.end(); ++m) {
  329. if (m->id() != id) {
  330. nm.push_back(*m);
  331. }
  332. else {
  333. uint64_t idtmp[2];
  334. idtmp[0] = id;
  335. idtmp[1] = 0;
  336. RR->node->stateObjectDelete(tPtr, ZT_STATE_OBJECT_MOON, idtmp);
  337. }
  338. }
  339. _moons.swap(nm);
  340. std::vector<std::pair<uint64_t, Address> > cm;
  341. for (std::vector<std::pair<uint64_t, Address> >::const_iterator m(_moonSeeds.begin()); m != _moonSeeds.end(); ++m) {
  342. if (m->first != id) {
  343. cm.push_back(*m);
  344. }
  345. }
  346. _moonSeeds.swap(cm);
  347. _memoizeUpstreams(tPtr);
  348. }
  349. void Topology::doPeriodicTasks(void* tPtr, int64_t now)
  350. {
  351. {
  352. Mutex::Lock _l1(_peers_m);
  353. Mutex::Lock _l2(_upstreams_m);
  354. Hashtable<Address, SharedPtr<Peer> >::Iterator i(_peers);
  355. Address* a = (Address*)0;
  356. SharedPtr<Peer>* p = (SharedPtr<Peer>*)0;
  357. while (i.next(a, p)) {
  358. if ((! (*p)->isAlive(now)) && (std::find(_upstreamAddresses.begin(), _upstreamAddresses.end(), *a) == _upstreamAddresses.end())) {
  359. _savePeer(tPtr, *p);
  360. _peers.erase(*a);
  361. }
  362. }
  363. }
  364. {
  365. Mutex::Lock _l(_paths_m);
  366. Hashtable<Path::HashKey, SharedPtr<Path> >::Iterator i(_paths);
  367. Path::HashKey* k = (Path::HashKey*)0;
  368. SharedPtr<Path>* p = (SharedPtr<Path>*)0;
  369. while (i.next(k, p)) {
  370. if (p->references() <= 1) {
  371. _paths.erase(*k);
  372. }
  373. }
  374. }
  375. }
  376. void Topology::_memoizeUpstreams(void* tPtr)
  377. {
  378. // assumes _upstreams_m and _peers_m are locked
  379. _upstreamAddresses.clear();
  380. _amUpstream = false;
  381. for (std::vector<World::Root>::const_iterator i(_planet.roots().begin()); i != _planet.roots().end(); ++i) {
  382. const Identity& id = i->identity;
  383. if (id == RR->identity) {
  384. _amUpstream = true;
  385. }
  386. else if (std::find(_upstreamAddresses.begin(), _upstreamAddresses.end(), id.address()) == _upstreamAddresses.end()) {
  387. _upstreamAddresses.push_back(id.address());
  388. SharedPtr<Peer>& hp = _peers[id.address()];
  389. if (! hp) {
  390. hp = new Peer(RR, RR->identity, id);
  391. }
  392. }
  393. }
  394. for (std::vector<World>::const_iterator m(_moons.begin()); m != _moons.end(); ++m) {
  395. for (std::vector<World::Root>::const_iterator i(m->roots().begin()); i != m->roots().end(); ++i) {
  396. if (i->identity == RR->identity) {
  397. _amUpstream = true;
  398. }
  399. else if (std::find(_upstreamAddresses.begin(), _upstreamAddresses.end(), i->identity.address()) == _upstreamAddresses.end()) {
  400. _upstreamAddresses.push_back(i->identity.address());
  401. SharedPtr<Peer>& hp = _peers[i->identity.address()];
  402. if (! hp) {
  403. hp = new Peer(RR, RR->identity, i->identity);
  404. }
  405. }
  406. }
  407. }
  408. std::sort(_upstreamAddresses.begin(), _upstreamAddresses.end());
  409. }
  410. void Topology::_savePeer(void* tPtr, const SharedPtr<Peer>& peer)
  411. {
  412. try {
  413. Buffer<ZT_PEER_MAX_SERIALIZED_STATE_SIZE> buf;
  414. peer->serializeForCache(buf);
  415. uint64_t tmpid[2];
  416. tmpid[0] = peer->address().toInt();
  417. tmpid[1] = 0;
  418. RR->node->stateObjectPut(tPtr, ZT_STATE_OBJECT_PEER, tmpid, buf.data(), buf.size());
  419. }
  420. catch (...) {
  421. } // sanity check, discard invalid entries
  422. }
  423. } // namespace ZeroTier