-
Notifications
You must be signed in to change notification settings - Fork 3
/
jconf.cpp
275 lines (231 loc) · 6.87 KB
/
jconf.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright (c) 2014-2023, Epic Cash and fireice-uk
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "jconf.hpp"
#include "encdec.h"
#include "jconf_p.hpp"
#include "rapidjson/error/en.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
const char* default_config_file =
#include "config_json.tmpl"
;
jconfPrivate::jconfPrivate(jconf* q) : q(q)
{
}
jconf::jconf() : d(*new jconfPrivate(this))
{
}
const char* jconf::get_pid_file()
{
return d.configValues[sPidFile]->GetString();
}
const char* jconf::get_log_file()
{
return d.configValues[sLogFile]->GetString();
}
log_level jconf::get_log_level()
{
static log_level val = log_level::invalid;
if(val != log_level::invalid)
return val;
const char* valstr = d.configValues[sLogLevel]->GetString();
if(strcmp(valstr, "error") == 0)
val = log_level::error;
else if(strcmp(valstr, "warn") == 0)
val = log_level::warn;
else if(strcmp(valstr, "info") == 0)
val = log_level::info;
else if(strcmp(valstr, "debug_hi") == 0)
val = log_level::debug_hi;
else if(strcmp(valstr, "debug_lo") == 0)
val = log_level::debug_lo;
else
val = log_level::invalid;
return val;
}
const char* jconf::get_db_hostname()
{
return d.configValues[sDbHostname]->GetString();
}
const char* jconf::get_db_port()
{
return d.configValues[sDbPort]->GetString();
}
const char* jconf::get_db_name()
{
return d.configValues[sDbName]->GetString();
}
const char* jconf::get_db_username()
{
return d.configValues[sDbUsername]->GetString();
}
const char* jconf::get_db_password()
{
return d.configValues[sDbPassword]->GetString();
}
const char* jconf::get_node_hostname()
{
return d.configValues[sNodeHostname]->GetString();
}
const char* jconf::get_node_port()
{
return d.configValues[sNodePort]->GetString();
}
const char* jconf::get_node_username()
{
return d.configValues[sNodeUsername]->GetString();
}
const char* jconf::get_node_password()
{
return d.configValues[sNodePassword]->GetString();
}
bool jconf::daemonize()
{
return d.configValues[bDaemonize]->GetBool();
}
uint16_t jconf::get_server_port()
{
return d.configValues[iPlainPort]->GetUint();
}
uint16_t jconf::get_server_tls_port()
{
return d.configValues[iTlsPort]->GetUint();
}
const char* jconf::get_tls_cert_filename()
{
return d.configValues[sTlsCert]->GetString();
}
const char* jconf::get_tls_cipher_list()
{
return d.configValues[sTlsCipers]->GetString();
}
size_t jconf::get_template_timeout()
{
return d.configValues[iTemplateTimeout]->GetUint();
}
size_t jconf::get_fatal_node_timeout()
{
return d.configValues[iFatalNodeTimeout]->GetUint();
}
inline std::string read_file(const char* filename, bool& error)
{
struct stat sb;
if(stat(filename, &sb) == -1)
{
fprintf(stderr, "Config file %s not found. Will create it and exit. Adjust the settings and run again.\n", filename);
int fd = open(filename, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
if(fd == -1)
{
fputs("Panic! Could not open config file for writing!\n", stderr);
error = true;
return "";
}
ssize_t conf_len = strlen(default_config_file);
if(write(fd, default_config_file, conf_len) != conf_len)
{
fputs("Panic! Writing to config file failed!\n", stderr);
error = true;
return "";
}
close(fd);
error = true;
return "";
}
std::string conf(sb.st_size, '\0');
int fd = open(filename, O_RDONLY);
if(fd == -1)
{
fputs("Panic! Could not open config file for reading!\n", stderr);
error = true;
return "";
}
ssize_t ln = read(fd, &conf[0], sb.st_size);
close(fd);
if(ln != sb.st_size)
{
fputs("Panic! Reading config file failed!\n", stderr);
error = true;
return "";
}
return conf;
}
bool jconf::parse_config(const char* filename)
{
bool rd_error;
std::string file = read_file(filename, rd_error);
if(rd_error)
return false;
d.jsonDoc.Parse<kParseCommentsFlag | kParseTrailingCommasFlag>(file.c_str(), file.length());
if(d.jsonDoc.HasParseError())
{
fprintf(stderr, "JSON config parse error(offset %zu): %s\n", d.jsonDoc.GetErrorOffset(), GetParseError_En(d.jsonDoc.GetParseError()));
return false;
}
if(!d.jsonDoc.IsObject())
{
fputs("JSON root is not an object.\n", stderr);
return false;
}
for(size_t i = 0; i < iConfigCnt; i++)
{
if(oConfigValues[i].iName != i)
{
fputs("Code error. oConfigValues are not in order.\n", stderr);
return false;
}
d.configValues[i] = GetObjectMember(d.jsonDoc, oConfigValues[i].sName);
if(d.configValues[i] == nullptr)
{
fprintf(stderr, "Invalid config file. Missing value \"%s\".\n", oConfigValues[i].sName);
return false;
}
if(!checkType(d.configValues[i]->GetType(), oConfigValues[i].iType))
{
fprintf(stderr, "Invalid config file. Value \"%s\" has unexpected type.\n", oConfigValues[i].sName);
return false;
}
if((oConfigValues[i].flagConstraints & flag_unsigned) != 0 && !check_constraint_unsigned(d.configValues[i]))
{
fprintf(stderr, "Invalid config file. Value \"%s\" needs to be unsigned.\n", oConfigValues[i].sName);
return false;
}
if((oConfigValues[i].flagConstraints & flag_u16bit) != 0 && !check_constraint_u16bit(d.configValues[i]))
{
fprintf(stderr, "Invalid config file. Value \"%s\" needs to be lower than 65535.\n", oConfigValues[i].sName);
return false;
}
}
if(get_log_level() == log_level::invalid)
{
fprintf(stderr, "Invalid log_level, allowed values are \"error\", \"warn\", \"info\", \"debug_hi\", \"debug_lo\"\n");
return false;
}
return true;
}