premiere-libtorrent/src/smart_ban.cpp

409 lines
12 KiB
C++
Raw Normal View History

/*
Copyright (c) 2007-2016, Arvid Norberg
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.
*/
2011-01-29 13:13:49 +01:00
#ifndef TORRENT_DISABLE_EXTENSIONS
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <vector>
#include <map>
#include <utility>
#include <numeric>
#include <cstdio>
#include "libtorrent/aux_/disable_warnings_pop.hpp"
#include "libtorrent/hasher.hpp"
#include "libtorrent/torrent.hpp"
#include "libtorrent/torrent_handle.hpp"
#include "libtorrent/extensions.hpp"
#include "libtorrent/extensions/smart_ban.hpp"
#include "libtorrent/disk_io_thread.hpp"
#include "libtorrent/aux_/session_impl.hpp"
2009-11-23 18:05:26 +01:00
#include "libtorrent/peer_connection.hpp"
2009-11-26 22:05:57 +01:00
#include "libtorrent/peer_info.hpp"
#include "libtorrent/random.hpp"
#include "libtorrent/operations.hpp" // for operation_t enum
2012-03-26 05:57:15 +02:00
//#define TORRENT_LOG_HASH_FAILURES
#ifndef TORRENT_DISABLE_LOGGING
2014-07-06 21:18:00 +02:00
#include "libtorrent/socket_io.hpp"
#endif
2012-03-26 05:57:15 +02:00
#ifdef TORRENT_LOG_HASH_FAILURES
#include "libtorrent/peer_id.hpp" // sha1_hash
#include "libtorrent/hex.hpp" // to_hex
2014-07-06 21:18:00 +02:00
#include "libtorrent/socket_io.hpp"
2012-03-26 05:57:15 +02:00
void log_hash_block(FILE** f, libtorrent::torrent const& t, int piece, int block
, libtorrent::address a, char const* bytes, int len, bool corrupt)
{
using namespace libtorrent;
mkdir("hash_failures", 0755);
if (*f == NULL)
{
char filename[1024];
snprintf(filename, sizeof(filename), "hash_failures/%s.log"
, to_hex(t.info_hash().to_string()).c_str());
*f = fopen(filename, "w");
}
file_storage const& fs = t.torrent_file().files();
std::vector<file_slice> files = fs.map_block(piece, block * 0x4000, len);
2015-08-09 04:53:11 +02:00
2012-03-26 05:57:15 +02:00
std::string fn = fs.file_path(fs.internal_at(files[0].file_index));
char filename[4094];
int offset = 0;
for (int i = 0; i < files.size(); ++i)
{
offset += snprintf(filename+offset, sizeof(filename)-offset
2015-08-09 04:53:11 +02:00
, "%s[%" PRId64 ",%d]", libtorrent::filename(fn).c_str(), files[i].offset, int(files[i].size));
2012-03-26 05:57:15 +02:00
if (offset >= sizeof(filename)) break;
}
fprintf(*f, "%s\t%04d\t%04d\t%s\t%s\t%s\n", to_hex(t.info_hash().to_string()).c_str(), piece
, block, corrupt ? " bad" : "good", print_address(a).c_str(), filename);
snprintf(filename, sizeof(filename), "hash_failures/%s_%d_%d_%s.block"
, to_hex(t.info_hash().to_string()).c_str(), piece, block, corrupt ? "bad" : "good");
FILE* data = fopen(filename, "w+");
fwrite(bytes, 1, len, data);
fclose(data);
}
#endif
namespace libtorrent {
class torrent;
namespace
{
struct smart_ban_plugin TORRENT_FINAL
: torrent_plugin
, boost::enable_shared_from_this<smart_ban_plugin>
{
smart_ban_plugin(torrent& t)
: m_torrent(t)
, m_salt(random())
{
2012-03-26 05:57:15 +02:00
#ifdef TORRENT_LOG_HASH_FAILURES
m_log_file = NULL;
#endif
}
2012-03-26 05:57:15 +02:00
#ifdef TORRENT_LOG_HASH_FAILURES
~smart_ban_plugin()
{ fclose(m_log_file); }
#endif
virtual void on_piece_pass(int p) TORRENT_OVERRIDE
{
#ifndef TORRENT_DISABLE_LOGGING
2014-07-06 21:18:00 +02:00
m_torrent.debug_log(" PIECE PASS [ p: %d | block_hash_size: %d ]"
, p, int(m_block_hashes.size()));
#endif
// has this piece failed earlier? If it has, go through the
// CRCs from the time it failed and ban the peers that
// sent bad blocks
2010-03-16 07:14:22 +01:00
std::map<piece_block, block_entry>::iterator i = m_block_hashes.lower_bound(piece_block(p, 0));
if (i == m_block_hashes.end() || int(i->first.piece_index) != p) return;
int size = m_torrent.torrent_file().piece_size(p);
peer_request r = {p, 0, (std::min)(16*1024, size)};
piece_block pb(p, 0);
while (size > 0)
{
if (i->first.block_index == pb.block_index)
{
2014-07-06 21:18:00 +02:00
m_torrent.session().disk_thread().async_read(&m_torrent.storage()
, r, boost::bind(&smart_ban_plugin::on_read_ok_block
2015-08-11 02:03:24 +02:00
, shared_from_this(), *i, i->second.peer->address(), _1)
, reinterpret_cast<void*>(1));
2010-03-16 07:14:22 +01:00
m_block_hashes.erase(i++);
}
else
{
TORRENT_ASSERT(i->first.block_index > pb.block_index);
}
if (i == m_block_hashes.end() || int(i->first.piece_index) != p)
break;
r.start += 16*1024;
size -= 16*1024;
r.length = (std::min)(16*1024, size);
++pb.block_index;
}
#ifndef NDEBUG
// make sure we actually removed all the entries for piece 'p'
2010-03-16 07:14:22 +01:00
i = m_block_hashes.lower_bound(piece_block(p, 0));
TORRENT_ASSERT(i == m_block_hashes.end() || int(i->first.piece_index) != p);
#endif
if (m_torrent.is_seed())
{
2010-03-16 07:14:22 +01:00
std::map<piece_block, block_entry>().swap(m_block_hashes);
return;
}
}
virtual void on_piece_failed(int p) TORRENT_OVERRIDE
{
// The piece failed the hash check. Record
// the CRC and origin peer of every block
// if the torrent is aborted, no point in starting
// a bunch of read operations on it
if (m_torrent.is_aborted()) return;
std::vector<torrent_peer*> downloaders;
m_torrent.picker().get_downloaders(downloaders, p);
int size = m_torrent.torrent_file().piece_size(p);
peer_request r = {p, 0, (std::min)(16*1024, size)};
piece_block pb(p, 0);
for (std::vector<torrent_peer*>::iterator i = downloaders.begin()
, end(downloaders.end()); i != end; ++i)
{
if (*i != NULL)
{
2014-07-06 21:18:00 +02:00
// for very sad and involved reasons, this read need to force a copy out of the cache
// since the piece has failed, this block is very likely to be replaced with a newly
// downloaded one very soon, and to get a block by reference would fail, since the
// block read will have been deleted by the time it gets back to the network thread
m_torrent.session().disk_thread().async_read(&m_torrent.storage(), r
, boost::bind(&smart_ban_plugin::on_read_failed_block
, shared_from_this(), pb, (*i)->address(), _1)
, reinterpret_cast<torrent_peer*>(1)
2014-07-06 21:18:00 +02:00
, disk_io_job::force_copy);
}
r.start += 16*1024;
size -= 16*1024;
r.length = (std::min)(16*1024, size);
++pb.block_index;
}
TORRENT_ASSERT(size <= 0);
}
private:
// this entry ties a specific block CRC to
// a peer.
struct block_entry
{
2014-07-06 21:18:00 +02:00
torrent_peer* peer;
2010-03-16 07:14:22 +01:00
sha1_hash digest;
};
2014-07-06 21:18:00 +02:00
void on_read_failed_block(piece_block b, address a, disk_io_job const* j)
{
2014-07-06 21:18:00 +02:00
TORRENT_ASSERT(m_torrent.session().is_single_thread());
2015-08-11 02:03:24 +02:00
2014-07-06 21:18:00 +02:00
disk_buffer_holder buffer(m_torrent.session(), *j);
// ignore read errors
2014-07-06 21:18:00 +02:00
if (j->ret != j->d.io.buffer_size) return;
2010-03-16 07:14:22 +01:00
hasher h;
h.update(j->buffer.disk_block, j->d.io.buffer_size);
2015-08-11 02:03:24 +02:00
h.update(reinterpret_cast<char const*>(&m_salt), sizeof(m_salt));
2014-10-26 08:34:31 +01:00
std::pair<peer_list::iterator, peer_list::iterator> range
2014-07-06 21:18:00 +02:00
= m_torrent.find_peers(a);
2009-02-25 06:53:24 +01:00
// there is no peer with this address anymore
if (range.first == range.second) return;
2014-07-06 21:18:00 +02:00
torrent_peer* p = (*range.first);
2010-03-16 07:14:22 +01:00
block_entry e = {p, h.final()};
2009-02-25 06:53:24 +01:00
2012-03-26 05:57:15 +02:00
#ifdef TORRENT_LOG_HASH_FAILURES
log_hash_block(&m_log_file, m_torrent, b.piece_index
, b.block_index, p->address(), j->buffer.disk_block, j->buffer_size, true);
2012-03-26 05:57:15 +02:00
#endif
2010-03-16 07:14:22 +01:00
std::map<piece_block, block_entry>::iterator i = m_block_hashes.lower_bound(b);
2009-02-25 06:53:24 +01:00
2010-03-16 07:14:22 +01:00
if (i != m_block_hashes.end() && i->first == b && i->second.peer == p)
{
// this peer has sent us this block before
2014-07-06 21:18:00 +02:00
// if the peer is already banned, it doesn't matter if it sent
// good or bad data. Nothings going to change it
if (!p->banned && i->second.digest != e.digest)
{
2010-03-16 07:14:22 +01:00
// this time the digest of the block is different
// from the first time it sent it
// at least one of them must be bad
#ifndef TORRENT_DISABLE_LOGGING
char const* client = "-";
peer_info info;
if (p->connection)
{
p->connection->get_peer_info(info);
client = info.client.c_str();
}
2014-07-06 21:18:00 +02:00
m_torrent.debug_log(" BANNING PEER [ p: %d | b: %d | c: %s"
" | hash1: %s | hash2: %s | ip: %s ]"
, b.piece_index, b.block_index, client
, to_hex(i->second.digest.to_string()).c_str()
, to_hex(e.digest.to_string()).c_str()
, print_endpoint(p->ip()).c_str());
#endif
2014-07-06 21:18:00 +02:00
m_torrent.ban_peer(p);
if (p->connection) p->connection->disconnect(
errors::peer_banned, op_bittorrent);
}
// we already have this exact entry in the map
// we don't have to insert it
return;
}
2010-03-16 07:14:22 +01:00
m_block_hashes.insert(i, std::pair<const piece_block, block_entry>(b, e));
#ifndef TORRENT_DISABLE_LOGGING
char const* client = "-";
peer_info info;
if (p->connection)
{
p->connection->get_peer_info(info);
client = info.client.c_str();
}
2014-07-06 21:18:00 +02:00
m_torrent.debug_log(" STORE BLOCK CRC [ p: %d | b: %d | c: %s"
" | digest: %s | ip: %s ]"
, b.piece_index, b.block_index, client
, to_hex(e.digest.to_string()).c_str()
, print_address(p->ip().address()).c_str());
#endif
}
2014-07-06 21:18:00 +02:00
void on_read_ok_block(std::pair<piece_block, block_entry> b, address a, disk_io_job const* j)
{
2014-07-06 21:18:00 +02:00
TORRENT_ASSERT(m_torrent.session().is_single_thread());
2014-07-06 21:18:00 +02:00
disk_buffer_holder buffer(m_torrent.session(), *j);
// ignore read errors
2014-07-06 21:18:00 +02:00
if (j->ret != j->d.io.buffer_size) return;
2010-03-16 07:14:22 +01:00
hasher h;
h.update(j->buffer.disk_block, j->d.io.buffer_size);
2015-08-11 02:03:24 +02:00
h.update(reinterpret_cast<char const*>(&m_salt), sizeof(m_salt));
2010-03-16 07:14:22 +01:00
sha1_hash ok_digest = h.final();
2010-03-16 07:14:22 +01:00
if (b.second.digest == ok_digest) return;
2012-03-26 05:57:15 +02:00
#ifdef TORRENT_LOG_HASH_FAILURES
log_hash_block(&m_log_file, m_torrent, b.first.piece_index
, b.first.block_index, a, j->buffer.disk_block, j->buffer_size, false);
2012-03-26 05:57:15 +02:00
#endif
2014-07-06 21:18:00 +02:00
// find the peer
2014-10-26 08:34:31 +01:00
std::pair<peer_list::iterator, peer_list::iterator> range
2014-07-06 21:18:00 +02:00
= m_torrent.find_peers(a);
if (range.first == range.second) return;
torrent_peer* p = NULL;
for (; range.first != range.second; ++range.first)
{
if (b.second.peer != *range.first) continue;
p = *range.first;
}
if (p == NULL) return;
#ifndef TORRENT_DISABLE_LOGGING
char const* client = "-";
peer_info info;
if (p->connection)
{
p->connection->get_peer_info(info);
client = info.client.c_str();
}
2014-07-06 21:18:00 +02:00
m_torrent.debug_log(" BANNING PEER [ p: %d | b: %d | c: %s"
" | ok_digest: %s | bad_digest: %s | ip: %s ]"
, b.first.piece_index, b.first.block_index, client
, to_hex(ok_digest.to_string()).c_str()
, to_hex(b.second.digest.to_string()).c_str()
, print_address(p->ip().address()).c_str());
#endif
2014-07-06 21:18:00 +02:00
m_torrent.ban_peer(p);
if (p->connection) p->connection->disconnect(
errors::peer_banned, op_bittorrent);
}
torrent& m_torrent;
// This table maps a piece_block (piece and block index
// pair) to a peer and the block CRC. The CRC is calculated
// from the data in the block + the salt
2010-03-16 07:14:22 +01:00
std::map<piece_block, block_entry> m_block_hashes;
// This salt is a random value used to calculate the block CRCs
// Since the CRC function that is used is not a one way function
// the salt is required to avoid attacks where bad data is sent
// that is forged to match the CRC of the good data.
int m_salt;
2012-03-26 05:57:15 +02:00
#ifdef TORRENT_LOG_HASH_FAILURES
FILE* m_log_file;
#endif
// explicitly disallow assignment, to silence msvc warning
smart_ban_plugin& operator=(smart_ban_plugin const&);
};
} }
namespace libtorrent
{
boost::shared_ptr<torrent_plugin> create_smart_ban_plugin(torrent_handle const& th, void*)
{
2015-07-22 04:11:41 +02:00
torrent* t = th.native_handle().get();
return boost::shared_ptr<torrent_plugin>(new smart_ban_plugin(*t));
}
}
2011-01-29 13:13:49 +01:00
#endif