2019-03-29 15:17:49 +00:00
|
|
|
#include <iwp/linklayer.hpp>
|
2019-08-22 20:53:27 +00:00
|
|
|
#include <iwp/session.hpp>
|
2019-12-03 17:58:53 +00:00
|
|
|
#include <config/key_manager.hpp>
|
|
|
|
#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)
|
|
|
|
, m_Wakeup{ev->make_event_loop_waker([self = this]() { self->HandleWakeupPlaintext(); })}
|
|
|
|
, m_PlaintextRecv{1024}
|
|
|
|
, permitInbound{allowInbound}
|
|
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
LinkLayer::~LinkLayer()
|
2019-03-29 12:19:59 +00:00
|
|
|
{
|
2021-02-22 13:26:32 +00:00
|
|
|
m_Wakeup->End();
|
|
|
|
}
|
2019-03-29 12:19:59 +00:00
|
|
|
|
2021-02-22 13:26:32 +00:00
|
|
|
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
|
|
|
|
LinkLayer::UnmapAddr(const IpAddress& addr)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
m_PlaintextRecv.tryPushBack(session);
|
|
|
|
}
|
2019-03-29 12:19:59 +00:00
|
|
|
|
2021-02-22 13:26:32 +00:00
|
|
|
void
|
|
|
|
LinkLayer::WakeupPlaintext()
|
|
|
|
{
|
|
|
|
m_Wakeup->Wakeup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LinkLayer::HandleWakeupPlaintext()
|
|
|
|
{
|
|
|
|
while (not m_PlaintextRecv.empty())
|
2019-03-29 12:19:59 +00:00
|
|
|
{
|
2021-02-22 13:26:32 +00:00
|
|
|
auto session = m_PlaintextRecv.popFront();
|
|
|
|
auto ptr = session.lock();
|
|
|
|
if (ptr)
|
|
|
|
ptr->HandlePlaintext();
|
2019-03-29 12:19:59 +00:00
|
|
|
}
|
2021-02-22 13:26:32 +00:00
|
|
|
PumpDone();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llarp::iwp
|