mirror of
https://github.com/oxen-io/oxen-core.git
synced 2023-12-14 02:22:56 +01:00
Replace epee::byte_slice with much simpler shared_sv
byte_slice was an abomination that used more than 400 lines of code to implement what can be implemented in about 20 lines of code with a std::shared_ptr<std::string> and a std::string_view and a couple convenience methods. Much of the levin noise generation using it used it gratuitously: they always allocate a new string, but then return that wrapped it in a byte_slice abomination because... well, there's no reason at all except apparently that the byte_slice author wanted to push byte_slice into places it didn't belong at all (even if you accept the overbuilt monstrosity). Those methods now take a string_view and return a string, which is what they should have done in the first place.
This commit is contained in:
parent
5009de02a5
commit
0706727000
21 changed files with 248 additions and 941 deletions
|
@ -1,145 +0,0 @@
|
|||
// Copyright (c) 2019, The Monero Project
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "span.h"
|
||||
|
||||
namespace epee
|
||||
{
|
||||
struct byte_slice_data;
|
||||
|
||||
struct release_byte_slice
|
||||
{
|
||||
void operator()(byte_slice_data*) const noexcept;
|
||||
};
|
||||
|
||||
/*! Inspired by slices in golang. Storage is thread-safe reference counted,
|
||||
allowing for cheap copies or range selection on the bytes. The bytes
|
||||
owned by this class are always immutable.
|
||||
|
||||
The functions `operator=`, `take_slice` and `remove_prefix` may alter the
|
||||
reference count for the backing store, which will invalidate pointers
|
||||
previously returned if the reference count is zero. Be careful about
|
||||
"caching" pointers in these circumstances. */
|
||||
class byte_slice
|
||||
{
|
||||
/* A custom reference count is used instead of shared_ptr because it allows
|
||||
for an allocation optimization for the span constructor. This also
|
||||
reduces the size of this class by one pointer. */
|
||||
std::unique_ptr<byte_slice_data, release_byte_slice> storage_;
|
||||
span<const std::uint8_t> portion_; // within storage_
|
||||
|
||||
//! Internal use only; use to increase `storage` reference count.
|
||||
byte_slice(byte_slice_data* storage, span<const std::uint8_t> portion) noexcept;
|
||||
|
||||
struct adapt_buffer{};
|
||||
|
||||
template<typename T>
|
||||
explicit byte_slice(const adapt_buffer, T&& buffer);
|
||||
|
||||
public:
|
||||
using value_type = std::uint8_t;
|
||||
using size_type = std::size_t;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using pointer = const std::uint8_t*;
|
||||
using const_pointer = const std::uint8_t*;
|
||||
using reference = std::uint8_t;
|
||||
using const_reference = std::uint8_t;
|
||||
using iterator = pointer;
|
||||
using const_iterator = const_pointer;
|
||||
|
||||
//! Construct empty slice.
|
||||
byte_slice() noexcept
|
||||
: storage_(nullptr), portion_()
|
||||
{}
|
||||
|
||||
//! Construct empty slice
|
||||
byte_slice(std::nullptr_t) noexcept
|
||||
: byte_slice()
|
||||
{}
|
||||
|
||||
//! Scatter-gather (copy) multiple `sources` into a single allocated slice.
|
||||
explicit byte_slice(std::initializer_list<span<const std::uint8_t>> sources);
|
||||
|
||||
//! Convert `buffer` into a slice using one allocation for shared count.
|
||||
explicit byte_slice(std::vector<std::uint8_t>&& buffer);
|
||||
|
||||
//! Convert `buffer` into a slice using one allocation for shared count.
|
||||
explicit byte_slice(std::string&& buffer);
|
||||
|
||||
byte_slice(byte_slice&& source) noexcept;
|
||||
~byte_slice() noexcept = default;
|
||||
|
||||
//! \note May invalidate previously retrieved pointers.
|
||||
byte_slice& operator=(byte_slice&&) noexcept;
|
||||
|
||||
//! \return A shallow (cheap) copy of the data from `this` slice.
|
||||
byte_slice clone() const noexcept { return {storage_.get(), portion_}; }
|
||||
|
||||
iterator begin() const noexcept { return portion_.begin(); }
|
||||
const_iterator cbegin() const noexcept { return portion_.begin(); }
|
||||
|
||||
iterator end() const noexcept { return portion_.end(); }
|
||||
const_iterator cend() const noexcept { return portion_.end(); }
|
||||
|
||||
bool empty() const noexcept { return storage_ == nullptr; }
|
||||
const std::uint8_t* data() const noexcept { return portion_.data(); }
|
||||
std::size_t size() const noexcept { return portion_.size(); }
|
||||
|
||||
/*! Drop bytes from the beginning of `this` slice.
|
||||
|
||||
\note May invalidate previously retrieved pointers.
|
||||
\post `this->size() = this->size() - std::min(this->size(), max_bytes)`
|
||||
\post `if (this->size() <= max_bytes) this->data() = nullptr`
|
||||
\return Number of bytes removed. */
|
||||
std::size_t remove_prefix(std::size_t max_bytes) noexcept;
|
||||
|
||||
/*! "Take" bytes from the beginning of `this` slice.
|
||||
|
||||
\note May invalidate previously retrieved pointers.
|
||||
\post `this->size() = this->size() - std::min(this->size(), max_bytes)`
|
||||
\post `if (this->size() <= max_bytes) this->data() = nullptr`
|
||||
\return Slice containing the bytes removed from `this` slice. */
|
||||
byte_slice take_slice(std::size_t max_bytes) noexcept;
|
||||
|
||||
/*! Return a shallow (cheap) copy of a slice from `begin` and `end` offsets.
|
||||
|
||||
\throw std::out_of_range If `end < begin`.
|
||||
\throw std::out_of_range If `size() < end`.
|
||||
\return Slice starting at `data() + begin` of size `end - begin`. */
|
||||
byte_slice get_slice(std::size_t begin, std::size_t end) const;
|
||||
};
|
||||
} // epee
|
||||
|
|
@ -45,7 +45,6 @@
|
|||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/asio/ssl.hpp>
|
||||
#include "byte_slice.h"
|
||||
#include "net_utils_base.h"
|
||||
#include "syncobj.h"
|
||||
#include "connection_basic.hpp"
|
||||
|
@ -129,7 +128,7 @@ namespace net_utils
|
|||
|
||||
private:
|
||||
//----------------- i_service_endpoint ---------------------
|
||||
virtual bool do_send(byte_slice message); ///< (see do_send from i_service_endpoint)
|
||||
virtual bool do_send(shared_sv message); ///< (see do_send from i_service_endpoint)
|
||||
virtual bool send_done();
|
||||
virtual bool close();
|
||||
virtual bool call_run_once_service_io();
|
||||
|
@ -138,7 +137,7 @@ namespace net_utils
|
|||
virtual bool add_ref();
|
||||
virtual bool release();
|
||||
//------------------------------------------------------
|
||||
bool do_send_chunk(byte_slice chunk); ///< will send (or queue) a part of data. internal use only
|
||||
bool do_send_chunk(shared_sv chunk); ///< will send (or queue) a part of data. internal use only
|
||||
|
||||
std::shared_ptr<connection<t_protocol_handler> > safe_shared_from_this();
|
||||
bool shutdown();
|
||||
|
|
|
@ -525,7 +525,7 @@ PRAGMA_WARNING_DISABLE_VS(4355)
|
|||
}
|
||||
//---------------------------------------------------------------------------------
|
||||
template<class t_protocol_handler>
|
||||
bool connection<t_protocol_handler>::do_send(byte_slice message) {
|
||||
bool connection<t_protocol_handler>::do_send(shared_sv message) {
|
||||
TRY_ENTRY();
|
||||
|
||||
// Use safe_shared_from_this, because of this is public method and it can be called on the object being deleted
|
||||
|
@ -534,9 +534,6 @@ PRAGMA_WARNING_DISABLE_VS(4355)
|
|||
if (m_was_shutdown) return false;
|
||||
// TODO avoid copy
|
||||
|
||||
std::uint8_t const* const message_data = message.data();
|
||||
const std::size_t message_size = message.size();
|
||||
|
||||
const double factor = 32; // TODO config
|
||||
typedef long long signed int t_safe; // my t_size to avoid any overunderflow in arithmetic
|
||||
const t_safe chunksize_good = (t_safe)( 1024 * std::max(1.0,factor) );
|
||||
|
@ -546,43 +543,26 @@ PRAGMA_WARNING_DISABLE_VS(4355)
|
|||
CHECK_AND_ASSERT_MES(! (chunksize_max<0), false, "Negative chunksize_max" ); // make sure it is unsigned before removin sign with cast:
|
||||
long long unsigned int chunksize_max_unsigned = static_cast<long long unsigned int>( chunksize_max ) ;
|
||||
|
||||
if (allow_split && (message_size > chunksize_max_unsigned)) {
|
||||
{ // LOCK: chunking
|
||||
epee::critical_region_t<decltype(m_chunking_lock)> send_guard(m_chunking_lock); // *** critical ***
|
||||
if (allow_split && (message.size() > chunksize_max_unsigned)) {
|
||||
{ // LOCK: chunking
|
||||
epee::critical_region_t<decltype(m_chunking_lock)> send_guard(m_chunking_lock); // *** critical ***
|
||||
|
||||
MDEBUG("do_send() will SPLIT into small chunks, from packet="<<message_size<<" B for ptr="<<message_data);
|
||||
// 01234567890
|
||||
// ^^^^ (pos=0, len=4) ; pos:=pos+len, pos=4
|
||||
// ^^^^ (pos=4, len=4) ; pos:=pos+len, pos=8
|
||||
// ^^^ (pos=8, len=4) ;
|
||||
MDEBUG("do_send() will SPLIT into small chunks, from packet="<<message.size()<<" B for ptr="<<(void*)message.ptr.get());
|
||||
|
||||
// const size_t bufsize = chunksize_good; // TODO safecast
|
||||
// char* buf = new char[ bufsize ];
|
||||
while (!message.view.empty()) {
|
||||
bool ok = do_send_chunk(message.extract_prefix(chunksize_good));
|
||||
|
||||
bool all_ok = true;
|
||||
while (!message.empty()) {
|
||||
byte_slice chunk = message.take_slice(chunksize_good);
|
||||
|
||||
MDEBUG("chunk_start="<<(void*)chunk.data()<<" ptr="<<message_data<<" pos="<<(chunk.data() - message_data));
|
||||
MDEBUG("part of " << message.size() << ": pos="<<(chunk.data() - message_data) << " len="<<chunk.size());
|
||||
|
||||
bool ok = do_send_chunk(std::move(chunk)); // <====== ***
|
||||
|
||||
all_ok = all_ok && ok;
|
||||
if (!all_ok) {
|
||||
MDEBUG("do_send() DONE ***FAILED*** from packet="<<message_size<<" B for ptr="<<message_data);
|
||||
MDEBUG("do_send() SEND was aborted in middle of big package - this is mostly harmless "
|
||||
<< " (e.g. peer closed connection) but if it causes trouble tell us at #monero-dev. " << message_size);
|
||||
return false; // partial failure in sending
|
||||
}
|
||||
// (in catch block, or uniq pointer) delete buf;
|
||||
} // each chunk
|
||||
|
||||
MDEBUG("do_send() DONE SPLIT from packet="<<message_size<<" B for ptr="<<message_data);
|
||||
if (!ok) {
|
||||
MDEBUG("do_send() DONE ***FAILED***");
|
||||
MDEBUG("do_send() SEND was aborted in middle of big package - this is probably harmless (e.g. peer closed connection)");
|
||||
return false; // partial failure in sending
|
||||
}
|
||||
}
|
||||
|
||||
MDEBUG("do_send() DONE SPLIT send");
|
||||
MDEBUG("do_send() m_connection_type = " << m_connection_type);
|
||||
|
||||
return all_ok; // done - e.g. queued - all the chunks of current do_send call
|
||||
return true; // done - e.g. queued - all the chunks of current do_send call
|
||||
} // LOCK: chunking
|
||||
} // a big block (to be chunked) - all chunks
|
||||
else { // small block
|
||||
|
@ -594,7 +574,7 @@ PRAGMA_WARNING_DISABLE_VS(4355)
|
|||
|
||||
//---------------------------------------------------------------------------------
|
||||
template<class t_protocol_handler>
|
||||
bool connection<t_protocol_handler>::do_send_chunk(byte_slice chunk)
|
||||
bool connection<t_protocol_handler>::do_send_chunk(shared_sv chunk)
|
||||
{
|
||||
TRY_ENTRY();
|
||||
// Use safe_shared_from_this, because of this is public method and it can be called on the object being deleted
|
||||
|
|
|
@ -49,7 +49,7 @@
|
|||
#include <boost/asio.hpp>
|
||||
#include <boost/asio/ssl.hpp>
|
||||
|
||||
#include "byte_slice.h"
|
||||
#include "shared_sv.h"
|
||||
#include "net/net_utils_base.h"
|
||||
#include "net/net_ssl.h"
|
||||
#include "syncobj.h"
|
||||
|
@ -109,7 +109,7 @@ class connection_basic { // not-templated base class for rapid developmet of som
|
|||
std::atomic<bool> m_want_close_connection;
|
||||
std::atomic<bool> m_was_shutdown;
|
||||
critical_section m_send_que_lock;
|
||||
std::deque<byte_slice> m_send_que;
|
||||
std::deque<shared_sv> m_send_que;
|
||||
std::atomic<bool> m_is_multithreaded;
|
||||
/// Strand to ensure the connection's handlers are not called concurrently.
|
||||
boost::asio::io_service::strand strand_;
|
||||
|
|
|
@ -39,6 +39,7 @@
|
|||
#include <string_view>
|
||||
|
||||
#include "net_helper.h"
|
||||
#include "abstract_http_client.h"
|
||||
#include "http_client_base.h"
|
||||
|
||||
#ifdef HTTP_ENABLE_GZIP
|
||||
|
|
|
@ -577,10 +577,10 @@ namespace net_utils
|
|||
|
||||
LOG_PRINT_L3("HTTP_RESPONSE_HEAD: << \r\n" << response_data);
|
||||
|
||||
m_psnd_hndlr->do_send(byte_slice{std::move(response_data)});
|
||||
m_psnd_hndlr->do_send(shared_sv{std::move(response_data)});
|
||||
if (query_info.m_http_method != http::http_method_head)
|
||||
for (auto& body_piece : response.m_body_pieces)
|
||||
m_psnd_hndlr->do_send(byte_slice{std::move(body_piece)});
|
||||
m_psnd_hndlr->do_send(shared_sv{std::move(body_piece)});
|
||||
|
||||
m_psnd_hndlr->send_done();
|
||||
return res;
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
|
||||
#include <cstdint>
|
||||
|
||||
#include "byte_slice.h"
|
||||
#include "shared_sv.h"
|
||||
#include "net_utils_base.h"
|
||||
#include "span.h"
|
||||
|
||||
|
@ -128,14 +128,14 @@ namespace levin
|
|||
bucket_head2 make_header(uint32_t command, uint64_t msg_size, uint32_t flags, bool expect_response) noexcept;
|
||||
|
||||
//! \return A levin notification message.
|
||||
byte_slice make_notify(int command, epee::span<const std::uint8_t> payload);
|
||||
std::string make_notify(int command, epee::span<const std::uint8_t> payload);
|
||||
|
||||
/*! Generate a dummy levin message.
|
||||
|
||||
\param noise_bytes Total size of the returned `byte_slice`.
|
||||
\param noise_bytes Total size of the returned `shared_sv`.
|
||||
\return `nullptr` if `noise_size` is smaller than the levin header.
|
||||
Otherwise, a dummy levin message. */
|
||||
byte_slice make_noise_notify(std::size_t noise_bytes);
|
||||
std::string make_noise_notify(std::size_t noise_bytes);
|
||||
|
||||
/*! Generate 1+ levin messages that are identical to the noise message size.
|
||||
|
||||
|
@ -144,7 +144,7 @@ namespace levin
|
|||
\return `nullptr` if `noise.size()` is less than the levin header size.
|
||||
Otherwise, a levin notification message OR 2+ levin fragment messages.
|
||||
Each message is `noise.size()` in length. */
|
||||
byte_slice make_fragmented_notify(const byte_slice& noise, int command, epee::span<const std::uint8_t> payload);
|
||||
std::string make_fragmented_notify(const std::string_view noise, int command, epee::span<const std::uint8_t> payload);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -159,7 +159,7 @@ namespace levin
|
|||
m_current_head.m_have_to_return_data = false;
|
||||
|
||||
return_buff.insert(0, (const char*)&m_current_head, sizeof(m_current_head));
|
||||
if(!m_psnd_hndlr->do_send(byte_slice{std::move(return_buff)}))
|
||||
if(!m_psnd_hndlr->do_send(shared_sv{std::move(return_buff)}))
|
||||
return false;
|
||||
|
||||
}
|
||||
|
|
|
@ -97,7 +97,7 @@ public:
|
|||
int invoke_async(int command, const epee::span<const uint8_t> in_buff, boost::uuids::uuid connection_id, const callback_t &cb, size_t timeout = LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED);
|
||||
|
||||
int notify(int command, const epee::span<const uint8_t> in_buff, boost::uuids::uuid connection_id);
|
||||
int send(epee::byte_slice message, const boost::uuids::uuid& connection_id);
|
||||
int send(epee::shared_sv message, const boost::uuids::uuid& connection_id);
|
||||
bool close(boost::uuids::uuid connection_id);
|
||||
bool update_connection_context(const t_connection_context& contxt);
|
||||
bool request_callback(boost::uuids::uuid connection_id);
|
||||
|
@ -129,7 +129,11 @@ class async_protocol_handler
|
|||
bool send_message(uint32_t command, epee::span<const uint8_t> in_buff, uint32_t flags, bool expect_response)
|
||||
{
|
||||
const bucket_head2 head = make_header(command, in_buff.size(), flags, expect_response);
|
||||
if(!m_pservice_endpoint->do_send(byte_slice{as_byte_span(head), in_buff}))
|
||||
std::string data;
|
||||
data.reserve(sizeof(head) + in_buff.size());
|
||||
data.append(reinterpret_cast<const char*>(&head), sizeof(head));
|
||||
data.append(reinterpret_cast<const char*>(in_buff.data()), in_buff.size());
|
||||
if(!m_pservice_endpoint->do_send(shared_sv{std::move(data)}))
|
||||
return false;
|
||||
|
||||
MDEBUG(m_connection_context << "LEVIN_PACKET_SENT. [len=" << head.m_cb
|
||||
|
@ -528,7 +532,7 @@ public:
|
|||
head.m_return_code = SWAP32LE(return_code);
|
||||
return_buff.insert(0, reinterpret_cast<const char*>(&head), sizeof(head));
|
||||
|
||||
if(!m_pservice_endpoint->do_send(byte_slice{std::move(return_buff)}))
|
||||
if(!m_pservice_endpoint->do_send(shared_sv{std::move(return_buff)}))
|
||||
return false;
|
||||
|
||||
MDEBUG(m_connection_context << "LEVIN_PACKET_SENT. [len=" << head.m_cb
|
||||
|
@ -745,7 +749,7 @@ public:
|
|||
`make_fragmented_notify`.
|
||||
|
||||
\return 1 on success */
|
||||
int send(byte_slice message)
|
||||
int send(shared_sv message)
|
||||
{
|
||||
const misc_utils::auto_scope_leave_caller scope_exit_handler = misc_utils::create_scope_leave_handler(
|
||||
[this] { return finish_outer_call(); });
|
||||
|
@ -753,7 +757,7 @@ public:
|
|||
if(m_deletion_initiated)
|
||||
return LEVIN_ERROR_CONNECTION_DESTROYED;
|
||||
|
||||
const std::size_t length = message.size();
|
||||
const std::size_t length = message.view.size();
|
||||
if (!m_pservice_endpoint->do_send(std::move(message)))
|
||||
{
|
||||
LOG_ERROR_CC(m_connection_context, "Failed to send message, dropping it");
|
||||
|
@ -941,7 +945,7 @@ int async_protocol_handler_config<t_connection_context>::notify(int command, con
|
|||
}
|
||||
//------------------------------------------------------------------------------------------
|
||||
template<class t_connection_context>
|
||||
int async_protocol_handler_config<t_connection_context>::send(byte_slice message, const boost::uuids::uuid& connection_id)
|
||||
int async_protocol_handler_config<t_connection_context>::send(shared_sv message, const boost::uuids::uuid& connection_id)
|
||||
{
|
||||
async_protocol_handler<t_connection_context>* aph;
|
||||
int r = find_and_lock_connection(connection_id, aph);
|
||||
|
|
|
@ -34,7 +34,7 @@
|
|||
#include <boost/asio/ip/address_v6.hpp>
|
||||
#include <typeinfo>
|
||||
#include <type_traits>
|
||||
#include "byte_slice.h"
|
||||
#include "shared_sv.h"
|
||||
#include "enums.h"
|
||||
#include "misc_log_ex.h"
|
||||
#include "serialization/keyvalue_serialization.h"
|
||||
|
@ -422,7 +422,7 @@ namespace net_utils
|
|||
/************************************************************************/
|
||||
struct i_service_endpoint
|
||||
{
|
||||
virtual bool do_send(byte_slice message)=0;
|
||||
virtual bool do_send(shared_sv message)=0;
|
||||
virtual bool close()=0;
|
||||
virtual bool send_done()=0;
|
||||
virtual bool call_run_once_service_io()=0;
|
||||
|
|
64
contrib/epee/include/shared_sv.h
Normal file
64
contrib/epee/include/shared_sv.h
Normal file
|
@ -0,0 +1,64 @@
|
|||
// Copyright (c) 2020, The Loki Project
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace epee {
|
||||
|
||||
/// Class that holds a shared pointer to a string plus a separate string_view referencing some
|
||||
/// substring of that shared string.
|
||||
struct shared_sv {
|
||||
std::shared_ptr<std::string> ptr;
|
||||
std::string_view view;
|
||||
shared_sv() = default;
|
||||
/// Constructs from a shared_ptr to a string; the view is initialized to refer to the entire string
|
||||
explicit shared_sv(std::shared_ptr<std::string> src_ptr) : ptr{std::move(src_ptr)}, view{*ptr} {}
|
||||
/// Constructs a new shared ptr by moving from a given string rvalue reference
|
||||
explicit shared_sv(std::string&& str) : shared_sv{std::make_shared<std::string>(std::move(str))} {}
|
||||
/// Constructs from a shared_ptr and a view
|
||||
shared_sv(std::shared_ptr<std::string> src_ptr, std::string_view view) : ptr{std::move(src_ptr)}, view{view} {}
|
||||
|
||||
/// Shortcut for obj.view.size()
|
||||
auto size() const { return view.size(); }
|
||||
/// Shortcut for obj.view.data()
|
||||
auto data() const { return view.data(); }
|
||||
|
||||
/// Extracts a view prefix of up to size bytes and returns it in a new shared_sv that shares
|
||||
/// ownership with this shared_sv. The prefix is removed from the view of this.
|
||||
shared_sv extract_prefix(size_t size) {
|
||||
auto prefix_view = view.substr(0, size);
|
||||
view.remove_prefix(prefix_view.size());
|
||||
return {ptr, prefix_view};
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
} // namespace epee
|
|
@ -26,7 +26,7 @@
|
|||
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
add_library(epee byte_slice.cpp hex.cpp abstract_http_client.cpp http_auth.cpp mlog.cpp net_helper.cpp net_utils_base.cpp string_tools.cpp wipeable_string.cpp
|
||||
add_library(epee hex.cpp abstract_http_client.cpp http_auth.cpp mlog.cpp net_helper.cpp net_utils_base.cpp string_tools.cpp wipeable_string.cpp
|
||||
levin_base.cpp memwipe.c connection_basic.cpp network_throttle.cpp network_throttle-detail.cpp mlocker.cpp buffer.cpp net_ssl.cpp portable_storage.cpp
|
||||
time_helper.cpp)
|
||||
|
||||
|
|
|
@ -1,212 +0,0 @@
|
|||
// Copyright (c) 2019, The Monero Project
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
#include "byte_slice.h"
|
||||
|
||||
namespace epee
|
||||
{
|
||||
struct byte_slice_data
|
||||
{
|
||||
byte_slice_data() noexcept
|
||||
: ref_count(1)
|
||||
{}
|
||||
|
||||
virtual ~byte_slice_data() noexcept
|
||||
{}
|
||||
|
||||
std::atomic<std::size_t> ref_count;
|
||||
};
|
||||
|
||||
void release_byte_slice::operator()(byte_slice_data* ptr) const noexcept
|
||||
{
|
||||
if (ptr && --(ptr->ref_count) == 0)
|
||||
{
|
||||
ptr->~byte_slice_data();
|
||||
free(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
template<typename T>
|
||||
struct adapted_byte_slice final : byte_slice_data
|
||||
{
|
||||
explicit adapted_byte_slice(T&& buffer)
|
||||
: byte_slice_data(), buffer(std::move(buffer))
|
||||
{}
|
||||
|
||||
virtual ~adapted_byte_slice() noexcept final override
|
||||
{}
|
||||
|
||||
const T buffer;
|
||||
};
|
||||
|
||||
// bytes "follow" this structure in memory slab
|
||||
struct raw_byte_slice final : byte_slice_data
|
||||
{
|
||||
raw_byte_slice() noexcept
|
||||
: byte_slice_data()
|
||||
{}
|
||||
|
||||
virtual ~raw_byte_slice() noexcept final override
|
||||
{}
|
||||
};
|
||||
|
||||
/* This technique is not-standard, but allows for the reference count and
|
||||
memory for the bytes (when given a list of spans) to be allocated in a
|
||||
single call. In that situation, the dynamic sized bytes are after/behind
|
||||
the raw_byte_slice class. The C runtime has to track the number of bytes
|
||||
allocated regardless, so free'ing is relatively easy. */
|
||||
|
||||
template<typename T, typename... U>
|
||||
std::unique_ptr<T, release_byte_slice> allocate_slice(std::size_t extra_bytes, U&&... args)
|
||||
{
|
||||
if (std::numeric_limits<std::size_t>::max() - sizeof(T) < extra_bytes)
|
||||
throw std::bad_alloc{};
|
||||
|
||||
void* const ptr = malloc(sizeof(T) + extra_bytes);
|
||||
if (ptr == nullptr)
|
||||
throw std::bad_alloc{};
|
||||
|
||||
try
|
||||
{
|
||||
new (ptr) T{std::forward<U>(args)...};
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
free(ptr);
|
||||
throw;
|
||||
}
|
||||
return std::unique_ptr<T, release_byte_slice>{reinterpret_cast<T*>(ptr)};
|
||||
}
|
||||
} // anonymous
|
||||
|
||||
byte_slice::byte_slice(byte_slice_data* storage, span<const std::uint8_t> portion) noexcept
|
||||
: storage_(storage), portion_(portion)
|
||||
{
|
||||
if (storage_)
|
||||
++(storage_->ref_count);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
byte_slice::byte_slice(const adapt_buffer, T&& buffer)
|
||||
: storage_(nullptr), portion_(nullptr)
|
||||
{
|
||||
if (!buffer.empty())
|
||||
{
|
||||
storage_ = allocate_slice<adapted_byte_slice<T>>(0, std::move(buffer));
|
||||
portion_ = to_byte_span(to_span(static_cast<adapted_byte_slice<T> *>(storage_.get())->buffer));
|
||||
}
|
||||
}
|
||||
|
||||
byte_slice::byte_slice(std::initializer_list<span<const std::uint8_t>> sources)
|
||||
: byte_slice()
|
||||
{
|
||||
std::size_t space_needed = 0;
|
||||
for (const auto source : sources)
|
||||
space_needed += source.size();
|
||||
|
||||
if (space_needed)
|
||||
{
|
||||
auto storage = allocate_slice<raw_byte_slice>(space_needed);
|
||||
span<std::uint8_t> out{reinterpret_cast<std::uint8_t*>(storage.get() + 1), space_needed};
|
||||
portion_ = {out.data(), out.size()};
|
||||
|
||||
for (const auto source : sources)
|
||||
{
|
||||
std::memcpy(out.data(), source.data(), source.size());
|
||||
if (out.remove_prefix(source.size()) < source.size())
|
||||
throw std::bad_alloc{}; // size_t overflow on space_needed
|
||||
}
|
||||
storage_ = std::move(storage);
|
||||
}
|
||||
}
|
||||
|
||||
byte_slice::byte_slice(std::string&& buffer)
|
||||
: byte_slice(adapt_buffer{}, std::move(buffer))
|
||||
{}
|
||||
|
||||
byte_slice::byte_slice(std::vector<std::uint8_t>&& buffer)
|
||||
: byte_slice(adapt_buffer{}, std::move(buffer))
|
||||
{}
|
||||
|
||||
byte_slice::byte_slice(byte_slice&& source) noexcept
|
||||
: storage_(std::move(source.storage_)), portion_(source.portion_)
|
||||
{
|
||||
source.portion_ = epee::span<const std::uint8_t>{};
|
||||
}
|
||||
|
||||
byte_slice& byte_slice::operator=(byte_slice&& source) noexcept
|
||||
{
|
||||
storage_ = std::move(source.storage_);
|
||||
portion_ = source.portion_;
|
||||
if (source.storage_ == nullptr)
|
||||
source.portion_ = epee::span<const std::uint8_t>{};
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::size_t byte_slice::remove_prefix(std::size_t max_bytes) noexcept
|
||||
{
|
||||
max_bytes = portion_.remove_prefix(max_bytes);
|
||||
if (portion_.empty())
|
||||
storage_ = nullptr;
|
||||
return max_bytes;
|
||||
}
|
||||
|
||||
byte_slice byte_slice::take_slice(const std::size_t max_bytes) noexcept
|
||||
{
|
||||
byte_slice out{};
|
||||
std::uint8_t const* const ptr = data();
|
||||
out.portion_ = {ptr, portion_.remove_prefix(max_bytes)};
|
||||
|
||||
if (portion_.empty())
|
||||
out.storage_ = std::move(storage_); // no atomic inc/dec
|
||||
else
|
||||
out = {storage_.get(), out.portion_};
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
byte_slice byte_slice::get_slice(const std::size_t begin, const std::size_t end) const
|
||||
{
|
||||
if (end < begin || portion_.size() < end)
|
||||
throw std::out_of_range{"bad slice range"};
|
||||
|
||||
if (begin == end)
|
||||
return {};
|
||||
return {storage_.get(), {portion_.begin() + begin, end - begin}};
|
||||
}
|
||||
} // epee
|
|
@ -47,62 +47,69 @@ namespace levin
|
|||
return head;
|
||||
}
|
||||
|
||||
byte_slice make_notify(int command, epee::span<const std::uint8_t> payload)
|
||||
std::string make_notify(int command, epee::span<const std::uint8_t> payload)
|
||||
{
|
||||
const bucket_head2 head = make_header(command, payload.size(), LEVIN_PACKET_REQUEST, false);
|
||||
return byte_slice{epee::as_byte_span(head), payload};
|
||||
std::string result;
|
||||
result.reserve(sizeof(head) + payload.size());
|
||||
result.append(reinterpret_cast<const char*>(&head), sizeof(head));
|
||||
result.append(reinterpret_cast<const char*>(payload.data()), payload.size());
|
||||
return result;
|
||||
}
|
||||
|
||||
byte_slice make_noise_notify(const std::size_t noise_bytes)
|
||||
std::string make_noise_notify(const std::size_t noise_bytes)
|
||||
{
|
||||
static constexpr const std::uint32_t flags =
|
||||
LEVIN_PACKET_BEGIN | LEVIN_PACKET_END;
|
||||
|
||||
if (noise_bytes < sizeof(bucket_head2))
|
||||
return nullptr;
|
||||
return ""s;
|
||||
|
||||
std::string buffer(noise_bytes, char(0));
|
||||
const bucket_head2 head = make_header(0, noise_bytes - sizeof(bucket_head2), flags, false);
|
||||
std::memcpy(std::addressof(buffer[0]), std::addressof(head), sizeof(head));
|
||||
|
||||
return byte_slice{std::move(buffer)};
|
||||
return buffer;
|
||||
}
|
||||
|
||||
byte_slice make_fragmented_notify(const byte_slice& noise_message, int command, epee::span<const std::uint8_t> payload)
|
||||
std::string make_fragmented_notify(const std::string_view noise_message, int command, epee::span<const std::uint8_t> payload)
|
||||
{
|
||||
std::string result;
|
||||
const size_t noise_size = noise_message.size();
|
||||
if (noise_size < sizeof(bucket_head2) * 2)
|
||||
return nullptr;
|
||||
return result;
|
||||
|
||||
if (payload.size() <= noise_size - sizeof(bucket_head2))
|
||||
{
|
||||
/* The entire message can be sent at once, and the levin binary parser
|
||||
will ignore extra bytes. So just pad with zeroes and otherwise send
|
||||
a "normal", not fragmented message. */
|
||||
const size_t padding = noise_size - sizeof(bucket_head2) - payload.size();
|
||||
const span<const uint8_t> padding_bytes{noise_message.end() - padding, padding};
|
||||
auto padding = noise_message.substr(sizeof(bucket_head2) + payload.size());
|
||||
|
||||
const bucket_head2 head = make_header(command, noise_size - sizeof(bucket_head2), LEVIN_PACKET_REQUEST, false);
|
||||
return byte_slice{as_byte_span(head), payload, padding_bytes};
|
||||
result.reserve(sizeof(head) + payload.size() + padding.size());
|
||||
result.append(reinterpret_cast<const char*>(&head), sizeof(head));
|
||||
result.append(reinterpret_cast<const char*>(payload.data()), payload.size());
|
||||
result.append(padding);
|
||||
return result;
|
||||
}
|
||||
|
||||
// fragment message
|
||||
const size_t payload_space = noise_size - sizeof(bucket_head2);
|
||||
const size_t expected_fragments = ((payload.size() - 2) / payload_space) + 1;
|
||||
|
||||
std::string buffer{};
|
||||
buffer.reserve((expected_fragments + 1) * noise_size); // +1 here overselects for internal bucket_head2 value
|
||||
result.reserve((expected_fragments + 1) * noise_size); // +1 here overselects for internal bucket_head2 value
|
||||
|
||||
bucket_head2 head = make_header(0, noise_size - sizeof(bucket_head2), LEVIN_PACKET_BEGIN, false);
|
||||
buffer.append(reinterpret_cast<const char*>(&head), sizeof(head));
|
||||
result.append(reinterpret_cast<const char*>(&head), sizeof(head));
|
||||
|
||||
head.m_command = command;
|
||||
head.m_flags = LEVIN_PACKET_REQUEST;
|
||||
head.m_cb = payload.size();
|
||||
buffer.append(reinterpret_cast<const char*>(&head), sizeof(head));
|
||||
result.append(reinterpret_cast<const char*>(&head), sizeof(head));
|
||||
|
||||
size_t copy_size = payload.remove_prefix(payload_space - sizeof(bucket_head2));
|
||||
buffer.append(reinterpret_cast<const char*>(payload.data()) - copy_size, copy_size);
|
||||
result.append(reinterpret_cast<const char*>(payload.data()) - copy_size, copy_size);
|
||||
|
||||
head.m_command = 0;
|
||||
head.m_flags = 0;
|
||||
|
@ -115,14 +122,13 @@ namespace levin
|
|||
if (payload.empty())
|
||||
head.m_flags = LEVIN_PACKET_END;
|
||||
|
||||
buffer.append(reinterpret_cast<const char*>(&head), sizeof(head));
|
||||
buffer.append(reinterpret_cast<const char*>(payload.data()) - copy_size, copy_size);
|
||||
result.append(reinterpret_cast<const char*>(&head), sizeof(head));
|
||||
result.append(reinterpret_cast<const char*>(payload.data()) - copy_size, copy_size);
|
||||
}
|
||||
|
||||
const size_t padding = noise_size - copy_size - sizeof(bucket_head2);
|
||||
buffer.append(reinterpret_cast<const char*>(noise_message.end()) - padding, padding);
|
||||
result.append(noise_message.substr(copy_size + sizeof(bucket_head2)));
|
||||
|
||||
return byte_slice{std::move(buffer)};
|
||||
return result;
|
||||
}
|
||||
} // levin
|
||||
} // epee
|
||||
|
|
|
@ -165,8 +165,7 @@ namespace levin
|
|||
struct noise_channel
|
||||
{
|
||||
explicit noise_channel(boost::asio::io_service& io_service)
|
||||
: active(nullptr),
|
||||
queue(),
|
||||
: queue(),
|
||||
strand(io_service),
|
||||
next_noise(io_service),
|
||||
connection(boost::uuids::nil_uuid())
|
||||
|
@ -178,8 +177,8 @@ namespace levin
|
|||
|
||||
// Only read/write these values "inside the strand"
|
||||
|
||||
epee::byte_slice active;
|
||||
std::deque<epee::byte_slice> queue;
|
||||
epee::shared_sv active;
|
||||
std::deque<epee::shared_sv> queue;
|
||||
boost::asio::io_service::strand strand;
|
||||
boost::asio::steady_timer next_noise;
|
||||
boost::uuids::uuid connection;
|
||||
|
@ -190,7 +189,7 @@ namespace levin
|
|||
{
|
||||
struct zone
|
||||
{
|
||||
explicit zone(boost::asio::io_service& io_service, std::shared_ptr<connections> p2p, epee::byte_slice noise_in, bool is_public)
|
||||
explicit zone(boost::asio::io_service& io_service, std::shared_ptr<connections> p2p, epee::shared_sv noise_in, bool is_public)
|
||||
: p2p(std::move(p2p)),
|
||||
noise(std::move(noise_in)),
|
||||
next_epoch(io_service),
|
||||
|
@ -200,12 +199,12 @@ namespace levin
|
|||
connection_count(0),
|
||||
is_public(is_public)
|
||||
{
|
||||
for (std::size_t count = 0; !noise.empty() && count < CRYPTONOTE_NOISE_CHANNELS; ++count)
|
||||
for (std::size_t count = 0; !noise.view.empty() && count < CRYPTONOTE_NOISE_CHANNELS; ++count)
|
||||
channels.emplace_back(io_service);
|
||||
}
|
||||
|
||||
const std::shared_ptr<connections> p2p;
|
||||
const epee::byte_slice noise; //!< `!empty()` means zone is using noise channels
|
||||
const epee::shared_sv noise; //!< `!empty()` means zone is using noise channels
|
||||
boost::asio::steady_timer next_epoch;
|
||||
boost::asio::io_service::strand strand;
|
||||
net::dandelionpp::connection_map map;//!< Tracks outgoing uuid's for noise channels or Dandelion++ stems
|
||||
|
@ -221,17 +220,17 @@ namespace levin
|
|||
class queue_covert_notify
|
||||
{
|
||||
std::shared_ptr<detail::zone> zone_;
|
||||
epee::byte_slice message_; // Requires manual copy constructor
|
||||
epee::shared_sv message_; // Requires manual copy constructor
|
||||
const std::size_t destination_;
|
||||
|
||||
public:
|
||||
queue_covert_notify(std::shared_ptr<detail::zone> zone, epee::byte_slice message, std::size_t destination)
|
||||
queue_covert_notify(std::shared_ptr<detail::zone> zone, epee::shared_sv message, std::size_t destination)
|
||||
: zone_(std::move(zone)), message_(std::move(message)), destination_(destination)
|
||||
{}
|
||||
|
||||
queue_covert_notify(queue_covert_notify&&) = default;
|
||||
queue_covert_notify(const queue_covert_notify& source)
|
||||
: zone_(source.zone_), message_(source.message_.clone()), destination_(source.destination_)
|
||||
: zone_(source.zone_), message_(source.message_), destination_(source.destination_)
|
||||
{}
|
||||
|
||||
//! \pre Called within `zone_->channels[destionation_].strand`.
|
||||
|
@ -254,17 +253,17 @@ namespace levin
|
|||
class flood_notify
|
||||
{
|
||||
std::shared_ptr<detail::zone> zone_;
|
||||
epee::byte_slice message_; // Requires manual copy
|
||||
epee::shared_sv message_; // Requires manual copy
|
||||
boost::uuids::uuid source_;
|
||||
|
||||
public:
|
||||
explicit flood_notify(std::shared_ptr<detail::zone> zone, epee::byte_slice message, const boost::uuids::uuid& source)
|
||||
: zone_(std::move(zone)), message_(message.clone()), source_(source)
|
||||
explicit flood_notify(std::shared_ptr<detail::zone> zone, epee::shared_sv message, const boost::uuids::uuid& source)
|
||||
: zone_(std::move(zone)), message_(message), source_(source)
|
||||
{}
|
||||
|
||||
flood_notify(flood_notify&&) = default;
|
||||
flood_notify(const flood_notify& source)
|
||||
: zone_(source.zone_), message_(source.message_.clone()), source_(source.source_)
|
||||
: zone_(source.zone_), message_(source.message_), source_(source.source_)
|
||||
{}
|
||||
|
||||
void operator()() const
|
||||
|
@ -292,7 +291,7 @@ namespace levin
|
|||
});
|
||||
|
||||
for (const boost::uuids::uuid& connection : connections)
|
||||
zone_->p2p->send(message_.clone(), connection);
|
||||
zone_->p2p->send(message_, connection);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -322,7 +321,7 @@ namespace levin
|
|||
notify (tx) instead of dummy noise. */
|
||||
|
||||
channel.connection = connection_;
|
||||
channel.active = nullptr;
|
||||
channel.active = {};
|
||||
|
||||
if (connection_.is_nil())
|
||||
channel.queue.clear();
|
||||
|
@ -413,7 +412,7 @@ namespace levin
|
|||
//! \pre Called within `zone_->channels[channel_].strand`.
|
||||
void operator()(boost::system::error_code error)
|
||||
{
|
||||
if (!zone_ || !zone_->p2p || zone_->noise.empty())
|
||||
if (!zone_ || !zone_->p2p || zone_->noise.view.empty())
|
||||
return;
|
||||
|
||||
if (error && error != boost::system::errc::operation_canceled)
|
||||
|
@ -426,25 +425,25 @@ namespace levin
|
|||
|
||||
if (!channel.connection.is_nil())
|
||||
{
|
||||
epee::byte_slice message = nullptr;
|
||||
if (!channel.active.empty())
|
||||
message = channel.active.take_slice(zone_->noise.size());
|
||||
epee::shared_sv message;
|
||||
if (!channel.active.view.empty())
|
||||
message = channel.active.extract_prefix(zone_->noise.size());
|
||||
else if (!channel.queue.empty())
|
||||
{
|
||||
channel.active = channel.queue.front().clone();
|
||||
message = channel.active.take_slice(zone_->noise.size());
|
||||
channel.active = channel.queue.front();
|
||||
message = channel.active.extract_prefix(zone_->noise.size());
|
||||
}
|
||||
else
|
||||
message = zone_->noise.clone();
|
||||
message = zone_->noise;
|
||||
|
||||
if (zone_->p2p->send(std::move(message), channel.connection))
|
||||
{
|
||||
if (!channel.queue.empty() && channel.active.empty())
|
||||
if (!channel.queue.empty() && channel.active.view.empty())
|
||||
channel.queue.pop_front();
|
||||
}
|
||||
else
|
||||
{
|
||||
channel.active = nullptr;
|
||||
channel.active = {};
|
||||
channel.connection = boost::uuids::nil_uuid();
|
||||
|
||||
auto connections = get_out_connections(*zone_->p2p);
|
||||
|
@ -489,13 +488,13 @@ namespace levin
|
|||
};
|
||||
} // anonymous
|
||||
|
||||
notify::notify(boost::asio::io_service& service, std::shared_ptr<connections> p2p, epee::byte_slice noise, bool is_public)
|
||||
notify::notify(boost::asio::io_service& service, std::shared_ptr<connections> p2p, epee::shared_sv noise, bool is_public)
|
||||
: zone_(std::make_shared<detail::zone>(service, std::move(p2p), std::move(noise), is_public))
|
||||
{
|
||||
if (!zone_->p2p)
|
||||
throw std::logic_error{"cryptonote::levin::notify cannot have nullptr p2p argument"};
|
||||
|
||||
if (!zone_->noise.empty())
|
||||
if (!zone_->noise.view.empty())
|
||||
{
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
start_epoch{zone_, noise_min_epoch, noise_epoch_range, CRYPTONOTE_NOISE_CHANNELS}();
|
||||
|
@ -512,12 +511,12 @@ namespace levin
|
|||
if (!zone_)
|
||||
return {false, false};
|
||||
|
||||
return {!zone_->noise.empty(), CRYPTONOTE_NOISE_CHANNELS <= zone_->connection_count};
|
||||
return {!zone_->noise.view.empty(), CRYPTONOTE_NOISE_CHANNELS <= zone_->connection_count};
|
||||
}
|
||||
|
||||
void notify::new_out_connection()
|
||||
{
|
||||
if (!zone_ || zone_->noise.empty() || CRYPTONOTE_NOISE_CHANNELS <= zone_->connection_count)
|
||||
if (!zone_ || zone_->noise.view.empty() || CRYPTONOTE_NOISE_CHANNELS <= zone_->connection_count)
|
||||
return;
|
||||
|
||||
zone_->strand.dispatch(
|
||||
|
@ -549,7 +548,7 @@ namespace levin
|
|||
if (zone_->is_public)
|
||||
std::sort(txs.begin(), txs.end()); // don't leak receive order
|
||||
|
||||
if (!zone_->noise.empty() && !zone_->channels.empty())
|
||||
if (!zone_->noise.view.empty() && !zone_->channels.empty())
|
||||
{
|
||||
// covert send in "noise" channel
|
||||
static_assert(
|
||||
|
@ -558,9 +557,9 @@ namespace levin
|
|||
|
||||
// padding is not useful when using noise mode
|
||||
const std::string payload = make_tx_payload(std::move(txs), false);
|
||||
epee::byte_slice message = epee::levin::make_fragmented_notify(
|
||||
zone_->noise, NOTIFY_NEW_TRANSACTIONS::ID, epee::strspan<std::uint8_t>(payload)
|
||||
);
|
||||
epee::shared_sv message{epee::levin::make_fragmented_notify(
|
||||
zone_->noise.view, NOTIFY_NEW_TRANSACTIONS::ID, epee::strspan<std::uint8_t>(payload)
|
||||
)};
|
||||
if (CRYPTONOTE_MAX_FRAGMENTS * zone_->noise.size() < message.size())
|
||||
{
|
||||
MERROR("notify::send_txs provided message exceeding covert fragment size");
|
||||
|
@ -570,15 +569,15 @@ namespace levin
|
|||
for (std::size_t channel = 0; channel < zone_->channels.size(); ++channel)
|
||||
{
|
||||
zone_->channels[channel].strand.dispatch(
|
||||
queue_covert_notify{zone_, message.clone(), channel}
|
||||
queue_covert_notify{zone_, message, channel}
|
||||
);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const std::string payload = make_tx_payload(std::move(txs), pad_txs);
|
||||
epee::byte_slice message =
|
||||
epee::levin::make_notify(NOTIFY_NEW_TRANSACTIONS::ID, epee::strspan<std::uint8_t>(payload));
|
||||
epee::shared_sv message{
|
||||
epee::levin::make_notify(NOTIFY_NEW_TRANSACTIONS::ID, epee::strspan<std::uint8_t>(payload))};
|
||||
|
||||
// traditional monero send technique
|
||||
zone_->strand.dispatch(flood_notify{zone_, std::move(message), source});
|
||||
|
|
|
@ -33,7 +33,7 @@
|
|||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "byte_slice.h"
|
||||
#include "shared_sv.h"
|
||||
#include "cryptonote_basic/blobdatatype.h"
|
||||
#include "net/enums.h"
|
||||
#include "span.h"
|
||||
|
@ -86,7 +86,7 @@ namespace levin
|
|||
{}
|
||||
|
||||
//! Construct an instance with available notification `zones`.
|
||||
explicit notify(boost::asio::io_service& service, std::shared_ptr<connections> p2p, epee::byte_slice noise, bool is_public);
|
||||
explicit notify(boost::asio::io_service& service, std::shared_ptr<connections> p2p, epee::shared_sv noise, bool is_public);
|
||||
|
||||
notify(const notify&) = delete;
|
||||
notify(notify&&) = default;
|
||||
|
|
|
@ -387,7 +387,7 @@ namespace nodetool
|
|||
m_use_ipv6 = command_line::get_arg(vm, arg_p2p_use_ipv6);
|
||||
m_require_ipv4 = !command_line::get_arg(vm, arg_p2p_ignore_ipv4);
|
||||
public_zone.m_notifier = cryptonote::levin::notify{
|
||||
public_zone.m_net_server.get_io_service(), public_zone.m_net_server.get_config_shared(), nullptr, true
|
||||
public_zone.m_net_server.get_io_service(), public_zone.m_net_server.get_config_shared(), {}, true
|
||||
};
|
||||
|
||||
if (command_line::has_arg(vm, arg_p2p_add_peer))
|
||||
|
@ -470,7 +470,7 @@ namespace nodetool
|
|||
return false;
|
||||
|
||||
|
||||
epee::byte_slice noise = nullptr;
|
||||
epee::shared_sv noise;
|
||||
auto proxies = get_proxies(vm);
|
||||
if (!proxies)
|
||||
return false;
|
||||
|
@ -489,14 +489,14 @@ namespace nodetool
|
|||
if (!set_max_out_peers(zone, proxy.max_connections))
|
||||
return false;
|
||||
|
||||
epee::byte_slice this_noise = nullptr;
|
||||
epee::shared_sv this_noise;
|
||||
if (proxy.noise)
|
||||
{
|
||||
static_assert(sizeof(epee::levin::bucket_head2) < CRYPTONOTE_NOISE_BYTES, "noise bytes too small");
|
||||
if (noise.empty())
|
||||
noise = epee::levin::make_noise_notify(CRYPTONOTE_NOISE_BYTES);
|
||||
if (noise.view.empty())
|
||||
noise = epee::shared_sv{epee::levin::make_noise_notify(CRYPTONOTE_NOISE_BYTES)};
|
||||
|
||||
this_noise = noise.clone();
|
||||
this_noise = noise;
|
||||
}
|
||||
|
||||
zone.m_notifier = cryptonote::levin::notify{
|
||||
|
|
|
@ -149,7 +149,7 @@ namespace
|
|||
}
|
||||
|
||||
// Implement epee::net_utils::i_service_endpoint interface
|
||||
virtual bool do_send(epee::byte_slice message)
|
||||
virtual bool do_send(epee::shared_sv message)
|
||||
{
|
||||
m_send_counter.inc();
|
||||
std::unique_lock lock{m_mutex};
|
||||
|
|
|
@ -139,7 +139,7 @@ namespace
|
|||
}
|
||||
|
||||
// Implement epee::net_utils::i_service_endpoint interface
|
||||
virtual bool do_send(epee::byte_slice message)
|
||||
virtual bool do_send(epee::shared_sv message)
|
||||
{
|
||||
//std::cout << "test_connection::do_send()" << std::endl;
|
||||
m_send_counter.inc();
|
||||
|
@ -430,8 +430,8 @@ TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_process
|
|||
const int expected_command = 4673261;
|
||||
const std::string in_data(256, 'e');
|
||||
|
||||
const epee::byte_slice noise = epee::levin::make_noise_notify(1024);
|
||||
const epee::byte_slice notify = epee::levin::make_notify(expected_command, epee::strspan<std::uint8_t>(in_data));
|
||||
const epee::shared_sv noise{epee::levin::make_noise_notify(1024)};
|
||||
const epee::shared_sv notify{epee::levin::make_notify(expected_command, epee::strspan<std::uint8_t>(in_data))};
|
||||
|
||||
test_connection_ptr conn = create_connection();
|
||||
|
||||
|
@ -466,16 +466,16 @@ TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_process
|
|||
const std::string in_data(256, 'e');
|
||||
std::string in_fragmented_data(1024 * 4, 'c');
|
||||
|
||||
const epee::byte_slice noise = epee::levin::make_noise_notify(1024);
|
||||
const epee::byte_slice notify = epee::levin::make_notify(expected_command, epee::strspan<std::uint8_t>(in_data));
|
||||
epee::byte_slice fragmented = epee::levin::make_fragmented_notify(noise, expected_fragmented_command, epee::strspan<std::uint8_t>(in_fragmented_data));
|
||||
const epee::shared_sv noise{epee::levin::make_noise_notify(1024)};
|
||||
const epee::shared_sv notify{epee::levin::make_notify(expected_command, epee::strspan<std::uint8_t>(in_data))};
|
||||
epee::shared_sv fragmented{epee::levin::make_fragmented_notify(noise.view, expected_fragmented_command, epee::strspan<std::uint8_t>(in_fragmented_data))};
|
||||
|
||||
EXPECT_EQ(5u, fragmented.size() / 1024);
|
||||
EXPECT_EQ(0u, fragmented.size() % 1024);
|
||||
|
||||
test_connection_ptr conn = create_connection();
|
||||
|
||||
while (!fragmented.empty())
|
||||
while (!fragmented.view.empty())
|
||||
{
|
||||
if ((fragmented.size() / 1024) % 2 == 1)
|
||||
{
|
||||
|
@ -489,7 +489,7 @@ TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_process
|
|||
ASSERT_EQ(0u, conn->send_counter());
|
||||
ASSERT_TRUE(conn->last_send_data().empty());
|
||||
|
||||
epee::byte_slice next = fragmented.take_slice(1024);
|
||||
epee::shared_sv next = fragmented.extract_prefix(1024);
|
||||
ASSERT_TRUE(conn->m_protocol_handler.handle_recv(next.data(), next.size()));
|
||||
}
|
||||
|
||||
|
|
|
@ -36,6 +36,7 @@
|
|||
#include <iterator>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#ifndef _WIN32
|
||||
|
@ -44,7 +45,7 @@
|
|||
|
||||
#include "boost/archive/portable_binary_iarchive.hpp"
|
||||
#include "boost/archive/portable_binary_oarchive.hpp"
|
||||
#include "byte_slice.h"
|
||||
#include "shared_sv.h"
|
||||
#include "crypto/crypto.h"
|
||||
#include "hex.h"
|
||||
#include "net/net_utils_base.h"
|
||||
|
@ -377,436 +378,46 @@ TEST(Span, ToMutSpan)
|
|||
EXPECT_EQ((std::vector<unsigned>{1, 2, 3, 4}), mut);
|
||||
}
|
||||
|
||||
TEST(ByteSlice, Construction)
|
||||
static_assert(std::is_default_constructible_v<epee::shared_sv>);
|
||||
static_assert(std::is_move_constructible_v<epee::shared_sv>);
|
||||
static_assert(std::is_copy_constructible_v<epee::shared_sv>);
|
||||
static_assert(std::is_move_assignable_v<epee::shared_sv>);
|
||||
static_assert(std::is_copy_assignable_v<epee::shared_sv>);
|
||||
static_assert(std::is_nothrow_default_constructible_v<epee::shared_sv>);
|
||||
static_assert(std::is_nothrow_move_constructible_v<epee::shared_sv>);
|
||||
static_assert(std::is_nothrow_move_assignable_v<epee::shared_sv>);
|
||||
|
||||
TEST(SharedSV, Tests)
|
||||
{
|
||||
EXPECT_TRUE(std::is_default_constructible<epee::byte_slice>());
|
||||
EXPECT_TRUE(std::is_move_constructible<epee::byte_slice>());
|
||||
EXPECT_FALSE(std::is_copy_constructible<epee::byte_slice>());
|
||||
EXPECT_TRUE(std::is_move_assignable<epee::byte_slice>());
|
||||
EXPECT_FALSE(std::is_copy_assignable<epee::byte_slice>());
|
||||
}
|
||||
|
||||
TEST(ByteSlice, NoExcept)
|
||||
{
|
||||
EXPECT_TRUE(std::is_nothrow_default_constructible<epee::byte_slice>());
|
||||
EXPECT_TRUE(std::is_nothrow_move_constructible<epee::byte_slice>());
|
||||
EXPECT_TRUE(std::is_nothrow_move_assignable<epee::byte_slice>());
|
||||
|
||||
epee::byte_slice lvalue{};
|
||||
const epee::byte_slice clvalue{};
|
||||
|
||||
EXPECT_TRUE(noexcept(lvalue.clone()));
|
||||
EXPECT_TRUE(noexcept(clvalue.clone()));
|
||||
|
||||
EXPECT_TRUE(noexcept(lvalue.begin()));
|
||||
EXPECT_TRUE(noexcept(clvalue.begin()));
|
||||
EXPECT_TRUE(noexcept(lvalue.end()));
|
||||
EXPECT_TRUE(noexcept(clvalue.end()));
|
||||
|
||||
EXPECT_TRUE(noexcept(lvalue.cbegin()));
|
||||
EXPECT_TRUE(noexcept(clvalue.cbegin()));
|
||||
EXPECT_TRUE(noexcept(lvalue.cend()));
|
||||
EXPECT_TRUE(noexcept(clvalue.cend()));
|
||||
|
||||
EXPECT_TRUE(noexcept(lvalue.empty()));
|
||||
EXPECT_TRUE(noexcept(clvalue.empty()));
|
||||
|
||||
EXPECT_TRUE(noexcept(lvalue.data()));
|
||||
EXPECT_TRUE(noexcept(clvalue.data()));
|
||||
EXPECT_TRUE(noexcept(lvalue.size()));
|
||||
EXPECT_TRUE(noexcept(clvalue.size()));
|
||||
|
||||
EXPECT_TRUE(noexcept(lvalue.remove_prefix(0)));
|
||||
EXPECT_TRUE(noexcept(lvalue.take_slice(0)));
|
||||
}
|
||||
|
||||
TEST(ByteSlice, Empty)
|
||||
{
|
||||
epee::byte_slice slice{};
|
||||
|
||||
EXPECT_EQ(slice.begin(), slice.end());
|
||||
EXPECT_EQ(slice.begin(), slice.cbegin());
|
||||
EXPECT_EQ(slice.end(), slice.cend());
|
||||
|
||||
EXPECT_TRUE(slice.empty());
|
||||
EXPECT_EQ(0u, slice.size());
|
||||
EXPECT_EQ(slice.begin(), slice.data());
|
||||
|
||||
EXPECT_EQ(0u, slice.get_slice(0, 0).size());
|
||||
EXPECT_THROW(slice.get_slice(0, 1), std::out_of_range);
|
||||
EXPECT_EQ(0u, slice.remove_prefix(1));
|
||||
EXPECT_EQ(0u, slice.take_slice(1).size());
|
||||
}
|
||||
|
||||
TEST(ByteSlice, CopySpans)
|
||||
{
|
||||
const epee::span<const std::uint8_t> part1 = epee::as_byte_span("this is part1");
|
||||
const epee::span<const std::uint8_t> part2 = epee::as_byte_span("then part2");
|
||||
const epee::span<const std::uint8_t> part3 = epee::as_byte_span("finally part3");
|
||||
|
||||
const epee::byte_slice slice{part1, part2, part3};
|
||||
|
||||
EXPECT_NE(nullptr, slice.begin());
|
||||
EXPECT_NE(nullptr, slice.end());
|
||||
EXPECT_NE(slice.begin(), slice.end());
|
||||
EXPECT_NE(slice.cbegin(), slice.cend());
|
||||
EXPECT_EQ(slice.begin(), slice.cbegin());
|
||||
EXPECT_EQ(slice.end(), slice.cend());
|
||||
ASSERT_EQ(slice.size(), std::size_t(slice.end() - slice.begin()));
|
||||
|
||||
EXPECT_FALSE(slice.empty());
|
||||
EXPECT_EQ(slice.begin(), slice.data());
|
||||
ASSERT_EQ(part1.size() + part2.size() + part3.size(), slice.size());
|
||||
EXPECT_TRUE(
|
||||
boost::range::equal(
|
||||
part1, boost::make_iterator_range(slice.begin(), slice.begin() + part1.size())
|
||||
)
|
||||
);
|
||||
EXPECT_TRUE(
|
||||
boost::range::equal(
|
||||
part2, boost::make_iterator_range(slice.begin() + part1.size(), slice.end() - part3.size())
|
||||
)
|
||||
);
|
||||
EXPECT_TRUE(
|
||||
boost::range::equal(
|
||||
part3, boost::make_iterator_range(slice.end() - part3.size(), slice.end())
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
TEST(ByteSlice, AdaptString)
|
||||
{
|
||||
static constexpr const char base_string[] = "this is an example message";
|
||||
std::string adapted = base_string;
|
||||
|
||||
const epee::span<const uint8_t> original = epee::to_byte_span(epee::to_span(adapted));
|
||||
const epee::byte_slice slice{std::move(adapted)};
|
||||
|
||||
EXPECT_EQ(original.begin(), slice.begin());
|
||||
EXPECT_EQ(original.cbegin(), slice.cbegin());
|
||||
EXPECT_EQ(original.end(), slice.end());
|
||||
EXPECT_EQ(original.cend(), slice.cend());
|
||||
|
||||
EXPECT_FALSE(slice.empty());
|
||||
EXPECT_EQ(original.data(), slice.data());
|
||||
EXPECT_EQ(original.size(), slice.size());
|
||||
EXPECT_TRUE(boost::range::equal(boost::string_ref{base_string}, slice));
|
||||
}
|
||||
|
||||
TEST(ByteSlice, EmptyAdaptString)
|
||||
{
|
||||
epee::byte_slice slice{std::string{}};
|
||||
|
||||
EXPECT_EQ(slice.begin(), slice.end());
|
||||
EXPECT_EQ(slice.begin(), slice.cbegin());
|
||||
EXPECT_EQ(slice.end(), slice.cend());
|
||||
|
||||
EXPECT_TRUE(slice.empty());
|
||||
EXPECT_EQ(0u, slice.size());
|
||||
EXPECT_EQ(slice.begin(), slice.data());
|
||||
|
||||
EXPECT_EQ(0u, slice.get_slice(0, 0).size());
|
||||
EXPECT_THROW(slice.get_slice(0, 1), std::out_of_range);
|
||||
EXPECT_EQ(0u, slice.remove_prefix(1));
|
||||
EXPECT_EQ(0u, slice.take_slice(1).size());
|
||||
}
|
||||
|
||||
TEST(ByteSlice, AdaptVector)
|
||||
{
|
||||
static constexpr const char base_string[] = "this is an example message";
|
||||
std::vector<std::uint8_t> adapted(sizeof(base_string));
|
||||
|
||||
ASSERT_EQ(sizeof(base_string), adapted.size());
|
||||
std::memcpy(adapted.data(), base_string, sizeof(base_string));
|
||||
|
||||
const epee::span<const uint8_t> original = epee::to_span(adapted);
|
||||
const epee::byte_slice slice{std::move(adapted)};
|
||||
|
||||
EXPECT_EQ(sizeof(base_string), original.size());
|
||||
|
||||
EXPECT_EQ(original.begin(), slice.begin());
|
||||
EXPECT_EQ(original.cbegin(), slice.cbegin());
|
||||
EXPECT_EQ(original.end(), slice.end());
|
||||
EXPECT_EQ(original.cend(), slice.cend());
|
||||
|
||||
EXPECT_FALSE(slice.empty());
|
||||
EXPECT_EQ(original.data(), slice.data());
|
||||
EXPECT_EQ(original.size(), slice.size());
|
||||
EXPECT_TRUE(boost::range::equal(base_string, slice));
|
||||
}
|
||||
|
||||
TEST(ByteSlice, EmptyAdaptVector)
|
||||
{
|
||||
epee::byte_slice slice{std::vector<std::uint8_t>{}};
|
||||
|
||||
EXPECT_EQ(slice.begin(), slice.end());
|
||||
EXPECT_EQ(slice.begin(), slice.cbegin());
|
||||
EXPECT_EQ(slice.end(), slice.cend());
|
||||
|
||||
EXPECT_TRUE(slice.empty());
|
||||
EXPECT_EQ(0u, slice.size());
|
||||
EXPECT_EQ(slice.begin(), slice.data());
|
||||
|
||||
EXPECT_EQ(0u, slice.get_slice(0, 0).size());
|
||||
EXPECT_THROW(slice.get_slice(0, 1), std::out_of_range);
|
||||
EXPECT_EQ(0u, slice.remove_prefix(1));
|
||||
EXPECT_EQ(0u, slice.take_slice(1).size());
|
||||
}
|
||||
|
||||
TEST(ByteSlice, Move)
|
||||
{
|
||||
static constexpr const char base_string[] = "another example message";
|
||||
|
||||
epee::byte_slice slice{epee::as_byte_span(base_string)};
|
||||
EXPECT_TRUE(boost::range::equal(base_string, slice));
|
||||
|
||||
const epee::span<const std::uint8_t> original = epee::to_span(slice);
|
||||
epee::byte_slice moved{std::move(slice)};
|
||||
EXPECT_TRUE(boost::range::equal(base_string, moved));
|
||||
|
||||
EXPECT_EQ(slice.begin(), slice.end());
|
||||
EXPECT_EQ(slice.begin(), slice.cbegin());
|
||||
EXPECT_EQ(slice.end(), slice.cend());
|
||||
|
||||
EXPECT_EQ(original.begin(), moved.begin());
|
||||
EXPECT_EQ(moved.begin(), moved.cbegin());
|
||||
EXPECT_EQ(original.end(), moved.end());
|
||||
EXPECT_EQ(moved.end(), moved.cend());
|
||||
|
||||
EXPECT_TRUE(slice.empty());
|
||||
EXPECT_EQ(slice.begin(), slice.data());
|
||||
EXPECT_EQ(0u, slice.size());
|
||||
|
||||
EXPECT_FALSE(moved.empty());
|
||||
EXPECT_EQ(moved.begin(), moved.data());
|
||||
EXPECT_EQ(original.size(), moved.size());
|
||||
|
||||
slice = std::move(moved);
|
||||
EXPECT_TRUE(boost::range::equal(base_string, slice));
|
||||
|
||||
EXPECT_EQ(original.begin(), slice.begin());
|
||||
EXPECT_EQ(slice.begin(), slice.cbegin());
|
||||
EXPECT_EQ(original.end(), slice.end());
|
||||
EXPECT_EQ(slice.end(), slice.cend());
|
||||
|
||||
EXPECT_FALSE(slice.empty());
|
||||
EXPECT_EQ(slice.begin(), slice.data());
|
||||
EXPECT_EQ(original.size(), slice.size());
|
||||
|
||||
EXPECT_TRUE(moved.empty());
|
||||
EXPECT_EQ(moved.begin(), moved.data());
|
||||
EXPECT_EQ(0u, moved.size());
|
||||
}
|
||||
|
||||
TEST(ByteSlice, Clone)
|
||||
{
|
||||
static constexpr const char base_string[] = "another example message";
|
||||
|
||||
const epee::byte_slice slice{epee::as_byte_span(base_string)};
|
||||
EXPECT_TRUE(boost::range::equal(base_string, slice));
|
||||
|
||||
const epee::byte_slice clone{slice.clone()};
|
||||
EXPECT_TRUE(boost::range::equal(base_string, clone));
|
||||
|
||||
EXPECT_EQ(slice.begin(), clone.begin());
|
||||
EXPECT_EQ(slice.cbegin(), clone.cbegin());
|
||||
EXPECT_EQ(slice.end(), clone.end());
|
||||
EXPECT_EQ(slice.cend(), clone.cend());
|
||||
|
||||
EXPECT_FALSE(slice.empty());
|
||||
EXPECT_FALSE(clone.empty());
|
||||
EXPECT_EQ(slice.cbegin(), slice.data());
|
||||
EXPECT_EQ(slice.data(), clone.data());
|
||||
EXPECT_EQ(sizeof(base_string), slice.size());
|
||||
EXPECT_EQ(slice.size(), clone.size());
|
||||
}
|
||||
|
||||
TEST(ByteSlice, RemovePrefix)
|
||||
{
|
||||
static constexpr const char base_string[] = "another example message";
|
||||
static constexpr std::size_t remove_size = sizeof("another");
|
||||
static constexpr std::size_t remaining = sizeof(base_string) - remove_size;
|
||||
|
||||
epee::byte_slice slice{epee::as_byte_span(base_string)};
|
||||
EXPECT_TRUE(boost::range::equal(base_string, slice));
|
||||
|
||||
const epee::span<const std::uint8_t> original = epee::to_span(slice);
|
||||
EXPECT_EQ(remove_size, slice.remove_prefix(remove_size));
|
||||
|
||||
EXPECT_EQ(original.begin() + remove_size, slice.begin());
|
||||
EXPECT_EQ(slice.begin(), slice.cbegin());
|
||||
EXPECT_EQ(original.end(), slice.end());
|
||||
EXPECT_EQ(slice.end(), slice.cend());
|
||||
|
||||
EXPECT_FALSE(slice.empty());
|
||||
EXPECT_EQ(slice.cbegin(), slice.data());
|
||||
EXPECT_EQ(remaining, slice.size());
|
||||
|
||||
// touch original pointers to check "free" status
|
||||
EXPECT_TRUE(boost::range::equal(base_string, original));
|
||||
|
||||
EXPECT_EQ(remaining, slice.remove_prefix(remaining + 1));
|
||||
|
||||
EXPECT_EQ(slice.begin(), slice.end());
|
||||
EXPECT_EQ(slice.begin(), slice.cbegin());
|
||||
EXPECT_EQ(slice.end(), slice.cend());
|
||||
|
||||
EXPECT_TRUE(slice.empty());
|
||||
EXPECT_EQ(slice.cbegin(), slice.data());
|
||||
EXPECT_EQ(0, slice.size());
|
||||
}
|
||||
|
||||
TEST(ByteSlice, TakeSlice)
|
||||
{
|
||||
static constexpr const char base_string[] = "another example message";
|
||||
static constexpr std::size_t remove_size = sizeof("another");
|
||||
static constexpr std::size_t remaining = sizeof(base_string) - remove_size;
|
||||
|
||||
epee::byte_slice slice{epee::as_byte_span(base_string)};
|
||||
EXPECT_TRUE(boost::range::equal(base_string, slice));
|
||||
|
||||
const epee::span<const std::uint8_t> original = epee::to_span(slice);
|
||||
const epee::byte_slice slice2 = slice.take_slice(remove_size);
|
||||
|
||||
EXPECT_EQ(original.begin() + remove_size, slice.begin());
|
||||
EXPECT_EQ(slice.begin(), slice.cbegin());
|
||||
EXPECT_EQ(original.end(), slice.end());
|
||||
EXPECT_EQ(slice.end(), slice.cend());
|
||||
|
||||
EXPECT_EQ(original.begin(), slice2.begin());
|
||||
EXPECT_EQ(slice2.begin(), slice2.cbegin());
|
||||
EXPECT_EQ(original.begin() + remove_size, slice2.end());
|
||||
EXPECT_EQ(slice2.end(), slice2.cend());
|
||||
|
||||
EXPECT_FALSE(slice.empty());
|
||||
EXPECT_EQ(slice.cbegin(), slice.data());
|
||||
EXPECT_EQ(remaining, slice.size());
|
||||
|
||||
EXPECT_FALSE(slice2.empty());
|
||||
EXPECT_EQ(slice2.cbegin(), slice2.data());
|
||||
EXPECT_EQ(remove_size, slice2.size());
|
||||
|
||||
// touch original pointers to check "free" status
|
||||
EXPECT_TRUE(boost::range::equal(base_string, original));
|
||||
|
||||
const epee::byte_slice slice3 = slice.take_slice(remaining + 1);
|
||||
|
||||
EXPECT_EQ(slice.begin(), slice.end());
|
||||
EXPECT_EQ(slice.begin(), slice.cbegin());
|
||||
EXPECT_EQ(slice.end(), slice.cend());
|
||||
|
||||
EXPECT_EQ(original.begin(), slice2.begin());
|
||||
EXPECT_EQ(slice2.begin(), slice2.cbegin());
|
||||
EXPECT_EQ(original.begin() + remove_size, slice2.end());
|
||||
EXPECT_EQ(slice2.end(), slice2.cend());
|
||||
|
||||
EXPECT_EQ(slice2.end(), slice3.begin());
|
||||
EXPECT_EQ(slice3.begin(), slice3.cbegin());
|
||||
EXPECT_EQ(original.end(), slice3.end());
|
||||
EXPECT_EQ(slice3.end(), slice3.cend());
|
||||
|
||||
EXPECT_TRUE(slice.empty());
|
||||
EXPECT_EQ(slice.cbegin(), slice.data());
|
||||
EXPECT_EQ(0, slice.size());
|
||||
|
||||
EXPECT_FALSE(slice2.empty());
|
||||
EXPECT_EQ(slice2.cbegin(), slice2.data());
|
||||
EXPECT_EQ(remove_size, slice2.size());
|
||||
|
||||
EXPECT_FALSE(slice3.empty());
|
||||
EXPECT_EQ(slice3.cbegin(), slice3.data());
|
||||
EXPECT_EQ(remaining, slice3.size());
|
||||
|
||||
// touch original pointers to check "free" status
|
||||
slice = nullptr;
|
||||
EXPECT_TRUE(boost::range::equal(base_string, original));
|
||||
}
|
||||
|
||||
TEST(ByteSlice, GetSlice)
|
||||
{
|
||||
static constexpr const char base_string[] = "another example message";
|
||||
static constexpr std::size_t get_size = sizeof("another");
|
||||
static constexpr std::size_t get2_size = sizeof(base_string) - get_size;
|
||||
|
||||
epee::span<const std::uint8_t> original{};
|
||||
epee::byte_slice slice2{};
|
||||
epee::byte_slice slice3{};
|
||||
|
||||
// make sure get_slice increments ref count
|
||||
{
|
||||
const epee::byte_slice slice{epee::as_byte_span(base_string)};
|
||||
EXPECT_TRUE(boost::range::equal(base_string, slice));
|
||||
|
||||
original = epee::to_span(slice);
|
||||
slice2 = slice.get_slice(0, get_size);
|
||||
|
||||
EXPECT_EQ(original.begin(), slice.begin());
|
||||
EXPECT_EQ(slice.begin(), slice.cbegin());
|
||||
EXPECT_EQ(original.end(), slice.end());
|
||||
EXPECT_EQ(slice.end(), slice.cend());
|
||||
|
||||
EXPECT_EQ(original.begin(), slice2.begin());
|
||||
EXPECT_EQ(slice2.begin(), slice2.cbegin());
|
||||
EXPECT_EQ(original.begin() + get_size, slice2.end());
|
||||
EXPECT_EQ(slice2.end(), slice2.cend());
|
||||
|
||||
EXPECT_FALSE(slice.empty());
|
||||
EXPECT_EQ(slice.cbegin(), slice.data());
|
||||
EXPECT_EQ(original.size(), slice.size());
|
||||
|
||||
EXPECT_FALSE(slice2.empty());
|
||||
EXPECT_EQ(slice2.cbegin(), slice2.data());
|
||||
EXPECT_EQ(get_size, slice2.size());
|
||||
|
||||
// touch original pointers to check "free" status
|
||||
EXPECT_TRUE(boost::range::equal(base_string, original));
|
||||
|
||||
slice3 = slice.get_slice(get_size, sizeof(base_string));
|
||||
|
||||
EXPECT_EQ(original.begin(), slice.begin());
|
||||
EXPECT_EQ(slice.begin(), slice.cbegin());
|
||||
EXPECT_EQ(original.end(), slice.end());
|
||||
EXPECT_EQ(slice.end(), slice.cend());
|
||||
|
||||
EXPECT_EQ(original.begin(), slice2.begin());
|
||||
EXPECT_EQ(slice2.begin(), slice2.cbegin());
|
||||
EXPECT_EQ(original.begin() + get_size, slice2.end());
|
||||
EXPECT_EQ(slice2.end(), slice2.cend());
|
||||
|
||||
EXPECT_EQ(slice2.end(), slice3.begin());
|
||||
EXPECT_EQ(slice3.begin(), slice3.cbegin());
|
||||
EXPECT_EQ(original.end(), slice3.end());
|
||||
EXPECT_EQ(slice3.end(), slice3.cend());
|
||||
|
||||
EXPECT_FALSE(slice.empty());
|
||||
EXPECT_EQ(slice.cbegin(), slice.data());
|
||||
EXPECT_EQ(original.size(), slice.size());
|
||||
|
||||
EXPECT_FALSE(slice2.empty());
|
||||
EXPECT_EQ(slice2.cbegin(), slice2.data());
|
||||
EXPECT_EQ(get_size, slice2.size());
|
||||
|
||||
EXPECT_FALSE(slice3.empty());
|
||||
EXPECT_EQ(slice3.cbegin(), slice3.data());
|
||||
EXPECT_EQ(get2_size, slice3.size());
|
||||
|
||||
EXPECT_THROW(slice.get_slice(1, 0), std::out_of_range);
|
||||
EXPECT_THROW(slice.get_slice(0, sizeof(base_string) + 1), std::out_of_range);
|
||||
EXPECT_THROW(slice.get_slice(sizeof(base_string) + 1, sizeof(base_string) + 1), std::out_of_range);
|
||||
EXPECT_TRUE(slice.get_slice(sizeof(base_string), sizeof(base_string)).empty());
|
||||
|
||||
EXPECT_EQ(original.begin(), slice.begin());
|
||||
EXPECT_EQ(slice.begin(), slice.cbegin());
|
||||
EXPECT_EQ(original.end(), slice.end());
|
||||
EXPECT_EQ(slice.end(), slice.cend());
|
||||
|
||||
EXPECT_FALSE(slice.empty());
|
||||
EXPECT_EQ(slice.cbegin(), slice.data());
|
||||
EXPECT_EQ(original.size(), slice.size());
|
||||
}
|
||||
|
||||
// touch original pointers to check "free" status
|
||||
EXPECT_TRUE(boost::range::equal(base_string, original));
|
||||
epee::shared_sv slice{};
|
||||
auto& view = slice.view;
|
||||
|
||||
EXPECT_TRUE(view.empty());
|
||||
EXPECT_EQ(0u, slice.extract_prefix(0).size());
|
||||
EXPECT_EQ(0u, slice.extract_prefix(1).size());
|
||||
|
||||
using namespace std::literals;
|
||||
epee::shared_sv from_str{"abcdef"s};
|
||||
EXPECT_EQ("abcdef"sv, from_str.view);
|
||||
auto sv2 = from_str.extract_prefix(4);
|
||||
EXPECT_EQ(4, sv2.size());
|
||||
EXPECT_EQ(2, from_str.size());
|
||||
auto sv3 = sv2.extract_prefix(1);
|
||||
EXPECT_EQ("a"sv, sv3.view);
|
||||
EXPECT_EQ("bcd"sv, sv2.view);
|
||||
EXPECT_EQ("ef"sv, from_str.view);
|
||||
EXPECT_EQ(3, from_str.ptr.use_count());
|
||||
sv2 = {};
|
||||
EXPECT_EQ(2, from_str.ptr.use_count());
|
||||
sv3 = {};
|
||||
EXPECT_EQ(1, from_str.ptr.use_count());
|
||||
auto ptr = from_str.ptr;
|
||||
from_str = {};
|
||||
EXPECT_EQ(1, ptr.use_count());
|
||||
ptr.reset();
|
||||
EXPECT_EQ(0, from_str.size());
|
||||
EXPECT_EQ(0, sv2.size());
|
||||
EXPECT_EQ(0, sv3.size());
|
||||
}
|
||||
|
||||
TEST(ToHex, String)
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
#include <limits>
|
||||
#include <set>
|
||||
|
||||
#include "byte_slice.h"
|
||||
#include "shared_sv.h"
|
||||
#include "crypto/crypto.h"
|
||||
#include "cryptonote_basic/connection_context.h"
|
||||
#include "cryptonote_core/cryptonote_core.h"
|
||||
|
@ -54,7 +54,7 @@ namespace
|
|||
boost::asio::io_service& io_service_;
|
||||
std::size_t ref_count_;
|
||||
|
||||
virtual bool do_send(epee::byte_slice message) override final
|
||||
virtual bool do_send(epee::shared_sv message) override final
|
||||
{
|
||||
send_queue_.push_back(std::move(message));
|
||||
return true;
|
||||
|
@ -110,7 +110,7 @@ namespace
|
|||
EXPECT_EQ(0u, ref_count_);
|
||||
}
|
||||
|
||||
std::deque<epee::byte_slice> send_queue_;
|
||||
std::deque<epee::shared_sv> send_queue_;
|
||||
};
|
||||
|
||||
class test_connection
|
||||
|
@ -273,9 +273,9 @@ namespace
|
|||
|
||||
cryptonote::levin::notify make_notifier(const std::size_t noise_size, bool is_public)
|
||||
{
|
||||
epee::byte_slice noise = nullptr;
|
||||
epee::shared_sv noise;
|
||||
if (noise_size)
|
||||
noise = epee::levin::make_noise_notify(noise_size);
|
||||
noise = epee::shared_sv{epee::levin::make_noise_notify(noise_size)};
|
||||
return cryptonote::levin::notify{io_service_, connections_, std::move(noise), is_public};
|
||||
}
|
||||
|
||||
|
@ -312,7 +312,7 @@ TEST(make_header, expect_return)
|
|||
|
||||
TEST(make_notify, empty_payload)
|
||||
{
|
||||
const epee::byte_slice message = epee::levin::make_notify(443, nullptr);
|
||||
const epee::shared_sv message{epee::levin::make_notify(443, nullptr)};
|
||||
const epee::levin::bucket_head2 header =
|
||||
epee::levin::make_header(443, 0, LEVIN_PACKET_REQUEST, false);
|
||||
ASSERT_EQ(sizeof(header), message.size());
|
||||
|
@ -324,7 +324,7 @@ TEST(make_notify, with_payload)
|
|||
std::string bytes(100, 'a');
|
||||
std::generate(bytes.begin(), bytes.end(), crypto::random_device{});
|
||||
|
||||
const epee::byte_slice message = epee::levin::make_notify(443, epee::strspan<std::uint8_t>(bytes));
|
||||
const epee::shared_sv message{epee::levin::make_notify(443, epee::strspan<std::uint8_t>(bytes))};
|
||||
const epee::levin::bucket_head2 header =
|
||||
epee::levin::make_header(443, bytes.size(), LEVIN_PACKET_REQUEST, false);
|
||||
|
||||
|
@ -343,31 +343,31 @@ TEST(make_noise, valid)
|
|||
static constexpr const std::uint32_t flags =
|
||||
LEVIN_PACKET_BEGIN | LEVIN_PACKET_END;
|
||||
|
||||
const epee::byte_slice noise = epee::levin::make_noise_notify(1024);
|
||||
const epee::shared_sv noise{epee::levin::make_noise_notify(1024)};
|
||||
const epee::levin::bucket_head2 header =
|
||||
epee::levin::make_header(0, 1024 - sizeof(epee::levin::bucket_head2), flags, false);
|
||||
|
||||
ASSERT_EQ(1024, noise.size());
|
||||
EXPECT_TRUE(std::memcmp(std::addressof(header), noise.data(), sizeof(header)) == 0);
|
||||
EXPECT_EQ(1024 - sizeof(header), std::count(noise.cbegin() + sizeof(header), noise.cend(), 0));
|
||||
EXPECT_EQ(1024 - sizeof(header), std::count(noise.view.cbegin() + sizeof(header), noise.view.cend(), 0));
|
||||
}
|
||||
|
||||
TEST(make_fragment, invalid)
|
||||
{
|
||||
EXPECT_TRUE(epee::levin::make_fragmented_notify(nullptr, 0, nullptr).empty());
|
||||
EXPECT_TRUE(epee::levin::make_fragmented_notify({}, 0, nullptr).empty());
|
||||
}
|
||||
|
||||
TEST(make_fragment, single)
|
||||
{
|
||||
const epee::byte_slice noise = epee::levin::make_noise_notify(1024);
|
||||
const epee::byte_slice fragment = epee::levin::make_fragmented_notify(noise, 11, nullptr);
|
||||
const epee::shared_sv noise{epee::levin::make_noise_notify(1024)};
|
||||
const epee::shared_sv fragment{epee::levin::make_fragmented_notify(noise.view, 11, nullptr)};
|
||||
const epee::levin::bucket_head2 header =
|
||||
epee::levin::make_header(11, 1024 - sizeof(epee::levin::bucket_head2), LEVIN_PACKET_REQUEST, false);
|
||||
|
||||
EXPECT_EQ(1024, noise.size());
|
||||
ASSERT_EQ(1024, fragment.size());
|
||||
EXPECT_TRUE(std::memcmp(std::addressof(header), fragment.data(), sizeof(header)) == 0);
|
||||
EXPECT_EQ(1024 - sizeof(header), std::count(noise.cbegin() + sizeof(header), noise.cend(), 0));
|
||||
EXPECT_EQ(1024 - sizeof(header), std::count(noise.view.cbegin() + sizeof(header), noise.view.cend(), 0));
|
||||
}
|
||||
|
||||
TEST(make_fragment, multiple)
|
||||
|
@ -375,8 +375,8 @@ TEST(make_fragment, multiple)
|
|||
std::string bytes(1024 * 3 - 150, 'a');
|
||||
std::generate(bytes.begin(), bytes.end(), crypto::random_device{});
|
||||
|
||||
const epee::byte_slice noise = epee::levin::make_noise_notify(1024);
|
||||
epee::byte_slice fragment = epee::levin::make_fragmented_notify(noise, 114, epee::strspan<std::uint8_t>(bytes));
|
||||
const epee::shared_sv noise{epee::levin::make_noise_notify(1024)};
|
||||
epee::shared_sv fragment{epee::levin::make_fragmented_notify(noise.view, 114, epee::strspan<std::uint8_t>(bytes))};
|
||||
|
||||
epee::levin::bucket_head2 header =
|
||||
epee::levin::make_header(0, 1024 - sizeof(epee::levin::bucket_head2), LEVIN_PACKET_BEGIN, false);
|
||||
|
@ -384,7 +384,7 @@ TEST(make_fragment, multiple)
|
|||
ASSERT_LE(sizeof(header), fragment.size());
|
||||
EXPECT_TRUE(std::memcmp(std::addressof(header), fragment.data(), sizeof(header)) == 0);
|
||||
|
||||
fragment.take_slice(sizeof(header));
|
||||
fragment.view.remove_prefix(sizeof(header));
|
||||
header.m_flags = LEVIN_PACKET_REQUEST;
|
||||
header.m_cb = bytes.size();
|
||||
header.m_command = 114;
|
||||
|
@ -392,13 +392,13 @@ TEST(make_fragment, multiple)
|
|||
ASSERT_LE(sizeof(header), fragment.size());
|
||||
EXPECT_TRUE(std::memcmp(std::addressof(header), fragment.data(), sizeof(header)) == 0);
|
||||
|
||||
fragment.take_slice(sizeof(header));
|
||||
fragment.view.remove_prefix(sizeof(header));
|
||||
|
||||
ASSERT_LE(bytes.size(), fragment.size());
|
||||
EXPECT_TRUE(std::memcmp(bytes.data(), fragment.data(), 1024 - sizeof(header) * 2) == 0);
|
||||
|
||||
bytes.erase(0, 1024 - sizeof(header) * 2);
|
||||
fragment.take_slice(1024 - sizeof(header) * 2);
|
||||
fragment.view.remove_prefix(1024 - sizeof(header) * 2);
|
||||
header.m_flags = 0;
|
||||
header.m_cb = 1024 - sizeof(header);
|
||||
header.m_command = 0;
|
||||
|
@ -406,24 +406,24 @@ TEST(make_fragment, multiple)
|
|||
ASSERT_LE(sizeof(header), fragment.size());
|
||||
EXPECT_TRUE(std::memcmp(std::addressof(header), fragment.data(), sizeof(header)) == 0);
|
||||
|
||||
fragment.take_slice(sizeof(header));
|
||||
fragment.view.remove_prefix(sizeof(header));
|
||||
|
||||
ASSERT_LE(bytes.size(), fragment.size());
|
||||
EXPECT_TRUE(std::memcmp(bytes.data(), fragment.data(), 1024 - sizeof(header)) == 0);
|
||||
|
||||
bytes.erase(0, 1024 - sizeof(header));
|
||||
fragment.take_slice(1024 - sizeof(header));
|
||||
fragment.view.remove_prefix(1024 - sizeof(header));
|
||||
header.m_flags = LEVIN_PACKET_END;
|
||||
|
||||
ASSERT_LE(sizeof(header), fragment.size());
|
||||
EXPECT_TRUE(std::memcmp(std::addressof(header), fragment.data(), sizeof(header)) == 0);
|
||||
|
||||
fragment.take_slice(sizeof(header));
|
||||
fragment.view.remove_prefix(sizeof(header));
|
||||
EXPECT_TRUE(std::memcmp(bytes.data(), fragment.data(), bytes.size()) == 0);
|
||||
|
||||
fragment.take_slice(bytes.size());
|
||||
fragment.view.remove_prefix(bytes.size());
|
||||
|
||||
EXPECT_EQ(18, std::count(fragment.cbegin(), fragment.cend(), 0));
|
||||
EXPECT_EQ(18, std::count(fragment.view.cbegin(), fragment.view.cend(), 0));
|
||||
}
|
||||
|
||||
TEST_F(levin_notify, defaulted)
|
||||
|
|
Loading…
Reference in a new issue