util.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 <string.h>
  14. #include <pthread.h>
  15. #include <jansson.h>
  16. #include <curl/curl.h>
  17. #include "miner.h"
  18. #include "elist.h"
  19. struct data_buffer {
  20. void *buf;
  21. size_t len;
  22. };
  23. struct upload_buffer {
  24. const void *buf;
  25. size_t len;
  26. };
  27. struct tq_ent {
  28. void *data;
  29. struct list_head q_node;
  30. };
  31. struct thread_q {
  32. struct list_head q;
  33. bool frozen;
  34. pthread_mutex_t mutex;
  35. pthread_cond_t cond;
  36. };
  37. static void databuf_free(struct data_buffer *db)
  38. {
  39. if (!db)
  40. return;
  41. free(db->buf);
  42. memset(db, 0, sizeof(*db));
  43. }
  44. static size_t all_data_cb(const void *ptr, size_t size, size_t nmemb,
  45. void *user_data)
  46. {
  47. struct data_buffer *db = user_data;
  48. size_t len = size * nmemb;
  49. size_t oldlen, newlen;
  50. void *newmem;
  51. static const unsigned char zero;
  52. oldlen = db->len;
  53. newlen = oldlen + len;
  54. newmem = realloc(db->buf, newlen + 1);
  55. if (!newmem)
  56. return 0;
  57. db->buf = newmem;
  58. db->len = newlen;
  59. memcpy(db->buf + oldlen, ptr, len);
  60. memcpy(db->buf + newlen, &zero, 1); /* null terminate */
  61. return len;
  62. }
  63. static size_t upload_data_cb(void *ptr, size_t size, size_t nmemb,
  64. void *user_data)
  65. {
  66. struct upload_buffer *ub = user_data;
  67. int len = size * nmemb;
  68. if (len > ub->len)
  69. len = ub->len;
  70. if (len) {
  71. memcpy(ptr, ub->buf, len);
  72. ub->buf += len;
  73. ub->len -= len;
  74. }
  75. return len;
  76. }
  77. json_t *json_rpc_call(CURL *curl, const char *url,
  78. const char *userpass, const char *rpc_req)
  79. {
  80. json_t *val, *err_val, *res_val;
  81. int rc;
  82. struct data_buffer all_data = { };
  83. struct upload_buffer upload_data;
  84. json_error_t err = { };
  85. struct curl_slist *headers = NULL;
  86. char len_hdr[64];
  87. char curl_err_str[CURL_ERROR_SIZE];
  88. /* it is assumed that 'curl' is freshly [re]initialized at this pt */
  89. if (opt_protocol)
  90. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  91. curl_easy_setopt(curl, CURLOPT_URL, url);
  92. curl_easy_setopt(curl, CURLOPT_ENCODING, "");
  93. curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
  94. curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
  95. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, all_data_cb);
  96. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &all_data);
  97. curl_easy_setopt(curl, CURLOPT_READFUNCTION, upload_data_cb);
  98. curl_easy_setopt(curl, CURLOPT_READDATA, &upload_data);
  99. curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_err_str);
  100. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  101. if (userpass) {
  102. curl_easy_setopt(curl, CURLOPT_USERPWD, userpass);
  103. curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  104. }
  105. curl_easy_setopt(curl, CURLOPT_POST, 1);
  106. if (opt_protocol)
  107. printf("JSON protocol request:\n%s\n", rpc_req);
  108. upload_data.buf = rpc_req;
  109. upload_data.len = strlen(rpc_req);
  110. sprintf(len_hdr, "Content-Length: %lu",
  111. (unsigned long) upload_data.len);
  112. headers = curl_slist_append(headers,
  113. "Content-type: application/json");
  114. headers = curl_slist_append(headers, len_hdr);
  115. headers = curl_slist_append(headers, "Expect:"); /* disable Expect hdr*/
  116. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  117. rc = curl_easy_perform(curl);
  118. if (rc) {
  119. fprintf(stderr, "HTTP request failed: %s\n", curl_err_str);
  120. goto err_out;
  121. }
  122. val = json_loads(all_data.buf, &err);
  123. if (!val) {
  124. fprintf(stderr, "JSON decode failed(%d): %s\n", err.line, err.text);
  125. goto err_out;
  126. }
  127. if (opt_protocol) {
  128. char *s = json_dumps(val, JSON_INDENT(3));
  129. printf("JSON protocol response:\n%s\n", s);
  130. free(s);
  131. }
  132. /* JSON-RPC valid response returns a non-null 'result',
  133. * and a null 'error'.
  134. */
  135. res_val = json_object_get(val, "result");
  136. err_val = json_object_get(val, "error");
  137. if (!res_val || json_is_null(res_val) ||
  138. (err_val && !json_is_null(err_val))) {
  139. char *s;
  140. if (err_val)
  141. s = json_dumps(err_val, JSON_INDENT(3));
  142. else
  143. s = strdup("(unknown reason)");
  144. fprintf(stderr, "JSON-RPC call failed: %s\n", s);
  145. free(s);
  146. goto err_out;
  147. }
  148. databuf_free(&all_data);
  149. curl_slist_free_all(headers);
  150. curl_easy_reset(curl);
  151. return val;
  152. err_out:
  153. databuf_free(&all_data);
  154. curl_slist_free_all(headers);
  155. curl_easy_reset(curl);
  156. return NULL;
  157. }
  158. char *bin2hex(const unsigned char *p, size_t len)
  159. {
  160. int i;
  161. char *s = malloc((len * 2) + 1);
  162. if (!s)
  163. return NULL;
  164. for (i = 0; i < len; i++)
  165. sprintf(s + (i * 2), "%02x", (unsigned int) p[i]);
  166. return s;
  167. }
  168. bool hex2bin(unsigned char *p, const char *hexstr, size_t len)
  169. {
  170. while (*hexstr && len) {
  171. char hex_byte[3];
  172. unsigned int v;
  173. if (!hexstr[1]) {
  174. fprintf(stderr, "hex2bin str truncated\n");
  175. return false;
  176. }
  177. hex_byte[0] = hexstr[0];
  178. hex_byte[1] = hexstr[1];
  179. hex_byte[2] = 0;
  180. if (sscanf(hex_byte, "%x", &v) != 1) {
  181. fprintf(stderr, "hex2bin sscanf '%s' failed\n",
  182. hex_byte);
  183. return false;
  184. }
  185. *p = (unsigned char) v;
  186. p++;
  187. hexstr += 2;
  188. len--;
  189. }
  190. return (len == 0 && *hexstr == 0) ? true : false;
  191. }
  192. /* Subtract the `struct timeval' values X and Y,
  193. storing the result in RESULT.
  194. Return 1 if the difference is negative, otherwise 0. */
  195. int
  196. timeval_subtract (
  197. struct timeval *result, struct timeval *x, struct timeval *y)
  198. {
  199. /* Perform the carry for the later subtraction by updating Y. */
  200. if (x->tv_usec < y->tv_usec) {
  201. int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
  202. y->tv_usec -= 1000000 * nsec;
  203. y->tv_sec += nsec;
  204. }
  205. if (x->tv_usec - y->tv_usec > 1000000) {
  206. int nsec = (x->tv_usec - y->tv_usec) / 1000000;
  207. y->tv_usec += 1000000 * nsec;
  208. y->tv_sec -= nsec;
  209. }
  210. /* Compute the time remaining to wait.
  211. `tv_usec' is certainly positive. */
  212. result->tv_sec = x->tv_sec - y->tv_sec;
  213. result->tv_usec = x->tv_usec - y->tv_usec;
  214. /* Return 1 if result is negative. */
  215. return x->tv_sec < y->tv_sec;
  216. }
  217. bool fulltest(const unsigned char *hash, const unsigned char *target)
  218. {
  219. unsigned char hash_swap[32], target_swap[32];
  220. uint32_t *hash32 = (uint32_t *) hash_swap;
  221. uint32_t *target32 = (uint32_t *) target_swap;
  222. int i;
  223. bool rc = true;
  224. char *hash_str, *target_str;
  225. swap256(hash_swap, hash);
  226. swap256(target_swap, target);
  227. for (i = 0; i < 32/4; i++) {
  228. uint32_t h32tmp = swab32(hash32[i]);
  229. uint32_t t32tmp = target32[i];
  230. target32[i] = swab32(target32[i]); /* for printing */
  231. if (h32tmp > t32tmp) {
  232. rc = false;
  233. break;
  234. }
  235. if (h32tmp < t32tmp) {
  236. rc = true;
  237. break;
  238. }
  239. }
  240. if (opt_debug) {
  241. hash_str = bin2hex(hash_swap, 32);
  242. target_str = bin2hex(target_swap, 32);
  243. fprintf(stderr, " Proof: %s\nTarget: %s\nTrgVal? %s\n",
  244. hash_str,
  245. target_str,
  246. rc ? "YES (hash < target)" :
  247. "no (false positive; hash > target)");
  248. free(hash_str);
  249. free(target_str);
  250. }
  251. return true; /* FIXME: return rc; */
  252. }
  253. struct thread_q *tq_new(void)
  254. {
  255. struct thread_q *tq;
  256. tq = calloc(1, sizeof(*tq));
  257. if (!tq)
  258. return NULL;
  259. INIT_LIST_HEAD(&tq->q);
  260. pthread_mutex_init(&tq->mutex, NULL);
  261. pthread_cond_init(&tq->cond, NULL);
  262. return tq;
  263. }
  264. void tq_free(struct thread_q *tq)
  265. {
  266. struct tq_ent *ent, *iter;
  267. if (!tq)
  268. return;
  269. list_for_each_entry_safe(ent, iter, &tq->q, q_node) {
  270. list_del(&ent->q_node);
  271. free(ent);
  272. }
  273. pthread_cond_destroy(&tq->cond);
  274. pthread_mutex_destroy(&tq->mutex);
  275. memset(tq, 0, sizeof(*tq)); /* poison */
  276. free(tq);
  277. }
  278. static void tq_freezethaw(struct thread_q *tq, bool frozen)
  279. {
  280. pthread_mutex_lock(&tq->mutex);
  281. tq->frozen = frozen;
  282. pthread_cond_signal(&tq->cond);
  283. pthread_mutex_unlock(&tq->mutex);
  284. }
  285. void tq_freeze(struct thread_q *tq)
  286. {
  287. tq_freezethaw(tq, true);
  288. }
  289. void tq_thaw(struct thread_q *tq)
  290. {
  291. tq_freezethaw(tq, false);
  292. }
  293. bool tq_push(struct thread_q *tq, void *data)
  294. {
  295. struct tq_ent *ent;
  296. bool rc = true;
  297. ent = calloc(1, sizeof(*ent));
  298. if (!ent)
  299. return false;
  300. ent->data = data;
  301. INIT_LIST_HEAD(&ent->q_node);
  302. pthread_mutex_lock(&tq->mutex);
  303. if (!tq->frozen) {
  304. list_add_tail(&ent->q_node, &tq->q);
  305. } else {
  306. free(ent);
  307. rc = false;
  308. }
  309. pthread_cond_signal(&tq->cond);
  310. pthread_mutex_unlock(&tq->mutex);
  311. return rc;
  312. }
  313. void *tq_pop(struct thread_q *tq, const struct timespec *abstime)
  314. {
  315. struct tq_ent *ent;
  316. void *rval = NULL;
  317. int rc;
  318. pthread_mutex_lock(&tq->mutex);
  319. if (!list_empty(&tq->q))
  320. goto pop;
  321. if (abstime)
  322. rc = pthread_cond_timedwait(&tq->cond, &tq->mutex, abstime);
  323. else
  324. rc = pthread_cond_wait(&tq->cond, &tq->mutex);
  325. if (rc)
  326. goto out;
  327. if (list_empty(&tq->q))
  328. goto out;
  329. pop:
  330. ent = list_entry(tq->q.next, struct tq_ent, q_node);
  331. rval = ent->data;
  332. list_del(&ent->q_node);
  333. free(ent);
  334. out:
  335. pthread_mutex_unlock(&tq->mutex);
  336. return rval;
  337. }