2 * libwebsockets-test-server - libwebsockets test implementation
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
21 #include "test-server.h"
24 int max_poll_elements;
28 struct lws_pollfd *pollfds;
32 volatile int force_exit = 0;
33 struct lws_context *context;
34 struct lws_plat_file_ops fops_plat;
36 /* http server gets files from this path */
37 #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
38 char *resource_path = LOCAL_RESOURCE_PATH;
39 #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
40 char crl_path[1024] = "";
43 /* singlethreaded version --> no locks */
45 void test_server_lock(int care)
48 void test_server_unlock(int care)
53 * This demo server shows how to use libwebsockets for one or more
54 * websocket protocols in the same server
56 * It defines the following websocket protocols:
58 * dumb-increment-protocol: once the socket is opened, an incrementing
59 * ascii string is sent down it every 50ms.
60 * If you send "reset\n" on the websocket, then
61 * the incrementing number is reset to 0.
63 * lws-mirror-protocol: copies any received packet to every connection also
64 * using this protocol, including the sender
71 PROTOCOL_DUMB_INCREMENT,
80 /* list of supported protocols and callbacks */
82 static struct lws_protocols protocols[] = {
83 /* first protocol must always be HTTP handler */
86 "http-only", /* name */
87 callback_http, /* callback */
88 sizeof (struct per_session_data__http), /* per_session_data_size */
89 0, /* max frame size / rx buffer */
92 "dumb-increment-protocol",
93 callback_dumb_increment,
94 sizeof(struct per_session_data__dumb_increment),
95 10, /* rx buf size must be >= permessage-deflate rx size */
98 "lws-mirror-protocol",
100 sizeof(struct per_session_data__lws_mirror),
101 128, /* rx buf size must be >= permessage-deflate rx size */
105 callback_lws_echogen,
106 sizeof(struct per_session_data__echogen),
107 128, /* rx buf size must be >= permessage-deflate rx size */
112 sizeof(struct per_session_data__lws_status),
113 128, /* rx buf size must be >= permessage-deflate rx size */
115 { NULL, NULL, 0, 0 } /* terminator */
119 /* this shows how to override the lws file operations. You don't need
120 * to do any of this unless you have a reason (eg, want to serve
121 * compressed files without decompressing the whole archive)
123 static lws_filefd_type
124 test_server_fops_open(struct lws *wsi, const char *filename,
125 unsigned long *filelen, int *flags)
129 /* call through to original platform implementation */
130 n = fops_plat.open(wsi, filename, filelen, flags);
132 lwsl_info("%s: opening %s, ret %ld, len %lu\n", __func__, filename,
138 void sighandler(int sig)
141 lws_cancel_service(context);
144 static const struct lws_extension exts[] = {
146 "permessage-deflate",
147 lws_extension_callback_pm_deflate,
152 lws_extension_callback_pm_deflate,
155 { NULL, NULL, NULL /* terminator */ }
160 static struct option options[] = {
161 { "help", no_argument, NULL, 'h' },
162 { "debug", required_argument, NULL, 'd' },
163 { "port", required_argument, NULL, 'p' },
164 { "ssl", no_argument, NULL, 's' },
165 { "allow-non-ssl", no_argument, NULL, 'a' },
166 { "interface", required_argument, NULL, 'i' },
167 { "closetest", no_argument, NULL, 'c' },
168 { "ssl-cert", required_argument, NULL, 'C' },
169 { "ssl-key", required_argument, NULL, 'K' },
170 { "ssl-ca", required_argument, NULL, 'A' },
171 #if defined(LWS_OPENSSL_SUPPORT)
172 { "ssl-verify-client", no_argument, NULL, 'v' },
173 #if defined(LWS_HAVE_SSL_CTX_set1_param)
174 { "ssl-crl", required_argument, NULL, 'R' },
177 { "libev", no_argument, NULL, 'e' },
178 #ifndef LWS_NO_DAEMONIZE
179 { "daemonize", no_argument, NULL, 'D' },
181 { "resource_path", required_argument, NULL, 'r' },
182 { "pingpong-secs", required_argument, NULL, 'P' },
186 int main(int argc, char **argv)
188 struct lws_context_creation_info info;
189 char interface_name[128] = "";
190 unsigned int ms, oldms = 0;
191 const char *iface = NULL;
192 char cert_path[1024] = "";
193 char key_path[1024] = "";
194 char ca_path[1024] = "";
195 int uid = -1, gid = -1;
201 /* LOG_PERROR is not POSIX standard, and may not be portable */
203 int syslog_options = LOG_PID;
205 int syslog_options = LOG_PID | LOG_PERROR;
208 #ifndef LWS_NO_DAEMONIZE
213 * take care to zero down the info struct, he contains random garbaage
214 * from the stack otherwise
216 memset(&info, 0, sizeof info);
220 n = getopt_long(argc, argv, "eci:hsap:d:Dr:C:K:A:R:vu:g:P:", options, NULL);
225 opts |= LWS_SERVER_OPTION_LIBEV;
227 #ifndef LWS_NO_DAEMONIZE
230 #if !defined(_WIN32) && !defined(__sun)
231 syslog_options &= ~LOG_PERROR;
242 debug_level = atoi(optarg);
248 opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
251 info.port = atoi(optarg);
254 strncpy(interface_name, optarg, sizeof interface_name);
255 interface_name[(sizeof interface_name) - 1] = '\0';
256 iface = interface_name;
260 fprintf(stderr, " Close testing mode -- closes on "
261 "client after 50 dumb increments"
262 "and suppresses lws_mirror spam\n");
265 resource_path = optarg;
266 printf("Setting resource path to \"%s\"\n", resource_path);
269 strncpy(cert_path, optarg, sizeof(cert_path) - 1);
270 cert_path[sizeof(cert_path) - 1] = '\0';
273 strncpy(key_path, optarg, sizeof(key_path) - 1);
274 key_path[sizeof(key_path) - 1] = '\0';
277 strncpy(ca_path, optarg, sizeof(ca_path) - 1);
278 ca_path[sizeof(ca_path) - 1] = '\0';
281 pp_secs = atoi(optarg);
282 lwsl_notice("Setting pingpong interval to %d\n", pp_secs);
284 #if defined(LWS_OPENSSL_SUPPORT)
287 opts |= LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT;
290 #if defined(LWS_HAVE_SSL_CTX_set1_param)
292 strncpy(crl_path, optarg, sizeof(crl_path) - 1);
293 crl_path[sizeof(crl_path) - 1] = '\0';
298 fprintf(stderr, "Usage: test-server "
299 "[--port=<p>] [--ssl] "
300 "[-d <log bitfield>] "
301 "[--resource_path <path>]\n");
306 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
308 * normally lock path would be /var/lock/lwsts or similar, to
309 * simplify getting started without having to take care about
310 * permissions or running as root, set to /tmp/.lwsts-lock
312 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
313 fprintf(stderr, "Failed to daemonize\n");
318 signal(SIGINT, sighandler);
321 /* we will only try to log things according to our debug_level */
322 setlogmask(LOG_UPTO (LOG_DEBUG));
323 openlog("lwsts", syslog_options, LOG_DAEMON);
326 /* tell the library what debug level to emit and to send it to syslog */
327 lws_set_log_level(debug_level, lwsl_emit_syslog);
329 lwsl_notice("libwebsockets test server - license LGPL2.1+SLE\n");
330 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
332 printf("Using resource path \"%s\"\n", resource_path);
334 max_poll_elements = getdtablesize();
335 pollfds = malloc(max_poll_elements * sizeof (struct lws_pollfd));
336 fd_lookup = malloc(max_poll_elements * sizeof (int));
337 if (pollfds == NULL || fd_lookup == NULL) {
338 lwsl_err("Out of memory pollfds=%d\n", max_poll_elements);
344 info.protocols = protocols;
345 info.ssl_cert_filepath = NULL;
346 info.ssl_private_key_filepath = NULL;
347 info.ws_ping_pong_interval = pp_secs;
350 if (strlen(resource_path) > sizeof(cert_path) - 32) {
351 lwsl_err("resource path too long\n");
355 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
357 if (strlen(resource_path) > sizeof(key_path) - 32) {
358 lwsl_err("resource path too long\n");
362 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
365 info.ssl_cert_filepath = cert_path;
366 info.ssl_private_key_filepath = key_path;
368 info.ssl_ca_filepath = ca_path;
372 info.max_http_header_pool = 16;
373 info.options = opts | LWS_SERVER_OPTION_VALIDATE_UTF8;
374 info.extensions = exts;
375 info.timeout_secs = 5;
376 info.ssl_cipher_list = "ECDHE-ECDSA-AES256-GCM-SHA384:"
377 "ECDHE-RSA-AES256-GCM-SHA384:"
378 "DHE-RSA-AES256-GCM-SHA384:"
379 "ECDHE-RSA-AES256-SHA384:"
380 "HIGH:!aNULL:!eNULL:!EXPORT:"
381 "!DES:!MD5:!PSK:!RC4:!HMAC_SHA1:"
382 "!SHA1:!DHE-RSA-AES128-GCM-SHA256:"
383 "!DHE-RSA-AES128-SHA256:"
384 "!AES128-GCM-SHA256:"
386 "!DHE-RSA-AES256-SHA256:"
387 "!AES256-GCM-SHA384:"
391 /* redirect guys coming on http */
392 info.options |= LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS;
394 context = lws_create_context(&info);
395 if (context == NULL) {
396 lwsl_err("libwebsocket init failed\n");
400 /* this shows how to override the lws file operations. You don't need
401 * to do any of this unless you have a reason (eg, want to serve
402 * compressed files without decompressing the whole archive)
404 /* stash original platform fops */
405 fops_plat = *(lws_get_fops(context));
406 /* override the active fops */
407 lws_get_fops(context)->open = test_server_fops_open;
413 while (n >= 0 && !force_exit) {
416 gettimeofday(&tv, NULL);
419 * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every
420 * live websocket connection using the DUMB_INCREMENT protocol,
421 * as soon as it can take more packets (usually immediately)
424 ms = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
425 if ((ms - oldms) > 50) {
426 lws_callback_on_writable_all_protocol(context,
427 &protocols[PROTOCOL_DUMB_INCREMENT]);
433 * this represents an existing server's single poll action
434 * which also includes libwebsocket sockets
437 n = poll(pollfds, count_pollfds, 50);
442 for (n = 0; n < count_pollfds; n++)
443 if (pollfds[n].revents)
445 * returns immediately if the fd does not
446 * match anything under libwebsockets
449 if (lws_service_fd(context,
453 /* if needed, force-service wsis that may not have read all input */
454 while (!lws_service_adjust_timeout(context, 1, 0)) {
455 lwsl_notice("extpoll doing forced service!\n");
456 lws_service_tsi(context, -1, 0);
459 /* no revents, but before polling again, make lws check for any timeouts */
460 if (ms - ms_1sec > 1000) {
461 lwsl_notice("1 per sec\n");
462 lws_service_fd(context, NULL);
468 * If libwebsockets sockets are all we care about,
469 * you can use this api which takes care of the poll()
470 * and looping through finding who needed service.
472 * If no socket needs service, it'll return anyway after
473 * the number of ms in the second argument.
476 n = lws_service(context, 50);
484 lws_context_destroy(context);
486 lwsl_notice("libwebsockets-test-server exited cleanly\n");