Peer.cpp 19 KB

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