driver-getwork.c 6.8 KB

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