2 * libwebsockets-test-servet - 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;
40 /* singlethreaded version --> no locks */
42 void test_server_lock(int care)
45 void test_server_unlock(int care)
50 * This demo server shows how to use libwebsockets for one or more
51 * websocket protocols in the same server
53 * It defines the following websocket protocols:
55 * dumb-increment-protocol: once the socket is opened, an incrementing
56 * ascii string is sent down it every 50ms.
57 * If you send "reset\n" on the websocket, then
58 * the incrementing number is reset to 0.
60 * lws-mirror-protocol: copies any received packet to every connection also
61 * using this protocol, including the sender
68 PROTOCOL_DUMB_INCREMENT,
76 /* list of supported protocols and callbacks */
78 static struct lws_protocols protocols[] = {
79 /* first protocol must always be HTTP handler */
82 "http-only", /* name */
83 callback_http, /* callback */
84 sizeof (struct per_session_data__http), /* per_session_data_size */
85 0, /* max frame size / rx buffer */
88 "dumb-increment-protocol",
89 callback_dumb_increment,
90 sizeof(struct per_session_data__dumb_increment),
94 "lws-mirror-protocol",
96 sizeof(struct per_session_data__lws_mirror),
101 callback_lws_echogen,
102 sizeof(struct per_session_data__echogen),
105 { NULL, NULL, 0, 0 } /* terminator */
109 /* this shows how to override the lws file operations. You don't need
110 * to do any of this unless you have a reason (eg, want to serve
111 * compressed files without decompressing the whole archive)
113 static lws_filefd_type
114 test_server_fops_open(struct lws *wsi, const char *filename,
115 unsigned long *filelen, int flags)
119 /* call through to original platform implementation */
120 n = fops_plat.open(wsi, filename, filelen, flags);
122 lwsl_notice("%s: opening %s, ret %ld, len %lu\n", __func__, filename,
128 void sighandler(int sig)
131 lws_cancel_service(context);
134 static const struct lws_extension exts[] = {
136 "permessage-deflate",
137 lws_extension_callback_pm_deflate,
142 lws_extension_callback_pm_deflate,
145 { NULL, NULL, NULL /* terminator */ }
150 static struct option options[] = {
151 { "help", no_argument, NULL, 'h' },
152 { "debug", required_argument, NULL, 'd' },
153 { "port", required_argument, NULL, 'p' },
154 { "ssl", no_argument, NULL, 's' },
155 { "allow-non-ssl", no_argument, NULL, 'a' },
156 { "interface", required_argument, NULL, 'i' },
157 { "closetest", no_argument, NULL, 'c' },
158 { "ssl-cert", required_argument, NULL, 'C' },
159 { "ssl-key", required_argument, NULL, 'K' },
160 { "ssl-ca", required_argument, NULL, 'A' },
161 { "libev", no_argument, NULL, 'e' },
162 #ifndef LWS_NO_DAEMONIZE
163 { "daemonize", no_argument, NULL, 'D' },
165 { "resource_path", required_argument, NULL, 'r' },
169 int main(int argc, char **argv)
171 struct lws_context_creation_info info;
172 char interface_name[128] = "";
173 unsigned int ms, oldms = 0;
174 const char *iface = NULL;
175 char cert_path[1024] = "";
176 char key_path[1024] = "";
177 char ca_path[1024] = "";
182 int syslog_options = LOG_PID | LOG_PERROR;
184 #ifndef LWS_NO_DAEMONIZE
189 * take care to zero down the info struct, he contains random garbaage
190 * from the stack otherwise
192 memset(&info, 0, sizeof info);
196 n = getopt_long(argc, argv, "eci:hsap:d:Dr:C:K:A:", options, NULL);
201 opts |= LWS_SERVER_OPTION_LIBEV;
203 #ifndef LWS_NO_DAEMONIZE
207 syslog_options &= ~LOG_PERROR;
212 debug_level = atoi(optarg);
218 opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
221 info.port = atoi(optarg);
224 strncpy(interface_name, optarg, sizeof interface_name);
225 interface_name[(sizeof interface_name) - 1] = '\0';
226 iface = interface_name;
230 fprintf(stderr, " Close testing mode -- closes on "
231 "client after 50 dumb increments"
232 "and suppresses lws_mirror spam\n");
235 resource_path = optarg;
236 printf("Setting resource path to \"%s\"\n", resource_path);
239 strncpy(cert_path, optarg, sizeof cert_path);
242 strncpy(key_path, optarg, sizeof key_path);
245 strncpy(ca_path, optarg, sizeof ca_path);
248 fprintf(stderr, "Usage: test-server "
249 "[--port=<p>] [--ssl] "
250 "[-d <log bitfield>] "
251 "[--resource_path <path>]\n");
256 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
258 * normally lock path would be /var/lock/lwsts or similar, to
259 * simplify getting started without having to take care about
260 * permissions or running as root, set to /tmp/.lwsts-lock
262 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
263 fprintf(stderr, "Failed to daemonize\n");
268 signal(SIGINT, sighandler);
271 /* we will only try to log things according to our debug_level */
272 setlogmask(LOG_UPTO (LOG_DEBUG));
273 openlog("lwsts", syslog_options, LOG_DAEMON);
276 /* tell the library what debug level to emit and to send it to syslog */
277 lws_set_log_level(debug_level, lwsl_emit_syslog);
279 lwsl_notice("libwebsockets test server - license LGPL2.1+SLE\n");
280 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
282 printf("Using resource path \"%s\"\n", resource_path);
284 max_poll_elements = getdtablesize();
285 pollfds = malloc(max_poll_elements * sizeof (struct lws_pollfd));
286 fd_lookup = malloc(max_poll_elements * sizeof (int));
287 if (pollfds == NULL || fd_lookup == NULL) {
288 lwsl_err("Out of memory pollfds=%d\n", max_poll_elements);
294 info.protocols = protocols;
295 info.ssl_cert_filepath = NULL;
296 info.ssl_private_key_filepath = NULL;
299 if (strlen(resource_path) > sizeof(cert_path) - 32) {
300 lwsl_err("resource path too long\n");
304 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
306 if (strlen(resource_path) > sizeof(key_path) - 32) {
307 lwsl_err("resource path too long\n");
311 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
314 info.ssl_cert_filepath = cert_path;
315 info.ssl_private_key_filepath = key_path;
317 info.ssl_ca_filepath = ca_path;
321 info.max_http_header_pool = 1;
322 info.options = opts | LWS_SERVER_OPTION_VALIDATE_UTF8;
323 info.extensions = exts;
324 info.ssl_cipher_list = "ECDHE-ECDSA-AES256-GCM-SHA384:"
325 "ECDHE-RSA-AES256-GCM-SHA384:"
326 "DHE-RSA-AES256-GCM-SHA384:"
327 "ECDHE-RSA-AES256-SHA384:"
328 "HIGH:!aNULL:!eNULL:!EXPORT:"
329 "!DES:!MD5:!PSK:!RC4:!HMAC_SHA1:"
330 "!SHA1:!DHE-RSA-AES128-GCM-SHA256:"
331 "!DHE-RSA-AES128-SHA256:"
332 "!AES128-GCM-SHA256:"
334 "!DHE-RSA-AES256-SHA256:"
335 "!AES256-GCM-SHA384:"
337 context = lws_create_context(&info);
338 if (context == NULL) {
339 lwsl_err("libwebsocket init failed\n");
343 /* this shows how to override the lws file operations. You don't need
344 * to do any of this unless you have a reason (eg, want to serve
345 * compressed files without decompressing the whole archive)
347 /* stash original platform fops */
348 fops_plat = *(lws_get_fops(context));
349 /* override the active fops */
350 lws_get_fops(context)->open = test_server_fops_open;
353 while (n >= 0 && !force_exit) {
356 gettimeofday(&tv, NULL);
359 * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every
360 * live websocket connection using the DUMB_INCREMENT protocol,
361 * as soon as it can take more packets (usually immediately)
364 ms = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
365 if ((ms - oldms) > 50) {
366 lws_callback_on_writable_all_protocol(context,
367 &protocols[PROTOCOL_DUMB_INCREMENT]);
373 * this represents an existing server's single poll action
374 * which also includes libwebsocket sockets
377 n = poll(pollfds, count_pollfds, 50);
382 for (n = 0; n < count_pollfds; n++)
383 if (pollfds[n].revents)
385 * returns immediately if the fd does not
386 * match anything under libwebsockets
389 if (lws_service_fd(context,
394 * If libwebsockets sockets are all we care about,
395 * you can use this api which takes care of the poll()
396 * and looping through finding who needed service.
398 * If no socket needs service, it'll return anyway after
399 * the number of ms in the second argument.
402 n = lws_service(context, 50);
410 lws_context_destroy(context);
412 lwsl_notice("libwebsockets-test-server exited cleanly\n");