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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-08-27 23:41:26 +00:00
|
|
|
#include <limits.h> /* INT_MAX */
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <string.h> /* memcpy(), memset(), memcmp() */
|
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"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "crypto-utils.h" /* tr_sha1() */
|
2014-07-08 00:08:43 +00:00
|
|
|
#include "file.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2009-11-24 02:16:31 +00:00
|
|
|
#include "magnet.h"
|
|
|
|
#include "metainfo.h"
|
2010-02-06 05:22:27 +00:00
|
|
|
#include "resume.h"
|
2009-11-24 02:16:31 +00:00
|
|
|
#include "torrent.h"
|
|
|
|
#include "torrent-magnet.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"
|
2009-11-24 02:16:31 +00:00
|
|
|
#include "web.h"
|
|
|
|
|
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
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* don't ask for the same metadata piece more than this often */
|
|
|
|
MIN_REPEAT_INTERVAL_SECS = 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
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
uint8_t* metadata;
|
|
|
|
int metadata_size;
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_torrentHasMetadata(tor))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tor->incompleteMetadata != NULL)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (m == NULL)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
m->pieceCount = n;
|
|
|
|
m->metadata = tr_new(uint8_t, size);
|
|
|
|
m->metadata_size = size;
|
|
|
|
m->piecesNeededCount = n;
|
|
|
|
m->piecesNeeded = tr_new(struct metadata_node, n);
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (m->metadata == NULL || m->piecesNeeded == NULL)
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t fileLen;
|
|
|
|
uint8_t* fileContents;
|
|
|
|
size_t offset = 0;
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* load the file, and find the info dict's offset inside the file */
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((fileContents = tr_loadFile(tor->info.torrent, &fileLen, NULL)) != NULL)
|
2010-06-16 03:05:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant top;
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_variantFromBenc(&top, fileContents, fileLen) == 0)
|
2010-06-16 03:05:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* infoDict;
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindDict(&top, TR_KEY_info, &infoDict))
|
2010-06-16 03:05:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t infoLen;
|
|
|
|
char* infoContents = tr_variantToStr(infoDict, TR_VARIANT_FMT_BENC, &infoLen);
|
2017-04-20 16:02:19 +00:00
|
|
|
uint8_t const* i = (uint8_t const*)tr_memmem((char*)fileContents, fileLen, infoContents, infoLen);
|
2017-04-19 12:04:45 +00:00
|
|
|
offset = i != NULL ? i - fileContents : 0;
|
|
|
|
tr_free(infoContents);
|
2010-06-16 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(&top);
|
2010-06-16 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(fileContents);
|
2010-06-16 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_torrentHasMetadata(tor));
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tor->infoDictOffsetIsCached)
|
2010-06-16 03:05:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tor->infoDictOffset = findInfoDictOffset(tor);
|
|
|
|
tor->infoDictOffsetIsCached = 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);
|
|
|
|
TR_ASSERT(len != NULL);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
char* ret = NULL;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_torrentHasMetadata(tor))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_file_t fd;
|
|
|
|
|
|
|
|
ensureInfoDictOffsetIsCached(tor);
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tor->infoDictLength > 0);
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
fd = tr_sys_file_open(tor->info.torrent, TR_SYS_FILE_READ, 0, NULL);
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (fd != TR_BAD_SYS_FILE)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const o = piece * METADATA_PIECE_SIZE;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_sys_file_seek(fd, tor->infoDictOffset + o, TR_SEEK_SET, NULL, NULL))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const l = o + METADATA_PIECE_SIZE <= tor->infoDictLength ? METADATA_PIECE_SIZE : tor->infoDictLength - o;
|
2009-12-15 16:34:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (0 < l && l <= METADATA_PIECE_SIZE)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* buf = tr_new(char, l);
|
|
|
|
uint64_t n;
|
|
|
|
|
|
|
|
if (tr_sys_file_read(fd, buf, l, &n, NULL) && n == l)
|
2009-12-15 16:34:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
*len = l;
|
|
|
|
ret = buf;
|
|
|
|
buf = NULL;
|
2009-12-15 16:34:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(buf);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_file_close(fd, NULL);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(ret == NULL || *len > 0);
|
2015-05-09 08:37:55 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2009-11-24 02:16:31 +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));
|
|
|
|
TR_ASSERT(data != NULL);
|
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* are we set up to download metadata? */
|
2017-06-13 02:24:09 +00:00
|
|
|
struct tr_incomplete_metadata* m = tor->incompleteMetadata;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (m == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* does this data pass the smell test? */
|
|
|
|
if (piece < 0 || piece >= m->pieceCount)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (piece < m->pieceCount - 1 ? len != METADATA_PIECE_SIZE : len > METADATA_PIECE_SIZE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
int const offset = piece * METADATA_PIECE_SIZE;
|
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(offset <= m->metadata_size);
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (len == 0 || len > m->metadata_size - offset)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
int neededPieceIndex = 0;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* do we need this piece? */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < m->piecesNeededCount; ++i, ++neededPieceIndex)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (m->piecesNeeded[i].piece == piece)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
if (neededPieceIndex == m->piecesNeededCount)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
memcpy(m->metadata + offset, data, len);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2019-03-17 05:00:15 +00:00
|
|
|
tr_removeElementFromArray(m->piecesNeeded, neededPieceIndex, sizeof(struct metadata_node), m->piecesNeededCount);
|
|
|
|
--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
|
|
|
bool success = false;
|
|
|
|
bool metainfoParsed = false;
|
|
|
|
uint8_t sha1[SHA_DIGEST_LENGTH];
|
|
|
|
|
|
|
|
/* we've got a complete set of metainfo... see if it passes the checksum test */
|
|
|
|
dbgmsg(tor, "metainfo piece %d was the last one", piece);
|
|
|
|
tr_sha1(sha1, m->metadata, m->metadata_size, NULL);
|
|
|
|
|
2020-08-27 23:41:26 +00:00
|
|
|
bool const checksumPassed = memcmp(sha1, tor->info.hash, SHA_DIGEST_LENGTH) == 0;
|
|
|
|
if (checksumPassed)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* checksum passed; now try to parse it as benc */
|
|
|
|
tr_variant infoDict;
|
2017-04-20 16:02:19 +00:00
|
|
|
int const err = tr_variantFromBenc(&infoDict, m->metadata, m->metadata_size);
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(tor, "err is %d", err);
|
|
|
|
|
2020-08-27 23:41:26 +00:00
|
|
|
metainfoParsed = err == 0;
|
|
|
|
if (metainfoParsed)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* yay we have bencoded metainfo... merge it into our .torrent file */
|
|
|
|
tr_variant newMetainfo;
|
|
|
|
char* path = tr_strdup(tor->info.torrent);
|
2010-02-06 05:22:27 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantFromFile(&newMetainfo, TR_VARIANT_FMT_BENC, path, NULL))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool hasInfo;
|
|
|
|
tr_info info;
|
|
|
|
size_t infoDictLength;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* remove any old .torrent and .resume files */
|
|
|
|
tr_sys_path_remove(path, NULL);
|
|
|
|
tr_torrentRemoveResume(tor);
|
2010-02-06 05:22:27 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(tor, "Saving completed metadata to \"%s\"", path);
|
|
|
|
tr_variantMergeDicts(tr_variantDictAddDict(&newMetainfo, TR_KEY_info, 0), &infoDict);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
memset(&info, 0, sizeof(tr_info));
|
|
|
|
success = tr_metainfoParse(tor->session, &newMetainfo, &info, &hasInfo, &infoDictLength);
|
2010-02-02 22:45:22 +00:00
|
|
|
|
2019-07-14 12:40:41 +00:00
|
|
|
if (success && tr_getBlockSize(info.pieceSize) == 0)
|
2010-06-24 20:36:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrentSetLocalError(tor, "%s", _("Magnet torrent's metadata is not usable"));
|
|
|
|
tr_metainfoFree(&info);
|
|
|
|
success = false;
|
2010-06-24 20:36:05 +00:00
|
|
|
}
|
2010-02-02 22:45:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (success)
|
2010-06-24 20:36:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* keep the new info */
|
|
|
|
tor->info = info;
|
|
|
|
tor->infoDictLength = infoDictLength;
|
|
|
|
|
|
|
|
/* save the new .torrent file */
|
|
|
|
tr_variantToFile(&newMetainfo, TR_VARIANT_FMT_BENC, tor->info.torrent);
|
|
|
|
tr_sessionSetTorrentFile(tor->session, tor->info.hashString, tor->info.torrent);
|
|
|
|
tr_torrentGotNewInfoDict(tor);
|
|
|
|
tr_torrentSetDirty(tor);
|
2010-06-24 20:36:05 +00:00
|
|
|
}
|
2009-11-24 19:45:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(&newMetainfo);
|
2010-02-02 22:45:22 +00:00
|
|
|
}
|
2010-03-17 17:07:40 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(&infoDict);
|
|
|
|
tr_free(path);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (success)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
incompleteMetadataFree(tor->incompleteMetadata);
|
|
|
|
tor->incompleteMetadata = NULL;
|
|
|
|
tor->isStopping = true;
|
|
|
|
tor->magnetVerify = true;
|
|
|
|
tor->startAfterVerify = true;
|
2019-11-12 23:13:42 +00:00
|
|
|
tr_torrentMarkEdited(tor);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
else /* drat. */
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const n = m->pieceCount;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2010-02-02 22:45:22 +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
|
|
|
}
|
2010-02-02 22:45:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
m->piecesNeededCount = n;
|
|
|
|
dbgmsg(tor, "metadata error; trying again. %d pieces left", n);
|
|
|
|
|
|
|
|
tr_logAddError("magnet status: checksum passed %d, metainfo parsed %d", (int)checksumPassed, (int)metainfoParsed);
|
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
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (m != NULL && m->piecesNeededCount > 0 && m->piecesNeeded[0].requestedAt + MIN_REPEAT_INTERVAL_SECS < 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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
double ret;
|
2013-01-07 18:16:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_torrentHasMetadata(tor))
|
2013-01-07 18:16:34 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = 1.0;
|
2009-11-24 17:10:40 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2013-01-07 18:16:34 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
struct tr_incomplete_metadata const* m = tor->incompleteMetadata;
|
2009-11-24 17:10:40 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (m == NULL || m->pieceCount == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
ret = 0.0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = (m->pieceCount - m->piecesNeededCount) / (double)m->pieceCount;
|
|
|
|
}
|
2013-01-07 18:16:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* name;
|
2017-04-19 12:04:45 +00:00
|
|
|
struct evbuffer* s = evbuffer_new();
|
|
|
|
|
|
|
|
evbuffer_add_printf(s, "magnet:?xt=urn:btih:%s", inf->hashString);
|
2009-11-29 08:27:55 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
name = inf->name;
|
2009-11-29 08:27:55 +00:00
|
|
|
|
2019-07-13 08:52:44 +00:00
|
|
|
if (!tr_str_is_empty(name))
|
2010-05-11 15:30:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add_printf(s, "%s", "&dn=");
|
|
|
|
tr_http_escape(s, name, TR_BAD_SIZE, true);
|
2010-05-11 15:30:30 +00:00
|
|
|
}
|
2012-07-23 15:28:27 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (unsigned int i = 0; i < inf->trackerCount; ++i)
|
2009-11-29 08:27:55 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add_printf(s, "%s", "&tr=");
|
|
|
|
tr_http_escape(s, inf->trackers[i].announce, TR_BAD_SIZE, true);
|
2009-11-29 08:27:55 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (unsigned int i = 0; i < inf->webseedCount; i++)
|
2013-02-04 18:54:38 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add_printf(s, "%s", "&ws=");
|
|
|
|
tr_http_escape(s, inf->webseeds[i], TR_BAD_SIZE, true);
|
2013-02-04 18:54:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return evbuffer_free_to_str(s, NULL);
|
2009-11-29 08:27:55 +00:00
|
|
|
}
|