summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2026-11-28 19:46:59 +0100
committerDirk Engling <erdgeist@erdgeist.org>2026-11-28 19:46:59 +0100
commitadc518a2063ee78e4217a8aba51b116af1af42f4 (patch)
treee5099842d850fffaef5bfd959822eeaa22599f55 /src
parentcc7482269feb12c275a156efd42dd6a3d6cb189d (diff)
Add helper to pack uint32 in network byte order
Diffstat (limited to 'src')
-rw-r--r--src/ot_fullscrape.c5
-rw-r--r--src/ot_udp.c7
-rw-r--r--src/trackerlogic.c45
-rw-r--r--src/trackerlogic.h10
4 files changed, 38 insertions, 29 deletions
diff --git a/src/ot_fullscrape.c b/src/ot_fullscrape.c
index 0cd1910..3e3b6cb 100644
--- a/src/ot_fullscrape.c
+++ b/src/ot_fullscrape.c
@@ -121,9 +121,8 @@ static void fullscrape_write_one( ot_tasktype mode, char **cursor, ot_torrent *t
121 case TASK_FULLSCRAPE_TPB_BINARY: 121 case TASK_FULLSCRAPE_TPB_BINARY:
122 memcpy(r, *hash, sizeof(ot_hash)); 122 memcpy(r, *hash, sizeof(ot_hash));
123 r += sizeof(ot_hash); 123 r += sizeof(ot_hash);
124 *(uint32_t *)(r + 0) = htonl((uint32_t)seed_count); 124 pack_uint32(&r, (uint32_t)seed_count);
125 *(uint32_t *)(r + 4) = htonl((uint32_t)(peer_count - seed_count)); 125 pack_uint32(&r, (uint32_t)(peer_count - seed_count));
126 r += 8;
127 break; 126 break;
128 case TASK_FULLSCRAPE_TPB_URLENCODED: 127 case TASK_FULLSCRAPE_TPB_URLENCODED:
129 r += urlencoded_format(r, *hash, sizeof(ot_hash)); 128 r += urlencoded_format(r, *hash, sizeof(ot_hash));
diff --git a/src/ot_udp.c b/src/ot_udp.c
index 2918c25..7a05b61 100644
--- a/src/ot_udp.c
+++ b/src/ot_udp.c
@@ -204,11 +204,9 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) {
204 if (!left) 204 if (!left)
205 OT_PEERFLAG(ws->peer) |= PEER_FLAG_SEEDING; 205 OT_PEERFLAG(ws->peer) |= PEER_FLAG_SEEDING;
206 206
207 reply = udp_reply_header(ws->outbuf, UDP_ACTION_ANNOUNCE, request); 207 ws->reply = udp_reply_header(ws->outbuf, UDP_ACTION_ANNOUNCE, request);
208 208
209 if (OT_PEERFLAG(ws->peer) & PEER_FLAG_STOPPED) { /* Peer is gone. */ 209 if (OT_PEERFLAG(ws->peer) & PEER_FLAG_STOPPED) { /* Peer is gone. */
210 /* This one writes the whole reply, and leaves the header alone */
211 ws->reply = ws->outbuf;
212 ws->reply_size = remove_peer_from_torrent(FLAG_UDP, ws); 210 ws->reply_size = remove_peer_from_torrent(FLAG_UDP, ws);
213 } else { 211 } else {
214 /* List of peers should fit in a single UDP packet */ 212 /* List of peers should fit in a single UDP packet */
@@ -216,7 +214,6 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) {
216 if (numwant > max_peers) 214 if (numwant > max_peers)
217 numwant = max_peers; 215 numwant = max_peers;
218 216
219 ws->reply = reply;
220 ws->reply_size = add_peer_to_torrent_and_return_peers(FLAG_UDP, ws, numwant); 217 ws->reply_size = add_peer_to_torrent_and_return_peers(FLAG_UDP, ws, numwant);
221 218
222 /* The announce path answers a refusal itself, so nothing is left here 219 /* The announce path answers a refusal itself, so nothing is left here
@@ -225,8 +222,8 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) {
225 udp_send_error(serversocket, ws, "Tracker is out of resources, try again later.", remoteip, remoteport); 222 udp_send_error(serversocket, ws, "Tracker is out of resources, try again later.", remoteip, remoteport);
226 return 1; 223 return 1;
227 } 224 }
228 ws->reply_size += UDP_REPLY_HEADER_SIZE;
229 } 225 }
226 ws->reply_size += UDP_REPLY_HEADER_SIZE;
230 227
231 socket_send(serversocket, ws->outbuf, ws->reply_size, remoteip, remoteport); 228 socket_send(serversocket, ws->outbuf, ws->reply_size, remoteip, remoteport);
232 stats_issue_event(ws, EVENT_ANNOUNCE, FLAG_UDP, ws->reply_size); 229 stats_issue_event(ws, EVENT_ANNOUNCE, FLAG_UDP, ws->reply_size);
diff --git a/src/trackerlogic.c b/src/trackerlogic.c
index 1f56a2c..0b032e1 100644
--- a/src/trackerlogic.c
+++ b/src/trackerlogic.c
@@ -94,10 +94,11 @@ size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstru
94 return strlen(invalid_hash); 94 return strlen(invalid_hash);
95 } 95 }
96 if (proto == FLAG_UDP) { 96 if (proto == FLAG_UDP) {
97 ((uint32_t *)ws->reply)[0] = htonl(OT_CLIENT_REJECT_INTERVAL_RANDOM); 97 char *r = ws->reply;
98 ((uint32_t *)ws->reply)[1] = 0; 98 pack_uint32(&r, OT_CLIENT_REJECT_INTERVAL_RANDOM);
99 ((uint32_t *)ws->reply)[2] = 0; 99 pack_uint32(&r, 0);
100 return 12; 100 pack_uint32(&r, 0);
101 return r - ws->reply;
101 } 102 }
102 return 0; 103 return 0;
103 } 104 }
@@ -297,10 +298,9 @@ static size_t return_peers_for_torrent_udp(struct ot_workstruct *ws, ot_torrent
297 if (amount > list_count) 298 if (amount > list_count)
298 amount = list_count; 299 amount = list_count;
299 300
300 *(uint32_t *)(r + 0) = htonl(OT_CLIENT_REQUEST_INTERVAL_RANDOM); 301 pack_uint32(&r, OT_CLIENT_REQUEST_INTERVAL_RANDOM);
301 *(uint32_t *)(r + 4) = htonl(peer_count - seed_count); 302 pack_uint32(&r, peer_count - seed_count);
302 *(uint32_t *)(r + 8) = htonl(seed_count); 303 pack_uint32(&r, seed_count);
303 r += 12;
304 304
305 if (amount) { 305 if (amount) {
306 if (amount == list_count) 306 if (amount == list_count)
@@ -400,23 +400,25 @@ size_t return_udp_scrape_for_torrent(ot_hash const hash, char *reply) {
400 ot_vector *torrents_list = mutex_bucket_lock_by_hash(hash); 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); 401 ot_torrent *torrent = vector_binary_search(hash, torrents_list->data, torrents_list->size, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch);
402 402
403 if (!exactmatch) { 403 uint32_t seeders = 0, completed = 0, leechers = 0;
404 memset(reply, 0, 12); 404 char *r = reply;
405 } else {
406 uint32_t *r = (uint32_t *)reply;
407 405
406 if (exactmatch) {
408 if (clean_single_torrent(torrent)) { 407 if (clean_single_torrent(torrent)) {
409 vector_remove_torrent(torrents_list, torrent); 408 vector_remove_torrent(torrents_list, torrent);
410 memset(reply, 0, 12);
411 delta_torrentcount = -1; 409 delta_torrentcount = -1;
412 } else { 410 } else {
413 r[0] = htonl(torrent_seed_count(torrent)); 411 seeders = torrent_seed_count(torrent);
414 r[1] = htonl(torrent->down_count); 412 completed = torrent->down_count;
415 r[2] = htonl(torrent_peer_count(torrent) - torrent_seed_count(torrent)); 413 leechers = torrent_peer_count(torrent) - torrent_seed_count(torrent);
416 } 414 }
417 } 415 }
418 mutex_bucket_unlock_by_hash(hash, delta_torrentcount); 416 mutex_bucket_unlock_by_hash(hash, delta_torrentcount);
419 return 12; 417
418 pack_uint32(&r, seeders);
419 pack_uint32(&r, completed);
420 pack_uint32(&r, leechers);
421 return r - reply;
420} 422}
421 423
422/* Fetches scrape info for a specific torrent */ 424/* Fetches scrape info for a specific torrent */
@@ -485,10 +487,11 @@ size_t remove_peer_from_torrent(PROTO_FLAG proto, struct ot_workstruct *ws) {
485 487
486 /* Handle UDP reply */ 488 /* Handle UDP reply */
487 if (proto == FLAG_UDP) { 489 if (proto == FLAG_UDP) {
488 ((uint32_t *)ws->reply)[2] = htonl(OT_CLIENT_REQUEST_INTERVAL_RANDOM); 490 char *r = ws->reply;
489 ((uint32_t *)ws->reply)[3] = htonl(peer_count - seed_count); 491 pack_uint32(&r, OT_CLIENT_REQUEST_INTERVAL_RANDOM);
490 ((uint32_t *)ws->reply)[4] = htonl(seed_count); 492 pack_uint32(&r, peer_count - seed_count);
491 ws->reply_size = 20; 493 pack_uint32(&r, seed_count);
494 ws->reply_size = r - ws->reply;
492 } 495 }
493 496
494 mutex_bucket_unlock_by_hash(*ws->hash, 0); 497 mutex_bucket_unlock_by_hash(*ws->hash, 0);
diff --git a/src/trackerlogic.h b/src/trackerlogic.h
index 4086b23..a05c790 100644
--- a/src/trackerlogic.h
+++ b/src/trackerlogic.h
@@ -4,10 +4,12 @@
4#ifndef OT_TRACKERLOGIC_H__ 4#ifndef OT_TRACKERLOGIC_H__
5#define OT_TRACKERLOGIC_H__ 5#define OT_TRACKERLOGIC_H__
6 6
7#include <arpa/inet.h>
7#include <signal.h> 8#include <signal.h>
8#include <stdatomic.h> 9#include <stdatomic.h>
9#include <stdint.h> 10#include <stdint.h>
10#include <stdlib.h> 11#include <stdlib.h>
12#include <string.h>
11#include <sys/time.h> 13#include <sys/time.h>
12#include <sys/types.h> 14#include <sys/types.h>
13#include <time.h> 15#include <time.h>
@@ -124,6 +126,14 @@ static inline int time_is_past(ot_time deadline) { return time_now_seconds() > d
124 survive a restart, and the clock above counts from boot. */ 126 survive a restart, and the clock above counts from boot. */
125static inline ot_time time_wall_minutes(void) { return time(NULL) / 60; } 127static inline ot_time time_wall_minutes(void) { return time(NULL) / 60; }
126 128
129/* Appends a 32 bit value in network byte order to a binary reply and
130 advances the cursor. The caller has made sure of the room. */
131static inline void pack_uint32(char **cursor, uint32_t value) {
132 value = htonl(value);
133 memcpy(*cursor, &value, sizeof(value));
134 *cursor += sizeof(value);
135}
136
127/* Cleared from the interrupt handler and read by every worker */ 137/* Cleared from the interrupt handler and read by every worker */
128extern _Atomic int g_opentracker_running; 138extern _Atomic int g_opentracker_running;
129 139