2 * libwebsockets-test-fraggle - random fragmentation test
4 * Copyright (C) 2011-2016 Andy Green <andy@warmcat.com>
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
9 * The person who associated a work with this deed has dedicated
10 * the work to the public domain by waiving all of his or her rights
11 * to the work worldwide under copyright law, including all related
12 * and neighboring rights, to the extent allowed by law. You can copy,
13 * modify, distribute and perform the work, even for commercial purposes,
14 * all without asking permission.
16 * The test apps are intended to be adapted for use in your code, which
17 * may be proprietary. So unlike the library itself, they are licensed
25 #include "../lib/libwebsockets.h"
27 #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
39 /* fraggle protocol */
42 FRAGSTATE_START_MESSAGE,
43 FRAGSTATE_RANDOM_PAYLOAD,
44 FRAGSTATE_POST_PAYLOAD_SUM,
47 struct per_session_data__fraggle {
51 enum fraggle_states state;
55 callback_fraggle(struct lws *wsi, enum lws_callback_reasons reason,
56 void *user, void *in, size_t len)
59 unsigned char buf[LWS_PRE + 8000];
60 struct per_session_data__fraggle *psf = user;
62 int write_mode = LWS_WRITE_CONTINUATION;
64 unsigned char *p = (unsigned char *)in;
65 unsigned char *bp = &buf[LWS_PRE];
70 case LWS_CALLBACK_ESTABLISHED:
72 fprintf(stderr, "server sees client connect\n");
73 psf->state = FRAGSTATE_START_MESSAGE;
74 /* start the ball rolling */
75 lws_callback_on_writable(wsi);
78 case LWS_CALLBACK_CLIENT_ESTABLISHED:
80 fprintf(stderr, "client connects to server\n");
81 psf->state = FRAGSTATE_START_MESSAGE;
84 case LWS_CALLBACK_CLIENT_RECEIVE:
88 case FRAGSTATE_START_MESSAGE:
90 psf->state = FRAGSTATE_RANDOM_PAYLOAD;
92 psf->total_message = 0;
93 psf->packets_left = 0;
97 case FRAGSTATE_RANDOM_PAYLOAD:
99 for (n = 0; (unsigned int)n < len; n++)
102 psf->total_message += len;
105 if (lws_is_final_fragment(wsi))
106 psf->state = FRAGSTATE_POST_PAYLOAD_SUM;
109 case FRAGSTATE_POST_PAYLOAD_SUM:
111 sum = ((unsigned int)p[0]) << 24;
116 fprintf(stderr, "EOM received %d correctly "
117 "from %d fragments\n",
118 psf->total_message, psf->packets_left);
120 fprintf(stderr, "**** ERROR at EOM: "
121 "length %d, rx sum = 0x%lX, "
122 "server says it sent 0x%lX\n",
123 psf->total_message, psf->sum, sum);
125 psf->state = FRAGSTATE_START_MESSAGE;
130 case LWS_CALLBACK_SERVER_WRITEABLE:
132 switch (psf->state) {
134 case FRAGSTATE_START_MESSAGE:
135 lws_get_random(lws_get_context(wsi), &ran, sizeof(ran));
136 psf->packets_left = (ran & 1023) + 1;
137 fprintf(stderr, "Spamming %d random fragments\n",
140 psf->total_message = 0;
141 write_mode = LWS_WRITE_BINARY;
142 psf->state = FRAGSTATE_RANDOM_PAYLOAD;
146 case FRAGSTATE_RANDOM_PAYLOAD:
149 * note how one chunk can be 8000, but we use the
150 * default rx buffer size of 4096, so we exercise the
151 * code for rx spill because the rx buffer is full
154 lws_get_random(lws_get_context(wsi), &ran, sizeof(ran));
155 chunk = (ran & 511) + 1;
156 psf->total_message += chunk;
158 lws_get_random(lws_get_context(wsi), bp, chunk);
159 for (n = 0; n < chunk; n++)
163 if (psf->packets_left)
164 write_mode |= LWS_WRITE_NO_FIN;
166 psf->state = FRAGSTATE_POST_PAYLOAD_SUM;
168 n = lws_write(wsi, bp, chunk, write_mode);
172 lwsl_err("Partial write\n");
176 lws_callback_on_writable(wsi);
179 case FRAGSTATE_POST_PAYLOAD_SUM:
181 fprintf(stderr, "Spamming session over, "
182 "len = %d. sum = 0x%lX\n",
183 psf->total_message, psf->sum);
185 bp[0] = psf->sum >> 24;
186 bp[1] = (unsigned char)(psf->sum >> 16);
187 bp[2] = (unsigned char)(psf->sum >> 8);
188 bp[3] = (unsigned char)psf->sum;
190 n = lws_write(wsi, (unsigned char *)bp,
191 4, LWS_WRITE_BINARY);
195 lwsl_err("Partial write\n");
199 psf->state = FRAGSTATE_START_MESSAGE;
201 lws_callback_on_writable(wsi);
206 case LWS_CALLBACK_CLOSED:
211 /* because we are protocols[0] ... */
213 case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
214 if (strcmp(in, "deflate-stream") == 0) {
215 fprintf(stderr, "denied deflate-stream extension\n");
228 /* list of supported protocols and callbacks */
230 static struct lws_protocols protocols[] = {
234 sizeof(struct per_session_data__fraggle),
237 NULL, NULL, 0 /* End of list */
241 static const struct lws_extension exts[] = {
243 "permessage-deflate",
244 lws_extension_callback_pm_deflate,
245 "permessage-deflate; client_no_context_takeover; client_max_window_bits"
249 lws_extension_callback_pm_deflate,
252 { NULL, NULL, NULL /* terminator */ }
255 static struct option options[] = {
256 { "help", no_argument, NULL, 'h' },
257 { "debug", required_argument, NULL, 'd' },
258 { "port", required_argument, NULL, 'p' },
259 { "ssl", no_argument, NULL, 's' },
260 { "interface", required_argument, NULL, 'i' },
261 { "client", no_argument, NULL, 'c' },
265 int main(int argc, char **argv)
270 struct lws_context *context;
272 char interface_name[128] = "", ads_port[300];
273 const char *iface = NULL;
275 const char *address = NULL;
276 int server_port = port;
277 struct lws_context_creation_info info;
279 memset(&info, 0, sizeof info);
280 lwsl_notice("libwebsockets test server fraggle - license LGPL2.1+SLE\n");
281 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
284 n = getopt_long(argc, argv, "ci:hsp:d:", options, NULL);
289 lws_set_log_level(atoi(optarg), NULL);
299 strncpy(interface_name, optarg, sizeof interface_name);
300 interface_name[(sizeof interface_name) - 1] = '\0';
301 iface = interface_name;
305 fprintf(stderr, " Client mode\n");
308 fprintf(stderr, "Usage: libwebsockets-test-fraggle "
309 "[--port=<p>] [--ssl] "
310 "[-d <log bitfield>] "
317 server_port = CONTEXT_PORT_NO_LISTEN;
318 if (optind >= argc) {
319 fprintf(stderr, "Must give address of server\n");
324 info.port = server_port;
326 info.protocols = protocols;
327 info.extensions = exts;
330 info.ssl_cert_filepath = LOCAL_RESOURCE_PATH
331 "/libwebsockets-test-server.pem";
332 info.ssl_private_key_filepath = LOCAL_RESOURCE_PATH
333 "/libwebsockets-test-server.key.pem";
339 context = lws_create_context(&info);
340 if (context == NULL) {
341 fprintf(stderr, "libwebsocket init failed\n");
346 struct lws_client_connect_info i;
348 address = argv[optind];
349 sprintf(ads_port, "%s:%u", address, port & 65535);
350 memset(&i, 0, sizeof(i));
354 i.ssl_connection = use_ssl;
358 i.protocol = protocols[PROTOCOL_FRAGGLE].name;
359 i.client_exts = exts;
361 lwsl_notice("Connecting to %s:%u\n", address, port);
362 wsi = lws_client_connect_via_info(&i);
364 fprintf(stderr, "Client connect to server failed\n");
370 while (!n && !terminate)
371 n = lws_service(context, 50);
373 fprintf(stderr, "Terminating...\n");
376 lws_context_destroy(context);