diff options
Diffstat (limited to 'src/trackerlogic.c')
| -rw-r--r-- | src/trackerlogic.c | 247 |
1 files changed, 130 insertions, 117 deletions
diff --git a/src/trackerlogic.c b/src/trackerlogic.c index 8e30a48..0b032e1 100644 --- a/src/trackerlogic.c +++ b/src/trackerlogic.c | |||
| @@ -28,20 +28,33 @@ | |||
| 28 | /* Forward declaration */ | 28 | /* Forward declaration */ |
| 29 | size_t return_peers_for_torrent(struct ot_workstruct *ws, ot_torrent *torrent, size_t amount, char *reply, PROTO_FLAG proto); | 29 | size_t return_peers_for_torrent(struct ot_workstruct *ws, ot_torrent *torrent, size_t amount, char *reply, PROTO_FLAG proto); |
| 30 | 30 | ||
| 31 | void free_peer_vector(ot_vector *peer_vector) { | ||
| 32 | if (OT_VECTOR_HASBUCKETS(peer_vector)) | ||
| 33 | vector_clean_list((ot_vector *)peer_vector->data, peer_vector->size); | ||
| 34 | else | ||
| 35 | free(peer_vector->data); /* free(NULL) on an absent family is fine */ | ||
| 36 | } | ||
| 37 | |||
| 31 | void free_peerlist(ot_peerlist *peer_list) { | 38 | void free_peerlist(ot_peerlist *peer_list) { |
| 32 | if (peer_list && peer_list->peers.data) { | 39 | if (!peer_list) |
| 33 | if (OT_PEERLIST_HASBUCKETS(peer_list)) | 40 | return; |
| 34 | vector_clean_list((ot_vector *)peer_list->peers.data, peer_list->peers.size); | 41 | free_peer_vector(&peer_list->peers6); |
| 35 | else | 42 | free_peer_vector(&peer_list->peers4); |
| 36 | free(peer_list->peers.data); | ||
| 37 | } | ||
| 38 | free(peer_list); | 43 | free(peer_list); |
| 39 | } | 44 | } |
| 40 | 45 | ||
| 41 | void add_torrent_from_saved_state(ot_hash const hash, ot_time base, size_t down_count) { | 46 | /* How many minutes before this moment the torrent was last touched, which is |
| 47 | what a state dump records. */ | ||
| 48 | void add_torrent_from_saved_state(ot_hash const hash, ot_time age, size_t down_count) { | ||
| 42 | int exactmatch; | 49 | int exactmatch; |
| 50 | ot_time now = time_now_minutes(); | ||
| 43 | ot_torrent *torrent; | 51 | ot_torrent *torrent; |
| 44 | ot_vector *torrents_list = mutex_bucket_lock_by_hash(hash); | 52 | ot_vector *torrents_list; |
| 53 | |||
| 54 | if (age > OT_TORRENT_TIMEOUT) | ||
| 55 | return; | ||
| 56 | |||
| 57 | torrents_list = mutex_bucket_lock_by_hash(hash); | ||
| 45 | 58 | ||
| 46 | if (!accesslist_hashisvalid(hash)) { | 59 | if (!accesslist_hashisvalid(hash)) { |
| 47 | mutex_bucket_unlock_by_hash(hash, 0); | 60 | mutex_bucket_unlock_by_hash(hash, 0); |
| @@ -54,28 +67,18 @@ void add_torrent_from_saved_state(ot_hash const hash, ot_time base, size_t down_ | |||
| 54 | return; | 67 | return; |
| 55 | } | 68 | } |
| 56 | 69 | ||
| 57 | /* Create a new torrent entry, then */ | 70 | /* A restored torrent starts with no peers, carrying only its base and its |
| 71 | download count until the first announce brings a family list to life */ | ||
| 58 | memset(torrent, 0, sizeof(ot_torrent)); | 72 | memset(torrent, 0, sizeof(ot_torrent)); |
| 59 | memcpy(torrent->hash, hash, sizeof(ot_hash)); | 73 | memcpy(torrent->hash, hash, sizeof(ot_hash)); |
| 60 | 74 | torrent->base = now > age ? (uint32_t)(now - age) : 0; | |
| 61 | if (!(torrent->peer_list6 = malloc(sizeof(ot_peerlist))) || !(torrent->peer_list4 = malloc(sizeof(ot_peerlist)))) { | 75 | torrent->down_count = (uint32_t)down_count; |
| 62 | vector_remove_torrent(torrents_list, torrent); | ||
| 63 | mutex_bucket_unlock_by_hash(hash, 0); | ||
| 64 | return; | ||
| 65 | } | ||
| 66 | |||
| 67 | memset(torrent->peer_list6, 0, sizeof(ot_peerlist)); | ||
| 68 | memset(torrent->peer_list4, 0, sizeof(ot_peerlist)); | ||
| 69 | torrent->peer_list6->base = base; | ||
| 70 | torrent->peer_list4->base = base; | ||
| 71 | torrent->peer_list6->down_count = down_count; | ||
| 72 | torrent->peer_list4->down_count = down_count; | ||
| 73 | 76 | ||
| 74 | mutex_bucket_unlock_by_hash(hash, 1); | 77 | mutex_bucket_unlock_by_hash(hash, 1); |
| 75 | } | 78 | } |
| 76 | 79 | ||
| 77 | size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstruct *ws, size_t amount) { | 80 | size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstruct *ws, size_t amount) { |
| 78 | int exactmatch, delta_torrentcount = 0; | 81 | int exactmatch, delta_torrentcount = 0, completed = 0; |
| 79 | ot_torrent *torrent; | 82 | ot_torrent *torrent; |
| 80 | ot_peer *peer_dest; | 83 | ot_peer *peer_dest; |
| 81 | ot_vector *torrents_list = mutex_bucket_lock_by_hash(*ws->hash); | 84 | ot_vector *torrents_list = mutex_bucket_lock_by_hash(*ws->hash); |
| @@ -90,6 +93,13 @@ size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstru | |||
| 90 | memcpy(ws->reply, invalid_hash, strlen(invalid_hash)); | 93 | memcpy(ws->reply, invalid_hash, strlen(invalid_hash)); |
| 91 | return strlen(invalid_hash); | 94 | return strlen(invalid_hash); |
| 92 | } | 95 | } |
| 96 | if (proto == FLAG_UDP) { | ||
| 97 | char *r = ws->reply; | ||
| 98 | pack_uint32(&r, OT_CLIENT_REJECT_INTERVAL_RANDOM); | ||
| 99 | pack_uint32(&r, 0); | ||
| 100 | pack_uint32(&r, 0); | ||
| 101 | return r - ws->reply; | ||
| 102 | } | ||
| 93 | return 0; | 103 | return 0; |
| 94 | } | 104 | } |
| 95 | 105 | ||
| @@ -103,26 +113,23 @@ size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstru | |||
| 103 | /* Create a new torrent entry, then */ | 113 | /* Create a new torrent entry, then */ |
| 104 | memset(torrent, 0, sizeof(ot_torrent)); | 114 | memset(torrent, 0, sizeof(ot_torrent)); |
| 105 | memcpy(torrent->hash, *ws->hash, sizeof(ot_hash)); | 115 | memcpy(torrent->hash, *ws->hash, sizeof(ot_hash)); |
| 106 | |||
| 107 | if (!(torrent->peer_list6 = malloc(sizeof(ot_peerlist))) || !(torrent->peer_list4 = malloc(sizeof(ot_peerlist)))) { | ||
| 108 | vector_remove_torrent(torrents_list, torrent); | ||
| 109 | mutex_bucket_unlock_by_hash(*ws->hash, 0); | ||
| 110 | return 0; | ||
| 111 | } | ||
| 112 | |||
| 113 | memset(torrent->peer_list6, 0, sizeof(ot_peerlist)); | ||
| 114 | memset(torrent->peer_list4, 0, sizeof(ot_peerlist)); | ||
| 115 | delta_torrentcount = 1; | 116 | delta_torrentcount = 1; |
| 116 | } else | 117 | } else |
| 117 | clean_single_torrent(torrent); | 118 | clean_single_torrent(torrent); |
| 118 | 119 | ||
| 119 | torrent->peer_list6->base = time_now_minutes(); | 120 | torrent->base = time_now_minutes(); |
| 120 | torrent->peer_list4->base = time_now_minutes(); | ||
| 121 | 121 | ||
| 122 | peer_list = peer_size == OT_PEER_SIZE6 ? torrent->peer_list6 : torrent->peer_list4; | 122 | /* The peerlist, holding both families, is created on the first peer */ |
| 123 | if (!torrent->peer_list && !(torrent->peer_list = calloc(1, sizeof(ot_peerlist)))) { | ||
| 124 | if (delta_torrentcount) | ||
| 125 | vector_remove_torrent(torrents_list, torrent); | ||
| 126 | mutex_bucket_unlock_by_hash(*ws->hash, 0); | ||
| 127 | return 0; | ||
| 128 | } | ||
| 129 | peer_list = torrent->peer_list; | ||
| 123 | 130 | ||
| 124 | /* Check for peer in torrent */ | 131 | /* Check for peer in its family's vector */ |
| 125 | peer_dest = vector_find_or_insert_peer(&(peer_list->peers), peer_src, peer_size, &exactmatch); | 132 | peer_dest = vector_find_or_insert_peer(peer_size == OT_PEER_SIZE6 ? &peer_list->peers6 : &peer_list->peers4, peer_src, peer_size, &exactmatch); |
| 126 | if (!peer_dest) { | 133 | if (!peer_dest) { |
| 127 | mutex_bucket_unlock_by_hash(*ws->hash, delta_torrentcount); | 134 | mutex_bucket_unlock_by_hash(*ws->hash, delta_torrentcount); |
| 128 | return 0; | 135 | return 0; |
| @@ -145,10 +152,9 @@ size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstru | |||
| 145 | livesync_tell(ws); | 152 | livesync_tell(ws); |
| 146 | #endif | 153 | #endif |
| 147 | 154 | ||
| 148 | peer_list->peer_count++; | ||
| 149 | if (OT_PEERFLAG(ws->peer) & PEER_FLAG_COMPLETED) { | 155 | if (OT_PEERFLAG(ws->peer) & PEER_FLAG_COMPLETED) { |
| 150 | peer_list->down_count++; | 156 | torrent->down_count++; |
| 151 | stats_issue_event(ws, EVENT_COMPLETED, 0, 0); | 157 | completed = 1; /* the event, which may syslog, is issued after unlock */ |
| 152 | } | 158 | } |
| 153 | if (OT_PEERFLAG(ws->peer) & PEER_FLAG_SEEDING) | 159 | if (OT_PEERFLAG(ws->peer) & PEER_FLAG_SEEDING) |
| 154 | peer_list->seed_count++; | 160 | peer_list->seed_count++; |
| @@ -174,8 +180,8 @@ size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstru | |||
| 174 | if (!(OT_PEERFLAG_D(peer_dest, peer_size) & PEER_FLAG_SEEDING) && (OT_PEERFLAG(ws->peer) & PEER_FLAG_SEEDING)) | 180 | if (!(OT_PEERFLAG_D(peer_dest, peer_size) & PEER_FLAG_SEEDING) && (OT_PEERFLAG(ws->peer) & PEER_FLAG_SEEDING)) |
| 175 | peer_list->seed_count++; | 181 | peer_list->seed_count++; |
| 176 | if (!(OT_PEERFLAG_D(peer_dest, peer_size) & PEER_FLAG_COMPLETED) && (OT_PEERFLAG(ws->peer) & PEER_FLAG_COMPLETED)) { | 182 | if (!(OT_PEERFLAG_D(peer_dest, peer_size) & PEER_FLAG_COMPLETED) && (OT_PEERFLAG(ws->peer) & PEER_FLAG_COMPLETED)) { |
| 177 | peer_list->down_count++; | 183 | torrent->down_count++; |
| 178 | stats_issue_event(ws, EVENT_COMPLETED, 0, 0); | 184 | completed = 1; /* the event, which may syslog, is issued after unlock */ |
| 179 | } | 185 | } |
| 180 | if (OT_PEERFLAG_D(peer_dest, peer_size) & PEER_FLAG_COMPLETED) | 186 | if (OT_PEERFLAG_D(peer_dest, peer_size) & PEER_FLAG_COMPLETED) |
| 181 | OT_PEERFLAG(ws->peer) |= PEER_FLAG_COMPLETED; | 187 | OT_PEERFLAG(ws->peer) |= PEER_FLAG_COMPLETED; |
| @@ -185,23 +191,29 @@ size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstru | |||
| 185 | #ifdef WANT_SYNC | 191 | #ifdef WANT_SYNC |
| 186 | if (proto == FLAG_MCA) { | 192 | if (proto == FLAG_MCA) { |
| 187 | mutex_bucket_unlock_by_hash(*ws->hash, delta_torrentcount); | 193 | mutex_bucket_unlock_by_hash(*ws->hash, delta_torrentcount); |
| 194 | if (completed) | ||
| 195 | stats_issue_event(ws, EVENT_COMPLETED, 0, 0); | ||
| 188 | return 0; | 196 | return 0; |
| 189 | } | 197 | } |
| 190 | #endif | 198 | #endif |
| 191 | 199 | ||
| 192 | ws->reply_size = return_peers_for_torrent(ws, torrent, amount, ws->reply, proto); | 200 | ws->reply_size = return_peers_for_torrent(ws, torrent, amount, ws->reply, proto); |
| 193 | mutex_bucket_unlock_by_hash(*ws->hash, delta_torrentcount); | 201 | mutex_bucket_unlock_by_hash(*ws->hash, delta_torrentcount); |
| 202 | |||
| 203 | /* Issued after bucket unlock, so a slow syslog cannot stall the bucket */ | ||
| 204 | if (completed) | ||
| 205 | stats_issue_event(ws, EVENT_COMPLETED, 0, 0); | ||
| 194 | return ws->reply_size; | 206 | return ws->reply_size; |
| 195 | } | 207 | } |
| 196 | 208 | ||
| 197 | static size_t return_peers_all(ot_peerlist *peer_list, size_t peer_size, char *reply) { | 209 | static size_t return_peers_all(ot_vector *peer_vector, size_t peer_size, size_t peer_count, char *reply) { |
| 198 | size_t bucket, num_buckets = 1; | 210 | size_t bucket, num_buckets = 1; |
| 199 | ot_vector *bucket_list = &peer_list->peers; | 211 | ot_vector *bucket_list = peer_vector; |
| 200 | size_t compare_size = OT_PEER_COMPARE_SIZE_FROM_PEER_SIZE(peer_size); | 212 | size_t compare_size = OT_PEER_COMPARE_SIZE_FROM_PEER_SIZE(peer_size); |
| 201 | size_t result = compare_size * peer_list->peer_count; | 213 | size_t result = compare_size * peer_count; |
| 202 | char *r_end = reply + result; | 214 | char *r_end = reply + result; |
| 203 | 215 | ||
| 204 | if (OT_PEERLIST_HASBUCKETS(peer_list)) { | 216 | if (OT_VECTOR_HASBUCKETS(peer_vector)) { |
| 205 | num_buckets = bucket_list->size; | 217 | num_buckets = bucket_list->size; |
| 206 | bucket_list = (ot_vector *)bucket_list->data; | 218 | bucket_list = (ot_vector *)bucket_list->data; |
| 207 | } | 219 | } |
| @@ -223,17 +235,17 @@ static size_t return_peers_all(ot_peerlist *peer_list, size_t peer_size, char *r | |||
| 223 | return result; | 235 | return result; |
| 224 | } | 236 | } |
| 225 | 237 | ||
| 226 | static size_t return_peers_selection(struct ot_workstruct *ws, ot_peerlist *peer_list, size_t peer_size, size_t amount, char *reply) { | 238 | static size_t return_peers_selection(struct ot_workstruct *ws, ot_vector *peer_vector, size_t peer_size, size_t amount, size_t peer_count, char *reply) { |
| 227 | size_t bucket_offset, bucket_index = 0, num_buckets = 1; | 239 | size_t bucket_offset, bucket_index = 0, num_buckets = 1; |
| 228 | ot_vector *bucket_list = &peer_list->peers; | 240 | ot_vector *bucket_list = peer_vector; |
| 229 | unsigned int shifted_pc = peer_list->peer_count; | 241 | unsigned int shifted_pc = peer_count; |
| 230 | unsigned int shifted_step = 0; | 242 | unsigned int shifted_step = 0; |
| 231 | unsigned int shift = 0; | 243 | unsigned int shift = 0; |
| 232 | size_t compare_size = OT_PEER_COMPARE_SIZE_FROM_PEER_SIZE(peer_size); | 244 | size_t compare_size = OT_PEER_COMPARE_SIZE_FROM_PEER_SIZE(peer_size); |
| 233 | size_t result = compare_size * amount; | 245 | size_t result = compare_size * amount; |
| 234 | char *r_end = reply + result; | 246 | char *r_end = reply + result; |
| 235 | 247 | ||
| 236 | if (OT_PEERLIST_HASBUCKETS(peer_list)) { | 248 | if (OT_VECTOR_HASBUCKETS(peer_vector)) { |
| 237 | num_buckets = bucket_list->size; | 249 | num_buckets = bucket_list->size; |
| 238 | bucket_list = (ot_vector *)bucket_list->data; | 250 | bucket_list = (ot_vector *)bucket_list->data; |
| 239 | } | 251 | } |
| @@ -249,7 +261,7 @@ static size_t return_peers_selection(struct ot_workstruct *ws, ot_peerlist *peer | |||
| 249 | 261 | ||
| 250 | /* Initialize somewhere in the middle of peers so that | 262 | /* Initialize somewhere in the middle of peers so that |
| 251 | fixpoint's aliasing doesn't alway miss the same peers */ | 263 | fixpoint's aliasing doesn't alway miss the same peers */ |
| 252 | bucket_offset = nrand48(ws->rand48_state) % peer_list->peer_count; | 264 | bucket_offset = nrand48(ws->rand48_state) % peer_count; |
| 253 | 265 | ||
| 254 | while (amount--) { | 266 | while (amount--) { |
| 255 | ot_peer *peer; | 267 | ot_peer *peer; |
| @@ -277,23 +289,24 @@ static size_t return_peers_selection(struct ot_workstruct *ws, ot_peerlist *peer | |||
| 277 | static size_t return_peers_for_torrent_udp(struct ot_workstruct *ws, ot_torrent *torrent, size_t amount, char *reply) { | 289 | static size_t return_peers_for_torrent_udp(struct ot_workstruct *ws, ot_torrent *torrent, size_t amount, char *reply) { |
| 278 | char *r = reply; | 290 | char *r = reply; |
| 279 | size_t peer_size = peer_size_from_peer6(&ws->peer); | 291 | size_t peer_size = peer_size_from_peer6(&ws->peer); |
| 280 | ot_peerlist *peer_list = peer_size == OT_PEER_SIZE6 ? torrent->peer_list6 : torrent->peer_list4; | 292 | ot_peerlist *peer_list = torrent->peer_list; |
| 281 | size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count; | 293 | ot_vector *family = peer_size == OT_PEER_SIZE6 ? &peer_list->peers6 : &peer_list->peers4; |
| 282 | size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count; | 294 | size_t list_count = vector_peer_count(family); |
| 295 | size_t peer_count = torrent_peer_count(torrent); | ||
| 296 | size_t seed_count = torrent_seed_count(torrent); | ||
| 283 | 297 | ||
| 284 | if (amount > peer_list->peer_count) | 298 | if (amount > list_count) |
| 285 | amount = peer_list->peer_count; | 299 | amount = list_count; |
| 286 | 300 | ||
| 287 | *(uint32_t *)(r + 0) = htonl(OT_CLIENT_REQUEST_INTERVAL_RANDOM); | 301 | pack_uint32(&r, OT_CLIENT_REQUEST_INTERVAL_RANDOM); |
| 288 | *(uint32_t *)(r + 4) = htonl(peer_count - seed_count); | 302 | pack_uint32(&r, peer_count - seed_count); |
| 289 | *(uint32_t *)(r + 8) = htonl(seed_count); | 303 | pack_uint32(&r, seed_count); |
| 290 | r += 12; | ||
| 291 | 304 | ||
| 292 | if (amount) { | 305 | if (amount) { |
| 293 | if (amount == peer_list->peer_count) | 306 | if (amount == list_count) |
| 294 | r += return_peers_all(peer_list, peer_size, r); | 307 | r += return_peers_all(family, peer_size, list_count, r); |
| 295 | else | 308 | else |
| 296 | r += return_peers_selection(ws, peer_list, peer_size, amount, r); | 309 | r += return_peers_selection(ws, family, peer_size, amount, list_count, r); |
| 297 | } | 310 | } |
| 298 | return r - reply; | 311 | return r - reply; |
| 299 | } | 312 | } |
| @@ -302,13 +315,16 @@ static size_t return_peers_for_torrent_tcp(struct ot_workstruct *ws, ot_torrent | |||
| 302 | char *r = reply; | 315 | char *r = reply; |
| 303 | const char *r_end = ws->outbuf + G_OUTBUF_SIZE; | 316 | const char *r_end = ws->outbuf + G_OUTBUF_SIZE; |
| 304 | int erval = OT_CLIENT_REQUEST_INTERVAL_RANDOM; | 317 | int erval = OT_CLIENT_REQUEST_INTERVAL_RANDOM; |
| 305 | size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count; | 318 | ot_peerlist *peer_list = torrent->peer_list; |
| 306 | size_t down_count = torrent->peer_list6->down_count + torrent->peer_list4->down_count; | 319 | size_t count6 = vector_peer_count(&peer_list->peers6); |
| 307 | size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count - seed_count; | 320 | size_t count4 = vector_peer_count(&peer_list->peers4); |
| 321 | size_t seed_count = peer_list->seed_count; | ||
| 322 | size_t down_count = torrent->down_count; | ||
| 323 | size_t peer_count = count6 + count4 - seed_count; | ||
| 308 | 324 | ||
| 309 | /* Simple case: amount of peers in both lists is less than requested, here we return all results */ | 325 | /* Simple case: amount of peers in both lists is less than requested, here we return all results */ |
| 310 | size_t amount_v4 = torrent->peer_list4->peer_count; | 326 | size_t amount_v4 = count4; |
| 311 | size_t amount_v6 = torrent->peer_list6->peer_count; | 327 | size_t amount_v6 = count6; |
| 312 | 328 | ||
| 313 | /* Complex case: both lists have more than enough entries and we need to split between v4 and v6 clients */ | 329 | /* Complex case: both lists have more than enough entries and we need to split between v4 and v6 clients */ |
| 314 | if (amount_v4 + amount_v6 > amount) { | 330 | if (amount_v4 + amount_v6 > amount) { |
| @@ -324,8 +340,8 @@ static size_t return_peers_for_torrent_tcp(struct ot_workstruct *ws, ot_torrent | |||
| 324 | /* Fill the rest according to which family's pool provides more peers */ | 340 | /* Fill the rest according to which family's pool provides more peers */ |
| 325 | amount_left = amount - (amount_v4 + amount_v6); | 341 | amount_left = amount - (amount_v4 + amount_v6); |
| 326 | 342 | ||
| 327 | left_v4 = torrent->peer_list4->peer_count - amount_v4; | 343 | left_v4 = count4 - amount_v4; |
| 328 | left_v6 = torrent->peer_list6->peer_count - amount_v6; | 344 | left_v6 = count6 - amount_v6; |
| 329 | 345 | ||
| 330 | if (left_v4 + left_v6) { | 346 | if (left_v4 + left_v6) { |
| 331 | percent_v4 = (SCALE * left_v4) / (left_v4 + left_v6); | 347 | percent_v4 = (SCALE * left_v4) / (left_v4 + left_v6); |
| @@ -336,9 +352,9 @@ static size_t return_peers_for_torrent_tcp(struct ot_workstruct *ws, ot_torrent | |||
| 336 | amount_v6 += (amount_left * percent_v6) / SCALE; | 352 | amount_v6 += (amount_left * percent_v6) / SCALE; |
| 337 | 353 | ||
| 338 | /* Integer division rounding can leave out a peer */ | 354 | /* Integer division rounding can leave out a peer */ |
| 339 | if (amount_v4 + amount_v6 < amount && amount_v6 < torrent->peer_list6->peer_count) | 355 | if (amount_v4 + amount_v6 < amount && amount_v6 < count6) |
| 340 | ++amount_v6; | 356 | ++amount_v6; |
| 341 | if (amount_v4 + amount_v6 < amount && amount_v4 < torrent->peer_list4->peer_count) | 357 | if (amount_v4 + amount_v6 < amount && amount_v4 < count4) |
| 342 | ++amount_v4; | 358 | ++amount_v4; |
| 343 | } | 359 | } |
| 344 | 360 | ||
| @@ -347,18 +363,18 @@ static size_t return_peers_for_torrent_tcp(struct ot_workstruct *ws, ot_torrent | |||
| 347 | 363 | ||
| 348 | if (amount_v4) { | 364 | if (amount_v4) { |
| 349 | fmt_append(&r, r_end, PEERS_BENCODED4 "%zd:", OT_PEER_COMPARE_SIZE4 * amount_v4); | 365 | fmt_append(&r, r_end, PEERS_BENCODED4 "%zd:", OT_PEER_COMPARE_SIZE4 * amount_v4); |
| 350 | if (amount_v4 == torrent->peer_list4->peer_count) | 366 | if (amount_v4 == count4) |
| 351 | r += return_peers_all(torrent->peer_list4, OT_PEER_SIZE4, r); | 367 | r += return_peers_all(&peer_list->peers4, OT_PEER_SIZE4, count4, r); |
| 352 | else | 368 | else |
| 353 | r += return_peers_selection(ws, torrent->peer_list4, OT_PEER_SIZE4, amount_v4, r); | 369 | r += return_peers_selection(ws, &peer_list->peers4, OT_PEER_SIZE4, amount_v4, count4, r); |
| 354 | } | 370 | } |
| 355 | 371 | ||
| 356 | if (amount_v6) { | 372 | if (amount_v6) { |
| 357 | fmt_append(&r, r_end, PEERS_BENCODED6 "%zd:", OT_PEER_COMPARE_SIZE6 * amount_v6); | 373 | fmt_append(&r, r_end, PEERS_BENCODED6 "%zd:", OT_PEER_COMPARE_SIZE6 * amount_v6); |
| 358 | if (amount_v6 == torrent->peer_list6->peer_count) | 374 | if (amount_v6 == count6) |
| 359 | r += return_peers_all(torrent->peer_list6, OT_PEER_SIZE6, r); | 375 | r += return_peers_all(&peer_list->peers6, OT_PEER_SIZE6, count6, r); |
| 360 | else | 376 | else |
| 361 | r += return_peers_selection(ws, torrent->peer_list6, OT_PEER_SIZE6, amount_v6, r); | 377 | r += return_peers_selection(ws, &peer_list->peers6, OT_PEER_SIZE6, amount_v6, count6, r); |
| 362 | } | 378 | } |
| 363 | 379 | ||
| 364 | fmt_append(&r, r_end, "e"); | 380 | fmt_append(&r, r_end, "e"); |
| @@ -384,23 +400,25 @@ size_t return_udp_scrape_for_torrent(ot_hash const hash, char *reply) { | |||
| 384 | ot_vector *torrents_list = mutex_bucket_lock_by_hash(hash); | 400 | ot_vector *torrents_list = mutex_bucket_lock_by_hash(hash); |
| 385 | ot_torrent *torrent = vector_binary_search(hash, torrents_list->data, torrents_list->size, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch); | 401 | ot_torrent *torrent = vector_binary_search(hash, torrents_list->data, torrents_list->size, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch); |
| 386 | 402 | ||
| 387 | if (!exactmatch) { | 403 | uint32_t seeders = 0, completed = 0, leechers = 0; |
| 388 | memset(reply, 0, 12); | 404 | char *r = reply; |
| 389 | } else { | ||
| 390 | uint32_t *r = (uint32_t *)reply; | ||
| 391 | 405 | ||
| 406 | if (exactmatch) { | ||
| 392 | if (clean_single_torrent(torrent)) { | 407 | if (clean_single_torrent(torrent)) { |
| 393 | vector_remove_torrent(torrents_list, torrent); | 408 | vector_remove_torrent(torrents_list, torrent); |
| 394 | memset(reply, 0, 12); | ||
| 395 | delta_torrentcount = -1; | 409 | delta_torrentcount = -1; |
| 396 | } else { | 410 | } else { |
| 397 | r[0] = htonl(torrent->peer_list6->seed_count + torrent->peer_list4->seed_count); | 411 | seeders = torrent_seed_count(torrent); |
| 398 | r[1] = htonl(torrent->peer_list6->down_count + torrent->peer_list4->down_count); | 412 | completed = torrent->down_count; |
| 399 | r[2] = htonl(torrent->peer_list6->peer_count + torrent->peer_list4->peer_count - torrent->peer_list6->seed_count - torrent->peer_list4->seed_count); | 413 | leechers = torrent_peer_count(torrent) - torrent_seed_count(torrent); |
| 400 | } | 414 | } |
| 401 | } | 415 | } |
| 402 | mutex_bucket_unlock_by_hash(hash, delta_torrentcount); | 416 | mutex_bucket_unlock_by_hash(hash, delta_torrentcount); |
| 403 | return 12; | 417 | |
| 418 | pack_uint32(&r, seeders); | ||
| 419 | pack_uint32(&r, completed); | ||
| 420 | pack_uint32(&r, leechers); | ||
| 421 | return r - reply; | ||
| 404 | } | 422 | } |
| 405 | 423 | ||
| 406 | /* Fetches scrape info for a specific torrent */ | 424 | /* Fetches scrape info for a specific torrent */ |
| @@ -423,9 +441,9 @@ void return_tcp_scrape_for_torrent(ot_hash const *hash_list, size_t amount, char | |||
| 423 | } else { | 441 | } else { |
| 424 | fmt_append(reply, reply_end, "20:"); | 442 | fmt_append(reply, reply_end, "20:"); |
| 425 | fmt_bytes(reply, reply_end, hash, sizeof(ot_hash)); | 443 | fmt_bytes(reply, reply_end, hash, sizeof(ot_hash)); |
| 426 | fmt_append(reply, reply_end, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee", | 444 | size_t seed_count = torrent_seed_count(torrent); |
| 427 | torrent->peer_list6->seed_count + torrent->peer_list4->seed_count, torrent->peer_list6->down_count + torrent->peer_list4->down_count, | 445 | size_t peer_count = torrent_peer_count(torrent); |
| 428 | torrent->peer_list6->peer_count + torrent->peer_list4->peer_count - torrent->peer_list6->seed_count - torrent->peer_list4->seed_count); | 446 | fmt_append(reply, reply_end, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee", seed_count, (size_t)torrent->down_count, peer_count - seed_count); |
| 429 | } | 447 | } |
| 430 | } | 448 | } |
| 431 | mutex_bucket_unlock_by_hash(*hash, delta_torrentcount); | 449 | mutex_bucket_unlock_by_hash(*hash, delta_torrentcount); |
| @@ -434,12 +452,10 @@ void return_tcp_scrape_for_torrent(ot_hash const *hash_list, size_t amount, char | |||
| 434 | fmt_append(reply, reply_end, "ee"); | 452 | fmt_append(reply, reply_end, "ee"); |
| 435 | } | 453 | } |
| 436 | 454 | ||
| 437 | static ot_peerlist dummy_list; | ||
| 438 | size_t remove_peer_from_torrent(PROTO_FLAG proto, struct ot_workstruct *ws) { | 455 | size_t remove_peer_from_torrent(PROTO_FLAG proto, struct ot_workstruct *ws) { |
| 439 | int exactmatch; | 456 | int exactmatch; |
| 440 | ot_vector *torrents_list = mutex_bucket_lock_by_hash(*ws->hash); | 457 | ot_vector *torrents_list = mutex_bucket_lock_by_hash(*ws->hash); |
| 441 | ot_torrent *torrent = vector_binary_search(ws->hash, torrents_list->data, torrents_list->size, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch); | 458 | ot_torrent *torrent = vector_binary_search(ws->hash, torrents_list->data, torrents_list->size, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch); |
| 442 | ot_peerlist *peer_list = &dummy_list; | ||
| 443 | size_t peer_size; /* initialized in next line */ | 459 | size_t peer_size; /* initialized in next line */ |
| 444 | ot_peer const *peer_src = peer_from_peer6(&ws->peer, &peer_size); | 460 | ot_peer const *peer_src = peer_from_peer6(&ws->peer, &peer_size); |
| 445 | size_t peer_count = 0, seed_count = 0; | 461 | size_t peer_count = 0, seed_count = 0; |
| @@ -451,19 +467,13 @@ size_t remove_peer_from_torrent(PROTO_FLAG proto, struct ot_workstruct *ws) { | |||
| 451 | } | 467 | } |
| 452 | #endif | 468 | #endif |
| 453 | 469 | ||
| 454 | if (exactmatch) { | 470 | if (exactmatch && torrent->peer_list) { |
| 455 | peer_list = peer_size == OT_PEER_SIZE6 ? torrent->peer_list6 : torrent->peer_list4; | 471 | ot_vector *family = peer_size == OT_PEER_SIZE6 ? &torrent->peer_list->peers6 : &torrent->peer_list->peers4; |
| 456 | switch (vector_remove_peer(&peer_list->peers, peer_src, peer_size)) { | 472 | if (vector_remove_peer(family, peer_src, peer_size) == 2) |
| 457 | case 2: | 473 | torrent->peer_list->seed_count--; |
| 458 | peer_list->seed_count--; /* Intentional fallthrough */ | ||
| 459 | case 1: | ||
| 460 | peer_list->peer_count--; /* Intentional fallthrough */ | ||
| 461 | default: | ||
| 462 | break; | ||
| 463 | } | ||
| 464 | 474 | ||
| 465 | peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count; | 475 | peer_count = torrent_peer_count(torrent); |
| 466 | seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count; | 476 | seed_count = torrent_seed_count(torrent); |
| 467 | } | 477 | } |
| 468 | 478 | ||
| 469 | if (proto == FLAG_TCP) { | 479 | if (proto == FLAG_TCP) { |
| @@ -477,10 +487,11 @@ size_t remove_peer_from_torrent(PROTO_FLAG proto, struct ot_workstruct *ws) { | |||
| 477 | 487 | ||
| 478 | /* Handle UDP reply */ | 488 | /* Handle UDP reply */ |
| 479 | if (proto == FLAG_UDP) { | 489 | if (proto == FLAG_UDP) { |
| 480 | ((uint32_t *)ws->reply)[2] = htonl(OT_CLIENT_REQUEST_INTERVAL_RANDOM); | 490 | char *r = ws->reply; |
| 481 | ((uint32_t *)ws->reply)[3] = htonl(peer_count - seed_count); | 491 | pack_uint32(&r, OT_CLIENT_REQUEST_INTERVAL_RANDOM); |
| 482 | ((uint32_t *)ws->reply)[4] = htonl(seed_count); | 492 | pack_uint32(&r, peer_count - seed_count); |
| 483 | ws->reply_size = 20; | 493 | pack_uint32(&r, seed_count); |
| 494 | ws->reply_size = r - ws->reply; | ||
| 484 | } | 495 | } |
| 485 | 496 | ||
| 486 | mutex_bucket_unlock_by_hash(*ws->hash, 0); | 497 | mutex_bucket_unlock_by_hash(*ws->hash, 0); |
| @@ -563,7 +574,6 @@ void trackerlogic_init(void) { | |||
| 563 | clean_init(); | 574 | clean_init(); |
| 564 | fullscrape_init(); | 575 | fullscrape_init(); |
| 565 | accesslist_init(); | 576 | accesslist_init(); |
| 566 | livesync_init(); | ||
| 567 | stats_init(); | 577 | stats_init(); |
| 568 | } | 578 | } |
| 569 | 579 | ||
| @@ -576,8 +586,14 @@ int workstruct_init(struct ot_workstruct *ws, size_t inbuf_size) { | |||
| 576 | #ifdef _DEBUG_HTTPERROR | 586 | #ifdef _DEBUG_HTTPERROR |
| 577 | ws->debugbuf = malloc(G_DEBUGBUF_SIZE); | 587 | ws->debugbuf = malloc(G_DEBUGBUF_SIZE); |
| 578 | #endif | 588 | #endif |
| 579 | if (!ws->inbuf || !ws->outbuf) | 589 | if (!ws->inbuf || !ws->outbuf) { |
| 590 | free(ws->inbuf); | ||
| 591 | free(ws->outbuf); | ||
| 592 | #ifdef _DEBUG_HTTPERROR | ||
| 593 | free(ws->debugbuf); | ||
| 594 | #endif | ||
| 580 | return -1; | 595 | return -1; |
| 596 | } | ||
| 581 | 597 | ||
| 582 | /* Picking which peers to hand out starts here, so two workers seeded alike | 598 | /* Picking which peers to hand out starts here, so two workers seeded alike |
| 583 | would hand out alike */ | 599 | would hand out alike */ |
| @@ -599,24 +615,21 @@ void workstruct_deinit(struct ot_workstruct *ws) { | |||
| 599 | } | 615 | } |
| 600 | 616 | ||
| 601 | void trackerlogic_deinit(void) { | 617 | void trackerlogic_deinit(void) { |
| 602 | int delta_torrentcount = 0; | ||
| 603 | size_t bucket, j; | 618 | size_t bucket, j; |
| 604 | 619 | ||
| 605 | /* Stop the background workers first */ | 620 | clean_deinit(); |
| 606 | stats_deinit(); | 621 | stats_deinit(); |
| 607 | livesync_deinit(); | ||
| 608 | accesslist_deinit(); | 622 | accesslist_deinit(); |
| 609 | fullscrape_deinit(); | 623 | fullscrape_deinit(); |
| 610 | clean_deinit(); | ||
| 611 | 624 | ||
| 612 | /* Free all torrents... */ | 625 | /* Free all torrents... */ |
| 613 | for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) { | 626 | for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) { |
| 614 | ot_vector *torrents_list = mutex_bucket_lock(bucket); | 627 | ot_vector *torrents_list = mutex_bucket_lock(bucket); |
| 628 | int delta_torrentcount = 0; | ||
| 615 | if (torrents_list->size) { | 629 | if (torrents_list->size) { |
| 616 | for (j = 0; j < torrents_list->size; ++j) { | 630 | for (j = 0; j < torrents_list->size; ++j) { |
| 617 | ot_torrent *torrent = ((ot_torrent *)(torrents_list->data)) + j; | 631 | ot_torrent *torrent = ((ot_torrent *)(torrents_list->data)) + j; |
| 618 | free_peerlist(torrent->peer_list6); | 632 | free_peerlist(torrent->peer_list); |
| 619 | free_peerlist(torrent->peer_list4); | ||
| 620 | delta_torrentcount -= 1; | 633 | delta_torrentcount -= 1; |
| 621 | } | 634 | } |
| 622 | free(torrents_list->data); | 635 | free(torrents_list->data); |
