From 33c9c530d00f6993118ee23cc4cde1b16c7ad978 Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Mon, 10 Sep 2007 02:43:11 +0000 Subject: An announce with event=stopped now returns correct number of leechers and seeders. In TCP and UDP. --- opentracker.c | 19 ++++++------------- trackerlogic.c | 45 ++++++++++++++++++++++++++++++++++++++------- trackerlogic.h | 5 +++-- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/opentracker.c b/opentracker.c index 4ffde37..646184c 100644 --- a/opentracker.c +++ b/opentracker.c @@ -441,10 +441,9 @@ ANNOUNCE_WORKAROUND: reply_size = sprintf( static_outbuf + SUCCESS_HTTP_HEADER_LENGTH, "d14:failure reason81:Your client forgot to send your torrent's info_hash. Please upgrade your client.e" ); break; } - if( OT_FLAG( &peer ) & PEER_FLAG_STOPPED ) { - remove_peer_from_torrent( hash, &peer ); - reply_size = sprintf( static_outbuf + SUCCESS_HTTP_HEADER_LENGTH, "d8:completei0e10:incompletei0e8:intervali%ie5:peers0:e", OT_CLIENT_REQUEST_INTERVAL_RANDOM ); - } else { + if( OT_FLAG( &peer ) & PEER_FLAG_STOPPED ) + reply_size = remove_peer_from_torrent( hash, &peer, SUCCESS_HTTP_HEADER_LENGTH + static_outbuf, 1 ); + else { torrent = add_peer_to_torrent( hash, &peer, 0 ); if( !torrent || !( reply_size = return_peers_for_torrent( torrent, numwant, SUCCESS_HTTP_HEADER_LENGTH + static_outbuf, 1 ) ) ) HTTPERROR_500; } @@ -680,15 +679,9 @@ static void handle_udp4( int64 serversocket ) { outpacket[0] = htonl( 1 ); /* announce action */ outpacket[1] = inpacket[12/4]; - if( OT_FLAG( &peer ) & PEER_FLAG_STOPPED ) { - /* Peer is gone. */ - remove_peer_from_torrent( hash, &peer ); - - /* Create fake packet to satisfy parser on the other end */ - outpacket[2] = htonl( OT_CLIENT_REQUEST_INTERVAL_RANDOM ); - outpacket[3] = outpacket[4] = 0; - r = 20; - } else { + if( OT_FLAG( &peer ) & PEER_FLAG_STOPPED ) /* Peer is gone. */ + r = remove_peer_from_torrent( hash, &peer, static_outbuf, 0 ); + else { torrent = add_peer_to_torrent( hash, &peer, 0 ); if( !torrent ) return; /* XXX maybe send error */ diff --git a/trackerlogic.c b/trackerlogic.c index c8b9e03..4306822 100644 --- a/trackerlogic.c +++ b/trackerlogic.c @@ -693,19 +693,50 @@ size_t return_stats_for_slash24s( char *reply, size_t amount, ot_dword thresh ) return r - reply; } -void remove_peer_from_torrent( ot_hash *hash, ot_peer *peer ) { - int exactmatch, i; +size_t remove_peer_from_torrent( ot_hash *hash, ot_peer *peer, char *reply, int is_tcp ) { + int exactmatch; + size_t peer_count, seed_count, index; ot_vector *torrents_list = &all_torrents[*hash[0]]; ot_torrent *torrent = binary_search( hash, torrents_list->data, torrents_list->size, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch ); - if( !exactmatch ) return; + if( !exactmatch ) { + if( is_tcp ) + return sprintf( reply, "d8:completei0e10:incompletei0e8:intervali%ie5:peers0:e", OT_CLIENT_REQUEST_INTERVAL_RANDOM ); - for( i=0; ipeer_list->peers[i], peer, i == 0 ) ) { + /* Create fake packet to satisfy parser on the other end */ + ((ot_dword*)reply)[2] = htonl( OT_CLIENT_REQUEST_INTERVAL_RANDOM ); + ((ot_dword*)reply)[3] = ((ot_dword*)reply)[4] = 0; + return (size_t)20; + } + + for( peer_count = seed_count = index = 0; indexpeer_list->peers[index].size; + seed_count += torrent->peer_list->seed_count[index]; + + switch( vector_remove_peer( &torrent->peer_list->peers[index], peer, index == 0 ) ) { case 0: continue; - case 2: torrent->peer_list->seed_count[i]--; - case 1: default: return; + case 2: torrent->peer_list->seed_count[index]--; + seed_count--; + case 1: default: + peer_count--; + goto exit_loop; } + } + +exit_loop: + for( ++index; index < OT_POOLS_COUNT; ++index ) { + peer_count += torrent->peer_list->peers[index].size; + seed_count += torrent->peer_list->seed_count[index]; + } + + if( is_tcp ) + return sprintf( reply, "d8:completei%zde10:incompletei%zde8:intervali%ie5:peers0:e", seed_count, peer_count - seed_count, OT_CLIENT_REQUEST_INTERVAL_RANDOM ); + + /* else { Handle UDP reply */ + ((ot_dword*)reply)[2] = htonl( OT_CLIENT_REQUEST_INTERVAL_RANDOM ); + ((ot_dword*)reply)[3] = peer_count - seed_count; + ((ot_dword*)reply)[4] = seed_count; + return (size_t)20; } int init_logic( const char * const serverdir ) { diff --git a/trackerlogic.h b/trackerlogic.h index 8fcf66e..cb67244 100644 --- a/trackerlogic.h +++ b/trackerlogic.h @@ -87,7 +87,7 @@ void deinit_logic( void ); enum { STATS_MRTG, STATS_TOP5, STATS_DMEM, STATS_TCP, STATS_UDP, STATS_SLASH24S, SYNC_IN, SYNC_OUT }; ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer, int from_changeset ); -int add_changeset_to_tracker( ot_byte *data, size_t len ); +size_t remove_peer_from_torrent( ot_hash *hash, ot_peer *peer, char *reply, int is_tcp ); size_t return_peers_for_torrent( ot_torrent *torrent, size_t amount, char *reply, int is_tcp ); size_t return_fullscrape_for_tracker( char **reply ); size_t return_tcp_scrape_for_torrent( ot_hash *hash, char *reply ); @@ -96,8 +96,9 @@ size_t return_stats_for_tracker( char *reply, int mode ); size_t return_stats_for_slash24s( char *reply, size_t amount, ot_dword thresh ); size_t return_memstat_for_tracker( char **reply ); size_t return_changeset_for_tracker( char **reply ); +int add_changeset_to_tracker( ot_byte *data, size_t len ); void clean_all_torrents( void ); -void remove_peer_from_torrent( ot_hash *hash, ot_peer *peer ); + #if defined ( WANT_BLACKLISTING ) || defined ( WANT_CLOSED_TRACKER ) int accesslist_addentry( ot_hash *hash ); void accesslist_reset( void ); -- cgit v1.2.3