lokinet/llarp/service/IntroSet.hpp

170 lines
3.9 KiB
C++
Raw Normal View History

2018-07-09 17:32:11 +00:00
#ifndef LLARP_SERVICE_INTROSET_HPP
#define LLARP_SERVICE_INTROSET_HPP
#include <crypto/types.hpp>
#include <pow.hpp>
2018-12-12 02:15:08 +00:00
#include <service/Info.hpp>
#include <service/Intro.hpp>
#include <service/tag.hpp>
#include <util/bencode.hpp>
#include <util/time.hpp>
2019-02-08 19:43:25 +00:00
#include <util/status.hpp>
2018-07-09 17:32:11 +00:00
#include <algorithm>
2019-01-22 01:14:02 +00:00
#include <functional>
2018-12-12 02:15:08 +00:00
#include <iostream>
#include <vector>
2018-07-09 17:32:11 +00:00
namespace llarp
{
struct Crypto;
2018-07-09 17:32:11 +00:00
namespace service
{
constexpr std::size_t MAX_INTROSET_SIZE = 4096;
// 10 seconds clock skew permitted for introset expiration
constexpr llarp_time_t MAX_INTROSET_TIME_DELTA = (10 * 1000);
2019-02-08 19:43:25 +00:00
struct IntroSet final : public llarp::IBEncodeMessage,
public util::IStateful
2018-07-09 17:32:11 +00:00
{
2019-02-11 17:14:43 +00:00
util::StatusObject
ExtractStatus() const override;
2019-02-08 19:43:25 +00:00
2018-07-09 17:32:11 +00:00
ServiceInfo A;
2018-07-20 04:50:28 +00:00
std::vector< Introduction > I;
PQPubKey K;
2018-07-17 07:30:03 +00:00
Tag topic;
llarp_time_t T = 0;
llarp::PoW* W = nullptr;
2018-07-09 17:32:11 +00:00
llarp::Signature Z;
2018-08-10 21:34:11 +00:00
IntroSet() = default;
IntroSet(IntroSet&& other) : IBEncodeMessage(other.version)
2018-08-10 21:34:11 +00:00
{
A = std::move(other.A);
I = std::move(other.I);
K = std::move(other.K);
T = std::move(other.T);
2018-08-10 21:34:11 +00:00
version = std::move(other.version);
topic = std::move(other.topic);
W = std::move(other.W);
Z = std::move(other.Z);
}
IntroSet(const IntroSet& other) : IBEncodeMessage(other.version)
2018-08-10 21:34:11 +00:00
{
A = other.A;
I = other.I;
K = other.K;
T = other.T;
2018-08-10 21:34:11 +00:00
version = other.version;
topic = other.topic;
if(other.W)
W = new llarp::PoW(*other.W);
Z = other.Z;
}
2018-07-09 17:32:11 +00:00
~IntroSet();
IntroSet&
operator=(const IntroSet& other)
{
2018-10-01 17:16:15 +00:00
I.clear();
2018-07-09 17:32:11 +00:00
A = other.A;
I = other.I;
K = other.K;
T = other.T;
2018-07-09 17:32:11 +00:00
version = other.version;
topic = other.topic;
2018-07-09 17:32:11 +00:00
if(W)
{
2018-07-09 17:32:11 +00:00
delete W;
W = nullptr;
}
2018-08-10 03:51:38 +00:00
if(other.W)
W = new llarp::PoW(*other.W);
2018-07-09 17:32:11 +00:00
Z = other.Z;
return *this;
}
2018-07-18 03:10:21 +00:00
bool
operator<(const IntroSet& other) const
{
2018-07-19 04:58:39 +00:00
return A < other.A;
2018-07-18 03:10:21 +00:00
}
bool
operator==(const IntroSet& other) const
{
return A == other.A && I == other.I && K == other.K && T == other.T
&& version == other.version && topic == other.topic && W == other.W
&& Z == other.Z;
}
bool
operator!=(const IntroSet& other) const
{
return !(*this == other);
}
2018-09-10 17:37:28 +00:00
bool
OtherIsNewer(const IntroSet& other) const
{
return T < other.T;
2018-09-10 17:37:28 +00:00
}
2018-07-09 17:32:11 +00:00
friend std::ostream&
operator<<(std::ostream& out, const IntroSet& i)
{
out << "A=[" << i.A << "] I=[";
for(const auto& intro : i.I)
{
out << intro << ", ";
2018-07-09 17:32:11 +00:00
}
2018-07-17 07:30:03 +00:00
out << "]";
out << "K=" << i.K;
2018-07-17 07:30:03 +00:00
auto topic = i.topic.ToString();
if(topic.size())
{
out << " topic=" << topic;
}
else
{
out << " topic=" << i.topic;
}
out << " T=" << i.T;
if(i.W)
{
out << " W=" << *i.W;
}
2018-07-17 07:30:03 +00:00
return out << " V=" << i.version << " Z=" << i.Z;
2018-07-09 17:32:11 +00:00
}
llarp_time_t
GetNewestIntroExpiration() const;
2018-08-10 21:34:11 +00:00
2018-07-17 06:17:13 +00:00
bool
2018-07-18 22:50:05 +00:00
HasExpiredIntros(llarp_time_t now) const;
2018-07-17 06:17:13 +00:00
2018-07-18 00:25:24 +00:00
bool
IsExpired(llarp_time_t now) const;
2018-07-09 17:32:11 +00:00
bool
BEncode(llarp_buffer_t* buf) const override;
2018-07-09 17:32:11 +00:00
bool
2019-02-05 00:41:33 +00:00
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override;
2018-07-09 17:32:11 +00:00
bool
Verify(llarp::Crypto* crypto, llarp_time_t now) const;
2018-07-09 17:32:11 +00:00
};
2019-01-22 01:14:02 +00:00
using IntroSetLookupHandler =
std::function< void(const std::vector< IntroSet >&) >;
2018-07-09 17:32:11 +00:00
} // namespace service
} // namespace llarp
#endif