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 {
int render_lsd_packet(char* dst, int len, int listen_port
, char const* info_hash_hex, int m_cookie, char const* host)
int render_lsd_packet(char* dst, int const len, int const listen_port
, char const* info_hash_hex, int const cookie, char const* host)
{
TORRENT_ASSERT(len > 0);
return snprintf(dst, len,
@ -67,7 +67,7 @@ int render_lsd_packet(char* dst, int len, int listen_port
"Port: %d\r\n"
"Infohash: %s\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
@ -87,7 +87,7 @@ lsd::lsd(io_service& ios, peer_callback_t const& cb
, m_log_cb(log)
#endif
, m_broadcast_timer(ios)
, m_cookie(random() & 0x7fffffff)
, m_cookie((random() ^ boost::uintptr_t(this)) & 0x7fffffff)
, m_disabled(false)
#if TORRENT_USE_IPV6
, 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);
}
void lsd::announce_impl(sha1_hash const& ih, int listen_port, bool broadcast
, int retry_count)
void lsd::announce_impl(sha1_hash const& ih, int const listen_port
, bool const broadcast, int retry_count)
{
#if TORRENT_USE_IPV6
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;
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_socket.send(msg, msg_len, ec, broadcast ? broadcast_socket::broadcast : 0);
if (ec)
@ -164,7 +164,7 @@ void lsd::announce_impl(sha1_hash const& ih, int listen_port, bool broadcast
#if TORRENT_USE_IPV6
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_socket6.send(msg, msg_len, ec, broadcast ? broadcast_socket::broadcast : 0);
if (ec)
@ -252,12 +252,12 @@ void lsd::on_announce(udp::endpoint const& from, char* buf
{
// we expect it to be hexadecimal
// 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)
{
#ifndef TORRENT_DISABLE_LOGGING
debug_log("<== LSD: ignoring packet (cookie matched our own): %x == %x"
, cookie, m_cookie);
debug_log("<== LSD: ignoring packet (cookie matched our own): %x"
, cookie);
#endif
return;
}