driver-getwork.c 6.7 KB

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