context.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010-2018 Andy Green <[email protected]>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation:
  9. * version 2.1 of the License.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301 USA
  20. */
  21. #include "core/private.h"
  22. #ifndef LWS_BUILD_HASH
  23. #define LWS_BUILD_HASH "unknown-build-hash"
  24. #endif
  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. NULL
  39. };
  40. const struct lws_event_loop_ops *available_event_libs[] = {
  41. #if defined(LWS_WITH_POLL)
  42. &event_loop_ops_poll,
  43. #endif
  44. #if defined(LWS_WITH_LIBUV)
  45. &event_loop_ops_uv,
  46. #endif
  47. #if defined(LWS_WITH_LIBEVENT)
  48. &event_loop_ops_event,
  49. #endif
  50. #if defined(LWS_WITH_LIBEV)
  51. &event_loop_ops_ev,
  52. #endif
  53. NULL
  54. };
  55. static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH;
  56. /**
  57. * lws_get_library_version: get version and git hash library built from
  58. *
  59. * returns a const char * to a string like "1.1 178d78c"
  60. * representing the library version followed by the git head hash it
  61. * was built from
  62. */
  63. LWS_VISIBLE const char *
  64. lws_get_library_version(void)
  65. {
  66. return library_version;
  67. }
  68. int
  69. lws_role_call_alpn_negotiated(struct lws *wsi, const char *alpn)
  70. {
  71. #if defined(LWS_WITH_TLS)
  72. if (!alpn)
  73. return 0;
  74. lwsl_info("%s: '%s'\n", __func__, alpn);
  75. LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar)
  76. if (ar->alpn && !strcmp(ar->alpn, alpn) && ar->alpn_negotiated)
  77. return ar->alpn_negotiated(wsi, alpn);
  78. LWS_FOR_EVERY_AVAILABLE_ROLE_END;
  79. #endif
  80. return 0;
  81. }
  82. #if !defined(LWS_WITHOUT_SERVER)
  83. int
  84. lws_role_call_adoption_bind(struct lws *wsi, int type, const char *prot)
  85. {
  86. LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar)
  87. if (ar->adoption_bind)
  88. if (ar->adoption_bind(wsi, type, prot))
  89. return 0;
  90. LWS_FOR_EVERY_AVAILABLE_ROLE_END;
  91. /* fall back to raw socket role if, eg, h1 not configured */
  92. if (role_ops_raw_skt.adoption_bind &&
  93. role_ops_raw_skt.adoption_bind(wsi, type, prot))
  94. return 0;
  95. /* fall back to raw file role if, eg, h1 not configured */
  96. if (role_ops_raw_file.adoption_bind &&
  97. role_ops_raw_file.adoption_bind(wsi, type, prot))
  98. return 0;
  99. return 1;
  100. }
  101. #endif
  102. #if !defined(LWS_WITHOUT_CLIENT)
  103. int
  104. lws_role_call_client_bind(struct lws *wsi,
  105. const struct lws_client_connect_info *i)
  106. {
  107. LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar)
  108. if (ar->client_bind) {
  109. int m = ar->client_bind(wsi, i);
  110. if (m < 0)
  111. return m;
  112. if (m)
  113. return 0;
  114. }
  115. LWS_FOR_EVERY_AVAILABLE_ROLE_END;
  116. /* fall back to raw socket role if, eg, h1 not configured */
  117. if (role_ops_raw_skt.client_bind &&
  118. role_ops_raw_skt.client_bind(wsi, i))
  119. return 0;
  120. return 1;
  121. }
  122. #endif
  123. static const char * const mount_protocols[] = {
  124. "http://",
  125. "https://",
  126. "file://",
  127. "cgi://",
  128. ">http://",
  129. ">https://",
  130. "callback://"
  131. };
  132. LWS_VISIBLE void *
  133. lws_protocol_vh_priv_zalloc(struct lws_vhost *vhost,
  134. const struct lws_protocols *prot, int size)
  135. {
  136. int n = 0;
  137. /* allocate the vh priv array only on demand */
  138. if (!vhost->protocol_vh_privs) {
  139. vhost->protocol_vh_privs = (void **)lws_zalloc(
  140. vhost->count_protocols * sizeof(void *),
  141. "protocol_vh_privs");
  142. if (!vhost->protocol_vh_privs)
  143. return NULL;
  144. }
  145. while (n < vhost->count_protocols && &vhost->protocols[n] != prot)
  146. n++;
  147. if (n == vhost->count_protocols) {
  148. n = 0;
  149. while (n < vhost->count_protocols &&
  150. strcmp(vhost->protocols[n].name, prot->name))
  151. n++;
  152. if (n == vhost->count_protocols)
  153. return NULL;
  154. }
  155. vhost->protocol_vh_privs[n] = lws_zalloc(size, "vh priv");
  156. return vhost->protocol_vh_privs[n];
  157. }
  158. LWS_VISIBLE void *
  159. lws_protocol_vh_priv_get(struct lws_vhost *vhost,
  160. const struct lws_protocols *prot)
  161. {
  162. int n = 0;
  163. if (!vhost || !vhost->protocol_vh_privs || !prot)
  164. return NULL;
  165. while (n < vhost->count_protocols && &vhost->protocols[n] != prot)
  166. n++;
  167. if (n == vhost->count_protocols) {
  168. n = 0;
  169. while (n < vhost->count_protocols &&
  170. strcmp(vhost->protocols[n].name, prot->name))
  171. n++;
  172. if (n == vhost->count_protocols) {
  173. lwsl_err("%s: unknown protocol %p\n", __func__, prot);
  174. return NULL;
  175. }
  176. }
  177. return vhost->protocol_vh_privs[n];
  178. }
  179. static const struct lws_protocol_vhost_options *
  180. lws_vhost_protocol_options(struct lws_vhost *vh, const char *name)
  181. {
  182. const struct lws_protocol_vhost_options *pvo = vh->pvo;
  183. if (!name)
  184. return NULL;
  185. while (pvo) {
  186. if (!strcmp(pvo->name, name))
  187. return pvo;
  188. pvo = pvo->next;
  189. }
  190. return NULL;
  191. }
  192. /*
  193. * inform every vhost that hasn't already done it, that
  194. * his protocols are initializing
  195. */
  196. LWS_VISIBLE int
  197. lws_protocol_init(struct lws_context *context)
  198. {
  199. struct lws_vhost *vh = context->vhost_list;
  200. const struct lws_protocol_vhost_options *pvo, *pvo1;
  201. struct lws wsi;
  202. int n, any = 0;
  203. if (context->doing_protocol_init)
  204. return 0;
  205. context->doing_protocol_init = 1;
  206. memset(&wsi, 0, sizeof(wsi));
  207. wsi.context = context;
  208. lwsl_info("%s\n", __func__);
  209. while (vh) {
  210. wsi.vhost = vh;
  211. /* only do the protocol init once for a given vhost */
  212. if (vh->created_vhost_protocols ||
  213. (vh->options & LWS_SERVER_OPTION_SKIP_PROTOCOL_INIT))
  214. goto next;
  215. /* initialize supported protocols on this vhost */
  216. for (n = 0; n < vh->count_protocols; n++) {
  217. wsi.protocol = &vh->protocols[n];
  218. if (!vh->protocols[n].name)
  219. continue;
  220. pvo = lws_vhost_protocol_options(vh,
  221. vh->protocols[n].name);
  222. if (pvo) {
  223. /*
  224. * linked list of options specific to
  225. * vh + protocol
  226. */
  227. pvo1 = pvo;
  228. pvo = pvo1->options;
  229. while (pvo) {
  230. lwsl_debug(
  231. " vhost \"%s\", "
  232. "protocol \"%s\", "
  233. "option \"%s\"\n",
  234. vh->name,
  235. vh->protocols[n].name,
  236. pvo->name);
  237. if (!strcmp(pvo->name, "default")) {
  238. lwsl_info("Setting default "
  239. "protocol for vh %s to %s\n",
  240. vh->name,
  241. vh->protocols[n].name);
  242. vh->default_protocol_index = n;
  243. }
  244. if (!strcmp(pvo->name, "raw")) {
  245. lwsl_info("Setting raw "
  246. "protocol for vh %s to %s\n",
  247. vh->name,
  248. vh->protocols[n].name);
  249. vh->raw_protocol_index = n;
  250. }
  251. pvo = pvo->next;
  252. }
  253. pvo = pvo1->options;
  254. }
  255. #if defined(LWS_WITH_TLS)
  256. any |= !!vh->tls.ssl_ctx;
  257. #endif
  258. /*
  259. * inform all the protocols that they are doing their
  260. * one-time initialization if they want to.
  261. *
  262. * NOTE the wsi is all zeros except for the context, vh
  263. * + protocol ptrs so lws_get_context(wsi) etc can work
  264. */
  265. if (vh->protocols[n].callback(&wsi,
  266. LWS_CALLBACK_PROTOCOL_INIT, NULL,
  267. (void *)pvo, 0)) {
  268. lws_free(vh->protocol_vh_privs[n]);
  269. vh->protocol_vh_privs[n] = NULL;
  270. lwsl_err("%s: protocol %s failed init\n",
  271. __func__, vh->protocols[n].name);
  272. return 1;
  273. }
  274. }
  275. vh->created_vhost_protocols = 1;
  276. next:
  277. vh = vh->vhost_next;
  278. }
  279. context->doing_protocol_init = 0;
  280. if (!context->protocol_init_done)
  281. lws_finalize_startup(context);
  282. context->protocol_init_done = 1;
  283. if (any)
  284. lws_tls_check_all_cert_lifetimes(context);
  285. return 0;
  286. }
  287. /* list of supported protocols and callbacks */
  288. static const struct lws_protocols protocols_dummy[] = {
  289. /* first protocol must always be HTTP handler */
  290. {
  291. "http-only", /* name */
  292. lws_callback_http_dummy, /* callback */
  293. 0, /* per_session_data_size */
  294. 0, /* rx_buffer_size */
  295. 0, /* id */
  296. NULL, /* user */
  297. 0 /* tx_packet_size */
  298. },
  299. /*
  300. * the other protocols are provided by lws plugins
  301. */
  302. { NULL, NULL, 0, 0, 0, NULL, 0} /* terminator */
  303. };
  304. #ifdef LWS_PLAT_OPTEE
  305. #undef LWS_HAVE_GETENV
  306. #endif
  307. LWS_VISIBLE struct lws_vhost *
  308. lws_create_vhost(struct lws_context *context,
  309. const struct lws_context_creation_info *info)
  310. {
  311. struct lws_vhost *vh = lws_zalloc(sizeof(*vh), "create vhost"),
  312. **vh1 = &context->vhost_list;
  313. const struct lws_http_mount *mounts;
  314. const struct lws_protocols *pcols = info->protocols;
  315. const struct lws_protocol_vhost_options *pvo;
  316. #ifdef LWS_WITH_PLUGINS
  317. struct lws_plugin *plugin = context->plugin_list;
  318. #endif
  319. struct lws_protocols *lwsp;
  320. int m, f = !info->pvo;
  321. char buf[20];
  322. #if !defined(LWS_WITHOUT_CLIENT) && defined(LWS_HAVE_GETENV)
  323. char *p;
  324. #endif
  325. int n;
  326. if (!vh)
  327. return NULL;
  328. #if LWS_MAX_SMP > 1
  329. pthread_mutex_init(&vh->lock, NULL);
  330. #endif
  331. if (!pcols)
  332. pcols = &protocols_dummy[0];
  333. vh->context = context;
  334. if (!info->vhost_name)
  335. vh->name = "default";
  336. else
  337. vh->name = info->vhost_name;
  338. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  339. vh->http.error_document_404 = info->error_document_404;
  340. #endif
  341. if (info->options & LWS_SERVER_OPTION_ONLY_RAW)
  342. lwsl_info("%s set to only support RAW\n", vh->name);
  343. vh->iface = info->iface;
  344. #if !defined(LWS_WITH_ESP32) && \
  345. !defined(OPTEE_TA) && !defined(WIN32)
  346. vh->bind_iface = info->bind_iface;
  347. #endif
  348. for (vh->count_protocols = 0;
  349. pcols[vh->count_protocols].callback;
  350. vh->count_protocols++)
  351. ;
  352. vh->options = info->options;
  353. vh->pvo = info->pvo;
  354. vh->headers = info->headers;
  355. vh->user = info->user;
  356. vh->finalize = info->finalize;
  357. vh->finalize_arg = info->finalize_arg;
  358. LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar)
  359. if (ar->init_vhost)
  360. if (ar->init_vhost(vh, info))
  361. return NULL;
  362. LWS_FOR_EVERY_AVAILABLE_ROLE_END;
  363. if (info->keepalive_timeout)
  364. vh->keepalive_timeout = info->keepalive_timeout;
  365. else
  366. vh->keepalive_timeout = 5;
  367. if (info->timeout_secs_ah_idle)
  368. vh->timeout_secs_ah_idle = info->timeout_secs_ah_idle;
  369. else
  370. vh->timeout_secs_ah_idle = 10;
  371. #if defined(LWS_WITH_TLS)
  372. vh->tls.alpn = info->alpn;
  373. vh->tls.ssl_info_event_mask = info->ssl_info_event_mask;
  374. if (info->ecdh_curve)
  375. lws_strncpy(vh->tls.ecdh_curve, info->ecdh_curve,
  376. sizeof(vh->tls.ecdh_curve));
  377. /* carefully allocate and take a copy of cert + key paths if present */
  378. n = 0;
  379. if (info->ssl_cert_filepath)
  380. n += (int)strlen(info->ssl_cert_filepath) + 1;
  381. if (info->ssl_private_key_filepath)
  382. n += (int)strlen(info->ssl_private_key_filepath) + 1;
  383. if (n) {
  384. vh->tls.key_path = vh->tls.alloc_cert_path =
  385. lws_malloc(n, "vh paths");
  386. if (info->ssl_cert_filepath) {
  387. n = (int)strlen(info->ssl_cert_filepath) + 1;
  388. memcpy(vh->tls.alloc_cert_path,
  389. info->ssl_cert_filepath, n);
  390. vh->tls.key_path += n;
  391. }
  392. if (info->ssl_private_key_filepath)
  393. memcpy(vh->tls.key_path, info->ssl_private_key_filepath,
  394. strlen(info->ssl_private_key_filepath) + 1);
  395. }
  396. #endif
  397. /*
  398. * give the vhost a unified list of protocols including the
  399. * ones that came from plugins
  400. */
  401. lwsp = lws_zalloc(sizeof(struct lws_protocols) * (vh->count_protocols +
  402. context->plugin_protocol_count + 1),
  403. "vhost-specific plugin table");
  404. if (!lwsp) {
  405. lwsl_err("OOM\n");
  406. return NULL;
  407. }
  408. m = vh->count_protocols;
  409. memcpy(lwsp, pcols, sizeof(struct lws_protocols) * m);
  410. /* for compatibility, all protocols enabled on vhost if only
  411. * the default vhost exists. Otherwise only vhosts who ask
  412. * for a protocol get it enabled.
  413. */
  414. if (context->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
  415. f = 0;
  416. (void)f;
  417. #ifdef LWS_WITH_PLUGINS
  418. if (plugin) {
  419. while (plugin) {
  420. for (n = 0; n < plugin->caps.count_protocols; n++) {
  421. /*
  422. * for compatibility's sake, no pvo implies
  423. * allow all protocols
  424. */
  425. if (f || lws_vhost_protocol_options(vh,
  426. plugin->caps.protocols[n].name)) {
  427. memcpy(&lwsp[m],
  428. &plugin->caps.protocols[n],
  429. sizeof(struct lws_protocols));
  430. m++;
  431. vh->count_protocols++;
  432. }
  433. }
  434. plugin = plugin->list;
  435. }
  436. }
  437. #endif
  438. if (
  439. #ifdef LWS_WITH_PLUGINS
  440. (context->plugin_list) ||
  441. #endif
  442. context->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
  443. vh->protocols = lwsp;
  444. else {
  445. vh->protocols = pcols;
  446. lws_free(lwsp);
  447. }
  448. vh->same_vh_protocol_heads = (struct lws_dll_lws *)
  449. lws_zalloc(sizeof(struct lws_dll_lws) *
  450. vh->count_protocols, "same vh list");
  451. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  452. vh->http.mount_list = info->mounts;
  453. #endif
  454. #ifdef LWS_WITH_UNIX_SOCK
  455. if (LWS_UNIX_SOCK_ENABLED(vh)) {
  456. lwsl_notice("Creating Vhost '%s' path \"%s\", %d protocols\n",
  457. vh->name, vh->iface, vh->count_protocols);
  458. } else
  459. #endif
  460. {
  461. switch(info->port) {
  462. case CONTEXT_PORT_NO_LISTEN:
  463. strcpy(buf, "(serving disabled)");
  464. break;
  465. case CONTEXT_PORT_NO_LISTEN_SERVER:
  466. strcpy(buf, "(no listener)");
  467. break;
  468. default:
  469. lws_snprintf(buf, sizeof(buf), "port %u", info->port);
  470. break;
  471. }
  472. lwsl_notice("Creating Vhost '%s' %s, %d protocols, IPv6 %s\n",
  473. vh->name, buf, vh->count_protocols,
  474. LWS_IPV6_ENABLED(vh) ? "on" : "off");
  475. }
  476. mounts = info->mounts;
  477. while (mounts) {
  478. (void)mount_protocols[0];
  479. lwsl_info(" mounting %s%s to %s\n",
  480. mount_protocols[mounts->origin_protocol],
  481. mounts->origin, mounts->mountpoint);
  482. /* convert interpreter protocol names to pointers */
  483. pvo = mounts->interpret;
  484. while (pvo) {
  485. for (n = 0; n < vh->count_protocols; n++) {
  486. if (strcmp(pvo->value, vh->protocols[n].name))
  487. continue;
  488. ((struct lws_protocol_vhost_options *)pvo)->
  489. value = (const char *)(lws_intptr_t)n;
  490. break;
  491. }
  492. if (n == vh->count_protocols)
  493. lwsl_err("ignoring unknown interp pr %s\n",
  494. pvo->value);
  495. pvo = pvo->next;
  496. }
  497. mounts = mounts->mount_next;
  498. }
  499. vh->listen_port = info->port;
  500. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  501. vh->http.http_proxy_port = 0;
  502. vh->http.http_proxy_address[0] = '\0';
  503. #endif
  504. #if defined(LWS_WITH_SOCKS5)
  505. vh->socks_proxy_port = 0;
  506. vh->socks_proxy_address[0] = '\0';
  507. #endif
  508. #if !defined(LWS_WITHOUT_CLIENT)
  509. /* either use proxy from info, or try get it from env var */
  510. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  511. /* http proxy */
  512. if (info->http_proxy_address) {
  513. /* override for backwards compatibility */
  514. if (info->http_proxy_port)
  515. vh->http.http_proxy_port = info->http_proxy_port;
  516. lws_set_proxy(vh, info->http_proxy_address);
  517. } else
  518. #endif
  519. {
  520. #ifdef LWS_HAVE_GETENV
  521. p = getenv("http_proxy");
  522. if (p)
  523. lws_set_proxy(vh, p);
  524. #endif
  525. }
  526. #endif
  527. #if defined(LWS_WITH_SOCKS5)
  528. /* socks proxy */
  529. if (info->socks_proxy_address) {
  530. /* override for backwards compatibility */
  531. if (info->socks_proxy_port)
  532. vh->socks_proxy_port = info->socks_proxy_port;
  533. lws_set_socks(vh, info->socks_proxy_address);
  534. } else {
  535. #ifdef LWS_HAVE_GETENV
  536. p = getenv("socks_proxy");
  537. if (p)
  538. lws_set_socks(vh, p);
  539. #endif
  540. }
  541. #endif
  542. vh->ka_time = info->ka_time;
  543. vh->ka_interval = info->ka_interval;
  544. vh->ka_probes = info->ka_probes;
  545. if (vh->options & LWS_SERVER_OPTION_STS)
  546. lwsl_notice(" STS enabled\n");
  547. #ifdef LWS_WITH_ACCESS_LOG
  548. if (info->log_filepath) {
  549. vh->log_fd = lws_open(info->log_filepath,
  550. O_CREAT | O_APPEND | O_RDWR, 0600);
  551. if (vh->log_fd == (int)LWS_INVALID_FILE) {
  552. lwsl_err("unable to open log filepath %s\n",
  553. info->log_filepath);
  554. goto bail;
  555. }
  556. #ifndef WIN32
  557. if (context->uid != -1)
  558. if (chown(info->log_filepath, context->uid,
  559. context->gid) == -1)
  560. lwsl_err("unable to chown log file %s\n",
  561. info->log_filepath);
  562. #endif
  563. } else
  564. vh->log_fd = (int)LWS_INVALID_FILE;
  565. #endif
  566. if (lws_context_init_server_ssl(info, vh)) {
  567. lwsl_err("%s: lws_context_init_server_ssl failed\n", __func__);
  568. goto bail1;
  569. }
  570. if (lws_context_init_client_ssl(info, vh)) {
  571. lwsl_err("%s: lws_context_init_client_ssl failed\n", __func__);
  572. goto bail1;
  573. }
  574. lws_context_lock(context, "create_vhost");
  575. n = _lws_vhost_init_server(info, vh);
  576. lws_context_unlock(context);
  577. if (n < 0) {
  578. lwsl_err("init server failed\n");
  579. goto bail1;
  580. }
  581. while (1) {
  582. if (!(*vh1)) {
  583. *vh1 = vh;
  584. break;
  585. }
  586. vh1 = &(*vh1)->vhost_next;
  587. };
  588. /* for the case we are adding a vhost much later, after server init */
  589. if (context->protocol_init_done)
  590. if (lws_protocol_init(context)) {
  591. lwsl_err("%s: lws_protocol_init failed\n", __func__);
  592. goto bail1;
  593. }
  594. return vh;
  595. bail1:
  596. lws_vhost_destroy(vh);
  597. return NULL;
  598. #ifdef LWS_WITH_ACCESS_LOG
  599. bail:
  600. lws_free(vh);
  601. #endif
  602. return NULL;
  603. }
  604. LWS_VISIBLE int
  605. lws_init_vhost_client_ssl(const struct lws_context_creation_info *info,
  606. struct lws_vhost *vhost)
  607. {
  608. struct lws_context_creation_info i;
  609. memcpy(&i, info, sizeof(i));
  610. i.port = CONTEXT_PORT_NO_LISTEN;
  611. return lws_context_init_client_ssl(&i, vhost);
  612. }
  613. LWS_VISIBLE void
  614. lws_cancel_service_pt(struct lws *wsi)
  615. {
  616. lws_plat_pipe_signal(wsi);
  617. }
  618. LWS_VISIBLE void
  619. lws_cancel_service(struct lws_context *context)
  620. {
  621. struct lws_context_per_thread *pt = &context->pt[0];
  622. short m = context->count_threads;
  623. if (context->being_destroyed1)
  624. return;
  625. lwsl_info("%s\n", __func__);
  626. while (m--) {
  627. if (pt->pipe_wsi)
  628. lws_plat_pipe_signal(pt->pipe_wsi);
  629. pt++;
  630. }
  631. }
  632. int
  633. lws_create_event_pipes(struct lws_context *context)
  634. {
  635. struct lws *wsi;
  636. int n;
  637. /*
  638. * Create the pt event pipes... these are unique in that they are
  639. * not bound to a vhost or protocol (both are NULL)
  640. */
  641. for (n = 0; n < context->count_threads; n++) {
  642. if (context->pt[n].pipe_wsi)
  643. continue;
  644. wsi = lws_zalloc(sizeof(*wsi), "event pipe wsi");
  645. if (!wsi) {
  646. lwsl_err("%s: Out of mem\n", __func__);
  647. return 1;
  648. }
  649. wsi->context = context;
  650. lws_role_transition(wsi, 0, LRS_UNCONNECTED, &role_ops_pipe);
  651. wsi->protocol = NULL;
  652. wsi->tsi = n;
  653. wsi->vhost = NULL;
  654. wsi->event_pipe = 1;
  655. wsi->desc.sockfd = LWS_SOCK_INVALID;
  656. context->pt[n].pipe_wsi = wsi;
  657. context->count_wsi_allocated++;
  658. if (lws_plat_pipe_create(wsi))
  659. /*
  660. * platform code returns 0 if it actually created pipes
  661. * and initialized pt->dummy_pipe_fds[]. If it used
  662. * some other mechanism outside of signaling in the
  663. * normal event loop, we skip treating the pipe as
  664. * related to dummy_pipe_fds[], adding it to the fds,
  665. * etc.
  666. */
  667. continue;
  668. wsi->desc.sockfd = context->pt[n].dummy_pipe_fds[0];
  669. lwsl_debug("event pipe fd %d\n", wsi->desc.sockfd);
  670. if (context->event_loop_ops->accept)
  671. if (context->event_loop_ops->accept(wsi))
  672. return 1;
  673. if (__insert_wsi_socket_into_fds(context, wsi))
  674. return 1;
  675. }
  676. return 0;
  677. }
  678. void
  679. lws_destroy_event_pipe(struct lws *wsi)
  680. {
  681. lwsl_info("%s\n", __func__);
  682. __remove_wsi_socket_from_fds(wsi);
  683. if (wsi->context->event_loop_ops->wsi_logical_close) {
  684. wsi->context->event_loop_ops->wsi_logical_close(wsi);
  685. lws_plat_pipe_close(wsi);
  686. wsi->context->count_wsi_allocated--;
  687. return;
  688. }
  689. if (wsi->context->event_loop_ops->destroy_wsi)
  690. wsi->context->event_loop_ops->destroy_wsi(wsi);
  691. lws_plat_pipe_close(wsi);
  692. wsi->context->count_wsi_allocated--;
  693. lws_free(wsi);
  694. }
  695. LWS_VISIBLE struct lws_context *
  696. lws_create_context(const struct lws_context_creation_info *info)
  697. {
  698. struct lws_context *context = NULL;
  699. struct lws_plat_file_ops *prev;
  700. #ifndef LWS_NO_DAEMONIZE
  701. int pid_daemon = get_daemonize_pid();
  702. #endif
  703. int n;
  704. #if defined(__ANDROID__)
  705. struct rlimit rt;
  706. #endif
  707. lwsl_info("Initial logging level %d\n", log_level);
  708. lwsl_info("Libwebsockets version: %s\n", library_version);
  709. #ifdef LWS_WITH_IPV6
  710. if (!lws_check_opt(info->options, LWS_SERVER_OPTION_DISABLE_IPV6))
  711. lwsl_info("IPV6 compiled in and enabled\n");
  712. else
  713. lwsl_info("IPV6 compiled in but disabled\n");
  714. #else
  715. lwsl_info("IPV6 not compiled in\n");
  716. #endif
  717. lwsl_info(" LWS_DEF_HEADER_LEN : %u\n", LWS_DEF_HEADER_LEN);
  718. lwsl_info(" LWS_MAX_PROTOCOLS : %u\n", LWS_MAX_PROTOCOLS);
  719. lwsl_info(" LWS_MAX_SMP : %u\n", LWS_MAX_SMP);
  720. lwsl_info(" sizeof (*info) : %ld\n", (long)sizeof(*info));
  721. #if defined(LWS_WITH_STATS)
  722. lwsl_info(" LWS_WITH_STATS : on\n");
  723. #endif
  724. lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
  725. #if defined(LWS_WITH_HTTP2)
  726. lwsl_info(" HTTP2 support : available\n");
  727. #else
  728. lwsl_info(" HTTP2 support : not configured\n");
  729. #endif
  730. if (lws_plat_context_early_init())
  731. return NULL;
  732. context = lws_zalloc(sizeof(struct lws_context), "context");
  733. if (!context) {
  734. lwsl_err("No memory for websocket context\n");
  735. return NULL;
  736. }
  737. #if defined(LWS_WITH_TLS)
  738. #if defined(LWS_WITH_MBEDTLS)
  739. context->tls_ops = &tls_ops_mbedtls;
  740. #else
  741. context->tls_ops = &tls_ops_openssl;
  742. #endif
  743. #endif
  744. if (info->pt_serv_buf_size)
  745. context->pt_serv_buf_size = info->pt_serv_buf_size;
  746. else
  747. context->pt_serv_buf_size = 4096;
  748. #if defined(LWS_ROLE_H2)
  749. role_ops_h2.init_context(context, info);
  750. #endif
  751. #if LWS_MAX_SMP > 1
  752. lws_mutex_refcount_init(&context->mr);
  753. #endif
  754. #if defined(LWS_WITH_ESP32)
  755. context->last_free_heap = esp_get_free_heap_size();
  756. #endif
  757. /* default to just the platform fops implementation */
  758. context->fops_platform.LWS_FOP_OPEN = _lws_plat_file_open;
  759. context->fops_platform.LWS_FOP_CLOSE = _lws_plat_file_close;
  760. context->fops_platform.LWS_FOP_SEEK_CUR = _lws_plat_file_seek_cur;
  761. context->fops_platform.LWS_FOP_READ = _lws_plat_file_read;
  762. context->fops_platform.LWS_FOP_WRITE = _lws_plat_file_write;
  763. context->fops_platform.fi[0].sig = NULL;
  764. /*
  765. * arrange a linear linked-list of fops starting from context->fops
  766. *
  767. * platform fops
  768. * [ -> fops_zip (copied into context so .next settable) ]
  769. * [ -> info->fops ]
  770. */
  771. context->fops = &context->fops_platform;
  772. prev = (struct lws_plat_file_ops *)context->fops;
  773. #if defined(LWS_WITH_ZIP_FOPS)
  774. /* make a soft copy so we can set .next */
  775. context->fops_zip = fops_zip;
  776. prev->next = &context->fops_zip;
  777. prev = (struct lws_plat_file_ops *)prev->next;
  778. #endif
  779. /* if user provided fops, tack them on the end of the list */
  780. if (info->fops)
  781. prev->next = info->fops;
  782. context->reject_service_keywords = info->reject_service_keywords;
  783. if (info->external_baggage_free_on_destroy)
  784. context->external_baggage_free_on_destroy =
  785. info->external_baggage_free_on_destroy;
  786. context->time_up = time(NULL);
  787. context->pcontext_finalize = info->pcontext;
  788. context->simultaneous_ssl_restriction =
  789. info->simultaneous_ssl_restriction;
  790. #ifndef LWS_NO_DAEMONIZE
  791. if (pid_daemon) {
  792. context->started_with_parent = pid_daemon;
  793. lwsl_info(" Started with daemon pid %d\n", pid_daemon);
  794. }
  795. #endif
  796. #if defined(__ANDROID__)
  797. n = getrlimit ( RLIMIT_NOFILE,&rt);
  798. if (-1 == n) {
  799. lwsl_err("Get RLIMIT_NOFILE failed!\n");
  800. return NULL;
  801. }
  802. context->max_fds = rt.rlim_cur;
  803. #else
  804. context->max_fds = getdtablesize();
  805. #endif
  806. if (info->count_threads)
  807. context->count_threads = info->count_threads;
  808. else
  809. context->count_threads = 1;
  810. if (context->count_threads > LWS_MAX_SMP)
  811. context->count_threads = LWS_MAX_SMP;
  812. context->token_limits = info->token_limits;
  813. context->options = info->options;
  814. /*
  815. * set the context event loops ops struct
  816. *
  817. * after this, all event_loop actions use the generic ops
  818. */
  819. #if defined(LWS_WITH_POLL)
  820. context->event_loop_ops = &event_loop_ops_poll;
  821. #endif
  822. if (lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV))
  823. #if defined(LWS_WITH_LIBUV)
  824. context->event_loop_ops = &event_loop_ops_uv;
  825. #else
  826. goto fail_event_libs;
  827. #endif
  828. if (lws_check_opt(context->options, LWS_SERVER_OPTION_LIBEV))
  829. #if defined(LWS_WITH_LIBEV)
  830. context->event_loop_ops = &event_loop_ops_ev;
  831. #else
  832. goto fail_event_libs;
  833. #endif
  834. if (lws_check_opt(context->options, LWS_SERVER_OPTION_LIBEVENT))
  835. #if defined(LWS_WITH_LIBEVENT)
  836. context->event_loop_ops = &event_loop_ops_event;
  837. #else
  838. goto fail_event_libs;
  839. #endif
  840. if (!context->event_loop_ops)
  841. goto fail_event_libs;
  842. lwsl_info("Using event loop: %s\n", context->event_loop_ops->name);
  843. #if defined(LWS_WITH_TLS)
  844. time(&context->tls.last_cert_check_s);
  845. if (info->alpn)
  846. context->tls.alpn_default = info->alpn;
  847. else {
  848. char *p = context->tls.alpn_discovered, first = 1;
  849. LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar) {
  850. if (ar->alpn) {
  851. if (!first)
  852. *p++ = ',';
  853. p += lws_snprintf(p,
  854. context->tls.alpn_discovered +
  855. sizeof(context->tls.alpn_discovered) -
  856. 2 - p, "%s", ar->alpn);
  857. first = 0;
  858. }
  859. } LWS_FOR_EVERY_AVAILABLE_ROLE_END;
  860. context->tls.alpn_default = context->tls.alpn_discovered;
  861. }
  862. lwsl_info("Default ALPN advertisment: %s\n", context->tls.alpn_default);
  863. #endif
  864. if (info->timeout_secs)
  865. context->timeout_secs = info->timeout_secs;
  866. else
  867. context->timeout_secs = AWAITING_TIMEOUT;
  868. context->ws_ping_pong_interval = info->ws_ping_pong_interval;
  869. lwsl_info(" default timeout (secs): %u\n", context->timeout_secs);
  870. if (info->max_http_header_data)
  871. context->max_http_header_data = info->max_http_header_data;
  872. else
  873. if (info->max_http_header_data2)
  874. context->max_http_header_data =
  875. info->max_http_header_data2;
  876. else
  877. context->max_http_header_data = LWS_DEF_HEADER_LEN;
  878. if (info->max_http_header_pool)
  879. context->max_http_header_pool = info->max_http_header_pool;
  880. else
  881. if (info->max_http_header_pool2)
  882. context->max_http_header_pool =
  883. info->max_http_header_pool2;
  884. else
  885. context->max_http_header_pool = context->max_fds;
  886. if (info->fd_limit_per_thread)
  887. context->fd_limit_per_thread = info->fd_limit_per_thread;
  888. else
  889. context->fd_limit_per_thread = context->max_fds /
  890. context->count_threads;
  891. /*
  892. * Allocate the per-thread storage for scratchpad buffers,
  893. * and header data pool
  894. */
  895. for (n = 0; n < context->count_threads; n++) {
  896. context->pt[n].serv_buf = lws_malloc(context->pt_serv_buf_size,
  897. "pt_serv_buf");
  898. if (!context->pt[n].serv_buf) {
  899. lwsl_err("OOM\n");
  900. return NULL;
  901. }
  902. context->pt[n].context = context;
  903. context->pt[n].tid = n;
  904. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  905. context->pt[n].http.ah_list = NULL;
  906. context->pt[n].http.ah_pool_length = 0;
  907. #endif
  908. lws_pt_mutex_init(&context->pt[n]);
  909. }
  910. lwsl_info(" Threads: %d each %d fds\n", context->count_threads,
  911. context->fd_limit_per_thread);
  912. if (!info->ka_interval && info->ka_time > 0) {
  913. lwsl_err("info->ka_interval can't be 0 if ka_time used\n");
  914. return NULL;
  915. }
  916. #if defined(LWS_WITH_PEER_LIMITS)
  917. /* scale the peer hash table according to the max fds for the process,
  918. * so that the max list depth averages 16. Eg, 1024 fd -> 64,
  919. * 102400 fd -> 6400
  920. */
  921. context->pl_hash_elements =
  922. (context->count_threads * context->fd_limit_per_thread) / 16;
  923. context->pl_hash_table = lws_zalloc(sizeof(struct lws_peer *) *
  924. context->pl_hash_elements, "peer limits hash table");
  925. context->ip_limit_ah = info->ip_limit_ah;
  926. context->ip_limit_wsi = info->ip_limit_wsi;
  927. #endif
  928. lwsl_info(" mem: context: %5lu B (%ld ctx + (%ld thr x %d))\n",
  929. (long)sizeof(struct lws_context) +
  930. (context->count_threads * context->pt_serv_buf_size),
  931. (long)sizeof(struct lws_context),
  932. (long)context->count_threads,
  933. context->pt_serv_buf_size);
  934. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  935. lwsl_info(" mem: http hdr rsvd: %5lu B (%u thr x (%u + %lu) x %u))\n",
  936. (long)(context->max_http_header_data +
  937. sizeof(struct allocated_headers)) *
  938. context->max_http_header_pool * context->count_threads,
  939. context->count_threads,
  940. context->max_http_header_data,
  941. (long)sizeof(struct allocated_headers),
  942. context->max_http_header_pool);
  943. #endif
  944. n = sizeof(struct lws_pollfd) * context->count_threads *
  945. context->fd_limit_per_thread;
  946. context->pt[0].fds = lws_zalloc(n, "fds table");
  947. if (context->pt[0].fds == NULL) {
  948. lwsl_err("OOM allocating %d fds\n", context->max_fds);
  949. goto bail;
  950. }
  951. lwsl_info(" mem: pollfd map: %5u\n", n);
  952. if (info->server_string) {
  953. context->server_string = info->server_string;
  954. context->server_string_len = (short)
  955. strlen(context->server_string);
  956. }
  957. #if LWS_MAX_SMP > 1
  958. /* each thread serves his own chunk of fds */
  959. for (n = 1; n < (int)info->count_threads; n++)
  960. context->pt[n].fds = context->pt[n - 1].fds +
  961. context->fd_limit_per_thread;
  962. #endif
  963. if (lws_plat_init(context, info))
  964. goto bail;
  965. if (context->event_loop_ops->init_context)
  966. if (context->event_loop_ops->init_context(context, info))
  967. goto bail;
  968. if (context->event_loop_ops->init_pt)
  969. for (n = 0; n < context->count_threads; n++) {
  970. void *lp = NULL;
  971. if (info->foreign_loops)
  972. lp = info->foreign_loops[n];
  973. if (context->event_loop_ops->init_pt(context, lp, n))
  974. goto bail;
  975. }
  976. if (lws_create_event_pipes(context))
  977. goto bail;
  978. lws_context_init_ssl_library(info);
  979. context->user_space = info->user;
  980. /*
  981. * if he's not saying he'll make his own vhosts later then act
  982. * compatibly and make a default vhost using the data in the info
  983. */
  984. if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
  985. if (!lws_create_vhost(context, info)) {
  986. lwsl_err("Failed to create default vhost\n");
  987. for (n = 0; n < context->count_threads; n++)
  988. lws_free_set_NULL(context->pt[n].serv_buf);
  989. #if defined(LWS_WITH_PEER_LIMITS)
  990. lws_free_set_NULL(context->pl_hash_table);
  991. #endif
  992. lws_free_set_NULL(context->pt[0].fds);
  993. lws_plat_context_late_destroy(context);
  994. lws_free_set_NULL(context);
  995. return NULL;
  996. }
  997. lws_context_init_extensions(info, context);
  998. lwsl_info(" mem: per-conn: %5lu bytes + protocol rx buf\n",
  999. (unsigned long)sizeof(struct lws));
  1000. strcpy(context->canonical_hostname, "unknown");
  1001. lws_server_get_canonical_hostname(context, info);
  1002. context->uid = info->uid;
  1003. context->gid = info->gid;
  1004. #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
  1005. memcpy(context->caps, info->caps, sizeof(context->caps));
  1006. context->count_caps = info->count_caps;
  1007. #endif
  1008. /*
  1009. * drop any root privs for this process
  1010. * to listen on port < 1023 we would have needed root, but now we are
  1011. * listening, we don't want the power for anything else
  1012. */
  1013. if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
  1014. lws_plat_drop_app_privileges(info);
  1015. /* expedite post-context init (eg, protocols) */
  1016. lws_cancel_service(context);
  1017. #if defined(LWS_WITH_SELFTESTS)
  1018. lws_jws_selftest();
  1019. #endif
  1020. return context;
  1021. bail:
  1022. lws_context_destroy(context);
  1023. return NULL;
  1024. fail_event_libs:
  1025. lwsl_err("Requested event library support not configured, available:\n");
  1026. {
  1027. const struct lws_event_loop_ops **elops = available_event_libs;
  1028. while (*elops) {
  1029. lwsl_err(" - %s\n", (*elops)->name);
  1030. elops++;
  1031. }
  1032. }
  1033. lws_free(context);
  1034. return NULL;
  1035. }
  1036. LWS_VISIBLE LWS_EXTERN void
  1037. lws_context_deprecate(struct lws_context *context, lws_reload_func cb)
  1038. {
  1039. struct lws_vhost *vh = context->vhost_list, *vh1;
  1040. /*
  1041. * "deprecation" means disable the context from accepting any new
  1042. * connections and free up listen sockets to be used by a replacement
  1043. * context.
  1044. *
  1045. * Otherwise the deprecated context remains operational, until its
  1046. * number of connected sockets falls to zero, when it is deleted.
  1047. */
  1048. /* for each vhost, close his listen socket */
  1049. while (vh) {
  1050. struct lws *wsi = vh->lserv_wsi;
  1051. if (wsi) {
  1052. wsi->socket_is_permanently_unusable = 1;
  1053. lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS, "ctx deprecate");
  1054. wsi->context->deprecation_pending_listen_close_count++;
  1055. /*
  1056. * other vhosts can share the listen port, they
  1057. * point to the same wsi. So zap those too.
  1058. */
  1059. vh1 = context->vhost_list;
  1060. while (vh1) {
  1061. if (vh1->lserv_wsi == wsi)
  1062. vh1->lserv_wsi = NULL;
  1063. vh1 = vh1->vhost_next;
  1064. }
  1065. }
  1066. vh = vh->vhost_next;
  1067. }
  1068. context->deprecated = 1;
  1069. context->deprecation_cb = cb;
  1070. }
  1071. LWS_VISIBLE LWS_EXTERN int
  1072. lws_context_is_deprecated(struct lws_context *context)
  1073. {
  1074. return context->deprecated;
  1075. }
  1076. void
  1077. lws_vhost_destroy1(struct lws_vhost *vh)
  1078. {
  1079. struct lws_context *context = vh->context;
  1080. lwsl_info("%s\n", __func__);
  1081. lws_context_lock(context, "vhost destroy 1"); /* ---------- context { */
  1082. if (vh->being_destroyed)
  1083. goto out;
  1084. lws_vhost_lock(vh); /* -------------- vh { */
  1085. vh->being_destroyed = 1;
  1086. /*
  1087. * PHASE 1: take down or reassign any listen wsi
  1088. *
  1089. * Are there other vhosts that are piggybacking on our listen socket?
  1090. * If so we need to hand the listen socket off to one of the others
  1091. * so it will remain open.
  1092. *
  1093. * If not, leave it attached to the closing vhost, the vh being marked
  1094. * being_destroyed will defeat any service and it will get closed in
  1095. * later phases.
  1096. */
  1097. if (vh->lserv_wsi)
  1098. lws_start_foreach_ll(struct lws_vhost *, v,
  1099. context->vhost_list) {
  1100. if (v != vh &&
  1101. !v->being_destroyed &&
  1102. v->listen_port == vh->listen_port &&
  1103. ((!v->iface && !vh->iface) ||
  1104. (v->iface && vh->iface &&
  1105. !strcmp(v->iface, vh->iface)))) {
  1106. /*
  1107. * this can only be a listen wsi, which is
  1108. * restricted... it has no protocol or other
  1109. * bindings or states. So we can simply
  1110. * swap it to a vhost that has the same
  1111. * iface + port, but is not closing.
  1112. */
  1113. assert(v->lserv_wsi == NULL);
  1114. v->lserv_wsi = vh->lserv_wsi;
  1115. lwsl_notice("%s: listen skt from %s to %s\n",
  1116. __func__, vh->name, v->name);
  1117. if (v->lserv_wsi) {
  1118. lws_vhost_unbind_wsi(vh->lserv_wsi);
  1119. lws_vhost_bind_wsi(v, v->lserv_wsi);
  1120. }
  1121. break;
  1122. }
  1123. } lws_end_foreach_ll(v, vhost_next);
  1124. lws_vhost_unlock(vh); /* } vh -------------- */
  1125. /*
  1126. * lws_check_deferred_free() will notice there is a vhost that is
  1127. * marked for destruction during the next 1s, for all tsi.
  1128. *
  1129. * It will start closing all wsi on this vhost. When the last wsi
  1130. * is closed, it will trigger lws_vhost_destroy2()
  1131. */
  1132. out:
  1133. lws_context_unlock(context); /* --------------------------- context { */
  1134. }
  1135. void
  1136. __lws_vhost_destroy2(struct lws_vhost *vh)
  1137. {
  1138. const struct lws_protocols *protocol = NULL;
  1139. struct lws_context *context = vh->context;
  1140. struct lws_deferred_free *df;
  1141. struct lws wsi;
  1142. int n;
  1143. /*
  1144. * destroy any pending timed events
  1145. */
  1146. while (vh->timed_vh_protocol_list)
  1147. __lws_timed_callback_remove(vh, vh->timed_vh_protocol_list);
  1148. /*
  1149. * let the protocols destroy the per-vhost protocol objects
  1150. */
  1151. memset(&wsi, 0, sizeof(wsi));
  1152. wsi.context = vh->context;
  1153. wsi.vhost = vh; /* not a real bound wsi */
  1154. protocol = vh->protocols;
  1155. if (protocol && vh->created_vhost_protocols) {
  1156. n = 0;
  1157. while (n < vh->count_protocols) {
  1158. wsi.protocol = protocol;
  1159. protocol->callback(&wsi, LWS_CALLBACK_PROTOCOL_DESTROY,
  1160. NULL, NULL, 0);
  1161. protocol++;
  1162. n++;
  1163. }
  1164. }
  1165. /*
  1166. * remove vhost from context list of vhosts
  1167. */
  1168. lws_start_foreach_llp(struct lws_vhost **, pv, context->vhost_list) {
  1169. if (*pv == vh) {
  1170. *pv = vh->vhost_next;
  1171. break;
  1172. }
  1173. } lws_end_foreach_llp(pv, vhost_next);
  1174. /* add ourselves to the pending destruction list */
  1175. vh->vhost_next = vh->context->vhost_pending_destruction_list;
  1176. vh->context->vhost_pending_destruction_list = vh;
  1177. lwsl_info("%s: %p\n", __func__, vh);
  1178. /* if we are still on deferred free list, remove ourselves */
  1179. lws_start_foreach_llp(struct lws_deferred_free **, pdf,
  1180. context->deferred_free_list) {
  1181. if ((*pdf)->payload == vh) {
  1182. df = *pdf;
  1183. *pdf = df->next;
  1184. lws_free(df);
  1185. break;
  1186. }
  1187. } lws_end_foreach_llp(pdf, next);
  1188. /* remove ourselves from the pending destruction list */
  1189. lws_start_foreach_llp(struct lws_vhost **, pv,
  1190. context->vhost_pending_destruction_list) {
  1191. if ((*pv) == vh) {
  1192. *pv = (*pv)->vhost_next;
  1193. break;
  1194. }
  1195. } lws_end_foreach_llp(pv, vhost_next);
  1196. /*
  1197. * Free all the allocations associated with the vhost
  1198. */
  1199. protocol = vh->protocols;
  1200. if (protocol) {
  1201. n = 0;
  1202. while (n < vh->count_protocols) {
  1203. if (vh->protocol_vh_privs &&
  1204. vh->protocol_vh_privs[n]) {
  1205. lws_free(vh->protocol_vh_privs[n]);
  1206. vh->protocol_vh_privs[n] = NULL;
  1207. }
  1208. protocol++;
  1209. n++;
  1210. }
  1211. }
  1212. if (vh->protocol_vh_privs)
  1213. lws_free(vh->protocol_vh_privs);
  1214. lws_ssl_SSL_CTX_destroy(vh);
  1215. lws_free(vh->same_vh_protocol_heads);
  1216. if (context->plugin_list ||
  1217. (context->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
  1218. lws_free((void *)vh->protocols);
  1219. LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar)
  1220. if (ar->destroy_vhost)
  1221. ar->destroy_vhost(vh);
  1222. LWS_FOR_EVERY_AVAILABLE_ROLE_END;
  1223. #ifdef LWS_WITH_ACCESS_LOG
  1224. if (vh->log_fd != (int)LWS_INVALID_FILE)
  1225. close(vh->log_fd);
  1226. #endif
  1227. #if defined (LWS_WITH_TLS)
  1228. lws_free_set_NULL(vh->tls.alloc_cert_path);
  1229. #endif
  1230. #if LWS_MAX_SMP > 1
  1231. pthread_mutex_destroy(&vh->lock);
  1232. #endif
  1233. #if defined(LWS_WITH_UNIX_SOCK)
  1234. if (LWS_UNIX_SOCK_ENABLED(vh)) {
  1235. n = unlink(vh->iface);
  1236. if (n)
  1237. lwsl_info("Closing unix socket %s: errno %d\n",
  1238. vh->iface, errno);
  1239. }
  1240. #endif
  1241. /*
  1242. * although async event callbacks may still come for wsi handles with
  1243. * pending close in the case of asycn event library like libuv,
  1244. * they do not refer to the vhost. So it's safe to free.
  1245. */
  1246. if (vh->finalize)
  1247. vh->finalize(vh, vh->finalize_arg);
  1248. lwsl_info(" %s: Freeing vhost %p\n", __func__, vh);
  1249. memset(vh, 0, sizeof(*vh));
  1250. lws_free(vh);
  1251. }
  1252. /*
  1253. * each service thread calls this once a second or so
  1254. */
  1255. int
  1256. lws_check_deferred_free(struct lws_context *context, int tsi, int force)
  1257. {
  1258. struct lws_context_per_thread *pt;
  1259. int n;
  1260. /*
  1261. * If we see a vhost is being destroyed, forcibly close every wsi on
  1262. * this tsi associated with this vhost. That will include the listen
  1263. * socket if it is still associated with the closing vhost.
  1264. *
  1265. * For SMP, we do this once per tsi per destroyed vhost. The reference
  1266. * counting on the vhost as the bound wsi close will notice that there
  1267. * are no bound wsi left, that vhost destruction can complete,
  1268. * and perform it. It doesn't matter which service thread does that
  1269. * because there is nothing left using the vhost to conflict.
  1270. */
  1271. lws_context_lock(context, "check deferred free"); /* ------ context { */
  1272. lws_start_foreach_ll_safe(struct lws_vhost *, v, context->vhost_list, vhost_next) {
  1273. if (v->being_destroyed
  1274. #if LWS_MAX_SMP > 1
  1275. && !v->close_flow_vs_tsi[tsi]
  1276. #endif
  1277. ) {
  1278. pt = &context->pt[tsi];
  1279. lws_pt_lock(pt, "vhost removal"); /* -------------- pt { */
  1280. #if LWS_MAX_SMP > 1
  1281. v->close_flow_vs_tsi[tsi] = 1;
  1282. #endif
  1283. for (n = 0; (unsigned int)n < pt->fds_count; n++) {
  1284. struct lws *wsi = wsi_from_fd(context, pt->fds[n].fd);
  1285. if (!wsi)
  1286. continue;
  1287. if (wsi->vhost != v)
  1288. continue;
  1289. __lws_close_free_wsi(wsi,
  1290. LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY,
  1291. "vh destroy"
  1292. /* no protocol close */);
  1293. n--;
  1294. }
  1295. lws_pt_unlock(pt); /* } pt -------------- */
  1296. }
  1297. } lws_end_foreach_ll_safe(v);
  1298. lws_context_unlock(context); /* } context ------------------- */
  1299. return 0;
  1300. }
  1301. LWS_VISIBLE void
  1302. lws_vhost_destroy(struct lws_vhost *vh)
  1303. {
  1304. struct lws_deferred_free *df = lws_malloc(sizeof(*df), "deferred free");
  1305. struct lws_context *context = vh->context;
  1306. if (!df)
  1307. return;
  1308. lws_context_lock(context, __func__); /* ------ context { */
  1309. lws_vhost_destroy1(vh);
  1310. if (!vh->count_bound_wsi) {
  1311. /*
  1312. * After listen handoff, there are already no wsi bound to this
  1313. * vhost by any pt: nothing can be servicing any wsi belonging
  1314. * to it any more.
  1315. *
  1316. * Finalize the vh destruction immediately
  1317. */
  1318. __lws_vhost_destroy2(vh);
  1319. lws_free(df);
  1320. goto out;
  1321. }
  1322. /* part 2 is deferred to allow all the handle closes to complete */
  1323. df->next = vh->context->deferred_free_list;
  1324. df->deadline = lws_now_secs();
  1325. df->payload = vh;
  1326. vh->context->deferred_free_list = df;
  1327. out:
  1328. lws_context_unlock(context); /* } context ------------------- */
  1329. }
  1330. /*
  1331. * When using an event loop, the context destruction is in three separate
  1332. * parts. This is to cover both internal and foreign event loops cleanly.
  1333. *
  1334. * - lws_context_destroy() simply starts a soft close of all wsi and
  1335. * related allocations. The event loop continues.
  1336. *
  1337. * As the closes complete in the event loop, reference counting is used
  1338. * to determine when everything is closed. It then calls
  1339. * lws_context_destroy2().
  1340. *
  1341. * - lws_context_destroy2() cleans up the rest of the higher-level logical
  1342. * lws pieces like vhosts. If the loop was foreign, it then proceeds to
  1343. * lws_context_destroy3(). If it the loop is internal, it stops the
  1344. * internal loops and waits for lws_context_destroy() to be called again
  1345. * outside the event loop (since we cannot destroy the loop from
  1346. * within the loop). That will cause lws_context_destroy3() to run
  1347. * directly.
  1348. *
  1349. * - lws_context_destroy3() destroys any internal event loops and then
  1350. * destroys the context itself, setting what was info.pcontext to NULL.
  1351. */
  1352. /*
  1353. * destroy the actual context itself
  1354. */
  1355. static void
  1356. lws_context_destroy3(struct lws_context *context)
  1357. {
  1358. struct lws_context **pcontext_finalize = context->pcontext_finalize;
  1359. int n;
  1360. for (n = 0; n < context->count_threads; n++) {
  1361. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  1362. struct lws_context_per_thread *pt = &context->pt[n];
  1363. #endif
  1364. if (context->event_loop_ops->destroy_pt)
  1365. context->event_loop_ops->destroy_pt(context, n);
  1366. lws_free_set_NULL(context->pt[n].serv_buf);
  1367. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  1368. while (pt->http.ah_list)
  1369. _lws_destroy_ah(pt, pt->http.ah_list);
  1370. #endif
  1371. }
  1372. if (context->pt[0].fds)
  1373. lws_free_set_NULL(context->pt[0].fds);
  1374. lws_free(context);
  1375. lwsl_info("%s: ctx %p freed\n", __func__, context);
  1376. if (pcontext_finalize)
  1377. *pcontext_finalize = NULL;
  1378. }
  1379. /*
  1380. * really start destroying things
  1381. */
  1382. void
  1383. lws_context_destroy2(struct lws_context *context)
  1384. {
  1385. struct lws_vhost *vh = NULL, *vh1;
  1386. #if defined(LWS_WITH_PEER_LIMITS)
  1387. uint32_t nu;
  1388. #endif
  1389. lwsl_info("%s: ctx %p\n", __func__, context);
  1390. lws_context_lock(context, "context destroy 2"); /* ------ context { */
  1391. context->being_destroyed2 = 1;
  1392. /*
  1393. * free all the per-vhost allocations
  1394. */
  1395. vh = context->vhost_list;
  1396. while (vh) {
  1397. vh1 = vh->vhost_next;
  1398. __lws_vhost_destroy2(vh);
  1399. vh = vh1;
  1400. }
  1401. /* remove ourselves from the pending destruction list */
  1402. while (context->vhost_pending_destruction_list)
  1403. /* removes itself from list */
  1404. __lws_vhost_destroy2(context->vhost_pending_destruction_list);
  1405. lws_stats_log_dump(context);
  1406. lws_ssl_context_destroy(context);
  1407. lws_plat_context_late_destroy(context);
  1408. #if defined(LWS_WITH_PEER_LIMITS)
  1409. for (nu = 0; nu < context->pl_hash_elements; nu++) {
  1410. lws_start_foreach_llp(struct lws_peer **, peer,
  1411. context->pl_hash_table[nu]) {
  1412. struct lws_peer *df = *peer;
  1413. *peer = df->next;
  1414. lws_free(df);
  1415. continue;
  1416. } lws_end_foreach_llp(peer, next);
  1417. }
  1418. lws_free(context->pl_hash_table);
  1419. #endif
  1420. if (context->external_baggage_free_on_destroy)
  1421. free(context->external_baggage_free_on_destroy);
  1422. lws_check_deferred_free(context, 0, 1);
  1423. #if LWS_MAX_SMP > 1
  1424. lws_mutex_refcount_destroy(&context->mr);
  1425. #endif
  1426. if (context->event_loop_ops->destroy_context2)
  1427. if (context->event_loop_ops->destroy_context2(context)) {
  1428. lws_context_unlock(context); /* } context ----------- */
  1429. context->finalize_destroy_after_internal_loops_stopped = 1;
  1430. return;
  1431. }
  1432. if (!context->pt[0].event_loop_foreign) {
  1433. int n;
  1434. for (n = 0; n < context->count_threads; n++)
  1435. if (context->pt[n].inside_service) {
  1436. lws_context_unlock(context); /* } context --- */
  1437. return;
  1438. }
  1439. }
  1440. lws_context_unlock(context); /* } context ------------------- */
  1441. lws_context_destroy3(context);
  1442. }
  1443. /*
  1444. * Begin the context takedown
  1445. */
  1446. LWS_VISIBLE void
  1447. lws_context_destroy(struct lws_context *context)
  1448. {
  1449. volatile struct lws_foreign_thread_pollfd *ftp, *next;
  1450. volatile struct lws_context_per_thread *vpt;
  1451. struct lws_vhost *vh = NULL;
  1452. struct lws wsi;
  1453. int n, m;
  1454. if (!context)
  1455. return;
  1456. if (context->finalize_destroy_after_internal_loops_stopped) {
  1457. if (context->event_loop_ops->destroy_context2)
  1458. context->event_loop_ops->destroy_context2(context);
  1459. lws_context_destroy3(context);
  1460. return;
  1461. }
  1462. if (context->being_destroyed1) {
  1463. if (!context->being_destroyed2) {
  1464. lws_context_destroy2(context);
  1465. return;
  1466. }
  1467. lwsl_info("%s: ctx %p: already being destroyed\n",
  1468. __func__, context);
  1469. lws_context_destroy3(context);
  1470. return;
  1471. }
  1472. lwsl_info("%s: ctx %p\n", __func__, context);
  1473. m = context->count_threads;
  1474. context->being_destroyed = 1;
  1475. context->being_destroyed1 = 1;
  1476. context->requested_kill = 1;
  1477. memset(&wsi, 0, sizeof(wsi));
  1478. wsi.context = context;
  1479. #ifdef LWS_LATENCY
  1480. if (context->worst_latency_info[0])
  1481. lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
  1482. #endif
  1483. while (m--) {
  1484. struct lws_context_per_thread *pt = &context->pt[m];
  1485. vpt = (volatile struct lws_context_per_thread *)pt;
  1486. ftp = vpt->foreign_pfd_list;
  1487. while (ftp) {
  1488. next = ftp->next;
  1489. lws_free((void *)ftp);
  1490. ftp = next;
  1491. }
  1492. vpt->foreign_pfd_list = NULL;
  1493. for (n = 0; (unsigned int)n < context->pt[m].fds_count; n++) {
  1494. struct lws *wsi = wsi_from_fd(context, pt->fds[n].fd);
  1495. if (!wsi)
  1496. continue;
  1497. if (wsi->event_pipe)
  1498. lws_destroy_event_pipe(wsi);
  1499. else
  1500. lws_close_free_wsi(wsi,
  1501. LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY,
  1502. "ctx destroy"
  1503. /* no protocol close */);
  1504. n--;
  1505. }
  1506. lws_pt_mutex_destroy(pt);
  1507. }
  1508. /*
  1509. * inform all the protocols that they are done and will have no more
  1510. * callbacks.
  1511. *
  1512. * We can't free things until after the event loop shuts down.
  1513. */
  1514. if (context->protocol_init_done)
  1515. vh = context->vhost_list;
  1516. while (vh) {
  1517. struct lws_vhost *vhn = vh->vhost_next;
  1518. lws_vhost_destroy1(vh);
  1519. vh = vhn;
  1520. }
  1521. lws_plat_context_early_destroy(context);
  1522. /*
  1523. * We face two different needs depending if foreign loop or not.
  1524. *
  1525. * 1) If foreign loop, we really want to advance the destroy_context()
  1526. * past here, and block only for libuv-style async close completion.
  1527. *
  1528. * 2a) If poll, and we exited by ourselves and are calling a final
  1529. * destroy_context() outside of any service already, we want to
  1530. * advance all the way in one step.
  1531. *
  1532. * 2b) If poll, and we are reacting to a SIGINT, service thread(s) may
  1533. * be in poll wait or servicing. We can't advance the
  1534. * destroy_context() to the point it's freeing things; we have to
  1535. * leave that for the final destroy_context() after the service
  1536. * thread(s) are finished calling for service.
  1537. */
  1538. if (context->event_loop_ops->destroy_context1) {
  1539. context->event_loop_ops->destroy_context1(context);
  1540. return;
  1541. }
  1542. lws_context_destroy2(context);
  1543. }