summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--opentracker.c4
-rw-r--r--trackerlogic.c356
-rw-r--r--trackerlogic.h55
3 files changed, 179 insertions, 236 deletions
diff --git a/opentracker.c b/opentracker.c
index f3a7a29..9c76f7f 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -141,8 +141,8 @@ e400:
141 case 8: 141 case 8:
142 if( byte_diff(data,8,"announce")) 142 if( byte_diff(data,8,"announce"))
143 goto e404; 143 goto e404;
144 byte_copy( peer.ip, 4, h->ip ); 144 byte_copy( &peer.ip, 4, h->ip );
145 peer.port = 6881; 145 peer.port_flags = 6881 << 16;
146 numwant = 50; 146 numwant = 50;
147 compact = 1; 147 compact = 1;
148 scanon = 1; 148 scanon = 1;
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//
15int compare_hash( const void *hash1, const void *hash2 ) { return memcmp( hash1, hash2, sizeof( ot_hash )); } 20int compare_hash( const void *hash1, const void *hash2 ) { return memcmp( hash1, hash2, sizeof( ot_hash )); }
16int compare_ip_port( const void *peer1, const void *peer2 ) { return memcmp( &((ot_peer)peer1)->ip, &((ot_peer)peer2)->ip, 6); } 21int compare_ip_port( const void *peer1, const void *peer2 ) {
22if( ((ot_peer)peer1)->ip != ((ot_peer)peer2)->ip ) return ((ot_peer)peer1)->ip - ((ot_peer)peer2)->ip;
23return ((ot_peer)peer1)->port_flags - ((ot_peer)peer2)->port_flags; }
17 24
18void *binary_search( const void *key, const void *base, 25void *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//
43char 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;} 50char 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//
47unsigned long torrents_count = 0; 54struct ot_vector all_torrents[256];
48ot_torrent torrents_list = 0; 55
49ot_byte *scratch_space = 0; 56void *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
73int 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
88void 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
96int 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); 112void 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
57ot_torrent add_peer_to_torrent( ot_hash *hash, ot_peer peer ) { 116ot_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
98inline 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//
109size_t return_peers_for_torrent( ot_torrent torrent, unsigned long amount, char *reply ) { 172size_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//
170void 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
214void 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//
226void *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);
230printf( "%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
242void 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
248void 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
256int init_logic( char *directory ) { 200int 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
308void deinit_logic( ) { 238void 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}
diff --git a/trackerlogic.h b/trackerlogic.h
index ad18cba..73f5d16 100644
--- a/trackerlogic.h
+++ b/trackerlogic.h
@@ -2,6 +2,7 @@
2#define __TRACKERLOGIC_H__ 2#define __TRACKERLOGIC_H__
3 3
4#include <sys/types.h> 4#include <sys/types.h>
5#include <sys/time.h>
5 6
6/* Should be called BYTE, WORD, DWORD - but some OSs already have that and there's no #iftypedef */ 7/* Should be called BYTE, WORD, DWORD - but some OSs already have that and there's no #iftypedef */
7/* They mark memory used as data instead of integer or human readable string - 8/* They mark memory used as data instead of integer or human readable string -
@@ -10,15 +11,9 @@ typedef unsigned char ot_byte;
10typedef unsigned short ot_word; 11typedef unsigned short ot_word;
11typedef unsigned long ot_dword; 12typedef unsigned long ot_dword;
12 13
13typedef unsigned long ot_time;
14typedef ot_byte ot_hash[20]; 14typedef ot_byte ot_hash[20];
15typedef ot_byte ot_ip[ 4/*0*/ ]; 15typedef ot_dword ot_ip;
16// tunables 16typedef time_t ot_time;
17static const unsigned long OT_TIMEOUT = 2700;
18static const unsigned long OT_HUGE_FILESIZE = 1024*1024*256; // Thats 256MB per file, enough for 204800 peers of 128 bytes
19
20// We will not service v6, yes
21#define OT_COMPACT_ONLY
22 17
23#define MEMMOVE memmove 18#define MEMMOVE memmove
24#define BZERO bzero 19#define BZERO bzero
@@ -27,28 +22,41 @@ static const unsigned long OT_HUGE_FILESIZE = 1024*1024*256; // Thats 256MB p
27#define BINARY_FIND binary_search 22#define BINARY_FIND binary_search
28#define NOW time(NULL) 23#define NOW time(NULL)
29 24
25// We maintain a list of 256 pointers to sorted list of ot_torrent structs
26// Sort key is, of course, its hash
27
28// This list points to 9 pools of peers each grouped in five-minute-intervals
29// thus achieving a timeout of 2700s or 45 minutes
30// These pools are sorted by its binary content
31
32#define OT_POOLS_COUNT 9
33#define OT_POOLS_TIMEOUT 300
34
35typedef struct ot_vector {
36 void *data;
37 size_t size;
38 size_t space;
39} *ot_vector;
40
30typedef struct ot_peer { 41typedef struct ot_peer {
31#ifndef OT_COMPACT_ONLY 42 ot_ip ip;
32 ot_hash id; 43 ot_dword port_flags;
33 ot_hash key;
34#endif
35 ot_ip ip;
36 ot_word port;
37 ot_time death;
38 ot_byte flags;
39} *ot_peer; 44} *ot_peer;
40static const ot_byte PEER_FLAG_SEEDING = 0x80; 45static const ot_byte PEER_FLAG_SEEDING = 0x80;
41static const ot_byte PEER_IP_LENGTH_MASK = 0x3f;
42 46
43typedef struct { 47typedef struct ot_peerlist {
48 ot_time base;
49 unsigned long seed_count[ OT_POOLS_COUNT ];
50 struct ot_vector peers[ OT_POOLS_COUNT ];
51} *ot_peerlist;
52
53typedef struct ot_torrent {
44 ot_hash hash; 54 ot_hash hash;
45 ot_peer peer_list; 55 ot_peerlist peer_list;
46 unsigned long peer_count;
47 unsigned long seed_count;
48} *ot_torrent; 56} *ot_torrent;
49 57
50void *map_file( char *file_name ); 58void *map_file( char *file_name, size_t map_size );
51void unmap_file( char *file_name, void *map, unsigned long real_size ); 59void unmap_file( char *file_name, void *map, size_t mapped_size, unsigned long real_size );
52 60
53// This behaves quite like bsearch but allows to find 61// This behaves quite like bsearch but allows to find
54// the insertion point for inserts after unsuccessful searches 62// the insertion point for inserts after unsuccessful searches
@@ -68,6 +76,5 @@ void deinit_logic( );
68 76
69ot_torrent add_peer_to_torrent( ot_hash *hash, ot_peer peer ); 77ot_torrent add_peer_to_torrent( ot_hash *hash, ot_peer peer );
70size_t return_peers_for_torrent( ot_torrent torrent, unsigned long amount, char *reply ); 78size_t return_peers_for_torrent( ot_torrent torrent, unsigned long amount, char *reply );
71void heal_torrent( ot_torrent torrent );
72 79
73#endif 80#endif