Peer.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /*
  2. * Copyright (c)2013-2020 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: 2026-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 "../include/ZeroTierOne.h"
  16. #include "AES.hpp"
  17. #include "Address.hpp"
  18. #include "AtomicCounter.hpp"
  19. #include "Bond.hpp"
  20. #include "Constants.hpp"
  21. #include "Identity.hpp"
  22. #include "InetAddress.hpp"
  23. #include "Metrics.hpp"
  24. #include "Mutex.hpp"
  25. #include "Packet.hpp"
  26. #include "Path.hpp"
  27. #include "RuntimeEnvironment.hpp"
  28. #include "SharedPtr.hpp"
  29. #include "Utils.hpp"
  30. #include <list>
  31. #include <vector>
  32. #define ZT_PEER_MAX_SERIALIZED_STATE_SIZE (sizeof(Peer) + 32 + (sizeof(Path) * 2))
  33. namespace ZeroTier {
  34. /**
  35. * Peer on P2P Network (virtual layer 1)
  36. */
  37. class Peer {
  38. friend class SharedPtr<Peer>;
  39. friend class SharedPtr<Bond>;
  40. friend class Switch;
  41. friend class Bond;
  42. private:
  43. Peer() = delete; // disabled to prevent bugs -- should not be constructed uninitialized
  44. public:
  45. ~Peer()
  46. {
  47. Utils::burn(_key, sizeof(_key));
  48. }
  49. /**
  50. * Construct a new peer
  51. *
  52. * @param renv Runtime environment
  53. * @param myIdentity Identity of THIS node (for key agreement)
  54. * @param peerIdentity Identity of peer
  55. * @throws std::runtime_error Key agreement with peer's identity failed
  56. */
  57. Peer(const RuntimeEnvironment* renv, const Identity& myIdentity, const Identity& peerIdentity);
  58. /**
  59. * @return This peer's ZT address (short for identity().address())
  60. */
  61. inline const Address& address() const
  62. {
  63. return _id.address();
  64. }
  65. /**
  66. * @return This peer's identity
  67. */
  68. inline const Identity& identity() const
  69. {
  70. return _id;
  71. }
  72. /**
  73. * Log receipt of an authenticated packet
  74. *
  75. * This is called by the decode pipe when a packet is proven to be authentic
  76. * and appears to be valid.
  77. *
  78. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  79. * @param path Path over which packet was received
  80. * @param hops ZeroTier (not IP) hops
  81. * @param packetId Packet ID
  82. * @param verb Packet verb
  83. * @param inRePacketId Packet ID in reply to (default: none)
  84. * @param inReVerb Verb in reply to (for OK/ERROR, default: VERB_NOP)
  85. * @param trustEstablished If true, some form of non-trivial trust (like allowed in network) has been established
  86. * @param networkId Network ID if this pertains to a network, or 0 otherwise
  87. */
  88. void received(
  89. void* tPtr,
  90. const SharedPtr<Path>& path,
  91. const unsigned int hops,
  92. const uint64_t packetId,
  93. const unsigned int payloadLength,
  94. const Packet::Verb verb,
  95. const uint64_t inRePacketId,
  96. const Packet::Verb inReVerb,
  97. const bool trustEstablished,
  98. const uint64_t networkId,
  99. const int32_t flowId);
  100. /**
  101. * Check whether we have an active path to this peer via the given address
  102. *
  103. * @param now Current time
  104. * @param addr Remote address
  105. * @return True if we have an active path to this destination
  106. */
  107. inline bool hasActivePathTo(int64_t now, const InetAddress& addr) const
  108. {
  109. Mutex::Lock _l(_paths_m);
  110. for (unsigned int i = 0; i < ZT_MAX_PEER_NETWORK_PATHS; ++i) {
  111. if (_paths[i].p) {
  112. if (((now - _paths[i].lr) < ZT_PEER_PATH_EXPIRATION) && (_paths[i].p->address() == addr)) {
  113. return true;
  114. }
  115. }
  116. else {
  117. break;
  118. }
  119. }
  120. return false;
  121. }
  122. /**
  123. * Send via best direct path
  124. *
  125. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  126. * @param data Packet data
  127. * @param len Packet length
  128. * @param now Current time
  129. * @param force If true, send even if path is not alive
  130. * @return True if we actually sent something
  131. */
  132. inline bool sendDirect(void* tPtr, const void* data, unsigned int len, int64_t now, bool force)
  133. {
  134. SharedPtr<Path> bp(getAppropriatePath(now, force));
  135. if (bp) {
  136. return bp->send(RR, tPtr, data, len, now);
  137. }
  138. return false;
  139. }
  140. /**
  141. * Record incoming packets to
  142. *
  143. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  144. * @param path Path over which packet was received
  145. * @param packetId Packet ID
  146. * @param payloadLength Length of packet data payload
  147. * @param verb Packet verb
  148. * @param flowId Flow ID
  149. * @param now Current time
  150. */
  151. void recordIncomingPacket(const SharedPtr<Path>& path, const uint64_t packetId, uint16_t payloadLength, const Packet::Verb verb, const int32_t flowId, int64_t now);
  152. /**
  153. *
  154. * @param path Path over which packet is being sent
  155. * @param packetId Packet ID
  156. * @param payloadLength Length of packet data payload
  157. * @param verb Packet verb
  158. * @param flowId Flow ID
  159. * @param now Current time
  160. */
  161. void recordOutgoingPacket(const SharedPtr<Path>& path, const uint64_t packetId, uint16_t payloadLength, const Packet::Verb verb, const int32_t flowId, int64_t now);
  162. /**
  163. * Record an invalid incoming packet. This packet failed
  164. * MAC/compression/cipher checks and will now contribute to a
  165. * Packet Error Ratio (PER).
  166. *
  167. * @param path Path over which packet was received
  168. */
  169. void recordIncomingInvalidPacket(const SharedPtr<Path>& path);
  170. /**
  171. * Get the most appropriate direct path based on current multipath and QoS configuration
  172. *
  173. * @param now Current time
  174. * @param includeExpired If true, include even expired paths
  175. * @return Best current path or NULL if none
  176. */
  177. SharedPtr<Path> getAppropriatePath(int64_t now, bool includeExpired, int32_t flowId = -1);
  178. /**
  179. * Send VERB_RENDEZVOUS to this and another peer via the best common IP scope and path
  180. */
  181. void introduce(void* const tPtr, const int64_t now, const SharedPtr<Peer>& other) const;
  182. /**
  183. * Send a HELLO to this peer at a specified physical address
  184. *
  185. * No statistics or sent times are updated here.
  186. *
  187. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  188. * @param localSocket Local source socket
  189. * @param atAddress Destination address
  190. * @param now Current time
  191. */
  192. void sendHELLO(void* tPtr, const int64_t localSocket, const InetAddress& atAddress, int64_t now);
  193. /**
  194. * Send ECHO (or HELLO for older peers) to this peer at the given address
  195. *
  196. * No statistics or sent times are updated here.
  197. *
  198. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  199. * @param localSocket Local source socket
  200. * @param atAddress Destination address
  201. * @param now Current time
  202. * @param sendFullHello If true, always send a full HELLO instead of just an ECHO
  203. */
  204. void attemptToContactAt(void* tPtr, const int64_t localSocket, const InetAddress& atAddress, int64_t now, bool sendFullHello);
  205. /**
  206. * Try a memorized or statically defined path if any are known
  207. *
  208. * Under the hood this is done periodically based on ZT_TRY_MEMORIZED_PATH_INTERVAL.
  209. *
  210. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  211. * @param now Current time
  212. */
  213. void tryMemorizedPath(void* tPtr, int64_t now);
  214. /**
  215. * A check to be performed periodically which determines whether multipath communication is
  216. * possible with this peer. This check should be performed early in the life-cycle of the peer
  217. * as well as during the process of learning new paths.
  218. */
  219. void performMultipathStateCheck(void* tPtr, int64_t now);
  220. /**
  221. * Send pings or keepalives depending on configured timeouts
  222. *
  223. * This also cleans up some internal data structures. It's called periodically from Node.
  224. *
  225. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  226. * @param now Current time
  227. * @param inetAddressFamily Keep this address family alive, or -1 for any
  228. * @return 0 if nothing sent or bit mask: bit 0x1 if IPv4 sent, bit 0x2 if IPv6 sent (0x3 means both sent)
  229. */
  230. unsigned int doPingAndKeepalive(void* tPtr, int64_t now);
  231. /**
  232. * Process a cluster redirect sent by this peer
  233. *
  234. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  235. * @param originatingPath Path from which redirect originated
  236. * @param remoteAddress Remote address
  237. * @param now Current time
  238. */
  239. void clusterRedirect(void* tPtr, const SharedPtr<Path>& originatingPath, const InetAddress& remoteAddress, const int64_t now);
  240. /**
  241. * Reset paths within a given IP scope and address family
  242. *
  243. * Resetting a path involves sending an ECHO to it and then deactivating
  244. * it until or unless it responds. This is done when we detect a change
  245. * to our external IP or another system change that might invalidate
  246. * many or all current paths.
  247. *
  248. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  249. * @param scope IP scope
  250. * @param inetAddressFamily Family e.g. AF_INET
  251. * @param now Current time
  252. */
  253. void resetWithinScope(void* tPtr, InetAddress::IpScope scope, int inetAddressFamily, int64_t now);
  254. /**
  255. * @param now Current time
  256. * @return All known paths to this peer
  257. */
  258. inline std::vector<SharedPtr<Path> > paths(const int64_t now) const
  259. {
  260. std::vector<SharedPtr<Path> > pp;
  261. Mutex::Lock _l(_paths_m);
  262. for (unsigned int i = 0; i < ZT_MAX_PEER_NETWORK_PATHS; ++i) {
  263. if (! _paths[i].p) {
  264. break;
  265. }
  266. pp.push_back(_paths[i].p);
  267. }
  268. return pp;
  269. }
  270. /**
  271. * @return Time of last receive of anything, whether direct or relayed
  272. */
  273. inline int64_t lastReceive() const
  274. {
  275. return _lastReceive;
  276. }
  277. /**
  278. * @return True if we've heard from this peer in less than ZT_PEER_ACTIVITY_TIMEOUT
  279. */
  280. inline bool isAlive(const int64_t now) const
  281. {
  282. return ((now - _lastReceive) < ZT_PEER_ACTIVITY_TIMEOUT);
  283. }
  284. /**
  285. * @return True if this peer has sent us real network traffic recently
  286. */
  287. inline int64_t isActive(int64_t now) const
  288. {
  289. return ((now - _lastNontrivialReceive) < ZT_PEER_ACTIVITY_TIMEOUT);
  290. }
  291. inline int64_t lastSentFullHello()
  292. {
  293. return _lastSentFullHello;
  294. }
  295. /**
  296. * @return Latency in milliseconds of best/aggregate path or 0xffff if unknown / no paths
  297. */
  298. inline unsigned int latency(const int64_t now)
  299. {
  300. if (_localMultipathSupported) {
  301. return (int)_lastComputedAggregateMeanLatency;
  302. }
  303. else {
  304. SharedPtr<Path> bp(getAppropriatePath(now, false));
  305. if (bp) {
  306. return (unsigned int)bp->latency();
  307. }
  308. return 0xffff;
  309. }
  310. }
  311. /**
  312. * This computes a quality score for relays and root servers
  313. *
  314. * If we haven't heard anything from these in ZT_PEER_ACTIVITY_TIMEOUT, they
  315. * receive the worst possible quality (max unsigned int). Otherwise the
  316. * quality is a product of latency and the number of potential missed
  317. * pings. This causes roots and relays to switch over a bit faster if they
  318. * fail.
  319. *
  320. * @return Relay quality score computed from latency and other factors, lower is better
  321. */
  322. inline unsigned int relayQuality(const int64_t now)
  323. {
  324. const uint64_t tsr = now - _lastReceive;
  325. if (tsr >= ZT_PEER_ACTIVITY_TIMEOUT) {
  326. return (~(unsigned int)0);
  327. }
  328. unsigned int l = latency(now);
  329. if (! l) {
  330. l = 0xffff;
  331. }
  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
  338. {
  339. return _key;
  340. }
  341. /**
  342. * Set the currently known remote version of this peer's client
  343. *
  344. * @param vproto Protocol version
  345. * @param vmaj Major version
  346. * @param vmin Minor version
  347. * @param vrev Revision
  348. */
  349. inline void setRemoteVersion(unsigned int vproto, unsigned int vmaj, unsigned int vmin, unsigned int vrev)
  350. {
  351. _vProto = (uint16_t)vproto;
  352. _vMajor = (uint16_t)vmaj;
  353. _vMinor = (uint16_t)vmin;
  354. _vRevision = (uint16_t)vrev;
  355. }
  356. inline unsigned int remoteVersionProtocol() const
  357. {
  358. return _vProto;
  359. }
  360. inline unsigned int remoteVersionMajor() const
  361. {
  362. return _vMajor;
  363. }
  364. inline unsigned int remoteVersionMinor() const
  365. {
  366. return _vMinor;
  367. }
  368. inline unsigned int remoteVersionRevision() const
  369. {
  370. return _vRevision;
  371. }
  372. inline bool remoteVersionKnown() const
  373. {
  374. return ((_vMajor > 0) || (_vMinor > 0) || (_vRevision > 0));
  375. }
  376. /**
  377. * @return True if peer has received a trust established packet (e.g. common network membership) in the past ZT_TRUST_EXPIRATION ms
  378. */
  379. inline bool trustEstablished(const int64_t now) const
  380. {
  381. return ((now - _lastTrustEstablishedPacketReceived) < ZT_TRUST_EXPIRATION);
  382. }
  383. /**
  384. * Rate limit gate for VERB_PUSH_DIRECT_PATHS
  385. */
  386. inline bool rateGatePushDirectPaths(const int64_t now)
  387. {
  388. if ((now - _lastDirectPathPushReceive) <= ZT_PUSH_DIRECT_PATHS_CUTOFF_TIME) {
  389. ++_directPathPushCutoffCount;
  390. }
  391. else {
  392. _directPathPushCutoffCount = 0;
  393. }
  394. _lastDirectPathPushReceive = now;
  395. return (_directPathPushCutoffCount < ZT_PUSH_DIRECT_PATHS_CUTOFF_LIMIT);
  396. }
  397. /**
  398. * Rate limit gate for VERB_NETWORK_CREDENTIALS
  399. */
  400. inline bool rateGateCredentialsReceived(const int64_t now)
  401. {
  402. if ((now - _lastCredentialsReceived) >= ZT_PEER_CREDENTIALS_RATE_LIMIT) {
  403. _lastCredentialsReceived = now;
  404. return true;
  405. }
  406. return false;
  407. }
  408. /**
  409. * Rate limit gate for sending of ERROR_NEED_MEMBERSHIP_CERTIFICATE
  410. */
  411. inline bool rateGateRequestCredentials(const int64_t now)
  412. {
  413. if ((now - _lastCredentialRequestSent) >= ZT_PEER_GENERAL_RATE_LIMIT) {
  414. _lastCredentialRequestSent = now;
  415. return true;
  416. }
  417. return false;
  418. }
  419. /**
  420. * Rate limit gate for inbound WHOIS requests
  421. */
  422. inline bool rateGateInboundWhoisRequest(const int64_t now)
  423. {
  424. if ((now - _lastWhoisRequestReceived) >= ZT_PEER_WHOIS_RATE_LIMIT) {
  425. _lastWhoisRequestReceived = now;
  426. return true;
  427. }
  428. return false;
  429. }
  430. /**
  431. * See definition in Bond
  432. */
  433. inline bool rateGateQoS(int64_t now, SharedPtr<Path>& path)
  434. {
  435. Mutex::Lock _l(_bond_m);
  436. if (_bond) {
  437. return _bond->rateGateQoS(now, path);
  438. }
  439. return false; // Default behavior. If there is no bond, we drop these
  440. }
  441. /**
  442. * See definition in Bond
  443. */
  444. void receivedQoS(const SharedPtr<Path>& path, int64_t now, int count, uint64_t* rx_id, uint16_t* rx_ts)
  445. {
  446. Mutex::Lock _l(_bond_m);
  447. if (_bond) {
  448. _bond->receivedQoS(path, now, count, rx_id, rx_ts);
  449. }
  450. }
  451. /**
  452. * See definition in Bond
  453. */
  454. void processIncomingPathNegotiationRequest(uint64_t now, SharedPtr<Path>& path, int16_t remoteUtility)
  455. {
  456. Mutex::Lock _l(_bond_m);
  457. if (_bond) {
  458. _bond->processIncomingPathNegotiationRequest(now, path, remoteUtility);
  459. }
  460. }
  461. /**
  462. * See definition in Bond
  463. */
  464. inline bool rateGatePathNegotiation(int64_t now, SharedPtr<Path>& path)
  465. {
  466. Mutex::Lock _l(_bond_m);
  467. if (_bond) {
  468. return _bond->rateGatePathNegotiation(now, path);
  469. }
  470. return false; // Default behavior. If there is no bond, we drop these
  471. }
  472. /**
  473. * See definition in Bond
  474. */
  475. bool flowHashingSupported()
  476. {
  477. Mutex::Lock _l(_bond_m);
  478. if (_bond) {
  479. return _bond->flowHashingSupported();
  480. }
  481. return false;
  482. }
  483. /**
  484. * Serialize a peer for storage in local cache
  485. *
  486. * This does not serialize everything, just non-ephemeral information.
  487. */
  488. template <unsigned int C> inline void serializeForCache(Buffer<C>& b) const
  489. {
  490. b.append((uint8_t)2);
  491. _id.serialize(b);
  492. b.append((uint16_t)_vProto);
  493. b.append((uint16_t)_vMajor);
  494. b.append((uint16_t)_vMinor);
  495. b.append((uint16_t)_vRevision);
  496. {
  497. Mutex::Lock _l(_paths_m);
  498. unsigned int pc = 0;
  499. for (unsigned int i = 0; i < ZT_MAX_PEER_NETWORK_PATHS; ++i) {
  500. if (_paths[i].p) {
  501. ++pc;
  502. }
  503. else {
  504. break;
  505. }
  506. }
  507. b.append((uint16_t)pc);
  508. for (unsigned int i = 0; i < pc; ++i) {
  509. _paths[i].p->address().serialize(b);
  510. }
  511. }
  512. }
  513. template <unsigned int C> inline static SharedPtr<Peer> deserializeFromCache(int64_t now, void* tPtr, Buffer<C>& b, const RuntimeEnvironment* renv)
  514. {
  515. try {
  516. unsigned int ptr = 0;
  517. if (b[ptr++] != 2) {
  518. return SharedPtr<Peer>();
  519. }
  520. Identity id;
  521. ptr += id.deserialize(b, ptr);
  522. if (! id) {
  523. return SharedPtr<Peer>();
  524. }
  525. SharedPtr<Peer> p(new Peer(renv, renv->identity, id));
  526. p->_vProto = b.template at<uint16_t>(ptr);
  527. ptr += 2;
  528. p->_vMajor = b.template at<uint16_t>(ptr);
  529. ptr += 2;
  530. p->_vMinor = b.template at<uint16_t>(ptr);
  531. ptr += 2;
  532. p->_vRevision = b.template at<uint16_t>(ptr);
  533. ptr += 2;
  534. // When we deserialize from the cache we don't actually restore paths. We
  535. // just try them and then re-learn them if they happen to still be up.
  536. // Paths are fairly ephemeral in the real world in most cases.
  537. const unsigned int tryPathCount = b.template at<uint16_t>(ptr);
  538. ptr += 2;
  539. for (unsigned int i = 0; i < tryPathCount; ++i) {
  540. InetAddress inaddr;
  541. try {
  542. ptr += inaddr.deserialize(b, ptr);
  543. if (inaddr) {
  544. p->attemptToContactAt(tPtr, -1, inaddr, now, true);
  545. }
  546. }
  547. catch (...) {
  548. break;
  549. }
  550. }
  551. return p;
  552. }
  553. catch (...) {
  554. return SharedPtr<Peer>();
  555. }
  556. }
  557. /**
  558. * @return The bonding policy used to reach this peer
  559. */
  560. SharedPtr<Bond> bond()
  561. {
  562. return _bond;
  563. }
  564. /**
  565. * @return The bonding policy used to reach this peer
  566. */
  567. inline int8_t bondingPolicy()
  568. {
  569. Mutex::Lock _l(_bond_m);
  570. if (_bond) {
  571. return _bond->policy();
  572. }
  573. return ZT_BOND_POLICY_NONE;
  574. }
  575. /**
  576. * @return the number of links in this bond which are considered alive
  577. */
  578. inline uint8_t getNumAliveLinks()
  579. {
  580. Mutex::Lock _l(_paths_m);
  581. if (_bond) {
  582. return _bond->getNumAliveLinks();
  583. }
  584. return 0;
  585. }
  586. /**
  587. * @return the number of links in this bond
  588. */
  589. inline uint8_t getNumTotalLinks()
  590. {
  591. Mutex::Lock _l(_paths_m);
  592. if (_bond) {
  593. return _bond->getNumTotalLinks();
  594. }
  595. return 0;
  596. }
  597. // inline const AES *aesKeysIfSupported() const
  598. //{ return (const AES *)0; }
  599. inline const AES* aesKeysIfSupported() const
  600. {
  601. return (_vProto >= 12) ? _aesKeys : (const AES*)0;
  602. }
  603. inline const AES* aesKeys() const
  604. {
  605. return _aesKeys;
  606. }
  607. private:
  608. struct _PeerPath {
  609. _PeerPath() : lr(0), p(), priority(1)
  610. {
  611. }
  612. int64_t lr; // time of last valid ZeroTier packet
  613. SharedPtr<Path> p;
  614. long priority; // >= 1, higher is better
  615. };
  616. uint8_t _key[ZT_SYMMETRIC_KEY_SIZE];
  617. AES _aesKeys[2];
  618. const RuntimeEnvironment* RR;
  619. int64_t _lastReceive; // direct or indirect
  620. int64_t _lastNontrivialReceive; // frames, things like netconf, etc.
  621. int64_t _lastTriedMemorizedPath;
  622. int64_t _lastDirectPathPushSent;
  623. int64_t _lastDirectPathPushReceive;
  624. int64_t _lastCredentialRequestSent;
  625. int64_t _lastWhoisRequestReceived;
  626. int64_t _lastCredentialsReceived;
  627. int64_t _lastTrustEstablishedPacketReceived;
  628. int64_t _lastSentFullHello;
  629. int64_t _lastEchoCheck;
  630. unsigned char _freeRandomByte;
  631. uint16_t _vProto;
  632. uint16_t _vMajor;
  633. uint16_t _vMinor;
  634. uint16_t _vRevision;
  635. std::list<std::pair<Path*, int64_t> > _lastTriedPath;
  636. Mutex _lastTriedPath_m;
  637. _PeerPath _paths[ZT_MAX_PEER_NETWORK_PATHS];
  638. Mutex _paths_m;
  639. Mutex _bond_m;
  640. bool _isLeaf;
  641. Identity _id;
  642. unsigned int _directPathPushCutoffCount;
  643. unsigned int _echoRequestCutoffCount;
  644. AtomicCounter __refCount;
  645. bool _localMultipathSupported;
  646. volatile bool _shouldCollectPathStatistics;
  647. int32_t _lastComputedAggregateMeanLatency;
  648. SharedPtr<Bond> _bond;
  649. #ifndef ZT_NO_PEER_METRICS
  650. prometheus::Histogram<uint64_t>& _peer_latency;
  651. prometheus::simpleapi::gauge_metric_t _alive_path_count;
  652. prometheus::simpleapi::gauge_metric_t _dead_path_count;
  653. prometheus::simpleapi::counter_metric_t _incoming_packet;
  654. prometheus::simpleapi::counter_metric_t _outgoing_packet;
  655. prometheus::simpleapi::counter_metric_t _packet_errors;
  656. #endif
  657. };
  658. } // namespace ZeroTier
  659. // Add a swap() for shared ptr's to peers to speed up peer sorts
  660. namespace std {
  661. template <> inline void swap(ZeroTier::SharedPtr<ZeroTier::Peer>& a, ZeroTier::SharedPtr<ZeroTier::Peer>& b)
  662. {
  663. a.swap(b);
  664. }
  665. } // namespace std
  666. #endif