summaryrefslogtreecommitdiff
path: root/ot_fullscrape.c
blob: 6fd6d1cde30b19e1aa954c2cbb4ed57cf745926a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
   It is considered beerware. Prost. Skol. Cheers or whatever.

   $id$ */

#ifdef WANT_FULLSCRAPE

/* System */
#include <arpa/inet.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#ifdef WANT_COMPRESSION_GZIP
#include <zlib.h>
#endif
#ifdef WANT_COMPRESSION_ZSTD
#include <zstd.h>
#endif


/* Libowfat */
#include "byte.h"
#include "io.h"
#include "textcode.h"

/* Opentracker */
#include "ot_fullscrape.h"
#include "ot_iovec.h"
#include "ot_mutex.h"
#include "trackerlogic.h"

/* Fetch full scrape info for all torrents
   Full scrapes usually are huge and one does not want to
   allocate more memory. So lets get them in 512k units
*/
#define OT_SCRAPE_CHUNK_SIZE  (1024 * 1024)

/* "d8:completei%zde10:downloadedi%zde10:incompletei%zdee" */
#define OT_SCRAPE_MAXENTRYLEN 256

/* Forward declaration */
static void fullscrape_make(int taskid, ot_tasktype mode);
#ifdef WANT_COMPRESSION_GZIP
static void fullscrape_make_gzip(int taskid, ot_tasktype mode);
#endif
#ifdef WANT_COMPRESSION_ZSTD
static void fullscrape_make_zstd(int taskid, ot_tasktype mode);
#endif

/* Converter function from memory to human readable hex strings
   XXX - Duplicated from ot_stats. Needs fix. */
static char *to_hex(char *d, uint8_t *s) {
  char *m = "0123456789ABCDEF";
  char *t = d;
  char *e = d + 40;
  while (d < e) {
    *d++ = m[*s >> 4];
    *d++ = m[*s++ & 15];
  }
  *d = 0;
  return t;
}

/* This is the entry point into this worker thread
   It grabs tasks from mutex_tasklist and delivers results back
*/
static void *fullscrape_worker(void *args) {
  (void)args;

  while (g_opentracker_running) {
    ot_tasktype tasktype = TASK_FULLSCRAPE;
    ot_taskid   taskid   = mutex_workqueue_poptask(&tasktype);
#ifdef WANT_COMPRESSION_ZSTD
    if (tasktype & TASK_FLAG_ZSTD)
      fullscrape_make_zstd(taskid, tasktype);
    else
#endif
#ifdef WANT_COMPRESSION_GZIP
    if (tasktype & TASK_FLAG_GZIP)
      fullscrape_make_gzip(taskid, tasktype);
    else
#endif
      fullscrape_make(taskid, tasktype);
    mutex_workqueue_pushchunked(taskid, NULL);
  }
  return NULL;
}

static pthread_t thread_id;
void fullscrape_init( ) {
  pthread_create( &thread_id, NULL, fullscrape_worker, NULL );
}

void fullscrape_deinit( ) {
  pthread_cancel( thread_id );
}

void fullscrape_deliver( int64 sock, ot_tasktype tasktype ) {
  mutex_workqueue_pushtask( sock, tasktype );
}

static char * fullscrape_write_one( ot_tasktype mode, char *r, ot_torrent *torrent, ot_hash *hash ) {
  size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count;
  size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count;
  size_t down_count = torrent->peer_list6->down_count + torrent->peer_list4->down_count;

  switch (mode & TASK_TASK_MASK) {
  case TASK_FULLSCRAPE:
  default:
    /* push hash as bencoded string */
    *r++ = '2';
    *r++ = '0';
    *r++ = ':';
    memcpy(r, hash, sizeof(ot_hash));
    r += sizeof(ot_hash);
    /* push rest of the scrape string */
    r += sprintf(r, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee", seed_count, down_count, peer_count - seed_count);

    break;
  case TASK_FULLSCRAPE_TPB_ASCII:
    to_hex(r, *hash);
    r += 2 * sizeof(ot_hash);
    r += sprintf(r, ":%zd:%zd\n", seed_count, peer_count - seed_count);
    break;
  case TASK_FULLSCRAPE_TPB_ASCII_PLUS:
    to_hex(r, *hash);
    r += 2 * sizeof(ot_hash);
    r += sprintf(r, ":%zd:%zd:%zd\n", seed_count, peer_count - seed_count, down_count);
    break;
  case TASK_FULLSCRAPE_TPB_BINARY:
    memcpy(r, *hash, sizeof(ot_hash));
    r                    += sizeof(ot_hash);
    *(uint32_t *)(r + 0)  = htonl((uint32_t)seed_count);
    *(uint32_t *)(r + 4)  = htonl((uint32_t)(peer_count - seed_count));
    r                    += 8;
    break;
  case TASK_FULLSCRAPE_TPB_URLENCODED:
    r += fmt_urlencoded(r, (char *)*hash, 20);
    r += sprintf(r, ":%zd:%zd\n", seed_count, peer_count - seed_count);
    break;
  case TASK_FULLSCRAPE_TRACKERSTATE:
    to_hex(r, *hash);
    r += 2 * sizeof(ot_hash);
    r += sprintf(r, ":%zd:%zd\n", torrent->peer_list6->base, down_count);
    break;
  }
  return r;
}

static void fullscrape_make(int taskid, ot_tasktype mode) {
  int          bucket;
  char        *r, *re;
  struct iovec iovector = {NULL, 0};

  /* Setup return vector... */
  r = iovector.iov_base = malloc(OT_SCRAPE_CHUNK_SIZE);
  if (!r)
    return;

  /* re points to low watermark */
  re = r + OT_SCRAPE_CHUNK_SIZE - OT_SCRAPE_MAXENTRYLEN;

  if ((mode & TASK_TASK_MASK) == TASK_FULLSCRAPE)
    r += sprintf(r, "d5:filesd");

  /* For each bucket... */
  for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) {
    /* Get exclusive access to that bucket */
    ot_vector  *torrents_list = mutex_bucket_lock(bucket);
    ot_torrent *torrents      = (ot_torrent *)(torrents_list->data);
    size_t      i;

    /* For each torrent in this bucket.. */
    for (i = 0; i < torrents_list->size; ++i) {
      r = fullscrape_write_one(mode, r, torrents + i, &torrents[i].hash);

      if (r > re) {
        iovector.iov_len = r - (char *)iovector.iov_base;

        if (mutex_workqueue_pushchunked(taskid, &iovector)) {
          free(iovector.iov_base);
          return mutex_bucket_unlock(bucket, 0);
        }
        /* Allocate a fresh output buffer */
        r = iovector.iov_base = malloc(OT_SCRAPE_CHUNK_SIZE);
        if (!r)
          return mutex_bucket_unlock(bucket, 0);

        /* re points to low watermark */
        re = r + OT_SCRAPE_CHUNK_SIZE - OT_SCRAPE_MAXENTRYLEN;
      }
    }

    /* All torrents done: release lock on current bucket */
    mutex_bucket_unlock(bucket, 0);

    /* Parent thread died? */
    if (!g_opentracker_running)
      return;
  }

  if ((mode & TASK_TASK_MASK) == TASK_FULLSCRAPE)
    r += sprintf(r, "ee");

  /* Send rest of data */
  iovector.iov_len = r - (char *)iovector.iov_base;
  if (mutex_workqueue_pushchunked(taskid, &iovector))
    free(iovector.iov_base);
}

#ifdef WANT_COMPRESSION_GZIP

static void fullscrape_make_gzip(int taskid, ot_tasktype mode) {
  int          bucket;
  char        *r;
  struct iovec iovector = {NULL, 0};
  int          zres;
  z_stream     strm;
  /* Setup return vector... */
  iovector.iov_base = malloc(OT_SCRAPE_CHUNK_SIZE);
  if (!iovector.iov_base)
    return;

  byte_zero(&strm, sizeof(strm));
  strm.next_out  = (uint8_t *)iovector.iov_base;
  strm.avail_out = OT_SCRAPE_CHUNK_SIZE;
  if (deflateInit2(&strm, 7, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY) != Z_OK)
    fprintf(stderr, "not ok.\n");

  if ((mode & TASK_TASK_MASK) == TASK_FULLSCRAPE) {
    strm.next_in  = (uint8_t *)"d5:filesd";
    strm.avail_in = strlen("d5:filesd");
    zres          = deflate(&strm, Z_NO_FLUSH);
  }

  /* For each bucket... */
  for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) {
    /* Get exclusive access to that bucket */
    ot_vector  *torrents_list = mutex_bucket_lock(bucket);
    ot_torrent *torrents      = (ot_torrent *)(torrents_list->data);
    size_t      i;

    /* For each torrent in this bucket.. */
    for (i = 0; i < torrents_list->size; ++i) {
      char compress_buffer[OT_SCRAPE_MAXENTRYLEN];
      r             = fullscrape_write_one(mode, compress_buffer, torrents + i, &torrents[i].hash);
      strm.next_in  = (uint8_t *)compress_buffer;
      strm.avail_in = r - compress_buffer;
      zres          = deflate(&strm, Z_NO_FLUSH);
      if ((zres < Z_OK) && (zres != Z_BUF_ERROR))
        fprintf(stderr, "deflate() failed while in fullscrape_make().\n");

      /* Check if there still is enough buffer left */
      while (!strm.avail_out) {
        iovector.iov_len = (char *)strm.next_out - (char *)iovector.iov_base;

        if (mutex_workqueue_pushchunked(taskid, &iovector)) {
          free(iovector.iov_base);
          return mutex_bucket_unlock(bucket, 0);
        }
        /* Allocate a fresh output buffer */
        iovector.iov_base = malloc(OT_SCRAPE_CHUNK_SIZE);
        if (!iovector.iov_base) {
          fprintf(stderr, "Out of memory trying to claim ouput buffer\n");
          deflateEnd(&strm);
          return mutex_bucket_unlock(bucket, 0);
        }
        strm.next_out  = (uint8_t *)iovector.iov_base;
        strm.avail_out = OT_SCRAPE_CHUNK_SIZE;
        zres           = deflate(&strm, Z_NO_FLUSH);
        if ((zres < Z_OK) && (zres != Z_BUF_ERROR))
          fprintf(stderr, "deflate() failed while in fullscrape_make().\n");
      }
    }

    /* All torrents done: release lock on current bucket */
    mutex_bucket_unlock(bucket, 0);

    /* Parent thread died? */
    if (!g_opentracker_running) {
      deflateEnd(&strm);
      return;
    }
  }

  if ((mode & TASK_TASK_MASK) == TASK_FULLSCRAPE) {
    strm.next_in  = (uint8_t *)"ee";
    strm.avail_in = strlen("ee");
  }

  if (deflate(&strm, Z_FINISH) < Z_OK)
    fprintf(stderr, "deflate() failed while in fullscrape_make()'s endgame.\n");

  iovector.iov_len = (char *)strm.next_out - (char *)iovector.iov_base;
  if (mutex_workqueue_pushchunked(taskid, &iovector)) {
    free(iovector.iov_base);
    deflateEnd(&strm);
    return;
  }

  /* Check if there's a last batch of data in the zlib buffer */
  if (!strm.avail_out) {
      /* Allocate a fresh output buffer */
      iovector.iov_base = malloc(OT_SCRAPE_CHUNK_SIZE);

      if (!iovector.iov_base) {
        fprintf(stderr, "Problem with iovec_fix_increase_or_free\n");
        deflateEnd(&strm);
        return;
      }
      strm.next_out  = iovector.iov_base;
      strm.avail_out = OT_SCRAPE_CHUNK_SIZE;
      if (deflate(&strm, Z_FINISH) < Z_OK)
        fprintf(stderr, "deflate() failed while in fullscrape_make()'s endgame.\n");

      /* Only pass the new buffer if there actually was some data left in the buffer */
      iovector.iov_len = (char *)strm.next_out - (char *)iovector.iov_base;
      if (!iovector.iov_len || mutex_workqueue_pushchunked(taskid, &iovector))
        free(iovector.iov_base);
  }

  deflateEnd(&strm);
}
/* WANT_COMPRESSION_GZIP */
#endif

#ifdef WANT_COMPRESSION_ZSTD

static void fullscrape_make_zstd(int taskid, ot_tasktype mode) {
  int            bucket;
  char          *r;
  struct iovec   iovector = {NULL, 0};
  ZSTD_CCtx     *zstream = ZSTD_createCCtx();
  ZSTD_inBuffer  inbuf;
  ZSTD_outBuffer outbuf;
  size_t         more_bytes;

  if (!zstream)
    return;

  /* Setup return vector... */
  iovector.iov_base = malloc(OT_SCRAPE_CHUNK_SIZE);
  if (!iovector.iov_base) {
    ZSTD_freeCCtx(zstream);
    return;
  }

  /* Working with a compression level 6 is half as fast as level 3, but
     seems to be the last reasonable bump that's worth extra cpu */
  ZSTD_CCtx_setParameter(zstream, ZSTD_c_compressionLevel, 6);

  outbuf.dst  = iovector.iov_base;
  outbuf.size = OT_SCRAPE_CHUNK_SIZE;
  outbuf.pos  = 0;

  if ((mode & TASK_TASK_MASK) == TASK_FULLSCRAPE) {
    inbuf.src  = (const void *)"d5:filesd";
    inbuf.size = strlen("d5:filesd");
    inbuf.pos  = 0;
    ZSTD_compressStream2(zstream, &outbuf, &inbuf, ZSTD_e_continue);
  }

  /* For each bucket... */
  for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) {
    /* Get exclusive access to that bucket */
    ot_vector  *torrents_list = mutex_bucket_lock(bucket);
    ot_torrent *torrents      = (ot_torrent *)(torrents_list->data);
    size_t      i;

    /* For each torrent in this bucket.. */
    for (i = 0; i < torrents_list->size; ++i) {
      char compress_buffer[OT_SCRAPE_MAXENTRYLEN];
      r             = fullscrape_write_one(mode, compress_buffer, torrents + i, &torrents[i].hash);
      inbuf.src     = compress_buffer;
      inbuf.size    = r - compress_buffer;
      inbuf.pos     = 0;
      ZSTD_compressStream2(zstream, &outbuf, &inbuf, ZSTD_e_continue);

      /* Check if there still is enough buffer left */
      while (outbuf.pos + OT_SCRAPE_MAXENTRYLEN > outbuf.size) {
        iovector.iov_len = outbuf.size;

        if (mutex_workqueue_pushchunked(taskid, &iovector)) {
          free(iovector.iov_base);
          ZSTD_freeCCtx(zstream);
          return mutex_bucket_unlock(bucket, 0);
        }
        /* Allocate a fresh output buffer */
        iovector.iov_base = malloc(OT_SCRAPE_CHUNK_SIZE);
        if (!iovector.iov_base) {
          fprintf(stderr, "Out of memory trying to claim ouput buffer\n");
          ZSTD_freeCCtx(zstream);
          return mutex_bucket_unlock(bucket, 0);
        }

        outbuf.dst  = iovector.iov_base;
        outbuf.size = OT_SCRAPE_CHUNK_SIZE;
        outbuf.pos  = 0;

        ZSTD_compressStream2(zstream, &outbuf, &inbuf, ZSTD_e_continue);
      }
    }

    /* All torrents done: release lock on current bucket */
    mutex_bucket_unlock(bucket, 0);

    /* Parent thread died? */
    if (!g_opentracker_running)
      return;
  }

  if ((mode & TASK_TASK_MASK) == TASK_FULLSCRAPE) {
    inbuf.src  = (const void *)"ee";
    inbuf.size = strlen("ee");
    inbuf.pos  = 0;
  }

  more_bytes = ZSTD_compressStream2(zstream, &outbuf, &inbuf, ZSTD_e_end);

  iovector.iov_len = outbuf.pos;
  if (mutex_workqueue_pushchunked(taskid, &iovector)) {
    free(iovector.iov_base);
    ZSTD_freeCCtx(zstream);
    return;
  }

  /* Check if there's a last batch of data in the zlib buffer */
  if (more_bytes) {
      /* Allocate a fresh output buffer */
      iovector.iov_base = malloc(OT_SCRAPE_CHUNK_SIZE);

      if (!iovector.iov_base) {
        fprintf(stderr, "Problem with iovec_fix_increase_or_free\n");
        ZSTD_freeCCtx(zstream);
        return;
      }

      outbuf.dst  = iovector.iov_base;
      outbuf.size = OT_SCRAPE_CHUNK_SIZE;
      outbuf.pos  = 0;

      ZSTD_compressStream2(zstream, &outbuf, &inbuf, ZSTD_e_end);

      /* Only pass the new buffer if there actually was some data left in the buffer */
      iovector.iov_len = outbuf.pos;
      if (!iovector.iov_len || mutex_workqueue_pushchunked(taskid, &iovector))
        free(iovector.iov_base);
  }

  ZSTD_freeCCtx(zstream);
}
/* WANT_COMPRESSION_ZSTD */
#endif

/* WANT_FULLSCRAPE */
#endif