Topology.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include "Constants.hpp"
  28. #include "Topology.hpp"
  29. #include "RuntimeEnvironment.hpp"
  30. #include "Node.hpp"
  31. #include "Network.hpp"
  32. #include "NetworkConfig.hpp"
  33. #include "Buffer.hpp"
  34. namespace ZeroTier {
  35. // Old root servers
  36. //#define ZT_DEFAULT_WORLD_LENGTH 494
  37. //static const unsigned char ZT_DEFAULT_WORLD[ZT_DEFAULT_WORLD_LENGTH] = {0x01,0x00,0x00,0x00,0x00,0x08,0xea,0xc9,0x0a,0x00,0x00,0x01,0x4f,0xdf,0xbf,0xfc,0xbb,0x6c,0x7e,0x15,0x67,0x85,0x1b,0xb4,0x65,0x04,0x01,0xaf,0x56,0xbf,0xe7,0x63,0x9d,0x77,0xef,0xa4,0x1e,0x61,0x53,0x88,0xcb,0x8d,0x78,0xe5,0x47,0x38,0x98,0x5a,0x6c,0x8a,0xdd,0xe6,0x9c,0x65,0xdf,0x1a,0x80,0x63,0xce,0x2e,0x4d,0x48,0x24,0x3d,0x68,0x87,0x96,0x13,0x89,0xba,0x25,0x6f,0xc9,0xb0,0x9f,0x20,0xc5,0x4c,0x51,0x7b,0x30,0xb7,0x5f,0xba,0xca,0xa4,0xc5,0x48,0xa3,0x15,0xab,0x2f,0x1d,0x64,0xe8,0x04,0x42,0xb3,0x1c,0x51,0x8b,0x2a,0x04,0x01,0xf8,0xe1,0x81,0xaf,0x60,0x2f,0x70,0x3e,0xcd,0x0b,0x21,0x38,0x19,0x62,0x02,0xbd,0x0e,0x33,0x1d,0x0a,0x7b,0xf1,0xec,0xad,0xef,0x54,0xb3,0x7b,0x17,0x84,0xaa,0xda,0x0a,0x85,0x5d,0x0b,0x1c,0x05,0x83,0xb9,0x0e,0x3e,0xe3,0xb4,0xd1,0x8b,0x5b,0x64,0xf7,0xcf,0xe1,0xff,0x5d,0xc2,0x2a,0xcf,0x60,0x7b,0x09,0xb4,0xa3,0x86,0x3c,0x5a,0x7e,0x31,0xa0,0xc7,0xb4,0x86,0xe3,0x41,0x33,0x04,0x7e,0x19,0x87,0x6a,0xba,0x00,0x2a,0x6e,0x2b,0x23,0x18,0x93,0x0f,0x60,0xeb,0x09,0x7f,0x70,0xd0,0xf4,0xb0,0x28,0xb2,0xcd,0x6d,0x3d,0x0c,0x63,0xc0,0x14,0xb9,0x03,0x9f,0xf3,0x53,0x90,0xe4,0x11,0x81,0xf2,0x16,0xfb,0x2e,0x6f,0xa8,0xd9,0x5c,0x1e,0xe9,0x66,0x71,0x56,0x41,0x19,0x05,0xc3,0xdc,0xcf,0xea,0x78,0xd8,0xc6,0xdf,0xaf,0xba,0x68,0x81,0x70,0xb3,0xfa,0x00,0x01,0x04,0xc6,0xc7,0x61,0xdc,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,0x01,0x04,0x6b,0xbf,0x2e,0xd2,0x27,0x09,0x8a,0xcf,0x05,0x9f,0xe3,0x00,0x48,0x2f,0x6e,0xe5,0xdf,0xe9,0x02,0x31,0x9b,0x41,0x9d,0xe5,0xbd,0xc7,0x65,0x20,0x9c,0x0e,0xcd,0xa3,0x8c,0x4d,0x6e,0x4f,0xcf,0x0d,0x33,0x65,0x83,0x98,0xb4,0x52,0x7d,0xcd,0x22,0xf9,0x31,0x12,0xfb,0x9b,0xef,0xd0,0x2f,0xd7,0x8b,0xf7,0x26,0x1b,0x33,0x3f,0xc1,0x05,0xd1,0x92,0xa6,0x23,0xca,0x9e,0x50,0xfc,0x60,0xb3,0x74,0xa5,0x00,0x01,0x04,0xa2,0xf3,0x4d,0x6f,0x27,0x09,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,0x01,0x04,0x80,0xc7,0xc5,0xd9,0x27,0x09};
  38. #define ZT_DEFAULT_WORLD_LENGTH 582
  39. static const unsigned char ZT_DEFAULT_WORLD[ZT_DEFAULT_WORLD_LENGTH] = {0x01,0x00,0x00,0x00,0x00,0x08,0xea,0xc9,0x0a,0x00,0x00,0x01,0x50,0xa6,0x54,0xe4,0x8e,0x72,0xb0,0x3b,0xbe,0x73,0xda,0xbd,0xfb,0x85,0x77,0x9f,0xc9,0x2e,0x17,0xc8,0x11,0x6e,0xda,0x61,0x80,0xd1,0x41,0xcb,0x7c,0x2d,0x2b,0xa4,0x34,0x75,0x19,0x64,0x20,0x80,0x0a,0x22,0x32,0xf2,0x01,0x6c,0xfe,0x79,0xa6,0x7d,0xec,0x10,0x7e,0x03,0xf3,0xa2,0xa0,0x19,0xc8,0x7c,0xfd,0x6c,0x56,0x52,0xa8,0xfb,0xdc,0xfb,0x93,0x81,0x3e,0xe4,0xe9,0x51,0xc1,0xe1,0x39,0x50,0xcd,0x17,0x82,0x9d,0x74,0xf1,0xa9,0x5b,0x03,0x14,0x2c,0xa7,0xc0,0x7f,0x21,0x8b,0xad,0xdd,0xa5,0x04,0x26,0x35,0xa6,0xab,0xc1,0x49,0x64,0x2c,0xda,0x65,0x52,0x77,0xf3,0xf0,0x70,0x00,0xcd,0xc3,0xff,0x3b,0x19,0x77,0x4c,0xab,0xb6,0x35,0xbb,0x77,0xcf,0x54,0xe5,0x6d,0x01,0x9d,0x43,0x92,0x0a,0x6d,0x00,0x23,0x8e,0x0a,0x3d,0xba,0x36,0xc3,0xa1,0xa4,0xad,0x13,0x8f,0x46,0xff,0xcc,0x8f,0x9e,0xc2,0x3c,0x06,0xf8,0x3b,0xf3,0xa2,0x5f,0x71,0xcc,0x07,0x35,0x7f,0x02,0xd6,0xdd,0xca,0x6a,0xb5,0x00,0x4e,0x76,0x12,0x07,0xd8,0xb4,0x20,0x0b,0xe4,0x4f,0x47,0x8e,0x3d,0xa1,0x48,0xc1,0x60,0x99,0x11,0x0e,0xe7,0x1b,0x64,0x58,0x6d,0xda,0x11,0x8e,0x40,0x22,0xab,0x63,0x68,0x2c,0xe1,0x37,0xda,0x8b,0xa8,0x17,0xfc,0x7f,0x73,0xaa,0x31,0x63,0xf2,0xe3,0x33,0x93,0x3e,0x29,0x94,0xc4,0x6b,0x4f,0x41,0x19,0x30,0x7b,0xe8,0x85,0x5a,0x72,0x00,0x0a,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,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,0x68,0xee,0xb6,0x53,0x27,0x09,0x06,0x20,0x01,0x19,0xf0,0xac,0x00,0x08,0x09,0x54,0x00,0x00,0xff,0xfe,0x15,0xf3,0xf4,0x27,0x09,0x04,0x80,0xc7,0xb6,0x09,0x27,0x09,0x06,0x24,0x00,0x61,0x80,0x00,0x00,0x00,0xd0,0x00,0x00,0x00,0x00,0x00,0x1b,0x10,0x01,0x27,0x09,0x16,0xeb,0xbd,0x6c,0x5d,0x00,0x47,0xd3,0x9b,0xca,0x9d,0x0a,0x5c,0xf7,0x01,0x48,0xe3,0x9f,0x6c,0x45,0x19,0x9e,0x17,0xe0,0xe3,0x2e,0x4e,0x46,0xca,0xc0,0x1a,0xe5,0xbc,0xb2,0x12,0x24,0x13,0x7b,0x09,0x7f,0x40,0xbd,0xd9,0x82,0xa9,0x21,0xc3,0xaa,0xbd,0xcb,0x9a,0xda,0x8b,0x4f,0x2b,0xb0,0x59,0x37,0x53,0xbf,0xdb,0x21,0xcf,0x12,0xea,0xc2,0x8c,0x8d,0x90,0x42,0x00,0x0a,0x04,0x2d,0x21,0x04,0x43,0x27,0x09,0x06,0x26,0x00,0x3c,0x00,0x00,0x00,0x00,0x00,0xf0,0x3c,0x91,0xff,0xfe,0x67,0xb7,0x04,0x27,0x09,0x04,0x8b,0xa2,0x9d,0xf3,0x27,0x09,0x06,0x2a,0x01,0x7e,0x01,0x00,0x00,0x00,0x00,0xf0,0x3c,0x91,0xff,0xfe,0x67,0x3f,0xfd,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};
  40. Topology::Topology(const RuntimeEnvironment *renv) :
  41. RR(renv),
  42. _amRoot(false)
  43. {
  44. std::string alls(RR->node->dataStoreGet("peers.save"));
  45. const uint8_t *all = reinterpret_cast<const uint8_t *>(alls.data());
  46. RR->node->dataStoreDelete("peers.save");
  47. unsigned int ptr = 0;
  48. while ((ptr + 4) < alls.size()) {
  49. try {
  50. const unsigned int reclen = ( // each Peer serialized record is prefixed by a record length
  51. ((((unsigned int)all[ptr]) & 0xff) << 24) |
  52. ((((unsigned int)all[ptr + 1]) & 0xff) << 16) |
  53. ((((unsigned int)all[ptr + 2]) & 0xff) << 8) |
  54. (((unsigned int)all[ptr + 3]) & 0xff)
  55. );
  56. unsigned int pos = 0;
  57. SharedPtr<Peer> p(Peer::deserializeNew(RR->identity,Buffer<ZT_PEER_SUGGESTED_SERIALIZATION_BUFFER_SIZE>(all + ptr,reclen + 4),pos));
  58. ptr += pos;
  59. if (!p)
  60. break; // stop if invalid records
  61. if (p->address() != RR->identity.address())
  62. _peers[p->address()] = p;
  63. } catch ( ... ) {
  64. break; // stop if invalid records
  65. }
  66. }
  67. clean(RR->node->now());
  68. std::string dsWorld(RR->node->dataStoreGet("world"));
  69. World cachedWorld;
  70. try {
  71. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> dswtmp(dsWorld.data(),dsWorld.length());
  72. cachedWorld.deserialize(dswtmp,0);
  73. } catch ( ... ) {
  74. cachedWorld = World(); // clear if cached world is invalid
  75. }
  76. World defaultWorld;
  77. {
  78. Buffer<ZT_DEFAULT_WORLD_LENGTH> wtmp(ZT_DEFAULT_WORLD,ZT_DEFAULT_WORLD_LENGTH);
  79. defaultWorld.deserialize(wtmp,0); // throws on error, which would indicate a bad static variable up top
  80. }
  81. if (cachedWorld.shouldBeReplacedBy(defaultWorld,false)) {
  82. _setWorld(defaultWorld);
  83. if (dsWorld.length() > 0)
  84. RR->node->dataStoreDelete("world");
  85. } else _setWorld(cachedWorld);
  86. }
  87. Topology::~Topology()
  88. {
  89. Buffer<ZT_PEER_SUGGESTED_SERIALIZATION_BUFFER_SIZE> pbuf;
  90. std::string all;
  91. Address *a = (Address *)0;
  92. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  93. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
  94. while (i.next(a,p)) {
  95. if (std::find(_rootAddresses.begin(),_rootAddresses.end(),*a) == _rootAddresses.end()) {
  96. pbuf.clear();
  97. try {
  98. (*p)->serialize(pbuf);
  99. try {
  100. all.append((const char *)pbuf.data(),pbuf.size());
  101. } catch ( ... ) {
  102. return; // out of memory? just skip
  103. }
  104. } catch ( ... ) {} // peer too big? shouldn't happen, but it so skip
  105. }
  106. }
  107. RR->node->dataStorePut("peers.save",all,true);
  108. }
  109. SharedPtr<Peer> Topology::addPeer(const SharedPtr<Peer> &peer)
  110. {
  111. if (peer->address() == RR->identity.address()) {
  112. TRACE("BUG: addPeer() caught and ignored attempt to add peer for self");
  113. throw std::logic_error("cannot add peer for self");
  114. }
  115. SharedPtr<Peer> np;
  116. {
  117. Mutex::Lock _l(_lock);
  118. SharedPtr<Peer> &hp = _peers[peer->address()];
  119. if (!hp)
  120. hp = peer;
  121. np = hp;
  122. }
  123. np->use(RR->node->now());
  124. saveIdentity(np->identity());
  125. return np;
  126. }
  127. SharedPtr<Peer> Topology::getPeer(const Address &zta)
  128. {
  129. if (zta == RR->identity.address()) {
  130. TRACE("BUG: ignored attempt to getPeer() for self, returned NULL");
  131. return SharedPtr<Peer>();
  132. }
  133. Mutex::Lock _l(_lock);
  134. SharedPtr<Peer> &ap = _peers[zta];
  135. if (ap) {
  136. ap->use(RR->node->now());
  137. return ap;
  138. }
  139. Identity id(_getIdentity(zta));
  140. if (id) {
  141. try {
  142. ap = SharedPtr<Peer>(new Peer(RR->identity,id));
  143. ap->use(RR->node->now());
  144. return ap;
  145. } catch ( ... ) {} // invalid identity?
  146. }
  147. // If we get here it means we read an invalid cache identity or had some other error
  148. _peers.erase(zta);
  149. return SharedPtr<Peer>();
  150. }
  151. Identity Topology::getIdentity(const Address &zta)
  152. {
  153. {
  154. Mutex::Lock _l(_lock);
  155. SharedPtr<Peer> &ap = _peers[zta];
  156. if (ap)
  157. return ap->identity();
  158. }
  159. return _getIdentity(zta);
  160. }
  161. void Topology::saveIdentity(const Identity &id)
  162. {
  163. if (id) {
  164. char p[128];
  165. Utils::snprintf(p,sizeof(p),"iddb.d/%.10llx",(unsigned long long)id.address().toInt());
  166. RR->node->dataStorePut(p,id.toString(false),false);
  167. }
  168. }
  169. SharedPtr<Peer> Topology::getBestRoot(const Address *avoid,unsigned int avoidCount,bool strictAvoid)
  170. {
  171. const uint64_t now = RR->node->now();
  172. Mutex::Lock _l(_lock);
  173. if (_amRoot) {
  174. /* If I am a root server, the "best" root server is the one whose address
  175. * is numerically greater than mine (with wrap at top of list). This
  176. * causes packets searching for a route to pretty much literally
  177. * circumnavigate the globe rather than bouncing between just two. */
  178. if (_rootAddresses.size() > 1) { // gotta be one other than me for this to work
  179. for(unsigned long p=0;p<_rootAddresses.size();++p) {
  180. if (_rootAddresses[p] == RR->identity.address()) {
  181. for(unsigned long q=1;q<_rootAddresses.size();++q) {
  182. SharedPtr<Peer> *nextsn = _peers.get(_rootAddresses[(p + q) % _rootAddresses.size()]);
  183. if ((nextsn)&&((*nextsn)->hasActiveDirectPath(now))) {
  184. (*nextsn)->use(now);
  185. return *nextsn;
  186. }
  187. }
  188. break;
  189. }
  190. }
  191. }
  192. } else {
  193. /* If I am not a root server, the best root server is the active one with
  194. * the lowest latency. */
  195. unsigned int bestLatencyOverall = ~((unsigned int)0);
  196. unsigned int bestLatencyNotAvoid = ~((unsigned int)0);
  197. const SharedPtr<Peer> *bestOverall = (const SharedPtr<Peer> *)0;
  198. const SharedPtr<Peer> *bestNotAvoid = (const SharedPtr<Peer> *)0;
  199. for(std::vector< SharedPtr<Peer> >::const_iterator r(_rootPeers.begin());r!=_rootPeers.end();++r) {
  200. if ((*r)->hasActiveDirectPath(now)) {
  201. bool avoiding = false;
  202. for(unsigned int i=0;i<avoidCount;++i) {
  203. if (avoid[i] == (*r)->address()) {
  204. avoiding = true;
  205. break;
  206. }
  207. }
  208. unsigned int l = (*r)->latency();
  209. if (!l) l = ~l; // zero latency indicates no measurment, so make this 'max'
  210. if (l <= bestLatencyOverall) {
  211. bestLatencyOverall = l;
  212. bestOverall = &(*r);
  213. }
  214. if ((!avoiding)&&(l <= bestLatencyNotAvoid)) {
  215. bestLatencyNotAvoid = l;
  216. bestNotAvoid = &(*r);
  217. }
  218. }
  219. }
  220. if (bestNotAvoid) {
  221. (*bestNotAvoid)->use(now);
  222. return *bestNotAvoid;
  223. } else if ((!strictAvoid)&&(bestOverall)) {
  224. (*bestOverall)->use(now);
  225. return *bestOverall;
  226. }
  227. /*
  228. unsigned int l,bestLatency = 65536;
  229. uint64_t lds,ldr;
  230. // First look for a best root by comparing latencies, but exclude
  231. // root servers that have not responded to direct messages in order to
  232. // try to exclude any that are dead or unreachable.
  233. for(std::vector< SharedPtr<Peer> >::const_iterator sn(_rootPeers.begin());sn!=_rootPeers.end();) {
  234. // Skip explicitly avoided relays
  235. for(unsigned int i=0;i<avoidCount;++i) {
  236. if (avoid[i] == (*sn)->address())
  237. goto keep_searching_for_roots;
  238. }
  239. // Skip possibly comatose or unreachable relays
  240. lds = (*sn)->lastDirectSend();
  241. ldr = (*sn)->lastDirectReceive();
  242. if ((lds)&&(lds > ldr)&&((lds - ldr) > ZT_PEER_RELAY_CONVERSATION_LATENCY_THRESHOLD))
  243. goto keep_searching_for_roots;
  244. if ((*sn)->hasActiveDirectPath(now)) {
  245. l = (*sn)->latency();
  246. if (bestRoot) {
  247. if ((l)&&(l < bestLatency)) {
  248. bestLatency = l;
  249. bestRoot = *sn;
  250. }
  251. } else {
  252. if (l)
  253. bestLatency = l;
  254. bestRoot = *sn;
  255. }
  256. }
  257. keep_searching_for_roots:
  258. ++sn;
  259. }
  260. if (bestRoot) {
  261. bestRoot->use(now);
  262. return bestRoot;
  263. } else if (strictAvoid)
  264. return SharedPtr<Peer>();
  265. // If we have nothing from above, just pick one without avoidance criteria.
  266. for(std::vector< SharedPtr<Peer> >::const_iterator sn=_rootPeers.begin();sn!=_rootPeers.end();++sn) {
  267. if ((*sn)->hasActiveDirectPath(now)) {
  268. unsigned int l = (*sn)->latency();
  269. if (bestRoot) {
  270. if ((l)&&(l < bestLatency)) {
  271. bestLatency = l;
  272. bestRoot = *sn;
  273. }
  274. } else {
  275. if (l)
  276. bestLatency = l;
  277. bestRoot = *sn;
  278. }
  279. }
  280. }
  281. */
  282. }
  283. return SharedPtr<Peer>();
  284. }
  285. bool Topology::isUpstream(const Identity &id) const
  286. {
  287. if (isRoot(id))
  288. return true;
  289. std::vector< SharedPtr<Network> > nws(RR->node->allNetworks());
  290. for(std::vector< SharedPtr<Network> >::const_iterator nw(nws.begin());nw!=nws.end();++nw) {
  291. SharedPtr<NetworkConfig> nc((*nw)->config2());
  292. if (nc) {
  293. for(std::vector< std::pair<Address,InetAddress> >::const_iterator r(nc->relays().begin());r!=nc->relays().end();++r) {
  294. if (r->first == id.address())
  295. return true;
  296. }
  297. }
  298. }
  299. return false;
  300. }
  301. bool Topology::worldUpdateIfValid(const World &newWorld)
  302. {
  303. Mutex::Lock _l(_lock);
  304. if (_world.shouldBeReplacedBy(newWorld,true)) {
  305. _setWorld(newWorld);
  306. try {
  307. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> dswtmp;
  308. newWorld.serialize(dswtmp,false);
  309. RR->node->dataStorePut("world",dswtmp.data(),dswtmp.size(),false);
  310. } catch ( ... ) {
  311. RR->node->dataStoreDelete("world");
  312. }
  313. return true;
  314. }
  315. return false;
  316. }
  317. void Topology::clean(uint64_t now)
  318. {
  319. Mutex::Lock _l(_lock);
  320. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
  321. Address *a = (Address *)0;
  322. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  323. while (i.next(a,p)) {
  324. if (((now - (*p)->lastUsed()) >= ZT_PEER_IN_MEMORY_EXPIRATION)&&(std::find(_rootAddresses.begin(),_rootAddresses.end(),*a) == _rootAddresses.end())) {
  325. _peers.erase(*a);
  326. } else {
  327. (*p)->clean(RR,now);
  328. }
  329. }
  330. }
  331. unsigned long Topology::countAlive() const
  332. {
  333. const uint64_t now = RR->node->now();
  334. unsigned long cnt = 0;
  335. Mutex::Lock _l(_lock);
  336. Hashtable< Address,SharedPtr<Peer> >::Iterator i(const_cast<Topology *>(this)->_peers);
  337. Address *a = (Address *)0;
  338. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  339. while (i.next(a,p)) {
  340. if ((*p)->alive(now))
  341. ++cnt;
  342. }
  343. return cnt;
  344. }
  345. Identity Topology::_getIdentity(const Address &zta)
  346. {
  347. char p[128];
  348. Utils::snprintf(p,sizeof(p),"iddb.d/%.10llx",(unsigned long long)zta.toInt());
  349. std::string ids(RR->node->dataStoreGet(p));
  350. if (ids.length() > 0) {
  351. try {
  352. return Identity(ids);
  353. } catch ( ... ) {} // ignore invalid IDs
  354. }
  355. return Identity();
  356. }
  357. void Topology::_setWorld(const World &newWorld)
  358. {
  359. // assumed _lock is locked (or in constructor)
  360. _world = newWorld;
  361. _amRoot = false;
  362. _rootAddresses.clear();
  363. _rootPeers.clear();
  364. for(std::vector<World::Root>::const_iterator r(_world.roots().begin());r!=_world.roots().end();++r) {
  365. _rootAddresses.push_back(r->identity.address());
  366. if (r->identity.address() == RR->identity.address()) {
  367. _amRoot = true;
  368. } else {
  369. SharedPtr<Peer> *rp = _peers.get(r->identity.address());
  370. if (rp) {
  371. _rootPeers.push_back(*rp);
  372. } else {
  373. SharedPtr<Peer> newrp(new Peer(RR->identity,r->identity));
  374. _peers.set(r->identity.address(),newrp);
  375. _rootPeers.push_back(newrp);
  376. }
  377. }
  378. }
  379. }
  380. } // namespace ZeroTier