util.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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(const char *url, const char *userpass, const char *rpc_req)
  66. {
  67. CURL *curl;
  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. curl = curl_easy_init();
  77. if (!curl) {
  78. fprintf(stderr, "CURL initialization failed, aborting JSON-RPC call\n");
  79. return NULL;
  80. }
  81. if (opt_protocol)
  82. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  83. curl_easy_setopt(curl, CURLOPT_URL, url);
  84. curl_easy_setopt(curl, CURLOPT_ENCODING, "");
  85. curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
  86. curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
  87. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, all_data_cb);
  88. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &all_data);
  89. curl_easy_setopt(curl, CURLOPT_READFUNCTION, upload_data_cb);
  90. curl_easy_setopt(curl, CURLOPT_READDATA, &upload_data);
  91. curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_err_str);
  92. if (userpass) {
  93. curl_easy_setopt(curl, CURLOPT_USERPWD, userpass);
  94. curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  95. }
  96. curl_easy_setopt(curl, CURLOPT_POST, 1);
  97. if (opt_protocol)
  98. printf("JSON protocol request:\n%s\n", rpc_req);
  99. upload_data.buf = rpc_req;
  100. upload_data.len = strlen(rpc_req);
  101. sprintf(len_hdr, "Content-Length: %lu",
  102. (unsigned long) upload_data.len);
  103. headers = curl_slist_append(headers,
  104. "Content-type: application/json");
  105. headers = curl_slist_append(headers, len_hdr);
  106. headers = curl_slist_append(headers, "Expect:"); /* disable Expect hdr*/
  107. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  108. rc = curl_easy_perform(curl);
  109. if (rc) {
  110. fprintf(stderr, "HTTP request failed: %s\n", curl_err_str);
  111. goto err_out;
  112. }
  113. val = json_loads(all_data.buf, &err);
  114. if (!val) {
  115. fprintf(stderr, "JSON decode failed(%d): %s\n", err.line, err.text);
  116. goto err_out;
  117. }
  118. if (opt_protocol) {
  119. char *s = json_dumps(val, JSON_INDENT(3));
  120. printf("JSON protocol response:\n%s\n", s);
  121. free(s);
  122. }
  123. /* JSON-RPC valid response returns a non-null 'result',
  124. * and a null 'error'.
  125. */
  126. res_val = json_object_get(val, "result");
  127. err_val = json_object_get(val, "error");
  128. if (!res_val || json_is_null(res_val) ||
  129. (err_val && !json_is_null(err_val))) {
  130. char *s;
  131. if (err_val)
  132. s = json_dumps(err_val, JSON_INDENT(3));
  133. else
  134. s = strdup("(unknown reason)");
  135. fprintf(stderr, "JSON-RPC call failed: %s\n", s);
  136. free(s);
  137. goto err_out;
  138. }
  139. databuf_free(&all_data);
  140. curl_slist_free_all(headers);
  141. curl_easy_cleanup(curl);
  142. return val;
  143. err_out:
  144. databuf_free(&all_data);
  145. curl_slist_free_all(headers);
  146. curl_easy_cleanup(curl);
  147. return NULL;
  148. }
  149. char *bin2hex(unsigned char *p, size_t len)
  150. {
  151. int i;
  152. char *s = malloc((len * 2) + 1);
  153. if (!s)
  154. return NULL;
  155. for (i = 0; i < len; i++)
  156. sprintf(s + (i * 2), "%02x", (unsigned int) p[i]);
  157. return s;
  158. }
  159. bool hex2bin(unsigned char *p, const char *hexstr, size_t len)
  160. {
  161. while (*hexstr && len) {
  162. char hex_byte[3];
  163. unsigned int v;
  164. if (!hexstr[1]) {
  165. fprintf(stderr, "hex2bin str truncated\n");
  166. return false;
  167. }
  168. hex_byte[0] = hexstr[0];
  169. hex_byte[1] = hexstr[1];
  170. hex_byte[2] = 0;
  171. if (sscanf(hex_byte, "%x", &v) != 1) {
  172. fprintf(stderr, "hex2bin sscanf '%s' failed\n",
  173. hex_byte);
  174. return false;
  175. }
  176. *p = (unsigned char) v;
  177. p++;
  178. hexstr += 2;
  179. len--;
  180. }
  181. return (len == 0 && *hexstr == 0) ? true : false;
  182. }
  183. /* Subtract the `struct timeval' values X and Y,
  184. storing the result in RESULT.
  185. Return 1 if the difference is negative, otherwise 0. */
  186. int
  187. timeval_subtract (
  188. struct timeval *result, struct timeval *x, struct timeval *y)
  189. {
  190. /* Perform the carry for the later subtraction by updating Y. */
  191. if (x->tv_usec < y->tv_usec) {
  192. int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
  193. y->tv_usec -= 1000000 * nsec;
  194. y->tv_sec += nsec;
  195. }
  196. if (x->tv_usec - y->tv_usec > 1000000) {
  197. int nsec = (x->tv_usec - y->tv_usec) / 1000000;
  198. y->tv_usec += 1000000 * nsec;
  199. y->tv_sec -= nsec;
  200. }
  201. /* Compute the time remaining to wait.
  202. `tv_usec' is certainly positive. */
  203. result->tv_sec = x->tv_sec - y->tv_sec;
  204. result->tv_usec = x->tv_usec - y->tv_usec;
  205. /* Return 1 if result is negative. */
  206. return x->tv_sec < y->tv_sec;
  207. }