lokinet/llarp/service/endpoint_util.cpp
Jeff 21930cf667
LNS (#1342)
* initial relay side lns

* fix typo

* add reserved names and refactor test for dns

* lns name decryption

* all wired up (allegedly)

* refact to use service::EncryptedName for LNS responses to include nonce with ciphertext

* fully rwemove tag_lookup_job

* replace lns cache with DecayingHashTable

* check for lns name validity against the following rules:

* not localhost.loki, loki.loki, or snode.loki

* if it contains no dash then max 32 characters long, not including the .loki tld (and also assuming a leading subdomain has been stripped)

* These are from general DNS requirements, and also enforced in
registrations:

* Must be all [A-Za-z0-9-]. (A-Z will be lower-cased by the RPC call).

* cannot start or end with a -

* max 63 characters long if it does contain a dash

* cannot contain -- in the third and fourth characters unless it starts with xn--

* handle timeout in name lookup job by calling the right handler with std::nullopt
2020-09-17 15:18:08 -04:00

180 lines
4.1 KiB
C++

#include <service/endpoint_util.hpp>
#include <exit/session.hpp>
#include <service/outbound_context.hpp>
#include <service/lookup.hpp>
#include <util/logging/logger.hpp>
namespace llarp
{
namespace service
{
void
EndpointUtil::ExpireSNodeSessions(llarp_time_t now, SNodeSessions& sessions)
{
auto itr = sessions.begin();
while (itr != sessions.end())
{
if (itr->second.first->ShouldRemove() && itr->second.first->IsStopped())
{
itr = sessions.erase(itr);
continue;
}
// expunge next tick
if (itr->second.first->IsExpired(now))
{
itr->second.first->Stop();
}
else
{
itr->second.first->Tick(now);
}
++itr;
}
}
void
EndpointUtil::ExpirePendingTx(llarp_time_t now, PendingLookups& lookups)
{
for (auto itr = lookups.begin(); itr != lookups.end();)
{
if (!itr->second->IsTimedOut(now))
{
++itr;
continue;
}
std::unique_ptr<IServiceLookup> lookup = std::move(itr->second);
LogWarn(lookup->name, " timed out txid=", lookup->txid);
lookup->HandleTimeout();
itr = lookups.erase(itr);
}
}
void
EndpointUtil::ExpirePendingRouterLookups(llarp_time_t now, PendingRouters& routers)
{
for (auto itr = routers.begin(); itr != routers.end();)
{
if (!itr->second.IsExpired(now))
{
++itr;
continue;
}
LogWarn("lookup for ", itr->first, " timed out");
itr->second.InformResult({});
itr = routers.erase(itr);
}
}
void
EndpointUtil::DeregisterDeadSessions(llarp_time_t now, Sessions& sessions)
{
auto itr = sessions.begin();
while (itr != sessions.end())
{
if (itr->second->IsDone(now))
{
itr = sessions.erase(itr);
}
else
{
++itr;
}
}
}
void
EndpointUtil::TickRemoteSessions(
llarp_time_t now, Sessions& remoteSessions, Sessions& deadSessions, ConvoMap& sessions)
{
auto itr = remoteSessions.begin();
while (itr != remoteSessions.end())
{
itr->second->Tick(now);
if (itr->second->Pump(now))
{
LogInfo("marking session as dead T=", itr->first);
itr->second->Stop();
sessions.erase(itr->second->currentConvoTag);
deadSessions.emplace(std::move(*itr));
itr = remoteSessions.erase(itr);
}
else
{
++itr;
}
}
}
void
EndpointUtil::ExpireConvoSessions(llarp_time_t now, ConvoMap& sessions)
{
auto itr = sessions.begin();
while (itr != sessions.end())
{
if (itr->second.IsExpired(now))
{
LogInfo("Expire session T=", itr->first);
itr = sessions.erase(itr);
}
else
++itr;
}
}
void
EndpointUtil::StopRemoteSessions(Sessions& remoteSessions)
{
for (auto& item : remoteSessions)
{
item.second->Stop();
}
}
void
EndpointUtil::StopSnodeSessions(SNodeSessions& sessions)
{
for (auto& item : sessions)
{
item.second.first->Stop();
}
}
bool
EndpointUtil::HasPathToService(const Address& addr, const Sessions& remoteSessions)
{
auto range = remoteSessions.equal_range(addr);
auto itr = range.first;
while (itr != range.second)
{
if (itr->second->ReadyToSend())
return true;
++itr;
}
return false;
}
bool
EndpointUtil::GetConvoTagsForService(
const ConvoMap& sessions, const Address& info, std::set<ConvoTag>& tags)
{
bool inserted = false;
auto itr = sessions.begin();
while (itr != sessions.end())
{
if (itr->second.remote.Addr() == info)
{
if (tags.emplace(itr->first).second)
{
inserted = true;
}
}
++itr;
}
return inserted;
}
} // namespace service
} // namespace llarp