From 8549840d5a14d737ec828c92862bfa1f8433d0dd Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 26 Mar 2012 03:57:15 +0000 Subject: [PATCH] add debug option to log hash failures --- Makefile.am | 2 +- src/smart_ban.cpp | 73 +++++++++++++++++++++++++- tools/Makefile.am | 20 +++++++ tools/parse_hash_fails.cpp | 103 +++++++++++++++++++++++++++++++++++++ 4 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 tools/Makefile.am create mode 100644 tools/parse_hash_fails.cpp diff --git a/Makefile.am b/Makefile.am index 5738c2b6b..ae9e8b851 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,7 +2,7 @@ ACLOCAL_AMFLAGS = -I m4 #DISTCHECK_CONFIGURE_FLAGS = --enable-tests -SUBDIRS = include/libtorrent src examples test bindings +SUBDIRS = include/libtorrent src examples test bindings tools DOCS_IMAGES = \ docs/arctic_thumb.png \ diff --git a/src/smart_ban.cpp b/src/smart_ban.cpp index f6a967ce8..15d2bb293 100644 --- a/src/smart_ban.cpp +++ b/src/smart_ban.cpp @@ -61,6 +61,55 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/peer_info.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 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 { class torrent; @@ -74,8 +123,16 @@ namespace : m_torrent(t) , 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) { #ifdef TORRENT_LOGGING @@ -190,6 +247,11 @@ namespace policy::peer* p = (*range.first); 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::iterator i = m_block_hashes.lower_bound(b); 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)); sha1_hash ok_digest = h.final(); + policy::peer* p = b.second.peer; + 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 (!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 // that is forged to match the CRC of the good data. int m_salt; + +#ifdef TORRENT_LOG_HASH_FAILURES + FILE* m_log_file; +#endif }; } } diff --git a/tools/Makefile.am b/tools/Makefile.am new file mode 100644 index 000000000..e93b856a1 --- /dev/null +++ b/tools/Makefile.am @@ -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@ diff --git a/tools/parse_hash_fails.cpp b/tools/parse_hash_fails.cpp new file mode 100644 index 000000000..876046572 --- /dev/null +++ b/tools/parse_hash_fails.cpp @@ -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 +#include +#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; +} + +