Files
monero/contrib/epee/include/net/levin_base.h
T
Thomas 00d21ee75e epee: remove dead code
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. Added 9bfa593ee (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
  added eabb51960 (2015) on the TCP send path; their calls in do_send_chunk /
  the write-completion handler were deleted by 3be1dbd09 (2022). The real send
  path is connection<T>::start_write().
- sleep_before_packet: outbound rate-limit sleep added eabb51960 (2015);
  callers were thinned over time (cf5f62361, 2017) and the last removed by
  3be1dbd09 (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 in 0c545f614
  (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 in
  5833d66f6 (2017, "Change logging to easylogging++").
- LOG_PRINT_CCONTEXT_L3: had a single "REMOTE PEERLIST" call site, removed in
  the same 5833d66f6 (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 in 5833d66f6 (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: added eabb51960 (2015); its only consumers
  (set_target_kill / m_target_MB) were stripped the same day in 5ce4256e3
  (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 - added bda8c5983 (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 in
  7414e2bac (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. Added 7f407c027 (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.
2026-06-30 15:59:47 +02:00

171 lines
6.3 KiB
C++

// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * 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.
// * Neither the name of the Andrey N. Sabelnikov 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 OWNER 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.
//
#ifndef _LEVIN_BASE_H_
#define _LEVIN_BASE_H_
#include <chrono>
#include <cstdint>
#include "byte_stream.h"
#include "net_utils_base.h"
#include "span.h"
#define LEVIN_SIGNATURE 0x0101010101012101LL //Bender's nightmare
namespace epee
{
class byte_slice;
namespace levin
{
#pragma pack(push)
#pragma pack(1)
struct bucket_head2
{
uint64_t m_signature;
uint64_t m_cb;
uint8_t m_have_to_return_data;
uint32_t m_command;
int32_t m_return_code;
uint32_t m_flags;
uint32_t m_protocol_version;
};
#pragma pack(pop)
constexpr const std::chrono::milliseconds LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED{0};
#define LEVIN_INITIAL_MAX_PACKET_SIZE 256*1024 // 256 KiB before handshake
#define LEVIN_DEFAULT_MAX_PACKET_SIZE 100000000 //100MB by default after handshake
#define LEVIN_PACKET_REQUEST 0x00000001
#define LEVIN_PACKET_RESPONSE 0x00000002
#define LEVIN_PACKET_BEGIN 0x00000004
#define LEVIN_PACKET_END 0x00000008
#define LEVIN_PROTOCOL_VER_1 1
template<class t_connection_context = net_utils::connection_context_base>
struct levin_commands_handler
{
virtual int invoke(int command, const epee::span<const uint8_t> in_buff, byte_stream& buff_out, t_connection_context& context)=0;
virtual int notify(int command, const epee::span<const uint8_t> in_buff, t_connection_context& context)=0;
virtual void callback(t_connection_context& context){};
virtual void on_connection_new(t_connection_context& context){};
virtual void on_connection_close(t_connection_context& context){};
virtual ~levin_commands_handler(){}
};
#define LEVIN_OK 0
#define LEVIN_ERROR_CONNECTION -1
#define LEVIN_ERROR_CONNECTION_NOT_FOUND -2
#define LEVIN_ERROR_CONNECTION_DESTROYED -3
#define LEVIN_ERROR_CONNECTION_TIMEDOUT -4
#define LEVIN_ERROR_CONNECTION_NO_DUPLEX_PROTOCOL -5
#define LEVIN_ERROR_CONNECTION_HANDLER_NOT_DEFINED -6
#define LEVIN_ERROR_FORMAT -7
#define DESCRIBE_RET_CODE(code) case code: return #code;
inline
const char* get_err_descr(int err)
{
switch(err)
{
DESCRIBE_RET_CODE(LEVIN_OK);
DESCRIBE_RET_CODE(LEVIN_ERROR_CONNECTION);
DESCRIBE_RET_CODE(LEVIN_ERROR_CONNECTION_NOT_FOUND);
DESCRIBE_RET_CODE(LEVIN_ERROR_CONNECTION_DESTROYED);
DESCRIBE_RET_CODE(LEVIN_ERROR_CONNECTION_TIMEDOUT);
DESCRIBE_RET_CODE(LEVIN_ERROR_CONNECTION_NO_DUPLEX_PROTOCOL);
DESCRIBE_RET_CODE(LEVIN_ERROR_CONNECTION_HANDLER_NOT_DEFINED);
DESCRIBE_RET_CODE(LEVIN_ERROR_FORMAT);
default:
return "unknown code";
}
}
//! Provides space for levin (p2p) header, so that payload can be sent without copy
class message_writer
{
byte_slice finalize(uint32_t command, uint32_t flags, uint32_t return_code, bool expect_response);
public:
using header = bucket_head2;
explicit message_writer(std::size_t reserve = 8192);
message_writer(const message_writer&) = delete;
message_writer(message_writer&&) = default;
~message_writer() = default;
message_writer& operator=(const message_writer&) = delete;
message_writer& operator=(message_writer&&) = default;
//! \return Size of payload (excludes header size).
std::size_t payload_size() const noexcept
{
return buffer.size() < sizeof(header) ? 0 : buffer.size() - sizeof(header);
}
byte_slice finalize_invoke(uint32_t command) { return finalize(command, LEVIN_PACKET_REQUEST, 0, true); }
byte_slice finalize_notify(uint32_t command) { return finalize(command, LEVIN_PACKET_REQUEST, 0, false); }
byte_slice finalize_response(uint32_t command, uint32_t return_code)
{
return finalize(command, LEVIN_PACKET_RESPONSE, return_code, false);
}
//! Has space for levin header until a finalize method is used
byte_stream buffer;
};
//! \return Initialized levin header.
bucket_head2 make_header(uint32_t command, uint64_t msg_size, uint32_t flags, bool expect_response) noexcept;
/*! Generate a dummy levin message.
\param noise_bytes Total size of the returned `byte_slice`.
\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);
/*! Generate 1+ levin messages that are identical to the noise message size.
\param noise_size Each levin message will be identical to this value.
\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 std::size_t noise_size, int command, message_writer message);
}
}
#endif //_LEVIN_BASE_H_