i2pd/libi2pd/NetDbRequests.cpp

162 lines
4.6 KiB
C++
Raw Normal View History

2015-04-09 16:45:00 +00:00
#include "Log.h"
#include "I2NPProtocol.h"
#include "Transports.h"
#include "NetDb.hpp"
2015-04-09 16:45:00 +00:00
#include "NetDbRequests.h"
namespace i2p
{
namespace data
{
2015-06-22 19:47:45 +00:00
std::shared_ptr<I2NPMessage> RequestedDestination::CreateRequestMessage (std::shared_ptr<const RouterInfo> router,
2015-04-09 16:45:00 +00:00
std::shared_ptr<const i2p::tunnel::InboundTunnel> replyTunnel)
{
2016-11-14 17:05:44 +00:00
std::shared_ptr<I2NPMessage> msg;
if(replyTunnel)
msg = i2p::CreateRouterInfoDatabaseLookupMsg (m_Destination,
2015-04-09 16:45:00 +00:00
replyTunnel->GetNextIdentHash (), replyTunnel->GetNextTunnelID (), m_IsExploratory,
&m_ExcludedPeers);
2016-11-14 17:05:44 +00:00
else
msg = i2p::CreateRouterInfoDatabaseLookupMsg(m_Destination, i2p::context.GetIdentHash(), 0, m_IsExploratory, &m_ExcludedPeers);
if(router)
m_ExcludedPeers.insert (router->GetIdentHash ());
2015-04-09 16:45:00 +00:00
m_CreationTime = i2p::util::GetSecondsSinceEpoch ();
2015-11-03 14:15:49 +00:00
return msg;
2015-04-09 16:45:00 +00:00
}
std::shared_ptr<I2NPMessage> RequestedDestination::CreateRequestMessage (const IdentHash& floodfill)
2015-04-09 16:45:00 +00:00
{
2015-11-03 14:15:49 +00:00
auto msg = i2p::CreateRouterInfoDatabaseLookupMsg (m_Destination,
2015-04-09 16:45:00 +00:00
i2p::context.GetRouterInfo ().GetIdentHash () , 0, false, &m_ExcludedPeers);
m_ExcludedPeers.insert (floodfill);
m_CreationTime = i2p::util::GetSecondsSinceEpoch ();
2015-11-03 14:15:49 +00:00
return msg;
2015-04-09 16:45:00 +00:00
}
void RequestedDestination::ClearExcludedPeers ()
{
m_ExcludedPeers.clear ();
}
void RequestedDestination::Success (std::shared_ptr<RouterInfo> r)
{
if (m_RequestComplete)
{
m_RequestComplete (r);
m_RequestComplete = nullptr;
}
}
void RequestedDestination::Fail ()
{
if (m_RequestComplete)
{
m_RequestComplete (nullptr);
m_RequestComplete = nullptr;
}
}
void NetDbRequests::Start ()
{
}
void NetDbRequests::Stop ()
{
m_RequestedDestinations.clear ();
}
std::shared_ptr<RequestedDestination> NetDbRequests::CreateRequest (const IdentHash& destination, bool isExploratory, RequestedDestination::RequestComplete requestComplete)
{
// request RouterInfo directly
auto dest = std::make_shared<RequestedDestination> (destination, isExploratory);
dest->SetRequestComplete (requestComplete);
{
std::unique_lock<std::mutex> l(m_RequestedDestinationsMutex);
2016-08-16 02:36:58 +00:00
if (!m_RequestedDestinations.insert (std::make_pair (destination, dest)).second) // not inserted
2015-04-09 16:45:00 +00:00
return nullptr;
}
return dest;
}
void NetDbRequests::RequestComplete (const IdentHash& ident, std::shared_ptr<RouterInfo> r)
{
2016-08-16 02:36:58 +00:00
std::shared_ptr<RequestedDestination> request;
{
std::unique_lock<std::mutex> l(m_RequestedDestinationsMutex);
auto it = m_RequestedDestinations.find (ident);
if (it != m_RequestedDestinations.end ())
{
request = it->second;
m_RequestedDestinations.erase (it);
}
}
if (request)
{
2015-04-09 16:45:00 +00:00
if (r)
2016-08-16 02:36:58 +00:00
request->Success (r);
2015-04-09 16:45:00 +00:00
else
2016-08-16 02:36:58 +00:00
request->Fail ();
2015-04-09 16:45:00 +00:00
}
}
std::shared_ptr<RequestedDestination> NetDbRequests::FindRequest (const IdentHash& ident) const
{
2016-08-16 02:36:58 +00:00
std::unique_lock<std::mutex> l(m_RequestedDestinationsMutex);
2015-04-09 16:45:00 +00:00
auto it = m_RequestedDestinations.find (ident);
if (it != m_RequestedDestinations.end ())
return it->second;
return nullptr;
}
void NetDbRequests::ManageRequests ()
{
uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
std::unique_lock<std::mutex> l(m_RequestedDestinationsMutex);
for (auto it = m_RequestedDestinations.begin (); it != m_RequestedDestinations.end ();)
{
auto& dest = it->second;
bool done = false;
if (ts < dest->GetCreationTime () + 60) // request is worthless after 1 minute
{
if (ts > dest->GetCreationTime () + 5) // no response for 5 seconds
{
auto count = dest->GetExcludedPeers ().size ();
if (!dest->IsExploratory () && count < 7)
{
auto pool = i2p::tunnel::tunnels.GetExploratoryPool ();
auto outbound = pool->GetNextOutboundTunnel ();
auto inbound = pool->GetNextInboundTunnel ();
auto nextFloodfill = netdb.GetClosestFloodfill (dest->GetDestination (), dest->GetExcludedPeers ());
if (nextFloodfill && outbound && inbound)
outbound->SendTunnelDataMsg (nextFloodfill->GetIdentHash (), 0,
2015-06-22 19:47:45 +00:00
dest->CreateRequestMessage (nextFloodfill, inbound));
2015-04-09 16:45:00 +00:00
else
{
done = true;
2016-01-18 00:00:00 +00:00
if (!inbound) LogPrint (eLogWarning, "NetDbReq: No inbound tunnels");
if (!outbound) LogPrint (eLogWarning, "NetDbReq: No outbound tunnels");
if (!nextFloodfill) LogPrint (eLogWarning, "NetDbReq: No more floodfills");
2015-04-09 16:45:00 +00:00
}
}
else
{
if (!dest->IsExploratory ())
2016-01-18 00:00:00 +00:00
LogPrint (eLogWarning, "NetDbReq: ", dest->GetDestination ().ToBase64 (), " not found after 7 attempts");
2015-04-09 16:45:00 +00:00
done = true;
}
}
}
else // delete obsolete request
done = true;
if (done)
it = m_RequestedDestinations.erase (it);
else
2016-08-05 18:23:54 +00:00
++it;
2015-04-09 16:45:00 +00:00
}
}
}
}