summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--trackerlogic.c33
1 files changed, 22 insertions, 11 deletions
diff --git a/trackerlogic.c b/trackerlogic.c
index 37cca03..18895ba 100644
--- a/trackerlogic.c
+++ b/trackerlogic.c
@@ -183,8 +183,9 @@ size_t add_peer_to_torrent_and_return_peers( ot_hash hash, ot_peer *peer, PROTO_
183 183
184static size_t return_peers_all( ot_peerlist *peer_list, char *reply ) { 184static size_t return_peers_all( ot_peerlist *peer_list, char *reply ) {
185 unsigned int bucket, num_buckets = 1; 185 unsigned int bucket, num_buckets = 1;
186 ot_vector * bucket_list = &peer_list->peers; 186 ot_vector * bucket_list = &peer_list->peers;
187 char * r = reply; 187 size_t result = OT_PEER_COMPARE_SIZE * peer_list->peer_count;
188 char * r_end = reply + result;
188 189
189 if( OT_PEERLIST_HASBUCKETS(peer_list) ) { 190 if( OT_PEERLIST_HASBUCKETS(peer_list) ) {
190 num_buckets = bucket_list->size; 191 num_buckets = bucket_list->size;
@@ -195,12 +196,16 @@ static size_t return_peers_all( ot_peerlist *peer_list, char *reply ) {
195 ot_peer * peers = (ot_peer*)bucket_list[bucket].data; 196 ot_peer * peers = (ot_peer*)bucket_list[bucket].data;
196 size_t peer_count = bucket_list[bucket].size; 197 size_t peer_count = bucket_list[bucket].size;
197 while( peer_count-- ) { 198 while( peer_count-- ) {
198 memcpy(r,peers++,OT_PEER_COMPARE_SIZE); 199 if( OT_PEERFLAG(peers) & PEER_FLAG_SEEDING ) {
199 r+=OT_PEER_COMPARE_SIZE; 200 r_end-=OT_PEER_COMPARE_SIZE;
201 memcpy(r_end,peers++,OT_PEER_COMPARE_SIZE);
202 } else {
203 memcpy(reply,peers++,OT_PEER_COMPARE_SIZE);
204 reply+=OT_PEER_COMPARE_SIZE;
205 }
200 } 206 }
201 } 207 }
202 208 return result;
203 return r - reply;
204} 209}
205 210
206static size_t return_peers_selection( ot_peerlist *peer_list, size_t amount, char *reply ) { 211static size_t return_peers_selection( ot_peerlist *peer_list, size_t amount, char *reply ) {
@@ -209,8 +214,9 @@ static size_t return_peers_selection( ot_peerlist *peer_list, size_t amount, cha
209 unsigned int shifted_pc = peer_list->peer_count; 214 unsigned int shifted_pc = peer_list->peer_count;
210 unsigned int shifted_step = 0; 215 unsigned int shifted_step = 0;
211 unsigned int shift = 0; 216 unsigned int shift = 0;
212 char * r = reply; 217 size_t result = OT_PEER_COMPARE_SIZE * amount;
213 218 char * r_end = reply + result;
219
214 if( OT_PEERLIST_HASBUCKETS(peer_list) ) { 220 if( OT_PEERLIST_HASBUCKETS(peer_list) ) {
215 num_buckets = bucket_list->size; 221 num_buckets = bucket_list->size;
216 bucket_list = (ot_vector *)bucket_list->data; 222 bucket_list = (ot_vector *)bucket_list->data;
@@ -239,10 +245,15 @@ static size_t return_peers_selection( ot_peerlist *peer_list, size_t amount, cha
239 bucket_index = ( bucket_index + 1 ) % num_buckets; 245 bucket_index = ( bucket_index + 1 ) % num_buckets;
240 } 246 }
241 peer = ((ot_peer*)bucket_list[bucket_index].data) + bucket_offset; 247 peer = ((ot_peer*)bucket_list[bucket_index].data) + bucket_offset;
242 memcpy(r,peer,OT_PEER_COMPARE_SIZE); 248 if( OT_PEERFLAG(peer) & PEER_FLAG_SEEDING ) {
243 r+=OT_PEER_COMPARE_SIZE; 249 r_end-=OT_PEER_COMPARE_SIZE;
250 memcpy(r_end,peer,OT_PEER_COMPARE_SIZE);
251 } else {
252 memcpy(reply,peer,OT_PEER_COMPARE_SIZE);
253 reply+=OT_PEER_COMPARE_SIZE;
254 }
244 } 255 }
245 return r - reply; 256 return result;
246} 257}
247 258
248/* Compiles a list of random peers for a torrent 259/* Compiles a list of random peers for a torrent