2018-12-12 00:48:54 +00:00
|
|
|
#include <dht/context.hpp>
|
|
|
|
#include <dht/messages/findintro.hpp>
|
|
|
|
#include <dht/messages/gotintro.hpp>
|
2018-12-12 02:04:32 +00:00
|
|
|
#include <routing/message.hpp>
|
2018-07-12 18:21:44 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dht
|
|
|
|
{
|
2018-07-18 20:58:16 +00:00
|
|
|
/*
|
2018-07-17 04:37:50 +00:00
|
|
|
struct IntroSetLookupInformer
|
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::Router* router;
|
2018-07-17 04:37:50 +00:00
|
|
|
service::Address target;
|
|
|
|
|
|
|
|
void
|
|
|
|
SendReply(const llarp::routing::IMessage* msg)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
2018-07-18 20:58:16 +00:00
|
|
|
*/
|
2018-07-17 04:37:50 +00:00
|
|
|
|
2018-07-12 18:21:44 +00:00
|
|
|
FindIntroMessage::~FindIntroMessage()
|
|
|
|
{
|
|
|
|
}
|
2018-07-16 21:22:25 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
FindIntroMessage::DecodeKey(llarp_buffer_t k, llarp_buffer_t* val)
|
|
|
|
{
|
2018-08-10 03:51:38 +00:00
|
|
|
bool read = false;
|
2018-07-17 04:37:50 +00:00
|
|
|
|
2018-07-18 03:10:21 +00:00
|
|
|
if(!BEncodeMaybeReadDictEntry("N", N, read, k, val))
|
|
|
|
return false;
|
|
|
|
|
2018-07-17 04:37:50 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
if(!BEncodeMaybeReadVersion("V", version, LLARP_PROTO_VERSION, read, k,
|
|
|
|
val))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return read;
|
2018-07-16 21:22:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FindIntroMessage::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
2018-07-17 04:37:50 +00:00
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// message id
|
|
|
|
if(!BEncodeWriteDictMsgType(buf, "A", "F"))
|
|
|
|
return false;
|
2018-08-10 21:34:11 +00:00
|
|
|
if(N.Empty())
|
2018-07-18 03:10:21 +00:00
|
|
|
{
|
2018-11-08 15:15:02 +00:00
|
|
|
// recursion
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("R", R, buf))
|
2018-07-18 03:10:21 +00:00
|
|
|
return false;
|
|
|
|
// service address
|
|
|
|
if(!BEncodeWriteDictEntry("S", S, buf))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!BEncodeWriteDictEntry("N", N, buf))
|
|
|
|
return false;
|
2018-11-08 15:15:02 +00:00
|
|
|
// recursion
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("R", R, buf))
|
2018-07-18 03:10:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-07-17 04:37:50 +00:00
|
|
|
// txid
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("T", T, buf))
|
2018-07-17 04:37:50 +00:00
|
|
|
return false;
|
|
|
|
// protocol version
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("V", LLARP_PROTO_VERSION, buf))
|
2018-07-17 04:37:50 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return bencode_end(buf);
|
2018-07-16 21:22:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FindIntroMessage::HandleMessage(
|
|
|
|
llarp_dht_context* ctx,
|
2018-09-02 18:25:42 +00:00
|
|
|
std::vector< std::unique_ptr< IMessage > >& replies) const
|
2018-07-16 21:22:25 +00:00
|
|
|
{
|
2018-08-04 02:59:32 +00:00
|
|
|
if(R > 5)
|
|
|
|
{
|
|
|
|
llarp::LogError("R value too big, ", R, "> 5");
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-18 20:58:16 +00:00
|
|
|
auto& dht = ctx->impl;
|
2018-08-29 20:40:26 +00:00
|
|
|
if(dht.pendingIntrosetLookups.HasPendingLookupFrom(TXOwner{From, T}))
|
2018-08-10 03:51:38 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("duplicate FIM from ", From, " txid=", T);
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-18 20:58:16 +00:00
|
|
|
Key_t peer;
|
|
|
|
std::set< Key_t > exclude = {dht.OurKey(), From};
|
2018-08-10 21:34:11 +00:00
|
|
|
if(N.Empty())
|
2018-07-17 04:37:50 +00:00
|
|
|
{
|
2018-08-10 21:34:11 +00:00
|
|
|
llarp::LogInfo("lookup ", S.ToString());
|
2018-07-18 20:58:16 +00:00
|
|
|
const auto introset = dht.GetIntroSetByServiceAddress(S);
|
|
|
|
if(introset)
|
|
|
|
{
|
2018-08-02 01:41:40 +00:00
|
|
|
service::IntroSet i = *introset;
|
2018-09-02 18:25:42 +00:00
|
|
|
replies.emplace_back(new GotIntroMessage({i}, T));
|
2018-08-14 21:17:18 +00:00
|
|
|
return true;
|
2018-07-18 20:58:16 +00:00
|
|
|
}
|
|
|
|
else
|
2018-07-17 04:37:50 +00:00
|
|
|
{
|
2018-08-10 21:34:11 +00:00
|
|
|
if(R == 0)
|
2018-07-18 03:10:21 +00:00
|
|
|
{
|
2018-11-08 15:15:02 +00:00
|
|
|
// we don't have it
|
2019-01-02 01:03:53 +00:00
|
|
|
Key_t target = S.ToKey();
|
2018-11-08 15:15:02 +00:00
|
|
|
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));
|
2018-08-14 21:17:18 +00:00
|
|
|
return true;
|
2018-07-18 03:10:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-10 17:22:59 +00:00
|
|
|
Key_t us = dht.OurKey();
|
2019-01-02 01:03:53 +00:00
|
|
|
Key_t target = S.ToKey();
|
2018-08-02 01:41:40 +00:00
|
|
|
// we are recursive
|
2018-08-10 21:34:11 +00:00
|
|
|
if(dht.nodes->FindCloseExcluding(target, peer, exclude))
|
2018-08-02 01:41:40 +00:00
|
|
|
{
|
|
|
|
if(relayed)
|
|
|
|
dht.LookupIntroSetForPath(S, T, pathID, peer);
|
2018-08-10 03:51:38 +00:00
|
|
|
else
|
2018-08-10 21:34:11 +00:00
|
|
|
{
|
|
|
|
if((us ^ target) < (peer ^ target))
|
|
|
|
{
|
|
|
|
// we are not closer than our peer to the target so don't
|
2018-08-29 20:40:26 +00:00
|
|
|
// recurse farther
|
2018-09-02 18:25:42 +00:00
|
|
|
replies.emplace_back(new GotIntroMessage({}, T));
|
2018-08-14 21:17:18 +00:00
|
|
|
return true;
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
2018-08-29 20:40:26 +00:00
|
|
|
else if(R > 0)
|
|
|
|
dht.LookupIntroSetRecursive(S, From, T, peer, R - 1);
|
2018-08-10 21:34:11 +00:00
|
|
|
else
|
2018-08-29 20:40:26 +00:00
|
|
|
dht.LookupIntroSetIterative(S, From, T, peer);
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
2018-08-29 20:40:26 +00:00
|
|
|
return true;
|
2018-08-02 01:41:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-01 12:47:14 +00:00
|
|
|
// no more closer peers
|
|
|
|
replies.emplace_back(new GotIntroMessage({}, T));
|
2018-08-29 20:40:26 +00:00
|
|
|
return true;
|
2018-08-02 01:41:40 +00:00
|
|
|
}
|
2018-07-18 03:10:21 +00:00
|
|
|
}
|
2018-07-17 04:37:50 +00:00
|
|
|
}
|
2018-07-16 21:22:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-18 20:58:16 +00:00
|
|
|
if(relayed)
|
2018-07-17 04:37:50 +00:00
|
|
|
{
|
2018-07-18 20:58:16 +00:00
|
|
|
// tag lookup
|
2018-08-04 02:59:32 +00:00
|
|
|
if(dht.nodes->GetRandomNodeExcluding(peer, exclude))
|
2018-07-18 20:58:16 +00:00
|
|
|
{
|
|
|
|
dht.LookupTagForPath(N, T, pathID, peer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-01 12:47:14 +00:00
|
|
|
// no more closer peers
|
|
|
|
replies.emplace_back(new GotIntroMessage({}, T));
|
|
|
|
return true;
|
2018-07-18 20:58:16 +00:00
|
|
|
}
|
2018-07-17 04:37:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-10 03:51:38 +00:00
|
|
|
if(R == 0)
|
2018-07-18 20:58:16 +00:00
|
|
|
{
|
2018-08-29 20:40:26 +00:00
|
|
|
// base case
|
|
|
|
auto introsets = dht.FindRandomIntroSetsWithTagExcluding(N, 2, {});
|
2018-08-02 01:41:40 +00:00
|
|
|
std::vector< service::IntroSet > reply;
|
|
|
|
for(const auto& introset : introsets)
|
|
|
|
{
|
|
|
|
reply.push_back(introset);
|
|
|
|
}
|
2018-09-02 18:25:42 +00:00
|
|
|
replies.emplace_back(new GotIntroMessage(reply, T));
|
2018-08-14 21:17:18 +00:00
|
|
|
return true;
|
2018-07-18 20:58:16 +00:00
|
|
|
}
|
2018-10-21 14:19:49 +00:00
|
|
|
else if(R < 5)
|
2018-07-18 20:58:16 +00:00
|
|
|
{
|
2018-07-18 22:50:05 +00:00
|
|
|
// tag lookup
|
2018-08-04 02:59:32 +00:00
|
|
|
if(dht.nodes->GetRandomNodeExcluding(peer, exclude))
|
2018-07-18 20:58:16 +00:00
|
|
|
{
|
2018-08-29 20:40:26 +00:00
|
|
|
dht.LookupTagRecursive(N, From, T, peer, R - 1);
|
2018-07-18 20:58:16 +00:00
|
|
|
}
|
2018-10-21 14:19:49 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
replies.emplace_back(new GotIntroMessage({}, T));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// too big R value
|
|
|
|
replies.emplace_back(new GotIntroMessage({}, T));
|
2018-07-18 20:58:16 +00:00
|
|
|
}
|
2018-07-17 04:37:50 +00:00
|
|
|
}
|
2018-07-16 21:22:25 +00:00
|
|
|
}
|
2018-07-18 20:58:16 +00:00
|
|
|
return true;
|
2018-07-16 21:22:25 +00:00
|
|
|
}
|
2018-07-12 18:21:44 +00:00
|
|
|
} // namespace dht
|
2018-08-29 20:40:26 +00:00
|
|
|
} // namespace llarp
|