Peer.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include "../version.h"
  27. #include "Constants.hpp"
  28. #include "Peer.hpp"
  29. #include "Node.hpp"
  30. #include "Switch.hpp"
  31. #include "Network.hpp"
  32. #include "SelfAwareness.hpp"
  33. #include "Packet.hpp"
  34. #include "Trace.hpp"
  35. #include "InetAddress.hpp"
  36. namespace ZeroTier {
  37. Peer::Peer(const RuntimeEnvironment *renv,const Identity &myIdentity,const Identity &peerIdentity) :
  38. RR(renv),
  39. _lastReceive(0),
  40. _lastNontrivialReceive(0),
  41. _lastTriedMemorizedPath(0),
  42. _lastDirectPathPushSent(0),
  43. _lastDirectPathPushReceive(0),
  44. _lastCredentialRequestSent(0),
  45. _lastWhoisRequestReceived(0),
  46. _lastEchoRequestReceived(0),
  47. _lastComRequestReceived(0),
  48. _lastComRequestSent(0),
  49. _lastCredentialsReceived(0),
  50. _lastTrustEstablishedPacketReceived(0),
  51. _lastSentFullHello(0),
  52. _vProto(0),
  53. _vMajor(0),
  54. _vMinor(0),
  55. _vRevision(0),
  56. _id(peerIdentity),
  57. _directPathPushCutoffCount(0),
  58. _credentialsCutoffCount(0)
  59. {
  60. if (!myIdentity.agree(peerIdentity,_key,ZT_PEER_SECRET_KEY_LENGTH))
  61. throw ZT_EXCEPTION_INVALID_ARGUMENT;
  62. }
  63. void Peer::received(
  64. void *tPtr,
  65. const SharedPtr<Path> &path,
  66. const unsigned int hops,
  67. const uint64_t packetId,
  68. const Packet::Verb verb,
  69. const uint64_t inRePacketId,
  70. const Packet::Verb inReVerb,
  71. const bool trustEstablished,
  72. const uint64_t networkId)
  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: break;
  85. }
  86. if (trustEstablished) {
  87. _lastTrustEstablishedPacketReceived = now;
  88. path->trustedPacketReceived(now);
  89. }
  90. if (_vProto >= 9)
  91. path->updateLinkQuality((unsigned int)(packetId & 7));
  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. if ((!havePath)&&(RR->node->shouldUsePathForZeroTierTraffic(tPtr,_id.address(),path->localSocket(),path->address()))) {
  109. Mutex::Lock _l(_paths_m);
  110. // Paths are redunant if they duplicate an alive path to the same IP or
  111. // with the same local socket and address family.
  112. bool redundant = false;
  113. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  114. if (_paths[i].p) {
  115. 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())) ) ) {
  116. redundant = true;
  117. break;
  118. }
  119. } else break;
  120. }
  121. if (!redundant) {
  122. unsigned int replacePath = ZT_MAX_PEER_NETWORK_PATHS;
  123. int replacePathQuality = 0;
  124. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  125. if (_paths[i].p) {
  126. const int q = _paths[i].p->quality(now);
  127. if (q > replacePathQuality) {
  128. replacePathQuality = q;
  129. replacePath = i;
  130. }
  131. } else {
  132. replacePath = i;
  133. break;
  134. }
  135. }
  136. if (replacePath != ZT_MAX_PEER_NETWORK_PATHS) {
  137. if (verb == Packet::VERB_OK) {
  138. RR->t->peerLearnedNewPath(tPtr,networkId,*this,path,packetId);
  139. _paths[replacePath].lr = now;
  140. _paths[replacePath].p = path;
  141. _paths[replacePath].priority = 1;
  142. } else {
  143. attemptToContact = true;
  144. }
  145. }
  146. }
  147. }
  148. if (attemptToContact) {
  149. attemptToContactAt(tPtr,path->localSocket(),path->address(),now,true,path->nextOutgoingCounter());
  150. path->sent(now);
  151. RR->t->peerConfirmingUnknownPath(tPtr,networkId,*this,path,packetId,verb);
  152. }
  153. }
  154. // If we have a trust relationship periodically push a message enumerating
  155. // all known external addresses for ourselves. We now do this even if we
  156. // have a current path since we'll want to use new ones too.
  157. if (this->trustEstablished(now)) {
  158. if ((now - _lastDirectPathPushSent) >= ZT_DIRECT_PATH_PUSH_INTERVAL) {
  159. _lastDirectPathPushSent = now;
  160. std::vector<InetAddress> pathsToPush;
  161. std::vector<InetAddress> dps(RR->node->directPaths());
  162. for(std::vector<InetAddress>::const_iterator i(dps.begin());i!=dps.end();++i)
  163. pathsToPush.push_back(*i);
  164. // Do symmetric NAT prediction if we are communicating indirectly.
  165. if (hops > 0) {
  166. std::vector<InetAddress> sym(RR->sa->getSymmetricNatPredictions());
  167. for(unsigned long i=0,added=0;i<sym.size();++i) {
  168. InetAddress tmp(sym[(unsigned long)RR->node->prng() % sym.size()]);
  169. if (std::find(pathsToPush.begin(),pathsToPush.end(),tmp) == pathsToPush.end()) {
  170. pathsToPush.push_back(tmp);
  171. if (++added >= ZT_PUSH_DIRECT_PATHS_MAX_PER_SCOPE_AND_FAMILY)
  172. break;
  173. }
  174. }
  175. }
  176. if (pathsToPush.size() > 0) {
  177. std::vector<InetAddress>::const_iterator p(pathsToPush.begin());
  178. while (p != pathsToPush.end()) {
  179. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_PUSH_DIRECT_PATHS);
  180. outp.addSize(2); // leave room for count
  181. unsigned int count = 0;
  182. while ((p != pathsToPush.end())&&((outp.size() + 24) < 1200)) {
  183. uint8_t addressType = 4;
  184. switch(p->ss_family) {
  185. case AF_INET:
  186. break;
  187. case AF_INET6:
  188. addressType = 6;
  189. break;
  190. default: // we currently only push IP addresses
  191. ++p;
  192. continue;
  193. }
  194. outp.append((uint8_t)0); // no flags
  195. outp.append((uint16_t)0); // no extensions
  196. outp.append(addressType);
  197. outp.append((uint8_t)((addressType == 4) ? 6 : 18));
  198. outp.append(p->rawIpData(),((addressType == 4) ? 4 : 16));
  199. outp.append((uint16_t)p->port());
  200. ++count;
  201. ++p;
  202. }
  203. if (count) {
  204. outp.setAt(ZT_PACKET_IDX_PAYLOAD,(uint16_t)count);
  205. outp.armor(_key,true,path->nextOutgoingCounter());
  206. path->send(RR,tPtr,outp.data(),outp.size(),now);
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
  213. SharedPtr<Path> Peer::getBestPath(int64_t now,bool includeExpired) const
  214. {
  215. Mutex::Lock _l(_paths_m);
  216. unsigned int bestPath = ZT_MAX_PEER_NETWORK_PATHS;
  217. long bestPathQuality = 2147483647;
  218. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  219. if (_paths[i].p) {
  220. if ((includeExpired)||((now - _paths[i].lr) < ZT_PEER_PATH_EXPIRATION)) {
  221. const long q = _paths[i].p->quality(now) / _paths[i].priority;
  222. if (q <= bestPathQuality) {
  223. bestPathQuality = q;
  224. bestPath = i;
  225. }
  226. }
  227. } else break;
  228. }
  229. if (bestPath != ZT_MAX_PEER_NETWORK_PATHS)
  230. return _paths[bestPath].p;
  231. return SharedPtr<Path>();
  232. }
  233. void Peer::introduce(void *const tPtr,const int64_t now,const SharedPtr<Peer> &other) const
  234. {
  235. unsigned int myBestV4ByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  236. unsigned int myBestV6ByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  237. long myBestV4QualityByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  238. long myBestV6QualityByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  239. unsigned int theirBestV4ByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  240. unsigned int theirBestV6ByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  241. long theirBestV4QualityByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  242. long theirBestV6QualityByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  243. for(int i=0;i<=ZT_INETADDRESS_MAX_SCOPE;++i) {
  244. myBestV4ByScope[i] = ZT_MAX_PEER_NETWORK_PATHS;
  245. myBestV6ByScope[i] = ZT_MAX_PEER_NETWORK_PATHS;
  246. myBestV4QualityByScope[i] = 2147483647;
  247. myBestV6QualityByScope[i] = 2147483647;
  248. theirBestV4ByScope[i] = ZT_MAX_PEER_NETWORK_PATHS;
  249. theirBestV6ByScope[i] = ZT_MAX_PEER_NETWORK_PATHS;
  250. theirBestV4QualityByScope[i] = 2147483647;
  251. theirBestV6QualityByScope[i] = 2147483647;
  252. }
  253. Mutex::Lock _l1(_paths_m);
  254. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  255. if (_paths[i].p) {
  256. const long q = _paths[i].p->quality(now) / _paths[i].priority;
  257. const unsigned int s = (unsigned int)_paths[i].p->ipScope();
  258. switch(_paths[i].p->address().ss_family) {
  259. case AF_INET:
  260. if (q <= myBestV4QualityByScope[s]) {
  261. myBestV4QualityByScope[s] = q;
  262. myBestV4ByScope[s] = i;
  263. }
  264. break;
  265. case AF_INET6:
  266. if (q <= myBestV6QualityByScope[s]) {
  267. myBestV6QualityByScope[s] = q;
  268. myBestV6ByScope[s] = i;
  269. }
  270. break;
  271. }
  272. } else break;
  273. }
  274. Mutex::Lock _l2(other->_paths_m);
  275. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  276. if (other->_paths[i].p) {
  277. const long q = other->_paths[i].p->quality(now) / other->_paths[i].priority;
  278. const unsigned int s = (unsigned int)other->_paths[i].p->ipScope();
  279. switch(other->_paths[i].p->address().ss_family) {
  280. case AF_INET:
  281. if (q <= theirBestV4QualityByScope[s]) {
  282. theirBestV4QualityByScope[s] = q;
  283. theirBestV4ByScope[s] = i;
  284. }
  285. break;
  286. case AF_INET6:
  287. if (q <= theirBestV6QualityByScope[s]) {
  288. theirBestV6QualityByScope[s] = q;
  289. theirBestV6ByScope[s] = i;
  290. }
  291. break;
  292. }
  293. } else break;
  294. }
  295. unsigned int mine = ZT_MAX_PEER_NETWORK_PATHS;
  296. unsigned int theirs = ZT_MAX_PEER_NETWORK_PATHS;
  297. for(int s=ZT_INETADDRESS_MAX_SCOPE;s>=0;--s) {
  298. if ((myBestV6ByScope[s] != ZT_MAX_PEER_NETWORK_PATHS)&&(theirBestV6ByScope[s] != ZT_MAX_PEER_NETWORK_PATHS)) {
  299. mine = myBestV6ByScope[s];
  300. theirs = theirBestV6ByScope[s];
  301. break;
  302. }
  303. if ((myBestV4ByScope[s] != ZT_MAX_PEER_NETWORK_PATHS)&&(theirBestV4ByScope[s] != ZT_MAX_PEER_NETWORK_PATHS)) {
  304. mine = myBestV4ByScope[s];
  305. theirs = theirBestV4ByScope[s];
  306. break;
  307. }
  308. }
  309. if (mine != ZT_MAX_PEER_NETWORK_PATHS) {
  310. unsigned int alt = (unsigned int)RR->node->prng() & 1; // randomize which hint we send first for black magickal NAT-t reasons
  311. const unsigned int completed = alt + 2;
  312. while (alt != completed) {
  313. if ((alt & 1) == 0) {
  314. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_RENDEZVOUS);
  315. outp.append((uint8_t)0);
  316. other->_id.address().appendTo(outp);
  317. outp.append((uint16_t)other->_paths[theirs].p->address().port());
  318. if (other->_paths[theirs].p->address().ss_family == AF_INET6) {
  319. outp.append((uint8_t)16);
  320. outp.append(other->_paths[theirs].p->address().rawIpData(),16);
  321. } else {
  322. outp.append((uint8_t)4);
  323. outp.append(other->_paths[theirs].p->address().rawIpData(),4);
  324. }
  325. outp.armor(_key,true,_paths[mine].p->nextOutgoingCounter());
  326. _paths[mine].p->send(RR,tPtr,outp.data(),outp.size(),now);
  327. } else {
  328. Packet outp(other->_id.address(),RR->identity.address(),Packet::VERB_RENDEZVOUS);
  329. outp.append((uint8_t)0);
  330. _id.address().appendTo(outp);
  331. outp.append((uint16_t)_paths[mine].p->address().port());
  332. if (_paths[mine].p->address().ss_family == AF_INET6) {
  333. outp.append((uint8_t)16);
  334. outp.append(_paths[mine].p->address().rawIpData(),16);
  335. } else {
  336. outp.append((uint8_t)4);
  337. outp.append(_paths[mine].p->address().rawIpData(),4);
  338. }
  339. outp.armor(other->_key,true,other->_paths[theirs].p->nextOutgoingCounter());
  340. other->_paths[theirs].p->send(RR,tPtr,outp.data(),outp.size(),now);
  341. }
  342. ++alt;
  343. }
  344. }
  345. }
  346. void Peer::sendHELLO(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now,unsigned int counter)
  347. {
  348. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_HELLO);
  349. outp.append((unsigned char)ZT_PROTO_VERSION);
  350. outp.append((unsigned char)ZEROTIER_ONE_VERSION_MAJOR);
  351. outp.append((unsigned char)ZEROTIER_ONE_VERSION_MINOR);
  352. outp.append((uint16_t)ZEROTIER_ONE_VERSION_REVISION);
  353. outp.append(now);
  354. RR->identity.serialize(outp,false);
  355. atAddress.serialize(outp);
  356. outp.append((uint64_t)RR->topology->planetWorldId());
  357. outp.append((uint64_t)RR->topology->planetWorldTimestamp());
  358. const unsigned int startCryptedPortionAt = outp.size();
  359. std::vector<World> moons(RR->topology->moons());
  360. std::vector<uint64_t> moonsWanted(RR->topology->moonsWanted());
  361. outp.append((uint16_t)(moons.size() + moonsWanted.size()));
  362. for(std::vector<World>::const_iterator m(moons.begin());m!=moons.end();++m) {
  363. outp.append((uint8_t)m->type());
  364. outp.append((uint64_t)m->id());
  365. outp.append((uint64_t)m->timestamp());
  366. }
  367. for(std::vector<uint64_t>::const_iterator m(moonsWanted.begin());m!=moonsWanted.end();++m) {
  368. outp.append((uint8_t)World::TYPE_MOON);
  369. outp.append(*m);
  370. outp.append((uint64_t)0);
  371. }
  372. outp.cryptField(_key,startCryptedPortionAt,outp.size() - startCryptedPortionAt);
  373. RR->node->expectReplyTo(outp.packetId());
  374. if (atAddress) {
  375. outp.armor(_key,false,counter); // false == don't encrypt full payload, but add MAC
  376. RR->node->putPacket(tPtr,localSocket,atAddress,outp.data(),outp.size());
  377. } else {
  378. RR->sw->send(tPtr,outp,false); // false == don't encrypt full payload, but add MAC
  379. }
  380. }
  381. void Peer::attemptToContactAt(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now,bool sendFullHello,unsigned int counter)
  382. {
  383. if ( (!sendFullHello) && (_vProto >= 5) && (!((_vMajor == 1)&&(_vMinor == 1)&&(_vRevision == 0))) ) {
  384. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_ECHO);
  385. RR->node->expectReplyTo(outp.packetId());
  386. outp.armor(_key,true,counter);
  387. RR->node->putPacket(tPtr,localSocket,atAddress,outp.data(),outp.size());
  388. } else {
  389. sendHELLO(tPtr,localSocket,atAddress,now,counter);
  390. }
  391. }
  392. void Peer::tryMemorizedPath(void *tPtr,int64_t now)
  393. {
  394. if ((now - _lastTriedMemorizedPath) >= ZT_TRY_MEMORIZED_PATH_INTERVAL) {
  395. _lastTriedMemorizedPath = now;
  396. InetAddress mp;
  397. if (RR->node->externalPathLookup(tPtr,_id.address(),-1,mp))
  398. attemptToContactAt(tPtr,-1,mp,now,true,0);
  399. }
  400. }
  401. unsigned int Peer::doPingAndKeepalive(void *tPtr,int64_t now)
  402. {
  403. unsigned int sent = 0;
  404. Mutex::Lock _l(_paths_m);
  405. const bool sendFullHello = ((now - _lastSentFullHello) >= ZT_PEER_PING_PERIOD);
  406. _lastSentFullHello = now;
  407. // Right now we only keep pinging links that have the maximum priority. The
  408. // priority is used to track cluster redirections, meaning that when a cluster
  409. // redirects us its redirect target links override all other links and we
  410. // let those old links expire.
  411. long maxPriority = 0;
  412. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  413. if (_paths[i].p)
  414. maxPriority = std::max(_paths[i].priority,maxPriority);
  415. else break;
  416. }
  417. unsigned int j = 0;
  418. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  419. if (_paths[i].p) {
  420. // Clean expired and reduced priority paths
  421. if ( ((now - _paths[i].lr) < ZT_PEER_PATH_EXPIRATION) && (_paths[i].priority == maxPriority) ) {
  422. if ((sendFullHello)||(_paths[i].p->needsHeartbeat(now))) {
  423. attemptToContactAt(tPtr,_paths[i].p->localSocket(),_paths[i].p->address(),now,sendFullHello,_paths[i].p->nextOutgoingCounter());
  424. _paths[i].p->sent(now);
  425. sent |= (_paths[i].p->address().ss_family == AF_INET) ? 0x1 : 0x2;
  426. }
  427. if (i != j)
  428. _paths[j] = _paths[i];
  429. ++j;
  430. }
  431. } else break;
  432. }
  433. while(j < ZT_MAX_PEER_NETWORK_PATHS) {
  434. _paths[j].lr = 0;
  435. _paths[j].p.zero();
  436. _paths[j].priority = 1;
  437. ++j;
  438. }
  439. return sent;
  440. }
  441. void Peer::clusterRedirect(void *tPtr,const SharedPtr<Path> &originatingPath,const InetAddress &remoteAddress,const int64_t now)
  442. {
  443. SharedPtr<Path> np(RR->topology->getPath(originatingPath->localSocket(),remoteAddress));
  444. RR->t->peerRedirected(tPtr,0,*this,np);
  445. attemptToContactAt(tPtr,originatingPath->localSocket(),remoteAddress,now,true,np->nextOutgoingCounter());
  446. {
  447. Mutex::Lock _l(_paths_m);
  448. // New priority is higher than the priority of the originating path (if known)
  449. long newPriority = 1;
  450. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  451. if (_paths[i].p) {
  452. if (_paths[i].p == originatingPath) {
  453. newPriority = _paths[i].priority;
  454. break;
  455. }
  456. } else break;
  457. }
  458. newPriority += 2;
  459. // Erase any paths with lower priority than this one or that are duplicate
  460. // IPs and add this path.
  461. unsigned int j = 0;
  462. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  463. if (_paths[i].p) {
  464. if ((_paths[i].priority >= newPriority)&&(!_paths[i].p->address().ipsEqual2(remoteAddress))) {
  465. if (i != j)
  466. _paths[j] = _paths[i];
  467. ++j;
  468. }
  469. }
  470. }
  471. if (j < ZT_MAX_PEER_NETWORK_PATHS) {
  472. _paths[j].lr = now;
  473. _paths[j].p = np;
  474. _paths[j].priority = newPriority;
  475. ++j;
  476. while (j < ZT_MAX_PEER_NETWORK_PATHS) {
  477. _paths[j].lr = 0;
  478. _paths[j].p.zero();
  479. _paths[j].priority = 1;
  480. ++j;
  481. }
  482. }
  483. }
  484. }
  485. void Peer::resetWithinScope(void *tPtr,InetAddress::IpScope scope,int inetAddressFamily,int64_t now)
  486. {
  487. Mutex::Lock _l(_paths_m);
  488. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  489. if (_paths[i].p) {
  490. if ((_paths[i].p->address().ss_family == inetAddressFamily)&&(_paths[i].p->ipScope() == scope)) {
  491. attemptToContactAt(tPtr,_paths[i].p->localSocket(),_paths[i].p->address(),now,false,_paths[i].p->nextOutgoingCounter());
  492. _paths[i].p->sent(now);
  493. _paths[i].lr = 0; // path will not be used unless it speaks again
  494. }
  495. } else break;
  496. }
  497. }
  498. } // namespace ZeroTier