Browse Source

Moving accesslist stuff to its own object

master
Dirk Engling 14 years ago
parent
commit
f03b2740dd
  1. 11
      Makefile
  2. 60
      opentracker.c
  3. 95
      ot_accesslist.c
  4. 22
      ot_accesslist.h
  5. 38
      trackerlogic.c
  6. 5
      trackerlogic.h

11
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)

60
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 );

95
ot_accesslist.c

@ -0,0 +1,95 @@
/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
It is considered beerware. Prost. Skol. Cheers or whatever. */
/* System */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/* 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

22
ot_accesslist.h

@ -0,0 +1,22 @@
/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
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

38
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( );

5
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 );

Loading…
Cancel
Save