driver-getwork.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 "httpsrv.h"
  19. #include "miner.h"
  20. struct device_drv getwork_drv;
  21. struct getwork_client {
  22. char *username;
  23. struct cgpu_info *cgpu;
  24. struct work *work;
  25. struct timeval tv_hashes_done;
  26. UT_hash_handle hh;
  27. };
  28. static
  29. struct getwork_client *getwork_clients;
  30. static
  31. pthread_mutex_t getwork_clients_mutex;
  32. static
  33. void prune_worklog()
  34. {
  35. struct getwork_client *client, *tmp;
  36. struct work *work, *tmp2;
  37. mutex_lock(&getwork_clients_mutex);
  38. HASH_ITER(hh, getwork_clients, client, tmp)
  39. {
  40. HASH_ITER(hh, client->work, work, tmp2)
  41. {
  42. if (!stale_work(work, true))
  43. break;
  44. HASH_DEL(client->work, work);
  45. free_work(work);
  46. }
  47. }
  48. mutex_unlock(&getwork_clients_mutex);
  49. }
  50. static
  51. pthread_t prune_worklog_pth;
  52. static
  53. void *prune_worklog_thread(void *userdata)
  54. {
  55. struct cgpu_info *cgpu = userdata;
  56. pthread_detach(pthread_self());
  57. RenameThread("SGW_pruner");
  58. while (!cgpu->shutdown)
  59. {
  60. prune_worklog();
  61. sleep(60);
  62. }
  63. return NULL;
  64. }
  65. static
  66. void getwork_init()
  67. {
  68. mutex_init(&getwork_clients_mutex);
  69. }
  70. static
  71. void getwork_first_client()
  72. {
  73. pthread_create(&prune_worklog_pth, NULL, prune_worklog_thread, getwork_clients);
  74. }
  75. static
  76. void getwork_prepare_resp(struct MHD_Response *resp)
  77. {
  78. httpsrv_prepare_resp(resp);
  79. MHD_add_response_header(resp, MHD_HTTP_HEADER_CONTENT_TYPE, "application/json");
  80. MHD_add_response_header(resp, "X-Mining-Extensions", "hashesdone");
  81. }
  82. static
  83. struct MHD_Response *getwork_gen_error(int16_t errcode, const char *errmsg, const char *idstr, size_t idstr_sz)
  84. {
  85. size_t replysz = 0x40 + strlen(errmsg) + idstr_sz;
  86. char * const reply = malloc(replysz);
  87. replysz = snprintf(reply, replysz, "{\"result\":null,\"error\":{\"code\":%d,\"message\":\"%s\"},\"id\":%s}", errcode, errmsg, idstr ?: "0");
  88. struct MHD_Response * const resp = MHD_create_response_from_buffer(replysz, reply, MHD_RESPMEM_MUST_FREE);
  89. getwork_prepare_resp(resp);
  90. return resp;
  91. }
  92. static
  93. int getwork_error(struct MHD_Connection *conn, int16_t errcode, const char *errmsg, const char *idstr, size_t idstr_sz)
  94. {
  95. struct MHD_Response * const resp = getwork_gen_error(errcode, errmsg, idstr, idstr_sz);
  96. const int ret = MHD_queue_response(conn, 500, resp);
  97. MHD_destroy_response(resp);
  98. return ret;
  99. }
  100. int handle_getwork(struct MHD_Connection *conn, bytes_t *upbuf)
  101. {
  102. static bool _init = false, b;
  103. struct getwork_client *client;
  104. struct MHD_Response *resp;
  105. char *user, *idstr = NULL, *submit = NULL;
  106. size_t idstr_sz = 1;
  107. struct cgpu_info *cgpu;
  108. struct thr_info *thr;
  109. json_t *json = NULL, *j2;
  110. json_error_t jerr;
  111. struct work *work;
  112. char *reply;
  113. const char *hashesdone = NULL;
  114. int ret;
  115. if (unlikely(!_init))
  116. {
  117. _init = true;
  118. getwork_init();
  119. }
  120. if (bytes_len(upbuf))
  121. {
  122. bytes_nullterminate(upbuf);
  123. json = JSON_LOADS((char*)bytes_buf(upbuf), &jerr);
  124. if (!json)
  125. {
  126. ret = getwork_error(conn, -32700, "JSON parse error", idstr, idstr_sz);
  127. goto out;
  128. }
  129. j2 = json_object_get(json, "id");
  130. if (j2)
  131. {
  132. idstr = json_dumps_ANY(j2, 0);
  133. idstr_sz = strlen(idstr);
  134. }
  135. if (strcmp("getwork", bfg_json_obj_string(json, "method", "getwork")))
  136. {
  137. ret = getwork_error(conn, -32601, "Only getwork supported", idstr, idstr_sz);
  138. goto out;
  139. }
  140. j2 = json_object_get(json, "params");
  141. submit = j2 ? __json_array_string(j2, 0) : NULL;
  142. }
  143. user = MHD_basic_auth_get_username_password(conn, NULL);
  144. if (!user)
  145. {
  146. resp = getwork_gen_error(-4096, "Please provide a username", idstr, idstr_sz);
  147. ret = MHD_queue_basic_auth_fail_response(conn, PACKAGE, resp);
  148. goto out;
  149. }
  150. mutex_lock(&getwork_clients_mutex);
  151. HASH_FIND_STR(getwork_clients, user, client);
  152. if (!client)
  153. {
  154. cgpu = malloc(sizeof(*cgpu));
  155. client = malloc(sizeof(*client));
  156. *cgpu = (struct cgpu_info){
  157. .drv = &getwork_drv,
  158. .threads = 0,
  159. .device_data = client,
  160. .device_path = user,
  161. };
  162. if (unlikely(!create_new_cgpus(add_cgpu_live, cgpu)))
  163. {
  164. free(client);
  165. free(cgpu);
  166. ret = getwork_error(conn, -32603, "Failed creating new cgpu", idstr, idstr_sz);
  167. goto out;
  168. }
  169. *client = (struct getwork_client){
  170. .username = user,
  171. .cgpu = cgpu,
  172. };
  173. b = HASH_COUNT(getwork_clients);
  174. HASH_ADD_KEYPTR(hh, getwork_clients, client->username, strlen(user), client);
  175. mutex_unlock(&getwork_clients_mutex);
  176. if (!b)
  177. getwork_first_client();
  178. }
  179. else
  180. {
  181. mutex_unlock(&getwork_clients_mutex);
  182. free(user);
  183. cgpu = client->cgpu;
  184. }
  185. user = NULL;
  186. thr = cgpu->thr[0];
  187. hashesdone = MHD_lookup_connection_value(conn, MHD_HEADER_KIND, "X-Hashes-Done");
  188. if (submit)
  189. {
  190. unsigned char hdr[80];
  191. const char *rejreason;
  192. uint32_t nonce;
  193. // NOTE: expecting hex2bin to fail since we only parse 80 of the 128
  194. hex2bin(hdr, submit, 80);
  195. nonce = le32toh(*(uint32_t *)&hdr[76]);
  196. HASH_FIND(hh, client->work, hdr, 76, work);
  197. if (!work)
  198. {
  199. inc_hw_errors2(thr, NULL, &nonce);
  200. rejreason = "unknown-work";
  201. }
  202. else
  203. {
  204. if (!submit_nonce(thr, work, nonce))
  205. rejreason = "H-not-zero";
  206. else
  207. if (stale_work(work, true))
  208. rejreason = "stale";
  209. else
  210. rejreason = NULL;
  211. if (!hashesdone)
  212. hashesdone = "0x100000000";
  213. }
  214. reply = malloc(36 + idstr_sz);
  215. const size_t replysz =
  216. sprintf(reply, "{\"error\":null,\"result\":%s,\"id\":%s}",
  217. rejreason ? "false" : "true", idstr);
  218. resp = MHD_create_response_from_buffer(replysz, reply, MHD_RESPMEM_MUST_FREE);
  219. getwork_prepare_resp(resp);
  220. MHD_add_response_header(resp, "X-Mining-Identifier", cgpu->proc_repr);
  221. if (rejreason)
  222. MHD_add_response_header(resp, "X-Reject-Reason", rejreason);
  223. ret = MHD_queue_response(conn, 200, resp);
  224. MHD_destroy_response(resp);
  225. goto out;
  226. }
  227. if (cgpu->deven == DEV_DISABLED)
  228. {
  229. resp = getwork_gen_error(-10, "Virtual device has been disabled", idstr, idstr_sz);
  230. MHD_add_response_header(resp, "X-Mining-Identifier", cgpu->proc_repr);
  231. ret = MHD_queue_response(conn, 500, resp);
  232. MHD_destroy_response(resp);
  233. goto out;
  234. }
  235. {
  236. const size_t replysz = 590 + idstr_sz;
  237. work = get_work(thr);
  238. reply = malloc(replysz);
  239. memcpy(reply, "{\"error\":null,\"result\":{\"target\":\"ffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000\",\"data\":\"", 108);
  240. bin2hex(&reply[108], work->data, 128);
  241. memcpy(&reply[364], "\",\"midstate\":\"", 14);
  242. bin2hex(&reply[378], work->midstate, 32);
  243. memcpy(&reply[442], "\",\"hash1\":\"00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000\"},\"id\":", 147);
  244. memcpy(&reply[589], idstr ?: "0", idstr_sz);
  245. memcpy(&reply[589 + idstr_sz], "}", 1);
  246. timer_set_now(&work->tv_work_start);
  247. HASH_ADD_KEYPTR(hh, client->work, work->data, 76, work);
  248. resp = MHD_create_response_from_buffer(replysz, reply, MHD_RESPMEM_MUST_FREE);
  249. getwork_prepare_resp(resp);
  250. MHD_add_response_header(resp, "X-Mining-Identifier", cgpu->proc_repr);
  251. ret = MHD_queue_response(conn, 200, resp);
  252. MHD_destroy_response(resp);
  253. }
  254. out:
  255. if (hashesdone)
  256. {
  257. struct timeval tv_now, tv_delta;
  258. long long lld = strtoll(hashesdone, NULL, 0);
  259. timer_set_now(&tv_now);
  260. timersub(&tv_now, &client->tv_hashes_done, &tv_delta);
  261. client->tv_hashes_done = tv_now;
  262. hashes_done(thr, lld, &tv_delta, NULL);
  263. }
  264. free(user);
  265. free(idstr);
  266. if (json)
  267. json_decref(json);
  268. return ret;
  269. }
  270. #ifdef HAVE_CURSES
  271. static
  272. void getwork_wlogprint_status(struct cgpu_info *cgpu)
  273. {
  274. struct getwork_client *client = cgpu->device_data;
  275. wlogprint("Username: %s\n", client->username);
  276. }
  277. #endif
  278. struct device_drv getwork_drv = {
  279. .dname = "getwork",
  280. .name = "SGW",
  281. #ifdef HAVE_CURSES
  282. .proc_wlogprint_status = getwork_wlogprint_status,
  283. #endif
  284. };