From 2e5b19945eb03fe4e3fa47562888401c7cbae71d Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sun, 25 Oct 2026 22:56:34 +0100 Subject: Factor out the timeout setter, will probably go later once we do the io loop ourself --- opentracker.c | 8 ++------ ot_http.c | 23 ++++++----------------- ot_timeout.h | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 23 deletions(-) create mode 100644 ot_timeout.h diff --git a/opentracker.c b/opentracker.c index 50fff29..60bdceb 100644 --- a/opentracker.c +++ b/opentracker.c @@ -35,6 +35,7 @@ #include "ot_random.h" #include "ot_scan.h" #include "ot_socket.h" +#include "ot_timeout.h" #include "ot_stats.h" #include "ot_udp.h" #include "trackerlogic.h" @@ -204,7 +205,6 @@ static void handle_accept(const int64_t serversocket) { int64_t sock; ot_ip6 ip; uint16_t port; - tai6464 t; while ((sock = socket_accept(serversocket, ip, &port)) != -1) { @@ -223,11 +223,7 @@ static void handle_accept(const int64_t serversocket) { stats_issue_event(EVENT_ACCEPT, FLAG_TCP, (uintptr_t)ip); - /* That breaks taia encapsulation. But there is no way to take system - time this often in FreeBSD and libowfat does not allow to set unix time */ - taia_uint(&t, 0); /* Clear t */ - tai_unix(&(t.sec), (g_now_seconds + OT_CLIENT_TIMEOUT)); - io_timeout(sock, t); + timeout_in(sock, OT_CLIENT_TIMEOUT); } io_eagain(serversocket); } diff --git a/ot_http.c b/ot_http.c index 4675efb..331a457 100644 --- a/ot_http.c +++ b/ot_http.c @@ -26,6 +26,7 @@ #include "ot_mutex.h" #include "ot_scan.h" #include "ot_stats.h" +#include "ot_timeout.h" #include "scan_urlencoded_query.h" #include "trackerlogic.h" @@ -72,7 +73,6 @@ static void http_senddata(const int64_t sock, struct ot_workstruct *ws) { if (written_size < ws->reply_size) { char *outbuf; - tai6464 t; if (!(outbuf = malloc(ws->reply_size - written_size))) { buf_reset(&cookie->request); @@ -97,8 +97,7 @@ static void http_senddata(const int64_t sock, struct ot_workstruct *ws) { /* writeable short data sockets just have a tcp timeout */ if (!ws->keep_alive) { - taia_uint(&t, 0); - io_timeout(sock, t); + timeout_never(sock); io_dontwantread(sock); } io_wantwrite(sock); @@ -139,7 +138,6 @@ ssize_t http_sendiovecdata(const int64_t sock, struct ot_workstruct *ws, size_t char *header; const char *encoding = ""; size_t header_size, size = iovec_length(&iovec_entries, (const struct iovec **)&iovector); - tai6464 t; /* No cookie? Bad socket. Leave. */ if (!cookie) { @@ -209,10 +207,7 @@ ssize_t http_sendiovecdata(const int64_t sock, struct ot_workstruct *ws, size_t cookie->flag &= ~STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER; } - /* writeable sockets timeout after 10 minutes */ - taia_now(&t); - taia_addsec(&t, &t, OT_CLIENT_TIMEOUT_SEND); - io_timeout(sock, t); + timeout_in(sock, OT_CLIENT_TIMEOUT_SEND); io_wantwrite(sock); return 0; } @@ -291,7 +286,6 @@ static ssize_t http_handle_stats(const int64_t sock, struct ot_workstruct *ws, c } if (mode == TASK_STATS_TPB) { - tai6464 t; #ifdef WANT_COMPRESSION_GZIP ws->request[ws->request_size - 1] = 0; #ifndef WANT_COMPRESSION_GZIP_ALWAYS @@ -307,8 +301,7 @@ static ssize_t http_handle_stats(const int64_t sock, struct ot_workstruct *ws, c cookie->flag |= STRUCT_HTTP_FLAG_WAITINGFORTASK | STRUCT_HTTP_FLAG_CHUNKED; /* Clients waiting for us should not easily timeout */ - taia_uint(&t, 0); - io_timeout(sock, t); + timeout_never(sock); fullscrape_deliver(sock, format); io_dontwantread(sock); return ws->reply_size = -2; @@ -317,13 +310,11 @@ static ssize_t http_handle_stats(const int64_t sock, struct ot_workstruct *ws, c /* default format for now */ if ((mode & TASK_CLASS_MASK) == TASK_STATS) { - tai6464 t; cookie->flag |= STRUCT_HTTP_FLAG_WAITINGFORTASK; /* Complex stats also include expensive memory debugging tools */ - taia_uint(&t, 0); - io_timeout(sock, t); + timeout_never(sock); stats_deliver(sock, mode); return ws->reply_size = -2; } @@ -345,7 +336,6 @@ typedef struct { static ssize_t http_handle_fullscrape(const int64_t sock, struct ot_workstruct *ws) { struct http_data *cookie = io_getcookie(sock); int format = 0; - tai6464 t; #ifdef WANT_MODEST_FULLSCRAPES { @@ -404,8 +394,7 @@ static ssize_t http_handle_fullscrape(const int64_t sock, struct ot_workstruct * /* Pass this task to the worker thread */ cookie->flag |= STRUCT_HTTP_FLAG_WAITINGFORTASK | STRUCT_HTTP_FLAG_CHUNKED; /* Clients waiting for us should not easily timeout */ - taia_uint(&t, 0); - io_timeout(sock, t); + timeout_never(sock); fullscrape_deliver(sock, format); io_dontwantread(sock); return ws->reply_size = -2; diff --git a/ot_timeout.h b/ot_timeout.h new file mode 100644 index 0000000..1027c2e --- /dev/null +++ b/ot_timeout.h @@ -0,0 +1,39 @@ +/* This software was written by Dirk Engling + It is considered beerware. Prost. Skol. Cheers or whatever. + + $id$ */ + +#ifndef OT_TIMEOUT_H__ +#define OT_TIMEOUT_H__ + +#include +#include + +/* Libowfat */ +#include "io.h" + +#include "trackerlogic.h" + +/* io_timeout wants an absolute TAI64 deadline and io_timeouted skips any + socket whose deadline is zero, so that is how a socket is told to wait + indefinitely. + + The deadline is taken from the cached clock rather than from + gettimeofday, which is too expensive to call at this rate on FreeBSD. */ + +static inline void timeout_never(int64_t sock) { + tai6464 t; + + memset(&t, 0, sizeof(t)); + io_timeout(sock, t); +} + +static inline void timeout_in(int64_t sock, unsigned int seconds) { + tai6464 t; + + memset(&t, 0, sizeof(t)); + tai_unix(&t.sec, g_now_seconds + seconds); + io_timeout(sock, t); +} + +#endif -- cgit v1.3