1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-03-03 18:25:35 +00:00

(trunk libT) Add an enumeration for the peer id length. Use that enum for the peer_id fields in tr_session and tr_torrent.

This also avoids an extra malloc/free per-torrent and per-session, but mostly this tweak is for the extra readability of the PEER_ID_LEN=20 enum.
This commit is contained in:
Jordan Lee 2011-03-10 12:35:23 +00:00
parent b38466323b
commit 896c9b54e1
6 changed files with 30 additions and 34 deletions

View file

@ -50,7 +50,6 @@ enum
HANDSHAKE_NAME_LEN = 20,
HANDSHAKE_FLAGS_LEN = 8,
HANDSHAKE_SIZE = 68,
PEER_ID_LEN = 20,
INCOMING_HANDSHAKE_LEN = 48,
/* Encryption Constants */
@ -218,7 +217,7 @@ buildHandshakeMessage( tr_handshake * handshake, uint8_t * buf )
uint8_t * walk = buf;
const uint8_t * torrentHash = tr_cryptoGetTorrentHash( handshake->crypto );
const tr_torrent * tor = tr_torrentFindFromHash( handshake->session, torrentHash );
const uint8_t * peer_id = tor && tor->peer_id ? tor->peer_id : tr_getPeerId( );
const uint8_t * peer_id = tor ? tor->peer_id : tr_getPeerId( handshake->session );
memcpy( walk, HANDSHAKE_NAME, HANDSHAKE_NAME_LEN );
walk += HANDSHAKE_NAME_LEN;
@ -298,7 +297,7 @@ parseHandshake( tr_handshake * handshake,
dbgmsg( handshake, "peer-id is [%*.*s]", PEER_ID_LEN, PEER_ID_LEN, peer_id );
tor = tr_torrentFindFromHash( handshake->session, hash );
tor_peer_id = tor && tor->peer_id ? tor->peer_id : tr_getPeerId( );
tor_peer_id = tor ? tor->peer_id : tr_getPeerId( handshake->session );
if( !memcmp( peer_id, tor_peer_id, PEER_ID_LEN ) )
{
dbgmsg( handshake, "streuth! we've connected to ourselves." );
@ -741,7 +740,7 @@ readPeerId( tr_handshake * handshake,
/* if we've somehow connected to ourselves, don't keep the connection */
tor = tr_torrentFindFromHash( handshake->session, tr_peerIoGetTorrentHash( handshake->io ) );
tor_peer_id = tor && tor->peer_id ? tor->peer_id : tr_getPeerId( );
tor_peer_id = tor ? tor->peer_id : tr_getPeerId( handshake->session );
peerIsGood = memcmp( peer_id, tor_peer_id, PEER_ID_LEN ) != 0;
dbgmsg( handshake, "isPeerGood == %d", (int)peerIsGood );
return tr_handshakeDone( handshake, peerIsGood );

View file

@ -81,13 +81,12 @@ getRandomPort( tr_session * s )
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) */
uint8_t*
tr_peerIdNew( void )
void
tr_peerIdInit( uint8_t * buf )
{
int i;
int val;
int total = 0;
uint8_t * buf = tr_new( uint8_t, 21 );
const char * pool = "0123456789abcdefghijklmnopqrstuvwxyz";
const int base = 36;
@ -103,18 +102,6 @@ tr_peerIdNew( void )
val = total % base ? base - ( total % base ) : 0;
buf[19] = pool[val];
buf[20] = '\0';
return buf;
}
const uint8_t*
tr_getPeerId( void )
{
static uint8_t * id = NULL;
if( id == NULL )
id = tr_peerIdNew( );
return id;
}
/***
@ -580,6 +567,7 @@ tr_sessionInit( const char * tag,
session->tag = tr_strdup( tag );
session->magicNumber = SESSION_MAGIC_NUMBER;
session->buffer = tr_valloc( SESSION_BUFFER_SIZE );
tr_peerIdInit( session->peer_id );
tr_bencInitList( &session->removedTorrents, 0 );
/* nice to start logging at the very beginning */

View file

@ -33,9 +33,12 @@
typedef enum { TR_NET_OK, TR_NET_ERROR, TR_NET_WAIT } tr_tristate_t;
uint8_t* tr_peerIdNew( void );
enum
{
PEER_ID_LEN = 20
};
const uint8_t* tr_getPeerId( void );
void tr_peerIdInit( uint8_t * setme );
struct event_base;
struct tr_address;
@ -206,6 +209,8 @@ struct tr_session
tr_bool bufferInUse;
tr_web_config_func * curl_easy_config_func;
uint8_t peer_id[PEER_ID_LEN+1];
};
static inline tr_port
@ -214,6 +219,12 @@ tr_sessionGetPublicPeerPort( const tr_session * session )
return session->public_peer_port;
}
static inline const uint8_t*
tr_getPeerId( tr_session * session )
{
return session->peer_id;
}
tr_bool tr_sessionAllowsDHT( const tr_session * session );
tr_bool tr_sessionAllowsLPD( const tr_session * session );

View file

@ -35,25 +35,25 @@ main( void )
{
int i;
int test = 0;
uint8_t peer_id[PEER_ID_LEN+1];
for( i = 0; i < 100000; ++i )
{
int j;
int val = 0;
uint8_t * pch = tr_peerIdNew( );
int j;
int val = 0;
check( strlen( (char*)pch ) == 20 );
check( !memcmp( pch, PEERID_PREFIX, 8 ) );
tr_peerIdInit( peer_id );
for( j = 8; j < 20; ++j )
check( strlen( (char*)peer_id ) == PEER_ID_LEN );
check( !memcmp( peer_id, PEERID_PREFIX, 8 ) );
for( j = 8; j < PEER_ID_LEN; ++j )
{
char tmp[2] = { pch[j], '\0' };
char tmp[2] = { peer_id[j], '\0' };
val += strtoul( tmp, NULL, 36 );
}
check( ( val % 36 ) == 0 );
tr_free( pch );
}
return 0;

View file

@ -1508,7 +1508,6 @@ freeTorrent( tr_torrent * tor )
tr_free( tor->downloadDir );
tr_free( tor->incompleteDir );
tr_free( tor->peer_id );
if( tor == session->torrentList )
session->torrentList = tor->next;
@ -1617,8 +1616,7 @@ torrentStart( tr_torrent * tor )
* 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. */
tr_free( tor->peer_id );
tor->peer_id = tr_peerIdNew( );
tr_peerIdInit( tor->peer_id );
tor->isRunning = 1;
tr_torrentSetDirty( tor );
tr_runInEventThread( tor->session, torrentStartImpl, tor );

View file

@ -157,7 +157,7 @@ struct tr_torrent
* peer_id that was registered by the peer. The peer_id from the tracker
* and in the handshake are expected to match.
*/
uint8_t * peer_id;
uint8_t peer_id[PEER_ID_LEN+1];
/* Where the files will be when it's complete */
char * downloadDir;