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/trackerlogic.c | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) (limited to 'src/trackerlogic.c') 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); -- cgit v1.3