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/dht/messages/pubintro.cpp

189 lines
5.2 KiB
C++

#include <dht/messages/pubintro.hpp>
5 years ago
#include <dht/context.hpp>
#include <dht/messages/gotintro.hpp>
#include <messages/dht_immediate.hpp>
#include <router/abstractrouter.hpp>
#include <routing/dht_message.hpp>
#include <nodedb.hpp>
5 years ago
namespace llarp
{
namespace dht
{
const uint64_t PublishIntroMessage::MaxPropagationDepth = 5;
PublishIntroMessage::~PublishIntroMessage() = default;
5 years ago
bool
5 years ago
PublishIntroMessage::DecodeKey(const llarp_buffer_t &key,
llarp_buffer_t *val)
5 years ago
{
bool read = false;
if(!BEncodeMaybeReadDictEntry("I", introset, read, key, val))
5 years ago
return false;
if(read)
return true;
if(!BEncodeMaybeReadDictInt("O", relayOrder, read, key, val))
return false;
if(read)
return true;
uint64_t relayedInt = (relayed ? 1 : 0);
if(!BEncodeMaybeReadDictInt("R", relayedInt, read, key, val))
5 years ago
return false;
if(read)
{
relayed = relayedInt;
return true;
}
5 years ago
if(!BEncodeMaybeReadDictInt("T", txID, read, key, val))
return false;
if(read)
return true;
5 years ago
if(!BEncodeMaybeReadDictInt("V", version, read, key, val))
return false;
if(read)
return true;
return false;
5 years ago
}
bool
PublishIntroMessage::HandleMessage(
llarp_dht_context *ctx,
std::vector< std::unique_ptr< IMessage > > &replies) const
{
auto now = ctx->impl->Now();
const auto keyStr = introset.derivedSigningKey.ToHex();
auto &dht = *ctx->impl;
if(!introset.Verify(now))
5 years ago
{
llarp::LogWarn("Received PublishIntroMessage with invalid introset: ",
4 years ago
introset);
5 years ago
// don't propogate or store
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
}
if(introset.IsExpired(now + llarp::service::MAX_INTROSET_TIME_DELTA))
5 years ago
{
// don't propogate or store
llarp::LogWarn("Received PublishIntroMessage with expired Introset: ",
4 years ago
introset);
5 years ago
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
}
const llarp::dht::Key_t addr(introset.derivedSigningKey);
// identify closest 4 routers
static constexpr size_t StorageRedundancy = 4;
auto closestRCs = dht.GetRouter()->nodedb()->FindClosestTo(addr, StorageRedundancy);
if(closestRCs.size() != StorageRedundancy)
5 years ago
{
4 years ago
llarp::LogWarn("Received PublishIntroMessage but only know ",
closestRCs.size(), " nodes");
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
5 years ago
}
4 years ago
const auto &us = dht.OurKey();
// function to identify the closest 4 routers we know of for this introset
auto propagateIfNotUs = [&](size_t index) {
assert(index < StorageRedundancy);
const auto &rc = closestRCs[index];
const Key_t peer{rc.pubkey};
if(peer == us)
{
llarp::LogInfo("we are peer ", index,
" so storing instead of propagating");
dht.services()->PutNode(introset);
replies.emplace_back(new GotIntroMessage({introset}, txID));
}
else
{
llarp::LogInfo("propagating to peer ", index);
dht.PropagateIntroSetTo(From, txID, introset, peer, false, 0);
}
};
4 years ago
if(relayed)
{
if(relayOrder >= StorageRedundancy)
{
4 years ago
llarp::LogWarn(
"Received PublishIntroMessage with invalid relayOrder: ",
relayOrder);
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
}
llarp::LogInfo("Relaying PublishIntroMessage for ", keyStr,
", txid=", txID);
propagateIfNotUs(relayOrder);
}
else
{
bool found = false;
4 years ago
for(const auto &rc : closestRCs)
{
4 years ago
if(rc.pubkey == dht.OurKey())
{
found = true;
break;
}
}
4 years ago
if(found)
{
dht.services()->PutNode(introset);
replies.emplace_back(new GotIntroMessage({introset}, txID));
}
else
{
LogWarn(
"!!! Received PubIntro with relayed==false but we aren't"
" candidate, intro derived key: ",
keyStr, ", txid=", txID, ", message from: ", From);
for(size_t i = 0; i < StorageRedundancy; ++i)
{
propagateIfNotUs(i);
}
}
}
5 years ago
return true;
}
bool
PublishIntroMessage::BEncode(llarp_buffer_t *buf) const
{
if(!bencode_start_dict(buf))
return false;
if(!BEncodeWriteDictMsgType(buf, "A", "I"))
return false;
if(!BEncodeWriteDictEntry("I", introset, buf))
5 years ago
return false;
if(!BEncodeWriteDictInt("O", relayOrder, buf))
return false;
if(!BEncodeWriteDictInt("R", relayed, buf))
5 years ago
return false;
if(!BEncodeWriteDictInt("T", txID, buf))
return false;
if(!BEncodeWriteDictInt("V", LLARP_PROTO_VERSION, buf))
return false;
return bencode_end(buf);
}
} // namespace dht
} // namespace llarp