]> Git Repo - cpuminer-multi.git/blob - util.c
fix a few vstudio warnings
[cpuminer-multi.git] / util.c
1 /*
2  * Copyright 2010 Jeff Garzik
3  * Copyright 2012 Luke Dashjr
4  * Copyright 2012-2014 pooler
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 2 of the License, or (at your option)
9  * any later version.  See COPYING for more details.
10  */
11
12 #define _GNU_SOURCE
13 #include <cpuminer-config.h>
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <ctype.h>
18 #include <stdarg.h>
19 #include <string.h>
20 #include <stdbool.h>
21 #include <inttypes.h>
22 #include <limits.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <jansson.h>
26 #include <curl/curl.h>
27 #include <time.h>
28 #include <sys/stat.h>
29 #if defined(WIN32)
30 #include <winsock2.h>
31 #include <mstcpip.h>
32 #include "compat/winansi.h"
33 #else
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <netinet/tcp.h>
37 #endif
38
39 #ifndef _MSC_VER
40 /* dirname() linux/mingw, else in compat.h */
41 #include <libgen.h>
42 #endif
43
44 #include "miner.h"
45 #include "elist.h"
46
47 extern pthread_mutex_t stats_lock;
48
49 struct data_buffer {
50         void            *buf;
51         size_t          len;
52 };
53
54 struct upload_buffer {
55         const void      *buf;
56         size_t          len;
57         size_t          pos;
58 };
59
60 struct header_info {
61         char            *lp_path;
62         char            *reason;
63         char            *stratum_url;
64 };
65
66 struct tq_ent {
67         void                    *data;
68         struct list_head        q_node;
69 };
70
71 struct thread_q {
72         struct list_head        q;
73
74         bool frozen;
75
76         pthread_mutex_t         mutex;
77         pthread_cond_t          cond;
78 };
79
80 void applog(int prio, const char *fmt, ...)
81 {
82         va_list ap;
83
84         va_start(ap, fmt);
85
86 #ifdef HAVE_SYSLOG_H
87         if (use_syslog) {
88                 va_list ap2;
89                 char *buf;
90                 int len;
91
92                 /* custom colors to syslog prio */
93                 if (prio > LOG_DEBUG) {
94                         switch (prio) {
95                                 case LOG_BLUE: prio = LOG_NOTICE; break;
96                         }
97                 }
98
99                 va_copy(ap2, ap);
100                 len = vsnprintf(NULL, 0, fmt, ap2) + 1;
101                 va_end(ap2);
102                 buf = alloca(len);
103                 if (vsnprintf(buf, len, fmt, ap) >= 0)
104                         syslog(prio, "%s", buf);
105         }
106 #else
107         if (0) {}
108 #endif
109         else {
110                 const char* color = "";
111                 char *f;
112                 int len;
113                 struct tm tm;
114                 time_t now = time(NULL);
115
116                 localtime_r(&now, &tm);
117
118                 switch (prio) {
119                         case LOG_ERR:     color = CL_RED; break;
120                         case LOG_WARNING: color = CL_YLW; break;
121                         case LOG_NOTICE:  color = CL_WHT; break;
122                         case LOG_INFO:    color = ""; break;
123                         case LOG_DEBUG:   color = CL_GRY; break;
124
125                         case LOG_BLUE:
126                                 prio = LOG_NOTICE;
127                                 color = CL_CYN;
128                                 break;
129                 }
130                 if (!use_colors)
131                         color = "";
132
133                 len = 64 + (int) strlen(fmt) + 2;
134                 f = (char*) malloc(len);
135                 sprintf(f, "[%d-%02d-%02d %02d:%02d:%02d]%s %s%s\n",
136                         tm.tm_year + 1900,
137                         tm.tm_mon + 1,
138                         tm.tm_mday,
139                         tm.tm_hour,
140                         tm.tm_min,
141                         tm.tm_sec,
142                         color,
143                         fmt,
144                         use_colors ? CL_N : ""
145                 );
146                 pthread_mutex_lock(&applog_lock);
147                 vfprintf(stdout, f, ap);        /* atomic write to stdout */
148                 fflush(stdout);
149                 free(f);
150                 pthread_mutex_unlock(&applog_lock);
151         }
152         va_end(ap);
153 }
154
155 /* Get default config.json path (will be system specific) */
156 void get_defconfig_path(char *out, size_t bufsize, char *argv0)
157 {
158         char *cmd = strdup(argv0);
159         char *dir = dirname(cmd);
160         const char *sep = strstr(dir, "\\") ? "\\" : "/";
161         struct stat info = { 0 };
162 #ifdef WIN32
163         snprintf(out, bufsize, "%s\\cpuminer\\cpuminer-conf.json", getenv("APPDATA"));
164 #else
165         snprintf(out, bufsize, "%s\\.cpuminer\\cpuminer-conf.json", getenv("HOME"));
166 #endif
167         if (dir && stat(out, &info) != 0) {
168                 snprintf(out, bufsize, "%s%scpuminer-conf.json", dir, sep);
169         }
170         if (stat(out, &info) != 0) {
171                 out[0] = '\0';
172                 return;
173         }
174         out[bufsize - 1] = '\0';
175         free(cmd);
176 }
177
178
179 void format_hashrate(double hashrate, char *output)
180 {
181         char prefix = '\0';
182
183         if (hashrate < 10000) {
184                 // nop
185         }
186         else if (hashrate < 1e7) {
187                 prefix = 'k';
188                 hashrate *= 1e-3;
189         }
190         else if (hashrate < 1e10) {
191                 prefix = 'M';
192                 hashrate *= 1e-6;
193         }
194         else if (hashrate < 1e13) {
195                 prefix = 'G';
196                 hashrate *= 1e-9;
197         }
198         else {
199                 prefix = 'T';
200                 hashrate *= 1e-12;
201         }
202
203         sprintf(
204                 output,
205                 prefix ? "%.2f %cH/s" : "%.2f H/s%c",
206                 hashrate, prefix
207         );
208 }
209
210 /* Modify the representation of integer numbers which would cause an overflow
211  * so that they are treated as floating-point numbers.
212  * This is a hack to overcome the limitations of some versions of Jansson. */
213 static char *hack_json_numbers(const char *in)
214 {
215         char *out;
216         int i, off, intoff;
217         bool in_str, in_int;
218
219         out = (char*) calloc(2 * strlen(in) + 1, 1);
220         if (!out)
221                 return NULL;
222         off = intoff = 0;
223         in_str = in_int = false;
224         for (i = 0; in[i]; i++) {
225                 char c = in[i];
226                 if (c == '"') {
227                         in_str = !in_str;
228                 } else if (c == '\\') {
229                         out[off++] = c;
230                         if (!in[++i])
231                                 break;
232                 } else if (!in_str && !in_int && isdigit(c)) {
233                         intoff = off;
234                         in_int = true;
235                 } else if (in_int && !isdigit(c)) {
236                         if (c != '.' && c != 'e' && c != 'E' && c != '+' && c != '-') {
237                                 in_int = false;
238                                 if (off - intoff > 4) {
239                                         char *end;
240 #if JSON_INTEGER_IS_LONG_LONG
241                                         errno = 0;
242                                         strtoll(out + intoff, &end, 10);
243                                         if (!*end && errno == ERANGE) {
244 #else
245                                         long l;
246                                         errno = 0;
247                                         l = strtol(out + intoff, &end, 10);
248                                         if (!*end && (errno == ERANGE || l > INT_MAX)) {
249 #endif
250                                                 out[off++] = '.';
251                                                 out[off++] = '0';
252                                         }
253                                 }
254                         }
255                 }
256                 out[off++] = in[i];
257         }
258         return out;
259 }
260
261 static void databuf_free(struct data_buffer *db)
262 {
263         if (!db)
264                 return;
265
266         free(db->buf);
267
268         memset(db, 0, sizeof(*db));
269 }
270
271 static size_t all_data_cb(const void *ptr, size_t size, size_t nmemb,
272                           void *user_data)
273 {
274         struct data_buffer *db = (struct data_buffer *) user_data;
275         size_t len = size * nmemb;
276         size_t oldlen, newlen;
277         void *newmem;
278         static const unsigned char zero = 0;
279
280         oldlen = db->len;
281         newlen = oldlen + len;
282
283         newmem = realloc(db->buf, newlen + 1);
284         if (!newmem)
285                 return 0;
286
287         db->buf = newmem;
288         db->len = newlen;
289         memcpy((uchar*) db->buf + oldlen, ptr, len);
290         memcpy((uchar*) db->buf + newlen, &zero, 1);    /* null terminate */
291
292         return len;
293 }
294
295 static size_t upload_data_cb(void *ptr, size_t size, size_t nmemb,
296                              void *user_data)
297 {
298         struct upload_buffer *ub = (struct upload_buffer *) user_data;
299         size_t len = size * nmemb;
300
301         if (len > ub->len - ub->pos)
302                 len = ub->len - ub->pos;
303
304         if (len) {
305                 memcpy(ptr, ((uchar*)ub->buf) + ub->pos, len);
306                 ub->pos += len;
307         }
308
309         return len;
310 }
311
312 #if LIBCURL_VERSION_NUM >= 0x071200
313 static int seek_data_cb(void *user_data, curl_off_t offset, int origin)
314 {
315         struct upload_buffer *ub = (struct upload_buffer *) user_data;
316         
317         switch (origin) {
318         case SEEK_SET:
319                 ub->pos = (size_t) offset;
320                 break;
321         case SEEK_CUR:
322                 ub->pos += (size_t) offset;
323                 break;
324         case SEEK_END:
325                 ub->pos = ub->len + (size_t) offset;
326                 break;
327         default:
328                 return 1; /* CURL_SEEKFUNC_FAIL */
329         }
330
331         return 0; /* CURL_SEEKFUNC_OK */
332 }
333 #endif
334
335 static size_t resp_hdr_cb(void *ptr, size_t size, size_t nmemb, void *user_data)
336 {
337         struct header_info *hi = (struct header_info *) user_data;
338         size_t remlen, slen, ptrlen = size * nmemb;
339         char *rem, *val = NULL, *key = NULL;
340         void *tmp;
341
342         val = (char*) calloc(1, ptrlen);
343         key = (char*) calloc(1, ptrlen);
344         if (!key || !val)
345                 goto out;
346
347         tmp = memchr(ptr, ':', ptrlen);
348         if (!tmp || (tmp == ptr))       /* skip empty keys / blanks */
349                 goto out;
350         slen = (char*)tmp - (char*)ptr;
351         if ((slen + 1) == ptrlen)       /* skip key w/ no value */
352                 goto out;
353         memcpy(key, ptr, slen);         /* store & nul term key */
354         key[slen] = 0;
355
356         rem = (char*)ptr + slen + 1;            /* trim value's leading whitespace */
357         remlen = ptrlen - slen - 1;
358         while ((remlen > 0) && (isspace(*rem))) {
359                 remlen--;
360                 rem++;
361         }
362
363         memcpy(val, rem, remlen);       /* store value, trim trailing ws */
364         val[remlen] = 0;
365         while ((*val) && (isspace(val[strlen(val) - 1]))) {
366                 val[strlen(val) - 1] = 0;
367         }
368
369         if (!strcasecmp("X-Long-Polling", key)) {
370                 hi->lp_path = val;      /* steal memory reference */
371                 val = NULL;
372         }
373
374         if (!strcasecmp("X-Reject-Reason", key)) {
375                 hi->reason = val;       /* steal memory reference */
376                 val = NULL;
377         }
378
379         if (!strcasecmp("X-Stratum", key)) {
380                 hi->stratum_url = val;  /* steal memory reference */
381                 val = NULL;
382         }
383
384 out:
385         free(key);
386         free(val);
387         return ptrlen;
388 }
389
390 #if LIBCURL_VERSION_NUM >= 0x070f06
391 static int sockopt_keepalive_cb(void *userdata, curl_socket_t fd,
392         curlsocktype purpose)
393 {
394 #ifdef __linux
395         int tcp_keepcnt = 3;
396 #endif
397         int tcp_keepintvl = 50;
398         int tcp_keepidle = 50;
399 #ifndef WIN32
400         int keepalive = 1;
401         if (unlikely(setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &keepalive,
402                 sizeof(keepalive))))
403                 return 1;
404 #ifdef __linux
405         if (unlikely(setsockopt(fd, SOL_TCP, TCP_KEEPCNT,
406                 &tcp_keepcnt, sizeof(tcp_keepcnt))))
407                 return 1;
408         if (unlikely(setsockopt(fd, SOL_TCP, TCP_KEEPIDLE,
409                 &tcp_keepidle, sizeof(tcp_keepidle))))
410                 return 1;
411         if (unlikely(setsockopt(fd, SOL_TCP, TCP_KEEPINTVL,
412                 &tcp_keepintvl, sizeof(tcp_keepintvl))))
413                 return 1;
414 #endif /* __linux */
415 #ifdef __APPLE_CC__
416         if (unlikely(setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE,
417                 &tcp_keepintvl, sizeof(tcp_keepintvl))))
418                 return 1;
419 #endif /* __APPLE_CC__ */
420 #else /* WIN32 */
421         struct tcp_keepalive vals;
422         vals.onoff = 1;
423         vals.keepalivetime = tcp_keepidle * 1000;
424         vals.keepaliveinterval = tcp_keepintvl * 1000;
425         DWORD outputBytes;
426         if (unlikely(WSAIoctl(fd, SIO_KEEPALIVE_VALS, &vals, sizeof(vals),
427                 NULL, 0, &outputBytes, NULL, NULL)))
428                 return 1;
429 #endif /* WIN32 */
430
431         return 0;
432 }
433 #endif
434
435 json_t *json_rpc_call(CURL *curl, const char *url,
436                       const char *userpass, const char *rpc_req,
437                       int *curl_err, int flags)
438 {
439         json_t *val, *err_val, *res_val;
440         int rc;
441         long http_rc;
442         struct data_buffer all_data = {0};
443         struct upload_buffer upload_data;
444         char *json_buf;
445         json_error_t err;
446         struct curl_slist *headers = NULL;
447         char len_hdr[64];
448         char curl_err_str[CURL_ERROR_SIZE] = { 0 };
449         long timeout = (flags & JSON_RPC_LONGPOLL) ? opt_timeout : 30;
450         struct header_info hi = {0};
451
452         /* it is assumed that 'curl' is freshly [re]initialized at this pt */
453
454         if (opt_protocol)
455                 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
456         curl_easy_setopt(curl, CURLOPT_URL, url);
457         if (opt_cert)
458                 curl_easy_setopt(curl, CURLOPT_CAINFO, opt_cert);
459         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
460         curl_easy_setopt(curl, CURLOPT_ENCODING, "");
461         curl_easy_setopt(curl, CURLOPT_FAILONERROR, 0);
462         curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
463         curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
464         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, all_data_cb);
465         curl_easy_setopt(curl, CURLOPT_WRITEDATA, &all_data);
466         curl_easy_setopt(curl, CURLOPT_READFUNCTION, upload_data_cb);
467         curl_easy_setopt(curl, CURLOPT_READDATA, &upload_data);
468 #if LIBCURL_VERSION_NUM >= 0x071200
469         curl_easy_setopt(curl, CURLOPT_SEEKFUNCTION, &seek_data_cb);
470         curl_easy_setopt(curl, CURLOPT_SEEKDATA, &upload_data);
471 #endif
472         curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_err_str);
473         if (opt_redirect)
474                 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
475         curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
476         curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, resp_hdr_cb);
477         curl_easy_setopt(curl, CURLOPT_HEADERDATA, &hi);
478         if (opt_proxy) {
479                 curl_easy_setopt(curl, CURLOPT_PROXY, opt_proxy);
480                 curl_easy_setopt(curl, CURLOPT_PROXYTYPE, opt_proxy_type);
481         }
482         if (userpass) {
483                 curl_easy_setopt(curl, CURLOPT_USERPWD, userpass);
484                 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
485         }
486 #if LIBCURL_VERSION_NUM >= 0x070f06
487         if (flags & JSON_RPC_LONGPOLL)
488                 curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_keepalive_cb);
489 #endif
490         curl_easy_setopt(curl, CURLOPT_POST, 1);
491
492         if (opt_protocol)
493                 applog(LOG_DEBUG, "JSON protocol request:\n%s\n", rpc_req);
494
495         upload_data.buf = rpc_req;
496         upload_data.len = strlen(rpc_req);
497         upload_data.pos = 0;
498         sprintf(len_hdr, "Content-Length: %lu",
499                 (unsigned long) upload_data.len);
500
501         headers = curl_slist_append(headers, "Content-Type: application/json");
502         headers = curl_slist_append(headers, len_hdr);
503         headers = curl_slist_append(headers, "User-Agent: " USER_AGENT);
504         headers = curl_slist_append(headers, "X-Mining-Extensions: longpoll reject-reason");
505         //headers = curl_slist_append(headers, "Accept:"); /* disable Accept hdr*/
506         //headers = curl_slist_append(headers, "Expect:"); /* disable Expect hdr*/
507
508         curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
509
510         rc = curl_easy_perform(curl);
511         if (curl_err != NULL)
512                 *curl_err = rc;
513         if (rc) {
514                 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_rc);
515                 if (!((flags & JSON_RPC_LONGPOLL) && rc == CURLE_OPERATION_TIMEDOUT) &&
516                     !((flags & JSON_RPC_QUIET_404) && http_rc == 404))
517                         applog(LOG_ERR, "HTTP request failed: %s", curl_err_str);
518                 if (curl_err && (flags & JSON_RPC_QUIET_404) && http_rc == 404)
519                         *curl_err = CURLE_OK;
520                 goto err_out;
521         }
522
523         /* If X-Stratum was found, activate Stratum */
524         if (want_stratum && hi.stratum_url &&
525             !strncasecmp(hi.stratum_url, "stratum+tcp://", 14)) {
526                 have_stratum = true;
527                 tq_push(thr_info[stratum_thr_id].q, hi.stratum_url);
528                 hi.stratum_url = NULL;
529         }
530
531         /* If X-Long-Polling was found, activate long polling */
532         if (!have_longpoll && want_longpoll && hi.lp_path && !have_gbt &&
533             allow_getwork && !have_stratum) {
534                 have_longpoll = true;
535                 tq_push(thr_info[longpoll_thr_id].q, hi.lp_path);
536                 hi.lp_path = NULL;
537         }
538
539         if (!all_data.buf) {
540                 applog(LOG_ERR, "Empty data received in json_rpc_call.");
541                 goto err_out;
542         }
543
544         json_buf = hack_json_numbers((char*) all_data.buf);
545         errno = 0; /* needed for Jansson < 2.1 */
546         val = JSON_LOADS(json_buf, &err);
547         free(json_buf);
548         if (!val) {
549                 applog(LOG_ERR, "JSON decode failed(%d): %s", err.line, err.text);
550                 goto err_out;
551         }
552
553         if (opt_protocol) {
554                 char *s = json_dumps(val, JSON_INDENT(3));
555                 applog(LOG_DEBUG, "JSON protocol response:\n%s", s);
556                 free(s);
557         }
558
559         /* JSON-RPC valid response returns a 'result' and a null 'error'. */
560         res_val = json_object_get(val, "result");
561         err_val = json_object_get(val, "error");
562
563         if (!res_val || (err_val && !json_is_null(err_val)
564                 && !(flags & JSON_RPC_IGNOREERR))) {
565
566                 char *s = NULL;
567
568                 if (err_val) {
569                         s = json_dumps(err_val, 0);
570                         json_t *msg = json_object_get(err_val, "message");
571                         json_t *err_code = json_object_get(err_val, "code");
572                         if (curl_err && json_integer_value(err_code))
573                                 *curl_err = (int)json_integer_value(err_code);
574
575                         if (msg && json_is_string(msg)) {
576                                 free(s);
577                                 s = strdup(json_string_value(msg));
578                                 if (have_longpoll && s && !strcmp(s, "method not getwork")) {
579                                         json_decref(err_val);
580                                         free(s);
581                                         goto err_out;
582                                 }
583                         }
584                         json_decref(err_val);
585                 }
586                 else
587                         s = strdup("(unknown reason)");
588
589                 if (!curl_err || opt_debug)
590                         applog(LOG_ERR, "JSON-RPC call failed: %s", s);
591
592                 free(s);
593
594                 goto err_out;
595         }
596
597         if (hi.reason)
598                 json_object_set_new(val, "reject-reason", json_string(hi.reason));
599
600         databuf_free(&all_data);
601         curl_slist_free_all(headers);
602         curl_easy_reset(curl);
603         return val;
604
605 err_out:
606         free(hi.lp_path);
607         free(hi.reason);
608         free(hi.stratum_url);
609         databuf_free(&all_data);
610         curl_slist_free_all(headers);
611         curl_easy_reset(curl);
612         return NULL;
613 }
614
615 /* used to load a remote config */
616 json_t* json_load_url(char* cfg_url, json_error_t *err)
617 {
618         char err_str[CURL_ERROR_SIZE] = { 0 };
619         struct data_buffer all_data = { 0 };
620         int rc = 0; json_t *cfg = NULL;
621         CURL *curl = curl_easy_init();
622         if (unlikely(!curl)) {
623                 applog(LOG_ERR, "Remote config init failed!");
624                 return NULL;
625         }
626         curl_easy_setopt(curl, CURLOPT_URL, cfg_url);
627         curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1);
628         curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 15);
629         curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, err_str);
630         curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
631         curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
632         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, all_data_cb);
633         curl_easy_setopt(curl, CURLOPT_WRITEDATA, &all_data);
634         if (opt_proxy) {
635                 curl_easy_setopt(curl, CURLOPT_PROXY, opt_proxy);
636                 curl_easy_setopt(curl, CURLOPT_PROXYTYPE, opt_proxy_type);
637         } else if (getenv("http_proxy")) {
638                 if (getenv("all_proxy"))
639                         curl_easy_setopt(curl, CURLOPT_PROXY, getenv("all_proxy"));
640                 else if (getenv("ALL_PROXY"))
641                         curl_easy_setopt(curl, CURLOPT_PROXY, getenv("ALL_PROXY"));
642                 else
643                         curl_easy_setopt(curl, CURLOPT_PROXY, "");
644         }
645         rc = curl_easy_perform(curl);
646         if (rc) {
647                 applog(LOG_ERR, "Remote config read failed: %s", err_str);
648                 goto err_out;
649         }
650         if (!all_data.buf || !all_data.len) {
651                 applog(LOG_ERR, "Empty data received for config");
652                 goto err_out;
653         }
654
655         cfg = JSON_LOADS((char*)all_data.buf, err);
656 err_out:
657         curl_easy_cleanup(curl);
658         return cfg;
659 }
660
661 void bin2hex(char *s, const unsigned char *p, size_t len)
662 {
663         for (size_t i = 0; i < len; i++)
664                 sprintf(s + (i * 2), "%02x", (unsigned int) p[i]);
665 }
666
667 char *abin2hex(const unsigned char *p, size_t len)
668 {
669         char *s = (char*) malloc((len * 2) + 1);
670         if (!s)
671                 return NULL;
672         bin2hex(s, p, len);
673         return s;
674 }
675
676 bool hex2bin(unsigned char *p, const char *hexstr, size_t len)
677 {
678         char hex_byte[3];
679         char *ep;
680
681         hex_byte[2] = '\0';
682
683         while (*hexstr && len) {
684                 if (!hexstr[1]) {
685                         applog(LOG_ERR, "hex2bin str truncated");
686                         return false;
687                 }
688                 hex_byte[0] = hexstr[0];
689                 hex_byte[1] = hexstr[1];
690                 *p = (unsigned char) strtol(hex_byte, &ep, 16);
691                 if (*ep) {
692                         applog(LOG_ERR, "hex2bin failed on '%s'", hex_byte);
693                         return false;
694                 }
695                 p++;
696                 hexstr += 2;
697                 len--;
698         }
699
700         return(!len) ? true : false;
701 /*      return (len == 0 && *hexstr == 0) ? true : false; */
702 }
703
704 int varint_encode(unsigned char *p, uint64_t n)
705 {
706         int i;
707         if (n < 0xfd) {
708                 p[0] = (uchar) n;
709                 return 1;
710         }
711         if (n <= 0xffff) {
712                 p[0] = 0xfd;
713                 p[1] = n & 0xff;
714                 p[2] = (uchar) (n >> 8);
715                 return 3;
716         }
717         if (n <= 0xffffffff) {
718                 p[0] = 0xfe;
719                 for (i = 1; i < 5; i++) {
720                         p[i] = n & 0xff;
721                         n >>= 8;
722                 }
723                 return 5;
724         }
725         p[0] = 0xff;
726         for (i = 1; i < 9; i++) {
727                 p[i] = n & 0xff;
728                 n >>= 8;
729         }
730         return 9;
731 }
732
733 static const char b58digits[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
734
735 static bool b58dec(unsigned char *bin, size_t binsz, const char *b58)
736 {
737         size_t i, j;
738         uint64_t t;
739         uint32_t c;
740         uint32_t *outi;
741         size_t outisz = (binsz + 3) / 4;
742         int rem = binsz % 4;
743         uint32_t remmask = 0xffffffff << (8 * rem);
744         size_t b58sz = strlen(b58);
745         bool rc = false;
746
747         outi = (uint32_t *) calloc(outisz, sizeof(*outi));
748
749         for (i = 0; i < b58sz; ++i) {
750                 for (c = 0; b58digits[c] != b58[i]; c++)
751                         if (!b58digits[c])
752                                 goto out;
753                 for (j = outisz; j--; ) {
754                         t = (uint64_t)outi[j] * 58 + c;
755                         c = t >> 32;
756                         outi[j] = t & 0xffffffff;
757                 }
758                 if (c || outi[0] & remmask)
759                         goto out;
760         }
761
762         j = 0;
763         switch (rem) {
764                 case 3:
765                         *(bin++) = (outi[0] >> 16) & 0xff;
766                 case 2:
767                         *(bin++) = (outi[0] >> 8) & 0xff;
768                 case 1:
769                         *(bin++) = outi[0] & 0xff;
770                         ++j;
771                 default:
772                         break;
773         }
774         for (; j < outisz; ++j) {
775                 be32enc((uint32_t *)bin, outi[j]);
776                 bin += sizeof(uint32_t);
777         }
778
779         rc = true;
780 out:
781         free(outi);
782         return rc;
783 }
784
785 static int b58check(unsigned char *bin, size_t binsz, const char *b58)
786 {
787         unsigned char buf[32];
788         int i;
789
790         sha256d(buf, bin, (int) (binsz - 4));
791         if (memcmp(&bin[binsz - 4], buf, 4))
792                 return -1;
793
794         /* Check number of zeros is correct AFTER verifying checksum
795          * (to avoid possibility of accessing the string beyond the end) */
796         for (i = 0; bin[i] == '\0' && b58[i] == '1'; ++i);
797         if (bin[i] == '\0' || b58[i] == '1')
798                 return -3;
799
800         return bin[0];
801 }
802
803 bool jobj_binary(const json_t *obj, const char *key, void *buf, size_t buflen)
804 {
805         const char *hexstr;
806         json_t *tmp;
807
808         tmp = json_object_get(obj, key);
809         if (unlikely(!tmp)) {
810                 applog(LOG_ERR, "JSON key '%s' not found", key);
811                 return false;
812         }
813         hexstr = json_string_value(tmp);
814         if (unlikely(!hexstr)) {
815                 applog(LOG_ERR, "JSON key '%s' is not a string", key);
816                 return false;
817         }
818         if (!hex2bin((uchar*) buf, hexstr, buflen))
819                 return false;
820
821         return true;
822 }
823
824 size_t address_to_script(unsigned char *out, size_t outsz, const char *addr)
825 {
826         unsigned char addrbin[25];
827         int addrver;
828         size_t rv;
829
830         if (!b58dec(addrbin, sizeof(addrbin), addr))
831                 return 0;
832         addrver = b58check(addrbin, sizeof(addrbin), addr);
833         if (addrver < 0)
834                 return 0;
835         switch (addrver) {
836                 case 5:    /* Bitcoin script hash */
837                 case 196:  /* Testnet script hash */
838                         if (outsz < (rv = 23))
839                                 return rv;
840                         out[ 0] = 0xa9;  /* OP_HASH160 */
841                         out[ 1] = 0x14;  /* push 20 bytes */
842                         memcpy(&out[2], &addrbin[1], 20);
843                         out[22] = 0x87;  /* OP_EQUAL */
844                         return rv;
845                 default:
846                         if (outsz < (rv = 25))
847                                 return rv;
848                         out[ 0] = 0x76;  /* OP_DUP */
849                         out[ 1] = 0xa9;  /* OP_HASH160 */
850                         out[ 2] = 0x14;  /* push 20 bytes */
851                         memcpy(&out[3], &addrbin[1], 20);
852                         out[23] = 0x88;  /* OP_EQUALVERIFY */
853                         out[24] = 0xac;  /* OP_CHECKSIG */
854                         return rv;
855         }
856 }
857
858 /* Subtract the `struct timeval' values X and Y,
859    storing the result in RESULT.
860    Return 1 if the difference is negative, otherwise 0.  */
861 int timeval_subtract(struct timeval *result, struct timeval *x,
862         struct timeval *y)
863 {
864         /* Perform the carry for the later subtraction by updating Y. */
865         if (x->tv_usec < y->tv_usec) {
866                 int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
867                 y->tv_usec -= 1000000 * nsec;
868                 y->tv_sec += nsec;
869         }
870         if (x->tv_usec - y->tv_usec > 1000000) {
871                 int nsec = (x->tv_usec - y->tv_usec) / 1000000;
872                 y->tv_usec += 1000000 * nsec;
873                 y->tv_sec -= nsec;
874         }
875
876         /* Compute the time remaining to wait.
877          * `tv_usec' is certainly positive. */
878         result->tv_sec = x->tv_sec - y->tv_sec;
879         result->tv_usec = x->tv_usec - y->tv_usec;
880
881         /* Return 1 if result is negative. */
882         return x->tv_sec < y->tv_sec;
883 }
884
885 bool fulltest(const uint32_t *hash, const uint32_t *target)
886 {
887         int i;
888         bool rc = true;
889         
890         for (i = 7; i >= 0; i--) {
891                 if (hash[i] > target[i]) {
892                         rc = false;
893                         break;
894                 }
895                 if (hash[i] < target[i]) {
896                         rc = true;
897                         break;
898                 }
899         }
900
901         if (opt_debug) {
902                 uint32_t hash_be[8], target_be[8];
903                 char hash_str[65], target_str[65];
904                 
905                 for (i = 0; i < 8; i++) {
906                         be32enc(hash_be + i, hash[7 - i]);
907                         be32enc(target_be + i, target[7 - i]);
908                 }
909                 bin2hex(hash_str, (unsigned char *)hash_be, 32);
910                 bin2hex(target_str, (unsigned char *)target_be, 32);
911
912                 applog(LOG_DEBUG, "DEBUG: %s\nHash:   %s\nTarget: %s",
913                         rc ? "hash <= target"
914                            : "hash > target (false positive)",
915                         hash_str,
916                         target_str);
917         }
918
919         return rc;
920 }
921
922 void diff_to_target(uint32_t *target, double diff)
923 {
924         uint64_t m;
925         int k;
926         
927         for (k = 6; k > 0 && diff > 1.0; k--)
928                 diff /= 4294967296.0;
929         m = (uint64_t)(4294901760.0 / diff);
930         if (m == 0 && k == 6)
931                 memset(target, 0xff, 32);
932         else {
933                 memset(target, 0, 32);
934                 target[k] = (uint32_t)m;
935                 target[k + 1] = (uint32_t)(m >> 32);
936         }
937 }
938
939 // Only used by stratum pools
940 void work_set_target(struct work* work, double diff)
941 {
942         diff_to_target(work->target, diff);
943         work->targetdiff = diff;
944 }
945
946 // Only used by longpoll pools
947 double target_to_diff(uint32_t* target)
948 {
949         uchar* tgt = (uchar*) target;
950         uint64_t m =
951                 (uint64_t)tgt[29] << 56 |
952                 (uint64_t)tgt[28] << 48 |
953                 (uint64_t)tgt[27] << 40 |
954                 (uint64_t)tgt[26] << 32 |
955                 (uint64_t)tgt[25] << 24 |
956                 (uint64_t)tgt[24] << 16 |
957                 (uint64_t)tgt[23] << 8  |
958                 (uint64_t)tgt[22] << 0;
959
960         if (!m)
961                 return 0.;
962         else
963                 return (double)0x0000ffff00000000/m;
964 }
965
966 #ifdef WIN32
967 #define socket_blocks() (WSAGetLastError() == WSAEWOULDBLOCK)
968 #else
969 #define socket_blocks() (errno == EAGAIN || errno == EWOULDBLOCK)
970 #endif
971
972 static bool send_line(curl_socket_t sock, char *s)
973 {
974         size_t sent = 0;
975         int len;
976
977         len = (int) strlen(s);
978         s[len++] = '\n';
979
980         while (len > 0) {
981                 struct timeval timeout = {0, 0};
982                 int n;
983                 fd_set wd;
984
985                 FD_ZERO(&wd);
986                 FD_SET(sock, &wd);
987                 if (select((int) (sock + 1), NULL, &wd, NULL, &timeout) < 1)
988                         return false;
989                 n = send(sock, s + sent, len, 0);
990                 if (n < 0) {
991                         if (!socket_blocks())
992                                 return false;
993                         n = 0;
994                 }
995                 sent += n;
996                 len -= n;
997         }
998
999         return true;
1000 }
1001
1002 bool stratum_send_line(struct stratum_ctx *sctx, char *s)
1003 {
1004         bool ret = false;
1005
1006         if (opt_protocol)
1007                 applog(LOG_DEBUG, "> %s", s);
1008
1009         pthread_mutex_lock(&sctx->sock_lock);
1010         ret = send_line(sctx->sock, s);
1011         pthread_mutex_unlock(&sctx->sock_lock);
1012
1013         return ret;
1014 }
1015
1016 static bool socket_full(curl_socket_t sock, int timeout)
1017 {
1018         struct timeval tv;
1019         fd_set rd;
1020
1021         FD_ZERO(&rd);
1022         FD_SET(sock, &rd);
1023         tv.tv_sec = timeout;
1024         tv.tv_usec = 0;
1025         if (select((int)(sock + 1), &rd, NULL, NULL, &tv) > 0)
1026                 return true;
1027         return false;
1028 }
1029
1030 bool stratum_socket_full(struct stratum_ctx *sctx, int timeout)
1031 {
1032         return strlen(sctx->sockbuf) || socket_full(sctx->sock, timeout);
1033 }
1034
1035 #define RBUFSIZE 2048
1036 #define RECVSIZE (RBUFSIZE - 4)
1037
1038 static void stratum_buffer_append(struct stratum_ctx *sctx, const char *s)
1039 {
1040         size_t old, n;
1041
1042         old = strlen(sctx->sockbuf);
1043         n = old + strlen(s) + 1;
1044         if (n >= sctx->sockbuf_size) {
1045                 sctx->sockbuf_size = n + (RBUFSIZE - (n % RBUFSIZE));
1046                 sctx->sockbuf = (char*) realloc(sctx->sockbuf, sctx->sockbuf_size);
1047         }
1048         strcpy(sctx->sockbuf + old, s);
1049 }
1050
1051 char *stratum_recv_line(struct stratum_ctx *sctx)
1052 {
1053         ssize_t len, buflen;
1054         char *tok, *sret = NULL;
1055
1056         if (!strstr(sctx->sockbuf, "\n")) {
1057                 bool ret = true;
1058                 time_t rstart;
1059
1060                 time(&rstart);
1061                 if (!socket_full(sctx->sock, 60)) {
1062                         applog(LOG_ERR, "stratum_recv_line timed out");
1063                         goto out;
1064                 }
1065                 do {
1066                         char s[RBUFSIZE];
1067                         ssize_t n;
1068
1069                         memset(s, 0, RBUFSIZE);
1070                         n = recv(sctx->sock, s, RECVSIZE, 0);
1071                         if (!n) {
1072                                 ret = false;
1073                                 break;
1074                         }
1075                         if (n < 0) {
1076                                 if (!socket_blocks() || !socket_full(sctx->sock, 1)) {
1077                                         ret = false;
1078                                         break;
1079                                 }
1080                         } else
1081                                 stratum_buffer_append(sctx, s);
1082                 } while (time(NULL) - rstart < 60 && !strstr(sctx->sockbuf, "\n"));
1083
1084                 if (!ret) {
1085                         applog(LOG_ERR, "stratum_recv_line failed");
1086                         goto out;
1087                 }
1088         }
1089
1090         buflen = (ssize_t) strlen(sctx->sockbuf);
1091         tok = strtok(sctx->sockbuf, "\n");
1092         if (!tok) {
1093                 applog(LOG_ERR, "stratum_recv_line failed to parse a newline-terminated string");
1094                 goto out;
1095         }
1096         sret = strdup(tok);
1097         len = (ssize_t) strlen(sret);
1098
1099         if (buflen > len + 1)
1100                 memmove(sctx->sockbuf, sctx->sockbuf + len + 1, buflen - len + 1);
1101         else
1102                 sctx->sockbuf[0] = '\0';
1103
1104 out:
1105         if (sret && opt_protocol)
1106                 applog(LOG_DEBUG, "< %s", sret);
1107         return sret;
1108 }
1109
1110 #if LIBCURL_VERSION_NUM >= 0x071101
1111 static curl_socket_t opensocket_grab_cb(void *clientp, curlsocktype purpose,
1112         struct curl_sockaddr *addr)
1113 {
1114         curl_socket_t *sock = (curl_socket_t*) clientp;
1115         *sock = socket(addr->family, addr->socktype, addr->protocol);
1116         return *sock;
1117 }
1118 #endif
1119
1120 bool stratum_connect(struct stratum_ctx *sctx, const char *url)
1121 {
1122         CURL *curl;
1123         int rc;
1124
1125         pthread_mutex_lock(&sctx->sock_lock);
1126         if (sctx->curl)
1127                 curl_easy_cleanup(sctx->curl);
1128         sctx->curl = curl_easy_init();
1129         if (!sctx->curl) {
1130                 applog(LOG_ERR, "CURL initialization failed");
1131                 pthread_mutex_unlock(&sctx->sock_lock);
1132                 return false;
1133         }
1134         curl = sctx->curl;
1135         if (!sctx->sockbuf) {
1136                 sctx->sockbuf = (char*) calloc(RBUFSIZE, 1);
1137                 sctx->sockbuf_size = RBUFSIZE;
1138         }
1139         sctx->sockbuf[0] = '\0';
1140         pthread_mutex_unlock(&sctx->sock_lock);
1141
1142         if (url != sctx->url) {
1143                 free(sctx->url);
1144                 sctx->url = strdup(url);
1145         }
1146         free(sctx->curl_url);
1147         sctx->curl_url = (char*) malloc(strlen(url));
1148         sprintf(sctx->curl_url, "http%s", strstr(url, "://"));
1149
1150         if (opt_protocol)
1151                 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
1152         curl_easy_setopt(curl, CURLOPT_URL, sctx->curl_url);
1153         curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1);
1154         curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30);
1155         curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, sctx->curl_err_str);
1156         curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
1157         curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
1158         if (opt_proxy) {
1159                 curl_easy_setopt(curl, CURLOPT_PROXY, opt_proxy);
1160                 curl_easy_setopt(curl, CURLOPT_PROXYTYPE, opt_proxy_type);
1161         }
1162         curl_easy_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1);
1163 #if LIBCURL_VERSION_NUM >= 0x070f06
1164         curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_keepalive_cb);
1165 #endif
1166 #if LIBCURL_VERSION_NUM >= 0x071101
1167         curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket_grab_cb);
1168         curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sctx->sock);
1169 #endif
1170         curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1);
1171
1172         rc = curl_easy_perform(curl);
1173         if (rc) {
1174                 applog(LOG_ERR, "Stratum connection failed: %s", sctx->curl_err_str);
1175                 curl_easy_cleanup(curl);
1176                 sctx->curl = NULL;
1177                 return false;
1178         }
1179
1180 #if LIBCURL_VERSION_NUM < 0x071101
1181         /* CURLINFO_LASTSOCKET is broken on Win64; only use it as a last resort */
1182         curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, (long *)&sctx->sock);
1183 #endif
1184
1185         return true;
1186 }
1187
1188 void stratum_disconnect(struct stratum_ctx *sctx)
1189 {
1190         pthread_mutex_lock(&sctx->sock_lock);
1191         if (sctx->curl) {
1192                 curl_easy_cleanup(sctx->curl);
1193                 sctx->curl = NULL;
1194                 sctx->sockbuf[0] = '\0';
1195         }
1196         pthread_mutex_unlock(&sctx->sock_lock);
1197 }
1198
1199 static const char *get_stratum_session_id(json_t *val)
1200 {
1201         json_t *arr_val;
1202         int i, n;
1203
1204         arr_val = json_array_get(val, 0);
1205         if (!arr_val || !json_is_array(arr_val))
1206                 return NULL;
1207         n = (int) json_array_size(arr_val);
1208         for (i = 0; i < n; i++) {
1209                 const char *notify;
1210                 json_t *arr = json_array_get(arr_val, i);
1211
1212                 if (!arr || !json_is_array(arr))
1213                         break;
1214                 notify = json_string_value(json_array_get(arr, 0));
1215                 if (!notify)
1216                         continue;
1217                 if (!strcasecmp(notify, "mining.notify"))
1218                         return json_string_value(json_array_get(arr, 1));
1219         }
1220         return NULL;
1221 }
1222
1223 static bool stratum_parse_extranonce(struct stratum_ctx *sctx, json_t *params, int pndx)
1224 {
1225         const char* xnonce1;
1226         int xn2_size;
1227
1228         xnonce1 = json_string_value(json_array_get(params, pndx));
1229         if (!xnonce1) {
1230                 applog(LOG_ERR, "Failed to get extranonce1");
1231                 goto out;
1232         }
1233         xn2_size = (int) json_integer_value(json_array_get(params, pndx+1));
1234         if (!xn2_size) {
1235                 applog(LOG_ERR, "Failed to get extranonce2_size");
1236                 goto out;
1237         }
1238         if (xn2_size < 2 || xn2_size > 16) {
1239                 applog(LOG_INFO, "Failed to get valid n2size in parse_extranonce");
1240                 goto out;
1241         }
1242
1243         pthread_mutex_lock(&sctx->work_lock);
1244         if (sctx->xnonce1)
1245                 free(sctx->xnonce1);
1246         sctx->xnonce1_size = strlen(xnonce1) / 2;
1247         sctx->xnonce1 = (uchar*) calloc(1, sctx->xnonce1_size);
1248         if (unlikely(!sctx->xnonce1)) {
1249                 applog(LOG_ERR, "Failed to alloc xnonce1");
1250                 pthread_mutex_unlock(&sctx->work_lock);
1251                 goto out;
1252         }
1253         hex2bin(sctx->xnonce1, xnonce1, sctx->xnonce1_size);
1254         sctx->xnonce2_size = xn2_size;
1255         pthread_mutex_unlock(&sctx->work_lock);
1256
1257         if (pndx == 0 && opt_debug) /* pool dynamic change */
1258                 applog(LOG_DEBUG, "Stratum set nonce %s with extranonce2 size=%d",
1259                         xnonce1, xn2_size);
1260
1261         return true;
1262 out:
1263         return false;
1264 }
1265
1266 bool stratum_subscribe(struct stratum_ctx *sctx)
1267 {
1268         char *s, *sret = NULL;
1269         const char *sid;
1270         json_t *val = NULL, *res_val, *err_val;
1271         json_error_t err;
1272         bool ret = false, retry = false;
1273
1274         if (jsonrpc_2)
1275                 return true;
1276
1277 start:
1278         s = (char*) malloc(128 + (sctx->session_id ? strlen(sctx->session_id) : 0));
1279         if (retry)
1280                 sprintf(s, "{\"id\": 1, \"method\": \"mining.subscribe\", \"params\": []}");
1281         else if (sctx->session_id)
1282                 sprintf(s, "{\"id\": 1, \"method\": \"mining.subscribe\", \"params\": [\"" USER_AGENT "\", \"%s\"]}", sctx->session_id);
1283         else
1284                 sprintf(s, "{\"id\": 1, \"method\": \"mining.subscribe\", \"params\": [\"" USER_AGENT "\"]}");
1285
1286         if (!stratum_send_line(sctx, s)) {
1287                 applog(LOG_ERR, "stratum_subscribe send failed");
1288                 goto out;
1289         }
1290
1291         if (!socket_full(sctx->sock, 30)) {
1292                 applog(LOG_ERR, "stratum_subscribe timed out");
1293                 goto out;
1294         }
1295
1296         sret = stratum_recv_line(sctx);
1297         if (!sret)
1298                 goto out;
1299
1300         val = JSON_LOADS(sret, &err);
1301         free(sret);
1302         if (!val) {
1303                 applog(LOG_ERR, "JSON decode failed(%d): %s", err.line, err.text);
1304                 goto out;
1305         }
1306
1307         res_val = json_object_get(val, "result");
1308         err_val = json_object_get(val, "error");
1309
1310         if (!res_val || json_is_null(res_val) ||
1311             (err_val && !json_is_null(err_val))) {
1312                 if (opt_debug || retry) {
1313                         free(s);
1314                         if (err_val)
1315                                 s = json_dumps(err_val, JSON_INDENT(3));
1316                         else
1317                                 s = strdup("(unknown reason)");
1318                         applog(LOG_ERR, "JSON-RPC call failed: %s", s);
1319                 }
1320                 goto out;
1321         }
1322
1323         sid = get_stratum_session_id(res_val);
1324         if (opt_debug && sid)
1325                 applog(LOG_DEBUG, "Stratum session id: %s", sid);
1326
1327         pthread_mutex_lock(&sctx->work_lock);
1328         if (sctx->session_id)
1329                 free(sctx->session_id);
1330         sctx->session_id = sid ? strdup(sid) : NULL;
1331         sctx->next_diff = 1.0;
1332         pthread_mutex_unlock(&sctx->work_lock);
1333
1334         // sid is param 1, extranonce params are 2 and 3
1335         if (!stratum_parse_extranonce(sctx, res_val, 1)) {
1336                 goto out;
1337         }
1338
1339         ret = true;
1340
1341 out:
1342         free(s);
1343         if (val)
1344                 json_decref(val);
1345
1346         if (!ret) {
1347                 if (sret && !retry) {
1348                         retry = true;
1349                         goto start;
1350                 }
1351         }
1352
1353         return ret;
1354 }
1355
1356 extern bool opt_extranonce;
1357
1358 bool stratum_authorize(struct stratum_ctx *sctx, const char *user, const char *pass)
1359 {
1360         json_t *val = NULL, *res_val, *err_val;
1361         char *s, *sret;
1362         json_error_t err;
1363         bool ret = false;
1364
1365         if (jsonrpc_2) {
1366                 s = (char*) malloc(300 + strlen(user) + strlen(pass));
1367                 sprintf(s, "{\"method\": \"login\", \"params\": {"
1368                         "\"login\": \"%s\", \"pass\": \"%s\", \"agent\": \"%s\"}, \"id\": 1}",
1369                         user, pass, USER_AGENT);
1370         } else {
1371                 s = (char*) malloc(80 + strlen(user) + strlen(pass));
1372                 sprintf(s, "{\"id\": 2, \"method\": \"mining.authorize\", \"params\": [\"%s\", \"%s\"]}",
1373                         user, pass);
1374         }
1375
1376         if (!stratum_send_line(sctx, s))
1377                 goto out;
1378
1379         while (1) {
1380                 sret = stratum_recv_line(sctx);
1381                 if (!sret)
1382                         goto out;
1383                 if (!stratum_handle_method(sctx, sret))
1384                         break;
1385                 free(sret);
1386         }
1387
1388         val = JSON_LOADS(sret, &err);
1389         free(sret);
1390         if (!val) {
1391                 applog(LOG_ERR, "JSON decode failed(%d): %s", err.line, err.text);
1392                 goto out;
1393         }
1394
1395         res_val = json_object_get(val, "result");
1396         err_val = json_object_get(val, "error");
1397
1398         if (!res_val || json_is_false(res_val) ||
1399             (err_val && !json_is_null(err_val)))  {
1400                 applog(LOG_ERR, "Stratum authentication failed");
1401                 goto out;
1402         }
1403
1404         if (jsonrpc_2) {
1405                 rpc2_login_decode(val);
1406                 json_t *job_val = json_object_get(res_val, "job");
1407                 pthread_mutex_lock(&sctx->work_lock);
1408                 if(job_val) rpc2_job_decode(job_val, &sctx->work);
1409                 pthread_mutex_unlock(&sctx->work_lock);
1410         }
1411
1412         ret = true;
1413
1414         if (!opt_extranonce)
1415                 goto out;
1416
1417         // subscribe to extranonce (optional)
1418         sprintf(s, "{\"id\": 3, \"method\": \"mining.extranonce.subscribe\", \"params\": []}");
1419
1420         if (!stratum_send_line(sctx, s))
1421                 goto out;
1422
1423         if (!socket_full(sctx->sock, 3)) {
1424                 if (opt_debug)
1425                         applog(LOG_DEBUG, "stratum extranonce subscribe timed out");
1426                 goto out;
1427         }
1428
1429         sret = stratum_recv_line(sctx);
1430         if (sret) {
1431                 json_t *extra = JSON_LOADS(sret, &err);
1432                 if (!extra) {
1433                         applog(LOG_WARNING, "JSON decode failed(%d): %s", err.line, err.text);
1434                 } else {
1435                         if (json_integer_value(json_object_get(extra, "id")) != 3) {
1436                                 // we receive a standard method if extranonce is ignored
1437                                 if (!stratum_handle_method(sctx, sret))
1438                                         applog(LOG_WARNING, "Stratum answer id is not correct!");
1439                         }
1440                         json_decref(extra);
1441                 }
1442                 free(sret);
1443         }
1444
1445 out:
1446         free(s);
1447         if (val)
1448                 json_decref(val);
1449
1450         return ret;
1451 }
1452
1453 // -------------------- RPC 2.0 (XMR/AEON) -------------------------
1454
1455 extern pthread_mutex_t rpc2_login_lock;
1456 extern pthread_mutex_t rpc2_job_lock;
1457
1458 bool rpc2_login_decode(const json_t *val)
1459 {
1460         const char *id;
1461         const char *s;
1462
1463         json_t *res = json_object_get(val, "result");
1464         if(!res) {
1465                 applog(LOG_ERR, "JSON invalid result");
1466                 goto err_out;
1467         }
1468
1469         json_t *tmp;
1470         tmp = json_object_get(res, "id");
1471         if(!tmp) {
1472                 applog(LOG_ERR, "JSON inval id");
1473                 goto err_out;
1474         }
1475         id = json_string_value(tmp);
1476         if(!id) {
1477                 applog(LOG_ERR, "JSON id is not a string");
1478                 goto err_out;
1479         }
1480
1481         memcpy(&rpc2_id, id, 64);
1482
1483         if(opt_debug)
1484                 applog(LOG_DEBUG, "Auth id: %s", id);
1485
1486         tmp = json_object_get(res, "status");
1487         if(!tmp) {
1488                 applog(LOG_ERR, "JSON inval status");
1489                 goto err_out;
1490         }
1491         s = json_string_value(tmp);
1492         if(!s) {
1493                 applog(LOG_ERR, "JSON status is not a string");
1494                 goto err_out;
1495         }
1496         if(strcmp(s, "OK")) {
1497                 applog(LOG_ERR, "JSON returned status \"%s\"", s);
1498                 return false;
1499         }
1500
1501         return true;
1502
1503 err_out:
1504         applog(LOG_WARNING,"%s: fail", __func__);
1505         return false;
1506 }
1507
1508 json_t* json_rpc2_call_recur(CURL *curl, const char *url, const char *userpass,
1509         json_t *rpc_req, int *curl_err, int flags, int recur)
1510 {
1511         if(recur >= 5) {
1512                 if(opt_debug)
1513                         applog(LOG_DEBUG, "Failed to call rpc command after %i tries", recur);
1514                 return NULL;
1515         }
1516         if(!strcmp(rpc2_id, "")) {
1517                 if(opt_debug)
1518                         applog(LOG_DEBUG, "Tried to call rpc2 command before authentication");
1519                 return NULL;
1520         }
1521         json_t *params = json_object_get(rpc_req, "params");
1522         if (params) {
1523                 json_t *auth_id = json_object_get(params, "id");
1524                 if (auth_id) {
1525                         json_string_set(auth_id, rpc2_id);
1526                 }
1527         }
1528         json_t *res = json_rpc_call(curl, url, userpass, json_dumps(rpc_req, 0),
1529                         curl_err, flags | JSON_RPC_IGNOREERR);
1530         if(!res) goto end;
1531         json_t *error = json_object_get(res, "error");
1532         if(!error) goto end;
1533         json_t *message;
1534         if(json_is_string(error))
1535                 message = error;
1536         else
1537                 message = json_object_get(error, "message");
1538         if(!message || !json_is_string(message)) goto end;
1539         const char *mes = json_string_value(message);
1540         if(!strcmp(mes, "Unauthenticated")) {
1541                 pthread_mutex_lock(&rpc2_login_lock);
1542                 rpc2_login(curl);
1543                 sleep(1);
1544                 pthread_mutex_unlock(&rpc2_login_lock);
1545                 return json_rpc2_call_recur(curl, url, userpass, rpc_req,
1546                                 curl_err, flags, recur + 1);
1547         } else if(!strcmp(mes, "Low difficulty share") || !strcmp(mes, "Block expired") || !strcmp(mes, "Invalid job id") || !strcmp(mes, "Duplicate share")) {
1548                 json_t *result = json_object_get(res, "result");
1549                 if(!result) {
1550                         goto end;
1551                 }
1552                 json_object_set(result, "reject-reason", json_string(mes));
1553         } else {
1554                 applog(LOG_ERR, "json_rpc2.0 error: %s", mes);
1555                 return NULL;
1556         }
1557         end:
1558         return res;
1559 }
1560
1561 json_t *json_rpc2_call(CURL *curl, const char *url, const char *userpass, const char *rpc_req, int *curl_err, int flags)
1562 {
1563         json_t* req_json = JSON_LOADS(rpc_req, NULL);
1564         json_t* res = json_rpc2_call_recur(curl, url, userpass, req_json, curl_err, flags, 0);
1565         json_decref(req_json);
1566         return res;
1567 }
1568
1569 bool rpc2_job_decode(const json_t *job, struct work *work)
1570 {
1571         if (!jsonrpc_2) {
1572                 applog(LOG_ERR, "Tried to decode job without JSON-RPC 2.0");
1573                 return false;
1574         }
1575         json_t *tmp;
1576         tmp = json_object_get(job, "job_id");
1577         if (!tmp) {
1578                 applog(LOG_ERR, "JSON invalid job id");
1579                 goto err_out;
1580         }
1581         const char *job_id = json_string_value(tmp);
1582         tmp = json_object_get(job, "blob");
1583         if (!tmp) {
1584                 applog(LOG_ERR, "JSON invalid blob");
1585                 goto err_out;
1586         }
1587         const char *hexblob = json_string_value(tmp);
1588         size_t blobLen = strlen(hexblob);
1589         if (blobLen % 2 != 0 || ((blobLen / 2) < 40 && blobLen != 0) || (blobLen / 2) > 128) {
1590                 applog(LOG_ERR, "JSON invalid blob length");
1591                 goto err_out;
1592         }
1593         if (blobLen != 0) {
1594                 uint32_t target = 0;
1595                 pthread_mutex_lock(&rpc2_job_lock);
1596                 uchar *blob = (uchar*) malloc(blobLen / 2);
1597                 if (!hex2bin(blob, hexblob, blobLen / 2)) {
1598                         applog(LOG_ERR, "JSON invalid blob");
1599                         pthread_mutex_unlock(&rpc2_job_lock);
1600                         goto err_out;
1601                 }
1602                 rpc2_bloblen = blobLen / 2;
1603                 if (rpc2_blob) free(rpc2_blob);
1604                 rpc2_blob = (char*) malloc(rpc2_bloblen);
1605                 if (!rpc2_blob)  {
1606                         applog(LOG_ERR, "RPC2 OOM!");
1607                         goto err_out;
1608                 }
1609                 memcpy(rpc2_blob, blob, blobLen / 2);
1610                 free(blob);
1611
1612                 jobj_binary(job, "target", &target, 4);
1613                 if(rpc2_target != target) {
1614                         double hashrate = 0.0;
1615                         pthread_mutex_lock(&stats_lock);
1616                         for (int i = 0; i < opt_n_threads; i++)
1617                                 hashrate += thr_hashrates[i];
1618                         pthread_mutex_unlock(&stats_lock);
1619                         double difficulty = (((double) 0xffffffff) / target);
1620                         if (!opt_quiet) {
1621                                 // xmr pool diff can change a lot...
1622                                 applog(LOG_WARNING, "Stratum difficulty set to %g", difficulty);
1623                         }
1624                         stratum_diff = difficulty;
1625                         rpc2_target = target;
1626                 }
1627
1628                 if (rpc2_job_id) free(rpc2_job_id);
1629                 rpc2_job_id = strdup(job_id);
1630                 pthread_mutex_unlock(&rpc2_job_lock);
1631         }
1632         if(work) {
1633                 if (!rpc2_blob) {
1634                         applog(LOG_WARNING, "Work requested before it was received");
1635                         goto err_out;
1636                 }
1637                 memcpy(work->data, rpc2_blob, rpc2_bloblen);
1638                 memset(work->target, 0xff, sizeof(work->target));
1639                 work->target[7] = rpc2_target;
1640                 if (work->job_id) free(work->job_id);
1641                 work->job_id = strdup(rpc2_job_id);
1642         }
1643         return true;
1644
1645 err_out:
1646         applog(LOG_WARNING, "%s", __func__);
1647         return false;
1648 }
1649
1650 /**
1651  * Extract bloc height     L H... here len=3, height=0x1333e8
1652  * "...0000000000ffffffff2703e83313062f503253482f043d61105408"
1653  */
1654 static uint32_t getblocheight(struct stratum_ctx *sctx)
1655 {
1656         uint32_t height = 0;
1657         uint8_t hlen = 0, *p, *m;
1658
1659         // find 0xffff tag
1660         p = (uint8_t*) sctx->job.coinbase + 32;
1661         m = p + 128;
1662         while (*p != 0xff && p < m) p++;
1663         while (*p == 0xff && p < m) p++;
1664         if (*(p-1) == 0xff && *(p-2) == 0xff) {
1665                 p++; hlen = *p;
1666                 p++; height = le16dec(p);
1667                 p += 2;
1668                 switch (hlen) {
1669                         case 4:
1670                                 height += 0x10000UL * le16dec(p);
1671                                 break;
1672                         case 3:
1673                                 height += 0x10000UL * (*p);
1674                                 break;
1675                 }
1676         }
1677         return height;
1678 }
1679
1680 static bool stratum_notify(struct stratum_ctx *sctx, json_t *params)
1681 {
1682         const char *job_id, *prevhash, *coinb1, *coinb2, *version, *nbits, *ntime;
1683         size_t coinb1_size, coinb2_size;
1684         bool clean, ret = false;
1685         int merkle_count, i;
1686         json_t *merkle_arr;
1687         uchar **merkle;
1688
1689         job_id = json_string_value(json_array_get(params, 0));
1690         prevhash = json_string_value(json_array_get(params, 1));
1691         coinb1 = json_string_value(json_array_get(params, 2));
1692         coinb2 = json_string_value(json_array_get(params, 3));
1693         merkle_arr = json_array_get(params, 4);
1694         if (!merkle_arr || !json_is_array(merkle_arr))
1695                 goto out;
1696         merkle_count = (int) json_array_size(merkle_arr);
1697         version = json_string_value(json_array_get(params, 5));
1698         nbits = json_string_value(json_array_get(params, 6));
1699         ntime = json_string_value(json_array_get(params, 7));
1700         clean = json_is_true(json_array_get(params, 8));
1701
1702         if (!job_id || !prevhash || !coinb1 || !coinb2 || !version || !nbits || !ntime ||
1703             strlen(prevhash) != 64 || strlen(version) != 8 ||
1704             strlen(nbits) != 8 || strlen(ntime) != 8) {
1705                 applog(LOG_ERR, "Stratum notify: invalid parameters");
1706                 goto out;
1707         }
1708         merkle = (uchar**) malloc(merkle_count * sizeof(char *));
1709         for (i = 0; i < merkle_count; i++) {
1710                 const char *s = json_string_value(json_array_get(merkle_arr, i));
1711                 if (!s || strlen(s) != 64) {
1712                         while (i--)
1713                                 free(merkle[i]);
1714                         free(merkle);
1715                         applog(LOG_ERR, "Stratum notify: invalid Merkle branch");
1716                         goto out;
1717                 }
1718                 merkle[i] = (uchar*) malloc(32);
1719                 hex2bin(merkle[i], s, 32);
1720         }
1721
1722         pthread_mutex_lock(&sctx->work_lock);
1723
1724         coinb1_size = strlen(coinb1) / 2;
1725         coinb2_size = strlen(coinb2) / 2;
1726         sctx->job.coinbase_size = coinb1_size + sctx->xnonce1_size +
1727                                   sctx->xnonce2_size + coinb2_size;
1728         sctx->job.coinbase = (uchar*) realloc(sctx->job.coinbase, sctx->job.coinbase_size);
1729         sctx->job.xnonce2 = sctx->job.coinbase + coinb1_size + sctx->xnonce1_size;
1730         hex2bin(sctx->job.coinbase, coinb1, coinb1_size);
1731         memcpy(sctx->job.coinbase + coinb1_size, sctx->xnonce1, sctx->xnonce1_size);
1732         if (!sctx->job.job_id || strcmp(sctx->job.job_id, job_id))
1733                 memset(sctx->job.xnonce2, 0, sctx->xnonce2_size);
1734         hex2bin(sctx->job.xnonce2 + sctx->xnonce2_size, coinb2, coinb2_size);
1735
1736         free(sctx->job.job_id);
1737         sctx->job.job_id = strdup(job_id);
1738         hex2bin(sctx->job.prevhash, prevhash, 32);
1739
1740         sctx->bloc_height = getblocheight(sctx);
1741
1742         for (i = 0; i < sctx->job.merkle_count; i++)
1743                 free(sctx->job.merkle[i]);
1744         free(sctx->job.merkle);
1745         sctx->job.merkle = merkle;
1746         sctx->job.merkle_count = merkle_count;
1747
1748         hex2bin(sctx->job.version, version, 4);
1749         hex2bin(sctx->job.nbits, nbits, 4);
1750         hex2bin(sctx->job.ntime, ntime, 4);
1751         sctx->job.clean = clean;
1752
1753         sctx->job.diff = sctx->next_diff;
1754
1755         pthread_mutex_unlock(&sctx->work_lock);
1756
1757         ret = true;
1758
1759 out:
1760         return ret;
1761 }
1762
1763 static bool stratum_set_difficulty(struct stratum_ctx *sctx, json_t *params)
1764 {
1765         double diff;
1766
1767         diff = json_number_value(json_array_get(params, 0));
1768         if (diff == 0)
1769                 return false;
1770
1771         pthread_mutex_lock(&sctx->work_lock);
1772         sctx->next_diff = diff;
1773         pthread_mutex_unlock(&sctx->work_lock);
1774
1775         return true;
1776 }
1777
1778 static bool stratum_reconnect(struct stratum_ctx *sctx, json_t *params)
1779 {
1780         json_t *port_val;
1781         char *url;
1782         const char *host;
1783         int port;
1784
1785         host = json_string_value(json_array_get(params, 0));
1786         port_val = json_array_get(params, 1);
1787         if (json_is_string(port_val))
1788                 port = atoi(json_string_value(port_val));
1789         else
1790                 port = (int) json_integer_value(port_val);
1791         if (!host || !port)
1792                 return false;
1793
1794         url = (char*) malloc(32 + strlen(host));
1795         sprintf(url, "stratum+tcp://%s:%d", host, port);
1796
1797         if (!opt_redirect) {
1798                 applog(LOG_INFO, "Ignoring request to reconnect to %s", url);
1799                 free(url);
1800                 return true;
1801         }
1802
1803         applog(LOG_NOTICE, "Server requested reconnection to %s", url);
1804
1805         free(sctx->url);
1806         sctx->url = url;
1807         stratum_disconnect(sctx);
1808
1809         return true;
1810 }
1811
1812 static bool json_object_set_error(json_t *result, int code, const char *msg)
1813 {
1814         json_t *val = json_object();
1815         json_object_set_new(val, "code", json_integer(code));
1816         json_object_set_new(val, "message", json_string(msg));
1817         return json_object_set_new(result, "error", val) != -1;
1818 }
1819
1820 /* allow to report algo perf to the pool for algo stats */
1821 static bool stratum_benchdata(json_t *result, json_t *params, int thr_id)
1822 {
1823         char algo[64] = { 0 };
1824         char cpuname[80] = { 0 };
1825         char vendorid[32] = { 0 };
1826         char compiler[32] = { 0 };
1827         char arch[16] = { 0 };
1828         char os[8];
1829         char *p;
1830         double cpufreq = 0;
1831         json_t *val;
1832
1833         if (!opt_stratum_stats) return false;
1834
1835         get_currentalgo(algo, sizeof(algo));
1836
1837 #if defined(WIN32) && (defined(_M_X64) || defined(__x86_64__))
1838         strcpy(os, "win64");
1839 #else
1840         strcpy(os, is_windows() ? "win32" : "linux");
1841 #endif
1842
1843 #ifdef _MSC_VER
1844         sprintf(compiler, "VC++ %d\n", _MSC_VER / 100);
1845 #elif defined(__clang__)
1846         sprintf(compiler, "clang %s\n", __clang_version__);
1847 #elif defined(__GNUC__)
1848         sprintf(compiler, "GCC %d.%d.%d\n", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
1849 #endif
1850
1851 #ifdef __AVX2__
1852         strcat(compiler, " AVX2");
1853 #elif defined(__AVX__)
1854         strcat(compiler, " AVX");
1855 #elif defined(__FMA4__)
1856         strcat(compiler, " FMA4");
1857 #elif defined(__FMA3__)
1858         strcat(compiler, " FMA3");
1859 #elif defined(__SSE4_2__)
1860         strcat(compiler, " SSE4.2");
1861 #elif defined(__SSE4_1__)
1862         strcat(compiler, " SSE4");
1863 #elif defined(__SSE3__)
1864         strcat(compiler, " SSE3");
1865 #elif defined(__SSE2__)
1866         strcat(compiler, " SSE2");
1867 #elif defined(__SSE__)
1868         strcat(compiler, " SSE");
1869 #endif
1870
1871         cpu_bestfeature(arch, 16);
1872         if (has_aes_ni()) strcat(arch, " NI");
1873
1874         cpu_getmodelid(vendorid, 32);
1875         cpu_getname(cpuname, 80);
1876         p = strstr(cpuname, " @ ");
1877         if (p) {
1878                 // linux only
1879                 char freq[32] = { 0 };
1880                 *p = '\0'; p += 3;
1881                 snprintf(freq, 32, "%s", p);
1882                 cpufreq = atof(freq);
1883                 p = strstr(freq, "GHz"); if (p) cpufreq *= 1000;
1884                 applog(LOG_NOTICE, "sharing CPU stats with freq %s", freq);
1885         }
1886
1887         compiler[31] = '\0';
1888
1889         val = json_object();
1890         json_object_set_new(val, "algo", json_string(algo));
1891         json_object_set_new(val, "type", json_string("cpu"));
1892         json_object_set_new(val, "device", json_string(cpuname));
1893         json_object_set_new(val, "vendorid", json_string(vendorid));
1894         json_object_set_new(val, "arch", json_string(arch));
1895         json_object_set_new(val, "freq", json_integer((uint64_t)cpufreq));
1896         json_object_set_new(val, "memf", json_integer(0));
1897         json_object_set_new(val, "power", json_integer(0));
1898         json_object_set_new(val, "khashes", json_real((double)global_hashrate / 1000.0));
1899         json_object_set_new(val, "intensity", json_real(opt_priority));
1900         json_object_set_new(val, "throughput", json_integer(opt_n_threads));
1901         json_object_set_new(val, "client", json_string(PACKAGE_NAME "/" PACKAGE_VERSION));
1902         json_object_set_new(val, "os", json_string(os));
1903         json_object_set_new(val, "driver", json_string(compiler));
1904
1905         json_object_set_new(result, "result", val);
1906
1907         return true;
1908 }
1909
1910 static bool stratum_get_stats(struct stratum_ctx *sctx, json_t *id, json_t *params)
1911 {
1912         char *s;
1913         json_t *val;
1914         bool ret;
1915
1916         if (!id || json_is_null(id))
1917                 return false;
1918
1919         val = json_object();
1920         json_object_set(val, "id", id);
1921
1922         ret = stratum_benchdata(val, params, 0);
1923
1924         if (!ret) {
1925                 json_object_set_error(val, 1, "disabled"); //EPERM
1926         } else {
1927                 json_object_set_new(val, "error", json_null());
1928         }
1929
1930         s = json_dumps(val, 0);
1931         ret = stratum_send_line(sctx, s);
1932         json_decref(val);
1933         free(s);
1934
1935         return ret;
1936 }
1937
1938 static bool stratum_unknown_method(struct stratum_ctx *sctx, json_t *id)
1939 {
1940         char *s;
1941         json_t *val;
1942         bool ret = false;
1943
1944         if (!id || json_is_null(id))
1945                 return ret;
1946
1947         val = json_object();
1948         json_object_set(val, "id", id);
1949         json_object_set_new(val, "result", json_false());
1950         json_object_set_error(val, 38, "unknown method"); // ENOSYS
1951
1952         s = json_dumps(val, 0);
1953         ret = stratum_send_line(sctx, s);
1954         json_decref(val);
1955         free(s);
1956
1957         return ret;
1958 }
1959
1960 static bool stratum_pong(struct stratum_ctx *sctx, json_t *id)
1961 {
1962         char buf[64];
1963         bool ret = false;
1964
1965         if (!id || json_is_null(id))
1966                 return ret;
1967
1968         sprintf(buf, "{\"id\":%d,\"result\":\"pong\",\"error\":null}",
1969                 (int) json_integer_value(id));
1970         ret = stratum_send_line(sctx, buf);
1971
1972         return ret;
1973 }
1974
1975 static bool stratum_get_algo(struct stratum_ctx *sctx, json_t *id, json_t *params)
1976 {
1977         char algo[64] = { 0 };
1978         char *s;
1979         json_t *val;
1980         bool ret = true;
1981
1982         if (!id || json_is_null(id))
1983                 return false;
1984
1985         get_currentalgo(algo, sizeof(algo));
1986
1987         val = json_object();
1988         json_object_set(val, "id", id);
1989         json_object_set_new(val, "error", json_null());
1990         json_object_set_new(val, "result", json_string(algo));
1991
1992         s = json_dumps(val, 0);
1993         ret = stratum_send_line(sctx, s);
1994         json_decref(val);
1995         free(s);
1996
1997         return ret;
1998 }
1999
2000 static bool stratum_get_version(struct stratum_ctx *sctx, json_t *id)
2001 {
2002         char *s;
2003         json_t *val;
2004         bool ret;
2005         
2006         if (!id || json_is_null(id))
2007                 return false;
2008
2009         val = json_object();
2010         json_object_set(val, "id", id);
2011         json_object_set_new(val, "error", json_null());
2012         json_object_set_new(val, "result", json_string(USER_AGENT));
2013         s = json_dumps(val, 0);
2014         ret = stratum_send_line(sctx, s);
2015         json_decref(val);
2016         free(s);
2017
2018         return ret;
2019 }
2020
2021 static bool stratum_show_message(struct stratum_ctx *sctx, json_t *id, json_t *params)
2022 {
2023         char *s;
2024         json_t *val;
2025         bool ret;
2026
2027         val = json_array_get(params, 0);
2028         if (val)
2029                 applog(LOG_NOTICE, "MESSAGE FROM SERVER: %s", json_string_value(val));
2030         
2031         if (!id || json_is_null(id))
2032                 return true;
2033
2034         val = json_object();
2035         json_object_set(val, "id", id);
2036         json_object_set_new(val, "error", json_null());
2037         json_object_set_new(val, "result", json_true());
2038         s = json_dumps(val, 0);
2039         ret = stratum_send_line(sctx, s);
2040         json_decref(val);
2041         free(s);
2042
2043         return ret;
2044 }
2045
2046 bool stratum_handle_method(struct stratum_ctx *sctx, const char *s)
2047 {
2048         json_t *val, *id, *params;
2049         json_error_t err;
2050         const char *method;
2051         bool ret = false;
2052
2053         val = JSON_LOADS(s, &err);
2054         if (!val) {
2055                 applog(LOG_ERR, "JSON decode failed(%d): %s", err.line, err.text);
2056                 goto out;
2057         }
2058
2059         method = json_string_value(json_object_get(val, "method"));
2060         if (!method)
2061                 goto out;
2062
2063         params = json_object_get(val, "params");
2064
2065         if (jsonrpc_2) {
2066                 if (!strcasecmp(method, "job")) {
2067                         ret = rpc2_stratum_job(sctx, params);
2068                 }
2069                 goto out;
2070         }
2071
2072         id = json_object_get(val, "id");
2073
2074         if (!strcasecmp(method, "mining.notify")) {
2075                 ret = stratum_notify(sctx, params);
2076                 goto out;
2077         }
2078         if (!strcasecmp(method, "mining.ping")) { // cgminer 4.7.1+
2079                 if (opt_debug) applog(LOG_DEBUG, "Pool ping");
2080                 ret = stratum_pong(sctx, id);
2081                 goto out;
2082         }
2083         if (!strcasecmp(method, "mining.set_difficulty")) {
2084                 ret = stratum_set_difficulty(sctx, params);
2085                 goto out;
2086         }
2087         if (!strcasecmp(method, "mining.set_extranonce")) {
2088                 ret = stratum_parse_extranonce(sctx, params, 0);
2089                 goto out;
2090         }
2091         if (!strcasecmp(method, "client.reconnect")) {
2092                 ret = stratum_reconnect(sctx, params);
2093                 goto out;
2094         }
2095         if (!strcasecmp(method, "client.get_algo")) {
2096                 // will prevent wrong algo parameters on a pool, will be used as test on rejects
2097                 if (!opt_quiet) applog(LOG_NOTICE, "Pool asked your algo parameter");
2098                 ret = stratum_get_algo(sctx, id, params);
2099                 goto out;
2100         }
2101         if (!strcasecmp(method, "client.get_stats")) {
2102                 // optional to fill device benchmarks
2103                 ret = stratum_get_stats(sctx, id, params);
2104                 goto out;
2105         }
2106         if (!strcasecmp(method, "client.get_version")) {
2107                 ret = stratum_get_version(sctx, id);
2108                 goto out;
2109         }
2110         if (!strcasecmp(method, "client.show_message")) {
2111                 ret = stratum_show_message(sctx, id, params);
2112                 goto out;
2113         }
2114
2115         if (!ret) {
2116                 // don't fail = disconnect stratum on unknown (and optional?) methods
2117                 if (opt_debug) applog(LOG_WARNING, "unknown stratum method %s!", method);
2118                 ret = stratum_unknown_method(sctx, id);
2119         }
2120
2121 out:
2122         if (val)
2123                 json_decref(val);
2124
2125         return ret;
2126 }
2127
2128 struct thread_q *tq_new(void)
2129 {
2130         struct thread_q *tq;
2131
2132         tq = (struct thread_q*) calloc(1, sizeof(*tq));
2133         if (!tq)
2134                 return NULL;
2135
2136         INIT_LIST_HEAD(&tq->q);
2137         pthread_mutex_init(&tq->mutex, NULL);
2138         pthread_cond_init(&tq->cond, NULL);
2139
2140         return tq;
2141 }
2142
2143 void tq_free(struct thread_q *tq)
2144 {
2145         struct tq_ent *ent, *iter;
2146
2147         if (!tq)
2148                 return;
2149
2150         list_for_each_entry_safe(ent, iter, &tq->q, q_node, struct tq_ent) {
2151                 list_del(&ent->q_node);
2152                 free(ent);
2153         }
2154
2155         pthread_cond_destroy(&tq->cond);
2156         pthread_mutex_destroy(&tq->mutex);
2157
2158         memset(tq, 0, sizeof(*tq));     /* poison */
2159         free(tq);
2160 }
2161
2162 static void tq_freezethaw(struct thread_q *tq, bool frozen)
2163 {
2164         pthread_mutex_lock(&tq->mutex);
2165
2166         tq->frozen = frozen;
2167
2168         pthread_cond_signal(&tq->cond);
2169         pthread_mutex_unlock(&tq->mutex);
2170 }
2171
2172 void tq_freeze(struct thread_q *tq)
2173 {
2174         tq_freezethaw(tq, true);
2175 }
2176
2177 void tq_thaw(struct thread_q *tq)
2178 {
2179         tq_freezethaw(tq, false);
2180 }
2181
2182 bool tq_push(struct thread_q *tq, void *data)
2183 {
2184         struct tq_ent *ent;
2185         bool rc = true;
2186
2187         ent = (struct tq_ent*) calloc(1, sizeof(*ent));
2188         if (!ent)
2189                 return false;
2190
2191         ent->data = data;
2192         INIT_LIST_HEAD(&ent->q_node);
2193
2194         pthread_mutex_lock(&tq->mutex);
2195
2196         if (!tq->frozen) {
2197                 list_add_tail(&ent->q_node, &tq->q);
2198         } else {
2199                 free(ent);
2200                 rc = false;
2201         }
2202
2203         pthread_cond_signal(&tq->cond);
2204         pthread_mutex_unlock(&tq->mutex);
2205
2206         return rc;
2207 }
2208
2209 void *tq_pop(struct thread_q *tq, const struct timespec *abstime)
2210 {
2211         struct tq_ent *ent;
2212         void *rval = NULL;
2213         int rc;
2214
2215         pthread_mutex_lock(&tq->mutex);
2216
2217         if (!list_empty(&tq->q))
2218                 goto pop;
2219
2220         if (abstime)
2221                 rc = pthread_cond_timedwait(&tq->cond, &tq->mutex, abstime);
2222         else
2223                 rc = pthread_cond_wait(&tq->cond, &tq->mutex);
2224         if (rc)
2225                 goto out;
2226         if (list_empty(&tq->q))
2227                 goto out;
2228
2229 pop:
2230         ent = list_entry(tq->q.next, struct tq_ent, q_node);
2231         rval = ent->data;
2232
2233         list_del(&ent->q_node);
2234         free(ent);
2235
2236 out:
2237         pthread_mutex_unlock(&tq->mutex);
2238         return rval;
2239 }
2240
2241 /* sprintf can be used in applog */
2242 static char* format_hash(char* buf, uint8_t *hash)
2243 {
2244         int len = 0;
2245         for (int i=0; i < 32; i += 4) {
2246                 len += sprintf(buf+len, "%02x%02x%02x%02x ",
2247                         hash[i], hash[i+1], hash[i+2], hash[i+3]);
2248         }
2249         return buf;
2250 }
2251
2252 void applog_compare_hash(void *hash, void *hash_ref)
2253 {
2254         char s[256] = "";
2255         int len = 0;
2256         uchar* hash1 = (uchar*)hash;
2257         uchar* hash2 = (uchar*)hash_ref;
2258         for (int i=0; i < 32; i += 4) {
2259                 const char *color = memcmp(hash1+i, hash2+i, 4) ? CL_WHT : CL_GRY;
2260                 len += sprintf(s+len, "%s%02x%02x%02x%02x " CL_GRY, color,
2261                         hash1[i], hash1[i+1], hash1[i+2], hash1[i+3]);
2262                 s[len] = '\0';
2263         }
2264         applog(LOG_DEBUG, "%s", s);
2265 }
2266
2267 void applog_hash(void *hash)
2268 {
2269         char s[128] = {'\0'};
2270         applog(LOG_DEBUG, "%s", format_hash(s, (uchar*) hash));
2271 }
2272
2273 void applog_hex(void *data, int len)
2274 {
2275         char* hex = abin2hex((uchar*)data, len);
2276         applog(LOG_DEBUG, "%s", hex);
2277         free(hex);
2278 }
2279
2280 void applog_hash64(void *hash)
2281 {
2282         char s[128] = {'\0'};
2283         char t[128] = {'\0'};
2284         applog(LOG_DEBUG, "%s %s", format_hash(s, (uchar*)hash), format_hash(t, &((uchar*)hash)[32]));
2285 }
2286
2287
2288 #define printpfx(n,h) \
2289         printf("%s%11s%s: %s\n", CL_CYN, n, CL_N, format_hash(s, (uint8_t*) h))
2290
2291 void print_hash_tests(void)
2292 {
2293         uchar *scratchbuf = NULL;
2294         char hash[128], s[80];
2295         char buf[192] = { 0 };
2296
2297         scratchbuf = (uchar*) calloc(128, 1024);
2298
2299         printf(CL_WHT "CPU HASH ON EMPTY BUFFER RESULTS:" CL_N "\n\n");
2300
2301         //buf[0] = 1; buf[64] = 2; // for endian tests
2302
2303         axiomhash(&hash[0], &buf[0]);
2304         printpfx("axiom", hash);
2305
2306         bastionhash(&hash[0], &buf[0]);
2307         printpfx("bastion", hash);
2308
2309         blakehash(&hash[0], &buf[0]);
2310         printpfx("blake", hash);
2311
2312         blakecoinhash(&hash[0], &buf[0]);
2313         printpfx("blakecoin", hash);
2314
2315         blake2s_hash(&hash[0], &buf[0]);
2316         printpfx("blake2s", hash);
2317
2318         bmwhash(&hash[0], &buf[0]);
2319         printpfx("bmw", hash);
2320
2321         c11hash(&hash[0], &buf[0]);
2322         printpfx("c11", hash);
2323
2324         cryptolight_hash(&hash[0], &buf[0], 76);
2325         printpfx("cryptolight", hash);
2326
2327         cryptonight_hash(&hash[0], &buf[0], 76);
2328         printpfx("cryptonight", hash);
2329
2330         decred_hash(&hash[0], &buf[0]);
2331         printpfx("decred", hash);
2332
2333         droplp_hash(&hash[0], &buf[0]);
2334         printpfx("drop", hash);
2335
2336         freshhash(&hash[0], &buf[0], 80);
2337         printpfx("fresh", hash);
2338
2339         groestlhash(&hash[0], &buf[0]);
2340         printpfx("groestl", hash);
2341
2342         heavyhash((uint8_t*) &hash[0], (uint8_t*) &buf[0], 32);
2343         printpfx("heavy", hash);
2344
2345         keccakhash(&hash[0], &buf[0]);
2346         printpfx("keccak", hash);
2347
2348         luffahash(&hash[0], &buf[0]);
2349         printpfx("luffa", hash);
2350
2351         lyra2_hash(&hash[0], &buf[0]);
2352         printpfx("lyra2", hash);
2353
2354         lyra2rev2_hash(&hash[0], &buf[0]);
2355         printpfx("lyra2v2", hash);
2356
2357         myriadhash(&hash[0], &buf[0]);
2358         printpfx("myr-gr", hash);
2359
2360         neoscrypt((uchar*) &hash[0], (uchar*)&buf[0], 80000620);
2361         printpfx("neoscrypt", hash);
2362
2363         nist5hash(&hash[0], &buf[0]);
2364         printpfx("nist5", hash);
2365
2366         pentablakehash(&hash[0], &buf[0]);
2367         printpfx("pentablake", hash);
2368
2369         pluck_hash((uint32_t*)&hash[0], (uint32_t*)&buf[0], scratchbuf, 128);
2370         memset(&buf[0], 0, sizeof(buf));
2371         printpfx("pluck", hash);
2372
2373         init_quarkhash_contexts();
2374         quarkhash(&hash[0], &buf[0]);
2375         printpfx("quark", hash);
2376
2377         qubithash(&hash[0], &buf[0]);
2378         printpfx("qubit", hash);
2379
2380         scrypthash(&hash[0], &buf[0], 1024);
2381         printpfx("scrypt", hash);
2382
2383         scrypthash(&hash[0], &buf[0], 2048);
2384         printpfx("scrypt:2048", hash);
2385
2386         scryptjanehash(&hash[0], &buf[0], 9);
2387         printpfx("scrypt-jane", hash);
2388
2389         inkhash(&hash[0], &buf[0]);
2390         printpfx("shavite3", hash);
2391
2392         sha256d((uint8_t*) &hash[0], (uint8_t*)&buf[0], 64);
2393         printpfx("sha256d", hash);
2394
2395         sibhash(&hash[0], &buf[0]);
2396         printpfx("sib", hash);
2397
2398         skeinhash(&hash[0], &buf[0]);
2399         printpfx("skein", hash);
2400
2401         skein2hash(&hash[0], &buf[0]);
2402         printpfx("skein2", hash);
2403
2404         s3hash(&hash[0], &buf[0]);
2405         printpfx("s3", hash);
2406
2407         x11hash(&hash[0], &buf[0]);
2408         printpfx("x11", hash);
2409
2410         x13hash(&hash[0], &buf[0]);
2411         printpfx("x13", hash);
2412
2413         x14hash(&hash[0], &buf[0]);
2414         printpfx("x14", hash);
2415
2416         x15hash(&hash[0], &buf[0]);
2417         printpfx("x15", hash);
2418
2419         yescrypthash(&hash[0], &buf[0]);
2420         printpfx("yescrypt", hash);
2421
2422         //zr5hash(&hash[0], &buf[0]);
2423         zr5hash_pok(&hash[0], (uint32_t*) &buf[0]);
2424         memset(buf, 0, sizeof(buf));
2425         printpfx("zr5", hash);
2426
2427         printf("\n");
2428
2429         free(scratchbuf);
2430 }
2431
This page took 0.163881 seconds and 4 git commands to generate.