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 --- src/opentracker.c | 13 +++++++++++++ src/ot_socket.c | 14 ++++++++++++++ src/ot_socket.h | 2 ++ 3 files changed, 29 insertions(+) (limited to 'src') 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); -- cgit v1.3