util.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. struct data_buffer {
  18. void *buf;
  19. size_t len;
  20. };
  21. struct upload_buffer {
  22. const void *buf;
  23. size_t len;
  24. };
  25. static void databuf_free(struct data_buffer *db)
  26. {
  27. if (!db)
  28. return;
  29. free(db->buf);
  30. memset(db, 0, sizeof(*db));
  31. }
  32. static size_t all_data_cb(const void *ptr, size_t size, size_t nmemb,
  33. void *user_data)
  34. {
  35. struct data_buffer *db = user_data;
  36. size_t len = size * nmemb;
  37. size_t oldlen, newlen;
  38. void *newmem;
  39. static const unsigned char zero;
  40. oldlen = db->len;
  41. newlen = oldlen + len;
  42. newmem = realloc(db->buf, newlen + 1);
  43. if (!newmem)
  44. return 0;
  45. db->buf = newmem;
  46. db->len = newlen;
  47. memcpy(db->buf + oldlen, ptr, len);
  48. memcpy(db->buf + newlen, &zero, 1); /* null terminate */
  49. return len;
  50. }
  51. static size_t upload_data_cb(void *ptr, size_t size, size_t nmemb,
  52. void *user_data)
  53. {
  54. struct upload_buffer *ub = user_data;
  55. int len = size * nmemb;
  56. if (len > ub->len)
  57. len = ub->len;
  58. if (len) {
  59. memcpy(ptr, ub->buf, len);
  60. ub->buf += len;
  61. ub->len -= len;
  62. }
  63. return len;
  64. }
  65. json_t *json_rpc_call(CURL *curl, const char *url,
  66. const char *userpass, const char *rpc_req)
  67. {
  68. json_t *val, *err_val, *res_val;
  69. int rc;
  70. struct data_buffer all_data = { };
  71. struct upload_buffer upload_data;
  72. json_error_t err = { };
  73. struct curl_slist *headers = NULL;
  74. char len_hdr[64];
  75. char curl_err_str[CURL_ERROR_SIZE];
  76. /* it is assumed that 'curl' is freshly [re]initialized at this pt */
  77. if (opt_protocol)
  78. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  79. curl_easy_setopt(curl, CURLOPT_URL, url);
  80. curl_easy_setopt(curl, CURLOPT_ENCODING, "");
  81. curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
  82. curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
  83. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, all_data_cb);
  84. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &all_data);
  85. curl_easy_setopt(curl, CURLOPT_READFUNCTION, upload_data_cb);
  86. curl_easy_setopt(curl, CURLOPT_READDATA, &upload_data);
  87. curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_err_str);
  88. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  89. if (userpass) {
  90. curl_easy_setopt(curl, CURLOPT_USERPWD, userpass);
  91. curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  92. }
  93. curl_easy_setopt(curl, CURLOPT_POST, 1);
  94. if (opt_protocol)
  95. printf("JSON protocol request:\n%s\n", rpc_req);
  96. upload_data.buf = rpc_req;
  97. upload_data.len = strlen(rpc_req);
  98. sprintf(len_hdr, "Content-Length: %lu",
  99. (unsigned long) upload_data.len);
  100. headers = curl_slist_append(headers,
  101. "Content-type: application/json");
  102. headers = curl_slist_append(headers, len_hdr);
  103. headers = curl_slist_append(headers, "Expect:"); /* disable Expect hdr*/
  104. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  105. rc = curl_easy_perform(curl);
  106. if (rc) {
  107. fprintf(stderr, "HTTP request failed: %s\n", curl_err_str);
  108. goto err_out;
  109. }
  110. val = json_loads(all_data.buf, &err);
  111. if (!val) {
  112. fprintf(stderr, "JSON decode failed(%d): %s\n", err.line, err.text);
  113. goto err_out;
  114. }
  115. if (opt_protocol) {
  116. char *s = json_dumps(val, JSON_INDENT(3));
  117. printf("JSON protocol response:\n%s\n", s);
  118. free(s);
  119. }
  120. /* JSON-RPC valid response returns a non-null 'result',
  121. * and a null 'error'.
  122. */
  123. res_val = json_object_get(val, "result");
  124. err_val = json_object_get(val, "error");
  125. if (!res_val || json_is_null(res_val) ||
  126. (err_val && !json_is_null(err_val))) {
  127. char *s;
  128. if (err_val)
  129. s = json_dumps(err_val, JSON_INDENT(3));
  130. else
  131. s = strdup("(unknown reason)");
  132. fprintf(stderr, "JSON-RPC call failed: %s\n", s);
  133. free(s);
  134. goto err_out;
  135. }
  136. databuf_free(&all_data);
  137. curl_slist_free_all(headers);
  138. curl_easy_reset(curl);
  139. return val;
  140. err_out:
  141. databuf_free(&all_data);
  142. curl_slist_free_all(headers);
  143. curl_easy_reset(curl);
  144. return NULL;
  145. }
  146. char *bin2hex(unsigned char *p, size_t len)
  147. {
  148. int i;
  149. char *s = malloc((len * 2) + 1);
  150. if (!s)
  151. return NULL;
  152. for (i = 0; i < len; i++)
  153. sprintf(s + (i * 2), "%02x", (unsigned int) p[i]);
  154. return s;
  155. }
  156. bool hex2bin(unsigned char *p, const char *hexstr, size_t len)
  157. {
  158. while (*hexstr && len) {
  159. char hex_byte[3];
  160. unsigned int v;
  161. if (!hexstr[1]) {
  162. fprintf(stderr, "hex2bin str truncated\n");
  163. return false;
  164. }
  165. hex_byte[0] = hexstr[0];
  166. hex_byte[1] = hexstr[1];
  167. hex_byte[2] = 0;
  168. if (sscanf(hex_byte, "%x", &v) != 1) {
  169. fprintf(stderr, "hex2bin sscanf '%s' failed\n",
  170. hex_byte);
  171. return false;
  172. }
  173. *p = (unsigned char) v;
  174. p++;
  175. hexstr += 2;
  176. len--;
  177. }
  178. return (len == 0 && *hexstr == 0) ? true : false;
  179. }
  180. /* Subtract the `struct timeval' values X and Y,
  181. storing the result in RESULT.
  182. Return 1 if the difference is negative, otherwise 0. */
  183. int
  184. timeval_subtract (
  185. struct timeval *result, struct timeval *x, struct timeval *y)
  186. {
  187. /* Perform the carry for the later subtraction by updating Y. */
  188. if (x->tv_usec < y->tv_usec) {
  189. int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
  190. y->tv_usec -= 1000000 * nsec;
  191. y->tv_sec += nsec;
  192. }
  193. if (x->tv_usec - y->tv_usec > 1000000) {
  194. int nsec = (x->tv_usec - y->tv_usec) / 1000000;
  195. y->tv_usec += 1000000 * nsec;
  196. y->tv_sec -= nsec;
  197. }
  198. /* Compute the time remaining to wait.
  199. `tv_usec' is certainly positive. */
  200. result->tv_sec = x->tv_sec - y->tv_sec;
  201. result->tv_usec = x->tv_usec - y->tv_usec;
  202. /* Return 1 if result is negative. */
  203. return x->tv_sec < y->tv_sec;
  204. }
  205. bool fulltest(const unsigned char *hash, const unsigned char *target)
  206. {
  207. unsigned char hash_swap[32], target_swap[32];
  208. uint32_t *hash32 = (uint32_t *) hash_swap;
  209. uint32_t *target32 = (uint32_t *) target_swap;
  210. int i;
  211. bool rc = true;
  212. char *hash_str, *target_str;
  213. swap256(hash_swap, hash);
  214. swap256(target_swap, target);
  215. for (i = 0; i < 32/4; i++) {
  216. uint32_t h32tmp = swab32(hash32[i]);
  217. uint32_t t32tmp = target32[i];
  218. target32[i] = swab32(target32[i]); /* for printing */
  219. if (h32tmp > t32tmp) {
  220. rc = false;
  221. break;
  222. }
  223. if (h32tmp < t32tmp) {
  224. rc = true;
  225. break;
  226. }
  227. }
  228. if (opt_debug) {
  229. hash_str = bin2hex(hash_swap, 32);
  230. target_str = bin2hex(target_swap, 32);
  231. fprintf(stderr, " Proof: %s\nTarget: %s\nTrgVal? %s\n",
  232. hash_str,
  233. target_str,
  234. rc ? "YES (hash < target)" :
  235. "no (false positive; hash > target)");
  236. free(hash_str);
  237. free(target_str);
  238. }
  239. return true; /* FIXME: return rc; */
  240. }