File: multi.c

package info (click to toggle)
libwebsockets 4.3.5-1
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 30,588 kB
  • sloc: ansic: 194,407; javascript: 1,550; sh: 1,387; cpp: 505; java: 461; perl: 405; xml: 118; makefile: 76; awk: 5
file content (419 lines) | stat: -rw-r--r-- 11,271 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 * lws-minimal-secure-streams-smd
 *
 * Written in 2010-2021 by Andy Green <andy@warmcat.com>
 *
 * This file is made available under the Creative Commons CC0 1.0
 * Universal Public Domain Dedication.
 *
 *
 * This demonstrates a minimal http client using secure streams to access the
 * SMD api.  This file is only built when LWS_SS_USE_SSPC defined.
 *
 * This is an alternative test implementation selected by --multi at runtime,
 * it's in its own file to stop muddying up the main test sources.  It's only
 * available when built with SSPC / produces -client executable.
 *
 * We will fork several times, the original thread and the forks hook up to
 * the proxy with smd SS, each fork waits a second for everyone to have joined,
 * and then each fork (NOT the original process) sends a bunch of user messages
 * that all the forks should receive, having been distributed by SMD and the
 * ss proxy.
 *
 * The participants check they received all the messages expected from everyone
 * and then send a final message indicating success and exits.  The original
 * fork is watching for these to arrive before the timeout, if so it's a PASS.
 */

#include <libwebsockets.h>
#include <string.h>
#include <signal.h>

static int bad = 1, interrupted;

/* number of forks */
#define FORKS 4
/* number of messages each will send, eg, 4 forks 64 message == 256 messages */
#define MSGCOUNT 64

typedef struct myss {
	struct lws_ss_handle 		*ss;
	void				*opaque_data;
	/* ... application specific state ... */
	uint64_t			seen_mask[FORKS];
	int				seen_msgs[FORKS];
	lws_sorted_usec_list_t		sul;
	int				count;
	char				seen_all;
	char				send_seen_all;
	char				starting;
} myss_t;


/* secure streams payload interface */

static lws_ss_state_return_t
multi_myss_rx(void *userobj, const uint8_t *buf, size_t len, int flags)
{
	myss_t *m = (myss_t *)userobj;
	const char *p;
	int fk, t, n;
	size_t al;

	/* ignore our and other forks announcing their result */

	if (lws_json_simple_find((const char *)buf, len, "\"seen_all\":", &al))
		return LWSSSSRET_OK;

	/*
	 * otherwise once we saw the expected messages, any other messages
	 * coming in this class are wrong
	 */

	if (m->seen_all) {
		lwsl_err("%s: unexpected extra messages\n", __func__);
		return LWSSSSRET_DESTROY_ME;
	}

	p = lws_json_simple_find((const char *)buf, len, "\"fork\":", &al);
	if (!p)
		return LWSSSSRET_DESTROY_ME;
	fk = atoi(p);
	if (fk < 1 || fk > FORKS)
		return LWSSSSRET_DESTROY_ME;

	p = lws_json_simple_find((const char *)buf, len, "\"test\":", &al);
	if (!p)
		return LWSSSSRET_DESTROY_ME;
	t = atoi(p);

	if (t < 0 || t >= MSGCOUNT)
		return LWSSSSRET_DESTROY_ME;

	m->seen_mask[fk - 1] |= 1ull << t;
	m->seen_msgs[fk - 1]++; /* keep an eye on dupes */

	/* Have we seen a full set of messages from everyone? */

	for (n = 0; n < FORKS; n++) {
		if (m->seen_msgs[n] != (int)MSGCOUNT)
			return LWSSSSRET_OK;
		if (m->seen_mask[n] != 0xffffffffffffffffull)
			return LWSSSSRET_OK;
	}

	/*
	 * Oh... so we have finished collecting messages
	 */

	lwsl_user("%s: test thread %d: %s received all messages\n", __func__,
			(int)(intptr_t)lws_context_user(lws_ss_get_context(m->ss)),
			lws_ss_tag(m->ss));
	m->seen_all = m->send_seen_all = 1;

	/*
	 * Prepare to inform the original process we saw everything
	 * from everyone OK
	 */

	lws_ss_request_tx(m->ss);

	return LWSSSSRET_OK;
}

static void
sul_multi_tx_periodic_cb(lws_sorted_usec_list_t *sul)
{
	myss_t *m = lws_container_of(sul, myss_t, sul);

	if (!m->send_seen_all && m->seen_all) {
		lws_ss_destroy(&m->ss);
		return;
	}

	m->starting = 1;
	if (m->count < MSGCOUNT ||  m->send_seen_all)
		lws_ss_request_tx(m->ss);
}

static lws_ss_state_return_t
multi_myss_tx(void *userobj, lws_ss_tx_ordinal_t ord, uint8_t *buf, size_t *len,
	int *flags)
{
	myss_t *m = (myss_t *)userobj;

	/*
	 * We want to send exactly MSGCOUNT user class smd messages
	 */

	if (!m->starting || (m->count == MSGCOUNT && !m->send_seen_all))
		return LWSSSSRET_TX_DONT_SEND;

//	lwsl_notice("%s: sending SS smd\n", __func__);

	lws_ser_wu64be(buf, 1 << LWSSMDCL_USER_BASE_BITNUM);
	lws_ser_wu64be(buf + 8, 0); /* valgrind notices uninitialized if left */

	if (m->send_seen_all) {
		*len = LWS_SMD_SS_RX_HEADER_LEN + (unsigned int)
			lws_snprintf((char *)buf + LWS_SMD_SS_RX_HEADER_LEN, *len,
			     "{\"class\":\"user\",\"fork\": %d,\"seen_all\":true}",
			     (int)(intptr_t)lws_context_user(lws_ss_get_context(m->ss)));

		m->send_seen_all = 0;
		lwsl_info("%s: test thread %d: sent summary message\n", __func__,
				(int)(intptr_t)lws_context_user(lws_ss_get_context(m->ss)));
	} else
		*len = LWS_SMD_SS_RX_HEADER_LEN + (unsigned int)
			lws_snprintf((char *)buf + LWS_SMD_SS_RX_HEADER_LEN, *len,
			     "{\"class\":\"user\",\"fork\": %d,\"test\":%u}",
			     (int)(intptr_t)lws_context_user(lws_ss_get_context(m->ss)),
			     m->count++);

	*flags = LWSSS_FLAG_SOM | LWSSS_FLAG_EOM;

	lws_sul_schedule(lws_ss_get_context(m->ss), 0, &m->sul,
			sul_multi_tx_periodic_cb, 25 * LWS_US_PER_MS);

	return LWSSSSRET_OK;
}

static lws_ss_state_return_t
multi_myss_state(void *userobj, void *h_src, lws_ss_constate_t state,
	   lws_ss_tx_ordinal_t ack)
{
	myss_t *m = (myss_t *)userobj;
	int n;

	lwsl_notice("%s: %s: %s (%d), ord 0x%x\n", __func__, lws_ss_tag(m->ss),
		    lws_ss_state_name((int)state), state, (unsigned int)ack);

	switch (state) {
	case LWSSSCS_DESTROYING:
		lws_sul_cancel(&m->sul);
		interrupted = 1;
		return 0;

	case LWSSSCS_CONNECTED:
		lwsl_notice("%s: CONNECTED: test fork %d\n", __func__,
				(int)(intptr_t)lws_context_user(lws_ss_get_context(m->ss)));
		/*
		 * Because in this test everybody is watching and counting
		 * everybody else's messages from different forks, we have to
		 * hold off starting sending for 2s so all forks can join the
		 * proxy first and not miss anything
		 */
		lws_sul_schedule(lws_ss_get_context(m->ss), 0, &m->sul,
				sul_multi_tx_periodic_cb, 2 * LWS_US_PER_SEC);
		m->starting = 0;
		return 0;
	case LWSSSCS_DISCONNECTED:
		for (n = 0; n < FORKS; n++)
			lwsl_notice("%s: testfork %d: peer %d: seen_msg = %d, "
				    "seen make = 0x%llx\n", __func__,
				    (int)(intptr_t)lws_context_user(lws_ss_get_context(m->ss)),
				    n, m->seen_msgs[n],
				    (unsigned long long)m->seen_mask[n]);
		break;
	default:
		break;
	}

	return 0;
}

static const lws_ss_info_t ssi_multi_lws_smd = {
	.handle_offset		  = offsetof(myss_t, ss),
	.opaque_user_data_offset  = offsetof(myss_t, opaque_data),
	.rx			  = multi_myss_rx,
	.tx			  = multi_myss_tx,
	.state			  = multi_myss_state,
	.user_alloc		  = sizeof(myss_t),
	.streamtype		  = LWS_SMD_STREAMTYPENAME,
	.manual_initial_tx_credit = 1 << LWSSMDCL_USER_BASE_BITNUM,
};

static lws_ss_state_return_t
multi_myss_rx_monitor(void *userobj, const uint8_t *buf, size_t len, int flags)
{
	myss_t *m = (myss_t *)userobj;
	const char *p;
	size_t al;
	int fk, n;

	/* ignore our and other forks announcing their result */

	if (!lws_json_simple_find((const char *)buf, len, "\"seen_all\":", &al))
		return LWSSSSRET_OK;

	p = lws_json_simple_find((const char *)buf, len, "\"fork\":", &al);
	if (!p)
		return LWSSSSRET_DESTROY_ME;
	fk = atoi(p);
	if (fk < 1 || fk > FORKS)
		return LWSSSSRET_DESTROY_ME;

	if (m->seen_msgs[fk - 1])
		/* expected only once ... dupe */
		return LWSSSSRET_DESTROY_ME;

	m->seen_msgs[fk - 1] = 1;

	for (n = 0; n < FORKS; n++)
		if (!m->seen_msgs[n])
			return LWSSSSRET_OK;

	/* the test has succeeded */

	bad = 0;
	interrupted = 1;

	return LWSSSSRET_OK;
}

static const lws_ss_info_t ssi_multi_lws_smd_monitor = {
	.handle_offset		  = offsetof(myss_t, ss),
	.opaque_user_data_offset  = offsetof(myss_t, opaque_data),
	.rx			  = multi_myss_rx_monitor,
//	.state			  = multi_myss_state_monitor,
	.user_alloc		  = sizeof(myss_t),
	.streamtype		  = LWS_SMD_STREAMTYPENAME,
	.manual_initial_tx_credit = 1 << LWSSMDCL_USER_BASE_BITNUM,
};

/* for comparison, this is a non-SS lws_smd participant */

static int
direct_smd_cb(void *opaque, lws_smd_class_t _class, lws_usec_t timestamp,
	      void *buf, size_t len)
{
	struct lws_context **pctx = (struct lws_context **)opaque;

	if (_class != LWSSMDCL_SYSTEM_STATE)
		return 0;

	if (!lws_json_simple_strcmp(buf, len, "\"state\":", "OPERATIONAL")) {

		/*
		 * Create the SSPC link to lws_smd... notice in ssi_lws_smd
		 * above, we tell this link to use the user class filter.
		 *
		 * If context->user is zero, we are the original process
		 * monitoring the progress of the others, otherwise we are
		 * 1 .. FORKS and producing / checking the smd messages
		 */

		lwsl_info("%s: starting ss for test fork %d\n", __func__,
				(int)(intptr_t)lws_context_user(*pctx));

		if (lws_ss_create(*pctx, 0, lws_context_user(*pctx) ?
				&ssi_multi_lws_smd /* forked process send / check */:
				&ssi_multi_lws_smd_monitor /* original monitors */,
				NULL, NULL, NULL, NULL)) {
			lwsl_err("%s: failed to create secure stream\n",
				 __func__);

			return -1;
		}
	}

	return 0;
}


static void
sul_timeout_cb(lws_sorted_usec_list_t *sul)
{
	interrupted = 1;
}

int
smd_ss_multi_test(int argc, const char **argv)
{
	struct lws_context_creation_info info;
	lws_sorted_usec_list_t sul_timeout;
	struct lws_context *context;
	pid_t pid;
	int n;

	lwsl_user("LWS Secure Streams SMD MULTI test client [-d<verb>]\n");

	for (n = 0; n < FORKS; n++) {
		pid = fork();
		if (!pid) /* forked child */ {
			break;
		}
		lwsl_notice("%s: forked test process %u\n", __func__, pid);
	}

	if (n == FORKS)
		/* the original process */
		n = -1; /* so original ends up with context.user as 0 below */

	memset(&info, 0, sizeof info);
	memset(&sul_timeout, 0, sizeof sul_timeout);

	lws_cmdline_option_handle_builtin(argc, argv, &info);

	{
		const char *p;

		/* connect to ssproxy via UDS by default, else via
		 * tcp connection to this port */
		if ((p = lws_cmdline_option(argc, argv, "-p")))
			info.ss_proxy_port = (uint16_t)atoi(p);

		/* UDS "proxy.ss.lws" in abstract namespace, else this socket
		 * path; when -p given this can specify the network interface
		 * to bind to */
		if ((p = lws_cmdline_option(argc, argv, "-i")))
			info.ss_proxy_bind = p;

		/* if -p given, -a specifies the proxy address to connect to */
		if ((p = lws_cmdline_option(argc, argv, "-a")))
			info.ss_proxy_address = p;
	}

	info.fd_limit_per_thread	= 1 + 6 + 1;
	info.port			= CONTEXT_PORT_NO_LISTEN;
	info.protocols			= lws_sspc_protocols;
	info.options			= LWS_SERVER_OPTION_EXPLICIT_VHOSTS |
					  LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;

	info.early_smd_cb		= direct_smd_cb;
	info.early_smd_class_filter	= 0xffffffff;
	info.early_smd_opaque		= &context;

	info.user			= (void *)(intptr_t)(n + 1);

	/* create the context */

	context = lws_create_context(&info);
	if (!context) {
		lwsl_err("lws init failed\n");
		return 1;
	}

	if (!lws_create_vhost(context, &info)) {
		lwsl_err("%s: failed to create default vhost\n", __func__);
		goto bail;
	}

	/* set up the test timeout */

	lws_sul_schedule(context, 0, &sul_timeout, sul_timeout_cb,
			 10 * LWS_US_PER_SEC);

	/* the event loop */

	while (lws_service(context, 0) >= 0 && !interrupted)
		;

bail:
	lws_context_destroy(context);

	if (n == -1)
		lwsl_user("%s: finished %s\n", __func__, bad ? "FAIL" : "PASS");

	return bad;
}