summaryrefslogtreecommitdiff
path: root/ot_accesslist.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_accesslist.c')
-rw-r--r--ot_accesslist.c95
1 files changed, 95 insertions, 0 deletions
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