Peer.hpp 19 KB

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