Peer.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 "SelfAwareness.hpp"
  20. #include "InetAddress.hpp"
  21. #include "Protocol.hpp"
  22. #include <set>
  23. namespace ZeroTier {
  24. struct _PathPriorityComparisonOperator
  25. {
  26. ZT_ALWAYS_INLINE bool operator()(const SharedPtr<Path> &a,const SharedPtr<Path> &b) const
  27. {
  28. return ( ((a)&&(a->lastIn() > 0)) && ((!b)||(b->lastIn() <= 0)||(a->lastIn() < b->lastIn())) );
  29. }
  30. };
  31. Peer::Peer(const RuntimeEnvironment *renv) :
  32. RR(renv),
  33. _lastReceive(0),
  34. _lastWhoisRequestReceived(0),
  35. _lastEchoRequestReceived(0),
  36. _lastPushDirectPathsReceived(0),
  37. _lastProbeReceived(0),
  38. _lastAttemptedP2PInit(0),
  39. _lastTriedStaticPath(0),
  40. _lastPrioritizedPaths(0),
  41. _lastAttemptedAggressiveNATTraversal(0),
  42. _latency(0xffff),
  43. _alivePathCount(0),
  44. _vProto(0),
  45. _vMajor(0),
  46. _vMinor(0),
  47. _vRevision(0)
  48. {
  49. }
  50. bool Peer::init(const Identity &peerIdentity)
  51. {
  52. RWMutex::Lock l(_lock);
  53. if (_id == peerIdentity)
  54. return true;
  55. _id = peerIdentity;
  56. if (!RR->identity.agree(peerIdentity,_key))
  57. return false;
  58. _incomingProbe = Protocol::createProbe(_id,RR->identity,_key);
  59. return true;
  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 Protocol::Verb verb,
  68. const Protocol::Verb inReVerb)
  69. {
  70. const int64_t now = RR->node->now();
  71. _lastReceive = now;
  72. if (hops == 0) {
  73. _lock.rlock();
  74. for(int i=0;i<(int)_alivePathCount;++i) {
  75. if (_paths[i] == path) {
  76. _lock.runlock();
  77. goto path_check_done;
  78. }
  79. }
  80. _lock.runlock();
  81. if (verb == Protocol::VERB_OK) {
  82. RWMutex::Lock l(_lock);
  83. int64_t lastReceiveTimeMax = 0;
  84. int lastReceiveTimeMaxAt = 0;
  85. for(int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  86. if ((_paths[i]->address().ss_family == path->address().ss_family) &&
  87. (_paths[i]->localSocket() == path->localSocket()) && // TODO: should be localInterface when multipath is integrated
  88. (_paths[i]->address().ipsEqual2(path->address()))) {
  89. // Replace older path if everything is the same except the port number.
  90. _paths[i] = path;
  91. goto path_check_done;
  92. } else {
  93. if (_paths[i]) {
  94. if (_paths[i]->lastIn() > lastReceiveTimeMax) {
  95. lastReceiveTimeMax = _paths[i]->lastIn();
  96. lastReceiveTimeMaxAt = i;
  97. }
  98. } else {
  99. lastReceiveTimeMax = 0x7fffffffffffffffLL;
  100. lastReceiveTimeMaxAt = i;
  101. }
  102. }
  103. }
  104. _lastPrioritizedPaths = now;
  105. InetAddress old;
  106. if (_paths[lastReceiveTimeMaxAt])
  107. old = _paths[lastReceiveTimeMaxAt]->address();
  108. _paths[lastReceiveTimeMaxAt] = path;
  109. _bootstrap = path->address();
  110. _prioritizePaths(now);
  111. RR->t->learnedNewPath(tPtr,0x582fabdd,packetId,_id,path->address(),old);
  112. } else {
  113. if (RR->node->shouldUsePathForZeroTierTraffic(tPtr,_id,path->localSocket(),path->address())) {
  114. RR->t->tryingNewPath(tPtr,0xb7747ddd,_id,path->address(),path->address(),packetId,(uint8_t)verb,_id.address(),_id.hash().data(),ZT_TRACE_TRYING_NEW_PATH_REASON_PACKET_RECEIVED_FROM_UNKNOWN_PATH);
  115. sendHELLO(tPtr,path->localSocket(),path->address(),now);
  116. path->sent(now);
  117. }
  118. }
  119. }
  120. path_check_done:
  121. if ((now - _lastAttemptedP2PInit) >= ((hops == 0) ? ZT_DIRECT_PATH_PUSH_INTERVAL_HAVEPATH : ZT_DIRECT_PATH_PUSH_INTERVAL)) {
  122. _lastAttemptedP2PInit = now;
  123. InetAddress addr;
  124. if ((_bootstrap.type() == Endpoint::INETADDR_V4)||(_bootstrap.type() == Endpoint::INETADDR_V6)) {
  125. RR->t->tryingNewPath(tPtr,0x0a009444,_id,_bootstrap.inetAddr(),InetAddress::NIL,0,0,0,nullptr,ZT_TRACE_TRYING_NEW_PATH_REASON_BOOTSTRAP_ADDRESS);
  126. sendHELLO(tPtr,-1,_bootstrap.inetAddr(),now);
  127. } if (RR->node->externalPathLookup(tPtr,_id,-1,addr)) {
  128. if (RR->node->shouldUsePathForZeroTierTraffic(tPtr,_id,-1,addr)) {
  129. RR->t->tryingNewPath(tPtr,0x84a10000,_id,_bootstrap.inetAddr(),InetAddress::NIL,0,0,0,nullptr,ZT_TRACE_TRYING_NEW_PATH_REASON_EXPLICITLY_SUGGESTED_ADDRESS);
  130. sendHELLO(tPtr,-1,addr,now);
  131. }
  132. }
  133. std::vector<ZT_InterfaceAddress> localInterfaceAddresses(RR->node->localInterfaceAddresses());
  134. std::multimap<unsigned long,InetAddress> detectedAddresses(RR->sa->externalAddresses(now));
  135. std::set<InetAddress> addrs;
  136. for(std::vector<ZT_InterfaceAddress>::const_iterator i(localInterfaceAddresses.begin());i!=localInterfaceAddresses.end();++i)
  137. addrs.insert(asInetAddress(i->address));
  138. for(std::multimap<unsigned long,InetAddress>::const_reverse_iterator i(detectedAddresses.rbegin());i!=detectedAddresses.rend();++i) {
  139. if (i->first <= 1)
  140. break;
  141. if (addrs.count(i->second) == 0) {
  142. addrs.insert(i->second);
  143. break;
  144. }
  145. }
  146. if (!addrs.empty()) {
  147. #if 0
  148. ScopedPtr<Packet> outp(new Packet(_id.address(),RR->identity.address(),Packet::VERB_PUSH_DIRECT_PATHS));
  149. outp->addSize(2); // leave room for count
  150. unsigned int count = 0;
  151. for(std::set<InetAddress>::iterator a(addrs.begin());a!=addrs.end();++a) {
  152. uint8_t addressType = 4;
  153. uint8_t addressLength = 6;
  154. unsigned int ipLength = 4;
  155. const void *rawIpData = nullptr;
  156. uint16_t port = 0;
  157. switch(a->ss_family) {
  158. case AF_INET:
  159. rawIpData = &(reinterpret_cast<const sockaddr_in *>(&(*a))->sin_addr.s_addr);
  160. port = Utils::ntoh((uint16_t)reinterpret_cast<const sockaddr_in *>(&(*a))->sin_port);
  161. break;
  162. case AF_INET6:
  163. rawIpData = reinterpret_cast<const sockaddr_in6 *>(&(*a))->sin6_addr.s6_addr;
  164. port = Utils::ntoh((uint16_t)reinterpret_cast<const sockaddr_in6 *>(&(*a))->sin6_port);
  165. addressType = 6;
  166. addressLength = 18;
  167. ipLength = 16;
  168. break;
  169. default:
  170. continue;
  171. }
  172. outp->append((uint8_t)0); // no flags
  173. outp->append((uint16_t)0); // no extensions
  174. outp->append(addressType);
  175. outp->append(addressLength);
  176. outp->append(rawIpData,ipLength);
  177. outp->append(port);
  178. ++count;
  179. if (outp->size() >= (ZT_PROTO_MAX_PACKET_LENGTH - 32))
  180. break;
  181. }
  182. if (count > 0) {
  183. outp->setAt(ZT_PACKET_IDX_PAYLOAD,(uint16_t)count);
  184. outp->compress();
  185. outp->armor(_key,true);
  186. path->send(RR,tPtr,outp->data(),outp->size(),now);
  187. }
  188. #endif
  189. }
  190. }
  191. }
  192. void Peer::sendHELLO(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now)
  193. {
  194. #if 0
  195. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_HELLO);
  196. outp.append((unsigned char)ZT_PROTO_VERSION);
  197. outp.append((unsigned char)ZEROTIER_ONE_VERSION_MAJOR);
  198. outp.append((unsigned char)ZEROTIER_ONE_VERSION_MINOR);
  199. outp.append((uint16_t)ZEROTIER_ONE_VERSION_REVISION);
  200. outp.append(now);
  201. RR->identity.serialize(outp,false);
  202. atAddress.serialize(outp);
  203. RR->node->expectReplyTo(outp.packetId());
  204. if (atAddress) {
  205. outp.armor(_key,false); // false == don't encrypt full payload, but add MAC
  206. RR->node->putPacket(tPtr,localSocket,atAddress,outp.data(),outp.size());
  207. } else {
  208. RR->sw->send(tPtr,outp,false); // false == don't encrypt full payload, but add MAC
  209. }
  210. #endif
  211. }
  212. void Peer::sendNOP(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now)
  213. {
  214. Buf outp;
  215. Protocol::Header &ph = outp.as<Protocol::Header>();
  216. ph.packetId = Protocol::getPacketId();
  217. _id.address().copyTo(ph.destination);
  218. RR->identity.address().copyTo(ph.source);
  219. ph.flags = 0;
  220. ph.verb = Protocol::VERB_NOP;
  221. Protocol::armor(outp,sizeof(Protocol::Header),_key,this->cipher());
  222. RR->node->putPacket(tPtr,localSocket,atAddress,outp.unsafeData,sizeof(Protocol::Header));
  223. }
  224. void Peer::ping(void *tPtr,int64_t now,const bool pingAllAddressTypes)
  225. {
  226. RWMutex::RLock l(_lock);
  227. _lastPrioritizedPaths = now;
  228. _prioritizePaths(now);
  229. if (_alivePathCount > 0) {
  230. for (unsigned int i = 0; i < _alivePathCount; ++i) {
  231. sendHELLO(tPtr,_paths[i]->localSocket(),_paths[i]->address(),now);
  232. _paths[i]->sent(now);
  233. if (!pingAllAddressTypes)
  234. return;
  235. }
  236. return;
  237. }
  238. if ((_bootstrap.type() == Endpoint::INETADDR_V4)||(_bootstrap.type() == Endpoint::INETADDR_V6))
  239. sendHELLO(tPtr,-1,_bootstrap.inetAddr(),now);
  240. SharedPtr<Peer> r(RR->topology->root());
  241. if ((r)&&(r.ptr() != this)) {
  242. SharedPtr<Path> rp(r->path(now));
  243. if (rp) {
  244. sendHELLO(tPtr,rp->localSocket(),rp->address(),now);
  245. rp->sent(now);
  246. return;
  247. }
  248. }
  249. }
  250. void Peer::resetWithinScope(void *tPtr,InetAddress::IpScope scope,int inetAddressFamily,int64_t now)
  251. {
  252. RWMutex::RLock l(_lock);
  253. for(unsigned int i=0; i < _alivePathCount; ++i) {
  254. if ((_paths[i])&&((_paths[i]->address().ss_family == inetAddressFamily)&&(_paths[i]->address().ipScope() == scope))) {
  255. sendHELLO(tPtr,_paths[i]->localSocket(),_paths[i]->address(),now);
  256. _paths[i]->sent(now);
  257. }
  258. }
  259. }
  260. void Peer::updateLatency(const unsigned int l) noexcept
  261. {
  262. if ((l > 0)&&(l < 0xffff)) {
  263. unsigned int lat = _latency;
  264. if (lat < 0xffff) {
  265. _latency = (l + l + lat) / 3;
  266. } else {
  267. _latency = l;
  268. }
  269. }
  270. }
  271. SharedPtr<Path> Peer::path(const int64_t now)
  272. {
  273. if ((now - _lastPrioritizedPaths) > ZT_PEER_PRIORITIZE_PATHS_INTERVAL) {
  274. _lastPrioritizedPaths = now;
  275. RWMutex::Lock l(_lock);
  276. _prioritizePaths(now);
  277. if (_alivePathCount == 0)
  278. return SharedPtr<Path>();
  279. return _paths[0];
  280. } else {
  281. RWMutex::RLock l(_lock);
  282. if (_alivePathCount == 0)
  283. return SharedPtr<Path>();
  284. return _paths[0];
  285. }
  286. }
  287. bool Peer::direct(const int64_t now)
  288. {
  289. if ((now - _lastPrioritizedPaths) > ZT_PEER_PRIORITIZE_PATHS_INTERVAL) {
  290. _lastPrioritizedPaths = now;
  291. RWMutex::Lock l(_lock);
  292. _prioritizePaths(now);
  293. return (_alivePathCount > 0);
  294. } else {
  295. RWMutex::RLock l(_lock);
  296. return (_alivePathCount > 0);
  297. }
  298. }
  299. void Peer::getAllPaths(std::vector< SharedPtr<Path> > &paths)
  300. {
  301. RWMutex::RLock l(_lock);
  302. paths.clear();
  303. paths.assign(_paths,_paths + _alivePathCount);
  304. }
  305. void Peer::save(void *tPtr) const
  306. {
  307. uint8_t *const buf = (uint8_t *)malloc(8 + ZT_PEER_MARSHAL_SIZE_MAX);
  308. if (!buf) return;
  309. Utils::storeBigEndian<uint64_t>(buf,(uint64_t)RR->node->now());
  310. _lock.rlock();
  311. const int len = marshal(buf + 8);
  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 + 8);
  318. }
  319. free(buf);
  320. }
  321. void Peer::contact(void *tPtr,const Endpoint &ep,const int64_t now,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 ((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