2007-12-02 05:16:51 +01:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2007-2016, Arvid Norberg
|
2007-12-02 05:16:51 +01:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2011-01-29 13:13:49 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
|
2015-04-21 03:16:28 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
2007-12-02 05:16:51 +01:00
|
|
|
|
|
|
|
#include <boost/shared_ptr.hpp>
|
2014-07-06 21:18:00 +02:00
|
|
|
#include <boost/bind.hpp>
|
2007-12-02 05:16:51 +01:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <utility>
|
|
|
|
#include <numeric>
|
|
|
|
#include <cstdio>
|
|
|
|
|
2015-04-21 03:16:28 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
2015-04-18 04:33:39 +02:00
|
|
|
|
2007-12-02 05:16:51 +01:00
|
|
|
#include "libtorrent/peer_connection.hpp"
|
|
|
|
#include "libtorrent/bt_peer_connection.hpp"
|
2015-07-25 18:15:24 +02:00
|
|
|
#include "libtorrent/peer_connection_handle.hpp"
|
2007-12-02 05:16:51 +01:00
|
|
|
#include "libtorrent/hasher.hpp"
|
|
|
|
#include "libtorrent/bencode.hpp"
|
|
|
|
#include "libtorrent/torrent.hpp"
|
2015-07-25 18:39:25 +02:00
|
|
|
#include "libtorrent/torrent_handle.hpp"
|
2007-12-02 05:16:51 +01:00
|
|
|
#include "libtorrent/extensions.hpp"
|
2008-05-14 07:29:42 +02:00
|
|
|
#include "libtorrent/extensions/ut_metadata.hpp"
|
2007-12-02 05:16:51 +01:00
|
|
|
#include "libtorrent/alert_types.hpp"
|
2011-02-26 08:55:51 +01:00
|
|
|
#include "libtorrent/random.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/io.hpp"
|
|
|
|
#include "libtorrent/performance_counters.hpp" // for counters
|
|
|
|
|
2007-12-02 05:16:51 +01:00
|
|
|
namespace libtorrent { namespace
|
|
|
|
{
|
2012-07-18 05:27:50 +02:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
// this is the max number of bytes we'll
|
|
|
|
// queue up in the send buffer. If we exceed this,
|
|
|
|
// we'll wait another second before checking
|
|
|
|
// the send buffer size again. So, this may limit
|
|
|
|
// the rate at which we can server metadata to
|
|
|
|
// 160 kiB/s
|
|
|
|
send_buffer_limit = 0x4000 * 10,
|
|
|
|
|
|
|
|
// this is the max number of requests we'll queue
|
|
|
|
// up. If we get more requests tha this, we'll
|
|
|
|
// start rejecting them, claiming we don't have
|
|
|
|
// metadata. If the torrent is greater than 16 MiB,
|
|
|
|
// we may hit this case (and the client requesting
|
|
|
|
// doesn't throttle its requests)
|
2015-04-09 06:24:13 +02:00
|
|
|
max_incoming_requests = 1024,
|
|
|
|
|
|
|
|
metadata_req = 0,
|
|
|
|
metadata_piece = 1,
|
|
|
|
metadata_dont_have = 2
|
2012-07-18 05:27:50 +02:00
|
|
|
};
|
|
|
|
|
2007-12-02 05:16:51 +01:00
|
|
|
int div_round_up(int numerator, int denominator)
|
|
|
|
{
|
|
|
|
return (numerator + denominator - 1) / denominator;
|
|
|
|
}
|
|
|
|
|
2011-01-27 07:49:32 +01:00
|
|
|
struct ut_metadata_peer_plugin;
|
|
|
|
|
2015-11-20 05:37:45 +01:00
|
|
|
struct ut_metadata_plugin TORRENT_FINAL
|
|
|
|
: torrent_plugin
|
2007-12-02 05:16:51 +01:00
|
|
|
{
|
|
|
|
ut_metadata_plugin(torrent& t)
|
|
|
|
: m_torrent(t)
|
2015-09-02 07:30:40 +02:00
|
|
|
// , m_metadata_progress(0)
|
2007-12-02 05:16:51 +01:00
|
|
|
, m_metadata_size(0)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
// initialize m_metadata_size
|
|
|
|
if (m_torrent.valid_metadata())
|
|
|
|
metadata();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool need_loaded()
|
|
|
|
{ return m_torrent.need_loaded(); }
|
|
|
|
|
2015-11-20 05:37:45 +01:00
|
|
|
virtual void on_unload() TORRENT_OVERRIDE
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
|
|
|
m_metadata.reset();
|
|
|
|
}
|
|
|
|
|
2015-11-20 05:37:45 +01:00
|
|
|
virtual void on_load() TORRENT_OVERRIDE
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
|
|
|
// initialize m_metadata_size
|
|
|
|
TORRENT_ASSERT(m_torrent.is_loaded());
|
|
|
|
metadata();
|
2007-12-02 05:16:51 +01:00
|
|
|
}
|
|
|
|
|
2015-11-20 05:37:45 +01:00
|
|
|
virtual void on_files_checked() TORRENT_OVERRIDE
|
2007-12-02 05:16:51 +01:00
|
|
|
{
|
2015-02-17 03:08:47 +01:00
|
|
|
// TODO: 2 if we were to initialize m_metadata_size lazily instead,
|
|
|
|
// we would probably be more efficient
|
2014-07-06 21:18:00 +02:00
|
|
|
// initialize m_metadata_size
|
|
|
|
metadata();
|
2007-12-02 05:16:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual boost::shared_ptr<peer_plugin> new_connection(
|
2015-11-20 05:37:45 +01:00
|
|
|
peer_connection_handle const& pc) TORRENT_OVERRIDE;
|
2015-05-28 16:46:12 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
int get_metadata_size() const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_metadata_size > 0);
|
|
|
|
return m_metadata_size;
|
|
|
|
}
|
|
|
|
|
2008-05-14 07:29:42 +02:00
|
|
|
buffer::const_interval metadata() const
|
2007-12-02 05:16:51 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
if (!m_torrent.need_loaded()) return buffer::const_interval(NULL, NULL);
|
2007-12-02 05:16:51 +01:00
|
|
|
TORRENT_ASSERT(m_torrent.valid_metadata());
|
2008-05-14 07:29:42 +02:00
|
|
|
if (!m_metadata)
|
2007-12-02 05:16:51 +01:00
|
|
|
{
|
2008-05-14 07:29:42 +02:00
|
|
|
m_metadata = m_torrent.torrent_file().metadata();
|
|
|
|
m_metadata_size = m_torrent.torrent_file().metadata_size();
|
|
|
|
TORRENT_ASSERT(hasher(m_metadata.get(), m_metadata_size).final()
|
2007-12-02 05:16:51 +01:00
|
|
|
== m_torrent.torrent_file().info_hash());
|
|
|
|
}
|
2008-05-14 07:29:42 +02:00
|
|
|
return buffer::const_interval(m_metadata.get(), m_metadata.get()
|
|
|
|
+ m_metadata_size);
|
2007-12-02 05:16:51 +01:00
|
|
|
}
|
|
|
|
|
2011-04-02 10:13:35 +02:00
|
|
|
bool received_metadata(ut_metadata_peer_plugin& source
|
2011-01-27 07:49:32 +01:00
|
|
|
, char const* buf, int size, int piece, int total_size);
|
2007-12-02 05:16:51 +01:00
|
|
|
|
|
|
|
// returns a piece of the metadata that
|
|
|
|
// we should request.
|
2011-01-29 11:37:21 +01:00
|
|
|
// returns -1 if we should hold off the request
|
2012-08-14 18:06:32 +02:00
|
|
|
int metadata_request(bool has_metadata);
|
2015-09-02 07:30:40 +02:00
|
|
|
/*
|
2007-12-02 05:16:51 +01:00
|
|
|
// this is called from the peer_connection for
|
|
|
|
// each piece of metadata it receives
|
|
|
|
void metadata_progress(int total_size, int received)
|
|
|
|
{
|
|
|
|
m_metadata_progress += received;
|
|
|
|
m_metadata_size = total_size;
|
2010-12-30 02:47:30 +01:00
|
|
|
m_torrent.set_progress_ppm(boost::int64_t(m_metadata_progress) * 1000000 / m_metadata_size);
|
2007-12-02 05:16:51 +01:00
|
|
|
}
|
2015-09-02 07:30:40 +02:00
|
|
|
*/
|
2016-01-23 19:37:50 +01:00
|
|
|
void on_piece_pass(int) TORRENT_OVERRIDE
|
2007-12-02 05:16:51 +01:00
|
|
|
{
|
|
|
|
// if we became a seed, copy the metadata from
|
|
|
|
// the torrent before it is deallocated
|
|
|
|
if (m_torrent.is_seed())
|
|
|
|
metadata();
|
|
|
|
}
|
|
|
|
|
|
|
|
void metadata_size(int size)
|
|
|
|
{
|
2012-02-07 10:18:07 +01:00
|
|
|
if (m_metadata_size > 0 || size <= 0 || size > 4 * 1024 * 1024) return;
|
2007-12-02 05:16:51 +01:00
|
|
|
m_metadata_size = size;
|
2008-05-14 07:29:42 +02:00
|
|
|
m_metadata.reset(new char[size]);
|
2011-01-27 07:49:32 +01:00
|
|
|
m_requested_metadata.resize(div_round_up(size, 16 * 1024));
|
2007-12-02 05:16:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
torrent& m_torrent;
|
|
|
|
|
|
|
|
// this buffer is filled with the info-section of
|
|
|
|
// the metadata file while downloading it from
|
|
|
|
// peers, and while sending it.
|
|
|
|
// it is mutable because it's generated lazily
|
2008-05-14 07:29:42 +02:00
|
|
|
mutable boost::shared_array<char> m_metadata;
|
2007-12-02 05:16:51 +01:00
|
|
|
|
2015-09-02 07:30:40 +02:00
|
|
|
// int m_metadata_progress;
|
2008-05-14 07:29:42 +02:00
|
|
|
mutable int m_metadata_size;
|
2007-12-02 05:16:51 +01:00
|
|
|
|
2011-01-27 07:49:32 +01:00
|
|
|
struct metadata_piece
|
|
|
|
{
|
2014-08-16 22:55:44 +02:00
|
|
|
metadata_piece(): num_requests(0), last_request(min_time()) {}
|
2011-01-27 07:49:32 +01:00
|
|
|
int num_requests;
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point last_request;
|
2011-01-27 07:49:32 +01:00
|
|
|
boost::weak_ptr<ut_metadata_peer_plugin> source;
|
|
|
|
bool operator<(metadata_piece const& rhs) const
|
|
|
|
{ return num_requests < rhs.num_requests; }
|
|
|
|
};
|
|
|
|
|
2016-01-04 03:20:47 +01:00
|
|
|
// this vector keeps track of how many times each metadata
|
2011-01-27 07:49:32 +01:00
|
|
|
// block has been requested and who we ended up getting it from
|
2007-12-02 05:16:51 +01:00
|
|
|
// std::numeric_limits<int>::max() means we have the piece
|
2011-01-27 07:49:32 +01:00
|
|
|
std::vector<metadata_piece> m_requested_metadata;
|
2015-11-20 05:37:45 +01:00
|
|
|
|
|
|
|
// explicitly disallow assignment, to silence msvc warning
|
|
|
|
ut_metadata_plugin& operator=(ut_metadata_plugin const&);
|
2007-12-02 05:16:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-11-20 05:37:45 +01:00
|
|
|
struct ut_metadata_peer_plugin TORRENT_FINAL
|
|
|
|
: peer_plugin, boost::enable_shared_from_this<ut_metadata_peer_plugin>
|
2007-12-02 05:16:51 +01:00
|
|
|
{
|
2011-04-02 10:13:35 +02:00
|
|
|
friend struct ut_metadata_plugin;
|
|
|
|
|
2007-12-02 05:16:51 +01:00
|
|
|
ut_metadata_peer_plugin(torrent& t, bt_peer_connection& pc
|
|
|
|
, ut_metadata_plugin& tp)
|
|
|
|
: m_message_index(0)
|
2011-01-27 07:49:32 +01:00
|
|
|
, m_request_limit(min_time())
|
2007-12-02 05:16:51 +01:00
|
|
|
, m_torrent(t)
|
|
|
|
, m_pc(pc)
|
|
|
|
, m_tp(tp)
|
|
|
|
{}
|
|
|
|
|
2015-11-20 05:37:45 +01:00
|
|
|
virtual char const* type() const TORRENT_OVERRIDE { return "ut_metadata"; }
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2007-12-02 05:16:51 +01:00
|
|
|
// can add entries to the extension handshake
|
2015-11-20 05:37:45 +01:00
|
|
|
virtual void add_handshake(entry& h) TORRENT_OVERRIDE
|
2007-12-02 05:16:51 +01:00
|
|
|
{
|
|
|
|
entry& messages = h["m"];
|
2013-02-23 07:40:27 +01:00
|
|
|
messages["ut_metadata"] = 2;
|
2007-12-02 05:16:51 +01:00
|
|
|
if (m_torrent.valid_metadata())
|
2014-07-06 21:18:00 +02:00
|
|
|
h["metadata_size"] = m_tp.get_metadata_size();
|
2007-12-02 05:16:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// called when the extension handshake from the other end is received
|
2015-11-20 05:37:45 +01:00
|
|
|
virtual bool on_extension_handshake(bdecode_node const& h) TORRENT_OVERRIDE
|
2007-12-02 05:16:51 +01:00
|
|
|
{
|
2008-07-01 10:04:12 +02:00
|
|
|
m_message_index = 0;
|
2015-03-12 06:20:12 +01:00
|
|
|
if (h.type() != bdecode_node::dict_t) return false;
|
|
|
|
bdecode_node messages = h.dict_find_dict("m");
|
2011-05-28 00:47:14 +02:00
|
|
|
if (!messages) return false;
|
2008-07-01 10:04:12 +02:00
|
|
|
|
2015-03-12 06:20:12 +01:00
|
|
|
int index = messages.dict_find_int_value("ut_metadata", -1);
|
2008-07-01 10:04:12 +02:00
|
|
|
if (index == -1) return false;
|
|
|
|
m_message_index = index;
|
|
|
|
|
|
|
|
int metadata_size = h.dict_find_int_value("metadata_size");
|
|
|
|
if (metadata_size > 0)
|
|
|
|
m_tp.metadata_size(metadata_size);
|
2012-02-07 10:18:07 +01:00
|
|
|
else
|
|
|
|
m_pc.set_has_metadata(false);
|
|
|
|
|
2010-12-17 04:12:45 +01:00
|
|
|
maybe_send_request();
|
2008-04-07 03:39:29 +02:00
|
|
|
return true;
|
2007-12-02 05:16:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void write_metadata_packet(int type, int piece)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(type >= 0 && type <= 2);
|
|
|
|
TORRENT_ASSERT(!m_pc.associated_torrent().expired());
|
|
|
|
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2017-01-23 08:01:47 +01:00
|
|
|
static char const* names[] = {"request", "data", "dont-have"};
|
2013-10-12 08:03:19 +02:00
|
|
|
char const* n = "";
|
|
|
|
if (type >= 0 && type < 3) n = names[type];
|
2015-05-03 04:53:54 +02:00
|
|
|
m_pc.peer_log(peer_log_alert::outgoing_message, "UT_METADATA"
|
|
|
|
, "type: %d (%s) piece: %d", type, n, piece);
|
2007-12-02 05:16:51 +01:00
|
|
|
#endif
|
2011-01-27 07:49:32 +01:00
|
|
|
|
2007-12-02 05:16:51 +01:00
|
|
|
// abort if the peer doesn't support the metadata extension
|
|
|
|
if (m_message_index == 0) return;
|
|
|
|
|
2007-12-22 09:15:05 +01:00
|
|
|
entry e;
|
|
|
|
e["msg_type"] = type;
|
|
|
|
e["piece"] = piece;
|
2007-12-02 05:16:51 +01:00
|
|
|
|
|
|
|
char const* metadata = 0;
|
|
|
|
int metadata_piece_size = 0;
|
|
|
|
|
2015-04-09 06:24:13 +02:00
|
|
|
if (m_torrent.valid_metadata())
|
|
|
|
e["total_size"] = m_tp.get_metadata_size();
|
|
|
|
|
2007-12-02 05:16:51 +01:00
|
|
|
if (type == 1)
|
|
|
|
{
|
2015-04-09 06:24:13 +02:00
|
|
|
TORRENT_ASSERT(piece >= 0 && piece < int(m_tp.get_metadata_size() + 16 * 1024 - 1)/(16*1024));
|
2007-12-02 05:16:51 +01:00
|
|
|
TORRENT_ASSERT(m_pc.associated_torrent().lock()->valid_metadata());
|
2015-04-09 06:24:13 +02:00
|
|
|
TORRENT_ASSERT(m_torrent.valid_metadata());
|
|
|
|
|
2007-12-02 05:16:51 +01:00
|
|
|
int offset = piece * 16 * 1024;
|
2014-07-06 21:18:00 +02:00
|
|
|
// unloaded torrents don't have any metadata. Since we're
|
|
|
|
// about to send the metadata, we need it to be loaded
|
|
|
|
if (!m_tp.need_loaded()) return;
|
2008-05-14 07:29:42 +02:00
|
|
|
metadata = m_tp.metadata().begin + offset;
|
2007-12-22 09:15:05 +01:00
|
|
|
metadata_piece_size = (std::min)(
|
2014-07-06 21:18:00 +02:00
|
|
|
int(m_tp.get_metadata_size() - offset), 16 * 1024);
|
2007-12-02 05:16:51 +01:00
|
|
|
TORRENT_ASSERT(metadata_piece_size > 0);
|
|
|
|
TORRENT_ASSERT(offset >= 0);
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(offset + metadata_piece_size <= int(m_tp.get_metadata_size()));
|
2007-12-02 05:16:51 +01:00
|
|
|
}
|
|
|
|
|
2007-12-22 09:15:05 +01:00
|
|
|
char msg[200];
|
|
|
|
char* header = msg;
|
|
|
|
char* p = &msg[6];
|
|
|
|
int len = bencode(p, e);
|
|
|
|
int total_size = 2 + len + metadata_piece_size;
|
2007-12-02 05:16:51 +01:00
|
|
|
namespace io = detail;
|
2007-12-22 09:15:05 +01:00
|
|
|
io::write_uint32(total_size, header);
|
|
|
|
io::write_uint8(bt_peer_connection::msg_extended, header);
|
|
|
|
io::write_uint8(m_message_index, header);
|
|
|
|
|
|
|
|
m_pc.send_buffer(msg, len + 6);
|
2014-07-06 21:18:00 +02:00
|
|
|
// TODO: we really need to increment the refcounter on the torrent
|
|
|
|
// while this buffer is still in the peer's send buffer
|
2010-06-18 07:44:08 +02:00
|
|
|
if (metadata_piece_size) m_pc.append_const_send_buffer(
|
|
|
|
metadata, metadata_piece_size);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2014-07-13 06:56:53 +02:00
|
|
|
m_pc.stats_counters().inc_stats_counter(counters::num_outgoing_extended);
|
|
|
|
m_pc.stats_counters().inc_stats_counter(counters::num_outgoing_metadata);
|
2007-12-02 05:16:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool on_extended(int length
|
2015-11-20 05:37:45 +01:00
|
|
|
, int extended_msg, buffer::const_interval body) TORRENT_OVERRIDE
|
2007-12-02 05:16:51 +01:00
|
|
|
{
|
2013-02-23 07:40:27 +01:00
|
|
|
if (extended_msg != 2) return false;
|
2007-12-02 05:16:51 +01:00
|
|
|
if (m_message_index == 0) return false;
|
|
|
|
|
|
|
|
if (length > 17 * 1024)
|
2008-04-07 03:39:29 +02:00
|
|
|
{
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-03 04:53:54 +02:00
|
|
|
m_pc.peer_log(peer_log_alert::incoming_message, "UT_METADATA"
|
|
|
|
, "packet too big %d", length);
|
2011-01-27 07:49:32 +01:00
|
|
|
#endif
|
2015-02-15 06:17:09 +01:00
|
|
|
m_pc.disconnect(errors::invalid_metadata_message, op_bittorrent, 2);
|
2008-04-07 03:39:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
2007-12-02 05:16:51 +01:00
|
|
|
|
|
|
|
if (!m_pc.packet_finished()) return true;
|
|
|
|
|
2007-12-22 09:15:05 +01:00
|
|
|
int len;
|
|
|
|
entry msg = bdecode(body.begin, body.end, len);
|
2013-08-21 09:55:29 +02:00
|
|
|
if (msg.type() != entry::dictionary_t)
|
2008-06-24 00:00:27 +02:00
|
|
|
{
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-03 04:53:54 +02:00
|
|
|
m_pc.peer_log(peer_log_alert::incoming_message, "UT_METADATA"
|
|
|
|
, "not a dictionary");
|
2011-01-27 07:49:32 +01:00
|
|
|
#endif
|
2015-02-15 06:17:09 +01:00
|
|
|
m_pc.disconnect(errors::invalid_metadata_message, op_bittorrent, 2);
|
2008-06-24 00:00:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
2007-12-02 05:16:51 +01:00
|
|
|
|
2011-01-27 07:49:32 +01:00
|
|
|
entry const* type_ent = msg.find_key("msg_type");
|
|
|
|
entry const* piece_ent = msg.find_key("piece");
|
|
|
|
if (type_ent == 0 || type_ent->type() != entry::int_t
|
|
|
|
|| piece_ent == 0 || piece_ent->type() != entry::int_t)
|
|
|
|
{
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-03 04:53:54 +02:00
|
|
|
m_pc.peer_log(peer_log_alert::incoming_message, "UT_METADATA"
|
|
|
|
, "missing or invalid keys");
|
2011-01-27 07:49:32 +01:00
|
|
|
#endif
|
2015-02-15 06:17:09 +01:00
|
|
|
m_pc.disconnect(errors::invalid_metadata_message, op_bittorrent, 2);
|
2011-01-27 07:49:32 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
int type = type_ent->integer();
|
2011-01-28 04:49:20 +01:00
|
|
|
int piece = piece_ent->integer();
|
2007-12-02 05:16:51 +01:00
|
|
|
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-03 04:53:54 +02:00
|
|
|
m_pc.peer_log(peer_log_alert::incoming_message, "UT_METADATA"
|
|
|
|
, "type: %d piece: %d", type, piece);
|
2007-12-02 05:16:51 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
2015-04-09 06:24:13 +02:00
|
|
|
case metadata_req:
|
2007-12-02 05:16:51 +01:00
|
|
|
{
|
2015-04-09 06:24:13 +02:00
|
|
|
if (!m_torrent.valid_metadata()
|
|
|
|
|| piece < 0 || piece >= int(m_tp.get_metadata_size() + 16 * 1024 - 1)/(16*1024))
|
2007-12-02 05:16:51 +01:00
|
|
|
{
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-03 04:53:54 +02:00
|
|
|
m_pc.peer_log(peer_log_alert::info, "UT_METADATA"
|
|
|
|
, "have: %d invalid piece %d metadata size: %d"
|
2015-04-09 06:24:13 +02:00
|
|
|
, int(m_torrent.valid_metadata()), piece
|
2015-08-14 05:06:59 +02:00
|
|
|
, m_torrent.valid_metadata()
|
|
|
|
? int(m_tp.get_metadata_size()) : 0);
|
2015-04-09 06:24:13 +02:00
|
|
|
#endif
|
|
|
|
write_metadata_packet(metadata_dont_have, piece);
|
2007-12-02 05:16:51 +01:00
|
|
|
return true;
|
|
|
|
}
|
2012-07-18 05:27:50 +02:00
|
|
|
if (m_pc.send_buffer_size() < send_buffer_limit)
|
2015-04-09 06:24:13 +02:00
|
|
|
write_metadata_packet(metadata_piece, piece);
|
2012-07-18 05:27:50 +02:00
|
|
|
else if (m_incoming_requests.size() < max_incoming_requests)
|
|
|
|
m_incoming_requests.push_back(piece);
|
|
|
|
else
|
2015-04-09 06:24:13 +02:00
|
|
|
write_metadata_packet(metadata_dont_have, piece);
|
2007-12-02 05:16:51 +01:00
|
|
|
}
|
|
|
|
break;
|
2015-04-09 06:24:13 +02:00
|
|
|
case metadata_piece:
|
2007-12-02 05:16:51 +01:00
|
|
|
{
|
|
|
|
std::vector<int>::iterator i = std::find(m_sent_requests.begin()
|
|
|
|
, m_sent_requests.end(), piece);
|
|
|
|
|
|
|
|
// unwanted piece?
|
2011-04-02 10:13:35 +02:00
|
|
|
if (i == m_sent_requests.end())
|
|
|
|
{
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-03 04:53:54 +02:00
|
|
|
m_pc.peer_log(peer_log_alert::info, "UT_METADATA"
|
|
|
|
, "UNWANTED / TIMED OUT");
|
2011-04-02 10:13:35 +02:00
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
2007-12-02 05:16:51 +01:00
|
|
|
|
|
|
|
m_sent_requests.erase(i);
|
|
|
|
entry const* total_size = msg.find_key("total_size");
|
2011-04-02 10:13:35 +02:00
|
|
|
m_tp.received_metadata(*this, body.begin + len, body.left() - len, piece
|
2007-12-02 05:16:51 +01:00
|
|
|
, (total_size && total_size->type() == entry::int_t) ? total_size->integer() : 0);
|
2010-12-17 04:12:45 +01:00
|
|
|
maybe_send_request();
|
2007-12-02 05:16:51 +01:00
|
|
|
}
|
|
|
|
break;
|
2015-04-09 06:24:13 +02:00
|
|
|
case metadata_dont_have:
|
2007-12-02 05:16:51 +01:00
|
|
|
{
|
2015-03-12 05:34:54 +01:00
|
|
|
m_request_limit = (std::max)(aux::time_now() + minutes(1), m_request_limit);
|
2007-12-02 05:16:51 +01:00
|
|
|
std::vector<int>::iterator i = std::find(m_sent_requests.begin()
|
|
|
|
, m_sent_requests.end(), piece);
|
|
|
|
// unwanted piece?
|
|
|
|
if (i == m_sent_requests.end()) return true;
|
|
|
|
m_sent_requests.erase(i);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2009-11-24 20:40:54 +01:00
|
|
|
// unknown message, ignore
|
2009-11-25 03:18:17 +01:00
|
|
|
break;
|
2007-12-02 05:16:51 +01:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2014-07-13 06:56:53 +02:00
|
|
|
m_pc.stats_counters().inc_stats_counter(counters::num_incoming_metadata);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2007-12-02 05:16:51 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-20 05:37:45 +01:00
|
|
|
virtual void tick() TORRENT_OVERRIDE
|
2010-12-17 04:12:45 +01:00
|
|
|
{
|
|
|
|
maybe_send_request();
|
2012-07-18 05:27:50 +02:00
|
|
|
while (!m_incoming_requests.empty()
|
|
|
|
&& m_pc.send_buffer_size() < send_buffer_limit)
|
|
|
|
{
|
|
|
|
int piece = m_incoming_requests.front();
|
|
|
|
m_incoming_requests.erase(m_incoming_requests.begin());
|
2015-04-09 06:24:13 +02:00
|
|
|
write_metadata_packet(metadata_piece, piece);
|
2012-07-18 05:27:50 +02:00
|
|
|
}
|
2010-12-17 04:12:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void maybe_send_request()
|
2007-12-02 05:16:51 +01:00
|
|
|
{
|
2013-01-07 05:20:45 +01:00
|
|
|
if (m_pc.is_disconnecting()) return;
|
|
|
|
|
2007-12-02 05:16:51 +01:00
|
|
|
// if we don't have any metadata, and this peer
|
|
|
|
// supports the request metadata extension
|
|
|
|
// and we aren't currently waiting for a request
|
|
|
|
// reply. Then, send a request for some metadata.
|
|
|
|
if (!m_torrent.valid_metadata()
|
|
|
|
&& m_message_index != 0
|
|
|
|
&& m_sent_requests.size() < 2
|
|
|
|
&& has_metadata())
|
|
|
|
{
|
2012-08-14 18:06:32 +02:00
|
|
|
int piece = m_tp.metadata_request(m_pc.has_metadata());
|
2011-01-29 11:37:21 +01:00
|
|
|
if (piece == -1) return;
|
|
|
|
|
2007-12-02 05:16:51 +01:00
|
|
|
m_sent_requests.push_back(piece);
|
2015-04-09 06:24:13 +02:00
|
|
|
write_metadata_packet(metadata_req, piece);
|
2007-12-02 05:16:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_metadata() const
|
|
|
|
{
|
2015-03-12 05:34:54 +01:00
|
|
|
return m_pc.has_metadata() || (aux::time_now() > m_request_limit);
|
2011-01-27 07:49:32 +01:00
|
|
|
}
|
|
|
|
|
2015-03-12 05:34:54 +01:00
|
|
|
void failed_hash_check(time_point const& now)
|
2011-01-27 07:49:32 +01:00
|
|
|
{
|
2011-02-28 01:35:58 +01:00
|
|
|
m_request_limit = now + seconds(20 + (boost::int64_t(random()) * 50) / UINT_MAX);
|
2007-12-02 05:16:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// this is the message index the remote peer uses
|
|
|
|
// for metadata extension messages.
|
|
|
|
int m_message_index;
|
|
|
|
|
2011-01-27 07:49:32 +01:00
|
|
|
// this is set to the next time we can request pieces
|
|
|
|
// again. It is updated every time we get a
|
|
|
|
// "I don't have metadata" message, but also when
|
|
|
|
// we receive metadata that fails the infohash check
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point m_request_limit;
|
2007-12-02 05:16:51 +01:00
|
|
|
|
|
|
|
// request queues
|
|
|
|
std::vector<int> m_sent_requests;
|
|
|
|
std::vector<int> m_incoming_requests;
|
2015-08-14 05:06:59 +02:00
|
|
|
|
2007-12-02 05:16:51 +01:00
|
|
|
torrent& m_torrent;
|
|
|
|
bt_peer_connection& m_pc;
|
|
|
|
ut_metadata_plugin& m_tp;
|
2015-11-20 05:37:45 +01:00
|
|
|
|
|
|
|
// explicitly disallow assignment, to silence msvc warning
|
|
|
|
ut_metadata_peer_plugin& operator=(ut_metadata_peer_plugin const&);
|
2007-12-02 05:16:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
boost::shared_ptr<peer_plugin> ut_metadata_plugin::new_connection(
|
2015-07-25 18:15:24 +02:00
|
|
|
peer_connection_handle const& pc)
|
2007-12-02 05:16:51 +01:00
|
|
|
{
|
2015-07-02 06:13:26 +02:00
|
|
|
if (pc.type() != peer_connection::bittorrent_connection)
|
2009-11-02 21:43:38 +01:00
|
|
|
return boost::shared_ptr<peer_plugin>();
|
|
|
|
|
2015-07-02 06:13:26 +02:00
|
|
|
bt_peer_connection* c = static_cast<bt_peer_connection*>(pc.native_handle().get());
|
2007-12-02 05:16:51 +01:00
|
|
|
return boost::shared_ptr<peer_plugin>(new ut_metadata_peer_plugin(m_torrent, *c, *this));
|
|
|
|
}
|
|
|
|
|
2012-08-14 18:06:32 +02:00
|
|
|
// has_metadata is false if the peer making the request has not announced
|
|
|
|
// that it has metadata. In this case, it shouldn't prevent other peers
|
|
|
|
// from requesting this block by setting a timeout on it.
|
|
|
|
int ut_metadata_plugin::metadata_request(bool has_metadata)
|
2007-12-02 05:16:51 +01:00
|
|
|
{
|
2011-01-27 07:49:32 +01:00
|
|
|
std::vector<metadata_piece>::iterator i = std::min_element(
|
2007-12-02 05:16:51 +01:00
|
|
|
m_requested_metadata.begin(), m_requested_metadata.end());
|
|
|
|
|
|
|
|
if (m_requested_metadata.empty())
|
|
|
|
{
|
|
|
|
// if we don't know how many pieces there are
|
|
|
|
// just ask for piece 0
|
2011-01-27 07:49:32 +01:00
|
|
|
m_requested_metadata.resize(1);
|
|
|
|
i = m_requested_metadata.begin();
|
2007-12-02 05:16:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int piece = i - m_requested_metadata.begin();
|
2011-01-29 11:37:21 +01:00
|
|
|
|
|
|
|
// don't request the same block more than once every 3 seconds
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point now = aux::time_now();
|
2014-08-16 22:55:44 +02:00
|
|
|
if (m_requested_metadata[piece].last_request != min_time()
|
|
|
|
&& total_seconds(now - m_requested_metadata[piece].last_request) < 3)
|
|
|
|
return -1;
|
2011-01-29 11:37:21 +01:00
|
|
|
|
2011-01-27 07:49:32 +01:00
|
|
|
++m_requested_metadata[piece].num_requests;
|
2012-08-14 18:06:32 +02:00
|
|
|
|
|
|
|
// only set the timeout on this block, only if the peer
|
|
|
|
// has metadata. This is to prevent peers with no metadata
|
|
|
|
// to starve out sending requests to peers with metadata
|
|
|
|
if (has_metadata)
|
|
|
|
m_requested_metadata[piece].last_request = now;
|
|
|
|
|
2007-12-02 05:16:51 +01:00
|
|
|
return piece;
|
|
|
|
}
|
|
|
|
|
2015-11-20 05:37:45 +01:00
|
|
|
bool ut_metadata_plugin::received_metadata(
|
2011-04-02 10:13:35 +02:00
|
|
|
ut_metadata_peer_plugin& source
|
2011-01-27 07:49:32 +01:00
|
|
|
, char const* buf, int size, int piece, int total_size)
|
|
|
|
{
|
2011-01-29 11:37:21 +01:00
|
|
|
if (m_torrent.valid_metadata())
|
|
|
|
{
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-03 04:53:54 +02:00
|
|
|
source.m_pc.peer_log(peer_log_alert::info, "UT_METADATA"
|
|
|
|
, "already have metadata");
|
2011-04-02 10:13:35 +02:00
|
|
|
#endif
|
2011-11-16 03:29:59 +01:00
|
|
|
m_torrent.add_redundant_bytes(size, torrent::piece_unknown);
|
2011-01-29 11:37:21 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-08-14 05:06:59 +02:00
|
|
|
|
2011-01-27 07:49:32 +01:00
|
|
|
if (!m_metadata)
|
|
|
|
{
|
|
|
|
// verify the total_size
|
2014-07-06 21:18:00 +02:00
|
|
|
if (total_size <= 0 || total_size > m_torrent.session().settings().get_int(settings_pack::max_metadata_size))
|
2011-04-02 10:13:35 +02:00
|
|
|
{
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-03 04:53:54 +02:00
|
|
|
source.m_pc.peer_log(peer_log_alert::info, "UT_METADATA"
|
2015-08-14 05:06:59 +02:00
|
|
|
, "metadata size too big: %d", total_size);
|
2011-04-02 10:13:35 +02:00
|
|
|
#endif
|
|
|
|
// #error post alert
|
2011-01-29 11:37:21 +01:00
|
|
|
return false;
|
2011-04-02 10:13:35 +02:00
|
|
|
}
|
2011-01-27 07:49:32 +01:00
|
|
|
|
|
|
|
m_metadata.reset(new char[total_size]);
|
|
|
|
m_requested_metadata.resize(div_round_up(total_size, 16 * 1024));
|
|
|
|
m_metadata_size = total_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (piece < 0 || piece >= int(m_requested_metadata.size()))
|
2011-04-02 10:13:35 +02:00
|
|
|
{
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-03 04:53:54 +02:00
|
|
|
source.m_pc.peer_log(peer_log_alert::info, "UT_METADATA"
|
2015-08-14 05:06:59 +02:00
|
|
|
, "piece: %d INVALID", piece);
|
2011-04-02 10:13:35 +02:00
|
|
|
#endif
|
2011-01-27 07:49:32 +01:00
|
|
|
return false;
|
2011-04-02 10:13:35 +02:00
|
|
|
}
|
2011-01-27 07:49:32 +01:00
|
|
|
|
|
|
|
if (total_size != m_metadata_size)
|
|
|
|
{
|
2015-04-17 03:15:33 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2015-05-03 04:53:54 +02:00
|
|
|
source.m_pc.peer_log(peer_log_alert::info, "UT_METADATA"
|
|
|
|
, "total_size: %d INCONSISTENT WITH: %d"
|
2015-08-14 05:06:59 +02:00
|
|
|
, total_size, m_metadata_size);
|
2011-04-02 10:13:35 +02:00
|
|
|
#endif
|
2011-01-27 07:49:32 +01:00
|
|
|
// they disagree about the size!
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (piece * 16 * 1024 + size > m_metadata_size)
|
|
|
|
{
|
2015-08-14 05:06:59 +02:00
|
|
|
// this piece is invalid
|
2011-01-27 07:49:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::memcpy(&m_metadata[piece * 16 * 1024], buf, size);
|
|
|
|
// mark this piece has 'have'
|
|
|
|
m_requested_metadata[piece].num_requests = (std::numeric_limits<int>::max)();
|
2011-04-02 10:13:35 +02:00
|
|
|
m_requested_metadata[piece].source = source.shared_from_this();
|
2011-01-27 07:49:32 +01:00
|
|
|
|
|
|
|
bool have_all = std::count_if(m_requested_metadata.begin()
|
|
|
|
, m_requested_metadata.end(), boost::bind(&metadata_piece::num_requests, _1)
|
|
|
|
== (std::numeric_limits<int>::max)()) == int(m_requested_metadata.size());
|
|
|
|
|
|
|
|
if (!have_all) return false;
|
|
|
|
|
|
|
|
if (!m_torrent.set_metadata(&m_metadata[0], m_metadata_size))
|
|
|
|
{
|
|
|
|
if (!m_torrent.valid_metadata())
|
|
|
|
{
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point now = aux::time_now();
|
2011-01-27 07:49:32 +01:00
|
|
|
// any peer that we downloaded metadata from gets a random time
|
|
|
|
// penalty, from 5 to 30 seconds or so. During this time we don't
|
|
|
|
// make any metadata requests from those peers (to mix it up a bit
|
|
|
|
// of which peers we use)
|
|
|
|
// if we only have one block, and thus requested it from a single
|
|
|
|
// peer, we bump up the retry time a lot more to try other peers
|
|
|
|
bool single_peer = m_requested_metadata.size() == 1;
|
2011-02-21 06:24:41 +01:00
|
|
|
for (int i = 0; i < int(m_requested_metadata.size()); ++i)
|
2011-01-27 07:49:32 +01:00
|
|
|
{
|
|
|
|
m_requested_metadata[i].num_requests = 0;
|
|
|
|
boost::shared_ptr<ut_metadata_peer_plugin> peer
|
|
|
|
= m_requested_metadata[i].source.lock();
|
|
|
|
if (!peer) continue;
|
|
|
|
|
|
|
|
peer->failed_hash_check(single_peer ? now + minutes(5) : now);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// free our copy of the metadata and get a reference
|
|
|
|
// to the torrent's copy instead. No need to keep two
|
|
|
|
// identical copies around
|
|
|
|
m_metadata.reset();
|
|
|
|
metadata();
|
|
|
|
|
2011-01-27 07:49:32 +01:00
|
|
|
// clear the storage for the bitfield
|
|
|
|
std::vector<metadata_piece>().swap(m_requested_metadata);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-12-02 05:16:51 +01:00
|
|
|
} }
|
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
2015-07-25 18:39:25 +02:00
|
|
|
boost::shared_ptr<torrent_plugin> create_ut_metadata_plugin(torrent_handle const& th, void*)
|
2007-12-02 05:16:51 +01:00
|
|
|
{
|
2015-07-22 04:11:41 +02:00
|
|
|
torrent* t = th.native_handle().get();
|
2008-11-25 20:25:49 +01:00
|
|
|
// don't add this extension if the torrent is private
|
|
|
|
if (t->valid_metadata() && t->torrent_file().priv()) return boost::shared_ptr<torrent_plugin>();
|
2007-12-02 05:16:51 +01:00
|
|
|
return boost::shared_ptr<torrent_plugin>(new ut_metadata_plugin(*t));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-01-29 13:13:49 +01:00
|
|
|
#endif
|
2007-12-02 05:16:51 +01:00
|
|
|
|