summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2026-11-29 00:34:40 +0100
committerDirk Engling <erdgeist@erdgeist.org>2026-11-29 00:34:40 +0100
commit952da5949f677e932f2a48a5474210df571472fb (patch)
treed33525d29ee18b65b9e5df39d2e2fb1f5a868ca2
parentf08e7473ac51a024f49e5101fbc5e150fecef2d4 (diff)
Warn about udp addresses already taken by other tracker instancesHEADmaster
-rw-r--r--DESIGN10
-rw-r--r--src/opentracker.c13
-rw-r--r--src/ot_socket.c14
-rw-r--r--src/ot_socket.h2
-rw-r--r--tests/integration/test_udp.py19
-rw-r--r--tests/t_socket.c19
6 files changed, 76 insertions, 1 deletions
diff --git a/DESIGN b/DESIGN
index 2e1883e..7933223 100644
--- a/DESIGN
+++ b/DESIGN
@@ -214,6 +214,16 @@ packet arriving in that window created the torrent, and the saved state for it
214was then dropped on the exactmatch add_torrent_from_saved_state bails on, 214was then dropped on the exactmatch add_torrent_from_saved_state bails on,
215losing that torrent's completed count. 215losing that torrent's completed count.
216 216
217A udp address somebody else holds is warned about and bound all the same.
218Linux permits the second bind when both sockets set SO_REUSEADDR, which the
219tracker always does, and FreeBSD when both asked to share the port, which it
220does for a group of workers. The two then take or split each other's datagrams
221without either noticing. Every udp entry is probed before the first of them is
222bound, with a socket that asks for no sharing, so that an overlap within the
223tracker's own configuration is not mistaken for a stranger. TCP is not probed:
224there the bind itself fails unless a balanced group was asked for, and on
225Linux a probe would also trip over the connections a restart leaves behind.
226
217Teardown stops the cleaner first. It is the one worker that calls into other 227Teardown stops the cleaner first. It is the one worker that calls into other
218modules, accesslist_cleanup and stats_cleanup, so stopping it after them left 228modules, accesslist_cleanup and stats_cleanup, so stopping it after them left
219it locking a mutex accesslist_deinit had already destroyed: five shutdowns in 229it locking a mutex accesslist_deinit had already destroyed: five shutdowns in
diff --git a/src/opentracker.c b/src/opentracker.c
index 36dff71..81b4df6 100644
--- a/src/opentracker.c
+++ b/src/opentracker.c
@@ -227,6 +227,16 @@ static void listen_bind_one(ot_listen *entry) {
227#endif 227#endif
228} 228}
229 229
230static void listen_warn_if_taken(const ot_listen *entry) {
231 char address[64];
232
233 if (entry->proto != FLAG_UDP || !socket_udp_is_taken(entry->ip, entry->port))
234 return;
235
236 address[ip_format(address, entry->ip)] = 0;
237 fprintf(stderr, "Warning: udp address [%s]:%d is already in use, by another tracker instance perhaps.\n", address, entry->port);
238}
239
230/* Bound in the order the entries were given. 240/* Bound in the order the entries were given.
231 Note: On macOS SO_REUSEPORT delivers everything to the socket that bound last. */ 241 Note: On macOS SO_REUSEPORT delivers everything to the socket that bound last. */
232static void listen_bind_all(void) { 242static void listen_bind_all(void) {
@@ -234,6 +244,9 @@ static void listen_bind_all(void) {
234 ot_listen *entries = config_listen_entries(&count); 244 ot_listen *entries = config_listen_entries(&count);
235 245
236 for (i = 0; i < count; ++i) 246 for (i = 0; i < count; ++i)
247 listen_warn_if_taken(entries + i);
248
249 for (i = 0; i < count; ++i)
237 listen_bind_one(entries + i); 250 listen_bind_one(entries + i);
238} 251}
239 252
diff --git a/src/ot_socket.c b/src/ot_socket.c
index b6a1803..8b17080 100644
--- a/src/ot_socket.c
+++ b/src/ot_socket.c
@@ -161,6 +161,20 @@ int socket_udp_bind(const ot_ip6 ip, uint16_t port, socket_sharing sharing) {
161 return s; 161 return s;
162} 162}
163 163
164int socket_udp_is_taken(const ot_ip6 ip, uint16_t port) {
165 struct sockaddr_in6 sa;
166 int s = socket_v6(SOCK_DGRAM), taken;
167
168 if (s == -1)
169 return 0;
170
171 /* Asking for no sharing at all is refused whatever the holder allowed */
172 sockaddr_from_ip(&sa, ip, port);
173 taken = bind(s, (struct sockaddr *)&sa, sizeof(sa)) == -1 && errno == EADDRINUSE;
174 close(s);
175 return taken;
176}
177
164int socket_tcp_connect(const ot_ip6 srcip, uint16_t srcport, const ot_ip6 dstip, uint16_t dstport) { 178int socket_tcp_connect(const ot_ip6 srcip, uint16_t srcport, const ot_ip6 dstip, uint16_t dstport) {
165 struct sockaddr_in6 sa; 179 struct sockaddr_in6 sa;
166 int s = socket_v6(SOCK_STREAM); 180 int s = socket_v6(SOCK_STREAM);
diff --git a/src/ot_socket.h b/src/ot_socket.h
index 5c1d840..33446e4 100644
--- a/src/ot_socket.h
+++ b/src/ot_socket.h
@@ -32,6 +32,8 @@ int socket_can_balance(void);
32int socket_tcp_listen(const ot_ip6 ip, uint16_t port, socket_sharing sharing); 32int socket_tcp_listen(const ot_ip6 ip, uint16_t port, socket_sharing sharing);
33int socket_udp_bind(const ot_ip6 ip, uint16_t port, socket_sharing sharing); 33int socket_udp_bind(const ot_ip6 ip, uint16_t port, socket_sharing sharing);
34 34
35int socket_udp_is_taken(const ot_ip6 ip, uint16_t port);
36
35int socket_tcp_connect(const ot_ip6 srcip, uint16_t srcport, const ot_ip6 dstip, uint16_t dstport); 37int socket_tcp_connect(const ot_ip6 srcip, uint16_t srcport, const ot_ip6 dstip, uint16_t dstport);
36int socket_is_connected(int s); 38int socket_is_connected(int s);
37int socket_accept(int s, ot_ip6 ip, uint16_t *port); 39int socket_accept(int s, ot_ip6 ip, uint16_t *port);
diff --git a/tests/integration/test_udp.py b/tests/integration/test_udp.py
index b60bd56..8a255ec 100644
--- a/tests/integration/test_udp.py
+++ b/tests/integration/test_udp.py
@@ -9,7 +9,7 @@ import socket
9import struct 9import struct
10import unittest 10import unittest
11 11
12from harness import TrackerTestCase 12from harness import Tracker, TrackerTestCase
13 13
14MAGIC = 0x41727101980 14MAGIC = 0x41727101980
15 15
@@ -199,3 +199,20 @@ class UdpWorkersConfiguredAfterTheListener(TrackerTestCase):
199 199
200if __name__ == "__main__": 200if __name__ == "__main__":
201 unittest.main() 201 unittest.main()
202
203
204class UdpAddressAlreadyTaken(TrackerTestCase):
205 def test_a_second_tracker_on_the_same_udp_address_says_so(self):
206 """Linux lets it start all the same, and from then on the two take
207 each other's packets. FreeBSD refuses the bind that follows, and the
208 warning is then what explains the refusal."""
209 try:
210 second = Tracker("listen.udp 127.0.0.1:%d\n" % self.tracker.port)
211 except AssertionError as refused:
212 said = str(refused)
213 else:
214 said = second.output()
215 second.stop()
216 second._cleanup()
217 self.assertIn("is already in use", said)
218 self.assertNotIn("is already in use", self.tracker.output())
diff --git a/tests/t_socket.c b/tests/t_socket.c
index 8e917a3..6f07f95 100644
--- a/tests/t_socket.c
+++ b/tests/t_socket.c
@@ -342,6 +342,24 @@ static void case_bind_failure(void) {
342 CHECK_INT(next_descriptor(), before); 342 CHECK_INT(next_descriptor(), before);
343} 343}
344 344
345/* The holder binds the way the tracker does, which lets a second socket in
346 beside it, and that is what the probe is there to see through. */
347static void case_a_taken_udp_address_is_noticed(void) {
348 char ip[16];
349 uint16_t port;
350 int holder;
351
352 test_case("a udp address somebody holds is reported as taken");
353 holder = socket_udp_bind(ip_loopback, 0, SOCKET_SHARED);
354 CHECK(holder != -1);
355 bound_address(holder, ip, &port);
356 CHECK_INT(socket_udp_is_taken(ip_loopback, port), 1);
357
358 test_case("and as free again once it is let go");
359 close(holder);
360 CHECK_INT(socket_udp_is_taken(ip_loopback, port), 0);
361}
362
345#ifdef WANT_SYNC_LIVE 363#ifdef WANT_SYNC_LIVE
346/* Reading an IPv4 multicast option back: Linux answers with an int, the BSDs 364/* Reading an IPv4 multicast option back: Linux answers with an int, the BSDs
347 with a single byte, so ask how much was written. */ 365 with a single byte, so ask how much was written. */
@@ -391,6 +409,7 @@ void test_socket(void) {
391 case_deferred_accept(); 409 case_deferred_accept();
392 case_connect_refused(); 410 case_connect_refused();
393 case_bind_failure(); 411 case_bind_failure();
412 case_a_taken_udp_address_is_noticed();
394#ifdef WANT_SYNC_LIVE 413#ifdef WANT_SYNC_LIVE
395 case_mcast_sender(); 414 case_mcast_sender();
396#endif 415#endif