From 2a1b6673ffc96912322974cc85b93d13b6cd8e7c Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 28 Nov 2026 16:58:10 +0100 Subject: Simplify UDP scraper and prevent an incomplete 75th hash from being processed, adhering to the BEP now --- src/ot_udp.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/ot_udp.c b/src/ot_udp.c index 9e8e3c4..2755766 100644 --- a/src/ot_udp.c +++ b/src/ot_udp.c @@ -19,6 +19,9 @@ #include "ot_udp.h" #include "trackerlogic.h" +/* BEP 15: "Up to about 74 torrents can be scraped at once" */ +#define OT_MAX_SCRAPE_UDP 74 + static uint32_t g_rijndael_round_key[44] = {0}; static volatile uint32_t g_key_of_the_hour[2] = {0}; static volatile ot_time g_hour_of_the_key; @@ -70,7 +73,9 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) { uint32_t connid[2]; uint32_t action; uint16_t port, remoteport; - size_t byte_count, scrape_count; + size_t byte_count, scrape_count, i; + ot_hash *hashes; + char *reply; ssize_t result = socket_recv(serversocket, ws->inbuf, G_INBUF_SIZE, remoteip, &remoteport); if (result <= 0) @@ -193,14 +198,20 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) { stats_issue_event(ws, EVENT_ANNOUNCE, FLAG_UDP, ws->reply_size); break; - case 2: /* This is a scrape action */ + case 2: /* This is a scrape action */ + scrape_count = (byte_count - 16) / sizeof(ot_hash); + if (scrape_count > OT_MAX_SCRAPE_UDP) + scrape_count = OT_MAX_SCRAPE_UDP; + outpacket[0] = htonl(2); /* scrape action */ outpacket[1] = inpacket[12 / 4]; - for (scrape_count = 0; (scrape_count * 20 < byte_count - 16) && (scrape_count <= 74); scrape_count++) - return_udp_scrape_for_torrent(*(ot_hash *)(((char *)inpacket) + 16 + 20 * scrape_count), ((char *)outpacket) + 8 + 12 * scrape_count); + hashes = (ot_hash *)(ws->inbuf + 16); + reply = ws->outbuf + 8; + for (i = 0; i < scrape_count; ++i) + reply += return_udp_scrape_for_torrent(hashes[i], reply); - socket_send(serversocket, ws->outbuf, 8 + 12 * scrape_count, remoteip, remoteport); + socket_send(serversocket, ws->outbuf, reply - ws->outbuf, remoteip, remoteport); stats_issue_event(ws, EVENT_SCRAPE, FLAG_UDP, scrape_count); break; } -- cgit v1.3