Topology.hpp 5.7 KB

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