Peer.hpp 19 KB

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