2020-01-25 16:29:01 +00:00
|
|
|
#include <router_version.hpp>
|
|
|
|
#include <constants/version.hpp>
|
|
|
|
#include <constants/proto.hpp>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2020-01-25 17:21:28 +00:00
|
|
|
RouterVersion::RouterVersion(const Version_t& router, uint64_t proto)
|
|
|
|
: m_Version(router), m_ProtoVersion(proto)
|
2020-01-25 16:29:01 +00:00
|
|
|
{
|
2020-01-25 17:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RouterVersion::IsCompatableWith(const RouterVersion& other) const
|
|
|
|
{
|
|
|
|
return m_ProtoVersion == other.m_ProtoVersion;
|
2020-01-25 16:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RouterVersion::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not bencode_start_list(buf))
|
2020-01-25 16:29:01 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not IsEmpty())
|
2020-01-25 16:29:01 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not bencode_write_uint64(buf, m_ProtoVersion))
|
2020-01-25 17:21:28 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& i : m_Version)
|
2020-01-25 17:21:28 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not bencode_write_uint64(buf, i))
|
2020-01-25 16:29:01 +00:00
|
|
|
return false;
|
2020-01-25 17:21:28 +00:00
|
|
|
}
|
2020-01-25 16:29:01 +00:00
|
|
|
}
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RouterVersion::Clear()
|
|
|
|
{
|
2020-01-25 17:21:28 +00:00
|
|
|
m_Version.fill(0);
|
2020-01-31 20:31:00 +00:00
|
|
|
m_ProtoVersion = INVALID_VERSION;
|
2020-01-25 16:29:01 +00:00
|
|
|
assert(IsEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RouterVersion::IsEmpty() const
|
|
|
|
{
|
2020-01-25 17:38:12 +00:00
|
|
|
return *this == emptyRouterVersion;
|
2020-01-25 16:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RouterVersion::BDecode(llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
// clear before hand
|
2020-01-25 17:21:28 +00:00
|
|
|
Clear();
|
2020-01-25 16:29:01 +00:00
|
|
|
size_t idx = 0;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not bencode_read_list(
|
|
|
|
[self = this, &idx](llarp_buffer_t* buffer, bool has) {
|
|
|
|
if (has)
|
|
|
|
{
|
|
|
|
uint64_t i;
|
|
|
|
if (idx == 0)
|
|
|
|
{
|
|
|
|
uint64_t val = -1;
|
|
|
|
if (not bencode_read_integer(buffer, &val))
|
|
|
|
return false;
|
|
|
|
self->m_ProtoVersion = val;
|
|
|
|
}
|
|
|
|
else if (bencode_read_integer(buffer, &i))
|
|
|
|
{
|
|
|
|
// prevent overflow (note that idx includes version too)
|
|
|
|
if (idx > self->m_Version.max_size())
|
|
|
|
return false;
|
|
|
|
self->m_Version[idx - 1] = i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
buf))
|
2020-01-25 16:29:01 +00:00
|
|
|
return false;
|
|
|
|
// either full list or empty list is valid
|
2020-01-25 17:21:28 +00:00
|
|
|
return idx == 4 || idx == 0;
|
2020-01-25 16:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
RouterVersion::ToString() const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
return std::to_string(m_Version.at(0)) + "." + std::to_string(m_Version.at(1)) + "."
|
|
|
|
+ std::to_string(m_Version.at(2)) + " protocol version " + std::to_string(m_ProtoVersion);
|
2020-01-25 16:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llarp
|