Peer.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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 "Endpoint.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().family() == path->address().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 = Endpoint(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().data(),ZT_TRACE_TRYING_NEW_PATH_REASON_PACKET_RECEIVED_FROM_UNKNOWN_PATH);
  116. path->sent(now,sendHELLO(tPtr,path->localSocket(),path->address(),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::TYPE_INETADDR_V4)||(_bootstrap.type() == Endpoint::TYPE_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. unsigned int 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. _paths[i]->sent(now,sendHELLO(tPtr,_paths[i]->localSocket(),_paths[i]->address(),now));
  232. if (!pingAllAddressTypes)
  233. return;
  234. }
  235. return;
  236. }
  237. if ((_bootstrap.type() == Endpoint::TYPE_INETADDR_V4)||(_bootstrap.type() == Endpoint::TYPE_INETADDR_V6))
  238. sendHELLO(tPtr,-1,_bootstrap.inetAddr(),now);
  239. SharedPtr<Peer> r(RR->topology->root());
  240. if ((r)&&(r.ptr() != this)) {
  241. SharedPtr<Path> rp(r->path(now));
  242. if (rp) {
  243. rp->sent(now,sendHELLO(tPtr,rp->localSocket(),rp->address(),now));
  244. return;
  245. }
  246. }
  247. }
  248. void Peer::resetWithinScope(void *tPtr,InetAddress::IpScope scope,int inetAddressFamily,int64_t now)
  249. {
  250. RWMutex::RLock l(_lock);
  251. for(unsigned int i=0; i < _alivePathCount; ++i) {
  252. if ((_paths[i])&&((_paths[i]->address().family() == inetAddressFamily)&&(_paths[i]->address().ipScope() == scope))) {
  253. _paths[i]->sent(now,sendHELLO(tPtr,_paths[i]->localSocket(),_paths[i]->address(),now));
  254. }
  255. }
  256. }
  257. void Peer::updateLatency(const unsigned int l) noexcept
  258. {
  259. if ((l > 0)&&(l < 0xffff)) {
  260. unsigned int lat = _latency;
  261. if (lat < 0xffff) {
  262. _latency = (l + l + lat) / 3;
  263. } else {
  264. _latency = l;
  265. }
  266. }
  267. }
  268. SharedPtr<Path> Peer::path(const int64_t now)
  269. {
  270. if ((now - _lastPrioritizedPaths) > ZT_PEER_PRIORITIZE_PATHS_INTERVAL) {
  271. _lastPrioritizedPaths = now;
  272. RWMutex::Lock l(_lock);
  273. _prioritizePaths(now);
  274. if (_alivePathCount == 0)
  275. return SharedPtr<Path>();
  276. return _paths[0];
  277. } else {
  278. RWMutex::RLock l(_lock);
  279. if (_alivePathCount == 0)
  280. return SharedPtr<Path>();
  281. return _paths[0];
  282. }
  283. }
  284. bool Peer::direct(const int64_t now)
  285. {
  286. if ((now - _lastPrioritizedPaths) > ZT_PEER_PRIORITIZE_PATHS_INTERVAL) {
  287. _lastPrioritizedPaths = now;
  288. RWMutex::Lock l(_lock);
  289. _prioritizePaths(now);
  290. return (_alivePathCount > 0);
  291. } else {
  292. RWMutex::RLock l(_lock);
  293. return (_alivePathCount > 0);
  294. }
  295. }
  296. void Peer::getAllPaths(std::vector< SharedPtr<Path> > &paths)
  297. {
  298. RWMutex::RLock l(_lock);
  299. paths.clear();
  300. paths.assign(_paths,_paths + _alivePathCount);
  301. }
  302. void Peer::save(void *tPtr) const
  303. {
  304. uint8_t *const buf = (uint8_t *)malloc(8 + ZT_PEER_MARSHAL_SIZE_MAX);
  305. if (!buf) return;
  306. Utils::storeBigEndian<uint64_t>(buf,(uint64_t)RR->node->now());
  307. _lock.rlock();
  308. const int len = marshal(buf + 8);
  309. _lock.runlock();
  310. if (len > 0) {
  311. uint64_t id[2];
  312. id[0] = _id.address().toInt();
  313. id[1] = 0;
  314. RR->node->stateObjectPut(tPtr,ZT_STATE_OBJECT_PEER,id,buf,(unsigned int)len + 8);
  315. }
  316. free(buf);
  317. }
  318. void Peer::contact(void *tPtr,const Endpoint &ep,const int64_t now,const bool bfg1024)
  319. {
  320. static uint8_t junk = 0;
  321. InetAddress phyAddr(ep.inetAddr());
  322. if (phyAddr) { // only this endpoint type is currently implemented
  323. if (!RR->node->shouldUsePathForZeroTierTraffic(tPtr,_id,-1,phyAddr))
  324. return;
  325. // Sending a packet with a low TTL before the real message assists traversal with some
  326. // stateful firewalls and is harmless otherwise AFAIK.
  327. ++junk;
  328. RR->node->putPacket(tPtr,-1,phyAddr,&junk,1,2);
  329. // In a few hundred milliseconds we'll send the real packet.
  330. {
  331. RWMutex::Lock l(_lock);
  332. _contactQueue.push_back(_ContactQueueItem(phyAddr,ZT_MAX_PEER_NETWORK_PATHS));
  333. }
  334. // If the peer indicates that they may be behind a symmetric NAT and there are no
  335. // living direct paths, try a few more aggressive things.
  336. if ((phyAddr.ss_family == AF_INET) && (!direct(now))) {
  337. unsigned int port = phyAddr.port();
  338. if ((bfg1024)&&(port < 1024)&&(RR->node->natMustDie())) {
  339. // If the other side is using a low-numbered port and has elected to
  340. // have this done, we can try scanning every port below 1024. The search
  341. // space here is small enough that we have a very good chance of punching.
  342. // Generate a random order list of all <1024 ports except 0 and the original sending port.
  343. uint16_t ports[1022];
  344. uint16_t ctr = 1;
  345. for (int i=0;i<1022;++i) {
  346. if (ctr == port) ++ctr;
  347. ports[i] = ctr++;
  348. }
  349. for (int i=0;i<512;++i) {
  350. uint64_t rn = Utils::random();
  351. unsigned int a = ((unsigned int)rn) % 1022;
  352. unsigned int b = ((unsigned int)(rn >> 24U)) % 1022;
  353. if (a != b) {
  354. uint16_t tmp = ports[a];
  355. ports[a] = ports[b];
  356. ports[b] = tmp;
  357. }
  358. }
  359. // Chunk ports into chunks of 128 to try in few hundred millisecond intervals,
  360. // abandoning attempts once there is at least one direct path.
  361. {
  362. RWMutex::Lock l(_lock);
  363. for (int i=0;i<896;i+=128)
  364. _contactQueue.push_back(_ContactQueueItem(phyAddr,ports + i,ports + i + 128,1));
  365. _contactQueue.push_back(_ContactQueueItem(phyAddr,ports + 896,ports + 1022,1));
  366. }
  367. } else {
  368. // Otherwise use the simpler sequential port attempt method in intervals.
  369. RWMutex::Lock l(_lock);
  370. for (int k=0;k<3;++k) {
  371. if (++port > 65535) break;
  372. InetAddress tryNext(phyAddr);
  373. tryNext.setPort(port);
  374. _contactQueue.push_back(_ContactQueueItem(tryNext,1));
  375. }
  376. }
  377. }
  378. // Start alarms going off to actually send these...
  379. RR->node->setPeerAlarm(_id.address(),now + ZT_NAT_TRAVERSAL_INTERVAL);
  380. }
  381. }
  382. void Peer::alarm(void *tPtr,const int64_t now)
  383. {
  384. // Pop one contact queue item and also clean the queue of any that are no
  385. // longer applicable because the alive path count has exceeded their threshold.
  386. bool stillHaveContactQueueItems;
  387. _ContactQueueItem qi;
  388. {
  389. RWMutex::Lock l(_lock);
  390. if (_contactQueue.empty())
  391. return;
  392. while (_alivePathCount >= _contactQueue.front().alivePathThreshold) {
  393. _contactQueue.pop_front();
  394. if (_contactQueue.empty())
  395. return;
  396. }
  397. _ContactQueueItem &qi2 = _contactQueue.front();
  398. qi.address = qi2.address;
  399. qi.ports.swap(qi2.ports);
  400. qi.alivePathThreshold = qi2.alivePathThreshold;
  401. _contactQueue.pop_front();
  402. for(std::list<_ContactQueueItem>::iterator q(_contactQueue.begin());q!=_contactQueue.end();) {
  403. if (_alivePathCount >= q->alivePathThreshold)
  404. _contactQueue.erase(q++);
  405. else ++q;
  406. }
  407. stillHaveContactQueueItems = !_contactQueue.empty();
  408. }
  409. if (_vProto >= 11) {
  410. uint64_t outgoingProbe = Protocol::createProbe(RR->identity,_id,_key);
  411. if (qi.ports.empty()) {
  412. RR->node->putPacket(tPtr,-1,qi.address,&outgoingProbe,ZT_PROTO_PROBE_LENGTH);
  413. } else {
  414. for (std::vector<uint16_t>::iterator p(qi.ports.begin()); p != qi.ports.end(); ++p) {
  415. qi.address.setPort(*p);
  416. RR->node->putPacket(tPtr,-1,qi.address,&outgoingProbe,ZT_PROTO_PROBE_LENGTH);
  417. }
  418. }
  419. } else {
  420. if (qi.ports.empty()) {
  421. this->sendNOP(tPtr,-1,qi.address,now);
  422. } else {
  423. for (std::vector<uint16_t>::iterator p(qi.ports.begin()); p != qi.ports.end(); ++p) {
  424. qi.address.setPort(*p);
  425. this->sendNOP(tPtr,-1,qi.address,now);
  426. }
  427. }
  428. }
  429. if (stillHaveContactQueueItems)
  430. RR->node->setPeerAlarm(_id.address(),now + ZT_NAT_TRAVERSAL_INTERVAL);
  431. }
  432. int Peer::marshal(uint8_t data[ZT_PEER_MARSHAL_SIZE_MAX]) const noexcept
  433. {
  434. RWMutex::RLock l(_lock);
  435. data[0] = 0; // serialized peer version
  436. int s = _id.marshal(data + 1,false);
  437. if (s <= 0)
  438. return s;
  439. int p = 1 + s;
  440. s = _locator.marshal(data + p);
  441. if (s <= 0)
  442. return s;
  443. p += s;
  444. s = _bootstrap.marshal(data + p);
  445. if (s <= 0)
  446. return s;
  447. p += s;
  448. Utils::storeBigEndian(data + p,(uint16_t)_vProto);
  449. p += 2;
  450. Utils::storeBigEndian(data + p,(uint16_t)_vMajor);
  451. p += 2;
  452. Utils::storeBigEndian(data + p,(uint16_t)_vMinor);
  453. p += 2;
  454. Utils::storeBigEndian(data + p,(uint16_t)_vRevision);
  455. p += 2;
  456. data[p++] = 0;
  457. data[p++] = 0;
  458. return p;
  459. }
  460. int Peer::unmarshal(const uint8_t *restrict data,const int len) noexcept
  461. {
  462. int p;
  463. {
  464. RWMutex::Lock l(_lock);
  465. if ((len <= 1) || (data[0] != 0))
  466. return -1;
  467. int s = _id.unmarshal(data + 1,len - 1);
  468. if (s <= 0)
  469. return s;
  470. p = 1 + s;
  471. s = _locator.unmarshal(data + p,len - p);
  472. if (s <= 0)
  473. return s;
  474. p += s;
  475. s = _bootstrap.unmarshal(data + p,len - p);
  476. if (s <= 0)
  477. return s;
  478. p += s;
  479. if ((p + 10) > len)
  480. return -1;
  481. _vProto = Utils::loadBigEndian<uint16_t>(data + p);
  482. p += 2;
  483. _vMajor = Utils::loadBigEndian<uint16_t>(data + p);
  484. p += 2;
  485. _vMinor = Utils::loadBigEndian<uint16_t>(data + p);
  486. p += 2;
  487. _vRevision = Utils::loadBigEndian<uint16_t>(data + p);
  488. p += 2;
  489. p += 2 + (int)Utils::loadBigEndian<uint16_t>(data + p);
  490. if (p > len)
  491. return -1;
  492. }
  493. if (!RR->identity.agree(_id,_key))
  494. return -1;
  495. _incomingProbe = Protocol::createProbe(_id,RR->identity,_key);
  496. return p;
  497. }
  498. void Peer::_prioritizePaths(const int64_t now)
  499. {
  500. // assumes _lock is locked for writing
  501. std::sort(_paths,_paths + ZT_MAX_PEER_NETWORK_PATHS,_PathPriorityComparisonOperator());
  502. for(int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  503. if ((!_paths[i]) || (!_paths[i]->alive(now))) {
  504. _alivePathCount = i;
  505. for(;i<ZT_MAX_PEER_NETWORK_PATHS;++i)
  506. _paths[i].zero();
  507. return;
  508. }
  509. }
  510. }
  511. } // namespace ZeroTier