Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow execution of on_unknown_torrent method in the absence of active torrents #7537

Merged
merged 1 commit into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* allow on_unknown_torrent method in the absence of active torrents (new plugin feature added)
* fix missing python converter for dht::announce_flags_t

2.0.9 released
Expand Down
6 changes: 4 additions & 2 deletions include/libtorrent/aux_/session_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Copyright (c) 2015-2020, Alden Torres
Copyright (c) 2015, Thomas
Copyright (c) 2016-2017, Pavel Pimenov
Copyright (c) 2020, Paul-Louis Ageneau
Copyright (c) 2023, Joris Carrier
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -349,7 +350,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 <typename Fun, typename... Args>
Expand Down Expand Up @@ -1330,7 +1332,7 @@ namespace aux {

#ifndef TORRENT_DISABLE_EXTENSIONS
// this is a list to allow extensions to potentially remove themselves.
std::array<std::vector<std::shared_ptr<plugin>>, 4> m_ses_extensions;
std::array<std::vector<std::shared_ptr<plugin>>, 5> m_ses_extensions;
#endif

#if TORRENT_ABI_VERSION == 1
Expand Down
4 changes: 4 additions & 0 deletions include/libtorrent/extensions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 13 additions & 3 deletions src/session_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Copyright (c) 2020, Fonic
Copyright (c) 2020, Paul-Louis Ageneau
Copyright (c) 2022, Vladimir Golovnev (glassez)
Copyright (c) 2022, thrnz
Copyright (c) 2023, Joris Carrier
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -992,6 +993,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());
Expand Down Expand Up @@ -3084,9 +3087,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 ]");
Expand Down Expand Up @@ -3138,9 +3147,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<torrent> const& i)
Expand Down
Loading