driver-getwork.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #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)
  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. }
  34. static
  35. struct MHD_Response *getwork_gen_error(int16_t errcode, const char *errmsg, const char *idstr, size_t idstr_sz)
  36. {
  37. size_t replysz = 0x40 + strlen(errmsg) + idstr_sz;
  38. char * const reply = malloc(replysz);
  39. replysz = snprintf(reply, replysz, "{\"result\":null,\"error\":{\"code\":%d,\"message\":\"%s\"},\"id\":%s}", errcode, errmsg, idstr ?: "0");
  40. struct MHD_Response * const resp = MHD_create_response_from_buffer(replysz, reply, MHD_RESPMEM_MUST_FREE);
  41. getwork_prepare_resp(resp);
  42. return resp;
  43. }
  44. static
  45. int getwork_error(struct MHD_Connection *conn, int16_t errcode, const char *errmsg, const char *idstr, size_t idstr_sz)
  46. {
  47. struct MHD_Response * const resp = getwork_gen_error(errcode, errmsg, idstr, idstr_sz);
  48. const int ret = MHD_queue_response(conn, 500, resp);
  49. MHD_destroy_response(resp);
  50. return ret;
  51. }
  52. int handle_getwork(struct MHD_Connection *conn, bytes_t *upbuf)
  53. {
  54. struct proxy_client *client;
  55. struct MHD_Response *resp;
  56. char *user, *idstr = NULL;
  57. const char *submit = NULL;
  58. size_t idstr_sz = 1;
  59. struct cgpu_info *cgpu;
  60. struct thr_info *thr;
  61. json_t *json = NULL, *j2;
  62. json_error_t jerr;
  63. struct work *work;
  64. char *reply;
  65. const char *hashesdone = NULL;
  66. int ret;
  67. if (bytes_len(upbuf))
  68. {
  69. bytes_nullterminate(upbuf);
  70. json = JSON_LOADS((char*)bytes_buf(upbuf), &jerr);
  71. if (!json)
  72. {
  73. ret = getwork_error(conn, -32700, "JSON parse error", idstr, idstr_sz);
  74. goto out;
  75. }
  76. j2 = json_object_get(json, "id");
  77. if (j2)
  78. {
  79. idstr = json_dumps_ANY(j2, 0);
  80. idstr_sz = strlen(idstr);
  81. }
  82. if (strcmp("getwork", bfg_json_obj_string(json, "method", "getwork")))
  83. {
  84. ret = getwork_error(conn, -32601, "Only getwork supported", idstr, idstr_sz);
  85. goto out;
  86. }
  87. j2 = json_object_get(json, "params");
  88. submit = j2 ? __json_array_string(j2, 0) : NULL;
  89. }
  90. user = MHD_basic_auth_get_username_password(conn, NULL);
  91. if (!user)
  92. {
  93. resp = getwork_gen_error(-4096, "Please provide a username", idstr, idstr_sz);
  94. ret = MHD_queue_basic_auth_fail_response(conn, PACKAGE, resp);
  95. goto out;
  96. }
  97. client = proxy_find_or_create_client(user);
  98. free(user);
  99. if (!client)
  100. {
  101. ret = getwork_error(conn, -32603, "Failed creating new cgpu", idstr, idstr_sz);
  102. goto out;
  103. }
  104. cgpu = client->cgpu;
  105. thr = cgpu->thr[0];
  106. hashesdone = MHD_lookup_connection_value(conn, MHD_HEADER_KIND, "X-Hashes-Done");
  107. if (submit)
  108. {
  109. unsigned char hdr[80];
  110. const char *rejreason;
  111. uint32_t nonce;
  112. // NOTE: expecting hex2bin to fail since we only parse 80 of the 128
  113. hex2bin(hdr, submit, 80);
  114. nonce = le32toh(*(uint32_t *)&hdr[76]);
  115. HASH_FIND(hh, client->work, hdr, 76, work);
  116. if (!work)
  117. {
  118. inc_hw_errors2(thr, NULL, &nonce);
  119. rejreason = "unknown-work";
  120. }
  121. else
  122. {
  123. if (!submit_nonce(thr, work, nonce))
  124. rejreason = "H-not-zero";
  125. else
  126. if (stale_work(work, true))
  127. rejreason = "stale";
  128. else
  129. rejreason = NULL;
  130. if (!hashesdone)
  131. {
  132. if (opt_scrypt)
  133. hashesdone = "0x10000";
  134. else
  135. hashesdone = "0x100000000";
  136. }
  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);
  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);
  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. 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. if (opt_scrypt)
  171. {
  172. memset(&reply[90], 'f', 4);
  173. replysz += 21;
  174. reply = realloc(reply, replysz);
  175. memmove(&reply[443 + 21], &reply[443], replysz - (443 + 21));
  176. memcpy(&reply[443], ",\"algorithm\":\"scrypt\"", 21);
  177. }
  178. timer_set_now(&work->tv_work_start);
  179. HASH_ADD_KEYPTR(hh, client->work, work->data, 76, work);
  180. resp = MHD_create_response_from_buffer(replysz, reply, MHD_RESPMEM_MUST_FREE);
  181. getwork_prepare_resp(resp);
  182. MHD_add_response_header(resp, "X-Mining-Identifier", cgpu->proc_repr);
  183. ret = MHD_queue_response(conn, 200, resp);
  184. MHD_destroy_response(resp);
  185. }
  186. out:
  187. if (hashesdone)
  188. hashes_done2(thr, strtoll(hashesdone, NULL, 0), NULL);
  189. free(idstr);
  190. if (json)
  191. json_decref(json);
  192. return ret;
  193. }