-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathconf.c
More file actions
284 lines (262 loc) · 7.79 KB
/
conf.c
File metadata and controls
284 lines (262 loc) · 7.79 KB
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
/* Copyright 37pool.com. All rights reserved.
*
* Maintainer:
* Huang Le <4tar@37pool.com>
*
* The software is published under GNU Affero General Public License version 3,
* or GNU AGPL v3. The GNU AGPL is based on the GNU GPL, but has an additional
* term to allow users who interact with the licensed software over a network to
* receive the source for that program. For more information, please refer to:
*
* http://www.gnu.org/licenses/agpl.html
* http://www.gnu.org/licenses/why-affero-gpl.html
*
* WARN: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "defs.h"
#define DEFAULT_PROXY_HOST "0.0.0.0"
#define DEFAULT_PROXY_PORT 3737
#define DEFAULT_POOL_PORT 3333
#define DEFAULT_POOL_PRIORITY 1
#define DEFAULT_POOL_WEIGHT 10
#define DEFAULT_POOL_TIMEOUT 120000
#define DEFAULT_POOL_CBTOTAL 2500000000
extern log_level log_level_t;
int parse_config( const char* file, proxy_config *conf )
{
unsigned int i = 0, pool_disabled = 1;
char line[128];
FILE* fConf = fopen(file, "r");
if (!fConf) {
pr_err("Cannot open configuration file: %s", file);
return 1;
}
memset(conf, 0, sizeof(*conf));
while (fgets(line, sizeof(line), fConf)) {
char *p, *name, *value = NULL;
++i;
for (p = line; *p && *p != '#'; ++p);
if (*p)
*p = '\0';
for (--p; p >= line &&
(*p == '\r' || *p == '\n' || *p == ' ' || *p == '\t'); --p);
if (p < line)
continue;
if (p - line >= 80) {
pr_err("Invalid line (%d): (too long)\n", i);
i = 0;
break;
}
p[1] = '\0';
for (p = line; *p && (*p == ' ' || *p == '\t'); ++p);
if (*p == '#')
continue;
if (p[1] == '\0') {
unknown_line:
pr_err("Invalid line (%d): %s\n", i, line);
i = 0;
break;
}
name = p;
for (++p; *p && (*p != ' ' && *p != '\t' && *p != '='); ++p);
*p = '\0';
for (++p; *p && (*p == ' ' || *p == '\t' || *p == '='); ++p);
if (*p)
value = p;
if (!strcmp(name, "proxy.host")) {
if (conf->host[0]) {
duplicate_key:
pr_err("Invalid line (%d): (duplicate key '%s')", i, name);
i = 0;
break;
}
if (!value) {
empty_value:
pr_err("Invalid line (%d): (empty value)", i);
i = 0;
break;
}
if (strlen(value) >= sizeof(conf->host)) {
too_long_value:
pr_err("Invalid line (%d): (too long value)", i);
i = 0;
break;
}
strcpy(conf->host, value);
} else if (!strcmp(name, "proxy.port")) {
if (conf->port)
goto duplicate_key;
if (value)
conf->port = atoi(value);
} else if (!strcmp(name, "proxy.outip")) {
if (conf->outip[0])
goto duplicate_key;
if (!value)
continue;
if (strlen(value) >= sizeof(conf->outip))
goto too_long_value;
strcpy(conf->outip, value);
} else if (!strcmp(name, "proxy.outport")) {
if (conf->outport)
goto duplicate_key;
if (value)
conf->outport = atoi(value);
} else if (!strcmp(name, "proxy.timeout")) {
if (conf->timeout)
goto duplicate_key;
if (value)
conf->timeout = atoi(value);
} else if (!strcmp(name, "proxy.loglevel")) {
if ((int)log_level_t >= 0)
goto duplicate_key;
if (value) {
if (!strcmp(value, "debug"))
log_level_t = level_debug;
else if (!strcmp(value, "info"))
log_level_t = level_info;
else if (!strcmp(value, "warn"))
log_level_t = level_warn;
else if (!strcmp(value, "error"))
log_level_t = level_err;
else
goto unknown_line;
}
} else if (!strcmp(name, "pool.enable")) {
pool_disabled = (value && strcmp(value, "true"));
if (pool_disabled)
continue;
conf->pools[conf->count++].priority = -1;
} else if (!strcmp(name, "pool.priority")) {
if (pool_disabled)
continue;
if (conf->pools[conf->count - 1].priority >= 0)
goto duplicate_key;
if (value)
conf->pools[conf->count - 1].priority = atoi(value);
} else if (!strcmp(name, "pool.weight")) {
if (pool_disabled)
continue;
if (conf->pools[conf->count - 1].weight)
goto duplicate_key;
if (value)
conf->pools[conf->count - 1].weight = atoi(value);
} else if (!strcmp(name, "pool.host")) {
if (pool_disabled)
continue;
if (conf->pools[conf->count - 1].host[0])
goto duplicate_key;
if (!value)
goto empty_value;
if (strlen(value) >= sizeof(conf->pools[0].host))
goto too_long_value;
strcpy(conf->pools[conf->count - 1].host, value);
} else if (!strcmp(name, "pool.port")) {
if (pool_disabled)
continue;
if (conf->pools[conf->count - 1].port)
goto duplicate_key;
if (value)
conf->pools[conf->count - 1].port = atoi(value);
} else if (!strcmp(name, "pool.miner")) {
if (pool_disabled)
continue;
if (conf->pools[conf->count - 1].miner[0])
goto duplicate_key;
if (!value)
goto empty_value;
if (strlen(value) >= sizeof(conf->pools[0].miner))
goto too_long_value;
strcpy(conf->pools[conf->count - 1].miner, value);
} else if (!strcmp(name, "pool.passwd")) {
if (pool_disabled)
continue;
if (conf->pools[conf->count - 1].passwd[0])
goto duplicate_key;
if (value) {
if (strlen(value) >= sizeof(conf->pools[0].passwd))
goto too_long_value;
strcpy(conf->pools[conf->count - 1].passwd, value);
}
} else if (!strcmp(name, "pool.timeout")) {
if (pool_disabled)
continue;
if (conf->pools[conf->count - 1].timeout)
goto duplicate_key;
if (value)
conf->pools[conf->count - 1].timeout = 1000 * atoi(value);
} else if (!strcmp(name, "pool.cbaddr")) {
if (pool_disabled)
continue;
if (conf->pools[conf->count - 1].cbaddr[0])
goto duplicate_key;
if (!value)
continue;
if (strlen(value) >= sizeof(conf->pools[0].cbaddr))
goto too_long_value;
strcpy(conf->pools[conf->count - 1].cbaddr, value);
} else if (!strcmp(name, "pool.cbtotal")) {
if (pool_disabled)
continue;
if (conf->pools[conf->count - 1].cbtotal)
goto duplicate_key;
if (value)
conf->pools[conf->count - 1].cbtotal = strtoul(value, NULL, 10);
} else if (!strcmp(name, "pool.cbperc")) {
if (pool_disabled)
continue;
if (conf->pools[conf->count - 1].cbperc)
goto duplicate_key;
if (value)
conf->pools[conf->count - 1].cbperc = atof(value);
} else
goto unknown_line;
}
fclose(fConf);
if (!i)
return 1;
if ((int)log_level_t < 0)
log_level_t = level_info;
if (log_level_t <= level_info)
setbuf(stdout, NULL);
if (!conf->host[0])
strcpy(conf->host, DEFAULT_PROXY_HOST);
if (!conf->port)
conf->port = DEFAULT_PROXY_PORT;
pr_debug("proxy config: %s:%d/%d", conf->host, conf->port, conf->timeout);
for (i = 0; i < conf->count; ++i) {
if (!conf->pools[i].host[0]) {
pr_err("Invalid pool %d configuration: no host", i + 1);
i = 0;
break;
}
if (!conf->pools[i].miner[0]) {
pr_err("Invalid pool %d configuration: no miner", i + 1);
i = 0;
break;
}
conf->pools[i].conf = conf;
if (!conf->pools[i].port)
conf->pools[i].port = DEFAULT_POOL_PORT;
if (conf->pools[i].priority < 0)
conf->pools[i].priority = DEFAULT_POOL_PRIORITY;
if (!conf->pools[i].weight)
conf->pools[i].weight = DEFAULT_POOL_WEIGHT;
if (!conf->pools[i].timeout)
conf->pools[i].timeout = DEFAULT_POOL_TIMEOUT;
if (!conf->pools[i].cbtotal)
conf->pools[i].cbtotal = DEFAULT_POOL_CBTOTAL;
pool_config *p = &conf->pools[i];
pr_debug("Pool %d: prio: %d stratum: %s:%d login: %s/%s timeout: %d",
i, p->priority, p->host, p->port, p->miner, p->passwd, p->timeout);
}
return (i == 0);
}