2018-12-12 01:32:10 +00:00
|
|
|
#include <link/server.hpp>
|
2018-09-03 13:10:56 +00:00
|
|
|
|
2019-01-13 16:30:07 +00:00
|
|
|
#include <crypto/crypto.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/fs.hpp>
|
|
|
|
|
2018-09-03 13:10:56 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2019-05-08 12:17:48 +00:00
|
|
|
static constexpr size_t MaxSessionsPerKey = 16;
|
|
|
|
|
2019-01-02 01:04:04 +00:00
|
|
|
ILinkLayer::ILinkLayer(const SecretKey& routerEncSecret, GetRCFunc getrc,
|
2018-12-17 20:46:08 +00:00
|
|
|
LinkMessageHandler handler, SignBufferFunc signbuf,
|
|
|
|
SessionEstablishedHandler establishedSession,
|
2018-12-19 16:17:41 +00:00
|
|
|
SessionRenegotiateHandler reneg,
|
2018-12-17 20:46:08 +00:00
|
|
|
TimeoutHandler timeout, SessionClosedHandler closed)
|
|
|
|
: HandleMessage(handler)
|
|
|
|
, HandleTimeout(timeout)
|
|
|
|
, Sign(signbuf)
|
|
|
|
, GetOurRC(getrc)
|
|
|
|
, SessionEstablished(establishedSession)
|
|
|
|
, SessionClosed(closed)
|
2018-12-19 16:17:41 +00:00
|
|
|
, SessionRenegotiate(reneg)
|
2018-12-17 20:46:08 +00:00
|
|
|
, m_RouterEncSecret(routerEncSecret)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-09-03 13:10:56 +00:00
|
|
|
ILinkLayer::~ILinkLayer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-01-02 01:03:53 +00:00
|
|
|
ILinkLayer::HasSessionTo(const RouterID& id)
|
2018-09-03 13:10:56 +00:00
|
|
|
{
|
2019-03-03 15:01:05 +00:00
|
|
|
Lock l(&m_AuthedLinksMutex);
|
2019-01-02 01:03:53 +00:00
|
|
|
return m_AuthedLinks.find(id) != m_AuthedLinks.end();
|
2018-09-03 13:10:56 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 18:18:12 +00:00
|
|
|
void
|
2019-04-08 12:01:52 +00:00
|
|
|
ILinkLayer::ForEachSession(std::function< void(const ILinkSession*) > visit,
|
|
|
|
bool randomize) const
|
2018-10-25 18:18:12 +00:00
|
|
|
{
|
2019-07-03 12:42:11 +00:00
|
|
|
std::vector< std::shared_ptr< ILinkSession > > sessions;
|
2019-04-03 19:05:44 +00:00
|
|
|
{
|
2019-07-03 12:42:11 +00:00
|
|
|
Lock l(&m_AuthedLinksMutex);
|
|
|
|
if(m_AuthedLinks.size() == 0)
|
|
|
|
return;
|
|
|
|
const size_t sz = randint() % m_AuthedLinks.size();
|
|
|
|
auto itr = m_AuthedLinks.begin();
|
|
|
|
auto begin = itr;
|
|
|
|
if(randomize)
|
2019-04-03 19:05:44 +00:00
|
|
|
{
|
2019-07-03 12:42:11 +00:00
|
|
|
std::advance(itr, sz);
|
|
|
|
begin = itr;
|
|
|
|
}
|
|
|
|
while(itr != m_AuthedLinks.end())
|
|
|
|
{
|
|
|
|
sessions.emplace_back(itr->second);
|
2019-04-03 19:05:44 +00:00
|
|
|
++itr;
|
|
|
|
}
|
2019-07-03 12:42:11 +00:00
|
|
|
if(randomize)
|
|
|
|
{
|
|
|
|
itr = m_AuthedLinks.begin();
|
|
|
|
while(itr != begin)
|
|
|
|
{
|
|
|
|
sessions.emplace_back(itr->second);
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
2019-04-03 19:05:44 +00:00
|
|
|
}
|
2019-07-03 12:42:11 +00:00
|
|
|
for(const auto& session : sessions)
|
|
|
|
visit(session.get());
|
2018-10-25 18:18:12 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 16:17:41 +00:00
|
|
|
bool
|
2019-01-02 01:03:53 +00:00
|
|
|
ILinkLayer::VisitSessionByPubkey(const RouterID& pk,
|
2018-12-19 16:17:41 +00:00
|
|
|
std::function< bool(ILinkSession*) > visit)
|
|
|
|
{
|
2019-07-03 12:42:11 +00:00
|
|
|
std::shared_ptr< ILinkSession > session;
|
2018-12-19 16:17:41 +00:00
|
|
|
{
|
2019-07-03 12:42:11 +00:00
|
|
|
Lock l(&m_AuthedLinksMutex);
|
|
|
|
auto itr = m_AuthedLinks.find(pk);
|
|
|
|
if(itr == m_AuthedLinks.end())
|
|
|
|
return false;
|
|
|
|
session = itr->second;
|
2018-12-19 16:17:41 +00:00
|
|
|
}
|
2019-07-03 12:42:11 +00:00
|
|
|
return visit(session.get());
|
2018-12-19 16:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ILinkLayer::ForEachSession(std::function< void(ILinkSession*) > visit)
|
|
|
|
{
|
2019-07-03 12:42:11 +00:00
|
|
|
std::vector< std::shared_ptr< ILinkSession > > sessions;
|
2018-12-19 16:17:41 +00:00
|
|
|
{
|
2019-07-03 12:42:11 +00:00
|
|
|
Lock l(&m_AuthedLinksMutex);
|
|
|
|
auto itr = m_AuthedLinks.begin();
|
|
|
|
while(itr != m_AuthedLinks.end())
|
|
|
|
{
|
|
|
|
sessions.emplace_back(itr->second);
|
|
|
|
++itr;
|
|
|
|
}
|
2018-12-19 16:17:41 +00:00
|
|
|
}
|
2019-07-03 12:42:11 +00:00
|
|
|
for(const auto& s : sessions)
|
|
|
|
visit(s.get());
|
2018-12-19 16:17:41 +00:00
|
|
|
}
|
|
|
|
|
2018-09-03 13:10:56 +00:00
|
|
|
bool
|
2019-04-08 12:01:52 +00:00
|
|
|
ILinkLayer::Configure(llarp_ev_loop_ptr loop, const std::string& ifname,
|
|
|
|
int af, uint16_t port)
|
2018-09-03 13:10:56 +00:00
|
|
|
{
|
2019-05-21 15:24:20 +00:00
|
|
|
m_Loop = loop;
|
2018-09-03 13:10:56 +00:00
|
|
|
m_udp.user = this;
|
|
|
|
m_udp.recvfrom = &ILinkLayer::udp_recv_from;
|
|
|
|
m_udp.tick = &ILinkLayer::udp_tick;
|
|
|
|
if(ifname == "*")
|
|
|
|
{
|
|
|
|
if(!AllInterfaces(af, m_ourAddr))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if(!GetIFAddr(ifname, m_ourAddr, af))
|
2019-07-02 21:28:28 +00:00
|
|
|
m_ourAddr = Addr(ifname);
|
2018-09-04 19:15:06 +00:00
|
|
|
m_ourAddr.port(port);
|
2019-04-08 12:01:52 +00:00
|
|
|
return llarp_ev_add_udp(m_Loop.get(), &m_udp, m_ourAddr) != -1;
|
2018-09-03 13:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ILinkLayer::Pump()
|
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
auto _now = Now();
|
2018-09-03 13:10:56 +00:00
|
|
|
{
|
2019-03-03 15:01:05 +00:00
|
|
|
Lock lock(&m_AuthedLinksMutex);
|
2018-09-07 20:36:06 +00:00
|
|
|
auto itr = m_AuthedLinks.begin();
|
|
|
|
while(itr != m_AuthedLinks.end())
|
2018-09-07 17:41:49 +00:00
|
|
|
{
|
2018-12-27 19:10:38 +00:00
|
|
|
if(itr->second.get() && !itr->second->TimedOut(_now))
|
2018-09-07 20:36:06 +00:00
|
|
|
{
|
|
|
|
itr->second->Pump();
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
else
|
2018-11-19 21:55:41 +00:00
|
|
|
{
|
2018-12-11 13:33:23 +00:00
|
|
|
llarp::LogInfo("session to ", RouterID(itr->second->GetPubKey()),
|
|
|
|
" timed out");
|
2019-05-25 14:54:30 +00:00
|
|
|
itr->second->Close();
|
2018-09-07 20:36:06 +00:00
|
|
|
itr = m_AuthedLinks.erase(itr);
|
2018-11-19 21:55:41 +00:00
|
|
|
}
|
2018-09-07 17:41:49 +00:00
|
|
|
}
|
2018-09-07 20:36:06 +00:00
|
|
|
}
|
|
|
|
{
|
2019-03-03 15:01:05 +00:00
|
|
|
Lock lock(&m_PendingMutex);
|
2018-09-07 20:36:06 +00:00
|
|
|
|
|
|
|
auto itr = m_Pending.begin();
|
|
|
|
while(itr != m_Pending.end())
|
2018-09-03 13:10:56 +00:00
|
|
|
{
|
2019-01-07 12:47:57 +00:00
|
|
|
if(itr->second.get() && !itr->second->TimedOut(_now))
|
2018-09-07 20:36:06 +00:00
|
|
|
{
|
2019-01-07 12:47:57 +00:00
|
|
|
itr->second->Pump();
|
2018-09-07 20:36:06 +00:00
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
else
|
2019-03-11 13:01:43 +00:00
|
|
|
{
|
|
|
|
LogInfo("pending session at ", itr->first, " timed out");
|
2019-05-25 14:54:30 +00:00
|
|
|
itr->second->Close();
|
2018-09-07 20:36:06 +00:00
|
|
|
itr = m_Pending.erase(itr);
|
2019-03-11 13:01:43 +00:00
|
|
|
}
|
2018-09-06 13:16:24 +00:00
|
|
|
}
|
2018-09-03 13:10:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 12:47:57 +00:00
|
|
|
bool
|
2019-01-02 01:03:53 +00:00
|
|
|
ILinkLayer::MapAddr(const RouterID& pk, ILinkSession* s)
|
2018-09-07 17:41:49 +00:00
|
|
|
{
|
2019-03-03 15:01:05 +00:00
|
|
|
Lock l_authed(&m_AuthedLinksMutex);
|
|
|
|
Lock l_pending(&m_PendingMutex);
|
2019-01-10 12:30:21 +00:00
|
|
|
llarp::Addr addr = s->GetRemoteEndpoint();
|
2019-01-13 16:30:07 +00:00
|
|
|
auto itr = m_Pending.find(addr);
|
2019-01-04 12:43:53 +00:00
|
|
|
if(itr != m_Pending.end())
|
2018-09-07 17:41:49 +00:00
|
|
|
{
|
2019-01-10 12:30:21 +00:00
|
|
|
if(m_AuthedLinks.count(pk) > MaxSessionsPerKey)
|
|
|
|
{
|
2019-04-02 09:03:53 +00:00
|
|
|
s->Close();
|
2019-01-10 12:30:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-04-02 09:03:53 +00:00
|
|
|
m_AuthedLinks.emplace(pk, itr->second);
|
2019-01-04 12:43:53 +00:00
|
|
|
itr = m_Pending.erase(itr);
|
2019-01-10 12:30:21 +00:00
|
|
|
return true;
|
2019-01-07 12:47:57 +00:00
|
|
|
}
|
2019-01-10 12:30:21 +00:00
|
|
|
return false;
|
2018-09-07 17:41:49 +00:00
|
|
|
}
|
|
|
|
|
2018-09-03 13:10:56 +00:00
|
|
|
bool
|
2018-09-04 12:41:25 +00:00
|
|
|
ILinkLayer::PickAddress(const RouterContact& rc,
|
|
|
|
llarp::AddressInfo& picked) const
|
2018-09-03 13:10:56 +00:00
|
|
|
{
|
|
|
|
std::string OurDialect = Name();
|
|
|
|
for(const auto& addr : rc.addrs)
|
|
|
|
{
|
|
|
|
if(addr.dialect == OurDialect)
|
|
|
|
{
|
|
|
|
picked = addr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:19:19 +00:00
|
|
|
util::StatusObject
|
|
|
|
ILinkLayer::ExtractStatus() const
|
|
|
|
{
|
2019-02-18 23:58:12 +00:00
|
|
|
std::vector< util::StatusObject > pending, established;
|
|
|
|
|
2019-03-03 20:51:47 +00:00
|
|
|
{
|
|
|
|
Lock l(&m_PendingMutex);
|
|
|
|
std::transform(m_Pending.cbegin(), m_Pending.cend(),
|
|
|
|
std::back_inserter(pending),
|
|
|
|
[](const auto& item) -> util::StatusObject {
|
|
|
|
return item.second->ExtractStatus();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
{
|
|
|
|
Lock l(&m_AuthedLinksMutex);
|
|
|
|
std::transform(m_AuthedLinks.cbegin(), m_AuthedLinks.cend(),
|
|
|
|
std::back_inserter(established),
|
|
|
|
[](const auto& item) -> util::StatusObject {
|
|
|
|
return item.second->ExtractStatus();
|
|
|
|
});
|
|
|
|
}
|
2019-02-18 23:58:12 +00:00
|
|
|
|
|
|
|
return {{"name", Name()},
|
|
|
|
{"rank", uint64_t(Rank())},
|
|
|
|
{"addr", m_ourAddr.ToString()},
|
|
|
|
{"sessions",
|
|
|
|
util::StatusObject{{"pending", pending},
|
|
|
|
{"established", established}}}};
|
2019-02-15 22:19:19 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 14:56:12 +00:00
|
|
|
bool
|
2018-12-19 17:48:29 +00:00
|
|
|
ILinkLayer::TryEstablishTo(RouterContact rc)
|
2018-09-03 13:10:56 +00:00
|
|
|
{
|
2019-05-08 12:17:48 +00:00
|
|
|
{
|
|
|
|
Lock l(&m_AuthedLinksMutex);
|
|
|
|
if(m_AuthedLinks.count(rc.pubkey) >= MaxSessionsPerKey)
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
llarp::AddressInfo to;
|
2018-09-03 13:10:56 +00:00
|
|
|
if(!PickAddress(rc, to))
|
2018-11-21 14:56:12 +00:00
|
|
|
return false;
|
2019-05-14 17:35:01 +00:00
|
|
|
const llarp::Addr addr(to);
|
2019-05-08 12:17:48 +00:00
|
|
|
{
|
|
|
|
Lock l(&m_PendingMutex);
|
|
|
|
if(m_Pending.count(addr) >= MaxSessionsPerKey)
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-02 09:03:53 +00:00
|
|
|
std::shared_ptr< ILinkSession > s = NewOutboundSession(rc, to);
|
2019-01-07 12:47:57 +00:00
|
|
|
if(PutSession(s))
|
|
|
|
{
|
|
|
|
s->Start();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2018-09-03 13:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-05-22 16:20:50 +00:00
|
|
|
ILinkLayer::Start(std::shared_ptr< Logic > l)
|
2018-09-03 13:10:56 +00:00
|
|
|
{
|
|
|
|
m_Logic = l;
|
2018-09-07 17:41:49 +00:00
|
|
|
ScheduleTick(100);
|
2018-09-03 13:10:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:19:19 +00:00
|
|
|
void
|
|
|
|
ILinkLayer::Tick(llarp_time_t now)
|
|
|
|
{
|
|
|
|
{
|
2019-03-07 22:58:29 +00:00
|
|
|
Lock l(&m_AuthedLinksMutex);
|
2019-03-07 15:17:29 +00:00
|
|
|
auto itr = m_AuthedLinks.begin();
|
|
|
|
while(itr != m_AuthedLinks.end())
|
|
|
|
{
|
|
|
|
itr->second->Tick(now);
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
2019-03-07 22:58:29 +00:00
|
|
|
|
2019-03-07 15:17:29 +00:00
|
|
|
{
|
2019-03-07 22:58:29 +00:00
|
|
|
Lock l(&m_PendingMutex);
|
2019-03-07 15:17:29 +00:00
|
|
|
auto itr = m_Pending.begin();
|
|
|
|
while(itr != m_Pending.end())
|
|
|
|
{
|
|
|
|
itr->second->Tick(now);
|
|
|
|
++itr;
|
|
|
|
}
|
2019-02-15 22:19:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-03 13:10:56 +00:00
|
|
|
void
|
|
|
|
ILinkLayer::Stop()
|
|
|
|
{
|
|
|
|
if(m_Logic && tick_id)
|
2018-12-10 14:14:55 +00:00
|
|
|
m_Logic->remove_call(tick_id);
|
2018-09-24 10:23:11 +00:00
|
|
|
{
|
2019-03-03 15:01:05 +00:00
|
|
|
Lock l(&m_AuthedLinksMutex);
|
2018-09-24 10:23:11 +00:00
|
|
|
auto itr = m_AuthedLinks.begin();
|
|
|
|
while(itr != m_AuthedLinks.end())
|
|
|
|
{
|
2019-04-02 09:03:53 +00:00
|
|
|
itr->second->Close();
|
2018-12-24 16:31:58 +00:00
|
|
|
++itr;
|
2018-09-24 10:23:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2019-03-03 15:01:05 +00:00
|
|
|
Lock l(&m_PendingMutex);
|
2018-09-24 10:23:11 +00:00
|
|
|
auto itr = m_Pending.begin();
|
|
|
|
while(itr != m_Pending.end())
|
|
|
|
{
|
2019-04-02 09:03:53 +00:00
|
|
|
itr->second->Close();
|
2018-12-24 16:31:58 +00:00
|
|
|
++itr;
|
2018-09-24 10:23:11 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-03 13:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-01-02 01:04:04 +00:00
|
|
|
ILinkLayer::CloseSessionTo(const RouterID& remote)
|
2018-09-03 13:10:56 +00:00
|
|
|
{
|
2019-03-03 15:01:05 +00:00
|
|
|
Lock l(&m_AuthedLinksMutex);
|
2018-12-10 17:22:59 +00:00
|
|
|
RouterID r = remote;
|
|
|
|
llarp::LogInfo("Closing all to ", r);
|
|
|
|
auto range = m_AuthedLinks.equal_range(r);
|
2018-09-07 20:36:06 +00:00
|
|
|
auto itr = range.first;
|
|
|
|
while(itr != range.second)
|
|
|
|
{
|
2019-04-02 09:03:53 +00:00
|
|
|
itr->second->Close();
|
2018-09-07 20:36:06 +00:00
|
|
|
itr = m_AuthedLinks.erase(itr);
|
|
|
|
}
|
2018-09-03 13:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-01-02 01:04:04 +00:00
|
|
|
ILinkLayer::KeepAliveSessionTo(const RouterID& remote)
|
2018-09-03 13:10:56 +00:00
|
|
|
{
|
2019-03-03 15:01:05 +00:00
|
|
|
Lock l(&m_AuthedLinksMutex);
|
2018-09-07 20:36:06 +00:00
|
|
|
auto range = m_AuthedLinks.equal_range(remote);
|
|
|
|
auto itr = range.first;
|
|
|
|
while(itr != range.second)
|
|
|
|
{
|
2019-03-18 12:25:32 +00:00
|
|
|
if(itr->second->ShouldPing())
|
|
|
|
itr->second->SendKeepAlive();
|
2018-09-07 20:36:06 +00:00
|
|
|
++itr;
|
|
|
|
}
|
2018-09-03 13:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-07-26 16:19:31 +00:00
|
|
|
ILinkLayer::SendTo(const RouterID& remote, const llarp_buffer_t& buf,
|
|
|
|
ILinkSession::CompletionHandler completed)
|
2018-09-03 13:10:56 +00:00
|
|
|
{
|
2018-09-18 20:56:22 +00:00
|
|
|
ILinkSession* s = nullptr;
|
2018-09-07 20:36:06 +00:00
|
|
|
{
|
2019-03-03 15:01:05 +00:00
|
|
|
Lock l(&m_AuthedLinksMutex);
|
2018-11-05 13:19:08 +00:00
|
|
|
auto range = m_AuthedLinks.equal_range(remote);
|
|
|
|
auto itr = range.first;
|
|
|
|
// pick lowest backlog session
|
|
|
|
size_t min = std::numeric_limits< size_t >::max();
|
|
|
|
|
|
|
|
while(itr != range.second)
|
2018-09-18 20:56:22 +00:00
|
|
|
{
|
2018-11-05 13:19:08 +00:00
|
|
|
auto backlog = itr->second->SendQueueBacklog();
|
|
|
|
if(backlog < min)
|
|
|
|
{
|
|
|
|
s = itr->second.get();
|
|
|
|
min = backlog;
|
|
|
|
}
|
|
|
|
++itr;
|
2018-09-18 20:56:22 +00:00
|
|
|
}
|
2018-09-07 20:36:06 +00:00
|
|
|
}
|
2019-07-26 16:19:31 +00:00
|
|
|
return s && s->SendMessageBuffer(buf, completed);
|
2018-09-03 13:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ILinkLayer::GetOurAddressInfo(llarp::AddressInfo& addr) const
|
|
|
|
{
|
|
|
|
addr.dialect = Name();
|
|
|
|
addr.pubkey = TransportPubKey();
|
|
|
|
addr.rank = Rank();
|
|
|
|
addr.port = m_ourAddr.port();
|
|
|
|
addr.ip = *m_ourAddr.addr6();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const byte_t*
|
|
|
|
ILinkLayer::TransportPubKey() const
|
|
|
|
{
|
2018-09-04 12:55:20 +00:00
|
|
|
return llarp::seckey_topublic(TransportSecretKey());
|
|
|
|
}
|
|
|
|
|
2019-01-02 01:04:04 +00:00
|
|
|
const SecretKey&
|
2018-09-04 12:55:20 +00:00
|
|
|
ILinkLayer::TransportSecretKey() const
|
|
|
|
{
|
|
|
|
return m_SecretKey;
|
2018-09-03 13:10:56 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
bool
|
|
|
|
ILinkLayer::GenEphemeralKeys()
|
|
|
|
{
|
|
|
|
return KeyGen(m_SecretKey);
|
|
|
|
}
|
|
|
|
|
2018-09-03 13:10:56 +00:00
|
|
|
bool
|
|
|
|
ILinkLayer::EnsureKeys(const char* f)
|
|
|
|
{
|
|
|
|
fs::path fpath(f);
|
|
|
|
llarp::SecretKey keys;
|
|
|
|
std::error_code ec;
|
|
|
|
if(!fs::exists(fpath, ec))
|
|
|
|
{
|
|
|
|
if(!KeyGen(m_SecretKey))
|
|
|
|
return false;
|
|
|
|
// generated new keys
|
|
|
|
if(!BEncodeWriteFile< decltype(keys), 128 >(f, m_SecretKey))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// load keys
|
|
|
|
if(!BDecodeReadFile(f, m_SecretKey))
|
2018-09-04 19:15:06 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("Failed to load keyfile ", f);
|
2018-09-03 13:10:56 +00:00
|
|
|
return false;
|
2018-09-04 19:15:06 +00:00
|
|
|
}
|
2018-09-03 13:10:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-07 12:47:57 +00:00
|
|
|
bool
|
2019-04-02 09:03:53 +00:00
|
|
|
ILinkLayer::PutSession(const std::shared_ptr< ILinkSession >& s)
|
2018-09-07 17:41:49 +00:00
|
|
|
{
|
2019-03-26 13:51:57 +00:00
|
|
|
static constexpr size_t MaxSessionsPerEndpoint = 5;
|
2019-03-03 15:01:05 +00:00
|
|
|
Lock lock(&m_PendingMutex);
|
2019-01-09 14:21:55 +00:00
|
|
|
llarp::Addr addr = s->GetRemoteEndpoint();
|
2019-03-26 13:51:57 +00:00
|
|
|
if(m_Pending.count(addr) >= MaxSessionsPerEndpoint)
|
2019-01-07 12:47:57 +00:00
|
|
|
return false;
|
2019-04-02 09:03:53 +00:00
|
|
|
m_Pending.emplace(addr, s);
|
2019-01-07 12:47:57 +00:00
|
|
|
return true;
|
2018-09-07 17:41:49 +00:00
|
|
|
}
|
|
|
|
|
2018-09-03 13:10:56 +00:00
|
|
|
void
|
2018-10-29 16:48:36 +00:00
|
|
|
ILinkLayer::OnTick(uint64_t interval)
|
2018-09-03 13:10:56 +00:00
|
|
|
{
|
2018-12-27 19:10:38 +00:00
|
|
|
auto now = Now();
|
|
|
|
Tick(now);
|
2018-09-03 13:10:56 +00:00
|
|
|
ScheduleTick(interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ILinkLayer::ScheduleTick(uint64_t interval)
|
|
|
|
{
|
2018-12-10 14:14:55 +00:00
|
|
|
tick_id = m_Logic->call_later({interval, this, &ILinkLayer::on_timer_tick});
|
2018-09-03 13:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llarp
|