You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/llarp/router/rc_gossiper.cpp

104 lines
2.5 KiB
C++

4 years ago
#include <router/rc_gossiper.hpp>
#include <messages/dht_immediate.hpp>
#include <dht/messages/gotrouter.hpp>
#include <util/time.hpp>
4 years ago
#include <constants/link_layer.hpp>
4 years ago
namespace llarp
{
// 30 minutes
static constexpr auto RCGossipFilterDecayInterval = 30min;
4 years ago
// (30 minutes * 2) - 5 minutes
static constexpr auto GossipOurRCInterval =
(RCGossipFilterDecayInterval * 2) - (5min);
4 years ago
RCGossiper::RCGossiper()
: I_RCGossiper()
, m_Filter(
std::chrono::duration_cast< Time_t >(RCGossipFilterDecayInterval))
4 years ago
{
}
void
RCGossiper::Init(ILinkManager* l, const RouterID& ourID)
4 years ago
{
m_OurRouterID = ourID;
4 years ago
m_LinkManager = l;
}
bool
RCGossiper::ShouldGossipOurRC(Time_t now) const
{
return now >= (m_LastGossipedOurRC + GossipOurRCInterval);
}
bool
RCGossiper::IsOurRC(const RouterContact& rc) const
{
return rc.pubkey == m_OurRouterID;
}
4 years ago
void
RCGossiper::Decay(Time_t now)
4 years ago
{
LogDebug("decay filter at ", now.count());
m_Filter.Decay(now.count());
4 years ago
}
bool
RCGossiper::GossipRC(const RouterContact& rc)
{
// only distribute public routers
if(not rc.IsPublicRouter())
return false;
if(m_LinkManager == nullptr)
return false;
4 years ago
const RouterID pubkey(rc.pubkey);
// filter check
4 years ago
if(m_Filter.Contains(pubkey))
4 years ago
return false;
4 years ago
m_Filter.Insert(pubkey);
const auto now = time_now();
// is this our rc?
if(IsOurRC(rc))
{
// should we gossip our rc?
if(not ShouldGossipOurRC(now))
{
// nah drop it
return false;
}
// ya pop it
m_LastGossipedOurRC = now;
}
// send unwarrented GRCM as gossip method
4 years ago
DHTImmediateMessage unwarrentedGRCM;
unwarrentedGRCM.msgs.emplace_back(
4 years ago
new dht::GotRouterMessage(dht::Key_t{}, 0, {rc}, false));
4 years ago
// send it to everyone
4 years ago
m_LinkManager->ForEachPeer([&](ILinkSession* peerSession) {
4 years ago
// ensure connected session
4 years ago
if(not(peerSession && peerSession->IsEstablished()))
4 years ago
return;
// check if public router
4 years ago
const auto other_rc = peerSession->GetRemoteRC();
4 years ago
if(not other_rc.IsPublicRouter())
return;
// encode message
ILinkSession::Message_t msg;
4 years ago
msg.reserve(MAX_LINK_MSG_SIZE / 2);
4 years ago
llarp_buffer_t buf(msg);
4 years ago
if(not unwarrentedGRCM.BEncode(&buf))
4 years ago
return;
msg.resize(buf.cur - buf.base);
// send message
4 years ago
peerSession->SendMessageBuffer(std::move(msg), nullptr);
4 years ago
});
return true;
4 years ago
}
} // namespace llarp