mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 08:14:15 +00:00
select first hop for inbound tunnel from connected peers
This commit is contained in:
parent
9a9b38a8c3
commit
c896f6d0d7
@ -70,7 +70,7 @@ namespace tunnel
|
|||||||
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
|
||||||
m_OutboundTunnels.insert (createdTunnel);
|
m_OutboundTunnels.insert (createdTunnel);
|
||||||
}
|
}
|
||||||
CreatePairedInboundTunnel (createdTunnel);
|
//CreatePairedInboundTunnel (createdTunnel);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TunnelPool::TunnelExpired (std::shared_ptr<OutboundTunnel> expiredTunnel)
|
void TunnelPool::TunnelExpired (std::shared_ptr<OutboundTunnel> expiredTunnel)
|
||||||
@ -290,6 +290,35 @@ namespace tunnel
|
|||||||
hop = i2p::data::netdb.GetRandomRouter ();
|
hop = i2p::data::netdb.GetRandomRouter ();
|
||||||
return hop;
|
return hop;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool TunnelPool::SelectPeers (std::vector<std::shared_ptr<const i2p::data::RouterInfo> >& hops)
|
||||||
|
{
|
||||||
|
auto prevHop = i2p::context.GetSharedRouterInfo ();
|
||||||
|
int numHops = m_NumInboundHops;
|
||||||
|
if (i2p::transport::transports.GetNumPeers () > 25)
|
||||||
|
{
|
||||||
|
auto r = i2p::transport::transports.GetRandomPeer ();
|
||||||
|
if (r && !r->GetProfile ()->IsBad ())
|
||||||
|
{
|
||||||
|
prevHop = r;
|
||||||
|
hops.push_back (r);
|
||||||
|
numHops--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < numHops; i++)
|
||||||
|
{
|
||||||
|
auto hop = SelectNextHop (prevHop);
|
||||||
|
if (!hop)
|
||||||
|
{
|
||||||
|
LogPrint (eLogError, "Can't select next hop");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
prevHop = hop;
|
||||||
|
hops.push_back (hop);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void TunnelPool::CreateInboundTunnel ()
|
void TunnelPool::CreateInboundTunnel ()
|
||||||
{
|
{
|
||||||
@ -297,34 +326,15 @@ namespace tunnel
|
|||||||
if (!outboundTunnel)
|
if (!outboundTunnel)
|
||||||
outboundTunnel = tunnels.GetNextOutboundTunnel ();
|
outboundTunnel = tunnels.GetNextOutboundTunnel ();
|
||||||
LogPrint ("Creating destination inbound tunnel...");
|
LogPrint ("Creating destination inbound tunnel...");
|
||||||
auto prevHop = i2p::context.GetSharedRouterInfo ();
|
|
||||||
std::vector<std::shared_ptr<const i2p::data::RouterInfo> > hops;
|
std::vector<std::shared_ptr<const i2p::data::RouterInfo> > hops;
|
||||||
int numHops = m_NumInboundHops;
|
if (SelectPeers (hops))
|
||||||
if (outboundTunnel)
|
|
||||||
{
|
|
||||||
// last hop
|
|
||||||
auto hop = outboundTunnel->GetTunnelConfig ()->GetFirstHop ()->router;
|
|
||||||
if (hop->GetIdentHash () != i2p::context.GetIdentHash ()) // outbound shouldn't be zero-hop tunnel
|
|
||||||
{
|
|
||||||
prevHop = hop;
|
|
||||||
hops.push_back (prevHop);
|
|
||||||
numHops--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int i = 0; i < numHops; i++)
|
|
||||||
{
|
{
|
||||||
auto hop = SelectNextHop (prevHop);
|
std::reverse (hops.begin (), hops.end ());
|
||||||
if (!hop)
|
auto tunnel = tunnels.CreateTunnel<InboundTunnel> (std::make_shared<TunnelConfig> (hops), outboundTunnel);
|
||||||
{
|
tunnel->SetTunnelPool (shared_from_this ());
|
||||||
LogPrint (eLogError, "Can't select next hop for inbound tunnel");
|
}
|
||||||
return;
|
else
|
||||||
}
|
LogPrint (eLogError, "Can't create inbound tunnel. No peers available");
|
||||||
prevHop = hop;
|
|
||||||
hops.push_back (hop);
|
|
||||||
}
|
|
||||||
std::reverse (hops.begin (), hops.end ());
|
|
||||||
auto tunnel = tunnels.CreateTunnel<InboundTunnel> (std::make_shared<TunnelConfig> (hops), outboundTunnel);
|
|
||||||
tunnel->SetTunnelPool (shared_from_this ());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TunnelPool::RecreateInboundTunnel (std::shared_ptr<InboundTunnel> tunnel)
|
void TunnelPool::RecreateInboundTunnel (std::shared_ptr<InboundTunnel> tunnel)
|
||||||
@ -345,34 +355,15 @@ namespace tunnel
|
|||||||
if (inboundTunnel)
|
if (inboundTunnel)
|
||||||
{
|
{
|
||||||
LogPrint ("Creating destination outbound tunnel...");
|
LogPrint ("Creating destination outbound tunnel...");
|
||||||
int numHops = m_NumOutboundHops;
|
|
||||||
auto prevHop = i2p::context.GetSharedRouterInfo ();
|
|
||||||
std::vector<std::shared_ptr<const i2p::data::RouterInfo> > hops;
|
std::vector<std::shared_ptr<const i2p::data::RouterInfo> > hops;
|
||||||
if (i2p::transport::transports.GetNumPeers () > 25)
|
if (SelectPeers (hops))
|
||||||
{
|
{
|
||||||
auto r = i2p::transport::transports.GetRandomPeer ();
|
auto tunnel = tunnels.CreateTunnel<OutboundTunnel> (
|
||||||
if (r)
|
std::make_shared<TunnelConfig> (hops, inboundTunnel->GetTunnelConfig ()));
|
||||||
{
|
tunnel->SetTunnelPool (shared_from_this ());
|
||||||
prevHop = r;
|
|
||||||
hops.push_back (r);
|
|
||||||
numHops--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int i = 0; i < numHops; i++)
|
|
||||||
{
|
|
||||||
auto hop = SelectNextHop (prevHop);
|
|
||||||
if (!hop)
|
|
||||||
{
|
|
||||||
LogPrint (eLogError, "Can't select next hop for outbound tunnel");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
prevHop = hop;
|
|
||||||
hops.push_back (hop);
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
auto tunnel = tunnels.CreateTunnel<OutboundTunnel> (
|
LogPrint (eLogError, "Can't create outbound tunnel. No peers available");
|
||||||
std::make_shared<TunnelConfig> (hops, inboundTunnel->GetTunnelConfig ()));
|
|
||||||
tunnel->SetTunnelPool (shared_from_this ());
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
LogPrint (eLogError, "Can't create outbound tunnel. No inbound tunnels found");
|
LogPrint (eLogError, "Can't create outbound tunnel. No inbound tunnels found");
|
||||||
|
@ -61,6 +61,7 @@ namespace tunnel
|
|||||||
template<class TTunnels>
|
template<class TTunnels>
|
||||||
typename TTunnels::value_type GetNextTunnel (TTunnels& tunnels, typename TTunnels::value_type excluded) const;
|
typename TTunnels::value_type GetNextTunnel (TTunnels& tunnels, typename TTunnels::value_type excluded) const;
|
||||||
std::shared_ptr<const i2p::data::RouterInfo> SelectNextHop (std::shared_ptr<const i2p::data::RouterInfo> prevHop) const;
|
std::shared_ptr<const i2p::data::RouterInfo> SelectNextHop (std::shared_ptr<const i2p::data::RouterInfo> prevHop) const;
|
||||||
|
bool SelectPeers (std::vector<std::shared_ptr<const i2p::data::RouterInfo> >& hops);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user