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"
25 int max_poll_elements;
29 struct lws_pollfd *pollfds;
33 volatile int force_exit = 0;
34 struct lws_context *context;
36 #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
37 char crl_path[1024] = "";
40 #define LWS_PLUGIN_STATIC
41 #include "../plugins/protocol_lws_mirror.c"
42 #include "../plugins/protocol_lws_status.c"
45 * This mutex lock protects code that changes or relies on wsi list outside of
46 * the service thread. The service thread will acquire it when changing the
47 * wsi list and other threads should acquire it while dereferencing wsis or
48 * calling apis like lws_callback_on_writable_all_protocol() which
49 * use the wsi list and wsis from a different thread context.
51 pthread_mutex_t lock_established_conns;
53 /* http server gets files from this path */
54 #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
55 char *resource_path = LOCAL_RESOURCE_PATH;
58 * multithreaded version - protect wsi lifecycle changes in the library
59 * these are called from protocol 0 callbacks
62 void test_server_lock(int care)
65 pthread_mutex_lock(&lock_established_conns);
67 void test_server_unlock(int care)
70 pthread_mutex_unlock(&lock_established_conns);
74 * This demo server shows how to use libwebsockets for one or more
75 * websocket protocols in the same server
77 * It defines the following websocket protocols:
79 * dumb-increment-protocol: once the socket is opened, an incrementing
80 * ascii string is sent down it every 50ms.
81 * If you send "reset\n" on the websocket, then
82 * the incrementing number is reset to 0.
84 * lws-mirror-protocol: copies any received packet to every connection also
85 * using this protocol, including the sender
92 PROTOCOL_DUMB_INCREMENT,
100 /* list of supported protocols and callbacks */
102 static struct lws_protocols protocols[] = {
103 /* first protocol must always be HTTP handler */
106 "http-only", /* name */
107 callback_http, /* callback */
108 sizeof (struct per_session_data__http), /* per_session_data_size */
109 0, /* max frame size / rx buffer */
112 "dumb-increment-protocol",
113 callback_dumb_increment,
114 sizeof(struct per_session_data__dumb_increment),
115 10, /* rx buf size must be >= permessage-deflate rx size
116 * dumb-increment only sends very small packets, so we set
117 * this accordingly. If your protocol will send bigger
118 * things, adjust this to match */
120 LWS_PLUGIN_PROTOCOL_MIRROR,
121 LWS_PLUGIN_PROTOCOL_LWS_STATUS,
122 { NULL, NULL, 0, 0 } /* terminator */
125 void *thread_dumb_increment(void *threadid)
127 while (!force_exit) {
129 * this lock means wsi in the active list cannot
130 * disappear underneath us, because the code to add and remove
131 * them is protected by the same lock
133 pthread_mutex_lock(&lock_established_conns);
134 lws_callback_on_writable_all_protocol(context,
135 &protocols[PROTOCOL_DUMB_INCREMENT]);
136 pthread_mutex_unlock(&lock_established_conns);
143 void *thread_service(void *threadid)
145 while (lws_service_tsi(context, 50, (int)(lws_intptr_t)threadid) >= 0 && !force_exit)
151 void sighandler(int sig)
154 lws_cancel_service(context);
157 static const struct lws_extension exts[] = {
159 "permessage-deflate",
160 lws_extension_callback_pm_deflate,
161 "permessage-deflate; client_no_context_takeover; client_max_window_bits"
165 lws_extension_callback_pm_deflate,
168 { NULL, NULL, NULL /* terminator */ }
171 static struct option options[] = {
172 { "help", no_argument, NULL, 'h' },
173 { "debug", required_argument, NULL, 'd' },
174 { "port", required_argument, NULL, 'p' },
175 { "ssl", no_argument, NULL, 's' },
176 { "allow-non-ssl", no_argument, NULL, 'a' },
177 { "interface", required_argument, NULL, 'i' },
178 { "closetest", no_argument, NULL, 'c' },
179 { "libev", no_argument, NULL, 'e' },
180 { "threads", required_argument, NULL, 'j' },
181 #ifndef LWS_NO_DAEMONIZE
182 { "daemonize", no_argument, NULL, 'D' },
184 { "resource_path", required_argument, NULL, 'r' },
188 int main(int argc, char **argv)
190 struct lws_context_creation_info info;
191 char interface_name[128] = "";
192 const char *iface = NULL;
193 pthread_t pthread_dumb, pthread_service[32];
194 char cert_path[1024];
202 /* LOG_PERROR is not POSIX standard, and may not be portable */
204 int syslog_options = LOG_PID;
206 int syslog_options = LOG_PID | LOG_PERROR;
209 #ifndef LWS_NO_DAEMONIZE
214 * take care to zero down the info struct, he contains random garbaage
215 * from the stack otherwise
217 memset(&info, 0, sizeof info);
220 pthread_mutex_init(&lock_established_conns, NULL);
223 n = getopt_long(argc, argv, "eci:hsap:d:Dr:j:", options, NULL);
228 threads = atoi(optarg);
229 if (threads > ARRAY_SIZE(pthread_service)) {
230 lwsl_err("Max threads %lu\n",
231 (unsigned long)ARRAY_SIZE(pthread_service));
236 opts |= LWS_SERVER_OPTION_LIBEV;
238 #ifndef LWS_NO_DAEMONIZE
241 #if !defined(_WIN32) && !defined(__sun)
242 syslog_options &= ~LOG_PERROR;
247 debug_level = atoi(optarg);
253 opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
256 info.port = atoi(optarg);
259 strncpy(interface_name, optarg, sizeof interface_name);
260 interface_name[(sizeof interface_name) - 1] = '\0';
261 iface = interface_name;
265 fprintf(stderr, " Close testing mode -- closes on "
266 "client after 50 dumb increments"
267 "and suppresses lws_mirror spam\n");
270 resource_path = optarg;
271 printf("Setting resource path to \"%s\"\n", resource_path);
274 fprintf(stderr, "Usage: test-server "
275 "[--port=<p>] [--ssl] "
276 "[-d <log bitfield>] "
277 "[--resource_path <path>]\n");
282 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
284 * normally lock path would be /var/lock/lwsts or similar, to
285 * simplify getting started without having to take care about
286 * permissions or running as root, set to /tmp/.lwsts-lock
288 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
289 fprintf(stderr, "Failed to daemonize\n");
294 signal(SIGINT, sighandler);
297 /* we will only try to log things according to our debug_level */
298 setlogmask(LOG_UPTO (LOG_DEBUG));
299 openlog("lwsts", syslog_options, LOG_DAEMON);
302 /* tell the library what debug level to emit and to send it to syslog */
303 lws_set_log_level(debug_level, lwsl_emit_syslog);
304 lwsl_notice("libwebsockets test server pthreads - license LGPL2.1+SLE\n");
305 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
307 printf("Using resource path \"%s\"\n", resource_path);
309 max_poll_elements = getdtablesize();
310 pollfds = malloc(max_poll_elements * sizeof (struct lws_pollfd));
311 fd_lookup = malloc(max_poll_elements * sizeof (int));
312 if (pollfds == NULL || fd_lookup == NULL) {
313 lwsl_err("Out of memory pollfds=%d\n", max_poll_elements);
319 info.protocols = protocols;
320 info.extensions = exts;
322 info.ssl_cert_filepath = NULL;
323 info.ssl_private_key_filepath = NULL;
326 if (strlen(resource_path) > sizeof(cert_path) - 32) {
327 lwsl_err("resource path too long\n");
330 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
332 if (strlen(resource_path) > sizeof(key_path) - 32) {
333 lwsl_err("resource path too long\n");
336 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
339 info.ssl_cert_filepath = cert_path;
340 info.ssl_private_key_filepath = key_path;
345 info.count_threads = threads;
346 info.extensions = exts;
347 info.max_http_header_pool = 4;
349 context = lws_create_context(&info);
350 if (context == NULL) {
351 lwsl_err("libwebsocket init failed\n");
355 /* start the dumb increment thread */
357 n = pthread_create(&pthread_dumb, NULL, thread_dumb_increment, 0);
359 lwsl_err("Unable to create dumb thread\n");
364 * notice the actual number of threads may be capped by the library,
365 * so use lws_get_count_threads() to get the actual amount of threads
369 for (n = 0; n < lws_get_count_threads(context); n++)
370 if (pthread_create(&pthread_service[n], NULL, thread_service,
371 (void *)(lws_intptr_t)n))
372 lwsl_err("Failed to start service thread\n");
374 /* wait for all the service threads to exit */
377 pthread_join(pthread_service[n], &retval);
379 /* wait for pthread_dumb to exit */
380 pthread_join(pthread_dumb, &retval);
383 lws_context_destroy(context);
384 pthread_mutex_destroy(&lock_established_conns);
386 lwsl_notice("libwebsockets-test-server exited cleanly\n");