Changeset 3104


Ignore:
Timestamp:
Sep 20, 2007, 3:59:48 PM (14 years ago)
Author:
charles
Message:
  • don't initiate a new connection to a peer that we're already handshaking with
  • don't accidentally connect to ourselves. :)
Location:
branches/encryption/libtransmission
Files:
5 edited

Legend:

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

    r3101 r3104  
    3939***/
    4040
    41 #define HANDSHAKE_NAME          "\023BitTorrent protocol"
    42 #define HANDSHAKE_NAME_LEN      20
    43 #define HANDSHAKE_FLAGS_LEN     8
    44 #define HANDSHAKE_SIZE          68
    45 
    46 /***
    47 ****
    48 ***/
     41#define HANDSHAKE_NAME "\023BitTorrent protocol"
    4942
    5043enum
    5144{
     45    /* BitTorrent Handshake Constants */
     46    HANDSHAKE_NAME_LEN             = 20,
     47    HANDSHAKE_FLAGS_LEN            = 8,
     48    HANDSHAKE_SIZE                 = 68,
     49    PEER_ID_LEN                    = 20,
     50
     51    /* Extension Negotiation Constants */
    5252    HANDSHAKE_EXTPREF_LTEP_FORCE   = 0x0,
    5353    HANDSHAKE_EXTPREF_LTEP_PREFER  = 0x1,
    5454    HANDSHAKE_EXTPREF_AZMP_PREFER  = 0x2,
    55     HANDSHAKE_EXTPREF_AZMP_FORCE   = 0x3
     55    HANDSHAKE_EXTPREF_AZMP_FORCE   = 0x3,
     56
     57    /* Encryption Constants */
     58    PadA_MAXLEN                    = 512,
     59    PadB_MAXLEN                    = 512,
     60    PadC_MAXLEN                    = 512,
     61    PadD_MAXLEN                    = 512,
     62    VC_LENGTH                      = 8,
     63    KEY_LEN                        = 96
    5664};
    5765
     
    7886#define HANDSHAKE_GET_EXTPREF( reserved )      ( (reserved)[5] & 0x03 )
    7987#define HANDSHAKE_SET_EXTPREF( reserved, val ) ( (reserved)[5] |= 0x03 & (val) )
    80 #define HANDSHAKE_EXTPREF_LTEP_FORCE   ( 0x0 )
    81 #define HANDSHAKE_EXTPREF_LTEP_PREFER  ( 0x1 )
    82 #define HANDSHAKE_EXTPREF_AZMP_PREFER  ( 0x2 )
    83 #define HANDSHAKE_EXTPREF_AZMP_FORCE   ( 0x3 )
    84 
    85 enum
    86 {
    87     PadA_MAXLEN = 512,
    88     PadB_MAXLEN = 512,
    89     PadC_MAXLEN = 512,
    90     PadD_MAXLEN = 512
    91 };
    9288
    9389extern const char* getPeerId( void ) ;
    94 
    95 #define KEY_LEN 96
    96 #define VC_LENGTH 8
    9790
    9891struct tr_handshake
     
    10598    tr_crypto * crypto;
    10699    struct tr_handle * handle;
    107     uint8_t myPublicKey[96];
    108     uint8_t mySecret[96];
     100    uint8_t myPublicKey[KEY_LEN];
     101    uint8_t mySecret[KEY_LEN];
    109102    uint8_t state;
    110103    uint16_t pad_c_len;
     
    113106    uint32_t crypto_select;
    114107    uint8_t myReq1[SHA_DIGEST_LENGTH];
    115     uint8_t peer_id[20];
     108    uint8_t peer_id[PEER_ID_LEN];
    116109    handshakeDoneCB doneCB;
    117110    void * doneUserData;
     
    496489    uint8_t pstrlen;
    497490    uint8_t * pstr;
    498     uint8_t reserved[8];
     491    uint8_t reserved[HANDSHAKE_FLAGS_LEN];
    499492    uint8_t hash[SHA_DIGEST_LENGTH];
    500493
     
    503496    if( EVBUFFER_LENGTH(inbuf) < HANDSHAKE_SIZE )
    504497        return READ_MORE;
    505 
    506 for( i=0; i<(int)EVBUFFER_LENGTH(inbuf); ++i )
    507 fprintf( stderr, "[%c]", EVBUFFER_DATA(inbuf)[i] );
    508 fprintf( stderr, "\n" );
    509498
    510499    pstrlen = EVBUFFER_DATA(inbuf)[0]; /* peek, don't read.  We may be
     
    583572    tr_peerIoSetPeersId( handshake->io, handshake->peer_id );
    584573    handshake->havePeerID = TRUE;
     574    dbgmsg( handshake, "peer-id is [%*.*s]", PEER_ID_LEN, PEER_ID_LEN, handshake->peer_id );
     575    if( !memcmp( handshake->peer_id, getPeerId(), PEER_ID_LEN ) ) {
     576        dbgmsg( handshake, "streuth!  we've connected to ourselves." );
     577        tr_handshakeDone( handshake, FALSE );
     578        return READ_DONE;
     579    }
    585580
    586581    /**
     
    890885    return handshake;
    891886}
     887
     888const struct in_addr *
     889tr_handshakeGetAddr( const struct tr_handshake * handshake, uint16_t * port )
     890{
     891    assert( handshake != NULL );
     892    assert( handshake->io != NULL );
     893
     894    return tr_peerIoGetAddress( handshake->io, port );
     895}
     896
     897
  • branches/encryption/libtransmission/handshake.h

    r3101 r3104  
    1616#include "transmission.h"
    1717
     18struct in_addr;
    1819struct tr_peerIo;
    1920typedef struct tr_handshake tr_handshake;
     
    3132                                 void               * doneUserData );
    3233
     34const struct in_addr * tr_handshakeGetAddr( const struct tr_handshake * handshake,
     35                                            uint16_t                  * setme_port );
     36
    3337void tr_handshakeAbort( tr_handshake  * handshake );
    3438
  • branches/encryption/libtransmission/peer-mgr.c

    r3101 r3104  
    7171
    7272static int
     73handshakeCompareToAddr( const void * va, const void * vb )
     74{
     75    const tr_handshake * a = va;
     76    const struct in_addr * b = vb;
     77    return memcmp( tr_handshakeGetAddr( a, NULL ), b, sizeof( struct in_addr ) );
     78}
     79
     80static int
     81handshakeCompare( const void * va, const void * vb )
     82{
     83    const tr_handshake * a = va;
     84    const tr_handshake * b = vb;
     85    return memcmp( tr_handshakeGetAddr( a, NULL ),
     86                   tr_handshakeGetAddr( b, NULL ),
     87                   sizeof( struct in_addr ) );
     88}
     89
     90static tr_handshake*
     91getExistingHandshake( tr_peerMgr * mgr, const struct in_addr * in_addr )
     92{
     93    return tr_ptrArrayFindSorted( mgr->handshakes,
     94                                  in_addr,
     95                                  handshakeCompareToAddr );
     96}
     97
     98/**
     99***
     100**/
     101
     102static int
    73103torrentCompare( const void * va, const void * vb )
    74104{
    75     const Torrent * a = (const Torrent*) va;
    76     const Torrent * b = (const Torrent*) vb;
     105    const Torrent * a = va;
     106    const Torrent * b = vb;
    77107    return memcmp( a->hash, b->hash, SHA_DIGEST_LENGTH );
    78108}
     
    546576    ours = tr_ptrArrayRemoveSorted( manager->handshakes,
    547577                                    handshake,
    548                                     tr_comparePointers );
     578                                    handshakeCompare );
    549579    assert( handshake == ours );
    550580
     
    609639    ++manager->connectionCount;
    610640
    611     tr_ptrArrayInsertSorted( manager->handshakes, handshake, tr_comparePointers );
     641    tr_ptrArrayInsertSorted( manager->handshakes, handshake, handshakeCompare );
    612642}
    613643
     
    617647                       int               socket )
    618648{
    619     tr_peerIo * io = tr_peerIoNewIncoming( manager->handle, addr, socket );
    620     initiateHandshake( manager, io );
     649    if( getExistingHandshake( manager, addr ) == NULL )
     650    {
     651        tr_peerIo * io = tr_peerIoNewIncoming( manager->handle, addr, socket );
     652        initiateHandshake( manager, io );
     653    }
    621654}
    622655
     
    636669    if( !t->isRunning ) { /* torrent's not running */
    637670        fprintf( stderr, "OUTGOING connection not being made because t [%s] is not running\n", t->tor->info.name );
     671        return;
     672    }
     673    if( getExistingHandshake( manager, &peer->in_addr ) != NULL ) { /* already have a handshake pending */
     674        fprintf( stderr, "already have a handshake for that address\n" );
    638675        return;
    639676    }
  • branches/encryption/libtransmission/utils.c

    r3090 r3104  
    333333}
    334334
    335 int
    336 tr_comparePointers( const void * a, const void * b )
    337 {
    338     return a - b;
    339 }
    340 
    341335/**
    342336***
  • branches/encryption/libtransmission/utils.h

    r3049 r3104  
    155155int tr_compareUint64( uint64_t a, uint64_t b );
    156156
    157 int tr_comparePointers( const void * a, const void * b );
    158 
    159157/***
    160158****
Note: See TracChangeset for help on using the changeset viewer.