Peer.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. /*
  76. #ifdef ZT_ENABLE_CLUSTER
  77. bool isClusterSuboptimalPath = false;
  78. if ((RR->cluster)&&(hops == 0)) {
  79. // Note: findBetterEndpoint() is first since we still want to check
  80. // for a better endpoint even if we don't actually send a redirect.
  81. InetAddress redirectTo;
  82. if ( (verb != Packet::VERB_OK) && (verb != Packet::VERB_ERROR) && (verb != Packet::VERB_RENDEZVOUS) && (verb != Packet::VERB_PUSH_DIRECT_PATHS) && (RR->cluster->findBetterEndpoint(redirectTo,_id.address(),path->address(),false)) ) {
  83. if (_vProto >= 5) {
  84. // For newer peers we can send a more idiomatic verb: PUSH_DIRECT_PATHS.
  85. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_PUSH_DIRECT_PATHS);
  86. outp.append((uint16_t)1); // count == 1
  87. outp.append((uint8_t)ZT_PUSH_DIRECT_PATHS_FLAG_CLUSTER_REDIRECT); // flags: cluster redirect
  88. outp.append((uint16_t)0); // no extensions
  89. if (redirectTo.ss_family == AF_INET) {
  90. outp.append((uint8_t)4);
  91. outp.append((uint8_t)6);
  92. outp.append(redirectTo.rawIpData(),4);
  93. } else {
  94. outp.append((uint8_t)6);
  95. outp.append((uint8_t)18);
  96. outp.append(redirectTo.rawIpData(),16);
  97. }
  98. outp.append((uint16_t)redirectTo.port());
  99. outp.armor(_key,true,path->nextOutgoingCounter());
  100. path->send(RR,tPtr,outp.data(),outp.size(),now);
  101. } else {
  102. // For older peers we use RENDEZVOUS to coax them into contacting us elsewhere.
  103. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_RENDEZVOUS);
  104. outp.append((uint8_t)0); // no flags
  105. RR->identity.address().appendTo(outp);
  106. outp.append((uint16_t)redirectTo.port());
  107. if (redirectTo.ss_family == AF_INET) {
  108. outp.append((uint8_t)4);
  109. outp.append(redirectTo.rawIpData(),4);
  110. } else {
  111. outp.append((uint8_t)16);
  112. outp.append(redirectTo.rawIpData(),16);
  113. }
  114. outp.armor(_key,true,path->nextOutgoingCounter());
  115. path->send(RR,tPtr,outp.data(),outp.size(),now);
  116. }
  117. isClusterSuboptimalPath = true;
  118. }
  119. }
  120. #endif
  121. */
  122. _lastReceive = now;
  123. switch (verb) {
  124. case Packet::VERB_FRAME:
  125. case Packet::VERB_EXT_FRAME:
  126. case Packet::VERB_NETWORK_CONFIG_REQUEST:
  127. case Packet::VERB_NETWORK_CONFIG:
  128. case Packet::VERB_MULTICAST_FRAME:
  129. _lastNontrivialReceive = now;
  130. break;
  131. default: break;
  132. }
  133. if (trustEstablished) {
  134. _lastTrustEstablishedPacketReceived = now;
  135. path->trustedPacketReceived(now);
  136. }
  137. if (_vProto >= 9)
  138. path->updateLinkQuality((unsigned int)(packetId & 7));
  139. if (hops == 0) {
  140. // If this is a direct packet (no hops), update existing paths or learn new ones
  141. Mutex::Lock _l(_paths_m);
  142. unsigned int worstQualityPath = 0;
  143. int worstQuality = 0;
  144. bool havePath = false;
  145. for(unsigned int p=0;p<ZT_MAX_PEER_NETWORK_PATHS;++p) {
  146. if (_paths[p].p) {
  147. if (_paths[p].p == path) {
  148. _paths[p].lr = now;
  149. havePath = true;
  150. break;
  151. }
  152. const int q = _paths[p].p->quality(now) / _paths[p].priority;
  153. if (q >= worstQuality) {
  154. worstQuality = q;
  155. worstQualityPath = p;
  156. }
  157. } else {
  158. worstQualityPath = p;
  159. break;
  160. }
  161. }
  162. if ((!havePath)&&(RR->node->shouldUsePathForZeroTierTraffic(tPtr,_id.address(),path->localSocket(),path->address()))) {
  163. if (verb == Packet::VERB_OK) {
  164. RR->t->peerLearnedNewPath(tPtr,networkId,*this,_paths[worstQualityPath].p,path,packetId);
  165. _paths[worstQualityPath].lr = now;
  166. _paths[worstQualityPath].p = path;
  167. _paths[worstQualityPath].priority = 1;
  168. } else {
  169. attemptToContactAt(tPtr,path->localSocket(),path->address(),now,true,path->nextOutgoingCounter());
  170. path->sent(now);
  171. RR->t->peerConfirmingUnknownPath(tPtr,networkId,*this,path,packetId,verb);
  172. }
  173. }
  174. }
  175. // If we have a trust relationship periodically push a message enumerating
  176. // all known external addresses for ourselves. We now do this even if we
  177. // have a current path since we'll want to use new ones too.
  178. if (this->trustEstablished(now)) {
  179. if ((now - _lastDirectPathPushSent) >= ZT_DIRECT_PATH_PUSH_INTERVAL) {
  180. _lastDirectPathPushSent = now;
  181. std::vector<InetAddress> pathsToPush;
  182. std::vector<InetAddress> dps(RR->node->directPaths());
  183. for(std::vector<InetAddress>::const_iterator i(dps.begin());i!=dps.end();++i)
  184. pathsToPush.push_back(*i);
  185. // Do symmetric NAT prediction if we are communicating indirectly.
  186. if (hops > 0) {
  187. std::vector<InetAddress> sym(RR->sa->getSymmetricNatPredictions());
  188. for(unsigned long i=0,added=0;i<sym.size();++i) {
  189. InetAddress tmp(sym[(unsigned long)RR->node->prng() % sym.size()]);
  190. if (std::find(pathsToPush.begin(),pathsToPush.end(),tmp) == pathsToPush.end()) {
  191. pathsToPush.push_back(tmp);
  192. if (++added >= ZT_PUSH_DIRECT_PATHS_MAX_PER_SCOPE_AND_FAMILY)
  193. break;
  194. }
  195. }
  196. }
  197. if (pathsToPush.size() > 0) {
  198. std::vector<InetAddress>::const_iterator p(pathsToPush.begin());
  199. while (p != pathsToPush.end()) {
  200. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_PUSH_DIRECT_PATHS);
  201. outp.addSize(2); // leave room for count
  202. unsigned int count = 0;
  203. while ((p != pathsToPush.end())&&((outp.size() + 24) < 1200)) {
  204. uint8_t addressType = 4;
  205. switch(p->ss_family) {
  206. case AF_INET:
  207. break;
  208. case AF_INET6:
  209. addressType = 6;
  210. break;
  211. default: // we currently only push IP addresses
  212. ++p;
  213. continue;
  214. }
  215. outp.append((uint8_t)0); // no flags
  216. outp.append((uint16_t)0); // no extensions
  217. outp.append(addressType);
  218. outp.append((uint8_t)((addressType == 4) ? 6 : 18));
  219. outp.append(p->rawIpData(),((addressType == 4) ? 4 : 16));
  220. outp.append((uint16_t)p->port());
  221. ++count;
  222. ++p;
  223. }
  224. if (count) {
  225. outp.setAt(ZT_PACKET_IDX_PAYLOAD,(uint16_t)count);
  226. outp.armor(_key,true,path->nextOutgoingCounter());
  227. path->send(RR,tPtr,outp.data(),outp.size(),now);
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. SharedPtr<Path> Peer::getBestPath(int64_t now,bool includeExpired) const
  235. {
  236. Mutex::Lock _l(_paths_m);
  237. unsigned int bestPath = ZT_MAX_PEER_NETWORK_PATHS;
  238. int bestPathQuality = 2147483647; // INT_MAX
  239. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  240. if (_paths[i].p) {
  241. if ((includeExpired)||((now - _paths[i].lr) < ZT_PEER_PATH_EXPIRATION)) {
  242. const int q = _paths[i].p->quality(now) / _paths[i].priority;
  243. if (q <= bestPathQuality) {
  244. bestPathQuality = q;
  245. bestPath = i;
  246. }
  247. }
  248. } else break;
  249. }
  250. if (bestPath != ZT_MAX_PEER_NETWORK_PATHS)
  251. return _paths[bestPath].p;
  252. return SharedPtr<Path>();
  253. }
  254. void Peer::introduce(void *const tPtr,const int64_t now,const SharedPtr<Peer> &other) const
  255. {
  256. unsigned int myBestV4ByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  257. unsigned int myBestV6ByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  258. int myBestV4QualityByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  259. int myBestV6QualityByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  260. unsigned int theirBestV4ByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  261. unsigned int theirBestV6ByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  262. int theirBestV4QualityByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  263. int theirBestV6QualityByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  264. for(int i=0;i<=ZT_INETADDRESS_MAX_SCOPE;++i) {
  265. myBestV4ByScope[i] = ZT_MAX_PEER_NETWORK_PATHS;
  266. myBestV6ByScope[i] = ZT_MAX_PEER_NETWORK_PATHS;
  267. myBestV4QualityByScope[i] = 2147483647;
  268. myBestV6QualityByScope[i] = 2147483647;
  269. theirBestV4ByScope[i] = ZT_MAX_PEER_NETWORK_PATHS;
  270. theirBestV6ByScope[i] = ZT_MAX_PEER_NETWORK_PATHS;
  271. theirBestV4QualityByScope[i] = 2147483647;
  272. theirBestV6QualityByScope[i] = 2147483647;
  273. }
  274. Mutex::Lock _l1(_paths_m);
  275. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  276. if (_paths[i].p) {
  277. const int q = _paths[i].p->quality(now) / _paths[i].priority;
  278. const unsigned int s = (unsigned int)_paths[i].p->ipScope();
  279. switch(_paths[i].p->address().ss_family) {
  280. case AF_INET:
  281. if (q <= myBestV4QualityByScope[s]) {
  282. myBestV4QualityByScope[s] = q;
  283. myBestV4ByScope[s] = i;
  284. }
  285. break;
  286. case AF_INET6:
  287. if (q <= myBestV6QualityByScope[s]) {
  288. myBestV6QualityByScope[s] = q;
  289. myBestV6ByScope[s] = i;
  290. }
  291. break;
  292. }
  293. } else break;
  294. }
  295. Mutex::Lock _l2(other->_paths_m);
  296. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  297. if (other->_paths[i].p) {
  298. const int q = other->_paths[i].p->quality(now) / other->_paths[i].priority;
  299. const unsigned int s = (unsigned int)other->_paths[i].p->ipScope();
  300. switch(other->_paths[i].p->address().ss_family) {
  301. case AF_INET:
  302. if (q <= theirBestV4QualityByScope[s]) {
  303. theirBestV4QualityByScope[s] = q;
  304. theirBestV4ByScope[s] = i;
  305. }
  306. break;
  307. case AF_INET6:
  308. if (q <= theirBestV6QualityByScope[s]) {
  309. theirBestV6QualityByScope[s] = q;
  310. theirBestV6ByScope[s] = i;
  311. }
  312. break;
  313. }
  314. } else break;
  315. }
  316. unsigned int mine = ZT_MAX_PEER_NETWORK_PATHS;
  317. unsigned int theirs = ZT_MAX_PEER_NETWORK_PATHS;
  318. for(int s=ZT_INETADDRESS_MAX_SCOPE;s>=0;--s) {
  319. if ((myBestV6ByScope[s] != ZT_MAX_PEER_NETWORK_PATHS)&&(theirBestV6ByScope[s] != ZT_MAX_PEER_NETWORK_PATHS)) {
  320. mine = myBestV6ByScope[s];
  321. theirs = theirBestV6ByScope[s];
  322. break;
  323. }
  324. if ((myBestV4ByScope[s] != ZT_MAX_PEER_NETWORK_PATHS)&&(theirBestV4ByScope[s] != ZT_MAX_PEER_NETWORK_PATHS)) {
  325. mine = myBestV4ByScope[s];
  326. theirs = theirBestV4ByScope[s];
  327. break;
  328. }
  329. }
  330. if (mine != ZT_MAX_PEER_NETWORK_PATHS) {
  331. unsigned int alt = (unsigned int)RR->node->prng() & 1; // randomize which hint we send first for black magickal NAT-t reasons
  332. const unsigned int completed = alt + 2;
  333. while (alt != completed) {
  334. if ((alt & 1) == 0) {
  335. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_RENDEZVOUS);
  336. outp.append((uint8_t)0);
  337. other->_id.address().appendTo(outp);
  338. outp.append((uint16_t)other->_paths[theirs].p->address().port());
  339. if (other->_paths[theirs].p->address().ss_family == AF_INET6) {
  340. outp.append((uint8_t)16);
  341. outp.append(other->_paths[theirs].p->address().rawIpData(),16);
  342. } else {
  343. outp.append((uint8_t)4);
  344. outp.append(other->_paths[theirs].p->address().rawIpData(),4);
  345. }
  346. outp.armor(_key,true,_paths[mine].p->nextOutgoingCounter());
  347. _paths[mine].p->send(RR,tPtr,outp.data(),outp.size(),now);
  348. } else {
  349. Packet outp(other->_id.address(),RR->identity.address(),Packet::VERB_RENDEZVOUS);
  350. outp.append((uint8_t)0);
  351. _id.address().appendTo(outp);
  352. outp.append((uint16_t)_paths[mine].p->address().port());
  353. if (_paths[mine].p->address().ss_family == AF_INET6) {
  354. outp.append((uint8_t)16);
  355. outp.append(_paths[mine].p->address().rawIpData(),16);
  356. } else {
  357. outp.append((uint8_t)4);
  358. outp.append(_paths[mine].p->address().rawIpData(),4);
  359. }
  360. outp.armor(other->_key,true,other->_paths[theirs].p->nextOutgoingCounter());
  361. other->_paths[theirs].p->send(RR,tPtr,outp.data(),outp.size(),now);
  362. }
  363. ++alt;
  364. }
  365. }
  366. }
  367. void Peer::sendHELLO(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now,unsigned int counter)
  368. {
  369. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_HELLO);
  370. outp.append((unsigned char)ZT_PROTO_VERSION);
  371. outp.append((unsigned char)ZEROTIER_ONE_VERSION_MAJOR);
  372. outp.append((unsigned char)ZEROTIER_ONE_VERSION_MINOR);
  373. outp.append((uint16_t)ZEROTIER_ONE_VERSION_REVISION);
  374. outp.append(now);
  375. RR->identity.serialize(outp,false);
  376. atAddress.serialize(outp);
  377. outp.append((uint64_t)RR->topology->planetWorldId());
  378. outp.append((uint64_t)RR->topology->planetWorldTimestamp());
  379. const unsigned int startCryptedPortionAt = outp.size();
  380. std::vector<World> moons(RR->topology->moons());
  381. std::vector<uint64_t> moonsWanted(RR->topology->moonsWanted());
  382. outp.append((uint16_t)(moons.size() + moonsWanted.size()));
  383. for(std::vector<World>::const_iterator m(moons.begin());m!=moons.end();++m) {
  384. outp.append((uint8_t)m->type());
  385. outp.append((uint64_t)m->id());
  386. outp.append((uint64_t)m->timestamp());
  387. }
  388. for(std::vector<uint64_t>::const_iterator m(moonsWanted.begin());m!=moonsWanted.end();++m) {
  389. outp.append((uint8_t)World::TYPE_MOON);
  390. outp.append(*m);
  391. outp.append((uint64_t)0);
  392. }
  393. outp.cryptField(_key,startCryptedPortionAt,outp.size() - startCryptedPortionAt);
  394. RR->node->expectReplyTo(outp.packetId());
  395. if (atAddress) {
  396. outp.armor(_key,false,counter); // false == don't encrypt full payload, but add MAC
  397. RR->node->putPacket(tPtr,localSocket,atAddress,outp.data(),outp.size());
  398. } else {
  399. RR->sw->send(tPtr,outp,false); // false == don't encrypt full payload, but add MAC
  400. }
  401. }
  402. void Peer::attemptToContactAt(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now,bool sendFullHello,unsigned int counter)
  403. {
  404. if ( (!sendFullHello) && (_vProto >= 5) && (!((_vMajor == 1)&&(_vMinor == 1)&&(_vRevision == 0))) ) {
  405. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_ECHO);
  406. RR->node->expectReplyTo(outp.packetId());
  407. outp.armor(_key,true,counter);
  408. RR->node->putPacket(tPtr,localSocket,atAddress,outp.data(),outp.size());
  409. } else {
  410. sendHELLO(tPtr,localSocket,atAddress,now,counter);
  411. }
  412. }
  413. void Peer::tryMemorizedPath(void *tPtr,int64_t now)
  414. {
  415. if ((now - _lastTriedMemorizedPath) >= ZT_TRY_MEMORIZED_PATH_INTERVAL) {
  416. _lastTriedMemorizedPath = now;
  417. InetAddress mp;
  418. if (RR->node->externalPathLookup(tPtr,_id.address(),-1,mp))
  419. attemptToContactAt(tPtr,-1,mp,now,true,0);
  420. }
  421. }
  422. unsigned int Peer::doPingAndKeepalive(void *tPtr,int64_t now)
  423. {
  424. unsigned int sent = 0;
  425. Mutex::Lock _l(_paths_m);
  426. const bool sendFullHello = ((now - _lastSentFullHello) >= ZT_PEER_PING_PERIOD);
  427. _lastSentFullHello = now;
  428. unsigned int j = 0;
  429. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  430. if (!_paths[i].p) break;
  431. if ((now - _paths[i].lr) < ZT_PEER_PATH_EXPIRATION) {
  432. if ((sendFullHello)||(_paths[i].p->needsHeartbeat(now))) {
  433. attemptToContactAt(tPtr,_paths[i].p->localSocket(),_paths[i].p->address(),now,sendFullHello,_paths[i].p->nextOutgoingCounter());
  434. _paths[i].p->sent(now);
  435. sent |= (_paths[i].p->address().ss_family == AF_INET) ? 0x1 : 0x2;
  436. }
  437. if (i != j)
  438. _paths[j] = _paths[i];
  439. ++j;
  440. }
  441. }
  442. while(j < ZT_MAX_PEER_NETWORK_PATHS) {
  443. _paths[j].lr = 0;
  444. _paths[j].p.zero();
  445. _paths[j].priority = 1;
  446. ++j;
  447. }
  448. return sent;
  449. }
  450. void Peer::clusterRedirect(void *tPtr,const int64_t localSocket,const InetAddress &remoteAddress,const int64_t now)
  451. {
  452. SharedPtr<Path> np(RR->topology->getPath(localSocket,remoteAddress));
  453. RR->t->peerRedirected(tPtr,0,*this,np);
  454. attemptToContactAt(tPtr,localSocket,remoteAddress,now,true,np->nextOutgoingCounter());
  455. {
  456. Mutex::Lock _l(_paths_m);
  457. int worstQuality = 0;
  458. unsigned int worstQualityPath = 0;
  459. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  460. if (_paths[i].p) {
  461. if (_paths[i].p == np) { // <-- where's my Fields Medal?
  462. _paths[i].lr = now; // consider this a "receive"
  463. _paths[i].priority += 5; // kind of arbitrary, bumps way up in best path quality order
  464. return;
  465. }
  466. const int q = _paths[i].p->quality(now) / _paths[i].priority;
  467. if (q >= worstQuality) {
  468. worstQuality = q;
  469. worstQualityPath = i;
  470. }
  471. } else {
  472. worstQualityPath = i;
  473. break;
  474. }
  475. }
  476. _paths[worstQualityPath].lr = now;
  477. _paths[worstQualityPath].p = np;
  478. _paths[worstQualityPath].priority = 6; // 1 + 5
  479. }
  480. }
  481. void Peer::resetWithinScope(void *tPtr,InetAddress::IpScope scope,int inetAddressFamily,int64_t now)
  482. {
  483. Mutex::Lock _l(_paths_m);
  484. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  485. if (_paths[i].p) {
  486. if ((_paths[i].p->address().ss_family == inetAddressFamily)&&(_paths[i].p->ipScope() == scope)) {
  487. attemptToContactAt(tPtr,_paths[i].p->localSocket(),_paths[i].p->address(),now,false,_paths[i].p->nextOutgoingCounter());
  488. _paths[i].p->sent(now);
  489. _paths[i].lr = 0; // path will not be used unless it speaks again
  490. }
  491. } else break;
  492. }
  493. }
  494. } // namespace ZeroTier