driver-stratum.c 17 KB

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