lokinet/llarp/service/IntroSet.cpp

187 lines
4.3 KiB
C++
Raw Normal View History

2018-12-15 16:21:52 +00:00
#include <service/IntroSet.hpp>
2019-01-11 01:19:36 +00:00
#include <path/path.hpp>
namespace llarp
{
namespace service
{
IntroSet::~IntroSet()
{
if(W)
delete W;
}
2019-02-08 19:43:25 +00:00
void
IntroSet::ExtractStatus(util::StatusObject& obj) const
{
2019-02-08 21:29:56 +00:00
std::vector< util::StatusObject > introsObjs(I.size());
2019-02-08 19:43:25 +00:00
{
2019-02-08 21:29:56 +00:00
size_t idx = 0;
for(const auto& intro : I)
{
util::StatusObject& introObj = introsObjs[idx++];
intro.ExtractStatus(introObj);
}
2019-02-08 19:43:25 +00:00
}
obj.PutObjectArray("intros", introsObjs);
if(!topic.IsZero())
obj.PutString("topic", topic.ToString());
obj.PutInt("published", T);
}
bool
2019-02-02 23:12:42 +00:00
IntroSet::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
{
bool read = false;
if(!BEncodeMaybeReadDictEntry("a", A, read, key, buf))
return false;
if(llarp_buffer_eq(key, "i"))
{
return BEncodeReadList(I, buf);
}
if(!BEncodeMaybeReadDictEntry("k", K, read, key, buf))
return false;
if(!BEncodeMaybeReadDictEntry("n", topic, read, key, buf))
return false;
if(!BEncodeMaybeReadDictInt("t", T, read, key, buf))
return false;
if(llarp_buffer_eq(key, "w"))
{
if(W)
delete W;
W = new PoW();
return W->BDecode(buf);
}
if(!BEncodeMaybeReadDictInt("v", version, read, key, buf))
return false;
if(!BEncodeMaybeReadDictEntry("z", Z, read, key, buf))
return false;
return read;
}
bool
IntroSet::BEncode(llarp_buffer_t* buf) const
{
if(!bencode_start_dict(buf))
return false;
if(!BEncodeWriteDictEntry("a", A, buf))
return false;
// start introduction list
if(!bencode_write_bytestring(buf, "i", 1))
return false;
if(!BEncodeWriteList(I.begin(), I.end(), buf))
return false;
// end introduction list
// pq pubkey
if(!BEncodeWriteDictEntry("k", K, buf))
return false;
// topic tag
if(topic.ToString().size())
{
if(!BEncodeWriteDictEntry("n", topic, buf))
return false;
}
// Timestamp published
if(!BEncodeWriteDictInt("t", T, buf))
return false;
// write version
if(!BEncodeWriteDictInt("v", version, buf))
return false;
if(W)
{
if(!BEncodeWriteDictEntry("w", *W, buf))
return false;
}
if(!BEncodeWriteDictEntry("z", Z, buf))
return false;
return bencode_end(buf);
}
bool
IntroSet::HasExpiredIntros(llarp_time_t now) const
{
for(const auto& i : I)
if(now >= i.expiresAt)
return true;
return false;
}
bool
IntroSet::IsExpired(llarp_time_t now) const
{
return GetNewestIntroExpiration() < now;
}
bool
IntroSet::Verify(llarp::Crypto* crypto, llarp_time_t now) const
{
2019-02-02 23:12:42 +00:00
std::array< byte_t, MAX_INTROSET_SIZE > tmp;
llarp_buffer_t buf(tmp);
IntroSet copy;
copy = *this;
copy.Z.Zero();
if(!copy.BEncode(&buf))
2019-01-26 12:33:34 +00:00
{
return false;
2019-01-26 12:33:34 +00:00
}
// rewind and resize buffer
buf.sz = buf.cur - buf.base;
buf.cur = buf.base;
if(!A.Verify(crypto, buf, Z))
2019-01-26 12:33:34 +00:00
{
return false;
2019-01-26 12:33:34 +00:00
}
// validate PoW
using namespace std::placeholders;
if(W && !W->IsValid(std::bind(&Crypto::shorthash, crypto, _1, _2), now))
2019-01-26 12:33:34 +00:00
{
return false;
2019-01-26 12:33:34 +00:00
}
// valid timestamps
// add max clock skew
now += MAX_INTROSET_TIME_DELTA;
for(const auto& intro : I)
{
if(intro.expiresAt > now
&& intro.expiresAt - now > DEFAULT_PATH_LIFETIME)
{
if(W && intro.expiresAt - W->extendedLifetime > DEFAULT_PATH_LIFETIME)
return false;
else if(W == nullptr)
{
llarp::LogWarn("intro has too high expire time");
return false;
}
}
}
if(IsExpired(now))
{
llarp::LogWarn("introset expired: ", *this);
return false;
}
return true;
}
llarp_time_t
IntroSet::GetNewestIntroExpiration() const
{
llarp_time_t t = 0;
for(const auto& intro : I)
t = std::max(intro.expiresAt, t);
return t;
}
} // namespace service
} // namespace llarp