Coverage Report

Created: 2025-08-28 06:22

/src/libtorrent/src/close_reason.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
3
Copyright (c) 2015-2017, 2019, Arvid Norberg
4
Copyright (c) 2016, Alden Torres
5
All rights reserved.
6
7
Redistribution and use in source and binary forms, with or without
8
modification, are permitted provided that the following conditions
9
are met:
10
11
    * Redistributions of source code must retain the above copyright
12
      notice, this list of conditions and the following disclaimer.
13
    * Redistributions in binary form must reproduce the above copyright
14
      notice, this list of conditions and the following disclaimer in
15
      the documentation and/or other materials provided with the distribution.
16
    * Neither the name of the author nor the names of its
17
      contributors may be used to endorse or promote products derived
18
      from this software without specific prior written permission.
19
20
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
POSSIBILITY OF SUCH DAMAGE.
31
32
*/
33
34
#include "libtorrent/close_reason.hpp"
35
#include "libtorrent/error.hpp"
36
#include "libtorrent/assert.hpp"
37
38
namespace libtorrent {
39
40
  close_reason_t error_to_close_reason(error_code const& ec)
41
0
  {
42
0
    if (ec.category() == libtorrent_category())
43
0
    {
44
0
#define TORRENT_MAP(error, close_reason) \
45
0
  case errors:: error : \
46
0
    return close_reason;
47
48
0
      switch (ec.value())
49
0
      {
50
0
        TORRENT_MAP(invalid_swarm_metadata, close_reason_t::invalid_metadata)
51
0
        TORRENT_MAP(session_is_closing, close_reason_t::torrent_removed)
52
0
        TORRENT_MAP(peer_sent_empty_piece, close_reason_t::invalid_piece_message)
53
0
        TORRENT_MAP(mismatching_info_hash, close_reason_t::invalid_info_hash)
54
0
        TORRENT_MAP(port_blocked, close_reason_t::port_blocked)
55
0
        TORRENT_MAP(destructing_torrent, close_reason_t::torrent_removed)
56
0
        TORRENT_MAP(timed_out, close_reason_t::timeout)
57
0
        TORRENT_MAP(upload_upload_connection, close_reason_t::upload_to_upload)
58
0
        TORRENT_MAP(uninteresting_upload_peer, close_reason_t::not_interested_upload_only)
59
0
        TORRENT_MAP(invalid_info_hash, close_reason_t::invalid_info_hash)
60
0
        TORRENT_MAP(torrent_paused, close_reason_t::torrent_removed)
61
0
        TORRENT_MAP(invalid_have, close_reason_t::invalid_have_message)
62
0
        TORRENT_MAP(invalid_bitfield_size, close_reason_t::invalid_bitfield_message)
63
0
        TORRENT_MAP(too_many_requests_when_choked, close_reason_t::request_when_choked)
64
0
        TORRENT_MAP(invalid_piece, close_reason_t::invalid_piece_message)
65
0
        TORRENT_MAP(invalid_piece_size, close_reason_t::invalid_piece_message)
66
0
        TORRENT_MAP(no_memory, close_reason_t::no_memory)
67
0
        TORRENT_MAP(torrent_aborted, close_reason_t::torrent_removed)
68
0
        TORRENT_MAP(self_connection, close_reason_t::self_connection)
69
0
        TORRENT_MAP(timed_out_no_interest, close_reason_t::timed_out_interest)
70
0
        TORRENT_MAP(timed_out_inactivity, close_reason_t::timed_out_activity)
71
0
        TORRENT_MAP(timed_out_no_handshake, close_reason_t::timed_out_handshake)
72
0
        TORRENT_MAP(timed_out_no_request, close_reason_t::timed_out_request)
73
0
        TORRENT_MAP(invalid_choke, close_reason_t::invalid_choke_message)
74
0
        TORRENT_MAP(invalid_unchoke, close_reason_t::invalid_unchoke_message)
75
0
        TORRENT_MAP(invalid_interested, close_reason_t::invalid_interested_message)
76
0
        TORRENT_MAP(invalid_not_interested, close_reason_t::invalid_not_interested_message)
77
0
        TORRENT_MAP(invalid_request, close_reason_t::invalid_request_message)
78
0
        TORRENT_MAP(invalid_hash_list, close_reason_t::invalid_message)
79
0
        TORRENT_MAP(invalid_hash_piece, close_reason_t::invalid_message)
80
0
        TORRENT_MAP(invalid_cancel, close_reason_t::invalid_cancel_message)
81
0
        TORRENT_MAP(invalid_dht_port, close_reason_t::invalid_dht_port_message)
82
0
        TORRENT_MAP(invalid_suggest, close_reason_t::invalid_suggest_message)
83
0
        TORRENT_MAP(invalid_have_all, close_reason_t::invalid_have_all_message)
84
0
        TORRENT_MAP(invalid_have_none, close_reason_t::invalid_have_none_message)
85
0
        TORRENT_MAP(invalid_reject, close_reason_t::invalid_reject_message)
86
0
        TORRENT_MAP(invalid_allow_fast, close_reason_t::invalid_allow_fast_message)
87
0
        TORRENT_MAP(invalid_extended, close_reason_t::invalid_extended_message)
88
0
        TORRENT_MAP(invalid_message, close_reason_t::invalid_message_id)
89
0
        TORRENT_MAP(sync_hash_not_found, close_reason_t::encryption_error)
90
0
        TORRENT_MAP(invalid_encryption_constant, close_reason_t::encryption_error)
91
0
        TORRENT_MAP(no_plaintext_mode, close_reason_t::protocol_blocked)
92
0
        TORRENT_MAP(no_rc4_mode, close_reason_t::protocol_blocked)
93
0
        TORRENT_MAP(unsupported_encryption_mode_selected, close_reason_t::protocol_blocked)
94
0
        TORRENT_MAP(invalid_pad_size, close_reason_t::encryption_error)
95
0
        TORRENT_MAP(invalid_encrypt_handshake, close_reason_t::encryption_error)
96
0
        TORRENT_MAP(no_incoming_encrypted, close_reason_t::protocol_blocked)
97
0
        TORRENT_MAP(no_incoming_regular, close_reason_t::protocol_blocked)
98
0
        TORRENT_MAP(duplicate_peer_id, close_reason_t::duplicate_peer_id)
99
0
        TORRENT_MAP(torrent_removed, close_reason_t::torrent_removed)
100
0
        TORRENT_MAP(packet_too_large, close_reason_t::message_too_big)
101
0
        TORRENT_MAP(torrent_not_ready, close_reason_t::torrent_removed)
102
0
        TORRENT_MAP(session_closing, close_reason_t::torrent_removed)
103
0
        TORRENT_MAP(optimistic_disconnect, close_reason_t::peer_churn)
104
0
        TORRENT_MAP(torrent_finished, close_reason_t::upload_to_upload)
105
0
        TORRENT_MAP(too_many_corrupt_pieces, close_reason_t::corrupt_pieces)
106
0
        TORRENT_MAP(too_many_connections, close_reason_t::too_many_connections)
107
0
        TORRENT_MAP(peer_banned, close_reason_t::blocked)
108
0
        TORRENT_MAP(stopping_torrent, close_reason_t::torrent_removed)
109
0
        TORRENT_MAP(metadata_too_large, close_reason_t::metadata_too_big)
110
0
        TORRENT_MAP(invalid_metadata_size, close_reason_t::metadata_too_big)
111
0
        TORRENT_MAP(invalid_metadata_request, close_reason_t::invalid_metadata_request_message)
112
0
        TORRENT_MAP(invalid_metadata_offset, close_reason_t::invalid_metadata_offset)
113
0
        TORRENT_MAP(invalid_metadata_message, close_reason_t::invalid_metadata_message)
114
0
        TORRENT_MAP(pex_message_too_large, close_reason_t::pex_message_too_big)
115
0
        TORRENT_MAP(invalid_pex_message, close_reason_t::invalid_pex_message)
116
0
        TORRENT_MAP(invalid_lt_tracker_message, close_reason_t::invalid_message)
117
0
        TORRENT_MAP(too_frequent_pex, close_reason_t::pex_too_frequent)
118
0
        TORRENT_MAP(invalid_dont_have, close_reason_t::invalid_dont_have_message)
119
0
        TORRENT_MAP(requires_ssl_connection, close_reason_t::protocol_blocked)
120
0
        TORRENT_MAP(invalid_ssl_cert, close_reason_t::blocked)
121
0
        TORRENT_MAP(not_an_ssl_torrent, close_reason_t::blocked)
122
0
        TORRENT_MAP(banned_by_port_filter, close_reason_t::port_blocked)
123
124
0
#ifdef TORRENT_USE_ASSERTS
125
0
        case errors::redirecting:
126
0
          return close_reason_t::none;
127
0
#endif
128
129
0
        default:
130
0
          return close_reason_t::none;
131
0
      }
132
0
    }
133
0
    else if (ec.category() == boost::asio::error::get_misc_category())
134
0
    {
135
0
      switch (ec.value())
136
0
      {
137
0
        case boost::asio::error::eof:
138
0
          return close_reason_t::none;
139
0
      }
140
0
    }
141
0
    else if (ec.category() == generic_category())
142
0
    {
143
0
      switch (ec.value())
144
0
      {
145
0
#ifdef TORRENT_USE_ASSERTS
146
0
        case boost::system::errc::connection_reset:
147
0
        case boost::system::errc::broken_pipe:
148
0
          return close_reason_t::none;
149
0
#endif
150
0
        case boost::system::errc::timed_out:
151
0
          return close_reason_t::timeout;
152
0
        case boost::system::errc::too_many_files_open:
153
0
        case boost::system::errc::too_many_files_open_in_system:
154
0
          return close_reason_t::too_many_files;
155
0
        case boost::system::errc::not_enough_memory:
156
0
        case boost::system::errc::no_buffer_space:
157
0
          return close_reason_t::no_memory;
158
0
      }
159
0
    }
160
0
    else if (ec.category() == http_category())
161
0
    {
162
0
      return close_reason_t::no_memory;
163
0
    }
164
165
0
    return close_reason_t::none;
166
0
  }
167
}