Peer.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. bool havePath = false;
  142. {
  143. Mutex::Lock _l(_paths_m);
  144. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  145. if (_paths[i].p) {
  146. if (_paths[i].p == path) {
  147. _paths[i].lr = now;
  148. havePath = true;
  149. break;
  150. }
  151. } else break;
  152. }
  153. }
  154. if ((!havePath)&&(RR->node->shouldUsePathForZeroTierTraffic(tPtr,_id.address(),path->localSocket(),path->address()))) {
  155. Mutex::Lock _l(_paths_m);
  156. // Paths are redunant if they duplicate an alive path to the same IP or
  157. // with the same local socket and address family.
  158. bool redundant = false;
  159. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  160. if (_paths[i].p) {
  161. 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())) ) ) {
  162. redundant = true;
  163. break;
  164. }
  165. } else break;
  166. }
  167. if (!redundant) {
  168. unsigned int replacePath = ZT_MAX_PEER_NETWORK_PATHS;
  169. int replacePathQuality = 0;
  170. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  171. if (_paths[i].p) {
  172. const int q = _paths[i].p->quality(now);
  173. if (q > replacePathQuality) {
  174. replacePathQuality = q;
  175. replacePath = i;
  176. }
  177. } else {
  178. replacePath = i;
  179. break;
  180. }
  181. }
  182. if (replacePath != ZT_MAX_PEER_NETWORK_PATHS) {
  183. if (verb == Packet::VERB_OK) {
  184. RR->t->peerLearnedNewPath(tPtr,networkId,*this,_paths[replacePath].p,path,packetId);
  185. _paths[replacePath].lr = now;
  186. _paths[replacePath].p = path;
  187. _paths[replacePath].priority = 1;
  188. } else {
  189. attemptToContactAt(tPtr,path->localSocket(),path->address(),now,true,path->nextOutgoingCounter());
  190. path->sent(now);
  191. RR->t->peerConfirmingUnknownPath(tPtr,networkId,*this,path,packetId,verb);
  192. }
  193. }
  194. }
  195. }
  196. }
  197. // If we have a trust relationship periodically push a message enumerating
  198. // all known external addresses for ourselves. We now do this even if we
  199. // have a current path since we'll want to use new ones too.
  200. if (this->trustEstablished(now)) {
  201. if ((now - _lastDirectPathPushSent) >= ZT_DIRECT_PATH_PUSH_INTERVAL) {
  202. _lastDirectPathPushSent = now;
  203. std::vector<InetAddress> pathsToPush;
  204. std::vector<InetAddress> dps(RR->node->directPaths());
  205. for(std::vector<InetAddress>::const_iterator i(dps.begin());i!=dps.end();++i)
  206. pathsToPush.push_back(*i);
  207. // Do symmetric NAT prediction if we are communicating indirectly.
  208. if (hops > 0) {
  209. std::vector<InetAddress> sym(RR->sa->getSymmetricNatPredictions());
  210. for(unsigned long i=0,added=0;i<sym.size();++i) {
  211. InetAddress tmp(sym[(unsigned long)RR->node->prng() % sym.size()]);
  212. if (std::find(pathsToPush.begin(),pathsToPush.end(),tmp) == pathsToPush.end()) {
  213. pathsToPush.push_back(tmp);
  214. if (++added >= ZT_PUSH_DIRECT_PATHS_MAX_PER_SCOPE_AND_FAMILY)
  215. break;
  216. }
  217. }
  218. }
  219. if (pathsToPush.size() > 0) {
  220. std::vector<InetAddress>::const_iterator p(pathsToPush.begin());
  221. while (p != pathsToPush.end()) {
  222. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_PUSH_DIRECT_PATHS);
  223. outp.addSize(2); // leave room for count
  224. unsigned int count = 0;
  225. while ((p != pathsToPush.end())&&((outp.size() + 24) < 1200)) {
  226. uint8_t addressType = 4;
  227. switch(p->ss_family) {
  228. case AF_INET:
  229. break;
  230. case AF_INET6:
  231. addressType = 6;
  232. break;
  233. default: // we currently only push IP addresses
  234. ++p;
  235. continue;
  236. }
  237. outp.append((uint8_t)0); // no flags
  238. outp.append((uint16_t)0); // no extensions
  239. outp.append(addressType);
  240. outp.append((uint8_t)((addressType == 4) ? 6 : 18));
  241. outp.append(p->rawIpData(),((addressType == 4) ? 4 : 16));
  242. outp.append((uint16_t)p->port());
  243. ++count;
  244. ++p;
  245. }
  246. if (count) {
  247. outp.setAt(ZT_PACKET_IDX_PAYLOAD,(uint16_t)count);
  248. outp.armor(_key,true,path->nextOutgoingCounter());
  249. path->send(RR,tPtr,outp.data(),outp.size(),now);
  250. }
  251. }
  252. }
  253. }
  254. }
  255. }
  256. SharedPtr<Path> Peer::getBestPath(int64_t now,bool includeExpired) const
  257. {
  258. Mutex::Lock _l(_paths_m);
  259. unsigned int bestPath = ZT_MAX_PEER_NETWORK_PATHS;
  260. long bestPathQuality = 2147483647;
  261. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  262. if (_paths[i].p) {
  263. if ((includeExpired)||((now - _paths[i].lr) < ZT_PEER_PATH_EXPIRATION)) {
  264. const long q = _paths[i].p->quality(now) / _paths[i].priority;
  265. if (q <= bestPathQuality) {
  266. bestPathQuality = q;
  267. bestPath = i;
  268. }
  269. }
  270. } else break;
  271. }
  272. if (bestPath != ZT_MAX_PEER_NETWORK_PATHS)
  273. return _paths[bestPath].p;
  274. return SharedPtr<Path>();
  275. }
  276. void Peer::introduce(void *const tPtr,const int64_t now,const SharedPtr<Peer> &other) const
  277. {
  278. unsigned int myBestV4ByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  279. unsigned int myBestV6ByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  280. long myBestV4QualityByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  281. long myBestV6QualityByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  282. unsigned int theirBestV4ByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  283. unsigned int theirBestV6ByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  284. long theirBestV4QualityByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  285. long theirBestV6QualityByScope[ZT_INETADDRESS_MAX_SCOPE+1];
  286. for(int i=0;i<=ZT_INETADDRESS_MAX_SCOPE;++i) {
  287. myBestV4ByScope[i] = ZT_MAX_PEER_NETWORK_PATHS;
  288. myBestV6ByScope[i] = ZT_MAX_PEER_NETWORK_PATHS;
  289. myBestV4QualityByScope[i] = 2147483647;
  290. myBestV6QualityByScope[i] = 2147483647;
  291. theirBestV4ByScope[i] = ZT_MAX_PEER_NETWORK_PATHS;
  292. theirBestV6ByScope[i] = ZT_MAX_PEER_NETWORK_PATHS;
  293. theirBestV4QualityByScope[i] = 2147483647;
  294. theirBestV6QualityByScope[i] = 2147483647;
  295. }
  296. Mutex::Lock _l1(_paths_m);
  297. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  298. if (_paths[i].p) {
  299. const long q = _paths[i].p->quality(now) / _paths[i].priority;
  300. const unsigned int s = (unsigned int)_paths[i].p->ipScope();
  301. switch(_paths[i].p->address().ss_family) {
  302. case AF_INET:
  303. if (q <= myBestV4QualityByScope[s]) {
  304. myBestV4QualityByScope[s] = q;
  305. myBestV4ByScope[s] = i;
  306. }
  307. break;
  308. case AF_INET6:
  309. if (q <= myBestV6QualityByScope[s]) {
  310. myBestV6QualityByScope[s] = q;
  311. myBestV6ByScope[s] = i;
  312. }
  313. break;
  314. }
  315. } else break;
  316. }
  317. Mutex::Lock _l2(other->_paths_m);
  318. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  319. if (other->_paths[i].p) {
  320. const long q = other->_paths[i].p->quality(now) / other->_paths[i].priority;
  321. const unsigned int s = (unsigned int)other->_paths[i].p->ipScope();
  322. switch(other->_paths[i].p->address().ss_family) {
  323. case AF_INET:
  324. if (q <= theirBestV4QualityByScope[s]) {
  325. theirBestV4QualityByScope[s] = q;
  326. theirBestV4ByScope[s] = i;
  327. }
  328. break;
  329. case AF_INET6:
  330. if (q <= theirBestV6QualityByScope[s]) {
  331. theirBestV6QualityByScope[s] = q;
  332. theirBestV6ByScope[s] = i;
  333. }
  334. break;
  335. }
  336. } else break;
  337. }
  338. unsigned int mine = ZT_MAX_PEER_NETWORK_PATHS;
  339. unsigned int theirs = ZT_MAX_PEER_NETWORK_PATHS;
  340. for(int s=ZT_INETADDRESS_MAX_SCOPE;s>=0;--s) {
  341. if ((myBestV6ByScope[s] != ZT_MAX_PEER_NETWORK_PATHS)&&(theirBestV6ByScope[s] != ZT_MAX_PEER_NETWORK_PATHS)) {
  342. mine = myBestV6ByScope[s];
  343. theirs = theirBestV6ByScope[s];
  344. break;
  345. }
  346. if ((myBestV4ByScope[s] != ZT_MAX_PEER_NETWORK_PATHS)&&(theirBestV4ByScope[s] != ZT_MAX_PEER_NETWORK_PATHS)) {
  347. mine = myBestV4ByScope[s];
  348. theirs = theirBestV4ByScope[s];
  349. break;
  350. }
  351. }
  352. if (mine != ZT_MAX_PEER_NETWORK_PATHS) {
  353. unsigned int alt = (unsigned int)RR->node->prng() & 1; // randomize which hint we send first for black magickal NAT-t reasons
  354. const unsigned int completed = alt + 2;
  355. while (alt != completed) {
  356. if ((alt & 1) == 0) {
  357. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_RENDEZVOUS);
  358. outp.append((uint8_t)0);
  359. other->_id.address().appendTo(outp);
  360. outp.append((uint16_t)other->_paths[theirs].p->address().port());
  361. if (other->_paths[theirs].p->address().ss_family == AF_INET6) {
  362. outp.append((uint8_t)16);
  363. outp.append(other->_paths[theirs].p->address().rawIpData(),16);
  364. } else {
  365. outp.append((uint8_t)4);
  366. outp.append(other->_paths[theirs].p->address().rawIpData(),4);
  367. }
  368. outp.armor(_key,true,_paths[mine].p->nextOutgoingCounter());
  369. _paths[mine].p->send(RR,tPtr,outp.data(),outp.size(),now);
  370. } else {
  371. Packet outp(other->_id.address(),RR->identity.address(),Packet::VERB_RENDEZVOUS);
  372. outp.append((uint8_t)0);
  373. _id.address().appendTo(outp);
  374. outp.append((uint16_t)_paths[mine].p->address().port());
  375. if (_paths[mine].p->address().ss_family == AF_INET6) {
  376. outp.append((uint8_t)16);
  377. outp.append(_paths[mine].p->address().rawIpData(),16);
  378. } else {
  379. outp.append((uint8_t)4);
  380. outp.append(_paths[mine].p->address().rawIpData(),4);
  381. }
  382. outp.armor(other->_key,true,other->_paths[theirs].p->nextOutgoingCounter());
  383. other->_paths[theirs].p->send(RR,tPtr,outp.data(),outp.size(),now);
  384. }
  385. ++alt;
  386. }
  387. }
  388. }
  389. void Peer::sendHELLO(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now,unsigned int counter)
  390. {
  391. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_HELLO);
  392. outp.append((unsigned char)ZT_PROTO_VERSION);
  393. outp.append((unsigned char)ZEROTIER_ONE_VERSION_MAJOR);
  394. outp.append((unsigned char)ZEROTIER_ONE_VERSION_MINOR);
  395. outp.append((uint16_t)ZEROTIER_ONE_VERSION_REVISION);
  396. outp.append(now);
  397. RR->identity.serialize(outp,false);
  398. atAddress.serialize(outp);
  399. outp.append((uint64_t)RR->topology->planetWorldId());
  400. outp.append((uint64_t)RR->topology->planetWorldTimestamp());
  401. const unsigned int startCryptedPortionAt = outp.size();
  402. std::vector<World> moons(RR->topology->moons());
  403. std::vector<uint64_t> moonsWanted(RR->topology->moonsWanted());
  404. outp.append((uint16_t)(moons.size() + moonsWanted.size()));
  405. for(std::vector<World>::const_iterator m(moons.begin());m!=moons.end();++m) {
  406. outp.append((uint8_t)m->type());
  407. outp.append((uint64_t)m->id());
  408. outp.append((uint64_t)m->timestamp());
  409. }
  410. for(std::vector<uint64_t>::const_iterator m(moonsWanted.begin());m!=moonsWanted.end();++m) {
  411. outp.append((uint8_t)World::TYPE_MOON);
  412. outp.append(*m);
  413. outp.append((uint64_t)0);
  414. }
  415. outp.cryptField(_key,startCryptedPortionAt,outp.size() - startCryptedPortionAt);
  416. RR->node->expectReplyTo(outp.packetId());
  417. if (atAddress) {
  418. outp.armor(_key,false,counter); // false == don't encrypt full payload, but add MAC
  419. RR->node->putPacket(tPtr,localSocket,atAddress,outp.data(),outp.size());
  420. } else {
  421. RR->sw->send(tPtr,outp,false); // false == don't encrypt full payload, but add MAC
  422. }
  423. }
  424. void Peer::attemptToContactAt(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now,bool sendFullHello,unsigned int counter)
  425. {
  426. if ( (!sendFullHello) && (_vProto >= 5) && (!((_vMajor == 1)&&(_vMinor == 1)&&(_vRevision == 0))) ) {
  427. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_ECHO);
  428. RR->node->expectReplyTo(outp.packetId());
  429. outp.armor(_key,true,counter);
  430. RR->node->putPacket(tPtr,localSocket,atAddress,outp.data(),outp.size());
  431. } else {
  432. sendHELLO(tPtr,localSocket,atAddress,now,counter);
  433. }
  434. }
  435. void Peer::tryMemorizedPath(void *tPtr,int64_t now)
  436. {
  437. if ((now - _lastTriedMemorizedPath) >= ZT_TRY_MEMORIZED_PATH_INTERVAL) {
  438. _lastTriedMemorizedPath = now;
  439. InetAddress mp;
  440. if (RR->node->externalPathLookup(tPtr,_id.address(),-1,mp))
  441. attemptToContactAt(tPtr,-1,mp,now,true,0);
  442. }
  443. }
  444. unsigned int Peer::doPingAndKeepalive(void *tPtr,int64_t now)
  445. {
  446. unsigned int sent = 0;
  447. Mutex::Lock _l(_paths_m);
  448. const bool sendFullHello = ((now - _lastSentFullHello) >= ZT_PEER_PING_PERIOD);
  449. _lastSentFullHello = now;
  450. // Right now we only keep pinging links that have the maximum priority. The
  451. // priority is used to track cluster redirections, meaning that when a cluster
  452. // redirects us its redirect target links override all other links and we
  453. // let those old links expire.
  454. long maxPriority = 0;
  455. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  456. if (_paths[i].p)
  457. maxPriority = std::max(_paths[i].priority,maxPriority);
  458. else break;
  459. }
  460. unsigned int j = 0;
  461. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  462. if (_paths[i].p) {
  463. // Clean expired and reduced priority paths
  464. if ( ((now - _paths[i].lr) < ZT_PEER_PATH_EXPIRATION) && (_paths[i].priority == maxPriority) ) {
  465. if ((sendFullHello)||(_paths[i].p->needsHeartbeat(now))) {
  466. attemptToContactAt(tPtr,_paths[i].p->localSocket(),_paths[i].p->address(),now,sendFullHello,_paths[i].p->nextOutgoingCounter());
  467. _paths[i].p->sent(now);
  468. sent |= (_paths[i].p->address().ss_family == AF_INET) ? 0x1 : 0x2;
  469. }
  470. if (i != j)
  471. _paths[j] = _paths[i];
  472. ++j;
  473. }
  474. } else break;
  475. }
  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. return sent;
  483. }
  484. void Peer::clusterRedirect(void *tPtr,const SharedPtr<Path> &originatingPath,const InetAddress &remoteAddress,const int64_t now)
  485. {
  486. SharedPtr<Path> np(RR->topology->getPath(originatingPath->localSocket(),remoteAddress));
  487. RR->t->peerRedirected(tPtr,0,*this,np);
  488. attemptToContactAt(tPtr,originatingPath->localSocket(),remoteAddress,now,true,np->nextOutgoingCounter());
  489. {
  490. Mutex::Lock _l(_paths_m);
  491. // New priority is higher than the priority of the originating path (if known)
  492. long newPriority = 1;
  493. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  494. if (_paths[i].p) {
  495. if (_paths[i].p == originatingPath) {
  496. newPriority = _paths[i].priority;
  497. break;
  498. }
  499. } else break;
  500. }
  501. newPriority += 2;
  502. // Erase any paths with lower priority than this one or that are duplicate
  503. // IPs and add this path.
  504. unsigned int j = 0;
  505. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  506. if (_paths[i].p) {
  507. if ((_paths[i].priority >= newPriority)&&(!_paths[i].p->address().ipsEqual2(remoteAddress))) {
  508. if (i != j)
  509. _paths[j] = _paths[i];
  510. ++j;
  511. }
  512. }
  513. }
  514. if (j < ZT_MAX_PEER_NETWORK_PATHS) {
  515. _paths[j].lr = now;
  516. _paths[j].p = np;
  517. _paths[j].priority = newPriority;
  518. ++j;
  519. while (j < ZT_MAX_PEER_NETWORK_PATHS) {
  520. _paths[j].lr = 0;
  521. _paths[j].p.zero();
  522. _paths[j].priority = 1;
  523. ++j;
  524. }
  525. }
  526. }
  527. }
  528. void Peer::resetWithinScope(void *tPtr,InetAddress::IpScope scope,int inetAddressFamily,int64_t now)
  529. {
  530. Mutex::Lock _l(_paths_m);
  531. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  532. if (_paths[i].p) {
  533. if ((_paths[i].p->address().ss_family == inetAddressFamily)&&(_paths[i].p->ipScope() == scope)) {
  534. attemptToContactAt(tPtr,_paths[i].p->localSocket(),_paths[i].p->address(),now,false,_paths[i].p->nextOutgoingCounter());
  535. _paths[i].p->sent(now);
  536. _paths[i].lr = 0; // path will not be used unless it speaks again
  537. }
  538. } else break;
  539. }
  540. }
  541. } // namespace ZeroTier