From 0032bd86012d07cc1f0ac6d712c9023839a1cda7 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 25 Oct 2003 01:31:06 +0000 Subject: [PATCH] some cleanup, fixes, added documentation and added some configuration options. --- docs/index.html | 28 +++++++- examples/client_test.cpp | 28 ++------ include/libtorrent/debug.hpp | 24 ++++--- include/libtorrent/peer_connection.hpp | 3 +- include/libtorrent/session.hpp | 51 ++++++++------- include/libtorrent/stat.hpp | 2 + src/peer_connection.cpp | 90 +++++++++++++------------- src/session.cpp | 73 +++++++++++++-------- src/torrent.cpp | 9 +-- 9 files changed, 165 insertions(+), 143 deletions(-) diff --git a/docs/index.html b/docs/index.html index 403bf09c7..4c82d226f 100755 --- a/docs/index.html +++ b/docs/index.html @@ -572,6 +572,10 @@ in the torrent. Each boolean tells you if the peer has that piece (if it's set t or if the peer miss that piece (set to false).

+

+TODO: address +

+

http_settings

@@ -590,11 +594,33 @@ struct http_settings std::string proxy_login; std::string proxy_password; std::string user_agent; + int tracker_timeout; + int tracker_maximum_response_length; + char fingerprint[4]; };

-TODO: address +tracker_timeout is the number of seconds the tracker connection will +wait until it considers the tracker to have timed-out. Default value is 30 +seconds. +

+ +

+tracker_maximum_response_length is the maximum number of bytes in a +tracker response. If a response size passes this number it will be rejected +and the connection will be closed. On gzipped responses this size is measured +on the uncompressed data. So, if you get 20 bytes of gzip response that'll +expand to 2 megs, it will be interrupted before the entire response has been +uncompressed (given your limit is lower than 2 megs). Default limit is +1 megabyte. +

+ +

+fingerprint is a short string that will be used in the peer_id to +identify the client. If you want your fingerprint to be shorter than 4 +characters, you can terminate the string with a null. The default is an +empty string.

big_number

diff --git a/examples/client_test.cpp b/examples/client_test.cpp index 83cf1a392..bde29fdd9 100755 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -41,25 +41,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/http_settings.hpp" -#ifndef NDEBUG -struct cout_logger: libtorrent::logger -{ -public: - virtual void log(const char* text) { std::cout << text; } - virtual void clear() {} -}; - -struct cout_log_creator: libtorrent::log_spawner -{ - virtual libtorrent::logger* create_logger(const char* title) - { - cout_logger* log = new cout_logger(); - return log; - } -}; - -#endif - int main(int argc, char* argv[]) { using namespace libtorrent; @@ -78,15 +59,14 @@ int main(int argc, char* argv[]) // settings.proxy_password = "foobar"; settings.user_agent = "example"; + const char* fingerprint = "ex01"; + std::copy(fingerprint, fingerprint+4, settings.fingerprint); + try { std::vector handles; -#ifndef NDEBUG - cout_log_creator l; - session s(6881, &l); -#else session s(6881); -#endif + s.set_http_settings(settings); for (int i = 0; i < argc-1; ++i) { diff --git a/include/libtorrent/debug.hpp b/include/libtorrent/debug.hpp index e2f91c660..dbb02ec9d 100755 --- a/include/libtorrent/debug.hpp +++ b/include/libtorrent/debug.hpp @@ -40,7 +40,7 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) // DEBUG API struct logger @@ -65,7 +65,6 @@ namespace libtorrent return *this; } - logger& operator<<(char i) { char c[2]; @@ -76,20 +75,25 @@ namespace libtorrent } virtual void log(const char*) = 0; - virtual void clear() = 0; virtual ~logger() {} }; - // this is an abstract base for a log-window creator - // this is called every time a new peer connects - // the logger that gets created will be deleted - // by the library when it's finished with it. - struct log_spawner + struct cout_logger: libtorrent::logger { - virtual logger* create_logger(const char* title) = 0; - virtual ~log_spawner() {} + public: + virtual void log(const char* text) { std::cout << text; } }; + struct file_logger: libtorrent::logger + { + public: + file_logger(const char* filename) + : m_file(filename) + {} + virtual void log(const char* text) { m_file << text; } + + std::ofstream m_file; + }; #endif } diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index ad38c73e5..4bc2e6fbb 100755 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -75,7 +75,6 @@ namespace libtorrent { return piece == r.piece && start == r.start && length == r.length; } }; - // TODO: add checks to make sure we only get pieces we ask for class peer_connection: public boost::noncopyable { public: @@ -146,7 +145,7 @@ namespace libtorrent const peer_id& get_peer_id() const { return m_peer_id; } const std::vector& get_bitfield() const { return m_have_piece; } -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) boost::shared_ptr m_logger; #endif diff --git a/include/libtorrent/session.hpp b/include/libtorrent/session.hpp index 236fe8185..daa8b5f4e 100755 --- a/include/libtorrent/session.hpp +++ b/include/libtorrent/session.hpp @@ -115,16 +115,11 @@ namespace libtorrent { typedef std::map, boost::shared_ptr > connection_map; -#ifndef NDEBUG - session_impl(log_spawner* log_creator): m_abort(false) - { - m_log_spawner = log_creator; - } -#else - session_impl(): m_abort(false) - { - } -#endif + session_impl() + : m_abort(false) + , m_tracker_manager(m_settings) + {} + // must be locked to access the data // in this struct boost::mutex m_mutex; @@ -146,14 +141,24 @@ namespace libtorrent // them selector m_selector; + // the settings for the client + http_settings m_settings; + bool m_abort; void run(int listen_port); torrent* find_torrent(const sha1_hash& info_hash); const peer_id& get_peer_id() const { return m_peer_id; } -#ifndef NDEBUG - log_spawner* m_log_spawner; + +#if defined(TORRENT_VERBOSE_LOGGING) + boost::shared_ptr create_log(std::string name) + { + name += ".log"; + // current options are file_logger and cout_logger + return boost::shared_ptr(new file_logger(name.c_str())); + } + boost::shared_ptr m_logger; #endif }; @@ -161,12 +166,12 @@ namespace libtorrent struct main_loop_thread { main_loop_thread(int listen_port, session_impl* s) - : m_ses(s), m_listen_port(listen_port) {} + : m_ses(s), m_listen_port(listen_port) + {} + void operator()() - { - std::cout << "main thread started\n"; - m_ses->run(m_listen_port); - } + { m_ses->run(m_listen_port); } + session_impl* m_ses; int m_listen_port; }; @@ -175,6 +180,8 @@ namespace libtorrent struct http_settings; + std::string extract_fingerprint(const peer_id& p); + struct torrent_handle { friend class session; @@ -208,16 +215,10 @@ namespace libtorrent class session: public boost::noncopyable { public: -#ifndef NDEBUG - session(int listen_port, log_spawner* new_log) - : m_impl(new_log) - , m_thread(detail::main_loop_thread(listen_port, &m_impl)) - { - } -#else + session(int listen_port) : m_thread(detail::main_loop_thread(listen_port, &m_impl)) {} -#endif + ~session(); // all torrent_handles must be destructed before the session is destructed! diff --git a/include/libtorrent/stat.hpp b/include/libtorrent/stat.hpp index dad9b9265..bff5bc21e 100755 --- a/include/libtorrent/stat.hpp +++ b/include/libtorrent/stat.hpp @@ -51,6 +51,8 @@ namespace libtorrent , m_total_upload(0) , m_peak_downloaded_per_second(0) , m_peak_uploaded_per_second(0) + , m_mean_download_per_second(0) + , m_mean_upload_per_second(0) { std::fill(m_download_per_second_history, m_download_per_second_history+history, 0); std::fill(m_upload_per_second_history, m_upload_per_second_history+history, 0); diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index b4fa91c11..24d5b745e 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -90,9 +90,8 @@ libtorrent::peer_connection::peer_connection(detail::session_impl* ses, torrent* { assert(m_torrent != 0); -#ifndef NDEBUG - m_logger = boost::shared_ptr( - m_torrent->spawn_logger(s->sender().as_string().c_str())); +#if defined(TORRENT_VERBOSE_LOGGING) + m_logger = m_ses->create_log(s->sender().as_string().c_str()); #endif send_handshake(); @@ -126,9 +125,8 @@ libtorrent::peer_connection::peer_connection(detail::session_impl* ses, boost::s , m_choked(true) { -#ifndef NDEBUG - m_logger = boost::shared_ptr( - m_ses->m_log_spawner->create_logger(s->sender().as_string().c_str())); +#if defined(TORRENT_VERBOSE_LOGGING) + m_logger = m_ses->create_log(s->sender().as_string().c_str()); #endif // we are not attached to any torrent yet. @@ -159,7 +157,7 @@ void libtorrent::peer_connection::send_handshake() std::copy(m_torrent->torrent_file().info_hash().begin(), m_torrent->torrent_file().info_hash().end(), m_send_buffer.begin() + 28); std::copy(m_ses->get_peer_id().begin(), m_ses->get_peer_id().end(), m_send_buffer.begin() + 48); -#if !defined(NDEBUG) && defined(VERBOSE) +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " ==> HANDSHAKE\n"; #endif @@ -176,7 +174,7 @@ bool libtorrent::peer_connection::dispatch_message() // *************** CHOKE *************** case msg_choke: -#if !defined(NDEBUG) && defined(VERBOSE) +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " <== CHOKE\n"; #endif m_peer_choked = true; @@ -200,7 +198,7 @@ bool libtorrent::peer_connection::dispatch_message() // *************** UNCHOKE *************** case msg_unchoke: -#if !defined(NDEBUG) && defined(VERBOSE) +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " <== UNCHOKE\n"; #endif m_peer_choked = false; @@ -210,7 +208,7 @@ bool libtorrent::peer_connection::dispatch_message() // *************** INTERESTED *************** case msg_interested: -#if !defined(NDEBUG) && defined(VERBOSE) +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " <== INTERESTED\n"; #endif m_peer_interested = true; @@ -220,7 +218,7 @@ bool libtorrent::peer_connection::dispatch_message() // *************** NOT INTERESTED *************** case msg_not_interested: -#if !defined(NDEBUG) && defined(VERBOSE) +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " <== NOT_INTERESTED\n"; #endif m_peer_interested = false; @@ -237,13 +235,13 @@ bool libtorrent::peer_connection::dispatch_message() if (index >= m_have_piece.size()) return false; -#if !defined(NDEBUG) && defined(VERBOSE) +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " <== HAVE [ piece: " << index << "]\n"; #endif if (m_have_piece[index]) { -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " oops.. we already knew that: " << index << "\n"; #endif } @@ -265,7 +263,7 @@ bool libtorrent::peer_connection::dispatch_message() if (m_packet_size - 1 != (m_have_piece.size() + 7) / 8) return false; -#if !defined(NDEBUG) && defined(VERBOSE) +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " <== BITFIELD\n"; #endif bool interesting = false; @@ -287,7 +285,7 @@ bool libtorrent::peer_connection::dispatch_message() } if (is_seed) { -#if !defined(NDEBUG) && defined(VERBOSE) +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " *** THIS IS A SEED ***\n"; #endif } @@ -307,7 +305,7 @@ bool libtorrent::peer_connection::dispatch_message() r.length = read_int(&m_recv_buffer[9]); m_requests.push_back(r); -#if !defined(NDEBUG) && defined(VERBOSE) +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " <== REQUEST [ piece: " << r.piece << " | s: " << r.start << " | l: " << r.length << " ]\n"; #endif @@ -322,7 +320,7 @@ bool libtorrent::peer_connection::dispatch_message() std::size_t index = read_int(&m_recv_buffer[1]); if (index < 0 || index >= m_torrent->torrent_file().num_pieces()) { -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " piece index invalid\n"; #endif return false; @@ -332,7 +330,7 @@ bool libtorrent::peer_connection::dispatch_message() if (offset < 0) { -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " offset < 0\n"; #endif return false; @@ -340,7 +338,7 @@ bool libtorrent::peer_connection::dispatch_message() if (offset + len > m_torrent->torrent_file().piece_size(index)) { -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " piece packet contains more data than the piece size\n"; #endif return false; @@ -348,7 +346,7 @@ bool libtorrent::peer_connection::dispatch_message() if (offset % m_torrent->block_size() != 0) { -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " piece packet contains unaligned offset\n"; #endif return false; @@ -357,7 +355,7 @@ bool libtorrent::peer_connection::dispatch_message() piece_block req = m_download_queue.front(); if (req.piece_index != index) { -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " piece packet contains unrequested index\n"; #endif return false; @@ -365,7 +363,7 @@ bool libtorrent::peer_connection::dispatch_message() if (req.block_index != offset / m_torrent->block_size()) { -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " piece packet contains unrequested offset\n"; #endif return false; @@ -376,7 +374,7 @@ bool libtorrent::peer_connection::dispatch_message() m_receiving_piece.open(m_torrent->filesystem(), index, piece_file::out); // } -#if !defined(NDEBUG) && defined(VERBOSE) +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " <== PIECE [ piece: " << index << " | s: " << offset << " | l: " << len << " ]\n"; #endif @@ -432,7 +430,7 @@ bool libtorrent::peer_connection::dispatch_message() m_requests.erase(i); } -#if !defined(NDEBUG) && defined(VERBOSE) +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " <== CANCEL [ piece: " << r.piece << " | s: " << r.start << " | l: " << r.length << " ]\n"; #endif m_requests.clear(); @@ -482,8 +480,8 @@ void libtorrent::peer_connection::request_block(piece_block block) // length write_int(block_size, &m_send_buffer[start_offset]); start_offset += 4; -#if !defined(NDEBUG) && defined(VERBOSE) - (*m_logger) << m_socket->sender().as_string() << " ==> REQUEST [ piece: " << block.piece_index << " | s: " << block_offset << " | l: " << block_size << " | " << block.block_index << " ]\n"; +#if defined(TORRENT_VERBOSE_LOGGING) + (*m_logger) << m_socket->sender().as_string() << " ==> REQUEST [ piece: " << block.piece_index << " | s: " << block_offset << " | l: " << block_size << " | " << block.block_index << " ]\n"; #endif assert(start_offset == m_send_buffer.size()); @@ -491,7 +489,7 @@ void libtorrent::peer_connection::request_block(piece_block block) void libtorrent::peer_connection::send_bitfield() { -#if !defined(NDEBUG) && defined(VERBOSE) +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " ==> BITFIELD\n"; #endif const int packet_size = (m_have_piece.size() + 7) / 8 + 5; @@ -513,7 +511,7 @@ void libtorrent::peer_connection::choke() char msg[] = {0,0,0,1,msg_choke}; m_send_buffer.insert(m_send_buffer.end(), msg, msg+sizeof(msg)); m_choked = true; -#if !defined(NDEBUG) && defined(VERBOSE) +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " ==> CHOKE\n"; #endif } @@ -524,7 +522,7 @@ void libtorrent::peer_connection::unchoke() char msg[] = {0,0,0,1,msg_unchoke}; m_send_buffer.insert(m_send_buffer.end(), msg, msg+sizeof(msg)); m_choked = false; -#if !defined(NDEBUG) && defined(VERBOSE) +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " ==> UNCHOKE\n"; #endif } @@ -535,7 +533,7 @@ void libtorrent::peer_connection::interested() char msg[] = {0,0,0,1,msg_interested}; m_send_buffer.insert(m_send_buffer.end(), msg, msg+sizeof(msg)); m_interesting = true; -#if !defined(NDEBUG) && defined(VERBOSE) +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " ==> INTERESTED\n"; #endif } @@ -546,7 +544,7 @@ void libtorrent::peer_connection::not_interested() char msg[] = {0,0,0,1,msg_not_interested}; m_send_buffer.insert(m_send_buffer.end(), msg, msg+sizeof(msg)); m_interesting = false; -#if !defined(NDEBUG) && defined(VERBOSE) +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " ==> NOT_INTERESTED\n"; #endif } @@ -556,7 +554,7 @@ void libtorrent::peer_connection::send_have(int index) char msg[9] = {0,0,0,5,msg_have}; write_int(index, msg+5); m_send_buffer.insert(m_send_buffer.end(), msg, msg+9); -#if !defined(NDEBUG) && defined(VERBOSE) +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " ==> HAVE [ piece: " << index << " ]\n"; #endif } @@ -594,7 +592,7 @@ void libtorrent::peer_connection::receive_data() { case read_protocol_length: m_packet_size = reinterpret_cast(m_recv_buffer[0]); -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " protocol length: " << m_packet_size << "\n"; #endif m_state = read_protocol_version; @@ -608,7 +606,7 @@ void libtorrent::peer_connection::receive_data() case read_protocol_version: { const char* protocol_version = "BitTorrent protocol"; -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " protocol name: " << std::string(m_recv_buffer.begin(), m_recv_buffer.end()) << "\n"; #endif if (!std::equal(m_recv_buffer.begin(), m_recv_buffer.end(), protocol_version)) @@ -640,7 +638,7 @@ void libtorrent::peer_connection::receive_data() if (m_torrent == 0) { // we couldn't find the torrent! -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " couldn't find a torrent with the given info_hash\n"; #endif throw network_error(0); @@ -662,7 +660,7 @@ void libtorrent::peer_connection::receive_data() // verify info hash if (!std::equal(m_recv_buffer.begin()+8, m_recv_buffer.begin() + 28, (const char*)m_torrent->torrent_file().info_hash().begin())) { -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " received invalid info_hash\n"; #endif throw network_error(0); @@ -673,7 +671,7 @@ void libtorrent::peer_connection::receive_data() m_packet_size = 20; m_recv_pos = 0; m_recv_buffer.resize(20); -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " info_hash received\n"; #endif break; @@ -689,7 +687,7 @@ void libtorrent::peer_connection::receive_data() // can this be correct? if (!std::equal(m_recv_buffer.begin(), m_recv_buffer.begin() + 20, (const char*)m_peer_id.begin())) { -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " invalid peer_id (it doesn't equal the one from the tracker)\n"; #endif throw network_error(0); @@ -702,7 +700,7 @@ void libtorrent::peer_connection::receive_data() std::copy(m_recv_buffer.begin(), m_recv_buffer.begin() + 20, (char*)m_peer_id.begin()); if (m_torrent->num_connections(m_peer_id) > 1) { -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " duplicate connection, closing\n"; #endif throw network_error(0); @@ -713,7 +711,7 @@ void libtorrent::peer_connection::receive_data() m_packet_size = 4; m_recv_pos = 0; m_recv_buffer.resize(4); -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " received peer_id\n"; #endif break; @@ -726,7 +724,7 @@ void libtorrent::peer_connection::receive_data() // don't accept packets larger than 1 MB if (m_packet_size > 1024*1024 || m_packet_size < 0) { -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " packet too large (packet_size > 1 Megabyte), abort\n"; #endif // packet too large @@ -750,7 +748,7 @@ void libtorrent::peer_connection::receive_data() case read_packet: if (!dispatch_message()) { -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " received invalid packet\n"; #endif // invalid message @@ -825,7 +823,7 @@ void libtorrent::peer_connection::send_data() } m_sending_piece.read(&m_send_buffer[13], r.length); -#if !defined(NDEBUG) && defined(VERBOSE) +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " ==> PIECE [ idx: " << r.piece << " | s: " << r.start << " | l: " << r.length << " | dest: " << m_socket->sender().as_string() << " ]\n"; #endif // let the torrent keep track of how much we have uploaded @@ -834,7 +832,7 @@ void libtorrent::peer_connection::send_data() } else { -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " *** WARNING [ illegal piece request ]\n"; #endif } @@ -856,7 +854,7 @@ void libtorrent::peer_connection::send_data() // we have data that's scheduled for sending std::size_t sent = m_socket->send(&m_send_buffer[0], m_send_buffer.size()); -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " ==> SENT [ length: " << sent << " ]\n"; #endif @@ -888,7 +886,7 @@ void libtorrent::peer_connection::keep_alive() char noop[] = {0,0,0,0}; m_send_buffer.insert(m_send_buffer.end(), noop, noop+4); m_last_sent = boost::posix_time::second_clock::local_time(); -#if !defined(NDEBUG) && defined(VERBOSE) +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << m_socket->sender().as_string() << " ==> NOP\n"; #endif diff --git a/src/session.cpp b/src/session.cpp index dfc2833d9..a2dd96a7e 100755 --- a/src/session.cpp +++ b/src/session.cpp @@ -37,6 +37,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include #include @@ -53,6 +54,7 @@ POSSIBILITY OF SUCH DAMAGE. namespace std { using ::srand; + using ::isprint; }; #endif @@ -78,16 +80,30 @@ namespace { using namespace libtorrent; - peer_id generate_peer_id() + peer_id generate_peer_id(const http_settings& s) { peer_id ret; std::srand(std::time(0)); - // TODO: add ability to control fingerprint - unsigned char fingerprint[] = "lt.\0\0\0\0\0\0\0"; -// unsigned char fingerprint[] = "lt.\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; - const int len = sizeof(fingerprint)-1; + + // libtorrent's fingerprint + unsigned char fingerprint[] = "lt."; + const int len = sizeof(fingerprint)-1-(s.fingerprint[0] == 0)?1:0; std::copy(fingerprint, fingerprint+len, ret.begin()); - for (unsigned char* i = ret.begin()+len; i != ret.end(); ++i) *i = rand(); + + // the client's fingerprint + const int len2 = std::find(s.fingerprint, s.fingerprint+4, 0) - s.fingerprint; + std::copy(s.fingerprint, s.fingerprint+len2, ret.begin()+len); + + // the zeros + std::fill(ret.begin()+len+len2, ret.begin()+len+len2+3, 0); + + // the random number + for (unsigned char* i = ret.begin()+len+len2+3; + i != ret.end(); + ++i) + { + *i = rand(); + } return ret; } @@ -99,12 +115,11 @@ namespace libtorrent { void session_impl::run(int listen_port) { -#ifndef NDEBUG - m_logger = boost::shared_ptr( - m_log_spawner->create_logger("main session")); +#if defined(TORRENT_VERBOSE_LOGGING) + m_logger = create_log("main session"); #endif - m_peer_id = generate_peer_id(); + m_peer_id = generate_peer_id(m_settings); boost::shared_ptr listener(new socket(socket::tcp, false)); int max_port = listen_port + 9; @@ -127,7 +142,9 @@ namespace libtorrent break; } - std::cout << "listening on port: " << listen_port << "\n"; +#if defined(TORRENT_VERBOSE_LOGGING) + (*m_logger) << "listening on port: " << listen_port << "\n"; +#endif m_selector.monitor_readability(listener); m_selector.monitor_errors(listener); @@ -194,7 +211,7 @@ namespace libtorrent if (s) { // we got a connection request! -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << s->sender().as_string() << " <== INCOMING CONNECTION\n"; #endif // TODO: the send buffer size should be controllable from the outside @@ -364,7 +381,7 @@ namespace libtorrent } m_tracker_manager.tick(); -#ifndef NDEBUG +#if defined(TORRENT_VERBOSE_LOGGING) (*m_logger) << "peers: " << m_connections.size() << " \n"; for (connection_map::iterator i = m_connections.begin(); i != m_connections.end(); @@ -375,19 +392,6 @@ namespace libtorrent << " b/s | up: " << i->second->statistics().upload_rate() << " b/s \n"; } - - m_logger->clear(); -#else - std::cout << "peers: " << m_connections.size() << " \n"; - for (connection_map::iterator i = m_connections.begin(); - i != m_connections.end(); - ++i) - { - std::cout << "h: " << i->first->sender().as_string() - << " | down: " << i->second->statistics().download_rate() - << " b/s | up: " << i->second->statistics().upload_rate() - << " b/s \n"; - } #endif } @@ -474,7 +478,7 @@ namespace libtorrent void session::set_http_settings(const http_settings& s) { boost::mutex::scoped_lock l(m_impl.m_mutex); - m_impl.m_tracker_manager.set_settings(s); + m_impl.m_settings = s; } session::~session() @@ -544,4 +548,19 @@ namespace libtorrent m_ses = 0; } + // TODO: document + std::string extract_fingerprint(const peer_id& p) + { + std::string ret; + const unsigned char* c = p.begin(); + while (c != p.end() && *c != 0) + { + if (!std::isprint(*c)) return std::string(); + ret += *c; + ++c; + } + if (c == p.end()) return std::string(); + + return ret; + } } diff --git a/src/torrent.cpp b/src/torrent.cpp index abf295823..8865157b5 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -184,7 +184,7 @@ namespace libtorrent std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast(*j); } - std::cout << "\n"; + std::cout << " " << extract_fingerprint(i->id) << "\n"; } std::cout << std::dec << std::setfill(' '); @@ -355,13 +355,6 @@ namespace libtorrent std::cout << "connecting to: " << a.as_string() << ":" << a.port() << "\n"; } -#ifndef NDEBUG - logger* torrent::spawn_logger(const char* title) - { - return m_ses->m_log_spawner->create_logger(title); - } -#endif - void torrent::close_all_connections() { for (detail::session_impl::connection_map::iterator i = m_ses->m_connections.begin();