Ignore:
Timestamp:
Sep 28, 2007, 4:40:21 PM (15 years ago)
Author:
charles
Message:

commit the next step of tiennou's fastpeers code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/peer-mgr.c

    r3225 r3227  
    2020#include "clients.h"
    2121#include "completion.h"
     22#include "crypto.h"
    2223#include "handshake.h"
    2324#include "net.h"
     
    275276**/
    276277
     278struct tr_bitfield *
     279tr_peerMgrGenerateAllowedSet( const uint32_t         setCount,
     280                              const uint32_t         pieceCount,
     281                              const uint8_t          infohash[20],
     282                              const struct in_addr * ip )
     283{
     284    /* This has been checked against the spec example implementation. Feeding it with :
     285    setCount = 9, pieceCount = 1313, infohash = Oxaa,0xaa,...0xaa, ip = 80.4.4.200
     286generate :
     287    1059, 431, 808, 1217, 287, 376, 1188, 353, 508
     288    but since we're storing in a bitfield, it won't be in this order... */
     289    /* TODO : We should translate link-local IPv4 adresses to external IP,
     290     * so that being on same local network gives us the same allowed pieces */
     291   
     292    printf( "%d piece allowed fast set for torrent with %d pieces and hex infohash\n", setCount, pieceCount );
     293    printf( "%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x for node with IP %s:\n",
     294            infohash[0], infohash[1], infohash[2], infohash[3], infohash[4], infohash[5], infohash[6], infohash[7], infohash[8], infohash[9],
     295            infohash[10], infohash[11], infohash[12], infohash[13], infohash[14], infohash[15], infohash[16], infohash[7], infohash[18], infohash[19],
     296            inet_ntoa( *ip ) );
     297   
     298    uint8_t *seed = malloc(4 + SHA_DIGEST_LENGTH);
     299    char buf[4];
     300    uint32_t allowedPieceCount = 0;
     301    tr_bitfield_t * ret;
     302   
     303    ret = tr_bitfieldNew( pieceCount );
     304   
     305    /* We need a seed based on most significant bytes of peer address
     306        concatenated with torrent's infohash */
     307    *(uint32_t*)buf = ntohl( htonl(ip->s_addr) & 0xffffff00 );
     308   
     309    memcpy( seed, &buf, 4 );
     310    memcpy( seed + 4, infohash, SHA_DIGEST_LENGTH );
     311   
     312    tr_sha1( seed, seed, 4 + SHA_DIGEST_LENGTH, NULL );
     313   
     314    while ( allowedPieceCount < setCount )
     315    {
     316        int i;
     317        for ( i = 0 ; i < 5 && allowedPieceCount < setCount ; i++ )
     318        {
     319            /* We generate indices from 4-byte chunks of the seed */
     320            uint32_t j = i * 4;
     321            uint32_t y = ntohl( *(uint32_t*)(seed + j) );
     322            uint32_t index = y % pieceCount;
     323           
     324            if ( !tr_bitfieldHas( ret, index ) )
     325            {
     326                tr_bitfieldAdd( ret, index );
     327                allowedPieceCount++;
     328            }
     329        }
     330        /* We randomize the seed, in case we need to iterate more */
     331        tr_sha1( seed, seed, SHA_DIGEST_LENGTH, NULL );
     332    }
     333    tr_free( seed );
     334   
     335    return ret;
     336}
     337
    277338tr_peerMgr*
    278339tr_peerMgrNew( tr_handle * handle )
     
    323384    uint32_t piece;
    324385    uint32_t peerCount;
     386    uint32_t fastAllowed;
    325387};
    326388
     
    338400    if (a->peerCount != b->peerCount)
    339401        return a->peerCount < b->peerCount ? -1 : 1;
     402   
     403    /* otherwise if one *might be* fastallowed to us */
     404    if (a->fastAllowed != b->fastAllowed)
     405        return a->fastAllowed < b->fastAllowed ? -1 : 1;
    340406
    341407    /* otherwise go with the earlier piece */
     
    389455            setme->priority = inf->pieces[piece].priority;
    390456            setme->peerCount = 0;
     457            /* FIXME */
     458//            setme->fastAllowed = tr_bitfieldHas( t->tor->allowedList, i);
    391459
    392460            for( k=0; k<peerCount; ++k ) {
     
    11181186    tr_peer ** peers = getConnectedPeers( t, &peerCount );
    11191187    ChokeData * choke = tr_new0( ChokeData, peerCount );
    1120 
     1188   
    11211189    /* sort the peers by preference and rate */
    11221190    for( i=0; i<peerCount; ++i )
Note: See TracChangeset for help on using the changeset viewer.