summaryrefslogtreecommitdiff
path: root/trackerlogic.c
diff options
context:
space:
mode:
authorerdgeist <>2007-01-05 12:25:44 +0000
committererdgeist <>2007-01-05 12:25:44 +0000
commit2aa03492a51e637931981f413a4da28abc75da97 (patch)
tree583782dfd59c2a95eaa2dd65c4e74437c7210a79 /trackerlogic.c
parentcdf2fbabedcd3b9e2423cce74225744db1acc178 (diff)
added closed tracker and blacklist code
Diffstat (limited to 'trackerlogic.c')
-rw-r--r--trackerlogic.c51
1 files changed, 49 insertions, 2 deletions
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//
23static ot_vector all_torrents[256]; 27static ot_vector all_torrents[256];
24 28
29#ifdef WANT_CLOSED_TRACKER
30int g_closedtracker = 1;
31static ot_torrent* const OT_TORRENT_NOT_ON_WHITELIST = (ot_torrent*)1;
32#endif
33
34#ifdef WANT_BLACKLIST
35static 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//
51char 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;} 64char 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
53static void *vector_find_or_insert( ot_vector *vector, void *key, size_t member_size, int compare_size, int *exactmatch ) { 66static 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
279int init_logic( ) { 321int 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