Topology.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  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 <set>
  33. #include <vector>
  34. #include <stdexcept>
  35. #include "Constants.hpp"
  36. #include "Address.hpp"
  37. #include "Peer.hpp"
  38. #include "Mutex.hpp"
  39. #include "InetAddress.hpp"
  40. #include "Utils.hpp"
  41. #include "../ext/kissdb/kissdb.h"
  42. namespace ZeroTier {
  43. class RuntimeEnvironment;
  44. /**
  45. * Database of network topology
  46. */
  47. class Topology
  48. {
  49. public:
  50. Topology(const RuntimeEnvironment *renv,const char *dbpath)
  51. throw(std::runtime_error);
  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<InetAddress> > &sn);
  59. /**
  60. * Add a peer to database
  61. *
  62. * This will not replace existing peers. In that case the existing peer
  63. * record is returned.
  64. *
  65. * @param peer Peer to add
  66. * @return New or existing peer (should replace 'peer')
  67. */
  68. SharedPtr<Peer> addPeer(const SharedPtr<Peer> &peer);
  69. /**
  70. * Get a peer from its address
  71. *
  72. * @param zta ZeroTier address of peer
  73. * @return Peer or NULL if not found
  74. */
  75. SharedPtr<Peer> getPeer(const Address &zta);
  76. /**
  77. * @return Current network supernodes
  78. */
  79. inline std::map< Identity,std::vector<InetAddress> > supernodes() const
  80. {
  81. Mutex::Lock _l(_supernodes_m);
  82. return _supernodes;
  83. }
  84. /**
  85. * @return Vector of peers that are supernodes
  86. */
  87. inline std::vector< SharedPtr<Peer> > supernodePeers() const
  88. {
  89. Mutex::Lock _l(_supernodes_m);
  90. return _supernodePeers;
  91. }
  92. /**
  93. * Get the current favorite supernode
  94. *
  95. * @return Supernode with lowest latency or NULL if none
  96. */
  97. inline SharedPtr<Peer> getBestSupernode() const
  98. {
  99. return getBestSupernode((const Address *)0,0,false);
  100. }
  101. /**
  102. * Get the best supernode, avoiding supernodes listed in an array
  103. *
  104. * This will get the best supernode (lowest latency, etc.) but will
  105. * try to avoid the listed supernodes, only using them if no others
  106. * are available.
  107. *
  108. * @param avoid Nodes to avoid
  109. * @param avoidCount Number of nodes to avoid
  110. * @param strictAvoid If false, consider avoided supernodes anyway if no non-avoid supernodes are available
  111. * @return Supernode or NULL if none
  112. */
  113. SharedPtr<Peer> getBestSupernode(const Address *avoid,unsigned int avoidCount,bool strictAvoid) const;
  114. /**
  115. * @param zta ZeroTier address
  116. * @return True if this is a designated supernode
  117. */
  118. inline bool isSupernode(const Address &zta) const
  119. throw()
  120. {
  121. Mutex::Lock _l(_supernodes_m);
  122. return (_supernodeAddresses.count(zta) > 0);
  123. }
  124. /**
  125. * @return True if this node's identity is in the supernode set
  126. */
  127. inline bool amSupernode() const { return _amSupernode; }
  128. /**
  129. * Clean and flush database
  130. */
  131. void clean();
  132. /**
  133. * Apply a function or function object to all peers
  134. *
  135. * @param f Function to apply
  136. * @tparam F Function or function object type
  137. */
  138. template<typename F>
  139. inline void eachPeer(F f)
  140. {
  141. Mutex::Lock _l(_activePeers_m);
  142. for(std::map< Address,SharedPtr<Peer> >::const_iterator p(_activePeers.begin());p!=_activePeers.end();++p)
  143. f(*this,p->second);
  144. }
  145. /**
  146. * Function object to collect peers that need a firewall opener sent
  147. */
  148. class OpenPeersThatNeedFirewallOpener
  149. {
  150. public:
  151. OpenPeersThatNeedFirewallOpener(const RuntimeEnvironment *renv,uint64_t now) throw() :
  152. _now(now),
  153. _r(renv)
  154. {
  155. }
  156. inline void operator()(Topology &t,const SharedPtr<Peer> &p)
  157. {
  158. if ((p->hasDirectPath())&&((_now - std::max(p->lastFirewallOpener(),p->lastDirectSend())) >= ZT_FIREWALL_OPENER_DELAY))
  159. p->sendFirewallOpener(_r,_now);
  160. }
  161. private:
  162. uint64_t _now;
  163. const RuntimeEnvironment *_r;
  164. };
  165. /**
  166. * Function object to collect peers that need a ping sent
  167. */
  168. class PingPeersThatNeedPing
  169. {
  170. public:
  171. PingPeersThatNeedPing(const RuntimeEnvironment *renv,uint64_t now) throw() :
  172. _now(now),
  173. _r(renv)
  174. {
  175. }
  176. inline void operator()(Topology &t,const SharedPtr<Peer> &p)
  177. {
  178. if (
  179. ((_now - p->lastDirectReceive()) >= ZT_PEER_DIRECT_PING_DELAY) &&
  180. (
  181. (
  182. (p->hasDirectPath())&&
  183. ((_now - p->lastFrame()) < ZT_PEER_LINK_ACTIVITY_TIMEOUT)
  184. ) ||
  185. (t.isSupernode(p->address()))
  186. )
  187. ) {
  188. p->sendPing(_r,_now);
  189. }
  190. }
  191. private:
  192. uint64_t _now;
  193. const RuntimeEnvironment *_r;
  194. };
  195. /**
  196. * Function object to collect peers that we're talking to
  197. */
  198. class PingAllActivePeers
  199. {
  200. public:
  201. PingAllActivePeers(const RuntimeEnvironment *renv,uint64_t now) throw() :
  202. _now(now),
  203. _r(renv)
  204. {
  205. }
  206. inline void operator()(Topology &t,const SharedPtr<Peer> &p)
  207. {
  208. if (
  209. (
  210. (p->hasDirectPath())&&
  211. ((_now - p->lastFrame()) < ZT_PEER_LINK_ACTIVITY_TIMEOUT)
  212. ) ||
  213. (t.isSupernode(p->address()))
  214. ) {
  215. p->sendPing(_r,_now);
  216. }
  217. }
  218. private:
  219. uint64_t _now;
  220. const RuntimeEnvironment *_r;
  221. };
  222. /**
  223. * Function object to collect peers with any known direct path
  224. */
  225. class CollectPeersWithActiveDirectPath
  226. {
  227. public:
  228. CollectPeersWithActiveDirectPath(std::vector< SharedPtr<Peer> > &v,uint64_t now) throw() :
  229. _now(now),
  230. _v(v)
  231. {
  232. }
  233. inline void operator()(Topology &t,const SharedPtr<Peer> &p)
  234. {
  235. if (p->hasActiveDirectPath(_now))
  236. _v.push_back(p);
  237. }
  238. private:
  239. uint64_t _now;
  240. std::vector< SharedPtr<Peer> > &_v;
  241. };
  242. private:
  243. const RuntimeEnvironment *const _r;
  244. std::map< Address,SharedPtr<Peer> > _activePeers;
  245. Mutex _activePeers_m;
  246. std::map< Identity,std::vector<InetAddress> > _supernodes;
  247. std::set< Address > _supernodeAddresses;
  248. std::vector< SharedPtr<Peer> > _supernodePeers;
  249. Mutex _supernodes_m;
  250. // Set to true if my identity is in _supernodes
  251. volatile bool _amSupernode;
  252. KISSDB _dbm;
  253. Mutex _dbm_m;
  254. };
  255. } // namespace ZeroTier
  256. #endif