2009-11-20 04:38:19 +00:00
|
|
|
/*
|
2014-01-18 20:56:57 +00:00
|
|
|
* This file Copyright (C) 2010-2014 Mnemosyne LLC
|
2009-11-20 04:38:19 +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-20 04:38:19 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <string.h> /* strchr() */
|
|
|
|
#include <stdio.h> /* sscanf() */
|
2009-11-20 04:38:19 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "crypto-utils.h" /* tr_hex_to_sha1() */
|
2009-11-20 04:38:19 +00:00
|
|
|
#include "magnet.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2017-05-13 22:38:31 +00:00
|
|
|
#include "utils.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant.h"
|
2009-11-20 04:38:19 +00:00
|
|
|
#include "web.h"
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
/* this base32 code converted from code by Robert Kaye and Gordon Mohr
|
2010-12-27 19:18:17 +00:00
|
|
|
* and is public domain. see http://bitzi.com/publicdomain for more info */
|
2009-11-20 04:38:19 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int const base32Lookup[] =
|
2009-11-20 04:38:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
0xFF, 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, /* '0', '1', '2', '3', '4', '5', '6', '7' */
|
|
|
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* '8', '9', ':', ';', '<', '=', '>', '?' */
|
|
|
|
0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G' */
|
|
|
|
0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, /* 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O' */
|
|
|
|
0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, /* 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W' */
|
|
|
|
0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 'X', 'Y', 'Z', '[', '\', ']', '^', '_' */
|
|
|
|
0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g' */
|
|
|
|
0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, /* 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o' */
|
|
|
|
0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, /* 'p', 'q', 'r', 's', 't', 'u', 'v', 'w' */
|
|
|
|
0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF /* 'x', 'y', 'z', '{', '|', '}', '~', 'DEL' */
|
2009-11-20 04:38:19 +00:00
|
|
|
};
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void base32_to_sha1(uint8_t* out, char const* in, size_t const inlen)
|
2009-11-20 04:38:19 +00:00
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(inlen == 32);
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const outlen = 20;
|
2009-11-20 04:38:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
memset(out, 0, 20);
|
2009-11-20 04:38:19 +00:00
|
|
|
|
2020-11-04 00:59:19 +00:00
|
|
|
size_t index = 0;
|
|
|
|
size_t offset = 0;
|
|
|
|
for (size_t i = 0; i < inlen; ++i)
|
2009-11-20 04:38:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int digit;
|
|
|
|
int lookup = in[i] - '0';
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Skip chars outside the lookup table */
|
2020-09-11 21:07:45 +00:00
|
|
|
if (lookup < 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* If this digit is not in the table, ignore it */
|
|
|
|
digit = base32Lookup[lookup];
|
2009-11-22 03:57:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (digit == 0xFF)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index <= 3)
|
|
|
|
{
|
|
|
|
index = (index + 5) % 8;
|
|
|
|
|
|
|
|
if (index == 0)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
out[offset] |= digit;
|
|
|
|
offset++;
|
|
|
|
|
|
|
|
if (offset >= outlen)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
out[offset] |= digit << (8 - index);
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
index = (index + 5) % 8;
|
2017-04-30 16:30:03 +00:00
|
|
|
out[offset] |= digit >> index;
|
2017-04-19 12:04:45 +00:00
|
|
|
offset++;
|
|
|
|
|
|
|
|
if (offset >= outlen)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
out[offset] |= digit << (8 - index);
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-20 04:38:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
#define MAX_TRACKERS 64
|
2009-11-29 17:49:58 +00:00
|
|
|
#define MAX_WEBSEEDS 64
|
2009-11-20 04:38:19 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_magnet_info* tr_magnetParse(char const* uri)
|
2009-11-20 04:38:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool got_checksum = false;
|
|
|
|
int trCount = 0;
|
|
|
|
int wsCount = 0;
|
|
|
|
char* tr[MAX_TRACKERS];
|
|
|
|
char* ws[MAX_WEBSEEDS];
|
|
|
|
char* displayName = NULL;
|
|
|
|
uint8_t sha1[SHA_DIGEST_LENGTH];
|
|
|
|
tr_magnet_info* info = NULL;
|
|
|
|
|
|
|
|
if (uri != NULL && strncmp(uri, "magnet:?", 8) == 0)
|
2009-11-20 04:38:19 +00:00
|
|
|
{
|
2019-07-13 08:52:44 +00:00
|
|
|
for (char const* walk = uri + 8; !tr_str_is_empty(walk);)
|
2009-11-20 04:38:19 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* key = walk;
|
|
|
|
char const* delim = strchr(key, '=');
|
|
|
|
char const* val = delim == NULL ? NULL : delim + 1;
|
|
|
|
char const* next = strchr(delim == NULL ? key : val, '&');
|
2017-05-01 15:46:41 +00:00
|
|
|
size_t keylen;
|
|
|
|
size_t vallen;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (delim != NULL)
|
|
|
|
{
|
|
|
|
keylen = (size_t)(delim - key);
|
|
|
|
}
|
|
|
|
else if (next != NULL)
|
|
|
|
{
|
|
|
|
keylen = (size_t)(next - key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
keylen = strlen(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val == NULL)
|
|
|
|
{
|
|
|
|
vallen = 0;
|
|
|
|
}
|
|
|
|
else if (next != NULL)
|
2009-11-20 04:38:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
vallen = (size_t)(next - val);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vallen = strlen(val);
|
|
|
|
}
|
2009-11-20 04:38:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (keylen == 2 && memcmp(key, "xt", 2) == 0 && val != NULL && strncmp(val, "urn:btih:", 9) == 0)
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* hash = val + 9;
|
|
|
|
size_t const hashlen = vallen - 9;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (hashlen == 40)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_hex_to_sha1(sha1, hash);
|
|
|
|
got_checksum = true;
|
2009-11-20 04:38:19 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (hashlen == 32)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
base32_to_sha1(sha1, hash, hashlen);
|
|
|
|
got_checksum = true;
|
2009-11-20 04:38:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 00:57:06 +00:00
|
|
|
if (displayName == NULL && vallen > 0 && keylen == 2 && memcmp(key, "dn", 2) == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
displayName = tr_http_unescape(val, vallen);
|
|
|
|
}
|
2009-11-20 04:38:19 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (vallen > 0 && trCount < MAX_TRACKERS)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (keylen == 2 && memcmp(key, "tr", 2) == 0)
|
|
|
|
{
|
|
|
|
tr[trCount++] = tr_http_unescape(val, vallen);
|
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (sscanf(key, "tr.%d=", &i) == 1 && i >= 0) /* ticket #3341 and #5134 */
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr[trCount++] = tr_http_unescape(val, vallen);
|
|
|
|
}
|
2010-06-24 18:25:21 +00:00
|
|
|
}
|
2009-11-29 17:49:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (vallen > 0 && keylen == 2 && memcmp(key, "ws", 2) == 0 && wsCount < MAX_WEBSEEDS)
|
|
|
|
{
|
|
|
|
ws[wsCount++] = tr_http_unescape(val, vallen);
|
|
|
|
}
|
2009-11-20 04:38:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
walk = next != NULL ? next + 1 : NULL;
|
2009-11-20 04:38:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (got_checksum)
|
2009-11-20 04:38:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
info = tr_new0(tr_magnet_info, 1);
|
|
|
|
info->displayName = displayName;
|
|
|
|
info->trackerCount = trCount;
|
|
|
|
info->trackers = tr_memdup(tr, sizeof(char*) * trCount);
|
|
|
|
info->webseedCount = wsCount;
|
|
|
|
info->webseeds = tr_memdup(ws, sizeof(char*) * wsCount);
|
|
|
|
memcpy(info->hash, sha1, sizeof(uint8_t) * SHA_DIGEST_LENGTH);
|
2009-11-20 04:38:19 +00:00
|
|
|
}
|
2020-05-12 00:57:06 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i = 0; i < trCount; i++)
|
|
|
|
{
|
|
|
|
tr_free(tr[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < wsCount; i++)
|
|
|
|
{
|
|
|
|
tr_free(ws[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free(displayName);
|
|
|
|
}
|
2009-11-20 04:38:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return info;
|
2009-11-20 04:38:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_magnetFree(tr_magnet_info* info)
|
2009-11-20 04:38:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (info != NULL)
|
2009-11-20 04:38:19 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < info->trackerCount; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_free(info->trackers[i]);
|
|
|
|
}
|
2009-11-29 17:49:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(info->trackers);
|
2009-11-20 04:38:19 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < info->webseedCount; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_free(info->webseeds[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free(info->webseeds);
|
|
|
|
|
|
|
|
tr_free(info->displayName);
|
|
|
|
tr_free(info);
|
2009-11-20 04:38:19 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-02 22:45:22 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_magnetCreateMetainfo(tr_magnet_info const* info, tr_variant* top)
|
2010-02-02 22:45:22 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* d;
|
|
|
|
tr_variantInitDict(top, 4);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* announce list */
|
|
|
|
if (info->trackerCount == 1)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddStr(top, TR_KEY_announce, info->trackers[0]);
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* trackers = tr_variantDictAddList(top, TR_KEY_announce_list, info->trackerCount);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < info->trackerCount; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantListAddStr(tr_variantListAddList(trackers, 1), info->trackers[i]);
|
|
|
|
}
|
2010-02-02 22:45:22 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* webseeds */
|
|
|
|
if (info->webseedCount > 0)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* urls = tr_variantDictAddList(top, TR_KEY_url_list, info->webseedCount);
|
2010-02-02 22:45:22 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < info->webseedCount; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantListAddStr(urls, info->webseeds[i]);
|
|
|
|
}
|
|
|
|
}
|
2010-02-02 22:45:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* nonstandard keys */
|
|
|
|
d = tr_variantDictAddDict(top, TR_KEY_magnet_info, 2);
|
|
|
|
tr_variantDictAddRaw(d, TR_KEY_info_hash, info->hash, 20);
|
2010-02-02 22:45:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (info->displayName != NULL)
|
|
|
|
{
|
|
|
|
tr_variantDictAddStr(d, TR_KEY_display_name, info->displayName);
|
|
|
|
}
|
|
|
|
}
|