2021-03-09 22:24:35 +00:00
|
|
|
#include "pubintro.hpp"
|
2019-01-16 00:24:16 +00:00
|
|
|
|
2021-03-09 22:24:35 +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
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/tooling/dht_event.hpp>
|
2020-03-01 01:58:45 +00:00
|
|
|
|
2019-01-16 00:24:16 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dht
|
|
|
|
{
|
2020-01-23 12:11:11 +00:00
|
|
|
const uint64_t PublishIntroMessage::MaxPropagationDepth = 5;
|
2020-04-07 18:38:56 +00:00
|
|
|
PublishIntroMessage::~PublishIntroMessage() = default;
|
2019-01-16 00:24:16 +00:00
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
PublishIntroMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val)
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
|
|
|
bool read = false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictEntry("I", introset, read, key, val))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (read)
|
2020-02-28 18:01:02 +00:00
|
|
|
return true;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictInt("O", relayOrder, read, key, val))
|
2020-02-20 00:07:46 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (read)
|
2020-02-28 18:01:02 +00:00
|
|
|
return true;
|
|
|
|
|
2020-02-20 00:07:46 +00:00
|
|
|
uint64_t relayedInt = (relayed ? 1 : 0);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictInt("R", relayedInt, read, key, val))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (read)
|
2020-02-28 18:01:02 +00:00
|
|
|
{
|
|
|
|
relayed = relayedInt;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictInt("T", txID, read, key, val))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (read)
|
2020-02-28 18:01:02 +00:00
|
|
|
return true;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictInt("V", version, read, key, val))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (read)
|
2020-02-28 18:01:02 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PublishIntroMessage::HandleMessage(
|
2020-04-07 18:38:56 +00:00
|
|
|
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()};
|
2020-03-01 01:58:45 +00:00
|
|
|
|
|
|
|
auto router = ctx->impl->GetRouter();
|
2020-04-07 18:38:56 +00:00
|
|
|
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
|
|
|
{
|
2020-04-07 18:38:56 +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;
|
|
|
|
}
|
2019-01-26 15:40:58 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (introset.IsExpired(now + llarp::service::MAX_INTROSET_TIME_DELTA))
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
|
|
|
// don't propogate or store
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogWarn("Received PublishIntroMessage with expired Introset: ", introset);
|
2019-01-16 00:24:16 +00:00
|
|
|
replies.emplace_back(new GotIntroMessage({}, txID));
|
|
|
|
return true;
|
|
|
|
}
|
2020-02-20 00:07:46 +00:00
|
|
|
|
|
|
|
// identify closest 4 routers
|
2021-02-02 14:35:40 +00:00
|
|
|
auto closestRCs =
|
|
|
|
dht.GetRouter()->nodedb()->FindManyClosestTo(addr, IntroSetStorageRedundancy);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (closestRCs.size() != IntroSetStorageRedundancy)
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +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
|
|
|
}
|
2020-02-20 00:07:46 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
const auto& us = dht.OurKey();
|
2020-02-20 00:30:58 +00:00
|
|
|
|
2020-02-20 00:07:46 +00:00
|
|
|
// function to identify the closest 4 routers we know of for this introset
|
2020-02-26 20:27:27 +00:00
|
|
|
auto propagateIfNotUs = [&](size_t index) {
|
2020-03-02 16:54:11 +00:00
|
|
|
assert(index < IntroSetStorageRedundancy);
|
2020-02-20 00:30:58 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
const auto& rc = closestRCs[index];
|
2020-02-26 20:27:27 +00:00
|
|
|
const Key_t peer{rc.pubkey};
|
2020-02-20 00:30:58 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (peer == us)
|
2020-02-20 00:30:58 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogInfo("we are peer ", index, " so storing instead of propagating");
|
2020-02-26 20:27:27 +00:00
|
|
|
|
2020-02-20 00:30:58 +00:00
|
|
|
dht.services()->PutNode(introset);
|
|
|
|
replies.emplace_back(new GotIntroMessage({introset}, txID));
|
|
|
|
}
|
2020-02-26 20:27:27 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogInfo("propagating to peer ", index);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (relayed)
|
2020-03-01 17:30:05 +00:00
|
|
|
{
|
|
|
|
dht.PropagateLocalIntroSet(pathID, txID, introset, peer, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dht.PropagateIntroSetTo(From, txID, introset, peer, 0);
|
|
|
|
}
|
2020-02-26 20:27:27 +00:00
|
|
|
}
|
2020-02-20 00:07:46 +00:00
|
|
|
};
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (relayed)
|
2020-02-20 00:07:46 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (relayOrder >= IntroSetStorageRedundancy)
|
2020-02-20 00:07:46 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogWarn("Received PublishIntroMessage with invalid relayOrder: ", relayOrder);
|
2020-02-20 00:07:46 +00:00
|
|
|
replies.emplace_back(new GotIntroMessage({}, txID));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-06 18:14:12 +00:00
|
|
|
llarp::LogInfo("Relaying PublishIntroMessage for ", addr, ", txid=", txID);
|
2020-02-26 20:27:27 +00:00
|
|
|
|
|
|
|
propagateIfNotUs(relayOrder);
|
2020-02-20 00:07:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-29 00:00:49 +00:00
|
|
|
int candidateNumber = -1;
|
2020-04-07 18:38:56 +00:00
|
|
|
int index = 0;
|
|
|
|
for (const auto& rc : closestRCs)
|
2020-02-20 00:07:46 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (rc.pubkey == dht.OurKey())
|
2020-02-20 00:07:46 +00:00
|
|
|
{
|
2020-02-29 00:00:49 +00:00
|
|
|
candidateNumber = index;
|
2020-02-20 00:07:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-02-29 00:00:49 +00:00
|
|
|
++index;
|
2020-02-20 00:07:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (candidateNumber >= 0)
|
2020-02-20 00:07:46 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
LogInfo(
|
|
|
|
"Received PubIntro for ",
|
2022-08-06 18:14:12 +00:00
|
|
|
addr,
|
2020-04-07 18:38:56 +00:00
|
|
|
", txid=",
|
|
|
|
txID,
|
|
|
|
" and we are candidate ",
|
|
|
|
candidateNumber);
|
2020-02-20 00:07:46 +00:00
|
|
|
dht.services()->PutNode(introset);
|
|
|
|
replies.emplace_back(new GotIntroMessage({introset}, txID));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-26 20:27:27 +00:00
|
|
|
LogWarn(
|
|
|
|
"!!! Received PubIntro with relayed==false but we aren't"
|
|
|
|
" candidate, intro derived key: ",
|
2022-08-06 18:14:12 +00:00
|
|
|
addr,
|
2020-04-07 18:38:56 +00:00
|
|
|
", txid=",
|
|
|
|
txID,
|
|
|
|
", message from: ",
|
|
|
|
From);
|
2020-02-20 00:07:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 00:24:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
PublishIntroMessage::BEncode(llarp_buffer_t* buf) const
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_start_dict(buf))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictMsgType(buf, "A", "I"))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictEntry("I", introset, buf))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictInt("O", relayOrder, buf))
|
2020-02-20 00:07:46 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictInt("R", relayed, buf))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
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
|