summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerdgeist <>2007-08-18 09:56:22 +0000
committererdgeist <>2007-08-18 09:56:22 +0000
commit3e47339b6ca4c54c9140fa3e28729d1a7b7d6aef (patch)
treee932798c882ba45b55208b78c8f60679ee63d7db
parent992058383ad316b678a0def5baea0fd8a4058389 (diff)
Added whitelisting to reimplement the WANT_CLOSED_TRACKER functionality
-rw-r--r--opentracker.c68
-rw-r--r--trackerlogic.c28
-rw-r--r--trackerlogic.h6
3 files changed, 63 insertions, 39 deletions
diff --git a/opentracker.c b/opentracker.c
index f39dbdb..4ffde37 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -38,8 +38,13 @@ static time_t ot_start_time;
38static const size_t SUCCESS_HTTP_HEADER_LENGTH = 80; 38static const size_t SUCCESS_HTTP_HEADER_LENGTH = 80;
39static const size_t SUCCESS_HTTP_SIZE_OFF = 17; 39static const size_t SUCCESS_HTTP_SIZE_OFF = 17;
40static char g_adminip[4] = {0,0,0,0}; 40static char g_adminip[4] = {0,0,0,0};
41#ifdef WANT_BLACKLISTING 41
42static char *blacklist_filename = NULL; 42#if defined ( WANT_BLACKLISTING ) && defined (WANT_CLOSED_TRACKER )
43 #error WANT_BLACKLISTING and WANT_CLOSED_TRACKER are exclusive.
44#endif
45#if defined ( WANT_BLACKLISTING ) || defined (WANT_CLOSED_TRACKER )
46static char *accesslist_filename = NULL;
47#define WANT_ACCESS_CONTROL
43#endif 48#endif
44 49
45/* To always have space for error messages ;) */ 50/* To always have space for error messages ;) */
@@ -493,7 +498,13 @@ static void graceful( int s ) {
493} 498}
494 499
495static void usage( char *name ) { 500static void usage( char *name ) {
496 fprintf( stderr, "Usage: %s [-i ip] [-p port] [-P port] [-d dir] [-A ip]\n", name ); 501 fprintf( stderr, "Usage: %s [-i ip] [-p port] [-P port] [-d dir] [-A ip]"
502#ifdef WANT_BLACKLISTING
503 " [-b blacklistfile]"
504#elif defined ( WANT_CLOSED_TRACKER )
505 " [-w whitelistfile]"
506#endif
507 "\n", name );
497} 508}
498 509
499#define HELPLINE(opt,desc) fprintf(stderr, "\t%-10s%s\n",opt,desc) 510#define HELPLINE(opt,desc) fprintf(stderr, "\t%-10s%s\n",opt,desc)
@@ -507,6 +518,8 @@ static void help( char *name ) {
507 HELPLINE("-A ip","bless an ip address as admin address (e.g. to allow syncs from this address)"); 518 HELPLINE("-A ip","bless an ip address as admin address (e.g. to allow syncs from this address)");
508#ifdef WANT_BLACKLISTING 519#ifdef WANT_BLACKLISTING
509 HELPLINE("-b file","specify blacklist file."); 520 HELPLINE("-b file","specify blacklist file.");
521#elif defined( WANT_CLOSED_TRACKER )
522 HELPLINE("-w file","specify whitelist file.");
510#endif 523#endif
511 524
512 fprintf( stderr, "\nExample: ./opentracker -i 127.0.0.1 -p 6969 -P 6969 -i 10.1.1.23 -p 2710 -p 80\n" ); 525 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 ) {
756 ++ot_sockets_count; 769 ++ot_sockets_count;
757} 770}
758 771
759#ifdef WANT_BLACKLISTING 772#ifdef WANT_ACCESS_CONTROL
760/* Read initial black list */ 773/* Read initial access list */
761void read_blacklist_file( int foo ) { 774void read_accesslist_file( int foo ) {
762 FILE * blacklist_filehandle; 775 FILE * accesslist_filehandle;
763 ot_hash infohash; 776 ot_hash infohash;
764 foo = foo; 777 foo = foo;
765 778
766 signal( SIGHUP, SIG_IGN ); 779 accesslist_filehandle = fopen( accesslist_filename, "r" );
767 blacklist_filehandle = fopen( blacklist_filename, "r" );
768 780
769 /* Free blacklist vector in trackerlogic.c*/ 781 /* Free accesslist vector in trackerlogic.c*/
770 blacklist_reset(); 782 accesslist_reset();
771 783
772 if( blacklist_filehandle == NULL ) { 784 if( accesslist_filehandle == NULL ) {
773 fprintf( stderr, "Warning: Can't open blacklist file: %s (but will try to create it later, if necessary and possible).", blacklist_filename ); 785 fprintf( stderr, "Warning: Can't open accesslist file: %s (but will try to create it later, if necessary and possible).", accesslist_filename );
774 signal( SIGHUP, read_blacklist_file );
775 return; 786 return;
776 } 787 }
777 788
778 /* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" */ 789 /* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" */
779 while( fgets( static_inbuf, sizeof(static_inbuf), blacklist_filehandle ) ) { 790 while( fgets( static_inbuf, sizeof(static_inbuf), accesslist_filehandle ) ) {
780 int i; 791 int i;
781 for( i=0; i<20; ++i ) { 792 for( i=0; i<20; ++i ) {
782 int eger = 16 * scan_fromhex( static_inbuf[ 2*i ] ) + scan_fromhex( static_inbuf[ 1 + 2*i ] ); 793 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 ) {
787 if( scan_fromhex( static_inbuf[ 40 ] ) >= 0 ) 798 if( scan_fromhex( static_inbuf[ 40 ] ) >= 0 )
788 goto ignore_line; 799 goto ignore_line;
789 800
790 /* Append blacklist to blacklist vector */ 801 /* Append accesslist to accesslist vector */
791 blacklist_addentry( &infohash ); 802 accesslist_addentry( &infohash );
792 803
793ignore_line: 804ignore_line:
794 continue; 805 continue;
795 } 806 }
796 807
797 fclose( blacklist_filehandle ); 808 fclose( accesslist_filehandle );
798 signal( SIGHUP, read_blacklist_file );
799} 809}
800#endif 810#endif
801 811
@@ -806,12 +816,20 @@ int main( int argc, char **argv ) {
806 int scanon = 1; 816 int scanon = 1;
807 817
808 while( scanon ) { 818 while( scanon ) {
809 switch( getopt( argc, argv, ":i:p:A:P:d:b:h" ) ) { 819 switch( getopt( argc, argv, ":i:p:A:P:d:"
820#ifdef WANT_BLACKLISTING
821"b:"
822#elif defined( WANT_CLOSED_TRACKER )
823"w:"
824#endif
825 "h" ) ) {
810 case -1 : scanon = 0; break; 826 case -1 : scanon = 0; break;
811 case 'i': scan_ip4( optarg, serverip ); break; 827 case 'i': scan_ip4( optarg, serverip ); break;
812 case 'A': scan_ip4( optarg, g_adminip ); break; 828 case 'A': scan_ip4( optarg, g_adminip ); break;
813#ifdef WANT_BLACKLISTING 829#ifdef WANT_BLACKLISTING
814 case 'b': blacklist_filename = optarg; break; 830 case 'b': accesslist_filename = optarg; break;
831#elif defined( WANT_CLOSED_TRACKER )
832 case 'w': accesslist_filename = optarg; break;
815#endif 833#endif
816 case 'p': ot_try_bind( serverip, (uint16)atol( optarg ), 1 ); break; 834 case 'p': ot_try_bind( serverip, (uint16)atol( optarg ), 1 ); break;
817 case 'P': ot_try_bind( serverip, (uint16)atol( optarg ), 0 ); break; 835 case 'P': ot_try_bind( serverip, (uint16)atol( optarg ), 0 ); break;
@@ -839,11 +857,11 @@ int main( int argc, char **argv ) {
839 } 857 }
840 endpwent(); 858 endpwent();
841 859
842#ifdef WANT_BLACKLISTING 860#ifdef WANT_ACCESS_CONTROL
843 /* Passing "0" since read_blacklist_file also is SIGHUP handler */ 861 /* Passing "0" since read_blacklist_file also is SIGHUP handler */
844 if( blacklist_filename ) { 862 if( accesslist_filename ) {
845 read_blacklist_file( 0 ); 863 read_accesslist_file( 0 );
846 signal( SIGHUP, read_blacklist_file ); 864 signal( SIGHUP, read_accesslist_file );
847 } 865 }
848#endif 866#endif
849 867
diff --git a/trackerlogic.c b/trackerlogic.c
index 8308205..184edae 100644
--- a/trackerlogic.c
+++ b/trackerlogic.c
@@ -22,8 +22,9 @@
22/* GLOBAL VARIABLES */ 22/* GLOBAL VARIABLES */
23static ot_vector all_torrents[256]; 23static ot_vector all_torrents[256];
24static ot_vector changeset; 24static ot_vector changeset;
25#ifdef WANT_BLACKLISTING 25#if defined ( WANT_BLACKLISTING ) || defined( WANT_CLOSED_TRACKER )
26static ot_vector blacklist; 26static ot_vector accesslist;
27#define WANT_ACCESS_CONTROL
27#endif 28#endif
28 29
29size_t changeset_size = 0; 30size_t changeset_size = 0;
@@ -159,9 +160,14 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer, int from_changese
159 ot_vector *torrents_list = &all_torrents[*hash[0]], *peer_pool; 160 ot_vector *torrents_list = &all_torrents[*hash[0]], *peer_pool;
160 int base_pool = 0; 161 int base_pool = 0;
161 162
162#ifdef WANT_BLACKLISTING 163#ifdef WANT_ACCESS_CONTROL
163 binary_search( hash, blacklist.data, blacklist.size, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &exactmatch ); 164 binary_search( hash, accesslist.data, accesslist.size, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &exactmatch );
164 if( exactmatch ) 165
166#ifdef WANT_CLOSED_TRACKER
167 exactmatch = !exactmatch;
168#endif
169
170 if( !exactmatch )
165 return NULL; 171 return NULL;
166#endif 172#endif
167 173
@@ -736,15 +742,15 @@ void deinit_logic( void ) {
736 changeset_size = 0; 742 changeset_size = 0;
737} 743}
738 744
739#ifdef WANT_BLACKLISTING 745#ifdef WANT_ACCESS_CONTROL
740void blacklist_reset( void ) { 746void accesslist_reset( void ) {
741 free( blacklist.data ); 747 free( accesslist.data );
742 byte_zero( &blacklist, sizeof( blacklist ) ); 748 byte_zero( &accesslist, sizeof( accesslist ) );
743} 749}
744 750
745int blacklist_addentry( ot_hash *infohash ) { 751int accesslist_addentry( ot_hash *infohash ) {
746 int em; 752 int em;
747 void *insert = vector_find_or_insert( &blacklist, infohash, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &em ); 753 void *insert = vector_find_or_insert( &accesslist, infohash, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &em );
748 754
749 if( !insert ) 755 if( !insert )
750 return -1; 756 return -1;
diff --git a/trackerlogic.h b/trackerlogic.h
index 3b2d883..8fcf66e 100644
--- a/trackerlogic.h
+++ b/trackerlogic.h
@@ -98,9 +98,9 @@ size_t return_memstat_for_tracker( char **reply );
98size_t return_changeset_for_tracker( char **reply ); 98size_t return_changeset_for_tracker( char **reply );
99void clean_all_torrents( void ); 99void clean_all_torrents( void );
100void remove_peer_from_torrent( ot_hash *hash, ot_peer *peer ); 100void remove_peer_from_torrent( ot_hash *hash, ot_peer *peer );
101#ifdef WANT_BLACKLISTING 101#if defined ( WANT_BLACKLISTING ) || defined ( WANT_CLOSED_TRACKER )
102int blacklist_addentry( ot_hash *hash ); 102int accesslist_addentry( ot_hash *hash );
103void blacklist_reset( void ); 103void accesslist_reset( void );
104#endif 104#endif
105 105
106#endif 106#endif