2021-03-09 22:24:35 +00:00
|
|
|
#include "endpoint_util.hpp"
|
2019-04-30 01:06:20 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/exit/session.hpp>
|
|
|
|
#include "outbound_context.hpp"
|
|
|
|
#include "lookup.hpp"
|
|
|
|
#include <llarp/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
|
|
|
{
|
2021-04-07 12:03:04 +00:00
|
|
|
if (itr->second->ShouldRemove() && itr->second->IsStopped())
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
|
|
|
itr = sessions.erase(itr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// expunge next tick
|
2021-04-07 12:03:04 +00:00
|
|
|
if (itr->second->IsExpired(now))
|
2019-04-30 01:06:20 +00:00
|
|
|
{
|
2021-04-07 12:03:04 +00:00
|
|
|
itr->second->Stop();
|
2019-04-30 01:06:20 +00:00
|
|
|
}
|
2019-04-30 21:36:27 +00:00
|
|
|
else
|
|
|
|
{
|
2021-04-07 12:03:04 +00:00
|
|
|
itr->second->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
|
|
|
{
|
2021-06-03 21:51:23 +00:00
|
|
|
std::vector<std::unique_ptr<IServiceLookup>> timedout;
|
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;
|
|
|
|
}
|
2021-06-03 21:51:23 +00:00
|
|
|
timedout.emplace_back(std::move(itr->second));
|
|
|
|
itr = lookups.erase(itr);
|
|
|
|
}
|
2019-04-30 01:06:20 +00:00
|
|
|
|
2021-06-03 21:51:23 +00:00
|
|
|
for (const auto& lookup : timedout)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2021-06-03 12:21:15 +00:00
|
|
|
LogInfo(
|
|
|
|
"marking session as dead T=",
|
|
|
|
itr->second->currentConvoTag,
|
|
|
|
" to ",
|
|
|
|
itr->second->Addr());
|
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;
|
|
|
|
}
|
|
|
|
}
|
2021-06-02 19:52:13 +00:00
|
|
|
for (auto& item : deadSessions)
|
|
|
|
{
|
|
|
|
item.second->Tick(now);
|
|
|
|
}
|
2019-04-30 01:06:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2021-06-03 12:21:15 +00:00
|
|
|
LogInfo("Expire session T=", itr->first, " to ", itr->second.Addr());
|
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
|
|
|
{
|
2021-04-07 12:03:04 +00:00
|
|
|
item.second->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
|