2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2003, Arvid Norberg
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-03-17 18:15:16 +01:00
|
|
|
#include "libtorrent/pch.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2007-03-17 18:15:16 +01:00
|
|
|
#include <iostream>
|
2006-01-11 02:32:26 +01:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
2005-09-28 20:32:05 +02:00
|
|
|
#include <boost/bind.hpp>
|
2007-04-10 23:23:13 +02:00
|
|
|
#include <boost/utility.hpp>
|
2003-12-01 06:01:40 +01:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2007-03-17 18:15:16 +01:00
|
|
|
#include "libtorrent/peer_connection.hpp"
|
2006-04-25 23:04:48 +02:00
|
|
|
#include "libtorrent/web_peer_connection.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
#include "libtorrent/policy.hpp"
|
|
|
|
#include "libtorrent/torrent.hpp"
|
|
|
|
#include "libtorrent/socket.hpp"
|
2004-01-18 20:12:18 +01:00
|
|
|
#include "libtorrent/alert_types.hpp"
|
2005-05-12 01:03:12 +02:00
|
|
|
#include "libtorrent/invariant_check.hpp"
|
2007-04-05 00:27:36 +02:00
|
|
|
#include "libtorrent/time.hpp"
|
2006-10-11 16:02:21 +02:00
|
|
|
#include "libtorrent/aux_/session_impl.hpp"
|
2007-07-04 04:16:49 +02:00
|
|
|
#include "libtorrent/piece_picker.hpp"
|
2004-01-12 04:05:10 +01:00
|
|
|
|
2007-07-04 17:46:10 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#include "libtorrent/bt_peer_connection.hpp"
|
|
|
|
#endif
|
|
|
|
|
2004-01-15 17:45:34 +01:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
class peer_connection;
|
|
|
|
}
|
|
|
|
|
2005-09-28 20:32:05 +02:00
|
|
|
using boost::bind;
|
2004-10-14 03:17:04 +02:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
2003-11-05 00:27:06 +01:00
|
|
|
|
2007-02-12 10:20:49 +01:00
|
|
|
size_type collect_free_download(
|
|
|
|
torrent::peer_iterator start
|
|
|
|
, torrent::peer_iterator end)
|
|
|
|
{
|
|
|
|
size_type accumulator = 0;
|
|
|
|
for (torrent::peer_iterator i = start; i != end; ++i)
|
|
|
|
{
|
|
|
|
// if the peer is interested in us, it means it may
|
|
|
|
// want to trade it's surplus uploads for downloads itself
|
|
|
|
// (and we should not consider it free). If the share diff is
|
|
|
|
// negative, there's no free download to get from this peer.
|
2007-10-31 10:48:20 +01:00
|
|
|
size_type diff = (*i)->share_diff();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(diff < (std::numeric_limits<size_type>::max)());
|
2007-10-31 10:48:20 +01:00
|
|
|
if ((*i)->is_peer_interested() || diff <= 0)
|
2007-02-12 10:20:49 +01:00
|
|
|
continue;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(diff > 0);
|
2007-10-31 10:48:20 +01:00
|
|
|
(*i)->add_free_upload(-diff);
|
2007-02-12 10:20:49 +01:00
|
|
|
accumulator += diff;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(accumulator > 0);
|
2007-02-12 10:20:49 +01:00
|
|
|
}
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(accumulator >= 0);
|
2007-02-12 10:20:49 +01:00
|
|
|
return accumulator;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// returns the amount of free upload left after
|
|
|
|
// it has been distributed to the peers
|
|
|
|
size_type distribute_free_upload(
|
|
|
|
torrent::peer_iterator start
|
|
|
|
, torrent::peer_iterator end
|
|
|
|
, size_type free_upload)
|
|
|
|
{
|
|
|
|
if (free_upload <= 0) return free_upload;
|
|
|
|
int num_peers = 0;
|
|
|
|
size_type total_diff = 0;
|
|
|
|
for (torrent::peer_iterator i = start; i != end; ++i)
|
|
|
|
{
|
2007-10-31 10:48:20 +01:00
|
|
|
size_type d = (*i)->share_diff();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(d < (std::numeric_limits<size_type>::max)());
|
2007-02-12 10:20:49 +01:00
|
|
|
total_diff += d;
|
2007-10-31 10:48:20 +01:00
|
|
|
if (!(*i)->is_peer_interested() || (*i)->share_diff() >= 0) continue;
|
2007-02-12 10:20:49 +01:00
|
|
|
++num_peers;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_peers == 0) return free_upload;
|
|
|
|
size_type upload_share;
|
|
|
|
if (total_diff >= 0)
|
|
|
|
{
|
2007-08-21 09:33:06 +02:00
|
|
|
upload_share = (std::min)(free_upload, total_diff) / num_peers;
|
2007-02-12 10:20:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
upload_share = (free_upload + total_diff) / num_peers;
|
|
|
|
}
|
|
|
|
if (upload_share < 0) return free_upload;
|
|
|
|
|
|
|
|
for (torrent::peer_iterator i = start; i != end; ++i)
|
|
|
|
{
|
2007-10-31 10:48:20 +01:00
|
|
|
peer_connection* p = *i;
|
2007-02-12 10:20:49 +01:00
|
|
|
if (!p->is_peer_interested() || p->share_diff() >= 0) continue;
|
|
|
|
p->add_free_upload(upload_share);
|
|
|
|
free_upload -= upload_share;
|
|
|
|
}
|
|
|
|
return free_upload;
|
|
|
|
}
|
|
|
|
|
2007-09-19 08:05:14 +02:00
|
|
|
struct match_peer_endpoint
|
2007-06-09 01:02:31 +02:00
|
|
|
{
|
2007-09-19 08:05:14 +02:00
|
|
|
match_peer_endpoint(tcp::endpoint const& ep)
|
|
|
|
: m_ep(ep)
|
2007-06-09 01:02:31 +02:00
|
|
|
{}
|
|
|
|
|
2007-10-02 21:11:04 +02:00
|
|
|
bool operator()(std::pair<const address, policy::peer> const& p) const
|
|
|
|
{ return p.second.ip == m_ep; }
|
2007-06-09 01:02:31 +02:00
|
|
|
|
2007-09-19 08:05:14 +02:00
|
|
|
tcp::endpoint const& m_ep;
|
2007-02-12 10:20:49 +01:00
|
|
|
};
|
|
|
|
|
2007-09-22 18:24:26 +02:00
|
|
|
struct match_peer_id
|
|
|
|
{
|
|
|
|
match_peer_id(peer_id const& id_)
|
|
|
|
: m_id(id_)
|
|
|
|
{}
|
|
|
|
|
2007-10-02 21:11:04 +02:00
|
|
|
bool operator()(std::pair<const address, policy::peer> const& p) const
|
|
|
|
{ return p.second.connection && p.second.connection->pid() == m_id; }
|
2007-09-22 18:24:26 +02:00
|
|
|
|
|
|
|
peer_id const& m_id;
|
|
|
|
};
|
|
|
|
|
2007-02-12 10:20:49 +01:00
|
|
|
struct match_peer_connection
|
|
|
|
{
|
|
|
|
match_peer_connection(peer_connection const& c)
|
|
|
|
: m_conn(c)
|
|
|
|
{}
|
|
|
|
|
2007-10-02 21:11:04 +02:00
|
|
|
bool operator()(std::pair<const address, policy::peer> const& p) const
|
2007-06-09 01:02:31 +02:00
|
|
|
{
|
2007-10-02 21:11:04 +02:00
|
|
|
return p.second.connection == &m_conn
|
|
|
|
|| (p.second.ip == m_conn.remote()
|
|
|
|
&& p.second.type == policy::peer::connectable);
|
2007-06-09 01:02:31 +02:00
|
|
|
}
|
2007-02-12 10:20:49 +01:00
|
|
|
|
2007-06-09 01:02:31 +02:00
|
|
|
peer_connection const& m_conn;
|
2007-02-12 10:20:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
2004-02-01 11:45:54 +01:00
|
|
|
// the case where ignore_peer is motivated is if two peers
|
2004-02-01 10:47:58 +01:00
|
|
|
// have only one piece that we don't have, and it's the
|
|
|
|
// same piece for both peers. Then they might get into an
|
2005-08-15 00:04:58 +02:00
|
|
|
// infinite loop, fighting to request the same blocks.
|
2007-08-14 19:47:48 +02:00
|
|
|
void request_a_block(torrent& t, peer_connection& c)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-08-16 21:36:33 +02:00
|
|
|
if (t.is_seed()) return;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t.valid_metadata());
|
|
|
|
TORRENT_ASSERT(c.peer_info_struct() != 0 || !dynamic_cast<bt_peer_connection*>(&c));
|
2006-04-25 23:04:48 +02:00
|
|
|
int num_requests = c.desired_queue_size()
|
|
|
|
- (int)c.download_queue().size()
|
2005-09-27 10:07:24 +02:00
|
|
|
- (int)c.request_queue().size();
|
2003-11-05 00:27:06 +01:00
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*c.m_logger) << time_now_string() << " PIECE_PICKER [ req: " << num_requests << " ]\n";
|
|
|
|
#endif
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(c.desired_queue_size() > 0);
|
2003-11-05 00:27:06 +01:00
|
|
|
// if our request queue is already full, we
|
|
|
|
// don't have to make any new requests yet
|
|
|
|
if (num_requests <= 0) return;
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
piece_picker& p = t.picker();
|
|
|
|
std::vector<piece_block> interesting_pieces;
|
|
|
|
interesting_pieces.reserve(100);
|
2003-11-02 22:06:50 +01:00
|
|
|
|
2007-09-03 23:16:24 +02:00
|
|
|
int prefer_whole_pieces = c.prefer_whole_pieces();
|
2007-05-16 06:12:13 +02:00
|
|
|
|
2007-07-07 03:26:30 +02:00
|
|
|
bool rarest_first = t.num_pieces() >= t.settings().initial_picker_threshold;
|
|
|
|
|
2007-09-03 23:16:24 +02:00
|
|
|
if (prefer_whole_pieces == 0)
|
2006-11-14 01:08:16 +01:00
|
|
|
{
|
|
|
|
prefer_whole_pieces = c.statistics().download_payload_rate()
|
|
|
|
* t.settings().whole_pieces_threshold
|
2007-09-03 23:16:24 +02:00
|
|
|
> t.torrent_file().piece_length() ? 1 : 0;
|
2006-11-14 01:08:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we prefer whole pieces, the piece picker will pick at least
|
|
|
|
// the number of blocks we want, but it will try to make the picked
|
|
|
|
// blocks be from whole pieces, possibly by returning more blocks
|
|
|
|
// than we requested.
|
2008-02-08 11:22:05 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
asio::error_code ec;
|
|
|
|
TORRENT_ASSERT(c.remote() == c.get_socket()->remote_endpoint(ec) || ec);
|
|
|
|
#endif
|
2006-11-14 01:08:16 +01:00
|
|
|
|
2007-04-27 02:27:37 +02:00
|
|
|
piece_picker::piece_state_t state;
|
|
|
|
peer_connection::peer_speed_t speed = c.peer_speed();
|
|
|
|
if (speed == peer_connection::fast) state = piece_picker::fast;
|
|
|
|
else if (speed == peer_connection::medium) state = piece_picker::medium;
|
2007-04-27 18:26:30 +02:00
|
|
|
else state = piece_picker::slow;
|
2007-04-27 02:27:37 +02:00
|
|
|
|
2004-01-12 21:31:27 +01:00
|
|
|
// this vector is filled with the interesting pieces
|
2003-10-23 01:00:57 +02:00
|
|
|
// that some other peer is currently downloading
|
|
|
|
// we should then compare this peer's download speed
|
|
|
|
// with the other's, to see if we should abort another
|
|
|
|
// peer_connection in favour of this one
|
|
|
|
std::vector<piece_block> busy_pieces;
|
2007-08-03 08:13:26 +02:00
|
|
|
busy_pieces.reserve(num_requests);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2007-09-05 23:21:11 +02:00
|
|
|
std::vector<int> const& suggested = c.suggested_pieces();
|
2007-09-06 01:32:05 +02:00
|
|
|
std::vector<bool> const& bitfield = c.get_bitfield();
|
2007-09-05 23:21:11 +02:00
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
if (c.has_peer_choked())
|
|
|
|
{
|
|
|
|
// if we are choked we can only pick pieces from the
|
|
|
|
// allowed fast set. The allowed fast set is sorted
|
|
|
|
// in ascending priority order
|
|
|
|
std::vector<int> const& allowed_fast = c.allowed_fast();
|
|
|
|
|
2007-09-05 23:21:11 +02:00
|
|
|
// build a bitmask with only the allowed pieces in it
|
|
|
|
std::vector<bool> mask(c.get_bitfield().size(), false);
|
|
|
|
for (std::vector<int>::const_iterator i = allowed_fast.begin()
|
|
|
|
, end(allowed_fast.end()); i != end; ++i)
|
2007-09-06 01:32:05 +02:00
|
|
|
if (bitfield[*i]) mask[*i] = true;
|
2007-09-05 23:21:11 +02:00
|
|
|
|
|
|
|
p.pick_pieces(mask, interesting_pieces
|
|
|
|
, num_requests, prefer_whole_pieces, c.peer_info_struct()
|
|
|
|
, state, rarest_first, c.on_parole(), suggested);
|
2007-08-14 19:47:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// picks the interesting pieces from this peer
|
|
|
|
// the integer is the number of pieces that
|
|
|
|
// should be guaranteed to be available for download
|
|
|
|
// (if num_requests is too big, too many pieces are
|
|
|
|
// picked and cpu-time is wasted)
|
|
|
|
// the last argument is if we should prefer whole pieces
|
|
|
|
// for this peer. If we're downloading one piece in 20 seconds
|
|
|
|
// then use this mode.
|
2007-09-06 01:32:05 +02:00
|
|
|
p.pick_pieces(bitfield, interesting_pieces
|
2007-09-05 23:21:11 +02:00
|
|
|
, num_requests, prefer_whole_pieces, c.peer_info_struct()
|
|
|
|
, state, rarest_first, c.on_parole(), suggested);
|
2007-08-14 19:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2007-09-03 23:16:24 +02:00
|
|
|
(*c.m_logger) << time_now_string() << " PIECE_PICKER [ php: " << prefer_whole_pieces
|
|
|
|
<< " picked: " << interesting_pieces.size() << " ]\n";
|
2007-08-14 19:47:48 +02:00
|
|
|
#endif
|
2007-09-03 23:16:24 +02:00
|
|
|
std::deque<piece_block> const& dq = c.download_queue();
|
|
|
|
std::deque<piece_block> const& rq = c.request_queue();
|
2003-10-23 01:00:57 +02:00
|
|
|
for (std::vector<piece_block>::iterator i = interesting_pieces.begin();
|
2005-05-25 12:01:01 +02:00
|
|
|
i != interesting_pieces.end(); ++i)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-09-10 10:07:18 +02:00
|
|
|
if (prefer_whole_pieces == 0 && num_requests <= 0) break;
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
if (p.is_requested(*i))
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-08-21 03:17:42 +02:00
|
|
|
if (num_requests <= 0) break;
|
2007-07-06 19:15:35 +02:00
|
|
|
// don't request pieces we already have in our request queue
|
|
|
|
if (std::find(dq.begin(), dq.end(), *i) != dq.end()
|
|
|
|
|| std::find(rq.begin(), rq.end(), *i) != rq.end())
|
|
|
|
continue;
|
|
|
|
|
2008-02-11 07:52:40 +01:00
|
|
|
TORRENT_ASSERT(p.num_peers(*i) > 0);
|
2003-10-23 01:00:57 +02:00
|
|
|
busy_pieces.push_back(*i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(p.num_peers(*i) == 0);
|
2003-10-23 01:00:57 +02:00
|
|
|
// ok, we found a piece that's not being downloaded
|
|
|
|
// by somebody else. request it from this peer
|
2004-02-01 10:47:58 +01:00
|
|
|
// and return
|
2006-04-25 23:04:48 +02:00
|
|
|
c.add_request(*i);
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(p.num_peers(*i) == 1);
|
|
|
|
TORRENT_ASSERT(p.is_requested(*i));
|
2003-11-05 00:27:06 +01:00
|
|
|
num_requests--;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2007-08-21 03:17:42 +02:00
|
|
|
if (busy_pieces.empty() || num_requests <= 0)
|
2003-11-05 00:27:06 +01:00
|
|
|
{
|
2007-08-14 19:47:48 +02:00
|
|
|
// in this case, we could not find any blocks
|
|
|
|
// that was free. If we couldn't find any busy
|
|
|
|
// blocks as well, we cannot download anything
|
|
|
|
// more from this peer.
|
|
|
|
|
2007-07-06 19:15:35 +02:00
|
|
|
c.send_block_requests();
|
|
|
|
return;
|
|
|
|
}
|
2004-02-04 12:00:29 +01:00
|
|
|
|
2007-08-03 08:13:26 +02:00
|
|
|
// if all blocks has the same number of peers on them
|
|
|
|
// we want to pick a random block
|
2007-07-06 19:15:35 +02:00
|
|
|
std::random_shuffle(busy_pieces.begin(), busy_pieces.end());
|
|
|
|
|
|
|
|
// find the block with the fewest requests to it
|
|
|
|
std::vector<piece_block>::iterator i = std::min_element(
|
|
|
|
busy_pieces.begin(), busy_pieces.end()
|
|
|
|
, bind(&piece_picker::num_peers, boost::cref(p), _1) <
|
|
|
|
bind(&piece_picker::num_peers, boost::cref(p), _2));
|
2007-08-03 08:13:26 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
piece_picker::downloading_piece st;
|
|
|
|
p.piece_info(i->piece_index, st);
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(st.requested + st.finished + st.writing == p.blocks_in_piece(i->piece_index));
|
2007-08-03 08:13:26 +02:00
|
|
|
#endif
|
2008-02-11 07:52:40 +01:00
|
|
|
TORRENT_ASSERT(p.is_requested(*i));
|
|
|
|
TORRENT_ASSERT(p.num_peers(*i) > 0);
|
2007-07-06 19:15:35 +02:00
|
|
|
c.add_request(*i);
|
2006-05-15 00:30:05 +02:00
|
|
|
c.send_block_requests();
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2003-12-01 06:01:40 +01:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
policy::policy(torrent* t)
|
2006-07-08 21:41:39 +02:00
|
|
|
: m_torrent(t)
|
2003-12-14 23:55:32 +01:00
|
|
|
, m_available_free_upload(0)
|
2008-03-29 19:47:24 +01:00
|
|
|
, m_num_connect_candidates(0)
|
2007-10-05 02:30:00 +02:00
|
|
|
{ TORRENT_ASSERT(t); }
|
2007-07-26 09:04:35 +02:00
|
|
|
|
|
|
|
// disconnects and removes all peers that are now filtered
|
|
|
|
void policy::ip_filter_updated()
|
|
|
|
{
|
|
|
|
aux::session_impl& ses = m_torrent->session();
|
|
|
|
piece_picker* p = 0;
|
|
|
|
if (m_torrent->has_picker())
|
|
|
|
p = &m_torrent->picker();
|
2007-10-02 21:11:04 +02:00
|
|
|
for (iterator i = m_peers.begin()
|
2007-07-26 09:04:35 +02:00
|
|
|
, end(m_peers.end()); i != end;)
|
|
|
|
{
|
2007-10-02 21:11:04 +02:00
|
|
|
if ((ses.m_ip_filter.access(i->second.ip.address()) & ip_filter::blocked) == 0)
|
2007-07-26 09:04:35 +02:00
|
|
|
{
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-10-02 21:11:04 +02:00
|
|
|
if (i->second.connection)
|
2007-07-26 09:04:35 +02:00
|
|
|
{
|
2008-01-07 02:10:46 +01:00
|
|
|
i->second.connection->disconnect("peer banned by IP filter");
|
2007-07-26 09:04:35 +02:00
|
|
|
if (ses.m_alerts.should_post(alert::info))
|
|
|
|
{
|
2007-10-02 21:11:04 +02:00
|
|
|
ses.m_alerts.post_alert(peer_blocked_alert(i->second.ip.address()
|
2007-07-26 09:04:35 +02:00
|
|
|
, "disconnected blocked peer"));
|
|
|
|
}
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(i->second.connection == 0
|
2007-10-02 21:11:04 +02:00
|
|
|
|| i->second.connection->peer_info_struct() == 0);
|
2007-07-26 09:04:35 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ses.m_alerts.should_post(alert::info))
|
|
|
|
{
|
2007-10-02 21:11:04 +02:00
|
|
|
ses.m_alerts.post_alert(peer_blocked_alert(i->second.ip.address()
|
2007-07-26 09:04:35 +02:00
|
|
|
, "blocked peer removed from peer list"));
|
|
|
|
}
|
|
|
|
}
|
2007-10-02 21:11:04 +02:00
|
|
|
if (p) p->clear_peer(&i->second);
|
2008-04-01 19:38:19 +02:00
|
|
|
if (i->second.seed) --m_num_seeds;
|
2007-07-26 09:04:35 +02:00
|
|
|
m_peers.erase(i++);
|
|
|
|
}
|
|
|
|
}
|
2008-03-29 19:47:24 +01:00
|
|
|
|
|
|
|
bool policy::is_connect_candidate(peer const& p, bool finished)
|
2003-12-14 06:56:12 +01:00
|
|
|
{
|
2008-03-29 19:47:24 +01:00
|
|
|
if (p.connection
|
|
|
|
|| p.banned
|
|
|
|
|| p.type == peer::not_connectable
|
|
|
|
|| (p.seed && finished)
|
|
|
|
|| p.failcount >= m_torrent->settings().max_failcount)
|
|
|
|
return false;
|
2005-04-05 02:54:33 +02:00
|
|
|
|
2008-03-29 19:47:24 +01:00
|
|
|
aux::session_impl& ses = m_torrent->session();
|
|
|
|
if (ses.m_port_filter.access(p.ip.port()) & port_filter::blocked)
|
|
|
|
return false;
|
|
|
|
return true;
|
2003-12-14 06:56:12 +01:00
|
|
|
}
|
|
|
|
|
2007-04-12 12:21:55 +02:00
|
|
|
policy::iterator policy::find_disconnect_candidate()
|
2004-01-21 01:59:38 +01:00
|
|
|
{
|
2007-04-13 19:47:40 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-04-12 12:21:55 +02:00
|
|
|
iterator disconnect_peer = m_peers.end();
|
2007-08-21 09:33:06 +02:00
|
|
|
double slowest_transfer_rate = (std::numeric_limits<double>::max)();
|
2004-01-21 01:59:38 +01:00
|
|
|
|
2007-04-12 12:21:55 +02:00
|
|
|
ptime now = time_now();
|
2004-01-21 01:59:38 +01:00
|
|
|
|
2007-04-10 23:23:13 +02:00
|
|
|
for (iterator i = m_peers.begin();
|
2005-08-18 22:38:03 +02:00
|
|
|
i != m_peers.end(); ++i)
|
2004-01-21 01:59:38 +01:00
|
|
|
{
|
2007-10-02 21:11:04 +02:00
|
|
|
peer_connection* c = i->second.connection;
|
2007-04-12 12:21:55 +02:00
|
|
|
if (c == 0) continue;
|
|
|
|
if (c->is_disconnecting()) continue;
|
|
|
|
|
|
|
|
// never disconnect an interesting peer if we have a candidate that
|
|
|
|
// isn't interesting
|
|
|
|
if (disconnect_peer != m_peers.end()
|
|
|
|
&& c->is_interesting()
|
2007-10-02 21:11:04 +02:00
|
|
|
&& !disconnect_peer->second.connection->is_interesting())
|
2004-01-21 01:59:38 +01:00
|
|
|
continue;
|
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
double transferred_amount
|
|
|
|
= (double)c->statistics().total_payload_download();
|
2004-01-21 01:59:38 +01:00
|
|
|
|
2007-10-02 21:11:04 +02:00
|
|
|
time_duration connected_time = now - i->second.connected;
|
2004-01-21 01:59:38 +01:00
|
|
|
|
2007-04-12 12:21:55 +02:00
|
|
|
double connected_time_in_seconds = total_seconds(connected_time);
|
2004-01-21 01:59:38 +01:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
double transfer_rate
|
2007-04-12 12:21:55 +02:00
|
|
|
= transferred_amount / (connected_time_in_seconds + 1);
|
2004-01-21 01:59:38 +01:00
|
|
|
|
2007-04-13 19:47:40 +02:00
|
|
|
// prefer to disconnect uninteresting peers, and secondly slow peers
|
2008-03-04 04:52:08 +01:00
|
|
|
if (transfer_rate <= slowest_transfer_rate)
|
2004-01-21 01:59:38 +01:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
slowest_transfer_rate = transfer_rate;
|
2007-04-12 12:21:55 +02:00
|
|
|
disconnect_peer = i;
|
2004-01-21 01:59:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return disconnect_peer;
|
|
|
|
}
|
|
|
|
|
2007-04-12 12:21:55 +02:00
|
|
|
policy::iterator policy::find_connect_candidate()
|
2004-01-14 17:18:53 +01:00
|
|
|
{
|
2007-08-27 07:32:47 +02:00
|
|
|
// too expensive
|
|
|
|
// INVARIANT_CHECK;
|
2007-04-13 19:47:40 +02:00
|
|
|
|
2007-04-05 00:27:36 +02:00
|
|
|
ptime now = time_now();
|
2007-05-07 18:24:08 +02:00
|
|
|
ptime min_connect_time(now);
|
2007-04-12 12:21:55 +02:00
|
|
|
iterator candidate = m_peers.end();
|
2007-04-14 23:47:07 +02:00
|
|
|
|
|
|
|
int min_reconnect_time = m_torrent->settings().min_reconnect_time;
|
2007-09-04 00:57:09 +02:00
|
|
|
bool finished = m_torrent->is_finished();
|
2008-04-05 06:53:22 +02:00
|
|
|
|
|
|
|
int min_cidr_distance = (std::numeric_limits<int>::max)();
|
2008-03-29 23:45:55 +01:00
|
|
|
address external_ip = m_torrent->session().external_address();
|
2008-02-28 04:09:34 +01:00
|
|
|
|
2008-04-05 06:53:22 +02:00
|
|
|
// don't bias any particular peers when seeding
|
|
|
|
if (finished || external_ip == address())
|
2008-02-28 04:09:34 +01:00
|
|
|
{
|
|
|
|
// set external_ip to a random value, to
|
|
|
|
// radomize which peers we prefer
|
|
|
|
address_v4::bytes_type bytes;
|
|
|
|
std::generate(bytes.begin(), bytes.end(), &std::rand);
|
|
|
|
external_ip = address_v4(bytes);
|
|
|
|
}
|
2004-01-14 17:18:53 +01:00
|
|
|
|
2008-04-05 06:53:22 +02:00
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
|
|
|
int max_inet_as_rate = -1;
|
|
|
|
bool has_db = m_torrent->session().has_asnum_db();
|
|
|
|
#endif
|
|
|
|
|
2008-03-29 19:47:24 +01:00
|
|
|
int connect_candidates = 0;
|
2008-04-01 19:38:19 +02:00
|
|
|
int seeds = 0;
|
2007-05-07 18:24:08 +02:00
|
|
|
for (iterator i = m_peers.begin(); i != m_peers.end(); ++i)
|
2004-01-14 17:18:53 +01:00
|
|
|
{
|
2008-04-01 19:38:19 +02:00
|
|
|
if (i->second.seed) ++seeds;
|
2008-03-29 19:47:24 +01:00
|
|
|
if (!is_connect_candidate(i->second, finished)) continue;
|
|
|
|
++connect_candidates;
|
2007-12-24 09:17:32 +01:00
|
|
|
|
|
|
|
// prefer peers with lower failcount
|
|
|
|
if (candidate != m_peers.end()
|
|
|
|
&& candidate->second.failcount < i->second.failcount)
|
|
|
|
continue;
|
|
|
|
|
2007-10-02 21:11:04 +02:00
|
|
|
if (now - i->second.connected < seconds(i->second.failcount * min_reconnect_time))
|
2007-04-14 23:47:07 +02:00
|
|
|
continue;
|
2004-01-14 17:22:49 +01:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(i->second.connected <= now);
|
2004-01-14 17:18:53 +01:00
|
|
|
|
2007-12-22 19:20:23 +01:00
|
|
|
// don't replace a candidate that is on the local
|
|
|
|
// network with one that isn't. Local peers should
|
|
|
|
// always be tried first
|
|
|
|
if (candidate != m_peers.end()
|
|
|
|
&& is_local(candidate->second.ip.address())
|
|
|
|
&& !is_local(i->second.ip.address()))
|
|
|
|
continue;
|
|
|
|
|
2008-02-28 04:09:34 +01:00
|
|
|
if (i->second.connected > min_connect_time) continue;
|
|
|
|
|
2008-04-05 06:53:22 +02:00
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
|
|
|
if (!finished && has_db)
|
|
|
|
{
|
|
|
|
// don't bias fast peers when seeding
|
|
|
|
std::pair<const int, int>* inet_as = i->second.inet_as;
|
|
|
|
int peak_rate = inet_as ? inet_as->second : 0;
|
|
|
|
if (peak_rate <= max_inet_as_rate) continue;
|
|
|
|
max_inet_as_rate = peak_rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max_inet_as_rate <= 0)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
int distance = cidr_distance(external_ip, i->second.ip.address());
|
|
|
|
if (distance > min_cidr_distance) continue;
|
|
|
|
|
|
|
|
min_cidr_distance = distance;
|
|
|
|
}
|
|
|
|
|
2008-02-28 04:09:34 +01:00
|
|
|
min_connect_time = i->second.connected;
|
|
|
|
candidate = i;
|
2004-01-14 17:18:53 +01:00
|
|
|
}
|
|
|
|
|
2008-03-29 19:47:24 +01:00
|
|
|
m_num_connect_candidates = connect_candidates;
|
2008-04-01 19:38:19 +02:00
|
|
|
m_num_seeds = seeds;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(min_connect_time <= now);
|
2004-01-14 17:18:53 +01:00
|
|
|
|
2008-02-28 04:09:34 +01:00
|
|
|
#if defined TORRENT_LOGGING || defined TORRENT_VERBOSE_LOGGING
|
|
|
|
if (candidate != m_peers.end())
|
|
|
|
{
|
2008-02-28 08:34:07 +01:00
|
|
|
(*m_torrent->session().m_logger) << time_now_string()
|
|
|
|
<< " *** FOUND CONNECTION CANDIDATE ["
|
2008-02-28 04:09:34 +01:00
|
|
|
" ip: " << candidate->second.ip <<
|
|
|
|
" d: " << min_cidr_distance <<
|
|
|
|
" external: " << external_ip <<
|
|
|
|
" t: " << total_seconds(time_now() - min_connect_time) <<
|
|
|
|
" ]\n";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-01-14 17:18:53 +01:00
|
|
|
return candidate;
|
|
|
|
}
|
2004-05-14 01:34:42 +02:00
|
|
|
|
2003-12-01 06:01:40 +01:00
|
|
|
void policy::pulse()
|
|
|
|
{
|
2005-05-12 01:03:12 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2004-11-30 12:17:32 +01:00
|
|
|
if (m_torrent->is_paused()) return;
|
|
|
|
|
2007-07-04 04:16:49 +02:00
|
|
|
piece_picker* p = 0;
|
|
|
|
if (m_torrent->has_picker())
|
|
|
|
p = &m_torrent->picker();
|
|
|
|
|
2008-04-05 23:18:27 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2008-04-01 19:38:19 +02:00
|
|
|
bool pinged = false;
|
2008-04-05 23:18:27 +02:00
|
|
|
#endif
|
2008-04-01 19:38:19 +02:00
|
|
|
|
2007-05-05 02:29:33 +02:00
|
|
|
ptime now = time_now();
|
2004-01-21 01:59:38 +01:00
|
|
|
// remove old disconnected peers from the list
|
2007-04-13 19:47:40 +02:00
|
|
|
for (iterator i = m_peers.begin(); i != m_peers.end();)
|
|
|
|
{
|
2008-04-01 19:38:19 +02:00
|
|
|
peer& pe = i->second;
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
// try to send a DHT ping to this peer
|
|
|
|
// as well, to figure out if it supports
|
|
|
|
// DHT (uTorrent and BitComet doesn't
|
|
|
|
// advertise support)
|
|
|
|
if (!pinged && !pe.added_to_dht)
|
|
|
|
{
|
|
|
|
udp::endpoint node(pe.ip.address(), pe.ip.port());
|
|
|
|
m_torrent->session().add_dht_node(node);
|
|
|
|
pe.added_to_dht = true;
|
|
|
|
pinged = true;
|
|
|
|
}
|
|
|
|
#endif
|
2007-04-13 19:47:40 +02:00
|
|
|
// this timeout has to be customizable!
|
2007-10-03 19:37:15 +02:00
|
|
|
// don't remove banned peers, they should
|
|
|
|
// remain banned
|
2008-04-01 19:38:19 +02:00
|
|
|
if (pe.connection == 0
|
|
|
|
&& pe.connected != min_time()
|
|
|
|
&& !pe.banned
|
|
|
|
&& now - pe.connected > minutes(120))
|
2007-04-13 19:47:40 +02:00
|
|
|
{
|
2008-04-01 19:38:19 +02:00
|
|
|
if (p) p->clear_peer(&pe);
|
|
|
|
if (pe.seed) --m_num_seeds;
|
2007-04-13 19:47:40 +02:00
|
|
|
m_peers.erase(i++);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2003-12-01 06:01:40 +01:00
|
|
|
|
2004-03-01 22:54:10 +01:00
|
|
|
// -------------------------------------
|
|
|
|
// maintain the number of connections
|
|
|
|
// -------------------------------------
|
2007-08-16 14:41:46 +02:00
|
|
|
/*
|
2004-03-01 22:54:10 +01:00
|
|
|
// count the number of connected peers except for peers
|
|
|
|
// that are currently in the process of disconnecting
|
|
|
|
int num_connected_peers = 0;
|
|
|
|
|
2007-04-10 23:23:13 +02:00
|
|
|
for (iterator i = m_peers.begin();
|
2007-04-12 12:21:55 +02:00
|
|
|
i != m_peers.end(); ++i)
|
2004-01-21 01:59:38 +01:00
|
|
|
{
|
2004-03-01 22:54:10 +01:00
|
|
|
if (i->connection && !i->connection->is_disconnecting())
|
|
|
|
++num_connected_peers;
|
|
|
|
}
|
2004-01-21 01:59:38 +01:00
|
|
|
|
2007-08-21 09:33:06 +02:00
|
|
|
if (m_torrent->max_connections() != (std::numeric_limits<int>::max)())
|
2004-03-01 22:54:10 +01:00
|
|
|
{
|
2007-08-16 14:41:46 +02:00
|
|
|
int max_connections = m_torrent->max_connections();
|
2004-01-21 01:59:38 +01:00
|
|
|
|
2004-03-01 22:54:10 +01:00
|
|
|
if (num_connected_peers >= max_connections)
|
2004-01-21 01:59:38 +01:00
|
|
|
{
|
|
|
|
// every minute, disconnect the worst peer in hope of finding a better peer
|
|
|
|
|
2007-04-05 00:27:36 +02:00
|
|
|
ptime local_time = time_now();
|
|
|
|
if (m_last_optimistic_disconnect + seconds(120) <= local_time
|
2007-04-12 12:21:55 +02:00
|
|
|
&& find_connect_candidate() != m_peers.end())
|
2004-01-21 01:59:38 +01:00
|
|
|
{
|
2004-03-01 22:54:10 +01:00
|
|
|
m_last_optimistic_disconnect = local_time;
|
2004-01-21 01:59:38 +01:00
|
|
|
--max_connections; // this will have the effect of disconnecting the worst peer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// don't do a disconnect earlier than 1 minute after some peer was connected
|
2007-04-05 00:27:36 +02:00
|
|
|
m_last_optimistic_disconnect = time_now();
|
2004-01-21 01:59:38 +01:00
|
|
|
}
|
|
|
|
|
2004-03-01 22:54:10 +01:00
|
|
|
while (num_connected_peers > max_connections)
|
2004-01-21 01:59:38 +01:00
|
|
|
{
|
2005-03-24 13:13:47 +01:00
|
|
|
bool ret = disconnect_one_peer();
|
2005-05-31 00:50:54 +02:00
|
|
|
(void)ret;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(ret);
|
2004-01-21 01:59:38 +01:00
|
|
|
--num_connected_peers;
|
|
|
|
}
|
|
|
|
}
|
2007-08-16 14:41:46 +02:00
|
|
|
*/
|
2004-03-01 22:54:10 +01:00
|
|
|
// ------------------------
|
|
|
|
// upload shift
|
|
|
|
// ------------------------
|
|
|
|
|
|
|
|
// this part will shift downloads
|
|
|
|
// from peers that are seeds and peers
|
|
|
|
// that don't want to download from us
|
|
|
|
// to peers that cannot upload anything
|
|
|
|
// to us. The shifting will make sure
|
|
|
|
// that the torrent's share ratio
|
|
|
|
// will be maintained
|
|
|
|
|
2004-01-14 02:19:30 +01:00
|
|
|
// if the share ratio is 0 (infinite)
|
|
|
|
// m_available_free_upload isn't used
|
|
|
|
// because it isn't necessary
|
|
|
|
if (m_torrent->ratio() != 0.f)
|
|
|
|
{
|
|
|
|
// accumulate all the free download we get
|
|
|
|
// and add it to the available free upload
|
|
|
|
m_available_free_upload
|
|
|
|
+= collect_free_download(
|
|
|
|
m_torrent->begin()
|
|
|
|
, m_torrent->end());
|
|
|
|
|
|
|
|
// distribute the free upload among the peers
|
|
|
|
m_available_free_upload = distribute_free_upload(
|
2003-12-14 23:55:32 +01:00
|
|
|
m_torrent->begin()
|
2004-01-14 02:19:30 +01:00
|
|
|
, m_torrent->end()
|
|
|
|
, m_available_free_upload);
|
|
|
|
}
|
2003-12-01 06:01:40 +01:00
|
|
|
}
|
|
|
|
|
2007-06-08 00:37:58 +02:00
|
|
|
int policy::count_choked() const
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
for (const_iterator i = m_peers.begin();
|
|
|
|
i != m_peers.end(); ++i)
|
|
|
|
{
|
2007-10-02 21:11:04 +02:00
|
|
|
if (!i->second.connection
|
|
|
|
|| i->second.connection->is_connecting()
|
|
|
|
|| i->second.connection->is_disconnecting()
|
|
|
|
|| !i->second.connection->is_peer_interested())
|
2007-06-08 00:37:58 +02:00
|
|
|
continue;
|
2007-10-02 21:11:04 +02:00
|
|
|
if (i->second.connection->is_choked()) ++ret;
|
2007-06-08 00:37:58 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-01-07 02:10:46 +01:00
|
|
|
bool policy::new_connection(peer_connection& c)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!c.is_local());
|
2006-11-14 16:53:38 +01:00
|
|
|
|
2007-10-31 10:48:20 +01:00
|
|
|
// INVARIANT_CHECK;
|
2006-11-14 16:53:38 +01:00
|
|
|
|
2004-03-01 01:50:00 +01:00
|
|
|
// if the connection comes from the tracker,
|
|
|
|
// it's probably just a NAT-check. Ignore the
|
|
|
|
// num connections constraint then.
|
2004-09-16 03:14:16 +02:00
|
|
|
|
2004-03-28 19:45:37 +02:00
|
|
|
// TODO: only allow _one_ connection to use this
|
|
|
|
// override at a time
|
2008-01-07 02:10:46 +01:00
|
|
|
asio::error_code ec;
|
|
|
|
TORRENT_ASSERT(c.remote() == c.get_socket()->remote_endpoint(ec) || ec);
|
2007-02-12 06:46:29 +01:00
|
|
|
|
2008-04-05 06:53:22 +02:00
|
|
|
aux::session_impl& ses = m_torrent->session();
|
|
|
|
|
2007-08-16 14:41:46 +02:00
|
|
|
if (m_torrent->num_peers() >= m_torrent->max_connections()
|
2008-04-05 06:53:22 +02:00
|
|
|
&& ses.num_connections() >= ses.max_connections()
|
2006-04-25 23:04:48 +02:00
|
|
|
&& c.remote().address() != m_torrent->current_tracker().address())
|
2004-03-01 01:50:00 +01:00
|
|
|
{
|
2008-01-07 02:10:46 +01:00
|
|
|
c.disconnect("too many connections, refusing incoming connection");
|
|
|
|
return false;
|
2004-03-01 01:50:00 +01:00
|
|
|
}
|
|
|
|
|
2008-02-28 04:09:34 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING
|
2006-04-25 23:04:48 +02:00
|
|
|
if (c.remote().address() == m_torrent->current_tracker().address())
|
2004-03-01 01:50:00 +01:00
|
|
|
{
|
|
|
|
m_torrent->debug_log("overriding connection limit for tracker NAT-check");
|
|
|
|
}
|
|
|
|
#endif
|
2004-01-21 01:59:38 +01:00
|
|
|
|
2007-04-10 23:23:13 +02:00
|
|
|
iterator i;
|
2004-01-15 02:29:43 +01:00
|
|
|
|
2006-11-14 16:53:38 +01:00
|
|
|
if (m_torrent->settings().allow_multiple_connections_per_ip)
|
|
|
|
{
|
2007-10-31 10:48:20 +01:00
|
|
|
tcp::endpoint remote = c.remote();
|
|
|
|
std::pair<iterator, iterator> range = m_peers.equal_range(remote.address());
|
|
|
|
i = std::find_if(range.first, range.second, match_peer_endpoint(remote));
|
|
|
|
|
|
|
|
if (i == range.second) i = m_peers.end();
|
2006-11-14 16:53:38 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-02 21:11:04 +02:00
|
|
|
i = m_peers.find(c.remote().address());
|
2006-11-14 16:53:38 +01:00
|
|
|
}
|
2006-09-28 15:27:34 +02:00
|
|
|
|
|
|
|
if (i != m_peers.end())
|
|
|
|
{
|
2007-10-02 21:11:04 +02:00
|
|
|
if (i->second.banned)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
|
|
|
c.disconnect("ip address banned, closing");
|
|
|
|
return false;
|
|
|
|
}
|
2006-09-28 15:27:34 +02:00
|
|
|
|
2007-10-02 21:11:04 +02:00
|
|
|
if (i->second.connection != 0)
|
2006-09-28 15:27:34 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(i->second.connection != &c);
|
2006-09-28 15:27:34 +02:00
|
|
|
// the new connection is a local (outgoing) connection
|
|
|
|
// or the current one is already connected
|
2007-10-02 21:11:04 +02:00
|
|
|
if (!i->second.connection->is_connecting() || c.is_local())
|
2006-09-28 15:27:34 +02:00
|
|
|
{
|
2008-01-07 02:10:46 +01:00
|
|
|
c.disconnect("duplicate connection, closing");
|
|
|
|
return false;
|
2006-09-28 15:27:34 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-02-28 04:09:34 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING
|
2006-09-28 15:27:34 +02:00
|
|
|
m_torrent->debug_log("duplicate connection. existing connection"
|
|
|
|
" is connecting and this connection is incoming. closing existing "
|
|
|
|
"connection in favour of this one");
|
|
|
|
#endif
|
2008-01-07 02:10:46 +01:00
|
|
|
i->second.connection->disconnect("incoming duplicate connection "
|
|
|
|
"with higher priority, closing");
|
2006-09-28 15:27:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2003-12-01 06:01:40 +01:00
|
|
|
{
|
2007-07-10 19:25:10 +02:00
|
|
|
// we don't have any info about this peer.
|
2003-12-01 06:01:40 +01:00
|
|
|
// add a new entry
|
2008-01-07 02:10:46 +01:00
|
|
|
asio::error_code ec;
|
|
|
|
TORRENT_ASSERT(c.remote() == c.get_socket()->remote_endpoint(ec) || ec);
|
2007-02-12 06:46:29 +01:00
|
|
|
|
2007-04-10 23:23:13 +02:00
|
|
|
peer p(c.remote(), peer::not_connectable, 0);
|
2007-10-03 02:55:06 +02:00
|
|
|
i = m_peers.insert(std::make_pair(c.remote().address(), p));
|
2008-04-05 06:53:22 +02:00
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
|
|
|
int as = ses.as_for_ip(c.remote().address());
|
|
|
|
#ifndef NDEBUG
|
|
|
|
i->second.inet_as_num = as;
|
|
|
|
#endif
|
|
|
|
i->second.inet_as = ses.lookup_as(as);
|
|
|
|
#endif
|
2003-12-01 06:01:40 +01:00
|
|
|
}
|
2007-07-10 19:25:10 +02:00
|
|
|
|
2007-10-02 21:11:04 +02:00
|
|
|
c.set_peer_info(&i->second);
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(i->second.connection == 0);
|
2007-10-02 21:11:04 +02:00
|
|
|
c.add_stat(i->second.prev_amount_download, i->second.prev_amount_upload);
|
|
|
|
i->second.prev_amount_download = 0;
|
|
|
|
i->second.prev_amount_upload = 0;
|
|
|
|
i->second.connection = &c;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(i->second.connection);
|
2007-10-15 07:03:29 +02:00
|
|
|
if (!c.fast_reconnect())
|
|
|
|
i->second.connected = time_now();
|
2008-03-29 19:47:24 +01:00
|
|
|
if (m_num_connect_candidates > 0)
|
|
|
|
--m_num_connect_candidates;
|
2008-01-07 02:10:46 +01:00
|
|
|
return true;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2008-01-07 02:10:46 +01:00
|
|
|
bool policy::update_peer_port(int port, policy::peer* p, int src)
|
2007-10-31 10:48:20 +01:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(p != 0);
|
2008-01-07 02:10:46 +01:00
|
|
|
TORRENT_ASSERT(p->connection);
|
|
|
|
|
|
|
|
if (p->ip.port() == port) return true;
|
2007-10-31 10:48:20 +01:00
|
|
|
|
|
|
|
if (m_torrent->settings().allow_multiple_connections_per_ip)
|
|
|
|
{
|
|
|
|
tcp::endpoint remote(p->ip.address(), port);
|
|
|
|
std::pair<iterator, iterator> range = m_peers.equal_range(remote.address());
|
|
|
|
iterator i = std::find_if(range.first, range.second
|
|
|
|
, match_peer_endpoint(remote));
|
|
|
|
if (i != m_peers.end())
|
|
|
|
{
|
|
|
|
policy::peer& pp = i->second;
|
|
|
|
if (pp.connection)
|
|
|
|
{
|
2008-01-07 02:10:46 +01:00
|
|
|
p->connection->disconnect("duplicate connection");
|
|
|
|
return false;
|
2007-10-31 10:48:20 +01:00
|
|
|
}
|
|
|
|
if (m_torrent->has_picker())
|
|
|
|
m_torrent->picker().clear_peer(&i->second);
|
2008-04-01 19:38:19 +02:00
|
|
|
if (i->second.seed) --m_num_seeds;
|
2007-10-31 10:48:20 +01:00
|
|
|
m_peers.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_peers.count(p->ip.address()) == 1);
|
|
|
|
}
|
|
|
|
p->ip.port(port);
|
|
|
|
p->source |= src;
|
2008-01-07 02:10:46 +01:00
|
|
|
return true;
|
2007-10-31 10:48:20 +01:00
|
|
|
}
|
|
|
|
|
2007-12-27 21:57:58 +01:00
|
|
|
bool policy::has_peer(policy::peer const* p) const
|
|
|
|
{
|
|
|
|
// find p in m_peers
|
|
|
|
for (std::multimap<address, peer>::const_iterator i = m_peers.begin()
|
|
|
|
, end(m_peers.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (&i->second == p) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-10-31 10:48:20 +01:00
|
|
|
policy::peer* policy::peer_from_tracker(tcp::endpoint const& remote, peer_id const& pid
|
2007-04-10 23:23:13 +02:00
|
|
|
, int src, char flags)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-08-27 07:32:47 +02:00
|
|
|
// too expensive
|
|
|
|
// INVARIANT_CHECK;
|
2005-09-28 18:12:47 +02:00
|
|
|
|
2007-05-05 02:29:33 +02:00
|
|
|
// just ignore the obviously invalid entries
|
2007-08-14 19:47:48 +02:00
|
|
|
if (remote.address() == address() || remote.port() == 0)
|
2007-10-02 22:30:53 +02:00
|
|
|
return 0;
|
2004-01-16 17:19:27 +01:00
|
|
|
|
2007-06-01 03:05:57 +02:00
|
|
|
aux::session_impl& ses = m_torrent->session();
|
|
|
|
|
|
|
|
port_filter const& pf = ses.m_port_filter;
|
|
|
|
if (pf.access(remote.port()) & port_filter::blocked)
|
|
|
|
{
|
|
|
|
if (ses.m_alerts.should_post(alert::info))
|
|
|
|
{
|
|
|
|
ses.m_alerts.post_alert(peer_blocked_alert(remote.address()
|
|
|
|
, "outgoing port blocked, peer not added to peer list"));
|
|
|
|
}
|
2007-10-02 22:30:53 +02:00
|
|
|
return 0;
|
2007-06-01 03:05:57 +02:00
|
|
|
}
|
|
|
|
|
2008-04-07 03:29:21 +02:00
|
|
|
iterator i;
|
|
|
|
|
|
|
|
if (m_torrent->settings().allow_multiple_connections_per_ip)
|
2003-10-27 17:06:00 +01:00
|
|
|
{
|
2008-04-07 03:29:21 +02:00
|
|
|
std::pair<iterator, iterator> range = m_peers.equal_range(remote.address());
|
|
|
|
i = std::find_if(range.first, range.second, match_peer_endpoint(remote));
|
|
|
|
if (i == range.second) i = m_peers.end();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i = m_peers.find(remote.address());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == m_peers.end())
|
|
|
|
{
|
|
|
|
// if the IP is blocked, don't add it
|
|
|
|
if (ses.m_ip_filter.access(remote.address()) & ip_filter::blocked)
|
2003-12-01 06:01:40 +01:00
|
|
|
{
|
2008-04-07 03:29:21 +02:00
|
|
|
if (ses.m_alerts.should_post(alert::info))
|
2007-05-06 00:55:34 +02:00
|
|
|
{
|
2008-04-07 03:29:21 +02:00
|
|
|
ses.m_alerts.post_alert(peer_blocked_alert(remote.address()
|
|
|
|
, "blocked peer not added to peer list"));
|
2007-05-06 00:55:34 +02:00
|
|
|
}
|
2008-04-07 03:29:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we don't have any info about this peer.
|
|
|
|
// add a new entry
|
|
|
|
i = m_peers.insert(std::make_pair(remote.address()
|
2007-10-12 00:02:00 +02:00
|
|
|
, peer(remote, peer::connectable, src)));
|
2007-06-06 02:41:20 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
2008-04-07 03:29:21 +02:00
|
|
|
if (flags & 0x01) i->second.pe_support = true;
|
2007-06-06 02:41:20 +02:00
|
|
|
#endif
|
2008-04-07 03:29:21 +02:00
|
|
|
if (flags & 0x02)
|
|
|
|
{
|
|
|
|
i->second.seed = true;
|
|
|
|
++m_num_seeds;
|
|
|
|
}
|
2008-04-05 06:53:22 +02:00
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
2008-04-07 03:29:21 +02:00
|
|
|
int as = ses.as_for_ip(remote.address());
|
2008-04-05 06:53:22 +02:00
|
|
|
#ifndef NDEBUG
|
2008-04-07 03:29:21 +02:00
|
|
|
i->second.inet_as_num = as;
|
2008-04-05 06:53:22 +02:00
|
|
|
#endif
|
2008-04-07 03:29:21 +02:00
|
|
|
i->second.inet_as = ses.lookup_as(as);
|
2008-04-05 06:53:22 +02:00
|
|
|
#endif
|
2008-04-07 03:29:21 +02:00
|
|
|
if (is_connect_candidate(i->second, m_torrent->is_finished()))
|
|
|
|
++m_num_connect_candidates;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool was_conn_cand = is_connect_candidate(i->second, m_torrent->is_finished());
|
|
|
|
|
|
|
|
i->second.type = peer::connectable;
|
2004-01-15 17:45:34 +01:00
|
|
|
|
2008-04-07 03:29:21 +02:00
|
|
|
i->second.ip = remote;
|
|
|
|
i->second.source |= src;
|
2007-04-14 23:47:07 +02:00
|
|
|
|
2008-04-07 03:29:21 +02:00
|
|
|
// if this peer has failed before, decrease the
|
|
|
|
// counter to allow it another try, since somebody
|
|
|
|
// else is appearantly able to connect to it
|
|
|
|
// only trust this if it comes from the tracker
|
|
|
|
if (i->second.failcount > 0 && src == peer_info::tracker)
|
|
|
|
--i->second.failcount;
|
|
|
|
|
|
|
|
// if we're connected to this peer
|
|
|
|
// we already know if it's a seed or not
|
|
|
|
// so we don't have to trust this source
|
|
|
|
if ((flags & 0x02) && !i->second.connection)
|
|
|
|
{
|
|
|
|
if (!i->second.seed) ++m_num_seeds;
|
|
|
|
i->second.seed = true;
|
|
|
|
}
|
2004-01-15 02:29:43 +01:00
|
|
|
|
2008-02-28 04:09:34 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING
|
2008-04-07 03:29:21 +02:00
|
|
|
if (i->second.connection)
|
|
|
|
{
|
|
|
|
// this means we're already connected
|
|
|
|
// to this peer. don't connect to
|
|
|
|
// it again.
|
2005-09-18 12:18:23 +02:00
|
|
|
|
2008-04-07 03:29:21 +02:00
|
|
|
m_torrent->debug_log("already connected to peer: " + remote.address().to_string() + ":"
|
|
|
|
+ boost::lexical_cast<std::string>(remote.port()) + " "
|
|
|
|
+ boost::lexical_cast<std::string>(i->second.connection->pid()));
|
2005-09-18 12:18:23 +02:00
|
|
|
|
2008-04-07 03:29:21 +02:00
|
|
|
TORRENT_ASSERT(i->second.connection->associated_torrent().lock().get() == m_torrent);
|
2003-12-01 06:01:40 +01:00
|
|
|
}
|
2008-04-07 03:29:21 +02:00
|
|
|
#endif
|
2008-03-29 19:47:24 +01:00
|
|
|
|
2008-04-07 03:29:21 +02:00
|
|
|
if (was_conn_cand != is_connect_candidate(i->second, m_torrent->is_finished()))
|
|
|
|
if (was_conn_cand) --m_num_connect_candidates;
|
|
|
|
else ++m_num_connect_candidates;
|
2004-01-12 04:05:10 +01:00
|
|
|
}
|
2008-04-07 03:29:21 +02:00
|
|
|
|
|
|
|
return &i->second;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// this is called when we are choked by a peer
|
|
|
|
// i.e. a peer lets us know that we will not receive
|
|
|
|
// anything for a while
|
2005-04-24 02:50:52 +02:00
|
|
|
void policy::choked(peer_connection&)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-12-14 06:56:12 +01:00
|
|
|
void policy::piece_finished(int index, bool successfully_verified)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2005-09-28 18:12:47 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(index >= 0 && index < m_torrent->torrent_file().num_pieces());
|
2004-01-25 23:41:55 +01:00
|
|
|
|
2003-12-17 04:40:13 +01:00
|
|
|
if (successfully_verified)
|
|
|
|
{
|
|
|
|
// have all peers update their interested-flag
|
2007-04-10 23:23:13 +02:00
|
|
|
for (iterator i = m_peers.begin();
|
2005-08-18 22:38:03 +02:00
|
|
|
i != m_peers.end(); ++i)
|
2003-12-17 04:40:13 +01:00
|
|
|
{
|
2007-10-02 21:11:04 +02:00
|
|
|
if (i->second.connection == 0) continue;
|
2003-12-17 04:40:13 +01:00
|
|
|
// if we're not interested, we will not become interested
|
2007-10-02 21:11:04 +02:00
|
|
|
if (!i->second.connection->is_interesting()) continue;
|
|
|
|
if (!i->second.connection->has_piece(index)) continue;
|
2003-12-17 04:40:13 +01:00
|
|
|
|
2007-10-02 21:11:04 +02:00
|
|
|
i->second.connection->update_interest();
|
2003-12-17 04:40:13 +01:00
|
|
|
}
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// this is called when we are unchoked by a peer
|
|
|
|
// i.e. a peer lets us know that we will receive
|
|
|
|
// data from now on
|
|
|
|
void policy::unchoked(peer_connection& c)
|
|
|
|
{
|
2007-09-22 18:24:26 +02:00
|
|
|
// INVARIANT_CHECK;
|
2003-12-01 06:01:40 +01:00
|
|
|
if (c.is_interesting())
|
|
|
|
{
|
|
|
|
request_a_block(*m_torrent, c);
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2003-12-14 06:56:12 +01:00
|
|
|
// called when a peer is interested in us
|
2005-08-05 04:43:44 +02:00
|
|
|
void policy::interested(peer_connection& c)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-09-22 18:24:26 +02:00
|
|
|
// INVARIANT_CHECK;
|
2005-09-28 18:12:47 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(std::find_if(m_peers.begin(), m_peers.end()
|
2007-10-02 21:11:04 +02:00
|
|
|
, boost::bind<bool>(std::equal_to<peer_connection*>(), bind(&peer::connection
|
|
|
|
, bind(&iterator::value_type::second, _1)), &c)) != m_peers.end());
|
2005-09-28 20:32:05 +02:00
|
|
|
|
2008-04-05 06:53:22 +02:00
|
|
|
aux::session_impl& ses = m_torrent->session();
|
|
|
|
|
2005-08-05 04:43:44 +02:00
|
|
|
// if the peer is choked and we have upload slots left,
|
|
|
|
// then unchoke it. Another condition that has to be met
|
|
|
|
// is that the torrent doesn't keep track of the individual
|
|
|
|
// up/down ratio for each peer (ratio == 0) or (if it does
|
|
|
|
// keep track) this particular connection isn't a leecher.
|
|
|
|
// If the peer was choked because it was leeching, don't
|
|
|
|
// unchoke it again.
|
|
|
|
// The exception to this last condition is if we're a seed.
|
|
|
|
// In that case we don't care if people are leeching, they
|
|
|
|
// can't pay for their downloads anyway.
|
|
|
|
if (c.is_choked()
|
2008-04-05 06:53:22 +02:00
|
|
|
&& ses.num_uploads() < ses.max_uploads()
|
2005-09-28 20:07:00 +02:00
|
|
|
&& (m_torrent->ratio() == 0
|
|
|
|
|| c.share_diff() >= -free_upload_amount
|
2007-09-04 00:57:09 +02:00
|
|
|
|| m_torrent->is_finished()))
|
2005-08-05 04:43:44 +02:00
|
|
|
{
|
2008-04-05 06:53:22 +02:00
|
|
|
ses.unchoke_peer(c);
|
2007-08-16 14:41:46 +02:00
|
|
|
}
|
2008-02-28 04:09:34 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING
|
2007-08-16 14:41:46 +02:00
|
|
|
else if (c.is_choked())
|
|
|
|
{
|
|
|
|
std::string reason;
|
2008-04-05 06:53:22 +02:00
|
|
|
if (ses.num_uploads() >= ses.max_uploads())
|
2007-08-16 14:41:46 +02:00
|
|
|
{
|
|
|
|
reason = "the number of uploads ("
|
2008-04-05 06:53:22 +02:00
|
|
|
+ boost::lexical_cast<std::string>(ses.num_uploads())
|
2007-08-16 14:41:46 +02:00
|
|
|
+ ") is more than or equal to the limit ("
|
2008-04-05 06:53:22 +02:00
|
|
|
+ boost::lexical_cast<std::string>(ses.max_uploads())
|
2007-08-16 14:41:46 +02:00
|
|
|
+ ")";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reason = "the share ratio ("
|
|
|
|
+ boost::lexical_cast<std::string>(c.share_diff())
|
|
|
|
+ ") is <= free_upload_amount ("
|
|
|
|
+ boost::lexical_cast<std::string>(int(free_upload_amount))
|
|
|
|
+ ") and we are not seeding and the ratio ("
|
|
|
|
+ boost::lexical_cast<std::string>(m_torrent->ratio())
|
|
|
|
+ ")is non-zero";
|
|
|
|
}
|
|
|
|
(*c.m_logger) << time_now_string() << " DID NOT UNCHOKE [ " << reason << " ]\n";
|
2005-08-05 04:43:44 +02:00
|
|
|
}
|
2007-08-16 14:41:46 +02:00
|
|
|
#endif
|
2003-12-14 23:55:32 +01:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2003-12-14 06:56:12 +01:00
|
|
|
// called when a peer is no longer interested in us
|
2003-10-23 01:00:57 +02:00
|
|
|
void policy::not_interested(peer_connection& c)
|
|
|
|
{
|
2005-09-28 18:12:47 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2004-01-17 21:04:19 +01:00
|
|
|
if (m_torrent->ratio() != 0.f)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(c.share_diff() < (std::numeric_limits<size_type>::max)());
|
2004-01-25 19:18:36 +01:00
|
|
|
size_type diff = c.share_diff();
|
2004-01-17 21:04:19 +01:00
|
|
|
if (diff > 0 && c.is_seed())
|
|
|
|
{
|
|
|
|
// the peer is a seed and has sent
|
|
|
|
// us more than we have sent it back.
|
|
|
|
// consider the download as free download
|
|
|
|
m_available_free_upload += diff;
|
|
|
|
c.add_free_upload(-diff);
|
|
|
|
}
|
|
|
|
}
|
2007-06-09 01:02:31 +02:00
|
|
|
/*
|
2004-03-21 03:03:37 +01:00
|
|
|
if (!c.is_choked())
|
|
|
|
{
|
|
|
|
c.send_choke();
|
|
|
|
--m_num_unchoked;
|
2006-06-12 01:24:36 +02:00
|
|
|
|
|
|
|
if (m_torrent->is_seed()) seed_unchoke_one_peer();
|
|
|
|
else unchoke_one_peer();
|
2004-03-21 03:03:37 +01:00
|
|
|
}
|
2007-06-09 01:02:31 +02:00
|
|
|
*/
|
2003-12-14 06:56:12 +01:00
|
|
|
}
|
2007-08-16 14:41:46 +02:00
|
|
|
/*
|
2003-12-14 06:56:12 +01:00
|
|
|
bool policy::unchoke_one_peer()
|
|
|
|
{
|
2007-04-13 19:47:40 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-04-12 12:21:55 +02:00
|
|
|
iterator p = find_unchoke_candidate();
|
|
|
|
if (p == m_peers.end()) return false;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(p->connection);
|
|
|
|
TORRENT_ASSERT(!p->connection->is_disconnecting());
|
2003-12-14 06:56:12 +01:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(p->connection->is_choked());
|
2004-01-12 21:31:27 +01:00
|
|
|
p->connection->send_unchoke();
|
2007-04-05 00:27:36 +02:00
|
|
|
p->last_optimistically_unchoked = time_now();
|
2003-12-14 06:56:12 +01:00
|
|
|
++m_num_unchoked;
|
|
|
|
return true;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2005-08-18 22:38:03 +02:00
|
|
|
void policy::choke_one_peer()
|
|
|
|
{
|
2007-04-13 19:47:40 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-04-12 12:21:55 +02:00
|
|
|
iterator p = find_choke_candidate();
|
|
|
|
if (p == m_peers.end()) return;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(p->connection);
|
|
|
|
TORRENT_ASSERT(!p->connection->is_disconnecting());
|
|
|
|
TORRENT_ASSERT(!p->connection->is_choked());
|
2005-08-18 22:38:03 +02:00
|
|
|
p->connection->send_choke();
|
|
|
|
--m_num_unchoked;
|
|
|
|
}
|
2007-08-16 14:41:46 +02:00
|
|
|
*/
|
2004-01-14 17:18:53 +01:00
|
|
|
bool policy::connect_one_peer()
|
|
|
|
{
|
2007-09-22 18:24:26 +02:00
|
|
|
// INVARIANT_CHECK;
|
2007-04-13 19:47:40 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_torrent->want_more_peers());
|
2007-04-12 12:21:55 +02:00
|
|
|
|
2007-05-06 00:55:34 +02:00
|
|
|
iterator p = find_connect_candidate();
|
|
|
|
if (p == m_peers.end()) return false;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!p->second.banned);
|
|
|
|
TORRENT_ASSERT(!p->second.connection);
|
|
|
|
TORRENT_ASSERT(p->second.type == peer::connectable);
|
2004-02-25 14:18:41 +01:00
|
|
|
|
2008-04-07 03:29:21 +02:00
|
|
|
if (!m_torrent->connect_to_peer(&p->second))
|
2004-02-26 19:55:10 +01:00
|
|
|
{
|
2007-10-02 21:11:04 +02:00
|
|
|
++p->second.failcount;
|
2007-05-06 00:55:34 +02:00
|
|
|
return false;
|
2007-04-12 12:21:55 +02:00
|
|
|
}
|
2008-04-07 03:29:21 +02:00
|
|
|
return true;
|
2004-01-14 17:18:53 +01:00
|
|
|
}
|
|
|
|
|
2004-01-21 01:59:38 +01:00
|
|
|
bool policy::disconnect_one_peer()
|
|
|
|
{
|
2007-04-12 12:21:55 +02:00
|
|
|
iterator p = find_disconnect_candidate();
|
|
|
|
if (p == m_peers.end())
|
2004-01-21 01:59:38 +01:00
|
|
|
return false;
|
2008-02-28 04:09:34 +01:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
|
2007-10-02 21:11:04 +02:00
|
|
|
(*p->second.connection->m_logger) << "*** CLOSING CONNECTION 'too many connections'\n";
|
2005-09-01 23:04:21 +02:00
|
|
|
#endif
|
|
|
|
|
2008-01-07 02:10:46 +01:00
|
|
|
p->second.connection->disconnect("too many connections, closing");
|
2004-01-21 01:59:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-12-01 06:01:40 +01:00
|
|
|
// this is called whenever a peer connection is closed
|
2008-04-07 03:29:21 +02:00
|
|
|
void policy::connection_closed(const peer_connection& c)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-08-27 07:32:47 +02:00
|
|
|
// too expensive
|
|
|
|
// INVARIANT_CHECK;
|
2005-09-28 18:12:47 +02:00
|
|
|
|
2007-08-17 01:53:14 +02:00
|
|
|
peer* p = c.peer_info_struct();
|
2005-05-12 01:03:12 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT((std::find_if(
|
2004-01-15 17:45:34 +01:00
|
|
|
m_peers.begin()
|
|
|
|
, m_peers.end()
|
2007-08-17 01:53:14 +02:00
|
|
|
, match_peer_connection(c))
|
2007-08-21 19:45:28 +02:00
|
|
|
!= m_peers.end()) == (p != 0));
|
|
|
|
|
|
|
|
// if we couldn't find the connection in our list, just ignore it.
|
|
|
|
if (p == 0) return;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(p->connection == &c);
|
2003-12-01 06:01:40 +01:00
|
|
|
|
2007-08-17 01:53:14 +02:00
|
|
|
p->connection = 0;
|
|
|
|
p->optimistically_unchoked = false;
|
2003-12-01 06:01:40 +01:00
|
|
|
|
2007-10-04 23:26:50 +02:00
|
|
|
// if fast reconnect is true, we won't
|
|
|
|
// update the timestamp, and it will remain
|
|
|
|
// the time when we initiated the connection.
|
|
|
|
if (!c.fast_reconnect())
|
|
|
|
p->connected = time_now();
|
2004-01-14 02:19:30 +01:00
|
|
|
|
2004-03-21 03:03:37 +01:00
|
|
|
if (c.failed())
|
|
|
|
{
|
2007-08-17 01:53:14 +02:00
|
|
|
++p->failcount;
|
|
|
|
// p->connected = time_now();
|
2004-03-21 03:03:37 +01:00
|
|
|
}
|
|
|
|
|
2008-03-29 19:47:24 +01:00
|
|
|
if (is_connect_candidate(*p, m_torrent->is_finished()))
|
|
|
|
++m_num_connect_candidates;
|
|
|
|
|
2004-01-14 02:19:30 +01:00
|
|
|
// if the share ratio is 0 (infinite), the
|
|
|
|
// m_available_free_upload isn't used,
|
|
|
|
// because it isn't necessary.
|
|
|
|
if (m_torrent->ratio() != 0.f)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(c.associated_torrent().lock().get() == m_torrent);
|
|
|
|
TORRENT_ASSERT(c.share_diff() < (std::numeric_limits<size_type>::max)());
|
2007-04-12 12:21:55 +02:00
|
|
|
m_available_free_upload += c.share_diff();
|
2004-01-14 02:19:30 +01:00
|
|
|
}
|
2007-08-17 01:53:14 +02:00
|
|
|
p->prev_amount_download += c.statistics().total_payload_download();
|
|
|
|
p->prev_amount_upload += c.statistics().total_payload_upload();
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2003-12-14 06:56:12 +01:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
void policy::peer_is_interesting(peer_connection& c)
|
|
|
|
{
|
2007-09-22 18:24:26 +02:00
|
|
|
// INVARIANT_CHECK;
|
2005-09-28 18:12:47 +02:00
|
|
|
|
2004-01-12 21:31:27 +01:00
|
|
|
c.send_interested();
|
2007-08-14 19:47:48 +02:00
|
|
|
if (c.has_peer_choked()
|
|
|
|
&& c.allowed_fast().empty())
|
|
|
|
return;
|
2003-10-23 01:00:57 +02:00
|
|
|
request_a_block(*m_torrent, c);
|
|
|
|
}
|
2003-12-01 06:01:40 +01:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2004-01-15 02:29:43 +01:00
|
|
|
bool policy::has_connection(const peer_connection* c)
|
2003-12-01 06:01:40 +01:00
|
|
|
{
|
2007-08-21 21:18:06 +02:00
|
|
|
// too expensive
|
|
|
|
// INVARIANT_CHECK;
|
2007-04-13 19:47:40 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(c);
|
2008-04-07 03:29:21 +02:00
|
|
|
asio::error_code ec;
|
|
|
|
TORRENT_ASSERT(c->remote() == c->get_socket()->remote_endpoint(ec) || ec);
|
2007-02-12 06:46:29 +01:00
|
|
|
|
2004-01-15 17:45:34 +01:00
|
|
|
return std::find_if(
|
|
|
|
m_peers.begin()
|
|
|
|
, m_peers.end()
|
2006-11-14 16:53:38 +01:00
|
|
|
, match_peer_connection(*c)) != m_peers.end();
|
2003-12-01 06:01:40 +01:00
|
|
|
}
|
2003-12-14 06:56:12 +01:00
|
|
|
|
2004-05-10 08:12:29 +02:00
|
|
|
void policy::check_invariant() const
|
2003-12-14 06:56:12 +01:00
|
|
|
{
|
2008-03-29 19:47:24 +01:00
|
|
|
TORRENT_ASSERT(m_num_connect_candidates >= 0);
|
2005-05-12 01:14:58 +02:00
|
|
|
if (m_torrent->is_aborted()) return;
|
2007-10-31 10:48:20 +01:00
|
|
|
|
2005-09-28 22:22:34 +02:00
|
|
|
int connected_peers = 0;
|
2005-11-02 17:28:39 +01:00
|
|
|
|
|
|
|
int total_connections = 0;
|
|
|
|
int nonempty_connections = 0;
|
2007-08-14 19:47:48 +02:00
|
|
|
|
2007-10-02 21:11:04 +02:00
|
|
|
std::set<tcp::endpoint> unique_test;
|
2007-04-10 23:23:13 +02:00
|
|
|
for (const_iterator i = m_peers.begin();
|
2005-05-12 01:03:12 +02:00
|
|
|
i != m_peers.end(); ++i)
|
2003-12-14 06:56:12 +01:00
|
|
|
{
|
2007-10-02 21:11:04 +02:00
|
|
|
peer const& p = i->second;
|
2008-04-05 06:53:22 +02:00
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
|
|
|
TORRENT_ASSERT(p.inet_as == 0 || p.inet_as->first == p.inet_as_num);
|
|
|
|
#endif
|
2007-10-02 21:11:04 +02:00
|
|
|
if (!m_torrent->settings().allow_multiple_connections_per_ip)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_peers.count(p.ip.address()) == 1);
|
2007-10-02 21:11:04 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(unique_test.count(p.ip) == 0);
|
2007-10-02 21:11:04 +02:00
|
|
|
unique_test.insert(p.ip);
|
2007-10-31 10:48:20 +01:00
|
|
|
TORRENT_ASSERT(i->first == p.ip.address());
|
|
|
|
// TORRENT_ASSERT(p.connection == 0 || p.ip == p.connection->remote());
|
2007-10-02 21:11:04 +02:00
|
|
|
}
|
2005-11-02 17:28:39 +01:00
|
|
|
++total_connections;
|
2007-09-19 08:05:14 +02:00
|
|
|
if (!p.connection)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2007-08-21 03:17:42 +02:00
|
|
|
if (p.optimistically_unchoked)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(p.connection);
|
|
|
|
TORRENT_ASSERT(!p.connection->is_choked());
|
2007-08-21 03:17:42 +02:00
|
|
|
}
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(p.connection->peer_info_struct() == 0
|
2007-07-04 17:46:10 +02:00
|
|
|
|| p.connection->peer_info_struct() == &p);
|
2005-11-02 17:28:39 +01:00
|
|
|
++nonempty_connections;
|
2007-07-04 17:46:10 +02:00
|
|
|
if (!p.connection->is_disconnecting())
|
2005-09-29 01:58:55 +02:00
|
|
|
++connected_peers;
|
2003-12-14 06:56:12 +01:00
|
|
|
}
|
2005-09-28 22:22:34 +02:00
|
|
|
|
2005-09-29 01:58:55 +02:00
|
|
|
int num_torrent_peers = 0;
|
|
|
|
for (torrent::const_peer_iterator i = m_torrent->begin();
|
|
|
|
i != m_torrent->end(); ++i)
|
|
|
|
{
|
2007-10-31 10:48:20 +01:00
|
|
|
if ((*i)->is_disconnecting()) continue;
|
2006-04-25 23:04:48 +02:00
|
|
|
// ignore web_peer_connections since they are not managed
|
|
|
|
// by the policy class
|
2007-10-31 10:48:20 +01:00
|
|
|
if (dynamic_cast<web_peer_connection*>(*i)) continue;
|
2005-09-29 01:58:55 +02:00
|
|
|
++num_torrent_peers;
|
|
|
|
}
|
2005-09-28 22:22:34 +02:00
|
|
|
|
2007-07-04 04:16:49 +02:00
|
|
|
if (m_torrent->has_picker())
|
|
|
|
{
|
|
|
|
piece_picker& p = m_torrent->picker();
|
|
|
|
std::vector<piece_picker::downloading_piece> downloaders = p.get_download_queue();
|
|
|
|
|
|
|
|
std::set<void*> peer_set;
|
|
|
|
std::vector<void*> peers;
|
|
|
|
for (std::vector<piece_picker::downloading_piece>::iterator i = downloaders.begin()
|
|
|
|
, end(downloaders.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
p.get_downloaders(peers, i->index);
|
|
|
|
std::copy(peers.begin(), peers.end()
|
|
|
|
, std::insert_iterator<std::set<void*> >(peer_set, peer_set.begin()));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::set<void*>::iterator i = peer_set.begin()
|
|
|
|
, end(peer_set.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
policy::peer* p = static_cast<policy::peer*>(*i);
|
|
|
|
if (p == 0) continue;
|
2007-10-06 23:15:44 +02:00
|
|
|
if (p->connection == 0) continue;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(std::find_if(m_peers.begin(), m_peers.end()
|
2007-10-02 21:11:04 +02:00
|
|
|
, match_peer_connection(*p->connection)) != m_peers.end());
|
2007-07-04 04:16:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-01 17:12:10 +02:00
|
|
|
// this invariant is a bit complicated.
|
|
|
|
// the usual case should be that connected_peers
|
|
|
|
// == num_torrent_peers. But when there's an incoming
|
|
|
|
// connection, it will first be added to the policy
|
|
|
|
// and then be added to the torrent.
|
|
|
|
// When there's an outgoing connection, it will first
|
|
|
|
// be added to the torrent and then to the policy.
|
|
|
|
// that's why the two second cases are in there.
|
2007-05-25 19:06:30 +02:00
|
|
|
/*
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(connected_peers == num_torrent_peers
|
2005-10-01 17:12:10 +02:00
|
|
|
|| (connected_peers == num_torrent_peers + 1
|
|
|
|
&& connected_peers > 0)
|
|
|
|
|| (connected_peers + 1 == num_torrent_peers
|
|
|
|
&& num_torrent_peers > 0));
|
2007-05-25 19:06:30 +02:00
|
|
|
*/
|
2003-12-14 06:56:12 +01:00
|
|
|
}
|
2003-12-01 06:01:40 +01:00
|
|
|
#endif
|
2003-12-14 06:56:12 +01:00
|
|
|
|
2007-04-10 23:23:13 +02:00
|
|
|
policy::peer::peer(const tcp::endpoint& ip_, peer::connection_type t, int src)
|
2007-03-28 21:56:53 +02:00
|
|
|
: ip(ip_)
|
2008-04-05 06:53:22 +02:00
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
|
|
|
, inet_as(0)
|
|
|
|
#endif
|
2007-03-28 21:56:53 +02:00
|
|
|
, failcount(0)
|
2008-04-01 19:38:19 +02:00
|
|
|
, trust_points(0)
|
|
|
|
, source(src)
|
2007-05-25 21:42:10 +02:00
|
|
|
, hashfails(0)
|
2008-04-05 06:53:22 +02:00
|
|
|
, type(t)
|
2007-10-12 03:12:30 +02:00
|
|
|
, fast_reconnects(0)
|
2008-04-01 19:38:19 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
, pe_support(true)
|
|
|
|
#endif
|
2007-08-16 14:41:46 +02:00
|
|
|
, optimistically_unchoked(false)
|
2008-04-01 19:38:19 +02:00
|
|
|
, seed(false)
|
2007-05-25 21:42:10 +02:00
|
|
|
, on_parole(false)
|
2003-12-14 06:56:12 +01:00
|
|
|
, banned(false)
|
2008-04-01 19:38:19 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
, added_to_dht(false)
|
|
|
|
#endif
|
2004-01-14 17:18:53 +01:00
|
|
|
, connection(0)
|
2008-04-01 19:38:19 +02:00
|
|
|
, prev_amount_upload(0)
|
|
|
|
, prev_amount_download(0)
|
|
|
|
, last_optimistically_unchoked(min_time())
|
|
|
|
, connected(min_time())
|
2004-01-14 17:18:53 +01:00
|
|
|
{
|
2008-04-01 19:38:19 +02:00
|
|
|
TORRENT_ASSERT((src & 0xff) == src);
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(connected < time_now());
|
2004-01-14 17:18:53 +01:00
|
|
|
}
|
2003-12-14 06:56:12 +01:00
|
|
|
|
2004-01-21 01:59:38 +01:00
|
|
|
size_type policy::peer::total_download() const
|
2003-12-14 06:56:12 +01:00
|
|
|
{
|
|
|
|
if (connection != 0)
|
2004-01-15 17:45:34 +01:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(prev_amount_download == 0);
|
2004-02-25 00:55:42 +01:00
|
|
|
return connection->statistics().total_payload_download();
|
2004-01-15 17:45:34 +01:00
|
|
|
}
|
2003-12-14 06:56:12 +01:00
|
|
|
else
|
2004-01-15 17:45:34 +01:00
|
|
|
{
|
2003-12-14 06:56:12 +01:00
|
|
|
return prev_amount_download;
|
2004-01-15 17:45:34 +01:00
|
|
|
}
|
2003-12-14 06:56:12 +01:00
|
|
|
}
|
|
|
|
|
2004-01-21 01:59:38 +01:00
|
|
|
size_type policy::peer::total_upload() const
|
2003-12-14 06:56:12 +01:00
|
|
|
{
|
|
|
|
if (connection != 0)
|
2004-01-15 17:45:34 +01:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(prev_amount_upload == 0);
|
2004-02-25 00:55:42 +01:00
|
|
|
return connection->statistics().total_payload_upload();
|
2004-01-15 17:45:34 +01:00
|
|
|
}
|
2003-12-14 06:56:12 +01:00
|
|
|
else
|
2004-01-15 17:45:34 +01:00
|
|
|
{
|
2003-12-14 06:56:12 +01:00
|
|
|
return prev_amount_upload;
|
2004-01-15 17:45:34 +01:00
|
|
|
}
|
2003-12-14 06:56:12 +01:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2005-08-18 22:38:03 +02:00
|
|
|
|