Changeset 3228


Ignore:
Timestamp:
Sep 29, 2007, 12:46:41 AM (15 years ago)
Author:
charles
Message:

improved uTorrent handshaking, after talking to #utorrent-dev about minutae...

Location:
trunk/libtransmission
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/handshake.c

    r3227 r3228  
    105105struct tr_handshake
    106106{
    107     unsigned int peerSupportsEncryption       : 1;
    108107    unsigned int havePeerID                   : 1;
    109108    unsigned int haveSentBitTorrentHandshake  : 1;
     
    407406    dbgmsg( handshake, "got a %s handshake", (isEncrypted ? "encrypted" : "plaintext") );
    408407
    409     handshake->peerSupportsEncryption = isEncrypted;
    410408    tr_peerIoSetEncryption( handshake->io, isEncrypted ? PEER_ENCRYPTION_RC4
    411409                                                       : PEER_ENCRYPTION_NONE );
     
    10021000                          isConnected,
    10031001                          peer_id,
    1004                           handshake->peerSupportsEncryption,
    10051002                          handshake->doneUserData );
    10061003}
  • trunk/libtransmission/handshake.h

    r3105 r3228  
    2424                                int                   isConnected,
    2525                                const uint8_t       * peerId,
    26                                 int                   peerSupportsEncryption,
    2726                                void                * userData );
    2827
  • trunk/libtransmission/peer-mgr-private.h

    r3183 r3228  
    2222struct tr_peermsgs;
    2323
     24enum
     25{
     26    ENCRYPTION_PREFERENCE_UNKNOWN,
     27    ENCRYPTION_PREFERENCE_YES,
     28    ENCRYPTION_PREFERENCE_NO
     29};
     30
    2431typedef struct tr_peer
    2532{
     
    2835    unsigned int  clientIsChoked : 1;
    2936    unsigned int  clientIsInterested : 1;
    30     unsigned int  peerSupportsEncryption : 1;
    3137    unsigned int  doPurge : 1;
    3238
     39    uint8_t encryption_preference;
    3340    uint8_t from;
    3441    uint16_t port;
  • trunk/libtransmission/peer-mgr.c

    r3227 r3228  
    747747                   int               isConnected,
    748748                   const uint8_t   * peer_id,
    749                    int               peerSupportsEncryption,
    750749                   void            * vmanager )
    751750{
     
    760759    assert( io != NULL );
    761760    assert( isConnected==0 || isConnected==1 );
    762     assert( peerSupportsEncryption==0 || peerSupportsEncryption==1 );
    763761
    764762    ours = tr_ptrArrayRemoveSorted( manager->handshakes,
     
    808806            tr_free( peer->client );
    809807            peer->client = peer_id ? tr_clientForId( peer_id ) : NULL;
    810             peer->peerSupportsEncryption = peerSupportsEncryption ? 1 : 0;
    811808            peer->msgsTag = tr_peerMsgsSubscribe( peer->msgs, msgsCallbackFunc, t );
    812809            peer->connectionChangedAt = time( NULL );
     
    923920int tr_pexCompare( const void * a, const void * b );
    924921
     922static int
     923peerPrefersCrypto( const tr_peer * peer )
     924{
     925    if( peer->encryption_preference == ENCRYPTION_PREFERENCE_YES )
     926        return TRUE;
     927
     928    if( peer->encryption_preference == ENCRYPTION_PREFERENCE_NO )
     929        return FALSE;
     930
     931    return tr_peerIoIsEncrypted( peer->io );
     932};
    925933
    926934int
     
    944952
    945953        walk->flags = 0;
    946         if( peer->peerSupportsEncryption ) walk->flags |= 1;
    947         if( peer->progress >= 1.0 )        walk->flags |= 2;
     954        if( peerPrefersCrypto(peer) ) walk->flags |= 1;
     955        if( peer->progress >= 1.0 )    walk->flags |= 2;
    948956    }
    949957
  • trunk/libtransmission/peer-msgs.c

    r3227 r3228  
    123123    time_t clientSentAnythingAt;
    124124
    125     unsigned int notListening          : 1;
    126     unsigned int peerSupportsPex       : 1;
    127     unsigned int hasSentLtepHandshake  : 1;
     125    unsigned int notListening             : 1;
     126    unsigned int peerSupportsPex          : 1;
     127    unsigned int clientSentLtepHandshake  : 1;
     128    unsigned int peerSentLtepHandshake    : 1;
    128129   
    129130    tr_bitfield * clientAllowedPieces;
     
    530531    char * buf;
    531532    int len;
     533    int pex;
    532534    const char * v = TR_NAME " " USERAGENT_PREFIX;
    533535    const int port = tr_getPublicPort( msgs->handle );
    534536    struct evbuffer * outbuf = evbuffer_new( );
    535537
     538    if( msgs->clientSentLtepHandshake )
     539        return;
     540
    536541    dbgmsg( msgs, "sending an ltep handshake" );
     542    msgs->clientSentLtepHandshake = 1;
     543
     544    /* decide if we want to advertise pex support */
     545    if( msgs->torrent->pexDisabled )
     546        pex = 0;
     547    else if( msgs->peerSentLtepHandshake )
     548        pex = msgs->peerSupportsPex ? 1 : 0;
     549    else
     550        pex = 1;
     551
    537552    tr_bencInit( &val, TYPE_DICT );
    538     tr_bencDictReserve( &val, 3 );
     553    tr_bencDictReserve( &val, 4 );
     554    tr_bencInitInt( tr_bencDictAdd( &val, "e" ), 1 );
    539555    m  = tr_bencDictAdd( &val, "m" );
    540556    tr_bencInit( m, TYPE_DICT );
    541     tr_bencDictReserve( m, 1 );
    542     tr_bencInitInt( tr_bencDictAdd( m, "ut_pex" ), OUR_LTEP_PEX );
     557    if( pex ) {
     558        tr_bencDictReserve( m, 1 );
     559        tr_bencInitInt( tr_bencDictAdd( m, "ut_pex" ), OUR_LTEP_PEX );
     560    }
    543561    if( port > 0 )
    544562        tr_bencInitInt( tr_bencDictAdd( &val, "p" ), port );
     
    552570
    553571    tr_peerIoWriteBuf( msgs->io, outbuf );
    554     msgs->hasSentLtepHandshake = 1;
     572
     573    dbgmsg( msgs, "here is the ltep handshake we sent:" );
     574    tr_bencPrint( &val );
    555575
    556576    /* cleanup */
     
    568588
    569589    tr_peerIoReadBytes( msgs->io, inbuf, tmp, len );
     590    msgs->peerSentLtepHandshake = 1;
    570591
    571592    if( tr_bencLoad( tmp, len, &val, NULL ) || val.type!=TYPE_DICT ) {
     
    574595        return;
    575596    }
     597
     598    dbgmsg( msgs, "here is the ltep handshake we read:" );
     599    tr_bencPrint( &val );
     600
     601    /* does the peer prefer encrypted connections? */
     602    sub = tr_bencDictFind( &val, "e" );
     603    if( tr_bencIsInt( sub ) )
     604        msgs->info->encryption_preference = sub->val.i
     605                                      ? ENCRYPTION_PREFERENCE_YES
     606                                      : ENCRYPTION_PREFERENCE_NO;
    576607
    577608    /* check supported messages for utorrent pex */
     
    658689        dbgmsg( msgs, "got ltep handshake" );
    659690        parseLtepHandshake( msgs, msglen, inbuf );
    660         if( !msgs->hasSentLtepHandshake )
    661             sendLtepHandshake( msgs );
     691        sendLtepHandshake( msgs );
    662692    }
    663693    else if( ltep_msgid == msgs->ut_pex_id )
Note: See TracChangeset for help on using the changeset viewer.