Topology.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 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. Topology::Topology(const RuntimeEnvironment *renv,void *tPtr) :
  36. RR(renv),
  37. _numConfiguredPhysicalPaths(0)
  38. {
  39. }
  40. Topology::~Topology()
  41. {
  42. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
  43. Address *a = (Address *)0;
  44. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  45. while (i.next(a,p))
  46. _savePeer((void *)0,*p);
  47. }
  48. SharedPtr<Peer> Topology::addPeer(void *tPtr,const SharedPtr<Peer> &peer)
  49. {
  50. SharedPtr<Peer> np;
  51. {
  52. Mutex::Lock _l(_peers_m);
  53. SharedPtr<Peer> &hp = _peers[peer->address()];
  54. if (!hp)
  55. hp = peer;
  56. np = hp;
  57. }
  58. return np;
  59. }
  60. SharedPtr<Peer> Topology::getPeer(void *tPtr,const Address &zta)
  61. {
  62. if (zta == RR->identity.address())
  63. return SharedPtr<Peer>();
  64. {
  65. Mutex::Lock _l(_peers_m);
  66. const SharedPtr<Peer> *const ap = _peers.get(zta);
  67. if (ap)
  68. return *ap;
  69. }
  70. try {
  71. Buffer<ZT_PEER_MAX_SERIALIZED_STATE_SIZE> buf;
  72. uint64_t idbuf[2]; idbuf[0] = zta.toInt(); idbuf[1] = 0;
  73. int len = RR->node->stateObjectGet(tPtr,ZT_STATE_OBJECT_PEER,idbuf,buf.unsafeData(),ZT_PEER_MAX_SERIALIZED_STATE_SIZE);
  74. if (len > 0) {
  75. buf.setSize(len);
  76. Mutex::Lock _l(_peers_m);
  77. SharedPtr<Peer> &ap = _peers[zta];
  78. if (ap)
  79. return ap;
  80. ap = Peer::deserializeFromCache(RR->node->now(),tPtr,buf,RR);
  81. if (!ap) {
  82. _peers.erase(zta);
  83. }
  84. return SharedPtr<Peer>();
  85. }
  86. } catch ( ... ) {} // ignore invalid identities or other strange failures
  87. return SharedPtr<Peer>();
  88. }
  89. Identity Topology::getIdentity(void *tPtr,const Address &zta)
  90. {
  91. if (zta == RR->identity.address()) {
  92. return RR->identity;
  93. } else {
  94. Mutex::Lock _l(_peers_m);
  95. const SharedPtr<Peer> *const ap = _peers.get(zta);
  96. if (ap)
  97. return (*ap)->identity();
  98. }
  99. return Identity();
  100. }
  101. SharedPtr<Peer> Topology::getUpstreamPeer()
  102. {
  103. return SharedPtr<Peer>();
  104. }
  105. bool Topology::isUpstream(const Identity &id) const
  106. {
  107. return false;
  108. }
  109. ZT_PeerRole Topology::role(const Address &ztaddr) const
  110. {
  111. return ZT_PEER_ROLE_LEAF;
  112. }
  113. bool Topology::isProhibitedEndpoint(const Address &ztaddr,const InetAddress &ipaddr) const
  114. {
  115. return false;
  116. }
  117. void Topology::doPeriodicTasks(void *tPtr,int64_t now)
  118. {
  119. {
  120. Mutex::Lock _l1(_peers_m);
  121. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
  122. Address *a = (Address *)0;
  123. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  124. while (i.next(a,p)) {
  125. if (!(*p)->isAlive(now)) {
  126. _savePeer(tPtr,*p);
  127. _peers.erase(*a);
  128. }
  129. }
  130. }
  131. {
  132. Mutex::Lock _l(_paths_m);
  133. Hashtable< Path::HashKey,SharedPtr<Path> >::Iterator i(_paths);
  134. Path::HashKey *k = (Path::HashKey *)0;
  135. SharedPtr<Path> *p = (SharedPtr<Path> *)0;
  136. while (i.next(k,p)) {
  137. if (p->references() <= 1)
  138. _paths.erase(*k);
  139. }
  140. }
  141. }
  142. void Topology::setPhysicalPathConfiguration(const struct sockaddr_storage *pathNetwork,const ZT_PhysicalPathConfiguration *pathConfig)
  143. {
  144. if (!pathNetwork) {
  145. _numConfiguredPhysicalPaths = 0;
  146. } else {
  147. std::map<InetAddress,ZT_PhysicalPathConfiguration> cpaths;
  148. for(unsigned int i=0,j=_numConfiguredPhysicalPaths;i<j;++i)
  149. cpaths[_physicalPathConfig[i].first] = _physicalPathConfig[i].second;
  150. if (pathConfig) {
  151. ZT_PhysicalPathConfiguration pc(*pathConfig);
  152. if (pc.mtu <= 0)
  153. pc.mtu = ZT_DEFAULT_PHYSMTU;
  154. else if (pc.mtu < ZT_MIN_PHYSMTU)
  155. pc.mtu = ZT_MIN_PHYSMTU;
  156. else if (pc.mtu > ZT_MAX_PHYSMTU)
  157. pc.mtu = ZT_MAX_PHYSMTU;
  158. cpaths[*(reinterpret_cast<const InetAddress *>(pathNetwork))] = pc;
  159. } else {
  160. cpaths.erase(*(reinterpret_cast<const InetAddress *>(pathNetwork)));
  161. }
  162. unsigned int cnt = 0;
  163. for(std::map<InetAddress,ZT_PhysicalPathConfiguration>::const_iterator i(cpaths.begin());((i!=cpaths.end())&&(cnt<ZT_MAX_CONFIGURABLE_PATHS));++i) {
  164. _physicalPathConfig[cnt].first = i->first;
  165. _physicalPathConfig[cnt].second = i->second;
  166. ++cnt;
  167. }
  168. _numConfiguredPhysicalPaths = cnt;
  169. }
  170. }
  171. void Topology::_savePeer(void *tPtr,const SharedPtr<Peer> &peer)
  172. {
  173. try {
  174. Buffer<ZT_PEER_MAX_SERIALIZED_STATE_SIZE> buf;
  175. peer->serializeForCache(buf);
  176. uint64_t tmpid[2]; tmpid[0] = peer->address().toInt(); tmpid[1] = 0;
  177. RR->node->stateObjectPut(tPtr,ZT_STATE_OBJECT_PEER,tmpid,buf.data(),buf.size());
  178. } catch ( ... ) {} // sanity check, discard invalid entries
  179. }
  180. } // namespace ZeroTier