lokinet/llarp/router_contact.hpp

198 lines
4.1 KiB
C++
Raw Normal View History

2018-12-12 01:55:30 +00: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 14:00:50 +00:00
#include <util/aligned.hpp>
#include <util/bencode.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
{
struct Crypto;
/// NetID
struct NetID final : public AlignedBuffer< 8 >
{
2019-01-05 00:49:06 +00:00
static NetID &
DefaultValue();
NetID();
2019-01-05 00:49:06 +00: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);
}
friend std::ostream &
operator<<(std::ostream &out, const NetID &id)
{
return out << id.ToString();
}
std::string
ToString() const;
bool
BDecode(llarp_buffer_t *buf);
bool
BEncode(llarp_buffer_t *buf) const;
};
/// RouterContact
2018-12-12 01:55:30 +00:00
struct RouterContact final : public IBEncodeMessage
{
/// for unit tests
static bool IgnoreBogons;
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)
, 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)
{
}
// advertised addresses
std::vector< AddressInfo > addrs;
// 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;
uint64_t last_updated;
bool
BEncode(llarp_buffer_t *buf) const override;
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;
}
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
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
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);
/// 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-01-17 15:11:17 +00:00
friend std::ostream &
operator<<(std::ostream &out, const RouterContact &rc)
{
out << "[RouterContact k=" << rc.pubkey;
out << " updated=" << rc.last_updated;
out << " netid=" << rc.netID;
out << " v=" << rc.version;
out << " ai=[ ";
for(const auto &addr : rc.addrs)
out << addr << " ";
out << " ] xi=[ ";
for(const auto &xi : rc.exits)
out << xi << " ";
return out << " ] e=" << rc.enckey << " z=" << rc.signature << " ]";
}
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
using RouterLookupHandler =
std::function< void(const std::vector< RouterContact >&) >;
2018-12-12 01:55:30 +00:00
} // namespace llarp
#endif