1d4afb5ceSopenharmony_ci /*
2d4afb5ceSopenharmony_ci * libwebsockets - small server side websockets and web server implementation
3d4afb5ceSopenharmony_ci *
4d4afb5ceSopenharmony_ci * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5d4afb5ceSopenharmony_ci *
6d4afb5ceSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
7d4afb5ceSopenharmony_ci * of this software and associated documentation files (the "Software"), to
8d4afb5ceSopenharmony_ci * deal in the Software without restriction, including without limitation the
9d4afb5ceSopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10d4afb5ceSopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is
11d4afb5ceSopenharmony_ci * furnished to do so, subject to the following conditions:
12d4afb5ceSopenharmony_ci *
13d4afb5ceSopenharmony_ci * The above copyright notice and this permission notice shall be included in
14d4afb5ceSopenharmony_ci * all copies or substantial portions of the Software.
15d4afb5ceSopenharmony_ci *
16d4afb5ceSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17d4afb5ceSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18d4afb5ceSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19d4afb5ceSopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20d4afb5ceSopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21d4afb5ceSopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22d4afb5ceSopenharmony_ci * IN THE SOFTWARE.
23d4afb5ceSopenharmony_ci *
24d4afb5ceSopenharmony_ci *  This is included from private-lib-core.h if LWS_ROLE_WS
25d4afb5ceSopenharmony_ci */
26d4afb5ceSopenharmony_ci
27d4afb5ceSopenharmony_ciextern const struct lws_role_ops role_ops_ws;
28d4afb5ceSopenharmony_ci
29d4afb5ceSopenharmony_ci#define lwsi_role_ws(wsi) (wsi->role_ops == &role_ops_ws)
30d4afb5ceSopenharmony_ci
31d4afb5ceSopenharmony_cienum lws_rx_parse_state {
32d4afb5ceSopenharmony_ci	LWS_RXPS_NEW,
33d4afb5ceSopenharmony_ci
34d4afb5ceSopenharmony_ci	LWS_RXPS_04_mask_1,
35d4afb5ceSopenharmony_ci	LWS_RXPS_04_mask_2,
36d4afb5ceSopenharmony_ci	LWS_RXPS_04_mask_3,
37d4afb5ceSopenharmony_ci
38d4afb5ceSopenharmony_ci	LWS_RXPS_04_FRAME_HDR_1,
39d4afb5ceSopenharmony_ci	LWS_RXPS_04_FRAME_HDR_LEN,
40d4afb5ceSopenharmony_ci	LWS_RXPS_04_FRAME_HDR_LEN16_2,
41d4afb5ceSopenharmony_ci	LWS_RXPS_04_FRAME_HDR_LEN16_1,
42d4afb5ceSopenharmony_ci	LWS_RXPS_04_FRAME_HDR_LEN64_8,
43d4afb5ceSopenharmony_ci	LWS_RXPS_04_FRAME_HDR_LEN64_7,
44d4afb5ceSopenharmony_ci	LWS_RXPS_04_FRAME_HDR_LEN64_6,
45d4afb5ceSopenharmony_ci	LWS_RXPS_04_FRAME_HDR_LEN64_5,
46d4afb5ceSopenharmony_ci	LWS_RXPS_04_FRAME_HDR_LEN64_4,
47d4afb5ceSopenharmony_ci	LWS_RXPS_04_FRAME_HDR_LEN64_3,
48d4afb5ceSopenharmony_ci	LWS_RXPS_04_FRAME_HDR_LEN64_2,
49d4afb5ceSopenharmony_ci	LWS_RXPS_04_FRAME_HDR_LEN64_1,
50d4afb5ceSopenharmony_ci
51d4afb5ceSopenharmony_ci	LWS_RXPS_07_COLLECT_FRAME_KEY_1,
52d4afb5ceSopenharmony_ci	LWS_RXPS_07_COLLECT_FRAME_KEY_2,
53d4afb5ceSopenharmony_ci	LWS_RXPS_07_COLLECT_FRAME_KEY_3,
54d4afb5ceSopenharmony_ci	LWS_RXPS_07_COLLECT_FRAME_KEY_4,
55d4afb5ceSopenharmony_ci
56d4afb5ceSopenharmony_ci	LWS_RXPS_WS_FRAME_PAYLOAD
57d4afb5ceSopenharmony_ci};
58d4afb5ceSopenharmony_ci
59d4afb5ceSopenharmony_cienum lws_websocket_opcodes_07 {
60d4afb5ceSopenharmony_ci	LWSWSOPC_CONTINUATION = 0,
61d4afb5ceSopenharmony_ci	LWSWSOPC_TEXT_FRAME = 1,
62d4afb5ceSopenharmony_ci	LWSWSOPC_BINARY_FRAME = 2,
63d4afb5ceSopenharmony_ci
64d4afb5ceSopenharmony_ci	LWSWSOPC_NOSPEC__MUX = 7,
65d4afb5ceSopenharmony_ci
66d4afb5ceSopenharmony_ci	/* control extensions 8+ */
67d4afb5ceSopenharmony_ci
68d4afb5ceSopenharmony_ci	LWSWSOPC_CLOSE = 8,
69d4afb5ceSopenharmony_ci	LWSWSOPC_PING = 9,
70d4afb5ceSopenharmony_ci	LWSWSOPC_PONG = 0xa,
71d4afb5ceSopenharmony_ci};
72d4afb5ceSopenharmony_ci
73d4afb5ceSopenharmony_ci/* this is not usable directly by user code any more, lws_close_reason() */
74d4afb5ceSopenharmony_ci#define LWS_WRITE_CLOSE 4
75d4afb5ceSopenharmony_ci
76d4afb5ceSopenharmony_ci#define ALREADY_PROCESSED_IGNORE_CHAR 1
77d4afb5ceSopenharmony_ci#define ALREADY_PROCESSED_NO_CB 2
78d4afb5ceSopenharmony_ci
79d4afb5ceSopenharmony_ci#if !defined(LWS_WITHOUT_EXTENSIONS)
80d4afb5ceSopenharmony_cistruct lws_vhost_role_ws {
81d4afb5ceSopenharmony_ci	const struct lws_extension *extensions;
82d4afb5ceSopenharmony_ci};
83d4afb5ceSopenharmony_ci
84d4afb5ceSopenharmony_cistruct lws_pt_role_ws {
85d4afb5ceSopenharmony_ci	struct lws *rx_draining_ext_list;
86d4afb5ceSopenharmony_ci	struct lws *tx_draining_ext_list;
87d4afb5ceSopenharmony_ci};
88d4afb5ceSopenharmony_ci#endif
89d4afb5ceSopenharmony_ci
90d4afb5ceSopenharmony_ci#define PAYLOAD_BUF_SIZE 128 - 3 + LWS_PRE
91d4afb5ceSopenharmony_ci
92d4afb5ceSopenharmony_cistruct _lws_websocket_related {
93d4afb5ceSopenharmony_ci	unsigned char *rx_ubuf;
94d4afb5ceSopenharmony_ci#if !defined(LWS_WITHOUT_EXTENSIONS)
95d4afb5ceSopenharmony_ci	const struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
96d4afb5ceSopenharmony_ci	void *act_ext_user[LWS_MAX_EXTENSIONS_ACTIVE];
97d4afb5ceSopenharmony_ci	struct lws *rx_draining_ext_list;
98d4afb5ceSopenharmony_ci	struct lws *tx_draining_ext_list;
99d4afb5ceSopenharmony_ci#endif
100d4afb5ceSopenharmony_ci
101d4afb5ceSopenharmony_ci#if defined(LWS_WITH_HTTP_PROXY)
102d4afb5ceSopenharmony_ci	struct lws_dll2_owner proxy_owner;
103d4afb5ceSopenharmony_ci	char actual_protocol[16];
104d4afb5ceSopenharmony_ci	size_t proxy_buffered;
105d4afb5ceSopenharmony_ci#endif
106d4afb5ceSopenharmony_ci
107d4afb5ceSopenharmony_ci	/* Also used for close content... control opcode == < 128 */
108d4afb5ceSopenharmony_ci	uint8_t ping_payload_buf[PAYLOAD_BUF_SIZE];
109d4afb5ceSopenharmony_ci	uint8_t pong_payload_buf[PAYLOAD_BUF_SIZE];
110d4afb5ceSopenharmony_ci
111d4afb5ceSopenharmony_ci	unsigned int final:1;
112d4afb5ceSopenharmony_ci	unsigned int frame_is_binary:1;
113d4afb5ceSopenharmony_ci	unsigned int all_zero_nonce:1;
114d4afb5ceSopenharmony_ci	unsigned int this_frame_masked:1;
115d4afb5ceSopenharmony_ci	unsigned int inside_frame:1; /* next write will be more of frame */
116d4afb5ceSopenharmony_ci	unsigned int clean_buffer:1; /* buffer not rewritten by extension */
117d4afb5ceSopenharmony_ci	unsigned int payload_is_close:1; /* process as PONG, but it is close */
118d4afb5ceSopenharmony_ci	unsigned int pong_pending_flag:1;
119d4afb5ceSopenharmony_ci	unsigned int continuation_possible:1;
120d4afb5ceSopenharmony_ci	unsigned int owed_a_fin:1;
121d4afb5ceSopenharmony_ci	unsigned int check_utf8:1;
122d4afb5ceSopenharmony_ci	unsigned int defeat_check_utf8:1;
123d4afb5ceSopenharmony_ci	unsigned int stashed_write_pending:1;
124d4afb5ceSopenharmony_ci	unsigned int send_check_ping:1;
125d4afb5ceSopenharmony_ci	unsigned int first_fragment:1;
126d4afb5ceSopenharmony_ci	unsigned int peer_has_sent_close:1;
127d4afb5ceSopenharmony_ci#if !defined(LWS_WITHOUT_EXTENSIONS)
128d4afb5ceSopenharmony_ci	unsigned int extension_data_pending:1;
129d4afb5ceSopenharmony_ci	unsigned int rx_draining_ext:1;
130d4afb5ceSopenharmony_ci	unsigned int tx_draining_ext:1;
131d4afb5ceSopenharmony_ci	unsigned int pmd_trailer_application:1;
132d4afb5ceSopenharmony_ci#endif
133d4afb5ceSopenharmony_ci
134d4afb5ceSopenharmony_ci	uint8_t mask[4];
135d4afb5ceSopenharmony_ci
136d4afb5ceSopenharmony_ci	size_t rx_packet_length;
137d4afb5ceSopenharmony_ci	uint32_t rx_ubuf_head;
138d4afb5ceSopenharmony_ci	uint32_t rx_ubuf_alloc;
139d4afb5ceSopenharmony_ci
140d4afb5ceSopenharmony_ci	uint8_t pong_payload_len;
141d4afb5ceSopenharmony_ci	uint8_t mask_idx;
142d4afb5ceSopenharmony_ci	uint8_t opcode;
143d4afb5ceSopenharmony_ci	uint8_t rsv;
144d4afb5ceSopenharmony_ci	uint8_t rsv_first_msg;
145d4afb5ceSopenharmony_ci	/* zero if no info, or length including 2-byte close code */
146d4afb5ceSopenharmony_ci	uint8_t close_in_ping_buffer_len;
147d4afb5ceSopenharmony_ci	uint8_t utf8;
148d4afb5ceSopenharmony_ci	uint8_t stashed_write_type;
149d4afb5ceSopenharmony_ci	uint8_t tx_draining_stashed_wp;
150d4afb5ceSopenharmony_ci	uint8_t ietf_spec_revision;
151d4afb5ceSopenharmony_ci#if !defined(LWS_WITHOUT_EXTENSIONS)
152d4afb5ceSopenharmony_ci	uint8_t count_act_ext;
153d4afb5ceSopenharmony_ci#endif
154d4afb5ceSopenharmony_ci};
155d4afb5ceSopenharmony_ci
156d4afb5ceSopenharmony_ci/*
157d4afb5ceSopenharmony_ci * we need to separately track what's happening with both compressed rx in
158d4afb5ceSopenharmony_ci * and with inflated rx out that will be passed to the user code
159d4afb5ceSopenharmony_ci */
160d4afb5ceSopenharmony_ci
161d4afb5ceSopenharmony_cistruct lws_ext_pm_deflate_rx_ebufs {
162d4afb5ceSopenharmony_ci	struct lws_tokens eb_in;
163d4afb5ceSopenharmony_ci	struct lws_tokens eb_out;
164d4afb5ceSopenharmony_ci};
165d4afb5ceSopenharmony_ci
166d4afb5ceSopenharmony_ciint
167d4afb5ceSopenharmony_cilws_ws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len);
168d4afb5ceSopenharmony_ci
169d4afb5ceSopenharmony_ci#if !defined(LWS_WITHOUT_EXTENSIONS)
170d4afb5ceSopenharmony_ciLWS_VISIBLE void
171d4afb5ceSopenharmony_cilws_context_init_extensions(const struct lws_context_creation_info *info,
172d4afb5ceSopenharmony_ci			    struct lws_context *context);
173d4afb5ceSopenharmony_ciLWS_EXTERN int
174d4afb5ceSopenharmony_cilws_any_extension_handled(struct lws *wsi, enum lws_extension_callback_reasons r,
175d4afb5ceSopenharmony_ci			  void *v, size_t len);
176d4afb5ceSopenharmony_ci
177d4afb5ceSopenharmony_ciLWS_EXTERN int
178d4afb5ceSopenharmony_cilws_ext_cb_active(struct lws *wsi, int reason, void *buf, int len);
179d4afb5ceSopenharmony_ciLWS_EXTERN int
180d4afb5ceSopenharmony_cilws_ext_cb_all_exts(struct lws_context *context, struct lws *wsi, int reason,
181d4afb5ceSopenharmony_ci		    void *arg, int len);
182d4afb5ceSopenharmony_ci#endif
183d4afb5ceSopenharmony_ci
184d4afb5ceSopenharmony_ciint
185d4afb5ceSopenharmony_cihandshake_0405(struct lws_context *context, struct lws *wsi);
186d4afb5ceSopenharmony_ciint
187d4afb5ceSopenharmony_cilws_process_ws_upgrade(struct lws *wsi);
188d4afb5ceSopenharmony_ci
189d4afb5ceSopenharmony_ciint
190d4afb5ceSopenharmony_cilws_process_ws_upgrade2(struct lws *wsi);
191d4afb5ceSopenharmony_ci
192d4afb5ceSopenharmony_ciextern const struct lws_protocols lws_ws_proxy;
193d4afb5ceSopenharmony_ci
194d4afb5ceSopenharmony_ciint
195d4afb5ceSopenharmony_cilws_server_init_wsi_for_ws(struct lws *wsi);
196d4afb5ceSopenharmony_ci
197d4afb5ceSopenharmony_civoid
198d4afb5ceSopenharmony_cilws_sul_wsping_cb(lws_sorted_usec_list_t *sul);
199