2019-04-30 01:06:20 +00:00
|
|
|
#include <service/endpoint_util.hpp>
|
|
|
|
|
2019-07-15 09:15:51 +00:00
|
|
|
#include <exit/session.hpp>
|
2019-04-30 01:06:20 +00:00
|
|
|
#include <service/outbound_context.hpp>
|
2019-07-15 09:15:51 +00:00
|
|
|
#include <service/lookup.hpp>
|
2019-09-01 12:10:49 +00:00
|
|
|
#include <util/logging/logger.hpp>
|
2019-04-30 01:06:20 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace service
|
|
|
|
{
|
|
|
|
void
|
2019-07-15 09:15:51 +00:00
|
|
|
EndpointUtil::ExpireSNodeSessions(llarp_time_t now, SNodeSessions& sessions)
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
|
|
|
auto itr = sessions.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != sessions.end())
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr->second.first->ShouldRemove() && itr->second.first->IsStopped())
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
|
|
|
itr = sessions.erase(itr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// expunge next tick
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr->second.first->IsExpired(now))
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
2019-07-01 14:56:56 +00:00
|
|
|
itr->second.first->Stop();
|
2019-04-30 01:06:20 +00:00
|
|
|
}
|
2019-04-30 21:36:27 +00:00
|
|
|
else
|
|
|
|
{
|
2019-07-01 14:56:56 +00:00
|
|
|
itr->second.first->Tick(now);
|
2019-04-30 21:36:27 +00:00
|
|
|
}
|
2019-04-30 01:06:20 +00:00
|
|
|
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-07-15 09:15:51 +00:00
|
|
|
EndpointUtil::ExpirePendingTx(llarp_time_t now, PendingLookups& lookups)
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
for (auto itr = lookups.begin(); itr != lookups.end();)
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!itr->second->IsTimedOut(now))
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
|
|
|
++itr;
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
std::unique_ptr<IServiceLookup> lookup = std::move(itr->second);
|
2019-04-30 01:06:20 +00:00
|
|
|
|
2019-05-05 14:15:48 +00:00
|
|
|
LogWarn(lookup->name, " timed out txid=", lookup->txid);
|
2020-09-17 19:18:08 +00:00
|
|
|
lookup->HandleTimeout();
|
2019-04-30 01:06:20 +00:00
|
|
|
itr = lookups.erase(itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
EndpointUtil::ExpirePendingRouterLookups(llarp_time_t now, PendingRouters& routers)
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
for (auto itr = routers.begin(); itr != routers.end();)
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!itr->second.IsExpired(now))
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
|
|
|
++itr;
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-05 14:15:48 +00:00
|
|
|
LogWarn("lookup for ", itr->first, " timed out");
|
2019-05-05 14:42:13 +00:00
|
|
|
itr->second.InformResult({});
|
2019-04-30 01:06:20 +00:00
|
|
|
itr = routers.erase(itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-07-15 09:15:51 +00:00
|
|
|
EndpointUtil::DeregisterDeadSessions(llarp_time_t now, Sessions& sessions)
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
|
|
|
auto itr = sessions.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != sessions.end())
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr->second->IsDone(now))
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
|
|
|
itr = sessions.erase(itr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
EndpointUtil::TickRemoteSessions(
|
|
|
|
llarp_time_t now, Sessions& remoteSessions, Sessions& deadSessions, ConvoMap& sessions)
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
|
|
|
auto itr = remoteSessions.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != remoteSessions.end())
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
2019-04-30 16:49:34 +00:00
|
|
|
itr->second->Tick(now);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr->second->Pump(now))
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
2019-09-19 20:28:12 +00:00
|
|
|
LogInfo("marking session as dead T=", itr->first);
|
2019-04-30 01:06:20 +00:00
|
|
|
itr->second->Stop();
|
2020-02-27 16:54:18 +00:00
|
|
|
sessions.erase(itr->second->currentConvoTag);
|
2019-04-30 01:06:20 +00:00
|
|
|
deadSessions.emplace(std::move(*itr));
|
|
|
|
itr = remoteSessions.erase(itr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-07-15 09:15:51 +00:00
|
|
|
EndpointUtil::ExpireConvoSessions(llarp_time_t now, ConvoMap& sessions)
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
|
|
|
auto itr = sessions.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != sessions.end())
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr->second.IsExpired(now))
|
2019-06-14 12:49:45 +00:00
|
|
|
{
|
2019-09-20 16:56:19 +00:00
|
|
|
LogInfo("Expire session T=", itr->first);
|
2019-04-30 01:06:20 +00:00
|
|
|
itr = sessions.erase(itr);
|
2019-06-14 12:49:45 +00:00
|
|
|
}
|
2019-04-30 01:06:20 +00:00
|
|
|
else
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-07-15 09:15:51 +00:00
|
|
|
EndpointUtil::StopRemoteSessions(Sessions& remoteSessions)
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
for (auto& item : remoteSessions)
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
|
|
|
item.second->Stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-07-15 09:15:51 +00:00
|
|
|
EndpointUtil::StopSnodeSessions(SNodeSessions& sessions)
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
for (auto& item : sessions)
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
2019-07-01 14:56:56 +00:00
|
|
|
item.second.first->Stop();
|
2019-04-30 01:06:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
EndpointUtil::HasPathToService(const Address& addr, const Sessions& remoteSessions)
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
|
|
|
auto range = remoteSessions.equal_range(addr);
|
2020-04-07 18:38:56 +00:00
|
|
|
auto itr = range.first;
|
|
|
|
while (itr != range.second)
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr->second->ReadyToSend())
|
2019-04-30 01:06:20 +00:00
|
|
|
return true;
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-07 08:29:47 +00:00
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
EndpointUtil::GetConvoTagsForService(
|
|
|
|
const ConvoMap& sessions, const Address& info, std::set<ConvoTag>& tags)
|
2019-05-07 08:29:47 +00:00
|
|
|
{
|
|
|
|
bool inserted = false;
|
2020-04-07 18:38:56 +00:00
|
|
|
auto itr = sessions.begin();
|
|
|
|
while (itr != sessions.end())
|
2019-05-07 08:29:47 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr->second.remote.Addr() == info)
|
2019-05-07 08:29:47 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (tags.emplace(itr->first).second)
|
2019-05-07 08:29:47 +00:00
|
|
|
{
|
|
|
|
inserted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
return inserted;
|
|
|
|
}
|
2019-04-30 01:06:20 +00:00
|
|
|
} // namespace service
|
|
|
|
} // namespace llarp
|