lokinet/llarp/dht/find_intro.cpp

184 lines
4.7 KiB
C++
Raw Normal View History

#include <llarp/dht/context.hpp>
2018-07-12 18:21:44 +00:00
#include <llarp/dht/messages/findintro.hpp>
#include <llarp/dht/messages/gotintro.hpp>
2018-07-17 04:37:50 +00:00
#include <llarp/routing/message.hpp>
2018-07-12 18:21:44 +00:00
namespace llarp
{
namespace dht
{
/*
2018-07-17 04:37:50 +00:00
struct IntroSetLookupInformer
{
llarp_router* router;
service::Address target;
void
SendReply(const llarp::routing::IMessage* msg)
{
}
};
*/
2018-07-17 04:37:50 +00:00
2018-07-12 18:21:44 +00:00
FindIntroMessage::~FindIntroMessage()
{
}
bool
FindIntroMessage::DecodeKey(llarp_buffer_t k, llarp_buffer_t* val)
{
2018-07-17 04:37:50 +00:00
uint64_t i = 0;
bool read = false;
if(!BEncodeMaybeReadDictInt("I", i, read, k, val))
return false;
if(read)
2018-07-17 04:37:50 +00:00
{
iterative = i != 0;
return true;
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;
}
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;
// iterative
if(!BEncodeWriteDictInt("I", iterative ? 1 : 0, buf))
2018-07-17 04:37:50 +00:00
return false;
2018-07-18 03:10:21 +00:00
if(N.IsZero())
{
2018-07-17 04:37:50 +00:00
return false;
2018-07-18 03:10:21 +00:00
// r5n counter
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;
// r5n counter
if(!BEncodeWriteDictInt("R", R, buf))
2018-07-18 03:10:21 +00:00
return false;
}
2018-07-17 04:37:50 +00:00
// txid
if(!BEncodeWriteDictInt("T", T, buf))
2018-07-17 04:37:50 +00:00
return false;
// protocol version
if(!BEncodeWriteDictInt("V", LLARP_PROTO_VERSION, buf))
2018-07-17 04:37:50 +00:00
return false;
return bencode_end(buf);
}
bool
FindIntroMessage::HandleMessage(
llarp_dht_context* ctx,
std::vector< llarp::dht::IMessage* >& replies) const
{
auto& dht = ctx->impl;
Key_t peer;
std::set< Key_t > exclude = {dht.OurKey(), From};
if(N.IsZero())
2018-07-17 04:37:50 +00:00
{
const auto introset = dht.GetIntroSetByServiceAddress(S);
if(introset)
{
2018-08-02 01:41:40 +00:00
service::IntroSet i = *introset;
replies.push_back(new GotIntroMessage({i}, T));
}
else
2018-07-17 04:37:50 +00:00
{
2018-08-02 01:41:40 +00:00
if(iterative)
2018-07-18 03:10:21 +00:00
{
2018-08-02 01:41:40 +00:00
// we are iterative and don't have it, reply with a direct reply
replies.push_back(new GotIntroMessage({}, T));
2018-07-18 03:10:21 +00:00
}
else
{
2018-08-02 01:41:40 +00:00
// we are recursive
if(dht.nodes->FindCloseExcluding(S, peer, exclude))
{
if(relayed)
dht.LookupIntroSetForPath(S, T, pathID, peer);
2018-08-02 04:34:46 +00:00
else if((peer ^ dht.OurKey())
> (peer
^ From)) // peer is closer than us, recursive search
2018-08-02 01:41:40 +00:00
dht.LookupIntroSet(S, From, T, peer);
2018-08-02 04:34:46 +00:00
else // we are closer than peer so do iterative search
dht.LookupIntroSet(S, From, T, peer, true);
2018-08-02 01:41:40 +00:00
}
else
{
llarp::LogError(
"cannot find closer peers for introset lookup for ", S);
}
2018-07-18 03:10:21 +00:00
}
2018-07-17 04:37:50 +00:00
}
}
else
{
if(relayed)
2018-07-17 04:37:50 +00:00
{
// tag lookup
if(dht.nodes->FindCloseExcluding(N.Key(), peer, exclude))
{
dht.LookupTagForPath(N, T, pathID, peer);
}
else
{
llarp::LogWarn("no closer peers for tag ", N.ToString());
}
2018-07-17 04:37:50 +00:00
}
else
{
2018-08-02 01:41:40 +00:00
auto introsets = dht.FindRandomIntroSetsWithTag(N);
2018-07-18 22:50:05 +00:00
if(iterative)
{
2018-08-02 01:41:40 +00:00
std::vector< service::IntroSet > reply;
for(const auto& introset : introsets)
{
reply.push_back(introset);
}
2018-07-18 22:50:05 +00:00
// we are iterative and don't have it, reply with a direct reply
2018-08-02 01:41:40 +00:00
replies.push_back(new GotIntroMessage(reply, T));
}
else
{
2018-07-18 22:50:05 +00:00
// tag lookup
if(dht.nodes->FindCloseExcluding(N.Key(), peer, exclude))
{
2018-08-02 01:41:40 +00:00
dht.LookupTag(N, From, T, peer, introsets, true);
}
}
2018-07-17 04:37:50 +00:00
}
}
return true;
}
2018-07-12 18:21:44 +00:00
} // namespace dht
} // namespace llarp