2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <llarp/crypto/types.hpp>
|
|
|
|
#include <llarp/pow.hpp>
|
|
|
|
#include "info.hpp"
|
|
|
|
#include "intro.hpp"
|
|
|
|
#include "tag.hpp"
|
2021-04-14 15:07:06 +00:00
|
|
|
#include "protocol_type.hpp"
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/util/bencode.hpp>
|
|
|
|
#include <llarp/util/time.hpp>
|
|
|
|
#include <llarp/util/status.hpp>
|
|
|
|
#include <llarp/dns/srv_data.hpp>
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2021-04-14 15:07:06 +00:00
|
|
|
#include <llarp/net/ip_range.hpp>
|
|
|
|
#include <llarp/net/traffic_policy.hpp>
|
|
|
|
|
2020-05-01 19:51:15 +00:00
|
|
|
#include <optional>
|
2018-09-29 08:16:54 +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
|
|
|
|
{
|
|
|
|
namespace service
|
|
|
|
{
|
2018-08-13 23:22:31 +00:00
|
|
|
constexpr std::size_t MAX_INTROSET_SIZE = 4096;
|
2018-09-21 15:24:00 +00:00
|
|
|
// 10 seconds clock skew permitted for introset expiration
|
2020-02-24 19:40:45 +00:00
|
|
|
constexpr llarp_time_t MAX_INTROSET_TIME_DELTA = 10s;
|
2020-01-27 21:30:41 +00:00
|
|
|
|
2019-05-24 02:01:36 +00:00
|
|
|
struct IntroSet
|
2018-07-09 17:32:11 +00:00
|
|
|
{
|
2021-04-14 15:07:06 +00:00
|
|
|
ServiceInfo addressKeys;
|
|
|
|
std::vector<Introduction> intros;
|
|
|
|
PQPubKey sntrupKey;
|
2018-07-17 07:30:03 +00:00
|
|
|
Tag topic;
|
2020-08-31 20:07:17 +00:00
|
|
|
std::vector<llarp::dns::SRVTuple> SRVs;
|
2021-04-14 15:07:06 +00:00
|
|
|
llarp_time_t timestampSignedAt = 0s;
|
|
|
|
|
|
|
|
/// ethertypes we advertise that we speak
|
|
|
|
std::vector<ProtocolType> supportedProtocols;
|
|
|
|
/// aonnuce that these ranges are reachable via our endpoint
|
|
|
|
/// only set when we support exit traffic ethertype is supported
|
2021-04-14 19:40:57 +00:00
|
|
|
std::set<IPRange> ownedRanges;
|
2021-04-14 15:07:06 +00:00
|
|
|
|
|
|
|
/// policies about traffic that we are willing to carry
|
|
|
|
/// a protocol/range whitelist or blacklist
|
|
|
|
/// only set when we support exit traffic ethertype
|
|
|
|
std::optional<net::TrafficPolicy> exitTrafficPolicy;
|
|
|
|
|
|
|
|
Signature signature;
|
2022-05-26 15:59:44 +00:00
|
|
|
uint64_t version = llarp::constants::proto_version;
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2018-09-10 17:37:28 +00:00
|
|
|
bool
|
|
|
|
OtherIsNewer(const IntroSet& other) const
|
|
|
|
{
|
2021-04-14 15:07:06 +00:00
|
|
|
return timestampSignedAt < other.timestampSignedAt;
|
2018-09-10 17:37:28 +00:00
|
|
|
}
|
|
|
|
|
2022-07-16 00:41:14 +00:00
|
|
|
std::string
|
|
|
|
ToString() const;
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2018-09-10 16:36:36 +00:00
|
|
|
llarp_time_t
|
2018-10-11 11:41:10 +00:00
|
|
|
GetNewestIntroExpiration() const;
|
2018-08-10 21:34:11 +00:00
|
|
|
|
2019-05-07 12:31:34 +00:00
|
|
|
bool
|
|
|
|
GetNewestIntro(Introduction& intro) const;
|
|
|
|
|
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
|
|
|
|
2021-06-02 19:52:13 +00:00
|
|
|
/// return true if any of our intros expires soon given a delta
|
|
|
|
bool
|
|
|
|
HasStaleIntros(llarp_time_t now, llarp_time_t delta) const;
|
|
|
|
|
2018-07-18 00:25:24 +00:00
|
|
|
bool
|
|
|
|
IsExpired(llarp_time_t now) const;
|
|
|
|
|
2020-08-31 23:25:58 +00:00
|
|
|
std::vector<llarp::dns::SRVData>
|
|
|
|
GetMatchingSRVRecords(std::string_view service_proto) const;
|
|
|
|
|
2018-07-09 17:32:11 +00:00
|
|
|
bool
|
2019-05-24 02:01:36 +00:00
|
|
|
BEncode(llarp_buffer_t* buf) const;
|
2018-07-09 17:32:11 +00:00
|
|
|
|
|
|
|
bool
|
2019-05-24 02:01:36 +00:00
|
|
|
BDecode(llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
return bencode_decode_dict(*this, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf);
|
2018-07-09 17:32:11 +00:00
|
|
|
|
|
|
|
bool
|
2019-05-28 19:45:08 +00:00
|
|
|
Verify(llarp_time_t now) const;
|
2019-04-22 17:55:04 +00:00
|
|
|
|
|
|
|
util::StatusObject
|
|
|
|
ExtractStatus() const;
|
2018-07-09 17:32:11 +00:00
|
|
|
};
|
2019-01-22 01:14:02 +00:00
|
|
|
|
2019-04-23 09:25:03 +00:00
|
|
|
inline bool
|
|
|
|
operator<(const IntroSet& lhs, const IntroSet& rhs)
|
|
|
|
{
|
2021-04-14 15:07:06 +00:00
|
|
|
return lhs.addressKeys < rhs.addressKeys;
|
2019-04-23 09:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
operator==(const IntroSet& lhs, const IntroSet& rhs)
|
|
|
|
{
|
2021-04-14 15:07:06 +00:00
|
|
|
return std::tie(
|
|
|
|
lhs.addressKeys,
|
|
|
|
lhs.intros,
|
|
|
|
lhs.sntrupKey,
|
|
|
|
lhs.timestampSignedAt,
|
|
|
|
lhs.version,
|
|
|
|
lhs.topic,
|
|
|
|
lhs.signature)
|
|
|
|
== std::tie(
|
|
|
|
rhs.addressKeys,
|
|
|
|
rhs.intros,
|
|
|
|
rhs.sntrupKey,
|
|
|
|
rhs.timestampSignedAt,
|
|
|
|
rhs.version,
|
|
|
|
rhs.topic,
|
|
|
|
rhs.signature);
|
2019-04-23 09:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
operator!=(const IntroSet& lhs, const IntroSet& rhs)
|
|
|
|
{
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2020-01-31 20:45:52 +00:00
|
|
|
/// public version of the introset that is encrypted
|
2020-01-27 21:30:41 +00:00
|
|
|
struct EncryptedIntroSet
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
using Payload_t = std::vector<byte_t>;
|
2020-01-27 21:30:41 +00:00
|
|
|
|
|
|
|
PubKey derivedSigningKey;
|
2020-02-24 19:40:45 +00:00
|
|
|
llarp_time_t signedAt = 0s;
|
2020-01-27 21:30:41 +00:00
|
|
|
Payload_t introsetPayload;
|
|
|
|
TunnelNonce nounce;
|
2020-05-01 19:51:15 +00:00
|
|
|
std::optional<Tag> topic;
|
2020-01-27 21:30:41 +00:00
|
|
|
Signature sig;
|
|
|
|
|
|
|
|
bool
|
2020-01-31 20:45:52 +00:00
|
|
|
Sign(const PrivateKey& k);
|
2020-01-27 21:30:41 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
IsExpired(llarp_time_t now) const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
BEncode(llarp_buffer_t* buf) const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
BDecode(llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
return bencode_decode_dict(*this, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf);
|
|
|
|
|
|
|
|
bool
|
|
|
|
OtherIsNewer(const EncryptedIntroSet& other) const;
|
|
|
|
|
|
|
|
/// verify signature and timestamp
|
|
|
|
bool
|
|
|
|
Verify(llarp_time_t now) const;
|
|
|
|
|
2022-07-16 00:41:14 +00:00
|
|
|
std::string
|
|
|
|
ToString() const;
|
2020-01-27 21:30:41 +00:00
|
|
|
|
|
|
|
util::StatusObject
|
|
|
|
ExtractStatus() const;
|
|
|
|
|
2020-05-01 19:51:15 +00:00
|
|
|
std::optional<IntroSet>
|
2020-01-27 21:30:41 +00:00
|
|
|
MaybeDecrypt(const PubKey& rootKey) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
operator<(const EncryptedIntroSet& lhs, const EncryptedIntroSet& rhs)
|
|
|
|
{
|
|
|
|
return lhs.derivedSigningKey < rhs.derivedSigningKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
operator==(const EncryptedIntroSet& lhs, const EncryptedIntroSet& rhs)
|
|
|
|
{
|
|
|
|
return std::tie(lhs.signedAt, lhs.derivedSigningKey, lhs.nounce, lhs.sig)
|
|
|
|
== std::tie(rhs.signedAt, rhs.derivedSigningKey, rhs.nounce, rhs.sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
operator!=(const EncryptedIntroSet& lhs, const EncryptedIntroSet& rhs)
|
|
|
|
{
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
using EncryptedIntroSetLookupHandler =
|
2020-04-07 18:38:56 +00:00
|
|
|
std::function<void(const std::vector<EncryptedIntroSet>&)>;
|
|
|
|
using IntroSetLookupHandler = std::function<void(const std::vector<IntroSet>&)>;
|
2019-01-22 01:14:02 +00:00
|
|
|
|
2018-07-09 17:32:11 +00:00
|
|
|
} // namespace service
|
|
|
|
} // namespace llarp
|
2022-07-16 00:41:14 +00:00
|
|
|
|
|
|
|
template <>
|
|
|
|
constexpr inline bool llarp::IsToStringFormattable<llarp::service::IntroSet> = true;
|
|
|
|
template <>
|
|
|
|
constexpr inline bool llarp::IsToStringFormattable<llarp::service::EncryptedIntroSet> = true;
|