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 --- tests/integration/test_udp.py | 19 ++++++++++++++++++- tests/t_socket.c | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) (limited to 'tests') 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