diff options
| author | Soikk | 2025-06-06 21:10:34 +0200 |
|---|---|---|
| committer | Soikk | 2025-06-06 21:10:34 +0200 |
| commit | 422898bc6fb8c48812cb50389fa4f13ba9358881 (patch) | |
| tree | b006ef3a9bbb17587c845634a207b403e1e8ce0a | |
| parent | 3687db3e3817a14e3d8e2be28a5ebec841707cdb (diff) | |
| download | soikk-server-422898bc6fb8c48812cb50389fa4f13ba9358881.tar.xz soikk-server-422898bc6fb8c48812cb50389fa4f13ba9358881.tar.zst | |
Added config
| -rwxr-xr-x | Makefile | 2 | ||||
| -rw-r--r-- | src/config/config.c | 54 | ||||
| -rw-r--r-- | src/config/config.h | 19 | ||||
| -rwxr-xr-x | src/net/net.c | 4 |
4 files changed, 76 insertions, 3 deletions
@@ -12,7 +12,7 @@ else RUNCMD = ./server.exe endif -OBJS = $(addprefix $(OBJDIR)/, bit.o str.o log.o list.o crc64.o dir.o net.o ipc.o) +OBJS = $(addprefix $(OBJDIR)/, bit.o str.o log.o list.o crc64.o dir.o net.o ipc.o config.o) INCL = -I$(LIBDIR) -I$(SRCDIR) diff --git a/src/config/config.c b/src/config/config.c new file mode 100644 index 0000000..5b4c6cc --- /dev/null +++ b/src/config/config.c @@ -0,0 +1,54 @@ +#include "config.h" + + +config read_config(str cfg){ + config conf = {0}; + int off = 0; + while(off < cfg.len){ + while(charisspace(cfg.ptr[off])) off++; + if(cfg.ptr[off] == '#'){ + while(!charislinebreak(cfg.ptr[off])) off++; + continue; + } + str key = sread_delim_f(cfg.ptr + off, charisspace, true); + off += key.len + 1; + while(charisspace(cfg.ptr[off])) off++; + + if(streq(key, sstr("port"))){ + str val = sread_delim_f(cfg.ptr + off, charisspace, true); + off += val.len; + conf.port = (int)strtou(val); + }else if(streq(key, sstr("ipv4"))){ + conf.ipv4 = 1; + }else if(streq(key, sstr("ipv6"))){ + conf.ipv6 = 1; + }else if(streq(key, sstr("root"))){ + str val = sread_delim_f(cfg.ptr + off, charisspace, true); + str trailslash = val.ptr[val.len-1] == '/' ? sstr("") : sstr("/"); + conf.root = dup_strs(val, trailslash); + off += conf.root.len; + }else{ + log_warn("Unexpected entry in configuration: '%.*s'", key.len, key.ptr); + } + while(!charislinebreak(cfg.ptr[off])) off++; + off++; + }; + + printf( + "CONFIGURATION:\n" + "\t- port: %d\n" + "\t- secure: %s\n" + "\t- ipv4: %s\n" + "\t- ipv6: %s\n" + "\t- workers: %d\n" + "\t- root: %s\n", + conf.port, + conf.secure ? "yes" : "no", + conf.ipv4 ? "yes" : "no", + conf.ipv6 ? "yes" : "no", + conf.workers, + conf.root.ptr + ); + + return conf; +} diff --git a/src/config/config.h b/src/config/config.h new file mode 100644 index 0000000..4f04459 --- /dev/null +++ b/src/config/config.h @@ -0,0 +1,19 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#include "str/str.h" +#include "log/log.h" + +typedef struct config { + int port; + int secure : 1; + int ipv4 : 1; + int ipv6 : 1; + int workers; + str root; + +} config; + +config read_config(str cfg); + +#endif diff --git a/src/net/net.c b/src/net/net.c index f4a66d6..91014e7 100755 --- a/src/net/net.c +++ b/src/net/net.c @@ -232,7 +232,7 @@ void terminate_https(http_worker *hw){ int accept_connection(http_worker *hw, char ip[INET_ADDRSTRLEN]){ struct sockaddr_storage caddr; int casize = sizeof(caddr); - log_info("waiting..."); + log_info("Waiting..."); if((hw->csocket = accept(hw->ssocket, (struct sockaddr *)&caddr, (socklen_t*)&casize)) == -1){ log_error("accept_socket() -> accept(): %s", strerror(errno)); return -1; @@ -260,7 +260,7 @@ static inline int worker_read(http_worker *hw, str *buf){ } int receive_request(http_worker *hw, str *request){ - // for some reason SSL_has_pending can return 0 but we can still read data + // SSL_has_pending can return 0 if you havent read any bytes yet (https://stackoverflow.com/questions/6616976/why-does-this-ssl-pending-call-always-return-zero) struct pollfd pfd[1] = { {.fd = hw->csocket, .events = POLLIN } }; while((hw->secure && SSL_has_pending(hw->ssl)) || poll(pfd, 1, 100)){ int new = worker_read(hw, request); |
