Peer.hpp 19 KB

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