From 2aa03492a51e637931981f413a4da28abc75da97 Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Fri, 5 Jan 2007 12:25:44 +0000 Subject: added closed tracker and blacklist code --- Makefile | 2 +- opentracker.c | 36 +++++++++++++++++++++++++++++++----- trackerlogic.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- trackerlogic.h | 6 +++++- 4 files changed, 86 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 19163cc..4802c18 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC?=gcc -CFLAGS+=-I../libowfat -Wall -pipe -Os # -DWANT_IP_FROM_QUERY_STRING -g -ggdb +CFLAGS+=-I../libowfat -Wall -pipe -Os -DWANT_BLACKLIST -DWANT_CLOSED_TRACKER # -DWANT_IP_FROM_QUERY_STRING -g -ggdb LDFLAGS+=-L../libowfat/ -lowfat -s -lm HEADERS=trackerlogic.h scan_urlencoded_query.h diff --git a/opentracker.c b/opentracker.c index de98bb3..b992117 100644 --- a/opentracker.c +++ b/opentracker.c @@ -339,14 +339,40 @@ void graceful( int s ) { } } -int main() -{ +void usage( char *name ) { + fprintf( stderr, "Usage: %s [-i serverip] [-p serverport] [-d serverdirectory]" +#ifdef WANT_CLOSED_TRACKER + " [-o|c]" +#endif + "\n", name ); + exit(1); +} + +int main( int argc, char **argv ) { int s=socket_tcp4(); unsigned long ip; - uint16 port; + char *serverip = NULL; + char *serverdir = "."; + uint16 port = 6969; + + while( 1 ) { + switch( getopt(argc,argv,":i:p:d:") ) { + case -1: goto allparsed; + case 'i': serverip = optarg; break; + case 'p': port = (uint16)atol( optarg ); break; + case 'd': serverdir = optarg; break; +#ifdef WANT_CLOSED_TRACKER + case 'o': g_closedtracker = 0; + case 'c': g_closedtracker = 1; +#endif + default: + case '?': usage( argv[0] ); + } + } +allparsed: ot_start_time = time( NULL ); - if (socket_bind4_reuse(s,NULL,6969)==-1) + if (socket_bind4_reuse(s,serverip,port)==-1) panic("socket_bind4_reuse"); if (socket_listen(s,16)==-1) @@ -356,7 +382,7 @@ int main() panic("io_fd"); signal( SIGINT, graceful ); - if( init_logic( ) == -1 ) + if( init_logic( serverdir ) == -1 ) panic("Logic not started"); io_wantread(s); diff --git a/trackerlogic.c b/trackerlogic.c index 84330df..5a74e2c 100644 --- a/trackerlogic.c +++ b/trackerlogic.c @@ -18,10 +18,23 @@ #include "scan.h" #include "byte.h" +#if defined( WANT_CLOSED_TRACKER ) || defined( WANT_BLACKLIST ) +#include +#endif + // GLOBAL VARIABLES // static ot_vector all_torrents[256]; +#ifdef WANT_CLOSED_TRACKER +int g_closedtracker = 1; +static ot_torrent* const OT_TORRENT_NOT_ON_WHITELIST = (ot_torrent*)1; +#endif + +#ifdef WANT_BLACKLIST +static ot_torrent* const OT_TORRENT_ON_BLACKLIST = (ot_torrent*)2; +#endif + // 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 // calling functions the chance to insert data @@ -48,7 +61,7 @@ static void *binary_search( const void *key, const void *base, unsigned long mem // Converter function from memory to human readable hex strings // * definitely not thread safe!!! // -char ths[1+2*20];char*to_hex(ot_byte*s){char*m="0123456789ABCDEF";char*e=ths+40;char*t=ths;while(t>4];*t++=m[*s++&15];}*t=0;return ths;} +char ths[2+2*20]="-";char*to_hex(ot_byte*s){char*m="0123456789ABCDEF";char*e=ths+41;char*t=ths+1;while(t>4];*t++=m[*s++&15];}*t=0;return ths+1;} static void *vector_find_or_insert( ot_vector *vector, void *key, size_t member_size, int compare_size, int *exactmatch ) { ot_byte *match = BINARY_FIND( key, vector->data, vector->size, member_size, compare_size, exactmatch ); @@ -141,6 +154,20 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer ) { ot_torrent *torrent; ot_peer *peer_dest; ot_vector *torrents_list = &all_torrents[*hash[0]], *peer_pool; +#if defined( WANT_CLOSED_TRACKER ) || defined( WANT_BLACKLIST ) + struct stat dummy_sb; + char *fn = to_hex( (ot_byte*)hash ); +#endif + +#ifdef WANT_CLOSED_TRACKER + if( g_closedtracker && stat( fn, &dummy_sb ) ) + return OT_TORRENT_NOT_ON_WHITELIST; +#endif + +#ifdef WANT_BLACKLIST + if( stat( fn - 1, &dummy_sb ) ) + return OT_TORRENT_ON_BLACKLIST; +#endif torrent = vector_find_or_insert( torrents_list, (void*)hash, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch ); if( !torrent ) return NULL; @@ -200,6 +227,18 @@ size_t return_peers_for_torrent( ot_torrent *torrent, unsigned long amount, char signed long pool_offset = -1, pool_index = 0; signed long wert = -1; +#ifdef WANT_CLOSED_TRACKER + if( torrent == OT_TORRENT_NOT_ON_WHITELIST ) { + return( FORMAT_FORMAT_STRING( reply, "d14:failure reason43:This torrent is not served by this tracker.e" ) ); + } +#endif + +#ifdef WANT_BLACKLIST + if( torrent == OT_TORRENT_ON_BLACKLIST ) { + return( FORMAT_FORMAT_STRING( reply, "d14:failure reason29:This torrent is black listed.e" ) ); + } +#endif + for( peer_count=seed_count=index=0; indexpeer_list->peers[index].size; seed_count += torrent->peer_list->seed_count[index]; @@ -260,6 +299,9 @@ void remove_peer_from_torrent( ot_hash *hash, ot_peer *peer ) { // Maybe this does the job if( clean_peerlist( torrent->peer_list ) ) { +#ifdef WANT_CLOSED_TRACKER + if( !g_closedtracker ) +#endif vector_remove_torrent( torrents_list, hash ); return; } @@ -276,7 +318,12 @@ void cleanup_torrents( void ) { } -int init_logic( ) { +int init_logic( char *serverdir ) { + if( serverdir && chdir( serverdir ) ) { + fprintf( stderr, "Could not chdir() to %s\n", serverdir ); + return -1; + } + srandom( time(NULL)); // Initialize control structures diff --git a/trackerlogic.h b/trackerlogic.h index 06a9dd1..baa0bd1 100644 --- a/trackerlogic.h +++ b/trackerlogic.h @@ -75,9 +75,13 @@ typedef struct { // Exported functions // -int init_logic( ); +int init_logic( char *serverdir ); void deinit_logic( ); +#ifdef WANT_CLOSED_TRACKER +extern int g_closedtracker; +#endif + ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer ); size_t return_peers_for_torrent( ot_torrent *torrent, unsigned long amount, char *reply ); size_t return_scrape_for_torrent( ot_hash *hash, char *reply ); -- cgit v1.2.3