diff options
| author | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-31 01:24:18 +0100 |
|---|---|---|
| committer | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-31 01:24:18 +0100 |
| commit | 440cfeee71829308fa1cc9b5aef63beb7ab756dc (patch) | |
| tree | 27f8ca155a4d0b71a5a3b2bb089b83cde6eb7017 | |
| parent | 9a2d71c69518a0448940b8873051ac3a2ee37999 (diff) | |
Refactor all tcp specific code into the ot_tcp module to prepare for multiple concurrent workers
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | opentracker.c | 143 | ||||
| -rw-r--r-- | ot_tcp.c | 157 | ||||
| -rw-r--r-- | ot_tcp.h | 22 |
4 files changed, 185 insertions, 141 deletions
| @@ -69,8 +69,8 @@ CFLAGS_libowfat=-I$(LIBOWFAT_HEADERS) | |||
| 69 | LDFLAGS_libowfat=-L$(LIBOWFAT_LIBRARY) -lowfat | 69 | LDFLAGS_libowfat=-L$(LIBOWFAT_LIBRARY) -lowfat |
| 70 | 70 | ||
| 71 | BINARY =opentracker | 71 | BINARY =opentracker |
| 72 | HEADERS=trackerlogic.h ot_urlencoded.h ot_batch.h ot_buf.h ot_event.h ot_fdstate.h ot_fmt.h ot_hex.h ot_ip.h ot_config.h ot_scan.h ot_socket.h ot_mutex.h ot_random.h ot_stats.h ot_vector.h ot_clean.h ot_udp.h ot_iovec.h ot_fullscrape.h ot_accesslist.h ot_http.h ot_livesync.h ot_rijndael.h | 72 | HEADERS=trackerlogic.h ot_urlencoded.h ot_batch.h ot_buf.h ot_event.h ot_fdstate.h ot_fmt.h ot_hex.h ot_ip.h ot_config.h ot_scan.h ot_socket.h ot_tcp.h ot_mutex.h ot_random.h ot_stats.h ot_vector.h ot_clean.h ot_udp.h ot_iovec.h ot_fullscrape.h ot_accesslist.h ot_http.h ot_livesync.h ot_rijndael.h |
| 73 | SOURCES=opentracker.c trackerlogic.c ot_urlencoded.c ot_batch.c ot_buf.c ot_event.c ot_fdstate.c ot_fmt.c ot_hex.c ot_ip.c ot_config.c ot_scan.c ot_socket.c ot_mutex.c ot_random.c ot_stats.c ot_vector.c ot_clean.c ot_udp.c ot_iovec.c ot_fullscrape.c ot_accesslist.c ot_http.c ot_livesync.c ot_rijndael.c | 73 | SOURCES=opentracker.c trackerlogic.c ot_urlencoded.c ot_batch.c ot_buf.c ot_event.c ot_fdstate.c ot_fmt.c ot_hex.c ot_ip.c ot_config.c ot_scan.c ot_socket.c ot_tcp.c ot_mutex.c ot_random.c ot_stats.c ot_vector.c ot_clean.c ot_udp.c ot_iovec.c ot_fullscrape.c ot_accesslist.c ot_http.c ot_livesync.c ot_rijndael.c |
| 74 | SOURCES_proxy=proxy.c ot_batch.c ot_fdstate.c ot_vector.c ot_mutex.c ot_random.c ot_scan.c ot_ip.c ot_socket.c | 74 | SOURCES_proxy=proxy.c ot_batch.c ot_fdstate.c ot_vector.c ot_mutex.c ot_random.c ot_scan.c ot_ip.c ot_socket.c |
| 75 | 75 | ||
| 76 | all: $(BINARY) $(BINARY).debug | 76 | all: $(BINARY) $(BINARY).debug |
diff --git a/opentracker.c b/opentracker.c index b47759e..2eaf988 100644 --- a/opentracker.c +++ b/opentracker.c | |||
| @@ -35,6 +35,7 @@ | |||
| 35 | #include "ot_fdstate.h" | 35 | #include "ot_fdstate.h" |
| 36 | #include "ot_socket.h" | 36 | #include "ot_socket.h" |
| 37 | #include "ot_stats.h" | 37 | #include "ot_stats.h" |
| 38 | #include "ot_tcp.h" | ||
| 38 | #include "ot_udp.h" | 39 | #include "ot_udp.h" |
| 39 | #include "trackerlogic.h" | 40 | #include "trackerlogic.h" |
| 40 | 41 | ||
| @@ -96,142 +97,6 @@ static void install_signal_handlers(void) { | |||
| 96 | } | 97 | } |
| 97 | 98 | ||
| 98 | 99 | ||
| 99 | static ssize_t header_complete(char *request, ssize_t byte_count) { | ||
| 100 | ssize_t i = 0, state = 0; | ||
| 101 | |||
| 102 | for (i = 1; i < byte_count; i += 2) | ||
| 103 | if (request[i] <= 13) { | ||
| 104 | i--; | ||
| 105 | for (state = 0; i < byte_count; ++i) { | ||
| 106 | char c = request[i]; | ||
| 107 | if (c == '\r' || c == '\n') | ||
| 108 | state = (state >> 2) | ((c << 6) & 0xc0); | ||
| 109 | else | ||
| 110 | break; | ||
| 111 | if (state >= 0xa0 || state == 0x99) | ||
| 112 | return i + 1; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | return 0; | ||
| 116 | } | ||
| 117 | |||
| 118 | static void handle_read(const int64_t sock, struct ot_workstruct *ws) { | ||
| 119 | struct http_data *cookie = fdstate_cookie(sock); | ||
| 120 | ssize_t byte_count = read((int)sock, ws->inbuf, G_INBUF_SIZE); | ||
| 121 | |||
| 122 | if (byte_count == 0 || (byte_count == -1 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)) { | ||
| 123 | http_close(sock); | ||
| 124 | return; | ||
| 125 | } | ||
| 126 | |||
| 127 | if (byte_count == -1) | ||
| 128 | return; | ||
| 129 | |||
| 130 | ws->request = NULL; | ||
| 131 | ws->request_size = 0; | ||
| 132 | ws->header_size = 0; | ||
| 133 | ws->keep_alive = 0; | ||
| 134 | |||
| 135 | /* If we get the whole request in one packet, handle it without copying */ | ||
| 136 | if (!cookie->request.data) { | ||
| 137 | if ((ws->header_size = header_complete(ws->inbuf, byte_count))) { | ||
| 138 | ws->request = ws->inbuf; | ||
| 139 | ws->request_size = byte_count; | ||
| 140 | http_handle_request(sock, ws); | ||
| 141 | } else if (buf_cat(&cookie->request, ws->inbuf, (size_t)byte_count)) | ||
| 142 | http_close(sock); | ||
| 143 | return; | ||
| 144 | } | ||
| 145 | |||
| 146 | if (buf_cat(&cookie->request, ws->inbuf, (size_t)byte_count) || cookie->request.size > G_INBUF_SIZE) { | ||
| 147 | http_issue_error(sock, ws, CODE_HTTPERROR_500); | ||
| 148 | return; | ||
| 149 | } | ||
| 150 | |||
| 151 | while ((ws->header_size = header_complete(cookie->request.data, (ssize_t)cookie->request.size))) { | ||
| 152 | ws->request = cookie->request.data; | ||
| 153 | ws->request_size = (ssize_t)cookie->request.size; | ||
| 154 | http_handle_request(sock, ws); | ||
| 155 | #ifdef WANT_KEEPALIVE | ||
| 156 | if (!ws->keep_alive) | ||
| 157 | #endif | ||
| 158 | return; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | static void handle_write(const int64_t sock) { | ||
| 163 | struct http_data *cookie = fdstate_cookie(sock); | ||
| 164 | int chunked = 0; | ||
| 165 | int keep_alive = 0; | ||
| 166 | |||
| 167 | if (cookie) { | ||
| 168 | chunked = (cookie->flag & STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER) != 0; | ||
| 169 | keep_alive = (cookie->flag & STRUCT_HTTP_FLAG_KEEPALIVE) != 0; | ||
| 170 | |||
| 171 | if (cookie->batch) { | ||
| 172 | if (batch_send(sock, cookie->batch) == BATCH_FAILED) { | ||
| 173 | http_close(sock); | ||
| 174 | return; | ||
| 175 | } | ||
| 176 | if (batch_pending(cookie->batch)) | ||
| 177 | return; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | /* In a chunked transfer, wait for the next piece once everything queued has gone out */ | ||
| 182 | if (chunked) | ||
| 183 | http_interest_set(sock, cookie, 0); | ||
| 184 | else if (keep_alive) { | ||
| 185 | /* The reply is out and the client asked to keep the connection, so go | ||
| 186 | back to waiting for the next request */ | ||
| 187 | http_interest_set(sock, cookie, EVENT_IN); | ||
| 188 | fdstate_set_expiry(sock, time_future_seconds(OT_CLIENT_TIMEOUT)); | ||
| 189 | } else | ||
| 190 | http_close(sock); | ||
| 191 | } | ||
| 192 | |||
| 193 | /* The fast path, assuming that the complete request has usually arrived by the time a | ||
| 194 | connection is accepted, so it is answered here outside the event loop. The more load | ||
| 195 | a tracker has, the more likely it is that by the time it accepts, the roundtrip to | ||
| 196 | the peer has completed. | ||
| 197 | If the request could not be completely handled, the stack cookie is promoted to heap | ||
| 198 | and the connection handed over to the event loop to finish */ | ||
| 199 | static void handle_accept(const int64_t serversocket, struct ot_workstruct *ws) { | ||
| 200 | int64_t sock; | ||
| 201 | ot_ip6 ip; | ||
| 202 | uint16_t port; | ||
| 203 | |||
| 204 | while ((sock = socket_accept(serversocket, ip, &port)) != -1) { | ||
| 205 | struct http_data cookie; | ||
| 206 | uint32_t generation; | ||
| 207 | |||
| 208 | memset(&cookie, 0, sizeof(cookie)); | ||
| 209 | memcpy(cookie.ip, ip, sizeof(ot_ip6)); | ||
| 210 | cookie.interest = EVENT_IN; | ||
| 211 | cookie.flag = STRUCT_HTTP_FLAG_ONSTACK; | ||
| 212 | |||
| 213 | generation = fdstate_open(sock, &cookie, time_future_seconds(OT_CLIENT_TIMEOUT)); | ||
| 214 | if (!fdstate_claim((int)sock, generation)) { | ||
| 215 | fdstate_close(sock); | ||
| 216 | event_close((int)sock); | ||
| 217 | continue; | ||
| 218 | } | ||
| 219 | |||
| 220 | stats_issue_event(ws, EVENT_ACCEPT, FLAG_TCP, (uintptr_t)ip); | ||
| 221 | |||
| 222 | handle_read(sock, ws); | ||
| 223 | |||
| 224 | /* Gone already if it was answered and closed inline */ | ||
| 225 | if (fdstate_generation((int)sock) != generation) | ||
| 226 | continue; | ||
| 227 | |||
| 228 | if (http_adopt(sock, &cookie) == -1) | ||
| 229 | http_close(sock); | ||
| 230 | else | ||
| 231 | fdstate_release((int)sock); | ||
| 232 | } | ||
| 233 | } | ||
| 234 | |||
| 235 | /* How many ready descriptors one wait hands back at a time */ | 100 | /* How many ready descriptors one wait hands back at a time */ |
| 236 | #define OT_EVENT_BATCH 512 | 101 | #define OT_EVENT_BATCH 512 |
| 237 | 102 | ||
| @@ -277,7 +142,7 @@ static void *server_mainloop(void *args) { | |||
| 277 | cookie = fdstate_cookie((int)sock); | 142 | cookie = fdstate_cookie((int)sock); |
| 278 | 143 | ||
| 279 | if ((intptr_t)cookie == FLAG_TCP) | 144 | if ((intptr_t)cookie == FLAG_TCP) |
| 280 | handle_accept(sock, &ws); | 145 | tcp_accept(sock, &ws); |
| 281 | else if ((intptr_t)cookie == FLAG_UDP) | 146 | else if ((intptr_t)cookie == FLAG_UDP) |
| 282 | handle_udp6(sock, &ws); | 147 | handle_udp6(sock, &ws); |
| 283 | else if ((intptr_t)cookie == FLAG_SELFPIPE) | 148 | else if ((intptr_t)cookie == FLAG_SELFPIPE) |
| @@ -285,10 +150,10 @@ static void *server_mainloop(void *args) { | |||
| 285 | ; | 150 | ; |
| 286 | else { | 151 | else { |
| 287 | if (ready[i].what & EVENT_IN) | 152 | if (ready[i].what & EVENT_IN) |
| 288 | handle_read(sock, &ws); | 153 | tcp_read(sock, &ws); |
| 289 | /* The read may have closed it, so look again before writing */ | 154 | /* The read may have closed it, so look again before writing */ |
| 290 | if ((ready[i].what & EVENT_OUT) && fdstate_generation((int)sock) == ready[i].generation) | 155 | if ((ready[i].what & EVENT_OUT) && fdstate_generation((int)sock) == ready[i].generation) |
| 291 | handle_write(sock); | 156 | tcp_write(sock); |
| 292 | } | 157 | } |
| 293 | } | 158 | } |
| 294 | 159 | ||
diff --git a/ot_tcp.c b/ot_tcp.c new file mode 100644 index 0000000..8647a47 --- /dev/null +++ b/ot_tcp.c | |||
| @@ -0,0 +1,157 @@ | |||
| 1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
| 2 | It is considered beerware. Prost. Skol. Cheers or whatever. | ||
| 3 | |||
| 4 | $id$ */ | ||
| 5 | |||
| 6 | /* System */ | ||
| 7 | #include <errno.h> | ||
| 8 | #include <string.h> | ||
| 9 | #include <unistd.h> | ||
| 10 | |||
| 11 | /* Opentracker */ | ||
| 12 | #include "ot_batch.h" | ||
| 13 | #include "ot_buf.h" | ||
| 14 | #include "ot_event.h" | ||
| 15 | #include "ot_fdstate.h" | ||
| 16 | #include "ot_http.h" | ||
| 17 | #include "ot_mutex.h" | ||
| 18 | #include "ot_socket.h" | ||
| 19 | #include "ot_stats.h" | ||
| 20 | #include "ot_tcp.h" | ||
| 21 | #include "trackerlogic.h" | ||
| 22 | |||
| 23 | static ssize_t header_complete(char *request, ssize_t byte_count) { | ||
| 24 | ssize_t i = 0, state = 0; | ||
| 25 | |||
| 26 | for (i = 1; i < byte_count; i += 2) | ||
| 27 | if (request[i] <= 13) { | ||
| 28 | i--; | ||
| 29 | for (state = 0; i < byte_count; ++i) { | ||
| 30 | char c = request[i]; | ||
| 31 | if (c == '\r' || c == '\n') | ||
| 32 | state = (state >> 2) | ((c << 6) & 0xc0); | ||
| 33 | else | ||
| 34 | break; | ||
| 35 | if (state >= 0xa0 || state == 0x99) | ||
| 36 | return i + 1; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | return 0; | ||
| 40 | } | ||
| 41 | |||
| 42 | void tcp_read(const int64_t sock, struct ot_workstruct *ws) { | ||
| 43 | struct http_data *cookie = fdstate_cookie(sock); | ||
| 44 | ssize_t byte_count = read((int)sock, ws->inbuf, G_INBUF_SIZE); | ||
| 45 | |||
| 46 | if (byte_count == 0 || (byte_count == -1 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)) { | ||
| 47 | http_close(sock); | ||
| 48 | return; | ||
| 49 | } | ||
| 50 | |||
| 51 | if (byte_count == -1) | ||
| 52 | return; | ||
| 53 | |||
| 54 | ws->request = NULL; | ||
| 55 | ws->request_size = 0; | ||
| 56 | ws->header_size = 0; | ||
| 57 | ws->keep_alive = 0; | ||
| 58 | |||
| 59 | /* If we get the whole request in one packet, handle it without copying */ | ||
| 60 | if (!cookie->request.data) { | ||
| 61 | if ((ws->header_size = header_complete(ws->inbuf, byte_count))) { | ||
| 62 | ws->request = ws->inbuf; | ||
| 63 | ws->request_size = byte_count; | ||
| 64 | http_handle_request(sock, ws); | ||
| 65 | } else if (buf_cat(&cookie->request, ws->inbuf, (size_t)byte_count)) | ||
| 66 | http_close(sock); | ||
| 67 | return; | ||
| 68 | } | ||
| 69 | |||
| 70 | if (buf_cat(&cookie->request, ws->inbuf, (size_t)byte_count) || cookie->request.size > G_INBUF_SIZE) { | ||
| 71 | http_issue_error(sock, ws, CODE_HTTPERROR_500); | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | |||
| 75 | while ((ws->header_size = header_complete(cookie->request.data, (ssize_t)cookie->request.size))) { | ||
| 76 | ws->request = cookie->request.data; | ||
| 77 | ws->request_size = (ssize_t)cookie->request.size; | ||
| 78 | http_handle_request(sock, ws); | ||
| 79 | #ifdef WANT_KEEPALIVE | ||
| 80 | if (!ws->keep_alive) | ||
| 81 | #endif | ||
| 82 | return; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | void tcp_write(const int64_t sock) { | ||
| 87 | struct http_data *cookie = fdstate_cookie(sock); | ||
| 88 | int chunked = 0; | ||
| 89 | int keep_alive = 0; | ||
| 90 | |||
| 91 | if (cookie) { | ||
| 92 | chunked = (cookie->flag & STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER) != 0; | ||
| 93 | keep_alive = (cookie->flag & STRUCT_HTTP_FLAG_KEEPALIVE) != 0; | ||
| 94 | |||
| 95 | if (cookie->batch) { | ||
| 96 | if (batch_send(sock, cookie->batch) == BATCH_FAILED) { | ||
| 97 | http_close(sock); | ||
| 98 | return; | ||
| 99 | } | ||
| 100 | if (batch_pending(cookie->batch)) | ||
| 101 | return; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | /* In a chunked transfer, wait for the next piece once everything queued has gone out */ | ||
| 106 | if (chunked) | ||
| 107 | http_interest_set(sock, cookie, 0); | ||
| 108 | else if (keep_alive) { | ||
| 109 | /* The reply is out and the client asked to keep the connection, so go | ||
| 110 | back to waiting for the next request */ | ||
| 111 | http_interest_set(sock, cookie, EVENT_IN); | ||
| 112 | fdstate_set_expiry(sock, time_future_seconds(OT_CLIENT_TIMEOUT)); | ||
| 113 | } else | ||
| 114 | http_close(sock); | ||
| 115 | } | ||
| 116 | |||
| 117 | /* The fast path, assuming that the complete request has usually arrived by the time a | ||
| 118 | connection is accepted, so it is answered here outside the event loop. The more load | ||
| 119 | a tracker has, the more likely it is that by the time it accepts, the roundtrip to | ||
| 120 | the peer has completed. | ||
| 121 | If the request could not be completely handled, the stack cookie is promoted to heap | ||
| 122 | and the connection handed over to the event loop to finish */ | ||
| 123 | void tcp_accept(const int64_t serversocket, struct ot_workstruct *ws) { | ||
| 124 | int64_t sock; | ||
| 125 | ot_ip6 ip; | ||
| 126 | uint16_t port; | ||
| 127 | |||
| 128 | while ((sock = socket_accept(serversocket, ip, &port)) != -1) { | ||
| 129 | struct http_data cookie; | ||
| 130 | uint32_t generation; | ||
| 131 | |||
| 132 | memset(&cookie, 0, sizeof(cookie)); | ||
| 133 | memcpy(cookie.ip, ip, sizeof(ot_ip6)); | ||
| 134 | cookie.interest = EVENT_IN; | ||
| 135 | cookie.flag = STRUCT_HTTP_FLAG_ONSTACK; | ||
| 136 | |||
| 137 | generation = fdstate_open(sock, &cookie, time_future_seconds(OT_CLIENT_TIMEOUT)); | ||
| 138 | if (!fdstate_claim((int)sock, generation)) { | ||
| 139 | fdstate_close(sock); | ||
| 140 | event_close((int)sock); | ||
| 141 | continue; | ||
| 142 | } | ||
| 143 | |||
| 144 | stats_issue_event(ws, EVENT_ACCEPT, FLAG_TCP, (uintptr_t)ip); | ||
| 145 | |||
| 146 | tcp_read(sock, ws); | ||
| 147 | |||
| 148 | /* Gone already if it was answered and closed inline */ | ||
| 149 | if (fdstate_generation((int)sock) != generation) | ||
| 150 | continue; | ||
| 151 | |||
| 152 | if (http_adopt(sock, &cookie) == -1) | ||
| 153 | http_close(sock); | ||
| 154 | else | ||
| 155 | fdstate_release((int)sock); | ||
| 156 | } | ||
| 157 | } | ||
diff --git a/ot_tcp.h b/ot_tcp.h new file mode 100644 index 0000000..b0a93f4 --- /dev/null +++ b/ot_tcp.h | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
| 2 | It is considered beerware. Prost. Skol. Cheers or whatever. | ||
| 3 | |||
| 4 | $id$ */ | ||
| 5 | |||
| 6 | #ifndef OT_TCP_H__ | ||
| 7 | #define OT_TCP_H__ | ||
| 8 | |||
| 9 | #include <stdint.h> | ||
| 10 | |||
| 11 | #include "trackerlogic.h" | ||
| 12 | |||
| 13 | /* Take whatever connections are waiting on a listening socket and answer | ||
| 14 | them, each on this thread's stack, handing a connection that cannot be | ||
| 15 | finished to the event loop */ | ||
| 16 | void tcp_accept(const int64_t serversocket, struct ot_workstruct *ws); | ||
| 17 | |||
| 18 | /* Handler connections in the event loop has become readable or writable */ | ||
| 19 | void tcp_read(const int64_t sock, struct ot_workstruct *ws); | ||
| 20 | void tcp_write(const int64_t sock); | ||
| 21 | |||
| 22 | #endif | ||
