2018-07-16 03:32:13 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <llarp/service/Info.hpp>
|
|
|
|
#include <llarp/service/address.hpp>
|
|
|
|
#include "buffer.hpp"
|
2018-10-23 12:40:34 +00:00
|
|
|
|
|
|
|
#include <sodium/crypto_generichash.h>
|
2018-07-16 03:32:13 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace service
|
|
|
|
{
|
|
|
|
bool
|
|
|
|
ServiceInfo::DecodeKey(llarp_buffer_t key, llarp_buffer_t* val)
|
|
|
|
{
|
|
|
|
bool read = false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("e", enckey, read, key, val))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("s", signkey, read, key, val))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictInt("v", version, read, key, val))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("x", vanity, read, key, val))
|
|
|
|
return false;
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ServiceInfo::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("e", enckey, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("s", signkey, buf))
|
|
|
|
return false;
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("v", LLARP_PROTO_VERSION, buf))
|
2018-07-16 03:32:13 +00:00
|
|
|
return false;
|
|
|
|
if(!vanity.IsZero())
|
|
|
|
{
|
|
|
|
if(!BEncodeWriteDictEntry("x", vanity, buf))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ServiceInfo::Name() const
|
|
|
|
{
|
2018-07-18 03:10:21 +00:00
|
|
|
if(m_CachedAddr.IsZero())
|
|
|
|
{
|
|
|
|
Address addr;
|
2018-08-10 21:34:11 +00:00
|
|
|
CalculateAddress(addr.data());
|
2018-07-18 03:10:21 +00:00
|
|
|
return addr.ToString();
|
|
|
|
}
|
|
|
|
return m_CachedAddr.ToString();
|
2018-07-16 03:32:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ServiceInfo::CalculateAddress(byte_t* addr) const
|
|
|
|
{
|
2018-08-10 21:34:11 +00:00
|
|
|
byte_t tmp[256] = {0};
|
|
|
|
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
|
|
|
if(!BEncode(&buf))
|
|
|
|
return false;
|
2018-10-23 11:29:37 +00:00
|
|
|
return crypto_generichash_blake2b(addr, 32, buf.base, buf.cur - buf.base,
|
|
|
|
nullptr, 0)
|
2018-07-16 03:32:13 +00:00
|
|
|
!= -1;
|
|
|
|
}
|
2018-07-18 03:10:21 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
ServiceInfo::UpdateAddr()
|
|
|
|
{
|
2018-08-10 21:34:11 +00:00
|
|
|
return CalculateAddress(m_CachedAddr.data());
|
2018-07-18 03:10:21 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 03:32:13 +00:00
|
|
|
} // namespace service
|
2018-10-23 11:29:37 +00:00
|
|
|
} // namespace llarp
|