This commit is contained in:
Alec Obradovich 2019-05-08 13:10:26 -05:00
parent 6960ec36a3
commit 3898250a05
6 changed files with 59 additions and 16 deletions

View file

@ -1,13 +1,53 @@
#include "conf.h" #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) { int glv_config_load_global(const char* path) {
_config.global = glv_ini_read_file(path); _config.global = glv_ini_read_file(path);
if(_config.global == NULL) { if(_config.global == NULL) {
printf(GLV_CRIT "Global configuration failed to load."); printf(GLV_CRIT "Global config failed to load.");
return 0; 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; return 1;
} }

View file

@ -79,7 +79,7 @@ glv_ini_t* glv_ini_read_file(const char* path) {
return ini; 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)); char* section_lc = strlower(strdup(section));
int check = glv_map_has_key(ini, section_lc); 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 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)), char *section_lc = strlower(strdup(section)),
*key_lc = strlower(strdup(key)); *key_lc = strlower(strdup(key));
@ -104,7 +104,7 @@ int glv_ini_section_has_key
return check; 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)), char *section_lc = strlower(strdup(section)),
*key_lc = strlower(strdup(key)), *key_lc = strlower(strdup(key)),
*value = NULL; *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 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); char *value = glv_ini_get(ini, section, key);
if(value == NULL) if(value == NULL)
@ -140,7 +141,11 @@ int glv_ini_get_type
case GLV_INI_DOUBLE: case GLV_INI_DOUBLE:
*(double*)out = strtod(value, NULL); *(double*)out = strtod(value, NULL);
break; break;
default:
return 0;
} }
return 1;
} }
void glv_ini_destroy(glv_ini_t* ini) { void glv_ini_destroy(glv_ini_t* ini) {

View file

@ -18,13 +18,14 @@ typedef glv_map_t glv_ini_t;
glv_ini_t* glv_ini_read_file(const char* path); 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 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 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); void glv_ini_destroy(glv_ini_t* ini);

View file

@ -1,6 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include "conf/conf.h" #include "conf/conf.h"
#include "sock/tcp_ssl.c"
int main(int argc, char** argv) { int main(int argc, char** argv) {
printf("Starting Glove Chat server..."); printf("Starting Glove Chat server...");
@ -8,8 +7,6 @@ int main(int argc, char** argv) {
if(!glv_config_load_global("config.ini")) if(!glv_config_load_global("config.ini"))
return -1; return -1;
glv_ssl_init();
glv_config_unload(); glv_config_unload();
return 0; return 0;
} }

View file

@ -27,7 +27,7 @@ glv_map_t* glv_map_create_ex(int initial_size) {
return map; 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; uint32_t hash = glv_map_hash_func(key) % map->bucket_count, i;
for(i = 0; i < map->bucket_lengths[hash]; ++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)); 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; return glv_map_get(map, key) != NULL;
} }

View file

@ -24,7 +24,7 @@ typedef void (*glv_map_dealloc_func)(void*);
glv_map_t* glv_map_create(void); glv_map_t* glv_map_create(void);
glv_map_t* glv_map_create_ex(int initial_size); 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(glv_map_t* map, const char* key, void* value);
void* glv_map_set_copy 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_remove(glv_map_t* map, const char* key);
void glv_map_removef(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_resize(glv_map_t* map, int size);
void glv_map_destroy(glv_map_t* map); void glv_map_destroy(glv_map_t* map);