/src/libwebsockets/lib/core-net/vhost.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * libwebsockets - small server side websockets and web server implementation |
3 | | * |
4 | | * Copyright (C) 2010 - 2021 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 | | |
27 | | void |
28 | | lws_tls_session_vh_destroy(struct lws_vhost *vh); |
29 | | |
30 | | const struct lws_role_ops *available_roles[] = { |
31 | | #if defined(LWS_ROLE_H2) |
32 | | &role_ops_h2, |
33 | | #endif |
34 | | #if defined(LWS_ROLE_H1) |
35 | | &role_ops_h1, |
36 | | #endif |
37 | | #if defined(LWS_ROLE_WS) |
38 | | &role_ops_ws, |
39 | | #endif |
40 | | #if defined(LWS_ROLE_DBUS) |
41 | | &role_ops_dbus, |
42 | | #endif |
43 | | #if defined(LWS_ROLE_RAW_PROXY) |
44 | | &role_ops_raw_proxy, |
45 | | #endif |
46 | | #if defined(LWS_ROLE_MQTT) && defined(LWS_WITH_CLIENT) |
47 | | &role_ops_mqtt, |
48 | | #endif |
49 | | #if defined(LWS_WITH_NETLINK) |
50 | | &role_ops_netlink, |
51 | | #endif |
52 | | NULL |
53 | | }; |
54 | | |
55 | | #if defined(LWS_WITH_ABSTRACT) |
56 | | const struct lws_protocols *available_abstract_protocols[] = { |
57 | | #if defined(LWS_ROLE_RAW) |
58 | | &protocol_abs_client_raw_skt, |
59 | | #endif |
60 | | NULL |
61 | | }; |
62 | | #endif |
63 | | |
64 | | #if defined(LWS_WITH_SECURE_STREAMS) |
65 | | const struct lws_protocols *available_secstream_protocols[] = { |
66 | | #if defined(LWS_ROLE_H1) |
67 | | &protocol_secstream_h1, |
68 | | #endif |
69 | | #if defined(LWS_ROLE_H2) |
70 | | &protocol_secstream_h2, |
71 | | #endif |
72 | | #if defined(LWS_ROLE_WS) |
73 | | &protocol_secstream_ws, |
74 | | #endif |
75 | | #if defined(LWS_ROLE_MQTT) |
76 | | &protocol_secstream_mqtt, |
77 | | #endif |
78 | | &protocol_secstream_raw, |
79 | | NULL |
80 | | }; |
81 | | #endif |
82 | | |
83 | | static const char * const mount_protocols[] = { |
84 | | "http://", |
85 | | "https://", |
86 | | "file://", |
87 | | "cgi://", |
88 | | ">http://", |
89 | | ">https://", |
90 | | "callback://" |
91 | | }; |
92 | | |
93 | | const struct lws_role_ops * |
94 | | lws_role_by_name(const char *name) |
95 | 0 | { |
96 | 0 | LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar) |
97 | 0 | if (!strcmp(ar->name, name)) |
98 | 0 | return ar; |
99 | 0 | LWS_FOR_EVERY_AVAILABLE_ROLE_END; |
100 | |
|
101 | 0 | if (!strcmp(name, role_ops_raw_skt.name)) |
102 | 0 | return &role_ops_raw_skt; |
103 | | |
104 | 0 | #if defined(LWS_ROLE_RAW_FILE) |
105 | 0 | if (!strcmp(name, role_ops_raw_file.name)) |
106 | 0 | return &role_ops_raw_file; |
107 | 0 | #endif |
108 | | |
109 | 0 | return NULL; |
110 | 0 | } |
111 | | |
112 | | int |
113 | | lws_role_call_alpn_negotiated(struct lws *wsi, const char *alpn) |
114 | 0 | { |
115 | 0 | #if defined(LWS_WITH_TLS) |
116 | 0 | if (!alpn) |
117 | 0 | return 0; |
118 | | |
119 | 0 | #if !defined(LWS_ESP_PLATFORM) |
120 | 0 | lwsl_wsi_info(wsi, "'%s'", alpn); |
121 | 0 | #endif |
122 | |
|
123 | 0 | LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar) |
124 | 0 | if (ar->alpn && !strcmp(ar->alpn, alpn) && |
125 | 0 | lws_rops_fidx(ar, LWS_ROPS_alpn_negotiated)) { |
126 | 0 | #if defined(LWS_WITH_SERVER) |
127 | 0 | lws_metrics_tag_wsi_add(wsi, "upg", ar->name); |
128 | 0 | #endif |
129 | 0 | return (lws_rops_func_fidx(ar, LWS_ROPS_alpn_negotiated)). |
130 | 0 | alpn_negotiated(wsi, alpn); |
131 | 0 | } |
132 | 0 | LWS_FOR_EVERY_AVAILABLE_ROLE_END; |
133 | 0 | #endif |
134 | 0 | return 0; |
135 | 0 | } |
136 | | |
137 | | int |
138 | | lws_role_call_adoption_bind(struct lws *wsi, int type, const char *prot) |
139 | 0 | { |
140 | 0 | int n; |
141 | | |
142 | | /* |
143 | | * if the vhost is told to bind accepted sockets to a given role, |
144 | | * then look it up by name and try to bind to the specific role. |
145 | | */ |
146 | 0 | if (lws_check_opt(wsi->a.vhost->options, |
147 | 0 | LWS_SERVER_OPTION_ADOPT_APPLY_LISTEN_ACCEPT_CONFIG) && |
148 | 0 | wsi->a.vhost->listen_accept_role) { |
149 | 0 | const struct lws_role_ops *role = |
150 | 0 | lws_role_by_name(wsi->a.vhost->listen_accept_role); |
151 | |
|
152 | 0 | if (!prot) |
153 | 0 | prot = wsi->a.vhost->listen_accept_protocol; |
154 | |
|
155 | 0 | if (!role) |
156 | 0 | lwsl_wsi_err(wsi, "can't find role '%s'", |
157 | 0 | wsi->a.vhost->listen_accept_role); |
158 | |
|
159 | 0 | if (!strcmp(wsi->a.vhost->listen_accept_role, "raw-proxy")) |
160 | 0 | type |= LWS_ADOPT_FLAG_RAW_PROXY; |
161 | |
|
162 | 0 | if (role && lws_rops_fidx(role, LWS_ROPS_adoption_bind)) { |
163 | 0 | n = (lws_rops_func_fidx(role, LWS_ROPS_adoption_bind)). |
164 | 0 | adoption_bind(wsi, type, prot); |
165 | 0 | if (n < 0) |
166 | 0 | return -1; |
167 | 0 | if (n) /* did the bind */ |
168 | 0 | return 0; |
169 | 0 | } |
170 | | |
171 | 0 | if (type & _LWS_ADOPT_FINISH) { |
172 | 0 | lwsl_wsi_debug(wsi, "leaving bound to role %s", |
173 | 0 | wsi->role_ops->name); |
174 | 0 | return 0; |
175 | 0 | } |
176 | | |
177 | 0 | lwsl_wsi_warn(wsi, "adoption bind to role '%s', " |
178 | 0 | "protocol '%s', type 0x%x, failed", |
179 | 0 | wsi->a.vhost->listen_accept_role, prot, type); |
180 | 0 | } |
181 | | |
182 | | /* |
183 | | * Otherwise ask each of the roles in order of preference if they |
184 | | * want to bind to this accepted socket |
185 | | */ |
186 | | |
187 | 0 | LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar) |
188 | 0 | if (lws_rops_fidx(ar, LWS_ROPS_adoption_bind) && |
189 | 0 | (lws_rops_func_fidx(ar, LWS_ROPS_adoption_bind)). |
190 | 0 | adoption_bind(wsi, type, prot)) |
191 | 0 | return 0; |
192 | 0 | LWS_FOR_EVERY_AVAILABLE_ROLE_END; |
193 | | |
194 | | /* fall back to raw socket role if, eg, h1 not configured */ |
195 | |
|
196 | 0 | if (lws_rops_fidx(&role_ops_raw_skt, LWS_ROPS_adoption_bind) && |
197 | 0 | (lws_rops_func_fidx(&role_ops_raw_skt, LWS_ROPS_adoption_bind)). |
198 | 0 | adoption_bind(wsi, type, prot)) |
199 | 0 | return 0; |
200 | | |
201 | 0 | #if defined(LWS_ROLE_RAW_FILE) |
202 | | |
203 | 0 | lwsl_wsi_info(wsi, "falling back to raw file role bind"); |
204 | | |
205 | | /* fall back to raw file role if, eg, h1 not configured */ |
206 | |
|
207 | 0 | if (lws_rops_fidx(&role_ops_raw_file, LWS_ROPS_adoption_bind) && |
208 | 0 | (lws_rops_func_fidx(&role_ops_raw_file, LWS_ROPS_adoption_bind)). |
209 | 0 | adoption_bind(wsi, type, prot)) |
210 | 0 | return 0; |
211 | 0 | #endif |
212 | | |
213 | 0 | return 1; |
214 | 0 | } |
215 | | |
216 | | #if defined(LWS_WITH_CLIENT) |
217 | | int |
218 | | lws_role_call_client_bind(struct lws *wsi, |
219 | | const struct lws_client_connect_info *i) |
220 | 0 | { |
221 | 0 | LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar) |
222 | 0 | if (lws_rops_fidx(ar, LWS_ROPS_client_bind)) { |
223 | 0 | int m = (lws_rops_func_fidx(ar, LWS_ROPS_client_bind)). |
224 | 0 | client_bind(wsi, i); |
225 | |
|
226 | 0 | if (m < 0) |
227 | 0 | return m; |
228 | 0 | if (m) |
229 | 0 | return 0; |
230 | 0 | } |
231 | 0 | LWS_FOR_EVERY_AVAILABLE_ROLE_END; |
232 | | |
233 | | /* fall back to raw socket role if, eg, h1 not configured */ |
234 | |
|
235 | 0 | if (lws_rops_fidx(&role_ops_raw_skt, LWS_ROPS_client_bind) && |
236 | 0 | (lws_rops_func_fidx(&role_ops_raw_skt, LWS_ROPS_client_bind)). |
237 | 0 | client_bind(wsi, i)) |
238 | 0 | return 0; |
239 | | |
240 | 0 | return 1; |
241 | 0 | } |
242 | | #endif |
243 | | |
244 | | void * |
245 | | lws_protocol_vh_priv_zalloc(struct lws_vhost *vhost, |
246 | | const struct lws_protocols *prot, int size) |
247 | 0 | { |
248 | 0 | int n = 0; |
249 | |
|
250 | 0 | if (!vhost || !prot || !vhost->protocols || !prot->name) |
251 | 0 | return NULL; |
252 | | |
253 | | /* allocate the vh priv array only on demand */ |
254 | 0 | if (!vhost->protocol_vh_privs) { |
255 | 0 | vhost->protocol_vh_privs = (void **)lws_zalloc( |
256 | 0 | (size_t)vhost->count_protocols * sizeof(void *), |
257 | 0 | "protocol_vh_privs"); |
258 | |
|
259 | 0 | if (!vhost->protocol_vh_privs) |
260 | 0 | return NULL; |
261 | 0 | } |
262 | | |
263 | 0 | while (n < vhost->count_protocols && &vhost->protocols[n] != prot) |
264 | 0 | n++; |
265 | |
|
266 | 0 | if (n == vhost->count_protocols) { |
267 | 0 | n = 0; |
268 | 0 | while (n < vhost->count_protocols) { |
269 | 0 | if (vhost->protocols[n].name && |
270 | 0 | !strcmp(vhost->protocols[n].name, prot->name)) |
271 | 0 | break; |
272 | 0 | n++; |
273 | 0 | } |
274 | |
|
275 | 0 | if (n == vhost->count_protocols) { |
276 | 0 | lwsl_vhost_err(vhost, "unknown protocol %p", prot); |
277 | 0 | return NULL; |
278 | 0 | } |
279 | 0 | } |
280 | | |
281 | 0 | vhost->protocol_vh_privs[n] = lws_zalloc((size_t)size, "vh priv"); |
282 | 0 | return vhost->protocol_vh_privs[n]; |
283 | 0 | } |
284 | | |
285 | | void * |
286 | | lws_protocol_vh_priv_get(struct lws_vhost *vhost, |
287 | | const struct lws_protocols *prot) |
288 | 0 | { |
289 | 0 | int n = 0; |
290 | |
|
291 | 0 | if (!vhost || !vhost->protocols || |
292 | 0 | !vhost->protocol_vh_privs || !prot || !prot->name) |
293 | 0 | return NULL; |
294 | | |
295 | 0 | while (n < vhost->count_protocols && &vhost->protocols[n] != prot) |
296 | 0 | n++; |
297 | |
|
298 | 0 | if (n == vhost->count_protocols) { |
299 | 0 | n = 0; |
300 | 0 | while (n < vhost->count_protocols) { |
301 | 0 | if (vhost->protocols[n].name && |
302 | 0 | !strcmp(vhost->protocols[n].name, prot->name)) |
303 | 0 | break; |
304 | 0 | n++; |
305 | 0 | } |
306 | |
|
307 | 0 | if (n == vhost->count_protocols) { |
308 | 0 | lwsl_vhost_err(vhost, "unknown protocol %p", prot); |
309 | 0 | return NULL; |
310 | 0 | } |
311 | 0 | } |
312 | | |
313 | 0 | return vhost->protocol_vh_privs[n]; |
314 | 0 | } |
315 | | |
316 | | void * |
317 | | lws_vhd_find_by_pvo(struct lws_context *cx, const char *protname, |
318 | | const char *pvo_name, const char *pvo_value) |
319 | 0 | { |
320 | 0 | struct lws_vhost *vh; |
321 | 0 | int n; |
322 | | |
323 | | /* let's go through all the vhosts */ |
324 | |
|
325 | 0 | vh = cx->vhost_list; |
326 | 0 | while (vh) { |
327 | |
|
328 | 0 | if (vh->protocol_vh_privs) { |
329 | |
|
330 | 0 | for (n = 0; n < vh->count_protocols; n++) { |
331 | 0 | const struct lws_protocol_vhost_options *pv; |
332 | |
|
333 | 0 | if (strcmp(vh->protocols[n].name, protname)) |
334 | 0 | continue; |
335 | | |
336 | | /* this vh has an instance of the required protocol */ |
337 | | |
338 | 0 | pv = lws_pvo_search(vh->pvo, protname); |
339 | 0 | if (!pv) |
340 | 0 | continue; |
341 | | |
342 | 0 | pv = lws_pvo_search(pv->options, pvo_name); |
343 | 0 | if (!pv) |
344 | 0 | continue; |
345 | | |
346 | | /* ... he also has a pvo of the right name... */ |
347 | 0 | if (!strcmp(pv->value, pvo_value)) |
348 | | /* |
349 | | * ... yes, the pvo has the right value too, |
350 | | * return a pointer to this vhost-protocol |
351 | | * private alloc (ie, its "vhd") |
352 | | */ |
353 | 0 | return vh->protocol_vh_privs[n]; |
354 | 0 | } |
355 | 0 | } else |
356 | 0 | lwsl_vhost_notice(vh, "no privs yet"); |
357 | 0 | vh = vh->vhost_next; |
358 | 0 | } |
359 | | |
360 | 0 | return NULL; |
361 | 0 | } |
362 | | |
363 | | const struct lws_protocol_vhost_options * |
364 | | lws_vhost_protocol_options(struct lws_vhost *vh, const char *name) |
365 | 0 | { |
366 | 0 | const struct lws_protocol_vhost_options *pvo = vh->pvo; |
367 | |
|
368 | 0 | if (!name) |
369 | 0 | return NULL; |
370 | | |
371 | 0 | while (pvo) { |
372 | 0 | if (!strcmp(pvo->name, name)) |
373 | 0 | return pvo; |
374 | 0 | pvo = pvo->next; |
375 | 0 | } |
376 | | |
377 | 0 | return NULL; |
378 | 0 | } |
379 | | |
380 | | int |
381 | | lws_protocol_init_vhost(struct lws_vhost *vh, int *any) |
382 | 0 | { |
383 | 0 | const struct lws_protocol_vhost_options *pvo, *pvo1; |
384 | 0 | int n; |
385 | | #if defined(LWS_PLAT_FREERTOS) |
386 | | struct lws_a _lwsa, *lwsa = &_lwsa; |
387 | | |
388 | | memset(&_lwsa, 0, sizeof(_lwsa)); |
389 | | #else |
390 | 0 | struct lws _lws; |
391 | 0 | struct lws_a *lwsa = &_lws.a; |
392 | |
|
393 | 0 | memset(&_lws, 0, sizeof(_lws)); |
394 | 0 | #endif |
395 | |
|
396 | 0 | lwsa->context = vh->context; |
397 | 0 | lwsa->vhost = vh; |
398 | | |
399 | | /* initialize supported protocols on this vhost */ |
400 | |
|
401 | 0 | for (n = 0; n < vh->count_protocols; n++) { |
402 | 0 | lwsa->protocol = &vh->protocols[n]; |
403 | 0 | if (!vh->protocols[n].name) |
404 | 0 | continue; |
405 | | |
406 | 0 | pvo = lws_vhost_protocol_options(vh, vh->protocols[n].name); |
407 | 0 | if (pvo) { |
408 | | /* |
409 | | * linked list of options specific to |
410 | | * vh + protocol |
411 | | */ |
412 | 0 | pvo1 = pvo; |
413 | 0 | pvo = pvo1->options; |
414 | |
|
415 | 0 | while (pvo) { |
416 | 0 | lwsl_vhost_debug(vh, "protocol \"%s\", " |
417 | 0 | "option \"%s\"", |
418 | 0 | vh->protocols[n].name, |
419 | 0 | pvo->name); |
420 | |
|
421 | 0 | if (!strcmp(pvo->name, "default")) { |
422 | 0 | lwsl_vhost_info(vh, "Setting default " |
423 | 0 | "protocol to %s", |
424 | 0 | vh->protocols[n].name); |
425 | 0 | vh->default_protocol_index = (unsigned char)n; |
426 | 0 | } |
427 | 0 | if (!strcmp(pvo->name, "raw")) { |
428 | 0 | lwsl_vhost_info(vh, "Setting raw " |
429 | 0 | "protocol to %s", |
430 | 0 | vh->protocols[n].name); |
431 | 0 | vh->raw_protocol_index = (unsigned char)n; |
432 | 0 | } |
433 | 0 | pvo = pvo->next; |
434 | 0 | } |
435 | 0 | } else |
436 | 0 | lwsl_vhost_debug(vh, "not instantiating %s", |
437 | 0 | vh->protocols[n].name); |
438 | |
|
439 | 0 | #if defined(LWS_WITH_TLS) |
440 | 0 | if (any) |
441 | 0 | *any |= !!vh->tls.ssl_ctx; |
442 | 0 | #endif |
443 | |
|
444 | 0 | pvo = lws_vhost_protocol_options(vh, vh->protocols[n].name); |
445 | | |
446 | | /* |
447 | | * inform all the protocols that they are doing their |
448 | | * one-time initialization if they want to. |
449 | | * |
450 | | * NOTE the fakewsi is garbage, except the key pointers that are |
451 | | * prepared in case the protocol handler wants to touch them |
452 | | */ |
453 | |
|
454 | 0 | if (pvo |
455 | 0 | #if !defined(LWS_WITH_PLUGINS) |
456 | | /* |
457 | | * with plugins, you have to explicitly |
458 | | * instantiate them per-vhost with pvos. |
459 | | * |
460 | | * Without plugins, not setting the vhost pvo |
461 | | * list at creation enables all the protocols |
462 | | * by default, for backwards compatibility |
463 | | */ |
464 | 0 | || !vh->pvo |
465 | 0 | #endif |
466 | 0 | ) { |
467 | 0 | lwsl_vhost_info(vh, "init %s.%s", vh->name, |
468 | 0 | vh->protocols[n].name); |
469 | 0 | vh->protocol_init |= 1u << n; |
470 | 0 | if (vh->protocols[n].callback((struct lws *)lwsa, |
471 | 0 | LWS_CALLBACK_PROTOCOL_INIT, NULL, |
472 | 0 | #if !defined(LWS_WITH_PLUGINS) |
473 | 0 | (void *)(pvo ? pvo->options : NULL), |
474 | | #else |
475 | | (void *)pvo->options, |
476 | | #endif |
477 | 0 | 0)) { |
478 | 0 | if (vh->protocol_vh_privs && vh->protocol_vh_privs[n]) { |
479 | 0 | lws_free(vh->protocol_vh_privs[n]); |
480 | 0 | vh->protocol_vh_privs[n] = NULL; |
481 | 0 | } |
482 | 0 | lwsl_vhost_err(vh, "protocol %s failed init", |
483 | 0 | vh->protocols[n].name); |
484 | |
|
485 | 0 | return 1; |
486 | 0 | } |
487 | 0 | } |
488 | 0 | } |
489 | | |
490 | 0 | vh->created_vhost_protocols = 1; |
491 | |
|
492 | 0 | return 0; |
493 | 0 | } |
494 | | |
495 | | /* |
496 | | * inform every vhost that hasn't already done it, that |
497 | | * his protocols are initializing |
498 | | */ |
499 | | int |
500 | | lws_protocol_init(struct lws_context *context) |
501 | 0 | { |
502 | 0 | struct lws_vhost *vh = context->vhost_list; |
503 | 0 | int any = 0, r = 0; |
504 | |
|
505 | 0 | if (context->doing_protocol_init) |
506 | 0 | return 0; |
507 | | |
508 | 0 | context->doing_protocol_init = 1; |
509 | |
|
510 | 0 | lwsl_cx_info(context, "\n"); |
511 | |
|
512 | 0 | while (vh) { |
513 | | |
514 | | /* only do the protocol init once for a given vhost */ |
515 | 0 | if (vh->created_vhost_protocols || |
516 | 0 | (lws_check_opt(vh->options, LWS_SERVER_OPTION_SKIP_PROTOCOL_INIT))) |
517 | 0 | goto next; |
518 | | |
519 | 0 | if (lws_protocol_init_vhost(vh, &any)) { |
520 | 0 | lwsl_vhost_warn(vh, "init vhost %s failed", vh->name); |
521 | 0 | r = -1; |
522 | 0 | } |
523 | 0 | next: |
524 | 0 | vh = vh->vhost_next; |
525 | 0 | } |
526 | | |
527 | 0 | context->doing_protocol_init = 0; |
528 | |
|
529 | 0 | if (r) |
530 | 0 | lwsl_cx_warn(context, "some protocols did not init"); |
531 | |
|
532 | 0 | if (!context->protocol_init_done) { |
533 | |
|
534 | 0 | context->protocol_init_done = 1; |
535 | 0 | lws_finalize_startup(context); |
536 | |
|
537 | 0 | return 0; |
538 | 0 | } |
539 | | |
540 | 0 | #if defined(LWS_WITH_SERVER) |
541 | 0 | if (any) { |
542 | 0 | lws_tls_check_all_cert_lifetimes(context); |
543 | 0 | } |
544 | 0 | #endif |
545 | |
|
546 | 0 | return 0; |
547 | 0 | } |
548 | | |
549 | | |
550 | | /* list of supported protocols and callbacks */ |
551 | | |
552 | | static const struct lws_protocols protocols_dummy[] = { |
553 | | /* first protocol must always be HTTP handler */ |
554 | | |
555 | | { |
556 | | "http-only", /* name */ |
557 | | lws_callback_http_dummy, /* callback */ |
558 | | 0, /* per_session_data_size */ |
559 | | 0, /* rx_buffer_size */ |
560 | | 0, /* id */ |
561 | | NULL, /* user */ |
562 | | 0 /* tx_packet_size */ |
563 | | }, |
564 | | /* |
565 | | * the other protocols are provided by lws plugins |
566 | | */ |
567 | | { NULL, NULL, 0, 0, 0, NULL, 0} /* terminator */ |
568 | | }; |
569 | | |
570 | | |
571 | | #ifdef LWS_PLAT_OPTEE |
572 | | #undef LWS_HAVE_GETENV |
573 | | #endif |
574 | | |
575 | | struct lws_vhost * |
576 | | lws_create_vhost(struct lws_context *context, |
577 | | const struct lws_context_creation_info *info) |
578 | 0 | { |
579 | 0 | struct lws_vhost *vh, **vh1 = &context->vhost_list; |
580 | 0 | const struct lws_http_mount *mounts; |
581 | 0 | const struct lws_protocols *pcols = info->protocols; |
582 | | #ifdef LWS_WITH_PLUGINS |
583 | | struct lws_plugin *plugin = context->plugin_list; |
584 | | #endif |
585 | 0 | struct lws_protocols *lwsp; |
586 | 0 | int m, f = !info->pvo, fx = 0, abs_pcol_count = 0, sec_pcol_count = 0; |
587 | 0 | const char *name = "default"; |
588 | 0 | char buf[96]; |
589 | 0 | char *p; |
590 | | #if defined(LWS_WITH_SYS_ASYNC_DNS) |
591 | | extern struct lws_protocols lws_async_dns_protocol; |
592 | | #endif |
593 | 0 | int n; |
594 | |
|
595 | 0 | if (!pcols && context->protocols_copy) |
596 | 0 | pcols = context->protocols_copy; |
597 | |
|
598 | 0 | if (info->vhost_name) |
599 | 0 | name = info->vhost_name; |
600 | |
|
601 | 0 | if (lws_fi(&info->fic, "vh_create_oom")) |
602 | 0 | vh = NULL; |
603 | 0 | else |
604 | 0 | vh = lws_zalloc(sizeof(*vh) + strlen(name) + 1 |
605 | | #if defined(LWS_WITH_EVENT_LIBS) |
606 | | + context->event_loop_ops->evlib_size_vh |
607 | | #endif |
608 | 0 | , __func__); |
609 | 0 | if (!vh) |
610 | 0 | goto early_bail; |
611 | | |
612 | 0 | if (info->log_cx) |
613 | 0 | vh->lc.log_cx = info->log_cx; |
614 | 0 | else |
615 | 0 | vh->lc.log_cx = &log_cx; |
616 | |
|
617 | | #if defined(LWS_WITH_EVENT_LIBS) |
618 | | vh->evlib_vh = (void *)&vh[1]; |
619 | | vh->name = (const char *)vh->evlib_vh + |
620 | | context->event_loop_ops->evlib_size_vh; |
621 | | #else |
622 | 0 | vh->name = (const char *)&vh[1]; |
623 | 0 | #endif |
624 | 0 | memcpy((char *)vh->name, name, strlen(name) + 1); |
625 | |
|
626 | | #if LWS_MAX_SMP > 1 |
627 | | lws_mutex_refcount_init(&vh->mr); |
628 | | #endif |
629 | |
|
630 | 0 | if (!pcols && !info->pprotocols) |
631 | 0 | pcols = &protocols_dummy[0]; |
632 | |
|
633 | 0 | vh->context = context; |
634 | 0 | { |
635 | 0 | char *end = buf + sizeof(buf) - 1; |
636 | 0 | p = buf; |
637 | |
|
638 | 0 | p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), "%s", vh->name); |
639 | 0 | if (info->iface) |
640 | 0 | p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), "|%s", info->iface); |
641 | 0 | if (info->port && !(info->port & 0xffff)) |
642 | 0 | p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), "|%u", info->port); |
643 | 0 | } |
644 | |
|
645 | 0 | __lws_lc_tag(context, &context->lcg[LWSLCG_VHOST], &vh->lc, "%s|%s|%d", |
646 | 0 | buf, info->iface ? info->iface : "", info->port); |
647 | |
|
648 | | #if defined(LWS_WITH_SYS_FAULT_INJECTION) |
649 | | vh->fic.name = "vh"; |
650 | | if (info->fic.fi_owner.count) |
651 | | /* |
652 | | * This moves all the lws_fi_t from info->fi to the vhost fi, |
653 | | * leaving it empty |
654 | | */ |
655 | | lws_fi_import(&vh->fic, &info->fic); |
656 | | |
657 | | lws_fi_inherit_copy(&vh->fic, &context->fic, "vh", vh->name); |
658 | | if (lws_fi(&vh->fic, "vh_create_oom")) |
659 | | goto bail; |
660 | | #endif |
661 | |
|
662 | 0 | #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2) |
663 | 0 | vh->http.error_document_404 = info->error_document_404; |
664 | 0 | #endif |
665 | |
|
666 | 0 | if (lws_check_opt(info->options, LWS_SERVER_OPTION_ONLY_RAW)) |
667 | 0 | lwsl_vhost_info(vh, "set to only support RAW"); |
668 | |
|
669 | 0 | vh->iface = info->iface; |
670 | 0 | #if !defined(LWS_PLAT_FREERTOS) && !defined(OPTEE_TA) && !defined(WIN32) |
671 | 0 | vh->bind_iface = info->bind_iface; |
672 | 0 | #endif |
673 | 0 | #if defined(LWS_WITH_CLIENT) |
674 | 0 | if (info->connect_timeout_secs) |
675 | 0 | vh->connect_timeout_secs = (int)info->connect_timeout_secs; |
676 | 0 | else |
677 | 0 | vh->connect_timeout_secs = 20; |
678 | 0 | #endif |
679 | | /* apply the context default lws_retry */ |
680 | |
|
681 | 0 | if (info->retry_and_idle_policy) |
682 | 0 | vh->retry_policy = info->retry_and_idle_policy; |
683 | 0 | else |
684 | 0 | vh->retry_policy = &context->default_retry; |
685 | | |
686 | | /* |
687 | | * let's figure out how many protocols the user is handing us, using the |
688 | | * old or new way depending on what he gave us |
689 | | */ |
690 | |
|
691 | 0 | if (!pcols) { |
692 | 0 | for (vh->count_protocols = 0; |
693 | 0 | info->pprotocols[vh->count_protocols]; |
694 | 0 | vh->count_protocols++) |
695 | 0 | ; |
696 | | //lwsl_user("%s: ppcols: %s\n", __func__, |
697 | | // info->pprotocols[vh->count_protocols]->name); |
698 | 0 | } else |
699 | 0 | for (vh->count_protocols = 0; |
700 | 0 | pcols[vh->count_protocols].callback; |
701 | 0 | vh->count_protocols++) |
702 | 0 | ; |
703 | |
|
704 | 0 | vh->options = info->options; |
705 | 0 | vh->pvo = info->pvo; |
706 | 0 | vh->headers = info->headers; |
707 | 0 | vh->user = info->user; |
708 | 0 | vh->finalize = info->finalize; |
709 | 0 | vh->finalize_arg = info->finalize_arg; |
710 | 0 | vh->listen_accept_role = info->listen_accept_role; |
711 | 0 | vh->listen_accept_protocol = info->listen_accept_protocol; |
712 | 0 | vh->unix_socket_perms = info->unix_socket_perms; |
713 | 0 | vh->fo_listen_queue = info->fo_listen_queue; |
714 | |
|
715 | 0 | LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar) |
716 | 0 | if (lws_rops_fidx(ar, LWS_ROPS_init_vhost) && |
717 | 0 | (lws_rops_func_fidx(ar, LWS_ROPS_init_vhost)).init_vhost(vh, info)) |
718 | 0 | return NULL; |
719 | 0 | LWS_FOR_EVERY_AVAILABLE_ROLE_END; |
720 | | |
721 | |
|
722 | 0 | if (info->keepalive_timeout) |
723 | 0 | vh->keepalive_timeout = info->keepalive_timeout; |
724 | 0 | else |
725 | 0 | vh->keepalive_timeout = 5; |
726 | |
|
727 | 0 | if (info->timeout_secs_ah_idle) |
728 | 0 | vh->timeout_secs_ah_idle = (int)info->timeout_secs_ah_idle; |
729 | 0 | else |
730 | 0 | vh->timeout_secs_ah_idle = 10; |
731 | |
|
732 | 0 | #if defined(LWS_WITH_TLS) |
733 | |
|
734 | 0 | vh->tls.alpn = info->alpn; |
735 | 0 | vh->tls.ssl_info_event_mask = info->ssl_info_event_mask; |
736 | |
|
737 | 0 | if (info->ecdh_curve) |
738 | 0 | lws_strncpy(vh->tls.ecdh_curve, info->ecdh_curve, |
739 | 0 | sizeof(vh->tls.ecdh_curve)); |
740 | | |
741 | | /* carefully allocate and take a copy of cert + key paths if present */ |
742 | 0 | n = 0; |
743 | 0 | if (info->ssl_cert_filepath) |
744 | 0 | n += (int)strlen(info->ssl_cert_filepath) + 1; |
745 | 0 | if (info->ssl_private_key_filepath) |
746 | 0 | n += (int)strlen(info->ssl_private_key_filepath) + 1; |
747 | |
|
748 | 0 | if (n) { |
749 | 0 | vh->tls.key_path = vh->tls.alloc_cert_path = |
750 | 0 | lws_malloc((unsigned int)n, "vh paths"); |
751 | 0 | if (info->ssl_cert_filepath) { |
752 | 0 | n = (int)strlen(info->ssl_cert_filepath) + 1; |
753 | 0 | memcpy(vh->tls.alloc_cert_path, |
754 | 0 | info->ssl_cert_filepath, (unsigned int)n); |
755 | 0 | vh->tls.key_path += n; |
756 | 0 | } |
757 | 0 | if (info->ssl_private_key_filepath) |
758 | 0 | memcpy(vh->tls.key_path, info->ssl_private_key_filepath, |
759 | 0 | strlen(info->ssl_private_key_filepath) + 1); |
760 | 0 | } |
761 | 0 | #endif |
762 | |
|
763 | | #if defined(LWS_WITH_HTTP_PROXY) && defined(LWS_ROLE_WS) |
764 | | fx = 1; |
765 | | #endif |
766 | | #if defined(LWS_WITH_ABSTRACT) |
767 | | abs_pcol_count = (int)LWS_ARRAY_SIZE(available_abstract_protocols) - 1; |
768 | | #endif |
769 | 0 | #if defined(LWS_WITH_SECURE_STREAMS) |
770 | 0 | sec_pcol_count = (int)LWS_ARRAY_SIZE(available_secstream_protocols) - 1; |
771 | 0 | #endif |
772 | | |
773 | | /* |
774 | | * give the vhost a unified list of protocols including: |
775 | | * |
776 | | * - internal, async_dns if enabled (first vhost only) |
777 | | * - internal, abstracted ones |
778 | | * - the ones that came from plugins |
779 | | * - his user protocols |
780 | | */ |
781 | |
|
782 | 0 | if (lws_fi(&vh->fic, "vh_create_pcols_oom")) |
783 | 0 | lwsp = NULL; |
784 | 0 | else |
785 | 0 | lwsp = lws_zalloc(sizeof(struct lws_protocols) * |
786 | 0 | ((unsigned int)vh->count_protocols + |
787 | 0 | (unsigned int)abs_pcol_count + |
788 | 0 | (unsigned int)sec_pcol_count + |
789 | 0 | (unsigned int)context->plugin_protocol_count + |
790 | 0 | (unsigned int)fx + 1), "vh plugin table"); |
791 | 0 | if (!lwsp) { |
792 | 0 | lwsl_err("OOM\n"); |
793 | 0 | goto bail; |
794 | 0 | } |
795 | | |
796 | | /* |
797 | | * 1: user protocols (from pprotocols or protocols) |
798 | | */ |
799 | | |
800 | 0 | m = vh->count_protocols; |
801 | 0 | if (!pcols) { |
802 | 0 | for (n = 0; n < m; n++) |
803 | 0 | memcpy(&lwsp[n], info->pprotocols[n], sizeof(lwsp[0])); |
804 | 0 | } else |
805 | 0 | memcpy(lwsp, pcols, sizeof(struct lws_protocols) * (unsigned int)m); |
806 | | |
807 | | /* |
808 | | * 2: abstract protocols |
809 | | */ |
810 | | #if defined(LWS_WITH_ABSTRACT) |
811 | | for (n = 0; n < abs_pcol_count; n++) { |
812 | | memcpy(&lwsp[m++], available_abstract_protocols[n], |
813 | | sizeof(*lwsp)); |
814 | | vh->count_protocols++; |
815 | | } |
816 | | #endif |
817 | | /* |
818 | | * 3: async dns protocol (first vhost only) |
819 | | */ |
820 | | #if defined(LWS_WITH_SYS_ASYNC_DNS) |
821 | | if (!context->vhost_list) { |
822 | | uint8_t seen = 0; |
823 | | |
824 | | for (n = 0; n < m; n++) |
825 | | if (!memcmp(&lwsp[n], &lws_async_dns_protocol, sizeof(struct lws_protocols))) { |
826 | | /* Already defined */ |
827 | | seen = 1; |
828 | | break; |
829 | | } |
830 | | |
831 | | if (!seen) { |
832 | | memcpy(&lwsp[m++], &lws_async_dns_protocol, |
833 | | sizeof(struct lws_protocols)); |
834 | | vh->count_protocols++; |
835 | | } |
836 | | } |
837 | | #endif |
838 | |
|
839 | 0 | #if defined(LWS_WITH_SECURE_STREAMS) |
840 | 0 | for (n = 0; n < sec_pcol_count; n++) { |
841 | 0 | memcpy(&lwsp[m++], available_secstream_protocols[n], |
842 | 0 | sizeof(*lwsp)); |
843 | 0 | vh->count_protocols++; |
844 | 0 | } |
845 | 0 | #endif |
846 | | |
847 | | /* |
848 | | * 3: For compatibility, all protocols enabled on vhost if only |
849 | | * the default vhost exists. Otherwise only vhosts who ask |
850 | | * for a protocol get it enabled. |
851 | | */ |
852 | |
|
853 | 0 | if (context->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS) |
854 | 0 | f = 0; |
855 | 0 | (void)f; |
856 | | #ifdef LWS_WITH_PLUGINS |
857 | | if (plugin) { |
858 | | while (plugin) { |
859 | | const lws_plugin_protocol_t *plpr = |
860 | | (const lws_plugin_protocol_t *)plugin->hdr; |
861 | | |
862 | | for (n = 0; n < plpr->count_protocols; n++) { |
863 | | /* |
864 | | * for compatibility's sake, no pvo implies |
865 | | * allow all protocols |
866 | | */ |
867 | | if (f || lws_vhost_protocol_options(vh, |
868 | | plpr->protocols[n].name)) { |
869 | | memcpy(&lwsp[m], |
870 | | &plpr->protocols[n], |
871 | | sizeof(struct lws_protocols)); |
872 | | m++; |
873 | | vh->count_protocols++; |
874 | | } |
875 | | } |
876 | | plugin = plugin->list; |
877 | | } |
878 | | } |
879 | | #endif |
880 | |
|
881 | | #if defined(LWS_WITH_HTTP_PROXY) && defined(LWS_ROLE_WS) |
882 | | memcpy(&lwsp[m++], &lws_ws_proxy, sizeof(*lwsp)); |
883 | | vh->count_protocols++; |
884 | | #endif |
885 | |
|
886 | 0 | vh->protocols = lwsp; |
887 | 0 | vh->allocated_vhost_protocols = 1; |
888 | |
|
889 | 0 | vh->same_vh_protocol_owner = (struct lws_dll2_owner *) |
890 | 0 | lws_zalloc(sizeof(struct lws_dll2_owner) * |
891 | 0 | (unsigned int)vh->count_protocols, "same vh list"); |
892 | 0 | #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2) |
893 | 0 | vh->http.mount_list = info->mounts; |
894 | 0 | #endif |
895 | |
|
896 | | #if defined(LWS_WITH_SYS_METRICS) && defined(LWS_WITH_SERVER) |
897 | | { |
898 | | char *end = buf + sizeof(buf) - 1; |
899 | | p = buf; |
900 | | |
901 | | p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), "vh.%s", vh->name); |
902 | | if (info->iface) |
903 | | p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), ".%s", info->iface); |
904 | | if (info->port && !(info->port & 0xffff)) |
905 | | p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), ".%u", info->port); |
906 | | p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), ".rx"); |
907 | | vh->mt_traffic_rx = lws_metric_create(context, 0, buf); |
908 | | p[-2] = 't'; |
909 | | vh->mt_traffic_tx = lws_metric_create(context, 0, buf); |
910 | | } |
911 | | #endif |
912 | |
|
913 | 0 | #ifdef LWS_WITH_UNIX_SOCK |
914 | 0 | if (LWS_UNIX_SOCK_ENABLED(vh)) { |
915 | 0 | lwsl_vhost_info(vh, "Creating '%s' path \"%s\", %d protocols", |
916 | 0 | vh->name, vh->iface, vh->count_protocols); |
917 | 0 | } else |
918 | 0 | #endif |
919 | 0 | { |
920 | 0 | switch(info->port) { |
921 | 0 | case CONTEXT_PORT_NO_LISTEN: |
922 | 0 | strcpy(buf, "(serving disabled)"); |
923 | 0 | break; |
924 | 0 | case CONTEXT_PORT_NO_LISTEN_SERVER: |
925 | 0 | strcpy(buf, "(no listener)"); |
926 | 0 | break; |
927 | 0 | default: |
928 | 0 | lws_snprintf(buf, sizeof(buf), "port %u", info->port); |
929 | 0 | break; |
930 | 0 | } |
931 | 0 | lwsl_vhost_info(vh, "Creating Vhost '%s' %s, %d protocols, IPv6 %s", |
932 | 0 | vh->name, buf, vh->count_protocols, |
933 | 0 | LWS_IPV6_ENABLED(vh) ? "on" : "off"); |
934 | 0 | } |
935 | 0 | mounts = info->mounts; |
936 | 0 | while (mounts) { |
937 | 0 | (void)mount_protocols[0]; |
938 | 0 | lwsl_vhost_info(vh, " mounting %s%s to %s", |
939 | 0 | mount_protocols[mounts->origin_protocol], |
940 | 0 | mounts->origin ? mounts->origin : "none", |
941 | 0 | mounts->mountpoint); |
942 | |
|
943 | 0 | mounts = mounts->mount_next; |
944 | 0 | } |
945 | |
|
946 | 0 | vh->listen_port = info->port; |
947 | |
|
948 | | #if defined(LWS_WITH_SOCKS5) |
949 | | vh->socks_proxy_port = 0; |
950 | | vh->socks_proxy_address[0] = '\0'; |
951 | | #endif |
952 | |
|
953 | 0 | #if defined(LWS_WITH_CLIENT) && defined(LWS_CLIENT_HTTP_PROXYING) |
954 | | /* either use proxy from info, or try get it from env var */ |
955 | 0 | #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2) |
956 | 0 | vh->http.http_proxy_port = 0; |
957 | 0 | vh->http.http_proxy_address[0] = '\0'; |
958 | | /* http proxy */ |
959 | 0 | if (info->http_proxy_address) { |
960 | | /* override for backwards compatibility */ |
961 | 0 | if (info->http_proxy_port) |
962 | 0 | vh->http.http_proxy_port = info->http_proxy_port; |
963 | 0 | lws_set_proxy(vh, info->http_proxy_address); |
964 | 0 | } else |
965 | 0 | #endif |
966 | 0 | { |
967 | 0 | #ifdef LWS_HAVE_GETENV |
968 | | #if defined(__COVERITY__) |
969 | | p = NULL; |
970 | | #else |
971 | 0 | p = getenv("http_proxy"); /* coverity[tainted_scalar] */ |
972 | 0 | if (p) { |
973 | 0 | lws_strncpy(buf, p, sizeof(buf)); |
974 | 0 | lws_set_proxy(vh, buf); |
975 | 0 | } |
976 | 0 | #endif |
977 | 0 | #endif |
978 | 0 | } |
979 | 0 | #endif |
980 | | #if defined(LWS_WITH_SOCKS5) |
981 | | lws_socks5c_ads_server(vh, info); |
982 | | #endif |
983 | |
|
984 | 0 | vh->ka_time = info->ka_time; |
985 | 0 | vh->ka_interval = info->ka_interval; |
986 | 0 | vh->ka_probes = info->ka_probes; |
987 | |
|
988 | 0 | if (vh->options & LWS_SERVER_OPTION_STS) |
989 | 0 | lwsl_vhost_notice(vh, " STS enabled"); |
990 | |
|
991 | | #ifdef LWS_WITH_ACCESS_LOG |
992 | | if (info->log_filepath) { |
993 | | if (lws_fi(&vh->fic, "vh_create_access_log_open_fail")) |
994 | | vh->log_fd = (int)LWS_INVALID_FILE; |
995 | | else |
996 | | vh->log_fd = lws_open(info->log_filepath, |
997 | | O_CREAT | O_APPEND | O_RDWR, 0600); |
998 | | if (vh->log_fd == (int)LWS_INVALID_FILE) { |
999 | | lwsl_vhost_err(vh, "unable to open log filepath %s", |
1000 | | info->log_filepath); |
1001 | | goto bail; |
1002 | | } |
1003 | | #ifndef WIN32 |
1004 | | if (context->uid != (uid_t)-1) |
1005 | | if (chown(info->log_filepath, context->uid, |
1006 | | context->gid) == -1) |
1007 | | lwsl_vhost_err(vh, "unable to chown log file %s", |
1008 | | info->log_filepath); |
1009 | | #endif |
1010 | | } else |
1011 | | vh->log_fd = (int)LWS_INVALID_FILE; |
1012 | | #endif |
1013 | 0 | if (lws_fi(&vh->fic, "vh_create_ssl_srv") || |
1014 | 0 | lws_context_init_server_ssl(info, vh)) { |
1015 | 0 | lwsl_vhost_err(vh, "lws_context_init_server_ssl failed"); |
1016 | 0 | goto bail1; |
1017 | 0 | } |
1018 | 0 | #if defined(LWS_WITH_CLIENT) |
1019 | 0 | if (lws_fi(&vh->fic, "vh_create_ssl_cli") || |
1020 | 0 | lws_context_init_client_ssl(info, vh)) { |
1021 | 0 | lwsl_vhost_err(vh, "lws_context_init_client_ssl failed"); |
1022 | 0 | goto bail1; |
1023 | 0 | } |
1024 | 0 | #endif |
1025 | 0 | #if defined(LWS_WITH_SERVER) |
1026 | 0 | lws_context_lock(context, __func__); |
1027 | 0 | if (lws_fi(&vh->fic, "vh_create_srv_init")) |
1028 | 0 | n = -1; |
1029 | 0 | else |
1030 | 0 | n = _lws_vhost_init_server(info, vh); |
1031 | 0 | lws_context_unlock(context); |
1032 | 0 | if (n < 0) { |
1033 | 0 | lwsl_vhost_err(vh, "init server failed\n"); |
1034 | 0 | goto bail1; |
1035 | 0 | } |
1036 | 0 | #endif |
1037 | | |
1038 | | #if defined(LWS_WITH_SYS_ASYNC_DNS) |
1039 | | n = !!context->vhost_list; |
1040 | | #endif |
1041 | | |
1042 | 0 | while (1) { |
1043 | 0 | if (!(*vh1)) { |
1044 | 0 | *vh1 = vh; |
1045 | 0 | break; |
1046 | 0 | } |
1047 | 0 | vh1 = &(*vh1)->vhost_next; |
1048 | 0 | }; |
1049 | |
|
1050 | | #if defined(LWS_WITH_SYS_ASYNC_DNS) |
1051 | | if (!n) |
1052 | | lws_async_dns_init(context); |
1053 | | #endif |
1054 | | |
1055 | | /* for the case we are adding a vhost much later, after server init */ |
1056 | |
|
1057 | 0 | if (context->protocol_init_done) |
1058 | 0 | if (lws_fi(&vh->fic, "vh_create_protocol_init") || |
1059 | 0 | lws_protocol_init(context)) { |
1060 | 0 | lwsl_vhost_err(vh, "lws_protocol_init failed"); |
1061 | 0 | goto bail1; |
1062 | 0 | } |
1063 | | |
1064 | 0 | return vh; |
1065 | | |
1066 | 0 | bail1: |
1067 | 0 | lws_vhost_destroy(vh); |
1068 | |
|
1069 | 0 | return NULL; |
1070 | | |
1071 | 0 | bail: |
1072 | 0 | __lws_lc_untag(vh->context, &vh->lc); |
1073 | 0 | lws_fi_destroy(&vh->fic); |
1074 | 0 | lws_free(vh); |
1075 | |
|
1076 | 0 | early_bail: |
1077 | 0 | lws_fi_destroy(&info->fic); |
1078 | |
|
1079 | 0 | return NULL; |
1080 | 0 | } |
1081 | | |
1082 | | void |
1083 | | lws_vhost_set_mounts(struct lws_vhost *vh, const struct lws_http_mount *mounts) |
1084 | 0 | { |
1085 | 0 | #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2) |
1086 | 0 | vh->http.mount_list = mounts; |
1087 | 0 | #endif |
1088 | 0 | } |
1089 | | |
1090 | | int |
1091 | | lws_init_vhost_client_ssl(const struct lws_context_creation_info *info, |
1092 | | struct lws_vhost *vhost) |
1093 | 0 | { |
1094 | 0 | struct lws_context_creation_info i; |
1095 | |
|
1096 | 0 | memcpy(&i, info, sizeof(i)); |
1097 | 0 | i.port = CONTEXT_PORT_NO_LISTEN; |
1098 | |
|
1099 | 0 | return lws_context_init_client_ssl(&i, vhost); |
1100 | 0 | } |
1101 | | |
1102 | | void |
1103 | | lws_cancel_service_pt(struct lws *wsi) |
1104 | 0 | { |
1105 | 0 | lws_plat_pipe_signal(wsi->a.context, wsi->tsi); |
1106 | 0 | } |
1107 | | |
1108 | | void |
1109 | | lws_cancel_service(struct lws_context *context) |
1110 | 0 | { |
1111 | 0 | struct lws_context_per_thread *pt = &context->pt[0]; |
1112 | 0 | unsigned short m; |
1113 | |
|
1114 | 0 | if (context->service_no_longer_possible) |
1115 | 0 | return; |
1116 | | |
1117 | 0 | lwsl_cx_debug(context, "\n"); |
1118 | |
|
1119 | 0 | for (m = 0; m < context->count_threads; m++) { |
1120 | 0 | if (pt->pipe_wsi) |
1121 | 0 | lws_plat_pipe_signal(pt->context, m); |
1122 | 0 | pt++; |
1123 | 0 | } |
1124 | 0 | } |
1125 | | |
1126 | | int |
1127 | | __lws_create_event_pipes(struct lws_context *context) |
1128 | 0 | { |
1129 | 0 | struct lws_context_per_thread *pt; |
1130 | 0 | struct lws *wsi; |
1131 | 0 | int n; |
1132 | | |
1133 | | /* |
1134 | | * Create the pt event pipes... these are unique in that they are |
1135 | | * not bound to a vhost or protocol (both are NULL) |
1136 | | */ |
1137 | |
|
1138 | | #if LWS_MAX_SMP > 1 |
1139 | | for (n = 0; n < context->count_threads; n++) { |
1140 | | #else |
1141 | 0 | n = 0; |
1142 | 0 | { |
1143 | 0 | #endif |
1144 | 0 | pt = &context->pt[n]; |
1145 | |
|
1146 | 0 | if (pt->pipe_wsi) |
1147 | 0 | return 0; |
1148 | | |
1149 | 0 | wsi = __lws_wsi_create_with_role(context, n, &role_ops_pipe, |
1150 | 0 | NULL); |
1151 | 0 | if (!wsi) |
1152 | 0 | return 1; |
1153 | | |
1154 | 0 | __lws_lc_tag(context, &context->lcg[LWSLCG_WSI], &wsi->lc, |
1155 | 0 | "pipe"); |
1156 | |
|
1157 | 0 | wsi->event_pipe = 1; |
1158 | 0 | pt->pipe_wsi = wsi; |
1159 | |
|
1160 | 0 | if (!lws_plat_pipe_create(wsi)) { |
1161 | | /* |
1162 | | * platform code returns 0 if it actually created pipes |
1163 | | * and initialized pt->dummy_pipe_fds[]. If it used |
1164 | | * some other mechanism outside of signaling in the |
1165 | | * normal event loop, we skip treating the pipe as |
1166 | | * related to dummy_pipe_fds[], adding it to the fds, |
1167 | | * etc. |
1168 | | */ |
1169 | |
|
1170 | 0 | wsi->desc.sockfd = context->pt[n].dummy_pipe_fds[0]; |
1171 | | // lwsl_debug("event pipe fd %d\n", wsi->desc.sockfd); |
1172 | |
|
1173 | 0 | if (lws_wsi_inject_to_loop(pt, wsi)) |
1174 | 0 | goto bail; |
1175 | 0 | return 0; |
1176 | 0 | } |
1177 | 0 | } |
1178 | | |
1179 | 0 | return 0; |
1180 | | |
1181 | 0 | bail: |
1182 | |
|
1183 | 0 | return 1; |
1184 | 0 | } |
1185 | | |
1186 | | void |
1187 | | lws_destroy_event_pipe(struct lws *wsi) |
1188 | 0 | { |
1189 | 0 | int n; |
1190 | |
|
1191 | 0 | lwsl_wsi_info(wsi, "in"); |
1192 | |
|
1193 | 0 | n = lws_wsi_extract_from_loop(wsi); |
1194 | 0 | lws_plat_pipe_close(wsi); |
1195 | 0 | if (!n) |
1196 | 0 | lws_free(wsi); |
1197 | 0 | } |
1198 | | |
1199 | | /* |
1200 | | * Start close process for any wsi bound to this vhost that belong to the |
1201 | | * service thread we are called from. Because of async event lib close, or |
1202 | | * protocol staged close on wsi, latency with pts joining in closing their |
1203 | | * wsi on the vhost, this may take some time. |
1204 | | * |
1205 | | * When the wsi count bound to the vhost (from all pts) drops to zero, the |
1206 | | * vhost destruction will be finalized. |
1207 | | */ |
1208 | | |
1209 | | void |
1210 | | __lws_vhost_destroy_pt_wsi_dieback_start(struct lws_vhost *vh) |
1211 | 0 | { |
1212 | | #if LWS_MAX_SMP > 1 |
1213 | | /* calling pt thread has done its wsi dieback */ |
1214 | | int tsi = lws_pthread_self_to_tsi(vh->context); |
1215 | | #else |
1216 | 0 | int tsi = 0; |
1217 | 0 | #endif |
1218 | 0 | struct lws_context *ctx = vh->context; |
1219 | 0 | struct lws_context_per_thread *pt = &ctx->pt[tsi]; |
1220 | 0 | unsigned int n; |
1221 | |
|
1222 | | #if LWS_MAX_SMP > 1 |
1223 | | if (vh->close_flow_vs_tsi[lws_pthread_self_to_tsi(vh->context)]) |
1224 | | /* this pt has already done its bit */ |
1225 | | return; |
1226 | | #endif |
1227 | |
|
1228 | 0 | #if defined(LWS_WITH_CLIENT) |
1229 | | /* |
1230 | | * destroy any wsi that are associated with us but have no socket |
1231 | | * (and will otherwise be missed for destruction) |
1232 | | */ |
1233 | 0 | lws_start_foreach_dll_safe(struct lws_dll2 *, d, d1, |
1234 | 0 | vh->vh_awaiting_socket_owner.head) { |
1235 | 0 | struct lws *w = |
1236 | 0 | lws_container_of(d, struct lws, vh_awaiting_socket); |
1237 | |
|
1238 | 0 | if (w->tsi == tsi) { |
1239 | |
|
1240 | 0 | lwsl_vhost_debug(vh, "closing aso"); |
1241 | 0 | lws_wsi_close(w, LWS_TO_KILL_ASYNC); |
1242 | 0 | } |
1243 | |
|
1244 | 0 | } lws_end_foreach_dll_safe(d, d1); |
1245 | 0 | #endif |
1246 | | |
1247 | | /* |
1248 | | * Close any wsi on this pt bound to the vhost |
1249 | | */ |
1250 | |
|
1251 | 0 | n = 0; |
1252 | 0 | while (n < pt->fds_count) { |
1253 | 0 | struct lws *wsi = wsi_from_fd(ctx, pt->fds[n].fd); |
1254 | |
|
1255 | 0 | if (wsi && wsi->tsi == tsi && wsi->a.vhost == vh) { |
1256 | |
|
1257 | 0 | lwsl_wsi_debug(wsi, "pt %d: closin, role %s", tsi, |
1258 | 0 | wsi->role_ops->name); |
1259 | |
|
1260 | 0 | lws_wsi_close(wsi, LWS_TO_KILL_ASYNC); |
1261 | |
|
1262 | 0 | if (pt->pipe_wsi == wsi) |
1263 | 0 | pt->pipe_wsi = NULL; |
1264 | 0 | } |
1265 | 0 | n++; |
1266 | 0 | } |
1267 | |
|
1268 | | #if LWS_MAX_SMP > 1 |
1269 | | /* calling pt thread has done its wsi dieback */ |
1270 | | vh->close_flow_vs_tsi[lws_pthread_self_to_tsi(vh->context)] = 1; |
1271 | | #endif |
1272 | 0 | } |
1273 | | |
1274 | | #if defined(LWS_WITH_NETWORK) |
1275 | | |
1276 | | /* returns nonzero if v1 and v2 can share listen sockets */ |
1277 | | int |
1278 | | lws_vhost_compare_listen(struct lws_vhost *v1, struct lws_vhost *v2) |
1279 | 0 | { |
1280 | 0 | return ((!v1->iface && !v2->iface) || |
1281 | 0 | (v1->iface && v2->iface && !strcmp(v1->iface, v2->iface))) && |
1282 | 0 | v1->listen_port == v2->listen_port; |
1283 | 0 | } |
1284 | | |
1285 | | /* helper to interate every listen socket on any vhost and call cb on it */ |
1286 | | int |
1287 | | lws_vhost_foreach_listen_wsi(struct lws_context *cx, void *arg, |
1288 | | lws_dll2_foreach_cb_t cb) |
1289 | 0 | { |
1290 | 0 | struct lws_vhost *v = cx->vhost_list; |
1291 | 0 | int n; |
1292 | |
|
1293 | 0 | while (v) { |
1294 | |
|
1295 | 0 | n = lws_dll2_foreach_safe(&v->listen_wsi, arg, cb); |
1296 | 0 | if (n) |
1297 | 0 | return n; |
1298 | | |
1299 | 0 | v = v->vhost_next; |
1300 | 0 | } |
1301 | | |
1302 | 0 | return 0; |
1303 | 0 | } |
1304 | | |
1305 | | #endif |
1306 | | |
1307 | | /* |
1308 | | * Mark the vhost as being destroyed, so things trying to use it abort. |
1309 | | * |
1310 | | * Dispose of the listen socket. |
1311 | | */ |
1312 | | |
1313 | | void |
1314 | | lws_vhost_destroy1(struct lws_vhost *vh) |
1315 | 0 | { |
1316 | 0 | struct lws_context *context = vh->context; |
1317 | 0 | int n; |
1318 | |
|
1319 | 0 | lwsl_vhost_info(vh, "\n"); |
1320 | |
|
1321 | 0 | lws_context_lock(context, "vhost destroy 1"); /* ---------- context { */ |
1322 | |
|
1323 | 0 | if (vh->being_destroyed) |
1324 | 0 | goto out; |
1325 | | |
1326 | | /* |
1327 | | * let's lock all the pts, to enforce pt->vh order... pt is refcounted |
1328 | | * so it's OK if we acquire it later inside this |
1329 | | */ |
1330 | | |
1331 | 0 | for (n = 0; n < context->count_threads; n++) |
1332 | 0 | lws_pt_lock((&context->pt[n]), __func__); |
1333 | |
|
1334 | 0 | lws_vhost_lock(vh); /* -------------- vh { */ |
1335 | |
|
1336 | 0 | #if defined(LWS_WITH_TLS_SESSIONS) && defined(LWS_WITH_TLS) |
1337 | 0 | lws_tls_session_vh_destroy(vh); |
1338 | 0 | #endif |
1339 | |
|
1340 | 0 | vh->being_destroyed = 1; |
1341 | 0 | lws_dll2_add_tail(&vh->vh_being_destroyed_list, |
1342 | 0 | &context->owner_vh_being_destroyed); |
1343 | |
|
1344 | 0 | #if defined(LWS_WITH_NETWORK) && defined(LWS_WITH_SERVER) |
1345 | | /* |
1346 | | * PHASE 1: take down or reassign any listen wsi |
1347 | | * |
1348 | | * Are there other vhosts that are piggybacking on our listen sockets? |
1349 | | * If so we need to hand each listen socket off to one of the others |
1350 | | * so it will remain open. |
1351 | | * |
1352 | | * If not, close the listen socket now. |
1353 | | * |
1354 | | * Either way the listen socket response to the vhost close is |
1355 | | * immediately performed. |
1356 | | */ |
1357 | |
|
1358 | 0 | lws_start_foreach_dll_safe(struct lws_dll2 *, d, d1, |
1359 | 0 | lws_dll2_get_head(&vh->listen_wsi)) { |
1360 | 0 | struct lws *wsi = lws_container_of(d, struct lws, listen_list); |
1361 | | |
1362 | | /* |
1363 | | * For each of our listen sockets, check every other vhost to |
1364 | | * see if another vhost should be given our listen socket. |
1365 | | * |
1366 | | * ipv4 and ipv6 sockets will both match and be migrated. |
1367 | | */ |
1368 | |
|
1369 | 0 | lws_start_foreach_ll(struct lws_vhost *, v, |
1370 | 0 | context->vhost_list) { |
1371 | 0 | if (v != vh && !v->being_destroyed && |
1372 | 0 | lws_vhost_compare_listen(v, vh)) { |
1373 | | /* |
1374 | | * this can only be a listen wsi, which is |
1375 | | * restricted... it has no protocol or other |
1376 | | * bindings or states. So we can simply |
1377 | | * swap it to a vhost that has the same |
1378 | | * iface + port, but is not closing. |
1379 | | */ |
1380 | |
|
1381 | 0 | lwsl_vhost_notice(vh, "listen skt migrate -> %s", |
1382 | 0 | lws_vh_tag(v)); |
1383 | |
|
1384 | 0 | lws_dll2_remove(&wsi->listen_list); |
1385 | 0 | lws_dll2_add_tail(&wsi->listen_list, |
1386 | 0 | &v->listen_wsi); |
1387 | | |
1388 | | /* req cx + vh lock */ |
1389 | | /* |
1390 | | * If the vhost sees it's being destroyed and |
1391 | | * in the unbind the number of wsis bound to |
1392 | | * it falls to zero, it will destroy the |
1393 | | * vhost opportunistically before we can |
1394 | | * complete the transfer. Add a fake wsi |
1395 | | * bind temporarily to disallow this... |
1396 | | */ |
1397 | 0 | v->count_bound_wsi++; |
1398 | 0 | __lws_vhost_unbind_wsi(wsi); |
1399 | 0 | lws_vhost_bind_wsi(v, wsi); |
1400 | | /* |
1401 | | * ... remove the fake wsi bind |
1402 | | */ |
1403 | 0 | v->count_bound_wsi--; |
1404 | 0 | break; |
1405 | 0 | } |
1406 | 0 | } lws_end_foreach_ll(v, vhost_next); |
1407 | |
|
1408 | 0 | } lws_end_foreach_dll_safe(d, d1); |
1409 | | |
1410 | | /* |
1411 | | * If any listen wsi left we couldn't pass to other vhosts, close them |
1412 | | */ |
1413 | |
|
1414 | 0 | lws_start_foreach_dll_safe(struct lws_dll2 *, d, d1, |
1415 | 0 | lws_dll2_get_head(&vh->listen_wsi)) { |
1416 | 0 | struct lws *wsi = lws_container_of(d, struct lws, listen_list); |
1417 | |
|
1418 | 0 | lws_dll2_remove(&wsi->listen_list); |
1419 | 0 | lws_wsi_close(wsi, LWS_TO_KILL_ASYNC); |
1420 | |
|
1421 | 0 | } lws_end_foreach_dll_safe(d, d1); |
1422 | |
|
1423 | 0 | #endif |
1424 | | #if defined(LWS_WITH_TLS_JIT_TRUST) |
1425 | | lws_sul_cancel(&vh->sul_unref); |
1426 | | #endif |
1427 | |
|
1428 | 0 | lws_vhost_unlock(vh); /* } vh -------------- */ |
1429 | |
|
1430 | 0 | for (n = 0; n < context->count_threads; n++) |
1431 | 0 | lws_pt_unlock((&context->pt[n])); |
1432 | |
|
1433 | 0 | out: |
1434 | 0 | lws_context_unlock(context); /* --------------------------- context { */ |
1435 | 0 | } |
1436 | | |
1437 | | #if defined(LWS_WITH_ABSTRACT) |
1438 | | static int |
1439 | | destroy_ais(struct lws_dll2 *d, void *user) |
1440 | | { |
1441 | | lws_abs_t *ai = lws_container_of(d, lws_abs_t, abstract_instances); |
1442 | | |
1443 | | lws_abs_destroy_instance(&ai); |
1444 | | |
1445 | | return 0; |
1446 | | } |
1447 | | #endif |
1448 | | |
1449 | | /* |
1450 | | * Either start close or destroy any wsi on the vhost that belong to this pt, |
1451 | | * if SMP mark the vh that we have done it for |
1452 | | * |
1453 | | * Must not have lock on vh |
1454 | | */ |
1455 | | |
1456 | | void |
1457 | | __lws_vhost_destroy2(struct lws_vhost *vh) |
1458 | 0 | { |
1459 | 0 | const struct lws_protocols *protocol = NULL; |
1460 | 0 | struct lws_context *context = vh->context; |
1461 | 0 | struct lws wsi; |
1462 | 0 | int n; |
1463 | |
|
1464 | 0 | vh->being_destroyed = 0; |
1465 | | |
1466 | | // lwsl_info("%s: %s\n", __func__, vh->name); |
1467 | | |
1468 | | /* |
1469 | | * remove ourselves from the defer binding list |
1470 | | */ |
1471 | 0 | lws_start_foreach_llp(struct lws_vhost **, pv, |
1472 | 0 | vh->context->no_listener_vhost_list) { |
1473 | 0 | if (*pv == vh) { |
1474 | 0 | lwsl_debug("deferred iface: removing vh %s\n", |
1475 | 0 | (*pv)->name); |
1476 | 0 | *pv = vh->no_listener_vhost_list; |
1477 | 0 | vh->no_listener_vhost_list = NULL; |
1478 | 0 | break; |
1479 | 0 | } |
1480 | 0 | } lws_end_foreach_llp(pv, no_listener_vhost_list); |
1481 | | |
1482 | | /* |
1483 | | * let the protocols destroy the per-vhost protocol objects |
1484 | | */ |
1485 | |
|
1486 | 0 | memset(&wsi, 0, sizeof(wsi)); |
1487 | 0 | wsi.a.context = vh->context; |
1488 | 0 | wsi.a.vhost = vh; /* not a real bound wsi */ |
1489 | 0 | protocol = vh->protocols; |
1490 | 0 | if (protocol && vh->created_vhost_protocols) { |
1491 | 0 | n = 0; |
1492 | 0 | while (n < vh->count_protocols) { |
1493 | 0 | wsi.a.protocol = protocol; |
1494 | |
|
1495 | 0 | if (protocol->callback && (vh->protocol_init & (1u << n))) { |
1496 | 0 | lwsl_vhost_debug(vh, "protocol %s destroy", protocol->name); |
1497 | 0 | protocol->callback(&wsi, LWS_CALLBACK_PROTOCOL_DESTROY, |
1498 | 0 | NULL, NULL, 0); |
1499 | 0 | } |
1500 | 0 | protocol++; |
1501 | 0 | n++; |
1502 | 0 | } |
1503 | 0 | } |
1504 | | |
1505 | | /* |
1506 | | * remove vhost from context list of vhosts |
1507 | | */ |
1508 | |
|
1509 | 0 | lws_start_foreach_llp(struct lws_vhost **, pv, context->vhost_list) { |
1510 | 0 | if (*pv == vh) { |
1511 | 0 | *pv = vh->vhost_next; |
1512 | 0 | break; |
1513 | 0 | } |
1514 | 0 | } lws_end_foreach_llp(pv, vhost_next); |
1515 | | |
1516 | | /* add ourselves to the pending destruction list */ |
1517 | |
|
1518 | 0 | if (vh->context->vhost_pending_destruction_list != vh) { |
1519 | 0 | vh->vhost_next = vh->context->vhost_pending_destruction_list; |
1520 | 0 | vh->context->vhost_pending_destruction_list = vh; |
1521 | 0 | } |
1522 | | |
1523 | | //lwsl_debug("%s: do dfl '%s'\n", __func__, vh->name); |
1524 | | |
1525 | | /* remove ourselves from the pending destruction list */ |
1526 | |
|
1527 | 0 | lws_start_foreach_llp(struct lws_vhost **, pv, |
1528 | 0 | context->vhost_pending_destruction_list) { |
1529 | 0 | if ((*pv) == vh) { |
1530 | 0 | *pv = (*pv)->vhost_next; |
1531 | 0 | break; |
1532 | 0 | } |
1533 | 0 | } lws_end_foreach_llp(pv, vhost_next); |
1534 | | |
1535 | | /* |
1536 | | * Free all the allocations associated with the vhost |
1537 | | */ |
1538 | |
|
1539 | 0 | protocol = vh->protocols; |
1540 | 0 | if (protocol) { |
1541 | 0 | n = 0; |
1542 | 0 | while (n < vh->count_protocols) { |
1543 | 0 | if (vh->protocol_vh_privs && |
1544 | 0 | vh->protocol_vh_privs[n]) { |
1545 | 0 | lws_free(vh->protocol_vh_privs[n]); |
1546 | 0 | vh->protocol_vh_privs[n] = NULL; |
1547 | 0 | } |
1548 | 0 | protocol++; |
1549 | 0 | n++; |
1550 | 0 | } |
1551 | 0 | } |
1552 | 0 | if (vh->protocol_vh_privs) |
1553 | 0 | lws_free(vh->protocol_vh_privs); |
1554 | 0 | lws_ssl_SSL_CTX_destroy(vh); |
1555 | 0 | lws_free(vh->same_vh_protocol_owner); |
1556 | |
|
1557 | 0 | if ( |
1558 | | #if defined(LWS_WITH_PLUGINS) |
1559 | | context->plugin_list || |
1560 | | #endif |
1561 | 0 | (context->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS) || |
1562 | 0 | vh->allocated_vhost_protocols) |
1563 | 0 | lws_free((void *)vh->protocols); |
1564 | 0 | #if defined(LWS_WITH_NETWORK) |
1565 | 0 | LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar) |
1566 | 0 | if (lws_rops_fidx(ar, LWS_ROPS_destroy_vhost)) |
1567 | 0 | lws_rops_func_fidx(ar, LWS_ROPS_destroy_vhost). |
1568 | 0 | destroy_vhost(vh); |
1569 | 0 | LWS_FOR_EVERY_AVAILABLE_ROLE_END; |
1570 | 0 | #endif |
1571 | |
|
1572 | | #ifdef LWS_WITH_ACCESS_LOG |
1573 | | if (vh->log_fd != (int)LWS_INVALID_FILE) |
1574 | | close(vh->log_fd); |
1575 | | #endif |
1576 | |
|
1577 | 0 | #if defined (LWS_WITH_TLS) |
1578 | 0 | lws_free_set_NULL(vh->tls.alloc_cert_path); |
1579 | 0 | #endif |
1580 | |
|
1581 | | #if LWS_MAX_SMP > 1 |
1582 | | lws_mutex_refcount_destroy(&vh->mr); |
1583 | | #endif |
1584 | |
|
1585 | 0 | #if defined(LWS_WITH_UNIX_SOCK) |
1586 | 0 | if (LWS_UNIX_SOCK_ENABLED(vh)) { |
1587 | 0 | n = unlink(vh->iface); |
1588 | 0 | if (n) |
1589 | 0 | lwsl_vhost_info(vh, "Closing unix socket %s: errno %d\n", |
1590 | 0 | vh->iface, errno); |
1591 | 0 | } |
1592 | 0 | #endif |
1593 | | /* |
1594 | | * although async event callbacks may still come for wsi handles with |
1595 | | * pending close in the case of asycn event library like libuv, |
1596 | | * they do not refer to the vhost. So it's safe to free. |
1597 | | */ |
1598 | |
|
1599 | 0 | if (vh->finalize) |
1600 | 0 | vh->finalize(vh, vh->finalize_arg); |
1601 | |
|
1602 | | #if defined(LWS_WITH_ABSTRACT) |
1603 | | /* |
1604 | | * abstract instances |
1605 | | */ |
1606 | | |
1607 | | lws_dll2_foreach_safe(&vh->abstract_instances_owner, NULL, destroy_ais); |
1608 | | #endif |
1609 | |
|
1610 | | #if defined(LWS_WITH_SERVER) && defined(LWS_WITH_SYS_METRICS) |
1611 | | lws_metric_destroy(&vh->mt_traffic_rx, 0); |
1612 | | lws_metric_destroy(&vh->mt_traffic_tx, 0); |
1613 | | #endif |
1614 | |
|
1615 | 0 | lws_dll2_remove(&vh->vh_being_destroyed_list); |
1616 | |
|
1617 | | #if defined(LWS_WITH_SYS_FAULT_INJECTION) |
1618 | | lws_fi_destroy(&vh->fic); |
1619 | | #endif |
1620 | | #if defined(LWS_WITH_TLS_JIT_TRUST) |
1621 | | lws_sul_cancel(&vh->sul_unref); |
1622 | | #endif |
1623 | |
|
1624 | 0 | __lws_lc_untag(vh->context, &vh->lc); |
1625 | |
|
1626 | 0 | memset(vh, 0, sizeof(*vh)); |
1627 | 0 | lws_free(vh); |
1628 | 0 | } |
1629 | | |
1630 | | /* |
1631 | | * Starts the vhost destroy process |
1632 | | * |
1633 | | * Vhosts are not simple to deal with because they are an abstraction that |
1634 | | * crosses SMP thread boundaries, a wsi on any pt can bind to any vhost. If we |
1635 | | * want another pt to do something to its wsis safely, we have to asynchronously |
1636 | | * ask it to do it. |
1637 | | * |
1638 | | * In addition, with event libs, closing any handles (which are bound to vhosts |
1639 | | * in their wsi) can happens asynchronously, so we can't just linearly do some |
1640 | | * cleanup flow and free it in one step. |
1641 | | * |
1642 | | * The vhost destroy is cut into two pieces: |
1643 | | * |
1644 | | * 1) dispose of the listen socket, either by passing it on to another vhost |
1645 | | * that was already sharing it, or just closing it. |
1646 | | * |
1647 | | * If any wsi bound to the vhost, mark the vhost as in the process of being |
1648 | | * destroyed, triggering each pt to close all wsi bound to the vhost next |
1649 | | * time around the event loop. Call lws_cancel_service() so all the pts wake |
1650 | | * to deal with this without long poll waits making delays. |
1651 | | * |
1652 | | * 2) When the number of wsis bound to the vhost reaches zero, do the final |
1653 | | * vhost destroy flow, this can be triggered from any pt. |
1654 | | */ |
1655 | | |
1656 | | void |
1657 | | lws_vhost_destroy(struct lws_vhost *vh) |
1658 | 0 | { |
1659 | 0 | struct lws_context *context = vh->context; |
1660 | |
|
1661 | 0 | lws_context_lock(context, __func__); /* ------ context { */ |
1662 | | |
1663 | | /* dispose of the listen socket one way or another */ |
1664 | 0 | lws_vhost_destroy1(vh); |
1665 | | |
1666 | | /* start async closure of all wsi on this pt thread attached to vh */ |
1667 | 0 | __lws_vhost_destroy_pt_wsi_dieback_start(vh); |
1668 | |
|
1669 | 0 | lwsl_vhost_info(vh, "count_bound_wsi %d", vh->count_bound_wsi); |
1670 | | |
1671 | | /* if there are none, finalize now since no further chance */ |
1672 | 0 | if (!vh->count_bound_wsi) { |
1673 | 0 | __lws_vhost_destroy2(vh); |
1674 | |
|
1675 | 0 | goto out; |
1676 | 0 | } |
1677 | | |
1678 | | /* |
1679 | | * We have some wsi bound to this vhost, we have to wait for these to |
1680 | | * complete close and unbind before progressing the vhost removal. |
1681 | | * |
1682 | | * When the last bound wsi on this vh is destroyed we will auto-call |
1683 | | * __lws_vhost_destroy2() to finalize vh destruction |
1684 | | */ |
1685 | | |
1686 | | #if LWS_MAX_SMP > 1 |
1687 | | /* alert other pts they also need to do dieback flow for their wsi */ |
1688 | | lws_cancel_service(context); |
1689 | | #endif |
1690 | | |
1691 | 0 | out: |
1692 | 0 | lws_context_unlock(context); /* } context ------------------- */ |
1693 | 0 | } |
1694 | | |
1695 | | |
1696 | | void * |
1697 | | lws_vhost_user(struct lws_vhost *vhost) |
1698 | 0 | { |
1699 | 0 | return vhost->user; |
1700 | 0 | } |
1701 | | |
1702 | | int |
1703 | | lws_get_vhost_listen_port(struct lws_vhost *vhost) |
1704 | 0 | { |
1705 | 0 | return vhost->listen_port; |
1706 | 0 | } |
1707 | | |
1708 | | #if defined(LWS_WITH_SERVER) |
1709 | | void |
1710 | | lws_context_deprecate(struct lws_context *cx, lws_reload_func cb) |
1711 | 0 | { |
1712 | 0 | struct lws_vhost *vh = cx->vhost_list; |
1713 | | |
1714 | | /* |
1715 | | * "deprecation" means disable the cx from accepting any new |
1716 | | * connections and free up listen sockets to be used by a replacement |
1717 | | * cx. |
1718 | | * |
1719 | | * Otherwise the deprecated cx remains operational, until its |
1720 | | * number of connected sockets falls to zero, when it is deleted. |
1721 | | * |
1722 | | * So, for each vhost, close his listen sockets |
1723 | | */ |
1724 | |
|
1725 | 0 | while (vh) { |
1726 | |
|
1727 | 0 | lws_start_foreach_dll_safe(struct lws_dll2 *, d, d1, |
1728 | 0 | lws_dll2_get_head(&vh->listen_wsi)) { |
1729 | 0 | struct lws *wsi = lws_container_of(d, struct lws, |
1730 | 0 | listen_list); |
1731 | |
|
1732 | 0 | wsi->socket_is_permanently_unusable = 1; |
1733 | 0 | lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS, |
1734 | 0 | __func__); |
1735 | 0 | cx->deprecation_pending_listen_close_count++; |
1736 | |
|
1737 | 0 | } lws_end_foreach_dll_safe(d, d1); |
1738 | |
|
1739 | 0 | vh = vh->vhost_next; |
1740 | 0 | } |
1741 | |
|
1742 | 0 | cx->deprecated = 1; |
1743 | 0 | cx->deprecation_cb = cb; |
1744 | 0 | } |
1745 | | #endif |
1746 | | |
1747 | | #if defined(LWS_WITH_NETWORK) |
1748 | | |
1749 | | struct lws_vhost * |
1750 | | lws_get_vhost_by_name(struct lws_context *context, const char *name) |
1751 | 0 | { |
1752 | 0 | lws_start_foreach_ll(struct lws_vhost *, v, |
1753 | 0 | context->vhost_list) { |
1754 | 0 | if (!v->being_destroyed && !strcmp(v->name, name)) |
1755 | 0 | return v; |
1756 | |
|
1757 | 0 | } lws_end_foreach_ll(v, vhost_next); |
1758 | |
|
1759 | 0 | return NULL; |
1760 | 0 | } |
1761 | | |
1762 | | |
1763 | | #if defined(LWS_WITH_CLIENT) |
1764 | | /* |
1765 | | * This is the logic checking to see if the new connection wsi should have a |
1766 | | * pipelining or muxing relationship with an existing "active connection" to |
1767 | | * the same endpoint under the same conditions. |
1768 | | * |
1769 | | * This was originally in the client code but since the list is held on the |
1770 | | * vhost (to ensure the same client tls ctx is involved) it's cleaner in vhost.c |
1771 | | * |
1772 | | * ACTIVE_CONNS_QUEUED: We're queued on an active connection, set *nwsi to that |
1773 | | * ACTIVE_CONNS_MUXED: We are joining an active mux conn *nwsi as a child |
1774 | | * ACTIVE_CONNS_SOLO: There's no existing conn to join either way |
1775 | | */ |
1776 | | |
1777 | | int |
1778 | | lws_vhost_active_conns(struct lws *wsi, struct lws **nwsi, const char *adsin) |
1779 | 0 | { |
1780 | 0 | #if defined(LWS_WITH_TLS) |
1781 | 0 | const char *my_alpn = lws_wsi_client_stash_item(wsi, CIS_ALPN, |
1782 | 0 | _WSI_TOKEN_CLIENT_ALPN); |
1783 | 0 | #endif |
1784 | 0 | #if defined(LWS_WITH_TLS) |
1785 | 0 | char newconn_cannot_use_h1 = 0; |
1786 | |
|
1787 | 0 | if ((wsi->tls.use_ssl & LCCSCF_USE_SSL) && |
1788 | 0 | my_alpn && !strstr(my_alpn, "http/1.1")) |
1789 | | /* |
1790 | | * new guy wants to use tls, he specifies the alpn and he does |
1791 | | * not list h1 as a choice ==> he can't bind to existing h1 |
1792 | | */ |
1793 | 0 | newconn_cannot_use_h1 = 1; |
1794 | 0 | #endif |
1795 | |
|
1796 | 0 | if (!lws_dll2_is_detached(&wsi->dll2_cli_txn_queue)) { |
1797 | 0 | struct lws *w = lws_container_of( |
1798 | 0 | wsi->dll2_cli_txn_queue.owner, struct lws, |
1799 | 0 | dll2_cli_txn_queue_owner); |
1800 | 0 | *nwsi = w; |
1801 | |
|
1802 | 0 | return ACTIVE_CONNS_QUEUED; |
1803 | 0 | } |
1804 | | |
1805 | 0 | #if defined(LWS_ROLE_H2) || defined(LWS_ROLE_MQTT) |
1806 | 0 | if (wsi->mux.parent_wsi) { |
1807 | | /* |
1808 | | * We already decided... |
1809 | | */ |
1810 | |
|
1811 | 0 | *nwsi = wsi->mux.parent_wsi; |
1812 | |
|
1813 | 0 | return ACTIVE_CONNS_MUXED; |
1814 | 0 | } |
1815 | 0 | #endif |
1816 | | |
1817 | 0 | lws_context_lock(wsi->a.context, __func__); /* -------------- cx { */ |
1818 | 0 | lws_vhost_lock(wsi->a.vhost); /* ----------------------------------- { */ |
1819 | |
|
1820 | 0 | lws_start_foreach_dll_safe(struct lws_dll2 *, d, d1, |
1821 | 0 | wsi->a.vhost->dll_cli_active_conns_owner.head) { |
1822 | 0 | struct lws *w = lws_container_of(d, struct lws, |
1823 | 0 | dll_cli_active_conns); |
1824 | |
|
1825 | 0 | lwsl_wsi_debug(wsi, "check %s %s %s %d %d", |
1826 | 0 | lws_wsi_tag(w), adsin, |
1827 | 0 | w->cli_hostname_copy ? w->cli_hostname_copy : |
1828 | 0 | "null", |
1829 | 0 | wsi->c_port, w->c_port); |
1830 | |
|
1831 | 0 | if (w != wsi && |
1832 | | /* |
1833 | | * "same internet protocol"... this is a bit tricky, |
1834 | | * since h2 start out as h1, and may stay at h1. |
1835 | | * |
1836 | | * But an idle h1 connection cannot be used by a connection |
1837 | | * request that doesn't have http/1.1 in its alpn list... |
1838 | | */ |
1839 | 0 | (w->role_ops == wsi->role_ops || |
1840 | 0 | (lwsi_role_http(w) && lwsi_role_http(wsi))) && |
1841 | | /* ... same role, or at least both some kind of http */ |
1842 | 0 | w->cli_hostname_copy && !strcmp(adsin, w->cli_hostname_copy) && |
1843 | | /* same endpoint hostname */ |
1844 | 0 | #if defined(LWS_WITH_TLS) |
1845 | 0 | !(newconn_cannot_use_h1 && w->role_ops == &role_ops_h1) && |
1846 | | /* if we can't use h1, old guy must not be h1 */ |
1847 | 0 | (wsi->tls.use_ssl & LCCSCF_USE_SSL) == |
1848 | 0 | (w->tls.use_ssl & LCCSCF_USE_SSL) && |
1849 | | /* must both agree on tls use or not */ |
1850 | 0 | #endif |
1851 | 0 | wsi->c_port == w->c_port) { |
1852 | | /* same endpoint port */ |
1853 | | |
1854 | | /* |
1855 | | * There's already an active connection. |
1856 | | * |
1857 | | * The server may have told the existing active |
1858 | | * connection that it doesn't support pipelining... |
1859 | | */ |
1860 | 0 | if (w->keepalive_rejected) { |
1861 | 0 | lwsl_wsi_notice(w, "defeating pipelining"); |
1862 | 0 | goto solo; |
1863 | 0 | } |
1864 | | |
1865 | 0 | #if defined(LWS_WITH_HTTP2) |
1866 | | /* |
1867 | | * h2: if in usable state already: just use it without |
1868 | | * going through the queue |
1869 | | */ |
1870 | 0 | if (w->client_h2_alpn && w->client_mux_migrated && |
1871 | 0 | (lwsi_state(w) == LRS_H2_WAITING_TO_SEND_HEADERS || |
1872 | 0 | lwsi_state(w) == LRS_ESTABLISHED || |
1873 | 0 | lwsi_state(w) == LRS_IDLING)) { |
1874 | |
|
1875 | 0 | lwsl_wsi_notice(w, "just join h2 directly 0x%x", |
1876 | 0 | lwsi_state(w)); |
1877 | |
|
1878 | 0 | if (lwsi_state(w) == LRS_IDLING) |
1879 | 0 | _lws_generic_transaction_completed_active_conn(&w, 0); |
1880 | | |
1881 | | //lwsi_set_state(w, LRS_H1C_ISSUE_HANDSHAKE2); |
1882 | |
|
1883 | 0 | wsi->client_h2_alpn = 1; |
1884 | 0 | lws_wsi_h2_adopt(w, wsi); |
1885 | 0 | lws_vhost_unlock(wsi->a.vhost); /* } ---------- */ |
1886 | 0 | lws_context_unlock(wsi->a.context); /* -------------- cx { */ |
1887 | |
|
1888 | 0 | *nwsi = w; |
1889 | |
|
1890 | 0 | return ACTIVE_CONNS_MUXED; |
1891 | 0 | } |
1892 | 0 | #endif |
1893 | | |
1894 | | #if defined(LWS_ROLE_MQTT) |
1895 | | /* |
1896 | | * MQTT: if in usable state already: just use it without |
1897 | | * going through the queue |
1898 | | */ |
1899 | | |
1900 | | if (lwsi_role_mqtt(wsi) && w->client_mux_migrated && |
1901 | | lwsi_state(w) == LRS_ESTABLISHED) { |
1902 | | |
1903 | | if (lws_wsi_mqtt_adopt(w, wsi)) { |
1904 | | lwsl_wsi_notice(w, "join mqtt directly"); |
1905 | | lws_dll2_remove(&wsi->dll2_cli_txn_queue); |
1906 | | wsi->client_mux_substream = 1; |
1907 | | |
1908 | | lws_vhost_unlock(wsi->a.vhost); /* } ---------- */ |
1909 | | lws_context_unlock(wsi->a.context); /* -------------- cx { */ |
1910 | | |
1911 | | return ACTIVE_CONNS_MUXED; |
1912 | | } |
1913 | | } |
1914 | | #endif |
1915 | | |
1916 | | /* |
1917 | | * If the connection is viable but not yet in a usable |
1918 | | * state, let's attach ourselves to it and wait for it |
1919 | | * to get there or fail. |
1920 | | */ |
1921 | | |
1922 | 0 | lwsl_wsi_info(wsi, "apply txn queue %s, state 0x%lx", |
1923 | 0 | lws_wsi_tag(w), |
1924 | 0 | (unsigned long)w->wsistate); |
1925 | | /* |
1926 | | * ...let's add ourselves to his transaction queue... |
1927 | | * we are adding ourselves at the TAIL |
1928 | | */ |
1929 | 0 | lws_dll2_add_tail(&wsi->dll2_cli_txn_queue, |
1930 | 0 | &w->dll2_cli_txn_queue_owner); |
1931 | |
|
1932 | 0 | if (lwsi_state(w) == LRS_IDLING) |
1933 | 0 | _lws_generic_transaction_completed_active_conn(&w, 0); |
1934 | | |
1935 | | /* |
1936 | | * For eg, h1 next we'd pipeline our headers out on him, |
1937 | | * and wait for our turn at client transaction_complete |
1938 | | * to take over parsing the rx. |
1939 | | */ |
1940 | 0 | lws_vhost_unlock(wsi->a.vhost); /* } ---------- */ |
1941 | 0 | lws_context_unlock(wsi->a.context); /* -------------- cx { */ |
1942 | |
|
1943 | 0 | *nwsi = w; |
1944 | |
|
1945 | 0 | return ACTIVE_CONNS_QUEUED; |
1946 | 0 | } |
1947 | |
|
1948 | 0 | } lws_end_foreach_dll_safe(d, d1); |
1949 | |
|
1950 | 0 | solo: |
1951 | 0 | lws_vhost_unlock(wsi->a.vhost); /* } ---------------------------------- */ |
1952 | 0 | lws_context_unlock(wsi->a.context); /* -------------- cx { */ |
1953 | | |
1954 | | /* there is nobody already connected in the same way */ |
1955 | |
|
1956 | 0 | return ACTIVE_CONNS_SOLO; |
1957 | 0 | } |
1958 | | #endif |
1959 | | #endif |
1960 | | |
1961 | | const char * |
1962 | | lws_vh_tag(struct lws_vhost *vh) |
1963 | 0 | { |
1964 | 0 | return lws_lc_tag(&vh->lc); |
1965 | 0 | } |
1966 | | |
1967 | | struct lws_log_cx * |
1968 | | lwsl_vhost_get_cx(struct lws_vhost *vh) |
1969 | 0 | { |
1970 | 0 | if (!vh) |
1971 | 0 | return NULL; |
1972 | | |
1973 | 0 | return vh->lc.log_cx; |
1974 | 0 | } |
1975 | | |
1976 | | void |
1977 | | lws_log_prepend_vhost(struct lws_log_cx *cx, void *obj, char **p, char *e) |
1978 | 0 | { |
1979 | 0 | struct lws_vhost *vh = (struct lws_vhost *)obj; |
1980 | |
|
1981 | 0 | *p += lws_snprintf(*p, lws_ptr_diff_size_t(e, (*p)), "%s: ", |
1982 | 0 | lws_vh_tag(vh)); |
1983 | 0 | } |