make local service discovery cookie support not so random numbers, to make the test reliable (#943)

This commit is contained in:
Arvid Norberg 2016-07-25 08:05:54 -04:00 committed by GitHub
parent 6370d5bc81
commit 0ea449a7b5
1 changed files with 11 additions and 11 deletions

View File

@ -57,8 +57,8 @@ namespace libtorrent
{ {
namespace { namespace {
int render_lsd_packet(char* dst, int len, int listen_port int render_lsd_packet(char* dst, int const len, int const listen_port
, char const* info_hash_hex, int m_cookie, char const* host) , char const* info_hash_hex, int const cookie, char const* host)
{ {
TORRENT_ASSERT(len > 0); TORRENT_ASSERT(len > 0);
return snprintf(dst, len, return snprintf(dst, len,
@ -67,7 +67,7 @@ int render_lsd_packet(char* dst, int len, int listen_port
"Port: %d\r\n" "Port: %d\r\n"
"Infohash: %s\r\n" "Infohash: %s\r\n"
"cookie: %x\r\n" "cookie: %x\r\n"
"\r\n\r\n", host, listen_port, info_hash_hex, m_cookie); "\r\n\r\n", host, listen_port, info_hash_hex, cookie);
} }
} // anonymous namespace } // anonymous namespace
@ -87,7 +87,7 @@ lsd::lsd(io_service& ios, peer_callback_t const& cb
, m_log_cb(log) , m_log_cb(log)
#endif #endif
, m_broadcast_timer(ios) , m_broadcast_timer(ios)
, m_cookie(random() & 0x7fffffff) , m_cookie((random() ^ boost::uintptr_t(this)) & 0x7fffffff)
, m_disabled(false) , m_disabled(false)
#if TORRENT_USE_IPV6 #if TORRENT_USE_IPV6
, m_disabled6(false) , m_disabled6(false)
@ -128,8 +128,8 @@ void lsd::announce(sha1_hash const& ih, int listen_port, bool broadcast)
announce_impl(ih, listen_port, broadcast, 0); announce_impl(ih, listen_port, broadcast, 0);
} }
void lsd::announce_impl(sha1_hash const& ih, int listen_port, bool broadcast void lsd::announce_impl(sha1_hash const& ih, int const listen_port
, int retry_count) , bool const broadcast, int retry_count)
{ {
#if TORRENT_USE_IPV6 #if TORRENT_USE_IPV6
if (m_disabled && m_disabled6) return; if (m_disabled && m_disabled6) return;
@ -148,7 +148,7 @@ void lsd::announce_impl(sha1_hash const& ih, int listen_port, bool broadcast
error_code ec; error_code ec;
if (!m_disabled) if (!m_disabled)
{ {
int msg_len = render_lsd_packet(msg, sizeof(msg), listen_port, ih_hex int const msg_len = render_lsd_packet(msg, sizeof(msg), listen_port, ih_hex
, m_cookie, "239.192.152.143"); , m_cookie, "239.192.152.143");
m_socket.send(msg, msg_len, ec, broadcast ? broadcast_socket::broadcast : 0); m_socket.send(msg, msg_len, ec, broadcast ? broadcast_socket::broadcast : 0);
if (ec) if (ec)
@ -164,7 +164,7 @@ void lsd::announce_impl(sha1_hash const& ih, int listen_port, bool broadcast
#if TORRENT_USE_IPV6 #if TORRENT_USE_IPV6
if (!m_disabled6) if (!m_disabled6)
{ {
int msg_len = render_lsd_packet(msg, sizeof(msg), listen_port, ih_hex int const msg_len = render_lsd_packet(msg, sizeof(msg), listen_port, ih_hex
, m_cookie, "[ff15::efc0:988f]"); , m_cookie, "[ff15::efc0:988f]");
m_socket6.send(msg, msg_len, ec, broadcast ? broadcast_socket::broadcast : 0); m_socket6.send(msg, msg_len, ec, broadcast ? broadcast_socket::broadcast : 0);
if (ec) if (ec)
@ -252,12 +252,12 @@ void lsd::on_announce(udp::endpoint const& from, char* buf
{ {
// we expect it to be hexadecimal // we expect it to be hexadecimal
// if it isn't, it's not our cookie anyway // if it isn't, it's not our cookie anyway
boost::int32_t cookie = strtol(cookie_iter->second.c_str(), NULL, 16); boost::int32_t const cookie = strtol(cookie_iter->second.c_str(), NULL, 16);
if (cookie == m_cookie) if (cookie == m_cookie)
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
debug_log("<== LSD: ignoring packet (cookie matched our own): %x == %x" debug_log("<== LSD: ignoring packet (cookie matched our own): %x"
, cookie, m_cookie); , cookie);
#endif #endif
return; return;
} }