diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | opentracker.c | 36 | ||||
| -rw-r--r-- | trackerlogic.c | 51 | ||||
| -rw-r--r-- | trackerlogic.h | 6 |
4 files changed, 86 insertions, 9 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | CC?=gcc | 1 | CC?=gcc |
| 2 | CFLAGS+=-I../libowfat -Wall -pipe -Os # -DWANT_IP_FROM_QUERY_STRING -g -ggdb | 2 | CFLAGS+=-I../libowfat -Wall -pipe -Os -DWANT_BLACKLIST -DWANT_CLOSED_TRACKER # -DWANT_IP_FROM_QUERY_STRING -g -ggdb |
| 3 | LDFLAGS+=-L../libowfat/ -lowfat -s -lm | 3 | LDFLAGS+=-L../libowfat/ -lowfat -s -lm |
| 4 | 4 | ||
| 5 | HEADERS=trackerlogic.h scan_urlencoded_query.h | 5 | 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 ) { | |||
| 339 | } | 339 | } |
| 340 | } | 340 | } |
| 341 | 341 | ||
| 342 | int main() | 342 | void usage( char *name ) { |
| 343 | { | 343 | fprintf( stderr, "Usage: %s [-i serverip] [-p serverport] [-d serverdirectory]" |
| 344 | #ifdef WANT_CLOSED_TRACKER | ||
| 345 | " [-o|c]" | ||
| 346 | #endif | ||
| 347 | "\n", name ); | ||
| 348 | exit(1); | ||
| 349 | } | ||
| 350 | |||
| 351 | int main( int argc, char **argv ) { | ||
| 344 | int s=socket_tcp4(); | 352 | int s=socket_tcp4(); |
| 345 | unsigned long ip; | 353 | unsigned long ip; |
| 346 | uint16 port; | 354 | char *serverip = NULL; |
| 355 | char *serverdir = "."; | ||
| 356 | uint16 port = 6969; | ||
| 357 | |||
| 358 | while( 1 ) { | ||
| 359 | switch( getopt(argc,argv,":i:p:d:") ) { | ||
| 360 | case -1: goto allparsed; | ||
| 361 | case 'i': serverip = optarg; break; | ||
| 362 | case 'p': port = (uint16)atol( optarg ); break; | ||
| 363 | case 'd': serverdir = optarg; break; | ||
| 364 | #ifdef WANT_CLOSED_TRACKER | ||
| 365 | case 'o': g_closedtracker = 0; | ||
| 366 | case 'c': g_closedtracker = 1; | ||
| 367 | #endif | ||
| 368 | default: | ||
| 369 | case '?': usage( argv[0] ); | ||
| 370 | } | ||
| 371 | } | ||
| 347 | 372 | ||
| 373 | allparsed: | ||
| 348 | ot_start_time = time( NULL ); | 374 | ot_start_time = time( NULL ); |
| 349 | if (socket_bind4_reuse(s,NULL,6969)==-1) | 375 | if (socket_bind4_reuse(s,serverip,port)==-1) |
| 350 | panic("socket_bind4_reuse"); | 376 | panic("socket_bind4_reuse"); |
| 351 | 377 | ||
| 352 | if (socket_listen(s,16)==-1) | 378 | if (socket_listen(s,16)==-1) |
| @@ -356,7 +382,7 @@ int main() | |||
| 356 | panic("io_fd"); | 382 | panic("io_fd"); |
| 357 | 383 | ||
| 358 | signal( SIGINT, graceful ); | 384 | signal( SIGINT, graceful ); |
| 359 | if( init_logic( ) == -1 ) | 385 | if( init_logic( serverdir ) == -1 ) |
| 360 | panic("Logic not started"); | 386 | panic("Logic not started"); |
| 361 | 387 | ||
| 362 | io_wantread(s); | 388 | 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 @@ | |||
| 18 | #include "scan.h" | 18 | #include "scan.h" |
| 19 | #include "byte.h" | 19 | #include "byte.h" |
| 20 | 20 | ||
| 21 | #if defined( WANT_CLOSED_TRACKER ) || defined( WANT_BLACKLIST ) | ||
| 22 | #include <sys/stat.h> | ||
| 23 | #endif | ||
| 24 | |||
| 21 | // GLOBAL VARIABLES | 25 | // GLOBAL VARIABLES |
| 22 | // | 26 | // |
| 23 | static ot_vector all_torrents[256]; | 27 | static ot_vector all_torrents[256]; |
| 24 | 28 | ||
| 29 | #ifdef WANT_CLOSED_TRACKER | ||
| 30 | int g_closedtracker = 1; | ||
| 31 | static ot_torrent* const OT_TORRENT_NOT_ON_WHITELIST = (ot_torrent*)1; | ||
| 32 | #endif | ||
| 33 | |||
| 34 | #ifdef WANT_BLACKLIST | ||
| 35 | static ot_torrent* const OT_TORRENT_ON_BLACKLIST = (ot_torrent*)2; | ||
| 36 | #endif | ||
| 37 | |||
| 25 | // This function gives us a binary search that returns a pointer, even if | 38 | // This function gives us a binary search that returns a pointer, even if |
| 26 | // no exact match is found. In that case it sets exactmatch 0 and gives | 39 | // no exact match is found. In that case it sets exactmatch 0 and gives |
| 27 | // calling functions the chance to insert data | 40 | // calling functions the chance to insert data |
| @@ -48,7 +61,7 @@ static void *binary_search( const void *key, const void *base, unsigned long mem | |||
| 48 | // Converter function from memory to human readable hex strings | 61 | // Converter function from memory to human readable hex strings |
| 49 | // * definitely not thread safe!!! | 62 | // * definitely not thread safe!!! |
| 50 | // | 63 | // |
| 51 | char ths[1+2*20];char*to_hex(ot_byte*s){char*m="0123456789ABCDEF";char*e=ths+40;char*t=ths;while(t<e){*t++=m[*s>>4];*t++=m[*s++&15];}*t=0;return ths;} | 64 | char ths[2+2*20]="-";char*to_hex(ot_byte*s){char*m="0123456789ABCDEF";char*e=ths+41;char*t=ths+1;while(t<e){*t++=m[*s>>4];*t++=m[*s++&15];}*t=0;return ths+1;} |
| 52 | 65 | ||
| 53 | static void *vector_find_or_insert( ot_vector *vector, void *key, size_t member_size, int compare_size, int *exactmatch ) { | 66 | static void *vector_find_or_insert( ot_vector *vector, void *key, size_t member_size, int compare_size, int *exactmatch ) { |
| 54 | ot_byte *match = BINARY_FIND( key, vector->data, vector->size, member_size, compare_size, exactmatch ); | 67 | 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 ) { | |||
| 141 | ot_torrent *torrent; | 154 | ot_torrent *torrent; |
| 142 | ot_peer *peer_dest; | 155 | ot_peer *peer_dest; |
| 143 | ot_vector *torrents_list = &all_torrents[*hash[0]], *peer_pool; | 156 | ot_vector *torrents_list = &all_torrents[*hash[0]], *peer_pool; |
| 157 | #if defined( WANT_CLOSED_TRACKER ) || defined( WANT_BLACKLIST ) | ||
| 158 | struct stat dummy_sb; | ||
| 159 | char *fn = to_hex( (ot_byte*)hash ); | ||
| 160 | #endif | ||
| 161 | |||
| 162 | #ifdef WANT_CLOSED_TRACKER | ||
| 163 | if( g_closedtracker && stat( fn, &dummy_sb ) ) | ||
| 164 | return OT_TORRENT_NOT_ON_WHITELIST; | ||
| 165 | #endif | ||
| 166 | |||
| 167 | #ifdef WANT_BLACKLIST | ||
| 168 | if( stat( fn - 1, &dummy_sb ) ) | ||
| 169 | return OT_TORRENT_ON_BLACKLIST; | ||
| 170 | #endif | ||
| 144 | 171 | ||
| 145 | torrent = vector_find_or_insert( torrents_list, (void*)hash, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch ); | 172 | torrent = vector_find_or_insert( torrents_list, (void*)hash, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch ); |
| 146 | if( !torrent ) return NULL; | 173 | if( !torrent ) return NULL; |
| @@ -200,6 +227,18 @@ size_t return_peers_for_torrent( ot_torrent *torrent, unsigned long amount, char | |||
| 200 | signed long pool_offset = -1, pool_index = 0; | 227 | signed long pool_offset = -1, pool_index = 0; |
| 201 | signed long wert = -1; | 228 | signed long wert = -1; |
| 202 | 229 | ||
| 230 | #ifdef WANT_CLOSED_TRACKER | ||
| 231 | if( torrent == OT_TORRENT_NOT_ON_WHITELIST ) { | ||
| 232 | return( FORMAT_FORMAT_STRING( reply, "d14:failure reason43:This torrent is not served by this tracker.e" ) ); | ||
| 233 | } | ||
| 234 | #endif | ||
| 235 | |||
| 236 | #ifdef WANT_BLACKLIST | ||
| 237 | if( torrent == OT_TORRENT_ON_BLACKLIST ) { | ||
| 238 | return( FORMAT_FORMAT_STRING( reply, "d14:failure reason29:This torrent is black listed.e" ) ); | ||
| 239 | } | ||
| 240 | #endif | ||
| 241 | |||
| 203 | for( peer_count=seed_count=index=0; index<OT_POOLS_COUNT; ++index) { | 242 | for( peer_count=seed_count=index=0; index<OT_POOLS_COUNT; ++index) { |
| 204 | peer_count += torrent->peer_list->peers[index].size; | 243 | peer_count += torrent->peer_list->peers[index].size; |
| 205 | seed_count += torrent->peer_list->seed_count[index]; | 244 | seed_count += torrent->peer_list->seed_count[index]; |
| @@ -260,6 +299,9 @@ void remove_peer_from_torrent( ot_hash *hash, ot_peer *peer ) { | |||
| 260 | 299 | ||
| 261 | // Maybe this does the job | 300 | // Maybe this does the job |
| 262 | if( clean_peerlist( torrent->peer_list ) ) { | 301 | if( clean_peerlist( torrent->peer_list ) ) { |
| 302 | #ifdef WANT_CLOSED_TRACKER | ||
| 303 | if( !g_closedtracker ) | ||
| 304 | #endif | ||
| 263 | vector_remove_torrent( torrents_list, hash ); | 305 | vector_remove_torrent( torrents_list, hash ); |
| 264 | return; | 306 | return; |
| 265 | } | 307 | } |
| @@ -276,7 +318,12 @@ void cleanup_torrents( void ) { | |||
| 276 | 318 | ||
| 277 | } | 319 | } |
| 278 | 320 | ||
| 279 | int init_logic( ) { | 321 | int init_logic( char *serverdir ) { |
| 322 | if( serverdir && chdir( serverdir ) ) { | ||
| 323 | fprintf( stderr, "Could not chdir() to %s\n", serverdir ); | ||
| 324 | return -1; | ||
| 325 | } | ||
| 326 | |||
| 280 | srandom( time(NULL)); | 327 | srandom( time(NULL)); |
| 281 | 328 | ||
| 282 | // Initialize control structures | 329 | // 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 { | |||
| 75 | // Exported functions | 75 | // Exported functions |
| 76 | // | 76 | // |
| 77 | 77 | ||
| 78 | int init_logic( ); | 78 | int init_logic( char *serverdir ); |
| 79 | void deinit_logic( ); | 79 | void deinit_logic( ); |
| 80 | 80 | ||
| 81 | #ifdef WANT_CLOSED_TRACKER | ||
| 82 | extern int g_closedtracker; | ||
| 83 | #endif | ||
| 84 | |||
| 81 | ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer ); | 85 | ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer ); |
| 82 | size_t return_peers_for_torrent( ot_torrent *torrent, unsigned long amount, char *reply ); | 86 | size_t return_peers_for_torrent( ot_torrent *torrent, unsigned long amount, char *reply ); |
| 83 | size_t return_scrape_for_torrent( ot_hash *hash, char *reply ); | 87 | size_t return_scrape_for_torrent( ot_hash *hash, char *reply ); |
