Peer.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. #include "../version.h"
  14. #include "Constants.hpp"
  15. #include "Peer.hpp"
  16. #include "Switch.hpp"
  17. #include "Network.hpp"
  18. #include "SelfAwareness.hpp"
  19. #include "Packet.hpp"
  20. #include "Trace.hpp"
  21. #include "InetAddress.hpp"
  22. #include "RingBuffer.hpp"
  23. #include "Utils.hpp"
  24. namespace ZeroTier {
  25. static unsigned char s_freeRandomByteCounter = 0;
  26. Peer::Peer(const RuntimeEnvironment *renv,const Identity &myIdentity,const Identity &peerIdentity) :
  27. RR(renv),
  28. _lastReceive(0),
  29. _lastNontrivialReceive(0),
  30. _lastTriedMemorizedPath(0),
  31. _lastDirectPathPushSent(0),
  32. _lastDirectPathPushReceive(0),
  33. _lastEchoRequestReceived(0),
  34. _lastCredentialRequestSent(0),
  35. _lastWhoisRequestReceived(0),
  36. _lastCredentialsReceived(0),
  37. _lastTrustEstablishedPacketReceived(0),
  38. _lastSentFullHello(0),
  39. _lastEchoCheck(0),
  40. _freeRandomByte((unsigned char)((uintptr_t)this >> 4) ^ ++s_freeRandomByteCounter),
  41. _vProto(0),
  42. _vMajor(0),
  43. _vMinor(0),
  44. _vRevision(0),
  45. _id(peerIdentity),
  46. _directPathPushCutoffCount(0),
  47. _credentialsCutoffCount(0),
  48. _echoRequestCutoffCount(0),
  49. _localMultipathSupported(false),
  50. _lastComputedAggregateMeanLatency(0)
  51. {
  52. if (!myIdentity.agree(peerIdentity,_key))
  53. throw ZT_EXCEPTION_INVALID_ARGUMENT;
  54. uint8_t ktmp[ZT_SYMMETRIC_KEY_SIZE];
  55. KBKDFHMACSHA384(_key,ZT_KBKDF_LABEL_AES_GMAC_SIV_K0,0,0,ktmp);
  56. _aesKeys[0].init(ktmp);
  57. KBKDFHMACSHA384(_key,ZT_KBKDF_LABEL_AES_GMAC_SIV_K1,0,0,ktmp);
  58. _aesKeys[1].init(ktmp);
  59. Utils::burn(ktmp,ZT_SYMMETRIC_KEY_SIZE);
  60. }
  61. void Peer::received(
  62. void *tPtr,
  63. const SharedPtr<Path> &path,
  64. const unsigned int hops,
  65. const uint64_t packetId,
  66. const unsigned int payloadLength,
  67. const Packet::Verb verb,
  68. const uint64_t inRePacketId,
  69. const Packet::Verb inReVerb,
  70. const bool trustEstablished,
  71. const uint64_t networkId,
  72. const int32_t flowId)
  73. {
  74. const int64_t now = RR->node->now();
  75. _lastReceive = now;
  76. switch (verb) {
  77. case Packet::VERB_FRAME:
  78. case Packet::VERB_EXT_FRAME:
  79. case Packet::VERB_NETWORK_CONFIG_REQUEST:
  80. case Packet::VERB_NETWORK_CONFIG:
  81. case Packet::VERB_MULTICAST_FRAME:
  82. _lastNontrivialReceive = now;
  83. break;
  84. default:
  85. break;
  86. }
  87. recordIncomingPacket(path, packetId, payloadLength, verb, flowId, now);
  88. if (trustEstablished) {
  89. _lastTrustEstablishedPacketReceived = now;
  90. path->trustedPacketReceived(now);
  91. }
  92. if (hops == 0) {
  93. // If this is a direct packet (no hops), update existing paths or learn new ones
  94. bool havePath = false;
  95. {
  96. Mutex::Lock _l(_paths_m);
  97. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  98. if (_paths[i].p) {
  99. if (_paths[i].p == path) {
  100. _paths[i].lr = now;
  101. havePath = true;
  102. break;
  103. }
  104. } else {
  105. break;
  106. }
  107. }
  108. }
  109. if ( (!havePath) && RR->node->shouldUsePathForZeroTierTraffic(tPtr,_id.address(),path->localSocket(),path->address()) ) {
  110. if (verb == Packet::VERB_OK) {
  111. Mutex::Lock _l(_paths_m);
  112. unsigned int replacePath = ZT_MAX_PEER_NETWORK_PATHS;
  113. long replacePathQuality = 0;
  114. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  115. if (_paths[i].p) {
  116. if ( (!_paths[i].p->alive(now)) || _paths[i].p->address().ipsEqual(path->address()) ) {
  117. replacePath = i;
  118. break;
  119. } else {
  120. const long q = _paths[i].p->quality(now) / _paths[i].priority;
  121. if (q > replacePathQuality) {
  122. replacePathQuality = q;
  123. replacePath = i;
  124. }
  125. }
  126. } else {
  127. replacePath = i;
  128. break;
  129. }
  130. }
  131. if (replacePath != ZT_MAX_PEER_NETWORK_PATHS) {
  132. RR->t->peerLearnedNewPath(tPtr, networkId, *this, path, packetId);
  133. _paths[replacePath].lr = now;
  134. _paths[replacePath].p = path;
  135. _paths[replacePath].priority = 1;
  136. }
  137. } else {
  138. Mutex::Lock ltl(_lastTriedPath_m);
  139. bool triedTooRecently = false;
  140. for(std::list< std::pair< Path *, int64_t > >::iterator i(_lastTriedPath.begin());i!=_lastTriedPath.end();) {
  141. if ((now - i->second) > 1000) {
  142. _lastTriedPath.erase(i++);
  143. } else if (i->first == path.ptr()) {
  144. ++i;
  145. triedTooRecently = true;
  146. } else {
  147. ++i;
  148. }
  149. }
  150. if (!triedTooRecently) {
  151. _lastTriedPath.push_back(std::pair< Path *, int64_t >(path.ptr(), now));
  152. attemptToContactAt(tPtr,path->localSocket(),path->address(),now,true);
  153. path->sent(now);
  154. RR->t->peerConfirmingUnknownPath(tPtr,networkId,*this,path,packetId,verb);
  155. }
  156. }
  157. }
  158. }
  159. // If we have a trust relationship periodically push a message enumerating
  160. // all known external addresses for ourselves. If we already have a path this
  161. // is done less frequently.
  162. if (this->trustEstablished(now)) {
  163. const int64_t sinceLastPush = now - _lastDirectPathPushSent;
  164. if (sinceLastPush >= ((hops == 0) ? ZT_DIRECT_PATH_PUSH_INTERVAL_HAVEPATH : ZT_DIRECT_PATH_PUSH_INTERVAL)) {
  165. _lastDirectPathPushSent = now;
  166. std::vector<InetAddress> pathsToPush(RR->node->directPaths());
  167. if (!pathsToPush.empty()) {
  168. std::vector<InetAddress>::const_iterator p(pathsToPush.begin());
  169. while (p != pathsToPush.end()) {
  170. Packet *const outp = new Packet(_id.address(),RR->identity.address(),Packet::VERB_PUSH_DIRECT_PATHS);
  171. outp->addSize(2); // leave room for count
  172. unsigned int count = 0;
  173. while ((p != pathsToPush.end())&&((outp->size() + 24) < 1200)) {
  174. uint8_t addressType = 4;
  175. switch(p->ss_family) {
  176. case AF_INET:
  177. break;
  178. case AF_INET6:
  179. addressType = 6;
  180. break;
  181. default: // we currently only push IP addresses
  182. ++p;
  183. continue;
  184. }
  185. outp->append((uint8_t)0); // no flags
  186. outp->append((uint16_t)0); // no extensions
  187. outp->append(addressType);
  188. outp->append((uint8_t)((addressType == 4) ? 6 : 18));
  189. outp->append(p->rawIpData(),((addressType == 4) ? 4 : 16));
  190. outp->append((uint16_t)p->port());
  191. ++count;
  192. ++p;
  193. }
  194. if (count) {
  195. outp->setAt(ZT_PACKET_IDX_PAYLOAD,(uint16_t)count);
  196. outp->compress();
  197. outp->armor(_key,true,aesKeysIfSupported());
  198. path->send(RR,tPtr,outp->data(),outp->size(),now);
  199. }
  200. delete outp;
  201. }
  202. }
  203. }
  204. }
  205. }
  206. SharedPtr<Path> Peer::getAppropriatePath(int64_t now, bool includeExpired, int32_t flowId)
  207. {
  208. Mutex::Lock _l(_bond_m);
  209. if (!_bond) {
  210. Mutex::Lock _l(_paths_m);
  211. unsigned int bestPath = ZT_MAX_PEER_NETWORK_PATHS;
  212. /**
  213. * Send traffic across the highest quality path only. This algorithm will still
  214. * use the old path quality metric from protocol version 9.
  215. */
  216. long bestPathQuality = 2147483647;
  217. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  218. if (_paths[i].p) {
  219. if ((includeExpired)||((now - _paths[i].lr) < ZT_PEER_PATH_EXPIRATION)) {
  220. const long q = _paths[i].p->quality(now) / _paths[i].priority;
  221. if (q <= bestPathQuality) {
  222. bestPathQuality = q;
  223. bestPath = i;
  224. }
  225. }
  226. } else break;
  227. }
  228. if (bestPath != ZT_MAX_PEER_NETWORK_PATHS) {
  229. return _paths[bestPath].p;
  230. }
  231. return SharedPtr<Path>();
  232. }
  233. return _bond->getAppropriatePath(now, flowId);
  234. }
  235. void Peer::introduce(void *const tPtr,const int64_t now,const SharedPtr<Peer> &other) const
  236. {
  237. unsigned int myBestV4ByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  238. unsigned int myBestV6ByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  239. long myBestV4QualityByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  240. long myBestV6QualityByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  241. unsigned int theirBestV4ByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  242. unsigned int theirBestV6ByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  243. long theirBestV4QualityByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  244. long theirBestV6QualityByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  245. for(int i=0;i<=ZT_INETADDRESS_MAX_SCOPE;++i) {
  246. myBestV4ByScope[i] = ZT_MAX_PEER_NETWORK_PATHS;
  247. myBestV6ByScope[i] = ZT_MAX_PEER_NETWORK_PATHS;
  248. myBestV4QualityByScope[i] = 2147483647;
  249. myBestV6QualityByScope[i] = 2147483647;
  250. theirBestV4ByScope[i] = ZT_MAX_PEER_NETWORK_PATHS;
  251. theirBestV6ByScope[i] = ZT_MAX_PEER_NETWORK_PATHS;
  252. theirBestV4QualityByScope[i] = 2147483647;
  253. theirBestV6QualityByScope[i] = 2147483647;
  254. }
  255. Mutex::Lock _l1(_paths_m);
  256. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  257. if (_paths[i].p) {
  258. const long q = _paths[i].p->quality(now) / _paths[i].priority;
  259. const unsigned int s = (unsigned int)_paths[i].p->ipScope();
  260. switch(_paths[i].p->address().ss_family) {
  261. case AF_INET:
  262. if (q <= myBestV4QualityByScope[s]) {
  263. myBestV4QualityByScope[s] = q;
  264. myBestV4ByScope[s] = i;
  265. }
  266. break;
  267. case AF_INET6:
  268. if (q <= myBestV6QualityByScope[s]) {
  269. myBestV6QualityByScope[s] = q;
  270. myBestV6ByScope[s] = i;
  271. }
  272. break;
  273. }
  274. } else break;
  275. }
  276. Mutex::Lock _l2(other->_paths_m);
  277. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  278. if (other->_paths[i].p) {
  279. const long q = other->_paths[i].p->quality(now) / other->_paths[i].priority;
  280. const unsigned int s = (unsigned int)other->_paths[i].p->ipScope();
  281. switch(other->_paths[i].p->address().ss_family) {
  282. case AF_INET:
  283. if (q <= theirBestV4QualityByScope[s]) {
  284. theirBestV4QualityByScope[s] = q;
  285. theirBestV4ByScope[s] = i;
  286. }
  287. break;
  288. case AF_INET6:
  289. if (q <= theirBestV6QualityByScope[s]) {
  290. theirBestV6QualityByScope[s] = q;
  291. theirBestV6ByScope[s] = i;
  292. }
  293. break;
  294. }
  295. } else break;
  296. }
  297. unsigned int mine = ZT_MAX_PEER_NETWORK_PATHS;
  298. unsigned int theirs = ZT_MAX_PEER_NETWORK_PATHS;
  299. for(int s=ZT_INETADDRESS_MAX_SCOPE;s>=0;--s) {
  300. if ((myBestV6ByScope[s] != ZT_MAX_PEER_NETWORK_PATHS)&&(theirBestV6ByScope[s] != ZT_MAX_PEER_NETWORK_PATHS)) {
  301. mine = myBestV6ByScope[s];
  302. theirs = theirBestV6ByScope[s];
  303. break;
  304. }
  305. if ((myBestV4ByScope[s] != ZT_MAX_PEER_NETWORK_PATHS)&&(theirBestV4ByScope[s] != ZT_MAX_PEER_NETWORK_PATHS)) {
  306. mine = myBestV4ByScope[s];
  307. theirs = theirBestV4ByScope[s];
  308. break;
  309. }
  310. }
  311. if (mine != ZT_MAX_PEER_NETWORK_PATHS) {
  312. unsigned int alt = (unsigned int)RR->node->prng() & 1; // randomize which hint we send first for black magickal NAT-t reasons
  313. const unsigned int completed = alt + 2;
  314. while (alt != completed) {
  315. if ((alt & 1) == 0) {
  316. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_RENDEZVOUS);
  317. outp.append((uint8_t)0);
  318. other->_id.address().appendTo(outp);
  319. outp.append((uint16_t)other->_paths[theirs].p->address().port());
  320. if (other->_paths[theirs].p->address().ss_family == AF_INET6) {
  321. outp.append((uint8_t)16);
  322. outp.append(other->_paths[theirs].p->address().rawIpData(),16);
  323. } else {
  324. outp.append((uint8_t)4);
  325. outp.append(other->_paths[theirs].p->address().rawIpData(),4);
  326. }
  327. outp.armor(_key,true,aesKeysIfSupported());
  328. _paths[mine].p->send(RR,tPtr,outp.data(),outp.size(),now);
  329. } else {
  330. Packet outp(other->_id.address(),RR->identity.address(),Packet::VERB_RENDEZVOUS);
  331. outp.append((uint8_t)0);
  332. _id.address().appendTo(outp);
  333. outp.append((uint16_t)_paths[mine].p->address().port());
  334. if (_paths[mine].p->address().ss_family == AF_INET6) {
  335. outp.append((uint8_t)16);
  336. outp.append(_paths[mine].p->address().rawIpData(),16);
  337. } else {
  338. outp.append((uint8_t)4);
  339. outp.append(_paths[mine].p->address().rawIpData(),4);
  340. }
  341. outp.armor(other->_key,true,aesKeysIfSupported());
  342. other->_paths[theirs].p->send(RR,tPtr,outp.data(),outp.size(),now);
  343. }
  344. ++alt;
  345. }
  346. }
  347. }
  348. void Peer::sendHELLO(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now)
  349. {
  350. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_HELLO);
  351. outp.append((unsigned char)ZT_PROTO_VERSION);
  352. outp.append((unsigned char)ZEROTIER_ONE_VERSION_MAJOR);
  353. outp.append((unsigned char)ZEROTIER_ONE_VERSION_MINOR);
  354. outp.append((uint16_t)ZEROTIER_ONE_VERSION_REVISION);
  355. outp.append(now);
  356. RR->identity.serialize(outp,false);
  357. atAddress.serialize(outp);
  358. outp.append((uint64_t)RR->topology->planetWorldId());
  359. outp.append((uint64_t)RR->topology->planetWorldTimestamp());
  360. const unsigned int startCryptedPortionAt = outp.size();
  361. std::vector<World> moons(RR->topology->moons());
  362. std::vector<uint64_t> moonsWanted(RR->topology->moonsWanted());
  363. outp.append((uint16_t)(moons.size() + moonsWanted.size()));
  364. for(std::vector<World>::const_iterator m(moons.begin());m!=moons.end();++m) {
  365. outp.append((uint8_t)m->type());
  366. outp.append((uint64_t)m->id());
  367. outp.append((uint64_t)m->timestamp());
  368. }
  369. for(std::vector<uint64_t>::const_iterator m(moonsWanted.begin());m!=moonsWanted.end();++m) {
  370. outp.append((uint8_t)World::TYPE_MOON);
  371. outp.append(*m);
  372. outp.append((uint64_t)0);
  373. }
  374. outp.cryptField(_key,startCryptedPortionAt,outp.size() - startCryptedPortionAt);
  375. if (atAddress) {
  376. outp.armor(_key,false,nullptr); // false == don't encrypt full payload, but add MAC
  377. RR->node->expectReplyTo(outp.packetId());
  378. RR->node->putPacket(tPtr,localSocket,atAddress,outp.data(),outp.size());
  379. } else {
  380. RR->node->expectReplyTo(outp.packetId());
  381. RR->sw->send(tPtr,outp,false); // false == don't encrypt full payload, but add MAC
  382. }
  383. }
  384. void Peer::attemptToContactAt(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now,bool sendFullHello)
  385. {
  386. if ( (!sendFullHello) && (_vProto >= 5) && (!((_vMajor == 1)&&(_vMinor == 1)&&(_vRevision == 0))) ) {
  387. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_ECHO);
  388. outp.armor(_key,true,aesKeysIfSupported());
  389. RR->node->expectReplyTo(outp.packetId());
  390. RR->node->putPacket(tPtr,localSocket,atAddress,outp.data(),outp.size());
  391. } else {
  392. sendHELLO(tPtr,localSocket,atAddress,now);
  393. }
  394. }
  395. void Peer::tryMemorizedPath(void *tPtr,int64_t now)
  396. {
  397. if ((now - _lastTriedMemorizedPath) >= ZT_TRY_MEMORIZED_PATH_INTERVAL) {
  398. _lastTriedMemorizedPath = now;
  399. InetAddress mp;
  400. if (RR->node->externalPathLookup(tPtr,_id.address(),-1,mp))
  401. attemptToContactAt(tPtr,-1,mp,now,true);
  402. }
  403. }
  404. void Peer::performMultipathStateCheck(void *tPtr, int64_t now)
  405. {
  406. Mutex::Lock _l(_bond_m);
  407. /**
  408. * Check for conditions required for multipath bonding and create a bond
  409. * if allowed.
  410. */
  411. _localMultipathSupported = ((RR->bc->inUse()) && (ZT_PROTO_VERSION > 9));
  412. if (_localMultipathSupported && !_bond) {
  413. if (RR->bc) {
  414. _bond = RR->bc->createTransportTriggeredBond(RR, this);
  415. /**
  416. * Allow new bond to retroactively learn all paths known to this peer
  417. */
  418. if (_bond) {
  419. for (unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  420. if (_paths[i].p) {
  421. _bond->nominatePathToBond(_paths[i].p, now);
  422. }
  423. }
  424. }
  425. }
  426. }
  427. }
  428. unsigned int Peer::doPingAndKeepalive(void *tPtr,int64_t now)
  429. {
  430. unsigned int sent = 0;
  431. Mutex::Lock _l(_paths_m);
  432. performMultipathStateCheck(tPtr, now);
  433. const bool sendFullHello = ((now - _lastSentFullHello) >= ZT_PEER_PING_PERIOD);
  434. _lastSentFullHello = now;
  435. // Right now we only keep pinging links that have the maximum priority. The
  436. // priority is used to track cluster redirections, meaning that when a cluster
  437. // redirects us its redirect target links override all other links and we
  438. // let those old links expire.
  439. long maxPriority = 0;
  440. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  441. if (_paths[i].p)
  442. maxPriority = std::max(_paths[i].priority,maxPriority);
  443. else break;
  444. }
  445. unsigned int j = 0;
  446. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  447. if (_paths[i].p) {
  448. // Clean expired and reduced priority paths
  449. if ( ((now - _paths[i].lr) < ZT_PEER_PATH_EXPIRATION) && (_paths[i].priority == maxPriority) ) {
  450. if ((sendFullHello)||(_paths[i].p->needsHeartbeat(now))) {
  451. attemptToContactAt(tPtr,_paths[i].p->localSocket(),_paths[i].p->address(),now,sendFullHello);
  452. _paths[i].p->sent(now);
  453. sent |= (_paths[i].p->address().ss_family == AF_INET) ? 0x1 : 0x2;
  454. }
  455. if (i != j)
  456. _paths[j] = _paths[i];
  457. ++j;
  458. }
  459. } else break;
  460. }
  461. return sent;
  462. }
  463. void Peer::clusterRedirect(void *tPtr,const SharedPtr<Path> &originatingPath,const InetAddress &remoteAddress,const int64_t now)
  464. {
  465. SharedPtr<Path> np(RR->topology->getPath(originatingPath->localSocket(),remoteAddress));
  466. RR->t->peerRedirected(tPtr,0,*this,np);
  467. attemptToContactAt(tPtr,originatingPath->localSocket(),remoteAddress,now,true);
  468. {
  469. Mutex::Lock _l(_paths_m);
  470. // New priority is higher than the priority of the originating path (if known)
  471. long newPriority = 1;
  472. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  473. if (_paths[i].p) {
  474. if (_paths[i].p == originatingPath) {
  475. newPriority = _paths[i].priority;
  476. break;
  477. }
  478. } else break;
  479. }
  480. newPriority += 2;
  481. // Erase any paths with lower priority than this one or that are duplicate
  482. // IPs and add this path.
  483. unsigned int j = 0;
  484. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  485. if (_paths[i].p) {
  486. if ((_paths[i].priority >= newPriority)&&(!_paths[i].p->address().ipsEqual2(remoteAddress))) {
  487. if (i != j)
  488. _paths[j] = _paths[i];
  489. ++j;
  490. }
  491. }
  492. }
  493. if (j < ZT_MAX_PEER_NETWORK_PATHS) {
  494. _paths[j].lr = now;
  495. _paths[j].p = np;
  496. _paths[j].priority = newPriority;
  497. ++j;
  498. while (j < ZT_MAX_PEER_NETWORK_PATHS) {
  499. _paths[j].lr = 0;
  500. _paths[j].p.zero();
  501. _paths[j].priority = 1;
  502. ++j;
  503. }
  504. }
  505. }
  506. }
  507. void Peer::resetWithinScope(void *tPtr,InetAddress::IpScope scope,int inetAddressFamily,int64_t now)
  508. {
  509. Mutex::Lock _l(_paths_m);
  510. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  511. if (_paths[i].p) {
  512. if ((_paths[i].p->address().ss_family == inetAddressFamily)&&(_paths[i].p->ipScope() == scope)) {
  513. attemptToContactAt(tPtr,_paths[i].p->localSocket(),_paths[i].p->address(),now,false);
  514. _paths[i].p->sent(now);
  515. _paths[i].lr = 0; // path will not be used unless it speaks again
  516. }
  517. } else break;
  518. }
  519. }
  520. void Peer::recordOutgoingPacket(const SharedPtr<Path> &path, const uint64_t packetId,
  521. uint16_t payloadLength, const Packet::Verb verb, const int32_t flowId, int64_t now)
  522. {
  523. if (!_shouldCollectPathStatistics || !_bond) {
  524. return;
  525. }
  526. _bond->recordOutgoingPacket(path, packetId, payloadLength, verb, flowId, now);
  527. }
  528. void Peer::recordIncomingInvalidPacket(const SharedPtr<Path>& path)
  529. {
  530. if (!_shouldCollectPathStatistics || !_bond) {
  531. return;
  532. }
  533. _bond->recordIncomingInvalidPacket(path);
  534. }
  535. void Peer::recordIncomingPacket(const SharedPtr<Path> &path, const uint64_t packetId,
  536. uint16_t payloadLength, const Packet::Verb verb, const int32_t flowId, int64_t now)
  537. {
  538. if (!_shouldCollectPathStatistics || !_bond) {
  539. return;
  540. }
  541. _bond->recordIncomingPacket(path, packetId, payloadLength, verb, flowId, now);
  542. }
  543. } // namespace ZeroTier