driver-getwork.c 8.1 KB

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