Topology.hpp 5.2 KB

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