2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2007-03-23 08:28:01 +00:00
|
|
|
* Copyright (c) 2005-2007 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-07-29 18:11:21 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#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-12 17:51:45 +00:00
|
|
|
#include "http.h" /* tr_httpParseUrl */
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "metainfo.h"
|
2007-07-31 14:26:44 +00:00
|
|
|
#include "platform.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
#define TORRENT_MAX_SIZE (5*1024*1024)
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* Local prototypes
|
|
|
|
**********************************************************************/
|
2007-09-20 16:32:01 +00:00
|
|
|
static int realparse( tr_info * inf, const uint8_t * buf, size_t len );
|
2007-03-13 06:56:50 +00:00
|
|
|
static void savedname( char * name, size_t len, const char * hash,
|
|
|
|
const char * tag );
|
2007-04-02 20:49:08 +00:00
|
|
|
static uint8_t * readtorrent( const char * path, size_t * len );
|
2007-03-13 06:56:50 +00:00
|
|
|
static int savetorrent( const char * hash, const char * tag,
|
|
|
|
const uint8_t * buf, size_t buflen );
|
2007-03-05 00:07:48 +00:00
|
|
|
static int getfile( char * buf, int size,
|
2007-03-30 00:12:39 +00:00
|
|
|
const char * prefix, benc_val_t * name );
|
2007-09-20 16:32:01 +00:00
|
|
|
static int getannounce( tr_info * inf, benc_val_t * meta );
|
2007-02-27 04:00:38 +00:00
|
|
|
static char * announceToScrape( const char * announce );
|
2007-09-20 16:32:01 +00:00
|
|
|
static int parseFiles( tr_info * inf, benc_val_t * name,
|
2007-03-30 00:12:39 +00:00
|
|
|
benc_val_t * files, benc_val_t * length );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* tr_metainfoParse
|
|
|
|
***********************************************************************
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
2007-04-02 20:38:23 +00:00
|
|
|
int
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_metainfoParseFile( tr_info * inf, const char * tag,
|
2007-04-02 20:38:23 +00:00
|
|
|
const char * path, int save )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-04-02 20:38:23 +00:00
|
|
|
uint8_t * buf;
|
|
|
|
size_t size;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-03-13 06:56:50 +00:00
|
|
|
/* read the torrent data */
|
2007-04-02 20:38:23 +00:00
|
|
|
buf = readtorrent( path, &size );
|
2007-03-13 06:56:50 +00:00
|
|
|
if( NULL == buf )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-04-02 20:38:23 +00:00
|
|
|
if( realparse( inf, buf, size ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
free( buf );
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-04-02 20:38:23 +00:00
|
|
|
if( save )
|
|
|
|
{
|
|
|
|
if( savetorrent( inf->hashString, tag, buf, size ) )
|
|
|
|
{
|
|
|
|
free( buf );
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2007-04-02 20:38:23 +00:00
|
|
|
}
|
|
|
|
savedname( inf->torrent, sizeof inf->torrent, inf->hashString, tag );
|
|
|
|
}
|
2007-04-08 21:25:01 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
snprintf( inf->torrent, sizeof inf->torrent, "%s", path );
|
|
|
|
}
|
2007-04-02 20:38:23 +00:00
|
|
|
|
|
|
|
free( buf );
|
|
|
|
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_OK;
|
2007-04-02 20:38:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_metainfoParseData( tr_info * inf, const char * tag,
|
2007-06-27 14:54:31 +00:00
|
|
|
const uint8_t * data, size_t size, int save )
|
2007-04-02 20:38:23 +00:00
|
|
|
{
|
|
|
|
if( realparse( inf, data, size ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-04-02 20:38:23 +00:00
|
|
|
|
|
|
|
if( save )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-04-02 20:38:23 +00:00
|
|
|
if( savetorrent( inf->hashString, tag, data, size ) )
|
|
|
|
{
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2007-04-02 20:38:23 +00:00
|
|
|
}
|
|
|
|
savedname( inf->torrent, sizeof inf->torrent, inf->hashString, tag );
|
|
|
|
}
|
|
|
|
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_OK;
|
2007-04-02 20:38:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_metainfoParseHash( tr_info * inf, const char * tag, const char * hash )
|
2007-04-02 20:38:23 +00:00
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
uint8_t * buf;
|
|
|
|
size_t size;
|
|
|
|
int save;
|
|
|
|
|
|
|
|
/* check it we should use an old file without a tag */
|
|
|
|
/* XXX this should go away at some point */
|
|
|
|
save = 0;
|
|
|
|
savedname( inf->torrent, sizeof inf->torrent, hash, tag );
|
|
|
|
if( 0 > stat( inf->torrent, &sb ) && ENOENT == errno )
|
|
|
|
{
|
|
|
|
savedname( inf->torrent, sizeof inf->torrent, hash, NULL );
|
|
|
|
if( 0 == stat( inf->torrent, &sb ))
|
|
|
|
{
|
|
|
|
save = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = readtorrent( inf->torrent, &size );
|
|
|
|
if( NULL == buf )
|
|
|
|
{
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-04-02 20:38:23 +00:00
|
|
|
if( realparse( inf, buf, size ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-04-02 20:38:23 +00:00
|
|
|
free( buf );
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2007-04-02 20:38:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* save a new tagged copy of the old untagged torrent */
|
|
|
|
if( save )
|
|
|
|
{
|
|
|
|
if( savetorrent( hash, tag, buf, size ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
free( buf );
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-04-02 20:38:23 +00:00
|
|
|
savedname( inf->torrent, sizeof inf->torrent, hash, tag );
|
2007-03-13 06:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free( buf );
|
|
|
|
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_OK;
|
2007-04-02 20:38:23 +00:00
|
|
|
}
|
|
|
|
|
2007-07-01 02:54:37 +00:00
|
|
|
static int
|
2007-09-20 16:32:01 +00:00
|
|
|
realparse( tr_info * inf, const uint8_t * buf, size_t size )
|
2007-04-02 20:38:23 +00:00
|
|
|
{
|
|
|
|
benc_val_t meta, * beInfo, * val, * val2;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Parse bencoded infos */
|
|
|
|
if( tr_bencLoad( buf, size, &meta, NULL ) )
|
2007-03-13 06:56:50 +00:00
|
|
|
{
|
2007-07-01 02:54:37 +00:00
|
|
|
tr_err( "Error while parsing bencoded data [%*.*s]", (int)size, (int)size, (char*)buf );
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-04-02 20:38:23 +00:00
|
|
|
|
|
|
|
/* Get info hash */
|
|
|
|
beInfo = tr_bencDictFind( &meta, "info" );
|
|
|
|
if( NULL == beInfo || TYPE_DICT != beInfo->type )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-04-02 20:38:23 +00:00
|
|
|
tr_err( "%s \"info\" dictionary", ( beInfo ? "Invalid" : "Missing" ) );
|
|
|
|
tr_bencFree( &meta );
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2007-04-02 20:38:23 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
tr_sha1( inf->hash, beInfo->begin, beInfo->end - beInfo->begin, NULL );
|
|
|
|
|
2007-04-02 20:38:23 +00:00
|
|
|
for( i = 0; i < SHA_DIGEST_LENGTH; i++ )
|
|
|
|
{
|
|
|
|
snprintf( inf->hashString + i * 2, sizeof( inf->hashString ) - i * 2,
|
|
|
|
"%02x", inf->hash[i] );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2006-11-23 01:38:18 +00:00
|
|
|
/* Comment info */
|
2007-03-05 00:07:48 +00:00
|
|
|
val = tr_bencDictFindFirst( &meta, "comment.utf-8", "comment", NULL );
|
|
|
|
if( NULL != val && TYPE_STR == val->type )
|
2006-11-23 01:38:18 +00:00
|
|
|
{
|
2007-08-15 23:02:56 +00:00
|
|
|
strlcat_utf8( inf->comment, val->val.s.s, sizeof( inf->comment ), 0 );
|
2006-11-23 01:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Creator info */
|
2007-03-06 03:39:48 +00:00
|
|
|
val = tr_bencDictFindFirst( &meta, "created by.utf-8", "created by", NULL );
|
2007-03-05 00:07:48 +00:00
|
|
|
if( NULL != val && TYPE_STR == val->type )
|
2006-11-23 01:38:18 +00:00
|
|
|
{
|
2007-08-15 23:02:56 +00:00
|
|
|
strlcat_utf8( inf->creator, val->val.s.s, sizeof( inf->creator ), 0 );
|
2006-11-23 01:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Date created */
|
2007-03-05 00:07:48 +00:00
|
|
|
inf->dateCreated = 0;
|
|
|
|
val = tr_bencDictFind( &meta, "creation date" );
|
|
|
|
if( NULL != val && TYPE_INT == val->type )
|
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" );
|
|
|
|
val2 = tr_bencDictFind( &meta, "private" );
|
|
|
|
if( ( NULL != val && ( TYPE_INT != val->type || 0 != val->val.i ) ) ||
|
|
|
|
( NULL != val2 && ( TYPE_INT != val2->type || 0 != val2->val.i ) ) )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-01-27 22:37:10 +00:00
|
|
|
inf->flags |= TR_FLAG_PRIVATE;
|
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" );
|
|
|
|
if( NULL == val || TYPE_INT != val->type )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-03-05 00:07:48 +00:00
|
|
|
tr_err( "%s \"piece length\" entry", ( val ? "Invalid" : "Missing" ) );
|
|
|
|
goto fail;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
inf->pieceSize = val->val.i;
|
|
|
|
|
|
|
|
/* Hashes */
|
|
|
|
val = tr_bencDictFind( beInfo, "pieces" );
|
2007-03-05 00:07:48 +00:00
|
|
|
if( NULL == val || TYPE_STR != val->type )
|
|
|
|
{
|
|
|
|
tr_err( "%s \"pieces\" entry", ( val ? "Invalid" : "Missing" ) );
|
|
|
|
goto fail;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
if( val->val.s.i % SHA_DIGEST_LENGTH )
|
|
|
|
{
|
2006-07-16 23:40:22 +00:00
|
|
|
tr_err( "Invalid \"piece\" string (size is %d)", val->val.s.i );
|
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
|
|
|
|
2007-07-27 01:50:26 +00:00
|
|
|
if( !inf->fileCount )
|
|
|
|
{
|
|
|
|
tr_err( "Torrent has no files." );
|
|
|
|
goto fail;
|
|
|
|
}
|
2007-07-27 01:58:49 +00:00
|
|
|
|
|
|
|
if( !inf->totalSize )
|
2007-07-27 01:50:26 +00:00
|
|
|
{
|
2007-07-27 01:58:49 +00:00
|
|
|
tr_err( "Torrent is zero bytes long." );
|
|
|
|
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 )
|
|
|
|
{
|
2006-07-16 23:40:22 +00:00
|
|
|
tr_err( "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 */
|
|
|
|
if( getannounce( inf, &meta ) )
|
|
|
|
{
|
2007-03-05 00:07:48 +00:00
|
|
|
goto fail;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_bencFree( &meta );
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_OK;
|
2007-03-05 00:07:48 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
tr_metainfoFree( inf );
|
|
|
|
tr_bencFree( &meta );
|
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
|
|
|
{
|
2007-08-14 15:24:48 +00:00
|
|
|
int i, j;
|
2006-12-17 16:36:27 +00:00
|
|
|
|
2007-06-28 02:02:30 +00:00
|
|
|
tr_free( inf->pieces );
|
|
|
|
tr_free( inf->files );
|
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
|
|
|
}
|
|
|
|
|
2007-03-05 00:07:48 +00:00
|
|
|
static int getfile( char * buf, int size,
|
2007-03-30 00:12:39 +00:00
|
|
|
const char * prefix, benc_val_t * name )
|
2007-03-05 00:07:48 +00:00
|
|
|
{
|
2007-03-30 00:12:39 +00:00
|
|
|
benc_val_t * dir;
|
|
|
|
const char ** list;
|
|
|
|
int ii, jj;
|
2007-03-05 00:07:48 +00:00
|
|
|
|
|
|
|
if( TYPE_LIST != name->type )
|
|
|
|
{
|
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] ) );
|
|
|
|
if( NULL == 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
|
|
|
{
|
2007-03-31 17:40:28 +00:00
|
|
|
dir = &name->val.l.vals[ii];
|
2007-03-30 00:12:39 +00:00
|
|
|
if( TYPE_STR != dir->type )
|
2007-03-05 00:07:48 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-08-15 23:02:56 +00:00
|
|
|
strlcat_utf8( buf, prefix, size, 0 );
|
2007-03-05 00:07:48 +00:00
|
|
|
for( ii = 0; jj > ii; ii++ )
|
|
|
|
{
|
2007-08-15 23:02:56 +00:00
|
|
|
strlcat_utf8( buf, TR_PATH_DELIMITER_STR, size, 0 );
|
|
|
|
strlcat_utf8( buf, list[ii], size, TR_PATH_DELIMITER );
|
2007-03-05 00:07:48 +00:00
|
|
|
}
|
|
|
|
free( list );
|
|
|
|
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_OK;
|
2007-03-05 00:07:48 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static int getannounce( tr_info * inf, benc_val_t * meta )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-03-30 00:12:39 +00:00
|
|
|
benc_val_t * val, * subval, * urlval;
|
|
|
|
char * address, * announce;
|
|
|
|
int ii, jj, port, random, subcount;
|
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" );
|
|
|
|
if( NULL != val && TYPE_LIST == val->type && 0 < val->val.l.count )
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2007-03-31 17:40:28 +00:00
|
|
|
subval = &val->val.l.vals[ii];
|
2006-12-17 16:36:27 +00:00
|
|
|
if( TYPE_LIST != subval->type || 0 >= subval->val.l.count )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
subcount = 0;
|
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 )
|
|
|
|
{
|
|
|
|
tr_inf( "No valid entries in \"announce-list\"" );
|
|
|
|
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" );
|
|
|
|
if( NULL == val || TYPE_STR != val->type )
|
|
|
|
{
|
|
|
|
tr_err( "No \"announce\" entry" );
|
|
|
|
return TR_EINVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !inf->trackerTiers )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if( tr_httpParseUrl( val->val.s.s, val->val.s.i,
|
|
|
|
&address, &port, &announce ) )
|
|
|
|
{
|
|
|
|
tr_err( "Invalid announce URL (%s)", val->val.s.s );
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2006-12-17 16:36:27 +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
|
|
|
{
|
|
|
|
int ret = tr_httpParseUrl( address, address_len,
|
|
|
|
&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-07-01 02:54:37 +00:00
|
|
|
static void
|
2007-06-18 19:39:52 +00:00
|
|
|
savedname( char * name, size_t len, const char * hash, const char * tag )
|
2007-03-13 06:56:50 +00:00
|
|
|
{
|
2007-06-18 19:39:52 +00:00
|
|
|
const char * torDir = tr_getTorrentsDirectory ();
|
|
|
|
|
|
|
|
if( tag == NULL )
|
2007-03-13 06:56:50 +00:00
|
|
|
{
|
2007-06-18 19:39:52 +00:00
|
|
|
tr_buildPath( name, len, torDir, hash, NULL );
|
2007-03-13 06:56:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-06-18 19:39:52 +00:00
|
|
|
char base[1024];
|
|
|
|
snprintf( base, sizeof(base), "%s-%s", hash, tag );
|
|
|
|
tr_buildPath( name, len, torDir, base, NULL );
|
2007-03-13 06:56:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_metainfoRemoveSaved( const char * hashString, const char * tag )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
char file[MAX_PATH_LENGTH];
|
|
|
|
|
2007-03-13 06:56:50 +00:00
|
|
|
savedname( file, sizeof file, hashString, tag );
|
2007-04-28 01:34:39 +00:00
|
|
|
unlink( file );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-07-01 02:54:37 +00:00
|
|
|
static uint8_t *
|
|
|
|
readtorrent( const char * path, size_t * size )
|
2007-03-13 06:56:50 +00:00
|
|
|
{
|
2007-04-02 20:49:08 +00:00
|
|
|
uint8_t * buf;
|
2007-03-13 06:56:50 +00:00
|
|
|
struct stat sb;
|
|
|
|
FILE * file;
|
|
|
|
|
|
|
|
/* try to stat the file */
|
2007-06-18 20:56:04 +00:00
|
|
|
errno = 0;
|
2007-03-13 06:56:50 +00:00
|
|
|
if( stat( path, &sb ) )
|
|
|
|
{
|
2007-06-18 21:03:10 +00:00
|
|
|
tr_err( "Couldn't get information for file \"%s\" %s", path, strerror(errno) );
|
2007-04-02 20:38:23 +00:00
|
|
|
return NULL;
|
2007-03-13 06:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ( sb.st_mode & S_IFMT ) != S_IFREG )
|
|
|
|
{
|
|
|
|
tr_err( "Not a regular file (%s)", path );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if( sb.st_size > TORRENT_MAX_SIZE )
|
|
|
|
{
|
2007-04-02 20:38:23 +00:00
|
|
|
tr_err( "Torrent file is too big (%"PRIu64" bytes)",
|
|
|
|
( uint64_t )sb.st_size );
|
2007-03-13 06:56:50 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the torrent file into our buffer */
|
|
|
|
file = fopen( path, "rb" );
|
|
|
|
if( !file )
|
|
|
|
{
|
2007-06-18 20:56:04 +00:00
|
|
|
tr_err( "Couldn't open file \"%s\" %s", path, strerror(errno) );
|
2007-03-13 06:56:50 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
buf = malloc( sb.st_size );
|
|
|
|
if( NULL == buf )
|
|
|
|
{
|
2007-06-18 20:56:04 +00:00
|
|
|
tr_err( "Couldn't allocate memory (%"PRIu64" bytes)",
|
2007-04-02 20:38:23 +00:00
|
|
|
( uint64_t )sb.st_size );
|
2007-03-13 06:56:50 +00:00
|
|
|
}
|
|
|
|
fseek( file, 0, SEEK_SET );
|
|
|
|
if( fread( buf, sb.st_size, 1, file ) != 1 )
|
|
|
|
{
|
2007-06-18 20:56:04 +00:00
|
|
|
tr_err( "Error reading \"%s\" %s", path, strerror(errno) );
|
2007-03-13 06:56:50 +00:00
|
|
|
free( buf );
|
|
|
|
fclose( file );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
fclose( file );
|
|
|
|
|
2007-04-02 20:38:23 +00:00
|
|
|
*size = sb.st_size;
|
2007-03-13 06:56:50 +00:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save a copy of the torrent file in the saved torrent directory */
|
2007-07-01 02:54:37 +00:00
|
|
|
static int
|
|
|
|
savetorrent( const char * hash, const char * tag,
|
|
|
|
const uint8_t * buf, size_t buflen )
|
2007-03-13 06:56:50 +00:00
|
|
|
{
|
|
|
|
char path[MAX_PATH_LENGTH];
|
|
|
|
FILE * file;
|
|
|
|
|
|
|
|
savedname( path, sizeof path, hash, tag );
|
|
|
|
file = fopen( path, "wb" );
|
|
|
|
if( !file )
|
|
|
|
{
|
|
|
|
tr_err( "Could not open file (%s) (%s)", path, 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 )
|
|
|
|
{
|
|
|
|
tr_err( "Could not write file (%s) (%s)", path, strerror( errno ) );
|
|
|
|
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
|
2007-09-20 16:32:01 +00:00
|
|
|
parseFiles( tr_info * inf, benc_val_t * name,
|
2007-03-30 00:12:39 +00:00
|
|
|
benc_val_t * files, benc_val_t * length )
|
|
|
|
{
|
|
|
|
benc_val_t * item, * path;
|
|
|
|
int ii;
|
|
|
|
|
|
|
|
if( NULL == name || TYPE_STR != name->type )
|
|
|
|
{
|
|
|
|
tr_err( "%s \"name\" string", ( name ? "Invalid" : "Missing" ) );
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2007-03-30 00:12:39 +00:00
|
|
|
}
|
|
|
|
|
2007-08-15 23:02:56 +00:00
|
|
|
strlcat_utf8( inf->name, name->val.s.s, sizeof( inf->name ),
|
|
|
|
TR_PATH_DELIMITER );
|
2007-03-30 00:12:39 +00:00
|
|
|
if( '\0' == inf->name[0] )
|
|
|
|
{
|
|
|
|
tr_err( "Invalid \"name\" string" );
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2007-03-30 00:12:39 +00:00
|
|
|
}
|
|
|
|
inf->totalSize = 0;
|
|
|
|
|
|
|
|
if( files && TYPE_LIST == files->type )
|
|
|
|
{
|
|
|
|
/* Multi-file mode */
|
|
|
|
inf->multifile = 1;
|
|
|
|
inf->fileCount = files->val.l.count;
|
|
|
|
inf->files = calloc( inf->fileCount, sizeof( inf->files[0] ) );
|
|
|
|
|
|
|
|
if( NULL == 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 );
|
2007-03-31 19:10:32 +00:00
|
|
|
if( getfile( inf->files[ii].name, sizeof( inf->files[0].name ),
|
2007-03-30 00:12:39 +00:00
|
|
|
inf->name, path ) )
|
|
|
|
{
|
|
|
|
tr_err( "%s \"path\" entry",
|
|
|
|
( path ? "Invalid" : "Missing" ) );
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2007-03-30 00:12:39 +00:00
|
|
|
}
|
|
|
|
length = tr_bencDictFind( item, "length" );
|
|
|
|
if( NULL == length || TYPE_INT != length->type )
|
|
|
|
{
|
|
|
|
tr_err( "%s \"length\" entry",
|
|
|
|
( length ? "Invalid" : "Missing" ) );
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( NULL != length && TYPE_INT == length->type )
|
|
|
|
{
|
|
|
|
/* Single-file mode */
|
|
|
|
inf->multifile = 0;
|
|
|
|
inf->fileCount = 1;
|
|
|
|
inf->files = calloc( 1, sizeof( inf->files[0] ) );
|
|
|
|
|
|
|
|
if( NULL == inf->files )
|
|
|
|
{
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_EINVALID;
|
2007-03-30 00:12:39 +00:00
|
|
|
}
|
|
|
|
|
2007-08-15 23:02:56 +00:00
|
|
|
strlcat_utf8( inf->files[0].name, name->val.s.s,
|
|
|
|
sizeof( inf->files[0].name ), TR_PATH_DELIMITER );
|
2007-03-30 00:12:39 +00:00
|
|
|
|
|
|
|
inf->files[0].length = length->val.i;
|
|
|
|
inf->totalSize += length->val.i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_err( "%s \"files\" entry and %s \"length\" entry",
|
|
|
|
( files ? "Invalid" : "Missing" ),
|
|
|
|
( length ? "invalid" : "missing" ) );
|
|
|
|
}
|
|
|
|
|
2007-06-27 14:54:31 +00:00
|
|
|
return TR_OK;
|
2007-03-30 00:12:39 +00:00
|
|
|
}
|