Changeset 12121


Ignore:
Timestamp:
Mar 10, 2011, 12:35:23 PM (12 years ago)
Author:
jordan
Message:

(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.

Location:
trunk/libtransmission
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/handshake.c

    r11954 r12121  
    5151    HANDSHAKE_FLAGS_LEN            = 8,
    5252    HANDSHAKE_SIZE                 = 68,
    53     PEER_ID_LEN                    = 20,
    5453    INCOMING_HANDSHAKE_LEN         = 48,
    5554
     
    219218    const uint8_t    * torrentHash = tr_cryptoGetTorrentHash( handshake->crypto );
    220219    const tr_torrent * tor = tr_torrentFindFromHash( handshake->session, torrentHash );
    221     const uint8_t    * peer_id = tor && tor->peer_id ? tor->peer_id : tr_getPeerId( );
     220    const uint8_t    * peer_id = tor ? tor->peer_id : tr_getPeerId( handshake->session );
    222221
    223222    memcpy( walk, HANDSHAKE_NAME, HANDSHAKE_NAME_LEN );
     
    299298
    300299    tor = tr_torrentFindFromHash( handshake->session, hash );
    301     tor_peer_id = tor && tor->peer_id ? tor->peer_id : tr_getPeerId( );
     300    tor_peer_id = tor ? tor->peer_id : tr_getPeerId( handshake->session );
    302301    if( !memcmp( peer_id, tor_peer_id, PEER_ID_LEN ) )
    303302    {
     
    742741    /* if we've somehow connected to ourselves, don't keep the connection */
    743742    tor = tr_torrentFindFromHash( handshake->session, tr_peerIoGetTorrentHash( handshake->io ) );
    744     tor_peer_id = tor && tor->peer_id ? tor->peer_id : tr_getPeerId( );
     743    tor_peer_id = tor ? tor->peer_id : tr_getPeerId( handshake->session );
    745744    peerIsGood = memcmp( peer_id, tor_peer_id, PEER_ID_LEN ) != 0;
    746745    dbgmsg( handshake, "isPeerGood == %d", (int)peerIsGood );
  • trunk/libtransmission/session.c

    r12103 r12121  
    8282   minor version number, z is the maintenance number, and b
    8383   designates beta (Azureus-style) */
    84 uint8_t*
    85 tr_peerIdNew( void )
     84void
     85tr_peerIdInit( uint8_t * buf )
    8686{
    8787    int          i;
    8888    int          val;
    8989    int          total = 0;
    90     uint8_t *    buf = tr_new( uint8_t, 21 );
    9190    const char * pool = "0123456789abcdefghijklmnopqrstuvwxyz";
    9291    const int    base = 36;
     
    104103    buf[19] = pool[val];
    105104    buf[20] = '\0';
    106 
    107     return buf;
    108 }
    109 
    110 const uint8_t*
    111 tr_getPeerId( void )
    112 {
    113     static uint8_t * id = NULL;
    114 
    115     if( id == NULL )
    116         id = tr_peerIdNew( );
    117     return id;
    118105}
    119106
     
    581568    session->magicNumber = SESSION_MAGIC_NUMBER;
    582569    session->buffer = tr_valloc( SESSION_BUFFER_SIZE );
     570    tr_peerIdInit( session->peer_id );
    583571    tr_bencInitList( &session->removedTorrents, 0 );
    584572
  • trunk/libtransmission/session.h

    r12059 r12121  
    3434typedef enum { TR_NET_OK, TR_NET_ERROR, TR_NET_WAIT } tr_tristate_t;
    3535
    36 uint8_t*       tr_peerIdNew( void );
    37 
    38 const uint8_t* tr_getPeerId( void );
     36enum
     37{
     38  PEER_ID_LEN = 20
     39};
     40
     41void tr_peerIdInit( uint8_t * setme );
    3942
    4043struct event_base;
     
    207210
    208211    tr_web_config_func          * curl_easy_config_func;
     212
     213    uint8_t peer_id[PEER_ID_LEN+1];
    209214};
    210215
     
    213218{
    214219    return session->public_peer_port;
     220}
     221
     222static inline const uint8_t*
     223tr_getPeerId( tr_session * session )
     224{
     225    return session->peer_id;
    215226}
    216227
  • trunk/libtransmission/test-peer-id.c

    r8233 r12121  
    3636    int i;
    3737    int test = 0;
     38    uint8_t peer_id[PEER_ID_LEN+1];
    3839
    3940    for( i = 0; i < 100000; ++i )
    4041    {
    41         int       j;
    42         int       val = 0;
    43         uint8_t * pch = tr_peerIdNew( );
     42        int j;
     43        int val = 0;
    4444
    45         check( strlen( (char*)pch ) == 20 );
    46         check( !memcmp( pch, PEERID_PREFIX, 8 ) );
     45        tr_peerIdInit( peer_id );
    4746
    48         for( j = 8; j < 20; ++j )
     47        check( strlen( (char*)peer_id ) == PEER_ID_LEN );
     48        check( !memcmp( peer_id, PEERID_PREFIX, 8 ) );
     49
     50        for( j = 8; j < PEER_ID_LEN; ++j )
    4951        {
    50             char tmp[2] = { pch[j], '\0' };
     52            char tmp[2] = { peer_id[j], '\0' };
    5153            val += strtoul( tmp, NULL, 36 );
    5254        }
    5355
    5456        check( ( val % 36 ) == 0 );
    55 
    56         tr_free( pch );
    5757    }
    5858
  • trunk/libtransmission/torrent.c

    r12096 r12121  
    15091509    tr_free( tor->downloadDir );
    15101510    tr_free( tor->incompleteDir );
    1511     tr_free( tor->peer_id );
    15121511
    15131512    if( tor == session->torrentList )
     
    16181617     * change the peerid. It would help sometimes if a stopped event
    16191618     * was missed to ensure that we didn't think someone was cheating. */
    1620     tr_free( tor->peer_id );
    1621     tor->peer_id = tr_peerIdNew( );
     1619    tr_peerIdInit( tor->peer_id );
    16221620    tor->isRunning = 1;
    16231621    tr_torrentSetDirty( tor );
  • trunk/libtransmission/torrent.h

    r12096 r12121  
    158158     * and in the handshake are expected to match.
    159159     */
    160     uint8_t * peer_id;
     160    uint8_t peer_id[PEER_ID_LEN+1];
    161161
    162162    /* Where the files will be when it's complete */
Note: See TracChangeset for help on using the changeset viewer.