Topology.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 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. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include "Constants.hpp"
  27. #include "Topology.hpp"
  28. #include "RuntimeEnvironment.hpp"
  29. #include "Node.hpp"
  30. #include "Network.hpp"
  31. #include "NetworkConfig.hpp"
  32. #include "Buffer.hpp"
  33. #include "Switch.hpp"
  34. namespace ZeroTier {
  35. /*
  36. * 2016-01-13 ZeroTier planet definition for the third planet of Sol:
  37. *
  38. * There are two roots, each of which is a cluster spread across multiple
  39. * continents and providers. They are named Alice and Bob after the
  40. * canonical example names used in cryptography.
  41. *
  42. * Alice:
  43. *
  44. * root-alice-ams-01: Amsterdam, Netherlands
  45. * root-alice-joh-01: Johannesburg, South Africa
  46. * root-alice-nyc-01: New York, New York, USA
  47. * root-alice-sao-01: Sao Paolo, Brazil
  48. * root-alice-sfo-01: San Francisco, California, USA
  49. * root-alice-sgp-01: Singapore
  50. *
  51. * Bob:
  52. *
  53. * root-bob-dfw-01: Dallas, Texas, USA
  54. * root-bob-fra-01: Frankfurt, Germany
  55. * root-bob-par-01: Paris, France
  56. * root-bob-syd-01: Sydney, Australia
  57. * root-bob-tok-01: Tokyo, Japan
  58. * root-bob-tor-01: Toronto, Canada
  59. */
  60. #define ZT_DEFAULT_WORLD_LENGTH 634
  61. 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};
  62. Topology::Topology(const RuntimeEnvironment *renv,void *tPtr) :
  63. RR(renv),
  64. _numConfiguredPhysicalPaths(0),
  65. _amRoot(false)
  66. {
  67. uint8_t tmp[ZT_WORLD_MAX_SERIALIZED_LENGTH];
  68. uint64_t idtmp[2];
  69. idtmp[0] = 0; idtmp[1] = 0;
  70. int n = RR->node->stateObjectGet(tPtr,ZT_STATE_OBJECT_PLANET,idtmp,tmp,sizeof(tmp));
  71. if (n > 0) {
  72. try {
  73. World cachedPlanet;
  74. cachedPlanet.deserialize(Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH>(tmp,(unsigned int)n),0);
  75. addWorld(tPtr,cachedPlanet,false);
  76. } catch ( ... ) {} // ignore invalid cached planets
  77. }
  78. World defaultPlanet;
  79. {
  80. Buffer<ZT_DEFAULT_WORLD_LENGTH> wtmp(ZT_DEFAULT_WORLD,ZT_DEFAULT_WORLD_LENGTH);
  81. defaultPlanet.deserialize(wtmp,0); // throws on error, which would indicate a bad static variable up top
  82. }
  83. addWorld(tPtr,defaultPlanet,false);
  84. }
  85. Topology::~Topology()
  86. {
  87. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
  88. Address *a = (Address *)0;
  89. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  90. while (i.next(a,p))
  91. _savePeer((void *)0,*p);
  92. }
  93. SharedPtr<Peer> Topology::addPeer(void *tPtr,const SharedPtr<Peer> &peer)
  94. {
  95. SharedPtr<Peer> np;
  96. {
  97. Mutex::Lock _l(_peers_m);
  98. SharedPtr<Peer> &hp = _peers[peer->address()];
  99. if (!hp)
  100. hp = peer;
  101. np = hp;
  102. }
  103. return np;
  104. }
  105. SharedPtr<Peer> Topology::getPeer(void *tPtr,const Address &zta)
  106. {
  107. if (zta == RR->identity.address())
  108. return SharedPtr<Peer>();
  109. {
  110. Mutex::Lock _l(_peers_m);
  111. const SharedPtr<Peer> *const ap = _peers.get(zta);
  112. if (ap)
  113. return *ap;
  114. }
  115. try {
  116. Buffer<ZT_PEER_MAX_SERIALIZED_STATE_SIZE> buf;
  117. uint64_t idbuf[2]; idbuf[0] = zta.toInt(); idbuf[1] = 0;
  118. int len = RR->node->stateObjectGet(tPtr,ZT_STATE_OBJECT_PEER,idbuf,buf.unsafeData(),ZT_PEER_MAX_SERIALIZED_STATE_SIZE);
  119. if (len > 0) {
  120. buf.setSize(len);
  121. Mutex::Lock _l(_peers_m);
  122. SharedPtr<Peer> &ap = _peers[zta];
  123. if (ap)
  124. return ap;
  125. ap = Peer::deserializeFromCache(RR->node->now(),tPtr,buf,RR);
  126. if (!ap) {
  127. _peers.erase(zta);
  128. }
  129. return SharedPtr<Peer>();
  130. }
  131. } catch ( ... ) {} // ignore invalid identities or other strage failures
  132. return SharedPtr<Peer>();
  133. }
  134. Identity Topology::getIdentity(void *tPtr,const Address &zta)
  135. {
  136. if (zta == RR->identity.address()) {
  137. return RR->identity;
  138. } else {
  139. Mutex::Lock _l(_peers_m);
  140. const SharedPtr<Peer> *const ap = _peers.get(zta);
  141. if (ap)
  142. return (*ap)->identity();
  143. }
  144. return Identity();
  145. }
  146. SharedPtr<Peer> Topology::getUpstreamPeer()
  147. {
  148. const int64_t now = RR->node->now();
  149. unsigned int bestq = ~((unsigned int)0);
  150. const SharedPtr<Peer> *best = (const SharedPtr<Peer> *)0;
  151. Mutex::Lock _l1(_peers_m);
  152. Mutex::Lock _l2(_upstreams_m);
  153. for(std::vector<Address>::const_iterator a(_upstreamAddresses.begin());a!=_upstreamAddresses.end();++a) {
  154. const SharedPtr<Peer> *p = _peers.get(*a);
  155. if (p) {
  156. const unsigned int q = (*p)->relayQuality(now);
  157. if (q <= bestq) {
  158. bestq = q;
  159. best = p;
  160. }
  161. }
  162. }
  163. if (!best)
  164. return SharedPtr<Peer>();
  165. return *best;
  166. }
  167. bool Topology::isUpstream(const Identity &id) const
  168. {
  169. Mutex::Lock _l(_upstreams_m);
  170. return (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),id.address()) != _upstreamAddresses.end());
  171. }
  172. bool Topology::shouldAcceptWorldUpdateFrom(const Address &addr) const
  173. {
  174. Mutex::Lock _l(_upstreams_m);
  175. if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),addr) != _upstreamAddresses.end())
  176. return true;
  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. return false;
  182. }
  183. ZT_PeerRole Topology::role(const Address &ztaddr) const
  184. {
  185. Mutex::Lock _l(_upstreams_m);
  186. if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),ztaddr) != _upstreamAddresses.end()) {
  187. for(std::vector<World::Root>::const_iterator i(_planet.roots().begin());i!=_planet.roots().end();++i) {
  188. if (i->identity.address() == ztaddr)
  189. return ZT_PEER_ROLE_PLANET;
  190. }
  191. return ZT_PEER_ROLE_MOON;
  192. }
  193. return ZT_PEER_ROLE_LEAF;
  194. }
  195. bool Topology::isProhibitedEndpoint(const Address &ztaddr,const InetAddress &ipaddr) const
  196. {
  197. Mutex::Lock _l(_upstreams_m);
  198. // For roots the only permitted addresses are those defined. This adds just a little
  199. // bit of extra security against spoofing, replaying, etc.
  200. if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),ztaddr) != _upstreamAddresses.end()) {
  201. for(std::vector<World::Root>::const_iterator r(_planet.roots().begin());r!=_planet.roots().end();++r) {
  202. if (r->identity.address() == ztaddr) {
  203. if (r->stableEndpoints.size() == 0)
  204. return false; // no stable endpoints specified, so allow dynamic paths
  205. for(std::vector<InetAddress>::const_iterator e(r->stableEndpoints.begin());e!=r->stableEndpoints.end();++e) {
  206. if (ipaddr.ipsEqual(*e))
  207. return false;
  208. }
  209. }
  210. }
  211. for(std::vector<World>::const_iterator m(_moons.begin());m!=_moons.end();++m) {
  212. for(std::vector<World::Root>::const_iterator r(m->roots().begin());r!=m->roots().end();++r) {
  213. if (r->identity.address() == ztaddr) {
  214. if (r->stableEndpoints.size() == 0)
  215. return false; // no stable endpoints specified, so allow dynamic paths
  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. return true;
  224. }
  225. return false;
  226. }
  227. bool Topology::addWorld(void *tPtr,const World &newWorld,bool alwaysAcceptNew)
  228. {
  229. if ((newWorld.type() != World::TYPE_PLANET)&&(newWorld.type() != World::TYPE_MOON))
  230. return false;
  231. Mutex::Lock _l1(_upstreams_m);
  232. Mutex::Lock _l2(_peers_m);
  233. World *existing = (World *)0;
  234. switch(newWorld.type()) {
  235. case World::TYPE_PLANET:
  236. existing = &_planet;
  237. break;
  238. case World::TYPE_MOON:
  239. for(std::vector< World >::iterator m(_moons.begin());m!=_moons.end();++m) {
  240. if (m->id() == newWorld.id()) {
  241. existing = &(*m);
  242. break;
  243. }
  244. }
  245. break;
  246. default:
  247. return false;
  248. }
  249. if (existing) {
  250. if (existing->shouldBeReplacedBy(newWorld))
  251. *existing = newWorld;
  252. else return false;
  253. } else if (newWorld.type() == World::TYPE_MOON) {
  254. if (alwaysAcceptNew) {
  255. _moons.push_back(newWorld);
  256. existing = &(_moons.back());
  257. } else {
  258. for(std::vector< std::pair<uint64_t,Address> >::iterator m(_moonSeeds.begin());m!=_moonSeeds.end();++m) {
  259. if (m->first == newWorld.id()) {
  260. for(std::vector<World::Root>::const_iterator r(newWorld.roots().begin());r!=newWorld.roots().end();++r) {
  261. if (r->identity.address() == m->second) {
  262. _moonSeeds.erase(m);
  263. _moons.push_back(newWorld);
  264. existing = &(_moons.back());
  265. break;
  266. }
  267. }
  268. if (existing)
  269. break;
  270. }
  271. }
  272. }
  273. if (!existing)
  274. return false;
  275. } else {
  276. return false;
  277. }
  278. try {
  279. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> sbuf;
  280. existing->serialize(sbuf,false);
  281. uint64_t idtmp[2];
  282. idtmp[0] = existing->id(); idtmp[1] = 0;
  283. RR->node->stateObjectPut(tPtr,(existing->type() == World::TYPE_PLANET) ? ZT_STATE_OBJECT_PLANET : ZT_STATE_OBJECT_MOON,idtmp,sbuf.data(),sbuf.size());
  284. } catch ( ... ) {}
  285. _memoizeUpstreams(tPtr);
  286. return true;
  287. }
  288. void Topology::addMoon(void *tPtr,const uint64_t id,const Address &seed)
  289. {
  290. char tmp[ZT_WORLD_MAX_SERIALIZED_LENGTH];
  291. uint64_t idtmp[2];
  292. idtmp[0] = id; idtmp[1] = 0;
  293. int n = RR->node->stateObjectGet(tPtr,ZT_STATE_OBJECT_MOON,idtmp,tmp,sizeof(tmp));
  294. if (n > 0) {
  295. try {
  296. World w;
  297. w.deserialize(Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH>(tmp,(unsigned int)n));
  298. if ((w.type() == World::TYPE_MOON)&&(w.id() == id)) {
  299. addWorld(tPtr,w,true);
  300. return;
  301. }
  302. } catch ( ... ) {}
  303. }
  304. if (seed) {
  305. Mutex::Lock _l(_upstreams_m);
  306. if (std::find(_moonSeeds.begin(),_moonSeeds.end(),std::pair<uint64_t,Address>(id,seed)) == _moonSeeds.end())
  307. _moonSeeds.push_back(std::pair<uint64_t,Address>(id,seed));
  308. }
  309. }
  310. void Topology::removeMoon(void *tPtr,const uint64_t id)
  311. {
  312. Mutex::Lock _l1(_upstreams_m);
  313. Mutex::Lock _l2(_peers_m);
  314. std::vector<World> nm;
  315. for(std::vector<World>::const_iterator m(_moons.begin());m!=_moons.end();++m) {
  316. if (m->id() != id) {
  317. nm.push_back(*m);
  318. } else {
  319. uint64_t idtmp[2];
  320. idtmp[0] = id; idtmp[1] = 0;
  321. RR->node->stateObjectDelete(tPtr,ZT_STATE_OBJECT_MOON,idtmp);
  322. }
  323. }
  324. _moons.swap(nm);
  325. std::vector< std::pair<uint64_t,Address> > cm;
  326. for(std::vector< std::pair<uint64_t,Address> >::const_iterator m(_moonSeeds.begin());m!=_moonSeeds.end();++m) {
  327. if (m->first != id)
  328. cm.push_back(*m);
  329. }
  330. _moonSeeds.swap(cm);
  331. _memoizeUpstreams(tPtr);
  332. }
  333. void Topology::doPeriodicTasks(void *tPtr,int64_t now)
  334. {
  335. {
  336. Mutex::Lock _l1(_peers_m);
  337. Mutex::Lock _l2(_upstreams_m);
  338. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
  339. Address *a = (Address *)0;
  340. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  341. while (i.next(a,p)) {
  342. if ( (!(*p)->isAlive(now)) && (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),*a) == _upstreamAddresses.end()) ) {
  343. _savePeer(tPtr,*p);
  344. _peers.erase(*a);
  345. }
  346. }
  347. }
  348. {
  349. Mutex::Lock _l(_paths_m);
  350. Hashtable< Path::HashKey,SharedPtr<Path> >::Iterator i(_paths);
  351. Path::HashKey *k = (Path::HashKey *)0;
  352. SharedPtr<Path> *p = (SharedPtr<Path> *)0;
  353. while (i.next(k,p)) {
  354. if (p->reclaimIfWeak())
  355. _paths.erase(*k);
  356. }
  357. }
  358. }
  359. void Topology::_memoizeUpstreams(void *tPtr)
  360. {
  361. // assumes _upstreams_m and _peers_m are locked
  362. _upstreamAddresses.clear();
  363. _amRoot = false;
  364. for(std::vector<World::Root>::const_iterator i(_planet.roots().begin());i!=_planet.roots().end();++i) {
  365. if (i->identity == RR->identity) {
  366. _amRoot = true;
  367. } else if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),i->identity.address()) == _upstreamAddresses.end()) {
  368. _upstreamAddresses.push_back(i->identity.address());
  369. SharedPtr<Peer> &hp = _peers[i->identity.address()];
  370. if (!hp)
  371. hp = new Peer(RR,RR->identity,i->identity);
  372. }
  373. }
  374. for(std::vector<World>::const_iterator m(_moons.begin());m!=_moons.end();++m) {
  375. for(std::vector<World::Root>::const_iterator i(m->roots().begin());i!=m->roots().end();++i) {
  376. if (i->identity == RR->identity) {
  377. _amRoot = true;
  378. } else if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),i->identity.address()) == _upstreamAddresses.end()) {
  379. _upstreamAddresses.push_back(i->identity.address());
  380. SharedPtr<Peer> &hp = _peers[i->identity.address()];
  381. if (!hp)
  382. hp = new Peer(RR,RR->identity,i->identity);
  383. }
  384. }
  385. }
  386. std::sort(_upstreamAddresses.begin(),_upstreamAddresses.end());
  387. _cor.clear();
  388. for(std::vector<Address>::const_iterator a(_upstreamAddresses.begin());a!=_upstreamAddresses.end();++a) {
  389. if (!_cor.addRepresentative(*a))
  390. break;
  391. }
  392. _cor.sign(RR->identity,RR->node->now());
  393. }
  394. void Topology::_savePeer(void *tPtr,const SharedPtr<Peer> &peer)
  395. {
  396. try {
  397. Buffer<ZT_PEER_MAX_SERIALIZED_STATE_SIZE> buf;
  398. peer->serialize(buf);
  399. uint64_t tmpid[2]; tmpid[0] = peer->address().toInt(); tmpid[1] = 0;
  400. RR->node->stateObjectPut(tPtr,ZT_STATE_OBJECT_PEER,tmpid,buf.data(),buf.size());
  401. } catch ( ... ) {} // sanity check, discard invalid entries
  402. }
  403. } // namespace ZeroTier