2006-11-14 01:08:16 +01:00
|
|
|
/*
|
|
|
|
|
2012-10-02 05:16:33 +02:00
|
|
|
Copyright (c) 2006-2012, Arvid Norberg
|
2006-11-14 01:08:16 +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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_EXTENSIONS_HPP_INCLUDED
|
|
|
|
#define TORRENT_EXTENSIONS_HPP_INCLUDED
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
2011-01-29 11:37:21 +01:00
|
|
|
#include <boost/weak_ptr.hpp>
|
2006-11-14 01:08:16 +01:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include "libtorrent/config.hpp"
|
|
|
|
#include "libtorrent/buffer.hpp"
|
2011-05-02 03:45:56 +02:00
|
|
|
#include "libtorrent/socket.hpp"
|
2013-08-04 01:31:43 +02:00
|
|
|
#include "libtorrent/error_code.hpp"
|
2006-11-14 01:08:16 +01:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
2011-01-29 11:37:21 +01:00
|
|
|
namespace aux { struct session_impl; }
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
struct peer_plugin;
|
|
|
|
class bt_peer_connection;
|
|
|
|
struct peer_request;
|
|
|
|
class peer_connection;
|
|
|
|
class entry;
|
2008-07-01 10:04:12 +02:00
|
|
|
struct lazy_entry;
|
2008-04-10 12:03:23 +02:00
|
|
|
struct disk_buffer_holder;
|
2008-05-28 04:35:02 +02:00
|
|
|
struct bitfield;
|
2011-01-29 11:37:21 +01:00
|
|
|
class alert;
|
2011-02-07 17:50:57 +01:00
|
|
|
struct torrent_plugin;
|
2011-02-08 07:46:53 +01:00
|
|
|
class torrent;
|
2011-01-29 11:37:21 +01:00
|
|
|
|
|
|
|
struct TORRENT_EXPORT plugin
|
|
|
|
{
|
2013-09-01 19:34:05 +02:00
|
|
|
// hidden
|
2011-01-29 11:37:21 +01:00
|
|
|
virtual ~plugin() {}
|
|
|
|
|
2013-09-01 19:34:05 +02:00
|
|
|
// this is called by the session every time a new torrent is added.
|
|
|
|
// The ``torrent*`` points to the internal torrent object created
|
|
|
|
// for the new torrent. The ``void*`` is the userdata pointer as
|
|
|
|
// passed in via add_torrent_params.
|
|
|
|
//
|
|
|
|
// If the plugin returns a torrent_plugin instance, it will be added
|
|
|
|
// to the new torrent. Otherwise, return an empty shared_ptr to a
|
|
|
|
// torrent_plugin (the default).
|
2013-05-10 06:35:23 +02:00
|
|
|
virtual boost::shared_ptr<torrent_plugin> new_torrent(torrent*, void*)
|
2011-01-29 11:37:21 +01:00
|
|
|
{ return boost::shared_ptr<torrent_plugin>(); }
|
|
|
|
|
|
|
|
// called when plugin is added to a session
|
2013-05-10 06:35:23 +02:00
|
|
|
virtual void added(boost::weak_ptr<aux::session_impl>) {}
|
2011-01-29 11:37:21 +01:00
|
|
|
|
|
|
|
// called when an alert is posted
|
|
|
|
// alerts that are filtered are not
|
|
|
|
// posted
|
2013-05-10 06:35:23 +02:00
|
|
|
virtual void on_alert(alert const*) {}
|
2011-01-29 11:37:21 +01:00
|
|
|
|
|
|
|
// called once per second
|
|
|
|
virtual void on_tick() {}
|
|
|
|
|
|
|
|
// called when saving settings state
|
2013-05-10 06:35:23 +02:00
|
|
|
virtual void save_state(entry&) const {}
|
2011-01-29 11:37:21 +01:00
|
|
|
|
|
|
|
// called when loading settings state
|
2013-05-10 06:35:23 +02:00
|
|
|
virtual void load_state(lazy_entry const&) {}
|
2011-01-29 11:37:21 +01:00
|
|
|
};
|
2006-11-14 01:08:16 +01:00
|
|
|
|
2013-09-01 19:34:05 +02:00
|
|
|
// Torrent plugins are associated with a single torrent and have a number
|
|
|
|
// of functions called at certain events. Many of its functions have the
|
|
|
|
// ability to change or override the default libtorrent behavior.
|
2006-11-14 01:08:16 +01:00
|
|
|
struct TORRENT_EXPORT torrent_plugin
|
|
|
|
{
|
2013-09-01 19:34:05 +02:00
|
|
|
// hidden
|
2006-11-14 01:08:16 +01:00
|
|
|
virtual ~torrent_plugin() {}
|
2013-09-01 19:34:05 +02:00
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
// throwing an exception closes the connection
|
|
|
|
// returning a 0 pointer is valid and will not add
|
|
|
|
// the peer_plugin to the peer_connection
|
|
|
|
virtual boost::shared_ptr<peer_plugin> new_connection(peer_connection*)
|
|
|
|
{ return boost::shared_ptr<peer_plugin>(); }
|
|
|
|
|
2013-08-17 09:01:03 +02:00
|
|
|
// called when a piece passes or fails the hash check.
|
|
|
|
// the argument is the piece index.
|
2013-05-10 06:35:23 +02:00
|
|
|
virtual void on_piece_pass(int /*index*/) {}
|
|
|
|
virtual void on_piece_failed(int /*index*/) {}
|
2006-11-14 01:08:16 +01:00
|
|
|
|
|
|
|
// called aproximately once every second
|
|
|
|
virtual void tick() {}
|
|
|
|
|
|
|
|
// if true is returned, it means the handler handled the event,
|
|
|
|
// and no other plugins will have their handlers called, and the
|
|
|
|
// default behavior will be skipped
|
|
|
|
virtual bool on_pause() { return false; }
|
2011-01-29 11:37:21 +01:00
|
|
|
virtual bool on_resume() { return false; }
|
2007-03-29 00:59:35 +02:00
|
|
|
|
|
|
|
// this is called when the initial checking of
|
|
|
|
// files is completed.
|
|
|
|
virtual void on_files_checked() {}
|
2011-01-29 11:37:21 +01:00
|
|
|
|
|
|
|
// called when the torrent changes state
|
|
|
|
// the state is one of torrent_status::state_t
|
|
|
|
// enum members
|
2013-05-10 06:35:23 +02:00
|
|
|
virtual void on_state(int /*s*/) {}
|
2011-05-02 03:45:56 +02:00
|
|
|
|
|
|
|
// called every time policy::add_peer is called
|
|
|
|
// src is a bitmask of which sources this peer
|
|
|
|
// has been seen from. flags is a bitmask of:
|
|
|
|
|
|
|
|
enum flags_t {
|
|
|
|
// this is the first time we see this peer
|
|
|
|
first_time = 1,
|
|
|
|
// this peer was not added because it was
|
|
|
|
// filtered by the IP filter
|
|
|
|
filtered = 2
|
|
|
|
};
|
|
|
|
|
2013-09-01 19:34:05 +02:00
|
|
|
// called every time a new peer is added to the peer list.
|
|
|
|
// This is before the peer is connected to. For ``flags``, see
|
|
|
|
// torrent_plugin::flags_t. The ``source`` argument refers to
|
|
|
|
// the source where we learned about this peer from. It's a
|
|
|
|
// bitmask, because many sources may have told us about the same
|
|
|
|
// peer. For peer source flags, see peer_info::peer_source_flags.
|
2013-05-10 06:35:23 +02:00
|
|
|
virtual void on_add_peer(tcp::endpoint const&,
|
|
|
|
int /*src*/, int /*flags*/) {}
|
2006-11-14 01:08:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TORRENT_EXPORT peer_plugin
|
|
|
|
{
|
2013-09-01 19:34:05 +02:00
|
|
|
// hidden
|
2006-11-14 01:08:16 +01:00
|
|
|
virtual ~peer_plugin() {}
|
|
|
|
|
2013-08-17 09:01:03 +02:00
|
|
|
// This function is expected to return the name of
|
|
|
|
// the plugin.
|
2010-11-29 02:33:05 +01:00
|
|
|
virtual char const* type() const { return ""; }
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
// can add entries to the extension handshake
|
2007-04-02 22:00:24 +02:00
|
|
|
// this is not called for web seeds
|
2006-11-14 01:08:16 +01:00
|
|
|
virtual void add_handshake(entry&) {}
|
|
|
|
|
2013-08-04 01:31:43 +02:00
|
|
|
// called when the peer is being disconnected.
|
|
|
|
virtual void on_disconnect(error_code const& ec) {}
|
|
|
|
|
|
|
|
// called when the peer is successfully connected. Note that
|
|
|
|
// incoming connections will have been connected by the time
|
|
|
|
// the peer plugin is attached to it, and won't have this hook
|
|
|
|
// called.
|
|
|
|
virtual void on_connected() {}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
// throwing an exception from any of the handlers (except add_handshake)
|
|
|
|
// closes the connection
|
|
|
|
|
|
|
|
// this is called when the initial BT handshake is received. Returning false
|
|
|
|
// means that the other end doesn't support this extension and will remove
|
|
|
|
// it from the list of plugins.
|
2007-04-02 22:00:24 +02:00
|
|
|
// this is not called for web seeds
|
2013-05-10 06:35:23 +02:00
|
|
|
virtual bool on_handshake(char const* /*reserved_bits*/) { return true; }
|
2006-11-14 01:08:16 +01:00
|
|
|
|
|
|
|
// called when the extension handshake from the other end is received
|
|
|
|
// if this returns false, it means that this extension isn't
|
|
|
|
// supported by this peer. It will result in this peer_plugin
|
|
|
|
// being removed from the peer_connection and destructed.
|
2007-04-02 22:00:24 +02:00
|
|
|
// this is not called for web seeds
|
2013-05-10 06:35:23 +02:00
|
|
|
virtual bool on_extension_handshake(lazy_entry const&) { return true; }
|
2006-11-14 01:08:16 +01:00
|
|
|
|
|
|
|
// returning true from any of the message handlers
|
|
|
|
// indicates that the plugin has handeled the message.
|
|
|
|
// it will break the plugin chain traversing and not let
|
|
|
|
// anyone else handle the message, including the default
|
|
|
|
// handler.
|
2013-08-04 01:31:43 +02:00
|
|
|
virtual bool on_choke() { return false; }
|
|
|
|
virtual bool on_unchoke() { return false; }
|
|
|
|
virtual bool on_interested() { return false; }
|
|
|
|
virtual bool on_not_interested() { return false; }
|
|
|
|
virtual bool on_have(int /*index*/) { return false; }
|
|
|
|
virtual bool on_dont_have(int /*index*/) { return false; }
|
|
|
|
virtual bool on_bitfield(bitfield const& /*bitfield*/) { return false; }
|
|
|
|
virtual bool on_have_all() { return false; }
|
|
|
|
virtual bool on_have_none() { return false; }
|
|
|
|
virtual bool on_allowed_fast(int /*index*/) { return false; }
|
|
|
|
virtual bool on_request(peer_request const&) { return false; }
|
|
|
|
virtual bool on_piece(peer_request const& /*piece*/, disk_buffer_holder& /*data*/) { return false; }
|
|
|
|
virtual bool on_cancel(peer_request const&) { return false; }
|
|
|
|
virtual bool on_reject(peer_request const&) { return false; }
|
|
|
|
virtual bool on_suggest(int /*index*/) { return false; }
|
2007-09-14 05:38:38 +02:00
|
|
|
|
2013-08-09 09:55:12 +02:00
|
|
|
// called when libtorrent think this peer should be disconnected.
|
|
|
|
// if the plugin returns false, the peer will not be disconnected.
|
|
|
|
virtual bool can_disconnect(error_code const& ec) { return true; }
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
// called when an extended message is received. If returning true,
|
|
|
|
// the message is not processed by any other plugin and if false
|
|
|
|
// is returned the next plugin in the chain will receive it to
|
|
|
|
// be able to handle it
|
2007-04-02 22:00:24 +02:00
|
|
|
// this is not called for web seeds
|
2013-05-10 06:35:23 +02:00
|
|
|
virtual bool on_extended(int /*length*/, int /*msg*/,
|
|
|
|
buffer::const_interval /*body*/)
|
2006-11-14 01:08:16 +01:00
|
|
|
{ return false; }
|
|
|
|
|
2007-04-02 22:00:24 +02:00
|
|
|
// this is not called for web seeds
|
2013-05-10 06:35:23 +02:00
|
|
|
virtual bool on_unknown_message(int /*length*/, int /*msg*/,
|
|
|
|
buffer::const_interval /*body*/)
|
2006-11-14 01:08:16 +01:00
|
|
|
{ return false; }
|
|
|
|
|
|
|
|
// called when a piece that this peer participated in either
|
|
|
|
// fails or passes the hash_check
|
2013-05-10 06:35:23 +02:00
|
|
|
virtual void on_piece_pass(int /*index*/) {}
|
|
|
|
virtual void on_piece_failed(int /*index*/) {}
|
2006-11-14 01:08:16 +01:00
|
|
|
|
|
|
|
// called aproximately once every second
|
|
|
|
virtual void tick() {}
|
|
|
|
|
|
|
|
// called each time a request message is to be sent. If true
|
|
|
|
// is returned, the original request message won't be sent and
|
|
|
|
// no other plugin will have this function called.
|
2013-05-10 06:35:23 +02:00
|
|
|
virtual bool write_request(peer_request const&) { return false; }
|
2006-11-14 01:08:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // TORRENT_EXTENSIONS_HPP_INCLUDED
|
|
|
|
|