2009-11-24 02:16:31 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2012-2014 Mnemosyne LLC
|
2009-11-24 02:16:31 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2009-11-24 02:16:31 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-01-08 23:41:05 +00:00
|
|
|
#include <algorithm>
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <climits> /* INT_MAX */
|
|
|
|
#include <cstring> /* memcpy(), memset(), memcmp() */
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <ctime>
|
|
|
|
#include <string_view>
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/buffer.h>
|
|
|
|
|
2009-11-24 02:16:31 +00:00
|
|
|
#include "transmission.h"
|
2021-11-09 03:30:03 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "crypto-utils.h" /* tr_sha1() */
|
2022-01-11 14:28:14 +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"
|
2021-11-10 02:42:18 +00:00
|
|
|
#include "magnet-metainfo.h"
|
2009-11-24 02:16:31 +00:00
|
|
|
#include "metainfo.h"
|
2010-02-06 05:22:27 +00:00
|
|
|
#include "resume.h"
|
2009-11-24 02:16:31 +00:00
|
|
|
#include "torrent-magnet.h"
|
2022-01-11 14:28:14 +00:00
|
|
|
#include "torrent-metainfo.h"
|
2021-11-09 03:30:03 +00:00
|
|
|
#include "torrent.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2009-11-24 02:16:31 +00:00
|
|
|
#include "utils.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant.h"
|
2021-11-09 03:30:03 +00:00
|
|
|
#include "web-utils.h"
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2021-12-27 22:47:25 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2017-05-22 20:12:57 +00:00
|
|
|
#define dbgmsg(tor, ...) tr_logAddDeepNamed(tr_torrentName(tor), __VA_ARGS__)
|
2009-11-24 02:16:31 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-21 00:39:05 +00:00
|
|
|
/* don't ask for the same metadata piece more than this often */
|
|
|
|
static auto constexpr MinRepeatIntervalSecs = int{ 3 };
|
2009-11-24 02:16:31 +00:00
|
|
|
|
|
|
|
struct metadata_node
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
time_t requestedAt;
|
|
|
|
int piece;
|
2009-11-24 02:16:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct tr_incomplete_metadata
|
|
|
|
{
|
2021-11-15 07:21:57 +00:00
|
|
|
char* metadata;
|
|
|
|
size_t metadata_size;
|
2017-04-19 12:04:45 +00:00
|
|
|
int pieceCount;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/** sorted from least to most recently requested */
|
|
|
|
struct metadata_node* piecesNeeded;
|
|
|
|
int piecesNeededCount;
|
2009-11-24 02:16:31 +00:00
|
|
|
};
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void incompleteMetadataFree(struct tr_incomplete_metadata* m)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(m->metadata);
|
|
|
|
tr_free(m->piecesNeeded);
|
|
|
|
tr_free(m);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_torrentSetMetadataSizeHint(tr_torrent* tor, int64_t size)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2021-12-17 04:51:53 +00:00
|
|
|
if (tor->hasMetadata())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tor->incompleteMetadata != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int const n = (size <= 0 || size > INT_MAX) ? -1 : size / METADATA_PIECE_SIZE + (size % METADATA_PIECE_SIZE != 0 ? 1 : 0);
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(tor, "metadata is %" PRId64 " bytes in %d pieces", size, n);
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (n <= 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_incomplete_metadata* m = tr_new(struct tr_incomplete_metadata, 1);
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (m == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
m->pieceCount = n;
|
2021-11-15 07:21:57 +00:00
|
|
|
m->metadata = tr_new(char, size);
|
2017-04-19 12:04:45 +00:00
|
|
|
m->metadata_size = size;
|
|
|
|
m->piecesNeededCount = n;
|
|
|
|
m->piecesNeeded = tr_new(struct metadata_node, n);
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (m->metadata == nullptr || m->piecesNeeded == nullptr)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
incompleteMetadataFree(m);
|
|
|
|
return false;
|
2016-01-07 17:12:14 +00:00
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2016-01-07 17:12:14 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
m->piecesNeeded[i].piece = i;
|
|
|
|
m->piecesNeeded[i].requestedAt = 0;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tor->incompleteMetadata = m;
|
|
|
|
return true;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static size_t findInfoDictOffset(tr_torrent const* tor)
|
2010-06-16 03:05:23 +00:00
|
|
|
{
|
2021-12-27 22:47:25 +00:00
|
|
|
// load the torrent's .torrent file
|
|
|
|
auto benc = std::vector<char>{};
|
|
|
|
if (!tr_loadFile(benc, tor->torrentFile()) || std::empty(benc))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2021-12-27 22:47:25 +00:00
|
|
|
// parse the benc
|
|
|
|
auto top = tr_variant{};
|
|
|
|
auto const benc_sv = std::string_view{ std::data(benc), std::size(benc) };
|
|
|
|
if (!tr_variantFromBuf(&top, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, benc_sv))
|
2010-06-16 03:05:23 +00:00
|
|
|
{
|
2021-12-27 22:47:25 +00:00
|
|
|
return {};
|
|
|
|
}
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2021-12-27 22:47:25 +00:00
|
|
|
auto offset = size_t{};
|
|
|
|
tr_variant* info_dict = nullptr;
|
|
|
|
if (tr_variantDictFindDict(&top, TR_KEY_info, &info_dict))
|
|
|
|
{
|
|
|
|
auto const info_dict_benc = tr_variantToStr(info_dict, TR_VARIANT_FMT_BENC);
|
|
|
|
auto const it = std::search(std::begin(benc), std::end(benc), std::begin(info_dict_benc), std::end(info_dict_benc));
|
|
|
|
if (it != std::end(benc))
|
|
|
|
{
|
|
|
|
offset = std::distance(std::begin(benc), it);
|
2010-06-16 03:05:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-27 22:47:25 +00:00
|
|
|
tr_variantFree(&top);
|
2017-04-19 12:04:45 +00:00
|
|
|
return offset;
|
2010-06-16 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void ensureInfoDictOffsetIsCached(tr_torrent* tor)
|
2010-06-16 03:05:23 +00:00
|
|
|
{
|
2021-12-17 04:51:53 +00:00
|
|
|
TR_ASSERT(tor->hasMetadata());
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2022-01-08 20:05:38 +00:00
|
|
|
if (!tor->info_dict_offset_is_cached)
|
2010-06-16 03:05:23 +00:00
|
|
|
{
|
2022-01-08 20:05:38 +00:00
|
|
|
tor->info_dict_offset = findInfoDictOffset(tor);
|
|
|
|
tor->info_dict_offset_is_cached = true;
|
2010-06-16 03:05:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void* tr_torrentGetMetadataPiece(tr_torrent* tor, int piece, size_t* len)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isTorrent(tor));
|
|
|
|
TR_ASSERT(piece >= 0);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(len != nullptr);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2022-01-08 18:53:35 +00:00
|
|
|
if (!tor->hasMetadata())
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2022-01-08 23:41:05 +00:00
|
|
|
auto const fd = tr_sys_file_open(tor->torrentFile().c_str(), TR_SYS_FILE_READ, 0, nullptr);
|
2022-01-08 18:53:35 +00:00
|
|
|
if (fd == TR_BAD_SYS_FILE)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2022-01-08 18:53:35 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ensureInfoDictOffsetIsCached(tor);
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2022-01-08 20:05:38 +00:00
|
|
|
auto const info_dict_size = tor->infoDictSize();
|
|
|
|
TR_ASSERT(info_dict_size > 0);
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2022-01-08 18:53:35 +00:00
|
|
|
char* ret = nullptr;
|
2022-01-08 20:05:38 +00:00
|
|
|
if (size_t o = piece * METADATA_PIECE_SIZE; tr_sys_file_seek(fd, tor->infoDictOffset() + o, TR_SEEK_SET, nullptr, nullptr))
|
2022-01-08 18:53:35 +00:00
|
|
|
{
|
2022-01-08 20:05:38 +00:00
|
|
|
size_t const l = o + METADATA_PIECE_SIZE <= info_dict_size ? METADATA_PIECE_SIZE : info_dict_size - o;
|
2022-01-08 18:53:35 +00:00
|
|
|
|
|
|
|
if (0 < l && l <= METADATA_PIECE_SIZE)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2022-01-08 18:53:35 +00:00
|
|
|
char* buf = tr_new(char, l);
|
|
|
|
auto n = uint64_t{};
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2022-01-08 18:53:35 +00:00
|
|
|
if (tr_sys_file_read(fd, buf, l, &n, nullptr) && n == l)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2022-01-08 18:53:35 +00:00
|
|
|
*len = l;
|
|
|
|
ret = buf;
|
|
|
|
buf = nullptr;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 18:53:35 +00:00
|
|
|
tr_free(buf);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 18:53:35 +00:00
|
|
|
tr_sys_file_close(fd, nullptr);
|
2015-05-09 08:37:55 +00:00
|
|
|
|
2022-01-08 18:53:35 +00:00
|
|
|
TR_ASSERT(ret == nullptr || *len > 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 00:59:19 +00:00
|
|
|
static int getPieceNeededIndex(struct tr_incomplete_metadata const* m, int piece)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m->piecesNeededCount; ++i)
|
|
|
|
{
|
|
|
|
if (m->piecesNeeded[i].piece == piece)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getPieceLength(struct tr_incomplete_metadata const* m, int piece)
|
|
|
|
{
|
|
|
|
return piece + 1 == m->pieceCount ? // last piece
|
|
|
|
m->metadata_size - (piece * METADATA_PIECE_SIZE) :
|
|
|
|
METADATA_PIECE_SIZE;
|
|
|
|
}
|
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
static void tr_buildMetainfoExceptInfoDict(tr_info const& tm, tr_variant* top)
|
2022-01-09 19:17:53 +00:00
|
|
|
{
|
2022-01-11 14:28:14 +00:00
|
|
|
tr_variantInitDict(top, 6);
|
|
|
|
|
|
|
|
if (auto const& val = tm.comment(); !std::empty(val))
|
2022-01-09 19:17:53 +00:00
|
|
|
{
|
2022-01-11 14:28:14 +00:00
|
|
|
tr_variantDictAddStr(top, TR_KEY_comment, val);
|
2022-01-09 19:17:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
if (auto const& val = tm.source(); !std::empty(val))
|
2022-01-09 19:17:53 +00:00
|
|
|
{
|
2022-01-11 14:28:14 +00:00
|
|
|
tr_variantDictAddStr(top, TR_KEY_source, val);
|
2022-01-09 19:17:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
if (auto const& val = tm.creator(); !std::empty(val))
|
2022-01-09 19:17:53 +00:00
|
|
|
{
|
2022-01-11 14:28:14 +00:00
|
|
|
tr_variantDictAddStr(top, TR_KEY_created_by, val);
|
|
|
|
}
|
2022-01-09 19:17:53 +00:00
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
if (auto const val = tm.dateCreated(); val != 0)
|
|
|
|
{
|
|
|
|
tr_variantDictAddInt(top, TR_KEY_creation_date, val);
|
|
|
|
}
|
2022-01-09 19:17:53 +00:00
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
if (auto const& announce_list = tm.announceList(); !std::empty(announce_list))
|
|
|
|
{
|
|
|
|
auto const n = std::size(announce_list);
|
|
|
|
if (n == 1)
|
2022-01-09 19:17:53 +00:00
|
|
|
{
|
2022-01-11 14:28:14 +00:00
|
|
|
tr_variantDictAddStr(top, TR_KEY_announce, announce_list.at(0).announce_str.sv());
|
2022-01-09 19:17:53 +00:00
|
|
|
}
|
2022-01-11 14:28:14 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
auto* const announce_list_variant = tr_variantDictAddList(top, TR_KEY_announce_list, n);
|
|
|
|
tr_variant* tier_variant = nullptr;
|
|
|
|
auto current_tier = std::optional<tr_tracker_tier_t>{};
|
|
|
|
for (auto const& tracker : announce_list)
|
|
|
|
{
|
|
|
|
if (!current_tier || *current_tier != tracker.tier)
|
|
|
|
{
|
|
|
|
tier_variant = tr_variantListAddList(announce_list_variant, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variantListAddStr(tier_variant, tracker.announce_str.sv());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-09 19:17:53 +00:00
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
if (auto const n_webseeds = tm.webseedCount(); n_webseeds > 0)
|
|
|
|
{
|
|
|
|
auto* const webseeds_variant = tr_variantDictAddList(top, TR_KEY_url_list, n_webseeds);
|
|
|
|
for (size_t i = 0; i < n_webseeds; ++i)
|
2022-01-09 19:17:53 +00:00
|
|
|
{
|
2022-01-11 14:28:14 +00:00
|
|
|
tr_variantListAddStr(webseeds_variant, tm.webseed(i));
|
|
|
|
}
|
|
|
|
}
|
2022-01-09 19:17:53 +00:00
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
if (tm.fileCount() == 0)
|
|
|
|
{
|
|
|
|
// local transmission extensions.
|
|
|
|
// these temporary placeholders are used for magnets until we have the info dict.
|
|
|
|
auto* const magnet_info = tr_variantDictAddDict(top, TR_KEY_magnet_info, 2);
|
|
|
|
tr_variantDictAddStr(
|
|
|
|
magnet_info,
|
|
|
|
TR_KEY_info_hash,
|
|
|
|
std::string_view{ reinterpret_cast<char const*>(std::data(tm.infoHash())), std::size(tm.infoHash()) });
|
|
|
|
if (auto const& val = tm.name(); !std::empty(val))
|
|
|
|
{
|
|
|
|
tr_variantDictAddStr(magnet_info, TR_KEY_display_name, val);
|
2022-01-09 19:17:53 +00:00
|
|
|
}
|
2022-01-11 14:28:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool useNewMetainfo(tr_torrent* tor, tr_incomplete_metadata* m, tr_error** error)
|
|
|
|
{
|
|
|
|
auto const sha1 = tr_sha1(std::string_view{ m->metadata, m->metadata_size });
|
|
|
|
bool const checksum_passed = sha1 && *sha1 == tor->infoHash();
|
|
|
|
if (!checksum_passed)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// checksum passed; now try to parse it as benc
|
|
|
|
auto info_dict_v = tr_variant{};
|
|
|
|
auto const info_dict_sv = std::string_view{ m->metadata, m->metadata_size };
|
|
|
|
if (!tr_variantFromBuf(&info_dict_v, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, info_dict_sv, nullptr, error))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-09 19:17:53 +00:00
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
// yay we have an info dict. Let's make a .torrent file
|
|
|
|
auto top_v = tr_variant{};
|
|
|
|
tr_buildMetainfoExceptInfoDict(tor->info, &top_v);
|
|
|
|
tr_variantMergeDicts(tr_variantDictAddDict(&top_v, TR_KEY_info, 0), &info_dict_v);
|
|
|
|
auto const benc = tr_variantToStr(&top_v, TR_VARIANT_FMT_BENC);
|
|
|
|
|
|
|
|
// does this synthetic .torrent file parse?
|
|
|
|
auto parsed = tr_metainfoParse(tor->session, &top_v, error);
|
|
|
|
tr_variantFree(&top_v);
|
|
|
|
tr_variantFree(&info_dict_v);
|
|
|
|
if (!parsed)
|
|
|
|
{
|
|
|
|
return false;
|
2022-01-09 19:17:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
// save it
|
|
|
|
auto const& torrent_dir = tor->session->torrent_dir;
|
|
|
|
auto const filename = tr_magnet_metainfo::makeFilename(
|
|
|
|
torrent_dir,
|
|
|
|
tor->name(),
|
|
|
|
tor->infoHashString(),
|
|
|
|
tr_magnet_metainfo::BasenameFormat::Hash,
|
|
|
|
".torrent"sv);
|
|
|
|
if (!tr_saveFile(filename, benc, error))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-09 19:17:53 +00:00
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
// tor should keep this metainfo
|
|
|
|
tor->swapMetainfo(*parsed);
|
|
|
|
tr_torrentGotNewInfoDict(tor);
|
|
|
|
tor->setDirty();
|
|
|
|
|
|
|
|
return true;
|
2022-01-09 19:17:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void onHaveAllMetainfo(tr_torrent* tor, tr_incomplete_metadata* m)
|
|
|
|
{
|
2022-01-11 14:28:14 +00:00
|
|
|
tr_error* error = nullptr;
|
|
|
|
if (useNewMetainfo(tor, m, &error))
|
2022-01-09 19:17:53 +00:00
|
|
|
{
|
|
|
|
incompleteMetadataFree(tor->incompleteMetadata);
|
|
|
|
tor->incompleteMetadata = nullptr;
|
|
|
|
tor->isStopping = true;
|
|
|
|
tor->magnetVerify = true;
|
|
|
|
tor->startAfterVerify = !tor->prefetchMagnetMetadata;
|
|
|
|
tor->markEdited();
|
|
|
|
}
|
|
|
|
else /* drat. */
|
|
|
|
{
|
|
|
|
int const n = m->pieceCount;
|
|
|
|
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
m->piecesNeeded[i].piece = i;
|
|
|
|
m->piecesNeeded[i].requestedAt = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
m->piecesNeededCount = n;
|
2022-01-11 14:28:14 +00:00
|
|
|
char const* const msg = error != nullptr && error->message != nullptr ? error->message : "unknown error";
|
|
|
|
dbgmsg(tor, "metadata error: %s. (trying again; %d pieces left)", msg, n);
|
|
|
|
tr_error_clear(&error);
|
2022-01-09 19:17:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_torrentSetMetadataPiece(tr_torrent* tor, int piece, void const* data, int len)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isTorrent(tor));
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(data != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(len >= 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
dbgmsg(tor, "got metadata piece %d of %d bytes", piece, len);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2020-11-04 00:59:19 +00:00
|
|
|
// are we set up to download metadata?
|
2022-01-09 19:17:53 +00:00
|
|
|
tr_incomplete_metadata* const m = tor->incompleteMetadata;
|
2021-09-15 00:18:09 +00:00
|
|
|
if (m == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2020-11-04 00:59:19 +00:00
|
|
|
// sanity test: is `piece` in range?
|
|
|
|
if ((piece < 0) || (piece >= m->pieceCount))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-04 00:59:19 +00:00
|
|
|
// sanity test: is `len` the right size?
|
|
|
|
if (getPieceLength(m, piece) != len)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2020-11-04 00:59:19 +00:00
|
|
|
// do we need this piece?
|
|
|
|
int const idx = getPieceNeededIndex(m, piece);
|
|
|
|
if (idx == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2020-11-04 00:59:19 +00:00
|
|
|
size_t const offset = piece * METADATA_PIECE_SIZE;
|
2022-01-08 23:41:05 +00:00
|
|
|
std::copy_n(reinterpret_cast<char const*>(data), len, m->metadata + offset);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2020-11-04 00:59:19 +00:00
|
|
|
tr_removeElementFromArray(m->piecesNeeded, idx, sizeof(struct metadata_node), m->piecesNeededCount);
|
2019-03-17 05:00:15 +00:00
|
|
|
--m->piecesNeededCount;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(tor, "saving metainfo piece %d... %d remain", piece, m->piecesNeededCount);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* are we done? */
|
|
|
|
if (m->piecesNeededCount == 0)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(tor, "metainfo piece %d was the last one", piece);
|
2022-01-09 19:17:53 +00:00
|
|
|
onHaveAllMetainfo(tor, m);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_torrentGetNextMetadataRequest(tr_torrent* tor, time_t now, int* setme_piece)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isTorrent(tor));
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool have_request = false;
|
|
|
|
struct tr_incomplete_metadata* m = tor->incompleteMetadata;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2021-10-21 00:39:05 +00:00
|
|
|
if (m != nullptr && m->piecesNeededCount > 0 && m->piecesNeeded[0].requestedAt + MinRepeatIntervalSecs < now)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const piece = m->piecesNeeded[0].piece;
|
2019-03-17 05:00:15 +00:00
|
|
|
tr_removeElementFromArray(m->piecesNeeded, 0, sizeof(struct metadata_node), m->piecesNeededCount);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2019-03-17 05:00:15 +00:00
|
|
|
int i = m->piecesNeededCount - 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
m->piecesNeeded[i].piece = piece;
|
|
|
|
m->piecesNeeded[i].requestedAt = now;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(tor, "next piece to request: %d", piece);
|
|
|
|
*setme_piece = piece;
|
|
|
|
have_request = true;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return have_request;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
2009-11-24 17:10:40 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
double tr_torrentGetMetadataPercent(tr_torrent const* tor)
|
2009-11-24 17:10:40 +00:00
|
|
|
{
|
2021-12-17 04:51:53 +00:00
|
|
|
if (tor->hasMetadata())
|
2013-01-07 18:16:34 +00:00
|
|
|
{
|
2021-10-21 17:40:36 +00:00
|
|
|
return 1.0;
|
2013-01-07 18:16:34 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 17:40:36 +00:00
|
|
|
auto const* const m = tor->incompleteMetadata;
|
|
|
|
return m == nullptr || m->pieceCount == 0 ? 0.0 : (m->pieceCount - m->piecesNeededCount) / (double)m->pieceCount;
|
2009-11-24 17:10:40 +00:00
|
|
|
}
|
2009-11-29 08:27:55 +00:00
|
|
|
|
2020-09-11 21:07:45 +00:00
|
|
|
/* TODO: this should be renamed tr_metainfoGetMagnetLink() and moved to metainfo.c for consistency */
|
2017-04-20 16:02:19 +00:00
|
|
|
char* tr_torrentInfoGetMagnetLink(tr_info const* inf)
|
2009-11-29 08:27:55 +00:00
|
|
|
{
|
2021-12-27 22:47:25 +00:00
|
|
|
auto buf = std::string{};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-27 22:47:25 +00:00
|
|
|
buf += "magnet:?xt=urn:btih:"sv;
|
2022-01-08 23:41:05 +00:00
|
|
|
buf += inf->infoHashString();
|
2009-11-29 08:27:55 +00:00
|
|
|
|
2022-01-08 23:41:05 +00:00
|
|
|
auto const& name = inf->name();
|
|
|
|
if (!std::empty(name))
|
2010-05-11 15:30:30 +00:00
|
|
|
{
|
2021-12-27 22:47:25 +00:00
|
|
|
buf += "&dn="sv;
|
|
|
|
tr_http_escape(buf, name, true);
|
2010-05-11 15:30:30 +00:00
|
|
|
}
|
2012-07-23 15:28:27 +00:00
|
|
|
|
2021-12-14 20:59:40 +00:00
|
|
|
for (size_t i = 0, n = std::size(*inf->announce_list); i < n; ++i)
|
2009-11-29 08:27:55 +00:00
|
|
|
{
|
2021-12-27 22:47:25 +00:00
|
|
|
buf += "&tr="sv;
|
|
|
|
tr_http_escape(buf, inf->announce_list->at(i).announce.full, true);
|
2009-11-29 08:27:55 +00:00
|
|
|
}
|
|
|
|
|
2022-01-09 16:55:09 +00:00
|
|
|
for (size_t i = 0, n = inf->webseedCount(); i < n; ++i)
|
2013-02-04 18:54:38 +00:00
|
|
|
{
|
2021-12-27 22:47:25 +00:00
|
|
|
buf += "&ws="sv;
|
2022-01-09 16:55:09 +00:00
|
|
|
tr_http_escape(buf, inf->webseed(i), true);
|
2013-02-04 18:54:38 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 22:47:25 +00:00
|
|
|
return tr_strvDup(buf);
|
2009-11-29 08:27:55 +00:00
|
|
|
}
|
2021-12-14 21:59:44 +00:00
|
|
|
|
|
|
|
char* tr_torrentGetMagnetLink(tr_torrent const* tor)
|
|
|
|
{
|
2021-12-16 08:49:04 +00:00
|
|
|
return tr_torrentInfoGetMagnetLink(&tor->info);
|
2021-12-14 21:59:44 +00:00
|
|
|
}
|