2018-04-11 13:39:42 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2020-02-27 06:38:23 +00:00
|
|
|
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
|
2018-04-11 13:39:42 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2018-04-11 13:39:42 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2018-04-11 13:39:42 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
2018-04-11 13:39:42 +08:00
|
|
|
*/
|
|
|
|
|
2019-08-15 10:49:52 +01:00
|
|
|
#include <private-lib-core.h>
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2022-01-25 19:31:36 +00:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
|
|
|
static int
|
|
|
|
lws_raw_skt_connect(struct lws *wsi)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
#if defined(LWS_WITH_TLS)
|
|
|
|
const char *cce = NULL;
|
|
|
|
char ccebuf[128];
|
|
|
|
|
|
|
|
#if !defined(LWS_WITH_SYS_ASYNC_DNS)
|
|
|
|
switch (lws_client_create_tls(wsi, &cce, 1)) {
|
|
|
|
#else
|
|
|
|
switch (lws_client_create_tls(wsi, &cce, 0)) {
|
|
|
|
#endif
|
|
|
|
case CCTLS_RETURN_ERROR:
|
|
|
|
lws_inform_client_conn_fail(wsi, (void *)cce, strlen(cce));
|
|
|
|
return -1;
|
|
|
|
case CCTLS_RETURN_RETRY:
|
|
|
|
return 0;
|
|
|
|
case CCTLS_RETURN_DONE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wsi->tls.use_ssl & LCCSCF_USE_SSL) {
|
|
|
|
n = lws_ssl_client_connect2(wsi, ccebuf, sizeof(ccebuf));
|
|
|
|
if (n < 0) {
|
|
|
|
lws_inform_client_conn_fail(wsi, (void *)ccebuf,
|
|
|
|
strlen(ccebuf));
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (n != 1)
|
|
|
|
return 0; /* wait */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-03-07 09:42:19 +00:00
|
|
|
if (!wsi->hdr_parsing_completed) {
|
|
|
|
n = user_callback_handle_rxflow(wsi->a.protocol->callback,
|
|
|
|
wsi, wsi->role_ops->adoption_cb[lwsi_role_server(wsi)],
|
|
|
|
wsi->user_space, NULL, 0);
|
|
|
|
if (n) {
|
|
|
|
lws_inform_client_conn_fail(wsi, (void *)"user", 4);
|
|
|
|
return 1;
|
|
|
|
}
|
2022-01-25 19:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
|
|
|
|
lwsi_set_state(wsi, LRS_ESTABLISHED);
|
|
|
|
|
|
|
|
return 1; /* success */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
static int
|
|
|
|
rops_handle_POLLIN_raw_skt(struct lws_context_per_thread *pt, struct lws *wsi,
|
|
|
|
struct lws_pollfd *pollfd)
|
|
|
|
{
|
2020-02-27 06:38:23 +00:00
|
|
|
#if defined(LWS_WITH_SOCKS5)
|
|
|
|
const char *cce = NULL;
|
|
|
|
#endif
|
2018-04-17 15:35:15 +08:00
|
|
|
struct lws_tokens ebuf;
|
2020-03-10 06:45:24 +00:00
|
|
|
int n = 0, buffered = 0;
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
/* pending truncated sends have uber priority */
|
|
|
|
|
2018-08-20 12:02:26 +08:00
|
|
|
if (lws_has_buffered_out(wsi)) {
|
2018-04-11 13:39:42 +08:00
|
|
|
if (!(pollfd->revents & LWS_POLLOUT))
|
|
|
|
return LWS_HPI_RET_HANDLED;
|
|
|
|
|
2018-08-20 12:02:26 +08:00
|
|
|
/* drain the output buflist */
|
|
|
|
if (lws_issue_raw(wsi, NULL, 0) < 0)
|
2018-04-11 13:39:42 +08:00
|
|
|
goto fail;
|
|
|
|
/*
|
|
|
|
* we can't afford to allow input processing to send
|
|
|
|
* something new, so spin around he event loop until
|
|
|
|
* he doesn't have any partials
|
|
|
|
*/
|
|
|
|
return LWS_HPI_RET_HANDLED;
|
|
|
|
}
|
|
|
|
|
2019-06-27 14:55:04 +01:00
|
|
|
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_SERVER)
|
2019-06-27 14:55:04 +01:00
|
|
|
if (!lwsi_role_client(wsi) && lwsi_state(wsi) != LRS_ESTABLISHED) {
|
|
|
|
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_debug(wsi, "wsistate 0x%x\n", (int)wsi->wsistate);
|
2019-06-27 14:55:04 +01:00
|
|
|
|
|
|
|
if (lwsi_state(wsi) != LRS_SSL_INIT)
|
|
|
|
if (lws_server_socket_service_ssl(wsi,
|
2020-04-19 08:43:01 +01:00
|
|
|
LWS_SOCK_INVALID,
|
|
|
|
!!(pollfd->revents & pollfd->events & LWS_POLLIN)))
|
2019-06-27 14:55:04 +01:00
|
|
|
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
|
|
|
|
|
|
|
return LWS_HPI_RET_HANDLED;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
if ((pollfd->revents & pollfd->events & LWS_POLLIN) &&
|
|
|
|
!(wsi->favoured_pollin &&
|
|
|
|
(pollfd->revents & pollfd->events & LWS_POLLOUT))) {
|
|
|
|
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_debug(wsi, "POLLIN: state 0x%x", lwsi_state(wsi));
|
2020-01-20 10:02:56 +00:00
|
|
|
|
2020-02-27 06:38:23 +00:00
|
|
|
switch (lwsi_state(wsi)) {
|
2018-05-02 08:46:16 +08:00
|
|
|
|
2020-02-27 06:38:23 +00:00
|
|
|
/* any tunnel has to have been established... */
|
|
|
|
case LRS_SSL_ACK_PENDING:
|
|
|
|
goto nope;
|
|
|
|
/* we are actually connected */
|
|
|
|
case LRS_WAITING_CONNECT:
|
|
|
|
goto nope;
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2022-01-25 19:31:36 +00:00
|
|
|
case LRS_WAITING_SSL:
|
|
|
|
#if defined(LWS_WITH_CLIENT)
|
|
|
|
n = lws_raw_skt_connect(wsi);
|
|
|
|
if (n < 0)
|
|
|
|
goto fail;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
2020-02-27 06:38:23 +00:00
|
|
|
#if defined(LWS_WITH_SOCKS5)
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2020-02-27 06:38:23 +00:00
|
|
|
/* SOCKS Greeting Reply */
|
|
|
|
case LRS_WAITING_SOCKS_GREETING_REPLY:
|
|
|
|
case LRS_WAITING_SOCKS_AUTH_REPLY:
|
|
|
|
case LRS_WAITING_SOCKS_CONNECT_REPLY:
|
|
|
|
|
|
|
|
switch (lws_socks5c_handle_state(wsi, pollfd, &cce)) {
|
|
|
|
case LW5CHS_RET_RET0:
|
|
|
|
goto nope;
|
|
|
|
case LW5CHS_RET_BAIL3:
|
|
|
|
lws_inform_client_conn_fail(wsi, (void *)cce, strlen(cce));
|
|
|
|
goto fail;
|
|
|
|
case LW5CHS_RET_STARTHS:
|
|
|
|
lwsi_set_state(wsi, LRS_ESTABLISHED);
|
|
|
|
lws_client_connect_4_established(wsi, NULL, 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now we got the socks5 connection, we need to
|
|
|
|
* go down the tls path on it now if that's what
|
|
|
|
* we want
|
|
|
|
*/
|
2020-03-07 20:03:58 +00:00
|
|
|
goto post_rx;
|
2020-02-27 06:38:23 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2019-09-08 08:08:55 +01:00
|
|
|
}
|
2020-02-27 06:38:23 +00:00
|
|
|
goto post_rx;
|
2019-09-30 09:42:38 -07:00
|
|
|
#endif
|
2020-02-27 06:38:23 +00:00
|
|
|
default:
|
|
|
|
ebuf.token = NULL;
|
2024-03-05 17:43:36 +08:00
|
|
|
ebuf.len = (int) wsi->a.protocol->rx_buffer_size;
|
2020-02-27 06:38:23 +00:00
|
|
|
|
|
|
|
buffered = lws_buflist_aware_read(pt, wsi, &ebuf, 1, __func__);
|
|
|
|
switch (ebuf.len) {
|
|
|
|
case 0:
|
2021-01-06 15:08:22 +00:00
|
|
|
if (wsi->unix_skt)
|
|
|
|
break;
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_info(wsi, "read 0 len");
|
2020-02-27 06:38:23 +00:00
|
|
|
wsi->seen_zero_length_recv = 1;
|
|
|
|
if (lws_change_pollfd(wsi, LWS_POLLIN, 0))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we need to go to fail here, since it's the only
|
|
|
|
* chance we get to understand that the socket has
|
|
|
|
* closed
|
|
|
|
*/
|
|
|
|
// goto try_pollout;
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
case LWS_SSL_CAPABLE_ERROR:
|
|
|
|
goto fail;
|
|
|
|
case LWS_SSL_CAPABLE_MORE_SERVICE:
|
|
|
|
goto try_pollout;
|
|
|
|
}
|
2019-09-08 08:08:55 +01:00
|
|
|
|
2019-09-30 09:42:38 -07:00
|
|
|
#if defined(LWS_WITH_UDP)
|
2021-03-16 13:32:05 +00:00
|
|
|
if (lws_fi(&wsi->fic, "udp_rx_loss")) {
|
|
|
|
n = ebuf.len;
|
|
|
|
goto post_rx;
|
2020-02-27 06:38:23 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
n = user_callback_handle_rxflow(wsi->a.protocol->callback,
|
2020-02-27 06:38:23 +00:00
|
|
|
wsi, LWS_CALLBACK_RAW_RX,
|
|
|
|
wsi->user_space, ebuf.token,
|
2020-12-12 06:21:40 +00:00
|
|
|
(unsigned int)ebuf.len);
|
2020-02-27 06:38:23 +00:00
|
|
|
#if defined(LWS_WITH_UDP) || defined(LWS_WITH_SOCKS5)
|
2019-09-08 08:08:55 +01:00
|
|
|
post_rx:
|
2019-09-30 09:42:38 -07:00
|
|
|
#endif
|
2020-02-27 06:38:23 +00:00
|
|
|
if (n < 0) {
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_info(wsi, "LWS_CALLBACK_RAW_RX_fail");
|
2020-02-27 06:38:23 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lws_buflist_aware_finished_consuming(wsi, &ebuf, ebuf.len,
|
|
|
|
buffered, __func__))
|
|
|
|
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
2018-04-17 15:35:15 +08:00
|
|
|
|
2020-02-27 06:38:23 +00:00
|
|
|
goto try_pollout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nope:
|
|
|
|
if (wsi->favoured_pollin &&
|
|
|
|
(pollfd->revents & pollfd->events & LWS_POLLOUT))
|
|
|
|
/* we balanced the last favouring of pollin */
|
|
|
|
wsi->favoured_pollin = 0;
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
try_pollout:
|
|
|
|
|
|
|
|
if (!(pollfd->revents & LWS_POLLOUT))
|
|
|
|
return LWS_HPI_RET_HANDLED;
|
|
|
|
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2022-01-25 19:31:36 +00:00
|
|
|
if (lwsi_state(wsi) == LRS_WAITING_CONNECT) {
|
|
|
|
if (!lws_client_connect_3_connect(wsi, NULL, NULL, 0, NULL))
|
2019-12-02 06:55:37 +00:00
|
|
|
return LWS_HPI_RET_WSI_ALREADY_DIED;
|
2022-01-25 19:31:36 +00:00
|
|
|
|
|
|
|
if (lws_raw_skt_connect(wsi) < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-04-21 19:57:19 +01:00
|
|
|
#endif
|
|
|
|
|
2022-01-25 19:31:36 +00:00
|
|
|
if (lwsi_state(wsi) == LRS_WAITING_SSL)
|
|
|
|
return LWS_HPI_RET_HANDLED;
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
/* one shot */
|
2022-01-25 19:31:36 +00:00
|
|
|
if (lws_change_pollfd(wsi, LWS_POLLOUT, 0))
|
2018-04-11 13:39:42 +08:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* clear back-to-back write detection */
|
|
|
|
wsi->could_have_pending = 0;
|
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
n = user_callback_handle_rxflow(wsi->a.protocol->callback,
|
2018-04-11 13:39:42 +08:00
|
|
|
wsi, LWS_CALLBACK_RAW_WRITEABLE,
|
|
|
|
wsi->user_space, NULL, 0);
|
|
|
|
if (n < 0) {
|
|
|
|
lwsl_info("writeable_fail\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return LWS_HPI_RET_HANDLED;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS, "raw svc fail");
|
|
|
|
|
2018-05-02 08:46:16 +08:00
|
|
|
return LWS_HPI_RET_WSI_ALREADY_DIED;
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_SERVER)
|
2018-05-10 16:13:26 +08:00
|
|
|
static int
|
|
|
|
rops_adoption_bind_raw_skt(struct lws *wsi, int type, const char *vh_prot_name)
|
|
|
|
{
|
2020-09-20 09:14:46 +01:00
|
|
|
|
|
|
|
// lwsl_notice("%s: bind type %d\n", __func__, type);
|
|
|
|
|
2018-05-10 16:13:26 +08:00
|
|
|
/* no http but socket... must be raw skt */
|
|
|
|
if ((type & LWS_ADOPT_HTTP) || !(type & LWS_ADOPT_SOCKET) ||
|
2020-09-20 09:14:46 +01:00
|
|
|
((type & _LWS_ADOPT_FINISH) && (!(type & LWS_ADOPT_FLAG_UDP))))
|
2018-05-10 16:13:26 +08:00
|
|
|
return 0; /* no match */
|
|
|
|
|
2019-09-30 09:42:38 -07:00
|
|
|
#if defined(LWS_WITH_UDP)
|
2020-09-20 09:14:46 +01:00
|
|
|
if ((type & LWS_ADOPT_FLAG_UDP) && !wsi->udp) {
|
2018-05-10 16:13:26 +08:00
|
|
|
/*
|
|
|
|
* these can be >128 bytes, so just alloc for UDP
|
|
|
|
*/
|
|
|
|
wsi->udp = lws_malloc(sizeof(*wsi->udp), "udp struct");
|
2020-09-20 09:14:46 +01:00
|
|
|
if (!wsi->udp)
|
|
|
|
return 0;
|
|
|
|
memset(wsi->udp, 0, sizeof(*wsi->udp));
|
|
|
|
}
|
2018-05-10 16:13:26 +08:00
|
|
|
#endif
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2018-10-10 13:54:43 +08:00
|
|
|
lws_role_transition(wsi, 0, (type & LWS_ADOPT_ALLOW_SSL) ? LRS_SSL_INIT :
|
2018-08-18 14:11:29 +08:00
|
|
|
LRS_ESTABLISHED, &role_ops_raw_skt);
|
|
|
|
|
2018-05-21 14:43:40 +08:00
|
|
|
if (vh_prot_name)
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
lws_bind_protocol(wsi, wsi->a.protocol, __func__);
|
2018-05-10 16:13:26 +08:00
|
|
|
else
|
|
|
|
/* this is the only time he will transition */
|
|
|
|
lws_bind_protocol(wsi,
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
&wsi->a.vhost->protocols[wsi->a.vhost->raw_protocol_index],
|
2018-09-02 14:35:37 +08:00
|
|
|
__func__);
|
2018-05-10 16:13:26 +08:00
|
|
|
|
|
|
|
return 1; /* bound */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2018-04-11 13:39:42 +08:00
|
|
|
static int
|
2018-05-10 16:13:26 +08:00
|
|
|
rops_client_bind_raw_skt(struct lws *wsi,
|
|
|
|
const struct lws_client_connect_info *i)
|
2018-04-11 13:39:42 +08:00
|
|
|
{
|
2018-05-10 16:13:26 +08:00
|
|
|
if (!i) {
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2018-05-10 16:13:26 +08:00
|
|
|
/* finalize */
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2019-08-17 20:46:53 +01:00
|
|
|
if (!wsi->user_space && wsi->stash->cis[CIS_METHOD])
|
2018-05-10 16:13:26 +08:00
|
|
|
if (lws_ensure_user_space(wsi))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
2018-05-10 16:13:26 +08:00
|
|
|
/* we are a fallback if nothing else matched */
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2020-01-02 08:32:23 +00:00
|
|
|
if (!i->local_protocol_name ||
|
|
|
|
strcmp(i->local_protocol_name, "raw-proxy"))
|
2019-09-10 18:25:53 +01:00
|
|
|
lws_role_transition(wsi, LWSIFR_CLIENT, LRS_UNCONNECTED,
|
2018-05-10 16:13:26 +08:00
|
|
|
&role_ops_raw_skt);
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2018-05-10 16:13:26 +08:00
|
|
|
return 1; /* matched */
|
|
|
|
}
|
|
|
|
#endif
|
2018-04-11 13:39:42 +08:00
|
|
|
|
roles: compress role ops structs
role ops are usually only sparsely filled, there are currently 20
function pointers but several roles only fill in two. No single
role has more than 14 of the ops. On a 32/64 bit build this part
of the ops struct takes a fixed 80 / 160 bytes then.
First reduce the type of the callback reason part from uint16_t to
uint8_t, this saves 12 bytes unconditionally.
Change to a separate function pointer array with a nybble index
array, it costs 10 bytes for the index and a pointer to the
separate array, for 32-bit the cost is
2 + (4 x ops_used)
and for 64-bit
6 + (8 x ops_used)
for 2 x ops_used it means 32-bit: 10 vs 80 / 64-bit: 22 vs 160
For a typical system with h1 (9), h2 (14), listen (2), netlink (2),
pipe (1), raw_skt (3), ws (12), == 43 ops_used out of 140, it means
the .rodata for this reduced from 32-bit: 560 -> 174 (386 byte
saving) and 64-bit: 1120 -> 350 (770 byte saving)
This doesn't account for the changed function ops calling code, two
ways were tried, a preprocessor macro and explicit functions
For an x86_64 gcc 10 build with most options, release mode,
.text + .rodata
before patch: 553282
accessor macro: 552714 (568 byte saving)
accessor functions: 553674 (392 bytes worse than without patch)
therefore we went with the macros
2020-10-19 13:55:21 +01:00
|
|
|
static const lws_rops_t rops_table_raw_skt[] = {
|
|
|
|
/* 1 */ { .handle_POLLIN = rops_handle_POLLIN_raw_skt },
|
|
|
|
#if defined(LWS_WITH_SERVER)
|
|
|
|
/* 2 */ { .adoption_bind = rops_adoption_bind_raw_skt },
|
|
|
|
#else
|
2021-02-01 11:48:04 +00:00
|
|
|
/* 2 */ { .adoption_bind = NULL },
|
roles: compress role ops structs
role ops are usually only sparsely filled, there are currently 20
function pointers but several roles only fill in two. No single
role has more than 14 of the ops. On a 32/64 bit build this part
of the ops struct takes a fixed 80 / 160 bytes then.
First reduce the type of the callback reason part from uint16_t to
uint8_t, this saves 12 bytes unconditionally.
Change to a separate function pointer array with a nybble index
array, it costs 10 bytes for the index and a pointer to the
separate array, for 32-bit the cost is
2 + (4 x ops_used)
and for 64-bit
6 + (8 x ops_used)
for 2 x ops_used it means 32-bit: 10 vs 80 / 64-bit: 22 vs 160
For a typical system with h1 (9), h2 (14), listen (2), netlink (2),
pipe (1), raw_skt (3), ws (12), == 43 ops_used out of 140, it means
the .rodata for this reduced from 32-bit: 560 -> 174 (386 byte
saving) and 64-bit: 1120 -> 350 (770 byte saving)
This doesn't account for the changed function ops calling code, two
ways were tried, a preprocessor macro and explicit functions
For an x86_64 gcc 10 build with most options, release mode,
.text + .rodata
before patch: 553282
accessor macro: 552714 (568 byte saving)
accessor functions: 553674 (392 bytes worse than without patch)
therefore we went with the macros
2020-10-19 13:55:21 +01:00
|
|
|
#endif
|
|
|
|
#if defined(LWS_WITH_CLIENT)
|
|
|
|
/* 3 */ { .client_bind = rops_client_bind_raw_skt },
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2020-01-15 06:31:19 +00:00
|
|
|
const struct lws_role_ops role_ops_raw_skt = {
|
2018-04-12 15:56:38 +08:00
|
|
|
/* role name */ "raw-skt",
|
|
|
|
/* alpn id */ NULL,
|
roles: compress role ops structs
role ops are usually only sparsely filled, there are currently 20
function pointers but several roles only fill in two. No single
role has more than 14 of the ops. On a 32/64 bit build this part
of the ops struct takes a fixed 80 / 160 bytes then.
First reduce the type of the callback reason part from uint16_t to
uint8_t, this saves 12 bytes unconditionally.
Change to a separate function pointer array with a nybble index
array, it costs 10 bytes for the index and a pointer to the
separate array, for 32-bit the cost is
2 + (4 x ops_used)
and for 64-bit
6 + (8 x ops_used)
for 2 x ops_used it means 32-bit: 10 vs 80 / 64-bit: 22 vs 160
For a typical system with h1 (9), h2 (14), listen (2), netlink (2),
pipe (1), raw_skt (3), ws (12), == 43 ops_used out of 140, it means
the .rodata for this reduced from 32-bit: 560 -> 174 (386 byte
saving) and 64-bit: 1120 -> 350 (770 byte saving)
This doesn't account for the changed function ops calling code, two
ways were tried, a preprocessor macro and explicit functions
For an x86_64 gcc 10 build with most options, release mode,
.text + .rodata
before patch: 553282
accessor macro: 552714 (568 byte saving)
accessor functions: 553674 (392 bytes worse than without patch)
therefore we went with the macros
2020-10-19 13:55:21 +01:00
|
|
|
|
|
|
|
/* rops_table */ rops_table_raw_skt,
|
|
|
|
/* rops_idx */ {
|
|
|
|
/* LWS_ROPS_check_upgrades */
|
|
|
|
/* LWS_ROPS_pt_init_destroy */ 0x00,
|
|
|
|
/* LWS_ROPS_init_vhost */
|
|
|
|
/* LWS_ROPS_destroy_vhost */ 0x00,
|
|
|
|
/* LWS_ROPS_service_flag_pending */
|
|
|
|
/* LWS_ROPS_handle_POLLIN */ 0x01,
|
|
|
|
/* LWS_ROPS_handle_POLLOUT */
|
|
|
|
/* LWS_ROPS_perform_user_POLLOUT */ 0x00,
|
|
|
|
/* LWS_ROPS_callback_on_writable */
|
|
|
|
/* LWS_ROPS_tx_credit */ 0x00,
|
|
|
|
/* LWS_ROPS_write_role_protocol */
|
|
|
|
/* LWS_ROPS_encapsulation_parent */ 0x00,
|
|
|
|
/* LWS_ROPS_alpn_negotiated */
|
|
|
|
/* LWS_ROPS_close_via_role_protocol */ 0x00,
|
|
|
|
/* LWS_ROPS_close_role */
|
|
|
|
/* LWS_ROPS_close_kill_connection */ 0x00,
|
|
|
|
/* LWS_ROPS_destroy_role */
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_SERVER)
|
roles: compress role ops structs
role ops are usually only sparsely filled, there are currently 20
function pointers but several roles only fill in two. No single
role has more than 14 of the ops. On a 32/64 bit build this part
of the ops struct takes a fixed 80 / 160 bytes then.
First reduce the type of the callback reason part from uint16_t to
uint8_t, this saves 12 bytes unconditionally.
Change to a separate function pointer array with a nybble index
array, it costs 10 bytes for the index and a pointer to the
separate array, for 32-bit the cost is
2 + (4 x ops_used)
and for 64-bit
6 + (8 x ops_used)
for 2 x ops_used it means 32-bit: 10 vs 80 / 64-bit: 22 vs 160
For a typical system with h1 (9), h2 (14), listen (2), netlink (2),
pipe (1), raw_skt (3), ws (12), == 43 ops_used out of 140, it means
the .rodata for this reduced from 32-bit: 560 -> 174 (386 byte
saving) and 64-bit: 1120 -> 350 (770 byte saving)
This doesn't account for the changed function ops calling code, two
ways were tried, a preprocessor macro and explicit functions
For an x86_64 gcc 10 build with most options, release mode,
.text + .rodata
before patch: 553282
accessor macro: 552714 (568 byte saving)
accessor functions: 553674 (392 bytes worse than without patch)
therefore we went with the macros
2020-10-19 13:55:21 +01:00
|
|
|
/* LWS_ROPS_adoption_bind */ 0x02,
|
2018-05-10 16:13:26 +08:00
|
|
|
#else
|
roles: compress role ops structs
role ops are usually only sparsely filled, there are currently 20
function pointers but several roles only fill in two. No single
role has more than 14 of the ops. On a 32/64 bit build this part
of the ops struct takes a fixed 80 / 160 bytes then.
First reduce the type of the callback reason part from uint16_t to
uint8_t, this saves 12 bytes unconditionally.
Change to a separate function pointer array with a nybble index
array, it costs 10 bytes for the index and a pointer to the
separate array, for 32-bit the cost is
2 + (4 x ops_used)
and for 64-bit
6 + (8 x ops_used)
for 2 x ops_used it means 32-bit: 10 vs 80 / 64-bit: 22 vs 160
For a typical system with h1 (9), h2 (14), listen (2), netlink (2),
pipe (1), raw_skt (3), ws (12), == 43 ops_used out of 140, it means
the .rodata for this reduced from 32-bit: 560 -> 174 (386 byte
saving) and 64-bit: 1120 -> 350 (770 byte saving)
This doesn't account for the changed function ops calling code, two
ways were tried, a preprocessor macro and explicit functions
For an x86_64 gcc 10 build with most options, release mode,
.text + .rodata
before patch: 553282
accessor macro: 552714 (568 byte saving)
accessor functions: 553674 (392 bytes worse than without patch)
therefore we went with the macros
2020-10-19 13:55:21 +01:00
|
|
|
/* LWS_ROPS_adoption_bind */ 0x00,
|
2018-05-10 16:13:26 +08:00
|
|
|
#endif
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
roles: compress role ops structs
role ops are usually only sparsely filled, there are currently 20
function pointers but several roles only fill in two. No single
role has more than 14 of the ops. On a 32/64 bit build this part
of the ops struct takes a fixed 80 / 160 bytes then.
First reduce the type of the callback reason part from uint16_t to
uint8_t, this saves 12 bytes unconditionally.
Change to a separate function pointer array with a nybble index
array, it costs 10 bytes for the index and a pointer to the
separate array, for 32-bit the cost is
2 + (4 x ops_used)
and for 64-bit
6 + (8 x ops_used)
for 2 x ops_used it means 32-bit: 10 vs 80 / 64-bit: 22 vs 160
For a typical system with h1 (9), h2 (14), listen (2), netlink (2),
pipe (1), raw_skt (3), ws (12), == 43 ops_used out of 140, it means
the .rodata for this reduced from 32-bit: 560 -> 174 (386 byte
saving) and 64-bit: 1120 -> 350 (770 byte saving)
This doesn't account for the changed function ops calling code, two
ways were tried, a preprocessor macro and explicit functions
For an x86_64 gcc 10 build with most options, release mode,
.text + .rodata
before patch: 553282
accessor macro: 552714 (568 byte saving)
accessor functions: 553674 (392 bytes worse than without patch)
therefore we went with the macros
2020-10-19 13:55:21 +01:00
|
|
|
/* LWS_ROPS_client_bind */
|
|
|
|
/* LWS_ROPS_issue_keepalive */ 0x30,
|
2018-05-10 16:13:26 +08:00
|
|
|
#else
|
roles: compress role ops structs
role ops are usually only sparsely filled, there are currently 20
function pointers but several roles only fill in two. No single
role has more than 14 of the ops. On a 32/64 bit build this part
of the ops struct takes a fixed 80 / 160 bytes then.
First reduce the type of the callback reason part from uint16_t to
uint8_t, this saves 12 bytes unconditionally.
Change to a separate function pointer array with a nybble index
array, it costs 10 bytes for the index and a pointer to the
separate array, for 32-bit the cost is
2 + (4 x ops_used)
and for 64-bit
6 + (8 x ops_used)
for 2 x ops_used it means 32-bit: 10 vs 80 / 64-bit: 22 vs 160
For a typical system with h1 (9), h2 (14), listen (2), netlink (2),
pipe (1), raw_skt (3), ws (12), == 43 ops_used out of 140, it means
the .rodata for this reduced from 32-bit: 560 -> 174 (386 byte
saving) and 64-bit: 1120 -> 350 (770 byte saving)
This doesn't account for the changed function ops calling code, two
ways were tried, a preprocessor macro and explicit functions
For an x86_64 gcc 10 build with most options, release mode,
.text + .rodata
before patch: 553282
accessor macro: 552714 (568 byte saving)
accessor functions: 553674 (392 bytes worse than without patch)
therefore we went with the macros
2020-10-19 13:55:21 +01:00
|
|
|
/* LWS_ROPS_client_bind */
|
|
|
|
/* LWS_ROPS_issue_keepalive */ 0x00,
|
2018-05-10 16:13:26 +08:00
|
|
|
#endif
|
roles: compress role ops structs
role ops are usually only sparsely filled, there are currently 20
function pointers but several roles only fill in two. No single
role has more than 14 of the ops. On a 32/64 bit build this part
of the ops struct takes a fixed 80 / 160 bytes then.
First reduce the type of the callback reason part from uint16_t to
uint8_t, this saves 12 bytes unconditionally.
Change to a separate function pointer array with a nybble index
array, it costs 10 bytes for the index and a pointer to the
separate array, for 32-bit the cost is
2 + (4 x ops_used)
and for 64-bit
6 + (8 x ops_used)
for 2 x ops_used it means 32-bit: 10 vs 80 / 64-bit: 22 vs 160
For a typical system with h1 (9), h2 (14), listen (2), netlink (2),
pipe (1), raw_skt (3), ws (12), == 43 ops_used out of 140, it means
the .rodata for this reduced from 32-bit: 560 -> 174 (386 byte
saving) and 64-bit: 1120 -> 350 (770 byte saving)
This doesn't account for the changed function ops calling code, two
ways were tried, a preprocessor macro and explicit functions
For an x86_64 gcc 10 build with most options, release mode,
.text + .rodata
before patch: 553282
accessor macro: 552714 (568 byte saving)
accessor functions: 553674 (392 bytes worse than without patch)
therefore we went with the macros
2020-10-19 13:55:21 +01:00
|
|
|
},
|
|
|
|
|
2019-04-21 19:57:19 +01:00
|
|
|
/* adoption_cb clnt, srv */ { LWS_CALLBACK_RAW_CONNECTED,
|
2018-11-29 08:29:48 +08:00
|
|
|
LWS_CALLBACK_RAW_ADOPT },
|
|
|
|
/* rx_cb clnt, srv */ { LWS_CALLBACK_RAW_RX,
|
|
|
|
LWS_CALLBACK_RAW_RX },
|
2019-04-21 19:57:19 +01:00
|
|
|
/* writeable cb clnt, srv */ { LWS_CALLBACK_RAW_WRITEABLE,
|
|
|
|
LWS_CALLBACK_RAW_WRITEABLE},
|
|
|
|
/* close cb clnt, srv */ { LWS_CALLBACK_RAW_CLOSE,
|
|
|
|
LWS_CALLBACK_RAW_CLOSE },
|
2018-08-18 14:11:29 +08:00
|
|
|
/* protocol_bind cb c, srv */ { LWS_CALLBACK_RAW_SKT_BIND_PROTOCOL,
|
|
|
|
LWS_CALLBACK_RAW_SKT_BIND_PROTOCOL },
|
|
|
|
/* protocol_unbind cb c, srv */ { LWS_CALLBACK_RAW_SKT_DROP_PROTOCOL,
|
|
|
|
LWS_CALLBACK_RAW_SKT_DROP_PROTOCOL },
|
2018-04-29 10:44:36 +08:00
|
|
|
/* file_handle */ 0,
|
2018-04-11 13:39:42 +08:00
|
|
|
};
|