Skip to content
/ i2pd Public
forked from PurpleI2P/i2pd

Commit

Permalink
select router with ipv4 for endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jul 11, 2023
1 parent 5022a9c commit 17c4038
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 25 deletions.
19 changes: 12 additions & 7 deletions libi2pd/NetDb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace data
{
Reseed ();
}
else if (!GetRandomRouter (i2p::context.GetSharedRouterInfo (), false))
else if (!GetRandomRouter (i2p::context.GetSharedRouterInfo (), false, false))
Reseed (); // we don't have a router we can connect to. Trying to reseed

auto it = m_RouterInfos.find (i2p::context.GetIdentHash ());
Expand Down Expand Up @@ -1199,15 +1199,17 @@ namespace data
});
}

std::shared_ptr<const RouterInfo> NetDb::GetRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith, bool reverse) const
std::shared_ptr<const RouterInfo> NetDb::GetRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith,
bool reverse, bool endpoint) const
{
return GetRandomRouter (
[compatibleWith, reverse](std::shared_ptr<const RouterInfo> router)->bool
[compatibleWith, reverse, endpoint](std::shared_ptr<const RouterInfo> router)->bool
{
return !router->IsHidden () && router != compatibleWith &&
(reverse ? compatibleWith->IsReachableFrom (*router) :
router->IsReachableFrom (*compatibleWith)) &&
router->IsECIES () && !router->IsHighCongestion (false);
router->IsECIES () && !router->IsHighCongestion (false) &&
(!endpoint || (router->IsV4 () && (!reverse || router->IsPublished (true)))); // endpoint must be ipv4 and published if inbound(reverse)
});
}

Expand All @@ -1231,17 +1233,20 @@ namespace data
});
}

std::shared_ptr<const RouterInfo> NetDb::GetHighBandwidthRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith, bool reverse) const
std::shared_ptr<const RouterInfo> NetDb::GetHighBandwidthRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith,
bool reverse, bool endpoint) const
{
return GetRandomRouter (
[compatibleWith, reverse](std::shared_ptr<const RouterInfo> router)->bool
[compatibleWith, reverse, endpoint](std::shared_ptr<const RouterInfo> router)->bool
{
return !router->IsHidden () && router != compatibleWith &&
(reverse ? compatibleWith->IsReachableFrom (*router) :
router->IsReachableFrom (*compatibleWith)) &&
(router->GetCaps () & RouterInfo::eHighBandwidth) &&
router->GetVersion () >= NETDB_MIN_HIGHBANDWIDTH_VERSION &&
router->IsECIES () && !router->IsHighCongestion (true);
router->IsECIES () && !router->IsHighCongestion (true) &&
(!endpoint || (router->IsV4 () && (!reverse || router->IsPublished (true)))); // endpoint must be ipv4 and published if inbound(reverse)

});
}

Expand Down
4 changes: 2 additions & 2 deletions libi2pd/NetDb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ namespace data
void HandleNTCP2RouterInfoMsg (std::shared_ptr<const I2NPMessage> m);

std::shared_ptr<const RouterInfo> GetRandomRouter () const;
std::shared_ptr<const RouterInfo> GetRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith, bool reverse) const;
std::shared_ptr<const RouterInfo> GetHighBandwidthRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith, bool reverse) const;
std::shared_ptr<const RouterInfo> GetRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith, bool reverse, bool endpoint) const;
std::shared_ptr<const RouterInfo> GetHighBandwidthRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith, bool reverse, bool endpoint) const;
std::shared_ptr<const RouterInfo> GetRandomSSU2PeerTestRouter (bool v4, const std::set<IdentHash>& excluded) const;
std::shared_ptr<const RouterInfo> GetRandomSSU2Introducer (bool v4, const std::set<IdentHash>& excluded) const;
std::shared_ptr<const RouterInfo> GetClosestFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded) const;
Expand Down
4 changes: 2 additions & 2 deletions libi2pd/Tunnel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ namespace tunnel
auto inboundTunnel = GetNextInboundTunnel ();
auto router = i2p::transport::transports.RoutesRestricted() ?
i2p::transport::transports.GetRestrictedPeer() :
i2p::data::netdb.GetRandomRouter (i2p::context.GetSharedRouterInfo (), false); // reachable by us
i2p::data::netdb.GetRandomRouter (i2p::context.GetSharedRouterInfo (), false, true); // reachable by us
if (!inboundTunnel || !router) return;
LogPrint (eLogDebug, "Tunnel: Creating one hop outbound tunnel");
CreateTunnel<OutboundTunnel> (
Expand Down Expand Up @@ -781,7 +781,7 @@ namespace tunnel
auto router = i2p::transport::transports.RoutesRestricted() ?
i2p::transport::transports.GetRestrictedPeer() :
// should be reachable by us because we send build request directly
i2p::data::netdb.GetRandomRouter (i2p::context.GetSharedRouterInfo (), false);
i2p::data::netdb.GetRandomRouter (i2p::context.GetSharedRouterInfo (), false, true);
if (!router) {
LogPrint (eLogWarning, "Tunnel: Can't find any router, skip creating tunnel");
return;
Expand Down
19 changes: 8 additions & 11 deletions libi2pd/TunnelPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,13 +470,14 @@ namespace tunnel
return i2p::tunnel::tunnels.GetExploratoryPool () == shared_from_this ();
}

std::shared_ptr<const i2p::data::RouterInfo> TunnelPool::SelectNextHop (std::shared_ptr<const i2p::data::RouterInfo> prevHop, bool reverse) const
std::shared_ptr<const i2p::data::RouterInfo> TunnelPool::SelectNextHop (std::shared_ptr<const i2p::data::RouterInfo> prevHop,
bool reverse, bool endpoint) const
{
auto hop = IsExploratory () ? i2p::data::netdb.GetRandomRouter (prevHop, reverse):
i2p::data::netdb.GetHighBandwidthRandomRouter (prevHop, reverse);
auto hop = IsExploratory () ? i2p::data::netdb.GetRandomRouter (prevHop, reverse, endpoint):
i2p::data::netdb.GetHighBandwidthRandomRouter (prevHop, reverse, endpoint);

if (!hop || hop->GetProfile ()->IsBad ())
hop = i2p::data::netdb.GetRandomRouter (prevHop, reverse);
hop = i2p::data::netdb.GetRandomRouter (prevHop, reverse, endpoint);
return hop;
}

Expand Down Expand Up @@ -508,7 +509,7 @@ namespace tunnel

for(int i = start; i < numHops; i++ )
{
auto hop = nextHop (prevHop, inbound);
auto hop = nextHop (prevHop, inbound, i == numHops - 1);
if (!hop && !i) // if no suitable peer found for first hop, try already connected
{
LogPrint (eLogInfo, "Tunnels: Can't select first hop for a tunnel. Trying already connected");
Expand All @@ -520,11 +521,6 @@ namespace tunnel
LogPrint (eLogError, "Tunnels: Can't select next hop for ", prevHop->GetIdentHashBase64 ());
return false;
}
if ((i == numHops - 1) && (!hop->IsV4 () || (inbound && !hop->IsPublished (true)))) // IBGW is not published ipv4
{
auto hop1 = nextHop (prevHop, inbound);
if (hop1) hop = hop1;
}
prevHop = hop;
path.Add (hop);
}
Expand Down Expand Up @@ -566,7 +562,8 @@ namespace tunnel
if (m_CustomPeerSelector)
return m_CustomPeerSelector->SelectPeers(path, numHops, isInbound);
}
return StandardSelectPeers(path, numHops, isInbound, std::bind(&TunnelPool::SelectNextHop, this, std::placeholders::_1, std::placeholders::_2));
return StandardSelectPeers(path, numHops, isInbound, std::bind(&TunnelPool::SelectNextHop, this,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
}

bool TunnelPool::SelectExplicitPeers (Path& path, bool isInbound)
Expand Down
4 changes: 2 additions & 2 deletions libi2pd/TunnelPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace tunnel

class TunnelPool: public std::enable_shared_from_this<TunnelPool> // per local destination
{
typedef std::function<std::shared_ptr<const i2p::data::RouterInfo>(std::shared_ptr<const i2p::data::RouterInfo>, bool)> SelectHopFunc;
typedef std::function<std::shared_ptr<const i2p::data::RouterInfo>(std::shared_ptr<const i2p::data::RouterInfo>, bool, bool)> SelectHopFunc;
public:

TunnelPool (int numInboundHops, int numOutboundHops, int numInboundTunnels,
Expand Down Expand Up @@ -112,7 +112,7 @@ namespace tunnel
std::shared_ptr<OutboundTunnel> GetLowestLatencyOutboundTunnel(std::shared_ptr<OutboundTunnel> exclude = nullptr) const;

// for overriding tunnel peer selection
std::shared_ptr<const i2p::data::RouterInfo> SelectNextHop (std::shared_ptr<const i2p::data::RouterInfo> prevHop, bool reverse) const;
std::shared_ptr<const i2p::data::RouterInfo> SelectNextHop (std::shared_ptr<const i2p::data::RouterInfo> prevHop, bool reverse, bool endpoint) const;
bool StandardSelectPeers(Path & path, int numHops, bool inbound, SelectHopFunc nextHop);

private:
Expand Down
3 changes: 2 additions & 1 deletion libi2pd_client/MatchedDestination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ namespace client
{
auto pool = GetTunnelPool();
if(!pool || !pool->StandardSelectPeers(path, hops, inbound,
std::bind(&i2p::tunnel::TunnelPool::SelectNextHop, pool, std::placeholders::_1, std::placeholders::_2)))
std::bind(&i2p::tunnel::TunnelPool::SelectNextHop, pool, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3)))
return false;
// more here for outbound tunnels
if(!inbound && m_RemoteLeaseSet)
Expand Down

0 comments on commit 17c4038

Please sign in to comment.