handle receiving an invalid request

This commit is contained in:
arvidn 2015-11-06 01:15:48 -05:00 committed by arvidn
parent 06f702b21a
commit a138b2daa9
2 changed files with 45 additions and 2 deletions

View File

@ -2219,12 +2219,17 @@ namespace libtorrent
m_counters.inc_stats_counter(counters::invalid_piece_requests);
++m_num_invalid_requests;
#ifndef TORRENT_DISABLE_LOGGING
const bool valid_piece_index
= r.piece >= 0 && r.piece < int(t->torrent_file().num_pieces());
peer_log(peer_log_alert::info, "INVALID_REQUEST", "piece not superseeded "
"i: %d t: %d n: %d h: %d ss1: %d ss2: %d"
, m_peer_interested
, int(t->torrent_file().piece_size(r.piece))
, valid_piece_index
? int(t->torrent_file().piece_size(r.piece))
: -1
, t->torrent_file().num_pieces()
, t->has_piece_passed(r.piece)
, valid_piece_index ? t->has_piece_passed(r.piece) : 0
, m_superseed_piece[0]
, m_superseed_piece[1]);
#endif

View File

@ -311,6 +311,22 @@ void send_extension_handshake(tcp::socket& s, entry const& e)
if (ec) TEST_ERROR(ec.message());
}
void send_request(tcp::socket& s, peer_request req)
{
using namespace libtorrent::detail;
log("==> request %d (%d,%d)", req.piece, req.start, req.length);
char msg[] = "\0\0\0\x0d\x06 "; // have_none
char* ptr = msg + 5;
write_uint32(req.piece, ptr);
write_uint32(req.start, ptr);
write_uint32(req.length, ptr);
error_code ec;
boost::asio::write(s, boost::asio::buffer(msg, 17)
, boost::asio::transfer_all(), ec);
if (ec) TEST_ERROR(ec.message());
}
entry read_extension_handshake(tcp::socket& s, char* recv_buffer, int size)
{
for (;;)
@ -837,6 +853,28 @@ TORRENT_TEST(invalid_metadata_request)
print_session_log(*ses);
}
TORRENT_TEST(invalid_request)
{
std::cerr << "\n === test request ===\n" << std::endl;
sha1_hash ih;
boost::shared_ptr<lt::session> ses;
io_service ios;
tcp::socket s(ios);
setup_peer(s, ih, ses);
char recv_buffer[1000];
do_handshake(s, ih, recv_buffer);
print_session_log(*ses);
send_have_none(s);
peer_request req;
req.piece = 124134235;
req.start = 0;
req.length = 0x4000;
send_request(s, req);
}
#endif // TORRENT_DISABLE_EXTENSIONS
// TODO: test sending invalid requests (out of bound piece index, offsets and