• 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 ((pollfd->revents & pollfd->events & LWS_POLLIN) &&
52 	    /* any tunnel has to have been established... */
53 	    lwsi_state(wsi) != LRS_SSL_ACK_PENDING &&
54 	    !(wsi->favoured_pollin &&
55 	      (pollfd->revents & pollfd->events & LWS_POLLOUT))) {
56 
57 		ebuf.token = NULL;
58 		ebuf.len = 0;
59 		buffered = lws_buflist_aware_read(pt, wsi, &ebuf, 1, __func__);
60 		if (buffered < 0)
61 			goto fail;
62 
63 		switch (ebuf.len) {
64 		case 0:
65 			lwsl_info("%s: read 0 len\n", __func__);
66 			wsi->seen_zero_length_recv = 1;
67 			if (lws_change_pollfd(wsi, LWS_POLLIN, 0))
68 				goto fail;
69 
70 			/*
71 			 * we need to go to fail here, since it's the only
72 			 * chance we get to understand that the socket has
73 			 * closed
74 			 */
75 			// goto try_pollout;
76 			goto fail;
77 
78 		case LWS_SSL_CAPABLE_ERROR:
79 			goto fail;
80 		case LWS_SSL_CAPABLE_MORE_SERVICE:
81 			goto try_pollout;
82 		}
83 		n = user_callback_handle_rxflow(wsi->protocol->callback,
84 						wsi, lwsi_role_client(wsi) ?
85 						 LWS_CALLBACK_RAW_PROXY_CLI_RX :
86 						 LWS_CALLBACK_RAW_PROXY_SRV_RX,
87 						wsi->user_space, ebuf.token,
88 						ebuf.len);
89 		if (n < 0) {
90 			lwsl_info("LWS_CALLBACK_RAW_PROXY_*_RX fail\n");
91 			goto fail;
92 		}
93 
94 		if (lws_buflist_aware_finished_consuming(wsi, &ebuf, ebuf.len,
95 							 buffered, __func__))
96 			return LWS_HPI_RET_PLEASE_CLOSE_ME;
97 	} else
98 		if (wsi->favoured_pollin &&
99 		    (pollfd->revents & pollfd->events & LWS_POLLOUT))
100 			/* we balanced the last favouring of pollin */
101 			wsi->favoured_pollin = 0;
102 
103 try_pollout:
104 
105 	if (!(pollfd->revents & LWS_POLLOUT))
106 		return LWS_HPI_RET_HANDLED;
107 
108 	if (lws_handle_POLLOUT_event(wsi, pollfd)) {
109 		lwsl_debug("POLLOUT event closed it\n");
110 		return LWS_HPI_RET_PLEASE_CLOSE_ME;
111 	}
112 
113 #if defined(LWS_WITH_CLIENT)
114 	if (lws_client_socket_service(wsi, pollfd))
115 		return LWS_HPI_RET_WSI_ALREADY_DIED;
116 #endif
117 
118 	return LWS_HPI_RET_HANDLED;
119 
120 fail:
121 	lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS, "raw svc fail");
122 
123 	return LWS_HPI_RET_WSI_ALREADY_DIED;
124 }
125 
126 static int
rops_adoption_bind_raw_proxy(struct lws * wsi,int type,const char * vh_prot_name)127 rops_adoption_bind_raw_proxy(struct lws *wsi, int type,
128 			     const char *vh_prot_name)
129 {
130 	/* no http but socket... must be raw skt */
131 	if ((type & LWS_ADOPT_HTTP) || !(type & LWS_ADOPT_SOCKET) ||
132 	    (!(type & LWS_ADOPT_FLAG_RAW_PROXY)) || (type & _LWS_ADOPT_FINISH))
133 		return 0; /* no match */
134 
135 #if defined(LWS_WITH_UDP)
136 	if (type & LWS_ADOPT_FLAG_UDP)
137 		/*
138 		 * these can be >128 bytes, so just alloc for UDP
139 		 */
140 		wsi->udp = lws_malloc(sizeof(*wsi->udp), "udp struct");
141 #endif
142 
143 	lws_role_transition(wsi, LWSIFR_SERVER, (type & LWS_ADOPT_ALLOW_SSL) ?
144 				    LRS_SSL_INIT : LRS_ESTABLISHED,
145 			    &role_ops_raw_proxy);
146 
147 	if (vh_prot_name)
148 		lws_bind_protocol(wsi, wsi->protocol, __func__);
149 	else
150 		/* this is the only time he will transition */
151 		lws_bind_protocol(wsi,
152 			&wsi->vhost->protocols[wsi->vhost->raw_protocol_index],
153 			__func__);
154 
155 	return 1; /* bound */
156 }
157 
158 static int
rops_client_bind_raw_proxy(struct lws * wsi,const struct lws_client_connect_info * i)159 rops_client_bind_raw_proxy(struct lws *wsi,
160 			   const struct lws_client_connect_info *i)
161 {
162 	if (!i) {
163 
164 		/* finalize */
165 
166 		if (!wsi->user_space && wsi->stash->cis[CIS_METHOD])
167 			if (lws_ensure_user_space(wsi))
168 				return 1;
169 
170 		return 0;
171 	}
172 
173 	/* we are a fallback if nothing else matched */
174 
175 	if (i->local_protocol_name && !strcmp(i->local_protocol_name, "raw-proxy"))
176 		lws_role_transition(wsi, LWSIFR_CLIENT, LRS_UNCONNECTED,
177 				    &role_ops_raw_proxy);
178 
179 	return 0;
180 }
181 
182 static int
rops_handle_POLLOUT_raw_proxy(struct lws * wsi)183 rops_handle_POLLOUT_raw_proxy(struct lws *wsi)
184 {
185 	if (lwsi_state(wsi) == LRS_ESTABLISHED)
186 		return LWS_HP_RET_USER_SERVICE;
187 
188 	if (lwsi_role_client(wsi))
189 		return LWS_HP_RET_USER_SERVICE;
190 
191 	return LWS_HP_RET_BAIL_OK;
192 }
193 
194 const struct lws_role_ops role_ops_raw_proxy = {
195 	/* role name */			"raw-proxy",
196 	/* alpn id */			NULL,
197 	/* check_upgrades */		NULL,
198 	/* pt_init_destroy */		NULL,
199 	/* init_vhost */		NULL,
200 	/* destroy_vhost */		NULL,
201 	/* service_flag_pending */	NULL,
202 	/* handle_POLLIN */		rops_handle_POLLIN_raw_proxy,
203 	/* handle_POLLOUT */		rops_handle_POLLOUT_raw_proxy,
204 	/* perform_user_POLLOUT */	NULL,
205 	/* callback_on_writable */	NULL,
206 	/* tx_credit */			NULL,
207 	/* write_role_protocol */	NULL,
208 	/* encapsulation_parent */	NULL,
209 	/* alpn_negotiated */		NULL,
210 	/* close_via_role_protocol */	NULL,
211 	/* close_role */		NULL,
212 	/* close_kill_connection */	NULL,
213 	/* destroy_role */		NULL,
214 	/* adoption_bind */		rops_adoption_bind_raw_proxy,
215 	/* client_bind */		rops_client_bind_raw_proxy,
216 	/* issue_keepalive */		NULL,
217 	/* adoption_cb clnt, srv */	{ LWS_CALLBACK_RAW_PROXY_CLI_ADOPT,
218 					  LWS_CALLBACK_RAW_PROXY_SRV_ADOPT },
219 	/* rx_cb clnt, srv */		{ LWS_CALLBACK_RAW_PROXY_CLI_RX,
220 					  LWS_CALLBACK_RAW_PROXY_SRV_RX },
221 	/* writeable cb clnt, srv */	{ LWS_CALLBACK_RAW_PROXY_CLI_WRITEABLE,
222 					  LWS_CALLBACK_RAW_PROXY_SRV_WRITEABLE, },
223 	/* close cb clnt, srv */	{ LWS_CALLBACK_RAW_PROXY_CLI_CLOSE,
224 					  LWS_CALLBACK_RAW_PROXY_SRV_CLOSE },
225 	/* protocol_bind cb c, srv */	{ LWS_CALLBACK_RAW_PROXY_CLI_BIND_PROTOCOL,
226 					  LWS_CALLBACK_RAW_PROXY_SRV_BIND_PROTOCOL },
227 	/* protocol_unbind cb c, srv */	{ LWS_CALLBACK_RAW_PROXY_CLI_DROP_PROTOCOL,
228 					  LWS_CALLBACK_RAW_PROXY_SRV_DROP_PROTOCOL },
229 	/* file_handle */		0,
230 };
231