summaryrefslogtreecommitdiff
path: root/ot_fullscrape.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_fullscrape.c')
-rw-r--r--ot_fullscrape.c43
1 files changed, 17 insertions, 26 deletions
diff --git a/ot_fullscrape.c b/ot_fullscrape.c
index b800431..3d0e2f1 100644
--- a/ot_fullscrape.c
+++ b/ot_fullscrape.c
@@ -10,7 +10,6 @@
10#include <pthread.h> 10#include <pthread.h>
11#include <stdio.h> 11#include <stdio.h>
12#include <string.h> 12#include <string.h>
13#include <sys/param.h>
14#ifdef WANT_COMPRESSION_GZIP 13#ifdef WANT_COMPRESSION_GZIP
15#include <zlib.h> 14#include <zlib.h>
16#endif 15#endif
@@ -18,16 +17,15 @@
18#include <zstd.h> 17#include <zstd.h>
19#endif 18#endif
20 19
21
22/* Libowfat */ 20/* Libowfat */
23#include "byte.h"
24#include "io.h" 21#include "io.h"
25#include "textcode.h"
26 22
27/* Opentracker */ 23/* Opentracker */
24#include "ot_hex.h"
28#include "ot_fullscrape.h" 25#include "ot_fullscrape.h"
29#include "ot_iovec.h" 26#include "ot_iovec.h"
30#include "ot_mutex.h" 27#include "ot_mutex.h"
28#include "scan_urlencoded_query.h"
31#include "trackerlogic.h" 29#include "trackerlogic.h"
32 30
33/* Fetch full scrape info for all torrents 31/* Fetch full scrape info for all torrents
@@ -50,18 +48,6 @@ static void fullscrape_make_zstd(int taskid, ot_tasktype mode);
50 48
51/* Converter function from memory to human readable hex strings 49/* Converter function from memory to human readable hex strings
52 XXX - Duplicated from ot_stats. Needs fix. */ 50 XXX - Duplicated from ot_stats. Needs fix. */
53static char *to_hex(char *d, uint8_t *s) {
54 char *m = "0123456789ABCDEF";
55 char *t = d;
56 char *e = d + 40;
57 while (d < e) {
58 *d++ = m[*s >> 4];
59 *d++ = m[*s++ & 15];
60 }
61 *d = 0;
62 return t;
63}
64
65/* This is the entry point into this worker thread 51/* This is the entry point into this worker thread
66 It grabs tasks from mutex_tasklist and delivers results back 52 It grabs tasks from mutex_tasklist and delivers results back
67*/ 53*/
@@ -119,12 +105,12 @@ static char * fullscrape_write_one( ot_tasktype mode, char *r, ot_torrent *torre
119 105
120 break; 106 break;
121 case TASK_FULLSCRAPE_TPB_ASCII: 107 case TASK_FULLSCRAPE_TPB_ASCII:
122 to_hex(r, *hash); 108 hex_fromhash(r, *hash);
123 r += 2 * sizeof(ot_hash); 109 r += 2 * sizeof(ot_hash);
124 r += sprintf(r, ":%zd:%zd\n", seed_count, peer_count - seed_count); 110 r += sprintf(r, ":%zd:%zd\n", seed_count, peer_count - seed_count);
125 break; 111 break;
126 case TASK_FULLSCRAPE_TPB_ASCII_PLUS: 112 case TASK_FULLSCRAPE_TPB_ASCII_PLUS:
127 to_hex(r, *hash); 113 hex_fromhash(r, *hash);
128 r += 2 * sizeof(ot_hash); 114 r += 2 * sizeof(ot_hash);
129 r += sprintf(r, ":%zd:%zd:%zd\n", seed_count, peer_count - seed_count, down_count); 115 r += sprintf(r, ":%zd:%zd:%zd\n", seed_count, peer_count - seed_count, down_count);
130 break; 116 break;
@@ -136,11 +122,11 @@ static char * fullscrape_write_one( ot_tasktype mode, char *r, ot_torrent *torre
136 r += 8; 122 r += 8;
137 break; 123 break;
138 case TASK_FULLSCRAPE_TPB_URLENCODED: 124 case TASK_FULLSCRAPE_TPB_URLENCODED:
139 r += fmt_urlencoded(r, (char *)*hash, 20); 125 r += fmt_urlencoded(r, *hash, sizeof(ot_hash));
140 r += sprintf(r, ":%zd:%zd\n", seed_count, peer_count - seed_count); 126 r += sprintf(r, ":%zd:%zd\n", seed_count, peer_count - seed_count);
141 break; 127 break;
142 case TASK_FULLSCRAPE_TRACKERSTATE: 128 case TASK_FULLSCRAPE_TRACKERSTATE:
143 to_hex(r, *hash); 129 hex_fromhash(r, *hash);
144 r += 2 * sizeof(ot_hash); 130 r += 2 * sizeof(ot_hash);
145 r += sprintf(r, ":%zd:%zd\n", torrent->peer_list6->base, down_count); 131 r += sprintf(r, ":%zd:%zd\n", torrent->peer_list6->base, down_count);
146 break; 132 break;
@@ -180,12 +166,15 @@ static void fullscrape_make(int taskid, ot_tasktype mode) {
180 166
181 if (mutex_workqueue_pushchunked(taskid, &iovector)) { 167 if (mutex_workqueue_pushchunked(taskid, &iovector)) {
182 free(iovector.iov_base); 168 free(iovector.iov_base);
183 return mutex_bucket_unlock(bucket, 0); 169 mutex_bucket_unlock(bucket, 0);
170 return;
184 } 171 }
185 /* Allocate a fresh output buffer */ 172 /* Allocate a fresh output buffer */
186 r = iovector.iov_base = malloc(OT_SCRAPE_CHUNK_SIZE); 173 r = iovector.iov_base = malloc(OT_SCRAPE_CHUNK_SIZE);
187 if (!r) 174 if (!r) {
188 return mutex_bucket_unlock(bucket, 0); 175 mutex_bucket_unlock(bucket, 0);
176 return;
177 }
189 178
190 /* re points to low watermark */ 179 /* re points to low watermark */
191 re = r + OT_SCRAPE_CHUNK_SIZE - OT_SCRAPE_MAXENTRYLEN; 180 re = r + OT_SCRAPE_CHUNK_SIZE - OT_SCRAPE_MAXENTRYLEN;
@@ -222,7 +211,7 @@ static void fullscrape_make_gzip(int taskid, ot_tasktype mode) {
222 if (!iovector.iov_base) 211 if (!iovector.iov_base)
223 return; 212 return;
224 213
225 byte_zero(&strm, sizeof(strm)); 214 memset(&strm, 0, sizeof(strm));
226 strm.next_out = (uint8_t *)iovector.iov_base; 215 strm.next_out = (uint8_t *)iovector.iov_base;
227 strm.avail_out = OT_SCRAPE_CHUNK_SIZE; 216 strm.avail_out = OT_SCRAPE_CHUNK_SIZE;
228 if (deflateInit2(&strm, 7, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY) != Z_OK) 217 if (deflateInit2(&strm, 7, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY) != Z_OK)
@@ -257,14 +246,16 @@ static void fullscrape_make_gzip(int taskid, ot_tasktype mode) {
257 246
258 if (mutex_workqueue_pushchunked(taskid, &iovector)) { 247 if (mutex_workqueue_pushchunked(taskid, &iovector)) {
259 free(iovector.iov_base); 248 free(iovector.iov_base);
260 return mutex_bucket_unlock(bucket, 0); 249 mutex_bucket_unlock(bucket, 0);
250 return;
261 } 251 }
262 /* Allocate a fresh output buffer */ 252 /* Allocate a fresh output buffer */
263 iovector.iov_base = malloc(OT_SCRAPE_CHUNK_SIZE); 253 iovector.iov_base = malloc(OT_SCRAPE_CHUNK_SIZE);
264 if (!iovector.iov_base) { 254 if (!iovector.iov_base) {
265 fprintf(stderr, "Out of memory trying to claim ouput buffer\n"); 255 fprintf(stderr, "Out of memory trying to claim ouput buffer\n");
266 deflateEnd(&strm); 256 deflateEnd(&strm);
267 return mutex_bucket_unlock(bucket, 0); 257 mutex_bucket_unlock(bucket, 0);
258 return;
268 } 259 }
269 strm.next_out = (uint8_t *)iovector.iov_base; 260 strm.next_out = (uint8_t *)iovector.iov_base;
270 strm.avail_out = OT_SCRAPE_CHUNK_SIZE; 261 strm.avail_out = OT_SCRAPE_CHUNK_SIZE;