1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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;
}
|