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