2019-01-16 00:24:16 +00:00
|
|
|
#include <dht/context.hpp>
|
2018-12-15 16:21:52 +00:00
|
|
|
#include <dht/messages/findintro.hpp>
|
2019-01-16 00:24:16 +00:00
|
|
|
#include <dht/messages/gotintro.hpp>
|
|
|
|
#include <routing/message.hpp>
|
2020-01-22 22:30:24 +00:00
|
|
|
#include <router/abstractrouter.hpp>
|
|
|
|
#include <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;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeVerifyVersion("V", version, LLARP_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
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictInt("V", LLARP_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-01-27 21:30:41 +00:00
|
|
|
return false;
|
|
|
|
|
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 =
|
|
|
|
dht.GetRouter()->nodedb()->FindClosestTo(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-04-07 18:38:56 +00:00
|
|
|
if (maybe.has_value())
|
2020-02-20 16:23:41 +00:00
|
|
|
{
|
|
|
|
replies.emplace_back(new GotIntroMessage({maybe.value()}, txID));
|
|
|
|
}
|
|
|
|
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
|