1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/llarp/router_contact.hpp

236 lines
4.7 KiB
C++
Raw Normal View History

#pragma once
#include "llarp/constants/version.hpp"
#include "llarp/crypto/types.hpp"
#include "llarp/net/address_info.hpp"
#include "llarp/net/exit_info.hpp"
#include "llarp/util/aligned.hpp"
#include "llarp/util/bencode.hpp"
#include "llarp/util/status.hpp"
#include "router_version.hpp"
2018-12-12 02:55:30 +01:00
#include "llarp/dns/srv_data.hpp"
2019-01-22 02:14:02 +01:00
#include <functional>
2019-08-13 00:09:44 +02:00
#include <nlohmann/json.hpp>
2018-12-12 02:55:30 +01:00
#include <vector>
#define MAX_RC_SIZE (1024)
#define NICKLEN (32)
namespace oxenc
{
class bt_list_consumer;
} // namespace oxenc
2018-12-12 02:55:30 +01:00
namespace llarp
{
/// NetID
struct NetID final : public AlignedBuffer<8>
{
static NetID&
2019-01-05 01:49:06 +01:00
DefaultValue();
NetID();
explicit NetID(const byte_t* val);
2019-01-05 01:49:06 +01:00
NetID(const NetID& other) = default;
2021-10-15 02:02:11 +02:00
NetID&
operator=(const NetID& other) = default;
2019-01-05 01:49:06 +01:00
bool
operator==(const NetID& other) const;
bool
operator!=(const NetID& other) const
{
return !(*this == other);
}
std::string
ToString() const;
bool
BDecode(llarp_buffer_t* buf);
bool
BEncode(llarp_buffer_t* buf) const;
};
/// RouterContact
2019-05-24 04:01:36 +02:00
struct RouterContact
2018-12-12 02:55:30 +01:00
{
/// for unit tests
2019-08-27 01:29:17 +02:00
static bool BlockBogons;
static llarp_time_t Lifetime;
static llarp_time_t UpdateInterval;
static llarp_time_t StaleInsertionAge;
2019-05-24 04:01:36 +02:00
RouterContact()
2018-12-12 02:55:30 +01:00
{
Clear();
}
// advertised addresses
std::vector<AddressInfo> addrs;
// network identifier
NetID netID;
2018-12-12 02:55:30 +01:00
// public encryption public key
llarp::PubKey enckey;
// public signing public key
llarp::PubKey pubkey;
// signature
llarp::Signature signature;
/// node nickname, yw kee
llarp::AlignedBuffer<NICKLEN> nickname;
2018-12-12 02:55:30 +01:00
2020-02-24 20:40:45 +01:00
llarp_time_t last_updated = 0s;
2022-05-26 17:59:44 +02:00
uint64_t version = llarp::constants::proto_version;
std::optional<RouterVersion> routerVersion;
2020-08-19 21:10:11 +02:00
/// should we serialize the exit info?
const static bool serializeExit = true;
2018-12-12 02:55:30 +01:00
std::string signed_bt_dict;
std::vector<dns::SRVData> srvRecords;
2019-02-11 18:14:43 +01:00
util::StatusObject
2019-04-19 17:10:26 +02:00
ExtractStatus() const;
2019-02-08 20:43:25 +01:00
2019-08-13 00:09:44 +02:00
nlohmann::json
ToJson() const
{
return ExtractStatus();
2019-08-13 00:09:44 +02:00
}
std::string
ToString() const;
2018-12-12 02:55:30 +01:00
bool
BEncode(llarp_buffer_t* buf) const;
2018-12-12 02:55:30 +01:00
bool
BEncodeSignedSection(llarp_buffer_t* buf) const;
std::string
ToTXTRecord() const;
bool
operator==(const RouterContact& other) const
{
return addrs == other.addrs && enckey == other.enckey && pubkey == other.pubkey
&& signature == other.signature && nickname == other.nickname
&& last_updated == other.last_updated && netID == other.netID;
}
bool
operator<(const RouterContact& other) const
{
return pubkey < other.pubkey;
}
bool
operator!=(const RouterContact& other) const
2019-02-27 13:55:26 +01:00
{
return !(*this == other);
}
2018-12-12 02:55:30 +01:00
void
Clear();
bool
IsExit() const
{
2020-08-19 21:10:11 +02:00
return false;
2018-12-12 02:55:30 +01:00
}
bool
BDecode(llarp_buffer_t* buf);
2018-12-12 02:55:30 +01:00
bool
DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf);
2018-12-12 02:55:30 +01:00
bool
HasNick() const;
std::string
Nick() const;
bool
IsPublicRouter() const;
void
SetNick(std::string_view nick);
2018-12-12 02:55:30 +01:00
bool
2019-06-04 15:19:45 +02:00
Verify(llarp_time_t now, bool allowExpired = true) const;
2018-12-12 02:55:30 +01:00
bool
Sign(const llarp::SecretKey& secret);
2018-12-12 02:55:30 +01:00
/// does this RC expire soon? default delta is 1 minute
bool
2020-02-24 20:40:45 +01:00
ExpiresSoon(llarp_time_t now, llarp_time_t dlt = 1min) const;
/// returns true if this RC is expired and should be removed
bool
IsExpired(llarp_time_t now) const;
2019-07-15 18:56:09 +02:00
/// returns time in ms until we expire or 0 if we have expired
llarp_time_t
TimeUntilExpires(llarp_time_t now) const;
/// get the age of this RC in ms
llarp_time_t
Age(llarp_time_t now) const;
2018-12-12 02:55:30 +01:00
bool
OtherIsNewer(const RouterContact& other) const
2018-12-12 02:55:30 +01:00
{
return last_updated < other.last_updated;
}
bool
Read(const fs::path& fname);
2018-12-12 02:55:30 +01:00
bool
Write(const fs::path& fname) const;
2018-12-12 02:55:30 +01:00
bool
VerifySignature() const;
/// return true if the netid in this rc is for the network id we are using
bool
FromOurNetwork() const;
private:
bool
DecodeVersion_0(llarp_buffer_t* buf);
bool
DecodeVersion_1(oxenc::bt_list_consumer& btlist);
2018-12-12 02:55:30 +01:00
};
2019-01-22 02:14:02 +01:00
template <>
constexpr inline bool IsToStringFormattable<NetID> = true;
template <>
constexpr inline bool IsToStringFormattable<RouterContact> = true;
2019-02-25 00:46:37 +01:00
using RouterLookupHandler = std::function<void(const std::vector<RouterContact>&)>;
2018-12-12 02:55:30 +01:00
} // namespace llarp
namespace std
{
template <>
struct hash<llarp::RouterContact>
{
size_t
operator()(const llarp::RouterContact& r) const
{
return std::hash<llarp::PubKey>{}(r.pubkey);
}
};
} // namespace std