diff --git a/llarp/CMakeLists.txt b/llarp/CMakeLists.txt index f033721d5..b860822d7 100644 --- a/llarp/CMakeLists.txt +++ b/llarp/CMakeLists.txt @@ -117,7 +117,6 @@ target_link_libraries(lokinet-config PUBLIC lokinet-dns lokinet-platform oxenmq: add_library(lokinet-amalgum STATIC - consensus/table.cpp consensus/reachability_testing.cpp bootstrap.cpp diff --git a/llarp/consensus/table.cpp b/llarp/consensus/table.cpp deleted file mode 100644 index 6645bc97c..000000000 --- a/llarp/consensus/table.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "table.hpp" -#include - -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 diff --git a/llarp/consensus/table.hpp b/llarp/consensus/table.hpp deleted file mode 100644 index 64327255b..000000000 --- a/llarp/consensus/table.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include -#include - -namespace llarp -{ - namespace consensus - { - /// consensus table - struct Table : public std::vector - { - ShortHash - CalculateHash() const; - }; - } // namespace consensus -} // namespace llarp diff --git a/llarp/exit/session.cpp b/llarp/exit/session.cpp index 1ae5870c3..9a873fb55 100644 --- a/llarp/exit/session.cpp +++ b/llarp/exit/session.cpp @@ -243,7 +243,7 @@ namespace llarp { queue.emplace_back(); 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(); // pack to nearest N @@ -251,10 +251,10 @@ namespace llarp { queue.emplace_back(); 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; - return back.PutBuffer(std::move(pkt), m_Counter++); + return back.PutBuffer(llarp_buffer_t{pkt}, m_Counter++); } bool diff --git a/llarp/quic/endpoint.cpp b/llarp/quic/endpoint.cpp index 4fa35434a..55fc9262c 100644 --- a/llarp/quic/endpoint.cpp +++ b/llarp/quic/endpoint.cpp @@ -185,7 +185,8 @@ namespace llarp::quic std::memcpy(&buf_[header_size], data.data(), data.size()); 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}); } diff --git a/llarp/service/outbound_context.cpp b/llarp/service/outbound_context.cpp index a67b70fe7..3494bceee 100644 --- a/llarp/service/outbound_context.cpp +++ b/llarp/service/outbound_context.cpp @@ -334,9 +334,10 @@ namespace llarp void OutboundContext::KeepAlive() { - Encrypted<64> tmp; - tmp.Randomize(); - SendPacketToRemote(tmp, ProtocolType::Control); + std::array tmp; + llarp_buffer_t buf{tmp}; + CryptoManager::instance()->randomize(buf); + SendPacketToRemote(buf, ProtocolType::Control); m_LastKeepAliveAt = Now(); } diff --git a/llarp/util/buffer.cpp b/llarp/util/buffer.cpp index 05c4162ea..2373ceadc 100644 --- a/llarp/util/buffer.cpp +++ b/llarp/util/buffer.cpp @@ -127,12 +127,6 @@ llarp_buffer_t::copy() const return copy; } -bool -operator==(const llarp_buffer_t& buff, std::string_view data) -{ - return std::string_view{reinterpret_cast(buff.cur), buff.size_left()} == data; -} - llarp::byte_view_t llarp_buffer_t::view() const { diff --git a/llarp/util/buffer.hpp b/llarp/util/buffer.hpp index 5d5899209..1bfef5db4 100644 --- a/llarp/util/buffer.hpp +++ b/llarp/util/buffer.hpp @@ -22,6 +22,8 @@ namespace llarp using byte_view_t = std::basic_string_view; } +struct ManagedBuffer; + /// 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 /// 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(const ManagedBuffer&) = delete; + llarp_buffer_t(ManagedBuffer &&) = delete; + + template + static constexpr bool is_basic_byte = sizeof(Byte) == 1 and std::is_trivially_copyable_v; + /// Construct referencing some 1-byte, trivially copyable (e.g. char, unsigned char, byte_t) /// pointer type and a buffer size. template < - typename T, - typename = std::enable_if_t>> - llarp_buffer_t(T * buf, size_t _sz) - : base(reinterpret_cast(const_cast*>(buf))) - , cur(base) - , sz(_sz) + typename Byte, + typename = std::enable_if_t && is_basic_byte>> + llarp_buffer_t(Byte * buf, size_t sz) : base{reinterpret_cast(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 && is_basic_byte>> + llarp_buffer_t(std::vector & 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 && is_basic_byte>> + llarp_buffer_t(std::array & b) : llarp_buffer_t{b.data(), b.size()} + {} + + // These overloads, const_casting away the const, are not just gross but downright dangerous: + template >> + [[deprecated("dangerous constructor that casts away constness, be very careful")]] llarp_buffer_t( + const Byte* buf, size_t sz) + : llarp_buffer_t{const_cast(buf), sz} + {} + + template >> + [[deprecated("dangerous constructor that casts away constness, be very careful")]] llarp_buffer_t( + const std::vector& b) + : llarp_buffer_t{const_cast(b.data()), b.size()} + {} + + template >> + [[deprecated("dangerous constructor that casts away constness, be very careful")]] llarp_buffer_t( + const std::array& b) + : llarp_buffer_t{const_cast(b.data()), b.size()} + {} + + /// Explicitly construct a llarp_buffer_t from anything with a `.data()` and a `.size()`. Cursed. template < typename T, typename = std::void_t().data() + std::declval().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() { return base; } - byte_t* begin() const + const byte_t* begin() const { return base; } @@ -74,7 +112,7 @@ struct [[deprecated("this type is stupid, use something else")]] llarp_buffer_t { return base + sz; } - byte_t* end() const + const byte_t* end() const { 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; }; - -bool -operator==(const llarp_buffer_t& buff, std::string_view data); - template bool llarp_buffer_t::read_into(OutputIt begin, OutputIt end)