lokinet/llarp/dht/messages/pubintro.cpp

203 lines
5.6 KiB
C++
Raw Permalink Normal View History

#include "pubintro.hpp"
2019-01-16 00:24:16 +00:00
#include <llarp/dht/context.hpp>
#include "gotintro.hpp"
#include <llarp/messages/dht_immediate.hpp>
#include <llarp/router/abstractrouter.hpp>
#include <llarp/routing/dht_message.hpp>
#include <llarp/nodedb.hpp>
2019-01-16 00:24:16 +00:00
#include <llarp/tooling/dht_event.hpp>
2019-01-16 00:24:16 +00:00
namespace llarp
{
namespace dht
{
const uint64_t PublishIntroMessage::MaxPropagationDepth = 5;
PublishIntroMessage::~PublishIntroMessage() = default;
2019-01-16 00:24:16 +00:00
bool
PublishIntroMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val)
2019-01-16 00:24:16 +00:00
{
bool read = false;
if (!BEncodeMaybeReadDictEntry("I", introset, read, key, val))
2019-01-16 00:24:16 +00:00
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))
2019-01-16 00:24:16 +00:00
return false;
if (read)
{
relayed = relayedInt;
return true;
}
if (!BEncodeMaybeReadDictInt("T", txID, read, key, val))
2019-01-16 00:24:16 +00:00
return false;
if (read)
return true;
if (!BEncodeMaybeReadDictInt("V", version, read, key, val))
2019-01-16 00:24:16 +00:00
return false;
if (read)
return true;
return false;
2019-01-16 00:24:16 +00:00
}
bool
PublishIntroMessage::HandleMessage(
llarp_dht_context* ctx, std::vector<std::unique_ptr<IMessage>>& replies) const
2019-01-16 00:24:16 +00:00
{
2020-03-07 01:20:11 +00:00
const auto now = ctx->impl->Now();
2022-08-06 18:14:12 +00:00
const llarp::dht::Key_t addr{introset.derivedSigningKey.data()};
auto router = ctx->impl->GetRouter();
router->NotifyRouterEvent<tooling::PubIntroReceivedEvent>(
router->pubkey(),
Key_t(relayed ? router->pubkey() : From.data()),
addr,
txID,
relayOrder);
auto& dht = *ctx->impl;
if (!introset.Verify(now))
2019-01-16 00:24:16 +00:00
{
llarp::LogWarn("Received PublishIntroMessage with invalid introset: ", introset);
2019-01-16 00:24:16 +00:00
// don't propogate or store
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
}
if (introset.IsExpired(now + llarp::service::MAX_INTROSET_TIME_DELTA))
2019-01-16 00:24:16 +00:00
{
// don't propogate or store
llarp::LogWarn("Received PublishIntroMessage with expired Introset: ", introset);
2019-01-16 00:24:16 +00:00
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
}
// identify closest 4 routers
auto closestRCs =
dht.GetRouter()->nodedb()->FindManyClosestTo(addr, IntroSetStorageRedundancy);
if (closestRCs.size() != IntroSetStorageRedundancy)
2019-01-16 00:24:16 +00:00
{
llarp::LogWarn("Received PublishIntroMessage but only know ", closestRCs.size(), " nodes");
2020-02-20 15:36:29 +00:00
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
2019-01-16 00:24:16 +00:00
}
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 < IntroSetStorageRedundancy);
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);
if (relayed)
{
dht.PropagateLocalIntroSet(pathID, txID, introset, peer, 0);
}
else
{
dht.PropagateIntroSetTo(From, txID, introset, peer, 0);
}
}
};
if (relayed)
{
if (relayOrder >= IntroSetStorageRedundancy)
{
llarp::LogWarn("Received PublishIntroMessage with invalid relayOrder: ", relayOrder);
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
}
2022-08-06 18:14:12 +00:00
llarp::LogInfo("Relaying PublishIntroMessage for ", addr, ", txid=", txID);
propagateIfNotUs(relayOrder);
}
else
{
int candidateNumber = -1;
int index = 0;
for (const auto& rc : closestRCs)
{
if (rc.pubkey == dht.OurKey())
{
candidateNumber = index;
break;
}
++index;
}
if (candidateNumber >= 0)
{
LogInfo(
"Received PubIntro for ",
2022-08-06 18:14:12 +00:00
addr,
", txid=",
txID,
" and we are candidate ",
candidateNumber);
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: ",
2022-08-06 18:14:12 +00:00
addr,
", txid=",
txID,
", message from: ",
From);
}
}
2019-01-16 00:24:16 +00:00
return true;
}
bool
PublishIntroMessage::BEncode(llarp_buffer_t* buf) const
2019-01-16 00:24:16 +00:00
{
if (!bencode_start_dict(buf))
2019-01-16 00:24:16 +00:00
return false;
if (!BEncodeWriteDictMsgType(buf, "A", "I"))
2019-01-16 00:24:16 +00:00
return false;
if (!BEncodeWriteDictEntry("I", introset, buf))
2019-01-16 00:24:16 +00:00
return false;
if (!BEncodeWriteDictInt("O", relayOrder, buf))
return false;
if (!BEncodeWriteDictInt("R", relayed, buf))
2019-01-16 00:24:16 +00:00
return false;
if (!BEncodeWriteDictInt("T", txID, buf))
2019-01-16 00:24:16 +00:00
return false;
2022-05-26 15:59:44 +00:00
if (!BEncodeWriteDictInt("V", llarp::constants::proto_version, buf))
2019-01-16 00:24:16 +00:00
return false;
return bencode_end(buf);
}
} // namespace dht
} // namespace llarp