This commit is contained in:
Charles Kerr 2008-01-07 06:19:34 +00:00
parent efd3910b09
commit 30d00fbc53
5 changed files with 44 additions and 28 deletions

View File

@ -85,8 +85,6 @@ enum
#define HANDSHAKE_GET_EXTPREF( reserved ) ( (reserved)[5] & 0x03 )
#define HANDSHAKE_SET_EXTPREF( reserved, val ) ( (reserved)[5] |= 0x03 & (val) )
extern const char* getPeerId( void ) ;
struct tr_handshake
{
unsigned int havePeerID : 1;
@ -201,6 +199,7 @@ buildHandshakeMessage( tr_handshake * handshake,
uint8_t * buf = tr_new0( uint8_t, HANDSHAKE_SIZE );
uint8_t * walk = buf;
const uint8_t * torrentHash = tr_cryptoGetTorrentHash( handshake->crypto );
const uint8_t * peerId = tr_getPeerId( );
memcpy( walk, HANDSHAKE_NAME, HANDSHAKE_NAME_LEN );
walk += HANDSHAKE_NAME_LEN;
@ -211,9 +210,10 @@ buildHandshakeMessage( tr_handshake * handshake,
walk += HANDSHAKE_FLAGS_LEN;
memcpy( walk, torrentHash, SHA_DIGEST_LENGTH );
walk += SHA_DIGEST_LENGTH;
memcpy( walk, getPeerId(), TR_ID_LEN );
walk += TR_ID_LEN;
memcpy( walk, peerId, PEER_ID_LEN );
walk += PEER_ID_LEN;
assert( strlen( ( const char* )peerId ) == PEER_ID_LEN );
assert( walk-buf == HANDSHAKE_SIZE );
*setme_len = walk - buf;
return buf;
@ -267,7 +267,7 @@ parseHandshake( tr_handshake * handshake,
/* peer id */
handshake->havePeerID = TRUE;
dbgmsg( handshake, "peer-id is [%*.*s]", PEER_ID_LEN, PEER_ID_LEN, handshake->peer_id );
if( !memcmp( handshake->peer_id, getPeerId(), PEER_ID_LEN ) ) {
if( !memcmp( handshake->peer_id, tr_getPeerId(), PEER_ID_LEN ) ) {
dbgmsg( handshake, "streuth! we've connected to ourselves." );
return HANDSHAKE_PEER_IS_SELF;
}
@ -698,7 +698,7 @@ readPeerId( tr_handshake * handshake, struct evbuffer * inbuf )
tr_free( client );
/* if we've somehow connected to ourselves, don't keep the connection */
peerIsGood = memcmp( handshake->peer_id, getPeerId(), PEER_ID_LEN ) ? 1 : 0;
peerIsGood = memcmp( handshake->peer_id, tr_getPeerId(), PEER_ID_LEN ) ? 1 : 0;
tr_handshakeDone( handshake, peerIsGood );
dbgmsg( handshake, "isPeerGood == %d", peerIsGood );
return READ_DONE;

View File

@ -49,7 +49,9 @@ int tr_trackerInfoInit( struct tr_tracker_info * info,
void tr_trackerInfoClear( struct tr_tracker_info * info );
void tr_peerIdNew ( char* buf, int buflen );
uint8_t* tr_peerIdNew( void );
const uint8_t* tr_getPeerId( void );
struct tr_handle
{

View File

@ -96,8 +96,6 @@ typedef enum
}
tr_recheck_state;
#define TR_ID_LEN 20
struct tr_torrent
{
tr_handle * handle;

View File

@ -104,7 +104,7 @@ struct tr_tracker
closed and opened again without quitting Transmission ...
change the peerid. It would help sometimes if a stopped event
was missed to ensure that we didn't think someone was cheating. */
char peer_id[TR_ID_LEN + 1];
uint8_t * peer_id;
/* these are set from the latest tracker response... -1 is 'unknown' */
int timesDownloaded;
@ -1058,6 +1058,7 @@ onTrackerFreeNow( void * vt )
tr_publisherFree( &t->publisher );
tr_free( t->name );
tr_free( t->trackerID );
tr_free( t->peer_id );
/* addresses... */
for( i=0; i<t->addressCount; ++i )
@ -1133,7 +1134,9 @@ tr_trackerGetCounts( const tr_tracker * t,
void
tr_trackerStart( tr_tracker * t )
{
tr_peerIdNew( t->peer_id, sizeof(t->peer_id) );
tr_free( t->peer_id );
t->peer_id = tr_peerIdNew( );
if( t->isRunning == 0 ) {
t->isRunning = 1;
enqueueRequest( t->handle, t, TR_REQ_STARTED );

View File

@ -51,30 +51,43 @@
characters, where x is the major version number, y is the
minor version number, z is the maintenance number, and b
designates beta (Azureus-style) */
void
tr_peerIdNew ( char * buf, int buflen )
uint8_t*
tr_peerIdNew( void )
{
int i;
assert( buflen == TR_ID_LEN + 1 );
int total = 0;
uint8_t * buf = tr_new( uint8_t, 21 );
const char * pool = "0123456789abcdefghijklmnopqrstuvwxyz";
const int base = strlen( pool );
snprintf( buf, TR_ID_LEN, "%s", PEERID_PREFIX );
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 ) ;
memcpy( buf, PEERID_PREFIX, 8 );
for( i=8; i<19; ++i ) {
const int val = tr_rand( base );
total += val;
buf[i] = pool[val];
}
buf[TR_ID_LEN] = '\0';
if( 1 ) {
int val = 0;
while( ( total + val ) % base )
++val;
buf[19] = pool[val];
total += val;
}
assert( ( total % base ) == 0 );
buf[20] = '\0';
return buf;
}
const char*
getPeerId( void )
const uint8_t*
tr_getPeerId( void )
{
static char * peerId = NULL;
if( !peerId ) {
peerId = tr_new0( char, TR_ID_LEN + 1 );
tr_peerIdNew( peerId, TR_ID_LEN + 1 );
}
return peerId;
static uint8_t * id = NULL;
if( id == NULL )
id = tr_peerIdNew( );
return id;
}
/***