Changeset 2924


Ignore:
Timestamp:
Aug 26, 2007, 4:44:27 PM (15 years ago)
Author:
joshe
Message:

Merge r2769 from trunk:

Use macros instead of magic numbers when creating and parsing handshake.
Make it trivial to disable extended messaging or azureus protocol at compile-time, for debugging.

Location:
branches/0.8x/libtransmission
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/0.8x/libtransmission/peer.c

    r2573 r2924  
    106106                                      than PEX_INTERVAL +
    107107                                      rand( PEX_INTERVAL / 10 ) seconds */
    108 #define PEER_SUPPORTS_EXTENDED_MESSAGES( bits ) ( (bits)[5] & 0x10 )
    109 #define PEER_SUPPORTS_AZUREUS_PROTOCOL( bits )  ( (bits)[0] & 0x80 )
     108
     109/* uncomment this to disable support for the extended messaging bit */
     110/* #define DISABLE_EXTMSGS */
     111/* uncomment this to disable support for the azureus protocol bit */
     112/* #define DISABLE_AZPROTO */
     113
     114#define HANDSHAKE_NAME          "\023BitTorrent protocol"
     115#define HANDSHAKE_NAME_LEN      20
     116#define HANDSHAKE_FLAGS_OFF     HANDSHAKE_NAME_LEN
     117#define HANDSHAKE_FLAGS_LEN     8
     118#define HANDSHAKE_HASH_OFF      ( HANDSHAKE_FLAGS_OFF + HANDSHAKE_FLAGS_LEN )
     119#define HANDSHAKE_PEERID_OFF    ( HANDSHAKE_HASH_OFF + SHA_DIGEST_LENGTH )
     120#define HANDSHAKE_SIZE          ( HANDSHAKE_PEERID_OFF + TR_ID_LEN )
     121
     122#ifdef DISABLE_EXTMSGS
     123#define HANDSHAKE_HAS_EXTMSGS( bits ) ( 0 )
     124#define HANDSHAKE_SET_EXTMSGS( bits ) ( (void)0 )
     125#else
     126#define HANDSHAKE_HAS_EXTMSGS( bits ) ( (bits)[5] & 0x10 )
     127#define HANDSHAKE_SET_EXTMSGS( bits ) ( (bits)[5] |= 0x10 )
     128#endif
     129
     130#ifdef DISABLE_AZPROTO
     131#define HANDSHAKE_HAS_AZPROTO( bits ) ( 0 )
     132#define HANDSHAKE_SET_AZPROTO( bits ) ( (void)0 )
     133#else
     134#define HANDSHAKE_HAS_AZPROTO( bits ) ( (bits)[0] & 0x80 )
     135#define HANDSHAKE_SET_AZPROTO( bits ) ( (bits)[0] |= 0x80 )
     136#endif
    110137
    111138#define PEER_MSG_CHOKE          0
     
    523550    if( PEER_STATUS_CONNECTING == peer->status )
    524551    {
    525         uint8_t buf[68];
    526         tr_info_t * inf = &tor->info;
    527 
    528         buf[0] = 19;
    529         memcpy( &buf[1], "BitTorrent protocol", 19 );
    530         memset( &buf[20], 0, 8 );
    531         buf[20] = 0x80;         /* azureus protocol */
    532         buf[25] = 0x10;         /* extended messages */
    533         memcpy( &buf[28], inf->hash, 20 );
    534         memcpy( &buf[48], tor->peer_id, 20 );
     552        uint8_t buf[HANDSHAKE_SIZE];
     553        const tr_info_t * inf;
     554
     555        inf = tr_torrentInfo( tor );
     556        assert( 68 == HANDSHAKE_SIZE );
     557        memcpy( buf, HANDSHAKE_NAME, HANDSHAKE_NAME_LEN );
     558        memset( buf + HANDSHAKE_FLAGS_OFF, 0, HANDSHAKE_FLAGS_LEN );
     559        HANDSHAKE_SET_EXTMSGS( buf + HANDSHAKE_FLAGS_OFF );
     560        HANDSHAKE_SET_AZPROTO( buf + HANDSHAKE_FLAGS_OFF );
     561        memcpy( buf + HANDSHAKE_HASH_OFF, inf->hash, SHA_DIGEST_LENGTH );
     562        memcpy( buf + HANDSHAKE_PEERID_OFF, tor->peer_id, TR_ID_LEN );
    535563
    536564        switch( tr_netSend( peer->socket, buf, 68 ) )
    537565        {
    538             case 68:
     566            case HANDSHAKE_SIZE:
    539567                peer_dbg( "SEND handshake" );
    540568                peer->status = PEER_STATUS_HANDSHAKE;
  • branches/0.8x/libtransmission/peerparse.h

    r2555 r2924  
    564564    }
    565565
    566     if( p[0] != 19 || memcmp( &p[1], "Bit", 3 ) )
     566    if( 0 != memcmp( p, HANDSHAKE_NAME, 4 ) )
    567567    {
    568568        /* Don't wait until we get 68 bytes, this is wrong
     
    576576        return TR_ERROR;
    577577    }
    578     if( peer->pos < 68 )
     578    if( HANDSHAKE_SIZE > peer->pos )
    579579    {
    580580        return TR_OK;
    581581    }
    582     if( memcmp( &p[4], "Torrent protocol", 16 ) )
     582    if( 0 != memcmp( peer->buf, HANDSHAKE_NAME, HANDSHAKE_NAME_LEN ) )
    583583    {
    584584        peer_dbg( "GET  handshake, invalid" );
     
    591591static const uint8_t * parseBufHash( const tr_peer_t * peer )
    592592{
    593     if( 48 > peer->pos )
     593    if( HANDSHAKE_HASH_OFF + SHA_DIGEST_LENGTH > peer->pos )
    594594    {
    595595        return NULL;
     
    597597    else
    598598    {
    599         return peer->buf + 28;
     599        return peer->buf + HANDSHAKE_HASH_OFF;
    600600    }
    601601}
     
    606606    int         ii;
    607607
    608     if( memcmp( &peer->buf[28], inf->hash, SHA_DIGEST_LENGTH ) )
     608    if( 0 != memcmp( peer->buf + HANDSHAKE_HASH_OFF, inf->hash,
     609                     SHA_DIGEST_LENGTH ) )
    609610    {
    610611        peer_dbg( "GET  handshake, wrong torrent hash" );
     
    612613    }
    613614
    614     if( !memcmp( &peer->buf[48], tor->peer_id, TR_ID_LEN ) )
     615    if( 0 == memcmp( peer->buf + HANDSHAKE_PEERID_OFF, tor->peer_id,
     616                     TR_ID_LEN ) )
    615617    {
    616618        /* We are connected to ourselves... */
     
    619621    }
    620622
    621     memcpy( peer->id, &peer->buf[48], TR_ID_LEN );
     623    memcpy( peer->id, peer->buf + HANDSHAKE_PEERID_OFF, TR_ID_LEN );
    622624
    623625    for( ii = 0; ii < tor->peerCount; ii++ )
     
    634636    }
    635637
    636     if( PEER_SUPPORTS_EXTENDED_MESSAGES( &peer->buf[20] ) )
     638    if( HANDSHAKE_HAS_EXTMSGS( peer->buf + HANDSHAKE_FLAGS_OFF ) )
    637639    {
    638640        peer->status = PEER_STATUS_CONNECTED;
     
    641643                  tr_peerClient( peer ) );
    642644    }
    643     else if( PEER_SUPPORTS_AZUREUS_PROTOCOL( &peer->buf[20] ) )
     645    else if( HANDSHAKE_HAS_AZPROTO( peer->buf + HANDSHAKE_FLAGS_OFF ) )
    644646    {
    645647        peer->status  = PEER_STATUS_AZ_GIVER;
     
    703705            }
    704706
    705             if( peer->pos < 68 )
     707            if( HANDSHAKE_SIZE > peer->pos )
    706708            {
    707709                break;
     
    713715                return ret;
    714716            }
    715             buf       += 68;
    716             peer->pos -= 68;
     717            buf       += HANDSHAKE_SIZE;
     718            peer->pos -= HANDSHAKE_SIZE;
    717719
    718720            ret = sendInitial( tor, peer );
Note: See TracChangeset for help on using the changeset viewer.