Changeset 3759


Ignore:
Timestamp:
Nov 8, 2007, 9:20:08 PM (15 years ago)
Author:
charles
Message:

added `plaintext preferred' encryption option.

Location:
trunk/libtransmission
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/handshake.c

    r3753 r3759  
    9090    unsigned int havePeerID                   : 1;
    9191    unsigned int haveSentBitTorrentHandshake  : 1;
    92     unsigned int allowUnencryptedPeers        : 1;
    9392    tr_peerIo * io;
    9493    tr_crypto * crypto;
     
    9796    uint8_t mySecret[KEY_LEN];
    9897    uint8_t state;
     98    tr_encryption_mode encryptionMode;
    9999    uint16_t pad_c_len;
    100100    uint16_t pad_d_len;
     
    325325
    326326static uint32_t
    327 getCryptoProvide( const tr_handshake * handshake UNUSED )
    328 {
    329     uint32_t i = 0;
    330 
    331     i |= CRYPTO_PROVIDE_CRYPTO; /* always allow crypto */
    332 
    333 #if 0
    334     /* by the time we send a crypto_provide, we _know_
    335      * the peer supports encryption. */
    336     if( handshake->allowUnencryptedPeers )
    337         i |= CRYPTO_PROVIDE_PLAINTEXT;
    338 #endif
    339 
    340    return i;
     327getCryptoProvide( const tr_handshake * handshake )
     328{
     329    uint32_t provide = 0;
     330
     331    switch( handshake->encryptionMode )
     332    {
     333        case TR_ENCRYPTION_REQUIRED:
     334        case TR_ENCRYPTION_PREFERRED:
     335            provide |= CRYPTO_PROVIDE_CRYPTO;
     336            break;
     337
     338        case TR_PLAINTEXT_PREFERRED:
     339            provide |= CRYPTO_PROVIDE_CRYPTO | CRYPTO_PROVIDE_PLAINTEXT;
     340            break;
     341    }
     342
     343    return provide;
     344}
     345
     346static uint32_t
     347getCryptoSelect( const tr_handshake * handshake, uint32_t crypto_provide )
     348{
     349    uint32_t choices[4];
     350    int i, nChoices=0;
     351
     352    switch( handshake->encryptionMode )
     353    {
     354        case TR_ENCRYPTION_REQUIRED:
     355            choices[nChoices++] = CRYPTO_PROVIDE_CRYPTO;
     356            break;
     357
     358        case TR_ENCRYPTION_PREFERRED:
     359            choices[nChoices++] = CRYPTO_PROVIDE_CRYPTO;
     360            choices[nChoices++] = CRYPTO_PROVIDE_PLAINTEXT;
     361            break;
     362
     363        case TR_PLAINTEXT_PREFERRED:
     364            choices[nChoices++] = CRYPTO_PROVIDE_PLAINTEXT;
     365            choices[nChoices++] = CRYPTO_PROVIDE_CRYPTO;
     366            break;
     367    }
     368
     369    for( i=0; i<nChoices; ++i )
     370        if( crypto_provide & choices[i] )
     371            return choices[i];
     372
     373    return 0;
    341374}
    342375
     
    545578        tr_peerIoSetEncryption( handshake->io, PEER_ENCRYPTION_NONE );
    546579
    547         if( !handshake->allowUnencryptedPeers )
     580        if( handshake->encryptionMode == TR_ENCRYPTION_REQUIRED )
    548581        {
    549582            dbgmsg( handshake, "peer is unencrypted, and we're disallowing that" );
     
    841874    }
    842875
    843 dbgmsg( handshake, "sending crypto_select" );
    844876    /* send crypto_select */
    845     {
    846 dbgmsg( handshake, "handshake->crypto_provide is %d", (int)handshake->crypto_provide );
    847         if( handshake->crypto_provide & CRYPTO_PROVIDE_CRYPTO )
    848             crypto_select = CRYPTO_PROVIDE_CRYPTO;
    849         else if( handshake->allowUnencryptedPeers )
    850             crypto_select = CRYPTO_PROVIDE_PLAINTEXT;
    851         else {
    852 dbgmsg( handshake, "gronk..." );
    853             evbuffer_free( outbuf );
    854             tr_handshakeDone( handshake, FALSE );
    855             return READ_DONE;
    856         }
    857 
    858 dbgmsg( handshake, "we select crypto_select as %d...", (int)crypto_select );
     877    crypto_select = getCryptoSelect( handshake, handshake->crypto_provide );
     878    if( crypto_select ) {
     879        dbgmsg( handshake, "selecting crypto mode '%d'", (int)crypto_select );
    859880        tr_peerIoWriteUint32( handshake->io, outbuf, crypto_select );
     881    } else {
     882        dbgmsg( handshake, "peer didn't offer an encryption mode we like." );
     883        evbuffer_free( outbuf );
     884        tr_handshakeDone( handshake, FALSE );
     885        return READ_DONE;
    860886    }
    861887
     
    965991     * try a plaintext handshake */
    966992    if(    ( ( handshake->state == AWAITING_YB ) || ( handshake->state == AWAITING_VC ) )
    967         && ( handshake->allowUnencryptedPeers )
     993        && ( handshake->encryptionMode != TR_ENCRYPTION_REQUIRED )
    968994        && ( !tr_peerIoReconnect( handshake->io ) ) )
    969995    {
     
    9911017tr_handshake*
    9921018tr_handshakeNew( tr_peerIo           * io,
    993                  tr_encryption_mode    encryption_mode,
     1019                 tr_encryption_mode    encryptionMode,
    9941020                 handshakeDoneCB       doneCB,
    9951021                 void                * doneUserData )
     
    10001026    handshake->io = io;
    10011027    handshake->crypto = tr_peerIoGetCrypto( io );
    1002     handshake->allowUnencryptedPeers = encryption_mode!=TR_ENCRYPTION_REQUIRED;
     1028    handshake->encryptionMode = encryptionMode;
    10031029    handshake->doneCB = doneCB;
    10041030    handshake->doneUserData = doneUserData;
     
    10101036    if( tr_peerIoIsIncoming( handshake->io ) )
    10111037        setReadState( handshake, AWAITING_HANDSHAKE );
    1012     else
     1038    else if( encryptionMode != TR_PLAINTEXT_PREFERRED )
    10131039        sendYa( handshake );
     1040    else {
     1041        int msgSize;
     1042        uint8_t * msg = buildHandshakeMessage( handshake, &msgSize );
     1043        handshake->haveSentBitTorrentHandshake = 1;
     1044        setReadState( handshake, AWAITING_HANDSHAKE );
     1045        tr_peerIoWrite( handshake->io, msg, msgSize );
     1046        tr_free( msg );
     1047    }
    10141048
    10151049    return handshake;
  • trunk/libtransmission/transmission.h

    r3666 r3759  
    117117typedef enum
    118118{
     119    TR_PLAINTEXT_PREFERRED,
    119120    TR_ENCRYPTION_PREFERRED,
    120121    TR_ENCRYPTION_REQUIRED
Note: See TracChangeset for help on using the changeset viewer.