2021-03-09 22:24:35 +00:00
|
|
|
#include "linklayer.hpp"
|
|
|
|
#include "session.hpp"
|
|
|
|
#include <llarp/config/key_manager.hpp>
|
2019-12-03 17:58:53 +00:00
|
|
|
#include <memory>
|
2019-09-16 16:12:05 +00:00
|
|
|
#include <unordered_set>
|
2019-03-29 12:19:59 +00:00
|
|
|
|
2021-02-22 13:26:32 +00:00
|
|
|
namespace llarp::iwp
|
2019-03-29 12:19:59 +00:00
|
|
|
{
|
2021-02-22 13:26:32 +00:00
|
|
|
LinkLayer::LinkLayer(
|
|
|
|
std::shared_ptr<KeyManager> keyManager,
|
|
|
|
std::shared_ptr<EventLoop> ev,
|
|
|
|
GetRCFunc getrc,
|
|
|
|
LinkMessageHandler h,
|
|
|
|
SignBufferFunc sign,
|
|
|
|
BeforeConnectFunc_t before,
|
|
|
|
SessionEstablishedHandler est,
|
|
|
|
SessionRenegotiateHandler reneg,
|
|
|
|
TimeoutHandler timeout,
|
|
|
|
SessionClosedHandler closed,
|
|
|
|
PumpDoneHandler pumpDone,
|
|
|
|
WorkerFunc_t worker,
|
|
|
|
bool allowInbound)
|
|
|
|
: ILinkLayer(
|
|
|
|
keyManager, getrc, h, sign, before, est, reneg, timeout, closed, pumpDone, worker)
|
2021-03-02 02:06:20 +00:00
|
|
|
, m_Wakeup{ev->make_waker([this]() { HandleWakeupPlaintext(); })}
|
2021-02-22 13:26:32 +00:00
|
|
|
, m_PlaintextRecv{1024}
|
|
|
|
, permitInbound{allowInbound}
|
|
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
const char*
|
|
|
|
LinkLayer::Name() const
|
|
|
|
{
|
|
|
|
return "iwp";
|
|
|
|
}
|
2019-03-29 12:19:59 +00:00
|
|
|
|
2021-02-22 13:26:32 +00:00
|
|
|
uint16_t
|
|
|
|
LinkLayer::Rank() const
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LinkLayer::RecvFrom(const SockAddr& from, ILinkSession::Packet_t pkt)
|
|
|
|
{
|
|
|
|
std::shared_ptr<ILinkSession> session;
|
|
|
|
auto itr = m_AuthedAddrs.find(from);
|
|
|
|
bool isNewSession = false;
|
|
|
|
if (itr == m_AuthedAddrs.end())
|
2019-03-29 12:19:59 +00:00
|
|
|
{
|
2021-02-22 13:26:32 +00:00
|
|
|
Lock_t lock(m_PendingMutex);
|
|
|
|
if (m_Pending.count(from) == 0)
|
|
|
|
{
|
|
|
|
if (not permitInbound)
|
|
|
|
return;
|
|
|
|
isNewSession = true;
|
|
|
|
m_Pending.insert({from, std::make_shared<Session>(this, from)});
|
|
|
|
}
|
|
|
|
session = m_Pending.find(from)->second;
|
2019-03-29 12:19:59 +00:00
|
|
|
}
|
2021-02-22 13:26:32 +00:00
|
|
|
else
|
2019-03-29 12:19:59 +00:00
|
|
|
{
|
2021-02-22 13:26:32 +00:00
|
|
|
Lock_t lock(m_AuthedLinksMutex);
|
|
|
|
auto range = m_AuthedLinks.equal_range(itr->second);
|
|
|
|
session = range.first->second;
|
2019-03-29 12:19:59 +00:00
|
|
|
}
|
2021-02-22 13:26:32 +00:00
|
|
|
if (session)
|
2019-03-29 12:19:59 +00:00
|
|
|
{
|
2021-02-22 13:26:32 +00:00
|
|
|
bool success = session->Recv_LL(std::move(pkt));
|
|
|
|
if (!success and isNewSession)
|
2019-08-23 11:32:52 +00:00
|
|
|
{
|
2021-02-22 13:26:32 +00:00
|
|
|
LogWarn("Brand new session failed; removing from pending sessions list");
|
|
|
|
m_Pending.erase(m_Pending.find(from));
|
2019-08-23 11:32:52 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-22 13:26:32 +00:00
|
|
|
}
|
2019-08-23 11:32:52 +00:00
|
|
|
|
2021-02-22 13:26:32 +00:00
|
|
|
bool
|
|
|
|
LinkLayer::MapAddr(const RouterID& r, ILinkSession* s)
|
|
|
|
{
|
|
|
|
if (!ILinkLayer::MapAddr(r, s))
|
|
|
|
return false;
|
|
|
|
m_AuthedAddrs.emplace(s->GetRemoteEndpoint(), r);
|
|
|
|
return true;
|
|
|
|
}
|
2019-08-23 11:32:52 +00:00
|
|
|
|
2021-02-22 13:26:32 +00:00
|
|
|
void
|
2021-02-22 15:01:05 +00:00
|
|
|
LinkLayer::UnmapAddr(const SockAddr& addr)
|
2021-02-22 13:26:32 +00:00
|
|
|
{
|
|
|
|
m_AuthedAddrs.erase(addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<ILinkSession>
|
|
|
|
LinkLayer::NewOutboundSession(const RouterContact& rc, const AddressInfo& ai)
|
|
|
|
{
|
|
|
|
return std::make_shared<Session>(this, rc, ai);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LinkLayer::AddWakeup(std::weak_ptr<Session> session)
|
|
|
|
{
|
2021-02-22 15:01:05 +00:00
|
|
|
if (auto ptr = session.lock())
|
|
|
|
m_PlaintextRecv[ptr->GetRemoteEndpoint()] = session;
|
2021-02-22 13:26:32 +00:00
|
|
|
}
|
2019-03-29 12:19:59 +00:00
|
|
|
|
2021-02-22 13:26:32 +00:00
|
|
|
void
|
|
|
|
LinkLayer::WakeupPlaintext()
|
|
|
|
{
|
2021-03-02 02:06:20 +00:00
|
|
|
m_Wakeup->Trigger();
|
2021-02-22 13:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LinkLayer::HandleWakeupPlaintext()
|
|
|
|
{
|
2021-02-22 15:01:05 +00:00
|
|
|
for (const auto& [addr, session] : m_PlaintextRecv)
|
2019-03-29 12:19:59 +00:00
|
|
|
{
|
2021-02-22 13:26:32 +00:00
|
|
|
auto ptr = session.lock();
|
|
|
|
if (ptr)
|
|
|
|
ptr->HandlePlaintext();
|
2019-03-29 12:19:59 +00:00
|
|
|
}
|
2021-02-22 15:01:05 +00:00
|
|
|
m_PlaintextRecv.clear();
|
2021-02-22 13:26:32 +00:00
|
|
|
PumpDone();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llarp::iwp
|