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

213 lines
4.1 KiB
C++
Raw Normal View History

2018-12-12 02:55:30 +01:00
#ifndef LLARP_RC_HPP
#define LLARP_RC_HPP
#include <constants/version.hpp>
#include <crypto/types.hpp>
#include <net/address_info.hpp>
#include <net/exit_info.hpp>
2019-01-13 15:00:50 +01:00
#include <util/aligned.hpp>
#include <util/bencode.hpp>
2019-02-08 20:43:25 +01:00
#include <util/status.hpp>
2018-12-12 02:55:30 +01:00
2019-01-22 02:14:02 +01:00
#include <functional>
2018-12-12 02:55:30 +01:00
#include <vector>
#define MAX_RC_SIZE (1024)
#define NICKLEN (32)
namespace llarp
{
/// NetID
struct NetID final : public AlignedBuffer< 8 >
{
2019-01-05 01:49:06 +01:00
static NetID &
DefaultValue();
NetID();
2019-01-05 01:49:06 +01:00
explicit NetID(const byte_t *val);
explicit NetID(const NetID &other) = default;
bool
operator==(const NetID &other) const;
bool
operator!=(const NetID &other) const
{
return !(*this == other);
}
2019-02-25 00:46:37 +01:00
std::ostream &
print(std::ostream &stream, int level, int spaces) const
{
2019-02-25 00:46:37 +01:00
Printer printer(stream, level, spaces);
printer.printValue(ToString());
return stream;
}
std::string
ToString() const;
bool
BDecode(llarp_buffer_t *buf);
bool
BEncode(llarp_buffer_t *buf) const;
};
2019-02-25 00:46:37 +01:00
inline std::ostream &
operator<<(std::ostream &out, const NetID &id)
{
return id.print(out, -1, -1);
}
/// RouterContact
2019-05-24 04:01:36 +02:00
struct RouterContact
2018-12-12 02:55:30 +01:00
{
/// for unit tests
static bool IgnoreBogons;
static llarp_time_t Lifetime;
static llarp_time_t UpdateInterval;
static llarp_time_t UpdateWindow;
2019-05-24 04:01:36 +02:00
RouterContact()
2018-12-12 02:55:30 +01:00
{
Clear();
}
2019-02-25 13:46:40 +01:00
struct Hash
{
size_t
operator()(const RouterContact &r) const
{
return PubKey::Hash()(r.pubkey);
}
};
2018-12-12 02:55:30 +01:00
// 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;
// advertised exits
std::vector< ExitInfo > exits;
// signature
llarp::Signature signature;
/// node nickname, yw kee
llarp::AlignedBuffer< NICKLEN > nickname;
uint64_t last_updated = 0;
2019-05-24 04:01:36 +02:00
uint64_t version = LLARP_PROTO_VERSION;
2018-12-12 02:55:30 +01:00
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
2018-12-12 02:55:30 +01:00
bool
2019-05-24 04:01:36 +02:00
BEncode(llarp_buffer_t *buf) const;
2018-12-12 02:55:30 +01:00
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
{
return exits.size() > 0;
}
bool
2019-05-24 04:01:36 +02:00
BDecode(llarp_buffer_t *buf)
2018-12-12 02:55:30 +01:00
{
Clear();
2019-05-24 04:01:36 +02:00
return bencode_decode_dict(*this, buf);
2018-12-12 02:55:30 +01:00
}
bool
2019-05-24 04:01:36 +02:00
DecodeKey(const llarp_buffer_t &k, llarp_buffer_t *buf);
2018-12-12 02:55:30 +01:00
RouterContact &
operator=(const RouterContact &other);
bool
HasNick() const;
std::string
Nick() const;
bool
IsPublicRouter() const;
void
SetNick(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
ExpiresSoon(llarp_time_t now, llarp_time_t dlt = 60000) const;
/// returns true if this RC is expired and should be removed
bool
IsExpired(llarp_time_t now) const;
2018-12-12 02:55:30 +01:00
bool
OtherIsNewer(const RouterContact &other) const
{
return last_updated < other.last_updated;
}
2019-02-25 00:46:37 +01:00
std::ostream &
print(std::ostream &stream, int level, int spaces) const;
2019-01-17 16:11:17 +01:00
2018-12-12 02:55:30 +01:00
bool
Read(const char *fname);
bool
Write(const char *fname) const;
private:
bool
VerifySignature() const;
2018-12-12 02:55:30 +01:00
};
2019-01-22 02:14:02 +01:00
2019-02-25 00:46:37 +01:00
inline std::ostream &
operator<<(std::ostream &out, const RouterContact &rc)
{
return rc.print(out, -1, -1);
}
2019-01-22 02:14:02 +01:00
using RouterLookupHandler =
2019-02-05 01:41:33 +01:00
std::function< void(const std::vector< RouterContact > &) >;
2018-12-12 02:55:30 +01:00
} // namespace llarp
#endif