util.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * Copyright 2010 Jeff Garzik
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #define _GNU_SOURCE
  10. #include "cpuminer-config.h"
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <ctype.h>
  14. #include <stdarg.h>
  15. #include <string.h>
  16. #include <jansson.h>
  17. #include <curl/curl.h>
  18. #include <time.h>
  19. #include "miner.h"
  20. #include "elist.h"
  21. struct data_buffer {
  22. void *buf;
  23. size_t len;
  24. };
  25. struct upload_buffer {
  26. const void *buf;
  27. size_t len;
  28. };
  29. struct header_info {
  30. char *lp_path;
  31. };
  32. struct tq_ent {
  33. void *data;
  34. struct list_head q_node;
  35. };
  36. struct thread_q {
  37. struct list_head q;
  38. bool frozen;
  39. pthread_mutex_t mutex;
  40. pthread_cond_t cond;
  41. };
  42. void applog(int prio, const char *fmt, ...)
  43. {
  44. va_list ap;
  45. va_start(ap, fmt);
  46. #ifdef HAVE_SYSLOG_H
  47. if (use_syslog) {
  48. vsyslog(prio, fmt, ap);
  49. }
  50. #else
  51. if (0) {}
  52. #endif
  53. else {
  54. char *f;
  55. int len;
  56. struct timeval tv = { };
  57. struct tm tm, *tm_p;
  58. gettimeofday(&tv, NULL);
  59. pthread_mutex_lock(&time_lock);
  60. tm_p = localtime(&tv.tv_sec);
  61. memcpy(&tm, tm_p, sizeof(tm));
  62. pthread_mutex_unlock(&time_lock);
  63. len = 40 + strlen(fmt) + 2;
  64. f = alloca(len);
  65. sprintf(f, "[%d-%02d-%02d %02d:%02d:%02d] %s\n",
  66. tm.tm_year + 1900,
  67. tm.tm_mon,
  68. tm.tm_mday,
  69. tm.tm_hour,
  70. tm.tm_min,
  71. tm.tm_sec,
  72. fmt);
  73. vfprintf(stderr, f, ap); /* atomic write to stderr */
  74. }
  75. va_end(ap);
  76. }
  77. static void databuf_free(struct data_buffer *db)
  78. {
  79. if (!db)
  80. return;
  81. free(db->buf);
  82. memset(db, 0, sizeof(*db));
  83. }
  84. static size_t all_data_cb(const void *ptr, size_t size, size_t nmemb,
  85. void *user_data)
  86. {
  87. struct data_buffer *db = user_data;
  88. size_t len = size * nmemb;
  89. size_t oldlen, newlen;
  90. void *newmem;
  91. static const unsigned char zero;
  92. oldlen = db->len;
  93. newlen = oldlen + len;
  94. newmem = realloc(db->buf, newlen + 1);
  95. if (!newmem)
  96. return 0;
  97. db->buf = newmem;
  98. db->len = newlen;
  99. memcpy(db->buf + oldlen, ptr, len);
  100. memcpy(db->buf + newlen, &zero, 1); /* null terminate */
  101. return len;
  102. }
  103. static size_t upload_data_cb(void *ptr, size_t size, size_t nmemb,
  104. void *user_data)
  105. {
  106. struct upload_buffer *ub = user_data;
  107. int len = size * nmemb;
  108. if (len > ub->len)
  109. len = ub->len;
  110. if (len) {
  111. memcpy(ptr, ub->buf, len);
  112. ub->buf += len;
  113. ub->len -= len;
  114. }
  115. return len;
  116. }
  117. static size_t resp_hdr_cb(void *ptr, size_t size, size_t nmemb, void *user_data)
  118. {
  119. struct header_info *hi = user_data;
  120. size_t remlen, slen, ptrlen = size * nmemb;
  121. char *rem, *val = NULL, *key = NULL;
  122. void *tmp;
  123. if (opt_protocol)
  124. printf("In resp_hdr_cb\n");
  125. val = calloc(1, ptrlen);
  126. key = calloc(1, ptrlen);
  127. if (!key || !val)
  128. goto out;
  129. tmp = memchr(ptr, ':', ptrlen);
  130. if (!tmp || (tmp == ptr)) /* skip empty keys / blanks */
  131. goto out;
  132. slen = tmp - ptr;
  133. if ((slen + 1) == ptrlen) /* skip key w/ no value */
  134. goto out;
  135. memcpy(key, ptr, slen); /* store & nul term key */
  136. key[slen] = 0;
  137. rem = ptr + slen + 1; /* trim value's leading whitespace */
  138. remlen = ptrlen - slen - 1;
  139. while ((remlen > 0) && (isspace(*rem))) {
  140. remlen--;
  141. rem++;
  142. }
  143. memcpy(val, rem, remlen); /* store value, trim trailing ws */
  144. val[remlen] = 0;
  145. while ((*val) && (isspace(val[strlen(val) - 1]))) {
  146. val[strlen(val) - 1] = 0;
  147. }
  148. if (!*val) /* skip blank value */
  149. goto out;
  150. if (opt_protocol)
  151. printf("HTTP hdr(%s): %s\n", key, val);
  152. if (!strcasecmp("X-Long-Polling", key)) {
  153. hi->lp_path = val; /* steal memory reference */
  154. val = NULL;
  155. }
  156. out:
  157. free(key);
  158. free(val);
  159. return ptrlen;
  160. }
  161. json_t *json_rpc_call(CURL *curl, const char *url,
  162. const char *userpass, const char *rpc_req,
  163. bool longpoll_scan, bool longpoll)
  164. {
  165. json_t *val, *err_val, *res_val;
  166. int rc;
  167. struct data_buffer all_data = { };
  168. struct upload_buffer upload_data;
  169. json_error_t err = { };
  170. struct curl_slist *headers = NULL;
  171. char len_hdr[64];
  172. char curl_err_str[CURL_ERROR_SIZE];
  173. long timeout = longpoll ? (60 * 60) : (60 * 10);
  174. struct header_info hi = { };
  175. bool lp_scanning = false;
  176. /* it is assumed that 'curl' is freshly [re]initialized at this pt */
  177. if (longpoll_scan)
  178. lp_scanning = want_longpoll && !have_longpoll;
  179. if (opt_protocol)
  180. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  181. curl_easy_setopt(curl, CURLOPT_URL, url);
  182. curl_easy_setopt(curl, CURLOPT_ENCODING, "");
  183. curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
  184. curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
  185. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, all_data_cb);
  186. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &all_data);
  187. curl_easy_setopt(curl, CURLOPT_READFUNCTION, upload_data_cb);
  188. curl_easy_setopt(curl, CURLOPT_READDATA, &upload_data);
  189. curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_err_str);
  190. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  191. curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
  192. if (lp_scanning) {
  193. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, resp_hdr_cb);
  194. curl_easy_setopt(curl, CURLOPT_HEADERDATA, &hi);
  195. }
  196. if (userpass) {
  197. curl_easy_setopt(curl, CURLOPT_USERPWD, userpass);
  198. curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  199. }
  200. curl_easy_setopt(curl, CURLOPT_POST, 1);
  201. if (opt_protocol)
  202. printf("JSON protocol request:\n%s\n", rpc_req);
  203. upload_data.buf = rpc_req;
  204. upload_data.len = strlen(rpc_req);
  205. sprintf(len_hdr, "Content-Length: %lu",
  206. (unsigned long) upload_data.len);
  207. headers = curl_slist_append(headers,
  208. "Content-type: application/json");
  209. headers = curl_slist_append(headers, len_hdr);
  210. headers = curl_slist_append(headers, "Expect:"); /* disable Expect hdr*/
  211. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  212. rc = curl_easy_perform(curl);
  213. if (rc) {
  214. fprintf(stderr, "HTTP request failed: %s\n", curl_err_str);
  215. goto err_out;
  216. }
  217. /* If X-Long-Polling was found, activate long polling */
  218. if (hi.lp_path) {
  219. have_longpoll = true;
  220. opt_scantime = 60;
  221. tq_push(thr_info[longpoll_thr_id].q, hi.lp_path);
  222. } else
  223. free(hi.lp_path);
  224. hi.lp_path = NULL;
  225. val = json_loads(all_data.buf, &err);
  226. if (!val) {
  227. fprintf(stderr, "JSON decode failed(%d): %s\n", err.line, err.text);
  228. goto err_out;
  229. }
  230. if (opt_protocol) {
  231. char *s = json_dumps(val, JSON_INDENT(3));
  232. printf("JSON protocol response:\n%s\n", s);
  233. free(s);
  234. }
  235. /* JSON-RPC valid response returns a non-null 'result',
  236. * and a null 'error'.
  237. */
  238. res_val = json_object_get(val, "result");
  239. err_val = json_object_get(val, "error");
  240. if (!res_val || json_is_null(res_val) ||
  241. (err_val && !json_is_null(err_val))) {
  242. char *s;
  243. if (err_val)
  244. s = json_dumps(err_val, JSON_INDENT(3));
  245. else
  246. s = strdup("(unknown reason)");
  247. fprintf(stderr, "JSON-RPC call failed: %s\n", s);
  248. free(s);
  249. goto err_out;
  250. }
  251. databuf_free(&all_data);
  252. curl_slist_free_all(headers);
  253. curl_easy_reset(curl);
  254. return val;
  255. err_out:
  256. databuf_free(&all_data);
  257. curl_slist_free_all(headers);
  258. curl_easy_reset(curl);
  259. return NULL;
  260. }
  261. char *bin2hex(const unsigned char *p, size_t len)
  262. {
  263. int i;
  264. char *s = malloc((len * 2) + 1);
  265. if (!s)
  266. return NULL;
  267. for (i = 0; i < len; i++)
  268. sprintf(s + (i * 2), "%02x", (unsigned int) p[i]);
  269. return s;
  270. }
  271. bool hex2bin(unsigned char *p, const char *hexstr, size_t len)
  272. {
  273. while (*hexstr && len) {
  274. char hex_byte[3];
  275. unsigned int v;
  276. if (!hexstr[1]) {
  277. fprintf(stderr, "hex2bin str truncated\n");
  278. return false;
  279. }
  280. hex_byte[0] = hexstr[0];
  281. hex_byte[1] = hexstr[1];
  282. hex_byte[2] = 0;
  283. if (sscanf(hex_byte, "%x", &v) != 1) {
  284. fprintf(stderr, "hex2bin sscanf '%s' failed\n",
  285. hex_byte);
  286. return false;
  287. }
  288. *p = (unsigned char) v;
  289. p++;
  290. hexstr += 2;
  291. len--;
  292. }
  293. return (len == 0 && *hexstr == 0) ? true : false;
  294. }
  295. /* Subtract the `struct timeval' values X and Y,
  296. storing the result in RESULT.
  297. Return 1 if the difference is negative, otherwise 0. */
  298. int
  299. timeval_subtract (
  300. struct timeval *result, struct timeval *x, struct timeval *y)
  301. {
  302. /* Perform the carry for the later subtraction by updating Y. */
  303. if (x->tv_usec < y->tv_usec) {
  304. int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
  305. y->tv_usec -= 1000000 * nsec;
  306. y->tv_sec += nsec;
  307. }
  308. if (x->tv_usec - y->tv_usec > 1000000) {
  309. int nsec = (x->tv_usec - y->tv_usec) / 1000000;
  310. y->tv_usec += 1000000 * nsec;
  311. y->tv_sec -= nsec;
  312. }
  313. /* Compute the time remaining to wait.
  314. `tv_usec' is certainly positive. */
  315. result->tv_sec = x->tv_sec - y->tv_sec;
  316. result->tv_usec = x->tv_usec - y->tv_usec;
  317. /* Return 1 if result is negative. */
  318. return x->tv_sec < y->tv_sec;
  319. }
  320. bool fulltest(const unsigned char *hash, const unsigned char *target)
  321. {
  322. unsigned char hash_swap[32], target_swap[32];
  323. uint32_t *hash32 = (uint32_t *) hash_swap;
  324. uint32_t *target32 = (uint32_t *) target_swap;
  325. int i;
  326. bool rc = true;
  327. char *hash_str, *target_str;
  328. swap256(hash_swap, hash);
  329. swap256(target_swap, target);
  330. for (i = 0; i < 32/4; i++) {
  331. uint32_t h32tmp = swab32(hash32[i]);
  332. uint32_t t32tmp = target32[i];
  333. target32[i] = swab32(target32[i]); /* for printing */
  334. if (h32tmp > t32tmp) {
  335. rc = false;
  336. break;
  337. }
  338. if (h32tmp < t32tmp) {
  339. rc = true;
  340. break;
  341. }
  342. }
  343. if (opt_debug) {
  344. hash_str = bin2hex(hash_swap, 32);
  345. target_str = bin2hex(target_swap, 32);
  346. fprintf(stderr, " Proof: %s\nTarget: %s\nTrgVal? %s\n",
  347. hash_str,
  348. target_str,
  349. rc ? "YES (hash < target)" :
  350. "no (false positive; hash > target)");
  351. free(hash_str);
  352. free(target_str);
  353. }
  354. return true; /* FIXME: return rc; */
  355. }
  356. struct thread_q *tq_new(void)
  357. {
  358. struct thread_q *tq;
  359. tq = calloc(1, sizeof(*tq));
  360. if (!tq)
  361. return NULL;
  362. INIT_LIST_HEAD(&tq->q);
  363. pthread_mutex_init(&tq->mutex, NULL);
  364. pthread_cond_init(&tq->cond, NULL);
  365. return tq;
  366. }
  367. void tq_free(struct thread_q *tq)
  368. {
  369. struct tq_ent *ent, *iter;
  370. if (!tq)
  371. return;
  372. list_for_each_entry_safe(ent, iter, &tq->q, q_node) {
  373. list_del(&ent->q_node);
  374. free(ent);
  375. }
  376. pthread_cond_destroy(&tq->cond);
  377. pthread_mutex_destroy(&tq->mutex);
  378. memset(tq, 0, sizeof(*tq)); /* poison */
  379. free(tq);
  380. }
  381. static void tq_freezethaw(struct thread_q *tq, bool frozen)
  382. {
  383. pthread_mutex_lock(&tq->mutex);
  384. tq->frozen = frozen;
  385. pthread_cond_signal(&tq->cond);
  386. pthread_mutex_unlock(&tq->mutex);
  387. }
  388. void tq_freeze(struct thread_q *tq)
  389. {
  390. tq_freezethaw(tq, true);
  391. }
  392. void tq_thaw(struct thread_q *tq)
  393. {
  394. tq_freezethaw(tq, false);
  395. }
  396. bool tq_push(struct thread_q *tq, void *data)
  397. {
  398. struct tq_ent *ent;
  399. bool rc = true;
  400. ent = calloc(1, sizeof(*ent));
  401. if (!ent)
  402. return false;
  403. ent->data = data;
  404. INIT_LIST_HEAD(&ent->q_node);
  405. pthread_mutex_lock(&tq->mutex);
  406. if (!tq->frozen) {
  407. list_add_tail(&ent->q_node, &tq->q);
  408. } else {
  409. free(ent);
  410. rc = false;
  411. }
  412. pthread_cond_signal(&tq->cond);
  413. pthread_mutex_unlock(&tq->mutex);
  414. return rc;
  415. }
  416. void *tq_pop(struct thread_q *tq, const struct timespec *abstime)
  417. {
  418. struct tq_ent *ent;
  419. void *rval = NULL;
  420. int rc;
  421. pthread_mutex_lock(&tq->mutex);
  422. if (!list_empty(&tq->q))
  423. goto pop;
  424. if (abstime)
  425. rc = pthread_cond_timedwait(&tq->cond, &tq->mutex, abstime);
  426. else
  427. rc = pthread_cond_wait(&tq->cond, &tq->mutex);
  428. if (rc)
  429. goto out;
  430. if (list_empty(&tq->q))
  431. goto out;
  432. pop:
  433. ent = list_entry(tq->q.next, struct tq_ent, q_node);
  434. rval = ent->data;
  435. list_del(&ent->q_node);
  436. free(ent);
  437. out:
  438. pthread_mutex_unlock(&tq->mutex);
  439. return rval;
  440. }