Topology.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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: 2023-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 "Constants.hpp"
  14. #include "Topology.hpp"
  15. #include "RuntimeEnvironment.hpp"
  16. #include "Node.hpp"
  17. #include "Network.hpp"
  18. #include "NetworkConfig.hpp"
  19. #include "Buffer.hpp"
  20. #include "Switch.hpp"
  21. namespace ZeroTier {
  22. /*
  23. * 2018-07-26 ZeroTier planet definition for the third planet of Sol:
  24. *
  25. * There are two roots, each of which is a cluster spread across multiple
  26. * continents and providers. They are named Alice and Bob after the
  27. * canonical example names used in cryptography.
  28. *
  29. * Alice:
  30. *
  31. * root-alice-ams-01: Amsterdam, Netherlands
  32. * root-alice-joh-01: Johannesburg, South Africa
  33. * root-alice-nyc-01: New York, New York, USA
  34. * root-alice-arg-01: Buenos Aires, Argentina
  35. * root-alice-sfo-01: San Francisco, California, USA
  36. * root-alice-sgp-01: Singapore
  37. *
  38. * Bob:
  39. *
  40. * root-bob-dfw-01: Dallas, Texas, USA
  41. * root-bob-fra-01: Frankfurt, Germany
  42. * root-bob-par-01: Paris, France
  43. * root-bob-syd-01: Sydney, Australia
  44. * root-bob-tok-01: Tokyo, Japan
  45. * root-bob-tor-01: Toronto, Canada
  46. */
  47. #define ZT_DEFAULT_WORLD_LENGTH 634
  48. static const unsigned char ZT_DEFAULT_WORLD[ZT_DEFAULT_WORLD_LENGTH] = {0x01,0x00,0x00,0x00,0x00,0x08,0xea,0xc9,0x0a,0x00,0x00,0x01,0x64,0xd3,0x71,0xf0,0x58,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,0xbf,0xfd,0xd5,0x32,0xf7,0x15,0x6e,0x02,0x6f,0xb9,0x01,0x0d,0xb5,0x7b,0x04,0xd8,0x3a,0xc5,0x17,0x39,0x04,0x36,0xfd,0x9d,0xc6,0x3d,0xa8,0xf3,0x8e,0x79,0xe7,0xc8,0x77,0x8d,0xcc,0x79,0xb8,0xab,0xc6,0x98,0x7c,0x9f,0x34,0x25,0x14,0xe1,0x2f,0xd7,0x97,0x11,0xec,0x34,0x4c,0x9f,0x0f,0xb4,0x85,0x0d,0x9b,0x11,0xd1,0xc2,0xce,0x00,0xc4,0x0a,0x13,0x4b,0xcb,0xc3,0xae,0x2e,0x16,0x00,0x4b,0xdc,0x90,0x5e,0x7e,0x9b,0x44,0x07,0x15,0x36,0x61,0x3c,0x64,0xaa,0xe9,0x46,0x78,0x3c,0xa7,0x18,0xc8,0xd8,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,0x7d,0x00,0x01,0x00,0x00,0x00,0x00,0x27,0x09,0x04,0x9a,0x42,0xc5,0x21,0x27,0x09,0x06,0x2c,0x0f,0xf8,0x50,0x01,0x54,0x01,0x97,0x00,0x33,0xcc,0x08,0xf8,0xfa,0xcc,0x08,0x27,0x09,0x04,0x9f,0xcb,0x61,0xab,0x27,0x09,0x06,0x26,0x04,0xa8,0x80,0x08,0x00,0x00,0xa1,0x00,0x54,0x60,0x01,0x00,0xfc,0xcc,0x08,0x27,0x09,0x04,0x83,0xff,0x06,0x10,0x27,0x09,0x06,0x28,0x03,0xeb,0x80,0x00,0x00,0x00,0x0e,0x00,0x02,0x60,0x01,0x00,0xfc,0xcc,0x08,0x27,0x09,0x04,0x6b,0xaa,0xc5,0x0e,0x27,0x09,0x06,0x26,0x04,0xa8,0x80,0x00,0x01,0x00,0x20,0x02,0x00,0xe0,0x01,0x08,0xfe,0xcc,0x08,0x27,0x09,0x04,0x80,0xc7,0xc5,0xd9,0x27,0x09,0x06,0x24,0x00,0x61,0x80,0x00,0x00,0x00,0xd0,0x00,0xb7,0x40,0x01,0x08,0xfe,0xcc,0x08,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,0x6a,0x30,0x01,0x78,0x00,0xcd,0x08,0x27,0x09,0x04,0x6b,0xbf,0x2e,0xd2,0x27,0x09,0x06,0x20,0x01,0x19,0xf0,0x68,0x00,0x83,0xa4,0x00,0x64,0xcd,0x08,0x80,0x01,0xcd,0x08,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,0x26,0x70,0x01,0xfe,0x15,0xc4,0xf5,0x27,0x09};
  49. Topology::Topology(const RuntimeEnvironment *renv,void *tPtr) :
  50. RR(renv),
  51. _numConfiguredPhysicalPaths(0),
  52. _amUpstream(false)
  53. {
  54. uint8_t tmp[ZT_WORLD_MAX_SERIALIZED_LENGTH];
  55. uint64_t idtmp[2];
  56. idtmp[0] = 0; idtmp[1] = 0;
  57. int n = RR->node->stateObjectGet(tPtr,ZT_STATE_OBJECT_PLANET,idtmp,tmp,sizeof(tmp));
  58. if (n > 0) {
  59. try {
  60. World cachedPlanet;
  61. cachedPlanet.deserialize(Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH>(tmp,(unsigned int)n),0);
  62. addWorld(tPtr,cachedPlanet,false);
  63. } catch ( ... ) {} // ignore invalid cached planets
  64. }
  65. World defaultPlanet;
  66. {
  67. Buffer<ZT_DEFAULT_WORLD_LENGTH> wtmp(ZT_DEFAULT_WORLD,ZT_DEFAULT_WORLD_LENGTH);
  68. defaultPlanet.deserialize(wtmp,0); // throws on error, which would indicate a bad static variable up top
  69. }
  70. addWorld(tPtr,defaultPlanet,false);
  71. }
  72. Topology::~Topology()
  73. {
  74. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
  75. Address *a = (Address *)0;
  76. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  77. while (i.next(a,p))
  78. _savePeer((void *)0,*p);
  79. }
  80. SharedPtr<Peer> Topology::addPeer(void *tPtr,const SharedPtr<Peer> &peer)
  81. {
  82. SharedPtr<Peer> np;
  83. {
  84. Mutex::Lock _l(_peers_m);
  85. SharedPtr<Peer> &hp = _peers[peer->address()];
  86. if (!hp)
  87. hp = peer;
  88. np = hp;
  89. }
  90. return np;
  91. }
  92. SharedPtr<Peer> Topology::getPeer(void *tPtr,const Address &zta)
  93. {
  94. if (zta == RR->identity.address())
  95. return SharedPtr<Peer>();
  96. {
  97. Mutex::Lock _l(_peers_m);
  98. const SharedPtr<Peer> *const ap = _peers.get(zta);
  99. if (ap)
  100. return *ap;
  101. }
  102. try {
  103. Buffer<ZT_PEER_MAX_SERIALIZED_STATE_SIZE> buf;
  104. uint64_t idbuf[2]; idbuf[0] = zta.toInt(); idbuf[1] = 0;
  105. int len = RR->node->stateObjectGet(tPtr,ZT_STATE_OBJECT_PEER,idbuf,buf.unsafeData(),ZT_PEER_MAX_SERIALIZED_STATE_SIZE);
  106. if (len > 0) {
  107. buf.setSize(len);
  108. Mutex::Lock _l(_peers_m);
  109. SharedPtr<Peer> &ap = _peers[zta];
  110. if (ap)
  111. return ap;
  112. ap = Peer::deserializeFromCache(RR->node->now(),tPtr,buf,RR);
  113. if (!ap) {
  114. _peers.erase(zta);
  115. }
  116. return SharedPtr<Peer>();
  117. }
  118. } catch ( ... ) {} // 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. } 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. return Identity();
  132. }
  133. SharedPtr<Peer> Topology::getUpstreamPeer()
  134. {
  135. const int64_t now = RR->node->now();
  136. unsigned int bestq = ~((unsigned int)0);
  137. const SharedPtr<Peer> *best = (const SharedPtr<Peer> *)0;
  138. Mutex::Lock _l2(_peers_m);
  139. Mutex::Lock _l1(_upstreams_m);
  140. for(std::vector<Address>::const_iterator a(_upstreamAddresses.begin());a!=_upstreamAddresses.end();++a) {
  141. const SharedPtr<Peer> *p = _peers.get(*a);
  142. if (p) {
  143. const unsigned int q = (*p)->relayQuality(now);
  144. if (q <= bestq) {
  145. bestq = q;
  146. best = p;
  147. }
  148. }
  149. }
  150. if (!best)
  151. return SharedPtr<Peer>();
  152. return *best;
  153. }
  154. bool Topology::isUpstream(const Identity &id) const
  155. {
  156. Mutex::Lock _l(_upstreams_m);
  157. return (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),id.address()) != _upstreamAddresses.end());
  158. }
  159. bool Topology::shouldAcceptWorldUpdateFrom(const Address &addr) const
  160. {
  161. Mutex::Lock _l(_upstreams_m);
  162. if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),addr) != _upstreamAddresses.end())
  163. return true;
  164. for(std::vector< std::pair< uint64_t,Address> >::const_iterator s(_moonSeeds.begin());s!=_moonSeeds.end();++s) {
  165. if (s->second == addr)
  166. return true;
  167. }
  168. return false;
  169. }
  170. ZT_PeerRole Topology::role(const Address &ztaddr) const
  171. {
  172. Mutex::Lock _l(_upstreams_m);
  173. if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),ztaddr) != _upstreamAddresses.end()) {
  174. for(std::vector<World::Root>::const_iterator i(_planet.roots().begin());i!=_planet.roots().end();++i) {
  175. if (i->identity.address() == ztaddr)
  176. return ZT_PEER_ROLE_PLANET;
  177. }
  178. return ZT_PEER_ROLE_MOON;
  179. }
  180. return ZT_PEER_ROLE_LEAF;
  181. }
  182. bool Topology::isProhibitedEndpoint(const Address &ztaddr,const InetAddress &ipaddr) const
  183. {
  184. Mutex::Lock _l(_upstreams_m);
  185. // For roots the only permitted addresses are those defined. This adds just a little
  186. // bit of extra security against spoofing, replaying, etc.
  187. if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),ztaddr) != _upstreamAddresses.end()) {
  188. for(std::vector<World::Root>::const_iterator r(_planet.roots().begin());r!=_planet.roots().end();++r) {
  189. if (r->identity.address() == ztaddr) {
  190. if (r->stableEndpoints.size() == 0)
  191. return false; // no stable endpoints specified, so allow dynamic paths
  192. for(std::vector<InetAddress>::const_iterator e(r->stableEndpoints.begin());e!=r->stableEndpoints.end();++e) {
  193. if (ipaddr.ipsEqual(*e))
  194. return false;
  195. }
  196. }
  197. }
  198. for(std::vector<World>::const_iterator m(_moons.begin());m!=_moons.end();++m) {
  199. for(std::vector<World::Root>::const_iterator r(m->roots().begin());r!=m->roots().end();++r) {
  200. if (r->identity.address() == ztaddr) {
  201. if (r->stableEndpoints.size() == 0)
  202. return false; // no stable endpoints specified, so allow dynamic paths
  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. return true;
  211. }
  212. return false;
  213. }
  214. bool Topology::addWorld(void *tPtr,const World &newWorld,bool alwaysAcceptNew)
  215. {
  216. if ((newWorld.type() != World::TYPE_PLANET)&&(newWorld.type() != World::TYPE_MOON))
  217. return false;
  218. Mutex::Lock _l2(_peers_m);
  219. Mutex::Lock _l1(_upstreams_m);
  220. World *existing = (World *)0;
  221. switch(newWorld.type()) {
  222. case World::TYPE_PLANET:
  223. existing = &_planet;
  224. break;
  225. case World::TYPE_MOON:
  226. for(std::vector< World >::iterator m(_moons.begin());m!=_moons.end();++m) {
  227. if (m->id() == newWorld.id()) {
  228. existing = &(*m);
  229. break;
  230. }
  231. }
  232. break;
  233. default:
  234. return false;
  235. }
  236. if (existing) {
  237. if (existing->shouldBeReplacedBy(newWorld))
  238. *existing = newWorld;
  239. else return false;
  240. } else if (newWorld.type() == World::TYPE_MOON) {
  241. if (alwaysAcceptNew) {
  242. _moons.push_back(newWorld);
  243. existing = &(_moons.back());
  244. } else {
  245. for(std::vector< std::pair<uint64_t,Address> >::iterator m(_moonSeeds.begin());m!=_moonSeeds.end();++m) {
  246. if (m->first == newWorld.id()) {
  247. for(std::vector<World::Root>::const_iterator r(newWorld.roots().begin());r!=newWorld.roots().end();++r) {
  248. if (r->identity.address() == m->second) {
  249. _moonSeeds.erase(m);
  250. _moons.push_back(newWorld);
  251. existing = &(_moons.back());
  252. break;
  253. }
  254. }
  255. if (existing)
  256. break;
  257. }
  258. }
  259. }
  260. if (!existing)
  261. return false;
  262. } else {
  263. return false;
  264. }
  265. try {
  266. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> sbuf;
  267. existing->serialize(sbuf,false);
  268. uint64_t idtmp[2];
  269. idtmp[0] = existing->id(); idtmp[1] = 0;
  270. RR->node->stateObjectPut(tPtr,(existing->type() == World::TYPE_PLANET) ? ZT_STATE_OBJECT_PLANET : ZT_STATE_OBJECT_MOON,idtmp,sbuf.data(),sbuf.size());
  271. } catch ( ... ) {}
  272. _memoizeUpstreams(tPtr);
  273. return true;
  274. }
  275. void Topology::addMoon(void *tPtr,const uint64_t id,const Address &seed)
  276. {
  277. char tmp[ZT_WORLD_MAX_SERIALIZED_LENGTH];
  278. uint64_t idtmp[2];
  279. idtmp[0] = id; idtmp[1] = 0;
  280. int n = RR->node->stateObjectGet(tPtr,ZT_STATE_OBJECT_MOON,idtmp,tmp,sizeof(tmp));
  281. if (n > 0) {
  282. try {
  283. World w;
  284. w.deserialize(Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH>(tmp,(unsigned int)n));
  285. if ((w.type() == World::TYPE_MOON)&&(w.id() == id)) {
  286. addWorld(tPtr,w,true);
  287. return;
  288. }
  289. } catch ( ... ) {}
  290. }
  291. if (seed) {
  292. Mutex::Lock _l(_upstreams_m);
  293. if (std::find(_moonSeeds.begin(),_moonSeeds.end(),std::pair<uint64_t,Address>(id,seed)) == _moonSeeds.end())
  294. _moonSeeds.push_back(std::pair<uint64_t,Address>(id,seed));
  295. }
  296. }
  297. void Topology::removeMoon(void *tPtr,const uint64_t id)
  298. {
  299. Mutex::Lock _l2(_peers_m);
  300. Mutex::Lock _l1(_upstreams_m);
  301. std::vector<World> nm;
  302. for(std::vector<World>::const_iterator m(_moons.begin());m!=_moons.end();++m) {
  303. if (m->id() != id) {
  304. nm.push_back(*m);
  305. } else {
  306. uint64_t idtmp[2];
  307. idtmp[0] = id; idtmp[1] = 0;
  308. RR->node->stateObjectDelete(tPtr,ZT_STATE_OBJECT_MOON,idtmp);
  309. }
  310. }
  311. _moons.swap(nm);
  312. std::vector< std::pair<uint64_t,Address> > cm;
  313. for(std::vector< std::pair<uint64_t,Address> >::const_iterator m(_moonSeeds.begin());m!=_moonSeeds.end();++m) {
  314. if (m->first != id)
  315. cm.push_back(*m);
  316. }
  317. _moonSeeds.swap(cm);
  318. _memoizeUpstreams(tPtr);
  319. }
  320. void Topology::doPeriodicTasks(void *tPtr,int64_t now)
  321. {
  322. {
  323. Mutex::Lock _l1(_peers_m);
  324. Mutex::Lock _l2(_upstreams_m);
  325. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
  326. Address *a = (Address *)0;
  327. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  328. while (i.next(a,p)) {
  329. if ( (!(*p)->isAlive(now)) && (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),*a) == _upstreamAddresses.end()) ) {
  330. _savePeer(tPtr,*p);
  331. _peers.erase(*a);
  332. }
  333. }
  334. }
  335. {
  336. Mutex::Lock _l(_paths_m);
  337. Hashtable< Path::HashKey,SharedPtr<Path> >::Iterator i(_paths);
  338. Path::HashKey *k = (Path::HashKey *)0;
  339. SharedPtr<Path> *p = (SharedPtr<Path> *)0;
  340. while (i.next(k,p)) {
  341. if (p->references() <= 1)
  342. _paths.erase(*k);
  343. }
  344. }
  345. }
  346. void Topology::_memoizeUpstreams(void *tPtr)
  347. {
  348. // assumes _upstreams_m and _peers_m are locked
  349. _upstreamAddresses.clear();
  350. _amUpstream = false;
  351. for(std::vector<World::Root>::const_iterator i(_planet.roots().begin());i!=_planet.roots().end();++i) {
  352. if (i->identity == RR->identity) {
  353. _amUpstream = true;
  354. } else if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),i->identity.address()) == _upstreamAddresses.end()) {
  355. _upstreamAddresses.push_back(i->identity.address());
  356. SharedPtr<Peer> &hp = _peers[i->identity.address()];
  357. if (!hp)
  358. hp = new Peer(RR,RR->identity,i->identity);
  359. }
  360. }
  361. for(std::vector<World>::const_iterator m(_moons.begin());m!=_moons.end();++m) {
  362. for(std::vector<World::Root>::const_iterator i(m->roots().begin());i!=m->roots().end();++i) {
  363. if (i->identity == RR->identity) {
  364. _amUpstream = true;
  365. } else if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),i->identity.address()) == _upstreamAddresses.end()) {
  366. _upstreamAddresses.push_back(i->identity.address());
  367. SharedPtr<Peer> &hp = _peers[i->identity.address()];
  368. if (!hp)
  369. hp = new Peer(RR,RR->identity,i->identity);
  370. }
  371. }
  372. }
  373. std::sort(_upstreamAddresses.begin(),_upstreamAddresses.end());
  374. }
  375. void Topology::_savePeer(void *tPtr,const SharedPtr<Peer> &peer)
  376. {
  377. try {
  378. Buffer<ZT_PEER_MAX_SERIALIZED_STATE_SIZE> buf;
  379. peer->serializeForCache(buf);
  380. uint64_t tmpid[2]; tmpid[0] = peer->address().toInt(); tmpid[1] = 0;
  381. RR->node->stateObjectPut(tPtr,ZT_STATE_OBJECT_PEER,tmpid,buf.data(),buf.size());
  382. } catch ( ... ) {} // sanity check, discard invalid entries
  383. }
  384. } // namespace ZeroTier