2018-07-09 17:32:11 +00:00
|
|
|
#ifndef LLARP_SERVICE_INFO_HPP
|
|
|
|
#define LLARP_SERVICE_INFO_HPP
|
2018-12-12 02:15:08 +00:00
|
|
|
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <bencode.hpp>
|
2018-12-12 00:38:58 +00:00
|
|
|
#include <crypto.hpp>
|
2018-12-12 02:15:08 +00:00
|
|
|
#include <service/types.hpp>
|
2018-07-09 17:32:11 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace service
|
|
|
|
{
|
2018-11-05 11:27:12 +00:00
|
|
|
struct ServiceInfo final : public llarp::IBEncodeMessage
|
2018-07-09 17:32:11 +00:00
|
|
|
{
|
2018-08-10 21:34:11 +00:00
|
|
|
private:
|
2018-07-09 17:32:11 +00:00
|
|
|
llarp::PubKey enckey;
|
|
|
|
llarp::PubKey signkey;
|
2018-12-02 18:07:07 +00:00
|
|
|
|
2018-08-10 21:34:11 +00:00
|
|
|
public:
|
2018-11-27 21:48:12 +00:00
|
|
|
VanityNonce vanity;
|
2018-12-02 18:07:07 +00:00
|
|
|
|
2018-08-10 21:34:11 +00:00
|
|
|
ServiceInfo() = default;
|
|
|
|
|
2018-11-08 12:31:50 +00:00
|
|
|
ServiceInfo(ServiceInfo&& other)
|
2018-08-10 21:34:11 +00:00
|
|
|
{
|
|
|
|
enckey = std::move(other.enckey);
|
|
|
|
signkey = std::move(other.signkey);
|
|
|
|
version = std::move(other.version);
|
|
|
|
vanity = std::move(other.vanity);
|
|
|
|
m_CachedAddr = std::move(other.m_CachedAddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceInfo(const ServiceInfo& other)
|
2018-11-07 15:30:22 +00:00
|
|
|
: IBEncodeMessage(other.version)
|
|
|
|
, enckey(other.enckey)
|
2018-08-29 20:40:26 +00:00
|
|
|
, signkey(other.signkey)
|
|
|
|
, vanity(other.vanity)
|
|
|
|
, m_CachedAddr(other.m_CachedAddr)
|
2018-08-10 21:34:11 +00:00
|
|
|
{
|
2018-08-29 20:40:26 +00:00
|
|
|
version = other.version;
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RandomizeVanity()
|
|
|
|
{
|
|
|
|
vanity.Randomize();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-12-11 00:53:11 +00:00
|
|
|
Verify(llarp::Crypto* crypto, llarp_buffer_t payload,
|
2018-08-10 21:34:11 +00:00
|
|
|
const Signature& sig) const
|
|
|
|
{
|
|
|
|
return crypto->verify(signkey, payload, sig);
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:22:31 +00:00
|
|
|
const byte_t*
|
|
|
|
EncryptionPublicKey() const
|
2018-08-10 21:34:11 +00:00
|
|
|
{
|
|
|
|
return enckey;
|
|
|
|
}
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2018-08-10 21:34:11 +00:00
|
|
|
bool
|
2018-12-02 18:07:07 +00:00
|
|
|
Update(const byte_t* enc, const byte_t* sign,
|
|
|
|
const byte_t* nonce = nullptr)
|
2018-08-10 21:34:11 +00:00
|
|
|
{
|
|
|
|
enckey = enc;
|
|
|
|
signkey = sign;
|
2018-11-27 21:54:06 +00:00
|
|
|
if(nonce)
|
|
|
|
vanity = nonce;
|
2018-08-10 21:34:11 +00:00
|
|
|
return UpdateAddr();
|
|
|
|
}
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2018-07-11 16:11:19 +00:00
|
|
|
bool
|
|
|
|
operator==(const ServiceInfo& other) const
|
|
|
|
{
|
|
|
|
return enckey == other.enckey && signkey == other.signkey
|
|
|
|
&& version == other.version && vanity == other.vanity;
|
|
|
|
}
|
|
|
|
|
2018-08-10 21:34:11 +00:00
|
|
|
bool
|
|
|
|
operator!=(const ServiceInfo& other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2018-07-09 17:32:11 +00:00
|
|
|
ServiceInfo&
|
|
|
|
operator=(const ServiceInfo& other)
|
|
|
|
{
|
|
|
|
enckey = other.enckey;
|
|
|
|
signkey = other.signkey;
|
|
|
|
version = other.version;
|
|
|
|
vanity = other.vanity;
|
2018-09-17 15:32:37 +00:00
|
|
|
version = other.version;
|
2018-07-18 03:10:21 +00:00
|
|
|
UpdateAddr();
|
2018-07-09 17:32:11 +00:00
|
|
|
return *this;
|
|
|
|
};
|
|
|
|
|
2018-07-18 03:10:21 +00:00
|
|
|
bool
|
|
|
|
operator<(const ServiceInfo& other) const
|
|
|
|
{
|
|
|
|
return Addr() < other.Addr();
|
|
|
|
}
|
|
|
|
|
2018-07-09 17:32:11 +00:00
|
|
|
friend std::ostream&
|
|
|
|
operator<<(std::ostream& out, const ServiceInfo& i)
|
|
|
|
{
|
|
|
|
return out << "[e=" << i.enckey << " s=" << i.signkey
|
|
|
|
<< " v=" << i.version << " x=" << i.vanity << "]";
|
|
|
|
}
|
|
|
|
|
2018-08-10 21:34:11 +00:00
|
|
|
/// .loki address
|
2018-07-16 03:32:13 +00:00
|
|
|
std::string
|
|
|
|
Name() const;
|
|
|
|
|
2018-07-18 03:10:21 +00:00
|
|
|
bool
|
|
|
|
UpdateAddr();
|
|
|
|
|
|
|
|
const Address&
|
|
|
|
Addr() const
|
|
|
|
{
|
|
|
|
return m_CachedAddr;
|
|
|
|
}
|
|
|
|
|
2018-07-09 17:32:11 +00:00
|
|
|
/// calculate our address
|
|
|
|
bool
|
2018-07-12 13:43:37 +00:00
|
|
|
CalculateAddress(byte_t* buf) const;
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2018-07-19 04:58:39 +00:00
|
|
|
bool
|
2018-11-05 11:27:12 +00:00
|
|
|
BDecode(llarp_buffer_t* buf) override
|
2018-07-19 04:58:39 +00:00
|
|
|
{
|
2018-08-10 21:34:11 +00:00
|
|
|
if(IBEncodeMessage::BDecode(buf))
|
|
|
|
return CalculateAddress(m_CachedAddr.data());
|
|
|
|
return false;
|
2018-07-19 04:58:39 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 17:32:11 +00:00
|
|
|
bool
|
2018-11-05 11:27:12 +00:00
|
|
|
BEncode(llarp_buffer_t* buf) const override;
|
2018-07-09 17:32:11 +00:00
|
|
|
|
|
|
|
bool
|
2018-11-05 11:27:12 +00:00
|
|
|
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) override;
|
2018-07-18 03:10:21 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Address m_CachedAddr;
|
2018-07-09 17:32:11 +00:00
|
|
|
};
|
|
|
|
} // namespace service
|
|
|
|
} // namespace llarp
|
|
|
|
|
2018-08-29 20:40:26 +00:00
|
|
|
#endif
|