diff options
author | erdgeist <> | 2006-12-12 02:37:19 +0000 |
---|---|---|
committer | erdgeist <> | 2006-12-12 02:37:19 +0000 |
commit | 31c20c515d96c7311aa3483287202a48dd39cefb (patch) | |
tree | 949d07b127bfb9b498989d1d839652d08a9c5fd6 /trackerlogic.c | |
parent | 0de82a8925e38e39f01a732ee22b27607b820d0b (diff) |
This is a complete rewrite... assume nothing works.
Diffstat (limited to 'trackerlogic.c')
-rw-r--r-- | trackerlogic.c | 356 |
1 files changed, 146 insertions, 210 deletions
diff --git a/trackerlogic.c b/trackerlogic.c index 03e7bd8..c57d1a9 100644 --- a/trackerlogic.c +++ b/trackerlogic.c | |||
@@ -8,12 +8,19 @@ | |||
8 | #include <sys/mman.h> | 8 | #include <sys/mman.h> |
9 | #include <unistd.h> | 9 | #include <unistd.h> |
10 | #include <time.h> | 10 | #include <time.h> |
11 | #include <math.h> | ||
11 | #include <glob.h> | 12 | #include <glob.h> |
12 | 13 | ||
14 | #include <errno.h> | ||
15 | #include "scan.h" | ||
16 | #include "byte.h" | ||
17 | |||
13 | // Helper functions for binary_find | 18 | // Helper functions for binary_find |
14 | // | 19 | // |
15 | int compare_hash( const void *hash1, const void *hash2 ) { return memcmp( hash1, hash2, sizeof( ot_hash )); } | 20 | int compare_hash( const void *hash1, const void *hash2 ) { return memcmp( hash1, hash2, sizeof( ot_hash )); } |
16 | int compare_ip_port( const void *peer1, const void *peer2 ) { return memcmp( &((ot_peer)peer1)->ip, &((ot_peer)peer2)->ip, 6); } | 21 | int compare_ip_port( const void *peer1, const void *peer2 ) { |
22 | if( ((ot_peer)peer1)->ip != ((ot_peer)peer2)->ip ) return ((ot_peer)peer1)->ip - ((ot_peer)peer2)->ip; | ||
23 | return ((ot_peer)peer1)->port_flags - ((ot_peer)peer2)->port_flags; } | ||
17 | 24 | ||
18 | void *binary_search( const void *key, const void *base, | 25 | void *binary_search( const void *key, const void *base, |
19 | unsigned long member_count, const unsigned long member_size, | 26 | unsigned long member_count, const unsigned long member_size, |
@@ -40,235 +47,169 @@ void *binary_search( const void *key, const void *base, | |||
40 | // Converter function from memory to human readable hex strings | 47 | // Converter function from memory to human readable hex strings |
41 | // * definitely not thread safe!!! | 48 | // * definitely not thread safe!!! |
42 | // | 49 | // |
43 | char 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;} | 50 | char 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;} |
44 | 51 | ||
45 | // GLOBAL VARIABLES | 52 | // GLOBAL VARIABLES |
46 | // | 53 | // |
47 | unsigned long torrents_count = 0; | 54 | struct ot_vector all_torrents[256]; |
48 | ot_torrent torrents_list = 0; | 55 | |
49 | ot_byte *scratch_space = 0; | 56 | void *vector_find_or_insert( ot_vector vector, void *key, size_t member_size, int(*compare_func)(const void*, const void*), int *exactmatch ) { |
57 | ot_byte *end = ((ot_byte*)vector->data) + member_size * vector->size; | ||
58 | ot_byte *match = BINARY_FIND( key, vector->data, vector->size, member_size, compare_func, exactmatch ); | ||
59 | |||
60 | if( exactmatch ) return match; | ||
61 | |||
62 | if( vector->size + 1 >= vector->space ) { | ||
63 | void *new_data = realloc( vector->data, vector->space ? 2 * vector->space : 1024 ); | ||
64 | if( !new_data ) return NULL; | ||
65 | vector->data = new_data; | ||
66 | vector->space = vector->space ? vector->space * 2 : 1024; | ||
67 | } | ||
68 | MEMMOVE( match + member_size, match, end - match ); | ||
69 | vector->size++; | ||
70 | return match; | ||
71 | } | ||
72 | |||
73 | int vector_remove_peer( ot_vector vector, ot_peer peer ) { | ||
74 | int exactmatch; | ||
75 | ot_peer end = ((ot_peer)vector->data) + vector->size; | ||
76 | ot_peer match; | ||
77 | |||
78 | if( !vector->size ) return 0; | ||
79 | match = BINARY_FIND( peer, vector->data, vector->size, sizeof( struct ot_peer ), compare_ip_port, &exactmatch ); | ||
80 | |||
81 | if( !exactmatch ) return 0; | ||
82 | exactmatch = match->port_flags & PEER_FLAG_SEEDING ? 2 : 1; | ||
83 | MEMMOVE( match, match + 1, end - match - 1 ); | ||
84 | vector->size--; | ||
85 | return exactmatch; | ||
86 | } | ||
87 | |||
88 | void free_peerlist( ot_peerlist peer_list ) { | ||
89 | int i; | ||
90 | for( i=0; i<OT_POOLS_COUNT; ++i ) | ||
91 | if( peer_list->peers[i].data ) | ||
92 | free( peer_list->peers[i].data ); | ||
93 | free( peer_list ); | ||
94 | } | ||
95 | |||
96 | int vector_remove_torrent( ot_vector vector, ot_hash *hash ) { | ||
97 | int exactmatch; | ||
98 | ot_torrent end = ((ot_torrent)vector->data) + vector->size; | ||
99 | ot_torrent match = BINARY_FIND( hash, vector->data, vector->size, sizeof( struct ot_torrent ), compare_hash, &exactmatch ); | ||
100 | |||
101 | if( !exactmatch ) return -1; | ||
102 | free_peerlist( match->peer_list ); | ||
103 | MEMMOVE( match, match + 1, end - match - 1 ); | ||
104 | vector->size--; | ||
105 | if( ( 3*vector->size < vector->space ) && ( vector->space > 1024 ) ) { | ||
106 | realloc( vector->data, vector->space >> 1 ); | ||
107 | vector->space >>= 1; | ||
108 | } | ||
109 | return 0; | ||
110 | } | ||
50 | 111 | ||
51 | #define SETINVALID( i ) (scratch_space[index] = 3); | 112 | void clean_peerlist( ot_peerlist peer_list ) { |
52 | #define SETSELECTED( i ) (scratch_space[index] = 1); | 113 | exit( 1 ); |
53 | #define TESTSELECTED( i ) (scratch_space[index] == 1 ) | 114 | } |
54 | #define TESTSET( i ) (scratch_space[index]) | ||
55 | #define RANDOM random() | ||
56 | 115 | ||
57 | ot_torrent add_peer_to_torrent( ot_hash *hash, ot_peer peer ) { | 116 | ot_torrent add_peer_to_torrent( ot_hash *hash, ot_peer peer ) { |
117 | int exactmatch; | ||
58 | ot_torrent torrent; | 118 | ot_torrent torrent; |
59 | ot_peer peer_dest; | 119 | ot_peer peer_dest; |
60 | int exactmatch; | 120 | ot_vector torrents_list = all_torrents + *hash[0], peer_pool; |
61 | 121 | ||
62 | torrent = BINARY_FIND( hash, torrents_list, torrents_count, sizeof( *torrent ), compare_hash, &exactmatch ); | 122 | torrent = vector_find_or_insert( torrents_list, (void*)hash, sizeof( struct ot_torrent ), compare_hash, &exactmatch ); |
63 | if( !exactmatch ) { | 123 | if( !torrent ) return NULL; |
64 | // Assume, OS will provide us with space, after all, this is file backed | ||
65 | MEMMOVE( torrent + 1, torrent, ( torrents_list + torrents_count ) - torrent ); | ||
66 | 124 | ||
125 | if( !exactmatch ) { | ||
67 | // Create a new torrent entry, then | 126 | // Create a new torrent entry, then |
127 | torrent->peer_list = malloc( sizeof (struct ot_peerlist) ); | ||
128 | if( !torrent->peer_list ) { | ||
129 | vector_remove_torrent( torrents_list, hash ); | ||
130 | return NULL; | ||
131 | } | ||
68 | MEMMOVE( &torrent->hash, hash, sizeof( ot_hash ) ); | 132 | MEMMOVE( &torrent->hash, hash, sizeof( ot_hash ) ); |
69 | torrent->peer_list = map_file( to_hex( *hash ) ); | ||
70 | torrent->peer_count = 0; | ||
71 | torrent->seed_count = 0; | ||
72 | } | ||
73 | 133 | ||
74 | peer_dest = BINARY_FIND( peer, torrent->peer_list, torrent->peer_count, sizeof( *peer_dest ), compare_ip_port, &exactmatch ); | 134 | byte_zero( torrent->peer_list, sizeof( struct ot_peerlist )); |
75 | if( exactmatch ) { | 135 | torrent->peer_list->base = NOW; |
76 | // If peer was a seeder but isn't anymore, decrease seeder count | 136 | } else |
77 | if( ( peer_dest->flags & PEER_FLAG_SEEDING ) && !( peer->flags & PEER_FLAG_SEEDING ) ) | 137 | clean_peerlist( torrent->peer_list ); |
78 | torrent->seed_count--; | ||
79 | if( !( peer_dest->flags & PEER_FLAG_SEEDING ) && ( peer->flags & PEER_FLAG_SEEDING ) ) | ||
80 | torrent->seed_count++; | ||
81 | } else { | ||
82 | // Assume, OS will provide us with space, after all, this is file backed | ||
83 | MEMMOVE( peer_dest + 1, peer_dest, ( torrent->peer_list + torrent->peer_count ) - peer_dest ); | ||
84 | 138 | ||
85 | // Create a new peer entry, then | 139 | peer_pool = &torrent->peer_list->peers[0]; |
86 | MEMMOVE( peer_dest, peer, sizeof( ot_peer ) ); | 140 | peer_dest = vector_find_or_insert( peer_pool, (void*)peer, sizeof( struct ot_peer ), compare_ip_port, &exactmatch ); |
87 | 141 | ||
88 | torrent->peer_count++; | 142 | // If we hadn't had a match in current pool, create peer there and |
89 | torrent->seed_count+= ( peer->flags & PEER_FLAG_SEEDING ) ? 1 : 0; | 143 | // remove it from all older pools |
144 | if( !exactmatch ) { | ||
145 | int i; | ||
146 | MEMMOVE( peer_dest, peer, sizeof( struct ot_peer ) ); | ||
147 | if( peer->port_flags & PEER_FLAG_SEEDING ) | ||
148 | torrent->peer_list->seed_count[0]++; | ||
149 | for( i=1; i<OT_POOLS_COUNT; ++i ) { | ||
150 | switch( vector_remove_peer( &torrent->peer_list->peers[i], peer ) ) { | ||
151 | case 0: continue; | ||
152 | case 2: torrent->peer_list->seed_count[i]--; | ||
153 | case 1: default: return torrent; | ||
154 | } | ||
155 | } | ||
156 | } else { | ||
157 | if( (peer_dest->port_flags & PEER_FLAG_SEEDING ) && !(peer->port_flags & PEER_FLAG_SEEDING ) ) | ||
158 | torrent->peer_list->seed_count[0]--; | ||
159 | if( !(peer_dest->port_flags & PEER_FLAG_SEEDING ) && (peer->port_flags & PEER_FLAG_SEEDING ) ) | ||
160 | torrent->peer_list->seed_count[0]++; | ||
90 | } | 161 | } |
91 | 162 | ||
92 | // Set new time out time | ||
93 | peer_dest->death = NOW + OT_TIMEOUT; | ||
94 | |||
95 | return torrent; | 163 | return torrent; |
96 | } | 164 | } |
97 | 165 | ||
98 | inline int TESTVALIDPEER( ot_peer p ) { return p->death > NOW; } | ||
99 | |||
100 | // Compiles a list of random peers for a torrent | 166 | // Compiles a list of random peers for a torrent |
101 | // * scratch space keeps track of death or already selected peers | 167 | // * reply must have enough space to hold 24+6*amount bytes |
102 | // * reply must have enough space to hold 1+(1+16+2+1)*amount+1 bytes | ||
103 | // * Selector function can be anything, maybe test for seeds, etc. | 168 | // * Selector function can be anything, maybe test for seeds, etc. |
104 | // * that RANDOM may return huge values | 169 | // * RANDOM may return huge values |
105 | // * does not yet check not to return self | 170 | // * does not yet check not to return self |
106 | // * it is not guaranteed to see all peers, so no assumptions on active seeders/peers may be done | ||
107 | // * since compact format cannot handle v6 addresses, it must be enabled by OT_COMPACT_ONLY | ||
108 | // | 171 | // |
109 | size_t return_peers_for_torrent( ot_torrent torrent, unsigned long amount, char *reply ) { | 172 | size_t return_peers_for_torrent( ot_torrent torrent, unsigned long amount, char *reply ) { |
110 | register ot_peer peer_base = torrent->peer_list; | 173 | char *r = reply; |
111 | char *r = reply; | 174 | unsigned long peer_count, index; |
112 | unsigned long peer_count = torrent->peer_count; | 175 | unsigned long pool_offset = 0, pool_index = 0; |
113 | unsigned long selected_count = 0, invalid_count = 0; | 176 | signed long wert = -1; |
114 | unsigned long index = 0; | 177 | |
115 | 178 | for( peer_count=index=0; index<OT_POOLS_COUNT; ++index) peer_count += torrent->peer_list->peers[index].size; | |
116 | // optimize later ;) | 179 | if( peer_count < amount ) amount = peer_count; |
117 | BZERO( scratch_space, peer_count ); | 180 | |
118 | 181 | r += FORMAT_FORMAT_STRING( r, "d5:peers%li:",6*amount ); | |
119 | while( ( selected_count < amount ) && ( selected_count + invalid_count < peer_count ) ) { | 182 | for( index = 0; index < amount; ++index ) { |
120 | // skip to first non-flagged peer | 183 | double step = 1.8*((double)( peer_count - wert - 1 ))/((double)( amount - index )); |
121 | while( TESTSET(index) ) index = ( index + 1 ) % peer_count; | 184 | int off = random() % (int)floor( step ); |
122 | 185 | off = 1 + ( off % ( peer_count - wert - 1 )); | |
123 | if( TESTVALIDPEER( peer_base + index ) ) { | 186 | wert += off; |
124 | SETINVALID(index); invalid_count++; | 187 | |
125 | } else { | 188 | while( pool_offset + off > torrent->peer_list->peers[pool_index].size ) { |
126 | SETSELECTED(index); selected_count++; | 189 | off -= torrent->peer_list->peers[pool_index].size - pool_offset; |
127 | index = ( index + RANDOM ) % peer_count; | 190 | pool_offset = 0; pool_index++; |
128 | } | 191 | } |
129 | } | ||
130 | 192 | ||
131 | // Now our scratchspace contains a list of selected_count valid peers | 193 | MEMMOVE( r, ((ot_peer*)torrent->peer_list->peers[pool_index].data)[pool_offset], 6 ); |
132 | // Collect them into a reply string | ||
133 | index = 0; | ||
134 | |||
135 | #ifndef OT_COMPACT_ONLY | ||
136 | r += FORMAT_FIXED_STRING( r, "d5:peersl" ); | ||
137 | #else | ||
138 | r += FORMAT_FORMAT_STRING( r, "d5:peers%li:",6*selected_count ); | ||
139 | #endif | ||
140 | |||
141 | while( selected_count-- ) { | ||
142 | ot_peer peer; | ||
143 | while( !TESTSELECTED( index ) ) ++index; | ||
144 | peer = peer_base + index; | ||
145 | #ifdef OT_COMPACT_ONLY | ||
146 | MEMMOVE( r, &peer->ip, 4 ); | ||
147 | MEMMOVE( r+4, &peer->port, 2 ); | ||
148 | r += 6; | 194 | r += 6; |
149 | #else | ||
150 | r += FORMAT_FORMAT_STRING( r, "d2:ip%d:%s7:peer id20:%20c4:porti%ie", | ||
151 | peer->flags & PEER_IP_LENGTH_MASK, | ||
152 | peer->ip, | ||
153 | peer->id, | ||
154 | peer->port ); | ||
155 | #endif | ||
156 | } | 195 | } |
157 | #ifndef OT_COMPACT_ONLY | 196 | *r++ = 'e'; |
158 | r += FORMAT_FIXED_STRING( r, "ee" ); | ||
159 | #else | ||
160 | r += FORMAT_FIXED_STRING( r, "e" ); | ||
161 | #endif | ||
162 | return r - reply; | 197 | return r - reply; |
163 | } | 198 | } |
164 | 199 | ||
165 | // Compacts a torrents peer list | ||
166 | // * torrents older than OT_TIMEOUT are being kicked | ||
167 | // * is rather expensive | ||
168 | // * if this fails, torrent file is invalid, should add flag | ||
169 | // | ||
170 | void heal_torrent( ot_torrent torrent ) { | ||
171 | unsigned long index = 0, base = 0, end, seed_count = 0, now = NOW; | ||
172 | |||
173 | // Initialize base to first dead peer. | ||
174 | while( ( base < torrent->peer_count ) && torrent->peer_list[base].death <= now ) { | ||
175 | seed_count += ( torrent->peer_list[base].flags & PEER_FLAG_SEEDING ) ? 1 : 0; | ||
176 | base++; | ||
177 | } | ||
178 | |||
179 | // No dead peers? Home. | ||
180 | if( base == torrent->peer_count ) return; | ||
181 | |||
182 | // From now index always looks to the next living peer while base keeps track of | ||
183 | // the dead peer that marks the beginning of insert space. | ||
184 | index = base + 1; | ||
185 | |||
186 | while( 1 ) { | ||
187 | // Let index search for next living peer | ||
188 | while( ( index < torrent->peer_count ) && torrent->peer_list[index].death > now ) index++; | ||
189 | |||
190 | // No further living peers found - base is our new peer count | ||
191 | if( index == torrent->peer_count ) { | ||
192 | torrent->peer_count = base; | ||
193 | torrent->seed_count = seed_count; | ||
194 | return; | ||
195 | } | ||
196 | |||
197 | end = index + 1; | ||
198 | |||
199 | // Let end search for next dead peer (end of living peers) | ||
200 | while( ( end < torrent->peer_count ) && torrent->peer_list[end].death <= now ) { | ||
201 | seed_count += ( torrent->peer_list[end].flags & PEER_FLAG_SEEDING ) ? 1 : 0; | ||
202 | end++; | ||
203 | } | ||
204 | |||
205 | // We either hit a dead peer or the end of our peers | ||
206 | // In both cases: move block towards base | ||
207 | MEMMOVE( torrent->peer_list + base, torrent->peer_list + index, ( end - index ) * sizeof( struct ot_peer ) ); | ||
208 | base += end - index; | ||
209 | |||
210 | index = end; | ||
211 | } | ||
212 | } | ||
213 | |||
214 | void dispose_torrent( ot_torrent torrent ) { | ||
215 | unmap_file( NULL, torrent->peer_list, 0 ); | ||
216 | unlink( to_hex( torrent->hash ) ); | ||
217 | MEMMOVE( torrent, torrent + 1, ( torrents_list + torrents_count ) - ( torrent + 1 ) ); | ||
218 | torrents_count--; | ||
219 | } | ||
220 | |||
221 | // This function maps a "huge" file into process space | ||
222 | // * giving no name will aqcuire anonymous growable memory | ||
223 | // * memory will not be "freed" from systems vm if once used, until unmap_file | ||
224 | // * I guess, we should be checking for more errors... | ||
225 | // | ||
226 | void *map_file( char *file_name ) { | ||
227 | char *map; | ||
228 | if( file_name ) { | ||
229 | int file_desc=open(file_name,O_RDWR|O_CREAT|O_NDELAY,0644); | ||
230 | printf( "%s\n", file_name ); | ||
231 | if( file_desc < 0) return 0; | ||
232 | lseek( file_desc, OT_HUGE_FILESIZE, SEEK_SET ); | ||
233 | write( file_desc, "_", 1 ); | ||
234 | map=mmap(0,OT_HUGE_FILESIZE,PROT_READ|PROT_WRITE,MAP_SHARED,file_desc,0); | ||
235 | close(file_desc); | ||
236 | } else | ||
237 | map=mmap(0,OT_HUGE_FILESIZE,PROT_READ|PROT_WRITE,MAP_ANON|MAP_PRIVATE,-1,0); | ||
238 | |||
239 | return (map == (char*)-1) ? 0 : map; | ||
240 | } | ||
241 | |||
242 | void unmap_file( char *file_name, void *map, unsigned long real_size ) { | ||
243 | munmap( map, OT_HUGE_FILESIZE ); | ||
244 | if( file_name) | ||
245 | truncate( file_name, real_size ); | ||
246 | } | ||
247 | |||
248 | void count_peers_and_seeds( ot_peer peer_list, unsigned long *peers, unsigned long *seeds ) { | ||
249 | *peers = *seeds = 0; | ||
250 | if( peer_list[*peers].ip ) | ||
251 | do { | ||
252 | *seeds += peer_list[*peers++].flags & PEER_FLAG_SEEDING ? 1 : 0; | ||
253 | } while( compare_ip_port( peer_list + *peers, peer_list + *peers - 1 ) < 0 ); | ||
254 | } | ||
255 | |||
256 | int init_logic( char *directory ) { | 200 | int init_logic( char *directory ) { |
257 | glob_t globber; | 201 | glob_t globber; |
258 | int i; | 202 | int i; |
259 | 203 | ||
260 | if( directory ) | 204 | if( directory ) { |
261 | chdir( directory ); | 205 | if( chdir( directory )) |
206 | return -1; | ||
207 | } | ||
262 | 208 | ||
263 | scratch_space = map_file( NULL ); | 209 | srandom( time(NULL)); |
264 | torrents_list = map_file( NULL ); | ||
265 | torrents_count = 0; | ||
266 | 210 | ||
267 | if( !scratch_space || !torrents_list ) { | 211 | // Initialize control structures |
268 | if( scratch_space || torrents_list ) | 212 | byte_zero( all_torrents, sizeof (all_torrents)); |
269 | unmap_file( NULL, scratch_space ? (void*)scratch_space : (void*)torrents_list, 0 ); | ||
270 | return -1; | ||
271 | } | ||
272 | 213 | ||
273 | // Scan directory for filenames in the form [0-9A-F]{20} | 214 | // Scan directory for filenames in the form [0-9A-F]{20} |
274 | // * I know this looks ugly, but I've seen A-F to match umlauts as well in strange locales | 215 | // * I know this looks ugly, but I've seen A-F to match umlauts as well in strange locales |
@@ -286,19 +227,8 @@ int init_logic( char *directory ) { | |||
286 | "[0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef]" | 227 | "[0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef][0-9ABCDEFabcdef]" |
287 | , GLOB_NOCHECK, 0, &globber) ) | 228 | , GLOB_NOCHECK, 0, &globber) ) |
288 | { | 229 | { |
289 | for( i=0; i<globber.gl_matchc; ++i ) { | 230 | for( i=0; i<globber.gl_matchc; ++i ) |
290 | #ifdef _DEBUG | 231 | printf( "Found: %s\n", globber.gl_pathv[i] ); |
291 | printf( "Found dir: %s\n", globber.gl_pathv[i] ); | ||
292 | #endif | ||
293 | |||
294 | if( ( torrents_list[torrents_count].peer_list = map_file( globber.gl_pathv[i] ) ) ) { | ||
295 | MEMMOVE( &torrents_list[torrents_count].hash, globber.gl_pathv[i], sizeof( ot_hash ) ); | ||
296 | count_peers_and_seeds( torrents_list[torrents_count].peer_list, | ||
297 | &torrents_list[torrents_count].peer_count, | ||
298 | &torrents_list[torrents_count].seed_count ); | ||
299 | torrents_count++; | ||
300 | } | ||
301 | } | ||
302 | } | 232 | } |
303 | 233 | ||
304 | globfree( &globber ); | 234 | globfree( &globber ); |
@@ -306,9 +236,15 @@ int init_logic( char *directory ) { | |||
306 | } | 236 | } |
307 | 237 | ||
308 | void deinit_logic( ) { | 238 | void deinit_logic( ) { |
309 | // For all torrents... blablabla | 239 | int i, j; |
310 | while( torrents_count-- ) | 240 | // Free all torrents... |
311 | unmap_file( to_hex(torrents_list[torrents_count].hash), torrents_list[torrents_count].peer_list, torrents_list[torrents_count].peer_count * sizeof(struct ot_peer) ); | 241 | for(i=0; i<256; ++i ) { |
312 | unmap_file( NULL, torrents_list, 0 ); | 242 | if( all_torrents[i].size ) { |
313 | unmap_file( NULL, scratch_space, 0 ); | 243 | ot_torrent torrents_list = (ot_torrent)all_torrents[i].data; |
244 | for( j=0; j<all_torrents[i].size; ++j ) | ||
245 | free_peerlist( torrents_list[j].peer_list ); | ||
246 | free( all_torrents[i].data ); | ||
247 | } | ||
248 | } | ||
249 | byte_zero( all_torrents, sizeof (all_torrents)); | ||
314 | } | 250 | } |