aboutsummaryrefslogtreecommitdiff
path: root/contrib/epee/src
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/epee/src')
-rw-r--r--contrib/epee/src/CMakeLists.txt4
-rw-r--r--contrib/epee/src/byte_slice.cpp209
-rw-r--r--contrib/epee/src/connection_basic.cpp4
-rw-r--r--contrib/epee/src/levin_base.cpp128
4 files changed, 341 insertions, 4 deletions
diff --git a/contrib/epee/src/CMakeLists.txt b/contrib/epee/src/CMakeLists.txt
index 2465afebb..c512e3b86 100644
--- a/contrib/epee/src/CMakeLists.txt
+++ b/contrib/epee/src/CMakeLists.txt
@@ -26,8 +26,8 @@
# 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 STATIC hex.cpp http_auth.cpp mlog.cpp net_helper.cpp net_utils_base.cpp string_tools.cpp wipeable_string.cpp memwipe.c
- connection_basic.cpp network_throttle.cpp network_throttle-detail.cpp mlocker.cpp buffer.cpp net_ssl.cpp)
+add_library(epee STATIC byte_slice.cpp hex.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)
if (USE_READLINE AND GNU_READLINE_FOUND)
add_library(epee_readline STATIC readline_buffer.cpp)
diff --git a/contrib/epee/src/byte_slice.cpp b/contrib/epee/src/byte_slice.cpp
new file mode 100644
index 000000000..216049e5b
--- /dev/null
+++ b/contrib/epee/src/byte_slice.cpp
@@ -0,0 +1,209 @@
+// 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_(to_byte_span(to_span(buffer)))
+ {
+ if (!buffer.empty())
+ storage_ = allocate_slice<adapted_byte_slice<T>>(0, std::move(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
diff --git a/contrib/epee/src/connection_basic.cpp b/contrib/epee/src/connection_basic.cpp
index 82d9e3b53..7526dde26 100644
--- a/contrib/epee/src/connection_basic.cpp
+++ b/contrib/epee/src/connection_basic.cpp
@@ -128,7 +128,7 @@ connection_basic_pimpl::connection_basic_pimpl(const std::string &name) : m_thro
int connection_basic_pimpl::m_default_tos;
// methods:
-connection_basic::connection_basic(boost::asio::ip::tcp::socket&& sock, boost::shared_ptr<connection_basic_shared_state> state, ssl_support_t ssl_support)
+connection_basic::connection_basic(boost::asio::ip::tcp::socket&& sock, std::shared_ptr<connection_basic_shared_state> state, ssl_support_t ssl_support)
:
m_state(std::move(state)),
mI( new connection_basic_pimpl("peer") ),
@@ -152,7 +152,7 @@ connection_basic::connection_basic(boost::asio::ip::tcp::socket&& sock, boost::s
_note("Spawned connection #"<<mI->m_peer_number<<" to " << remote_addr_str << " currently we have sockets count:" << m_state->sock_count);
}
-connection_basic::connection_basic(boost::asio::io_service &io_service, boost::shared_ptr<connection_basic_shared_state> state, ssl_support_t ssl_support)
+connection_basic::connection_basic(boost::asio::io_service &io_service, std::shared_ptr<connection_basic_shared_state> state, ssl_support_t ssl_support)
:
m_state(std::move(state)),
mI( new connection_basic_pimpl("peer") ),
diff --git a/contrib/epee/src/levin_base.cpp b/contrib/epee/src/levin_base.cpp
new file mode 100644
index 000000000..ff845e2a7
--- /dev/null
+++ b/contrib/epee/src/levin_base.cpp
@@ -0,0 +1,128 @@
+// 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 "net/levin_base.h"
+
+#include "int-util.h"
+
+namespace epee
+{
+namespace levin
+{
+ bucket_head2 make_header(uint32_t command, uint64_t msg_size, uint32_t flags, bool expect_response) noexcept
+ {
+ bucket_head2 head = {0};
+ head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
+ head.m_have_to_return_data = expect_response;
+ head.m_cb = SWAP64LE(msg_size);
+
+ head.m_command = SWAP32LE(command);
+ head.m_protocol_version = SWAP32LE(LEVIN_PROTOCOL_VER_1);
+ head.m_flags = SWAP32LE(flags);
+ return head;
+ }
+
+ byte_slice 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};
+ }
+
+ byte_slice 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;
+
+ 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)};
+ }
+
+ byte_slice make_fragmented_notify(const byte_slice& noise_message, int command, epee::span<const std::uint8_t> payload)
+ {
+ const size_t noise_size = noise_message.size();
+ if (noise_size < sizeof(bucket_head2) * 2)
+ return nullptr;
+
+ 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};
+
+ 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};
+ }
+
+ // 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
+
+ bucket_head2 head = make_header(0, noise_size - sizeof(bucket_head2), LEVIN_PACKET_BEGIN, false);
+ buffer.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));
+
+ size_t copy_size = payload.remove_prefix(payload_space - sizeof(bucket_head2));
+ buffer.append(reinterpret_cast<const char*>(payload.data()) - copy_size, copy_size);
+
+ head.m_command = 0;
+ head.m_flags = 0;
+ head.m_cb = noise_size - sizeof(bucket_head2);
+
+ while (!payload.empty())
+ {
+ copy_size = payload.remove_prefix(payload_space);
+
+ 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);
+ }
+
+ const size_t padding = noise_size - copy_size - sizeof(bucket_head2);
+ buffer.append(reinterpret_cast<const char*>(noise_message.end()) - padding, padding);
+
+ return byte_slice{std::move(buffer)};
+ }
+} // levin
+} // epee