driver-getwork.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. // TODO: X-Hashes-Done?
  33. static
  34. void prune_worklog()
  35. {
  36. struct getwork_client *client, *tmp;
  37. struct work *work, *tmp2;
  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 (!stale_work(work, true))
  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. }
  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. int ret;
  114. if (unlikely(!_init))
  115. {
  116. _init = true;
  117. getwork_init();
  118. }
  119. if (bytes_len(upbuf))
  120. {
  121. bytes_nullterminate(upbuf);
  122. json = JSON_LOADS((char*)bytes_buf(upbuf), &jerr);
  123. if (!json)
  124. {
  125. ret = getwork_error(conn, -32700, "JSON parse error", idstr, idstr_sz);
  126. goto out;
  127. }
  128. j2 = json_object_get(json, "id");
  129. if (j2)
  130. {
  131. idstr = json_dumps_ANY(j2, 0);
  132. idstr_sz = strlen(idstr);
  133. }
  134. if (strcmp("getwork", bfg_json_obj_string(json, "method", "getwork")))
  135. {
  136. ret = getwork_error(conn, -32601, "Only getwork supported", idstr, idstr_sz);
  137. goto out;
  138. }
  139. j2 = json_object_get(json, "params");
  140. submit = j2 ? __json_array_string(j2, 0) : NULL;
  141. }
  142. user = MHD_basic_auth_get_username_password(conn, NULL);
  143. if (!user)
  144. {
  145. resp = getwork_gen_error(-4096, "Please provide a username", idstr, idstr_sz);
  146. ret = MHD_queue_basic_auth_fail_response(conn, PACKAGE, resp);
  147. goto out;
  148. }
  149. mutex_lock(&getwork_clients_mutex);
  150. HASH_FIND_STR(getwork_clients, user, client);
  151. if (!client)
  152. {
  153. cgpu = malloc(sizeof(*cgpu));
  154. client = malloc(sizeof(*client));
  155. *cgpu = (struct cgpu_info){
  156. .drv = &getwork_drv,
  157. .threads = 0,
  158. .device_data = client,
  159. .device_path = user,
  160. };
  161. if (unlikely(!create_new_cgpus(add_cgpu_live, cgpu)))
  162. {
  163. free(client);
  164. free(cgpu);
  165. ret = getwork_error(conn, -32603, "Failed creating new cgpu", idstr, idstr_sz);
  166. goto out;
  167. }
  168. *client = (struct getwork_client){
  169. .username = user,
  170. .cgpu = cgpu,
  171. };
  172. b = HASH_COUNT(getwork_clients);
  173. HASH_ADD_KEYPTR(hh, getwork_clients, client->username, strlen(user), client);
  174. mutex_unlock(&getwork_clients_mutex);
  175. if (!b)
  176. getwork_first_client();
  177. }
  178. else
  179. {
  180. mutex_unlock(&getwork_clients_mutex);
  181. free(user);
  182. cgpu = client->cgpu;
  183. }
  184. user = NULL;
  185. thr = cgpu->thr[0];
  186. if (submit)
  187. {
  188. unsigned char hdr[80];
  189. const char *rejreason;
  190. uint32_t nonce;
  191. struct timeval tv_now, tv_delta;
  192. // NOTE: expecting hex2bin to fail since we only parse 80 of the 128
  193. hex2bin(hdr, submit, 80);
  194. nonce = le32toh(*(uint32_t *)&hdr[76]);
  195. HASH_FIND(hh, client->work, hdr, 76, work);
  196. if (!work)
  197. {
  198. inc_hw_errors2(thr, NULL, &nonce);
  199. rejreason = "unknown-work";
  200. }
  201. else
  202. {
  203. if (!submit_nonce(thr, work, nonce))
  204. rejreason = "H-not-zero";
  205. else
  206. if (stale_work(work, true))
  207. rejreason = "stale";
  208. else
  209. rejreason = NULL;
  210. timer_set_now(&tv_now);
  211. timersub(&tv_now, &client->tv_hashes_done, &tv_delta);
  212. client->tv_hashes_done = tv_now;
  213. hashes_done(thr, 0x100000000, &tv_delta, NULL);
  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. free(user);
  257. free(idstr);
  258. if (json)
  259. json_decref(json);
  260. return ret;
  261. }
  262. #ifdef HAVE_CURSES
  263. static
  264. void getwork_wlogprint_status(struct cgpu_info *cgpu)
  265. {
  266. struct getwork_client *client = cgpu->device_data;
  267. wlogprint("Username: %s\n", client->username);
  268. }
  269. #endif
  270. struct device_drv getwork_drv = {
  271. .dname = "getwork",
  272. .name = "SGW",
  273. #ifdef HAVE_CURSES
  274. .proc_wlogprint_status = getwork_wlogprint_status,
  275. #endif
  276. };