summaryrefslogtreecommitdiff
path: root/src/trackerlogic.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/trackerlogic.c')
-rw-r--r--src/trackerlogic.c643
1 files changed, 643 insertions, 0 deletions
diff --git a/src/trackerlogic.c b/src/trackerlogic.c
new file mode 100644
index 0000000..0b032e1
--- /dev/null
+++ b/src/trackerlogic.c
@@ -0,0 +1,643 @@
1/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2 It is considered beerware. Prost. Skol. Cheers or whatever. */
3
4/* System */
5#include <arpa/inet.h>
6#include <stdint.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10
11
12/* Opentracker */
13#include "ot_accesslist.h"
14#include "ot_clean.h"
15#include "ot_fmt.h"
16#include "ot_fullscrape.h"
17#include "ot_http.h"
18#include "ot_ip.h"
19#include "ot_livesync.h"
20#include "ot_mutex.h"
21#include "ot_workqueue.h"
22#include "ot_random.h"
23#include "ot_stats.h"
24#include "ot_udp.h"
25#include "ot_vector.h"
26#include "trackerlogic.h"
27
28/* Forward declaration */
29size_t return_peers_for_torrent(struct ot_workstruct *ws, ot_torrent *torrent, size_t amount, char *reply, PROTO_FLAG proto);
30
31void 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
38void free_peerlist(ot_peerlist *peer_list) {
39 if (!peer_list)
40 return;
41 free_peer_vector(&peer_list->peers6);
42 free_peer_vector(&peer_list->peers4);
43 free(peer_list);
44}
45
46/* How many minutes before this moment the torrent was last touched, which is
47 what a state dump records. */
48void add_torrent_from_saved_state(ot_hash const hash, ot_time age, size_t down_count) {
49 int exactmatch;
50 ot_time now = time_now_minutes();
51 ot_torrent *torrent;
52 ot_vector *torrents_list;
53
54 if (age > OT_TORRENT_TIMEOUT)
55 return;
56
57 torrents_list = mutex_bucket_lock_by_hash(hash);
58
59 if (!accesslist_hashisvalid(hash)) {
60 mutex_bucket_unlock_by_hash(hash, 0);
61 return;
62 }
63
64 torrent = vector_find_or_insert(torrents_list, (void *)hash, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch);
65 if (!torrent || exactmatch) {
66 mutex_bucket_unlock_by_hash(hash, 0);
67 return;
68 }
69
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 */
72 memset(torrent, 0, sizeof(ot_torrent));
73 memcpy(torrent->hash, hash, sizeof(ot_hash));
74 torrent->base = now > age ? (uint32_t)(now - age) : 0;
75 torrent->down_count = (uint32_t)down_count;
76
77 mutex_bucket_unlock_by_hash(hash, 1);
78}
79
80size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstruct *ws, size_t amount) {
81 int exactmatch, delta_torrentcount = 0, completed = 0;
82 ot_torrent *torrent;
83 ot_peer *peer_dest;
84 ot_vector *torrents_list = mutex_bucket_lock_by_hash(*ws->hash);
85 ot_peerlist *peer_list;
86 size_t peer_size; /* initialized in next line */
87 ot_peer const *peer_src = peer_from_peer6(&ws->peer, &peer_size);
88
89 if (!accesslist_hashisvalid(*ws->hash)) {
90 mutex_bucket_unlock_by_hash(*ws->hash, 0);
91 if (proto == FLAG_TCP) {
92 const char invalid_hash[] = "d14:failure reason63:Requested download is not authorized for use with this tracker.e";
93 memcpy(ws->reply, invalid_hash, strlen(invalid_hash));
94 return strlen(invalid_hash);
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 }
103 return 0;
104 }
105
106 torrent = vector_find_or_insert(torrents_list, (void *)ws->hash, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch);
107 if (!torrent) {
108 mutex_bucket_unlock_by_hash(*ws->hash, 0);
109 return 0;
110 }
111
112 if (!exactmatch) {
113 /* Create a new torrent entry, then */
114 memset(torrent, 0, sizeof(ot_torrent));
115 memcpy(torrent->hash, *ws->hash, sizeof(ot_hash));
116 delta_torrentcount = 1;
117 } else
118 clean_single_torrent(torrent);
119
120 torrent->base = time_now_minutes();
121
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;
130
131 /* Check for peer in its family's vector */
132 peer_dest = vector_find_or_insert_peer(peer_size == OT_PEER_SIZE6 ? &peer_list->peers6 : &peer_list->peers4, peer_src, peer_size, &exactmatch);
133 if (!peer_dest) {
134 mutex_bucket_unlock_by_hash(*ws->hash, delta_torrentcount);
135 return 0;
136 }
137
138 /* Tell peer that it's fresh */
139 OT_PEERTIME(ws->peer, OT_PEER_SIZE6) = 0;
140
141 /* Sanitize flags: Whoever claims to have completed download, must be a seeder */
142 if ((OT_PEERFLAG(ws->peer) & (PEER_FLAG_COMPLETED | PEER_FLAG_SEEDING)) == PEER_FLAG_COMPLETED)
143 OT_PEERFLAG(ws->peer) ^= PEER_FLAG_COMPLETED;
144
145 /* If we hadn't had a match create peer there */
146 if (!exactmatch) {
147
148#ifdef WANT_SYNC_LIVE
149 if (proto == FLAG_MCA)
150 OT_PEERFLAG(ws->peer) |= PEER_FLAG_FROM_SYNC;
151 else
152 livesync_tell(ws);
153#endif
154
155 if (OT_PEERFLAG(ws->peer) & PEER_FLAG_COMPLETED) {
156 torrent->down_count++;
157 completed = 1; /* the event, which may syslog, is issued after unlock */
158 }
159 if (OT_PEERFLAG(ws->peer) & PEER_FLAG_SEEDING)
160 peer_list->seed_count++;
161
162 } else {
163 stats_issue_event(ws, EVENT_RENEW, 0, OT_PEERTIME(peer_dest, peer_size));
164#ifdef WANT_SPOT_WOODPECKER
165 if ((OT_PEERTIME(peer_dest, peer_size) > 0) && (OT_PEERTIME(peer_dest, peer_size) < 20))
166 stats_issue_event(ws, EVENT_WOODPECKER, 0, (uintptr_t)&ws->peer);
167#endif
168#ifdef WANT_SYNC_LIVE
169 /* Won't live sync peers that come back too fast. Only exception:
170 fresh "completed" reports */
171 if (proto != FLAG_MCA) {
172 if (OT_PEERTIME(peer_dest, peer_size) > OT_CLIENT_SYNC_RENEW_BOUNDARY ||
173 (!(OT_PEERFLAG_D(peer_dest, peer_size) & PEER_FLAG_COMPLETED) && (OT_PEERFLAG(ws->peer) & PEER_FLAG_COMPLETED)))
174 livesync_tell(ws);
175 }
176#endif
177
178 if ((OT_PEERFLAG_D(peer_dest, peer_size) & PEER_FLAG_SEEDING) && !(OT_PEERFLAG(ws->peer) & PEER_FLAG_SEEDING))
179 peer_list->seed_count--;
180 if (!(OT_PEERFLAG_D(peer_dest, peer_size) & PEER_FLAG_SEEDING) && (OT_PEERFLAG(ws->peer) & PEER_FLAG_SEEDING))
181 peer_list->seed_count++;
182 if (!(OT_PEERFLAG_D(peer_dest, peer_size) & PEER_FLAG_COMPLETED) && (OT_PEERFLAG(ws->peer) & PEER_FLAG_COMPLETED)) {
183 torrent->down_count++;
184 completed = 1; /* the event, which may syslog, is issued after unlock */
185 }
186 if (OT_PEERFLAG_D(peer_dest, peer_size) & PEER_FLAG_COMPLETED)
187 OT_PEERFLAG(ws->peer) |= PEER_FLAG_COMPLETED;
188 }
189
190 memcpy(peer_dest, peer_src, peer_size);
191#ifdef WANT_SYNC
192 if (proto == FLAG_MCA) {
193 mutex_bucket_unlock_by_hash(*ws->hash, delta_torrentcount);
194 if (completed)
195 stats_issue_event(ws, EVENT_COMPLETED, 0, 0);
196 return 0;
197 }
198#endif
199
200 ws->reply_size = return_peers_for_torrent(ws, torrent, amount, ws->reply, proto);
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);
206 return ws->reply_size;
207}
208
209static size_t return_peers_all(ot_vector *peer_vector, size_t peer_size, size_t peer_count, char *reply) {
210 size_t bucket, num_buckets = 1;
211 ot_vector *bucket_list = peer_vector;
212 size_t compare_size = OT_PEER_COMPARE_SIZE_FROM_PEER_SIZE(peer_size);
213 size_t result = compare_size * peer_count;
214 char *r_end = reply + result;
215
216 if (OT_VECTOR_HASBUCKETS(peer_vector)) {
217 num_buckets = bucket_list->size;
218 bucket_list = (ot_vector *)bucket_list->data;
219 }
220
221 for (bucket = 0; bucket < num_buckets; ++bucket) {
222 ot_peer *peers = bucket_list[bucket].data;
223 size_t peer_count = bucket_list[bucket].size;
224 while (peer_count--) {
225 if (OT_PEERFLAG_D(peers, peer_size) & PEER_FLAG_SEEDING) {
226 r_end -= compare_size;
227 memcpy(r_end, peers, compare_size);
228 } else {
229 memcpy(reply, peers, compare_size);
230 reply += compare_size;
231 }
232 peers += peer_size;
233 }
234 }
235 return result;
236}
237
238static 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) {
239 size_t bucket_offset, bucket_index = 0, num_buckets = 1;
240 ot_vector *bucket_list = peer_vector;
241 unsigned int shifted_pc = peer_count;
242 unsigned int shifted_step = 0;
243 unsigned int shift = 0;
244 size_t compare_size = OT_PEER_COMPARE_SIZE_FROM_PEER_SIZE(peer_size);
245 size_t result = compare_size * amount;
246 char *r_end = reply + result;
247
248 if (OT_VECTOR_HASBUCKETS(peer_vector)) {
249 num_buckets = bucket_list->size;
250 bucket_list = (ot_vector *)bucket_list->data;
251 }
252
253 /* Make fixpoint arithmetic as exact as possible */
254#define MAXPRECBIT (1 << (8 * sizeof(int) - 3))
255 while (!(shifted_pc & MAXPRECBIT)) {
256 shifted_pc <<= 1;
257 shift++;
258 }
259 shifted_step = shifted_pc / amount;
260#undef MAXPRECBIT
261
262 /* Initialize somewhere in the middle of peers so that
263 fixpoint's aliasing doesn't alway miss the same peers */
264 bucket_offset = nrand48(ws->rand48_state) % peer_count;
265
266 while (amount--) {
267 ot_peer *peer;
268
269 /* This is the aliased, non shifted range, next value may fall into */
270 unsigned int diff = (((amount + 1) * shifted_step) >> shift) - ((amount * shifted_step) >> shift);
271 bucket_offset += 1 + nrand48(ws->rand48_state) % diff;
272
273 while (bucket_offset >= bucket_list[bucket_index].size) {
274 bucket_offset -= bucket_list[bucket_index].size;
275 bucket_index = (bucket_index + 1) % num_buckets;
276 }
277 peer = (ot_peer*)bucket_list[bucket_index].data + peer_size * bucket_offset;
278 if (OT_PEERFLAG_D(peer, peer_size) & PEER_FLAG_SEEDING) {
279 r_end -= compare_size;
280 memcpy(r_end, peer, compare_size);
281 } else {
282 memcpy(reply, peer, compare_size);
283 reply += compare_size;
284 }
285 }
286 return result;
287}
288
289static size_t return_peers_for_torrent_udp(struct ot_workstruct *ws, ot_torrent *torrent, size_t amount, char *reply) {
290 char *r = reply;
291 size_t peer_size = peer_size_from_peer6(&ws->peer);
292 ot_peerlist *peer_list = torrent->peer_list;
293 ot_vector *family = peer_size == OT_PEER_SIZE6 ? &peer_list->peers6 : &peer_list->peers4;
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);
297
298 if (amount > list_count)
299 amount = list_count;
300
301 pack_uint32(&r, OT_CLIENT_REQUEST_INTERVAL_RANDOM);
302 pack_uint32(&r, peer_count - seed_count);
303 pack_uint32(&r, seed_count);
304
305 if (amount) {
306 if (amount == list_count)
307 r += return_peers_all(family, peer_size, list_count, r);
308 else
309 r += return_peers_selection(ws, family, peer_size, amount, list_count, r);
310 }
311 return r - reply;
312}
313
314static size_t return_peers_for_torrent_tcp(struct ot_workstruct *ws, ot_torrent *torrent, size_t amount, char *reply) {
315 char *r = reply;
316 const char *r_end = ws->outbuf + G_OUTBUF_SIZE;
317 int erval = OT_CLIENT_REQUEST_INTERVAL_RANDOM;
318 ot_peerlist *peer_list = torrent->peer_list;
319 size_t count6 = vector_peer_count(&peer_list->peers6);
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;
324
325 /* Simple case: amount of peers in both lists is less than requested, here we return all results */
326 size_t amount_v4 = count4;
327 size_t amount_v6 = count6;
328
329 /* Complex case: both lists have more than enough entries and we need to split between v4 and v6 clients */
330 if (amount_v4 + amount_v6 > amount) {
331 size_t amount_left, percent_v6 = 0, percent_v4 = 0, left_v6, left_v4;
332 const size_t SCALE = 1024;
333
334 /* If possible, fill at least a quarter of peer from each family */
335 if (amount / 4 <= amount_v4)
336 amount_v4 = amount / 4;
337 if (amount / 4 <= amount_v6)
338 amount_v6 = amount / 4;
339
340 /* Fill the rest according to which family's pool provides more peers */
341 amount_left = amount - (amount_v4 + amount_v6);
342
343 left_v4 = count4 - amount_v4;
344 left_v6 = count6 - amount_v6;
345
346 if (left_v4 + left_v6) {
347 percent_v4 = (SCALE * left_v4) / (left_v4 + left_v6);
348 percent_v6 = (SCALE * left_v6) / (left_v4 + left_v6);
349 }
350
351 amount_v4 += (amount_left * percent_v4) / SCALE;
352 amount_v6 += (amount_left * percent_v6) / SCALE;
353
354 /* Integer division rounding can leave out a peer */
355 if (amount_v4 + amount_v6 < amount && amount_v6 < count6)
356 ++amount_v6;
357 if (amount_v4 + amount_v6 < amount && amount_v4 < count4)
358 ++amount_v4;
359 }
360
361 fmt_append(&r, r_end, "d8:completei%zde10:downloadedi%zde10:incompletei%zde8:intervali%ie12:min intervali%ie", seed_count, down_count, peer_count, erval,
362 erval / 2);
363
364 if (amount_v4) {
365 fmt_append(&r, r_end, PEERS_BENCODED4 "%zd:", OT_PEER_COMPARE_SIZE4 * amount_v4);
366 if (amount_v4 == count4)
367 r += return_peers_all(&peer_list->peers4, OT_PEER_SIZE4, count4, r);
368 else
369 r += return_peers_selection(ws, &peer_list->peers4, OT_PEER_SIZE4, amount_v4, count4, r);
370 }
371
372 if (amount_v6) {
373 fmt_append(&r, r_end, PEERS_BENCODED6 "%zd:", OT_PEER_COMPARE_SIZE6 * amount_v6);
374 if (amount_v6 == count6)
375 r += return_peers_all(&peer_list->peers6, OT_PEER_SIZE6, count6, r);
376 else
377 r += return_peers_selection(ws, &peer_list->peers6, OT_PEER_SIZE6, amount_v6, count6, r);
378 }
379
380 fmt_append(&r, r_end, "e");
381
382 return r - reply;
383}
384
385/* Compiles a list of random peers for a torrent
386 * Reply must have enough space to hold:
387 * 92 + 6 * amount bytes for TCP/IPv4
388 * 92 + 18 * amount bytes for TCP/IPv6
389 * 12 + 6 * amount bytes for UDP/IPv4
390 * 12 + 18 * amount bytes for UDP/IPv6
391 * Does not yet check not to return self
392 */
393size_t return_peers_for_torrent(struct ot_workstruct *ws, ot_torrent *torrent, size_t amount, char *reply, PROTO_FLAG proto) {
394 return proto == FLAG_TCP ? return_peers_for_torrent_tcp(ws, torrent, amount, reply) : return_peers_for_torrent_udp(ws, torrent, amount, reply);
395}
396
397/* Fetches scrape info for a specific torrent */
398size_t return_udp_scrape_for_torrent(ot_hash const hash, char *reply) {
399 int exactmatch, delta_torrentcount = 0;
400 ot_vector *torrents_list = mutex_bucket_lock_by_hash(hash);
401 ot_torrent *torrent = vector_binary_search(hash, torrents_list->data, torrents_list->size, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch);
402
403 uint32_t seeders = 0, completed = 0, leechers = 0;
404 char *r = reply;
405
406 if (exactmatch) {
407 if (clean_single_torrent(torrent)) {
408 vector_remove_torrent(torrents_list, torrent);
409 delta_torrentcount = -1;
410 } else {
411 seeders = torrent_seed_count(torrent);
412 completed = torrent->down_count;
413 leechers = torrent_peer_count(torrent) - torrent_seed_count(torrent);
414 }
415 }
416 mutex_bucket_unlock_by_hash(hash, delta_torrentcount);
417
418 pack_uint32(&r, seeders);
419 pack_uint32(&r, completed);
420 pack_uint32(&r, leechers);
421 return r - reply;
422}
423
424/* Fetches scrape info for a specific torrent */
425void return_tcp_scrape_for_torrent(ot_hash const *hash_list, size_t amount, char **reply, const char *reply_end) {
426 int exactmatch;
427 size_t i;
428
429 fmt_append(reply, reply_end, "d5:filesd");
430
431 for (i = 0; i < amount; ++i) {
432 int delta_torrentcount = 0;
433 ot_hash const *hash = hash_list + i;
434 ot_vector *torrents_list = mutex_bucket_lock_by_hash(*hash);
435 ot_torrent *torrent = vector_binary_search(hash, torrents_list->data, torrents_list->size, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch);
436
437 if (exactmatch) {
438 if (clean_single_torrent(torrent)) {
439 vector_remove_torrent(torrents_list, torrent);
440 delta_torrentcount = -1;
441 } else {
442 fmt_append(reply, reply_end, "20:");
443 fmt_bytes(reply, reply_end, hash, sizeof(ot_hash));
444 size_t seed_count = torrent_seed_count(torrent);
445 size_t peer_count = torrent_peer_count(torrent);
446 fmt_append(reply, reply_end, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee", seed_count, (size_t)torrent->down_count, peer_count - seed_count);
447 }
448 }
449 mutex_bucket_unlock_by_hash(*hash, delta_torrentcount);
450 }
451
452 fmt_append(reply, reply_end, "ee");
453}
454
455size_t remove_peer_from_torrent(PROTO_FLAG proto, struct ot_workstruct *ws) {
456 int exactmatch;
457 ot_vector *torrents_list = mutex_bucket_lock_by_hash(*ws->hash);
458 ot_torrent *torrent = vector_binary_search(ws->hash, torrents_list->data, torrents_list->size, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch);
459 size_t peer_size; /* initialized in next line */
460 ot_peer const *peer_src = peer_from_peer6(&ws->peer, &peer_size);
461 size_t peer_count = 0, seed_count = 0;
462
463#ifdef WANT_SYNC_LIVE
464 if (proto != FLAG_MCA) {
465 OT_PEERFLAG(ws->peer) |= PEER_FLAG_STOPPED;
466 livesync_tell(ws);
467 }
468#endif
469
470 if (exactmatch && torrent->peer_list) {
471 ot_vector *family = peer_size == OT_PEER_SIZE6 ? &torrent->peer_list->peers6 : &torrent->peer_list->peers4;
472 if (vector_remove_peer(family, peer_src, peer_size) == 2)
473 torrent->peer_list->seed_count--;
474
475 peer_count = torrent_peer_count(torrent);
476 seed_count = torrent_seed_count(torrent);
477 }
478
479 if (proto == FLAG_TCP) {
480 int erval = OT_CLIENT_REQUEST_INTERVAL_RANDOM;
481 char *r = ws->reply;
482
483 fmt_append(&r, ws->outbuf + G_OUTBUF_SIZE, "d8:completei%zde10:incompletei%zde8:intervali%ie12:min intervali%ie%s0:e", seed_count, peer_count - seed_count,
484 erval, erval / 2, peer_size == OT_PEER_SIZE6 ? PEERS_BENCODED6 : PEERS_BENCODED4);
485 ws->reply_size = r - ws->reply;
486 }
487
488 /* Handle UDP reply */
489 if (proto == FLAG_UDP) {
490 char *r = ws->reply;
491 pack_uint32(&r, OT_CLIENT_REQUEST_INTERVAL_RANDOM);
492 pack_uint32(&r, peer_count - seed_count);
493 pack_uint32(&r, seed_count);
494 ws->reply_size = r - ws->reply;
495 }
496
497 mutex_bucket_unlock_by_hash(*ws->hash, 0);
498 return ws->reply_size;
499}
500
501void iterate_all_torrents(int (*for_each)(ot_torrent *torrent, uintptr_t data), uintptr_t data) {
502 size_t bucket, j;
503
504 for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) {
505 ot_vector *torrents_list = mutex_bucket_lock(bucket);
506 ot_torrent *torrents = (ot_torrent *)(torrents_list->data);
507
508 for (j = 0; j < torrents_list->size; ++j)
509 if (for_each(torrents + j, data))
510 break;
511
512 mutex_bucket_unlock(bucket, 0);
513 if (!g_opentracker_running)
514 return;
515 }
516}
517
518ot_peer *peer_from_peer6(ot_peer6 *peer, size_t *peer_size) {
519 ot_ip6 *ip = (ot_ip6 *)peer;
520 if (!ip_is_v4mapped(ip)) {
521 *peer_size = OT_PEER_SIZE6;
522 return (ot_peer *)peer;
523 }
524 *peer_size = OT_PEER_SIZE4;
525 return (ot_peer *)(((uint8_t *)peer) + 12);
526}
527
528size_t peer_size_from_peer6(ot_peer6 *peer) {
529 ot_ip6 *ip = (ot_ip6 *)peer;
530 if (!ip_is_v4mapped(ip))
531 return OT_PEER_SIZE6;
532 return OT_PEER_SIZE4;
533}
534
535#ifdef _DEBUG_RANDOMTORRENTS
536void trackerlogic_add_random_torrents(size_t amount) {
537 struct ot_workstruct ws;
538 memset(&ws, 0, sizeof(ws));
539
540 ws.inbuf = malloc(G_INBUF_SIZE);
541 ws.outbuf = malloc(G_OUTBUF_SIZE);
542 ws.reply = ws.outbuf;
543 ws.hash = (ot_hash *)ws.inbuf;
544
545 while (amount--) {
546 random_bytes(ws.hash, sizeof(ot_hash));
547 random_bytes(&ws.peer, sizeof(ws.peer));
548
549 OT_PEERFLAG(ws.peer) &= PEER_FLAG_SEEDING | PEER_FLAG_COMPLETED | PEER_FLAG_STOPPED;
550
551 add_peer_to_torrent_and_return_peers(FLAG_TCP, &ws, 1);
552 }
553
554 free(ws.inbuf);
555 free(ws.outbuf);
556}
557#endif
558
559void exerr(const char *message) {
560 fprintf(stderr, "%s\n", message);
561 exit(111);
562}
563
564void trackerlogic_init(void) {
565 g_tracker_id = random_uint32();
566
567 if (!g_stats_path)
568 g_stats_path = "stats";
569 g_stats_path_len = strlen(g_stats_path);
570
571 /* Initialise background worker threads */
572 mutex_init();
573 workqueue_init();
574 clean_init();
575 fullscrape_init();
576 accesslist_init();
577 stats_init();
578}
579
580/* Live sync reads whole packets rather than requests and wants a larger
581 inbuf than the request path does, hence the size. */
582int workstruct_init(struct ot_workstruct *ws, size_t inbuf_size) {
583 memset(ws, 0, sizeof(*ws));
584 ws->inbuf = malloc(inbuf_size);
585 ws->outbuf = malloc(G_OUTBUF_SIZE);
586#ifdef _DEBUG_HTTPERROR
587 ws->debugbuf = malloc(G_DEBUGBUF_SIZE);
588#endif
589 if (!ws->inbuf || !ws->outbuf) {
590 free(ws->inbuf);
591 free(ws->outbuf);
592#ifdef _DEBUG_HTTPERROR
593 free(ws->debugbuf);
594#endif
595 return -1;
596 }
597
598 /* Picking which peers to hand out starts here, so two workers seeded alike
599 would hand out alike */
600 random_bytes(ws->rand48_state, sizeof(ws->rand48_state));
601
602 stats_counters_register(ws);
603 return 0;
604}
605
606void workstruct_deinit(struct ot_workstruct *ws) {
607 /* The tally goes to the global before this struct goes out of scope */
608 stats_counters_retire(ws);
609
610 free(ws->inbuf);
611 free(ws->outbuf);
612#ifdef _DEBUG_HTTPERROR
613 free(ws->debugbuf);
614#endif
615}
616
617void trackerlogic_deinit(void) {
618 size_t bucket, j;
619
620 clean_deinit();
621 stats_deinit();
622 accesslist_deinit();
623 fullscrape_deinit();
624
625 /* Free all torrents... */
626 for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) {
627 ot_vector *torrents_list = mutex_bucket_lock(bucket);
628 int delta_torrentcount = 0;
629 if (torrents_list->size) {
630 for (j = 0; j < torrents_list->size; ++j) {
631 ot_torrent *torrent = ((ot_torrent *)(torrents_list->data)) + j;
632 free_peerlist(torrent->peer_list);
633 delta_torrentcount -= 1;
634 }
635 free(torrents_list->data);
636 }
637 mutex_bucket_unlock(bucket, delta_torrentcount);
638 }
639
640 /* Release mutexes */
641 workqueue_deinit();
642 mutex_deinit();
643}