Peer.cpp 19 KB

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