2019-01-13 06:58:21 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
2019-01-13 06:58:21 +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:
|
2019-01-13 06:58:21 +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.
|
2019-01-13 06:58:21 +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.
|
2019-01-13 06:58:21 +08:00
|
|
|
*/
|
|
|
|
|
2019-08-15 10:49:52 +01:00
|
|
|
#include "private-lib-core.h"
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2019-08-26 18:41:40 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
|
|
|
static int
|
|
|
|
lws_close_trans_q_leader(struct lws_dll2 *d, void *user)
|
|
|
|
{
|
|
|
|
struct lws *w = lws_container_of(d, struct lws, dll2_cli_txn_queue);
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
__lws_close_free_wsi(w, (enum lws_close_status)-1, "trans q leader closing");
|
2019-08-26 18:41:40 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-13 06:58:21 +08:00
|
|
|
void
|
2019-08-26 18:41:40 +01:00
|
|
|
__lws_reset_wsi(struct lws *wsi)
|
2019-01-13 06:58:21 +08:00
|
|
|
{
|
|
|
|
if (!wsi)
|
|
|
|
return;
|
|
|
|
|
2019-08-26 18:41:40 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
|
|
|
|
|
|
|
lws_free_set_NULL(wsi->cli_hostname_copy);
|
|
|
|
|
2021-03-15 05:20:15 +00:00
|
|
|
#if defined(LWS_WITH_CONMON)
|
|
|
|
|
|
|
|
if (wsi->conmon.dns_results_copy) {
|
|
|
|
lws_conmon_addrinfo_destroy(wsi->conmon.dns_results_copy);
|
|
|
|
wsi->conmon.dns_results_copy = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
wsi->conmon.ciu_dns =
|
|
|
|
wsi->conmon.ciu_sockconn =
|
|
|
|
wsi->conmon.ciu_tls =
|
|
|
|
wsi->conmon.ciu_txn_resp = 0;
|
|
|
|
#endif
|
|
|
|
|
2019-08-26 18:41:40 +01:00
|
|
|
/*
|
|
|
|
* if we have wsi in our transaction queue, if we are closing we
|
|
|
|
* must go through and close all those first
|
|
|
|
*/
|
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
|
|
|
if (wsi->a.vhost) {
|
2019-08-26 18:41:40 +01:00
|
|
|
|
|
|
|
/* we are no longer an active client connection that can piggyback */
|
|
|
|
lws_dll2_remove(&wsi->dll_cli_active_conns);
|
|
|
|
|
|
|
|
lws_dll2_foreach_safe(&wsi->dll2_cli_txn_queue_owner, NULL,
|
|
|
|
lws_close_trans_q_leader);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* !!! If we are closing, but we have pending pipelined
|
|
|
|
* transaction results we already sent headers for, that's going
|
|
|
|
* to destroy sync for HTTP/1 and leave H2 stream with no live
|
|
|
|
* swsi.`
|
|
|
|
*
|
|
|
|
* However this is normal if we are being closed because the
|
|
|
|
* transaction queue leader is closing.
|
|
|
|
*/
|
|
|
|
lws_dll2_remove(&wsi->dll2_cli_txn_queue);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-08-31 08:52:54 +01:00
|
|
|
if (wsi->a.vhost) {
|
|
|
|
lws_vhost_lock(wsi->a.vhost);
|
2019-09-08 08:08:55 +01:00
|
|
|
lws_dll2_remove(&wsi->vh_awaiting_socket);
|
2020-08-31 08:52:54 +01:00
|
|
|
lws_vhost_unlock(wsi->a.vhost);
|
|
|
|
}
|
2019-09-08 08:08:55 +01:00
|
|
|
|
2019-01-13 06:58:21 +08:00
|
|
|
/*
|
|
|
|
* Protocol user data may be allocated either internally by lws
|
|
|
|
* or by specified the user. We should only free what we allocated.
|
|
|
|
*/
|
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
|
|
|
if (wsi->a.protocol && wsi->a.protocol->per_session_data_size &&
|
2020-07-15 10:25:18 +01:00
|
|
|
wsi->user_space && !wsi->user_space_externally_allocated) {
|
|
|
|
/* confirm no sul left scheduled in user data itself */
|
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_sul_debug_zombies(wsi->a.context, wsi->user_space,
|
|
|
|
wsi->a.protocol->per_session_data_size, __func__);
|
2020-02-25 13:28:25 +00:00
|
|
|
lws_free_set_NULL(wsi->user_space);
|
2020-07-15 10:25:18 +01:00
|
|
|
}
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2020-05-18 06:14:19 +01:00
|
|
|
/*
|
|
|
|
* Don't let buflist content or state from the wsi's previous life
|
|
|
|
* carry over to the new life
|
|
|
|
*/
|
|
|
|
|
2019-01-13 06:58:21 +08:00
|
|
|
lws_buflist_destroy_all_segments(&wsi->buflist);
|
2020-05-18 06:14:19 +01:00
|
|
|
lws_dll2_remove(&wsi->dll_buflist);
|
2019-01-13 06:58:21 +08:00
|
|
|
lws_buflist_destroy_all_segments(&wsi->buflist_out);
|
2019-09-30 09:42:38 -07:00
|
|
|
#if defined(LWS_WITH_UDP)
|
2020-07-15 10:25:18 +01:00
|
|
|
if (wsi->udp) {
|
|
|
|
/* confirm no sul left scheduled in wsi->udp itself */
|
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_sul_debug_zombies(wsi->a.context, wsi->udp,
|
2020-07-15 10:25:18 +01:00
|
|
|
sizeof(*wsi->udp), "close udp wsi");
|
|
|
|
lws_free_set_NULL(wsi->udp);
|
|
|
|
}
|
2019-09-30 09:42:38 -07:00
|
|
|
#endif
|
2019-09-08 08:08:55 +01:00
|
|
|
wsi->retry = 0;
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2019-08-26 18:41:40 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
|
|
|
lws_dll2_remove(&wsi->dll2_cli_txn_queue);
|
|
|
|
lws_dll2_remove(&wsi->dll_cli_active_conns);
|
2021-06-17 10:07:04 +01:00
|
|
|
if (wsi->cli_hostname_copy)
|
|
|
|
lws_free_set_NULL(wsi->cli_hostname_copy);
|
2019-08-26 18:41:40 +01:00
|
|
|
#endif
|
|
|
|
|
2019-09-19 09:48:17 +01:00
|
|
|
#if defined(LWS_WITH_SYS_ASYNC_DNS)
|
2019-08-26 18:41:40 +01:00
|
|
|
lws_async_dns_cancel(wsi);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_HTTP_PROXY)
|
|
|
|
if (wsi->http.buflist_post_body)
|
|
|
|
lws_buflist_destroy_all_segments(&wsi->http.buflist_post_body);
|
|
|
|
#endif
|
|
|
|
|
2021-06-21 08:50:13 +01:00
|
|
|
#if defined(LWS_WITH_SERVER)
|
|
|
|
lws_dll2_remove(&wsi->listen_list);
|
|
|
|
#endif
|
|
|
|
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
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
|
|
|
if (wsi->a.vhost)
|
2019-08-08 16:58:55 +01:00
|
|
|
lws_dll2_remove(&wsi->dll_cli_active_conns);
|
2019-01-13 06:58:21 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
__lws_same_vh_protocol_remove(wsi);
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2021-06-17 10:07:04 +01:00
|
|
|
//lws_free_set_NULL(wsi->stash);
|
2019-03-20 07:39:55 +08:00
|
|
|
lws_free_set_NULL(wsi->cli_hostname_copy);
|
2019-01-13 06:58:21 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_PEER_LIMITS)
|
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_peer_track_wsi_close(wsi->a.context, wsi->peer);
|
2019-01-13 06:58:21 +08:00
|
|
|
wsi->peer = NULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* since we will destroy the wsi, make absolutely sure now */
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_OPENSSL)
|
|
|
|
__lws_ssl_remove_wsi_from_buffered_list(wsi);
|
|
|
|
#endif
|
2019-08-09 10:12:09 +01:00
|
|
|
__lws_wsi_remove_from_sul(wsi);
|
2019-01-13 06:58:21 +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
|
|
|
if (lws_rops_fidx(wsi->role_ops, LWS_ROPS_destroy_role))
|
|
|
|
lws_rops_func_fidx(wsi->role_ops,
|
|
|
|
LWS_ROPS_destroy_role).destroy_role(wsi);
|
2019-08-26 18:41:40 +01:00
|
|
|
|
|
|
|
#if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
|
|
|
|
__lws_header_table_detach(wsi, 0);
|
|
|
|
#endif
|
2021-06-17 10:07:04 +01:00
|
|
|
|
|
|
|
#if defined(LWS_ROLE_H2)
|
|
|
|
/*
|
|
|
|
* Let's try to clean out the h2-ness of the wsi
|
|
|
|
*/
|
|
|
|
|
|
|
|
memset(&wsi->h2, 0, sizeof(wsi->h2));
|
|
|
|
|
|
|
|
wsi->hdr_parsing_completed = wsi->mux_substream =
|
|
|
|
wsi->upgraded_to_http2 = wsi->mux_stream_immortal =
|
|
|
|
wsi->h2_acked_settings = wsi->seen_nonpseudoheader =
|
|
|
|
wsi->socket_is_permanently_unusable = wsi->favoured_pollin =
|
|
|
|
wsi->already_did_cce = wsi->told_user_closed =
|
|
|
|
wsi->waiting_to_send_close_frame = wsi->close_needs_ack =
|
|
|
|
wsi->parent_pending_cb_on_writable = wsi->seen_zero_length_recv =
|
|
|
|
wsi->close_when_buffered_out_drained = wsi->could_have_pending = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_CLIENT)
|
|
|
|
wsi->do_ws = wsi->chunked = wsi->client_rx_avail =
|
|
|
|
wsi->client_http_body_pending = wsi->transaction_from_pipeline_queue =
|
|
|
|
wsi->keepalive_active = wsi->keepalive_rejected =
|
|
|
|
wsi->redirected_to_get = wsi->client_pipeline = wsi->client_h2_alpn =
|
|
|
|
wsi->client_mux_substream = wsi->client_mux_migrated =
|
|
|
|
wsi->tls_session_reused = wsi->perf_done = 0;
|
|
|
|
|
|
|
|
wsi->immortal_substream_count = 0;
|
|
|
|
#endif
|
2019-08-26 18:41:40 +01:00
|
|
|
}
|
|
|
|
|
2021-04-04 04:06:24 +01:00
|
|
|
/* req cx lock */
|
|
|
|
|
2019-08-26 18:41:40 +01:00
|
|
|
void
|
|
|
|
__lws_free_wsi(struct lws *wsi)
|
|
|
|
{
|
2021-04-04 04:06:24 +01:00
|
|
|
struct lws_vhost *vh;
|
|
|
|
|
2019-08-26 18:41:40 +01:00
|
|
|
if (!wsi)
|
|
|
|
return;
|
|
|
|
|
2021-04-04 04:06:24 +01:00
|
|
|
lws_context_assert_lock_held(wsi->a.context);
|
|
|
|
|
2020-12-21 23:16:10 +00:00
|
|
|
#if defined(LWS_WITH_SECURE_STREAMS)
|
2020-11-24 08:41:57 +00:00
|
|
|
if (wsi->for_ss) {
|
|
|
|
|
2021-06-26 09:22:21 +01:00
|
|
|
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
|
|
|
|
if (wsi->client_bound_sspc) {
|
|
|
|
lws_sspc_handle_t *h = (lws_sspc_handle_t *)
|
|
|
|
wsi->a.opaque_user_data;
|
|
|
|
if (h) {
|
|
|
|
h->cwsi = NULL;
|
|
|
|
wsi->a.opaque_user_data = NULL;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Make certain it is disconnected from the ss by now
|
|
|
|
*/
|
|
|
|
lws_ss_handle_t *h = (lws_ss_handle_t *)
|
|
|
|
wsi->a.opaque_user_data;
|
|
|
|
|
|
|
|
if (h) {
|
|
|
|
h->wsi = NULL;
|
|
|
|
wsi->a.opaque_user_data = NULL;
|
|
|
|
}
|
2020-11-24 08:41:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-06-17 10:07:04 +01:00
|
|
|
vh = wsi->a.vhost;
|
|
|
|
|
2019-08-26 18:41:40 +01:00
|
|
|
__lws_reset_wsi(wsi);
|
2020-04-14 19:04:13 +01:00
|
|
|
__lws_wsi_remove_from_sul(wsi);
|
2019-08-26 18:41:40 +01:00
|
|
|
|
2021-06-17 10:07:04 +01:00
|
|
|
if (vh)
|
|
|
|
/* this may destroy vh */
|
|
|
|
__lws_vhost_unbind_wsi(wsi); /* req cx + vh lock */
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_CLIENT)
|
|
|
|
if (wsi->stash)
|
|
|
|
lws_free_set_NULL(wsi->stash);
|
|
|
|
#endif
|
2021-04-04 04:06:24 +01:00
|
|
|
|
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
|
|
|
if (wsi->a.context->event_loop_ops->destroy_wsi)
|
|
|
|
wsi->a.context->event_loop_ops->destroy_wsi(wsi);
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_debug(wsi, "tsi fds count %d\n",
|
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.context->pt[(int)wsi->tsi].fds_count);
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2020-07-15 10:25:18 +01:00
|
|
|
/* confirm no sul left scheduled in wsi itself */
|
2021-06-26 08:28:04 +01:00
|
|
|
lws_sul_debug_zombies(wsi->a.context, wsi, sizeof(*wsi), __func__);
|
2020-07-15 10:25:18 +01:00
|
|
|
|
2021-06-26 17:24:19 +01:00
|
|
|
__lws_lc_untag(wsi->a.context, &wsi->lc);
|
2019-01-13 06:58:21 +08:00
|
|
|
lws_free(wsi);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
lws_remove_child_from_any_parent(struct lws *wsi)
|
|
|
|
{
|
|
|
|
struct lws **pwsi;
|
|
|
|
int seen = 0;
|
|
|
|
|
|
|
|
if (!wsi->parent)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* detach ourselves from parent's child list */
|
|
|
|
pwsi = &wsi->parent->child_list;
|
|
|
|
while (*pwsi) {
|
|
|
|
if (*pwsi == wsi) {
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_info(wsi, "detach from parent %s",
|
|
|
|
lws_wsi_tag(wsi->parent));
|
2019-01-13 06:58:21 +08:00
|
|
|
|
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
|
|
|
if (wsi->parent->a.protocol)
|
|
|
|
wsi->parent->a.protocol->callback(wsi,
|
2019-01-13 06:58:21 +08:00
|
|
|
LWS_CALLBACK_CHILD_CLOSING,
|
|
|
|
wsi->parent->user_space, wsi, 0);
|
|
|
|
|
|
|
|
*pwsi = wsi->sibling_list;
|
|
|
|
seen = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pwsi = &(*pwsi)->sibling_list;
|
|
|
|
}
|
|
|
|
if (!seen)
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_err(wsi, "failed to detach from parent");
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
wsi->parent = NULL;
|
|
|
|
}
|
|
|
|
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2019-08-10 09:20:27 +01:00
|
|
|
void
|
|
|
|
lws_inform_client_conn_fail(struct lws *wsi, void *arg, size_t len)
|
|
|
|
{
|
2019-09-19 06:54:53 +01:00
|
|
|
lws_addrinfo_clean(wsi);
|
|
|
|
|
2019-08-10 09:20:27 +01:00
|
|
|
if (wsi->already_did_cce)
|
|
|
|
return;
|
|
|
|
|
|
|
|
wsi->already_did_cce = 1;
|
|
|
|
|
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
|
|
|
if (!wsi->a.protocol)
|
2019-08-10 09:20:27 +01:00
|
|
|
return;
|
|
|
|
|
2020-06-26 09:58:20 +01:00
|
|
|
if (!wsi->client_suppress_CONNECTION_ERROR)
|
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.protocol->callback(wsi,
|
2020-06-26 09:58:20 +01:00
|
|
|
LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
|
|
|
|
wsi->user_space, arg, len);
|
2019-08-10 09:20:27 +01:00
|
|
|
}
|
2019-03-20 07:39:55 +08:00
|
|
|
#endif
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2019-09-19 06:54:53 +01:00
|
|
|
void
|
|
|
|
lws_addrinfo_clean(struct lws *wsi)
|
|
|
|
{
|
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2020-09-20 09:14:46 +01:00
|
|
|
struct lws_dll2 *d = lws_dll2_get_head(&wsi->dns_sorted_list), *d1;
|
2019-09-19 06:54:53 +01:00
|
|
|
|
2020-09-20 09:14:46 +01:00
|
|
|
while (d) {
|
|
|
|
lws_dns_sort_t *r = lws_container_of(d, lws_dns_sort_t, list);
|
|
|
|
|
|
|
|
d1 = d->next;
|
|
|
|
lws_dll2_remove(d);
|
|
|
|
lws_free(r);
|
|
|
|
|
|
|
|
d = d1;
|
|
|
|
}
|
2019-09-19 06:54:53 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-08-27 13:08:03 +01:00
|
|
|
/* requires cx and pt lock */
|
2021-04-04 04:06:24 +01:00
|
|
|
|
2019-01-13 06:58:21 +08:00
|
|
|
void
|
|
|
|
__lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason,
|
|
|
|
const char *caller)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt;
|
2020-02-29 12:37:24 +00:00
|
|
|
const struct lws_protocols *pro;
|
2019-01-13 06:58:21 +08:00
|
|
|
struct lws_context *context;
|
2020-02-29 12:37:24 +00:00
|
|
|
struct lws *wsi1, *wsi2;
|
2019-09-08 08:08:55 +01:00
|
|
|
int n, ccb;
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
if (!wsi)
|
|
|
|
return;
|
|
|
|
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_info(wsi, "caller: %s", caller);
|
2021-06-26 17:24:19 +01:00
|
|
|
|
2019-01-13 06:58:21 +08:00
|
|
|
lws_access_log(wsi);
|
|
|
|
|
2021-06-26 17:24:19 +01:00
|
|
|
if (!lws_dll2_is_detached(&wsi->dll_buflist))
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_info(wsi, "going down with stuff in buflist");
|
2019-09-11 14:43:09 +01:00
|
|
|
|
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
|
|
|
context = wsi->a.context;
|
2019-01-13 06:58:21 +08:00
|
|
|
pt = &context->pt[(int)wsi->tsi];
|
2020-08-31 08:12:16 +01:00
|
|
|
|
2021-06-29 13:30:48 +01:00
|
|
|
if (pt->pipe_wsi == wsi)
|
|
|
|
pt->pipe_wsi = NULL;
|
|
|
|
|
2021-01-06 15:08:22 +00:00
|
|
|
#if defined(LWS_WITH_SYS_METRICS) && \
|
|
|
|
(defined(LWS_WITH_CLIENT) || defined(LWS_WITH_SERVER))
|
|
|
|
/* wsi level: only reports if dangling caliper */
|
|
|
|
if (wsi->cal_conn.mt && wsi->cal_conn.us_start) {
|
|
|
|
if ((lws_metrics_priv_to_pub(wsi->cal_conn.mt)->flags) & LWSMTFL_REPORT_HIST) {
|
|
|
|
lws_metrics_caliper_report_hist(wsi->cal_conn, (struct lws *)NULL);
|
|
|
|
} else {
|
|
|
|
lws_metrics_caliper_report(wsi->cal_conn, METRES_NOGO);
|
|
|
|
lws_metrics_caliper_done(wsi->cal_conn);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
lws_metrics_caliper_done(wsi->cal_conn);
|
|
|
|
#endif
|
|
|
|
|
2020-09-20 09:14:46 +01:00
|
|
|
#if defined(LWS_WITH_SYS_ASYNC_DNS)
|
|
|
|
if (wsi == context->async_dns.wsi)
|
|
|
|
context->async_dns.wsi = NULL;
|
|
|
|
#endif
|
|
|
|
|
2020-08-31 08:12:16 +01:00
|
|
|
lws_pt_assert_lock_held(pt);
|
|
|
|
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2019-03-20 07:39:55 +08:00
|
|
|
lws_free_set_NULL(wsi->cli_hostname_copy);
|
2021-06-17 10:07:04 +01:00
|
|
|
wsi->client_mux_substream_was = wsi->client_mux_substream;
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2019-09-19 06:54:53 +01:00
|
|
|
lws_addrinfo_clean(wsi);
|
2019-01-13 06:58:21 +08:00
|
|
|
#endif
|
|
|
|
|
2019-09-13 10:33:24 +01:00
|
|
|
#if defined(LWS_WITH_HTTP2)
|
2019-12-23 11:31:57 +00:00
|
|
|
if (wsi->mux_stream_immortal)
|
2019-09-13 10:33:24 +01:00
|
|
|
lws_http_close_immortal(wsi);
|
|
|
|
#endif
|
|
|
|
|
2019-01-13 06:58:21 +08:00
|
|
|
/* if we have children, close them first */
|
|
|
|
if (wsi->child_list) {
|
|
|
|
wsi2 = wsi->child_list;
|
|
|
|
while (wsi2) {
|
|
|
|
wsi1 = wsi2->sibling_list;
|
2020-04-14 19:04:13 +01:00
|
|
|
// wsi2->parent = NULL;
|
2019-01-13 06:58:21 +08:00
|
|
|
/* stop it doing shutdown processing */
|
|
|
|
wsi2->socket_is_permanently_unusable = 1;
|
|
|
|
__lws_close_free_wsi(wsi2, reason,
|
|
|
|
"general child recurse");
|
|
|
|
wsi2 = wsi1;
|
|
|
|
}
|
|
|
|
wsi->child_list = NULL;
|
|
|
|
}
|
|
|
|
|
2020-02-28 15:50:15 +00:00
|
|
|
#if defined(LWS_ROLE_RAW_FILE)
|
2019-01-13 06:58:21 +08:00
|
|
|
if (wsi->role_ops == &role_ops_raw_file) {
|
|
|
|
lws_remove_child_from_any_parent(wsi);
|
|
|
|
__remove_wsi_socket_from_fds(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
|
|
|
if (wsi->a.protocol)
|
|
|
|
wsi->a.protocol->callback(wsi, wsi->role_ops->close_cb[0],
|
2019-01-13 06:58:21 +08:00
|
|
|
wsi->user_space, NULL, 0);
|
|
|
|
goto async_close;
|
|
|
|
}
|
2020-02-28 15:50:15 +00:00
|
|
|
#endif
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
wsi->wsistate_pre_close = wsi->wsistate;
|
|
|
|
|
|
|
|
#ifdef LWS_WITH_CGI
|
|
|
|
if (wsi->role_ops == &role_ops_cgi) {
|
2019-02-17 10:46:30 +08:00
|
|
|
|
2020-02-12 10:12:39 +00:00
|
|
|
// lwsl_debug("%s: closing stdwsi index %d\n", __func__, (int)wsi->lsp_channel);
|
2019-02-17 10:46:30 +08:00
|
|
|
|
2019-01-13 06:58:21 +08:00
|
|
|
/* we are not a network connection, but a handler for CGI io */
|
|
|
|
if (wsi->parent && wsi->parent->http.cgi) {
|
|
|
|
|
2020-07-17 13:54:13 +01:00
|
|
|
/*
|
|
|
|
* We need to keep the logical cgi around so we can
|
|
|
|
* drain it
|
|
|
|
*/
|
|
|
|
|
|
|
|
// if (wsi->parent->child_list == wsi && !wsi->sibling_list)
|
|
|
|
// lws_cgi_remove_and_kill(wsi->parent);
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2020-10-11 07:43:46 +01:00
|
|
|
/* end the binding between us and network connection */
|
2020-04-14 19:04:13 +01:00
|
|
|
if (wsi->parent->http.cgi && wsi->parent->http.cgi->lsp)
|
2020-02-12 10:12:39 +00:00
|
|
|
wsi->parent->http.cgi->lsp->stdwsi[(int)wsi->lsp_channel] =
|
2019-01-13 06:58:21 +08:00
|
|
|
NULL;
|
|
|
|
}
|
|
|
|
wsi->socket_is_permanently_unusable = 1;
|
|
|
|
|
|
|
|
goto just_kill_connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wsi->http.cgi)
|
|
|
|
lws_cgi_remove_and_kill(wsi);
|
|
|
|
#endif
|
|
|
|
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2021-06-17 10:07:04 +01:00
|
|
|
if (!wsi->close_is_redirect)
|
|
|
|
lws_free_set_NULL(wsi->stash);
|
2019-01-13 06:58:21 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (wsi->role_ops == &role_ops_raw_skt) {
|
|
|
|
wsi->socket_is_permanently_unusable = 1;
|
|
|
|
goto just_kill_connection;
|
|
|
|
}
|
2019-08-18 10:35:43 +01:00
|
|
|
#if defined(LWS_WITH_FILE_OPS) && (defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2))
|
2019-01-13 06:58:21 +08:00
|
|
|
if (lwsi_role_http(wsi) && lwsi_role_server(wsi) &&
|
|
|
|
wsi->http.fop_fd != NULL)
|
|
|
|
lws_vfs_file_close(&wsi->http.fop_fd);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (lwsi_state(wsi) == LRS_DEAD_SOCKET)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (wsi->socket_is_permanently_unusable ||
|
|
|
|
reason == LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY ||
|
|
|
|
lwsi_state(wsi) == LRS_SHUTDOWN)
|
|
|
|
goto just_kill_connection;
|
|
|
|
|
|
|
|
switch (lwsi_state_PRE_CLOSE(wsi)) {
|
|
|
|
case LRS_DEAD_SOCKET:
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* we tried the polite way... */
|
|
|
|
case LRS_WAITING_TO_SEND_CLOSE:
|
|
|
|
case LRS_AWAITING_CLOSE_ACK:
|
|
|
|
case LRS_RETURNED_CLOSE:
|
|
|
|
goto just_kill_connection;
|
|
|
|
|
|
|
|
case LRS_FLUSHING_BEFORE_CLOSE:
|
|
|
|
if (lws_has_buffered_out(wsi)
|
|
|
|
#if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
|
|
|
|
|| wsi->http.comp_ctx.buflist_comp ||
|
|
|
|
wsi->http.comp_ctx.may_have_more
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
lws_callback_on_writable(wsi);
|
|
|
|
return;
|
|
|
|
}
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_info(wsi, " end LRS_FLUSHING_BEFORE_CLOSE");
|
2019-01-13 06:58:21 +08:00
|
|
|
goto just_kill_connection;
|
|
|
|
default:
|
|
|
|
if (lws_has_buffered_out(wsi)
|
|
|
|
#if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
|
|
|
|
|| wsi->http.comp_ctx.buflist_comp ||
|
|
|
|
wsi->http.comp_ctx.may_have_more
|
|
|
|
#endif
|
|
|
|
) {
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_info(wsi, "LRS_FLUSHING_BEFORE_CLOSE");
|
2019-01-13 06:58:21 +08:00
|
|
|
lwsi_set_state(wsi, LRS_FLUSHING_BEFORE_CLOSE);
|
|
|
|
__lws_set_timeout(wsi,
|
|
|
|
PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE, 5);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lwsi_state(wsi) == LRS_WAITING_CONNECT ||
|
2019-09-19 06:54:53 +01:00
|
|
|
lwsi_state(wsi) == LRS_WAITING_DNS ||
|
2019-01-13 06:58:21 +08:00
|
|
|
lwsi_state(wsi) == LRS_H1C_ISSUE_HANDSHAKE)
|
|
|
|
goto just_kill_connection;
|
|
|
|
|
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
|
|
|
if (!wsi->told_user_closed && wsi->user_space && wsi->a.protocol &&
|
2019-01-13 06:58:21 +08:00
|
|
|
wsi->protocol_bind_balance) {
|
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.protocol->callback(wsi,
|
2019-01-13 06:58:21 +08:00
|
|
|
wsi->role_ops->protocol_unbind_cb[
|
|
|
|
!!lwsi_role_server(wsi)],
|
|
|
|
wsi->user_space, (void *)__func__, 0);
|
|
|
|
wsi->protocol_bind_balance = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* signal we are closing, lws_write will
|
|
|
|
* add any necessary version-specific stuff. If the write fails,
|
|
|
|
* no worries we are closing anyway. If we didn't initiate this
|
|
|
|
* close, then our state has been changed to
|
|
|
|
* LRS_RETURNED_CLOSE and we will skip this.
|
|
|
|
*
|
|
|
|
* Likewise if it's a second call to close this connection after we
|
|
|
|
* sent the close indication to the peer already, we are in state
|
|
|
|
* LRS_AWAITING_CLOSE_ACK and will skip doing this a second time.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
if (lws_rops_fidx(wsi->role_ops, LWS_ROPS_close_via_role_protocol) &&
|
|
|
|
lws_rops_func_fidx(wsi->role_ops, LWS_ROPS_close_via_role_protocol).
|
2020-12-27 19:34:30 +00:00
|
|
|
close_via_role_protocol(wsi, reason)) {
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_info(wsi, "close_via_role took over (sockfd %d)",
|
|
|
|
wsi->desc.sockfd);
|
2019-01-13 06:58:21 +08:00
|
|
|
return;
|
2020-12-27 19:34:30 +00:00
|
|
|
}
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
just_kill_connection:
|
|
|
|
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_debug(wsi, "real just_kill_connection A: (sockfd %d)",
|
|
|
|
wsi->desc.sockfd);
|
2020-12-27 19:34:30 +00:00
|
|
|
|
2022-04-11 06:42:53 +01:00
|
|
|
#if defined(LWS_WITH_THREADPOOL) && defined(LWS_HAVE_PTHREAD_H)
|
2021-06-19 16:59:11 +01:00
|
|
|
lws_threadpool_wsi_closing(wsi);
|
|
|
|
#endif
|
|
|
|
|
2019-11-16 03:06:51 +00:00
|
|
|
#if defined(LWS_WITH_FILE_OPS) && (defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2))
|
|
|
|
if (lwsi_role_http(wsi) && lwsi_role_server(wsi) &&
|
|
|
|
wsi->http.fop_fd != NULL)
|
|
|
|
lws_vfs_file_close(&wsi->http.fop_fd);
|
|
|
|
#endif
|
|
|
|
|
2020-10-09 21:29:07 +01:00
|
|
|
lws_sul_cancel(&wsi->sul_connect_timeout);
|
2019-09-19 06:54:53 +01:00
|
|
|
#if defined(LWS_WITH_SYS_ASYNC_DNS)
|
|
|
|
lws_async_dns_cancel(wsi);
|
|
|
|
#endif
|
|
|
|
|
2019-03-22 06:22:40 +08:00
|
|
|
#if defined(LWS_WITH_HTTP_PROXY)
|
|
|
|
if (wsi->http.buflist_post_body)
|
|
|
|
lws_buflist_destroy_all_segments(&wsi->http.buflist_post_body);
|
|
|
|
#endif
|
2019-09-30 09:42:38 -07:00
|
|
|
#if defined(LWS_WITH_UDP)
|
2020-07-15 10:25:18 +01:00
|
|
|
if (wsi->udp) {
|
|
|
|
/* confirm no sul left scheduled in wsi->udp itself */
|
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_sul_debug_zombies(wsi->a.context, wsi->udp,
|
2020-07-15 10:25:18 +01:00
|
|
|
sizeof(*wsi->udp), "close udp wsi");
|
|
|
|
|
2019-09-08 08:08:55 +01:00
|
|
|
lws_free_set_NULL(wsi->udp);
|
2020-07-15 10:25:18 +01:00
|
|
|
}
|
2019-09-30 09:42:38 -07:00
|
|
|
#endif
|
2019-03-22 06:22:40 +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
|
|
|
if (lws_rops_fidx(wsi->role_ops, LWS_ROPS_close_kill_connection))
|
|
|
|
lws_rops_func_fidx(wsi->role_ops,
|
|
|
|
LWS_ROPS_close_kill_connection).
|
|
|
|
close_kill_connection(wsi, reason);
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
if (!wsi->told_user_closed && wsi->user_space &&
|
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->protocol_bind_balance && wsi->a.protocol) {
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_debug("%s: %s: DROP_PROTOCOL %s\n", __func__, lws_wsi_tag(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.protocol ? wsi->a.protocol->name: "NULL");
|
|
|
|
if (wsi->a.protocol)
|
|
|
|
wsi->a.protocol->callback(wsi,
|
2019-01-13 06:58:21 +08:00
|
|
|
wsi->role_ops->protocol_unbind_cb[
|
|
|
|
!!lwsi_role_server(wsi)],
|
|
|
|
wsi->user_space, (void *)__func__, 0);
|
|
|
|
wsi->protocol_bind_balance = 0;
|
|
|
|
}
|
|
|
|
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2020-09-16 12:24:43 +01:00
|
|
|
if ((
|
|
|
|
#if defined(LWS_ROLE_WS)
|
|
|
|
/*
|
|
|
|
* If our goal is a ws upgrade, effectively we did not reach
|
|
|
|
* ESTABLISHED if we did not get the upgrade server reply
|
|
|
|
*/
|
|
|
|
(lwsi_state(wsi) == LRS_WAITING_SERVER_REPLY &&
|
|
|
|
wsi->role_ops == &role_ops_ws) ||
|
|
|
|
#endif
|
2019-09-19 06:54:53 +01:00
|
|
|
lwsi_state(wsi) == LRS_WAITING_DNS ||
|
2019-03-21 13:41:36 +08:00
|
|
|
lwsi_state(wsi) == LRS_WAITING_CONNECT) &&
|
2021-06-17 10:07:04 +01:00
|
|
|
!wsi->already_did_cce && wsi->a.protocol &&
|
|
|
|
!wsi->close_is_redirect) {
|
2019-12-12 14:23:06 +01:00
|
|
|
static const char _reason[] = "closed before established";
|
|
|
|
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_debug(wsi, "closing in unestablished state 0x%x",
|
|
|
|
lwsi_state(wsi));
|
2020-05-04 07:24:58 +01:00
|
|
|
wsi->socket_is_permanently_unusable = 1;
|
2020-04-19 08:43:01 +01:00
|
|
|
|
2019-08-10 09:20:27 +01:00
|
|
|
lws_inform_client_conn_fail(wsi,
|
2019-12-12 14:23:06 +01:00
|
|
|
(void *)_reason, sizeof(_reason));
|
|
|
|
}
|
2019-08-10 09:20:27 +01:00
|
|
|
#endif
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Testing with ab shows that we have to stage the socket close when
|
|
|
|
* the system is under stress... shutdown any further TX, change the
|
|
|
|
* state to one that won't emit anything more, and wait with a timeout
|
|
|
|
* for the POLLIN to show a zero-size rx before coming back and doing
|
|
|
|
* the actual close.
|
|
|
|
*/
|
|
|
|
if (wsi->role_ops != &role_ops_raw_skt && !lwsi_role_client(wsi) &&
|
|
|
|
lwsi_state(wsi) != LRS_SHUTDOWN &&
|
|
|
|
lwsi_state(wsi) != LRS_UNCONNECTED &&
|
|
|
|
reason != LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY &&
|
|
|
|
!wsi->socket_is_permanently_unusable) {
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_TLS)
|
2019-07-13 11:48:33 -07:00
|
|
|
if (lws_is_ssl(wsi) && wsi->tls.ssl) {
|
|
|
|
n = 0;
|
|
|
|
switch (__lws_tls_shutdown(wsi)) {
|
|
|
|
case LWS_SSL_CAPABLE_DONE:
|
|
|
|
case LWS_SSL_CAPABLE_ERROR:
|
|
|
|
case LWS_SSL_CAPABLE_MORE_SERVICE_READ:
|
|
|
|
case LWS_SSL_CAPABLE_MORE_SERVICE_WRITE:
|
|
|
|
case LWS_SSL_CAPABLE_MORE_SERVICE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
2019-01-13 06:58:21 +08:00
|
|
|
#endif
|
|
|
|
{
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_info("%s: shutdown conn: %s (sk %d, state 0x%x)\n",
|
|
|
|
__func__, lws_wsi_tag(wsi), (int)(lws_intptr_t)wsi->desc.sockfd,
|
2019-01-13 06:58:21 +08:00
|
|
|
lwsi_state(wsi));
|
|
|
|
if (!wsi->socket_is_permanently_unusable &&
|
|
|
|
lws_socket_is_valid(wsi->desc.sockfd)) {
|
|
|
|
wsi->socket_is_permanently_unusable = 1;
|
|
|
|
n = shutdown(wsi->desc.sockfd, SHUT_WR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n)
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_debug(wsi, "closing: shutdown (state 0x%x) ret %d",
|
2019-01-13 06:58:21 +08:00
|
|
|
lwsi_state(wsi), LWS_ERRNO);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This causes problems on WINCE / ESP32 with disconnection
|
|
|
|
* when the events are half closing connection
|
|
|
|
*/
|
2019-08-18 10:35:43 +01:00
|
|
|
#if !defined(_WIN32_WCE) && !defined(LWS_PLAT_FREERTOS)
|
2019-01-13 06:58:21 +08:00
|
|
|
/* libuv: no event available to guarantee completion */
|
|
|
|
if (!wsi->socket_is_permanently_unusable &&
|
2021-06-17 10:07:04 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
|
|
|
!wsi->close_is_redirect &&
|
|
|
|
#endif
|
2019-01-13 06:58:21 +08:00
|
|
|
lws_socket_is_valid(wsi->desc.sockfd) &&
|
|
|
|
lwsi_state(wsi) != LRS_SHUTDOWN &&
|
2020-02-07 11:39:32 +00:00
|
|
|
(context->event_loop_ops->flags & LELOF_ISPOLL)) {
|
2019-01-13 06:58:21 +08:00
|
|
|
__lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN);
|
|
|
|
lwsi_set_state(wsi, LRS_SHUTDOWN);
|
|
|
|
__lws_set_timeout(wsi, PENDING_TIMEOUT_SHUTDOWN_FLUSH,
|
2020-12-12 06:21:40 +00:00
|
|
|
(int)context->timeout_secs);
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_info(wsi, "real just_kill_connection: sockfd %d\n",
|
|
|
|
wsi->desc.sockfd);
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
#ifdef LWS_WITH_HUBBUB
|
|
|
|
if (wsi->http.rw) {
|
|
|
|
lws_rewrite_destroy(wsi->http.rw);
|
|
|
|
wsi->http.rw = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (wsi->http.pending_return_headers)
|
|
|
|
lws_free_set_NULL(wsi->http.pending_return_headers);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we won't be servicing or receiving anything further from this guy
|
|
|
|
* delete socket from the internal poll list if still present
|
|
|
|
*/
|
|
|
|
__lws_ssl_remove_wsi_from_buffered_list(wsi);
|
2019-08-09 10:12:09 +01:00
|
|
|
__lws_wsi_remove_from_sul(wsi);
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2019-02-17 10:46:30 +08:00
|
|
|
//if (wsi->told_event_loop_closed) // cgi std close case (dummy-callback)
|
|
|
|
// return;
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
/* checking return redundant since we anyway close */
|
2020-05-04 07:24:58 +01:00
|
|
|
__remove_wsi_socket_from_fds(wsi);
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
lwsi_set_state(wsi, LRS_DEAD_SOCKET);
|
|
|
|
lws_buflist_destroy_all_segments(&wsi->buflist);
|
2019-04-21 06:24:05 +01:00
|
|
|
lws_dll2_remove(&wsi->dll_buflist);
|
2019-01-13 06:58:21 +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
|
|
|
if (lws_rops_fidx(wsi->role_ops, LWS_ROPS_close_role))
|
|
|
|
lws_rops_func_fidx(wsi->role_ops, LWS_ROPS_close_role).
|
|
|
|
close_role(pt, wsi);
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
/* tell the user it's all over for this guy */
|
|
|
|
|
2019-09-08 08:08:55 +01:00
|
|
|
ccb = 0;
|
2019-01-13 06:58:21 +08:00
|
|
|
if ((lwsi_state_est_PRE_CLOSE(wsi) ||
|
2019-06-27 14:55:04 +01:00
|
|
|
/* raw skt adopted but didn't complete tls hs should CLOSE */
|
|
|
|
(wsi->role_ops == &role_ops_raw_skt && !lwsi_role_client(wsi)) ||
|
2019-01-13 06:58:21 +08:00
|
|
|
lwsi_state_PRE_CLOSE(wsi) == LRS_WAITING_SERVER_REPLY) &&
|
|
|
|
!wsi->told_user_closed &&
|
|
|
|
wsi->role_ops->close_cb[lwsi_role_server(wsi)]) {
|
2019-09-08 08:08:55 +01:00
|
|
|
if (!wsi->upgraded_to_http2 || !lwsi_role_client(wsi))
|
|
|
|
ccb = 1;
|
2019-01-13 06:58:21 +08:00
|
|
|
/*
|
|
|
|
* The network wsi for a client h2 connection shouldn't
|
|
|
|
* call back for its role: the child stream connections
|
|
|
|
* own the role. Otherwise h2 will call back closed
|
|
|
|
* one too many times as the children do it and then
|
|
|
|
* the closing network stream.
|
|
|
|
*/
|
2019-09-08 08:08:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!wsi->told_user_closed &&
|
|
|
|
!lws_dll2_is_detached(&wsi->vh_awaiting_socket))
|
|
|
|
/*
|
|
|
|
* He's a guy who go started with dns, but failed or is
|
|
|
|
* caught with a shutdown before he got the result. We have
|
2021-06-17 10:07:04 +01:00
|
|
|
* to issclient_mux_substream_wasue him a close cb
|
2019-09-08 08:08:55 +01:00
|
|
|
*/
|
|
|
|
ccb = 1;
|
|
|
|
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_info(wsi, "cce=%d", ccb);
|
2020-12-27 19:34:30 +00:00
|
|
|
|
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
|
|
|
pro = wsi->a.protocol;
|
2020-02-29 12:37:24 +00:00
|
|
|
|
2020-09-16 12:24:43 +01:00
|
|
|
if (wsi->already_did_cce)
|
|
|
|
/*
|
|
|
|
* If we handled this by CLIENT_CONNECTION_ERROR, it's
|
|
|
|
* mutually exclusive with CLOSE
|
|
|
|
*/
|
|
|
|
ccb = 0;
|
|
|
|
|
2020-02-29 12:37:24 +00:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2021-06-17 10:07:04 +01:00
|
|
|
if (!wsi->close_is_redirect && !ccb &&
|
|
|
|
(lwsi_state_PRE_CLOSE(wsi) & LWSIFS_NOT_EST) &&
|
2020-02-29 12:37:24 +00:00
|
|
|
lwsi_role_client(wsi)) {
|
|
|
|
lws_inform_client_conn_fail(wsi, "Closed before conn", 18);
|
|
|
|
}
|
|
|
|
#endif
|
2021-06-17 10:07:04 +01:00
|
|
|
if (ccb
|
|
|
|
#if defined(LWS_WITH_CLIENT)
|
|
|
|
&& !wsi->close_is_redirect
|
|
|
|
#endif
|
|
|
|
) {
|
2019-09-08 08:08:55 +01:00
|
|
|
|
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
|
|
|
if (!wsi->a.protocol && wsi->a.vhost && wsi->a.vhost->protocols)
|
|
|
|
pro = &wsi->a.vhost->protocols[0];
|
2019-09-08 08:08:55 +01:00
|
|
|
|
|
|
|
if (pro)
|
2019-01-13 06:58:21 +08:00
|
|
|
pro->callback(wsi,
|
2019-09-08 08:08:55 +01:00
|
|
|
wsi->role_ops->close_cb[lwsi_role_server(wsi)],
|
|
|
|
wsi->user_space, NULL, 0);
|
2019-01-13 06:58:21 +08:00
|
|
|
wsi->told_user_closed = 1;
|
|
|
|
}
|
|
|
|
|
2020-02-28 15:50:15 +00:00
|
|
|
#if defined(LWS_ROLE_RAW_FILE)
|
2019-01-13 06:58:21 +08:00
|
|
|
async_close:
|
2020-02-28 15:50:15 +00:00
|
|
|
#endif
|
2020-12-27 19:34:30 +00:00
|
|
|
|
2021-05-26 13:56:51 +01:00
|
|
|
#if defined(LWS_WITH_SECURE_STREAMS)
|
2020-12-27 19:34:30 +00:00
|
|
|
if (wsi->for_ss) {
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_debug(wsi, "for_ss");
|
2020-12-27 19:34:30 +00:00
|
|
|
/*
|
|
|
|
* We were adopted for a particular ss, but, eg, we may not
|
|
|
|
* have succeeded with the connection... we are closing which is
|
|
|
|
* good, but we have to invalidate any pointer the related ss
|
|
|
|
* handle may be holding on us
|
|
|
|
*/
|
|
|
|
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
|
|
|
|
|
|
|
|
if (wsi->client_proxy_onward) {
|
|
|
|
/*
|
|
|
|
* We are an onward proxied wsi at the proxy,
|
|
|
|
* opaque is proxing "conn", we must remove its pointer
|
|
|
|
* to us since we are destroying
|
|
|
|
*/
|
|
|
|
lws_proxy_clean_conn_ss(wsi);
|
|
|
|
} else
|
|
|
|
|
|
|
|
if (wsi->client_bound_sspc) {
|
|
|
|
lws_sspc_handle_t *h = (lws_sspc_handle_t *)wsi->a.opaque_user_data;
|
|
|
|
|
|
|
|
if (h) { // && (h->info.flags & LWSSSINFLAGS_ACCEPTED)) {
|
2021-01-06 15:08:22 +00:00
|
|
|
|
|
|
|
#if defined(LWS_WITH_SYS_METRICS)
|
|
|
|
/*
|
|
|
|
* If any hanging caliper measurement, dump it, and free any tags
|
|
|
|
*/
|
|
|
|
lws_metrics_caliper_report_hist(h->cal_txn, (struct lws *)NULL);
|
|
|
|
#endif
|
|
|
|
|
2020-12-27 19:34:30 +00:00
|
|
|
h->cwsi = NULL;
|
|
|
|
//wsi->a.opaque_user_data = NULL;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
lws_ss_handle_t *h = (lws_ss_handle_t *)wsi->a.opaque_user_data;
|
|
|
|
|
|
|
|
if (h) { // && (h->info.flags & LWSSSINFLAGS_ACCEPTED)) {
|
|
|
|
|
2021-01-06 15:08:22 +00:00
|
|
|
/*
|
|
|
|
* ss level: only reports if dangling caliper
|
|
|
|
* not already reported
|
|
|
|
*/
|
|
|
|
lws_metrics_caliper_report_hist(h->cal_txn, wsi);
|
|
|
|
|
2020-12-27 19:34:30 +00:00
|
|
|
h->wsi = NULL;
|
|
|
|
wsi->a.opaque_user_data = NULL;
|
|
|
|
|
|
|
|
if (h->ss_dangling_connected &&
|
|
|
|
lws_ss_event_helper(h, LWSSSCS_DISCONNECTED) ==
|
|
|
|
LWSSSSRET_DESTROY_ME) {
|
|
|
|
|
|
|
|
lws_ss_destroy(&h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2019-03-19 11:53:57 +08:00
|
|
|
lws_remove_child_from_any_parent(wsi);
|
2019-01-13 06:58:21 +08:00
|
|
|
wsi->socket_is_permanently_unusable = 1;
|
|
|
|
|
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
|
|
|
if (wsi->a.context->event_loop_ops->wsi_logical_close)
|
|
|
|
if (wsi->a.context->event_loop_ops->wsi_logical_close(wsi))
|
2019-01-13 06:58:21 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
__lws_close_free_wsi_final(wsi);
|
|
|
|
}
|
|
|
|
|
2021-04-04 04:06:24 +01:00
|
|
|
|
|
|
|
/* cx + vh lock */
|
|
|
|
|
2019-01-13 06:58:21 +08:00
|
|
|
void
|
|
|
|
__lws_close_free_wsi_final(struct lws *wsi)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (!wsi->shadow &&
|
|
|
|
lws_socket_is_valid(wsi->desc.sockfd) && !lws_ssl_close(wsi)) {
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_debug(wsi, "fd %d", wsi->desc.sockfd);
|
2024-09-25 08:38:15 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* if this is the pt pipe, skip the actual close,
|
|
|
|
* go through the motions though so we will reach 0 open wsi
|
|
|
|
* on the pt, and trigger the pt destroy to close the pipe fds
|
|
|
|
*/
|
|
|
|
if (!lws_plat_pipe_is_fd_assocated(wsi->a.context, wsi->tsi,
|
|
|
|
wsi->desc.sockfd)) {
|
|
|
|
n = compatible_close(wsi->desc.sockfd);
|
|
|
|
if (n)
|
|
|
|
lwsl_wsi_debug(wsi, "closing: close ret %d",
|
|
|
|
LWS_ERRNO);
|
|
|
|
}
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2020-05-04 07:24:58 +01:00
|
|
|
__remove_wsi_socket_from_fds(wsi);
|
|
|
|
if (lws_socket_is_valid(wsi->desc.sockfd))
|
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
|
|
|
delete_from_fd(wsi->a.context, wsi->desc.sockfd);
|
2020-05-04 07:24:58 +01:00
|
|
|
|
|
|
|
#if !defined(LWS_PLAT_FREERTOS) && !defined(WIN32) && !defined(LWS_PLAT_OPTEE)
|
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
|
|
|
delete_from_fdwsi(wsi->a.context, wsi);
|
2020-05-04 07:24:58 +01: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
|
|
|
sanity_assert_no_sockfd_traces(wsi->a.context, wsi->desc.sockfd);
|
2021-06-17 10:07:04 +01:00
|
|
|
}
|
|
|
|
|
2021-12-08 06:28:16 +00:00
|
|
|
/* ... if we're closing the cancel pipe, account for it */
|
|
|
|
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt =
|
|
|
|
&wsi->a.context->pt[(int)wsi->tsi];
|
|
|
|
|
|
|
|
if (pt->pipe_wsi == wsi)
|
|
|
|
pt->pipe_wsi = NULL;
|
|
|
|
if (pt->dummy_pipe_fds[0] == wsi->desc.sockfd)
|
|
|
|
pt->dummy_pipe_fds[0] = LWS_SOCK_INVALID;
|
|
|
|
}
|
|
|
|
|
2021-06-17 10:07:04 +01:00
|
|
|
wsi->desc.sockfd = LWS_SOCK_INVALID;
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2021-06-26 17:24:19 +01:00
|
|
|
lws_free_set_NULL(wsi->cli_hostname_copy);
|
2021-06-17 10:07:04 +01:00
|
|
|
if (wsi->close_is_redirect) {
|
|
|
|
|
|
|
|
wsi->close_is_redirect = 0;
|
|
|
|
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_info(wsi, "picking up redirection");
|
2021-06-17 10:07:04 +01:00
|
|
|
|
|
|
|
lws_role_transition(wsi, LWSIFR_CLIENT, LRS_UNCONNECTED,
|
|
|
|
&role_ops_h1);
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_HTTP2)
|
|
|
|
if (wsi->client_mux_substream_was)
|
|
|
|
wsi->h2.END_STREAM = wsi->h2.END_HEADERS = 0;
|
|
|
|
#endif
|
|
|
|
#if defined(LWS_ROLE_H2) || defined(LWS_ROLE_MQTT)
|
|
|
|
if (wsi->mux.parent_wsi) {
|
|
|
|
lws_wsi_mux_sibling_disconnect(wsi);
|
|
|
|
wsi->mux.parent_wsi = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_TLS)
|
|
|
|
memset(&wsi->tls, 0, sizeof(wsi->tls));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// wsi->a.protocol = NULL;
|
|
|
|
if (wsi->a.protocol)
|
|
|
|
lws_bind_protocol(wsi, wsi->a.protocol, "client_reset");
|
|
|
|
wsi->pending_timeout = NO_PENDING_TIMEOUT;
|
|
|
|
wsi->hdr_parsing_completed = 0;
|
2020-05-04 07:24:58 +01:00
|
|
|
|
2021-06-17 10:07:04 +01:00
|
|
|
#if defined(LWS_WITH_TLS)
|
|
|
|
if (wsi->stash->cis[CIS_ALPN])
|
|
|
|
lws_strncpy(wsi->alpn, wsi->stash->cis[CIS_ALPN],
|
|
|
|
sizeof(wsi->alpn));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (lws_header_table_attach(wsi, 0)) {
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_err(wsi, "failed to get ah");
|
2021-06-17 10:07:04 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
//_lws_header_table_reset(wsi->http.ah);
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_TLS)
|
2021-12-23 06:13:27 +00:00
|
|
|
wsi->tls.use_ssl = (unsigned int)wsi->flags;
|
2021-06-17 10:07:04 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_TLS_JIT_TRUST)
|
|
|
|
if (wsi->stash && wsi->stash->cis[CIS_ADDRESS]) {
|
|
|
|
struct lws_vhost *vh = NULL;
|
|
|
|
lws_tls_jit_trust_vhost_bind(wsi->a.context,
|
|
|
|
wsi->stash->cis[CIS_ADDRESS],
|
|
|
|
&vh);
|
|
|
|
if (vh) {
|
|
|
|
if (!vh->count_bound_wsi && vh->grace_after_unref) {
|
2021-06-18 07:28:23 +01:00
|
|
|
lwsl_wsi_info(wsi, "%s in use\n",
|
|
|
|
vh->lc.gutag);
|
2021-06-17 10:07:04 +01:00
|
|
|
lws_sul_cancel(&vh->sul_unref);
|
|
|
|
}
|
|
|
|
vh->count_bound_wsi++;
|
|
|
|
wsi->a.vhost = vh;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return;
|
2019-01-13 06:58:21 +08:00
|
|
|
}
|
2021-06-17 10:07:04 +01:00
|
|
|
#endif
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
/* outermost destroy notification for wsi (user_space still intact) */
|
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
|
|
|
if (wsi->a.vhost)
|
|
|
|
wsi->a.vhost->protocols[0].callback(wsi, LWS_CALLBACK_WSI_DESTROY,
|
2019-01-13 06:58:21 +08:00
|
|
|
wsi->user_space, NULL, 0);
|
|
|
|
|
|
|
|
#ifdef LWS_WITH_CGI
|
|
|
|
if (wsi->http.cgi) {
|
2020-02-12 10:12:39 +00:00
|
|
|
lws_spawn_piped_destroy(&wsi->http.cgi->lsp);
|
2020-07-17 15:35:28 +01:00
|
|
|
lws_sul_cancel(&wsi->http.cgi->sul_grace);
|
2019-02-17 10:46:30 +08:00
|
|
|
lws_free_set_NULL(wsi->http.cgi);
|
2019-01-13 06:58:21 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-02-17 10:31:22 +00:00
|
|
|
#if defined(LWS_WITH_SYS_FAULT_INJECTION)
|
2021-03-16 13:32:05 +00:00
|
|
|
lws_fi_destroy(&wsi->fic);
|
2021-02-17 10:31:22 +00:00
|
|
|
#endif
|
|
|
|
|
2020-05-04 07:24:58 +01:00
|
|
|
__lws_wsi_remove_from_sul(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
|
|
|
sanity_assert_no_wsi_traces(wsi->a.context, wsi);
|
2019-01-13 06:58:21 +08:00
|
|
|
__lws_free_wsi(wsi);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason, const char *caller)
|
|
|
|
{
|
2021-04-04 04:06:24 +01:00
|
|
|
struct lws_context *cx = wsi->a.context;
|
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
|
|
|
struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2021-04-04 04:06:24 +01:00
|
|
|
lws_context_lock(cx, __func__);
|
|
|
|
|
2019-01-13 06:58:21 +08:00
|
|
|
lws_pt_lock(pt, __func__);
|
2021-04-04 04:06:24 +01:00
|
|
|
/* may destroy vhost, cannot hold vhost lock outside it */
|
2019-01-13 06:58:21 +08:00
|
|
|
__lws_close_free_wsi(wsi, reason, caller);
|
|
|
|
lws_pt_unlock(pt);
|
2021-04-04 04:06:24 +01:00
|
|
|
|
|
|
|
lws_context_unlock(cx);
|
2019-01-13 06:58:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|