2015-07-02 06:13:26 +02:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2015-2016, Arvid Norberg, Steven Siloti
|
2015-07-02 06:13:26 +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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libtorrent/peer_connection_handle.hpp"
|
|
|
|
#include "libtorrent/bt_peer_connection.hpp"
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-07-10 05:17:55 +02:00
|
|
|
#include <cstdarg> // for va_start, va_end
|
2015-07-02 06:13:26 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
2016-09-24 19:47:17 +02:00
|
|
|
connection_type peer_connection_handle::type() const
|
2015-07-02 06:13:26 +02:00
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->type();
|
|
|
|
}
|
|
|
|
|
2016-08-17 20:30:24 +02:00
|
|
|
void peer_connection_handle::add_extension(std::shared_ptr<peer_plugin> ext)
|
2015-07-02 06:13:26 +02:00
|
|
|
{
|
2015-07-13 05:17:45 +02:00
|
|
|
#ifndef TORRENT_DISABLE_EXTENSIONS
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
pc->add_extension(ext);
|
2015-08-22 00:28:12 +02:00
|
|
|
#else
|
|
|
|
TORRENT_UNUSED(ext);
|
2015-07-13 05:17:45 +02:00
|
|
|
#endif
|
2015-07-02 06:13:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool peer_connection_handle::is_seed() const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->is_seed();
|
|
|
|
}
|
|
|
|
|
2015-07-13 00:44:47 +02:00
|
|
|
bool peer_connection_handle::upload_only() const
|
2015-07-02 06:13:26 +02:00
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
2015-07-13 00:44:47 +02:00
|
|
|
return pc->upload_only();
|
2015-07-02 06:13:26 +02:00
|
|
|
}
|
|
|
|
|
2015-07-13 00:44:47 +02:00
|
|
|
peer_id const& peer_connection_handle::pid() const
|
2015-07-02 06:13:26 +02:00
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
2015-07-13 00:44:47 +02:00
|
|
|
return pc->pid();
|
2015-07-02 06:13:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool peer_connection_handle::has_piece(int i) const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->has_piece(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool peer_connection_handle::is_interesting() const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->is_interesting();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool peer_connection_handle::is_choked() const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->is_choked();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool peer_connection_handle::is_peer_interested() const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->is_peer_interested();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool peer_connection_handle::has_peer_choked() const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->has_peer_choked();
|
|
|
|
}
|
|
|
|
|
|
|
|
void peer_connection_handle::choke_this_peer()
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
pc->choke_this_peer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void peer_connection_handle::maybe_unchoke_this_peer()
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
pc->maybe_unchoke_this_peer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void peer_connection_handle::get_peer_info(peer_info& p) const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
pc->get_peer_info(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
torrent_handle peer_connection_handle::associated_torrent() const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
if (!pc) return torrent_handle();
|
2016-08-31 14:27:36 +02:00
|
|
|
std::shared_ptr<torrent> t = pc->associated_torrent().lock();
|
2015-07-02 06:13:26 +02:00
|
|
|
if (!t) return torrent_handle();
|
|
|
|
return t->get_handle();
|
|
|
|
}
|
|
|
|
|
|
|
|
tcp::endpoint const& peer_connection_handle::remote() const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->remote();
|
|
|
|
}
|
|
|
|
|
|
|
|
tcp::endpoint peer_connection_handle::local_endpoint() const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->local_endpoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
void peer_connection_handle::disconnect(error_code const& ec, operation_t op, int error)
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
pc->disconnect(ec, op, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool peer_connection_handle::is_disconnecting() const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->is_disconnecting();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool peer_connection_handle::is_connecting() const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->is_connecting();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool peer_connection_handle::is_outgoing() const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->is_outgoing();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool peer_connection_handle::on_local_network() const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->on_local_network();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool peer_connection_handle::ignore_unchoke_slots() const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->ignore_unchoke_slots();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool peer_connection_handle::failed() const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->failed();
|
|
|
|
}
|
|
|
|
|
2016-09-13 23:07:22 +02:00
|
|
|
bool peer_connection_handle::should_log(peer_log_alert::direction_t direction) const
|
|
|
|
{
|
2016-10-04 06:35:40 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-09-13 23:07:22 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->should_log(direction);
|
2016-10-04 06:35:40 +02:00
|
|
|
#else
|
|
|
|
TORRENT_UNUSED(direction);
|
|
|
|
return false;
|
|
|
|
#endif
|
2016-09-13 23:07:22 +02:00
|
|
|
}
|
|
|
|
|
2015-08-16 18:17:23 +02:00
|
|
|
TORRENT_FORMAT(4,5)
|
2015-07-02 06:13:26 +02:00
|
|
|
void peer_connection_handle::peer_log(peer_log_alert::direction_t direction
|
|
|
|
, char const* event, char const* fmt, ...) const
|
|
|
|
{
|
2016-10-04 06:35:40 +02:00
|
|
|
#ifndef TORRENT_DISABLE_LOGGING
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
va_list v;
|
|
|
|
va_start(v, fmt);
|
2015-09-02 07:30:40 +02:00
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wformat-nonliteral"
|
2016-08-03 06:35:40 +02:00
|
|
|
#pragma clang diagnostic ignored "-Wclass-varargs"
|
2015-09-02 07:30:40 +02:00
|
|
|
#endif
|
2015-07-02 06:13:26 +02:00
|
|
|
pc->peer_log(direction, event, fmt, v);
|
2015-09-02 07:30:40 +02:00
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#endif
|
2015-07-02 06:13:26 +02:00
|
|
|
va_end(v);
|
2016-10-04 06:35:40 +02:00
|
|
|
#else // TORRENT_DISABLE_LOGGING
|
|
|
|
TORRENT_UNUSED(direction);
|
|
|
|
TORRENT_UNUSED(event);
|
|
|
|
TORRENT_UNUSED(fmt);
|
|
|
|
#endif
|
2015-07-13 05:17:45 +02:00
|
|
|
}
|
2015-07-02 06:13:26 +02:00
|
|
|
|
|
|
|
bool peer_connection_handle::can_disconnect(error_code const& ec) const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->can_disconnect(ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool peer_connection_handle::has_metadata() const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->has_metadata();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool peer_connection_handle::in_handshake() const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->in_handshake();
|
|
|
|
}
|
|
|
|
|
|
|
|
void peer_connection_handle::send_buffer(char const* begin, int size, int flags)
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
pc->send_buffer(begin, size, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t peer_connection_handle::last_seen_complete() const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->last_seen_complete();
|
|
|
|
}
|
|
|
|
|
|
|
|
time_point peer_connection_handle::time_of_last_unchoke() const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->time_of_last_unchoke();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bt_peer_connection_handle::packet_finished() const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<bt_peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->packet_finished();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bt_peer_connection_handle::support_extensions() const
|
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<bt_peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->support_extensions();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bt_peer_connection_handle::supports_encryption() const
|
|
|
|
{
|
2015-07-13 05:17:45 +02:00
|
|
|
#if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS)
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<bt_peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
return pc->supports_encryption();
|
2015-07-13 05:17:45 +02:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2015-07-02 06:13:26 +02:00
|
|
|
}
|
|
|
|
|
2016-08-17 20:30:24 +02:00
|
|
|
void bt_peer_connection_handle::switch_send_crypto(std::shared_ptr<crypto_plugin> crypto)
|
2015-07-02 06:13:26 +02:00
|
|
|
{
|
2015-07-13 05:17:45 +02:00
|
|
|
#if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS)
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<bt_peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
pc->switch_send_crypto(crypto);
|
2015-08-22 00:28:12 +02:00
|
|
|
#else
|
|
|
|
TORRENT_UNUSED(crypto);
|
2015-07-13 05:17:45 +02:00
|
|
|
#endif
|
2015-07-02 06:13:26 +02:00
|
|
|
}
|
|
|
|
|
2016-08-17 20:30:24 +02:00
|
|
|
void bt_peer_connection_handle::switch_recv_crypto(std::shared_ptr<crypto_plugin> crypto)
|
2015-07-02 06:13:26 +02:00
|
|
|
{
|
2015-07-13 05:17:45 +02:00
|
|
|
#if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS)
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<bt_peer_connection> pc = native_handle();
|
2015-07-02 06:13:26 +02:00
|
|
|
TORRENT_ASSERT(pc);
|
|
|
|
pc->switch_recv_crypto(crypto);
|
2015-08-22 00:28:12 +02:00
|
|
|
#else
|
|
|
|
TORRENT_UNUSED(crypto);
|
2015-07-13 00:44:47 +02:00
|
|
|
#endif
|
2015-07-13 05:17:45 +02:00
|
|
|
}
|
2015-07-13 00:44:47 +02:00
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<bt_peer_connection> bt_peer_connection_handle::native_handle() const
|
2015-07-13 00:44:47 +02:00
|
|
|
{
|
2016-09-01 03:42:18 +02:00
|
|
|
return std::static_pointer_cast<bt_peer_connection>(
|
2015-07-13 00:44:47 +02:00
|
|
|
peer_connection_handle::native_handle());
|
|
|
|
}
|
2015-07-02 06:13:26 +02:00
|
|
|
|
|
|
|
} // namespace libtorrent
|