TunnelPool.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. #include <algorithm>
  2. #include "I2PEndian.h"
  3. #include "Crypto.h"
  4. #include "Tunnel.h"
  5. #include "NetDb.hpp"
  6. #include "Timestamp.h"
  7. #include "Garlic.h"
  8. #include "Transports.h"
  9. #include "Log.h"
  10. #include "Tunnel.h"
  11. #include "TunnelPool.h"
  12. #include "Destination.h"
  13. #ifdef WITH_EVENTS
  14. #include "Event.h"
  15. #endif
  16. namespace i2p
  17. {
  18. namespace tunnel
  19. {
  20. TunnelPool::TunnelPool (int numInboundHops, int numOutboundHops, int numInboundTunnels, int numOutboundTunnels):
  21. m_NumInboundHops (numInboundHops), m_NumOutboundHops (numOutboundHops),
  22. m_NumInboundTunnels (numInboundTunnels), m_NumOutboundTunnels (numOutboundTunnels), m_IsActive (true),
  23. m_CustomPeerSelector(nullptr)
  24. {
  25. }
  26. TunnelPool::~TunnelPool ()
  27. {
  28. DetachTunnels ();
  29. }
  30. void TunnelPool::SetExplicitPeers (std::shared_ptr<std::vector<i2p::data::IdentHash> > explicitPeers)
  31. {
  32. m_ExplicitPeers = explicitPeers;
  33. if (m_ExplicitPeers)
  34. {
  35. int size = m_ExplicitPeers->size ();
  36. if (m_NumInboundHops > size)
  37. {
  38. m_NumInboundHops = size;
  39. LogPrint (eLogInfo, "Tunnels: Inbound tunnel length has beed adjusted to ", size, " for explicit peers");
  40. }
  41. if (m_NumOutboundHops > size)
  42. {
  43. m_NumOutboundHops = size;
  44. LogPrint (eLogInfo, "Tunnels: Outbound tunnel length has beed adjusted to ", size, " for explicit peers");
  45. }
  46. m_NumInboundTunnels = 1;
  47. m_NumOutboundTunnels = 1;
  48. }
  49. }
  50. void TunnelPool::DetachTunnels ()
  51. {
  52. {
  53. std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
  54. for (auto& it: m_InboundTunnels)
  55. it->SetTunnelPool (nullptr);
  56. m_InboundTunnels.clear ();
  57. }
  58. {
  59. std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
  60. for (auto& it: m_OutboundTunnels)
  61. it->SetTunnelPool (nullptr);
  62. m_OutboundTunnels.clear ();
  63. }
  64. m_Tests.clear ();
  65. }
  66. bool TunnelPool::Reconfigure(int inHops, int outHops, int inQuant, int outQuant) {
  67. if( inHops >= 0 && outHops >= 0 && inQuant > 0 && outQuant > 0)
  68. {
  69. m_NumInboundHops = inHops;
  70. m_NumOutboundHops = outHops;
  71. m_NumInboundTunnels = inQuant;
  72. m_NumOutboundTunnels = outQuant;
  73. return true;
  74. }
  75. return false;
  76. }
  77. void TunnelPool::TunnelCreated (std::shared_ptr<InboundTunnel> createdTunnel)
  78. {
  79. if (!m_IsActive) return;
  80. {
  81. #ifdef WITH_EVENTS
  82. EmitTunnelEvent("tunnels.created", createdTunnel);
  83. #endif
  84. std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
  85. m_InboundTunnels.insert (createdTunnel);
  86. }
  87. if (m_LocalDestination)
  88. m_LocalDestination->SetLeaseSetUpdated ();
  89. OnTunnelBuildResult(createdTunnel, eBuildResultOkay);
  90. }
  91. void TunnelPool::TunnelExpired (std::shared_ptr<InboundTunnel> expiredTunnel)
  92. {
  93. if (expiredTunnel)
  94. {
  95. #ifdef WITH_EVENTS
  96. EmitTunnelEvent("tunnels.expired", expiredTunnel);
  97. #endif
  98. expiredTunnel->SetTunnelPool (nullptr);
  99. for (auto& it: m_Tests)
  100. if (it.second.second == expiredTunnel) it.second.second = nullptr;
  101. std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
  102. m_InboundTunnels.erase (expiredTunnel);
  103. }
  104. }
  105. void TunnelPool::TunnelCreated (std::shared_ptr<OutboundTunnel> createdTunnel)
  106. {
  107. if (!m_IsActive) return;
  108. {
  109. #ifdef WITH_EVENTS
  110. EmitTunnelEvent("tunnels.created", createdTunnel);
  111. #endif
  112. std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
  113. m_OutboundTunnels.insert (createdTunnel);
  114. }
  115. OnTunnelBuildResult(createdTunnel, eBuildResultOkay);
  116. //CreatePairedInboundTunnel (createdTunnel);
  117. }
  118. void TunnelPool::TunnelExpired (std::shared_ptr<OutboundTunnel> expiredTunnel)
  119. {
  120. if (expiredTunnel)
  121. {
  122. #ifdef WITH_EVENTS
  123. EmitTunnelEvent("tunnels.expired", expiredTunnel);
  124. #endif
  125. expiredTunnel->SetTunnelPool (nullptr);
  126. for (auto& it: m_Tests)
  127. if (it.second.first == expiredTunnel) it.second.first = nullptr;
  128. std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
  129. m_OutboundTunnels.erase (expiredTunnel);
  130. }
  131. }
  132. std::vector<std::shared_ptr<InboundTunnel> > TunnelPool::GetInboundTunnels (int num) const
  133. {
  134. std::vector<std::shared_ptr<InboundTunnel> > v;
  135. int i = 0;
  136. std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
  137. for (const auto& it : m_InboundTunnels)
  138. {
  139. if (i >= num) break;
  140. if (it->IsEstablished ())
  141. {
  142. v.push_back (it);
  143. i++;
  144. }
  145. }
  146. return v;
  147. }
  148. std::shared_ptr<OutboundTunnel> TunnelPool::GetNextOutboundTunnel (std::shared_ptr<OutboundTunnel> excluded) const
  149. {
  150. std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
  151. return GetNextTunnel (m_OutboundTunnels, excluded);
  152. }
  153. std::shared_ptr<InboundTunnel> TunnelPool::GetNextInboundTunnel (std::shared_ptr<InboundTunnel> excluded) const
  154. {
  155. std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
  156. return GetNextTunnel (m_InboundTunnels, excluded);
  157. }
  158. template<class TTunnels>
  159. typename TTunnels::value_type TunnelPool::GetNextTunnel (TTunnels& tunnels, typename TTunnels::value_type excluded) const
  160. {
  161. if (tunnels.empty ()) return nullptr;
  162. uint32_t ind = rand () % (tunnels.size ()/2 + 1), i = 0;
  163. typename TTunnels::value_type tunnel = nullptr;
  164. for (const auto& it: tunnels)
  165. {
  166. if (it->IsEstablished () && it != excluded)
  167. {
  168. if(HasLatencyRequirement() && it->LatencyIsKnown() && !it->LatencyFitsRange(m_MinLatency, m_MaxLatency)) {
  169. i ++;
  170. continue;
  171. }
  172. tunnel = it;
  173. i++;
  174. }
  175. if (i > ind && tunnel) break;
  176. }
  177. if(HasLatencyRequirement() && !tunnel) {
  178. ind = rand () % (tunnels.size ()/2 + 1), i = 0;
  179. for (const auto& it: tunnels)
  180. {
  181. if (it->IsEstablished () && it != excluded)
  182. {
  183. tunnel = it;
  184. i++;
  185. }
  186. if (i > ind && tunnel) break;
  187. }
  188. }
  189. if (!tunnel && excluded && excluded->IsEstablished ()) tunnel = excluded;
  190. return tunnel;
  191. }
  192. std::shared_ptr<OutboundTunnel> TunnelPool::GetNewOutboundTunnel (std::shared_ptr<OutboundTunnel> old) const
  193. {
  194. if (old && old->IsEstablished ()) return old;
  195. std::shared_ptr<OutboundTunnel> tunnel;
  196. if (old)
  197. {
  198. std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
  199. for (const auto& it: m_OutboundTunnels)
  200. if (it->IsEstablished () && old->GetEndpointIdentHash () == it->GetEndpointIdentHash ())
  201. {
  202. tunnel = it;
  203. break;
  204. }
  205. }
  206. if (!tunnel)
  207. tunnel = GetNextOutboundTunnel ();
  208. return tunnel;
  209. }
  210. void TunnelPool::CreateTunnels ()
  211. {
  212. int num = 0;
  213. {
  214. std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
  215. for (const auto& it : m_OutboundTunnels)
  216. if (it->IsEstablished ()) num++;
  217. }
  218. for (int i = num; i < m_NumOutboundTunnels; i++)
  219. CreateOutboundTunnel ();
  220. num = 0;
  221. {
  222. std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
  223. for (const auto& it : m_InboundTunnels)
  224. if (it->IsEstablished ()) num++;
  225. }
  226. for (int i = num; i < m_NumInboundTunnels; i++)
  227. CreateInboundTunnel ();
  228. if (num < m_NumInboundTunnels && m_NumInboundHops <= 0 && m_LocalDestination) // zero hops IB
  229. m_LocalDestination->SetLeaseSetUpdated (); // update LeaseSet immediately
  230. }
  231. void TunnelPool::TestTunnels ()
  232. {
  233. decltype(m_Tests) tests;
  234. {
  235. std::unique_lock<std::mutex> l(m_TestsMutex);
  236. tests.swap(m_Tests);
  237. }
  238. for (auto& it: tests)
  239. {
  240. LogPrint (eLogWarning, "Tunnels: test of tunnel ", it.first, " failed");
  241. // if test failed again with another tunnel we consider it failed
  242. if (it.second.first)
  243. {
  244. if (it.second.first->GetState () == eTunnelStateTestFailed)
  245. {
  246. it.second.first->SetState (eTunnelStateFailed);
  247. std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
  248. m_OutboundTunnels.erase (it.second.first);
  249. }
  250. else
  251. it.second.first->SetState (eTunnelStateTestFailed);
  252. }
  253. if (it.second.second)
  254. {
  255. if (it.second.second->GetState () == eTunnelStateTestFailed)
  256. {
  257. it.second.second->SetState (eTunnelStateFailed);
  258. {
  259. std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
  260. m_InboundTunnels.erase (it.second.second);
  261. }
  262. if (m_LocalDestination)
  263. m_LocalDestination->SetLeaseSetUpdated ();
  264. }
  265. else
  266. it.second.second->SetState (eTunnelStateTestFailed);
  267. }
  268. }
  269. // new tests
  270. auto it1 = m_OutboundTunnels.begin ();
  271. auto it2 = m_InboundTunnels.begin ();
  272. while (it1 != m_OutboundTunnels.end () && it2 != m_InboundTunnels.end ())
  273. {
  274. bool failed = false;
  275. if ((*it1)->IsFailed ())
  276. {
  277. failed = true;
  278. ++it1;
  279. }
  280. if ((*it2)->IsFailed ())
  281. {
  282. failed = true;
  283. ++it2;
  284. }
  285. if (!failed)
  286. {
  287. uint32_t msgID;
  288. RAND_bytes ((uint8_t *)&msgID, 4);
  289. {
  290. std::unique_lock<std::mutex> l(m_TestsMutex);
  291. m_Tests[msgID] = std::make_pair (*it1, *it2);
  292. }
  293. (*it1)->SendTunnelDataMsg ((*it2)->GetNextIdentHash (), (*it2)->GetNextTunnelID (),
  294. CreateDeliveryStatusMsg (msgID));
  295. ++it1; ++it2;
  296. }
  297. }
  298. }
  299. void TunnelPool::ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg)
  300. {
  301. if (m_LocalDestination)
  302. m_LocalDestination->ProcessGarlicMessage (msg);
  303. else
  304. LogPrint (eLogWarning, "Tunnels: local destination doesn't exist, dropped");
  305. }
  306. void TunnelPool::ProcessDeliveryStatus (std::shared_ptr<I2NPMessage> msg)
  307. {
  308. const uint8_t * buf = msg->GetPayload ();
  309. uint32_t msgID = bufbe32toh (buf);
  310. buf += 4;
  311. uint64_t timestamp = bufbe64toh (buf);
  312. decltype(m_Tests)::mapped_type test;
  313. bool found = false;
  314. {
  315. std::unique_lock<std::mutex> l(m_TestsMutex);
  316. auto it = m_Tests.find (msgID);
  317. if (it != m_Tests.end ())
  318. {
  319. found = true;
  320. test = it->second;
  321. m_Tests.erase (it);
  322. }
  323. }
  324. if (found)
  325. {
  326. // restore from test failed state if any
  327. if (test.first->GetState () == eTunnelStateTestFailed)
  328. test.first->SetState (eTunnelStateEstablished);
  329. if (test.second->GetState () == eTunnelStateTestFailed)
  330. test.second->SetState (eTunnelStateEstablished);
  331. uint64_t dlt = i2p::util::GetMillisecondsSinceEpoch () - timestamp;
  332. LogPrint (eLogDebug, "Tunnels: test of ", msgID, " successful. ", dlt, " milliseconds");
  333. // update latency
  334. uint64_t latency = dlt / 2;
  335. test.first->AddLatencySample(latency);
  336. test.second->AddLatencySample(latency);
  337. }
  338. else
  339. {
  340. if (m_LocalDestination)
  341. m_LocalDestination->ProcessDeliveryStatusMessage (msg);
  342. else
  343. LogPrint (eLogWarning, "Tunnels: Local destination doesn't exist, dropped");
  344. }
  345. }
  346. std::shared_ptr<const i2p::data::RouterInfo> TunnelPool::SelectNextHop (std::shared_ptr<const i2p::data::RouterInfo> prevHop) const
  347. {
  348. bool isExploratory = (i2p::tunnel::tunnels.GetExploratoryPool () == shared_from_this ());
  349. auto hop = isExploratory ? i2p::data::netdb.GetRandomRouter (prevHop):
  350. i2p::data::netdb.GetHighBandwidthRandomRouter (prevHop);
  351. if (!hop || hop->GetProfile ()->IsBad ())
  352. hop = i2p::data::netdb.GetRandomRouter (prevHop);
  353. return hop;
  354. }
  355. bool StandardSelectPeers(Path & peers, int numHops, bool inbound, SelectHopFunc nextHop)
  356. {
  357. auto prevHop = i2p::context.GetSharedRouterInfo ();
  358. if(i2p::transport::transports.RoutesRestricted())
  359. {
  360. /** if routes are restricted prepend trusted first hop */
  361. auto hop = i2p::transport::transports.GetRestrictedPeer();
  362. if(!hop) return false;
  363. peers.push_back(hop->GetRouterIdentity());
  364. prevHop = hop;
  365. }
  366. else if (i2p::transport::transports.GetNumPeers () > 25)
  367. {
  368. auto r = i2p::transport::transports.GetRandomPeer ();
  369. if (r && !r->GetProfile ()->IsBad ())
  370. {
  371. prevHop = r;
  372. peers.push_back (r->GetRouterIdentity ());
  373. numHops--;
  374. }
  375. }
  376. for(int i = 0; i < numHops; i++ )
  377. {
  378. auto hop = nextHop (prevHop);
  379. if (!hop)
  380. {
  381. LogPrint (eLogError, "Tunnels: Can't select next hop for ", prevHop->GetIdentHashBase64 ());
  382. return false;
  383. }
  384. prevHop = hop;
  385. peers.push_back (hop->GetRouterIdentity ());
  386. }
  387. return true;
  388. }
  389. bool TunnelPool::SelectPeers (std::vector<std::shared_ptr<const i2p::data::IdentityEx> >& peers, bool isInbound)
  390. {
  391. int numHops = isInbound ? m_NumInboundHops : m_NumOutboundHops;
  392. // peers is empty
  393. if (numHops <= 0) return true;
  394. // custom peer selector in use ?
  395. {
  396. std::lock_guard<std::mutex> lock(m_CustomPeerSelectorMutex);
  397. if (m_CustomPeerSelector)
  398. return m_CustomPeerSelector->SelectPeers(peers, numHops, isInbound);
  399. }
  400. // explicit peers in use
  401. if (m_ExplicitPeers) return SelectExplicitPeers (peers, isInbound);
  402. return StandardSelectPeers(peers, numHops, isInbound, std::bind(&TunnelPool::SelectNextHop, this, std::placeholders::_1));
  403. }
  404. bool TunnelPool::SelectExplicitPeers (std::vector<std::shared_ptr<const i2p::data::IdentityEx> >& peers, bool isInbound)
  405. {
  406. int size = m_ExplicitPeers->size ();
  407. std::vector<int> peerIndicies;
  408. for (int i = 0; i < size; i++) peerIndicies.push_back(i);
  409. std::random_shuffle (peerIndicies.begin(), peerIndicies.end());
  410. int numHops = isInbound ? m_NumInboundHops : m_NumOutboundHops;
  411. for (int i = 0; i < numHops; i++)
  412. {
  413. auto& ident = (*m_ExplicitPeers)[peerIndicies[i]];
  414. auto r = i2p::data::netdb.FindRouter (ident);
  415. if (r)
  416. peers.push_back (r->GetRouterIdentity ());
  417. else
  418. {
  419. LogPrint (eLogInfo, "Tunnels: Can't find router for ", ident.ToBase64 ());
  420. i2p::data::netdb.RequestDestination (ident);
  421. return false;
  422. }
  423. }
  424. return true;
  425. }
  426. void TunnelPool::CreateInboundTunnel ()
  427. {
  428. auto outboundTunnel = GetNextOutboundTunnel ();
  429. if (!outboundTunnel)
  430. outboundTunnel = tunnels.GetNextOutboundTunnel ();
  431. LogPrint (eLogDebug, "Tunnels: Creating destination inbound tunnel...");
  432. std::vector<std::shared_ptr<const i2p::data::IdentityEx> > peers;
  433. if (SelectPeers (peers, true))
  434. {
  435. std::shared_ptr<TunnelConfig> config;
  436. if (m_NumInboundHops > 0)
  437. {
  438. std::reverse (peers.begin (), peers.end ());
  439. config = std::make_shared<TunnelConfig> (peers);
  440. }
  441. auto tunnel = tunnels.CreateInboundTunnel (config, outboundTunnel);
  442. tunnel->SetTunnelPool (shared_from_this ());
  443. if (tunnel->IsEstablished ()) // zero hops
  444. TunnelCreated (tunnel);
  445. }
  446. else
  447. LogPrint (eLogError, "Tunnels: Can't create inbound tunnel, no peers available");
  448. }
  449. void TunnelPool::RecreateInboundTunnel (std::shared_ptr<InboundTunnel> tunnel)
  450. {
  451. auto outboundTunnel = GetNextOutboundTunnel ();
  452. if (!outboundTunnel)
  453. outboundTunnel = tunnels.GetNextOutboundTunnel ();
  454. LogPrint (eLogDebug, "Tunnels: Re-creating destination inbound tunnel...");
  455. std::shared_ptr<TunnelConfig> config;
  456. if (m_NumInboundHops > 0 && tunnel->GetPeers().size())
  457. {
  458. config = std::make_shared<TunnelConfig>(tunnel->GetPeers ());
  459. }
  460. if (m_NumInboundHops == 0 || config)
  461. {
  462. auto newTunnel = tunnels.CreateInboundTunnel (config, outboundTunnel);
  463. newTunnel->SetTunnelPool (shared_from_this());
  464. if (newTunnel->IsEstablished ()) // zero hops
  465. TunnelCreated (newTunnel);
  466. }
  467. }
  468. void TunnelPool::CreateOutboundTunnel ()
  469. {
  470. auto inboundTunnel = GetNextInboundTunnel ();
  471. if (!inboundTunnel)
  472. inboundTunnel = tunnels.GetNextInboundTunnel ();
  473. if (inboundTunnel)
  474. {
  475. LogPrint (eLogDebug, "Tunnels: Creating destination outbound tunnel...");
  476. std::vector<std::shared_ptr<const i2p::data::IdentityEx> > peers;
  477. if (SelectPeers (peers, false))
  478. {
  479. std::shared_ptr<TunnelConfig> config;
  480. if (m_NumOutboundHops > 0)
  481. config = std::make_shared<TunnelConfig>(peers, inboundTunnel->GetNextTunnelID (), inboundTunnel->GetNextIdentHash ());
  482. auto tunnel = tunnels.CreateOutboundTunnel (config);
  483. tunnel->SetTunnelPool (shared_from_this ());
  484. if (tunnel->IsEstablished ()) // zero hops
  485. TunnelCreated (tunnel);
  486. }
  487. else
  488. LogPrint (eLogError, "Tunnels: Can't create outbound tunnel, no peers available");
  489. }
  490. else
  491. LogPrint (eLogError, "Tunnels: Can't create outbound tunnel, no inbound tunnels found");
  492. }
  493. void TunnelPool::RecreateOutboundTunnel (std::shared_ptr<OutboundTunnel> tunnel)
  494. {
  495. auto inboundTunnel = GetNextInboundTunnel ();
  496. if (!inboundTunnel)
  497. inboundTunnel = tunnels.GetNextInboundTunnel ();
  498. if (inboundTunnel)
  499. {
  500. LogPrint (eLogDebug, "Tunnels: Re-creating destination outbound tunnel...");
  501. std::shared_ptr<TunnelConfig> config;
  502. if (m_NumOutboundHops > 0 && tunnel->GetPeers().size())
  503. {
  504. config = std::make_shared<TunnelConfig>(tunnel->GetPeers (), inboundTunnel->GetNextTunnelID (), inboundTunnel->GetNextIdentHash ());
  505. }
  506. if(m_NumOutboundHops == 0 || config)
  507. {
  508. auto newTunnel = tunnels.CreateOutboundTunnel (config);
  509. newTunnel->SetTunnelPool (shared_from_this ());
  510. if (newTunnel->IsEstablished ()) // zero hops
  511. TunnelCreated (newTunnel);
  512. }
  513. }
  514. else
  515. LogPrint (eLogDebug, "Tunnels: Can't re-create outbound tunnel, no inbound tunnels found");
  516. }
  517. void TunnelPool::CreatePairedInboundTunnel (std::shared_ptr<OutboundTunnel> outboundTunnel)
  518. {
  519. LogPrint (eLogDebug, "Tunnels: Creating paired inbound tunnel...");
  520. auto tunnel = tunnels.CreateInboundTunnel (std::make_shared<TunnelConfig>(outboundTunnel->GetInvertedPeers ()), outboundTunnel);
  521. tunnel->SetTunnelPool (shared_from_this ());
  522. }
  523. void TunnelPool::SetCustomPeerSelector(ITunnelPeerSelector * selector)
  524. {
  525. std::lock_guard<std::mutex> lock(m_CustomPeerSelectorMutex);
  526. m_CustomPeerSelector = selector;
  527. }
  528. void TunnelPool::UnsetCustomPeerSelector()
  529. {
  530. SetCustomPeerSelector(nullptr);
  531. }
  532. bool TunnelPool::HasCustomPeerSelector()
  533. {
  534. std::lock_guard<std::mutex> lock(m_CustomPeerSelectorMutex);
  535. return m_CustomPeerSelector != nullptr;
  536. }
  537. std::shared_ptr<InboundTunnel> TunnelPool::GetLowestLatencyInboundTunnel(std::shared_ptr<InboundTunnel> exclude) const
  538. {
  539. std::shared_ptr<InboundTunnel> tun = nullptr;
  540. std::unique_lock<std::mutex> lock(m_InboundTunnelsMutex);
  541. uint64_t min = 1000000;
  542. for (const auto & itr : m_InboundTunnels) {
  543. if(!itr->LatencyIsKnown()) continue;
  544. auto l = itr->GetMeanLatency();
  545. if (l >= min) continue;
  546. tun = itr;
  547. if(tun == exclude) continue;
  548. min = l;
  549. }
  550. return tun;
  551. }
  552. std::shared_ptr<OutboundTunnel> TunnelPool::GetLowestLatencyOutboundTunnel(std::shared_ptr<OutboundTunnel> exclude) const
  553. {
  554. std::shared_ptr<OutboundTunnel> tun = nullptr;
  555. std::unique_lock<std::mutex> lock(m_OutboundTunnelsMutex);
  556. uint64_t min = 1000000;
  557. for (const auto & itr : m_OutboundTunnels) {
  558. if(!itr->LatencyIsKnown()) continue;
  559. auto l = itr->GetMeanLatency();
  560. if (l >= min) continue;
  561. tun = itr;
  562. if(tun == exclude) continue;
  563. min = l;
  564. }
  565. return tun;
  566. }
  567. void TunnelPool::OnTunnelBuildResult(std::shared_ptr<Tunnel> tunnel, TunnelBuildResult result)
  568. {
  569. auto peers = tunnel->GetPeers();
  570. if(m_CustomPeerSelector) m_CustomPeerSelector->OnBuildResult(peers, tunnel->IsInbound(), result);
  571. }
  572. }
  573. }