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 LWS_WITH_IPV6
52 #if defined(WIN32) || defined(_WIN32)
53 #include <iphlpapi.h>
54 #else
55 #include <net/if.h>
56 #endif
57 #endif
58 
59 #ifdef __cplusplus
60 extern "C" {
61 #endif
62 
63 /*
64  * All lws_tls...() functions must return this type, converting the
65  * native backend result and doing the extra work to determine which one
66  * as needed.
67  *
68  * Native TLS backend return codes are NOT ALLOWED outside the backend.
69  *
70  * Non-SSL mode also uses these types.
71  */
72 enum lws_ssl_capable_status {
73 	LWS_SSL_CAPABLE_ERROR			= -1, /* it failed */
74 	LWS_SSL_CAPABLE_DONE			= 0,  /* it succeeded */
75 	LWS_SSL_CAPABLE_MORE_SERVICE_READ	= -2, /* retry WANT_READ */
76 	LWS_SSL_CAPABLE_MORE_SERVICE_WRITE	= -3, /* retry WANT_WRITE */
77 	LWS_SSL_CAPABLE_MORE_SERVICE		= -4, /* general retry */
78 };
79 
80 
81 /*
82  *
83  *  ------ roles ------
84  *
85  */
86 
87 /* null-terminated array of pointers to roles lws built with */
88 extern const struct lws_role_ops *available_roles[];
89 
90 #define LWS_FOR_EVERY_AVAILABLE_ROLE_START(xx) { \
91 		const struct lws_role_ops **ppxx = available_roles; \
92 		while (*ppxx) { \
93 			const struct lws_role_ops *xx = *ppxx++;
94 
95 #define LWS_FOR_EVERY_AVAILABLE_ROLE_END }}
96 
97 /*
98  *
99  *  ------ event_loop ops ------
100  *
101  */
102 
103 /* enums of socks version */
104 enum socks_version {
105 	SOCKS_VERSION_4 = 4,
106 	SOCKS_VERSION_5 = 5
107 };
108 
109 /* enums of subnegotiation version */
110 enum socks_subnegotiation_version {
111 	SOCKS_SUBNEGOTIATION_VERSION_1 = 1,
112 };
113 
114 /* enums of socks commands */
115 enum socks_command {
116 	SOCKS_COMMAND_CONNECT = 1,
117 	SOCKS_COMMAND_BIND = 2,
118 	SOCKS_COMMAND_UDP_ASSOCIATE = 3
119 };
120 
121 /* enums of socks address type */
122 enum socks_atyp {
123 	SOCKS_ATYP_IPV4 = 1,
124 	SOCKS_ATYP_DOMAINNAME = 3,
125 	SOCKS_ATYP_IPV6 = 4
126 };
127 
128 /* enums of socks authentication methods */
129 enum socks_auth_method {
130 	SOCKS_AUTH_NO_AUTH = 0,
131 	SOCKS_AUTH_GSSAPI = 1,
132 	SOCKS_AUTH_USERNAME_PASSWORD = 2
133 };
134 
135 /* enums of subnegotiation status */
136 enum socks_subnegotiation_status {
137 	SOCKS_SUBNEGOTIATION_STATUS_SUCCESS = 0,
138 };
139 
140 /* enums of socks request reply */
141 enum socks_request_reply {
142 	SOCKS_REQUEST_REPLY_SUCCESS = 0,
143 	SOCKS_REQUEST_REPLY_FAILURE_GENERAL = 1,
144 	SOCKS_REQUEST_REPLY_CONNECTION_NOT_ALLOWED = 2,
145 	SOCKS_REQUEST_REPLY_NETWORK_UNREACHABLE = 3,
146 	SOCKS_REQUEST_REPLY_HOST_UNREACHABLE = 4,
147 	SOCKS_REQUEST_REPLY_CONNECTION_REFUSED = 5,
148 	SOCKS_REQUEST_REPLY_TTL_EXPIRED = 6,
149 	SOCKS_REQUEST_REPLY_COMMAND_NOT_SUPPORTED = 7,
150 	SOCKS_REQUEST_REPLY_ATYP_NOT_SUPPORTED = 8
151 };
152 
153 /* enums used to generate socks messages */
154 enum socks_msg_type {
155 	/* greeting */
156 	SOCKS_MSG_GREETING,
157 	/* credential, user name and password */
158 	SOCKS_MSG_USERNAME_PASSWORD,
159 	/* connect command */
160 	SOCKS_MSG_CONNECT
161 };
162 
163 enum {
164 	LWS_RXFLOW_ALLOW = (1 << 0),
165 	LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
166 };
167 
168 typedef enum lws_parser_return {
169 	LPR_FORBIDDEN	= -2,
170 	LPR_FAIL	= -1,
171 	LPR_OK		= 0,
172 	LPR_DO_FALLBACK = 2,
173 } lws_parser_return_t;
174 
175 enum pmd_return {
176 	PMDR_UNKNOWN,
177 	PMDR_DID_NOTHING,
178 	PMDR_HAS_PENDING,
179 	PMDR_EMPTY_NONFINAL,
180 	PMDR_EMPTY_FINAL,
181 
182 	PMDR_FAILED = -1
183 };
184 
185 #if defined(LWS_WITH_PEER_LIMITS)
186 struct lws_peer {
187 	struct lws_peer *next;
188 	struct lws_peer *peer_wait_list;
189 
190 	time_t time_created;
191 	time_t time_closed_all;
192 
193 	uint8_t addr[32];
194 	uint32_t hash;
195 	uint32_t count_wsi;
196 	uint32_t total_wsi;
197 
198 #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
199 	struct lws_peer_role_http http;
200 #endif
201 
202 	uint8_t af;
203 };
204 #endif
205 
206 enum {
207 	LWS_EV_READ = (1 << 0),
208 	LWS_EV_WRITE = (1 << 1),
209 	LWS_EV_START = (1 << 2),
210 	LWS_EV_STOP = (1 << 3),
211 
212 	LWS_EV_PREPARE_DELETION = (1u << 31),
213 };
214 
215 #ifdef LWS_WITH_IPV6
216 #define LWS_IPV6_ENABLED(vh) \
217 	(!lws_check_opt(vh->context->options, LWS_SERVER_OPTION_DISABLE_IPV6) && \
218 	 !lws_check_opt(vh->options, LWS_SERVER_OPTION_DISABLE_IPV6))
219 #else
220 #define LWS_IPV6_ENABLED(context) (0)
221 #endif
222 
223 #ifdef LWS_WITH_UNIX_SOCK
224 #define LWS_UNIX_SOCK_ENABLED(vhost) \
225 	(vhost->options & LWS_SERVER_OPTION_UNIX_SOCK)
226 #else
227 #define LWS_UNIX_SOCK_ENABLED(vhost) (0)
228 #endif
229 
230 enum uri_path_states {
231 	URIPS_IDLE,
232 	URIPS_SEEN_SLASH,
233 	URIPS_SEEN_SLASH_DOT,
234 	URIPS_SEEN_SLASH_DOT_DOT,
235 };
236 
237 enum uri_esc_states {
238 	URIES_IDLE,
239 	URIES_SEEN_PERCENT,
240 	URIES_SEEN_PERCENT_H1,
241 };
242 
243 #if defined(LWS_WITH_CLIENT)
244 
245 enum {
246 	CIS_ADDRESS,
247 	CIS_PATH,
248 	CIS_HOST,
249 	CIS_ORIGIN,
250 	CIS_PROTOCOL,
251 	CIS_METHOD,
252 	CIS_IFACE,
253 	CIS_ALPN,
254 
255 
256 	CIS_COUNT
257 };
258 
259 struct client_info_stash {
260 	char *cis[CIS_COUNT];
261 	void *opaque_user_data; /* not allocated or freed by lws */
262 };
263 #endif
264 
265 #if defined(LWS_WITH_UDP)
266 #define lws_wsi_is_udp(___wsi) (!!___wsi->udp)
267 #endif
268 
269 #define LWS_H2_FRAME_HEADER_LENGTH 9
270 
271 int
272 __lws_sul_insert(lws_dll2_owner_t *own, lws_sorted_usec_list_t *sul,
273 		 lws_usec_t us);
274 
275 lws_usec_t
276 __lws_sul_service_ripe(lws_dll2_owner_t *own, lws_usec_t usnow);
277 
278 struct lws_timed_vh_protocol {
279 	struct lws_timed_vh_protocol	*next;
280 	lws_sorted_usec_list_t		sul;
281 	const struct lws_protocols	*protocol;
282 	struct lws_vhost *vhost; /* only used for pending processing */
283 	int				reason;
284 	int				tsi_req;
285 };
286 
287 /*
288  * lws_dsh
289 */
290 
291 typedef struct lws_dsh_obj_head {
292 	lws_dll2_owner_t		owner;
293 	int				kind;
294 } lws_dsh_obj_head_t;
295 
296 typedef struct lws_dsh_obj {
297 	lws_dll2_t			list;	/* must be first */
298 	struct lws_dsh	  		*dsh;	/* invalid when on free list */
299 	size_t				size;	/* invalid when on free list */
300 	size_t				asize;
301 } lws_dsh_obj_t;
302 
303 typedef struct lws_dsh {
304 	lws_dll2_t			list;
305 	uint8_t				*buf;
306 	lws_dsh_obj_head_t		*oha;	/* array of object heads/kind */
307 	size_t				buffer_size;
308 	size_t				locally_in_use;
309 	size_t				locally_free;
310 	int				count_kinds;
311 	uint8_t				being_destroyed;
312 	/*
313 	 * Overallocations at create:
314 	 *
315 	 *  - the buffer itself
316 	 *  - the object heads array
317 	 */
318 } lws_dsh_t;
319 
320 /*
321  * lws_async_dns
322  */
323 
324 typedef struct lws_async_dns {
325 	lws_sockaddr46 		sa46; /* nameserver */
326 	lws_dll2_owner_t	waiting;
327 	lws_dll2_owner_t	cached;
328 	struct lws		*wsi;
329 	time_t			time_set_server;
330 	char			dns_server_set;
331 } lws_async_dns_t;
332 
333 typedef enum {
334 	LADNS_CONF_SERVER_UNKNOWN				= -1,
335 	LADNS_CONF_SERVER_SAME,
336 	LADNS_CONF_SERVER_CHANGED
337 } lws_async_dns_server_check_t;
338 
339 #if defined(LWS_WITH_SYS_ASYNC_DNS)
340 void
341 lws_aysnc_dns_completed(struct lws *wsi, void *sa, size_t salen,
342 			lws_async_dns_retcode_t ret);
343 #endif
344 void
345 lws_async_dns_cancel(struct lws *wsi);
346 
347 /*
348  * so we can have n connections being serviced simultaneously,
349  * these things need to be isolated per-thread.
350  */
351 
352 struct lws_context_per_thread {
353 #if LWS_MAX_SMP > 1
354 	pthread_mutex_t lock_stats;
355 	struct lws_mutex_refcount mr;
356 	pthread_t self;
357 #endif
358 	struct lws_dll2_owner dll_buflist_owner;  /* guys with pending rxflow */
359 	struct lws_dll2_owner seq_owner;	   /* list of lws_sequencer-s */
360 	lws_dll2_owner_t      attach_owner;	/* pending lws_attach */
361 
362 #if defined(LWS_WITH_SECURE_STREAMS)
363 	lws_dll2_owner_t ss_owner;
364 #endif
365 #if defined(LWS_WITH_SECURE_STREAMS_PROXY_API) || \
366     defined(LWS_WITH_SECURE_STREAMS_THREAD_API)
367 	lws_dll2_owner_t ss_dsh_owner;
368 	lws_dll2_owner_t ss_client_owner;
369 #endif
370 
371 	struct lws_dll2_owner pt_sul_owner;
372 
373 #if defined (LWS_WITH_SEQUENCER)
374 	lws_sorted_usec_list_t sul_seq_heartbeat;
375 #endif
376 #if (defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)) && defined(LWS_WITH_SERVER)
377 	lws_sorted_usec_list_t sul_ah_lifecheck;
378 #endif
379 #if defined(LWS_WITH_TLS) && defined(LWS_WITH_SERVER)
380 	lws_sorted_usec_list_t sul_tls;
381 #endif
382 #if defined(LWS_PLAT_UNIX)
383 	lws_sorted_usec_list_t sul_plat;
384 #endif
385 #if defined(LWS_ROLE_CGI)
386 	lws_sorted_usec_list_t sul_cgi;
387 #endif
388 #if defined(LWS_WITH_STATS)
389 	uint64_t lws_stats[LWSSTATS_SIZE];
390 	int updated;
391 	lws_sorted_usec_list_t sul_stats;
392 #endif
393 #if defined(LWS_WITH_PEER_LIMITS)
394 	lws_sorted_usec_list_t sul_peer_limits;
395 #endif
396 
397 #if defined(LWS_WITH_TLS)
398 	struct lws_pt_tls tls;
399 #endif
400 	struct lws *fake_wsi;	/* used for callbacks where there's no wsi */
401 
402 	struct lws_context *context;
403 
404 	/*
405 	 * usable by anything in the service code, but only if the scope
406 	 * does not last longer than the service action (since next service
407 	 * of any socket can likewise use it and overwrite)
408 	 */
409 	unsigned char *serv_buf;
410 
411 	struct lws_pollfd *fds;
412 	volatile struct lws_foreign_thread_pollfd * volatile foreign_pfd_list;
413 #ifdef _WIN32
414 	WSAEVENT events;
415 	CRITICAL_SECTION interrupt_lock;
416 #endif
417 	lws_sockfd_type dummy_pipe_fds[2];
418 	struct lws *pipe_wsi;
419 
420 	/* --- role based members --- */
421 
422 #if defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)
423 	struct lws_pt_role_ws ws;
424 #endif
425 #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
426 	struct lws_pt_role_http http;
427 #endif
428 #if defined(LWS_ROLE_DBUS)
429 	struct lws_pt_role_dbus dbus;
430 #endif
431 	/* --- event library based members --- */
432 
433 #if defined(LWS_WITH_LIBEV)
434 	struct lws_pt_eventlibs_libev ev;
435 #endif
436 #if defined(LWS_WITH_LIBUV)
437 	struct lws_pt_eventlibs_libuv uv;
438 #endif
439 #if defined(LWS_WITH_LIBEVENT)
440 	struct lws_pt_eventlibs_libevent event;
441 #endif
442 #if defined(LWS_WITH_GLIB)
443 	struct lws_pt_eventlibs_glib glib;
444 #endif
445 
446 #if defined(LWS_WITH_LIBEV) || defined(LWS_WITH_LIBUV) || \
447     defined(LWS_WITH_LIBEVENT) || defined(LWS_WITH_GLIB)
448 	struct lws_signal_watcher w_sigint;
449 #endif
450 
451 #if defined(LWS_WITH_DETAILED_LATENCY)
452 	lws_usec_t	ust_left_poll;
453 #endif
454 
455 	/* --- */
456 
457 	unsigned long count_conns;
458 	unsigned int fds_count;
459 
460 	/*
461 	 * set to the Thread ID that's doing the service loop just before entry
462 	 * to poll indicates service thread likely idling in poll()
463 	 * volatile because other threads may check it as part of processing
464 	 * for pollfd event change.
465 	 */
466 	volatile int service_tid;
467 	int service_tid_detected;
468 
469 	volatile unsigned char inside_poll;
470 	volatile unsigned char foreign_spinlock;
471 
472 	unsigned char tid;
473 
474 	unsigned char inside_service:1;
475 	unsigned char inside_lws_service:1;
476 	unsigned char event_loop_foreign:1;
477 	unsigned char event_loop_destroy_processing_done:1;
478 	unsigned char destroy_self:1;
479 	unsigned char is_destroyed:1;
480 #ifdef _WIN32
481 	unsigned char interrupt_requested:1;
482 #endif
483 };
484 
485 #if defined(LWS_WITH_SERVER_STATUS)
486 struct lws_conn_stats {
487 	unsigned long long rx, tx;
488 	unsigned long h1_conn, h1_trans, h2_trans, ws_upg, h2_alpn, h2_subs,
489 		      h2_upg, rejected, mqtt_subs;
490 };
491 #endif
492 
493 /*
494  * virtual host -related context information
495  *   vhostwide SSL context
496  *   vhostwide proxy
497  *
498  * hierarchy:
499  *
500  * context -> vhost -> wsi
501  *
502  * incoming connection non-SSL vhost binding:
503  *
504  *    listen socket -> wsi -> select vhost after first headers
505  *
506  * incoming connection SSL vhost binding:
507  *
508  *    SSL SNI -> wsi -> bind after SSL negotiation
509  */
510 
511 struct lws_vhost {
512 #if defined(LWS_WITH_CLIENT) && defined(LWS_CLIENT_HTTP_PROXYING)
513 	char proxy_basic_auth_token[128];
514 #endif
515 #if LWS_MAX_SMP > 1
516 	pthread_mutex_t lock;
517 	char close_flow_vs_tsi[LWS_MAX_SMP];
518 #endif
519 
520 #if defined(LWS_ROLE_H2)
521 	struct lws_vhost_role_h2 h2;
522 #endif
523 #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
524 	struct lws_vhost_role_http http;
525 #endif
526 #if defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)
527 	struct lws_vhost_role_ws ws;
528 #endif
529 
530 #if defined(LWS_WITH_SOCKS5)
531 	char socks_proxy_address[128];
532 	char socks_user[96];
533 	char socks_password[96];
534 #endif
535 #if defined(LWS_WITH_LIBEV)
536 	struct lws_io_watcher w_accept;
537 #endif
538 #if defined(LWS_WITH_SERVER_STATUS)
539 	struct lws_conn_stats conn_stats;
540 #endif
541 
542 	uint64_t options;
543 
544 	struct lws_context *context;
545 	struct lws_vhost *vhost_next;
546 
547 	const lws_retry_bo_t *retry_policy;
548 
549 	struct lws *lserv_wsi;
550 	const char *name;
551 	const char *iface;
552 	const char *listen_accept_role;
553 	const char *listen_accept_protocol;
554 	const char *unix_socket_perms;
555 
556 	void (*finalize)(struct lws_vhost *vh, void *arg);
557 	void *finalize_arg;
558 
559 	const struct lws_protocols *protocols;
560 	void **protocol_vh_privs;
561 	const struct lws_protocol_vhost_options *pvo;
562 	const struct lws_protocol_vhost_options *headers;
563 	struct lws_dll2_owner *same_vh_protocol_owner;
564 	struct lws_vhost *no_listener_vhost_list;
565 	struct lws_dll2_owner abstract_instances_owner;		/* vh lock */
566 
567 #if defined(LWS_WITH_CLIENT)
568 	struct lws_dll2_owner dll_cli_active_conns_owner;
569 #endif
570 	struct lws_dll2_owner vh_awaiting_socket_owner;
571 
572 #if defined(LWS_WITH_TLS)
573 	struct lws_vhost_tls tls;
574 #endif
575 
576 	struct lws_timed_vh_protocol *timed_vh_protocol_list;
577 	void *user;
578 
579 	int listen_port;
580 #if !defined(LWS_PLAT_FREERTOS) && !defined(OPTEE_TA) && !defined(WIN32)
581 	int bind_iface;
582 #endif
583 
584 #if defined(LWS_WITH_SOCKS5)
585 	unsigned int socks_proxy_port;
586 #endif
587 	int count_protocols;
588 	int ka_time;
589 	int ka_probes;
590 	int ka_interval;
591 	int keepalive_timeout;
592 	int timeout_secs_ah_idle;
593 
594 	int count_bound_wsi;
595 
596 #ifdef LWS_WITH_ACCESS_LOG
597 	int log_fd;
598 #endif
599 
600 	uint8_t allocated_vhost_protocols:1;
601 	uint8_t created_vhost_protocols:1;
602 	uint8_t being_destroyed:1;
603 	uint8_t from_ss_policy:1;
604 
605 	unsigned char default_protocol_index;
606 	unsigned char raw_protocol_index;
607 };
608 
609 void
610 __lws_vhost_destroy2(struct lws_vhost *vh);
611 
612 #define mux_to_wsi(_m) lws_container_of(_m, struct lws, mux)
613 
614 void
615 lws_wsi_mux_insert(struct lws *wsi, struct lws *parent_wsi, int sid);
616 int
617 lws_wsi_mux_mark_parents_needing_writeable(struct lws *wsi);
618 struct lws *
619 lws_wsi_mux_move_child_to_tail(struct lws **wsi2);
620 int
621 lws_wsi_mux_action_pending_writeable_reqs(struct lws *wsi);
622 
623 void
624 lws_wsi_mux_dump_children(struct lws *wsi);
625 
626 void
627 lws_wsi_mux_close_children(struct lws *wsi, int reason);
628 
629 void
630 lws_wsi_mux_sibling_disconnect(struct lws *wsi);
631 
632 void
633 lws_wsi_mux_dump_waiting_children(struct lws *wsi);
634 
635 int
636 lws_wsi_mux_apply_queue(struct lws *wsi);
637 
638 /*
639  * struct lws
640  */
641 
642 struct lws {
643 	/* structs */
644 
645 #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
646 	struct _lws_http_mode_related	http;
647 #endif
648 #if defined(LWS_ROLE_H2)
649 	struct _lws_h2_related		h2;
650 #endif
651 #if defined(LWS_ROLE_WS)
652 	struct _lws_websocket_related	*ws; /* allocated if we upgrade to ws */
653 #endif
654 #if defined(LWS_ROLE_DBUS)
655 	struct _lws_dbus_mode_related	dbus;
656 #endif
657 #if defined(LWS_ROLE_MQTT)
658 	struct _lws_mqtt_related	*mqtt;
659 #endif
660 
661 #if defined(LWS_ROLE_H2) || defined(LWS_ROLE_MQTT)
662 	struct lws_muxable		mux;
663 	struct lws_tx_credit		txc;
664 #endif
665 
666 	/* lifetime members */
667 
668 #if defined(LWS_WITH_LIBEV) || defined(LWS_WITH_LIBUV) || \
669     defined(LWS_WITH_LIBEVENT) || defined(LWS_WITH_GLIB)
670 	struct lws_io_watcher		w_read;
671 #endif
672 #if defined(LWS_WITH_LIBEV) || defined(LWS_WITH_LIBEVENT)
673 	struct lws_io_watcher		w_write;
674 #endif
675 
676 #if defined(LWS_WITH_DETAILED_LATENCY)
677 	lws_detlat_t	detlat;
678 #endif
679 
680 	lws_sorted_usec_list_t		sul_timeout;
681 	lws_sorted_usec_list_t		sul_hrtimer;
682 	lws_sorted_usec_list_t		sul_validity;
683 
684 	struct lws_dll2			dll_buflist; /* guys with pending rxflow */
685 	struct lws_dll2			same_vh_protocol;
686 	struct lws_dll2			vh_awaiting_socket;
687 #if defined(LWS_WITH_SYS_ASYNC_DNS)
688 	struct lws_dll2			adns; /* on adns list of guys to tell result */
689 	lws_async_dns_cb_t		adns_cb; /* callback with result */
690 #endif
691 #if defined(LWS_WITH_CLIENT)
692 	struct lws_dll2			dll_cli_active_conns;
693 	struct lws_dll2			dll2_cli_txn_queue;
694 	struct lws_dll2_owner		dll2_cli_txn_queue_owner;
695 #endif
696 
697 #if defined(LWS_WITH_ACCESS_LOG)
698 	char simple_ip[(8 * 5)];
699 #endif
700 	/* pointers */
701 
702 	struct lws_context		*context;
703 	struct lws_vhost		*vhost;
704 	struct lws			*parent; /* points to parent, if any */
705 	struct lws			*child_list; /* points to first child */
706 	struct lws			*sibling_list; /* subsequent children at same level */
707 	const struct lws_role_ops	*role_ops;
708 	const struct lws_protocols	*protocol;
709 	struct lws_sequencer		*seq;	/* associated sequencer if any */
710 	const lws_retry_bo_t		*retry_policy;
711 
712 #if defined(LWS_WITH_THREADPOOL)
713 	struct lws_threadpool_task	*tp_task;
714 #endif
715 
716 #if defined(LWS_WITH_PEER_LIMITS)
717 	struct lws_peer			*peer;
718 #endif
719 
720 #if defined(LWS_WITH_UDP)
721 	struct lws_udp			*udp;
722 #endif
723 #if defined(LWS_WITH_CLIENT)
724 	struct client_info_stash	*stash;
725 	char				*cli_hostname_copy;
726 	const struct addrinfo		*dns_results;
727 	const struct addrinfo		*dns_results_next;
728 #endif
729 	void				*user_space;
730 	void				*opaque_parent_data;
731 	void				*opaque_user_data;
732 
733 	struct lws_buflist		*buflist; /* input-side buflist */
734 	struct lws_buflist		*buflist_out; /* output-side buflist */
735 
736 #if defined(LWS_WITH_TLS)
737 	struct lws_lws_tls		tls;
738 #endif
739 
740 	lws_sock_file_fd_type		desc; /* .filefd / .sockfd */
741 #if defined(LWS_WITH_STATS)
742 	uint64_t active_writable_req_us;
743 #if defined(LWS_WITH_TLS)
744 	uint64_t accept_start_us;
745 #endif
746 #endif
747 	lws_wsi_state_t			wsistate;
748 	lws_wsi_state_t			wsistate_pre_close;
749 
750 	/* ints */
751 #define LWS_NO_FDS_POS (-1)
752 	int				position_in_fds_table;
753 
754 #if defined(LWS_WITH_CLIENT)
755 	int				chunk_remaining;
756 	int				flags;
757 #endif
758 	unsigned int			cache_secs;
759 
760 	unsigned int			hdr_parsing_completed:1;
761 	unsigned int			mux_substream:1;
762 	unsigned int			upgraded_to_http2:1;
763 	unsigned int			mux_stream_immortal:1;
764 	unsigned int			h2_stream_carries_ws:1; /* immortal set as well */
765 	unsigned int			h2_stream_carries_sse:1; /* immortal set as well */
766 	unsigned int			h2_acked_settings:1;
767 	unsigned int			seen_nonpseudoheader:1;
768 	unsigned int			listener:1;
769 	unsigned int			pf_packet:1;
770 	unsigned int			do_broadcast:1;
771 	unsigned int			user_space_externally_allocated:1;
772 	unsigned int			socket_is_permanently_unusable:1;
773 	unsigned int			rxflow_change_to:2;
774 	unsigned int			conn_stat_done:1;
775 	unsigned int			cache_reuse:1;
776 	unsigned int			cache_revalidate:1;
777 	unsigned int			cache_intermediaries:1;
778 	unsigned int			favoured_pollin:1;
779 	unsigned int			sending_chunked:1;
780 	unsigned int			interpreting:1;
781 	unsigned int			already_did_cce:1;
782 	unsigned int			told_user_closed:1;
783 	unsigned int			told_event_loop_closed:1;
784 	unsigned int			waiting_to_send_close_frame:1;
785 	unsigned int			close_needs_ack:1;
786 	unsigned int			ipv6:1;
787 	unsigned int			parent_pending_cb_on_writable:1;
788 	unsigned int			cgi_stdout_zero_length:1;
789 	unsigned int			seen_zero_length_recv:1;
790 	unsigned int			rxflow_will_be_applied:1;
791 	unsigned int			event_pipe:1;
792 	unsigned int			handling_404:1;
793 	unsigned int			protocol_bind_balance:1;
794 	unsigned int			unix_skt:1;
795 	unsigned int			close_when_buffered_out_drained:1;
796 	unsigned int			h1_ws_proxied:1;
797 	unsigned int			proxied_ws_parent:1;
798 	unsigned int			do_bind:1;
799 	unsigned int			oom4:1;
800 	unsigned int			validity_hup:1;
801 	unsigned int			skip_fallback:1;
802 
803 	unsigned int			could_have_pending:1; /* detect back-to-back writes */
804 	unsigned int			outer_will_close:1;
805 	unsigned int			shadow:1; /* we do not control fd lifecycle at all */
806 
807 #ifdef LWS_WITH_ACCESS_LOG
808 	unsigned int			access_log_pending:1;
809 #endif
810 #if defined(LWS_WITH_CLIENT)
811 	unsigned int			do_ws:1; /* whether we are doing http or ws flow */
812 	unsigned int			chunked:1; /* if the clientside connection is chunked */
813 	unsigned int			client_rx_avail:1;
814 	unsigned int			client_http_body_pending:1;
815 	unsigned int			transaction_from_pipeline_queue:1;
816 	unsigned int			keepalive_active:1;
817 	unsigned int			keepalive_rejected:1;
818 	unsigned int			redirected_to_get:1;
819 	unsigned int			client_pipeline:1;
820 	unsigned int			client_h2_alpn:1;
821 	unsigned int			client_mux_substream:1;
822 	unsigned int			client_mux_migrated:1;
823 	unsigned int			client_subsequent_mime_part:1;
824 	unsigned int                    client_no_follow_redirect:1;
825 #endif
826 
827 #ifdef _WIN32
828 	unsigned int sock_send_blocking:1;
829 #endif
830 
831 	uint16_t			ocport, c_port;
832 	uint16_t			retry;
833 
834 	/* chars */
835 
836 	char lws_rx_parse_state; /* enum lws_rx_parse_state */
837 	char rx_frame_type; /* enum lws_write_protocol */
838 	char pending_timeout; /* enum pending_timeout */
839 	char tsi; /* thread service index we belong to */
840 	char protocol_interpret_idx;
841 	char redirects;
842 	uint8_t rxflow_bitmap;
843 	uint8_t bound_vhost_index;
844 	uint8_t lsp_channel; /* which of stdin/out/err */
845 #ifdef LWS_WITH_CGI
846 	char hdr_state;
847 #endif
848 #if defined(LWS_WITH_CLIENT)
849 	char chunk_parser; /* enum lws_chunk_parser */
850 	uint8_t addrinfo_idx;
851 	uint8_t sys_tls_client_cert;
852 #endif
853 #if defined(LWS_WITH_CGI) || defined(LWS_WITH_CLIENT)
854 	char reason_bf; /* internal writeable callback reason bitfield */
855 #endif
856 #if defined(LWS_WITH_STATS) && defined(LWS_WITH_TLS)
857 	char seen_rx;
858 #endif
859 	uint8_t immortal_substream_count;
860 	/* volatile to make sure code is aware other thread can change */
861 	volatile char handling_pollout;
862 	volatile char leave_pollout_active;
863 #if LWS_MAX_SMP > 1
864 	volatile char undergoing_init_from_other_pt;
865 #endif
866 
867 };
868 
869 #define lws_is_flowcontrolled(w) (!!(wsi->rxflow_bitmap))
870 
871 #if defined(LWS_WITH_SPAWN)
872 
873 #if defined(WIN32) || defined(_WIN32)
874 #else
875 #include <sys/wait.h>
876 #include <sys/times.h>
877 #endif
878 
879 struct lws_spawn_piped {
880 
881 	struct lws_spawn_piped_info	info;
882 
883 	struct lws_dll2			dll;
884 	lws_sorted_usec_list_t		sul;
885 
886 	struct lws			*stdwsi[3];
887 	int				pipe_fds[3][2];
888 	int				count_log_lines;
889 
890 	lws_usec_t			created; /* set by lws_spawn_piped() */
891 	lws_usec_t			reaped;
892 
893 	lws_usec_t			accounting[4];
894 
895 	pid_t				child_pid;
896 
897 	siginfo_t			si;
898 
899 	uint8_t				pipes_alive:2;
900 	uint8_t				we_killed_him_timeout:1;
901 	uint8_t				we_killed_him_spew:1;
902 	uint8_t				ungraceful:1;
903 };
904 
905 void
906 lws_spawn_piped_destroy(struct lws_spawn_piped **lsp);
907 
908 int
909 lws_spawn_reap(struct lws_spawn_piped *lsp);
910 
911 #endif
912 
913 void
914 lws_service_do_ripe_rxflow(struct lws_context_per_thread *pt);
915 
916 const struct lws_role_ops *
917 lws_role_by_name(const char *name);
918 
919 int
920 lws_socket_bind(struct lws_vhost *vhost, lws_sockfd_type sockfd, int port,
921 		const char *iface, int ipv6_allowed);
922 
923 #if defined(LWS_WITH_IPV6)
924 unsigned long
925 lws_get_addr_scope(const char *ipaddr);
926 #endif
927 
928 void
929 lws_close_free_wsi(struct lws *wsi, enum lws_close_status, const char *caller);
930 void
931 __lws_close_free_wsi(struct lws *wsi, enum lws_close_status, const char *caller);
932 
933 void
934 __lws_free_wsi(struct lws *wsi);
935 
936 #if LWS_MAX_SMP > 1
937 
938 static LWS_INLINE void
lws_pt_mutex_init(struct lws_context_per_thread * pt)939 lws_pt_mutex_init(struct lws_context_per_thread *pt)
940 {
941 	lws_mutex_refcount_init(&pt->mr);
942 	pthread_mutex_init(&pt->lock_stats, NULL);
943 }
944 
945 static LWS_INLINE void
lws_pt_mutex_destroy(struct lws_context_per_thread * pt)946 lws_pt_mutex_destroy(struct lws_context_per_thread *pt)
947 {
948 	pthread_mutex_destroy(&pt->lock_stats);
949 	lws_mutex_refcount_destroy(&pt->mr);
950 }
951 
952 #define lws_pt_lock(pt, reason) lws_mutex_refcount_lock(&pt->mr, reason)
953 #define lws_pt_unlock(pt) lws_mutex_refcount_unlock(&pt->mr)
954 
955 static LWS_INLINE void
lws_pt_stats_lock(struct lws_context_per_thread * pt)956 lws_pt_stats_lock(struct lws_context_per_thread *pt)
957 {
958 	pthread_mutex_lock(&pt->lock_stats);
959 }
960 
961 static LWS_INLINE void
lws_pt_stats_unlock(struct lws_context_per_thread * pt)962 lws_pt_stats_unlock(struct lws_context_per_thread *pt)
963 {
964 	pthread_mutex_unlock(&pt->lock_stats);
965 }
966 #endif
967 
968 /*
969  * EXTENSIONS
970  */
971 
972 #if defined(LWS_WITHOUT_EXTENSIONS)
973 #define lws_any_extension_handled(_a, _b, _c, _d) (0)
974 #define lws_ext_cb_active(_a, _b, _c, _d) (0)
975 #define lws_ext_cb_all_exts(_a, _b, _c, _d, _e) (0)
976 #define lws_issue_raw_ext_access lws_issue_raw
977 #define lws_context_init_extensions(_a, _b)
978 #endif
979 
980 int LWS_WARN_UNUSED_RESULT
981 lws_client_interpret_server_handshake(struct lws *wsi);
982 
983 int LWS_WARN_UNUSED_RESULT
984 lws_ws_rx_sm(struct lws *wsi, char already_processed, unsigned char c);
985 
986 int LWS_WARN_UNUSED_RESULT
987 lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);
988 
989 void
990 lws_role_transition(struct lws *wsi, enum lwsi_role role, enum lwsi_state state,
991 		    const struct lws_role_ops *ops);
992 
993 int
994 lws_http_to_fallback(struct lws *wsi, unsigned char *buf, size_t len);
995 
996 int LWS_WARN_UNUSED_RESULT
997 user_callback_handle_rxflow(lws_callback_function, struct lws *wsi,
998 			    enum lws_callback_reasons reason, void *user,
999 			    void *in, size_t len);
1000 
1001 int
1002 lws_plat_set_nonblocking(lws_sockfd_type fd);
1003 
1004 int
1005 lws_plat_set_socket_options(struct lws_vhost *vhost, lws_sockfd_type fd,
1006 			    int unix_skt);
1007 
1008 int
1009 lws_plat_check_connection_error(struct lws *wsi);
1010 
1011 int LWS_WARN_UNUSED_RESULT
1012 lws_header_table_attach(struct lws *wsi, int autoservice);
1013 
1014 int
1015 lws_header_table_detach(struct lws *wsi, int autoservice);
1016 int
1017 __lws_header_table_detach(struct lws *wsi, int autoservice);
1018 
1019 void
1020 lws_header_table_reset(struct lws *wsi, int autoservice);
1021 
1022 void
1023 __lws_header_table_reset(struct lws *wsi, int autoservice);
1024 
1025 char * LWS_WARN_UNUSED_RESULT
1026 lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h);
1027 
1028 int LWS_WARN_UNUSED_RESULT
1029 lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s);
1030 
1031 int LWS_WARN_UNUSED_RESULT
1032 lws_ensure_user_space(struct lws *wsi);
1033 
1034 int LWS_WARN_UNUSED_RESULT
1035 lws_change_pollfd(struct lws *wsi, int _and, int _or);
1036 
1037 #if defined(LWS_WITH_SERVER)
1038  int _lws_vhost_init_server(const struct lws_context_creation_info *info,
1039 			      struct lws_vhost *vhost);
1040  LWS_EXTERN struct lws_vhost *
1041  lws_select_vhost(struct lws_context *context, int port, const char *servername);
1042  LWS_EXTERN int LWS_WARN_UNUSED_RESULT
1043  lws_parse_ws(struct lws *wsi, unsigned char **buf, size_t len);
1044  LWS_EXTERN void
1045  lws_server_get_canonical_hostname(struct lws_context *context,
1046 				   const struct lws_context_creation_info *info);
1047 #else
1048  #define _lws_vhost_init_server(_a, _b) (0)
1049  #define lws_parse_ws(_a, _b, _c) (0)
1050  #define lws_server_get_canonical_hostname(_a, _b)
1051 #endif
1052 
1053 int
1054 __remove_wsi_socket_from_fds(struct lws *wsi);
1055 
1056 enum {
1057 	LWSRXFC_ERROR = -1,
1058 	LWSRXFC_CACHED = 0,
1059 	LWSRXFC_ADDITIONAL = 1,
1060 	LWSRXFC_TRIMMED = 2,
1061 };
1062 
1063 
1064 int
1065 _lws_plat_service_forced_tsi(struct lws_context *context, int tsi);
1066 
1067 int
1068 lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len);
1069 
1070 int
1071 lws_service_flag_pending(struct lws_context *context, int tsi);
1072 
1073 static LWS_INLINE int
lws_has_buffered_out(struct lws * wsi)1074 lws_has_buffered_out(struct lws *wsi) { return !!wsi->buflist_out; }
1075 
1076 int LWS_WARN_UNUSED_RESULT
1077 lws_ws_client_rx_sm(struct lws *wsi, unsigned char c);
1078 
1079 lws_parser_return_t LWS_WARN_UNUSED_RESULT
1080 lws_parse(struct lws *wsi, unsigned char *buf, int *len);
1081 
1082 int LWS_WARN_UNUSED_RESULT
1083 lws_parse_urldecode(struct lws *wsi, uint8_t *_c);
1084 
1085 int LWS_WARN_UNUSED_RESULT
1086 lws_http_action(struct lws *wsi);
1087 
1088 void
1089 __lws_close_free_wsi_final(struct lws *wsi);
1090 void
1091 lws_libuv_closehandle(struct lws *wsi);
1092 int
1093 lws_libuv_check_watcher_active(struct lws *wsi);
1094 
1095 LWS_VISIBLE LWS_EXTERN int
1096 lws_plat_plugins_init(struct lws_context * context, const char * const *d);
1097 
1098 LWS_VISIBLE LWS_EXTERN int
1099 lws_plat_plugins_destroy(struct lws_context * context);
1100 
1101 struct lws *
1102 lws_adopt_socket_vhost(struct lws_vhost *vh, lws_sockfd_type accept_fd);
1103 
1104 void
1105 lws_vhost_bind_wsi(struct lws_vhost *vh, struct lws *wsi);
1106 void
1107 lws_vhost_unbind_wsi(struct lws *wsi);
1108 
1109 void
1110 __lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs);
1111 int
1112 __lws_change_pollfd(struct lws *wsi, int _and, int _or);
1113 
1114 
1115 int
1116 lws_callback_as_writeable(struct lws *wsi);
1117 
1118 int
1119 lws_role_call_client_bind(struct lws *wsi,
1120 			  const struct lws_client_connect_info *i);
1121 void
1122 lws_remove_child_from_any_parent(struct lws *wsi);
1123 
1124 char *
1125 lws_generate_client_ws_handshake(struct lws *wsi, char *p, const char *conn1);
1126 int
1127 lws_client_ws_upgrade(struct lws *wsi, const char **cce);
1128 int
1129 lws_create_client_ws_object(const struct lws_client_connect_info *i,
1130 			    struct lws *wsi);
1131 int
1132 lws_alpn_comma_to_openssl(const char *comma, uint8_t *os, int len);
1133 int
1134 lws_role_call_alpn_negotiated(struct lws *wsi, const char *alpn);
1135 int
1136 lws_tls_server_conn_alpn(struct lws *wsi);
1137 
1138 int
1139 lws_ws_client_rx_sm_block(struct lws *wsi, unsigned char **buf, size_t len);
1140 void
1141 lws_destroy_event_pipe(struct lws *wsi);
1142 
1143 /* socks */
1144 int
1145 lws_socks5c_generate_msg(struct lws *wsi, enum socks_msg_type type, ssize_t *msg_len);
1146 
1147 #if defined(LWS_WITH_SERVER_STATUS)
1148 void
1149 lws_sum_stats(const struct lws_context *ctx, struct lws_conn_stats *cs);
1150 #endif
1151 
1152 int
1153 __lws_timed_callback_remove(struct lws_vhost *vh, struct lws_timed_vh_protocol *p);
1154 
1155 int LWS_WARN_UNUSED_RESULT
1156 __insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi);
1157 
1158 int LWS_WARN_UNUSED_RESULT
1159 lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
1160 
1161 lws_usec_t
1162 __lws_seq_timeout_check(struct lws_context_per_thread *pt, lws_usec_t usnow);
1163 
1164 lws_usec_t
1165 __lws_ss_timeout_check(struct lws_context_per_thread *pt, lws_usec_t usnow);
1166 
1167 struct lws * LWS_WARN_UNUSED_RESULT
1168 lws_client_connect_2_dnsreq(struct lws *wsi);
1169 
1170 LWS_VISIBLE struct lws * LWS_WARN_UNUSED_RESULT
1171 lws_client_reset(struct lws **wsi, int ssl, const char *address, int port,
1172 		 const char *path, const char *host, char weak);
1173 
1174 struct lws * LWS_WARN_UNUSED_RESULT
1175 lws_create_new_server_wsi(struct lws_vhost *vhost, int fixed_tsi);
1176 
1177 char * LWS_WARN_UNUSED_RESULT
1178 lws_generate_client_handshake(struct lws *wsi, char *pkt);
1179 
1180 int
1181 lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
1182 
1183 struct lws *
1184 lws_http_client_connect_via_info2(struct lws *wsi);
1185 
1186 
1187 #if defined(LWS_WITH_CLIENT)
1188 int
1189 lws_client_socket_service(struct lws *wsi, struct lws_pollfd *pollfd);
1190 
1191 int LWS_WARN_UNUSED_RESULT
1192 lws_http_transaction_completed_client(struct lws *wsi);
1193 #if !defined(LWS_WITH_TLS)
1194 	#define lws_context_init_client_ssl(_a, _b) (0)
1195 #endif
1196 void
1197 lws_decode_ssl_error(void);
1198 #else
1199 #define lws_context_init_client_ssl(_a, _b) (0)
1200 #endif
1201 
1202 int
1203 __lws_rx_flow_control(struct lws *wsi);
1204 
1205 int
1206 _lws_change_pollfd(struct lws *wsi, int _and, int _or, struct lws_pollargs *pa);
1207 
1208 #if defined(LWS_WITH_SERVER)
1209 int
1210 lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len);
1211 #else
1212 #define lws_server_socket_service(_b, _c) (0)
1213 #define lws_handshake_server(_a, _b, _c) (0)
1214 #endif
1215 
1216 #ifdef LWS_WITH_ACCESS_LOG
1217 int
1218 lws_access_log(struct lws *wsi);
1219 void
1220 lws_prepare_access_log_info(struct lws *wsi, char *uri_ptr, int len, int meth);
1221 #else
1222 #define lws_access_log(_a)
1223 #endif
1224 
1225 #if defined(_DEBUG)
1226 void
1227 lws_wsi_txc_describe(struct lws_tx_credit *txc, const char *at, uint32_t sid);
1228 #else
1229 #define lws_wsi_txc_describe(x, y, z) { (void)x; }
1230 #endif
1231 
1232 int
1233 lws_wsi_txc_check_skint(struct lws_tx_credit *txc, int32_t tx_cr);
1234 
1235 int
1236 lws_wsi_txc_report_manual_txcr_in(struct lws *wsi, int32_t bump);
1237 
1238 void
1239 lws_mux_mark_immortal(struct lws *wsi);
1240 void
1241 lws_http_close_immortal(struct lws *wsi);
1242 
1243 int
1244 lws_cgi_kill_terminated(struct lws_context_per_thread *pt);
1245 
1246 void
1247 lws_cgi_remove_and_kill(struct lws *wsi);
1248 
1249 void
1250 lws_plat_delete_socket_from_fds(struct lws_context *context,
1251 				struct lws *wsi, int m);
1252 void
1253 lws_plat_insert_socket_into_fds(struct lws_context *context,
1254 				struct lws *wsi);
1255 
1256 int
1257 lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
1258 		       struct lws_pollfd *pfd);
1259 
1260 
1261 int
1262 lws_plat_pipe_create(struct lws *wsi);
1263 int
1264 lws_plat_pipe_signal(struct lws *wsi);
1265 void
1266 lws_plat_pipe_close(struct lws *wsi);
1267 
1268 void
1269 lws_addrinfo_clean(struct lws *wsi);
1270 
1271 void
1272 lws_add_wsi_to_draining_ext_list(struct lws *wsi);
1273 void
1274 lws_remove_wsi_from_draining_ext_list(struct lws *wsi);
1275 int
1276 lws_poll_listen_fd(struct lws_pollfd *fd);
1277 int
1278 lws_plat_service(struct lws_context *context, int timeout_ms);
1279 LWS_VISIBLE int
1280 _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi);
1281 
1282 int
1283 lws_pthread_self_to_tsi(struct lws_context *context);
1284 const char * LWS_WARN_UNUSED_RESULT
1285 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt);
1286 int LWS_WARN_UNUSED_RESULT
1287 lws_plat_inet_pton(int af, const char *src, void *dst);
1288 
1289 void
1290 lws_same_vh_protocol_remove(struct lws *wsi);
1291 void
1292 __lws_same_vh_protocol_remove(struct lws *wsi);
1293 void
1294 lws_same_vh_protocol_insert(struct lws *wsi, int n);
1295 
1296 void
1297 lws_seq_destroy_all_on_pt(struct lws_context_per_thread *pt);
1298 
1299 int
1300 lws_broadcast(struct lws_context_per_thread *pt, int reason, void *in, size_t len);
1301 
1302 #if defined(LWS_WITH_STATS)
1303  void
1304  lws_stats_bump(struct lws_context_per_thread *pt, int i, uint64_t bump);
1305  void
1306  lws_stats_max(struct lws_context_per_thread *pt, int index, uint64_t val);
1307 #else
lws_stats_bump(struct lws_context_per_thread * pt,int index,uint64_t bump)1308  static LWS_INLINE uint64_t lws_stats_bump(
1309 		struct lws_context_per_thread *pt, int index, uint64_t bump) {
1310 	(void)pt; (void)index; (void)bump; return 0; }
lws_stats_max(struct lws_context_per_thread * pt,int index,uint64_t val)1311  static LWS_INLINE uint64_t lws_stats_max(
1312 		struct lws_context_per_thread *pt, int index, uint64_t val) {
1313 	(void)pt; (void)index; (void)val; return 0; }
1314 #endif
1315 
1316 
1317 
1318 #if defined(LWS_WITH_PEER_LIMITS)
1319 void
1320 lws_peer_track_wsi_close(struct lws_context *context, struct lws_peer *peer);
1321 int
1322 lws_peer_confirm_ah_attach_ok(struct lws_context *context,
1323 			      struct lws_peer *peer);
1324 void
1325 lws_peer_track_ah_detach(struct lws_context *context, struct lws_peer *peer);
1326 void
1327 lws_peer_cull_peer_wait_list(struct lws_context *context);
1328 struct lws_peer *
1329 lws_get_or_create_peer(struct lws_vhost *vhost, lws_sockfd_type sockfd);
1330 void
1331 lws_peer_add_wsi(struct lws_context *context, struct lws_peer *peer,
1332 		 struct lws *wsi);
1333 void
1334 lws_peer_dump_from_wsi(struct lws *wsi);
1335 #endif
1336 
1337 #ifdef LWS_WITH_HUBBUB
1338 hubbub_error
1339 html_parser_cb(const hubbub_token *token, void *pw);
1340 #endif
1341 
1342 int
1343 lws_threadpool_tsi_context(struct lws_context *context, int tsi);
1344 
1345 void
1346 __lws_wsi_remove_from_sul(struct lws *wsi);
1347 
1348 void
1349 lws_validity_confirmed(struct lws *wsi);
1350 void
1351 _lws_validity_confirmed_role(struct lws *wsi);
1352 
1353 int
1354 lws_seq_pt_init(struct lws_context_per_thread *pt);
1355 
1356 int
1357 lws_buflist_aware_read(struct lws_context_per_thread *pt, struct lws *wsi,
1358 		       struct lws_tokens *ebuf, char fr, const char *hint);
1359 int
1360 lws_buflist_aware_finished_consuming(struct lws *wsi, struct lws_tokens *ebuf,
1361 				     int used, int buffered, const char *hint);
1362 
1363 extern const struct lws_protocols protocol_abs_client_raw_skt,
1364 				  protocol_abs_client_unit_test;
1365 
1366 void
1367 __lws_reset_wsi(struct lws *wsi);
1368 
1369 void
1370 lws_inform_client_conn_fail(struct lws *wsi, void *arg, size_t len);
1371 
1372 #if defined(LWS_WITH_SYS_ASYNC_DNS)
1373 lws_async_dns_server_check_t
1374 lws_plat_asyncdns_init(struct lws_context *context, lws_sockaddr46 *sa);
1375 int
1376 lws_async_dns_init(struct lws_context *context);
1377 void
1378 lws_async_dns_deinit(lws_async_dns_t *dns);
1379 #endif
1380 
1381 int
1382 lws_protocol_init_vhost(struct lws_vhost *vh, int *any);
1383 int
1384 _lws_generic_transaction_completed_active_conn(struct lws **wsi);
1385 
1386 #define ACTIVE_CONNS_SOLO 0
1387 #define ACTIVE_CONNS_MUXED 1
1388 #define ACTIVE_CONNS_QUEUED 2
1389 #define ACTIVE_CONNS_FAILED 3
1390 
1391 int
1392 lws_vhost_active_conns(struct lws *wsi, struct lws **nwsi, const char *adsin);
1393 
1394 const char *
1395 lws_wsi_client_stash_item(struct lws *wsi, int stash_idx, int hdr_idx);
1396 
1397 int
1398 lws_plat_BINDTODEVICE(lws_sockfd_type fd, const char *ifname);
1399 
1400 int
1401 lws_socks5c_ads_server(struct lws_vhost *vh,
1402 		       const struct lws_context_creation_info *info);
1403 
1404 int
1405 lws_socks5c_handle_state(struct lws *wsi, struct lws_pollfd *pollfd,
1406 			 const char **pcce);
1407 
1408 int
1409 lws_socks5c_greet(struct lws *wsi, const char **pcce);
1410 
1411 enum {
1412 	LW5CHS_RET_RET0,
1413 	LW5CHS_RET_BAIL3,
1414 	LW5CHS_RET_STARTHS,
1415 	LW5CHS_RET_NOTHING
1416 };
1417 
1418 #ifdef __cplusplus
1419 };
1420 #endif
1421 
1422 #endif
1423