2006-04-25 23:04:48 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2003-2018, Arvid Norberg
|
|
|
|
Copyright (c) 2007-2018, Arvid Norberg, Un Shyam
|
2006-04-25 23:04:48 +02: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_BT_PEER_CONNECTION_HPP_INCLUDED
|
|
|
|
#define TORRENT_BT_PEER_CONNECTION_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include <ctime>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2016-06-20 17:32:06 +02:00
|
|
|
#include <array>
|
|
|
|
#include <cstdint>
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
#include "libtorrent/debug.hpp"
|
|
|
|
#include "libtorrent/buffer.hpp"
|
|
|
|
#include "libtorrent/peer_connection.hpp"
|
|
|
|
#include "libtorrent/socket.hpp"
|
|
|
|
#include "libtorrent/peer_id.hpp"
|
|
|
|
#include "libtorrent/stat.hpp"
|
|
|
|
#include "libtorrent/alert.hpp"
|
|
|
|
#include "libtorrent/torrent.hpp"
|
|
|
|
#include "libtorrent/peer_request.hpp"
|
|
|
|
#include "libtorrent/piece_block_progress.hpp"
|
|
|
|
#include "libtorrent/config.hpp"
|
2007-06-06 02:41:20 +02:00
|
|
|
#include "libtorrent/pe_crypto.hpp"
|
2017-04-19 02:02:20 +02:00
|
|
|
#include "libtorrent/io.hpp"
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2016-08-16 03:05:39 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
struct TORRENT_EXTRA_EXPORT ut_pex_peer_store
|
|
|
|
{
|
|
|
|
// stores all peers this peer is connected to. These lists
|
|
|
|
// are updated with each pex message and are limited in size
|
|
|
|
// to protect against malicious clients. These lists are also
|
|
|
|
// used for looking up which peer a peer that supports holepunch
|
|
|
|
// came from.
|
|
|
|
// these are vectors to save memory and keep the items close
|
|
|
|
// together for performance. Inserting and removing is relatively
|
|
|
|
// cheap since the lists' size is limited
|
|
|
|
using peers4_t = std::vector<std::pair<address_v4::bytes_type, std::uint16_t>>;
|
|
|
|
peers4_t m_peers;
|
|
|
|
using peers6_t = std::vector<std::pair<address_v6::bytes_type, std::uint16_t>>;
|
|
|
|
peers6_t m_peers6;
|
|
|
|
|
|
|
|
bool was_introduced_by(tcp::endpoint const& ep);
|
2016-08-17 20:30:24 +02:00
|
|
|
|
|
|
|
virtual ~ut_pex_peer_store() {}
|
2016-08-16 03:05:39 +02:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2012-03-19 00:31:04 +01:00
|
|
|
class TORRENT_EXTRA_EXPORT bt_peer_connection
|
2006-04-25 23:04:48 +02:00
|
|
|
: public peer_connection
|
|
|
|
{
|
|
|
|
friend class invariant_access;
|
|
|
|
public:
|
|
|
|
|
|
|
|
// this is the constructor where the we are the active part.
|
2016-06-13 13:47:16 +02:00
|
|
|
// The peer_connection should handshake and verify that the
|
2006-04-25 23:04:48 +02:00
|
|
|
// other end has the correct id
|
2018-01-23 23:06:12 +01:00
|
|
|
explicit bt_peer_connection(peer_connection_args const& pack);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2017-09-12 23:10:11 +02:00
|
|
|
void start() override;
|
2008-03-31 06:46:24 +02:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
enum
|
|
|
|
{
|
2013-02-23 07:40:27 +01:00
|
|
|
// pex_msg = 1,
|
|
|
|
// metadata_msg = 2,
|
|
|
|
upload_only_msg = 3,
|
|
|
|
holepunch_msg = 4,
|
|
|
|
// recommend_msg = 5,
|
|
|
|
// comment_msg = 6,
|
|
|
|
dont_have_msg = 7,
|
2014-07-05 01:40:31 +02:00
|
|
|
share_mode_msg = 8
|
2010-11-29 02:33:05 +01:00
|
|
|
};
|
2009-11-24 19:49:59 +01:00
|
|
|
|
2017-06-01 10:12:02 +02:00
|
|
|
~bt_peer_connection() override;
|
2016-01-31 03:33:47 +01:00
|
|
|
|
2018-08-18 10:36:38 +02:00
|
|
|
peer_id our_pid() const override { return m_our_peer_id; }
|
|
|
|
|
2018-07-29 00:22:10 +02:00
|
|
|
#if !defined TORRENT_DISABLE_ENCRYPTION
|
2007-06-06 02:41:20 +02:00
|
|
|
bool supports_encryption() const
|
|
|
|
{ return m_encrypted; }
|
2012-03-02 09:52:54 +01:00
|
|
|
bool rc4_encrypted() const
|
|
|
|
{ return m_rc4_encrypted; }
|
2014-11-23 07:14:47 +01:00
|
|
|
|
2016-08-17 20:30:24 +02:00
|
|
|
void switch_send_crypto(std::shared_ptr<crypto_plugin> crypto);
|
|
|
|
void switch_recv_crypto(std::shared_ptr<crypto_plugin> crypto);
|
2007-06-06 02:41:20 +02:00
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2017-09-12 23:10:11 +02:00
|
|
|
connection_type type() const override
|
2016-09-24 19:47:17 +02:00
|
|
|
{ return connection_type::bittorrent; }
|
2009-11-02 21:43:38 +01:00
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
enum message_type
|
|
|
|
{
|
2017-04-19 02:02:20 +02:00
|
|
|
// standard messages
|
2006-11-14 01:08:16 +01:00
|
|
|
msg_choke = 0,
|
|
|
|
msg_unchoke,
|
|
|
|
msg_interested,
|
|
|
|
msg_not_interested,
|
|
|
|
msg_have,
|
|
|
|
msg_bitfield,
|
|
|
|
msg_request,
|
|
|
|
msg_piece,
|
|
|
|
msg_cancel,
|
2007-08-14 19:47:48 +02:00
|
|
|
// DHT extension
|
2006-11-14 01:08:16 +01:00
|
|
|
msg_dht_port,
|
2007-08-14 19:47:48 +02:00
|
|
|
// FAST extension
|
|
|
|
msg_suggest_piece = 0xd,
|
|
|
|
msg_have_all,
|
|
|
|
msg_have_none,
|
|
|
|
msg_reject_request,
|
|
|
|
msg_allowed_fast,
|
2009-03-13 07:09:39 +01:00
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
// extension protocol message
|
2006-11-14 01:08:16 +01:00
|
|
|
msg_extended = 20,
|
|
|
|
|
|
|
|
num_supported_messages
|
|
|
|
};
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
enum hp_message_t
|
|
|
|
{
|
|
|
|
// msg_types
|
|
|
|
hp_rendezvous = 0,
|
|
|
|
hp_connect = 1,
|
|
|
|
hp_failed = 2,
|
|
|
|
|
|
|
|
// error codes
|
|
|
|
hp_no_such_peer = 1,
|
|
|
|
hp_not_connected = 2,
|
|
|
|
hp_no_support = 3,
|
|
|
|
hp_no_self = 4
|
|
|
|
};
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// called from the main loop when this connection has any
|
|
|
|
// work to do.
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void on_sent(error_code const& error
|
2016-04-30 17:05:54 +02:00
|
|
|
, std::size_t bytes_transferred) override;
|
2008-05-03 18:05:42 +02:00
|
|
|
void on_receive(error_code const& error
|
2016-04-30 17:05:54 +02:00
|
|
|
, std::size_t bytes_transferred) override;
|
2014-11-23 07:14:47 +01:00
|
|
|
void on_receive_impl(std::size_t bytes_transferred);
|
|
|
|
|
2018-07-29 00:22:10 +02:00
|
|
|
#if !defined TORRENT_DISABLE_ENCRYPTION
|
2016-06-20 05:05:59 +02:00
|
|
|
// next_barrier, buffers-to-prepend
|
2016-07-28 04:31:08 +02:00
|
|
|
std::tuple<int, span<span<char const>>>
|
|
|
|
hit_send_barrier(span<span<char>> iovec) override;
|
2014-11-23 07:14:47 +01:00
|
|
|
#endif
|
2015-06-06 08:10:53 +02:00
|
|
|
|
2017-09-12 23:10:11 +02:00
|
|
|
void get_specific_peer_info(peer_info& p) const override;
|
|
|
|
bool in_handshake() const override;
|
2014-11-23 07:14:47 +01:00
|
|
|
bool packet_finished() const { return m_recv_buffer.packet_finished(); }
|
2007-01-29 08:39:33 +01:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
bool supports_holepunch() const { return m_holepunch_id != 0; }
|
2018-07-24 08:18:48 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
2016-08-17 20:30:24 +02:00
|
|
|
void set_ut_pex(std::weak_ptr<ut_pex_peer_store> ut_pex)
|
|
|
|
{ m_ut_pex = std::move(ut_pex); }
|
2016-08-16 03:05:39 +02:00
|
|
|
bool was_introduced_by(tcp::endpoint const& ep) const
|
2016-08-17 20:30:24 +02:00
|
|
|
{ auto p = m_ut_pex.lock(); return p && p->was_introduced_by(ep); }
|
2011-01-29 13:13:49 +01:00
|
|
|
#endif
|
2010-11-29 02:33:05 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
bool support_extensions() const { return m_supports_extensions; }
|
|
|
|
|
|
|
|
// the message handlers are called
|
|
|
|
// each time a recv() returns some new
|
|
|
|
// data, the last time it will be called
|
|
|
|
// is when the entire packet has been
|
|
|
|
// received, then it will no longer
|
|
|
|
// be called. i.e. most handlers need
|
|
|
|
// to check how much of the packet they
|
|
|
|
// have received before any processing
|
|
|
|
void on_choke(int received);
|
|
|
|
void on_unchoke(int received);
|
|
|
|
void on_interested(int received);
|
|
|
|
void on_not_interested(int received);
|
|
|
|
void on_have(int received);
|
|
|
|
void on_bitfield(int received);
|
|
|
|
void on_request(int received);
|
|
|
|
void on_piece(int received);
|
|
|
|
void on_cancel(int received);
|
2007-08-14 19:47:48 +02:00
|
|
|
|
|
|
|
// DHT extension
|
2006-04-25 23:04:48 +02:00
|
|
|
void on_dht_port(int received);
|
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
// FAST extension
|
|
|
|
void on_suggest_piece(int received);
|
|
|
|
void on_have_all(int received);
|
|
|
|
void on_have_none(int received);
|
|
|
|
void on_reject_request(int received);
|
|
|
|
void on_allowed_fast(int received);
|
2010-11-29 02:33:05 +01:00
|
|
|
void on_holepunch();
|
2007-08-14 19:47:48 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void on_extended(int received);
|
|
|
|
|
|
|
|
void on_extended_handshake();
|
|
|
|
|
|
|
|
// the following functions appends messages
|
|
|
|
// to the send buffer
|
2016-04-30 17:05:54 +02:00
|
|
|
void write_choke() override;
|
|
|
|
void write_unchoke() override;
|
|
|
|
void write_interested() override;
|
|
|
|
void write_not_interested() override;
|
|
|
|
void write_request(peer_request const& r) override;
|
|
|
|
void write_cancel(peer_request const& r) override;
|
|
|
|
void write_bitfield() override;
|
2016-12-22 16:42:33 +01:00
|
|
|
void write_have(piece_index_t index) override;
|
|
|
|
void write_dont_have(piece_index_t index) override;
|
2016-06-16 14:24:41 +02:00
|
|
|
void write_piece(peer_request const& r, disk_buffer_holder buffer) override;
|
2016-04-30 17:05:54 +02:00
|
|
|
void write_keepalive() override;
|
2016-06-08 03:45:48 +02:00
|
|
|
void write_handshake();
|
2017-09-21 18:57:34 +02:00
|
|
|
void write_upload_only(bool enabled) override;
|
2006-04-25 23:04:48 +02:00
|
|
|
void write_extensions();
|
2010-09-05 18:01:36 +02:00
|
|
|
void write_share_mode();
|
2011-01-29 13:13:49 +01:00
|
|
|
void write_holepunch_msg(int type, tcp::endpoint const& ep, int error);
|
2018-07-24 08:18:48 +02:00
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
// DHT extension
|
2006-04-25 23:04:48 +02:00
|
|
|
void write_dht_port(int listen_port);
|
2007-08-14 19:47:48 +02:00
|
|
|
|
|
|
|
// FAST extension
|
|
|
|
void write_have_all();
|
|
|
|
void write_have_none();
|
2016-04-30 17:05:54 +02:00
|
|
|
void write_reject_request(peer_request const&) override;
|
2016-12-22 16:42:33 +01:00
|
|
|
void write_allow_fast(piece_index_t piece) override;
|
|
|
|
void write_suggest(piece_index_t piece) override;
|
2016-06-08 03:45:48 +02:00
|
|
|
|
2016-04-30 17:05:54 +02:00
|
|
|
void on_connected() override;
|
|
|
|
void on_metadata() override;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2014-01-21 20:26:09 +01:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2006-04-25 23:04:48 +02:00
|
|
|
void check_invariant() const;
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point m_last_choke;
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
2017-04-19 02:02:20 +02:00
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
void send_message(message_type const type
|
|
|
|
, counters::stats_counter_t const counter
|
|
|
|
, std::uint32_t flags
|
|
|
|
, Args... args)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_sent_handshake);
|
|
|
|
TORRENT_ASSERT(m_sent_bitfield);
|
|
|
|
|
|
|
|
char msg[5 + sizeof...(Args) * 4]
|
|
|
|
= { 0,0,0,1 + sizeof...(Args) * 4, static_cast<char>(type) };
|
|
|
|
char* ptr = msg + 5;
|
|
|
|
TORRENT_UNUSED(ptr);
|
|
|
|
|
|
|
|
int tmp[] = {0, (detail::write_int32(args, ptr), 0)...};
|
|
|
|
TORRENT_UNUSED(tmp);
|
|
|
|
|
2017-04-23 06:37:29 +02:00
|
|
|
send_buffer(msg, flags);
|
2017-04-19 02:02:20 +02:00
|
|
|
|
|
|
|
stats_counters().inc_stats_counter(counter);
|
|
|
|
}
|
|
|
|
|
2016-11-29 14:11:13 +01:00
|
|
|
void write_dht_port();
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
bool dispatch_message(int received);
|
|
|
|
// returns the block currently being
|
|
|
|
// downloaded. And the progress of that
|
|
|
|
// block. If the peer isn't downloading
|
|
|
|
// a piece for the moment, the boost::optional
|
|
|
|
// will be invalid.
|
2016-09-04 18:31:02 +02:00
|
|
|
piece_block_progress downloading_piece_progress() const override;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2018-07-29 00:22:10 +02:00
|
|
|
#if !defined TORRENT_DISABLE_ENCRYPTION
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
// if (is_local()), we are 'a' otherwise 'b'
|
|
|
|
//
|
|
|
|
// 1. a -> b dhkey, pad
|
|
|
|
// 2. b -> a dhkey, pad
|
|
|
|
// 3. a -> b sync, payload
|
|
|
|
// 4. b -> a sync, payload
|
|
|
|
// 5. a -> b payload
|
|
|
|
|
|
|
|
void write_pe1_2_dhkey();
|
|
|
|
void write_pe3_sync();
|
|
|
|
void write_pe4_sync(int crypto_select);
|
|
|
|
|
2017-04-23 06:37:29 +02:00
|
|
|
void write_pe_vc_cryptofield(span<char> write_buf
|
2018-11-01 23:05:30 +01:00
|
|
|
, int crypto_field, int pad_size);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2014-11-23 07:14:47 +01:00
|
|
|
// helper to cut down on boilerplate
|
2016-07-26 08:15:31 +02:00
|
|
|
void rc4_decrypt(span<char> buf);
|
2013-11-10 12:05:11 +01:00
|
|
|
#endif
|
|
|
|
|
2016-12-29 02:47:18 +01:00
|
|
|
public:
|
2007-12-02 05:16:51 +01:00
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
// these functions encrypt the send buffer if m_rc4_encrypted
|
|
|
|
// is true, otherwise it passes the call to the
|
|
|
|
// peer_connection functions of the same names
|
2016-12-29 02:47:18 +01:00
|
|
|
template <typename Holder>
|
2017-08-28 11:37:53 +02:00
|
|
|
void append_const_send_buffer(Holder holder, int size)
|
2016-12-29 02:47:18 +01:00
|
|
|
{
|
2018-07-29 00:22:10 +02:00
|
|
|
#if !defined TORRENT_DISABLE_ENCRYPTION
|
2016-12-29 02:47:18 +01:00
|
|
|
if (!m_enc_handler.is_send_plaintext())
|
|
|
|
{
|
|
|
|
// if we're encrypting this buffer, we need to make a copy
|
|
|
|
// since we'll mutate it
|
2018-11-01 23:05:30 +01:00
|
|
|
buffer buf(size, {holder.data(), size});
|
2016-12-29 02:47:18 +01:00
|
|
|
append_send_buffer(std::move(buf), size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2017-08-28 11:37:53 +02:00
|
|
|
append_send_buffer(std::move(holder), size);
|
2016-12-29 02:47:18 +01:00
|
|
|
}
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-12-29 02:47:18 +01:00
|
|
|
private:
|
2007-12-02 05:16:51 +01:00
|
|
|
|
2016-08-30 02:29:27 +02:00
|
|
|
enum class state_t : std::uint8_t
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2018-07-29 00:22:10 +02:00
|
|
|
#if !defined TORRENT_DISABLE_ENCRYPTION
|
2018-07-24 08:18:48 +02:00
|
|
|
read_pe_dhkey,
|
2007-06-06 02:41:20 +02:00
|
|
|
read_pe_syncvc,
|
|
|
|
read_pe_synchash,
|
|
|
|
read_pe_skey_vc,
|
|
|
|
read_pe_cryptofield,
|
|
|
|
read_pe_pad,
|
|
|
|
read_pe_ia,
|
|
|
|
init_bt_handshake,
|
|
|
|
#endif
|
2018-07-24 08:18:48 +02:00
|
|
|
read_protocol_identifier,
|
2006-04-25 23:04:48 +02:00
|
|
|
read_info_hash,
|
|
|
|
read_peer_id,
|
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
// handshake complete
|
2006-04-25 23:04:48 +02:00
|
|
|
read_packet_size,
|
|
|
|
read_packet
|
|
|
|
};
|
2015-11-27 03:18:58 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// state of on_receive. one of the enums in state_t
|
2016-08-30 02:29:27 +02:00
|
|
|
state_t m_state = state_t::read_protocol_identifier;
|
2013-12-02 05:24:10 +01:00
|
|
|
|
|
|
|
// this is set to true if the handshake from
|
|
|
|
// the peer indicated that it supports the
|
|
|
|
// extension protocol
|
|
|
|
bool m_supports_extensions:1;
|
|
|
|
bool m_supports_dht_port:1;
|
|
|
|
bool m_supports_fast:1;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// this is set to true when we send the bitfield message.
|
|
|
|
// for magnet links we can't do that right away,
|
|
|
|
// since we don't know how many pieces there are in
|
|
|
|
// the torrent.
|
2013-12-02 05:24:10 +01:00
|
|
|
bool m_sent_bitfield:1;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// true if we're done sending the bittorrent handshake,
|
|
|
|
// and can send bittorrent messages
|
2013-12-02 05:24:10 +01:00
|
|
|
bool m_sent_handshake:1;
|
|
|
|
|
2016-06-09 14:02:41 +02:00
|
|
|
// set to true once we send the allowed-fast messages. This is
|
|
|
|
// only done once per connection
|
|
|
|
bool m_sent_allowed_fast:1;
|
|
|
|
|
2018-07-29 00:22:10 +02:00
|
|
|
#if !defined TORRENT_DISABLE_ENCRYPTION
|
2013-12-02 05:24:10 +01:00
|
|
|
// this is set to true after the encryption method has been
|
2016-01-02 17:24:07 +01:00
|
|
|
// successfully negotiated (either plaintext or rc4), to signal
|
2013-12-02 05:24:10 +01:00
|
|
|
// automatic encryption/decryption.
|
|
|
|
bool m_encrypted:1;
|
|
|
|
|
|
|
|
// true if rc4, false if plaintext
|
|
|
|
bool m_rc4_encrypted:1;
|
2014-11-23 07:14:47 +01:00
|
|
|
|
2017-06-01 10:12:02 +02:00
|
|
|
// this is a legitimate use of a shadow field
|
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
// macOS clang doesn't have -Wshadow-field
|
|
|
|
#pragma clang diagnostic ignored "-Wunknown-pragmas"
|
2017-09-22 00:49:10 +02:00
|
|
|
// Xcode 9 needs this
|
|
|
|
#pragma clang diagnostic ignored "-Wunknown-warning-option"
|
2017-06-01 10:12:02 +02:00
|
|
|
#pragma clang diagnostic ignored "-Wshadow-field"
|
|
|
|
#endif
|
2014-11-23 07:14:47 +01:00
|
|
|
crypto_receive_buffer m_recv_buffer;
|
2017-06-01 10:12:02 +02:00
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2013-12-20 07:38:51 +01:00
|
|
|
#endif
|
2013-12-02 05:24:10 +01:00
|
|
|
|
|
|
|
std::string m_client_version;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2014-04-23 01:37:21 +02:00
|
|
|
// the peer ID we advertise for ourself
|
2018-08-18 10:36:38 +02:00
|
|
|
peer_id const m_our_peer_id;
|
2014-04-23 01:37:21 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// this is a queue of ranges that describes
|
|
|
|
// where in the send buffer actual payload
|
|
|
|
// data is located. This is currently
|
|
|
|
// only used to be able to gather statistics
|
2016-01-02 17:24:07 +01:00
|
|
|
// separately on payload and protocol data.
|
2006-04-25 23:04:48 +02:00
|
|
|
struct range
|
|
|
|
{
|
|
|
|
range(int s, int l)
|
|
|
|
: start(s)
|
|
|
|
, length(l)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(s >= 0);
|
|
|
|
TORRENT_ASSERT(l > 0);
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
int start;
|
|
|
|
int length;
|
|
|
|
};
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
std::vector<range> m_payloads;
|
2010-01-15 17:45:42 +01:00
|
|
|
|
2018-07-29 00:22:10 +02:00
|
|
|
#if !defined TORRENT_DISABLE_ENCRYPTION
|
2007-06-06 02:41:20 +02:00
|
|
|
// initialized during write_pe1_2_dhkey, and destroyed on
|
2011-08-29 04:00:17 +02:00
|
|
|
// creation of m_enc_handler. Cannot reinitialize once
|
2007-06-06 02:41:20 +02:00
|
|
|
// initialized.
|
2016-06-05 20:07:24 +02:00
|
|
|
std::unique_ptr<dh_key_exchange> m_dh_key_exchange;
|
2016-01-22 05:32:20 +01:00
|
|
|
|
2014-11-23 07:14:47 +01:00
|
|
|
// used during an encrypted handshake then moved
|
|
|
|
// into m_enc_handler if rc4 encryption is negotiated
|
|
|
|
// otherwise it is destroyed when the handshake completes
|
2016-08-17 20:30:24 +02:00
|
|
|
std::shared_ptr<rc4_handler> m_rc4;
|
2014-11-23 07:14:47 +01:00
|
|
|
|
2011-08-29 04:00:17 +02:00
|
|
|
// if encryption is negotiated, this is used for
|
2014-11-23 07:14:47 +01:00
|
|
|
// encryption/decryption during the entire session.
|
|
|
|
encryption_handler m_enc_handler;
|
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
// (outgoing only) synchronize verification constant with
|
2011-06-18 18:58:36 +02:00
|
|
|
// remote peer, this will hold rc4_decrypt(vc). Destroyed
|
2007-06-06 02:41:20 +02:00
|
|
|
// after the sync step.
|
2016-09-01 15:43:53 +02:00
|
|
|
std::unique_ptr<char[]> m_sync_vc;
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
// (incoming only) synchronize hash with remote peer, holds
|
|
|
|
// the sync hash (hash("req1",secret)). Destroyed after the
|
|
|
|
// sync step.
|
2016-06-05 20:07:24 +02:00
|
|
|
std::unique_ptr<sha1_hash> m_sync_hash;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2013-12-02 05:24:10 +01:00
|
|
|
// used to disconnect peer if sync points are not found within
|
|
|
|
// the maximum number of bytes
|
2016-08-30 02:29:27 +02:00
|
|
|
int m_sync_bytes_read = 0;
|
2014-07-06 21:18:00 +02:00
|
|
|
#endif
|
2006-11-14 01:08:16 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// the message ID for upload only message
|
|
|
|
// 0 if not supported
|
2016-08-30 02:29:27 +02:00
|
|
|
std::uint8_t m_upload_only_id = 0;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// the message ID for holepunch messages
|
2016-08-30 02:29:27 +02:00
|
|
|
std::uint8_t m_holepunch_id = 0;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2013-12-02 05:24:10 +01:00
|
|
|
// the message ID for don't-have message
|
2016-08-30 02:29:27 +02:00
|
|
|
std::uint8_t m_dont_have_id = 0;
|
2013-12-02 05:24:10 +01:00
|
|
|
|
|
|
|
// the message ID for share mode message
|
|
|
|
// 0 if not supported
|
2016-08-30 02:29:27 +02:00
|
|
|
std::uint8_t m_share_mode_id = 0;
|
2013-12-02 05:24:10 +01:00
|
|
|
|
2018-07-24 08:18:48 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
2016-08-17 20:30:24 +02:00
|
|
|
std::weak_ptr<ut_pex_peer_store> m_ut_pex;
|
2018-07-24 08:18:48 +02:00
|
|
|
#endif
|
2016-08-16 03:05:39 +02:00
|
|
|
|
2016-08-17 20:30:24 +02:00
|
|
|
std::array<char, 8> m_reserved_bits;
|
2006-04-25 23:04:48 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_BT_PEER_CONNECTION_HPP_INCLUDED
|