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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_POLICY_HPP_INCLUDED
|
|
|
|
#define TORRENT_POLICY_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
#include "libtorrent/peer.hpp"
|
2003-10-24 04:18:11 +02:00
|
|
|
#include "libtorrent/piece_picker.hpp"
|
2004-01-12 04:05:10 +01:00
|
|
|
#include "libtorrent/socket.hpp"
|
2004-01-21 01:59:38 +01:00
|
|
|
#include "libtorrent/size_type.hpp"
|
2005-08-18 22:38:03 +02:00
|
|
|
#include "libtorrent/invariant_check.hpp"
|
2005-11-01 19:30:39 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2007-04-05 00:27:36 +02:00
|
|
|
#include "libtorrent/time.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
|
|
|
class torrent;
|
|
|
|
class peer_connection;
|
|
|
|
|
2005-09-27 10:07:24 +02:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
// the limits of the download queue size
|
|
|
|
min_request_queue = 2,
|
|
|
|
|
|
|
|
// the amount of free upload allowed before
|
|
|
|
// the peer is choked
|
|
|
|
free_upload_amount = 4 * 16 * 1024
|
|
|
|
};
|
|
|
|
|
2007-07-06 19:15:35 +02:00
|
|
|
void request_a_block(torrent& t, peer_connection& c);
|
2005-09-27 10:07:24 +02:00
|
|
|
|
2005-11-01 19:30:39 +01:00
|
|
|
class TORRENT_EXPORT policy
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
policy(torrent* t);
|
2003-12-01 06:01:40 +01:00
|
|
|
|
|
|
|
// this is called every 10 seconds to allow
|
|
|
|
// for peer choking management
|
|
|
|
void pulse();
|
|
|
|
|
2007-10-02 22:30:53 +02:00
|
|
|
struct peer;
|
2003-10-23 01:00:57 +02:00
|
|
|
// this is called once for every peer we get from
|
2007-04-10 23:23:13 +02:00
|
|
|
// the tracker, pex, lsd or dht.
|
2007-10-02 22:30:53 +02:00
|
|
|
policy::peer* peer_from_tracker(const tcp::endpoint& remote, const peer_id& pid
|
2007-04-10 23:23:13 +02:00
|
|
|
, int source, char flags);
|
2008-01-07 02:10:46 +01:00
|
|
|
|
|
|
|
// false means duplicate connection
|
|
|
|
bool update_peer_port(int port, policy::peer* p, int src);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-14 17:18:53 +01:00
|
|
|
// called when an incoming connection is accepted
|
2008-01-07 02:10:46 +01:00
|
|
|
// false means the connection was refused or failed
|
|
|
|
bool new_connection(peer_connection& c);
|
2004-01-14 17:18:53 +01:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// the given connection was just closed
|
2008-04-07 03:29:21 +02:00
|
|
|
void connection_closed(const peer_connection& c);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
// the peer has got at least one interesting piece
|
|
|
|
void peer_is_interesting(peer_connection& c);
|
|
|
|
|
2003-12-14 06:56:12 +01:00
|
|
|
void piece_finished(int index, bool successfully_verified);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
// the peer choked us
|
|
|
|
void choked(peer_connection& c);
|
|
|
|
|
2007-06-08 00:37:58 +02:00
|
|
|
int count_choked() const;
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// the peer unchoked us
|
|
|
|
void unchoked(peer_connection& c);
|
|
|
|
|
|
|
|
// the peer is interested in our pieces
|
|
|
|
void interested(peer_connection& c);
|
|
|
|
|
|
|
|
// the peer is not interested in our pieces
|
|
|
|
void not_interested(peer_connection& c);
|
|
|
|
|
2007-07-26 09:04:35 +02:00
|
|
|
void ip_filter_updated();
|
|
|
|
|
2003-12-01 06:01:40 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
bool has_connection(const peer_connection* p);
|
2003-12-14 06:56:12 +01:00
|
|
|
|
2004-05-10 08:12:29 +02:00
|
|
|
void check_invariant() const;
|
2003-12-01 06:01:40 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
struct peer
|
|
|
|
{
|
2007-07-04 20:05:29 +02:00
|
|
|
enum connection_type { not_connectable, connectable };
|
|
|
|
peer(tcp::endpoint const& ip, connection_type t, int src);
|
2003-12-14 06:56:12 +01:00
|
|
|
|
2004-01-21 01:59:38 +01:00
|
|
|
size_type total_download() const;
|
|
|
|
size_type total_upload() const;
|
2003-12-01 06:01:40 +01:00
|
|
|
|
2004-01-12 04:05:10 +01:00
|
|
|
// the ip/port pair this peer is or was connected on
|
2006-04-25 23:04:48 +02:00
|
|
|
tcp::endpoint ip;
|
2008-04-05 06:53:22 +02:00
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_GEO_IP
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// only used in debug mode to assert that
|
|
|
|
// the first entry in the AS pair keeps the same
|
|
|
|
boost::uint16_t inet_as_num;
|
|
|
|
#endif
|
|
|
|
// The AS this peer belongs to
|
|
|
|
std::pair<const int, int>* inet_as;
|
|
|
|
#endif
|
2004-01-12 04:05:10 +01:00
|
|
|
|
2008-04-01 19:38:19 +02:00
|
|
|
// the number of failed connection attempts
|
|
|
|
// this peer has
|
|
|
|
boost::uint8_t failcount;
|
|
|
|
|
|
|
|
// for every valid piece we receive where this
|
|
|
|
// peer was one of the participants, we increase
|
|
|
|
// this value. For every invalid piece we receive
|
|
|
|
// where this peer was a participant, we decrease
|
|
|
|
// this value. If it sinks below a threshold, its
|
|
|
|
// considered a bad peer and will be banned.
|
|
|
|
boost::int8_t trust_points;
|
|
|
|
|
|
|
|
// a bitmap combining the peer_source flags
|
|
|
|
// from peer_info.
|
|
|
|
boost::uint8_t source;
|
2007-03-28 21:56:53 +02:00
|
|
|
|
2007-05-25 21:42:10 +02:00
|
|
|
// the number of times this peer has been
|
|
|
|
// part of a piece that failed the hash check
|
2008-04-01 19:38:19 +02:00
|
|
|
boost::uint8_t hashfails;
|
2007-05-25 21:42:10 +02:00
|
|
|
|
2008-04-05 06:53:22 +02:00
|
|
|
// type specifies if the connection was incoming
|
|
|
|
// or outgoing. If we ever saw this peer as connectable
|
|
|
|
// it will remain as connectable
|
|
|
|
unsigned type:4;
|
|
|
|
|
2008-04-01 19:38:19 +02:00
|
|
|
// the number of times we have allowed a fast
|
|
|
|
// reconnect for this peer.
|
2008-04-05 06:53:22 +02:00
|
|
|
unsigned fast_reconnects:4;
|
2007-10-12 03:12:30 +02:00
|
|
|
|
2008-04-01 19:38:19 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
// Hints encryption support of peer. Only effective
|
|
|
|
// for and when the outgoing encryption policy
|
|
|
|
// allows both encrypted and non encrypted
|
|
|
|
// connections (pe_settings::out_enc_policy
|
|
|
|
// == enabled). The initial state of this flag
|
|
|
|
// determines the initial connection attempt
|
|
|
|
// type (true = encrypted, false = standard).
|
|
|
|
// This will be toggled everytime either an
|
|
|
|
// encrypted or non-encrypted handshake fails.
|
|
|
|
bool pe_support:1;
|
|
|
|
#endif
|
2007-08-16 14:41:46 +02:00
|
|
|
// true if this peer currently is unchoked
|
|
|
|
// because of an optimistic unchoke.
|
|
|
|
// when the optimistic unchoke is moved to
|
|
|
|
// another peer, this peer will be choked
|
|
|
|
// if this is true
|
2008-04-01 19:38:19 +02:00
|
|
|
bool optimistically_unchoked:1;
|
2007-08-16 14:41:46 +02:00
|
|
|
|
2008-04-01 19:38:19 +02:00
|
|
|
// this is true if the peer is a seed
|
|
|
|
bool seed:1;
|
|
|
|
|
|
|
|
// if this is true, the peer has previously
|
|
|
|
// participated in a piece that failed the piece
|
|
|
|
// hash check. This will put the peer on parole
|
|
|
|
// and only request entire pieces. If a piece pass
|
|
|
|
// that was partially requested from this peer it
|
|
|
|
// will leave parole mode and continue download
|
|
|
|
// pieces as normal peers.
|
|
|
|
bool on_parole:1;
|
2003-12-01 06:01:40 +01:00
|
|
|
|
2008-04-01 19:38:19 +02:00
|
|
|
// is set to true if this peer has been banned
|
|
|
|
bool banned:1;
|
2003-12-01 06:01:40 +01:00
|
|
|
|
2008-04-01 19:38:19 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
// this is set to true when this peer as been
|
|
|
|
// pinged by the DHT
|
|
|
|
bool added_to_dht:1;
|
|
|
|
#endif
|
2007-05-25 21:42:10 +02:00
|
|
|
|
2008-04-01 19:38:19 +02:00
|
|
|
// if the peer is connected now, this
|
|
|
|
// will refer to a valid peer_connection
|
|
|
|
peer_connection* connection;
|
2007-05-25 21:42:10 +02:00
|
|
|
|
2003-12-01 06:01:40 +01:00
|
|
|
// this is the accumulated amount of
|
|
|
|
// uploaded and downloaded data to this
|
|
|
|
// peer. It only accounts for what was
|
|
|
|
// shared during the last connection to
|
|
|
|
// this peer. i.e. These are only updated
|
|
|
|
// when the connection is closed. For the
|
|
|
|
// total amount of upload and download
|
|
|
|
// we'll have to add thes figures with the
|
|
|
|
// statistics from the peer_connection.
|
2004-01-21 01:59:38 +01:00
|
|
|
size_type prev_amount_upload;
|
|
|
|
size_type prev_amount_download;
|
2003-12-01 06:01:40 +01:00
|
|
|
|
2008-04-01 19:38:19 +02:00
|
|
|
// the time when this peer was optimistically unchoked
|
|
|
|
// the last time.
|
|
|
|
libtorrent::ptime last_optimistically_unchoked;
|
2007-04-10 23:23:13 +02:00
|
|
|
|
2008-04-01 19:38:19 +02:00
|
|
|
// the time when the peer connected to us
|
|
|
|
// or disconnected if it isn't connected right now
|
|
|
|
libtorrent::ptime connected;
|
2003-12-01 06:01:40 +01:00
|
|
|
};
|
|
|
|
|
2007-08-16 14:41:46 +02:00
|
|
|
int num_peers() const { return m_peers.size(); }
|
2004-03-21 03:03:37 +01:00
|
|
|
|
2007-10-02 21:11:04 +02:00
|
|
|
typedef std::multimap<address, peer>::iterator iterator;
|
|
|
|
typedef std::multimap<address, peer>::const_iterator const_iterator;
|
2006-05-28 21:03:54 +02:00
|
|
|
iterator begin_peer() { return m_peers.begin(); }
|
|
|
|
iterator end_peer() { return m_peers.end(); }
|
2007-10-31 10:48:20 +01:00
|
|
|
const_iterator begin_peer() const { return m_peers.begin(); }
|
|
|
|
const_iterator end_peer() const { return m_peers.end(); }
|
2004-10-29 15:21:09 +02:00
|
|
|
|
2007-05-05 02:29:33 +02:00
|
|
|
bool connect_one_peer();
|
2007-08-16 14:41:46 +02:00
|
|
|
bool disconnect_one_peer();
|
2007-05-05 02:29:33 +02:00
|
|
|
|
2007-12-27 21:57:58 +01:00
|
|
|
bool has_peer(policy::peer const* p) const;
|
|
|
|
|
2008-04-01 19:38:19 +02:00
|
|
|
int num_seeds() const { return m_num_seeds; }
|
2008-03-29 19:47:24 +01:00
|
|
|
int num_connect_candidates() const { return m_num_connect_candidates; }
|
|
|
|
void recalculate_connect_candidates()
|
|
|
|
{
|
|
|
|
if (m_num_connect_candidates == 0)
|
|
|
|
m_num_connect_candidates = 1;
|
|
|
|
}
|
|
|
|
|
2008-04-24 09:49:23 +02:00
|
|
|
void erase_peer(iterator i);
|
|
|
|
|
2004-01-15 17:45:34 +01:00
|
|
|
private:
|
2008-03-29 19:47:24 +01:00
|
|
|
|
2008-04-24 09:49:23 +02:00
|
|
|
bool compare_peer(policy::peer const& lhs, policy::peer const& rhs
|
|
|
|
, address const& external_ip) const;
|
|
|
|
|
|
|
|
// since the peer list can grow too large
|
|
|
|
// to scan all of it, start at this iterator
|
|
|
|
iterator m_round_robin;
|
|
|
|
|
2007-04-12 12:21:55 +02:00
|
|
|
iterator find_disconnect_candidate();
|
|
|
|
iterator find_connect_candidate();
|
2004-01-14 17:18:53 +01:00
|
|
|
|
2008-03-29 19:47:24 +01:00
|
|
|
bool is_connect_candidate(peer const& p, bool finished);
|
|
|
|
|
2007-10-02 21:11:04 +02:00
|
|
|
std::multimap<address, peer> m_peers;
|
2003-12-01 06:01:40 +01:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
torrent* m_torrent;
|
|
|
|
|
2003-12-14 23:55:32 +01:00
|
|
|
// free download we have got that hasn't
|
|
|
|
// been distributed yet.
|
2004-01-25 19:18:36 +01:00
|
|
|
size_type m_available_free_upload;
|
2004-01-21 01:59:38 +01:00
|
|
|
|
2008-03-29 19:47:24 +01:00
|
|
|
// The number of peers in our peer list
|
|
|
|
// that are connect candidates. i.e. they're
|
|
|
|
// not already connected and they have not
|
|
|
|
// yet reached their max try count and they
|
|
|
|
// have the connectable state (we have a listen
|
|
|
|
// port for them).
|
|
|
|
int m_num_connect_candidates;
|
2008-04-01 19:38:19 +02:00
|
|
|
|
|
|
|
// the number of seeds in the peer list
|
|
|
|
int m_num_seeds;
|
2003-10-23 01:00:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_POLICY_HPP_INCLUDED
|
2005-11-01 19:30:39 +01:00
|
|
|
|