2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
#include "router_id.hpp"
|
2023-10-24 13:18:03 +00:00
|
|
|
#include "router_version.hpp"
|
|
|
|
|
|
|
|
#include <llarp/constants/version.hpp>
|
|
|
|
#include <llarp/crypto/types.hpp>
|
|
|
|
#include <llarp/dns/srv_data.hpp>
|
|
|
|
#include <llarp/util/aligned.hpp>
|
|
|
|
#include <llarp/util/bencode.hpp>
|
|
|
|
#include <llarp/util/status.hpp>
|
2023-11-28 12:55:01 +00:00
|
|
|
#include <llarp/util/time.hpp>
|
2023-10-24 13:18:03 +00:00
|
|
|
|
2023-10-19 21:59:57 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <oxenc/bt_producer.h>
|
|
|
|
#include <quic.hpp>
|
|
|
|
|
2023-10-19 21:59:57 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-12-12 01:55:30 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2023-10-31 20:49:01 +00:00
|
|
|
static auto logcat = log::Cat("RC");
|
|
|
|
|
2023-11-02 12:30:38 +00:00
|
|
|
static inline constexpr size_t NETID_SIZE{8};
|
|
|
|
|
2023-11-02 12:39:20 +00:00
|
|
|
/// On the wire we encode the data as a dict containing:
|
|
|
|
/// "" -- the RC format version, which must be == RouterContact::Version for us to attempt to
|
|
|
|
/// parse the reset of the fields. (Future versions might have backwards-compat support
|
|
|
|
/// for lower versions).
|
|
|
|
/// "4" -- 6 byte packed IPv4 address & port: 4 bytes of IPv4 address followed by 2 bytes of
|
|
|
|
/// port, both encoded in network (i.e. big-endian) order.
|
|
|
|
/// "6" -- optional 18 byte IPv6 address & port: 16 byte raw IPv6 address followed by 2 bytes
|
|
|
|
/// of port in network order.
|
|
|
|
/// "i" -- optional network ID string of up to 8 bytes; this is omitted for the default network
|
2023-12-04 18:26:58 +00:00
|
|
|
/// ID ("lokinet") but included for others (such as "testnet" for testnet).
|
2023-11-02 12:39:20 +00:00
|
|
|
/// "p" -- 32-byte router pubkey
|
|
|
|
/// "t" -- timestamp when this RC record was created (which also implicitly determines when it
|
|
|
|
/// goes stale and when it expires).
|
|
|
|
/// "v" -- lokinet version of the router; this is a three-byte packed value of
|
|
|
|
/// MAJOR, MINOR, PATCH, e.g. \x00\x0a\x03 for 0.10.3.
|
|
|
|
/// "~" -- signature of all of the previous serialized data, signed by "p"
|
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
/// RouterContact
|
|
|
|
struct RouterContact
|
2018-12-19 16:17:41 +00:00
|
|
|
{
|
2023-10-31 20:49:01 +00:00
|
|
|
static constexpr uint8_t RC_VERSION = 0;
|
2018-12-21 13:08:01 +00:00
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
/// Unit tests disable this to allow private IP ranges in RCs, which normally get rejected.
|
|
|
|
static inline bool BLOCK_BOGONS = true;
|
2018-12-19 16:17:41 +00:00
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
static inline std::string ACTIVE_NETID{LOKINET_DEFAULT_NETID};
|
2018-12-19 16:17:41 +00:00
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
static inline constexpr size_t MAX_RC_SIZE = 1024;
|
2018-12-19 16:17:41 +00:00
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
/// Timespans for RCs:
|
2018-12-19 16:17:41 +00:00
|
|
|
|
2023-11-17 06:26:59 +00:00
|
|
|
/// How long (from its signing time) before an RC is considered "stale". Relays republish
|
|
|
|
/// their RCs slightly more frequently than this so that ideally this won't happen.
|
|
|
|
static constexpr auto STALE_AGE = 6h;
|
|
|
|
|
|
|
|
/// How long (from its signing time) before an RC becomes "outdated". Outdated records are used
|
|
|
|
/// (e.g. for path building) only if there are no newer records available, such as might be
|
2023-10-31 20:49:01 +00:00
|
|
|
/// the case when a client has been turned off for a while.
|
2023-11-17 06:26:59 +00:00
|
|
|
static constexpr auto OUTDATED_AGE = 12h;
|
2018-12-19 16:17:41 +00:00
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
/// How long before an RC becomes invalid (and thus deleted).
|
|
|
|
static constexpr auto LIFETIME = 30 * 24h;
|
|
|
|
|
2023-11-02 12:39:20 +00:00
|
|
|
ustring_view
|
|
|
|
view() const
|
|
|
|
{
|
|
|
|
return _payload;
|
|
|
|
}
|
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
/// Getters for private attributes
|
|
|
|
const oxen::quic::Address&
|
|
|
|
addr() const
|
|
|
|
{
|
|
|
|
return _addr;
|
|
|
|
}
|
2018-12-19 16:17:41 +00:00
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
const std::optional<oxen::quic::Address>&
|
|
|
|
addr6() const
|
2018-12-12 01:55:30 +00:00
|
|
|
{
|
2023-10-31 20:49:01 +00:00
|
|
|
return _addr6;
|
2018-12-12 01:55:30 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
const RouterID&
|
|
|
|
router_id() const
|
|
|
|
{
|
|
|
|
return _router_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
const rc_time&
|
|
|
|
timestamp() const
|
|
|
|
{
|
|
|
|
return _timestamp;
|
|
|
|
}
|
2023-10-03 20:00:23 +00:00
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
protected:
|
2018-12-12 01:55:30 +00:00
|
|
|
// advertised addresses
|
2023-10-31 20:49:01 +00:00
|
|
|
oxen::quic::Address _addr; // refactor all 15 uses to use addr() method
|
|
|
|
std::optional<oxen::quic::Address> _addr6; // optional ipv6
|
2018-12-12 01:55:30 +00:00
|
|
|
// public signing public key
|
2023-10-31 20:49:01 +00:00
|
|
|
RouterID _router_id; // refactor all 103 uses to use router_id() method
|
|
|
|
|
|
|
|
rc_time _timestamp{};
|
2018-12-12 01:55:30 +00:00
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
// Lokinet version at the time the RC was produced
|
|
|
|
std::array<uint8_t, 3> _router_version;
|
|
|
|
|
2023-11-02 12:30:38 +00:00
|
|
|
// In both Remote and Local RC's, the entire bt-encoded payload given at construction is
|
|
|
|
// emplaced here.
|
|
|
|
//
|
|
|
|
// In a RemoteRC, this value will be held for the lifetime of the object
|
|
|
|
// s.t. it can be returned upon calls to ::bt_encode.
|
|
|
|
// In a LocalRC, this value will be supplanted any time a mutator is invoked, requiring
|
|
|
|
// the re-signing of the payload.
|
|
|
|
ustring _payload;
|
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
public:
|
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
|
|
|
|
2019-02-11 17:14:43 +00:00
|
|
|
util::StatusObject
|
2023-10-31 20:49:01 +00:00
|
|
|
extract_status() const;
|
2019-02-08 19:43:25 +00:00
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
nlohmann::json
|
|
|
|
to_json() const
|
2023-10-12 18:46:31 +00:00
|
|
|
{
|
2023-10-31 20:49:01 +00:00
|
|
|
return extract_status();
|
2023-10-12 18:46:31 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
virtual std::string
|
|
|
|
to_string() const
|
2019-08-12 22:09:44 +00:00
|
|
|
{
|
2023-10-31 20:49:01 +00:00
|
|
|
return fmt::format(
|
2023-11-02 12:30:38 +00:00
|
|
|
"[RC k={} updated={} v={} addr={}]",
|
|
|
|
_router_id.ToView(),
|
|
|
|
_timestamp.time_since_epoch().count(),
|
|
|
|
RC_VERSION,
|
|
|
|
_addr.to_string());
|
2019-08-12 22:09:44 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 12:30:38 +00:00
|
|
|
bool
|
|
|
|
write(const fs::path& fname) const;
|
|
|
|
|
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
|
|
|
{
|
2023-10-31 20:49:01 +00:00
|
|
|
return _router_id == other._router_id and _addr == other._addr and _addr6 == other._addr6
|
|
|
|
and _timestamp == other._timestamp and _router_version == other._router_version;
|
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
|
|
|
{
|
2023-10-31 20:49:01 +00:00
|
|
|
return _router_id < other._router_id;
|
2019-03-11 13:58:31 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
virtual void
|
2023-11-02 12:30:38 +00:00
|
|
|
clear()
|
2023-10-31 20:49:01 +00:00
|
|
|
{}
|
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
|
2023-08-31 16:28:02 +00:00
|
|
|
decode_key(const llarp_buffer_t& k, llarp_buffer_t* buf);
|
2018-12-12 01:55:30 +00:00
|
|
|
|
|
|
|
bool
|
2023-12-06 21:54:51 +00:00
|
|
|
is_public_addressable() const;
|
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
|
2023-10-31 20:49:01 +00:00
|
|
|
expires_within_delta(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
|
2023-10-31 20:49:01 +00:00
|
|
|
is_expired(llarp_time_t now) const;
|
2018-12-19 16:17:41 +00:00
|
|
|
|
2019-07-15 16:56:09 +00:00
|
|
|
/// returns time in ms until we expire or 0 if we have expired
|
|
|
|
llarp_time_t
|
2023-10-31 20:49:01 +00:00
|
|
|
time_to_expiry(llarp_time_t now) const;
|
2019-07-15 16:56:09 +00:00
|
|
|
|
|
|
|
/// get the age of this RC in ms
|
|
|
|
llarp_time_t
|
2023-10-31 20:49:01 +00:00
|
|
|
age(llarp_time_t now) const;
|
2019-07-15 16:56:09 +00:00
|
|
|
|
2018-12-12 01:55:30 +00:00
|
|
|
bool
|
2023-10-31 20:49:01 +00:00
|
|
|
other_is_newer(const RouterContact& other) const
|
2018-12-12 01:55:30 +00:00
|
|
|
{
|
2023-10-31 20:49:01 +00:00
|
|
|
return _timestamp < other._timestamp;
|
2018-12-12 01:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2023-10-31 20:49:01 +00:00
|
|
|
is_obsolete_bootstrap() const;
|
2020-09-25 18:05:28 +00:00
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
void
|
|
|
|
bt_load(oxenc::bt_dict_consumer& data);
|
|
|
|
};
|
2022-09-27 17:00:27 +00:00
|
|
|
|
2023-12-11 15:08:02 +00:00
|
|
|
struct RemoteRC;
|
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
/// Extension of RouterContact used to store a local "RC," and inserts a RouterContact by
|
|
|
|
/// re-parsing and sending it out. This sub-class contains a pubkey and all the other attributes
|
|
|
|
/// required for signing and serialization
|
|
|
|
///
|
|
|
|
/// Note: this class may be entirely superfluous, so it is used here as a placeholder until its
|
|
|
|
/// marginal utility is determined. It may end up as a free-floating method that reads in
|
|
|
|
/// parameters and outputs a bt-serialized string
|
2023-11-02 12:30:38 +00:00
|
|
|
struct LocalRC final : public RouterContact
|
2023-10-31 20:49:01 +00:00
|
|
|
{
|
2023-11-02 12:39:20 +00:00
|
|
|
static LocalRC
|
|
|
|
make(const SecretKey secret, oxen::quic::Address local);
|
|
|
|
|
2020-09-28 15:15:07 +00:00
|
|
|
private:
|
2023-10-31 20:49:01 +00:00
|
|
|
ustring _signature;
|
2023-11-02 12:39:20 +00:00
|
|
|
SecretKey _secret_key;
|
2023-10-31 20:49:01 +00:00
|
|
|
|
|
|
|
void
|
2023-11-02 12:30:38 +00:00
|
|
|
bt_sign(oxenc::bt_dict_producer& btdp);
|
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
void
|
2023-11-02 12:39:20 +00:00
|
|
|
bt_encode(oxenc::bt_dict_producer& btdp);
|
|
|
|
|
|
|
|
LocalRC(const SecretKey secret, oxen::quic::Address local);
|
2023-10-31 20:49:01 +00:00
|
|
|
|
|
|
|
public:
|
2023-11-02 12:30:38 +00:00
|
|
|
LocalRC() = default;
|
|
|
|
explicit LocalRC(std::string payload, const SecretKey sk);
|
|
|
|
~LocalRC() = default;
|
2023-10-31 20:49:01 +00:00
|
|
|
|
2023-12-11 15:08:02 +00:00
|
|
|
RemoteRC
|
|
|
|
to_remote();
|
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
void
|
2023-11-02 12:30:38 +00:00
|
|
|
resign();
|
|
|
|
|
2023-10-31 20:49:01 +00:00
|
|
|
void
|
|
|
|
clear() override
|
|
|
|
{
|
2023-11-17 16:53:10 +00:00
|
|
|
_addr = oxen::quic::Address{};
|
2023-10-31 20:49:01 +00:00
|
|
|
_addr6.reset();
|
|
|
|
_router_id.Zero();
|
|
|
|
_timestamp = {};
|
|
|
|
_router_version.fill(0);
|
|
|
|
_signature.clear();
|
|
|
|
}
|
2020-09-25 18:05:28 +00:00
|
|
|
|
|
|
|
bool
|
2023-10-31 20:49:01 +00:00
|
|
|
operator==(const LocalRC& other) const
|
|
|
|
{
|
|
|
|
return _router_id == other._router_id and _addr == other._addr and _addr6 == other._addr6
|
|
|
|
and _timestamp == other._timestamp and _router_version == other._router_version
|
|
|
|
and _signature == other._signature;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mutators for the private member attributes. Calling on the mutators
|
|
|
|
/// will clear the current signature and re-sign the RC
|
|
|
|
void
|
|
|
|
set_addr(oxen::quic::Address new_addr)
|
|
|
|
{
|
|
|
|
_addr = std::move(new_addr);
|
2023-11-02 12:30:38 +00:00
|
|
|
resign();
|
2023-10-31 20:49:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
set_addr6(oxen::quic::Address new_addr)
|
|
|
|
{
|
|
|
|
_addr6 = std::move(new_addr);
|
2023-11-02 12:30:38 +00:00
|
|
|
resign();
|
2023-10-31 20:49:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
set_router_id(RouterID rid)
|
|
|
|
{
|
|
|
|
_router_id = std::move(rid);
|
2023-11-02 12:30:38 +00:00
|
|
|
resign();
|
2023-10-31 20:49:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
set_timestamp(llarp_time_t ts)
|
|
|
|
{
|
|
|
|
set_timestamp(rc_time{std::chrono::duration_cast<std::chrono::seconds>(ts)});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
set_timestamp(rc_time ts)
|
|
|
|
{
|
|
|
|
_timestamp = ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets RC timestamp to current system clock time
|
|
|
|
void
|
|
|
|
set_systime_timestamp()
|
|
|
|
{
|
2023-11-28 12:55:01 +00:00
|
|
|
set_timestamp(time_point_now());
|
2023-10-31 20:49:01 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Extension of RouterContact used in a "read-only" fashion. Parses the incoming RC to query
|
|
|
|
/// the data in the constructor, eliminating the need for a ::verify method/
|
2023-11-02 12:30:38 +00:00
|
|
|
struct RemoteRC final : public RouterContact
|
2023-10-31 20:49:01 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
void
|
2023-11-02 12:30:38 +00:00
|
|
|
bt_verify(oxenc::bt_dict_consumer& data, bool reject_expired = false) const;
|
2023-10-31 20:49:01 +00:00
|
|
|
|
2023-11-02 12:30:38 +00:00
|
|
|
public:
|
|
|
|
RemoteRC() = default;
|
|
|
|
RemoteRC(std::string_view data) : RemoteRC{oxenc::bt_dict_consumer{data}}
|
2023-12-07 22:39:39 +00:00
|
|
|
{
|
|
|
|
_payload = {reinterpret_cast<const unsigned char*>(data.data()), data.size()};
|
|
|
|
}
|
2023-11-02 12:30:38 +00:00
|
|
|
RemoteRC(ustring_view data) : RemoteRC{oxenc::bt_dict_consumer{data}}
|
|
|
|
{
|
|
|
|
_payload = data;
|
|
|
|
}
|
|
|
|
explicit RemoteRC(oxenc::bt_dict_consumer btdc);
|
|
|
|
~RemoteRC() = default;
|
|
|
|
|
|
|
|
std::string_view
|
|
|
|
view() const
|
|
|
|
{
|
|
|
|
return {reinterpret_cast<const char*>(_payload.data()), _payload.size()};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
verify() const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
read(const fs::path& fname);
|
2023-10-31 20:49:01 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
clear() override
|
|
|
|
{
|
2023-11-17 16:53:10 +00:00
|
|
|
_addr = oxen::quic::Address{};
|
2023-10-31 20:49:01 +00:00
|
|
|
_addr6.reset();
|
|
|
|
_router_id.Zero();
|
|
|
|
_timestamp = {};
|
|
|
|
_router_version.fill(0);
|
|
|
|
}
|
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<RouterContact> = true;
|
2023-10-31 20:49:01 +00:00
|
|
|
template <>
|
|
|
|
constexpr inline bool IsToStringFormattable<RemoteRC> = true;
|
|
|
|
template <>
|
|
|
|
constexpr inline bool IsToStringFormattable<LocalRC> = true;
|
2019-02-24 23:46:37 +00:00
|
|
|
|
2023-11-02 12:30:38 +00:00
|
|
|
using RouterLookupHandler = std::function<void(const std::vector<RemoteRC>&)>;
|
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>
|
|
|
|
{
|
2023-12-01 17:19:07 +00:00
|
|
|
virtual size_t
|
2021-03-09 18:39:40 +00:00
|
|
|
operator()(const llarp::RouterContact& r) const
|
|
|
|
{
|
2023-10-31 20:49:01 +00:00
|
|
|
return std::hash<llarp::PubKey>{}(r.router_id());
|
2021-03-09 18:39:40 +00:00
|
|
|
}
|
|
|
|
};
|
2023-12-01 17:19:07 +00:00
|
|
|
|
|
|
|
template <>
|
2023-12-05 20:04:24 +00:00
|
|
|
struct hash<llarp::RemoteRC> : public hash<llarp::RouterContact>
|
|
|
|
{};
|
2023-12-01 17:19:07 +00:00
|
|
|
|
|
|
|
template <>
|
|
|
|
struct hash<llarp::LocalRC> final : public hash<llarp::RouterContact>
|
2023-12-05 20:04:24 +00:00
|
|
|
{};
|
2021-03-09 18:39:40 +00:00
|
|
|
} // namespace std
|