2006-04-25 23:04:48 +02:00
|
|
|
/*
|
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
Copyright (c) 2003 - 2006, Arvid Norberg
|
|
|
|
Copyright (c) 2007, 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-03-17 18:15:16 +01:00
|
|
|
#include "libtorrent/pch.hpp"
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
#include <vector>
|
2009-11-28 23:41:21 +01:00
|
|
|
#include <boost/limits.hpp>
|
2006-04-25 23:04:48 +02:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
|
|
|
#include "libtorrent/bt_peer_connection.hpp"
|
|
|
|
#include "libtorrent/session.hpp"
|
|
|
|
#include "libtorrent/identify_client.hpp"
|
|
|
|
#include "libtorrent/entry.hpp"
|
|
|
|
#include "libtorrent/bencode.hpp"
|
|
|
|
#include "libtorrent/alert_types.hpp"
|
|
|
|
#include "libtorrent/invariant_check.hpp"
|
|
|
|
#include "libtorrent/io.hpp"
|
|
|
|
#include "libtorrent/version.hpp"
|
2006-11-14 01:08:16 +01:00
|
|
|
#include "libtorrent/extensions.hpp"
|
2006-10-11 16:02:21 +02:00
|
|
|
#include "libtorrent/aux_/session_impl.hpp"
|
2008-12-26 08:00:21 +01:00
|
|
|
#include "libtorrent/broadcast_socket.hpp"
|
2009-06-23 03:53:47 +02:00
|
|
|
#include "libtorrent/escape_string.hpp"
|
2009-11-26 06:45:43 +01:00
|
|
|
#include "libtorrent/peer_info.hpp"
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
#include "libtorrent/pe_crypto.hpp"
|
|
|
|
#include "libtorrent/hasher.hpp"
|
|
|
|
#endif
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
using boost::bind;
|
|
|
|
using boost::shared_ptr;
|
2006-10-11 16:02:21 +02:00
|
|
|
using libtorrent::aux::session_impl;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
const bt_peer_connection::message_handler
|
|
|
|
bt_peer_connection::m_message_handler[] =
|
|
|
|
{
|
|
|
|
&bt_peer_connection::on_choke,
|
|
|
|
&bt_peer_connection::on_unchoke,
|
|
|
|
&bt_peer_connection::on_interested,
|
|
|
|
&bt_peer_connection::on_not_interested,
|
|
|
|
&bt_peer_connection::on_have,
|
|
|
|
&bt_peer_connection::on_bitfield,
|
|
|
|
&bt_peer_connection::on_request,
|
|
|
|
&bt_peer_connection::on_piece,
|
|
|
|
&bt_peer_connection::on_cancel,
|
|
|
|
&bt_peer_connection::on_dht_port,
|
2007-08-14 19:47:48 +02:00
|
|
|
0, 0, 0,
|
|
|
|
// FAST extension messages
|
|
|
|
&bt_peer_connection::on_suggest_piece,
|
|
|
|
&bt_peer_connection::on_have_all,
|
|
|
|
&bt_peer_connection::on_have_none,
|
|
|
|
&bt_peer_connection::on_reject_request,
|
|
|
|
&bt_peer_connection::on_allowed_fast,
|
|
|
|
0, 0,
|
2006-04-25 23:04:48 +02:00
|
|
|
&bt_peer_connection::on_extended
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
bt_peer_connection::bt_peer_connection(
|
2006-10-11 16:02:21 +02:00
|
|
|
session_impl& ses
|
2006-04-25 23:04:48 +02:00
|
|
|
, boost::weak_ptr<torrent> tor
|
2007-04-23 23:36:21 +02:00
|
|
|
, shared_ptr<socket_type> s
|
2007-04-10 23:23:13 +02:00
|
|
|
, tcp::endpoint const& remote
|
|
|
|
, policy::peer* peerinfo)
|
|
|
|
: peer_connection(ses, tor, s, remote
|
2007-04-25 20:26:35 +02:00
|
|
|
, peerinfo)
|
2007-06-06 02:41:20 +02:00
|
|
|
, m_state(read_protocol_identifier)
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
2009-11-24 19:49:59 +01:00
|
|
|
, m_upload_only_id(0)
|
2006-04-25 23:04:48 +02:00
|
|
|
, m_supports_extensions(false)
|
2006-11-14 01:08:16 +01:00
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
, m_supports_dht_port(false)
|
2007-08-14 19:47:48 +02:00
|
|
|
, m_supports_fast(false)
|
2007-06-06 02:41:20 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
, m_encrypted(false)
|
|
|
|
, m_rc4_encrypted(false)
|
|
|
|
, m_sync_bytes_read(0)
|
|
|
|
, m_enc_send_buffer(0, 0)
|
|
|
|
#endif
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2006-11-14 01:08:16 +01:00
|
|
|
, m_sent_bitfield(false)
|
2006-04-25 23:04:48 +02:00
|
|
|
, m_in_constructor(true)
|
2007-07-09 06:22:38 +02:00
|
|
|
, m_sent_handshake(false)
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << "*** bt_peer_connection\n";
|
|
|
|
#endif
|
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2006-04-25 23:04:48 +02:00
|
|
|
m_in_constructor = false;
|
2009-01-28 05:20:05 +01:00
|
|
|
m_encrypted_bytes = 0;
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_peer_connection::bt_peer_connection(
|
2006-10-11 16:02:21 +02:00
|
|
|
session_impl& ses
|
2007-04-23 23:36:21 +02:00
|
|
|
, boost::shared_ptr<socket_type> s
|
2008-05-12 12:10:39 +02:00
|
|
|
, tcp::endpoint const& remote
|
2007-04-10 23:23:13 +02:00
|
|
|
, policy::peer* peerinfo)
|
2008-05-12 12:10:39 +02:00
|
|
|
: peer_connection(ses, s, remote, peerinfo)
|
2007-06-06 02:41:20 +02:00
|
|
|
, m_state(read_protocol_identifier)
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
2006-04-25 23:04:48 +02:00
|
|
|
, m_supports_extensions(false)
|
2006-11-14 01:08:16 +01:00
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
, m_supports_dht_port(false)
|
2007-08-14 19:47:48 +02:00
|
|
|
, m_supports_fast(false)
|
2007-06-06 02:41:20 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
, m_encrypted(false)
|
|
|
|
, m_rc4_encrypted(false)
|
|
|
|
, m_sync_bytes_read(0)
|
|
|
|
, m_enc_send_buffer(0, 0)
|
|
|
|
#endif
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2006-11-14 01:08:16 +01:00
|
|
|
, m_sent_bitfield(false)
|
2006-04-25 23:04:48 +02:00
|
|
|
, m_in_constructor(true)
|
2007-07-09 06:22:38 +02:00
|
|
|
, m_sent_handshake(false)
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
|
|
|
{
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// we are not attached to any torrent yet.
|
|
|
|
// we have to wait for the handshake to see
|
|
|
|
// which torrent the connector want's to connect to
|
|
|
|
|
2007-01-10 16:02:25 +01:00
|
|
|
|
|
|
|
// upload bandwidth will only be given to connections
|
|
|
|
// that are part of a torrent. Since this is an incoming
|
|
|
|
// connection, we have to give it some initial bandwidth
|
|
|
|
// to send the handshake.
|
2007-06-06 02:41:20 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
2009-04-26 02:21:59 +02:00
|
|
|
m_quota[download_channel] = 2048;
|
|
|
|
m_quota[upload_channel] = 2048;
|
2007-06-06 02:41:20 +02:00
|
|
|
#else
|
2009-04-26 02:21:59 +02:00
|
|
|
m_quota[download_channel] = 80;
|
|
|
|
m_quota[upload_channel] = 80;
|
2007-06-06 02:41:20 +02:00
|
|
|
#endif
|
2007-01-10 16:02:25 +01:00
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2008-03-31 06:46:24 +02:00
|
|
|
m_in_constructor = false;
|
2009-01-28 05:20:05 +01:00
|
|
|
m_encrypted_bytes = 0;
|
2008-03-31 06:46:24 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_peer_connection::start()
|
|
|
|
{
|
|
|
|
peer_connection::start();
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// start in the state where we are trying to read the
|
|
|
|
// handshake from the other side
|
2007-06-06 02:41:20 +02:00
|
|
|
reset_recv_buffer(20);
|
2006-04-25 23:04:48 +02:00
|
|
|
setup_receive();
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_peer_connection::~bt_peer_connection()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-07-09 06:22:38 +02:00
|
|
|
void bt_peer_connection::on_connected()
|
|
|
|
{
|
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
|
|
|
|
pe_settings::enc_policy const& out_enc_policy = m_ses.get_pe_settings().out_enc_policy;
|
|
|
|
|
|
|
|
if (out_enc_policy == pe_settings::forced)
|
|
|
|
{
|
|
|
|
write_pe1_2_dhkey();
|
2008-06-28 12:10:05 +02:00
|
|
|
if (is_disconnecting()) return;
|
2007-07-09 06:22:38 +02:00
|
|
|
|
|
|
|
m_state = read_pe_dhkey;
|
|
|
|
reset_recv_buffer(dh_key_len);
|
|
|
|
setup_receive();
|
|
|
|
}
|
|
|
|
else if (out_enc_policy == pe_settings::enabled)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(peer_info_struct());
|
2007-07-09 06:22:38 +02:00
|
|
|
|
|
|
|
policy::peer* pi = peer_info_struct();
|
|
|
|
if (pi->pe_support == true)
|
|
|
|
{
|
|
|
|
// toggle encryption support flag, toggled back to
|
|
|
|
// true if encrypted portion of the handshake
|
|
|
|
// completes correctly
|
|
|
|
pi->pe_support = false;
|
|
|
|
|
2007-10-04 11:46:12 +02:00
|
|
|
// if this fails, we need to reconnect
|
|
|
|
// fast.
|
2007-10-04 23:26:50 +02:00
|
|
|
fast_reconnect(true);
|
2007-10-04 11:46:12 +02:00
|
|
|
|
2007-07-09 06:22:38 +02:00
|
|
|
write_pe1_2_dhkey();
|
2008-06-28 12:10:05 +02:00
|
|
|
if (is_disconnecting()) return;
|
2007-07-09 06:22:38 +02:00
|
|
|
m_state = read_pe_dhkey;
|
|
|
|
reset_recv_buffer(dh_key_len);
|
|
|
|
setup_receive();
|
|
|
|
}
|
|
|
|
else // pi->pe_support == false
|
|
|
|
{
|
|
|
|
// toggled back to false if standard handshake
|
|
|
|
// completes correctly (without encryption)
|
|
|
|
pi->pe_support = true;
|
|
|
|
|
|
|
|
write_handshake();
|
|
|
|
reset_recv_buffer(20);
|
|
|
|
setup_receive();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (out_enc_policy == pe_settings::disabled)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
write_handshake();
|
|
|
|
|
|
|
|
// start in the state where we are trying to read the
|
|
|
|
// handshake from the other side
|
|
|
|
reset_recv_buffer(20);
|
|
|
|
setup_receive();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
void bt_peer_connection::on_metadata()
|
|
|
|
{
|
2008-08-29 19:21:56 +02:00
|
|
|
// connections that are still in the handshake
|
|
|
|
// will send their bitfield when the handshake
|
|
|
|
// is done
|
|
|
|
if (m_state < read_packet_size) return;
|
2006-11-14 01:08:16 +01:00
|
|
|
boost::shared_ptr<torrent> t = associated_torrent().lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2008-06-07 04:58:28 +02:00
|
|
|
write_bitfield();
|
2007-08-14 19:47:48 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
if (m_supports_dht_port && m_ses.m_dht)
|
|
|
|
write_dht_port(m_ses.get_dht_settings().service_port);
|
|
|
|
#endif
|
2006-11-14 01:08:16 +01:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void bt_peer_connection::write_dht_port(int listen_port)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
2007-07-09 06:22:38 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_sent_handshake && m_sent_bitfield);
|
2007-07-09 06:22:38 +02:00
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2007-04-10 09:52:58 +02:00
|
|
|
(*m_logger) << time_now_string()
|
2007-03-15 23:03:56 +01:00
|
|
|
<< " ==> DHT_PORT [ " << listen_port << " ]\n";
|
|
|
|
#endif
|
2007-09-29 18:14:03 +02:00
|
|
|
char msg[] = {0,0,0,3, msg_dht_port, 0, 0};
|
|
|
|
char* ptr = msg + 5;
|
|
|
|
detail::write_uint16(listen_port, ptr);
|
|
|
|
send_buffer(msg, sizeof(msg));
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
void bt_peer_connection::write_have_all()
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_sent_handshake && !m_sent_bitfield);
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-08-14 19:47:48 +02:00
|
|
|
m_sent_bitfield = true;
|
|
|
|
#endif
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << time_now_string()
|
|
|
|
<< " ==> HAVE_ALL\n";
|
|
|
|
#endif
|
2007-09-29 18:14:03 +02:00
|
|
|
char msg[] = {0,0,0,1, msg_have_all};
|
|
|
|
send_buffer(msg, sizeof(msg));
|
2007-08-14 19:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void bt_peer_connection::write_have_none()
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_sent_handshake && !m_sent_bitfield);
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-08-14 19:47:48 +02:00
|
|
|
m_sent_bitfield = true;
|
|
|
|
#endif
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << time_now_string()
|
|
|
|
<< " ==> HAVE_NONE\n";
|
|
|
|
#endif
|
2007-09-29 18:14:03 +02:00
|
|
|
char msg[] = {0,0,0,1, msg_have_none};
|
|
|
|
send_buffer(msg, sizeof(msg));
|
2007-08-14 19:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void bt_peer_connection::write_reject_request(peer_request const& r)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-01-02 04:18:29 +01:00
|
|
|
if (!m_supports_fast) return;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_sent_handshake && m_sent_bitfield);
|
|
|
|
TORRENT_ASSERT(associated_torrent().lock()->valid_metadata());
|
2007-08-14 19:47:48 +02:00
|
|
|
|
2007-09-29 18:14:03 +02:00
|
|
|
char msg[] = {0,0,0,13, msg_reject_request,0,0,0,0, 0,0,0,0, 0,0,0,0};
|
|
|
|
char* ptr = msg + 5;
|
|
|
|
detail::write_int32(r.piece, ptr); // index
|
|
|
|
detail::write_int32(r.start, ptr); // begin
|
|
|
|
detail::write_int32(r.length, ptr); // length
|
|
|
|
send_buffer(msg, sizeof(msg));
|
2007-08-14 19:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void bt_peer_connection::write_allow_fast(int piece)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2010-01-17 22:26:47 +01:00
|
|
|
if (!m_supports_fast) return;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_sent_handshake && m_sent_bitfield);
|
|
|
|
TORRENT_ASSERT(associated_torrent().lock()->valid_metadata());
|
2007-08-14 19:47:48 +02:00
|
|
|
|
2007-09-29 18:14:03 +02:00
|
|
|
char msg[] = {0,0,0,5, msg_allowed_fast, 0, 0, 0, 0};
|
|
|
|
char* ptr = msg + 5;
|
2007-08-14 19:47:48 +02:00
|
|
|
detail::write_int32(piece, ptr);
|
2007-09-29 18:14:03 +02:00
|
|
|
send_buffer(msg, sizeof(msg));
|
2007-08-14 19:47:48 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 17:45:42 +01:00
|
|
|
void bt_peer_connection::write_suggest(int piece)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2010-01-17 22:06:08 +01:00
|
|
|
if (!m_supports_fast) return;
|
|
|
|
|
2010-01-15 17:45:42 +01:00
|
|
|
TORRENT_ASSERT(m_sent_handshake && m_sent_bitfield);
|
|
|
|
TORRENT_ASSERT(associated_torrent().lock()->valid_metadata());
|
|
|
|
|
|
|
|
boost::shared_ptr<torrent> t = associated_torrent().lock();
|
|
|
|
TORRENT_ASSERT(t);
|
|
|
|
|
|
|
|
if (m_sent_suggested_pieces.empty())
|
|
|
|
m_sent_suggested_pieces.resize(t->torrent_file().num_pieces(), false);
|
|
|
|
|
|
|
|
if (m_sent_suggested_pieces[piece]) return;
|
|
|
|
m_sent_suggested_pieces.set_bit(piece);
|
|
|
|
|
|
|
|
char msg[] = {0,0,0,5, msg_suggest_piece, 0, 0, 0, 0};
|
|
|
|
char* ptr = msg + 5;
|
|
|
|
detail::write_int32(piece, ptr);
|
|
|
|
send_buffer(msg, sizeof(msg));
|
|
|
|
}
|
|
|
|
|
2007-05-25 21:42:10 +02:00
|
|
|
void bt_peer_connection::get_specific_peer_info(peer_info& p) const
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!associated_torrent().expired());
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
if (is_interesting()) p.flags |= peer_info::interesting;
|
|
|
|
if (is_choked()) p.flags |= peer_info::choked;
|
|
|
|
if (is_peer_interested()) p.flags |= peer_info::remote_interested;
|
|
|
|
if (has_peer_choked()) p.flags |= peer_info::remote_choked;
|
|
|
|
if (support_extensions()) p.flags |= peer_info::supports_extensions;
|
|
|
|
if (is_local()) p.flags |= peer_info::local_connection;
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
if (m_encrypted)
|
|
|
|
{
|
|
|
|
m_rc4_encrypted ?
|
|
|
|
p.flags |= peer_info::rc4_encrypted :
|
|
|
|
p.flags |= peer_info::plaintext_encrypted;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!is_connecting() && in_handshake())
|
2006-04-25 23:04:48 +02:00
|
|
|
p.flags |= peer_info::handshake;
|
|
|
|
if (is_connecting() && !is_queued()) p.flags |= peer_info::connecting;
|
|
|
|
if (is_queued()) p.flags |= peer_info::queued;
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
p.client = m_client_version;
|
|
|
|
p.connection_type = peer_info::standard_bittorrent;
|
2007-04-10 23:23:13 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2007-01-29 08:39:33 +01:00
|
|
|
|
|
|
|
bool bt_peer_connection::in_handshake() const
|
|
|
|
{
|
|
|
|
return m_state < read_packet_size;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
|
|
|
|
void bt_peer_connection::write_pe1_2_dhkey()
|
|
|
|
{
|
2007-06-07 12:18:13 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_encrypted);
|
|
|
|
TORRENT_ASSERT(!m_rc4_encrypted);
|
2008-06-28 12:10:05 +02:00
|
|
|
TORRENT_ASSERT(!m_dh_key_exchange.get());
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_sent_handshake);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
if (is_local())
|
|
|
|
(*m_logger) << " initiating encrypted handshake\n";
|
|
|
|
#endif
|
|
|
|
|
2008-06-28 12:10:05 +02:00
|
|
|
m_dh_key_exchange.reset(new (std::nothrow) dh_key_exchange);
|
|
|
|
if (!m_dh_key_exchange || !m_dh_key_exchange->good())
|
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::no_memory);
|
2008-06-28 12:10:05 +02:00
|
|
|
return;
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
int pad_size = std::rand() % 512;
|
2007-06-18 19:12:42 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << " pad size: " << pad_size << "\n";
|
|
|
|
#endif
|
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
buffer::interval send_buf = allocate_send_buffer(dh_key_len + pad_size);
|
2008-06-28 12:10:05 +02:00
|
|
|
if (send_buf.begin == 0)
|
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::no_memory);
|
2008-06-28 12:10:05 +02:00
|
|
|
return;
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2008-06-28 12:10:05 +02:00
|
|
|
std::copy(m_dh_key_exchange->get_local_key(),
|
|
|
|
m_dh_key_exchange->get_local_key() + dh_key_len,
|
|
|
|
send_buf.begin);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
std::generate(send_buf.begin + dh_key_len, send_buf.end, std::rand);
|
2009-01-28 05:20:05 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
|
|
|
m_encrypted_bytes += send_buf.left();
|
|
|
|
TORRENT_ASSERT(m_encrypted_bytes <= send_buffer_size());
|
|
|
|
#endif
|
2007-06-06 02:41:20 +02:00
|
|
|
setup_send();
|
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << " sent DH key\n";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_peer_connection::write_pe3_sync()
|
|
|
|
{
|
2007-06-07 12:18:13 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_encrypted);
|
|
|
|
TORRENT_ASSERT(!m_rc4_encrypted);
|
|
|
|
TORRENT_ASSERT(is_local());
|
|
|
|
TORRENT_ASSERT(!m_sent_handshake);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
boost::shared_ptr<torrent> t = associated_torrent().lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
hasher h;
|
|
|
|
sha1_hash const& info_hash = t->torrent_file().info_hash();
|
2008-06-28 12:10:05 +02:00
|
|
|
char const* const secret = m_dh_key_exchange->get_secret();
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2008-01-31 07:34:43 +01:00
|
|
|
int pad_size = rand() % 512;
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2009-02-04 06:51:00 +01:00
|
|
|
TORRENT_ASSERT(send_buffer_size() == m_encrypted_bytes);
|
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
// synchash,skeyhash,vc,crypto_provide,len(pad),pad,len(ia)
|
|
|
|
buffer::interval send_buf =
|
2008-01-31 07:34:43 +01:00
|
|
|
allocate_send_buffer(20 + 20 + 8 + 4 + 2 + pad_size + 2);
|
2008-02-05 06:51:05 +01:00
|
|
|
if (send_buf.begin == 0) return; // out of memory
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
// sync hash (hash('req1',S))
|
|
|
|
h.reset();
|
|
|
|
h.update("req1",4);
|
|
|
|
h.update(secret, dh_key_len);
|
|
|
|
sha1_hash sync_hash = h.final();
|
|
|
|
|
2008-01-31 07:34:43 +01:00
|
|
|
std::copy(sync_hash.begin(), sync_hash.end(), send_buf.begin);
|
2007-06-06 02:41:20 +02:00
|
|
|
send_buf.begin += 20;
|
|
|
|
|
|
|
|
// stream key obfuscated hash [ hash('req2',SKEY) xor hash('req3',S) ]
|
|
|
|
h.reset();
|
|
|
|
h.update("req2",4);
|
|
|
|
h.update((const char*)info_hash.begin(), 20);
|
|
|
|
sha1_hash streamkey_hash = h.final();
|
|
|
|
|
|
|
|
h.reset();
|
|
|
|
h.update("req3",4);
|
|
|
|
h.update(secret, dh_key_len);
|
|
|
|
sha1_hash obfsc_hash = h.final();
|
|
|
|
obfsc_hash ^= streamkey_hash;
|
|
|
|
|
2008-01-31 07:34:43 +01:00
|
|
|
std::copy(obfsc_hash.begin(), obfsc_hash.end(), send_buf.begin);
|
2007-06-06 02:41:20 +02:00
|
|
|
send_buf.begin += 20;
|
|
|
|
|
|
|
|
// Discard DH key exchange data, setup RC4 keys
|
|
|
|
init_pe_RC4_handler(secret, info_hash);
|
2008-06-28 12:10:05 +02:00
|
|
|
m_dh_key_exchange.reset(); // secret should be invalid at this point
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
// write the verification constant and crypto field
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(send_buf.left() == 8 + 4 + 2 + pad_size + 2);
|
2007-06-06 02:41:20 +02:00
|
|
|
int encrypt_size = send_buf.left();
|
|
|
|
|
|
|
|
int crypto_provide = 0;
|
|
|
|
pe_settings::enc_level const& allowed_enc_level = m_ses.get_pe_settings().allowed_enc_level;
|
|
|
|
|
|
|
|
if (allowed_enc_level == pe_settings::both)
|
|
|
|
crypto_provide = 0x03;
|
|
|
|
else if (allowed_enc_level == pe_settings::rc4)
|
|
|
|
crypto_provide = 0x02;
|
|
|
|
else if (allowed_enc_level == pe_settings::plaintext)
|
|
|
|
crypto_provide = 0x01;
|
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << " crypto provide : [ ";
|
|
|
|
if (allowed_enc_level == pe_settings::both)
|
|
|
|
(*m_logger) << "plaintext rc4 ]\n";
|
|
|
|
else if (allowed_enc_level == pe_settings::rc4)
|
|
|
|
(*m_logger) << "rc4 ]\n";
|
|
|
|
else if (allowed_enc_level == pe_settings::plaintext)
|
|
|
|
(*m_logger) << "plaintext ]\n";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
write_pe_vc_cryptofield(send_buf, crypto_provide, pad_size);
|
|
|
|
m_RC4_handler->encrypt(send_buf.end - encrypt_size, encrypt_size);
|
2009-01-28 05:20:05 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
|
|
|
const int packet_size = 20 + 20 + 8 + 4 + 2 + pad_size + 2;
|
|
|
|
TORRENT_ASSERT(send_buffer_size() - packet_size == m_encrypted_bytes);
|
|
|
|
m_encrypted_bytes += packet_size;
|
2009-02-04 06:51:00 +01:00
|
|
|
TORRENT_ASSERT(m_encrypted_bytes == send_buffer_size());
|
2009-01-28 05:20:05 +01:00
|
|
|
#endif
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(send_buf.begin == send_buf.end);
|
2007-06-06 02:41:20 +02:00
|
|
|
setup_send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_peer_connection::write_pe4_sync(int crypto_select)
|
|
|
|
{
|
2007-06-07 12:18:13 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!is_local());
|
|
|
|
TORRENT_ASSERT(!m_encrypted);
|
|
|
|
TORRENT_ASSERT(!m_rc4_encrypted);
|
|
|
|
TORRENT_ASSERT(crypto_select == 0x02 || crypto_select == 0x01);
|
|
|
|
TORRENT_ASSERT(!m_sent_handshake);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2008-01-31 07:34:43 +01:00
|
|
|
int pad_size =rand() % 512;
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2009-02-04 06:51:00 +01:00
|
|
|
TORRENT_ASSERT(send_buffer_size() == m_encrypted_bytes);
|
|
|
|
|
2008-01-31 07:34:43 +01:00
|
|
|
const int buf_size = 8 + 4 + 2 + pad_size;
|
2007-06-06 02:41:20 +02:00
|
|
|
buffer::interval send_buf = allocate_send_buffer(buf_size);
|
2008-02-05 06:51:05 +01:00
|
|
|
if (send_buf.begin == 0) return; // out of memory
|
2007-06-06 02:41:20 +02:00
|
|
|
write_pe_vc_cryptofield(send_buf, crypto_select, pad_size);
|
|
|
|
|
|
|
|
m_RC4_handler->encrypt(send_buf.end - buf_size, buf_size);
|
2009-01-28 05:20:05 +01:00
|
|
|
TORRENT_ASSERT(send_buffer_size() - buf_size == m_encrypted_bytes);
|
|
|
|
#ifdef TORRENT_DEBUG
|
|
|
|
m_encrypted_bytes += buf_size;
|
|
|
|
TORRENT_ASSERT(m_encrypted_bytes <= send_buffer_size());
|
|
|
|
#endif
|
2007-06-06 02:41:20 +02:00
|
|
|
setup_send();
|
|
|
|
|
|
|
|
// encryption method has been negotiated
|
|
|
|
if (crypto_select == 0x02)
|
|
|
|
m_rc4_encrypted = true;
|
|
|
|
else // 0x01
|
|
|
|
m_rc4_encrypted = false;
|
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << " crypto select : [ ";
|
|
|
|
if (crypto_select == 0x01)
|
|
|
|
(*m_logger) << "plaintext ]\n";
|
|
|
|
else
|
|
|
|
(*m_logger) << "rc4 ]\n";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-07-09 06:22:38 +02:00
|
|
|
void bt_peer_connection::write_pe_vc_cryptofield(buffer::interval& write_buf
|
|
|
|
, int crypto_field, int pad_size)
|
2007-06-06 02:41:20 +02:00
|
|
|
{
|
2007-06-07 12:18:13 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(crypto_field <= 0x03 && crypto_field > 0);
|
2007-06-06 02:41:20 +02:00
|
|
|
// vc,crypto_field,len(pad),pad, (len(ia))
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT( (write_buf.left() == 8+4+2+pad_size+2 && is_local()) ||
|
2007-06-06 02:41:20 +02:00
|
|
|
(write_buf.left() == 8+4+2+pad_size && !is_local()) );
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_sent_handshake);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
// encrypt(vc, crypto_provide/select, len(Pad), len(IA))
|
|
|
|
// len(pad) is zero for now, len(IA) only for outgoing connections
|
|
|
|
|
|
|
|
// vc
|
|
|
|
std::fill(write_buf.begin, write_buf.begin + 8, 0);
|
|
|
|
write_buf.begin += 8;
|
|
|
|
|
|
|
|
detail::write_uint32(crypto_field, write_buf.begin);
|
|
|
|
detail::write_uint16(pad_size, write_buf.begin); // len (pad)
|
|
|
|
|
|
|
|
// fill pad with zeroes
|
2008-01-31 07:34:43 +01:00
|
|
|
std::generate(write_buf.begin, write_buf.begin + pad_size, &std::rand);
|
|
|
|
write_buf.begin += pad_size;
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
// append len(ia) if we are initiating
|
|
|
|
if (is_local())
|
|
|
|
detail::write_uint16(handshake_len, write_buf.begin); // len(IA)
|
|
|
|
|
2008-01-31 07:34:43 +01:00
|
|
|
TORRENT_ASSERT(write_buf.begin == write_buf.end);
|
2007-06-06 02:41:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void bt_peer_connection::init_pe_RC4_handler(char const* secret, sha1_hash const& stream_key)
|
|
|
|
{
|
2007-06-07 12:18:13 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(secret);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
hasher h;
|
2007-09-29 18:14:03 +02:00
|
|
|
static const char keyA[] = "keyA";
|
|
|
|
static const char keyB[] = "keyB";
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
// encryption rc4 longkeys
|
|
|
|
// outgoing connection : hash ('keyA',S,SKEY)
|
|
|
|
// incoming connection : hash ('keyB',S,SKEY)
|
|
|
|
|
|
|
|
is_local() ? h.update(keyA, 4) : h.update(keyB, 4);
|
|
|
|
h.update(secret, dh_key_len);
|
|
|
|
h.update((char const*)stream_key.begin(), 20);
|
|
|
|
const sha1_hash local_key = h.final();
|
|
|
|
|
|
|
|
h.reset();
|
|
|
|
|
|
|
|
// decryption rc4 longkeys
|
|
|
|
// outgoing connection : hash ('keyB',S,SKEY)
|
|
|
|
// incoming connection : hash ('keyA',S,SKEY)
|
|
|
|
|
|
|
|
is_local() ? h.update(keyB, 4) : h.update(keyA, 4);
|
|
|
|
h.update(secret, dh_key_len);
|
|
|
|
h.update((char const*)stream_key.begin(), 20);
|
|
|
|
const sha1_hash remote_key = h.final();
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_RC4_handler.get());
|
2009-02-22 21:52:55 +01:00
|
|
|
m_RC4_handler.reset(new (std::nothrow) RC4_handler(local_key, remote_key));
|
|
|
|
if (!m_RC4_handler)
|
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::no_memory);
|
2009-02-22 21:52:55 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << " computed RC4 keys\n";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-01-27 07:57:55 +01:00
|
|
|
void bt_peer_connection::send_buffer(char const* buf, int size, int flags)
|
2007-06-06 02:41:20 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(buf);
|
|
|
|
TORRENT_ASSERT(size > 0);
|
2009-01-28 05:20:05 +01:00
|
|
|
|
|
|
|
encrypt_pending_buffer();
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2009-02-12 04:57:53 +01:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
2007-11-01 00:51:45 +01:00
|
|
|
if (m_encrypted && m_rc4_encrypted)
|
2009-01-28 05:20:05 +01:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(send_buffer_size() == m_encrypted_bytes);
|
2009-01-27 07:57:55 +01:00
|
|
|
m_RC4_handler->encrypt(const_cast<char*>(buf), size);
|
2009-01-28 05:20:05 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
|
|
|
m_encrypted_bytes += size;
|
|
|
|
#endif
|
|
|
|
}
|
2009-02-12 04:57:53 +01:00
|
|
|
#endif
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2008-07-08 02:03:08 +02:00
|
|
|
peer_connection::send_buffer(buf, size, flags);
|
2007-06-06 02:41:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer::interval bt_peer_connection::allocate_send_buffer(int size)
|
|
|
|
{
|
2009-02-12 04:57:53 +01:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
2009-01-28 05:20:05 +01:00
|
|
|
encrypt_pending_buffer();
|
2007-11-01 00:51:45 +01:00
|
|
|
if (m_encrypted && m_rc4_encrypted)
|
2007-06-06 02:41:20 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_enc_send_buffer.left() == 0);
|
2007-06-06 02:41:20 +02:00
|
|
|
m_enc_send_buffer = peer_connection::allocate_send_buffer(size);
|
|
|
|
return m_enc_send_buffer;
|
|
|
|
}
|
|
|
|
else
|
2009-02-12 04:57:53 +01:00
|
|
|
#endif
|
2007-06-06 02:41:20 +02:00
|
|
|
{
|
|
|
|
buffer::interval i = peer_connection::allocate_send_buffer(size);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-12 04:57:53 +01:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
2009-01-28 05:20:05 +01:00
|
|
|
void bt_peer_connection::encrypt_pending_buffer()
|
2007-06-06 02:41:20 +02:00
|
|
|
{
|
2007-11-01 00:51:45 +01:00
|
|
|
if (m_encrypted && m_rc4_encrypted && m_enc_send_buffer.left())
|
2007-06-06 02:41:20 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_enc_send_buffer.begin);
|
|
|
|
TORRENT_ASSERT(m_enc_send_buffer.end);
|
2009-01-28 05:20:05 +01:00
|
|
|
TORRENT_ASSERT(m_RC4_handler);
|
|
|
|
TORRENT_ASSERT(send_buffer_size() - m_enc_send_buffer.left() == m_encrypted_bytes);
|
|
|
|
#ifdef TORRENT_DEBUG
|
|
|
|
m_encrypted_bytes += m_enc_send_buffer.left();
|
|
|
|
TORRENT_ASSERT(m_encrypted_bytes <= send_buffer_size());
|
|
|
|
#endif
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2007-09-29 18:14:03 +02:00
|
|
|
m_RC4_handler->encrypt(m_enc_send_buffer.begin, m_enc_send_buffer.left());
|
|
|
|
m_enc_send_buffer.end = m_enc_send_buffer.begin;
|
2007-06-06 02:41:20 +02:00
|
|
|
}
|
2009-01-28 05:20:05 +01:00
|
|
|
}
|
2009-02-12 04:57:53 +01:00
|
|
|
#endif
|
2009-01-28 05:20:05 +01:00
|
|
|
|
|
|
|
void bt_peer_connection::setup_send()
|
|
|
|
{
|
2009-02-12 04:57:53 +01:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
2009-01-28 05:20:05 +01:00
|
|
|
encrypt_pending_buffer();
|
2009-02-04 06:51:00 +01:00
|
|
|
TORRENT_ASSERT(!m_encrypted || !m_rc4_encrypted || m_encrypted_bytes == send_buffer_size());
|
2009-02-12 04:57:53 +01:00
|
|
|
#endif
|
2007-06-06 02:41:20 +02:00
|
|
|
peer_connection::setup_send();
|
|
|
|
}
|
|
|
|
|
|
|
|
int bt_peer_connection::get_syncoffset(char const* src, int src_size,
|
2007-09-29 18:14:03 +02:00
|
|
|
char const* target, int target_size) const
|
2007-06-06 02:41:20 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(target_size >= src_size);
|
|
|
|
TORRENT_ASSERT(src_size > 0);
|
|
|
|
TORRENT_ASSERT(src);
|
|
|
|
TORRENT_ASSERT(target);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
int traverse_limit = target_size - src_size;
|
|
|
|
|
|
|
|
// TODO: this could be optimized using knuth morris pratt
|
|
|
|
for (int i = 0; i < traverse_limit; ++i)
|
|
|
|
{
|
|
|
|
char const* target_ptr = target + i;
|
|
|
|
if (std::equal(src, src+src_size, target_ptr))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// // Partial sync
|
|
|
|
// for (int i = 0; i < target_size; ++i)
|
|
|
|
// {
|
|
|
|
// // first is iterator in src[] at which mismatch occurs
|
|
|
|
// // second is iterator in target[] at which mismatch occurs
|
|
|
|
// std::pair<const char*, const char*> ret;
|
|
|
|
// int src_sync_size;
|
|
|
|
// if (i > traverse_limit) // partial sync test
|
|
|
|
// {
|
|
|
|
// ret = std::mismatch(src, src + src_size - (i - traverse_limit), &target[i]);
|
|
|
|
// src_sync_size = ret.first - src;
|
|
|
|
// if (src_sync_size == (src_size - (i - traverse_limit)))
|
|
|
|
// return i;
|
|
|
|
// }
|
|
|
|
// else // complete sync test
|
|
|
|
// {
|
|
|
|
// ret = std::mismatch(src, src + src_size, &target[i]);
|
|
|
|
// src_sync_size = ret.first - src;
|
|
|
|
// if (src_sync_size == src_size)
|
|
|
|
// return i;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// no complete sync
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif // #ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void bt_peer_connection::write_handshake()
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_sent_handshake);
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-07-09 06:22:38 +02:00
|
|
|
m_sent_handshake = true;
|
|
|
|
#endif
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::shared_ptr<torrent> t = associated_torrent().lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
// add handshake to the send buffer
|
|
|
|
const char version_string[] = "BitTorrent protocol";
|
|
|
|
const int string_len = sizeof(version_string)-1;
|
|
|
|
|
|
|
|
buffer::interval i = allocate_send_buffer(1 + string_len + 8 + 20 + 20);
|
2008-02-05 06:51:05 +01:00
|
|
|
if (i.begin == 0) return; // out of memory
|
2006-04-25 23:04:48 +02:00
|
|
|
// length of version string
|
|
|
|
*i.begin = string_len;
|
|
|
|
++i.begin;
|
|
|
|
|
|
|
|
// version string itself
|
|
|
|
std::copy(
|
|
|
|
version_string
|
|
|
|
, version_string + string_len
|
|
|
|
, i.begin);
|
|
|
|
i.begin += string_len;
|
|
|
|
|
|
|
|
// 8 zeroes
|
2006-11-14 01:08:16 +01:00
|
|
|
std::fill(i.begin, i.begin + 8, 0);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2006-08-01 17:27:08 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2006-04-25 23:04:48 +02:00
|
|
|
// indicate that we support the DHT messages
|
2007-09-10 00:45:31 +02:00
|
|
|
*(i.begin + 7) |= 0x01;
|
2006-08-01 17:27:08 +02:00
|
|
|
#endif
|
2006-11-14 01:08:16 +01:00
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
2006-04-25 23:04:48 +02:00
|
|
|
// we support extensions
|
2007-09-10 00:45:31 +02:00
|
|
|
*(i.begin + 5) |= 0x10;
|
2006-11-14 01:08:16 +01:00
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2009-03-13 07:09:39 +01:00
|
|
|
// we support merkle torrents
|
|
|
|
*(i.begin + 5) |= 0x08;
|
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
// we support FAST extension
|
2007-09-10 00:45:31 +02:00
|
|
|
*(i.begin + 7) |= 0x04;
|
2007-08-14 19:47:48 +02:00
|
|
|
|
2009-03-13 07:09:39 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
for (int k = 0; k < 8; ++k)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < 8; ++j)
|
|
|
|
{
|
|
|
|
if (i.begin[k] & (0x80 >> j)) (*m_logger) << "1";
|
|
|
|
else (*m_logger) << "0";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
i.begin += 8;
|
|
|
|
|
|
|
|
// info hash
|
|
|
|
sha1_hash const& ih = t->torrent_file().info_hash();
|
|
|
|
std::copy(ih.begin(), ih.end(), i.begin);
|
|
|
|
i.begin += 20;
|
|
|
|
|
|
|
|
// peer id
|
|
|
|
std::copy(
|
|
|
|
m_ses.get_peer_id().begin()
|
|
|
|
, m_ses.get_peer_id().end()
|
|
|
|
, i.begin);
|
|
|
|
i.begin += 20;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(i.begin == i.end);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2007-04-10 09:52:58 +02:00
|
|
|
(*m_logger) << time_now_string() << " ==> HANDSHAKE\n";
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
|
|
|
setup_send();
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<piece_block_progress> bt_peer_connection::downloading_piece_progress() const
|
|
|
|
{
|
|
|
|
boost::shared_ptr<torrent> t = associated_torrent().lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
buffer::const_interval recv_buffer = receive_buffer();
|
|
|
|
// are we currently receiving a 'piece' message?
|
|
|
|
if (m_state != read_packet
|
2009-03-17 10:34:44 +01:00
|
|
|
|| recv_buffer.left() <= 9
|
2006-04-25 23:04:48 +02:00
|
|
|
|| recv_buffer[0] != msg_piece)
|
|
|
|
return boost::optional<piece_block_progress>();
|
|
|
|
|
|
|
|
const char* ptr = recv_buffer.begin + 1;
|
|
|
|
peer_request r;
|
|
|
|
r.piece = detail::read_int32(ptr);
|
|
|
|
r.start = detail::read_int32(ptr);
|
|
|
|
r.length = packet_size() - 9;
|
|
|
|
|
|
|
|
// is any of the piece message header data invalid?
|
|
|
|
if (!verify_piece(r))
|
|
|
|
return boost::optional<piece_block_progress>();
|
|
|
|
|
|
|
|
piece_block_progress p;
|
|
|
|
|
|
|
|
p.piece_index = r.piece;
|
|
|
|
p.block_index = r.start / t->block_size();
|
2007-02-12 06:46:29 +01:00
|
|
|
p.bytes_downloaded = recv_buffer.left() - 9;
|
2006-04-25 23:04:48 +02:00
|
|
|
p.full_block_bytes = r.length;
|
|
|
|
|
|
|
|
return boost::optional<piece_block_progress>(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// message handlers
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
// --------- KEEPALIVE ---------
|
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
void bt_peer_connection::on_keepalive()
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2007-04-10 09:52:58 +02:00
|
|
|
(*m_logger) << time_now_string() << " <== KEEPALIVE\n";
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
|
|
|
incoming_keepalive();
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
// ----------- CHOKE -----------
|
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
void bt_peer_connection::on_choke(int received)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(received > 0);
|
2008-10-01 17:19:31 +02:00
|
|
|
m_statistics.received_bytes(0, received);
|
2006-04-25 23:04:48 +02:00
|
|
|
if (packet_size() != 1)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_choke, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
if (!packet_finished()) return;
|
|
|
|
|
|
|
|
incoming_choke();
|
2008-05-12 05:05:27 +02:00
|
|
|
if (is_disconnecting()) return;
|
2007-08-14 19:47:48 +02:00
|
|
|
if (!m_supports_fast)
|
|
|
|
{
|
2009-08-05 20:20:33 +02:00
|
|
|
// we just got choked, and the peer that choked use
|
|
|
|
// doesn't support fast extensions, so we have to
|
|
|
|
// assume that the choke message implies that all
|
|
|
|
// of our requests are rejected. Go through them and
|
|
|
|
// pretend that we received reject request messages
|
2007-08-14 19:47:48 +02:00
|
|
|
boost::shared_ptr<torrent> t = associated_torrent().lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2007-10-10 04:27:55 +02:00
|
|
|
while (!download_queue().empty())
|
2007-08-14 19:47:48 +02:00
|
|
|
{
|
2008-07-07 14:04:06 +02:00
|
|
|
piece_block const& b = download_queue().front().block;
|
2007-08-14 19:47:48 +02:00
|
|
|
peer_request r;
|
|
|
|
r.piece = b.piece_index;
|
|
|
|
r.start = b.block_index * t->block_size();
|
|
|
|
r.length = t->block_size();
|
2009-08-05 20:20:33 +02:00
|
|
|
// if it's the last piece, make sure to
|
|
|
|
// set the length of the request to not
|
|
|
|
// exceed the end of the torrent. This is
|
|
|
|
// necessary in order to maintain a correct
|
|
|
|
// m_outsanding_bytes
|
|
|
|
if (r.piece == t->torrent_file().num_pieces() - 1)
|
|
|
|
{
|
|
|
|
r.length = (std::min)(t->torrent_file().piece_size(
|
|
|
|
r.piece) - r.start, r.length);
|
|
|
|
}
|
2007-08-14 19:47:48 +02:00
|
|
|
incoming_reject_request(r);
|
|
|
|
}
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
// ---------- UNCHOKE ----------
|
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
void bt_peer_connection::on_unchoke(int received)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(received > 0);
|
2008-10-01 17:19:31 +02:00
|
|
|
m_statistics.received_bytes(0, received);
|
2006-04-25 23:04:48 +02:00
|
|
|
if (packet_size() != 1)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_unchoke, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
if (!packet_finished()) return;
|
|
|
|
|
|
|
|
incoming_unchoke();
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
// -------- INTERESTED ---------
|
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
void bt_peer_connection::on_interested(int received)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(received > 0);
|
2008-10-01 17:19:31 +02:00
|
|
|
m_statistics.received_bytes(0, received);
|
2006-04-25 23:04:48 +02:00
|
|
|
if (packet_size() != 1)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_interested, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
if (!packet_finished()) return;
|
|
|
|
|
|
|
|
incoming_interested();
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
// ------ NOT INTERESTED -------
|
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
void bt_peer_connection::on_not_interested(int received)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(received > 0);
|
2008-10-01 17:19:31 +02:00
|
|
|
m_statistics.received_bytes(0, received);
|
2006-04-25 23:04:48 +02:00
|
|
|
if (packet_size() != 1)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_not_interested, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
if (!packet_finished()) return;
|
|
|
|
|
|
|
|
incoming_not_interested();
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
// ----------- HAVE ------------
|
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
void bt_peer_connection::on_have(int received)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(received > 0);
|
2008-10-01 17:19:31 +02:00
|
|
|
m_statistics.received_bytes(0, received);
|
2006-04-25 23:04:48 +02:00
|
|
|
if (packet_size() != 5)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_have, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
if (!packet_finished()) return;
|
|
|
|
|
|
|
|
buffer::const_interval recv_buffer = receive_buffer();
|
|
|
|
|
|
|
|
const char* ptr = recv_buffer.begin + 1;
|
|
|
|
int index = detail::read_int32(ptr);
|
|
|
|
|
|
|
|
incoming_have(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
// --------- BITFIELD ----------
|
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
void bt_peer_connection::on_bitfield(int received)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(received > 0);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
boost::shared_ptr<torrent> t = associated_torrent().lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-10-01 17:19:31 +02:00
|
|
|
m_statistics.received_bytes(0, received);
|
2006-04-25 23:04:48 +02:00
|
|
|
// if we don't have the metedata, we cannot
|
|
|
|
// verify the bitfield size
|
|
|
|
if (t->valid_metadata()
|
2008-08-29 19:21:56 +02:00
|
|
|
&& packet_size() - 1 != (t->torrent_file().num_pieces() + 7) / 8)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_bitfield_size, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
if (!packet_finished()) return;
|
|
|
|
|
|
|
|
buffer::const_interval recv_buffer = receive_buffer();
|
|
|
|
|
2008-05-28 04:35:02 +02:00
|
|
|
bitfield bits;
|
2008-06-02 08:52:24 +02:00
|
|
|
bits.borrow_bytes((char*)recv_buffer.begin + 1
|
2008-05-28 04:35:02 +02:00
|
|
|
, t->valid_metadata()?get_bitfield().size():(packet_size()-1)*8);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-05-28 04:35:02 +02:00
|
|
|
incoming_bitfield(bits);
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
// ---------- REQUEST ----------
|
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
void bt_peer_connection::on_request(int received)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(received > 0);
|
2008-10-01 17:19:31 +02:00
|
|
|
m_statistics.received_bytes(0, received);
|
2006-04-25 23:04:48 +02:00
|
|
|
if (packet_size() != 13)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_request, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
if (!packet_finished()) return;
|
|
|
|
|
|
|
|
buffer::const_interval recv_buffer = receive_buffer();
|
|
|
|
|
|
|
|
peer_request r;
|
|
|
|
const char* ptr = recv_buffer.begin + 1;
|
|
|
|
r.piece = detail::read_int32(ptr);
|
|
|
|
r.start = detail::read_int32(ptr);
|
|
|
|
r.length = detail::read_int32(ptr);
|
2008-12-08 07:36:22 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
incoming_request(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
// ----------- PIECE -----------
|
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
void bt_peer_connection::on_piece(int received)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(received > 0);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
buffer::const_interval recv_buffer = receive_buffer();
|
|
|
|
int recv_pos = recv_buffer.end - recv_buffer.begin;
|
|
|
|
|
2009-03-13 07:09:39 +01:00
|
|
|
boost::shared_ptr<torrent> t = associated_torrent().lock();
|
|
|
|
TORRENT_ASSERT(t);
|
|
|
|
bool merkle = (unsigned char)recv_buffer.begin[0] == 250;
|
|
|
|
if (merkle)
|
2008-04-10 12:03:23 +02:00
|
|
|
{
|
2009-03-13 07:09:39 +01:00
|
|
|
if (recv_pos == 1)
|
|
|
|
{
|
|
|
|
set_soft_packet_size(13);
|
|
|
|
m_statistics.received_bytes(0, received);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (recv_pos < 13)
|
2008-10-01 17:19:31 +02:00
|
|
|
{
|
|
|
|
m_statistics.received_bytes(0, received);
|
2008-04-10 12:03:23 +02:00
|
|
|
return;
|
2008-10-01 17:19:31 +02:00
|
|
|
}
|
2009-03-13 07:09:39 +01:00
|
|
|
if (recv_pos == 13)
|
|
|
|
{
|
|
|
|
const char* ptr = recv_buffer.begin + 9;
|
|
|
|
int list_size = detail::read_int32(ptr);
|
|
|
|
// now we know how long the bencoded hash list is
|
|
|
|
// and we can allocate the disk buffer and receive
|
|
|
|
// into it
|
|
|
|
|
|
|
|
if (list_size > packet_size() - 13)
|
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_hash_list, 2);
|
2009-03-13 07:09:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_ASSERT(!has_disk_receive_buffer());
|
|
|
|
if (!allocate_disk_receive_buffer(packet_size() - 13 - list_size))
|
|
|
|
{
|
|
|
|
m_statistics.received_bytes(0, received);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (recv_pos == 1)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(!has_disk_receive_buffer());
|
|
|
|
if (!allocate_disk_receive_buffer(packet_size() - 9))
|
|
|
|
{
|
|
|
|
m_statistics.received_bytes(0, received);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2008-04-10 12:03:23 +02:00
|
|
|
}
|
2008-11-17 20:19:45 +01:00
|
|
|
TORRENT_ASSERT(has_disk_receive_buffer() || packet_size() == 9);
|
2006-04-25 23:04:48 +02:00
|
|
|
// classify the received data as protocol chatter
|
|
|
|
// or data payload for the statistics
|
2009-03-17 10:34:44 +01:00
|
|
|
int piece_bytes = 0;
|
2006-04-25 23:04:48 +02:00
|
|
|
if (recv_pos <= 9)
|
2009-03-17 10:34:44 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
// only received protocol data
|
|
|
|
m_statistics.received_bytes(0, received);
|
2009-03-17 10:34:44 +01:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
else if (recv_pos - received >= 9)
|
2009-03-17 10:34:44 +01:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
// only received payload data
|
|
|
|
m_statistics.received_bytes(received, 0);
|
2009-03-17 10:34:44 +01:00
|
|
|
piece_bytes = received;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// received a bit of both
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(recv_pos - received < 9);
|
|
|
|
TORRENT_ASSERT(recv_pos > 9);
|
|
|
|
TORRENT_ASSERT(9 - (recv_pos - received) <= 9);
|
2006-04-25 23:04:48 +02:00
|
|
|
m_statistics.received_bytes(
|
|
|
|
recv_pos - 9
|
|
|
|
, 9 - (recv_pos - received));
|
2009-03-17 10:34:44 +01:00
|
|
|
piece_bytes = recv_pos - 9;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2009-03-19 17:50:37 +01:00
|
|
|
const int header_size = merkle?13:9;
|
|
|
|
|
|
|
|
if (recv_pos < header_size) return;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
const char* ptr = recv_buffer.begin + 1;
|
|
|
|
peer_request p;
|
|
|
|
p.piece = detail::read_int32(ptr);
|
|
|
|
p.start = detail::read_int32(ptr);
|
2009-03-13 07:09:39 +01:00
|
|
|
|
|
|
|
int list_size = 0;
|
|
|
|
|
|
|
|
if (merkle)
|
|
|
|
{
|
|
|
|
list_size = detail::read_int32(ptr);
|
|
|
|
p.length = packet_size() - list_size - header_size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p.length = packet_size() - header_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
// (*m_logger) << time_now_string() << " <== PIECE_FRAGMENT p: " << p.piece
|
|
|
|
// << " start: " << p.start << " length: " << p.length << "\n";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (recv_pos - received < header_size && recv_pos >= header_size)
|
|
|
|
{
|
2009-03-17 10:34:44 +01:00
|
|
|
// call this once, the first time the entire header
|
|
|
|
// has been received
|
|
|
|
start_receive_piece(p);
|
2009-04-17 19:56:58 +02:00
|
|
|
if (is_disconnecting()) return;
|
2009-03-13 07:09:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_ASSERT(has_disk_receive_buffer() || packet_size() == header_size);
|
|
|
|
|
2009-03-17 10:34:44 +01:00
|
|
|
incoming_piece_fragment(piece_bytes);
|
2009-03-13 07:09:39 +01:00
|
|
|
if (!packet_finished()) return;
|
|
|
|
|
|
|
|
if (merkle && list_size > 0)
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << time_now_string() << " <== HASHPIECE " << p.piece << " list: " << list_size << " ";
|
|
|
|
#endif
|
|
|
|
lazy_entry hash_list;
|
|
|
|
if (lazy_bdecode(recv_buffer.begin + 13, recv_buffer.end + 13 + list_size, hash_list) != 0)
|
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_hash_piece, 2);
|
2009-03-13 07:09:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the list has this format:
|
|
|
|
// [ [node-index, hash], [node-index, hash], ... ]
|
|
|
|
if (hash_list.type() != lazy_entry::list_t)
|
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_hash_list, 2);
|
2009-03-13 07:09:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<int, sha1_hash> nodes;
|
|
|
|
for (int i = 0; i < hash_list.list_size(); ++i)
|
|
|
|
{
|
|
|
|
lazy_entry const* e = hash_list.list_at(i);
|
|
|
|
if (e->type() != lazy_entry::list_t
|
|
|
|
|| e->list_size() != 2
|
|
|
|
|| e->list_at(0)->type() != lazy_entry::int_t
|
|
|
|
|| e->list_at(1)->type() != lazy_entry::string_t
|
|
|
|
|| e->list_at(1)->string_length() != 20) continue;
|
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << " " << e->list_int_value_at(0) << ": "
|
|
|
|
<< sha1_hash(e->list_at(1)->string_ptr());
|
|
|
|
#endif
|
2009-08-30 09:38:52 +02:00
|
|
|
nodes.insert(std::make_pair(int(e->list_int_value_at(0))
|
2009-03-13 07:09:39 +01:00
|
|
|
, sha1_hash(e->list_at(1)->string_ptr())));
|
|
|
|
}
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << "\n";
|
|
|
|
#endif
|
|
|
|
if (!nodes.empty() && !t->add_merkle_nodes(nodes, p.piece))
|
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_hash_piece, 2);
|
2009-03-13 07:09:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-04-10 12:03:23 +02:00
|
|
|
disk_buffer_holder holder(m_ses, release_disk_receive_buffer());
|
|
|
|
incoming_piece(p, holder);
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
// ---------- CANCEL -----------
|
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
void bt_peer_connection::on_cancel(int received)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(received > 0);
|
2008-10-01 17:19:31 +02:00
|
|
|
m_statistics.received_bytes(0, received);
|
2006-04-25 23:04:48 +02:00
|
|
|
if (packet_size() != 13)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_cancel, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
if (!packet_finished()) return;
|
|
|
|
|
|
|
|
buffer::const_interval recv_buffer = receive_buffer();
|
|
|
|
|
|
|
|
peer_request r;
|
|
|
|
const char* ptr = recv_buffer.begin + 1;
|
|
|
|
r.piece = detail::read_int32(ptr);
|
|
|
|
r.start = detail::read_int32(ptr);
|
|
|
|
r.length = detail::read_int32(ptr);
|
|
|
|
|
|
|
|
incoming_cancel(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------
|
|
|
|
// --------- DHT PORT ----------
|
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
void bt_peer_connection::on_dht_port(int received)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-09-30 20:52:53 +02:00
|
|
|
TORRENT_ASSERT(received > 0);
|
|
|
|
m_statistics.received_bytes(0, received);
|
2006-04-25 23:04:48 +02:00
|
|
|
if (packet_size() != 3)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_dht_port, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
if (!packet_finished()) return;
|
|
|
|
|
|
|
|
buffer::const_interval recv_buffer = receive_buffer();
|
|
|
|
|
|
|
|
const char* ptr = recv_buffer.begin + 1;
|
|
|
|
int listen_port = detail::read_uint16(ptr);
|
|
|
|
|
|
|
|
incoming_dht_port(listen_port);
|
2010-02-18 06:27:58 +01:00
|
|
|
|
|
|
|
if (!m_supports_dht_port)
|
|
|
|
{
|
|
|
|
m_supports_dht_port = true;
|
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
if (m_supports_dht_port && m_ses.m_dht)
|
|
|
|
write_dht_port(m_ses.get_dht_settings().service_port);
|
|
|
|
#endif
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
void bt_peer_connection::on_suggest_piece(int received)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-09-30 20:52:53 +02:00
|
|
|
m_statistics.received_bytes(0, received);
|
2007-08-14 19:47:48 +02:00
|
|
|
if (!m_supports_fast)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_suggest, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-08-14 19:47:48 +02:00
|
|
|
|
2007-09-01 09:38:10 +02:00
|
|
|
if (!packet_finished()) return;
|
|
|
|
|
|
|
|
buffer::const_interval recv_buffer = receive_buffer();
|
|
|
|
|
|
|
|
const char* ptr = recv_buffer.begin + 1;
|
|
|
|
int piece = detail::read_uint32(ptr);
|
|
|
|
incoming_suggest(piece);
|
2007-08-14 19:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void bt_peer_connection::on_have_all(int received)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-09-30 20:52:53 +02:00
|
|
|
m_statistics.received_bytes(0, received);
|
2007-08-14 19:47:48 +02:00
|
|
|
if (!m_supports_fast)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_have_all, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-08-14 19:47:48 +02:00
|
|
|
incoming_have_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_peer_connection::on_have_none(int received)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-09-30 20:52:53 +02:00
|
|
|
m_statistics.received_bytes(0, received);
|
2007-08-14 19:47:48 +02:00
|
|
|
if (!m_supports_fast)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_have_none, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-08-14 19:47:48 +02:00
|
|
|
incoming_have_none();
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_peer_connection::on_reject_request(int received)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-09-30 20:52:53 +02:00
|
|
|
m_statistics.received_bytes(0, received);
|
2007-08-14 19:47:48 +02:00
|
|
|
if (!m_supports_fast)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_reject, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-08-14 19:47:48 +02:00
|
|
|
|
|
|
|
if (!packet_finished()) return;
|
|
|
|
|
|
|
|
buffer::const_interval recv_buffer = receive_buffer();
|
|
|
|
|
|
|
|
peer_request r;
|
|
|
|
const char* ptr = recv_buffer.begin + 1;
|
|
|
|
r.piece = detail::read_int32(ptr);
|
|
|
|
r.start = detail::read_int32(ptr);
|
|
|
|
r.length = detail::read_int32(ptr);
|
|
|
|
|
|
|
|
incoming_reject_request(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_peer_connection::on_allowed_fast(int received)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-09-30 20:52:53 +02:00
|
|
|
m_statistics.received_bytes(0, received);
|
2007-08-14 19:47:48 +02:00
|
|
|
if (!m_supports_fast)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_allow_fast, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-08-14 19:47:48 +02:00
|
|
|
|
|
|
|
if (!packet_finished()) return;
|
|
|
|
buffer::const_interval recv_buffer = receive_buffer();
|
|
|
|
const char* ptr = recv_buffer.begin + 1;
|
|
|
|
int index = detail::read_int32(ptr);
|
|
|
|
|
|
|
|
incoming_allowed_fast(index);
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// -----------------------------
|
|
|
|
// --------- EXTENDED ----------
|
|
|
|
// -----------------------------
|
|
|
|
|
|
|
|
void bt_peer_connection::on_extended(int received)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(received > 0);
|
2006-04-25 23:04:48 +02:00
|
|
|
m_statistics.received_bytes(0, received);
|
|
|
|
if (packet_size() < 2)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_extended, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
if (associated_torrent().expired())
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_extended, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
buffer::const_interval recv_buffer = receive_buffer();
|
2006-11-14 01:08:16 +01:00
|
|
|
if (recv_buffer.left() < 2) return;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(*recv_buffer.begin == msg_extended);
|
2006-04-25 23:04:48 +02:00
|
|
|
++recv_buffer.begin;
|
|
|
|
|
|
|
|
int extended_id = detail::read_uint8(recv_buffer.begin);
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
if (extended_id == 0)
|
|
|
|
{
|
|
|
|
on_extended_handshake();
|
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2009-11-24 19:49:59 +01:00
|
|
|
if (extended_id == upload_only_msg)
|
|
|
|
{
|
|
|
|
if (!packet_finished()) return;
|
|
|
|
set_upload_only(detail::read_uint8(recv_buffer.begin));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2006-11-14 01:08:16 +01:00
|
|
|
if ((*i)->on_extended(packet_size() - 2, extended_id
|
|
|
|
, recv_buffer))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_message, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
2006-11-14 01:08:16 +01:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
void bt_peer_connection::on_extended_handshake()
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
|
|
|
if (!packet_finished()) return;
|
|
|
|
|
|
|
|
boost::shared_ptr<torrent> t = associated_torrent().lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
buffer::const_interval recv_buffer = receive_buffer();
|
|
|
|
|
2008-07-01 10:04:12 +02:00
|
|
|
lazy_entry root;
|
|
|
|
lazy_bdecode(recv_buffer.begin + 2, recv_buffer.end, root);
|
|
|
|
if (root.type() != lazy_entry::dict_t)
|
2006-11-14 01:08:16 +01:00
|
|
|
{
|
2006-11-19 16:29:58 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2008-04-07 04:07:23 +02:00
|
|
|
(*m_logger) << "invalid extended handshake\n";
|
2006-11-14 01:08:16 +01:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-11-19 16:29:58 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2008-07-01 10:04:12 +02:00
|
|
|
(*m_logger) << "<== EXTENDED HANDSHAKE: \n" << root;
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
2008-07-01 22:22:25 +02:00
|
|
|
for (extension_list_t::iterator i = m_extensions.begin();
|
|
|
|
!m_extensions.empty() && i != m_extensions.end();)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2006-11-14 01:08:16 +01:00
|
|
|
// a false return value means that the extension
|
|
|
|
// isn't supported by the other end. So, it is removed.
|
|
|
|
if (!(*i)->on_extension_handshake(root))
|
|
|
|
i = m_extensions.erase(i);
|
|
|
|
else
|
|
|
|
++i;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2008-10-23 18:31:15 +02:00
|
|
|
if (is_disconnecting()) return;
|
2006-11-14 01:08:16 +01:00
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2009-11-24 19:49:59 +01:00
|
|
|
// upload_only
|
|
|
|
if (lazy_entry const* m = root.dict_find_dict("m"))
|
|
|
|
m_upload_only_id = m->dict_find_int_value("upload_only", 0);
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// there is supposed to be a remote listen port
|
2008-07-01 10:04:12 +02:00
|
|
|
int listen_port = root.dict_find_int_value("p");
|
|
|
|
if (listen_port > 0 && peer_info_struct() != 0)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2008-07-01 10:04:12 +02:00
|
|
|
t->get_policy().update_peer_port(listen_port
|
|
|
|
, peer_info_struct(), peer_info::incoming);
|
2008-11-01 20:34:12 +01:00
|
|
|
if (is_disconnecting()) return;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
// there should be a version too
|
|
|
|
// but where do we put that info?
|
2010-03-19 19:39:51 +01:00
|
|
|
|
|
|
|
int last_seen_complete = root.dict_find_int_value("complete_ago", -1);
|
|
|
|
if (last_seen_complete >= 0) set_last_seen_complete(last_seen_complete);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-07-01 10:04:12 +02:00
|
|
|
std::string client_info = root.dict_find_string_value("v");
|
|
|
|
if (!client_info.empty()) m_client_version = client_info;
|
2006-05-15 00:30:05 +02:00
|
|
|
|
2008-07-01 10:04:12 +02:00
|
|
|
int reqq = root.dict_find_int_value("reqq");
|
|
|
|
if (reqq > 0) m_max_out_request_queue = reqq;
|
2008-02-28 04:09:34 +01:00
|
|
|
|
2009-11-24 19:49:59 +01:00
|
|
|
if (root.dict_find_int_value("upload_only", 0))
|
2008-07-01 10:04:12 +02:00
|
|
|
set_upload_only(true);
|
2008-05-18 07:59:47 +02:00
|
|
|
|
2008-07-01 10:04:12 +02:00
|
|
|
std::string myip = root.dict_find_string_value("yourip");
|
|
|
|
if (!myip.empty())
|
2008-02-28 04:09:34 +01:00
|
|
|
{
|
|
|
|
// TODO: don't trust this blindly
|
2008-07-01 10:04:12 +02:00
|
|
|
if (myip.size() == address_v4::bytes_type::static_size)
|
|
|
|
{
|
|
|
|
address_v4::bytes_type bytes;
|
|
|
|
std::copy(myip.begin(), myip.end(), bytes.begin());
|
|
|
|
m_ses.set_external_address(address_v4(bytes));
|
|
|
|
}
|
2009-11-27 19:46:29 +01:00
|
|
|
#if TORRENT_USE_IPV6
|
2008-07-01 10:04:12 +02:00
|
|
|
else if (myip.size() == address_v6::bytes_type::static_size)
|
2008-02-28 04:09:34 +01:00
|
|
|
{
|
2008-07-01 10:04:12 +02:00
|
|
|
address_v6::bytes_type bytes;
|
|
|
|
std::copy(myip.begin(), myip.end(), bytes.begin());
|
2008-09-20 18:21:16 +02:00
|
|
|
address_v6 ipv6_address(bytes);
|
|
|
|
if (ipv6_address.is_v4_mapped())
|
|
|
|
m_ses.set_external_address(ipv6_address.to_v4());
|
|
|
|
else
|
|
|
|
m_ses.set_external_address(ipv6_address);
|
2008-02-28 04:09:34 +01:00
|
|
|
}
|
2009-11-27 19:46:29 +01:00
|
|
|
#endif
|
2008-02-28 04:09:34 +01:00
|
|
|
}
|
2008-05-18 07:59:47 +02:00
|
|
|
|
|
|
|
// if we're finished and this peer is uploading only
|
|
|
|
// disconnect it
|
|
|
|
if (t->is_finished() && upload_only())
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::upload_upload_connection);
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool bt_peer_connection::dispatch_message(int received)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(received > 0);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
// this means the connection has been closed already
|
2008-11-06 10:01:27 +01:00
|
|
|
if (associated_torrent().expired())
|
|
|
|
{
|
|
|
|
m_statistics.received_bytes(0, received);
|
|
|
|
return false;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
buffer::const_interval recv_buffer = receive_buffer();
|
|
|
|
|
2008-04-10 12:03:23 +02:00
|
|
|
TORRENT_ASSERT(recv_buffer.left() >= 1);
|
2009-03-13 07:09:39 +01:00
|
|
|
int packet_type = (unsigned char)recv_buffer[0];
|
|
|
|
if (packet_type == 250) packet_type = msg_piece;
|
2006-04-25 23:04:48 +02:00
|
|
|
if (packet_type < 0
|
|
|
|
|| packet_type >= num_supported_messages
|
|
|
|
|| m_message_handler[packet_type] == 0)
|
|
|
|
{
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if ((*i)->on_unknown_message(packet_size(), packet_type
|
|
|
|
, buffer::const_interval(recv_buffer.begin+1
|
|
|
|
, recv_buffer.end)))
|
|
|
|
return packet_finished();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-06 10:01:27 +01:00
|
|
|
m_statistics.received_bytes(0, received);
|
2010-01-17 22:49:26 +01:00
|
|
|
// What's going on here?!
|
|
|
|
// break in debug builds to allow investigation
|
2010-04-11 19:22:21 +02:00
|
|
|
// TORRENT_ASSERT(false);
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_message);
|
2008-01-07 02:10:46 +01:00
|
|
|
return packet_finished();
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_message_handler[packet_type] != 0);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2008-09-21 19:12:26 +02:00
|
|
|
size_type cur_payload_dl = m_statistics.last_payload_downloaded();
|
|
|
|
size_type cur_protocol_dl = m_statistics.last_protocol_downloaded();
|
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
// call the correct handler for this packet type
|
|
|
|
(this->*m_message_handler[packet_type])(received);
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2008-09-21 19:12:26 +02:00
|
|
|
TORRENT_ASSERT(m_statistics.last_payload_downloaded() - cur_payload_dl >= 0);
|
|
|
|
TORRENT_ASSERT(m_statistics.last_protocol_downloaded() - cur_protocol_dl >= 0);
|
|
|
|
size_type stats_diff = m_statistics.last_payload_downloaded() - cur_payload_dl +
|
|
|
|
m_statistics.last_protocol_downloaded() - cur_protocol_dl;
|
|
|
|
TORRENT_ASSERT(stats_diff == received);
|
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
return packet_finished();
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2009-11-24 19:49:59 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
void bt_peer_connection::write_upload_only()
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
boost::shared_ptr<torrent> t = associated_torrent().lock();
|
|
|
|
if (m_upload_only_id == 0) return;
|
|
|
|
|
|
|
|
char msg[7] = {0, 0, 0, 3, msg_extended};
|
|
|
|
char* ptr = msg + 5;
|
|
|
|
detail::write_uint8(m_upload_only_id, ptr);
|
|
|
|
detail::write_uint8(t->is_upload_only(), ptr);
|
|
|
|
send_buffer(msg, sizeof(msg));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
void bt_peer_connection::write_keepalive()
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-11-19 08:07:57 +01:00
|
|
|
// Don't require the bitfield to have been sent at this point
|
|
|
|
// the case where m_sent_bitfield may not be true is if the
|
|
|
|
// torrent doesn't have any metadata, and a peer is timimg out.
|
|
|
|
// then the keep-alive message will be sent before the bitfield
|
|
|
|
// this is a violation to the original protocol, but necessary
|
|
|
|
// for the metadata extension.
|
|
|
|
TORRENT_ASSERT(m_sent_handshake);
|
2007-07-09 06:22:38 +02:00
|
|
|
|
2007-09-29 18:14:03 +02:00
|
|
|
char msg[] = {0,0,0,0};
|
|
|
|
send_buffer(msg, sizeof(msg));
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void bt_peer_connection::write_cancel(peer_request const& r)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_sent_handshake && m_sent_bitfield);
|
|
|
|
TORRENT_ASSERT(associated_torrent().lock()->valid_metadata());
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-09-29 18:14:03 +02:00
|
|
|
char msg[17] = {0,0,0,13, msg_cancel};
|
|
|
|
char* ptr = msg + 5;
|
|
|
|
detail::write_int32(r.piece, ptr); // index
|
|
|
|
detail::write_int32(r.start, ptr); // begin
|
|
|
|
detail::write_int32(r.length, ptr); // length
|
|
|
|
send_buffer(msg, sizeof(msg));
|
2008-01-02 04:18:29 +01:00
|
|
|
|
|
|
|
if (!m_supports_fast)
|
|
|
|
incoming_reject_request(r);
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void bt_peer_connection::write_request(peer_request const& r)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_sent_handshake && m_sent_bitfield);
|
|
|
|
TORRENT_ASSERT(associated_torrent().lock()->valid_metadata());
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-09-29 18:14:03 +02:00
|
|
|
char msg[17] = {0,0,0,13, msg_request};
|
|
|
|
char* ptr = msg + 5;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-09-29 18:14:03 +02:00
|
|
|
detail::write_int32(r.piece, ptr); // index
|
|
|
|
detail::write_int32(r.start, ptr); // begin
|
|
|
|
detail::write_int32(r.length, ptr); // length
|
2008-07-08 02:03:08 +02:00
|
|
|
send_buffer(msg, sizeof(msg), message_type_request);
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2008-06-07 04:58:28 +02:00
|
|
|
void bt_peer_connection::write_bitfield()
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
boost::shared_ptr<torrent> t = associated_torrent().lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
|
|
|
TORRENT_ASSERT(m_sent_handshake && !m_sent_bitfield);
|
|
|
|
TORRENT_ASSERT(t->valid_metadata());
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
// in this case, have_all or have_none should be sent instead
|
2008-06-07 16:03:21 +02:00
|
|
|
TORRENT_ASSERT(!m_supports_fast || !t->is_seed() || t->num_have() != 0);
|
2007-08-14 19:47:48 +02:00
|
|
|
|
2008-12-08 07:36:22 +01:00
|
|
|
if (t->super_seeding())
|
|
|
|
{
|
|
|
|
if (m_supports_fast) write_have_none();
|
|
|
|
|
|
|
|
// if we are super seeding, pretend to not have any piece
|
|
|
|
// and don't send a bitfield
|
|
|
|
#ifdef TORRENT_DEBUG
|
|
|
|
m_sent_bitfield = true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// bootstrap superseeding by sending one have message
|
|
|
|
superseed_piece(t->get_piece_to_super_seed(
|
|
|
|
get_bitfield()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (m_supports_fast && t->is_seed())
|
2007-08-14 19:47:48 +02:00
|
|
|
{
|
|
|
|
write_have_all();
|
|
|
|
send_allowed_set();
|
|
|
|
return;
|
|
|
|
}
|
2008-06-07 16:03:21 +02:00
|
|
|
else if (m_supports_fast && t->num_have() == 0)
|
2007-08-14 19:47:48 +02:00
|
|
|
{
|
|
|
|
write_have_none();
|
|
|
|
send_allowed_set();
|
|
|
|
return;
|
|
|
|
}
|
2008-06-07 16:03:21 +02:00
|
|
|
else if (t->num_have() == 0)
|
2007-12-21 00:58:58 +01:00
|
|
|
{
|
|
|
|
// don't send a bitfield if we don't have any pieces
|
2008-08-17 00:07:15 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2008-09-04 16:39:02 +02:00
|
|
|
(*m_logger) << time_now_string() << " *** NOT SENDING BITFIELD\n";
|
2008-08-17 00:07:15 +02:00
|
|
|
#endif
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-12-22 20:10:38 +01:00
|
|
|
m_sent_bitfield = true;
|
|
|
|
#endif
|
2007-12-21 00:58:58 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-08-14 19:47:48 +02:00
|
|
|
|
2008-06-07 16:03:21 +02:00
|
|
|
int num_pieces = t->torrent_file().num_pieces();
|
2008-12-08 07:36:22 +01:00
|
|
|
|
2007-05-27 00:27:40 +02:00
|
|
|
int lazy_pieces[50];
|
|
|
|
int num_lazy_pieces = 0;
|
|
|
|
int lazy_piece = 0;
|
|
|
|
|
2010-02-25 20:19:12 +01:00
|
|
|
if (t->is_seed() && m_ses.settings().lazy_bitfields
|
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
&& !m_encrypted
|
|
|
|
#endif
|
|
|
|
)
|
2007-05-27 00:27:40 +02:00
|
|
|
{
|
2007-08-17 00:14:17 +02:00
|
|
|
num_lazy_pieces = (std::min)(50, num_pieces / 10);
|
2007-05-27 00:27:40 +02:00
|
|
|
if (num_lazy_pieces < 1) num_lazy_pieces = 1;
|
|
|
|
for (int i = 0; i < num_pieces; ++i)
|
|
|
|
{
|
|
|
|
if (rand() % (num_pieces - i) >= num_lazy_pieces - lazy_piece) continue;
|
|
|
|
lazy_pieces[lazy_piece++] = i;
|
|
|
|
}
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(lazy_piece == num_lazy_pieces);
|
2007-05-27 00:27:40 +02:00
|
|
|
lazy_piece = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int packet_size = (num_pieces + 7) / 8 + 5;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
buffer::interval i = allocate_send_buffer(packet_size);
|
2008-02-05 06:51:05 +01:00
|
|
|
if (i.begin == 0) return; // out of memory
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
detail::write_int32(packet_size - 4, i.begin);
|
|
|
|
detail::write_uint8(msg_bitfield, i.begin);
|
|
|
|
|
2008-06-07 04:58:28 +02:00
|
|
|
if (t->is_seed())
|
|
|
|
{
|
2009-02-06 09:51:25 +01:00
|
|
|
memset(i.begin, 0xff, packet_size - 6);
|
|
|
|
|
|
|
|
// Clear trailing bits
|
|
|
|
unsigned char *p = ((unsigned char *)i.begin) + packet_size - 6;
|
|
|
|
*p = (0xff << ((8 - (num_pieces & 7)) & 7)) & 0xff;
|
2008-06-07 04:58:28 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memset(i.begin, 0, packet_size - 5);
|
|
|
|
piece_picker const& p = t->picker();
|
|
|
|
int mask = 0x80;
|
|
|
|
unsigned char* byte = (unsigned char*)i.begin;
|
|
|
|
for (int i = 0; i < num_pieces; ++i)
|
|
|
|
{
|
|
|
|
if (p.have_piece(i)) *byte |= mask;
|
|
|
|
mask >>= 1;
|
|
|
|
if (mask == 0)
|
|
|
|
{
|
|
|
|
mask = 0x80;
|
|
|
|
++byte;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-05-28 04:35:02 +02:00
|
|
|
for (int c = 0; c < num_lazy_pieces; ++c)
|
|
|
|
i.begin[lazy_pieces[c] / 8] &= ~(0x80 >> (lazy_pieces[c] & 7));
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(i.end - i.begin == (num_pieces + 7) / 8);
|
2007-06-05 12:20:45 +02:00
|
|
|
|
2008-06-07 04:58:28 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << time_now_string() << " ==> BITFIELD ";
|
|
|
|
|
2009-04-25 20:39:44 +02:00
|
|
|
std::string bitfield_string;
|
|
|
|
bitfield_string.resize(num_pieces + 1);
|
2008-06-07 04:58:28 +02:00
|
|
|
for (int k = 0; k < num_pieces; ++k)
|
|
|
|
{
|
2009-04-04 11:52:25 +02:00
|
|
|
if (i.begin[k / 8] & (0x80 >> (k % 8))) bitfield_string[k] = '1';
|
|
|
|
else bitfield_string[k] = '0';
|
2008-06-07 04:58:28 +02:00
|
|
|
}
|
2009-04-04 11:52:25 +02:00
|
|
|
bitfield_string[num_pieces] = '\n';
|
|
|
|
(*m_logger) << bitfield_string;
|
2008-06-07 04:58:28 +02:00
|
|
|
#endif
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-06-05 12:20:45 +02:00
|
|
|
m_sent_bitfield = true;
|
|
|
|
#endif
|
|
|
|
|
2009-01-28 05:20:05 +01:00
|
|
|
setup_send();
|
|
|
|
|
2007-05-27 00:27:40 +02:00
|
|
|
if (num_lazy_pieces > 0)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < num_lazy_pieces; ++i)
|
|
|
|
{
|
|
|
|
write_have(lazy_pieces[i]);
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << time_now_string()
|
|
|
|
<< " ==> HAVE [ piece: " << lazy_pieces[i] << "]\n";
|
|
|
|
#endif
|
|
|
|
}
|
2008-12-08 07:36:22 +01:00
|
|
|
// TODO: if we're finished, send upload_only message
|
2007-05-27 00:27:40 +02:00
|
|
|
}
|
2007-08-14 19:47:48 +02:00
|
|
|
|
|
|
|
if (m_supports_fast)
|
|
|
|
send_allowed_set();
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
2006-04-25 23:04:48 +02:00
|
|
|
void bt_peer_connection::write_extensions()
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2007-04-10 09:52:58 +02:00
|
|
|
(*m_logger) << time_now_string() << " ==> EXTENSIONS\n";
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_supports_extensions);
|
|
|
|
TORRENT_ASSERT(m_sent_handshake);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2009-11-24 19:49:59 +01:00
|
|
|
entry handshake;
|
|
|
|
entry::dictionary_type& m = handshake["m"].dict();
|
2006-11-26 18:44:15 +01:00
|
|
|
|
|
|
|
// only send the port in case we bade the connection
|
|
|
|
// on incoming connections the other end already knows
|
|
|
|
// our listen port
|
2010-04-13 06:30:34 +02:00
|
|
|
if (!m_ses.m_settings.anonymous_mode)
|
|
|
|
{
|
|
|
|
if (is_local()) handshake["p"] = m_ses.listen_port();
|
|
|
|
handshake["v"] = m_ses.settings().user_agent;
|
|
|
|
}
|
|
|
|
|
2006-09-28 02:49:40 +02:00
|
|
|
std::string remote_address;
|
|
|
|
std::back_insert_iterator<std::string> out(remote_address);
|
|
|
|
detail::write_address(remote().address(), out);
|
2007-07-07 03:36:40 +02:00
|
|
|
handshake["yourip"] = remote_address;
|
2006-10-11 16:02:21 +02:00
|
|
|
handshake["reqq"] = m_ses.settings().max_allowed_in_request_queue;
|
2008-05-18 07:59:47 +02:00
|
|
|
boost::shared_ptr<torrent> t = associated_torrent().lock();
|
|
|
|
TORRENT_ASSERT(t);
|
2008-12-08 07:36:22 +01:00
|
|
|
|
2009-11-24 19:49:59 +01:00
|
|
|
m["upload_only"] = upload_only_msg;
|
2010-03-19 19:39:51 +01:00
|
|
|
int complete_ago = -1;
|
|
|
|
if (t->last_seen_complete() > 0) complete_ago = t->time_since_complete();
|
|
|
|
handshake["complete_ago"] = complete_ago;
|
2009-11-24 19:49:59 +01:00
|
|
|
|
2008-12-08 07:36:22 +01:00
|
|
|
// if we're using lazy bitfields or if we're super seeding, don't say
|
|
|
|
// we're upload only, since it might make peers disconnect
|
2010-02-25 20:19:12 +01:00
|
|
|
if (t->is_upload_only() && (!m_ses.settings().lazy_bitfields
|
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
|| m_encrypted
|
|
|
|
#endif
|
|
|
|
))
|
2008-12-08 07:36:22 +01:00
|
|
|
handshake["upload_only"] = 1;
|
2007-09-22 18:27:29 +02:00
|
|
|
|
2010-04-13 06:30:34 +02:00
|
|
|
if (!m_ses.m_settings.anonymous_mode)
|
2007-09-19 23:54:26 +02:00
|
|
|
{
|
2010-04-13 06:30:34 +02:00
|
|
|
tcp::endpoint ep = m_ses.get_ipv6_interface();
|
|
|
|
if (!is_any(ep.address()))
|
|
|
|
{
|
|
|
|
std::string ipv6_address;
|
|
|
|
std::back_insert_iterator<std::string> out(ipv6_address);
|
|
|
|
detail::write_address(ep.address(), out);
|
|
|
|
handshake["ipv6"] = ipv6_address;
|
|
|
|
}
|
2007-09-19 23:54:26 +02:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
// loop backwards, to make the first extension be the last
|
|
|
|
// to fill in the handshake (i.e. give the first extensions priority)
|
|
|
|
for (extension_list_t::reverse_iterator i = m_extensions.rbegin()
|
|
|
|
, end(m_extensions.rend()); i != end; ++i)
|
|
|
|
{
|
|
|
|
(*i)->add_handshake(handshake);
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
std::vector<char> msg;
|
|
|
|
bencode(std::back_inserter(msg), handshake);
|
|
|
|
|
|
|
|
// make room for message
|
|
|
|
buffer::interval i = allocate_send_buffer(6 + msg.size());
|
2008-02-05 06:51:05 +01:00
|
|
|
if (i.begin == 0) return; // out of memory
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
// write the length of the message
|
|
|
|
detail::write_int32((int)msg.size() + 2, i.begin);
|
|
|
|
detail::write_uint8(msg_extended, i.begin);
|
|
|
|
// signal handshake message
|
2006-11-14 01:08:16 +01:00
|
|
|
detail::write_uint8(0, i.begin);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
std::copy(msg.begin(), msg.end(), i.begin);
|
|
|
|
i.begin += msg.size();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(i.begin == i.end);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2009-04-04 23:44:04 +02:00
|
|
|
#if defined TORRENT_VERBOSE_LOGGING && TORRENT_USE_IOSTREAM
|
2006-04-25 23:04:48 +02:00
|
|
|
std::stringstream ext;
|
|
|
|
handshake.print(ext);
|
|
|
|
(*m_logger) << "==> EXTENDED HANDSHAKE: \n" << ext.str();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
setup_send();
|
|
|
|
}
|
2006-11-14 01:08:16 +01:00
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
void bt_peer_connection::write_choke()
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
2007-07-09 06:22:38 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_sent_handshake && m_sent_bitfield);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
if (is_choked()) return;
|
|
|
|
char msg[] = {0,0,0,1,msg_choke};
|
2007-09-29 18:14:03 +02:00
|
|
|
send_buffer(msg, sizeof(msg));
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void bt_peer_connection::write_unchoke()
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
2007-07-09 06:22:38 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_sent_handshake && m_sent_bitfield);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
char msg[] = {0,0,0,1,msg_unchoke};
|
2007-09-29 18:14:03 +02:00
|
|
|
send_buffer(msg, sizeof(msg));
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void bt_peer_connection::write_interested()
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
2007-07-09 06:22:38 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_sent_handshake && m_sent_bitfield);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
char msg[] = {0,0,0,1,msg_interested};
|
2007-09-29 18:14:03 +02:00
|
|
|
send_buffer(msg, sizeof(msg));
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void bt_peer_connection::write_not_interested()
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
2007-07-09 06:22:38 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_sent_handshake && m_sent_bitfield);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
char msg[] = {0,0,0,1,msg_not_interested};
|
2007-09-29 18:14:03 +02:00
|
|
|
send_buffer(msg, sizeof(msg));
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void bt_peer_connection::write_have(int index)
|
|
|
|
{
|
2007-06-21 02:51:42 +02:00
|
|
|
INVARIANT_CHECK;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(associated_torrent().lock()->valid_metadata());
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
TORRENT_ASSERT(index < associated_torrent().lock()->torrent_file().num_pieces());
|
|
|
|
TORRENT_ASSERT(m_sent_handshake && m_sent_bitfield);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-09-29 18:14:03 +02:00
|
|
|
char msg[] = {0,0,0,5,msg_have,0,0,0,0};
|
2006-04-25 23:04:48 +02:00
|
|
|
char* ptr = msg + 5;
|
|
|
|
detail::write_int32(index, ptr);
|
2007-09-29 18:14:03 +02:00
|
|
|
send_buffer(msg, sizeof(msg));
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2008-04-10 12:03:23 +02:00
|
|
|
void bt_peer_connection::write_piece(peer_request const& r, disk_buffer_holder& buffer)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2006-04-30 02:39:18 +02:00
|
|
|
INVARIANT_CHECK;
|
2007-07-09 06:22:38 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_sent_handshake && m_sent_bitfield);
|
2006-04-30 02:39:18 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
boost::shared_ptr<torrent> t = associated_torrent().lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2009-03-13 07:09:39 +01:00
|
|
|
bool merkle = t->torrent_file().is_merkle_torrent() && r.start == 0;
|
|
|
|
// the hash piece looks like this:
|
|
|
|
// uint8_t msg
|
|
|
|
// uint32_t piece index
|
|
|
|
// uint32_t start
|
|
|
|
// uint32_t list len
|
|
|
|
// var bencoded list
|
|
|
|
// var piece data
|
|
|
|
char msg[4 + 1 + 4 + 4 + 4];
|
2007-09-29 18:14:03 +02:00
|
|
|
char* ptr = msg;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(r.length <= 16 * 1024);
|
2007-09-29 18:14:03 +02:00
|
|
|
detail::write_int32(r.length + 1 + 4 + 4, ptr);
|
2009-03-13 07:09:39 +01:00
|
|
|
if (merkle)
|
|
|
|
detail::write_uint8(250, ptr);
|
|
|
|
else
|
|
|
|
detail::write_uint8(msg_piece, ptr);
|
2007-09-29 18:14:03 +02:00
|
|
|
detail::write_int32(r.piece, ptr);
|
|
|
|
detail::write_int32(r.start, ptr);
|
2009-03-13 07:09:39 +01:00
|
|
|
|
|
|
|
// if this is a merkle torrent and the start offset
|
|
|
|
// is 0, we need to include the merkle node hashes
|
|
|
|
if (merkle)
|
|
|
|
{
|
|
|
|
std::vector<char> piece_list_buf;
|
|
|
|
entry piece_list;
|
|
|
|
entry::list_type& l = piece_list.list();
|
|
|
|
std::map<int, sha1_hash> merkle_node_list = t->torrent_file().build_merkle_list(r.piece);
|
|
|
|
for (std::map<int, sha1_hash>::iterator i = merkle_node_list.begin()
|
|
|
|
, end(merkle_node_list.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
l.push_back(entry(entry::list_t));
|
|
|
|
l.back().list().push_back(i->first);
|
|
|
|
l.back().list().push_back(i->second.to_string());
|
|
|
|
}
|
|
|
|
bencode(std::back_inserter(piece_list_buf), piece_list);
|
|
|
|
detail::write_int32(piece_list_buf.size(), ptr);
|
|
|
|
|
|
|
|
char* ptr = msg;
|
|
|
|
detail::write_int32(r.length + 1 + 4 + 4 + 4 + piece_list_buf.size(), ptr);
|
|
|
|
|
|
|
|
send_buffer(msg, 17);
|
|
|
|
send_buffer(&piece_list_buf[0], piece_list_buf.size());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
send_buffer(msg, 13);
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-05-05 08:25:22 +02:00
|
|
|
append_send_buffer(buffer.get(), r.length
|
2007-09-29 18:14:03 +02:00
|
|
|
, boost::bind(&session_impl::free_disk_buffer
|
|
|
|
, boost::ref(m_ses), _1));
|
2008-04-10 12:03:23 +02:00
|
|
|
buffer.release();
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
m_payloads.push_back(range(send_buffer_size() - r.length, r.length));
|
|
|
|
setup_send();
|
|
|
|
}
|
|
|
|
|
2006-11-30 12:56:19 +01:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
struct match_peer_id
|
|
|
|
{
|
|
|
|
match_peer_id(peer_id const& id, peer_connection const* pc)
|
|
|
|
: m_id(id), m_pc(pc)
|
2007-10-05 02:30:00 +02:00
|
|
|
{ TORRENT_ASSERT(pc); }
|
2006-11-30 12:56:19 +01:00
|
|
|
|
2009-05-07 00:36:24 +02:00
|
|
|
bool operator()(policy::peer const* p) const
|
2006-11-30 12:56:19 +01:00
|
|
|
{
|
2009-05-07 00:36:24 +02:00
|
|
|
return p->connection != m_pc
|
|
|
|
&& p->connection
|
|
|
|
&& p->connection->pid() == m_id
|
|
|
|
&& !p->connection->pid().is_all_zeros()
|
|
|
|
&& p->address() == m_pc->remote().address();
|
2006-11-30 12:56:19 +01:00
|
|
|
}
|
|
|
|
|
2007-06-09 01:02:31 +02:00
|
|
|
peer_id const& m_id;
|
2006-11-30 12:56:19 +01:00
|
|
|
peer_connection const* m_pc;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// --------------------------
|
|
|
|
// RECEIVE DATA
|
|
|
|
// --------------------------
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void bt_peer_connection::on_receive(error_code const& error
|
2006-04-25 23:04:48 +02:00
|
|
|
, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-09-21 19:12:26 +02:00
|
|
|
if (error)
|
|
|
|
{
|
2007-06-06 02:41:20 +02:00
|
|
|
m_statistics.received_bytes(0, bytes_transferred);
|
2008-09-21 19:12:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::shared_ptr<torrent> t = associated_torrent().lock();
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(in_handshake() || !m_rc4_encrypted || m_encrypted);
|
2007-06-07 13:04:00 +02:00
|
|
|
if (m_rc4_encrypted && m_encrypted)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2008-04-10 12:03:23 +02:00
|
|
|
std::pair<buffer::interval, buffer::interval> wr_buf = wr_recv_buffers(bytes_transferred);
|
|
|
|
m_RC4_handler->decrypt(wr_buf.first.begin, wr_buf.first.left());
|
|
|
|
if (wr_buf.second.left()) m_RC4_handler->decrypt(wr_buf.second.begin, wr_buf.second.left());
|
2007-06-06 02:41:20 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
buffer::const_interval recv_buffer = receive_buffer();
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
// m_state is set to read_pe_dhkey in initial state
|
|
|
|
// (read_protocol_identifier) for incoming, or in constructor
|
|
|
|
// for outgoing
|
|
|
|
if (m_state == read_pe_dhkey)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2008-09-21 19:12:26 +02:00
|
|
|
m_statistics.received_bytes(0, bytes_transferred);
|
|
|
|
|
2008-04-10 12:03:23 +02:00
|
|
|
TORRENT_ASSERT(!m_encrypted);
|
|
|
|
TORRENT_ASSERT(!m_rc4_encrypted);
|
|
|
|
TORRENT_ASSERT(packet_size() == dh_key_len);
|
|
|
|
TORRENT_ASSERT(recv_buffer == receive_buffer());
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
if (!packet_finished()) return;
|
|
|
|
|
2008-06-28 12:10:05 +02:00
|
|
|
// write our dh public key. m_dh_key_exchange is
|
2007-06-06 02:41:20 +02:00
|
|
|
// initialized in write_pe1_2_dhkey()
|
2008-06-28 12:10:05 +02:00
|
|
|
if (!is_local()) write_pe1_2_dhkey();
|
|
|
|
if (is_disconnecting()) return;
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
// read dh key, generate shared secret
|
2008-06-28 12:10:05 +02:00
|
|
|
if (m_dh_key_exchange->compute_secret(recv_buffer.begin) == -1)
|
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::no_memory);
|
2008-06-28 12:10:05 +02:00
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << " received DH key\n";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// PadA/B can be a max of 512 bytes, and 20 bytes more for
|
|
|
|
// the sync hash (if incoming), or 8 bytes more for the
|
|
|
|
// encrypted verification constant (if outgoing). Instead
|
|
|
|
// of requesting the maximum possible, request the maximum
|
|
|
|
// possible to ensure we do not overshoot the standard
|
|
|
|
// handshake.
|
|
|
|
|
|
|
|
if (is_local())
|
|
|
|
{
|
|
|
|
m_state = read_pe_syncvc;
|
|
|
|
write_pe3_sync();
|
|
|
|
|
|
|
|
// initial payload is the standard handshake, this is
|
|
|
|
// always rc4 if sent here. m_rc4_encrypted is flagged
|
|
|
|
// again according to peer selection.
|
|
|
|
m_rc4_encrypted = true;
|
|
|
|
m_encrypted = true;
|
|
|
|
write_handshake();
|
|
|
|
m_rc4_encrypted = false;
|
|
|
|
m_encrypted = false;
|
|
|
|
|
|
|
|
// vc,crypto_select,len(pad),pad, encrypt(handshake)
|
|
|
|
// 8+4+2+0+handshake_len
|
2008-01-27 23:39:50 +01:00
|
|
|
reset_recv_buffer(8+4+2+0+handshake_len);
|
2007-06-06 02:41:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// already written dh key
|
|
|
|
m_state = read_pe_synchash;
|
|
|
|
// synchash,skeyhash,vc,crypto_provide,len(pad),pad,encrypt(handshake)
|
|
|
|
reset_recv_buffer(20+20+8+4+2+0+handshake_len);
|
|
|
|
}
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!packet_finished());
|
2007-06-06 02:41:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cannot fall through into
|
|
|
|
if (m_state == read_pe_synchash)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_encrypted);
|
|
|
|
TORRENT_ASSERT(!m_rc4_encrypted);
|
|
|
|
TORRENT_ASSERT(!is_local());
|
|
|
|
TORRENT_ASSERT(recv_buffer == receive_buffer());
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
if (recv_buffer.left() < 20)
|
|
|
|
{
|
2008-09-21 19:12:26 +02:00
|
|
|
m_statistics.received_bytes(0, bytes_transferred);
|
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
if (packet_finished())
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::sync_hash_not_found, 2);
|
2007-06-06 02:41:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_sync_hash.get())
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_sync_bytes_read == 0);
|
2007-06-06 02:41:20 +02:00
|
|
|
hasher h;
|
|
|
|
|
|
|
|
// compute synchash (hash('req1',S))
|
|
|
|
h.update("req1", 4);
|
2008-06-28 12:10:05 +02:00
|
|
|
h.update(m_dh_key_exchange->get_secret(), dh_key_len);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2009-02-22 21:52:55 +01:00
|
|
|
m_sync_hash.reset(new (std::nothrow) sha1_hash(h.final()));
|
|
|
|
if (!m_sync_hash)
|
|
|
|
{
|
2009-04-11 04:19:57 +02:00
|
|
|
m_statistics.received_bytes(0, bytes_transferred);
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::no_memory);
|
2009-02-22 21:52:55 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int syncoffset = get_syncoffset((char*)m_sync_hash->begin(), 20
|
|
|
|
, recv_buffer.begin, recv_buffer.left());
|
|
|
|
|
|
|
|
// No sync
|
|
|
|
if (syncoffset == -1)
|
|
|
|
{
|
2008-09-21 19:12:26 +02:00
|
|
|
m_statistics.received_bytes(0, bytes_transferred);
|
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
std::size_t bytes_processed = recv_buffer.left() - 20;
|
|
|
|
m_sync_bytes_read += bytes_processed;
|
|
|
|
if (m_sync_bytes_read >= 512)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::sync_hash_not_found, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2008-08-21 01:05:12 +02:00
|
|
|
cut_receive_buffer(bytes_processed, (std::min)(packet_size()
|
|
|
|
, (512+20) - m_sync_bytes_read));
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!packet_finished());
|
2007-06-06 02:41:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// found complete sync
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::size_t bytes_processed = syncoffset + 20;
|
2006-04-25 23:04:48 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2007-06-06 02:41:20 +02:00
|
|
|
(*m_logger) << " sync point (hash) found at offset "
|
|
|
|
<< m_sync_bytes_read + bytes_processed - 20 << "\n";
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
2007-06-06 02:41:20 +02:00
|
|
|
m_state = read_pe_skey_vc;
|
|
|
|
// skey,vc - 28 bytes
|
|
|
|
m_sync_hash.reset();
|
2008-09-21 19:12:26 +02:00
|
|
|
int transferred_used = bytes_processed - recv_buffer.left() + bytes_transferred;
|
|
|
|
TORRENT_ASSERT(transferred_used <= int(bytes_transferred));
|
|
|
|
m_statistics.received_bytes(0, transferred_used);
|
|
|
|
bytes_transferred -= transferred_used;
|
2007-06-06 02:41:20 +02:00
|
|
|
cut_receive_buffer(bytes_processed, 28);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_state == read_pe_skey_vc)
|
|
|
|
{
|
2008-09-21 19:12:26 +02:00
|
|
|
m_statistics.received_bytes(0, bytes_transferred);
|
2008-09-24 04:38:20 +02:00
|
|
|
bytes_transferred = 0;
|
2008-09-21 19:12:26 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_encrypted);
|
|
|
|
TORRENT_ASSERT(!m_rc4_encrypted);
|
|
|
|
TORRENT_ASSERT(!is_local());
|
|
|
|
TORRENT_ASSERT(packet_size() == 28);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
if (!packet_finished()) return;
|
|
|
|
|
|
|
|
recv_buffer = receive_buffer();
|
|
|
|
|
2008-08-21 01:05:12 +02:00
|
|
|
aux::session_impl::torrent_map::const_iterator i;
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2008-08-21 01:05:12 +02:00
|
|
|
for (i = m_ses.m_torrents.begin(); i != m_ses.m_torrents.end(); ++i)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2008-08-21 01:05:12 +02:00
|
|
|
torrent const& ti = *i->second;
|
|
|
|
sha1_hash const& skey_hash = ti.obfuscated_hash();
|
|
|
|
sha1_hash obfs_hash = m_dh_key_exchange->get_hash_xor_mask();
|
2007-06-06 02:41:20 +02:00
|
|
|
obfs_hash ^= skey_hash;
|
|
|
|
|
2008-08-21 01:05:12 +02:00
|
|
|
if (std::equal(recv_buffer.begin, recv_buffer.begin + 20,
|
|
|
|
(char*)&obfs_hash[0]))
|
2007-06-06 02:41:20 +02:00
|
|
|
{
|
|
|
|
if (!t)
|
|
|
|
{
|
2008-08-21 01:05:12 +02:00
|
|
|
attach_to_torrent(ti.info_hash());
|
2008-01-07 02:10:46 +01:00
|
|
|
if (is_disconnecting()) return;
|
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
t = associated_torrent().lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2007-06-06 02:41:20 +02:00
|
|
|
}
|
|
|
|
|
2008-08-21 01:05:12 +02:00
|
|
|
init_pe_RC4_handler(m_dh_key_exchange->get_secret(), ti.info_hash());
|
2007-06-06 02:41:20 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << " stream key found, torrent located.\n";
|
|
|
|
#endif
|
2008-08-21 01:05:12 +02:00
|
|
|
break;
|
2007-06-06 02:41:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_RC4_handler.get())
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_info_hash, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
// verify constant
|
|
|
|
buffer::interval wr_recv_buf = wr_recv_buffer();
|
|
|
|
m_RC4_handler->decrypt(wr_recv_buf.begin + 20, 8);
|
|
|
|
wr_recv_buf.begin += 28;
|
|
|
|
|
|
|
|
const char sh_vc[] = {0,0,0,0, 0,0,0,0};
|
|
|
|
if (!std::equal(sh_vc, sh_vc+8, recv_buffer.begin + 20))
|
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_encryption_constant, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << " verification constant found\n";
|
|
|
|
#endif
|
|
|
|
m_state = read_pe_cryptofield;
|
|
|
|
reset_recv_buffer(4 + 2);
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
// cannot fall through into
|
|
|
|
if (m_state == read_pe_syncvc)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(is_local());
|
|
|
|
TORRENT_ASSERT(!m_encrypted);
|
|
|
|
TORRENT_ASSERT(!m_rc4_encrypted);
|
|
|
|
TORRENT_ASSERT(recv_buffer == receive_buffer());
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
if (recv_buffer.left() < 8)
|
|
|
|
{
|
2008-09-21 19:12:26 +02:00
|
|
|
m_statistics.received_bytes(0, bytes_transferred);
|
2007-06-06 02:41:20 +02:00
|
|
|
if (packet_finished())
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_encryption_constant, 2);
|
2007-06-06 02:41:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate the verification constant
|
|
|
|
if (!m_sync_vc.get())
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_sync_bytes_read == 0);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2009-02-22 21:52:55 +01:00
|
|
|
m_sync_vc.reset(new (std::nothrow) char[8]);
|
|
|
|
if (!m_sync_vc)
|
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::no_memory);
|
2009-02-22 21:52:55 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
std::fill(m_sync_vc.get(), m_sync_vc.get() + 8, 0);
|
|
|
|
m_RC4_handler->decrypt(m_sync_vc.get(), 8);
|
|
|
|
}
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_sync_vc.get());
|
2007-06-06 02:41:20 +02:00
|
|
|
int syncoffset = get_syncoffset(m_sync_vc.get(), 8
|
|
|
|
, recv_buffer.begin, recv_buffer.left());
|
|
|
|
|
|
|
|
// No sync
|
|
|
|
if (syncoffset == -1)
|
|
|
|
{
|
|
|
|
std::size_t bytes_processed = recv_buffer.left() - 8;
|
|
|
|
m_sync_bytes_read += bytes_processed;
|
2008-09-21 19:12:26 +02:00
|
|
|
m_statistics.received_bytes(0, bytes_transferred);
|
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
if (m_sync_bytes_read >= 512)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_encryption_constant, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2008-09-21 19:12:26 +02:00
|
|
|
cut_receive_buffer(bytes_processed, (std::min)(packet_size()
|
|
|
|
, (512+8) - m_sync_bytes_read));
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!packet_finished());
|
2007-06-06 02:41:20 +02:00
|
|
|
}
|
|
|
|
// found complete sync
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::size_t bytes_processed = syncoffset + 8;
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << " sync point (verification constant) found at offset "
|
|
|
|
<< m_sync_bytes_read + bytes_processed - 8 << "\n";
|
|
|
|
#endif
|
2008-09-21 19:12:26 +02:00
|
|
|
int transferred_used = bytes_processed - recv_buffer.left() + bytes_transferred;
|
|
|
|
TORRENT_ASSERT(transferred_used <= int(bytes_transferred));
|
|
|
|
m_statistics.received_bytes(0, transferred_used);
|
|
|
|
bytes_transferred -= transferred_used;
|
|
|
|
|
|
|
|
cut_receive_buffer(bytes_processed, 4 + 2);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
// delete verification constant
|
|
|
|
m_sync_vc.reset();
|
|
|
|
m_state = read_pe_cryptofield;
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_state == read_pe_cryptofield) // local/remote
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_encrypted);
|
|
|
|
TORRENT_ASSERT(!m_rc4_encrypted);
|
|
|
|
TORRENT_ASSERT(packet_size() == 4+2);
|
2008-09-21 19:12:26 +02:00
|
|
|
m_statistics.received_bytes(0, bytes_transferred);
|
|
|
|
bytes_transferred = 0;
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
if (!packet_finished()) return;
|
|
|
|
|
|
|
|
buffer::interval wr_buf = wr_recv_buffer();
|
|
|
|
m_RC4_handler->decrypt(wr_buf.begin, packet_size());
|
|
|
|
|
|
|
|
recv_buffer = receive_buffer();
|
|
|
|
|
|
|
|
int crypto_field = detail::read_int32(recv_buffer.begin);
|
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
if (!is_local())
|
|
|
|
(*m_logger) << " crypto provide : [ ";
|
|
|
|
else
|
|
|
|
(*m_logger) << " crypto select : [ ";
|
|
|
|
|
|
|
|
if (crypto_field & 0x01)
|
|
|
|
(*m_logger) << "plaintext ";
|
|
|
|
if (crypto_field & 0x02)
|
|
|
|
(*m_logger) << "rc4 ";
|
|
|
|
(*m_logger) << "]\n";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!is_local())
|
|
|
|
{
|
|
|
|
int crypto_select = 0;
|
|
|
|
// select a crypto method
|
|
|
|
switch (m_ses.get_pe_settings().allowed_enc_level)
|
|
|
|
{
|
2008-04-08 07:51:12 +02:00
|
|
|
case pe_settings::plaintext:
|
2007-06-06 02:41:20 +02:00
|
|
|
if (!(crypto_field & 0x01))
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::no_plaintext_mode, 1);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
crypto_select = 0x01;
|
2008-04-08 07:51:12 +02:00
|
|
|
break;
|
|
|
|
case pe_settings::rc4:
|
2007-06-06 02:41:20 +02:00
|
|
|
if (!(crypto_field & 0x02))
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::no_rc4_mode, 1);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
crypto_select = 0x02;
|
2008-04-08 07:51:12 +02:00
|
|
|
break;
|
|
|
|
case pe_settings::both:
|
2007-06-06 02:41:20 +02:00
|
|
|
if (m_ses.get_pe_settings().prefer_rc4)
|
|
|
|
{
|
|
|
|
if (crypto_field & 0x02)
|
|
|
|
crypto_select = 0x02;
|
|
|
|
else if (crypto_field & 0x01)
|
|
|
|
crypto_select = 0x01;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (crypto_field & 0x01)
|
|
|
|
crypto_select = 0x01;
|
|
|
|
else if (crypto_field & 0x02)
|
|
|
|
crypto_select = 0x02;
|
|
|
|
}
|
|
|
|
if (!crypto_select)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::unsupported_encryption_mode, 1);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-04-08 07:51:12 +02:00
|
|
|
break;
|
2007-06-06 02:41:20 +02:00
|
|
|
} // switch
|
|
|
|
|
|
|
|
// write the pe4 step
|
|
|
|
write_pe4_sync(crypto_select);
|
|
|
|
}
|
|
|
|
else // is_local()
|
|
|
|
{
|
|
|
|
// check if crypto select is valid
|
|
|
|
pe_settings::enc_level const& allowed_enc_level = m_ses.get_pe_settings().allowed_enc_level;
|
|
|
|
|
|
|
|
if (crypto_field == 0x02)
|
|
|
|
{
|
|
|
|
if (allowed_enc_level == pe_settings::plaintext)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::unsupported_encryption_mode_selected, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
m_rc4_encrypted = true;
|
|
|
|
}
|
|
|
|
else if (crypto_field == 0x01)
|
|
|
|
{
|
|
|
|
if (allowed_enc_level == pe_settings::rc4)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::unsupported_encryption_mode_selected, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
m_rc4_encrypted = false;
|
|
|
|
}
|
|
|
|
else
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::unsupported_encryption_mode_selected, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int len_pad = detail::read_int16(recv_buffer.begin);
|
|
|
|
if (len_pad < 0 || len_pad > 512)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_pad_size, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
m_state = read_pe_pad;
|
|
|
|
if (!is_local())
|
|
|
|
reset_recv_buffer(len_pad + 2); // len(IA) at the end of pad
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (len_pad == 0)
|
|
|
|
{
|
|
|
|
m_encrypted = true;
|
|
|
|
m_state = init_bt_handshake;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
reset_recv_buffer(len_pad);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_state == read_pe_pad)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_encrypted);
|
2008-09-21 19:12:26 +02:00
|
|
|
m_statistics.received_bytes(0, bytes_transferred);
|
|
|
|
bytes_transferred = 0;
|
2007-06-06 02:41:20 +02:00
|
|
|
if (!packet_finished()) return;
|
|
|
|
|
|
|
|
int pad_size = is_local() ? packet_size() : packet_size() - 2;
|
|
|
|
|
|
|
|
buffer::interval wr_buf = wr_recv_buffer();
|
|
|
|
m_RC4_handler->decrypt(wr_buf.begin, packet_size());
|
|
|
|
|
|
|
|
recv_buffer = receive_buffer();
|
|
|
|
|
|
|
|
if (!is_local())
|
|
|
|
{
|
|
|
|
recv_buffer.begin += pad_size;
|
|
|
|
int len_ia = detail::read_int16(recv_buffer.begin);
|
2007-06-07 13:04:00 +02:00
|
|
|
|
2008-01-07 02:10:46 +01:00
|
|
|
if (len_ia < 0)
|
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_encrypt_handshake, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-06-07 13:04:00 +02:00
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << " len(IA) : " << len_ia << "\n";
|
|
|
|
#endif
|
|
|
|
if (len_ia == 0)
|
|
|
|
{
|
|
|
|
// everything after this is Encrypt2
|
|
|
|
m_encrypted = true;
|
|
|
|
m_state = init_bt_handshake;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_state = read_pe_ia;
|
|
|
|
reset_recv_buffer(len_ia);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // is_local()
|
|
|
|
{
|
|
|
|
// everything that arrives after this is Encrypt2
|
|
|
|
m_encrypted = true;
|
|
|
|
m_state = init_bt_handshake;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_state == read_pe_ia)
|
|
|
|
{
|
2008-09-21 19:12:26 +02:00
|
|
|
m_statistics.received_bytes(0, bytes_transferred);
|
2008-09-24 04:38:20 +02:00
|
|
|
bytes_transferred = 0;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!is_local());
|
|
|
|
TORRENT_ASSERT(!m_encrypted);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
if (!packet_finished()) return;
|
|
|
|
|
|
|
|
// ia is always rc4, so decrypt it
|
|
|
|
buffer::interval wr_buf = wr_recv_buffer();
|
|
|
|
m_RC4_handler->decrypt(wr_buf.begin, packet_size());
|
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << " decrypted ia : " << packet_size() << " bytes\n";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!m_rc4_encrypted)
|
|
|
|
{
|
|
|
|
m_RC4_handler.reset();
|
2006-04-25 23:04:48 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2007-06-06 02:41:20 +02:00
|
|
|
(*m_logger) << " destroyed rc4 keys\n";
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
2007-06-06 02:41:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// everything that arrives after this is Encrypt2
|
|
|
|
m_encrypted = true;
|
|
|
|
|
|
|
|
m_state = read_protocol_identifier;
|
|
|
|
cut_receive_buffer(0, 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_state == init_bt_handshake)
|
|
|
|
{
|
2008-09-21 19:12:26 +02:00
|
|
|
m_statistics.received_bytes(0, bytes_transferred);
|
|
|
|
bytes_transferred = 0;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_encrypted);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
// decrypt remaining received bytes
|
|
|
|
if (m_rc4_encrypted)
|
|
|
|
{
|
|
|
|
buffer::interval wr_buf = wr_recv_buffer();
|
|
|
|
wr_buf.begin += packet_size();
|
|
|
|
m_RC4_handler->decrypt(wr_buf.begin, wr_buf.left());
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << " decrypted remaining " << wr_buf.left() << " bytes\n";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else // !m_rc4_encrypted
|
|
|
|
{
|
|
|
|
m_RC4_handler.reset();
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << " destroyed rc4 keys\n";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// payload stream, start with 20 handshake bytes
|
|
|
|
m_state = read_protocol_identifier;
|
|
|
|
reset_recv_buffer(20);
|
|
|
|
|
|
|
|
// encrypted portion of handshake completed, toggle
|
|
|
|
// peer_info pe_support flag back to true
|
|
|
|
if (is_local() &&
|
|
|
|
m_ses.get_pe_settings().out_enc_policy == pe_settings::enabled)
|
|
|
|
{
|
|
|
|
policy::peer* pi = peer_info_struct();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(pi);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2007-06-30 09:28:44 +02:00
|
|
|
pi->pe_support = true;
|
2007-06-06 02:41:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // #ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
|
|
|
|
if (m_state == read_protocol_identifier)
|
|
|
|
{
|
2008-09-21 19:12:26 +02:00
|
|
|
m_statistics.received_bytes(0, bytes_transferred);
|
|
|
|
bytes_transferred = 0;
|
2008-04-10 12:03:23 +02:00
|
|
|
TORRENT_ASSERT(packet_size() == 20);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
if (!packet_finished()) return;
|
|
|
|
recv_buffer = receive_buffer();
|
|
|
|
|
|
|
|
int packet_size = recv_buffer[0];
|
2006-04-25 23:04:48 +02:00
|
|
|
const char protocol_string[] = "BitTorrent protocol";
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
if (packet_size != 19 ||
|
|
|
|
!std::equal(recv_buffer.begin + 1, recv_buffer.begin + 19, protocol_string))
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2007-06-06 02:41:20 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
if (!is_local() && m_ses.get_pe_settings().in_enc_policy == pe_settings::disabled)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::no_incoming_encrypted);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
// Don't attempt to perform an encrypted handshake
|
|
|
|
// within an encrypted connection
|
|
|
|
if (!m_encrypted && !is_local())
|
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
2007-06-06 02:41:20 +02:00
|
|
|
(*m_logger) << " attempting encrypted connection\n";
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
2007-06-06 02:41:20 +02:00
|
|
|
m_state = read_pe_dhkey;
|
|
|
|
cut_receive_buffer(0, dh_key_len);
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!packet_finished());
|
2007-06-06 02:41:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-10 12:03:23 +02:00
|
|
|
TORRENT_ASSERT((!is_local() && m_encrypted) || is_local());
|
2007-06-06 02:41:20 +02:00
|
|
|
#endif // #ifndef TORRENT_DISABLE_ENCRYPTION
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_info_hash, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
2008-04-10 12:03:23 +02:00
|
|
|
TORRENT_ASSERT(m_state != read_pe_dhkey);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
if (!is_local() &&
|
|
|
|
(m_ses.get_pe_settings().in_enc_policy == pe_settings::forced) &&
|
|
|
|
!m_encrypted)
|
2008-01-07 02:10:46 +01:00
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::no_incoming_regular);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << " BitTorrent protocol\n";
|
|
|
|
#endif
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
m_state = read_info_hash;
|
|
|
|
reset_recv_buffer(28);
|
|
|
|
}
|
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
// fall through
|
|
|
|
if (m_state == read_info_hash)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2008-09-21 19:12:26 +02:00
|
|
|
m_statistics.received_bytes(0, bytes_transferred);
|
|
|
|
bytes_transferred = 0;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(packet_size() == 28);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
if (!packet_finished()) return;
|
|
|
|
recv_buffer = receive_buffer();
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
for (int i=0; i < 8; ++i)
|
|
|
|
{
|
|
|
|
for (int j=0; j < 8; ++j)
|
|
|
|
{
|
|
|
|
if (recv_buffer[i] & (0x80 >> j)) (*m_logger) << "1";
|
|
|
|
else (*m_logger) << "0";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(*m_logger) << "\n";
|
|
|
|
if (recv_buffer[7] & 0x01)
|
|
|
|
(*m_logger) << "supports DHT port message\n";
|
2007-06-06 02:41:20 +02:00
|
|
|
if (recv_buffer[7] & 0x04)
|
2006-11-30 12:56:19 +01:00
|
|
|
(*m_logger) << "supports FAST extensions\n";
|
2006-04-25 23:04:48 +02:00
|
|
|
if (recv_buffer[5] & 0x10)
|
2006-11-30 12:56:19 +01:00
|
|
|
(*m_logger) << "supports extensions protocol\n";
|
2006-04-25 23:04:48 +02:00
|
|
|
#endif
|
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifndef DISABLE_EXTENSIONS
|
2009-02-13 08:17:20 +01:00
|
|
|
std::memcpy(m_reserved_bits, recv_buffer.begin, 8);
|
2006-11-14 01:08:16 +01:00
|
|
|
if ((recv_buffer[5] & 0x10))
|
2006-04-25 23:04:48 +02:00
|
|
|
m_supports_extensions = true;
|
2006-11-14 01:08:16 +01:00
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
if (recv_buffer[7] & 0x01)
|
|
|
|
m_supports_dht_port = true;
|
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
if (recv_buffer[7] & 0x04)
|
|
|
|
m_supports_fast = true;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
// ok, now we have got enough of the handshake. Is this connection
|
|
|
|
// attached to a torrent?
|
|
|
|
if (!t)
|
|
|
|
{
|
|
|
|
// now, we have to see if there's a torrent with the
|
|
|
|
// info_hash we got from the peer
|
|
|
|
sha1_hash info_hash;
|
|
|
|
std::copy(recv_buffer.begin + 8, recv_buffer.begin + 28
|
|
|
|
, (char*)info_hash.begin());
|
|
|
|
|
|
|
|
attach_to_torrent(info_hash);
|
2008-01-07 02:10:46 +01:00
|
|
|
if (is_disconnecting()) return;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// verify info hash
|
|
|
|
if (!std::equal(recv_buffer.begin + 8, recv_buffer.begin + 28
|
|
|
|
, (const char*)t->torrent_file().info_hash().begin()))
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << " received invalid info_hash\n";
|
|
|
|
#endif
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::invalid_info_hash, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << " info_hash received\n";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-06-07 12:18:13 +02:00
|
|
|
t = associated_torrent().lock();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t);
|
2007-06-07 12:18:13 +02:00
|
|
|
|
2007-06-21 02:51:42 +02:00
|
|
|
// if this is a local connection, we have already
|
2007-08-14 19:47:48 +02:00
|
|
|
// sent the handshake
|
2007-06-21 02:51:42 +02:00
|
|
|
if (!is_local()) write_handshake();
|
2007-08-14 19:47:48 +02:00
|
|
|
// if (t->valid_metadata())
|
2008-06-07 04:58:28 +02:00
|
|
|
// write_bitfield();
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-01-07 02:10:46 +01:00
|
|
|
if (is_disconnecting()) return;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t->get_policy().has_connection(this));
|
2006-09-12 01:34:07 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
m_state = read_peer_id;
|
2007-06-06 02:41:20 +02:00
|
|
|
reset_recv_buffer(20);
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
// fall through
|
|
|
|
if (m_state == read_peer_id)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2008-09-21 19:12:26 +02:00
|
|
|
m_statistics.received_bytes(0, bytes_transferred);
|
|
|
|
bytes_transferred = 0;
|
2007-06-06 02:41:20 +02:00
|
|
|
if (!t)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!packet_finished()); // TODO
|
2007-06-06 02:41:20 +02:00
|
|
|
return;
|
|
|
|
}
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(packet_size() == 20);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
if (!packet_finished()) return;
|
|
|
|
recv_buffer = receive_buffer();
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
{
|
2009-04-04 11:52:25 +02:00
|
|
|
char hex_pid[41];
|
|
|
|
to_hex(recv_buffer.begin, 20, hex_pid);
|
|
|
|
char ascii_pid[21];
|
|
|
|
for (int i = 0; i != 20; ++i)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2009-06-23 03:53:47 +02:00
|
|
|
if (is_print(recv_buffer.begin[i])) ascii_pid[i] = recv_buffer.begin[i];
|
2009-04-04 11:52:25 +02:00
|
|
|
else ascii_pid[i] = '.';
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2009-04-04 11:52:25 +02:00
|
|
|
char msg[200];
|
|
|
|
snprintf(msg, 200, "received peer_id: %s client: %s\nas ascii: %s\n"
|
|
|
|
, hex_pid, identify_client(peer_id(recv_buffer.begin)).c_str(), ascii_pid);
|
|
|
|
(*m_logger) << msg;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
peer_id pid;
|
|
|
|
std::copy(recv_buffer.begin, recv_buffer.begin + 20, (char*)pid.begin());
|
|
|
|
set_pid(pid);
|
2006-11-30 12:56:19 +01:00
|
|
|
|
|
|
|
if (t->settings().allow_multiple_connections_per_ip)
|
|
|
|
{
|
|
|
|
// now, let's see if this connection should be closed
|
|
|
|
policy& p = t->get_policy();
|
|
|
|
policy::iterator i = std::find_if(p.begin_peer(), p.end_peer()
|
|
|
|
, match_peer_id(pid, this));
|
|
|
|
if (i != p.end_peer())
|
|
|
|
{
|
2009-05-07 00:36:24 +02:00
|
|
|
TORRENT_ASSERT((*i)->connection->pid() == pid);
|
2006-11-30 12:56:19 +01:00
|
|
|
// we found another connection with the same peer-id
|
|
|
|
// which connection should be closed in order to be
|
|
|
|
// sure that the other end closes the same connection?
|
|
|
|
// the peer with greatest peer-id is the one allowed to
|
|
|
|
// initiate connections. So, if our peer-id is greater than
|
|
|
|
// the others, we should close the incoming connection,
|
|
|
|
// if not, we should close the outgoing one.
|
|
|
|
if (pid < m_ses.get_peer_id() && is_local())
|
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
(*i)->connection->disconnect(errors::duplicate_peer_id);
|
2006-11-30 12:56:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::duplicate_peer_id);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
2006-11-30 12:56:19 +01:00
|
|
|
}
|
|
|
|
}
|
2007-05-25 19:06:30 +02:00
|
|
|
}
|
|
|
|
|
2009-05-23 23:42:29 +02:00
|
|
|
// disconnect if the peer has the same peer-id as ourself
|
|
|
|
// since it most likely is ourself then
|
2007-05-25 19:06:30 +02:00
|
|
|
if (pid == m_ses.get_peer_id())
|
|
|
|
{
|
2009-08-02 00:48:43 +02:00
|
|
|
if (peer_info_struct()) t->get_policy().ban_peer(peer_info_struct());
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::self_connection, 1);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
2006-11-30 12:56:19 +01:00
|
|
|
}
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
m_client_version = identify_client(pid);
|
2006-05-15 00:30:05 +02:00
|
|
|
boost::optional<fingerprint> f = client_fingerprint(pid);
|
|
|
|
if (f && std::equal(f->name, f->name + 2, "BC"))
|
|
|
|
{
|
|
|
|
// if this is a bitcomet client, lower the request queue size limit
|
|
|
|
if (m_max_out_request_queue > 50) m_max_out_request_queue = 50;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2006-11-14 01:08:16 +01:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
|
|
|
for (extension_list_t::iterator i = m_extensions.begin()
|
|
|
|
, end(m_extensions.end()); i != end;)
|
|
|
|
{
|
2008-05-12 05:05:27 +02:00
|
|
|
if (!(*i)->on_handshake(m_reserved_bits))
|
2006-11-14 01:08:16 +01:00
|
|
|
{
|
|
|
|
i = m_extensions.erase(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2008-05-12 05:05:27 +02:00
|
|
|
if (is_disconnecting()) return;
|
2006-11-14 01:08:16 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (m_supports_extensions) write_extensions();
|
2006-11-14 01:08:16 +01:00
|
|
|
#endif
|
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_LOGGING
|
|
|
|
(*m_logger) << time_now_string() << " <== HANDSHAKE\n";
|
|
|
|
#endif
|
2007-04-15 04:14:02 +02:00
|
|
|
// consider this a successful connection, reset the failcount
|
2009-08-02 00:48:43 +02:00
|
|
|
if (peer_info_struct()) t->get_policy().set_failcount(peer_info_struct(), 0);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
|
|
|
// Toggle pe_support back to false if this is a
|
|
|
|
// standard successful connection
|
|
|
|
if (is_local() && !m_encrypted &&
|
|
|
|
m_ses.get_pe_settings().out_enc_policy == pe_settings::enabled)
|
|
|
|
{
|
|
|
|
policy::peer* pi = peer_info_struct();
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(pi);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2007-06-30 09:28:44 +02:00
|
|
|
pi->pe_support = false;
|
2007-06-06 02:41:20 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
m_state = read_packet_size;
|
2008-04-10 12:03:23 +02:00
|
|
|
reset_recv_buffer(5);
|
2008-08-29 19:21:56 +02:00
|
|
|
if (t->ready_for_connections())
|
2007-08-14 19:47:48 +02:00
|
|
|
{
|
2008-06-07 04:58:28 +02:00
|
|
|
write_bitfield();
|
2007-08-14 19:47:48 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
if (m_supports_dht_port && m_ses.m_dht)
|
|
|
|
write_dht_port(m_ses.get_dht_settings().service_port);
|
|
|
|
#endif
|
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!packet_finished());
|
2007-06-06 02:41:20 +02:00
|
|
|
return;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
// cannot fall through into
|
|
|
|
if (m_state == read_packet_size)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2007-06-06 02:41:20 +02:00
|
|
|
// Make sure this is not fallen though into
|
2008-04-10 12:03:23 +02:00
|
|
|
TORRENT_ASSERT(recv_buffer == receive_buffer());
|
2008-09-21 19:12:26 +02:00
|
|
|
TORRENT_ASSERT(packet_size() == 5);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (!t) return;
|
2008-04-10 12:03:23 +02:00
|
|
|
|
2008-09-21 19:12:26 +02:00
|
|
|
if (recv_buffer.left() < 4)
|
|
|
|
{
|
|
|
|
m_statistics.received_bytes(0, bytes_transferred);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int transferred_used = 4 - recv_buffer.left() + bytes_transferred;
|
|
|
|
TORRENT_ASSERT(transferred_used <= int(bytes_transferred));
|
|
|
|
m_statistics.received_bytes(0, transferred_used);
|
|
|
|
bytes_transferred -= transferred_used;
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
const char* ptr = recv_buffer.begin;
|
|
|
|
int packet_size = detail::read_int32(ptr);
|
|
|
|
|
|
|
|
// don't accept packets larger than 1 MB
|
|
|
|
if (packet_size > 1024*1024 || packet_size < 0)
|
|
|
|
{
|
2008-09-21 19:12:26 +02:00
|
|
|
m_statistics.received_bytes(0, bytes_transferred);
|
2006-04-25 23:04:48 +02:00
|
|
|
// packet too large
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::packet_too_large, 2);
|
2008-01-07 02:10:46 +01:00
|
|
|
return;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (packet_size == 0)
|
|
|
|
{
|
2008-09-21 19:12:26 +02:00
|
|
|
m_statistics.received_bytes(0, bytes_transferred);
|
2006-04-25 23:04:48 +02:00
|
|
|
incoming_keepalive();
|
2008-05-12 05:05:27 +02:00
|
|
|
if (is_disconnecting()) return;
|
2006-04-25 23:04:48 +02:00
|
|
|
// keepalive message
|
|
|
|
m_state = read_packet_size;
|
2008-09-21 19:12:26 +02:00
|
|
|
cut_receive_buffer(4, 5);
|
2008-04-10 12:03:23 +02:00
|
|
|
return;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-04-10 12:03:23 +02:00
|
|
|
if (recv_buffer.left() < 5) return;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
m_state = read_packet;
|
2008-04-10 12:03:23 +02:00
|
|
|
cut_receive_buffer(4, packet_size);
|
2008-09-21 19:12:26 +02:00
|
|
|
TORRENT_ASSERT(bytes_transferred == 1);
|
2008-04-10 12:03:23 +02:00
|
|
|
recv_buffer = receive_buffer();
|
|
|
|
TORRENT_ASSERT(recv_buffer.left() == 1);
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
if (m_state == read_packet)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(recv_buffer == receive_buffer());
|
2008-10-09 05:33:53 +02:00
|
|
|
if (!t)
|
|
|
|
{
|
|
|
|
m_statistics.received_bytes(0, bytes_transferred);
|
2009-11-29 08:06:38 +01:00
|
|
|
disconnect(errors::torrent_removed, 1);
|
2008-10-09 05:33:53 +02:00
|
|
|
return;
|
|
|
|
}
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2008-10-09 05:33:53 +02:00
|
|
|
size_type cur_payload_dl = m_statistics.last_payload_downloaded();
|
|
|
|
size_type cur_protocol_dl = m_statistics.last_protocol_downloaded();
|
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
if (dispatch_message(bytes_transferred))
|
|
|
|
{
|
|
|
|
m_state = read_packet_size;
|
2008-04-10 12:03:23 +02:00
|
|
|
reset_recv_buffer(5);
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2008-10-09 05:33:53 +02:00
|
|
|
TORRENT_ASSERT(m_statistics.last_payload_downloaded() - cur_payload_dl >= 0);
|
|
|
|
TORRENT_ASSERT(m_statistics.last_protocol_downloaded() - cur_protocol_dl >= 0);
|
|
|
|
size_type stats_diff = m_statistics.last_payload_downloaded() - cur_payload_dl +
|
|
|
|
m_statistics.last_protocol_downloaded() - cur_protocol_dl;
|
|
|
|
TORRENT_ASSERT(stats_diff == bytes_transferred);
|
|
|
|
#endif
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!packet_finished());
|
2007-06-06 02:41:20 +02:00
|
|
|
return;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!packet_finished());
|
2007-06-06 02:41:20 +02:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
// --------------------------
|
|
|
|
// SEND DATA
|
|
|
|
// --------------------------
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void bt_peer_connection::on_sent(error_code const& error
|
2006-04-25 23:04:48 +02:00
|
|
|
, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-09-21 19:12:26 +02:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
m_statistics.sent_bytes(0, bytes_transferred);
|
|
|
|
return;
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
|
|
|
// manage the payload markers
|
|
|
|
int amount_payload = 0;
|
|
|
|
if (!m_payloads.empty())
|
|
|
|
{
|
2009-05-03 22:21:24 +02:00
|
|
|
for (std::vector<range>::iterator i = m_payloads.begin();
|
2006-04-25 23:04:48 +02:00
|
|
|
i != m_payloads.end(); ++i)
|
|
|
|
{
|
|
|
|
i->start -= bytes_transferred;
|
|
|
|
if (i->start < 0)
|
|
|
|
{
|
|
|
|
if (i->start + i->length <= 0)
|
|
|
|
{
|
|
|
|
amount_payload += i->length;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
amount_payload += -i->start;
|
|
|
|
i->length -= -i->start;
|
|
|
|
i->start = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: move the erasing into the loop above
|
|
|
|
// remove all payload ranges that has been sent
|
|
|
|
m_payloads.erase(
|
|
|
|
std::remove_if(m_payloads.begin(), m_payloads.end(), range_below_zero)
|
|
|
|
, m_payloads.end());
|
|
|
|
|
2009-02-12 04:57:53 +01:00
|
|
|
#if defined TORRENT_DEBUG && !defined TORRENT_DISABLE_ENCRYPTION
|
2009-01-28 05:20:05 +01:00
|
|
|
if (m_encrypted_bytes > 0)
|
|
|
|
{
|
2009-02-04 06:51:00 +01:00
|
|
|
if (m_rc4_encrypted)
|
|
|
|
{
|
|
|
|
m_encrypted_bytes -= bytes_transferred;
|
|
|
|
TORRENT_ASSERT(m_encrypted_bytes >= 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_encrypted_bytes -= (std::min)(int(bytes_transferred), m_encrypted_bytes);
|
|
|
|
}
|
2009-01-28 05:20:05 +01:00
|
|
|
TORRENT_ASSERT(m_encrypted_bytes >= 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(amount_payload <= (int)bytes_transferred);
|
2006-04-25 23:04:48 +02:00
|
|
|
m_statistics.sent_bytes(amount_payload, bytes_transferred - amount_payload);
|
|
|
|
}
|
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2006-04-25 23:04:48 +02:00
|
|
|
void bt_peer_connection::check_invariant() const
|
|
|
|
{
|
2009-11-29 20:38:32 +01:00
|
|
|
boost::shared_ptr<torrent> t = associated_torrent().lock();
|
|
|
|
|
|
|
|
if (!m_disconnect_started && m_initialized)
|
|
|
|
{
|
|
|
|
// none of this matters if we're disconnecting anyway
|
|
|
|
if (t->is_finished())
|
|
|
|
TORRENT_ASSERT(!is_interesting());
|
|
|
|
if (is_seed())
|
|
|
|
TORRENT_ASSERT(upload_only());
|
|
|
|
}
|
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
#ifndef TORRENT_DISABLE_ENCRYPTION
|
2008-06-28 12:10:05 +02:00
|
|
|
TORRENT_ASSERT( (bool(m_state != read_pe_dhkey) || m_dh_key_exchange.get())
|
2007-06-07 12:18:13 +02:00
|
|
|
|| !is_local());
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_rc4_encrypted || m_RC4_handler.get());
|
2007-06-06 02:41:20 +02:00
|
|
|
#endif
|
2007-11-19 08:07:57 +01:00
|
|
|
if (!in_handshake())
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_sent_handshake);
|
|
|
|
}
|
|
|
|
|
2006-04-30 02:39:18 +02:00
|
|
|
if (!m_payloads.empty())
|
|
|
|
{
|
2009-05-03 22:21:24 +02:00
|
|
|
for (std::vector<range>::const_iterator i = m_payloads.begin();
|
2006-04-30 02:39:18 +02:00
|
|
|
i != m_payloads.end() - 1; ++i)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(i->start + i->length <= (i+1)->start);
|
2006-04-30 02:39:18 +02:00
|
|
|
}
|
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|