Peer.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_PEER_HPP
  27. #define ZT_PEER_HPP
  28. #include <stdint.h>
  29. #include "Constants.hpp"
  30. #include <algorithm>
  31. #include <utility>
  32. #include <vector>
  33. #include <stdexcept>
  34. #include "../include/ZeroTierOne.h"
  35. #include "RuntimeEnvironment.hpp"
  36. #include "Path.hpp"
  37. #include "Address.hpp"
  38. #include "Utils.hpp"
  39. #include "Identity.hpp"
  40. #include "InetAddress.hpp"
  41. #include "Packet.hpp"
  42. #include "SharedPtr.hpp"
  43. #include "AtomicCounter.hpp"
  44. #include "Hashtable.hpp"
  45. #include "Mutex.hpp"
  46. #define ZT_PEER_MAX_SERIALIZED_STATE_SIZE (sizeof(Peer) + 32 + (sizeof(Path) * 2))
  47. namespace ZeroTier {
  48. /**
  49. * Peer on P2P Network (virtual layer 1)
  50. */
  51. class Peer
  52. {
  53. friend class SharedPtr<Peer>;
  54. private:
  55. Peer() {} // disabled to prevent bugs -- should not be constructed uninitialized
  56. public:
  57. ~Peer() { Utils::burn(_key,sizeof(_key)); }
  58. /**
  59. * Construct a new peer
  60. *
  61. * @param renv Runtime environment
  62. * @param myIdentity Identity of THIS node (for key agreement)
  63. * @param peerIdentity Identity of peer
  64. * @throws std::runtime_error Key agreement with peer's identity failed
  65. */
  66. Peer(const RuntimeEnvironment *renv,const Identity &myIdentity,const Identity &peerIdentity);
  67. /**
  68. * @return This peer's ZT address (short for identity().address())
  69. */
  70. inline const Address &address() const { return _id.address(); }
  71. /**
  72. * @return This peer's identity
  73. */
  74. inline const Identity &identity() const { return _id; }
  75. /**
  76. * Log receipt of an authenticated packet
  77. *
  78. * This is called by the decode pipe when a packet is proven to be authentic
  79. * and appears to be valid.
  80. *
  81. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  82. * @param path Path over which packet was received
  83. * @param hops ZeroTier (not IP) hops
  84. * @param packetId Packet ID
  85. * @param verb Packet verb
  86. * @param inRePacketId Packet ID in reply to (default: none)
  87. * @param inReVerb Verb in reply to (for OK/ERROR, default: VERB_NOP)
  88. * @param trustEstablished If true, some form of non-trivial trust (like allowed in network) has been established
  89. * @param networkId Network ID if this pertains to a network, or 0 otherwise
  90. */
  91. void received(
  92. void *tPtr,
  93. const SharedPtr<Path> &path,
  94. const unsigned int hops,
  95. const uint64_t packetId,
  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. /**
  102. * Check whether we have an active path to this peer via the given address
  103. *
  104. * @param now Current time
  105. * @param addr Remote address
  106. * @return True if we have an active path to this destination
  107. */
  108. inline bool hasActivePathTo(int64_t now,const InetAddress &addr) const
  109. {
  110. Mutex::Lock _l(_paths_m);
  111. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  112. if (_paths[i].p) {
  113. if (((now - _paths[i].lr) < ZT_PEER_PATH_EXPIRATION)&&(_paths[i].p->address() == addr))
  114. return true;
  115. } else break;
  116. }
  117. return false;
  118. }
  119. /**
  120. * Send via best direct path
  121. *
  122. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  123. * @param data Packet data
  124. * @param len Packet length
  125. * @param now Current time
  126. * @param force If true, send even if path is not alive
  127. * @return True if we actually sent something
  128. */
  129. inline bool sendDirect(void *tPtr,const void *data,unsigned int len,int64_t now,bool force)
  130. {
  131. SharedPtr<Path> bp(getBestPath(now,force));
  132. if (bp)
  133. return bp->send(RR,tPtr,data,len,now);
  134. return false;
  135. }
  136. /**
  137. * Get the best current direct path
  138. *
  139. * @param now Current time
  140. * @param includeExpired If true, include even expired paths
  141. * @return Best current path or NULL if none
  142. */
  143. SharedPtr<Path> getBestPath(int64_t now,bool includeExpired) const;
  144. /**
  145. * Send VERB_RENDEZVOUS to this and another peer via the best common IP scope and path
  146. */
  147. void introduce(void *const tPtr,const int64_t now,const SharedPtr<Peer> &other) const;
  148. /**
  149. * Send a HELLO to this peer at a specified physical address
  150. *
  151. * No statistics or sent times are updated here.
  152. *
  153. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  154. * @param localSocket Local source socket
  155. * @param atAddress Destination address
  156. * @param now Current time
  157. */
  158. void sendHELLO(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now);
  159. /**
  160. * Send ECHO (or HELLO for older peers) to this peer at the given address
  161. *
  162. * No statistics or sent times are updated here.
  163. *
  164. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  165. * @param localSocket Local source socket
  166. * @param atAddress Destination address
  167. * @param now Current time
  168. * @param sendFullHello If true, always send a full HELLO instead of just an ECHO
  169. */
  170. void attemptToContactAt(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now,bool sendFullHello);
  171. /**
  172. * Try a memorized or statically defined path if any are known
  173. *
  174. * Under the hood this is done periodically based on ZT_TRY_MEMORIZED_PATH_INTERVAL.
  175. *
  176. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  177. * @param now Current time
  178. */
  179. void tryMemorizedPath(void *tPtr,int64_t now);
  180. /**
  181. * Send pings or keepalives depending on configured timeouts
  182. *
  183. * This also cleans up some internal data structures. It's called periodically from Node.
  184. *
  185. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  186. * @param now Current time
  187. * @param inetAddressFamily Keep this address family alive, or -1 for any
  188. * @return 0 if nothing sent or bit mask: bit 0x1 if IPv4 sent, bit 0x2 if IPv6 sent (0x3 means both sent)
  189. */
  190. unsigned int doPingAndKeepalive(void *tPtr,int64_t now);
  191. /**
  192. * Process a cluster redirect sent by this peer
  193. *
  194. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  195. * @param originatingPath Path from which redirect originated
  196. * @param remoteAddress Remote address
  197. * @param now Current time
  198. */
  199. void clusterRedirect(void *tPtr,const SharedPtr<Path> &originatingPath,const InetAddress &remoteAddress,const int64_t now);
  200. /**
  201. * Reset paths within a given IP scope and address family
  202. *
  203. * Resetting a path involves sending an ECHO to it and then deactivating
  204. * it until or unless it responds. This is done when we detect a change
  205. * to our external IP or another system change that might invalidate
  206. * many or all current paths.
  207. *
  208. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  209. * @param scope IP scope
  210. * @param inetAddressFamily Family e.g. AF_INET
  211. * @param now Current time
  212. */
  213. void resetWithinScope(void *tPtr,InetAddress::IpScope scope,int inetAddressFamily,int64_t now);
  214. /**
  215. * @param now Current time
  216. * @return All known paths to this peer
  217. */
  218. inline std::vector< SharedPtr<Path> > paths(const int64_t now) const
  219. {
  220. std::vector< SharedPtr<Path> > pp;
  221. Mutex::Lock _l(_paths_m);
  222. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  223. if (!_paths[i].p) break;
  224. pp.push_back(_paths[i].p);
  225. }
  226. return pp;
  227. }
  228. /**
  229. * @return Time of last receive of anything, whether direct or relayed
  230. */
  231. inline int64_t lastReceive() const { return _lastReceive; }
  232. /**
  233. * @return True if we've heard from this peer in less than ZT_PEER_ACTIVITY_TIMEOUT
  234. */
  235. inline bool isAlive(const int64_t now) const { return ((now - _lastReceive) < ZT_PEER_ACTIVITY_TIMEOUT); }
  236. /**
  237. * @return True if this peer has sent us real network traffic recently
  238. */
  239. inline int64_t isActive(int64_t now) const { return ((now - _lastNontrivialReceive) < ZT_PEER_ACTIVITY_TIMEOUT); }
  240. /**
  241. * @return Latency in milliseconds of best path or 0xffff if unknown / no paths
  242. */
  243. inline unsigned int latency(const int64_t now) const
  244. {
  245. SharedPtr<Path> bp(getBestPath(now,false));
  246. if (bp)
  247. return bp->latency();
  248. return 0xffff;
  249. }
  250. /**
  251. * This computes a quality score for relays and root servers
  252. *
  253. * If we haven't heard anything from these in ZT_PEER_ACTIVITY_TIMEOUT, they
  254. * receive the worst possible quality (max unsigned int). Otherwise the
  255. * quality is a product of latency and the number of potential missed
  256. * pings. This causes roots and relays to switch over a bit faster if they
  257. * fail.
  258. *
  259. * @return Relay quality score computed from latency and other factors, lower is better
  260. */
  261. inline unsigned int relayQuality(const int64_t now) const
  262. {
  263. const uint64_t tsr = now - _lastReceive;
  264. if (tsr >= ZT_PEER_ACTIVITY_TIMEOUT)
  265. return (~(unsigned int)0);
  266. unsigned int l = latency(now);
  267. if (!l)
  268. l = 0xffff;
  269. return (l * (((unsigned int)tsr / (ZT_PEER_PING_PERIOD + 1000)) + 1));
  270. }
  271. /**
  272. * @return 256-bit secret symmetric encryption key
  273. */
  274. inline const unsigned char *key() const { return _key; }
  275. /**
  276. * Set the currently known remote version of this peer's client
  277. *
  278. * @param vproto Protocol version
  279. * @param vmaj Major version
  280. * @param vmin Minor version
  281. * @param vrev Revision
  282. */
  283. inline void setRemoteVersion(unsigned int vproto,unsigned int vmaj,unsigned int vmin,unsigned int vrev)
  284. {
  285. _vProto = (uint16_t)vproto;
  286. _vMajor = (uint16_t)vmaj;
  287. _vMinor = (uint16_t)vmin;
  288. _vRevision = (uint16_t)vrev;
  289. }
  290. inline unsigned int remoteVersionProtocol() const { return _vProto; }
  291. inline unsigned int remoteVersionMajor() const { return _vMajor; }
  292. inline unsigned int remoteVersionMinor() const { return _vMinor; }
  293. inline unsigned int remoteVersionRevision() const { return _vRevision; }
  294. inline bool remoteVersionKnown() const { return ((_vMajor > 0)||(_vMinor > 0)||(_vRevision > 0)); }
  295. /**
  296. * @return True if peer has received a trust established packet (e.g. common network membership) in the past ZT_TRUST_EXPIRATION ms
  297. */
  298. inline bool trustEstablished(const int64_t now) const { return ((now - _lastTrustEstablishedPacketReceived) < ZT_TRUST_EXPIRATION); }
  299. /**
  300. * Rate limit gate for VERB_PUSH_DIRECT_PATHS
  301. */
  302. inline bool rateGatePushDirectPaths(const int64_t now)
  303. {
  304. if ((now - _lastDirectPathPushReceive) <= ZT_PUSH_DIRECT_PATHS_CUTOFF_TIME)
  305. ++_directPathPushCutoffCount;
  306. else _directPathPushCutoffCount = 0;
  307. _lastDirectPathPushReceive = now;
  308. return (_directPathPushCutoffCount < ZT_PUSH_DIRECT_PATHS_CUTOFF_LIMIT);
  309. }
  310. /**
  311. * Rate limit gate for VERB_NETWORK_CREDENTIALS
  312. */
  313. inline bool rateGateCredentialsReceived(const int64_t now)
  314. {
  315. if ((now - _lastCredentialsReceived) <= ZT_PEER_CREDENTIALS_CUTOFF_TIME)
  316. ++_credentialsCutoffCount;
  317. else _credentialsCutoffCount = 0;
  318. _lastCredentialsReceived = now;
  319. return (_directPathPushCutoffCount < ZT_PEER_CREDEITIALS_CUTOFF_LIMIT);
  320. }
  321. /**
  322. * Rate limit gate for sending of ERROR_NEED_MEMBERSHIP_CERTIFICATE
  323. */
  324. inline bool rateGateRequestCredentials(const int64_t now)
  325. {
  326. if ((now - _lastCredentialRequestSent) >= ZT_PEER_GENERAL_RATE_LIMIT) {
  327. _lastCredentialRequestSent = now;
  328. return true;
  329. }
  330. return false;
  331. }
  332. /**
  333. * Rate limit gate for inbound WHOIS requests
  334. */
  335. inline bool rateGateInboundWhoisRequest(const int64_t now)
  336. {
  337. if ((now - _lastWhoisRequestReceived) >= ZT_PEER_WHOIS_RATE_LIMIT) {
  338. _lastWhoisRequestReceived = now;
  339. return true;
  340. }
  341. return false;
  342. }
  343. /**
  344. * Rate limit gate for inbound ECHO requests
  345. */
  346. inline bool rateGateEchoRequest(const int64_t now)
  347. {
  348. if ((now - _lastEchoRequestReceived) >= ZT_PEER_GENERAL_RATE_LIMIT) {
  349. _lastEchoRequestReceived = now;
  350. return true;
  351. }
  352. return false;
  353. }
  354. /**
  355. * Rate gate incoming requests for network COM
  356. */
  357. inline bool rateGateIncomingComRequest(const int64_t now)
  358. {
  359. if ((now - _lastComRequestReceived) >= ZT_PEER_GENERAL_RATE_LIMIT) {
  360. _lastComRequestReceived = now;
  361. return true;
  362. }
  363. return false;
  364. }
  365. /**
  366. * Rate gate outgoing requests for network COM
  367. */
  368. inline bool rateGateOutgoingComRequest(const int64_t now)
  369. {
  370. if ((now - _lastComRequestSent) >= ZT_PEER_GENERAL_RATE_LIMIT) {
  371. _lastComRequestSent = now;
  372. return true;
  373. }
  374. return false;
  375. }
  376. /**
  377. * Serialize a peer for storage in local cache
  378. *
  379. * This does not serialize everything, just non-ephemeral information.
  380. */
  381. template<unsigned int C>
  382. inline void serializeForCache(Buffer<C> &b) const
  383. {
  384. b.append((uint8_t)1);
  385. _id.serialize(b);
  386. b.append((uint16_t)_vProto);
  387. b.append((uint16_t)_vMajor);
  388. b.append((uint16_t)_vMinor);
  389. b.append((uint16_t)_vRevision);
  390. {
  391. Mutex::Lock _l(_paths_m);
  392. unsigned int pc = 0;
  393. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  394. if (_paths[i].p)
  395. ++pc;
  396. else break;
  397. }
  398. b.append((uint16_t)pc);
  399. for(unsigned int i=0;i<pc;++i)
  400. _paths[i].p->address().serialize(b);
  401. }
  402. }
  403. template<unsigned int C>
  404. inline static SharedPtr<Peer> deserializeFromCache(int64_t now,void *tPtr,Buffer<C> &b,const RuntimeEnvironment *renv)
  405. {
  406. try {
  407. unsigned int ptr = 0;
  408. if (b[ptr++] != 1)
  409. return SharedPtr<Peer>();
  410. Identity id;
  411. ptr += id.deserialize(b,ptr);
  412. if (!id)
  413. return SharedPtr<Peer>();
  414. SharedPtr<Peer> p(new Peer(renv,renv->identity,id));
  415. p->_vProto = b.template at<uint16_t>(ptr); ptr += 2;
  416. p->_vMajor = b.template at<uint16_t>(ptr); ptr += 2;
  417. p->_vMinor = b.template at<uint16_t>(ptr); ptr += 2;
  418. p->_vRevision = b.template at<uint16_t>(ptr); ptr += 2;
  419. // When we deserialize from the cache we don't actually restore paths. We
  420. // just try them and then re-learn them if they happen to still be up.
  421. // Paths are fairly ephemeral in the real world in most cases.
  422. const unsigned int tryPathCount = b.template at<uint16_t>(ptr); ptr += 2;
  423. for(unsigned int i=0;i<tryPathCount;++i) {
  424. InetAddress inaddr;
  425. try {
  426. ptr += inaddr.deserialize(b,ptr);
  427. if (inaddr)
  428. p->attemptToContactAt(tPtr,-1,inaddr,now,true);
  429. } catch ( ... ) {
  430. break;
  431. }
  432. }
  433. return p;
  434. } catch ( ... ) {
  435. return SharedPtr<Peer>();
  436. }
  437. }
  438. private:
  439. struct _PeerPath
  440. {
  441. _PeerPath() : lr(0),p(),priority(1) {}
  442. int64_t lr; // time of last valid ZeroTier packet
  443. SharedPtr<Path> p;
  444. long priority; // >= 1, higher is better
  445. };
  446. uint8_t _key[ZT_PEER_SECRET_KEY_LENGTH];
  447. const RuntimeEnvironment *RR;
  448. int64_t _lastReceive; // direct or indirect
  449. int64_t _lastNontrivialReceive; // frames, things like netconf, etc.
  450. int64_t _lastTriedMemorizedPath;
  451. int64_t _lastDirectPathPushSent;
  452. int64_t _lastDirectPathPushReceive;
  453. int64_t _lastCredentialRequestSent;
  454. int64_t _lastWhoisRequestReceived;
  455. int64_t _lastEchoRequestReceived;
  456. int64_t _lastComRequestReceived;
  457. int64_t _lastComRequestSent;
  458. int64_t _lastCredentialsReceived;
  459. int64_t _lastTrustEstablishedPacketReceived;
  460. int64_t _lastSentFullHello;
  461. uint16_t _vProto;
  462. uint16_t _vMajor;
  463. uint16_t _vMinor;
  464. uint16_t _vRevision;
  465. _PeerPath _paths[ZT_MAX_PEER_NETWORK_PATHS];
  466. Mutex _paths_m;
  467. Identity _id;
  468. unsigned int _directPathPushCutoffCount;
  469. unsigned int _credentialsCutoffCount;
  470. AtomicCounter __refCount;
  471. };
  472. } // namespace ZeroTier
  473. // Add a swap() for shared ptr's to peers to speed up peer sorts
  474. namespace std {
  475. template<>
  476. inline void swap(ZeroTier::SharedPtr<ZeroTier::Peer> &a,ZeroTier::SharedPtr<ZeroTier::Peer> &b)
  477. {
  478. a.swap(b);
  479. }
  480. }
  481. #endif