summaryrefslogtreecommitdiff
path: root/ot_fullscrape.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_fullscrape.c')
-rw-r--r--ot_fullscrape.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/ot_fullscrape.c b/ot_fullscrape.c
new file mode 100644
index 0000000..0571049
--- /dev/null
+++ b/ot_fullscrape.c
@@ -0,0 +1,67 @@
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 <sys/uio.h>
6#include <stdio.h>
7#include <string.h>
8
9/* Libowfat */
10
11/* Opentracker */
12#include "trackerlogic.h"
13#include "ot_mutex.h"
14#include "ot_iovec.h"
15#include "ot_fullscrape.h"
16
17/* Fetch full scrape info for all torrents
18 Full scrapes usually are huge and one does not want to
19 allocate more memory. So lets get them in 1M units
20*/
21#define OT_SCRAPE_CHUNK_SIZE (256*1024)
22
23/* "d8:completei%zde10:downloadedi%zde10:incompletei%zdee" */
24#define OT_FULLSCRAPE_MAXENTRYLEN 100
25
26size_t return_fullscrape_for_tracker( int *iovec_entries, struct iovec **iovector ) {
27 int bucket;
28 size_t j;
29 char *r, *re;
30
31 *iovec_entries = 0;
32 if( !( r = iovec_increase( iovec_entries, iovector, OT_SCRAPE_CHUNK_SIZE ) ) )
33 return 0;
34 re = r + OT_SCRAPE_CHUNK_SIZE;
35
36 memmove( r, "d5:filesd", 9 ); r += 9;
37 for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) {
38 ot_vector *torrents_list = mutex_bucket_lock( bucket );
39 for( j=0; j<torrents_list->size; ++j ) {
40 ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[j] ).peer_list;
41 ot_hash *hash =&( ((ot_torrent*)(torrents_list->data))[j] ).hash;
42 if( peer_list->peer_count || peer_list->down_count ) {
43 *r++='2'; *r++='0'; *r++=':';
44 memmove( r, hash, 20 ); r+=20;
45 r += sprintf( r, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee", peer_list->seed_count, peer_list->down_count, peer_list->peer_count-peer_list->seed_count );
46 }
47
48 if( re - r <= OT_FULLSCRAPE_MAXENTRYLEN ) {
49 iovec_fixlast( iovec_entries, iovector, OT_SCRAPE_CHUNK_SIZE - ( re - r ) );
50 if( !( r = iovec_increase( iovec_entries, iovector, OT_SCRAPE_CHUNK_SIZE ) ) ) {
51 iovec_free( iovec_entries, iovector );
52 mutex_bucket_unlock( bucket );
53 return 0;
54 }
55 re = r + OT_SCRAPE_CHUNK_SIZE;
56 }
57
58 }
59 mutex_bucket_unlock( bucket );
60 }
61
62 *r++='e'; *r++='e';
63
64 iovec_fixlast( iovec_entries, iovector, OT_SCRAPE_CHUNK_SIZE - ( re - r ) );
65
66 return iovec_length( iovec_entries, iovector );
67}