2018-12-12 01:55:30 +00:00
|
|
|
#ifndef LLARP_RC_HPP
|
|
|
|
#define LLARP_RC_HPP
|
|
|
|
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <constants/version.hpp>
|
2019-01-13 16:30:07 +00:00
|
|
|
#include <crypto/types.hpp>
|
2019-01-14 21:46:07 +00:00
|
|
|
#include <net/address_info.hpp>
|
|
|
|
#include <net/exit_info.hpp>
|
2019-01-13 14:00:50 +00:00
|
|
|
#include <util/aligned.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/bencode.hpp>
|
2019-02-08 19:43:25 +00:00
|
|
|
#include <util/status.hpp>
|
2020-01-25 16:28:07 +00:00
|
|
|
#include <router_version.hpp>
|
2018-12-12 01:55:30 +00:00
|
|
|
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
explicit NetID(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);
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::ostream&
|
|
|
|
print(std::ostream& stream, int level, int spaces) const
|
2018-12-19 16:17:41 +00:00
|
|
|
{
|
2019-02-24 23:46:37 +00:00
|
|
|
Printer printer(stream, level, spaces);
|
|
|
|
printer.printValue(ToString());
|
|
|
|
return stream;
|
2018-12-19 16:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
inline std::ostream&
|
|
|
|
operator<<(std::ostream& out, const NetID& id)
|
2019-02-24 23:46:37 +00:00
|
|
|
{
|
|
|
|
return id.print(out, -1, -1);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2019-02-25 12:46:40 +00:00
|
|
|
struct Hash
|
|
|
|
{
|
|
|
|
size_t
|
2020-04-07 18:38:56 +00:00
|
|
|
operator()(const RouterContact& r) const
|
2019-02-25 12:46:40 +00:00
|
|
|
{
|
|
|
|
return PubKey::Hash()(r.pubkey);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-12 01:55:30 +00:00
|
|
|
// 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;
|
|
|
|
// advertised exits
|
2020-04-07 18:38:56 +00:00
|
|
|
std::vector<ExitInfo> exits;
|
2018-12-12 01:55:30 +00:00
|
|
|
// 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;
|
2020-04-07 18:38:56 +00:00
|
|
|
uint64_t version = LLARP_PROTO_VERSION;
|
2020-05-01 19:51:15 +00:00
|
|
|
std::optional<RouterVersion> routerVersion;
|
2018-12-12 01:55:30 +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
|
|
|
|
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
|
|
|
|
ToString() const
|
|
|
|
{
|
|
|
|
return ToJson().dump();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
{
|
2019-08-26 23:29:17 +00:00
|
|
|
return !exits.empty();
|
2018-12-12 01:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
BDecode(llarp_buffer_t* buf)
|
2018-12-12 01:55:30 +00:00
|
|
|
{
|
|
|
|
Clear();
|
2019-11-27 18:30:19 +00:00
|
|
|
return bencode_decode_dict(*this, 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;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::ostream&
|
|
|
|
print(std::ostream& stream, int level, int spaces) const;
|
2019-01-17 15:11:17 +00:00
|
|
|
|
2018-12-12 01:55:30 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
Read(const char* fname);
|
2018-12-12 01:55:30 +00:00
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
Write(const char* fname) const;
|
2018-12-12 01:55:30 +00:00
|
|
|
|
|
|
|
bool
|
2019-05-28 19:45:08 +00:00
|
|
|
VerifySignature() const;
|
2018-12-12 01:55:30 +00:00
|
|
|
};
|
2019-01-22 01:14:02 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
inline std::ostream&
|
|
|
|
operator<<(std::ostream& out, const RouterContact& rc)
|
2019-02-24 23:46:37 +00:00
|
|
|
{
|
|
|
|
return rc.print(out, -1, -1);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
#endif
|