#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, ssize_t 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 || (int64_t)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 || (int64_t)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 || (int64_t)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 || (int64_t)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 || (int64_t)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 || (int64_t)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 || (int64_t)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 || (int64_t)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 || (int64_t)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; } ssize_t cfg_file_size = (ssize_t)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 ( (int64_t)fread(sv_config->config_contents, sizeof(char), (size_t)(cfg_file_size * sizeof(char)), cfg_file) == (int64_t)-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; }