2018-06-07 11:33:24 +00:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2019-04-01 00:14:42 +00:00
|
|
|
* Copyright (C) 2010-2017 Andy Green <andy@warmcat.com>
|
2018-06-07 11:33:24 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation:
|
|
|
|
* version 2.1 of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "core/private.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* notice this returns number of bytes consumed, or -1
|
|
|
|
*/
|
|
|
|
int lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len)
|
|
|
|
{
|
|
|
|
struct lws_context *context = lws_get_context(wsi);
|
|
|
|
struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
|
|
|
|
size_t real_len = len;
|
2019-04-01 00:14:42 +00:00
|
|
|
unsigned int n;
|
2018-06-07 11:33:24 +00:00
|
|
|
|
2019-04-01 00:14:42 +00:00
|
|
|
// lwsl_hexdump_err(buf, len);
|
2018-06-07 11:33:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Detect if we got called twice without going through the
|
2019-04-01 00:14:42 +00:00
|
|
|
* event loop to handle pending. This would be caused by either
|
|
|
|
* back-to-back writes in one WRITABLE (illegal) or calling lws_write()
|
|
|
|
* from outside the WRITABLE callback (illegal).
|
2018-06-07 11:33:24 +00:00
|
|
|
*/
|
2019-04-01 00:14:42 +00:00
|
|
|
if (wsi->could_have_pending) {
|
|
|
|
lwsl_hexdump_level(LLL_ERR, buf, len);
|
|
|
|
lwsl_err("** %p: vh: %s, prot: %s, role %s: "
|
|
|
|
"Illegal back-to-back write of %lu detected...\n",
|
2018-06-07 11:33:24 +00:00
|
|
|
wsi, wsi->vhost->name, wsi->protocol->name,
|
|
|
|
wsi->role_ops->name,
|
|
|
|
(unsigned long)len);
|
2019-04-01 00:14:42 +00:00
|
|
|
// assert(0);
|
|
|
|
|
|
|
|
return -1;
|
2018-06-07 11:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_API_WRITE, 1);
|
|
|
|
|
2019-04-01 00:14:42 +00:00
|
|
|
if (!len)
|
|
|
|
return 0;
|
2018-06-07 11:33:24 +00:00
|
|
|
/* just ignore sends after we cleared the truncation buffer */
|
2019-04-01 00:14:42 +00:00
|
|
|
if (lwsi_state(wsi) == LRS_FLUSHING_BEFORE_CLOSE && !wsi->trunc_len)
|
2018-06-07 11:33:24 +00:00
|
|
|
return (int)len;
|
|
|
|
|
2019-04-01 00:14:42 +00:00
|
|
|
if (wsi->trunc_len && (buf < wsi->trunc_alloc ||
|
|
|
|
buf > (wsi->trunc_alloc + wsi->trunc_len + wsi->trunc_offset))) {
|
|
|
|
lwsl_hexdump_level(LLL_ERR, buf, len);
|
|
|
|
lwsl_err("** %p: vh: %s, prot: %s, Sending new %lu, pending truncated ...\n"
|
|
|
|
" It's illegal to do an lws_write outside of\n"
|
|
|
|
" the writable callback: fix your code\n",
|
2018-06-07 11:33:24 +00:00
|
|
|
wsi, wsi->vhost->name, wsi->protocol->name,
|
|
|
|
(unsigned long)len);
|
2019-04-01 00:14:42 +00:00
|
|
|
assert(0);
|
2018-06-07 11:33:24 +00:00
|
|
|
|
2019-04-01 00:14:42 +00:00
|
|
|
return -1;
|
2019-03-06 00:07:13 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 11:33:24 +00:00
|
|
|
if (!wsi->http2_substream && !lws_socket_is_valid(wsi->desc.sockfd))
|
|
|
|
lwsl_warn("** error invalid sock but expected to send\n");
|
|
|
|
|
|
|
|
/* limit sending */
|
|
|
|
if (wsi->protocol->tx_packet_size)
|
|
|
|
n = (int)wsi->protocol->tx_packet_size;
|
|
|
|
else {
|
|
|
|
n = (int)wsi->protocol->rx_buffer_size;
|
|
|
|
if (!n)
|
|
|
|
n = context->pt_serv_buf_size;
|
|
|
|
}
|
|
|
|
n += LWS_PRE + 4;
|
|
|
|
if (n > len)
|
|
|
|
n = (int)len;
|
|
|
|
|
|
|
|
/* nope, send it on the socket directly */
|
|
|
|
lws_latency_pre(context, wsi);
|
2019-04-01 00:14:42 +00:00
|
|
|
n = lws_ssl_capable_write(wsi, buf, n);
|
|
|
|
lws_latency(context, wsi, "send lws_issue_raw", n, n == len);
|
2018-06-07 11:33:24 +00:00
|
|
|
|
|
|
|
/* something got written, it can have been truncated now */
|
|
|
|
wsi->could_have_pending = 1;
|
|
|
|
|
2019-04-01 00:14:42 +00:00
|
|
|
switch (n) {
|
2018-06-07 11:33:24 +00:00
|
|
|
case LWS_SSL_CAPABLE_ERROR:
|
|
|
|
/* we're going to close, let close know sends aren't possible */
|
|
|
|
wsi->socket_is_permanently_unusable = 1;
|
|
|
|
return -1;
|
|
|
|
case LWS_SSL_CAPABLE_MORE_SERVICE:
|
|
|
|
/*
|
|
|
|
* nothing got sent, not fatal. Retry the whole thing later,
|
|
|
|
* ie, implying treat it was a truncated send so it gets
|
|
|
|
* retried
|
|
|
|
*/
|
2019-04-01 00:14:42 +00:00
|
|
|
n = 0;
|
2018-06-07 11:33:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-04-01 00:14:42 +00:00
|
|
|
* we were already handling a truncated send?
|
2018-06-07 11:33:24 +00:00
|
|
|
*/
|
2019-04-01 00:14:42 +00:00
|
|
|
if (wsi->trunc_len) {
|
|
|
|
lwsl_info("%p partial adv %d (vs %ld)\n", wsi, n, (long)real_len);
|
|
|
|
wsi->trunc_offset += n;
|
|
|
|
wsi->trunc_len -= n;
|
|
|
|
|
|
|
|
if (!wsi->trunc_len) {
|
|
|
|
lwsl_info("** %p partial send completed\n", wsi);
|
|
|
|
/* done with it, but don't free it */
|
|
|
|
n = (int)real_len;
|
2018-06-07 11:33:24 +00:00
|
|
|
if (lwsi_state(wsi) == LRS_FLUSHING_BEFORE_CLOSE) {
|
2019-04-01 00:14:42 +00:00
|
|
|
lwsl_info("** %p signalling to close now\n", wsi);
|
2018-06-07 11:33:24 +00:00
|
|
|
return -1; /* retry closing now */
|
|
|
|
}
|
2018-11-03 14:00:17 +00:00
|
|
|
|
|
|
|
#if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
|
|
|
|
#if !defined(LWS_WITHOUT_SERVER)
|
|
|
|
if (wsi->http.deferred_transaction_completed) {
|
|
|
|
lwsl_notice("%s: partial completed, doing "
|
|
|
|
"deferred transaction completed\n",
|
|
|
|
__func__);
|
|
|
|
wsi->http.deferred_transaction_completed = 0;
|
2019-04-01 00:14:42 +00:00
|
|
|
return lws_http_transaction_completed(wsi);
|
2018-11-03 14:00:17 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
2018-06-07 11:33:24 +00:00
|
|
|
}
|
|
|
|
/* always callback on writeable */
|
|
|
|
lws_callback_on_writable(wsi);
|
|
|
|
|
2019-04-01 00:14:42 +00:00
|
|
|
return n;
|
2018-06-07 11:33:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-01 00:14:42 +00:00
|
|
|
if ((unsigned int)n == real_len)
|
2018-06-07 11:33:24 +00:00
|
|
|
/* what we just sent went out cleanly */
|
2019-04-01 00:14:42 +00:00
|
|
|
return n;
|
2018-06-07 11:33:24 +00:00
|
|
|
|
|
|
|
/*
|
2019-04-01 00:14:42 +00:00
|
|
|
* Newly truncated send. Buffer the remainder (it will get
|
|
|
|
* first priority next time the socket is writable).
|
2018-06-07 11:33:24 +00:00
|
|
|
*/
|
2019-04-01 00:14:42 +00:00
|
|
|
lwsl_debug("%p new partial sent %d from %lu total\n", wsi, n,
|
2018-06-07 11:33:24 +00:00
|
|
|
(unsigned long)real_len);
|
|
|
|
|
2019-03-06 00:07:13 +00:00
|
|
|
lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_WRITE_PARTIALS, 1);
|
2019-04-01 00:14:42 +00:00
|
|
|
lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_B_PARTIALS_ACCEPTED_PARTS, n);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* - if we still have a suitable malloc lying around, use it
|
|
|
|
* - or, if too small, reallocate it
|
|
|
|
* - or, if no buffer, create it
|
|
|
|
*/
|
|
|
|
if (!wsi->trunc_alloc || real_len - n > wsi->trunc_alloc_len) {
|
|
|
|
lws_free(wsi->trunc_alloc);
|
|
|
|
|
|
|
|
wsi->trunc_alloc_len = (unsigned int)(real_len - n);
|
|
|
|
wsi->trunc_alloc = lws_malloc(real_len - n,
|
|
|
|
"truncated send alloc");
|
|
|
|
if (!wsi->trunc_alloc) {
|
|
|
|
lwsl_err("truncated send: unable to malloc %lu\n",
|
|
|
|
(unsigned long)(real_len - n));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wsi->trunc_offset = 0;
|
|
|
|
wsi->trunc_len = (unsigned int)(real_len - n);
|
|
|
|
memcpy(wsi->trunc_alloc, buf + n, real_len - n);
|
2018-06-07 11:33:24 +00:00
|
|
|
|
|
|
|
#if !defined(LWS_WITH_ESP32)
|
|
|
|
if (lws_wsi_is_udp(wsi)) {
|
|
|
|
/* stash original destination for fulfilling UDP partials */
|
|
|
|
wsi->udp->sa_pending = wsi->udp->sa;
|
|
|
|
wsi->udp->salen_pending = wsi->udp->salen;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* since something buffered, force it to get another chance to send */
|
|
|
|
lws_callback_on_writable(wsi);
|
|
|
|
|
|
|
|
return (int)real_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
LWS_VISIBLE int lws_write(struct lws *wsi, unsigned char *buf, size_t len,
|
|
|
|
enum lws_write_protocol wp)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
|
|
|
|
|
2019-04-01 00:14:42 +00:00
|
|
|
if (wsi->parent_carries_io) {
|
|
|
|
struct lws_write_passthru pas;
|
|
|
|
|
|
|
|
pas.buf = buf;
|
|
|
|
pas.len = len;
|
|
|
|
pas.wp = wp;
|
|
|
|
pas.wsi = wsi;
|
|
|
|
|
|
|
|
if (wsi->parent->protocol->callback(wsi->parent,
|
|
|
|
LWS_CALLBACK_CHILD_WRITE_VIA_PARENT,
|
|
|
|
wsi->parent->user_space,
|
|
|
|
(void *)&pas, 0))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return (int)len;
|
|
|
|
}
|
|
|
|
|
2018-06-07 11:33:24 +00:00
|
|
|
lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_API_LWS_WRITE, 1);
|
|
|
|
|
|
|
|
if ((int)len < 0) {
|
|
|
|
lwsl_err("%s: suspicious len int %d, ulong %lu\n", __func__,
|
|
|
|
(int)len, (unsigned long)len);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_B_WRITE, len);
|
|
|
|
|
|
|
|
#ifdef LWS_WITH_ACCESS_LOG
|
|
|
|
wsi->http.access_log.sent += len;
|
|
|
|
#endif
|
|
|
|
if (wsi->vhost)
|
|
|
|
wsi->vhost->conn_stats.tx += len;
|
|
|
|
|
|
|
|
assert(wsi->role_ops);
|
|
|
|
if (!wsi->role_ops->write_role_protocol)
|
|
|
|
return lws_issue_raw(wsi, buf, len);
|
|
|
|
|
|
|
|
return wsi->role_ops->write_role_protocol(wsi, buf, len, &wp);
|
|
|
|
}
|
|
|
|
|
|
|
|
LWS_VISIBLE int
|
|
|
|
lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len)
|
|
|
|
{
|
|
|
|
struct lws_context *context = wsi->context;
|
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
lws_stats_atomic_bump(context, pt, LWSSTATS_C_API_READ, 1);
|
|
|
|
|
|
|
|
if (lws_wsi_is_udp(wsi)) {
|
|
|
|
#if !defined(LWS_WITH_ESP32)
|
|
|
|
wsi->udp->salen = sizeof(wsi->udp->sa);
|
|
|
|
n = recvfrom(wsi->desc.sockfd, (char *)buf, len, 0,
|
|
|
|
&wsi->udp->sa, &wsi->udp->salen);
|
|
|
|
#endif
|
|
|
|
} else
|
|
|
|
n = recv(wsi->desc.sockfd, (char *)buf, len, 0);
|
|
|
|
|
|
|
|
if (n >= 0) {
|
|
|
|
if (wsi->vhost)
|
|
|
|
wsi->vhost->conn_stats.rx += n;
|
|
|
|
lws_stats_atomic_bump(context, pt, LWSSTATS_B_READ, n);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LWS_ERRNO == LWS_EAGAIN ||
|
|
|
|
LWS_ERRNO == LWS_EWOULDBLOCK ||
|
|
|
|
LWS_ERRNO == LWS_EINTR)
|
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
|
|
|
|
2019-04-01 00:14:42 +00:00
|
|
|
lwsl_notice("error on reading from skt : %d\n", LWS_ERRNO);
|
2018-06-07 11:33:24 +00:00
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
LWS_VISIBLE int
|
|
|
|
lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
if (lws_wsi_is_udp(wsi)) {
|
|
|
|
#if !defined(LWS_WITH_ESP32)
|
2019-04-01 00:14:42 +00:00
|
|
|
if (wsi->trunc_len)
|
|
|
|
n = sendto(wsi->desc.sockfd, buf, len, 0, &wsi->udp->sa_pending, wsi->udp->salen_pending);
|
2018-06-07 11:33:24 +00:00
|
|
|
else
|
2019-04-01 00:14:42 +00:00
|
|
|
n = sendto(wsi->desc.sockfd, buf, len, 0, &wsi->udp->sa, wsi->udp->salen);
|
2018-06-07 11:33:24 +00:00
|
|
|
#endif
|
|
|
|
} else
|
|
|
|
n = send(wsi->desc.sockfd, (char *)buf, len, MSG_NOSIGNAL);
|
|
|
|
// lwsl_info("%s: sent len %d result %d", __func__, len, n);
|
|
|
|
if (n >= 0)
|
|
|
|
return n;
|
|
|
|
|
|
|
|
if (LWS_ERRNO == LWS_EAGAIN ||
|
|
|
|
LWS_ERRNO == LWS_EWOULDBLOCK ||
|
|
|
|
LWS_ERRNO == LWS_EINTR) {
|
|
|
|
if (LWS_ERRNO == LWS_EWOULDBLOCK) {
|
|
|
|
lws_set_blocking_send(wsi);
|
|
|
|
}
|
|
|
|
|
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
|
|
|
}
|
|
|
|
|
|
|
|
lwsl_debug("ERROR writing len %d to skt fd %d err %d / errno %d\n",
|
2019-04-01 00:14:42 +00:00
|
|
|
len, wsi->desc.sockfd, n, LWS_ERRNO);
|
2018-06-07 11:33:24 +00:00
|
|
|
|
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
LWS_VISIBLE int
|
|
|
|
lws_ssl_pending_no_ssl(struct lws *wsi)
|
|
|
|
{
|
|
|
|
(void)wsi;
|
|
|
|
#if defined(LWS_WITH_ESP32)
|
|
|
|
return 100;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|