From 3898250a05d1f612af9c877c9dcc1ad294e86b40 Mon Sep 17 00:00:00 2001 From: Alec Obradovich Date: Wed, 8 May 2019 13:10:26 -0500 Subject: [PATCH] confdone --- src/conf/conf.c | 42 +++++++++++++++++++++++++++++++++++++++++- src/conf/ini.c | 13 +++++++++---- src/conf/ini.h | 9 +++++---- src/main.c | 3 --- src/util/containers.c | 4 ++-- src/util/containers.h | 4 ++-- 6 files changed, 59 insertions(+), 16 deletions(-) diff --git a/src/conf/conf.c b/src/conf/conf.c index fe73f7e..d791096 100644 --- a/src/conf/conf.c +++ b/src/conf/conf.c @@ -1,13 +1,53 @@ #include "conf.h" +int glv_verify_config + (const glv_ini_t* config, const char* name, + const char* const* sections, int section_count, + const char* const* const* keys, const int* key_counts) +{ + int i, j; + + for(i = 0; i < section_count; ++i) { + if(!glv_ini_has_section(config, sections[i])) { + printf(GLV_CRIT + "%s missing required section '%s'.", name, sections[i] + ); + return 0; + } + + for(j = 0; j < key_counts[i]; ++j) { + if(!glv_ini_section_has_key(config, sections[i], keys[i][j])) { + printf(GLV_CRIT + "%s section '%s' missing required key '%s'.", + name, sections[i], keys[i][j] + ); + return 0; + } + } + } + + return 1; +} + int glv_config_load_global(const char* path) { _config.global = glv_ini_read_file(path); if(_config.global == NULL) { - printf(GLV_CRIT "Global configuration failed to load."); + printf(GLV_CRIT "Global config failed to load."); return 0; } + char* sections[] = {"SSL"}; + char* ssl[] = {"Certificate", "Private Key"}; + char** keys[] = {ssl}; + int key_counts[] = {sizeof(ssl) / sizeof(ssl[0])}; + if(!glv_verify_config(_config.global, "Global config", + sections, sizeof(sections) / sizeof(sections[0]), keys, key_counts)) + { + printf(GLV_CRIT "Global config failed to load."); + glv_ini_destroy(_config.global); + return 0; + } return 1; } diff --git a/src/conf/ini.c b/src/conf/ini.c index cad0941..406a20a 100644 --- a/src/conf/ini.c +++ b/src/conf/ini.c @@ -79,7 +79,7 @@ glv_ini_t* glv_ini_read_file(const char* path) { return ini; } -int glv_ini_has_section(glv_ini_t* ini, const char* section) { +int glv_ini_has_section(const glv_ini_t* ini, const char* section) { char* section_lc = strlower(strdup(section)); int check = glv_map_has_key(ini, section_lc); @@ -88,7 +88,7 @@ int glv_ini_has_section(glv_ini_t* ini, const char* section) { } int glv_ini_section_has_key - (glv_ini_t* ini, const char* section, const char* key) + (const glv_ini_t* ini, const char* section, const char* key) { char *section_lc = strlower(strdup(section)), *key_lc = strlower(strdup(key)); @@ -104,7 +104,7 @@ int glv_ini_section_has_key return check; } -char* glv_ini_get(glv_ini_t* ini, const char* section, const char* key) { +char* glv_ini_get(const glv_ini_t* ini, const char* section, const char* key) { char *section_lc = strlower(strdup(section)), *key_lc = strlower(strdup(key)), *value = NULL; @@ -120,7 +120,8 @@ char* glv_ini_get(glv_ini_t* ini, const char* section, const char* key) { } int glv_ini_get_type - (glv_ini_t* ini, const char* section, const char* key, int type, void* out) + (const glv_ini_t* ini, const char* section, + const char* key, int type, void* out) { char *value = glv_ini_get(ini, section, key); if(value == NULL) @@ -140,7 +141,11 @@ int glv_ini_get_type case GLV_INI_DOUBLE: *(double*)out = strtod(value, NULL); break; + default: + return 0; } + + return 1; } void glv_ini_destroy(glv_ini_t* ini) { diff --git a/src/conf/ini.h b/src/conf/ini.h index 50a711f..b3c53f1 100644 --- a/src/conf/ini.h +++ b/src/conf/ini.h @@ -18,13 +18,14 @@ typedef glv_map_t glv_ini_t; glv_ini_t* glv_ini_read_file(const char* path); -int glv_ini_has_section(glv_ini_t* ini, const char* section); +int glv_ini_has_section(const glv_ini_t* ini, const char* section); int glv_ini_section_has_key - (glv_ini_t* ini, const char* section, const char* key); + (const glv_ini_t* ini, const char* section, const char* key); -char* glv_ini_get(glv_ini_t* ini, const char* section, const char* key); +char* glv_ini_get(const glv_ini_t* ini, const char* section, const char* key); int glv_ini_get_type - (glv_ini_t* ini, const char* section, const char* key, int type, void* out); + (const glv_ini_t* ini, const char* section, + const char* key, int type, void* out); void glv_ini_destroy(glv_ini_t* ini); diff --git a/src/main.c b/src/main.c index a9090a9..f7bcf07 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,5 @@ #include #include "conf/conf.h" -#include "sock/tcp_ssl.c" int main(int argc, char** argv) { printf("Starting Glove Chat server..."); @@ -8,8 +7,6 @@ int main(int argc, char** argv) { if(!glv_config_load_global("config.ini")) return -1; - glv_ssl_init(); - glv_config_unload(); return 0; } \ No newline at end of file diff --git a/src/util/containers.c b/src/util/containers.c index 5e7d3b5..3de87cb 100644 --- a/src/util/containers.c +++ b/src/util/containers.c @@ -27,7 +27,7 @@ glv_map_t* glv_map_create_ex(int initial_size) { return map; } -void* glv_map_get(glv_map_t* map, const char* key) { +void* glv_map_get(const glv_map_t* map, const char* key) { uint32_t hash = glv_map_hash_func(key) % map->bucket_count, i; for(i = 0; i < map->bucket_lengths[hash]; ++i) @@ -123,7 +123,7 @@ void glv_map_removef(glv_map_t* map, const char* key) { free(glv_map_remove(map, key)); } -int glv_map_has_key(glv_map_t* map, const char* key) { +int glv_map_has_key(const glv_map_t* map, const char* key) { return glv_map_get(map, key) != NULL; } diff --git a/src/util/containers.h b/src/util/containers.h index a0db2ae..2196dda 100644 --- a/src/util/containers.h +++ b/src/util/containers.h @@ -24,7 +24,7 @@ typedef void (*glv_map_dealloc_func)(void*); glv_map_t* glv_map_create(void); glv_map_t* glv_map_create_ex(int initial_size); -void* glv_map_get(glv_map_t* map, const char* key); +void* glv_map_get(const glv_map_t* map, const char* key); void* glv_map_set(glv_map_t* map, const char* key, void* value); void* glv_map_set_copy @@ -37,7 +37,7 @@ void glv_map_setf_copy void* glv_map_remove(glv_map_t* map, const char* key); void glv_map_removef(glv_map_t* map, const char* key); -int glv_map_has_key(glv_map_t* map, const char* key); +int glv_map_has_key(const glv_map_t* map, const char* key); void glv_map_resize(glv_map_t* map, int size); void glv_map_destroy(glv_map_t* map);