diff options
Diffstat (limited to 'ot_fullscrape.c')
| -rw-r--r-- | ot_fullscrape.c | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/ot_fullscrape.c b/ot_fullscrape.c index 2ba3b24..8518bd2 100644 --- a/ot_fullscrape.c +++ b/ot_fullscrape.c | |||
| @@ -18,6 +18,7 @@ | |||
| 18 | #endif | 18 | #endif |
| 19 | 19 | ||
| 20 | /* Opentracker */ | 20 | /* Opentracker */ |
| 21 | #include "ot_fmt.h" | ||
| 21 | #include "ot_hex.h" | 22 | #include "ot_hex.h" |
| 22 | #include "ot_fullscrape.h" | 23 | #include "ot_fullscrape.h" |
| 23 | #include "ot_iovec.h" | 24 | #include "ot_iovec.h" |
| @@ -83,10 +84,15 @@ void fullscrape_deliver( int64_t sock, ot_tasktype tasktype ) { | |||
| 83 | mutex_workqueue_pushtask( sock, TASK_FULLSCRAPE | tasktype ); | 84 | mutex_workqueue_pushtask( sock, TASK_FULLSCRAPE | tasktype ); |
| 84 | } | 85 | } |
| 85 | 86 | ||
| 86 | static char * fullscrape_write_one( ot_tasktype mode, char *r, ot_torrent *torrent, ot_hash *hash ) { | 87 | static void fullscrape_write_one( ot_tasktype mode, char **cursor, ot_torrent *torrent, ot_hash *hash ) { |
| 87 | size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count; | 88 | /* Callers guarantee this much room: the chunk writer keeps its low |
| 88 | size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count; | 89 | watermark that far from the end, the compressors hand over a buffer of |
| 89 | size_t down_count = torrent->peer_list6->down_count + torrent->peer_list4->down_count; | 90 | exactly that size */ |
| 91 | char *r = *cursor; | ||
| 92 | const char *r_end = r + OT_SCRAPE_MAXENTRYLEN; | ||
| 93 | size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count; | ||
| 94 | size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count; | ||
| 95 | size_t down_count = torrent->peer_list6->down_count + torrent->peer_list4->down_count; | ||
| 90 | 96 | ||
| 91 | switch (mode & TASK_TASK_MASK) { | 97 | switch (mode & TASK_TASK_MASK) { |
| 92 | case TASK_FULLSCRAPE: | 98 | case TASK_FULLSCRAPE: |
| @@ -98,18 +104,18 @@ static char * fullscrape_write_one( ot_tasktype mode, char *r, ot_torrent *torre | |||
| 98 | memcpy(r, hash, sizeof(ot_hash)); | 104 | memcpy(r, hash, sizeof(ot_hash)); |
| 99 | r += sizeof(ot_hash); | 105 | r += sizeof(ot_hash); |
| 100 | /* push rest of the scrape string */ | 106 | /* push rest of the scrape string */ |
| 101 | r += sprintf(r, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee", seed_count, down_count, peer_count - seed_count); | 107 | fmt_append(&r, r_end, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee", seed_count, down_count, peer_count - seed_count); |
| 102 | 108 | ||
| 103 | break; | 109 | break; |
| 104 | case TASK_FULLSCRAPE_TPB_ASCII: | 110 | case TASK_FULLSCRAPE_TPB_ASCII: |
| 105 | hex_from_hash(r, *hash); | 111 | hex_from_hash(r, *hash); |
| 106 | r += 2 * sizeof(ot_hash); | 112 | r += 2 * sizeof(ot_hash); |
| 107 | r += sprintf(r, ":%zd:%zd\n", seed_count, peer_count - seed_count); | 113 | fmt_append(&r, r_end, ":%zd:%zd\n", seed_count, peer_count - seed_count); |
| 108 | break; | 114 | break; |
| 109 | case TASK_FULLSCRAPE_TPB_ASCII_PLUS: | 115 | case TASK_FULLSCRAPE_TPB_ASCII_PLUS: |
| 110 | hex_from_hash(r, *hash); | 116 | hex_from_hash(r, *hash); |
| 111 | r += 2 * sizeof(ot_hash); | 117 | r += 2 * sizeof(ot_hash); |
| 112 | r += sprintf(r, ":%zd:%zd:%zd\n", seed_count, peer_count - seed_count, down_count); | 118 | fmt_append(&r, r_end, ":%zd:%zd:%zd\n", seed_count, peer_count - seed_count, down_count); |
| 113 | break; | 119 | break; |
| 114 | case TASK_FULLSCRAPE_TPB_BINARY: | 120 | case TASK_FULLSCRAPE_TPB_BINARY: |
| 115 | memcpy(r, *hash, sizeof(ot_hash)); | 121 | memcpy(r, *hash, sizeof(ot_hash)); |
| @@ -120,15 +126,15 @@ static char * fullscrape_write_one( ot_tasktype mode, char *r, ot_torrent *torre | |||
| 120 | break; | 126 | break; |
| 121 | case TASK_FULLSCRAPE_TPB_URLENCODED: | 127 | case TASK_FULLSCRAPE_TPB_URLENCODED: |
| 122 | r += urlencoded_format(r, *hash, sizeof(ot_hash)); | 128 | r += urlencoded_format(r, *hash, sizeof(ot_hash)); |
| 123 | r += sprintf(r, ":%zd:%zd\n", seed_count, peer_count - seed_count); | 129 | fmt_append(&r, r_end, ":%zd:%zd\n", seed_count, peer_count - seed_count); |
| 124 | break; | 130 | break; |
| 125 | case TASK_FULLSCRAPE_TRACKERSTATE: | 131 | case TASK_FULLSCRAPE_TRACKERSTATE: |
| 126 | hex_from_hash(r, *hash); | 132 | hex_from_hash(r, *hash); |
| 127 | r += 2 * sizeof(ot_hash); | 133 | r += 2 * sizeof(ot_hash); |
| 128 | r += sprintf(r, ":%zd:%zd\n", torrent->peer_list6->base, down_count); | 134 | fmt_append(&r, r_end, ":%zd:%zd\n", torrent->peer_list6->base, down_count); |
| 129 | break; | 135 | break; |
| 130 | } | 136 | } |
| 131 | return r; | 137 | *cursor = r; |
| 132 | } | 138 | } |
| 133 | 139 | ||
| 134 | static void fullscrape_make(int taskid, ot_tasktype mode) { | 140 | static void fullscrape_make(int taskid, ot_tasktype mode) { |
| @@ -145,7 +151,7 @@ static void fullscrape_make(int taskid, ot_tasktype mode) { | |||
| 145 | re = r + OT_SCRAPE_CHUNK_SIZE - OT_SCRAPE_MAXENTRYLEN; | 151 | re = r + OT_SCRAPE_CHUNK_SIZE - OT_SCRAPE_MAXENTRYLEN; |
| 146 | 152 | ||
| 147 | if ((mode & TASK_TASK_MASK) == TASK_FULLSCRAPE) | 153 | if ((mode & TASK_TASK_MASK) == TASK_FULLSCRAPE) |
| 148 | r += sprintf(r, "d5:filesd"); | 154 | fmt_append(&r, (char *)iovector.iov_base + OT_SCRAPE_CHUNK_SIZE, "d5:filesd"); |
| 149 | 155 | ||
| 150 | /* For each bucket... */ | 156 | /* For each bucket... */ |
| 151 | for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) { | 157 | for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) { |
| @@ -156,7 +162,7 @@ static void fullscrape_make(int taskid, ot_tasktype mode) { | |||
| 156 | 162 | ||
| 157 | /* For each torrent in this bucket.. */ | 163 | /* For each torrent in this bucket.. */ |
| 158 | for (i = 0; i < torrents_list->size; ++i) { | 164 | for (i = 0; i < torrents_list->size; ++i) { |
| 159 | r = fullscrape_write_one(mode, r, torrents + i, &torrents[i].hash); | 165 | fullscrape_write_one(mode, &r, torrents + i, &torrents[i].hash); |
| 160 | 166 | ||
| 161 | if (r > re) { | 167 | if (r > re) { |
| 162 | iovector.iov_len = r - (char *)iovector.iov_base; | 168 | iovector.iov_len = r - (char *)iovector.iov_base; |
| @@ -187,7 +193,7 @@ static void fullscrape_make(int taskid, ot_tasktype mode) { | |||
| 187 | } | 193 | } |
| 188 | 194 | ||
| 189 | if ((mode & TASK_TASK_MASK) == TASK_FULLSCRAPE) | 195 | if ((mode & TASK_TASK_MASK) == TASK_FULLSCRAPE) |
| 190 | r += sprintf(r, "ee"); | 196 | fmt_append(&r, (char *)iovector.iov_base + OT_SCRAPE_CHUNK_SIZE, "ee"); |
| 191 | 197 | ||
| 192 | /* Send rest of data */ | 198 | /* Send rest of data */ |
| 193 | iovector.iov_len = r - (char *)iovector.iov_base; | 199 | iovector.iov_len = r - (char *)iovector.iov_base; |
| @@ -230,7 +236,8 @@ static void fullscrape_make_gzip(int taskid, ot_tasktype mode) { | |||
| 230 | /* For each torrent in this bucket.. */ | 236 | /* For each torrent in this bucket.. */ |
| 231 | for (i = 0; i < torrents_list->size; ++i) { | 237 | for (i = 0; i < torrents_list->size; ++i) { |
| 232 | char compress_buffer[OT_SCRAPE_MAXENTRYLEN]; | 238 | char compress_buffer[OT_SCRAPE_MAXENTRYLEN]; |
| 233 | r = fullscrape_write_one(mode, compress_buffer, torrents + i, &torrents[i].hash); | 239 | r = compress_buffer; |
| 240 | fullscrape_write_one(mode, &r, torrents + i, &torrents[i].hash); | ||
| 234 | strm.next_in = (uint8_t *)compress_buffer; | 241 | strm.next_in = (uint8_t *)compress_buffer; |
| 235 | strm.avail_in = r - compress_buffer; | 242 | strm.avail_in = r - compress_buffer; |
| 236 | zres = deflate(&strm, Z_NO_FLUSH); | 243 | zres = deflate(&strm, Z_NO_FLUSH); |
| @@ -359,7 +366,8 @@ static void fullscrape_make_zstd(int taskid, ot_tasktype mode) { | |||
| 359 | /* For each torrent in this bucket.. */ | 366 | /* For each torrent in this bucket.. */ |
| 360 | for (i = 0; i < torrents_list->size; ++i) { | 367 | for (i = 0; i < torrents_list->size; ++i) { |
| 361 | char compress_buffer[OT_SCRAPE_MAXENTRYLEN]; | 368 | char compress_buffer[OT_SCRAPE_MAXENTRYLEN]; |
| 362 | r = fullscrape_write_one(mode, compress_buffer, torrents + i, &torrents[i].hash); | 369 | r = compress_buffer; |
| 370 | fullscrape_write_one(mode, &r, torrents + i, &torrents[i].hash); | ||
| 363 | inbuf.src = compress_buffer; | 371 | inbuf.src = compress_buffer; |
| 364 | inbuf.size = r - compress_buffer; | 372 | inbuf.size = r - compress_buffer; |
| 365 | inbuf.pos = 0; | 373 | inbuf.pos = 0; |
