1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2019 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 #if !defined(__LWS_CORE_NET_PRIVATE_H__)
26 #define __LWS_CORE_NET_PRIVATE_H__
27 
28 #if !defined(_POSIX_C_SOURCE)
29 #define _POSIX_C_SOURCE 200112L
30 #endif
31 
32 /*
33  * Generic pieces needed to manage muxable stream protocols like h2
34  */
35 
36 struct lws_muxable {
37 	struct lws	*parent_wsi;
38 	struct lws	*child_list;
39 	struct lws	*sibling_list;
40 
41 	unsigned int	my_sid;
42 	unsigned int	child_count;
43 
44 	uint32_t	highest_sid;
45 
46 	uint8_t		requested_POLLOUT;
47 };
48 
49 #include "private-lib-roles.h"
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
55 #define __lws_sul_insert_us(owner, sul, _us) \
56 		(sul)->us = lws_now_usecs() + (lws_usec_t)(_us); \
57 		__lws_sul_insert(owner, sul)
58 
59 
60 /*
61  *
62  *  ------ roles ------
63  *
64  */
65 
66 /* null-terminated array of pointers to roles lws built with */
67 extern const struct lws_role_ops *available_roles[];
68 
69 #define LWS_FOR_EVERY_AVAILABLE_ROLE_START(xx) { \
70 		const struct lws_role_ops **ppxx = available_roles; \
71 		while (*ppxx) { \
72 			const struct lws_role_ops *xx = *ppxx++;
73 
74 #define LWS_FOR_EVERY_AVAILABLE_ROLE_END }}
75 
76 /*
77  *
78  *  ------ event_loop ops ------
79  *
80  */
81 
82 /* enums of socks version */
83 enum socks_version {
84 	SOCKS_VERSION_4 = 4,
85 	SOCKS_VERSION_5 = 5
86 };
87 
88 /* enums of subnegotiation version */
89 enum socks_subnegotiation_version {
90 	SOCKS_SUBNEGOTIATION_VERSION_1 = 1,
91 };
92 
93 /* enums of socks commands */
94 enum socks_command {
95 	SOCKS_COMMAND_CONNECT = 1,
96 	SOCKS_COMMAND_BIND = 2,
97 	SOCKS_COMMAND_UDP_ASSOCIATE = 3
98 };
99 
100 /* enums of socks address type */
101 enum socks_atyp {
102 	SOCKS_ATYP_IPV4 = 1,
103 	SOCKS_ATYP_DOMAINNAME = 3,
104 	SOCKS_ATYP_IPV6 = 4
105 };
106 
107 /* enums of socks authentication methods */
108 enum socks_auth_method {
109 	SOCKS_AUTH_NO_AUTH = 0,
110 	SOCKS_AUTH_GSSAPI = 1,
111 	SOCKS_AUTH_USERNAME_PASSWORD = 2
112 };
113 
114 /* enums of subnegotiation status */
115 enum socks_subnegotiation_status {
116 	SOCKS_SUBNEGOTIATION_STATUS_SUCCESS = 0,
117 };
118 
119 /* enums of socks request reply */
120 enum socks_request_reply {
121 	SOCKS_REQUEST_REPLY_SUCCESS = 0,
122 	SOCKS_REQUEST_REPLY_FAILURE_GENERAL = 1,
123 	SOCKS_REQUEST_REPLY_CONNECTION_NOT_ALLOWED = 2,
124 	SOCKS_REQUEST_REPLY_NETWORK_UNREACHABLE = 3,
125 	SOCKS_REQUEST_REPLY_HOST_UNREACHABLE = 4,
126 	SOCKS_REQUEST_REPLY_CONNECTION_REFUSED = 5,
127 	SOCKS_REQUEST_REPLY_TTL_EXPIRED = 6,
128 	SOCKS_REQUEST_REPLY_COMMAND_NOT_SUPPORTED = 7,
129 	SOCKS_REQUEST_REPLY_ATYP_NOT_SUPPORTED = 8
130 };
131 
132 /* enums used to generate socks messages */
133 enum socks_msg_type {
134 	/* greeting */
135 	SOCKS_MSG_GREETING,
136 	/* credential, user name and password */
137 	SOCKS_MSG_USERNAME_PASSWORD,
138 	/* connect command */
139 	SOCKS_MSG_CONNECT
140 };
141 
142 enum {
143 	LWS_RXFLOW_ALLOW = (1 << 0),
144 	LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
145 };
146 
147 typedef enum lws_parser_return {
148 	LPR_FORBIDDEN	= -2,
149 	LPR_FAIL	= -1,
150 	LPR_OK		= 0,
151 	LPR_DO_FALLBACK = 2,
152 } lws_parser_return_t;
153 
154 enum pmd_return {
155 	PMDR_UNKNOWN,
156 	PMDR_DID_NOTHING,
157 	PMDR_HAS_PENDING,
158 	PMDR_EMPTY_NONFINAL,
159 	PMDR_EMPTY_FINAL,
160 	PMDR_NOTHING_WE_SHOULD_DO,
161 
162 	PMDR_FAILED = -1
163 };
164 
165 #if defined(LWS_WITH_PEER_LIMITS)
166 struct lws_peer {
167 	struct lws_peer *next;
168 	struct lws_peer *peer_wait_list;
169 
170 	lws_sockaddr46	sa46;
171 
172 	time_t time_created;
173 	time_t time_closed_all;
174 
175 	uint32_t hash;
176 	uint32_t count_wsi;
177 	uint32_t total_wsi;
178 
179 #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
180 	struct lws_peer_role_http http;
181 #endif
182 };
183 #endif
184 
185 enum {
186 	LWS_EV_READ = (1 << 0),
187 	LWS_EV_WRITE = (1 << 1),
188 	LWS_EV_START = (1 << 2),
189 	LWS_EV_STOP = (1 << 3),
190 
191 	LWS_EV_PREPARE_DELETION = (1u << 31),
192 };
193 
194 #ifdef LWS_WITH_IPV6
195 #define LWS_IPV6_ENABLED(vh) \
196 	(!lws_check_opt(vh->context->options, LWS_SERVER_OPTION_DISABLE_IPV6) && \
197 	 !lws_check_opt(vh->options, LWS_SERVER_OPTION_DISABLE_IPV6))
198 #else
199 #define LWS_IPV6_ENABLED(context) (0)
200 #endif
201 
202 #ifdef LWS_WITH_UNIX_SOCK
203 #define LWS_UNIX_SOCK_ENABLED(vhost) \
204 	(vhost->options & LWS_SERVER_OPTION_UNIX_SOCK)
205 #else
206 #define LWS_UNIX_SOCK_ENABLED(vhost) (0)
207 #endif
208 
209 enum uri_path_states {
210 	URIPS_IDLE,
211 	URIPS_SEEN_SLASH,
212 	URIPS_SEEN_SLASH_DOT,
213 	URIPS_SEEN_SLASH_DOT_DOT,
214 };
215 
216 enum uri_esc_states {
217 	URIES_IDLE,
218 	URIES_SEEN_PERCENT,
219 	URIES_SEEN_PERCENT_H1,
220 };
221 
222 #if defined(LWS_WITH_CLIENT)
223 
224 enum {
225 	CIS_ADDRESS,
226 	CIS_PATH,
227 	CIS_HOST,
228 	CIS_ORIGIN,
229 	CIS_PROTOCOL,
230 	CIS_METHOD,
231 	CIS_IFACE,
232 	CIS_ALPN,
233 
234 
235 	CIS_COUNT
236 };
237 
238 struct client_info_stash {
239 	char *cis[CIS_COUNT];
240 	void *opaque_user_data; /* not allocated or freed by lws */
241 };
242 #endif
243 
244 #if defined(LWS_WITH_UDP)
245 #define lws_wsi_is_udp(___wsi) (!!___wsi->udp)
246 #endif
247 
248 #define LWS_H2_FRAME_HEADER_LENGTH 9
249 
250 
251 lws_usec_t
252 __lws_sul_service_ripe(lws_dll2_owner_t *own, int num_own, lws_usec_t usnow);
253 
254 #if defined(LWS_WITH_DEPRECATED_THINGS)
255 
256 struct lws_timed_vh_protocol {
257 	struct lws_timed_vh_protocol	*next;
258 	lws_sorted_usec_list_t		sul;
259 	const struct lws_protocols	*protocol;
260 	struct lws_vhost *vhost; /* only used for pending processing */
261 	int				reason;
262 	int				tsi_req;
263 };
264 
265 #endif
266 
267 /*
268  * lws_async_dns
269  */
270 
271 typedef struct lws_async_dns {
272 	lws_sockaddr46 		sa46; /* nameserver */
273 	lws_dll2_owner_t	waiting;
274 	lws_dll2_owner_t	cached;
275 	struct lws		*wsi;
276 	time_t			time_set_server;
277 	uint8_t			dns_server_set:1;
278 	uint8_t			dns_server_connected:1;
279 } lws_async_dns_t;
280 
281 typedef enum {
282 	LADNS_CONF_SERVER_UNKNOWN				= -1,
283 	LADNS_CONF_SERVER_SAME,
284 	LADNS_CONF_SERVER_CHANGED
285 } lws_async_dns_server_check_t;
286 
287 #if defined(LWS_WITH_SYS_ASYNC_DNS)
288 void
289 lws_aysnc_dns_completed(struct lws *wsi, void *sa, size_t salen,
290 			lws_async_dns_retcode_t ret);
291 #endif
292 void
293 lws_async_dns_cancel(struct lws *wsi);
294 
295 void
296 lws_async_dns_drop_server(struct lws_context *context);
297 
298 /*
299  * so we can have n connections being serviced simultaneously,
300  * these things need to be isolated per-thread.
301  */
302 
303 struct lws_context_per_thread {
304 #if LWS_MAX_SMP > 1
305 	pthread_mutex_t lock_stats;
306 	struct lws_mutex_refcount mr;
307 	pthread_t self;
308 #endif
309 	struct lws_dll2_owner dll_buflist_owner;  /* guys with pending rxflow */
310 	struct lws_dll2_owner seq_owner;	   /* list of lws_sequencer-s */
311 	lws_dll2_owner_t      attach_owner;	/* pending lws_attach */
312 
313 #if defined(LWS_WITH_SECURE_STREAMS)
314 	lws_dll2_owner_t ss_owner;
315 #endif
316 #if defined(LWS_WITH_SECURE_STREAMS_PROXY_API) || \
317     defined(LWS_WITH_SECURE_STREAMS_THREAD_API)
318 	lws_dll2_owner_t ss_dsh_owner;
319 	lws_dll2_owner_t ss_client_owner;
320 #endif
321 
322 	struct lws_dll2_owner pt_sul_owner[LWS_COUNT_PT_SUL_OWNERS];
323 
324 #if defined (LWS_WITH_SEQUENCER)
325 	lws_sorted_usec_list_t sul_seq_heartbeat;
326 #endif
327 #if (defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)) && defined(LWS_WITH_SERVER)
328 	lws_sorted_usec_list_t sul_ah_lifecheck;
329 #endif
330 #if defined(LWS_WITH_TLS) && defined(LWS_WITH_SERVER)
331 	lws_sorted_usec_list_t sul_tls;
332 #endif
333 #if defined(LWS_PLAT_UNIX)
334 	lws_sorted_usec_list_t sul_plat;
335 #endif
336 #if defined(LWS_ROLE_CGI)
337 	lws_sorted_usec_list_t sul_cgi;
338 #endif
339 #if defined(LWS_WITH_PEER_LIMITS)
340 	lws_sorted_usec_list_t sul_peer_limits;
341 #endif
342 
343 #if !defined(LWS_PLAT_FREERTOS)
344 	struct lws *fake_wsi;   /* used for callbacks where there's no wsi */
345 #endif
346 
347 #if defined(WIN32)
348 	struct sockaddr_in frt_pipe_si;
349 #endif
350 
351 #if defined(LWS_WITH_TLS)
352 	struct lws_pt_tls tls;
353 #endif
354 	struct lws_context *context;
355 
356 	/*
357 	 * usable by anything in the service code, but only if the scope
358 	 * does not last longer than the service action (since next service
359 	 * of any socket can likewise use it and overwrite)
360 	 */
361 	unsigned char *serv_buf;
362 
363 	struct lws_pollfd *fds;
364 	volatile struct lws_foreign_thread_pollfd * volatile foreign_pfd_list;
365 
366 	lws_sockfd_type dummy_pipe_fds[2];
367 	struct lws *pipe_wsi;
368 
369 	/* --- role based members --- */
370 
371 #if defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)
372 	struct lws_pt_role_ws ws;
373 #endif
374 #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
375 	struct lws_pt_role_http http;
376 #endif
377 #if defined(LWS_ROLE_DBUS)
378 	struct lws_pt_role_dbus dbus;
379 #endif
380 	/* --- event library based members --- */
381 
382 #if defined(LWS_WITH_EVENT_LIBS)
383 	void		*evlib_pt; /* overallocated */
384 #endif
385 
386 	/* --- */
387 
388 	unsigned long count_conns;
389 	unsigned int fds_count;
390 
391 	/*
392 	 * set to the Thread ID that's doing the service loop just before entry
393 	 * to poll indicates service thread likely idling in poll()
394 	 * volatile because other threads may check it as part of processing
395 	 * for pollfd event change.
396 	 */
397 	volatile int service_tid;
398 	int service_tid_detected;
399 #if !defined(LWS_PLAT_FREERTOS)
400 	int count_event_loop_static_asset_handles;
401 #endif
402 
403 	volatile unsigned char inside_poll;
404 	volatile unsigned char foreign_spinlock;
405 
406 	unsigned char tid;
407 
408 	unsigned char inside_service:1;
409 	unsigned char inside_lws_service:1;
410 	unsigned char event_loop_foreign:1;
411 	unsigned char event_loop_destroy_processing_done:1;
412 	unsigned char event_loop_pt_unused:1;
413 	unsigned char destroy_self:1;
414 	unsigned char is_destroyed:1;
415 };
416 
417 /*
418  * virtual host -related context information
419  *   vhostwide SSL context
420  *   vhostwide proxy
421  *
422  * hierarchy:
423  *
424  * context -> vhost -> wsi
425  *
426  * incoming connection non-SSL vhost binding:
427  *
428  *    listen socket -> wsi -> select vhost after first headers
429  *
430  * incoming connection SSL vhost binding:
431  *
432  *    SSL SNI -> wsi -> bind after SSL negotiation
433  */
434 
435 struct lws_vhost {
436 #if defined(LWS_WITH_CLIENT) && defined(LWS_CLIENT_HTTP_PROXYING)
437 	char proxy_basic_auth_token[128];
438 #endif
439 #if LWS_MAX_SMP > 1
440 	struct lws_mutex_refcount		mr;
441 	char					close_flow_vs_tsi[LWS_MAX_SMP];
442 #endif
443 
444 #if defined(LWS_ROLE_H2)
445 	struct lws_vhost_role_h2 h2;
446 #endif
447 #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
448 	struct lws_vhost_role_http http;
449 #endif
450 #if defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)
451 	struct lws_vhost_role_ws ws;
452 #endif
453 
454 	lws_lifecycle_t		lc;
455 	lws_dll2_t		vh_being_destroyed_list;
456 
457 #if defined(LWS_WITH_SOCKS5)
458 	char socks_proxy_address[128];
459 	char socks_user[96];
460 	char socks_password[96];
461 #endif
462 
463 #if defined(LWS_WITH_TLS_SESSIONS)
464 	lws_dll2_owner_t	tls_sessions; /* vh lock */
465 #endif
466 
467 #if defined(LWS_WITH_EVENT_LIBS)
468 	void		*evlib_vh; /* overallocated */
469 #endif
470 #if defined(LWS_WITH_SYS_METRICS)
471 	lws_metric_t	*mt_traffic_rx;
472 	lws_metric_t	*mt_traffic_tx;
473 #endif
474 
475 #if defined(LWS_WITH_SYS_FAULT_INJECTION)
476 	lws_fi_ctx_t				fic;
477 	/**< Fault Injection ctx for the vhost, hierarchy vhost->context */
478 #endif
479 
480 	uint64_t options;
481 
482 	struct lws_context *context;
483 	struct lws_vhost *vhost_next;
484 
485 	const lws_retry_bo_t *retry_policy;
486 
487 #if defined(LWS_WITH_SERVER) && defined(LWS_WITH_SECURE_STREAMS)
488 	lws_ss_handle_t		*ss_handle; /* ss handle for the server obj */
489 #endif
490 
491 	struct lws *lserv_wsi;
492 	const char *name;
493 	const char *iface;
494 	const char *listen_accept_role;
495 	const char *listen_accept_protocol;
496 	const char *unix_socket_perms;
497 
498 	void (*finalize)(struct lws_vhost *vh, void *arg);
499 	void *finalize_arg;
500 
501 	const struct lws_protocols *protocols;
502 	void **protocol_vh_privs;
503 	const struct lws_protocol_vhost_options *pvo;
504 	const struct lws_protocol_vhost_options *headers;
505 	struct lws_dll2_owner *same_vh_protocol_owner;
506 	struct lws_vhost *no_listener_vhost_list;
507 	struct lws_dll2_owner abstract_instances_owner;		/* vh lock */
508 
509 #if defined(LWS_WITH_CLIENT)
510 	struct lws_dll2_owner dll_cli_active_conns_owner;
511 #endif
512 	struct lws_dll2_owner vh_awaiting_socket_owner;
513 
514 #if defined(LWS_WITH_TLS)
515 	struct lws_vhost_tls tls;
516 #endif
517 
518 #if defined(LWS_WITH_DEPRECATED_THINGS)
519 	struct lws_timed_vh_protocol *timed_vh_protocol_list;
520 #endif
521 	void *user;
522 
523 	int listen_port;
524 #if !defined(LWS_PLAT_FREERTOS) && !defined(OPTEE_TA) && !defined(WIN32)
525 	int bind_iface;
526 #endif
527 
528 #if defined(LWS_WITH_SOCKS5)
529 	unsigned int socks_proxy_port;
530 #endif
531 	int count_protocols;
532 	int ka_time;
533 	int ka_probes;
534 	int ka_interval;
535 	int keepalive_timeout;
536 	int timeout_secs_ah_idle;
537 	int connect_timeout_secs;
538 
539 	int count_bound_wsi;
540 
541 #ifdef LWS_WITH_ACCESS_LOG
542 	int log_fd;
543 #endif
544 
545 #if defined(LWS_WITH_TLS_SESSIONS)
546 	uint32_t		tls_session_cache_max;
547 #endif
548 
549 #if defined(LWS_WITH_SECURE_STREAMS_STATIC_POLICY_ONLY)
550 	int8_t		ss_refcount;
551 	/**< refcount of number of ss connections with streamtypes using this
552 	 * trust store */
553 #endif
554 
555 	uint8_t allocated_vhost_protocols:1;
556 	uint8_t created_vhost_protocols:1;
557 	uint8_t being_destroyed:1;
558 	uint8_t from_ss_policy:1;
559 
560 	unsigned char default_protocol_index;
561 	unsigned char raw_protocol_index;
562 };
563 
564 void
565 __lws_vhost_destroy2(struct lws_vhost *vh);
566 
567 #define mux_to_wsi(_m) lws_container_of(_m, struct lws, mux)
568 
569 void
570 lws_wsi_mux_insert(struct lws *wsi, struct lws *parent_wsi, unsigned int sid);
571 int
572 lws_wsi_mux_mark_parents_needing_writeable(struct lws *wsi);
573 struct lws *
574 lws_wsi_mux_move_child_to_tail(struct lws **wsi2);
575 int
576 lws_wsi_mux_action_pending_writeable_reqs(struct lws *wsi);
577 
578 void
579 lws_wsi_mux_dump_children(struct lws *wsi);
580 
581 void
582 lws_wsi_mux_close_children(struct lws *wsi, int reason);
583 
584 void
585 lws_wsi_mux_sibling_disconnect(struct lws *wsi);
586 
587 void
588 lws_wsi_mux_dump_waiting_children(struct lws *wsi);
589 
590 int
591 lws_wsi_mux_apply_queue(struct lws *wsi);
592 
593 /*
594  * struct lws
595  */
596 
597 /*
598  * These pieces are very commonly used (via accessors) in user protocol handlers
599  * and have to be valid, even in the case no real wsi is available for the cb.
600  *
601  * We put all this category of pointers in there and compose it at the top of
602  * struct lws, so a dummy wsi providing these only needs to be this big, while
603  * still being castable for being a struct wsi *
604  */
605 
606 struct lws_a {
607 	struct lws_context		*context;
608 	struct lws_vhost		*vhost;
609 	const struct lws_protocols	*protocol;
610 	void				*opaque_user_data;
611 };
612 
613 /*
614  * For RTOS-class platforms, their code is relatively new, post-minimal examples
615  * and tend to not have legacy user protocol handler baggage touching unexpected
616  * things in fakewsi unconditionally... we can use an lws_a on the stack and
617  * don't need to define the rest of the wsi content, just cast it, this saves
618  * a wsi footprint in heap (typ 800 bytes nowadays even on RTOS).
619  *
620  * For other platforms that have been around for years and have thousands of
621  * different user protocol handler implementations, it's likely some of them
622  * will be touching the struct lws content unconditionally in the handler even
623  * when we are calling back with a non wsi-specific reason, and may react badly
624  * to it being garbage.  So continue to implement those as a full, zero-ed down
625  * prepared fakewsi on heap at context creation time.
626  */
627 
628 #if defined(LWS_PLAT_FREERTOS)
629 #define lws_fakewsi_def_plwsa(pt) struct lws_a lwsa, *plwsa = &lwsa
630 #else
631 #define lws_fakewsi_def_plwsa(pt) struct lws_a *plwsa = &(pt)->fake_wsi->a
632 #endif
633 /* since we reuse the pt version, also correct to zero down the lws_a part */
634 #define lws_fakewsi_prep_plwsa_ctx(_c) \
635 		memset(plwsa, 0, sizeof(*plwsa)); plwsa->context = _c
636 
637 struct lws {
638 
639 	struct lws_a			a;
640 
641 	/* structs */
642 
643 #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
644 	struct _lws_http_mode_related	http;
645 #endif
646 #if defined(LWS_ROLE_H2)
647 	struct _lws_h2_related		h2;
648 #endif
649 #if defined(LWS_ROLE_WS)
650 	struct _lws_websocket_related	*ws; /* allocated if we upgrade to ws */
651 #endif
652 #if defined(LWS_ROLE_DBUS)
653 	struct _lws_dbus_mode_related	dbus;
654 #endif
655 #if defined(LWS_ROLE_MQTT)
656 	struct _lws_mqtt_related	*mqtt;
657 #endif
658 
659 #if defined(LWS_ROLE_H2) || defined(LWS_ROLE_MQTT)
660 	struct lws_muxable		mux;
661 	struct lws_tx_credit		txc;
662 #endif
663 
664 	lws_lifecycle_t			lc;
665 
666 	/* lifetime members */
667 
668 #if defined(LWS_WITH_EVENT_LIBS)
669 	void				*evlib_wsi; /* overallocated */
670 #endif
671 
672 	lws_sorted_usec_list_t		sul_timeout;
673 	lws_sorted_usec_list_t		sul_hrtimer;
674 	lws_sorted_usec_list_t		sul_validity;
675 	lws_sorted_usec_list_t		sul_connect_timeout;
676 
677 	struct lws_dll2			dll_buflist; /* guys with pending rxflow */
678 	struct lws_dll2			same_vh_protocol;
679 	struct lws_dll2			vh_awaiting_socket;
680 #if defined(LWS_WITH_SYS_ASYNC_DNS)
681 	struct lws_dll2			adns; /* on adns list of guys to tell result */
682 	lws_async_dns_cb_t		adns_cb; /* callback with result */
683 #endif
684 #if defined(LWS_WITH_CLIENT)
685 	struct lws_dll2			dll_cli_active_conns;
686 	struct lws_dll2			dll2_cli_txn_queue;
687 	struct lws_dll2_owner		dll2_cli_txn_queue_owner;
688 
689 	/**< caliper is reused for tcp, tls and txn conn phases */
690 
691 	lws_dll2_t			speculative_list;
692 	lws_dll2_owner_t		speculative_connect_owner;
693 	/* wsis: additional connection candidates */
694 	lws_dll2_owner_t		dns_sorted_list;
695 	/* lws_dns_sort_t: dns results wrapped and sorted in a linked-list...
696 	 * deleted as they are tried, list empty == everything tried */
697 #endif
698 
699 #if defined(LWS_WITH_SYS_FAULT_INJECTION)
700 	lws_fi_ctx_t				fic;
701 	/**< Fault Injection ctx for the wsi, hierarchy wsi->vhost->context */
702 #endif
703 
704 #if defined(LWS_WITH_SYS_METRICS)
705 	lws_metrics_caliper_compose(cal_conn)
706 #endif
707 
708 	lws_sockaddr46			sa46_local;
709 	lws_sockaddr46			sa46_peer;
710 
711 	/* pointers */
712 
713 	struct lws			*parent; /* points to parent, if any */
714 	struct lws			*child_list; /* points to first child */
715 	struct lws			*sibling_list; /* subsequent children at same level */
716 	const struct lws_role_ops	*role_ops;
717 	struct lws_sequencer		*seq;	/* associated sequencer if any */
718 	const lws_retry_bo_t		*retry_policy;
719 
720 #if defined(LWS_WITH_THREADPOOL)
721 	lws_dll2_owner_t		tp_task_owner; /* struct lws_threadpool_task */
722 #endif
723 
724 #if defined(LWS_WITH_PEER_LIMITS)
725 	struct lws_peer			*peer;
726 #endif
727 
728 #if defined(LWS_WITH_UDP)
729 	struct lws_udp			*udp;
730 #endif
731 #if defined(LWS_WITH_CLIENT)
732 	struct client_info_stash	*stash;
733 	char				*cli_hostname_copy;
734 
735 #if defined(LWS_WITH_CONMON)
736 	struct lws_conmon		conmon;
737 	lws_usec_t			conmon_datum;
738 #endif
739 #endif /* WITH_CLIENT */
740 	void				*user_space;
741 	void				*opaque_parent_data;
742 
743 	struct lws_buflist		*buflist; /* input-side buflist */
744 	struct lws_buflist		*buflist_out; /* output-side buflist */
745 
746 #if defined(LWS_WITH_TLS)
747 	struct lws_lws_tls		tls;
748 #endif
749 
750 	lws_sock_file_fd_type		desc; /* .filefd / .sockfd */
751 
752 	lws_wsi_state_t			wsistate;
753 	lws_wsi_state_t			wsistate_pre_close;
754 
755 	/* ints */
756 #define LWS_NO_FDS_POS (-1)
757 	int				position_in_fds_table;
758 
759 #if defined(LWS_WITH_CLIENT)
760 	int				chunk_remaining;
761 	int				flags;
762 #endif
763 	unsigned int			cache_secs;
764 
765 	unsigned int			hdr_parsing_completed:1;
766 	unsigned int			mux_substream:1;
767 	unsigned int			upgraded_to_http2:1;
768 	unsigned int			mux_stream_immortal:1;
769 	unsigned int			h2_stream_carries_ws:1; /* immortal set as well */
770 	unsigned int			h2_stream_carries_sse:1; /* immortal set as well */
771 	unsigned int			h2_acked_settings:1;
772 	unsigned int			seen_nonpseudoheader:1;
773 	unsigned int			listener:1;
774 	unsigned int			pf_packet:1;
775 	unsigned int			do_broadcast:1;
776 	unsigned int			user_space_externally_allocated:1;
777 	unsigned int			socket_is_permanently_unusable:1;
778 	unsigned int			rxflow_change_to:2;
779 	unsigned int			conn_stat_done:1;
780 	unsigned int			cache_reuse:1;
781 	unsigned int			cache_revalidate:1;
782 	unsigned int			cache_intermediaries:1;
783 	unsigned int			favoured_pollin:1;
784 	unsigned int			sending_chunked:1;
785 	unsigned int			interpreting:1;
786 	unsigned int			already_did_cce:1;
787 	unsigned int			told_user_closed:1;
788 	unsigned int			told_event_loop_closed:1;
789 	unsigned int			waiting_to_send_close_frame:1;
790 	unsigned int			close_needs_ack:1;
791 	unsigned int			ipv6:1;
792 	unsigned int			parent_pending_cb_on_writable:1;
793 	unsigned int			cgi_stdout_zero_length:1;
794 	unsigned int			seen_zero_length_recv:1;
795 	unsigned int			rxflow_will_be_applied:1;
796 	unsigned int			event_pipe:1;
797 	unsigned int			handling_404:1;
798 	unsigned int			protocol_bind_balance:1;
799 	unsigned int			unix_skt:1;
800 	unsigned int			close_when_buffered_out_drained:1;
801 	unsigned int			h1_ws_proxied:1;
802 	unsigned int			proxied_ws_parent:1;
803 	unsigned int			do_bind:1;
804 	unsigned int			validity_hup:1;
805 	unsigned int			skip_fallback:1;
806 	unsigned int			file_desc:1;
807 	unsigned int			conn_validity_wakesuspend:1;
808 	unsigned int			dns_reachability:1;
809 
810 	unsigned int			could_have_pending:1; /* detect back-to-back writes */
811 	unsigned int			outer_will_close:1;
812 	unsigned int			shadow:1; /* we do not control fd lifecycle at all */
813 #if defined(LWS_WITH_SECURE_STREAMS)
814 	unsigned int			for_ss:1;
815 	unsigned int			bound_ss_proxy_conn:1;
816 	unsigned int			client_bound_sspc:1;
817 	unsigned int			client_proxy_onward:1;
818 #endif
819 	unsigned int                    tls_borrowed:1;
820 
821 #ifdef LWS_WITH_ACCESS_LOG
822 	unsigned int			access_log_pending:1;
823 #endif
824 #if defined(LWS_WITH_CLIENT)
825 	unsigned int			do_ws:1; /* whether we are doing http or ws flow */
826 	unsigned int			chunked:1; /* if the clientside connection is chunked */
827 	unsigned int			client_rx_avail:1;
828 	unsigned int			client_http_body_pending:1;
829 	unsigned int			transaction_from_pipeline_queue:1;
830 	unsigned int			keepalive_active:1;
831 	unsigned int			keepalive_rejected:1;
832 	unsigned int			redirected_to_get:1;
833 	unsigned int			client_pipeline:1;
834 	unsigned int			client_h2_alpn:1;
835 	unsigned int			client_mux_substream:1;
836 	unsigned int			client_mux_migrated:1;
837 	unsigned int			client_subsequent_mime_part:1;
838 	unsigned int                    client_no_follow_redirect:1;
839 	unsigned int                    client_suppress_CONNECTION_ERROR:1;
840 	/**< because the client connection creation api is still the parent of
841 	 * this activity, and will report the failure */
842 	unsigned int			tls_session_reused:1;
843 	unsigned int			perf_done:1;
844 #endif
845 
846 #ifdef _WIN32
847 	unsigned int sock_send_blocking:1;
848 #endif
849 
850 	uint16_t			ocport, c_port, conn_port;
851 	uint16_t			retry;
852 #if defined(LWS_WITH_CLIENT)
853 	uint16_t			keep_warm_secs;
854 #endif
855 
856 	/* chars */
857 
858 	char lws_rx_parse_state; /* enum lws_rx_parse_state */
859 	char rx_frame_type; /* enum lws_write_protocol */
860 	char pending_timeout; /* enum pending_timeout */
861 	char tsi; /* thread service index we belong to */
862 	char protocol_interpret_idx;
863 	char redirects;
864 	uint8_t rxflow_bitmap;
865 	uint8_t bound_vhost_index;
866 	uint8_t lsp_channel; /* which of stdin/out/err */
867 #ifdef LWS_WITH_CGI
868 	char hdr_state;
869 #endif
870 #if defined(LWS_WITH_CLIENT)
871 	char chunk_parser; /* enum lws_chunk_parser */
872 	uint8_t addrinfo_idx;
873 	uint8_t sys_tls_client_cert;
874 	uint8_t c_pri;
875 #endif
876 #if defined(LWS_WITH_CGI) || defined(LWS_WITH_CLIENT)
877 	char reason_bf; /* internal writeable callback reason bitfield */
878 #endif
879 #if defined(LWS_WITH_NETLINK)
880 	lws_route_uidx_t		peer_route_uidx;
881 	/**< unique index of the route the connection is estimated to take */
882 #endif
883 	uint8_t immortal_substream_count;
884 	/* volatile to make sure code is aware other thread can change */
885 	volatile char handling_pollout;
886 	volatile char leave_pollout_active;
887 #if LWS_MAX_SMP > 1
888 	volatile char undergoing_init_from_other_pt;
889 #endif
890 
891 };
892 
893 #define lws_is_flowcontrolled(w) (!!(wsi->rxflow_bitmap))
894 
895 #if defined(LWS_WITH_SPAWN)
896 
897 #if defined(WIN32) || defined(_WIN32)
898 #else
899 #include <sys/wait.h>
900 #include <sys/times.h>
901 #endif
902 
903 struct lws_spawn_piped {
904 
905 	struct lws_spawn_piped_info	info;
906 
907 	struct lws_dll2			dll;
908 	lws_sorted_usec_list_t		sul;
909 	lws_sorted_usec_list_t		sul_reap;
910 
911 	struct lws_context		*context;
912 	struct lws			*stdwsi[3];
913 	lws_filefd_type			pipe_fds[3][2];
914 	int				count_log_lines;
915 
916 	lws_usec_t			created; /* set by lws_spawn_piped() */
917 	lws_usec_t			reaped;
918 
919 	lws_usec_t			accounting[4];
920 
921 #if defined(WIN32)
922 	HANDLE				child_pid;
923 	lws_sorted_usec_list_t		sul_poll;
924 #else
925 	pid_t				child_pid;
926 
927 	siginfo_t			si;
928 #endif
929 	int				reap_retry_budget;
930 
931 	uint8_t				pipes_alive:2;
932 	uint8_t				we_killed_him_timeout:1;
933 	uint8_t				we_killed_him_spew:1;
934 	uint8_t				ungraceful:1;
935 };
936 
937 void
938 lws_spawn_piped_destroy(struct lws_spawn_piped **lsp);
939 
940 int
941 lws_spawn_reap(struct lws_spawn_piped *lsp);
942 
943 #endif
944 
945 void
946 lws_service_do_ripe_rxflow(struct lws_context_per_thread *pt);
947 
948 const struct lws_role_ops *
949 lws_role_by_name(const char *name);
950 
951 int
952 lws_socket_bind(struct lws_vhost *vhost, struct lws *wsi,
953 		lws_sockfd_type sockfd, int port, const char *iface,
954 		int ipv6_allowed);
955 
956 #if defined(LWS_WITH_IPV6)
957 unsigned long
958 lws_get_addr_scope(const char *ipaddr);
959 #endif
960 
961 void
962 lws_close_free_wsi(struct lws *wsi, enum lws_close_status, const char *caller);
963 void
964 __lws_close_free_wsi(struct lws *wsi, enum lws_close_status, const char *caller);
965 
966 void
967 __lws_free_wsi(struct lws *wsi);
968 
969 void
970 lws_conmon_addrinfo_destroy(struct addrinfo *ai);
971 
972 int
973 lws_conmon_append_copy_new_dns_results(struct lws *wsi,
974 				       const struct addrinfo *cai);
975 
976 #if LWS_MAX_SMP > 1
977 
978 static LWS_INLINE void
lws_pt_mutex_init(struct lws_context_per_thread * pt)979 lws_pt_mutex_init(struct lws_context_per_thread *pt)
980 {
981 	lws_mutex_refcount_init(&pt->mr);
982 	pthread_mutex_init(&pt->lock_stats, NULL);
983 }
984 
985 static LWS_INLINE void
lws_pt_mutex_destroy(struct lws_context_per_thread * pt)986 lws_pt_mutex_destroy(struct lws_context_per_thread *pt)
987 {
988 	pthread_mutex_destroy(&pt->lock_stats);
989 	lws_mutex_refcount_destroy(&pt->mr);
990 }
991 
992 #define lws_pt_lock(pt, reason) lws_mutex_refcount_lock(&pt->mr, reason)
993 #define lws_pt_unlock(pt) lws_mutex_refcount_unlock(&pt->mr)
994 #define lws_pt_assert_lock_held(pt) lws_mutex_refcount_assert_held(&pt->mr)
995 
996 static LWS_INLINE void
lws_pt_stats_lock(struct lws_context_per_thread * pt)997 lws_pt_stats_lock(struct lws_context_per_thread *pt)
998 {
999 	pthread_mutex_lock(&pt->lock_stats);
1000 }
1001 
1002 static LWS_INLINE void
lws_pt_stats_unlock(struct lws_context_per_thread * pt)1003 lws_pt_stats_unlock(struct lws_context_per_thread *pt)
1004 {
1005 	pthread_mutex_unlock(&pt->lock_stats);
1006 }
1007 #endif
1008 
1009 /*
1010  * EXTENSIONS
1011  */
1012 
1013 #if defined(LWS_WITHOUT_EXTENSIONS)
1014 #define lws_any_extension_handled(_a, _b, _c, _d) (0)
1015 #define lws_ext_cb_active(_a, _b, _c, _d) (0)
1016 #define lws_ext_cb_all_exts(_a, _b, _c, _d, _e) (0)
1017 #define lws_issue_raw_ext_access lws_issue_raw
1018 #define lws_context_init_extensions(_a, _b)
1019 #endif
1020 
1021 int LWS_WARN_UNUSED_RESULT
1022 lws_client_interpret_server_handshake(struct lws *wsi);
1023 
1024 int LWS_WARN_UNUSED_RESULT
1025 lws_ws_rx_sm(struct lws *wsi, char already_processed, unsigned char c);
1026 
1027 int LWS_WARN_UNUSED_RESULT
1028 lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);
1029 
1030 void
1031 lws_role_transition(struct lws *wsi, enum lwsi_role role, enum lwsi_state state,
1032 		    const struct lws_role_ops *ops);
1033 
1034 int
1035 lws_http_to_fallback(struct lws *wsi, unsigned char *buf, size_t len);
1036 
1037 int LWS_WARN_UNUSED_RESULT
1038 user_callback_handle_rxflow(lws_callback_function, struct lws *wsi,
1039 			    enum lws_callback_reasons reason, void *user,
1040 			    void *in, size_t len);
1041 
1042 int
1043 lws_plat_set_nonblocking(lws_sockfd_type fd);
1044 
1045 int
1046 lws_plat_set_socket_options(struct lws_vhost *vhost, lws_sockfd_type fd,
1047 			    int unix_skt);
1048 
1049 int
1050 lws_plat_set_socket_options_ip(lws_sockfd_type fd, uint8_t pri, int lws_flags);
1051 
1052 int
1053 lws_plat_check_connection_error(struct lws *wsi);
1054 
1055 int LWS_WARN_UNUSED_RESULT
1056 lws_header_table_attach(struct lws *wsi, int autoservice);
1057 
1058 int
1059 lws_header_table_detach(struct lws *wsi, int autoservice);
1060 int
1061 __lws_header_table_detach(struct lws *wsi, int autoservice);
1062 
1063 void
1064 lws_header_table_reset(struct lws *wsi, int autoservice);
1065 
1066 void
1067 __lws_header_table_reset(struct lws *wsi, int autoservice);
1068 
1069 char * LWS_WARN_UNUSED_RESULT
1070 lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h);
1071 
1072 int LWS_WARN_UNUSED_RESULT
1073 lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s);
1074 
1075 int LWS_WARN_UNUSED_RESULT
1076 lws_ensure_user_space(struct lws *wsi);
1077 
1078 int LWS_WARN_UNUSED_RESULT
1079 lws_change_pollfd(struct lws *wsi, int _and, int _or);
1080 
1081 #if defined(LWS_WITH_SERVER)
1082  int _lws_vhost_init_server(const struct lws_context_creation_info *info,
1083 			      struct lws_vhost *vhost);
1084 struct lws_vhost *
1085  lws_select_vhost(struct lws_context *context, int port, const char *servername);
1086 int LWS_WARN_UNUSED_RESULT
1087  lws_parse_ws(struct lws *wsi, unsigned char **buf, size_t len);
1088 void
1089  lws_server_get_canonical_hostname(struct lws_context *context,
1090 				   const struct lws_context_creation_info *info);
1091 #else
1092  #define _lws_vhost_init_server(_a, _b) (0)
1093  #define lws_parse_ws(_a, _b, _c) (0)
1094  #define lws_server_get_canonical_hostname(_a, _b)
1095 #endif
1096 
1097 int
1098 __remove_wsi_socket_from_fds(struct lws *wsi);
1099 
1100 enum {
1101 	LWSRXFC_ERROR = -1,
1102 	LWSRXFC_CACHED = 0,
1103 	LWSRXFC_ADDITIONAL = 1,
1104 	LWSRXFC_TRIMMED = 2,
1105 };
1106 
1107 
1108 int
1109 _lws_plat_service_forced_tsi(struct lws_context *context, int tsi);
1110 
1111 int
1112 lws_rxflow_cache(struct lws *wsi, unsigned char *buf, size_t n, size_t len);
1113 
1114 int
1115 lws_service_flag_pending(struct lws_context *context, int tsi);
1116 
1117 static LWS_INLINE int
lws_has_buffered_out(struct lws * wsi)1118 lws_has_buffered_out(struct lws *wsi) { return !!wsi->buflist_out; }
1119 
1120 int LWS_WARN_UNUSED_RESULT
1121 lws_ws_client_rx_sm(struct lws *wsi, unsigned char c);
1122 
1123 lws_parser_return_t LWS_WARN_UNUSED_RESULT
1124 lws_parse(struct lws *wsi, unsigned char *buf, int *len);
1125 
1126 int LWS_WARN_UNUSED_RESULT
1127 lws_parse_urldecode(struct lws *wsi, uint8_t *_c);
1128 
1129 void
1130 lws_sa46_copy_address(lws_sockaddr46 *sa46a, const void *in, int af);
1131 
1132 int LWS_WARN_UNUSED_RESULT
1133 lws_http_action(struct lws *wsi);
1134 
1135 void
1136 __lws_close_free_wsi_final(struct lws *wsi);
1137 void
1138 lws_libuv_closehandle(struct lws *wsi);
1139 int
1140 lws_libuv_check_watcher_active(struct lws *wsi);
1141 
1142 #if defined(LWS_WITH_EVLIB_PLUGINS) || defined(LWS_WITH_PLUGINS)
1143 const lws_plugin_header_t *
1144 lws_plat_dlopen(struct lws_plugin **pplugin, const char *libpath,
1145 		const char *sofilename, const char *_class,
1146 		each_plugin_cb_t each, void *each_user);
1147 
1148 int
1149 lws_plat_destroy_dl(struct lws_plugin *p);
1150 #endif
1151 
1152 struct lws *
1153 lws_adopt_socket_vhost(struct lws_vhost *vh, lws_sockfd_type accept_fd);
1154 
1155 void
1156 lws_vhost_bind_wsi(struct lws_vhost *vh, struct lws *wsi);
1157 void
1158 __lws_vhost_unbind_wsi(struct lws *wsi); /* req cx + vh lock */
1159 
1160 void
1161 __lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs);
1162 int
1163 __lws_change_pollfd(struct lws *wsi, int _and, int _or);
1164 
1165 
1166 int
1167 lws_callback_as_writeable(struct lws *wsi);
1168 
1169 int
1170 lws_role_call_client_bind(struct lws *wsi,
1171 			  const struct lws_client_connect_info *i);
1172 void
1173 lws_remove_child_from_any_parent(struct lws *wsi);
1174 
1175 char *
1176 lws_generate_client_ws_handshake(struct lws *wsi, char *p, const char *conn1);
1177 int
1178 lws_client_ws_upgrade(struct lws *wsi, const char **cce);
1179 int
1180 lws_create_client_ws_object(const struct lws_client_connect_info *i,
1181 			    struct lws *wsi);
1182 int
1183 lws_alpn_comma_to_openssl(const char *comma, uint8_t *os, int len);
1184 int
1185 lws_role_call_alpn_negotiated(struct lws *wsi, const char *alpn);
1186 int
1187 lws_tls_server_conn_alpn(struct lws *wsi);
1188 
1189 int
1190 lws_ws_client_rx_sm_block(struct lws *wsi, unsigned char **buf, size_t len);
1191 void
1192 lws_destroy_event_pipe(struct lws *wsi);
1193 
1194 /* socks */
1195 int
1196 lws_socks5c_generate_msg(struct lws *wsi, enum socks_msg_type type, ssize_t *msg_len);
1197 
1198 #if defined(LWS_WITH_DEPRECATED_THINGS)
1199 int
1200 __lws_timed_callback_remove(struct lws_vhost *vh, struct lws_timed_vh_protocol *p);
1201 #endif
1202 
1203 int LWS_WARN_UNUSED_RESULT
1204 __insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi);
1205 
1206 int LWS_WARN_UNUSED_RESULT
1207 lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
1208 
1209 lws_usec_t
1210 __lws_seq_timeout_check(struct lws_context_per_thread *pt, lws_usec_t usnow);
1211 
1212 lws_usec_t
1213 __lws_ss_timeout_check(struct lws_context_per_thread *pt, lws_usec_t usnow);
1214 
1215 struct lws * LWS_WARN_UNUSED_RESULT
1216 lws_client_connect_2_dnsreq(struct lws *wsi);
1217 
1218 LWS_VISIBLE struct lws * LWS_WARN_UNUSED_RESULT
1219 lws_client_reset(struct lws **wsi, int ssl, const char *address, int port,
1220 		 const char *path, const char *host, char weak);
1221 
1222 struct lws * LWS_WARN_UNUSED_RESULT
1223 lws_create_new_server_wsi(struct lws_vhost *vhost, int fixed_tsi, const char *desc);
1224 
1225 char * LWS_WARN_UNUSED_RESULT
1226 lws_generate_client_handshake(struct lws *wsi, char *pkt);
1227 
1228 int
1229 lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
1230 
1231 struct lws *
1232 lws_http_client_connect_via_info2(struct lws *wsi);
1233 
1234 
1235 struct lws *
1236 __lws_wsi_create_with_role(struct lws_context *context, int tsi,
1237 			 const struct lws_role_ops *ops);
1238 int
1239 lws_wsi_inject_to_loop(struct lws_context_per_thread *pt, struct lws *wsi);
1240 
1241 int
1242 lws_wsi_extract_from_loop(struct lws *wsi);
1243 
1244 
1245 #if defined(LWS_WITH_CLIENT)
1246 int
1247 lws_http_client_socket_service(struct lws *wsi, struct lws_pollfd *pollfd);
1248 
1249 int LWS_WARN_UNUSED_RESULT
1250 lws_http_transaction_completed_client(struct lws *wsi);
1251 #if !defined(LWS_WITH_TLS)
1252 	#define lws_context_init_client_ssl(_a, _b) (0)
1253 #endif
1254 void
1255 lws_decode_ssl_error(void);
1256 #else
1257 #define lws_context_init_client_ssl(_a, _b) (0)
1258 #endif
1259 
1260 int
1261 __lws_rx_flow_control(struct lws *wsi);
1262 
1263 int
1264 _lws_change_pollfd(struct lws *wsi, int _and, int _or, struct lws_pollargs *pa);
1265 
1266 #if defined(LWS_WITH_SERVER)
1267 int
1268 lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len);
1269 #else
1270 #define lws_server_socket_service(_b, _c) (0)
1271 #define lws_handshake_server(_a, _b, _c) (0)
1272 #endif
1273 
1274 #ifdef LWS_WITH_ACCESS_LOG
1275 int
1276 lws_access_log(struct lws *wsi);
1277 void
1278 lws_prepare_access_log_info(struct lws *wsi, char *uri_ptr, int len, int meth);
1279 #else
1280 #define lws_access_log(_a)
1281 #endif
1282 
1283 #if defined(_DEBUG)
1284 void
1285 lws_wsi_txc_describe(struct lws_tx_credit *txc, const char *at, uint32_t sid);
1286 #else
1287 #define lws_wsi_txc_describe(x, y, z) { (void)x; }
1288 #endif
1289 
1290 int
1291 lws_wsi_txc_check_skint(struct lws_tx_credit *txc, int32_t tx_cr);
1292 
1293 int
1294 lws_wsi_txc_report_manual_txcr_in(struct lws *wsi, int32_t bump);
1295 
1296 void
1297 lws_mux_mark_immortal(struct lws *wsi);
1298 void
1299 lws_http_close_immortal(struct lws *wsi);
1300 
1301 int
1302 lws_cgi_kill_terminated(struct lws_context_per_thread *pt);
1303 
1304 void
1305 lws_cgi_remove_and_kill(struct lws *wsi);
1306 
1307 void
1308 lws_plat_delete_socket_from_fds(struct lws_context *context,
1309 				struct lws *wsi, int m);
1310 void
1311 lws_plat_insert_socket_into_fds(struct lws_context *context,
1312 				struct lws *wsi);
1313 
1314 int
1315 lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
1316 		       struct lws_pollfd *pfd);
1317 
1318 #if defined(LWS_WITH_SERVER) && defined(LWS_WITH_SECURE_STREAMS)
1319 int
1320 lws_adopt_ss_server_accept(struct lws *new_wsi);
1321 #endif
1322 
1323 int
1324 lws_plat_pipe_create(struct lws *wsi);
1325 int
1326 lws_plat_pipe_signal(struct lws_context *ctx, int tsi);
1327 void
1328 lws_plat_pipe_close(struct lws *wsi);
1329 
1330 void
1331 lws_addrinfo_clean(struct lws *wsi);
1332 
1333 void
1334 lws_add_wsi_to_draining_ext_list(struct lws *wsi);
1335 void
1336 lws_remove_wsi_from_draining_ext_list(struct lws *wsi);
1337 int
1338 lws_poll_listen_fd(struct lws_pollfd *fd);
1339 int
1340 lws_plat_service(struct lws_context *context, int timeout_ms);
1341 LWS_VISIBLE int
1342 _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi);
1343 
1344 int
1345 lws_pthread_self_to_tsi(struct lws_context *context);
1346 const char * LWS_WARN_UNUSED_RESULT
1347 lws_plat_inet_ntop(int af, const void *src, char *dst, socklen_t cnt);
1348 int LWS_WARN_UNUSED_RESULT
1349 lws_plat_inet_pton(int af, const char *src, void *dst);
1350 
1351 void
1352 lws_same_vh_protocol_remove(struct lws *wsi);
1353 void
1354 __lws_same_vh_protocol_remove(struct lws *wsi);
1355 void
1356 lws_same_vh_protocol_insert(struct lws *wsi, int n);
1357 
1358 void
1359 lws_seq_destroy_all_on_pt(struct lws_context_per_thread *pt);
1360 
1361 void
1362 lws_addrinfo_clean(struct lws *wsi);
1363 
1364 int
1365 _lws_route_pt_close_unroutable(struct lws_context_per_thread *pt);
1366 
1367 void
1368 _lws_routing_entry_dump(lws_route_t *rou);
1369 
1370 void
1371 _lws_routing_table_dump(struct lws_context *cx);
1372 
1373 #define LRR_IGNORE_PRI			(1 << 0)
1374 #define LRR_MATCH_SRC			(1 << 1)
1375 #define LRR_JUST_CHECK			(1 << 2)
1376 
1377 lws_route_t *
1378 _lws_route_remove(struct lws_context_per_thread *pt, lws_route_t *robj, int flags);
1379 
1380 void
1381 _lws_route_table_empty(struct lws_context_per_thread *pt);
1382 
1383 void
1384 _lws_route_table_ifdown(struct lws_context_per_thread *pt, int idx);
1385 
1386 lws_route_uidx_t
1387 _lws_route_get_uidx(struct lws_context *cx);
1388 
1389 int
1390 _lws_route_pt_close_route_users(struct lws_context_per_thread *pt,
1391 			        lws_route_uidx_t uidx);
1392 
1393 lws_route_t *
1394 _lws_route_est_outgoing(struct lws_context_per_thread *pt,
1395 		        const lws_sockaddr46 *dest);
1396 
1397 int
1398 lws_sort_dns(struct lws *wsi, const struct addrinfo *result);
1399 
1400 int
1401 lws_broadcast(struct lws_context_per_thread *pt, int reason, void *in, size_t len);
1402 
1403 
1404 #if defined(LWS_WITH_PEER_LIMITS)
1405 void
1406 lws_peer_track_wsi_close(struct lws_context *context, struct lws_peer *peer);
1407 int
1408 lws_peer_confirm_ah_attach_ok(struct lws_context *context,
1409 			      struct lws_peer *peer);
1410 void
1411 lws_peer_track_ah_detach(struct lws_context *context, struct lws_peer *peer);
1412 void
1413 lws_peer_cull_peer_wait_list(struct lws_context *context);
1414 struct lws_peer *
1415 lws_get_or_create_peer(struct lws_vhost *vhost, lws_sockfd_type sockfd);
1416 void
1417 lws_peer_add_wsi(struct lws_context *context, struct lws_peer *peer,
1418 		 struct lws *wsi);
1419 void
1420 lws_peer_dump_from_wsi(struct lws *wsi);
1421 #endif
1422 
1423 #ifdef LWS_WITH_HUBBUB
1424 hubbub_error
1425 html_parser_cb(const hubbub_token *token, void *pw);
1426 #endif
1427 
1428 int
1429 lws_threadpool_tsi_context(struct lws_context *context, int tsi);
1430 
1431 void
1432 lws_threadpool_wsi_closing(struct lws *wsi);
1433 
1434 void
1435 __lws_wsi_remove_from_sul(struct lws *wsi);
1436 
1437 void
1438 lws_validity_confirmed(struct lws *wsi);
1439 void
1440 _lws_validity_confirmed_role(struct lws *wsi);
1441 
1442 int
1443 lws_seq_pt_init(struct lws_context_per_thread *pt);
1444 
1445 int
1446 lws_buflist_aware_read(struct lws_context_per_thread *pt, struct lws *wsi,
1447 		       struct lws_tokens *ebuf, char fr, const char *hint);
1448 int
1449 lws_buflist_aware_finished_consuming(struct lws *wsi, struct lws_tokens *ebuf,
1450 				     int used, int buffered, const char *hint);
1451 
1452 extern const struct lws_protocols protocol_abs_client_raw_skt,
1453 				  protocol_abs_client_unit_test;
1454 
1455 void
1456 __lws_reset_wsi(struct lws *wsi);
1457 
1458 void
1459 lws_metrics_dump(struct lws_context *ctx);
1460 
1461 void
1462 lws_inform_client_conn_fail(struct lws *wsi, void *arg, size_t len);
1463 
1464 #if defined(LWS_WITH_SYS_ASYNC_DNS)
1465 lws_async_dns_server_check_t
1466 lws_plat_asyncdns_init(struct lws_context *context, lws_sockaddr46 *sa);
1467 int
1468 lws_async_dns_init(struct lws_context *context);
1469 void
1470 lws_async_dns_deinit(lws_async_dns_t *dns);
1471 #endif
1472 
1473 int
1474 lws_protocol_init_vhost(struct lws_vhost *vh, int *any);
1475 int
1476 _lws_generic_transaction_completed_active_conn(struct lws **wsi, char take_vh_lock);
1477 
1478 #define ACTIVE_CONNS_SOLO 0
1479 #define ACTIVE_CONNS_MUXED 1
1480 #define ACTIVE_CONNS_QUEUED 2
1481 #define ACTIVE_CONNS_FAILED 3
1482 
1483 #if defined(_DEBUG) && !defined(LWS_PLAT_FREERTOS) && !defined(WIN32) && !defined(LWS_PLAT_OPTEE)
1484 
1485 int
1486 sanity_assert_no_wsi_traces(const struct lws_context *context, struct lws *wsi);
1487 int
1488 sanity_assert_no_sockfd_traces(const struct lws_context *context,
1489 			       lws_sockfd_type sfd);
1490 #else
sanity_assert_no_wsi_traces(const struct lws_context * context,struct lws * wsi)1491 static inline int sanity_assert_no_wsi_traces(const struct lws_context *context, struct lws *wsi) { (void)context; (void)wsi; return 0; }
sanity_assert_no_sockfd_traces(const struct lws_context * context,lws_sockfd_type sfd)1492 static inline int sanity_assert_no_sockfd_traces(const struct lws_context *context, lws_sockfd_type sfd) { (void)context; (void)sfd; return 0; }
1493 #endif
1494 
1495 
1496 void
1497 delete_from_fdwsi(const struct lws_context *context, struct lws *wsi);
1498 
1499 int
1500 lws_vhost_active_conns(struct lws *wsi, struct lws **nwsi, const char *adsin);
1501 
1502 const char *
1503 lws_wsi_client_stash_item(struct lws *wsi, int stash_idx, int hdr_idx);
1504 
1505 int
1506 lws_plat_BINDTODEVICE(lws_sockfd_type fd, const char *ifname);
1507 
1508 int
1509 lws_socks5c_ads_server(struct lws_vhost *vh,
1510 		       const struct lws_context_creation_info *info);
1511 
1512 int
1513 lws_socks5c_handle_state(struct lws *wsi, struct lws_pollfd *pollfd,
1514 			 const char **pcce);
1515 
1516 int
1517 lws_socks5c_greet(struct lws *wsi, const char **pcce);
1518 
1519 int
1520 lws_plat_mbedtls_net_send(void *ctx, const uint8_t *buf, size_t len);
1521 
1522 int
1523 lws_plat_mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len);
1524 
1525 lws_usec_t
1526 lws_sul_nonmonotonic_adjust(struct lws_context *ctx, int64_t step_us);
1527 
1528 void
1529 __lws_vhost_destroy_pt_wsi_dieback_start(struct lws_vhost *vh);
1530 
1531 void
1532 lws_netdev_instance_remove_destroy(struct lws_netdev_instance *ni);
1533 
1534 int
1535 lws_score_dns_results(struct lws_context *ctx,
1536 			     const struct addrinfo **result);
1537 
1538 #if defined(LWS_WITH_SYS_SMD)
1539 int
1540 lws_netdev_smd_cb(void *opaque, lws_smd_class_t _class, lws_usec_t timestamp,
1541 		  void *buf, size_t len);
1542 #endif
1543 
1544 void
1545 lws_netdev_instance_create(lws_netdev_instance_t *ni, struct lws_context *ctx,
1546 			   const lws_netdev_ops_t *ops, const char *name,
1547 			   void *platinfo);
1548 
1549 int
1550 lws_netdev_wifi_rssi_sort_compare(const lws_dll2_t *d, const lws_dll2_t *i);
1551 void
1552 lws_netdev_wifi_scan_empty(lws_netdev_instance_wifi_t *wnd);
1553 
1554 lws_wifi_sta_t *
1555 lws_netdev_wifi_scan_find(lws_netdev_instance_wifi_t *wnd, const char *ssid,
1556 			  const uint8_t *bssid);
1557 
1558 int
1559 lws_netdev_wifi_scan_select(lws_netdev_instance_wifi_t *wnd);
1560 
1561 lws_wifi_creds_t *
1562 lws_netdev_credentials_find(lws_netdevs_t *netdevs, const char *ssid,
1563 			    const uint8_t *bssid);
1564 
1565 int
1566 lws_netdev_wifi_redo_last(lws_netdev_instance_wifi_t *wnd);
1567 
1568 void
1569 lws_ntpc_trigger(struct lws_context *ctx);
1570 
1571 void
1572 lws_netdev_wifi_scan(lws_sorted_usec_list_t *sul);
1573 
1574 #define lws_netdevs_from_ndi(ni) \
1575 		lws_container_of((ni)->list.owner, lws_netdevs_t, owner)
1576 
1577 #define lws_context_from_netdevs(nd) \
1578 		lws_container_of(nd, struct lws_context, netdevs)
1579 
1580 /* get the owner of the ni, then compute the context the owner is embedded in */
1581 #define netdev_instance_to_ctx(ni) \
1582 		lws_container_of(lws_netdevs_from_ndi(ni), \
1583 				 struct lws_context, netdevs)
1584 
1585 enum {
1586 	LW5CHS_RET_RET0,
1587 	LW5CHS_RET_BAIL3,
1588 	LW5CHS_RET_STARTHS,
1589 	LW5CHS_RET_NOTHING
1590 };
1591 
1592 void
1593 lws_4to6(uint8_t *v6addr, const uint8_t *v4addr);
1594 void
1595 lws_sa46_4to6(lws_sockaddr46 *sa46, const uint8_t *v4addr, uint16_t port);
1596 
1597 #ifdef __cplusplus
1598 };
1599 #endif
1600 
1601 #endif
1602