util.c 5.0 KB

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