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