2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2018 Andy Green <andy@warmcat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 #include "private-lib-core.h"
25 * notice this returns number of bytes consumed, or -1
27 int lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len)
29 struct lws_context *context = lws_get_context(wsi);
30 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
31 size_t real_len = len;
34 // lwsl_notice("%s: len %d\n", __func__, (int)len);
35 // lwsl_hexdump_level(LLL_NOTICE, buf, len);
38 * Detect if we got called twice without going through the
39 * event loop to handle pending. Since that guarantees extending any
40 * existing buflist_out it's inefficient.
42 if (0 && buf && wsi->could_have_pending) {
43 lwsl_hexdump_level(LLL_INFO, buf, len);
44 lwsl_info("** %p: vh: %s, prot: %s, role %s: "
45 "Inefficient back-to-back write of %lu detected...\n",
46 wsi, wsi->vhost ? wsi->vhost->name : "no vhost",
47 wsi->protocol->name, wsi->role_ops->name,
51 lws_stats_bump(pt, LWSSTATS_C_API_WRITE, 1);
53 /* just ignore sends after we cleared the truncation buffer */
54 if (lwsi_state(wsi) == LRS_FLUSHING_BEFORE_CLOSE &&
55 !lws_has_buffered_out(wsi)
56 #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
57 && !wsi->http.comp_ctx.may_have_more
62 if (buf && lws_has_buffered_out(wsi)) {
63 lwsl_info("** %p: vh: %s, prot: %s, incr buflist_out by %lu\n",
64 wsi, wsi->vhost ? wsi->vhost->name : "no vhost",
65 wsi->protocol->name, (unsigned long)len);
68 * already buflist ahead of this, add it on the tail of the
69 * buflist, then ignore it for now and act like we're flushing
73 if (lws_buflist_append_segment(&wsi->buflist_out, buf, len))
80 if (wsi->buflist_out) {
81 /* we have to drain the earliest buflist_out stuff first */
83 len = lws_buflist_next_segment_len(&wsi->buflist_out, &buf);
86 lwsl_debug("%s: draining %d\n", __func__, (int)len);
92 if (!wsi->http2_substream && !lws_socket_is_valid(wsi->desc.sockfd))
93 lwsl_warn("** error invalid sock but expected to send\n");
96 if (wsi->protocol->tx_packet_size)
97 n = (int)wsi->protocol->tx_packet_size;
99 n = (int)wsi->protocol->rx_buffer_size;
101 n = context->pt_serv_buf_size;
107 /* nope, send it on the socket directly */
108 lws_latency_pre(context, wsi);
109 m = lws_ssl_capable_write(wsi, buf, n);
110 lws_latency(context, wsi, "send lws_issue_raw", n, n == m);
112 lwsl_info("%s: ssl_capable_write (%d) says %d\n", __func__, n, m);
114 /* something got written, it can have been truncated now */
115 wsi->could_have_pending = 1;
118 case LWS_SSL_CAPABLE_ERROR:
119 /* we're going to close, let close know sends aren't possible */
120 wsi->socket_is_permanently_unusable = 1;
122 case LWS_SSL_CAPABLE_MORE_SERVICE:
124 * nothing got sent, not fatal. Retry the whole thing later,
125 * ie, implying treat it was a truncated send so it gets
136 * we were sending this from buflist_out? Then not sending everything
137 * is a small matter of advancing ourselves only by the amount we did
138 * send in the buflist.
140 if (lws_has_buffered_out(wsi)) {
142 lwsl_info("%p partial adv %d (vs %ld)\n", wsi, m,
144 lws_buflist_use_segment(&wsi->buflist_out, m);
147 if (!lws_has_buffered_out(wsi)) {
148 lwsl_info("%s: wsi %p: buflist_out flushed\n",
152 if (lwsi_state(wsi) == LRS_FLUSHING_BEFORE_CLOSE) {
153 lwsl_info("*%p signalling to close now\n", wsi);
154 return -1; /* retry closing now */
157 if (wsi->close_when_buffered_out_drained) {
158 wsi->close_when_buffered_out_drained = 0;
162 #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
163 #if !defined(LWS_WITHOUT_SERVER)
164 if (wsi->http.deferred_transaction_completed) {
165 lwsl_notice("%s: partial completed, doing "
166 "deferred transaction completed\n",
168 wsi->http.deferred_transaction_completed = 0;
169 return lws_http_transaction_completed(wsi) ?
175 /* always callback on writeable */
176 lws_callback_on_writable(wsi);
181 #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
182 if (wsi->http.comp_ctx.may_have_more)
183 lws_callback_on_writable(wsi);
187 /* what we just sent went out cleanly */
191 * We were not able to send everything... and we were not sending from
192 * an existing buflist_out. So we are starting a fresh buflist_out, by
193 * buffering the unsent remainder on it.
194 * (it will get first priority next time the socket is writable).
196 lwsl_debug("%p new partial sent %d from %lu total\n", wsi, m,
197 (unsigned long)real_len);
199 if (lws_buflist_append_segment(&wsi->buflist_out, buf + m,
203 lws_stats_bump(pt, LWSSTATS_C_WRITE_PARTIALS, 1);
204 lws_stats_bump(pt, LWSSTATS_B_PARTIALS_ACCEPTED_PARTS, m);
206 #if !defined(LWS_WITH_ESP32) && !defined(LWS_PLAT_OPTEE)
207 if (lws_wsi_is_udp(wsi)) {
208 /* stash original destination for fulfilling UDP partials */
209 wsi->udp->sa_pending = wsi->udp->sa;
210 wsi->udp->salen_pending = wsi->udp->salen;
214 /* since something buffered, force it to get another chance to send */
215 lws_callback_on_writable(wsi);
217 return (int)real_len;
220 LWS_VISIBLE int lws_write(struct lws *wsi, unsigned char *buf, size_t len,
221 enum lws_write_protocol wp)
223 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
225 lws_stats_bump(pt, LWSSTATS_C_API_LWS_WRITE, 1);
228 lwsl_err("%s: suspicious len int %d, ulong %lu\n", __func__,
229 (int)len, (unsigned long)len);
233 lws_stats_bump(pt, LWSSTATS_B_WRITE, len);
235 #ifdef LWS_WITH_ACCESS_LOG
236 wsi->http.access_log.sent += len;
239 wsi->vhost->conn_stats.tx += len;
241 assert(wsi->role_ops);
242 if (!wsi->role_ops->write_role_protocol)
243 return lws_issue_raw(wsi, buf, len);
245 return wsi->role_ops->write_role_protocol(wsi, buf, len, &wp);
249 lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len)
251 struct lws_context *context = wsi->context;
252 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
255 lws_stats_bump(pt, LWSSTATS_C_API_READ, 1);
258 if (lws_wsi_is_udp(wsi)) {
259 #if !defined(LWS_WITH_ESP32) && !defined(LWS_PLAT_OPTEE)
260 wsi->udp->salen = sizeof(wsi->udp->sa);
261 n = recvfrom(wsi->desc.sockfd, (char *)buf, len, 0,
262 &wsi->udp->sa, &wsi->udp->salen);
265 n = recv(wsi->desc.sockfd, (char *)buf, len, 0);
269 if (!n && wsi->unix_skt)
270 return LWS_SSL_CAPABLE_ERROR;
273 * See https://libwebsockets.org/
274 * pipermail/libwebsockets/2019-March/007857.html
277 return LWS_SSL_CAPABLE_ERROR;
280 wsi->vhost->conn_stats.rx += n;
281 lws_stats_bump(pt, LWSSTATS_B_READ, n);
286 if (LWS_ERRNO == LWS_EAGAIN ||
287 LWS_ERRNO == LWS_EWOULDBLOCK ||
288 LWS_ERRNO == LWS_EINTR)
289 return LWS_SSL_CAPABLE_MORE_SERVICE;
291 lwsl_info("error on reading from skt : %d\n", LWS_ERRNO);
292 return LWS_SSL_CAPABLE_ERROR;
296 lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len)
299 #if defined(LWS_PLAT_OPTEE)
300 ssize_t send(int sockfd, const void *buf, size_t len, int flags);
303 if (lws_wsi_is_udp(wsi)) {
304 #if !defined(LWS_WITH_ESP32) && !defined(LWS_PLAT_OPTEE)
305 if (lws_has_buffered_out(wsi))
306 n = sendto(wsi->desc.sockfd, (const char *)buf,
307 len, 0, &wsi->udp->sa_pending,
308 wsi->udp->salen_pending);
310 n = sendto(wsi->desc.sockfd, (const char *)buf,
311 len, 0, &wsi->udp->sa, wsi->udp->salen);
314 n = send(wsi->desc.sockfd, (char *)buf, len, MSG_NOSIGNAL);
315 // lwsl_info("%s: sent len %d result %d", __func__, len, n);
319 if (LWS_ERRNO == LWS_EAGAIN ||
320 LWS_ERRNO == LWS_EWOULDBLOCK ||
321 LWS_ERRNO == LWS_EINTR) {
322 if (LWS_ERRNO == LWS_EWOULDBLOCK) {
323 lws_set_blocking_send(wsi);
326 return LWS_SSL_CAPABLE_MORE_SERVICE;
329 lwsl_debug("ERROR writing len %d to skt fd %d err %d / errno %d\n",
330 len, wsi->desc.sockfd, n, LWS_ERRNO);
332 return LWS_SSL_CAPABLE_ERROR;
336 lws_ssl_pending_no_ssl(struct lws *wsi)
339 #if defined(LWS_WITH_ESP32)