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");
181 static struct lws_protocols protocols[] = {
182 /* first protocol must always be HTTP handler */
185 "", /* name - can be overridden with -e */
187 sizeof(struct per_session_data__echo), /* per_session_data_size */
191 NULL, NULL, 0 /* End of list */
195 static const struct lws_extension exts[] = {
197 "permessage-deflate",
198 lws_extension_callback_pm_deflate,
199 "permessage-deflate; client_no_context_takeover; client_max_window_bits"
201 { NULL, NULL, NULL /* terminator */ }
205 void sighandler(int sig)
210 static struct option options[] = {
211 { "help", no_argument, NULL, 'h' },
212 { "debug", required_argument, NULL, 'd' },
213 { "port", required_argument, NULL, 'p' },
214 { "ssl-cert", required_argument, NULL, 'C' },
215 { "ssl-key", required_argument, NULL, 'k' },
216 #ifndef LWS_NO_CLIENT
217 { "client", required_argument, NULL, 'c' },
218 { "ratems", required_argument, NULL, 'r' },
220 { "ssl", no_argument, NULL, 's' },
221 { "versa", no_argument, NULL, 'v' },
222 { "uri", required_argument, NULL, 'u' },
223 { "passphrase", required_argument, NULL, 'P' },
224 { "interface", required_argument, NULL, 'i' },
225 { "times", required_argument, NULL, 'n' },
226 { "echogen", no_argument, NULL, 'e' },
227 #ifndef LWS_NO_DAEMONIZE
228 { "daemonize", no_argument, NULL, 'D' },
233 int main(int argc, char **argv)
238 struct lws_context *context;
240 char interface_name[128] = "";
241 const char *_interface = NULL;
242 char ssl_cert[256] = LOCAL_RESOURCE_PATH"/libwebsockets-test-server.pem";
243 char ssl_key[256] = LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem";
245 /* LOG_PERROR is not POSIX standard, and may not be portable */
247 int syslog_options = LOG_PID;
249 int syslog_options = LOG_PID | LOG_PERROR;
253 int listen_port = 80;
254 struct lws_context_creation_info info;
255 char passphrase[256];
257 #ifndef LWS_NO_CLIENT
258 char address[256], ads_port[256 + 30];
259 int rate_us = 250000;
260 unsigned long long oldus;
262 int disallow_selfsigned = 0;
264 const char *connect_protocol = NULL;
265 struct lws_client_connect_info i;
269 #ifndef LWS_NO_DAEMONIZE
273 memset(&info, 0, sizeof info);
275 #ifndef LWS_NO_CLIENT
276 lwsl_notice("Built to support client operations\n");
278 #ifndef LWS_NO_SERVER
279 lwsl_notice("Built to support server operations\n");
283 n = getopt_long(argc, argv, "i:hsp:d:DC:k:P:vu:n:e"
284 #ifndef LWS_NO_CLIENT
292 strncpy(passphrase, optarg, sizeof(passphrase));
293 passphrase[sizeof(passphrase) - 1] = '\0';
294 info.ssl_private_key_password = passphrase;
297 strncpy(ssl_cert, optarg, sizeof(ssl_cert));
298 ssl_cert[sizeof(ssl_cert) - 1] = '\0';
299 disallow_selfsigned = 1;
302 strncpy(ssl_key, optarg, sizeof(ssl_key));
303 ssl_key[sizeof(ssl_key) - 1] = '\0';
306 strncpy(uri, optarg, sizeof(uri));
307 uri[sizeof(uri) - 1] = '\0';
310 #ifndef LWS_NO_DAEMONIZE
313 #if !defined(_WIN32) && !defined(__sun)
314 syslog_options &= ~LOG_PERROR;
318 #ifndef LWS_NO_CLIENT
321 strncpy(address, optarg, sizeof(address) - 1);
322 address[sizeof(address) - 1] = '\0';
326 rate_us = atoi(optarg) * 1000;
330 debug_level = atoi(optarg);
333 use_ssl = 1; /* 1 = take care about cert verification, 2 = allow anything */
342 protocols[0].name = "lws-echogen";
343 connect_protocol = protocols[0].name;
344 lwsl_err("using lws-echogen\n");
347 strncpy(interface_name, optarg, sizeof interface_name);
348 interface_name[(sizeof interface_name) - 1] = '\0';
349 _interface = interface_name;
352 times = atoi(optarg);
356 fprintf(stderr, "Usage: libwebsockets-test-echo\n"
357 " --debug / -d <debug bitfield>\n"
358 " --port / -p <port>\n"
359 " --ssl-cert / -C <cert path>\n"
360 " --ssl-key / -k <key path>\n"
361 #ifndef LWS_NO_CLIENT
362 " --client / -c <server IP>\n"
363 " --ratems / -r <rate in ms>\n"
366 " --passphrase / -P <passphrase>\n"
367 " --interface / -i <interface>\n"
368 " --uri / -u <uri path>\n"
369 " --times / -n <-1 unlimited or times to echo>\n"
370 #ifndef LWS_NO_DAEMONIZE
371 " --daemonize / -D\n"
378 #ifndef LWS_NO_DAEMONIZE
380 * normally lock path would be /var/lock/lwsts or similar, to
381 * simplify getting started without having to take care about
382 * permissions or running as root, set to /tmp/.lwsts-lock
384 #if defined(WIN32) || defined(_WIN32)
386 if (!client && daemonize && lws_daemonize("/tmp/.lwstecho-lock")) {
387 fprintf(stderr, "Failed to daemonize\n");
394 /* we will only try to log things according to our debug_level */
395 setlogmask(LOG_UPTO (LOG_DEBUG));
396 openlog("lwsts", syslog_options, LOG_DAEMON);
399 /* tell the library what debug level to emit and to send it to syslog */
400 lws_set_log_level(debug_level, lwsl_emit_syslog);
402 lwsl_notice("libwebsockets test server echo - license LGPL2.1+SLE\n");
403 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
405 #ifndef LWS_NO_CLIENT
407 lwsl_notice("Running in client mode\n");
408 listen_port = CONTEXT_PORT_NO_LISTEN;
409 if (use_ssl && !disallow_selfsigned) {
410 lwsl_info("allowing selfsigned\n");
413 lwsl_info("requiring server cert validation against %s\n",
415 info.ssl_ca_filepath = ssl_cert;
419 #ifndef LWS_NO_SERVER
420 lwsl_notice("Running in server mode\n");
423 #ifndef LWS_NO_CLIENT
427 info.port = listen_port;
428 info.iface = _interface;
429 info.protocols = protocols;
430 if (use_ssl && !client) {
431 info.ssl_cert_filepath = ssl_cert;
432 info.ssl_private_key_filepath = ssl_key;
434 if (use_ssl && client) {
435 info.ssl_cert_filepath = NULL;
436 info.ssl_private_key_filepath = NULL;
440 info.extensions = exts;
441 info.options = opts | LWS_SERVER_OPTION_VALIDATE_UTF8;
444 info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
445 #ifndef LWS_NO_EXTENSIONS
446 info.extensions = exts;
449 context = lws_create_context(&info);
450 if (context == NULL) {
451 lwsl_err("libwebsocket init failed\n");
456 signal(SIGINT, sighandler);
458 #ifndef LWS_NO_CLIENT
459 gettimeofday(&tv, NULL);
460 oldus = ((unsigned long long)tv.tv_sec * 1000000) + tv.tv_usec;
464 while (n >= 0 && !force_exit) {
465 #ifndef LWS_NO_CLIENT
466 if (client && !state && times) {
468 lwsl_notice("Client connecting to %s:%u....\n",
470 /* we are in client mode */
472 address[sizeof(address) - 1] = '\0';
473 sprintf(ads_port, "%s:%u", address, port & 65535);
477 memset(&i, 0, sizeof(i));
482 i.ssl_connection = use_ssl;
486 i.protocol = connect_protocol;
488 wsi = lws_client_connect_via_info(&i);
490 lwsl_err("Client failed to connect to %s:%u\n",
496 if (client && !versa && times) {
497 gettimeofday(&tv, NULL);
499 if (((((unsigned long long)tv.tv_sec * 1000000) + tv.tv_usec) - oldus) > rate_us) {
500 lws_callback_on_writable_all_protocol(context,
502 oldus = ((unsigned long long)tv.tv_sec * 1000000) + tv.tv_usec;
508 if (client && !state && !times)
511 n = lws_service(context, 10);
513 #ifndef LWS_NO_CLIENT
516 lws_context_destroy(context);
518 lwsl_notice("libwebsockets-test-echo exited cleanly\n");