clean up natpmp logging infrastructure a bit as well as test_peer_list (#735)

clean up natpmp logging infrastructure a bit as well as test_peer_list
This commit is contained in:
Arvid Norberg 2016-05-18 01:54:37 -04:00
parent 35eff73662
commit 178a41b187
11 changed files with 138 additions and 117 deletions

View File

@ -720,6 +720,7 @@ local usage-requirements =
# disable bogus deprecation warnings on msvc8 # disable bogus deprecation warnings on msvc8
<toolset>msvc:<define>_SCL_SECURE_NO_DEPRECATE <toolset>msvc:<define>_SCL_SECURE_NO_DEPRECATE
<toolset>msvc:<define>_CRT_SECURE_NO_DEPRECATE <toolset>msvc:<define>_CRT_SECURE_NO_DEPRECATE
<logging>off:<define>TORRENT_DISABLE_LOGGING
<cxxflags>$(CXXFLAGS) <cxxflags>$(CXXFLAGS)
<linkflags>$(LDFLAGS) <linkflags>$(LDFLAGS)

View File

@ -597,10 +597,13 @@ namespace libtorrent
virtual void announce(sha1_hash const& ih, address const& addr, int port) override; virtual void announce(sha1_hash const& ih, address const& addr, int port) override;
virtual void outgoing_get_peers(sha1_hash const& target virtual void outgoing_get_peers(sha1_hash const& target
, sha1_hash const& sent_target, udp::endpoint const& ep) override; , sha1_hash const& sent_target, udp::endpoint const& ep) override;
#ifndef TORRENT_DISABLE_LOGGING
virtual void log(libtorrent::dht::dht_logger::module_t m, char const* fmt, ...) virtual void log(libtorrent::dht::dht_logger::module_t m, char const* fmt, ...)
override TORRENT_FORMAT(3,4); override TORRENT_FORMAT(3,4);
virtual void log_packet(message_direction_t dir, char const* pkt, int len virtual void log_packet(message_direction_t dir, char const* pkt, int len
, udp::endpoint node) override; , udp::endpoint node) override;
#endif
virtual bool on_dht_request(char const* query, int query_len virtual bool on_dht_request(char const* query, int query_len
, dht::msg const& request, entry& response) override; , dht::msg const& request, entry& response) override;

View File

@ -41,6 +41,7 @@ namespace libtorrent { namespace dht
{ {
struct TORRENT_EXTRA_EXPORT dht_logger struct TORRENT_EXTRA_EXPORT dht_logger
{ {
#ifndef TORRENT_DISABLE_LOGGING
enum module_t enum module_t
{ {
tracker, tracker,
@ -59,6 +60,7 @@ namespace libtorrent { namespace dht
virtual void log(module_t m, char const* fmt, ...) TORRENT_FORMAT(3,4) = 0; virtual void log(module_t m, char const* fmt, ...) TORRENT_FORMAT(3,4) = 0;
virtual void log_packet(message_direction_t dir, char const* pkt, int len virtual void log_packet(message_direction_t dir, char const* pkt, int len
, udp::endpoint node) = 0; , udp::endpoint node) = 0;
#endif
protected: protected:
~dht_logger() {} ~dht_logger() {}

View File

@ -91,7 +91,10 @@ private:
void mapping_expired(error_code const& e, int i); void mapping_expired(error_code const& e, int i);
void close_impl(); void close_impl();
void log(char const* msg); #ifndef TORRENT_DISABLE_LOGGING
void log(char const* fmt, ...) const TORRENT_FORMAT(2, 3);
#endif
void disable(error_code const& ec); void disable(error_code const& ec);
struct mapping_t struct mapping_t

View File

@ -551,9 +551,7 @@ namespace libtorrent
, char const* event, char const* fmt, ...) const TORRENT_FORMAT(4,5); , char const* event, char const* fmt, ...) const TORRENT_FORMAT(4,5);
void peer_log(peer_log_alert::direction_t direction void peer_log(peer_log_alert::direction_t direction
, char const* event) const; , char const* event) const;
#endif
#ifndef TORRENT_DISABLE_LOGGING
time_point m_connect_time; time_point m_connect_time;
time_point m_bitfield_time; time_point m_bitfield_time;
time_point m_unchoke_time; time_point m_unchoke_time;

View File

@ -44,6 +44,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/disable_warnings_pop.hpp" #include "libtorrent/aux_/disable_warnings_pop.hpp"
#include <cstdio> // for snprintf #include <cstdio> // for snprintf
#include <cstdarg>
#include "libtorrent/natpmp.hpp" #include "libtorrent/natpmp.hpp"
#include "libtorrent/io.hpp" #include "libtorrent/io.hpp"
@ -91,10 +92,10 @@ void natpmp::start()
address gateway = get_default_gateway(m_socket.get_io_service(), ec); address gateway = get_default_gateway(m_socket.get_io_service(), ec);
if (ec) if (ec)
{ {
char msg[200]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "failed to find default route: %s" log("failed to find default route: %s"
, convert_from_native(ec.message()).c_str()); , convert_from_native(ec.message()).c_str());
log(msg); #endif
disable(ec); disable(ec);
return; return;
} }
@ -105,10 +106,10 @@ void natpmp::start()
if (nat_endpoint == m_nat_endpoint) return; if (nat_endpoint == m_nat_endpoint) return;
m_nat_endpoint = nat_endpoint; m_nat_endpoint = nat_endpoint;
char msg[200]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "found router at: %s" log("found router at: %s"
, print_address(m_nat_endpoint.address()).c_str()); , print_address(m_nat_endpoint.address()).c_str());
log(msg); #endif
m_socket.open(udp::v4(), ec); m_socket.open(udp::v4(), ec);
if (ec) if (ec)
@ -148,7 +149,9 @@ void natpmp::send_get_ip_address_request()
char* out = buf; char* out = buf;
write_uint8(0, out); // NAT-PMP version write_uint8(0, out); // NAT-PMP version
write_uint8(0, out); // public IP address request opcode write_uint8(0, out); // public IP address request opcode
#ifndef TORRENT_DISABLE_LOGGING
log("==> get public IP address"); log("==> get public IP address");
#endif
error_code ec; error_code ec;
m_socket.send_to(boost::asio::buffer(buf, sizeof(buf)), m_nat_endpoint, 0, ec); m_socket.send_to(boost::asio::buffer(buf, sizeof(buf)), m_nat_endpoint, 0, ec);
@ -168,11 +171,21 @@ bool natpmp::get_mapping(int index, int& local_port, int& external_port, int& pr
return true; return true;
} }
void natpmp::log(char const* msg) #ifndef TORRENT_DISABLE_LOGGING
TORRENT_FORMAT(2, 3)
void natpmp::log(char const* fmt, ...) const
{ {
TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(is_single_thread());
char msg[200];
va_list v;
va_start(v, fmt);
std::vsnprintf(msg, sizeof(msg), fmt, v);
va_end(v);
m_log_callback(msg); m_log_callback(msg);
} }
#endif
void natpmp::disable(error_code const& ec) void natpmp::disable(error_code const& ec)
{ {
@ -352,13 +365,13 @@ void natpmp::send_map_request(int i)
int ttl = m.action == mapping_t::action_add ? 3600 : 0; int ttl = m.action == mapping_t::action_add ? 3600 : 0;
write_uint32(ttl, out); // port mapping lifetime write_uint32(ttl, out); // port mapping lifetime
char msg[200]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "==> port map [ mapping: %d action: %s" log("==> port map [ mapping: %d action: %s"
" proto: %s local: %u external: %u ttl: %u ]" " proto: %s local: %u external: %u ttl: %u ]"
, i, m.action == mapping_t::action_add ? "add" : "delete" , i, m.action == mapping_t::action_add ? "add" : "delete"
, m.protocol == udp ? "udp" : "tcp" , m.protocol == udp ? "udp" : "tcp"
, m.local_port, m.external_port, ttl); , m.local_port, m.external_port, ttl);
log(msg); #endif
error_code ec; error_code ec;
m_socket.send_to(boost::asio::buffer(buf, sizeof(buf)), m_nat_endpoint, 0, ec); m_socket.send_to(boost::asio::buffer(buf, sizeof(buf)), m_nat_endpoint, 0, ec);
@ -414,10 +427,10 @@ void natpmp::on_reply(error_code const& e
using namespace libtorrent::detail; using namespace libtorrent::detail;
if (e) if (e)
{ {
char msg[200]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "error on receiving reply: %s" log("error on receiving reply: %s"
, convert_from_native(e.message()).c_str()); , convert_from_native(e.message()).c_str());
log(msg); #endif
return; return;
} }
@ -430,20 +443,12 @@ void natpmp::on_reply(error_code const& e
m_socket.async_receive_from(boost::asio::buffer(&m_response_buffer, 16) m_socket.async_receive_from(boost::asio::buffer(&m_response_buffer, 16)
, m_remote, boost::bind(&natpmp::on_reply, self(), _1, _2)); , m_remote, boost::bind(&natpmp::on_reply, self(), _1, _2));
// simulate packet loss
/*
if ((random() % 2) == 0)
{
log(" simulating drop");
return;
}
*/
if (m_remote != m_nat_endpoint) if (m_remote != m_nat_endpoint)
{ {
char msg[200]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "received packet from wrong IP: %s" log("received packet from wrong IP: %s"
, print_endpoint(m_remote).c_str()); , print_endpoint(m_remote).c_str());
log(msg); #endif
return; return;
} }
@ -452,9 +457,9 @@ void natpmp::on_reply(error_code const& e
if (bytes_transferred < 12) if (bytes_transferred < 12)
{ {
char msg[200]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "received packet of invalid size: %d", int(bytes_transferred)); log("received packet of invalid size: %d", int(bytes_transferred));
log(msg); #endif
return; return;
} }
@ -463,35 +468,36 @@ void natpmp::on_reply(error_code const& e
int cmd = read_uint8(in); int cmd = read_uint8(in);
int result = read_uint16(in); int result = read_uint16(in);
int time = read_uint32(in); int time = read_uint32(in);
TORRENT_UNUSED(version);
TORRENT_UNUSED(time);
if (cmd == 128) if (cmd == 128)
{ {
// public IP request response // public IP request response
m_external_ip = read_v4_address(in); m_external_ip = read_v4_address(in);
char msg[200]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "<== public IP address [ %s ]", print_address(m_external_ip).c_str()); log("<== public IP address [ %s ]", print_address(m_external_ip).c_str());
log(msg); #endif
return; return;
} }
if (bytes_transferred < 16) if (bytes_transferred < 16)
{ {
char msg[200]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "received packet of invalid size: %d", int(bytes_transferred)); log("received packet of invalid size: %d", int(bytes_transferred));
log(msg); #endif
return; return;
} }
int private_port = read_uint16(in); int const private_port = read_uint16(in);
int public_port = read_uint16(in); int const public_port = read_uint16(in);
int lifetime = read_uint32(in); int const lifetime = read_uint32(in);
(void)time; // to remove warning int const protocol = (cmd - 128 == 1)?udp:tcp;
int protocol = (cmd - 128 == 1)?udp:tcp;
#ifndef TORRENT_DISABLE_LOGGING
char msg[200]; char msg[200];
int num_chars = std::snprintf(msg, sizeof(msg), "<== port map [" int num_chars = std::snprintf(msg, sizeof(msg), "<== port map ["
" protocol: %s local: %u external: %u ttl: %u ]" " protocol: %s local: %u external: %u ttl: %u ]"
@ -502,8 +508,9 @@ void natpmp::on_reply(error_code const& e
{ {
std::snprintf(msg + num_chars, sizeof(msg) - num_chars, "unexpected version: %u" std::snprintf(msg + num_chars, sizeof(msg) - num_chars, "unexpected version: %u"
, version); , version);
log(msg); log("%s", msg);
} }
#endif
mapping_t* m = 0; mapping_t* m = 0;
int index = -1; int index = -1;
@ -521,13 +528,17 @@ void natpmp::on_reply(error_code const& e
if (m == 0) if (m == 0)
{ {
std::snprintf(msg + num_chars, sizeof(msg) - num_chars, " not found in map table"); #ifndef TORRENT_DISABLE_LOGGING
log(msg); snprintf(msg + num_chars, sizeof(msg) - num_chars, " not found in map table");
log("%s", msg);
#endif
return; return;
} }
m->outstanding_request = false; m->outstanding_request = false;
log(msg); #ifndef TORRENT_DISABLE_LOGGING
log("%s", msg);
#endif
if (public_port == 0 || lifetime == 0) if (public_port == 0 || lifetime == 0)
{ {
@ -605,9 +616,9 @@ void natpmp::update_expiration_timer()
int index = i - m_mappings.begin(); int index = i - m_mappings.begin();
if (i->expires < now) if (i->expires < now)
{ {
char msg[200]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "mapping %u expired", index); log("mapping %u expired", index);
log(msg); #endif
i->action = mapping_t::action_add; i->action = mapping_t::action_add;
if (m_next_refresh == index) m_next_refresh = -1; if (m_next_refresh == index) m_next_refresh = -1;
update_mapping(index); update_mapping(index);
@ -645,9 +656,9 @@ void natpmp::mapping_expired(error_code const& e, int i)
TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(is_single_thread());
COMPLETE_ASYNC("natpmp::mapping_expired"); COMPLETE_ASYNC("natpmp::mapping_expired");
if (e) return; if (e) return;
char msg[200]; #ifndef TORRENT_DISABLE_LOGGING
std::snprintf(msg, sizeof(msg), "mapping %u expired", i); log("mapping %u expired", i);
log(msg); #endif
m_mappings[i].action = mapping_t::action_add; m_mappings[i].action = mapping_t::action_add;
if (m_next_refresh == i) m_next_refresh = -1; if (m_next_refresh == i) m_next_refresh = -1;
update_mapping(i); update_mapping(i);
@ -663,7 +674,9 @@ void natpmp::close_impl()
{ {
TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(is_single_thread());
m_abort = true; m_abort = true;
#ifndef TORRENT_DISABLE_LOGGING
log("closing"); log("closing");
#endif
#ifdef NATPMP_LOG #ifdef NATPMP_LOG
std::cout << time_now_string() << " close" << std::endl; std::cout << time_now_string() << " close" << std::endl;
time_point now = aux::time_now(); time_point now = aux::time_now();

View File

@ -738,9 +738,9 @@ namespace libtorrent
} }
#if TORRENT_USE_IPV6 #if TORRENT_USE_IPV6
bool is_v6 = c.remote().address().is_v6(); bool const is_v6 = c.remote().address().is_v6();
#else #else
bool is_v6 = false; bool const is_v6 = false;
#endif #endif
torrent_peer* p = state->peer_allocator->allocate_peer_entry( torrent_peer* p = state->peer_allocator->allocate_peer_entry(
is_v6 ? torrent_peer_allocator_interface::ipv6_peer_type is_v6 ? torrent_peer_allocator_interface::ipv6_peer_type
@ -808,7 +808,7 @@ namespace libtorrent
TORRENT_ASSERT(pp.in_use); TORRENT_ASSERT(pp.in_use);
if (pp.connection) if (pp.connection)
{ {
bool was_conn_cand = is_connect_candidate(pp); bool const was_conn_cand = is_connect_candidate(pp);
// if we already have an entry with this // if we already have an entry with this
// new endpoint, disconnect this one // new endpoint, disconnect this one
pp.connectable = true; pp.connectable = true;
@ -844,7 +844,7 @@ namespace libtorrent
} }
#endif #endif
bool was_conn_cand = is_connect_candidate(*p); bool const was_conn_cand = is_connect_candidate(*p);
p->port = port; p->port = port;
p->source |= src; p->source |= src;
p->connectable = true; p->connectable = true;
@ -870,7 +870,7 @@ namespace libtorrent
if (p == 0) return; if (p == 0) return;
TORRENT_ASSERT(p->in_use); TORRENT_ASSERT(p->in_use);
if (p->seed == s) return; if (p->seed == s) return;
bool was_conn_cand = is_connect_candidate(*p); bool const was_conn_cand = is_connect_candidate(*p);
p->seed = s; p->seed = s;
if (was_conn_cand && !is_connect_candidate(*p)) if (was_conn_cand && !is_connect_candidate(*p))
update_connect_candidates(-1); update_connect_candidates(-1);
@ -896,7 +896,7 @@ namespace libtorrent
TORRENT_ASSERT(p); TORRENT_ASSERT(p);
TORRENT_ASSERT(p->in_use); TORRENT_ASSERT(p->in_use);
int max_peerlist_size = state->max_peerlist_size; int const max_peerlist_size = state->max_peerlist_size;
if (max_peerlist_size if (max_peerlist_size
&& int(m_peers.size()) >= max_peerlist_size) && int(m_peers.size()) >= max_peerlist_size)
@ -950,7 +950,7 @@ namespace libtorrent
, tcp::endpoint const& remote, char const* /* destination*/) , tcp::endpoint const& remote, char const* /* destination*/)
{ {
TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(is_single_thread());
bool was_conn_cand = is_connect_candidate(*p); bool const was_conn_cand = is_connect_candidate(*p);
TORRENT_ASSERT(p->in_use); TORRENT_ASSERT(p->in_use);
p->connectable = true; p->connectable = true;
@ -1094,9 +1094,9 @@ namespace libtorrent
// add a new entry // add a new entry
#if TORRENT_USE_IPV6 #if TORRENT_USE_IPV6
bool is_v6 = remote.address().is_v6(); bool const is_v6 = remote.address().is_v6();
#else #else
bool is_v6 = false; bool const is_v6 = false;
#endif #endif
p = state->peer_allocator->allocate_peer_entry( p = state->peer_allocator->allocate_peer_entry(
is_v6 ? torrent_peer_allocator_interface::ipv6_peer_type is_v6 ? torrent_peer_allocator_interface::ipv6_peer_type

View File

@ -6622,8 +6622,7 @@ namespace aux {
m_alerts.emplace_alert<dht_outgoing_get_peers_alert>(target, sent_target, ep); m_alerts.emplace_alert<dht_outgoing_get_peers_alert>(target, sent_target, ep);
} }
// TODO: 2 perhaps DHT logging should be disabled by TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
// too
TORRENT_FORMAT(3,4) TORRENT_FORMAT(3,4)
void session_impl::log(libtorrent::dht::dht_logger::module_t m, char const* fmt, ...) void session_impl::log(libtorrent::dht::dht_logger::module_t m, char const* fmt, ...)
{ {
@ -6632,7 +6631,7 @@ namespace aux {
va_list v; va_list v;
va_start(v, fmt); va_start(v, fmt);
char buf[1024]; char buf[1024];
vsnprintf(buf, sizeof(buf), fmt, v); std::vsnprintf(buf, sizeof(buf), fmt, v);
va_end(v); va_end(v);
m_alerts.emplace_alert<dht_log_alert>(static_cast<dht_log_alert::dht_module_t>(m), buf); m_alerts.emplace_alert<dht_log_alert>(static_cast<dht_log_alert::dht_module_t>(m), buf);
} }
@ -6647,6 +6646,7 @@ namespace aux {
m_alerts.emplace_alert<dht_pkt_alert>(pkt, len, d, node); m_alerts.emplace_alert<dht_pkt_alert>(pkt, len, d, node);
} }
#endif
bool session_impl::on_dht_request(char const* query, int query_len bool session_impl::on_dht_request(char const* query, int query_len
, dht::msg const& request, entry& response) , dht::msg const& request, entry& response)

View File

@ -487,6 +487,7 @@ struct obs : dht::dht_observer
virtual void outgoing_get_peers(sha1_hash const& target virtual void outgoing_get_peers(sha1_hash const& target
, sha1_hash const& sent_target, udp::endpoint const& ep) override {} , sha1_hash const& sent_target, udp::endpoint const& ep) override {}
virtual void announce(sha1_hash const& ih, address const& addr, int port) override {} virtual void announce(sha1_hash const& ih, address const& addr, int port) override {}
#ifndef TORRENT_DISABLE_LOGGING
virtual void log(dht_logger::module_t l, char const* fmt, ...) override virtual void log(dht_logger::module_t l, char const* fmt, ...) override
{ {
va_list v; va_list v;
@ -498,10 +499,13 @@ struct obs : dht::dht_observer
} }
virtual void log_packet(message_direction_t dir, char const* pkt, int len virtual void log_packet(message_direction_t dir, char const* pkt, int len
, udp::endpoint node) override {} , udp::endpoint node) override {}
#endif
virtual bool on_dht_request(char const* query, int query_len virtual bool on_dht_request(char const* query, int query_len
, dht::msg const& request, entry& response) override { return false; } , dht::msg const& request, entry& response) override { return false; }
#ifndef TORRENT_DISABLE_LOGGING
std::vector<std::string> m_log; std::vector<std::string> m_log;
#endif
}; };
dht_settings test_settings() dht_settings test_settings()
@ -2589,6 +2593,7 @@ TORRENT_TEST(read_only_node)
TEST_CHECK(!parsed[3]); TEST_CHECK(!parsed[3]);
} }
#ifndef TORRENT_DISABLE_LOGGING
TORRENT_TEST(invalid_error_msg) TORRENT_TEST(invalid_error_msg)
{ {
dht_settings sett = test_settings(); dht_settings sett = test_settings();
@ -2688,6 +2693,7 @@ TORRENT_TEST(rpc_invalid_error_msg)
TEST_EQUAL(found, true); TEST_EQUAL(found, true);
} }
#endif
// test bucket distribution // test bucket distribution
TORRENT_TEST(node_id_bucket_distribution) TORRENT_TEST(node_id_bucket_distribution)

View File

@ -42,6 +42,7 @@ POSSIBILITY OF SUCH DAMAGE.
using namespace libtorrent; using namespace libtorrent;
#ifndef TORRENT_DISABLE_LOGGING
struct log_t : libtorrent::dht::dht_logger struct log_t : libtorrent::dht::dht_logger
{ {
virtual void log(dht_logger::module_t m, char const* fmt, ...) virtual void log(dht_logger::module_t m, char const* fmt, ...)
@ -69,9 +70,11 @@ struct log_t : libtorrent::dht::dht_logger
, msg.c_str()); , msg.c_str());
} }
}; };
#endif
TORRENT_TEST(dos_blocker) TORRENT_TEST(dos_blocker)
{ {
#ifndef TORRENT_DISABLE_LOGGING
#ifndef TORRENT_DISABLE_DHT #ifndef TORRENT_DISABLE_DHT
using namespace libtorrent::dht; using namespace libtorrent::dht;
@ -93,5 +96,6 @@ TORRENT_TEST(dos_blocker)
TEST_EQUAL(b.incoming(spammer, now, &l), false); TEST_EQUAL(b.incoming(spammer, now, &l), false);
#endif #endif
#endif
} }

View File

@ -44,14 +44,16 @@ POSSIBILITY OF SUCH DAMAGE.
#include "test.hpp" #include "test.hpp"
#include "setup_transfer.hpp" #include "setup_transfer.hpp"
#include <vector> #include <vector>
#include <memory> // for shared_ptr
#include <stdarg.h> #include <stdarg.h>
using namespace libtorrent; using namespace libtorrent;
struct mock_torrent; struct mock_torrent;
struct mock_peer_connection : peer_connection_interface struct mock_peer_connection
, boost::enable_shared_from_this<mock_peer_connection> : peer_connection_interface
, std::enable_shared_from_this<mock_peer_connection>
{ {
mock_peer_connection(mock_torrent* tor, bool out, tcp::endpoint const& remote) mock_peer_connection(mock_torrent* tor, bool out, tcp::endpoint const& remote)
: m_choked(false) : m_choked(false)
@ -69,7 +71,7 @@ struct mock_peer_connection : peer_connection_interface
#if !defined TORRENT_DISABLE_LOGGING #if !defined TORRENT_DISABLE_LOGGING
virtual void peer_log(peer_log_alert::direction_t dir, char const* event virtual void peer_log(peer_log_alert::direction_t dir, char const* event
, char const* fmt, ...) const , char const* fmt, ...) const override
{ {
va_list v; va_list v;
va_start(v, fmt); va_start(v, fmt);
@ -91,21 +93,21 @@ struct mock_peer_connection : peer_connection_interface
bool m_disconnect_called; bool m_disconnect_called;
mock_torrent& m_torrent; mock_torrent& m_torrent;
virtual void get_peer_info(peer_info& p) const {} virtual void get_peer_info(peer_info& p) const override {}
virtual tcp::endpoint const& remote() const { return m_remote; } virtual tcp::endpoint const& remote() const override { return m_remote; }
virtual tcp::endpoint local_endpoint() const { return m_local; } virtual tcp::endpoint local_endpoint() const override { return m_local; }
virtual void disconnect(error_code const& ec virtual void disconnect(error_code const& ec
, operation_t op, int error = 0); , operation_t op, int error = 0);
virtual peer_id const& pid() const { return m_id; } virtual peer_id const& pid() const override { return m_id; }
virtual void set_holepunch_mode() {} virtual void set_holepunch_mode() override {}
virtual torrent_peer* peer_info_struct() const { return m_tp; } virtual torrent_peer* peer_info_struct() const override { return m_tp; }
virtual void set_peer_info(torrent_peer* pi) { m_tp = pi; } virtual void set_peer_info(torrent_peer* pi) override { m_tp = pi; }
virtual bool is_outgoing() const { return m_outgoing; } virtual bool is_outgoing() const override { return m_outgoing; }
virtual void add_stat(boost::int64_t downloaded, boost::int64_t uploaded) virtual void add_stat(boost::int64_t downloaded, boost::int64_t uploaded) override
{ m_stat.add_stat(downloaded, uploaded); } { m_stat.add_stat(downloaded, uploaded); }
virtual bool fast_reconnect() const { return true; } virtual bool fast_reconnect() const override { return true; }
virtual bool is_choked() const { return m_choked; } virtual bool is_choked() const override { return m_choked; }
virtual bool failed() const { return false; } virtual bool failed() const override { return false; }
virtual libtorrent::stat const& statistics() const { return m_stat; } virtual libtorrent::stat const& statistics() const { return m_stat; }
}; };
@ -118,8 +120,7 @@ struct mock_torrent
{ {
TORRENT_ASSERT(peerinfo->connection == NULL); TORRENT_ASSERT(peerinfo->connection == NULL);
if (peerinfo->connection) return false; if (peerinfo->connection) return false;
boost::shared_ptr<mock_peer_connection> c auto c = std::make_shared<mock_peer_connection>(this, true, peerinfo->ip());
= boost::make_shared<mock_peer_connection>(this, true, peerinfo->ip());
c->set_peer_info(peerinfo); c->set_peer_info(peerinfo);
m_connections.push_back(c); m_connections.push_back(c);
@ -139,16 +140,16 @@ struct mock_torrent
peer_list* m_p; peer_list* m_p;
torrent_state* m_state; torrent_state* m_state;
std::vector<boost::shared_ptr<mock_peer_connection> > m_connections; std::vector<std::shared_ptr<mock_peer_connection> > m_connections;
}; };
void mock_peer_connection::disconnect(error_code const& ec void mock_peer_connection::disconnect(error_code const& ec
, operation_t op, int error) , operation_t op, int error)
{ {
m_torrent.m_p->connection_closed(*this, 0, m_torrent.m_state); m_torrent.m_p->connection_closed(*this, 0, m_torrent.m_state);
std::vector<boost::shared_ptr<mock_peer_connection> >::iterator i std::vector<std::shared_ptr<mock_peer_connection> >::iterator i
= std::find(m_torrent.m_connections.begin(), m_torrent.m_connections.end() = std::find(m_torrent.m_connections.begin(), m_torrent.m_connections.end()
, shared_from_this()); , std::static_pointer_cast<mock_peer_connection>(shared_from_this()));
if (i != m_torrent.m_connections.end()) m_torrent.m_connections.erase(i); if (i != m_torrent.m_connections.end()) m_torrent.m_connections.erase(i);
m_tp = 0; m_tp = 0;
@ -329,8 +330,7 @@ TORRENT_TEST(update_peer_port)
peer_list p; peer_list p;
t.m_p = &p; t.m_p = &p;
TEST_EQUAL(p.num_connect_candidates(), 0); TEST_EQUAL(p.num_connect_candidates(), 0);
boost::shared_ptr<mock_peer_connection> c auto c = std::make_shared<mock_peer_connection>(&t, true, ep("10.0.0.1", 8080));
= boost::make_shared<mock_peer_connection>(&t, true, ep("10.0.0.1", 8080));
p.new_connection(*c, 0, &st); p.new_connection(*c, 0, &st);
TEST_EQUAL(p.num_connect_candidates(), 0); TEST_EQUAL(p.num_connect_candidates(), 0);
TEST_EQUAL(p.num_peers(), 1); TEST_EQUAL(p.num_peers(), 1);
@ -357,8 +357,7 @@ TORRENT_TEST(update_peer_port_collide)
TEST_CHECK(peer2); TEST_CHECK(peer2);
TEST_EQUAL(p.num_connect_candidates(), 1); TEST_EQUAL(p.num_connect_candidates(), 1);
boost::shared_ptr<mock_peer_connection> c auto c = std::make_shared<mock_peer_connection>(&t, true, ep("10.0.0.1", 8080));
= boost::make_shared<mock_peer_connection>(&t, true, ep("10.0.0.1", 8080));
p.new_connection(*c, 0, &st); p.new_connection(*c, 0, &st);
TEST_EQUAL(p.num_connect_candidates(), 1); TEST_EQUAL(p.num_connect_candidates(), 1);
// at this point we have two peers, because we think they have different // at this point we have two peers, because we think they have different
@ -375,6 +374,12 @@ TORRENT_TEST(update_peer_port_collide)
st.erased.clear(); st.erased.clear();
} }
std::shared_ptr<mock_peer_connection> shared_from_this(libtorrent::peer_connection_interface* p)
{
return std::static_pointer_cast<mock_peer_connection>(
static_cast<mock_peer_connection*>(p)->shared_from_this());
}
// test ip filter // test ip filter
TORRENT_TEST(ip_filter) TORRENT_TEST(ip_filter)
{ {
@ -393,11 +398,9 @@ TORRENT_TEST(ip_filter)
connect_peer(p, t, st); connect_peer(p, t, st);
connect_peer(p, t, st); connect_peer(p, t, st);
boost::shared_ptr<mock_peer_connection> con1 auto con1 = shared_from_this(peer1->connection);
= static_cast<mock_peer_connection*>(peer1->connection)->shared_from_this();
TEST_EQUAL(con1->was_disconnected(), false); TEST_EQUAL(con1->was_disconnected(), false);
boost::shared_ptr<mock_peer_connection> con2 auto con2 = shared_from_this(peer2->connection);
= static_cast<mock_peer_connection*>(peer2->connection)->shared_from_this();
TEST_EQUAL(con2->was_disconnected(), false); TEST_EQUAL(con2->was_disconnected(), false);
// now, filter one of the IPs and make sure the peer is removed // now, filter one of the IPs and make sure the peer is removed
@ -433,11 +436,9 @@ TORRENT_TEST(port_filter)
connect_peer(p, t, st); connect_peer(p, t, st);
connect_peer(p, t, st); connect_peer(p, t, st);
boost::shared_ptr<mock_peer_connection> con1 auto con1 = shared_from_this(peer1->connection);
= static_cast<mock_peer_connection*>(peer1->connection)->shared_from_this();
TEST_EQUAL(con1->was_disconnected(), false); TEST_EQUAL(con1->was_disconnected(), false);
boost::shared_ptr<mock_peer_connection> con2 auto con2 = shared_from_this(peer2->connection);
= static_cast<mock_peer_connection*>(peer2->connection)->shared_from_this();
TEST_EQUAL(con2->was_disconnected(), false); TEST_EQUAL(con2->was_disconnected(), false);
// now, filter one of the IPs and make sure the peer is removed // now, filter one of the IPs and make sure the peer is removed
@ -467,8 +468,7 @@ TORRENT_TEST(ban_peers)
torrent_peer* peer1 = add_peer(p, st, ep("10.0.0.1", 4000)); torrent_peer* peer1 = add_peer(p, st, ep("10.0.0.1", 4000));
TEST_EQUAL(p.num_connect_candidates(), 1); TEST_EQUAL(p.num_connect_candidates(), 1);
boost::shared_ptr<mock_peer_connection> c auto c = std::make_shared<mock_peer_connection>(&t, true, ep("10.0.0.1", 8080));
= boost::make_shared<mock_peer_connection>(&t, true, ep("10.0.0.1", 8080));
p.new_connection(*c, 0, &st); p.new_connection(*c, 0, &st);
TEST_EQUAL(p.num_connect_candidates(), 0); TEST_EQUAL(p.num_connect_candidates(), 0);
TEST_EQUAL(p.num_peers(), 1); TEST_EQUAL(p.num_peers(), 1);
@ -488,7 +488,7 @@ TORRENT_TEST(ban_peers)
TEST_EQUAL(p.num_connect_candidates(), 0); TEST_EQUAL(p.num_connect_candidates(), 0);
st.erased.clear(); st.erased.clear();
c = boost::make_shared<mock_peer_connection>(&t, true, ep("10.0.0.1", 8080)); c = std::make_shared<mock_peer_connection>(&t, true, ep("10.0.0.1", 8080));
ok = p.new_connection(*c, 0, &st); ok = p.new_connection(*c, 0, &st);
// since it's banned, we should not allow this incoming connection // since it's banned, we should not allow this incoming connection
TEST_EQUAL(ok, false); TEST_EQUAL(ok, false);
@ -740,12 +740,10 @@ TORRENT_TEST(self_connection)
torrent_peer* peer = add_peer(p, st, ep("10.0.0.2", 3000)); torrent_peer* peer = add_peer(p, st, ep("10.0.0.2", 3000));
connect_peer(p, t, st); connect_peer(p, t, st);
boost::shared_ptr<mock_peer_connection> con_out auto con_out = shared_from_this(peer->connection);
= static_cast<mock_peer_connection*>(peer->connection)->shared_from_this();
con_out->set_local_ep(ep("10.0.0.2", 8080)); con_out->set_local_ep(ep("10.0.0.2", 8080));
boost::shared_ptr<mock_peer_connection> con_in auto con_in = std::make_shared<mock_peer_connection>(&t, false, ep("10.0.0.2", 8080));
= boost::make_shared<mock_peer_connection>(&t, false, ep("10.0.0.2", 8080));
con_in->set_local_ep(ep("10.0.0.2", 3000)); con_in->set_local_ep(ep("10.0.0.2", 3000));
p.new_connection(*con_in, 0, &st); p.new_connection(*con_in, 0, &st);
@ -771,15 +769,13 @@ TORRENT_TEST(double_connection)
// we are 10.0.0.1 and the other peer is 10.0.0.2 // we are 10.0.0.1 and the other peer is 10.0.0.2
// first incoming connection // first incoming connection
boost::shared_ptr<mock_peer_connection> con1 auto con1 = std::make_shared<mock_peer_connection>(&t, false, ep("10.0.0.2", 7528));
= boost::make_shared<mock_peer_connection>(&t, false, ep("10.0.0.2", 7528));
con1->set_local_ep(ep("10.0.0.1", 8080)); con1->set_local_ep(ep("10.0.0.1", 8080));
p.new_connection(*con1, 0, &st); p.new_connection(*con1, 0, &st);
// and the incoming connection // and the incoming connection
boost::shared_ptr<mock_peer_connection> con2 auto con2 = std::make_shared<mock_peer_connection>(&t, false, ep("10.0.0.2", 3561));
= boost::make_shared<mock_peer_connection>(&t, false, ep("10.0.0.2", 3561));
con2->set_local_ep(ep("10.0.0.1", 8080)); con2->set_local_ep(ep("10.0.0.1", 8080));
p.new_connection(*con2, 0, &st); p.new_connection(*con2, 0, &st);
@ -804,13 +800,11 @@ TORRENT_TEST(double_connection_loose)
torrent_peer* peer = add_peer(p, st, ep("10.0.0.2", 3000)); torrent_peer* peer = add_peer(p, st, ep("10.0.0.2", 3000));
connect_peer(p, t, st); connect_peer(p, t, st);
boost::shared_ptr<mock_peer_connection> con_out auto con_out = shared_from_this(peer->connection);
= static_cast<mock_peer_connection*>(peer->connection)->shared_from_this();
con_out->set_local_ep(ep("10.0.0.1", 3163)); con_out->set_local_ep(ep("10.0.0.1", 3163));
// and the incoming connection // and the incoming connection
boost::shared_ptr<mock_peer_connection> con_in auto con_in = std::make_shared<mock_peer_connection>(&t, false, ep("10.0.0.2", 3561));
= boost::make_shared<mock_peer_connection>(&t, false, ep("10.0.0.2", 3561));
con_in->set_local_ep(ep("10.0.0.1", 8080)); con_in->set_local_ep(ep("10.0.0.1", 8080));
p.new_connection(*con_in, 0, &st); p.new_connection(*con_in, 0, &st);
@ -835,13 +829,11 @@ TORRENT_TEST(double_connection_win)
torrent_peer* peer = add_peer(p, st, ep("10.0.0.2", 8080)); torrent_peer* peer = add_peer(p, st, ep("10.0.0.2", 8080));
connect_peer(p, t, st); connect_peer(p, t, st);
boost::shared_ptr<mock_peer_connection> con_out auto con_out = shared_from_this(peer->connection);
= static_cast<mock_peer_connection*>(peer->connection)->shared_from_this();
con_out->set_local_ep(ep("10.0.0.1", 3163)); con_out->set_local_ep(ep("10.0.0.1", 3163));
//and the incoming connection //and the incoming connection
boost::shared_ptr<mock_peer_connection> con_in auto con_in = std::make_shared<mock_peer_connection>(&t, false, ep("10.0.0.2", 3561));
= boost::make_shared<mock_peer_connection>(&t, false, ep("10.0.0.2", 3561));
con_in->set_local_ep(ep("10.0.0.1", 3000)); con_in->set_local_ep(ep("10.0.0.1", 3000));
p.new_connection(*con_in, 0, &st); p.new_connection(*con_in, 0, &st);
@ -877,8 +869,7 @@ TORRENT_TEST(incoming_size_limit)
TEST_CHECK(peer5); TEST_CHECK(peer5);
TEST_EQUAL(p.num_peers(), 5); TEST_EQUAL(p.num_peers(), 5);
boost::shared_ptr<mock_peer_connection> con_in auto con_in = std::make_shared<mock_peer_connection>(&t, false, ep("10.0.1.2", 3561));
= boost::make_shared<mock_peer_connection>(&t, false, ep("10.0.1.2", 3561));
con_in->set_local_ep(ep("10.0.2.1", 3000)); con_in->set_local_ep(ep("10.0.2.1", 3000));
// since we're already at 5 peers in the peer list, this call should // since we're already at 5 peers in the peer list, this call should