re-randomize peer_id each time we start a torrent. (ticket #257)

This commit is contained in:
Charles Kerr 2007-07-16 11:19:44 +00:00
parent 551b2dd106
commit 1d95bcc689
6 changed files with 43 additions and 36 deletions

View File

@ -132,6 +132,8 @@ typedef enum { TR_NET_OK, TR_NET_ERROR, TR_NET_WAIT } tr_tristate_t;
#define FALSE 0
#endif
void tr_peerIdNew ( char* buf, int buflen );
void tr_torrentResetTransferStats( tr_torrent_t * );
int tr_torrentAddCompact( tr_torrent_t * tor, int from,
@ -156,6 +158,9 @@ typedef enum
}
run_status_t;
#define TR_ID_LEN 20
#define TR_KEY_LEN 20
struct tr_torrent_s
{
tr_handle_t * handle;
@ -171,7 +176,7 @@ struct tr_torrent_s
char errorString[128];
int hasChangedState;
char * id;
char peer_id[TR_ID_LEN+1];
char * key;
uint8_t * azId;
int publicPort;
@ -234,9 +239,6 @@ struct tr_handle_s
tr_shared_t * shared;
#define TR_ID_LEN 20
char id[TR_ID_LEN+1];
#define TR_KEY_LEN 20
char key[TR_KEY_LEN+1];
tr_handle_status_t stats[2];

View File

@ -496,7 +496,7 @@ int tr_peerPulse( tr_peer_t * peer )
buf[20] = 0x80; /* azureus protocol */
buf[25] = 0x10; /* extended messages */
memcpy( &buf[28], inf->hash, 20 );
memcpy( &buf[48], tor->id, 20 );
memcpy( &buf[48], tor->peer_id, 20 );
switch( tr_netSend( peer->socket, buf, 68 ) )
{

View File

@ -611,7 +611,7 @@ static int parseHandshake( tr_torrent_t * tor, tr_peer_t * peer )
return TR_ERROR;
}
if( !memcmp( &peer->buf[48], tor->id, TR_ID_LEN ) )
if( !memcmp( &peer->buf[48], tor->peer_id, TR_ID_LEN ) )
{
/* We are connected to ourselves... */
peer_dbg( "GET handshake, that is us" );

View File

@ -187,7 +187,6 @@ torrentRealInit( tr_handle_t * h,
tr_torrentInitFilePieces( tor );
tor->handle = h;
tor->id = h->id;
tor->key = h->key;
tor->azId = h->azId;
tor->hasChangedState = -1;
@ -687,16 +686,16 @@ tr_torrentFiles( const tr_torrent_t * tor, int * fileCount )
for( i=0; i<n; ++i, ++walk )
{
const uint64_t length = tor->info.files[i].length;
const tr_file_t * file = tor->info.files + i;
cp_status_t cp;
walk->bytesCompleted = fileBytesCompleted( tor, i );
walk->progress = length
? walk->bytesCompleted / (float)length
walk->progress = file->length
? walk->bytesCompleted / (float)file->length
: 1.0;
if( walk->bytesCompleted >= length )
if( walk->bytesCompleted >= file->length )
cp = TR_CP_COMPLETE;
else if( tor->info.files[i].dnd )
cp = TR_CP_DONE;
@ -1089,6 +1088,7 @@ torrentThreadLoop ( void * _tor )
tor->recheckFlag = TRUE;
continue;
}
tr_peerIdNew ( tor->peer_id, sizeof(tor->peer_id) );
tor->tracker = tr_trackerInit( tor );
tor->startDate = tr_date();
}

View File

@ -46,7 +46,7 @@ struct tr_tracker_s
{
tr_torrent_t * tor;
char * id;
const char * peer_id;
char * trackerid;
struct tchead * tiers;
@ -97,7 +97,7 @@ struct tr_tracker_s
static void setAnnounce ( tr_tracker_t * tc, struct tclist * new );
static void failureAnnouncing( tr_tracker_t * tc );
static tr_http_t * getQuery ( tr_tracker_t * tc );
static tr_http_t * getScrapeQuery ( tr_tracker_t * tc );
static tr_http_t * getScrapeQuery ( const tr_tracker_t * tc );
static void readAnswer ( tr_tracker_t * tc, const char *, int,
int * peerCount, uint8_t ** peerCompact );
static void readScrapeAnswer ( tr_tracker_t * tc, const char *, int );
@ -120,7 +120,7 @@ tr_tracker_t * tr_trackerInit( tr_torrent_t * tor )
}
tc->tor = tor;
tc->id = tor->id;
tc->peer_id = tor->peer_id;
tc->started = 1;
@ -524,9 +524,11 @@ void tr_trackerClose( tr_tracker_t * tc )
static tr_http_t * getQuery( tr_tracker_t * tc )
{
tr_torrent_t * tor = tc->tor;
tr_tracker_info_t * tcInf = tc->tcCur->tl_inf;
const tr_tracker_info_t * tcInf = tc->tcCur->tl_inf;
char * event, * trackerid, * idparam;
const char * trackerid;
const char * event;
const char * idparam;
uint64_t left;
char start;
int numwant = 50;
@ -584,18 +586,15 @@ static tr_http_t * getQuery( tr_tracker_t * tc )
"%s%s"
"%s",
tcInf->announce, start, tor->escapedHashString,
tc->id, tc->publicPort, tor->uploadedCur, tor->downloadedCur,
tc->peer_id, tc->publicPort, tor->uploadedCur, tor->downloadedCur,
left, numwant, tor->key, idparam, trackerid, event );
}
static tr_http_t * getScrapeQuery( tr_tracker_t * tc )
static tr_http_t * getScrapeQuery( const tr_tracker_t * tc )
{
tr_torrent_t * tor = tc->tor;
tr_tracker_info_t * tcInf = tc->tcCur->tl_inf;
char start;
start = ( strchr( tcInf->scrape, '?' ) ? '&' : '?' );
const tr_torrent_t * tor = tc->tor;
const tr_tracker_info_t * tcInf = tc->tcCur->tl_inf;
const char start = ( strchr( tcInf->scrape, '?' ) ? '&' : '?' );
return tr_httpClient( TR_HTTP_GET, tcInf->address, tcInf->port,
"%s%c"
"info_hash=%s",

View File

@ -28,6 +28,24 @@
#include "net.h"
#include "shared.h"
/* Generate a peer id : "-TRxxyz-" + 12 random alphanumeric
characters, where xx is the major version number, y is the
minor version number, and z is the maintenance number (Azureus-style) */
void
tr_peerIdNew ( char * buf, int buflen )
{
int i;
assert( buflen == TR_ID_LEN + 1 );
snprintf( buf, TR_ID_LEN, "-TR" VERSION_MAJOR VERSION_MINOR VERSION_MAINTENANCE VERSION_BETA "-" );
assert( strlen(buf) == 8 );
for( i=8; i<TR_ID_LEN; ++i ) {
const int r = tr_rand( 36 );
buf[i] = ( r < 26 ) ? ( 'a' + r ) : ( '0' + r - 26 ) ;
}
buf[TR_ID_LEN] = '\0';
}
/***********************************************************************
* tr_init
***********************************************************************
@ -54,18 +72,6 @@ tr_handle_t * tr_init( const char * tag )
return NULL;
}
/* Generate a peer id : "-TRxxyz-" + 12 random alphanumeric
characters, where xx is the major version number, y is the
minor version number, and z is the maintenance number (Azureus-style) */
snprintf( h->id, sizeof h->id, "-TR" VERSION_MAJOR VERSION_MINOR VERSION_MAINTENANCE VERSION_BETA "-" );
assert( strlen(h->id) == 8 );
for( i=8; i<TR_ID_LEN; ++i )
{
const int r = tr_rand( 36 );
h->id[i] = ( r < 26 ) ? ( 'a' + r ) : ( '0' + r - 26 ) ;
}
tr_dbg( "Transmission ID is [%s]", h->id );
/* Random key */
for( i=0; i < TR_KEY_LEN; ++i )
{