add debug option to log hash failures

This commit is contained in:
Arvid Norberg 2012-03-26 03:57:15 +00:00
parent 2be6f990b4
commit 8549840d5a
4 changed files with 196 additions and 2 deletions

View File

@ -2,7 +2,7 @@ ACLOCAL_AMFLAGS = -I m4
#DISTCHECK_CONFIGURE_FLAGS = --enable-tests #DISTCHECK_CONFIGURE_FLAGS = --enable-tests
SUBDIRS = include/libtorrent src examples test bindings SUBDIRS = include/libtorrent src examples test bindings tools
DOCS_IMAGES = \ DOCS_IMAGES = \
docs/arctic_thumb.png \ docs/arctic_thumb.png \

View File

@ -61,6 +61,55 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/peer_info.hpp" #include "libtorrent/peer_info.hpp"
#include "libtorrent/random.hpp" #include "libtorrent/random.hpp"
//#define TORRENT_LOG_HASH_FAILURES
#ifdef TORRENT_LOG_HASH_FAILURES
#include "libtorrent/peer_id.hpp" // sha1_hash
#include "libtorrent/escape_string.hpp" // to_hex
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);
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
, "%s[%"PRId64",%d]", libtorrent::filename(fn).c_str(), files[i].offset, int(files[i].size));
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 { namespace libtorrent {
class torrent; class torrent;
@ -74,8 +123,16 @@ namespace
: m_torrent(t) : m_torrent(t)
, m_salt(random()) , m_salt(random())
{ {
#ifdef TORRENT_LOG_HASH_FAILURES
m_log_file = NULL;
#endif
} }
#ifdef TORRENT_LOG_HASH_FAILURES
~smart_ban_plugin()
{ fclose(m_log_file); }
#endif
void on_piece_pass(int p) void on_piece_pass(int p)
{ {
#ifdef TORRENT_LOGGING #ifdef TORRENT_LOGGING
@ -190,6 +247,11 @@ namespace
policy::peer* p = (*range.first); policy::peer* p = (*range.first);
block_entry e = {p, h.final()}; block_entry e = {p, h.final()};
#ifdef TORRENT_LOG_HASH_FAILURES
log_hash_block(&m_log_file, m_torrent, b.piece_index
, b.block_index, p->address(), j.buffer, j.buffer_size, true);
#endif
std::map<piece_block, block_entry>::iterator i = m_block_hashes.lower_bound(b); std::map<piece_block, block_entry>::iterator i = m_block_hashes.lower_bound(b);
if (i != m_block_hashes.end() && i->first == b && i->second.peer == p) if (i != m_block_hashes.end() && i->first == b && i->second.peer == p)
@ -263,9 +325,14 @@ namespace
h.update((char const*)&m_salt, sizeof(m_salt)); h.update((char const*)&m_salt, sizeof(m_salt));
sha1_hash ok_digest = h.final(); sha1_hash ok_digest = h.final();
policy::peer* p = b.second.peer;
if (b.second.digest == ok_digest) return; if (b.second.digest == ok_digest) return;
policy::peer* p = b.second.peer; #ifdef TORRENT_LOG_HASH_FAILURES
log_hash_block(&m_log_file, m_torrent, b.first.piece_index
, b.first.block_index, p->address(), j.buffer, j.buffer_size, false);
#endif
if (p == 0) return; if (p == 0) return;
if (!m_torrent.get_policy().has_peer(p)) return; if (!m_torrent.get_policy().has_peer(p)) return;
@ -302,6 +369,10 @@ namespace
// the salt is required to avoid attacks where bad data is sent // the salt is required to avoid attacks where bad data is sent
// that is forged to match the CRC of the good data. // that is forged to match the CRC of the good data.
int m_salt; int m_salt;
#ifdef TORRENT_LOG_HASH_FAILURES
FILE* m_log_file;
#endif
}; };
} } } }

20
tools/Makefile.am Normal file
View File

@ -0,0 +1,20 @@
tool_programs = \
parse_hash_fails
if ENABLE_EXAMPLES
bin_PROGRAMS = $(tool_programs)
endif
EXTRA_PROGRAMS = $(tool_programs)
EXTRA_DIST = Jamfile
parse_hash_fails_SOURCES = parse_hash_fails.cpp
#client_test_LDADD = $(top_builddir)/src/libtorrent-rasterbar.la
LDADD = $(top_builddir)/src/libtorrent-rasterbar.la
AM_CPPFLAGS = -ftemplate-depth-50 -I$(top_srcdir)/include @DEBUGFLAGS@
#AM_LDFLAGS = ${LDLAGS} -L./ @BOOST_SYSTEM_LIB@ @OPENSSL_LDFLAGS@ @OPENSSL_LIBS@
#AM_LDFLAGS = $(LDFLAGS) @BOOST_SYSTEM_LIB@ @OPENSSL_LDFLAGS@ @OPENSSL_LIBS@
#AM_LDFLAGS = @OPENSSL_LDFLAGS@

103
tools/parse_hash_fails.cpp Normal file
View File

@ -0,0 +1,103 @@
/*
Copyright (c) 2012, 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.
*/
#include <stdlib.h>
#include <string>
#include "libtorrent/file.hpp"
using namespace libtorrent;
void print_usage()
{
fprintf(stderr, "usage: parse_hash_fails log-directory\n");
exit(1);
}
int main(int argc, char* argv[])
{
if (argc != 2) print_usage();
error_code ec;
directory d(argv[1], ec);
if (ec)
{
fprintf(stderr, "failed to open directory: %s\n%s\n"
, argv[1], ec.message().c_str());
return 1;
}
for (; !d.done(); d.next(ec))
{
if (ec)
{
fprintf(stderr, "error listing directory: %s\n", ec.message().c_str());
return 1;
}
std::string filename = d.file();
char hash[41];
int piece;
int block;
char state[10]; // good, bad
if (sscanf(filename.c_str(), "%40s_%d_%d_%4s.block", hash, &piece, &block, state) != 4)
{
fprintf(stderr, "no match: %s\n", filename.c_str());
continue;
}
if (strcmp(state, "good") != 0) continue;
char target_filename[1024];
snprintf(target_filename, sizeof(target_filename), "%s_%d_%d.diff", hash, piece, block);
fprintf(stderr, "diffing %s\n", filename.c_str());
char cmdline[2048];
snprintf(cmdline, sizeof(cmdline), "xxd %s >temp_good", combine_path(argv[1], filename).c_str());
system(cmdline);
snprintf(cmdline, sizeof(cmdline), "xxd %s/%s_%d_%d_bad.block >temp_bad", argv[1], hash, piece, block);
system(cmdline);
snprintf(cmdline, sizeof(cmdline), "diff -y temp_good temp_bad | colordiff >%s"
, combine_path(argv[1], target_filename).c_str());
system(cmdline);
}
FILE* log_file = fopen(argv[1], "r");
if (log_file == 0)
{
fprintf(stderr, "failed to open logfile: %s\n%d: %s\n"
, argv[1], errno, strerror(errno));
return 1;
}
return 0;
}