summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerdgeist <>2007-11-12 04:39:53 +0000
committererdgeist <>2007-11-12 04:39:53 +0000
commit697e239027b1490eaa51e1d2dfc1f93b9941e07b (patch)
tree3f5bcf8a629a2cc035b307a80fda355bb7481735
parent0547e2ade2cfb974526fdcd8b350db0aead7d802 (diff)
Moving accesslist stuff to its own object
-rw-r--r--Makefile11
-rw-r--r--opentracker.c60
-rw-r--r--ot_accesslist.c95
-rw-r--r--ot_accesslist.h22
-rw-r--r--trackerlogic.c38
-rw-r--r--trackerlogic.h5
6 files changed, 135 insertions, 96 deletions
diff --git a/Makefile b/Makefile
index 9422087..ec1b1ad 100644
--- a/Makefile
+++ b/Makefile
@@ -1,13 +1,18 @@
1CC?=gcc 1CC?=gcc
2FEATURES=#-DWANT_TRACKER_SYNC -DWANT_BLACKLISTING -DWANT_CLOSED_TRACKER -DWANT_UTORRENT1600_WORKAROUND #-DWANT_IP_FROM_QUERY_STRING -D_DEBUG_HTTPERROR 2#FEATURES =-DWANT_TRACKER_SYNC
3#FEATURES+=-DWANT_BLACKLISTING
4#FEATURES+=-DWANT_CLOSED_TRACKER
5#FEATURES+=-DWANT_UTORRENT1600_WORKAROUND
6#FEATURES+=-DWANT_IP_FROM_QUERY_STRING
7#FEATURES+=-D_DEBUG_HTTPERROR
3OPTS_debug=-g -ggdb #-pg # -fprofile-arcs -ftest-coverage 8OPTS_debug=-g -ggdb #-pg # -fprofile-arcs -ftest-coverage
4OPTS_production=-Os 9OPTS_production=-Os
5CFLAGS+=-I../libowfat -Wall -pipe -Wextra #-pedantic -ansi 10CFLAGS+=-I../libowfat -Wall -pipe -Wextra #-pedantic -ansi
6LDFLAGS+=-L../libowfat/ -lowfat 11LDFLAGS+=-L../libowfat/ -lowfat
7 12
8BINARY =opentracker 13BINARY =opentracker
9HEADERS=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 14HEADERS=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
10SOURCES=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 15SOURCES=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
11 16
12OBJECTS = $(SOURCES:%.c=%.o) 17OBJECTS = $(SOURCES:%.c=%.o)
13OBJECTS_debug = $(SOURCES:%.c=%.debug.o) 18OBJECTS_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 @@
38#include "ot_udp.h" 38#include "ot_udp.h"
39#include "ot_fullscrape.h" 39#include "ot_fullscrape.h"
40#include "ot_iovec.h" 40#include "ot_iovec.h"
41#include "ot_accesslist.h"
41 42
42/* Globals */ 43/* Globals */
43static const size_t SUCCESS_HTTP_HEADER_LENGTH = 80; 44static const size_t SUCCESS_HTTP_HEADER_LENGTH = 80;
@@ -47,14 +48,6 @@ static unsigned int g_adminip_count = 0;
47time_t ot_start_time; 48time_t ot_start_time;
48time_t g_now; 49time_t g_now;
49 50
50#if defined ( WANT_BLACKLISTING ) && defined (WANT_CLOSED_TRACKER )
51 #error WANT_BLACKLISTING and WANT_CLOSED_TRACKER are exclusive.
52#endif
53#if defined ( WANT_BLACKLISTING ) || defined (WANT_CLOSED_TRACKER )
54static char *accesslist_filename = NULL;
55#define WANT_ACCESS_CONTROL
56#endif
57
58#ifndef WANT_TRACKER_SYNC 51#ifndef WANT_TRACKER_SYNC
59#define add_peer_to_torrent(A,B,C) add_peer_to_torrent(A,B) 52#define add_peer_to_torrent(A,B,C) add_peer_to_torrent(A,B)
60#endif 53#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] );
331 } 324 }
332 325
333 if( mode == SYNC_OUT ) { 326 if( mode == SYNC_OUT ) {
327 char *reply;
334 if( !( reply_size = return_changeset_for_tracker( &reply ) ) ) HTTPERROR_500; 328 if( !( reply_size = return_changeset_for_tracker( &reply ) ) ) HTTPERROR_500;
335 return sendmmapdata( s, reply, reply_size ); 329 return sendmmapdata( s, reply, reply_size );
336 } 330 }
337 331
338 /* Simple but proof for now */ 332 /* Simple but proof for now */
339 reply = "OK"; 333 memmove( static_outbuf + SUCCESS_HTTP_HEADER_LENGTH, "OK", 2);
340 reply_size = 2; 334 reply_size = 2;
341 335
342 break; 336 break;
@@ -772,48 +766,14 @@ static void ot_try_bind( char ip[4], uint16 port, int is_tcp ) {
772 ++ot_sockets_count; 766 ++ot_sockets_count;
773} 767}
774 768
775#ifdef WANT_ACCESS_CONTROL
776/* Read initial access list */
777void read_accesslist_file( int foo ) {
778 FILE * accesslist_filehandle;
779 ot_hash infohash;
780 foo = foo;
781
782 accesslist_filehandle = fopen( accesslist_filename, "r" );
783
784 /* Free accesslist vector in trackerlogic.c*/
785 accesslist_reset();
786
787 if( accesslist_filehandle == NULL ) {
788 fprintf( stderr, "Warning: Can't open accesslist file: %s (but will try to create it later, if necessary and possible).", accesslist_filename );
789 return;
790 }
791
792 /* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" */
793 while( fgets( static_inbuf, sizeof(static_inbuf), accesslist_filehandle ) ) {
794 int i;
795 for( i=0; i<20; ++i ) {
796 int eger = 16 * scan_fromhex( static_inbuf[ 2*i ] ) + scan_fromhex( static_inbuf[ 1 + 2*i ] );
797 if( eger < 0 )
798 continue;
799 infohash[i] = eger;
800 }
801 if( scan_fromhex( static_inbuf[ 40 ] ) >= 0 )
802 continue;
803
804 /* Append accesslist to accesslist vector */
805 accesslist_addentry( &infohash );
806 }
807
808 fclose( accesslist_filehandle );
809}
810#endif
811
812int main( int argc, char **argv ) { 769int main( int argc, char **argv ) {
813 struct passwd *pws = NULL; 770 struct passwd *pws = NULL;
814 char serverip[4] = {0,0,0,0}; 771 char serverip[4] = {0,0,0,0};
815 char *serverdir = "."; 772 char *serverdir = ".";
816 int scanon = 1; 773 int scanon = 1;
774#ifdef WANT_ACCESS_CONTROL
775 char *accesslist_filename = NULL;
776#endif
817 777
818 while( scanon ) { 778 while( scanon ) {
819 switch( getopt( argc, argv, ":i:p:A:P:d:" 779 switch( getopt( argc, argv, ":i:p:A:P:d:"
@@ -863,13 +823,7 @@ int main( int argc, char **argv ) {
863 } 823 }
864 endpwent(); 824 endpwent();
865 825
866#ifdef WANT_ACCESS_CONTROL 826 accesslist_init( accesslist_filename );
867 /* Passing "0" since read_blacklist_file also is SIGHUP handler */
868 if( accesslist_filename ) {
869 read_accesslist_file( 0 );
870 signal( SIGHUP, read_accesslist_file );
871 }
872#endif
873 827
874 signal( SIGPIPE, SIG_IGN ); 828 signal( SIGPIPE, SIG_IGN );
875 signal( SIGINT, signal_handler ); 829 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 @@
1/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2 It is considered beerware. Prost. Skol. Cheers or whatever. */
3
4/* System */
5#include <stdlib.h>
6#include <string.h>
7#include <stdio.h>
8
9/* Libowfat */
10#include "byte.h"
11#include "scan.h"
12
13/* Opentracker */
14#include "ot_accesslist.h"
15
16/* GLOBAL VARIABLES */
17#ifdef WANT_ACCESS_CONTROL
18static char *accesslist_filename = NULL;
19static ot_vector accesslist;
20
21static void accesslist_reset( void ) {
22 free( accesslist.data );
23 byte_zero( &accesslist, sizeof( accesslist ) );
24}
25
26static int accesslist_addentry( ot_hash *infohash ) {
27 int em;
28 void *insert = vector_find_or_insert( &accesslist, infohash, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &em );
29
30 if( !insert )
31 return -1;
32
33 memmove( insert, infohash, OT_HASH_COMPARE_SIZE );
34
35 return 0;
36}
37
38/* Read initial access list */
39static void accesslist_readfile( int foo ) {
40 FILE * accesslist_filehandle;
41 ot_hash infohash;
42 foo = foo;
43
44 accesslist_filehandle = fopen( accesslist_filename, "r" );
45
46 /* Free accesslist vector in trackerlogic.c*/
47 accesslist_reset();
48
49 if( accesslist_filehandle == NULL ) {
50 fprintf( stderr, "Warning: Can't open accesslist file: %s (but will try to create it later, if necessary and possible).", accesslist_filename );
51 return;
52 }
53
54 /* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" */
55 while( fgets( static_inbuf, sizeof(static_inbuf), accesslist_filehandle ) ) {
56 int i;
57 for( i=0; i<20; ++i ) {
58 int eger = 16 * scan_fromhex( static_inbuf[ 2*i ] ) + scan_fromhex( static_inbuf[ 1 + 2*i ] );
59 if( eger < 0 )
60 continue;
61 infohash[i] = eger;
62 }
63 if( scan_fromhex( static_inbuf[ 40 ] ) >= 0 )
64 continue;
65
66 /* Append accesslist to accesslist vector */
67 accesslist_addentry( &infohash );
68 }
69
70 fclose( accesslist_filehandle );
71}
72
73int accesslist_hashisvalid( ot_hash *hash ) {
74 int exactmatch;
75 binary_search( hash, accesslist.data, accesslist.size, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &exactmatch );
76
77#ifdef WANT_BLACKLISTING
78 exactmatch = !exactmatch;
79#endif
80
81 return exactmatch;
82}
83
84void accesslist_init( char *accesslist_filename_in ) {
85 byte_zero( &accesslist, sizeof( accesslist ) );
86
87 /* Passing "0" since read_blacklist_file also is SIGHUP handler */
88 if( accesslist_filename_in ) {
89 accesslist_filename = accesslist_filename_in;
90 accesslist_readfile( 0 );
91 signal( SIGHUP, accesslist_readfile );
92 }
93}
94
95#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 @@
1/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2 It is considered beerware. Prost. Skol. Cheers or whatever. */
3
4#ifndef __OT_ACCESSLIST_H__
5#define __OT_ACCESSLIST_H__
6
7#include "trackerlogic.h"
8
9#if defined ( WANT_BLACKLISTING ) && defined (WANT_CLOSED_TRACKER )
10 #error WANT_BLACKLISTING and WANT_CLOSED_TRACKER are exclusive.
11#endif
12
13#if defined ( WANT_BLACKLISTING ) || defined (WANT_CLOSED_TRACKER )
14#define WANT_ACCESS_CONTROL
15void accesslist_init( char *accesslist_filename );
16int accesslist_hashisvalid( ot_hash *hash );
17#else
18#define accesslist_init( accesslist_filename )
19#define accesslist_hashisvalid( hash ) 1
20#endif
21
22#endif
diff --git a/trackerlogic.c b/trackerlogic.c
index 7f28903..d0d5235 100644
--- a/trackerlogic.c
+++ b/trackerlogic.c
@@ -21,12 +21,7 @@
21#include "ot_mutex.h" 21#include "ot_mutex.h"
22#include "ot_stats.h" 22#include "ot_stats.h"
23#include "ot_clean.h" 23#include "ot_clean.h"
24 24#include "ot_accesslist.h"
25/* GLOBAL VARIABLES */
26#if defined ( WANT_BLACKLISTING ) || defined( WANT_CLOSED_TRACKER )
27static ot_vector accesslist;
28#define WANT_ACCESS_CONTROL
29#endif
30 25
31void free_peerlist( ot_peerlist *peer_list ) { 26void free_peerlist( ot_peerlist *peer_list ) {
32 size_t i; 27 size_t i;
@@ -46,18 +41,10 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer WANT_TRACKER_SYNC
46 ot_vector *torrents_list = mutex_bucket_lock_by_hash( hash ), *peer_pool; 41 ot_vector *torrents_list = mutex_bucket_lock_by_hash( hash ), *peer_pool;
47 int base_pool = 0; 42 int base_pool = 0;
48 43
49#ifdef WANT_ACCESS_CONTROL 44 if( !accesslist_hashisvalid( hash ) ) {
50 binary_search( hash, accesslist.data, accesslist.size, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &exactmatch );
51
52#ifdef WANT_CLOSED_TRACKER
53 exactmatch = !exactmatch;
54#endif
55
56 if( exactmatch ) {
57 mutex_bucket_unlock_by_hash( hash ); 45 mutex_bucket_unlock_by_hash( hash );
58 return NULL; 46 return NULL;
59 } 47 }
60#endif
61 48
62 torrent = vector_find_or_insert( torrents_list, (void*)hash, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch ); 49 torrent = vector_find_or_insert( torrents_list, (void*)hash, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch );
63 if( !torrent ) { 50 if( !torrent ) {
@@ -326,25 +313,6 @@ exit_loop:
326 return (size_t)20; 313 return (size_t)20;
327} 314}
328 315
329#ifdef WANT_ACCESS_CONTROL
330void accesslist_reset( void ) {
331 free( accesslist.data );
332 byte_zero( &accesslist, sizeof( accesslist ) );
333}
334
335int accesslist_addentry( ot_hash *infohash ) {
336 int em;
337 void *insert = vector_find_or_insert( &accesslist, infohash, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &em );
338
339 if( !insert )
340 return -1;
341
342 memmove( insert, infohash, OT_HASH_COMPARE_SIZE );
343
344 return 0;
345}
346#endif
347
348int trackerlogic_init( const char * const serverdir ) { 316int trackerlogic_init( const char * const serverdir ) {
349 if( serverdir && chdir( serverdir ) ) { 317 if( serverdir && chdir( serverdir ) ) {
350 fprintf( stderr, "Could not chdir() to %s\n", serverdir ); 318 fprintf( stderr, "Could not chdir() to %s\n", serverdir );
@@ -352,7 +320,7 @@ int trackerlogic_init( const char * const serverdir ) {
352 } 320 }
353 321
354 srandom( time(NULL) ); 322 srandom( time(NULL) );
355 323
356 clean_init( ); 324 clean_init( );
357 mutex_init( ); 325 mutex_init( );
358 326
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 );
107size_t return_udp_scrape_for_torrent( ot_hash *hash, char *reply ); 107size_t return_udp_scrape_for_torrent( ot_hash *hash, char *reply );
108void clean_all_torrents( void ); 108void clean_all_torrents( void );
109 109
110#if defined ( WANT_BLACKLISTING ) || defined ( WANT_CLOSED_TRACKER )
111int accesslist_addentry( ot_hash *hash );
112void accesslist_reset( void );
113#endif
114
115/* Helper, before it moves to its own object */ 110/* Helper, before it moves to its own object */
116void fix_mmapallocation( void *buf, size_t old_alloc, size_t new_alloc ); 111void fix_mmapallocation( void *buf, size_t old_alloc, size_t new_alloc );
117void free_peerlist( ot_peerlist *peer_list ); 112void free_peerlist( ot_peerlist *peer_list );