Peer.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2023-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_PEER_HPP
  14. #define ZT_PEER_HPP
  15. #include <vector>
  16. #include "../include/ZeroTierOne.h"
  17. #include "Constants.hpp"
  18. #include "RuntimeEnvironment.hpp"
  19. #include "Node.hpp"
  20. #include "Path.hpp"
  21. #include "Address.hpp"
  22. #include "Utils.hpp"
  23. #include "Identity.hpp"
  24. #include "InetAddress.hpp"
  25. #include "Packet.hpp"
  26. #include "SharedPtr.hpp"
  27. #include "AtomicCounter.hpp"
  28. #include "Hashtable.hpp"
  29. #include "Mutex.hpp"
  30. #define ZT_PEER_MAX_SERIALIZED_STATE_SIZE (sizeof(Peer) + 32 + (sizeof(Path) * 2))
  31. namespace ZeroTier {
  32. /**
  33. * Peer on P2P Network (virtual layer 1)
  34. */
  35. class Peer
  36. {
  37. friend class SharedPtr<Peer>;
  38. private:
  39. Peer() {} // disabled to prevent bugs -- should not be constructed uninitialized
  40. public:
  41. ~Peer() { Utils::burn(_key,sizeof(_key)); }
  42. /**
  43. * Construct a new peer
  44. *
  45. * @param renv Runtime environment
  46. * @param myIdentity Identity of THIS node (for key agreement)
  47. * @param peerIdentity Identity of peer
  48. * @throws std::runtime_error Key agreement with peer's identity failed
  49. */
  50. Peer(const RuntimeEnvironment *renv,const Identity &myIdentity,const Identity &peerIdentity);
  51. /**
  52. * @return This peer's ZT address (short for identity().address())
  53. */
  54. inline const Address &address() const { return _id.address(); }
  55. /**
  56. * @return This peer's identity
  57. */
  58. inline const Identity &identity() const { return _id; }
  59. /**
  60. * Log receipt of an authenticated packet
  61. *
  62. * This is called by the decode pipe when a packet is proven to be authentic
  63. * and appears to be valid.
  64. *
  65. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  66. * @param path Path over which packet was received
  67. * @param hops ZeroTier (not IP) hops
  68. * @param packetId Packet ID
  69. * @param verb Packet verb
  70. * @param inRePacketId Packet ID in reply to (default: none)
  71. * @param inReVerb Verb in reply to (for OK/ERROR, default: VERB_NOP)
  72. * @param trustEstablished If true, some form of non-trivial trust (like allowed in network) has been established
  73. * @param networkId Network ID if this pertains to a network, or 0 otherwise
  74. */
  75. void received(
  76. void *tPtr,
  77. const SharedPtr<Path> &path,
  78. const unsigned int hops,
  79. const uint64_t packetId,
  80. const unsigned int payloadLength,
  81. const Packet::Verb verb,
  82. const uint64_t inRePacketId,
  83. const Packet::Verb inReVerb,
  84. const bool trustEstablished,
  85. const uint64_t networkId);
  86. /**
  87. * Check whether we have an active path to this peer via the given address
  88. *
  89. * @param now Current time
  90. * @param addr Remote address
  91. * @return True if we have an active path to this destination
  92. */
  93. inline bool hasActivePathTo(int64_t now,const InetAddress &addr) const
  94. {
  95. Mutex::Lock _l(_paths_m);
  96. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  97. if (_paths[i].p) {
  98. if (((now - _paths[i].lr) < ZT_PEER_PATH_EXPIRATION)&&(_paths[i].p->address() == addr))
  99. return true;
  100. } else break;
  101. }
  102. return false;
  103. }
  104. /**
  105. * Send via best direct path
  106. *
  107. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  108. * @param data Packet data
  109. * @param len Packet length
  110. * @param now Current time
  111. * @param force If true, send even if path is not alive
  112. * @return True if we actually sent something
  113. */
  114. inline bool sendDirect(void *tPtr,const void *data,unsigned int len,int64_t now,bool force)
  115. {
  116. SharedPtr<Path> bp(getAppropriatePath(now,force));
  117. if (bp)
  118. return bp->send(RR,tPtr,data,len,now);
  119. return false;
  120. }
  121. /**
  122. * Record statistics on outgoing packets
  123. *
  124. * @param path Path over which packet was sent
  125. * @param id Packet ID
  126. * @param len Length of packet payload
  127. * @param verb Packet verb
  128. * @param now Current time
  129. */
  130. void recordOutgoingPacket(const SharedPtr<Path> &path, const uint64_t packetId, uint16_t payloadLength, const Packet::Verb verb, int64_t now);
  131. /**
  132. * Record statistics on incoming packets
  133. *
  134. * @param path Path over which packet was sent
  135. * @param id Packet ID
  136. * @param len Length of packet payload
  137. * @param verb Packet verb
  138. * @param now Current time
  139. */
  140. void recordIncomingPacket(void *tPtr, const SharedPtr<Path> &path, const uint64_t packetId, uint16_t payloadLength, const Packet::Verb verb, int64_t now);
  141. /**
  142. * Send an ACK to peer for the most recent packets received
  143. *
  144. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  145. * @param localSocket Raw socket the ACK packet will be sent over
  146. * @param atAddress Destination for the ACK packet
  147. * @param now Current time
  148. */
  149. void sendACK(void *tPtr, const SharedPtr<Path> &path, const int64_t localSocket,const InetAddress &atAddress,int64_t now);
  150. /**
  151. * Send a QoS packet to peer so that it can evaluate the quality of this link
  152. *
  153. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  154. * @param localSocket Raw socket the QoS packet will be sent over
  155. * @param atAddress Destination for the QoS packet
  156. * @param now Current time
  157. */
  158. void sendQOS_MEASUREMENT(void *tPtr, const SharedPtr<Path> &path, const int64_t localSocket,const InetAddress &atAddress,int64_t now);
  159. /**
  160. * Compute relative quality values and allocations for the components of the aggregate link
  161. *
  162. * @param now Current time
  163. */
  164. void computeAggregateProportionalAllocation(int64_t now);
  165. /**
  166. * @return The aggregate link Packet Delay Variance (PDV)
  167. */
  168. int computeAggregateLinkPacketDelayVariance();
  169. /**
  170. * @return The aggregate link mean latency
  171. */
  172. int computeAggregateLinkMeanLatency();
  173. /**
  174. * @return The number of currently alive "physical" paths in the aggregate link
  175. */
  176. int aggregateLinkPhysicalPathCount();
  177. /**
  178. * @return The number of currently alive "logical" paths in the aggregate link
  179. */
  180. int aggregateLinkLogicalPathCount();
  181. /**
  182. * Get the most appropriate direct path based on current multipath and QoS configuration
  183. *
  184. * @param now Current time
  185. * @param includeExpired If true, include even expired paths
  186. * @return Best current path or NULL if none
  187. */
  188. SharedPtr<Path> getAppropriatePath(int64_t now, bool includeExpired);
  189. /**
  190. * Generate a human-readable string of interface names making up the aggregate link, also include
  191. * moving allocation and IP version number for each (for tracing)
  192. */
  193. char *interfaceListStr();
  194. /**
  195. * Send VERB_RENDEZVOUS to this and another peer via the best common IP scope and path
  196. */
  197. void introduce(void *const tPtr,const int64_t now,const SharedPtr<Peer> &other) const;
  198. /**
  199. * Send a HELLO to this peer at a specified physical address
  200. *
  201. * No statistics or sent times are updated here.
  202. *
  203. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  204. * @param localSocket Local source socket
  205. * @param atAddress Destination address
  206. * @param now Current time
  207. */
  208. void sendHELLO(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now);
  209. /**
  210. * Send ECHO (or HELLO for older peers) to this peer at the given address
  211. *
  212. * No statistics or sent times are updated here.
  213. *
  214. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  215. * @param localSocket Local source socket
  216. * @param atAddress Destination address
  217. * @param now Current time
  218. * @param sendFullHello If true, always send a full HELLO instead of just an ECHO
  219. */
  220. void attemptToContactAt(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now,bool sendFullHello);
  221. /**
  222. * Try a memorized or statically defined path if any are known
  223. *
  224. * Under the hood this is done periodically based on ZT_TRY_MEMORIZED_PATH_INTERVAL.
  225. *
  226. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  227. * @param now Current time
  228. */
  229. void tryMemorizedPath(void *tPtr,int64_t now);
  230. /**
  231. * Send pings or keepalives depending on configured timeouts
  232. *
  233. * This also cleans up some internal data structures. It's called periodically from Node.
  234. *
  235. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  236. * @param now Current time
  237. * @param inetAddressFamily Keep this address family alive, or -1 for any
  238. * @return 0 if nothing sent or bit mask: bit 0x1 if IPv4 sent, bit 0x2 if IPv6 sent (0x3 means both sent)
  239. */
  240. unsigned int doPingAndKeepalive(void *tPtr,int64_t now);
  241. /**
  242. * Clear paths whose localSocket(s) are in a CLOSED state or have an otherwise INVALID state.
  243. * This should be called frequently so that we can detect and remove unproductive or invalid paths.
  244. *
  245. * Under the hood this is done periodically based on ZT_CLOSED_PATH_PRUNING_INTERVAL.
  246. *
  247. * @return Number of paths that were pruned this round
  248. */
  249. unsigned int prunePaths();
  250. /**
  251. * Process a cluster redirect sent by this peer
  252. *
  253. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  254. * @param originatingPath Path from which redirect originated
  255. * @param remoteAddress Remote address
  256. * @param now Current time
  257. */
  258. void clusterRedirect(void *tPtr,const SharedPtr<Path> &originatingPath,const InetAddress &remoteAddress,const int64_t now);
  259. /**
  260. * Reset paths within a given IP scope and address family
  261. *
  262. * Resetting a path involves sending an ECHO to it and then deactivating
  263. * it until or unless it responds. This is done when we detect a change
  264. * to our external IP or another system change that might invalidate
  265. * many or all current paths.
  266. *
  267. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  268. * @param scope IP scope
  269. * @param inetAddressFamily Family e.g. AF_INET
  270. * @param now Current time
  271. */
  272. void resetWithinScope(void *tPtr,InetAddress::IpScope scope,int inetAddressFamily,int64_t now);
  273. /**
  274. * @param now Current time
  275. * @return All known paths to this peer
  276. */
  277. inline std::vector< SharedPtr<Path> > paths(const int64_t now) const
  278. {
  279. std::vector< SharedPtr<Path> > pp;
  280. Mutex::Lock _l(_paths_m);
  281. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  282. if (!_paths[i].p) break;
  283. pp.push_back(_paths[i].p);
  284. }
  285. return pp;
  286. }
  287. /**
  288. * @return Time of last receive of anything, whether direct or relayed
  289. */
  290. inline int64_t lastReceive() const { return _lastReceive; }
  291. /**
  292. * @return True if we've heard from this peer in less than ZT_PEER_ACTIVITY_TIMEOUT
  293. */
  294. inline bool isAlive(const int64_t now) const { return ((now - _lastReceive) < ZT_PEER_ACTIVITY_TIMEOUT); }
  295. /**
  296. * @return True if this peer has sent us real network traffic recently
  297. */
  298. inline int64_t isActive(int64_t now) const { return ((now - _lastNontrivialReceive) < ZT_PEER_ACTIVITY_TIMEOUT); }
  299. /**
  300. * @return Latency in milliseconds of best/aggregate path or 0xffff if unknown / no paths
  301. */
  302. inline unsigned int latency(const int64_t now)
  303. {
  304. if (_canUseMultipath) {
  305. return (int)computeAggregateLinkMeanLatency();
  306. } else {
  307. SharedPtr<Path> bp(getAppropriatePath(now,false));
  308. if (bp)
  309. return bp->latency();
  310. return 0xffff;
  311. }
  312. }
  313. /**
  314. * This computes a quality score for relays and root servers
  315. *
  316. * If we haven't heard anything from these in ZT_PEER_ACTIVITY_TIMEOUT, they
  317. * receive the worst possible quality (max unsigned int). Otherwise the
  318. * quality is a product of latency and the number of potential missed
  319. * pings. This causes roots and relays to switch over a bit faster if they
  320. * fail.
  321. *
  322. * @return Relay quality score computed from latency and other factors, lower is better
  323. */
  324. inline unsigned int relayQuality(const int64_t now)
  325. {
  326. const uint64_t tsr = now - _lastReceive;
  327. if (tsr >= ZT_PEER_ACTIVITY_TIMEOUT)
  328. return (~(unsigned int)0);
  329. unsigned int l = latency(now);
  330. if (!l)
  331. l = 0xffff;
  332. return (l * (((unsigned int)tsr / (ZT_PEER_PING_PERIOD + 1000)) + 1));
  333. }
  334. /**
  335. * @return 256-bit secret symmetric encryption key
  336. */
  337. inline const unsigned char *key() const { return _key; }
  338. /**
  339. * Set the currently known remote version of this peer's client
  340. *
  341. * @param vproto Protocol version
  342. * @param vmaj Major version
  343. * @param vmin Minor version
  344. * @param vrev Revision
  345. */
  346. inline void setRemoteVersion(unsigned int vproto,unsigned int vmaj,unsigned int vmin,unsigned int vrev)
  347. {
  348. _vProto = (uint16_t)vproto;
  349. _vMajor = (uint16_t)vmaj;
  350. _vMinor = (uint16_t)vmin;
  351. _vRevision = (uint16_t)vrev;
  352. }
  353. inline unsigned int remoteVersionProtocol() const { return _vProto; }
  354. inline unsigned int remoteVersionMajor() const { return _vMajor; }
  355. inline unsigned int remoteVersionMinor() const { return _vMinor; }
  356. inline unsigned int remoteVersionRevision() const { return _vRevision; }
  357. inline bool remoteVersionKnown() const { return ((_vMajor > 0)||(_vMinor > 0)||(_vRevision > 0)); }
  358. /**
  359. * Periodically update known multipath activation constraints. This is done so that we know when and when
  360. * not to use multipath logic. Doing this once every few seconds is sufficient.
  361. *
  362. * @param now Current time
  363. */
  364. inline void processBackgroundPeerTasks(const int64_t now);
  365. /**
  366. * Record that the remote peer does have multipath enabled. As is evident by the receipt of a VERB_ACK
  367. * or a VERB_QOS_MEASUREMENT packet at some point in the past. Until this flag is set, the local client
  368. * shall assume that multipath is not enabled and should only use classical Protocol 9 logic.
  369. */
  370. inline void inferRemoteMultipathEnabled() { _remotePeerMultipathEnabled = true; }
  371. /**
  372. * @return Whether the local client supports and is configured to use multipath
  373. */
  374. inline bool localMultipathSupport() { return _localMultipathSupported; }
  375. /**
  376. * @return Whether the remote peer supports and is configured to use multipath
  377. */
  378. inline bool remoteMultipathSupport() { return _remoteMultipathSupported; }
  379. /**
  380. * @return Whether this client can use multipath to communicate with this peer. True if both peers are using
  381. * the correct protocol and if both peers have multipath enabled. False if otherwise.
  382. */
  383. inline bool canUseMultipath() { return _canUseMultipath; }
  384. /**
  385. * @return True if peer has received a trust established packet (e.g. common network membership) in the past ZT_TRUST_EXPIRATION ms
  386. */
  387. inline bool trustEstablished(const int64_t now) const { return ((now - _lastTrustEstablishedPacketReceived) < ZT_TRUST_EXPIRATION); }
  388. /**
  389. * Rate limit gate for VERB_PUSH_DIRECT_PATHS
  390. */
  391. inline bool rateGatePushDirectPaths(const int64_t now)
  392. {
  393. if ((now - _lastDirectPathPushReceive) <= ZT_PUSH_DIRECT_PATHS_CUTOFF_TIME)
  394. ++_directPathPushCutoffCount;
  395. else _directPathPushCutoffCount = 0;
  396. _lastDirectPathPushReceive = now;
  397. return (_directPathPushCutoffCount < ZT_PUSH_DIRECT_PATHS_CUTOFF_LIMIT);
  398. }
  399. /**
  400. * Rate limit gate for VERB_NETWORK_CREDENTIALS
  401. */
  402. inline bool rateGateCredentialsReceived(const int64_t now)
  403. {
  404. if ((now - _lastCredentialsReceived) <= ZT_PEER_CREDENTIALS_CUTOFF_TIME)
  405. ++_credentialsCutoffCount;
  406. else _credentialsCutoffCount = 0;
  407. _lastCredentialsReceived = now;
  408. return (_directPathPushCutoffCount < ZT_PEER_CREDEITIALS_CUTOFF_LIMIT);
  409. }
  410. /**
  411. * Rate limit gate for sending of ERROR_NEED_MEMBERSHIP_CERTIFICATE
  412. */
  413. inline bool rateGateRequestCredentials(const int64_t now)
  414. {
  415. if ((now - _lastCredentialRequestSent) >= ZT_PEER_GENERAL_RATE_LIMIT) {
  416. _lastCredentialRequestSent = now;
  417. return true;
  418. }
  419. return false;
  420. }
  421. /**
  422. * Rate limit gate for inbound WHOIS requests
  423. */
  424. inline bool rateGateInboundWhoisRequest(const int64_t now)
  425. {
  426. if ((now - _lastWhoisRequestReceived) >= ZT_PEER_WHOIS_RATE_LIMIT) {
  427. _lastWhoisRequestReceived = now;
  428. return true;
  429. }
  430. return false;
  431. }
  432. /**
  433. * Rate limit gate for inbound ECHO requests
  434. */
  435. inline bool rateGateEchoRequest(const int64_t now)
  436. {
  437. if ((now - _lastEchoRequestReceived) >= ZT_PEER_GENERAL_RATE_LIMIT) {
  438. _lastEchoRequestReceived = now;
  439. return true;
  440. }
  441. return false;
  442. }
  443. /**
  444. * Rate limit gate for VERB_ACK
  445. */
  446. inline bool rateGateACK(const int64_t now)
  447. {
  448. if ((now - _lastACKWindowReset) >= ZT_PATH_QOS_ACK_CUTOFF_TIME) {
  449. _lastACKWindowReset = now;
  450. _ACKCutoffCount = 0;
  451. } else {
  452. ++_ACKCutoffCount;
  453. }
  454. return (_ACKCutoffCount < ZT_PATH_QOS_ACK_CUTOFF_LIMIT);
  455. }
  456. /**
  457. * Rate limit gate for VERB_QOS_MEASUREMENT
  458. */
  459. inline bool rateGateQoS(const int64_t now)
  460. {
  461. if ((now - _lastQoSWindowReset) >= ZT_PATH_QOS_ACK_CUTOFF_TIME) {
  462. _lastQoSWindowReset = now;
  463. _QoSCutoffCount = 0;
  464. } else {
  465. ++_QoSCutoffCount;
  466. }
  467. return (_QoSCutoffCount < ZT_PATH_QOS_ACK_CUTOFF_LIMIT);
  468. }
  469. /**
  470. * @return Whether this peer is reachable via an aggregate link
  471. */
  472. inline bool hasAggregateLink() {
  473. return _localMultipathSupported && _remoteMultipathSupported && _remotePeerMultipathEnabled;
  474. }
  475. /**
  476. * Serialize a peer for storage in local cache
  477. *
  478. * This does not serialize everything, just non-ephemeral information.
  479. */
  480. template<unsigned int C>
  481. inline void serializeForCache(Buffer<C> &b) const
  482. {
  483. b.append((uint8_t)1);
  484. _id.serialize(b);
  485. b.append((uint16_t)_vProto);
  486. b.append((uint16_t)_vMajor);
  487. b.append((uint16_t)_vMinor);
  488. b.append((uint16_t)_vRevision);
  489. {
  490. Mutex::Lock _l(_paths_m);
  491. unsigned int pc = 0;
  492. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  493. if (_paths[i].p)
  494. ++pc;
  495. else break;
  496. }
  497. b.append((uint16_t)pc);
  498. for(unsigned int i=0;i<pc;++i)
  499. _paths[i].p->address().serialize(b);
  500. }
  501. }
  502. template<unsigned int C>
  503. inline static SharedPtr<Peer> deserializeFromCache(int64_t now,void *tPtr,Buffer<C> &b,const RuntimeEnvironment *renv)
  504. {
  505. try {
  506. unsigned int ptr = 0;
  507. if (b[ptr++] != 1)
  508. return SharedPtr<Peer>();
  509. Identity id;
  510. ptr += id.deserialize(b,ptr);
  511. if (!id)
  512. return SharedPtr<Peer>();
  513. SharedPtr<Peer> p(new Peer(renv,renv->identity,id));
  514. p->_vProto = b.template at<uint16_t>(ptr); ptr += 2;
  515. p->_vMajor = b.template at<uint16_t>(ptr); ptr += 2;
  516. p->_vMinor = b.template at<uint16_t>(ptr); ptr += 2;
  517. p->_vRevision = b.template at<uint16_t>(ptr); ptr += 2;
  518. // When we deserialize from the cache we don't actually restore paths. We
  519. // just try them and then re-learn them if they happen to still be up.
  520. // Paths are fairly ephemeral in the real world in most cases.
  521. const unsigned int tryPathCount = b.template at<uint16_t>(ptr); ptr += 2;
  522. for(unsigned int i=0;i<tryPathCount;++i) {
  523. InetAddress inaddr;
  524. try {
  525. ptr += inaddr.deserialize(b,ptr);
  526. if (inaddr)
  527. p->attemptToContactAt(tPtr,-1,inaddr,now,true);
  528. } catch ( ... ) {
  529. break;
  530. }
  531. }
  532. return p;
  533. } catch ( ... ) {
  534. return SharedPtr<Peer>();
  535. }
  536. }
  537. private:
  538. struct _PeerPath
  539. {
  540. _PeerPath() : lr(0),p(),priority(1) {}
  541. int64_t lr; // time of last valid ZeroTier packet
  542. SharedPtr<Path> p;
  543. long priority; // >= 1, higher is better
  544. };
  545. uint8_t _key[ZT_PEER_SECRET_KEY_LENGTH];
  546. const RuntimeEnvironment *RR;
  547. int64_t _lastReceive; // direct or indirect
  548. int64_t _lastNontrivialReceive; // frames, things like netconf, etc.
  549. int64_t _lastTriedMemorizedPath;
  550. int64_t _lastDirectPathPushSent;
  551. int64_t _lastDirectPathPushReceive;
  552. int64_t _lastCredentialRequestSent;
  553. int64_t _lastWhoisRequestReceived;
  554. int64_t _lastEchoRequestReceived;
  555. int64_t _lastCredentialsReceived;
  556. int64_t _lastTrustEstablishedPacketReceived;
  557. int64_t _lastSentFullHello;
  558. int64_t _lastPathPrune;
  559. int64_t _lastACKWindowReset;
  560. int64_t _lastQoSWindowReset;
  561. int64_t _lastMultipathCompatibilityCheck;
  562. unsigned char _freeRandomByte;
  563. int _uniqueAlivePathCount;
  564. bool _localMultipathSupported;
  565. bool _remoteMultipathSupported;
  566. bool _canUseMultipath;
  567. uint16_t _vProto;
  568. uint16_t _vMajor;
  569. uint16_t _vMinor;
  570. uint16_t _vRevision;
  571. _PeerPath _paths[ZT_MAX_PEER_NETWORK_PATHS];
  572. Mutex _paths_m;
  573. Identity _id;
  574. unsigned int _directPathPushCutoffCount;
  575. unsigned int _credentialsCutoffCount;
  576. unsigned int _QoSCutoffCount;
  577. unsigned int _ACKCutoffCount;
  578. AtomicCounter __refCount;
  579. RingBuffer<int,ZT_MULTIPATH_PROPORTION_WIN_SZ> _pathChoiceHist;
  580. bool _linkIsBalanced;
  581. bool _linkIsRedundant;
  582. bool _remotePeerMultipathEnabled;
  583. int64_t _lastAggregateStatsReport;
  584. int64_t _lastAggregateAllocation;
  585. char _interfaceListStr[256]; // 16 characters * 16 paths in a link
  586. };
  587. } // namespace ZeroTier
  588. // Add a swap() for shared ptr's to peers to speed up peer sorts
  589. namespace std {
  590. template<>
  591. inline void swap(ZeroTier::SharedPtr<ZeroTier::Peer> &a,ZeroTier::SharedPtr<ZeroTier::Peer> &b)
  592. {
  593. a.swap(b);
  594. }
  595. }
  596. #endif