Ignore:
Timestamp:
Dec 22, 2008, 12:51:14 AM (12 years ago)
Author:
charles
Message:

(1.4x libT) backport handshake, peer, bandwidth, peer-io to 1.4x.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/1.4x/libtransmission/handshake.c

    r7341 r7455  
    11/*
    2  * This file Copyright (C) 2007-2008 Charles Kerr <charles@rebelbase.com>
     2 * This file Copyright (C) 2007-2008 Charles Kerr <charles@transmissionbt.com>
    33 *
    44 * This file is licensed by the GPL version 2.  Works owned by the
     
    2525#include "crypto.h"
    2626#include "handshake.h"
    27 #include "iobuf.h"
    2827#include "peer-io.h"
    2928#include "peer-mgr.h"
     
    3433/* enable LibTransmission extension protocol */
    3534#define ENABLE_LTEP * /
    36 
    37 /* enable fast peers extension protocol */
    38 /* #define ENABLE_FASTPEER */
     35/* fast extensions */
     36#define ENABLE_FAST * /
    3937
    4038/***
     
    7371#endif
    7472
    75 #ifdef ENABLE_FASTPEER
    76  #define HANDSHAKE_HAS_FASTEXT( bits ) ( ( ( bits )[7] & 0x04 ) ? 1 : 0 )
    77  #define HANDSHAKE_SET_FASTEXT( bits ) ( ( bits )[7] |= 0x04 )
    78 #else
    79  #define HANDSHAKE_HAS_FASTEXT( bits ) ( 0 )
    80  #define HANDSHAKE_SET_FASTEXT( bits ) ( (void)0 )
    81 #endif
     73#ifdef ENABLE_FAST 
     74 #define HANDSHAKE_HAS_FASTEXT( bits ) ( ( ( bits )[7] & 0x04 ) ? 1 : 0 ) 
     75 #define HANDSHAKE_SET_FASTEXT( bits ) ( ( bits )[7] |= 0x04 ) 
     76#else 
     77 #define HANDSHAKE_HAS_FASTEXT( bits ) ( 0 ) 
     78 #define HANDSHAKE_SET_FASTEXT( bits ) ( (void)0 ) 
     79#endif 
    8280
    8381/* http://www.azureuswiki.com/index.php/Extension_negotiation_protocol
     
    9492    tr_peerIo *           io;
    9593    tr_crypto *           crypto;
    96     struct tr_handle *    handle;
     94    tr_session *          session;
    9795    uint8_t               myPublicKey[KEY_LEN];
    9896    uint8_t               mySecret[KEY_LEN];
     
    208206    uint8_t          * walk = buf;
    209207    const uint8_t    * torrentHash = tr_cryptoGetTorrentHash( handshake->crypto );
    210     const tr_torrent * tor = tr_torrentFindFromHash( handshake->handle, torrentHash );
     208    const tr_torrent * tor = tr_torrentFindFromHash( handshake->session, torrentHash );
    211209    const uint8_t    * peer_id = tor && tor->peer_id ? tor->peer_id : tr_getPeerId( );
    212210
     
    250248    const uint8_t * peer_id;
    251249
    252     dbgmsg( handshake, "payload: need %d, got %d", (int)HANDSHAKE_SIZE,
    253            (int)EVBUFFER_LENGTH( inbuf ) );
     250    dbgmsg( handshake, "payload: need %d, got %zu",
     251            (int)HANDSHAKE_SIZE, EVBUFFER_LENGTH( inbuf ) );
    254252
    255253    if( EVBUFFER_LENGTH( inbuf ) < HANDSHAKE_SIZE )
     
    267265    tr_peerIoReadBytes( handshake->io, inbuf, hash, sizeof( hash ) );
    268266    assert( tr_peerIoHasTorrentHash( handshake->io ) );
    269     if( !tr_torrentExists( handshake->handle, hash )
     267    if( !tr_torrentExists( handshake->session, hash )
    270268      || memcmp( hash, tr_peerIoGetTorrentHash( handshake->io ),
    271269                 SHA_DIGEST_LENGTH ) )
     
    285283            handshake->peer_id );
    286284
    287     tor = tr_torrentFindFromHash( handshake->handle, hash );
     285    tor = tr_torrentFindFromHash( handshake->session, hash );
    288286    peer_id = tor && tor->peer_id ? tor->peer_id : tr_getPeerId( );
    289287    if( !memcmp( handshake->peer_id, peer_id, PEER_ID_LEN ) )
     
    297295    **/
    298296
    299     if( HANDSHAKE_HAS_LTEP( reserved ) )
    300     {
    301         tr_peerIoEnableLTEP( handshake->io, 1 );
    302         dbgmsg( handshake, "using ltep" );
    303     }
    304 
    305     if( HANDSHAKE_HAS_FASTEXT( reserved ) )
    306     {
    307         tr_peerIoEnableFEXT( handshake->io, 1 );
    308         dbgmsg( handshake, "using fext" );
    309     }
     297    tr_peerIoEnableLTEP( handshake->io, HANDSHAKE_HAS_LTEP( reserved ) );
     298
     299    tr_peerIoEnableFEXT( handshake->io, HANDSHAKE_HAS_FASTEXT( reserved ) );
    310300
    311301    return HANDSHAKE_OK;
     
    410400        return READ_LATER;
    411401
    412     isEncrypted = memcmp( EVBUFFER_DATA(
    413                               inbuf ), HANDSHAKE_NAME, HANDSHAKE_NAME_LEN );
     402    isEncrypted = memcmp( EVBUFFER_DATA( inbuf ), HANDSHAKE_NAME, HANDSHAKE_NAME_LEN );
    414403    if( isEncrypted )
    415404    {
     
    422411           ( isEncrypted ? "encrypted" : "plaintext" ) );
    423412
    424     tr_peerIoSetEncryption(
    425         handshake->io, isEncrypted ? PEER_ENCRYPTION_RC4
    426         : PEER_ENCRYPTION_NONE );
     413    tr_peerIoSetEncryption( handshake->io, isEncrypted ? PEER_ENCRYPTION_RC4
     414                                                       : PEER_ENCRYPTION_NONE );
    427415    if( !isEncrypted )
    428416    {
     
    454442        uint8_t buf[SHA_DIGEST_LENGTH];
    455443        tr_sha1( req2, "req2", 4,
    456                  tr_cryptoGetTorrentHash(
    457                      handshake->crypto ), SHA_DIGEST_LENGTH, NULL );
     444                 tr_cryptoGetTorrentHash( handshake->crypto ),
     445                 SHA_DIGEST_LENGTH, NULL );
    458446        tr_sha1( req3, "req3", 4, secret, KEY_LEN, NULL );
    459447        for( i = 0; i < SHA_DIGEST_LENGTH; ++i )
     
    576564    uint8_t *    tmp;
    577565
    578     dbgmsg( handshake, "pad d: need %d, got %d", (int)needlen,
    579            (int)EVBUFFER_LENGTH(
    580                inbuf ) );
     566    dbgmsg( handshake, "pad d: need %zu, got %zu",
     567            needlen, EVBUFFER_LENGTH( inbuf ) );
    581568    if( EVBUFFER_LENGTH( inbuf ) < needlen )
    582569        return READ_LATER;
     
    608595    uint8_t   hash[SHA_DIGEST_LENGTH];
    609596
    610     dbgmsg( handshake, "payload: need %d, got %d",
    611            (int)INCOMING_HANDSHAKE_LEN, (int)EVBUFFER_LENGTH(
    612                inbuf ) );
     597    dbgmsg( handshake, "payload: need %d, got %zu",
     598            (int)INCOMING_HANDSHAKE_LEN, EVBUFFER_LENGTH( inbuf ) );
    613599
    614600    if( EVBUFFER_LENGTH( inbuf ) < INCOMING_HANDSHAKE_LEN )
     
    667653
    668654    /**
    669     *** Extension negotiation
     655    *** Extensions
    670656    **/
    671657
    672     if( HANDSHAKE_HAS_LTEP( reserved ) )
    673     {
    674         tr_peerIoEnableLTEP( handshake->io, 1 );
    675         dbgmsg( handshake, "using ltep" );
    676     }
    677     if( HANDSHAKE_HAS_FASTEXT( reserved ) )
    678     {
    679         tr_peerIoEnableFEXT( handshake->io, 1 );
    680         dbgmsg( handshake, "using fext" );
    681     }
     658    tr_peerIoEnableLTEP( handshake->io, HANDSHAKE_HAS_LTEP( reserved ) );
     659
     660    tr_peerIoEnableFEXT( handshake->io, HANDSHAKE_HAS_FASTEXT( reserved ) );
    682661
    683662    /* torrent hash */
     
    685664    if( tr_peerIoIsIncoming( handshake->io ) )
    686665    {
    687         if( !tr_torrentExists( handshake->handle, hash ) )
     666        if( !tr_torrentExists( handshake->session, hash ) )
    688667        {
    689668            dbgmsg( handshake, "peer is trying to connect to us for a torrent we don't have." );
     
    737716
    738717    /* peer id */
    739     tr_peerIoReadBytes( handshake->io, inbuf, handshake->peer_id,
    740                         PEER_ID_LEN );
     718    tr_peerIoReadBytes( handshake->io, inbuf, handshake->peer_id, PEER_ID_LEN );
    741719    tr_peerIoSetPeersId( handshake->io, handshake->peer_id );
    742720    handshake->havePeerID = TRUE;
    743721    tr_clientForId( client, sizeof( client ), handshake->peer_id );
    744722    dbgmsg( handshake, "peer-id is [%s] ... isIncoming is %d", client,
    745            tr_peerIoIsIncoming(
    746                handshake->io ) );
     723            tr_peerIoIsIncoming( handshake->io ) );
    747724
    748725    /* if we've somehow connected to ourselves, don't keep the connection */
    749     tor = tr_torrentFindFromHash( handshake->handle, tr_peerIoGetTorrentHash( handshake->io ) );
     726    tor = tr_torrentFindFromHash( handshake->session, tr_peerIoGetTorrentHash( handshake->io ) );
    750727    peer_id = tor && tor->peer_id ? tor->peer_id : tr_getPeerId( );
    751728    peerIsGood = memcmp( handshake->peer_id, peer_id, PEER_ID_LEN ) != 0;
     
    763740    int            len;
    764741
    765     dbgmsg( handshake, "in readYa... need %d, have %d", (int)KEY_LEN,
    766            (int)EVBUFFER_LENGTH(
    767                inbuf ) );
     742    dbgmsg( handshake, "in readYa... need %d, have %zu",
     743            (int)KEY_LEN, EVBUFFER_LENGTH( inbuf ) );
    768744    if( EVBUFFER_LENGTH( inbuf ) < KEY_LEN )
    769745        return READ_LATER;
     
    796772    uint8_t * pch;
    797773
    798     dbgmsg(
    799          handshake,
    800         "looking to get past pad a... & resync on hash('req',S) ... have %d bytes",
    801         (int)EVBUFFER_LENGTH( inbuf ) );
     774    dbgmsg( handshake, "looking to get past pad a... & resync on hash('req',S) ... have %zu bytes",
     775            EVBUFFER_LENGTH( inbuf ) );
    802776    /**
    803777    *** Resynchronizing on HASH('req1',S)
     
    809783    if( pch == NULL )
    810784    {
    811         dbgmsg( handshake, "no luck so far.. draining %d bytes",
    812                (int)EVBUFFER_LENGTH(
    813                    inbuf ) );
     785        dbgmsg( handshake, "no luck so far.. draining %zu bytes",
     786                EVBUFFER_LENGTH( inbuf ) );
    814787        evbuffer_drain( inbuf, EVBUFFER_LENGTH( inbuf ) );
    815788        return READ_LATER;
     
    871844        obfuscatedTorrentHash[i] = req2[i] ^ req3[i];
    872845    if( ( tor =
    873              tr_torrentFindFromObfuscatedHash( handshake->handle,
     846             tr_torrentFindFromObfuscatedHash( handshake->session,
    874847                                               obfuscatedTorrentHash ) ) )
    875848    {
    876         dbgmsg(
    877             handshake,
    878             "got INCOMING connection's encrypted handshake for torrent [%s]",
    879             tor->info.name );
     849        dbgmsg( handshake, "got INCOMING connection's encrypted handshake for torrent [%s]",
     850                tor->info.name );
    880851        tr_peerIoSetTorrentHash( handshake->io, tor->info.hash );
    881852        if( !tr_torrentAllowsPex( tor )
    882           && tr_peerMgrPeerIsSeed( handshake->handle->peerMgr,
     853          && tr_peerMgrPeerIsSeed( handshake->session->peerMgr,
    883854                                  tor->info.hash,
    884855                                  tr_peerIoGetAddress( handshake->io, NULL ) ) )
     
    938909    uint32_t          crypto_select;
    939910
    940     dbgmsg( handshake, "reading IA... have %d, need %d",
    941             (int)EVBUFFER_LENGTH(
    942                 inbuf ), (int)needlen );
     911    dbgmsg( handshake, "reading IA... have %zu, need %zu",
     912            EVBUFFER_LENGTH( inbuf ), needlen );
    943913    if( EVBUFFER_LENGTH( inbuf ) < needlen )
    944914        return READ_LATER;
    945915
    946916    /**
    947     ***  B->A: ENCRYPT(VC, crypto_select, len(padD), padD), ENCRYPT2(Payload
    948     ***Stream)
     917    ***  B->A: ENCRYPT(VC, crypto_select, len(padD), padD), ENCRYPT2(Payload Stream)
    949918    **/
    950919
     
    975944
    976945    dbgmsg( handshake, "sending pad d" );
    977     /* ENCRYPT(VC, crypto_provide, len(PadC), PadC
     946    /* ENCRYPT(VC, crypto_provide, len(PadD), PadD
    978947     * PadD is reserved for future extensions to the handshake...
    979948     * standard practice at this time is for it to be zero-length */
     
    1013982    const size_t      needlen = HANDSHAKE_SIZE;
    1014983
    1015     dbgmsg( handshake, "reading payload stream... have %d, need %d",
    1016             (int)EVBUFFER_LENGTH( inbuf ), (int)needlen );
     984    dbgmsg( handshake, "reading payload stream... have %zu, need %zu",
     985            EVBUFFER_LENGTH( inbuf ), needlen );
    1017986    if( EVBUFFER_LENGTH( inbuf ) < needlen )
    1018987        return READ_LATER;
     
    10351004
    10361005static ReadState
    1037 canRead( struct tr_iobuf * iobuf, void * arg, size_t * piece )
    1038 {
    1039     tr_handshake *    handshake = arg;
    1040     struct evbuffer * inbuf = tr_iobuf_input( iobuf );
     1006canRead( struct tr_peerIo * io, void * arg, size_t * piece )
     1007{
     1008    tr_handshake    * handshake = arg;
     1009    struct evbuffer * inbuf = tr_peerIoGetReadBuffer( io );
    10411010    ReadState         ret;
    1042     int               readyForMore = TRUE;
     1011    tr_bool           readyForMore = TRUE;
    10431012
    10441013    /* no piece data in handshake */
     
    11221091}
    11231092
     1093void
     1094tr_handshakeFree( tr_handshake * handshake )
     1095{
     1096    if( handshake->io )
     1097        tr_peerIoFree( handshake->io );
     1098
     1099    tr_free( handshake );
     1100}
     1101
    11241102static int
    11251103tr_handshakeDone( tr_handshake * handshake,
    11261104                  int            isOK )
    11271105{
    1128     int success;
     1106    tr_bool success;
    11291107
    11301108    dbgmsg( handshake, "handshakeDone: %s", isOK ? "connected" : "aborting" );
     
    11331111    success = fireDoneFunc( handshake, isOK );
    11341112
    1135     tr_free( handshake );
    1136 
    11371113    return success ? READ_LATER : READ_ERR;
    11381114}
     
    11451121
    11461122static void
    1147 gotError( struct tr_iobuf  * iobuf UNUSED,
    1148           short              what,
    1149           void             * arg )
     1123gotError( tr_peerIo  * io UNUSED,
     1124          short        what,
     1125          void       * arg )
    11501126{
    11511127    tr_handshake * handshake = (tr_handshake *) arg;
     
    11941170    handshake->doneCB = doneCB;
    11951171    handshake->doneUserData = doneUserData;
    1196     handshake->handle = tr_peerIoGetSession( io );
    1197     tr_peerIoSetTimeoutSecs( io, 15 );
     1172    handshake->session = tr_peerIoGetSession( io );
    11981173
    11991174    tr_peerIoSetIOFuncs( handshake->io, canRead, NULL, gotError, handshake );
     
    12251200}
    12261201
    1227 const struct in_addr *
    1228 tr_handshakeGetAddr( const struct tr_handshake * handshake,
    1229                                                  uint16_t * port )
    1230 {
     1202struct tr_peerIo*
     1203tr_handshakeStealIO( tr_handshake * handshake )
     1204{
     1205    struct tr_peerIo * io;
     1206
    12311207    assert( handshake );
    12321208    assert( handshake->io );
    12331209
     1210    io = handshake->io;
     1211    handshake->io = NULL;
     1212    return io;
     1213}
     1214
     1215const tr_address *
     1216tr_handshakeGetAddr( const struct tr_handshake * handshake,
     1217                     tr_port                   * port )
     1218{
     1219    assert( handshake );
     1220    assert( handshake->io );
     1221
    12341222    return tr_peerIoGetAddress( handshake->io, port );
    12351223}
Note: See TracChangeset for help on using the changeset viewer.