vhost.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  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. #include "private-lib-core.h"
  25. const struct lws_role_ops *available_roles[] = {
  26. #if defined(LWS_ROLE_H2)
  27. &role_ops_h2,
  28. #endif
  29. #if defined(LWS_ROLE_H1)
  30. &role_ops_h1,
  31. #endif
  32. #if defined(LWS_ROLE_WS)
  33. &role_ops_ws,
  34. #endif
  35. #if defined(LWS_ROLE_DBUS)
  36. &role_ops_dbus,
  37. #endif
  38. #if defined(LWS_ROLE_RAW_PROXY)
  39. &role_ops_raw_proxy,
  40. #endif
  41. #if defined(LWS_ROLE_MQTT) && defined(LWS_WITH_CLIENT)
  42. &role_ops_mqtt,
  43. #endif
  44. NULL
  45. };
  46. #if defined(LWS_WITH_ABSTRACT)
  47. const struct lws_protocols *available_abstract_protocols[] = {
  48. #if defined(LWS_ROLE_RAW)
  49. &protocol_abs_client_raw_skt,
  50. #endif
  51. NULL
  52. };
  53. #endif
  54. #if defined(LWS_WITH_SECURE_STREAMS)
  55. const struct lws_protocols *available_secstream_protocols[] = {
  56. #if defined(LWS_ROLE_H1)
  57. &protocol_secstream_h1,
  58. #endif
  59. #if defined(LWS_ROLE_H2)
  60. &protocol_secstream_h2,
  61. #endif
  62. #if defined(LWS_ROLE_WS)
  63. &protocol_secstream_ws,
  64. #endif
  65. #if defined(LWS_ROLE_MQTT)
  66. &protocol_secstream_mqtt,
  67. #endif
  68. &protocol_secstream_raw,
  69. NULL
  70. };
  71. #endif
  72. static const char * const mount_protocols[] = {
  73. "http://",
  74. "https://",
  75. "file://",
  76. "cgi://",
  77. ">http://",
  78. ">https://",
  79. "callback://"
  80. };
  81. const struct lws_role_ops *
  82. lws_role_by_name(const char *name)
  83. {
  84. LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar)
  85. if (!strcmp(ar->name, name))
  86. return ar;
  87. LWS_FOR_EVERY_AVAILABLE_ROLE_END;
  88. if (!strcmp(name, role_ops_raw_skt.name))
  89. return &role_ops_raw_skt;
  90. #if defined(LWS_ROLE_RAW_FILE)
  91. if (!strcmp(name, role_ops_raw_file.name))
  92. return &role_ops_raw_file;
  93. #endif
  94. return NULL;
  95. }
  96. int
  97. lws_role_call_alpn_negotiated(struct lws *wsi, const char *alpn)
  98. {
  99. #if defined(LWS_WITH_TLS)
  100. if (!alpn)
  101. return 0;
  102. #if !defined(LWS_ESP_PLATFORM)
  103. lwsl_info("%s: '%s'\n", __func__, alpn);
  104. #endif
  105. LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar)
  106. if (ar->alpn && !strcmp(ar->alpn, alpn) && ar->alpn_negotiated)
  107. return ar->alpn_negotiated(wsi, alpn);
  108. LWS_FOR_EVERY_AVAILABLE_ROLE_END;
  109. #endif
  110. return 0;
  111. }
  112. int
  113. lws_role_call_adoption_bind(struct lws *wsi, int type, const char *prot)
  114. {
  115. int n;
  116. /*
  117. * if the vhost is told to bind accepted sockets to a given role,
  118. * then look it up by name and try to bind to the specific role.
  119. */
  120. if (lws_check_opt(wsi->a.vhost->options,
  121. LWS_SERVER_OPTION_ADOPT_APPLY_LISTEN_ACCEPT_CONFIG) &&
  122. wsi->a.vhost->listen_accept_role) {
  123. const struct lws_role_ops *role =
  124. lws_role_by_name(wsi->a.vhost->listen_accept_role);
  125. if (!prot)
  126. prot = wsi->a.vhost->listen_accept_protocol;
  127. if (!role)
  128. lwsl_err("%s: can't find role '%s'\n", __func__,
  129. wsi->a.vhost->listen_accept_role);
  130. if (role && role->adoption_bind) {
  131. n = role->adoption_bind(wsi, type, prot);
  132. if (n < 0)
  133. return -1;
  134. if (n) /* did the bind */
  135. return 0;
  136. }
  137. if (type & _LWS_ADOPT_FINISH) {
  138. lwsl_debug("%s: leaving bound to role %s\n", __func__,
  139. wsi->role_ops->name);
  140. return 0;
  141. }
  142. lwsl_warn("%s: adoption bind to role '%s', "
  143. "protocol '%s', type 0x%x, failed\n", __func__,
  144. wsi->a.vhost->listen_accept_role, prot, type);
  145. }
  146. /*
  147. * Otherwise ask each of the roles in order of preference if they
  148. * want to bind to this accepted socket
  149. */
  150. LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar)
  151. if (ar->adoption_bind && ar->adoption_bind(wsi, type, prot))
  152. return 0;
  153. LWS_FOR_EVERY_AVAILABLE_ROLE_END;
  154. /* fall back to raw socket role if, eg, h1 not configured */
  155. if (role_ops_raw_skt.adoption_bind &&
  156. role_ops_raw_skt.adoption_bind(wsi, type, prot))
  157. return 0;
  158. #if defined(LWS_ROLE_RAW_FILE)
  159. /* fall back to raw file role if, eg, h1 not configured */
  160. if (role_ops_raw_file.adoption_bind &&
  161. role_ops_raw_file.adoption_bind(wsi, type, prot))
  162. return 0;
  163. #endif
  164. return 1;
  165. }
  166. #if defined(LWS_WITH_CLIENT)
  167. int
  168. lws_role_call_client_bind(struct lws *wsi,
  169. const struct lws_client_connect_info *i)
  170. {
  171. LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar)
  172. if (ar->client_bind) {
  173. int m = ar->client_bind(wsi, i);
  174. if (m < 0)
  175. return m;
  176. if (m)
  177. return 0;
  178. }
  179. LWS_FOR_EVERY_AVAILABLE_ROLE_END;
  180. /* fall back to raw socket role if, eg, h1 not configured */
  181. if (role_ops_raw_skt.client_bind &&
  182. role_ops_raw_skt.client_bind(wsi, i))
  183. return 0;
  184. return 1;
  185. }
  186. #endif
  187. void *
  188. lws_protocol_vh_priv_zalloc(struct lws_vhost *vhost,
  189. const struct lws_protocols *prot, int size)
  190. {
  191. int n = 0;
  192. /* allocate the vh priv array only on demand */
  193. if (!vhost->protocol_vh_privs) {
  194. vhost->protocol_vh_privs = (void **)lws_zalloc(
  195. vhost->count_protocols * sizeof(void *),
  196. "protocol_vh_privs");
  197. if (!vhost->protocol_vh_privs)
  198. return NULL;
  199. }
  200. while (n < vhost->count_protocols && &vhost->protocols[n] != prot)
  201. n++;
  202. if (n == vhost->count_protocols) {
  203. n = 0;
  204. while (n < vhost->count_protocols &&
  205. strcmp(vhost->protocols[n].name, prot->name))
  206. n++;
  207. if (n == vhost->count_protocols)
  208. return NULL;
  209. }
  210. vhost->protocol_vh_privs[n] = lws_zalloc(size, "vh priv");
  211. return vhost->protocol_vh_privs[n];
  212. }
  213. void *
  214. lws_protocol_vh_priv_get(struct lws_vhost *vhost,
  215. const struct lws_protocols *prot)
  216. {
  217. int n = 0;
  218. if (!vhost || !vhost->protocol_vh_privs || !prot)
  219. return NULL;
  220. while (n < vhost->count_protocols && &vhost->protocols[n] != prot)
  221. n++;
  222. if (n == vhost->count_protocols) {
  223. n = 0;
  224. while (n < vhost->count_protocols &&
  225. strcmp(vhost->protocols[n].name, prot->name))
  226. n++;
  227. if (n == vhost->count_protocols) {
  228. lwsl_err("%s: unknown protocol %p\n", __func__, prot);
  229. return NULL;
  230. }
  231. }
  232. return vhost->protocol_vh_privs[n];
  233. }
  234. const struct lws_protocol_vhost_options *
  235. lws_vhost_protocol_options(struct lws_vhost *vh, const char *name)
  236. {
  237. const struct lws_protocol_vhost_options *pvo = vh->pvo;
  238. if (!name)
  239. return NULL;
  240. while (pvo) {
  241. if (!strcmp(pvo->name, name))
  242. return pvo;
  243. pvo = pvo->next;
  244. }
  245. return NULL;
  246. }
  247. int
  248. lws_protocol_init_vhost(struct lws_vhost *vh, int *any)
  249. {
  250. const struct lws_protocol_vhost_options *pvo, *pvo1;
  251. lws_fakewsi_def_plwsa(&vh->context->pt[0]);
  252. int n;
  253. lws_fakewsi_prep_plwsa_ctx(vh->context);
  254. plwsa->vhost = vh;
  255. /* initialize supported protocols on this vhost */
  256. for (n = 0; n < vh->count_protocols; n++) {
  257. plwsa->protocol = &vh->protocols[n];
  258. if (!vh->protocols[n].name)
  259. continue;
  260. pvo = lws_vhost_protocol_options(vh, vh->protocols[n].name);
  261. if (pvo) {
  262. /*
  263. * linked list of options specific to
  264. * vh + protocol
  265. */
  266. pvo1 = pvo;
  267. pvo = pvo1->options;
  268. while (pvo) {
  269. lwsl_debug(
  270. " vhost \"%s\", "
  271. "protocol \"%s\", "
  272. "option \"%s\"\n",
  273. vh->name,
  274. vh->protocols[n].name,
  275. pvo->name);
  276. if (!strcmp(pvo->name, "default")) {
  277. lwsl_info("Setting default "
  278. "protocol for vh %s to %s\n",
  279. vh->name,
  280. vh->protocols[n].name);
  281. vh->default_protocol_index = n;
  282. }
  283. if (!strcmp(pvo->name, "raw")) {
  284. lwsl_info("Setting raw "
  285. "protocol for vh %s to %s\n",
  286. vh->name,
  287. vh->protocols[n].name);
  288. vh->raw_protocol_index = n;
  289. }
  290. pvo = pvo->next;
  291. }
  292. pvo = pvo1->options;
  293. }
  294. #if defined(LWS_WITH_TLS)
  295. if (any)
  296. *any |= !!vh->tls.ssl_ctx;
  297. #endif
  298. /*
  299. * inform all the protocols that they are doing their
  300. * one-time initialization if they want to.
  301. *
  302. * NOTE the fakewsi is garbage, except the key pointers that are
  303. * prepared in case the protocol handler wants to touch them
  304. */
  305. if (vh->protocols[n].callback((struct lws *)plwsa,
  306. LWS_CALLBACK_PROTOCOL_INIT, NULL,
  307. (void *)pvo, 0)) {
  308. if (vh->protocol_vh_privs[n]) {
  309. lws_free(vh->protocol_vh_privs[n]);
  310. vh->protocol_vh_privs[n] = NULL;
  311. }
  312. lwsl_err("%s: protocol %s failed init\n",
  313. __func__, vh->protocols[n].name);
  314. return 1;
  315. }
  316. }
  317. vh->created_vhost_protocols = 1;
  318. return 0;
  319. }
  320. /*
  321. * inform every vhost that hasn't already done it, that
  322. * his protocols are initializing
  323. */
  324. int
  325. lws_protocol_init(struct lws_context *context)
  326. {
  327. struct lws_vhost *vh = context->vhost_list;
  328. int any = 0;
  329. if (context->doing_protocol_init)
  330. return 0;
  331. context->doing_protocol_init = 1;
  332. lwsl_info("%s\n", __func__);
  333. while (vh) {
  334. /* only do the protocol init once for a given vhost */
  335. if (vh->created_vhost_protocols ||
  336. (lws_check_opt(vh->options, LWS_SERVER_OPTION_SKIP_PROTOCOL_INIT)))
  337. goto next;
  338. if (lws_protocol_init_vhost(vh, &any))
  339. return 1;
  340. next:
  341. vh = vh->vhost_next;
  342. }
  343. context->doing_protocol_init = 0;
  344. if (!context->protocol_init_done && lws_finalize_startup(context))
  345. return 1;
  346. context->protocol_init_done = 1;
  347. #if defined(LWS_WITH_SERVER)
  348. if (any) {
  349. lws_tls_check_all_cert_lifetimes(context);
  350. }
  351. #endif
  352. return 0;
  353. }
  354. /* list of supported protocols and callbacks */
  355. static const struct lws_protocols protocols_dummy[] = {
  356. /* first protocol must always be HTTP handler */
  357. {
  358. "http-only", /* name */
  359. lws_callback_http_dummy, /* callback */
  360. 0, /* per_session_data_size */
  361. 0, /* rx_buffer_size */
  362. 0, /* id */
  363. NULL, /* user */
  364. 0 /* tx_packet_size */
  365. },
  366. /*
  367. * the other protocols are provided by lws plugins
  368. */
  369. { NULL, NULL, 0, 0, 0, NULL, 0} /* terminator */
  370. };
  371. #ifdef LWS_PLAT_OPTEE
  372. #undef LWS_HAVE_GETENV
  373. #endif
  374. struct lws_vhost *
  375. lws_create_vhost(struct lws_context *context,
  376. const struct lws_context_creation_info *info)
  377. {
  378. struct lws_vhost *vh, **vh1 = &context->vhost_list;
  379. const struct lws_http_mount *mounts;
  380. const struct lws_protocols *pcols = info->protocols;
  381. #ifdef LWS_WITH_PLUGINS
  382. struct lws_plugin *plugin = context->plugin_list;
  383. #endif
  384. struct lws_protocols *lwsp;
  385. int m, f = !info->pvo, fx = 0, abs_pcol_count = 0, sec_pcol_count = 0;
  386. char buf[96];
  387. #if defined(LWS_CLIENT_HTTP_PROXYING) && defined(LWS_WITH_CLIENT) \
  388. && defined(LWS_HAVE_GETENV)
  389. char *p;
  390. #endif
  391. #if defined(LWS_WITH_SYS_ASYNC_DNS)
  392. extern struct lws_protocols lws_async_dns_protocol;
  393. #endif
  394. int n;
  395. vh = lws_zalloc(sizeof(*vh)
  396. #if defined(LWS_WITH_EVENT_LIBS)
  397. + context->event_loop_ops->evlib_size_vh
  398. #endif
  399. , __func__);
  400. if (!vh)
  401. return NULL;
  402. #if defined(LWS_WITH_EVENT_LIBS)
  403. vh->evlib_vh = (void *)&vh[1];
  404. #endif
  405. #if LWS_MAX_SMP > 1
  406. pthread_mutex_init(&vh->lock, NULL);
  407. #endif
  408. if (!pcols && !info->pprotocols)
  409. pcols = &protocols_dummy[0];
  410. vh->context = context;
  411. if (!info->vhost_name)
  412. vh->name = "default";
  413. else
  414. vh->name = info->vhost_name;
  415. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  416. vh->http.error_document_404 = info->error_document_404;
  417. #endif
  418. if (lws_check_opt(info->options, LWS_SERVER_OPTION_ONLY_RAW))
  419. lwsl_info("%s set to only support RAW\n", vh->name);
  420. vh->iface = info->iface;
  421. #if !defined(LWS_PLAT_FREERTOS) && !defined(OPTEE_TA) && !defined(WIN32)
  422. vh->bind_iface = info->bind_iface;
  423. #endif
  424. #if defined(LWS_WITH_CLIENT)
  425. if (info->connect_timeout_secs)
  426. vh->connect_timeout_secs = info->connect_timeout_secs;
  427. else
  428. vh->connect_timeout_secs = 20;
  429. #endif
  430. /* apply the context default lws_retry */
  431. if (info->retry_and_idle_policy)
  432. vh->retry_policy = info->retry_and_idle_policy;
  433. else
  434. vh->retry_policy = &context->default_retry;
  435. /*
  436. * let's figure out how many protocols the user is handing us, using the
  437. * old or new way depending on what he gave us
  438. */
  439. if (!pcols)
  440. for (vh->count_protocols = 0;
  441. info->pprotocols[vh->count_protocols];
  442. vh->count_protocols++)
  443. ;
  444. else
  445. for (vh->count_protocols = 0;
  446. pcols[vh->count_protocols].callback;
  447. vh->count_protocols++)
  448. ;
  449. vh->options = info->options;
  450. vh->pvo = info->pvo;
  451. vh->headers = info->headers;
  452. vh->user = info->user;
  453. vh->finalize = info->finalize;
  454. vh->finalize_arg = info->finalize_arg;
  455. vh->listen_accept_role = info->listen_accept_role;
  456. vh->listen_accept_protocol = info->listen_accept_protocol;
  457. vh->unix_socket_perms = info->unix_socket_perms;
  458. LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar)
  459. if (ar->init_vhost)
  460. if (ar->init_vhost(vh, info))
  461. return NULL;
  462. LWS_FOR_EVERY_AVAILABLE_ROLE_END;
  463. if (info->keepalive_timeout)
  464. vh->keepalive_timeout = info->keepalive_timeout;
  465. else
  466. vh->keepalive_timeout = 5;
  467. if (info->timeout_secs_ah_idle)
  468. vh->timeout_secs_ah_idle = info->timeout_secs_ah_idle;
  469. else
  470. vh->timeout_secs_ah_idle = 10;
  471. #if defined(LWS_WITH_TLS)
  472. vh->tls.alpn = info->alpn;
  473. vh->tls.ssl_info_event_mask = info->ssl_info_event_mask;
  474. if (info->ecdh_curve)
  475. lws_strncpy(vh->tls.ecdh_curve, info->ecdh_curve,
  476. sizeof(vh->tls.ecdh_curve));
  477. /* carefully allocate and take a copy of cert + key paths if present */
  478. n = 0;
  479. if (info->ssl_cert_filepath)
  480. n += (int)strlen(info->ssl_cert_filepath) + 1;
  481. if (info->ssl_private_key_filepath)
  482. n += (int)strlen(info->ssl_private_key_filepath) + 1;
  483. if (n) {
  484. vh->tls.key_path = vh->tls.alloc_cert_path =
  485. lws_malloc(n, "vh paths");
  486. if (info->ssl_cert_filepath) {
  487. n = (int)strlen(info->ssl_cert_filepath) + 1;
  488. memcpy(vh->tls.alloc_cert_path,
  489. info->ssl_cert_filepath, n);
  490. vh->tls.key_path += n;
  491. }
  492. if (info->ssl_private_key_filepath)
  493. memcpy(vh->tls.key_path, info->ssl_private_key_filepath,
  494. strlen(info->ssl_private_key_filepath) + 1);
  495. }
  496. #endif
  497. #if defined(LWS_WITH_HTTP_PROXY) && defined(LWS_ROLE_WS)
  498. fx = 1;
  499. #endif
  500. #if defined(LWS_WITH_ABSTRACT)
  501. abs_pcol_count = (int)LWS_ARRAY_SIZE(available_abstract_protocols) - 1;
  502. #endif
  503. #if defined(LWS_WITH_SECURE_STREAMS)
  504. sec_pcol_count = (int)LWS_ARRAY_SIZE(available_secstream_protocols) - 1;
  505. #endif
  506. /*
  507. * give the vhost a unified list of protocols including:
  508. *
  509. * - internal, async_dns if enabled (first vhost only)
  510. * - internal, abstracted ones
  511. * - the ones that came from plugins
  512. * - his user protocols
  513. */
  514. lwsp = lws_zalloc(sizeof(struct lws_protocols) *
  515. (vh->count_protocols +
  516. abs_pcol_count + sec_pcol_count +
  517. context->plugin_protocol_count +
  518. fx + 1),
  519. "vhost-specific plugin table");
  520. if (!lwsp) {
  521. lwsl_err("OOM\n");
  522. return NULL;
  523. }
  524. /*
  525. * 1: user protocols (from pprotocols or protocols)
  526. */
  527. m = vh->count_protocols;
  528. if (!pcols) {
  529. for (n = 0; n < m; n++)
  530. memcpy(&lwsp[n], info->pprotocols[n], sizeof(lwsp[0]));
  531. } else
  532. memcpy(lwsp, pcols, sizeof(struct lws_protocols) * m);
  533. /*
  534. * 2: abstract protocols
  535. */
  536. #if defined(LWS_WITH_ABSTRACT)
  537. for (n = 0; n < abs_pcol_count; n++) {
  538. memcpy(&lwsp[m++], available_abstract_protocols[n],
  539. sizeof(*lwsp));
  540. vh->count_protocols++;
  541. }
  542. #endif
  543. /*
  544. * 3: async dns protocol (first vhost only)
  545. */
  546. #if defined(LWS_WITH_SYS_ASYNC_DNS)
  547. if (!context->vhost_list) {
  548. memcpy(&lwsp[m++], &lws_async_dns_protocol,
  549. sizeof(struct lws_protocols));
  550. vh->count_protocols++;
  551. }
  552. #endif
  553. #if defined(LWS_WITH_SECURE_STREAMS)
  554. for (n = 0; n < sec_pcol_count; n++) {
  555. memcpy(&lwsp[m++], available_secstream_protocols[n],
  556. sizeof(*lwsp));
  557. vh->count_protocols++;
  558. }
  559. #endif
  560. /*
  561. * 3: For compatibility, all protocols enabled on vhost if only
  562. * the default vhost exists. Otherwise only vhosts who ask
  563. * for a protocol get it enabled.
  564. */
  565. if (context->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
  566. f = 0;
  567. (void)f;
  568. #ifdef LWS_WITH_PLUGINS
  569. if (plugin) {
  570. while (plugin) {
  571. const lws_plugin_protocol_t *plpr =
  572. (const lws_plugin_protocol_t *)plugin->hdr;
  573. for (n = 0; n < plpr->count_protocols; n++) {
  574. /*
  575. * for compatibility's sake, no pvo implies
  576. * allow all protocols
  577. */
  578. if (f || lws_vhost_protocol_options(vh,
  579. plpr->protocols[n].name)) {
  580. memcpy(&lwsp[m],
  581. &plpr->protocols[n],
  582. sizeof(struct lws_protocols));
  583. m++;
  584. vh->count_protocols++;
  585. }
  586. }
  587. plugin = plugin->list;
  588. }
  589. }
  590. #endif
  591. #if defined(LWS_WITH_HTTP_PROXY) && defined(LWS_ROLE_WS)
  592. memcpy(&lwsp[m++], &lws_ws_proxy, sizeof(*lwsp));
  593. vh->count_protocols++;
  594. #endif
  595. vh->protocols = lwsp;
  596. vh->allocated_vhost_protocols = 1;
  597. vh->same_vh_protocol_owner = (struct lws_dll2_owner *)
  598. lws_zalloc(sizeof(struct lws_dll2_owner) *
  599. vh->count_protocols, "same vh list");
  600. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  601. vh->http.mount_list = info->mounts;
  602. #endif
  603. #ifdef LWS_WITH_UNIX_SOCK
  604. if (LWS_UNIX_SOCK_ENABLED(vh)) {
  605. lwsl_info("Creating Vhost '%s' path \"%s\", %d protocols\n",
  606. vh->name, vh->iface, vh->count_protocols);
  607. } else
  608. #endif
  609. {
  610. switch(info->port) {
  611. case CONTEXT_PORT_NO_LISTEN:
  612. strcpy(buf, "(serving disabled)");
  613. break;
  614. case CONTEXT_PORT_NO_LISTEN_SERVER:
  615. strcpy(buf, "(no listener)");
  616. break;
  617. default:
  618. lws_snprintf(buf, sizeof(buf), "port %u", info->port);
  619. break;
  620. }
  621. lwsl_info("Creating Vhost '%s' %s, %d protocols, IPv6 %s\n",
  622. vh->name, buf, vh->count_protocols,
  623. LWS_IPV6_ENABLED(vh) ? "on" : "off");
  624. }
  625. mounts = info->mounts;
  626. while (mounts) {
  627. (void)mount_protocols[0];
  628. lwsl_info(" mounting %s%s to %s\n",
  629. mount_protocols[mounts->origin_protocol],
  630. mounts->origin, mounts->mountpoint);
  631. mounts = mounts->mount_next;
  632. }
  633. vh->listen_port = info->port;
  634. #if defined(LWS_WITH_SOCKS5)
  635. vh->socks_proxy_port = 0;
  636. vh->socks_proxy_address[0] = '\0';
  637. #endif
  638. #if defined(LWS_WITH_CLIENT) && defined(LWS_CLIENT_HTTP_PROXYING)
  639. /* either use proxy from info, or try get it from env var */
  640. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  641. vh->http.http_proxy_port = 0;
  642. vh->http.http_proxy_address[0] = '\0';
  643. /* http proxy */
  644. if (info->http_proxy_address) {
  645. /* override for backwards compatibility */
  646. if (info->http_proxy_port)
  647. vh->http.http_proxy_port = info->http_proxy_port;
  648. lws_set_proxy(vh, info->http_proxy_address);
  649. } else
  650. #endif
  651. {
  652. #ifdef LWS_HAVE_GETENV
  653. #if defined(__COVERITY__)
  654. p = NULL;
  655. #else
  656. p = getenv("http_proxy"); /* coverity[tainted_scalar] */
  657. if (p) {
  658. lws_strncpy(buf, p, sizeof(buf));
  659. lws_set_proxy(vh, buf);
  660. }
  661. #endif
  662. #endif
  663. }
  664. #endif
  665. #if defined(LWS_WITH_SOCKS5)
  666. lws_socks5c_ads_server(vh, info);
  667. #endif
  668. vh->ka_time = info->ka_time;
  669. vh->ka_interval = info->ka_interval;
  670. vh->ka_probes = info->ka_probes;
  671. if (vh->options & LWS_SERVER_OPTION_STS)
  672. lwsl_notice(" STS enabled\n");
  673. #ifdef LWS_WITH_ACCESS_LOG
  674. if (info->log_filepath) {
  675. vh->log_fd = lws_open(info->log_filepath,
  676. O_CREAT | O_APPEND | O_RDWR, 0600);
  677. if (vh->log_fd == (int)LWS_INVALID_FILE) {
  678. lwsl_err("unable to open log filepath %s\n",
  679. info->log_filepath);
  680. goto bail;
  681. }
  682. #ifndef WIN32
  683. if (context->uid != -1)
  684. if (chown(info->log_filepath, context->uid,
  685. context->gid) == -1)
  686. lwsl_err("unable to chown log file %s\n",
  687. info->log_filepath);
  688. #endif
  689. } else
  690. vh->log_fd = (int)LWS_INVALID_FILE;
  691. #endif
  692. if (lws_context_init_server_ssl(info, vh)) {
  693. lwsl_err("%s: lws_context_init_server_ssl failed\n", __func__);
  694. goto bail1;
  695. }
  696. if (lws_context_init_client_ssl(info, vh)) {
  697. lwsl_err("%s: lws_context_init_client_ssl failed\n", __func__);
  698. goto bail1;
  699. }
  700. #if defined(LWS_WITH_SERVER)
  701. lws_context_lock(context, "create_vhost");
  702. n = _lws_vhost_init_server(info, vh);
  703. lws_context_unlock(context);
  704. if (n < 0) {
  705. lwsl_err("init server failed\n");
  706. goto bail1;
  707. }
  708. #endif
  709. #if defined(LWS_WITH_SYS_ASYNC_DNS)
  710. n = !!context->vhost_list;
  711. #endif
  712. while (1) {
  713. if (!(*vh1)) {
  714. *vh1 = vh;
  715. break;
  716. }
  717. vh1 = &(*vh1)->vhost_next;
  718. };
  719. #if defined(LWS_WITH_SYS_ASYNC_DNS)
  720. if (!n && lws_async_dns_init(context))
  721. goto bail1;
  722. #endif
  723. /* for the case we are adding a vhost much later, after server init */
  724. if (context->protocol_init_done)
  725. if (lws_protocol_init(context)) {
  726. lwsl_err("%s: lws_protocol_init failed\n", __func__);
  727. goto bail1;
  728. }
  729. return vh;
  730. bail1:
  731. lws_vhost_destroy(vh);
  732. return NULL;
  733. #ifdef LWS_WITH_ACCESS_LOG
  734. bail:
  735. lws_free(vh);
  736. #endif
  737. return NULL;
  738. }
  739. int
  740. lws_init_vhost_client_ssl(const struct lws_context_creation_info *info,
  741. struct lws_vhost *vhost)
  742. {
  743. struct lws_context_creation_info i;
  744. memcpy(&i, info, sizeof(i));
  745. i.port = CONTEXT_PORT_NO_LISTEN;
  746. return lws_context_init_client_ssl(&i, vhost);
  747. }
  748. void
  749. lws_cancel_service_pt(struct lws *wsi)
  750. {
  751. lws_plat_pipe_signal(wsi);
  752. }
  753. void
  754. lws_cancel_service(struct lws_context *context)
  755. {
  756. struct lws_context_per_thread *pt = &context->pt[0];
  757. short m = context->count_threads;
  758. if (context->being_destroyed1)
  759. return;
  760. lwsl_debug("%s\n", __func__);
  761. while (m--) {
  762. if (pt->pipe_wsi)
  763. lws_plat_pipe_signal(pt->pipe_wsi);
  764. pt++;
  765. }
  766. }
  767. int
  768. lws_create_event_pipes(struct lws_context *context)
  769. {
  770. struct lws_context_per_thread *pt;
  771. size_t s = sizeof(struct lws);
  772. struct lws *wsi;
  773. int n;
  774. /*
  775. * Create the pt event pipes... these are unique in that they are
  776. * not bound to a vhost or protocol (both are NULL)
  777. */
  778. #if LWS_MAX_SMP > 1
  779. for (n = 0; n < context->count_threads; n++) {
  780. #else
  781. n = 0;
  782. {
  783. #endif
  784. pt = &context->pt[n];
  785. if (pt->pipe_wsi)
  786. return 0;
  787. #if defined(LWS_WITH_EVENT_LIBS)
  788. s += context->event_loop_ops->evlib_size_wsi;
  789. #endif
  790. wsi = lws_zalloc(s, "event pipe wsi");
  791. if (!wsi) {
  792. lwsl_err("%s: Out of mem\n", __func__);
  793. return 1;
  794. }
  795. #if defined(LWS_WITH_EVENT_LIBS)
  796. wsi->evlib_wsi = (uint8_t *)wsi + sizeof(*wsi);
  797. #endif
  798. wsi->a.context = context;
  799. lws_role_transition(wsi, 0, LRS_UNCONNECTED, &role_ops_pipe);
  800. wsi->a.protocol = NULL;
  801. wsi->tsi = n;
  802. wsi->a.vhost = NULL;
  803. wsi->event_pipe = 1;
  804. wsi->desc.sockfd = LWS_SOCK_INVALID;
  805. context->pt[n].pipe_wsi = wsi;
  806. context->count_wsi_allocated++;
  807. lws_pt_lock(pt, __func__); /* -------------- pt { */
  808. if (!lws_plat_pipe_create(wsi)) {
  809. /*
  810. * platform code returns 0 if it actually created pipes
  811. * and initialized pt->dummy_pipe_fds[]. If it used
  812. * some other mechanism outside of signaling in the
  813. * normal event loop, we skip treating the pipe as
  814. * related to dummy_pipe_fds[], adding it to the fds,
  815. * etc.
  816. */
  817. wsi->desc.sockfd = context->pt[n].dummy_pipe_fds[0];
  818. lwsl_debug("event pipe fd %d\n", wsi->desc.sockfd);
  819. if (context->event_loop_ops->sock_accept)
  820. if (context->event_loop_ops->sock_accept(wsi))
  821. goto bail;
  822. if (__insert_wsi_socket_into_fds(context, wsi))
  823. goto bail;
  824. }
  825. lws_pt_unlock(pt);
  826. }
  827. return 0;
  828. bail:
  829. lws_pt_unlock(pt);
  830. return 1;
  831. }
  832. void
  833. lws_destroy_event_pipe(struct lws *wsi)
  834. {
  835. lwsl_info("%s\n", __func__);
  836. if (lws_socket_is_valid(wsi->desc.sockfd))
  837. __remove_wsi_socket_from_fds(wsi);
  838. if (!wsi->a.context->event_loop_ops->destroy_wsi &&
  839. wsi->a.context->event_loop_ops->wsi_logical_close) {
  840. wsi->a.context->event_loop_ops->wsi_logical_close(wsi);
  841. lws_plat_pipe_close(wsi);
  842. return;
  843. }
  844. if (wsi->a.context->event_loop_ops->destroy_wsi)
  845. wsi->a.context->event_loop_ops->destroy_wsi(wsi);
  846. lws_plat_pipe_close(wsi);
  847. wsi->a.context->count_wsi_allocated--;
  848. lws_free(wsi);
  849. }
  850. void
  851. lws_vhost_destroy1(struct lws_vhost *vh)
  852. {
  853. struct lws_context *context = vh->context;
  854. lwsl_info("%s\n", __func__);
  855. lws_context_lock(context, "vhost destroy 1"); /* ---------- context { */
  856. if (vh->being_destroyed)
  857. goto out;
  858. lws_vhost_lock(vh); /* -------------- vh { */
  859. #if defined(LWS_WITH_NETWORK)
  860. /*
  861. * PHASE 1: take down or reassign any listen wsi
  862. *
  863. * Are there other vhosts that are piggybacking on our listen socket?
  864. * If so we need to hand the listen socket off to one of the others
  865. * so it will remain open.
  866. *
  867. * If not, leave it attached to the closing vhost, the vh being marked
  868. * being_destroyed will defeat any service and it will get closed in
  869. * later phases.
  870. */
  871. if (vh->lserv_wsi)
  872. lws_start_foreach_ll(struct lws_vhost *, v,
  873. context->vhost_list) {
  874. if (v != vh &&
  875. !v->being_destroyed &&
  876. v->listen_port == vh->listen_port &&
  877. ((!v->iface && !vh->iface) ||
  878. (v->iface && vh->iface &&
  879. !strcmp(v->iface, vh->iface)))) {
  880. /*
  881. * this can only be a listen wsi, which is
  882. * restricted... it has no protocol or other
  883. * bindings or states. So we can simply
  884. * swap it to a vhost that has the same
  885. * iface + port, but is not closing.
  886. */
  887. assert(v->lserv_wsi == NULL);
  888. v->lserv_wsi = vh->lserv_wsi;
  889. lwsl_notice("%s: listen skt from %s to %s\n",
  890. __func__, vh->name, v->name);
  891. if (v->lserv_wsi) {
  892. lws_vhost_unbind_wsi(vh->lserv_wsi);
  893. lws_vhost_bind_wsi(v, v->lserv_wsi);
  894. }
  895. break;
  896. }
  897. } lws_end_foreach_ll(v, vhost_next);
  898. #endif
  899. lws_vhost_unlock(vh); /* } vh -------------- */
  900. /*
  901. * lws_check_deferred_free() will notice there is a vhost that is
  902. * marked for destruction during the next 1s, for all tsi.
  903. *
  904. * It will start closing all wsi on this vhost. When the last wsi
  905. * is closed, it will trigger lws_vhost_destroy2()
  906. */
  907. out:
  908. lws_context_unlock(context); /* --------------------------- context { */
  909. }
  910. #if defined(LWS_WITH_ABSTRACT)
  911. static int
  912. destroy_ais(struct lws_dll2 *d, void *user)
  913. {
  914. lws_abs_t *ai = lws_container_of(d, lws_abs_t, abstract_instances);
  915. lws_abs_destroy_instance(&ai);
  916. return 0;
  917. }
  918. #endif
  919. void
  920. __lws_vhost_destroy2(struct lws_vhost *vh)
  921. {
  922. const struct lws_protocols *protocol = NULL;
  923. struct lws_context *context = vh->context;
  924. struct lws_deferred_free *df;
  925. struct lws wsi;
  926. int n;
  927. vh->being_destroyed = 0;
  928. #if defined(LWS_WITH_CLIENT)
  929. /*
  930. * destroy any wsi that are associated with us but have no socket
  931. * (and will otherwise be missed for destruction)
  932. */
  933. lws_start_foreach_dll_safe(struct lws_dll2 *, d, d1,
  934. vh->vh_awaiting_socket_owner.head) {
  935. struct lws *w =
  936. lws_container_of(d, struct lws, vh_awaiting_socket);
  937. lws_close_free_wsi(w, LWS_CLOSE_STATUS_NOSTATUS,
  938. "awaiting skt");
  939. } lws_end_foreach_dll_safe(d, d1);
  940. #endif
  941. #if defined(LWS_WITH_DEPRECATED_THINGS)
  942. /*
  943. * destroy any pending timed events
  944. */
  945. while (vh->timed_vh_protocol_list)
  946. __lws_timed_callback_remove(vh, vh->timed_vh_protocol_list);
  947. #endif
  948. /*
  949. * let the protocols destroy the per-vhost protocol objects
  950. */
  951. memset(&wsi, 0, sizeof(wsi));
  952. wsi.a.context = vh->context;
  953. wsi.a.vhost = vh; /* not a real bound wsi */
  954. protocol = vh->protocols;
  955. if (protocol && vh->created_vhost_protocols) {
  956. n = 0;
  957. while (n < vh->count_protocols) {
  958. wsi.a.protocol = protocol;
  959. if (protocol->callback)
  960. protocol->callback(&wsi, LWS_CALLBACK_PROTOCOL_DESTROY,
  961. NULL, NULL, 0);
  962. protocol++;
  963. n++;
  964. }
  965. }
  966. /*
  967. * remove vhost from context list of vhosts
  968. */
  969. lws_start_foreach_llp(struct lws_vhost **, pv, context->vhost_list) {
  970. if (*pv == vh) {
  971. *pv = vh->vhost_next;
  972. break;
  973. }
  974. } lws_end_foreach_llp(pv, vhost_next);
  975. /* add ourselves to the pending destruction list */
  976. if (vh->context->vhost_pending_destruction_list != vh) {
  977. vh->vhost_next = vh->context->vhost_pending_destruction_list;
  978. vh->context->vhost_pending_destruction_list = vh;
  979. }
  980. lwsl_info("%s: %p\n", __func__, vh);
  981. /* if we are still on deferred free list, remove ourselves */
  982. lws_start_foreach_llp(struct lws_deferred_free **, pdf,
  983. context->deferred_free_list) {
  984. if ((*pdf)->payload == vh) {
  985. df = *pdf;
  986. *pdf = df->next;
  987. lws_free(df);
  988. break;
  989. }
  990. } lws_end_foreach_llp(pdf, next);
  991. /* remove ourselves from the pending destruction list */
  992. lws_start_foreach_llp(struct lws_vhost **, pv,
  993. context->vhost_pending_destruction_list) {
  994. if ((*pv) == vh) {
  995. *pv = (*pv)->vhost_next;
  996. break;
  997. }
  998. } lws_end_foreach_llp(pv, vhost_next);
  999. /*
  1000. * Free all the allocations associated with the vhost
  1001. */
  1002. protocol = vh->protocols;
  1003. if (protocol) {
  1004. n = 0;
  1005. while (n < vh->count_protocols) {
  1006. if (vh->protocol_vh_privs &&
  1007. vh->protocol_vh_privs[n]) {
  1008. lws_free(vh->protocol_vh_privs[n]);
  1009. vh->protocol_vh_privs[n] = NULL;
  1010. }
  1011. protocol++;
  1012. n++;
  1013. }
  1014. }
  1015. if (vh->protocol_vh_privs)
  1016. lws_free(vh->protocol_vh_privs);
  1017. lws_ssl_SSL_CTX_destroy(vh);
  1018. lws_free(vh->same_vh_protocol_owner);
  1019. if (
  1020. #if defined(LWS_WITH_PLUGINS)
  1021. context->plugin_list ||
  1022. #endif
  1023. (context->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS) ||
  1024. vh->allocated_vhost_protocols)
  1025. lws_free((void *)vh->protocols);
  1026. #if defined(LWS_WITH_NETWORK)
  1027. LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar)
  1028. if (ar->destroy_vhost)
  1029. ar->destroy_vhost(vh);
  1030. LWS_FOR_EVERY_AVAILABLE_ROLE_END;
  1031. #endif
  1032. #ifdef LWS_WITH_ACCESS_LOG
  1033. if (vh->log_fd != (int)LWS_INVALID_FILE)
  1034. close(vh->log_fd);
  1035. #endif
  1036. #if defined (LWS_WITH_TLS)
  1037. lws_free_set_NULL(vh->tls.alloc_cert_path);
  1038. #endif
  1039. #if LWS_MAX_SMP > 1
  1040. pthread_mutex_destroy(&vh->lock);
  1041. #endif
  1042. #if defined(LWS_WITH_UNIX_SOCK)
  1043. if (LWS_UNIX_SOCK_ENABLED(vh)) {
  1044. n = unlink(vh->iface);
  1045. if (n)
  1046. lwsl_info("Closing unix socket %s: errno %d\n",
  1047. vh->iface, errno);
  1048. }
  1049. #endif
  1050. /*
  1051. * although async event callbacks may still come for wsi handles with
  1052. * pending close in the case of asycn event library like libuv,
  1053. * they do not refer to the vhost. So it's safe to free.
  1054. */
  1055. if (vh->finalize)
  1056. vh->finalize(vh, vh->finalize_arg);
  1057. #if defined(LWS_WITH_ABSTRACT)
  1058. /*
  1059. * abstract instances
  1060. */
  1061. lws_dll2_foreach_safe(&vh->abstract_instances_owner, NULL, destroy_ais);
  1062. #endif
  1063. lwsl_info(" %s: Freeing vhost %p\n", __func__, vh);
  1064. memset(vh, 0, sizeof(*vh));
  1065. lws_free(vh);
  1066. }
  1067. /*
  1068. * each service thread calls this once a second or so
  1069. */
  1070. int
  1071. lws_check_deferred_free(struct lws_context *context, int tsi, int force)
  1072. {
  1073. struct lws_context_per_thread *pt;
  1074. int n;
  1075. /*
  1076. * If we see a vhost is being destroyed, forcibly close every wsi on
  1077. * this tsi associated with this vhost. That will include the listen
  1078. * socket if it is still associated with the closing vhost.
  1079. *
  1080. * For SMP, we do this once per tsi per destroyed vhost. The reference
  1081. * counting on the vhost as the bound wsi close will notice that there
  1082. * are no bound wsi left, that vhost destruction can complete,
  1083. * and perform it. It doesn't matter which service thread does that
  1084. * because there is nothing left using the vhost to conflict.
  1085. */
  1086. lws_context_lock(context, "check deferred free"); /* ------ context { */
  1087. lws_start_foreach_ll_safe(struct lws_vhost *, v, context->vhost_list, vhost_next) {
  1088. if (v->being_destroyed
  1089. #if LWS_MAX_SMP > 1
  1090. && !v->close_flow_vs_tsi[tsi]
  1091. #endif
  1092. ) {
  1093. pt = &context->pt[tsi];
  1094. lws_pt_lock(pt, "vhost removal"); /* -------------- pt { */
  1095. #if LWS_MAX_SMP > 1
  1096. v->close_flow_vs_tsi[tsi] = 1;
  1097. #endif
  1098. for (n = 0; (unsigned int)n < pt->fds_count; n++) {
  1099. struct lws *wsi = wsi_from_fd(context, pt->fds[n].fd);
  1100. if (!wsi)
  1101. continue;
  1102. if (wsi->a.vhost != v)
  1103. continue;
  1104. __lws_close_free_wsi(wsi,
  1105. LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY,
  1106. "vh destroy"
  1107. /* no protocol close */);
  1108. n--;
  1109. }
  1110. lws_pt_unlock(pt); /* } pt -------------- */
  1111. }
  1112. } lws_end_foreach_ll_safe(v);
  1113. lws_context_unlock(context); /* } context ------------------- */
  1114. return 0;
  1115. }
  1116. void
  1117. lws_vhost_destroy(struct lws_vhost *vh)
  1118. {
  1119. struct lws_deferred_free *df = lws_malloc(sizeof(*df), "deferred free");
  1120. struct lws_context *context = vh->context;
  1121. if (!df)
  1122. return;
  1123. lws_context_lock(context, __func__); /* ------ context { */
  1124. lws_vhost_destroy1(vh);
  1125. lwsl_debug("%s: count_bound_wsi %d\n", __func__, vh->count_bound_wsi);
  1126. if (!vh->count_bound_wsi) {
  1127. /*
  1128. * After listen handoff, there are already no wsi bound to this
  1129. * vhost by any pt: nothing can be servicing any wsi belonging
  1130. * to it any more.
  1131. *
  1132. * Finalize the vh destruction immediately
  1133. */
  1134. __lws_vhost_destroy2(vh);
  1135. lws_free(df);
  1136. goto out;
  1137. }
  1138. /* part 2 is deferred to allow all the handle closes to complete */
  1139. df->next = vh->context->deferred_free_list;
  1140. df->deadline = lws_now_secs();
  1141. df->payload = vh;
  1142. vh->context->deferred_free_list = df;
  1143. out:
  1144. lws_context_unlock(context); /* } context ------------------- */
  1145. }
  1146. void *
  1147. lws_vhost_user(struct lws_vhost *vhost)
  1148. {
  1149. return vhost->user;
  1150. }
  1151. int
  1152. lws_get_vhost_listen_port(struct lws_vhost *vhost)
  1153. {
  1154. return vhost->listen_port;
  1155. }
  1156. #if defined(LWS_WITH_SERVER)
  1157. void
  1158. lws_context_deprecate(struct lws_context *context, lws_reload_func cb)
  1159. {
  1160. struct lws_vhost *vh = context->vhost_list, *vh1;
  1161. /*
  1162. * "deprecation" means disable the context from accepting any new
  1163. * connections and free up listen sockets to be used by a replacement
  1164. * context.
  1165. *
  1166. * Otherwise the deprecated context remains operational, until its
  1167. * number of connected sockets falls to zero, when it is deleted.
  1168. */
  1169. /* for each vhost, close his listen socket */
  1170. while (vh) {
  1171. struct lws *wsi = vh->lserv_wsi;
  1172. if (wsi) {
  1173. wsi->socket_is_permanently_unusable = 1;
  1174. lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS, "ctx deprecate");
  1175. wsi->a.context->deprecation_pending_listen_close_count++;
  1176. /*
  1177. * other vhosts can share the listen port, they
  1178. * point to the same wsi. So zap those too.
  1179. */
  1180. vh1 = context->vhost_list;
  1181. while (vh1) {
  1182. if (vh1->lserv_wsi == wsi)
  1183. vh1->lserv_wsi = NULL;
  1184. vh1 = vh1->vhost_next;
  1185. }
  1186. }
  1187. vh = vh->vhost_next;
  1188. }
  1189. context->deprecated = 1;
  1190. context->deprecation_cb = cb;
  1191. }
  1192. #endif
  1193. #if defined(LWS_WITH_NETWORK)
  1194. struct lws_vhost *
  1195. lws_get_vhost_by_name(struct lws_context *context, const char *name)
  1196. {
  1197. lws_start_foreach_ll(struct lws_vhost *, v,
  1198. context->vhost_list) {
  1199. if (!strcmp(v->name, name))
  1200. return v;
  1201. } lws_end_foreach_ll(v, vhost_next);
  1202. return NULL;
  1203. }
  1204. #if defined(LWS_WITH_CLIENT)
  1205. /*
  1206. * This is the logic checking to see if the new connection wsi should have a
  1207. * pipelining or muxing relationship with an existing "active connection" to
  1208. * the same endpoint under the same conditions.
  1209. *
  1210. * This was originally in the client code but since the list is held on the
  1211. * vhost (to ensure the same client tls ctx is involved) it's cleaner in vhost.c
  1212. *
  1213. * ACTIVE_CONNS_QUEUED: We're queued on an active connection, set *nwsi to that
  1214. * ACTIVE_CONNS_MUXED: We are joining an active mux conn *nwsi as a child
  1215. * ACTIVE_CONNS_SOLO: There's no existing conn to join either way
  1216. */
  1217. int
  1218. lws_vhost_active_conns(struct lws *wsi, struct lws **nwsi, const char *adsin)
  1219. {
  1220. if (!lws_dll2_is_detached(&wsi->dll2_cli_txn_queue)) {
  1221. struct lws *w = lws_container_of(
  1222. wsi->dll2_cli_txn_queue.owner, struct lws,
  1223. dll2_cli_txn_queue_owner);
  1224. *nwsi = w;
  1225. return ACTIVE_CONNS_QUEUED;
  1226. }
  1227. #if defined(LWS_ROLE_H2) || defined(LWS_ROLE_MQTT)
  1228. if (wsi->mux.parent_wsi) {
  1229. /*
  1230. * We already decided...
  1231. */
  1232. *nwsi = wsi->mux.parent_wsi;
  1233. return ACTIVE_CONNS_MUXED;
  1234. }
  1235. #endif
  1236. lws_vhost_lock(wsi->a.vhost); /* ----------------------------------- { */
  1237. lws_start_foreach_dll_safe(struct lws_dll2 *, d, d1,
  1238. wsi->a.vhost->dll_cli_active_conns_owner.head) {
  1239. struct lws *w = lws_container_of(d, struct lws,
  1240. dll_cli_active_conns);
  1241. lwsl_debug("%s: check %p %p %s %s %d %d\n", __func__, wsi, w,
  1242. adsin, w->cli_hostname_copy, wsi->c_port, w->c_port);
  1243. if (w != wsi &&
  1244. /*
  1245. * "same internet protocol"... this is a bit tricky,
  1246. * since h2 start out as h1
  1247. */
  1248. (w->role_ops == wsi->role_ops ||
  1249. (lwsi_role_http(w) && lwsi_role_http(wsi))) &&
  1250. w->cli_hostname_copy &&
  1251. !strcmp(adsin, w->cli_hostname_copy) &&
  1252. #if defined(LWS_WITH_TLS)
  1253. (wsi->tls.use_ssl & LCCSCF_USE_SSL) ==
  1254. (w->tls.use_ssl & LCCSCF_USE_SSL) &&
  1255. #endif
  1256. wsi->c_port == w->c_port) {
  1257. /*
  1258. * There's already an active connection.
  1259. *
  1260. * The server may have told the existing active
  1261. * connection that it doesn't support pipelining...
  1262. */
  1263. if (w->keepalive_rejected) {
  1264. lwsl_notice("defeating pipelining due to no "
  1265. "keepalive on server\n");
  1266. goto solo;
  1267. }
  1268. #if defined(LWS_WITH_HTTP2)
  1269. /*
  1270. * h2: if in usable state already: just use it without
  1271. * going through the queue
  1272. */
  1273. if (w->client_h2_alpn && w->client_mux_migrated &&
  1274. (lwsi_state(w) == LRS_H2_WAITING_TO_SEND_HEADERS ||
  1275. lwsi_state(w) == LRS_ESTABLISHED ||
  1276. lwsi_state(w) == LRS_IDLING)) {
  1277. lwsl_notice("%s: just join h2 directly 0x%x\n",
  1278. __func__, lwsi_state(w));
  1279. if (lwsi_state(w) == LRS_IDLING) {
  1280. // lwsi_set_state(w, LRS_ESTABLISHED);
  1281. _lws_generic_transaction_completed_active_conn(&w, 0);
  1282. }
  1283. //lwsi_set_state(w, LRS_H1C_ISSUE_HANDSHAKE2);
  1284. wsi->client_h2_alpn = 1;
  1285. lws_wsi_h2_adopt(w, wsi);
  1286. lws_vhost_unlock(wsi->a.vhost); /* } ---------- */
  1287. *nwsi = w;
  1288. return ACTIVE_CONNS_MUXED;
  1289. }
  1290. #endif
  1291. #if defined(LWS_ROLE_MQTT)
  1292. /*
  1293. * MQTT: if in usable state already: just use it without
  1294. * going through the queue
  1295. */
  1296. if (lwsi_role_mqtt(wsi) && w->client_mux_migrated &&
  1297. lwsi_state(w) == LRS_ESTABLISHED) {
  1298. if (lws_wsi_mqtt_adopt(w, wsi)) {
  1299. lwsl_notice("%s: join mqtt directly\n", __func__);
  1300. lws_dll2_remove(&wsi->dll2_cli_txn_queue);
  1301. wsi->client_mux_substream = 1;
  1302. lws_vhost_unlock(wsi->a.vhost); /* } ---------- */
  1303. return ACTIVE_CONNS_MUXED;
  1304. }
  1305. }
  1306. #endif
  1307. /*
  1308. * If the connection is viable but not yet in a usable
  1309. * state, let's attach ourselves to it and wait for it
  1310. * to get there or fail.
  1311. */
  1312. lwsl_notice("%s: apply %p to txn queue on %p state 0x%lx\n",
  1313. __func__, wsi, w, (unsigned long)w->wsistate);
  1314. /*
  1315. * ...let's add ourselves to his transaction queue...
  1316. * we are adding ourselves at the TAIL
  1317. */
  1318. lws_dll2_add_tail(&wsi->dll2_cli_txn_queue,
  1319. &w->dll2_cli_txn_queue_owner);
  1320. if (lwsi_state(w) == LRS_IDLING) {
  1321. // lwsi_set_state(w, LRS_ESTABLISHED);
  1322. _lws_generic_transaction_completed_active_conn(&w, 0);
  1323. }
  1324. /*
  1325. * For eg, h1 next we'd pipeline our headers out on him,
  1326. * and wait for our turn at client transaction_complete
  1327. * to take over parsing the rx.
  1328. */
  1329. lws_vhost_unlock(wsi->a.vhost); /* } ---------- */
  1330. *nwsi = w;
  1331. return ACTIVE_CONNS_QUEUED;
  1332. }
  1333. } lws_end_foreach_dll_safe(d, d1);
  1334. solo:
  1335. lws_vhost_unlock(wsi->a.vhost); /* } ---------------------------------- */
  1336. /* there is nobody already connected in the same way */
  1337. return ACTIVE_CONNS_SOLO;
  1338. }
  1339. #endif
  1340. #endif