Peer.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include "../version.h"
  28. #include "Constants.hpp"
  29. #include "Peer.hpp"
  30. #include "Node.hpp"
  31. #include "Switch.hpp"
  32. #include "Network.hpp"
  33. #include "AntiRecursion.hpp"
  34. #include "SelfAwareness.hpp"
  35. #include "Cluster.hpp"
  36. #include "Packet.hpp"
  37. #include <algorithm>
  38. #define ZT_PEER_PATH_SORT_INTERVAL 5000
  39. namespace ZeroTier {
  40. // Used to send varying values for NAT keepalive
  41. static uint32_t _natKeepaliveBuf = 0;
  42. Peer::Peer(const RuntimeEnvironment *renv,const Identity &myIdentity,const Identity &peerIdentity) :
  43. RR(renv),
  44. _lastUsed(0),
  45. _lastReceive(0),
  46. _lastUnicastFrame(0),
  47. _lastMulticastFrame(0),
  48. _lastAnnouncedTo(0),
  49. _lastDirectPathPushSent(0),
  50. _lastDirectPathPushReceive(0),
  51. _lastPathSort(0),
  52. _vProto(0),
  53. _vMajor(0),
  54. _vMinor(0),
  55. _vRevision(0),
  56. _id(peerIdentity),
  57. _numPaths(0),
  58. _latency(0),
  59. _directPathPushCutoffCount(0),
  60. _networkComs(4),
  61. _lastPushedComs(4)
  62. {
  63. if (!myIdentity.agree(peerIdentity,_key,ZT_PEER_SECRET_KEY_LENGTH))
  64. throw std::runtime_error("new peer identity key agreement failed");
  65. }
  66. void Peer::received(
  67. const InetAddress &localAddr,
  68. const InetAddress &remoteAddr,
  69. unsigned int hops,
  70. uint64_t packetId,
  71. Packet::Verb verb,
  72. uint64_t inRePacketId,
  73. Packet::Verb inReVerb)
  74. {
  75. #ifdef ZT_ENABLE_CLUSTER
  76. bool suboptimalPath = false;
  77. if ((RR->cluster)&&(hops == 0)) {
  78. // Note: findBetterEndpoint() is first since we still want to check
  79. // for a better endpoint even if we don't actually send a redirect.
  80. InetAddress redirectTo;
  81. if ( (RR->cluster->findBetterEndpoint(redirectTo,_id.address(),remoteAddr,false)) && (verb != Packet::VERB_OK)&&(verb != Packet::VERB_ERROR)&&(verb != Packet::VERB_RENDEZVOUS)&&(verb != Packet::VERB_PUSH_DIRECT_PATHS) ) {
  82. if (_vProto >= 5) {
  83. // For newer peers we can send a more idiomatic verb: PUSH_DIRECT_PATHS.
  84. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_PUSH_DIRECT_PATHS);
  85. outp.append((uint16_t)1); // count == 1
  86. outp.append((uint8_t)0); // no flags
  87. outp.append((uint16_t)0); // no extensions
  88. if (redirectTo.ss_family == AF_INET) {
  89. outp.append((uint8_t)4);
  90. outp.append((uint8_t)6);
  91. outp.append(redirectTo.rawIpData(),4);
  92. } else {
  93. outp.append((uint8_t)6);
  94. outp.append((uint8_t)18);
  95. outp.append(redirectTo.rawIpData(),16);
  96. }
  97. outp.append((uint16_t)redirectTo.port());
  98. outp.armor(_key,true);
  99. RR->antiRec->logOutgoingZT(outp.data(),outp.size());
  100. RR->node->putPacket(localAddr,remoteAddr,outp.data(),outp.size());
  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);
  115. RR->antiRec->logOutgoingZT(outp.data(),outp.size());
  116. RR->node->putPacket(localAddr,remoteAddr,outp.data(),outp.size());
  117. }
  118. suboptimalPath = true;
  119. }
  120. }
  121. #endif
  122. const uint64_t now = RR->node->now();
  123. bool needMulticastGroupAnnounce = false;
  124. { // begin _lock
  125. Mutex::Lock _l(_lock);
  126. _lastReceive = now;
  127. if ((verb == Packet::VERB_FRAME)||(verb == Packet::VERB_EXT_FRAME))
  128. _lastUnicastFrame = now;
  129. else if (verb == Packet::VERB_MULTICAST_FRAME)
  130. _lastMulticastFrame = now;
  131. if ((now - _lastAnnouncedTo) >= ((ZT_MULTICAST_LIKE_EXPIRE / 2) - 1000)) {
  132. _lastAnnouncedTo = now;
  133. needMulticastGroupAnnounce = true;
  134. }
  135. if (hops == 0) {
  136. bool pathIsConfirmed = false;
  137. unsigned int np = _numPaths;
  138. for(unsigned int p=0;p<np;++p) {
  139. if ((_paths[p].address() == remoteAddr)&&(_paths[p].localAddress() == localAddr)) {
  140. _paths[p].received(now);
  141. #ifdef ZT_ENABLE_CLUSTER
  142. _paths[p].setClusterSuboptimal(suboptimalPath);
  143. #endif
  144. pathIsConfirmed = true;
  145. break;
  146. }
  147. }
  148. if (!pathIsConfirmed) {
  149. if (verb == Packet::VERB_OK) {
  150. Path *slot = (Path *)0;
  151. if (np < ZT_MAX_PEER_NETWORK_PATHS) {
  152. slot = &(_paths[np++]);
  153. } else {
  154. uint64_t slotLRmin = 0xffffffffffffffffULL;
  155. for(unsigned int p=0;p<ZT_MAX_PEER_NETWORK_PATHS;++p) {
  156. if (!_paths[p].active(now)) {
  157. slot = &(_paths[p]);
  158. break;
  159. } else if (_paths[p].lastReceived() <= slotLRmin) {
  160. slotLRmin = _paths[p].lastReceived();
  161. slot = &(_paths[p]);
  162. }
  163. }
  164. }
  165. if (slot) {
  166. *slot = Path(localAddr,remoteAddr);
  167. slot->received(now);
  168. #ifdef ZT_ENABLE_CLUSTER
  169. slot->setClusterSuboptimal(suboptimalPath);
  170. #endif
  171. _numPaths = np;
  172. }
  173. #ifdef ZT_ENABLE_CLUSTER
  174. if (RR->cluster)
  175. RR->cluster->broadcastHavePeer(_id);
  176. #endif
  177. } else {
  178. TRACE("got %s via unknown path %s(%s), confirming...",Packet::verbString(verb),_id.address().toString().c_str(),remoteAddr.toString().c_str());
  179. if ( (_vProto >= 5) && ( !((_vMajor == 1)&&(_vMinor == 1)&&(_vRevision == 0)) ) ) {
  180. // 1.1.1 and newer nodes support ECHO, which is smaller -- but 1.1.0 has a bug so use HELLO there too
  181. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_ECHO);
  182. outp.armor(_key,true);
  183. RR->node->putPacket(localAddr,remoteAddr,outp.data(),outp.size());
  184. } else {
  185. sendHELLO(localAddr,remoteAddr,now);
  186. }
  187. }
  188. }
  189. }
  190. } // end _lock
  191. if (needMulticastGroupAnnounce) {
  192. const std::vector< SharedPtr<Network> > networks(RR->node->allNetworks());
  193. for(std::vector< SharedPtr<Network> >::const_iterator n(networks.begin());n!=networks.end();++n)
  194. (*n)->tryAnnounceMulticastGroupsTo(SharedPtr<Peer>(this));
  195. }
  196. }
  197. void Peer::sendHELLO(const InetAddress &localAddr,const InetAddress &atAddress,uint64_t now,unsigned int ttl)
  198. {
  199. // _lock not required here since _id is immutable and nothing else is accessed
  200. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_HELLO);
  201. outp.append((unsigned char)ZT_PROTO_VERSION);
  202. outp.append((unsigned char)ZEROTIER_ONE_VERSION_MAJOR);
  203. outp.append((unsigned char)ZEROTIER_ONE_VERSION_MINOR);
  204. outp.append((uint16_t)ZEROTIER_ONE_VERSION_REVISION);
  205. outp.append(now);
  206. RR->identity.serialize(outp,false);
  207. atAddress.serialize(outp);
  208. outp.append((uint64_t)RR->topology->worldId());
  209. outp.append((uint64_t)RR->topology->worldTimestamp());
  210. outp.armor(_key,false); // HELLO is sent in the clear
  211. RR->antiRec->logOutgoingZT(outp.data(),outp.size());
  212. RR->node->putPacket(localAddr,atAddress,outp.data(),outp.size(),ttl);
  213. }
  214. bool Peer::doPingAndKeepalive(uint64_t now,int inetAddressFamily)
  215. {
  216. Path *p = (Path *)0;
  217. Mutex::Lock _l(_lock);
  218. if (inetAddressFamily != 0) {
  219. p = _getBestPath(now,inetAddressFamily);
  220. } else {
  221. p = _getBestPath(now);
  222. }
  223. if (p) {
  224. if ((now - p->lastReceived()) >= ZT_PEER_DIRECT_PING_DELAY) {
  225. //TRACE("PING %s(%s) after %llums/%llums send/receive inactivity",_id.address().toString().c_str(),p->address().toString().c_str(),now - p->lastSend(),now - p->lastReceived());
  226. sendHELLO(p->localAddress(),p->address(),now);
  227. p->sent(now);
  228. p->pinged(now);
  229. } else if (((now - std::max(p->lastSend(),p->lastKeepalive())) >= ZT_NAT_KEEPALIVE_DELAY)&&(!p->reliable())) {
  230. //TRACE("NAT keepalive %s(%s) after %llums/%llums send/receive inactivity",_id.address().toString().c_str(),p->address().toString().c_str(),now - p->lastSend(),now - p->lastReceived());
  231. _natKeepaliveBuf += (uint32_t)((now * 0x9e3779b1) >> 1); // tumble this around to send constantly varying (meaningless) payloads
  232. RR->node->putPacket(p->localAddress(),p->address(),&_natKeepaliveBuf,sizeof(_natKeepaliveBuf));
  233. p->sentKeepalive(now);
  234. } else {
  235. //TRACE("no PING or NAT keepalive: addr==%s reliable==%d %llums/%llums send/receive inactivity",p->address().toString().c_str(),(int)p->reliable(),now - p->lastSend(),now - p->lastReceived());
  236. }
  237. return true;
  238. }
  239. return false;
  240. }
  241. void Peer::pushDirectPaths(Path *path,uint64_t now,bool force)
  242. {
  243. #ifdef ZT_ENABLE_CLUSTER
  244. // Cluster mode disables normal PUSH_DIRECT_PATHS in favor of cluster-based peer redirection
  245. if (RR->cluster)
  246. return;
  247. #endif
  248. Mutex::Lock _l(_lock);
  249. if (((now - _lastDirectPathPushSent) >= ZT_DIRECT_PATH_PUSH_INTERVAL)||(force)) {
  250. _lastDirectPathPushSent = now;
  251. std::vector<InetAddress> dps(RR->node->directPaths());
  252. if (dps.empty())
  253. return;
  254. #ifdef ZT_TRACE
  255. {
  256. std::string ps;
  257. for(std::vector<InetAddress>::const_iterator p(dps.begin());p!=dps.end();++p) {
  258. if (ps.length() > 0)
  259. ps.push_back(',');
  260. ps.append(p->toString());
  261. }
  262. TRACE("pushing %u direct paths to %s: %s",(unsigned int)dps.size(),_id.address().toString().c_str(),ps.c_str());
  263. }
  264. #endif
  265. std::vector<InetAddress>::const_iterator p(dps.begin());
  266. while (p != dps.end()) {
  267. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_PUSH_DIRECT_PATHS);
  268. outp.addSize(2); // leave room for count
  269. unsigned int count = 0;
  270. while ((p != dps.end())&&((outp.size() + 24) < ZT_PROTO_MAX_PACKET_LENGTH)) {
  271. uint8_t addressType = 4;
  272. switch(p->ss_family) {
  273. case AF_INET:
  274. break;
  275. case AF_INET6:
  276. addressType = 6;
  277. break;
  278. default: // we currently only push IP addresses
  279. ++p;
  280. continue;
  281. }
  282. outp.append((uint8_t)0); // no flags
  283. outp.append((uint16_t)0); // no extensions
  284. outp.append(addressType);
  285. outp.append((uint8_t)((addressType == 4) ? 6 : 18));
  286. outp.append(p->rawIpData(),((addressType == 4) ? 4 : 16));
  287. outp.append((uint16_t)p->port());
  288. ++count;
  289. ++p;
  290. }
  291. if (count) {
  292. outp.setAt(ZT_PACKET_IDX_PAYLOAD,(uint16_t)count);
  293. outp.armor(_key,true);
  294. path->send(RR,outp.data(),outp.size(),now);
  295. }
  296. }
  297. }
  298. }
  299. bool Peer::resetWithinScope(InetAddress::IpScope scope,uint64_t now)
  300. {
  301. Mutex::Lock _l(_lock);
  302. unsigned int np = _numPaths;
  303. unsigned int x = 0;
  304. unsigned int y = 0;
  305. while (x < np) {
  306. if (_paths[x].address().ipScope() == scope) {
  307. // Resetting a path means sending a HELLO and then forgetting it. If we
  308. // get OK(HELLO) then it will be re-learned.
  309. sendHELLO(_paths[x].localAddress(),_paths[x].address(),now);
  310. } else {
  311. _paths[y++] = _paths[x];
  312. }
  313. ++x;
  314. }
  315. _numPaths = y;
  316. return (y < np);
  317. }
  318. void Peer::getBestActiveAddresses(uint64_t now,InetAddress &v4,InetAddress &v6) const
  319. {
  320. Mutex::Lock _l(_lock);
  321. uint64_t bestV4 = 0,bestV6 = 0;
  322. for(unsigned int p=0,np=_numPaths;p<np;++p) {
  323. if (_paths[p].active(now)) {
  324. uint64_t lr = _paths[p].lastReceived();
  325. if (lr) {
  326. if (_paths[p].address().isV4()) {
  327. if (lr >= bestV4) {
  328. bestV4 = lr;
  329. v4 = _paths[p].address();
  330. }
  331. } else if (_paths[p].address().isV6()) {
  332. if (lr >= bestV6) {
  333. bestV6 = lr;
  334. v6 = _paths[p].address();
  335. }
  336. }
  337. }
  338. }
  339. }
  340. }
  341. bool Peer::networkMembershipCertificatesAgree(uint64_t nwid,const CertificateOfMembership &com) const
  342. {
  343. Mutex::Lock _l(_lock);
  344. const _NetworkCom *ourCom = _networkComs.get(nwid);
  345. if (ourCom)
  346. return ourCom->com.agreesWith(com);
  347. return false;
  348. }
  349. bool Peer::validateAndSetNetworkMembershipCertificate(uint64_t nwid,const CertificateOfMembership &com)
  350. {
  351. // Sanity checks
  352. if ((!com)||(com.issuedTo() != _id.address()))
  353. return false;
  354. // Return true if we already have this *exact* COM
  355. {
  356. Mutex::Lock _l(_lock);
  357. _NetworkCom *ourCom = _networkComs.get(nwid);
  358. if ((ourCom)&&(ourCom->com == com))
  359. return true;
  360. }
  361. // Check signature, log and return if cert is invalid
  362. if (com.signedBy() != Network::controllerFor(nwid)) {
  363. TRACE("rejected network membership certificate for %.16llx signed by %s: signer not a controller of this network",(unsigned long long)_id,com.signedBy().toString().c_str());
  364. return false; // invalid signer
  365. }
  366. if (com.signedBy() == RR->identity.address()) {
  367. // We are the controller: RR->identity.address() == controller() == cert.signedBy()
  368. // So, verify that we signed th cert ourself
  369. if (!com.verify(RR->identity)) {
  370. TRACE("rejected network membership certificate for %.16llx self signed by %s: signature check failed",(unsigned long long)_id,com.signedBy().toString().c_str());
  371. return false; // invalid signature
  372. }
  373. } else {
  374. SharedPtr<Peer> signer(RR->topology->getPeer(com.signedBy()));
  375. if (!signer) {
  376. // This would be rather odd, since this is our controller... could happen
  377. // if we get packets before we've gotten config.
  378. RR->sw->requestWhois(com.signedBy());
  379. return false; // signer unknown
  380. }
  381. if (!com.verify(signer->identity())) {
  382. TRACE("rejected network membership certificate for %.16llx signed by %s: signature check failed",(unsigned long long)_id,com.signedBy().toString().c_str());
  383. return false; // invalid signature
  384. }
  385. }
  386. // If we made it past all those checks, add or update cert in our cert info store
  387. {
  388. Mutex::Lock _l(_lock);
  389. _networkComs.set(nwid,_NetworkCom(RR->node->now(),com));
  390. }
  391. return true;
  392. }
  393. bool Peer::needsOurNetworkMembershipCertificate(uint64_t nwid,uint64_t now,bool updateLastPushedTime)
  394. {
  395. Mutex::Lock _l(_lock);
  396. uint64_t &lastPushed = _lastPushedComs[nwid];
  397. const uint64_t tmp = lastPushed;
  398. if (updateLastPushedTime)
  399. lastPushed = now;
  400. return ((now - tmp) >= (ZT_NETWORK_AUTOCONF_DELAY / 2));
  401. }
  402. void Peer::clean(uint64_t now)
  403. {
  404. Mutex::Lock _l(_lock);
  405. {
  406. unsigned int np = _numPaths;
  407. unsigned int x = 0;
  408. unsigned int y = 0;
  409. while (x < np) {
  410. if (_paths[x].active(now))
  411. _paths[y++] = _paths[x];
  412. ++x;
  413. }
  414. _numPaths = y;
  415. }
  416. {
  417. uint64_t *k = (uint64_t *)0;
  418. _NetworkCom *v = (_NetworkCom *)0;
  419. Hashtable< uint64_t,_NetworkCom >::Iterator i(_networkComs);
  420. while (i.next(k,v)) {
  421. if ( (!RR->node->belongsToNetwork(*k)) && ((now - v->ts) >= ZT_PEER_NETWORK_COM_EXPIRATION) )
  422. _networkComs.erase(*k);
  423. }
  424. }
  425. {
  426. uint64_t *k = (uint64_t *)0;
  427. uint64_t *v = (uint64_t *)0;
  428. Hashtable< uint64_t,uint64_t >::Iterator i(_lastPushedComs);
  429. while (i.next(k,v)) {
  430. if ((now - *v) > (ZT_NETWORK_AUTOCONF_DELAY * 2))
  431. _lastPushedComs.erase(*k);
  432. }
  433. }
  434. }
  435. bool Peer::_checkPath(Path &p,const uint64_t now)
  436. {
  437. // assumes _lock is locked
  438. if (!p.active(now))
  439. return false;
  440. /* Dead path detection: if we have sent something to this peer and have not
  441. * yet received a reply, double check this path. The majority of outbound
  442. * packets including Ethernet frames do generate some kind of reply either
  443. * immediately or at some point in the near future. This will occasionally
  444. * (every NO_ANSWER_TIMEOUT ms) check paths unnecessarily if traffic that
  445. * does not generate a response is being sent such as multicast announcements
  446. * or frames belonging to unidirectional UDP protocols, but the cost is very
  447. * tiny and the benefit in reliability is very large. This takes care of many
  448. * failure modes including crap NATs that forget links and spurious changes
  449. * to physical network topology that cannot be otherwise detected.
  450. *
  451. * Each time we do this we increment a probation counter in the path. This
  452. * counter is reset on any packet receive over this path. If it reaches the
  453. * MAX_PROBATION threshold the path is considred dead. */
  454. if ( (p.lastSend() > p.lastReceived()) && ((p.lastSend() - p.lastReceived()) >= ZT_PEER_DEAD_PATH_DETECTION_NO_ANSWER_TIMEOUT) && ((now - p.lastPing()) >= ZT_PEER_DEAD_PATH_DETECTION_NO_ANSWER_TIMEOUT) ) {
  455. TRACE("%s(%s) has not answered, checking if dead (probation: %u)",_id.address().toString().c_str(),p.address().toString().c_str(),p.probation());
  456. if ( (_vProto >= 5) && ( !((_vMajor == 1)&&(_vMinor == 1)&&(_vRevision == 0)) ) ) {
  457. // 1.1.1 and newer nodes support ECHO, which is smaller -- but 1.1.0 has a bug so use HELLO there too
  458. Packet outp(_id.address(),RR->identity.address(),Packet::VERB_ECHO);
  459. outp.armor(_key,true);
  460. p.send(RR,outp.data(),outp.size(),now);
  461. p.pinged(now);
  462. } else {
  463. sendHELLO(p.localAddress(),p.address(),now);
  464. p.sent(now);
  465. p.pinged(now);
  466. }
  467. p.increaseProbation();
  468. }
  469. return true;
  470. }
  471. Path *Peer::_getBestPath(const uint64_t now)
  472. {
  473. // assumes _lock is locked
  474. Path *bestPath = (Path *)0;
  475. uint64_t bestPathScore = 0;
  476. for(unsigned int i=0;i<_numPaths;++i) {
  477. const uint64_t score = _paths[i].score();
  478. if ((score >= bestPathScore)&&(_checkPath(_paths[i],now))) {
  479. bestPathScore = score;
  480. bestPath = &(_paths[i]);
  481. }
  482. }
  483. return bestPath;
  484. }
  485. Path *Peer::_getBestPath(const uint64_t now,int inetAddressFamily)
  486. {
  487. // assumes _lock is locked
  488. Path *bestPath = (Path *)0;
  489. uint64_t bestPathScore = 0;
  490. for(unsigned int i=0;i<_numPaths;++i) {
  491. const uint64_t score = _paths[i].score();
  492. if (((int)_paths[i].address().ss_family == inetAddressFamily)&&(score >= bestPathScore)&&(_checkPath(_paths[i],now))) {
  493. bestPathScore = score;
  494. bestPath = &(_paths[i]);
  495. }
  496. }
  497. return bestPath;
  498. }
  499. } // namespace ZeroTier