2 * libwebsockets-test-echo
4 * Copyright (C) 2010-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
28 #include "../lib/libwebsockets.h"
35 #include "gettimeofday.h"
39 static volatile int force_exit = 0;
40 static int versa, state;
41 static int times = -1;
43 #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
45 #define MAX_ECHO_PAYLOAD 1024
47 struct per_session_data__echo {
49 unsigned char buf[LWS_PRE + MAX_ECHO_PAYLOAD];
58 callback_echo(struct lws *wsi, enum lws_callback_reasons reason, void *user,
61 struct per_session_data__echo *pss =
62 (struct per_session_data__echo *)user;
69 case LWS_CALLBACK_ESTABLISHED:
74 case LWS_CALLBACK_SERVER_WRITEABLE:
77 n = LWS_WRITE_CONTINUATION;
78 if (!pss->continuation) {
83 pss->continuation = 1;
86 n |= LWS_WRITE_NO_FIN;
87 lwsl_info("+++ test-echo: writing %d, with final %d\n",
88 pss->len, pss->final);
91 n = lws_write(wsi, &pss->buf[LWS_PRE], pss->len, n);
93 lwsl_err("ERROR %d writing to socket, hanging up\n", n);
96 if (n < (int)pss->len) {
97 lwsl_err("Partial write\n");
102 pss->continuation = 0;
103 lws_rx_flow_control(wsi, 1);
106 case LWS_CALLBACK_RECEIVE:
108 pss->final = lws_is_final_fragment(wsi);
109 pss->binary = lws_frame_is_binary(wsi);
110 lwsl_info("+++ test-echo: RX len %ld final %ld, pss->len=%ld\n",
111 (long)len, (long)pss->final, (long)pss->len);
113 memcpy(&pss->buf[LWS_PRE], in, len);
114 assert((int)pss->len == -1);
115 pss->len = (unsigned int)len;
118 lws_rx_flow_control(wsi, 0);
119 lws_callback_on_writable(wsi);
123 #ifndef LWS_NO_CLIENT
124 /* when the callback is used for client operations --> */
126 case LWS_CALLBACK_CLOSED:
127 case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
128 lwsl_debug("closed\n");
132 case LWS_CALLBACK_CLIENT_ESTABLISHED:
133 lwsl_debug("Client has connected\n");
139 case LWS_CALLBACK_CLIENT_RECEIVE:
140 #ifndef LWS_NO_SERVER
144 lwsl_notice("Client RX: %s", (char *)in);
147 case LWS_CALLBACK_CLIENT_WRITEABLE:
148 #ifndef LWS_NO_SERVER
150 if (pss->len != (unsigned int)-1)
155 /* we will send our packet... */
156 pss->len = sprintf((char *)&pss->buf[LWS_PRE],
157 "hello from libwebsockets-test-echo client pid %d index %d\n",
158 getpid(), pss->index++);
159 lwsl_notice("Client TX: %s", &pss->buf[LWS_PRE]);
160 n = lws_write(wsi, &pss->buf[LWS_PRE], pss->len, LWS_WRITE_TEXT);
162 lwsl_err("ERROR %d writing to socket, hanging up\n", n);
165 if (n < (int)pss->len) {
166 lwsl_err("Partial write\n");
171 case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
172 /* reject everything else except permessage-deflate */
173 if (strcmp(in, "permessage-deflate"))
186 static struct lws_protocols protocols[] = {
187 /* first protocol must always be HTTP handler */
190 "", /* name - can be overridden with -e */
192 sizeof(struct per_session_data__echo), /* per_session_data_size */
196 NULL, NULL, 0 /* End of list */
200 static const struct lws_extension exts[] = {
202 "permessage-deflate",
203 lws_extension_callback_pm_deflate,
204 "permessage-deflate; client_no_context_takeover; client_max_window_bits"
208 lws_extension_callback_pm_deflate,
211 { NULL, NULL, NULL /* terminator */ }
215 void sighandler(int sig)
220 static struct option options[] = {
221 { "help", no_argument, NULL, 'h' },
222 { "debug", required_argument, NULL, 'd' },
223 { "port", required_argument, NULL, 'p' },
224 { "ssl-cert", required_argument, NULL, 'C' },
225 { "ssl-key", required_argument, NULL, 'k' },
226 #ifndef LWS_NO_CLIENT
227 { "client", required_argument, NULL, 'c' },
228 { "ratems", required_argument, NULL, 'r' },
230 { "ssl", no_argument, NULL, 's' },
231 { "versa", no_argument, NULL, 'v' },
232 { "uri", required_argument, NULL, 'u' },
233 { "passphrase", required_argument, NULL, 'P' },
234 { "interface", required_argument, NULL, 'i' },
235 { "times", required_argument, NULL, 'n' },
236 { "echogen", no_argument, NULL, 'e' },
237 #ifndef LWS_NO_DAEMONIZE
238 { "daemonize", no_argument, NULL, 'D' },
243 int main(int argc, char **argv)
248 struct lws_context *context;
250 char interface_name[128] = "";
251 const char *_interface = NULL;
252 char ssl_cert[256] = LOCAL_RESOURCE_PATH"/libwebsockets-test-server.pem";
253 char ssl_key[256] = LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem";
255 /* LOG_PERROR is not POSIX standard, and may not be portable */
257 int syslog_options = LOG_PID;
259 int syslog_options = LOG_PID | LOG_PERROR;
263 int listen_port = 80;
264 struct lws_context_creation_info info;
265 char passphrase[256];
267 #ifndef LWS_NO_CLIENT
268 char address[256], ads_port[256 + 30];
269 int rate_us = 250000;
270 unsigned long long oldus;
272 int disallow_selfsigned = 0;
274 const char *connect_protocol = NULL;
275 struct lws_client_connect_info i;
279 #ifndef LWS_NO_DAEMONIZE
283 memset(&info, 0, sizeof info);
285 #ifndef LWS_NO_CLIENT
286 lwsl_notice("Built to support client operations\n");
288 #ifndef LWS_NO_SERVER
289 lwsl_notice("Built to support server operations\n");
293 n = getopt_long(argc, argv, "i:hsp:d:DC:k:P:vu:n:e"
294 #ifndef LWS_NO_CLIENT
302 strncpy(passphrase, optarg, sizeof(passphrase));
303 passphrase[sizeof(passphrase) - 1] = '\0';
304 info.ssl_private_key_password = passphrase;
307 strncpy(ssl_cert, optarg, sizeof(ssl_cert));
308 ssl_cert[sizeof(ssl_cert) - 1] = '\0';
309 disallow_selfsigned = 1;
312 strncpy(ssl_key, optarg, sizeof(ssl_key));
313 ssl_key[sizeof(ssl_key) - 1] = '\0';
316 strncpy(uri, optarg, sizeof(uri));
317 uri[sizeof(uri) - 1] = '\0';
320 #ifndef LWS_NO_DAEMONIZE
323 #if !defined(_WIN32) && !defined(__sun)
324 syslog_options &= ~LOG_PERROR;
328 #ifndef LWS_NO_CLIENT
331 strncpy(address, optarg, sizeof(address) - 1);
332 address[sizeof(address) - 1] = '\0';
336 rate_us = atoi(optarg) * 1000;
340 debug_level = atoi(optarg);
343 use_ssl = 1; /* 1 = take care about cert verification, 2 = allow anything */
352 protocols[0].name = "lws-echogen";
353 connect_protocol = protocols[0].name;
354 lwsl_err("using lws-echogen\n");
357 strncpy(interface_name, optarg, sizeof interface_name);
358 interface_name[(sizeof interface_name) - 1] = '\0';
359 _interface = interface_name;
362 times = atoi(optarg);
366 fprintf(stderr, "Usage: libwebsockets-test-echo\n"
367 " --debug / -d <debug bitfield>\n"
368 " --port / -p <port>\n"
369 " --ssl-cert / -C <cert path>\n"
370 " --ssl-key / -k <key path>\n"
371 #ifndef LWS_NO_CLIENT
372 " --client / -c <server IP>\n"
373 " --ratems / -r <rate in ms>\n"
376 " --passphrase / -P <passphrase>\n"
377 " --interface / -i <interface>\n"
378 " --uri / -u <uri path>\n"
379 " --times / -n <-1 unlimited or times to echo>\n"
380 #ifndef LWS_NO_DAEMONIZE
381 " --daemonize / -D\n"
388 #ifndef LWS_NO_DAEMONIZE
390 * normally lock path would be /var/lock/lwsts or similar, to
391 * simplify getting started without having to take care about
392 * permissions or running as root, set to /tmp/.lwsts-lock
394 #if defined(WIN32) || defined(_WIN32)
396 if (!client && daemonize && lws_daemonize("/tmp/.lwstecho-lock")) {
397 fprintf(stderr, "Failed to daemonize\n");
404 /* we will only try to log things according to our debug_level */
405 setlogmask(LOG_UPTO (LOG_DEBUG));
406 openlog("lwsts", syslog_options, LOG_DAEMON);
409 /* tell the library what debug level to emit and to send it to syslog */
410 lws_set_log_level(debug_level, lwsl_emit_syslog);
412 lwsl_notice("libwebsockets test server echo - license LGPL2.1+SLE\n");
413 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
415 #ifndef LWS_NO_CLIENT
417 lwsl_notice("Running in client mode\n");
418 listen_port = CONTEXT_PORT_NO_LISTEN;
419 if (use_ssl && !disallow_selfsigned) {
420 lwsl_info("allowing selfsigned\n");
423 lwsl_info("requiring server cert validation against %s\n",
425 info.ssl_ca_filepath = ssl_cert;
429 #ifndef LWS_NO_SERVER
430 lwsl_notice("Running in server mode\n");
433 #ifndef LWS_NO_CLIENT
437 info.port = listen_port;
438 info.iface = _interface;
439 info.protocols = protocols;
440 if (use_ssl && !client) {
441 info.ssl_cert_filepath = ssl_cert;
442 info.ssl_private_key_filepath = ssl_key;
444 if (use_ssl && client) {
445 info.ssl_cert_filepath = NULL;
446 info.ssl_private_key_filepath = NULL;
450 info.extensions = exts;
451 info.options = opts | LWS_SERVER_OPTION_VALIDATE_UTF8;
454 info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
455 #ifndef LWS_NO_EXTENSIONS
456 info.extensions = exts;
459 context = lws_create_context(&info);
460 if (context == NULL) {
461 lwsl_err("libwebsocket init failed\n");
466 signal(SIGINT, sighandler);
468 #ifndef LWS_NO_CLIENT
469 gettimeofday(&tv, NULL);
470 oldus = ((unsigned long long)tv.tv_sec * 1000000) + tv.tv_usec;
474 while (n >= 0 && !force_exit) {
475 #ifndef LWS_NO_CLIENT
476 if (client && !state && times) {
478 lwsl_notice("Client connecting to %s:%u....\n",
480 /* we are in client mode */
482 address[sizeof(address) - 1] = '\0';
483 sprintf(ads_port, "%s:%u", address, port & 65535);
487 memset(&i, 0, sizeof(i));
492 i.ssl_connection = use_ssl;
496 i.protocol = connect_protocol;
498 wsi = lws_client_connect_via_info(&i);
500 lwsl_err("Client failed to connect to %s:%u\n",
506 if (client && !versa && times) {
507 gettimeofday(&tv, NULL);
509 if (((((unsigned long long)tv.tv_sec * 1000000) + tv.tv_usec) - oldus) > rate_us) {
510 lws_callback_on_writable_all_protocol(context,
512 oldus = ((unsigned long long)tv.tv_sec * 1000000) + tv.tv_usec;
518 if (client && !state && !times)
521 n = lws_service(context, 10);
523 #ifndef LWS_NO_CLIENT
526 lws_context_destroy(context);
528 lwsl_notice("libwebsockets-test-echo exited cleanly\n");