summaryrefslogtreecommitdiff
path: root/trackerlogic.c
diff options
context:
space:
mode:
Diffstat (limited to 'trackerlogic.c')
-rw-r--r--trackerlogic.c26
1 files changed, 14 insertions, 12 deletions
diff --git a/trackerlogic.c b/trackerlogic.c
index 5d83abb..9b8c541 100644
--- a/trackerlogic.c
+++ b/trackerlogic.c
@@ -54,25 +54,27 @@ char ths[1+2*20];char*to_hex(ot_byte*s){char*m="0123456789ABCDEF";char*e=ths+40;
54struct ot_vector all_torrents[256]; 54struct ot_vector all_torrents[256];
55 55
56void *vector_find_or_insert( ot_vector vector, void *key, size_t member_size, int(*compare_func)(const void*, const void*), int *exactmatch ) { 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 ); 57 ot_byte *match = BINARY_FIND( key, vector->data, vector->size, member_size, compare_func, exactmatch );
59 58
60 if( exactmatch ) return match; 59 if( *exactmatch ) return match;
61 60
62 if( vector->size + 1 >= vector->space ) { 61 if( vector->size + 1 >= vector->space ) {
63 void *new_data = realloc( vector->data, vector->space ? 2 * vector->space : 1024 ); 62 ot_byte *new_data = realloc( vector->data, vector->space ? 2 * vector->space : 1024 );
64 if( !new_data ) return NULL; 63 if( !new_data ) return NULL;
64
65 // Adjust pointer if it moved by realloc
66 match = match - (ot_byte*)vector->data + new_data;
67
65 vector->data = new_data; 68 vector->data = new_data;
66 vector->space = vector->space ? vector->space * 2 : 1024; 69 vector->space = vector->space ? vector->space * 2 : 1024;
67 } 70 }
68 MEMMOVE( match + member_size, match, end - match ); 71 MEMMOVE( match + member_size, match, ((ot_byte*)vector->data) + member_size * vector->size - match );
69 vector->size++; 72 vector->size++;
70 return match; 73 return match;
71} 74}
72 75
73int vector_remove_peer( ot_vector vector, ot_peer peer ) { 76int vector_remove_peer( ot_vector vector, ot_peer peer ) {
74 int exactmatch; 77 int exactmatch;
75 ot_peer end = ((ot_peer)vector->data) + vector->size;
76 ot_peer match; 78 ot_peer match;
77 79
78 if( !vector->size ) return 0; 80 if( !vector->size ) return 0;
@@ -80,7 +82,7 @@ int vector_remove_peer( ot_vector vector, ot_peer peer ) {
80 82
81 if( !exactmatch ) return 0; 83 if( !exactmatch ) return 0;
82 exactmatch = match->port_flags & PEER_FLAG_SEEDING ? 2 : 1; 84 exactmatch = match->port_flags & PEER_FLAG_SEEDING ? 2 : 1;
83 MEMMOVE( match, match + 1, end - match - 1 ); 85 MEMMOVE( match, match + 1, ((ot_peer)vector->data) + vector->size - match - 1 );
84 vector->size--; 86 vector->size--;
85 return exactmatch; 87 return exactmatch;
86} 88}
@@ -187,7 +189,7 @@ ot_torrent add_peer_to_torrent( ot_hash *hash, ot_peer peer ) {
187size_t return_peers_for_torrent( ot_torrent torrent, unsigned long amount, char *reply ) { 189size_t return_peers_for_torrent( ot_torrent torrent, unsigned long amount, char *reply ) {
188 char *r = reply; 190 char *r = reply;
189 unsigned long peer_count, index; 191 unsigned long peer_count, index;
190 unsigned long pool_offset = 0, pool_index = 0; 192 signed long pool_offset = -1, pool_index = 0;
191 signed long wert = -1; 193 signed long wert = -1;
192 194
193 for( peer_count=index=0; index<OT_POOLS_COUNT; ++index) peer_count += torrent->peer_list->peers[index].size; 195 for( peer_count=index=0; index<OT_POOLS_COUNT; ++index) peer_count += torrent->peer_list->peers[index].size;
@@ -198,14 +200,14 @@ size_t return_peers_for_torrent( ot_torrent torrent, unsigned long amount, char
198 double step = 1.8*((double)( peer_count - wert - 1 ))/((double)( amount - index )); 200 double step = 1.8*((double)( peer_count - wert - 1 ))/((double)( amount - index ));
199 int off = random() % (int)floor( step ); 201 int off = random() % (int)floor( step );
200 off = 1 + ( off % ( peer_count - wert - 1 )); 202 off = 1 + ( off % ( peer_count - wert - 1 ));
201 wert += off; 203 wert += off; pool_offset += off;
202 204
203 while( pool_offset + off > torrent->peer_list->peers[pool_index].size ) { 205 while( pool_offset >= torrent->peer_list->peers[pool_index].size ) {
204 off -= torrent->peer_list->peers[pool_index].size - pool_offset; 206 pool_offset -= torrent->peer_list->peers[pool_index].size;
205 pool_offset = 0; pool_index++; 207 pool_index++;
206 } 208 }
207 209
208 MEMMOVE( r, ((ot_peer*)torrent->peer_list->peers[pool_index].data)[pool_offset], 6 ); 210 MEMMOVE( r, ((ot_peer*)torrent->peer_list->peers[pool_index].data) + pool_offset, 6 );
209 r += 6; 211 r += 6;
210 } 212 }
211 *r++ = 'e'; 213 *r++ = 'e';