Changeset 3227 for trunk/libtransmission/peer-mgr.c
- Timestamp:
- Sep 28, 2007, 4:40:21 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/peer-mgr.c
r3225 r3227 20 20 #include "clients.h" 21 21 #include "completion.h" 22 #include "crypto.h" 22 23 #include "handshake.h" 23 24 #include "net.h" … … 275 276 **/ 276 277 278 struct tr_bitfield * 279 tr_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 286 generate : 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 277 338 tr_peerMgr* 278 339 tr_peerMgrNew( tr_handle * handle ) … … 323 384 uint32_t piece; 324 385 uint32_t peerCount; 386 uint32_t fastAllowed; 325 387 }; 326 388 … … 338 400 if (a->peerCount != b->peerCount) 339 401 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; 340 406 341 407 /* otherwise go with the earlier piece */ … … 389 455 setme->priority = inf->pieces[piece].priority; 390 456 setme->peerCount = 0; 457 /* FIXME */ 458 // setme->fastAllowed = tr_bitfieldHas( t->tor->allowedList, i); 391 459 392 460 for( k=0; k<peerCount; ++k ) { … … 1118 1186 tr_peer ** peers = getConnectedPeers( t, &peerCount ); 1119 1187 ChokeData * choke = tr_new0( ChokeData, peerCount ); 1120 1188 1121 1189 /* sort the peers by preference and rate */ 1122 1190 for( i=0; i<peerCount; ++i )
Note: See TracChangeset
for help on using the changeset viewer.