From a05acc4baca00d167cac117b51334c4f9869f9bf Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 18 Nov 2010 05:51:52 +0000 Subject: [PATCH] support trackerid tracker extension --- ChangeLog | 1 + include/libtorrent/add_torrent_params.hpp | 1 + include/libtorrent/alert_types.hpp | 17 +++++++++++++++++ include/libtorrent/torrent.hpp | 4 +++- include/libtorrent/torrent_info.hpp | 1 + include/libtorrent/tracker_manager.hpp | 4 +++- src/alert.cpp | 5 +++++ src/http_tracker_connection.cpp | 12 +++++++++++- src/torrent.cpp | 13 ++++++++++++- src/udp_tracker_connection.cpp | 2 +- 10 files changed, 55 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index b18fec08d..d41f674e8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ + * support trackerid tracker extension * graceful peer disconnect mode which finishes transactions before disconnecting peers * support chunked encoding for web seeds * optimized session startup time diff --git a/include/libtorrent/add_torrent_params.hpp b/include/libtorrent/add_torrent_params.hpp index 2e46314a1..aec9edf1c 100644 --- a/include/libtorrent/add_torrent_params.hpp +++ b/include/libtorrent/add_torrent_params.hpp @@ -84,6 +84,7 @@ namespace libtorrent bool upload_mode; std::vector const* file_priorities; bool share_mode; + std::string trackerid; }; } diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index 6ca9916c2..212e23aca 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -1167,6 +1167,23 @@ namespace libtorrent virtual std::string message() const; }; + struct TORRENT_EXPORT trackerid_alert: tracker_alert + { + trackerid_alert(torrent_handle const& h + , std::string const& url_ + , const std::string& id) + : tracker_alert(h, url_) + , trackerid(id) + {} + + TORRENT_DEFINE_ALERT(trackerid_alert); + + const static int static_category = alert::status_notification; + virtual std::string message() const; + + std::string trackerid; + }; + } diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 815a60992..3ac9e1313 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -444,7 +444,8 @@ namespace libtorrent , address const& tracker_ip , std::list
const& ip_list , std::vector& e, int interval, int min_interval - , int complete, int incomplete, address const& external_ip); + , int complete, int incomplete, address const& external_ip + , std::string const& trackerid); virtual void tracker_request_error(tracker_request const& r , int response_code, error_code const& ec, const std::string& msg , int retry_interval); @@ -942,6 +943,7 @@ namespace libtorrent // this list is sorted by time_critical_piece::deadline std::list m_time_critical_pieces; + std::string m_trackerid; std::string m_username; std::string m_password; diff --git a/include/libtorrent/torrent_info.hpp b/include/libtorrent/torrent_info.hpp index 98c92aecb..bf9738adb 100644 --- a/include/libtorrent/torrent_info.hpp +++ b/include/libtorrent/torrent_info.hpp @@ -91,6 +91,7 @@ namespace libtorrent // tracker URL as it appeared in the torrent file std::string url; + std::string trackerid; // if this tracker has returned an error or warning message // that message is stored here diff --git a/include/libtorrent/tracker_manager.hpp b/include/libtorrent/tracker_manager.hpp index 56d964d4c..6fec8e179 100644 --- a/include/libtorrent/tracker_manager.hpp +++ b/include/libtorrent/tracker_manager.hpp @@ -115,6 +115,7 @@ namespace libtorrent unsigned short listen_port; event_t event; std::string url; + std::string trackerid; int key; int num_want; std::string ipv6; @@ -142,7 +143,8 @@ namespace libtorrent , int min_interval , int complete , int incomplete - , address const& external_ip) = 0; + , address const& external_ip + , std::string const& trackerid) = 0; virtual void tracker_request_error( tracker_request const& req , int response_code diff --git a/src/alert.cpp b/src/alert.cpp index 7892c7646..938a19529 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -486,5 +486,10 @@ namespace libtorrent { return msg; } + std::string trackerid_alert::message() const + { + return "trackerid received: " + trackerid; + } + } // namespace libtorrent diff --git a/src/http_tracker_connection.cpp b/src/http_tracker_connection.cpp index d2adf53dc..6e4ce9cd8 100644 --- a/src/http_tracker_connection.cpp +++ b/src/http_tracker_connection.cpp @@ -156,6 +156,12 @@ namespace libtorrent if (m_ses.get_pe_settings().in_enc_policy != pe_settings::disabled) url += "&supportcrypto=1"; #endif + if (!tracker_req().trackerid.empty()) + { + std::string id = tracker_req().trackerid; + url += "&trackerid="; + url += escape_string(id.c_str(), id.length()); + } if (tracker_req().event != tracker_request::none) { @@ -363,6 +369,10 @@ namespace libtorrent int interval = e.dict_find_int_value("interval", 1800); int min_interval = e.dict_find_int_value("min interval", 60); + std::string trackerid; + lazy_entry const* tracker_id = e.dict_find_string("tracker id"); + if (tracker_id) + trackerid = tracker_id->string_value(); // parse the response lazy_entry const* failure = e.dict_find_string("failure reason"); if (failure) @@ -506,7 +516,7 @@ namespace libtorrent } cb->tracker_response(tracker_req(), m_tracker_ip, ip_list, peer_list - , interval, min_interval, complete, incomplete, external_ip); + , interval, min_interval, complete, incomplete, external_ip, trackerid); } } diff --git a/src/torrent.cpp b/src/torrent.cpp index b28022752..ab55b6d0c 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -329,6 +329,7 @@ namespace libtorrent , m_ses(ses) , m_trackers(m_torrent_file->trackers()) , m_save_path(complete(p.save_path)) + , m_trackerid(p.trackerid) , m_storage_constructor(p.storage) , m_ratio(0.f) , m_available_free_upload(0) @@ -1494,6 +1495,8 @@ namespace libtorrent for (int i = 0; i < int(m_trackers.size()); ++i) { announce_entry& ae = m_trackers[i]; + // if trackerid is not specified for tracker use default one, probably set explicitly + req.trackerid = ae.trackerid.empty() ? m_trackerid : ae.trackerid; if (settings().announce_to_all_tiers && !settings().announce_to_all_trackers && sent_announce @@ -1652,7 +1655,8 @@ namespace libtorrent , int min_interval , int complete , int incomplete - , address const& external_ip) + , address const& external_ip + , const std::string& trackerid) { TORRENT_ASSERT(m_ses.is_network_thread()); @@ -1681,6 +1685,13 @@ namespace libtorrent ae->min_announce = now + seconds(min_interval); int tracker_index = ae - &m_trackers[0]; m_last_working_tracker = prioritize_tracker(tracker_index); + + if ((!trackerid.empty()) && (ae->trackerid != trackerid)) + { + ae->trackerid = trackerid; + if (m_ses.m_alerts.should_post()) + m_ses.m_alerts.post_alert(trackerid_alert(get_handle(), r.url, trackerid)); + } } update_tracker_timer(now); diff --git a/src/udp_tracker_connection.cpp b/src/udp_tracker_connection.cpp index 954b7ca16..a25f9381f 100644 --- a/src/udp_tracker_connection.cpp +++ b/src/udp_tracker_connection.cpp @@ -527,7 +527,7 @@ namespace libtorrent } cb->tracker_response(tracker_req(), m_target.address(), ip_list - , peer_list, interval, min_interval, complete, incomplete, address()); + , peer_list, interval, min_interval, complete, incomplete, address(), "" /*trackerid*/); close(); return true;