From 30d00fbc5329c8329e1d430cabf4528f2eeb12d6 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 7 Jan 2008 06:19:34 +0000 Subject: [PATCH] cleanup --- libtransmission/handshake.c | 12 ++++----- libtransmission/internal.h | 4 ++- libtransmission/torrent.h | 2 -- libtransmission/tracker.c | 7 +++-- libtransmission/transmission.c | 47 ++++++++++++++++++++++------------ 5 files changed, 44 insertions(+), 28 deletions(-) diff --git a/libtransmission/handshake.c b/libtransmission/handshake.c index 277c0adf3..09b0cb5c1 100644 --- a/libtransmission/handshake.c +++ b/libtransmission/handshake.c @@ -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; diff --git a/libtransmission/internal.h b/libtransmission/internal.h index 58dce3a42..6071d5b45 100644 --- a/libtransmission/internal.h +++ b/libtransmission/internal.h @@ -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 { diff --git a/libtransmission/torrent.h b/libtransmission/torrent.h index f94ee305e..89a556467 100644 --- a/libtransmission/torrent.h +++ b/libtransmission/torrent.h @@ -96,8 +96,6 @@ typedef enum } tr_recheck_state; -#define TR_ID_LEN 20 - struct tr_torrent { tr_handle * handle; diff --git a/libtransmission/tracker.c b/libtransmission/tracker.c index c8f4f6e5a..2e1306ba4 100644 --- a/libtransmission/tracker.c +++ b/libtransmission/tracker.c @@ -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; iaddressCount; ++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 ); diff --git a/libtransmission/transmission.c b/libtransmission/transmission.c index 3f3183195..301fa5d71 100644 --- a/libtransmission/transmission.c +++ b/libtransmission/transmission.c @@ -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