diff options
author | Miguel Herranz <miguel@ipglider.org> | 2017-01-14 13:21:20 +0100 |
---|---|---|
committer | Miguel Herranz <miguel@ipglider.org> | 2017-01-15 12:11:27 +0100 |
commit | 3f269e988c9dae849208dca4167cbadbcd1ab90d (patch) | |
tree | da7a6ea6569635a8ee08ec2f3c31c50c27cf783e /src/p2p/net_node.inl | |
parent | Merge pull request #1559 (diff) | |
download | monero-3f269e988c9dae849208dca4167cbadbcd1ab90d.tar.xz |
Limit incoming connections from the same IP
Diffstat (limited to 'src/p2p/net_node.inl')
-rw-r--r-- | src/p2p/net_node.inl | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index f32e7a435..e9847b64d 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -1457,6 +1457,14 @@ namespace nodetool drop_connection(context); return 1; } + + if(has_too_many_connections(context.m_remote_ip)) + { + LOG_PRINT_CCONTEXT_L1("CONNECTION FROM " << epee::string_tools::get_ip_string_from_int32(context.m_remote_ip) << " 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; @@ -1677,4 +1685,26 @@ namespace nodetool return true; } + + template<class t_payload_net_handler> + bool node_server<t_payload_net_handler>::has_too_many_connections(const uint32_t ip) + { + const uint8_t max_connections = 3; + uint8_t count = 0; + + m_net_server.get_config_object().foreach_connection([&](const p2p_connection_context& cntxt) + { + if (cntxt.m_is_income && cntxt.m_remote_ip == ip) { + count++; + + if (count > max_connections) { + return false; + } + } + + return true; + }); + + return count > max_connections; + } } |