summaryrefslogtreecommitdiff
path: root/opentracker.c
diff options
context:
space:
mode:
Diffstat (limited to 'opentracker.c')
-rw-r--r--opentracker.c36
1 files changed, 19 insertions, 17 deletions
diff --git a/opentracker.c b/opentracker.c
index 50fff29..7de6c6a 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -34,6 +34,7 @@
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_stats.h" 39#include "ot_stats.h"
39#include "ot_udp.h" 40#include "ot_udp.h"
@@ -117,7 +118,7 @@ static ssize_t header_complete(char *request, ssize_t byte_count) {
117} 118}
118 119
119static void handle_dead(const int64_t sock) { 120static void handle_dead(const int64_t sock) {
120 struct http_data *cookie = io_getcookie(sock); 121 struct http_data *cookie = fdstate_cookie(sock);
121 if (cookie) { 122 if (cookie) {
122 if (cookie->batch) 123 if (cookie->batch)
123 batch_reset(cookie->batch); 124 batch_reset(cookie->batch);
@@ -127,11 +128,13 @@ static void handle_dead(const int64_t sock) {
127 mutex_workqueue_canceltask(sock); 128 mutex_workqueue_canceltask(sock);
128 free(cookie); 129 free(cookie);
129 } 130 }
131 /* The generation has to move before the kernel can reissue the number */
132 fdstate_close(sock);
130 io_close(sock); 133 io_close(sock);
131} 134}
132 135
133static void handle_read(const int64_t sock, struct ot_workstruct *ws) { 136static void handle_read(const int64_t sock, struct ot_workstruct *ws) {
134 struct http_data *cookie = io_getcookie(sock); 137 struct http_data *cookie = fdstate_cookie(sock);
135 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);
136 139
137 if (byte_count == 0 || byte_count == -3) { 140 if (byte_count == 0 || byte_count == -3) {
@@ -175,7 +178,7 @@ static void handle_read(const int64_t sock, struct ot_workstruct *ws) {
175} 178}
176 179
177static void handle_write(const int64_t sock) { 180static void handle_write(const int64_t sock) {
178 struct http_data *cookie = io_getcookie(sock); 181 struct http_data *cookie = fdstate_cookie(sock);
179 int chunked = 0; 182 int chunked = 0;
180 183
181 if (cookie) { 184 if (cookie) {
@@ -204,7 +207,6 @@ static void handle_accept(const int64_t serversocket) {
204 int64_t sock; 207 int64_t sock;
205 ot_ip6 ip; 208 ot_ip6 ip;
206 uint16_t port; 209 uint16_t port;
207 tai6464 t;
208 210
209 while ((sock = socket_accept(serversocket, ip, &port)) != -1) { 211 while ((sock = socket_accept(serversocket, ip, &port)) != -1) {
210 212
@@ -218,16 +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 /* That breaks taia encapsulation. But there is no way to take system
227 time this often in FreeBSD and libowfat does not allow to set unix time */
228 taia_uint(&t, 0); /* Clear t */
229 tai_unix(&(t.sec), (g_now_seconds + OT_CLIENT_TIMEOUT));
230 io_timeout(sock, t);
231 } 228 }
232 io_eagain(serversocket); 229 io_eagain(serversocket);
233} 230}
@@ -259,7 +256,7 @@ static void *server_mainloop(void *args) {
259 io_wait(); 256 io_wait();
260 257
261 while ((sock = io_canread()) != -1) { 258 while ((sock = io_canread()) != -1) {
262 const void *cookie = io_getcookie(sock); 259 const void *cookie = fdstate_cookie(sock);
263 if ((intptr_t)cookie == FLAG_TCP) 260 if ((intptr_t)cookie == FLAG_TCP)
264 handle_accept(sock); 261 handle_accept(sock);
265 else if ((intptr_t)cookie == FLAG_UDP) 262 else if ((intptr_t)cookie == FLAG_UDP)
@@ -277,7 +274,9 @@ static void *server_mainloop(void *args) {
277 handle_write(sock); 274 handle_write(sock);
278 275
279 if (g_now_seconds > next_timeout_check) { 276 if (g_now_seconds > next_timeout_check) {
280 while ((sock = io_timeouted()) != -1) 277 int cursor = 0;
278
279 while ((sock = fdstate_next_expired(g_now_seconds, &cursor)) != -1)
281 handle_dead(sock); 280 handle_dead(sock);
282 next_timeout_check = g_now_seconds + OT_CLIENT_TIMEOUT_CHECKINTERVAL; 281 next_timeout_check = g_now_seconds + OT_CLIENT_TIMEOUT_CHECKINTERVAL;
283 } 282 }
@@ -323,7 +322,7 @@ static void listen_bind_one(ot_listen *entry) {
323 if (!io_fd(entry->sock)) 322 if (!io_fd(entry->sock))
324 panic("io_fd"); 323 panic("io_fd");
325 324
326 io_setcookie(entry->sock, (void *)entry->proto); 325 fdstate_open(entry->sock, (void *)entry->proto, 0);
327 io_wantread(entry->sock); 326 io_wantread(entry->sock);
328 327
329#ifdef _DEBUG 328#ifdef _DEBUG
@@ -376,14 +375,14 @@ void load_state(const char *const state_filename) {
376 while (fgets(inbuf, sizeof(inbuf), state_filehandle)) { 375 while (fgets(inbuf, sizeof(inbuf), state_filehandle)) {
377 size_t i; 376 size_t i;
378 377
379 if (!hex_tohash(infohash, inbuf)) 378 if (!hex_to_hash(infohash, inbuf))
380 continue; 379 continue;
381 i = 2 * sizeof(ot_hash); 380 i = 2 * sizeof(ot_hash);
382 381
383 if (inbuf[i++] != ':' || !(consumed = scan_udec(inbuf + i, &base, UINT64_MAX))) 382 if (inbuf[i++] != ':' || !(consumed = scan_uint(inbuf + i, &base, UINT64_MAX)))
384 continue; 383 continue;
385 i += consumed; 384 i += consumed;
386 if (inbuf[i++] != ':' || !(consumed = scan_udec(inbuf + i, &downcount, UINT64_MAX))) 385 if (inbuf[i++] != ':' || !(consumed = scan_uint(inbuf + i, &downcount, UINT64_MAX)))
387 continue; 386 continue;
388 add_torrent_from_saved_state(infohash, base, downcount); 387 add_torrent_from_saved_state(infohash, base, downcount);
389 } 388 }
@@ -452,6 +451,8 @@ int main(int argc, char **argv) {
452 pthread_t thread_id; /* time cacher */ 451 pthread_t thread_id; /* time cacher */
453 452
454 random_init(); 453 random_init();
454 if (fdstate_init() == -1)
455 panic("fdstate_init");
455 config_parse(argc, argv); 456 config_parse(argc, argv);
456 457
457 listen_bind_all(); 458 listen_bind_all();
@@ -477,7 +478,7 @@ int main(int argc, char **argv) {
477 panic("selfpipe io_fd failed: "); 478 panic("selfpipe io_fd failed: ");
478 if (!io_fd(g_self_pipe[1])) 479 if (!io_fd(g_self_pipe[1]))
479 panic("selfpipe io_fd failed: "); 480 panic("selfpipe io_fd failed: ");
480 io_setcookie(g_self_pipe[0], (void *)FLAG_SELFPIPE); 481 fdstate_open(g_self_pipe[0], (void *)FLAG_SELFPIPE, 0);
481 io_wantread(g_self_pipe[0]); 482 io_wantread(g_self_pipe[0]);
482 483
483 /* Init all sub systems. This call may fail with an exit() */ 484 /* Init all sub systems. This call may fail with an exit() */
@@ -498,5 +499,6 @@ int main(int argc, char **argv) {
498 499
499 server_mainloop(0); 500 server_mainloop(0);
500 501
502 fdstate_deinit();
501 return 0; 503 return 0;
502} 504}