Peer.cpp 17 KB

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