diff --git a/include/libtorrent/http_seed_connection.hpp b/include/libtorrent/http_seed_connection.hpp index 14c3352e5..8f594fdf6 100644 --- a/include/libtorrent/http_seed_connection.hpp +++ b/include/libtorrent/http_seed_connection.hpp @@ -72,7 +72,8 @@ namespace libtorrent { std::string const& url() const override { return m_url; } void get_specific_peer_info(peer_info& p) const override; - void disconnect(error_code const& ec, operation_t op, int error = 0) override; + void disconnect(error_code const& ec, operation_t op + , disconnect_severity_t error = peer_connection_interface::normal) override; void write_request(peer_request const& r) override; diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index d2f5b6fad..42fda5713 100644 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -65,6 +65,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/vector.hpp" #include "libtorrent/disk_interface.hpp" #include "libtorrent/piece_picker.hpp" // for picker_options_t +#include "libtorrent/units.hpp" #include #include @@ -455,7 +456,7 @@ namespace aux { // this will cause this peer_connection to be disconnected. void disconnect(error_code const& ec - , operation_t op, int error = 0) override; + , operation_t op, disconnect_severity_t = peer_connection_interface::normal) override; // called when a connect attempt fails (not when an // established connection fails) diff --git a/include/libtorrent/peer_connection_handle.hpp b/include/libtorrent/peer_connection_handle.hpp index f34bd0f0b..c0509f68a 100644 --- a/include/libtorrent/peer_connection_handle.hpp +++ b/include/libtorrent/peer_connection_handle.hpp @@ -79,7 +79,8 @@ struct TORRENT_EXPORT peer_connection_handle tcp::endpoint const& remote() const; tcp::endpoint local_endpoint() const; - void disconnect(error_code const& ec, operation_t op, int error = 0); + void disconnect(error_code const& ec, operation_t op + , disconnect_severity_t = peer_connection_interface::normal); bool is_disconnecting() const; bool is_connecting() const; bool is_outgoing() const; diff --git a/include/libtorrent/peer_connection_interface.hpp b/include/libtorrent/peer_connection_interface.hpp index 20958d310..cada62f06 100644 --- a/include/libtorrent/peer_connection_interface.hpp +++ b/include/libtorrent/peer_connection_interface.hpp @@ -38,19 +38,26 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/error_code.hpp" #include "libtorrent/alert_types.hpp" #include "libtorrent/operations.hpp" // for operation_t enum +#include "libtorrent/units.hpp" namespace libtorrent { struct torrent_peer; class stat; + using disconnect_severity_t = aux::strong_typedef; + // TODO: make this interface smaller! struct TORRENT_EXTRA_EXPORT peer_connection_interface { + static constexpr disconnect_severity_t normal{0}; + static constexpr disconnect_severity_t failure{1}; + static constexpr disconnect_severity_t peer_error{2}; + virtual tcp::endpoint const& remote() const = 0; virtual tcp::endpoint local_endpoint() const = 0; virtual void disconnect(error_code const& ec - , operation_t op, int error = 0) = 0; + , operation_t op, disconnect_severity_t = peer_connection_interface::normal) = 0; virtual peer_id const& pid() const = 0; virtual peer_id our_pid() const = 0; virtual void set_holepunch_mode() = 0; diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 57c577c96..da79b027d 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -634,7 +634,7 @@ namespace libtorrent { void retry_web_seed(peer_connection* p, boost::optional retry = boost::none); void remove_web_seed_conn(peer_connection* p, error_code const& ec - , operation_t op, int error = 0); + , operation_t op, disconnect_severity_t error = peer_connection_interface::normal); std::set web_seeds(web_seed_entry::type_t type) const; diff --git a/include/libtorrent/web_peer_connection.hpp b/include/libtorrent/web_peer_connection.hpp index 0dcab4ef3..ee99f7ef2 100644 --- a/include/libtorrent/web_peer_connection.hpp +++ b/include/libtorrent/web_peer_connection.hpp @@ -74,7 +74,7 @@ namespace libtorrent { void get_specific_peer_info(peer_info& p) const override; void disconnect(error_code const& ec - , operation_t op, int error = 0) override; + , operation_t op, disconnect_severity_t error = peer_connection_interface::normal) override; void write_request(peer_request const& r) override; diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index 9312b44b9..88ed1dae4 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -794,7 +794,7 @@ namespace { received_bytes(0, received); if (m_recv_buffer.packet_size() != 1) { - disconnect(errors::invalid_choke, operation_t::bittorrent, 2); + disconnect(errors::invalid_choke, operation_t::bittorrent, peer_error); return; } if (!m_recv_buffer.packet_finished()) return; @@ -844,7 +844,7 @@ namespace { received_bytes(0, received); if (m_recv_buffer.packet_size() != 1) { - disconnect(errors::invalid_unchoke, operation_t::bittorrent, 2); + disconnect(errors::invalid_unchoke, operation_t::bittorrent, peer_error); return; } if (!m_recv_buffer.packet_finished()) return; @@ -864,7 +864,7 @@ namespace { received_bytes(0, received); if (m_recv_buffer.packet_size() != 1) { - disconnect(errors::invalid_interested, operation_t::bittorrent, 2); + disconnect(errors::invalid_interested, operation_t::bittorrent, peer_error); return; } if (!m_recv_buffer.packet_finished()) return; @@ -893,7 +893,7 @@ namespace { received_bytes(0, received); if (m_recv_buffer.packet_size() != 1) { - disconnect(errors::invalid_not_interested, operation_t::bittorrent, 2); + disconnect(errors::invalid_not_interested, operation_t::bittorrent, peer_error); return; } if (!m_recv_buffer.packet_finished()) return; @@ -913,7 +913,7 @@ namespace { received_bytes(0, received); if (m_recv_buffer.packet_size() != 5) { - disconnect(errors::invalid_have, operation_t::bittorrent, 2); + disconnect(errors::invalid_have, operation_t::bittorrent, peer_error); return; } if (!m_recv_buffer.packet_finished()) return; @@ -945,7 +945,7 @@ namespace { if (t->valid_metadata() && m_recv_buffer.packet_size() - 1 != (t->torrent_file().num_pieces() + CHAR_BIT - 1) / CHAR_BIT) { - disconnect(errors::invalid_bitfield_size, operation_t::bittorrent, 2); + disconnect(errors::invalid_bitfield_size, operation_t::bittorrent, peer_error); return; } @@ -972,7 +972,7 @@ namespace { received_bytes(0, received); if (m_recv_buffer.packet_size() != 13) { - disconnect(errors::invalid_request, operation_t::bittorrent, 2); + disconnect(errors::invalid_request, operation_t::bittorrent, peer_error); return; } if (!m_recv_buffer.packet_finished()) return; @@ -1023,13 +1023,13 @@ namespace { if (list_size > m_recv_buffer.packet_size() - 13) { - disconnect(errors::invalid_hash_list, operation_t::bittorrent, 2); + disconnect(errors::invalid_hash_list, operation_t::bittorrent, peer_error); return; } if (m_recv_buffer.packet_size() - 13 - list_size > t->block_size()) { - disconnect(errors::packet_too_large, operation_t::bittorrent, 2); + disconnect(errors::packet_too_large, operation_t::bittorrent, peer_error); return; } } @@ -1040,7 +1040,7 @@ namespace { { if (m_recv_buffer.packet_size() - 9 > t->block_size()) { - disconnect(errors::packet_too_large, operation_t::bittorrent, 2); + disconnect(errors::packet_too_large, operation_t::bittorrent, peer_error); return; } } @@ -1130,7 +1130,7 @@ namespace { if (bdecode(recv_buffer.begin() + 13, recv_buffer.begin() + 13 + list_size , hash_list, ec) != 0) { - disconnect(errors::invalid_hash_piece, operation_t::bittorrent, 2); + disconnect(errors::invalid_hash_piece, operation_t::bittorrent, peer_error); return; } @@ -1138,7 +1138,7 @@ namespace { // [ [node-index, hash], [node-index, hash], ... ] if (hash_list.type() != bdecode_node::list_t) { - disconnect(errors::invalid_hash_list, operation_t::bittorrent, 2); + disconnect(errors::invalid_hash_list, operation_t::bittorrent, peer_error); return; } @@ -1157,7 +1157,7 @@ namespace { } if (!nodes.empty() && !t->add_merkle_nodes(nodes, p.piece)) { - disconnect(errors::invalid_hash_piece, operation_t::bittorrent, 2); + disconnect(errors::invalid_hash_piece, operation_t::bittorrent, peer_error); return; } } @@ -1177,7 +1177,7 @@ namespace { received_bytes(0, received); if (m_recv_buffer.packet_size() != 13) { - disconnect(errors::invalid_cancel, operation_t::bittorrent, 2); + disconnect(errors::invalid_cancel, operation_t::bittorrent, peer_error); return; } if (!m_recv_buffer.packet_finished()) return; @@ -1205,7 +1205,7 @@ namespace { received_bytes(0, received); if (m_recv_buffer.packet_size() != 3) { - disconnect(errors::invalid_dht_port, operation_t::bittorrent, 2); + disconnect(errors::invalid_dht_port, operation_t::bittorrent, peer_error); return; } if (!m_recv_buffer.packet_finished()) return; @@ -1231,7 +1231,7 @@ namespace { received_bytes(0, received); if (!m_supports_fast) { - disconnect(errors::invalid_suggest, operation_t::bittorrent, 2); + disconnect(errors::invalid_suggest, operation_t::bittorrent, peer_error); return; } @@ -1251,7 +1251,7 @@ namespace { received_bytes(0, received); if (!m_supports_fast) { - disconnect(errors::invalid_have_all, operation_t::bittorrent, 2); + disconnect(errors::invalid_have_all, operation_t::bittorrent, peer_error); return; } incoming_have_all(); @@ -1264,7 +1264,7 @@ namespace { received_bytes(0, received); if (!m_supports_fast) { - disconnect(errors::invalid_have_none, operation_t::bittorrent, 2); + disconnect(errors::invalid_have_none, operation_t::bittorrent, peer_error); return; } incoming_have_none(); @@ -1277,7 +1277,7 @@ namespace { received_bytes(0, received); if (!m_supports_fast) { - disconnect(errors::invalid_reject, operation_t::bittorrent, 2); + disconnect(errors::invalid_reject, operation_t::bittorrent, peer_error); return; } @@ -1301,7 +1301,7 @@ namespace { received_bytes(0, received); if (!m_supports_fast) { - disconnect(errors::invalid_allow_fast, operation_t::bittorrent, 2); + disconnect(errors::invalid_allow_fast, operation_t::bittorrent, peer_error); return; } @@ -1540,13 +1540,13 @@ namespace { received_bytes(0, received); if (m_recv_buffer.packet_size() < 2) { - disconnect(errors::invalid_extended, operation_t::bittorrent, 2); + disconnect(errors::invalid_extended, operation_t::bittorrent, peer_error); return; } if (associated_torrent().expired()) { - disconnect(errors::invalid_extended, operation_t::bittorrent, 2); + disconnect(errors::invalid_extended, operation_t::bittorrent, peer_error); return; } @@ -1646,7 +1646,7 @@ namespace { } #endif - disconnect(errors::invalid_message, operation_t::bittorrent, 2); + disconnect(errors::invalid_message, operation_t::bittorrent, peer_error); } void bt_peer_connection::on_extended_handshake() @@ -2366,7 +2366,7 @@ namespace { if (!m_recv_buffer.crypto_packet_finished() && m_recv_buffer.crypto_packet_size() > 1025 * 1024) { - disconnect(errors::packet_too_large, operation_t::encryption, 2); + disconnect(errors::packet_too_large, operation_t::encryption, peer_error); return; } @@ -2481,7 +2481,7 @@ namespace { received_bytes(0, int(bytes_transferred)); if (m_recv_buffer.packet_finished()) - disconnect(errors::sync_hash_not_found, operation_t::bittorrent, 1); + disconnect(errors::sync_hash_not_found, operation_t::bittorrent, failure); return; } @@ -2518,7 +2518,7 @@ namespace { m_sync_bytes_read += bytes_processed; if (m_sync_bytes_read >= 512) { - disconnect(errors::sync_hash_not_found, operation_t::encryption, 1); + disconnect(errors::sync_hash_not_found, operation_t::encryption, failure); return; } @@ -2594,7 +2594,7 @@ namespace { if (!m_rc4) { - disconnect(errors::invalid_info_hash, operation_t::bittorrent, 1); + disconnect(errors::invalid_info_hash, operation_t::bittorrent, failure); return; } @@ -2604,7 +2604,7 @@ namespace { static const char sh_vc[] = {0,0,0,0, 0,0,0,0}; if (!std::equal(sh_vc, sh_vc + 8, recv_buffer.begin() + 20)) { - disconnect(errors::invalid_encryption_constant, operation_t::encryption, 2); + disconnect(errors::invalid_encryption_constant, operation_t::encryption, peer_error); return; } @@ -2628,7 +2628,7 @@ namespace { { received_bytes(0, int(bytes_transferred)); if (m_recv_buffer.packet_finished()) - disconnect(errors::invalid_encryption_constant, operation_t::encryption, 2); + disconnect(errors::invalid_encryption_constant, operation_t::encryption, peer_error); return; } @@ -2659,7 +2659,7 @@ namespace { if (m_sync_bytes_read >= 512) { - disconnect(errors::invalid_encryption_constant, operation_t::encryption, 2); + disconnect(errors::invalid_encryption_constant, operation_t::encryption, peer_error); return; } @@ -2747,7 +2747,7 @@ namespace { if (crypto_select == 0) { - disconnect(errors::unsupported_encryption_mode, operation_t::encryption, 1); + disconnect(errors::unsupported_encryption_mode, operation_t::encryption, failure); return; } @@ -2763,7 +2763,7 @@ namespace { if (crypto_field == 0) { // we don't allow any of the offered encryption levels - disconnect(errors::unsupported_encryption_mode_selected, operation_t::encryption, 2); + disconnect(errors::unsupported_encryption_mode_selected, operation_t::encryption, peer_error); return; } @@ -2776,7 +2776,7 @@ namespace { int const len_pad = aux::read_int16(recv_buffer); if (len_pad < 0 || len_pad > 512) { - disconnect(errors::invalid_pad_size, operation_t::encryption, 2); + disconnect(errors::invalid_pad_size, operation_t::encryption, peer_error); return; } @@ -2821,7 +2821,7 @@ namespace { if (len_ia < 0) { - disconnect(errors::invalid_encrypt_handshake, operation_t::encryption, 2); + disconnect(errors::invalid_encrypt_handshake, operation_t::encryption, peer_error); return; } @@ -2955,7 +2955,7 @@ namespace { peer_log(peer_log_alert::info, "ENCRYPTION" , "SSL peers are not allowed to use any other encryption"); #endif - disconnect(errors::invalid_info_hash, operation_t::bittorrent, 1); + disconnect(errors::invalid_info_hash, operation_t::bittorrent, failure); return; } #endif // TORRENT_USE_OPENSSL @@ -2974,7 +2974,7 @@ namespace { // handshake by this point if (m_encrypted || is_outgoing()) { - disconnect(errors::invalid_info_hash, operation_t::bittorrent, 1); + disconnect(errors::invalid_info_hash, operation_t::bittorrent, failure); return; } @@ -2986,7 +2986,7 @@ namespace { TORRENT_ASSERT(!m_recv_buffer.packet_finished()); return; #else - disconnect(errors::invalid_info_hash, operation_t::bittorrent, 1); + disconnect(errors::invalid_info_hash, operation_t::bittorrent, failure); return; #endif // TORRENT_DISABLE_ENCRYPTION } @@ -3080,7 +3080,7 @@ namespace { #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::info, "ERROR", "received invalid info_hash"); #endif - disconnect(errors::invalid_info_hash, operation_t::bittorrent, 1); + disconnect(errors::invalid_info_hash, operation_t::bittorrent, failure); return; } @@ -3274,7 +3274,7 @@ namespace { { // packet too large received_bytes(0, int(bytes_transferred)); - disconnect(errors::packet_too_large, operation_t::bittorrent, 2); + disconnect(errors::packet_too_large, operation_t::bittorrent, peer_error); return; } @@ -3305,7 +3305,7 @@ namespace { if (!t) { received_bytes(0, int(bytes_transferred)); - disconnect(errors::torrent_removed, operation_t::bittorrent, 1); + disconnect(errors::torrent_removed, operation_t::bittorrent, failure); return; } #if TORRENT_USE_ASSERTS diff --git a/src/http_seed_connection.cpp b/src/http_seed_connection.cpp index 4e8eea030..3df3490ea 100644 --- a/src/http_seed_connection.cpp +++ b/src/http_seed_connection.cpp @@ -80,7 +80,7 @@ namespace libtorrent { } void http_seed_connection::disconnect(error_code const& ec - , operation_t const op, int const error) + , operation_t const op, disconnect_severity_t const error) { if (is_disconnecting()) return; @@ -231,7 +231,7 @@ namespace libtorrent { if (m_requests.empty()) { received_bytes(0, int(bytes_transferred)); - disconnect(errors::http_error, operation_t::bittorrent, 2); + disconnect(errors::http_error, operation_t::bittorrent, peer_error); return; } @@ -255,7 +255,7 @@ namespace libtorrent { if (parse_error) { received_bytes(0, int(bytes_transferred)); - disconnect(errors::http_parse_error, operation_t::bittorrent, 2); + disconnect(errors::http_parse_error, operation_t::bittorrent, peer_error); return; } @@ -288,7 +288,7 @@ namespace libtorrent { , error_msg); } received_bytes(0, int(bytes_transferred)); - disconnect(error_code(m_parser.status_code(), http_category()), operation_t::bittorrent, 1); + disconnect(error_code(m_parser.status_code(), http_category()), operation_t::bittorrent, failure); return; } if (!m_parser.header_finished()) @@ -312,13 +312,13 @@ namespace libtorrent { if (location.empty()) { // we should not try this server again. - t->remove_web_seed_conn(this, errors::missing_location, operation_t::bittorrent, 2); + t->remove_web_seed_conn(this, errors::missing_location, operation_t::bittorrent, peer_error); return; } // add the redirected url and remove the current one t->add_web_seed(location, web_seed_entry::http_seed); - t->remove_web_seed_conn(this, errors::redirecting, operation_t::bittorrent, 2); + t->remove_web_seed_conn(this, errors::redirecting, operation_t::bittorrent, peer_error); return; } @@ -337,14 +337,14 @@ namespace libtorrent { { received_bytes(0, int(bytes_transferred)); // we should not try this server again. - t->remove_web_seed_conn(this, errors::no_content_length, operation_t::bittorrent, 2); + t->remove_web_seed_conn(this, errors::no_content_length, operation_t::bittorrent, peer_error); return; } if (m_response_left != front_request.length) { received_bytes(0, int(bytes_transferred)); // we should not try this server again. - t->remove_web_seed_conn(this, errors::invalid_range, operation_t::bittorrent, 2); + t->remove_web_seed_conn(this, errors::invalid_range, operation_t::bittorrent, peer_error); return; } m_body_start = m_parser.body_start(); @@ -423,7 +423,7 @@ namespace libtorrent { received_bytes(0, int(bytes_transferred)); // temporarily unavailable, retry later t->retry_web_seed(this, seconds32(retry_time)); - disconnect(error_code(m_parser.status_code(), http_category()), operation_t::bittorrent, 1); + disconnect(error_code(m_parser.status_code(), http_category()), operation_t::bittorrent, failure); return; } diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 61ef2a949..79e04d82d 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -64,6 +64,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/has_block.hpp" #include "libtorrent/aux_/time.hpp" #include "libtorrent/buffer.hpp" +#include "libtorrent/aux_/array.hpp" #if TORRENT_USE_ASSERTS #include @@ -105,6 +106,10 @@ namespace libtorrent { constexpr piece_index_t piece_block_progress::invalid_index; + constexpr disconnect_severity_t peer_connection_interface::normal; + constexpr disconnect_severity_t peer_connection_interface::failure; + constexpr disconnect_severity_t peer_connection_interface::peer_error; + #if TORRENT_USE_ASSERTS bool peer_connection::is_single_thread() const { @@ -249,12 +254,12 @@ namespace libtorrent { peer_log(peer_log_alert::info, "PEER_ERROR" ,"error: %s" , e.what()); #endif - disconnect(error_code(), operation_t::unknown, 2); + disconnect(error_code(), operation_t::unknown, peer_error); } void peer_connection::on_error(error_code const& ec) { - disconnect(ec, operation_t::unknown, 2); + disconnect(ec, operation_t::unknown, peer_error); } void peer_connection::increase_est_reciprocation_rate() @@ -1247,7 +1252,7 @@ namespace libtorrent { m_ses.ban_ip(m_remote.address()); } #endif - disconnect(errors::invalid_info_hash, operation_t::bittorrent, 1); + disconnect(errors::invalid_info_hash, operation_t::bittorrent, failure); return; } @@ -1269,7 +1274,7 @@ namespace libtorrent { #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::info, "ATTACH", "rejected connection to paused torrent"); #endif - disconnect(errors::torrent_paused, operation_t::bittorrent, 2); + disconnect(errors::torrent_paused, operation_t::bittorrent, peer_error); return; } @@ -1283,7 +1288,7 @@ namespace libtorrent { #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::info, "ATTACH", "rejected regular connection to i2p torrent"); #endif - disconnect(errors::peer_banned, operation_t::bittorrent, 2); + disconnect(errors::peer_banned, operation_t::bittorrent, peer_error); return; } #endif // TORRENT_USE_I2P @@ -1884,7 +1889,7 @@ namespace libtorrent { peer_log(peer_log_alert::info, "ERROR", "have-metadata have_piece: %d size: %d" , static_cast(index), m_have_piece.size()); #endif - disconnect(errors::invalid_have, operation_t::bittorrent, 2); + disconnect(errors::invalid_have, operation_t::bittorrent, peer_error); return; } @@ -2008,7 +2013,7 @@ namespace libtorrent { // if we got an invalid message, abort if (index >= m_have_piece.end_index() || index < piece_index_t(0)) { - disconnect(errors::invalid_dont_have, operation_t::bittorrent, 2); + disconnect(errors::invalid_dont_have, operation_t::bittorrent, peer_error); return; } @@ -2085,7 +2090,7 @@ namespace libtorrent { , m_have_piece.size()); } #endif - disconnect(errors::invalid_bitfield_size, operation_t::bittorrent, 2); + disconnect(errors::invalid_bitfield_size, operation_t::bittorrent, peer_error); return; } @@ -2427,7 +2432,7 @@ namespace libtorrent { if (m_num_invalid_requests > 300 && !m_peer_choked && can_disconnect(errors::too_many_requests_when_choked)) { - disconnect(errors::too_many_requests_when_choked, operation_t::bittorrent, 2); + disconnect(errors::too_many_requests_when_choked, operation_t::bittorrent, peer_error); return; } #ifndef TORRENT_DISABLE_LOGGING @@ -2449,7 +2454,7 @@ namespace libtorrent { if (m_choked && fast_idx != -1 && m_accept_fast_piece_cnt[fast_idx] >= 3 * blocks_per_piece && can_disconnect(errors::too_many_requests_when_choked)) { - disconnect(errors::too_many_requests_when_choked, operation_t::bittorrent, 2); + disconnect(errors::too_many_requests_when_choked, operation_t::bittorrent, peer_error); return; } @@ -2468,7 +2473,7 @@ namespace libtorrent { if (aux::time_now() - seconds(2) > m_last_choke && can_disconnect(errors::too_many_requests_when_choked)) { - disconnect(errors::too_many_requests_when_choked, operation_t::bittorrent, 2); + disconnect(errors::too_many_requests_when_choked, operation_t::bittorrent, peer_error); return; } } @@ -2556,7 +2561,7 @@ namespace libtorrent { peer_log(peer_log_alert::info, "INVALID_PIECE", "piece: %d s: %d l: %d" , static_cast(r.piece), r.start, r.length); #endif - disconnect(errors::invalid_piece, operation_t::bittorrent, 2); + disconnect(errors::invalid_piece, operation_t::bittorrent, peer_error); return; } @@ -4081,7 +4086,7 @@ namespace libtorrent { m_peer_info->supports_utp = false; // reconnect immediately using TCP fast_reconnect(true); - disconnect(e, operation_t::connect, 0); + disconnect(e, operation_t::connect, normal); if (t && m_peer_info) { std::weak_ptr weak_t = t; @@ -4121,14 +4126,14 @@ namespace libtorrent { } #endif - disconnect(e, operation_t::connect, 1); + disconnect(e, operation_t::connect, failure); } // the error argument defaults to 0, which means deliberate disconnect // 1 means unexpected disconnect/error // 2 protocol error (client sent something invalid) void peer_connection::disconnect(error_code const& ec - , operation_t const op, int const error) + , operation_t const op, disconnect_severity_t const error) { TORRENT_ASSERT(is_single_thread()); #if TORRENT_USE_ASSERTS @@ -4154,21 +4159,10 @@ namespace libtorrent { #ifndef TORRENT_DISABLE_LOGGING if (should_log(peer_log_alert::info)) try { - switch (error) - { - case 0: - peer_log(peer_log_alert::info, "CONNECTION_CLOSED", "op: %d error: %s" - , static_cast(op), ec.message().c_str()); - break; - case 1: - peer_log(peer_log_alert::info, "CONNECTION_FAILED", "op: %d error: %s" - , static_cast(op), ec.message().c_str()); - break; - case 2: - peer_log(peer_log_alert::info, "PEER_ERROR" ,"op: %d error: %s" - , static_cast(op), ec.message().c_str()); - break; - } + static aux::array const str{{{ + "CONNECTION_CLOSED", "CONNECTION_FAILED", "PEER_ERROR"}}}; + peer_log(peer_log_alert::info, str[error], "op: %d error: %s" + , static_cast(op), ec.message().c_str()); if (ec == boost::asio::error::eof && !in_handshake() @@ -4194,7 +4188,7 @@ namespace libtorrent { // we cannot do this in a constructor TORRENT_ASSERT(m_in_constructor == false); - if (error > 0) + if (error > normal) { m_failed = true; } @@ -4211,7 +4205,7 @@ namespace libtorrent { // TORRENT_ASSERT(ec != error::invalid_argument || !m_outgoing); m_counters.inc_stats_counter(counters::disconnected_peers); - if (error == 2) m_counters.inc_stats_counter(counters::error_peers); + if (error == peer_error) m_counters.inc_stats_counter(counters::error_peers); if (ec == error::connection_reset) m_counters.inc_stats_counter(counters::connreset_peers); @@ -4264,7 +4258,7 @@ namespace libtorrent { if (ec == errors::timed_out_no_handshake) m_counters.inc_stats_counter(counters::connect_timeouts); - if (error > 0) + if (error > normal) { if (is_utp(*m_socket)) m_counters.inc_stats_counter(counters::error_utp_peers); else m_counters.inc_stats_counter(counters::error_tcp_peers); @@ -4337,14 +4331,14 @@ namespace libtorrent { { if (ec) { - if ((error > 1 || ec.category() == socks_category()) + if ((error > failure || ec.category() == socks_category()) && t->alerts().should_post()) { t->alerts().emplace_alert(handle, remote() , pid(), op, ec); } - if (error <= 1 && t->alerts().should_post()) + if (error <= failure && t->alerts().should_post()) { t->alerts().emplace_alert(handle , remote(), pid(), op, m_socket->type(), ec, close_reason); @@ -6110,7 +6104,7 @@ namespace libtorrent { if (m_remote == m_socket->local_endpoint(ec)) { - disconnect(errors::self_connection, operation_t::bittorrent, 1); + disconnect(errors::self_connection, operation_t::bittorrent, failure); return; } diff --git a/src/peer_connection_handle.cpp b/src/peer_connection_handle.cpp index 47fe9a5f6..2f8815bd2 100644 --- a/src/peer_connection_handle.cpp +++ b/src/peer_connection_handle.cpp @@ -169,7 +169,8 @@ tcp::endpoint peer_connection_handle::local_endpoint() const return pc->local_endpoint(); } -void peer_connection_handle::disconnect(error_code const& ec, operation_t op, int error) +void peer_connection_handle::disconnect(error_code const& ec, operation_t const op + , disconnect_severity_t const error) { std::shared_ptr pc = native_handle(); TORRENT_ASSERT(pc); diff --git a/src/peer_list.cpp b/src/peer_list.cpp index 9493b3fb9..e8f7074a7 100644 --- a/src/peer_list.cpp +++ b/src/peer_list.cpp @@ -643,9 +643,9 @@ namespace libtorrent { if (self_connection) { - c.disconnect(errors::self_connection, operation_t::bittorrent, 1); + c.disconnect(errors::self_connection, operation_t::bittorrent, peer_connection_interface::failure); TORRENT_ASSERT(i->connection->peer_info_struct() == i); - i->connection->disconnect(errors::self_connection, operation_t::bittorrent, 1); + i->connection->disconnect(errors::self_connection, operation_t::bittorrent, peer_connection_interface::failure); TORRENT_ASSERT(i->connection == nullptr); return false; } diff --git a/src/torrent.cpp b/src/torrent.cpp index 6303fae06..7a755429d 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -6552,7 +6552,7 @@ bool is_downloading_state(int const st) TORRENT_CATCH (std::exception const&) { TORRENT_ASSERT(m_iterating_connections == 0); - c->disconnect(errors::no_error, operation_t::bittorrent, 1); + c->disconnect(errors::no_error, operation_t::bittorrent, peer_connection_interface::failure); return false; } @@ -7215,7 +7215,8 @@ bool is_downloading_state(int const st) } } for (auto& p : seeds) - p->disconnect(errors::torrent_finished, operation_t::bittorrent, 0); + p->disconnect(errors::torrent_finished, operation_t::bittorrent + , peer_connection_interface::normal); } if (m_abort) return; @@ -9825,7 +9826,7 @@ bool is_downloading_state(int const st) } void torrent::remove_web_seed_conn(peer_connection* p, error_code const& ec - , operation_t const op, int const error) + , operation_t const op, disconnect_severity_t const error) { auto const i = std::find_if(m_web_seeds.begin(), m_web_seeds.end() , [p] (web_seed_t const& ws) { return ws.peer_info.connection == p; }); diff --git a/src/ut_metadata.cpp b/src/ut_metadata.cpp index 590c7cd18..d1c706826 100644 --- a/src/ut_metadata.cpp +++ b/src/ut_metadata.cpp @@ -303,7 +303,7 @@ namespace libtorrent {namespace { m_pc.peer_log(peer_log_alert::incoming_message, "UT_METADATA" , "packet too big %d", length); #endif - m_pc.disconnect(errors::invalid_metadata_message, operation_t::bittorrent, 2); + m_pc.disconnect(errors::invalid_metadata_message, operation_t::bittorrent, peer_connection_interface::peer_error); return true; } @@ -317,7 +317,7 @@ namespace libtorrent {namespace { m_pc.peer_log(peer_log_alert::incoming_message, "UT_METADATA" , "not a dictionary"); #endif - m_pc.disconnect(errors::invalid_metadata_message, operation_t::bittorrent, 2); + m_pc.disconnect(errors::invalid_metadata_message, operation_t::bittorrent, peer_connection_interface::peer_error); return true; } @@ -330,7 +330,7 @@ namespace libtorrent {namespace { m_pc.peer_log(peer_log_alert::incoming_message, "UT_METADATA" , "missing or invalid keys"); #endif - m_pc.disconnect(errors::invalid_metadata_message, operation_t::bittorrent, 2); + m_pc.disconnect(errors::invalid_metadata_message, operation_t::bittorrent, peer_connection_interface::peer_error); return true; } // TODO: make this an enum class diff --git a/src/ut_pex.cpp b/src/ut_pex.cpp index 9a081171b..82fe17af6 100644 --- a/src/ut_pex.cpp +++ b/src/ut_pex.cpp @@ -262,7 +262,7 @@ namespace libtorrent { namespace { if (length > 500 * 1024) { - m_pc.disconnect(errors::pex_message_too_large, operation_t::bittorrent, 2); + m_pc.disconnect(errors::pex_message_too_large, operation_t::bittorrent, peer_connection_interface::peer_error); return true; } @@ -287,7 +287,7 @@ namespace libtorrent { namespace { int const ret = bdecode(body.begin(), body.end(), pex_msg, ec); if (ret != 0 || pex_msg.type() != bdecode_node::dict_t) { - m_pc.disconnect(errors::invalid_pex_message, operation_t::bittorrent, 2); + m_pc.disconnect(errors::invalid_pex_message, operation_t::bittorrent, peer_connection_interface::peer_error); return true; } diff --git a/src/web_peer_connection.cpp b/src/web_peer_connection.cpp index dd0d99ade..4735c27ba 100644 --- a/src/web_peer_connection.cpp +++ b/src/web_peer_connection.cpp @@ -177,7 +177,7 @@ void web_peer_connection::on_connected() } void web_peer_connection::disconnect(error_code const& ec - , operation_t op, int error) + , operation_t op, disconnect_severity_t const error) { if (is_disconnecting()) return; @@ -241,7 +241,7 @@ void web_peer_connection::disconnect(error_code const& ec m_requests.clear(); } - if (m_web && !m_web->supports_keepalive && error == 0) + if (m_web && !m_web->supports_keepalive && error == peer_connection_interface::normal) { // if the web server doesn't support keepalive and we were // disconnected as a graceful EOF, reconnect right away @@ -589,7 +589,7 @@ void web_peer_connection::handle_error(int const bytes_left) , error_msg); } received_bytes(0, bytes_left); - disconnect(error_code(m_parser.status_code(), http_category()), operation_t::bittorrent, 1); + disconnect(error_code(m_parser.status_code(), http_category()), operation_t::bittorrent, failure); } void web_peer_connection::handle_redirect(int const bytes_left) @@ -605,7 +605,7 @@ void web_peer_connection::handle_redirect(int const bytes_left) if (location.empty()) { // we should not try this server again. - t->remove_web_seed_conn(this, errors::missing_location, operation_t::bittorrent, 2); + t->remove_web_seed_conn(this, errors::missing_location, operation_t::bittorrent, peer_error); m_web = nullptr; TORRENT_ASSERT(is_disconnecting()); return; @@ -635,7 +635,7 @@ void web_peer_connection::handle_redirect(int const bytes_left) if (ec) { // we should not try this server again. - disconnect(errors::missing_location, operation_t::bittorrent, 1); + disconnect(errors::missing_location, operation_t::bittorrent, failure); return; } @@ -679,7 +679,7 @@ void web_peer_connection::handle_redirect(int const bytes_left) if (m_web->have_files[file_index]) { m_web->have_files.clear_bit(file_index); - disconnect(errors::redirecting, operation_t::bittorrent, 2); + disconnect(errors::redirecting, operation_t::bittorrent, peer_error); } } else @@ -694,7 +694,7 @@ void web_peer_connection::handle_redirect(int const bytes_left) // this web seed doesn't have any files. Don't try to request from it // again this session m_web->have_files.resize(t->torrent_file().num_files(), false); - disconnect(errors::redirecting, operation_t::bittorrent, 2); + disconnect(errors::redirecting, operation_t::bittorrent, peer_error); m_web = nullptr; TORRENT_ASSERT(is_disconnecting()); } @@ -749,7 +749,7 @@ void web_peer_connection::on_receive(error_code const& error , "%*s", int(recv_buffer.size()), recv_buffer.data()); } #endif - disconnect(errors::http_parse_error, operation_t::bittorrent, 2); + disconnect(errors::http_parse_error, operation_t::bittorrent, peer_error); return; } @@ -841,7 +841,7 @@ void web_peer_connection::on_receive(error_code const& error { received_bytes(0, int(recv_buffer.size())); // we should not try this server again. - t->remove_web_seed_conn(this, ec, operation_t::bittorrent, 2); + t->remove_web_seed_conn(this, ec, operation_t::bittorrent, peer_error); m_web = nullptr; TORRENT_ASSERT(is_disconnecting()); return; @@ -864,7 +864,7 @@ void web_peer_connection::on_receive(error_code const& error , static_cast(file_req.file_index), file_req.start, file_req.start + file_req.length - 1); } #endif - disconnect(errors::invalid_range, operation_t::bittorrent, 2); + disconnect(errors::invalid_range, operation_t::bittorrent, peer_error); return; } @@ -894,7 +894,7 @@ void web_peer_connection::on_receive(error_code const& error , "received body: %d request size: %d" , m_received_body, file_req.length); #endif - disconnect(errors::invalid_range, operation_t::bittorrent, 2); + disconnect(errors::invalid_range, operation_t::bittorrent, peer_error); return; } incoming_payload(recv_buffer.data(), copy_size); @@ -956,7 +956,7 @@ void web_peer_connection::on_receive(error_code const& error , "received body: %d request size: %d" , m_received_body, file_req.length); #endif - disconnect(errors::invalid_range, operation_t::bittorrent, 2); + disconnect(errors::invalid_range, operation_t::bittorrent, peer_error); return; } // we just completed an HTTP file request. pop it from m_file_requests diff --git a/test/test_peer_list.cpp b/test/test_peer_list.cpp index 34d6dcb76..f617732bd 100644 --- a/test/test_peer_list.cpp +++ b/test/test_peer_list.cpp @@ -111,7 +111,7 @@ struct mock_peer_connection tcp::endpoint const& remote() const override { return m_remote; } tcp::endpoint local_endpoint() const override { return m_local; } void disconnect(error_code const& ec - , operation_t op, int error = 0) override; + , operation_t op, disconnect_severity_t error = peer_connection_interface::normal) override; peer_id const& pid() const override { return m_id; } peer_id our_pid() const override { return m_our_id; } void set_holepunch_mode() override {} @@ -149,7 +149,7 @@ struct mock_torrent }; void mock_peer_connection::disconnect(error_code const& - , operation_t, int /*error*/) + , operation_t, disconnect_severity_t) { m_torrent.m_p->connection_closed(*this, 0, m_torrent.m_state); auto const i = std::find(m_torrent.m_connections.begin(), m_torrent.m_connections.end()