From 952da5949f677e932f2a48a5474210df571472fb Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sun, 29 Nov 2026 00:34:40 +0100 Subject: Warn about udp addresses already taken by other tracker instances --- DESIGN | 10 ++++++++++ src/opentracker.c | 13 +++++++++++++ src/ot_socket.c | 14 ++++++++++++++ src/ot_socket.h | 2 ++ tests/integration/test_udp.py | 19 ++++++++++++++++++- tests/t_socket.c | 19 +++++++++++++++++++ 6 files changed, 76 insertions(+), 1 deletion(-) 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 was then dropped on the exactmatch add_torrent_from_saved_state bails on, losing that torrent's completed count. +A udp address somebody else holds is warned about and bound all the same. +Linux permits the second bind when both sockets set SO_REUSEADDR, which the +tracker always does, and FreeBSD when both asked to share the port, which it +does for a group of workers. The two then take or split each other's datagrams +without either noticing. Every udp entry is probed before the first of them is +bound, with a socket that asks for no sharing, so that an overlap within the +tracker's own configuration is not mistaken for a stranger. TCP is not probed: +there the bind itself fails unless a balanced group was asked for, and on +Linux a probe would also trip over the connections a restart leaves behind. + Teardown stops the cleaner first. It is the one worker that calls into other modules, accesslist_cleanup and stats_cleanup, so stopping it after them left it 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,12 +227,25 @@ static void listen_bind_one(ot_listen *entry) { #endif } +static void listen_warn_if_taken(const ot_listen *entry) { + char address[64]; + + if (entry->proto != FLAG_UDP || !socket_udp_is_taken(entry->ip, entry->port)) + return; + + address[ip_format(address, entry->ip)] = 0; + fprintf(stderr, "Warning: udp address [%s]:%d is already in use, by another tracker instance perhaps.\n", address, entry->port); +} + /* Bound in the order the entries were given. Note: On macOS SO_REUSEPORT delivers everything to the socket that bound last. */ static void listen_bind_all(void) { size_t count, i; ot_listen *entries = config_listen_entries(&count); + for (i = 0; i < count; ++i) + listen_warn_if_taken(entries + i); + for (i = 0; i < count; ++i) listen_bind_one(entries + i); } 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) { return s; } +int socket_udp_is_taken(const ot_ip6 ip, uint16_t port) { + struct sockaddr_in6 sa; + int s = socket_v6(SOCK_DGRAM), taken; + + if (s == -1) + return 0; + + /* Asking for no sharing at all is refused whatever the holder allowed */ + sockaddr_from_ip(&sa, ip, port); + taken = bind(s, (struct sockaddr *)&sa, sizeof(sa)) == -1 && errno == EADDRINUSE; + close(s); + return taken; +} + int socket_tcp_connect(const ot_ip6 srcip, uint16_t srcport, const ot_ip6 dstip, uint16_t dstport) { struct sockaddr_in6 sa; 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); int socket_tcp_listen(const ot_ip6 ip, uint16_t port, socket_sharing sharing); int socket_udp_bind(const ot_ip6 ip, uint16_t port, socket_sharing sharing); +int socket_udp_is_taken(const ot_ip6 ip, uint16_t port); + int socket_tcp_connect(const ot_ip6 srcip, uint16_t srcport, const ot_ip6 dstip, uint16_t dstport); int socket_is_connected(int s); int 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 import struct import unittest -from harness import TrackerTestCase +from harness import Tracker, TrackerTestCase MAGIC = 0x41727101980 @@ -199,3 +199,20 @@ class UdpWorkersConfiguredAfterTheListener(TrackerTestCase): if __name__ == "__main__": unittest.main() + + +class UdpAddressAlreadyTaken(TrackerTestCase): + def test_a_second_tracker_on_the_same_udp_address_says_so(self): + """Linux lets it start all the same, and from then on the two take + each other's packets. FreeBSD refuses the bind that follows, and the + warning is then what explains the refusal.""" + try: + second = Tracker("listen.udp 127.0.0.1:%d\n" % self.tracker.port) + except AssertionError as refused: + said = str(refused) + else: + said = second.output() + second.stop() + second._cleanup() + self.assertIn("is already in use", said) + 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) { CHECK_INT(next_descriptor(), before); } +/* The holder binds the way the tracker does, which lets a second socket in + beside it, and that is what the probe is there to see through. */ +static void case_a_taken_udp_address_is_noticed(void) { + char ip[16]; + uint16_t port; + int holder; + + test_case("a udp address somebody holds is reported as taken"); + holder = socket_udp_bind(ip_loopback, 0, SOCKET_SHARED); + CHECK(holder != -1); + bound_address(holder, ip, &port); + CHECK_INT(socket_udp_is_taken(ip_loopback, port), 1); + + test_case("and as free again once it is let go"); + close(holder); + CHECK_INT(socket_udp_is_taken(ip_loopback, port), 0); +} + #ifdef WANT_SYNC_LIVE /* Reading an IPv4 multicast option back: Linux answers with an int, the BSDs with a single byte, so ask how much was written. */ @@ -391,6 +409,7 @@ void test_socket(void) { case_deferred_accept(); case_connect_refused(); case_bind_failure(); + case_a_taken_udp_address_is_noticed(); #ifdef WANT_SYNC_LIVE case_mcast_sender(); #endif -- cgit v1.3