Topology.hpp 6.7 KB

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