-
Notifications
You must be signed in to change notification settings - Fork 34
/
client_core.cpp
348 lines (276 loc) · 7.58 KB
/
client_core.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
#include "stratum.h"
static int g_extraonce1_counter = 0;
void get_next_extraonce1(char *extraonce1)
{
CommonLock(&g_nonce1_mutex);
g_extraonce1_counter++;
sprintf(extraonce1, "%08x", g_extraonce1_counter|0x81000000);
CommonUnlock(&g_nonce1_mutex);
}
void get_random_key(char *key)
{
int i1 = rand();
int i2 = rand();
int i3 = rand();
int i4 = rand();
sprintf(key, "%08x%08x%08x%08x", i1, i2, i3, i4);
}
YAAMP_CLIENT *client_find_notify_id(const char *notify_id, bool reconnecting)
{
if (!notify_id || !strlen(notify_id))
return NULL;
g_list_client.Enter();
for(CLI li = g_list_client.first; li; li = li->next)
{
YAAMP_CLIENT *client = (YAAMP_CLIENT *)li->data;
if(client->reconnecting == reconnecting && !strcmp(client->notify_id, notify_id))
{
g_list_client.Leave();
return client;
}
}
g_list_client.Leave();
return NULL;
}
void client_sort()
{
for(CLI li = g_list_client.first; li && li->next; li = li->next)
{
YAAMP_CLIENT *client1 = (YAAMP_CLIENT *)li->data;
YAAMP_CLIENT *client2 = (YAAMP_CLIENT *)li->next->data;
// if(client2->difficulty_actual > client1->difficulty_actual)
if(client2->speed > client1->speed*1.5)
{
g_list_client.Swap(li, li->next);
client_sort();
return;
}
}
}
int client_send_error(YAAMP_CLIENT *client, int error, const char *string)
{
char buffer3[1024];
if(client->id_str)
sprintf(buffer3, "\"%s\"", client->id_str);
else
sprintf(buffer3, "%d", client->id_int);
return socket_send(client->sock, "{\"id\":%s,\"result\":false,\"error\":[%d,\"%s\",null]}\n", buffer3, error, string);
}
int client_send_result(YAAMP_CLIENT *client, const char *format, ...)
{
char buffer[YAAMP_SMALLBUFSIZE];
va_list args;
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
char buffer3[1024];
if(client->id_str)
sprintf(buffer3, "\"%s\"", client->id_str);
else
sprintf(buffer3, "%d", client->id_int);
return socket_send(client->sock, "{\"id\":%s,\"result\":%s,\"error\":null}\n", buffer3, buffer);
}
int client_call(YAAMP_CLIENT *client, const char *method, const char *format, ...)
{
char buffer[YAAMP_SMALLBUFSIZE];
va_list args;
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
return socket_send(client->sock, "{\"id\":null,\"method\":\"%s\",\"params\":%s}\n", method, buffer);
}
int client_ask(YAAMP_CLIENT *client, const char *method, const char *format, ...)
{
char buffer[YAAMP_SMALLBUFSIZE];
va_list args;
int64_t id = client->shares;
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
int ret = socket_send(client->sock, "{\"id\":%d,\"method\":\"%s\",\"params\":%s}\n", id, method, buffer);
if (ret == -1) {
debuglog("unable to ask %s\n", method);
return 0; // -errno
}
client->reqid = id;
return id;
}
void client_block_ip(YAAMP_CLIENT *client, const char *reason)
{
char buffer[1024];
sprintf(buffer, "iptables -A INPUT -s %s -p tcp --dport %d -j REJECT", client->sock->ip, g_tcp_port);
if(strcmp("0.0.0.0", client->sock->ip) == 0) return;
if(strstr(client->sock->ip, "192.168.")) return;
if(strstr(client->sock->ip, "127.0.0.")) return;
int s = system(buffer);
stratumlog("%s: %s blocked (%s)\n", g_stratum_algo, client->sock->ip, reason);
}
void client_block_ipset(YAAMP_CLIENT *client, const char *ipset_name)
{
char buffer[1024];
sprintf(buffer, "ipset -q -A %s %s", ipset_name, client->sock->ip);
if(strcmp("0.0.0.0", client->sock->ip) == 0) return;
if(strstr(client->sock->ip, "192.168.")) return;
if(strstr(client->sock->ip, "127.0.0.")) return;
int s = system(buffer);
stratumlog("%s: %s blocked via ipset %s %s\n", g_stratum_algo, client->sock->ip, ipset_name, client->username);
}
bool client_reset_multialgo(YAAMP_CLIENT *client, bool first)
{
// return false;
if(!client->algos_subscribed[0].algo) return false;
// debuglog("client_reset_multialgo\n");
YAAMP_CLIENT_ALGO *best = NULL;
YAAMP_CLIENT_ALGO *current = NULL;
for(int i=0; g_algos[i].name[0]; i++)
{
YAAMP_ALGO *algo = &g_algos[i];
for(int j=0; client->algos_subscribed[j].algo; j++)
{
YAAMP_CLIENT_ALGO *candidate = &client->algos_subscribed[j];
if(candidate->algo == algo)
{
if(!best || algo->profit*candidate->factor > best->algo->profit*best->factor)
best = candidate;
}
if(!current && candidate->algo == g_current_algo)
current = candidate;
}
}
if(!best || !current || best == current)
{
client->last_best = time(NULL);
return false;
}
if(!first)
{
int e = time(NULL) - client->last_best;
double d = best->algo->profit*best->factor - current->algo->profit*current->factor;
double p = d/best->algo->profit/best->factor;
#ifdef DEBUG_BEST_MULTI
debuglog("current %s %f\n", current->algo->name, current->algo->profit*current->factor);
debuglog("best %s %f\n", best->algo->name, best->algo->profit*best->factor);
debuglog(" %d * %f = %f --- percent %f %f\n", e, d, e*d, p, e*p);
#endif
if(p < 0.02) return false;
if(e*p < 100) return false;
}
shutdown(client->sock->sock, SHUT_RDWR);
return true;
}
bool client_initialize_multialgo(YAAMP_CLIENT *client)
{
char *p = strstr(client->password, "p=");
if(p)
{
double profit = atof(p+2);
if(profit > g_current_algo->profit)
return true;
}
char tmp[1024];
memset(tmp, 0, 1024);
strncpy(tmp, client->password, 1023);
p = tmp;
while(p)
{
double value = 0;
char *p1 = strchr(p, ',');
if(p1) *p1 = 0;
char *p2 = strchr(p, '=');
if(p2)
{
*p2 = 0;
value = atof(p2+1);
}
for(int i=0; g_algos[i].name[0]; i++)
{
YAAMP_ALGO *algo = &g_algos[i];
if(!strcmp(algo->name, p))
{
int i=0;
for(; i<YAAMP_MAXALGOS-1 && client->algos_subscribed[i].algo; i++);
client->algos_subscribed[i].algo = algo;
client->algos_subscribed[i].factor = value? value: algo->factor;
}
}
p = p1? p1+1: p1;
}
bool reset = client_reset_multialgo(client, true);
return reset;
}
void client_add_job_history(YAAMP_CLIENT *client, int jobid)
{
if(!jobid)
{
debuglog("trying to add jobid 0\n");
return;
}
bool b = client_find_job_history(client, jobid, 0);
if(b)
{
// debuglog("ERROR history already added job %x\n", jobid);
return;
}
for(int i=YAAMP_JOB_MAXHISTORY-1; i>0; i--)
client->job_history[i] = client->job_history[i-1];
client->job_history[0] = jobid;
}
bool client_find_job_history(YAAMP_CLIENT *client, int jobid, int startat)
{
for(int i=startat; i<YAAMP_JOB_MAXHISTORY; i++)
{
if(client->job_history[i] == jobid)
{
// if(!startat)
// debuglog("job %x already sent, index %d\n", jobid, i);
return true;
}
}
return false;
}
int hostname_to_ip(const char *hostname , char* ip)
{
struct hostent *he;
struct in_addr **addr_list;
int i;
if(hostname[0]>='0' && hostname[0]<='9')
{
strcpy(ip, hostname);
return 0;
}
if ( (he = gethostbyname( hostname ) ) == NULL)
{
// get the host info
herror("gethostbyname");
return 1;
}
addr_list = (struct in_addr **) he->h_addr_list;
for(i = 0; addr_list[i] != NULL; i++)
{
//Return the first one;
strcpy(ip, inet_ntoa(*addr_list[i]));
return 0;
}
return 1;
}
bool client_find_my_ip(const char *name)
{
// return false;
char ip[1024] = "";
hostname_to_ip(name, ip);
if(!ip[0]) return false;
char host[NI_MAXHOST];
for(struct ifaddrs *ifa = g_ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
if(ifa->ifa_addr == NULL) continue;
host[0] = 0;
getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if(!host[0]) continue;
if(!strcmp(host, ip))
{
debuglog("found my ip %s\n", ip);
return true;
}
}
return false;
}