lokinet/llarp/dht/publish_intro.cpp

106 lines
2.9 KiB
C++
Raw Normal View History

#include <llarp/dht/context.hpp>
#include <llarp/dht/messages/pubintro.hpp>
#include <llarp/messages/dht.hpp>
#include <llarp/messages/dht_immediate.hpp>
#include "router.hpp"
namespace llarp
{
namespace dht
{
PublishIntroMessage::~PublishIntroMessage()
{
}
bool
PublishIntroMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *val)
{
bool read = false;
2018-07-19 04:58:39 +00:00
if(llarp_buffer_eq(key, "E"))
{
2018-07-20 04:50:28 +00:00
return BEncodeReadList(E, val);
2018-07-19 04:58:39 +00:00
}
if(!BEncodeMaybeReadDictEntry("I", I, read, key, val))
return false;
if(!BEncodeMaybeReadDictInt("R", R, read, key, val))
return false;
if(llarp_buffer_eq(key, "S"))
{
read = true;
hasS = true;
if(!bencode_read_integer(val, &S))
return false;
}
if(!BEncodeMaybeReadDictInt("T", txID, read, key, val))
return false;
if(!BEncodeMaybeReadDictInt("V", version, read, key, val))
return false;
return read;
}
bool
PublishIntroMessage::HandleMessage(llarp_dht_context *ctx,
std::vector< IMessage * > &replies) const
{
2018-07-18 22:50:05 +00:00
if(S > 5)
{
llarp::LogWarn("invalid S value ", S, " > 5");
return false;
}
auto &dht = ctx->impl;
if(!I.VerifySignature(&dht.router->crypto))
{
llarp::LogWarn("invalid introset signature, ", I);
return false;
}
if(I.W && !I.W->IsValid(dht.router->crypto.shorthash))
{
llarp::LogWarn("proof of work not good enough for IntroSet");
return false;
}
llarp::dht::Key_t addr;
if(!I.A.CalculateAddress(addr))
{
llarp::LogWarn(
"failed to calculate hidden service address for PubIntro message");
return false;
}
dht.services->PutNode(I);
2018-07-18 03:10:21 +00:00
replies.push_back(new GotIntroMessage({I}, txID));
Key_t peer;
2018-07-20 04:50:28 +00:00
std::set< Key_t > exclude;
for(const auto &e : E)
exclude.insert(e);
2018-07-19 04:58:39 +00:00
exclude.insert(From);
2018-07-20 04:50:28 +00:00
exclude.insert(dht.OurKey());
2018-07-18 22:50:05 +00:00
if(S && dht.nodes->FindCloseExcluding(addr, peer, exclude))
{
2018-07-19 04:58:39 +00:00
dht.PropagateIntroSetTo(From, txID, I, peer, S - 1);
}
return true;
}
bool
PublishIntroMessage::BEncode(llarp_buffer_t *buf) const
{
if(!bencode_start_dict(buf))
return false;
if(!BEncodeWriteDictMsgType(buf, "A", "I"))
return false;
2018-07-19 04:58:39 +00:00
if(!BEncodeWriteDictList("E", E, buf))
return false;
if(!BEncodeWriteDictEntry("I", I, buf))
return false;
if(!BEncodeWriteDictInt("R", R, buf))
return false;
if(!BEncodeWriteDictInt("S", S, buf))
2018-07-18 22:50:05 +00:00
return false;
if(!BEncodeWriteDictInt("T", txID, buf))
return false;
if(!BEncodeWriteDictInt("V", LLARP_PROTO_VERSION, buf))
return false;
return bencode_end(buf);
}
} // namespace dht
} // namespace llarp