driver-getwork.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. MHD_add_response_header(resp, MHD_HTTP_HEADER_CONTENT_TYPE, "application/json");
  83. const int ret = MHD_queue_response(conn, 500, resp);
  84. MHD_destroy_response(resp);
  85. return ret;
  86. }
  87. int handle_getwork(struct MHD_Connection *conn, bytes_t *upbuf)
  88. {
  89. static bool _init = false, b;
  90. struct getwork_client *client;
  91. struct MHD_Response *resp;
  92. char *user, *idstr = NULL, *submit = NULL;
  93. size_t idstr_sz = 1;
  94. struct cgpu_info *cgpu;
  95. struct thr_info *thr;
  96. json_t *json = NULL, *j2;
  97. json_error_t jerr;
  98. struct work *work;
  99. char *reply;
  100. int ret;
  101. if (unlikely(!_init))
  102. {
  103. _init = true;
  104. getwork_init();
  105. }
  106. user = MHD_basic_auth_get_username_password(conn, NULL);
  107. if (!user)
  108. {
  109. static const char fail[] = "Please provide a username\n";
  110. resp = MHD_create_response_from_buffer(sizeof(fail)-1, (char*)fail, MHD_RESPMEM_PERSISTENT);
  111. return MHD_queue_basic_auth_fail_response(conn, PACKAGE, resp);
  112. }
  113. if (bytes_len(upbuf))
  114. {
  115. bytes_nullterminate(upbuf);
  116. json = JSON_LOADS((char*)bytes_buf(upbuf), &jerr);
  117. if (!json)
  118. {
  119. ret = getwork_error(conn, -32700, "JSON parse error", idstr, idstr_sz);
  120. goto out;
  121. }
  122. j2 = json_object_get(json, "id");
  123. if (j2)
  124. {
  125. idstr = json_dumps_ANY(j2, 0);
  126. idstr_sz = strlen(idstr);
  127. }
  128. if (strcmp("getwork", bfg_json_obj_string(json, "method", "getwork")))
  129. {
  130. ret = getwork_error(conn, -32601, "Only getwork supported", idstr, idstr_sz);
  131. goto out;
  132. }
  133. j2 = json_object_get(json, "params");
  134. submit = j2 ? __json_array_string(j2, 0) : NULL;
  135. }
  136. mutex_lock(&getwork_clients_mutex);
  137. HASH_FIND_STR(getwork_clients, user, client);
  138. if (!client)
  139. {
  140. cgpu = malloc(sizeof(*cgpu));
  141. client = malloc(sizeof(*client));
  142. *cgpu = (struct cgpu_info){
  143. .drv = &getwork_drv,
  144. .threads = 0,
  145. .device_data = client,
  146. .device_path = user,
  147. };
  148. if (unlikely(!create_new_cgpus(add_cgpu_live, cgpu)))
  149. {
  150. free(client);
  151. free(cgpu);
  152. ret = getwork_error(conn, -32603, "Failed creating new cgpu", idstr, idstr_sz);
  153. goto out;
  154. }
  155. *client = (struct getwork_client){
  156. .username = user,
  157. .cgpu = cgpu,
  158. };
  159. b = HASH_COUNT(getwork_clients);
  160. HASH_ADD_KEYPTR(hh, getwork_clients, client->username, strlen(user), client);
  161. mutex_unlock(&getwork_clients_mutex);
  162. if (!b)
  163. getwork_first_client();
  164. }
  165. else
  166. {
  167. mutex_unlock(&getwork_clients_mutex);
  168. free(user);
  169. cgpu = client->cgpu;
  170. }
  171. user = NULL;
  172. thr = cgpu->thr[0];
  173. if (submit)
  174. {
  175. unsigned char hdr[80];
  176. const char *rejreason;
  177. uint32_t nonce;
  178. struct timeval tv_now, tv_delta;
  179. // NOTE: expecting hex2bin to fail since we only parse 80 of the 128
  180. hex2bin(hdr, submit, 80);
  181. nonce = le32toh(*(uint32_t *)&hdr[76]);
  182. HASH_FIND(hh, client->work, hdr, 76, work);
  183. if (!work)
  184. {
  185. inc_hw_errors2(thr, NULL, &nonce);
  186. rejreason = "unknown-work";
  187. }
  188. else
  189. {
  190. if (!submit_nonce(thr, work, nonce))
  191. rejreason = "H-not-zero";
  192. else
  193. if (stale_work(work, true))
  194. rejreason = "stale";
  195. else
  196. rejreason = NULL;
  197. timer_set_now(&tv_now);
  198. timersub(&tv_now, &client->tv_hashes_done, &tv_delta);
  199. client->tv_hashes_done = tv_now;
  200. hashes_done(thr, 0x100000000, &tv_delta, NULL);
  201. }
  202. reply = malloc(36 + idstr_sz);
  203. const size_t replysz =
  204. sprintf(reply, "{\"error\":null,\"result\":%s,\"id\":%s}",
  205. rejreason ? "false" : "true", idstr);
  206. resp = MHD_create_response_from_buffer(replysz, reply, MHD_RESPMEM_MUST_FREE);
  207. MHD_add_response_header(resp, MHD_HTTP_HEADER_CONTENT_TYPE, "application/json");
  208. if (rejreason)
  209. MHD_add_response_header(resp, "X-Reject-Reason", rejreason);
  210. ret = MHD_queue_response(conn, 200, resp);
  211. MHD_destroy_response(resp);
  212. goto out;
  213. }
  214. if (cgpu->deven == DEV_DISABLED)
  215. {
  216. ret = getwork_error(conn, -10, "Virtual device has been disabled", idstr, idstr_sz);
  217. goto out;
  218. }
  219. {
  220. const size_t replysz = 451 + idstr_sz;
  221. work = get_work(thr);
  222. reply = malloc(replysz);
  223. memcpy(reply, "{\"error\":null,\"result\":{\"target\":\"ffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000\",\"data\":\"", 108);
  224. bin2hex(&reply[108], work->data, 128);
  225. memcpy(&reply[364], "\",\"midstate\":\"", 14);
  226. bin2hex(&reply[378], work->midstate, 32);
  227. memcpy(&reply[442], "\"},\"id\":", 8);
  228. memcpy(&reply[450], idstr ?: "0", idstr_sz);
  229. memcpy(&reply[450 + idstr_sz], "}", 1);
  230. timer_set_now(&work->tv_work_start);
  231. HASH_ADD_KEYPTR(hh, client->work, work->data, 76, work);
  232. resp = MHD_create_response_from_buffer(replysz, reply, MHD_RESPMEM_MUST_FREE);
  233. MHD_add_response_header(resp, MHD_HTTP_HEADER_CONTENT_TYPE, "application/json");
  234. ret = MHD_queue_response(conn, 200, resp);
  235. MHD_destroy_response(resp);
  236. }
  237. out:
  238. free(user);
  239. free(idstr);
  240. if (json)
  241. json_decref(json);
  242. return ret;
  243. }
  244. #ifdef HAVE_CURSES
  245. static
  246. void getwork_wlogprint_status(struct cgpu_info *cgpu)
  247. {
  248. struct getwork_client *client = cgpu->device_data;
  249. wlogprint("Username: %s\n", client->username);
  250. }
  251. #endif
  252. struct device_drv getwork_drv = {
  253. .dname = "getwork",
  254. .name = "SGW",
  255. #ifdef HAVE_CURSES
  256. .proc_wlogprint_status = getwork_wlogprint_status,
  257. #endif
  258. };