From 3d0e98b2c7a77b5fe5cb4847a15f7565246dc0df Mon Sep 17 00:00:00 2001 From: Josh Elsasser Date: Wed, 15 Aug 2007 23:03:50 +0000 Subject: [PATCH] Save client name from azureus or extended handshake, overriding name deduced from peer id. --- libtransmission/peer.c | 1 + libtransmission/peeraz.h | 46 +++++++++++++++++++++++++-------------- libtransmission/peerext.h | 37 ++++++++++++++++++++++--------- 3 files changed, 58 insertions(+), 26 deletions(-) diff --git a/libtransmission/peer.c b/libtransmission/peer.c index 1ec7c6b7a..868f362c3 100644 --- a/libtransmission/peer.c +++ b/libtransmission/peer.c @@ -259,6 +259,7 @@ struct tr_peer_s tr_ratecontrol_t * upload; char * client; + int extclient; int64_t credit; }; diff --git a/libtransmission/peeraz.h b/libtransmission/peeraz.h index 633e5a8f3..3bb88404f 100644 --- a/libtransmission/peeraz.h +++ b/libtransmission/peeraz.h @@ -341,9 +341,11 @@ parseAZMessageHeader( tr_peer_t * peer, uint8_t * buf, int len, static int parseAZHandshake( tr_peer_t * peer, uint8_t * buf, int len ) { - benc_val_t val, * sub, * dict, * subsub; + benc_val_t val, * sub, * sub2, * dict, * subsub; tr_bitfield_t * msgs; - int ii, idx; + int ii, idx, newclient; + size_t size; + char * client; if( tr_bencLoad( buf, len, &val, NULL ) ) { @@ -358,28 +360,37 @@ parseAZHandshake( tr_peer_t * peer, uint8_t * buf, int len ) return TR_ERROR; } -#if 0 /* ugh, we have to deal with encoding if we do this */ /* get peer's client name */ - sub = tr_bencDictFind( &val, "client" ); - sub2 = tr_bencDictFind( &val, "version" ); - if( NULL != sub && TYPE_STR == sub->type && - NULL != sub2 && TYPE_STR == sub->type ) + newclient = 0; + if( !peer->extclient ) { - if( NULL == peer->client || - ( 0 != strncmp( peer->client, sub->val.s.s, sub->val.s.i ) || - ' ' != peer->client[sub->val.s.i] || - 0 != strcmp( peer->client + sub->val.s.i + 1, sub2->val.s.s ) ) ) + sub = tr_bencDictFind( &val, "client" ); + sub2 = tr_bencDictFind( &val, "version" ); + if( NULL != sub && TYPE_STR == sub->type && + NULL != sub2 && TYPE_STR == sub->type ) { - client = NULL; - asprintf( &client, "%s %s", sub->val.s.s, sub2->val.s.s ); - if( NULL != client ) + size = bufsize_utf8( sub->val.s.s, NULL ) + + bufsize_utf8( sub2->val.s.s, NULL ); + client = calloc( size, 1 ); + if( NULL == client ) + { + tr_bencFree( &val ); + return TR_ERROR; + } + strlcat_utf8( client, sub->val.s.s, size, 0 ); + strlcat( client, " ", size ); + strlcat_utf8( client, sub2->val.s.s, size, 0 ); + if( NULL == peer->client || 0 != strcmp( client, peer->client ) ) { free( peer->client ); peer->client = client; + newclient = 1; } + else + free( client ); + peer->extclient = 1; } } -#endif /* get the peer's listening port */ sub = tr_bencDictFind( &val, "tcp_port" ); @@ -400,7 +411,10 @@ parseAZHandshake( tr_peer_t * peer, uint8_t * buf, int len ) return TR_ERROR; } - peer_dbg( "GET azureus-handshake, ok" ); + if( newclient ) + peer_dbg( "GET azureus-handshake, ok (%s)", peer->client ); + else + peer_dbg( "GET azureus-handshake, ok" ); /* fill bitmask with supported message info */ msgs = tr_bitfieldNew( azmsgCount() ); diff --git a/libtransmission/peerext.h b/libtransmission/peerext.h index 24384ebb8..16251db39 100644 --- a/libtransmission/peerext.h +++ b/libtransmission/peerext.h @@ -219,7 +219,8 @@ static int parseExtendedHandshake( tr_peer_t * peer, uint8_t * buf, int len ) { benc_val_t val, * sub; - int dbgport, dbgpex; + int dbgport, dbgpex, dbgclient, grow; + size_t size; if( tr_bencLoad( buf, len, &val, NULL ) ) { @@ -250,20 +251,31 @@ parseExtendedHandshake( tr_peer_t * peer, uint8_t * buf, int len ) } } -#if 0 /* ugh, we have to deal with encoding if we do this */ /* get peer's client name */ - sub = tr_bencDictFind( &val, "v" ); - if( NULL != sub && TYPE_STR == sub->type && - ( NULL == peer->client || 0 != strcmp( sub->val.s.s, peer->client ) ) ) + dbgclient = 0; + if( !peer->extclient ) { - client = tr_bencStealStr( sub ); - if( NULL != client ) + sub = tr_bencDictFind( &val, "v" ); + if( NULL != sub && TYPE_STR == sub->type ) { + dbgclient = 1; free( peer->client ); - peer->client = client; + size = bufsize_utf8( sub->val.s.s, &grow ); + if( grow ) + { + peer->client = calloc( size, 1 ); + if( NULL == peer->client ) + { + tr_bencFree( &val ); + return TR_ERROR; + } + strlcat_utf8( peer->client, sub->val.s.s, size, 0 ); + } + else + peer->client = tr_bencStealStr( sub ); + peer->extclient = 1; } } -#endif /* get peer's listening port */ sub = tr_bencDictFind( &val, "p" ); @@ -275,7 +287,12 @@ parseExtendedHandshake( tr_peer_t * peer, uint8_t * buf, int len ) dbgport = sub->val.i; } - peer_dbg( "GET extended-handshake, ok port=%i pex=%i", dbgport, dbgpex ); + if( dbgclient ) + peer_dbg( "GET extended-handshake, ok port=%d pex=%d client=%s", + dbgport, dbgpex, peer->client ); + else + peer_dbg( "GET extended-handshake, ok port=%d pex=%d", + dbgport, dbgpex ); tr_bencFree( &val ); return TR_OK;