2010-10-10 04:22:57 +02:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2006-2016, Arvid Norberg
|
2010-10-10 04:22:57 +02:00
|
|
|
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 author 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 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/kademlia/dos_blocker.hpp"
|
2010-10-10 04:22:57 +02:00
|
|
|
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-10 06:54:02 +02:00
|
|
|
#include "libtorrent/socket_io.hpp" // for print_address
|
|
|
|
#include "libtorrent/kademlia/dht_observer.hpp" // for dht_logger
|
2014-07-06 21:18:00 +02:00
|
|
|
#endif
|
2010-10-10 04:22:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
namespace libtorrent { namespace dht
|
2010-10-10 04:22:57 +02:00
|
|
|
{
|
2014-10-01 20:19:44 +02:00
|
|
|
dos_blocker::dos_blocker()
|
2015-01-02 00:24:21 +01:00
|
|
|
: m_message_rate_limit(5)
|
|
|
|
, m_block_timeout(5 * 60)
|
2014-10-01 20:19:44 +02:00
|
|
|
{
|
|
|
|
for (int i = 0; i < num_ban_nodes; ++i)
|
|
|
|
{
|
|
|
|
m_ban_nodes[i].count = 0;
|
|
|
|
m_ban_nodes[i].limit = min_time();
|
|
|
|
}
|
|
|
|
}
|
2010-10-10 04:22:57 +02:00
|
|
|
|
2015-05-10 06:54:02 +02:00
|
|
|
bool dos_blocker::incoming(address addr, time_point now, dht_logger* logger)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2016-07-09 22:26:26 +02:00
|
|
|
node_ban_entry* match = nullptr;
|
2014-07-06 21:18:00 +02:00
|
|
|
node_ban_entry* min = m_ban_nodes;
|
|
|
|
for (node_ban_entry* i = m_ban_nodes; i < m_ban_nodes + num_ban_nodes; ++i)
|
|
|
|
{
|
|
|
|
if (i->src == addr)
|
|
|
|
{
|
|
|
|
match = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i->count < min->count) min = i;
|
2014-08-28 04:06:21 +02:00
|
|
|
else if (i->count == min->count
|
|
|
|
&& i->limit < min->limit) min = i;
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
2010-10-10 04:22:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (match)
|
|
|
|
{
|
|
|
|
++match->count;
|
2014-08-28 10:06:11 +02:00
|
|
|
|
2015-01-02 00:24:21 +01:00
|
|
|
if (match->count >= m_message_rate_limit * 10)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
|
|
|
if (now < match->limit)
|
|
|
|
{
|
2015-01-02 00:24:21 +01:00
|
|
|
if (match->count == m_message_rate_limit * 10)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2015-05-16 21:29:49 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-10 06:54:02 +02:00
|
|
|
logger->log(dht_logger::tracker, "BANNING PEER [ ip: %s time: %f count: %d ]"
|
|
|
|
, print_address(addr).c_str()
|
2016-08-02 06:15:26 +02:00
|
|
|
, total_milliseconds((now - match->limit) + seconds(10)) / 1000.0
|
|
|
|
, match->count);
|
2010-10-10 04:22:57 +02:00
|
|
|
#endif
|
2015-01-02 00:24:21 +01:00
|
|
|
// we've received too many messages in less than 10 seconds
|
|
|
|
// from this node. Ignore it until it's silent for 5 minutes
|
|
|
|
match->limit = now + seconds(m_block_timeout);
|
2014-08-28 04:06:21 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-02 00:24:21 +01:00
|
|
|
// the messages we received from this peer took more than 10
|
|
|
|
// seconds. Reset the counter and the timer
|
2014-07-06 21:18:00 +02:00
|
|
|
match->count = 0;
|
2014-08-28 04:06:21 +02:00
|
|
|
match->limit = now + seconds(10);
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
min->count = 1;
|
2014-08-28 04:06:21 +02:00
|
|
|
min->limit = now + seconds(10);
|
2014-07-06 21:18:00 +02:00
|
|
|
min->src = addr;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2015-01-02 00:24:21 +01:00
|
|
|
}}
|
2010-10-10 04:22:57 +02:00
|
|
|
|