driver-getwork.c 5.7 KB

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