From 697e239027b1490eaa51e1d2dfc1f93b9941e07b Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Mon, 12 Nov 2007 04:39:53 +0000 Subject: Moving accesslist stuff to its own object --- Makefile | 11 +++++-- opentracker.c | 60 +++++------------------------------- ot_accesslist.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ot_accesslist.h | 22 +++++++++++++ trackerlogic.c | 38 ++--------------------- trackerlogic.h | 5 --- 6 files changed, 135 insertions(+), 96 deletions(-) create mode 100644 ot_accesslist.c create mode 100644 ot_accesslist.h diff --git a/Makefile b/Makefile index 9422087..ec1b1ad 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,18 @@ CC?=gcc -FEATURES=#-DWANT_TRACKER_SYNC -DWANT_BLACKLISTING -DWANT_CLOSED_TRACKER -DWANT_UTORRENT1600_WORKAROUND #-DWANT_IP_FROM_QUERY_STRING -D_DEBUG_HTTPERROR +#FEATURES =-DWANT_TRACKER_SYNC +#FEATURES+=-DWANT_BLACKLISTING +#FEATURES+=-DWANT_CLOSED_TRACKER +#FEATURES+=-DWANT_UTORRENT1600_WORKAROUND +#FEATURES+=-DWANT_IP_FROM_QUERY_STRING +#FEATURES+=-D_DEBUG_HTTPERROR OPTS_debug=-g -ggdb #-pg # -fprofile-arcs -ftest-coverage OPTS_production=-Os CFLAGS+=-I../libowfat -Wall -pipe -Wextra #-pedantic -ansi LDFLAGS+=-L../libowfat/ -lowfat BINARY =opentracker -HEADERS=trackerlogic.h scan_urlencoded_query.h ot_mutex.h ot_stats.h ot_sync.h ot_vector.h ot_clean.h ot_udp.h ot_iovec.h ot_fullscrape.h -SOURCES=opentracker.c trackerlogic.c scan_urlencoded_query.c ot_mutex.c ot_stats.c ot_sync.c ot_vector.c ot_clean.c ot_udp.c ot_iovec.c ot_fullscrape.c +HEADERS=trackerlogic.h scan_urlencoded_query.h ot_mutex.h ot_stats.h ot_sync.h ot_vector.h ot_clean.h ot_udp.h ot_iovec.h ot_fullscrape.h ot_accesslist.h +SOURCES=opentracker.c trackerlogic.c scan_urlencoded_query.c ot_mutex.c ot_stats.c ot_sync.c ot_vector.c ot_clean.c ot_udp.c ot_iovec.c ot_fullscrape.c ot_accesslist.c OBJECTS = $(SOURCES:%.c=%.o) OBJECTS_debug = $(SOURCES:%.c=%.debug.o) diff --git a/opentracker.c b/opentracker.c index 15f4871..3c2aab7 100644 --- a/opentracker.c +++ b/opentracker.c @@ -38,6 +38,7 @@ #include "ot_udp.h" #include "ot_fullscrape.h" #include "ot_iovec.h" +#include "ot_accesslist.h" /* Globals */ static const size_t SUCCESS_HTTP_HEADER_LENGTH = 80; @@ -47,14 +48,6 @@ static unsigned int g_adminip_count = 0; time_t ot_start_time; time_t g_now; -#if defined ( WANT_BLACKLISTING ) && defined (WANT_CLOSED_TRACKER ) - #error WANT_BLACKLISTING and WANT_CLOSED_TRACKER are exclusive. -#endif -#if defined ( WANT_BLACKLISTING ) || defined (WANT_CLOSED_TRACKER ) -static char *accesslist_filename = NULL; -#define WANT_ACCESS_CONTROL -#endif - #ifndef WANT_TRACKER_SYNC #define add_peer_to_torrent(A,B,C) add_peer_to_torrent(A,B) #endif @@ -331,12 +324,13 @@ LOG_TO_STDERR( "sync: %d.%d.%d.%d\n", h->ip[0], h->ip[1], h->ip[2], h->ip[3] ); } if( mode == SYNC_OUT ) { + char *reply; if( !( reply_size = return_changeset_for_tracker( &reply ) ) ) HTTPERROR_500; return sendmmapdata( s, reply, reply_size ); } /* Simple but proof for now */ - reply = "OK"; + memmove( static_outbuf + SUCCESS_HTTP_HEADER_LENGTH, "OK", 2); reply_size = 2; break; @@ -772,48 +766,14 @@ static void ot_try_bind( char ip[4], uint16 port, int is_tcp ) { ++ot_sockets_count; } -#ifdef WANT_ACCESS_CONTROL -/* Read initial access list */ -void read_accesslist_file( int foo ) { - FILE * accesslist_filehandle; - ot_hash infohash; - foo = foo; - - accesslist_filehandle = fopen( accesslist_filename, "r" ); - - /* Free accesslist vector in trackerlogic.c*/ - accesslist_reset(); - - if( accesslist_filehandle == NULL ) { - fprintf( stderr, "Warning: Can't open accesslist file: %s (but will try to create it later, if necessary and possible).", accesslist_filename ); - return; - } - - /* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" */ - while( fgets( static_inbuf, sizeof(static_inbuf), accesslist_filehandle ) ) { - int i; - for( i=0; i<20; ++i ) { - int eger = 16 * scan_fromhex( static_inbuf[ 2*i ] ) + scan_fromhex( static_inbuf[ 1 + 2*i ] ); - if( eger < 0 ) - continue; - infohash[i] = eger; - } - if( scan_fromhex( static_inbuf[ 40 ] ) >= 0 ) - continue; - - /* Append accesslist to accesslist vector */ - accesslist_addentry( &infohash ); - } - - fclose( accesslist_filehandle ); -} -#endif - int main( int argc, char **argv ) { struct passwd *pws = NULL; char serverip[4] = {0,0,0,0}; char *serverdir = "."; int scanon = 1; +#ifdef WANT_ACCESS_CONTROL + char *accesslist_filename = NULL; +#endif while( scanon ) { switch( getopt( argc, argv, ":i:p:A:P:d:" @@ -863,13 +823,7 @@ int main( int argc, char **argv ) { } endpwent(); -#ifdef WANT_ACCESS_CONTROL - /* Passing "0" since read_blacklist_file also is SIGHUP handler */ - if( accesslist_filename ) { - read_accesslist_file( 0 ); - signal( SIGHUP, read_accesslist_file ); - } -#endif + accesslist_init( accesslist_filename ); signal( SIGPIPE, SIG_IGN ); signal( SIGINT, signal_handler ); diff --git a/ot_accesslist.c b/ot_accesslist.c new file mode 100644 index 0000000..e63cff8 --- /dev/null +++ b/ot_accesslist.c @@ -0,0 +1,95 @@ +/* This software was written by Dirk Engling + It is considered beerware. Prost. Skol. Cheers or whatever. */ + +/* System */ +#include +#include +#include + +/* Libowfat */ +#include "byte.h" +#include "scan.h" + +/* Opentracker */ +#include "ot_accesslist.h" + +/* GLOBAL VARIABLES */ +#ifdef WANT_ACCESS_CONTROL +static char *accesslist_filename = NULL; +static ot_vector accesslist; + +static void accesslist_reset( void ) { + free( accesslist.data ); + byte_zero( &accesslist, sizeof( accesslist ) ); +} + +static 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; +} + +/* Read initial access list */ +static void accesslist_readfile( int foo ) { + FILE * accesslist_filehandle; + ot_hash infohash; + foo = foo; + + accesslist_filehandle = fopen( accesslist_filename, "r" ); + + /* Free accesslist vector in trackerlogic.c*/ + accesslist_reset(); + + if( accesslist_filehandle == NULL ) { + fprintf( stderr, "Warning: Can't open accesslist file: %s (but will try to create it later, if necessary and possible).", accesslist_filename ); + return; + } + + /* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" */ + while( fgets( static_inbuf, sizeof(static_inbuf), accesslist_filehandle ) ) { + int i; + for( i=0; i<20; ++i ) { + int eger = 16 * scan_fromhex( static_inbuf[ 2*i ] ) + scan_fromhex( static_inbuf[ 1 + 2*i ] ); + if( eger < 0 ) + continue; + infohash[i] = eger; + } + if( scan_fromhex( static_inbuf[ 40 ] ) >= 0 ) + continue; + + /* Append accesslist to accesslist vector */ + accesslist_addentry( &infohash ); + } + + fclose( accesslist_filehandle ); +} + +int accesslist_hashisvalid( ot_hash *hash ) { + int exactmatch; + binary_search( hash, accesslist.data, accesslist.size, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &exactmatch ); + +#ifdef WANT_BLACKLISTING + exactmatch = !exactmatch; +#endif + + return exactmatch; +} + +void accesslist_init( char *accesslist_filename_in ) { + byte_zero( &accesslist, sizeof( accesslist ) ); + + /* Passing "0" since read_blacklist_file also is SIGHUP handler */ + if( accesslist_filename_in ) { + accesslist_filename = accesslist_filename_in; + accesslist_readfile( 0 ); + signal( SIGHUP, accesslist_readfile ); + } +} + +#endif diff --git a/ot_accesslist.h b/ot_accesslist.h new file mode 100644 index 0000000..d24463d --- /dev/null +++ b/ot_accesslist.h @@ -0,0 +1,22 @@ +/* This software was written by Dirk Engling + It is considered beerware. Prost. Skol. Cheers or whatever. */ + +#ifndef __OT_ACCESSLIST_H__ +#define __OT_ACCESSLIST_H__ + +#include "trackerlogic.h" + +#if defined ( WANT_BLACKLISTING ) && defined (WANT_CLOSED_TRACKER ) + #error WANT_BLACKLISTING and WANT_CLOSED_TRACKER are exclusive. +#endif + +#if defined ( WANT_BLACKLISTING ) || defined (WANT_CLOSED_TRACKER ) +#define WANT_ACCESS_CONTROL +void accesslist_init( char *accesslist_filename ); +int accesslist_hashisvalid( ot_hash *hash ); +#else +#define accesslist_init( accesslist_filename ) +#define accesslist_hashisvalid( hash ) 1 +#endif + +#endif diff --git a/trackerlogic.c b/trackerlogic.c index 7f28903..d0d5235 100644 --- a/trackerlogic.c +++ b/trackerlogic.c @@ -21,12 +21,7 @@ #include "ot_mutex.h" #include "ot_stats.h" #include "ot_clean.h" - -/* GLOBAL VARIABLES */ -#if defined ( WANT_BLACKLISTING ) || defined( WANT_CLOSED_TRACKER ) -static ot_vector accesslist; -#define WANT_ACCESS_CONTROL -#endif +#include "ot_accesslist.h" void free_peerlist( ot_peerlist *peer_list ) { size_t i; @@ -46,18 +41,10 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer WANT_TRACKER_SYNC ot_vector *torrents_list = mutex_bucket_lock_by_hash( hash ), *peer_pool; int base_pool = 0; -#ifdef WANT_ACCESS_CONTROL - binary_search( hash, accesslist.data, accesslist.size, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &exactmatch ); - -#ifdef WANT_CLOSED_TRACKER - exactmatch = !exactmatch; -#endif - - if( exactmatch ) { + if( !accesslist_hashisvalid( hash ) ) { mutex_bucket_unlock_by_hash( hash ); return NULL; } -#endif torrent = vector_find_or_insert( torrents_list, (void*)hash, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch ); if( !torrent ) { @@ -326,25 +313,6 @@ 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 ); @@ -352,7 +320,7 @@ int trackerlogic_init( const char * const serverdir ) { } srandom( time(NULL) ); - + clean_init( ); mutex_init( ); diff --git a/trackerlogic.h b/trackerlogic.h index b216e50..5e97e7c 100644 --- a/trackerlogic.h +++ b/trackerlogic.h @@ -107,11 +107,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 ); void clean_all_torrents( void ); -#if defined ( WANT_BLACKLISTING ) || defined ( WANT_CLOSED_TRACKER ) -int accesslist_addentry( ot_hash *hash ); -void accesslist_reset( void ); -#endif - /* Helper, before it moves to its own object */ void fix_mmapallocation( void *buf, size_t old_alloc, size_t new_alloc ); void free_peerlist( ot_peerlist *peer_list ); -- cgit v1.2.3