From 785a9f13bdda7dfd4c206914645d15df7cae2af6 Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Tue, 6 Nov 2007 03:21:03 +0000 Subject: Make to_hex thread safe. Get rid off old /24 counting code. Protect more bucket accesses by locks. --- trackerlogic.c | 129 ++++++++++++++++++--------------------------------------- trackerlogic.h | 1 - 2 files changed, 41 insertions(+), 89 deletions(-) diff --git a/trackerlogic.c b/trackerlogic.c index 80fc23e..2725399 100644 --- a/trackerlogic.c +++ b/trackerlogic.c @@ -56,10 +56,8 @@ static void *unlock_bucket_by_hash( ot_hash *hash ) { return NULL; } -/* Converter function from memory to human readable hex strings - - definitely not thread safe!!! -*/ -static char ths[2+2*20]="-";static char*to_hex(ot_byte*s){const char*m="0123456789ABCDEF";char*e=ths+41;char*t=ths+1;while(t>4];*t++=m[*s++&15];}*t=0;return ths+1;} +/* Converter function from memory to human readable hex strings */ +static char*to_hex(char*d,ot_byte*s){const char*m="0123456789ABCDEF";char*e=d+40;while(d>4];*d++=m[*s++&15];}*d=0;return d;} /* This function gives us a binary search that returns a pointer, even if no exact match is found. In that case it sets exactmatch 0 and gives @@ -409,10 +407,11 @@ size_t return_memstat_for_tracker( char **reply ) { for( i=0; isize; ++j ) { ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list; ot_hash *hash =&( ((ot_torrent*)(torrents_list->data))[j] ).hash; - r += sprintf( r, "\n%s:\n", to_hex( (ot_byte*)hash ) ); + r += sprintf( r, "\n%s:\n", to_hex( hex_out, (ot_byte*)hash) ); for( k=0; kpeers[k].size), (unsigned int)peer_list->peers[k].space ); } @@ -647,6 +646,7 @@ void clean_all_torrents( void ) { all_torrents_clean[bucket] = time_now; + mutex_bucket_lock( bucket ); torrents_list = all_torrents + bucket; for( i=0; isize; ++i ) { ot_torrent *torrent = ((ot_torrent*)(torrents_list->data)) + i; @@ -655,6 +655,7 @@ void clean_all_torrents( void ) { --i; continue; } } + mutex_bucket_unlock( bucket ); } typedef struct { size_t val; ot_torrent * torrent; } ot_record; @@ -664,13 +665,14 @@ size_t return_stats_for_tracker( char *reply, int mode ) { size_t torrent_count = 0, peer_count = 0, seed_count = 0, j; ot_record top5s[5], top5c[5]; char *r = reply; - int i; + int bucket; byte_zero( top5s, sizeof( top5s ) ); byte_zero( top5c, sizeof( top5c ) ); - for( i=0; isize; for( j=0; jsize; ++j ) { ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list; @@ -690,20 +692,21 @@ size_t return_stats_for_tracker( char *reply, int mode ) { } peer_count += peer_list->peer_count; seed_count += peer_list->seed_count; } + mutex_bucket_unlock( bucket ); } if( mode == STATS_TOP5 ) { + char hex_out[42]; int idx; r += sprintf( r, "Top5 torrents by peers:\n" ); for( idx=0; idx<5; ++idx ) if( top5c[idx].torrent ) - r += sprintf( r, "\t%zd\t%s\n", top5c[idx].val, to_hex(top5c[idx].torrent->hash) ); + r += sprintf( r, "\t%zd\t%s\n", top5c[idx].val, to_hex( hex_out, top5c[idx].torrent->hash) ); r += sprintf( r, "Top5 torrents by seeds:\n" ); for( idx=0; idx<5; ++idx ) if( top5s[idx].torrent ) - r += sprintf( r, "\t%zd\t%s\n", top5s[idx].val, to_hex(top5s[idx].torrent->hash) ); - } else { + r += sprintf( r, "\t%zd\t%s\n", top5s[idx].val, to_hex( hex_out, top5s[idx].torrent->hash) ); + } else r += sprintf( r, "%zd\n%zd\nopentracker serving %zd torrents\nopentracker", peer_count, seed_count, torrent_count ); - } return r - reply; } @@ -720,8 +723,9 @@ size_t return_stats_for_slash24s( char *reply, size_t amount, ot_dword thresh ) #define MSK_S24S (NUM_S24S-1) ot_dword *counts[ NUM_BUFS ]; - ot_dword slash24s[amount*2]; /* first dword amount, second dword subnet */ - size_t i, j, k, l; + ot_dword slash24s[amount*2]; /* first dword amount, second dword subnet */ + int bucket; + size_t i, j, k, l; char *r = reply; byte_zero( counts, sizeof( counts ) ); @@ -729,8 +733,9 @@ size_t return_stats_for_slash24s( char *reply, size_t amount, ot_dword thresh ) r += sprintf( r, "Stats for all /24s with more than %u announced torrents:\n\n", thresh ); - for( i=0; isize; ++j ) { ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list; for( k=0; ksize; ++j ) { - ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list; - for( k=0; kpeers[k].data; - size_t numpeers = peer_list->peers[k].size; - for( l=0; l>8 ] == 65335 ) - count[ ntohl(*(ot_dword*)(peers+l))>>8 ] = 65334; - } - } - } - - for( i=0; i<0x1000000; ++i ) - if( count[i] > thresh ) { - /* This subnet seems to announce more torrents than the last in our list */ - int insert_pos = amount - 1; - while( ( insert_pos >= 0 ) && ( count[i] > slash24s[ 2 * insert_pos ] ) ) - --insert_pos; - ++insert_pos; - memmove( slash24s + 2 * ( insert_pos + 1 ), slash24s + 2 * ( insert_pos ), 2 * sizeof( ot_dword ) * ( amount - insert_pos - 1 ) ); - slash24s[ 2 * insert_pos ] = count[i]; - slash24s[ 2 * insert_pos + 1 ] = i; - if( slash24s[ 2 * amount - 2 ] > thresh ) - thresh = slash24s[ 2 * amount - 2 ]; - } - - free( count ); - - for( i=0; i < amount; ++i ) - if( slash24s[ 2*i ] >= thresh ) { - unsigned long ip = slash24s[ 2*i +1 ]; - r += sprintf( r, "% 10ld %d.%d.%d.0/24\n", (long)slash24s[ 2*i ], (int)(ip >> 16), (int)(255 & ( ip >> 8 )), (int)(ip & 255) ); - } - - return r - reply; -} - size_t remove_peer_from_torrent( ot_hash *hash, ot_peer *peer, char *reply, int is_tcp ) { int exactmatch; size_t index; @@ -895,6 +848,25 @@ exit_loop: return (size_t)20; } +#ifdef WANT_ACCESS_CONTROL +void accesslist_reset( void ) { + free( accesslist.data ); + byte_zero( &accesslist, sizeof( accesslist ) ); +} + +int accesslist_addentry( ot_hash *infohash ) { + int em; + void *insert = vector_find_or_insert( &accesslist, infohash, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &em ); + + if( !insert ) + return -1; + + memmove( insert, infohash, OT_HASH_COMPARE_SIZE ); + + return 0; +} +#endif + int trackerlogic_init( const char * const serverdir ) { if( serverdir && chdir( serverdir ) ) { fprintf( stderr, "Could not chdir() to %s\n", serverdir ); @@ -929,22 +901,3 @@ void trackerlogic_deinit( void ) { mutex_deinit( ); } - -#ifdef WANT_ACCESS_CONTROL -void accesslist_reset( void ) { - free( accesslist.data ); - byte_zero( &accesslist, sizeof( accesslist ) ); -} - -int accesslist_addentry( ot_hash *infohash ) { - int em; - void *insert = vector_find_or_insert( &accesslist, infohash, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &em ); - - if( !insert ) - return -1; - - memmove( insert, infohash, OT_HASH_COMPARE_SIZE ); - - return 0; -} -#endif diff --git a/trackerlogic.h b/trackerlogic.h index 86737ab..5b03012 100644 --- a/trackerlogic.h +++ b/trackerlogic.h @@ -110,7 +110,6 @@ size_t return_tcp_scrape_for_torrent( ot_hash *hash, int amount, char *reply ); size_t return_udp_scrape_for_torrent( ot_hash *hash, char *reply ); 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_stats_for_slash24s_old( char *reply, size_t amount, ot_dword thresh ); size_t return_memstat_for_tracker( char **reply ); void clean_all_torrents( void ); -- cgit v1.2.3