2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/crypto/types.hpp>
|
|
|
|
#include <llarp/path/path_types.hpp>
|
|
|
|
#include <llarp/util/bencode.hpp>
|
|
|
|
#include <llarp/util/status.hpp>
|
2018-07-09 17:32:11 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace service
|
|
|
|
{
|
2019-05-24 02:01:36 +00:00
|
|
|
struct Introduction
|
2018-07-09 17:32:11 +00:00
|
|
|
{
|
2021-10-12 21:37:01 +00:00
|
|
|
RouterID router;
|
2019-04-19 15:10:26 +00:00
|
|
|
PathID_t pathID;
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_time_t latency = 0s;
|
2020-02-24 19:40:45 +00:00
|
|
|
llarp_time_t expiresAt = 0s;
|
2022-05-26 15:59:44 +00:00
|
|
|
uint64_t version = llarp::constants::proto_version;
|
2018-07-11 16:11:19 +00:00
|
|
|
|
2019-02-11 17:14:43 +00:00
|
|
|
util::StatusObject
|
2019-04-19 15:10:26 +00:00
|
|
|
ExtractStatus() const;
|
2019-02-08 19:43:25 +00:00
|
|
|
|
2018-09-10 16:36:36 +00:00
|
|
|
bool
|
|
|
|
IsExpired(llarp_time_t now) const
|
|
|
|
{
|
2018-09-11 13:21:35 +00:00
|
|
|
return now >= expiresAt;
|
2018-09-10 16:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-02-24 19:40:45 +00:00
|
|
|
ExpiresSoon(llarp_time_t now, llarp_time_t dlt = 30s) const
|
2018-09-10 16:36:36 +00:00
|
|
|
{
|
2020-02-24 15:26:46 +00:00
|
|
|
return IsExpired(now + dlt);
|
2018-09-10 16:36:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-16 00:41:14 +00:00
|
|
|
std::string
|
|
|
|
ToString() const;
|
2018-07-09 17:32:11 +00:00
|
|
|
|
|
|
|
bool
|
2019-05-24 02:01:36 +00:00
|
|
|
BEncode(llarp_buffer_t* buf) const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
BDecode(llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
return bencode_decode_dict(*this, buf);
|
|
|
|
}
|
2018-07-09 17:32:11 +00:00
|
|
|
|
|
|
|
bool
|
2019-05-24 02:01:36 +00:00
|
|
|
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf);
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
void
|
|
|
|
Clear();
|
|
|
|
|
2018-07-09 17:32:11 +00:00
|
|
|
bool
|
|
|
|
operator<(const Introduction& other) const
|
|
|
|
{
|
2022-10-13 17:19:25 +00:00
|
|
|
return std::tie(expiresAt, pathID, router, version, latency)
|
|
|
|
< std::tie(other.expiresAt, other.pathID, other.router, other.version, other.latency);
|
2018-07-09 17:32:11 +00:00
|
|
|
}
|
2018-09-17 15:32:37 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
operator==(const Introduction& other) const
|
|
|
|
{
|
2018-09-27 17:51:42 +00:00
|
|
|
return pathID == other.pathID && router == other.router;
|
2018-09-17 15:32:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator!=(const Introduction& other) const
|
|
|
|
{
|
2018-10-08 21:23:45 +00:00
|
|
|
return pathID != other.pathID || router != other.router;
|
2018-09-17 15:32:37 +00:00
|
|
|
}
|
2018-07-09 17:32:11 +00:00
|
|
|
};
|
2019-02-24 23:46:37 +00:00
|
|
|
|
2021-06-08 21:54:40 +00:00
|
|
|
/// comparator for introset timestamp
|
2021-06-02 19:52:13 +00:00
|
|
|
struct CompareIntroTimestamp
|
|
|
|
{
|
|
|
|
bool
|
|
|
|
operator()(const Introduction& left, const Introduction& right) const
|
|
|
|
{
|
|
|
|
return left.expiresAt > right.expiresAt;
|
|
|
|
}
|
|
|
|
};
|
2018-07-09 17:32:11 +00:00
|
|
|
} // namespace service
|
|
|
|
} // namespace llarp
|
2021-03-09 18:39:40 +00:00
|
|
|
|
2022-07-16 00:41:14 +00:00
|
|
|
template <>
|
|
|
|
constexpr inline bool llarp::IsToStringFormattable<llarp::service::Introduction> = true;
|
|
|
|
|
2021-03-09 18:39:40 +00:00
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
template <>
|
|
|
|
struct hash<llarp::service::Introduction>
|
|
|
|
{
|
|
|
|
size_t
|
|
|
|
operator()(const llarp::service::Introduction& i) const
|
|
|
|
{
|
|
|
|
return std::hash<llarp::PubKey>{}(i.router) ^ std::hash<llarp::PathID_t>{}(i.pathID);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace std
|