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>
|
2018-12-12 01:55:30 +00:00
|
|
|
|
2019-01-22 01:14:02 +00:00
|
|
|
#include <functional>
|
2018-12-12 01:55:30 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#define MAX_RC_SIZE (1024)
|
|
|
|
#define NICKLEN (32)
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2019-01-13 16:30:07 +00:00
|
|
|
struct Crypto;
|
|
|
|
|
2018-12-19 16:17:41 +00:00
|
|
|
/// NetID
|
|
|
|
struct NetID final : public AlignedBuffer< 8 >
|
|
|
|
{
|
2019-01-05 00:49:06 +00:00
|
|
|
static NetID &
|
|
|
|
DefaultValue();
|
2018-12-21 13:08:01 +00:00
|
|
|
|
2018-12-19 16:17:41 +00:00
|
|
|
NetID();
|
|
|
|
|
2019-01-05 00:49:06 +00:00
|
|
|
explicit NetID(const byte_t *val);
|
|
|
|
|
|
|
|
explicit NetID(const NetID &other) = default;
|
|
|
|
|
2018-12-19 16:17:41 +00:00
|
|
|
bool
|
|
|
|
operator==(const NetID &other) const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator!=(const NetID &other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2019-02-24 23:46:37 +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
|
|
|
|
BDecode(llarp_buffer_t *buf);
|
|
|
|
|
|
|
|
bool
|
|
|
|
BEncode(llarp_buffer_t *buf) const;
|
|
|
|
};
|
|
|
|
|
2019-02-24 23:46:37 +00:00
|
|
|
inline std::ostream &
|
|
|
|
operator<<(std::ostream &out, const NetID &id)
|
|
|
|
{
|
|
|
|
return id.print(out, -1, -1);
|
|
|
|
}
|
|
|
|
|
2018-12-19 16:17:41 +00:00
|
|
|
/// RouterContact
|
2019-02-08 19:43:25 +00:00
|
|
|
struct RouterContact final : public IBEncodeMessage, public util::IStateful
|
2018-12-12 01:55:30 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
/// for unit tests
|
|
|
|
static bool IgnoreBogons;
|
|
|
|
|
2018-12-19 16:17:41 +00:00
|
|
|
static llarp_time_t Lifetime;
|
|
|
|
|
2018-12-12 01:55:30 +00:00
|
|
|
RouterContact() : IBEncodeMessage()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
RouterContact(const RouterContact &other)
|
2019-01-02 22:21:29 +00:00
|
|
|
: IBEncodeMessage(other.version)
|
2018-12-12 01:55:30 +00:00
|
|
|
, addrs(other.addrs)
|
2018-12-19 16:17:41 +00:00
|
|
|
, netID(other.netID)
|
2018-12-12 01:55:30 +00:00
|
|
|
, enckey(other.enckey)
|
|
|
|
, pubkey(other.pubkey)
|
|
|
|
, exits(other.exits)
|
|
|
|
, signature(other.signature)
|
|
|
|
, nickname(other.nickname)
|
|
|
|
, last_updated(other.last_updated)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-25 12:46:40 +00:00
|
|
|
struct Hash
|
|
|
|
{
|
|
|
|
size_t
|
|
|
|
operator()(const RouterContact &r) const
|
|
|
|
{
|
|
|
|
return PubKey::Hash()(r.pubkey);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-12 01:55:30 +00:00
|
|
|
// advertised addresses
|
|
|
|
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
|
|
|
|
std::vector< ExitInfo > exits;
|
|
|
|
// signature
|
|
|
|
llarp::Signature signature;
|
|
|
|
/// node nickname, yw kee
|
|
|
|
llarp::AlignedBuffer< NICKLEN > nickname;
|
|
|
|
|
2019-02-08 13:42:12 +00:00
|
|
|
uint64_t last_updated = 0;
|
2018-12-12 01:55:30 +00:00
|
|
|
|
2019-02-11 17:14:43 +00:00
|
|
|
util::StatusObject
|
|
|
|
ExtractStatus() const override;
|
2019-02-08 19:43:25 +00:00
|
|
|
|
2018-12-12 01:55:30 +00:00
|
|
|
bool
|
|
|
|
BEncode(llarp_buffer_t *buf) const override;
|
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
bool
|
|
|
|
operator==(const RouterContact &other) const
|
|
|
|
{
|
|
|
|
return addrs == other.addrs && enckey == other.enckey
|
|
|
|
&& pubkey == other.pubkey && signature == other.signature
|
2018-12-19 16:17:41 +00:00
|
|
|
&& 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
|
|
|
|
operator<(const RouterContact &other) const
|
|
|
|
{
|
|
|
|
return pubkey < other.pubkey;
|
|
|
|
}
|
|
|
|
|
2019-03-01 19:10:42 +00:00
|
|
|
bool
|
|
|
|
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
|
|
|
|
{
|
|
|
|
return exits.size() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BDecode(llarp_buffer_t *buf) override
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
return IBEncodeMessage::BDecode(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-02-01 01:58:06 +00:00
|
|
|
DecodeKey(const llarp_buffer_t &k, llarp_buffer_t *buf) override;
|
2018-12-12 01:55:30 +00:00
|
|
|
|
|
|
|
RouterContact &
|
|
|
|
operator=(const RouterContact &other);
|
|
|
|
|
|
|
|
bool
|
|
|
|
HasNick() const;
|
|
|
|
|
|
|
|
std::string
|
|
|
|
Nick() const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsPublicRouter() const;
|
|
|
|
|
|
|
|
void
|
|
|
|
SetNick(const std::string &nick);
|
|
|
|
|
|
|
|
bool
|
2018-12-19 16:17:41 +00:00
|
|
|
Verify(llarp::Crypto *crypto, llarp_time_t now) const;
|
2018-12-12 01:55:30 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
Sign(llarp::Crypto *crypto, const llarp::SecretKey &secret);
|
|
|
|
|
2018-12-19 16:17:41 +00:00
|
|
|
/// does this RC expire soon? default delta is 1 minute
|
|
|
|
bool
|
|
|
|
ExpiresSoon(llarp_time_t now, llarp_time_t dlt = 60000) const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsExpired(llarp_time_t now) const;
|
|
|
|
|
2018-12-12 01:55:30 +00:00
|
|
|
bool
|
|
|
|
OtherIsNewer(const RouterContact &other) const
|
|
|
|
{
|
|
|
|
return last_updated < other.last_updated;
|
|
|
|
}
|
|
|
|
|
2019-02-24 23:46:37 +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
|
|
|
|
Read(const char *fname);
|
|
|
|
|
|
|
|
bool
|
|
|
|
Write(const char *fname) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool
|
|
|
|
VerifySignature(llarp::Crypto *crypto) const;
|
|
|
|
};
|
2019-01-22 01:14:02 +00:00
|
|
|
|
2019-02-24 23:46:37 +00:00
|
|
|
inline std::ostream &
|
|
|
|
operator<<(std::ostream &out, const RouterContact &rc)
|
|
|
|
{
|
|
|
|
return rc.print(out, -1, -1);
|
|
|
|
}
|
|
|
|
|
2019-01-22 01:14:02 +00:00
|
|
|
using RouterLookupHandler =
|
2019-02-05 00:41:33 +00:00
|
|
|
std::function< void(const std::vector< RouterContact > &) >;
|
2018-12-12 01:55:30 +00:00
|
|
|
} // namespace llarp
|
|
|
|
|
|
|
|
#endif
|