mirror of https://github.com/oxen-io/lokinet
rc gossiping
parent
1403cff805
commit
ea3851d15f
@ -0,0 +1 @@
|
||||
#include <router/i_gossiper.hpp>
|
@ -0,0 +1,21 @@
|
||||
#ifndef LLARP_GOSSIPER_HPP
|
||||
#define LLARP_GOSSIPER_HPP
|
||||
#include <router_contact.hpp>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
struct I_RCGossiper
|
||||
{
|
||||
virtual ~I_RCGossiper() = default;
|
||||
/// try goissping RC
|
||||
/// return false if we hit a cooldown for this rc
|
||||
/// return true if we gossiped this rc to at least 1 peer
|
||||
virtual bool
|
||||
GossipRC(const RouterContact &rc) = 0;
|
||||
|
||||
virtual void
|
||||
Decay(llarp_time_t now) = 0;
|
||||
};
|
||||
} // namespace llarp
|
||||
|
||||
#endif
|
@ -0,0 +1,68 @@
|
||||
#include <router/rc_gossiper.hpp>
|
||||
#include <messages/dht_immediate.hpp>
|
||||
#include <dht/messages/gotrouter.hpp>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
RCGossiper::RCGossiper()
|
||||
: I_RCGossiper(), m_Filter(RouterContact::UpdateInterval)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
RCGossiper::Init(ILinkManager* l)
|
||||
{
|
||||
m_LinkManager = l;
|
||||
}
|
||||
|
||||
void
|
||||
RCGossiper::Decay(llarp_time_t now)
|
||||
{
|
||||
m_Filter.Decay(now);
|
||||
}
|
||||
|
||||
bool
|
||||
RCGossiper::GossipRC(const RouterContact& rc)
|
||||
{
|
||||
// only distribute public routers
|
||||
if(not rc.IsPublicRouter())
|
||||
return false;
|
||||
if(m_LinkManager == nullptr)
|
||||
return false;
|
||||
// check for filter hit
|
||||
if(not m_Filter.Insert(rc.pubkey))
|
||||
return false;
|
||||
bool sent = false;
|
||||
// unwarrented GRCM
|
||||
DHTImmediateMessage m;
|
||||
m.msgs.emplace_back(
|
||||
new dht::GotRouterMessage(dht::Key_t{}, 0, {rc}, false));
|
||||
|
||||
m_LinkManager->ForEachPeer([&](ILinkSession* s) {
|
||||
// ensure connected session
|
||||
if(not(s && s->IsEstablished()))
|
||||
return;
|
||||
// check if public router
|
||||
const auto other_rc = s->GetRemoteRC();
|
||||
if(not other_rc.IsPublicRouter())
|
||||
return;
|
||||
// dont send it to the owner
|
||||
if(other_rc.pubkey == rc.pubkey)
|
||||
return;
|
||||
// encode message
|
||||
ILinkSession::Message_t msg;
|
||||
msg.resize(1024);
|
||||
llarp_buffer_t buf(msg);
|
||||
if(not m.BEncode(&buf))
|
||||
return;
|
||||
msg.resize(buf.cur - buf.base);
|
||||
// send message
|
||||
if(s->SendMessageBuffer(std::move(msg), nullptr))
|
||||
{
|
||||
sent = true;
|
||||
}
|
||||
});
|
||||
return sent;
|
||||
}
|
||||
|
||||
} // namespace llarp
|
@ -0,0 +1,32 @@
|
||||
#ifndef LLARP_RC_GOSSIPER_HPP
|
||||
#define LLARP_RC_GOSSIPER_HPP
|
||||
|
||||
#include <util/decaying_hashset.hpp>
|
||||
#include <router/i_gossiper.hpp>
|
||||
#include <router/i_outbound_message_handler.hpp>
|
||||
#include <link/i_link_manager.hpp>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
struct RCGossiper : public I_RCGossiper
|
||||
{
|
||||
RCGossiper();
|
||||
|
||||
~RCGossiper() override = default;
|
||||
|
||||
bool
|
||||
GossipRC(const RouterContact &rc) override;
|
||||
|
||||
void
|
||||
Decay(llarp_time_t now) override;
|
||||
|
||||
void
|
||||
Init(ILinkManager *);
|
||||
|
||||
private:
|
||||
ILinkManager *m_LinkManager = nullptr;
|
||||
util::DecayingHashSet< RouterID > m_Filter;
|
||||
};
|
||||
} // namespace llarp
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue