2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/dht/context.hpp>
|
|
|
|
#include "findintro.hpp"
|
|
|
|
#include "gotintro.hpp"
|
|
|
|
#include <llarp/routing/message.hpp>
|
|
|
|
#include <llarp/router/abstractrouter.hpp>
|
|
|
|
#include <llarp/nodedb.hpp>
|
2019-01-16 00:24:16 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dht
|
|
|
|
{
|
2020-02-20 20:14:20 +00:00
|
|
|
FindIntroMessage::~FindIntroMessage() = default;
|
2019-01-16 00:24:16 +00:00
|
|
|
|
|
|
|
bool
|
2019-02-05 00:41:33 +00:00
|
|
|
FindIntroMessage::DecodeKey(const llarp_buffer_t& k, 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("N", tagName, read, k, val))
|
2020-02-14 16:46:41 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictInt("O", relayOrder, read, k, val))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictEntry("S", location, read, k, val))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictInt("T", txID, read, k, val))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
|
|
|
|
2022-05-26 15:59:44 +00:00
|
|
|
if (!BEncodeMaybeVerifyVersion("V", version, llarp::constants::proto_version, read, k, val))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FindIntroMessage::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_start_dict(buf))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// message id
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictMsgType(buf, "A", "F"))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (tagName.Empty())
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-02-14 18:06:19 +00:00
|
|
|
// relay order
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictInt("O", relayOrder, buf))
|
2020-02-14 18:06:19 +00:00
|
|
|
return false;
|
|
|
|
|
2019-01-16 00:24:16 +00:00
|
|
|
// service address
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictEntry("S", location, buf))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictEntry("N", tagName, buf))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
2020-02-14 18:06:19 +00:00
|
|
|
|
|
|
|
// relay order
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictInt("O", relayOrder, buf))
|
2020-02-14 18:06:19 +00:00
|
|
|
return false;
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
|
|
|
// txid
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictInt("T", txID, buf))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
|
|
|
// protocol version
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FindIntroMessage::HandleMessage(
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_dht_context* ctx, std::vector<IMessage::Ptr_t>& replies) const
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2019-02-22 15:08:00 +00:00
|
|
|
auto& dht = *ctx->impl;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (dht.pendingIntrosetLookups().HasPendingLookupFrom(TXOwner{From, txID}))
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-01-23 12:11:11 +00:00
|
|
|
llarp::LogWarn("duplicate FIM from ", From, " txid=", txID);
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-02-07 19:50:02 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not tagName.Empty())
|
2020-09-17 19:18:08 +00:00
|
|
|
{
|
2020-01-27 21:30:41 +00:00
|
|
|
return false;
|
2020-09-17 19:18:08 +00:00
|
|
|
}
|
2020-02-20 16:23:41 +00:00
|
|
|
// bad request (request for zero-key)
|
2020-04-07 18:38:56 +00:00
|
|
|
if (location.IsZero())
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-01-27 21:30:41 +00:00
|
|
|
// we dont got it
|
|
|
|
replies.emplace_back(new GotIntroMessage({}, txID));
|
2019-07-06 17:03:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-01-27 21:30:41 +00:00
|
|
|
|
2020-02-20 16:23:41 +00:00
|
|
|
// we are relaying this message for e.g. a client
|
2020-04-07 18:38:56 +00:00
|
|
|
if (relayed)
|
2020-02-13 22:19:12 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (relayOrder >= IntroSetStorageRedundancy)
|
2020-02-13 22:19:12 +00:00
|
|
|
{
|
2020-03-02 16:27:07 +00:00
|
|
|
llarp::LogWarn("Invalid relayOrder received: ", relayOrder);
|
|
|
|
replies.emplace_back(new GotIntroMessage({}, txID));
|
|
|
|
return true;
|
2020-02-13 22:19:12 +00:00
|
|
|
}
|
2020-01-27 21:30:41 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
auto closestRCs =
|
2021-02-02 14:35:40 +00:00
|
|
|
dht.GetRouter()->nodedb()->FindManyClosestTo(location, IntroSetStorageRedundancy);
|
2020-01-27 21:30:41 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (closestRCs.size() <= relayOrder)
|
2020-02-07 19:35:57 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp::LogWarn("Can't fulfill FindIntro for relayOrder: ", relayOrder);
|
2020-03-02 16:27:07 +00:00
|
|
|
replies.emplace_back(new GotIntroMessage({}, txID));
|
|
|
|
return true;
|
2020-02-13 22:19:12 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 16:27:07 +00:00
|
|
|
const auto& entry = closestRCs[relayOrder];
|
2020-04-07 18:38:56 +00:00
|
|
|
Key_t peer = Key_t(entry.pubkey);
|
2020-03-02 16:27:07 +00:00
|
|
|
dht.LookupIntroSetForPath(location, txID, pathID, peer, 0);
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-20 16:23:41 +00:00
|
|
|
// we should have this value if introset was propagated properly
|
|
|
|
const auto maybe = dht.GetIntroSetByLocation(location);
|
2020-05-20 19:46:08 +00:00
|
|
|
if (maybe)
|
2020-02-20 16:23:41 +00:00
|
|
|
{
|
2020-05-20 19:46:08 +00:00
|
|
|
replies.emplace_back(new GotIntroMessage({*maybe}, txID));
|
2020-02-20 16:23:41 +00:00
|
|
|
}
|
|
|
|
else
|
2020-02-13 22:19:12 +00:00
|
|
|
{
|
2020-02-20 18:18:05 +00:00
|
|
|
LogWarn("Got FIM with relayed == false and we don't have entry");
|
|
|
|
replies.emplace_back(new GotIntroMessage({}, txID));
|
2020-02-20 16:23:41 +00:00
|
|
|
}
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} // namespace dht
|
|
|
|
} // namespace llarp
|