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
|
|
|
*
|
2010-10-01 13:33:39 +00:00
|
|
|
* $Id$
|
2009-11-24 02:16:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2012-12-05 17:29:46 +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"
|
2014-12-04 12:13:59 +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"
|
|
|
|
#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"
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
#define dbgmsg(tor, ...) \
|
|
|
|
do \
|
|
|
|
{ \
|
2013-01-25 23:34:20 +00:00
|
|
|
if (tr_logGetDeepEnabled ()) \
|
|
|
|
tr_logAddDeep (__FILE__, __LINE__, tr_torrentName (tor), __VA_ARGS__); \
|
2012-12-05 17:29:46 +00:00
|
|
|
} \
|
|
|
|
while (0)
|
2009-11-24 02:16:31 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2013-01-07 18:16:34 +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
|
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
time_t requestedAt;
|
|
|
|
int piece;
|
2009-11-24 02:16:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct tr_incomplete_metadata
|
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
uint8_t * metadata;
|
|
|
|
int metadata_size;
|
|
|
|
int pieceCount;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
/** sorted from least to most recently requested */
|
|
|
|
struct metadata_node * piecesNeeded;
|
|
|
|
int piecesNeededCount;
|
2009-11-24 02:16:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
incompleteMetadataFree (struct tr_incomplete_metadata * m)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
tr_free (m->metadata);
|
|
|
|
tr_free (m->piecesNeeded);
|
|
|
|
tr_free (m);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentSetMetadataSizeHint (tr_torrent * tor, int size)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
if (!tr_torrentHasMetadata (tor))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
if (tor->incompleteMetadata == NULL)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
int i;
|
|
|
|
struct tr_incomplete_metadata * m;
|
|
|
|
const int n = (size + (METADATA_PIECE_SIZE - 1)) / METADATA_PIECE_SIZE;
|
|
|
|
dbgmsg (tor, "metadata is %d bytes in %d pieces", size, n);
|
|
|
|
|
|
|
|
m = tr_new (struct tr_incomplete_metadata, 1);
|
|
|
|
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);
|
|
|
|
|
|
|
|
for (i=0; i<n; ++i)
|
|
|
|
{
|
|
|
|
m->piecesNeeded[i].piece = i;
|
|
|
|
m->piecesNeeded[i].requestedAt = 0;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
tor->incompleteMetadata = m;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-25 11:34:35 +00:00
|
|
|
static size_t
|
2012-12-05 17:29:46 +00:00
|
|
|
findInfoDictOffset (const tr_torrent * tor)
|
2010-06-16 03:05:23 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
size_t fileLen;
|
|
|
|
uint8_t * fileContents;
|
2015-12-25 11:34:35 +00:00
|
|
|
size_t offset = 0;
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
/* load the file, and find the info dict's offset inside the file */
|
2015-04-11 10:51:59 +00:00
|
|
|
if ((fileContents = tr_loadFile (tor->info.torrent, &fileLen, NULL)))
|
2010-06-16 03:05:23 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
tr_variant top;
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
if (!tr_variantFromBenc (&top, fileContents, fileLen))
|
2010-06-16 03:05:23 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
tr_variant * infoDict;
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
if (tr_variantDictFindDict (&top, TR_KEY_info, &infoDict))
|
2010-06-16 03:05:23 +00:00
|
|
|
{
|
2015-12-25 11:34:35 +00:00
|
|
|
size_t infoLen;
|
2013-01-07 18:16:34 +00:00
|
|
|
char * infoContents = tr_variantToStr (infoDict, TR_VARIANT_FMT_BENC, &infoLen);
|
|
|
|
const uint8_t * i = (const uint8_t*) tr_memmem ((char*)fileContents, fileLen, infoContents, infoLen);
|
|
|
|
offset = i != NULL ? i - fileContents : 0;
|
|
|
|
tr_free (infoContents);
|
2010-06-16 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
tr_variantFree (&top);
|
2010-06-16 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
tr_free (fileContents);
|
2010-06-16 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
return offset;
|
2010-06-16 03:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
ensureInfoDictOffsetIsCached (tr_torrent * tor)
|
2010-06-16 03:05:23 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
assert (tr_torrentHasMetadata (tor));
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
if (!tor->infoDictOffsetIsCached)
|
2010-06-16 03:05:23 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
tor->infoDictOffset = findInfoDictOffset (tor);
|
|
|
|
tor->infoDictOffsetIsCached = true;
|
2010-06-16 03:05:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-24 02:16:31 +00:00
|
|
|
void*
|
2015-12-25 11:34:35 +00:00
|
|
|
tr_torrentGetMetadataPiece (tr_torrent * tor, int piece, size_t * len)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
char * ret = NULL;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
assert (tr_isTorrent (tor));
|
|
|
|
assert (piece >= 0);
|
|
|
|
assert (len != NULL);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
if (tr_torrentHasMetadata (tor))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2014-07-28 04:13:38 +00:00
|
|
|
tr_sys_file_t fd;
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
ensureInfoDictOffsetIsCached (tor);
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
assert (tor->infoDictLength > 0);
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2014-07-28 04:13:38 +00:00
|
|
|
fd = tr_sys_file_open (tor->info.torrent, TR_SYS_FILE_READ, 0, NULL);
|
|
|
|
if (fd != TR_BAD_SYS_FILE)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2015-12-25 11:34:35 +00:00
|
|
|
const size_t o = piece * METADATA_PIECE_SIZE;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2014-07-28 04:13:38 +00:00
|
|
|
if (tr_sys_file_seek (fd, tor->infoDictOffset + o, TR_SEEK_SET, NULL, NULL))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2015-12-25 11:34:35 +00:00
|
|
|
const size_t l = o + METADATA_PIECE_SIZE <= tor->infoDictLength
|
|
|
|
? METADATA_PIECE_SIZE
|
|
|
|
: tor->infoDictLength - o;
|
2009-12-15 16:34:12 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
if (0<l && l<=METADATA_PIECE_SIZE)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
char * buf = tr_new (char, l);
|
2014-07-28 04:13:38 +00:00
|
|
|
uint64_t n;
|
2015-12-25 11:34:35 +00:00
|
|
|
if (tr_sys_file_read (fd, buf, l, &n, NULL) && n == l)
|
2009-12-15 16:34:12 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
*len = l;
|
|
|
|
ret = buf;
|
|
|
|
buf = NULL;
|
2009-12-15 16:34:12 +00:00
|
|
|
}
|
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
tr_free (buf);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-28 04:13:38 +00:00
|
|
|
tr_sys_file_close (fd, NULL);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-31 22:13:31 +00:00
|
|
|
assert (ret == NULL || *len > 0);
|
2015-05-09 08:37:55 +00:00
|
|
|
|
2015-05-31 22:13:31 +00:00
|
|
|
return ret;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentSetMetadataPiece (tr_torrent * tor, int piece, const void * data, int len)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
int i;
|
|
|
|
struct tr_incomplete_metadata * m;
|
|
|
|
const int offset = piece * METADATA_PIECE_SIZE;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
assert (tr_isTorrent (tor));
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
dbgmsg (tor, "got metadata piece %d", piece);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
/* are we set up to download metadata? */
|
|
|
|
m = tor->incompleteMetadata;
|
|
|
|
if (m == NULL)
|
|
|
|
return;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
/* does this data pass the smell test? */
|
|
|
|
if (offset + len > m->metadata_size)
|
|
|
|
return;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
/* do we need this piece? */
|
|
|
|
for (i=0; i<m->piecesNeededCount; ++i)
|
|
|
|
if (m->piecesNeeded[i].piece == piece)
|
|
|
|
break;
|
|
|
|
if (i==m->piecesNeededCount)
|
|
|
|
return;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
memcpy (m->metadata + offset, data, len);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
tr_removeElementFromArray (m->piecesNeeded, i,
|
|
|
|
sizeof (struct metadata_node),
|
|
|
|
m->piecesNeededCount--);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
dbgmsg (tor, "saving metainfo piece %d... %d remain", piece, m->piecesNeededCount);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
/* are we done? */
|
|
|
|
if (m->piecesNeededCount == 0)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
bool success = false;
|
|
|
|
bool checksumPassed = 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);
|
|
|
|
if ((checksumPassed = !memcmp (sha1, tor->info.hash, SHA_DIGEST_LENGTH)))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
/* checksum passed; now try to parse it as benc */
|
|
|
|
tr_variant infoDict;
|
|
|
|
const int err = tr_variantFromBenc (&infoDict, m->metadata, m->metadata_size);
|
|
|
|
dbgmsg (tor, "err is %d", err);
|
|
|
|
if ((metainfoParsed = !err))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +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
|
|
|
|
2015-04-11 10:51:59 +00:00
|
|
|
if (tr_variantFromFile (&newMetainfo, TR_VARIANT_FMT_BENC, path, NULL))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
bool hasInfo;
|
|
|
|
tr_info info;
|
2015-12-25 11:34:35 +00:00
|
|
|
size_t infoDictLength;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
/* remove any old .torrent and .resume files */
|
2014-07-08 00:08:43 +00:00
|
|
|
tr_sys_path_remove (path, NULL);
|
2013-01-07 18:16:34 +00:00
|
|
|
tr_torrentRemoveResume (tor);
|
2010-02-06 05:22:27 +00:00
|
|
|
|
2013-01-07 18:16:34 +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
|
|
|
|
2013-01-07 18:16:34 +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
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
if (success && !tr_getBlockSize (info.pieceSize))
|
2010-06-24 20:36:05 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
tr_torrentSetLocalError (tor, "%s", _("Magnet torrent's metadata is not usable"));
|
|
|
|
success = false;
|
2010-06-24 20:36:05 +00:00
|
|
|
}
|
2010-02-02 22:45:22 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
if (success)
|
2010-06-24 20:36:05 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +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
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
tr_variantFree (&newMetainfo);
|
2010-02-02 22:45:22 +00:00
|
|
|
}
|
2010-03-17 17:07:40 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
tr_variantFree (&infoDict);
|
|
|
|
tr_free (path);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
if (success)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
incompleteMetadataFree (tor->incompleteMetadata);
|
|
|
|
tor->incompleteMetadata = NULL;
|
2014-01-19 05:03:27 +00:00
|
|
|
tor->isStopping = true;
|
|
|
|
tor->magnetVerify = true;
|
|
|
|
tor->startAfterVerify = true;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
else /* drat. */
|
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
const int n = m->pieceCount;
|
|
|
|
for (i=0; i<n; ++i)
|
2010-02-02 22:45:22 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
m->piecesNeeded[i].piece = i;
|
|
|
|
m->piecesNeeded[i].requestedAt = 0;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
2013-01-07 18:16:34 +00:00
|
|
|
m->piecesNeededCount = n;
|
|
|
|
dbgmsg (tor, "metadata error; trying again. %d pieces left", n);
|
2010-02-02 22:45:22 +00:00
|
|
|
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddError ("magnet status: checksum passed %d, metainfo parsed %d",
|
2012-12-05 17:29:46 +00:00
|
|
|
(int)checksumPassed, (int)metainfoParsed);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentGetNextMetadataRequest (tr_torrent * tor, time_t now, int * setme_piece)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
bool have_request = false;
|
|
|
|
struct tr_incomplete_metadata * m;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
assert (tr_isTorrent (tor));
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
m = tor->incompleteMetadata;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2013-01-07 18:16:34 +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
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
int i;
|
|
|
|
const int piece = m->piecesNeeded[0].piece;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
tr_removeElementFromArray (m->piecesNeeded, 0,
|
|
|
|
sizeof (struct metadata_node),
|
|
|
|
m->piecesNeededCount--);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
i = m->piecesNeededCount++;
|
|
|
|
m->piecesNeeded[i].piece = piece;
|
|
|
|
m->piecesNeeded[i].requestedAt = now;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
dbgmsg (tor, "next piece to request: %d", piece);
|
|
|
|
*setme_piece = piece;
|
|
|
|
have_request = true;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
return have_request;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
2009-11-24 17:10:40 +00:00
|
|
|
|
2010-07-01 05:14:34 +00:00
|
|
|
double
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentGetMetadataPercent (const tr_torrent * tor)
|
2009-11-24 17:10:40 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
double ret;
|
|
|
|
|
|
|
|
if (tr_torrentHasMetadata (tor))
|
|
|
|
{
|
|
|
|
ret = 1.0;
|
2009-11-24 17:10:40 +00:00
|
|
|
}
|
2013-01-07 18:16:34 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
const struct tr_incomplete_metadata * m = tor->incompleteMetadata;
|
2009-11-24 17:10:40 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
if (!m || !m->pieceCount)
|
|
|
|
ret = 0.0;
|
|
|
|
else
|
|
|
|
ret = (m->pieceCount - m->piecesNeededCount) / (double)m->pieceCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2009-11-24 17:10:40 +00:00
|
|
|
}
|
2009-11-29 08:27:55 +00:00
|
|
|
|
2013-01-22 00:25:42 +00:00
|
|
|
/* FIXME: this should be renamed tr_metainfoGetMagnetLink() and moved to metainfo.c for consistency */
|
2013-01-07 18:16:34 +00:00
|
|
|
char *
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrentInfoGetMagnetLink (const tr_info * inf)
|
2009-11-29 08:27:55 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
unsigned int i;
|
|
|
|
const char * name;
|
|
|
|
struct evbuffer * s = evbuffer_new ();
|
2009-11-29 08:27:55 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
evbuffer_add_printf (s, "magnet:?xt=urn:btih:%s", inf->hashString);
|
2009-11-29 08:27:55 +00:00
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
name = inf->name;
|
|
|
|
if (name && *name)
|
2010-05-11 15:30:30 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
evbuffer_add_printf (s, "%s", "&dn=");
|
2015-12-25 11:34:35 +00:00
|
|
|
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
|
|
|
|
2013-01-07 18:16:34 +00:00
|
|
|
for (i=0; i<inf->trackerCount; ++i)
|
2009-11-29 08:27:55 +00:00
|
|
|
{
|
2013-01-07 18:16:34 +00:00
|
|
|
evbuffer_add_printf (s, "%s", "&tr=");
|
2015-12-25 11:34:35 +00:00
|
|
|
tr_http_escape (s, inf->trackers[i].announce, TR_BAD_SIZE, true);
|
2009-11-29 08:27:55 +00:00
|
|
|
}
|
|
|
|
|
2013-02-04 18:54:38 +00:00
|
|
|
for (i=0; i<inf->webseedCount; i++)
|
|
|
|
{
|
|
|
|
evbuffer_add_printf (s, "%s", "&ws=");
|
2015-12-25 11:34:35 +00:00
|
|
|
tr_http_escape (s, inf->webseeds[i], TR_BAD_SIZE, true);
|
2013-02-04 18:54:38 +00:00
|
|
|
}
|
|
|
|
|
2015-12-13 10:23:22 +00:00
|
|
|
return evbuffer_free_to_str (s, NULL);
|
2009-11-29 08:27:55 +00:00
|
|
|
}
|