• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <private-lib-core.h>
26 
27 static int
rops_handle_POLLIN_raw_proxy(struct lws_context_per_thread * pt,struct lws * wsi,struct lws_pollfd * pollfd)28 rops_handle_POLLIN_raw_proxy(struct lws_context_per_thread *pt, struct lws *wsi,
29 			     struct lws_pollfd *pollfd)
30 {
31 	struct lws_tokens ebuf;
32 	int n, buffered;
33 
34 	/* pending truncated sends have uber priority */
35 
36 	if (lws_has_buffered_out(wsi)) {
37 		if (!(pollfd->revents & LWS_POLLOUT))
38 			return LWS_HPI_RET_HANDLED;
39 
40 		/* drain the output buflist */
41 		if (lws_issue_raw(wsi, NULL, 0) < 0)
42 			goto fail;
43 		/*
44 		 * we can't afford to allow input processing to send
45 		 * something new, so spin around he event loop until
46 		 * he doesn't have any partials
47 		 */
48 		return LWS_HPI_RET_HANDLED;
49 	}
50 
51 	if (lwsi_state(wsi) == LRS_WAITING_CONNECT)
52 		goto try_pollout;
53 
54 	if ((pollfd->revents & pollfd->events & LWS_POLLIN) &&
55 	    /* any tunnel has to have been established... */
56 	    lwsi_state(wsi) != LRS_SSL_ACK_PENDING &&
57 	    !(wsi->favoured_pollin &&
58 	      (pollfd->revents & pollfd->events & LWS_POLLOUT))) {
59 
60 		ebuf.token = NULL;
61 		ebuf.len = 0;
62 		buffered = lws_buflist_aware_read(pt, wsi, &ebuf, 1, __func__);
63 		if (buffered < 0)
64 			goto fail;
65 
66 		switch (ebuf.len) {
67 		case 0:
68 			lwsl_info("%s: read 0 len\n", __func__);
69 			wsi->seen_zero_length_recv = 1;
70 			if (lws_change_pollfd(wsi, LWS_POLLIN, 0))
71 				goto fail;
72 
73 			/*
74 			 * we need to go to fail here, since it's the only
75 			 * chance we get to understand that the socket has
76 			 * closed
77 			 */
78 			// goto try_pollout;
79 			goto fail;
80 
81 		case LWS_SSL_CAPABLE_ERROR:
82 			goto fail;
83 		case LWS_SSL_CAPABLE_MORE_SERVICE:
84 			goto try_pollout;
85 		}
86 		n = user_callback_handle_rxflow(wsi->a.protocol->callback,
87 						wsi, lwsi_role_client(wsi) ?
88 						 LWS_CALLBACK_RAW_PROXY_CLI_RX :
89 						 LWS_CALLBACK_RAW_PROXY_SRV_RX,
90 						wsi->user_space, ebuf.token,
91 						(size_t)ebuf.len);
92 		if (n < 0) {
93 			lwsl_info("LWS_CALLBACK_RAW_PROXY_*_RX fail\n");
94 			goto fail;
95 		}
96 
97 		if (lws_buflist_aware_finished_consuming(wsi, &ebuf, ebuf.len,
98 							 buffered, __func__))
99 			return LWS_HPI_RET_PLEASE_CLOSE_ME;
100 	} else
101 		if (wsi->favoured_pollin &&
102 		    (pollfd->revents & pollfd->events & LWS_POLLOUT))
103 			/* we balanced the last favouring of pollin */
104 			wsi->favoured_pollin = 0;
105 
106 try_pollout:
107 
108 	if (!(pollfd->revents & LWS_POLLOUT))
109 		return LWS_HPI_RET_HANDLED;
110 
111 	if (lws_handle_POLLOUT_event(wsi, pollfd)) {
112 		lwsl_debug("POLLOUT event closed it\n");
113 		return LWS_HPI_RET_PLEASE_CLOSE_ME;
114 	}
115 
116 #if defined(LWS_WITH_CLIENT)
117 	if (lws_http_client_socket_service(wsi, pollfd))
118 		return LWS_HPI_RET_WSI_ALREADY_DIED;
119 #endif
120 
121 	return LWS_HPI_RET_HANDLED;
122 
123 fail:
124 	lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS, "raw svc fail");
125 
126 	return LWS_HPI_RET_WSI_ALREADY_DIED;
127 }
128 
129 static int
rops_adoption_bind_raw_proxy(struct lws * wsi,int type,const char * vh_prot_name)130 rops_adoption_bind_raw_proxy(struct lws *wsi, int type,
131 			     const char *vh_prot_name)
132 {
133 	/* no http but socket... must be raw skt */
134 	if ((type & LWS_ADOPT_HTTP) || !(type & LWS_ADOPT_SOCKET) ||
135 	    (!(type & LWS_ADOPT_FLAG_RAW_PROXY)) || (type & _LWS_ADOPT_FINISH))
136 		return 0; /* no match */
137 
138 #if defined(LWS_WITH_UDP)
139 	if (type & LWS_ADOPT_FLAG_UDP)
140 		/*
141 		 * these can be >128 bytes, so just alloc for UDP
142 		 */
143 		wsi->udp = lws_malloc(sizeof(*wsi->udp), "udp struct");
144 #endif
145 
146 	lws_role_transition(wsi, LWSIFR_SERVER, (type & LWS_ADOPT_ALLOW_SSL) ?
147 				    LRS_SSL_INIT : LRS_ESTABLISHED,
148 			    &role_ops_raw_proxy);
149 
150 	if (vh_prot_name)
151 		lws_bind_protocol(wsi, wsi->a.protocol, __func__);
152 	else
153 		/* this is the only time he will transition */
154 		lws_bind_protocol(wsi,
155 			&wsi->a.vhost->protocols[wsi->a.vhost->raw_protocol_index],
156 			__func__);
157 
158 	return 1; /* bound */
159 }
160 
161 static int
rops_client_bind_raw_proxy(struct lws * wsi,const struct lws_client_connect_info * i)162 rops_client_bind_raw_proxy(struct lws *wsi,
163 			   const struct lws_client_connect_info *i)
164 {
165 	if (!i) {
166 
167 		/* finalize */
168 
169 		if (!wsi->user_space && wsi->stash->cis[CIS_METHOD])
170 			if (lws_ensure_user_space(wsi))
171 				return 1;
172 
173 		return 0;
174 	}
175 
176 	/* we are a fallback if nothing else matched */
177 
178 	if (i->local_protocol_name && !strcmp(i->local_protocol_name, "raw-proxy"))
179 		lws_role_transition(wsi, LWSIFR_CLIENT, LRS_UNCONNECTED,
180 				    &role_ops_raw_proxy);
181 
182 	return 0;
183 }
184 
185 static int
rops_handle_POLLOUT_raw_proxy(struct lws * wsi)186 rops_handle_POLLOUT_raw_proxy(struct lws *wsi)
187 {
188 	if (lwsi_state(wsi) == LRS_ESTABLISHED)
189 		return LWS_HP_RET_USER_SERVICE;
190 
191 	if (lwsi_role_client(wsi))
192 		return LWS_HP_RET_USER_SERVICE;
193 
194 	return LWS_HP_RET_BAIL_OK;
195 }
196 
197 static const lws_rops_t rops_table_raw_proxy[] = {
198 	/*  1 */ { .handle_POLLIN	= rops_handle_POLLIN_raw_proxy },
199 	/*  2 */ { .handle_POLLOUT	= rops_handle_POLLOUT_raw_proxy },
200 	/*  3 */ { .adoption_bind	= rops_adoption_bind_raw_proxy },
201 	/*  4 */ { .client_bind		= rops_client_bind_raw_proxy },
202 };
203 
204 
205 const struct lws_role_ops role_ops_raw_proxy = {
206 	/* role name */			"raw-proxy",
207 	/* alpn id */			NULL,
208 
209 	/* rops_table */		rops_table_raw_proxy,
210 	/* rops_idx */			{
211 	  /* LWS_ROPS_check_upgrades */
212 	  /* LWS_ROPS_pt_init_destroy */		0x00,
213 	  /* LWS_ROPS_init_vhost */
214 	  /* LWS_ROPS_destroy_vhost */			0x00,
215 	  /* LWS_ROPS_service_flag_pending */
216 	  /* LWS_ROPS_handle_POLLIN */			0x01,
217 	  /* LWS_ROPS_handle_POLLOUT */
218 	  /* LWS_ROPS_perform_user_POLLOUT */		0x20,
219 	  /* LWS_ROPS_callback_on_writable */
220 	  /* LWS_ROPS_tx_credit */			0x00,
221 	  /* LWS_ROPS_write_role_protocol */
222 	  /* LWS_ROPS_encapsulation_parent */		0x00,
223 	  /* LWS_ROPS_alpn_negotiated */
224 	  /* LWS_ROPS_close_via_role_protocol */	0x00,
225 	  /* LWS_ROPS_close_role */
226 	  /* LWS_ROPS_close_kill_connection */		0x00,
227 	  /* LWS_ROPS_destroy_role */
228 	  /* LWS_ROPS_adoption_bind */			0x03,
229 	  /* LWS_ROPS_client_bind */
230 	  /* LWS_ROPS_issue_keepalive */		0x40,
231 					},
232 
233 	/* adoption_cb clnt, srv */	{ LWS_CALLBACK_RAW_PROXY_CLI_ADOPT,
234 					  LWS_CALLBACK_RAW_PROXY_SRV_ADOPT },
235 	/* rx_cb clnt, srv */		{ LWS_CALLBACK_RAW_PROXY_CLI_RX,
236 					  LWS_CALLBACK_RAW_PROXY_SRV_RX },
237 	/* writeable cb clnt, srv */	{ LWS_CALLBACK_RAW_PROXY_CLI_WRITEABLE,
238 					  LWS_CALLBACK_RAW_PROXY_SRV_WRITEABLE, },
239 	/* close cb clnt, srv */	{ LWS_CALLBACK_RAW_PROXY_CLI_CLOSE,
240 					  LWS_CALLBACK_RAW_PROXY_SRV_CLOSE },
241 	/* protocol_bind cb c, srv */	{ LWS_CALLBACK_RAW_PROXY_CLI_BIND_PROTOCOL,
242 					  LWS_CALLBACK_RAW_PROXY_SRV_BIND_PROTOCOL },
243 	/* protocol_unbind cb c, srv */	{ LWS_CALLBACK_RAW_PROXY_CLI_DROP_PROTOCOL,
244 					  LWS_CALLBACK_RAW_PROXY_SRV_DROP_PROTOCOL },
245 	/* file_handle */		0,
246 };
247