driver-getwork.c 7.8 KB

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