Topology.cpp 16 KB

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