driver-getwork.c 6.2 KB

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