Changeset 12365


Ignore:
Timestamp:
Apr 17, 2011, 5:22:50 AM (11 years ago)
Author:
jordan
Message:

(trunk libT) more heap pruning: use composition rather than aggregation for the tr_crypto object owned by tr_peerIo.

Location:
trunk/libtransmission
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/crypto.c

    r12223 r12365  
    7878static const uint8_t dh_G[] = { 2 };
    7979
    80 /** @brief Holds state information for encrypted peer communications */
    81 struct tr_crypto
    82 {
    83     RC4_KEY         dec_key;
    84     RC4_KEY         enc_key;
    85     uint8_t         torrentHash[SHA_DIGEST_LENGTH];
    86     bool            isIncoming;
    87     bool            torrentHashIsSet;
    88     bool            mySecretIsSet;
    89     uint8_t         myPublicKey[KEY_LEN];
    90     uint8_t         mySecret[KEY_LEN];
    91     DH *            dh;
    92 };
    93 
    9480/**
    9581***
     
    144130}
    145131
    146 tr_crypto *
    147 tr_cryptoNew( const uint8_t * torrentHash,
    148               int             isIncoming )
    149 {
    150     tr_crypto * crypto;
    151 
    152     crypto = tr_new0( tr_crypto, 1 );
    153     crypto->isIncoming = isIncoming ? 1 : 0;
     132void
     133tr_cryptoConstruct( tr_crypto * crypto, const uint8_t * torrentHash, bool isIncoming )
     134{
     135    memset( crypto, 0, sizeof ( tr_crypto ) );
     136
     137    crypto->dh = NULL;
     138    crypto->isIncoming = isIncoming;
    154139    tr_cryptoSetTorrentHash( crypto, torrentHash );
    155     crypto->dh = NULL;
    156 
    157     return crypto;
    158 }
    159 
    160 void
    161 tr_cryptoFree( tr_crypto * crypto )
     140}
     141
     142void
     143tr_cryptoDestruct( tr_crypto * crypto )
    162144{
    163145    if( crypto->dh != NULL )
    164146        DH_free( crypto->dh );
    165     tr_free( crypto );
    166147}
    167148
  • trunk/libtransmission/crypto.h

    r12226 r12365  
    2727**/
    2828
    29 typedef struct tr_crypto tr_crypto;
     29#include <openssl/dh.h> /* RC4_KEY */
     30#include <openssl/rc4.h> /* DH */
    3031
    31 /** @brief create a new tr_crypto object */
    32 tr_crypto*  tr_cryptoNew( const uint8_t * torrentHash, int isIncoming );
     32enum
     33{
     34    KEY_LEN = 96
     35};
    3336
    34 /** @brief destroy an existing tr_crypto object */
    35 void           tr_cryptoFree( tr_crypto * crypto );
     37/** @brief Holds state information for encrypted peer communications */
     38typedef struct
     39{
     40    RC4_KEY         dec_key;
     41    RC4_KEY         enc_key;
     42    DH *            dh;
     43    uint8_t         myPublicKey[KEY_LEN];
     44    uint8_t         mySecret[KEY_LEN];
     45    uint8_t         torrentHash[SHA_DIGEST_LENGTH];
     46    bool            isIncoming;
     47    bool            torrentHashIsSet;
     48    bool            mySecretIsSet;
     49}
     50tr_crypto;
     51
     52/** @brief construct a new tr_crypto object */
     53void tr_cryptoConstruct( tr_crypto * crypto, const uint8_t * torrentHash, bool isIncoming );
     54
     55/** @brief destruct an existing tr_crypto object */
     56void tr_cryptoDestruct( tr_crypto * crypto );
    3657
    3758
  • trunk/libtransmission/handshake.c

    r12344 r12365  
    5656    PadD_MAXLEN                    = 512,
    5757    VC_LENGTH                      = 8,
    58     KEY_LEN                        = 96,
    5958    CRYPTO_PROVIDE_PLAINTEXT       = 1,
    6059    CRYPTO_PROVIDE_CRYPTO          = 2,
  • trunk/libtransmission/peer-io.c

    r12343 r12365  
    597597    io->magicNumber = PEER_IO_MAGIC_NUMBER;
    598598    io->refCount = 1;
    599     io->crypto = tr_cryptoNew( torrentHash, isIncoming );
     599    tr_cryptoConstruct( &io->crypto, torrentHash, isIncoming );
    600600    io->session = session;
    601601    io->addr = *addr;
     
    809809    evbuffer_free( io->inbuf );
    810810    io_close_socket( io );
    811     tr_cryptoFree( io->crypto );
     811    tr_cryptoDestruct( &io->crypto );
    812812
    813813    while( io->outbuf_datatypes != NULL )
     
    940940    assert( tr_isPeerIo( io ) );
    941941
    942     tr_cryptoSetTorrentHash( io->crypto, hash );
     942    tr_cryptoSetTorrentHash( &io->crypto, hash );
    943943}
    944944
     
    947947{
    948948    assert( tr_isPeerIo( io ) );
    949     assert( io->crypto );
    950 
    951     return tr_cryptoGetTorrentHash( io->crypto );
     949
     950    return tr_cryptoGetTorrentHash( &io->crypto );
    952951}
    953952
     
    956955{
    957956    assert( tr_isPeerIo( io ) );
    958     assert( io->crypto );
    959 
    960     return tr_cryptoHasTorrentHash( io->crypto );
     957
     958    return tr_cryptoHasTorrentHash( &io->crypto );
    961959}
    962960
     
    966964
    967965void
    968 tr_peerIoSetPeersId( tr_peerIo *     io,
    969                      const uint8_t * peer_id )
     966tr_peerIoSetPeersId( tr_peerIo * io, const uint8_t * peer_id )
    970967{
    971968    assert( tr_isPeerIo( io ) );
     
    10461043        do {
    10471044            evbuffer_peek( buf, -1, &pos, &iovec, 1 );
    1048             tr_cryptoEncrypt( io->crypto, iovec.iov_len, iovec.iov_base, iovec.iov_base );
     1045            tr_cryptoEncrypt( &io->crypto, iovec.iov_len, iovec.iov_base, iovec.iov_base );
    10491046        } while( !evbuffer_ptr_set( buf, &pos, iovec.iov_len, EVBUFFER_PTR_ADD ) );
    10501047    }
     
    11261123        do {
    11271124            evbuffer_peek( outbuf, byteCount, &pos, &iovec, 1 );
    1128             tr_cryptoDecrypt( io->crypto, iovec.iov_len, iovec.iov_base, iovec.iov_base );
     1125            tr_cryptoDecrypt( &io->crypto, iovec.iov_len, iovec.iov_base, iovec.iov_base );
    11291126            byteCount -= iovec.iov_len;
    11301127        } while( !evbuffer_ptr_set( outbuf, &pos, iovec.iov_len, EVBUFFER_PTR_ADD ) );
     
    11461143        case PEER_ENCRYPTION_RC4:
    11471144            evbuffer_remove( inbuf, bytes, byteCount );
    1148             tr_cryptoDecrypt( io->crypto, byteCount, bytes, bytes );
     1145            tr_cryptoDecrypt( &io->crypto, byteCount, bytes, bytes );
    11491146            break;
    11501147
  • trunk/libtransmission/peer-io.h

    r12343 r12365  
    2626#include "transmission.h"
    2727#include "bandwidth.h"
     28#include "crypto.h"
    2829#include "net.h" /* tr_address */
    2930#include "utils.h" /* tr_time() */
     
    3132struct evbuffer;
    3233struct tr_bandwidth;
    33 struct tr_crypto;
    3434struct tr_datatype;
    3535struct tr_peerIo;
     
    108108
    109109    struct tr_bandwidth   bandwidth;
    110     struct tr_crypto    * crypto;
     110    tr_crypto            crypto;
    111111
    112112    struct evbuffer     * inbuf;
     
    283283**/
    284284
    285 static inline struct tr_crypto * tr_peerIoGetCrypto( tr_peerIo * io )
    286 {
    287     return io->crypto;
     285static inline tr_crypto * tr_peerIoGetCrypto( tr_peerIo * io )
     286{
     287    return &io->crypto;
    288288}
    289289
Note: See TracChangeset for help on using the changeset viewer.