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>
|
2023-10-24 13:18:03 +00:00
|
|
|
|
|
|
|
#include <oxenc/bt.h>
|
|
|
|
|
2018-07-09 17:32:11 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
static auto intro_cat = llarp::log::Cat("lokinet.intro");
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace llarp::service
|
2018-07-09 17:32:11 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
struct Introduction
|
2018-07-09 17:32:11 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
RouterID router;
|
|
|
|
PathID_t path_id;
|
|
|
|
llarp_time_t latency = 0s;
|
|
|
|
llarp_time_t expiry = 0s;
|
|
|
|
uint64_t version = llarp::constants::proto_version;
|
|
|
|
|
2023-10-04 13:25:25 +00:00
|
|
|
Introduction() = default;
|
2023-10-03 20:00:23 +00:00
|
|
|
Introduction(std::string buf);
|
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
util::StatusObject
|
|
|
|
ExtractStatus() const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsExpired(llarp_time_t now) const
|
|
|
|
{
|
|
|
|
return now >= expiry;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ExpiresSoon(llarp_time_t now, llarp_time_t dlt = 30s) const
|
|
|
|
{
|
|
|
|
return IsExpired(now + dlt);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ToString() const;
|
|
|
|
|
|
|
|
void
|
2023-10-03 20:00:23 +00:00
|
|
|
bt_encode(oxenc::bt_list_producer& btlp) const;
|
2023-10-18 12:48:09 +00:00
|
|
|
void
|
|
|
|
bt_encode(oxenc::bt_dict_producer& subdict) const;
|
2023-08-31 16:28:02 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
BDecode(llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
return bencode_decode_dict(*this, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
decode_key(const llarp_buffer_t& key, llarp_buffer_t* buf);
|
|
|
|
|
|
|
|
void
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator<(const Introduction& other) const
|
|
|
|
{
|
|
|
|
return std::tie(expiry, path_id, router, version, latency)
|
|
|
|
< std::tie(other.expiry, other.path_id, other.router, other.version, other.latency);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator==(const Introduction& other) const
|
2018-07-09 17:32:11 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
return path_id == other.path_id && router == other.router;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator!=(const Introduction& other) const
|
2021-06-02 19:52:13 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
return path_id != other.path_id || router != other.router;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// comparator for introset timestamp
|
|
|
|
struct CompareIntroTimestamp
|
|
|
|
{
|
|
|
|
bool
|
|
|
|
operator()(const Introduction& left, const Introduction& right) const
|
|
|
|
{
|
|
|
|
return left.expiry > right.expiry;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace llarp::service
|
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
|
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
return std::hash<llarp::PubKey>{}(i.router) ^ std::hash<llarp::PathID_t>{}(i.path_id);
|
2021-03-09 18:39:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace std
|