1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2019 - 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
secstream_ws(struct lws * wsi,enum lws_callback_reasons reason,void * user,void * in,size_t len)28 secstream_ws(struct lws *wsi, enum lws_callback_reasons reason, void *user,
29 	     void *in, size_t len)
30 {
31 	lws_ss_handle_t *h = (lws_ss_handle_t *)lws_get_opaque_user_data(wsi);
32 	uint8_t buf[LWS_PRE + 1400];
33 	size_t buflen;
34 	int f = 0, f1;
35 
36 	switch (reason) {
37 
38 	/* because we are protocols[0] ... */
39 	case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
40 		lwsl_info("%s: CLIENT_CONNECTION_ERROR: %s\n", __func__,
41 			 in ? (char *)in : "(null)");
42 		if (!h)
43 			break;
44 		lws_ss_event_helper(h, LWSSSCS_UNREACHABLE);
45 		h->wsi = NULL;
46 		lws_ss_backoff(h);
47 		break;
48 
49 	case LWS_CALLBACK_CLIENT_CLOSED:
50 		if (!h)
51 			break;
52 		f = lws_ss_event_helper(h, LWSSSCS_DISCONNECTED);
53 		if (h->wsi)
54 			lws_set_opaque_user_data(h->wsi, NULL);
55 		h->wsi = NULL;
56 
57 		if (f) {
58 			lws_ss_destroy(&h);
59 			break;
60 		}
61 
62 		if (h->policy && !(h->policy->flags & LWSSSPOLF_OPPORTUNISTIC) &&
63 		    !h->txn_ok && !wsi->context->being_destroyed)
64 			lws_ss_backoff(h);
65 		break;
66 
67 	case LWS_CALLBACK_CLIENT_ESTABLISHED:
68 		h->retry = 0;
69 		h->seqstate = SSSEQ_CONNECTED;
70 		lws_ss_set_timeout_us(h, LWS_SET_TIMER_USEC_CANCEL);
71 		lws_ss_event_helper(h, LWSSSCS_CONNECTED);
72 		break;
73 
74 	case LWS_CALLBACK_CLIENT_RECEIVE:
75 		// lwsl_user("LWS_CALLBACK_CLIENT_RECEIVE: read %d\n", (int)len);
76 		if (!h)
77 			return 0;
78 		if (lws_is_first_fragment(wsi))
79 			f |= LWSSS_FLAG_SOM;
80 		if (lws_is_final_fragment(wsi))
81 			f |= LWSSS_FLAG_EOM;
82 		// lws_frame_is_binary(wsi);
83 
84 		h->subseq = 1;
85 
86 		h->info.rx(ss_to_userobj(h), (const uint8_t *)in, len, f);
87 
88 		return 0; /* don't passthru */
89 
90 	case LWS_CALLBACK_CLIENT_WRITEABLE:
91 		if (!h)
92 			return 0;
93 		// lwsl_notice("%s: ss %p: WRITEABLE\n", __func__, h);
94 
95 		if (h->seqstate != SSSEQ_CONNECTED) {
96 			lwsl_warn("%s: seqstate %d\n", __func__, h->seqstate);
97 			break;
98 		}
99 
100 		buflen = sizeof(buf) - LWS_PRE;
101 		if (h->info.tx(ss_to_userobj(h),  h->txord++, buf + LWS_PRE,
102 				&buflen, &f))
103 			/* don't want to send anything */
104 			return 0;
105 
106 		f1 = lws_write_ws_flags(LWS_WRITE_BINARY,
107 					!!(f & LWSSS_FLAG_SOM),
108 					!!(f & LWSSS_FLAG_EOM));
109 
110 		if (lws_write(wsi, buf + LWS_PRE, buflen, f1) != (int)buflen) {
111 			lwsl_err("%s: write failed\n", __func__);
112 			return -1;
113 		}
114 
115 		return 0;
116 
117 	default:
118 		break;
119 	}
120 
121 	return lws_callback_http_dummy(wsi, reason, user, in, len);
122 }
123 
124 const struct lws_protocols protocol_secstream_ws = {
125 	"lws-secstream-ws",
126 	secstream_ws,
127 	0,
128 	0,
129 };
130 /*
131  * Munge connect info according to protocol-specific considerations... this
132  * usually means interpreting aux in a protocol-specific way and using the
133  * pieces at connection setup time, eg, http url pieces.
134  *
135  * len bytes of buf can be used for things with scope until after the actual
136  * connect.
137  *
138  * For ws, protocol aux is <url path>;<ws subprotocol name>
139  */
140 
141 static int
secstream_connect_munge_ws(lws_ss_handle_t * h,char * buf,size_t len,struct lws_client_connect_info * i,union lws_ss_contemp * ct)142 secstream_connect_munge_ws(lws_ss_handle_t *h, char *buf, size_t len,
143 			   struct lws_client_connect_info *i,
144 			   union lws_ss_contemp *ct)
145 {
146 	lwsl_notice("%s\n", __func__);
147 
148 	if (!h->policy->u.http.url)
149 		return 0;
150 
151 	/* protocol aux is the path part ; ws subprotocol name */
152 
153 	i->path = h->policy->u.http.url;
154 	lws_snprintf(buf, len, "/%s", h->policy->u.http.url);
155 
156 	i->protocol = h->policy->u.http.u.ws.subprotocol;
157 
158 	lwsl_notice("%s: url %s, ws subprotocol %s\n", __func__, buf, i->protocol);
159 
160 	return 0;
161 }
162 
163 const struct ss_pcols ss_pcol_ws = {
164 	"ws",  "http/1.1",  "lws-secstream-ws", secstream_connect_munge_ws
165 };
166