add a max levin packet size by command type

This commit is contained in:
moneromooo-monero 2020-12-31 16:51:59 +00:00
parent 16c780d568
commit b652d598d1
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3
8 changed files with 60 additions and 4 deletions

View file

@ -434,7 +434,7 @@ public:
//async call scenario //async call scenario
boost::shared_ptr<invoke_response_handler_base> response_handler = m_invoke_response_handlers.front(); boost::shared_ptr<invoke_response_handler_base> response_handler = m_invoke_response_handlers.front();
response_handler->reset_timer(); response_handler->reset_timer();
MDEBUG(m_connection_context << "LEVIN_PACKET partial msg received. len=" << cb); MDEBUG(m_connection_context << "LEVIN_PACKET partial msg received. len=" << cb << ", current total " << m_cache_in_buffer.size() << "/" << m_current_head.m_cb << " (" << (100.0f * m_cache_in_buffer.size() / (m_current_head.m_cb ? m_current_head.m_cb : 1)) << "%)");
} }
} }
break; break;
@ -469,6 +469,14 @@ public:
temp = std::move(m_fragment_buffer); temp = std::move(m_fragment_buffer);
m_fragment_buffer.clear(); m_fragment_buffer.clear();
std::memcpy(std::addressof(m_current_head), std::addressof(temp[0]), sizeof(bucket_head2)); std::memcpy(std::addressof(m_current_head), std::addressof(temp[0]), sizeof(bucket_head2));
const size_t max_bytes = m_connection_context.get_max_bytes(m_current_head.m_command);
if(m_current_head.m_cb > std::min<size_t>(max_packet_size, max_bytes))
{
MERROR(m_connection_context << "Maximum packet size exceed!, m_max_packet_size = " << std::min<size_t>(max_packet_size, max_bytes)
<< ", packet header received " << m_current_head.m_cb << ", command " << m_current_head.m_command
<< ", connection will be closed.");
return false;
}
buff_to_invoke = {reinterpret_cast<const uint8_t*>(temp.data()) + sizeof(bucket_head2), temp.size() - sizeof(bucket_head2)}; buff_to_invoke = {reinterpret_cast<const uint8_t*>(temp.data()) + sizeof(bucket_head2), temp.size() - sizeof(bucket_head2)};
} }
@ -584,10 +592,11 @@ public:
m_cache_in_buffer.erase(sizeof(bucket_head2)); m_cache_in_buffer.erase(sizeof(bucket_head2));
m_state = stream_state_body; m_state = stream_state_body;
m_oponent_protocol_ver = m_current_head.m_protocol_version; m_oponent_protocol_ver = m_current_head.m_protocol_version;
if(m_current_head.m_cb > max_packet_size) const size_t max_bytes = m_connection_context.get_max_bytes(m_current_head.m_command);
if(m_current_head.m_cb > std::min<size_t>(max_packet_size, max_bytes))
{ {
LOG_ERROR_CC(m_connection_context, "Maximum packet size exceed!, m_max_packet_size = " << max_packet_size LOG_ERROR_CC(m_connection_context, "Maximum packet size exceed!, m_max_packet_size = " << std::min<size_t>(max_packet_size, max_bytes)
<< ", packet header received " << m_current_head.m_cb << ", packet header received " << m_current_head.m_cb << ", command " << m_current_head.m_command
<< ", connection will be closed."); << ", connection will be closed.");
return false; return false;
} }

View file

@ -31,6 +31,7 @@
#pragma once #pragma once
#include <unordered_set> #include <unordered_set>
#include <atomic> #include <atomic>
#include <algorithm>
#include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/posix_time/posix_time.hpp>
#include "net/net_utils_base.h" #include "net/net_utils_base.h"
#include "copyable_atomic.h" #include "copyable_atomic.h"
@ -57,6 +58,27 @@ namespace cryptonote
bool handshake_complete() const noexcept { return m_state != state_before_handshake; } bool handshake_complete() const noexcept { return m_state != state_before_handshake; }
void set_max_bytes(int command, size_t bytes) {
const auto i = std::lower_bound(m_max_bytes.begin(), m_max_bytes.end(), std::make_pair(command, bytes), [](const std::pair<int, size_t> &e0, const std::pair<int, size_t> &e1){
return e0.first < e1.first;
});
if (i == m_max_bytes.end())
m_max_bytes.push_back(std::make_pair(command, bytes));
else if (i->first == command)
i->second = bytes;
else
m_max_bytes.insert(i, std::make_pair(command, bytes));
}
size_t get_max_bytes(int command) const {
const auto i = std::lower_bound(m_max_bytes.begin(), m_max_bytes.end(), std::make_pair(command, 0), [](const std::pair<int, size_t> &e0, const std::pair<int, size_t> &e1){
return e0.first < e1.first;
});
if (i == m_max_bytes.end() || i->first != command)
return std::numeric_limits<size_t>::max();
else
return i->second;
}
state m_state; state m_state;
std::vector<std::pair<crypto::hash, uint64_t>> m_needed_objects; std::vector<std::pair<crypto::hash, uint64_t>> m_needed_objects;
std::unordered_set<crypto::hash> m_requested_objects; std::unordered_set<crypto::hash> m_requested_objects;
@ -73,6 +95,7 @@ namespace cryptonote
int m_expect_response; int m_expect_response;
uint64_t m_expect_height; uint64_t m_expect_height;
size_t m_num_requested; size_t m_num_requested;
std::vector<std::pair<int, size_t>> m_max_bytes;
}; };
inline std::string get_protocol_state_string(cryptonote_connection_context::state s) inline std::string get_protocol_state_string(cryptonote_connection_context::state s)

View file

@ -110,6 +110,7 @@ namespace cryptonote
std::list<connection_info> get_connections(); std::list<connection_info> get_connections();
const block_queue &get_block_queue() const { return m_block_queue; } const block_queue &get_block_queue() const { return m_block_queue; }
void stop(); void stop();
void on_connection_new(cryptonote_connection_context &context);
void on_connection_close(cryptonote_connection_context &context); void on_connection_close(cryptonote_connection_context &context);
void set_max_out_peers(unsigned int max) { m_max_out_peers = max; } void set_max_out_peers(unsigned int max) { m_max_out_peers = max; }
bool no_sync() const { return m_no_sync; } bool no_sync() const { return m_no_sync; }

View file

@ -2873,6 +2873,25 @@ skip:
} }
//------------------------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------------------------
template<class t_core> template<class t_core>
void t_cryptonote_protocol_handler<t_core>::on_connection_new(cryptonote_connection_context &context)
{
context.set_max_bytes(nodetool::COMMAND_HANDSHAKE_T<cryptonote::CORE_SYNC_DATA>::ID, 65536);
context.set_max_bytes(nodetool::COMMAND_TIMED_SYNC_T<cryptonote::CORE_SYNC_DATA>::ID, 65536);
context.set_max_bytes(nodetool::COMMAND_PING::ID, 4096);
context.set_max_bytes(nodetool::COMMAND_REQUEST_SUPPORT_FLAGS::ID, 4096);
context.set_max_bytes(cryptonote::NOTIFY_NEW_BLOCK::ID, 1024 * 1024 * 128); // 128 MB (max packet is a bit less than 100 MB though)
context.set_max_bytes(cryptonote::NOTIFY_NEW_TRANSACTIONS::ID, 1024 * 1024 * 128); // 128 MB (max packet is a bit less than 100 MB though)
context.set_max_bytes(cryptonote::NOTIFY_REQUEST_GET_OBJECTS::ID, 1024 * 1024 * 2); // 2 MB
context.set_max_bytes(cryptonote::NOTIFY_RESPONSE_GET_OBJECTS::ID, 1024 * 1024 * 128); // 128 MB (max packet is a bit less than 100 MB though)
context.set_max_bytes(cryptonote::NOTIFY_REQUEST_CHAIN::ID, 512 * 1024); // 512 kB
context.set_max_bytes(cryptonote::NOTIFY_RESPONSE_CHAIN_ENTRY::ID, 1024 * 1024 * 4); // 4 MB
context.set_max_bytes(cryptonote::NOTIFY_NEW_FLUFFY_BLOCK::ID, 1024 * 1024 * 4); // 4 MB, but it does not includes transaction data
context.set_max_bytes(cryptonote::NOTIFY_REQUEST_FLUFFY_MISSING_TX::ID, 1024 * 1024); // 1 MB
context.set_max_bytes(cryptonote::NOTIFY_GET_TXPOOL_COMPLEMENT::ID, 1024 * 1024 * 4); // 4 MB
}
//------------------------------------------------------------------------------------------------------------------------
template<class t_core>
void t_cryptonote_protocol_handler<t_core>::on_connection_close(cryptonote_connection_context &context) void t_cryptonote_protocol_handler<t_core>::on_connection_close(cryptonote_connection_context &context)
{ {
uint64_t target = 0; uint64_t target = 0;

View file

@ -2633,6 +2633,7 @@ namespace nodetool
void node_server<t_payload_net_handler>::on_connection_new(p2p_connection_context& context) void node_server<t_payload_net_handler>::on_connection_new(p2p_connection_context& context)
{ {
MINFO("["<< epee::net_utils::print_connection_context(context) << "] NEW CONNECTION"); MINFO("["<< epee::net_utils::print_connection_context(context) << "] NEW CONNECTION");
m_payload_handler.on_connection_new(context);
} }
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
template<class t_payload_net_handler> template<class t_payload_net_handler>

View file

@ -54,6 +54,7 @@ namespace
{ {
static constexpr int handshake_command() noexcept { return 1001; } static constexpr int handshake_command() noexcept { return 1001; }
static constexpr bool handshake_complete() noexcept { return true; } static constexpr bool handshake_complete() noexcept { return true; }
size_t get_max_bytes(int command) const { return LEVIN_DEFAULT_MAX_PACKET_SIZE; }
}; };
typedef epee::levin::async_protocol_handler_config<test_levin_connection_context> test_levin_protocol_handler_config; typedef epee::levin::async_protocol_handler_config<test_levin_connection_context> test_levin_protocol_handler_config;

View file

@ -50,6 +50,7 @@ namespace net_load_tests
test_connection_context(): epee::net_utils::connection_context_base(boost::uuids::nil_uuid(), {}, false, false), m_closed(false) {} test_connection_context(): epee::net_utils::connection_context_base(boost::uuids::nil_uuid(), {}, false, false), m_closed(false) {}
static constexpr int handshake_command() noexcept { return 1001; } static constexpr int handshake_command() noexcept { return 1001; }
static constexpr bool handshake_complete() noexcept { return true; } static constexpr bool handshake_complete() noexcept { return true; }
size_t get_max_bytes(int command) const { return LEVIN_DEFAULT_MAX_PACKET_SIZE; }
volatile bool m_closed; volatile bool m_closed;
}; };

View file

@ -45,6 +45,7 @@ namespace
{ {
static constexpr int handshake_command() noexcept { return 1001; } static constexpr int handshake_command() noexcept { return 1001; }
static constexpr bool handshake_complete() noexcept { return true; } static constexpr bool handshake_complete() noexcept { return true; }
size_t get_max_bytes(int command) const { return LEVIN_DEFAULT_MAX_PACKET_SIZE; }
}; };
typedef epee::levin::async_protocol_handler_config<test_levin_connection_context> test_levin_protocol_handler_config; typedef epee::levin::async_protocol_handler_config<test_levin_connection_context> test_levin_protocol_handler_config;