diff options
Diffstat (limited to 'ot_accesslist.c')
-rw-r--r-- | ot_accesslist.c | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/ot_accesslist.c b/ot_accesslist.c index 4eb63a7..e57f09e 100644 --- a/ot_accesslist.c +++ b/ot_accesslist.c | |||
@@ -34,9 +34,9 @@ void accesslist_deinit( void ) { | |||
34 | accesslist_reset( ); | 34 | accesslist_reset( ); |
35 | } | 35 | } |
36 | 36 | ||
37 | static int accesslist_addentry( ot_hash infohash ) { | 37 | static int accesslist_addentry( ot_vector *al, ot_hash infohash ) { |
38 | int eger; | 38 | int eger; |
39 | void *insert = vector_find_or_insert( &accesslist, infohash, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &eger ); | 39 | void *insert = vector_find_or_insert( al, infohash, OT_HASH_COMPARE_SIZE, OT_HASH_COMPARE_SIZE, &eger ); |
40 | 40 | ||
41 | if( !insert ) | 41 | if( !insert ) |
42 | return -1; | 42 | return -1; |
@@ -48,22 +48,24 @@ static int accesslist_addentry( ot_hash infohash ) { | |||
48 | 48 | ||
49 | /* Read initial access list */ | 49 | /* Read initial access list */ |
50 | static void accesslist_readfile( int sig ) { | 50 | static void accesslist_readfile( int sig ) { |
51 | FILE * accesslist_filehandle; | 51 | FILE * accesslist_filehandle; |
52 | ot_hash infohash; | 52 | ot_hash infohash; |
53 | char inbuf[512]; | 53 | ot_vector accesslist_tmp; |
54 | void *olddata = accesslist.data; | ||
55 | char inbuf[512]; | ||
54 | 56 | ||
55 | if( sig != SIGHUP ) return; | 57 | if( sig != SIGHUP ) return; |
56 | 58 | ||
57 | accesslist_filehandle = fopen( g_accesslist_filename, "r" ); | 59 | accesslist_filehandle = fopen( g_accesslist_filename, "r" ); |
58 | 60 | ||
59 | /* Free accesslist vector in trackerlogic.c*/ | ||
60 | accesslist_reset(); | ||
61 | |||
62 | if( accesslist_filehandle == NULL ) { | 61 | if( accesslist_filehandle == NULL ) { |
63 | fprintf( stderr, "Warning: Can't open accesslist file: %s (but will try to create it later, if necessary and possible).", g_accesslist_filename ); | 62 | fprintf( stderr, "Warning: Can't open accesslist file: %s (but will try to create it later, if necessary and possible).", g_accesslist_filename ); |
64 | return; | 63 | return; |
65 | } | 64 | } |
66 | 65 | ||
66 | /* Initialise an empty accesslist vector */ | ||
67 | memset( &accesslist_tmp, 0, sizeof(accesslist_tmp)); | ||
68 | |||
67 | /* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" */ | 69 | /* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" */ |
68 | while( fgets( inbuf, sizeof(inbuf), accesslist_filehandle ) ) { | 70 | while( fgets( inbuf, sizeof(inbuf), accesslist_filehandle ) ) { |
69 | int i; | 71 | int i; |
@@ -77,10 +79,18 @@ static void accesslist_readfile( int sig ) { | |||
77 | continue; | 79 | continue; |
78 | 80 | ||
79 | /* Append accesslist to accesslist vector */ | 81 | /* Append accesslist to accesslist vector */ |
80 | accesslist_addentry( infohash ); | 82 | accesslist_addentry( &accesslist_tmp, infohash ); |
81 | } | 83 | } |
84 | #ifdef _DEBUG | ||
85 | fprintf( stderr, "Added %zd info_hashes to accesslist\n", accesslist_tmp.size ); | ||
86 | #endif | ||
82 | 87 | ||
83 | fclose( accesslist_filehandle ); | 88 | fclose( accesslist_filehandle ); |
89 | |||
90 | /* Now exchange the accesslist vector in the least race condition prone way */ | ||
91 | accesslist.size = 0; | ||
92 | memcpy( &accesslist, &accesslist_tmp, sizeof( &accesslist_tmp )); | ||
93 | free( olddata ); | ||
84 | } | 94 | } |
85 | 95 | ||
86 | int accesslist_hashisvalid( ot_hash hash ) { | 96 | int accesslist_hashisvalid( ot_hash hash ) { |