(trunk libT) more heap pruning: use composition rather than aggregation for the tr_crypto object owned by tr_peerIo.
This commit is contained in:
parent
bbc35bd546
commit
244bd7bc1c
|
@ -77,20 +77,6 @@ static const uint8_t dh_P[PRIME_LEN] =
|
|||
|
||||
static const uint8_t dh_G[] = { 2 };
|
||||
|
||||
/** @brief Holds state information for encrypted peer communications */
|
||||
struct tr_crypto
|
||||
{
|
||||
RC4_KEY dec_key;
|
||||
RC4_KEY enc_key;
|
||||
uint8_t torrentHash[SHA_DIGEST_LENGTH];
|
||||
bool isIncoming;
|
||||
bool torrentHashIsSet;
|
||||
bool mySecretIsSet;
|
||||
uint8_t myPublicKey[KEY_LEN];
|
||||
uint8_t mySecret[KEY_LEN];
|
||||
DH * dh;
|
||||
};
|
||||
|
||||
/**
|
||||
***
|
||||
**/
|
||||
|
@ -143,26 +129,21 @@ ensureKeyExists( tr_crypto * crypto)
|
|||
}
|
||||
}
|
||||
|
||||
tr_crypto *
|
||||
tr_cryptoNew( const uint8_t * torrentHash,
|
||||
int isIncoming )
|
||||
void
|
||||
tr_cryptoConstruct( tr_crypto * crypto, const uint8_t * torrentHash, bool isIncoming )
|
||||
{
|
||||
tr_crypto * crypto;
|
||||
memset( crypto, 0, sizeof ( tr_crypto ) );
|
||||
|
||||
crypto = tr_new0( tr_crypto, 1 );
|
||||
crypto->isIncoming = isIncoming ? 1 : 0;
|
||||
tr_cryptoSetTorrentHash( crypto, torrentHash );
|
||||
crypto->dh = NULL;
|
||||
|
||||
return crypto;
|
||||
crypto->isIncoming = isIncoming;
|
||||
tr_cryptoSetTorrentHash( crypto, torrentHash );
|
||||
}
|
||||
|
||||
void
|
||||
tr_cryptoFree( tr_crypto * crypto )
|
||||
tr_cryptoDestruct( tr_crypto * crypto )
|
||||
{
|
||||
if( crypto->dh != NULL )
|
||||
DH_free( crypto->dh );
|
||||
tr_free( crypto );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -26,13 +26,34 @@
|
|||
*** @{
|
||||
**/
|
||||
|
||||
typedef struct tr_crypto tr_crypto;
|
||||
#include <openssl/dh.h> /* RC4_KEY */
|
||||
#include <openssl/rc4.h> /* DH */
|
||||
|
||||
/** @brief create a new tr_crypto object */
|
||||
tr_crypto* tr_cryptoNew( const uint8_t * torrentHash, int isIncoming );
|
||||
enum
|
||||
{
|
||||
KEY_LEN = 96
|
||||
};
|
||||
|
||||
/** @brief destroy an existing tr_crypto object */
|
||||
void tr_cryptoFree( tr_crypto * crypto );
|
||||
/** @brief Holds state information for encrypted peer communications */
|
||||
typedef struct
|
||||
{
|
||||
RC4_KEY dec_key;
|
||||
RC4_KEY enc_key;
|
||||
DH * dh;
|
||||
uint8_t myPublicKey[KEY_LEN];
|
||||
uint8_t mySecret[KEY_LEN];
|
||||
uint8_t torrentHash[SHA_DIGEST_LENGTH];
|
||||
bool isIncoming;
|
||||
bool torrentHashIsSet;
|
||||
bool mySecretIsSet;
|
||||
}
|
||||
tr_crypto;
|
||||
|
||||
/** @brief construct a new tr_crypto object */
|
||||
void tr_cryptoConstruct( tr_crypto * crypto, const uint8_t * torrentHash, bool isIncoming );
|
||||
|
||||
/** @brief destruct an existing tr_crypto object */
|
||||
void tr_cryptoDestruct( tr_crypto * crypto );
|
||||
|
||||
|
||||
void tr_cryptoSetTorrentHash( tr_crypto * crypto, const uint8_t * torrentHash );
|
||||
|
|
|
@ -55,7 +55,6 @@ enum
|
|||
PadC_MAXLEN = 512,
|
||||
PadD_MAXLEN = 512,
|
||||
VC_LENGTH = 8,
|
||||
KEY_LEN = 96,
|
||||
CRYPTO_PROVIDE_PLAINTEXT = 1,
|
||||
CRYPTO_PROVIDE_CRYPTO = 2,
|
||||
|
||||
|
|
|
@ -596,7 +596,7 @@ tr_peerIoNew( tr_session * session,
|
|||
io = tr_new0( tr_peerIo, 1 );
|
||||
io->magicNumber = PEER_IO_MAGIC_NUMBER;
|
||||
io->refCount = 1;
|
||||
io->crypto = tr_cryptoNew( torrentHash, isIncoming );
|
||||
tr_cryptoConstruct( &io->crypto, torrentHash, isIncoming );
|
||||
io->session = session;
|
||||
io->addr = *addr;
|
||||
io->isSeed = isSeed;
|
||||
|
@ -808,7 +808,7 @@ io_dtor( void * vio )
|
|||
evbuffer_free( io->outbuf );
|
||||
evbuffer_free( io->inbuf );
|
||||
io_close_socket( io );
|
||||
tr_cryptoFree( io->crypto );
|
||||
tr_cryptoDestruct( &io->crypto );
|
||||
|
||||
while( io->outbuf_datatypes != NULL )
|
||||
peer_io_pull_datatype( io );
|
||||
|
@ -939,25 +939,23 @@ tr_peerIoSetTorrentHash( tr_peerIo * io,
|
|||
{
|
||||
assert( tr_isPeerIo( io ) );
|
||||
|
||||
tr_cryptoSetTorrentHash( io->crypto, hash );
|
||||
tr_cryptoSetTorrentHash( &io->crypto, hash );
|
||||
}
|
||||
|
||||
const uint8_t*
|
||||
tr_peerIoGetTorrentHash( tr_peerIo * io )
|
||||
{
|
||||
assert( tr_isPeerIo( io ) );
|
||||
assert( io->crypto );
|
||||
|
||||
return tr_cryptoGetTorrentHash( io->crypto );
|
||||
return tr_cryptoGetTorrentHash( &io->crypto );
|
||||
}
|
||||
|
||||
int
|
||||
tr_peerIoHasTorrentHash( const tr_peerIo * io )
|
||||
{
|
||||
assert( tr_isPeerIo( io ) );
|
||||
assert( io->crypto );
|
||||
|
||||
return tr_cryptoHasTorrentHash( io->crypto );
|
||||
return tr_cryptoHasTorrentHash( &io->crypto );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -965,8 +963,7 @@ tr_peerIoHasTorrentHash( const tr_peerIo * io )
|
|||
**/
|
||||
|
||||
void
|
||||
tr_peerIoSetPeersId( tr_peerIo * io,
|
||||
const uint8_t * peer_id )
|
||||
tr_peerIoSetPeersId( tr_peerIo * io, const uint8_t * peer_id )
|
||||
{
|
||||
assert( tr_isPeerIo( io ) );
|
||||
|
||||
|
@ -1045,7 +1042,7 @@ maybeEncryptBuffer( tr_peerIo * io, struct evbuffer * buf )
|
|||
evbuffer_ptr_set( buf, &pos, 0, EVBUFFER_PTR_SET );
|
||||
do {
|
||||
evbuffer_peek( buf, -1, &pos, &iovec, 1 );
|
||||
tr_cryptoEncrypt( io->crypto, iovec.iov_len, iovec.iov_base, iovec.iov_base );
|
||||
tr_cryptoEncrypt( &io->crypto, iovec.iov_len, iovec.iov_base, iovec.iov_base );
|
||||
} while( !evbuffer_ptr_set( buf, &pos, iovec.iov_len, EVBUFFER_PTR_ADD ) );
|
||||
}
|
||||
}
|
||||
|
@ -1125,7 +1122,7 @@ tr_peerIoReadBytesToBuf( tr_peerIo * io, struct evbuffer * inbuf, struct evbuffe
|
|||
evbuffer_ptr_set( outbuf, &pos, old_length, EVBUFFER_PTR_SET );
|
||||
do {
|
||||
evbuffer_peek( outbuf, byteCount, &pos, &iovec, 1 );
|
||||
tr_cryptoDecrypt( io->crypto, iovec.iov_len, iovec.iov_base, iovec.iov_base );
|
||||
tr_cryptoDecrypt( &io->crypto, iovec.iov_len, iovec.iov_base, iovec.iov_base );
|
||||
byteCount -= iovec.iov_len;
|
||||
} while( !evbuffer_ptr_set( outbuf, &pos, iovec.iov_len, EVBUFFER_PTR_ADD ) );
|
||||
}
|
||||
|
@ -1145,7 +1142,7 @@ tr_peerIoReadBytes( tr_peerIo * io, struct evbuffer * inbuf, void * bytes, size_
|
|||
|
||||
case PEER_ENCRYPTION_RC4:
|
||||
evbuffer_remove( inbuf, bytes, byteCount );
|
||||
tr_cryptoDecrypt( io->crypto, byteCount, bytes, bytes );
|
||||
tr_cryptoDecrypt( &io->crypto, byteCount, bytes, bytes );
|
||||
break;
|
||||
|
||||
default:
|
||||
|
|
|
@ -25,12 +25,12 @@
|
|||
|
||||
#include "transmission.h"
|
||||
#include "bandwidth.h"
|
||||
#include "crypto.h"
|
||||
#include "net.h" /* tr_address */
|
||||
#include "utils.h" /* tr_time() */
|
||||
|
||||
struct evbuffer;
|
||||
struct tr_bandwidth;
|
||||
struct tr_crypto;
|
||||
struct tr_datatype;
|
||||
struct tr_peerIo;
|
||||
|
||||
|
@ -107,7 +107,7 @@ typedef struct tr_peerIo
|
|||
void * userData;
|
||||
|
||||
struct tr_bandwidth bandwidth;
|
||||
struct tr_crypto * crypto;
|
||||
tr_crypto crypto;
|
||||
|
||||
struct evbuffer * inbuf;
|
||||
struct evbuffer * outbuf;
|
||||
|
@ -282,9 +282,9 @@ void tr_peerIoWriteBuf ( tr_peerIo * io,
|
|||
***
|
||||
**/
|
||||
|
||||
static inline struct tr_crypto * tr_peerIoGetCrypto( tr_peerIo * io )
|
||||
static inline tr_crypto * tr_peerIoGetCrypto( tr_peerIo * io )
|
||||
{
|
||||
return io->crypto;
|
||||
return &io->crypto;
|
||||
}
|
||||
|
||||
void tr_peerIoSetEncryption( tr_peerIo * io, tr_encryption_type encryption_type );
|
||||
|
|
Loading…
Reference in New Issue