2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2008-2023 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2021-09-27 18:28:41 +00:00
|
|
|
#include <algorithm> // std::partial_sort(), std::min(), std::max()
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <climits> /* INT_MAX */
|
2022-03-04 22:43:56 +00:00
|
|
|
#include <condition_variable>
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <csignal>
|
|
|
|
#include <cstdint>
|
2022-07-27 21:53:39 +00:00
|
|
|
#include <cstdlib> // atoi()
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <ctime>
|
2022-11-10 18:04:49 +00:00
|
|
|
#include <future>
|
2022-08-06 01:37:21 +00:00
|
|
|
#include <iterator> // for std::back_inserter
|
2021-09-26 22:23:11 +00:00
|
|
|
#include <list>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <memory>
|
2022-08-06 01:37:21 +00:00
|
|
|
#include <numeric> // for std::accumulate()
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <string>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <string_view>
|
2021-11-09 03:30:03 +00:00
|
|
|
#include <unordered_set>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2021-09-15 14:32:07 +00:00
|
|
|
#include <vector>
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2014-12-13 15:22:39 +00:00
|
|
|
#ifndef _WIN32
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <sys/types.h> /* umask() */
|
|
|
|
#include <sys/stat.h> /* umask() */
|
2014-12-13 15:22:39 +00:00
|
|
|
#endif
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2010-12-24 08:58:41 +00:00
|
|
|
#include <event2/event.h>
|
2009-03-25 19:50:58 +00:00
|
|
|
|
2022-04-22 19:56:54 +00:00
|
|
|
#include <fmt/chrono.h>
|
2022-03-14 04:43:35 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
#include <fmt/format.h> // fmt::ptr
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2021-11-09 03:30:03 +00:00
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
#include "announcer.h"
|
2008-11-24 04:21:23 +00:00
|
|
|
#include "bandwidth.h"
|
2008-03-30 13:22:45 +00:00
|
|
|
#include "blocklist.h"
|
2010-06-19 14:25:11 +00:00
|
|
|
#include "cache.h"
|
2014-12-04 11:27:38 +00:00
|
|
|
#include "crypto-utils.h"
|
2015-04-11 10:51:59 +00:00
|
|
|
#include "error-types.h"
|
2021-11-09 03:30:03 +00:00
|
|
|
#include "error.h"
|
2014-07-08 00:08:43 +00:00
|
|
|
#include "file.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "net.h"
|
2009-05-14 13:42:29 +00:00
|
|
|
#include "peer-io.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "peer-mgr.h"
|
2008-04-11 17:01:13 +00:00
|
|
|
#include "port-forwarding.h"
|
2008-05-18 16:44:30 +00:00
|
|
|
#include "rpc-server.h"
|
2016-09-05 19:06:26 +00:00
|
|
|
#include "session-id.h"
|
2021-11-09 03:30:03 +00:00
|
|
|
#include "session.h"
|
2022-08-08 22:53:20 +00:00
|
|
|
#include "timer-ev.h"
|
2007-12-25 05:37:32 +00:00
|
|
|
#include "torrent.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2021-11-09 03:30:03 +00:00
|
|
|
#include "tr-lpd.h"
|
2022-03-26 00:31:27 +00:00
|
|
|
#include "tr-strbuf.h"
|
2011-02-18 00:33:11 +00:00
|
|
|
#include "tr-utp.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant.h"
|
2009-04-16 13:10:25 +00:00
|
|
|
#include "verify.h"
|
2009-07-01 14:58:57 +00:00
|
|
|
#include "version.h"
|
2008-04-24 01:42:53 +00:00
|
|
|
#include "web.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2021-11-13 00:10:04 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
namespace bandwidth_group_helpers
|
|
|
|
{
|
|
|
|
auto constexpr BandwidthGroupsFilename = "bandwidth-groups.json"sv;
|
|
|
|
|
|
|
|
void bandwidthGroupRead(tr_session* session, std::string_view config_dir)
|
|
|
|
{
|
|
|
|
auto const filename = tr_pathbuf{ config_dir, '/', BandwidthGroupsFilename };
|
|
|
|
auto groups_dict = tr_variant{};
|
|
|
|
if (!tr_sys_path_exists(filename) || !tr_variantFromFile(&groups_dict, TR_VARIANT_PARSE_JSON, filename, nullptr) ||
|
|
|
|
!tr_variantIsDict(&groups_dict))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto idx = size_t{ 0 };
|
|
|
|
auto key = tr_quark{};
|
|
|
|
tr_variant* dict = nullptr;
|
|
|
|
while (tr_variantDictChild(&groups_dict, idx, &key, &dict))
|
|
|
|
{
|
|
|
|
++idx;
|
|
|
|
|
|
|
|
auto name = tr_interned_string(key);
|
|
|
|
auto& group = session->getBandwidthGroup(name);
|
|
|
|
|
|
|
|
auto limits = tr_bandwidth_limits{};
|
|
|
|
tr_variantDictFindBool(dict, TR_KEY_uploadLimited, &limits.up_limited);
|
|
|
|
tr_variantDictFindBool(dict, TR_KEY_downloadLimited, &limits.down_limited);
|
|
|
|
|
|
|
|
if (auto limit = int64_t{}; tr_variantDictFindInt(dict, TR_KEY_uploadLimit, &limit))
|
|
|
|
{
|
|
|
|
limits.up_limit_KBps = static_cast<tr_kilobytes_per_second_t>(limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto limit = int64_t{}; tr_variantDictFindInt(dict, TR_KEY_downloadLimit, &limit))
|
|
|
|
{
|
|
|
|
limits.down_limit_KBps = static_cast<tr_kilobytes_per_second_t>(limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
group.setLimits(&limits);
|
|
|
|
|
|
|
|
if (auto honors = bool{}; tr_variantDictFindBool(dict, TR_KEY_honorsSessionLimits, &honors))
|
|
|
|
{
|
|
|
|
group.honorParentLimits(TR_UP, honors);
|
|
|
|
group.honorParentLimits(TR_DOWN, honors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tr_variantClear(&groups_dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
int bandwidthGroupWrite(tr_session const* session, std::string_view config_dir)
|
|
|
|
{
|
|
|
|
auto const& groups = session->bandwidthGroups();
|
|
|
|
|
|
|
|
auto groups_dict = tr_variant{};
|
|
|
|
tr_variantInitDict(&groups_dict, std::size(groups));
|
|
|
|
|
|
|
|
for (auto const& [name, group] : groups)
|
|
|
|
{
|
|
|
|
auto const limits = group->getLimits();
|
|
|
|
|
|
|
|
auto* const dict = tr_variantDictAddDict(&groups_dict, name.quark(), 5);
|
|
|
|
tr_variantDictAddStrView(dict, TR_KEY_name, name.sv());
|
|
|
|
tr_variantDictAddBool(dict, TR_KEY_uploadLimited, limits.up_limited);
|
|
|
|
tr_variantDictAddInt(dict, TR_KEY_uploadLimit, limits.up_limit_KBps);
|
|
|
|
tr_variantDictAddBool(dict, TR_KEY_downloadLimited, limits.down_limited);
|
|
|
|
tr_variantDictAddInt(dict, TR_KEY_downloadLimit, limits.down_limit_KBps);
|
|
|
|
tr_variantDictAddBool(dict, TR_KEY_honorsSessionLimits, group->areParentLimitsHonored(TR_UP));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const filename = tr_pathbuf{ config_dir, '/', BandwidthGroupsFilename };
|
|
|
|
auto const ret = tr_variantToFile(&groups_dict, TR_VARIANT_FMT_JSON, filename);
|
|
|
|
tr_variantClear(&groups_dict);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace bandwidth_group_helpers
|
2023-01-07 22:55:00 +00:00
|
|
|
|
|
|
|
void update_bandwidth(tr_session* session, tr_direction dir)
|
|
|
|
{
|
|
|
|
if (auto const limit_bytes_per_second = session->activeSpeedLimitBps(dir); limit_bytes_per_second)
|
|
|
|
{
|
|
|
|
session->top_bandwidth_.setLimited(dir, *limit_bytes_per_second > 0U);
|
|
|
|
session->top_bandwidth_.setDesiredSpeedBytesPerSecond(dir, *limit_bytes_per_second);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
session->top_bandwidth_.setLimited(dir, false);
|
|
|
|
}
|
|
|
|
}
|
2023-01-07 14:27:54 +00:00
|
|
|
} // namespace
|
2022-03-18 13:11:59 +00:00
|
|
|
|
2022-08-14 01:04:36 +00:00
|
|
|
tr_port tr_session::randomPort() const
|
2008-12-13 23:17:36 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
auto const lower = std::min(settings_.peer_port_random_low.host(), settings_.peer_port_random_high.host());
|
|
|
|
auto const upper = std::max(settings_.peer_port_random_low.host(), settings_.peer_port_random_high.host());
|
2022-04-21 15:58:13 +00:00
|
|
|
auto const range = upper - lower;
|
2022-12-21 15:58:32 +00:00
|
|
|
return tr_port::fromHost(lower + tr_rand_int(range + 1U));
|
2008-12-13 23:17:36 +00:00
|
|
|
}
|
2008-12-11 04:41:23 +00:00
|
|
|
|
2007-07-16 21:57:22 +00:00
|
|
|
/* Generate a peer id : "-TRxyzb-" + 12 random alphanumeric
|
|
|
|
characters, where x is the major version number, y is the
|
|
|
|
minor version number, z is the maintenance number, and b
|
|
|
|
designates beta (Azureus-style) */
|
2021-10-22 02:40:55 +00:00
|
|
|
tr_peer_id_t tr_peerIdInit()
|
2007-07-16 11:19:44 +00:00
|
|
|
{
|
2021-10-22 02:40:55 +00:00
|
|
|
auto peer_id = tr_peer_id_t{};
|
|
|
|
auto* it = std::data(peer_id);
|
|
|
|
|
|
|
|
// starts with -TRXXXX-
|
|
|
|
auto constexpr Prefix = std::string_view{ PEERID_PREFIX };
|
|
|
|
auto const* const end = it + std::size(peer_id);
|
|
|
|
it = std::copy_n(std::data(Prefix), std::size(Prefix), it);
|
|
|
|
|
|
|
|
// remainder is randomly-generated characters
|
|
|
|
auto constexpr Pool = std::string_view{ "0123456789abcdefghijklmnopqrstuvwxyz" };
|
|
|
|
auto total = int{ 0 };
|
|
|
|
tr_rand_buffer(it, end - it);
|
|
|
|
while (it + 1 < end)
|
2013-05-23 05:43:12 +00:00
|
|
|
{
|
2021-10-22 02:40:55 +00:00
|
|
|
int const val = *it % std::size(Pool);
|
2017-04-19 12:04:45 +00:00
|
|
|
total += val;
|
2021-10-22 02:40:55 +00:00
|
|
|
*it++ = Pool[val];
|
2008-01-07 06:19:34 +00:00
|
|
|
}
|
2021-10-22 02:40:55 +00:00
|
|
|
int const val = total % std::size(Pool) != 0 ? std::size(Pool) - total % std::size(Pool) : 0;
|
|
|
|
*it = Pool[val];
|
2007-07-28 04:10:09 +00:00
|
|
|
|
2021-10-22 02:40:55 +00:00
|
|
|
return peer_id;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-11-11 16:09:24 +00:00
|
|
|
|
|
|
|
std::vector<tr_torrent_id_t> tr_session::DhtMediator::torrentsAllowingDHT() const
|
|
|
|
{
|
|
|
|
auto ids = std::vector<tr_torrent_id_t>{};
|
|
|
|
auto const& torrents = session_.torrents();
|
|
|
|
|
|
|
|
ids.reserve(std::size(torrents));
|
|
|
|
for (auto const* const tor : torrents)
|
|
|
|
{
|
|
|
|
if (tor->isRunning && tor->allowsDht())
|
|
|
|
{
|
|
|
|
ids.push_back(tor->id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_sha1_digest_t tr_session::DhtMediator::torrentInfoHash(tr_torrent_id_t id) const
|
|
|
|
{
|
|
|
|
if (auto const* const tor = session_.torrents().get(id); tor != nullptr)
|
|
|
|
{
|
|
|
|
return tor->infoHash();
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_session::DhtMediator::addPex(tr_sha1_digest_t const& info_hash, tr_pex const* pex, size_t n_pex)
|
|
|
|
{
|
|
|
|
if (auto* const tor = session_.torrents().get(info_hash); tor != nullptr)
|
|
|
|
{
|
|
|
|
tr_peerMgrAddPex(tor, TR_PEER_FROM_DHT, pex, n_pex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-08-24 21:03:30 +00:00
|
|
|
bool tr_session::LpdMediator::onPeerFound(std::string_view info_hash_str, tr_address address, tr_port port)
|
|
|
|
{
|
|
|
|
auto const digest = tr_sha1_from_string(info_hash_str);
|
|
|
|
if (!digest)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_torrent* const tor = session_.torrents_.get(*digest);
|
|
|
|
if (!tr_isTorrent(tor) || !tor->allowsLpd())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we found a suitable peer, add it to the torrent
|
|
|
|
auto pex = tr_pex{ address, port };
|
|
|
|
tr_peerMgrAddPex(tor, TR_PEER_FROM_LPD, &pex, 1U);
|
2022-12-08 22:44:19 +00:00
|
|
|
tr_logAddDebugTor(tor, fmt::format(FMT_STRING("Found a local peer from LPD ({:s})"), address.display_name(port)));
|
2022-08-24 21:03:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<tr_lpd::Mediator::TorrentInfo> tr_session::LpdMediator::torrents() const
|
|
|
|
{
|
|
|
|
auto ret = std::vector<tr_lpd::Mediator::TorrentInfo>{};
|
|
|
|
ret.reserve(std::size(session_.torrents()));
|
|
|
|
for (auto const* const tor : session_.torrents())
|
|
|
|
{
|
|
|
|
auto info = tr_lpd::Mediator::TorrentInfo{};
|
|
|
|
info.info_hash_str = tor->infoHashString();
|
2022-11-22 20:00:09 +00:00
|
|
|
info.activity = tor->activity();
|
2022-08-24 21:03:30 +00:00
|
|
|
info.allows_lpd = tor->allowsLpd();
|
|
|
|
info.announce_after = tor->lpdAnnounceAt;
|
|
|
|
ret.emplace_back(info);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_session::LpdMediator::setNextAnnounceTime(std::string_view info_hash_str, time_t announce_after)
|
|
|
|
{
|
|
|
|
if (auto digest = tr_sha1_from_string(info_hash_str); digest)
|
|
|
|
{
|
|
|
|
if (tr_torrent* const tor = session_.torrents_.get(*digest); tr_isTorrent(tor))
|
|
|
|
{
|
|
|
|
tor->lpdAnnounceAt = announce_after;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2022-08-24 21:03:30 +00:00
|
|
|
|
2022-02-22 15:09:24 +00:00
|
|
|
std::optional<std::string> tr_session::WebMediator::cookieFile() const
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2022-07-23 07:13:18 +00:00
|
|
|
auto const path = tr_pathbuf{ session_->configDir(), "/cookies.txt"sv };
|
2022-03-28 22:13:32 +00:00
|
|
|
|
|
|
|
if (!tr_sys_path_exists(path))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::string{ path };
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 14:28:01 +00:00
|
|
|
std::optional<std::string_view> tr_session::WebMediator::userAgent() const
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2022-05-31 14:28:01 +00:00
|
|
|
return TR_NAME "/" SHORT_VERSION_STRING;
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
|
2022-08-21 23:37:38 +00:00
|
|
|
std::optional<std::string> tr_session::WebMediator::publicAddressV4() const
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2022-12-28 08:03:35 +00:00
|
|
|
if (auto const [addr, is_any] = session_->publicAddress(TR_AF_INET); !is_any)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2022-12-08 22:44:19 +00:00
|
|
|
return addr.display_name();
|
2022-08-21 23:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::string> tr_session::WebMediator::publicAddressV6() const
|
|
|
|
{
|
2022-12-28 08:03:35 +00:00
|
|
|
if (auto const [addr, is_any] = session_->publicAddress(TR_AF_INET6); !is_any)
|
2022-08-21 23:37:38 +00:00
|
|
|
{
|
2022-12-08 22:44:19 +00:00
|
|
|
return addr.display_name();
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2022-11-21 16:19:45 +00:00
|
|
|
size_t tr_session::WebMediator::clamp(int torrent_id, size_t byte_count) const
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
auto const lock = session_->unique_lock();
|
2022-03-18 16:34:29 +00:00
|
|
|
|
2022-03-01 23:06:29 +00:00
|
|
|
auto const* const tor = session_->torrents().get(torrent_id);
|
2022-03-18 16:34:29 +00:00
|
|
|
return tor == nullptr ? 0U : tor->bandwidth_.clamp(TR_DOWN, byte_count);
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 15:09:24 +00:00
|
|
|
void tr_session::WebMediator::notifyBandwidthConsumed(int torrent_id, size_t byte_count)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
auto const lock = session_->unique_lock();
|
2022-03-18 16:34:29 +00:00
|
|
|
|
|
|
|
if (auto* const tor = session_->torrents().get(torrent_id); tor != nullptr)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2022-03-18 16:34:29 +00:00
|
|
|
tor->bandwidth_.notifyBandwidthConsumed(TR_DOWN, byte_count, true, tr_time_msec());
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 15:09:24 +00:00
|
|
|
void tr_session::WebMediator::run(tr_web::FetchDoneFunc&& func, tr_web::FetchResponse&& response) const
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2022-11-04 21:20:27 +00:00
|
|
|
session_->runInSessionThread(std::move(func), std::move(response));
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 14:03:26 +00:00
|
|
|
time_t tr_session::WebMediator::now() const
|
|
|
|
{
|
|
|
|
return tr_time();
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
void tr_sessionFetch(tr_session* session, tr_web::FetchOptions&& options)
|
|
|
|
{
|
2022-10-14 02:25:02 +00:00
|
|
|
session->fetch(std::move(options));
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2022-02-16 18:33:50 +00:00
|
|
|
|
2022-06-15 22:19:24 +00:00
|
|
|
tr_encryption_mode tr_sessionGetEncryption(tr_session const* session)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-08-14 01:04:36 +00:00
|
|
|
return session->encryptionMode();
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetEncryption(tr_session* session, tr_encryption_mode mode)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(mode == TR_ENCRYPTION_PREFERRED || mode == TR_ENCRYPTION_REQUIRED || mode == TR_CLEAR_PREFERRED);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.encryption_mode = mode;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
void tr_session::onIncomingPeerConnection(tr_socket_t fd, void* vsession)
|
2009-05-14 13:42:29 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* session = static_cast<tr_session*>(vsession);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-10-14 06:20:39 +00:00
|
|
|
if (auto const incoming_info = tr_netAccept(session, fd); incoming_info)
|
2013-05-23 05:43:12 +00:00
|
|
|
{
|
2022-10-25 03:13:09 +00:00
|
|
|
auto const& [addr, port, sock] = *incoming_info;
|
2022-12-08 22:44:19 +00:00
|
|
|
tr_logAddTrace(fmt::format("new incoming connection {} ({})", sock, addr.display_name(port)));
|
2022-12-06 16:28:28 +00:00
|
|
|
session->addIncoming(tr_peer_socket{ session, addr, port, sock });
|
2009-05-14 13:42:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
tr_session::BoundSocket::BoundSocket(
|
|
|
|
event_base* evbase,
|
|
|
|
tr_address const& addr,
|
|
|
|
tr_port port,
|
|
|
|
IncomingCallback cb,
|
|
|
|
void* cb_data)
|
|
|
|
: cb_{ cb }
|
|
|
|
, cb_data_{ cb_data }
|
|
|
|
, socket_{ tr_netBindTCP(addr, port, false) }
|
2022-11-06 21:11:30 +00:00
|
|
|
, ev_{ event_new(evbase, socket_, EV_READ | EV_PERSIST, &BoundSocket::onCanRead, this) }
|
2009-05-14 13:42:29 +00:00
|
|
|
{
|
2022-11-06 16:35:48 +00:00
|
|
|
if (socket_ == TR_BAD_SOCKET)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-08-26 02:27:11 +00:00
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
tr_logAddInfo(
|
2022-12-08 22:44:19 +00:00
|
|
|
fmt::format(_("Listening to incoming peer connections on {hostport}"), fmt::arg("hostport", addr.display_name(port))));
|
2022-11-06 21:11:30 +00:00
|
|
|
event_add(ev_.get(), nullptr);
|
2022-11-06 16:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_session::BoundSocket::~BoundSocket()
|
|
|
|
{
|
2022-11-06 21:11:30 +00:00
|
|
|
ev_.reset();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-07-29 13:11:47 +00:00
|
|
|
if (socket_ != TR_BAD_SOCKET)
|
2013-05-23 05:43:12 +00:00
|
|
|
{
|
2023-01-04 21:37:55 +00:00
|
|
|
tr_net_close_socket(socket_);
|
2022-11-06 16:35:48 +00:00
|
|
|
socket_ = TR_BAD_SOCKET;
|
2009-05-14 13:42:29 +00:00
|
|
|
}
|
2022-07-29 13:11:47 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
tr_session::PublicAddressResult tr_session::publicAddress(tr_address_type type) const noexcept
|
2009-05-14 13:42:29 +00:00
|
|
|
{
|
2022-11-06 16:35:48 +00:00
|
|
|
if (type == TR_AF_INET)
|
2022-07-09 23:44:20 +00:00
|
|
|
{
|
2022-12-28 08:03:35 +00:00
|
|
|
// if user provided an address, use it.
|
|
|
|
// otherwise, use any_ipv4 (0.0.0.0).
|
2022-12-09 02:27:52 +00:00
|
|
|
static auto constexpr DefaultAddr = tr_address::any_ipv4();
|
|
|
|
auto addr = tr_address::from_string(settings_.bind_address_ipv4).value_or(DefaultAddr);
|
2022-11-06 16:35:48 +00:00
|
|
|
return { addr, addr == DefaultAddr };
|
|
|
|
}
|
2022-07-09 23:44:20 +00:00
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
if (type == TR_AF_INET6)
|
|
|
|
{
|
2022-12-28 08:03:35 +00:00
|
|
|
// if user provided an address, use it.
|
|
|
|
// otherwise, if we can determine which one to use via tr_globalIPv6 magic, use it.
|
|
|
|
// otherwise, use any_ipv6 (::).
|
|
|
|
static auto constexpr AnyAddr = tr_address::any_ipv6();
|
|
|
|
auto const default_addr = tr_globalIPv6().value_or(AnyAddr);
|
|
|
|
auto addr = tr_address::from_string(settings_.bind_address_ipv6).value_or(default_addr);
|
|
|
|
return { addr, addr == AnyAddr };
|
2022-07-09 23:44:20 +00:00
|
|
|
}
|
2022-11-06 16:35:48 +00:00
|
|
|
|
|
|
|
TR_ASSERT_MSG(false, "invalid type");
|
|
|
|
return {};
|
2009-05-14 13:42:29 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
// ---
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
namespace settings_helpers
|
|
|
|
{
|
|
|
|
|
|
|
|
void get_settings_filename(tr_pathbuf& setme, char const* config_dir, char const* appname)
|
|
|
|
{
|
|
|
|
if (!tr_str_is_empty(config_dir))
|
|
|
|
{
|
|
|
|
setme.assign(std::string_view{ config_dir }, "/settings.json"sv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const default_config_dir = tr_getDefaultConfigDir(appname);
|
|
|
|
setme.assign(std::string_view{ default_config_dir }, "/settings.json"sv);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace settings_helpers
|
|
|
|
} // namespace
|
2009-05-14 13:42:29 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionGetDefaultSettings(tr_variant* setme_dictionary)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
tr_session_settings{}.save(setme_dictionary);
|
|
|
|
tr_rpc_server::defaultSettings(setme_dictionary);
|
|
|
|
tr_session_alt_speeds::defaultSettings(setme_dictionary);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_sessionGetSettings(tr_session const* session, tr_variant* setme_dictionary)
|
|
|
|
{
|
|
|
|
session->settings_.save(setme_dictionary);
|
|
|
|
session->alt_speeds_.save(setme_dictionary);
|
|
|
|
session->rpc_server_->save(setme_dictionary);
|
2022-11-26 13:32:51 +00:00
|
|
|
|
|
|
|
tr_variantDictRemove(setme_dictionary, TR_KEY_message_level);
|
|
|
|
tr_variantDictAddInt(setme_dictionary, TR_KEY_message_level, tr_logGetLevel());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
bool tr_sessionLoadSettings(tr_variant* dict, char const* config_dir, char const* app_name)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-01-07 22:55:00 +00:00
|
|
|
using namespace settings_helpers;
|
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(tr_variantIsDict(dict));
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* initializing the defaults: caller may have passed in some app-level defaults.
|
|
|
|
* preserve those and use the session defaults to fill in any missing gaps. */
|
2022-09-07 16:04:28 +00:00
|
|
|
auto old_dict = *dict;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(dict, 0);
|
|
|
|
tr_sessionGetDefaultSettings(dict);
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_variantMergeDicts(dict, &old_dict);
|
|
|
|
tr_variantClear(&old_dict);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* file settings override the defaults */
|
2021-10-18 12:51:02 +00:00
|
|
|
auto success = bool{};
|
2022-07-23 01:10:02 +00:00
|
|
|
auto filename = tr_pathbuf{};
|
2023-01-07 22:55:00 +00:00
|
|
|
get_settings_filename(filename, config_dir, app_name);
|
2022-07-23 01:10:02 +00:00
|
|
|
if (!tr_sys_path_exists(filename))
|
|
|
|
{
|
|
|
|
success = true;
|
|
|
|
}
|
2022-09-07 16:04:28 +00:00
|
|
|
else if (auto file_settings = tr_variant{}; tr_variantFromFile(&file_settings, TR_VARIANT_PARSE_JSON, filename))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_variantMergeDicts(dict, &file_settings);
|
|
|
|
tr_variantClear(&file_settings);
|
2017-04-19 12:04:45 +00:00
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-23 01:10:02 +00:00
|
|
|
success = false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
return success;
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSaveSettings(tr_session* session, char const* config_dir, tr_variant const* client_settings)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-01-07 14:27:54 +00:00
|
|
|
using namespace bandwidth_group_helpers;
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
TR_ASSERT(tr_variantIsDict(client_settings));
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant settings;
|
2022-03-26 03:39:08 +00:00
|
|
|
auto const filename = tr_pathbuf{ config_dir, "/settings.json"sv };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_variantInitDict(&settings, 0);
|
|
|
|
|
|
|
|
/* the existing file settings are the fallback values */
|
2022-04-02 14:06:02 +00:00
|
|
|
if (auto file_settings = tr_variant{}; tr_variantFromFile(&file_settings, TR_VARIANT_PARSE_JSON, filename))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-02 14:06:02 +00:00
|
|
|
tr_variantMergeDicts(&settings, &file_settings);
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&file_settings);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the client's settings override the file settings */
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_variantMergeDicts(&settings, client_settings);
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the session's true values override the file & client settings */
|
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto session_settings = tr_variant{};
|
|
|
|
tr_variantInitDict(&session_settings, 0);
|
|
|
|
tr_sessionGetSettings(session, &session_settings);
|
|
|
|
tr_variantMergeDicts(&settings, &session_settings);
|
|
|
|
tr_variantClear(&session_settings);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* save the result */
|
2022-03-28 22:13:32 +00:00
|
|
|
tr_variantToFile(&settings, TR_VARIANT_FMT_JSON, filename);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&settings);
|
2022-03-18 13:11:59 +00:00
|
|
|
|
|
|
|
/* Write bandwidth groups limits to file */
|
|
|
|
bandwidthGroupWrite(session, config_dir);
|
2008-12-13 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
// ---
|
2009-08-13 14:47:56 +00:00
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
struct tr_session::init_data
|
2009-02-04 16:58:52 +00:00
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
bool message_queuing_enabled;
|
|
|
|
std::string_view config_dir;
|
|
|
|
tr_variant* client_settings;
|
2022-03-04 22:43:56 +00:00
|
|
|
std::condition_variable_any done_cv;
|
2009-02-04 16:58:52 +00:00
|
|
|
};
|
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_session* tr_sessionInit(char const* config_dir, bool message_queueing_enabled, tr_variant* client_settings)
|
2008-12-13 23:17:36 +00:00
|
|
|
{
|
2023-01-07 14:27:54 +00:00
|
|
|
using namespace bandwidth_group_helpers;
|
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
TR_ASSERT(tr_variantIsDict(client_settings));
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_timeUpdate(time(nullptr));
|
2010-12-03 22:56:17 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* initialize the bare skeleton of the session object */
|
2022-07-28 23:56:40 +00:00
|
|
|
auto* const session = new tr_session{ config_dir };
|
2022-03-18 13:11:59 +00:00
|
|
|
bandwidthGroupRead(session, config_dir);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
// nice to start logging at the very beginning
|
|
|
|
if (auto val = int64_t{}; tr_variantDictFindInt(client_settings, TR_KEY_message_level, &val))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
tr_logSetLevel(static_cast<tr_log_level>(val));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-10-23 05:00:16 +00:00
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
auto data = tr_session::init_data{};
|
2022-01-02 17:51:59 +00:00
|
|
|
data.config_dir = config_dir;
|
2022-08-13 18:36:34 +00:00
|
|
|
data.message_queuing_enabled = message_queueing_enabled;
|
|
|
|
data.client_settings = client_settings;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-04 21:20:27 +00:00
|
|
|
// run initImpl() in the libtransmission thread
|
|
|
|
auto lock = session->unique_lock();
|
|
|
|
session->runInSessionThread([&session, &data]() { session->initImpl(data); });
|
|
|
|
data.done_cv.wait(lock); // wait for the session to be ready
|
2009-02-04 16:58:52 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return session;
|
2009-02-04 16:58:52 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 17:22:23 +00:00
|
|
|
void tr_session::onNowTimer()
|
2009-11-27 02:24:48 +00:00
|
|
|
{
|
2022-08-12 17:22:23 +00:00
|
|
|
TR_ASSERT(now_timer_);
|
2023-01-11 15:26:58 +00:00
|
|
|
auto const now = std::chrono::system_clock::now();
|
2010-12-23 19:32:59 +00:00
|
|
|
|
2022-08-08 22:53:20 +00:00
|
|
|
// tr_session upkeep tasks to perform once per second
|
2023-01-11 15:26:58 +00:00
|
|
|
tr_timeUpdate(std::chrono::system_clock::to_time_t(now));
|
2022-11-02 00:32:26 +00:00
|
|
|
alt_speeds_.checkScheduler();
|
2010-12-23 19:32:59 +00:00
|
|
|
|
2022-08-11 17:28:37 +00:00
|
|
|
// set the timer to kick again right after (10ms after) the next second
|
|
|
|
auto const target_time = std::chrono::time_point_cast<std::chrono::seconds>(now) + 1s + 10ms;
|
|
|
|
auto target_interval = target_time - now;
|
|
|
|
if (target_interval < 100ms)
|
2022-08-08 22:53:20 +00:00
|
|
|
{
|
2022-08-11 17:28:37 +00:00
|
|
|
target_interval += 1s;
|
2022-08-08 22:53:20 +00:00
|
|
|
}
|
2022-08-12 17:22:23 +00:00
|
|
|
now_timer_->setInterval(std::chrono::duration_cast<std::chrono::milliseconds>(target_interval));
|
2009-11-26 18:47:08 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
void tr_session::initImpl(init_data& data)
|
2009-02-04 16:58:52 +00:00
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
auto lock = unique_lock();
|
2022-11-04 21:20:27 +00:00
|
|
|
TR_ASSERT(amInSessionThread());
|
2009-02-04 16:58:52 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
auto* const client_settings = data.client_settings;
|
2022-08-13 18:36:34 +00:00
|
|
|
TR_ASSERT(tr_variantIsDict(client_settings));
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_logAddTrace(fmt::format("tr_sessionInit: the session's top-level bandwidth object is {}", fmt::ptr(&top_bandwidth_)));
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
auto settings = tr_variant{};
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(&settings, 0);
|
|
|
|
tr_sessionGetDefaultSettings(&settings);
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_variantMergeDicts(&settings, client_settings);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifndef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Don't exit when writing on a broken socket */
|
2022-04-22 19:56:54 +00:00
|
|
|
(void)signal(SIGPIPE, SIG_IGN);
|
2007-09-20 16:32:01 +00:00
|
|
|
#endif
|
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_logSetQueueEnabled(data.message_queuing_enabled);
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
this->blocklists_ = libtransmission::Blocklist::loadBlocklists(blocklist_dir_, useBlocklist());
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddInfo(fmt::format(_("Transmission version {version} starting"), fmt::arg("version", LONG_VERSION_STRING)));
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
setSettings(client_settings, true);
|
2007-12-24 07:02:40 +00:00
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
if (this->allowsLPD())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-10-01 14:12:49 +00:00
|
|
|
this->lpd_ = tr_lpd::create(lpd_mediator_, eventBase());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_utpInit(this);
|
2022-07-08 15:13:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* cleanup */
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&settings);
|
2022-08-13 18:36:34 +00:00
|
|
|
data.done_cv.notify_one();
|
2009-10-23 03:41:36 +00:00
|
|
|
}
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
void tr_session::setSettings(tr_variant* settings_dict, bool force)
|
2009-10-23 03:41:36 +00:00
|
|
|
{
|
2022-11-04 21:20:27 +00:00
|
|
|
TR_ASSERT(amInSessionThread());
|
2022-11-06 16:35:48 +00:00
|
|
|
TR_ASSERT(tr_variantIsDict(settings_dict));
|
|
|
|
|
|
|
|
// load the session settings
|
|
|
|
auto new_settings = tr_session_settings{};
|
|
|
|
new_settings.load(settings_dict);
|
|
|
|
setSettings(std::move(new_settings), force);
|
|
|
|
|
|
|
|
// delegate loading out the other settings
|
|
|
|
alt_speeds_.load(settings_dict);
|
|
|
|
rpc_server_->load(settings_dict);
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2022-11-12 15:53:09 +00:00
|
|
|
void tr_session::setSettings(tr_session_settings&& settings_in, bool force)
|
2022-11-06 16:35:48 +00:00
|
|
|
{
|
|
|
|
auto const lock = unique_lock();
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
std::swap(settings_, settings_in);
|
|
|
|
auto const& new_settings = settings_;
|
|
|
|
auto const& old_settings = settings_in;
|
2022-11-02 00:32:26 +00:00
|
|
|
|
|
|
|
// the rest of the func is session_ responding to settings changes
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if (auto const& val = new_settings.log_level; force || val != old_settings.log_level)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
tr_logSetLevel(val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-10-23 05:00:16 +00:00
|
|
|
|
2014-12-13 15:22:39 +00:00
|
|
|
#ifndef _WIN32
|
2022-11-02 00:32:26 +00:00
|
|
|
if (auto const& val = new_settings.umask; force || val != old_settings.umask)
|
2022-04-28 14:16:19 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
::umask(val);
|
2009-10-23 03:41:36 +00:00
|
|
|
}
|
2014-12-13 15:22:39 +00:00
|
|
|
#endif
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if (auto const& val = new_settings.cache_size_mb; force || val != old_settings.cache_size_mb)
|
2022-02-20 17:54:20 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
tr_sessionSetCacheLimit_MB(this, val);
|
2022-02-20 17:54:20 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if (auto const& val = new_settings.default_trackers_str; force || val != old_settings.default_trackers_str)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
setDefaultTrackers(val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if (auto const& val = new_settings.utp_enabled; force || val != old_settings.utp_enabled)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
tr_sessionSetUTPEnabled(this, val);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
useBlocklist(new_settings.blocklist_enabled);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
auto local_peer_port = force && settings_.peer_port_random_on_start ? randomPort() : new_settings.peer_port;
|
|
|
|
bool port_changed = false;
|
|
|
|
if (force || local_peer_port_ != local_peer_port)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-06 16:35:48 +00:00
|
|
|
local_peer_port_ = local_peer_port;
|
|
|
|
advertised_peer_port_ = local_peer_port;
|
|
|
|
port_changed = true;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-11 16:09:24 +00:00
|
|
|
bool addr_changed = false;
|
2022-11-06 16:35:48 +00:00
|
|
|
if (new_settings.tcp_enabled)
|
|
|
|
{
|
|
|
|
if (auto const& val = new_settings.bind_address_ipv4; force || port_changed || val != old_settings.bind_address_ipv4)
|
2022-07-25 22:25:55 +00:00
|
|
|
{
|
2022-11-06 16:35:48 +00:00
|
|
|
auto const [addr, is_default] = publicAddress(TR_AF_INET);
|
|
|
|
bound_ipv4_.emplace(eventBase(), addr, local_peer_port_, &tr_session::onIncomingPeerConnection, this);
|
2022-11-11 16:09:24 +00:00
|
|
|
addr_changed = true;
|
2022-07-25 22:25:55 +00:00
|
|
|
}
|
2022-07-29 13:11:47 +00:00
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
if (auto const& val = new_settings.bind_address_ipv6; force || port_changed || val != old_settings.bind_address_ipv6)
|
2022-07-25 22:25:55 +00:00
|
|
|
{
|
2022-11-06 16:35:48 +00:00
|
|
|
auto const [addr, is_default] = publicAddress(TR_AF_INET6);
|
|
|
|
bound_ipv6_.emplace(eventBase(), addr, local_peer_port_, &tr_session::onIncomingPeerConnection, this);
|
2022-11-11 16:09:24 +00:00
|
|
|
addr_changed = true;
|
2022-07-25 22:25:55 +00:00
|
|
|
}
|
2022-11-06 16:35:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bound_ipv4_.reset();
|
|
|
|
bound_ipv6_.reset();
|
2022-11-11 16:09:24 +00:00
|
|
|
addr_changed = true;
|
2022-11-06 16:35:48 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 07:31:48 +00:00
|
|
|
if (auto const& val = new_settings.port_forwarding_enabled; force || val != old_settings.port_forwarding_enabled)
|
|
|
|
{
|
|
|
|
tr_sessionSetPortForwardingEnabled(this, val);
|
|
|
|
}
|
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
if (port_changed)
|
|
|
|
{
|
|
|
|
port_forwarding_->localPortChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool const dht_changed = new_settings.dht_enabled != old_settings.dht_enabled;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
if (!udp_core_ || force || port_changed || dht_changed)
|
|
|
|
{
|
|
|
|
udp_core_ = std::make_unique<tr_session::tr_udp_core>(*this, udpPort());
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
// Sends out announce messages with advertisedPeerPort(), so this
|
|
|
|
// section neesd be happen here after the peer port settings changes
|
|
|
|
if (auto const& val = new_settings.lpd_enabled; force || val != old_settings.lpd_enabled)
|
|
|
|
{
|
|
|
|
if (val)
|
2022-04-21 15:58:13 +00:00
|
|
|
{
|
2022-11-06 16:35:48 +00:00
|
|
|
lpd_ = tr_lpd::create(lpd_mediator_, eventBase());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lpd_.reset();
|
2022-04-21 15:58:13 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-11 16:09:24 +00:00
|
|
|
if (!allowsDHT())
|
|
|
|
{
|
|
|
|
dht_.reset();
|
|
|
|
}
|
|
|
|
else if (force || !dht_ || port_changed || addr_changed || dht_changed)
|
|
|
|
{
|
|
|
|
dht_ = tr_dht::create(dht_mediator_, localPeerPort(), udp_core_->socket4(), udp_core_->socket6());
|
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
// We need to update bandwidth if speed settings changed.
|
|
|
|
// It's a harmless call, so just call it instead of checking for settings changes
|
2023-01-07 22:55:00 +00:00
|
|
|
update_bandwidth(this, TR_UP);
|
|
|
|
update_bandwidth(this, TR_DOWN);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_sessionSet(tr_session* session, tr_variant* settings)
|
|
|
|
{
|
2022-11-10 18:04:49 +00:00
|
|
|
// do the work in the session thread
|
|
|
|
auto done_promise = std::promise<void>{};
|
|
|
|
auto done_future = done_promise.get_future();
|
|
|
|
session->runInSessionThread(
|
|
|
|
[&session, &settings, &done_promise]()
|
|
|
|
{
|
|
|
|
session->setSettings(settings, false);
|
|
|
|
done_promise.set_value();
|
|
|
|
});
|
|
|
|
done_future.wait();
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_sessionSetDownloadDir(tr_session* session, char const* dir)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2013-02-09 04:05:03 +00:00
|
|
|
|
2022-02-07 04:28:36 +00:00
|
|
|
session->setDownloadDir(dir != nullptr ? dir : "");
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_sessionGetDownloadDir(tr_session const* session)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2013-02-09 04:05:03 +00:00
|
|
|
|
2021-11-13 18:09:14 +00:00
|
|
|
return session->downloadDir().c_str();
|
2011-01-05 04:41:19 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 07:13:18 +00:00
|
|
|
char const* tr_sessionGetConfigDir(tr_session const* session)
|
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2022-07-23 07:13:18 +00:00
|
|
|
|
|
|
|
return session->configDir().c_str();
|
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2007-10-01 15:17:15 +00:00
|
|
|
|
2022-08-15 00:33:28 +00:00
|
|
|
void tr_sessionSetIncompleteFileNamingEnabled(tr_session* session, bool enabled)
|
2009-10-20 03:14:44 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-10-20 03:14:44 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.is_incomplete_file_naming_enabled = enabled;
|
2009-10-20 03:14:44 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionIsIncompleteFileNamingEnabled(tr_session const* session)
|
2009-10-20 03:14:44 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-10-20 03:14:44 +00:00
|
|
|
|
2022-08-15 00:33:28 +00:00
|
|
|
return session->isIncompleteFileNamingEnabled();
|
2009-10-20 03:14:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2009-10-20 03:14:44 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_sessionSetIncompleteDir(tr_session* session, char const* dir)
|
2009-10-19 05:05:00 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2022-02-07 04:28:36 +00:00
|
|
|
session->setIncompleteDir(dir != nullptr ? dir : "");
|
2009-10-19 05:05:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_sessionGetIncompleteDir(tr_session const* session)
|
2009-10-19 05:05:00 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2021-11-13 20:49:10 +00:00
|
|
|
return session->incompleteDir().c_str();
|
2009-10-19 05:05:00 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 19:49:43 +00:00
|
|
|
void tr_sessionSetIncompleteDirEnabled(tr_session* session, bool enabled)
|
2009-10-19 05:05:00 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2023-01-21 19:49:43 +00:00
|
|
|
session->useIncompleteDir(enabled);
|
2009-10-19 05:05:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionIsIncompleteDirEnabled(tr_session const* session)
|
2009-10-19 05:05:00 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2021-11-13 20:49:10 +00:00
|
|
|
return session->useIncompleteDir();
|
2009-10-19 05:05:00 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Peer Port
|
2007-11-22 06:13:57 +00:00
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
void tr_sessionSetPeerPort(tr_session* session, uint16_t hport)
|
2008-12-13 23:17:36 +00:00
|
|
|
{
|
2022-10-14 02:25:02 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
if (auto const port = tr_port::fromHost(hport); port != session->localPeerPort())
|
|
|
|
{
|
|
|
|
session->runInSessionThread(
|
|
|
|
[session, port]()
|
|
|
|
{
|
|
|
|
auto settings = session->settings_;
|
|
|
|
settings.peer_port = port;
|
|
|
|
session->setSettings(std::move(settings), false);
|
|
|
|
});
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
uint16_t tr_sessionGetPeerPort(tr_session const* session)
|
2009-04-03 04:29:27 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
return session != nullptr ? session->localPeerPort().host() : 0U;
|
2009-04-03 04:29:27 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
uint16_t tr_sessionSetPeerPortRandom(tr_session* session)
|
2008-12-11 04:41:23 +00:00
|
|
|
{
|
2022-08-14 01:04:36 +00:00
|
|
|
auto const p = session->randomPort();
|
2022-08-03 03:38:37 +00:00
|
|
|
tr_sessionSetPeerPort(session, p.host());
|
|
|
|
return p.host();
|
2008-12-11 04:41:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetPeerPortRandomOnStart(tr_session* session, bool random)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.peer_port_random_on_start = random;
|
2009-04-03 04:29:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 22:19:24 +00:00
|
|
|
bool tr_sessionGetPeerPortRandomOnStart(tr_session const* session)
|
2009-04-03 04:29:27 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-04-03 04:29:27 +00:00
|
|
|
|
2022-08-15 00:33:28 +00:00
|
|
|
return session->isPortRandom();
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 13:59:10 +00:00
|
|
|
tr_port_forwarding_state tr_sessionGetPortForwarding(tr_session const* session)
|
2006-09-25 18:37:45 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-09-30 13:59:10 +00:00
|
|
|
return session->port_forwarding_->state();
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-11 16:09:24 +00:00
|
|
|
void tr_session::onAdvertisedPeerPortChanged()
|
|
|
|
{
|
|
|
|
for (auto* const tor : torrents())
|
|
|
|
{
|
|
|
|
tr_torrentChangeMyPort(tor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2007-07-20 08:33:59 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
void tr_sessionSetRatioLimited(tr_session* session, bool is_limited)
|
2009-03-01 13:56:22 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-01 13:56:22 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.ratio_limit_enabled = is_limited;
|
2009-03-01 13:56:22 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 00:33:28 +00:00
|
|
|
void tr_sessionSetRatioLimit(tr_session* session, double desired_ratio)
|
2009-03-01 13:56:22 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-01 13:56:22 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.ratio_limit = desired_ratio;
|
2009-03-01 13:56:22 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionIsRatioLimited(tr_session const* session)
|
2009-03-01 13:56:22 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-01 13:56:22 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
return session->isRatioLimited();
|
2009-03-01 13:56:22 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
double tr_sessionGetRatioLimit(tr_session const* session)
|
2009-03-01 13:56:22 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-01 13:56:22 +00:00
|
|
|
|
2022-08-15 00:33:28 +00:00
|
|
|
return session->desiredRatio();
|
2009-03-01 13:56:22 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2010-07-16 03:12:57 +00:00
|
|
|
|
2022-08-15 00:33:28 +00:00
|
|
|
void tr_sessionSetIdleLimited(tr_session* session, bool is_limited)
|
2010-07-16 03:12:57 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-07-16 03:12:57 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.idle_seeding_limit_enabled = is_limited;
|
2010-07-16 03:12:57 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
void tr_sessionSetIdleLimit(tr_session* session, uint16_t idle_minutes)
|
2010-07-16 03:12:57 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-07-16 03:12:57 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.idle_seeding_limit_minutes = idle_minutes;
|
2010-07-16 03:12:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionIsIdleLimited(tr_session const* session)
|
2010-07-16 03:12:57 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-07-16 03:12:57 +00:00
|
|
|
|
2022-08-15 00:33:28 +00:00
|
|
|
return session->isIdleLimited();
|
2010-07-16 03:12:57 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
uint16_t tr_sessionGetIdleLimit(tr_session const* session)
|
2010-07-16 03:12:57 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-07-16 03:12:57 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
return session->idleLimitMinutes();
|
2010-07-16 03:12:57 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Speed limits
|
2009-03-01 13:56:22 +00:00
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
std::optional<tr_bytes_per_second_t> tr_session::activeSpeedLimitBps(tr_direction dir) const noexcept
|
2009-03-28 16:47:01 +00:00
|
|
|
{
|
2022-08-14 19:41:57 +00:00
|
|
|
if (tr_sessionUsesAltSpeed(this))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
return tr_toSpeedBytes(tr_sessionGetAltSpeed_KBps(this, dir));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-06-30 18:08:50 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
if (this->isSpeedLimited(dir))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
return tr_toSpeedBytes(tr_sessionGetSpeedLimit_KBps(this, dir));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
return {};
|
2010-07-04 06:07:21 +00:00
|
|
|
}
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
time_t tr_session::AltSpeedMediator::time()
|
2009-03-28 16:47:01 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
return tr_time();
|
2010-02-04 23:39:50 +00:00
|
|
|
}
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
void tr_session::AltSpeedMediator::isActiveChanged(bool is_active, tr_session_alt_speeds::ChangeReason reason)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
auto const in_session_thread = [session = &session_, is_active, reason]()
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-01-07 22:55:00 +00:00
|
|
|
update_bandwidth(session, TR_UP);
|
|
|
|
update_bandwidth(session, TR_DOWN);
|
2010-03-12 19:35:28 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if (session->alt_speed_active_changed_func_ != nullptr)
|
|
|
|
{
|
|
|
|
session->alt_speed_active_changed_func_(
|
|
|
|
session,
|
|
|
|
is_active,
|
|
|
|
reason == tr_session_alt_speeds::ChangeReason::User,
|
|
|
|
session->alt_speed_active_changed_func_user_data_);
|
|
|
|
}
|
|
|
|
};
|
2010-02-16 00:00:22 +00:00
|
|
|
|
2022-11-04 21:20:27 +00:00
|
|
|
session_.runInSessionThread(in_session_thread);
|
2008-12-13 22:52:15 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Session primary speed limits
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
void tr_sessionSetSpeedLimit_KBps(tr_session* session, tr_direction dir, tr_kilobytes_per_second_t limit)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2022-08-15 17:48:05 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if (dir == TR_DOWN)
|
|
|
|
{
|
|
|
|
session->settings_.speed_limit_down = limit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
session->settings_.speed_limit_up = limit;
|
|
|
|
}
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
update_bandwidth(session, dir);
|
2009-03-28 16:47:01 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
tr_kilobytes_per_second_t tr_sessionGetSpeedLimit_KBps(tr_session const* session, tr_direction dir)
|
2010-07-04 06:07:21 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
return dir == TR_DOWN ? session->settings_.speed_limit_down : session->settings_.speed_limit_up;
|
2010-07-04 06:07:21 +00:00
|
|
|
}
|
2008-05-12 16:33:17 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
void tr_sessionLimitSpeed(tr_session* session, tr_direction dir, bool limited)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2022-08-15 17:48:05 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2008-12-13 22:52:15 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if (dir == TR_DOWN)
|
|
|
|
{
|
|
|
|
session->settings_.speed_limit_down_enabled = limited;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
session->settings_.speed_limit_up_enabled = limited;
|
|
|
|
}
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
update_bandwidth(session, dir);
|
2008-12-13 22:52:15 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
bool tr_sessionIsSpeedLimited(tr_session const* session, tr_direction dir)
|
2008-12-13 22:52:15 +00:00
|
|
|
{
|
2022-08-15 17:48:05 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2008-12-13 22:52:15 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
return session->isSpeedLimited(dir);
|
2007-01-08 21:53:55 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Session alt speed limits
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
void tr_sessionSetAltSpeed_KBps(tr_session* session, tr_direction dir, tr_kilobytes_per_second_t limit)
|
2009-03-25 19:50:58 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->alt_speeds_.setLimitKBps(dir, limit);
|
2023-01-07 22:55:00 +00:00
|
|
|
update_bandwidth(session, dir);
|
2010-07-04 06:07:21 +00:00
|
|
|
}
|
2009-03-25 19:50:58 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
tr_kilobytes_per_second_t tr_sessionGetAltSpeed_KBps(tr_session const* session, tr_direction dir)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
return session->alt_speeds_.limitKBps(dir);
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
2010-02-04 23:39:50 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
void tr_sessionUseAltSpeedTime(tr_session* session, bool enabled)
|
2009-03-30 17:25:35 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->alt_speeds_.setSchedulerEnabled(enabled);
|
2009-03-30 17:25:35 +00:00
|
|
|
}
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
bool tr_sessionUsesAltSpeedTime(tr_session const* session)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
return session->alt_speeds_.isSchedulerEnabled();
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
void tr_sessionSetAltSpeedBegin(tr_session* session, size_t minutes_since_midnight)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-25 19:50:58 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->alt_speeds_.setStartMinute(minutes_since_midnight);
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
size_t tr_sessionGetAltSpeedBegin(tr_session const* session)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
return session->alt_speeds_.startMinute();
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
2022-11-02 00:32:26 +00:00
|
|
|
void tr_sessionSetAltSpeedEnd(tr_session* session, size_t minutes_since_midnight)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->alt_speeds_.setEndMinute(minutes_since_midnight);
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
size_t tr_sessionGetAltSpeedEnd(tr_session const* session)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
return session->alt_speeds_.endMinute();
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
void tr_sessionSetAltSpeedDay(tr_session* session, tr_sched_day days)
|
2009-04-04 02:17:39 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-04-04 02:17:39 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->alt_speeds_.setWeekdays(days);
|
2009-04-04 02:17:39 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
tr_sched_day tr_sessionGetAltSpeedDay(tr_session const* session)
|
2009-04-04 02:17:39 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-04-04 02:17:39 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
return session->alt_speeds_.weekdays();
|
2009-04-04 02:17:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionUseAltSpeed(tr_session* session, bool enabled)
|
2009-03-30 17:25:35 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
session->alt_speeds_.setActive(enabled, tr_session_alt_speeds::ChangeReason::User);
|
2009-03-30 17:25:35 +00:00
|
|
|
}
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
bool tr_sessionUsesAltSpeed(tr_session const* session)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
return session->alt_speeds_.isActive();
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
void tr_sessionSetAltSpeedFunc(tr_session* session, tr_altSpeedFunc func, void* user_data)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->alt_speed_active_changed_func_ = func;
|
|
|
|
session->alt_speed_active_changed_func_user_data_ = user_data;
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2007-12-20 21:44:16 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetPeerLimit(tr_session* session, uint16_t max_global_peers)
|
2007-12-20 21:44:16 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.peer_limit_global = max_global_peers;
|
2007-12-20 21:44:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
uint16_t tr_sessionGetPeerLimit(tr_session const* session)
|
2007-12-20 21:44:16 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-08-13 22:41:42 +00:00
|
|
|
return session->peerLimit();
|
2007-12-20 21:44:16 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetPeerLimitPerTorrent(tr_session* session, uint16_t max_peers)
|
2008-12-14 22:57:08 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.peer_limit_per_torrent = max_peers;
|
2008-12-14 22:57:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
uint16_t tr_sessionGetPeerLimitPerTorrent(tr_session const* session)
|
2008-12-14 22:57:08 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-08-13 22:41:42 +00:00
|
|
|
return session->peerLimitPerTorrent();
|
2008-12-14 22:57:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
// ---
|
2008-05-12 16:33:17 +00:00
|
|
|
|
2022-08-14 01:04:36 +00:00
|
|
|
void tr_sessionSetPaused(tr_session* session, bool is_paused)
|
2010-03-17 19:23:03 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-03-17 19:23:03 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.should_start_added_torrents = !is_paused;
|
2010-03-17 19:23:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionGetPaused(tr_session const* session)
|
2010-03-17 19:23:03 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-03-17 19:23:03 +00:00
|
|
|
|
2022-08-14 01:04:36 +00:00
|
|
|
return session->shouldPauseAddedTorrents();
|
2010-03-17 19:23:03 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 01:04:36 +00:00
|
|
|
void tr_sessionSetDeleteSource(tr_session* session, bool delete_source)
|
2010-03-17 19:23:03 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-03-17 19:23:03 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.should_delete_source_torrents = delete_source;
|
2010-03-17 19:23:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
// ---
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
double tr_sessionGetRawSpeed_KBps(tr_session const* session, tr_direction dir)
|
2010-07-04 06:07:21 +00:00
|
|
|
{
|
2023-01-07 22:55:00 +00:00
|
|
|
auto const bps = session != nullptr ? session->top_bandwidth_.getRawSpeedBytesPerSecond(0, dir) : 0;
|
|
|
|
return tr_toSpeedKBps(bps);
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
void tr_session::closeImplPart1(std::promise<void>* closed_promise, std::chrono::time_point<std::chrono::steady_clock> deadline)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2022-08-12 17:22:23 +00:00
|
|
|
is_closing_ = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-04 21:20:27 +00:00
|
|
|
// close the low-hanging fruit that can be closed immediately w/o consequences
|
2022-11-30 22:35:10 +00:00
|
|
|
utp_timer.reset();
|
2022-11-04 21:20:27 +00:00
|
|
|
verifier_.reset();
|
2022-08-12 17:22:23 +00:00
|
|
|
save_timer_.reset();
|
|
|
|
now_timer_.reset();
|
2022-11-04 21:20:27 +00:00
|
|
|
rpc_server_.reset();
|
2022-11-11 16:09:24 +00:00
|
|
|
dht_.reset();
|
2022-11-04 21:20:27 +00:00
|
|
|
lpd_.reset();
|
2022-11-11 16:09:24 +00:00
|
|
|
|
2022-09-30 13:59:10 +00:00
|
|
|
port_forwarding_.reset();
|
2022-11-06 16:35:48 +00:00
|
|
|
bound_ipv6_.reset();
|
|
|
|
bound_ipv4_.reset();
|
2009-08-13 14:47:56 +00:00
|
|
|
|
2022-11-04 21:20:27 +00:00
|
|
|
// Close the torrents in order of most active to least active
|
|
|
|
// so that the most important announce=stopped events are
|
|
|
|
// fired out first...
|
2022-08-14 19:41:57 +00:00
|
|
|
auto torrents = getAllTorrents();
|
2021-09-24 23:31:02 +00:00
|
|
|
std::sort(
|
|
|
|
std::begin(torrents),
|
|
|
|
std::end(torrents),
|
|
|
|
[](auto const* a, auto const* b)
|
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
auto const a_cur = a->downloadedCur + a->uploadedCur;
|
|
|
|
auto const b_cur = b->downloadedCur + b->uploadedCur;
|
|
|
|
return a_cur > b_cur; // larger xfers go first
|
2021-09-24 23:31:02 +00:00
|
|
|
});
|
|
|
|
for (auto* tor : torrents)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-04 21:20:27 +00:00
|
|
|
tr_torrentFreeInSessionThread(tor);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-09-24 23:31:02 +00:00
|
|
|
torrents.clear();
|
2022-11-30 03:05:11 +00:00
|
|
|
// ...now that all the torrents have been closed, any remaining
|
|
|
|
// `&event=stopped` announce messages are queued in the announcer.
|
|
|
|
// Tell the announcer to start shutdown, which sends out the stop
|
|
|
|
// events and stops scraping.
|
|
|
|
this->announcer_->startShutdown();
|
|
|
|
// ...and now that those are queued, tell web_ that we're shutting
|
|
|
|
// down soon. This leaves the `event=stopped` going but refuses any
|
|
|
|
// new tasks.
|
2022-11-30 19:00:34 +00:00
|
|
|
this->web_->startShutdown(10s);
|
2022-08-12 17:22:23 +00:00
|
|
|
this->cache.reset();
|
2015-12-31 14:24:15 +00:00
|
|
|
|
2022-11-04 21:20:27 +00:00
|
|
|
// recycle the now-unused save_timer_ here to wait for UDP shutdown
|
2022-08-12 17:22:23 +00:00
|
|
|
TR_ASSERT(!save_timer_);
|
2022-11-30 19:00:34 +00:00
|
|
|
save_timer_ = timerMaker().create([this, closed_promise, deadline]() { closeImplPart2(closed_promise, deadline); });
|
2022-11-04 21:20:27 +00:00
|
|
|
save_timer_->startRepeating(50ms);
|
2015-12-31 14:17:37 +00:00
|
|
|
}
|
2013-05-23 05:43:12 +00:00
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
void tr_session::closeImplPart2(std::promise<void>* closed_promise, std::chrono::time_point<std::chrono::steady_clock> deadline)
|
2015-12-31 14:17:37 +00:00
|
|
|
{
|
2022-11-04 21:20:27 +00:00
|
|
|
// try to keep the UDP announcer alive long enough to send out
|
|
|
|
// all the &event=stopped tracker announces
|
2022-11-30 19:00:34 +00:00
|
|
|
if (n_pending_stops_ != 0U && std::chrono::steady_clock::now() < deadline)
|
2015-12-31 14:17:37 +00:00
|
|
|
{
|
2022-10-21 18:15:14 +00:00
|
|
|
announcer_udp_->upkeep();
|
2017-04-19 12:04:45 +00:00
|
|
|
return;
|
2015-12-31 14:17:37 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 17:22:23 +00:00
|
|
|
save_timer_.reset();
|
2017-01-17 20:55:34 +00:00
|
|
|
|
2022-11-30 03:05:11 +00:00
|
|
|
this->announcer_.reset();
|
2022-10-21 18:15:14 +00:00
|
|
|
this->announcer_udp_.reset();
|
2015-12-31 14:24:15 +00:00
|
|
|
|
2022-08-12 17:22:23 +00:00
|
|
|
stats().saveIfDirty();
|
2022-11-02 00:32:26 +00:00
|
|
|
peer_mgr_.reset();
|
2022-08-12 17:22:23 +00:00
|
|
|
openFiles().closeAll();
|
2022-12-18 17:18:25 +00:00
|
|
|
tr_utpClose(this);
|
|
|
|
this->udp_core_.reset();
|
2022-11-10 02:30:34 +00:00
|
|
|
|
|
|
|
// tada we are done!
|
2022-11-10 18:04:49 +00:00
|
|
|
closed_promise->set_value();
|
2015-12-31 14:17:37 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
void tr_sessionClose(tr_session* session, size_t timeout_secs)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2022-11-10 02:30:34 +00:00
|
|
|
TR_ASSERT(!session->amInSessionThread());
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddInfo(fmt::format(_("Transmission version {version} shutting down"), fmt::arg("version", LONG_VERSION_STRING)));
|
2008-02-04 19:54:47 +00:00
|
|
|
|
2022-11-10 18:04:49 +00:00
|
|
|
auto closed_promise = std::promise<void>{};
|
|
|
|
auto closed_future = closed_promise.get_future();
|
2022-11-30 19:00:34 +00:00
|
|
|
auto const deadline = std::chrono::steady_clock::now() + std::chrono::seconds{ timeout_secs };
|
|
|
|
session->runInSessionThread([&closed_promise, deadline, session]() { session->closeImplPart1(&closed_promise, deadline); });
|
|
|
|
closed_future.wait();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-24 23:31:02 +00:00
|
|
|
delete session;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-07-21 18:46:54 +00:00
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
namespace load_torrents_helpers
|
|
|
|
{
|
|
|
|
void session_load_torrents(tr_session* session, tr_ctor* ctor, std::promise<size_t>* loaded_promise)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-11-10 18:04:49 +00:00
|
|
|
auto const& dirname = session->torrentDir();
|
2022-08-02 17:41:04 +00:00
|
|
|
auto const info = tr_sys_path_get_info(dirname);
|
|
|
|
auto const odir = info && info->isFolder() ? tr_sys_dir_open(dirname.c_str()) : TR_BAD_SYS_DIR;
|
2019-03-17 06:09:08 +00:00
|
|
|
|
2022-11-10 18:04:49 +00:00
|
|
|
auto n_torrents = size_t{};
|
2019-03-17 06:09:08 +00:00
|
|
|
if (odir != TR_BAD_SYS_DIR)
|
2007-07-21 18:46:54 +00:00
|
|
|
{
|
2021-12-27 00:32:36 +00:00
|
|
|
char const* name = nullptr;
|
2022-03-27 17:37:29 +00:00
|
|
|
while ((name = tr_sys_dir_read_name(odir)) != nullptr)
|
2007-07-21 18:46:54 +00:00
|
|
|
{
|
2022-02-24 21:52:29 +00:00
|
|
|
if (!tr_strvEndsWith(name, ".torrent"sv) && !tr_strvEndsWith(name, ".magnet"sv))
|
2007-07-21 18:46:54 +00:00
|
|
|
{
|
2021-12-27 00:32:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-24 21:52:29 +00:00
|
|
|
// is a magnet link?
|
2022-11-12 15:53:09 +00:00
|
|
|
if (auto const path = tr_pathbuf{ dirname, '/', name }; !tr_ctorSetMetainfoFromFile(ctor, path.sv(), nullptr))
|
2022-02-24 21:52:29 +00:00
|
|
|
{
|
2022-03-28 22:13:32 +00:00
|
|
|
if (auto buf = std::vector<char>{}; tr_loadFile(path, buf))
|
2022-02-24 21:52:29 +00:00
|
|
|
{
|
2022-11-10 18:04:49 +00:00
|
|
|
tr_ctorSetMetainfoFromMagnetLink(ctor, std::string_view{ std::data(buf), std::size(buf) }, nullptr);
|
2022-02-24 21:52:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-12 15:53:09 +00:00
|
|
|
if (tr_torrentNew(ctor, nullptr) != nullptr)
|
2021-12-27 00:32:36 +00:00
|
|
|
{
|
2022-11-10 18:04:49 +00:00
|
|
|
++n_torrents;
|
2007-07-21 18:46:54 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_dir_close(odir);
|
2007-07-21 18:46:54 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 18:04:49 +00:00
|
|
|
if (n_torrents != 0U)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-10 18:04:49 +00:00
|
|
|
tr_logAddInfo(fmt::format(
|
2022-12-26 21:13:21 +00:00
|
|
|
tr_ngettext("Loaded {count} torrent", "Loaded {count} torrents", n_torrents),
|
2022-11-10 18:04:49 +00:00
|
|
|
fmt::arg("count", n_torrents)));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 18:04:49 +00:00
|
|
|
loaded_promise->set_value(n_torrents);
|
2011-06-19 18:30:57 +00:00
|
|
|
}
|
2023-01-07 22:55:00 +00:00
|
|
|
} // namespace load_torrents_helpers
|
|
|
|
} // namespace
|
2011-06-19 18:30:57 +00:00
|
|
|
|
2022-08-18 14:14:12 +00:00
|
|
|
size_t tr_sessionLoadTorrents(tr_session* session, tr_ctor* ctor)
|
2011-06-19 18:30:57 +00:00
|
|
|
{
|
2023-01-07 22:55:00 +00:00
|
|
|
using namespace load_torrents_helpers;
|
|
|
|
|
2022-11-10 18:04:49 +00:00
|
|
|
auto loaded_promise = std::promise<size_t>{};
|
|
|
|
auto loaded_future = loaded_promise.get_future();
|
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
session->runInSessionThread(session_load_torrents, session, ctor, &loaded_promise);
|
2022-11-10 18:04:49 +00:00
|
|
|
loaded_future.wait();
|
|
|
|
auto const n_torrents = loaded_future.get();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-10 18:04:49 +00:00
|
|
|
return n_torrents;
|
2022-08-18 14:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t tr_sessionGetAllTorrents(tr_session* session, tr_torrent** buf, size_t buflen)
|
|
|
|
{
|
|
|
|
auto& torrents = session->torrents();
|
|
|
|
auto const n = std::size(torrents);
|
|
|
|
|
|
|
|
if (buflen >= n)
|
|
|
|
{
|
|
|
|
std::copy_n(std::begin(torrents), n, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
2007-07-21 18:46:54 +00:00
|
|
|
}
|
2007-12-24 05:03:40 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2007-12-24 05:03:40 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetPexEnabled(tr_session* session, bool enabled)
|
2007-12-24 05:03:40 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.pex_enabled = enabled;
|
2007-12-24 05:03:40 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionIsPexEnabled(tr_session const* session)
|
2007-12-24 05:03:40 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-08-15 00:33:28 +00:00
|
|
|
return session->allowsPEX();
|
2007-12-24 05:03:40 +00:00
|
|
|
}
|
2008-04-01 19:20:21 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionIsDHTEnabled(tr_session const* session)
|
2009-05-19 18:38:26 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
return session->allowsDHT();
|
2009-06-12 22:17:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetDHTEnabled(tr_session* session, bool enabled)
|
2009-06-12 22:17:59 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-06-12 22:17:59 +00:00
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
if (enabled != session->allowsDHT())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-06 16:35:48 +00:00
|
|
|
session->runInSessionThread(
|
|
|
|
[session, enabled]()
|
|
|
|
{
|
|
|
|
auto settings = session->settings_;
|
|
|
|
settings.dht_enabled = enabled;
|
|
|
|
session->setSettings(std::move(settings), false);
|
|
|
|
});
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-05-21 00:15:54 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2011-02-18 00:33:11 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
bool tr_session::allowsUTP() const noexcept
|
2011-02-18 00:33:11 +00:00
|
|
|
{
|
2011-02-18 00:45:44 +00:00
|
|
|
#ifdef WITH_UTP
|
2022-11-02 00:32:26 +00:00
|
|
|
return settings_.utp_enabled;
|
2011-02-18 00:45:44 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
return false;
|
2011-02-18 00:45:44 +00:00
|
|
|
#endif
|
2011-02-18 00:33:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
bool tr_sessionIsUTPEnabled(tr_session const* session)
|
2011-02-18 00:33:11 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2011-02-18 00:33:11 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
return session->allowsUTP();
|
2011-02-18 00:33:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetUTPEnabled(tr_session* session, bool enabled)
|
2011-02-18 00:33:11 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2011-02-18 00:33:11 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
if (enabled == session->allowsUTP())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-15 17:48:05 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-02-18 00:33:11 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.utp_enabled = enabled;
|
2022-09-21 18:25:53 +00:00
|
|
|
}
|
2011-02-18 00:33:11 +00:00
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
void tr_sessionSetLPDEnabled(tr_session* session, bool enabled)
|
2010-08-07 03:29:47 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-08-07 03:29:47 +00:00
|
|
|
|
2022-11-06 16:35:48 +00:00
|
|
|
if (enabled != session->allowsLPD())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-06 16:35:48 +00:00
|
|
|
session->runInSessionThread(
|
|
|
|
[session, enabled]()
|
2022-08-14 19:41:57 +00:00
|
|
|
{
|
2022-11-06 16:35:48 +00:00
|
|
|
auto settings = session->settings_;
|
|
|
|
settings.lpd_enabled = enabled;
|
|
|
|
session->setSettings(std::move(settings), false);
|
|
|
|
});
|
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionIsLPDEnabled(tr_session const* session)
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
return session->allowsLPD();
|
2010-05-01 16:04:00 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2008-04-01 19:20:21 +00:00
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
void tr_sessionSetCacheLimit_MB(tr_session* session, size_t mb)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.cache_size_mb = mb;
|
2022-08-03 06:15:37 +00:00
|
|
|
session->cache->setLimit(tr_toMemBytes(mb));
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
size_t tr_sessionGetCacheLimit_MB(tr_session const* session)
|
2010-07-04 06:07:21 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2010-07-04 06:07:21 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
return session->settings_.cache_size_mb;
|
2010-07-07 23:37:03 +00:00
|
|
|
}
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-02-20 17:54:20 +00:00
|
|
|
void tr_session::setDefaultTrackers(std::string_view trackers)
|
|
|
|
{
|
2022-02-21 03:25:49 +00:00
|
|
|
auto const oldval = default_trackers_;
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
settings_.default_trackers_str = trackers;
|
2022-02-21 03:25:49 +00:00
|
|
|
default_trackers_.parse(trackers);
|
|
|
|
|
|
|
|
// if the list changed, update all the public torrents
|
|
|
|
if (default_trackers_ != oldval)
|
|
|
|
{
|
2022-03-01 23:06:29 +00:00
|
|
|
for (auto* const tor : torrents())
|
2022-02-21 03:25:49 +00:00
|
|
|
{
|
|
|
|
if (tor->isPublic())
|
|
|
|
{
|
2022-11-19 05:00:25 +00:00
|
|
|
announcer_->resetTorrent(tor);
|
2022-02-21 03:25:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-20 17:54:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_sessionSetDefaultTrackers(tr_session* session, char const* trackers)
|
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2022-02-20 17:54:20 +00:00
|
|
|
|
2022-02-20 19:00:52 +00:00
|
|
|
session->setDefaultTrackers(trackers != nullptr ? trackers : "");
|
2022-02-20 17:54:20 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2022-02-20 17:54:20 +00:00
|
|
|
|
2022-06-29 20:08:58 +00:00
|
|
|
tr_bandwidth& tr_session::getBandwidthGroup(std::string_view name)
|
2022-03-18 13:11:59 +00:00
|
|
|
{
|
2022-03-18 16:34:29 +00:00
|
|
|
auto& groups = this->bandwidth_groups_;
|
2022-03-18 13:11:59 +00:00
|
|
|
|
2022-04-22 13:08:02 +00:00
|
|
|
for (auto const& [group_name, group] : groups)
|
2022-03-18 13:11:59 +00:00
|
|
|
{
|
2022-04-22 13:08:02 +00:00
|
|
|
if (group_name == name)
|
|
|
|
{
|
|
|
|
return *group;
|
|
|
|
}
|
2022-03-18 13:11:59 +00:00
|
|
|
}
|
2022-03-18 16:34:29 +00:00
|
|
|
|
2022-06-29 20:08:58 +00:00
|
|
|
auto& [group_name, group] = groups.emplace_back(name, std::make_unique<tr_bandwidth>(new tr_bandwidth(&top_bandwidth_)));
|
2022-04-22 13:08:02 +00:00
|
|
|
return *group;
|
2022-03-18 13:11:59 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2022-03-18 13:11:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetPortForwardingEnabled(tr_session* session, bool enabled)
|
2010-02-06 20:25:25 +00:00
|
|
|
{
|
2023-02-12 02:49:16 +00:00
|
|
|
session->runInSessionThread(
|
|
|
|
[session, enabled]()
|
|
|
|
{
|
|
|
|
session->settings_.port_forwarding_enabled = enabled;
|
|
|
|
session->port_forwarding_->setEnabled(enabled);
|
|
|
|
});
|
2008-05-12 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionIsPortForwardingEnabled(tr_session const* session)
|
2008-05-12 16:33:17 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-09-30 13:59:10 +00:00
|
|
|
return session->port_forwarding_->isEnabled();
|
2008-05-12 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2008-05-12 16:33:17 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
void tr_session::useBlocklist(bool enabled)
|
2009-12-01 00:07:18 +00:00
|
|
|
{
|
2022-11-02 00:32:26 +00:00
|
|
|
settings_.blocklist_enabled = enabled;
|
2022-08-15 17:48:05 +00:00
|
|
|
|
|
|
|
std::for_each(
|
|
|
|
std::begin(blocklists_),
|
|
|
|
std::end(blocklists_),
|
2022-11-03 20:46:27 +00:00
|
|
|
[enabled](auto& blocklist) { blocklist.setEnabled(enabled); });
|
2022-08-15 17:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tr_session::addressIsBlocked(tr_address const& addr) const noexcept
|
|
|
|
{
|
|
|
|
return std::any_of(
|
|
|
|
std::begin(blocklists_),
|
|
|
|
std::end(blocklists_),
|
2022-11-03 20:46:27 +00:00
|
|
|
[&addr](auto& blocklist) { return blocklist.contains(addr); });
|
2009-12-01 00:07:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionReloadBlocklists(tr_session* session)
|
2009-12-01 00:07:18 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
session->blocklists_ = libtransmission::Blocklist::loadBlocklists(session->blocklist_dir_, session->useBlocklist());
|
2010-06-14 12:01:50 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if (session->peer_mgr_)
|
|
|
|
{
|
|
|
|
tr_peerMgrOnBlocklistChanged(session->peer_mgr_.get());
|
|
|
|
}
|
2009-12-01 00:07:18 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 05:31:24 +00:00
|
|
|
size_t tr_blocklistGetRuleCount(tr_session const* session)
|
2008-04-01 19:20:21 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2013-05-23 05:43:12 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
auto& src = session->blocklists_;
|
2022-11-03 20:46:27 +00:00
|
|
|
return std::accumulate(std::begin(src), std::end(src), 0, [](int sum, auto& cur) { return sum + std::size(cur); });
|
2008-04-01 19:20:21 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_blocklistIsEnabled(tr_session const* session)
|
2008-04-01 19:20:21 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2021-11-14 01:09:33 +00:00
|
|
|
return session->useBlocklist();
|
2008-04-01 19:20:21 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 01:09:33 +00:00
|
|
|
void tr_blocklistSetEnabled(tr_session* session, bool enabled)
|
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2021-11-14 01:09:33 +00:00
|
|
|
|
|
|
|
session->useBlocklist(enabled);
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_blocklistExists(tr_session const* session)
|
2008-04-01 19:20:21 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
return !std::empty(session->blocklists_);
|
2008-04-01 19:20:21 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
size_t tr_blocklistSetContent(tr_session* session, char const* content_filename)
|
2008-04-01 19:20:21 +00:00
|
|
|
{
|
2021-11-20 21:20:45 +00:00
|
|
|
auto const lock = session->unique_lock();
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
// These rules will replace the default blocklist.
|
|
|
|
// Build the path of the default blocklist .bin file where we'll save these rules.
|
|
|
|
auto const bin_file = tr_pathbuf{ session->blocklist_dir_, '/', DEFAULT_BLOCKLIST_FILENAME };
|
|
|
|
|
|
|
|
// Try to save it
|
|
|
|
auto added = libtransmission::Blocklist::saveNew(content_filename, bin_file, session->useBlocklist());
|
|
|
|
if (!added)
|
|
|
|
{
|
|
|
|
return 0U;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const n_rules = std::size(*added);
|
2021-12-16 22:58:58 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
// Add (or replace) it in our blocklists_ vector
|
|
|
|
auto& src = session->blocklists_;
|
|
|
|
if (auto iter = std::find_if(
|
|
|
|
std::begin(src),
|
|
|
|
std::end(src),
|
|
|
|
[&bin_file](auto const& candidate) { return bin_file == candidate.binFile(); });
|
|
|
|
iter != std::end(src))
|
2021-09-27 18:28:41 +00:00
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
*iter = std::move(*added);
|
2008-06-11 16:15:45 +00:00
|
|
|
}
|
2021-09-27 18:28:41 +00:00
|
|
|
else
|
|
|
|
{
|
2022-11-03 20:46:27 +00:00
|
|
|
src.emplace_back(std::move(*added));
|
2021-09-27 18:28:41 +00:00
|
|
|
}
|
2008-06-11 16:15:45 +00:00
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
return n_rules;
|
2008-04-01 19:20:21 +00:00
|
|
|
}
|
2008-04-14 14:39:13 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_blocklistSetURL(tr_session* session, char const* url)
|
2010-10-31 17:16:12 +00:00
|
|
|
{
|
2022-02-07 04:28:36 +00:00
|
|
|
session->setBlocklistUrl(url != nullptr ? url : "");
|
2010-10-31 17:16:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_blocklistGetURL(tr_session const* session)
|
2010-10-31 17:16:12 +00:00
|
|
|
{
|
2021-11-14 01:09:33 +00:00
|
|
|
return session->blocklistUrl().c_str();
|
2010-10-31 17:16:12 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2008-04-14 14:39:13 +00:00
|
|
|
|
2021-12-15 21:25:42 +00:00
|
|
|
void tr_session::setRpcWhitelist(std::string_view whitelist) const
|
|
|
|
{
|
2022-04-22 14:19:20 +00:00
|
|
|
this->rpc_server_->setWhitelist(whitelist);
|
2021-12-15 21:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_session::useRpcWhitelist(bool enabled) const
|
|
|
|
{
|
2022-04-22 14:19:20 +00:00
|
|
|
this->rpc_server_->setWhitelistEnabled(enabled);
|
2021-12-15 21:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool tr_session::useRpcWhitelist() const
|
|
|
|
{
|
2022-04-22 14:19:20 +00:00
|
|
|
return this->rpc_server_->isWhitelistEnabled();
|
2021-12-15 21:25:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
void tr_sessionSetRPCEnabled(tr_session* session, bool is_enabled)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
session->rpc_server_->setEnabled(is_enabled);
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionIsRPCEnabled(tr_session const* session)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
return session->rpc_server_->isEnabled();
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
void tr_sessionSetRPCPort(tr_session* session, uint16_t hport)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
if (session->rpc_server_)
|
|
|
|
{
|
|
|
|
session->rpc_server_->setPort(tr_port::fromHost(hport));
|
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
uint16_t tr_sessionGetRPCPort(tr_session const* session)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
return session->rpc_server_ ? session->rpc_server_->port().host() : uint16_t{};
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetRPCCallback(tr_session* session, tr_rpc_func func, void* user_data)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-08-14 15:40:03 +00:00
|
|
|
session->rpc_func_ = func;
|
|
|
|
session->rpc_func_user_data_ = user_data;
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_sessionSetRPCWhitelist(tr_session* session, char const* whitelist)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-02-07 04:28:36 +00:00
|
|
|
session->setRpcWhitelist(whitelist != nullptr ? whitelist : "");
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_sessionGetRPCWhitelist(tr_session const* session)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
return session->rpc_server_->whitelist().c_str();
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-11-14 06:02:45 +00:00
|
|
|
void tr_sessionSetRPCWhitelistEnabled(tr_session* session, bool enabled)
|
2008-10-01 22:59:29 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2021-11-14 06:02:45 +00:00
|
|
|
session->useRpcWhitelist(enabled);
|
2008-10-01 22:59:29 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionGetRPCWhitelistEnabled(tr_session const* session)
|
2008-10-01 22:59:29 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2021-11-14 06:02:45 +00:00
|
|
|
return session->useRpcWhitelist();
|
2008-10-01 22:59:29 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_sessionSetRPCPassword(tr_session* session, char const* password)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
session->rpc_server_->setPassword(password != nullptr ? password : "");
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_sessionGetRPCPassword(tr_session const* session)
|
2009-03-25 00:36:11 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-25 00:36:11 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
return session->rpc_server_->getSaltedPassword().c_str();
|
2009-03-25 00:36:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_sessionSetRPCUsername(tr_session* session, char const* username)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
session->rpc_server_->setUsername(username != nullptr ? username : "");
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_sessionGetRPCUsername(tr_session const* session)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
return session->rpc_server_->username().c_str();
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
void tr_sessionSetRPCPasswordEnabled(tr_session* session, bool enabled)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
session->rpc_server_->setPasswordEnabled(enabled);
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionIsRPCPasswordEnabled(tr_session const* session)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
return session->rpc_server_->isPasswordEnabled();
|
2009-04-15 21:05:58 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2010-05-08 22:42:28 +00:00
|
|
|
|
2021-10-13 03:46:12 +00:00
|
|
|
void tr_sessionSetScriptEnabled(tr_session* session, TrScript type, bool enabled)
|
2021-10-12 21:32:32 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2021-10-13 03:46:12 +00:00
|
|
|
TR_ASSERT(type < TR_SCRIPT_N_TYPES);
|
2021-10-12 21:32:32 +00:00
|
|
|
|
2021-11-13 23:33:18 +00:00
|
|
|
session->useScript(type, enabled);
|
2021-10-12 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 03:46:12 +00:00
|
|
|
bool tr_sessionIsScriptEnabled(tr_session const* session, TrScript type)
|
2021-10-12 21:32:32 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2021-10-13 03:46:12 +00:00
|
|
|
TR_ASSERT(type < TR_SCRIPT_N_TYPES);
|
2021-10-12 21:32:32 +00:00
|
|
|
|
2021-11-13 23:33:18 +00:00
|
|
|
return session->useScript(type);
|
2021-10-12 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 03:46:12 +00:00
|
|
|
void tr_sessionSetScript(tr_session* session, TrScript type, char const* script)
|
2021-10-12 21:32:32 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2021-10-13 03:46:12 +00:00
|
|
|
TR_ASSERT(type < TR_SCRIPT_N_TYPES);
|
2021-10-12 21:32:32 +00:00
|
|
|
|
2022-02-07 04:28:36 +00:00
|
|
|
session->setScript(type, script != nullptr ? script : "");
|
2021-10-12 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 03:46:12 +00:00
|
|
|
char const* tr_sessionGetScript(tr_session const* session, TrScript type)
|
2021-10-12 21:32:32 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2021-10-13 03:46:12 +00:00
|
|
|
TR_ASSERT(type < TR_SCRIPT_N_TYPES);
|
2021-10-12 21:32:32 +00:00
|
|
|
|
2021-11-13 23:33:18 +00:00
|
|
|
return session->script(type).c_str();
|
2010-05-08 22:42:28 +00:00
|
|
|
}
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
void tr_sessionSetQueueSize(tr_session* session, tr_direction dir, size_t max_simultaneous_torrents)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if (dir == TR_DOWN)
|
|
|
|
{
|
|
|
|
session->settings_.download_queue_size = max_simultaneous_torrents;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
session->settings_.seed_queue_size = max_simultaneous_torrents;
|
|
|
|
}
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 16:14:42 +00:00
|
|
|
size_t tr_sessionGetQueueSize(tr_session const* session, tr_direction dir)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2022-08-13 19:56:10 +00:00
|
|
|
return session->queueSize(dir);
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
void tr_sessionSetQueueEnabled(tr_session* session, tr_direction dir, bool do_limit_simultaneous_torrents)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
if (dir == TR_DOWN)
|
|
|
|
{
|
|
|
|
session->settings_.download_queue_enabled = do_limit_simultaneous_torrents;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
session->settings_.seed_queue_enabled = do_limit_simultaneous_torrents;
|
|
|
|
}
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionGetQueueEnabled(tr_session const* session, tr_direction dir)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2022-08-13 19:56:10 +00:00
|
|
|
return session->queueEnabled(dir);
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetQueueStalledMinutes(tr_session* session, int minutes)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(minutes > 0);
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.queue_stalled_minutes = minutes;
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetQueueStalledEnabled(tr_session* session, bool is_enabled)
|
2011-08-07 19:24:33 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2011-08-07 19:24:33 +00:00
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
session->settings_.queue_stalled_enabled = is_enabled;
|
2011-08-07 19:24:33 +00:00
|
|
|
}
|
2012-02-03 16:44:07 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionGetQueueStalledEnabled(tr_session const* session)
|
2011-08-07 19:24:33 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2011-08-07 19:24:33 +00:00
|
|
|
|
2022-08-13 19:56:10 +00:00
|
|
|
return session->queueStalledEnabled();
|
2011-08-07 19:24:33 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 16:19:45 +00:00
|
|
|
size_t tr_sessionGetQueueStalledMinutes(tr_session const* session)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2022-08-13 19:56:10 +00:00
|
|
|
return session->queueStalledMinutes();
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetAntiBruteForceThreshold(tr_session* session, int max_bad_requests)
|
2020-11-14 18:43:42 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2022-08-03 06:15:37 +00:00
|
|
|
TR_ASSERT(max_bad_requests > 0);
|
2022-04-22 14:19:20 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
session->rpc_server_->setAntiBruteForceLimit(max_bad_requests);
|
2020-11-14 18:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
int tr_sessionGetAntiBruteForceThreshold(tr_session const* session)
|
2020-11-14 18:43:42 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2020-11-14 18:43:42 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
return session->rpc_server_->getAntiBruteForceLimit();
|
2020-11-14 18:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
void tr_sessionSetAntiBruteForceEnabled(tr_session* session, bool is_enabled)
|
2020-11-14 18:43:42 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2020-11-14 18:43:42 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
session->rpc_server_->setAntiBruteForceEnabled(is_enabled);
|
2020-11-14 18:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
bool tr_sessionGetAntiBruteForceEnabled(tr_session const* session)
|
2020-11-14 18:43:42 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2020-11-14 18:43:42 +00:00
|
|
|
|
2022-04-22 14:19:20 +00:00
|
|
|
return session->rpc_server_->isAntiBruteForceEnabled();
|
2020-11-14 18:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
std::vector<tr_torrent*> tr_session::getNextQueuedTorrents(tr_direction dir, size_t num_wanted) const
|
2012-12-28 20:10:03 +00:00
|
|
|
{
|
2022-08-14 19:41:57 +00:00
|
|
|
TR_ASSERT(tr_isDirection(dir));
|
2012-12-28 20:10:03 +00:00
|
|
|
|
2021-09-15 14:32:07 +00:00
|
|
|
// build an array of the candidates
|
|
|
|
auto candidates = std::vector<tr_torrent*>{};
|
2022-08-14 19:41:57 +00:00
|
|
|
candidates.reserve(std::size(torrents()));
|
|
|
|
for (auto* const tor : torrents())
|
2011-08-02 03:59:54 +00:00
|
|
|
{
|
2022-08-14 19:41:57 +00:00
|
|
|
if (tor->isQueued() && (dir == tor->queueDirection()))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 14:32:07 +00:00
|
|
|
candidates.push_back(tor);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 14:32:07 +00:00
|
|
|
// find the best n candidates
|
2021-10-16 14:04:19 +00:00
|
|
|
num_wanted = std::min(num_wanted, std::size(candidates));
|
2021-09-15 14:32:07 +00:00
|
|
|
if (num_wanted < candidates.size())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 14:32:07 +00:00
|
|
|
std::partial_sort(
|
|
|
|
std::begin(candidates),
|
|
|
|
std::begin(candidates) + num_wanted,
|
|
|
|
std::end(candidates),
|
|
|
|
[](auto const* a, auto const* b) { return tr_torrentGetQueuePosition(a) < tr_torrentGetQueuePosition(b); });
|
|
|
|
candidates.resize(num_wanted);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-28 20:10:03 +00:00
|
|
|
|
2021-09-29 22:21:04 +00:00
|
|
|
return candidates;
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
size_t tr_session::countQueueFreeSlots(tr_direction dir) const noexcept
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2022-08-14 19:41:57 +00:00
|
|
|
if (!queueEnabled(dir))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-14 19:41:57 +00:00
|
|
|
return std::numeric_limits<size_t>::max();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
auto const max = queueSize(dir);
|
|
|
|
auto const activity = dir == TR_UP ? TR_STATUS_SEED : TR_STATUS_DOWNLOAD;
|
|
|
|
|
2021-09-10 20:15:35 +00:00
|
|
|
/* count how many torrents are active */
|
2022-10-26 16:20:10 +00:00
|
|
|
auto active_count = size_t{};
|
2022-08-14 19:41:57 +00:00
|
|
|
bool const stalled_enabled = queueStalledEnabled();
|
2022-11-21 16:19:45 +00:00
|
|
|
auto const stalled_if_idle_for_n_seconds = queueStalledMinutes() * 60;
|
2021-09-10 20:15:35 +00:00
|
|
|
time_t const now = tr_time();
|
2022-08-14 19:41:57 +00:00
|
|
|
for (auto const* const tor : torrents())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-10 20:15:35 +00:00
|
|
|
/* is it the right activity? */
|
2022-11-22 20:00:09 +00:00
|
|
|
if (activity != tor->activity())
|
2021-09-15 14:32:07 +00:00
|
|
|
{
|
2021-09-10 20:15:35 +00:00
|
|
|
continue;
|
2021-09-15 14:32:07 +00:00
|
|
|
}
|
2021-09-10 20:15:35 +00:00
|
|
|
|
|
|
|
/* is it stalled? */
|
2022-11-23 19:47:04 +00:00
|
|
|
if (stalled_enabled && difftime(now, std::max(tor->startDate, tor->activityDate)) >= stalled_if_idle_for_n_seconds)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-23 19:47:04 +00:00
|
|
|
continue;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2021-09-10 20:15:35 +00:00
|
|
|
++active_count;
|
|
|
|
|
|
|
|
/* if we've reached the limit, no need to keep counting */
|
|
|
|
if (active_count >= max)
|
2021-09-15 14:32:07 +00:00
|
|
|
{
|
2021-09-10 20:15:35 +00:00
|
|
|
return 0;
|
2021-09-15 14:32:07 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-08-01 22:24:24 +00:00
|
|
|
|
|
|
|
return max - active_count;
|
|
|
|
}
|
2022-03-18 13:11:59 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-04-29 02:35:47 +00:00
|
|
|
|
|
|
|
void tr_session::closeTorrentFiles(tr_torrent* tor) noexcept
|
|
|
|
{
|
2022-06-12 00:46:30 +00:00
|
|
|
this->cache->flushTorrent(tor);
|
2022-06-17 15:43:04 +00:00
|
|
|
openFiles().closeTorrent(tor->id());
|
2022-04-29 02:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_session::closeTorrentFile(tr_torrent* tor, tr_file_index_t file_num) noexcept
|
|
|
|
{
|
2022-06-12 00:46:30 +00:00
|
|
|
this->cache->flushFile(tor, file_num);
|
2022-06-17 15:43:04 +00:00
|
|
|
openFiles().closeFile(tor->id(), file_num);
|
2022-04-29 02:35:47 +00:00
|
|
|
}
|
2022-07-23 06:04:34 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-07-23 06:04:34 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetQueueStartCallback(tr_session* session, void (*callback)(tr_session*, tr_torrent*, void*), void* user_data)
|
2022-07-23 06:04:34 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
session->setQueueStartCallback(callback, user_data);
|
2022-07-23 06:04:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetRatioLimitHitCallback(tr_session* session, tr_session_ratio_limit_hit_func callback, void* user_data)
|
2022-07-23 06:04:34 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
session->setRatioLimitHitCallback(callback, user_data);
|
2022-07-23 06:04:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetIdleLimitHitCallback(tr_session* session, tr_session_idle_limit_hit_func callback, void* user_data)
|
2022-07-23 06:04:34 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
session->setIdleLimitHitCallback(callback, user_data);
|
2022-07-23 06:04:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetMetadataCallback(tr_session* session, tr_session_metadata_func callback, void* user_data)
|
2022-07-23 06:04:34 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
session->setMetadataCallback(callback, user_data);
|
2022-07-23 06:04:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetCompletenessCallback(tr_session* session, tr_torrent_completeness_func callback, void* user_data)
|
2022-07-23 06:04:34 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
session->setTorrentCompletenessCallback(callback, user_data);
|
2022-07-23 06:04:34 +00:00
|
|
|
}
|
2022-07-23 07:13:18 +00:00
|
|
|
|
2022-07-28 23:56:40 +00:00
|
|
|
tr_session_stats tr_sessionGetStats(tr_session const* session)
|
|
|
|
{
|
|
|
|
return session->stats().current();
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_session_stats tr_sessionGetCumulativeStats(tr_session const* session)
|
|
|
|
{
|
|
|
|
return session->stats().cumulative();
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_sessionClearStats(tr_session* session)
|
|
|
|
{
|
|
|
|
session->stats().clear();
|
|
|
|
}
|
2022-07-23 07:13:18 +00:00
|
|
|
|
2022-07-28 23:56:40 +00:00
|
|
|
namespace
|
2022-07-23 07:13:18 +00:00
|
|
|
{
|
2023-01-07 22:55:00 +00:00
|
|
|
auto constexpr SaveIntervalSecs = 360s;
|
2022-07-23 07:13:18 +00:00
|
|
|
|
2022-07-28 23:56:40 +00:00
|
|
|
auto makeResumeDir(std::string_view config_dir)
|
|
|
|
{
|
2022-07-23 07:13:18 +00:00
|
|
|
#if defined(__APPLE__) || defined(_WIN32)
|
2022-07-28 23:56:40 +00:00
|
|
|
auto dir = fmt::format("{:s}/Resume"sv, config_dir);
|
2022-07-23 07:13:18 +00:00
|
|
|
#else
|
2022-07-28 23:56:40 +00:00
|
|
|
auto dir = fmt::format("{:s}/resume"sv, config_dir);
|
2022-07-23 07:13:18 +00:00
|
|
|
#endif
|
2022-07-28 23:56:40 +00:00
|
|
|
tr_sys_dir_create(dir.c_str(), TR_SYS_DIR_CREATE_PARENTS, 0777);
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto makeTorrentDir(std::string_view config_dir)
|
|
|
|
{
|
|
|
|
#if defined(__APPLE__) || defined(_WIN32)
|
|
|
|
auto dir = fmt::format("{:s}/Torrents"sv, config_dir);
|
|
|
|
#else
|
|
|
|
auto dir = fmt::format("{:s}/torrents"sv, config_dir);
|
|
|
|
#endif
|
|
|
|
tr_sys_dir_create(dir.c_str(), TR_SYS_DIR_CREATE_PARENTS, 0777);
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
2022-11-03 20:46:27 +00:00
|
|
|
auto makeBlocklistDir(std::string_view config_dir)
|
|
|
|
{
|
|
|
|
auto dir = fmt::format("{:s}/blocklists"sv, config_dir);
|
|
|
|
tr_sys_dir_create(dir.c_str(), TR_SYS_DIR_CREATE_PARENTS, 0777);
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
2022-07-28 23:56:40 +00:00
|
|
|
} // namespace
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
tr_session::tr_session(std::string_view config_dir, tr_variant* settings_dict)
|
2022-10-13 21:25:54 +00:00
|
|
|
: config_dir_{ config_dir }
|
|
|
|
, resume_dir_{ makeResumeDir(config_dir) }
|
|
|
|
, torrent_dir_{ makeTorrentDir(config_dir) }
|
2022-11-03 20:46:27 +00:00
|
|
|
, blocklist_dir_{ makeBlocklistDir(config_dir) }
|
2022-11-04 21:20:27 +00:00
|
|
|
, session_thread_{ tr_session_thread::create() }
|
2022-08-11 19:30:00 +00:00
|
|
|
, timer_maker_{ std::make_unique<libtransmission::EvTimerMaker>(eventBase()) }
|
2022-11-02 00:32:26 +00:00
|
|
|
, settings_{ settings_dict }
|
2022-10-13 21:25:54 +00:00
|
|
|
, session_id_{ tr_time }
|
2022-11-24 16:17:02 +00:00
|
|
|
, peer_mgr_{ tr_peerMgrNew(this), &tr_peerMgrFree }
|
2022-11-02 00:32:26 +00:00
|
|
|
, rpc_server_{ std::make_unique<tr_rpc_server>(this, settings_dict) }
|
2022-07-28 23:56:40 +00:00
|
|
|
{
|
2022-08-12 17:22:23 +00:00
|
|
|
now_timer_ = timerMaker().create([this]() { onNowTimer(); });
|
|
|
|
now_timer_->startRepeating(1s);
|
|
|
|
|
|
|
|
// Periodically save the .resume files of any torrents whose
|
|
|
|
// status has recently changed. This prevents loss of metadata
|
|
|
|
// in the case of a crash, unclean shutdown, clumsy user, etc.
|
|
|
|
save_timer_ = timerMaker().create(
|
|
|
|
[this]()
|
|
|
|
{
|
|
|
|
for (auto* const tor : torrents())
|
|
|
|
{
|
|
|
|
tr_torrentSave(tor);
|
|
|
|
}
|
|
|
|
|
|
|
|
stats().saveIfDirty();
|
|
|
|
});
|
|
|
|
save_timer_->startRepeating(SaveIntervalSecs);
|
2022-09-06 04:43:59 +00:00
|
|
|
|
|
|
|
verifier_->addCallback(tr_torrentOnVerifyDone);
|
2022-07-23 07:13:18 +00:00
|
|
|
}
|
2022-10-14 02:25:02 +00:00
|
|
|
|
2022-12-06 16:28:28 +00:00
|
|
|
void tr_session::addIncoming(tr_peer_socket&& socket)
|
2022-10-14 02:25:02 +00:00
|
|
|
{
|
2022-12-06 16:28:28 +00:00
|
|
|
tr_peerMgrAddIncoming(peer_mgr_.get(), std::move(socket));
|
2022-10-14 02:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_session::addTorrent(tr_torrent* tor)
|
|
|
|
{
|
|
|
|
tor->unique_id_ = torrents().add(tor);
|
|
|
|
|
2022-11-02 00:32:26 +00:00
|
|
|
tr_peerMgrAddTorrent(peer_mgr_.get(), tor);
|
2022-10-14 02:25:02 +00:00
|
|
|
}
|