driver-getwork.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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, *submit = NULL;
  113. size_t idstr_sz = 1;
  114. struct cgpu_info *cgpu;
  115. struct thr_info *thr;
  116. json_t *json = NULL, *j2;
  117. json_error_t jerr;
  118. struct work *work;
  119. char *reply;
  120. const char *hashesdone = NULL;
  121. int ret;
  122. if (unlikely(!_init))
  123. {
  124. _init = true;
  125. getwork_init();
  126. }
  127. if (bytes_len(upbuf))
  128. {
  129. bytes_nullterminate(upbuf);
  130. json = JSON_LOADS((char*)bytes_buf(upbuf), &jerr);
  131. if (!json)
  132. {
  133. ret = getwork_error(conn, -32700, "JSON parse error", idstr, idstr_sz);
  134. goto out;
  135. }
  136. j2 = json_object_get(json, "id");
  137. if (j2)
  138. {
  139. idstr = json_dumps_ANY(j2, 0);
  140. idstr_sz = strlen(idstr);
  141. }
  142. if (strcmp("getwork", bfg_json_obj_string(json, "method", "getwork")))
  143. {
  144. ret = getwork_error(conn, -32601, "Only getwork supported", idstr, idstr_sz);
  145. goto out;
  146. }
  147. j2 = json_object_get(json, "params");
  148. submit = j2 ? __json_array_string(j2, 0) : NULL;
  149. }
  150. user = MHD_basic_auth_get_username_password(conn, NULL);
  151. if (!user)
  152. {
  153. resp = getwork_gen_error(-4096, "Please provide a username", idstr, idstr_sz);
  154. ret = MHD_queue_basic_auth_fail_response(conn, PACKAGE, resp);
  155. goto out;
  156. }
  157. mutex_lock(&getwork_clients_mutex);
  158. HASH_FIND_STR(getwork_clients, user, client);
  159. if (!client)
  160. {
  161. cgpu = malloc(sizeof(*cgpu));
  162. client = malloc(sizeof(*client));
  163. *cgpu = (struct cgpu_info){
  164. .drv = &getwork_drv,
  165. .threads = 0,
  166. .device_data = client,
  167. .device_path = user,
  168. };
  169. if (unlikely(!create_new_cgpus(add_cgpu_live, cgpu)))
  170. {
  171. free(client);
  172. free(cgpu);
  173. ret = getwork_error(conn, -32603, "Failed creating new cgpu", idstr, idstr_sz);
  174. goto out;
  175. }
  176. *client = (struct getwork_client){
  177. .username = user,
  178. .cgpu = cgpu,
  179. };
  180. b = HASH_COUNT(getwork_clients);
  181. HASH_ADD_KEYPTR(hh, getwork_clients, client->username, strlen(user), client);
  182. mutex_unlock(&getwork_clients_mutex);
  183. if (!b)
  184. getwork_first_client(cgpu);
  185. }
  186. else
  187. {
  188. mutex_unlock(&getwork_clients_mutex);
  189. free(user);
  190. cgpu = client->cgpu;
  191. }
  192. user = NULL;
  193. thr = cgpu->thr[0];
  194. hashesdone = MHD_lookup_connection_value(conn, MHD_HEADER_KIND, "X-Hashes-Done");
  195. if (submit)
  196. {
  197. unsigned char hdr[80];
  198. const char *rejreason;
  199. uint32_t nonce;
  200. // NOTE: expecting hex2bin to fail since we only parse 80 of the 128
  201. hex2bin(hdr, submit, 80);
  202. nonce = le32toh(*(uint32_t *)&hdr[76]);
  203. HASH_FIND(hh, client->work, hdr, 76, work);
  204. if (!work)
  205. {
  206. inc_hw_errors2(thr, NULL, &nonce);
  207. rejreason = "unknown-work";
  208. }
  209. else
  210. {
  211. if (!submit_nonce(thr, work, nonce))
  212. rejreason = "H-not-zero";
  213. else
  214. if (stale_work(work, true))
  215. rejreason = "stale";
  216. else
  217. rejreason = NULL;
  218. if (!hashesdone)
  219. hashesdone = "0x100000000";
  220. }
  221. reply = malloc(36 + idstr_sz);
  222. const size_t replysz =
  223. sprintf(reply, "{\"error\":null,\"result\":%s,\"id\":%s}",
  224. rejreason ? "false" : "true", idstr);
  225. resp = MHD_create_response_from_buffer(replysz, reply, MHD_RESPMEM_MUST_FREE);
  226. getwork_prepare_resp(resp);
  227. MHD_add_response_header(resp, "X-Mining-Identifier", cgpu->proc_repr);
  228. if (rejreason)
  229. MHD_add_response_header(resp, "X-Reject-Reason", rejreason);
  230. ret = MHD_queue_response(conn, 200, resp);
  231. MHD_destroy_response(resp);
  232. goto out;
  233. }
  234. if (cgpu->deven == DEV_DISABLED)
  235. {
  236. resp = getwork_gen_error(-10, "Virtual device has been disabled", idstr, idstr_sz);
  237. MHD_add_response_header(resp, "X-Mining-Identifier", cgpu->proc_repr);
  238. ret = MHD_queue_response(conn, 500, resp);
  239. MHD_destroy_response(resp);
  240. goto out;
  241. }
  242. {
  243. const size_t replysz = 590 + idstr_sz;
  244. work = get_work(thr);
  245. reply = malloc(replysz);
  246. memcpy(reply, "{\"error\":null,\"result\":{\"target\":\"ffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000\",\"data\":\"", 108);
  247. bin2hex(&reply[108], work->data, 128);
  248. memcpy(&reply[364], "\",\"midstate\":\"", 14);
  249. bin2hex(&reply[378], work->midstate, 32);
  250. memcpy(&reply[442], "\",\"hash1\":\"00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000\"},\"id\":", 147);
  251. memcpy(&reply[589], idstr ?: "0", idstr_sz);
  252. memcpy(&reply[589 + idstr_sz], "}", 1);
  253. timer_set_now(&work->tv_work_start);
  254. HASH_ADD_KEYPTR(hh, client->work, work->data, 76, work);
  255. resp = MHD_create_response_from_buffer(replysz, reply, MHD_RESPMEM_MUST_FREE);
  256. getwork_prepare_resp(resp);
  257. MHD_add_response_header(resp, "X-Mining-Identifier", cgpu->proc_repr);
  258. ret = MHD_queue_response(conn, 200, resp);
  259. MHD_destroy_response(resp);
  260. }
  261. out:
  262. if (hashesdone)
  263. {
  264. struct timeval tv_now, tv_delta;
  265. long long lld = strtoll(hashesdone, NULL, 0);
  266. timer_set_now(&tv_now);
  267. timersub(&tv_now, &client->tv_hashes_done, &tv_delta);
  268. client->tv_hashes_done = tv_now;
  269. hashes_done(thr, lld, &tv_delta, NULL);
  270. }
  271. free(user);
  272. free(idstr);
  273. if (json)
  274. json_decref(json);
  275. return ret;
  276. }
  277. #ifdef HAVE_CURSES
  278. static
  279. void getwork_wlogprint_status(struct cgpu_info *cgpu)
  280. {
  281. struct getwork_client *client = cgpu->device_data;
  282. wlogprint("Username: %s\n", client->username);
  283. }
  284. #endif
  285. struct device_drv getwork_drv = {
  286. .dname = "getwork",
  287. .name = "SGW",
  288. #ifdef HAVE_CURSES
  289. .proc_wlogprint_status = getwork_wlogprint_status,
  290. #endif
  291. };