2009-12-07 03:57:55 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2007-2014 Mnemosyne LLC
|
2006-07-16 19:39:23 +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.
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2009-12-07 03:57:55 +00:00
|
|
|
* $Id$
|
|
|
|
*/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <string.h> /* strlen () */
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/buffer.h>
|
2008-04-24 01:42:53 +00:00
|
|
|
|
2006-07-16 19:39:23 +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"
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "metainfo.h"
|
2012-12-05 17:29:46 +00:00
|
|
|
#include "platform.h" /* tr_getTorrentDir () */
|
2014-07-08 00:08:43 +00:00
|
|
|
#include "session.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-20 15:17:36 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2014-04-27 20:17:16 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2014-04-27 20:17:16 +00:00
|
|
|
#define PATH_DELIMITER_CHARS "/\\"
|
|
|
|
#else
|
|
|
|
#define PATH_DELIMITER_CHARS "/"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
char_is_path_separator (char c)
|
|
|
|
{
|
|
|
|
return strchr(PATH_DELIMITER_CHARS, c) != NULL;
|
|
|
|
}
|
|
|
|
|
2010-04-20 23:14:00 +00:00
|
|
|
char*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_metainfoGetBasename (const tr_info * inf)
|
2010-04-20 23:14:00 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
size_t i;
|
2013-01-22 00:25:42 +00:00
|
|
|
const char * name = inf->originalName;
|
|
|
|
const size_t name_len = strlen (name);
|
|
|
|
char * ret = tr_strdup_printf ("%s.%16.16s", name, inf->hashString);
|
2010-04-20 23:14:00 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<name_len; ++i)
|
2014-04-27 20:17:16 +00:00
|
|
|
if (char_is_path_separator (ret[i]))
|
2012-12-05 17:29:46 +00:00
|
|
|
ret[i] = '_';
|
2010-11-11 15:31:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return ret;
|
2010-04-20 23:14:00 +00:00
|
|
|
}
|
|
|
|
|
2008-08-20 19:00:52 +00:00
|
|
|
static char*
|
2012-12-05 17:29:46 +00:00
|
|
|
getTorrentFilename (const tr_session * session, const tr_info * inf)
|
2008-04-14 11:52:50 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char * base = tr_metainfoGetBasename (inf);
|
|
|
|
char * filename = tr_strdup_printf ("%s" TR_PATH_DELIMITER_STR "%s.torrent",
|
|
|
|
tr_getTorrentDir (session), base);
|
|
|
|
tr_free (base);
|
|
|
|
return filename;
|
2008-04-14 11:52:50 +00:00
|
|
|
}
|
|
|
|
|
2008-08-20 21:01:17 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool
|
2014-04-27 20:17:16 +00:00
|
|
|
path_component_is_suspicious (const char * component)
|
2009-12-28 00:35:29 +00:00
|
|
|
{
|
2014-04-27 20:17:16 +00:00
|
|
|
return (component == NULL)
|
|
|
|
|| (strpbrk (component, PATH_DELIMITER_CHARS) != NULL)
|
|
|
|
|| (strcmp (component, ".") == 0)
|
|
|
|
|| (strcmp (component, "..") == 0);
|
2009-12-28 00:35:29 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool
|
2012-12-14 04:34:42 +00:00
|
|
|
getfile (char ** setme, const char * root, tr_variant * path, struct evbuffer * buf)
|
2008-08-20 21:01:17 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
bool success = false;
|
2014-06-09 02:53:04 +00:00
|
|
|
size_t root_len = 0;
|
2008-08-20 21:01:17 +00:00
|
|
|
|
2014-04-27 20:17:16 +00:00
|
|
|
*setme = NULL;
|
|
|
|
|
|
|
|
/* root's already been checked by caller */
|
|
|
|
assert (!path_component_is_suspicious (root));
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
if (tr_variantIsList (path))
|
2008-08-20 21:01:17 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int i;
|
2012-12-14 04:34:42 +00:00
|
|
|
const int n = tr_variantListSize (path);
|
2008-08-20 21:01:17 +00:00
|
|
|
|
2014-04-27 20:17:16 +00:00
|
|
|
success = true;
|
2012-12-05 17:29:46 +00:00
|
|
|
evbuffer_drain (buf, evbuffer_get_length (buf));
|
2014-06-09 02:53:04 +00:00
|
|
|
root_len = strlen (root);
|
|
|
|
evbuffer_add (buf, root, root_len);
|
2014-04-27 20:17:16 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<n; i++)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
size_t len;
|
2012-12-05 17:29:46 +00:00
|
|
|
const char * str;
|
|
|
|
|
2014-04-27 20:17:16 +00:00
|
|
|
if (!tr_variantGetStr (tr_variantListChild (path, i), &str, &len) ||
|
|
|
|
path_component_is_suspicious (str))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2014-04-27 20:17:16 +00:00
|
|
|
success = false;
|
|
|
|
break;
|
2008-08-20 21:01:17 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 02:53:04 +00:00
|
|
|
if (!*str)
|
|
|
|
continue;
|
|
|
|
|
2014-04-27 20:17:16 +00:00
|
|
|
evbuffer_add (buf, TR_PATH_DELIMITER_STR, 1);
|
|
|
|
evbuffer_add (buf, str, len);
|
|
|
|
}
|
2008-08-20 21:01:17 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 02:53:04 +00:00
|
|
|
if (success && (evbuffer_get_length (buf) <= root_len))
|
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
2014-04-27 20:17:16 +00:00
|
|
|
if (success)
|
2009-12-28 00:35:29 +00:00
|
|
|
{
|
2014-04-27 20:17:16 +00:00
|
|
|
*setme = tr_utf8clean ((char*)evbuffer_pullup (buf, -1), evbuffer_get_length (buf));
|
|
|
|
/*fprintf (stderr, "[%s]\n", *setme);*/
|
2009-12-28 00:35:29 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return success;
|
2008-08-20 21:01:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
2012-12-14 04:34:42 +00:00
|
|
|
parseFiles (tr_info * inf, tr_variant * files, const tr_variant * length)
|
2008-08-20 21:01:17 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int64_t len;
|
2008-08-20 21:01:17 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
inf->totalSize = 0;
|
2008-08-20 21:01:17 +00:00
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
if (tr_variantIsList (files)) /* multi-file mode */
|
2008-08-20 21:01:17 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_file_index_t i;
|
2014-04-27 20:17:16 +00:00
|
|
|
struct evbuffer * buf;
|
|
|
|
const char * result;
|
|
|
|
|
|
|
|
if (path_component_is_suspicious (inf->name))
|
|
|
|
return "path";
|
|
|
|
|
|
|
|
buf = evbuffer_new ();
|
|
|
|
result = NULL;
|
2008-08-21 16:12:17 +00:00
|
|
|
|
2014-06-08 22:21:27 +00:00
|
|
|
inf->isFolder = true;
|
2012-12-14 04:34:42 +00:00
|
|
|
inf->fileCount = tr_variantListSize (files);
|
2012-12-05 17:29:46 +00:00
|
|
|
inf->files = tr_new0 (tr_file, inf->fileCount);
|
2008-08-20 21:01:17 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<inf->fileCount; i++)
|
2008-08-20 21:01:17 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * file;
|
|
|
|
tr_variant * path;
|
2008-08-20 21:01:17 +00:00
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
file = tr_variantListChild (files, i);
|
|
|
|
if (!tr_variantIsDict (file))
|
2014-04-27 20:17:16 +00:00
|
|
|
{
|
|
|
|
result = "files";
|
|
|
|
break;
|
|
|
|
}
|
2008-08-20 21:01:17 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (!tr_variantDictFindList (file, TR_KEY_path_utf_8, &path))
|
|
|
|
if (!tr_variantDictFindList (file, TR_KEY_path, &path))
|
2014-04-27 20:17:16 +00:00
|
|
|
{
|
|
|
|
result = "path";
|
|
|
|
break;
|
|
|
|
}
|
2008-08-20 21:01:17 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!getfile (&inf->files[i].name, inf->name, path, buf))
|
2014-04-27 20:17:16 +00:00
|
|
|
{
|
|
|
|
result = "path";
|
|
|
|
break;
|
|
|
|
}
|
2008-08-20 21:01:17 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (!tr_variantDictFindInt (file, TR_KEY_length, &len))
|
2014-04-27 20:17:16 +00:00
|
|
|
{
|
|
|
|
result = "length";
|
|
|
|
break;
|
|
|
|
}
|
2008-08-20 21:01:17 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
inf->files[i].length = len;
|
|
|
|
inf->totalSize += len;
|
2008-08-20 21:01:17 +00:00
|
|
|
}
|
2011-04-01 03:09:24 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
evbuffer_free (buf);
|
2014-04-27 20:17:16 +00:00
|
|
|
return result;
|
2008-08-20 21:01:17 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
else if (tr_variantGetInt (length, &len)) /* single-file mode */
|
2008-08-20 21:01:17 +00:00
|
|
|
{
|
2014-04-27 20:17:16 +00:00
|
|
|
if (path_component_is_suspicious (inf->name))
|
2012-12-05 17:29:46 +00:00
|
|
|
return "path";
|
|
|
|
|
2014-06-08 22:21:27 +00:00
|
|
|
inf->isFolder = false;
|
2012-12-05 17:29:46 +00:00
|
|
|
inf->fileCount = 1;
|
|
|
|
inf->files = tr_new0 (tr_file, 1);
|
|
|
|
inf->files[0].name = tr_strdup (inf->name);
|
|
|
|
inf->files[0].length = len;
|
|
|
|
inf->totalSize += len;
|
2008-08-20 21:01:17 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else
|
2008-08-20 21:01:17 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return "length";
|
2008-08-20 21:01:17 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return NULL;
|
2008-08-20 21:01:17 +00:00
|
|
|
}
|
|
|
|
|
2008-04-24 03:26:36 +00:00
|
|
|
static char *
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_convertAnnounceToScrape (const char * announce)
|
2008-04-24 03:26:36 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char * scrape = NULL;
|
|
|
|
const char * s;
|
|
|
|
|
|
|
|
/* To derive the scrape URL use the following steps:
|
|
|
|
* Begin with the announce URL. Find the last '/' in it.
|
|
|
|
* If the text immediately following that '/' isn't 'announce'
|
|
|
|
* it will be taken as a sign that that tracker doesn't support
|
|
|
|
* the scrape convention. If it does, substitute 'scrape' for
|
|
|
|
* 'announce' to find the scrape page. */
|
|
|
|
if (((s = strrchr (announce, '/'))) && !strncmp (++s, "announce", 8))
|
2008-04-24 03:26:36 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const char * prefix = announce;
|
|
|
|
const size_t prefix_len = s - announce;
|
|
|
|
const char * suffix = s + 8;
|
|
|
|
const size_t suffix_len = strlen (suffix);
|
|
|
|
const size_t alloc_len = prefix_len + 6 + suffix_len + 1;
|
|
|
|
char * walk = scrape = tr_new (char, alloc_len);
|
|
|
|
memcpy (walk, prefix, prefix_len); walk += prefix_len;
|
|
|
|
memcpy (walk, "scrape", 6); walk += 6;
|
|
|
|
memcpy (walk, suffix, suffix_len); walk += suffix_len;
|
|
|
|
*walk++ = '\0';
|
|
|
|
assert (walk - scrape == (int)alloc_len);
|
2008-04-24 03:26:36 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
/* Some torrents with UDP annouce URLs don't have /announce. */
|
|
|
|
else if (!strncmp (announce, "udp:", 4))
|
2012-05-20 14:47:18 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
scrape = tr_strdup (announce);
|
2012-05-20 14:47:18 +00:00
|
|
|
}
|
2008-04-24 03:26:36 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return scrape;
|
2008-04-24 03:26:36 +00:00
|
|
|
}
|
|
|
|
|
2008-08-20 21:01:17 +00:00
|
|
|
static const char*
|
2012-12-14 04:34:42 +00:00
|
|
|
getannounce (tr_info * inf, tr_variant * meta)
|
2008-04-24 03:26:36 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
size_t len;
|
2012-12-05 17:29:46 +00:00
|
|
|
const char * str;
|
|
|
|
tr_tracker_info * trackers = NULL;
|
|
|
|
int trackerCount = 0;
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * tiers;
|
2008-04-24 03:26:36 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* Announce-list */
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindList (meta, TR_KEY_announce_list, &tiers))
|
2008-04-24 03:26:36 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int n;
|
|
|
|
int i, j, validTiers;
|
2012-12-14 04:34:42 +00:00
|
|
|
const int numTiers = tr_variantListSize (tiers);
|
2008-04-24 03:26:36 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
n = 0;
|
|
|
|
for (i=0; i<numTiers; i++)
|
2012-12-14 04:34:42 +00:00
|
|
|
n += tr_variantListSize (tr_variantListChild (tiers, i));
|
2008-04-24 03:26:36 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
trackers = tr_new0 (tr_tracker_info, n);
|
2008-04-24 03:26:36 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0, validTiers=0; i<numTiers; i++)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * tier = tr_variantListChild (tiers, i);
|
|
|
|
const int tierSize = tr_variantListSize (tier);
|
2012-12-05 17:29:46 +00:00
|
|
|
bool anyAdded = false;
|
|
|
|
for (j=0; j<tierSize; j++)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
if (tr_variantGetStr (tr_variantListChild (tier, j), &str, &len))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
char * url = tr_strstrip (tr_strndup (str, len));
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!tr_urlIsValidTracker (url))
|
|
|
|
{
|
|
|
|
tr_free (url);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_tracker_info * t = trackers + trackerCount;
|
|
|
|
t->tier = validTiers;
|
|
|
|
t->announce = url;
|
|
|
|
t->scrape = tr_convertAnnounceToScrape (url);
|
|
|
|
t->id = trackerCount;
|
|
|
|
|
|
|
|
anyAdded = true;
|
|
|
|
++trackerCount;
|
2008-09-05 19:11:30 +00:00
|
|
|
}
|
2008-04-24 03:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (anyAdded)
|
|
|
|
++validTiers;
|
2008-04-24 03:26:36 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* did we use any of the tiers? */
|
|
|
|
if (!trackerCount)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (trackers);
|
|
|
|
trackers = NULL;
|
2008-04-24 03:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* Regular announce value */
|
2012-12-22 20:35:19 +00:00
|
|
|
if (!trackerCount && tr_variantDictFindStr (meta, TR_KEY_announce, &str, &len))
|
2008-04-24 03:26:36 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
char * url = tr_strstrip (tr_strndup (str, len));
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!tr_urlIsValidTracker (url))
|
|
|
|
{
|
|
|
|
tr_free (url);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
trackers = tr_new0 (tr_tracker_info, 1);
|
|
|
|
trackers[trackerCount].tier = 0;
|
|
|
|
trackers[trackerCount].announce = url;
|
|
|
|
trackers[trackerCount].scrape = tr_convertAnnounceToScrape (url);
|
|
|
|
trackers[trackerCount].id = 0;
|
|
|
|
trackerCount++;
|
|
|
|
/*fprintf (stderr, "single announce: [%s]\n", url);*/
|
2008-09-05 19:11:30 +00:00
|
|
|
}
|
2008-04-24 03:26:36 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
inf->trackers = trackers;
|
|
|
|
inf->trackerCount = trackerCount;
|
2008-04-24 03:26:36 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return NULL;
|
2008-08-20 21:01:17 +00:00
|
|
|
}
|
|
|
|
|
2011-09-06 16:45:48 +00:00
|
|
|
/**
|
|
|
|
* @brief Ensure that the URLs for multfile torrents end in a slash.
|
|
|
|
*
|
|
|
|
* See http://bittorrent.org/beps/bep_0019.html#metadata-extension
|
|
|
|
* for background on how the trailing slash is used for "url-list"
|
|
|
|
* fields.
|
|
|
|
*
|
|
|
|
* This function is to workaround some .torrent generators, such as
|
|
|
|
* mktorrent and very old versions of utorrent, that don't add the
|
|
|
|
* trailing slash for multifile torrents if omitted by the end user.
|
|
|
|
*/
|
|
|
|
static char*
|
2012-12-05 17:29:46 +00:00
|
|
|
fix_webseed_url (const tr_info * inf, const char * url_in)
|
2011-09-06 16:45:48 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
size_t len;
|
|
|
|
char * url;
|
|
|
|
char * ret = NULL;
|
2012-10-14 18:10:17 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
url = tr_strdup (url_in);
|
|
|
|
tr_strstrip (url);
|
|
|
|
len = strlen (url);
|
2011-09-06 16:45:48 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tr_urlIsValid (url, len))
|
2011-09-06 16:45:48 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((inf->fileCount > 1) && (len > 0) && (url[len-1] != '/'))
|
|
|
|
ret = tr_strdup_printf ("%*.*s/", (int)len, (int)len, url);
|
|
|
|
else
|
|
|
|
ret = tr_strndup (url, len);
|
2011-09-06 16:45:48 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (url);
|
|
|
|
return ret;
|
2011-09-06 16:45:48 +00:00
|
|
|
}
|
|
|
|
|
2008-08-20 21:01:17 +00:00
|
|
|
static void
|
2012-12-14 04:34:42 +00:00
|
|
|
geturllist (tr_info * inf, tr_variant * meta)
|
2008-08-20 21:01:17 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * urls;
|
2012-12-05 17:29:46 +00:00
|
|
|
const char * url;
|
2008-08-20 21:01:17 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindList (meta, TR_KEY_url_list, &urls))
|
2008-08-20 21:01:17 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int i;
|
2012-12-14 04:34:42 +00:00
|
|
|
const int n = tr_variantListSize (urls);
|
2008-08-20 21:01:17 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
inf->webseedCount = 0;
|
|
|
|
inf->webseeds = tr_new0 (char*, n);
|
2008-05-29 00:38:31 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<n; i++)
|
2011-05-05 03:10:51 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
if (tr_variantGetStr (tr_variantListChild (urls, i), &url, NULL))
|
2011-05-05 03:10:51 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char * fixed_url = fix_webseed_url (inf, url);
|
2011-05-05 03:10:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (fixed_url != NULL)
|
|
|
|
inf->webseeds[inf->webseedCount++] = fixed_url;
|
2011-05-05 03:10:51 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-20 21:01:17 +00:00
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
else if (tr_variantDictFindStr (meta, TR_KEY_url_list, &url, NULL)) /* handle single items in webseeds */
|
2010-08-22 18:40:18 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char * fixed_url = fix_webseed_url (inf, url);
|
2011-05-05 03:10:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (fixed_url != NULL)
|
2011-05-05 03:10:51 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
inf->webseedCount = 1;
|
|
|
|
inf->webseeds = tr_new0 (char*, 1);
|
|
|
|
inf->webseeds[0] = fixed_url;
|
2011-05-05 03:10:51 +00:00
|
|
|
}
|
2010-08-22 18:40:18 +00:00
|
|
|
}
|
2008-04-24 03:26:36 +00:00
|
|
|
}
|
|
|
|
|
2008-08-20 21:01:17 +00:00
|
|
|
static const char*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_metainfoParseImpl (const tr_session * session,
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_info * inf,
|
2011-03-22 15:19:54 +00:00
|
|
|
bool * hasInfoDict,
|
2010-02-02 22:45:22 +00:00
|
|
|
int * infoDictLength,
|
2012-12-14 04:34:42 +00:00
|
|
|
const tr_variant * meta_in)
|
2007-12-21 22:18:40 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int64_t i;
|
2012-12-14 04:34:42 +00:00
|
|
|
size_t len;
|
2012-12-05 17:29:46 +00:00
|
|
|
const char * str;
|
|
|
|
const uint8_t * raw;
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * d;
|
|
|
|
tr_variant * infoDict = NULL;
|
|
|
|
tr_variant * meta = (tr_variant *) meta_in;
|
2012-12-05 17:29:46 +00:00
|
|
|
bool b;
|
|
|
|
bool isMagnet = false;
|
|
|
|
|
|
|
|
/* info_hash: urlencoded 20-byte SHA1 hash of the value of the info key
|
|
|
|
* from the Metainfo file. Note that the value will be a bencoded
|
|
|
|
* dictionary, given the definition of the info key above. */
|
2012-12-22 20:35:19 +00:00
|
|
|
b = tr_variantDictFindDict (meta, TR_KEY_info, &infoDict);
|
2012-12-05 17:29:46 +00:00
|
|
|
if (hasInfoDict != NULL)
|
|
|
|
*hasInfoDict = b;
|
|
|
|
|
|
|
|
if (!b)
|
2010-02-02 22:45:22 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
/* no info dictionary... is this a magnet link? */
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindDict (meta, TR_KEY_magnet_info, &d))
|
2010-02-02 22:45:22 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
isMagnet = true;
|
|
|
|
|
|
|
|
/* get the info-hash */
|
2012-12-22 20:35:19 +00:00
|
|
|
if (!tr_variantDictFindRaw (d, TR_KEY_info_hash, &raw, &len))
|
2012-12-05 17:29:46 +00:00
|
|
|
return "info_hash";
|
2012-12-14 04:34:42 +00:00
|
|
|
if (len != SHA_DIGEST_LENGTH)
|
2012-12-05 17:29:46 +00:00
|
|
|
return "info_hash";
|
2012-12-14 04:34:42 +00:00
|
|
|
memcpy (inf->hash, raw, len);
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_sha1_to_hex (inf->hashString, inf->hash);
|
|
|
|
|
|
|
|
/* maybe get the display name */
|
2012-12-22 20:35:19 +00:00
|
|
|
if (tr_variantDictFindStr (d, TR_KEY_display_name, &str, &len))
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
|
|
|
tr_free (inf->name);
|
2013-01-22 00:25:42 +00:00
|
|
|
tr_free (inf->originalName);
|
2012-12-14 04:34:42 +00:00
|
|
|
inf->name = tr_strndup (str, len);
|
2013-01-22 00:25:42 +00:00
|
|
|
inf->originalName = tr_strndup (str, len);
|
2010-02-02 22:45:22 +00:00
|
|
|
}
|
2010-08-03 03:16:21 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!inf->name)
|
2013-01-22 00:25:42 +00:00
|
|
|
inf->name = tr_strdup (inf->hashString);
|
|
|
|
if (!inf->originalName)
|
|
|
|
inf->originalName = tr_strdup (inf->hashString);
|
2010-02-02 22:45:22 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else /* not a magnet link and has no info dict... */
|
2010-02-02 22:45:22 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return "info";
|
2010-02-02 22:45:22 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int len;
|
2012-12-14 04:34:42 +00:00
|
|
|
char * bstr = tr_variantToStr (infoDict, TR_VARIANT_FMT_BENC, &len);
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_sha1 (inf->hash, bstr, len, NULL);
|
|
|
|
tr_sha1_to_hex (inf->hashString, inf->hash);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (infoDictLength != NULL)
|
|
|
|
*infoDictLength = len;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (bstr);
|
2007-12-21 22:18:40 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* name */
|
|
|
|
if (!isMagnet)
|
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
len = 0;
|
2012-12-22 20:35:19 +00:00
|
|
|
if (!tr_variantDictFindStr (infoDict, TR_KEY_name_utf_8, &str, &len))
|
|
|
|
if (!tr_variantDictFindStr (infoDict, TR_KEY_name, &str, &len))
|
2012-12-05 17:29:46 +00:00
|
|
|
str = "";
|
|
|
|
if (!str || !*str)
|
|
|
|
return "name";
|
|
|
|
tr_free (inf->name);
|
2013-01-22 00:25:42 +00:00
|
|
|
tr_free (inf->originalName);
|
2012-12-14 04:34:42 +00:00
|
|
|
inf->name = tr_utf8clean (str, len);
|
2013-01-22 00:25:42 +00:00
|
|
|
inf->originalName = tr_strdup (inf->name);
|
2010-02-02 22:45:22 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* comment */
|
2012-12-14 04:34:42 +00:00
|
|
|
len = 0;
|
2012-12-22 20:35:19 +00:00
|
|
|
if (!tr_variantDictFindStr (meta, TR_KEY_comment_utf_8, &str, &len))
|
|
|
|
if (!tr_variantDictFindStr (meta, TR_KEY_comment, &str, &len))
|
2012-12-05 17:29:46 +00:00
|
|
|
str = "";
|
|
|
|
tr_free (inf->comment);
|
2012-12-14 04:34:42 +00:00
|
|
|
inf->comment = tr_utf8clean (str, len);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
|
|
|
/* created by */
|
2012-12-14 04:34:42 +00:00
|
|
|
len = 0;
|
2012-12-22 20:35:19 +00:00
|
|
|
if (!tr_variantDictFindStr (meta, TR_KEY_created_by_utf_8, &str, &len))
|
|
|
|
if (!tr_variantDictFindStr (meta, TR_KEY_created_by, &str, &len))
|
2012-12-05 17:29:46 +00:00
|
|
|
str = "";
|
|
|
|
tr_free (inf->creator);
|
2012-12-14 04:34:42 +00:00
|
|
|
inf->creator = tr_utf8clean (str, len);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
|
|
|
/* creation date */
|
2012-12-22 20:35:19 +00:00
|
|
|
if (!tr_variantDictFindInt (meta, TR_KEY_creation_date, &i))
|
2012-12-05 17:29:46 +00:00
|
|
|
i = 0;
|
|
|
|
inf->dateCreated = i;
|
|
|
|
|
|
|
|
/* private */
|
2012-12-22 20:35:19 +00:00
|
|
|
if (!tr_variantDictFindInt (infoDict, TR_KEY_private, &i))
|
|
|
|
if (!tr_variantDictFindInt (meta, TR_KEY_private, &i))
|
2012-12-05 17:29:46 +00:00
|
|
|
i = 0;
|
|
|
|
inf->isPrivate = i != 0;
|
|
|
|
|
|
|
|
/* piece length */
|
|
|
|
if (!isMagnet)
|
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
if (!tr_variantDictFindInt (infoDict, TR_KEY_piece_length, &i) || (i < 1))
|
2012-12-05 17:29:46 +00:00
|
|
|
return "piece length";
|
|
|
|
inf->pieceSize = i;
|
2010-02-02 22:45:22 +00:00
|
|
|
}
|
2008-08-20 21:01:17 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* pieces */
|
|
|
|
if (!isMagnet)
|
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
if (!tr_variantDictFindRaw (infoDict, TR_KEY_pieces, &raw, &len))
|
2012-12-05 17:29:46 +00:00
|
|
|
return "pieces";
|
2012-12-14 04:34:42 +00:00
|
|
|
if (len % SHA_DIGEST_LENGTH)
|
2012-12-05 17:29:46 +00:00
|
|
|
return "pieces";
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
inf->pieceCount = len / SHA_DIGEST_LENGTH;
|
2012-12-05 17:29:46 +00:00
|
|
|
inf->pieces = tr_new0 (tr_piece, inf->pieceCount);
|
|
|
|
for (i=0; i<inf->pieceCount; i++)
|
|
|
|
memcpy (inf->pieces[i].hash, &raw[i * SHA_DIGEST_LENGTH], SHA_DIGEST_LENGTH);
|
2010-02-02 22:45:22 +00:00
|
|
|
}
|
2008-08-20 19:21:57 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* files */
|
|
|
|
if (!isMagnet)
|
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
if ((str = parseFiles (inf, tr_variantDictFind (infoDict, TR_KEY_files),
|
|
|
|
tr_variantDictFind (infoDict, TR_KEY_length))))
|
2012-12-05 17:29:46 +00:00
|
|
|
return str;
|
|
|
|
|
|
|
|
if (!inf->fileCount || !inf->totalSize)
|
|
|
|
return "files";
|
|
|
|
|
|
|
|
if ((uint64_t) inf->pieceCount != (inf->totalSize + inf->pieceSize - 1) / inf->pieceSize)
|
|
|
|
return "files";
|
2010-02-02 22:45:22 +00:00
|
|
|
}
|
2006-12-17 16:36:27 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* get announce or announce-list */
|
|
|
|
if ((str = getannounce (inf, meta)))
|
|
|
|
return str;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* get the url-list */
|
|
|
|
geturllist (inf, meta);
|
2008-06-07 21:26:41 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* filename of Transmission's copy */
|
|
|
|
tr_free (inf->torrent);
|
|
|
|
inf->torrent = session ? getTorrentFilename (session, inf) : NULL;
|
2008-04-14 11:52:50 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return NULL;
|
2008-08-20 21:01:17 +00:00
|
|
|
}
|
2007-03-05 00:07:48 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_metainfoParse (const tr_session * session,
|
2012-12-14 04:34:42 +00:00
|
|
|
const tr_variant * meta_in,
|
2009-08-05 01:59:16 +00:00
|
|
|
tr_info * inf,
|
2011-03-22 15:19:54 +00:00
|
|
|
bool * hasInfoDict,
|
2012-12-05 17:29:46 +00:00
|
|
|
int * infoDictLength)
|
2008-08-20 21:01:17 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const char * badTag = tr_metainfoParseImpl (session,
|
|
|
|
inf,
|
|
|
|
hasInfoDict,
|
|
|
|
infoDictLength,
|
|
|
|
meta_in);
|
|
|
|
const bool success = badTag == NULL;
|
|
|
|
|
|
|
|
if (badTag)
|
2008-08-20 21:01:17 +00:00
|
|
|
{
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddNamedError (inf->name, _("Invalid metadata entry \"%s\""), badTag);
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_metainfoFree (inf);
|
2008-08-20 21:01:17 +00:00
|
|
|
}
|
2009-08-05 01:59:16 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return success;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-08-20 21:01:17 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_metainfoFree (tr_info * inf)
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2012-12-30 22:06:45 +00:00
|
|
|
unsigned int i;
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_file_index_t ff;
|
2006-12-17 16:36:27 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<inf->webseedCount; i++)
|
|
|
|
tr_free (inf->webseeds[i]);
|
2008-06-07 21:26:41 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (ff=0; ff<inf->fileCount; ff++)
|
|
|
|
tr_free (inf->files[ff].name);
|
2008-02-14 00:52:58 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (inf->webseeds);
|
|
|
|
tr_free (inf->pieces);
|
|
|
|
tr_free (inf->files);
|
|
|
|
tr_free (inf->comment);
|
|
|
|
tr_free (inf->creator);
|
|
|
|
tr_free (inf->torrent);
|
2013-01-22 00:25:42 +00:00
|
|
|
tr_free (inf->originalName);
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (inf->name);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<inf->trackerCount; i++)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (inf->trackers[i].announce);
|
|
|
|
tr_free (inf->trackers[i].scrape);
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (inf->trackers);
|
2007-07-25 17:19:29 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
memset (inf, '\0', sizeof (tr_info));
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
|
|
|
|
2007-12-25 01:23:07 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_metainfoRemoveSaved (const tr_session * session, const tr_info * inf)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char * filename;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
filename = getTorrentFilename (session, inf);
|
2014-07-08 00:08:43 +00:00
|
|
|
tr_sys_path_remove (filename, NULL);
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (filename);
|
2007-03-13 06:56:50 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|