/src/libwebsockets/lib/core-net/close.c
Line | Count | Source |
1 | | /* |
2 | | * libwebsockets - small server side websockets and web server implementation |
3 | | * |
4 | | * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com> |
5 | | * |
6 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | | * of this software and associated documentation files (the "Software"), to |
8 | | * deal in the Software without restriction, including without limitation the |
9 | | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
10 | | * sell copies of the Software, and to permit persons to whom the Software is |
11 | | * furnished to do so, subject to the following conditions: |
12 | | * |
13 | | * The above copyright notice and this permission notice shall be included in |
14 | | * all copies or substantial portions of the Software. |
15 | | * |
16 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
21 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
22 | | * IN THE SOFTWARE. |
23 | | */ |
24 | | |
25 | | #include "private-lib-core.h" |
26 | | #include "private-lib-async-dns.h" |
27 | | |
28 | | #if defined(LWS_WITH_CLIENT) |
29 | | static int |
30 | | lws_close_trans_q_leader(struct lws_dll2 *d, void *user) |
31 | 0 | { |
32 | 0 | struct lws *w = lws_container_of(d, struct lws, dll2_cli_txn_queue); |
33 | |
|
34 | 0 | __lws_close_free_wsi(w, (enum lws_close_status)-1, "trans q leader closing"); |
35 | |
|
36 | 0 | return 0; |
37 | 0 | } |
38 | | #endif |
39 | | |
40 | | void |
41 | | __lws_reset_wsi(struct lws *wsi) |
42 | 0 | { |
43 | 0 | if (!wsi) |
44 | 0 | return; |
45 | | |
46 | 0 | #if defined(LWS_WITH_CLIENT) |
47 | | |
48 | 0 | lws_free_set_NULL(wsi->cli_hostname_copy); |
49 | |
|
50 | 0 | #if defined(LWS_WITH_CONMON) |
51 | |
|
52 | 0 | if (wsi->conmon.dns_results_copy) { |
53 | 0 | lws_conmon_addrinfo_destroy(wsi->conmon.dns_results_copy); |
54 | 0 | wsi->conmon.dns_results_copy = NULL; |
55 | 0 | } |
56 | |
|
57 | 0 | wsi->conmon.ciu_dns = |
58 | 0 | wsi->conmon.ciu_sockconn = |
59 | 0 | wsi->conmon.ciu_tls = |
60 | 0 | wsi->conmon.ciu_txn_resp = 0; |
61 | 0 | #endif |
62 | | |
63 | | /* |
64 | | * if we have wsi in our transaction queue, if we are closing we |
65 | | * must go through and close all those first |
66 | | */ |
67 | 0 | if (wsi->a.vhost) { |
68 | | |
69 | | /* we are no longer an active client connection that can piggyback */ |
70 | 0 | lws_dll2_remove(&wsi->dll_cli_active_conns); |
71 | |
|
72 | 0 | lws_dll2_foreach_safe(&wsi->dll2_cli_txn_queue_owner, NULL, |
73 | 0 | lws_close_trans_q_leader); |
74 | | |
75 | | /* |
76 | | * !!! If we are closing, but we have pending pipelined |
77 | | * transaction results we already sent headers for, that's going |
78 | | * to destroy sync for HTTP/1 and leave H2 stream with no live |
79 | | * swsi.` |
80 | | * |
81 | | * However this is normal if we are being closed because the |
82 | | * transaction queue leader is closing. |
83 | | */ |
84 | 0 | lws_dll2_remove(&wsi->dll2_cli_txn_queue); |
85 | 0 | } |
86 | 0 | #endif |
87 | |
|
88 | 0 | if (wsi->a.vhost) { |
89 | 0 | lws_vhost_lock(wsi->a.vhost); |
90 | 0 | lws_dll2_remove(&wsi->vh_awaiting_socket); |
91 | 0 | lws_vhost_unlock(wsi->a.vhost); |
92 | 0 | } |
93 | | |
94 | | /* |
95 | | * Protocol user data may be allocated either internally by lws |
96 | | * or by specified the user. We should only free what we allocated. |
97 | | */ |
98 | 0 | if (wsi->a.protocol && wsi->a.protocol->per_session_data_size && |
99 | 0 | wsi->user_space && !wsi->user_space_externally_allocated) { |
100 | | /* confirm no sul left scheduled in user data itself */ |
101 | 0 | lws_sul_debug_zombies(wsi->a.context, wsi->user_space, |
102 | 0 | wsi->a.protocol->per_session_data_size, __func__); |
103 | 0 | lws_free_set_NULL(wsi->user_space); |
104 | 0 | } |
105 | | |
106 | | /* |
107 | | * Don't let buflist content or state from the wsi's previous life |
108 | | * carry over to the new life |
109 | | */ |
110 | |
|
111 | 0 | lws_buflist_destroy_all_segments(&wsi->buflist); |
112 | 0 | lws_dll2_remove(&wsi->dll_buflist); |
113 | 0 | lws_buflist_destroy_all_segments(&wsi->buflist_out); |
114 | 0 | #if defined(LWS_WITH_UDP) |
115 | 0 | if (wsi->udp) { |
116 | | /* confirm no sul left scheduled in wsi->udp itself */ |
117 | 0 | lws_sul_debug_zombies(wsi->a.context, wsi->udp, |
118 | 0 | sizeof(*wsi->udp), "close udp wsi"); |
119 | 0 | lws_free_set_NULL(wsi->udp); |
120 | 0 | } |
121 | 0 | #endif |
122 | 0 | wsi->retry = 0; |
123 | 0 | wsi->mount_hit = 0; |
124 | |
|
125 | 0 | #if defined(LWS_WITH_CLIENT) |
126 | 0 | lws_dll2_remove(&wsi->dll2_cli_txn_queue); |
127 | 0 | lws_dll2_remove(&wsi->dll_cli_active_conns); |
128 | 0 | if (wsi->cli_hostname_copy) |
129 | 0 | lws_free_set_NULL(wsi->cli_hostname_copy); |
130 | 0 | #endif |
131 | |
|
132 | | #if defined(LWS_WITH_SYS_ASYNC_DNS) |
133 | | lws_async_dns_cancel(wsi); |
134 | | #endif |
135 | |
|
136 | | #if defined(LWS_WITH_HTTP_PROXY) |
137 | | if (wsi->http.buflist_post_body) |
138 | | lws_buflist_destroy_all_segments(&wsi->http.buflist_post_body); |
139 | | #endif |
140 | |
|
141 | 0 | #if defined(LWS_WITH_HTTP_DIGEST_AUTH) |
142 | 0 | if (wsi->http.digest_auth_hdr) { |
143 | 0 | lws_free(wsi->http.digest_auth_hdr); |
144 | 0 | wsi->http.digest_auth_hdr = NULL; |
145 | 0 | } |
146 | 0 | #endif |
147 | |
|
148 | 0 | #if defined(LWS_WITH_SERVER) |
149 | 0 | lws_dll2_remove(&wsi->listen_list); |
150 | 0 | #endif |
151 | |
|
152 | 0 | #if defined(LWS_WITH_CLIENT) |
153 | 0 | if (wsi->a.vhost) |
154 | 0 | lws_dll2_remove(&wsi->dll_cli_active_conns); |
155 | 0 | #endif |
156 | |
|
157 | 0 | __lws_same_vh_protocol_remove(wsi); |
158 | 0 | #if defined(LWS_WITH_CLIENT) |
159 | | //lws_free_set_NULL(wsi->stash); |
160 | 0 | lws_free_set_NULL(wsi->cli_hostname_copy); |
161 | 0 | #endif |
162 | |
|
163 | | #if defined(LWS_WITH_PEER_LIMITS) |
164 | | lws_peer_track_wsi_close(wsi->a.context, wsi->peer); |
165 | | wsi->peer = NULL; |
166 | | #endif |
167 | | |
168 | | /* since we will destroy the wsi, make absolutely sure now */ |
169 | |
|
170 | | #if defined(LWS_WITH_OPENSSL) |
171 | | __lws_ssl_remove_wsi_from_buffered_list(wsi); |
172 | | #endif |
173 | 0 | __lws_wsi_remove_from_sul(wsi); |
174 | |
|
175 | 0 | if (lws_rops_fidx(wsi->role_ops, LWS_ROPS_destroy_role)) |
176 | 0 | lws_rops_func_fidx(wsi->role_ops, |
177 | 0 | LWS_ROPS_destroy_role).destroy_role(wsi); |
178 | |
|
179 | 0 | #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2) |
180 | 0 | __lws_header_table_detach(wsi, 0); |
181 | 0 | #endif |
182 | |
|
183 | 0 | #if defined(LWS_ROLE_H2) |
184 | | /* |
185 | | * Let's try to clean out the h2-ness of the wsi |
186 | | */ |
187 | |
|
188 | 0 | memset(&wsi->h2, 0, sizeof(wsi->h2)); |
189 | |
|
190 | 0 | wsi->hdr_parsing_completed = wsi->mux_substream = |
191 | 0 | wsi->upgraded_to_http2 = wsi->mux_stream_immortal = |
192 | 0 | wsi->h2_acked_settings = wsi->seen_nonpseudoheader = |
193 | 0 | wsi->socket_is_permanently_unusable = wsi->favoured_pollin = |
194 | 0 | wsi->already_did_cce = wsi->told_user_closed = |
195 | 0 | wsi->waiting_to_send_close_frame = wsi->close_needs_ack = |
196 | 0 | wsi->parent_pending_cb_on_writable = wsi->seen_zero_length_recv = |
197 | 0 | wsi->close_when_buffered_out_drained = wsi->could_have_pending = 0; |
198 | 0 | #endif |
199 | |
|
200 | 0 | #if defined(LWS_WITH_CLIENT) |
201 | 0 | wsi->do_ws = wsi->chunked = wsi->client_rx_avail = |
202 | 0 | wsi->client_http_body_pending = wsi->transaction_from_pipeline_queue = |
203 | 0 | wsi->keepalive_active = wsi->keepalive_rejected = |
204 | 0 | wsi->redirected_to_get = wsi->client_pipeline = wsi->client_h2_alpn = |
205 | 0 | wsi->client_mux_substream = wsi->client_mux_migrated = |
206 | 0 | wsi->tls_session_reused = wsi->perf_done = 0; |
207 | |
|
208 | 0 | wsi->immortal_substream_count = 0; |
209 | 0 | #endif |
210 | 0 | } |
211 | | |
212 | | /* req cx lock */ |
213 | | |
214 | | void |
215 | | __lws_free_wsi(struct lws *wsi) |
216 | 0 | { |
217 | 0 | struct lws_vhost *vh; |
218 | |
|
219 | 0 | if (!wsi) |
220 | 0 | return; |
221 | | |
222 | 0 | lws_context_assert_lock_held(wsi->a.context); |
223 | | |
224 | | /* just in case */ |
225 | 0 | lws_dll2_remove(&wsi->pre_natal); |
226 | |
|
227 | 0 | #if defined(LWS_WITH_SECURE_STREAMS) |
228 | 0 | if (wsi->for_ss) { |
229 | |
|
230 | | #if defined(LWS_WITH_SECURE_STREAMS_PROXY_API) |
231 | | if (wsi->client_bound_sspc) { |
232 | | lws_sspc_handle_t *h = (lws_sspc_handle_t *) |
233 | | wsi->a.opaque_user_data; |
234 | | if (h) { |
235 | | h->txp_path.priv_onw = NULL; |
236 | | wsi->a.opaque_user_data = NULL; |
237 | | } |
238 | | } else |
239 | | #endif |
240 | 0 | { |
241 | | /* |
242 | | * Make certain it is disconnected from the ss by now |
243 | | */ |
244 | 0 | lws_ss_handle_t *h = (lws_ss_handle_t *) |
245 | 0 | wsi->a.opaque_user_data; |
246 | |
|
247 | 0 | if (h) { |
248 | 0 | h->wsi = NULL; |
249 | 0 | wsi->a.opaque_user_data = NULL; |
250 | 0 | } |
251 | 0 | } |
252 | 0 | } |
253 | 0 | #endif |
254 | |
|
255 | 0 | vh = wsi->a.vhost; |
256 | |
|
257 | 0 | __lws_reset_wsi(wsi); |
258 | 0 | __lws_wsi_remove_from_sul(wsi); |
259 | |
|
260 | 0 | if (vh) |
261 | | /* this may destroy vh */ |
262 | 0 | __lws_vhost_unbind_wsi(wsi); /* req cx + vh lock */ |
263 | |
|
264 | 0 | #if defined(LWS_WITH_CLIENT) |
265 | 0 | if (wsi->stash) |
266 | 0 | lws_free_set_NULL(wsi->stash); |
267 | 0 | #endif |
268 | |
|
269 | 0 | if (wsi->a.context->event_loop_ops->destroy_wsi) |
270 | 0 | wsi->a.context->event_loop_ops->destroy_wsi(wsi); |
271 | |
|
272 | 0 | lwsl_wsi_debug(wsi, "tsi fds count %d\n", |
273 | 0 | wsi->a.context->pt[(int)wsi->tsi].fds_count); |
274 | | |
275 | | /* confirm no sul left scheduled in wsi itself */ |
276 | 0 | lws_sul_debug_zombies(wsi->a.context, wsi, sizeof(*wsi), __func__); |
277 | |
|
278 | 0 | __lws_lc_untag(wsi->a.context, &wsi->lc); |
279 | 0 | lws_free(wsi); |
280 | 0 | } |
281 | | |
282 | | |
283 | | void |
284 | | lws_remove_child_from_any_parent(struct lws *wsi) |
285 | 0 | { |
286 | 0 | struct lws **pwsi; |
287 | 0 | int seen = 0; |
288 | |
|
289 | 0 | if (!wsi->parent) |
290 | 0 | return; |
291 | | |
292 | | /* detach ourselves from parent's child list */ |
293 | 0 | pwsi = &wsi->parent->child_list; |
294 | 0 | while (*pwsi) { |
295 | 0 | if (*pwsi == wsi) { |
296 | 0 | lwsl_wsi_info(wsi, "detach from parent %s", |
297 | 0 | lws_wsi_tag(wsi->parent)); |
298 | |
|
299 | 0 | if (wsi->parent->a.protocol) |
300 | 0 | wsi->parent->a.protocol->callback(wsi, |
301 | 0 | LWS_CALLBACK_CHILD_CLOSING, |
302 | 0 | wsi->parent->user_space, wsi, 0); |
303 | |
|
304 | 0 | *pwsi = wsi->sibling_list; |
305 | 0 | seen = 1; |
306 | 0 | break; |
307 | 0 | } |
308 | 0 | pwsi = &(*pwsi)->sibling_list; |
309 | 0 | } |
310 | 0 | if (!seen) |
311 | 0 | lwsl_wsi_err(wsi, "failed to detach from parent"); |
312 | |
|
313 | 0 | wsi->parent = NULL; |
314 | 0 | } |
315 | | |
316 | | #if defined(LWS_WITH_CLIENT) |
317 | | void |
318 | | lws_inform_client_conn_fail(struct lws *wsi, void *arg, size_t len) |
319 | 0 | { |
320 | 0 | lws_addrinfo_clean(wsi); |
321 | |
|
322 | 0 | if (wsi->already_did_cce) |
323 | 0 | return; |
324 | | |
325 | 0 | wsi->already_did_cce = 1; |
326 | |
|
327 | 0 | if (!wsi->a.protocol) |
328 | 0 | return; |
329 | | |
330 | 0 | if (!wsi->client_suppress_CONNECTION_ERROR) |
331 | 0 | wsi->a.protocol->callback(wsi, |
332 | 0 | LWS_CALLBACK_CLIENT_CONNECTION_ERROR, |
333 | 0 | wsi->user_space, arg, len); |
334 | 0 | } |
335 | | #endif |
336 | | |
337 | | void |
338 | | lws_addrinfo_clean(struct lws *wsi) |
339 | 0 | { |
340 | 0 | #if defined(LWS_WITH_CLIENT) |
341 | 0 | struct lws_dll2 *d = lws_dll2_get_head(&wsi->dns_sorted_list), *d1; |
342 | |
|
343 | 0 | while (d) { |
344 | 0 | lws_dns_sort_t *r = lws_container_of(d, lws_dns_sort_t, list); |
345 | |
|
346 | 0 | d1 = d->next; |
347 | 0 | lws_dll2_remove(d); |
348 | 0 | lws_free(r); |
349 | |
|
350 | 0 | d = d1; |
351 | 0 | } |
352 | 0 | #endif |
353 | 0 | } |
354 | | |
355 | | /* requires cx and pt lock */ |
356 | | |
357 | | void |
358 | | __lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason, |
359 | | const char *caller) |
360 | 0 | { |
361 | 0 | struct lws_context_per_thread *pt; |
362 | 0 | const struct lws_protocols *pro; |
363 | 0 | #if defined(LWS_WITH_SECURE_STREAMS) |
364 | 0 | lws_ss_handle_t *hh = NULL; |
365 | 0 | #endif |
366 | 0 | struct lws_context *context; |
367 | 0 | struct lws *wsi1, *wsi2; |
368 | 0 | int n, ccb; |
369 | |
|
370 | 0 | if (!wsi) |
371 | 0 | return; |
372 | | |
373 | 0 | lwsl_wsi_info(wsi, "caller: %s", caller); |
374 | |
|
375 | 0 | lws_access_log(wsi); |
376 | |
|
377 | 0 | if (!lws_dll2_is_detached(&wsi->dll_buflist)) |
378 | 0 | lwsl_wsi_info(wsi, "going down with stuff in buflist"); |
379 | |
|
380 | 0 | context = wsi->a.context; |
381 | 0 | pt = &context->pt[(int)wsi->tsi]; |
382 | |
|
383 | 0 | if (pt->pipe_wsi == wsi) { |
384 | 0 | lws_plat_pipe_close(pt->pipe_wsi); |
385 | 0 | pt->pipe_wsi = NULL; |
386 | 0 | } |
387 | |
|
388 | | #if defined(LWS_WITH_SYS_METRICS) && \ |
389 | | (defined(LWS_WITH_CLIENT) || defined(LWS_WITH_SERVER)) |
390 | | /* wsi level: only reports if dangling caliper */ |
391 | | if (wsi->cal_conn.mt && wsi->cal_conn.us_start) { |
392 | | if ((lws_metrics_priv_to_pub(wsi->cal_conn.mt)->flags) & LWSMTFL_REPORT_HIST) { |
393 | | lws_metrics_caliper_report_hist(wsi->cal_conn, (struct lws *)NULL); |
394 | | } else { |
395 | | lws_metrics_caliper_report(wsi->cal_conn, METRES_NOGO); |
396 | | lws_metrics_caliper_done(wsi->cal_conn); |
397 | | } |
398 | | } else |
399 | | lws_metrics_caliper_done(wsi->cal_conn); |
400 | | #endif |
401 | |
|
402 | | #if defined(LWS_WITH_SYS_ASYNC_DNS) |
403 | | /* is this wsi handling the interface to a dns server? */ |
404 | | { |
405 | | lws_async_dns_server_t *dsrv = |
406 | | __lws_async_dns_server_find_wsi(&context->async_dns, wsi); |
407 | | |
408 | | if (dsrv) |
409 | | dsrv->wsi = NULL; |
410 | | } |
411 | | #endif |
412 | |
|
413 | 0 | lws_pt_assert_lock_held(pt); |
414 | |
|
415 | 0 | #if defined(LWS_WITH_CLIENT) |
416 | |
|
417 | 0 | lws_free_set_NULL(wsi->cli_hostname_copy); |
418 | 0 | wsi->client_mux_substream_was = wsi->client_mux_substream; |
419 | |
|
420 | 0 | lws_addrinfo_clean(wsi); |
421 | 0 | #endif |
422 | |
|
423 | 0 | #if defined(LWS_WITH_HTTP2) |
424 | 0 | if (wsi->mux_stream_immortal) |
425 | 0 | lws_http_close_immortal(wsi); |
426 | 0 | #endif |
427 | | |
428 | | /* if we have children, close them first */ |
429 | 0 | if (wsi->child_list) { |
430 | 0 | wsi2 = wsi->child_list; |
431 | 0 | while (wsi2) { |
432 | 0 | wsi1 = wsi2->sibling_list; |
433 | | // wsi2->parent = NULL; |
434 | | /* stop it doing shutdown processing */ |
435 | 0 | wsi2->socket_is_permanently_unusable = 1; |
436 | 0 | __lws_close_free_wsi(wsi2, reason, |
437 | 0 | "general child recurse"); |
438 | 0 | wsi2 = wsi1; |
439 | 0 | } |
440 | 0 | wsi->child_list = NULL; |
441 | 0 | } |
442 | |
|
443 | 0 | #if defined(LWS_ROLE_RAW_FILE) |
444 | 0 | if (wsi->role_ops == &role_ops_raw_file) { |
445 | 0 | lws_remove_child_from_any_parent(wsi); |
446 | 0 | __remove_wsi_socket_from_fds(wsi); |
447 | 0 | if (wsi->a.protocol) |
448 | 0 | wsi->a.protocol->callback(wsi, wsi->role_ops->close_cb[0], |
449 | 0 | wsi->user_space, NULL, 0); |
450 | 0 | goto async_close; |
451 | 0 | } |
452 | 0 | #endif |
453 | | |
454 | 0 | wsi->wsistate_pre_close = wsi->wsistate; |
455 | |
|
456 | | #ifdef LWS_WITH_CGI |
457 | | if (wsi->role_ops == &role_ops_cgi) { |
458 | | |
459 | | // lwsl_debug("%s: closing stdwsi index %d\n", __func__, (int)wsi->lsp_channel); |
460 | | |
461 | | /* we are not a network connection, but a handler for CGI io */ |
462 | | if (wsi->parent && wsi->parent->http.cgi) { |
463 | | |
464 | | /* |
465 | | * We need to keep the logical cgi around so we can |
466 | | * drain it |
467 | | */ |
468 | | |
469 | | // if (wsi->parent->child_list == wsi && !wsi->sibling_list) |
470 | | // lws_cgi_remove_and_kill(wsi->parent); |
471 | | |
472 | | /* end the binding between us and network connection */ |
473 | | if (wsi->parent->http.cgi && wsi->parent->http.cgi->lsp) |
474 | | wsi->parent->http.cgi->lsp->stdwsi[(int)wsi->lsp_channel] = |
475 | | NULL; |
476 | | } |
477 | | wsi->socket_is_permanently_unusable = 1; |
478 | | |
479 | | goto just_kill_connection; |
480 | | } |
481 | | |
482 | | if (wsi->http.cgi) |
483 | | lws_cgi_remove_and_kill(wsi); |
484 | | #endif |
485 | |
|
486 | 0 | #if defined(LWS_WITH_CLIENT) |
487 | 0 | if (!wsi->close_is_redirect) |
488 | 0 | lws_free_set_NULL(wsi->stash); |
489 | 0 | #endif |
490 | |
|
491 | 0 | if (wsi->role_ops == &role_ops_raw_skt) { |
492 | 0 | wsi->socket_is_permanently_unusable = 1; |
493 | 0 | goto just_kill_connection; |
494 | 0 | } |
495 | 0 | #if defined(LWS_WITH_FILE_OPS) && (defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)) |
496 | 0 | if (lwsi_role_http(wsi) && lwsi_role_server(wsi) && |
497 | 0 | wsi->http.fop_fd != NULL) |
498 | 0 | lws_vfs_file_close(&wsi->http.fop_fd); |
499 | 0 | #endif |
500 | |
|
501 | 0 | if (lwsi_state(wsi) == LRS_DEAD_SOCKET) |
502 | 0 | return; |
503 | | |
504 | 0 | if (wsi->socket_is_permanently_unusable || |
505 | 0 | reason == LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY || |
506 | 0 | lwsi_state(wsi) == LRS_SHUTDOWN) |
507 | 0 | goto just_kill_connection; |
508 | | |
509 | 0 | switch (lwsi_state_PRE_CLOSE(wsi)) { |
510 | 0 | case LRS_DEAD_SOCKET: |
511 | 0 | return; |
512 | | |
513 | | /* we tried the polite way... */ |
514 | 0 | case LRS_WAITING_TO_SEND_CLOSE: |
515 | 0 | case LRS_AWAITING_CLOSE_ACK: |
516 | 0 | case LRS_RETURNED_CLOSE: |
517 | 0 | goto just_kill_connection; |
518 | | |
519 | 0 | case LRS_FLUSHING_BEFORE_CLOSE: |
520 | 0 | if (lws_has_buffered_out(wsi) |
521 | | #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION) |
522 | | || wsi->http.comp_ctx.buflist_comp || |
523 | | wsi->http.comp_ctx.may_have_more |
524 | | #endif |
525 | 0 | ) { |
526 | 0 | lws_callback_on_writable(wsi); |
527 | 0 | return; |
528 | 0 | } |
529 | 0 | lwsl_wsi_info(wsi, " end LRS_FLUSHING_BEFORE_CLOSE"); |
530 | 0 | goto just_kill_connection; |
531 | 0 | default: |
532 | 0 | if (lws_has_buffered_out(wsi) |
533 | | #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION) |
534 | | || wsi->http.comp_ctx.buflist_comp || |
535 | | wsi->http.comp_ctx.may_have_more |
536 | | #endif |
537 | 0 | ) { |
538 | 0 | lwsl_wsi_info(wsi, "LRS_FLUSHING_BEFORE_CLOSE"); |
539 | 0 | lwsi_set_state(wsi, LRS_FLUSHING_BEFORE_CLOSE); |
540 | 0 | __lws_set_timeout(wsi, |
541 | 0 | PENDING_FLUSH_STORED_SEND_BEFORE_CLOSE, 5); |
542 | 0 | return; |
543 | 0 | } |
544 | 0 | break; |
545 | 0 | } |
546 | | |
547 | 0 | if (lwsi_state(wsi) == LRS_WAITING_CONNECT || |
548 | 0 | lwsi_state(wsi) == LRS_WAITING_DNS || |
549 | 0 | lwsi_state(wsi) == LRS_H1C_ISSUE_HANDSHAKE) |
550 | 0 | goto just_kill_connection; |
551 | | |
552 | 0 | if (!wsi->told_user_closed && wsi->user_space && wsi->a.protocol && |
553 | 0 | wsi->protocol_bind_balance) { |
554 | 0 | wsi->a.protocol->callback(wsi, |
555 | 0 | wsi->role_ops->protocol_unbind_cb[ |
556 | 0 | !!lwsi_role_server(wsi)], |
557 | 0 | wsi->user_space, (void *)__func__, 0); |
558 | 0 | wsi->protocol_bind_balance = 0; |
559 | 0 | } |
560 | | |
561 | | /* |
562 | | * signal we are closing, lws_write will |
563 | | * add any necessary version-specific stuff. If the write fails, |
564 | | * no worries we are closing anyway. If we didn't initiate this |
565 | | * close, then our state has been changed to |
566 | | * LRS_RETURNED_CLOSE and we will skip this. |
567 | | * |
568 | | * Likewise if it's a second call to close this connection after we |
569 | | * sent the close indication to the peer already, we are in state |
570 | | * LRS_AWAITING_CLOSE_ACK and will skip doing this a second time. |
571 | | */ |
572 | |
|
573 | 0 | if (lws_rops_fidx(wsi->role_ops, LWS_ROPS_close_via_role_protocol) && |
574 | 0 | lws_rops_func_fidx(wsi->role_ops, LWS_ROPS_close_via_role_protocol). |
575 | 0 | close_via_role_protocol(wsi, reason)) { |
576 | 0 | lwsl_wsi_info(wsi, "close_via_role took over (sockfd %d)", |
577 | 0 | wsi->desc.sockfd); |
578 | 0 | return; |
579 | 0 | } |
580 | | |
581 | 0 | just_kill_connection: |
582 | |
|
583 | 0 | lwsl_wsi_debug(wsi, "real just_kill_connection A: (sockfd %d)", |
584 | 0 | wsi->desc.sockfd); |
585 | |
|
586 | | #if defined(LWS_WITH_THREADPOOL) && defined(LWS_HAVE_PTHREAD_H) |
587 | | lws_threadpool_wsi_closing(wsi); |
588 | | #endif |
589 | |
|
590 | 0 | #if defined(LWS_WITH_FILE_OPS) && (defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)) |
591 | 0 | if (lwsi_role_http(wsi) && lwsi_role_server(wsi) && |
592 | 0 | wsi->http.fop_fd != NULL) |
593 | 0 | lws_vfs_file_close(&wsi->http.fop_fd); |
594 | 0 | #endif |
595 | |
|
596 | 0 | lws_sul_cancel(&wsi->sul_connect_timeout); |
597 | | #if defined(WIN32) |
598 | | lws_sul_cancel(&wsi->win32_sul_connect_async_check); |
599 | | #endif |
600 | | #if defined(LWS_WITH_SYS_ASYNC_DNS) |
601 | | lws_async_dns_cancel(wsi); |
602 | | #endif |
603 | |
|
604 | | #if defined(LWS_WITH_HTTP_PROXY) |
605 | | if (wsi->http.buflist_post_body) |
606 | | lws_buflist_destroy_all_segments(&wsi->http.buflist_post_body); |
607 | | #endif |
608 | 0 | #if defined(LWS_WITH_UDP) |
609 | 0 | if (wsi->udp) { |
610 | | /* confirm no sul left scheduled in wsi->udp itself */ |
611 | 0 | lws_sul_debug_zombies(wsi->a.context, wsi->udp, |
612 | 0 | sizeof(*wsi->udp), "close udp wsi"); |
613 | |
|
614 | 0 | lws_free_set_NULL(wsi->udp); |
615 | 0 | } |
616 | 0 | #endif |
617 | |
|
618 | 0 | if (lws_rops_fidx(wsi->role_ops, LWS_ROPS_close_kill_connection)) |
619 | 0 | lws_rops_func_fidx(wsi->role_ops, |
620 | 0 | LWS_ROPS_close_kill_connection). |
621 | 0 | close_kill_connection(wsi, reason); |
622 | |
|
623 | 0 | n = 0; |
624 | |
|
625 | 0 | if (!wsi->told_user_closed && wsi->user_space && |
626 | 0 | wsi->protocol_bind_balance && wsi->a.protocol) { |
627 | 0 | lwsl_debug("%s: %s: DROP_PROTOCOL %s\n", __func__, lws_wsi_tag(wsi), |
628 | 0 | wsi->a.protocol ? wsi->a.protocol->name: "NULL"); |
629 | 0 | if (wsi->a.protocol) |
630 | 0 | wsi->a.protocol->callback(wsi, |
631 | 0 | wsi->role_ops->protocol_unbind_cb[ |
632 | 0 | !!lwsi_role_server(wsi)], |
633 | 0 | wsi->user_space, (void *)__func__, 0); |
634 | 0 | wsi->protocol_bind_balance = 0; |
635 | 0 | } |
636 | |
|
637 | 0 | #if defined(LWS_WITH_CLIENT) |
638 | 0 | if (( |
639 | 0 | #if defined(LWS_ROLE_WS) |
640 | | /* |
641 | | * If our goal is a ws upgrade, effectively we did not reach |
642 | | * ESTABLISHED if we did not get the upgrade server reply |
643 | | */ |
644 | 0 | (lwsi_state(wsi) == LRS_WAITING_SERVER_REPLY && |
645 | 0 | wsi->role_ops == &role_ops_ws) || |
646 | 0 | #endif |
647 | 0 | lwsi_state(wsi) == LRS_WAITING_DNS || |
648 | 0 | lwsi_state(wsi) == LRS_WAITING_CONNECT) && |
649 | 0 | !wsi->already_did_cce && wsi->a.protocol && |
650 | 0 | !wsi->close_is_redirect) { |
651 | 0 | static const char _reason[] = "closed before established"; |
652 | |
|
653 | 0 | lwsl_wsi_debug(wsi, "closing in unestablished state 0x%x", |
654 | 0 | lwsi_state(wsi)); |
655 | 0 | wsi->socket_is_permanently_unusable = 1; |
656 | |
|
657 | 0 | lws_inform_client_conn_fail(wsi, |
658 | 0 | (void *)_reason, sizeof(_reason) - 1); |
659 | 0 | } |
660 | 0 | #endif |
661 | | |
662 | | /* |
663 | | * Testing with ab shows that we have to stage the socket close when |
664 | | * the system is under stress... shutdown any further TX, change the |
665 | | * state to one that won't emit anything more, and wait with a timeout |
666 | | * for the POLLIN to show a zero-size rx before coming back and doing |
667 | | * the actual close. |
668 | | */ |
669 | 0 | if (wsi->role_ops != &role_ops_raw_skt && !lwsi_role_client(wsi) && |
670 | 0 | lwsi_state(wsi) != LRS_SHUTDOWN && |
671 | 0 | lwsi_state(wsi) != LRS_UNCONNECTED && |
672 | 0 | reason != LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY && |
673 | 0 | !wsi->socket_is_permanently_unusable) { |
674 | |
|
675 | 0 | #if defined(LWS_WITH_TLS) |
676 | 0 | if (lws_is_ssl(wsi) && wsi->tls.ssl) { |
677 | 0 | n = 0; |
678 | 0 | switch (__lws_tls_shutdown(wsi)) { |
679 | 0 | case LWS_SSL_CAPABLE_DONE: |
680 | 0 | case LWS_SSL_CAPABLE_ERROR: |
681 | 0 | case LWS_SSL_CAPABLE_MORE_SERVICE_READ: |
682 | 0 | case LWS_SSL_CAPABLE_MORE_SERVICE_WRITE: |
683 | 0 | case LWS_SSL_CAPABLE_MORE_SERVICE: |
684 | 0 | if (wsi->lsp_channel++ == 8) { |
685 | 0 | lwsl_wsi_info(wsi, "avoiding shutdown spin"); |
686 | 0 | lwsi_set_state(wsi, LRS_SHUTDOWN); |
687 | 0 | } |
688 | 0 | break; |
689 | 0 | } |
690 | 0 | } else |
691 | 0 | #endif |
692 | 0 | { |
693 | 0 | lwsl_info("%s: shutdown conn: %s (sk %d, state 0x%x)\n", |
694 | 0 | __func__, lws_wsi_tag(wsi), (int)(lws_intptr_t)wsi->desc.sockfd, |
695 | 0 | lwsi_state(wsi)); |
696 | 0 | if (!wsi->socket_is_permanently_unusable && |
697 | 0 | lws_socket_is_valid(wsi->desc.sockfd)) { |
698 | 0 | wsi->socket_is_permanently_unusable = 1; |
699 | 0 | n = shutdown(wsi->desc.sockfd, SHUT_WR); |
700 | 0 | } |
701 | 0 | } |
702 | 0 | if (n) |
703 | 0 | lwsl_wsi_debug(wsi, "closing: shutdown (state 0x%x) ret %d", |
704 | 0 | lwsi_state(wsi), LWS_ERRNO); |
705 | | |
706 | | /* |
707 | | * This causes problems on WINCE / ESP32 with disconnection |
708 | | * when the events are half closing connection |
709 | | */ |
710 | 0 | #if !defined(_WIN32_WCE) && !defined(LWS_PLAT_FREERTOS) |
711 | | /* libuv: no event available to guarantee completion */ |
712 | 0 | if (!wsi->socket_is_permanently_unusable && |
713 | 0 | #if defined(LWS_WITH_CLIENT) |
714 | 0 | !wsi->close_is_redirect && |
715 | 0 | #endif |
716 | 0 | lws_socket_is_valid(wsi->desc.sockfd) && |
717 | 0 | lwsi_state(wsi) != LRS_SHUTDOWN && |
718 | 0 | (context->event_loop_ops->flags & LELOF_ISPOLL)) { |
719 | 0 | __lws_change_pollfd(wsi, LWS_POLLOUT, LWS_POLLIN); |
720 | 0 | lwsi_set_state(wsi, LRS_SHUTDOWN); |
721 | 0 | __lws_set_timeout(wsi, PENDING_TIMEOUT_SHUTDOWN_FLUSH, |
722 | 0 | (int)context->timeout_secs); |
723 | |
|
724 | 0 | return; |
725 | 0 | } |
726 | 0 | #endif |
727 | 0 | } |
728 | | |
729 | 0 | lwsl_wsi_info(wsi, "real just_kill_connection: sockfd %d\n", |
730 | 0 | wsi->desc.sockfd); |
731 | |
|
732 | | #ifdef LWS_WITH_HUBBUB |
733 | | if (wsi->http.rw) { |
734 | | lws_rewrite_destroy(wsi->http.rw); |
735 | | wsi->http.rw = NULL; |
736 | | } |
737 | | #endif |
738 | |
|
739 | 0 | if (wsi->http.pending_return_headers) |
740 | 0 | lws_free_set_NULL(wsi->http.pending_return_headers); |
741 | | |
742 | | /* |
743 | | * we won't be servicing or receiving anything further from this guy |
744 | | * delete socket from the internal poll list if still present |
745 | | */ |
746 | 0 | __lws_ssl_remove_wsi_from_buffered_list(wsi); |
747 | 0 | __lws_wsi_remove_from_sul(wsi); |
748 | | |
749 | | //if (wsi->told_event_loop_closed) // cgi std close case (dummy-callback) |
750 | | // return; |
751 | | |
752 | | /* checking return redundant since we anyway close */ |
753 | 0 | __remove_wsi_socket_from_fds(wsi); |
754 | |
|
755 | 0 | lwsi_set_state(wsi, LRS_DEAD_SOCKET); |
756 | 0 | lws_buflist_destroy_all_segments(&wsi->buflist); |
757 | 0 | lws_dll2_remove(&wsi->dll_buflist); |
758 | |
|
759 | 0 | if (lws_rops_fidx(wsi->role_ops, LWS_ROPS_close_role)) |
760 | 0 | lws_rops_func_fidx(wsi->role_ops, LWS_ROPS_close_role). |
761 | 0 | close_role(pt, wsi); |
762 | | |
763 | | /* tell the user it's all over for this guy */ |
764 | |
|
765 | 0 | ccb = 0; |
766 | 0 | if ((lwsi_state_est_PRE_CLOSE(wsi) || |
767 | | /* raw skt adopted but didn't complete tls hs should CLOSE */ |
768 | 0 | (wsi->role_ops == &role_ops_raw_skt && !lwsi_role_client(wsi)) || |
769 | 0 | lwsi_state_PRE_CLOSE(wsi) == LRS_WAITING_SERVER_REPLY) && |
770 | 0 | !wsi->told_user_closed && |
771 | 0 | wsi->role_ops->close_cb[lwsi_role_server(wsi)]) { |
772 | 0 | if (!wsi->upgraded_to_http2 || !lwsi_role_client(wsi)) |
773 | 0 | ccb = 1; |
774 | | /* |
775 | | * The network wsi for a client h2 connection shouldn't |
776 | | * call back for its role: the child stream connections |
777 | | * own the role. Otherwise h2 will call back closed |
778 | | * one too many times as the children do it and then |
779 | | * the closing network stream. |
780 | | */ |
781 | 0 | } |
782 | |
|
783 | 0 | if (!wsi->told_user_closed && |
784 | 0 | !lws_dll2_is_detached(&wsi->vh_awaiting_socket)) |
785 | | /* |
786 | | * He's a guy who go started with dns, but failed or is |
787 | | * caught with a shutdown before he got the result. We have |
788 | | * to issclient_mux_substream_wasue him a close cb |
789 | | */ |
790 | 0 | ccb = 1; |
791 | |
|
792 | 0 | lwsl_wsi_info(wsi, "cce=%d", ccb); |
793 | |
|
794 | 0 | pro = wsi->a.protocol; |
795 | |
|
796 | 0 | if (wsi->already_did_cce) |
797 | | /* |
798 | | * If we handled this by CLIENT_CONNECTION_ERROR, it's |
799 | | * mutually exclusive with CLOSE |
800 | | */ |
801 | 0 | ccb = 0; |
802 | |
|
803 | 0 | #if defined(LWS_WITH_CLIENT) |
804 | 0 | if (!wsi->close_is_redirect && !ccb && |
805 | 0 | (lwsi_state_PRE_CLOSE(wsi) & LWSIFS_NOT_EST) && |
806 | 0 | lwsi_role_client(wsi)) { |
807 | 0 | lws_inform_client_conn_fail(wsi, "Closed before conn", 18); |
808 | 0 | } |
809 | 0 | #endif |
810 | 0 | if (ccb |
811 | 0 | #if defined(LWS_WITH_CLIENT) |
812 | 0 | && !wsi->close_is_redirect |
813 | 0 | #endif |
814 | 0 | ) { |
815 | |
|
816 | 0 | if (!wsi->a.protocol && wsi->a.vhost && wsi->a.vhost->protocols) |
817 | 0 | pro = &wsi->a.vhost->protocols[0]; |
818 | |
|
819 | 0 | if (pro && pro->callback && wsi->role_ops) |
820 | 0 | pro->callback(wsi, |
821 | 0 | wsi->role_ops->close_cb[lwsi_role_server(wsi)], |
822 | 0 | wsi->user_space, NULL, 0); |
823 | 0 | wsi->told_user_closed = 1; |
824 | 0 | } |
825 | |
|
826 | 0 | #if defined(LWS_ROLE_RAW_FILE) |
827 | 0 | async_close: |
828 | 0 | #endif |
829 | |
|
830 | 0 | #if defined(LWS_WITH_SECURE_STREAMS) |
831 | 0 | if (wsi->for_ss) { |
832 | 0 | lwsl_wsi_debug(wsi, "for_ss"); |
833 | | /* |
834 | | * We were adopted for a particular ss, but, eg, we may not |
835 | | * have succeeded with the connection... we are closing which is |
836 | | * good, but we have to invalidate any pointer the related ss |
837 | | * handle may be holding on us |
838 | | */ |
839 | | #if defined(LWS_WITH_SECURE_STREAMS_PROXY_API) |
840 | | |
841 | | if (wsi->client_proxy_onward) { |
842 | | /* |
843 | | * We are an onward proxied wsi at the proxy, |
844 | | * opaque is proxing "conn", we must remove its pointer |
845 | | * to us since we are destroying |
846 | | */ |
847 | | lws_proxy_clean_conn_ss(wsi); |
848 | | } else |
849 | | |
850 | | if (wsi->client_bound_sspc) { |
851 | | lws_sspc_handle_t *h = (lws_sspc_handle_t *)wsi->a.opaque_user_data; |
852 | | |
853 | | if (h) { // && (h->info.flags & LWSSSINFLAGS_ACCEPTED)) { |
854 | | |
855 | | #if defined(LWS_WITH_SYS_METRICS) |
856 | | /* |
857 | | * If any hanging caliper measurement, dump it, and free any tags |
858 | | */ |
859 | | lws_metrics_caliper_report_hist(h->cal_txn, (struct lws *)NULL); |
860 | | #endif |
861 | | |
862 | | h->txp_path.priv_onw = NULL; |
863 | | //wsi->a.opaque_user_data = NULL; |
864 | | } |
865 | | } else |
866 | | #endif |
867 | 0 | { |
868 | 0 | hh = (lws_ss_handle_t *)wsi->a.opaque_user_data; |
869 | |
|
870 | 0 | if (hh) { // && (h->info.flags & LWSSSINFLAGS_ACCEPTED)) { |
871 | | |
872 | | /* |
873 | | * ss level: only reports if dangling caliper |
874 | | * not already reported |
875 | | */ |
876 | 0 | lws_metrics_caliper_report_hist(hh->cal_txn, wsi); |
877 | |
|
878 | 0 | hh->wsi = NULL; |
879 | 0 | wsi->a.opaque_user_data = NULL; |
880 | 0 | } |
881 | 0 | } |
882 | 0 | } |
883 | 0 | #endif |
884 | | |
885 | |
|
886 | 0 | lws_remove_child_from_any_parent(wsi); |
887 | 0 | wsi->socket_is_permanently_unusable = 1; |
888 | |
|
889 | 0 | if (wsi->a.context->event_loop_ops->wsi_logical_close) |
890 | 0 | if (wsi->a.context->event_loop_ops->wsi_logical_close(wsi)) |
891 | 0 | return; |
892 | | |
893 | 0 | __lws_close_free_wsi_final(wsi); |
894 | |
|
895 | 0 | #if defined(LWS_WITH_SECURE_STREAMS) |
896 | 0 | if (hh && hh->ss_dangling_connected && |
897 | 0 | lws_ss_event_helper(hh, LWSSSCS_DISCONNECTED) == LWSSSSRET_DESTROY_ME) |
898 | 0 | lws_ss_destroy(&hh); |
899 | 0 | #endif |
900 | 0 | } |
901 | | |
902 | | |
903 | | /* cx + vh lock */ |
904 | | |
905 | | void |
906 | | __lws_close_free_wsi_final(struct lws *wsi) |
907 | 0 | { |
908 | 0 | int n; |
909 | |
|
910 | 0 | if (!wsi->shadow && |
911 | 0 | lws_socket_is_valid(wsi->desc.sockfd) && !lws_ssl_close(wsi)) { |
912 | 0 | lwsl_wsi_debug(wsi, "fd %d", wsi->desc.sockfd); |
913 | | |
914 | | /* |
915 | | * if this is the pt pipe, skip the actual close, |
916 | | * go through the motions though so we will reach 0 open wsi |
917 | | * on the pt, and trigger the pt destroy to close the pipe fds |
918 | | */ |
919 | 0 | if (!lws_plat_pipe_is_fd_assocated(wsi->a.context, wsi->tsi, |
920 | 0 | wsi->desc.sockfd)) { |
921 | 0 | n = compatible_close(wsi->desc.sockfd); |
922 | 0 | if (n) |
923 | 0 | lwsl_wsi_debug(wsi, "closing: close ret %d", |
924 | 0 | LWS_ERRNO); |
925 | 0 | } |
926 | |
|
927 | 0 | __remove_wsi_socket_from_fds(wsi); |
928 | 0 | if (lws_socket_is_valid(wsi->desc.sockfd)) |
929 | 0 | delete_from_fd(wsi->a.context, wsi->desc.sockfd); |
930 | |
|
931 | 0 | #if !defined(LWS_PLAT_FREERTOS) && !defined(WIN32) && !defined(LWS_PLAT_OPTEE) |
932 | 0 | delete_from_fdwsi(wsi->a.context, wsi); |
933 | 0 | #endif |
934 | |
|
935 | 0 | sanity_assert_no_sockfd_traces(wsi->a.context, wsi->desc.sockfd); |
936 | 0 | } |
937 | | |
938 | | /* ... if we're closing the cancel pipe, account for it */ |
939 | |
|
940 | 0 | { |
941 | 0 | struct lws_context_per_thread *pt = |
942 | 0 | &wsi->a.context->pt[(int)wsi->tsi]; |
943 | |
|
944 | 0 | if (pt->pipe_wsi == wsi) |
945 | 0 | pt->pipe_wsi = NULL; |
946 | 0 | if (pt->dummy_pipe_fds[0] == wsi->desc.sockfd) |
947 | 0 | { |
948 | 0 | #if !defined(LWS_PLAT_FREERTOS) |
949 | 0 | pt->dummy_pipe_fds[0] = LWS_SOCK_INVALID; |
950 | 0 | #endif |
951 | 0 | } |
952 | 0 | } |
953 | |
|
954 | 0 | wsi->desc.sockfd = LWS_SOCK_INVALID; |
955 | |
|
956 | 0 | #if defined(LWS_WITH_CLIENT) |
957 | 0 | lws_free_set_NULL(wsi->cli_hostname_copy); |
958 | 0 | if (wsi->close_is_redirect) { |
959 | |
|
960 | 0 | wsi->close_is_redirect = 0; |
961 | |
|
962 | 0 | lwsl_wsi_info(wsi, "picking up redirection"); |
963 | |
|
964 | 0 | lws_role_transition(wsi, LWSIFR_CLIENT, LRS_UNCONNECTED, |
965 | 0 | &role_ops_h1); |
966 | |
|
967 | 0 | #if defined(LWS_WITH_HTTP2) |
968 | 0 | if (wsi->client_mux_substream_was) |
969 | 0 | wsi->h2.END_STREAM = wsi->h2.END_HEADERS = 0; |
970 | 0 | #endif |
971 | 0 | #if defined(LWS_ROLE_H2) || defined(LWS_ROLE_MQTT) |
972 | 0 | if (wsi->mux.parent_wsi) { |
973 | 0 | lws_wsi_mux_sibling_disconnect(wsi); |
974 | 0 | wsi->mux.parent_wsi = NULL; |
975 | 0 | } |
976 | 0 | #endif |
977 | |
|
978 | 0 | #if defined(LWS_WITH_TLS) |
979 | 0 | memset(&wsi->tls, 0, sizeof(wsi->tls)); |
980 | 0 | #endif |
981 | | |
982 | | // wsi->a.protocol = NULL; |
983 | 0 | if (wsi->a.protocol) |
984 | 0 | lws_bind_protocol(wsi, wsi->a.protocol, "client_reset"); |
985 | 0 | wsi->pending_timeout = NO_PENDING_TIMEOUT; |
986 | 0 | wsi->hdr_parsing_completed = 0; |
987 | |
|
988 | 0 | #if defined(LWS_WITH_TLS) |
989 | 0 | if (wsi->stash->cis[CIS_ALPN]) |
990 | 0 | lws_strncpy(wsi->alpn, wsi->stash->cis[CIS_ALPN], |
991 | 0 | sizeof(wsi->alpn)); |
992 | 0 | #endif |
993 | |
|
994 | 0 | if (lws_header_table_attach(wsi, 0)) { |
995 | 0 | lwsl_wsi_err(wsi, "failed to get ah"); |
996 | 0 | return; |
997 | 0 | } |
998 | | // } |
999 | | //_lws_header_table_reset(wsi->http.ah); |
1000 | | |
1001 | 0 | #if defined(LWS_WITH_TLS) |
1002 | 0 | wsi->tls.use_ssl = (unsigned int)wsi->flags; |
1003 | 0 | #endif |
1004 | |
|
1005 | | #if defined(LWS_WITH_TLS_JIT_TRUST) |
1006 | | if (wsi->stash && wsi->stash->cis[CIS_ADDRESS]) { |
1007 | | struct lws_vhost *vh = NULL; |
1008 | | lws_tls_jit_trust_vhost_bind(wsi->a.context, |
1009 | | wsi->stash->cis[CIS_ADDRESS], |
1010 | | &vh); |
1011 | | if (vh) { |
1012 | | if (!vh->count_bound_wsi && vh->grace_after_unref) { |
1013 | | lwsl_wsi_info(wsi, "%s in use\n", |
1014 | | vh->lc.gutag); |
1015 | | lws_sul_cancel(&vh->sul_unref); |
1016 | | } |
1017 | | vh->count_bound_wsi++; |
1018 | | wsi->a.vhost = vh; |
1019 | | } |
1020 | | } |
1021 | | #endif |
1022 | |
|
1023 | 0 | return; |
1024 | 0 | } |
1025 | 0 | #endif |
1026 | | |
1027 | | /* outermost destroy notification for wsi (user_space still intact) */ |
1028 | 0 | if (wsi->a.vhost) |
1029 | 0 | wsi->a.vhost->protocols[0].callback(wsi, LWS_CALLBACK_WSI_DESTROY, |
1030 | 0 | wsi->user_space, NULL, 0); |
1031 | |
|
1032 | | #ifdef LWS_WITH_CGI |
1033 | | if (wsi->http.cgi) { |
1034 | | lws_spawn_piped_destroy(&wsi->http.cgi->lsp); |
1035 | | lws_sul_cancel(&wsi->http.cgi->sul_grace); |
1036 | | lws_free_set_NULL(wsi->http.cgi); |
1037 | | } |
1038 | | #endif |
1039 | |
|
1040 | | #if defined(LWS_WITH_SYS_FAULT_INJECTION) |
1041 | | lws_fi_destroy(&wsi->fic); |
1042 | | #endif |
1043 | |
|
1044 | 0 | __lws_wsi_remove_from_sul(wsi); |
1045 | 0 | sanity_assert_no_wsi_traces(wsi->a.context, wsi); |
1046 | 0 | __lws_free_wsi(wsi); |
1047 | 0 | } |
1048 | | |
1049 | | |
1050 | | void |
1051 | | lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason, const char *caller) |
1052 | 0 | { |
1053 | 0 | struct lws_context *cx = wsi->a.context; |
1054 | 0 | struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi]; |
1055 | |
|
1056 | 0 | lws_context_lock(cx, __func__); |
1057 | |
|
1058 | 0 | lws_pt_lock(pt, __func__); |
1059 | | /* may destroy vhost, cannot hold vhost lock outside it */ |
1060 | 0 | __lws_close_free_wsi(wsi, reason, caller); |
1061 | 0 | lws_pt_unlock(pt); |
1062 | |
|
1063 | 0 | lws_context_unlock(cx); |
1064 | 0 | } |
1065 | | |
1066 | | |