summaryrefslogtreecommitdiff
path: root/opentracker.c
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2026-10-26 16:37:55 +0100
committerDirk Engling <erdgeist@erdgeist.org>2026-10-26 16:37:55 +0100
commit5e86efde052760a53ca92ddd411db095fcf4a970 (patch)
treec538485595c26bc7c47ea281667d0652448fddf3 /opentracker.c
parent0194b1b8a00d6646f03291e6045918aaaf7cfdc4 (diff)
Use new fdstate subsystem to store per fd cookie and timeout
Diffstat (limited to 'opentracker.c')
-rw-r--r--opentracker.c26
1 files changed, 16 insertions, 10 deletions
diff --git a/opentracker.c b/opentracker.c
index 60bdceb..0f0b314 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -34,8 +34,8 @@
34#include "ot_mutex.h" 34#include "ot_mutex.h"
35#include "ot_random.h" 35#include "ot_random.h"
36#include "ot_scan.h" 36#include "ot_scan.h"
37#include "ot_fdstate.h"
37#include "ot_socket.h" 38#include "ot_socket.h"
38#include "ot_timeout.h"
39#include "ot_stats.h" 39#include "ot_stats.h"
40#include "ot_udp.h" 40#include "ot_udp.h"
41#include "trackerlogic.h" 41#include "trackerlogic.h"
@@ -118,7 +118,7 @@ static ssize_t header_complete(char *request, ssize_t byte_count) {
118} 118}
119 119
120static void handle_dead(const int64_t sock) { 120static void handle_dead(const int64_t sock) {
121 struct http_data *cookie = io_getcookie(sock); 121 struct http_data *cookie = fdstate_cookie(sock);
122 if (cookie) { 122 if (cookie) {
123 if (cookie->batch) 123 if (cookie->batch)
124 batch_reset(cookie->batch); 124 batch_reset(cookie->batch);
@@ -128,11 +128,13 @@ static void handle_dead(const int64_t sock) {
128 mutex_workqueue_canceltask(sock); 128 mutex_workqueue_canceltask(sock);
129 free(cookie); 129 free(cookie);
130 } 130 }
131 /* The generation has to move before the kernel can reissue the number */
132 fdstate_close(sock);
131 io_close(sock); 133 io_close(sock);
132} 134}
133 135
134static void handle_read(const int64_t sock, struct ot_workstruct *ws) { 136static void handle_read(const int64_t sock, struct ot_workstruct *ws) {
135 struct http_data *cookie = io_getcookie(sock); 137 struct http_data *cookie = fdstate_cookie(sock);
136 ssize_t byte_count = io_tryread(sock, ws->inbuf, G_INBUF_SIZE); 138 ssize_t byte_count = io_tryread(sock, ws->inbuf, G_INBUF_SIZE);
137 139
138 if (byte_count == 0 || byte_count == -3) { 140 if (byte_count == 0 || byte_count == -3) {
@@ -176,7 +178,7 @@ static void handle_read(const int64_t sock, struct ot_workstruct *ws) {
176} 178}
177 179
178static void handle_write(const int64_t sock) { 180static void handle_write(const int64_t sock) {
179 struct http_data *cookie = io_getcookie(sock); 181 struct http_data *cookie = fdstate_cookie(sock);
180 int chunked = 0; 182 int chunked = 0;
181 183
182 if (cookie) { 184 if (cookie) {
@@ -218,12 +220,11 @@ static void handle_accept(const int64_t serversocket) {
218 memset(cookie, 0, sizeof(struct http_data)); 220 memset(cookie, 0, sizeof(struct http_data));
219 memcpy(cookie->ip, ip, sizeof(ot_ip6)); 221 memcpy(cookie->ip, ip, sizeof(ot_ip6));
220 222
221 io_setcookie(sock, cookie); 223 fdstate_open(sock, cookie, g_now_seconds + OT_CLIENT_TIMEOUT);
222 io_wantread(sock); 224 io_wantread(sock);
223 225
224 stats_issue_event(EVENT_ACCEPT, FLAG_TCP, (uintptr_t)ip); 226 stats_issue_event(EVENT_ACCEPT, FLAG_TCP, (uintptr_t)ip);
225 227
226 timeout_in(sock, OT_CLIENT_TIMEOUT);
227 } 228 }
228 io_eagain(serversocket); 229 io_eagain(serversocket);
229} 230}
@@ -255,7 +256,7 @@ static void *server_mainloop(void *args) {
255 io_wait(); 256 io_wait();
256 257
257 while ((sock = io_canread()) != -1) { 258 while ((sock = io_canread()) != -1) {
258 const void *cookie = io_getcookie(sock); 259 const void *cookie = fdstate_cookie(sock);
259 if ((intptr_t)cookie == FLAG_TCP) 260 if ((intptr_t)cookie == FLAG_TCP)
260 handle_accept(sock); 261 handle_accept(sock);
261 else if ((intptr_t)cookie == FLAG_UDP) 262 else if ((intptr_t)cookie == FLAG_UDP)
@@ -273,7 +274,9 @@ static void *server_mainloop(void *args) {
273 handle_write(sock); 274 handle_write(sock);
274 275
275 if (g_now_seconds > next_timeout_check) { 276 if (g_now_seconds > next_timeout_check) {
276 while ((sock = io_timeouted()) != -1) 277 int cursor = 0;
278
279 while ((sock = fdstate_next_expired(g_now_seconds, &cursor)) != -1)
277 handle_dead(sock); 280 handle_dead(sock);
278 next_timeout_check = g_now_seconds + OT_CLIENT_TIMEOUT_CHECKINTERVAL; 281 next_timeout_check = g_now_seconds + OT_CLIENT_TIMEOUT_CHECKINTERVAL;
279 } 282 }
@@ -319,7 +322,7 @@ static void listen_bind_one(ot_listen *entry) {
319 if (!io_fd(entry->sock)) 322 if (!io_fd(entry->sock))
320 panic("io_fd"); 323 panic("io_fd");
321 324
322 io_setcookie(entry->sock, (void *)entry->proto); 325 fdstate_open(entry->sock, (void *)entry->proto, 0);
323 io_wantread(entry->sock); 326 io_wantread(entry->sock);
324 327
325#ifdef _DEBUG 328#ifdef _DEBUG
@@ -448,6 +451,8 @@ int main(int argc, char **argv) {
448 pthread_t thread_id; /* time cacher */ 451 pthread_t thread_id; /* time cacher */
449 452
450 random_init(); 453 random_init();
454 if (fdstate_init() == -1)
455 panic("fdstate_init");
451 config_parse(argc, argv); 456 config_parse(argc, argv);
452 457
453 listen_bind_all(); 458 listen_bind_all();
@@ -473,7 +478,7 @@ int main(int argc, char **argv) {
473 panic("selfpipe io_fd failed: "); 478 panic("selfpipe io_fd failed: ");
474 if (!io_fd(g_self_pipe[1])) 479 if (!io_fd(g_self_pipe[1]))
475 panic("selfpipe io_fd failed: "); 480 panic("selfpipe io_fd failed: ");
476 io_setcookie(g_self_pipe[0], (void *)FLAG_SELFPIPE); 481 fdstate_open(g_self_pipe[0], (void *)FLAG_SELFPIPE, 0);
477 io_wantread(g_self_pipe[0]); 482 io_wantread(g_self_pipe[0]);
478 483
479 /* Init all sub systems. This call may fail with an exit() */ 484 /* Init all sub systems. This call may fail with an exit() */
@@ -494,5 +499,6 @@ int main(int argc, char **argv) {
494 499
495 server_mainloop(0); 500 server_mainloop(0);
496 501
502 fdstate_deinit();
497 return 0; 503 return 0;
498} 504}