driver-getwork.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright 2013-2014 Luke Dashjr
  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 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #ifdef WIN32
  11. #include <winsock2.h>
  12. #endif
  13. #include <stdint.h>
  14. #include <string.h>
  15. #ifndef WIN32
  16. #include <sys/types.h>
  17. #include <sys/select.h>
  18. #include <sys/socket.h>
  19. #endif
  20. #include <jansson.h>
  21. #include <microhttpd.h>
  22. #include <uthash.h>
  23. #include "deviceapi.h"
  24. #include "driver-proxy.h"
  25. #include "httpsrv.h"
  26. #include "miner.h"
  27. static
  28. void getwork_prepare_resp(struct MHD_Response *resp, struct MHD_Connection * const conn)
  29. {
  30. httpsrv_prepare_resp(resp);
  31. MHD_add_response_header(resp, MHD_HTTP_HEADER_CONTENT_TYPE, "application/json");
  32. MHD_add_response_header(resp, "X-Mining-Extensions", "hashesdone");
  33. #if MHD_VERSION >= 0x00093701
  34. const char * const agent = MHD_lookup_connection_value(conn, MHD_HEADER_KIND, "User-Agent");
  35. // Block Erupter products can't handle the Connection: close header, so force HTTP/1.0 for them
  36. if (!(strcmp(agent, "BE Cube BTC Miner") && strcmp(agent, "Jephis PIC Miner")))
  37. MHD_set_response_options(resp, MHD_RF_HTTP_VERSION_1_0_ONLY, MHD_RO_END);
  38. #endif
  39. }
  40. static
  41. struct MHD_Response *getwork_gen_error(int16_t errcode, const char *errmsg, const char *idstr, size_t idstr_sz, struct MHD_Connection * const conn)
  42. {
  43. size_t replysz = 0x40 + strlen(errmsg) + idstr_sz;
  44. char * const reply = malloc(replysz);
  45. replysz = snprintf(reply, replysz, "{\"result\":null,\"error\":{\"code\":%d,\"message\":\"%s\"},\"id\":%s}", errcode, errmsg, idstr ?: "0");
  46. struct MHD_Response * const resp = MHD_create_response_from_buffer(replysz, reply, MHD_RESPMEM_MUST_FREE);
  47. getwork_prepare_resp(resp, conn);
  48. return resp;
  49. }
  50. static
  51. int getwork_error(struct MHD_Connection *conn, int16_t errcode, const char *errmsg, const char *idstr, size_t idstr_sz)
  52. {
  53. struct MHD_Response * const resp = getwork_gen_error(errcode, errmsg, idstr, idstr_sz, conn);
  54. const int ret = MHD_queue_response(conn, 500, resp);
  55. MHD_destroy_response(resp);
  56. return ret;
  57. }
  58. int handle_getwork(struct MHD_Connection *conn, bytes_t *upbuf)
  59. {
  60. struct proxy_client *client;
  61. struct MHD_Response *resp;
  62. char *user, *idstr = NULL;
  63. const char *submit = NULL;
  64. size_t idstr_sz = 1;
  65. struct cgpu_info *cgpu;
  66. struct thr_info *thr;
  67. json_t *json = NULL, *j2;
  68. json_error_t jerr;
  69. struct work *work;
  70. char *reply;
  71. long long hashes_done = -1;
  72. int ret;
  73. if (bytes_len(upbuf))
  74. {
  75. bytes_nullterminate(upbuf);
  76. json = JSON_LOADS((char*)bytes_buf(upbuf), &jerr);
  77. if (!json)
  78. {
  79. ret = getwork_error(conn, -32700, "JSON parse error", idstr, idstr_sz);
  80. goto out;
  81. }
  82. j2 = json_object_get(json, "id");
  83. if (j2)
  84. {
  85. idstr = json_dumps_ANY(j2, 0);
  86. idstr_sz = strlen(idstr);
  87. }
  88. if (strcmp("getwork", bfg_json_obj_string(json, "method", "getwork")))
  89. {
  90. ret = getwork_error(conn, -32601, "Only getwork supported", idstr, idstr_sz);
  91. goto out;
  92. }
  93. j2 = json_object_get(json, "params");
  94. submit = j2 ? __json_array_string(j2, 0) : NULL;
  95. }
  96. user = MHD_basic_auth_get_username_password(conn, NULL);
  97. if (!user)
  98. {
  99. resp = getwork_gen_error(-4096, "Please provide a username", idstr, idstr_sz, conn);
  100. ret = MHD_queue_basic_auth_fail_response(conn, PACKAGE, resp);
  101. goto out;
  102. }
  103. client = proxy_find_or_create_client(user);
  104. free(user);
  105. if (!client)
  106. {
  107. ret = getwork_error(conn, -32603, "Failed creating new cgpu", idstr, idstr_sz);
  108. goto out;
  109. }
  110. cgpu = client->cgpu;
  111. thr = cgpu->thr[0];
  112. {
  113. const char * const hashesdone = MHD_lookup_connection_value(conn, MHD_HEADER_KIND, "X-Hashes-Done");
  114. if (hashesdone)
  115. hashes_done = strtoll(hashesdone, NULL, 0);
  116. }
  117. if (submit)
  118. {
  119. unsigned char hdr[80];
  120. const char *rejreason;
  121. uint32_t nonce;
  122. // NOTE: expecting hex2bin to fail since we only parse 80 of the 128
  123. hex2bin(hdr, submit, 80);
  124. nonce = le32toh(*(uint32_t *)&hdr[76]);
  125. HASH_FIND(hh, client->work, hdr, 76, work);
  126. if (!work)
  127. {
  128. inc_hw_errors2(thr, NULL, &nonce);
  129. rejreason = "unknown-work";
  130. }
  131. else
  132. {
  133. if (!submit_nonce(thr, work, nonce))
  134. rejreason = "H-not-zero";
  135. else
  136. if (stale_work(work, true))
  137. rejreason = "stale";
  138. else
  139. rejreason = NULL;
  140. if (hashes_done == -1)
  141. hashes_done = (double)0x100000000 * work->nonce_diff;
  142. }
  143. reply = malloc(36 + idstr_sz);
  144. const size_t replysz =
  145. sprintf(reply, "{\"error\":null,\"result\":%s,\"id\":%s}",
  146. rejreason ? "false" : "true", idstr);
  147. resp = MHD_create_response_from_buffer(replysz, reply, MHD_RESPMEM_MUST_FREE);
  148. getwork_prepare_resp(resp, conn);
  149. MHD_add_response_header(resp, "X-Mining-Identifier", cgpu->proc_repr);
  150. if (rejreason)
  151. MHD_add_response_header(resp, "X-Reject-Reason", rejreason);
  152. ret = MHD_queue_response(conn, 200, resp);
  153. MHD_destroy_response(resp);
  154. goto out;
  155. }
  156. if (cgpu->deven == DEV_DISABLED)
  157. {
  158. resp = getwork_gen_error(-10, "Virtual device has been disabled", idstr, idstr_sz, conn);
  159. MHD_add_response_header(resp, "X-Mining-Identifier", cgpu->proc_repr);
  160. ret = MHD_queue_response(conn, 500, resp);
  161. MHD_destroy_response(resp);
  162. goto out;
  163. }
  164. {
  165. size_t replysz = 590 + idstr_sz;
  166. work = get_work(thr);
  167. work->nonce_diff = client->desired_share_pdiff;
  168. if (work->nonce_diff > work->work_difficulty)
  169. work->nonce_diff = work->work_difficulty;
  170. reply = malloc(replysz);
  171. uint8_t target[0x20];
  172. set_target_to_pdiff(target, work->nonce_diff);
  173. memcpy(reply, "{\"error\":null,\"result\":{\"target\":\"", 34);
  174. bin2hex(&reply[34], target, sizeof(target));
  175. memcpy(&reply[98], "\",\"data\":\"", 10);
  176. bin2hex(&reply[108], work->data, 128);
  177. memcpy(&reply[364], "\",\"midstate\":\"", 14);
  178. bin2hex(&reply[378], work->midstate, 32);
  179. memcpy(&reply[442], "\",\"hash1\":\"00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000\"},\"id\":", 147);
  180. memcpy(&reply[589], idstr ?: "0", idstr_sz);
  181. memcpy(&reply[589 + idstr_sz], "}", 1);
  182. if (opt_scrypt)
  183. {
  184. replysz += 21;
  185. reply = realloc(reply, replysz);
  186. memmove(&reply[443 + 21], &reply[443], replysz - (443 + 21));
  187. memcpy(&reply[443], ",\"algorithm\":\"scrypt\"", 21);
  188. }
  189. timer_set_now(&work->tv_work_start);
  190. HASH_ADD_KEYPTR(hh, client->work, work->data, 76, work);
  191. resp = MHD_create_response_from_buffer(replysz, reply, MHD_RESPMEM_MUST_FREE);
  192. getwork_prepare_resp(resp, conn);
  193. MHD_add_response_header(resp, "X-Mining-Identifier", cgpu->proc_repr);
  194. ret = MHD_queue_response(conn, 200, resp);
  195. MHD_destroy_response(resp);
  196. }
  197. out:
  198. if (hashes_done != -1)
  199. hashes_done2(thr, hashes_done, NULL);
  200. free(idstr);
  201. if (json)
  202. json_decref(json);
  203. return ret;
  204. }