2018-12-12 00:58:08 +00:00
|
|
|
#include <dns/serialize.hpp>
|
2019-01-11 01:42:02 +00:00
|
|
|
#include <net/net_int.hpp>
|
2018-12-03 22:22:59 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dns
|
|
|
|
{
|
2019-07-30 23:42:13 +00:00
|
|
|
Serialize::~Serialize() = default;
|
2019-04-24 23:27:31 +00:00
|
|
|
|
2018-12-03 22:22:59 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
EncodeRData(llarp_buffer_t* buf, const std::vector<byte_t>& v)
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (v.size() > 65536)
|
2018-12-03 22:22:59 +00:00
|
|
|
return false;
|
2018-12-04 16:16:43 +00:00
|
|
|
uint16_t len = v.size();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!buf->put_uint16(len))
|
2018-12-04 16:16:43 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (buf->size_left() < len)
|
2018-12-04 16:16:43 +00:00
|
|
|
return false;
|
|
|
|
memcpy(buf->cur, v.data(), len);
|
|
|
|
buf->cur += len;
|
|
|
|
return true;
|
2018-12-03 22:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
DecodeRData(llarp_buffer_t* buf, std::vector<byte_t>& v)
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2018-12-04 16:16:43 +00:00
|
|
|
uint16_t len;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!buf->read_uint16(len))
|
2018-12-03 22:22:59 +00:00
|
|
|
return false;
|
2019-02-17 12:13:34 +00:00
|
|
|
size_t left = buf->size_left();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (left < len)
|
2018-12-03 22:22:59 +00:00
|
|
|
return false;
|
2018-12-04 16:16:43 +00:00
|
|
|
v.resize(size_t(len));
|
2020-04-07 18:38:56 +00:00
|
|
|
if (len)
|
2018-12-04 16:16:43 +00:00
|
|
|
{
|
|
|
|
memcpy(v.data(), buf->cur, len);
|
|
|
|
buf->cur += len;
|
|
|
|
}
|
2018-12-03 22:22:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dns
|
|
|
|
} // namespace llarp
|