output.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include "private-lib-core.h"
  25. /*
  26. * notice this returns number of bytes consumed, or -1
  27. */
  28. int
  29. lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len)
  30. {
  31. struct lws_context *context = lws_get_context(wsi);
  32. struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
  33. size_t real_len = len;
  34. unsigned int n, m;
  35. /*
  36. * If you're looking to dump data being sent down the tls tunnel, see
  37. * lws_ssl_capable_write() in lib/tls/mbedtls/mbedtls-ssl.c or
  38. * lib/tls/openssl/openssl-ssl.c.
  39. *
  40. * There's also a corresponding lws_ssl_capable_read() in those files
  41. * where you can enable a dump of decrypted data as soon as it was
  42. * read.
  43. */
  44. /*
  45. * Detect if we got called twice without going through the
  46. * event loop to handle pending. Since that guarantees extending any
  47. * existing buflist_out it's inefficient.
  48. */
  49. if (0 && buf && wsi->could_have_pending) {
  50. lwsl_hexdump_level(LLL_INFO, buf, len);
  51. lwsl_info("** %p: vh: %s, prot: %s, role %s: "
  52. "Inefficient back-to-back write of %lu detected...\n",
  53. wsi, wsi->a.vhost ? wsi->a.vhost->name : "no vhost",
  54. wsi->a.protocol->name, wsi->role_ops->name,
  55. (unsigned long)len);
  56. }
  57. lws_stats_bump(pt, LWSSTATS_C_API_WRITE, 1);
  58. /* just ignore sends after we cleared the truncation buffer */
  59. if (lwsi_state(wsi) == LRS_FLUSHING_BEFORE_CLOSE &&
  60. !lws_has_buffered_out(wsi)
  61. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  62. && !wsi->http.comp_ctx.may_have_more
  63. #endif
  64. )
  65. return (int)len;
  66. if (buf && lws_has_buffered_out(wsi)) {
  67. lwsl_info("** %p: vh: %s, prot: %s, incr buflist_out by %lu\n",
  68. wsi, wsi->a.vhost ? wsi->a.vhost->name : "no vhost",
  69. wsi->a.protocol->name, (unsigned long)len);
  70. /*
  71. * already buflist ahead of this, add it on the tail of the
  72. * buflist, then ignore it for now and act like we're flushing
  73. * the buflist...
  74. */
  75. if (lws_buflist_append_segment(&wsi->buflist_out, buf, len))
  76. return -1;
  77. buf = NULL;
  78. len = 0;
  79. }
  80. if (wsi->buflist_out) {
  81. /* we have to drain the earliest buflist_out stuff first */
  82. len = lws_buflist_next_segment_len(&wsi->buflist_out, &buf);
  83. real_len = len;
  84. lwsl_debug("%s: draining %d\n", __func__, (int)len);
  85. }
  86. if (!len || !buf)
  87. return 0;
  88. if (!wsi->mux_substream && !lws_socket_is_valid(wsi->desc.sockfd))
  89. lwsl_err("%s: invalid sock %p\n", __func__, wsi);
  90. /* limit sending */
  91. if (wsi->a.protocol->tx_packet_size)
  92. n = (int)wsi->a.protocol->tx_packet_size;
  93. else {
  94. n = (int)wsi->a.protocol->rx_buffer_size;
  95. if (!n)
  96. n = context->pt_serv_buf_size;
  97. }
  98. n += LWS_PRE + 4;
  99. if (n > len)
  100. n = (int)len;
  101. /* nope, send it on the socket directly */
  102. m = lws_ssl_capable_write(wsi, buf, n);
  103. lwsl_info("%s: ssl_capable_write (%d) says %d\n", __func__, n, m);
  104. /* something got written, it can have been truncated now */
  105. wsi->could_have_pending = 1;
  106. switch (m) {
  107. case LWS_SSL_CAPABLE_ERROR:
  108. /* we're going to close, let close know sends aren't possible */
  109. wsi->socket_is_permanently_unusable = 1;
  110. return -1;
  111. case LWS_SSL_CAPABLE_MORE_SERVICE:
  112. /*
  113. * nothing got sent, not fatal. Retry the whole thing later,
  114. * ie, implying treat it was a truncated send so it gets
  115. * retried
  116. */
  117. m = 0;
  118. break;
  119. }
  120. if ((int)m < 0)
  121. m = 0;
  122. /*
  123. * we were sending this from buflist_out? Then not sending everything
  124. * is a small matter of advancing ourselves only by the amount we did
  125. * send in the buflist.
  126. */
  127. if (lws_has_buffered_out(wsi)) {
  128. if (m) {
  129. lwsl_info("%p partial adv %d (vs %ld)\n", wsi, m,
  130. (long)real_len);
  131. lws_buflist_use_segment(&wsi->buflist_out, m);
  132. }
  133. if (!lws_has_buffered_out(wsi)) {
  134. lwsl_info("%s: wsi %p: buflist_out flushed\n",
  135. __func__, wsi);
  136. m = (int)real_len;
  137. if (lwsi_state(wsi) == LRS_FLUSHING_BEFORE_CLOSE) {
  138. lwsl_info("*%p signalling to close now\n", wsi);
  139. return -1; /* retry closing now */
  140. }
  141. if (wsi->close_when_buffered_out_drained) {
  142. wsi->close_when_buffered_out_drained = 0;
  143. return -1;
  144. }
  145. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  146. #if defined(LWS_WITH_SERVER)
  147. if (wsi->http.deferred_transaction_completed) {
  148. lwsl_notice("%s: partial completed, doing "
  149. "deferred transaction completed\n",
  150. __func__);
  151. wsi->http.deferred_transaction_completed = 0;
  152. return lws_http_transaction_completed(wsi) ?
  153. -1 : (int)real_len;
  154. }
  155. #endif
  156. #endif
  157. #if defined(LWS_ROLE_WS)
  158. /* Since buflist_out flushed, we're not inside a frame any more */
  159. if (wsi->ws)
  160. wsi->ws->inside_frame = 0;
  161. #endif
  162. }
  163. /* always callback on writeable */
  164. lws_callback_on_writable(wsi);
  165. return m;
  166. }
  167. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  168. if (wsi->http.comp_ctx.may_have_more)
  169. lws_callback_on_writable(wsi);
  170. #endif
  171. if (m == real_len)
  172. /* what we just sent went out cleanly */
  173. return m;
  174. /*
  175. * We were not able to send everything... and we were not sending from
  176. * an existing buflist_out. So we are starting a fresh buflist_out, by
  177. * buffering the unsent remainder on it.
  178. * (it will get first priority next time the socket is writable).
  179. */
  180. lwsl_debug("%p new partial sent %d from %lu total\n", wsi, m,
  181. (unsigned long)real_len);
  182. if (lws_buflist_append_segment(&wsi->buflist_out, buf + m,
  183. real_len - m) < 0)
  184. return -1;
  185. lws_stats_bump(pt, LWSSTATS_C_WRITE_PARTIALS, 1);
  186. lws_stats_bump(pt, LWSSTATS_B_PARTIALS_ACCEPTED_PARTS, m);
  187. #if defined(LWS_WITH_UDP)
  188. if (lws_wsi_is_udp(wsi)) {
  189. /* stash original destination for fulfilling UDP partials */
  190. wsi->udp->sa_pending = wsi->udp->sa;
  191. wsi->udp->salen_pending = wsi->udp->salen;
  192. }
  193. #endif
  194. /* since something buffered, force it to get another chance to send */
  195. lws_callback_on_writable(wsi);
  196. return (int)real_len;
  197. }
  198. int
  199. lws_write(struct lws *wsi, unsigned char *buf, size_t len,
  200. enum lws_write_protocol wp)
  201. {
  202. struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
  203. #if defined(LWS_WITH_DETAILED_LATENCY)
  204. lws_usec_t us;
  205. #endif
  206. int m;
  207. lws_stats_bump(pt, LWSSTATS_C_API_LWS_WRITE, 1);
  208. if ((int)len < 0) {
  209. lwsl_err("%s: suspicious len int %d, ulong %lu\n", __func__,
  210. (int)len, (unsigned long)len);
  211. return -1;
  212. }
  213. lws_stats_bump(pt, LWSSTATS_B_WRITE, len);
  214. #ifdef LWS_WITH_ACCESS_LOG
  215. wsi->http.access_log.sent += len;
  216. #endif
  217. #if defined(LWS_WITH_SERVER_STATUS)
  218. if (wsi->a.vhost)
  219. wsi->a.vhost->conn_stats.tx += len;
  220. #endif
  221. #if defined(LWS_WITH_DETAILED_LATENCY)
  222. us = lws_now_usecs();
  223. #endif
  224. assert(wsi->role_ops);
  225. if (!wsi->role_ops->write_role_protocol)
  226. return lws_issue_raw(wsi, buf, len);
  227. m = wsi->role_ops->write_role_protocol(wsi, buf, len, &wp);
  228. if (m < 0)
  229. return m;
  230. #if defined(LWS_WITH_DETAILED_LATENCY)
  231. if (wsi->a.context->detailed_latency_cb) {
  232. wsi->detlat.req_size = len;
  233. wsi->detlat.acc_size = m;
  234. wsi->detlat.type = LDLT_WRITE;
  235. if (wsi->detlat.earliest_write_req_pre_write)
  236. wsi->detlat.latencies[LAT_DUR_PROXY_PROXY_REQ_TO_WRITE] =
  237. us - wsi->detlat.earliest_write_req_pre_write;
  238. else
  239. wsi->detlat.latencies[LAT_DUR_PROXY_PROXY_REQ_TO_WRITE] = 0;
  240. wsi->detlat.latencies[LAT_DUR_USERCB] = lws_now_usecs() - us;
  241. lws_det_lat_cb(wsi->a.context, &wsi->detlat);
  242. }
  243. #endif
  244. return m;
  245. }
  246. int
  247. lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len)
  248. {
  249. struct lws_context *context = wsi->a.context;
  250. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  251. int n = 0;
  252. lws_stats_bump(pt, LWSSTATS_C_API_READ, 1);
  253. errno = 0;
  254. #if defined(LWS_WITH_UDP)
  255. if (lws_wsi_is_udp(wsi)) {
  256. wsi->udp->salen = sizeof(wsi->udp->sa);
  257. n = recvfrom(wsi->desc.sockfd, (char *)buf, len, 0,
  258. &wsi->udp->sa, &wsi->udp->salen);
  259. } else
  260. #endif
  261. n = recv(wsi->desc.sockfd, (char *)buf, len, 0);
  262. if (n >= 0) {
  263. if (!n && wsi->unix_skt)
  264. return LWS_SSL_CAPABLE_ERROR;
  265. /*
  266. * See https://libwebsockets.org/
  267. * pipermail/libwebsockets/2019-March/007857.html
  268. */
  269. if (!n)
  270. return LWS_SSL_CAPABLE_ERROR;
  271. #if defined(LWS_WITH_SERVER_STATUS)
  272. if (wsi->a.vhost)
  273. wsi->a.vhost->conn_stats.rx += n;
  274. #endif
  275. lws_stats_bump(pt, LWSSTATS_B_READ, n);
  276. return n;
  277. }
  278. if (LWS_ERRNO == LWS_EAGAIN ||
  279. LWS_ERRNO == LWS_EWOULDBLOCK ||
  280. LWS_ERRNO == LWS_EINTR)
  281. return LWS_SSL_CAPABLE_MORE_SERVICE;
  282. lwsl_info("error on reading from skt : %d\n", LWS_ERRNO);
  283. return LWS_SSL_CAPABLE_ERROR;
  284. }
  285. int
  286. lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len)
  287. {
  288. int n = 0;
  289. #if defined(LWS_PLAT_OPTEE)
  290. ssize_t send(int sockfd, const void *buf, size_t len, int flags);
  291. #endif
  292. #if defined(LWS_WITH_UDP)
  293. if (lws_wsi_is_udp(wsi)) {
  294. if (wsi->a.context->udp_loss_sim_tx_pc) {
  295. uint16_t u16;
  296. /*
  297. * We should randomly drop some of these
  298. */
  299. if (lws_get_random(wsi->a.context, &u16, 2) == 2 &&
  300. ((u16 * 100) / 0xffff) <=
  301. wsi->a.context->udp_loss_sim_tx_pc) {
  302. lwsl_warn("%s: dropping udp tx\n", __func__);
  303. /* pretend it was sent */
  304. n = len;
  305. goto post_send;
  306. }
  307. }
  308. if (lws_has_buffered_out(wsi))
  309. n = sendto(wsi->desc.sockfd, (const char *)buf,
  310. len, 0, &wsi->udp->sa_pending,
  311. wsi->udp->salen_pending);
  312. else
  313. n = sendto(wsi->desc.sockfd, (const char *)buf,
  314. len, 0, &wsi->udp->sa, wsi->udp->salen);
  315. } else
  316. #endif
  317. if (wsi->role_ops->file_handle)
  318. n = write((int)(lws_intptr_t)wsi->desc.filefd, buf, len);
  319. else
  320. n = send(wsi->desc.sockfd, (char *)buf, len, MSG_NOSIGNAL);
  321. // lwsl_info("%s: sent len %d result %d", __func__, len, n);
  322. #if defined(LWS_WITH_UDP)
  323. post_send:
  324. #endif
  325. if (n >= 0)
  326. return n;
  327. if (LWS_ERRNO == LWS_EAGAIN ||
  328. LWS_ERRNO == LWS_EWOULDBLOCK ||
  329. LWS_ERRNO == LWS_EINTR) {
  330. if (LWS_ERRNO == LWS_EWOULDBLOCK) {
  331. lws_set_blocking_send(wsi);
  332. }
  333. return LWS_SSL_CAPABLE_MORE_SERVICE;
  334. }
  335. lwsl_debug("ERROR writing len %d to skt fd %d err %d / errno %d\n",
  336. len, wsi->desc.sockfd, n, LWS_ERRNO);
  337. return LWS_SSL_CAPABLE_ERROR;
  338. }
  339. int
  340. lws_ssl_pending_no_ssl(struct lws *wsi)
  341. {
  342. (void)wsi;
  343. #if defined(LWS_PLAT_FREERTOS)
  344. return 100;
  345. #else
  346. return 0;
  347. #endif
  348. }