driver-getwork.c 8.2 KB

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