driver-getwork.c 8.1 KB

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