From e1b952cef66a227376363c844660fac82f29a4f0 Mon Sep 17 00:00:00 2001 From: ruti <> Date: Sat, 4 May 2024 16:31:47 +0300 Subject: [PATCH] fix: ignore closed connection, don't use mmap in ftob --- conev.c | 24 +++++++++++++++++++++--- conev.h | 2 ++ main.c | 23 +++++------------------ proxy.c | 2 +- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/conev.c b/conev.c index 10e415c..2c20c17 100644 --- a/conev.c +++ b/conev.c @@ -3,6 +3,7 @@ #include #include +#include struct poolhd *init_pool(int count) @@ -33,6 +34,7 @@ struct poolhd *init_pool(int count) for (int i = 0; i < count; i++) { pool->links[i] = &(pool->items[i]); } + memset(pool->items, 0, sizeof(*pool->items)); return pool; } @@ -40,9 +42,14 @@ struct poolhd *init_pool(int count) struct eval *add_event(struct poolhd *pool, enum eid type, int fd, int e) { - if (pool->count >= pool->max) + if (pool->count >= pool->max) { return 0; + } struct eval *val = pool->links[pool->count]; + if (pool->iters && + val->del_iter == pool->iters) { + return 0; + } memset(val, 0, sizeof(*val)); val->fd = fd; @@ -72,7 +79,7 @@ struct eval *add_event(struct poolhd *pool, enum eid type, void del_event(struct poolhd *pool, struct eval *val) { - if (!val->fd) { + if (val->del_iter) { return; } if (val->buff.data) { @@ -81,6 +88,7 @@ void del_event(struct poolhd *pool, struct eval *val) } close(val->fd); val->fd = 0; + val->del_iter = pool->iters; pool->count--; struct eval *ev = pool->links[pool->count]; @@ -147,6 +155,11 @@ struct eval *next_event(struct poolhd *pool, int *offs, int *type) } *offs = i - 1; *type = pool->pevents[i].events; + + if (pool->iters == UINT_MAX) { + pool->iters = 0; + } + pool->iters++; return pool->pevents[i].data.ptr; } @@ -181,6 +194,11 @@ struct eval *next_event(struct poolhd *pool, int *offs, int *typel) pool->pevents[i].revents = 0; *offs = i - 1; *typel = type; + + if (pool->iters == UINT_MAX) { + pool->iters = 0; + } + pool->iters++; return pool->links[i]; } } @@ -195,4 +213,4 @@ int mod_etype(struct poolhd *pool, struct eval *val, int type, char add) pool->pevents[index].events &= ~type; return 0; } -#endif \ No newline at end of file +#endif diff --git a/conev.h b/conev.h index 9388303..09245b7 100644 --- a/conev.h +++ b/conev.h @@ -80,6 +80,7 @@ struct eval { #ifndef NOEPOLL uint32_t events; #endif + unsigned int del_iter; }; struct poolhd { @@ -93,6 +94,7 @@ struct poolhd { #else struct pollfd *pevents; #endif + unsigned int iters; }; struct poolhd *init_pool(int count); diff --git a/main.c b/main.c index fbf020b..880768c 100644 --- a/main.c +++ b/main.c @@ -221,13 +221,6 @@ char *ftob(const char *str, ssize_t *sl) if (fseek(file, 0, SEEK_SET)) { break; } - #ifndef _WIN32 - buffer = mmap(0, size, PROT_READ, MAP_PRIVATE, fileno(file), 0); - if (buffer == MAP_FAILED) { - buffer = 0; - break; - } - #else if (!(buffer = malloc(size))) { break; } @@ -235,7 +228,6 @@ char *ftob(const char *str, ssize_t *sl) free(buffer); buffer = 0; } - #endif } while (0); if (buffer) { *sl = size; @@ -390,11 +382,6 @@ void *add(void **root, int *n, size_t ss) return p; } -#ifndef _WIN32 -#define FREE_MAP(p, s) munmap(p, s) -#else -#define FREE_MAP(p, s) free(p) -#endif void clear_params(void) { @@ -420,12 +407,12 @@ void clear_params(void) free(s.tlsrec); s.tlsrec = 0; } - if (s.fake_data.data) { - FREE_MAP(s.fake_data.data, s.fake_data.size); + if (s.fake_data.data != 0) { + free(s.fake_data.data); s.fake_data.data = 0; } - if (s.file_ptr) { - FREE_MAP(s.file_ptr, s.file_size); + if (s.file_ptr != 0) { + free(s.file_ptr); s.file_ptr = 0; } } @@ -433,7 +420,7 @@ void clear_params(void) params.dp = 0; } if (oob_data.data != oob_char) { - FREE_MAP(oob_data.data, oob_data.size); + free(oob_data.data); oob_data.data = oob_char; } } diff --git a/proxy.c b/proxy.c index 5fe36de..569d5d0 100644 --- a/proxy.c +++ b/proxy.c @@ -809,7 +809,7 @@ int event_loop(int srvfd) } LOG(LOG_L, "new event: fd: %d, evt: %s\n", val->fd, eid_name[val->type]); - if (!val->fd) { + if (val->del_iter) { continue; } switch (val->type) {