Topology.hpp 6.9 KB

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