lokinet/llarp/dht/messages/findintro.cpp

149 lines
3.8 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;
2020-01-27 21:30:41 +00:00
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;
2019-11-22 21:14:37 +00:00
if(!BEncodeMaybeVerifyVersion("V", version, LLARP_PROTO_VERSION, read, k,
2019-12-06 17:13:09 +00:00
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))
return false;
// message id
if(!BEncodeWriteDictMsgType(buf, "A", "F"))
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
2020-01-27 21:30:41 +00:00
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))
return false;
return bencode_end(buf);
}
bool
FindIntroMessage::HandleMessage(
2019-05-03 13:15:03 +00:00
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
2020-01-27 21:30:41 +00:00
if(not tagName.Empty())
return false;
// bad request (request for zero-key)
2020-01-27 21:30:41 +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
// we are relaying this message for e.g. a client
if(relayed)
{
uint32_t numDesired = 0;
if(relayOrder == 0)
numDesired = 2;
else if(relayOrder == 1)
numDesired = 4;
else
{
// TODO: consider forward-compatibility here
LogError("Error: relayOrder must be 0 or 1");
return false;
}
2020-01-27 21:30:41 +00:00
auto closestRCs =
dht.GetRouter()->nodedb()->FindClosestTo(location, numDesired);
2020-01-27 21:30:41 +00:00
// if relayOrder == 1, we want the 3rd and 4th closest, so remove the
// 1st and 2nd closest
if(relayOrder == 1)
{
auto itr = closestRCs.begin();
std::advance(itr, 2);
closestRCs.erase(closestRCs.begin(), itr);
}
for(const auto& entry : closestRCs)
{
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.has_value())
{
replies.emplace_back(new GotIntroMessage({maybe.value()}, 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