Topology.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (c)2013-2020 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: 2024-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 "Topology.hpp"
  14. namespace ZeroTier {
  15. // Sorts roots so as to put the lowest latency alive root first.
  16. struct _RootSortComparisonOperator
  17. {
  18. ZT_ALWAYS_INLINE _RootSortComparisonOperator(const int64_t now) : _now(now) {}
  19. ZT_ALWAYS_INLINE bool operator()(const SharedPtr<Peer> &a,const SharedPtr<Peer> &b)
  20. {
  21. const int64_t now = _now;
  22. if (a->active(now)) {
  23. if (b->active(now))
  24. return (a->latency() < b->latency());
  25. return true;
  26. }
  27. return a->lastReceive() < b->lastReceive();
  28. }
  29. const int64_t _now;
  30. };
  31. Topology::Topology(const RuntimeEnvironment *renv,const Identity &myId,void *tPtr) :
  32. RR(renv),
  33. _myIdentity(myId),
  34. _numConfiguredPhysicalPaths(0),
  35. _peers(128),
  36. _paths(256)
  37. {
  38. uint64_t idtmp[2]; idtmp[0] = 0; idtmp[1] = 0;
  39. std::vector<uint8_t> data(RR->node->stateObjectGet(tPtr,ZT_STATE_OBJECT_ROOTS,idtmp));
  40. if (!data.empty()) {
  41. uint8_t *dptr = data.data();
  42. int drem = (int)data.size();
  43. while (drem > 0) {
  44. Identity id;
  45. int l = id.unmarshal(dptr,drem);
  46. if (l > 0) {
  47. _roots.insert(id);
  48. dptr += l;
  49. drem -= l;
  50. }
  51. }
  52. }
  53. for(std::set<Identity>::const_iterator r(_roots.begin());r!=_roots.end();++r) {
  54. SharedPtr<Peer> p;
  55. _loadCached(tPtr,r->address(),p);
  56. if ((!p)||(p->identity() != *r)) {
  57. p.set(new Peer(RR));
  58. p->init(myId,*r);
  59. }
  60. _rootPeers.push_back(p);
  61. }
  62. }
  63. Topology::~Topology()
  64. {
  65. }
  66. SharedPtr<Peer> Topology::add(void *tPtr,const SharedPtr<Peer> &peer)
  67. {
  68. RWMutex::Lock _l(_peers_l);
  69. SharedPtr<Peer> &hp = _peers[peer->address()];
  70. if (hp)
  71. return hp;
  72. _loadCached(tPtr,peer->address(),hp);
  73. if (hp)
  74. return hp;
  75. hp = peer;
  76. return peer;
  77. }
  78. void Topology::getAllPeers(std::vector< SharedPtr<Peer> > &allPeers) const
  79. {
  80. RWMutex::RLock l(_peers_l);
  81. allPeers.clear();
  82. allPeers.reserve(_peers.size());
  83. Hashtable< Address,SharedPtr<Peer> >::Iterator i(*(const_cast<Hashtable< Address,SharedPtr<Peer> > *>(&_peers)));
  84. Address *a = (Address *)0;
  85. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  86. while (i.next(a,p))
  87. allPeers.push_back(*p);
  88. }
  89. void Topology::setPhysicalPathConfiguration(const struct sockaddr_storage *pathNetwork,const ZT_PhysicalPathConfiguration *pathConfig)
  90. {
  91. if (!pathNetwork) {
  92. _numConfiguredPhysicalPaths = 0;
  93. } else {
  94. std::map<InetAddress,ZT_PhysicalPathConfiguration> cpaths;
  95. for(unsigned int i=0,j=_numConfiguredPhysicalPaths;i<j;++i)
  96. cpaths[_physicalPathConfig[i].first] = _physicalPathConfig[i].second;
  97. if (pathConfig) {
  98. ZT_PhysicalPathConfiguration pc(*pathConfig);
  99. if (pc.mtu <= 0)
  100. pc.mtu = ZT_DEFAULT_PHYSMTU;
  101. else if (pc.mtu < ZT_MIN_PHYSMTU)
  102. pc.mtu = ZT_MIN_PHYSMTU;
  103. else if (pc.mtu > ZT_MAX_PHYSMTU)
  104. pc.mtu = ZT_MAX_PHYSMTU;
  105. cpaths[*(reinterpret_cast<const InetAddress *>(pathNetwork))] = pc;
  106. } else {
  107. cpaths.erase(*(reinterpret_cast<const InetAddress *>(pathNetwork)));
  108. }
  109. unsigned int cnt = 0;
  110. for(std::map<InetAddress,ZT_PhysicalPathConfiguration>::const_iterator i(cpaths.begin());((i!=cpaths.end())&&(cnt<ZT_MAX_CONFIGURABLE_PATHS));++i) {
  111. _physicalPathConfig[cnt].first = i->first;
  112. _physicalPathConfig[cnt].second = i->second;
  113. ++cnt;
  114. }
  115. _numConfiguredPhysicalPaths = cnt;
  116. }
  117. }
  118. void Topology::addRoot(void *tPtr,const Identity &id,const InetAddress &bootstrap)
  119. {
  120. if (id == _myIdentity) return; // sanity check
  121. RWMutex::Lock l1(_peers_l);
  122. std::pair< std::set<Identity>::iterator,bool > ir(_roots.insert(id));
  123. if (ir.second) {
  124. SharedPtr<Peer> &p = _peers[id.address()];
  125. if (!p) {
  126. p.set(new Peer(RR));
  127. p->init(_myIdentity,id);
  128. if (bootstrap)
  129. p->setBootstrap(Endpoint(bootstrap));
  130. }
  131. _rootPeers.push_back(p);
  132. uint8_t *const roots = (uint8_t *)malloc(ZT_IDENTITY_MARSHAL_SIZE_MAX * _roots.size());
  133. if (roots) {
  134. int p = 0;
  135. for(std::set<Identity>::const_iterator i(_roots.begin());i!=_roots.end();++i) {
  136. int pp = i->marshal(roots + p,false);
  137. if (pp > 0)
  138. p += pp;
  139. }
  140. uint64_t id[2];
  141. id[0] = 0;
  142. id[1] = 0;
  143. RR->node->stateObjectPut(tPtr,ZT_STATE_OBJECT_ROOTS,id,roots,(unsigned int)p);
  144. free(roots);
  145. }
  146. }
  147. }
  148. bool Topology::removeRoot(const Identity &id)
  149. {
  150. RWMutex::Lock l1(_peers_l);
  151. std::set<Identity>::iterator r(_roots.find(id));
  152. if (r != _roots.end()) {
  153. for(std::vector< SharedPtr<Peer> >::iterator p(_rootPeers.begin());p!=_rootPeers.end();++p) {
  154. if ((*p)->identity() == id) {
  155. _rootPeers.erase(p);
  156. break;
  157. }
  158. }
  159. _roots.erase(r);
  160. return true;
  161. }
  162. return false;
  163. }
  164. void Topology::rankRoots(const int64_t now)
  165. {
  166. RWMutex::Lock l1(_peers_l);
  167. std::sort(_rootPeers.begin(),_rootPeers.end(),_RootSortComparisonOperator(now));
  168. }
  169. void Topology::doPeriodicTasks(void *tPtr,const int64_t now)
  170. {
  171. {
  172. RWMutex::Lock l1(_peers_l);
  173. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
  174. Address *a = (Address *)0;
  175. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  176. while (i.next(a,p)) {
  177. if ( (!(*p)->alive(now)) && (_roots.count((*p)->identity()) == 0) ) {
  178. (*p)->save(tPtr);
  179. _peers.erase(*a);
  180. }
  181. }
  182. }
  183. {
  184. RWMutex::Lock l1(_paths_l);
  185. Hashtable< Path::HashKey,SharedPtr<Path> >::Iterator i(_paths);
  186. Path::HashKey *k = (Path::HashKey *)0;
  187. SharedPtr<Path> *p = (SharedPtr<Path> *)0;
  188. while (i.next(k,p)) {
  189. if (p->references() <= 1)
  190. _paths.erase(*k);
  191. }
  192. }
  193. }
  194. void Topology::saveAll(void *tPtr)
  195. {
  196. RWMutex::RLock l(_peers_l);
  197. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
  198. Address *a = (Address *)0;
  199. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  200. while (i.next(a,p)) {
  201. if ( (!(*p)->alive(RR->node->now())) && (_roots.count((*p)->identity()) == 0) ) {
  202. (*p)->save((void *)0);
  203. }
  204. }
  205. }
  206. void Topology::_loadCached(void *tPtr,const Address &zta,SharedPtr<Peer> &peer)
  207. {
  208. uint64_t id[2];
  209. id[0] = zta.toInt();
  210. id[1] = 0;
  211. std::vector<uint8_t> data(RR->node->stateObjectGet(tPtr,ZT_STATE_OBJECT_PEER,id));
  212. if (!data.empty()) {
  213. const uint8_t *d = data.data();
  214. int dl = (int)data.size();
  215. for(;;) {
  216. Peer *const p = new Peer(RR);
  217. int n = p->unmarshal(d,dl);
  218. if (n > 0) {
  219. // TODO: will eventually handle multiple peers
  220. peer.set(p);
  221. return;
  222. } else {
  223. delete p;
  224. }
  225. }
  226. }
  227. }
  228. } // namespace ZeroTier