2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2008-2022 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-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
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
#include <event2/dns.h>
|
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"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "tr-dht.h" /* tr_dhtUpkeep() */
|
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-01-09 21:48:33 +00:00
|
|
|
#include "tr-udp.h"
|
2011-02-18 00:33:11 +00:00
|
|
|
#include "tr-utp.h"
|
2007-08-18 03:02:32 +00:00
|
|
|
#include "trevent.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;
|
|
|
|
|
2021-11-20 21:20:45 +00:00
|
|
|
std::recursive_mutex tr_session::session_mutex_;
|
|
|
|
|
2022-08-19 03:47:23 +00:00
|
|
|
static auto constexpr DefaultBindAddressIpv4 = "0.0.0.0"sv;
|
|
|
|
static auto constexpr DefaultBindAddressIpv6 = "::"sv;
|
|
|
|
static auto constexpr DefaultRpcHostWhitelist = ""sv;
|
2011-01-27 03:53:02 +00:00
|
|
|
#ifdef TR_LIGHTWEIGHT
|
2021-10-21 00:39:05 +00:00
|
|
|
static auto constexpr DefaultCacheSizeMB = int{ 2 };
|
|
|
|
static auto constexpr DefaultPrefetchEnabled = bool{ false };
|
2011-01-27 03:53:02 +00:00
|
|
|
#else
|
2021-10-21 00:39:05 +00:00
|
|
|
static auto constexpr DefaultCacheSizeMB = int{ 4 };
|
|
|
|
static auto constexpr DefaultPrefetchEnabled = bool{ true };
|
2011-01-27 03:53:02 +00:00
|
|
|
#endif
|
2022-04-28 14:16:19 +00:00
|
|
|
static auto constexpr DefaultUmask = int{ 022 };
|
2022-08-08 22:53:20 +00:00
|
|
|
static auto constexpr SaveIntervalSecs = 360s;
|
2009-08-13 14:47:56 +00:00
|
|
|
|
2022-03-18 16:34:29 +00:00
|
|
|
static void bandwidthGroupRead(tr_session* session, std::string_view config_dir);
|
|
|
|
static int bandwidthGroupWrite(tr_session const* session, std::string_view config_dir);
|
|
|
|
static auto constexpr BandwidthGroupsFilename = "bandwidth-groups.json"sv;
|
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-08-14 01:04:36 +00:00
|
|
|
auto const lower = std::min(random_port_low_.host(), random_port_high_.host());
|
|
|
|
auto const upper = std::max(random_port_low_.host(), random_port_high_.host());
|
2022-04-21 15:58:13 +00:00
|
|
|
auto const range = upper - lower;
|
|
|
|
return tr_port::fromHost(lower + tr_rand_int_weak(range + 1));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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);
|
|
|
|
tr_logAddDebugTor(tor, fmt::format(FMT_STRING("Found a local peer from LPD ({:s})"), address.readable(port)));
|
|
|
|
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();
|
|
|
|
info.activity = tr_torrentGetActivity(tor);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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-08-21 23:37:38 +00:00
|
|
|
auto const [addr, is_default_value] = session_->publicAddress(TR_AF_INET);
|
|
|
|
if (!is_default_value)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2022-08-21 23:37:38 +00:00
|
|
|
return addr.readable();
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::string> tr_session::WebMediator::publicAddressV6() const
|
|
|
|
{
|
|
|
|
auto const [addr, is_default_value] = session_->publicAddress(TR_AF_INET6);
|
|
|
|
if (!is_default_value)
|
|
|
|
{
|
|
|
|
return addr.readable();
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2022-02-22 15:09:24 +00:00
|
|
|
unsigned int tr_session::WebMediator::clamp(int torrent_id, unsigned int 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-03-05 04:26:03 +00:00
|
|
|
tr_runInEventThread(session_, std::move(func), std::move(response));
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_sessionFetch(tr_session* session, tr_web::FetchOptions&& options)
|
|
|
|
{
|
|
|
|
session->web->fetch(std::move(options));
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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-08-14 01:04:36 +00:00
|
|
|
session->encryption_mode_ = mode;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2007-08-18 03:02:32 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2022-07-29 13:11:47 +00:00
|
|
|
void tr_bindinfo::close()
|
2009-05-14 13:42:29 +00:00
|
|
|
{
|
2022-07-29 13:11:47 +00:00
|
|
|
if (ev_ != nullptr)
|
2009-05-14 13:42:29 +00:00
|
|
|
{
|
2022-07-29 13:11:47 +00:00
|
|
|
event_free(ev_);
|
|
|
|
ev_ = nullptr;
|
2009-05-14 13:42:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 13:11:47 +00:00
|
|
|
if (socket_ != TR_BAD_SOCKET)
|
|
|
|
{
|
|
|
|
tr_netCloseSocket(socket_);
|
|
|
|
socket_ = TR_BAD_SOCKET;
|
|
|
|
}
|
2009-05-14 13:42:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-09 23:44:20 +00:00
|
|
|
static void accept_incoming_peer(evutil_socket_t fd, short /*what*/, 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-07-09 23:44:20 +00:00
|
|
|
auto clientAddr = tr_address{};
|
|
|
|
auto clientPort = tr_port{};
|
|
|
|
auto const clientSocket = tr_netAccept(session, fd, &clientAddr, &clientPort);
|
|
|
|
|
|
|
|
if (clientSocket != TR_BAD_SOCKET)
|
2013-05-23 05:43:12 +00:00
|
|
|
{
|
2022-07-10 01:03:40 +00:00
|
|
|
tr_logAddTrace(fmt::format("new incoming connection {} ({})", clientSocket, clientAddr.readable(clientPort)));
|
2022-07-09 23:44:20 +00:00
|
|
|
|
|
|
|
tr_peerMgrAddIncoming(session->peerMgr, &clientAddr, clientPort, tr_peer_socket_tcp_create(clientSocket));
|
2009-05-14 13:42:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-29 13:11:47 +00:00
|
|
|
void tr_bindinfo::bindAndListenForIncomingPeers(tr_session* session)
|
2009-05-14 13:42:29 +00:00
|
|
|
{
|
2022-08-26 02:27:11 +00:00
|
|
|
TR_ASSERT(session->allowsTCP());
|
|
|
|
|
2022-07-29 13:11:47 +00:00
|
|
|
socket_ = tr_netBindTCP(&addr_, session->private_peer_port, false);
|
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
|
|
|
{
|
2022-08-03 15:07:45 +00:00
|
|
|
tr_logAddInfo(fmt::format(
|
|
|
|
_("Listening to incoming peer connections on {hostport}"),
|
|
|
|
fmt::arg("hostport", addr_.readable(session->private_peer_port))));
|
2022-08-08 22:53:20 +00:00
|
|
|
ev_ = event_new(session->eventBase(), socket_, EV_READ | EV_PERSIST, accept_incoming_peer, session);
|
2022-07-29 13:11:47 +00:00
|
|
|
event_add(ev_, nullptr);
|
2009-05-14 13:42:29 +00:00
|
|
|
}
|
2022-07-29 13:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void close_incoming_peer_port(tr_session* session)
|
|
|
|
{
|
|
|
|
session->bind_ipv4.close();
|
|
|
|
session->bind_ipv6.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void open_incoming_peer_port(tr_session* session)
|
|
|
|
{
|
2022-08-26 02:27:11 +00:00
|
|
|
TR_ASSERT(session->allowsTCP());
|
|
|
|
|
2022-07-29 13:11:47 +00:00
|
|
|
session->bind_ipv4.bindAndListenForIncomingPeers(session);
|
2009-05-14 13:42:29 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_net_hasIPv6(session->private_peer_port))
|
2013-05-23 05:43:12 +00:00
|
|
|
{
|
2022-07-29 13:11:47 +00:00
|
|
|
session->bind_ipv6.bindAndListenForIncomingPeers(session);
|
2009-05-14 13:42:29 +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-08-14 19:41:57 +00:00
|
|
|
switch (type)
|
2022-07-09 23:44:20 +00:00
|
|
|
{
|
|
|
|
case TR_AF_INET:
|
2022-08-19 03:47:23 +00:00
|
|
|
return { bind_ipv4.addr_, bind_ipv4.addr_.readable() == DefaultBindAddressIpv4 };
|
2022-07-09 23:44:20 +00:00
|
|
|
|
|
|
|
case TR_AF_INET6:
|
2022-08-19 03:47:23 +00:00
|
|
|
return { bind_ipv6.addr_, bind_ipv6.addr_.readable() == DefaultBindAddressIpv6 };
|
2022-07-09 23:44:20 +00:00
|
|
|
|
|
|
|
default:
|
2022-08-14 19:41:57 +00:00
|
|
|
TR_ASSERT_MSG(false, "invalid type");
|
|
|
|
return {};
|
2022-07-09 23:44:20 +00:00
|
|
|
}
|
2009-05-14 13:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-11-14 05:23:19 +00:00
|
|
|
#ifdef TR_LIGHTWEIGHT
|
|
|
|
#define TR_DEFAULT_ENCRYPTION TR_CLEAR_PREFERRED
|
|
|
|
#else
|
|
|
|
#define TR_DEFAULT_ENCRYPTION TR_ENCRYPTION_PREFERRED
|
|
|
|
#endif
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionGetDefaultSettings(tr_variant* setme_dictionary)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-17 00:28:57 +00:00
|
|
|
auto const download_dir = tr_getDefaultDownloadDir();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
auto* const d = setme_dictionary;
|
|
|
|
TR_ASSERT(tr_variantIsDict(d));
|
2022-02-14 04:07:12 +00:00
|
|
|
tr_variantDictReserve(d, 71);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_blocklist_enabled, false);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(d, TR_KEY_blocklist_url, "http://www.example.com/blocklist"sv);
|
2021-10-21 00:39:05 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_cache_size_mb, DefaultCacheSizeMB);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_dht_enabled, true);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_utp_enabled, true);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_lpd_enabled, false);
|
2022-07-23 01:10:02 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_download_dir, download_dir);
|
2022-02-20 17:54:20 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_default_trackers, "");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_speed_limit_down, 100);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_speed_limit_down_enabled, false);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_encryption, TR_DEFAULT_ENCRYPTION);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_idle_seeding_limit, 30);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_idle_seeding_limit_enabled, false);
|
2022-07-23 01:10:02 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_incomplete_dir, download_dir);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_incomplete_dir_enabled, false);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_message_level, TR_LOG_INFO);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_download_queue_size, 5);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_download_queue_enabled, true);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_peer_limit_global, atoi(TR_DEFAULT_PEER_LIMIT_GLOBAL_STR));
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_peer_limit_per_torrent, atoi(TR_DEFAULT_PEER_LIMIT_TORRENT_STR));
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_peer_port, atoi(TR_DEFAULT_PEER_PORT_STR));
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_peer_port_random_on_start, false);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_peer_port_random_low, 49152);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_peer_port_random_high, 65535);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(d, TR_KEY_peer_socket_tos, TR_DEFAULT_PEER_SOCKET_TOS_STR);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_pex_enabled, true);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_port_forwarding_enabled, true);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_preallocation, TR_PREALLOCATE_SPARSE);
|
2021-10-21 00:39:05 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_prefetch_enabled, DefaultPrefetchEnabled);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_peer_id_ttl_hours, 6);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_queue_stalled_enabled, true);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_queue_stalled_minutes, 30);
|
|
|
|
tr_variantDictAddReal(d, TR_KEY_ratio_limit, 2.0);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_ratio_limit_enabled, false);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_rename_partial_files, true);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_rpc_authentication_required, false);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(d, TR_KEY_rpc_bind_address, "0.0.0.0");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_rpc_enabled, false);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(d, TR_KEY_rpc_password, "");
|
|
|
|
tr_variantDictAddStrView(d, TR_KEY_rpc_username, "");
|
|
|
|
tr_variantDictAddStrView(d, TR_KEY_rpc_whitelist, TR_DEFAULT_RPC_WHITELIST);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_rpc_whitelist_enabled, true);
|
2022-08-19 03:47:23 +00:00
|
|
|
tr_variantDictAddStrView(d, TR_KEY_rpc_host_whitelist, DefaultRpcHostWhitelist);
|
2018-01-11 18:00:41 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_rpc_host_whitelist_enabled, true);
|
2020-07-27 04:30:58 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_rpc_port, TR_DEFAULT_RPC_PORT);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(d, TR_KEY_rpc_url, TR_DEFAULT_RPC_URL_STR);
|
2022-06-11 00:04:34 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_rpc_socket_mode, fmt::format("{:03o}", tr_rpc_server::DefaultRpcSocketMode));
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_scrape_paused_torrents_enabled, true);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(d, TR_KEY_script_torrent_added_filename, "");
|
2021-10-12 21:32:32 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_script_torrent_added_enabled, false);
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variantDictAddStrView(d, TR_KEY_script_torrent_done_filename, "");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_script_torrent_done_enabled, false);
|
2022-02-14 04:07:12 +00:00
|
|
|
tr_variantDictAddStrView(d, TR_KEY_script_torrent_done_seeding_filename, "");
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_script_torrent_done_seeding_enabled, false);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_seed_queue_size, 10);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_seed_queue_enabled, false);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_alt_speed_enabled, false);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_alt_speed_up, 50); /* half the regular */
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_alt_speed_down, 50); /* half the regular */
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_alt_speed_time_begin, 540); /* 9am */
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_alt_speed_time_enabled, false);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_alt_speed_time_end, 1020); /* 5pm */
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_alt_speed_time_day, TR_SCHED_ALL);
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_speed_limit_up, 100);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_speed_limit_up_enabled, false);
|
2022-06-11 00:04:34 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_umask, fmt::format("{:03o}", DefaultUmask));
|
2022-07-19 15:27:12 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_upload_slots_per_torrent, 8);
|
2022-08-19 03:47:23 +00:00
|
|
|
tr_variantDictAddStrView(d, TR_KEY_bind_address_ipv4, DefaultBindAddressIpv4);
|
|
|
|
tr_variantDictAddStrView(d, TR_KEY_bind_address_ipv6, DefaultBindAddressIpv6);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_start_added_torrents, true);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_trash_original_torrent_files, false);
|
2020-11-14 18:43:42 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_anti_brute_force_threshold, 100);
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_anti_brute_force_enabled, true);
|
2022-07-20 19:04:52 +00:00
|
|
|
tr_variantDictAddStrView(d, TR_KEY_announce_ip, "");
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_announce_ip_enabled, false);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionGetSettings(tr_session const* s, tr_variant* setme_dictionary)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
auto* const d = setme_dictionary;
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_variantIsDict(d));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-14 04:07:12 +00:00
|
|
|
tr_variantDictReserve(d, 70);
|
2021-11-14 01:09:33 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_blocklist_enabled, s->useBlocklist());
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_blocklist_url, s->blocklistUrl());
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_cache_size_mb, tr_sessionGetCacheLimit_MB(s));
|
2022-08-14 19:41:57 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_dht_enabled, s->allowsDHT());
|
2022-08-15 17:48:05 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_utp_enabled, s->allowsUTP());
|
2022-08-14 19:41:57 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_lpd_enabled, s->allowsLPD());
|
2022-08-26 02:27:11 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_tcp_enabled, s->allowsTCP());
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_download_dir, tr_sessionGetDownloadDir(s));
|
2022-02-20 19:00:52 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_default_trackers, s->defaultTrackersStr());
|
2022-08-13 19:56:10 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_download_queue_size, s->queueSize(TR_DOWN));
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_download_queue_enabled, s->queueEnabled(TR_DOWN));
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_speed_limit_down, tr_sessionGetSpeedLimit_KBps(s, TR_DOWN));
|
2022-08-15 17:48:05 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_speed_limit_down_enabled, s->isSpeedLimited(TR_DOWN));
|
2022-08-14 01:04:36 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_encryption, s->encryptionMode());
|
2022-08-15 17:48:05 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_idle_seeding_limit, s->idleLimitMinutes());
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_idle_seeding_limit_enabled, s->isIdleLimited());
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_incomplete_dir, tr_sessionGetIncompleteDir(s));
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_incomplete_dir_enabled, tr_sessionIsIncompleteDirEnabled(s));
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_message_level, tr_logGetLevel());
|
2022-08-13 22:41:42 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_peer_limit_global, s->peerLimit());
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_peer_limit_per_torrent, s->peerLimitPerTorrent());
|
2022-04-21 15:58:13 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_peer_port, s->peerPort().host());
|
2022-08-15 00:33:28 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_peer_port_random_on_start, s->isPortRandom());
|
2022-08-14 01:04:36 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_peer_port_random_low, s->random_port_low_.host());
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_peer_port_random_high, s->random_port_high_.host());
|
2022-02-10 21:35:28 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_peer_socket_tos, tr_netTosToName(s->peer_socket_tos_));
|
2021-11-14 06:41:44 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_peer_congestion_algorithm, s->peerCongestionAlgorithm());
|
2022-08-15 00:33:28 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_pex_enabled, s->allowsPEX());
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_port_forwarding_enabled, tr_sessionIsPortForwardingEnabled(s));
|
2022-08-14 01:04:36 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_preallocation, s->preallocationMode());
|
2022-08-15 17:48:05 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_prefetch_enabled, s->allowsPrefetch());
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_peer_id_ttl_hours, s->peerIdTTLHours());
|
2022-08-13 19:56:10 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_queue_stalled_enabled, s->queueStalledEnabled());
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_queue_stalled_minutes, s->queueStalledMinutes());
|
2022-08-15 00:33:28 +00:00
|
|
|
tr_variantDictAddReal(d, TR_KEY_ratio_limit, s->desiredRatio());
|
2022-08-15 17:48:05 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_ratio_limit_enabled, s->isRatioLimited());
|
2022-08-15 00:33:28 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_rename_partial_files, s->isIncompleteFileNamingEnabled());
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_rpc_authentication_required, tr_sessionIsRPCPasswordEnabled(s));
|
2022-04-22 14:19:20 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_rpc_bind_address, s->rpc_server_->getBindAddress());
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_rpc_enabled, tr_sessionIsRPCEnabled(s));
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_rpc_password, tr_sessionGetRPCPassword(s));
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_rpc_port, tr_sessionGetRPCPort(s));
|
2022-04-28 14:16:19 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_rpc_socket_mode, fmt::format("{:#o}", s->rpc_server_->socket_mode_));
|
2022-08-19 03:47:23 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_rpc_url, s->rpc_server_->url());
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_rpc_username, tr_sessionGetRPCUsername(s));
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_rpc_whitelist, tr_sessionGetRPCWhitelist(s));
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_rpc_whitelist_enabled, tr_sessionGetRPCWhitelistEnabled(s));
|
2022-08-14 01:04:36 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_scrape_paused_torrents_enabled, s->shouldScrapePausedTorrents());
|
2022-08-13 19:56:10 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_seed_queue_size, s->queueSize(TR_UP));
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_seed_queue_enabled, s->queueEnabled(TR_UP));
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_alt_speed_enabled, tr_sessionUsesAltSpeed(s));
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_alt_speed_up, tr_sessionGetAltSpeed_KBps(s, TR_UP));
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_alt_speed_down, tr_sessionGetAltSpeed_KBps(s, TR_DOWN));
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_alt_speed_time_begin, tr_sessionGetAltSpeedBegin(s));
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_alt_speed_time_enabled, tr_sessionUsesAltSpeedTime(s));
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_alt_speed_time_end, tr_sessionGetAltSpeedEnd(s));
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_alt_speed_time_day, tr_sessionGetAltSpeedDay(s));
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_speed_limit_up, tr_sessionGetSpeedLimit_KBps(s, TR_UP));
|
2022-08-15 17:48:05 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_speed_limit_up_enabled, s->isSpeedLimited(TR_UP));
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_umask, fmt::format("{:#o}", s->umask_));
|
|
|
|
tr_variantDictAddInt(d, TR_KEY_upload_slots_per_torrent, s->uploadSlotsPerTorrent());
|
2022-07-29 13:11:47 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_bind_address_ipv4, s->bind_ipv4.readable());
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_bind_address_ipv6, s->bind_ipv6.readable());
|
2022-08-14 01:04:36 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_start_added_torrents, !s->shouldPauseAddedTorrents());
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddBool(d, TR_KEY_trash_original_torrent_files, tr_sessionGetDeleteSource(s));
|
2020-11-14 18:43:42 +00:00
|
|
|
tr_variantDictAddInt(d, TR_KEY_anti_brute_force_threshold, tr_sessionGetAntiBruteForceThreshold(s));
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_anti_brute_force_enabled, tr_sessionGetAntiBruteForceEnabled(s));
|
2022-07-20 19:04:52 +00:00
|
|
|
tr_variantDictAddStr(d, TR_KEY_announce_ip, s->announceIP());
|
|
|
|
tr_variantDictAddBool(d, TR_KEY_announce_ip_enabled, s->useAnnounceIP());
|
2022-02-14 04:07:12 +00:00
|
|
|
for (auto const& [enabled_key, script_key, script] : tr_session::Scripts)
|
|
|
|
{
|
2022-06-15 22:19:24 +00:00
|
|
|
tr_variantDictAddBool(d, enabled_key, s->useScript(script));
|
|
|
|
tr_variantDictAddStr(d, script_key, s->script(script));
|
2022-02-14 04:07:12 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 01:10:02 +00:00
|
|
|
static void getSettingsFilename(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;
|
|
|
|
}
|
|
|
|
|
2022-08-17 00:28:57 +00:00
|
|
|
auto const default_config_dir = tr_getDefaultConfigDir(appname);
|
2022-07-23 01:10:02 +00:00
|
|
|
setme.assign(std::string_view{ default_config_dir }, "/settings.json"sv);
|
|
|
|
}
|
|
|
|
|
2022-01-02 17:51:59 +00:00
|
|
|
bool tr_sessionLoadSettings(tr_variant* dict, char const* config_dir, char const* appName)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
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. */
|
2021-10-18 12:51:02 +00:00
|
|
|
auto oldDict = *dict;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(dict, 0);
|
|
|
|
tr_sessionGetDefaultSettings(dict);
|
|
|
|
tr_variantMergeDicts(dict, &oldDict);
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&oldDict);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* file settings override the defaults */
|
2021-10-18 12:51:02 +00:00
|
|
|
auto fileSettings = tr_variant{};
|
|
|
|
auto success = bool{};
|
2022-07-23 01:10:02 +00:00
|
|
|
auto filename = tr_pathbuf{};
|
|
|
|
getSettingsFilename(filename, config_dir, appName);
|
|
|
|
if (!tr_sys_path_exists(filename))
|
|
|
|
{
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
else if (tr_variantFromFile(&fileSettings, TR_VARIANT_PARSE_JSON, filename))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantMergeDicts(dict, &fileSettings);
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&fileSettings);
|
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
|
|
|
{
|
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 */
|
|
|
|
{
|
|
|
|
tr_variant sessionSettings;
|
|
|
|
tr_variantInitDict(&sessionSettings, 0);
|
|
|
|
tr_sessionGetSettings(session, &sessionSettings);
|
|
|
|
tr_variantMergeDicts(&settings, &sessionSettings);
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&sessionSettings);
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
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 };
|
2017-04-19 12:04:45 +00:00
|
|
|
session->udp_socket = TR_BAD_SOCKET;
|
|
|
|
session->udp6_socket = TR_BAD_SOCKET;
|
2022-06-12 00:46:30 +00:00
|
|
|
session->cache = std::make_unique<Cache>(session->torrents(), 1024 * 1024 * 2);
|
2022-03-18 13:11:59 +00:00
|
|
|
bandwidthGroupRead(session, config_dir);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* nice to start logging at the very beginning */
|
2022-08-13 18:36:34 +00:00
|
|
|
if (auto i = int64_t{}; tr_variantDictFindInt(client_settings, TR_KEY_message_level, &i))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
tr_logSetLevel(tr_log_level(i));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-10-23 05:00:16 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* start the libtransmission thread */
|
|
|
|
tr_net_init(); /* must go before tr_eventInit */
|
|
|
|
tr_eventInit(session);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(session->events != nullptr);
|
2009-02-04 16:58:52 +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-03-04 22:43:56 +00:00
|
|
|
// run it in the libtransmission thread
|
|
|
|
if (tr_amInEventThread(session))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
session->initImpl(data);
|
2022-03-04 22:43:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto lock = session->unique_lock();
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_runInEventThread(session, [&session, &data]() { session->initImpl(data); });
|
2022-03-04 22:43:56 +00:00
|
|
|
data.done_cv.wait(lock); // wait for the session to be ready
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void turtleCheckClock(tr_session* s, struct tr_turtle_info* t);
|
2009-03-30 17:25:35 +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_);
|
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
|
|
|
|
tr_timeUpdate(time(nullptr));
|
2022-08-12 17:22:23 +00:00
|
|
|
tr_dhtUpkeep(this);
|
|
|
|
if (turtle.isClockEnabled)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-12 17:22:23 +00:00
|
|
|
turtleCheckClock(this, &this->turtle);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-12-23 19:32:59 +00:00
|
|
|
|
2021-09-24 23:31:02 +00:00
|
|
|
// TODO: this seems a little silly. Why do we increment this
|
|
|
|
// every second instead of computing the value as needed by
|
|
|
|
// subtracting the current time from a start time?
|
2022-08-12 17:22:23 +00:00
|
|
|
for (auto* const tor : torrents())
|
2013-05-23 05:43:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tor->isRunning)
|
2013-05-23 05:43:12 +00:00
|
|
|
{
|
2021-12-15 07:04:26 +00:00
|
|
|
if (tor->isDone())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
++tor->secondsSeeding;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++tor->secondsDownloading;
|
|
|
|
}
|
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 now = std::chrono::system_clock::now();
|
|
|
|
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();
|
|
|
|
TR_ASSERT(tr_amInEventThread(this));
|
2009-02-04 16:58:52 +00:00
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_variant const* const client_settings = data.client_settings;
|
|
|
|
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-08-13 18:36:34 +00:00
|
|
|
this->peerMgr = tr_peerMgrNew(this);
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
this->shared = tr_sharedInit(*this);
|
2009-10-20 03:14:44 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/**
|
|
|
|
*** Blocklist
|
|
|
|
**/
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sys_dir_create(tr_pathbuf{ configDir(), "/blocklists"sv }, TR_SYS_DIR_CREATE_PARENTS, 0777);
|
2022-08-15 17:48:05 +00:00
|
|
|
loadBlocklists();
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_announcerInit(this);
|
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-08-13 18:36:34 +00:00
|
|
|
tr_sessionSet(this, &settings);
|
2007-12-24 07:02:40 +00:00
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_udpInit(this);
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
this->web = tr_web::create(this->web_mediator_);
|
2022-02-16 18:33:50 +00:00
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
if (this->allowsLPD())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
this->lpd_ = tr_lpd::create(lpd_mediator_, timerMaker(), 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-02-07 04:28:36 +00:00
|
|
|
static void turtleBootstrap(tr_session* /*session*/, struct tr_turtle_info* /*turtle*/);
|
2017-04-19 12:04:45 +00:00
|
|
|
static void setPeerPort(tr_session* session, tr_port port);
|
2010-02-04 23:39:50 +00:00
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
void tr_session::setImpl(init_data& data)
|
2009-10-23 03:41:36 +00:00
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
TR_ASSERT(tr_amInEventThread(this));
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_variant* const settings = data.client_settings;
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_variantIsDict(settings));
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2021-10-18 12:51:02 +00:00
|
|
|
auto boolVal = bool{};
|
|
|
|
auto d = double{};
|
|
|
|
auto i = int64_t{};
|
2021-11-13 18:09:14 +00:00
|
|
|
auto sv = std::string_view{};
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_message_level, &i))
|
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
tr_logSetLevel(tr_log_level(i));
|
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
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-28 14:16:19 +00:00
|
|
|
if (tr_variantDictFindStrView(settings, TR_KEY_umask, &sv))
|
2013-05-23 05:43:12 +00:00
|
|
|
{
|
2022-04-28 14:16:19 +00:00
|
|
|
/* Read a umask as a string representing an octal number. */
|
2022-08-15 17:48:05 +00:00
|
|
|
this->umask_ = static_cast<mode_t>(tr_parseNum<uint32_t>(sv, 8).value_or(DefaultUmask));
|
|
|
|
::umask(this->umask_);
|
2022-04-28 14:16:19 +00:00
|
|
|
}
|
|
|
|
else if (tr_variantDictFindInt(settings, TR_KEY_umask, &i))
|
|
|
|
{
|
|
|
|
/* Or as a base 10 integer to remain compatible with the old settings format. */
|
2022-08-15 17:48:05 +00:00
|
|
|
this->umask_ = (mode_t)i;
|
|
|
|
::umask(this->umask_);
|
2009-10-23 03:41:36 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2014-12-13 15:22:39 +00:00
|
|
|
#endif
|
2009-02-06 23:35:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* misc features */
|
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_cache_size_mb, &i))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetCacheLimit_MB(this, i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-20 17:54:20 +00:00
|
|
|
if (tr_variantDictFindStrView(settings, TR_KEY_default_trackers, &sv))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
setDefaultTrackers(sv);
|
2022-02-20 17:54:20 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_peer_limit_per_torrent, &i))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetPeerLimitPerTorrent(this, i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_pex_enabled, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetPexEnabled(this, boolVal);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_dht_enabled, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetDHTEnabled(this, boolVal);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 02:27:11 +00:00
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_tcp_enabled, &boolVal))
|
|
|
|
{
|
|
|
|
is_tcp_enabled_ = boolVal;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_utp_enabled, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetUTPEnabled(this, boolVal);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_lpd_enabled, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetLPDEnabled(this, boolVal);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_encryption, &i))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetEncryption(this, tr_encryption_mode(i));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 21:35:28 +00:00
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_peer_socket_tos, &i))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
peer_socket_tos_ = i;
|
2022-02-10 21:35:28 +00:00
|
|
|
}
|
|
|
|
else if (tr_variantDictFindStrView(settings, TR_KEY_peer_socket_tos, &sv))
|
|
|
|
{
|
|
|
|
if (auto ip_tos = tr_netTosFromName(sv); ip_tos)
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
peer_socket_tos_ = *ip_tos;
|
2022-02-10 21:35:28 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 06:41:44 +00:00
|
|
|
sv = ""sv;
|
2021-11-17 21:56:50 +00:00
|
|
|
(void)tr_variantDictFindStrView(settings, TR_KEY_peer_congestion_algorithm, &sv);
|
2022-08-13 18:36:34 +00:00
|
|
|
setPeerCongestionAlgorithm(sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_blocklist_enabled, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
useBlocklist(boolVal);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 01:09:33 +00:00
|
|
|
if (tr_variantDictFindStrView(settings, TR_KEY_blocklist_url, &sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
setBlocklistUrl(sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_start_added_torrents, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetPaused(this, !boolVal);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_trash_original_torrent_files, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetDeleteSource(this, boolVal);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_peer_id_ttl_hours, &i))
|
|
|
|
{
|
2022-08-15 17:48:05 +00:00
|
|
|
this->peer_id_ttl_hours_ = i;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* torrent queues */
|
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_queue_stalled_minutes, &i))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetQueueStalledMinutes(this, i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_queue_stalled_enabled, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetQueueStalledEnabled(this, boolVal);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_download_queue_size, &i))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetQueueSize(this, TR_DOWN, i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_download_queue_enabled, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetQueueEnabled(this, TR_DOWN, boolVal);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_seed_queue_size, &i))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetQueueSize(this, TR_UP, i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_seed_queue_enabled, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetQueueEnabled(this, TR_UP, boolVal);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* files and directories */
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_prefetch_enabled, &boolVal))
|
|
|
|
{
|
2022-08-15 17:48:05 +00:00
|
|
|
this->is_prefetch_enabled_ = boolVal;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_preallocation, &i))
|
|
|
|
{
|
2022-08-14 01:04:36 +00:00
|
|
|
this->preallocation_mode_ = tr_preallocation_mode(i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 18:09:14 +00:00
|
|
|
if (tr_variantDictFindStrView(settings, TR_KEY_download_dir, &sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
this->setDownloadDir(sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 20:49:10 +00:00
|
|
|
if (tr_variantDictFindStrView(settings, TR_KEY_incomplete_dir, &sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
this->setIncompleteDir(sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_incomplete_dir_enabled, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
this->useIncompleteDir(boolVal);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_rename_partial_files, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetIncompleteFileNamingEnabled(this, boolVal);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* rpc server */
|
2022-08-13 18:36:34 +00:00
|
|
|
this->rpc_server_ = std::make_unique<tr_rpc_server>(this, settings);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* public addresses */
|
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
close_incoming_peer_port(this);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-07-29 13:11:47 +00:00
|
|
|
auto address = tr_inaddr_any;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-07-29 13:11:47 +00:00
|
|
|
if (tr_variantDictFindStrView(settings, TR_KEY_bind_address_ipv4, &sv))
|
|
|
|
{
|
|
|
|
if (auto const addr = tr_address::fromString(sv); addr && addr->isIPv4())
|
2022-07-25 22:25:55 +00:00
|
|
|
{
|
2022-07-29 13:11:47 +00:00
|
|
|
address = *addr;
|
2022-07-25 22:25:55 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
this->bind_ipv4 = tr_bindinfo{ address };
|
2022-07-29 13:11:47 +00:00
|
|
|
|
|
|
|
address = tr_in6addr_any;
|
2022-07-25 22:25:55 +00:00
|
|
|
|
2022-07-29 13:11:47 +00:00
|
|
|
if (tr_variantDictFindStrView(settings, TR_KEY_bind_address_ipv6, &sv))
|
|
|
|
{
|
|
|
|
if (auto const addr = tr_address::fromString(sv); addr && addr->isIPv6())
|
2022-07-25 22:25:55 +00:00
|
|
|
{
|
2022-07-29 13:11:47 +00:00
|
|
|
address = *addr;
|
2022-07-25 22:25:55 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
this->bind_ipv6 = tr_bindinfo{ address };
|
2022-07-29 13:11:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* incoming peer port */
|
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_peer_port_random_low, &i))
|
|
|
|
{
|
2022-08-14 01:04:36 +00:00
|
|
|
this->random_port_low_.setHost(i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_peer_port_random_high, &i))
|
|
|
|
{
|
2022-08-14 01:04:36 +00:00
|
|
|
this->random_port_high_.setHost(i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_peer_port_random_on_start, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetPeerPortRandomOnStart(this, boolVal);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
auto peer_port = this->private_peer_port;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-21 15:58:13 +00:00
|
|
|
if (auto port = int64_t{}; tr_variantDictFindInt(settings, TR_KEY_peer_port, &port))
|
|
|
|
{
|
|
|
|
peer_port.setHost(static_cast<uint16_t>(port));
|
|
|
|
}
|
|
|
|
|
2022-08-14 01:04:36 +00:00
|
|
|
::setPeerPort(this, boolVal ? randomPort() : peer_port);
|
2022-04-21 15:58:13 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_port_forwarding_enabled, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetPortForwardingEnabled(this, boolVal);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_peer_limit_global, &i))
|
|
|
|
{
|
2022-08-13 22:41:42 +00:00
|
|
|
this->peer_limit_ = i;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
**/
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_upload_slots_per_torrent, &i))
|
|
|
|
{
|
2022-08-15 17:48:05 +00:00
|
|
|
this->upload_slots_per_torrent_ = i;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_speed_limit_up, &i))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetSpeedLimit_KBps(this, TR_UP, i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_speed_limit_up_enabled, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionLimitSpeed(this, TR_UP, boolVal);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_speed_limit_down, &i))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetSpeedLimit_KBps(this, TR_DOWN, i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_speed_limit_down_enabled, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionLimitSpeed(this, TR_DOWN, boolVal);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindReal(settings, TR_KEY_ratio_limit, &d))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetRatioLimit(this, d);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_ratio_limit_enabled, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetRatioLimited(this, boolVal);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_idle_seeding_limit, &i))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetIdleLimit(this, i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_idle_seeding_limit_enabled, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetIdleLimited(this, boolVal);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*** Turtle Mode
|
|
|
|
**/
|
|
|
|
|
|
|
|
/* update the turtle mode's fields */
|
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_alt_speed_up, &i))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
turtle.speedLimit_Bps[TR_UP] = tr_toSpeedBytes(i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_alt_speed_down, &i))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
turtle.speedLimit_Bps[TR_DOWN] = tr_toSpeedBytes(i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_alt_speed_time_begin, &i))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
turtle.beginMinute = i;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_alt_speed_time_end, &i))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
turtle.endMinute = i;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_alt_speed_time_day, &i))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
turtle.days = tr_sched_day(i);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_alt_speed_time_enabled, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
turtle.isClockEnabled = boolVal;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_alt_speed_enabled, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
turtle.isEnabled = boolVal;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
turtleBootstrap(this, &turtle);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-14 04:07:12 +00:00
|
|
|
for (auto const& [enabled_key, script_key, script] : tr_session::Scripts)
|
2021-10-12 21:32:32 +00:00
|
|
|
{
|
2022-02-14 04:07:12 +00:00
|
|
|
if (auto enabled = bool{}; tr_variantDictFindBool(settings, enabled_key, &enabled))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
this->useScript(script, enabled);
|
2022-02-14 04:07:12 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-14 04:07:12 +00:00
|
|
|
if (auto file = std::string_view{}; tr_variantDictFindStrView(settings, script_key, &file))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
this->setScript(script, file);
|
2022-02-14 04:07:12 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_scrape_paused_torrents_enabled, &boolVal))
|
|
|
|
{
|
2022-08-14 01:04:36 +00:00
|
|
|
this->should_scrape_paused_torrents_ = boolVal;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 18:43:42 +00:00
|
|
|
/**
|
|
|
|
*** BruteForce
|
|
|
|
**/
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(settings, TR_KEY_anti_brute_force_threshold, &i))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetAntiBruteForceThreshold(this, i);
|
2020-11-14 18:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_anti_brute_force_enabled, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_sessionSetAntiBruteForceEnabled(this, boolVal);
|
2020-11-14 18:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-20 19:04:52 +00:00
|
|
|
/*
|
|
|
|
* Announce IP.
|
|
|
|
*/
|
|
|
|
if (tr_variantDictFindStrView(settings, TR_KEY_announce_ip, &sv))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
this->setAnnounceIP(sv);
|
2022-07-20 19:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_announce_ip_enabled, &boolVal))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
this->useAnnounceIP(boolVal);
|
2022-07-20 19:04:52 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 18:36:34 +00:00
|
|
|
data.done_cv.notify_one();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_sessionSet(tr_session* session, tr_variant* settings)
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
auto data = tr_session::init_data{};
|
|
|
|
data.client_settings = settings;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-03-04 22:43:56 +00:00
|
|
|
// run it in the libtransmission thread
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-03-04 22:43:56 +00:00
|
|
|
if (tr_amInEventThread(session))
|
|
|
|
{
|
2022-08-13 18:36:34 +00:00
|
|
|
session->setImpl(data);
|
2022-03-04 22:43:56 +00:00
|
|
|
}
|
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-04 22:43:56 +00:00
|
|
|
auto lock = session->unique_lock();
|
2022-08-13 18:36:34 +00:00
|
|
|
tr_runInEventThread(session, [&session, &data]() { session->setImpl(data); });
|
2022-03-04 22:43:56 +00:00
|
|
|
data.done_cv.wait(lock);
|
2017-04-19 12:04:45 +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();
|
|
|
|
}
|
|
|
|
|
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-08-15 00:33:28 +00:00
|
|
|
session->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
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetIncompleteDirEnabled(tr_session* session, bool b)
|
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
|
|
|
session->useIncompleteDir(b);
|
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
|
|
|
}
|
|
|
|
|
2013-05-23 05:43:12 +00:00
|
|
|
/***
|
|
|
|
**** Peer Port
|
|
|
|
***/
|
2007-11-22 06:13:57 +00:00
|
|
|
|
2022-03-05 04:26:03 +00:00
|
|
|
static void peerPortChanged(tr_session* const session)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
close_incoming_peer_port(session);
|
2022-08-26 02:27:11 +00:00
|
|
|
|
|
|
|
if (session->allowsTCP())
|
|
|
|
{
|
|
|
|
open_incoming_peer_port(session);
|
|
|
|
}
|
|
|
|
|
2022-08-12 00:59:58 +00:00
|
|
|
tr_sharedPortChanged(*session);
|
2009-05-14 13:42:29 +00:00
|
|
|
|
2022-03-01 23:06:29 +00:00
|
|
|
for (auto* const tor : session->torrents())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_torrentChangeMyPort(tor);
|
|
|
|
}
|
2006-09-25 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void setPeerPort(tr_session* session, tr_port port)
|
2010-04-28 04:57:21 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
session->private_peer_port = port;
|
|
|
|
session->public_peer_port = port;
|
2010-04-28 04:57:21 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_runInEventThread(session, peerPortChanged, session);
|
2010-04-28 04:57:21 +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-08-14 14:16:08 +00:00
|
|
|
if (auto const port = tr_port::fromHost(hport); session != nullptr && session->private_peer_port != port)
|
2009-10-23 03:41:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
setPeerPort(session, port);
|
2009-10-23 03:41:36 +00:00
|
|
|
}
|
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-08-14 14:16:08 +00:00
|
|
|
return session != nullptr ? session->public_peer_port.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-08-15 00:33:28 +00:00
|
|
|
session->is_port_random_ = 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
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_port_forwarding 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
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
return tr_port_forwarding(tr_sharedTraversalStatus(session->shared));
|
2006-09-25 18:37:45 +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-08-15 17:48:05 +00:00
|
|
|
session->is_ratio_limited_ = 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-08-15 00:33:28 +00:00
|
|
|
session->desired_ratio_ = 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
|
|
|
}
|
|
|
|
|
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-08-15 00:33:28 +00:00
|
|
|
session->is_idle_limited_ = 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-08-15 17:48:05 +00:00
|
|
|
session->idle_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
|
|
|
}
|
|
|
|
|
2009-03-01 13:56:22 +00:00
|
|
|
/***
|
2010-02-04 23:39:50 +00:00
|
|
|
****
|
2009-03-28 16:47:01 +00:00
|
|
|
**** SPEED LIMITS
|
2010-02-04 23:39:50 +00:00
|
|
|
****
|
2009-03-01 13:56:22 +00:00
|
|
|
***/
|
|
|
|
|
2021-10-05 18:54:02 +00:00
|
|
|
static unsigned int tr_sessionGetAltSpeed_Bps(tr_session const* s, tr_direction d);
|
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
std::optional<unsigned int> 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-08-14 19:41:57 +00:00
|
|
|
return tr_sessionGetAltSpeed_Bps(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-08-14 19:41:57 +00:00
|
|
|
return speedLimitBps(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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void updateBandwidth(tr_session* session, tr_direction dir)
|
2008-12-13 22:52:15 +00:00
|
|
|
{
|
2022-08-14 19:41:57 +00:00
|
|
|
if (auto const limit_Bps = session->activeSpeedLimitBps(dir); limit_Bps)
|
|
|
|
{
|
|
|
|
session->top_bandwidth_.setLimited(dir, *limit_Bps > 0U);
|
|
|
|
session->top_bandwidth_.setDesiredSpeedBytesPerSecond(dir, *limit_Bps);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
session->top_bandwidth_.setLimited(dir, false);
|
|
|
|
}
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 00:39:05 +00:00
|
|
|
static auto constexpr MinutesPerHour = int{ 60 };
|
|
|
|
static auto constexpr MinutesPerDay = int{ MinutesPerHour * 24 };
|
|
|
|
static auto constexpr MinutesPerWeek = int{ MinutesPerDay * 7 };
|
2010-03-10 22:19:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void turtleUpdateTable(struct tr_turtle_info* t)
|
2010-02-04 23:39:50 +00:00
|
|
|
{
|
2022-08-15 21:25:55 +00:00
|
|
|
t->minutes.setHasNone();
|
2010-02-04 23:39:50 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int day = 0; day < 7; ++day)
|
2010-03-10 22:19:31 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((t->days & (1 << day)) != 0)
|
2010-03-10 22:19:31 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t const begin = t->beginMinute;
|
2017-04-19 12:04:45 +00:00
|
|
|
time_t end = t->endMinute;
|
2010-03-10 22:19:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (end <= begin)
|
|
|
|
{
|
2021-10-21 00:39:05 +00:00
|
|
|
end += MinutesPerDay;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-03-10 22:19:31 +00:00
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
for (time_t i = begin; i < end; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-15 21:25:55 +00:00
|
|
|
t->minutes.set((i + day * MinutesPerDay) % MinutesPerWeek);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-03-10 22:19:31 +00:00
|
|
|
}
|
2010-02-04 23:39:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-05 04:26:03 +00:00
|
|
|
static void altSpeedToggled(tr_session* const session)
|
2009-03-28 16:47:01 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
updateBandwidth(session, TR_UP);
|
|
|
|
updateBandwidth(session, TR_DOWN);
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
struct tr_turtle_info* t = &session->turtle;
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (t->callback != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
(*t->callback)(session, t->isEnabled, t->changedByUser, t->callbackUserData);
|
|
|
|
}
|
2009-03-28 16:47:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void useAltSpeed(tr_session* s, struct tr_turtle_info* t, bool enabled, bool byUser)
|
2009-03-28 16:47:01 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(s != nullptr);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(t != nullptr);
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (t->isEnabled != enabled)
|
2010-02-04 23:39:50 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
t->isEnabled = enabled;
|
|
|
|
t->changedByUser = byUser;
|
|
|
|
tr_runInEventThread(s, altSpeedToggled, s);
|
2010-02-04 23:39:50 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2010-03-12 19:35:28 +00:00
|
|
|
/**
|
2012-09-18 03:46:01 +00:00
|
|
|
* @return whether turtle should be on/off according to the scheduler
|
2010-03-12 19:35:28 +00:00
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool getInTurtleTime(struct tr_turtle_info const* t)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-04-22 19:56:54 +00:00
|
|
|
auto const now = tr_time();
|
|
|
|
struct tm const tm = fmt::localtime(now);
|
2010-03-10 22:19:31 +00:00
|
|
|
|
2021-10-21 00:39:05 +00:00
|
|
|
size_t minute_of_the_week = tm.tm_wday * MinutesPerDay + tm.tm_hour * MinutesPerHour + tm.tm_min;
|
2010-03-10 22:19:31 +00:00
|
|
|
|
2021-10-21 00:39:05 +00:00
|
|
|
if (minute_of_the_week >= MinutesPerWeek) /* leap minutes? */
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-21 00:39:05 +00:00
|
|
|
minute_of_the_week = MinutesPerWeek - 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 21:25:55 +00:00
|
|
|
return t->minutes.test(minute_of_the_week);
|
2012-09-18 00:58:10 +00:00
|
|
|
}
|
2010-03-12 19:35:28 +00:00
|
|
|
|
2021-10-06 22:24:04 +00:00
|
|
|
static constexpr tr_auto_switch_state_t autoSwitchState(bool enabled)
|
2012-09-18 00:58:10 +00:00
|
|
|
{
|
|
|
|
return enabled ? TR_AUTO_SWITCH_ON : TR_AUTO_SWITCH_OFF;
|
2010-03-10 22:19:31 +00:00
|
|
|
}
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void turtleCheckClock(tr_session* s, struct tr_turtle_info* t)
|
2010-03-10 22:19:31 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(t->isClockEnabled);
|
2010-03-12 19:35:28 +00:00
|
|
|
|
2022-07-27 14:03:13 +00:00
|
|
|
bool const enabled = getInTurtleTime(t);
|
|
|
|
tr_auto_switch_state_t const newAutoTurtleState = autoSwitchState(enabled);
|
|
|
|
bool const alreadySwitched = t->autoTurtleState == newAutoTurtleState;
|
2010-03-12 19:35:28 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!alreadySwitched)
|
2010-03-12 19:35:28 +00:00
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddInfo(enabled ? _("Time to turn on turtle mode") : _("Time to turn off turtle mode"));
|
2017-04-19 12:04:45 +00:00
|
|
|
t->autoTurtleState = newAutoTurtleState;
|
|
|
|
useAltSpeed(s, t, enabled, false);
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
2010-02-04 23:39:50 +00:00
|
|
|
}
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2010-02-04 23:39:50 +00:00
|
|
|
/* Called after the turtle's fields are loaded from an outside source.
|
2010-03-17 17:07:40 +00:00
|
|
|
* It initializes the implementation fields
|
2010-02-04 23:39:50 +00:00
|
|
|
* and turns on turtle mode if the clock settings say to. */
|
2017-04-19 12:04:45 +00:00
|
|
|
static void turtleBootstrap(tr_session* session, struct tr_turtle_info* turtle)
|
2010-02-04 23:39:50 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
turtle->changedByUser = false;
|
|
|
|
turtle->autoTurtleState = TR_AUTO_SWITCH_UNUSED;
|
2022-08-15 21:25:55 +00:00
|
|
|
turtle->minutes.setHasNone();
|
2010-03-10 22:19:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
turtleUpdateTable(turtle);
|
2010-03-10 22:19:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (turtle->isClockEnabled)
|
2012-09-18 00:58:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
turtle->isEnabled = getInTurtleTime(turtle);
|
|
|
|
turtle->autoTurtleState = autoSwitchState(turtle->isEnabled);
|
2012-09-18 00:58:10 +00:00
|
|
|
}
|
2010-02-16 00:00:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
altSpeedToggled(session);
|
2008-12-13 22:52:15 +00:00
|
|
|
}
|
|
|
|
|
2009-03-28 16:47:01 +00:00
|
|
|
/***
|
|
|
|
**** Primary session speed limits
|
|
|
|
***/
|
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
void tr_sessionSetSpeedLimit_Bps(tr_session* session, tr_direction dir, unsigned int Bps)
|
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-08-15 17:48:05 +00:00
|
|
|
session->speed_limit_Bps_[dir] = Bps;
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
updateBandwidth(session, dir);
|
2009-03-28 16:47:01 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
void tr_sessionSetSpeedLimit_KBps(tr_session* session, tr_direction dir, unsigned int KBps)
|
2010-07-04 06:07:21 +00:00
|
|
|
{
|
2022-08-15 17:48:05 +00:00
|
|
|
tr_sessionSetSpeedLimit_Bps(session, dir, tr_toSpeedBytes(KBps));
|
2010-07-04 06:07:21 +00:00
|
|
|
}
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
unsigned int tr_sessionGetSpeedLimit_KBps(tr_session const* s, tr_direction d)
|
2010-07-04 06:07:21 +00:00
|
|
|
{
|
2022-08-14 19:41:57 +00:00
|
|
|
return tr_toSpeedKBps(s->speedLimitBps(d));
|
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-08-15 17:48:05 +00:00
|
|
|
session->speed_limit_enabled_[dir] = limited;
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
updateBandwidth(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
|
|
|
}
|
|
|
|
|
2009-03-28 16:47:01 +00:00
|
|
|
/***
|
2009-08-10 20:04:08 +00:00
|
|
|
**** Alternative speed limits that are used during scheduled times
|
2009-03-28 16:47:01 +00:00
|
|
|
***/
|
|
|
|
|
2021-10-05 18:54:02 +00:00
|
|
|
static void tr_sessionSetAltSpeed_Bps(tr_session* s, tr_direction d, unsigned int Bps)
|
2007-07-20 08:33:59 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(s != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isDirection(d));
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
s->turtle.speedLimit_Bps[d] = Bps;
|
2008-12-13 22:52:15 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
updateBandwidth(s, d);
|
2007-07-20 08:33:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetAltSpeed_KBps(tr_session* s, tr_direction d, unsigned int KBps)
|
2010-07-04 06:07:21 +00:00
|
|
|
{
|
2021-12-15 21:25:42 +00:00
|
|
|
tr_sessionSetAltSpeed_Bps(s, d, tr_toSpeedBytes(KBps));
|
2010-07-04 06:07:21 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 18:54:02 +00:00
|
|
|
static unsigned int tr_sessionGetAltSpeed_Bps(tr_session const* s, tr_direction d)
|
2009-03-25 19:50:58 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(s != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isDirection(d));
|
2009-03-28 16:47:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return s->turtle.speedLimit_Bps[d];
|
2009-03-25 19:50:58 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
unsigned int tr_sessionGetAltSpeed_KBps(tr_session const* s, tr_direction d)
|
2010-07-04 06:07:21 +00:00
|
|
|
{
|
2021-12-15 21:25:42 +00:00
|
|
|
return tr_toSpeedKBps(tr_sessionGetAltSpeed_Bps(s, d));
|
2010-07-04 06:07:21 +00:00
|
|
|
}
|
2009-03-25 19:50:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void userPokedTheClock(tr_session* s, struct tr_turtle_info* t)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTrace("Refreshing the turtle mode clock due to user changes");
|
2009-03-29 16:44:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
t->autoTurtleState = TR_AUTO_SWITCH_UNUSED;
|
2012-09-18 00:58:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
turtleUpdateTable(t);
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (t->isClockEnabled)
|
2010-03-12 19:35:28 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const enabled = getInTurtleTime(t);
|
2017-04-19 12:04:45 +00:00
|
|
|
useAltSpeed(s, t, enabled, true);
|
|
|
|
t->autoTurtleState = autoSwitchState(enabled);
|
2010-03-12 19:35:28 +00:00
|
|
|
}
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
2010-02-04 23:39:50 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionUseAltSpeedTime(tr_session* s, bool b)
|
2009-03-30 17:25:35 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(s != nullptr);
|
2010-02-04 23:39:50 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
struct tr_turtle_info* t = &s->turtle;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (t->isClockEnabled != b)
|
2013-05-23 05:43:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
t->isClockEnabled = b;
|
|
|
|
userPokedTheClock(s, t);
|
2010-02-04 23:39:50 +00:00
|
|
|
}
|
2009-03-30 17:25:35 +00:00
|
|
|
}
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionUsesAltSpeedTime(tr_session const* s)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(s != nullptr);
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return s->turtle.isClockEnabled;
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetAltSpeedBegin(tr_session* s, int minutes_since_midnight)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(s != nullptr);
|
2022-08-03 06:15:37 +00:00
|
|
|
TR_ASSERT(minutes_since_midnight >= 0);
|
|
|
|
TR_ASSERT(minutes_since_midnight < 60 * 24);
|
2009-03-25 19:50:58 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
if (s->turtle.beginMinute != minutes_since_midnight)
|
2013-05-23 05:43:12 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
s->turtle.beginMinute = minutes_since_midnight;
|
2017-04-19 12:04:45 +00:00
|
|
|
userPokedTheClock(s, &s->turtle);
|
2009-03-28 16:47:01 +00:00
|
|
|
}
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int tr_sessionGetAltSpeedBegin(tr_session const* s)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(s != nullptr);
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return s->turtle.beginMinute;
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetAltSpeedEnd(tr_session* s, int minutes_since_midnight)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(s != nullptr);
|
2022-08-03 06:15:37 +00:00
|
|
|
TR_ASSERT(minutes_since_midnight >= 0);
|
|
|
|
TR_ASSERT(minutes_since_midnight < 60 * 24);
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
if (s->turtle.endMinute != minutes_since_midnight)
|
2013-05-23 05:43:12 +00:00
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
s->turtle.endMinute = minutes_since_midnight;
|
2017-04-19 12:04:45 +00:00
|
|
|
userPokedTheClock(s, &s->turtle);
|
2009-03-28 16:47:01 +00:00
|
|
|
}
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int tr_sessionGetAltSpeedEnd(tr_session const* s)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(s != nullptr);
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return s->turtle.endMinute;
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetAltSpeedDay(tr_session* s, tr_sched_day days)
|
2009-04-04 02:17:39 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(s != nullptr);
|
2009-04-04 02:17:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (s->turtle.days != days)
|
2013-05-23 05:43:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
s->turtle.days = days;
|
|
|
|
userPokedTheClock(s, &s->turtle);
|
2009-04-04 02:17:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_sched_day tr_sessionGetAltSpeedDay(tr_session const* s)
|
2009-04-04 02:17:39 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(s != nullptr);
|
2009-04-04 02:17:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return s->turtle.days;
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
useAltSpeed(session, &session->turtle, enabled, true);
|
2009-03-30 17:25:35 +00:00
|
|
|
}
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionUsesAltSpeed(tr_session const* s)
|
2009-03-25 19:18:00 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(s != nullptr);
|
2009-03-25 19:18:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return s->turtle.isEnabled;
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetAltSpeedFunc(tr_session* session, tr_altSpeedFunc func, void* userData)
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
session->turtle.callback = func;
|
|
|
|
session->turtle.callbackUserData = userData;
|
2009-03-25 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
2008-05-12 16:33:17 +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-08-13 22:41:42 +00:00
|
|
|
session->peer_limit_ = 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-08-13 22:41:42 +00:00
|
|
|
session->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
|
|
|
}
|
|
|
|
|
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-08-14 01:04:36 +00:00
|
|
|
session->should_pause_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-08-14 01:04:36 +00:00
|
|
|
session->should_delete_source_torrents_ = delete_source;
|
2010-03-17 19:23:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sessionGetDeleteSource(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->shouldDeleteSource();
|
2010-03-17 19:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-05 18:54:02 +00:00
|
|
|
static unsigned int tr_sessionGetRawSpeed_Bps(tr_session const* session, tr_direction dir)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
return session != nullptr ? session->top_bandwidth_.getRawSpeedBytesPerSecond(0, dir) : 0;
|
2010-07-04 06:07:21 +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
|
|
|
{
|
2021-12-15 21:25:42 +00:00
|
|
|
return tr_toSpeedKBps(tr_sessionGetRawSpeed_Bps(session, dir));
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 17:22:23 +00:00
|
|
|
void tr_session::closeImplStart()
|
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-08-24 21:03:30 +00:00
|
|
|
lpd_.reset();
|
2013-02-15 01:52:47 +00:00
|
|
|
|
2022-08-12 17:22:23 +00:00
|
|
|
tr_dhtUninit(this);
|
2009-05-14 13:42:29 +00:00
|
|
|
|
2022-08-12 17:22:23 +00:00
|
|
|
save_timer_.reset();
|
|
|
|
now_timer_.reset();
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2022-08-12 17:22:23 +00:00
|
|
|
tr_verifyClose(this);
|
|
|
|
tr_sharedClose(*this);
|
2022-01-21 15:44:45 +00:00
|
|
|
|
2022-08-12 17:22:23 +00:00
|
|
|
close_incoming_peer_port(this);
|
|
|
|
this->rpc_server_.reset();
|
2009-08-13 14:47:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Close the torrents. Get the most active ones first so that
|
|
|
|
* if we can't get them all closed in a reasonable amount of time,
|
|
|
|
* at least we get the most important ones 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)
|
|
|
|
{
|
|
|
|
auto const aCur = a->downloadedCur + a->uploadedCur;
|
|
|
|
auto const bCur = b->downloadedCur + b->uploadedCur;
|
|
|
|
return aCur > bCur; // larger xfers go first
|
|
|
|
});
|
2009-11-26 18:47:08 +00:00
|
|
|
|
2021-09-24 23:31:02 +00:00
|
|
|
for (auto* tor : torrents)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-24 23:31:02 +00:00
|
|
|
tr_torrentFree(tor);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-11-29 02:31:21 +00:00
|
|
|
|
2021-09-24 23:31:02 +00:00
|
|
|
torrents.clear();
|
2013-05-23 05:43:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Close the announcer *after* closing the torrents
|
|
|
|
so that all the &event=stopped messages will be
|
2017-04-21 07:40:57 +00:00
|
|
|
queued to be sent by tr_announcerClose() */
|
2022-08-12 17:22:23 +00:00
|
|
|
tr_announcerClose(this);
|
2013-05-23 05:43:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* and this goes *after* announcer close so that
|
|
|
|
it won't be idle until the announce events are sent... */
|
2022-08-12 17:22:23 +00:00
|
|
|
this->web->closeSoon();
|
2013-05-23 05:43:12 +00:00
|
|
|
|
2022-08-12 17:22:23 +00:00
|
|
|
this->cache.reset();
|
2015-12-31 14:24:15 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* saveTimer is not used at this point, reusing for UDP shutdown wait */
|
2022-08-12 17:22:23 +00:00
|
|
|
TR_ASSERT(!save_timer_);
|
|
|
|
save_timer_ = timerMaker().create([this]() { closeImplWaitForIdleUdp(); });
|
|
|
|
save_timer_->start(1ms);
|
2015-12-31 14:17:37 +00:00
|
|
|
}
|
2013-05-23 05:43:12 +00:00
|
|
|
|
2022-08-12 17:22:23 +00:00
|
|
|
void tr_session::closeImplWaitForIdleUdp()
|
2015-12-31 14:17:37 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* gotta keep udp running long enough to send out all
|
|
|
|
the &event=stopped UDP tracker messages */
|
2022-08-12 17:22:23 +00:00
|
|
|
if (!tr_tracker_udp_is_idle(this))
|
2015-12-31 14:17:37 +00:00
|
|
|
{
|
2022-08-12 17:22:23 +00:00
|
|
|
tr_tracker_udp_upkeep(this);
|
|
|
|
save_timer_->start(100ms);
|
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
|
|
|
closeImplFinish();
|
2015-12-31 14:17:37 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 17:22:23 +00:00
|
|
|
void tr_session::closeImplFinish()
|
2015-12-31 14:24:15 +00:00
|
|
|
{
|
2022-08-12 17:22:23 +00:00
|
|
|
save_timer_.reset();
|
2017-01-17 20:55:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* we had to wait until UDP trackers were closed before closing these: */
|
2022-08-12 17:22:23 +00:00
|
|
|
tr_tracker_udp_close(this);
|
|
|
|
tr_udpUninit(this);
|
2015-12-31 14:24:15 +00:00
|
|
|
|
2022-08-12 17:22:23 +00:00
|
|
|
stats().saveIfDirty();
|
|
|
|
tr_peerMgrFree(peerMgr);
|
|
|
|
tr_utpClose(this);
|
2022-08-15 17:48:05 +00:00
|
|
|
blocklists_.clear();
|
2022-08-12 17:22:23 +00:00
|
|
|
openFiles().closeAll();
|
2022-08-14 01:04:36 +00:00
|
|
|
is_closed_ = true;
|
2015-12-31 14:17:37 +00:00
|
|
|
}
|
|
|
|
|
2019-07-14 12:40:41 +00:00
|
|
|
static bool deadlineReached(time_t const deadline)
|
2007-11-05 05:42:25 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return time(nullptr) >= deadline;
|
2007-11-05 05:42:25 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 18:39:50 +00:00
|
|
|
static auto constexpr ShutdownMaxSeconds = time_t{ 20 };
|
2007-11-29 00:43:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionClose(tr_session* session)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(session != nullptr);
|
2009-01-29 16:56:43 +00:00
|
|
|
|
2022-01-17 18:39:50 +00:00
|
|
|
time_t const deadline = time(nullptr) + ShutdownMaxSeconds;
|
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)));
|
|
|
|
tr_logAddDebug(fmt::format("now is {}, deadline is {}", time(nullptr), deadline));
|
2008-02-04 19:54:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* close the session */
|
2022-08-12 17:22:23 +00:00
|
|
|
tr_runInEventThread(session, [session]() { session->closeImplStart(); });
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-14 01:04:36 +00:00
|
|
|
while (!session->isClosed() && !deadlineReached(deadline))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTrace("waiting for the libtransmission thread to finish");
|
2022-03-04 22:43:56 +00:00
|
|
|
tr_wait_msec(10);
|
2008-08-01 13:46:03 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* "shared" and "tracker" have live sockets,
|
|
|
|
* so we need to keep the transmission thread alive
|
|
|
|
* for a bit while they tell the router & tracker
|
|
|
|
* that we're closing now */
|
2022-02-16 18:33:50 +00:00
|
|
|
while ((session->shared != nullptr || !session->web->isClosed() || session->announcer != nullptr ||
|
2021-09-15 00:18:09 +00:00
|
|
|
session->announcer_udp != nullptr) &&
|
2021-08-15 09:41:48 +00:00
|
|
|
!deadlineReached(deadline))
|
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTrace(fmt::format(
|
|
|
|
"waiting on port unmap ({}) or announcer ({})... now {} deadline {}",
|
|
|
|
fmt::ptr(session->shared),
|
|
|
|
fmt::ptr(session->announcer),
|
|
|
|
time(nullptr),
|
|
|
|
deadline));
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_wait_msec(50);
|
2008-08-01 13:46:03 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
session->web.reset();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* close the libtransmission thread */
|
|
|
|
tr_eventClose(session);
|
2010-03-06 15:05:05 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
while (session->events != nullptr)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
static bool forced = false;
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTrace(
|
2022-03-14 04:43:35 +00:00
|
|
|
fmt::format("waiting for libtransmission thread to finish... now {} deadline {}", time(nullptr), deadline));
|
2022-03-04 22:43:56 +00:00
|
|
|
tr_wait_msec(10);
|
2013-05-23 05:43:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (deadlineReached(deadline) && !forced)
|
2009-11-30 04:58:03 +00:00
|
|
|
{
|
2022-03-11 21:09:22 +00:00
|
|
|
tr_logAddTrace("calling event_loopbreak()");
|
2017-04-19 12:04:45 +00:00
|
|
|
forced = true;
|
2022-08-08 22:53:20 +00:00
|
|
|
event_base_loopbreak(session->eventBase());
|
2010-08-07 03:29:47 +00:00
|
|
|
}
|
2013-05-23 05:43:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (deadlineReached(deadline + 3))
|
2010-08-07 03:29:47 +00:00
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddTrace("deadline+3 reached... calling break...");
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2009-11-30 04:58:03 +00:00
|
|
|
}
|
2008-08-01 13:46:03 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +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
|
|
|
|
2011-06-19 18:30:57 +00:00
|
|
|
struct sessionLoadTorrentsData
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_session* session;
|
|
|
|
tr_ctor* ctor;
|
|
|
|
bool done;
|
2011-06-19 18:30:57 +00:00
|
|
|
};
|
2007-07-21 18:46:54 +00:00
|
|
|
|
2022-03-05 04:26:03 +00:00
|
|
|
static void sessionLoadTorrents(struct sessionLoadTorrentsData* const data)
|
2011-06-19 18:30:57 +00:00
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
TR_ASSERT(data->session != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2022-07-23 07:13:18 +00:00
|
|
|
auto const& dirname = data->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
|
|
|
|
2021-09-26 22:23:11 +00:00
|
|
|
auto torrents = std::list<tr_torrent*>{};
|
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-07-23 07:13:18 +00:00
|
|
|
auto const path = tr_pathbuf{ dirname, '/', name };
|
2022-02-24 21:52:29 +00:00
|
|
|
|
|
|
|
// is a magnet link?
|
2022-05-31 15:53:46 +00:00
|
|
|
if (!tr_ctorSetMetainfoFromFile(data->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-05-31 15:53:46 +00:00
|
|
|
tr_ctorSetMetainfoFromMagnetLink(data->ctor, std::string_view{ std::data(buf), std::size(buf) }, nullptr);
|
2022-02-24 21:52:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-27 00:32:36 +00:00
|
|
|
if (tr_torrent* const tor = tr_torrentNew(data->ctor, nullptr); tor != nullptr)
|
|
|
|
{
|
|
|
|
torrents.push_back(tor);
|
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-08-18 14:14:12 +00:00
|
|
|
if (auto const n = std::size(torrents); n != 0U)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-15 14:52:16 +00:00
|
|
|
tr_logAddInfo(fmt::format(ngettext("Loaded {count} torrent", "Loaded {count} torrents", n), fmt::arg("count", n)));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data->done = true;
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct sessionLoadTorrentsData data;
|
2011-06-19 18:30:57 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
data.session = session;
|
|
|
|
data.ctor = ctor;
|
|
|
|
data.done = false;
|
|
|
|
tr_runInEventThread(session, sessionLoadTorrents, &data);
|
|
|
|
while (!data.done)
|
|
|
|
{
|
|
|
|
tr_wait_msec(100);
|
|
|
|
}
|
|
|
|
|
2022-08-18 14:14:12 +00:00
|
|
|
return std::size(session->torrents());
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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-08-15 00:33:28 +00:00
|
|
|
session->is_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-08-14 19:41:57 +00:00
|
|
|
if (enabled == session->allowsDHT())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-14 19:41:57 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-08-14 19:41:57 +00:00
|
|
|
|
|
|
|
tr_runInEventThread(
|
|
|
|
session,
|
|
|
|
[session, enabled]()
|
|
|
|
{
|
|
|
|
tr_udpUninit(session);
|
|
|
|
session->is_dht_enabled_ = enabled;
|
|
|
|
tr_udpInit(session);
|
|
|
|
});
|
2009-05-21 00:15:54 +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-08-15 17:48:05 +00:00
|
|
|
return is_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
|
|
|
}
|
2022-08-15 17:48:05 +00:00
|
|
|
tr_runInEventThread(
|
|
|
|
session,
|
|
|
|
[session, enabled]()
|
|
|
|
{
|
|
|
|
session->is_utp_enabled_ = enabled;
|
|
|
|
tr_udpSetSocketBuffers(session);
|
|
|
|
tr_udpSetSocketTOS(session);
|
|
|
|
// But don't call tr_utpClose --
|
|
|
|
// see reset_timer in tr-utp.c for an explanation.
|
|
|
|
});
|
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-08-14 19:41:57 +00:00
|
|
|
if (enabled == session->allowsLPD())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-14 19:41:57 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-08-07 03:29:47 +00:00
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
tr_runInEventThread(
|
|
|
|
session,
|
|
|
|
[session, enabled]()
|
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
session->lpd_.reset();
|
2022-08-14 19:41:57 +00:00
|
|
|
session->is_lpd_enabled_ = enabled;
|
2022-08-24 21:03:30 +00:00
|
|
|
if (enabled)
|
2022-08-14 19:41:57 +00:00
|
|
|
{
|
2022-08-24 21:03:30 +00:00
|
|
|
session->lpd_ = tr_lpd::create(session->lpd_mediator_, session->timerMaker(), session->eventBase());
|
2022-08-14 19:41:57 +00:00
|
|
|
}
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
2008-04-01 19:20:21 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetCacheLimit_MB(tr_session* session, int 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-08-03 06:15:37 +00:00
|
|
|
session->cache->setLimit(tr_toMemBytes(mb));
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int 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-06-12 00:46:30 +00:00
|
|
|
return tr_toMemMB(session->cache->getLimit());
|
2010-07-07 23:37:03 +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_;
|
|
|
|
|
|
|
|
default_trackers_str_ = trackers;
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
tr_announcerResetTorrent(announcer, tor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionSetPortForwardingEnabled(tr_session* session, bool enabled)
|
2010-02-06 20:25:25 +00:00
|
|
|
{
|
2022-07-27 07:07:44 +00:00
|
|
|
tr_runInEventThread(session, tr_sharedTraversalEnable, session->shared, 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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_sharedTraversalIsEnabled(session->shared);
|
2008-05-12 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
void tr_session::loadBlocklists()
|
2009-12-01 00:07:18 +00:00
|
|
|
{
|
2021-09-29 21:23:34 +00:00
|
|
|
auto loadme = std::unordered_set<std::string>{};
|
2022-08-15 17:48:05 +00:00
|
|
|
auto const is_enabled = useBlocklist();
|
2013-01-21 21:14:14 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* walk the blocklist directory... */
|
2022-08-15 17:48:05 +00:00
|
|
|
auto const dirname = tr_pathbuf{ configDir(), "/blocklists"sv };
|
2022-04-22 16:35:56 +00:00
|
|
|
auto const odir = tr_sys_dir_open(dirname);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (odir == TR_BAD_SYS_DIR)
|
2014-01-21 02:27:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return;
|
2014-01-21 02:27:46 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 12:51:02 +00:00
|
|
|
char const* name = nullptr;
|
2022-03-27 17:37:29 +00:00
|
|
|
while ((name = tr_sys_dir_read_name(odir)) != nullptr)
|
2009-12-01 00:07:18 +00:00
|
|
|
{
|
2021-11-13 00:10:04 +00:00
|
|
|
auto load = std::string{};
|
2015-01-02 11:15:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (name[0] == '.') /* ignore dotfiles */
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2013-01-21 21:14:14 +00:00
|
|
|
|
2022-04-22 16:35:56 +00:00
|
|
|
if (auto const path = tr_pathbuf{ dirname, '/', name }; tr_strvEndsWith(path, ".bin"sv))
|
2013-01-21 21:14:14 +00:00
|
|
|
{
|
2021-11-13 00:10:04 +00:00
|
|
|
load = path;
|
2013-01-21 21:14:14 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2009-12-01 00:07:18 +00:00
|
|
|
{
|
2022-04-07 22:26:59 +00:00
|
|
|
auto const binname = tr_pathbuf{ dirname, '/', name, ".bin"sv };
|
2009-12-01 00:07:18 +00:00
|
|
|
|
2022-08-02 17:41:04 +00:00
|
|
|
if (auto const bininfo = tr_sys_path_get_info(binname); !bininfo)
|
2013-01-21 21:14:14 +00:00
|
|
|
{
|
2022-08-02 17:41:04 +00:00
|
|
|
// create it
|
|
|
|
auto b = BlocklistFile{ binname, is_enabled };
|
2022-05-15 16:32:22 +00:00
|
|
|
if (auto const n = b.setContent(path); n > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-13 00:10:04 +00:00
|
|
|
load = binname;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-01-21 21:14:14 +00:00
|
|
|
}
|
2022-08-02 17:41:04 +00:00
|
|
|
else if (auto const pathinfo = tr_sys_path_get_info(path);
|
2022-08-02 19:46:08 +00:00
|
|
|
pathinfo && pathinfo->last_modified_at >= bininfo->last_modified_at)
|
2009-12-01 00:07:18 +00:00
|
|
|
{
|
2022-08-02 17:41:04 +00:00
|
|
|
// update it
|
2022-04-07 22:26:59 +00:00
|
|
|
auto const old = tr_pathbuf{ binname, ".old"sv };
|
|
|
|
tr_sys_path_remove(old);
|
|
|
|
tr_sys_path_rename(binname, old);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-02 17:41:04 +00:00
|
|
|
BlocklistFile b(binname, is_enabled);
|
2022-05-15 16:32:22 +00:00
|
|
|
|
|
|
|
if (b.setContent(path) > 0)
|
2009-12-01 00:07:18 +00:00
|
|
|
{
|
2022-04-07 22:26:59 +00:00
|
|
|
tr_sys_path_remove(old);
|
2009-12-01 00:07:18 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2013-01-21 21:14:14 +00:00
|
|
|
{
|
2022-04-07 22:26:59 +00:00
|
|
|
tr_sys_path_remove(binname);
|
|
|
|
tr_sys_path_rename(old, binname);
|
2013-01-21 21:14:14 +00:00
|
|
|
}
|
2009-12-01 00:07:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 00:10:04 +00:00
|
|
|
if (!std::empty(load))
|
2013-01-21 21:14:14 +00:00
|
|
|
{
|
2021-09-29 21:23:34 +00:00
|
|
|
loadme.emplace(load);
|
2013-01-21 21:14:14 +00:00
|
|
|
}
|
2009-12-01 00:07:18 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 17:48:05 +00:00
|
|
|
blocklists_.clear();
|
2021-09-29 21:23:34 +00:00
|
|
|
std::transform(
|
|
|
|
std::begin(loadme),
|
|
|
|
std::end(loadme),
|
2022-08-15 17:48:05 +00:00
|
|
|
std::back_inserter(blocklists_),
|
2022-08-02 17:41:04 +00:00
|
|
|
[&is_enabled](auto const& path) { return std::make_unique<BlocklistFile>(path.c_str(), is_enabled); });
|
2009-12-01 00:07:18 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* cleanup */
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_dir_close(odir);
|
2009-12-01 00:07:18 +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-08-15 17:48:05 +00:00
|
|
|
this->blocklist_enabled_ = enabled;
|
|
|
|
|
|
|
|
std::for_each(
|
|
|
|
std::begin(blocklists_),
|
|
|
|
std::end(blocklists_),
|
|
|
|
[enabled](auto& blocklist) { blocklist->setEnabled(enabled); });
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tr_session::addressIsBlocked(tr_address const& addr) const noexcept
|
|
|
|
{
|
|
|
|
return std::any_of(
|
|
|
|
std::begin(blocklists_),
|
|
|
|
std::end(blocklists_),
|
|
|
|
[&addr](auto& blocklist) { return blocklist->hasAddress(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-08-15 17:48:05 +00:00
|
|
|
session->blocklists_.clear();
|
|
|
|
session->loadBlocklists();
|
2010-06-14 12:01:50 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_peerMgrOnBlocklistChanged(session->peerMgr);
|
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-05-15 16:32:22 +00:00
|
|
|
return std::accumulate(std::begin(src), std::end(src), 0, [](int sum, auto& cur) { return sum + cur->getRuleCount(); });
|
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
|
|
|
|
2021-09-27 18:28:41 +00:00
|
|
|
// find (or add) the default blocklist
|
2022-08-15 17:48:05 +00:00
|
|
|
auto& src = session->blocklists_;
|
2021-09-27 18:28:41 +00:00
|
|
|
char const* const name = DEFAULT_BLOCKLIST_FILENAME;
|
|
|
|
auto const it = std::find_if(
|
|
|
|
std::begin(src),
|
|
|
|
std::end(src),
|
2022-08-27 19:05:21 +00:00
|
|
|
[&name](auto const& blocklist) { return tr_strvEndsWith(blocklist->filename(), name); });
|
2021-12-16 22:58:58 +00:00
|
|
|
|
2022-05-15 16:32:22 +00:00
|
|
|
BlocklistFile* b = nullptr;
|
2021-09-27 18:28:41 +00:00
|
|
|
if (it == std::end(src))
|
|
|
|
{
|
2022-07-23 07:13:18 +00:00
|
|
|
auto path = tr_pathbuf{ session->configDir(), "/blocklists/"sv, name };
|
2022-05-15 16:32:22 +00:00
|
|
|
src.push_back(std::make_unique<BlocklistFile>(path, session->useBlocklist()));
|
|
|
|
b = std::rbegin(src)->get();
|
2008-06-11 16:15:45 +00:00
|
|
|
}
|
2021-09-27 18:28:41 +00:00
|
|
|
else
|
|
|
|
{
|
2022-05-15 16:32:22 +00:00
|
|
|
b = it->get();
|
2021-09-27 18:28:41 +00:00
|
|
|
}
|
2008-06-11 16:15:45 +00:00
|
|
|
|
2021-09-27 18:28:41 +00:00
|
|
|
// set the default blocklist's content
|
2022-08-15 17:48:05 +00:00
|
|
|
auto const rule_count = b->setContent(content_filename);
|
|
|
|
return rule_count;
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetQueueSize(tr_session* session, tr_direction dir, int max_simultaneous_seed_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-08-13 19:56:10 +00:00
|
|
|
session->queue_size_[dir] = max_simultaneous_seed_torrents;
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int 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-08-03 06:15:37 +00:00
|
|
|
void tr_sessionSetQueueEnabled(tr_session* session, tr_direction dir, bool do_limit_simultaneous_seed_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-08-13 19:56:10 +00:00
|
|
|
session->queue_enabled_[dir] = do_limit_simultaneous_seed_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-08-13 19:56:10 +00:00
|
|
|
session->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-08-13 19:56:10 +00:00
|
|
|
session->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
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int 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 */
|
|
|
|
int active_count = 0;
|
2022-08-14 19:41:57 +00:00
|
|
|
bool const stalled_enabled = queueStalledEnabled();
|
|
|
|
int 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? */
|
|
|
|
if (activity != tr_torrentGetActivity(tor))
|
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? */
|
|
|
|
if (stalled_enabled)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
auto const idle_secs = int(difftime(now, std::max(tor->startDate, tor->activityDate)));
|
2021-09-10 20:15:35 +00:00
|
|
|
if (idle_secs >= stalled_if_idle_for_n_seconds)
|
2022-01-24 19:07:55 +00:00
|
|
|
{
|
2021-09-10 20:15:35 +00:00
|
|
|
continue;
|
2022-01-24 19:07:55 +00:00
|
|
|
}
|
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
|
|
|
|
2022-03-18 16:34:29 +00:00
|
|
|
static void bandwidthGroupRead(tr_session* session, std::string_view config_dir)
|
2022-03-18 13:11:59 +00:00
|
|
|
{
|
2022-07-23 07:13:18 +00:00
|
|
|
auto const filename = tr_pathbuf{ config_dir, '/', BandwidthGroupsFilename };
|
2022-03-18 16:34:29 +00:00
|
|
|
auto groups_dict = tr_variant{};
|
2022-07-21 18:22:38 +00:00
|
|
|
if (!tr_sys_path_exists(filename) || !tr_variantFromFile(&groups_dict, TR_VARIANT_PARSE_JSON, filename, nullptr) ||
|
|
|
|
!tr_variantIsDict(&groups_dict))
|
2022-03-18 13:11:59 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-18 16:34:29 +00:00
|
|
|
auto idx = size_t{ 0 };
|
|
|
|
auto key = tr_quark{};
|
|
|
|
tr_variant* dict = nullptr;
|
2022-03-29 10:57:04 +00:00
|
|
|
while (tr_variantDictChild(&groups_dict, idx, &key, &dict))
|
2022-03-18 13:11:59 +00:00
|
|
|
{
|
2022-03-29 10:57:04 +00:00
|
|
|
++idx;
|
|
|
|
|
2022-03-18 16:34:29 +00:00
|
|
|
auto name = tr_interned_string(key);
|
2022-03-28 22:13:32 +00:00
|
|
|
auto& group = session->getBandwidthGroup(name);
|
2022-03-18 13:11:59 +00:00
|
|
|
|
2022-03-18 16:34:29 +00:00
|
|
|
auto limits = tr_bandwidth_limits{};
|
2022-03-18 13:11:59 +00:00
|
|
|
tr_variantDictFindBool(dict, TR_KEY_uploadLimited, &limits.up_limited);
|
2022-03-18 16:34:29 +00:00
|
|
|
tr_variantDictFindBool(dict, TR_KEY_downloadLimited, &limits.down_limited);
|
|
|
|
|
|
|
|
if (auto limit = int64_t{}; tr_variantDictFindInt(dict, TR_KEY_uploadLimit, &limit))
|
2022-03-18 13:11:59 +00:00
|
|
|
{
|
2022-03-18 16:34:29 +00:00
|
|
|
limits.up_limit_KBps = limit;
|
2022-03-18 13:11:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 16:34:29 +00:00
|
|
|
if (auto limit = int64_t{}; tr_variantDictFindInt(dict, TR_KEY_downloadLimit, &limit))
|
2022-03-18 13:11:59 +00:00
|
|
|
{
|
2022-03-18 16:34:29 +00:00
|
|
|
limits.down_limit_KBps = limit;
|
2022-03-18 13:11:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 16:34:29 +00:00
|
|
|
group.setLimits(&limits);
|
2022-03-18 13:11:59 +00:00
|
|
|
|
2022-03-18 16:34:29 +00:00
|
|
|
if (auto honors = bool{}; tr_variantDictFindBool(dict, TR_KEY_honorsSessionLimits, &honors))
|
2022-03-18 13:11:59 +00:00
|
|
|
{
|
2022-03-18 16:34:29 +00:00
|
|
|
group.honorParentLimits(TR_UP, honors);
|
|
|
|
group.honorParentLimits(TR_DOWN, honors);
|
2022-03-18 13:11:59 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&groups_dict);
|
2022-03-18 13:11:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 16:34:29 +00:00
|
|
|
static int bandwidthGroupWrite(tr_session const* session, std::string_view config_dir)
|
2022-03-18 13:11:59 +00:00
|
|
|
{
|
2022-03-18 16:34:29 +00:00
|
|
|
auto const& groups = session->bandwidth_groups_;
|
|
|
|
|
|
|
|
auto groups_dict = tr_variant{};
|
|
|
|
tr_variantInitDict(&groups_dict, std::size(groups));
|
|
|
|
|
|
|
|
for (auto const& [name, group] : groups)
|
2022-03-18 13:11:59 +00:00
|
|
|
{
|
2022-03-18 16:34:29 +00:00
|
|
|
auto const limits = group->getLimits();
|
2022-03-18 13:11:59 +00:00
|
|
|
|
2022-03-18 16:34:29 +00:00
|
|
|
auto* const dict = tr_variantDictAddDict(&groups_dict, name.quark(), 5);
|
|
|
|
tr_variantDictAddStrView(dict, TR_KEY_name, name.sv());
|
2022-03-18 13:11:59 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2022-07-23 07:13:18 +00:00
|
|
|
auto const filename = tr_pathbuf{ config_dir, '/', BandwidthGroupsFilename };
|
2022-03-28 22:13:32 +00:00
|
|
|
auto const ret = tr_variantToFile(&groups_dict, TR_VARIANT_FMT_JSON, filename);
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&groups_dict);
|
2022-03-18 13:11:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
///
|
|
|
|
|
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
|
|
|
{
|
|
|
|
|
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-08-11 19:30:00 +00:00
|
|
|
auto makeEventBase()
|
|
|
|
{
|
|
|
|
tr_evthread_init();
|
2022-08-31 00:30:47 +00:00
|
|
|
return std::unique_ptr<event_base, void (*)(event_base*)>{ event_base_new(), event_base_free };
|
2022-08-11 19:30:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 23:56:40 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
tr_session::tr_session(std::string_view config_dir)
|
2022-08-15 17:48:05 +00:00
|
|
|
: session_id_{ tr_time }
|
2022-08-11 19:30:00 +00:00
|
|
|
, event_base_{ makeEventBase() }
|
2022-08-15 17:48:05 +00:00
|
|
|
, evdns_base_{ evdns_base_new(eventBase(), EVDNS_BASE_INITIALIZE_NAMESERVERS),
|
|
|
|
[](evdns_base* dns)
|
|
|
|
{
|
|
|
|
// if zero, active requests will be aborted
|
|
|
|
evdns_base_free(dns, 0);
|
|
|
|
} }
|
2022-08-11 19:30:00 +00:00
|
|
|
, timer_maker_{ std::make_unique<libtransmission::EvTimerMaker>(eventBase()) }
|
2022-08-06 19:27:37 +00:00
|
|
|
, config_dir_{ config_dir }
|
2022-07-28 23:56:40 +00:00
|
|
|
, resume_dir_{ makeResumeDir(config_dir) }
|
|
|
|
, torrent_dir_{ makeTorrentDir(config_dir) }
|
|
|
|
, session_stats_{ config_dir, time(nullptr) }
|
|
|
|
{
|
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-07-23 07:13:18 +00:00
|
|
|
}
|