Topology.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #ifndef ZT_TOPOLOGY_HPP
  28. #define ZT_TOPOLOGY_HPP
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <map>
  32. #include <vector>
  33. #include <stdexcept>
  34. #include <algorithm>
  35. #include "Constants.hpp"
  36. #include "Address.hpp"
  37. #include "Identity.hpp"
  38. #include "Peer.hpp"
  39. #include "Mutex.hpp"
  40. #include "InetAddress.hpp"
  41. #include "Hashtable.hpp"
  42. #include "World.hpp"
  43. namespace ZeroTier {
  44. class RuntimeEnvironment;
  45. /**
  46. * Database of network topology
  47. */
  48. class Topology
  49. {
  50. public:
  51. Topology(const RuntimeEnvironment *renv);
  52. ~Topology();
  53. /**
  54. * Add a peer to database
  55. *
  56. * This will not replace existing peers. In that case the existing peer
  57. * record is returned.
  58. *
  59. * @param peer Peer to add
  60. * @return New or existing peer (should replace 'peer')
  61. */
  62. SharedPtr<Peer> addPeer(const SharedPtr<Peer> &peer);
  63. /**
  64. * Get a peer from its address
  65. *
  66. * @param zta ZeroTier address of peer
  67. * @return Peer or NULL if not found
  68. */
  69. SharedPtr<Peer> getPeer(const Address &zta);
  70. /**
  71. * @return Vector of peers that are root servers
  72. */
  73. inline std::vector< SharedPtr<Peer> > rootPeers() const
  74. {
  75. Mutex::Lock _l(_lock);
  76. return _rootPeers;
  77. }
  78. /**
  79. * Get the current favorite root server
  80. *
  81. * @return Root server with lowest latency or NULL if none
  82. */
  83. inline SharedPtr<Peer> getBestRoot()
  84. {
  85. return getBestRoot((const Address *)0,0,false);
  86. }
  87. /**
  88. * Get the best root server, avoiding root servers listed in an array
  89. *
  90. * This will get the best root server (lowest latency, etc.) but will
  91. * try to avoid the listed root servers, only using them if no others
  92. * are available.
  93. *
  94. * @param avoid Nodes to avoid
  95. * @param avoidCount Number of nodes to avoid
  96. * @param strictAvoid If false, consider avoided root servers anyway if no non-avoid root servers are available
  97. * @return Root server or NULL if none available
  98. */
  99. SharedPtr<Peer> getBestRoot(const Address *avoid,unsigned int avoidCount,bool strictAvoid);
  100. /**
  101. * @param id Identity to check
  102. * @return True if this is a designated root server in this world
  103. */
  104. inline bool isRoot(const Identity &id) const
  105. {
  106. Mutex::Lock _l(_lock);
  107. if (std::find(_rootAddresses.begin(),_rootAddresses.end(),id.address()) != _rootAddresses.end()) {
  108. // Double check full identity for security reasons
  109. for(std::vector<World::Root>::const_iterator r(_world.roots().begin());r!=_world.roots().end();++r) {
  110. if (id == r->identity)
  111. return true;
  112. }
  113. }
  114. return false;
  115. }
  116. /**
  117. * @return Vector of root server addresses
  118. */
  119. inline std::vector<Address> rootAddresses() const
  120. {
  121. Mutex::Lock _l(_lock);
  122. return _rootAddresses;
  123. }
  124. /**
  125. * @return Current World (copy)
  126. */
  127. inline World world() const
  128. {
  129. Mutex::Lock _l(_lock);
  130. return _world;
  131. }
  132. /**
  133. * Clean and flush database
  134. */
  135. void clean(uint64_t now);
  136. /**
  137. * Apply a function or function object to all peers
  138. *
  139. * Note: explicitly template this by reference if you want the object
  140. * passed by reference instead of copied.
  141. *
  142. * Warning: be careful not to use features in these that call any other
  143. * methods of Topology that may lock _lock, otherwise a recursive lock
  144. * and deadlock or lock corruption may occur.
  145. *
  146. * @param f Function to apply
  147. * @tparam F Function or function object type
  148. */
  149. template<typename F>
  150. inline void eachPeer(F f)
  151. {
  152. Mutex::Lock _l(_lock);
  153. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
  154. Address *a = (Address *)0;
  155. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  156. while (i.next(a,p))
  157. f(*this,*p);
  158. }
  159. /**
  160. * @return All currently active peers by address
  161. */
  162. inline std::vector< std::pair< Address,SharedPtr<Peer> > > allPeers() const
  163. {
  164. Mutex::Lock _l(_lock);
  165. return _peers.entries();
  166. }
  167. private:
  168. Identity _getIdentity(const Address &zta);
  169. void _saveIdentity(const Identity &id);
  170. const RuntimeEnvironment *RR;
  171. World _world;
  172. Hashtable< Address,SharedPtr<Peer> > _peers;
  173. std::vector< Address > _rootAddresses;
  174. std::vector< SharedPtr<Peer> > _rootPeers;
  175. bool _amRoot;
  176. Mutex _lock;
  177. };
  178. } // namespace ZeroTier
  179. #endif