diff --git a/Jamfile b/Jamfile index 8decfb830..9f33d4ef3 100755 --- a/Jamfile +++ b/Jamfile @@ -284,6 +284,9 @@ feature invariant-checks : on off full : composite propagated link-incompatible feature.compose off : TORRENT_DISABLE_INVARIANT_CHECKS ; feature.compose full : TORRENT_EXPENSIVE_INVARIANT_CHECKS ; +feature request-log : off on : composite propagated link-incompatible ; +feature.compose on : TORRENT_REQUEST_LOGGING ; + feature disk-stats : off on : composite propagated link-incompatible ; feature.compose on : TORRENT_DISK_STATS ; diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 5c21120c8..c0d357f82 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -907,6 +907,11 @@ namespace libtorrent int m_buffer_allocations; #endif +#ifdef TORRENT_REQUEST_LOGGING + // used to log all requests from peers + FILE* m_request_log; +#endif + #ifdef TORRENT_STATS void rotate_stats_log(); void print_log_line(int tick_interval_ms, ptime now); diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 6aff65bf0..7400ea9ba 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -73,6 +73,36 @@ namespace libtorrent return ((v & 7) == 0) ? v : v + (8 - (v & 7)); } +#if defined TORRENT_REQUEST_LOGGING + void write_request_log(FILE* f, sha1_hash const& ih + , peer_connection* p, peer_request const& r) + { + // the event format in the log is: + // uint64_t timestamp (microseconds) + // uint64_t info-hash prefix + // uint32_t peer identifier + // uint32_t piece + // uint32_t start offset + // uint32_t length + char event[32]; + char* ptr = event; + detail::write_uint64(total_microseconds((time_now_hires() - min_time())), ptr); + memcpy(ptr, &ih[0], 8); + ptr += 8; + detail::write_uint32(boost::uint32_t(p), ptr); + detail::write_uint32(r.piece, ptr); + detail::write_uint32(r.start, ptr); + detail::write_uint32(r.length, ptr); + + int ret = fwrite(event, 1, sizeof(event), f); + if (ret != sizeof(event)) + { + fprintf(stderr, "ERROR writing to request log: (%d) %s\n" + , errno, strerror(errno)); + } + } +#endif + // outbound connection peer_connection::peer_connection( session_impl& ses @@ -2103,6 +2133,10 @@ namespace libtorrent { m_choke_rejects = 0; m_requests.push_back(r); +#ifdef TORRENT_REQUEST_LOGGING + if (m_ses.m_request_log) + write_request_log(m_ses.m_request_log, t->info_hash(), this, r); +#endif m_last_incoming_request = time_now(); fill_send_buffer(); } diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 9c0ef45f8..cf2dde230 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -628,6 +628,16 @@ namespace aux { m_disk_queues[0] = 0; m_disk_queues[1] = 0; +#ifdef TORRENT_REQUEST_LOGGING + char log_filename[200]; + snprintf(log_filename, sizeof(log_filename), "requests-%d.log", getpid()); + m_request_log = fopen(log_filename, "w+"); + if (m_request_log == 0) + { + fprintf(stderr, "failed to open request log file: (%d) %s\n", errno, strerror(errno)); + } +#endif + #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING m_logger = create_log("main_session", listen_port(), false); (*m_logger) << time_now_string() << "\n"; @@ -5072,6 +5082,10 @@ namespace aux { TORRENT_ASSERT(m_connections.empty()); TORRENT_ASSERT(m_connections.empty()); +#ifdef TORRENT_REQUEST_LOGGING + if (m_request_log) fclose(m_request_log); +#endif + #ifdef TORRENT_STATS if (m_stats_logger) fclose(m_stats_logger); #endif