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         int *interrupted;
65         /*
66          * b0 = 1: test compressible text, = 0: test uncompressible binary
67          * b1 = 1: send as a single blob, = 0: send as fragments
68          */
69 	int *options;
70 };
71 
rng(uint64_t * r)72 static uint64_t rng(uint64_t *r)
73 {
74 	*r ^= *r << 21;
75 	*r ^= *r >> 35;
76 	*r ^= *r << 4;
77 
78 	return *r;
79 }
80 
81 static int
callback_minimal_pmd_bulk(struct lws * wsi,enum lws_callback_reasons reason,void * user,void * in,size_t len)82 callback_minimal_pmd_bulk(struct lws *wsi, enum lws_callback_reasons reason,
83 			  void *user, void *in, size_t len)
84 {
85 	struct per_session_data__minimal_pmd_bulk *pss =
86 			(struct per_session_data__minimal_pmd_bulk *)user;
87         struct vhd_minimal_pmd_bulk *vhd = (struct vhd_minimal_pmd_bulk *)
88                         lws_protocol_vh_priv_get(lws_get_vhost(wsi),
89                                 lws_get_protocol(wsi));
90 	uint8_t buf[LWS_PRE + MESSAGE_SIZE], *start = &buf[LWS_PRE], *p;
91 	int n, m, flags, olen, amount;
92 
93 	switch (reason) {
94         case LWS_CALLBACK_PROTOCOL_INIT:
95                 vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
96                                 lws_get_protocol(wsi),
97                                 sizeof(struct vhd_minimal_pmd_bulk));
98                 if (!vhd)
99                         return -1;
100 
101                 /* get the pointer to "interrupted" we were passed in pvo */
102                 vhd->interrupted = (int *)lws_pvo_search(
103                         (const struct lws_protocol_vhost_options *)in,
104                         "interrupted")->value;
105                 vhd->options = (int *)lws_pvo_search(
106                         (const struct lws_protocol_vhost_options *)in,
107                         "options")->value;
108                 break;
109 
110 	case LWS_CALLBACK_ESTABLISHED:
111 		pss->rng_tx = 4;
112 		pss->rng_rx = 4;
113 		lws_callback_on_writable(wsi);
114 		break;
115 
116 	case LWS_CALLBACK_SERVER_WRITEABLE:
117 		if (pss->position_tx == MESSAGE_SIZE)
118 			break;
119 
120 		amount = MESSAGE_CHUNK_SIZE;
121 		if ((*vhd->options) & 2) {
122 			amount = MESSAGE_SIZE;
123 			lwsl_user("(writing as one blob of %d)\n", amount);
124 		}
125 
126 		/* fill up one chunk's worth of message content */
127 
128 		p = start;
129 		n = amount;
130 		if (n > MESSAGE_SIZE - pss->position_tx)
131 			n = MESSAGE_SIZE - pss->position_tx;
132 
133 		flags = lws_write_ws_flags(LWS_WRITE_BINARY, !pss->position_tx,
134 					   pss->position_tx + n == MESSAGE_SIZE);
135 
136 		/*
137 		 * select between producing compressible repeated text,
138 		 * or uncompressible PRNG output
139 		 */
140 
141 		if (*vhd->options & 1) {
142 			while (n) {
143 				size_t s;
144 
145 				m = pss->position_tx % REPEAT_STRING_LEN;
146 				s = REPEAT_STRING_LEN - m;
147 				if (s > (size_t)n)
148 					s = n;
149 				memcpy(p, &redundant_string[m], s);
150 				pss->position_tx += s;
151 				p += s;
152 				n -= s;
153 			}
154 		} else {
155 			pss->position_tx += n;
156 			while (n--)
157 				*p++ = rng(&pss->rng_tx);
158 		}
159 
160 		n = lws_ptr_diff(p, start);
161 		m = lws_write(wsi, start, n, flags);
162 		lwsl_user("LWS_CALLBACK_SERVER_WRITEABLE: wrote %d\n", n);
163 		if (m < n) {
164 			lwsl_err("ERROR %d / %d writing ws\n", m, n);
165 			return -1;
166 		}
167 		if (pss->position_tx != MESSAGE_SIZE) /* if more to do... */
168 			lws_callback_on_writable(wsi);
169 		break;
170 
171 	case LWS_CALLBACK_RECEIVE:
172 		lwsl_user("LWS_CALLBACK_RECEIVE: %4d (pss->pos=%d, rpp %5d, last %d)\n",
173 				(int)len, (int)pss->position_rx, (int)lws_remaining_packet_payload(wsi),
174 				lws_is_final_fragment(wsi));
175 		olen = len;
176 
177 		if (*vhd->options & 1) {
178 			while (len) {
179 				size_t s;
180 				m = pss->position_rx % REPEAT_STRING_LEN;
181 				s = REPEAT_STRING_LEN - m;
182 				if (s > len)
183 					s = len;
184 				if (memcmp(in, &redundant_string[m], s)) {
185 					lwsl_user("echo'd data doesn't match\n");
186 					return -1;
187 				}
188 				pss->position_rx += s;
189 				in = ((char *)in) + s;
190 				len -= s;
191 			}
192 		} else {
193 			p = (uint8_t *)in;
194 			pss->position_rx += len;
195 			while (len--) {
196 				if (*p++ != (uint8_t)rng(&pss->rng_rx)) {
197 					lwsl_user("echo'd data doesn't match: 0x%02X 0x%02X (%d)\n",
198 						*(p - 1), (int)(0x40 + (pss->rng_rx & 0x3f)),
199 						(int)((pss->position_rx - olen) + olen - len));
200 					lwsl_hexdump_notice(in, olen);
201 					return -1;
202 				}
203 			}
204 			if (pss->position_rx == MESSAGE_SIZE)
205 				pss->position_rx = 0;
206 		}
207 		break;
208 
209 	default:
210 		break;
211 	}
212 
213 	return 0;
214 }
215 
216 #define LWS_PLUGIN_PROTOCOL_MINIMAL_PMD_BULK \
217 	{ \
218 		"lws-minimal-pmd-bulk", \
219 		callback_minimal_pmd_bulk, \
220 		sizeof(struct per_session_data__minimal_pmd_bulk), \
221 		4096, \
222 		0, NULL, 0 \
223 	}
224 
225 #if !defined (LWS_PLUGIN_STATIC)
226 
227 /* boilerplate needed if we are built as a dynamic plugin */
228 
229 static const struct lws_protocols protocols[] = {
230 	LWS_PLUGIN_PROTOCOL_MINIMAL_PMD_BULK
231 };
232 
233 int
init_protocol_minimal_pmd_bulk(struct lws_context * context,struct lws_plugin_capability * c)234 init_protocol_minimal_pmd_bulk(struct lws_context *context,
235 			       struct lws_plugin_capability *c)
236 {
237 	if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
238 		lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
239 			 c->api_magic);
240 		return 1;
241 	}
242 
243 	c->protocols = protocols;
244 	c->count_protocols = LWS_ARRAY_SIZE(protocols);
245 	c->extensions = NULL;
246 	c->count_extensions = 0;
247 
248 	return 0;
249 }
250 
251 int
destroy_protocol_minimal_pmd_bulk(struct lws_context * context)252 destroy_protocol_minimal_pmd_bulk(struct lws_context *context)
253 {
254 	return 0;
255 }
256 #endif
257