2011-03-07 21:40:59 +00:00
|
|
|
/*
|
|
|
|
* libwebsockets-test-fraggle - random fragmentation test
|
|
|
|
*
|
2016-01-29 09:35:58 +08:00
|
|
|
* Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
|
2011-03-07 21:40:59 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation:
|
|
|
|
* version 2.1 of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "../lib/libwebsockets.h"
|
|
|
|
|
2012-04-12 11:06:05 +08:00
|
|
|
#define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
|
2011-03-07 21:40:59 +00:00
|
|
|
|
|
|
|
static int client;
|
2011-03-09 09:21:24 +00:00
|
|
|
static int terminate;
|
2011-03-07 21:40:59 +00:00
|
|
|
|
|
|
|
enum demo_protocols {
|
|
|
|
PROTOCOL_FRAGGLE,
|
|
|
|
|
|
|
|
/* always last */
|
|
|
|
DEMO_PROTOCOL_COUNT
|
|
|
|
};
|
|
|
|
|
|
|
|
/* fraggle protocol */
|
|
|
|
|
2011-03-09 09:21:24 +00:00
|
|
|
enum fraggle_states {
|
|
|
|
FRAGSTATE_START_MESSAGE,
|
|
|
|
FRAGSTATE_RANDOM_PAYLOAD,
|
|
|
|
FRAGSTATE_POST_PAYLOAD_SUM,
|
|
|
|
};
|
|
|
|
|
2011-03-07 21:40:59 +00:00
|
|
|
struct per_session_data__fraggle {
|
|
|
|
int packets_left;
|
|
|
|
int total_message;
|
|
|
|
unsigned long sum;
|
2011-03-09 09:21:24 +00:00
|
|
|
enum fraggle_states state;
|
2011-03-07 21:40:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2015-12-17 07:54:44 +08:00
|
|
|
callback_fraggle(struct lws *wsi, enum lws_callback_reasons reason,
|
|
|
|
void *user, void *in, size_t len)
|
2011-03-07 21:40:59 +00:00
|
|
|
{
|
|
|
|
int n;
|
2016-01-11 11:34:01 +08:00
|
|
|
unsigned char buf[LWS_PRE + 8000];
|
2011-03-07 21:40:59 +00:00
|
|
|
struct per_session_data__fraggle *psf = user;
|
|
|
|
int chunk;
|
|
|
|
int write_mode = LWS_WRITE_CONTINUATION;
|
|
|
|
unsigned long sum;
|
|
|
|
unsigned char *p = (unsigned char *)in;
|
2016-01-11 11:34:01 +08:00
|
|
|
unsigned char *bp = &buf[LWS_PRE];
|
2014-11-30 13:55:40 +08:00
|
|
|
int ran;
|
2011-03-07 21:40:59 +00:00
|
|
|
|
|
|
|
switch (reason) {
|
|
|
|
|
|
|
|
case LWS_CALLBACK_ESTABLISHED:
|
2011-03-09 09:21:24 +00:00
|
|
|
|
2011-03-07 21:40:59 +00:00
|
|
|
fprintf(stderr, "server sees client connect\n");
|
2011-03-09 09:21:24 +00:00
|
|
|
psf->state = FRAGSTATE_START_MESSAGE;
|
2011-03-07 21:40:59 +00:00
|
|
|
/* start the ball rolling */
|
2015-12-16 18:19:08 +08:00
|
|
|
lws_callback_on_writable(wsi);
|
2011-03-07 21:40:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLIENT_ESTABLISHED:
|
2011-03-09 09:21:24 +00:00
|
|
|
|
2011-03-07 21:40:59 +00:00
|
|
|
fprintf(stderr, "client connects to server\n");
|
2011-03-09 09:21:24 +00:00
|
|
|
psf->state = FRAGSTATE_START_MESSAGE;
|
2011-03-07 21:40:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLIENT_RECEIVE:
|
2011-03-09 09:21:24 +00:00
|
|
|
|
|
|
|
switch (psf->state) {
|
|
|
|
|
|
|
|
case FRAGSTATE_START_MESSAGE:
|
|
|
|
|
|
|
|
psf->state = FRAGSTATE_RANDOM_PAYLOAD;
|
|
|
|
psf->sum = 0;
|
|
|
|
psf->total_message = 0;
|
|
|
|
psf->packets_left = 0;
|
|
|
|
|
|
|
|
/* fallthru */
|
|
|
|
|
|
|
|
case FRAGSTATE_RANDOM_PAYLOAD:
|
|
|
|
|
2015-12-27 18:16:32 +08:00
|
|
|
for (n = 0; (unsigned int)n < len; n++)
|
2011-03-09 09:21:24 +00:00
|
|
|
psf->sum += p[n];
|
|
|
|
|
|
|
|
psf->total_message += len;
|
|
|
|
psf->packets_left++;
|
|
|
|
|
2015-12-04 08:43:54 +08:00
|
|
|
if (lws_is_final_fragment(wsi))
|
2011-03-09 09:21:24 +00:00
|
|
|
psf->state = FRAGSTATE_POST_PAYLOAD_SUM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FRAGSTATE_POST_PAYLOAD_SUM:
|
|
|
|
|
2014-11-30 12:27:47 +08:00
|
|
|
sum = ((unsigned int)p[0]) << 24;
|
2011-03-07 21:40:59 +00:00
|
|
|
sum |= p[1] << 16;
|
|
|
|
sum |= p[2] << 8;
|
|
|
|
sum |= p[3];
|
|
|
|
if (sum == psf->sum)
|
|
|
|
fprintf(stderr, "EOM received %d correctly "
|
|
|
|
"from %d fragments\n",
|
|
|
|
psf->total_message, psf->packets_left);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "**** ERROR at EOM: "
|
|
|
|
"length %d, rx sum = 0x%lX, "
|
|
|
|
"server says it sent 0x%lX\n",
|
|
|
|
psf->total_message, psf->sum, sum);
|
|
|
|
|
2011-03-09 09:21:24 +00:00
|
|
|
psf->state = FRAGSTATE_START_MESSAGE;
|
2011-03-07 21:40:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_SERVER_WRITEABLE:
|
|
|
|
|
2011-03-09 09:21:24 +00:00
|
|
|
switch (psf->state) {
|
2011-03-07 21:40:59 +00:00
|
|
|
|
2011-03-09 09:21:24 +00:00
|
|
|
case FRAGSTATE_START_MESSAGE:
|
2015-12-17 18:25:25 +08:00
|
|
|
lws_get_random(lws_get_context(wsi), &ran, sizeof(ran));
|
2016-02-02 09:02:24 +08:00
|
|
|
psf->packets_left = (ran & 1023) + 1;
|
2011-03-07 21:40:59 +00:00
|
|
|
fprintf(stderr, "Spamming %d random fragments\n",
|
|
|
|
psf->packets_left);
|
|
|
|
psf->sum = 0;
|
|
|
|
psf->total_message = 0;
|
|
|
|
write_mode = LWS_WRITE_BINARY;
|
2011-03-09 09:21:24 +00:00
|
|
|
psf->state = FRAGSTATE_RANDOM_PAYLOAD;
|
2011-03-07 21:40:59 +00:00
|
|
|
|
2011-03-09 09:21:24 +00:00
|
|
|
/* fallthru */
|
2011-03-07 21:40:59 +00:00
|
|
|
|
2011-03-09 09:21:24 +00:00
|
|
|
case FRAGSTATE_RANDOM_PAYLOAD:
|
2011-03-07 21:40:59 +00:00
|
|
|
|
2013-02-14 14:15:58 +08:00
|
|
|
/*
|
|
|
|
* note how one chunk can be 8000, but we use the
|
|
|
|
* default rx buffer size of 4096, so we exercise the
|
|
|
|
* code for rx spill because the rx buffer is full
|
|
|
|
*/
|
|
|
|
|
2015-12-17 18:25:25 +08:00
|
|
|
lws_get_random(lws_get_context(wsi), &ran, sizeof(ran));
|
2016-02-02 09:02:24 +08:00
|
|
|
chunk = (ran & 511) + 1;
|
2011-03-09 09:21:24 +00:00
|
|
|
psf->total_message += chunk;
|
2011-03-07 21:40:59 +00:00
|
|
|
|
2015-12-17 18:25:25 +08:00
|
|
|
lws_get_random(lws_get_context(wsi), bp, chunk);
|
2011-03-09 09:21:24 +00:00
|
|
|
for (n = 0; n < chunk; n++)
|
|
|
|
psf->sum += bp[n];
|
2011-03-07 21:40:59 +00:00
|
|
|
|
2011-03-09 09:21:24 +00:00
|
|
|
psf->packets_left--;
|
|
|
|
if (psf->packets_left)
|
|
|
|
write_mode |= LWS_WRITE_NO_FIN;
|
|
|
|
else
|
|
|
|
psf->state = FRAGSTATE_POST_PAYLOAD_SUM;
|
|
|
|
|
2015-12-04 08:43:54 +08:00
|
|
|
n = lws_write(wsi, bp, chunk, write_mode);
|
2013-02-23 10:50:10 +08:00
|
|
|
if (n < 0)
|
|
|
|
return -1;
|
|
|
|
if (n < chunk) {
|
|
|
|
lwsl_err("Partial write\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-09 09:21:24 +00:00
|
|
|
|
2015-12-16 18:19:08 +08:00
|
|
|
lws_callback_on_writable(wsi);
|
2011-03-09 09:21:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FRAGSTATE_POST_PAYLOAD_SUM:
|
|
|
|
|
|
|
|
fprintf(stderr, "Spamming session over, "
|
|
|
|
"len = %d. sum = 0x%lX\n",
|
|
|
|
psf->total_message, psf->sum);
|
|
|
|
|
|
|
|
bp[0] = psf->sum >> 24;
|
2015-12-27 18:16:32 +08:00
|
|
|
bp[1] = (unsigned char)(psf->sum >> 16);
|
|
|
|
bp[2] = (unsigned char)(psf->sum >> 8);
|
|
|
|
bp[3] = (unsigned char)psf->sum;
|
2012-04-09 15:09:01 +08:00
|
|
|
|
2015-12-04 08:43:54 +08:00
|
|
|
n = lws_write(wsi, (unsigned char *)bp,
|
2011-03-09 09:21:24 +00:00
|
|
|
4, LWS_WRITE_BINARY);
|
2013-02-23 10:50:10 +08:00
|
|
|
if (n < 0)
|
|
|
|
return -1;
|
|
|
|
if (n < 4) {
|
|
|
|
lwsl_err("Partial write\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-09 09:21:24 +00:00
|
|
|
|
|
|
|
psf->state = FRAGSTATE_START_MESSAGE;
|
|
|
|
|
2015-12-16 18:19:08 +08:00
|
|
|
lws_callback_on_writable(wsi);
|
2011-03-09 09:21:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-03-07 21:40:59 +00:00
|
|
|
break;
|
|
|
|
|
2011-03-09 09:21:24 +00:00
|
|
|
case LWS_CALLBACK_CLOSED:
|
|
|
|
|
|
|
|
terminate = 1;
|
|
|
|
break;
|
2011-03-07 21:40:59 +00:00
|
|
|
|
|
|
|
/* because we are protocols[0] ... */
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
|
2011-03-09 09:21:24 +00:00
|
|
|
if (strcmp(in, "deflate-stream") == 0) {
|
2011-03-07 21:40:59 +00:00
|
|
|
fprintf(stderr, "denied deflate-stream extension\n");
|
|
|
|
return 1;
|
2011-03-09 09:21:24 +00:00
|
|
|
}
|
2011-03-07 21:40:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* list of supported protocols and callbacks */
|
|
|
|
|
2015-12-04 11:08:32 +08:00
|
|
|
static struct lws_protocols protocols[] = {
|
2011-03-07 21:40:59 +00:00
|
|
|
{
|
|
|
|
"fraggle-protocol",
|
|
|
|
callback_fraggle,
|
|
|
|
sizeof(struct per_session_data__fraggle),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NULL, NULL, 0 /* End of list */
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-02 09:02:24 +08:00
|
|
|
static const struct lws_extension exts[] = {
|
|
|
|
{
|
|
|
|
"permessage-deflate",
|
|
|
|
lws_extension_callback_pm_deflate,
|
|
|
|
"permessage-deflate; client_no_context_takeover; client_max_window_bits"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"deflate-frame",
|
|
|
|
lws_extension_callback_pm_deflate,
|
|
|
|
"deflate_frame"
|
|
|
|
},
|
|
|
|
{ NULL, NULL, NULL /* terminator */ }
|
|
|
|
};
|
|
|
|
|
2011-03-07 21:40:59 +00:00
|
|
|
static struct option options[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2013-01-10 22:28:59 +08:00
|
|
|
{ "debug", required_argument, NULL, 'd' },
|
2011-03-07 21:40:59 +00:00
|
|
|
{ "port", required_argument, NULL, 'p' },
|
|
|
|
{ "ssl", no_argument, NULL, 's' },
|
2012-04-09 15:09:01 +08:00
|
|
|
{ "interface", required_argument, NULL, 'i' },
|
|
|
|
{ "client", no_argument, NULL, 'c' },
|
2011-03-07 21:40:59 +00:00
|
|
|
{ NULL, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
int port = 7681;
|
|
|
|
int use_ssl = 0;
|
2015-12-04 11:08:32 +08:00
|
|
|
struct lws_context *context;
|
2011-03-07 21:40:59 +00:00
|
|
|
int opts = 0;
|
2016-02-02 09:02:24 +08:00
|
|
|
char interface_name[128] = "", ads_port[300];
|
2013-02-11 17:52:23 +01:00
|
|
|
const char *iface = NULL;
|
2015-12-04 11:08:32 +08:00
|
|
|
struct lws *wsi;
|
2016-02-02 09:02:24 +08:00
|
|
|
const char *address = NULL;
|
2011-03-07 21:40:59 +00:00
|
|
|
int server_port = port;
|
2013-02-09 14:01:09 +08:00
|
|
|
struct lws_context_creation_info info;
|
|
|
|
|
|
|
|
memset(&info, 0, sizeof info);
|
2016-01-29 09:35:58 +08:00
|
|
|
lwsl_notice("libwebsockets test server fraggle - license LGPL2.1+SLE\n");
|
|
|
|
lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
|
2011-03-07 21:40:59 +00:00
|
|
|
|
|
|
|
while (n >= 0) {
|
2013-01-21 09:53:35 +08:00
|
|
|
n = getopt_long(argc, argv, "ci:hsp:d:", options, NULL);
|
2011-03-07 21:40:59 +00:00
|
|
|
if (n < 0)
|
|
|
|
continue;
|
|
|
|
switch (n) {
|
2013-01-10 22:28:59 +08:00
|
|
|
case 'd':
|
2013-01-12 09:17:42 +08:00
|
|
|
lws_set_log_level(atoi(optarg), NULL);
|
2013-01-10 22:28:59 +08:00
|
|
|
break;
|
2011-03-07 21:40:59 +00:00
|
|
|
case 's':
|
|
|
|
use_ssl = 1;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
port = atoi(optarg);
|
|
|
|
server_port = port;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
strncpy(interface_name, optarg, sizeof interface_name);
|
|
|
|
interface_name[(sizeof interface_name) - 1] = '\0';
|
2013-02-11 17:52:23 +01:00
|
|
|
iface = interface_name;
|
2011-03-07 21:40:59 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
client = 1;
|
|
|
|
fprintf(stderr, " Client mode\n");
|
|
|
|
break;
|
|
|
|
case 'h':
|
2011-03-09 09:21:24 +00:00
|
|
|
fprintf(stderr, "Usage: libwebsockets-test-fraggle "
|
2013-01-10 22:28:59 +08:00
|
|
|
"[--port=<p>] [--ssl] "
|
|
|
|
"[-d <log bitfield>] "
|
|
|
|
"[--client]\n");
|
2011-03-07 21:40:59 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client) {
|
|
|
|
server_port = CONTEXT_PORT_NO_LISTEN;
|
|
|
|
if (optind >= argc) {
|
|
|
|
fprintf(stderr, "Must give address of server\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-09 14:01:09 +08:00
|
|
|
info.port = server_port;
|
2013-02-11 17:52:23 +01:00
|
|
|
info.iface = iface;
|
2013-02-09 14:01:09 +08:00
|
|
|
info.protocols = protocols;
|
2016-02-02 09:02:24 +08:00
|
|
|
info.extensions = exts;
|
|
|
|
|
2013-02-09 14:01:09 +08:00
|
|
|
if (use_ssl) {
|
2016-02-02 09:02:24 +08:00
|
|
|
info.ssl_cert_filepath = LOCAL_RESOURCE_PATH
|
|
|
|
"/libwebsockets-test-server.pem";
|
|
|
|
info.ssl_private_key_filepath = LOCAL_RESOURCE_PATH
|
|
|
|
"/libwebsockets-test-server.key.pem";
|
2013-02-09 14:01:09 +08:00
|
|
|
}
|
|
|
|
info.gid = -1;
|
|
|
|
info.uid = -1;
|
|
|
|
info.options = opts;
|
|
|
|
|
2015-12-04 08:43:54 +08:00
|
|
|
context = lws_create_context(&info);
|
2011-03-07 21:40:59 +00:00
|
|
|
if (context == NULL) {
|
|
|
|
fprintf(stderr, "libwebsocket init failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client) {
|
2016-02-02 09:02:24 +08:00
|
|
|
struct lws_client_connect_info i;
|
|
|
|
|
2011-03-07 21:40:59 +00:00
|
|
|
address = argv[optind];
|
2016-02-02 09:02:24 +08:00
|
|
|
sprintf(ads_port, "%s:%u", address, port & 65535);
|
|
|
|
memset(&i, 0, sizeof(i));
|
|
|
|
i.context = context;
|
|
|
|
i.address = address;
|
|
|
|
i.port = port;
|
|
|
|
i.ssl_connection = use_ssl;
|
|
|
|
i.path = "/";
|
|
|
|
i.host = ads_port;
|
|
|
|
i.origin = ads_port;
|
|
|
|
i.protocol = protocols[PROTOCOL_FRAGGLE].name;
|
|
|
|
i.client_exts = exts;
|
|
|
|
|
|
|
|
lwsl_notice("Connecting to %s:%u\n", address, port);
|
|
|
|
wsi = lws_client_connect_via_info(&i);
|
2011-03-07 21:40:59 +00:00
|
|
|
if (wsi == NULL) {
|
|
|
|
fprintf(stderr, "Client connect to server failed\n");
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n = 0;
|
2011-03-09 09:21:24 +00:00
|
|
|
while (!n && !terminate)
|
2015-12-04 08:43:54 +08:00
|
|
|
n = lws_service(context, 50);
|
2011-03-07 21:40:59 +00:00
|
|
|
|
2011-03-09 09:21:24 +00:00
|
|
|
fprintf(stderr, "Terminating...\n");
|
|
|
|
|
2011-03-07 21:40:59 +00:00
|
|
|
bail:
|
2015-12-04 08:43:54 +08:00
|
|
|
lws_context_destroy(context);
|
2011-03-07 21:40:59 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|