diff options
Diffstat (limited to 'load_config.h')
-rw-r--r-- | load_config.h | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/load_config.h b/load_config.h new file mode 100644 index 0000000..165c676 --- /dev/null +++ b/load_config.h @@ -0,0 +1,78 @@ +#define CONFIG_LOAD_SUCCESS 0 +#define CONFIG_LOAD_FAILED -1 + +#define CONFIG_PARSE_SUCCESS 0 +#define CONFIG_PARSE_FAILED -1 + +int parse_config( server_config_t *svconfig, long long int cfg_len ){ + if ( svconfig->config_contents == NULL || cfg_len <= 0 ) + return CONFIG_PARSE_FAILED; + svconfig->config_parsed = cJSON_ParseWithLength( svconfig->config_contents, (size_t)cfg_len ); + if ( svconfig->config_parsed == NULL ) + return CONFIG_PARSE_FAILED; + svconfig->log_enabled = cJSON_GetObjectItemCaseSensitive( svconfig->config_parsed, "log_enabled" ); + if ( svconfig->log_enabled == NULL || (long long int)strlen(svconfig->log_enabled->valuestring) <= 0 ) + return CONFIG_PARSE_FAILED; + svconfig->log_file = cJSON_GetObjectItemCaseSensitive( svconfig->config_parsed, "log_file" ); + if ( svconfig->log_file == NULL || (long long int)strlen(svconfig->log_file->valuestring) <= 0 ) + return CONFIG_PARSE_FAILED; + svconfig->bind_address = cJSON_GetObjectItemCaseSensitive( svconfig->config_parsed, "bind_address" ); + if ( svconfig->bind_address == NULL || (long long int)strlen(svconfig->bind_address->valuestring) <= 0 ) + return CONFIG_PARSE_FAILED; + svconfig->bind_port = cJSON_GetObjectItemCaseSensitive( svconfig->config_parsed, "bind_port" ); + if ( svconfig->bind_port == NULL || (long long int)strlen(svconfig->bind_port->valuestring) <= 0 ) + return CONFIG_PARSE_FAILED; + svconfig->server_password = cJSON_GetObjectItemCaseSensitive( svconfig->config_parsed, "server_password" ); + if ( svconfig->server_password == NULL || (long long int)strlen(svconfig->server_password->valuestring) <= 0 ) + return CONFIG_PARSE_FAILED; + svconfig->cert_file = cJSON_GetObjectItemCaseSensitive( svconfig->config_parsed, "cert_file" ); + if ( svconfig->cert_file == NULL || (long long int)strlen(svconfig->cert_file->valuestring) <= 0 ) + return CONFIG_PARSE_FAILED; + svconfig->cert_key_file = cJSON_GetObjectItemCaseSensitive( svconfig->config_parsed, "cert_key_file" ); + if ( svconfig->cert_key_file == NULL || (long long int)strlen(svconfig->cert_key_file->valuestring) <= 0 ) + return CONFIG_PARSE_FAILED; + svconfig->webhooks_file = cJSON_GetObjectItemCaseSensitive( svconfig->config_parsed, "webhooks_file" ); + if ( svconfig->webhooks_file == NULL || (long long int)strlen(svconfig->webhooks_file->valuestring) <= 0 ) + return CONFIG_PARSE_FAILED; + return CONFIG_PARSE_SUCCESS; +} + +int load_config( char *config_file_name, server_config_t *sv_config ){ + if ( config_file_name == NULL || (long long int)strlen(config_file_name) <= 0 || strcmp((const char*)config_file_name, "") == 0 ) + return CONFIG_LOAD_FAILED; + FILE *cfg_file = fopen( config_file_name, "r" ); + if ( cfg_file == NULL ) + return CONFIG_LOAD_FAILED; + if ( fseek(cfg_file, 0, SEEK_END) == -1 ){ + fclose( cfg_file ); + return CONFIG_LOAD_FAILED; + } + long long int cfg_file_size = (long long int)ftell( cfg_file ); + if ( cfg_file_size <= 0 ){ + rewind( cfg_file ); + fclose( cfg_file ); + return CONFIG_LOAD_FAILED; + } + rewind( cfg_file ); + sv_config->config_contents = (char*)calloc( (size_t)cfg_file_size, sizeof(char) ); + if ( sv_config->config_contents == NULL ){ + fclose( cfg_file ); + cfg_file_size = 0; + return CONFIG_LOAD_FAILED; + } + if ( (long long int)fread(sv_config->config_contents, sizeof(char), (size_t)(cfg_file_size * sizeof(char)), cfg_file) == (long long int)-1 ){ + free( sv_config->config_contents ); + sv_config->config_contents = NULL; + fclose( cfg_file ); + cfg_file_size = 0; + return CONFIG_LOAD_FAILED; + } + if ( parse_config(sv_config, cfg_file_size) != CONFIG_PARSE_SUCCESS ){ + free( sv_config->config_contents ); + sv_config->config_contents = NULL; + return CONFIG_LOAD_FAILED; + } + free( sv_config->config_contents ); + sv_config->config_contents = NULL; + return CONFIG_LOAD_SUCCESS; +} |