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.
This commit is contained in:
Josh Elsasser 2007-08-15 19:44:13 +00:00
parent cc38142915
commit 4c1c2667b6
2 changed files with 55 additions and 25 deletions

View File

@ -105,8 +105,35 @@ static const int SWIFT_REFRESH_INTERVAL_SEC = 5;
#define PEX_INTERVAL 60 /* don't send pex messages more frequently
than PEX_INTERVAL +
rand( PEX_INTERVAL / 10 ) seconds */
#define PEER_SUPPORTS_EXTENDED_MESSAGES( bits ) ( (bits)[5] & 0x10 )
#define PEER_SUPPORTS_AZUREUS_PROTOCOL( bits ) ( (bits)[0] & 0x80 )
/* uncomment this to disable support for the extended messaging bit */
/* #define DISABLE_EXTMSGS */
/* uncomment this to disable support for the azureus protocol bit */
/* #define DISABLE_AZPROTO */
#define HANDSHAKE_NAME "\023BitTorrent protocol"
#define HANDSHAKE_NAME_LEN 20
#define HANDSHAKE_FLAGS_OFF HANDSHAKE_NAME_LEN
#define HANDSHAKE_FLAGS_LEN 8
#define HANDSHAKE_HASH_OFF ( HANDSHAKE_FLAGS_OFF + HANDSHAKE_FLAGS_LEN )
#define HANDSHAKE_PEERID_OFF ( HANDSHAKE_HASH_OFF + SHA_DIGEST_LENGTH )
#define HANDSHAKE_SIZE ( HANDSHAKE_PEERID_OFF + TR_ID_LEN )
#ifdef DISABLE_EXTMSGS
#define HANDSHAKE_HAS_EXTMSGS( bits ) ( 0 )
#define HANDSHAKE_SET_EXTMSGS( bits ) ( (void)0 )
#else
#define HANDSHAKE_HAS_EXTMSGS( bits ) ( (bits)[5] & 0x10 )
#define HANDSHAKE_SET_EXTMSGS( bits ) ( (bits)[5] |= 0x10 )
#endif
#ifdef DISABLE_AZPROTO
#define HANDSHAKE_HAS_AZPROTO( bits ) ( 0 )
#define HANDSHAKE_SET_AZPROTO( bits ) ( (void)0 )
#else
#define HANDSHAKE_HAS_AZPROTO( bits ) ( (bits)[0] & 0x80 )
#define HANDSHAKE_SET_AZPROTO( bits ) ( (bits)[0] |= 0x80 )
#endif
#define PEER_MSG_CHOKE 0
#define PEER_MSG_UNCHOKE 1
@ -522,20 +549,21 @@ int tr_peerPulse( tr_peer_t * peer )
/* Try to send handshake */
if( PEER_STATUS_CONNECTING == peer->status )
{
uint8_t buf[68];
tr_info_t * inf = &tor->info;
uint8_t buf[HANDSHAKE_SIZE];
const tr_info_t * inf;
buf[0] = 19;
memcpy( &buf[1], "BitTorrent protocol", 19 );
memset( &buf[20], 0, 8 );
buf[20] = 0x80; /* azureus protocol */
buf[25] = 0x10; /* extended messages */
memcpy( &buf[28], inf->hash, 20 );
memcpy( &buf[48], tor->peer_id, 20 );
inf = tr_torrentInfo( tor );
assert( 68 == HANDSHAKE_SIZE );
memcpy( buf, HANDSHAKE_NAME, HANDSHAKE_NAME_LEN );
memset( buf + HANDSHAKE_FLAGS_OFF, 0, HANDSHAKE_FLAGS_LEN );
HANDSHAKE_SET_EXTMSGS( buf + HANDSHAKE_FLAGS_OFF );
HANDSHAKE_SET_AZPROTO( buf + HANDSHAKE_FLAGS_OFF );
memcpy( buf + HANDSHAKE_HASH_OFF, inf->hash, SHA_DIGEST_LENGTH );
memcpy( buf + HANDSHAKE_PEERID_OFF, tor->peer_id, TR_ID_LEN );
switch( tr_netSend( peer->socket, buf, 68 ) )
{
case 68:
case HANDSHAKE_SIZE:
peer_dbg( "SEND handshake" );
peer->status = PEER_STATUS_HANDSHAKE;
break;

View File

@ -563,7 +563,7 @@ static int parseBufHeader( tr_peer_t * peer )
return TR_OK;
}
if( p[0] != 19 || memcmp( &p[1], "Bit", 3 ) )
if( 0 != memcmp( p, HANDSHAKE_NAME, 4 ) )
{
/* Don't wait until we get 68 bytes, this is wrong
already */
@ -575,11 +575,11 @@ static int parseBufHeader( tr_peer_t * peer )
tr_netSend( peer->socket, badproto_tinfoil, sizeof badproto_tinfoil - 1 );
return TR_ERROR;
}
if( peer->pos < 68 )
if( HANDSHAKE_SIZE > peer->pos )
{
return TR_OK;
}
if( memcmp( &p[4], "Torrent protocol", 16 ) )
if( 0 != memcmp( peer->buf, HANDSHAKE_NAME, HANDSHAKE_NAME_LEN ) )
{
peer_dbg( "GET handshake, invalid" );
return TR_ERROR;
@ -590,13 +590,13 @@ static int parseBufHeader( tr_peer_t * peer )
static const uint8_t * parseBufHash( const tr_peer_t * peer )
{
if( 48 > peer->pos )
if( HANDSHAKE_HASH_OFF + SHA_DIGEST_LENGTH > peer->pos )
{
return NULL;
}
else
{
return peer->buf + 28;
return peer->buf + HANDSHAKE_HASH_OFF;
}
}
@ -605,20 +605,22 @@ static int parseHandshake( tr_torrent_t * tor, tr_peer_t * peer )
tr_info_t * inf = &tor->info;
int ii;
if( memcmp( &peer->buf[28], inf->hash, SHA_DIGEST_LENGTH ) )
if( 0 != memcmp( peer->buf + HANDSHAKE_HASH_OFF, inf->hash,
SHA_DIGEST_LENGTH ) )
{
peer_dbg( "GET handshake, wrong torrent hash" );
return TR_ERROR;
}
if( !memcmp( &peer->buf[48], tor->peer_id, TR_ID_LEN ) )
if( 0 == memcmp( peer->buf + HANDSHAKE_PEERID_OFF, tor->peer_id,
TR_ID_LEN ) )
{
/* We are connected to ourselves... */
peer_dbg( "GET handshake, that is us" );
return TR_ERROR;
}
memcpy( peer->id, &peer->buf[48], TR_ID_LEN );
memcpy( peer->id, peer->buf + HANDSHAKE_PEERID_OFF, TR_ID_LEN );
for( ii = 0; ii < tor->peerCount; ii++ )
{
@ -633,14 +635,14 @@ static int parseHandshake( tr_torrent_t * tor, tr_peer_t * peer )
}
}
if( PEER_SUPPORTS_EXTENDED_MESSAGES( &peer->buf[20] ) )
if( HANDSHAKE_HAS_EXTMSGS( peer->buf + HANDSHAKE_FLAGS_OFF ) )
{
peer->status = PEER_STATUS_CONNECTED;
peer->extStatus = EXTENDED_SUPPORTED;
peer_dbg( "GET handshake, ok (%s) extended messaging supported",
tr_peerClient( peer ) );
}
else if( PEER_SUPPORTS_AZUREUS_PROTOCOL( &peer->buf[20] ) )
else if( HANDSHAKE_HAS_AZPROTO( peer->buf + HANDSHAKE_FLAGS_OFF ) )
{
peer->status = PEER_STATUS_AZ_GIVER;
peer->azproto = 1;
@ -702,7 +704,7 @@ static int parseBuf( tr_torrent_t * tor, tr_peer_t * peer )
return ret;
}
if( peer->pos < 68 )
if( HANDSHAKE_SIZE > peer->pos )
{
break;
}
@ -712,8 +714,8 @@ static int parseBuf( tr_torrent_t * tor, tr_peer_t * peer )
{
return ret;
}
buf += 68;
peer->pos -= 68;
buf += HANDSHAKE_SIZE;
peer->pos -= HANDSHAKE_SIZE;
ret = sendInitial( tor, peer );
if( ret )