2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-01-01 17:20:20 +00:00
|
|
|
* Copyright (c) 2005-2008 Transmission authors and contributors
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2008-02-18 05:06:52 +00:00
|
|
|
#include <ctype.h> /* isspace */
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2007-07-12 17:51:45 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h> /* unlink, stat */
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "bencode.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "crypto.h" /* tr_sha1 */
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "metainfo.h"
|
2007-07-31 14:26:44 +00:00
|
|
|
#include "platform.h"
|
2008-02-18 05:06:52 +00:00
|
|
|
#include "trcompat.h" /* strlcpy */
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* Local prototypes
|
|
|
|
**********************************************************************/
|
2008-02-26 21:58:58 +00:00
|
|
|
static int getannounce( tr_info * inf, tr_benc * meta );
|
2007-02-27 04:00:38 +00:00
|
|
|
static char * announceToScrape( const char * announce );
|
2008-02-26 21:58:58 +00:00
|
|
|
static int parseFiles( tr_info * inf, tr_benc * name,
|
|
|
|
tr_benc * files, tr_benc * length );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-20 15:17:36 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
#define WANTBYTES( want, got ) \
|
|
|
|
if( (want) > (got) ) { return; } else { (got) -= (want); }
|
|
|
|
static void
|
|
|
|
strlcat_utf8( void * dest, const void * src, size_t len, char skip )
|
|
|
|
{
|
|
|
|
char * s = dest;
|
|
|
|
const char * append = src;
|
|
|
|
const char * p;
|
|
|
|
|
|
|
|
/* don't overwrite the nul at the end */
|
|
|
|
len--;
|
|
|
|
|
|
|
|
/* Go to the end of the destination string */
|
|
|
|
while( s[0] )
|
|
|
|
{
|
|
|
|
s++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now start appending, converting on the fly if necessary */
|
|
|
|
for( p = append; p[0]; )
|
|
|
|
{
|
|
|
|
/* skip over the requested character */
|
|
|
|
if( skip == p[0] )
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !( p[0] & 0x80 ) )
|
|
|
|
{
|
|
|
|
/* ASCII character */
|
|
|
|
WANTBYTES( 1, len );
|
|
|
|
*(s++) = *(p++);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( p[0] & 0xE0 ) == 0xC0 && ( p[1] & 0xC0 ) == 0x80 )
|
|
|
|
{
|
|
|
|
/* 2-bytes UTF-8 character */
|
|
|
|
WANTBYTES( 2, len );
|
|
|
|
*(s++) = *(p++); *(s++) = *(p++);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( p[0] & 0xF0 ) == 0xE0 && ( p[1] & 0xC0 ) == 0x80 &&
|
|
|
|
( p[2] & 0xC0 ) == 0x80 )
|
|
|
|
{
|
|
|
|
/* 3-bytes UTF-8 character */
|
|
|
|
WANTBYTES( 3, len );
|
|
|
|
*(s++) = *(p++); *(s++) = *(p++);
|
|
|
|
*(s++) = *(p++);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( p[0] & 0xF8 ) == 0xF0 && ( p[1] & 0xC0 ) == 0x80 &&
|
|
|
|
( p[2] & 0xC0 ) == 0x80 && ( p[3] & 0xC0 ) == 0x80 )
|
|
|
|
{
|
|
|
|
/* 4-bytes UTF-8 character */
|
|
|
|
WANTBYTES( 4, len );
|
|
|
|
*(s++) = *(p++); *(s++) = *(p++);
|
|
|
|
*(s++) = *(p++); *(s++) = *(p++);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ISO 8859-1 -> UTF-8 conversion */
|
|
|
|
WANTBYTES( 2, len );
|
|
|
|
*(s++) = 0xC0 | ( ( *p & 0xFF ) >> 6 );
|
|
|
|
*(s++) = 0x80 | ( *(p++) & 0x3F );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-25 01:23:07 +00:00
|
|
|
static void
|
2008-04-05 20:12:11 +00:00
|
|
|
savedname( const tr_handle * handle,
|
|
|
|
char * name,
|
|
|
|
size_t len,
|
|
|
|
const char * hash )
|
2007-12-25 01:23:07 +00:00
|
|
|
{
|
2008-04-05 20:12:11 +00:00
|
|
|
const char * torDir = tr_getTorrentDir( handle );
|
2007-12-25 01:23:07 +00:00
|
|
|
|
2008-04-05 20:12:11 +00:00
|
|
|
if( !handle->tag )
|
2007-12-25 01:23:07 +00:00
|
|
|
{
|
|
|
|
tr_buildPath( name, len, torDir, hash, NULL );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char base[1024];
|
2008-04-05 20:12:11 +00:00
|
|
|
snprintf( base, sizeof(base), "%s-%s", hash, handle->tag );
|
2007-12-25 01:23:07 +00:00
|
|
|
tr_buildPath( name, len, torDir, base, NULL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-02 20:38:23 +00:00
|
|
|
int
|
2008-04-05 20:12:11 +00:00
|
|
|
tr_metainfoParse( const tr_handle * handle,
|
|
|
|
tr_info * inf,
|
|
|
|
const tr_benc * meta_in )
|
2007-12-21 22:18:40 +00:00
|
|
|
{
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_piece_index_t i;
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc * beInfo, * val, * val2;
|
|
|
|
tr_benc * meta = (tr_benc *) meta_in;
|
2008-02-15 14:56:11 +00:00
|
|
|
char buf[4096];
|
2007-12-21 22:18:40 +00:00
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
if(( beInfo = tr_bencDictFindType( meta, "info", TYPE_DICT )))
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char * str = tr_bencSave( beInfo, &len );
|
|
|
|
tr_sha1( inf->hash, str, len, NULL );
|
|
|
|
tr_free( str );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-03-07 20:48:36 +00:00
|
|
|
tr_err( _( "Missing metadata entry \"%s\"" ), "info" );
|
2007-12-21 22:18:40 +00:00
|
|
|
return TR_EINVALID;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-02-25 20:21:22 +00:00
|
|
|
tr_sha1_to_hex( inf->hashString, inf->hash );
|
2008-04-05 20:12:11 +00:00
|
|
|
savedname( handle, buf, sizeof( buf ), inf->hashString );
|
2008-03-23 03:15:05 +00:00
|
|
|
tr_free( inf->torrent );
|
|
|
|
inf->torrent = tr_strdup( buf );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-02-15 14:56:11 +00:00
|
|
|
/* comment */
|
|
|
|
memset( buf, '\0', sizeof( buf ) );
|
2007-12-21 22:18:40 +00:00
|
|
|
val = tr_bencDictFindFirst( meta, "comment.utf-8", "comment", NULL );
|
2008-04-04 17:19:44 +00:00
|
|
|
if( tr_bencIsString( val ) )
|
2008-02-14 01:12:00 +00:00
|
|
|
strlcat_utf8( buf, val->val.s.s, sizeof( buf ), 0 );
|
2008-02-15 14:56:11 +00:00
|
|
|
tr_free( inf->comment );
|
|
|
|
inf->comment = tr_strdup( buf );
|
2006-11-23 01:38:18 +00:00
|
|
|
|
2008-02-15 14:56:11 +00:00
|
|
|
/* creator */
|
|
|
|
memset( buf, '\0', sizeof( buf ) );
|
2007-12-21 22:18:40 +00:00
|
|
|
val = tr_bencDictFindFirst( meta, "created by.utf-8", "created by", NULL );
|
2008-04-04 17:19:44 +00:00
|
|
|
if( tr_bencIsString( val ) )
|
2008-02-14 01:12:00 +00:00
|
|
|
strlcat_utf8( buf, val->val.s.s, sizeof( buf ), 0 );
|
2008-02-15 14:56:11 +00:00
|
|
|
tr_free( inf->creator );
|
|
|
|
inf->creator = tr_strdup( buf );
|
2006-11-23 01:38:18 +00:00
|
|
|
|
|
|
|
/* Date created */
|
2007-03-05 00:07:48 +00:00
|
|
|
inf->dateCreated = 0;
|
2007-12-21 22:18:40 +00:00
|
|
|
val = tr_bencDictFind( meta, "creation date" );
|
2008-04-04 17:19:44 +00:00
|
|
|
if( tr_bencIsInt( val ) )
|
2006-11-23 01:38:18 +00:00
|
|
|
inf->dateCreated = val->val.i;
|
2006-12-17 16:36:27 +00:00
|
|
|
|
|
|
|
/* Private torrent */
|
2007-03-25 15:37:08 +00:00
|
|
|
val = tr_bencDictFind( beInfo, "private" );
|
2007-12-21 22:18:40 +00:00
|
|
|
val2 = tr_bencDictFind( meta, "private" );
|
2008-04-04 17:19:44 +00:00
|
|
|
if( ( tr_bencIsInt(val) && val->val.i ) ||
|
|
|
|
( tr_bencIsInt(val2) && val2->val.i ) )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-10-15 20:58:39 +00:00
|
|
|
inf->isPrivate = 1;
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
/* Piece length */
|
2007-03-05 00:07:48 +00:00
|
|
|
val = tr_bencDictFind( beInfo, "piece length" );
|
2008-04-04 17:19:44 +00:00
|
|
|
if( !tr_bencIsInt( val ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-03-04 02:02:25 +00:00
|
|
|
if( val )
|
2008-03-07 03:26:59 +00:00
|
|
|
tr_err( _( "Invalid metadata entry \"%s\"" ), "piece length" );
|
2008-03-04 02:02:25 +00:00
|
|
|
else
|
2008-03-07 03:26:59 +00:00
|
|
|
tr_err( _( "Missing metadata entry \"%s\"" ), "piece length" );
|
2007-03-05 00:07:48 +00:00
|
|
|
goto fail;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
inf->pieceSize = val->val.i;
|
|
|
|
|
|
|
|
/* Hashes */
|
|
|
|
val = tr_bencDictFind( beInfo, "pieces" );
|
2008-04-04 17:19:44 +00:00
|
|
|
if( !tr_bencIsString( val ) )
|
2007-03-05 00:07:48 +00:00
|
|
|
{
|
2008-03-04 02:02:25 +00:00
|
|
|
if( val )
|
2008-03-07 03:26:59 +00:00
|
|
|
tr_err( _( "Invalid metadata entry \"%s\"" ), "pieces" );
|
2008-03-04 02:02:25 +00:00
|
|
|
else
|
2008-03-07 03:26:59 +00:00
|
|
|
tr_err( _( "Missing metadata entry \"%s\"" ), "pieces" );
|
2007-03-05 00:07:48 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
if( val->val.s.i % SHA_DIGEST_LENGTH )
|
|
|
|
{
|
2008-03-07 03:26:59 +00:00
|
|
|
tr_err( _( "Invalid metadata entry \"%s\"" ), "pieces" );
|
2007-03-05 00:07:48 +00:00
|
|
|
goto fail;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
inf->pieceCount = val->val.s.i / SHA_DIGEST_LENGTH;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
inf->pieces = calloc ( inf->pieceCount, sizeof(tr_piece) );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
for ( i=0; i<inf->pieceCount; ++i )
|
|
|
|
{
|
|
|
|
memcpy (inf->pieces[i].hash, &val->val.s.s[i*SHA_DIGEST_LENGTH], SHA_DIGEST_LENGTH);
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-03-05 00:07:48 +00:00
|
|
|
/* get file or top directory name */
|
|
|
|
val = tr_bencDictFindFirst( beInfo, "name.utf-8", "name", NULL );
|
2007-03-30 00:12:39 +00:00
|
|
|
if( parseFiles( inf, tr_bencDictFindFirst( beInfo,
|
|
|
|
"name.utf-8", "name", NULL ),
|
|
|
|
tr_bencDictFind( beInfo, "files" ),
|
|
|
|
tr_bencDictFind( beInfo, "length" ) ) )
|
2007-03-05 00:07:48 +00:00
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-03-07 20:48:36 +00:00
|
|
|
if( !inf->fileCount || !inf->totalSize )
|
2007-07-27 01:50:26 +00:00
|
|
|
{
|
2008-03-19 20:07:27 +00:00
|
|
|
tr_err( _( "Torrent is corrupt" ) ); /* the content is missing! */
|
2007-07-27 01:58:49 +00:00
|
|
|
goto fail;
|
2007-07-27 01:50:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO add more tests so we don't crash on weird files */
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
if( (uint64_t) inf->pieceCount !=
|
|
|
|
( inf->totalSize + inf->pieceSize - 1 ) / inf->pieceSize )
|
|
|
|
{
|
2008-03-07 20:48:36 +00:00
|
|
|
tr_err( _( "Torrent is corrupt" ) ); /* size of hashes and files don't match */
|
2007-03-05 00:07:48 +00:00
|
|
|
goto fail;
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get announce or announce-list */
|
2007-12-21 22:18:40 +00:00
|
|
|
if( getannounce( inf, meta ) )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-03-05 00:07:48 +00:00
|
|
|
goto fail;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_OK;
|
2007-03-05 00:07:48 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
tr_metainfoFree( inf );
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void tr_metainfoFree( tr_info * inf )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_file_index_t ff;
|
2007-08-14 15:24:48 +00:00
|
|
|
int i, j;
|
2006-12-17 16:36:27 +00:00
|
|
|
|
2008-03-22 18:10:59 +00:00
|
|
|
for( ff=0; ff<inf->fileCount; ++ff )
|
|
|
|
tr_free( inf->files[ff].name );
|
2008-02-14 00:52:58 +00:00
|
|
|
|
2007-06-28 02:02:30 +00:00
|
|
|
tr_free( inf->pieces );
|
|
|
|
tr_free( inf->files );
|
2008-02-14 01:12:00 +00:00
|
|
|
tr_free( inf->comment );
|
|
|
|
tr_free( inf->creator );
|
2008-03-23 03:15:05 +00:00
|
|
|
tr_free( inf->torrent );
|
|
|
|
tr_free( inf->name );
|
2007-08-18 22:35:09 +00:00
|
|
|
tr_free( inf->primaryAddress );
|
2006-12-17 16:36:27 +00:00
|
|
|
|
2007-08-14 15:24:48 +00:00
|
|
|
for( i=0; i<inf->trackerTiers; ++i ) {
|
|
|
|
for( j=0; j<inf->trackerList[i].count; ++j )
|
|
|
|
tr_trackerInfoClear( &inf->trackerList[i].list[j] );
|
|
|
|
tr_free( inf->trackerList[i].list );
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
2007-06-28 02:02:30 +00:00
|
|
|
tr_free( inf->trackerList );
|
2007-07-25 17:19:29 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
memset( inf, '\0', sizeof(tr_info) );
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
|
|
|
|
2008-02-26 21:58:58 +00:00
|
|
|
static int
|
|
|
|
getfile( char ** setme, const char * prefix, tr_benc * name )
|
2007-03-05 00:07:48 +00:00
|
|
|
{
|
2007-03-30 00:12:39 +00:00
|
|
|
const char ** list;
|
|
|
|
int ii, jj;
|
2008-02-14 00:52:58 +00:00
|
|
|
char buf[4096];
|
2007-03-05 00:07:48 +00:00
|
|
|
|
2008-04-04 17:19:44 +00:00
|
|
|
if( !tr_bencIsList( name ) )
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2007-03-05 00:07:48 +00:00
|
|
|
|
|
|
|
list = calloc( name->val.l.count, sizeof( list[0] ) );
|
2008-04-04 17:19:44 +00:00
|
|
|
if( !list )
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2007-03-05 00:07:48 +00:00
|
|
|
|
2007-03-31 17:40:28 +00:00
|
|
|
for( ii = jj = 0; name->val.l.count > ii; ii++ )
|
2007-03-05 00:07:48 +00:00
|
|
|
{
|
2008-04-04 17:19:44 +00:00
|
|
|
tr_benc * dir = &name->val.l.vals[ii];
|
|
|
|
|
|
|
|
if( !tr_bencIsString( dir ) )
|
2007-03-05 00:07:48 +00:00
|
|
|
continue;
|
2008-04-04 17:19:44 +00:00
|
|
|
|
2007-03-05 00:07:48 +00:00
|
|
|
if( 0 == strcmp( "..", dir->val.s.s ) )
|
|
|
|
{
|
|
|
|
if( 0 < jj )
|
|
|
|
{
|
|
|
|
jj--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( 0 != strcmp( ".", dir->val.s.s ) )
|
|
|
|
{
|
|
|
|
list[jj] = dir->val.s.s;
|
|
|
|
jj++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( 0 == jj )
|
|
|
|
{
|
2007-07-15 17:36:56 +00:00
|
|
|
free( list );
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2007-03-05 00:07:48 +00:00
|
|
|
}
|
|
|
|
|
2008-02-14 00:52:58 +00:00
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
|
|
strlcat_utf8( buf, prefix, sizeof(buf), 0 );
|
2007-03-05 00:07:48 +00:00
|
|
|
for( ii = 0; jj > ii; ii++ )
|
|
|
|
{
|
2008-02-14 00:52:58 +00:00
|
|
|
strlcat_utf8( buf, TR_PATH_DELIMITER_STR, sizeof(buf), 0 );
|
|
|
|
strlcat_utf8( buf, list[ii], sizeof(buf), TR_PATH_DELIMITER );
|
2007-03-05 00:07:48 +00:00
|
|
|
}
|
|
|
|
free( list );
|
|
|
|
|
2008-02-14 00:52:58 +00:00
|
|
|
tr_free( *setme );
|
|
|
|
*setme = tr_strdup( buf );
|
|
|
|
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_OK;
|
2007-03-05 00:07:48 +00:00
|
|
|
}
|
|
|
|
|
2008-02-26 21:58:58 +00:00
|
|
|
static int getannounce( tr_info * inf, tr_benc * meta )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2008-04-04 17:19:44 +00:00
|
|
|
tr_benc * val, * urlval;
|
2007-03-30 00:12:39 +00:00
|
|
|
char * address, * announce;
|
2008-04-04 17:19:44 +00:00
|
|
|
int ii, jj, port, random;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_tracker_info * sublist;
|
2006-12-17 16:36:27 +00:00
|
|
|
void * swapping;
|
|
|
|
|
|
|
|
/* Announce-list */
|
|
|
|
val = tr_bencDictFind( meta, "announce-list" );
|
2008-04-04 17:19:44 +00:00
|
|
|
if( tr_bencIsList(val) && 0 < val->val.l.count )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
|
|
|
inf->trackerTiers = 0;
|
2007-06-18 03:40:41 +00:00
|
|
|
inf->trackerList = calloc( val->val.l.count,
|
|
|
|
sizeof( inf->trackerList[0] ) );
|
2006-12-17 16:36:27 +00:00
|
|
|
|
|
|
|
/* iterate through the announce-list's tiers */
|
2007-03-31 17:40:28 +00:00
|
|
|
for( ii = 0; ii < val->val.l.count; ii++ )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2008-04-04 17:19:44 +00:00
|
|
|
int subcount = 0;
|
|
|
|
tr_benc * subval = &val->val.l.vals[ii];
|
|
|
|
|
|
|
|
if( !tr_bencIsList(subval) || 0 >= subval->val.l.count )
|
2006-12-17 16:36:27 +00:00
|
|
|
continue;
|
2008-04-04 17:19:44 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
sublist = calloc( subval->val.l.count, sizeof( sublist[0] ) );
|
2006-12-17 16:36:27 +00:00
|
|
|
|
|
|
|
/* iterate through the tier's items */
|
2007-03-31 17:40:28 +00:00
|
|
|
for( jj = 0; jj < subval->val.l.count; jj++ )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_tracker_info tmp;
|
2007-08-14 15:24:48 +00:00
|
|
|
|
2007-03-31 17:40:28 +00:00
|
|
|
urlval = &subval->val.l.vals[jj];
|
2006-12-17 16:36:27 +00:00
|
|
|
if( TYPE_STR != urlval->type ||
|
2007-08-14 15:24:48 +00:00
|
|
|
tr_trackerInfoInit( &tmp, urlval->val.s.s, urlval->val.s.i ) )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
if( !inf->primaryAddress ) {
|
|
|
|
char buf[1024];
|
|
|
|
snprintf( buf, sizeof(buf), "%s:%d", tmp.address, tmp.port );
|
|
|
|
inf->primaryAddress = tr_strdup( buf );
|
|
|
|
}
|
|
|
|
|
2006-12-17 16:36:27 +00:00
|
|
|
/* place the item info in a random location in the sublist */
|
|
|
|
random = tr_rand( subcount + 1 );
|
|
|
|
if( random != subcount )
|
|
|
|
sublist[subcount] = sublist[random];
|
2007-08-14 15:24:48 +00:00
|
|
|
sublist[random] = tmp;
|
2006-12-17 16:36:27 +00:00
|
|
|
subcount++;
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:12:39 +00:00
|
|
|
/* just use sublist as-is if it's full */
|
2006-12-17 16:36:27 +00:00
|
|
|
if( subcount == subval->val.l.count )
|
|
|
|
{
|
|
|
|
inf->trackerList[inf->trackerTiers].list = sublist;
|
|
|
|
inf->trackerList[inf->trackerTiers].count = subcount;
|
|
|
|
inf->trackerTiers++;
|
|
|
|
}
|
|
|
|
/* if we skipped some of the tier's items then trim the sublist */
|
|
|
|
else if( 0 < subcount )
|
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
inf->trackerList[inf->trackerTiers].list = calloc( subcount, sizeof( sublist[0] ) );
|
2006-12-17 16:36:27 +00:00
|
|
|
memcpy( inf->trackerList[inf->trackerTiers].list, sublist,
|
|
|
|
sizeof( sublist[0] ) * subcount );
|
|
|
|
inf->trackerList[inf->trackerTiers].count = subcount;
|
|
|
|
inf->trackerTiers++;
|
|
|
|
free( sublist );
|
|
|
|
}
|
|
|
|
/* drop the whole sublist if we didn't use any items at all */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
free( sublist );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* did we use any of the tiers? */
|
|
|
|
if( 0 == inf->trackerTiers )
|
|
|
|
{
|
2008-03-07 20:48:36 +00:00
|
|
|
tr_inf( _( "Invalid metadata entry \"%s\"" ), "announce-list" );
|
2006-12-17 16:36:27 +00:00
|
|
|
free( inf->trackerList );
|
|
|
|
inf->trackerList = NULL;
|
|
|
|
}
|
|
|
|
/* trim unused sublist pointers */
|
|
|
|
else if( inf->trackerTiers < val->val.l.count )
|
|
|
|
{
|
|
|
|
swapping = inf->trackerList;
|
2007-06-18 03:40:41 +00:00
|
|
|
inf->trackerList = calloc( inf->trackerTiers,
|
|
|
|
sizeof( inf->trackerList[0] ) );
|
2006-12-17 16:36:27 +00:00
|
|
|
memcpy( inf->trackerList, swapping,
|
|
|
|
sizeof( inf->trackerList[0] ) * inf->trackerTiers );
|
|
|
|
free( swapping );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Regular announce value */
|
2007-08-14 15:24:48 +00:00
|
|
|
val = tr_bencDictFind( meta, "announce" );
|
2008-04-04 17:19:44 +00:00
|
|
|
if( !tr_bencIsString( val ) )
|
2007-08-14 15:24:48 +00:00
|
|
|
{
|
2008-03-07 03:26:59 +00:00
|
|
|
tr_err( _( "Missing metadata entry \"%s\"" ), "announce" );
|
2007-08-14 15:24:48 +00:00
|
|
|
return TR_EINVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !inf->trackerTiers )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2008-02-18 05:06:52 +00:00
|
|
|
char buf[4096], *pch;
|
|
|
|
strlcpy( buf, val->val.s.s, sizeof( buf ) );
|
|
|
|
pch = buf;
|
|
|
|
while( isspace( *pch ) )
|
|
|
|
++pch;
|
2006-12-17 16:36:27 +00:00
|
|
|
|
2008-03-24 19:29:25 +00:00
|
|
|
if( tr_httpParseURL( pch, -1, &address, &port, &announce ) )
|
2008-03-02 19:55:04 +00:00
|
|
|
{
|
2008-03-04 02:02:25 +00:00
|
|
|
tr_err( _( "Invalid announce URL \"%s\"" ), val->val.s.s );
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2008-03-02 19:55:04 +00:00
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
sublist = calloc( 1, sizeof( sublist[0] ) );
|
2006-12-17 16:36:27 +00:00
|
|
|
sublist[0].address = address;
|
|
|
|
sublist[0].port = port;
|
|
|
|
sublist[0].announce = announce;
|
2007-02-27 04:00:38 +00:00
|
|
|
sublist[0].scrape = announceToScrape( announce );
|
2007-06-18 03:40:41 +00:00
|
|
|
inf->trackerList = calloc( 1, sizeof( inf->trackerList[0] ) );
|
2006-12-17 16:36:27 +00:00
|
|
|
inf->trackerList[0].list = sublist;
|
|
|
|
inf->trackerList[0].count = 1;
|
|
|
|
inf->trackerTiers = 1;
|
2007-08-16 20:00:06 +00:00
|
|
|
|
|
|
|
if( !inf->primaryAddress ) {
|
|
|
|
char buf[1024];
|
|
|
|
snprintf( buf, sizeof(buf), "%s:%d", sublist[0].address, sublist[0].port );
|
|
|
|
inf->primaryAddress = tr_strdup( buf );
|
|
|
|
}
|
|
|
|
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
|
|
|
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_OK;
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
|
|
|
|
2007-02-27 04:00:38 +00:00
|
|
|
static char * announceToScrape( const char * announce )
|
2007-08-14 15:24:48 +00:00
|
|
|
{
|
2007-02-27 04:00:38 +00:00
|
|
|
char old[] = "announce";
|
|
|
|
int oldlen = 8;
|
|
|
|
char new[] = "scrape";
|
|
|
|
int newlen = 6;
|
|
|
|
char * slash, * scrape;
|
2007-03-12 00:04:11 +00:00
|
|
|
size_t scrapelen, used;
|
|
|
|
|
2007-02-27 04:00:38 +00:00
|
|
|
slash = strrchr( announce, '/' );
|
|
|
|
if( NULL == slash )
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
slash++;
|
|
|
|
|
|
|
|
if( 0 != strncmp( slash, old, oldlen ) )
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-03-12 00:04:11 +00:00
|
|
|
scrapelen = strlen( announce ) - oldlen + newlen;
|
|
|
|
scrape = calloc( scrapelen + 1, 1 );
|
|
|
|
if( NULL == scrape )
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
assert( ( size_t )( slash - announce ) < scrapelen );
|
|
|
|
memcpy( scrape, announce, slash - announce );
|
|
|
|
used = slash - announce;
|
|
|
|
strncat( scrape, new, scrapelen - used );
|
|
|
|
used += newlen;
|
|
|
|
assert( strlen( scrape ) == used );
|
|
|
|
if( used < scrapelen )
|
|
|
|
{
|
|
|
|
assert( strlen( slash + oldlen ) == scrapelen - used );
|
|
|
|
strncat( scrape, slash + oldlen, scrapelen - used );
|
|
|
|
}
|
2007-02-27 04:00:38 +00:00
|
|
|
|
|
|
|
return scrape;
|
|
|
|
}
|
|
|
|
|
2007-08-14 15:24:48 +00:00
|
|
|
int
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_trackerInfoInit( tr_tracker_info * info,
|
|
|
|
const char * address,
|
|
|
|
int address_len )
|
2007-08-14 15:24:48 +00:00
|
|
|
{
|
2008-03-24 19:29:25 +00:00
|
|
|
int ret = tr_httpParseURL( address, address_len,
|
2007-08-14 15:24:48 +00:00
|
|
|
&info->address,
|
|
|
|
&info->port,
|
|
|
|
&info->announce );
|
|
|
|
if( !ret )
|
|
|
|
info->scrape = announceToScrape( info->announce );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_trackerInfoClear( tr_tracker_info * info )
|
2007-08-14 15:24:48 +00:00
|
|
|
{
|
|
|
|
tr_free( info->address );
|
|
|
|
tr_free( info->announce );
|
|
|
|
tr_free( info->scrape );
|
2007-09-20 16:32:01 +00:00
|
|
|
memset( info, '\0', sizeof(tr_tracker_info) );
|
2007-08-14 15:24:48 +00:00
|
|
|
}
|
|
|
|
|
2007-12-25 01:23:07 +00:00
|
|
|
void
|
2008-04-05 20:12:11 +00:00
|
|
|
tr_metainfoRemoveSaved( const tr_handle * handle,
|
|
|
|
const char * hashString )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
char file[MAX_PATH_LENGTH];
|
2008-04-05 20:12:11 +00:00
|
|
|
savedname( handle, file, sizeof file, hashString );
|
2007-04-28 01:34:39 +00:00
|
|
|
unlink( file );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-03-13 06:56:50 +00:00
|
|
|
/* Save a copy of the torrent file in the saved torrent directory */
|
2007-12-22 18:00:47 +00:00
|
|
|
int
|
2008-04-05 20:12:11 +00:00
|
|
|
tr_metainfoSave( const tr_handle * handle,
|
|
|
|
const char * hash,
|
|
|
|
const uint8_t * buf,
|
|
|
|
size_t buflen )
|
2007-03-13 06:56:50 +00:00
|
|
|
{
|
|
|
|
char path[MAX_PATH_LENGTH];
|
|
|
|
FILE * file;
|
|
|
|
|
2008-04-05 20:12:11 +00:00
|
|
|
savedname( handle, path, sizeof path, hash );
|
2007-10-16 02:19:40 +00:00
|
|
|
file = fopen( path, "wb+" );
|
2007-03-13 06:56:50 +00:00
|
|
|
if( !file )
|
|
|
|
{
|
2008-03-30 13:52:55 +00:00
|
|
|
tr_err( _( "Couldn't open \"%1$s\": %2$s" ), path, tr_strerror( errno ) );
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2007-03-13 06:56:50 +00:00
|
|
|
}
|
|
|
|
fseek( file, 0, SEEK_SET );
|
|
|
|
if( fwrite( buf, 1, buflen, file ) != buflen )
|
|
|
|
{
|
2008-03-30 13:52:55 +00:00
|
|
|
tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), path, tr_strerror( errno ) );
|
2007-03-13 06:56:50 +00:00
|
|
|
fclose( file );
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2007-03-13 06:56:50 +00:00
|
|
|
}
|
|
|
|
fclose( file );
|
|
|
|
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_OK;
|
2007-03-13 06:56:50 +00:00
|
|
|
}
|
|
|
|
|
2007-07-01 02:54:37 +00:00
|
|
|
static int
|
2008-02-26 21:58:58 +00:00
|
|
|
parseFiles( tr_info * inf, tr_benc * name,
|
|
|
|
tr_benc * files, tr_benc * length )
|
2007-03-30 00:12:39 +00:00
|
|
|
{
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc * item, * path;
|
2007-03-30 00:12:39 +00:00
|
|
|
int ii;
|
|
|
|
|
2008-04-04 17:19:44 +00:00
|
|
|
if( !tr_bencIsString( name ) )
|
2007-03-30 00:12:39 +00:00
|
|
|
{
|
2008-03-04 02:02:25 +00:00
|
|
|
if( name )
|
2008-03-07 03:26:59 +00:00
|
|
|
tr_err( _( "Invalid metadata entry \"%s\"" ), "name" );
|
2008-03-04 02:02:25 +00:00
|
|
|
else
|
2008-03-07 03:26:59 +00:00
|
|
|
tr_err( _( "Missing metadata entry \"%s\"" ), "name" );
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2007-03-30 00:12:39 +00:00
|
|
|
}
|
|
|
|
|
2008-03-23 03:15:05 +00:00
|
|
|
tr_free( inf->name );
|
|
|
|
inf->name = tr_strdup( name->val.s.s );
|
|
|
|
if( !inf->name || !*inf->name )
|
2007-03-30 00:12:39 +00:00
|
|
|
{
|
2008-03-07 03:26:59 +00:00
|
|
|
tr_err( _( "Invalid metadata entry \"%s\"" ), "name" );
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2007-03-30 00:12:39 +00:00
|
|
|
}
|
|
|
|
inf->totalSize = 0;
|
|
|
|
|
2008-04-04 17:19:44 +00:00
|
|
|
if( tr_bencIsList( files ) )
|
2007-03-30 00:12:39 +00:00
|
|
|
{
|
|
|
|
/* Multi-file mode */
|
2007-10-15 20:58:39 +00:00
|
|
|
inf->isMultifile = 1;
|
2007-03-30 00:12:39 +00:00
|
|
|
inf->fileCount = files->val.l.count;
|
|
|
|
inf->files = calloc( inf->fileCount, sizeof( inf->files[0] ) );
|
|
|
|
|
2008-02-14 00:52:58 +00:00
|
|
|
if( !inf->files )
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2007-03-30 00:12:39 +00:00
|
|
|
|
2007-03-31 17:40:28 +00:00
|
|
|
for( ii = 0; files->val.l.count > ii; ii++ )
|
2007-03-30 00:12:39 +00:00
|
|
|
{
|
2007-03-31 17:40:28 +00:00
|
|
|
item = &files->val.l.vals[ii];
|
2007-03-30 00:12:39 +00:00
|
|
|
path = tr_bencDictFindFirst( item, "path.utf-8", "path", NULL );
|
2008-02-14 00:52:58 +00:00
|
|
|
if( getfile( &inf->files[ii].name, inf->name, path ) )
|
2007-03-30 00:12:39 +00:00
|
|
|
{
|
2008-03-04 02:02:25 +00:00
|
|
|
if( path )
|
2008-03-07 03:26:59 +00:00
|
|
|
tr_err( _( "Invalid metadata entry \"%s\"" ), "path" );
|
2008-03-04 02:02:25 +00:00
|
|
|
else
|
2008-03-07 03:26:59 +00:00
|
|
|
tr_err( _( "Missing metadata entry \"%s\"" ), "path" );
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2007-03-30 00:12:39 +00:00
|
|
|
}
|
|
|
|
length = tr_bencDictFind( item, "length" );
|
2008-04-04 17:19:44 +00:00
|
|
|
if( !tr_bencIsInt( length ) )
|
2007-03-30 00:12:39 +00:00
|
|
|
{
|
2008-03-04 02:02:25 +00:00
|
|
|
if( length )
|
2008-03-07 03:26:59 +00:00
|
|
|
tr_err( _( "Invalid metadata entry \"%s\"" ), "length" );
|
2008-03-04 02:02:25 +00:00
|
|
|
else
|
2008-03-07 03:26:59 +00:00
|
|
|
tr_err( _( "Missing metadata entry \"%s\"" ), "length" );
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2007-03-30 00:12:39 +00:00
|
|
|
}
|
2007-03-31 19:10:32 +00:00
|
|
|
inf->files[ii].length = length->val.i;
|
2007-03-30 00:12:39 +00:00
|
|
|
inf->totalSize += length->val.i;
|
|
|
|
}
|
|
|
|
}
|
2008-04-04 17:19:44 +00:00
|
|
|
else if( tr_bencIsInt( length ) )
|
2007-03-30 00:12:39 +00:00
|
|
|
{
|
2008-02-14 00:52:58 +00:00
|
|
|
char buf[4096];
|
|
|
|
|
2007-03-30 00:12:39 +00:00
|
|
|
/* Single-file mode */
|
2007-10-15 20:58:39 +00:00
|
|
|
inf->isMultifile = 0;
|
2007-03-30 00:12:39 +00:00
|
|
|
inf->fileCount = 1;
|
|
|
|
inf->files = calloc( 1, sizeof( inf->files[0] ) );
|
|
|
|
|
2008-02-14 00:52:58 +00:00
|
|
|
if( !inf->files )
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2007-03-30 00:12:39 +00:00
|
|
|
|
2008-02-14 00:52:58 +00:00
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
|
|
strlcat_utf8( buf, name->val.s.s, sizeof(buf), TR_PATH_DELIMITER );
|
|
|
|
tr_free( inf->files[0].name );
|
|
|
|
inf->files[0].name = tr_strdup( buf );
|
2007-03-30 00:12:39 +00:00
|
|
|
|
|
|
|
inf->files[0].length = length->val.i;
|
|
|
|
inf->totalSize += length->val.i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-03-07 20:48:36 +00:00
|
|
|
tr_err( _( "Invalid or missing metadata entries \"length\" and \"files\"" ) );
|
2007-03-30 00:12:39 +00:00
|
|
|
}
|
|
|
|
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_OK;
|
2007-03-30 00:12:39 +00:00
|
|
|
}
|