Coverage Report

Created: 2023-03-26 07:20

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