driver-getwork.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #include <stdint.h>
  11. #include <sys/types.h>
  12. #include <sys/select.h>
  13. #include <sys/socket.h>
  14. #include <jansson.h>
  15. #include <microhttpd.h>
  16. #include <uthash.h>
  17. #include "deviceapi.h"
  18. #include "miner.h"
  19. struct device_drv getwork_drv;
  20. struct getwork_client {
  21. char *username;
  22. struct cgpu_info *cgpu;
  23. struct work *work;
  24. struct timeval tv_hashes_done;
  25. UT_hash_handle hh;
  26. };
  27. static
  28. struct getwork_client *getwork_clients;
  29. static
  30. pthread_mutex_t getwork_clients_mutex;
  31. // TODO: X-Hashes-Done?
  32. // TODO: TUI manage username display
  33. // TODO: block getworks if disabled?
  34. // TODO: maybe reject known-stale shares?
  35. static
  36. void prune_worklog()
  37. {
  38. struct getwork_client *client, *tmp;
  39. struct work *work, *tmp2;
  40. mutex_lock(&getwork_clients_mutex);
  41. HASH_ITER(hh, getwork_clients, client, tmp)
  42. {
  43. HASH_ITER(hh, client->work, work, tmp2)
  44. {
  45. if (!stale_work(work, true))
  46. break;
  47. HASH_DEL(client->work, work);
  48. free_work(work);
  49. }
  50. }
  51. mutex_unlock(&getwork_clients_mutex);
  52. }
  53. static
  54. pthread_t prune_worklog_pth;
  55. static
  56. void *prune_worklog_thread(void *userdata)
  57. {
  58. struct cgpu_info *cgpu = userdata;
  59. pthread_detach(pthread_self());
  60. RenameThread("SGW_pruner");
  61. while (!cgpu->shutdown)
  62. {
  63. prune_worklog();
  64. sleep(60);
  65. }
  66. return NULL;
  67. }
  68. static
  69. void getwork_init()
  70. {
  71. mutex_init(&getwork_clients_mutex);
  72. }
  73. static
  74. void getwork_first_client()
  75. {
  76. pthread_create(&prune_worklog_pth, NULL, prune_worklog_thread, getwork_clients);
  77. }
  78. static
  79. int getwork_error(struct MHD_Connection *conn, int16_t errcode, const char *errmsg, const char *idstr, size_t idstr_sz)
  80. {
  81. size_t replysz = 0x40 + strlen(errmsg) + idstr_sz;
  82. char * const reply = malloc(replysz);
  83. replysz = snprintf(reply, replysz, "{\"result\":null,\"error\":{\"code\":%d,\"message\":\"%s\"},\"id\":%s}", errcode, errmsg, idstr ?: "0");
  84. struct MHD_Response * const resp = MHD_create_response_from_buffer(replysz, reply, MHD_RESPMEM_MUST_FREE);
  85. const int ret = MHD_queue_response(conn, 500, resp);
  86. MHD_destroy_response(resp);
  87. return ret;
  88. }
  89. int handle_getwork(struct MHD_Connection *conn, bytes_t *upbuf)
  90. {
  91. static bool _init = false, b;
  92. struct getwork_client *client;
  93. struct MHD_Response *resp;
  94. char *user, *idstr = NULL, *submit = NULL;
  95. size_t idstr_sz = 1;
  96. struct cgpu_info *cgpu;
  97. struct thr_info *thr;
  98. json_t *json = NULL, *j2;
  99. json_error_t jerr;
  100. struct work *work;
  101. char *reply;
  102. int ret;
  103. if (unlikely(!_init))
  104. {
  105. _init = true;
  106. getwork_init();
  107. }
  108. user = MHD_basic_auth_get_username_password(conn, NULL);
  109. if (!user)
  110. {
  111. static const char fail[] = "Please provide a username\n";
  112. resp = MHD_create_response_from_buffer(sizeof(fail)-1, (char*)fail, MHD_RESPMEM_PERSISTENT);
  113. return MHD_queue_basic_auth_fail_response(conn, PACKAGE, resp);
  114. }
  115. if (bytes_len(upbuf))
  116. {
  117. bytes_nullterminate(upbuf);
  118. json = JSON_LOADS((char*)bytes_buf(upbuf), &jerr);
  119. if (!json)
  120. {
  121. ret = getwork_error(conn, -32700, "JSON parse error", idstr, idstr_sz);
  122. goto out;
  123. }
  124. j2 = json_object_get(json, "id");
  125. if (j2)
  126. {
  127. idstr = json_dumps_ANY(j2, 0);
  128. idstr_sz = strlen(idstr);
  129. }
  130. if (strcmp("getwork", bfg_json_obj_string(json, "method", "getwork")))
  131. {
  132. ret = getwork_error(conn, -32601, "Only getwork supported", idstr, idstr_sz);
  133. goto out;
  134. }
  135. j2 = json_object_get(json, "params");
  136. submit = j2 ? __json_array_string(j2, 0) : NULL;
  137. }
  138. mutex_lock(&getwork_clients_mutex);
  139. HASH_FIND_STR(getwork_clients, user, client);
  140. if (!client)
  141. {
  142. cgpu = malloc(sizeof(*cgpu));
  143. *cgpu = (struct cgpu_info){
  144. .drv = &getwork_drv,
  145. .threads = 0,
  146. };
  147. if (unlikely(!create_new_cgpus(add_cgpu_live, cgpu)))
  148. {
  149. free(cgpu);
  150. ret = getwork_error(conn, -32603, "Failed creating new cgpu", idstr, idstr_sz);
  151. goto out;
  152. }
  153. client = malloc(sizeof(*client));
  154. *client = (struct getwork_client){
  155. .username = user,
  156. .cgpu = cgpu,
  157. };
  158. b = HASH_COUNT(getwork_clients);
  159. HASH_ADD_KEYPTR(hh, getwork_clients, client->username, strlen(user), client);
  160. mutex_unlock(&getwork_clients_mutex);
  161. if (!b)
  162. getwork_first_client();
  163. }
  164. else
  165. {
  166. mutex_unlock(&getwork_clients_mutex);
  167. free(user);
  168. cgpu = client->cgpu;
  169. }
  170. user = NULL;
  171. thr = cgpu->thr[0];
  172. if (submit)
  173. {
  174. unsigned char hdr[80];
  175. const char *rejreason;
  176. uint32_t nonce;
  177. struct timeval tv_now, tv_delta;
  178. // NOTE: expecting hex2bin to fail since we only parse 80 of the 128
  179. hex2bin(hdr, submit, 80);
  180. nonce = le32toh(*(uint32_t *)&hdr[76]);
  181. HASH_FIND(hh, client->work, hdr, 76, work);
  182. if (!work)
  183. {
  184. inc_hw_errors2(thr, NULL, &nonce);
  185. rejreason = "unknown-work";
  186. }
  187. else
  188. {
  189. if (!submit_nonce(thr, work, nonce))
  190. rejreason = "H-not-zero";
  191. else
  192. rejreason = NULL;
  193. timer_set_now(&tv_now);
  194. timersub(&tv_now, &client->tv_hashes_done, &tv_delta);
  195. client->tv_hashes_done = tv_now;
  196. hashes_done(thr, 0x100000000, &tv_delta, NULL);
  197. }
  198. reply = malloc(36 + idstr_sz);
  199. const size_t replysz =
  200. sprintf(reply, "{\"error\":null,\"result\":%s,\"id\":%s}",
  201. rejreason ? "false" : "true", idstr);
  202. resp = MHD_create_response_from_buffer(replysz, reply, MHD_RESPMEM_MUST_FREE);
  203. if (rejreason)
  204. MHD_add_response_header(resp, "X-Reject-Reason", rejreason);
  205. ret = MHD_queue_response(conn, 200, resp);
  206. MHD_destroy_response(resp);
  207. goto out;
  208. }
  209. {
  210. const size_t replysz = 451 + idstr_sz;
  211. work = get_work(thr);
  212. reply = malloc(replysz);
  213. memcpy(reply, "{\"error\":null,\"result\":{\"target\":\"ffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000\",\"data\":\"", 108);
  214. bin2hex(&reply[108], work->data, 128);
  215. memcpy(&reply[364], "\",\"midstate\":\"", 14);
  216. bin2hex(&reply[378], work->midstate, 32);
  217. memcpy(&reply[442], "\"},\"id\":", 8);
  218. memcpy(&reply[450], idstr ?: "0", idstr_sz);
  219. memcpy(&reply[450 + idstr_sz], "}", 1);
  220. timer_set_now(&work->tv_work_start);
  221. HASH_ADD_KEYPTR(hh, client->work, work->data, 76, work);
  222. resp = MHD_create_response_from_buffer(replysz, reply, MHD_RESPMEM_MUST_FREE);
  223. ret = MHD_queue_response(conn, 200, resp);
  224. MHD_destroy_response(resp);
  225. }
  226. out:
  227. free(user);
  228. free(idstr);
  229. if (json)
  230. json_decref(json);
  231. return ret;
  232. }
  233. struct device_drv getwork_drv = {
  234. .dname = "getwork",
  235. .name = "SGW",
  236. // .get_api_stats = getwork_stats,
  237. };