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_USE_POLARSSL)
38 #if defined(LWS_USE_MBEDTLS)
40 #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
41 char crl_path[1024] = "";
47 * This mutex lock protects code that changes or relies on wsi list outside of
48 * the service thread. The service thread will acquire it when changing the
49 * wsi list and other threads should acquire it while dereferencing wsis or
50 * calling apis like lws_callback_on_writable_all_protocol() which
51 * use the wsi list and wsis from a different thread context.
53 pthread_mutex_t lock_established_conns;
55 /* http server gets files from this path */
56 #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
57 char *resource_path = LOCAL_RESOURCE_PATH;
60 * multithreaded version - protect wsi lifecycle changes in the library
61 * these are called from protocol 0 callbacks
64 void test_server_lock(int care)
67 pthread_mutex_lock(&lock_established_conns);
69 void test_server_unlock(int care)
72 pthread_mutex_unlock(&lock_established_conns);
76 * This demo server shows how to use libwebsockets for one or more
77 * websocket protocols in the same server
79 * It defines the following websocket protocols:
81 * dumb-increment-protocol: once the socket is opened, an incrementing
82 * ascii string is sent down it every 50ms.
83 * If you send "reset\n" on the websocket, then
84 * the incrementing number is reset to 0.
86 * lws-mirror-protocol: copies any received packet to every connection also
87 * using this protocol, including the sender
94 PROTOCOL_DUMB_INCREMENT,
101 /* list of supported protocols and callbacks */
103 static struct lws_protocols protocols[] = {
104 /* first protocol must always be HTTP handler */
107 "http-only", /* name */
108 callback_http, /* callback */
109 sizeof (struct per_session_data__http), /* per_session_data_size */
110 0, /* max frame size / rx buffer */
113 "dumb-increment-protocol",
114 callback_dumb_increment,
115 sizeof(struct per_session_data__dumb_increment),
119 "lws-mirror-protocol",
121 sizeof(struct per_session_data__lws_mirror),
124 { NULL, NULL, 0, 0 } /* terminator */
127 void *thread_dumb_increment(void *threadid)
129 while (!force_exit) {
131 * this lock means wsi in the active list cannot
132 * disappear underneath us, because the code to add and remove
133 * them is protected by the same lock
135 pthread_mutex_lock(&lock_established_conns);
136 lws_callback_on_writable_all_protocol(context,
137 &protocols[PROTOCOL_DUMB_INCREMENT]);
138 pthread_mutex_unlock(&lock_established_conns);
145 void *thread_service(void *threadid)
147 while (lws_service_tsi(context, 50, (int)(long)threadid) >= 0 && !force_exit)
153 void sighandler(int sig)
156 lws_cancel_service(context);
159 static const struct lws_extension exts[] = {
161 "permessage-deflate",
162 lws_extension_callback_pm_deflate,
163 "permessage-deflate; client_no_context_takeover; client_max_window_bits"
167 lws_extension_callback_pm_deflate,
170 { NULL, NULL, NULL /* terminator */ }
173 static struct option options[] = {
174 { "help", no_argument, NULL, 'h' },
175 { "debug", required_argument, NULL, 'd' },
176 { "port", required_argument, NULL, 'p' },
177 { "ssl", no_argument, NULL, 's' },
178 { "allow-non-ssl", no_argument, NULL, 'a' },
179 { "interface", required_argument, NULL, 'i' },
180 { "closetest", no_argument, NULL, 'c' },
181 { "libev", no_argument, NULL, 'e' },
182 { "threads", required_argument, NULL, 'j' },
183 #ifndef LWS_NO_DAEMONIZE
184 { "daemonize", no_argument, NULL, 'D' },
186 { "resource_path", required_argument, NULL, 'r' },
190 int main(int argc, char **argv)
192 struct lws_context_creation_info info;
193 char interface_name[128] = "";
194 const char *iface = NULL;
195 pthread_t pthread_dumb, pthread_service[32];
196 char cert_path[1024];
204 /* LOG_PERROR is not POSIX standard, and may not be portable */
206 int syslog_options = LOG_PID;
208 int syslog_options = LOG_PID | LOG_PERROR;
211 #ifndef LWS_NO_DAEMONIZE
216 * take care to zero down the info struct, he contains random garbaage
217 * from the stack otherwise
219 memset(&info, 0, sizeof info);
222 pthread_mutex_init(&lock_established_conns, NULL);
225 n = getopt_long(argc, argv, "eci:hsap:d:Dr:j:", options, NULL);
230 threads = atoi(optarg);
231 if (threads > ARRAY_SIZE(pthread_service)) {
232 lwsl_err("Max threads %d\n",
233 ARRAY_SIZE(pthread_service));
238 opts |= LWS_SERVER_OPTION_LIBEV;
240 #ifndef LWS_NO_DAEMONIZE
243 #if !defined(_WIN32) && !defined(__sun)
244 syslog_options &= ~LOG_PERROR;
249 debug_level = atoi(optarg);
255 opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
258 info.port = atoi(optarg);
261 strncpy(interface_name, optarg, sizeof interface_name);
262 interface_name[(sizeof interface_name) - 1] = '\0';
263 iface = interface_name;
267 fprintf(stderr, " Close testing mode -- closes on "
268 "client after 50 dumb increments"
269 "and suppresses lws_mirror spam\n");
272 resource_path = optarg;
273 printf("Setting resource path to \"%s\"\n", resource_path);
276 fprintf(stderr, "Usage: test-server "
277 "[--port=<p>] [--ssl] "
278 "[-d <log bitfield>] "
279 "[--resource_path <path>]\n");
284 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
286 * normally lock path would be /var/lock/lwsts or similar, to
287 * simplify getting started without having to take care about
288 * permissions or running as root, set to /tmp/.lwsts-lock
290 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
291 fprintf(stderr, "Failed to daemonize\n");
296 signal(SIGINT, sighandler);
299 /* we will only try to log things according to our debug_level */
300 setlogmask(LOG_UPTO (LOG_DEBUG));
301 openlog("lwsts", syslog_options, LOG_DAEMON);
304 /* tell the library what debug level to emit and to send it to syslog */
305 lws_set_log_level(debug_level, lwsl_emit_syslog);
306 lwsl_notice("libwebsockets test server pthreads - license LGPL2.1+SLE\n");
307 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
309 printf("Using resource path \"%s\"\n", resource_path);
311 max_poll_elements = getdtablesize();
312 pollfds = malloc(max_poll_elements * sizeof (struct lws_pollfd));
313 fd_lookup = malloc(max_poll_elements * sizeof (int));
314 if (pollfds == NULL || fd_lookup == NULL) {
315 lwsl_err("Out of memory pollfds=%d\n", max_poll_elements);
321 info.protocols = protocols;
322 info.extensions = exts;
324 info.ssl_cert_filepath = NULL;
325 info.ssl_private_key_filepath = NULL;
328 if (strlen(resource_path) > sizeof(cert_path) - 32) {
329 lwsl_err("resource path too long\n");
332 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
334 if (strlen(resource_path) > sizeof(key_path) - 32) {
335 lwsl_err("resource path too long\n");
338 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
341 info.ssl_cert_filepath = cert_path;
342 info.ssl_private_key_filepath = key_path;
347 info.count_threads = threads;
348 info.extensions = exts;
349 info.max_http_header_pool = 4;
351 context = lws_create_context(&info);
352 if (context == NULL) {
353 lwsl_err("libwebsocket init failed\n");
357 /* start the dumb increment thread */
359 n = pthread_create(&pthread_dumb, NULL, thread_dumb_increment, 0);
361 lwsl_err("Unable to create dumb thread\n");
366 * notice the actual number of threads may be capped by the library,
367 * so use lws_get_count_threads() to get the actual amount of threads
371 for (n = 0; n < lws_get_count_threads(context); n++)
372 if (pthread_create(&pthread_service[n], NULL, thread_service,
374 lwsl_err("Failed to start service thread\n");
376 /* wait for all the service threads to exit */
379 pthread_join(pthread_service[n], &retval);
381 /* wait for pthread_dumb to exit */
382 pthread_join(pthread_dumb, &retval);
385 lws_context_destroy(context);
386 pthread_mutex_destroy(&lock_established_conns);
388 lwsl_notice("libwebsockets-test-server exited cleanly\n");