secure-streams-client.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. /*
  2. * lws-minimal-secure-streams-client
  3. *
  4. * Written in 2010-2020 by Andy Green <[email protected]>
  5. *
  6. * This file is made available under the Creative Commons CC0 1.0
  7. * Universal Public Domain Dedication.
  8. *
  9. *
  10. * This client does not perform any INET networking... instead it opens a unix
  11. * domain socket on a proxy that is listening for it, and that creates the
  12. * actual secure stream connection.
  13. *
  14. * We are able to use the usual secure streams api in the client process, with
  15. * payloads and connection state information proxied over the unix domain
  16. * socket and fulfilled in the proxy process.
  17. *
  18. * The public client helper pieces are built as part of lws
  19. */
  20. #include <private-lib-core.h>
  21. static void
  22. lws_sspc_sul_retry_cb(lws_sorted_usec_list_t *sul)
  23. {
  24. lws_sspc_handle_t *h = lws_container_of(sul, lws_sspc_handle_t, sul_retry);
  25. static struct lws_client_connect_info i;
  26. /*
  27. * We may have started up before the system proxy, so be prepared with
  28. * a sul to retry at 1Hz
  29. */
  30. memset(&i, 0, sizeof i);
  31. i.context = h->context;
  32. if (h->context->ss_proxy_port) { /* tcp */
  33. i.address = h->context->ss_proxy_address;
  34. i.port = h->context->ss_proxy_port;
  35. i.iface = h->context->ss_proxy_bind;
  36. } else {
  37. if (h->context->ss_proxy_bind)
  38. i.address = h->context->ss_proxy_bind;
  39. else
  40. i.address = "[email protected]";
  41. }
  42. i.host = i.address;
  43. i.origin = i.address;
  44. i.method = "RAW";
  45. i.protocol = lws_sspc_protocols[0].name;
  46. i.local_protocol_name = lws_sspc_protocols[0].name;
  47. i.path = "";
  48. i.pwsi = &h->cwsi;
  49. i.opaque_user_data = (void *)h;
  50. if (!lws_client_connect_via_info(&i)) {
  51. lws_sul_schedule(h->context, 0, &h->sul_retry,
  52. lws_sspc_sul_retry_cb, LWS_US_PER_SEC);
  53. return;
  54. }
  55. lwsl_notice("%s: sspc ss wsi %p\n", __func__, h->cwsi);
  56. }
  57. static int
  58. lws_sspc_serialize_metadata(lws_sspc_metadata_t *md, uint8_t *p, uint8_t *end)
  59. {
  60. int n, txc;
  61. if (md->name[0] == '\0') {
  62. lwsl_info("%s: sending tx credit update %d\n", __func__,
  63. md->tx_cr_adjust);
  64. p[0] = LWSSS_SER_TXPRE_TXCR_UPDATE;
  65. lws_ser_wu16be(&p[1], 4);
  66. lws_ser_wu32be(&p[3], md->tx_cr_adjust);
  67. n = 7;
  68. } else {
  69. lwsl_info("%s: sending metadata\n", __func__);
  70. p[0] = LWSSS_SER_TXPRE_METADATA;
  71. txc = strlen(md->name);
  72. n = txc + 1 + md->len;
  73. if (n > 0xffff)
  74. /* we can't serialize this metadata in 16b length */
  75. return -1;
  76. if (n > lws_ptr_diff(end, &p[4]))
  77. /* we don't have space for this metadata */
  78. return -1;
  79. lws_ser_wu16be(&p[1], n);
  80. p[3] = txc;
  81. memcpy(&p[4], md->name, txc);
  82. memcpy(&p[4 + txc], &md[1], md->len);
  83. n = 4 + txc + md->len;
  84. }
  85. lws_dll2_remove(&md->list);
  86. lws_free(md);
  87. return n;
  88. }
  89. static int
  90. callback_sspc_client(struct lws *wsi, enum lws_callback_reasons reason,
  91. void *user, void *in, size_t len)
  92. {
  93. lws_sspc_handle_t *h = (lws_sspc_handle_t *)lws_get_opaque_user_data(wsi);
  94. uint8_t s[32], pkt[LWS_PRE + 2048], *p = pkt + LWS_PRE,
  95. *end = p + sizeof(pkt) - LWS_PRE;
  96. void *m = (void *)((uint8_t *)&h[1]);
  97. const uint8_t *cp;
  98. lws_usec_t us;
  99. int flags, n;
  100. switch (reason) {
  101. case LWS_CALLBACK_PROTOCOL_INIT:
  102. break;
  103. case LWS_CALLBACK_PROTOCOL_DESTROY:
  104. break;
  105. case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
  106. lwsl_warn("%s: CONNECTION_ERROR\n", __func__);
  107. lws_set_opaque_user_data(wsi, NULL);
  108. h->cwsi = NULL;
  109. lws_sul_schedule(h->context, 0, &h->sul_retry,
  110. lws_sspc_sul_retry_cb, LWS_US_PER_SEC);
  111. break;
  112. case LWS_CALLBACK_RAW_CONNECTED:
  113. if (!h)
  114. return -1;
  115. lwsl_info("%s: CONNECTED (%s)\n", __func__, h->ssi.streamtype);
  116. h->state = LPCSCLI_SENDING_INITIAL_TX;
  117. h->dsh = lws_dsh_create(NULL, (LWS_PRE + LWS_SS_MTU) * 160, 1);
  118. if (!h->dsh)
  119. return -1;
  120. lws_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND, 3);
  121. lws_callback_on_writable(wsi);
  122. break;
  123. case LWS_CALLBACK_RAW_CLOSE:
  124. /*
  125. * our ss proxy Unix Domain socket has closed...
  126. */
  127. lwsl_notice("%s: LWS_CALLBACK_RAW_CLOSE: proxy conn down\n",
  128. __func__);
  129. if (h) {
  130. h->cwsi = NULL;
  131. /*
  132. * schedule a reconnect in 1s
  133. */
  134. lws_sul_schedule(h->context, 0, &h->sul_retry,
  135. lws_sspc_sul_retry_cb, LWS_US_PER_SEC);
  136. }
  137. break;
  138. case LWS_CALLBACK_RAW_RX:
  139. /*
  140. * ie, the proxy has sent us something
  141. */
  142. lwsl_info("%s: RAW_RX: rx %d\n", __func__, (int)len);
  143. if (!h || !h->cwsi) {
  144. lwsl_err("%s: rx with bad conn state\n", __func__);
  145. return -1;
  146. }
  147. n = lws_ss_deserialize_parse(&h->parser, lws_get_context(wsi),
  148. h->dsh, in, len, &h->state, h,
  149. (lws_ss_handle_t **)m, &h->ssi, 1);
  150. switch (n) {
  151. case LWSSSSRET_OK:
  152. break;
  153. case LWSSSSRET_DISCONNECT_ME:
  154. return -1;
  155. case LWSSSSRET_DESTROY_ME:
  156. lws_sspc_destroy(&h);
  157. return -1;
  158. }
  159. if (h->state == LPCSCLI_LOCAL_CONNECTED ||
  160. h->state == LPCSCLI_ONWARD_CONNECT)
  161. lws_set_timeout(wsi, 0, 0);
  162. break;
  163. case LWS_CALLBACK_RAW_WRITEABLE:
  164. /*
  165. * We can transmit something to the proxy...
  166. */
  167. if (!h)
  168. break;
  169. lwsl_debug("%s: WRITEABLE %p: (%s) state %d\n", __func__, wsi,
  170. h->ssi.streamtype, h->state);
  171. /*
  172. * Management of ss timeout can happen any time and doesn't
  173. * depend on wsi existence or state
  174. */
  175. n = 0;
  176. cp = s;
  177. if (h->pending_timeout_update) {
  178. s[0] = LWSSS_SER_TXPRE_TIMEOUT_UPDATE;
  179. s[1] = 0;
  180. s[2] = 4;
  181. /*
  182. * 0: use policy timeout value
  183. * 0xffffffff: cancel the timeout
  184. */
  185. lws_ser_wu32be(&s[3], h->timeout_ms);
  186. /* in case anything else to write */
  187. lws_callback_on_writable(h->cwsi);
  188. h->pending_timeout_update = 0;
  189. n = 7;
  190. goto do_write;
  191. }
  192. s[1] = 0;
  193. /*
  194. * This is the state of the link that connects us to the onward
  195. * proxy
  196. */
  197. switch (h->state) {
  198. case LPCSCLI_SENDING_INITIAL_TX:
  199. /*
  200. * We are negotating the opening of a particular
  201. * streamtype
  202. */
  203. n = strlen(h->ssi.streamtype) + 4;
  204. s[0] = LWSSS_SER_TXPRE_STREAMTYPE;
  205. lws_ser_wu16be(&s[1], n);
  206. lws_ser_wu32be(&s[3], h->txc.peer_tx_cr_est);
  207. //h->txcr_out = txc;
  208. lws_strncpy((char *)&s[7], h->ssi.streamtype, sizeof(s) - 7);
  209. n += 3;
  210. h->state = LPCSCLI_WAITING_CREATE_RESULT;
  211. break;
  212. case LPCSCLI_LOCAL_CONNECTED:
  213. // lwsl_notice("%s: LPCSCLI_LOCAL_CONNECTED\n", __func__);
  214. /*
  215. * Do we need to prioritize sending any metadata
  216. * changes?
  217. */
  218. if (h->metadata_owner.count) {
  219. lws_sspc_metadata_t *md = lws_container_of(
  220. lws_dll2_get_tail(&h->metadata_owner),
  221. lws_sspc_metadata_t, list);
  222. cp = p;
  223. n = lws_sspc_serialize_metadata(md, p, end);
  224. if (n < 0)
  225. goto metadata_hangup;
  226. lwsl_debug("%s: (local_conn) metadata\n", __func__);
  227. goto req_write_and_issue;
  228. }
  229. if (h->pending_writeable_len) {
  230. lwsl_debug("%s: (local_conn) PAYLOAD_LENGTH_HINT %u\n",
  231. __func__, (unsigned int)h->writeable_len);
  232. s[0] = LWSSS_SER_TXPRE_PAYLOAD_LENGTH_HINT;
  233. lws_ser_wu16be(&s[1], 4);
  234. lws_ser_wu32be(&s[3], h->writeable_len);
  235. h->pending_writeable_len = 0;
  236. n = 7;
  237. goto req_write_and_issue;
  238. }
  239. if (h->conn_req_state >= LWSSSPC_ONW_ONGOING) {
  240. lwsl_info("%s: conn_req_state %d\n", __func__,
  241. h->conn_req_state);
  242. break;
  243. }
  244. lwsl_info("%s: (local_conn) onward connect\n", __func__);
  245. h->conn_req_state = LWSSSPC_ONW_ONGOING;
  246. s[0] = LWSSS_SER_TXPRE_ONWARD_CONNECT;
  247. s[1] = 0;
  248. s[2] = 0;
  249. n = 3;
  250. break;
  251. case LPCSCLI_OPERATIONAL:
  252. lwsl_notice("%s: LPCSCLI_OPERATIONAL\n", __func__);
  253. /*
  254. *
  255. * - Do we need to prioritize sending any metadata
  256. * changes? (includes txcr updates)
  257. *
  258. * - Do we need to forward a hint about the payload
  259. * length?
  260. */
  261. if (h->metadata_owner.count) {
  262. lws_sspc_metadata_t *md = lws_container_of(
  263. lws_dll2_get_tail(&h->metadata_owner),
  264. lws_sspc_metadata_t, list);
  265. cp = p;
  266. n = lws_sspc_serialize_metadata(md, p, end);
  267. if (n < 0)
  268. goto metadata_hangup;
  269. goto req_write_and_issue;
  270. }
  271. if (h->pending_writeable_len) {
  272. lwsl_info("%s: PAYLOAD_LENGTH_HINT %u\n",
  273. __func__, (unsigned int)h->writeable_len);
  274. s[0] = LWSSS_SER_TXPRE_PAYLOAD_LENGTH_HINT;
  275. lws_ser_wu16be(&s[1], 4);
  276. lws_ser_wu32be(&s[3], h->writeable_len);
  277. h->pending_writeable_len = 0;
  278. n = 7;
  279. goto req_write_and_issue;
  280. }
  281. /* we can't write anything if we don't have credit */
  282. if (!h->ignore_txc && h->txc.tx_cr <= 0) {
  283. lwsl_info("%s: WRITEABLE / OPERATIONAL:"
  284. " lack credit (%d)\n", __func__,
  285. h->txc.tx_cr);
  286. // break;
  287. }
  288. len = sizeof(pkt) - LWS_PRE - 19;
  289. flags = 0;
  290. n = h->ssi.tx(m, h->ord++, pkt + LWS_PRE + 19, &len,
  291. &flags);
  292. if (n == LWSSSSRET_TX_DONT_SEND) {
  293. n = 0;
  294. break;
  295. }
  296. h->txc.tx_cr -= len;
  297. cp = p;
  298. n = len + 19;
  299. us = lws_now_usecs();
  300. p[0] = LWSSS_SER_TXPRE_TX_PAYLOAD;
  301. lws_ser_wu16be(&p[1], len + 19 - 3);
  302. lws_ser_wu32be(&p[3], flags);
  303. /* time spent here waiting to send this */
  304. lws_ser_wu32be(&p[7], us - h->us_earliest_write_req);
  305. /* ust that the client write happened */
  306. lws_ser_wu64be(&p[11], us);
  307. h->us_earliest_write_req = 0;
  308. if (flags & LWSSS_FLAG_EOM)
  309. if (h->rsidx + 1 < (int)LWS_ARRAY_SIZE(h->rideshare_ofs) &&
  310. h->rideshare_ofs[h->rsidx + 1])
  311. h->rsidx++;
  312. break;
  313. default:
  314. break;
  315. }
  316. do_write_nz:
  317. if (!n)
  318. break;
  319. do_write:
  320. n = lws_write(wsi, (uint8_t *)cp, n, LWS_WRITE_RAW);
  321. if (n < 0) {
  322. lwsl_notice("%s: WRITEABLE: %d\n", __func__, n);
  323. goto hangup;
  324. }
  325. break;
  326. default:
  327. break;
  328. }
  329. return lws_callback_http_dummy(wsi, reason, user, in, len);
  330. metadata_hangup:
  331. lwsl_err("%s: metadata too large\n", __func__);
  332. hangup:
  333. lwsl_warn("hangup\n");
  334. /* hang up on him */
  335. return -1;
  336. req_write_and_issue:
  337. /* in case anything else to write */
  338. lws_callback_on_writable(h->cwsi);
  339. goto do_write_nz;
  340. }
  341. const struct lws_protocols lws_sspc_protocols[] = {
  342. {
  343. "ssproxy-protocol",
  344. callback_sspc_client,
  345. 0,
  346. 2048, 2048, NULL, 0
  347. },
  348. { NULL, NULL, 0, 0, 0, NULL, 0 }
  349. };
  350. int
  351. lws_sspc_create(struct lws_context *context, int tsi, const lws_ss_info_t *ssi,
  352. void *opaque_user_data, lws_sspc_handle_t **ppss,
  353. struct lws_sequencer *seq_owner, const char **ppayload_fmt)
  354. {
  355. lws_sspc_handle_t *h;
  356. uint8_t *ua;
  357. char *p;
  358. lwsl_notice("%s: streamtype %s\n", __func__, ssi->streamtype);
  359. /* allocate the handle (including ssi), the user alloc,
  360. * and the streamname */
  361. h = malloc(sizeof(lws_sspc_handle_t) + ssi->user_alloc +
  362. strlen(ssi->streamtype) + 1);
  363. if (!h)
  364. return 1;
  365. memset(h, 0, sizeof(*h));
  366. memcpy(&h->ssi, ssi, sizeof(*ssi));
  367. ua = (uint8_t *)&h[1];
  368. memset(ua, 0, ssi->user_alloc);
  369. p = (char *)ua + ssi->user_alloc;
  370. memcpy(p, ssi->streamtype, strlen(ssi->streamtype) + 1);
  371. h->ssi.streamtype = (const char *)p;
  372. h->context = context;
  373. if (!ssi->manual_initial_tx_credit)
  374. h->txc.peer_tx_cr_est = 500000000;
  375. else
  376. h->txc.peer_tx_cr_est = ssi->manual_initial_tx_credit;
  377. if (!strcmp(ssi->streamtype, "_lws_smd"))
  378. h->ignore_txc = 1;
  379. lws_dll2_add_head(&h->client_list, &context->pt[tsi].ss_client_owner);
  380. /* fill in the things the real api does for the caller */
  381. *((void **)(ua + ssi->opaque_user_data_offset)) = opaque_user_data;
  382. *((void **)(ua + ssi->handle_offset)) = h;
  383. if (ppss)
  384. *ppss = h;
  385. /* try the actual connect */
  386. lws_sspc_sul_retry_cb(&h->sul_retry);
  387. return 0;
  388. }
  389. /* used on context destroy when iterating listed lws_ss on a pt */
  390. int
  391. lws_sspc_destroy_dll(struct lws_dll2 *d, void *user)
  392. {
  393. lws_sspc_handle_t *h = lws_container_of(d, lws_sspc_handle_t, client_list);
  394. lws_sspc_destroy(&h);
  395. return 0;
  396. }
  397. void
  398. lws_sspc_destroy(lws_sspc_handle_t **ph)
  399. {
  400. lws_sspc_handle_t *h;
  401. void *m;
  402. lwsl_debug("%s\n", __func__);
  403. if (!*ph)
  404. return;
  405. h = *ph;
  406. m = (void *)((uint8_t *)&h[1]);
  407. if (h->destroying)
  408. return;
  409. h->destroying = 1;
  410. lws_sul_cancel(&h->sul_retry);
  411. lws_dll2_remove(&h->client_list);
  412. if (h->dsh)
  413. lws_dsh_destroy(&h->dsh);
  414. if (h->cwsi) {
  415. struct lws *wsi = h->cwsi;
  416. h->cwsi = NULL;
  417. if (wsi)
  418. lws_set_timeout(wsi, 1, LWS_TO_KILL_SYNC);
  419. }
  420. /* clean out any pending metadata changes that didn't make it */
  421. lws_start_foreach_dll_safe(struct lws_dll2 *, d, d1,
  422. lws_dll2_get_head(&(*ph)->metadata_owner)) {
  423. lws_sspc_metadata_t *md =
  424. lws_container_of(d, lws_sspc_metadata_t, list);
  425. lws_dll2_remove(&md->list);
  426. lws_free(md);
  427. } lws_end_foreach_dll_safe(d, d1);
  428. h->ssi.state(m, NULL, LWSSSCS_DESTROYING, 0);
  429. *ph = NULL;
  430. free(h);
  431. }
  432. lws_ss_state_return_t
  433. lws_sspc_request_tx(lws_sspc_handle_t *h)
  434. {
  435. if (!h || !h->cwsi)
  436. return LWSSSSRET_OK;
  437. if (!h->us_earliest_write_req)
  438. h->us_earliest_write_req = lws_now_usecs();
  439. if (h->state == LPCSCLI_LOCAL_CONNECTED &&
  440. h->conn_req_state == LWSSSPC_ONW_NONE)
  441. h->conn_req_state = LWSSSPC_ONW_REQ;
  442. lws_callback_on_writable(h->cwsi);
  443. return LWSSSSRET_OK;
  444. }
  445. /*
  446. * Currently we fulfil the writeable part locally by just enabling POLLOUT on
  447. * the UDS link, without serialization footprint, which is reasonable as far as
  448. * it goes.
  449. *
  450. * But for the ..._len() variant, the expected payload length hint we are being
  451. * told is something that must be serialized to the onward peer, since either
  452. * that guy or someone upstream of him is the guy who will compose the framing
  453. * with it that actually goes out.
  454. *
  455. * This information is needed at the upstream guy before we have sent any
  456. * payload, eg, for http POST, he has to prepare the content-length in the
  457. * headers, before any payload. So we have to issue a serialization of the
  458. * length at this point.
  459. */
  460. lws_ss_state_return_t
  461. lws_sspc_request_tx_len(lws_sspc_handle_t *h, unsigned long len)
  462. {
  463. /*
  464. * for client conns, they cannot even complete creation of the handle
  465. * without the onwared connection to the proxy, it's not legal to start
  466. * using it until it's operation and has the onward connection (and has
  467. * called CREATED state)
  468. */
  469. if (!h)
  470. return LWSSSSRET_OK;
  471. lwsl_notice("%s: setting h %p writeable_len %u\n", __func__, h,
  472. (unsigned int)len);
  473. h->writeable_len = len;
  474. h->pending_writeable_len = 1;
  475. if (!h->us_earliest_write_req)
  476. h->us_earliest_write_req = lws_now_usecs();
  477. if (h->state == LPCSCLI_LOCAL_CONNECTED &&
  478. h->conn_req_state == LWSSSPC_ONW_NONE)
  479. h->conn_req_state = LWSSSPC_ONW_REQ;
  480. /*
  481. * We're going to use this up with serializing h->writeable_len... that
  482. * will request again.
  483. */
  484. lws_callback_on_writable(h->cwsi);
  485. return LWSSSSRET_OK;
  486. }
  487. int
  488. lws_sspc_client_connect(lws_sspc_handle_t *h)
  489. {
  490. if (!h || h->state == LPCSCLI_OPERATIONAL)
  491. return 0;
  492. assert(h->state == LPCSCLI_LOCAL_CONNECTED);
  493. if (h->state == LPCSCLI_LOCAL_CONNECTED &&
  494. h->conn_req_state == LWSSSPC_ONW_NONE)
  495. h->conn_req_state = LWSSSPC_ONW_REQ;
  496. if (h->cwsi)
  497. lws_callback_on_writable(h->cwsi);
  498. return 0;
  499. }
  500. struct lws_context *
  501. lws_sspc_get_context(struct lws_sspc_handle *h)
  502. {
  503. return h->context;
  504. }
  505. const char *
  506. lws_sspc_rideshare(struct lws_sspc_handle *h)
  507. {
  508. /*
  509. * ...the serialized RX rideshare name if any...
  510. */
  511. if (h->parser.rideshare[0]) {
  512. lwsl_info("%s: parser %s\n", __func__, h->parser.rideshare);
  513. return h->parser.rideshare;
  514. }
  515. /*
  516. * The tx rideshare index
  517. */
  518. if (h->rideshare_list[0]) {
  519. lwsl_info("%s: tx list %s\n", __func__,
  520. &h->rideshare_list[h->rideshare_ofs[h->rsidx]]);
  521. return &h->rideshare_list[h->rideshare_ofs[h->rsidx]];
  522. }
  523. /*
  524. * ... otherwise default to our stream type name
  525. */
  526. lwsl_info("%s: def %s\n", __func__, h->ssi.streamtype);
  527. return h->ssi.streamtype;
  528. }
  529. static int
  530. _lws_sspc_set_metadata(struct lws_sspc_handle *h, const char *name,
  531. const void *value, size_t len, int tx_cr_adjust)
  532. {
  533. lws_sspc_metadata_t *md;
  534. /*
  535. * Are we replacing a pending metadata of the same name? It's not
  536. * efficient to do this but user code can do what it likes... let's
  537. * optimize away the old one.
  538. *
  539. * Tx credit adjust always has name ""
  540. */
  541. lws_start_foreach_dll_safe(struct lws_dll2 *, d, d1,
  542. lws_dll2_get_head(&h->metadata_owner)) {
  543. md = lws_container_of(d, lws_sspc_metadata_t, list);
  544. if (!strcmp(name, md->name)) {
  545. lws_dll2_remove(&md->list);
  546. lws_free(md);
  547. break;
  548. }
  549. } lws_end_foreach_dll_safe(d, d1);
  550. /*
  551. * We have to stash the metadata and pass it to the proxy
  552. */
  553. md = lws_malloc(sizeof(*md) + len, "set metadata");
  554. if (!md) {
  555. lwsl_err("%s: OOM\n", __func__);
  556. return 1;
  557. }
  558. memset(md, 0, sizeof(*md));
  559. md->tx_cr_adjust = tx_cr_adjust;
  560. h->txc.peer_tx_cr_est += tx_cr_adjust;
  561. lws_strncpy(md->name, name, sizeof(md->name));
  562. md->len = len;
  563. if (len)
  564. memcpy(&md[1], value, len);
  565. lws_dll2_add_tail(&md->list, &h->metadata_owner);
  566. if (len) {
  567. lwsl_info("%s: set metadata %s\n", __func__, name);
  568. lwsl_hexdump_info(value, len);
  569. } else
  570. lwsl_info("%s: serializing tx cr adj %d\n", __func__,
  571. (int)tx_cr_adjust);
  572. if (h->cwsi)
  573. lws_callback_on_writable(h->cwsi);
  574. return 0;
  575. }
  576. int
  577. lws_sspc_set_metadata(struct lws_sspc_handle *h, const char *name,
  578. const void *value, size_t len)
  579. {
  580. return _lws_sspc_set_metadata(h, name, value, len, 0);
  581. }
  582. int
  583. lws_sspc_add_peer_tx_credit(struct lws_sspc_handle *h, int32_t bump)
  584. {
  585. lwsl_notice("%s: %d\n", __func__, bump);
  586. return _lws_sspc_set_metadata(h, "", NULL, 0, (int)bump);
  587. }
  588. int
  589. lws_sspc_get_est_peer_tx_credit(struct lws_sspc_handle *h)
  590. {
  591. return h->txc.peer_tx_cr_est;
  592. }
  593. void
  594. lws_sspc_start_timeout(struct lws_sspc_handle *h, unsigned int timeout_ms)
  595. {
  596. h->timeout_ms = (uint32_t)timeout_ms;
  597. h->pending_timeout_update = 1;
  598. lws_callback_on_writable(h->cwsi);
  599. }
  600. void
  601. lws_sspc_cancel_timeout(struct lws_sspc_handle *h)
  602. {
  603. lws_sspc_start_timeout(h, (unsigned int)-1);
  604. }
  605. void *
  606. lws_sspc_to_user_object(struct lws_sspc_handle *h)
  607. {
  608. return (void *)&h[1];
  609. }
  610. void
  611. lws_sspc_change_handlers(struct lws_sspc_handle *h,
  612. lws_ss_state_return_t (*rx)(void *userobj, const uint8_t *buf, size_t len, int flags),
  613. lws_ss_state_return_t (*tx)(void *userobj, lws_ss_tx_ordinal_t ord, uint8_t *buf,
  614. size_t *len, int *flags),
  615. lws_ss_state_return_t (*state)(void *userobj, void *h_src /* ss handle type */,
  616. lws_ss_constate_t state, lws_ss_tx_ordinal_t ack))
  617. {
  618. if (rx)
  619. h->ssi.rx = rx;
  620. if (tx)
  621. h->ssi.tx = tx;
  622. if (state)
  623. h->ssi.state = state;
  624. }