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 ++++++++++++++++----- tests/integration/test_udp.py | 22 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) 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; } diff --git a/tests/integration/test_udp.py b/tests/integration/test_udp.py index 1cb2389..3afe4ef 100644 --- a/tests/integration/test_udp.py +++ b/tests/integration/test_udp.py @@ -148,6 +148,28 @@ class UdpScrape(TrackerTestCase): finally: sock.close() + def test_a_hash_cut_short_is_not_scraped(self): + """What follows the last whole hash is not a hash, and the bytes that + would complete it are not the client's.""" + info_hash = self.hash_for("udpcut") + sock, connection_id = self.tracker.udp_connect() + try: + sock.sendto( + struct.pack(">QII", connection_id, 2, 3) + info_hash + b"\x00" * 7, ("127.0.0.1", self.tracker.port) + ) + self.assertEqual(len(sock.recv(2048)), 8 + 12) + finally: + sock.close() + + def test_a_scrape_answers_no_more_hashes_than_the_protocol_allows(self): + sock, connection_id = self.tracker.udp_connect() + try: + hashes = b"".join(self.hash_for("udpmany%d" % n) for n in range(80)) + sock.sendto(struct.pack(">QII", connection_id, 2, 4) + hashes, ("127.0.0.1", self.tracker.port)) + self.assertEqual(len(sock.recv(2048)), 8 + 12 * 74) + finally: + sock.close() + class UdpWorkersConfiguredAfterTheListener(TrackerTestCase): """The harness writes its listen lines first, so this directive lands -- cgit v1.3