lokinet/llarp/dht/messages/findintro.cpp

140 lines
3.7 KiB
C++
Raw Normal View History

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>
#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;
if (!BEncodeMaybeReadDictEntry("N", tagName, read, k, val))
return false;
if (!BEncodeMaybeReadDictInt("O", relayOrder, read, k, val))
2019-01-16 00:24:16 +00:00
return false;
if (!BEncodeMaybeReadDictEntry("S", location, read, k, val))
2019-01-16 00:24:16 +00:00
return false;
if (!BEncodeMaybeReadDictInt("T", txID, read, k, val))
2019-01-16 00:24:16 +00:00
return false;
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
{
if (!bencode_start_dict(buf))
2019-01-16 00:24:16 +00:00
return false;
// message id
if (!BEncodeWriteDictMsgType(buf, "A", "F"))
2019-01-16 00:24:16 +00:00
return false;
if (tagName.Empty())
2019-01-16 00:24:16 +00:00
{
// relay order
if (!BEncodeWriteDictInt("O", relayOrder, buf))
return false;
2019-01-16 00:24:16 +00:00
// service address
if (!BEncodeWriteDictEntry("S", location, buf))
2019-01-16 00:24:16 +00:00
return false;
}
else
{
if (!BEncodeWriteDictEntry("N", tagName, buf))
2019-01-16 00:24:16 +00:00
return false;
// relay order
if (!BEncodeWriteDictInt("O", relayOrder, buf))
return false;
2019-01-16 00:24:16 +00:00
}
// txid
if (!BEncodeWriteDictInt("T", txID, buf))
2019-01-16 00:24:16 +00:00
return false;
// protocol version
if (!BEncodeWriteDictInt("V", LLARP_PROTO_VERSION, buf))
2019-01-16 00:24:16 +00:00
return false;
return bencode_end(buf);
}
bool
FindIntroMessage::HandleMessage(
llarp_dht_context* ctx, std::vector<IMessage::Ptr_t>& replies) const
2019-01-16 00:24:16 +00:00
{
auto& dht = *ctx->impl;
if (dht.pendingIntrosetLookups().HasPendingLookupFrom(TXOwner{From, txID}))
2019-01-16 00:24:16 +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
if (not tagName.Empty())
{
2020-01-27 21:30:41 +00:00
return false;
}
// bad request (request for zero-key)
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
// we are relaying this message for e.g. a client
if (relayed)
{
if (relayOrder >= IntroSetStorageRedundancy)
{
llarp::LogWarn("Invalid relayOrder received: ", relayOrder);
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
}
2020-01-27 21:30:41 +00:00
auto closestRCs =
dht.GetRouter()->nodedb()->FindClosestTo(location, IntroSetStorageRedundancy);
2020-01-27 21:30:41 +00:00
if (closestRCs.size() <= relayOrder)
{
llarp::LogWarn("Can't fulfill FindIntro for relayOrder: ", relayOrder);
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
}
const auto& entry = closestRCs[relayOrder];
Key_t peer = Key_t(entry.pubkey);
dht.LookupIntroSetForPath(location, txID, pathID, peer, 0);
2019-01-16 00:24:16 +00:00
}
else
{
// we should have this value if introset was propagated properly
const auto maybe = dht.GetIntroSetByLocation(location);
if (maybe)
{
replies.emplace_back(new GotIntroMessage({*maybe}, txID));
}
else
{
LogWarn("Got FIM with relayed == false and we don't have entry");
replies.emplace_back(new GotIntroMessage({}, txID));
}
2019-01-16 00:24:16 +00:00
}
return true;
}
} // namespace dht
} // namespace llarp