From 127b779905721b438a8efc15096dd8940ccd14f2 Mon Sep 17 00:00:00 2001 From: Max <51083570+DRdrProfessor@users.noreply.github.com> Date: Tue, 26 Dec 2023 02:55:18 +0100 Subject: [PATCH] Building base. --- src/net/CMakeLists.txt | 6 +- src/net/configuration.c | 16 ++++ src/net/include/CMakeLists.txt | 5 +- src/net/include/netex.h | 5 ++ src/net/include/netexssl.h | 11 --- src/net/include/netexthreads.h | 10 --- src/net/include/storage.h | 19 ++++ src/net/netex.c | 24 ++++-- src/net/storage.c | 153 +++++++++++++++++++++++++++++++++ src/server/server.c | 27 +++++- 10 files changed, 242 insertions(+), 34 deletions(-) create mode 100644 src/net/configuration.c delete mode 100644 src/net/include/netexssl.h delete mode 100644 src/net/include/netexthreads.h create mode 100644 src/net/include/storage.h create mode 100644 src/net/storage.c diff --git a/src/net/CMakeLists.txt b/src/net/CMakeLists.txt index 8848b80..a25f239 100644 --- a/src/net/CMakeLists.txt +++ b/src/net/CMakeLists.txt @@ -1,10 +1,14 @@ # netex src add_library(netex SHARED - "netex.c") + "configuration.c" + "netex.c" + "storage.c") find_package(OpenSSL REQUIRED) target_link_libraries(netex OpenSSL::SSL) +find_package(json-c REQUIRED) +target_link_libraries(netex json-c) target_include_directories(netex PRIVATE ${CMAKE_SOURCE_DIR}/src/net/include) diff --git a/src/net/configuration.c b/src/net/configuration.c new file mode 100644 index 0000000..4bc76eb --- /dev/null +++ b/src/net/configuration.c @@ -0,0 +1,16 @@ +#include "netex.h" + +void config_set(const char* key, const char* value) +{ + +} + +char* config_get(const char* key) +{ + return "test"; +} + +char* config_get_default(const char* key, const char* default_value) +{ + return ""; +} \ No newline at end of file diff --git a/src/net/include/CMakeLists.txt b/src/net/include/CMakeLists.txt index f3d3b9d..180d4c3 100644 --- a/src/net/include/CMakeLists.txt +++ b/src/net/include/CMakeLists.txt @@ -3,6 +3,5 @@ target_sources(netex PUBLIC "net.h" "netex.h" - "netexssl.h" - "netexthreads.h" - "sockets.h") \ No newline at end of file + "sockets.h" + "storage.h") \ No newline at end of file diff --git a/src/net/include/netex.h b/src/net/include/netex.h index b5a3f19..771f7b0 100644 --- a/src/net/include/netex.h +++ b/src/net/include/netex.h @@ -1,9 +1,14 @@ #ifndef NETEX_H #define NETEX_H +#include + #define PRINT(msg, args...) fprintf(stdout, msg, ##args) #define PRINT_LINE(msg, args...) fprintf(stdout, msg "\n", ##args) #define WARN(msg, args...) fprintf(stdout, "[Warning]: " msg "\n", ##args) #define ERROR(msg, args...) fprintf(stderr, "[ERROR]: " msg "\n", ##args) +void netex_init(void); +void netex_shutdown(void); + #endif //NETEX_H \ No newline at end of file diff --git a/src/net/include/netexssl.h b/src/net/include/netexssl.h deleted file mode 100644 index b0d24cc..0000000 --- a/src/net/include/netexssl.h +++ /dev/null @@ -1,11 +0,0 @@ -// -// Created by max on 24-12-23. -// - -#ifndef NETEXSSL_H -#define NETEXSSL_H - -void init_openssl(void); -void shutdown_openssl(void); - -#endif //NETEXSSL_H diff --git a/src/net/include/netexthreads.h b/src/net/include/netexthreads.h deleted file mode 100644 index c5220d5..0000000 --- a/src/net/include/netexthreads.h +++ /dev/null @@ -1,10 +0,0 @@ -// -// Created by max on 24-12-23. -// - -#ifndef NETEXTHREADS_H -#define NETEXTHREADS_H - -#include - -#endif //NETEXTHREADS_H diff --git a/src/net/include/storage.h b/src/net/include/storage.h new file mode 100644 index 0000000..0e0e05f --- /dev/null +++ b/src/net/include/storage.h @@ -0,0 +1,19 @@ +// +// Created by max on 25-12-23. +// + +#ifndef STORAGE_H +#define STORAGE_H +#include "netex.h" + +struct netex_configuration +{ + char* server_name; + size_t port; + char* certificate_path; +}; + +int save_config(void); +int load_config(void); + +#endif //STORAGE_H diff --git a/src/net/netex.c b/src/net/netex.c index a49a9d7..f43570e 100644 --- a/src/net/netex.c +++ b/src/net/netex.c @@ -1,20 +1,32 @@ -#include "netex.h" -#include "netexssl.h" #include #include #include +#include "netex.h" +#include "storage.h" -void init_openssl(void) + +void netex_init(void) { + // Config + const int config_loaded = load_config(); + if (config_loaded != 0) + { + ERROR("Failed to load config! Shutting down..."); + exit(EXIT_FAILURE); + } + + // OpenSSL PRINT_LINE("Initializing OpenSSL..."); SSL_load_error_strings(); SSL_library_init(); OpenSSL_add_all_algorithms(); - PRINT_LINE("OpenSSL initialized!"); } -void shutdown_openssl(void) +void netex_shutdown(void) { - PRINT_LINE("Shuttingdown OpenSSL..."); + // Config + save_config(); + + // OpenSSL ERR_free_strings(); EVP_cleanup(); } \ No newline at end of file diff --git a/src/net/storage.c b/src/net/storage.c new file mode 100644 index 0000000..09f38c3 --- /dev/null +++ b/src/net/storage.c @@ -0,0 +1,153 @@ +#include +#include +#include +#include +#include +#include +#include "storage.h" + +#define CONFIG_FILE_NAME "config.json" +#define PROP_SRV_NAME "server_name" +#define PROP_PORT "port" +#define PROP_CERT "certificate_path" + +const char* config_to_json(const struct netex_configuration* net_config); +struct netex_configuration* create_config(const char* server_name, const size_t port); +void set_config(struct netex_configuration* config); +struct netex_configuration* json_to_config(const char* json); + +struct netex_configuration* loaded_configuration; + +int save_config(void) +{ + if (loaded_configuration == NULL) + return 0; // Nothing to save + FILE* config_file = fopen(CONFIG_FILE_NAME, "w"); + if (config_file == NULL) + { + const int error = errno; + ERROR("Failed to open file '%s', code %i", CONFIG_FILE_NAME, error); + return -1; + } + const char* json = config_to_json(loaded_configuration); + const size_t json_len = strlen(json); + const int write_result = fwrite(json, 1, json_len, config_file); + fclose(config_file); + if (write_result <= 0) + { + const int write_error = errno; + ERROR("Failed to write config file to disk! Error: %i", write_error); + return -1; + } + return 0; +} +int load_config(void) +{ + struct stat config_stat; + if (lstat(CONFIG_FILE_NAME, &config_stat) == -1) + { + const int error = errno; + if (error == ENOENT)// No config file found -> create new config with defaults & save to disk. + { + set_config(NULL); + return save_config(); + } + WARN("Failed to get stat on file '%s', error: %i ", CONFIG_FILE_NAME, error); + return error; + } + const long int file_size = config_stat.st_size; + if (file_size <= 0) + { + WARN("Config file exists but is empty, fallback to default config..."); + set_config(NULL); + return save_config(); + } + FILE *config_fd = fopen(CONFIG_FILE_NAME, "r"); + if (config_fd == NULL) + { + const int error = errno; + WARN("Failed to open file: '%s' with error code: %i", CONFIG_FILE_NAME, error); + return error; + } + void* file_mem = malloc(file_size); + if (file_mem == NULL) + { + const int error = errno; + WARN("Failed to allocate memory!"); + return error; + } + const size_t total_read = fread(file_mem, file_size, file_size, config_fd); + fclose(config_fd); + if (total_read != 1) + { + free(file_mem); + return -1; + } + loaded_configuration = json_to_config(file_mem); + free(file_mem); + return 0; +} + +struct netex_configuration* get_configuration() +{ + if (loaded_configuration == NULL) + { + const int load_result = load_config(); + if (load_result != 0) + WARN("Failed to get configuration!"); + } + return loaded_configuration; +} + +struct netex_configuration* create_config(const char* server_name, const size_t port) +{ + struct netex_configuration* nx_config = malloc(sizeof(struct netex_configuration)); + if (nx_config == NULL) + { + WARN("Failed to allocate memory for configuration object!"); + return nx_config; + } + nx_config->server_name = strdup(server_name); + nx_config->port = port; + return nx_config; +} + +void set_config(struct netex_configuration* config) +{ + if (loaded_configuration != NULL) + { + if (loaded_configuration->server_name != NULL) + free(loaded_configuration->server_name); + free(loaded_configuration); + } + if (config == NULL) // Fallback to default values + config = create_config("Linux netex server", 6920); + loaded_configuration = config; +} + +struct netex_configuration* json_to_config(const char* json) +{ + const json_object* json_obj = json_tokener_parse(json); + json_object* json_prop_srv_name; + const char* srv_name = ""; + if (json_object_object_get_ex(json_obj, PROP_SRV_NAME, &json_prop_srv_name)) + srv_name = json_object_get_string(json_prop_srv_name); + json_object* json_prop_port; + size_t srv_port = 0; + if (json_object_object_get_ex(json_obj, PROP_PORT, &json_prop_port)) + srv_port = json_object_get_int64(json_prop_port); + struct netex_configuration* config = create_config(srv_name, srv_port); + return config; +} + +const char* config_to_json(const struct netex_configuration* net_config) +{ + json_object* jobj = json_object_new_object(); + json_object* srv_name_obj = json_object_new_string(net_config->server_name); + if (json_object_object_add(jobj, PROP_SRV_NAME, srv_name_obj) != 0) + WARN("Failed to add property: '%s' to JSON!", PROP_SRV_NAME); + json_object* srv_port_obj = json_object_new_int64(net_config->port); + if (json_object_object_add(jobj, PROP_PORT, srv_port_obj) != 0) + WARN("Failed to add property: '%s' to JSON!", PROP_PORT); + return json_object_to_json_string_ext(jobj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); +} \ No newline at end of file diff --git a/src/server/server.c b/src/server/server.c index 3cc3459..4fb9443 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -1,12 +1,22 @@ #include #include "netex.h" -#include "netexssl.h" + +int srv_setup(void); int main(int argc, char *argv[]) { - init_openssl(); + netex_init(); PRINT_LINE("Hello, server!"); - shutdown_openssl(); + if (argc > 1) + { + //TODO: Handle args + } + if (srv_setup() != 0) + { + ERROR("Failed to initialize server!"); + return 0; + } + netex_shutdown(); return 0; } @@ -16,4 +26,15 @@ void* th_srv_listen(void) { //TODO: Listen to connections & give a thread to handle communication } +} + +int svr_command(void) +{ + return 0; +} + +int srv_setup(void) +{ + + return -1; } \ No newline at end of file