2 * libwebsockets-test-server - libwebsockets test implementation
4 * Copyright (C) 2010-2011 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,
30 #include "../lib/libwebsockets.h"
32 static int close_testing;
37 #define MAX_POLL_ELEMENTS 100
39 #define MAX_POLL_ELEMENTS (MAX_CLIENTS)
42 struct poll_hash_map {
47 #define POLL_HASH_BITS 8
49 #define POLL_HASH_BUCKETS (1 << POLL_HASH_BITS)
50 #define POLL_ENTRIES_PER_BUCKET (MAX_POLL_ELEMENTS / (1 << (POLL_HASH_BITS - 2)))
51 #define POLL_HASH(num) (num & (POLL_HASH_BUCKETS - 1))
53 struct pollfd pollfds[MAX_POLL_ELEMENTS];
54 struct poll_hash_map pollfd_maps[POLL_HASH_BUCKETS][POLL_ENTRIES_PER_BUCKET];
55 int pollfd_count[POLL_HASH_BUCKETS];
56 int count_pollfds = 0;
58 static int find_poll_map_index(int hash, int fd)
62 for (n = 0; n < pollfd_count[hash]; n++)
63 if (pollfd_maps[hash][n].fd == fd)
69 static int find_pollfd_index(int fd)
72 int hash = POLL_HASH(fd);
74 n = find_poll_map_index(hash, fd);
78 return pollfd_maps[hash][n].index;
81 #endif /* EXTERNAL_POLL */
84 * This demo server shows how to use libwebsockets for one or more
85 * websocket protocols in the same server
87 * It defines the following websocket protocols:
89 * dumb-increment-protocol: once the socket is opened, an incrementing
90 * ascii string is sent down it every 50ms.
91 * If you send "reset\n" on the websocket, then
92 * the incrementing number is reset to 0.
94 * lws-mirror-protocol: copies any received packet to every connection also
95 * using this protocol, including the sender
102 PROTOCOL_DUMB_INCREMENT,
110 #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
113 * We take a strict whitelist approach to stop ../ attacks
118 const char *mimetype;
121 static const struct serveable whitelist[] = {
122 { "/favicon.ico", "image/x-icon" },
123 { "/libwebsockets.org-logo.png", "image/png" },
125 /* last one is the default served if no match */
126 { "/test.html", "text/html" },
129 /* this protocol server (always the first one) just knows how to do HTTP */
131 static int callback_http(struct libwebsocket_context *context,
132 struct libwebsocket *wsi,
133 enum libwebsocket_callback_reasons reason, void *user,
134 void *in, size_t len)
136 char client_name[128];
143 int fd = (int)(long)user;
147 case LWS_CALLBACK_HTTP:
149 for (n = 0; n < (sizeof(whitelist) / sizeof(whitelist[0]) - 1); n++)
150 if (in && strcmp(in, whitelist[n].urlpath) == 0)
153 sprintf(buf, LOCAL_RESOURCE_PATH"%s", whitelist[n].urlpath);
155 if (libwebsockets_serve_http_file(context, wsi, buf, whitelist[n].mimetype))
156 fprintf(stderr, "Failed to send HTTP file\n");
159 * notice that the sending of the file completes asynchronously,
160 * we'll get a LWS_CALLBACK_HTTP_FILE_COMPLETION callback when
166 case LWS_CALLBACK_HTTP_FILE_COMPLETION:
167 // fprintf(stderr, "LWS_CALLBACK_HTTP_FILE_COMPLETION seen\n");
168 /* kill the connection after we sent one file */
172 * callback for confirming to continue with client IP appear in
173 * protocol 0 callback since no websocket protocol has been agreed
174 * yet. You can just ignore this if you won't filter on client IP
175 * since the default uhandled callback return is 0 meaning let the
176 * connection continue.
179 case LWS_CALLBACK_FILTER_NETWORK_CONNECTION:
181 libwebsockets_get_peer_addresses((int)(long)user, client_name,
182 sizeof(client_name), client_ip, sizeof(client_ip));
184 // fprintf(stderr, "Received network connect from %s (%s)\n",
185 // client_name, client_ip);
187 /* if we returned non-zero from here, we kill the connection */
192 * callbacks for managing the external poll() array appear in
193 * protocol 0 callback
196 case LWS_CALLBACK_ADD_POLL_FD:
197 if (count_pollfds == MAX_POLL_ELEMENTS) {
198 fprintf(stderr, "LWS_CALLBACK_ADD_POLL_FD: too many sockets to track\n");
201 hash = POLL_HASH(fd);
202 if (pollfd_count[hash] == POLL_ENTRIES_PER_BUCKET) {
203 fprintf(stderr, "LWS_CALLBACK_ADD_POLL_FD: hash table overflow\n");
207 // fprintf(stderr, "Adding fd %d at pollfd_maps[%d][%d], pollfds[%d]\n", fd, hash, pollfd_count[hash], count_pollfds);
209 pollfd_maps[hash][pollfd_count[hash]].fd = fd;
210 pollfd_maps[hash][pollfd_count[hash]++].index = count_pollfds;
212 pollfds[count_pollfds].fd = (int)(long)user;
213 pollfds[count_pollfds].events = (int)len;
214 pollfds[count_pollfds++].revents = 0;
217 case LWS_CALLBACK_DEL_POLL_FD:
218 hash = POLL_HASH(fd);
219 n = find_poll_map_index(hash, fd);
221 fprintf(stderr, "unable to find fd %d in poll_maps\n", fd);
224 m = pollfd_maps[hash][n].index;
226 assert(pollfds[m].fd == fd);
227 assert(count_pollfds);
228 assert(pollfd_count[hash]);
230 // fprintf(stderr, "Removing fd %d at pollfd_maps[%d][%d], pollfds[%d]\n", fd, hash, n, m);
233 * swap the end guy into our vacant slot...
234 * works ok if n is the end guy
241 hash1 = POLL_HASH(pollfds[count_pollfds].fd);
242 m1 = find_poll_map_index(hash1, pollfds[count_pollfds].fd);
243 /* your index has changed... */
244 pollfd_maps[hash1][m1].index = m;
246 pollfds[m] = pollfds[count_pollfds];
247 pollfds[count_pollfds].fd = -1;
251 * similar trick with hashtable
252 * old end guy goes into vacant slot in hash table
255 pollfd_count[hash]--;
256 if (pollfd_count[hash]) {
257 pollfd_maps[hash][n].index = pollfd_maps[hash][pollfd_count[hash]].index;
258 pollfd_maps[hash][n].fd = pollfd_maps[hash][pollfd_count[hash]].fd;
262 case LWS_CALLBACK_SET_MODE_POLL_FD:
263 n = find_pollfd_index(fd);
265 fprintf(stderr, "unable to find fd %d\n", fd);
268 if(pollfds[n].fd != fd) {
269 fprintf(stderr, "Setting fd %d, found at pollfd_index %d, actually fd %d\n", fd, n, pollfds[n].fd);
272 pollfds[n].events |= (int)(long)len;
275 case LWS_CALLBACK_CLEAR_MODE_POLL_FD:
276 n = find_pollfd_index(fd);
278 fprintf(stderr, "unable to find fd %d\n", fd);
281 assert(pollfds[n].fd == fd);
282 pollfds[n].events &= ~(int)(long)len;
294 * this is just an example of parsing handshake headers, you don't need this
295 * in your code unless you will filter allowing connections by the header
300 dump_handshake_info(struct lws_tokens *lwst)
303 static const char *token_names[WSI_TOKEN_COUNT] = {
304 /*[WSI_TOKEN_GET_URI] =*/ "GET URI",
305 /*[WSI_TOKEN_HOST] =*/ "Host",
306 /*[WSI_TOKEN_CONNECTION] =*/ "Connection",
307 /*[WSI_TOKEN_KEY1] =*/ "key 1",
308 /*[WSI_TOKEN_KEY2] =*/ "key 2",
309 /*[WSI_TOKEN_PROTOCOL] =*/ "Protocol",
310 /*[WSI_TOKEN_UPGRADE] =*/ "Upgrade",
311 /*[WSI_TOKEN_ORIGIN] =*/ "Origin",
312 /*[WSI_TOKEN_DRAFT] =*/ "Draft",
313 /*[WSI_TOKEN_CHALLENGE] =*/ "Challenge",
316 /*[WSI_TOKEN_KEY] =*/ "Key",
317 /*[WSI_TOKEN_VERSION] =*/ "Version",
318 /*[WSI_TOKEN_SWORIGIN] =*/ "Sworigin",
321 /*[WSI_TOKEN_EXTENSIONS] =*/ "Extensions",
323 /* client receives these */
324 /*[WSI_TOKEN_ACCEPT] =*/ "Accept",
325 /*[WSI_TOKEN_NONCE] =*/ "Nonce",
326 /*[WSI_TOKEN_HTTP] =*/ "Http",
327 /*[WSI_TOKEN_MUXURL] =*/ "MuxURL",
330 for (n = 0; n < WSI_TOKEN_COUNT; n++) {
331 if (lwst[n].token == NULL)
334 fprintf(stderr, " %s = %s\n", token_names[n], lwst[n].token);
338 /* dumb_increment protocol */
341 * one of these is auto-created for each connection and a pointer to the
342 * appropriate instance is passed to the callback in the user parameter
344 * for this example protocol we use it to individualize the count for each
348 struct per_session_data__dumb_increment {
353 callback_dumb_increment(struct libwebsocket_context *context,
354 struct libwebsocket *wsi,
355 enum libwebsocket_callback_reasons reason,
356 void *user, void *in, size_t len)
359 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 +
360 LWS_SEND_BUFFER_POST_PADDING];
361 unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
362 struct per_session_data__dumb_increment *pss = user;
366 case LWS_CALLBACK_ESTABLISHED:
367 fprintf(stderr, "callback_dumb_increment: "
368 "LWS_CALLBACK_ESTABLISHED\n");
373 * in this protocol, we just use the broadcast action as the chance to
374 * send our own connection-specific data and ignore the broadcast info
375 * that is available in the 'in' parameter
378 case LWS_CALLBACK_BROADCAST:
379 n = sprintf((char *)p, "%d", pss->number++);
380 n = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
382 fprintf(stderr, "ERROR %d writing to socket\n", n);
385 if (close_testing && pss->number == 50) {
386 fprintf(stderr, "close tesing limit, closing\n");
387 libwebsocket_close_and_free_session(context, wsi,
388 LWS_CLOSE_STATUS_NORMAL);
392 case LWS_CALLBACK_RECEIVE:
393 fprintf(stderr, "rx %d\n", (int)len);
396 if (strcmp(in, "reset\n") == 0)
400 * this just demonstrates how to use the protocol filter. If you won't
401 * study and reject connections based on header content, you don't need
402 * to handle this callback
405 case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
406 dump_handshake_info((struct lws_tokens *)(long)user);
407 /* you could return non-zero here and kill the connection */
418 /* lws-mirror_protocol */
420 #define MAX_MESSAGE_QUEUE 64
422 struct per_session_data__lws_mirror {
423 struct libwebsocket *wsi;
432 static struct a_message ringbuffer[MAX_MESSAGE_QUEUE];
433 static int ringbuffer_head;
437 callback_lws_mirror(struct libwebsocket_context *context,
438 struct libwebsocket *wsi,
439 enum libwebsocket_callback_reasons reason,
440 void *user, void *in, size_t len)
443 struct per_session_data__lws_mirror *pss = user;
447 case LWS_CALLBACK_ESTABLISHED:
448 fprintf(stderr, "callback_lws_mirror: "
449 "LWS_CALLBACK_ESTABLISHED\n");
450 pss->ringbuffer_tail = ringbuffer_head;
454 case LWS_CALLBACK_SERVER_WRITEABLE:
457 if (pss->ringbuffer_tail != ringbuffer_head) {
459 n = libwebsocket_write(wsi, (unsigned char *)
460 ringbuffer[pss->ringbuffer_tail].payload +
461 LWS_SEND_BUFFER_PRE_PADDING,
462 ringbuffer[pss->ringbuffer_tail].len,
465 fprintf(stderr, "ERROR %d writing to socket\n", n);
469 if (pss->ringbuffer_tail == (MAX_MESSAGE_QUEUE - 1))
470 pss->ringbuffer_tail = 0;
472 pss->ringbuffer_tail++;
474 if (((ringbuffer_head - pss->ringbuffer_tail) %
475 MAX_MESSAGE_QUEUE) < (MAX_MESSAGE_QUEUE - 15))
476 libwebsocket_rx_flow_control(wsi, 1);
478 libwebsocket_callback_on_writable(context, wsi);
483 case LWS_CALLBACK_BROADCAST:
484 n = libwebsocket_write(wsi, in, len, LWS_WRITE_TEXT);
486 fprintf(stderr, "mirror write failed\n");
489 case LWS_CALLBACK_RECEIVE:
491 if (ringbuffer[ringbuffer_head].payload)
492 free(ringbuffer[ringbuffer_head].payload);
494 ringbuffer[ringbuffer_head].payload =
495 malloc(LWS_SEND_BUFFER_PRE_PADDING + len +
496 LWS_SEND_BUFFER_POST_PADDING);
497 ringbuffer[ringbuffer_head].len = len;
498 memcpy((char *)ringbuffer[ringbuffer_head].payload +
499 LWS_SEND_BUFFER_PRE_PADDING, in, len);
500 if (ringbuffer_head == (MAX_MESSAGE_QUEUE - 1))
505 if (((ringbuffer_head - pss->ringbuffer_tail) %
506 MAX_MESSAGE_QUEUE) > (MAX_MESSAGE_QUEUE - 10))
507 libwebsocket_rx_flow_control(wsi, 0);
509 libwebsocket_callback_on_writable_all_protocol(
510 libwebsockets_get_protocol(wsi));
513 * this just demonstrates how to use the protocol filter. If you won't
514 * study and reject connections based on header content, you don't need
515 * to handle this callback
518 case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
519 dump_handshake_info((struct lws_tokens *)(long)user);
520 /* you could return non-zero here and kill the connection */
531 /* list of supported protocols and callbacks */
533 static struct libwebsocket_protocols protocols[] = {
534 /* first protocol must always be HTTP handler */
537 "http-only", /* name */
538 callback_http, /* callback */
539 0 /* per_session_data_size */
542 "dumb-increment-protocol",
543 callback_dumb_increment,
544 sizeof(struct per_session_data__dumb_increment),
547 "lws-mirror-protocol",
549 sizeof(struct per_session_data__lws_mirror)
552 NULL, NULL, 0 /* End of list */
556 static struct option options[] = {
557 { "help", no_argument, NULL, 'h' },
558 { "debug", required_argument, NULL, 'd' },
559 { "port", required_argument, NULL, 'p' },
560 { "ssl", no_argument, NULL, 's' },
561 { "killmask", no_argument, NULL, 'k' },
562 { "interface", required_argument, NULL, 'i' },
563 { "closetest", no_argument, NULL, 'c' },
567 int main(int argc, char **argv)
570 const char *cert_path =
571 LOCAL_RESOURCE_PATH"/libwebsockets-test-server.pem";
572 const char *key_path =
573 LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem";
574 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1024 +
575 LWS_SEND_BUFFER_POST_PADDING];
578 struct libwebsocket_context *context;
580 char interface_name[128] = "";
581 const char *interface = NULL;
583 unsigned int oldus = 0;
586 fprintf(stderr, "libwebsockets test server\n"
587 "(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> "
588 "licensed under LGPL2.1\n");
591 n = getopt_long(argc, argv, "ci:khsp:d:", options, NULL);
596 lws_set_log_level(atoi(optarg), NULL);
602 opts = LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK;
608 strncpy(interface_name, optarg, sizeof interface_name);
609 interface_name[(sizeof interface_name) - 1] = '\0';
610 interface = interface_name;
614 fprintf(stderr, " Close testing mode -- closes on "
615 "client after 50 dumb increments"
616 "and suppresses lws_mirror spam\n");
619 fprintf(stderr, "Usage: test-server "
620 "[--port=<p>] [--ssl] "
621 "[-d <log bitfield>]\n");
627 cert_path = key_path = NULL;
629 context = libwebsocket_create_context(port, interface, protocols,
630 libwebsocket_internal_extensions,
631 cert_path, key_path, NULL, -1, -1, opts, NULL);
632 if (context == NULL) {
633 fprintf(stderr, "libwebsocket init failed\n");
637 buf[LWS_SEND_BUFFER_PRE_PADDING] = 'x';
642 * This example shows how to work with no forked service loop
645 fprintf(stderr, " Using no-fork service loop\n");
651 gettimeofday(&tv, NULL);
654 * This broadcasts to all dumb-increment-protocol connections
657 * We're just sending a character 'x', in these examples the
658 * callbacks send their own per-connection content.
660 * You have to send something with nonzero length to get the
661 * callback actions delivered.
663 * We take care of pre-and-post padding allocation.
666 if (((unsigned int)tv.tv_usec - oldus) > 50000) {
667 libwebsockets_broadcast(
668 &protocols[PROTOCOL_DUMB_INCREMENT],
669 &buf[LWS_SEND_BUFFER_PRE_PADDING], 1);
674 * This example server does not fork or create a thread for
675 * websocket service, it all runs in this single loop. So,
676 * we have to give the websockets an opportunity to service
679 * If no socket is needing service, the call below returns
680 * immediately and quickly. Negative return means we are
681 * in process of closing
686 * this represents an existing server's single poll action
687 * which also includes libwebsocket sockets
690 n = poll(pollfds, count_pollfds, 50);
696 for (n = 0; n < count_pollfds; n++)
697 if (pollfds[n].revents)
699 * returns immediately if the fd does not
700 * match anything under libwebsockets
703 if (libwebsocket_service_fd(context,
707 n = libwebsocket_service(context, 50);
711 #else /* !LWS_NO_FORK */
714 * This example shows how to work with the forked websocket service loop
717 fprintf(stderr, " Using forked service loop\n");
720 * This forks the websocket service action into a subprocess so we
721 * don't have to take care about it.
724 n = libwebsockets_fork_service_loop(context);
726 fprintf(stderr, "Unable to fork service loop %d\n", n);
735 * This broadcasts to all dumb-increment-protocol connections
738 * We're just sending a character 'x', in these examples the
739 * callbacks send their own per-connection content.
741 * You have to send something with nonzero length to get the
742 * callback actions delivered.
744 * We take care of pre-and-post padding allocation.
747 libwebsockets_broadcast(&protocols[PROTOCOL_DUMB_INCREMENT],
748 &buf[LWS_SEND_BUFFER_PRE_PADDING], 1);
756 libwebsocket_context_destroy(context);