diff --git a/Makefile b/Makefile index 4afdb62..00fad82 100644 --- a/Makefile +++ b/Makefile @@ -45,8 +45,9 @@ bin/lib/%.o: src/lib/%.c bin/worker/%.o: src/worker/%.c $(CC) -c -o $@ $(CFLAGS) $< -bin/sesimos: bin/server.o bin/logger.o bin/cache_handler.o bin/async.o \ +bin/sesimos: bin/server.o bin/logger.o bin/cache_handler.o bin/async.o bin/workers.o \ bin/worker/request_handler.o bin/worker/tcp_acceptor.o bin/worker/tcp_closer.o bin/worker/responder.o \ + bin/worker/fastcgi_handler.o \ bin/lib/compress.o bin/lib/config.o bin/lib/fastcgi.o bin/lib/geoip.o \ bin/lib/http.o bin/lib/http_static.o bin/lib/proxy.o bin/lib/sock.o bin/lib/uri.o \ bin/lib/utils.o bin/lib/websocket.o bin/lib/mpmc.o @@ -62,6 +63,8 @@ bin/cache_handler.o: src/cache_handler.h src/lib/utils.h src/lib/uri.h src/lib/c bin/async.o: src/async.h src/logger.h +bin/workers.o: src/workers.h src/lib/mpmc.h src/worker/*.h + bin/worker/request_handler.o: src/worker/request_handler.h bin/worker/tcp_acceptor.o: src/worker/tcp_acceptor.h diff --git a/src/server.c b/src/server.c index 16e538f..ff562b5 100644 --- a/src/server.c +++ b/src/server.c @@ -10,15 +10,12 @@ #include "server.h" #include "logger.h" #include "async.h" -#include "worker/tcp_acceptor.h" #include "cache_handler.h" #include "lib/config.h" #include "lib/proxy.h" #include "lib/geoip.h" -#include "worker/tcp_closer.h" -#include "worker/request_handler.h" -#include "worker/responder.h" +#include "workers.h" #include #include @@ -27,7 +24,6 @@ #include #include #include -#include #include #include #include @@ -111,15 +107,8 @@ static void terminate_gracefully(int sig) { signal(SIGINT, terminate_forcefully); signal(SIGTERM, terminate_forcefully); - tcp_acceptor_stop(); - request_handler_stop(); - tcp_closer_stop(); - responder_stop(); - - tcp_acceptor_destroy(); - request_handler_destroy(); - tcp_closer_destroy(); - responder_destroy(); + workers_stop(); + workers_destroy(); for (int i = 0; i < NUM_SOCKETS; i++) { close(sockets[i]); @@ -264,10 +253,7 @@ int main(int argc, char *const argv[]) { } } - tcp_acceptor_init(CNX_HANDLER_WORKERS, 64); - tcp_closer_init(CNX_HANDLER_WORKERS, 64); - request_handler_init(REQ_HANDLER_WORKERS, 64); - responder_init(REQ_HANDLER_WORKERS, 64); + workers_init(); for (int i = 0; i < NUM_SOCKETS; i++) { async(sockets[i], POLLIN, ASYNC_KEEP, accept_cb, &sockets[i], accept_err_cb, &sockets[i]); diff --git a/src/worker/fastcgi_handler.c b/src/worker/fastcgi_handler.c index 20c43a2..4138d9a 100644 --- a/src/worker/fastcgi_handler.c +++ b/src/worker/fastcgi_handler.c @@ -8,30 +8,9 @@ #include "fastcgi_handler.h" #include "../logger.h" -#include "../lib/mpmc.h" #include "../lib/utils.h" -static mpmc_t mpmc_ctx; - -static void fastcgi_handler_func(client_ctx_t *ctx); - -int fastcgi_handler_init(int n_workers, int buf_size) { - return mpmc_init(&mpmc_ctx, n_workers, buf_size, (void (*)(void *)) fastcgi_handler_func, "fcgi"); -} - -int fastcgi_handle(client_ctx_t *ctx) { - return mpmc_queue(&mpmc_ctx, ctx); -} - -void fastcgi_handler_stop(void) { - mpmc_stop(&mpmc_ctx); -} - -void fastcgi_handler_destroy(void) { - mpmc_destroy(&mpmc_ctx); -} - -static void fastcgi_handler_func(client_ctx_t *ctx) { +void fastcgi_handler_func(client_ctx_t *ctx) { logger_set_prefix("[%s%*s%s]%s", BLD_STR, INET6_ADDRSTRLEN, ctx->req_host, CLR_STR, ctx->log_prefix); // TODO } diff --git a/src/worker/fastcgi_handler.h b/src/worker/fastcgi_handler.h index bcbe14d..d8c9791 100644 --- a/src/worker/fastcgi_handler.h +++ b/src/worker/fastcgi_handler.h @@ -11,12 +11,6 @@ #include "../server.h" -int fastcgi_handler_init(int n_workers, int buf_size); - -int fastcgi_handle(client_ctx_t *ctx); - -void fastcgi_handler_stop(void); - -void fastcgi_handler_destroy(void); +void fastcgi_handler_func(client_ctx_t *ctx); #endif //SESIMOS_FASTCGI_HANDLER_H diff --git a/src/worker/request_handler.c b/src/worker/request_handler.c index 1419388..7d7373e 100644 --- a/src/worker/request_handler.c +++ b/src/worker/request_handler.c @@ -8,52 +8,23 @@ #include "../defs.h" #include "request_handler.h" +#include "../workers.h" #include "../lib/mpmc.h" -#include "tcp_closer.h" -#include "../async.h" - -#include "../server.h" #include "../logger.h" #include "../lib/utils.h" -#include "../lib/config.h" -#include "../lib/sock.h" -#include "../lib/http.h" #include "../lib/proxy.h" #include "../lib/fastcgi.h" -#include "../cache_handler.h" #include "../lib/compress.h" #include "../lib/websocket.h" -#include "responder.h" #include -#include -#include #include #include -static mpmc_t mpmc_ctx; - -static void request_handler_func(client_ctx_t *ctx); static int request_handler(client_ctx_t *ctx); -int request_handler_init(int n_workers, int buf_size) { - return mpmc_init(&mpmc_ctx, n_workers, buf_size, (void (*)(void *)) request_handler_func, "req"); -} - -int handle_request(client_ctx_t *ctx) { - return mpmc_queue(&mpmc_ctx, ctx); -} - -void request_handler_stop(void) { - mpmc_stop(&mpmc_ctx); -} - -void request_handler_destroy(void) { - mpmc_destroy(&mpmc_ctx); -} - -static void request_handler_func(client_ctx_t *ctx) { +void request_handler_func(client_ctx_t *ctx) { logger_set_prefix("[%*s]%s", INET6_ADDRSTRLEN, ctx->s_addr, ctx->log_prefix); if (request_handler(ctx) == 0) { diff --git a/src/worker/request_handler.h b/src/worker/request_handler.h index 2424146..9c552a8 100644 --- a/src/worker/request_handler.h +++ b/src/worker/request_handler.h @@ -11,12 +11,6 @@ #include "../server.h" -int request_handler_init(int n_workers, int buf_size); - -int handle_request(client_ctx_t *ctx); - -void request_handler_stop(void); - -void request_handler_destroy(void); +void request_handler_func(client_ctx_t *ctx); #endif //SESIMOS_REQUEST_HANDLER_H diff --git a/src/worker/responder.c b/src/worker/responder.c index 188aa2f..fb649dc 100644 --- a/src/worker/responder.c +++ b/src/worker/responder.c @@ -8,48 +8,23 @@ #include "../defs.h" #include "responder.h" -#include "../lib/mpmc.h" -#include "tcp_closer.h" #include "../async.h" #include "../logger.h" #include "../lib/utils.h" -#include "../lib/config.h" -#include "../lib/sock.h" -#include "../lib/http.h" #include "../lib/proxy.h" #include "../lib/fastcgi.h" -#include "../lib/compress.h" #include "../lib/websocket.h" -#include "request_handler.h" +#include "../workers.h" #include #include #include #include -static mpmc_t mpmc_ctx; - -static void responder_func(client_ctx_t *ctx); static void responder(client_ctx_t *ctx); -int responder_init(int n_workers, int buf_size) { - return mpmc_init(&mpmc_ctx, n_workers, buf_size, (void (*)(void *)) responder_func, "res"); -} - -int respond(client_ctx_t *ctx) { - return mpmc_queue(&mpmc_ctx, ctx); -} - -void responder_stop(void) { - mpmc_stop(&mpmc_ctx); -} - -void responder_destroy(void) { - mpmc_destroy(&mpmc_ctx); -} - -static void responder_func(client_ctx_t *ctx) { +void responder_func(client_ctx_t *ctx) { logger_set_prefix("[%s%*s%s]%s", BLD_STR, INET6_ADDRSTRLEN, ctx->req_host, CLR_STR, ctx->log_prefix); responder(ctx); diff --git a/src/worker/responder.h b/src/worker/responder.h index 4fae92f..9216f3c 100644 --- a/src/worker/responder.h +++ b/src/worker/responder.h @@ -11,12 +11,6 @@ #include "../server.h" -int responder_init(int n_workers, int buf_size); - -int respond(client_ctx_t *ctx); - -void responder_stop(void); - -void responder_destroy(void); +void responder_func(client_ctx_t *ctx); #endif //SESIMOS_RESPONDER_H diff --git a/src/worker/tcp_acceptor.c b/src/worker/tcp_acceptor.c index b82559b..49fc148 100644 --- a/src/worker/tcp_acceptor.c +++ b/src/worker/tcp_acceptor.c @@ -6,43 +6,20 @@ * @date 2022-12-28 */ -#include "../server.h" +#include "tcp_acceptor.h" #include "../async.h" #include "../logger.h" #include "../lib/mpmc.h" #include "../lib/utils.h" #include "../lib/geoip.h" -#include "../lib/config.h" -#include "tcp_closer.h" -#include "request_handler.h" -#include "tcp_acceptor.h" +#include "../workers.h" #include #include #include #include -static mpmc_t mpmc_ctx; - -static void tcp_acceptor_func(client_ctx_t *ctx); - -int tcp_acceptor_init(int n_workers, int buf_size) { - return mpmc_init(&mpmc_ctx, n_workers, buf_size, (void (*)(void *)) tcp_acceptor_func, "tcp_a"); -} - -int tcp_accept(client_ctx_t *ctx) { - return mpmc_queue(&mpmc_ctx, ctx); -} - -void tcp_acceptor_stop(void) { - mpmc_stop(&mpmc_ctx); -} - -void tcp_acceptor_destroy(void) { - mpmc_destroy(&mpmc_ctx); -} - -static void tcp_acceptor_func(client_ctx_t *ctx) { +void tcp_acceptor_func(client_ctx_t *ctx) { struct sockaddr_in6 server_addr; inet_ntop(ctx->socket.addr.ipv6.sin6_family, &ctx->socket.addr.ipv6.sin6_addr, ctx->_c_addr, sizeof(ctx->_c_addr)); diff --git a/src/worker/tcp_acceptor.h b/src/worker/tcp_acceptor.h index dc78232..52e8d1e 100644 --- a/src/worker/tcp_acceptor.h +++ b/src/worker/tcp_acceptor.h @@ -11,12 +11,6 @@ #include "../server.h" -int tcp_acceptor_init(int n_workers, int buf_size); - -int tcp_accept(client_ctx_t *ctx); - -void tcp_acceptor_stop(void); - -void tcp_acceptor_destroy(void); +void tcp_acceptor_func(client_ctx_t *ctx); #endif //SESIMOS_TCP_ACCEPTOR_H diff --git a/src/worker/tcp_closer.c b/src/worker/tcp_closer.c index dd4abd3..69dbf32 100644 --- a/src/worker/tcp_closer.c +++ b/src/worker/tcp_closer.c @@ -8,32 +8,11 @@ #include "tcp_closer.h" #include "../logger.h" -#include "../lib/mpmc.h" #include "../lib/utils.h" #include -static mpmc_t mpmc_ctx; - -static void tcp_closer_func(client_ctx_t *ctx); - -int tcp_closer_init(int n_workers, int buf_size) { - return mpmc_init(&mpmc_ctx, n_workers, buf_size, (void (*)(void *)) tcp_closer_func, "tcp_c"); -} - -int tcp_close(client_ctx_t *ctx) { - return mpmc_queue(&mpmc_ctx, ctx); -} - -void tcp_closer_stop(void) { - mpmc_stop(&mpmc_ctx); -} - -void tcp_closer_destroy(void) { - mpmc_destroy(&mpmc_ctx); -} - -static void tcp_closer_func(client_ctx_t *ctx) { +void tcp_closer_func(client_ctx_t *ctx) { logger_set_prefix("[%*s]%s", INET6_ADDRSTRLEN, ctx->s_addr, ctx->log_prefix); sock_close(&ctx->socket); diff --git a/src/worker/tcp_closer.h b/src/worker/tcp_closer.h index 8b864ad..318a351 100644 --- a/src/worker/tcp_closer.h +++ b/src/worker/tcp_closer.h @@ -11,12 +11,6 @@ #include "../server.h" -int tcp_closer_init(int n_workers, int buf_size); - -int tcp_close(client_ctx_t *ctx); - -void tcp_closer_stop(void); - -void tcp_closer_destroy(void); +void tcp_closer_func(client_ctx_t *ctx); #endif //SESIMOS_TCP_CLOSER_H diff --git a/src/workers.c b/src/workers.c new file mode 100644 index 0000000..3e0048a --- /dev/null +++ b/src/workers.c @@ -0,0 +1,62 @@ +/** + * sesimos - secure, simple, modern web server + * @brief Worker interface + * @file src/workers.c + * @author Lorenz Stechauner + * @date 2022-12-29 + */ + +#include "workers.h" +#include "lib/mpmc.h" + +#include "worker/tcp_acceptor.h" +#include "worker/tcp_closer.h" +#include "worker/responder.h" +#include "worker/request_handler.h" +#include "worker/fastcgi_handler.h" + +static mpmc_t tcp_acceptor_ctx, tcp_closer_ctx, request_handler_ctx, responder_ctx, fastcgi_handler_cxt; + +int workers_init(void) { + mpmc_init(&tcp_acceptor_ctx, 8, 64, (void (*)(void *)) tcp_acceptor_func, "tcp_a"); + mpmc_init(&tcp_closer_ctx, 8, 64, (void (*)(void *)) tcp_closer_func, "tcp_c"); + mpmc_init(&request_handler_ctx, 16, 64, (void (*)(void *)) request_handler_func, "req"); + mpmc_init(&responder_ctx, 16, 64, (void (*)(void *)) responder_func, "res"); + mpmc_init(&fastcgi_handler_cxt, 16, 64, (void (*)(void *)) fastcgi_handler_func, "fcgi"); + return -1; +} + +void workers_stop(void) { + mpmc_stop(&tcp_acceptor_ctx); + mpmc_stop(&request_handler_ctx); + mpmc_stop(&responder_ctx); + mpmc_stop(&tcp_closer_ctx); +} + +void workers_destroy(void) { + mpmc_destroy(&tcp_acceptor_ctx); + mpmc_destroy(&request_handler_ctx); + mpmc_destroy(&responder_ctx); + mpmc_destroy(&tcp_closer_ctx); +} + +int tcp_accept(client_ctx_t *ctx) { + return mpmc_queue(&tcp_acceptor_ctx, ctx); +} + +int tcp_close(client_ctx_t *ctx) { + return mpmc_queue(&tcp_closer_ctx, ctx); +} + +int handle_request(client_ctx_t *ctx) { + return mpmc_queue(&request_handler_ctx, ctx); +} + +int respond(client_ctx_t *ctx) { + return mpmc_queue(&responder_ctx, ctx); +} + +int fastcgi_handle(client_ctx_t *ctx) { + return mpmc_queue(&fastcgi_handler_cxt, ctx); +} + diff --git a/src/workers.h b/src/workers.h new file mode 100644 index 0000000..a355e55 --- /dev/null +++ b/src/workers.h @@ -0,0 +1,30 @@ +/** + * sesimos - secure, simple, modern web server + * @brief Worker interface (header file) + * @file src/workers.h + * @author Lorenz Stechauner + * @date 2022-12-29 + */ + +#ifndef SESIMOS_WORKERS_H +#define SESIMOS_WORKERS_H + +#include "server.h" + +int workers_init(void); + +void workers_stop(void); + +void workers_destroy(void); + +int tcp_accept(client_ctx_t *ctx); + +int tcp_close(client_ctx_t *ctx); + +int handle_request(client_ctx_t *ctx); + +int respond(client_ctx_t *ctx); + +int fastcgi_handle(client_ctx_t *ctx); + +#endif //SESIMOS_WORKERS_H