/src/libwebsockets/lib/roles/raw-skt/ops-raw-skt.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * libwebsockets - small server side websockets and web server implementation |
3 | | * |
4 | | * Copyright (C) 2010 - 2020 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 | | #include <private-lib-core.h> |
26 | | |
27 | | #if defined(LWS_WITH_CLIENT) |
28 | | static int |
29 | | lws_raw_skt_connect(struct lws *wsi) |
30 | 0 | { |
31 | 0 | int n; |
32 | 0 | #if defined(LWS_WITH_TLS) |
33 | 0 | const char *cce = NULL; |
34 | 0 | char ccebuf[128]; |
35 | |
|
36 | 0 | #if !defined(LWS_WITH_SYS_ASYNC_DNS) |
37 | 0 | switch (lws_client_create_tls(wsi, &cce, 1)) { |
38 | | #else |
39 | | switch (lws_client_create_tls(wsi, &cce, 0)) { |
40 | | #endif |
41 | 0 | case CCTLS_RETURN_ERROR: |
42 | 0 | lws_inform_client_conn_fail(wsi, (void *)cce, strlen(cce)); |
43 | 0 | return -1; |
44 | 0 | case CCTLS_RETURN_RETRY: |
45 | 0 | return 0; |
46 | 0 | case CCTLS_RETURN_DONE: |
47 | 0 | break; |
48 | 0 | } |
49 | | |
50 | 0 | if (wsi->tls.use_ssl & LCCSCF_USE_SSL) { |
51 | 0 | n = lws_ssl_client_connect2(wsi, ccebuf, sizeof(ccebuf)); |
52 | 0 | if (n < 0) { |
53 | 0 | lws_inform_client_conn_fail(wsi, (void *)ccebuf, |
54 | 0 | strlen(ccebuf)); |
55 | |
|
56 | 0 | return -1; |
57 | 0 | } |
58 | 0 | if (n != 1) |
59 | 0 | return 0; /* wait */ |
60 | 0 | } |
61 | 0 | #endif |
62 | | |
63 | 0 | if (!wsi->hdr_parsing_completed) { |
64 | 0 | n = user_callback_handle_rxflow(wsi->a.protocol->callback, |
65 | 0 | wsi, wsi->role_ops->adoption_cb[lwsi_role_server(wsi)], |
66 | 0 | wsi->user_space, NULL, 0); |
67 | 0 | if (n) { |
68 | 0 | lws_inform_client_conn_fail(wsi, (void *)"user", 4); |
69 | 0 | return 1; |
70 | 0 | } |
71 | 0 | } |
72 | | |
73 | 0 | lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0); |
74 | 0 | lwsi_set_state(wsi, LRS_ESTABLISHED); |
75 | |
|
76 | 0 | return 1; /* success */ |
77 | 0 | } |
78 | | #endif |
79 | | |
80 | | static lws_handling_result_t |
81 | | rops_handle_POLLIN_raw_skt(struct lws_context_per_thread *pt, struct lws *wsi, |
82 | | struct lws_pollfd *pollfd) |
83 | 0 | { |
84 | | #if defined(LWS_WITH_SOCKS5) |
85 | | const char *cce = NULL; |
86 | | #endif |
87 | 0 | struct lws_tokens ebuf; |
88 | 0 | int n = 0, buffered = 0; |
89 | | |
90 | | /* pending truncated sends have uber priority */ |
91 | |
|
92 | 0 | if (lws_has_buffered_out(wsi)) { |
93 | 0 | if (!(pollfd->revents & LWS_POLLOUT)) |
94 | 0 | return LWS_HPI_RET_HANDLED; |
95 | | |
96 | | /* drain the output buflist */ |
97 | 0 | if (lws_issue_raw(wsi, NULL, 0) < 0) |
98 | 0 | goto fail; |
99 | | /* |
100 | | * we can't afford to allow input processing to send |
101 | | * something new, so spin around he event loop until |
102 | | * he doesn't have any partials |
103 | | */ |
104 | 0 | return LWS_HPI_RET_HANDLED; |
105 | 0 | } |
106 | | |
107 | | |
108 | 0 | #if defined(LWS_WITH_SERVER) |
109 | 0 | if (!lwsi_role_client(wsi) && lwsi_state(wsi) != LRS_ESTABLISHED) { |
110 | |
|
111 | 0 | lwsl_wsi_debug(wsi, "wsistate 0x%x\n", (int)wsi->wsistate); |
112 | |
|
113 | 0 | if (lwsi_state(wsi) != LRS_SSL_INIT) |
114 | 0 | if (lws_server_socket_service_ssl(wsi, |
115 | 0 | LWS_SOCK_INVALID, |
116 | 0 | !!(pollfd->revents & pollfd->events & LWS_POLLIN))) |
117 | 0 | return LWS_HPI_RET_PLEASE_CLOSE_ME; |
118 | | |
119 | 0 | return LWS_HPI_RET_HANDLED; |
120 | 0 | } |
121 | 0 | #endif |
122 | | |
123 | 0 | if ((pollfd->revents & pollfd->events & LWS_POLLIN) && |
124 | 0 | !(wsi->favoured_pollin && |
125 | 0 | (pollfd->revents & pollfd->events & LWS_POLLOUT))) { |
126 | |
|
127 | 0 | lwsl_wsi_debug(wsi, "POLLIN: state 0x%x", lwsi_state(wsi)); |
128 | |
|
129 | 0 | switch (lwsi_state(wsi)) { |
130 | | |
131 | | /* any tunnel has to have been established... */ |
132 | 0 | case LRS_SSL_ACK_PENDING: |
133 | 0 | goto nope; |
134 | | /* we are actually connected */ |
135 | 0 | case LRS_WAITING_CONNECT: |
136 | 0 | goto nope; |
137 | | |
138 | 0 | case LRS_WAITING_SSL: |
139 | 0 | #if defined(LWS_WITH_CLIENT) |
140 | 0 | n = lws_raw_skt_connect(wsi); |
141 | 0 | if (n < 0) |
142 | 0 | goto fail; |
143 | 0 | #endif |
144 | 0 | break; |
145 | | |
146 | | #if defined(LWS_WITH_SOCKS5) |
147 | | |
148 | | /* SOCKS Greeting Reply */ |
149 | | case LRS_WAITING_SOCKS_GREETING_REPLY: |
150 | | case LRS_WAITING_SOCKS_AUTH_REPLY: |
151 | | case LRS_WAITING_SOCKS_CONNECT_REPLY: |
152 | | |
153 | | switch (lws_socks5c_handle_state(wsi, pollfd, &cce)) { |
154 | | case LW5CHS_RET_RET0: |
155 | | goto nope; |
156 | | case LW5CHS_RET_BAIL3: |
157 | | lws_inform_client_conn_fail(wsi, (void *)cce, strlen(cce)); |
158 | | goto fail; |
159 | | case LW5CHS_RET_STARTHS: |
160 | | lwsi_set_state(wsi, LRS_ESTABLISHED); |
161 | | lws_client_connect_4_established(wsi, NULL, 0); |
162 | | |
163 | | /* |
164 | | * Now we got the socks5 connection, we need to |
165 | | * go down the tls path on it now if that's what |
166 | | * we want |
167 | | */ |
168 | | goto post_rx; |
169 | | |
170 | | default: |
171 | | break; |
172 | | } |
173 | | goto post_rx; |
174 | | #endif |
175 | 0 | default: |
176 | 0 | ebuf.token = NULL; |
177 | 0 | ebuf.len = (int) wsi->a.protocol->rx_buffer_size; |
178 | |
|
179 | 0 | buffered = lws_buflist_aware_read(pt, wsi, &ebuf, 1, __func__); |
180 | 0 | switch (ebuf.len) { |
181 | 0 | case 0: |
182 | 0 | if (wsi->unix_skt) |
183 | 0 | break; |
184 | 0 | lwsl_wsi_info(wsi, "read 0 len"); |
185 | 0 | wsi->seen_zero_length_recv = 1; |
186 | 0 | if (lws_change_pollfd(wsi, LWS_POLLIN, 0)) |
187 | 0 | goto fail; |
188 | | |
189 | | /* |
190 | | * we need to go to fail here, since it's the only |
191 | | * chance we get to understand that the socket has |
192 | | * closed |
193 | | */ |
194 | | // goto try_pollout; |
195 | 0 | goto fail; |
196 | | |
197 | 0 | case LWS_SSL_CAPABLE_ERROR: |
198 | 0 | goto fail; |
199 | 0 | case LWS_SSL_CAPABLE_MORE_SERVICE: |
200 | 0 | goto try_pollout; |
201 | 0 | } |
202 | | |
203 | 0 | #if defined(LWS_WITH_UDP) |
204 | 0 | if (lws_fi(&wsi->fic, "udp_rx_loss")) { |
205 | 0 | n = ebuf.len; |
206 | 0 | goto post_rx; |
207 | 0 | } |
208 | 0 | #endif |
209 | | |
210 | 0 | n = user_callback_handle_rxflow(wsi->a.protocol->callback, |
211 | 0 | wsi, LWS_CALLBACK_RAW_RX, |
212 | 0 | wsi->user_space, ebuf.token, |
213 | 0 | (unsigned int)ebuf.len); |
214 | 0 | #if defined(LWS_WITH_UDP) || defined(LWS_WITH_SOCKS5) |
215 | 0 | post_rx: |
216 | 0 | #endif |
217 | 0 | if (n < 0) { |
218 | 0 | lwsl_wsi_info(wsi, "LWS_CALLBACK_RAW_RX_fail"); |
219 | 0 | goto fail; |
220 | 0 | } |
221 | | |
222 | 0 | if (lws_buflist_aware_finished_consuming(wsi, &ebuf, ebuf.len, |
223 | 0 | buffered, __func__)) |
224 | 0 | return LWS_HPI_RET_PLEASE_CLOSE_ME; |
225 | | |
226 | 0 | goto try_pollout; |
227 | 0 | } |
228 | 0 | } |
229 | 0 | nope: |
230 | 0 | if (wsi->favoured_pollin && |
231 | 0 | (pollfd->revents & pollfd->events & LWS_POLLOUT)) |
232 | | /* we balanced the last favouring of pollin */ |
233 | 0 | wsi->favoured_pollin = 0; |
234 | |
|
235 | 0 | try_pollout: |
236 | |
|
237 | 0 | if (!(pollfd->revents & LWS_POLLOUT)) |
238 | 0 | return LWS_HPI_RET_HANDLED; |
239 | | |
240 | 0 | #if defined(LWS_WITH_CLIENT) |
241 | 0 | if (lwsi_state(wsi) == LRS_WAITING_CONNECT) { |
242 | 0 | if (!lws_client_connect_3_connect(wsi, NULL, NULL, 0, NULL)) |
243 | 0 | return LWS_HPI_RET_WSI_ALREADY_DIED; |
244 | | |
245 | 0 | if (lws_raw_skt_connect(wsi) < 0) |
246 | 0 | goto fail; |
247 | 0 | } |
248 | 0 | #endif |
249 | | |
250 | 0 | if (lwsi_state(wsi) == LRS_WAITING_SSL) |
251 | 0 | return LWS_HPI_RET_HANDLED; |
252 | | |
253 | | /* one shot */ |
254 | 0 | if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) |
255 | 0 | goto fail; |
256 | | |
257 | | /* clear back-to-back write detection */ |
258 | 0 | wsi->could_have_pending = 0; |
259 | |
|
260 | 0 | n = user_callback_handle_rxflow(wsi->a.protocol->callback, |
261 | 0 | wsi, LWS_CALLBACK_RAW_WRITEABLE, |
262 | 0 | wsi->user_space, NULL, 0); |
263 | 0 | if (n < 0) { |
264 | 0 | lwsl_info("writeable_fail\n"); |
265 | 0 | goto fail; |
266 | 0 | } |
267 | | |
268 | 0 | return LWS_HPI_RET_HANDLED; |
269 | | |
270 | 0 | fail: |
271 | 0 | lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS, "raw svc fail"); |
272 | |
|
273 | 0 | return LWS_HPI_RET_WSI_ALREADY_DIED; |
274 | 0 | } |
275 | | |
276 | | #if defined(LWS_WITH_SERVER) |
277 | | static int |
278 | | rops_adoption_bind_raw_skt(struct lws *wsi, int type, const char *vh_prot_name) |
279 | 0 | { |
280 | | |
281 | | // lwsl_notice("%s: bind type %d\n", __func__, type); |
282 | | |
283 | | /* no http but socket... must be raw skt */ |
284 | 0 | if ((type & LWS_ADOPT_HTTP) || !(type & LWS_ADOPT_SOCKET) || |
285 | 0 | ((type & _LWS_ADOPT_FINISH) && (!(type & LWS_ADOPT_FLAG_UDP)))) |
286 | 0 | return 0; /* no match */ |
287 | | |
288 | 0 | #if defined(LWS_WITH_UDP) |
289 | 0 | if ((type & LWS_ADOPT_FLAG_UDP) && !wsi->udp) { |
290 | | /* |
291 | | * these can be >128 bytes, so just alloc for UDP |
292 | | */ |
293 | 0 | wsi->udp = lws_malloc(sizeof(*wsi->udp), "udp struct"); |
294 | 0 | if (!wsi->udp) |
295 | 0 | return 0; |
296 | 0 | memset(wsi->udp, 0, sizeof(*wsi->udp)); |
297 | 0 | } |
298 | 0 | #endif |
299 | | |
300 | 0 | lws_role_transition(wsi, 0, (type & LWS_ADOPT_ALLOW_SSL) ? LRS_SSL_INIT : |
301 | 0 | LRS_ESTABLISHED, &role_ops_raw_skt); |
302 | |
|
303 | 0 | if (vh_prot_name) |
304 | 0 | lws_bind_protocol(wsi, wsi->a.protocol, __func__); |
305 | 0 | else |
306 | | /* this is the only time he will transition */ |
307 | 0 | lws_bind_protocol(wsi, |
308 | 0 | &wsi->a.vhost->protocols[wsi->a.vhost->raw_protocol_index], |
309 | 0 | __func__); |
310 | |
|
311 | 0 | return 1; /* bound */ |
312 | 0 | } |
313 | | #endif |
314 | | |
315 | | #if defined(LWS_WITH_CLIENT) |
316 | | static int |
317 | | rops_client_bind_raw_skt(struct lws *wsi, |
318 | | const struct lws_client_connect_info *i) |
319 | 0 | { |
320 | 0 | if (!i) { |
321 | | |
322 | | /* finalize */ |
323 | |
|
324 | 0 | if (!wsi->user_space && wsi->stash->cis[CIS_METHOD]) |
325 | 0 | if (lws_ensure_user_space(wsi)) |
326 | 0 | return 1; |
327 | | |
328 | 0 | return 0; |
329 | 0 | } |
330 | | |
331 | | /* we are a fallback if nothing else matched */ |
332 | | |
333 | 0 | if (!i->local_protocol_name || |
334 | 0 | strcmp(i->local_protocol_name, "raw-proxy")) |
335 | 0 | lws_role_transition(wsi, LWSIFR_CLIENT, LRS_UNCONNECTED, |
336 | 0 | &role_ops_raw_skt); |
337 | |
|
338 | 0 | return 1; /* matched */ |
339 | 0 | } |
340 | | #endif |
341 | | |
342 | | static const lws_rops_t rops_table_raw_skt[] = { |
343 | | /* 1 */ { .handle_POLLIN = rops_handle_POLLIN_raw_skt }, |
344 | | #if defined(LWS_WITH_SERVER) |
345 | | /* 2 */ { .adoption_bind = rops_adoption_bind_raw_skt }, |
346 | | #else |
347 | | /* 2 */ { .adoption_bind = NULL }, |
348 | | #endif |
349 | | #if defined(LWS_WITH_CLIENT) |
350 | | /* 3 */ { .client_bind = rops_client_bind_raw_skt }, |
351 | | #endif |
352 | | }; |
353 | | |
354 | | const struct lws_role_ops role_ops_raw_skt = { |
355 | | /* role name */ "raw-skt", |
356 | | /* alpn id */ NULL, |
357 | | |
358 | | /* rops_table */ rops_table_raw_skt, |
359 | | /* rops_idx */ { |
360 | | /* LWS_ROPS_check_upgrades */ |
361 | | /* LWS_ROPS_pt_init_destroy */ 0x00, |
362 | | /* LWS_ROPS_init_vhost */ |
363 | | /* LWS_ROPS_destroy_vhost */ 0x00, |
364 | | /* LWS_ROPS_service_flag_pending */ |
365 | | /* LWS_ROPS_handle_POLLIN */ 0x01, |
366 | | /* LWS_ROPS_handle_POLLOUT */ |
367 | | /* LWS_ROPS_perform_user_POLLOUT */ 0x00, |
368 | | /* LWS_ROPS_callback_on_writable */ |
369 | | /* LWS_ROPS_tx_credit */ 0x00, |
370 | | /* LWS_ROPS_write_role_protocol */ |
371 | | /* LWS_ROPS_encapsulation_parent */ 0x00, |
372 | | /* LWS_ROPS_alpn_negotiated */ |
373 | | /* LWS_ROPS_close_via_role_protocol */ 0x00, |
374 | | /* LWS_ROPS_close_role */ |
375 | | /* LWS_ROPS_close_kill_connection */ 0x00, |
376 | | /* LWS_ROPS_destroy_role */ |
377 | | #if defined(LWS_WITH_SERVER) |
378 | | /* LWS_ROPS_adoption_bind */ 0x02, |
379 | | #else |
380 | | /* LWS_ROPS_adoption_bind */ 0x00, |
381 | | #endif |
382 | | #if defined(LWS_WITH_CLIENT) |
383 | | /* LWS_ROPS_client_bind */ |
384 | | /* LWS_ROPS_issue_keepalive */ 0x30, |
385 | | #else |
386 | | /* LWS_ROPS_client_bind */ |
387 | | /* LWS_ROPS_issue_keepalive */ 0x00, |
388 | | #endif |
389 | | }, |
390 | | |
391 | | /* adoption_cb clnt, srv */ { LWS_CALLBACK_RAW_CONNECTED, |
392 | | LWS_CALLBACK_RAW_ADOPT }, |
393 | | /* rx_cb clnt, srv */ { LWS_CALLBACK_RAW_RX, |
394 | | LWS_CALLBACK_RAW_RX }, |
395 | | /* writeable cb clnt, srv */ { LWS_CALLBACK_RAW_WRITEABLE, |
396 | | LWS_CALLBACK_RAW_WRITEABLE}, |
397 | | /* close cb clnt, srv */ { LWS_CALLBACK_RAW_CLOSE, |
398 | | LWS_CALLBACK_RAW_CLOSE }, |
399 | | /* protocol_bind cb c, srv */ { LWS_CALLBACK_RAW_SKT_BIND_PROTOCOL, |
400 | | LWS_CALLBACK_RAW_SKT_BIND_PROTOCOL }, |
401 | | /* protocol_unbind cb c, srv */ { LWS_CALLBACK_RAW_SKT_DROP_PROTOCOL, |
402 | | LWS_CALLBACK_RAW_SKT_DROP_PROTOCOL }, |
403 | | /* file_handle */ 0, |
404 | | }; |