@ -62,8 +62,8 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer WANT_SYNC_PARAM(
if ( ! exactmatch ) {
if ( ! exactmatch ) {
/* Create a new torrent entry, then */
/* Create a new torrent entry, then */
memmove ( & torrent - > hash , hash , sizeof ( ot_hash ) ) ;
int i ; for ( i = 0 ; i < 20 ; i + = 4 ) WRITE32 ( & torrent - > hash , i , READ32 ( hash , i ) ) ;
if ( ! ( torrent - > peer_list = malloc ( sizeof ( ot_peerlist ) ) ) ) {
if ( ! ( torrent - > peer_list = malloc ( sizeof ( ot_peerlist ) ) ) ) {
vector_remove_torrent ( torrents_list , torrent ) ;
vector_remove_torrent ( torrents_list , torrent ) ;
mutex_bucket_unlock_by_hash ( hash ) ;
mutex_bucket_unlock_by_hash ( hash ) ;
@ -86,8 +86,8 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer WANT_SYNC_PARAM(
OT_PEERTIME ( peer ) = 0 ;
OT_PEERTIME ( peer ) = 0 ;
/* Sanitize flags: Whoever claims to have completed download, must be a seeder */
/* Sanitize flags: Whoever claims to have completed download, must be a seeder */
if ( ( OT_ FLAG( peer ) & ( PEER_FLAG_COMPLETED | PEER_FLAG_SEEDING ) ) = = PEER_FLAG_COMPLETED )
if ( ( OT_ PEER FLAG( peer ) & ( PEER_FLAG_COMPLETED | PEER_FLAG_SEEDING ) ) = = PEER_FLAG_COMPLETED )
OT_ FLAG( peer ) ^ = PEER_FLAG_COMPLETED ;
OT_ PEER FLAG( peer ) ^ = PEER_FLAG_COMPLETED ;
/* If we hadn't had a match create peer there */
/* If we hadn't had a match create peer there */
if ( ! exactmatch ) {
if ( ! exactmatch ) {
@ -96,13 +96,13 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer WANT_SYNC_PARAM(
if ( ! from_sync )
if ( ! from_sync )
livesync_tell ( hash , peer ) ;
livesync_tell ( hash , peer ) ;
else
else
OT_ FLAG( peer ) | = PEER_FLAG_FROM_SYNC ;
OT_ PEER FLAG( peer ) | = PEER_FLAG_FROM_SYNC ;
# endif
# endif
torrent - > peer_list - > peer_count + + ;
torrent - > peer_list - > peer_count + + ;
if ( OT_ FLAG( peer ) & PEER_FLAG_COMPLETED )
if ( OT_ PEER FLAG( peer ) & PEER_FLAG_COMPLETED )
torrent - > peer_list - > down_count + + ;
torrent - > peer_list - > down_count + + ;
if ( OT_ FLAG( peer ) & PEER_FLAG_SEEDING )
if ( OT_ PEER FLAG( peer ) & PEER_FLAG_SEEDING )
torrent - > peer_list - > seed_count + + ;
torrent - > peer_list - > seed_count + + ;
} else {
} else {
@ -114,7 +114,7 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer WANT_SYNC_PARAM(
int i ;
int i ;
for ( i = 0 ; i < 20 ; + + i ) printf ( " %02X " , ( * hash ) [ i ] ) ;
for ( i = 0 ; i < 20 ; + + i ) printf ( " %02X " , ( * hash ) [ i ] ) ;
if ( g_this_peerid_data ) g_this_peerid_data [ g_this_peerid_len ] = 0 ;
if ( g_this_peerid_data ) g_this_peerid_data [ g_this_peerid_len ] = 0 ;
printf ( " %d.%d.%d.%d:%d \t %d %02X %s \n " , _ip [ 0 ] , _ip [ 1 ] , _ip [ 2 ] , _ip [ 3 ] , OT_PEERTIME ( peer_dest ) , * ( uint16_t * ) ( ( ( char * ) peer_dest ) + 4 ) , OT_ FLAG( peer_dest ) , g_this_peerid_data ? g_this_peerid_data : " - " ) ;
printf ( " %d.%d.%d.%d:%d \t %d %02X %s \n " , _ip [ 0 ] , _ip [ 1 ] , _ip [ 2 ] , _ip [ 3 ] , OT_PEERTIME ( peer_dest ) , * ( uint16_t * ) ( ( ( char * ) peer_dest ) + 4 ) , OT_ PEER FLAG( peer_dest ) , g_this_peerid_data ? g_this_peerid_data : " - " ) ;
}
}
# endif
# endif
@ -123,19 +123,19 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer WANT_SYNC_PARAM(
fresh " completed " reports */
fresh " completed " reports */
if ( ! from_sync ) {
if ( ! from_sync ) {
if ( OT_PEERTIME ( peer_dest ) > OT_CLIENT_SYNC_RENEW_BOUNDARY | |
if ( OT_PEERTIME ( peer_dest ) > OT_CLIENT_SYNC_RENEW_BOUNDARY | |
( ! ( OT_ FLAG( peer_dest ) & PEER_FLAG_COMPLETED ) & & ( OT_ FLAG( peer ) & PEER_FLAG_COMPLETED ) ) )
( ! ( OT_ PEER FLAG( peer_dest ) & PEER_FLAG_COMPLETED ) & & ( OT_ PEER FLAG( peer ) & PEER_FLAG_COMPLETED ) ) )
livesync_tell ( hash , peer ) ;
livesync_tell ( hash , peer ) ;
}
}
# endif
# endif
if ( ( OT_ FLAG( peer_dest ) & PEER_FLAG_SEEDING ) & & ! ( OT_ FLAG( peer ) & PEER_FLAG_SEEDING ) )
if ( ( OT_ PEER FLAG( peer_dest ) & PEER_FLAG_SEEDING ) & & ! ( OT_ PEER FLAG( peer ) & PEER_FLAG_SEEDING ) )
torrent - > peer_list - > seed_count - - ;
torrent - > peer_list - > seed_count - - ;
if ( ! ( OT_ FLAG( peer_dest ) & PEER_FLAG_SEEDING ) & & ( OT_ FLAG( peer ) & PEER_FLAG_SEEDING ) )
if ( ! ( OT_ PEER FLAG( peer_dest ) & PEER_FLAG_SEEDING ) & & ( OT_ PEER FLAG( peer ) & PEER_FLAG_SEEDING ) )
torrent - > peer_list - > seed_count + + ;
torrent - > peer_list - > seed_count + + ;
if ( ! ( OT_ FLAG( peer_dest ) & PEER_FLAG_COMPLETED ) & & ( OT_ FLAG( peer ) & PEER_FLAG_COMPLETED ) )
if ( ! ( OT_ PEER FLAG( peer_dest ) & PEER_FLAG_COMPLETED ) & & ( OT_ PEER FLAG( peer ) & PEER_FLAG_COMPLETED ) )
torrent - > peer_list - > down_count + + ;
torrent - > peer_list - > down_count + + ;
if ( OT_ FLAG( peer_dest ) & PEER_FLAG_COMPLETED )
if ( OT_ PEER FLAG( peer_dest ) & PEER_FLAG_COMPLETED )
OT_ FLAG( peer ) | = PEER_FLAG_COMPLETED ;
OT_ PEER FLAG( peer ) | = PEER_FLAG_COMPLETED ;
}
}
* ( uint64_t * ) ( peer_dest ) = * ( uint64_t * ) ( peer ) ;
* ( uint64_t * ) ( peer_dest ) = * ( uint64_t * ) ( peer ) ;
@ -163,8 +163,10 @@ static size_t return_peers_all( ot_peerlist *peer_list, char *reply ) {
for ( bucket = 0 ; bucket < num_buckets ; + + bucket ) {
for ( bucket = 0 ; bucket < num_buckets ; + + bucket ) {
ot_peer * peers = ( ot_peer * ) bucket_list [ bucket ] . data ;
ot_peer * peers = ( ot_peer * ) bucket_list [ bucket ] . data ;
size_t peer_count = bucket_list [ bucket ] . size ;
size_t peer_count = bucket_list [ bucket ] . size ;
while ( peer_count - - )
while ( peer_count - - ) {
memmove ( r + = 6 , peers + + , 6 ) ;
WRITE32 ( r + = 4 , 0 , READ32 ( peers , 0 ) ) ;
WRITE16 ( r + = 2 , 0 , READ16 ( peers + + , 4 ) ) ;
}
}
}
return r - reply ;
return r - reply ;
@ -194,6 +196,8 @@ static size_t return_peers_selection( ot_peerlist *peer_list, size_t amount, cha
bucket_offset = random ( ) % peer_list - > peer_count ;
bucket_offset = random ( ) % peer_list - > peer_count ;
while ( amount - - ) {
while ( amount - - ) {
ot_peer * peer ;
/* This is the aliased, non shifted range, next value may fall into */
/* This is the aliased, non shifted range, next value may fall into */
unsigned int diff = ( ( ( amount + 1 ) * shifted_step ) > > shift ) -
unsigned int diff = ( ( ( amount + 1 ) * shifted_step ) > > shift ) -
( ( amount * shifted_step ) > > shift ) ;
( ( amount * shifted_step ) > > shift ) ;
@ -203,9 +207,9 @@ static size_t return_peers_selection( ot_peerlist *peer_list, size_t amount, cha
bucket_offset - = bucket_list [ bucket_index ] . size ;
bucket_offset - = bucket_list [ bucket_index ] . size ;
bucket_index = ( bucket_index + 1 ) % num_buckets ;
bucket_index = ( bucket_index + 1 ) % num_buckets ;
}
}
peer = ( ( ot_peer * ) bucket_list [ bucket_index ] . data ) + bucket_offset ;
memmove( r , ( ( ot_peer * ) bucket_list [ bucket_index ] . data ) + bucket_offset , 6 ) ;
WRITE32( r + = 4 , 0 , READ32 ( peer , 0 ) ) ;
r + = 6 ;
WRITE16 ( r + = 2 , 0 , READ16 ( peer , 4 ) ) ;
}
}
return r - reply ;
return r - reply ;
}
}
@ -288,9 +292,11 @@ size_t return_tcp_scrape_for_torrent( ot_hash *hash_list, int amount, char *repl
if ( clean_single_torrent ( torrent ) ) {
if ( clean_single_torrent ( torrent ) ) {
vector_remove_torrent ( torrents_list , torrent ) ;
vector_remove_torrent ( torrents_list , torrent ) ;
} else {
} else {
memmove ( r , " 20: " , 3 ) ; memmove ( r + 3 , hash , 20 ) ;
int j ;
r + = sprintf ( r + 23 , " d8:completei%zde10:downloadedi%zde10:incompletei%zdee " ,
* r + + = ' 2 ' ; * r + + = ' 0 ' ; * r + + = ' : ' ;
torrent - > peer_list - > seed_count , torrent - > peer_list - > down_count , torrent - > peer_list - > peer_count - torrent - > peer_list - > seed_count ) + 23 ;
for ( j = 0 ; j < 20 ; j + = 4 ) WRITE32 ( r + = 4 , 0 , READ32 ( hash , i ) ) ;
r + = sprintf ( r , " d8:completei%zde10:downloadedi%zde10:incompletei%zdee " ,
torrent - > peer_list - > seed_count , torrent - > peer_list - > down_count , torrent - > peer_list - > peer_count - torrent - > peer_list - > seed_count ) ;
}
}
}
}
mutex_bucket_unlock_by_hash ( hash ) ;
mutex_bucket_unlock_by_hash ( hash ) ;
@ -310,7 +316,7 @@ size_t remove_peer_from_torrent( ot_hash *hash, ot_peer *peer, char *reply, PROT
# ifdef WANT_SYNC_LIVE
# ifdef WANT_SYNC_LIVE
if ( proto ! = FLAG_MCA ) {
if ( proto ! = FLAG_MCA ) {
OT_ FLAG( peer ) | = PEER_FLAG_STOPPED ;
OT_ PEER FLAG( peer ) | = PEER_FLAG_STOPPED ;
livesync_tell ( hash , peer ) ;
livesync_tell ( hash , peer ) ;
}
}
# endif
# endif