driver-getwork.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. const char *hashesdone = NULL;
  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. hashesdone = MHD_lookup_connection_value(conn, MHD_HEADER_KIND, "X-Hashes-Done");
  113. if (submit)
  114. {
  115. unsigned char hdr[80];
  116. const char *rejreason;
  117. uint32_t nonce;
  118. // NOTE: expecting hex2bin to fail since we only parse 80 of the 128
  119. hex2bin(hdr, submit, 80);
  120. nonce = le32toh(*(uint32_t *)&hdr[76]);
  121. HASH_FIND(hh, client->work, hdr, 76, work);
  122. if (!work)
  123. {
  124. inc_hw_errors2(thr, NULL, &nonce);
  125. rejreason = "unknown-work";
  126. }
  127. else
  128. {
  129. if (!submit_nonce(thr, work, nonce))
  130. rejreason = "H-not-zero";
  131. else
  132. if (stale_work(work, true))
  133. rejreason = "stale";
  134. else
  135. rejreason = NULL;
  136. if (!hashesdone)
  137. {
  138. if (opt_scrypt)
  139. hashesdone = "0x10000";
  140. else
  141. hashesdone = "0x100000000";
  142. }
  143. }
  144. reply = malloc(36 + idstr_sz);
  145. const size_t replysz =
  146. sprintf(reply, "{\"error\":null,\"result\":%s,\"id\":%s}",
  147. rejreason ? "false" : "true", idstr);
  148. resp = MHD_create_response_from_buffer(replysz, reply, MHD_RESPMEM_MUST_FREE);
  149. getwork_prepare_resp(resp, conn);
  150. MHD_add_response_header(resp, "X-Mining-Identifier", cgpu->proc_repr);
  151. if (rejreason)
  152. MHD_add_response_header(resp, "X-Reject-Reason", rejreason);
  153. ret = MHD_queue_response(conn, 200, resp);
  154. MHD_destroy_response(resp);
  155. goto out;
  156. }
  157. if (cgpu->deven == DEV_DISABLED)
  158. {
  159. resp = getwork_gen_error(-10, "Virtual device has been disabled", idstr, idstr_sz, conn);
  160. MHD_add_response_header(resp, "X-Mining-Identifier", cgpu->proc_repr);
  161. ret = MHD_queue_response(conn, 500, resp);
  162. MHD_destroy_response(resp);
  163. goto out;
  164. }
  165. {
  166. size_t replysz = 590 + idstr_sz;
  167. work = get_work(thr);
  168. reply = malloc(replysz);
  169. memcpy(reply, "{\"error\":null,\"result\":{\"target\":\"ffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000\",\"data\":\"", 108);
  170. bin2hex(&reply[108], work->data, 128);
  171. memcpy(&reply[364], "\",\"midstate\":\"", 14);
  172. bin2hex(&reply[378], work->midstate, 32);
  173. memcpy(&reply[442], "\",\"hash1\":\"00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000\"},\"id\":", 147);
  174. memcpy(&reply[589], idstr ?: "0", idstr_sz);
  175. memcpy(&reply[589 + idstr_sz], "}", 1);
  176. if (opt_scrypt)
  177. {
  178. memset(&reply[90], 'f', 4);
  179. replysz += 21;
  180. reply = realloc(reply, replysz);
  181. memmove(&reply[443 + 21], &reply[443], replysz - (443 + 21));
  182. memcpy(&reply[443], ",\"algorithm\":\"scrypt\"", 21);
  183. }
  184. timer_set_now(&work->tv_work_start);
  185. HASH_ADD_KEYPTR(hh, client->work, work->data, 76, work);
  186. resp = MHD_create_response_from_buffer(replysz, reply, MHD_RESPMEM_MUST_FREE);
  187. getwork_prepare_resp(resp, conn);
  188. MHD_add_response_header(resp, "X-Mining-Identifier", cgpu->proc_repr);
  189. ret = MHD_queue_response(conn, 200, resp);
  190. MHD_destroy_response(resp);
  191. }
  192. out:
  193. if (hashesdone)
  194. hashes_done2(thr, strtoll(hashesdone, NULL, 0), NULL);
  195. free(idstr);
  196. if (json)
  197. json_decref(json);
  198. return ret;
  199. }