Topology.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. #include "Constants.hpp"
  19. #include "Topology.hpp"
  20. #include "RuntimeEnvironment.hpp"
  21. #include "Node.hpp"
  22. #include "Network.hpp"
  23. #include "NetworkConfig.hpp"
  24. #include "Buffer.hpp"
  25. #include "Switch.hpp"
  26. namespace ZeroTier {
  27. /*
  28. * 2016-01-13 ZeroTier planet definition for the third planet of Sol:
  29. *
  30. * There are two roots, each of which is a cluster spread across multiple
  31. * continents and providers. They are named Alice and Bob after the
  32. * canonical example names used in cryptography.
  33. *
  34. * Alice:
  35. *
  36. * root-alice-ams-01: Amsterdam, Netherlands
  37. * root-alice-joh-01: Johannesburg, South Africa
  38. * root-alice-nyc-01: New York, New York, USA
  39. * root-alice-sao-01: Sao Paolo, Brazil
  40. * root-alice-sfo-01: San Francisco, California, USA
  41. * root-alice-sgp-01: Singapore
  42. *
  43. * Bob:
  44. *
  45. * root-bob-dfw-01: Dallas, Texas, USA
  46. * root-bob-fra-01: Frankfurt, Germany
  47. * root-bob-par-01: Paris, France
  48. * root-bob-syd-01: Sydney, Australia
  49. * root-bob-tok-01: Tokyo, Japan
  50. * root-bob-tor-01: Toronto, Canada
  51. */
  52. #define ZT_DEFAULT_WORLD_LENGTH 634
  53. static const unsigned char ZT_DEFAULT_WORLD[ZT_DEFAULT_WORLD_LENGTH] = {0x01,0x00,0x00,0x00,0x00,0x08,0xea,0xc9,0x0a,0x00,0x00,0x01,0x52,0x3c,0x32,0x50,0x1a,0xb8,0xb3,0x88,0xa4,0x69,0x22,0x14,0x91,0xaa,0x9a,0xcd,0x66,0xcc,0x76,0x4c,0xde,0xfd,0x56,0x03,0x9f,0x10,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,0x9a,0xc9,0xc8,0x18,0xeb,0x4a,0xf7,0x86,0xa8,0x40,0xd6,0x52,0xea,0xae,0x9e,0x7a,0xbf,0x4c,0x97,0x66,0xab,0x2d,0x6f,0xaf,0xc9,0x2b,0x3a,0xff,0xed,0xd6,0x30,0x3e,0xc4,0x6a,0x65,0xf2,0xbd,0x83,0x52,0xf5,0x40,0xe9,0xcc,0x0d,0x6e,0x89,0x3f,0x9a,0xa0,0xb8,0xdf,0x42,0xd2,0x2f,0x84,0xe6,0x03,0x26,0x0f,0xa8,0xe3,0xcc,0x05,0x05,0x03,0xef,0x12,0x80,0x0d,0xce,0x3e,0xb6,0x58,0x3b,0x1f,0xa8,0xad,0xc7,0x25,0xf9,0x43,0x71,0xa7,0x5c,0x9a,0xc7,0xe1,0xa3,0xb8,0x88,0xd0,0x71,0x6c,0x94,0x99,0x73,0x41,0x0b,0x1b,0x48,0x84,0x02,0x9d,0x21,0x90,0x39,0xf3,0x00,0x01,0xf0,0x92,0x2a,0x98,0xe3,0xb3,0x4e,0xbc,0xbf,0xf3,0x33,0x26,0x9d,0xc2,0x65,0xd7,0xa0,0x20,0xaa,0xb6,0x9d,0x72,0xbe,0x4d,0x4a,0xcc,0x9c,0x8c,0x92,0x94,0x78,0x57,0x71,0x25,0x6c,0xd1,0xd9,0x42,0xa9,0x0d,0x1b,0xd1,0xd2,0xdc,0xa3,0xea,0x84,0xef,0x7d,0x85,0xaf,0xe6,0x61,0x1f,0xb4,0x3f,0xf0,0xb7,0x41,0x26,0xd9,0x0a,0x6e,0x00,0x0c,0x04,0xbc,0xa6,0x5e,0xb1,0x27,0x09,0x06,0x2a,0x03,0xb0,0xc0,0x00,0x02,0x00,0xd0,0x00,0x00,0x00,0x00,0x00,0x7d,0x00,0x01,0x27,0x09,0x04,0x9a,0x42,0xc5,0x21,0x27,0x09,0x06,0x2c,0x0f,0xf8,0x50,0x01,0x54,0x01,0x97,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x27,0x09,0x04,0x9f,0xcb,0x61,0xab,0x27,0x09,0x06,0x26,0x04,0xa8,0x80,0x08,0x00,0x00,0xa1,0x00,0x00,0x00,0x00,0x00,0x54,0x60,0x01,0x27,0x09,0x04,0xa9,0x39,0x8f,0x68,0x27,0x09,0x06,0x26,0x07,0xf0,0xd0,0x1d,0x01,0x00,0x57,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x27,0x09,0x04,0x6b,0xaa,0xc5,0x0e,0x27,0x09,0x06,0x26,0x04,0xa8,0x80,0x00,0x01,0x00,0x20,0x00,0x00,0x00,0x00,0x02,0x00,0xe0,0x01,0x27,0x09,0x04,0x80,0xc7,0xc5,0xd9,0x27,0x09,0x06,0x24,0x00,0x61,0x80,0x00,0x00,0x00,0xd0,0x00,0x00,0x00,0x00,0x00,0xb7,0x40,0x01,0x27,0x09,0x88,0x41,0x40,0x8a,0x2e,0x00,0xbb,0x1d,0x31,0xf2,0xc3,0x23,0xe2,0x64,0xe9,0xe6,0x41,0x72,0xc1,0xa7,0x4f,0x77,0x89,0x95,0x55,0xed,0x10,0x75,0x1c,0xd5,0x6e,0x86,0x40,0x5c,0xde,0x11,0x8d,0x02,0xdf,0xfe,0x55,0x5d,0x46,0x2c,0xcf,0x6a,0x85,0xb5,0x63,0x1c,0x12,0x35,0x0c,0x8d,0x5d,0xc4,0x09,0xba,0x10,0xb9,0x02,0x5d,0x0f,0x44,0x5c,0xf4,0x49,0xd9,0x2b,0x1c,0x00,0x0c,0x04,0x2d,0x20,0xc6,0x82,0x27,0x09,0x06,0x20,0x01,0x19,0xf0,0x64,0x00,0x81,0xc3,0x54,0x00,0x00,0xff,0xfe,0x18,0x1d,0x61,0x27,0x09,0x04,0x2e,0x65,0xa0,0xf9,0x27,0x09,0x06,0x2a,0x03,0xb0,0xc0,0x00,0x03,0x00,0xd0,0x00,0x00,0x00,0x00,0x00,0x6a,0x30,0x01,0x27,0x09,0x04,0x6b,0xbf,0x2e,0xd2,0x27,0x09,0x06,0x20,0x01,0x19,0xf0,0x68,0x00,0x83,0xa4,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x64,0x27,0x09,0x04,0x2d,0x20,0xf6,0xb3,0x27,0x09,0x06,0x20,0x01,0x19,0xf0,0x58,0x00,0x8b,0xf8,0x54,0x00,0x00,0xff,0xfe,0x15,0xb3,0x9a,0x27,0x09,0x04,0x2d,0x20,0xf8,0x57,0x27,0x09,0x06,0x20,0x01,0x19,0xf0,0x70,0x00,0x9b,0xc9,0x54,0x00,0x00,0xff,0xfe,0x15,0xc4,0xf5,0x27,0x09,0x04,0x9f,0xcb,0x02,0x9a,0x27,0x09,0x06,0x26,0x04,0xa8,0x80,0x0c,0xad,0x00,0xd0,0x00,0x00,0x00,0x00,0x00,0x26,0x70,0x01,0x27,0x09};
  54. Topology::Topology(const RuntimeEnvironment *renv) :
  55. RR(renv),
  56. _trustedPathCount(0),
  57. _amRoot(false)
  58. {
  59. try {
  60. World cachedPlanet;
  61. std::string buf(RR->node->dataStoreGet("planet"));
  62. if (buf.length() > 0) {
  63. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> dswtmp(buf.data(),(unsigned int)buf.length());
  64. cachedPlanet.deserialize(dswtmp,0);
  65. }
  66. addWorld(cachedPlanet,false);
  67. } catch ( ... ) {}
  68. World defaultPlanet;
  69. {
  70. Buffer<ZT_DEFAULT_WORLD_LENGTH> wtmp(ZT_DEFAULT_WORLD,ZT_DEFAULT_WORLD_LENGTH);
  71. defaultPlanet.deserialize(wtmp,0); // throws on error, which would indicate a bad static variable up top
  72. }
  73. addWorld(defaultPlanet,false);
  74. }
  75. SharedPtr<Peer> Topology::addPeer(const SharedPtr<Peer> &peer)
  76. {
  77. #ifdef ZT_TRACE
  78. if ((!peer)||(peer->address() == RR->identity.address())) {
  79. if (!peer)
  80. fprintf(stderr,"FATAL BUG: addPeer() caught attempt to add NULL peer" ZT_EOL_S);
  81. else fprintf(stderr,"FATAL BUG: addPeer() caught attempt to add peer for self" ZT_EOL_S);
  82. abort();
  83. }
  84. #endif
  85. SharedPtr<Peer> np;
  86. {
  87. Mutex::Lock _l(_peers_m);
  88. SharedPtr<Peer> &hp = _peers[peer->address()];
  89. if (!hp)
  90. hp = peer;
  91. np = hp;
  92. }
  93. saveIdentity(np->identity());
  94. return np;
  95. }
  96. SharedPtr<Peer> Topology::getPeer(const Address &zta)
  97. {
  98. if (zta == RR->identity.address()) {
  99. TRACE("BUG: ignored attempt to getPeer() for self, returned NULL");
  100. return SharedPtr<Peer>();
  101. }
  102. {
  103. Mutex::Lock _l(_peers_m);
  104. const SharedPtr<Peer> *const ap = _peers.get(zta);
  105. if (ap)
  106. return *ap;
  107. }
  108. try {
  109. Identity id(_getIdentity(zta));
  110. if (id) {
  111. SharedPtr<Peer> np(new Peer(RR,RR->identity,id));
  112. {
  113. Mutex::Lock _l(_peers_m);
  114. SharedPtr<Peer> &ap = _peers[zta];
  115. if (!ap)
  116. ap.swap(np);
  117. return ap;
  118. }
  119. }
  120. } catch ( ... ) {} // invalid identity on disk?
  121. return SharedPtr<Peer>();
  122. }
  123. Identity Topology::getIdentity(const Address &zta)
  124. {
  125. if (zta == RR->identity.address()) {
  126. return RR->identity;
  127. } else {
  128. Mutex::Lock _l(_peers_m);
  129. const SharedPtr<Peer> *const ap = _peers.get(zta);
  130. if (ap)
  131. return (*ap)->identity();
  132. }
  133. return _getIdentity(zta);
  134. }
  135. void Topology::saveIdentity(const Identity &id)
  136. {
  137. if (id) {
  138. char p[128];
  139. Utils::snprintf(p,sizeof(p),"iddb.d/%.10llx",(unsigned long long)id.address().toInt());
  140. RR->node->dataStorePut(p,id.toString(false),false);
  141. }
  142. }
  143. SharedPtr<Peer> Topology::getUpstreamPeer(const Address *avoid,unsigned int avoidCount,bool strictAvoid)
  144. {
  145. const uint64_t now = RR->node->now();
  146. Mutex::Lock _l1(_peers_m);
  147. Mutex::Lock _l2(_upstreams_m);
  148. if (_amRoot) {
  149. /* If I am a root, pick another root that isn't mine and that
  150. * has a numerically greater ID. This causes packets to roam
  151. * around the top rather than bouncing between just two. */
  152. for(unsigned long p=0;p<_upstreamAddresses.size();++p) {
  153. if (_upstreamAddresses[p] == RR->identity.address()) {
  154. for(unsigned long q=1;q<_upstreamAddresses.size();++q) {
  155. const SharedPtr<Peer> *const nextsn = _peers.get(_upstreamAddresses[(p + q) % _upstreamAddresses.size()]);
  156. if ((nextsn)&&((*nextsn)->hasActiveDirectPath(now)))
  157. return *nextsn;
  158. }
  159. break;
  160. }
  161. }
  162. } else {
  163. /* Otherwise pick the bestest looking upstream */
  164. unsigned int bestQualityOverall = ~((unsigned int)0);
  165. unsigned int bestQualityNotAvoid = ~((unsigned int)0);
  166. const SharedPtr<Peer> *bestOverall = (const SharedPtr<Peer> *)0;
  167. const SharedPtr<Peer> *bestNotAvoid = (const SharedPtr<Peer> *)0;
  168. for(std::vector<Address>::const_iterator a(_upstreamAddresses.begin());a!=_upstreamAddresses.end();++a) {
  169. const SharedPtr<Peer> *p = _peers.get(*a);
  170. if (p) {
  171. bool avoiding = false;
  172. for(unsigned int i=0;i<avoidCount;++i) {
  173. if (avoid[i] == (*p)->address()) {
  174. avoiding = true;
  175. break;
  176. }
  177. }
  178. const unsigned int q = (*p)->relayQuality(now);
  179. if (q <= bestQualityOverall) {
  180. bestQualityOverall = q;
  181. bestOverall = &(*p);
  182. }
  183. if ((!avoiding)&&(q <= bestQualityNotAvoid)) {
  184. bestQualityNotAvoid = q;
  185. bestNotAvoid = &(*p);
  186. }
  187. }
  188. }
  189. if (bestNotAvoid) {
  190. return *bestNotAvoid;
  191. } else if ((!strictAvoid)&&(bestOverall)) {
  192. return *bestOverall;
  193. }
  194. }
  195. return SharedPtr<Peer>();
  196. }
  197. bool Topology::isUpstream(const Identity &id) const
  198. {
  199. Mutex::Lock _l(_upstreams_m);
  200. return (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),id.address()) != _upstreamAddresses.end());
  201. }
  202. bool Topology::shouldAcceptWorldUpdateFrom(const Address &addr) const
  203. {
  204. Mutex::Lock _l(_upstreams_m);
  205. if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),addr) != _upstreamAddresses.end())
  206. return true;
  207. for(std::vector< std::pair< uint64_t,Address> >::const_iterator s(_moonSeeds.begin());s!=_moonSeeds.end();++s) {
  208. if (s->second == addr)
  209. return true;
  210. }
  211. return false;
  212. }
  213. ZT_PeerRole Topology::role(const Address &ztaddr) const
  214. {
  215. Mutex::Lock _l(_upstreams_m);
  216. if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),ztaddr) != _upstreamAddresses.end()) {
  217. for(std::vector<World::Root>::const_iterator i(_planet.roots().begin());i!=_planet.roots().end();++i) {
  218. if (i->identity.address() == ztaddr)
  219. return ZT_PEER_ROLE_PLANET;
  220. }
  221. return ZT_PEER_ROLE_MOON;
  222. }
  223. return ZT_PEER_ROLE_LEAF;
  224. }
  225. bool Topology::isProhibitedEndpoint(const Address &ztaddr,const InetAddress &ipaddr) const
  226. {
  227. Mutex::Lock _l(_upstreams_m);
  228. // For roots the only permitted addresses are those defined. This adds just a little
  229. // bit of extra security against spoofing, replaying, etc.
  230. if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),ztaddr) != _upstreamAddresses.end()) {
  231. for(std::vector<World::Root>::const_iterator r(_planet.roots().begin());r!=_planet.roots().end();++r) {
  232. if (r->identity.address() == ztaddr) {
  233. if (r->stableEndpoints.size() == 0)
  234. return false; // no stable endpoints specified, so allow dynamic paths
  235. for(std::vector<InetAddress>::const_iterator e(r->stableEndpoints.begin());e!=r->stableEndpoints.end();++e) {
  236. if (ipaddr.ipsEqual(*e))
  237. return false;
  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 r(m->roots().begin());r!=m->roots().end();++r) {
  243. if (r->identity.address() == ztaddr) {
  244. if (r->stableEndpoints.size() == 0)
  245. return false; // no stable endpoints specified, so allow dynamic paths
  246. for(std::vector<InetAddress>::const_iterator e(r->stableEndpoints.begin());e!=r->stableEndpoints.end();++e) {
  247. if (ipaddr.ipsEqual(*e))
  248. return false;
  249. }
  250. }
  251. }
  252. }
  253. return true;
  254. }
  255. return false;
  256. }
  257. bool Topology::addWorld(const World &newWorld,bool alwaysAcceptNew)
  258. {
  259. if ((newWorld.type() != World::TYPE_PLANET)&&(newWorld.type() != World::TYPE_MOON))
  260. return false;
  261. Mutex::Lock _l1(_upstreams_m);
  262. Mutex::Lock _l2(_peers_m);
  263. World *existing = (World *)0;
  264. switch(newWorld.type()) {
  265. case World::TYPE_PLANET:
  266. existing = &_planet;
  267. break;
  268. case World::TYPE_MOON:
  269. for(std::vector< World >::iterator m(_moons.begin());m!=_moons.end();++m) {
  270. if (m->id() == newWorld.id()) {
  271. existing = &(*m);
  272. break;
  273. }
  274. }
  275. break;
  276. default:
  277. return false;
  278. }
  279. if (existing) {
  280. if (existing->shouldBeReplacedBy(newWorld))
  281. *existing = newWorld;
  282. else return false;
  283. } else if (newWorld.type() == World::TYPE_MOON) {
  284. if (alwaysAcceptNew) {
  285. _moons.push_back(newWorld);
  286. existing = &(_moons.back());
  287. } else {
  288. for(std::vector< std::pair<uint64_t,Address> >::iterator m(_moonSeeds.begin());m!=_moonSeeds.end();++m) {
  289. if (m->first == newWorld.id()) {
  290. for(std::vector<World::Root>::const_iterator r(newWorld.roots().begin());r!=newWorld.roots().end();++r) {
  291. if (r->identity.address() == m->second) {
  292. _moonSeeds.erase(m);
  293. _moons.push_back(newWorld);
  294. existing = &(_moons.back());
  295. break;
  296. }
  297. }
  298. if (existing)
  299. break;
  300. }
  301. }
  302. }
  303. if (!existing)
  304. return false;
  305. } else {
  306. return false;
  307. }
  308. char savePath[64];
  309. if (existing->type() == World::TYPE_MOON) {
  310. Utils::snprintf(savePath,sizeof(savePath),"moons.d/%.16llx.moon",existing->id());
  311. } else {
  312. Utils::scopy(savePath,sizeof(savePath),"planet");
  313. }
  314. try {
  315. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> dswtmp;
  316. existing->serialize(dswtmp,false);
  317. RR->node->dataStorePut(savePath,dswtmp.data(),dswtmp.size(),false);
  318. } catch ( ... ) {
  319. RR->node->dataStoreDelete(savePath);
  320. }
  321. _memoizeUpstreams();
  322. return true;
  323. }
  324. void Topology::addMoon(const uint64_t id,const Address &seed)
  325. {
  326. char savePath[64];
  327. Utils::snprintf(savePath,sizeof(savePath),"moons.d/%.16llx.moon",id);
  328. try {
  329. std::string moonBin(RR->node->dataStoreGet(savePath));
  330. if (moonBin.length() > 1) {
  331. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> wtmp(moonBin.data(),(unsigned int)moonBin.length());
  332. World w;
  333. w.deserialize(wtmp);
  334. if ((w.type() == World::TYPE_MOON)&&(w.id() == id)) {
  335. addWorld(w,true);
  336. return;
  337. }
  338. }
  339. } catch ( ... ) {}
  340. if (seed) {
  341. Mutex::Lock _l(_upstreams_m);
  342. if (std::find(_moonSeeds.begin(),_moonSeeds.end(),std::pair<uint64_t,Address>(id,seed)) == _moonSeeds.end())
  343. _moonSeeds.push_back(std::pair<uint64_t,Address>(id,seed));
  344. }
  345. }
  346. void Topology::removeMoon(const uint64_t id)
  347. {
  348. Mutex::Lock _l1(_upstreams_m);
  349. Mutex::Lock _l2(_peers_m);
  350. std::vector<World> nm;
  351. for(std::vector<World>::const_iterator m(_moons.begin());m!=_moons.end();++m) {
  352. if (m->id() != id) {
  353. nm.push_back(*m);
  354. } else {
  355. char savePath[64];
  356. Utils::snprintf(savePath,sizeof(savePath),"moons.d/%.16llx.moon",id);
  357. RR->node->dataStoreDelete(savePath);
  358. }
  359. }
  360. _moons.swap(nm);
  361. std::vector< std::pair<uint64_t,Address> > cm;
  362. for(std::vector< std::pair<uint64_t,Address> >::const_iterator m(_moonSeeds.begin());m!=_moonSeeds.end();++m) {
  363. if (m->first != id)
  364. cm.push_back(*m);
  365. }
  366. _moonSeeds.swap(cm);
  367. _memoizeUpstreams();
  368. }
  369. void Topology::clean(uint64_t now)
  370. {
  371. {
  372. Mutex::Lock _l1(_peers_m);
  373. Mutex::Lock _l2(_upstreams_m);
  374. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
  375. Address *a = (Address *)0;
  376. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  377. while (i.next(a,p)) {
  378. if ( (!(*p)->isAlive(now)) && (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),*a) == _upstreamAddresses.end()) )
  379. _peers.erase(*a);
  380. }
  381. }
  382. {
  383. Mutex::Lock _l(_paths_m);
  384. Hashtable< Path::HashKey,SharedPtr<Path> >::Iterator i(_paths);
  385. Path::HashKey *k = (Path::HashKey *)0;
  386. SharedPtr<Path> *p = (SharedPtr<Path> *)0;
  387. while (i.next(k,p)) {
  388. if (p->reclaimIfWeak())
  389. _paths.erase(*k);
  390. }
  391. }
  392. }
  393. Identity Topology::_getIdentity(const Address &zta)
  394. {
  395. char p[128];
  396. Utils::snprintf(p,sizeof(p),"iddb.d/%.10llx",(unsigned long long)zta.toInt());
  397. std::string ids(RR->node->dataStoreGet(p));
  398. if (ids.length() > 0) {
  399. try {
  400. return Identity(ids);
  401. } catch ( ... ) {} // ignore invalid IDs
  402. }
  403. return Identity();
  404. }
  405. void Topology::_memoizeUpstreams()
  406. {
  407. // assumes _upstreams_m and _peers_m are locked
  408. _upstreamAddresses.clear();
  409. _amRoot = false;
  410. for(std::vector<World::Root>::const_iterator i(_planet.roots().begin());i!=_planet.roots().end();++i) {
  411. if (i->identity == RR->identity) {
  412. _amRoot = true;
  413. } else if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),i->identity.address()) == _upstreamAddresses.end()) {
  414. _upstreamAddresses.push_back(i->identity.address());
  415. SharedPtr<Peer> &hp = _peers[i->identity.address()];
  416. if (!hp) {
  417. hp = new Peer(RR,RR->identity,i->identity);
  418. saveIdentity(i->identity);
  419. }
  420. }
  421. }
  422. for(std::vector<World>::const_iterator m(_moons.begin());m!=_moons.end();++m) {
  423. for(std::vector<World::Root>::const_iterator i(m->roots().begin());i!=m->roots().end();++i) {
  424. if (i->identity == RR->identity) {
  425. _amRoot = true;
  426. } else if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),i->identity.address()) == _upstreamAddresses.end()) {
  427. _upstreamAddresses.push_back(i->identity.address());
  428. SharedPtr<Peer> &hp = _peers[i->identity.address()];
  429. if (!hp) {
  430. hp = new Peer(RR,RR->identity,i->identity);
  431. saveIdentity(i->identity);
  432. }
  433. }
  434. }
  435. }
  436. std::sort(_upstreamAddresses.begin(),_upstreamAddresses.end());
  437. _cor.clear();
  438. for(std::vector<Address>::const_iterator a(_upstreamAddresses.begin());a!=_upstreamAddresses.end();++a) {
  439. if (!_cor.addRepresentative(*a))
  440. break;
  441. }
  442. _cor.sign(RR->identity,RR->node->now());
  443. }
  444. } // namespace ZeroTier