• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 /*! \defgroup context-and-vhost context and vhost related functions
26  * ##Context and Vhost releated functions
27  * \ingroup lwsapi
28  *
29  *
30  *  LWS requires that there is one context, in which you may define multiple
31  *  vhosts.  Each vhost is a virtual host, with either its own listen port
32  *  or sharing an existing one.  Each vhost has its own SSL context that can
33  *  be set up individually or left disabled.
34  *
35  *  If you don't care about multiple "site" support, you can ignore it and
36  *  lws will create a single default vhost at context creation time.
37  */
38 ///@{
39 
40 /*
41  * NOTE: These public enums are part of the abi.  If you want to add one,
42  * add it at where specified so existing users are unaffected.
43  */
44 
45 
46 #define LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT	 ((1ll << 1) | \
47 								  (1ll << 12))
48 	/**< (VH) Don't allow the connection unless the client has a
49 	 * client cert that we recognize; provides
50 	 * LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT */
51 #define LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME		  (1ll << 2)
52 	/**< (CTX) Don't try to get the server's hostname */
53 #define LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT		 ((1ll << 3) | \
54 								  (1ll << 12))
55 	/**< (VH) Allow non-SSL (plaintext) connections on the same
56 	 * port as SSL is listening.  If combined with
57 	 * LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS it will try to
58 	 * force http connections on an https listener (eg, http://x.com:443) to
59 	 * redirect to an explicit https connection (eg, https://x.com)
60 	 */
61 #define LWS_SERVER_OPTION_LIBEV					 (1ll << 4)
62 	/**< (CTX) Use libev event loop */
63 #define LWS_SERVER_OPTION_DISABLE_IPV6				 (1ll << 5)
64 	/**< (VH) Disable IPV6 support */
65 #define LWS_SERVER_OPTION_DISABLE_OS_CA_CERTS			 (1ll << 6)
66 	/**< (VH) Don't load OS CA certs, you will need to load your
67 	 * own CA cert(s) */
68 #define LWS_SERVER_OPTION_PEER_CERT_NOT_REQUIRED		 (1ll << 7)
69 	/**< (VH) Accept connections with no valid Cert (eg, selfsigned) */
70 #define LWS_SERVER_OPTION_VALIDATE_UTF8				 (1ll << 8)
71 	/**< (VH) Check UT-8 correctness */
72 #define LWS_SERVER_OPTION_SSL_ECDH				 ((1ll << 9) | \
73 								  (1ll << 12))
74 	/**< (VH)  initialize ECDH ciphers */
75 #define LWS_SERVER_OPTION_LIBUV					(1ll << 10)
76 	/**< (CTX)  Use libuv event loop */
77 #define LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS		((1ll << 11) |\
78 								 (1ll << 12))
79 	/**< (VH) Use an http redirect to force the client to ask for https.
80 	 * Notice if your http server issues the STS header and the client has
81 	 * ever seen that, the client will fail the http connection before it
82 	 * can actually do the redirect.
83 	 *
84 	 * Combine with LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS to handle, eg,
85 	 * http://x.com:443 -> https://x.com
86 	 *
87 	 * (deprecated: use mount redirection) */
88 #define LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT			 (1ll << 12)
89 	/**< (CTX) Initialize the SSL library at all */
90 #define LWS_SERVER_OPTION_EXPLICIT_VHOSTS			 (1ll << 13)
91 	/**< (CTX) Only create the context when calling context
92 	 * create api, implies user code will create its own vhosts */
93 #define LWS_SERVER_OPTION_UNIX_SOCK				 (1ll << 14)
94 	/**< (VH) Use Unix socket */
95 #define LWS_SERVER_OPTION_STS					 (1ll << 15)
96 	/**< (VH) Send Strict Transport Security header, making
97 	 * clients subsequently go to https even if user asked for http */
98 #define LWS_SERVER_OPTION_IPV6_V6ONLY_MODIFY			 (1ll << 16)
99 	/**< (VH) Enable LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE to take effect */
100 #define LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE			 (1ll << 17)
101 	/**< (VH) if set, only ipv6 allowed on the vhost */
102 #define LWS_SERVER_OPTION_UV_NO_SIGSEGV_SIGFPE_SPIN		 (1ll << 18)
103 	/**< (CTX) Libuv only: Do not spin on SIGSEGV / SIGFPE.  A segfault
104 	 * normally makes the lib spin so you can attach a debugger to it
105 	 * even if it happened without a debugger in place.  You can disable
106 	 * that by giving this option.
107 	 */
108 #define LWS_SERVER_OPTION_JUST_USE_RAW_ORIGIN			 (1ll << 19)
109 	/**< For backwards-compatibility reasons, by default
110 	 * lws prepends "http://" to the origin you give in the client
111 	 * connection info struct.  If you give this flag when you create
112 	 * the context, only the string you give in the client connect
113 	 * info for .origin (if any) will be used directly.
114 	 */
115 #define LWS_SERVER_OPTION_FALLBACK_TO_RAW /* use below name */	 (1ll << 20)
116 #define LWS_SERVER_OPTION_FALLBACK_TO_APPLY_LISTEN_ACCEPT_CONFIG (1ll << 20)
117 	/**< (VH) if invalid http is coming in the first line, then abandon
118 	 * trying to treat the connection as http, and belatedly apply the
119 	 * .listen_accept_role / .listen_accept_protocol info struct members to
120 	 * the connection.  If they are NULL, for backwards-compatibility the
121 	 * connection is bound to "raw-skt" role, and in order of priority:
122 	 * 1) the vh protocol with a pvo named "raw", 2) the vh protocol with a
123 	 * pvo named "default", or 3) protocols[0].
124 	 *
125 	 * Must be combined with LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT
126 	 * to work with a socket listening with tls.
127 	 */
128 
129 #define LWS_SERVER_OPTION_LIBEVENT				(1ll << 21)
130 	/**< (CTX) Use libevent event loop */
131 
132 #define LWS_SERVER_OPTION_ONLY_RAW /* Use below name instead */	(1ll << 22)
133 #define LWS_SERVER_OPTION_ADOPT_APPLY_LISTEN_ACCEPT_CONFIG	(1ll << 22)
134 	/**< (VH) All connections to this vhost / port are bound to the
135 	 * role and protocol given in .listen_accept_role /
136 	 * .listen_accept_protocol.
137 	 *
138 	 * If those explicit user-controlled names are NULL, for backwards-
139 	 * compatibility the connection is bound to "raw-skt" role, and in order
140 	 * of priority: 1) the vh protocol with a pvo named "raw", 2) the vh
141 	 * protocol with a pvo named "default", or 3) protocols[0].
142 	 *
143 	 * It's much preferred to specify the role + protocol using the
144 	 * .listen_accept_role and .listen_accept_protocol in the info struct.
145 	 */
146 #define LWS_SERVER_OPTION_ALLOW_LISTEN_SHARE			(1ll << 23)
147 	/**< (VH) Set to allow multiple listen sockets on one interface +
148 	 * address + port.  The default is to strictly allow only one
149 	 * listen socket at a time.  This is automatically selected if you
150 	 * have multiple service threads.  Linux only.
151 	 */
152 #define LWS_SERVER_OPTION_CREATE_VHOST_SSL_CTX			(1ll << 24)
153 	/**< (VH) Force setting up the vhost SSL_CTX, even though the user
154 	 * code doesn't explicitly provide a cert in the info struct.  It
155 	 * implies the user code is going to provide a cert at the
156 	 * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS callback, which
157 	 * provides the vhost SSL_CTX * in the user parameter.
158 	 */
159 #define LWS_SERVER_OPTION_SKIP_PROTOCOL_INIT			(1ll << 25)
160 	/**< (VH) You probably don't want this.  It forces this vhost to not
161 	 * call LWS_CALLBACK_PROTOCOL_INIT on its protocols.  It's used in the
162 	 * special case of a temporary vhost bound to a single protocol.
163 	 */
164 #define LWS_SERVER_OPTION_IGNORE_MISSING_CERT			(1ll << 26)
165 	/**< (VH) Don't fail if the vhost TLS cert or key are missing, just
166 	 * continue.  The vhost won't be able to serve anything, but if for
167 	 * example the ACME plugin was configured to fetch a cert, this lets
168 	 * you bootstrap your vhost from having no cert to start with.
169 	 */
170 #define LWS_SERVER_OPTION_VHOST_UPG_STRICT_HOST_CHECK		(1ll << 27)
171 	/**< (VH) On this vhost, if the connection is being upgraded, insist
172 	 * that there's a Host: header and that the contents match the vhost
173 	 * name + port (443 / 80 are assumed if no :port given based on if the
174 	 * connection is using TLS).
175 	 *
176 	 * By default, without this flag, on upgrade lws just checks that the
177 	 * Host: header was given without checking the contents... this is to
178 	 * allow lax hostname mappings like localhost / 127.0.0.1, and CNAME
179 	 * mappings like www.mysite.com / mysite.com
180 	 */
181 #define LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE (1ll << 28)
182 	/**< (VH) Send lws default HTTP headers recommended by Mozilla
183 	 * Observatory for security.  This is a helper option that sends canned
184 	 * headers on each http response enabling a VERY strict Content Security
185 	 * Policy.  The policy is so strict, for example it won't let the page
186 	 * run its own inline JS nor show images or take CSS from a different
187 	 * server.  In many cases your JS only comes from your server as do the
188 	 * image sources and CSS, so that is what you want... attackers hoping
189 	 * to inject JS into your DOM are completely out of luck since even if
190 	 * they succeed, it will be rejected for execution by the browser
191 	 * according to the strict CSP.  In other cases you have to deviate from
192 	 * the complete strictness, in which case don't use this flag: use the
193 	 * .headers member in the vhost init described in struct
194 	 * lws_context_creation_info instead to send the adapted headers
195 	 * yourself.
196 	 */
197 
198 #define LWS_SERVER_OPTION_ALLOW_HTTP_ON_HTTPS_LISTENER		 (1ll << 29)
199 	/**< (VH) If you really want to allow HTTP connections on a tls
200 	 * listener, you can do it with this combined with
201 	 * LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT.  But this is allowing
202 	 * accidental loss of the security assurances provided by tls depending
203 	 * on the client using http when he meant https... it's not
204 	 * recommended.
205 	 */
206 #define LWS_SERVER_OPTION_FAIL_UPON_UNABLE_TO_BIND		 (1ll << 30)
207 	/**< (VH) When instantiating a new vhost and the specified port is
208 	 * already in use, a null value shall be return to signal the error.
209 	 */
210 
211 #define LWS_SERVER_OPTION_H2_JUST_FIX_WINDOW_UPDATE_OVERFLOW	 (1ll << 31)
212 	/**< (VH) Indicates the connections using this vhost should ignore
213 	 * h2 WINDOW_UPDATE from broken peers and fix them up */
214 
215 #define LWS_SERVER_OPTION_VH_H2_HALF_CLOSED_LONG_POLL		 (1ll << 32)
216 	/**< (VH) Tell the vhost to treat half-closed remote clients as
217 	 * entered into an immortal (ie, not subject to normal timeouts) long
218 	 * poll mode.
219 	 */
220 
221 #define LWS_SERVER_OPTION_GLIB					 (1ll << 33)
222 	/**< (CTX) Use glib event loop */
223 
224 #define LWS_SERVER_OPTION_H2_PRIOR_KNOWLEDGE			 (1ll << 34)
225 	/**< (VH) Tell the vhost to treat plain text http connections as
226 	 * H2 with prior knowledge (no upgrade request involved)
227 	 */
228 
229 #define LWS_SERVER_OPTION_NO_LWS_SYSTEM_STATES			 (1ll << 35)
230 	/**< (CTX) Disable lws_system state, eg, because we are a secure streams
231 	 * proxy client that is not trying to track system state by itself. */
232 
233 #define LWS_SERVER_OPTION_SS_PROXY				 (1ll << 36)
234 	/**< (VH) We are being a SS Proxy listen socket for the vhost */
235 
236 #define LWS_SERVER_OPTION_SDEVENT			 	 (1ll << 37)
237 	/**< (CTX) Use sd-event loop */
238 
239 #define LWS_SERVER_OPTION_ULOOP					 (1ll << 38)
240 	/**< (CTX) Use libubox / uloop event loop */
241 
242 #define LWS_SERVER_OPTION_DISABLE_TLS_SESSION_CACHE		 (1ll << 39)
243 	/**< (VHOST) Disallow use of client tls caching (on by default) */
244 
245 
246 	/****** add new things just above ---^ ******/
247 
248 
249 #define lws_check_opt(c, f) ((((uint64_t)c) & ((uint64_t)f)) == ((uint64_t)f))
250 
251 struct lws_plat_file_ops;
252 struct lws_ss_policy;
253 struct lws_ss_plugin;
254 struct lws_metric_policy;
255 
256 typedef int (*lws_context_ready_cb_t)(struct lws_context *context);
257 
258 typedef int (*lws_peer_limits_notify_t)(struct lws_context *ctx,
259 					lws_sockfd_type sockfd,
260 					lws_sockaddr46 *sa46);
261 
262 /** struct lws_context_creation_info - parameters to create context and /or vhost with
263  *
264  * This is also used to create vhosts.... if LWS_SERVER_OPTION_EXPLICIT_VHOSTS
265  * is not given, then for backwards compatibility one vhost is created at
266  * context-creation time using the info from this struct.
267  *
268  * If LWS_SERVER_OPTION_EXPLICIT_VHOSTS is given, then no vhosts are created
269  * at the same time as the context, they are expected to be created afterwards.
270  */
271 struct lws_context_creation_info {
272 #if defined(LWS_WITH_NETWORK)
273 	const char *iface;
274 	/**< VHOST: NULL to bind the listen socket to all interfaces, or the
275 	 * interface name, eg, "eth2"
276 	 * If options specifies LWS_SERVER_OPTION_UNIX_SOCK, this member is
277 	 * the pathname of a UNIX domain socket. you can use the UNIX domain
278 	 * sockets in abstract namespace, by prepending an at symbol to the
279 	 * socket name. */
280 	const struct lws_protocols *protocols;
281 	/**< VHOST: Array of structures listing supported protocols and a
282 	 * protocol-specific callback for each one.  The list is ended with an
283 	 * entry that has a NULL callback pointer.  SEE ALSO .pprotocols below,
284 	 * which gives an alternative way to provide an array of pointers to
285 	 * protocol structs. */
286 #if defined(LWS_ROLE_WS)
287 	const struct lws_extension *extensions;
288 	/**< VHOST: NULL or array of lws_extension structs listing the
289 	 * extensions this context supports. */
290 #endif
291 #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
292 	const struct lws_token_limits *token_limits;
293 	/**< CONTEXT: NULL or struct lws_token_limits pointer which is
294 	 * initialized with a token length limit for each possible WSI_TOKEN_ */
295 	const char *http_proxy_address;
296 	/**< VHOST: If non-NULL, attempts to proxy via the given address.
297 	 * If proxy auth is required, use format
298 	 * "username:password\@server:port" */
299 	const struct lws_protocol_vhost_options *headers;
300 		/**< VHOST: pointer to optional linked list of per-vhost
301 		 * canned headers that are added to server responses */
302 
303 	const struct lws_protocol_vhost_options *reject_service_keywords;
304 	/**< CONTEXT: Optional list of keywords and rejection codes + text.
305 	 *
306 	 * The keywords are checked for existing in the user agent string.
307 	 *
308 	 * Eg, "badrobot" "404 Not Found"
309 	 */
310 	const struct lws_protocol_vhost_options *pvo;
311 	/**< VHOST: pointer to optional linked list of per-vhost
312 	 * options made accessible to protocols */
313 	const char *log_filepath;
314 	/**< VHOST: filepath to append logs to... this is opened before
315 	 *		any dropping of initial privileges */
316 	const struct lws_http_mount *mounts;
317 	/**< VHOST: optional linked list of mounts for this vhost */
318 	const char *server_string;
319 	/**< CONTEXT: string used in HTTP headers to identify server
320 	 * software, if NULL, "libwebsockets". */
321 
322 	const char *error_document_404;
323 	/**< VHOST: If non-NULL, when asked to serve a non-existent file,
324 	 *          lws attempts to server this url path instead.  Eg,
325 	 *          "/404.html" */
326 	int port;
327 	/**< VHOST: Port to listen on. Use CONTEXT_PORT_NO_LISTEN to suppress
328 	 * listening for a client. Use CONTEXT_PORT_NO_LISTEN_SERVER if you are
329 	 * writing a server but you are using \ref sock-adopt instead of the
330 	 * built-in listener.
331 	 *
332 	 * You can also set port to 0, in which case the kernel will pick
333 	 * a random port that is not already in use.  You can find out what
334 	 * port the vhost is listening on using lws_get_vhost_listen_port() */
335 
336 	unsigned int http_proxy_port;
337 	/**< VHOST: If http_proxy_address was non-NULL, uses this port */
338 	unsigned int max_http_header_data2;
339 	/**< CONTEXT: if max_http_header_data is 0 and this
340 	 * is nonzero, this will be used in place of the default.  It's
341 	 * like this for compatibility with the original short version,
342 	 * this is unsigned int length. */
343 	unsigned int max_http_header_pool2;
344 	/**< CONTEXT: if max_http_header_pool is 0 and this
345 	 * is nonzero, this will be used in place of the default.  It's
346 	 * like this for compatibility with the original short version:
347 	 * this is unsigned int length. */
348 
349 	int keepalive_timeout;
350 	/**< VHOST: (default = 0 = 5s, 31s for http/2) seconds to allow remote
351 	 * client to hold on to an idle HTTP/1.1 connection.  Timeout lifetime
352 	 * applied to idle h2 network connections */
353 	uint32_t	http2_settings[7];
354 	/**< VHOST:  if http2_settings[0] is nonzero, the values given in
355 	 *	      http2_settings[1]..[6] are used instead of the lws
356 	 *	      platform default values.
357 	 *	      Just leave all at 0 if you don't care.
358 	 */
359 
360 	unsigned short max_http_header_data;
361 	/**< CONTEXT: The max amount of header payload that can be handled
362 	 * in an http request (unrecognized header payload is dropped) */
363 	unsigned short max_http_header_pool;
364 	/**< CONTEXT: The max number of connections with http headers that
365 	 * can be processed simultaneously (the corresponding memory is
366 	 * allocated and deallocated dynamically as needed).  If the pool is
367 	 * fully busy new incoming connections must wait for accept until one
368 	 * becomes free. 0 = allow as many ah as number of availble fds for
369 	 * the process */
370 
371 #endif
372 
373 #if defined(LWS_WITH_TLS)
374 	const char *ssl_private_key_password;
375 	/**< VHOST: NULL or the passphrase needed for the private key. (For
376 	 * backwards compatibility, this can also be used to pass the client
377 	 * cert passphrase when setting up a vhost client SSL context, but it is
378 	 * preferred to use .client_ssl_private_key_password for that.) */
379 	const char *ssl_cert_filepath;
380 	/**< VHOST: If libwebsockets was compiled to use ssl, and you want
381 	 * to listen using SSL, set to the filepath to fetch the
382 	 * server cert from, otherwise NULL for unencrypted.  (For backwards
383 	 * compatibility, this can also be used to pass the client certificate
384 	 * when setting up a vhost client SSL context, but it is preferred to
385 	 * use .client_ssl_cert_filepath for that.)
386 	 *
387 	 * Notice you can alternatively set a single DER or PEM from a memory
388 	 * buffer as the vhost tls cert using \p server_ssl_cert_mem and
389 	 * \p server_ssl_cert_mem_len.
390 	 */
391 	const char *ssl_private_key_filepath;
392 	/**<  VHOST: filepath to private key if wanting SSL mode;
393 	 * if this is set to NULL but ssl_cert_filepath is set, the
394 	 * OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY callback is called
395 	 * to allow setting of the private key directly via openSSL
396 	 * library calls.   (For backwards compatibility, this can also be used
397 	 * to pass the client cert private key filepath when setting up a
398 	 * vhost client SSL context, but it is preferred to use
399 	 * .client_ssl_private_key_filepath for that.)
400 	 *
401 	 * Notice you can alternatively set a DER or PEM private key from a
402 	 * memory buffer as the vhost tls private key using
403 	 * \p server_ssl_private_key_mem and \p server_ssl_private_key_mem_len.
404 	 */
405 	const char *ssl_ca_filepath;
406 	/**< VHOST: CA certificate filepath or NULL.  (For backwards
407 	 * compatibility, this can also be used to pass the client CA
408 	 * filepath when setting up a vhost client SSL context,
409 	 * but it is preferred to use .client_ssl_ca_filepath for that.)
410 	 *
411 	 * Notice you can alternatively set a DER or PEM CA cert from a memory
412 	 * buffer using \p server_ssl_ca_mem and \p server_ssl_ca_mem_len.
413 	 */
414 	const char *ssl_cipher_list;
415 	/**< VHOST: List of valid ciphers to use ON TLS1.2 AND LOWER ONLY (eg,
416 	 * "RC4-MD5:RC4-SHA:AES128-SHA:AES256-SHA:HIGH:!DSS:!aNULL"
417 	 * or you can leave it as NULL to get "DEFAULT" (For backwards
418 	 * compatibility, this can also be used to pass the client cipher
419 	 * list when setting up a vhost client SSL context,
420 	 * but it is preferred to use .client_ssl_cipher_list for that.)
421 	 * SEE .tls1_3_plus_cipher_list and .client_tls_1_3_plus_cipher_list
422 	 * for the equivalent for tls1.3.
423 	 */
424 	const char *ecdh_curve;
425 	/**< VHOST: if NULL, defaults to initializing server with
426 	 *   "prime256v1" */
427 	const char *tls1_3_plus_cipher_list;
428 	/**< VHOST: List of valid ciphers to use for incoming server connections
429 	 * ON TLS1.3 AND ABOVE (eg, "TLS_CHACHA20_POLY1305_SHA256" on this vhost
430 	 * or you can leave it as NULL to get "DEFAULT".
431 	 * SEE .client_tls_1_3_plus_cipher_list to do the same on the vhost
432 	 * client SSL_CTX.
433 	 */
434 
435 	const void *server_ssl_cert_mem;
436 	/**< VHOST: Alternative for \p ssl_cert_filepath that allows setting
437 	 * from memory instead of from a file.  At most one of
438 	 * \p ssl_cert_filepath or \p server_ssl_cert_mem should be non-NULL. */
439 	const void *server_ssl_private_key_mem;
440 	/**<  VHOST: Alternative for \p ssl_private_key_filepath allowing
441 	 * init from a private key in memory instead of a file.  At most one
442 	 * of \p ssl_private_key_filepath or \p server_ssl_private_key_mem
443 	 * should be non-NULL. */
444 	const void *server_ssl_ca_mem;
445 	/**< VHOST: Alternative for \p ssl_ca_filepath allowing
446 	 * init from a CA cert in memory instead of a file.  At most one
447 	 * of \p ssl_ca_filepath or \p server_ssl_ca_mem should be non-NULL. */
448 
449 	long ssl_options_set;
450 	/**< VHOST: Any bits set here will be set as server SSL options */
451 	long ssl_options_clear;
452 	/**< VHOST: Any bits set here will be cleared as server SSL options */
453 	int simultaneous_ssl_restriction;
454 	/**< CONTEXT: 0 (no limit) or limit of simultaneous SSL sessions
455 	 * possible.*/
456 	int simultaneous_ssl_handshake_restriction;
457 	/**< CONTEXT: 0 (no limit) or limit of simultaneous SSL handshakes ongoing */
458 	int ssl_info_event_mask;
459 	/**< VHOST: mask of ssl events to be reported on LWS_CALLBACK_SSL_INFO
460 	 * callback for connections on this vhost.  The mask values are of
461 	 * the form SSL_CB_ALERT, defined in openssl/ssl.h.  The default of
462 	 * 0 means no info events will be reported.
463 	 */
464 	unsigned int server_ssl_cert_mem_len;
465 	/**< VHOST: Server SSL context init: length of server_ssl_cert_mem in
466 	 * bytes */
467 	unsigned int server_ssl_private_key_mem_len;
468 	/**< VHOST: length of \p server_ssl_private_key_mem in memory */
469 	unsigned int server_ssl_ca_mem_len;
470 	/**< VHOST: length of \p server_ssl_ca_mem in memory */
471 
472 	const char *alpn;
473 	/**< CONTEXT: If non-NULL, default list of advertised alpn, comma-
474 	 *	      separated
475 	 *
476 	 *     VHOST: If non-NULL, per-vhost list of advertised alpn, comma-
477 	 *	      separated
478 	 */
479 
480 
481 #if defined(LWS_WITH_CLIENT)
482 	const char *client_ssl_private_key_password;
483 	/**< VHOST: Client SSL context init: NULL or the passphrase needed
484 	 * for the private key */
485 	const char *client_ssl_cert_filepath;
486 	/**< VHOST: Client SSL context init: The certificate the client
487 	 * should present to the peer on connection */
488 	const void *client_ssl_cert_mem;
489 	/**< VHOST: Client SSL context init: client certificate memory buffer or
490 	 * NULL... use this to load client cert from memory instead of file */
491 	unsigned int client_ssl_cert_mem_len;
492 	/**< VHOST: Client SSL context init: length of client_ssl_cert_mem in
493 	 * bytes */
494 	const char *client_ssl_private_key_filepath;
495 	/**<  VHOST: Client SSL context init: filepath to client private key
496 	 * if this is set to NULL but client_ssl_cert_filepath is set, you
497 	 * can handle the LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS
498 	 * callback of protocols[0] to allow setting of the private key directly
499 	 * via tls library calls */
500 	const void *client_ssl_key_mem;
501 	/**< VHOST: Client SSL context init: client key memory buffer or
502 	 * NULL... use this to load client key from memory instead of file */
503 	const char *client_ssl_ca_filepath;
504 	/**< VHOST: Client SSL context init: CA certificate filepath or NULL */
505         const char *client_ssl_ca_dirs[10];
506 	/**< VHOST: Client SSL context init: CA certificate path or NULL */
507 	const void *client_ssl_ca_mem;
508 	/**< VHOST: Client SSL context init: CA certificate memory buffer or
509 	 * NULL... use this to load CA cert from memory instead of file */
510 
511 	const char *client_ssl_cipher_list;
512 	/**< VHOST: Client SSL context init: List of valid ciphers to use (eg,
513 	* "RC4-MD5:RC4-SHA:AES128-SHA:AES256-SHA:HIGH:!DSS:!aNULL"
514 	* or you can leave it as NULL to get "DEFAULT" */
515 	const char *client_tls_1_3_plus_cipher_list;
516 	/**< VHOST: List of valid ciphers to use for outgoing client connections
517 	 * ON TLS1.3 AND ABOVE on this vhost (eg,
518 	 * "TLS_CHACHA20_POLY1305_SHA256") or you can leave it as NULL to get
519 	 * "DEFAULT".
520 	 */
521 
522 	long ssl_client_options_set;
523 	/**< VHOST: Any bits set here will be set as CLIENT SSL options */
524 	long ssl_client_options_clear;
525 	/**< VHOST: Any bits set here will be cleared as CLIENT SSL options */
526 
527 
528 	unsigned int client_ssl_ca_mem_len;
529 	/**< VHOST: Client SSL context init: length of client_ssl_ca_mem in
530 	 * bytes */
531 	unsigned int client_ssl_key_mem_len;
532 	/**< VHOST: Client SSL context init: length of client_ssl_key_mem in
533 	 * bytes */
534 
535 #endif
536 
537 #if !defined(LWS_WITH_MBEDTLS)
538 	SSL_CTX *provided_client_ssl_ctx;
539 	/**< CONTEXT: If non-null, swap out libwebsockets ssl
540 	  * implementation for the one provided by provided_ssl_ctx.
541 	  * Libwebsockets no longer is responsible for freeing the context
542 	  * if this option is selected. */
543 #else /* WITH_MBEDTLS */
544 	const char *mbedtls_client_preload_filepath;
545 	/**< CONTEXT: If NULL, no effect.  Otherwise it should point to a
546 	 * filepath where every created client SSL_CTX is preloaded from the
547 	 * system trust bundle.
548 	 *
549 	 * This sets a processwide variable that affects all contexts.
550 	 *
551 	 * Requires that the mbedtls provides mbedtls_x509_crt_parse_file(),
552 	 * else disabled.
553 	 */
554 #endif
555 #endif
556 
557 	int ka_time;
558 	/**< CONTEXT: 0 for no TCP keepalive, otherwise apply this keepalive
559 	 * timeout to all libwebsocket sockets, client or server */
560 	int ka_probes;
561 	/**< CONTEXT: if ka_time was nonzero, after the timeout expires how many
562 	 * times to try to get a response from the peer before giving up
563 	 * and killing the connection */
564 	int ka_interval;
565 	/**< CONTEXT: if ka_time was nonzero, how long to wait before each ka_probes
566 	 * attempt */
567 	unsigned int timeout_secs;
568 	/**< VHOST: various processes involving network roundtrips in the
569 	 * library are protected from hanging forever by timeouts.  If
570 	 * nonzero, this member lets you set the timeout used in seconds.
571 	 * Otherwise a default timeout is used. */
572 	unsigned int connect_timeout_secs;
573 	/**< VHOST: client connections have this long to find a working server
574 	 * from the DNS results, or the whole connection times out.  If zero,
575 	 * a default timeout is used */
576 	int bind_iface;
577 	/**< VHOST: nonzero to strictly bind sockets to the interface name in
578 	 * .iface (eg, "eth2"), using SO_BIND_TO_DEVICE.
579 	 *
580 	 * Requires SO_BINDTODEVICE support from your OS and CAP_NET_RAW
581 	 * capability.
582 	 *
583 	 * Notice that common things like access network interface IP from
584 	 * your local machine use your lo / loopback interface and will be
585 	 * disallowed by this.
586 	 */
587 	unsigned int timeout_secs_ah_idle;
588 	/**< VHOST: seconds to allow a client to hold an ah without using it.
589 	 * 0 defaults to 10s. */
590 #endif /* WITH_NETWORK */
591 
592 #if defined(LWS_WITH_TLS_SESSIONS)
593 	uint32_t			tls_session_timeout;
594 	/**< VHOST: seconds until timeout/ttl for newly created sessions.
595 	 * 0 means default timeout (defined per protocol, usually 300s). */
596 	uint32_t			tls_session_cache_max;
597 	/**< VHOST: 0 for default limit of 10, or the maximum number of
598 	 * client tls sessions we are willing to cache */
599 #endif
600 
601 	gid_t gid;
602 	/**< CONTEXT: group id to change to after setting listen socket,
603 	 *   or -1. See also .username below. */
604 	uid_t uid;
605 	/**< CONTEXT: user id to change to after setting listen socket,
606 	 *   or -1.  See also .groupname below. */
607 	uint64_t options;
608 	/**< VHOST + CONTEXT: 0, or LWS_SERVER_OPTION_... bitfields */
609 	void *user;
610 	/**< VHOST + CONTEXT: optional user pointer that will be associated
611 	 * with the context when creating the context (and can be retrieved by
612 	 * lws_context_user(context), or with the vhost when creating the vhost
613 	 * (and can be retrieved by lws_vhost_user(vhost)).  You will need to
614 	 * use LWS_SERVER_OPTION_EXPLICIT_VHOSTS and create the vhost separately
615 	 * if you care about giving the context and vhost different user pointer
616 	 * values.
617 	 */
618 	unsigned int count_threads;
619 	/**< CONTEXT: how many contexts to create in an array, 0 = 1 */
620 	unsigned int fd_limit_per_thread;
621 	/**< CONTEXT: nonzero means restrict each service thread to this
622 	 * many fds, 0 means the default which is divide the process fd
623 	 * limit by the number of threads.
624 	 *
625 	 * Note if this is nonzero, and fd_limit_per_thread multiplied by the
626 	 * number of service threads is less than the process ulimit, then lws
627 	 * restricts internal lookup table allocation to the smaller size, and
628 	 * switches to a less efficient lookup scheme.  You should use this to
629 	 * trade off speed against memory usage if you know the lws context
630 	 * will only use a handful of fds.
631 	 *
632 	 * Bear in mind lws may use some fds internally, for example for the
633 	 * cancel pipe, so you may need to allow for some extras for normal
634 	 * operation.
635 	 */
636 	const char *vhost_name;
637 	/**< VHOST: name of vhost, must match external DNS name used to
638 	 * access the site, like "warmcat.com" as it's used to match
639 	 * Host: header and / or SNI name for SSL.
640 	 * CONTEXT: NULL, or the name to associate with the context for
641 	 * context-specific logging
642 	 */
643 #if defined(LWS_WITH_PLUGINS)
644 	const char * const *plugin_dirs;
645 	/**< CONTEXT: NULL, or NULL-terminated array of directories to
646 	 * scan for lws protocol plugins at context creation time */
647 #endif
648 	void *external_baggage_free_on_destroy;
649 	/**< CONTEXT: NULL, or pointer to something externally malloc'd, that
650 	 * should be freed when the context is destroyed.  This allows you to
651 	 * automatically sync the freeing action to the context destruction
652 	 * action, so there is no need for an external free() if the context
653 	 * succeeded to create.
654 	 */
655 
656 
657 	unsigned int pt_serv_buf_size;
658 	/**< CONTEXT: 0 = default of 4096.  This buffer is used by
659 	 * various service related features including file serving, it
660 	 * defines the max chunk of file that can be sent at once.
661 	 * At the risk of lws having to buffer failed large sends, it
662 	 * can be increased to, eg, 128KiB to improve throughput. */
663 #if defined(LWS_WITH_FILE_OPS)
664 	const struct lws_plat_file_ops *fops;
665 	/**< CONTEXT: NULL, or pointer to an array of fops structs, terminated
666 	 * by a sentinel with NULL .open.
667 	 *
668 	 * If NULL, lws provides just the platform file operations struct for
669 	 * backwards compatibility.
670 	 */
671 #endif
672 
673 #if defined(LWS_WITH_SOCKS5)
674 	const char *socks_proxy_address;
675 	/**< VHOST: If non-NULL, attempts to proxy via the given address.
676 	 * If proxy auth is required, use format
677 	 * "username:password\@server:port" */
678 	unsigned int socks_proxy_port;
679 	/**< VHOST: If socks_proxy_address was non-NULL, uses this port
680 	 * if nonzero, otherwise requires "server:port" in .socks_proxy_address
681 	 */
682 #endif
683 
684 #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
685 	cap_value_t caps[4];
686 	/**< CONTEXT: array holding Linux capabilities you want to
687 	 * continue to be available to the server after it transitions
688 	 * to a noprivileged user.  Usually none are needed but for, eg,
689 	 * .bind_iface, CAP_NET_RAW is required.  This gives you a way
690 	 * to still have the capability but drop root.
691 	 */
692 	char count_caps;
693 	/**< CONTEXT: count of Linux capabilities in .caps[].  0 means
694 	 * no capabilities will be inherited from root (the default) */
695 #endif
696 	void **foreign_loops;
697 	/**< CONTEXT: This is ignored if the context is not being started with
698 	 *		an event loop, ie, .options has a flag like
699 	 *		LWS_SERVER_OPTION_LIBUV.
700 	 *
701 	 *		NULL indicates lws should start its own even loop for
702 	 *		each service thread, and deal with closing the loops
703 	 *		when the context is destroyed.
704 	 *
705 	 *		Non-NULL means it points to an array of external
706 	 *		("foreign") event loops that are to be used in turn for
707 	 *		each service thread.  In the default case of 1 service
708 	 *		thread, it can just point to one foreign event loop.
709 	 */
710 	void (*signal_cb)(void *event_lib_handle, int signum);
711 	/**< CONTEXT: NULL: default signal handling.  Otherwise this receives
712 	 *		the signal handler callback.  event_lib_handle is the
713 	 *		native event library signal handle, eg uv_signal_t *
714 	 *		for libuv.
715 	 */
716 	struct lws_context **pcontext;
717 	/**< CONTEXT: if non-NULL, at the end of context destroy processing,
718 	 * the pointer pointed to by pcontext is written with NULL.  You can
719 	 * use this to let foreign event loops know that lws context destruction
720 	 * is fully completed.
721 	 */
722 	void (*finalize)(struct lws_vhost *vh, void *arg);
723 	/**< VHOST: NULL, or pointer to function that will be called back
724 	 *	    when the vhost is just about to be freed.  The arg parameter
725 	 *	    will be set to whatever finalize_arg is below.
726 	 */
727 	void *finalize_arg;
728 	/**< VHOST: opaque pointer lws ignores but passes to the finalize
729 	 *	    callback.  If you don't care, leave it NULL.
730 	 */
731 	const char *listen_accept_role;
732 	/**< VHOST: NULL for default, or force accepted incoming connections to
733 	 * bind to this role.  Uses the role names from their ops struct, eg,
734 	 * "raw-skt".
735 	 */
736 	const char *listen_accept_protocol;
737 	/**< VHOST: NULL for default, or force accepted incoming connections to
738 	 * bind to this vhost protocol name.
739 	 */
740 	const struct lws_protocols **pprotocols;
741 	/**< VHOST: NULL: use .protocols, otherwise ignore .protocols and use
742 	 * this array of pointers to protocols structs.  The end of the array
743 	 * is marked by a NULL pointer.
744 	 *
745 	 * This is preferred over .protocols, because it allows the protocol
746 	 * struct to be opaquely defined elsewhere, with just a pointer to it
747 	 * needed to create the context with it.  .protocols requires also
748 	 * the type of the user data to be known so its size can be given.
749 	 */
750 
751 	const char *username; /**< CONTEXT: string username for post-init
752 	 * permissions.  Like .uid but takes a string username. */
753 	const char *groupname; /**< CONTEXT: string groupname for post-init
754 	 * permissions.  Like .gid but takes a string groupname. */
755 	const char *unix_socket_perms; /**< VHOST: if your vhost is listening
756 	 * on a unix socket, you can give a "username:groupname" string here
757 	 * to control the owner:group it's created with.  It's always created
758 	 * with 0660 mode. */
759 	const lws_system_ops_t *system_ops;
760 	/**< CONTEXT: hook up lws_system_ apis to system-specific
761 	 * implementations */
762 	const lws_retry_bo_t *retry_and_idle_policy;
763 	/**< VHOST: optional retry and idle policy to apply to this vhost.
764 	 *   Currently only the idle parts are applied to the connections.
765 	 */
766 #if defined(LWS_WITH_SYS_STATE)
767 	lws_state_notify_link_t * const *register_notifier_list;
768 	/**< CONTEXT: NULL, or pointer to an array of notifiers that should
769 	 * be registered during context creation, so they can see state change
770 	 * events from very early on.  The array should end with a NULL. */
771 #endif
772 #if defined(LWS_WITH_SECURE_STREAMS)
773 #if defined(LWS_WITH_SECURE_STREAMS_STATIC_POLICY_ONLY)
774 	const struct lws_ss_policy *pss_policies; /**< CONTEXT: point to first
775 	 * in a linked-list of streamtype policies prepared by user code */
776 #else
777 	const char *pss_policies_json; /**< CONTEXT: point to a string
778 	 * containing a JSON description of the secure streams policies.  Set
779 	 * to NULL if not using Secure Streams.
780 	 * If the platform supports files and the string does not begin with
781 	 * '{', lws treats the string as a filepath to open to get the JSON
782 	 * policy.
783 	 */
784 #endif
785 	const struct lws_ss_plugin **pss_plugins; /**< CONTEXT: point to an array
786 	 * of pointers to plugin structs here, terminated with a NULL ptr.
787 	 * Set to NULL if not using Secure Streams. */
788 	const char *ss_proxy_bind; /**< CONTEXT: NULL, or: ss_proxy_port == 0:
789 	 * point to a string giving the Unix Domain Socket address to use (start
790 	 * with @ for abstract namespace), ss_proxy_port nonzero: set the
791 	 * network interface address (not name, it's ambiguous for ipv4/6) to
792 	 * bind the tcp connection to the proxy to */
793 	const char *ss_proxy_address; /**< CONTEXT: NULL, or if ss_proxy_port
794 	 * nonzero: the tcp address of the ss proxy to connect to */
795 	uint16_t ss_proxy_port; /* 0 = if connecting to ss proxy, do it via a
796 	 * Unix Domain Socket, "+@proxy.ss.lws" if ss_proxy_bind is NULL else
797 	 * the socket path given in ss_proxy_bind (start it with a + or +@);
798 	 * nonzero means connect via a tcp socket to the tcp address in
799 	 * ss_proxy_bind and the given port */
800 #endif
801 
802 	int rlimit_nofile;
803 	/**< 0 = inherit the initial ulimit for files / sockets from the startup
804 	 * environment.  Nonzero = try to set the limit for this process.
805 	 */
806 #if defined(LWS_WITH_PEER_LIMITS)
807 	lws_peer_limits_notify_t pl_notify_cb;
808 	/**< CONTEXT: NULL, or a callback to receive notifications each time a
809 	 * connection is being dropped because of peer limits.
810 	 *
811 	 * The callback provides the context, and an lws_sockaddr46 with the
812 	 * peer address and port.
813 	 */
814 	unsigned short ip_limit_ah;
815 	/**< CONTEXT: max number of ah a single IP may use simultaneously
816 	 *	      0 is no limit. This is a soft limit: if the limit is
817 	 *	      reached, connections from that IP will wait in the ah
818 	 *	      waiting list and not be able to acquire an ah until
819 	 *	      a connection belonging to the IP relinquishes one it
820 	 *	      already has.
821 	 */
822 	unsigned short ip_limit_wsi;
823 	/**< CONTEXT: max number of wsi a single IP may use simultaneously.
824 	 *	      0 is no limit.  This is a hard limit, connections from
825 	 *	      the same IP will simply be dropped once it acquires the
826 	 *	      amount of simultaneous wsi / accepted connections
827 	 *	      given here.
828 	 */
829 
830 #endif /* PEER_LIMITS */
831 
832 #if defined(LWS_WITH_SYS_FAULT_INJECTION)
833 	lws_fi_ctx_t				fic;
834 	/**< CONTEXT | VHOST: attach external Fault Injection context to the
835 	 * lws_context or vhost.  If creating the context + default vhost in
836 	 * one step, only the context binds to \p fi.  When creating a vhost
837 	 * otherwise this can bind to the vhost so the faults can be injected
838 	 * from the start.
839 	 */
840 #endif
841 
842 #if defined(LWS_WITH_SYS_SMD)
843 	lws_smd_notification_cb_t		early_smd_cb;
844 	/**< CONTEXT: NULL, or an smd notification callback that will be registered
845 	 * immediately after the smd in the context is initialized.  This ensures
846 	 * you can get all notifications without having to intercept the event loop
847 	 * creation, eg, when using an event library.  Other callbacks can be
848 	 * registered later manually without problems.
849 	 */
850 	void					*early_smd_opaque;
851 	lws_smd_class_t				early_smd_class_filter;
852 	lws_usec_t				smd_ttl_us;
853 	/**< CONTEXT: SMD messages older than this many us are removed from the
854 	 * queue and destroyed even if not fully delivered yet.  If zero,
855 	 * defaults to 2 seconds (5 second for FREERTOS).
856 	 */
857 	uint16_t				smd_queue_depth;
858 	/**< CONTEXT: Maximum queue depth, If zero defaults to 40
859 	 * (20 for FREERTOS) */
860 #endif
861 
862 #if defined(LWS_WITH_SYS_METRICS)
863 	const struct lws_metric_policy		*metrics_policies;
864 	/**< CONTEXT: non-SS policy metrics policies */
865 	const char				*metrics_prefix;
866 	/**< CONTEXT: prefix for this context's metrics, used to distinguish
867 	 * metrics pooled from different processes / applications, so, eg what
868 	 * would be "cpu.svc" if this is NULL becomes "myapp.cpu.svc" is this is
869 	 * set to "myapp".  Policies are applied using the name with the prefix,
870 	 * if present.
871 	 */
872 #endif
873 
874 	int					fo_listen_queue;
875 	/**< VHOST: 0 = no TCP_FASTOPEN, nonzero = enable TCP_FASTOPEN if the
876 	 * platform supports it, with the given queue length for the listen
877 	 * socket.
878 	 */
879 
880 	const struct lws_plugin_evlib		*event_lib_custom;
881 	/**< CONTEXT: If non-NULL, override event library selection so it uses
882 	 * this custom event library implementation, instead of default internal
883 	 * loop.  Don't set any other event lib context creation flags in that
884 	 * case. it will be used automatically.  This is useful for integration
885 	 * where an existing application is using its own handrolled event loop
886 	 * instead of an event library, it provides a way to allow lws to use
887 	 * the custom event loop natively as if it were an "event library".
888 	 */
889 
890 #if defined(LWS_WITH_TLS_JIT_TRUST)
891 	size_t					jitt_cache_max_footprint;
892 	/**< CONTEXT: 0 for no limit, else max bytes used by JIT Trust cache...
893 	 * LRU items are evicted to keep under this limit */
894 	int					vh_idle_grace_ms;
895 	/**< CONTEXT: 0 for default of 5000ms, or number of ms JIT Trust vhosts
896 	 * are allowed to live without active connections using them. */
897 #endif
898 
899 	lws_log_cx_t				*log_cx;
900 	/**< CONTEXT: NULL to use the default, process-scope logging context,
901 	 * else a specific logging context to associate with this context */
902 
903 #if defined(LWS_WITH_CACHE_NSCOOKIEJAR) && defined(LWS_WITH_CLIENT)
904 	const char				*http_nsc_filepath;
905 	/**< CONTEXT: Filepath to use for http netscape cookiejar file */
906 
907 	size_t					http_nsc_heap_max_footprint;
908 	/**< CONTEXT: 0, or limit in bytes for heap usage of memory cookie
909 	 * cache */
910 	size_t					http_nsc_heap_max_items;
911 	/**< CONTEXT: 0, or the max number of items allowed in the cookie cache
912 	 * before destroying lru items to keep it under the limit */
913 	size_t					http_nsc_heap_max_payload;
914 	/**< CONTEXT: 0, or the maximum size of a single cookie we are able to
915 	 * handle */
916 #endif
917 
918 	/* Add new things just above here ---^
919 	 * This is part of the ABI, don't needlessly break compatibility
920 	 *
921 	 * The below is to ensure later library versions with new
922 	 * members added above will see 0 (default) even if the app
923 	 * was not built against the newer headers.
924 	 */
925 
926 	void *_unused[2]; /**< dummy */
927 };
928 
929 /**
930  * lws_create_context() - Create the websocket handler
931  * \param info:	pointer to struct with parameters
932  *
933  *	This function creates the listening socket (if serving) and takes care
934  *	of all initialization in one step.
935  *
936  *	If option LWS_SERVER_OPTION_EXPLICIT_VHOSTS is given, no vhost is
937  *	created; you're expected to create your own vhosts afterwards using
938  *	lws_create_vhost().  Otherwise a vhost named "default" is also created
939  *	using the information in the vhost-related members, for compatibility.
940  *
941  *	After initialization, it returns a struct lws_context * that
942  *	represents this server.  After calling, user code needs to take care
943  *	of calling lws_service() with the context pointer to get the
944  *	server's sockets serviced.  This must be done in the same process
945  *	context as the initialization call.
946  *
947  *	The protocol callback functions are called for a handful of events
948  *	including http requests coming in, websocket connections becoming
949  *	established, and data arriving; it's also called periodically to allow
950  *	async transmission.
951  *
952  *	HTTP requests are sent always to the FIRST protocol in protocol, since
953  *	at that time websocket protocol has not been negotiated.  Other
954  *	protocols after the first one never see any HTTP callback activity.
955  *
956  *	The server created is a simple http server by default; part of the
957  *	websocket standard is upgrading this http connection to a websocket one.
958  *
959  *	This allows the same server to provide files like scripts and favicon /
960  *	images or whatever over http and dynamic data over websockets all in
961  *	one place; they're all handled in the user callback.
962  */
963 LWS_VISIBLE LWS_EXTERN struct lws_context *
964 lws_create_context(const struct lws_context_creation_info *info);
965 
966 
967 /**
968  * lws_context_destroy() - Destroy the websocket context
969  * \param context:	Websocket context
970  *
971  *	This function closes any active connections and then frees the
972  *	context.  After calling this, any further use of the context is
973  *	undefined.
974  */
975 LWS_VISIBLE LWS_EXTERN void
976 lws_context_destroy(struct lws_context *context);
977 
978 typedef int (*lws_reload_func)(void);
979 
980 /**
981  * lws_context_deprecate() - Deprecate the websocket context
982  *
983  * \param context:	Websocket context
984  * \param cb: Callback notified when old context listen sockets are closed
985  *
986  *	This function is used on an existing context before superceding it
987  *	with a new context.
988  *
989  *	It closes any listen sockets in the context, so new connections are
990  *	not possible.
991  *
992  *	And it marks the context to be deleted when the number of active
993  *	connections into it falls to zero.
994  *
995  *	This is aimed at allowing seamless configuration reloads.
996  *
997  *	The callback cb will be called after the listen sockets are actually
998  *	closed and may be reopened.  In the callback the new context should be
999  *	configured and created.  (With libuv, socket close happens async after
1000  *	more loop events).
1001  */
1002 LWS_VISIBLE LWS_EXTERN void
1003 lws_context_deprecate(struct lws_context *context, lws_reload_func cb);
1004 
1005 LWS_VISIBLE LWS_EXTERN int
1006 lws_context_is_deprecated(struct lws_context *context);
1007 
1008 /**
1009  * lws_set_proxy() - Setups proxy to lws_context.
1010  * \param vhost:	pointer to struct lws_vhost you want set proxy for
1011  * \param proxy: pointer to c string containing proxy in format address:port
1012  *
1013  * Returns 0 if proxy string was parsed and proxy was setup.
1014  * Returns -1 if proxy is NULL or has incorrect format.
1015  *
1016  * This is only required if your OS does not provide the http_proxy
1017  * environment variable (eg, OSX)
1018  *
1019  *   IMPORTANT! You should call this function right after creation of the
1020  *   lws_context and before call to connect. If you call this
1021  *   function after connect behavior is undefined.
1022  *   This function will override proxy settings made on lws_context
1023  *   creation with genenv() call.
1024  */
1025 LWS_VISIBLE LWS_EXTERN int
1026 lws_set_proxy(struct lws_vhost *vhost, const char *proxy);
1027 
1028 /**
1029  * lws_set_socks() - Setup socks to lws_context.
1030  * \param vhost:	pointer to struct lws_vhost you want set socks for
1031  * \param socks: pointer to c string containing socks in format address:port
1032  *
1033  * Returns 0 if socks string was parsed and socks was setup.
1034  * Returns -1 if socks is NULL or has incorrect format.
1035  *
1036  * This is only required if your OS does not provide the socks_proxy
1037  * environment variable (eg, OSX)
1038  *
1039  *   IMPORTANT! You should call this function right after creation of the
1040  *   lws_context and before call to connect. If you call this
1041  *   function after connect behavior is undefined.
1042  *   This function will override proxy settings made on lws_context
1043  *   creation with genenv() call.
1044  */
1045 LWS_VISIBLE LWS_EXTERN int
1046 lws_set_socks(struct lws_vhost *vhost, const char *socks);
1047 
1048 struct lws_vhost;
1049 
1050 /**
1051  * lws_create_vhost() - Create a vhost (virtual server context)
1052  * \param context:	pointer to result of lws_create_context()
1053  * \param info:		pointer to struct with parameters
1054  *
1055  * This function creates a virtual server (vhost) using the vhost-related
1056  * members of the info struct.  You can create many vhosts inside one context
1057  * if you created the context with the option LWS_SERVER_OPTION_EXPLICIT_VHOSTS
1058  */
1059 LWS_VISIBLE LWS_EXTERN struct lws_vhost *
1060 lws_create_vhost(struct lws_context *context,
1061 		 const struct lws_context_creation_info *info);
1062 
1063 /**
1064  * lws_vhost_destroy() - Destroy a vhost (virtual server context)
1065  *
1066  * \param vh:		pointer to result of lws_create_vhost()
1067  *
1068  * This function destroys a vhost.  Normally, if you just want to exit,
1069  * then lws_destroy_context() will take care of everything.  If you want
1070  * to destroy an individual vhost and all connections and allocations, you
1071  * can do it with this.
1072  *
1073  * If the vhost has a listen sockets shared by other vhosts, it will be given
1074  * to one of the vhosts sharing it rather than closed.
1075  *
1076  * The vhost close is staged according to the needs of the event loop, and if
1077  * there are multiple service threads.  At the point the vhost itself if
1078  * about to be freed, if you provided a finalize callback and optional arg at
1079  * vhost creation time, it will be called just before the vhost is freed.
1080  */
1081 LWS_VISIBLE LWS_EXTERN void
1082 lws_vhost_destroy(struct lws_vhost *vh);
1083 
1084 /**
1085  * lwsws_get_config_globals() - Parse a JSON server config file
1086  * \param info:		pointer to struct with parameters
1087  * \param d:		filepath of the config file
1088  * \param config_strings: storage for the config strings extracted from JSON,
1089  * 			  the pointer is incremented as strings are stored
1090  * \param len:		pointer to the remaining length left in config_strings
1091  *			  the value is decremented as strings are stored
1092  *
1093  * This function prepares a n lws_context_creation_info struct with global
1094  * settings from a file d.
1095  *
1096  * Requires CMake option LWS_WITH_LEJP_CONF to have been enabled
1097  */
1098 LWS_VISIBLE LWS_EXTERN int
1099 lwsws_get_config_globals(struct lws_context_creation_info *info, const char *d,
1100 			 char **config_strings, int *len);
1101 
1102 /**
1103  * lwsws_get_config_vhosts() - Create vhosts from a JSON server config file
1104  * \param context:	pointer to result of lws_create_context()
1105  * \param info:		pointer to struct with parameters
1106  * \param d:		filepath of the config file
1107  * \param config_strings: storage for the config strings extracted from JSON,
1108  * 			  the pointer is incremented as strings are stored
1109  * \param len:		pointer to the remaining length left in config_strings
1110  *			  the value is decremented as strings are stored
1111  *
1112  * This function creates vhosts into a context according to the settings in
1113  *JSON files found in directory d.
1114  *
1115  * Requires CMake option LWS_WITH_LEJP_CONF to have been enabled
1116  */
1117 LWS_VISIBLE LWS_EXTERN int
1118 lwsws_get_config_vhosts(struct lws_context *context,
1119 			struct lws_context_creation_info *info, const char *d,
1120 			char **config_strings, int *len);
1121 
1122 /**
1123  * lws_get_vhost() - return the vhost a wsi belongs to
1124  *
1125  * \param wsi: which connection
1126  */
1127 LWS_VISIBLE LWS_EXTERN struct lws_vhost *
1128 lws_get_vhost(struct lws *wsi);
1129 
1130 /**
1131  * lws_get_vhost_name() - returns the name of a vhost
1132  *
1133  * \param vhost: which vhost
1134  */
1135 LWS_VISIBLE LWS_EXTERN const char *
1136 lws_get_vhost_name(struct lws_vhost *vhost);
1137 
1138 /**
1139  * lws_get_vhost_by_name() - returns the vhost with the requested name, or NULL
1140  *
1141  * \param context: the lws_context to look in
1142  * \param name: vhost name we are looking for
1143  *
1144  * Returns NULL, or the vhost with the name \p name
1145  */
1146 LWS_VISIBLE LWS_EXTERN struct lws_vhost *
1147 lws_get_vhost_by_name(struct lws_context *context, const char *name);
1148 
1149 /**
1150  * lws_get_vhost_port() - returns the port a vhost listens on, or -1
1151  *
1152  * \param vhost: which vhost
1153  */
1154 LWS_VISIBLE LWS_EXTERN int
1155 lws_get_vhost_port(struct lws_vhost *vhost);
1156 
1157 /**
1158  * lws_get_vhost_user() - returns the user pointer for the vhost
1159  *
1160  * \param vhost: which vhost
1161  */
1162 LWS_VISIBLE LWS_EXTERN void *
1163 lws_get_vhost_user(struct lws_vhost *vhost);
1164 
1165 /**
1166  * lws_get_vhost_iface() - returns the binding for the vhost listen socket
1167  *
1168  * \param vhost: which vhost
1169  */
1170 LWS_VISIBLE LWS_EXTERN const char *
1171 lws_get_vhost_iface(struct lws_vhost *vhost);
1172 
1173 /**
1174  * lws_json_dump_vhost() - describe vhost state and stats in JSON
1175  *
1176  * \param vh: the vhost
1177  * \param buf: buffer to fill with JSON
1178  * \param len: max length of buf
1179  */
1180 LWS_VISIBLE LWS_EXTERN int
1181 lws_json_dump_vhost(const struct lws_vhost *vh, char *buf, int len);
1182 
1183 /**
1184  * lws_json_dump_context() - describe context state and stats in JSON
1185  *
1186  * \param context: the context
1187  * \param buf: buffer to fill with JSON
1188  * \param len: max length of buf
1189  * \param hide_vhosts: nonzero to not provide per-vhost mount etc information
1190  *
1191  * Generates a JSON description of vhost state into buf
1192  */
1193 LWS_VISIBLE LWS_EXTERN int
1194 lws_json_dump_context(const struct lws_context *context, char *buf, int len,
1195 		      int hide_vhosts);
1196 
1197 /**
1198  * lws_vhost_user() - get the user data associated with the vhost
1199  * \param vhost: Websocket vhost
1200  *
1201  * This returns the optional user pointer that can be attached to
1202  * a vhost when it was created.  Lws never dereferences this pointer, it only
1203  * sets it when the vhost is created, and returns it using this api.
1204  */
1205 LWS_VISIBLE LWS_EXTERN void *
1206 lws_vhost_user(struct lws_vhost *vhost);
1207 
1208 /**
1209  * lws_context_user() - get the user data associated with the context
1210  * \param context: Websocket context
1211  *
1212  * This returns the optional user allocation that can be attached to
1213  * the context the sockets live in at context_create time.  It's a way
1214  * to let all sockets serviced in the same context share data without
1215  * using globals statics in the user code.
1216  */
1217 LWS_VISIBLE LWS_EXTERN void *
1218 lws_context_user(struct lws_context *context);
1219 
1220 LWS_VISIBLE LWS_EXTERN const char *
1221 lws_vh_tag(struct lws_vhost *vh);
1222 
1223 /**
1224  * lws_context_is_being_destroyed() - find out if context is being destroyed
1225  *
1226  * \param context: the struct lws_context pointer
1227  *
1228  * Returns nonzero if the context has had lws_context_destroy() called on it...
1229  * when using event library loops the destroy process can be asynchronous.  In
1230  * the special case of libuv foreign loops, the failure to create the context
1231  * may have to do work on the foreign loop to reverse the partial creation,
1232  * meaning a failed context create cannot unpick what it did and return NULL.
1233  *
1234  * In that condition, a valid context that is already started the destroy
1235  * process is returned, and this test api will return nonzero as a way to
1236  * find out the create is in the middle of failing.
1237  */
1238 LWS_VISIBLE LWS_EXTERN int
1239 lws_context_is_being_destroyed(struct lws_context *context);
1240 
1241 /*! \defgroup vhost-mounts Vhost mounts and options
1242  * \ingroup context-and-vhost-creation
1243  *
1244  * ##Vhost mounts and options
1245  */
1246 ///@{
1247 /** struct lws_protocol_vhost_options - linked list of per-vhost protocol
1248  * 					name=value options
1249  *
1250  * This provides a general way to attach a linked-list of name=value pairs,
1251  * which can also have an optional child link-list using the options member.
1252  */
1253 struct lws_protocol_vhost_options {
1254 	const struct lws_protocol_vhost_options *next; /**< linked list */
1255 	const struct lws_protocol_vhost_options *options; /**< child linked-list of more options for this node */
1256 	const char *name; /**< name of name=value pair */
1257 	const char *value; /**< value of name=value pair */
1258 };
1259 
1260 /** enum lws_mount_protocols
1261  * This specifies the mount protocol for a mountpoint, whether it is to be
1262  * served from a filesystem, or it is a cgi etc.
1263  */
1264 enum lws_mount_protocols {
1265 	LWSMPRO_HTTP		= 0, /**< http reverse proxy */
1266 	LWSMPRO_HTTPS		= 1, /**< https reverse proxy */
1267 	LWSMPRO_FILE		= 2, /**< serve from filesystem directory */
1268 	LWSMPRO_CGI		= 3, /**< pass to CGI to handle */
1269 	LWSMPRO_REDIR_HTTP	= 4, /**< redirect to http:// url */
1270 	LWSMPRO_REDIR_HTTPS	= 5, /**< redirect to https:// url */
1271 	LWSMPRO_CALLBACK	= 6, /**< hand by named protocol's callback */
1272 };
1273 
1274 /** enum lws_authentication_mode
1275  * This specifies the authentication mode of the mount. The basic_auth_login_file mount parameter
1276  * is ignored unless LWSAUTHM_DEFAULT is set.
1277  */
1278 enum lws_authentication_mode {
1279 	LWSAUTHM_DEFAULT = 0, /**< default authenticate only if basic_auth_login_file is provided */
1280 	LWSAUTHM_BASIC_AUTH_CALLBACK = 1 << 28 /**< Basic auth with a custom verifier */
1281 };
1282 
1283 /** The authentication mode is stored in the top 4 bits of lws_http_mount.auth_mask */
1284 #define AUTH_MODE_MASK 0xF0000000
1285 
1286 /** struct lws_http_mount
1287  *
1288  * arguments for mounting something in a vhost's url namespace
1289  */
1290 struct lws_http_mount {
1291 	const struct lws_http_mount *mount_next;
1292 	/**< pointer to next struct lws_http_mount */
1293 	const char *mountpoint;
1294 	/**< mountpoint in http pathspace, eg, "/" */
1295 	const char *origin;
1296 	/**< path to be mounted, eg, "/var/www/warmcat.com" */
1297 	const char *def;
1298 	/**< default target, eg, "index.html" */
1299 	const char *protocol;
1300 	/**<"protocol-name" to handle mount */
1301 
1302 	const struct lws_protocol_vhost_options *cgienv;
1303 	/**< optional linked-list of cgi options.  These are created
1304 	 * as environment variables for the cgi process
1305 	 */
1306 	const struct lws_protocol_vhost_options *extra_mimetypes;
1307 	/**< optional linked-list of mimetype mappings */
1308 	const struct lws_protocol_vhost_options *interpret;
1309 	/**< optional linked-list of files to be interpreted */
1310 
1311 	int cgi_timeout;
1312 	/**< seconds cgi is allowed to live, if cgi://mount type */
1313 	int cache_max_age;
1314 	/**< max-age for reuse of client cache of files, seconds */
1315 	unsigned int auth_mask;
1316 	/**< bits set here must be set for authorized client session */
1317 
1318 	unsigned int cache_reusable:1; /**< set if client cache may reuse this */
1319 	unsigned int cache_revalidate:1; /**< set if client cache should revalidate on use */
1320 	unsigned int cache_intermediaries:1; /**< set if intermediaries are allowed to cache */
1321 
1322 	unsigned char origin_protocol; /**< one of enum lws_mount_protocols */
1323 	unsigned char mountpoint_len; /**< length of mountpoint string */
1324 
1325 	const char *basic_auth_login_file;
1326 	/**<NULL, or filepath to use to check basic auth logins against. (requires LWSAUTHM_DEFAULT) */
1327 
1328 	/* Add new things just above here ---^
1329 	 * This is part of the ABI, don't needlessly break compatibility
1330 	 */
1331 };
1332 
1333 ///@}
1334 ///@}
1335