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
|
|
|
|
2019-01-13 16:30:07 +00:00
|
|
|
#include <crypto/types.hpp>
|
2019-04-22 18:35:19 +00:00
|
|
|
#include <service/address.hpp>
|
|
|
|
#include <service/vanity.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/bencode.hpp>
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2019-02-03 02:13:31 +00:00
|
|
|
#include <absl/types/optional.h>
|
2019-01-02 01:04:04 +00:00
|
|
|
|
2018-07-09 17:32:11 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace service
|
|
|
|
{
|
2019-05-24 02:01:36 +00:00
|
|
|
struct ServiceInfo
|
2018-07-09 17:32:11 +00:00
|
|
|
{
|
2018-08-10 21:34:11 +00:00
|
|
|
private:
|
2019-04-22 18:35:19 +00:00
|
|
|
PubKey enckey;
|
|
|
|
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;
|
2019-05-24 02:01:36 +00:00
|
|
|
Address m_CachedAddr;
|
|
|
|
uint64_t version = LLARP_PROTO_VERSION;
|
2018-12-02 18:07:07 +00:00
|
|
|
|
2019-02-03 02:13:31 +00:00
|
|
|
using OptNonce = absl::optional< VanityNonce >;
|
2019-01-02 01:04:04 +00:00
|
|
|
|
2018-08-10 21:34:11 +00:00
|
|
|
void
|
|
|
|
RandomizeVanity()
|
|
|
|
{
|
|
|
|
vanity.Randomize();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-05-28 19:45:08 +00:00
|
|
|
Verify(const llarp_buffer_t& payload, const Signature& sig) const;
|
2018-08-10 21:34:11 +00:00
|
|
|
|
2019-01-02 01:04:04 +00:00
|
|
|
const PubKey&
|
2018-08-13 23:22:31 +00:00
|
|
|
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,
|
2019-01-02 01:04:04 +00:00
|
|
|
const OptNonce& nonce = OptNonce())
|
2018-08-10 21:34:11 +00:00
|
|
|
{
|
|
|
|
enckey = enc;
|
|
|
|
signkey = sign;
|
2018-11-27 21:54:06 +00:00
|
|
|
if(nonce)
|
2019-01-02 01:04:04 +00:00
|
|
|
{
|
|
|
|
vanity = nonce.value();
|
|
|
|
}
|
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-18 03:10:21 +00:00
|
|
|
bool
|
|
|
|
operator<(const ServiceInfo& other) const
|
|
|
|
{
|
|
|
|
return Addr() < other.Addr();
|
|
|
|
}
|
|
|
|
|
2019-02-24 23:46:37 +00:00
|
|
|
std::ostream&
|
|
|
|
print(std::ostream& stream, int level, int spaces) const;
|
2018-07-09 17:32:11 +00:00
|
|
|
|
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
|
2019-11-14 15:07:40 +00:00
|
|
|
bool CalculateAddress(std::array< byte_t, 32 >& data) const;
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2018-07-19 04:58:39 +00:00
|
|
|
bool
|
2019-05-24 02:01:36 +00:00
|
|
|
BDecode(llarp_buffer_t* buf)
|
2018-07-19 04:58:39 +00:00
|
|
|
{
|
2019-05-24 02:01:36 +00:00
|
|
|
if(bencode_decode_dict(*this, buf))
|
2019-01-02 01:03:53 +00:00
|
|
|
return CalculateAddress(m_CachedAddr.as_array());
|
2018-08-10 21:34:11 +00:00
|
|
|
return false;
|
2018-07-19 04:58:39 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 17:32:11 +00:00
|
|
|
bool
|
2019-05-24 02:01:36 +00:00
|
|
|
BEncode(llarp_buffer_t* buf) const;
|
2018-07-09 17:32:11 +00:00
|
|
|
|
|
|
|
bool
|
2019-05-24 02:01:36 +00:00
|
|
|
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf);
|
2018-07-09 17:32:11 +00:00
|
|
|
};
|
2019-02-24 23:46:37 +00:00
|
|
|
|
|
|
|
inline std::ostream&
|
|
|
|
operator<<(std::ostream& out, const ServiceInfo& i)
|
|
|
|
{
|
|
|
|
return i.print(out, -1, -1);
|
|
|
|
}
|
2018-07-09 17:32:11 +00:00
|
|
|
} // namespace service
|
|
|
|
} // namespace llarp
|
|
|
|
|
2018-08-29 20:40:26 +00:00
|
|
|
#endif
|