Added whitelisting to reimplement the WANT_CLOSED_TRACKER functionality

dynamic-accesslists
erdgeist 18 years ago
parent 992058383a
commit 3e47339b6c

@ -38,8 +38,13 @@ static time_t ot_start_time;
static const size_t SUCCESS_HTTP_HEADER_LENGTH = 80; static const size_t SUCCESS_HTTP_HEADER_LENGTH = 80;
static const size_t SUCCESS_HTTP_SIZE_OFF = 17; static const size_t SUCCESS_HTTP_SIZE_OFF = 17;
static char g_adminip[4] = {0,0,0,0}; static char g_adminip[4] = {0,0,0,0};
#ifdef WANT_BLACKLISTING
static char *blacklist_filename = NULL; #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 #endif
/* To always have space for error messages ;) */ /* To always have space for error messages ;) */
@ -493,7 +498,13 @@ static void graceful( int s ) {
} }
static void usage( char *name ) { static void usage( char *name ) {
fprintf( stderr, "Usage: %s [-i ip] [-p port] [-P port] [-d dir] [-A ip]\n", name ); fprintf( stderr, "Usage: %s [-i ip] [-p port] [-P port] [-d dir] [-A ip]"
#ifdef WANT_BLACKLISTING
" [-b blacklistfile]"
#elif defined ( WANT_CLOSED_TRACKER )
" [-w whitelistfile]"
#endif
"\n", name );
} }
#define HELPLINE(opt,desc) fprintf(stderr, "\t%-10s%s\n",opt,desc) #define HELPLINE(opt,desc) fprintf(stderr, "\t%-10s%s\n",opt,desc)
@ -507,6 +518,8 @@ static void help( char *name ) {
HELPLINE("-A ip","bless an ip address as admin address (e.g. to allow syncs from this address)"); HELPLINE("-A ip","bless an ip address as admin address (e.g. to allow syncs from this address)");
#ifdef WANT_BLACKLISTING #ifdef WANT_BLACKLISTING
HELPLINE("-b file","specify blacklist file."); HELPLINE("-b file","specify blacklist file.");
#elif defined( WANT_CLOSED_TRACKER )
HELPLINE("-w file","specify whitelist file.");
#endif #endif
fprintf( stderr, "\nExample: ./opentracker -i 127.0.0.1 -p 6969 -P 6969 -i 10.1.1.23 -p 2710 -p 80\n" ); fprintf( stderr, "\nExample: ./opentracker -i 127.0.0.1 -p 6969 -P 6969 -i 10.1.1.23 -p 2710 -p 80\n" );
@ -756,27 +769,25 @@ static void ot_try_bind( char ip[4], uint16 port, int is_tcp ) {
++ot_sockets_count; ++ot_sockets_count;
} }
#ifdef WANT_BLACKLISTING #ifdef WANT_ACCESS_CONTROL
/* Read initial black list */ /* Read initial access list */
void read_blacklist_file( int foo ) { void read_accesslist_file( int foo ) {
FILE * blacklist_filehandle; FILE * accesslist_filehandle;
ot_hash infohash; ot_hash infohash;
foo = foo; foo = foo;
signal( SIGHUP, SIG_IGN ); accesslist_filehandle = fopen( accesslist_filename, "r" );
blacklist_filehandle = fopen( blacklist_filename, "r" );
/* Free blacklist vector in trackerlogic.c*/ /* Free accesslist vector in trackerlogic.c*/
blacklist_reset(); accesslist_reset();
if( blacklist_filehandle == NULL ) { if( accesslist_filehandle == NULL ) {
fprintf( stderr, "Warning: Can't open blacklist file: %s (but will try to create it later, if necessary and possible).", blacklist_filename ); fprintf( stderr, "Warning: Can't open accesslist file: %s (but will try to create it later, if necessary and possible).", accesslist_filename );
signal( SIGHUP, read_blacklist_file );
return; return;
} }
/* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" */ /* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" */
while( fgets( static_inbuf, sizeof(static_inbuf), blacklist_filehandle ) ) { while( fgets( static_inbuf, sizeof(static_inbuf), accesslist_filehandle ) ) {
int i; int i;
for( i=0; i<20; ++i ) { for( i=0; i<20; ++i ) {
int eger = 16 * scan_fromhex( static_inbuf[ 2*i ] ) + scan_fromhex( static_inbuf[ 1 + 2*i ] ); int eger = 16 * scan_fromhex( static_inbuf[ 2*i ] ) + scan_fromhex( static_inbuf[ 1 + 2*i ] );
@ -787,15 +798,14 @@ void read_blacklist_file( int foo ) {
if( scan_fromhex( static_inbuf[ 40 ] ) >= 0 ) if( scan_fromhex( static_inbuf[ 40 ] ) >= 0 )
goto ignore_line; goto ignore_line;
/* Append blacklist to blacklist vector */ /* Append accesslist to accesslist vector */
blacklist_addentry( &infohash ); accesslist_addentry( &infohash );
ignore_line: ignore_line:
continue; continue;
} }
fclose( blacklist_filehandle ); fclose( accesslist_filehandle );
signal( SIGHUP, read_blacklist_file );
} }
#endif #endif
@ -806,12 +816,20 @@ int main( int argc, char **argv ) {
int scanon = 1; int scanon = 1;
while( scanon ) { while( scanon ) {
switch( getopt( argc, argv, ":i:p:A:P:d:b:h" ) ) { switch( getopt( argc, argv, ":i:p:A:P:d:"
#ifdef WANT_BLACKLISTING
"b:"
#elif defined( WANT_CLOSED_TRACKER )
"w:"
#endif
"h" ) ) {
case -1 : scanon = 0; break; case -1 : scanon = 0; break;
case 'i': scan_ip4( optarg, serverip ); break; case 'i': scan_ip4( optarg, serverip ); break;
case 'A': scan_ip4( optarg, g_adminip ); break; case 'A': scan_ip4( optarg, g_adminip ); break;
#ifdef WANT_BLACKLISTING #ifdef WANT_BLACKLISTING
case 'b': blacklist_filename = optarg; break; case 'b': accesslist_filename = optarg; break;
#elif defined( WANT_CLOSED_TRACKER )
case 'w': accesslist_filename = optarg; break;
#endif #endif
case 'p': ot_try_bind( serverip, (uint16)atol( optarg ), 1 ); break; case 'p': ot_try_bind( serverip, (uint16)atol( optarg ), 1 ); break;
case 'P': ot_try_bind( serverip, (uint16)atol( optarg ), 0 ); break; case 'P': ot_try_bind( serverip, (uint16)atol( optarg ), 0 ); break;
@ -839,11 +857,11 @@ int main( int argc, char **argv ) {
} }
endpwent(); endpwent();
#ifdef WANT_BLACKLISTING #ifdef WANT_ACCESS_CONTROL
/* Passing "0" since read_blacklist_file also is SIGHUP handler */ /* Passing "0" since read_blacklist_file also is SIGHUP handler */
if( blacklist_filename ) { if( accesslist_filename ) {
read_blacklist_file( 0 ); read_accesslist_file( 0 );
signal( SIGHUP, read_blacklist_file ); signal( SIGHUP, read_accesslist_file );
} }
#endif #endif

@ -22,8 +22,9 @@
/* GLOBAL VARIABLES */ /* GLOBAL VARIABLES */
static ot_vector all_torrents[256]; static ot_vector all_torrents[256];
static ot_vector changeset; static ot_vector changeset;
#ifdef WANT_BLACKLISTING #if defined ( WANT_BLACKLISTING ) || defined( WANT_CLOSED_TRACKER )
static ot_vector blacklist; static ot_vector accesslist;
#define WANT_ACCESS_CONTROL
#endif #endif
size_t changeset_size = 0; size_t changeset_size = 0;
@ -159,9 +160,14 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer, int from_changese
ot_vector *torrents_list = &all_torrents[*hash[0]], *peer_pool; ot_vector *torrents_list = &all_torrents[*hash[0]], *peer_pool;
int base_pool = 0; int base_pool = 0;
#ifdef WANT_BLACKLISTING #ifdef WANT_ACCESS_CONTROL
binary_search( hash, blacklist.data, blacklist.size, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &exactmatch ); binary_search( hash, accesslist.data, accesslist.size, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &exactmatch );
if( exactmatch )
#ifdef WANT_CLOSED_TRACKER
exactmatch = !exactmatch;
#endif
if( !exactmatch )
return NULL; return NULL;
#endif #endif
@ -736,15 +742,15 @@ void deinit_logic( void ) {
changeset_size = 0; changeset_size = 0;
} }
#ifdef WANT_BLACKLISTING #ifdef WANT_ACCESS_CONTROL
void blacklist_reset( void ) { void accesslist_reset( void ) {
free( blacklist.data ); free( accesslist.data );
byte_zero( &blacklist, sizeof( blacklist ) ); byte_zero( &accesslist, sizeof( accesslist ) );
} }
int blacklist_addentry( ot_hash *infohash ) { int accesslist_addentry( ot_hash *infohash ) {
int em; int em;
void *insert = vector_find_or_insert( &blacklist, infohash, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &em ); void *insert = vector_find_or_insert( &accesslist, infohash, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &em );
if( !insert ) if( !insert )
return -1; return -1;

@ -98,9 +98,9 @@ size_t return_memstat_for_tracker( char **reply );
size_t return_changeset_for_tracker( char **reply ); size_t return_changeset_for_tracker( char **reply );
void clean_all_torrents( void ); void clean_all_torrents( void );
void remove_peer_from_torrent( ot_hash *hash, ot_peer *peer ); void remove_peer_from_torrent( ot_hash *hash, ot_peer *peer );
#ifdef WANT_BLACKLISTING #if defined ( WANT_BLACKLISTING ) || defined ( WANT_CLOSED_TRACKER )
int blacklist_addentry( ot_hash *hash ); int accesslist_addentry( ot_hash *hash );
void blacklist_reset( void ); void accesslist_reset( void );
#endif #endif
#endif #endif

Loading…
Cancel
Save