Peer.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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 "Constants.hpp"
  14. #include "RuntimeEnvironment.hpp"
  15. #include "Trace.hpp"
  16. #include "Peer.hpp"
  17. #include "Topology.hpp"
  18. #include "Node.hpp"
  19. #include "Network.hpp"
  20. #include "SelfAwareness.hpp"
  21. #include "InetAddress.hpp"
  22. #include "Protocol.hpp"
  23. #include <set>
  24. namespace ZeroTier {
  25. struct _PathPriorityComparisonOperator
  26. {
  27. ZT_ALWAYS_INLINE bool operator()(const SharedPtr<Path> &a,const SharedPtr<Path> &b) const
  28. {
  29. return ( ((a)&&(a->lastIn() > 0)) && ((!b)||(b->lastIn() <= 0)||(a->lastIn() < b->lastIn())) );
  30. }
  31. };
  32. Peer::Peer(const RuntimeEnvironment *renv) :
  33. RR(renv),
  34. _lastReceive(0),
  35. _lastWhoisRequestReceived(0),
  36. _lastEchoRequestReceived(0),
  37. _lastPushDirectPathsReceived(0),
  38. _lastProbeReceived(0),
  39. _lastAttemptedP2PInit(0),
  40. _lastTriedStaticPath(0),
  41. _lastPrioritizedPaths(0),
  42. _lastAttemptedAggressiveNATTraversal(0),
  43. _latency(0xffff),
  44. _alivePathCount(0),
  45. _vProto(0),
  46. _vMajor(0),
  47. _vMinor(0),
  48. _vRevision(0)
  49. {
  50. }
  51. bool Peer::init(const Identity &peerIdentity)
  52. {
  53. RWMutex::Lock l(_lock);
  54. if (_id == peerIdentity)
  55. return true;
  56. _id = peerIdentity;
  57. if (!RR->identity.agree(peerIdentity,_key))
  58. return false;
  59. _incomingProbe = Protocol::createProbe(_id,RR->identity,_key);
  60. return true;
  61. }
  62. void Peer::received(
  63. void *tPtr,
  64. const SharedPtr<Path> &path,
  65. const unsigned int hops,
  66. const uint64_t packetId,
  67. const unsigned int payloadLength,
  68. const Protocol::Verb verb,
  69. const Protocol::Verb inReVerb)
  70. {
  71. const int64_t now = RR->node->now();
  72. _lastReceive = now;
  73. if (hops == 0) {
  74. _lock.rlock();
  75. for(int i=0;i<(int)_alivePathCount;++i) {
  76. if (_paths[i] == path) {
  77. _lock.runlock();
  78. goto path_check_done;
  79. }
  80. }
  81. _lock.runlock();
  82. if (verb == Protocol::VERB_OK) {
  83. RWMutex::Lock l(_lock);
  84. int64_t lastReceiveTimeMax = 0;
  85. int lastReceiveTimeMaxAt = 0;
  86. for(int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  87. if ((_paths[i]->address().ss_family == path->address().ss_family) &&
  88. (_paths[i]->localSocket() == path->localSocket()) && // TODO: should be localInterface when multipath is integrated
  89. (_paths[i]->address().ipsEqual2(path->address()))) {
  90. // Replace older path if everything is the same except the port number.
  91. _paths[i] = path;
  92. goto path_check_done;
  93. } else {
  94. if (_paths[i]) {
  95. if (_paths[i]->lastIn() > lastReceiveTimeMax) {
  96. lastReceiveTimeMax = _paths[i]->lastIn();
  97. lastReceiveTimeMaxAt = i;
  98. }
  99. } else {
  100. lastReceiveTimeMax = 0x7fffffffffffffffLL;
  101. lastReceiveTimeMaxAt = i;
  102. }
  103. }
  104. }
  105. _lastPrioritizedPaths = now;
  106. InetAddress old;
  107. if (_paths[lastReceiveTimeMaxAt])
  108. old = _paths[lastReceiveTimeMaxAt]->address();
  109. _paths[lastReceiveTimeMaxAt] = path;
  110. _bootstrap = path->address();
  111. _prioritizePaths(now);
  112. RR->t->learnedNewPath(tPtr,0x582fabdd,packetId,_id,path->address(),old);
  113. } else {
  114. if (RR->node->shouldUsePathForZeroTierTraffic(tPtr,_id,path->localSocket(),path->address())) {
  115. RR->t->tryingNewPath(tPtr,0xb7747ddd,_id,path->address(),path->address(),packetId,(uint8_t)verb,_id.address(),_id.hash(),ZT_TRACE_TRYING_NEW_PATH_REASON_PACKET_RECEIVED_FROM_UNKNOWN_PATH);
  116. sendHELLO(tPtr,path->localSocket(),path->address(),now);
  117. path->sent(now);
  118. }
  119. }
  120. }
  121. path_check_done:
  122. if ((now - _lastAttemptedP2PInit) >= ((hops == 0) ? ZT_DIRECT_PATH_PUSH_INTERVAL_HAVEPATH : ZT_DIRECT_PATH_PUSH_INTERVAL)) {
  123. _lastAttemptedP2PInit = now;
  124. InetAddress addr;
  125. if ((_bootstrap.type() == Endpoint::INETADDR_V4)||(_bootstrap.type() == Endpoint::INETADDR_V6)) {
  126. RR->t->tryingNewPath(tPtr,0x0a009444,_id,_bootstrap.inetAddr(),InetAddress::NIL,0,0,0,nullptr,ZT_TRACE_TRYING_NEW_PATH_REASON_BOOTSTRAP_ADDRESS);
  127. sendHELLO(tPtr,-1,_bootstrap.inetAddr(),now);
  128. } if (RR->node->externalPathLookup(tPtr,_id,-1,addr)) {
  129. if (RR->node->shouldUsePathForZeroTierTraffic(tPtr,_id,-1,addr)) {
  130. RR->t->tryingNewPath(tPtr,0x84a10000,_id,_bootstrap.inetAddr(),InetAddress::NIL,0,0,0,nullptr,ZT_TRACE_TRYING_NEW_PATH_REASON_EXPLICITLY_SUGGESTED_ADDRESS);
  131. sendHELLO(tPtr,-1,addr,now);
  132. }
  133. }
  134. std::vector<ZT_InterfaceAddress> localInterfaceAddresses(RR->node->localInterfaceAddresses());
  135. std::multimap<unsigned long,InetAddress> detectedAddresses(RR->sa->externalAddresses(now));
  136. std::set<InetAddress> addrs;
  137. for(std::vector<ZT_InterfaceAddress>::const_iterator i(localInterfaceAddresses.begin());i!=localInterfaceAddresses.end();++i)
  138. addrs.insert(asInetAddress(i->address));
  139. for(std::multimap<unsigned long,InetAddress>::const_reverse_iterator i(detectedAddresses.rbegin());i!=detectedAddresses.rend();++i) {
  140. if (i->first <= 1)
  141. break;
  142. if (addrs.count(i->second) == 0) {
  143. addrs.insert(i->second);
  144. break;
  145. }
  146. }
  147. if (!addrs.empty()) {
  148. #if 0
  149. ScopedPtr<Packet> outp(new Packet(_id.address(),RR->identity.address(),Packet::VERB_PUSH_DIRECT_PATHS));
  150. outp->addSize(2); // leave room for count
  151. unsigned int count = 0;
  152. for(std::set<InetAddress>::iterator a(addrs.begin());a!=addrs.end();++a) {
  153. uint8_t addressType = 4;
  154. uint8_t addressLength = 6;
  155. unsigned int ipLength = 4;
  156. const void *rawIpData = nullptr;
  157. uint16_t port = 0;
  158. switch(a->ss_family) {
  159. case AF_INET:
  160. rawIpData = &(reinterpret_cast<const sockaddr_in *>(&(*a))->sin_addr.s_addr);
  161. port = Utils::ntoh((uint16_t)reinterpret_cast<const sockaddr_in *>(&(*a))->sin_port);
  162. break;
  163. case AF_INET6:
  164. rawIpData = reinterpret_cast<const sockaddr_in6 *>(&(*a))->sin6_addr.s6_addr;
  165. port = Utils::ntoh((uint16_t)reinterpret_cast<const sockaddr_in6 *>(&(*a))->sin6_port);
  166. addressType = 6;
  167. addressLength = 18;
  168. ipLength = 16;
  169. break;
  170. default:
  171. continue;
  172. }
  173. outp->append((uint8_t)0); // no flags
  174. outp->append((uint16_t)0); // no extensions
  175. outp->append(addressType);
  176. outp->append(addressLength);
  177. outp->append(rawIpData,ipLength);
  178. outp->append(port);
  179. ++count;
  180. if (outp->size() >= (ZT_PROTO_MAX_PACKET_LENGTH - 32))
  181. break;
  182. }
  183. if (count > 0) {
  184. outp->setAt(ZT_PACKET_IDX_PAYLOAD,(uint16_t)count);
  185. outp->compress();
  186. outp->armor(_key,true);
  187. path->send(RR,tPtr,outp->data(),outp->size(),now);
  188. }
  189. #endif
  190. }
  191. }
  192. }
  193. void Peer::sendHELLO(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now)
  194. {
  195. #if 0
  196. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_HELLO);
  197. outp.append((unsigned char)ZT_PROTO_VERSION);
  198. outp.append((unsigned char)ZEROTIER_ONE_VERSION_MAJOR);
  199. outp.append((unsigned char)ZEROTIER_ONE_VERSION_MINOR);
  200. outp.append((uint16_t)ZEROTIER_ONE_VERSION_REVISION);
  201. outp.append(now);
  202. RR->identity.serialize(outp,false);
  203. atAddress.serialize(outp);
  204. RR->node->expectReplyTo(outp.packetId());
  205. if (atAddress) {
  206. outp.armor(_key,false); // false == don't encrypt full payload, but add MAC
  207. RR->node->putPacket(tPtr,localSocket,atAddress,outp.data(),outp.size());
  208. } else {
  209. RR->sw->send(tPtr,outp,false); // false == don't encrypt full payload, but add MAC
  210. }
  211. #endif
  212. }
  213. void Peer::sendNOP(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now)
  214. {
  215. Buf outp;
  216. Protocol::Header &ph = outp.as<Protocol::Header>();
  217. ph.packetId = Protocol::getPacketId();
  218. _id.address().copyTo(ph.destination);
  219. RR->identity.address().copyTo(ph.source);
  220. ph.flags = 0;
  221. ph.verb = Protocol::VERB_NOP;
  222. Protocol::armor(outp,sizeof(Protocol::Header),_key,this->cipher());
  223. RR->node->putPacket(tPtr,localSocket,atAddress,outp.b,sizeof(Protocol::Header));
  224. }
  225. void Peer::ping(void *tPtr,int64_t now,const bool pingAllAddressTypes)
  226. {
  227. RWMutex::RLock l(_lock);
  228. _lastPrioritizedPaths = now;
  229. _prioritizePaths(now);
  230. if (_alivePathCount > 0) {
  231. for (unsigned int i = 0; i < _alivePathCount; ++i) {
  232. sendHELLO(tPtr,_paths[i]->localSocket(),_paths[i]->address(),now);
  233. _paths[i]->sent(now);
  234. if (!pingAllAddressTypes)
  235. return;
  236. }
  237. return;
  238. }
  239. if ((_bootstrap.type() == Endpoint::INETADDR_V4)||(_bootstrap.type() == Endpoint::INETADDR_V6))
  240. sendHELLO(tPtr,-1,_bootstrap.inetAddr(),now);
  241. SharedPtr<Peer> r(RR->topology->root());
  242. if ((r)&&(r.ptr() != this)) {
  243. SharedPtr<Path> rp(r->path(now));
  244. if (rp) {
  245. sendHELLO(tPtr,rp->localSocket(),rp->address(),now);
  246. rp->sent(now);
  247. return;
  248. }
  249. }
  250. }
  251. void Peer::resetWithinScope(void *tPtr,InetAddress::IpScope scope,int inetAddressFamily,int64_t now)
  252. {
  253. RWMutex::RLock l(_lock);
  254. for(unsigned int i=0; i < _alivePathCount; ++i) {
  255. if ((_paths[i])&&((_paths[i]->address().ss_family == inetAddressFamily)&&(_paths[i]->address().ipScope() == scope))) {
  256. sendHELLO(tPtr,_paths[i]->localSocket(),_paths[i]->address(),now);
  257. _paths[i]->sent(now);
  258. }
  259. }
  260. }
  261. void Peer::updateLatency(const unsigned int l) noexcept
  262. {
  263. if ((l > 0)&&(l < 0xffff)) {
  264. unsigned int lat = _latency;
  265. if (lat < 0xffff) {
  266. _latency = (l + l + lat) / 3;
  267. } else {
  268. _latency = l;
  269. }
  270. }
  271. }
  272. SharedPtr<Path> Peer::path(const int64_t now)
  273. {
  274. if ((now - _lastPrioritizedPaths) > ZT_PEER_PRIORITIZE_PATHS_INTERVAL) {
  275. _lastPrioritizedPaths = now;
  276. RWMutex::Lock l(_lock);
  277. _prioritizePaths(now);
  278. if (_alivePathCount == 0)
  279. return SharedPtr<Path>();
  280. return _paths[0];
  281. } else {
  282. RWMutex::RLock l(_lock);
  283. if (_alivePathCount == 0)
  284. return SharedPtr<Path>();
  285. return _paths[0];
  286. }
  287. }
  288. bool Peer::direct(const int64_t now)
  289. {
  290. if ((now - _lastPrioritizedPaths) > ZT_PEER_PRIORITIZE_PATHS_INTERVAL) {
  291. _lastPrioritizedPaths = now;
  292. RWMutex::Lock l(_lock);
  293. _prioritizePaths(now);
  294. return (_alivePathCount > 0);
  295. } else {
  296. RWMutex::RLock l(_lock);
  297. return (_alivePathCount > 0);
  298. }
  299. }
  300. void Peer::getAllPaths(std::vector< SharedPtr<Path> > &paths)
  301. {
  302. RWMutex::RLock l(_lock);
  303. paths.clear();
  304. paths.assign(_paths,_paths + _alivePathCount);
  305. }
  306. void Peer::save(void *tPtr) const
  307. {
  308. uint8_t *const buf = (uint8_t *)malloc(ZT_PEER_MARSHAL_SIZE_MAX);
  309. if (!buf) return;
  310. _lock.rlock();
  311. const int len = marshal(buf);
  312. _lock.runlock();
  313. if (len > 0) {
  314. uint64_t id[2];
  315. id[0] = _id.address().toInt();
  316. id[1] = 0;
  317. RR->node->stateObjectPut(tPtr,ZT_STATE_OBJECT_PEER,id,buf,(unsigned int)len);
  318. }
  319. free(buf);
  320. }
  321. void Peer::contact(void *tPtr,const Endpoint &ep,const int64_t now,const bool behindSymmetric,const bool bfg1024)
  322. {
  323. static uint8_t junk = 0;
  324. InetAddress phyAddr(ep.inetAddr());
  325. if (phyAddr) { // only this endpoint type is currently implemented
  326. if (!RR->node->shouldUsePathForZeroTierTraffic(tPtr,_id,-1,phyAddr))
  327. return;
  328. // Sending a packet with a low TTL before the real message assists traversal with some
  329. // stateful firewalls and is harmless otherwise AFAIK.
  330. ++junk;
  331. RR->node->putPacket(tPtr,-1,phyAddr,&junk,1,2);
  332. // In a few hundred milliseconds we'll send the real packet.
  333. {
  334. RWMutex::Lock l(_lock);
  335. _contactQueue.push_back(_ContactQueueItem(phyAddr,ZT_MAX_PEER_NETWORK_PATHS));
  336. }
  337. // If the peer indicates that they may be behind a symmetric NAT and there are no
  338. // living direct paths, try a few more aggressive things.
  339. if ((behindSymmetric) && (phyAddr.ss_family == AF_INET) && (!direct(now))) {
  340. unsigned int port = phyAddr.port();
  341. if ((bfg1024)&&(port < 1024)&&(RR->node->natMustDie())) {
  342. // If the other side is using a low-numbered port and has elected to
  343. // have this done, we can try scanning every port below 1024. The search
  344. // space here is small enough that we have a very good chance of punching.
  345. // Generate a random order list of all <1024 ports except 0 and the original sending port.
  346. uint16_t ports[1022];
  347. uint16_t ctr = 1;
  348. for (int i=0;i<1022;++i) {
  349. if (ctr == port) ++ctr;
  350. ports[i] = ctr++;
  351. }
  352. for (int i=0;i<512;++i) {
  353. uint64_t rn = Utils::random();
  354. unsigned int a = ((unsigned int)rn) % 1022;
  355. unsigned int b = ((unsigned int)(rn >> 24U)) % 1022;
  356. if (a != b) {
  357. uint16_t tmp = ports[a];
  358. ports[a] = ports[b];
  359. ports[b] = tmp;
  360. }
  361. }
  362. // Chunk ports into chunks of 128 to try in few hundred millisecond intervals,
  363. // abandoning attempts once there is at least one direct path.
  364. {
  365. RWMutex::Lock l(_lock);
  366. for (int i=0;i<896;i+=128)
  367. _contactQueue.push_back(_ContactQueueItem(phyAddr,ports + i,ports + i + 128,1));
  368. _contactQueue.push_back(_ContactQueueItem(phyAddr,ports + 896,ports + 1022,1));
  369. }
  370. } else {
  371. // Otherwise use the simpler sequential port attempt method in intervals.
  372. RWMutex::Lock l(_lock);
  373. for (int k=0;k<3;++k) {
  374. if (++port > 65535) break;
  375. InetAddress tryNext(phyAddr);
  376. tryNext.setPort(port);
  377. _contactQueue.push_back(_ContactQueueItem(tryNext,1));
  378. }
  379. }
  380. }
  381. // Start alarms going off to actually send these...
  382. RR->node->setPeerAlarm(_id.address(),now + ZT_NAT_TRAVERSAL_INTERVAL);
  383. }
  384. }
  385. void Peer::alarm(void *tPtr,const int64_t now)
  386. {
  387. // Pop one contact queue item and also clean the queue of any that are no
  388. // longer applicable because the alive path count has exceeded their threshold.
  389. bool stillHaveContactQueueItems;
  390. _ContactQueueItem qi;
  391. {
  392. RWMutex::Lock l(_lock);
  393. if (_contactQueue.empty())
  394. return;
  395. while (_alivePathCount >= _contactQueue.front().alivePathThreshold) {
  396. _contactQueue.pop_front();
  397. if (_contactQueue.empty())
  398. return;
  399. }
  400. _ContactQueueItem &qi2 = _contactQueue.front();
  401. qi.address = qi2.address;
  402. qi.ports.swap(qi2.ports);
  403. qi.alivePathThreshold = qi2.alivePathThreshold;
  404. _contactQueue.pop_front();
  405. for(std::list<_ContactQueueItem>::iterator q(_contactQueue.begin());q!=_contactQueue.end();) {
  406. if (_alivePathCount >= q->alivePathThreshold)
  407. _contactQueue.erase(q++);
  408. else ++q;
  409. }
  410. stillHaveContactQueueItems = !_contactQueue.empty();
  411. }
  412. if (_vProto >= 11) {
  413. uint64_t outgoingProbe = Protocol::createProbe(RR->identity,_id,_key);
  414. if (qi.ports.empty()) {
  415. RR->node->putPacket(tPtr,-1,qi.address,&outgoingProbe,ZT_PROTO_PROBE_LENGTH);
  416. } else {
  417. for (std::vector<uint16_t>::iterator p(qi.ports.begin()); p != qi.ports.end(); ++p) {
  418. qi.address.setPort(*p);
  419. RR->node->putPacket(tPtr,-1,qi.address,&outgoingProbe,ZT_PROTO_PROBE_LENGTH);
  420. }
  421. }
  422. } else {
  423. if (qi.ports.empty()) {
  424. this->sendNOP(tPtr,-1,qi.address,now);
  425. } else {
  426. for (std::vector<uint16_t>::iterator p(qi.ports.begin()); p != qi.ports.end(); ++p) {
  427. qi.address.setPort(*p);
  428. this->sendNOP(tPtr,-1,qi.address,now);
  429. }
  430. }
  431. }
  432. if (stillHaveContactQueueItems)
  433. RR->node->setPeerAlarm(_id.address(),now + ZT_NAT_TRAVERSAL_INTERVAL);
  434. }
  435. int Peer::marshal(uint8_t data[ZT_PEER_MARSHAL_SIZE_MAX]) const noexcept
  436. {
  437. RWMutex::RLock l(_lock);
  438. data[0] = 0; // serialized peer version
  439. int s = _id.marshal(data + 1,false);
  440. if (s <= 0)
  441. return s;
  442. int p = 1 + s;
  443. s = _locator.marshal(data + p);
  444. if (s <= 0)
  445. return s;
  446. p += s;
  447. s = _bootstrap.marshal(data + p);
  448. if (s <= 0)
  449. return s;
  450. p += s;
  451. Utils::storeBigEndian(data + p,(uint16_t)_vProto);
  452. p += 2;
  453. Utils::storeBigEndian(data + p,(uint16_t)_vMajor);
  454. p += 2;
  455. Utils::storeBigEndian(data + p,(uint16_t)_vMinor);
  456. p += 2;
  457. Utils::storeBigEndian(data + p,(uint16_t)_vRevision);
  458. p += 2;
  459. data[p++] = 0;
  460. data[p++] = 0;
  461. return p;
  462. }
  463. int Peer::unmarshal(const uint8_t *restrict data,const int len) noexcept
  464. {
  465. int p;
  466. {
  467. RWMutex::Lock l(_lock);
  468. if ((len <= 1) || (data[0] != 0))
  469. return -1;
  470. int s = _id.unmarshal(data + 1,len - 1);
  471. if (s <= 0)
  472. return s;
  473. p = 1 + s;
  474. s = _locator.unmarshal(data + p,len - p);
  475. if (s <= 0)
  476. return s;
  477. p += s;
  478. s = _bootstrap.unmarshal(data + p,len - p);
  479. if (s <= 0)
  480. return s;
  481. p += s;
  482. if ((p + 10) > len)
  483. return -1;
  484. _vProto = Utils::loadBigEndian<uint16_t>(data + p);
  485. p += 2;
  486. _vMajor = Utils::loadBigEndian<uint16_t>(data + p);
  487. p += 2;
  488. _vMinor = Utils::loadBigEndian<uint16_t>(data + p);
  489. p += 2;
  490. _vRevision = Utils::loadBigEndian<uint16_t>(data + p);
  491. p += 2;
  492. p += 2 + (int)Utils::loadBigEndian<uint16_t>(data + p);
  493. if (p > len)
  494. return -1;
  495. }
  496. if (!RR->identity.agree(_id,_key))
  497. return -1;
  498. _incomingProbe = Protocol::createProbe(_id,RR->identity,_key);
  499. return p;
  500. }
  501. void Peer::_prioritizePaths(const int64_t now)
  502. {
  503. // assumes _lock is locked for writing
  504. std::sort(_paths,_paths + ZT_MAX_PEER_NETWORK_PATHS,_PathPriorityComparisonOperator());
  505. for(int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  506. if ((!_paths[i]) || (!_paths[i]->alive(now))) {
  507. _alivePathCount = i;
  508. for(;i<ZT_MAX_PEER_NETWORK_PATHS;++i)
  509. _paths[i].zero();
  510. return;
  511. }
  512. }
  513. }
  514. } // namespace ZeroTier