summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2026-11-28 16:58:10 +0100
committerDirk Engling <erdgeist@erdgeist.org>2026-11-28 16:58:10 +0100
commit2a1b6673ffc96912322974cc85b93d13b6cd8e7c (patch)
treef545926f635f83ba148d8eb1b3e15bfdb9b4d115
parent595bfd57dc8179f9588e2a8d1e0efd1194e6246c (diff)
Simplify UDP scraper and prevent an incomplete 75th hash from being processed, adhering to the BEP now
-rw-r--r--src/ot_udp.c21
-rw-r--r--tests/integration/test_udp.py22
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 @@
19#include "ot_udp.h" 19#include "ot_udp.h"
20#include "trackerlogic.h" 20#include "trackerlogic.h"
21 21
22/* BEP 15: "Up to about 74 torrents can be scraped at once" */
23#define OT_MAX_SCRAPE_UDP 74
24
22static uint32_t g_rijndael_round_key[44] = {0}; 25static uint32_t g_rijndael_round_key[44] = {0};
23static volatile uint32_t g_key_of_the_hour[2] = {0}; 26static volatile uint32_t g_key_of_the_hour[2] = {0};
24static volatile ot_time g_hour_of_the_key; 27static volatile ot_time g_hour_of_the_key;
@@ -70,7 +73,9 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) {
70 uint32_t connid[2]; 73 uint32_t connid[2];
71 uint32_t action; 74 uint32_t action;
72 uint16_t port, remoteport; 75 uint16_t port, remoteport;
73 size_t byte_count, scrape_count; 76 size_t byte_count, scrape_count, i;
77 ot_hash *hashes;
78 char *reply;
74 79
75 ssize_t result = socket_recv(serversocket, ws->inbuf, G_INBUF_SIZE, remoteip, &remoteport); 80 ssize_t result = socket_recv(serversocket, ws->inbuf, G_INBUF_SIZE, remoteip, &remoteport);
76 if (result <= 0) 81 if (result <= 0)
@@ -193,14 +198,20 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) {
193 stats_issue_event(ws, EVENT_ANNOUNCE, FLAG_UDP, ws->reply_size); 198 stats_issue_event(ws, EVENT_ANNOUNCE, FLAG_UDP, ws->reply_size);
194 break; 199 break;
195 200
196 case 2: /* This is a scrape action */ 201 case 2: /* This is a scrape action */
202 scrape_count = (byte_count - 16) / sizeof(ot_hash);
203 if (scrape_count > OT_MAX_SCRAPE_UDP)
204 scrape_count = OT_MAX_SCRAPE_UDP;
205
197 outpacket[0] = htonl(2); /* scrape action */ 206 outpacket[0] = htonl(2); /* scrape action */
198 outpacket[1] = inpacket[12 / 4]; 207 outpacket[1] = inpacket[12 / 4];
199 208
200 for (scrape_count = 0; (scrape_count * 20 < byte_count - 16) && (scrape_count <= 74); scrape_count++) 209 hashes = (ot_hash *)(ws->inbuf + 16);
201 return_udp_scrape_for_torrent(*(ot_hash *)(((char *)inpacket) + 16 + 20 * scrape_count), ((char *)outpacket) + 8 + 12 * scrape_count); 210 reply = ws->outbuf + 8;
211 for (i = 0; i < scrape_count; ++i)
212 reply += return_udp_scrape_for_torrent(hashes[i], reply);
202 213
203 socket_send(serversocket, ws->outbuf, 8 + 12 * scrape_count, remoteip, remoteport); 214 socket_send(serversocket, ws->outbuf, reply - ws->outbuf, remoteip, remoteport);
204 stats_issue_event(ws, EVENT_SCRAPE, FLAG_UDP, scrape_count); 215 stats_issue_event(ws, EVENT_SCRAPE, FLAG_UDP, scrape_count);
205 break; 216 break;
206 } 217 }
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):
148 finally: 148 finally:
149 sock.close() 149 sock.close()
150 150
151 def test_a_hash_cut_short_is_not_scraped(self):
152 """What follows the last whole hash is not a hash, and the bytes that
153 would complete it are not the client's."""
154 info_hash = self.hash_for("udpcut")
155 sock, connection_id = self.tracker.udp_connect()
156 try:
157 sock.sendto(
158 struct.pack(">QII", connection_id, 2, 3) + info_hash + b"\x00" * 7, ("127.0.0.1", self.tracker.port)
159 )
160 self.assertEqual(len(sock.recv(2048)), 8 + 12)
161 finally:
162 sock.close()
163
164 def test_a_scrape_answers_no_more_hashes_than_the_protocol_allows(self):
165 sock, connection_id = self.tracker.udp_connect()
166 try:
167 hashes = b"".join(self.hash_for("udpmany%d" % n) for n in range(80))
168 sock.sendto(struct.pack(">QII", connection_id, 2, 4) + hashes, ("127.0.0.1", self.tracker.port))
169 self.assertEqual(len(sock.recv(2048)), 8 + 12 * 74)
170 finally:
171 sock.close()
172
151 173
152class UdpWorkersConfiguredAfterTheListener(TrackerTestCase): 174class UdpWorkersConfiguredAfterTheListener(TrackerTestCase):
153 """The harness writes its listen lines first, so this directive lands 175 """The harness writes its listen lines first, so this directive lands