An open and free bittorrent tracker https://erdgeist.org/gitweb/opentracker
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

267 righe
9.7 KiB

  1. /* This software was written by Dirk Engling <erdgeist@erdgeist.org>
  2. It is considered beerware. Prost. Skol. Cheers or whatever.
  3. $id$ */
  4. /* System */
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <strings.h>
  8. #include <stdint.h>
  9. /* Opentracker */
  10. #include "trackerlogic.h"
  11. #include "ot_vector.h"
  12. /* Libowfat */
  13. #include "uint32.h"
  14. #include "uint16.h"
  15. static int vector_compare_peer(const void *peer1, const void *peer2 ) {
  16. return memcmp( peer1, peer2, OT_PEER_COMPARE_SIZE );
  17. }
  18. /* This function gives us a binary search that returns a pointer, even if
  19. no exact match is found. In that case it sets exactmatch 0 and gives
  20. calling functions the chance to insert data
  21. */
  22. void *binary_search( const void * const key, const void * base, const size_t member_count, const size_t member_size,
  23. size_t compare_size, int *exactmatch ) {
  24. size_t interval = member_count;
  25. while( interval ) {
  26. uint8_t *lookat = ((uint8_t*)base) + member_size * ( interval / 2 );
  27. int cmp = memcmp( lookat, key, compare_size );
  28. if(cmp == 0 ) {
  29. base = lookat;
  30. break;
  31. }
  32. if(cmp < 0) {
  33. base = lookat + member_size;
  34. interval--;
  35. }
  36. interval /= 2;
  37. }
  38. *exactmatch = interval;
  39. return (void*)base;
  40. }
  41. static uint8_t vector_hash_peer( ot_peer *peer, int bucket_count ) {
  42. unsigned int hash = 5381, i = OT_PEER_COMPARE_SIZE;
  43. uint8_t *p = (uint8_t*)peer;
  44. while( i-- ) hash += (hash<<5) + *(p++);
  45. return hash % bucket_count;
  46. }
  47. /* This is the generic insert operation for our vector type.
  48. It tries to locate the object at "key" with size "member_size" by comparing its first "compare_size" bytes with
  49. those of objects in vector. Our special "binary_search" function does that and either returns the match or a
  50. pointer to where the object is to be inserted. vector_find_or_insert makes space for the object and copies it,
  51. if it wasn't found in vector. Caller needs to check the passed "exactmatch" variable to see, whether an insert
  52. took place. If resizing the vector failed, NULL is returned, else the pointer to the object in vector.
  53. */
  54. void *vector_find_or_insert( ot_vector *vector, void *key, size_t member_size, size_t compare_size, int *exactmatch ) {
  55. uint8_t *match = binary_search( key, vector->data, vector->size, member_size, compare_size, exactmatch );
  56. if( *exactmatch ) return match;
  57. if( vector->size + 1 > vector->space ) {
  58. size_t new_space = vector->space ? OT_VECTOR_GROW_RATIO * vector->space : OT_VECTOR_MIN_MEMBERS;
  59. uint8_t *new_data = realloc( vector->data, new_space * member_size );
  60. if( !new_data ) return NULL;
  61. /* Adjust pointer if it moved by realloc */
  62. match = new_data + (match - (uint8_t*)vector->data);
  63. vector->data = new_data;
  64. vector->space = new_space;
  65. }
  66. memmove( match + member_size, match, ((uint8_t*)vector->data) + member_size * vector->size - match );
  67. vector->size++;
  68. return match;
  69. }
  70. ot_peer *vector_find_or_insert_peer( ot_vector *vector, ot_peer *peer, int *exactmatch ) {
  71. ot_peer *match;
  72. /* If space is zero but size is set, we're dealing with a list of vector->size buckets */
  73. if( vector->space < vector->size )
  74. vector = ((ot_vector*)vector->data) + vector_hash_peer(peer, vector->size );
  75. match = (ot_peer*)binary_search( peer, vector->data, vector->size, sizeof(ot_peer), OT_PEER_COMPARE_SIZE, exactmatch );
  76. if( *exactmatch ) return match;
  77. if( vector->size + 1 > vector->space ) {
  78. size_t new_space = vector->space ? OT_VECTOR_GROW_RATIO * vector->space : OT_VECTOR_MIN_MEMBERS;
  79. ot_peer *new_data = realloc( vector->data, new_space * sizeof(ot_peer) );
  80. if( !new_data ) return NULL;
  81. /* Adjust pointer if it moved by realloc */
  82. match = new_data + (match - (ot_peer*)vector->data);
  83. vector->data = new_data;
  84. vector->space = new_space;
  85. }
  86. memmove( match + 1, match, sizeof(ot_peer) * ( ((ot_peer*)vector->data) + vector->size - match ) );
  87. vector->size++;
  88. return match;
  89. }
  90. /* This is the non-generic delete from vector-operation specialized for peers in pools.
  91. It returns 0 if no peer was found (and thus not removed)
  92. 1 if a non-seeding peer was removed
  93. 2 if a seeding peer was removed
  94. */
  95. int vector_remove_peer( ot_vector *vector, ot_peer *peer ) {
  96. int exactmatch;
  97. ot_peer *match, *end;
  98. if( !vector->size ) return 0;
  99. /* If space is zero but size is set, we're dealing with a list of vector->size buckets */
  100. if( vector->space < vector->size )
  101. vector = ((ot_vector*)vector->data) + vector_hash_peer(peer, vector->size );
  102. end = ((ot_peer*)vector->data) + vector->size;
  103. match = (ot_peer*)binary_search( peer, vector->data, vector->size, sizeof(ot_peer), OT_PEER_COMPARE_SIZE, &exactmatch );
  104. if( !exactmatch ) return 0;
  105. exactmatch = ( OT_PEERFLAG( match ) & PEER_FLAG_SEEDING ) ? 2 : 1;
  106. memmove( match, match + 1, sizeof(ot_peer) * ( end - match - 1 ) );
  107. vector->size--;
  108. vector_fixup_peers( vector );
  109. return exactmatch;
  110. }
  111. void vector_remove_torrent( ot_vector *vector, ot_torrent *match ) {
  112. ot_torrent *end = ((ot_torrent*)vector->data) + vector->size;
  113. if( !vector->size ) return;
  114. /* If this is being called after a unsuccessful malloc() for peer_list
  115. in add_peer_to_torrent, match->peer_list actually might be NULL */
  116. if( match->peer_list) free_peerlist( match->peer_list );
  117. memmove( match, match + 1, sizeof(ot_torrent) * ( end - match - 1 ) );
  118. if( ( --vector->size * OT_VECTOR_SHRINK_THRESH < vector->space ) && ( vector->space >= OT_VECTOR_SHRINK_RATIO * OT_VECTOR_MIN_MEMBERS ) ) {
  119. vector->space /= OT_VECTOR_SHRINK_RATIO;
  120. vector->data = realloc( vector->data, vector->space * sizeof( ot_torrent ) );
  121. }
  122. }
  123. void vector_clean_list( ot_vector * vector, int num_buckets ) {
  124. while( num_buckets-- )
  125. free( vector[num_buckets].data );
  126. free( vector );
  127. return;
  128. }
  129. void vector_redistribute_buckets( ot_peerlist * peer_list ) {
  130. int tmp, bucket, bucket_size_new, num_buckets_new, num_buckets_old = 1;
  131. ot_vector * bucket_list_new, * bucket_list_old = &peer_list->peers;
  132. if( OT_PEERLIST_HASBUCKETS( peer_list ) ) {
  133. num_buckets_old = peer_list->peers.size;
  134. bucket_list_old = peer_list->peers.data;
  135. }
  136. if( peer_list->peer_count < 255 )
  137. num_buckets_new = 1;
  138. else if( peer_list->peer_count > 8192 )
  139. num_buckets_new = 64;
  140. else if( peer_list->peer_count >= 512 && peer_list->peer_count < 4096 )
  141. num_buckets_new = 16;
  142. else if( peer_list->peer_count < 512 && num_buckets_old <= 16 )
  143. num_buckets_new = num_buckets_old;
  144. else if( peer_list->peer_count < 512 )
  145. num_buckets_new = 1;
  146. else if( peer_list->peer_count < 8192 && num_buckets_old > 1 )
  147. num_buckets_new = num_buckets_old;
  148. else
  149. num_buckets_new = 16;
  150. if( num_buckets_new == num_buckets_old )
  151. return;
  152. /* Assume near perfect distribution */
  153. bucket_list_new = malloc( num_buckets_new * sizeof( ot_vector ) );
  154. if( !bucket_list_new) return;
  155. bzero( bucket_list_new, num_buckets_new * sizeof( ot_vector ) );
  156. tmp = peer_list->peer_count / num_buckets_new;
  157. bucket_size_new = OT_VECTOR_MIN_MEMBERS;
  158. while( bucket_size_new < tmp)
  159. bucket_size_new *= OT_VECTOR_GROW_RATIO;
  160. /* preallocate vectors to hold all peers */
  161. for( bucket=0; bucket<num_buckets_new; ++bucket ) {
  162. bucket_list_new[bucket].space = bucket_size_new;
  163. bucket_list_new[bucket].data = malloc( bucket_size_new * sizeof(ot_peer) );
  164. if( !bucket_list_new[bucket].data )
  165. return vector_clean_list( bucket_list_new, num_buckets_new );
  166. }
  167. /* Now sort them into the correct bucket */
  168. for( bucket=0; bucket<num_buckets_old; ++bucket ) {
  169. ot_peer * peers_old = bucket_list_old[bucket].data, * peers_new;
  170. int peer_count_old = bucket_list_old[bucket].size;
  171. while( peer_count_old-- ) {
  172. ot_vector * bucket_dest = bucket_list_new;
  173. if( num_buckets_new > 1 )
  174. bucket_dest += vector_hash_peer(peers_old, num_buckets_new);
  175. if( bucket_dest->size + 1 > bucket_dest->space ) {
  176. void * tmp = realloc( bucket_dest->data, sizeof(ot_peer) * OT_VECTOR_GROW_RATIO * bucket_dest->space );
  177. if( !tmp ) return vector_clean_list( bucket_list_new, num_buckets_new );
  178. bucket_dest->data = tmp;
  179. bucket_dest->space *= OT_VECTOR_GROW_RATIO;
  180. }
  181. peers_new = (ot_peer*)bucket_dest->data;
  182. memcpy(peers_new + bucket_dest->size++, peers_old++, sizeof(ot_peer));
  183. }
  184. }
  185. /* Now sort each bucket to later allow bsearch */
  186. for( bucket=0; bucket<num_buckets_new; ++bucket )
  187. qsort( bucket_list_new[bucket].data, bucket_list_new[bucket].size, sizeof( ot_peer ), vector_compare_peer );
  188. /* Everything worked fine. Now link new bucket_list to peer_list */
  189. if( OT_PEERLIST_HASBUCKETS( peer_list) )
  190. vector_clean_list( (ot_vector*)peer_list->peers.data, peer_list->peers.size );
  191. else
  192. free( peer_list->peers.data );
  193. if( num_buckets_new > 1 ) {
  194. peer_list->peers.data = bucket_list_new;
  195. peer_list->peers.size = num_buckets_new;
  196. peer_list->peers.space = 0; /* Magic marker for "is list of buckets" */
  197. } else {
  198. peer_list->peers.data = bucket_list_new->data;
  199. peer_list->peers.size = bucket_list_new->size;
  200. peer_list->peers.space = bucket_list_new->space;
  201. free( bucket_list_new );
  202. }
  203. }
  204. void vector_fixup_peers( ot_vector * vector ) {
  205. int need_fix = 0;
  206. if( !vector->size ) {
  207. free( vector->data );
  208. vector->data = NULL;
  209. vector->space = 0;
  210. return;
  211. }
  212. while( ( vector->size * OT_VECTOR_SHRINK_THRESH < vector->space ) &&
  213. ( vector->space >= OT_VECTOR_SHRINK_RATIO * OT_VECTOR_MIN_MEMBERS ) ) {
  214. vector->space /= OT_VECTOR_SHRINK_RATIO;
  215. need_fix++;
  216. }
  217. if( need_fix )
  218. vector->data = realloc( vector->data, vector->space * sizeof( ot_peer ) );
  219. }
  220. const char *g_version_vector_c = "$Source: /home/cvsroot/opentracker/ot_vector.c,v $: $Revision: 1.20 $\n";