driver-stratum.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. #ifndef WIN32
  11. #include <arpa/inet.h>
  12. #else
  13. #include <winsock2.h>
  14. #endif
  15. #include <stdbool.h>
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include <event2/buffer.h>
  19. #include <event2/bufferevent.h>
  20. #include <event2/event.h>
  21. #include <event2/listener.h>
  22. #include <jansson.h>
  23. #include "deviceapi.h"
  24. #include "driver-proxy.h"
  25. #include "miner.h"
  26. #include "util.h"
  27. #define MAX_CLIENTS 255
  28. static bool _ssm_xnonce1s[MAX_CLIENTS + 1] = { true };
  29. static uint8_t _ssm_client_octets;
  30. static uint8_t _ssm_client_xnonce2sz;
  31. static char *_ssm_notify;
  32. static int _ssm_notify_sz;
  33. static struct event *ev_notify;
  34. static notifier_t _ssm_update_notifier;
  35. struct stratumsrv_job {
  36. char *my_job_id;
  37. struct pool *pool;
  38. uint8_t work_restart_id;
  39. uint8_t n2size;
  40. struct timeval tv_prepared;
  41. struct stratum_work swork;
  42. char *nonce1;
  43. UT_hash_handle hh;
  44. };
  45. static struct stratumsrv_job *_ssm_jobs;
  46. static struct work _ssm_cur_job_work;
  47. static uint64_t _ssm_jobid;
  48. static struct event_base *_smm_evbase;
  49. static bool _smm_running;
  50. static struct evconnlistener *_smm_listener;
  51. struct stratumsrv_conn {
  52. struct bufferevent *bev;
  53. uint32_t xnonce1_le;
  54. struct timeval tv_hashes_done;
  55. bool hashes_done_ext;
  56. struct stratumsrv_conn *next;
  57. };
  58. static struct stratumsrv_conn *_ssm_connections;
  59. static
  60. void _ssm_gen_dummy_work(struct work *work, struct stratumsrv_job *ssj, const char * const extranonce2, uint32_t xnonce1)
  61. {
  62. uint8_t *p, *s;
  63. *work = (struct work){
  64. .pool = ssj->pool,
  65. .work_restart_id = ssj->work_restart_id,
  66. .tv_staged = ssj->tv_prepared,
  67. };
  68. bytes_resize(&work->nonce2, ssj->n2size);
  69. s = bytes_buf(&work->nonce2);
  70. p = &s[ssj->n2size - _ssm_client_xnonce2sz];
  71. if (extranonce2)
  72. hex2bin(p, extranonce2, _ssm_client_xnonce2sz);
  73. #ifndef __OPTIMIZE__
  74. else
  75. memset(p, '\0', _ssm_client_xnonce2sz);
  76. #endif
  77. p -= _ssm_client_octets;
  78. memcpy(p, &xnonce1, _ssm_client_octets);
  79. if (p != s)
  80. memset(s, '\xbb', p - s);
  81. gen_stratum_work2(work, &ssj->swork, ssj->nonce1);
  82. }
  83. static
  84. bool stratumsrv_update_notify_str(struct pool * const pool, bool clean)
  85. {
  86. cg_rlock(&pool->data_lock);
  87. struct stratumsrv_conn *conn;
  88. const struct stratum_work * const swork = &pool->swork;
  89. const int n2size = pool->n2size;
  90. char my_job_id[33];
  91. int i;
  92. struct stratumsrv_job *ssj;
  93. ssize_t n2pad = n2size - _ssm_client_octets - _ssm_client_xnonce2sz;
  94. if (n2pad < 0)
  95. return false;
  96. size_t coinb1in_lenx = swork->nonce2_offset * 2;
  97. size_t n2padx = n2pad * 2;
  98. size_t coinb1_lenx = coinb1in_lenx + n2padx;
  99. size_t coinb2_len = bytes_len(&swork->coinbase) - swork->nonce2_offset - n2size;
  100. size_t coinb2_lenx = coinb2_len * 2;
  101. sprintf(my_job_id, "%"PRIx64"-%"PRIx64, (uint64_t)time(NULL), _ssm_jobid++);
  102. size_t bufsz = 166 + strlen(my_job_id) + coinb1_lenx + coinb2_lenx + (swork->merkles * 67);
  103. char * const buf = malloc(bufsz);
  104. char *p = buf;
  105. char prevhash[65], coinb1[coinb1_lenx + 1], coinb2[coinb2_lenx], version[9], nbits[9], ntime[9];
  106. uint32_t ntime_n;
  107. bin2hex(prevhash, &swork->header1[4], 32);
  108. bin2hex(coinb1, bytes_buf(&swork->coinbase), swork->nonce2_offset);
  109. memset(&coinb1[coinb1in_lenx], 'B', n2padx);
  110. coinb1[coinb1_lenx] = '\0';
  111. bin2hex(coinb2, &bytes_buf(&swork->coinbase)[swork->nonce2_offset + n2size], coinb2_len);
  112. p += sprintf(p, "{\"params\":[\"%s\",\"%s\",\"%s\",\"%s\",[", my_job_id, prevhash, coinb1, coinb2);
  113. for (i = 0; i < swork->merkles; ++i)
  114. {
  115. if (i)
  116. *p++ = ',';
  117. *p++ = '"';
  118. bin2hex(p, &bytes_buf(&swork->merkle_bin)[i * 32], 32);
  119. p += 64;
  120. *p++ = '"';
  121. }
  122. bin2hex(version, swork->header1, 4);
  123. bin2hex(nbits, swork->diffbits, 4);
  124. ntime_n = htobe32(swork->ntime + timer_elapsed(&swork->tv_received, NULL));
  125. bin2hex(ntime, &ntime_n, 4);
  126. p += sprintf(p, "],\"%s\",\"%s\",\"%s\",%s],\"method\":\"mining.notify\",\"id\":null}\n", version, nbits, ntime, clean ? "true" : "false");
  127. ssj = malloc(sizeof(*ssj));
  128. *ssj = (struct stratumsrv_job){
  129. .my_job_id = strdup(my_job_id),
  130. .pool = pool,
  131. .work_restart_id = pool->work_restart_id,
  132. .n2size = n2size,
  133. .nonce1 = strdup(pool->nonce1),
  134. };
  135. timer_set_now(&ssj->tv_prepared);
  136. stratum_work_cpy(&ssj->swork, swork);
  137. cg_runlock(&pool->data_lock);
  138. ssj->swork.data_lock_p = NULL;
  139. HASH_ADD_KEYPTR(hh, _ssm_jobs, ssj->my_job_id, strlen(ssj->my_job_id), ssj);
  140. if (likely(_ssm_cur_job_work.pool))
  141. clean_work(&_ssm_cur_job_work);
  142. _ssm_gen_dummy_work(&_ssm_cur_job_work, ssj, NULL, 0);
  143. _ssm_notify_sz = p - buf;
  144. assert(_ssm_notify_sz <= bufsz);
  145. free(_ssm_notify);
  146. _ssm_notify = buf;
  147. LL_FOREACH(_ssm_connections, conn)
  148. {
  149. if (unlikely(!conn->xnonce1_le))
  150. continue;
  151. bufferevent_write(conn->bev, _ssm_notify, _ssm_notify_sz);
  152. }
  153. return true;
  154. }
  155. static
  156. void _ssj_free(struct stratumsrv_job * const ssj)
  157. {
  158. free(ssj->my_job_id);
  159. stratum_work_clean(&ssj->swork);
  160. free(ssj->nonce1);
  161. free(ssj);
  162. }
  163. static
  164. void stratumsrv_job_pruner()
  165. {
  166. struct stratumsrv_job *ssj, *tmp_ssj;
  167. struct timeval tv_now;
  168. timer_set_now(&tv_now);
  169. HASH_ITER(hh, _ssm_jobs, ssj, tmp_ssj)
  170. {
  171. if (timer_elapsed(&ssj->tv_prepared, &tv_now) <= opt_expiry)
  172. break;
  173. HASH_DEL(_ssm_jobs, ssj);
  174. applog(LOG_DEBUG, "SSM: Pruning job_id %s", ssj->my_job_id);
  175. _ssj_free(ssj);
  176. }
  177. }
  178. static void stratumsrv_client_close(struct stratumsrv_conn *);
  179. static
  180. void stratumsrv_conn_close_completion_cb(struct bufferevent *bev, void *p)
  181. {
  182. struct evbuffer * const output = bufferevent_get_output(bev);
  183. if (evbuffer_get_length(output))
  184. // Still have more data to write...
  185. return;
  186. stratumsrv_client_close(p);
  187. }
  188. static void stratumsrv_event(struct bufferevent *, short, void *);
  189. static
  190. void stratumsrv_boot(struct stratumsrv_conn * const conn, const char * const msg)
  191. {
  192. struct bufferevent * const bev = conn->bev;
  193. char buf[58 + strlen(msg)];
  194. int bufsz = sprintf(buf, "{\"params\":[\"%s\"],\"method\":\"client.show_message\",\"id\":null}\n", msg);
  195. bufferevent_write(bev, buf, bufsz);
  196. bufferevent_setcb(bev, NULL, stratumsrv_conn_close_completion_cb, stratumsrv_event, conn);
  197. }
  198. static
  199. void stratumsrv_boot_all_subscribed(const char * const msg)
  200. {
  201. struct stratumsrv_conn *conn, *tmp_conn;
  202. free(_ssm_notify);
  203. _ssm_notify = NULL;
  204. // Boot all connections
  205. LL_FOREACH_SAFE(_ssm_connections, conn, tmp_conn)
  206. {
  207. if (!conn->xnonce1_le)
  208. continue;
  209. stratumsrv_boot(conn, msg);
  210. }
  211. }
  212. static
  213. void _stratumsrv_update_notify(evutil_socket_t fd, short what, __maybe_unused void *p)
  214. {
  215. struct pool *pool = current_pool();
  216. bool clean;
  217. if (fd == _ssm_update_notifier[0])
  218. {
  219. evtimer_del(ev_notify);
  220. notifier_read(_ssm_update_notifier);
  221. applog(LOG_DEBUG, "SSM: Update triggered by notifier");
  222. }
  223. clean = _ssm_cur_job_work.pool ? stale_work(&_ssm_cur_job_work, true) : true;
  224. if (clean)
  225. {
  226. struct stratumsrv_job *ssj, *tmp;
  227. applog(LOG_DEBUG, "SSM: Current replacing job stale, pruning all jobs");
  228. HASH_ITER(hh, _ssm_jobs, ssj, tmp)
  229. {
  230. HASH_DEL(_ssm_jobs, ssj);
  231. _ssj_free(ssj);
  232. }
  233. }
  234. else
  235. stratumsrv_job_pruner();
  236. if (!pool->stratum_notify)
  237. {
  238. applog(LOG_WARNING, "SSM: Not using a stratum server upstream!");
  239. if (clean)
  240. stratumsrv_boot_all_subscribed("Current upstream pool does not have active stratum");
  241. goto out;
  242. }
  243. if (!stratumsrv_update_notify_str(pool, clean))
  244. {
  245. applog(LOG_WARNING, "SSM: Failed to subdivide upstream stratum notify!");
  246. if (clean)
  247. stratumsrv_boot_all_subscribed("Current upstream pool does not have active stratum");
  248. }
  249. out: ;
  250. struct timeval tv_scantime = {
  251. .tv_sec = opt_scantime,
  252. };
  253. evtimer_add(ev_notify, &tv_scantime);
  254. }
  255. static struct proxy_client *_stratumsrv_find_or_create_client(const char *);
  256. static
  257. struct proxy_client *(*stratumsrv_find_or_create_client)(const char *) = _stratumsrv_find_or_create_client;
  258. static
  259. struct proxy_client *_stratumsrv_find_or_create_client(const char *user)
  260. {
  261. struct proxy_client * const client = proxy_find_or_create_client(user);
  262. struct cgpu_info *cgpu;
  263. struct thr_info *thr;
  264. if (!client)
  265. return NULL;
  266. cgpu = client->cgpu;
  267. thr = cgpu->thr[0];
  268. memcpy(thr->work_restart_notifier, _ssm_update_notifier, sizeof(thr->work_restart_notifier));
  269. stratumsrv_find_or_create_client = proxy_find_or_create_client;
  270. return client;
  271. }
  272. static
  273. void _stratumsrv_failure(struct bufferevent * const bev, const char * const idstr, const int e, const char * const emsg)
  274. {
  275. if (!idstr)
  276. return;
  277. char buf[0x100];
  278. size_t bufsz = snprintf(buf, sizeof(buf), "{\"error\":[%d,\"%s\",null],\"id\":%s,\"result\":null}\n", e, emsg, idstr);
  279. bufferevent_write(bev, buf, bufsz);
  280. }
  281. #define return_stratumsrv_failure(e, emsg) do{ \
  282. _stratumsrv_failure(bev, idstr, e, emsg); \
  283. return; \
  284. }while(0)
  285. static
  286. void _stratumsrv_success(struct bufferevent * const bev, const char * const idstr)
  287. {
  288. if (!idstr)
  289. return;
  290. size_t bufsz = 36 + strlen(idstr);
  291. char buf[bufsz];
  292. bufsz = sprintf(buf, "{\"result\":true,\"id\":%s,\"error\":null}\n", idstr);
  293. bufferevent_write(bev, buf, bufsz);
  294. }
  295. static
  296. void stratumsrv_mining_subscribe(struct bufferevent *bev, json_t *params, const char *idstr, uint32_t *xnonce1_p)
  297. {
  298. char buf[90 + strlen(idstr) + (_ssm_client_octets * 2 * 2) + 0x10];
  299. char xnonce1x[(_ssm_client_octets * 2) + 1];
  300. int bufsz;
  301. if (!_ssm_notify)
  302. {
  303. evtimer_del(ev_notify);
  304. _stratumsrv_update_notify(-1, 0, NULL);
  305. if (!_ssm_notify)
  306. return_stratumsrv_failure(20, "No notify set (upstream not stratum?)");
  307. }
  308. if (!*xnonce1_p)
  309. {
  310. uint32_t xnonce1;
  311. for (xnonce1 = MAX_CLIENTS; _ssm_xnonce1s[xnonce1]; --xnonce1)
  312. if (!xnonce1)
  313. return_stratumsrv_failure(20, "Maximum clients already connected");
  314. _ssm_xnonce1s[xnonce1] = true;
  315. *xnonce1_p = htole32(xnonce1);
  316. }
  317. bin2hex(xnonce1x, xnonce1_p, _ssm_client_octets);
  318. bufsz = sprintf(buf, "{\"id\":%s,\"result\":[[[\"mining.set_difficulty\",\"x\"],[\"mining.notify\",\"%s\"]],\"%s\",%d],\"error\":null}\n", idstr, xnonce1x, xnonce1x, _ssm_client_xnonce2sz);
  319. bufferevent_write(bev, buf, bufsz);
  320. bufferevent_write(bev, "{\"params\":[0.9999847412109375],\"id\":null,\"method\":\"mining.set_difficulty\"}\n", 75);
  321. bufferevent_write(bev, _ssm_notify, _ssm_notify_sz);
  322. }
  323. static
  324. void stratumsrv_mining_authorize(struct bufferevent *bev, json_t *params, const char *idstr, uint32_t *xnonce1_p)
  325. {
  326. struct proxy_client * const client = stratumsrv_find_or_create_client(__json_array_string(params, 0));
  327. if (unlikely(!client))
  328. return_stratumsrv_failure(20, "Failed creating new cgpu");
  329. _stratumsrv_success(bev, idstr);
  330. }
  331. static
  332. void stratumsrv_mining_submit(struct bufferevent *bev, json_t *params, const char *idstr, struct stratumsrv_conn * const conn)
  333. {
  334. uint32_t * const xnonce1_p = &conn->xnonce1_le;
  335. struct work _work, *work;
  336. struct stratumsrv_job *ssj;
  337. struct proxy_client *client = stratumsrv_find_or_create_client(__json_array_string(params, 0));
  338. struct cgpu_info *cgpu;
  339. struct thr_info *thr;
  340. const char * const job_id = __json_array_string(params, 1);
  341. const char * const extranonce2 = __json_array_string(params, 2);
  342. const char * const ntime = __json_array_string(params, 3);
  343. const char * const nonce = __json_array_string(params, 4);
  344. uint32_t nonce_n;
  345. if (unlikely(!client))
  346. return_stratumsrv_failure(20, "Failed creating new cgpu");
  347. if (unlikely(!(job_id && extranonce2 && ntime && nonce)))
  348. return_stratumsrv_failure(20, "Couldn't understand parameters");
  349. if (unlikely(strlen(nonce) < 8))
  350. return_stratumsrv_failure(20, "nonce too short");
  351. if (unlikely(strlen(ntime) < 8))
  352. return_stratumsrv_failure(20, "ntime too short");
  353. if (unlikely(strlen(extranonce2) < _ssm_client_xnonce2sz * 2))
  354. return_stratumsrv_failure(20, "extranonce2 too short");
  355. cgpu = client->cgpu;
  356. thr = cgpu->thr[0];
  357. // Lookup job_id
  358. HASH_FIND_STR(_ssm_jobs, job_id, ssj);
  359. if (!ssj)
  360. return_stratumsrv_failure(21, "Job not found");
  361. // Generate dummy work
  362. work = &_work;
  363. _ssm_gen_dummy_work(work, ssj, extranonce2, *xnonce1_p);
  364. // Submit nonce
  365. hex2bin(&work->data[68], ntime, 4);
  366. hex2bin((void*)&nonce_n, nonce, 4);
  367. nonce_n = le32toh(nonce_n);
  368. if (!submit_nonce(thr, work, nonce_n))
  369. _stratumsrv_failure(bev, idstr, 23, "H-not-zero");
  370. else
  371. if (stale_work(work, true))
  372. _stratumsrv_failure(bev, idstr, 21, "stale");
  373. else
  374. _stratumsrv_success(bev, idstr);
  375. clean_work(work);
  376. if (!conn->hashes_done_ext)
  377. {
  378. struct timeval tv_now, tv_delta;
  379. timer_set_now(&tv_now);
  380. timersub(&tv_now, &conn->tv_hashes_done, &tv_delta);
  381. conn->tv_hashes_done = tv_now;
  382. hashes_done(thr, 0x100000000, &tv_delta, NULL);
  383. }
  384. }
  385. static
  386. void stratumsrv_mining_hashes_done(struct bufferevent * const bev, json_t * const params, const char * const idstr, struct stratumsrv_conn * const conn)
  387. {
  388. double f;
  389. struct timeval tv_delta;
  390. struct cgpu_info *cgpu;
  391. struct thr_info *thr;
  392. struct proxy_client * const client = stratumsrv_find_or_create_client(__json_array_string(params, 0));
  393. json_t *jduration = json_array_get(params, 1);
  394. json_t *jhashcount = json_array_get(params, 2);
  395. if (!(json_is_number(jduration) && json_is_number(jhashcount)))
  396. return_stratumsrv_failure(20, "mining.hashes_done(String username, Number duration-in-seconds, Number hashcount)");
  397. cgpu = client->cgpu;
  398. thr = cgpu->thr[0];
  399. f = json_number_value(jduration);
  400. tv_delta.tv_sec = f;
  401. tv_delta.tv_usec = (f - tv_delta.tv_sec) * 1e6;
  402. f = json_number_value(jhashcount);
  403. hashes_done(thr, f, &tv_delta, NULL);
  404. conn->hashes_done_ext = true;
  405. }
  406. static
  407. bool stratumsrv_process_line(struct bufferevent * const bev, const char * const ln, void * const p)
  408. {
  409. struct stratumsrv_conn *conn = p;
  410. json_error_t jerr;
  411. json_t *json, *params, *j2;
  412. const char *method;
  413. char *idstr;
  414. json = JSON_LOADS(ln, &jerr);
  415. if (!json)
  416. {
  417. applog(LOG_ERR, "SSM: JSON parse error: %s", ln);
  418. return false;
  419. }
  420. method = bfg_json_obj_string(json, "method", NULL);
  421. if (!method)
  422. {
  423. applog(LOG_ERR, "SSM: JSON missing method: %s", ln);
  424. errout:
  425. json_decref(json);
  426. return false;
  427. }
  428. params = json_object_get(json, "params");
  429. if (!params)
  430. {
  431. applog(LOG_ERR, "SSM: JSON missing params: %s", ln);
  432. goto errout;
  433. }
  434. applog(LOG_DEBUG, "SSM: RECV: %s", ln);
  435. j2 = json_object_get(json, "id");
  436. idstr = (j2 && !json_is_null(j2)) ? json_dumps_ANY(j2, 0) : NULL;
  437. if (!strcasecmp(method, "mining.submit"))
  438. stratumsrv_mining_submit(bev, params, idstr, conn);
  439. else
  440. if (!strcasecmp(method, "mining.hashes_done"))
  441. stratumsrv_mining_hashes_done(bev, params, idstr, conn);
  442. else
  443. if (!strcasecmp(method, "mining.authorize"))
  444. stratumsrv_mining_authorize(bev, params, idstr, &conn->xnonce1_le);
  445. else
  446. if (!strcasecmp(method, "mining.subscribe"))
  447. stratumsrv_mining_subscribe(bev, params, idstr, &conn->xnonce1_le);
  448. else
  449. _stratumsrv_failure(bev, idstr, -3, "Method not supported");
  450. free(idstr);
  451. json_decref(json);
  452. return true;
  453. }
  454. static
  455. void stratumsrv_client_close(struct stratumsrv_conn * const conn)
  456. {
  457. struct bufferevent * const bev = conn->bev;
  458. uint32_t xnonce1 = le32toh(conn->xnonce1_le);
  459. bufferevent_free(bev);
  460. LL_DELETE(_ssm_connections, conn);
  461. free(conn);
  462. _ssm_xnonce1s[xnonce1] = false;
  463. }
  464. static
  465. void stratumsrv_read(struct bufferevent *bev, void *p)
  466. {
  467. struct evbuffer *input = bufferevent_get_input(bev);
  468. char *ln;
  469. bool rv;
  470. while ( (ln = evbuffer_readln(input, NULL, EVBUFFER_EOL_ANY)) )
  471. {
  472. rv = stratumsrv_process_line(bev, ln, p);
  473. free(ln);
  474. if (unlikely(!rv))
  475. {
  476. stratumsrv_client_close(p);
  477. break;
  478. }
  479. }
  480. }
  481. static
  482. void stratumsrv_event(struct bufferevent *bev, short events, void *p)
  483. {
  484. if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR))
  485. {
  486. if (events & BEV_EVENT_ERROR)
  487. applog(LOG_ERR, "Error from bufferevent");
  488. if (events & BEV_EVENT_EOF)
  489. applog(LOG_DEBUG, "EOF from bufferevent");
  490. stratumsrv_client_close(p);
  491. }
  492. }
  493. static
  494. void stratumlistener(struct evconnlistener *listener, evutil_socket_t sock, struct sockaddr *addr, int len, void *p)
  495. {
  496. struct stratumsrv_conn *conn;
  497. struct event_base *evbase = evconnlistener_get_base(listener);
  498. struct bufferevent *bev = bufferevent_socket_new(evbase, sock, BEV_OPT_CLOSE_ON_FREE);
  499. conn = malloc(sizeof(*conn));
  500. *conn = (struct stratumsrv_conn){
  501. .bev = bev,
  502. };
  503. LL_PREPEND(_ssm_connections, conn);
  504. bufferevent_setcb(bev, stratumsrv_read, NULL, stratumsrv_event, conn);
  505. bufferevent_enable(bev, EV_READ | EV_WRITE);
  506. }
  507. void stratumsrv_start();
  508. void stratumsrv_change_port()
  509. {
  510. struct event_base * const evbase = _smm_evbase;
  511. if (_smm_listener)
  512. evconnlistener_free(_smm_listener);
  513. if (!_smm_running)
  514. {
  515. stratumsrv_start();
  516. return;
  517. }
  518. struct sockaddr_in sin = {
  519. .sin_family = AF_INET,
  520. .sin_addr.s_addr = INADDR_ANY,
  521. .sin_port = htons(stratumsrv_port),
  522. };
  523. _smm_listener = evconnlistener_new_bind(evbase, stratumlistener, NULL, (
  524. LEV_OPT_CLOSE_ON_FREE | LEV_OPT_CLOSE_ON_EXEC | LEV_OPT_REUSEABLE
  525. ), 0x10, (void*)&sin, sizeof(sin));
  526. // NOTE: libevent doesn't seem to implement LEV_OPT_CLOSE_ON_EXEC for Windows, so we must do this ourselves
  527. set_cloexec_socket(evconnlistener_get_fd(_smm_listener), true);
  528. }
  529. static
  530. void *stratumsrv_thread(__maybe_unused void *p)
  531. {
  532. pthread_detach(pthread_self());
  533. RenameThread("stratumsrv");
  534. for (uint64_t n = MAX_CLIENTS; n; n >>= 8)
  535. ++_ssm_client_octets;
  536. _ssm_client_xnonce2sz = 2;
  537. struct event_base *evbase = event_base_new();
  538. _smm_evbase = evbase;
  539. {
  540. ev_notify = evtimer_new(evbase, _stratumsrv_update_notify, NULL);
  541. _stratumsrv_update_notify(-1, 0, NULL);
  542. }
  543. {
  544. notifier_init(_ssm_update_notifier);
  545. struct event *ev_update_notifier = event_new(evbase, _ssm_update_notifier[0], EV_READ | EV_PERSIST, _stratumsrv_update_notify, NULL);
  546. event_add(ev_update_notifier, NULL);
  547. }
  548. stratumsrv_change_port();
  549. event_base_dispatch(evbase);
  550. return NULL;
  551. }
  552. void stratumsrv_start()
  553. {
  554. _smm_running = true;
  555. pthread_t pth;
  556. if (unlikely(pthread_create(&pth, NULL, stratumsrv_thread, NULL)))
  557. quit(1, "stratumsrv thread create failed");
  558. }