From 80cd560118ab8c224d8221e21cbf7a1f5eea028e Mon Sep 17 00:00:00 2001 From: Joris CARRIER Date: Fri, 17 Nov 2023 15:03:03 +0100 Subject: [PATCH] allow execution of on_unknown_torrent method in the absence of active torrents --- include/libtorrent/aux_/session_impl.hpp | 5 +++-- include/libtorrent/extensions.hpp | 4 ++++ src/session.cpp | 1 + src/session_impl.cpp | 15 ++++++++++++--- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index b32c2c75fb0..60e1844afb5 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -349,7 +349,8 @@ namespace aux { plugins_all_idx = 0, // to store all plugins plugins_optimistic_unchoke_idx = 1, // optimistic_unchoke_feature plugins_tick_idx = 2, // tick_feature - plugins_dht_request_idx = 3 // dht_request_feature + plugins_dht_request_idx = 3, // dht_request_feature + plugins_unknown_torrent_idx = 4 // unknown_torrent_feature }; template @@ -1330,7 +1331,7 @@ namespace aux { #ifndef TORRENT_DISABLE_EXTENSIONS // this is a list to allow extensions to potentially remove themselves. - std::array>, 4> m_ses_extensions; + std::array>, 5> m_ses_extensions; #endif #if TORRENT_ABI_VERSION == 1 diff --git a/include/libtorrent/extensions.hpp b/include/libtorrent/extensions.hpp index 708c2edc4a4..8302b9d2896 100644 --- a/include/libtorrent/extensions.hpp +++ b/include/libtorrent/extensions.hpp @@ -211,6 +211,10 @@ TORRENT_VERSION_NAMESPACE_3 // called static constexpr feature_flags_t alert_feature = 4_bit; + // include this bit if your plugin needs to have on_unknown_torrent() + // called even if there is no active torrent in the session + static constexpr feature_flags_t unknown_torrent_feature = 5_bit; + // This function is expected to return a bitmask indicating which features // this plugin implements. Some callbacks on this object may not be called // unless the corresponding feature flag is returned here. Note that diff --git a/src/session.cpp b/src/session.cpp index e0428bfa255..db43896ae8c 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -53,6 +53,7 @@ namespace libtorrent { constexpr feature_flags_t plugin::tick_feature; constexpr feature_flags_t plugin::dht_request_feature; constexpr feature_flags_t plugin::alert_feature; + constexpr feature_flags_t plugin::unknown_torrent_feature; #endif namespace aux { diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 1e1c34e6640..11a4a73827a 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -992,6 +992,8 @@ bool ssl_server_name_callback(ssl::stream_handle_type stream_handle, std::string m_ses_extensions[plugins_tick_idx].push_back(ext); if (features & plugin::dht_request_feature) m_ses_extensions[plugins_dht_request_idx].push_back(ext); + if (features & plugin::unknown_torrent_feature) + m_ses_extensions[plugins_unknown_torrent_idx].push_back(ext); if (features & plugin::alert_feature) m_alerts.add_extension(ext); session_handle h(shared_from_this()); @@ -3084,9 +3086,15 @@ namespace { return; } + bool want_on_unknown_torrent = false; +#ifndef TORRENT_DISABLE_EXTENSIONS + want_on_unknown_torrent = !m_ses_extensions[plugins_unknown_torrent_idx].empty(); +#endif + // check if we have any active torrents + // or if there is an extension that wants on_unknown_torrent // if we don't reject the connection - if (m_torrents.empty()) + if (m_torrents.empty() && !want_on_unknown_torrent) { #ifndef TORRENT_DISABLE_LOGGING session_log("<== INCOMING CONNECTION [ rejected, there are no torrents ]"); @@ -3138,9 +3146,10 @@ namespace { // if we don't have any active torrents, there's no // point in accepting this connection. If, however, // the setting to start up queued torrents when they - // get an incoming connection is enabled, we cannot + // get an incoming connection is enabled or if there is + // an extension that wants on_unknown_torrent, we cannot // perform this check. - if (!m_settings.get_bool(settings_pack::incoming_starts_queued_torrents)) + if (!m_settings.get_bool(settings_pack::incoming_starts_queued_torrents) && !want_on_unknown_torrent) { bool has_active_torrent = std::any_of(m_torrents.begin(), m_torrents.end() , [](std::shared_ptr const& i)