Topology.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 "Network.hpp"
  16. #include "Node.hpp"
  17. #include "RuntimeEnvironment.hpp"
  18. #include "Switch.hpp"
  19. #include "Trace.hpp"
  20. namespace ZeroTier {
  21. #define ZT_DEFAULT_WORLD_LENGTH 570
  22. static const unsigned char ZT_DEFAULT_WORLD[ZT_DEFAULT_WORLD_LENGTH] = {
  23. 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,
  24. 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,
  25. 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,
  26. 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,
  27. 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,
  28. 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,
  29. 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,
  30. 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,
  31. 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,
  32. 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,
  33. 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,
  34. 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,
  35. 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,
  36. 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,
  37. 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
  38. };
  39. Topology::Topology(const RuntimeEnvironment* renv, void* tPtr) : RR(renv), _numConfiguredPhysicalPaths(0), _amUpstream(false)
  40. {
  41. uint8_t tmp[ZT_WORLD_MAX_SERIALIZED_LENGTH];
  42. uint64_t idtmp[2];
  43. idtmp[0] = 0;
  44. idtmp[1] = 0;
  45. int n = RR->node->stateObjectGet(tPtr, ZT_STATE_OBJECT_PLANET, idtmp, tmp, sizeof(tmp));
  46. if (n > 0) {
  47. try {
  48. World cachedPlanet;
  49. cachedPlanet.deserialize(Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH>(tmp, (unsigned int)n), 0);
  50. addWorld(tPtr, cachedPlanet, false);
  51. }
  52. catch (...) {
  53. } // ignore invalid cached planets
  54. }
  55. World defaultPlanet;
  56. {
  57. Buffer<ZT_DEFAULT_WORLD_LENGTH> wtmp(ZT_DEFAULT_WORLD, ZT_DEFAULT_WORLD_LENGTH);
  58. defaultPlanet.deserialize(wtmp, 0); // throws on error, which would indicate a bad static variable up top
  59. }
  60. addWorld(tPtr, defaultPlanet, false);
  61. }
  62. Topology::~Topology()
  63. {
  64. Hashtable<Address, SharedPtr<Peer> >::Iterator i(_peers);
  65. Address* a = (Address*)0;
  66. SharedPtr<Peer>* p = (SharedPtr<Peer>*)0;
  67. while (i.next(a, p)) {
  68. _savePeer((void*)0, *p);
  69. }
  70. }
  71. SharedPtr<Peer> Topology::addPeer(void* tPtr, const SharedPtr<Peer>& peer)
  72. {
  73. SharedPtr<Peer> np;
  74. {
  75. Mutex::Lock _l(_peers_m);
  76. SharedPtr<Peer>& hp = _peers[peer->address()];
  77. if (! hp) {
  78. hp = peer;
  79. }
  80. np = hp;
  81. }
  82. return np;
  83. }
  84. SharedPtr<Peer> Topology::getPeer(void* tPtr, const Address& zta)
  85. {
  86. if (zta == RR->identity.address()) {
  87. return SharedPtr<Peer>();
  88. }
  89. {
  90. Mutex::Lock _l(_peers_m);
  91. const SharedPtr<Peer>* const ap = _peers.get(zta);
  92. if (ap) {
  93. return *ap;
  94. }
  95. }
  96. try {
  97. Buffer<ZT_PEER_MAX_SERIALIZED_STATE_SIZE> buf;
  98. uint64_t idbuf[2];
  99. idbuf[0] = zta.toInt();
  100. idbuf[1] = 0;
  101. int len = RR->node->stateObjectGet(tPtr, ZT_STATE_OBJECT_PEER, idbuf, buf.unsafeData(), ZT_PEER_MAX_SERIALIZED_STATE_SIZE);
  102. if (len > 0) {
  103. buf.setSize(len);
  104. Mutex::Lock _l(_peers_m);
  105. SharedPtr<Peer>& ap = _peers[zta];
  106. if (ap) {
  107. return ap;
  108. }
  109. ap = Peer::deserializeFromCache(RR->node->now(), tPtr, buf, RR);
  110. if (! ap) {
  111. _peers.erase(zta);
  112. }
  113. return SharedPtr<Peer>();
  114. }
  115. }
  116. catch (...) {
  117. } // ignore invalid identities or other strange failures
  118. return SharedPtr<Peer>();
  119. }
  120. Identity Topology::getIdentity(void* tPtr, const Address& zta)
  121. {
  122. if (zta == RR->identity.address()) {
  123. return RR->identity;
  124. }
  125. else {
  126. Mutex::Lock _l(_peers_m);
  127. const SharedPtr<Peer>* const ap = _peers.get(zta);
  128. if (ap) {
  129. return (*ap)->identity();
  130. }
  131. }
  132. return Identity();
  133. }
  134. SharedPtr<Peer> Topology::getUpstreamPeer(const uint64_t nwid)
  135. {
  136. const int64_t now = RR->node->now();
  137. unsigned int bestq = ~((unsigned int)0);
  138. const SharedPtr<Peer>* best = (const SharedPtr<Peer>*)0;
  139. // If this is related to a network, check for a network specific relay.
  140. if (nwid) {
  141. SharedPtr<Network> network = RR->node->network(nwid);
  142. if (network) {
  143. //
  144. }
  145. }
  146. // If this is unrelated to a network OR there is no network-specific relay, send via a root.
  147. {
  148. Mutex::Lock _l2(_peers_m);
  149. Mutex::Lock _l1(_upstreams_m);
  150. for (std::vector<Address>::const_iterator a(_upstreamAddresses.begin()); a != _upstreamAddresses.end(); ++a) {
  151. const SharedPtr<Peer>* p = _peers.get(*a);
  152. if (p) {
  153. const unsigned int q = (*p)->relayQuality(now);
  154. if (q <= bestq) {
  155. bestq = q;
  156. best = p;
  157. }
  158. }
  159. }
  160. if (best) {
  161. return *best;
  162. }
  163. }
  164. return SharedPtr<Peer>();
  165. }
  166. bool Topology::isUpstream(const Identity& id) const
  167. {
  168. Mutex::Lock _l(_upstreams_m);
  169. return (std::find(_upstreamAddresses.begin(), _upstreamAddresses.end(), id.address()) != _upstreamAddresses.end());
  170. }
  171. bool Topology::shouldAcceptWorldUpdateFrom(const Address& addr) const
  172. {
  173. Mutex::Lock _l(_upstreams_m);
  174. if (std::find(_upstreamAddresses.begin(), _upstreamAddresses.end(), addr) != _upstreamAddresses.end()) {
  175. return true;
  176. }
  177. for (std::vector<std::pair<uint64_t, Address> >::const_iterator s(_moonSeeds.begin()); s != _moonSeeds.end(); ++s) {
  178. if (s->second == addr) {
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. ZT_PeerRole Topology::role(const Address& ztaddr) const
  185. {
  186. Mutex::Lock _l(_upstreams_m);
  187. if (std::find(_upstreamAddresses.begin(), _upstreamAddresses.end(), ztaddr) != _upstreamAddresses.end()) {
  188. for (std::vector<World::Root>::const_iterator i(_planet.roots().begin()); i != _planet.roots().end(); ++i) {
  189. if (i->identity.address() == ztaddr) {
  190. return ZT_PEER_ROLE_PLANET;
  191. }
  192. }
  193. return ZT_PEER_ROLE_MOON;
  194. }
  195. return ZT_PEER_ROLE_LEAF;
  196. }
  197. bool Topology::isProhibitedEndpoint(const Address& ztaddr, const InetAddress& ipaddr) const
  198. {
  199. Mutex::Lock _l(_upstreams_m);
  200. // For roots the only permitted addresses are those defined. This adds just a little
  201. // bit of extra security against spoofing, replaying, etc.
  202. if (std::find(_upstreamAddresses.begin(), _upstreamAddresses.end(), ztaddr) != _upstreamAddresses.end()) {
  203. for (std::vector<World::Root>::const_iterator r(_planet.roots().begin()); r != _planet.roots().end(); ++r) {
  204. if (r->identity.address() == ztaddr) {
  205. if (r->stableEndpoints.empty()) {
  206. return false; // no stable endpoints specified, so allow dynamic paths
  207. }
  208. for (std::vector<InetAddress>::const_iterator e(r->stableEndpoints.begin()); e != r->stableEndpoints.end(); ++e) {
  209. if (ipaddr.ipsEqual(*e)) {
  210. return false;
  211. }
  212. }
  213. }
  214. }
  215. for (std::vector<World>::const_iterator m(_moons.begin()); m != _moons.end(); ++m) {
  216. for (std::vector<World::Root>::const_iterator r(m->roots().begin()); r != m->roots().end(); ++r) {
  217. if (r->identity.address() == ztaddr) {
  218. if (r->stableEndpoints.empty()) {
  219. return false; // no stable endpoints specified, so allow dynamic paths
  220. }
  221. for (std::vector<InetAddress>::const_iterator e(r->stableEndpoints.begin()); e != r->stableEndpoints.end(); ++e) {
  222. if (ipaddr.ipsEqual(*e)) {
  223. return false;
  224. }
  225. }
  226. }
  227. }
  228. }
  229. return true;
  230. }
  231. return false;
  232. }
  233. void Topology::getRootsToContact(Hashtable<Address, std::vector<InetAddress> >& eps) const
  234. {
  235. Mutex::Lock _l(_upstreams_m);
  236. for (std::vector<World::Root>::const_iterator i(_planet.roots().begin()); i != _planet.roots().end(); ++i) {
  237. if (i->identity != RR->identity) {
  238. std::vector<InetAddress>& ips = eps[i->identity.address()];
  239. for (std::vector<InetAddress>::const_iterator j(i->stableEndpoints.begin()); j != i->stableEndpoints.end(); ++j) {
  240. if (std::find(ips.begin(), ips.end(), *j) == ips.end()) {
  241. ips.push_back(*j);
  242. }
  243. }
  244. }
  245. }
  246. for (std::vector<World>::const_iterator m(_moons.begin()); m != _moons.end(); ++m) {
  247. for (std::vector<World::Root>::const_iterator i(m->roots().begin()); i != m->roots().end(); ++i) {
  248. if (i->identity != RR->identity) {
  249. std::vector<InetAddress>& ips = eps[i->identity.address()];
  250. for (std::vector<InetAddress>::const_iterator j(i->stableEndpoints.begin()); j != i->stableEndpoints.end(); ++j) {
  251. if (std::find(ips.begin(), ips.end(), *j) == ips.end()) {
  252. ips.push_back(*j);
  253. }
  254. }
  255. }
  256. }
  257. }
  258. for (std::vector<std::pair<uint64_t, Address> >::const_iterator m(_moonSeeds.begin()); m != _moonSeeds.end(); ++m) {
  259. eps[m->second];
  260. }
  261. }
  262. bool Topology::addWorld(void* tPtr, const World& newWorld, bool alwaysAcceptNew)
  263. {
  264. if ((newWorld.type() != World::TYPE_PLANET) && (newWorld.type() != World::TYPE_MOON)) {
  265. return false;
  266. }
  267. Mutex::Lock _l2(_peers_m);
  268. Mutex::Lock _l1(_upstreams_m);
  269. World* existing = (World*)0;
  270. switch (newWorld.type()) {
  271. case World::TYPE_PLANET:
  272. existing = &_planet;
  273. break;
  274. case World::TYPE_MOON:
  275. for (std::vector<World>::iterator m(_moons.begin()); m != _moons.end(); ++m) {
  276. if (m->id() == newWorld.id()) {
  277. existing = &(*m);
  278. break;
  279. }
  280. }
  281. break;
  282. default:
  283. return false;
  284. }
  285. if (existing) {
  286. if (existing->shouldBeReplacedBy(newWorld)) {
  287. *existing = newWorld;
  288. }
  289. else {
  290. return false;
  291. }
  292. }
  293. else if (newWorld.type() == World::TYPE_MOON) {
  294. if (alwaysAcceptNew) {
  295. _moons.push_back(newWorld);
  296. existing = &(_moons.back());
  297. }
  298. else {
  299. for (std::vector<std::pair<uint64_t, Address> >::iterator m(_moonSeeds.begin()); m != _moonSeeds.end(); ++m) {
  300. if (m->first == newWorld.id()) {
  301. for (std::vector<World::Root>::const_iterator r(newWorld.roots().begin()); r != newWorld.roots().end(); ++r) {
  302. if (r->identity.address() == m->second) {
  303. _moonSeeds.erase(m);
  304. _moons.push_back(newWorld);
  305. existing = &(_moons.back());
  306. break;
  307. }
  308. }
  309. if (existing) {
  310. break;
  311. }
  312. }
  313. }
  314. }
  315. if (! existing) {
  316. return false;
  317. }
  318. }
  319. else {
  320. return false;
  321. }
  322. try {
  323. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> sbuf;
  324. existing->serialize(sbuf, false);
  325. uint64_t idtmp[2];
  326. idtmp[0] = existing->id();
  327. idtmp[1] = 0;
  328. RR->node->stateObjectPut(tPtr, (existing->type() == World::TYPE_PLANET) ? ZT_STATE_OBJECT_PLANET : ZT_STATE_OBJECT_MOON, idtmp, sbuf.data(), sbuf.size());
  329. }
  330. catch (...) {
  331. }
  332. _memoizeUpstreams(tPtr);
  333. return true;
  334. }
  335. void Topology::addMoon(void* tPtr, const uint64_t id, const Address& seed)
  336. {
  337. char tmp[ZT_WORLD_MAX_SERIALIZED_LENGTH];
  338. uint64_t idtmp[2];
  339. idtmp[0] = id;
  340. idtmp[1] = 0;
  341. int n = RR->node->stateObjectGet(tPtr, ZT_STATE_OBJECT_MOON, idtmp, tmp, sizeof(tmp));
  342. if (n > 0) {
  343. try {
  344. World w;
  345. w.deserialize(Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH>(tmp, (unsigned int)n));
  346. if ((w.type() == World::TYPE_MOON) && (w.id() == id)) {
  347. addWorld(tPtr, w, true);
  348. return;
  349. }
  350. }
  351. catch (...) {
  352. }
  353. }
  354. if (seed) {
  355. Mutex::Lock _l(_upstreams_m);
  356. if (std::find(_moonSeeds.begin(), _moonSeeds.end(), std::pair<uint64_t, Address>(id, seed)) == _moonSeeds.end()) {
  357. _moonSeeds.push_back(std::pair<uint64_t, Address>(id, seed));
  358. }
  359. }
  360. }
  361. void Topology::removeMoon(void* tPtr, const uint64_t id)
  362. {
  363. Mutex::Lock _l2(_peers_m);
  364. Mutex::Lock _l1(_upstreams_m);
  365. std::vector<World> nm;
  366. for (std::vector<World>::const_iterator m(_moons.begin()); m != _moons.end(); ++m) {
  367. if (m->id() != id) {
  368. nm.push_back(*m);
  369. }
  370. else {
  371. uint64_t idtmp[2];
  372. idtmp[0] = id;
  373. idtmp[1] = 0;
  374. RR->node->stateObjectDelete(tPtr, ZT_STATE_OBJECT_MOON, idtmp);
  375. }
  376. }
  377. _moons.swap(nm);
  378. std::vector<std::pair<uint64_t, Address> > cm;
  379. for (std::vector<std::pair<uint64_t, Address> >::const_iterator m(_moonSeeds.begin()); m != _moonSeeds.end(); ++m) {
  380. if (m->first != id) {
  381. cm.push_back(*m);
  382. }
  383. }
  384. _moonSeeds.swap(cm);
  385. _memoizeUpstreams(tPtr);
  386. }
  387. void Topology::doPeriodicTasks(void* tPtr, int64_t now)
  388. {
  389. {
  390. Mutex::Lock _l1(_peers_m);
  391. Mutex::Lock _l2(_upstreams_m);
  392. Hashtable<Address, SharedPtr<Peer> >::Iterator i(_peers);
  393. Address* a = (Address*)0;
  394. SharedPtr<Peer>* p = (SharedPtr<Peer>*)0;
  395. while (i.next(a, p)) {
  396. if ((! (*p)->isAlive(now)) && (std::find(_upstreamAddresses.begin(), _upstreamAddresses.end(), *a) == _upstreamAddresses.end())) {
  397. _savePeer(tPtr, *p);
  398. _peers.erase(*a);
  399. }
  400. }
  401. }
  402. {
  403. Mutex::Lock _l(_paths_m);
  404. Hashtable<Path::HashKey, SharedPtr<Path> >::Iterator i(_paths);
  405. Path::HashKey* k = (Path::HashKey*)0;
  406. SharedPtr<Path>* p = (SharedPtr<Path>*)0;
  407. while (i.next(k, p)) {
  408. if (p->references() <= 1) {
  409. _paths.erase(*k);
  410. }
  411. }
  412. }
  413. }
  414. void Topology::_memoizeUpstreams(void* tPtr)
  415. {
  416. // assumes _upstreams_m and _peers_m are locked
  417. _upstreamAddresses.clear();
  418. _amUpstream = false;
  419. for (std::vector<World::Root>::const_iterator i(_planet.roots().begin()); i != _planet.roots().end(); ++i) {
  420. const Identity& id = i->identity;
  421. if (id == RR->identity) {
  422. _amUpstream = true;
  423. }
  424. else if (std::find(_upstreamAddresses.begin(), _upstreamAddresses.end(), id.address()) == _upstreamAddresses.end()) {
  425. _upstreamAddresses.push_back(id.address());
  426. SharedPtr<Peer>& hp = _peers[id.address()];
  427. if (! hp) {
  428. hp = new Peer(RR, RR->identity, id);
  429. }
  430. }
  431. }
  432. for (std::vector<World>::const_iterator m(_moons.begin()); m != _moons.end(); ++m) {
  433. for (std::vector<World::Root>::const_iterator i(m->roots().begin()); i != m->roots().end(); ++i) {
  434. if (i->identity == RR->identity) {
  435. _amUpstream = true;
  436. }
  437. else if (std::find(_upstreamAddresses.begin(), _upstreamAddresses.end(), i->identity.address()) == _upstreamAddresses.end()) {
  438. _upstreamAddresses.push_back(i->identity.address());
  439. SharedPtr<Peer>& hp = _peers[i->identity.address()];
  440. if (! hp) {
  441. hp = new Peer(RR, RR->identity, i->identity);
  442. }
  443. }
  444. }
  445. }
  446. std::sort(_upstreamAddresses.begin(), _upstreamAddresses.end());
  447. }
  448. void Topology::_savePeer(void* tPtr, const SharedPtr<Peer>& peer)
  449. {
  450. try {
  451. Buffer<ZT_PEER_MAX_SERIALIZED_STATE_SIZE> buf;
  452. peer->serializeForCache(buf);
  453. uint64_t tmpid[2];
  454. tmpid[0] = peer->address().toInt();
  455. tmpid[1] = 0;
  456. RR->node->stateObjectPut(tPtr, ZT_STATE_OBJECT_PEER, tmpid, buf.data(), buf.size());
  457. }
  458. catch (...) {
  459. } // sanity check, discard invalid entries
  460. }
  461. } // namespace ZeroTier