From 13cac341cd298b849e6b585afb3eb1c2010b3583 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 14 Jun 2009 14:39:51 +0000 Subject: [PATCH] (trunk libT) avoid an extra malloc/free when sending the opening handshake message --- libtransmission/crypto.c | 5 ++--- libtransmission/crypto.h | 3 +-- libtransmission/handshake.c | 15 ++++++--------- libtransmission/metainfo.c | 2 +- 4 files changed, 10 insertions(+), 15 deletions(-) diff --git a/libtransmission/crypto.c b/libtransmission/crypto.c index 68a696dd8..21a320d55 100644 --- a/libtransmission/crypto.c +++ b/libtransmission/crypto.c @@ -349,10 +349,9 @@ tr_cryptoWeakRandInt( int upperBound ) } void -tr_cryptoRandBuf( unsigned char *buf, - size_t len ) +tr_cryptoRandBuf( void * buf, size_t len ) { - if( RAND_pseudo_bytes ( buf, len ) != 1 ) + if( RAND_pseudo_bytes ( (unsigned char*)buf, len ) != 1 ) logErrorFromSSL( ); } diff --git a/libtransmission/crypto.h b/libtransmission/crypto.h index ee3edc659..19672043f 100644 --- a/libtransmission/crypto.h +++ b/libtransmission/crypto.h @@ -98,8 +98,7 @@ int tr_cryptoRandInt( int n ); int tr_cryptoWeakRandInt( int n ); /** Fills a buffer with random bytes */ -void tr_cryptoRandBuf( unsigned char *buf, - size_t len ); +void tr_cryptoRandBuf( void * buf, size_t len ); char* tr_ssha1( const void * plaintext ); diff --git a/libtransmission/handshake.c b/libtransmission/handshake.c index cfe904038..4dac17e27 100644 --- a/libtransmission/handshake.c +++ b/libtransmission/handshake.c @@ -332,26 +332,23 @@ sendYa( tr_handshake * handshake ) { int len; const uint8_t * public_key; - struct evbuffer * outbuf = evbuffer_new( ); - uint8_t pad_a[PadA_MAXLEN]; + char outbuf[ KEY_LEN + PadA_MAXLEN ], *walk=outbuf; /* add our public key (Ya) */ public_key = tr_cryptoGetMyPublicKey( handshake->crypto, &len ); assert( len == KEY_LEN ); assert( public_key ); - evbuffer_add( outbuf, public_key, len ); + memcpy( walk, public_key, len ); + walk += len; /* add some bullshit padding */ len = tr_cryptoRandInt( PadA_MAXLEN ); - tr_cryptoRandBuf( pad_a, len ); - evbuffer_add( outbuf, pad_a, len ); + tr_cryptoRandBuf( walk, len ); + walk += len; /* send it */ setReadState( handshake, AWAITING_YB ); - tr_peerIoWriteBuf( handshake->io, outbuf, FALSE ); - - /* cleanup */ - evbuffer_free( outbuf ); + tr_peerIoWrite( handshake->io, outbuf, walk-outbuf, FALSE ); } static uint32_t diff --git a/libtransmission/metainfo.c b/libtransmission/metainfo.c index de7f98b1c..daecfc058 100644 --- a/libtransmission/metainfo.c +++ b/libtransmission/metainfo.c @@ -243,7 +243,7 @@ announceToScrape( const char * announce ) memcpy( walk, "scrape", 6 ); walk += 6; memcpy( walk, suffix, suffix_len ); walk += suffix_len; *walk++ = '\0'; - assert( walk - scrape == alloc_len ); + assert( walk - scrape == (int)alloc_len ); } return scrape;