util.c 8.6 KB

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