transmission/libtransmission/torrent-magnet.cc

393 lines
9.9 KiB
C++
Raw Normal View History

// This file Copyright © 2012-2023 Mnemosyne LLC.
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
2009-11-24 02:16:31 +00:00
#include <algorithm>
#include <climits> /* INT_MAX */
2023-07-08 15:24:03 +00:00
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <string>
#include <vector>
2022-03-15 14:52:16 +00:00
#include <fmt/core.h>
#include "libtransmission/transmission.h"
#include "libtransmission/crypto-utils.h" // for tr_sha1()
#include "libtransmission/error.h"
2023-07-08 15:24:03 +00:00
#include "libtransmission/file.h"
#include "libtransmission/quark.h"
#include "libtransmission/torrent-magnet.h"
#include "libtransmission/torrent-metainfo.h"
#include "libtransmission/torrent.h"
#include "libtransmission/tr-assert.h"
#include "libtransmission/utils.h"
#include "libtransmission/variant.h"
namespace
{
// don't ask for the same metadata piece more than this often
auto constexpr MinRepeatIntervalSecs = int{ 3 };
[[nodiscard]] auto create_all_needed(int n_pieces)
2009-11-24 02:16:31 +00:00
{
auto ret = std::deque<tr_incomplete_metadata::metadata_node>{};
ret.resize(n_pieces);
for (int i = 0; i < n_pieces; ++i)
{
ret[i].piece = i;
}
return ret;
2009-11-24 02:16:31 +00:00
}
[[nodiscard]] int div_ceil(int numerator, int denominator)
{
auto const [quot, rem] = std::div(numerator, denominator);
return quot + (rem == 0 ? 0 : 1);
}
} // namespace
2009-11-24 02:16:31 +00:00
bool tr_torrentSetMetadataSizeHint(tr_torrent* tor, int64_t size)
2009-11-24 02:16:31 +00:00
{
if (tor->has_metainfo())
{
return false;
}
if (tor->incomplete_metadata)
{
return false;
}
int const n = (size <= 0 || size > INT_MAX) ? -1 : div_ceil(size, MetadataPieceSize);
tr_logAddDebugTor(tor, fmt::format("metadata is {} bytes in {} pieces", size, n));
if (n <= 0)
{
return false;
}
auto m = std::make_unique<tr_incomplete_metadata>();
m->piece_count = n;
m->metadata.resize(size);
m->pieces_needed = create_all_needed(n);
if (std::empty(m->metadata) || std::empty(m->pieces_needed))
2009-11-24 02:16:31 +00:00
{
return false;
}
2009-11-24 02:16:31 +00:00
tor->incomplete_metadata = std::move(m);
return true;
2009-11-24 02:16:31 +00:00
}
bool tr_torrentGetMetadataPiece(tr_torrent const* tor, int piece, tr_metadata_piece& setme)
2009-11-24 02:16:31 +00:00
{
TR_ASSERT(tr_isTorrent(tor));
TR_ASSERT(piece >= 0);
2009-11-24 02:16:31 +00:00
if (!tor->has_metainfo())
{
return {};
}
auto const n_pieces = std::max(1, div_ceil(tor->info_dict_size(), MetadataPieceSize));
2022-08-27 22:06:20 +00:00
if (piece < 0 || piece >= n_pieces)
{
return {};
}
auto in = std::ifstream{ tor->torrent_file(), std::ios_base::in };
if (!in.is_open())
2009-11-24 02:16:31 +00:00
{
return {};
}
auto const info_dict_size = tor->info_dict_size();
TR_ASSERT(info_dict_size > 0);
auto const offset_in_info_dict = static_cast<uint64_t>(piece) * MetadataPieceSize;
if (auto const offset_in_file = tor->info_dict_offset() + offset_in_info_dict; !in.seekg(offset_in_file))
{
return {};
}
auto const piece_len = offset_in_info_dict + MetadataPieceSize <= info_dict_size ? MetadataPieceSize :
info_dict_size - offset_in_info_dict;
setme.resize(piece_len);
return !!in.read(reinterpret_cast<char*>(std::data(setme)), std::size(setme));
2009-11-24 02:16:31 +00:00
}
bool tr_torrentUseMetainfoFromFile(
tr_torrent* tor,
tr_torrent_metainfo const* metainfo,
char const* filename_in,
tr_error** error)
{
// add .torrent file
if (!tr_sys_path_copy(filename_in, tor->torrent_file().c_str(), error))
{
return false;
}
// remove .magnet file
tr_sys_path_remove(tor->magnet_file());
// tor should keep this metainfo
tor->set_metainfo(*metainfo);
tor->incomplete_metadata.reset();
return true;
}
// ---
namespace
{
namespace set_metadata_piece_helpers
{
[[nodiscard]] constexpr size_t get_piece_length(tr_incomplete_metadata const& m, int piece)
{
return piece + 1 == m.piece_count ? // last piece
std::size(m.metadata) - (piece * MetadataPieceSize) :
MetadataPieceSize;
}
tr_variant build_metainfo_except_info_dict(tr_torrent_metainfo const& tm)
{
auto top = tr_variant::Map{ 8U };
2022-01-11 14:28:14 +00:00
if (auto const& val = tm.comment(); !std::empty(val))
{
top.try_emplace(TR_KEY_comment, val);
}
2022-01-11 14:28:14 +00:00
if (auto const& val = tm.source(); !std::empty(val))
{
top.try_emplace(TR_KEY_source, val);
}
2022-01-11 14:28:14 +00:00
if (auto const& val = tm.creator(); !std::empty(val))
{
top.try_emplace(TR_KEY_created_by, val);
2022-01-11 14:28:14 +00:00
}
if (auto const val = tm.date_created(); val != 0)
2022-01-11 14:28:14 +00:00
{
top.try_emplace(TR_KEY_creation_date, val);
2022-01-11 14:28:14 +00:00
}
if (auto const& announce_list = tm.announce_list(); !std::empty(announce_list))
2022-01-11 14:28:14 +00:00
{
announce_list.add_to_map(top);
2022-01-11 14:28:14 +00:00
}
if (auto const n_webseeds = tm.webseed_count(); n_webseeds > 0U)
2022-01-11 14:28:14 +00:00
{
auto webseed_vec = tr_variant::Vector{};
webseed_vec.reserve(n_webseeds);
for (size_t i = 0U; i < n_webseeds; ++i)
{
webseed_vec.emplace_back(tm.webseed(i));
2022-01-11 14:28:14 +00:00
}
}
return tr_variant{ std::move(top) };
2022-01-11 14:28:14 +00:00
}
bool use_new_metainfo(tr_torrent* tor, tr_error** error)
2022-01-11 14:28:14 +00:00
{
auto const& m = tor->incomplete_metadata;
TR_ASSERT(m);
2022-01-17 18:39:50 +00:00
// test the info_dict checksum
if (tr_sha1::digest(m->metadata) != tor->info_hash())
2022-01-11 14:28:14 +00:00
{
return false;
}
// checksum passed; now try to parse it as benc
2023-08-17 16:02:45 +00:00
auto serde = tr_variant_serde::benc().inplace();
auto info_dict_v = serde.parse(m->metadata);
if (!info_dict_v)
2022-01-11 14:28:14 +00:00
{
2023-08-17 16:02:45 +00:00
tr_error_propagate(error, &serde.error_);
2022-01-11 14:28:14 +00:00
return false;
}
// yay we have an info dict. Let's make a torrent file
auto top_var = build_metainfo_except_info_dict(tor->metainfo_);
tr_variantMergeDicts(tr_variantDictAddDict(&top_var, TR_KEY_info, 0), &*info_dict_v);
auto const benc = serde.to_string(top_var);
// does this synthetic torrent file parse?
auto metainfo = tr_torrent_metainfo{};
if (!metainfo.parse_benc(benc))
2022-01-11 14:28:14 +00:00
{
return false;
}
2022-01-11 14:28:14 +00:00
// save it
if (!tr_file_save(tor->torrent_file(), benc, error))
2022-01-11 14:28:14 +00:00
{
return false;
}
// remove .magnet file
tr_sys_path_remove(tor->magnet_file());
2022-01-11 14:28:14 +00:00
// tor should keep this metainfo
tor->set_metainfo(metainfo);
2022-01-11 14:28:14 +00:00
return true;
}
void on_have_all_metainfo(tr_torrent* tor)
{
2022-01-11 14:28:14 +00:00
tr_error* error = nullptr;
auto& m = tor->incomplete_metadata;
TR_ASSERT(m);
if (use_new_metainfo(tor, &error))
{
m.reset();
}
else /* drat. */
{
auto const n = m->piece_count;
m->pieces_needed = create_all_needed(n);
2022-01-11 14:28:14 +00:00
char const* const msg = error != nullptr && error->message != nullptr ? error->message : "unknown error";
tr_logAddWarnTor(
tor,
fmt::format(
tr_ngettext(
"Couldn't parse magnet metainfo: '{error}'. Redownloading {piece_count} piece",
"Couldn't parse magnet metainfo: '{error}'. Redownloading {piece_count} pieces",
n),
fmt::arg("error", msg),
fmt::arg("piece_count", n)));
2022-01-11 14:28:14 +00:00
tr_error_clear(&error);
}
}
} // namespace set_metadata_piece_helpers
} // namespace
void tr_torrentMagnetDoIdleWork(tr_torrent* const tor)
{
using namespace set_metadata_piece_helpers;
TR_ASSERT(tr_isTorrent(tor));
if (auto const& m = tor->incomplete_metadata; m && std::empty(m->pieces_needed))
{
tr_logAddDebugTor(tor, fmt::format("we now have all the metainfo!"));
on_have_all_metainfo(tor);
}
}
void tr_torrentSetMetadataPiece(tr_torrent* tor, int piece, void const* data, size_t len)
2009-11-24 02:16:31 +00:00
{
using namespace set_metadata_piece_helpers;
TR_ASSERT(tr_isTorrent(tor));
TR_ASSERT(data != nullptr);
tr_logAddDebugTor(tor, fmt::format("got metadata piece {} of {} bytes", piece, len));
2009-11-24 02:16:31 +00:00
// are we set up to download metadata?
auto& m = tor->incomplete_metadata;
if (!m)
{
return;
}
2009-11-24 02:16:31 +00:00
// sanity test: is `piece` in range?
if ((piece < 0) || (piece >= m->piece_count))
{
return;
}
// sanity test: is `len` the right size?
if (get_piece_length(*m, piece) != len)
{
return;
}
2009-11-24 02:16:31 +00:00
// do we need this piece?
auto& needed = m->pieces_needed;
auto const iter = std::find_if(
std::begin(needed),
std::end(needed),
[piece](auto const& item) { return item.piece == piece; });
if (iter == std::end(needed))
{
return;
}
2009-11-24 02:16:31 +00:00
size_t const offset = piece * MetadataPieceSize;
std::copy_n(reinterpret_cast<char const*>(data), len, std::begin(m->metadata) + offset);
2009-11-24 02:16:31 +00:00
needed.erase(iter);
tr_logAddDebugTor(tor, fmt::format("saving metainfo piece {}... {} remain", piece, std::size(needed)));
2009-11-24 02:16:31 +00:00
}
// ---
std::optional<int> tr_torrentGetNextMetadataRequest(tr_torrent* tor, time_t now)
2009-11-24 02:16:31 +00:00
{
TR_ASSERT(tr_isTorrent(tor));
2009-11-24 02:16:31 +00:00
auto& m = tor->incomplete_metadata;
if (!m)
{
return {};
}
2009-11-24 02:16:31 +00:00
auto& needed = m->pieces_needed;
if (std::empty(needed) || needed.front().requested_at + MinRepeatIntervalSecs >= now)
2009-11-24 02:16:31 +00:00
{
return {};
2009-11-24 02:16:31 +00:00
}
auto req = needed.front();
needed.pop_front();
req.requested_at = now;
needed.push_back(req);
tr_logAddDebugTor(tor, fmt::format("next piece to request: {}", req.piece));
return req.piece;
2009-11-24 02:16:31 +00:00
}
2009-11-24 17:10:40 +00:00
double tr_torrentGetMetadataPercent(tr_torrent const* tor)
2009-11-24 17:10:40 +00:00
{
if (tor->has_metainfo())
{
return 1.0;
}
if (auto const& m = tor->incomplete_metadata; m)
{
if (auto const& n = m->piece_count; n != 0)
{
return (n - std::size(m->pieces_needed)) / static_cast<double>(n);
}
}
return 0.0;
2009-11-24 17:10:40 +00:00
}
std::string tr_torrentGetMagnetLink(tr_torrent const* tor)
{
return tor->metainfo_.magnet();
}
size_t tr_torrentGetMagnetLinkToBuf(tr_torrent const* tor, char* buf, size_t buflen)
{
return tr_strv_to_buf(tr_torrentGetMagnetLink(tor), buf, buflen);
}