2 * libwebsockets-test-server - libwebsockets test implementation
4 * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 #include "test-server.h"
25 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,
75 /* list of supported protocols and callbacks */
77 static struct lws_protocols protocols[] = {
78 /* first protocol must always be HTTP handler */
81 "http-only", /* name */
82 callback_http, /* callback */
83 sizeof (struct per_session_data__http), /* per_session_data_size */
84 0, /* max frame size / rx buffer */
87 "dumb-increment-protocol",
88 callback_dumb_increment,
89 sizeof(struct per_session_data__dumb_increment),
93 "lws-mirror-protocol",
95 sizeof(struct per_session_data__lws_mirror),
98 { NULL, NULL, 0, 0 } /* terminator */
102 /* this shows how to override the lws file operations. You don't need
103 * to do any of this unless you have a reason (eg, want to serve
104 * compressed files without decompressing the whole archive)
106 static lws_filefd_type
107 test_server_fops_open(const char *filename, unsigned long *filelen, int flags)
111 /* call through to original platform implementation */
112 n = fops_plat.open(filename, filelen, flags);
114 lwsl_notice("%s: opening %s, ret %d, len %lu\n", __func__, filename,
120 void sighandler(int sig)
123 lws_cancel_service(context);
126 static struct option options[] = {
127 { "help", no_argument, NULL, 'h' },
128 { "debug", required_argument, NULL, 'd' },
129 { "port", required_argument, NULL, 'p' },
130 { "ssl", no_argument, NULL, 's' },
131 { "allow-non-ssl", no_argument, NULL, 'a' },
132 { "interface", required_argument, NULL, 'i' },
133 { "closetest", no_argument, NULL, 'c' },
134 { "libev", no_argument, NULL, 'e' },
135 #ifndef LWS_NO_DAEMONIZE
136 { "daemonize", no_argument, NULL, 'D' },
138 { "resource_path", required_argument, NULL, 'r' },
142 int main(int argc, char **argv)
144 struct lws_context_creation_info info;
145 char interface_name[128] = "";
146 unsigned int ms, oldms = 0;
147 const char *iface = NULL;
148 char cert_path[1024];
155 int syslog_options = LOG_PID | LOG_PERROR;
157 #ifndef LWS_NO_DAEMONIZE
162 * take care to zero down the info struct, he contains random garbaage
163 * from the stack otherwise
165 memset(&info, 0, sizeof info);
169 n = getopt_long(argc, argv, "eci:hsap:d:Dr:", options, NULL);
174 opts |= LWS_SERVER_OPTION_LIBEV;
176 #ifndef LWS_NO_DAEMONIZE
180 syslog_options &= ~LOG_PERROR;
185 debug_level = atoi(optarg);
191 opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
194 info.port = atoi(optarg);
197 strncpy(interface_name, optarg, sizeof interface_name);
198 interface_name[(sizeof interface_name) - 1] = '\0';
199 iface = interface_name;
203 fprintf(stderr, " Close testing mode -- closes on "
204 "client after 50 dumb increments"
205 "and suppresses lws_mirror spam\n");
208 resource_path = optarg;
209 printf("Setting resource path to \"%s\"\n", resource_path);
212 fprintf(stderr, "Usage: test-server "
213 "[--port=<p>] [--ssl] "
214 "[-d <log bitfield>] "
215 "[--resource_path <path>]\n");
220 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
222 * normally lock path would be /var/lock/lwsts or similar, to
223 * simplify getting started without having to take care about
224 * permissions or running as root, set to /tmp/.lwsts-lock
226 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
227 fprintf(stderr, "Failed to daemonize\n");
232 signal(SIGINT, sighandler);
235 /* we will only try to log things according to our debug_level */
236 setlogmask(LOG_UPTO (LOG_DEBUG));
237 openlog("lwsts", syslog_options, LOG_DAEMON);
240 /* tell the library what debug level to emit and to send it to syslog */
241 lws_set_log_level(debug_level, lwsl_emit_syslog);
243 lwsl_notice("libwebsockets test server - "
244 "(C) Copyright 2010-2015 Andy Green <andy@warmcat.com> - "
245 "licensed under LGPL2.1\n");
247 printf("Using resource path \"%s\"\n", resource_path);
249 max_poll_elements = getdtablesize();
250 pollfds = malloc(max_poll_elements * sizeof (struct lws_pollfd));
251 fd_lookup = malloc(max_poll_elements * sizeof (int));
252 if (pollfds == NULL || fd_lookup == NULL) {
253 lwsl_err("Out of memory pollfds=%d\n", max_poll_elements);
259 info.protocols = protocols;
260 #ifndef LWS_NO_EXTENSIONS
261 info.extensions = lws_get_internal_extensions();
264 info.ssl_cert_filepath = NULL;
265 info.ssl_private_key_filepath = NULL;
268 if (strlen(resource_path) > sizeof(cert_path) - 32) {
269 lwsl_err("resource path too long\n");
272 sprintf(cert_path, "%s/libwebsockets-test-server.pem",
274 if (strlen(resource_path) > sizeof(key_path) - 32) {
275 lwsl_err("resource path too long\n");
278 sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
281 info.ssl_cert_filepath = cert_path;
282 info.ssl_private_key_filepath = key_path;
288 context = lws_create_context(&info);
289 if (context == NULL) {
290 lwsl_err("libwebsocket init failed\n");
294 /* this shows how to override the lws file operations. You don't need
295 * to do any of this unless you have a reason (eg, want to serve
296 * compressed files without decompressing the whole archive)
298 /* stash original platform fops */
299 fops_plat = *(lws_get_fops(context));
300 /* override the active fops */
301 lws_get_fops(context)->open = test_server_fops_open;
304 while (n >= 0 && !force_exit) {
307 gettimeofday(&tv, NULL);
310 * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every
311 * live websocket connection using the DUMB_INCREMENT protocol,
312 * as soon as it can take more packets (usually immediately)
315 ms = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
316 if ((ms - oldms) > 50) {
317 lws_callback_on_writable_all_protocol(
318 &protocols[PROTOCOL_DUMB_INCREMENT]);
324 * this represents an existing server's single poll action
325 * which also includes libwebsocket sockets
328 n = poll(pollfds, count_pollfds, 50);
333 for (n = 0; n < count_pollfds; n++)
334 if (pollfds[n].revents)
336 * returns immediately if the fd does not
337 * match anything under libwebsockets
340 if (lws_service_fd(context,
345 * If libwebsockets sockets are all we care about,
346 * you can use this api which takes care of the poll()
347 * and looping through finding who needed service.
349 * If no socket needs service, it'll return anyway after
350 * the number of ms in the second argument.
353 n = lws_service(context, 50);
361 lws_context_destroy(context);
363 lwsl_notice("libwebsockets-test-server exited cleanly\n");