Apply some lipstick to llarp_buffer_t

This class is cursed, but also broken under gcc-12.  Apply some lipstick
to get it moving again (but we really need to refactor this because it
is a mess).
pull/1969/head
Jason Rhinelander 2 years ago
parent 24dcffabe5
commit 15443568db
No known key found for this signature in database
GPG Key ID: C4992CE7A88D4262

@ -117,7 +117,6 @@ target_link_libraries(lokinet-config PUBLIC lokinet-dns lokinet-platform oxenmq:
add_library(lokinet-amalgum add_library(lokinet-amalgum
STATIC STATIC
consensus/table.cpp
consensus/reachability_testing.cpp consensus/reachability_testing.cpp
bootstrap.cpp bootstrap.cpp

@ -1,17 +0,0 @@
#include "table.hpp"
#include <llarp/crypto/crypto.hpp>
namespace llarp
{
namespace consensus
{
ShortHash
Table::CalculateHash() const
{
ShortHash h;
const llarp_buffer_t buf(begin()->data(), size());
CryptoManager::instance()->shorthash(h, buf);
return h;
}
} // namespace consensus
} // namespace llarp

@ -1,17 +0,0 @@
#pragma once
#include <llarp/crypto/types.hpp>
#include <vector>
namespace llarp
{
namespace consensus
{
/// consensus table
struct Table : public std::vector<RouterID>
{
ShortHash
CalculateHash() const;
};
} // namespace consensus
} // namespace llarp

@ -243,7 +243,7 @@ namespace llarp
{ {
queue.emplace_back(); queue.emplace_back();
queue.back().protocol = t; queue.back().protocol = t;
return queue.back().PutBuffer(std::move(pkt), m_Counter++); return queue.back().PutBuffer(llarp_buffer_t{pkt}, m_Counter++);
} }
auto& back = queue.back(); auto& back = queue.back();
// pack to nearest N // pack to nearest N
@ -251,10 +251,10 @@ namespace llarp
{ {
queue.emplace_back(); queue.emplace_back();
queue.back().protocol = t; queue.back().protocol = t;
return queue.back().PutBuffer(std::move(pkt), m_Counter++); return queue.back().PutBuffer(llarp_buffer_t{pkt}, m_Counter++);
} }
back.protocol = t; back.protocol = t;
return back.PutBuffer(std::move(pkt), m_Counter++); return back.PutBuffer(llarp_buffer_t{pkt}, m_Counter++);
} }
bool bool

@ -185,7 +185,8 @@ namespace llarp::quic
std::memcpy(&buf_[header_size], data.data(), data.size()); std::memcpy(&buf_[header_size], data.data(), data.size());
bstring_view outgoing{buf_.data(), outgoing_len}; bstring_view outgoing{buf_.data(), outgoing_len};
if (service_endpoint.SendToOrQueue(to, outgoing, service::ProtocolType::QUIC)) if (service_endpoint.SendToOrQueue(
to, llarp_buffer_t{outgoing.data(), outgoing.size()}, service::ProtocolType::QUIC))
{ {
LogTrace("[", to, "]: sent ", buffer_printer{outgoing}); LogTrace("[", to, "]: sent ", buffer_printer{outgoing});
} }

@ -334,9 +334,10 @@ namespace llarp
void void
OutboundContext::KeepAlive() OutboundContext::KeepAlive()
{ {
Encrypted<64> tmp; std::array<byte_t, 64> tmp;
tmp.Randomize(); llarp_buffer_t buf{tmp};
SendPacketToRemote(tmp, ProtocolType::Control); CryptoManager::instance()->randomize(buf);
SendPacketToRemote(buf, ProtocolType::Control);
m_LastKeepAliveAt = Now(); m_LastKeepAliveAt = Now();
} }

@ -127,12 +127,6 @@ llarp_buffer_t::copy() const
return copy; return copy;
} }
bool
operator==(const llarp_buffer_t& buff, std::string_view data)
{
return std::string_view{reinterpret_cast<const char*>(buff.cur), buff.size_left()} == data;
}
llarp::byte_view_t llarp::byte_view_t
llarp_buffer_t::view() const llarp_buffer_t::view() const
{ {

@ -22,6 +22,8 @@ namespace llarp
using byte_view_t = std::basic_string_view<byte_t>; using byte_view_t = std::basic_string_view<byte_t>;
} }
struct ManagedBuffer;
/// TODO: replace usage of these with std::span (via a backport until we move to C++20). That's a /// TODO: replace usage of these with std::span (via a backport until we move to C++20). That's a
/// fairly big job, though, as llarp_buffer_t is currently used a bit differently (i.e. maintains /// fairly big job, though, as llarp_buffer_t is currently used a bit differently (i.e. maintains
/// both start and current position, plus has some value reading/writing methods). /// both start and current position, plus has some value reading/writing methods).
@ -44,29 +46,65 @@ struct [[deprecated("this type is stupid, use something else")]] llarp_buffer_t
llarp_buffer_t(byte_t * b, byte_t * c, size_t s) : base(b), cur(c), sz(s) llarp_buffer_t(byte_t * b, byte_t * c, size_t s) : base(b), cur(c), sz(s)
{} {}
llarp_buffer_t(const ManagedBuffer&) = delete;
llarp_buffer_t(ManagedBuffer &&) = delete;
template <typename Byte>
static constexpr bool is_basic_byte = sizeof(Byte) == 1 and std::is_trivially_copyable_v<Byte>;
/// Construct referencing some 1-byte, trivially copyable (e.g. char, unsigned char, byte_t) /// Construct referencing some 1-byte, trivially copyable (e.g. char, unsigned char, byte_t)
/// pointer type and a buffer size. /// pointer type and a buffer size.
template < template <
typename T, typename Byte,
typename = std::enable_if_t<sizeof(T) == 1 and std::is_trivially_copyable_v<T>>> typename = std::enable_if_t<not std::is_const_v<Byte> && is_basic_byte<Byte>>>
llarp_buffer_t(T * buf, size_t _sz) llarp_buffer_t(Byte * buf, size_t sz) : base{reinterpret_cast<byte_t*>(buf)}, cur{base}, sz{sz}
: base(reinterpret_cast<byte_t*>(const_cast<std::remove_const_t<T>*>(buf))) {}
, cur(base)
, sz(_sz) /// initialize llarp_buffer_t from vector or array of byte-like values
template <
typename Byte,
typename = std::enable_if_t<not std::is_const_v<Byte> && is_basic_byte<Byte>>>
llarp_buffer_t(std::vector<Byte> & b) : llarp_buffer_t{b.data(), b.size()}
{} {}
/// initialize llarp_buffer_t from containers supporting .data() and .size() template <
typename Byte,
size_t N,
typename = std::enable_if_t<not std::is_const_v<Byte> && is_basic_byte<Byte>>>
llarp_buffer_t(std::array<Byte, N> & b) : llarp_buffer_t{b.data(), b.size()}
{}
// These overloads, const_casting away the const, are not just gross but downright dangerous:
template <typename Byte, typename = std::enable_if_t<is_basic_byte<Byte>>>
[[deprecated("dangerous constructor that casts away constness, be very careful")]] llarp_buffer_t(
const Byte* buf, size_t sz)
: llarp_buffer_t{const_cast<Byte*>(buf), sz}
{}
template <typename Byte, typename = std::enable_if_t<is_basic_byte<Byte>>>
[[deprecated("dangerous constructor that casts away constness, be very careful")]] llarp_buffer_t(
const std::vector<Byte>& b)
: llarp_buffer_t{const_cast<Byte*>(b.data()), b.size()}
{}
template <typename Byte, size_t N, typename = std::enable_if_t<is_basic_byte<Byte>>>
[[deprecated("dangerous constructor that casts away constness, be very careful")]] llarp_buffer_t(
const std::array<Byte, N>& b)
: llarp_buffer_t{const_cast<Byte*>(b.data()), b.size()}
{}
/// Explicitly construct a llarp_buffer_t from anything with a `.data()` and a `.size()`. Cursed.
template < template <
typename T, typename T,
typename = std::void_t<decltype(std::declval<T>().data() + std::declval<T>().size())>> typename = std::void_t<decltype(std::declval<T>().data() + std::declval<T>().size())>>
llarp_buffer_t(T && t) : llarp_buffer_t{t.data(), t.size()} explicit llarp_buffer_t(T && t) : llarp_buffer_t{t.data(), t.size()}
{} {}
byte_t* begin() byte_t* begin()
{ {
return base; return base;
} }
byte_t* begin() const const byte_t* begin() const
{ {
return base; return base;
} }
@ -74,7 +112,7 @@ struct [[deprecated("this type is stupid, use something else")]] llarp_buffer_t
{ {
return base + sz; return base + sz;
} }
byte_t* end() const const byte_t* end() const
{ {
return base + sz; return base + sz;
} }
@ -125,10 +163,6 @@ struct [[deprecated("this type is stupid, use something else")]] llarp_buffer_t
llarp_buffer_t(llarp_buffer_t &&) = default; llarp_buffer_t(llarp_buffer_t &&) = default;
}; };
bool
operator==(const llarp_buffer_t& buff, std::string_view data);
template <typename OutputIt> template <typename OutputIt>
bool bool
llarp_buffer_t::read_into(OutputIt begin, OutputIt end) llarp_buffer_t::read_into(OutputIt begin, OutputIt end)

Loading…
Cancel
Save