Changeset 2971


Ignore:
Timestamp:
Sep 1, 2007, 8:31:58 PM (15 years ago)
Author:
charles
Message:

harden the read/write event synchronization.

Location:
branches/encryption/libtransmission
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/encryption/libtransmission/handshake.c

    r2954 r2971  
    1919#include <openssl/bn.h>
    2020#include <openssl/dh.h>
    21 //#include <openssl/rc4.h>
    22 //#include <openssl/sha.h>
    2321
    2422#include <event.h>
     
    8583    uint8_t state;
    8684    uint8_t encryptionPreference;
    87     uint8_t * resync;
    88     uint16_t PadC_len;
    89     uint16_t PadD_len;
    90     uint8_t pad_b_buf[512 + 8];
    91     int     pad_b_len;
     85    uint16_t pad_c_len;
     86    uint16_t pad_d_len;
    9287    int ia_len;
    93     int resync_len;
    94     int pad_d_len;
    9588    int crypto_select;
    9689    DH * dh;
     
    113106    AWAITING_HANDSHAKE,
    114107    AWAITING_YA,
    115     SENDING_YB,
    116108    AWAITING_PAD_A,
    117109    AWAITING_CRYPTO_PROVIDE,
     
    120112
    121113    /* outgoing */
    122     SENDING_YA,
    123114    AWAITING_YB,
    124     SENDING_CRYPTO_PROVIDE,
    125115    AWAITING_VC,
    126116    AWAITING_CRYPTO_SELECT,
    127117    AWAITING_PAD_D,
    128     //AWAITING_PLAINTEXT_RESPONSE,
    129    
    130     SENDING_PLAINTEXT_HANDSHAKE,
    131     SENDING_LTEP_HANDHAKE
    132118};
    133119
     
    142128        case AWAITING_HANDSHAKE:      str = "awaiting handshake"; break;
    143129        case AWAITING_YA:             str = "awaiting ya"; break;
    144         case SENDING_YB:              str = "sending yb"; break;
     130        //case SENDING_YB:              str = "sending yb"; break;
    145131        case AWAITING_PAD_A:          str = "awaiting pad a"; break;
    146132        case AWAITING_CRYPTO_PROVIDE: str = "awaiting crypto_provide"; break;
    147133        case AWAITING_PAD_C:          str = "awaiting pad c"; break;
    148134        case AWAITING_IA:             str = "awaiting ia"; break;
    149         case SENDING_YA:              str = "sending ya"; break;
     135        //case SENDING_YA:              str = "sending ya"; break;
    150136        case AWAITING_YB:             str = "awaiting yb"; break;
    151         case SENDING_CRYPTO_PROVIDE:  str = "sending crypto provide"; break;
     137        //case SENDING_CRYPTO_PROVIDE:  str = "sending crypto provide"; break;
    152138        case AWAITING_VC:             str = "awaiting vc"; break;
    153139        case AWAITING_CRYPTO_SELECT:  str = "awaiting crypto select"; break;
    154140        case AWAITING_PAD_D:          str = "awaiting pad d"; break;
    155         case SENDING_PLAINTEXT_HANDSHAKE: str = "sending plaintext handshake"; break;
     141        //case SENDING_PLAINTEXT_HANDSHAKE: str = "sending plaintext handshake"; break;
    156142    }
    157143    return str;
     
    161147setState( tr_handshake * handshake, short state )
    162148{
    163     fprintf( stderr, "setting handshake %p to state [%s]\n", handshake, getStateName(state) );
     149    fprintf( stderr, "handshake %p: setting to state [%s]\n", handshake, getStateName(state) );
    164150    handshake->state = state;
    165151}
     
    169155{
    170156    setState( handshake, state );
    171     tr_peerIoSetIOMode( handshake->io, EV_READ, EV_WRITE );
     157    //tr_peerIoSetIOMode( handshake->io, EV_READ, EV_WRITE );
    172158}
    173159
     
    194180
    195181    /* send it */
    196     setState( handshake, SENDING_YA );
     182    setReadState( handshake, AWAITING_YB );
     183    //setState( handshake, SENDING_YA );
    197184    tr_peerIoWriteBuf( handshake->io, outbuf );
    198     tr_peerIoSetIOMode( handshake->io, EV_WRITE, EV_READ );
    199185
    200186    /* cleanup */
     
    227213    uint8_t buf[HANDSHAKE_SIZE];
    228214    buildHandshakeMessage( handshake, buf );
    229     setState( handshake, SENDING_PLAINTEXT_HANDSHAKE );
     215
     216    setReadState( handshake, AWAITING_HANDSHAKE );
    230217    tr_peerIoWrite( handshake->io, buf, HANDSHAKE_SIZE );
    231     tr_peerIoSetIOMode( handshake->io, EV_WRITE, EV_READ );
    232218}
    233219
     
    269255    tr_bencInitStr( tr_bencDictAdd( &val, "v" ), v, 0, 1 );
    270256
    271     fprintf( stderr, "sending ltep handshake...\n" );
     257    fprintf( stderr, "handshake %p: sending ltep handshake...\n", handshake );
    272258    buf = tr_bencSaveMalloc( &val,  &len );
    273259    tr_bencPrint( &val );
     
    279265    tr_peerIoWriteBytes ( handshake->io, outbuf, buf, len );
    280266   
    281     handshake->state = SENDING_LTEP_HANDHAKE;
    282267    tr_peerIoWriteBuf( handshake->io, outbuf );
    283     tr_peerIoSetIOMode( handshake->io, EV_WRITE, EV_READ );
     268    fireDoneCB( handshake, TRUE );
    284269 
    285270    /* cleanup */
     
    318303    while( len-- )
    319304        *walk++ = tr_rand( UCHAR_MAX );
    320     setState( handshake, SENDING_YB );
     305
     306    setReadState( handshake, AWAITING_PAD_A );
    321307    tr_peerIoWrite( handshake->io, outbuf, walk-outbuf );
    322     tr_peerIoSetIOMode( handshake->io, EV_WRITE, EV_READ );
    323308
    324309    return READ_DONE;
     
    398383    tr_peerIoReadUint16( handshake->io, inbuf, &padc_len );
    399384    fprintf( stderr, "padc is %d\n", (int)padc_len );
    400     handshake->PadC_len = padc_len;
     385    handshake->pad_c_len = padc_len;
    401386    setState( handshake, AWAITING_PAD_C );
    402387    return READ_AGAIN;
     
    407392{
    408393    uint16_t ia_len;
    409     const size_t needlen = handshake->PadC_len + sizeof(uint16_t);
     394    const size_t needlen = handshake->pad_c_len + sizeof(uint16_t);
    410395
    411396    if( EVBUFFER_LENGTH(inbuf) < needlen )
     
    545530    /* send it */
    546531    tr_cryptoDecryptInit( handshake->crypto );
    547     setState( handshake, SENDING_CRYPTO_PROVIDE );
     532    setReadState( handshake, AWAITING_VC );
    548533    tr_peerIoWriteBuf( handshake->io, outbuf );
    549     tr_peerIoSetIOMode( handshake->io, EV_WRITE, EV_READ );
    550534
    551535    /* cleanup */
     
    744728    struct evbuffer * inbuf = EVBUFFER_INPUT ( evin );
    745729    ReadState ret;
    746     fprintf( stderr, "handling canRead; state is [%s]\n", getStateName(handshake->state) );
     730    fprintf( stderr, "handshake %p handling canRead; state is [%s]\n", handshake, getStateName(handshake->state) );
    747731
    748732    switch( handshake->state )
     
    771755    tr_handshake * handshake = (tr_handshake *) arg;
    772756    fprintf( stderr, "handshake %p, with a state of %s, got a didWrite event\n", handshake, getStateName(handshake->state) );
     757#if 0
     758    abort ( );
    773759
    774760    if( handshake->state == SENDING_LTEP_HANDHAKE )
     
    778764    else
    779765    {
     766cccccccccccccccccccccccccccccccccc
    780767        int state = -1;
    781768        switch( handshake->state )
    782769        {
    783             case SENDING_YA:                   state = AWAITING_YB; break;
    784             case SENDING_YB:                   state = AWAITING_PAD_A; break;
    785             case SENDING_CRYPTO_PROVIDE:       state = AWAITING_VC; break;
     770            //case SENDING_YA:                   state = AWAITING_YB; break;
     771            //case SENDING_YB:                   state = AWAITING_PAD_A; break;
     772            //case SENDING_CRYPTO_PROVIDE:       state = AWAITING_VC; break;
     773            //case SENDING_PLAINTEXT_HANDSHAKE:  state = AWAITING_HANDSHAKE; break;
    786774        }
    787775        assert( state != -1 );
     
    789777        tr_peerIoReadOrWait( handshake->io );
    790778    }
     779#endif
    791780}
    792781
     
    801790fireDoneCB( tr_handshake * handshake, int isConnected )
    802791{
     792fprintf( stderr, "handshake %p: firing done.  connected==%d\n", handshake, isConnected );
    803793    (*handshake->doneCB)(handshake->io, isConnected, handshake->doneUserData);
    804794    tr_handshakeFree( handshake );
     
    806796
    807797static void
    808 gotError( struct bufferevent * evbuf UNUSED, short what UNUSED, void * arg )
     798gotError( struct bufferevent * evbuf UNUSED, short what, void * arg )
    809799{
    810800    tr_handshake * handshake = (tr_handshake *) arg;
     801fprintf( stderr, "handshake %p: got error; what==%hd\n", handshake, what );
    811802
    812803
     
    814805     * have encountered a peer that doesn't do encryption... reconnect and
    815806     * try a plaintext handshake */
    816     if(    ( handshake->state == SENDING_YA )
     807    if(    ( handshake->state == AWAITING_YB )
    817808        && ( handshake->encryptionPreference != HANDSHAKE_ENCRYPTION_REQUIRED )
    818809        && ( !tr_peerIoReconnect( handshake->io ) ) )
     
    836827    tr_handshake * handshake;
    837828
     829//w00t
    838830//static int count = 0;
    839831//if( count++ ) return NULL;
     
    847839    handshake->handle = tr_peerIoGetHandle( io );
    848840
     841    tr_peerIoSetIOMode( io, EV_READ|EV_WRITE, 0 );
    849842    tr_peerIoSetIOFuncs( io, canRead, didWrite, gotError, handshake );
     843
     844fprintf( stderr, "handshake %p: new handshake for io %p\n", handshake, io );
    850845
    851846    if( tr_peerIoIsIncoming( io ) )
  • branches/encryption/libtransmission/peer-io.c

    r2965 r2971  
    113113    c->rateToPeer = tr_rcInit( );
    114114    c->rateToClient = tr_rcInit( );
     115fprintf( stderr, "io %p rates: peer %p client %p\n", c, c->rateToPeer, c->rateToClient );
    115116    c->bufev = bufferevent_new( c->socket,
    116117                                canReadWrapper,
     
    118119                                gotErrorWrapper,
    119120                                c );
     121    bufferevent_enable( c->bufev, EV_READ|EV_WRITE );
    120122    return c;
    121123}
     
    126128                      int                 socket )
    127129{
    128     tr_peerIo * c =
    129         tr_peerIoNew( handle, in_addr, NULL, 1, socket );
     130    tr_peerIo * c;
     131
     132    assert( handle != NULL );
     133    assert( in_addr != NULL );
     134    assert( socket >= 0 );
     135
     136    c = tr_peerIoNew( handle, in_addr, NULL, 1, socket );
    130137    c->port = -1;
    131138    return c;
     
    157164    {
    158165        bufferevent_free( c->bufev );
     166fprintf( stderr, "io %p destroying rate to client %p to peer %p\n", c, c->rateToClient, c->rateToPeer );
    159167        tr_rcClose( c->rateToClient );
    160168        tr_rcClose( c->rateToPeer );
     
    205213{
    206214    tr_setBufferEventMode( c->handle, c->bufev, enable, disable );
    207 }
    208 
    209 void
    210 tr_peerIoReadOrWait( tr_peerIo * c )
    211 {
    212     if( EVBUFFER_LENGTH( c->bufev->input ) )
    213         canReadWrapper( c->bufev, c );
    214     else
    215         tr_peerIoSetIOMode( c, EV_READ, EV_WRITE );
    216215}
    217216
  • branches/encryption/libtransmission/peer-io.h

    r2963 r2971  
    107107                           short         disable_mode );
    108108
    109 void  tr_peerIoReadOrWait( tr_peerIo * io );
    110 
    111109void tr_peerIoWrite( tr_peerIo   * io,
    112110                     const void  * writeme,
  • branches/encryption/libtransmission/peer-mgr.c

    r2969 r2971  
    1414#include <string.h> /* memcpy, memcmp */
    1515#include <stdlib.h> /* qsort */
     16#include <stdio.h> /* printf */
    1617
    1718#include "transmission.h"
     
    118119        memcpy( &peer->in_addr, in_addr, sizeof(struct in_addr) );
    119120        tr_ptrArrayInsertSorted( torrent->peers, peer, peerCompare );
     121fprintf( stderr, "getPeer: torrent %p now has %d peers\n", torrent, tr_ptrArraySize(torrent->peers) );
    120122    }
    121123    return peer;
     
    186188    tr_peerMgr * manager = (tr_peerMgr*) vmanager;
    187189    Torrent * t = getExistingTorrent( manager, hash );
    188 
     190    uint16_t port;
     191    const struct in_addr * in_addr;
     192
     193    fprintf( stderr, "peer-mgr: torrent [%s] finished a handshake; isConnected is %d\n", t->tor->info.name, isConnected );
     194
     195    assert( io != NULL );
     196
     197    in_addr = tr_peerIoGetAddress( io, &port );
     198
     199    /* if we couldn't connect or were snubbed,
     200     * the peer's probably not worth remembering. */
     201    if( !ok ) {
     202        tr_peer * peer = getExistingPeer( t, in_addr );
     203        fprintf( stderr, "peer-mgr: torrent [%s] got a bad one, and you know what? fuck them.\n", t->tor->info.name );
     204        if( peer ) {
     205            tr_ptrArrayRemoveSorted( t->peers, peer, peerCompare );
     206            freePeer( peer );
     207        } else  {
     208            tr_peerIoFree( io );
     209        }
     210        --manager->connectionCount;
     211        return;
     212    }
     213
     214#if 0
     215    /* ONLY DO THIS TEST FOR INCOMING CONNECTIONS */
    189216    /* check for duplicates */
    190     if( ok ) {
    191         if ( getExistingPeer( t, tr_peerIoGetAddress(io,NULL) ) ) {
    192             tr_dbg( "dropping a duplicate connection... dropping." );
    193             ok = FALSE;
    194         }
    195     }
    196 
    197     /* do something with this connection */
    198     if( !ok ) {
     217    if( getExistingPeer( t, in_addr ) ) {
     218        tr_dbg( "dropping a duplicate connection... dropping." );
    199219        tr_peerIoFree( io );
    200         --manager->connectionCount;
    201     } else {
    202         uint16_t port;
    203         tr_peer * peer = getPeer( t, tr_peerIoGetAddress(io,&port) );
     220        return;
     221    }
     222#endif
     223
     224    if( 1 ) {
     225        tr_peer * peer = getPeer( t, in_addr );
    204226        peer->port = port;
    205         peer->io = io;
    206227        peer->msgs = tr_peerMsgsNew( t->tor, peer );
    207228    }
     
    244265        peer->port = port;
    245266        peer->from = from;
     267
     268        if( tr_peerMgrIsAcceptingConnections( manager ) )
     269        {
     270            fprintf( stderr, "peer-mgr: torrent [%s] is handshaking with a new peer...\n", t->tor->info.name );
     271
     272            peer->io = tr_peerIoNewOutgoing( manager->handle, &addr, port, t->hash );
     273
     274            tr_handshakeAdd( peer->io, HANDSHAKE_ENCRYPTION_PREFERRED,
     275                             myHandshakeDoneCB, manager );
     276        }
    246277    }
    247278}
     
    357388            const tr_peer ** peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount );
    358389            for( j=0; j<peerCount; ++j )
    359                 if( tr_bitfieldHas( peers[i]->have, j ) )
     390                if( tr_bitfieldHas( peers[j]->have, i ) )
    360391                    ++tab[i];
    361392        }
     
    502533    ChokeData * data;
    503534
     535fprintf( stderr, "rechoking torrent %p, with %d peers\n", t, size );
     536
    504537    if( size < 1 )
    505538        return TRUE;
  • branches/encryption/libtransmission/peer-msgs.c

    r2969 r2971  
    245245        tr_free( peer->info->client );
    246246        fprintf( stderr, "dictionary says client is [%s]\n", sub->val.s.s );
    247 for( i=0; i<sub->val.s.i; ++i ) fprintf( stderr, "[%c] (%d)\n", sub->val.s.s[i], (int)sub->val.s.s[i] );
    248247        peer->info->client = tr_strndup( sub->val.s.s, sub->val.s.i );
     248for( i=0; i<sub->val.s.i; ++i ) { fprintf( stderr, "[%c] (%d)\n", sub->val.s.s[i], (int)sub->val.s.s[i] );
     249                                  if( (int)peer->info->client[i]==-75 ) peer->info->client[i]='u'; }
    249250        fprintf( stderr, "peer->client is now [%s]\n", peer->info->client );
    250251    }
  • branches/encryption/libtransmission/ratecontrol.c

    r2942 r2971  
    101101{
    102102    int ret;
    103     tr_lockLock( (tr_lock_t*)r->lock );
    104103
    105     ret = rateForInterval( r, SHORT_INTERVAL_MSEC ) < r->limit;
     104    if( r == NULL )
     105        ret = 0;
     106    else {
     107        tr_lockLock( (tr_lock_t*)r->lock );
     108        ret = rateForInterval( r, SHORT_INTERVAL_MSEC ) < r->limit;
     109        tr_lockUnlock( (tr_lock_t*)r->lock );
     110    }
    106111
    107     tr_lockUnlock( (tr_lock_t*)r->lock );
    108112    return ret;
    109113}
     
    113117{
    114118    float ret;
    115     tr_lockLock( (tr_lock_t*)r->lock );
    116119
    117     ret = rateForInterval( r, LONG_INTERVAL_MSEC );
     120    if( r == NULL )
     121        ret = 0.0f;
     122    else {
     123        tr_lockLock( (tr_lock_t*)r->lock );
     124        ret = rateForInterval( r, LONG_INTERVAL_MSEC );
     125        tr_lockUnlock( (tr_lock_t*)r->lock );
     126    }
    118127
    119     tr_lockUnlock( (tr_lock_t*)r->lock );
    120128    return ret;
    121129}
  • branches/encryption/libtransmission/trevent.c

    r2954 r2971  
    121121            read( fd, &mode, sizeof(short) );
    122122            bufferevent_disable( bufev, mode );
     123fprintf( stderr, "after enable/disable, the mode is %hd\n", bufev->enabled );
    123124            break;
    124125
Note: See TracChangeset for help on using the changeset viewer.