Topology.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <vector>
  32. #include <stdexcept>
  33. #include <algorithm>
  34. #include <utility>
  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. * Get the identity of a peer
  72. *
  73. * @param zta ZeroTier address of peer
  74. * @return Identity or NULL Identity if not found
  75. */
  76. Identity getIdentity(const Address &zta);
  77. /**
  78. * Cache an identity
  79. *
  80. * This is done automatically on addPeer(), and so is only useful for
  81. * cluster identity replication.
  82. *
  83. * @param id Identity to cache
  84. */
  85. void saveIdentity(const Identity &id);
  86. /**
  87. * @return Vector of peers that are root servers
  88. */
  89. inline std::vector< SharedPtr<Peer> > rootPeers() const
  90. {
  91. Mutex::Lock _l(_lock);
  92. return _rootPeers;
  93. }
  94. /**
  95. * Get the current favorite root server
  96. *
  97. * @return Root server with lowest latency or NULL if none
  98. */
  99. inline SharedPtr<Peer> getBestRoot()
  100. {
  101. return getBestRoot((const Address *)0,0,false);
  102. }
  103. /**
  104. * Get the best root server, avoiding root servers listed in an array
  105. *
  106. * This will get the best root server (lowest latency, etc.) but will
  107. * try to avoid the listed root servers, only using them if no others
  108. * are available.
  109. *
  110. * @param avoid Nodes to avoid
  111. * @param avoidCount Number of nodes to avoid
  112. * @param strictAvoid If false, consider avoided root servers anyway if no non-avoid root servers are available
  113. * @return Root server or NULL if none available
  114. */
  115. SharedPtr<Peer> getBestRoot(const Address *avoid,unsigned int avoidCount,bool strictAvoid);
  116. /**
  117. * @param id Identity to check
  118. * @return True if this is a designated root server in this world
  119. */
  120. inline bool isRoot(const Identity &id) const
  121. {
  122. Mutex::Lock _l(_lock);
  123. return (std::find(_rootAddresses.begin(),_rootAddresses.end(),id.address()) != _rootAddresses.end());
  124. }
  125. /**
  126. * @param id Identity to check
  127. * @return True if this is a root server or a network preferred relay from one of our networks
  128. */
  129. bool isUpstream(const Identity &id) const;
  130. /**
  131. * @return Vector of root server addresses
  132. */
  133. inline std::vector<Address> rootAddresses() const
  134. {
  135. Mutex::Lock _l(_lock);
  136. return _rootAddresses;
  137. }
  138. /**
  139. * @return Current World (copy)
  140. */
  141. inline World world() const
  142. {
  143. Mutex::Lock _l(_lock);
  144. return _world;
  145. }
  146. /**
  147. * @return Current world ID
  148. */
  149. inline uint64_t worldId() const
  150. {
  151. return _world.id(); // safe to read without lock, and used from within eachPeer() so don't lock
  152. }
  153. /**
  154. * @return Current world timestamp
  155. */
  156. inline uint64_t worldTimestamp() const
  157. {
  158. return _world.timestamp(); // safe to read without lock, and used from within eachPeer() so don't lock
  159. }
  160. /**
  161. * Validate new world and update if newer and signature is okay
  162. *
  163. * @param newWorld Potential new world definition revision
  164. * @return True if an update actually occurred
  165. */
  166. bool worldUpdateIfValid(const World &newWorld);
  167. /**
  168. * Clean and flush database
  169. */
  170. void clean(uint64_t now);
  171. /**
  172. * @return Number of 'alive' peers
  173. */
  174. unsigned long countAlive() const;
  175. /**
  176. * Apply a function or function object to all peers
  177. *
  178. * Note: explicitly template this by reference if you want the object
  179. * passed by reference instead of copied.
  180. *
  181. * Warning: be careful not to use features in these that call any other
  182. * methods of Topology that may lock _lock, otherwise a recursive lock
  183. * and deadlock or lock corruption may occur.
  184. *
  185. * @param f Function to apply
  186. * @tparam F Function or function object type
  187. */
  188. template<typename F>
  189. inline void eachPeer(F f)
  190. {
  191. Mutex::Lock _l(_lock);
  192. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
  193. Address *a = (Address *)0;
  194. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  195. while (i.next(a,p))
  196. f(*this,*p);
  197. }
  198. /**
  199. * @return All currently active peers by address (unsorted)
  200. */
  201. inline std::vector< std::pair< Address,SharedPtr<Peer> > > allPeers() const
  202. {
  203. Mutex::Lock _l(_lock);
  204. return _peers.entries();
  205. }
  206. /**
  207. * @return True if I am a root server in the current World
  208. */
  209. inline bool amRoot() const throw() { return _amRoot; }
  210. private:
  211. Identity _getIdentity(const Address &zta);
  212. void _setWorld(const World &newWorld);
  213. const RuntimeEnvironment *RR;
  214. World _world;
  215. Hashtable< Address,SharedPtr<Peer> > _peers;
  216. std::vector< Address > _rootAddresses;
  217. std::vector< SharedPtr<Peer> > _rootPeers;
  218. bool _amRoot;
  219. Mutex _lock;
  220. };
  221. } // namespace ZeroTier
  222. #endif