lokinet/llarp/link/server.cpp

288 lines
5.9 KiB
C++
Raw Normal View History

#include <llarp/link/server.hpp>
#include "fs.hpp"
namespace llarp
{
ILinkLayer::~ILinkLayer()
{
}
bool
ILinkLayer::HasSessionTo(const PubKey& pk)
{
2018-09-10 13:43:36 +00:00
Lock l(m_AuthedLinksMutex);
2018-09-07 20:36:06 +00:00
return m_AuthedLinks.find(pk) != m_AuthedLinks.end();
}
void
ILinkLayer::ForEachSession(
std::function< void(const ILinkSession*) > visit) const
{
auto itr = m_AuthedLinks.begin();
while(itr != m_AuthedLinks.end())
{
visit(itr->second.get());
++itr;
}
}
bool
ILinkLayer::Configure(llarp_ev_loop* loop, const std::string& ifname, int af,
uint16_t port)
{
2018-10-29 16:48:36 +00:00
m_Loop = loop;
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))
return false;
2018-09-04 19:15:06 +00:00
m_ourAddr.port(port);
return llarp_ev_add_udp(loop, &m_udp, m_ourAddr) != -1;
}
void
ILinkLayer::Pump()
{
2018-10-29 16:48:36 +00:00
auto _now = now();
{
2018-09-10 13:43:36 +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-10-29 16:48:36 +00:00
if(!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
{
llarp::LogInfo("session to ", itr->second->GetPubKey(), " timed out");
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
}
{
2018-09-10 13:43:36 +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-10-29 16:48:36 +00:00
if(!(*itr)->TimedOut(_now))
2018-09-07 20:36:06 +00:00
{
(*itr)->Pump();
++itr;
}
else
itr = m_Pending.erase(itr);
2018-09-06 13:16:24 +00:00
}
}
}
2018-09-07 17:41:49 +00:00
void
ILinkLayer::MapAddr(const PubKey& pk, ILinkSession* s)
{
2018-09-10 13:43:36 +00:00
Lock l_authed(m_AuthedLinksMutex);
Lock l_pending(m_PendingMutex);
2018-09-07 20:36:06 +00:00
auto itr = m_Pending.begin();
while(itr != m_Pending.end())
2018-09-07 17:41:49 +00:00
{
2018-09-07 20:36:06 +00:00
if(itr->get() == s)
{
m_AuthedLinks.insert(std::make_pair(pk, std::move(*itr)));
itr = m_Pending.erase(itr);
return;
}
else
++itr;
2018-09-07 17:41:49 +00:00
}
}
bool
ILinkLayer::PickAddress(const RouterContact& rc,
llarp::AddressInfo& picked) const
{
std::string OurDialect = Name();
for(const auto& addr : rc.addrs)
{
if(addr.dialect == OurDialect)
{
picked = addr;
return true;
}
}
return false;
}
bool
ILinkLayer::TryEstablishTo(const RouterContact& rc)
{
llarp::AddressInfo to;
if(!PickAddress(rc, to))
return false;
2018-11-19 21:33:51 +00:00
llarp::LogInfo("Try establish to ", rc.pubkey);
llarp::Addr addr(to);
2018-09-06 20:31:58 +00:00
auto s = NewOutboundSession(rc, to);
s->Start();
2018-09-10 13:43:36 +00:00
PutSession(s);
return true;
}
bool
ILinkLayer::Start(llarp_logic* l)
{
m_Logic = l;
2018-09-07 17:41:49 +00:00
ScheduleTick(100);
return true;
}
void
ILinkLayer::Stop()
{
if(m_Logic && tick_id)
llarp_logic_remove_call(m_Logic, tick_id);
{
Lock l(m_AuthedLinksMutex);
auto itr = m_AuthedLinks.begin();
while(itr != m_AuthedLinks.end())
{
itr->second->SendClose();
itr = m_AuthedLinks.erase(itr);
}
}
{
Lock l(m_PendingMutex);
auto itr = m_Pending.begin();
while(itr != m_Pending.end())
{
(*itr)->SendClose();
itr = m_Pending.erase(itr);
}
}
}
void
ILinkLayer::CloseSessionTo(const PubKey& remote)
{
2018-09-10 13:43:36 +00:00
Lock l(m_AuthedLinksMutex);
2018-11-19 21:33:51 +00:00
llarp::LogInfo("Closing all to ", remote);
2018-09-07 20:36:06 +00:00
auto range = m_AuthedLinks.equal_range(remote);
auto itr = range.first;
while(itr != range.second)
{
itr->second->SendClose();
itr = m_AuthedLinks.erase(itr);
}
}
void
ILinkLayer::KeepAliveSessionTo(const PubKey& remote)
{
2018-09-10 13:43:36 +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)
{
itr->second->SendKeepAlive();
++itr;
}
}
bool
ILinkLayer::SendTo(const PubKey& remote, llarp_buffer_t buf)
{
ILinkSession* s = nullptr;
2018-09-07 20:36:06 +00:00
{
Lock l(m_AuthedLinksMutex);
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)
{
auto backlog = itr->second->SendQueueBacklog();
if(backlog < min)
{
s = itr->second.get();
min = backlog;
}
++itr;
}
2018-09-07 20:36:06 +00:00
}
return s && s->SendMessageBuffer(buf);
}
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());
}
const byte_t*
ILinkLayer::TransportSecretKey() const
{
return m_SecretKey;
}
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);
return false;
2018-09-04 19:15:06 +00:00
}
return true;
}
2018-09-07 17:41:49 +00:00
void
2018-09-10 13:43:36 +00:00
ILinkLayer::PutSession(ILinkSession* s)
2018-09-07 17:41:49 +00:00
{
2018-09-10 13:43:36 +00:00
Lock lock(m_PendingMutex);
2018-09-07 20:36:06 +00:00
m_Pending.emplace_back(s);
2018-09-07 17:41:49 +00:00
}
void
2018-10-29 16:48:36 +00:00
ILinkLayer::OnTick(uint64_t interval)
{
2018-10-29 16:48:36 +00:00
Tick(now());
ScheduleTick(interval);
}
void
ILinkLayer::ScheduleTick(uint64_t interval)
{
tick_id = llarp_logic_call_later(
m_Logic, {interval, this, &ILinkLayer::on_timer_tick});
}
} // namespace llarp