diff --git a/ChangeLog b/ChangeLog index 26f7823c3..ce813929e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -5,6 +5,7 @@ * fix uTP edge case where udp socket buffer fills up * fix nagle implementation in uTP + * small optimization to local peer discovery to ignore our own broadcasts * try harder to bind the udp socket (uTP, DHT, UDP-trackers, LSD) to the same port as TCP * relax file timestamp requirements for accepting resume data * fix performance issue in web seed downloader (coalescing of blocks sometimes wouldn't work) diff --git a/include/libtorrent/lsd.hpp b/include/libtorrent/lsd.hpp index b2ebd9361..00c5a0119 100644 --- a/include/libtorrent/lsd.hpp +++ b/include/libtorrent/lsd.hpp @@ -73,9 +73,6 @@ private: peer_callback_t m_callback; - // current retry count - int m_retry_count; - // the udp socket used to send and receive // multicast messages on broadcast_socket m_socket; @@ -84,6 +81,16 @@ private: // they time out deadline_timer m_broadcast_timer; + // current retry count + boost::uint32_t m_retry_count; + + // this is a random (presumably unique) + // ID for this LSD node. It is used to + // ignore our own broadcast messages. + // There's no point in adding ourselves + // as a peer + int m_cookie; + bool m_disabled; #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) FILE* m_log; diff --git a/src/lsd.cpp b/src/lsd.cpp index d2751d53c..7168a8c2a 100644 --- a/src/lsd.cpp +++ b/src/lsd.cpp @@ -36,6 +36,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/io.hpp" #include "libtorrent/http_tracker_connection.hpp" #include "libtorrent/buffer.hpp" +#include "libtorrent/random.hpp" #include "libtorrent/http_parser.hpp" #include "libtorrent/escape_string.hpp" #include "libtorrent/socket_io.hpp" // for print_address @@ -69,10 +70,11 @@ static error_code ec; lsd::lsd(io_service& ios, address const& listen_interface , peer_callback_t const& cb) : m_callback(cb) - , m_retry_count(1) , m_socket(udp::endpoint(address_v4::from_string("239.192.152.143", ec), 6771) , boost::bind(&lsd::on_announce, self(), _1, _2, _3)) , m_broadcast_timer(ios) + , m_retry_count(1) + , m_cookie(random()) , m_disabled(false) { #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) @@ -115,7 +117,8 @@ void lsd::announce(sha1_hash const& ih, int listen_port, bool broadcast) "Host: 239.192.152.143:6771\r\n" "Port: %d\r\n" "Infohash: %s\r\n" - "\r\n\r\n", listen_port, ih_hex); + "cookie: %x\r\n" + "\r\n\r\n", listen_port, ih_hex, m_cookie); #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) { @@ -210,6 +213,23 @@ void lsd::on_announce(udp::endpoint const& from, char* buffer typedef std::multimap headers_t; headers_t const& headers = p.headers(); + + headers_t::const_iterator cookie_iter = headers.find("cookie"); + if (cookie_iter != headers.end()) + { + // we expect it to be hexadecimal + // if it isn't, it's not our cookie anyway + boost::uint32_t cookie = strtol(cookie_iter->second.c_str(), NULL, 16); + if (cookie == m_cookie) + { +#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) + if (m_log) fprintf(m_log, "%s <== announce: ignoring packet (cookie matched our own): %x == %x\n" + , time_now_string(), cookie, m_cookie); +#endif + return; + } + } + std::pair ihs = headers.equal_range("infohash");