• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * ws protocol handler plugin for "lws-minimal-pmd-bulk"
3  *
4  * Written in 2010-2019 by Andy Green <andy@warmcat.com>
5  *
6  * This file is made available under the Creative Commons CC0 1.0
7  * Universal Public Domain Dedication.
8  *
9  * The protocol shows how to send and receive bulk messages over a ws connection
10  * that optionally may have the permessage-deflate extension negotiated on it.
11  */
12 
13 #if !defined (LWS_PLUGIN_STATIC)
14 #define LWS_DLL
15 #define LWS_INTERNAL
16 #include <libwebsockets.h>
17 #endif
18 
19 #include <string.h>
20 
21 /*
22  * We will produce a large ws message either from this text repeated many times,
23  * or from 0x40 + a 6-bit pseudorandom number
24  */
25 
26 static const char * const redundant_string =
27 	"No one would have believed in the last years of the nineteenth "
28 	"century that this world was being watched keenly and closely by "
29 	"intelligences greater than man's and yet as mortal as his own; that as "
30 	"men busied themselves about their various concerns they were "
31 	"scrutinised and studied, perhaps almost as narrowly as a man with a "
32 	"microscope might scrutinise the transient creatures that swarm and "
33 	"multiply in a drop of water.  With infinite complacency men went to "
34 	"and fro over this globe about their little affairs, serene in their "
35 	"assurance of their empire over matter. It is possible that the "
36 	"infusoria under the microscope do the same.  No one gave a thought to "
37 	"the older worlds of space as sources of human danger, or thought of "
38 	"them only to dismiss the idea of life upon them as impossible or "
39 	"improbable.  It is curious to recall some of the mental habits of "
40 	"those departed days.  At most terrestrial men fancied there might be "
41 	"other men upon Mars, perhaps inferior to themselves and ready to "
42 	"welcome a missionary enterprise. Yet across the gulf of space, minds "
43 	"that are to our minds as ours are to those of the beasts that perish, "
44 	"intellects vast and cool and unsympathetic, regarded this earth with "
45 	"envious eyes, and slowly and surely drew their plans against us.  And "
46 	"early in the twentieth century came the great disillusionment. "
47 ;
48 
49 /* this reflects the length of the string above */
50 #define REPEAT_STRING_LEN 1337
51 /* this is the total size of the ws message we will send */
52 #define MESSAGE_SIZE (100 * REPEAT_STRING_LEN)
53 /* this is how much we will send each time the connection is writable */
54 #define MESSAGE_CHUNK_SIZE (1 * 1024)
55 
56 /* one of these is created for each client connecting to us */
57 
58 struct per_session_data__minimal_pmd_bulk {
59 	int position_tx, position_rx;
60 	uint64_t rng_rx, rng_tx;
61 };
62 
63 struct vhd_minimal_pmd_bulk {
64 	struct lws_context *context;
65 	struct lws_vhost *vhost;
66 	struct lws *client_wsi;
67 
68 	lws_sorted_usec_list_t sul;
69 
70 	int *interrupted;
71 	int *options;
72 };
73 
rng(uint64_t * r)74 static uint64_t rng(uint64_t *r)
75 {
76         *r ^= *r << 21;
77         *r ^= *r >> 35;
78         *r ^= *r << 4;
79 
80         return *r;
81 }
82 
83 static void
sul_connect_attempt(struct lws_sorted_usec_list * sul)84 sul_connect_attempt(struct lws_sorted_usec_list *sul)
85 {
86 	struct vhd_minimal_pmd_bulk *vhd =
87 		lws_container_of(sul, struct vhd_minimal_pmd_bulk, sul);
88 	struct lws_client_connect_info i;
89 
90 	memset(&i, 0, sizeof(i));
91 
92 	i.context = vhd->context;
93 	i.port = 7681;
94 	i.address = "localhost";
95 	i.path = "/";
96 	i.host = i.address;
97 	i.origin = i.address;
98 	i.ssl_connection = 0;
99 	i.vhost = vhd->vhost;
100 	i.protocol = "lws-minimal-pmd-bulk";
101 	i.pwsi = &vhd->client_wsi;
102 
103 	if (!lws_client_connect_via_info(&i))
104 		lws_sul_schedule(vhd->context, 0, &vhd->sul,
105 				 sul_connect_attempt, 10 * LWS_US_PER_SEC);
106 }
107 
108 static int
callback_minimal_pmd_bulk(struct lws * wsi,enum lws_callback_reasons reason,void * user,void * in,size_t len)109 callback_minimal_pmd_bulk(struct lws *wsi, enum lws_callback_reasons reason,
110 			  void *user, void *in, size_t len)
111 {
112 	struct per_session_data__minimal_pmd_bulk *pss =
113 			(struct per_session_data__minimal_pmd_bulk *)user;
114 	struct vhd_minimal_pmd_bulk *vhd = (struct vhd_minimal_pmd_bulk *)
115 			lws_protocol_vh_priv_get(lws_get_vhost(wsi),
116 				lws_get_protocol(wsi));
117 	uint8_t buf[LWS_PRE + MESSAGE_CHUNK_SIZE], *start = &buf[LWS_PRE], *p;
118 	int n, m, flags;
119 
120 	switch (reason) {
121 
122 	case LWS_CALLBACK_PROTOCOL_INIT:
123 		vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
124 				lws_get_protocol(wsi),
125 				sizeof(struct vhd_minimal_pmd_bulk));
126 		if (!vhd)
127 			return -1;
128 
129 		vhd->context = lws_get_context(wsi);
130 		vhd->vhost = lws_get_vhost(wsi);
131 
132 		/* get the pointer to "interrupted" we were passed in pvo */
133 		vhd->interrupted = (int *)lws_pvo_search(
134 			(const struct lws_protocol_vhost_options *)in,
135 			"interrupted")->value;
136 		vhd->options = (int *)lws_pvo_search(
137 			(const struct lws_protocol_vhost_options *)in,
138 			"options")->value;
139 
140 		sul_connect_attempt(&vhd->sul);
141 		break;
142 
143 	case LWS_CALLBACK_PROTOCOL_DESTROY:
144 		lws_sul_cancel(&vhd->sul);
145 		break;
146 
147 	case LWS_CALLBACK_CLIENT_ESTABLISHED:
148 		pss->rng_tx = 4;
149 		pss->rng_rx = 4;
150 		lws_callback_on_writable(wsi);
151 		break;
152 
153 	case LWS_CALLBACK_CLIENT_WRITEABLE:
154 
155 		/*
156 		 * when we connect, we will send the server a message
157 		 */
158 
159 		if (pss->position_tx == MESSAGE_SIZE)
160 			break;
161 
162 		/* fill up one chunk's worth of message content */
163 
164 		p = start;
165 		n = MESSAGE_CHUNK_SIZE;
166 		if (n > MESSAGE_SIZE - pss->position_tx)
167 			n = MESSAGE_SIZE - pss->position_tx;
168 
169 		flags = lws_write_ws_flags(LWS_WRITE_BINARY, !pss->position_tx,
170 					   pss->position_tx + n == MESSAGE_SIZE);
171 
172 		/*
173 		 * select between producing compressible repeated text,
174 		 * or uncompressible PRNG output
175 		 */
176 
177 		if (*vhd->options & 1) {
178 			while (n) {
179 				size_t s;
180 
181 				m = pss->position_tx % REPEAT_STRING_LEN;
182 				s = (unsigned int)(REPEAT_STRING_LEN - m);
183 				if (s > (size_t)n)
184 					s = (unsigned int)n;
185 				memcpy(p, &redundant_string[m], s);
186 				pss->position_tx += (int)s;
187 				p += s;
188 				n -= (int)s;
189 			}
190 		} else {
191 			pss->position_tx += n;
192 			while (n--)
193 				*p++ = (uint8_t)rng(&pss->rng_tx);
194 		}
195 
196 		n = lws_ptr_diff(p, start);
197 		m = lws_write(wsi, start, (unsigned int)n, (enum lws_write_protocol)flags);
198 		if (m < n) {
199 			lwsl_err("ERROR %d writing ws\n", m);
200 			return -1;
201 		}
202 		if (pss->position_tx != MESSAGE_SIZE) /* if more to do... */
203 			lws_callback_on_writable(wsi);
204 		else
205 			/* if we sent and received everything */
206 			if (pss->position_rx == MESSAGE_SIZE)
207 				*vhd->interrupted = 2;
208 		break;
209 
210 	case LWS_CALLBACK_CLIENT_RECEIVE:
211 
212 		/*
213 		 * When we connect, the server will send us a message too
214 		 */
215 
216 		lwsl_user("LWS_CALLBACK_CLIENT_RECEIVE: %4d (rpp %5d, last %d)\n",
217 			(int)len, (int)lws_remaining_packet_payload(wsi),
218 			lws_is_final_fragment(wsi));
219 
220 		if (*vhd->options & 1) {
221 			while (len) {
222 				size_t s;
223 
224 				m = pss->position_rx % REPEAT_STRING_LEN;
225 				s = (unsigned int)(REPEAT_STRING_LEN - m);
226 				if (s > len)
227 					s = len;
228 				if (memcmp(in, &redundant_string[m], s)) {
229 					lwsl_user("echo'd data doesn't match\n");
230 					return -1;
231 				}
232 				pss->position_rx += (int)s;
233 				in = ((unsigned char *)in) + s;
234 				len -= s;
235 			}
236 		} else {
237 			p = (uint8_t *)in;
238 			pss->position_rx += (int)len;
239 			while (len--)
240 				if (*p++ != (uint8_t)rng(&pss->rng_rx)) {
241 					lwsl_user("echo'd data doesn't match\n");
242 					return -1;
243 				}
244 		}
245 
246 		/* if we sent and received everything */
247 
248 		if (pss->position_rx == MESSAGE_SIZE &&
249 		    pss->position_tx == MESSAGE_SIZE)
250 			*vhd->interrupted = 2;
251 
252 		break;
253 
254 	case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
255 		lwsl_err("CLIENT_CONNECTION_ERROR: %s\n",
256 			 in ? (char *)in : "(null)");
257 		vhd->client_wsi = NULL;
258 		lws_sul_schedule(vhd->context, 0, &vhd->sul,
259 				 sul_connect_attempt, LWS_US_PER_SEC);
260 		break;
261 
262 	case LWS_CALLBACK_CLIENT_CLOSED:
263 		vhd->client_wsi = NULL;
264 		lws_sul_schedule(vhd->context, 0, &vhd->sul,
265 				 sul_connect_attempt, LWS_US_PER_SEC);
266 		break;
267 
268 	default:
269 		break;
270 	}
271 
272 	return 0;
273 }
274 
275 #define LWS_PLUGIN_PROTOCOL_MINIMAL_PMD_BULK \
276 	{ \
277 		"lws-minimal-pmd-bulk", \
278 		callback_minimal_pmd_bulk, \
279 		sizeof(struct per_session_data__minimal_pmd_bulk), \
280 		4096, \
281 		0, NULL, 0 \
282 	}
283