Topology.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "Utils.hpp"
  42. #include "Dictionary.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. * Set up supernodes for this network
  55. *
  56. * @param sn Supernodes for this network
  57. */
  58. void setSupernodes(const std::map< Identity,std::vector< std::pair<InetAddress,bool> > > &sn);
  59. /**
  60. * Set up supernodes for this network
  61. *
  62. * This performs no signature verification of any kind. The caller must
  63. * check the signature of the root topology dictionary first.
  64. *
  65. * @param sn Supernodes dictionary from root-topology
  66. */
  67. void setSupernodes(const Dictionary &sn);
  68. /**
  69. * Add a peer to database
  70. *
  71. * This will not replace existing peers. In that case the existing peer
  72. * record is returned.
  73. *
  74. * @param peer Peer to add
  75. * @return New or existing peer (should replace 'peer')
  76. */
  77. SharedPtr<Peer> addPeer(const SharedPtr<Peer> &peer);
  78. /**
  79. * Get a peer from its address
  80. *
  81. * @param zta ZeroTier address of peer
  82. * @return Peer or NULL if not found
  83. */
  84. SharedPtr<Peer> getPeer(const Address &zta);
  85. /**
  86. * @return Vector of peers that are supernodes
  87. */
  88. inline std::vector< SharedPtr<Peer> > supernodePeers() const
  89. {
  90. Mutex::Lock _l(_lock);
  91. return _supernodePeers;
  92. }
  93. /**
  94. * @return Number of supernodes
  95. */
  96. inline unsigned int numSupernodes() const
  97. {
  98. Mutex::Lock _l(_lock);
  99. return (unsigned int)_supernodePeers.size();
  100. }
  101. /**
  102. * Get the current favorite supernode
  103. *
  104. * @return Supernode with lowest latency or NULL if none
  105. */
  106. inline SharedPtr<Peer> getBestSupernode()
  107. {
  108. return getBestSupernode((const Address *)0,0,false);
  109. }
  110. /**
  111. * Get the best supernode, avoiding supernodes listed in an array
  112. *
  113. * This will get the best supernode (lowest latency, etc.) but will
  114. * try to avoid the listed supernodes, 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 supernodes anyway if no non-avoid supernodes are available
  120. * @return Supernode or NULL if none
  121. */
  122. SharedPtr<Peer> getBestSupernode(const Address *avoid,unsigned int avoidCount,bool strictAvoid);
  123. /**
  124. * @param zta ZeroTier address
  125. * @return True if this is a designated supernode
  126. */
  127. inline bool isSupernode(const Address &zta) const
  128. throw()
  129. {
  130. Mutex::Lock _l(_lock);
  131. return (std::find(_supernodeAddresses.begin(),_supernodeAddresses.end(),zta) != _supernodeAddresses.end());
  132. }
  133. /**
  134. * @return Vector of supernode addresses
  135. */
  136. inline std::vector<Address> supernodeAddresses() const
  137. {
  138. Mutex::Lock _l(_lock);
  139. return _supernodeAddresses;
  140. }
  141. /**
  142. * @return True if this node's identity is in the supernode set
  143. */
  144. inline bool amSupernode() const { return _amSupernode; }
  145. /**
  146. * Clean and flush database
  147. */
  148. void clean(uint64_t now);
  149. /**
  150. * Apply a function or function object to all peers
  151. *
  152. * Note: explicitly template this by reference if you want the object
  153. * passed by reference instead of copied.
  154. *
  155. * Warning: be careful not to use features in these that call any other
  156. * methods of Topology that may lock _lock, otherwise a recursive lock
  157. * and deadlock or lock corruption may occur.
  158. *
  159. * @param f Function to apply
  160. * @tparam F Function or function object type
  161. */
  162. template<typename F>
  163. inline void eachPeer(F f)
  164. {
  165. Mutex::Lock _l(_lock);
  166. for(std::map< Address,SharedPtr<Peer> >::const_iterator p(_activePeers.begin());p!=_activePeers.end();++p)
  167. f(*this,p->second);
  168. }
  169. /**
  170. * Validate a root topology dictionary against the identities specified in Defaults
  171. *
  172. * @param rt Root topology dictionary
  173. * @return True if dictionary signature is valid
  174. */
  175. static bool authenticateRootTopology(const Dictionary &rt);
  176. private:
  177. Identity _getIdentity(const Address &zta);
  178. void _saveIdentity(const Identity &id);
  179. const RuntimeEnvironment *RR;
  180. std::string _idCacheBase;
  181. std::map< Address,SharedPtr<Peer> > _activePeers;
  182. std::map< Identity,std::vector< std::pair<InetAddress,bool> > > _supernodes;
  183. std::vector< Address > _supernodeAddresses;
  184. std::vector< SharedPtr<Peer> > _supernodePeers;
  185. Mutex _lock;
  186. // Set to true if my identity is in _supernodes
  187. bool _amSupernode;
  188. };
  189. } // namespace ZeroTier
  190. #endif