Peer.hpp 19 KB

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