From adc518a2063ee78e4217a8aba51b116af1af42f4 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 28 Nov 2026 19:46:59 +0100 Subject: Add helper to pack uint32 in network byte order --- src/ot_fullscrape.c | 5 ++--- src/ot_udp.c | 7 ++----- src/trackerlogic.c | 45 ++++++++++++++++++++++++--------------------- src/trackerlogic.h | 10 ++++++++++ 4 files changed, 38 insertions(+), 29 deletions(-) (limited to 'src') 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 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; + pack_uint32(&r, (uint32_t)seed_count); + pack_uint32(&r, (uint32_t)(peer_count - seed_count)); break; case TASK_FULLSCRAPE_TPB_URLENCODED: 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) { if (!left) OT_PEERFLAG(ws->peer) |= PEER_FLAG_SEEDING; - reply = udp_reply_header(ws->outbuf, UDP_ACTION_ANNOUNCE, request); + ws->reply = udp_reply_header(ws->outbuf, UDP_ACTION_ANNOUNCE, request); if (OT_PEERFLAG(ws->peer) & PEER_FLAG_STOPPED) { /* Peer is gone. */ - /* This one writes the whole reply, and leaves the header alone */ - ws->reply = ws->outbuf; ws->reply_size = remove_peer_from_torrent(FLAG_UDP, ws); } else { /* List of peers should fit in a single UDP packet */ @@ -216,7 +214,6 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) { if (numwant > max_peers) numwant = max_peers; - ws->reply = reply; ws->reply_size = add_peer_to_torrent_and_return_peers(FLAG_UDP, ws, numwant); /* 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) { udp_send_error(serversocket, ws, "Tracker is out of resources, try again later.", remoteip, remoteport); return 1; } - ws->reply_size += UDP_REPLY_HEADER_SIZE; } + ws->reply_size += UDP_REPLY_HEADER_SIZE; socket_send(serversocket, ws->outbuf, ws->reply_size, remoteip, remoteport); 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 return strlen(invalid_hash); } if (proto == FLAG_UDP) { - ((uint32_t *)ws->reply)[0] = htonl(OT_CLIENT_REJECT_INTERVAL_RANDOM); - ((uint32_t *)ws->reply)[1] = 0; - ((uint32_t *)ws->reply)[2] = 0; - return 12; + char *r = ws->reply; + pack_uint32(&r, OT_CLIENT_REJECT_INTERVAL_RANDOM); + pack_uint32(&r, 0); + pack_uint32(&r, 0); + return r - ws->reply; } return 0; } @@ -297,10 +298,9 @@ static size_t return_peers_for_torrent_udp(struct ot_workstruct *ws, ot_torrent if (amount > list_count) amount = list_count; - *(uint32_t *)(r + 0) = htonl(OT_CLIENT_REQUEST_INTERVAL_RANDOM); - *(uint32_t *)(r + 4) = htonl(peer_count - seed_count); - *(uint32_t *)(r + 8) = htonl(seed_count); - r += 12; + pack_uint32(&r, OT_CLIENT_REQUEST_INTERVAL_RANDOM); + pack_uint32(&r, peer_count - seed_count); + pack_uint32(&r, seed_count); if (amount) { if (amount == list_count) @@ -400,23 +400,25 @@ size_t return_udp_scrape_for_torrent(ot_hash const hash, char *reply) { ot_vector *torrents_list = mutex_bucket_lock_by_hash(hash); ot_torrent *torrent = vector_binary_search(hash, torrents_list->data, torrents_list->size, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch); - if (!exactmatch) { - memset(reply, 0, 12); - } else { - uint32_t *r = (uint32_t *)reply; + uint32_t seeders = 0, completed = 0, leechers = 0; + char *r = reply; + if (exactmatch) { if (clean_single_torrent(torrent)) { vector_remove_torrent(torrents_list, torrent); - memset(reply, 0, 12); delta_torrentcount = -1; } else { - r[0] = htonl(torrent_seed_count(torrent)); - r[1] = htonl(torrent->down_count); - r[2] = htonl(torrent_peer_count(torrent) - torrent_seed_count(torrent)); + seeders = torrent_seed_count(torrent); + completed = torrent->down_count; + leechers = torrent_peer_count(torrent) - torrent_seed_count(torrent); } } mutex_bucket_unlock_by_hash(hash, delta_torrentcount); - return 12; + + pack_uint32(&r, seeders); + pack_uint32(&r, completed); + pack_uint32(&r, leechers); + return r - reply; } /* Fetches scrape info for a specific torrent */ @@ -485,10 +487,11 @@ size_t remove_peer_from_torrent(PROTO_FLAG proto, struct ot_workstruct *ws) { /* Handle UDP reply */ if (proto == FLAG_UDP) { - ((uint32_t *)ws->reply)[2] = htonl(OT_CLIENT_REQUEST_INTERVAL_RANDOM); - ((uint32_t *)ws->reply)[3] = htonl(peer_count - seed_count); - ((uint32_t *)ws->reply)[4] = htonl(seed_count); - ws->reply_size = 20; + char *r = ws->reply; + pack_uint32(&r, OT_CLIENT_REQUEST_INTERVAL_RANDOM); + pack_uint32(&r, peer_count - seed_count); + pack_uint32(&r, seed_count); + ws->reply_size = r - ws->reply; } 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 @@ #ifndef OT_TRACKERLOGIC_H__ #define OT_TRACKERLOGIC_H__ +#include #include #include #include #include +#include #include #include #include @@ -124,6 +126,14 @@ static inline int time_is_past(ot_time deadline) { return time_now_seconds() > d survive a restart, and the clock above counts from boot. */ static inline ot_time time_wall_minutes(void) { return time(NULL) / 60; } +/* Appends a 32 bit value in network byte order to a binary reply and + advances the cursor. The caller has made sure of the room. */ +static inline void pack_uint32(char **cursor, uint32_t value) { + value = htonl(value); + memcpy(*cursor, &value, sizeof(value)); + *cursor += sizeof(value); +} + /* Cleared from the interrupt handler and read by every worker */ extern _Atomic int g_opentracker_running; -- cgit v1.3