mirror of
https://github.com/monero-project/monero.git
synced 2026-07-28 14:47:15 -07:00
Each symbol below has no references anywhere in the tree (src/, contrib/, tests/); the full build still links cleanly. Per-symbol history is given so the "why is this unused" is auditable (hashes are ancestors of HEAD). net/connection_basic (all four lost their last caller in the 2022 connection rewrite,3be1dbd09"connection: fix implementation"): - to_string(t_connection_type): unused free function. Added9bfa593ee(2015) to print a connection's type in a debug log;3be1dbd09(2022) switched that log line to std::to_string on the enum, orphaning this overload. - do_send_handler_write / do_send_handler_write_from_queue: trace-only stubs addedeabb51960(2015) on the TCP send path; their calls in do_send_chunk / the write-completion handler were deleted by3be1dbd09(2022). The real send path is connection<T>::start_write(). - sleep_before_packet: outbound rate-limit sleep addedeabb51960(2015); callers were thinned over time (cf5f62361, 2017) and the last removed by3be1dbd09(2022). Rate-limiting now lives in connection<T>::start_write() via the global out-throttle. net/levin_base: - struct bucket_head: legacy levin packet header from the 2014 epee import, superseded by bucket_head2 (the wire header, `using header = bucket_head2`). Its last use went away with the old levin_protocol_handler in0c545f614(2024, "epee/test: remove levin_protocol_handler and core_proxy tests"). - LEVIN_PROTOCOL_VER_0: dead on arrival - present since the 2014 import, never referenced (the code uses LEVIN_PROTOCOL_VER_1). net/net_utils_base (connection-context logging macros): - LOG_PRINT_CC_L4: levin trace macro (2014 import); all call sites removed in5833d66f6(2017, "Change logging to easylogging++"). - LOG_PRINT_CCONTEXT_L3: had a single "REMOTE PEERLIST" call site, removed in the same5833d66f6(2017). - LOG_PRINT_CC_L3: only ever referenced inside LOG_PRINT_CCONTEXT_L3's body, never invoked directly - transitively dead once the above was removed. - LOG_CC: dead on arrival - added in5833d66f6(2017) but never invoked. (The plain LOG_PRINT_L3 macro that LOG_PRINT_CC_L3/CCONTEXT_L3 wrap is still used elsewhere and is left untouched; only these unused CC wrappers go.) net/network_throttle: - typedef network_MB: addedeabb51960(2015); its only consumers (set_target_kill / m_target_MB) were stripped the same day in5ce4256e3(2015), leaving an unused typedef. profile_tools: - START_WAY_POINTS / WAY_POINT / WAY_POINT2: dead on arrival - upstream-epee profiling macros present since the 2014 import, never once invoked (they only ever appear as definitions in profile_tools.h across all history). - TIME_MEASURE_PAUSE / TIME_MEASURE_RESTART and their _NS_ variants: dead on arrival - addedbda8c5983(2017, "epee: add nanosecond timer and pause/restart profiling macros") but never wired to any call site. Of this family only TIME_MEASURE_START / TIME_MEASURE_FINISH are actually used. storages: - typedef binarybuffer (portable_storage_base): used by the binary store/load API since the 2014 import; its last consumer (store_to_binary) was removed in7414e2bac(2020, "Change epee binary output from std::stringstream to byte_stream"). - the templated min_bytes() member declaration (portable_storage_from_bin): declared but never defined or called. Added7f407c027(2020-12-26, "portable_storage: add some sanity checks on data size") alongside the ps_min_bytes<> trait struct, but the size checks were implemented via that trait (ps_min_bytes<>::strict), so this member was never defined or wired up - dead on arrival. The ps_min_bytes<> trait remains in use and is left intact.
192 lines
7.7 KiB
C++
192 lines
7.7 KiB
C++
/// @file
|
|
/// @author rfree (current maintainer in monero.cc project)
|
|
/// @brief base for connection, contains e.g. the ratelimit hooks
|
|
|
|
// ! This file might contain variable names same as in template class connection<>
|
|
// ! from files contrib/epee/include/net/abstract_tcp_server2.*
|
|
// ! I am not a lawyer; afaik APIs, var names etc are not copyrightable ;)
|
|
// ! (how ever if in some wonderful juristdictions that is not the case, then why not make another sub-class withat that members and licence it as epee part)
|
|
// ! Working on above premise, IF this is valid in your juristdictions, then consider this code as released as:
|
|
|
|
// Copyright (c) 2014-2024, 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.
|
|
|
|
/* rfree: place for handlers for the non-template base, can be used by connection<> template class in abstract_tcp_server2 file */
|
|
|
|
#ifndef INCLUDED_p2p_connection_basic_hpp
|
|
#define INCLUDED_p2p_connection_basic_hpp
|
|
|
|
|
|
#include <string>
|
|
#include <atomic>
|
|
#include <memory>
|
|
|
|
#include <boost/asio.hpp>
|
|
#include <boost/asio/ssl.hpp>
|
|
|
|
#include "byte_slice.h"
|
|
#include "net/net_utils_base.h"
|
|
#include "net/net_ssl.h"
|
|
#include "syncobj.h"
|
|
|
|
namespace epee
|
|
{
|
|
namespace net_utils
|
|
{
|
|
|
|
class connection_basic_shared_state
|
|
{
|
|
ssl_options_t ssl_options_;
|
|
public:
|
|
boost::asio::ssl::context ssl_context;
|
|
std::atomic<long> sock_count;
|
|
std::atomic<long> sock_number;
|
|
|
|
connection_basic_shared_state()
|
|
: ssl_options_(ssl_support_t::e_ssl_support_disabled),
|
|
ssl_context(boost::asio::ssl::context::tlsv12),
|
|
sock_count(0),
|
|
sock_number(0)
|
|
{}
|
|
|
|
void configure_ssl(ssl_options_t src)
|
|
{
|
|
ssl_options_ = std::move(src);
|
|
ssl_context = ssl_options_.create_context();
|
|
}
|
|
|
|
const ssl_options_t& ssl_options() const noexcept { return ssl_options_; }
|
|
};
|
|
|
|
/************************************************************************/
|
|
/* */
|
|
/************************************************************************/
|
|
/// Represents a single connection from a client.
|
|
|
|
class connection_basic_pimpl; // PIMPL for this class
|
|
|
|
enum t_connection_type { // type of the connection (of this server), e.g. so that we will know how to limit it
|
|
e_connection_type_NET = 0, // default (not used?)
|
|
e_connection_type_RPC = 1, // the rpc commands (probably not rate limited, not chunked, etc)
|
|
e_connection_type_P2P = 2 // to other p2p node (probably limited)
|
|
};
|
|
|
|
|
|
class connection_basic { // not-templated base class for rapid development of some code parts
|
|
// beware of removing const, net_utils::connection is sketchily doing a cast to prevent storing ptr twice
|
|
const std::shared_ptr<connection_basic_shared_state> m_state;
|
|
public:
|
|
|
|
std::unique_ptr< connection_basic_pimpl > mI; // my Implementation
|
|
|
|
// moved here from original connection<> - common member variables that do not depend on template in connection<>
|
|
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;
|
|
volatile bool m_is_multithreaded;
|
|
/// Strand to ensure the connection's handlers are not called concurrently.
|
|
boost::asio::io_context::strand strand_;
|
|
/// Socket for the connection.
|
|
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket_;
|
|
ssl_support_t m_ssl_support;
|
|
|
|
public:
|
|
// first counter is the ++/-- count of current sockets, the other socket_number is only-increasing ++ number generator
|
|
connection_basic(boost::asio::io_context &context, boost::asio::ip::tcp::socket&& sock, std::shared_ptr<connection_basic_shared_state> state, ssl_support_t ssl_support);
|
|
connection_basic(boost::asio::io_context &context, std::shared_ptr<connection_basic_shared_state> state, ssl_support_t ssl_support);
|
|
|
|
virtual ~connection_basic() noexcept(false);
|
|
|
|
//! \return `shared_state` object passed in construction (ptr never changes).
|
|
connection_basic_shared_state& get_state() noexcept { return *m_state; /* verified in constructor */ }
|
|
|
|
boost::asio::ip::tcp::socket& socket() { return socket_.next_layer(); }
|
|
ssl_support_t get_ssl_support() const { return m_ssl_support; }
|
|
void disable_ssl() { m_ssl_support = epee::net_utils::ssl_support_t::e_ssl_support_disabled; }
|
|
|
|
bool handshake(boost::asio::ssl::stream_base::handshake_type type, boost::asio::const_buffer buffer = {})
|
|
{
|
|
//m_state != nullptr verified in constructor
|
|
return m_state->ssl_options().handshake(strand_.context(), socket_, type, buffer);
|
|
}
|
|
|
|
template<typename MutableBufferSequence, typename ReadHandler>
|
|
void async_read_some(const MutableBufferSequence &buffers, ReadHandler &&handler)
|
|
{
|
|
if (m_ssl_support == epee::net_utils::ssl_support_t::e_ssl_support_enabled)
|
|
socket_.async_read_some(buffers, std::forward<ReadHandler>(handler));
|
|
else
|
|
socket().async_read_some(buffers, std::forward<ReadHandler>(handler));
|
|
}
|
|
|
|
template<typename ConstBufferSequence, typename WriteHandler>
|
|
void async_write_some(const ConstBufferSequence &buffers, WriteHandler &&handler)
|
|
{
|
|
if (m_ssl_support == epee::net_utils::ssl_support_t::e_ssl_support_enabled)
|
|
socket_.async_write_some(buffers, std::forward<WriteHandler>(handler));
|
|
else
|
|
socket().async_write_some(buffers, std::forward<WriteHandler>(handler));
|
|
}
|
|
|
|
template<typename ConstBufferSequence, typename WriteHandler>
|
|
void async_write(const ConstBufferSequence &buffers, WriteHandler &&handler)
|
|
{
|
|
if (m_ssl_support == epee::net_utils::ssl_support_t::e_ssl_support_enabled)
|
|
boost::asio::async_write(socket_, buffers, std::forward<WriteHandler>(handler));
|
|
else
|
|
boost::asio::async_write(socket(), buffers, std::forward<WriteHandler>(handler));
|
|
}
|
|
|
|
// various handlers to be called from connection class:
|
|
|
|
void logger_handle_net_write(size_t size); // network data written
|
|
void logger_handle_net_read(size_t size); // network data read
|
|
|
|
// config for rate limit
|
|
|
|
static void set_rate_up_limit(uint64_t limit);
|
|
static void set_rate_down_limit(uint64_t limit);
|
|
static uint64_t get_rate_up_limit();
|
|
static uint64_t get_rate_down_limit();
|
|
|
|
// config misc
|
|
static void set_tos_flag(int tos); // ToS / QoS flag
|
|
static int get_tos_flag();
|
|
|
|
// handlers and sleep
|
|
static double get_sleep_time(size_t cb);
|
|
};
|
|
|
|
} // nameserver
|
|
} // nameserver
|
|
|
|
#endif
|
|
|
|
|