forked from xmrig/xmrig-aeon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stratum.c
696 lines (563 loc) · 16 KB
/
stratum.c
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/* XMRig
* Copyright 2010 Jeff Garzik <[email protected]>
* Copyright 2012-2014 pooler <[email protected]>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <[email protected]>
* Copyright 2016-2017 XMRig <[email protected]>
*
*
* 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
* (at your option) 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/>.
*/
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <jansson.h>
#include <unistd.h>
#if defined(WIN32)
# include <winsock2.h>
# include <mstcpip.h>
#else
# include <errno.h>
# include <netinet/tcp.h>
# include <poll.h>
#endif
#include "stratum.h"
#include "version.h"
#include "stats.h"
#include "util.h"
#include "utils/applog.h"
#ifdef WIN32
# define socket_blocks() (WSAGetLastError() == WSAEWOULDBLOCK)
# define poll(fdarray, nfds, timeout) WSAPoll(fdarray, nfds, timeout)
# define SHUT_RDWR SD_BOTH
#else
# define socket_blocks() (errno == EAGAIN || errno == EWOULDBLOCK)
# define closesocket(x) close((x))
#endif
#define RBUFSIZE 2048
#define RECVSIZE (RBUFSIZE - 4)
#define unlikely(expr) (__builtin_expect(!!(expr), 0))
static struct work work;
static bool send_line(curl_socket_t sock, char *s);
static bool socket_full(curl_socket_t sock, int timeout);
static void buffer_append(struct stratum_ctx *sctx, const char *s);
static bool job(struct stratum_ctx *sctx, json_t *params);
static int sockopt_keepalive_cb(void *userdata, curl_socket_t fd, curlsocktype purpose);
static curl_socket_t opensocket_grab_cb(void *clientp, curlsocktype purpose, struct curl_sockaddr *addr);
static int closesocket_cb(void *clientp, curl_socket_t item);
static bool login_decode(struct stratum_ctx *sctx, const json_t *val);
static bool job_decode(const json_t *job);
static bool jobj_binary(const json_t *obj, const char *key, void *buf, size_t buflen);
/**
* @brief stratum_socket_full
* @param sctx
* @param timeout
* @return
*/
bool stratum_socket_full(struct stratum_ctx *sctx, int timeout)
{
return strlen(sctx->sockbuf) || socket_full(sctx->sock, timeout);
}
/**
* @brief stratum_send_line
* @param sctx
* @param s
* @return
*/
bool stratum_send_line(struct stratum_ctx *sctx, char *s)
{
bool ret = false;
pthread_mutex_lock(&sctx->sock_lock);
ret = send_line(sctx->sock, s);
pthread_mutex_unlock(&sctx->sock_lock);
return ret;
}
/**
* @brief stratum_recv_line
* @param sctx
* @return
*/
char *stratum_recv_line(struct stratum_ctx *sctx)
{
if (!strstr(sctx->sockbuf, "\n")) {
bool ret = true;
time_t rstart;
time(&rstart);
if (!socket_full(sctx->sock, 60)) {
applog(LOG_ERR, "stratum_recv_line timed out");
return NULL;
}
do {
char s[RBUFSIZE];
ssize_t n;
memset(s, 0, RBUFSIZE);
n = recv(sctx->sock, s, RECVSIZE, 0);
if (!n) {
ret = false;
break;
}
if (n < 0) {
if (!socket_blocks() || !socket_full(sctx->sock, 1)) {
ret = false;
break;
}
} else {
buffer_append(sctx, s);
}
} while (time(NULL) - rstart < 60 && !strstr(sctx->sockbuf, "\n"));
if (!ret) {
applog(LOG_ERR, "stratum_recv_line failed");
return NULL;
}
}
ssize_t buflen = strlen(sctx->sockbuf);
char *tok = strtok(sctx->sockbuf, "\n");
if (!tok) {
applog(LOG_ERR, "stratum_recv_line failed to parse a newline-terminated string");
return NULL;
}
char *sret = strdup(tok);
ssize_t len = strlen(sret);
if (buflen > len + 1) {
memmove(sctx->sockbuf, sctx->sockbuf + len + 1, buflen - len + 1);
}
else {
sctx->sockbuf[0] = '\0';
}
return sret;
}
/**
* @brief stratum_disconnect
* @param sctx
*/
void stratum_disconnect(struct stratum_ctx *sctx)
{
pthread_mutex_lock(&sctx->sock_lock);
sctx->ready = false;
if (sctx->curl) {
curl_easy_cleanup(sctx->curl);
sctx->curl = NULL;
sctx->sockbuf[0] = '\0';
}
pthread_mutex_unlock(&sctx->sock_lock);
}
/**
* @brief stratum_handle_method
* @param sctx
* @param s
* @return
*/
bool stratum_handle_method(struct stratum_ctx *sctx, const char *s)
{
bool ret = false;
const char *method;
json_t *val = json_decode(s);
if (!val) {
return false;
}
if (method = json_string_value(json_object_get(val, "method"))) {
if (!strcasecmp(method, "job")) {
ret = job(sctx, json_object_get(val, "params"));
}
else {
applog(LOG_WARNING, "Unknown method: \"%s\"", method);
}
}
json_decref(val);
return ret;
}
/**
* @brief stratum_handle_response
* @param buf
* @return
*/
bool stratum_handle_response(char *buf) {
bool valid = false;
json_t *val = json_decode(buf);
if (!val) {
return false;
}
json_t *res_val = json_object_get(val, "result");
json_t *err_val = json_object_get(val, "error");
json_t *id_val = json_object_get(val, "id");
if (!id_val || json_is_null(id_val) || !res_val) {
json_decref(val);
return false;
}
json_t *status = json_object_get(res_val, "status");
if (!strcmp(json_string_value(status), "KEEPALIVED") ) {
applog(LOG_DEBUG, "Keepalived receveid");
json_decref(val);
return true;
}
if (status) {
valid = !strcmp(json_string_value(status), "OK") && json_is_null(err_val);
} else {
valid = json_is_null(err_val);
}
stats_share_result(valid);
json_decref(val);
return true;
}
/**
* @brief stratum_keepalived
* @param sctx
* @return
*/
bool stratum_keepalived(struct stratum_ctx *sctx)
{
char *s = malloc(128);
snprintf(s, 128, "{\"method\":\"keepalived\",\"params\":{\"id\":\"%s\"},\"id\":1}", sctx->id);
bool ret = stratum_send_line(sctx, s);
free(s);
return ret;
}
/**
* @brief stratum_authorize
* @param sctx
* @param user
* @param pass
* @return
*/
bool stratum_authorize(struct stratum_ctx *sctx, const char *user, const char *pass)
{
char *sret;
json_error_t err;
char *req = malloc(128 + strlen(user) + strlen(pass));
sprintf(req, "{\"method\":\"login\",\"params\":{\"login\":\"%s\",\"pass\":\"%s\",\"agent\":\"%s/%s\"},\"id\":1}", user, pass, APP_NAME, APP_VERSION);
if (!stratum_send_line(sctx, req)) {
free(req);
return false;
}
free(req);
while (1) {
sret = stratum_recv_line(sctx);
if (!sret) {
return false;
}
if (!stratum_handle_method(sctx, sret)) {
break;
}
free(sret);
}
json_t *val = json_decode(sret);
free(sret);
if (!val) {
return false;
}
json_t *result = json_object_get(val, "result");
json_t *error = json_object_get(val, "error");
if (!result || json_is_false(result) || (error && !json_is_null(error))) {
applog(LOG_ERR, "Stratum authentication failed");
json_decref(val);
return false;
}
if (login_decode(sctx, val) && job(sctx, json_object_get(result, "job"))) {
pthread_mutex_lock(&sctx->sock_lock);
sctx->ready = true;
pthread_mutex_unlock(&sctx->sock_lock);
}
json_decref(val);
return true;
}
/**
* @brief stratum_connect
* @param sctx
* @param url
* @return
*/
bool stratum_connect(struct stratum_ctx *sctx, const char *url)
{
CURL *curl;
pthread_mutex_lock(&sctx->sock_lock);
sctx->ready = false;
if (sctx->curl) {
curl_easy_cleanup(sctx->curl);
}
sctx->curl = curl_easy_init();
if (!sctx->curl) {
applog(LOG_ERR, "CURL initialization failed");
pthread_mutex_unlock(&sctx->sock_lock);
return false;
}
curl = sctx->curl;
if (!sctx->sockbuf) {
sctx->sockbuf = calloc(RBUFSIZE, 1);
sctx->sockbuf_size = RBUFSIZE;
}
sctx->sockbuf[0] = '\0';
pthread_mutex_unlock(&sctx->sock_lock);
if (url != sctx->url) {
free(sctx->url);
sctx->url = strdup(url);
}
free(sctx->curl_url);
sctx->curl_url = malloc(strlen(url));
sprintf(sctx->curl_url, "http%s/", strstr(url, "://"));
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
curl_easy_setopt(curl, CURLOPT_URL, sctx->curl_url);
curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, sctx->curl_err_str);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_keepalive_cb);
curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket_grab_cb);
curl_easy_setopt(curl, CURLOPT_CLOSESOCKETFUNCTION, closesocket_cb);
curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sctx->sock);
curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1);
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
int rc = curl_easy_perform(curl);
if (rc) {
applog(LOG_ERR, "Stratum connection failed: code: %d, text: %s", rc, sctx->curl_err_str);
curl_easy_cleanup(curl);
sctx->curl = NULL;
return false;
}
return true;
}
/**
* @brief send_line
* @param sock
* @param s
* @return
*/
static bool send_line(curl_socket_t sock, char *s)
{
ssize_t len, sent = 0;
len = strlen(s);
s[len++] = '\n';
while (len > 0) {
struct pollfd pfd;
pfd.fd = sock;
pfd.events = POLLOUT;
if (poll(&pfd, 1, 0) < 1) {
return false;
}
ssize_t n = send(sock, s + sent, len, 0);
if (n < 0) {
if (!socket_blocks()) {
return false;
}
n = 0;
}
sent += n;
len -= n;
}
return true;
}
/**
* @brief socket_full
* @param sock
* @param timeout
* @return
*/
static bool socket_full(curl_socket_t sock, int timeout)
{
struct pollfd pfd;
pfd.fd = sock;
pfd.events = POLLIN;
return poll(&pfd, 1, timeout * 1000) > 0;
}
/**
* @brief buffer_append
* @param sctx
* @param s
*/
static void buffer_append(struct stratum_ctx *sctx, const char *s)
{
size_t old, new;
old = strlen(sctx->sockbuf);
new = old + strlen(s) + 1;
if (new >= sctx->sockbuf_size) {
sctx->sockbuf_size = new + (RBUFSIZE - (new % RBUFSIZE));
sctx->sockbuf = realloc(sctx->sockbuf, sctx->sockbuf_size);
}
strcpy(sctx->sockbuf + old, s);
}
/**
* @brief job
* @param sctx
* @param params
* @return
*/
static bool job(struct stratum_ctx *sctx, json_t *params)
{
if (!job_decode(params)) {
return false;
}
pthread_mutex_lock(&sctx->work_lock);
if (sctx->work.target != work.target) {
stats_set_target(work.target);
}
memcpy(&sctx->work, &work, sizeof(struct work));
pthread_mutex_unlock(&sctx->work_lock);
return true;
}
/**
* @brief sockopt_keepalive_cb
* @param userdata
* @param fd
* @param purpose
* @return
*/
static int sockopt_keepalive_cb(void *userdata, curl_socket_t fd, curlsocktype purpose)
{
int keepalive = 1;
int tcp_keepcnt = 3;
int tcp_keepidle = 50;
int tcp_keepintvl = 50;
#ifndef WIN32
if (unlikely(setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &keepalive,
sizeof(keepalive))))
return 1;
#ifdef __linux
if (unlikely(setsockopt(fd, SOL_TCP, TCP_KEEPCNT,
&tcp_keepcnt, sizeof(tcp_keepcnt))))
return 1;
if (unlikely(setsockopt(fd, SOL_TCP, TCP_KEEPIDLE,
&tcp_keepidle, sizeof(tcp_keepidle))))
return 1;
if (unlikely(setsockopt(fd, SOL_TCP, TCP_KEEPINTVL,
&tcp_keepintvl, sizeof(tcp_keepintvl))))
return 1;
#endif /* __linux */
#ifdef __APPLE_CC__
if (unlikely(setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE,
&tcp_keepintvl, sizeof(tcp_keepintvl))))
return 1;
#endif /* __APPLE_CC__ */
#else /* WIN32 */
struct tcp_keepalive vals;
vals.onoff = 1;
vals.keepalivetime = tcp_keepidle * 1000;
vals.keepaliveinterval = tcp_keepintvl * 1000;
DWORD outputBytes;
if (unlikely(WSAIoctl(fd, SIO_KEEPALIVE_VALS, &vals, sizeof(vals), NULL, 0, &outputBytes, NULL, NULL))) {
return 1;
}
#endif /* WIN32 */
return 0;
}
static int closesocket_cb(void *clientp, curl_socket_t item) {
shutdown(item, SHUT_RDWR);
return closesocket(item);
}
/**
* @brief opensocket_grab_cb
* @param clientp
* @param purpose
* @param addr
* @return
*/
static curl_socket_t opensocket_grab_cb(void *clientp, curlsocktype purpose, struct curl_sockaddr *addr)
{
curl_socket_t *sock = clientp;
*sock = socket(addr->family, addr->socktype, addr->protocol);
return *sock;
}
/**
* @brief login_decode
* @param sctx
* @param val
* @return
*/
static bool login_decode(struct stratum_ctx *sctx, const json_t *val) {
json_t *res = json_object_get(val, "result");
if (!res) {
applog(LOG_ERR, "JSON invalid result");
return false;
}
const char *id = json_string_value(json_object_get(res, "id"));
if (!id || strlen(id) >= (sizeof(sctx->id))) {
applog(LOG_ERR, "JSON invalid id");
return false;
}
memset(&sctx->id, 0, sizeof(sctx->id));
memcpy(&sctx->id, id, strlen(id));
const char *s = json_string_value(json_object_get(res, "status"));
if (!s) {
applog(LOG_ERR, "JSON invalid status");
return false;
}
if (strcmp(s, "OK")) {
applog(LOG_ERR, "JSON returned status \"%s\"", s);
return false;
}
return true;
}
/**
* @brief job_decode
* @param sctx
* @param job
* @param work
* @return
*/
static bool job_decode(const json_t *job) {
const char *job_id = json_string_value(json_object_get(job, "job_id"));
if (!job_id || strlen(job_id) >= sizeof(work.job_id)) {
applog(LOG_ERR, "JSON invalid job id");
return false;
}
const char *blob = json_string_value(json_object_get(job, "blob"));
if (!blob) {
applog(LOG_ERR, "JSON invalid blob");
return false;
}
work.blob_size = strlen(blob);
if (work.blob_size % 2 != 0) {
applog(LOG_ERR, "JSON invalid blob length");
return false;
}
work.blob_size /= 2;
if (work.blob_size < 76 || work.blob_size > (sizeof(work.blob))) {
applog(LOG_ERR, "JSON invalid blob length");
return false;
}
if (!hex2bin((unsigned char *) work.blob, blob, work.blob_size)) {
applog(LOG_ERR, "JSON invalid blob");
return false;
}
jobj_binary(job, "target", &work.target, 4);
memset(work.job_id, 0, sizeof(work.job_id));
memcpy(work.job_id, job_id, strlen(job_id));
return true;
}
/**
* @brief jobj_binary
* @param obj
* @param key
* @param buf
* @param buflen
* @return
*/
static bool jobj_binary(const json_t *obj, const char *key, void *buf, size_t buflen) {
const char *hexstr;
json_t *tmp;
tmp = json_object_get(obj, key);
if (unlikely(!tmp)) {
applog(LOG_ERR, "JSON key '%s' not found", key);
return false;
}
hexstr = json_string_value(tmp);
if (unlikely(!hexstr)) {
applog(LOG_ERR, "JSON key '%s' is not a string", key);
return false;
}
if (!hex2bin(buf, hexstr, buflen)) {
return false;
}
return true;
}