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>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dht
|
|
|
|
{
|
2019-07-30 23:42:13 +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", N, read, k, val))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!BEncodeMaybeReadDictInt("R", R, read, k, val))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!BEncodeMaybeReadDictEntry("S", S, read, k, val))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!BEncodeMaybeReadDictInt("T", T, read, k, val))
|
|
|
|
return false;
|
|
|
|
|
2019-11-22 21:14:37 +00:00
|
|
|
if(!BEncodeMaybeVerifyVersion("V", version, LLARP_PROTO_VERSION, read, k,
|
2019-01-16 00:24:16 +00:00
|
|
|
val))
|
|
|
|
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(N.Empty())
|
|
|
|
{
|
|
|
|
// recursion
|
|
|
|
if(!BEncodeWriteDictInt("R", R, buf))
|
|
|
|
return false;
|
|
|
|
// service address
|
|
|
|
if(!BEncodeWriteDictEntry("S", S, buf))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!BEncodeWriteDictEntry("N", N, buf))
|
|
|
|
return false;
|
|
|
|
// recursion
|
|
|
|
if(!BEncodeWriteDictInt("R", R, buf))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// txid
|
|
|
|
if(!BEncodeWriteDictInt("T", T, buf))
|
|
|
|
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
|
|
|
{
|
|
|
|
if(R > 5)
|
|
|
|
{
|
|
|
|
llarp::LogError("R value too big, ", R, "> 5");
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-22 15:08:00 +00:00
|
|
|
auto& dht = *ctx->impl;
|
|
|
|
if(dht.pendingIntrosetLookups().HasPendingLookupFrom(TXOwner{From, T}))
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("duplicate FIM from ", From, " txid=", T);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Key_t peer;
|
|
|
|
std::set< Key_t > exclude = {dht.OurKey(), From};
|
|
|
|
if(N.Empty())
|
|
|
|
{
|
|
|
|
llarp::LogInfo("lookup ", S.ToString());
|
|
|
|
const auto introset = dht.GetIntroSetByServiceAddress(S);
|
|
|
|
if(introset)
|
|
|
|
{
|
|
|
|
service::IntroSet i = *introset;
|
|
|
|
replies.emplace_back(new GotIntroMessage({i}, T));
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-06 17:03:40 +00:00
|
|
|
|
|
|
|
if(R == 0)
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2019-07-06 17:03:40 +00:00
|
|
|
// we don't have it
|
|
|
|
Key_t target = S.ToKey();
|
|
|
|
Key_t closer;
|
|
|
|
// find closer peer
|
|
|
|
if(!dht.Nodes()->FindClosest(target, closer))
|
|
|
|
return false;
|
|
|
|
if(relayed)
|
|
|
|
dht.LookupIntroSetForPath(S, T, pathID, closer);
|
|
|
|
else
|
|
|
|
replies.emplace_back(new GotIntroMessage(From, closer, T));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Key_t us = dht.OurKey();
|
|
|
|
Key_t target = S.ToKey();
|
|
|
|
// we are recursive
|
|
|
|
if(dht.Nodes()->FindCloseExcluding(target, peer, exclude))
|
|
|
|
{
|
|
|
|
if(relayed)
|
|
|
|
dht.LookupIntroSetForPath(S, T, pathID, peer);
|
2019-01-16 00:24:16 +00:00
|
|
|
else
|
|
|
|
{
|
2019-07-06 17:03:40 +00:00
|
|
|
if((us ^ target) < (peer ^ target))
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2019-07-06 17:03:40 +00:00
|
|
|
// we are not closer than our peer to the target so don't
|
|
|
|
// recurse farther
|
2019-01-16 00:24:16 +00:00
|
|
|
replies.emplace_back(new GotIntroMessage({}, T));
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-06 17:03:40 +00:00
|
|
|
if(R > 0)
|
|
|
|
dht.LookupIntroSetRecursive(S, From, T, peer, R - 1);
|
|
|
|
else
|
|
|
|
dht.LookupIntroSetIterative(S, From, T, peer);
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
2019-07-06 17:03:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no more closer peers
|
|
|
|
replies.emplace_back(new GotIntroMessage({}, T));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(relayed)
|
|
|
|
{
|
|
|
|
// tag lookup
|
|
|
|
if(dht.Nodes()->GetRandomNodeExcluding(peer, exclude))
|
|
|
|
{
|
|
|
|
dht.LookupTagForPath(N, T, pathID, peer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// no more closer peers
|
|
|
|
replies.emplace_back(new GotIntroMessage({}, T));
|
|
|
|
return true;
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-07-06 17:03:40 +00:00
|
|
|
if(R == 0)
|
|
|
|
{
|
|
|
|
// base case
|
|
|
|
auto introsets = dht.FindRandomIntroSetsWithTagExcluding(N, 2, {});
|
|
|
|
std::vector< service::IntroSet > reply;
|
|
|
|
for(const auto& introset : introsets)
|
|
|
|
{
|
|
|
|
reply.push_back(introset);
|
|
|
|
}
|
|
|
|
replies.emplace_back(new GotIntroMessage(reply, T));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(R < 5)
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
|
|
|
// tag lookup
|
2019-02-22 15:08:00 +00:00
|
|
|
if(dht.Nodes()->GetRandomNodeExcluding(peer, exclude))
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2019-07-06 17:03:40 +00:00
|
|
|
dht.LookupTagRecursive(N, From, T, peer, R - 1);
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
replies.emplace_back(new GotIntroMessage({}, T));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-07-06 17:03:40 +00:00
|
|
|
// too big R value
|
|
|
|
replies.emplace_back(new GotIntroMessage({}, T));
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-06 17:03:40 +00:00
|
|
|
|
2019-01-16 00:24:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} // namespace dht
|
|
|
|
} // namespace llarp
|