diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/opentracker.c | 13 | ||||
| -rw-r--r-- | src/ot_socket.c | 14 | ||||
| -rw-r--r-- | src/ot_socket.h | 2 |
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 | ||
| 230 | static 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. */ |
| 232 | static void listen_bind_all(void) { | 242 | static 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 | ||
| 164 | int 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 | |||
| 164 | int socket_tcp_connect(const ot_ip6 srcip, uint16_t srcport, const ot_ip6 dstip, uint16_t dstport) { | 178 | int 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); | |||
| 32 | int socket_tcp_listen(const ot_ip6 ip, uint16_t port, socket_sharing sharing); | 32 | int socket_tcp_listen(const ot_ip6 ip, uint16_t port, socket_sharing sharing); |
| 33 | int socket_udp_bind(const ot_ip6 ip, uint16_t port, socket_sharing sharing); | 33 | int socket_udp_bind(const ot_ip6 ip, uint16_t port, socket_sharing sharing); |
| 34 | 34 | ||
| 35 | int socket_udp_is_taken(const ot_ip6 ip, uint16_t port); | ||
| 36 | |||
| 35 | int socket_tcp_connect(const ot_ip6 srcip, uint16_t srcport, const ot_ip6 dstip, uint16_t dstport); | 37 | int socket_tcp_connect(const ot_ip6 srcip, uint16_t srcport, const ot_ip6 dstip, uint16_t dstport); |
| 36 | int socket_is_connected(int s); | 38 | int socket_is_connected(int s); |
| 37 | int socket_accept(int s, ot_ip6 ip, uint16_t *port); | 39 | int socket_accept(int s, ot_ip6 ip, uint16_t *port); |
