From 5e86efde052760a53ca92ddd411db095fcf4a970 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Mon, 26 Oct 2026 16:37:55 +0100 Subject: Use new fdstate subsystem to store per fd cookie and timeout --- opentracker.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'opentracker.c') diff --git a/opentracker.c b/opentracker.c index 60bdceb..0f0b314 100644 --- a/opentracker.c +++ b/opentracker.c @@ -34,8 +34,8 @@ #include "ot_mutex.h" #include "ot_random.h" #include "ot_scan.h" +#include "ot_fdstate.h" #include "ot_socket.h" -#include "ot_timeout.h" #include "ot_stats.h" #include "ot_udp.h" #include "trackerlogic.h" @@ -118,7 +118,7 @@ static ssize_t header_complete(char *request, ssize_t byte_count) { } static void handle_dead(const int64_t sock) { - struct http_data *cookie = io_getcookie(sock); + struct http_data *cookie = fdstate_cookie(sock); if (cookie) { if (cookie->batch) batch_reset(cookie->batch); @@ -128,11 +128,13 @@ static void handle_dead(const int64_t sock) { mutex_workqueue_canceltask(sock); free(cookie); } + /* The generation has to move before the kernel can reissue the number */ + fdstate_close(sock); io_close(sock); } static void handle_read(const int64_t sock, struct ot_workstruct *ws) { - struct http_data *cookie = io_getcookie(sock); + struct http_data *cookie = fdstate_cookie(sock); ssize_t byte_count = io_tryread(sock, ws->inbuf, G_INBUF_SIZE); if (byte_count == 0 || byte_count == -3) { @@ -176,7 +178,7 @@ static void handle_read(const int64_t sock, struct ot_workstruct *ws) { } static void handle_write(const int64_t sock) { - struct http_data *cookie = io_getcookie(sock); + struct http_data *cookie = fdstate_cookie(sock); int chunked = 0; if (cookie) { @@ -218,12 +220,11 @@ static void handle_accept(const int64_t serversocket) { memset(cookie, 0, sizeof(struct http_data)); memcpy(cookie->ip, ip, sizeof(ot_ip6)); - io_setcookie(sock, cookie); + fdstate_open(sock, cookie, g_now_seconds + OT_CLIENT_TIMEOUT); io_wantread(sock); stats_issue_event(EVENT_ACCEPT, FLAG_TCP, (uintptr_t)ip); - timeout_in(sock, OT_CLIENT_TIMEOUT); } io_eagain(serversocket); } @@ -255,7 +256,7 @@ static void *server_mainloop(void *args) { io_wait(); while ((sock = io_canread()) != -1) { - const void *cookie = io_getcookie(sock); + const void *cookie = fdstate_cookie(sock); if ((intptr_t)cookie == FLAG_TCP) handle_accept(sock); else if ((intptr_t)cookie == FLAG_UDP) @@ -273,7 +274,9 @@ static void *server_mainloop(void *args) { handle_write(sock); if (g_now_seconds > next_timeout_check) { - while ((sock = io_timeouted()) != -1) + int cursor = 0; + + while ((sock = fdstate_next_expired(g_now_seconds, &cursor)) != -1) handle_dead(sock); next_timeout_check = g_now_seconds + OT_CLIENT_TIMEOUT_CHECKINTERVAL; } @@ -319,7 +322,7 @@ static void listen_bind_one(ot_listen *entry) { if (!io_fd(entry->sock)) panic("io_fd"); - io_setcookie(entry->sock, (void *)entry->proto); + fdstate_open(entry->sock, (void *)entry->proto, 0); io_wantread(entry->sock); #ifdef _DEBUG @@ -448,6 +451,8 @@ int main(int argc, char **argv) { pthread_t thread_id; /* time cacher */ random_init(); + if (fdstate_init() == -1) + panic("fdstate_init"); config_parse(argc, argv); listen_bind_all(); @@ -473,7 +478,7 @@ int main(int argc, char **argv) { panic("selfpipe io_fd failed: "); if (!io_fd(g_self_pipe[1])) panic("selfpipe io_fd failed: "); - io_setcookie(g_self_pipe[0], (void *)FLAG_SELFPIPE); + fdstate_open(g_self_pipe[0], (void *)FLAG_SELFPIPE, 0); io_wantread(g_self_pipe[0]); /* Init all sub systems. This call may fail with an exit() */ @@ -494,5 +499,6 @@ int main(int argc, char **argv) { server_mainloop(0); + fdstate_deinit(); return 0; } -- cgit v1.3