2009-11-24 02:16:31 +00:00
|
|
|
/*
|
2010-01-04 21:00:47 +00:00
|
|
|
* This file Copyright (C) 2009-2010 Mnemosyne LLC
|
2009-11-24 02:16:31 +00:00
|
|
|
*
|
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2010-10-01 13:33:39 +00:00
|
|
|
* $Id$
|
2009-11-24 02:16:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <event.h> /* struct evbuffer */
|
2010-02-06 05:22:27 +00:00
|
|
|
#include <stdio.h> /* remove() */
|
2009-11-24 02:16:31 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "bencode.h"
|
|
|
|
#include "crypto.h"
|
|
|
|
#include "magnet.h"
|
|
|
|
#include "metainfo.h"
|
2010-02-06 05:22:27 +00:00
|
|
|
#include "resume.h"
|
2009-11-24 02:16:31 +00:00
|
|
|
#include "torrent.h"
|
|
|
|
#include "torrent-magnet.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "web.h"
|
|
|
|
|
|
|
|
#define dbgmsg( tor, ... ) \
|
|
|
|
do { \
|
|
|
|
if( tr_deepLoggingIsActive( ) ) \
|
|
|
|
tr_deepLog( __FILE__, __LINE__, tor->info.name, __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
/* don't ask for the same metadata piece more than this often */
|
2009-11-24 17:10:40 +00:00
|
|
|
MIN_REPEAT_INTERVAL_SECS = 3
|
2009-11-24 02:16:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct metadata_node
|
|
|
|
{
|
|
|
|
time_t requestedAt;
|
|
|
|
int piece;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct tr_incomplete_metadata
|
|
|
|
{
|
|
|
|
uint8_t * metadata;
|
|
|
|
int metadata_size;
|
|
|
|
int pieceCount;
|
|
|
|
|
|
|
|
/** sorted from least to most recently requested */
|
|
|
|
struct metadata_node * piecesNeeded;
|
|
|
|
int piecesNeededCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
incompleteMetadataFree( struct tr_incomplete_metadata * m )
|
|
|
|
{
|
|
|
|
tr_free( m->metadata );
|
|
|
|
tr_free( m->piecesNeeded );
|
|
|
|
tr_free( m );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_torrentSetMetadataSizeHint( tr_torrent * tor, int size )
|
|
|
|
{
|
|
|
|
if( !tr_torrentHasMetadata( tor ) )
|
|
|
|
{
|
|
|
|
if( tor->incompleteMetadata == NULL )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct tr_incomplete_metadata * m;
|
2010-04-20 23:14:00 +00:00
|
|
|
const int n = ( size + ( METADATA_PIECE_SIZE - 1 ) ) / METADATA_PIECE_SIZE;
|
|
|
|
dbgmsg( tor, "metadata is %d bytes in %d pieces", size, n );
|
2009-11-24 02:16:31 +00:00
|
|
|
|
|
|
|
m = tr_new( struct tr_incomplete_metadata, 1 );
|
|
|
|
m->pieceCount = n;
|
|
|
|
m->metadata = tr_new( uint8_t, size );
|
|
|
|
m->metadata_size = size;
|
|
|
|
m->piecesNeededCount = n;
|
|
|
|
m->piecesNeeded = tr_new( struct metadata_node, n );
|
|
|
|
|
|
|
|
for( i=0; i<n; ++i ) {
|
|
|
|
m->piecesNeeded[i].piece = i;
|
|
|
|
m->piecesNeeded[i].requestedAt = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
tor->incompleteMetadata = m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-16 03:05:23 +00:00
|
|
|
static int
|
|
|
|
findInfoDictOffset( const tr_torrent * tor )
|
|
|
|
{
|
|
|
|
size_t fileLen;
|
|
|
|
uint8_t * fileContents;
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
/* load the file, and find the info dict's offset inside the file */
|
|
|
|
if(( fileContents = tr_loadFile( tor->info.torrent, &fileLen )))
|
|
|
|
{
|
|
|
|
tr_benc top;
|
|
|
|
|
|
|
|
if( !tr_bencParse( fileContents, fileContents + fileLen, &top, NULL ) )
|
|
|
|
{
|
|
|
|
tr_benc * infoDict;
|
|
|
|
|
|
|
|
if( tr_bencDictFindDict( &top, "info", &infoDict ) )
|
|
|
|
{
|
|
|
|
int infoLen;
|
|
|
|
char * infoContents = tr_bencToStr( infoDict, TR_FMT_BENC, &infoLen );
|
|
|
|
const uint8_t * i = (const uint8_t*) tr_memmem( (char*)fileContents, fileLen, infoContents, infoLen );
|
2010-09-19 21:07:57 +00:00
|
|
|
offset = i != NULL ? i - fileContents : 0;
|
2010-06-16 03:05:23 +00:00
|
|
|
tr_free( infoContents );
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free( fileContents );
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ensureInfoDictOffsetIsCached( tr_torrent * tor )
|
|
|
|
{
|
|
|
|
assert( tr_torrentHasMetadata( tor ) );
|
|
|
|
|
|
|
|
if( !tor->infoDictOffsetIsCached )
|
|
|
|
{
|
|
|
|
tor->infoDictOffset = findInfoDictOffset( tor );
|
|
|
|
tor->infoDictOffsetIsCached = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-24 02:16:31 +00:00
|
|
|
void*
|
2010-06-16 03:05:23 +00:00
|
|
|
tr_torrentGetMetadataPiece( tr_torrent * tor, int piece, int * len )
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
|
|
|
char * ret = NULL;
|
|
|
|
|
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
assert( piece >= 0 );
|
|
|
|
assert( len != NULL );
|
|
|
|
|
2010-06-16 03:05:23 +00:00
|
|
|
if( tr_torrentHasMetadata( tor ) )
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2010-06-16 03:05:23 +00:00
|
|
|
FILE * fp;
|
|
|
|
|
|
|
|
ensureInfoDictOffsetIsCached( tor );
|
|
|
|
|
|
|
|
assert( tor->infoDictLength > 0 );
|
|
|
|
assert( tor->infoDictOffset >= 0 );
|
|
|
|
|
|
|
|
fp = fopen( tor->info.torrent, "rb" );
|
2009-11-24 02:16:31 +00:00
|
|
|
if( fp != NULL )
|
|
|
|
{
|
2009-12-15 16:34:12 +00:00
|
|
|
const int o = piece * METADATA_PIECE_SIZE;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2009-12-15 16:34:12 +00:00
|
|
|
if( !fseek( fp, tor->infoDictOffset + o, SEEK_SET ) )
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2009-12-15 16:34:12 +00:00
|
|
|
const int l = o + METADATA_PIECE_SIZE <= tor->infoDictLength
|
2009-11-24 02:16:31 +00:00
|
|
|
? METADATA_PIECE_SIZE
|
2009-12-15 16:34:12 +00:00
|
|
|
: tor->infoDictLength - o;
|
|
|
|
|
|
|
|
if( 0<l && l<=METADATA_PIECE_SIZE )
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2009-12-15 16:34:12 +00:00
|
|
|
char * buf = tr_new( char, l );
|
|
|
|
const int n = fread( buf, 1, l, fp );
|
|
|
|
if( n == l )
|
|
|
|
{
|
|
|
|
*len = l;
|
|
|
|
ret = buf;
|
|
|
|
buf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free( buf );
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose( fp );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_torrentSetMetadataPiece( tr_torrent * tor, int piece, const void * data, int len )
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct tr_incomplete_metadata * m;
|
|
|
|
const int offset = piece * METADATA_PIECE_SIZE;
|
|
|
|
|
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
|
|
|
dbgmsg( tor, "got metadata piece %d", piece );
|
|
|
|
|
|
|
|
/* are we set up to download metadata? */
|
|
|
|
m = tor->incompleteMetadata;
|
|
|
|
if( m == NULL )
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* does this data pass the smell test? */
|
|
|
|
if( offset + len > m->metadata_size )
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* do we need this piece? */
|
|
|
|
for( i=0; i<m->piecesNeededCount; ++i )
|
|
|
|
if( m->piecesNeeded[i].piece == piece )
|
|
|
|
break;
|
|
|
|
if( i==m->piecesNeededCount )
|
|
|
|
return;
|
|
|
|
|
|
|
|
memcpy( m->metadata + offset, data, len );
|
|
|
|
|
|
|
|
tr_removeElementFromArray( m->piecesNeeded, i,
|
|
|
|
sizeof( struct metadata_node ),
|
|
|
|
m->piecesNeededCount-- );
|
|
|
|
|
|
|
|
dbgmsg( tor, "saving metainfo piece %d... %d remain", piece, m->piecesNeededCount );
|
|
|
|
|
|
|
|
/* are we done? */
|
|
|
|
if( m->piecesNeededCount == 0 )
|
|
|
|
{
|
|
|
|
tr_bool success = FALSE;
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_bool checksumPassed = FALSE;
|
|
|
|
tr_bool metainfoParsed = FALSE;
|
2009-11-24 02:16:31 +00:00
|
|
|
uint8_t sha1[SHA_DIGEST_LENGTH];
|
2010-02-02 22:45:22 +00:00
|
|
|
|
|
|
|
/* we've got a complete set of metainfo... see if it passes the checksum test */
|
2009-11-24 02:16:31 +00:00
|
|
|
dbgmsg( tor, "metainfo piece %d was the last one", piece );
|
|
|
|
tr_sha1( sha1, m->metadata, m->metadata_size, NULL );
|
2010-02-02 22:45:22 +00:00
|
|
|
if(( checksumPassed = !memcmp( sha1, tor->info.hash, SHA_DIGEST_LENGTH )))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2010-02-02 22:45:22 +00:00
|
|
|
/* checksum passed; now try to parse it as benc */
|
|
|
|
tr_benc infoDict;
|
|
|
|
const int err = tr_bencLoad( m->metadata, m->metadata_size, &infoDict, NULL );
|
2009-11-24 02:16:31 +00:00
|
|
|
dbgmsg( tor, "err is %d", err );
|
2010-02-02 22:45:22 +00:00
|
|
|
if(( metainfoParsed = !err ))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2010-02-06 05:22:27 +00:00
|
|
|
/* yay we have bencoded metainfo... merge it into our .torrent file */
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_benc newMetainfo;
|
2010-02-06 05:22:27 +00:00
|
|
|
char * path = tr_strdup( tor->info.torrent );
|
|
|
|
|
2010-02-02 22:45:22 +00:00
|
|
|
if( !tr_bencLoadFile( &newMetainfo, TR_FMT_BENC, path ) )
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_bool hasInfo;
|
2010-06-24 20:36:05 +00:00
|
|
|
tr_info info;
|
|
|
|
int infoDictLength;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2010-02-06 05:22:27 +00:00
|
|
|
/* remove any old .torrent and .resume files */
|
|
|
|
remove( path );
|
|
|
|
tr_torrentRemoveResume( tor );
|
|
|
|
|
2010-02-02 22:45:22 +00:00
|
|
|
dbgmsg( tor, "Saving completed metadata to \"%s\"", path );
|
|
|
|
tr_bencMergeDicts( tr_bencDictAddDict( &newMetainfo, "info", 0 ), &infoDict );
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2010-06-24 20:36:05 +00:00
|
|
|
memset( &info, 0, sizeof( tr_info ) );
|
|
|
|
success = tr_metainfoParse( tor->session, &newMetainfo, &info, &hasInfo, &infoDictLength );
|
2010-02-02 22:45:22 +00:00
|
|
|
|
2010-06-24 20:36:05 +00:00
|
|
|
if( success && !tr_getBlockSize( info.pieceSize ) )
|
|
|
|
{
|
2010-10-11 21:44:46 +00:00
|
|
|
tr_torrentSetLocalError( tor, "%s", _( "Magnet torrent's metadata is not usable" ) );
|
2010-06-24 20:36:05 +00:00
|
|
|
success = FALSE;
|
|
|
|
}
|
2010-02-02 22:45:22 +00:00
|
|
|
|
2010-06-24 20:36:05 +00:00
|
|
|
if( success )
|
|
|
|
{
|
|
|
|
/* keep the new info */
|
|
|
|
tor->info = info;
|
|
|
|
tor->infoDictLength = infoDictLength;
|
|
|
|
|
|
|
|
/* save the new .torrent file */
|
|
|
|
tr_bencToFile( &newMetainfo, TR_FMT_BENC, tor->info.torrent );
|
|
|
|
tr_sessionSetTorrentFile( tor->session, tor->info.hashString, tor->info.torrent );
|
|
|
|
tr_torrentGotNewInfoDict( tor );
|
|
|
|
tr_torrentSetDirty( tor );
|
|
|
|
}
|
2009-11-24 19:45:36 +00:00
|
|
|
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_bencFree( &newMetainfo );
|
|
|
|
}
|
2010-03-17 17:07:40 +00:00
|
|
|
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_bencFree( &infoDict );
|
2010-02-06 05:22:27 +00:00
|
|
|
tr_free( path );
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( success )
|
|
|
|
{
|
|
|
|
incompleteMetadataFree( tor->incompleteMetadata );
|
|
|
|
tor->incompleteMetadata = NULL;
|
|
|
|
}
|
|
|
|
else /* drat. */
|
|
|
|
{
|
|
|
|
const int n = m->pieceCount;
|
2010-02-02 22:45:22 +00:00
|
|
|
for( i=0; i<n; ++i )
|
|
|
|
{
|
2009-11-24 02:16:31 +00:00
|
|
|
m->piecesNeeded[i].piece = i;
|
|
|
|
m->piecesNeeded[i].requestedAt = 0;
|
|
|
|
}
|
|
|
|
m->piecesNeededCount = n;
|
|
|
|
dbgmsg( tor, "metadata error; trying again. %d pieces left", n );
|
2010-02-02 22:45:22 +00:00
|
|
|
|
|
|
|
tr_err( "magnet status: checksum passed %d, metainfo parsed %d",
|
|
|
|
(int)checksumPassed, (int)metainfoParsed );
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_bool
|
|
|
|
tr_torrentGetNextMetadataRequest( tr_torrent * tor, time_t now, int * setme_piece )
|
|
|
|
{
|
|
|
|
tr_bool have_request = FALSE;
|
|
|
|
struct tr_incomplete_metadata * m;
|
|
|
|
|
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
|
|
|
m = tor->incompleteMetadata;
|
|
|
|
|
|
|
|
if( ( m != NULL )
|
|
|
|
&& ( m->piecesNeededCount > 0 )
|
2009-11-24 17:10:40 +00:00
|
|
|
&& ( m->piecesNeeded[0].requestedAt + MIN_REPEAT_INTERVAL_SECS < now ) )
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const int piece = m->piecesNeeded[0].piece;
|
|
|
|
|
|
|
|
tr_removeElementFromArray( m->piecesNeeded, 0,
|
|
|
|
sizeof( struct metadata_node ),
|
|
|
|
m->piecesNeededCount-- );
|
|
|
|
|
|
|
|
i = m->piecesNeededCount++;
|
2009-11-24 17:10:40 +00:00
|
|
|
m->piecesNeeded[i].piece = piece;
|
|
|
|
m->piecesNeeded[i].requestedAt = now;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
|
|
|
dbgmsg( tor, "next piece to request: %d", piece );
|
|
|
|
*setme_piece = piece;
|
|
|
|
have_request = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return have_request;
|
|
|
|
}
|
2009-11-24 17:10:40 +00:00
|
|
|
|
2010-07-01 05:14:34 +00:00
|
|
|
double
|
2009-11-24 17:10:40 +00:00
|
|
|
tr_torrentGetMetadataPercent( const tr_torrent * tor )
|
|
|
|
{
|
2010-07-01 05:14:34 +00:00
|
|
|
double ret;
|
2009-11-24 17:10:40 +00:00
|
|
|
|
|
|
|
if( tr_torrentHasMetadata( tor ) )
|
|
|
|
ret = 1.0;
|
|
|
|
else {
|
|
|
|
const struct tr_incomplete_metadata * m = tor->incompleteMetadata;
|
|
|
|
if( m == NULL )
|
|
|
|
ret = 0.0;
|
|
|
|
else
|
2010-07-01 05:14:34 +00:00
|
|
|
ret = (m->pieceCount - m->piecesNeededCount) / (double)m->pieceCount;
|
2009-11-24 17:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2009-11-29 08:27:55 +00:00
|
|
|
|
|
|
|
char*
|
|
|
|
tr_torrentGetMagnetLink( const tr_torrent * tor )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char * ret;
|
2010-05-11 15:30:30 +00:00
|
|
|
const char * name;
|
2009-11-29 08:27:55 +00:00
|
|
|
struct evbuffer * s;
|
|
|
|
|
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
|
|
|
s = evbuffer_new( );
|
|
|
|
evbuffer_add_printf( s, "magnet:?xt=urn:btih:%s", tor->info.hashString );
|
2010-05-11 15:30:30 +00:00
|
|
|
name = tr_torrentName( tor );
|
|
|
|
if( name && *name )
|
|
|
|
{
|
|
|
|
evbuffer_add_printf( s, "%s", "&dn=" );
|
|
|
|
tr_http_escape( s, tr_torrentName( tor ), -1, TRUE );
|
|
|
|
}
|
2009-11-29 08:27:55 +00:00
|
|
|
for( i=0; i<tor->info.trackerCount; ++i )
|
|
|
|
{
|
|
|
|
evbuffer_add_printf( s, "%s", "&tr=" );
|
|
|
|
tr_http_escape( s, tor->info.trackers[i].announce, -1, TRUE );
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = tr_strndup( EVBUFFER_DATA( s ), EVBUFFER_LENGTH( s ) );
|
|
|
|
evbuffer_free( s );
|
|
|
|
return ret;
|
|
|
|
}
|