Peer.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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_PEER_HPP
  28. #define ZT_PEER_HPP
  29. #include <stdint.h>
  30. #include "Constants.hpp"
  31. #include <algorithm>
  32. #include <utility>
  33. #include <vector>
  34. #include <stdexcept>
  35. #include "../include/ZeroTierOne.h"
  36. #include "RuntimeEnvironment.hpp"
  37. #include "CertificateOfMembership.hpp"
  38. #include "Path.hpp"
  39. #include "Address.hpp"
  40. #include "Utils.hpp"
  41. #include "Identity.hpp"
  42. #include "InetAddress.hpp"
  43. #include "Packet.hpp"
  44. #include "SharedPtr.hpp"
  45. #include "AtomicCounter.hpp"
  46. #include "Hashtable.hpp"
  47. #include "Mutex.hpp"
  48. #include "NonCopyable.hpp"
  49. // Very rough computed estimate: (8 + 256 + 80 + (16 * 64) + (128 * 256) + (128 * 16))
  50. // 1048576 provides tons of headroom -- overflow would just cause peer not to be persisted
  51. #define ZT_PEER_SUGGESTED_SERIALIZATION_BUFFER_SIZE 1048576
  52. namespace ZeroTier {
  53. /**
  54. * Peer on P2P Network (virtual layer 1)
  55. */
  56. class Peer : NonCopyable
  57. {
  58. friend class SharedPtr<Peer>;
  59. private:
  60. Peer() {} // disabled to prevent bugs -- should not be constructed uninitialized
  61. public:
  62. ~Peer() { Utils::burn(_key,sizeof(_key)); }
  63. /**
  64. * Construct a new peer
  65. *
  66. * @param renv Runtime environment
  67. * @param myIdentity Identity of THIS node (for key agreement)
  68. * @param peerIdentity Identity of peer
  69. * @throws std::runtime_error Key agreement with peer's identity failed
  70. */
  71. Peer(const RuntimeEnvironment *renv,const Identity &myIdentity,const Identity &peerIdentity);
  72. /**
  73. * @return Time peer record was last used in any way
  74. */
  75. inline uint64_t lastUsed() const throw() { return _lastUsed; }
  76. /**
  77. * Log a use of this peer record (done by Topology when peers are looked up)
  78. *
  79. * @param now New time of last use
  80. */
  81. inline void use(uint64_t now) throw() { _lastUsed = now; }
  82. /**
  83. * @return This peer's ZT address (short for identity().address())
  84. */
  85. inline const Address &address() const throw() { return _id.address(); }
  86. /**
  87. * @return This peer's identity
  88. */
  89. inline const Identity &identity() const throw() { return _id; }
  90. /**
  91. * Log receipt of an authenticated packet
  92. *
  93. * This is called by the decode pipe when a packet is proven to be authentic
  94. * and appears to be valid.
  95. *
  96. * @param RR Runtime environment
  97. * @param localAddr Local address
  98. * @param remoteAddr Internet address of sender
  99. * @param hops ZeroTier (not IP) hops
  100. * @param packetId Packet ID
  101. * @param verb Packet verb
  102. * @param inRePacketId Packet ID in reply to (default: none)
  103. * @param inReVerb Verb in reply to (for OK/ERROR, default: VERB_NOP)
  104. */
  105. void received(
  106. const InetAddress &localAddr,
  107. const InetAddress &remoteAddr,
  108. unsigned int hops,
  109. uint64_t packetId,
  110. Packet::Verb verb,
  111. uint64_t inRePacketId = 0,
  112. Packet::Verb inReVerb = Packet::VERB_NOP);
  113. /**
  114. * Get the current best direct path to this peer
  115. *
  116. * @param now Current time
  117. * @return Best path or NULL if there are no active direct paths
  118. */
  119. inline Path *getBestPath(uint64_t now)
  120. {
  121. Mutex::Lock _l(_lock);
  122. return _getBestPath(now);
  123. }
  124. /**
  125. * Send via best path
  126. *
  127. * @param data Packet data
  128. * @param len Packet length
  129. * @param now Current time
  130. * @return Path used on success or NULL on failure
  131. */
  132. inline Path *send(const void *data,unsigned int len,uint64_t now)
  133. {
  134. Path *const bestPath = getBestPath(now);
  135. if (bestPath) {
  136. if (bestPath->send(RR,data,len,now))
  137. return bestPath;
  138. }
  139. return (Path *)0;
  140. }
  141. /**
  142. * Send a HELLO to this peer at a specified physical address
  143. *
  144. * This does not update any statistics. It's used to send initial HELLOs
  145. * for NAT traversal and path verification.
  146. *
  147. * @param localAddr Local address
  148. * @param atAddress Destination address
  149. * @param now Current time
  150. * @param ttl Desired IP TTL (default: 0 to leave alone)
  151. */
  152. void sendHELLO(const InetAddress &localAddr,const InetAddress &atAddress,uint64_t now,unsigned int ttl = 0);
  153. /**
  154. * Send pings or keepalives depending on configured timeouts
  155. *
  156. * @param now Current time
  157. * @param inetAddressFamily Keep this address family alive, or 0 to simply pick current best ignoring family
  158. * @return True if at least one direct path seems alive
  159. */
  160. bool doPingAndKeepalive(uint64_t now,int inetAddressFamily);
  161. /**
  162. * Push direct paths back to self if we haven't done so in the configured timeout
  163. *
  164. * @param path Remote path to use to send the push
  165. * @param now Current time
  166. * @param force If true, push regardless of rate limit
  167. */
  168. void pushDirectPaths(Path *path,uint64_t now,bool force);
  169. /**
  170. * @return All known direct paths to this peer
  171. */
  172. inline std::vector<Path> paths() const
  173. {
  174. std::vector<Path> pp;
  175. Mutex::Lock _l(_lock);
  176. for(unsigned int p=0,np=_numPaths;p<np;++p)
  177. pp.push_back(_paths[p]);
  178. return pp;
  179. }
  180. /**
  181. * @return Time of last receive of anything, whether direct or relayed
  182. */
  183. inline uint64_t lastReceive() const throw() { return _lastReceive; }
  184. /**
  185. * @return Time of most recent unicast frame received
  186. */
  187. inline uint64_t lastUnicastFrame() const throw() { return _lastUnicastFrame; }
  188. /**
  189. * @return Time of most recent multicast frame received
  190. */
  191. inline uint64_t lastMulticastFrame() const throw() { return _lastMulticastFrame; }
  192. /**
  193. * @return Time of most recent frame of any kind (unicast or multicast)
  194. */
  195. inline uint64_t lastFrame() const throw() { return std::max(_lastUnicastFrame,_lastMulticastFrame); }
  196. /**
  197. * @return Time we last announced state TO this peer, such as multicast LIKEs
  198. */
  199. inline uint64_t lastAnnouncedTo() const throw() { return _lastAnnouncedTo; }
  200. /**
  201. * @return True if this peer has sent us real network traffic recently
  202. */
  203. inline uint64_t activelyTransferringFrames(uint64_t now) const throw() { return ((now - lastFrame()) < ZT_PEER_ACTIVITY_TIMEOUT); }
  204. /**
  205. * @return Latency in milliseconds or 0 if unknown
  206. */
  207. inline unsigned int latency() const { return _latency; }
  208. /**
  209. * This computes a quality score for relays and root servers
  210. *
  211. * If we haven't heard anything from these in ZT_PEER_ACTIVITY_TIMEOUT, they
  212. * receive the worst possible quality (max unsigned int). Otherwise the
  213. * quality is a product of latency and the number of potential missed
  214. * pings. This causes roots and relays to switch over a bit faster if they
  215. * fail.
  216. *
  217. * @return Relay quality score computed from latency and other factors, lower is better
  218. */
  219. inline unsigned int relayQuality(const uint64_t now) const
  220. {
  221. const uint64_t tsr = now - _lastReceive;
  222. if (tsr >= ZT_PEER_ACTIVITY_TIMEOUT)
  223. return (~(unsigned int)0);
  224. unsigned int l = _latency;
  225. if (!l)
  226. l = 0xffff;
  227. return (l * (((unsigned int)tsr / (ZT_PEER_DIRECT_PING_DELAY + 1000)) + 1));
  228. }
  229. /**
  230. * Update latency with a new direct measurment
  231. *
  232. * @param l Direct latency measurment in ms
  233. */
  234. inline void addDirectLatencyMeasurment(unsigned int l)
  235. {
  236. unsigned int ol = _latency;
  237. if ((ol > 0)&&(ol < 10000))
  238. _latency = (ol + std::min(l,(unsigned int)65535)) / 2;
  239. else _latency = std::min(l,(unsigned int)65535);
  240. }
  241. /**
  242. * @param now Current time
  243. * @return True if this peer has at least one active direct path
  244. */
  245. inline bool hasActiveDirectPath(uint64_t now) const
  246. {
  247. Mutex::Lock _l(_lock);
  248. for(unsigned int p=0;p<_numPaths;++p) {
  249. if (_paths[p].active(now))
  250. return true;
  251. }
  252. return false;
  253. }
  254. #ifdef ZT_ENABLE_CLUSTER
  255. /**
  256. * @param now Current time
  257. * @return True if this peer has at least one active direct path that is not cluster-suboptimal
  258. */
  259. inline bool hasClusterOptimalPath(uint64_t now) const
  260. {
  261. Mutex::Lock _l(_lock);
  262. for(unsigned int p=0,np=_numPaths;p<np;++p) {
  263. if ((_paths[p].active(now))&&(!_paths[p].isClusterSuboptimal()))
  264. return true;
  265. }
  266. return false;
  267. }
  268. #endif
  269. /**
  270. * @param now Current time
  271. * @param addr Remote address
  272. * @return True if peer currently has an active direct path to addr
  273. */
  274. inline bool hasActivePathTo(uint64_t now,const InetAddress &addr) const
  275. {
  276. Mutex::Lock _l(_lock);
  277. for(unsigned int p=0;p<_numPaths;++p) {
  278. if ((_paths[p].active(now))&&(_paths[p].address() == addr))
  279. return true;
  280. }
  281. return false;
  282. }
  283. /**
  284. * Reset paths within a given scope
  285. *
  286. * @param scope IP scope of paths to reset
  287. * @param now Current time
  288. * @return True if at least one path was forgotten
  289. */
  290. bool resetWithinScope(InetAddress::IpScope scope,uint64_t now);
  291. /**
  292. * @return 256-bit secret symmetric encryption key
  293. */
  294. inline const unsigned char *key() const throw() { return _key; }
  295. /**
  296. * Set the currently known remote version of this peer's client
  297. *
  298. * @param vproto Protocol version
  299. * @param vmaj Major version
  300. * @param vmin Minor version
  301. * @param vrev Revision
  302. */
  303. inline void setRemoteVersion(unsigned int vproto,unsigned int vmaj,unsigned int vmin,unsigned int vrev)
  304. {
  305. _vProto = (uint16_t)vproto;
  306. _vMajor = (uint16_t)vmaj;
  307. _vMinor = (uint16_t)vmin;
  308. _vRevision = (uint16_t)vrev;
  309. }
  310. inline unsigned int remoteVersionProtocol() const throw() { return _vProto; }
  311. inline unsigned int remoteVersionMajor() const throw() { return _vMajor; }
  312. inline unsigned int remoteVersionMinor() const throw() { return _vMinor; }
  313. inline unsigned int remoteVersionRevision() const throw() { return _vRevision; }
  314. inline bool remoteVersionKnown() const throw() { return ((_vMajor > 0)||(_vMinor > 0)||(_vRevision > 0)); }
  315. /**
  316. * Get most recently active path addresses for IPv4 and/or IPv6
  317. *
  318. * Note that v4 and v6 are not modified if they are not found, so
  319. * initialize these to a NULL address to be able to check.
  320. *
  321. * @param now Current time
  322. * @param v4 Result parameter to receive active IPv4 address, if any
  323. * @param v6 Result parameter to receive active IPv6 address, if any
  324. */
  325. void getBestActiveAddresses(uint64_t now,InetAddress &v4,InetAddress &v6) const;
  326. /**
  327. * Check network COM agreement with this peer
  328. *
  329. * @param nwid Network ID
  330. * @param com Another certificate of membership
  331. * @return True if supplied COM agrees with ours, false if not or if we don't have one
  332. */
  333. bool networkMembershipCertificatesAgree(uint64_t nwid,const CertificateOfMembership &com) const;
  334. /**
  335. * Check the validity of the COM and add/update if valid and new
  336. *
  337. * @param nwid Network ID
  338. * @param com Externally supplied COM
  339. */
  340. bool validateAndSetNetworkMembershipCertificate(uint64_t nwid,const CertificateOfMembership &com);
  341. /**
  342. * @param nwid Network ID
  343. * @param now Current time
  344. * @param updateLastPushedTime If true, go ahead and update the last pushed time regardless of return value
  345. * @return Whether or not this peer needs another COM push from us
  346. */
  347. bool needsOurNetworkMembershipCertificate(uint64_t nwid,uint64_t now,bool updateLastPushedTime);
  348. /**
  349. * Perform periodic cleaning operations
  350. *
  351. * @param now Current time
  352. */
  353. void clean(uint64_t now);
  354. /**
  355. * Update direct path push stats and return true if we should respond
  356. *
  357. * This is a circuit breaker to make VERB_PUSH_DIRECT_PATHS not particularly
  358. * useful as a DDOS amplification attack vector. Otherwise a malicious peer
  359. * could send loads of these and cause others to bombard arbitrary IPs with
  360. * traffic.
  361. *
  362. * @param now Current time
  363. * @return True if we should respond
  364. */
  365. inline bool shouldRespondToDirectPathPush(const uint64_t now)
  366. {
  367. Mutex::Lock _l(_lock);
  368. if ((now - _lastDirectPathPushReceive) <= ZT_PUSH_DIRECT_PATHS_CUTOFF_TIME)
  369. ++_directPathPushCutoffCount;
  370. else _directPathPushCutoffCount = 0;
  371. _lastDirectPathPushReceive = now;
  372. return (_directPathPushCutoffCount < ZT_PUSH_DIRECT_PATHS_CUTOFF_LIMIT);
  373. }
  374. /**
  375. * Find a common set of addresses by which two peers can link, if any
  376. *
  377. * @param a Peer A
  378. * @param b Peer B
  379. * @param now Current time
  380. * @return Pair: B's address (to send to A), A's address (to send to B)
  381. */
  382. static inline std::pair<InetAddress,InetAddress> findCommonGround(const Peer &a,const Peer &b,uint64_t now)
  383. {
  384. std::pair<InetAddress,InetAddress> v4,v6;
  385. b.getBestActiveAddresses(now,v4.first,v6.first);
  386. a.getBestActiveAddresses(now,v4.second,v6.second);
  387. if ((v6.first)&&(v6.second)) // prefer IPv6 if both have it since NAT-t is (almost) unnecessary
  388. return v6;
  389. else if ((v4.first)&&(v4.second))
  390. return v4;
  391. else return std::pair<InetAddress,InetAddress>();
  392. }
  393. template<unsigned int C>
  394. inline void serialize(Buffer<C> &b) const
  395. {
  396. Mutex::Lock _l(_lock);
  397. const unsigned int recSizePos = b.size();
  398. b.addSize(4); // space for uint32_t field length
  399. b.append((uint16_t)1); // version of serialized Peer data
  400. _id.serialize(b,false);
  401. b.append((uint64_t)_lastUsed);
  402. b.append((uint64_t)_lastReceive);
  403. b.append((uint64_t)_lastUnicastFrame);
  404. b.append((uint64_t)_lastMulticastFrame);
  405. b.append((uint64_t)_lastAnnouncedTo);
  406. b.append((uint64_t)_lastDirectPathPushSent);
  407. b.append((uint64_t)_lastDirectPathPushReceive);
  408. b.append((uint64_t)_lastPathSort);
  409. b.append((uint16_t)_vProto);
  410. b.append((uint16_t)_vMajor);
  411. b.append((uint16_t)_vMinor);
  412. b.append((uint16_t)_vRevision);
  413. b.append((uint32_t)_latency);
  414. b.append((uint16_t)_directPathPushCutoffCount);
  415. b.append((uint16_t)_numPaths);
  416. for(unsigned int i=0;i<_numPaths;++i)
  417. _paths[i].serialize(b);
  418. b.append((uint32_t)_networkComs.size());
  419. {
  420. uint64_t *k = (uint64_t *)0;
  421. _NetworkCom *v = (_NetworkCom *)0;
  422. Hashtable<uint64_t,_NetworkCom>::Iterator i(const_cast<Peer *>(this)->_networkComs);
  423. while (i.next(k,v)) {
  424. b.append((uint64_t)*k);
  425. b.append((uint64_t)v->ts);
  426. v->com.serialize(b);
  427. }
  428. }
  429. b.append((uint32_t)_lastPushedComs.size());
  430. {
  431. uint64_t *k = (uint64_t *)0;
  432. uint64_t *v = (uint64_t *)0;
  433. Hashtable<uint64_t,uint64_t>::Iterator i(const_cast<Peer *>(this)->_lastPushedComs);
  434. while (i.next(k,v)) {
  435. b.append((uint64_t)*k);
  436. b.append((uint64_t)*v);
  437. }
  438. }
  439. b.template setAt<uint32_t>(recSizePos,(uint32_t)(b.size() - (recSizePos + 4))); // set size
  440. }
  441. /**
  442. * Create a new Peer from a serialized instance
  443. *
  444. * @param renv Runtime environment
  445. * @param myIdentity This node's identity
  446. * @param b Buffer containing serialized Peer data
  447. * @param p Pointer to current position in buffer, will be updated in place as buffer is read (value/result)
  448. * @return New instance of Peer or NULL if serialized data was corrupt or otherwise invalid (may also throw an exception via Buffer)
  449. */
  450. template<unsigned int C>
  451. static inline SharedPtr<Peer> deserializeNew(const RuntimeEnvironment *renv,const Identity &myIdentity,const Buffer<C> &b,unsigned int &p)
  452. {
  453. const unsigned int recSize = b.template at<uint32_t>(p); p += 4;
  454. if ((p + recSize) > b.size())
  455. return SharedPtr<Peer>(); // size invalid
  456. if (b.template at<uint16_t>(p) != 1)
  457. return SharedPtr<Peer>(); // version mismatch
  458. p += 2;
  459. Identity npid;
  460. p += npid.deserialize(b,p);
  461. if (!npid)
  462. return SharedPtr<Peer>();
  463. SharedPtr<Peer> np(new Peer(renv,myIdentity,npid));
  464. np->_lastUsed = b.template at<uint64_t>(p); p += 8;
  465. np->_lastReceive = b.template at<uint64_t>(p); p += 8;
  466. np->_lastUnicastFrame = b.template at<uint64_t>(p); p += 8;
  467. np->_lastMulticastFrame = b.template at<uint64_t>(p); p += 8;
  468. np->_lastAnnouncedTo = b.template at<uint64_t>(p); p += 8;
  469. np->_lastDirectPathPushSent = b.template at<uint64_t>(p); p += 8;
  470. np->_lastDirectPathPushReceive = b.template at<uint64_t>(p); p += 8;
  471. np->_lastPathSort = b.template at<uint64_t>(p); p += 8;
  472. np->_vProto = b.template at<uint16_t>(p); p += 2;
  473. np->_vMajor = b.template at<uint16_t>(p); p += 2;
  474. np->_vMinor = b.template at<uint16_t>(p); p += 2;
  475. np->_vRevision = b.template at<uint16_t>(p); p += 2;
  476. np->_latency = b.template at<uint32_t>(p); p += 4;
  477. np->_directPathPushCutoffCount = b.template at<uint16_t>(p); p += 2;
  478. const unsigned int numPaths = b.template at<uint16_t>(p); p += 2;
  479. for(unsigned int i=0;i<numPaths;++i) {
  480. if (i < ZT_MAX_PEER_NETWORK_PATHS) {
  481. p += np->_paths[np->_numPaths++].deserialize(b,p);
  482. } else {
  483. // Skip any paths beyond max, but still read stream
  484. Path foo;
  485. p += foo.deserialize(b,p);
  486. }
  487. }
  488. const unsigned int numNetworkComs = b.template at<uint32_t>(p); p += 4;
  489. for(unsigned int i=0;i<numNetworkComs;++i) {
  490. _NetworkCom &c = np->_networkComs[b.template at<uint64_t>(p)]; p += 8;
  491. c.ts = b.template at<uint64_t>(p); p += 8;
  492. p += c.com.deserialize(b,p);
  493. }
  494. const unsigned int numLastPushed = b.template at<uint32_t>(p); p += 4;
  495. for(unsigned int i=0;i<numLastPushed;++i) {
  496. const uint64_t nwid = b.template at<uint64_t>(p); p += 8;
  497. const uint64_t ts = b.template at<uint64_t>(p); p += 8;
  498. np->_lastPushedComs.set(nwid,ts);
  499. }
  500. return np;
  501. }
  502. private:
  503. bool _checkPath(Path &p,const uint64_t now);
  504. Path *_getBestPath(const uint64_t now);
  505. Path *_getBestPath(const uint64_t now,int inetAddressFamily);
  506. unsigned char _key[ZT_PEER_SECRET_KEY_LENGTH]; // computed with key agreement, not serialized
  507. const RuntimeEnvironment *RR;
  508. uint64_t _lastUsed;
  509. uint64_t _lastReceive; // direct or indirect
  510. uint64_t _lastUnicastFrame;
  511. uint64_t _lastMulticastFrame;
  512. uint64_t _lastAnnouncedTo;
  513. uint64_t _lastDirectPathPushSent;
  514. uint64_t _lastDirectPathPushReceive;
  515. uint64_t _lastPathSort;
  516. uint16_t _vProto;
  517. uint16_t _vMajor;
  518. uint16_t _vMinor;
  519. uint16_t _vRevision;
  520. Identity _id;
  521. Path _paths[ZT_MAX_PEER_NETWORK_PATHS];
  522. unsigned int _numPaths;
  523. unsigned int _latency;
  524. unsigned int _directPathPushCutoffCount;
  525. struct _NetworkCom
  526. {
  527. _NetworkCom() {}
  528. _NetworkCom(uint64_t t,const CertificateOfMembership &c) : ts(t),com(c) {}
  529. uint64_t ts;
  530. CertificateOfMembership com;
  531. };
  532. Hashtable<uint64_t,_NetworkCom> _networkComs;
  533. Hashtable<uint64_t,uint64_t> _lastPushedComs;
  534. Mutex _lock;
  535. AtomicCounter __refCount;
  536. };
  537. } // namespace ZeroTier
  538. // Add a swap() for shared ptr's to peers to speed up peer sorts
  539. namespace std {
  540. template<>
  541. inline void swap(ZeroTier::SharedPtr<ZeroTier::Peer> &a,ZeroTier::SharedPtr<ZeroTier::Peer> &b)
  542. {
  543. a.swap(b);
  544. }
  545. }
  546. #endif