Topology.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "Dictionary.hpp"
  42. #include "Hashtable.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. * @param sn Root server identities and addresses
  55. */
  56. void setRootServers(const std::map< Identity,std::vector<InetAddress> > &sn);
  57. /**
  58. * Set up root servers for this network
  59. *
  60. * This performs no signature verification of any kind. The caller must
  61. * check the signature of the root topology dictionary first.
  62. *
  63. * @param sn 'rootservers' key from root-topology Dictionary (deserialized as Dictionary)
  64. */
  65. void setRootServers(const Dictionary &sn);
  66. /**
  67. * Add a peer to database
  68. *
  69. * This will not replace existing peers. In that case the existing peer
  70. * record is returned.
  71. *
  72. * @param peer Peer to add
  73. * @return New or existing peer (should replace 'peer')
  74. */
  75. SharedPtr<Peer> addPeer(const SharedPtr<Peer> &peer);
  76. /**
  77. * Get a peer from its address
  78. *
  79. * @param zta ZeroTier address of peer
  80. * @return Peer or NULL if not found
  81. */
  82. SharedPtr<Peer> getPeer(const Address &zta);
  83. /**
  84. * @return Vector of peers that are root servers
  85. */
  86. inline std::vector< SharedPtr<Peer> > rootPeers() const
  87. {
  88. Mutex::Lock _l(_lock);
  89. return _rootPeers;
  90. }
  91. /**
  92. * Get the current favorite root server
  93. *
  94. * @return Root server with lowest latency or NULL if none
  95. */
  96. inline SharedPtr<Peer> getBestRoot()
  97. {
  98. return getBestRoot((const Address *)0,0,false);
  99. }
  100. /**
  101. * Get the best root server, avoiding root servers listed in an array
  102. *
  103. * This will get the best root server (lowest latency, etc.) but will
  104. * try to avoid the listed root servers, only using them if no others
  105. * are available.
  106. *
  107. * @param avoid Nodes to avoid
  108. * @param avoidCount Number of nodes to avoid
  109. * @param strictAvoid If false, consider avoided root servers anyway if no non-avoid root servers are available
  110. * @return Root server or NULL if none available
  111. */
  112. SharedPtr<Peer> getBestRoot(const Address *avoid,unsigned int avoidCount,bool strictAvoid);
  113. /**
  114. * @param id Identity to check
  115. * @return True if this is a designated root server
  116. */
  117. bool isRoot(const Identity &id) const
  118. throw();
  119. /**
  120. * @return Vector of root server addresses
  121. */
  122. inline std::vector<Address> rootAddresses() const
  123. {
  124. Mutex::Lock _l(_lock);
  125. return _rootAddresses;
  126. }
  127. /**
  128. * Clean and flush database
  129. */
  130. void clean(uint64_t now);
  131. /**
  132. * Apply a function or function object to all peers
  133. *
  134. * Note: explicitly template this by reference if you want the object
  135. * passed by reference instead of copied.
  136. *
  137. * Warning: be careful not to use features in these that call any other
  138. * methods of Topology that may lock _lock, otherwise a recursive lock
  139. * and deadlock or lock corruption may occur.
  140. *
  141. * @param f Function to apply
  142. * @tparam F Function or function object type
  143. */
  144. template<typename F>
  145. inline void eachPeer(F f)
  146. {
  147. Mutex::Lock _l(_lock);
  148. Hashtable< Address,SharedPtr<Peer> >::Iterator i(_activePeers);
  149. Address *a = (Address *)0;
  150. SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
  151. while (i.next(a,p))
  152. f(*this,*p);
  153. }
  154. /**
  155. * @return All currently active peers by address
  156. */
  157. inline std::vector< std::pair< Address,SharedPtr<Peer> > > allPeers() const
  158. {
  159. Mutex::Lock _l(_lock);
  160. return _activePeers.entries();
  161. }
  162. /**
  163. * Validate a root topology dictionary against the identities specified in Defaults
  164. *
  165. * @param rt Root topology dictionary
  166. * @return True if dictionary signature is valid
  167. */
  168. static bool authenticateRootTopology(const Dictionary &rt);
  169. private:
  170. Identity _getIdentity(const Address &zta);
  171. void _saveIdentity(const Identity &id);
  172. const RuntimeEnvironment *RR;
  173. Hashtable< Address,SharedPtr<Peer> > _activePeers;
  174. std::map< Identity,std::vector<InetAddress> > _roots;
  175. std::vector< Address > _rootAddresses;
  176. std::vector< SharedPtr<Peer> > _rootPeers;
  177. Mutex _lock;
  178. bool _amRoot;
  179. };
  180. } // namespace ZeroTier
  181. #endif