summaryrefslogtreecommitdiff
path: root/src
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 /src
parentf08e7473ac51a024f49e5101fbc5e150fecef2d4 (diff)
Warn about udp addresses already taken by other tracker instancesHEADmaster
Diffstat (limited to 'src')
-rw-r--r--src/opentracker.c13
-rw-r--r--src/ot_socket.c14
-rw-r--r--src/ot_socket.h2
3 files changed, 29 insertions, 0 deletions
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);