diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/p2p/net_node.h | 6 | ||||
| -rw-r--r-- | src/p2p/net_node.inl | 42 |
2 files changed, 30 insertions, 18 deletions
diff --git a/src/p2p/net_node.h b/src/p2p/net_node.h index 98d8ecfff..cbdeef7e3 100644 --- a/src/p2p/net_node.h +++ b/src/p2p/net_node.h @@ -125,7 +125,8 @@ namespace nodetool template<class t_payload_net_handler> class node_server: public epee::levin::levin_commands_handler<p2p_connection_context_t<typename t_payload_net_handler::connection_context> >, public i_p2p_endpoint<typename t_payload_net_handler::connection_context>, - public epee::net_utils::i_connection_filter + public epee::net_utils::i_connection_filter, + public epee::net_utils::i_connection_limit { struct by_conn_id{}; struct by_peer_id{}; @@ -351,7 +352,10 @@ namespace nodetool virtual bool add_host_fail(const epee::net_utils::network_address &address, unsigned int score = 1); //----------------- i_connection_filter -------------------------------------------------------- virtual bool is_remote_host_allowed(const epee::net_utils::network_address &address, time_t *t = NULL); + //----------------- i_connection_limit --------------------------------------------------------- + virtual bool is_host_limit(const epee::net_utils::network_address &address); //----------------------------------------------------------------------------------------------- + bool parse_peer_from_string(epee::net_utils::network_address& pe, const std::string& node_addr, uint16_t default_port = 0); bool handle_command_line( const boost::program_options::variables_map& vm diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index 284de3c6d..51825d1a5 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -228,6 +228,26 @@ namespace nodetool } //----------------------------------------------------------------------------------- template<class t_payload_net_handler> + bool node_server<t_payload_net_handler>::is_host_limit(const epee::net_utils::network_address &address) + { + const network_zone& zone = m_network_zones.at(address.get_zone()); + if (zone.m_current_number_of_in_peers >= zone.m_config.m_net_config.max_in_connection_count) // in peers limit + { + MWARNING("Exceeded max incoming connections, so dropping this one."); + return true; + } + + if(has_too_many_connections(address)) + { + MWARNING("CONNECTION FROM " << address.host_str() << " REFUSED, too many connections from the same address"); + return true; + } + + return false; + } + + //----------------------------------------------------------------------------------- + template<class t_payload_net_handler> bool node_server<t_payload_net_handler>::block_host(epee::net_utils::network_address addr, time_t seconds, bool add_only) { if(!addr.is_blockable()) @@ -981,6 +1001,7 @@ namespace nodetool std::string ipv6_addr = ""; std::string ipv6_port = ""; zone.second.m_net_server.set_connection_filter(this); + zone.second.m_net_server.set_connection_limit(this); MINFO("Binding (IPv4) on " << zone.second.m_bind_ip << ":" << zone.second.m_port); if (!zone.second.m_bind_ipv6_address.empty() && m_use_ipv6) { @@ -2560,13 +2581,6 @@ namespace nodetool return 1; } - if (zone.m_current_number_of_in_peers >= zone.m_config.m_net_config.max_in_connection_count) // in peers limit - { - LOG_WARNING_CC(context, "COMMAND_HANDSHAKE came, but already have max incoming connections, so dropping this one."); - drop_connection(context); - return 1; - } - if(!m_payload_handler.process_payload_sync_data(arg.payload_data, context, true)) { LOG_WARNING_CC(context, "COMMAND_HANDSHAKE came, but process_payload_sync_data returned false, dropping connection."); @@ -2576,13 +2590,6 @@ namespace nodetool zone.m_notifier.on_handshake_complete(context.m_connection_id, context.m_is_income); - if(has_too_many_connections(context.m_remote_address)) - { - LOG_PRINT_CCONTEXT_L1("CONNECTION FROM " << context.m_remote_address.host_str() << " REFUSED, too many connections from the same address"); - drop_connection(context); - return 1; - } - //associate peer_id with this connection context.peer_id = arg.node_data.peer_id; context.m_in_timedsync = false; @@ -2902,15 +2909,16 @@ namespace nodetool if (cntxt.m_is_income && cntxt.m_remote_address.is_same_host(address)) { count++; - if (count > max_connections) { + // the only call location happens BEFORE foreach_connection list is updated + if (count >= max_connections) { return false; } } return true; }); - - return count > max_connections; + // the only call location happens BEFORE foreach_connection list is updated + return count >= max_connections; } template<class t_payload_net_handler> |
