mirror of
https://github.com/hmaxnl/netex.git
synced 2025-01-18 15:34:20 +01:00
Building base.
This commit is contained in:
parent
15b8d8b041
commit
127b779905
|
@ -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)
|
||||
|
||||
|
|
16
src/net/configuration.c
Normal file
16
src/net/configuration.c
Normal file
|
@ -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 "";
|
||||
}
|
|
@ -3,6 +3,5 @@
|
|||
target_sources(netex PUBLIC
|
||||
"net.h"
|
||||
"netex.h"
|
||||
"netexssl.h"
|
||||
"netexthreads.h"
|
||||
"sockets.h")
|
||||
"sockets.h"
|
||||
"storage.h")
|
|
@ -1,9 +1,14 @@
|
|||
#ifndef NETEX_H
|
||||
#define NETEX_H
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#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
|
|
@ -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
|
|
@ -1,10 +0,0 @@
|
|||
//
|
||||
// Created by max on 24-12-23.
|
||||
//
|
||||
|
||||
#ifndef NETEXTHREADS_H
|
||||
#define NETEXTHREADS_H
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#endif //NETEXTHREADS_H
|
19
src/net/include/storage.h
Normal file
19
src/net/include/storage.h
Normal file
|
@ -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
|
|
@ -1,20 +1,32 @@
|
|||
#include "netex.h"
|
||||
#include "netexssl.h"
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/err.h>
|
||||
#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();
|
||||
}
|
153
src/net/storage.c
Normal file
153
src/net/storage.c
Normal file
|
@ -0,0 +1,153 @@
|
|||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <json-c/json.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#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);
|
||||
}
|
|
@ -1,12 +1,22 @@
|
|||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
|
@ -17,3 +27,14 @@ 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user