forked from fireice-uk/xmr-stak-nvidia
-
Notifications
You must be signed in to change notification settings - Fork 7
/
jconf.cpp
412 lines (342 loc) · 9.94 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional permission under GNU GPL version 3 section 7
*
* If you modify this Program, or any covered work, by linking or combining
* it with OpenSSL (or a modified version of that library), containing parts
* covered by the terms of OpenSSL License and SSLeay License, the licensors
* of this Program grant you additional permission to convey the resulting work.
*
*/
#include "jconf.h"
#include "console.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#define strcasecmp _stricmp
#include <intrin.h>
#else
#include <cpuid.h>
#endif
#include "rapidjson/document.h"
#include "rapidjson/error/en.h"
#include "jext.h"
#include "console.h"
using namespace rapidjson;
/*
* This enum needs to match index in oConfigValues, otherwise we will get a runtime error
*/
enum configEnum { aGpuThreadsConf,
bTlsMode, bTlsSecureAlgo, sTlsFingerprint, sPoolAddr, sWalletAddr, sPoolPwd,
iCallTimeout, iNetRetry, iGiveUpLimit, iVerboseLevel, iAutohashTime,
sOutputFile, iHttpdPort, bPreferIpv4 };
struct configVal {
configEnum iName;
const char* sName;
Type iType;
};
// Same order as in configEnum, as per comment above
// kNullType means any type
configVal oConfigValues[] = {
{ aGpuThreadsConf, "gpu_threads_conf", kNullType },
{ bTlsMode, "use_tls", kTrueType },
{ bTlsSecureAlgo, "tls_secure_algo", kTrueType },
{ sTlsFingerprint, "tls_fingerprint", kStringType },
{ sPoolAddr, "pool_address", kStringType },
{ sWalletAddr, "wallet_address", kStringType },
{ sPoolPwd, "pool_password", kStringType },
{ iCallTimeout, "call_timeout", kNumberType },
{ iNetRetry, "retry_time", kNumberType },
{ iGiveUpLimit, "giveup_limit", kNumberType },
{ iVerboseLevel, "verbose_level", kNumberType },
{ iAutohashTime, "h_print_time", kNumberType },
{ sOutputFile, "output_file", kStringType },
{ iHttpdPort, "httpd_port", kNumberType },
{ bPreferIpv4, "prefer_ipv4", kTrueType }
};
constexpr size_t iConfigCnt = (sizeof(oConfigValues)/sizeof(oConfigValues[0]));
inline bool checkType(Type have, Type want)
{
if(want == have)
return true;
else if(want == kNullType)
return true;
else if(want == kTrueType && have == kFalseType)
return true;
else if(want == kFalseType && have == kTrueType)
return true;
else
return false;
}
struct jconf::opaque_private
{
Document jsonDoc;
const Value* configValues[iConfigCnt]; //Compile time constant
opaque_private()
{
}
};
jconf* jconf::oInst = nullptr;
jconf::jconf()
{
prv = new opaque_private();
}
bool jconf::NeedsAutoconf()
{
return !prv->configValues[aGpuThreadsConf]->IsArray();
}
bool jconf::GetThreadConfig(size_t id, thd_cfg &cfg)
{
if(!prv->configValues[aGpuThreadsConf]->IsArray())
return false;
if(id >= prv->configValues[aGpuThreadsConf]->Size())
return false;
const Value& oThdConf = prv->configValues[aGpuThreadsConf]->GetArray()[id];
if(!oThdConf.IsObject())
return false;
const Value *gid, *blocks, *threads, *bfactor, *bsleep, *aff;
gid = GetObjectMember(oThdConf, "index");
blocks = GetObjectMember(oThdConf, "blocks");
threads = GetObjectMember(oThdConf, "threads");
bfactor = GetObjectMember(oThdConf, "bfactor");
bsleep = GetObjectMember(oThdConf, "bsleep");
aff = GetObjectMember(oThdConf, "affine_to_cpu");
if(gid == nullptr || blocks == nullptr || threads == nullptr ||
bfactor == nullptr || bsleep == nullptr || aff == nullptr)
{
return false;
}
if(!gid->IsNumber() || gid->GetInt() < 0)
return false;
if(!blocks->IsNumber() || blocks->GetInt() < 0)
return false;
if(!threads->IsNumber() || threads->GetInt() < 0)
return false;
if(!bfactor->IsNumber() || bfactor->GetInt() < 0)
return false;
if(!bsleep->IsNumber() || bsleep->GetInt() < 0)
return false;
if(!aff->IsUint64() && !aff->IsBool())
return false;
cfg.id = gid->GetInt();
cfg.blocks = blocks->GetInt();
cfg.threads = threads->GetInt();
cfg.bfactor = bfactor->GetInt();
cfg.bsleep = bsleep->GetInt();
if(aff->IsNumber())
cfg.cpu_aff = aff->GetInt();
else
cfg.cpu_aff = -1;
return true;
}
bool jconf::GetTlsSetting()
{
return prv->configValues[bTlsMode]->GetBool();
}
bool jconf::TlsSecureAlgos()
{
return prv->configValues[bTlsSecureAlgo]->GetBool();
}
const char* jconf::GetTlsFingerprint()
{
return prv->configValues[sTlsFingerprint]->GetString();
}
const char* jconf::GetPoolAddress()
{
return prv->configValues[sPoolAddr]->GetString();
}
const char* jconf::GetPoolPwd()
{
return prv->configValues[sPoolPwd]->GetString();
}
const char* jconf::GetWalletAddress()
{
return prv->configValues[sWalletAddr]->GetString();
}
bool jconf::PreferIpv4()
{
return prv->configValues[bPreferIpv4]->GetBool();
}
size_t jconf::GetThreadCount()
{
if(prv->configValues[aGpuThreadsConf]->IsArray())
return prv->configValues[aGpuThreadsConf]->Size();
else
return 0;
}
uint64_t jconf::GetCallTimeout()
{
return prv->configValues[iCallTimeout]->GetUint64();
}
uint64_t jconf::GetNetRetry()
{
return prv->configValues[iNetRetry]->GetUint64();
}
uint64_t jconf::GetGiveUpLimit()
{
return prv->configValues[iGiveUpLimit]->GetUint64();
}
uint64_t jconf::GetVerboseLevel()
{
return prv->configValues[iVerboseLevel]->GetUint64();
}
uint64_t jconf::GetAutohashTime()
{
return prv->configValues[iAutohashTime]->GetUint64();
}
uint16_t jconf::GetHttpdPort()
{
return prv->configValues[iHttpdPort]->GetUint();
}
const char* jconf::GetOutputFile()
{
return prv->configValues[sOutputFile]->GetString();
}
bool jconf::check_cpu_features()
{
constexpr int AESNI_BIT = 1 << 25;
constexpr int SSE2_BIT = 1 << 26;
int cpu_info[4];
#ifdef _WIN32
__cpuid(cpu_info, 1);
#else
__cpuid(1, cpu_info[0], cpu_info[1], cpu_info[2], cpu_info[3]);
#endif
bHaveAes = (cpu_info[2] & AESNI_BIT) != 0;
return (cpu_info[3] & SSE2_BIT) != 0;
}
bool jconf::parse_config(const char* sFilename)
{
FILE * pFile;
char * buffer;
size_t flen;
if(!check_cpu_features())
{
printer::inst()->print_msg(L0, "CPU support of SSE2 is required.");
return false;
}
pFile = fopen(sFilename, "rb");
if (pFile == NULL)
{
printer::inst()->print_msg(L0, "Failed to open config file %s.", sFilename);
return false;
}
fseek(pFile,0,SEEK_END);
flen = ftell(pFile);
rewind(pFile);
if(flen >= 64*1024)
{
fclose(pFile);
printer::inst()->print_msg(L0, "Oversized config file - %s.", sFilename);
return false;
}
if(flen <= 16)
{
printer::inst()->print_msg(L0, "File is empty or too short - %s.", sFilename);
return false;
}
buffer = (char*)malloc(flen + 3);
if(fread(buffer+1, flen, 1, pFile) != 1)
{
free(buffer);
fclose(pFile);
printer::inst()->print_msg(L0, "Read error while reading %s.", sFilename);
return false;
}
fclose(pFile);
//Replace Unicode BOM with spaces - we always use UTF-8
unsigned char* ubuffer = (unsigned char*)buffer;
if(ubuffer[1] == 0xEF && ubuffer[2] == 0xBB && ubuffer[3] == 0xBF)
{
buffer[1] = ' ';
buffer[2] = ' ';
buffer[3] = ' ';
}
buffer[0] = '{';
buffer[flen] = '}';
buffer[flen + 1] = '\0';
prv->jsonDoc.Parse<kParseCommentsFlag|kParseTrailingCommasFlag>(buffer, flen+2);
free(buffer);
if(prv->jsonDoc.HasParseError())
{
printer::inst()->print_msg(L0, "JSON config parse error(offset %llu): %s",
int_port(prv->jsonDoc.GetErrorOffset()), GetParseError_En(prv->jsonDoc.GetParseError()));
return false;
}
if(!prv->jsonDoc.IsObject())
{ //This should never happen as we created the root ourselves
printer::inst()->print_msg(L0, "Invalid config file. No root?\n");
return false;
}
for(size_t i = 0; i < iConfigCnt; i++)
{
if(oConfigValues[i].iName != i)
{
printer::inst()->print_msg(L0, "Code error. oConfigValues are not in order.");
return false;
}
prv->configValues[i] = GetObjectMember(prv->jsonDoc, oConfigValues[i].sName);
if(prv->configValues[i] == nullptr)
{
printer::inst()->print_msg(L0, "Invalid config file. Missing value \"%s\".", oConfigValues[i].sName);
return false;
}
if(!checkType(prv->configValues[i]->GetType(), oConfigValues[i].iType))
{
printer::inst()->print_msg(L0, "Invalid config file. Value \"%s\" has unexpected type.", oConfigValues[i].sName);
return false;
}
}
thd_cfg c;
for(size_t i=0; i < GetThreadCount(); i++) //Will be 0 on autoconf
{
if(!GetThreadConfig(i, c))
{
printer::inst()->print_msg(L0, "Thread %llu has invalid config.", int_port(i));
return false;
}
}
if(!prv->configValues[iCallTimeout]->IsUint64() ||
!prv->configValues[iNetRetry]->IsUint64() ||
!prv->configValues[iGiveUpLimit]->IsUint64())
{
printer::inst()->print_msg(L0,
"Invalid config file. call_timeout, retry_time and giveup_limit need to be positive integers.");
return false;
}
if(!prv->configValues[iVerboseLevel]->IsUint64() || !prv->configValues[iAutohashTime]->IsUint64())
{
printer::inst()->print_msg(L0,
"Invalid config file. verbose_level and h_print_time need to be positive integers.");
return false;
}
if(!prv->configValues[iHttpdPort]->IsUint() || prv->configValues[iHttpdPort]->GetUint() > 0xFFFF)
{
printer::inst()->print_msg(L0,
"Invalid config file. httpd_port has to be in the range 0 to 65535.");
return false;
}
#ifdef CONF_NO_TLS
if(prv->configValues[bTlsMode]->GetBool())
{
printer::inst()->print_msg(L0,
"Invalid config file. TLS enabled while the application has been compiled without TLS support.");
return false;
}
#endif // CONF_NO_TLS
printer::inst()->set_verbose_level(prv->configValues[iVerboseLevel]->GetUint64());
return true;
}