Andy Green | a0da8a8 | 2010-11-08 17:12:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libwebsockets-test-server - libwebsockets test implementation |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 3 | * |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 4 | * Copyright (C) 2010-2011 Andy Green <andy@warmcat.com> |
Andy Green | a0da8a8 | 2010-11-08 17:12:19 +0000 | [diff] [blame] | 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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, |
| 19 | * MA 02110-1301 USA |
| 20 | */ |
Joakim Soderberg | d2edfec | 2013-02-06 15:27:27 +0900 | [diff] [blame] | 21 | #ifdef CMAKE_BUILD |
| 22 | #include "lws_config.h" |
| 23 | #endif |
Andy Green | a0da8a8 | 2010-11-08 17:12:19 +0000 | [diff] [blame] | 24 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <unistd.h> |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 28 | #include <getopt.h> |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 29 | #include <string.h> |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 30 | #include <sys/time.h> |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 31 | #include <sys/stat.h> |
| 32 | #include <fcntl.h> |
Andy Green | 5fc460c | 2013-01-15 19:44:33 +0800 | [diff] [blame] | 33 | #include <assert.h> |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 34 | #ifdef WIN32 |
| 35 | |
| 36 | #ifdef EXTERNAL_POLL |
| 37 | #ifndef WIN32_LEAN_AND_MEAN |
| 38 | #define WIN32_LEAN_AND_MEAN |
| 39 | #endif |
| 40 | #include <winsock2.h> |
| 41 | #include <ws2tcpip.h> |
| 42 | #include <stddef.h> |
| 43 | |
| 44 | #include "websock-w32.h" |
| 45 | #endif |
| 46 | |
| 47 | #else // NOT WIN32 |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 48 | #include <syslog.h> |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 49 | #endif |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 50 | |
Andy Green | 08f2c01 | 2013-01-30 08:02:26 +0800 | [diff] [blame] | 51 | #include <signal.h> |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 52 | |
Andy Green | 7310e9c | 2010-11-01 09:12:17 +0000 | [diff] [blame] | 53 | #include "../lib/libwebsockets.h" |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 54 | |
Andy Green | 69758fa | 2011-03-07 07:08:07 +0000 | [diff] [blame] | 55 | static int close_testing; |
Edwin van den Oetelaar | 596b220 | 2013-01-20 20:51:14 +0800 | [diff] [blame] | 56 | int max_poll_elements; |
Andy Green | 5fc460c | 2013-01-15 19:44:33 +0800 | [diff] [blame] | 57 | |
Edwin van den Oetelaar | 596b220 | 2013-01-20 20:51:14 +0800 | [diff] [blame] | 58 | struct pollfd *pollfds; |
| 59 | int *fd_lookup; |
Edwin van der Oetelaar | 6c720c4 | 2013-01-17 11:16:15 +0800 | [diff] [blame] | 60 | int count_pollfds; |
Andy Green | 08f2c01 | 2013-01-30 08:02:26 +0800 | [diff] [blame] | 61 | int force_exit = 0; |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 62 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 63 | /* |
| 64 | * This demo server shows how to use libwebsockets for one or more |
| 65 | * websocket protocols in the same server |
| 66 | * |
| 67 | * It defines the following websocket protocols: |
| 68 | * |
| 69 | * dumb-increment-protocol: once the socket is opened, an incrementing |
| 70 | * ascii string is sent down it every 50ms. |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 71 | * If you send "reset\n" on the websocket, then |
| 72 | * the incrementing number is reset to 0. |
Andy Green | 5195968 | 2010-11-12 14:12:13 +0000 | [diff] [blame] | 73 | * |
| 74 | * lws-mirror-protocol: copies any received packet to every connection also |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 75 | * using this protocol, including the sender |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 76 | */ |
| 77 | |
Andy Green | c87fc2f | 2011-01-18 15:29:04 +0000 | [diff] [blame] | 78 | enum demo_protocols { |
| 79 | /* always first */ |
| 80 | PROTOCOL_HTTP = 0, |
| 81 | |
| 82 | PROTOCOL_DUMB_INCREMENT, |
| 83 | PROTOCOL_LWS_MIRROR, |
| 84 | |
| 85 | /* always last */ |
| 86 | DEMO_PROTOCOL_COUNT |
| 87 | }; |
| 88 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 89 | |
Andy Green | cce2a81 | 2012-04-12 11:06:05 +0800 | [diff] [blame] | 90 | #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server" |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 91 | char *resource_path = LOCAL_RESOURCE_PATH; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 92 | |
Andy Green | bb2dc8a | 2013-01-16 10:06:28 +0800 | [diff] [blame] | 93 | /* |
| 94 | * We take a strict whitelist approach to stop ../ attacks |
| 95 | */ |
| 96 | |
| 97 | struct serveable { |
| 98 | const char *urlpath; |
| 99 | const char *mimetype; |
| 100 | }; |
| 101 | |
| 102 | static const struct serveable whitelist[] = { |
| 103 | { "/favicon.ico", "image/x-icon" }, |
| 104 | { "/libwebsockets.org-logo.png", "image/png" }, |
| 105 | |
| 106 | /* last one is the default served if no match */ |
| 107 | { "/test.html", "text/html" }, |
| 108 | }; |
| 109 | |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 110 | struct per_session_data__http { |
| 111 | int fd; |
| 112 | }; |
| 113 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 114 | /* this protocol server (always the first one) just knows how to do HTTP */ |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 115 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 116 | static int callback_http(struct libwebsocket_context *context, |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 117 | struct libwebsocket *wsi, |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 118 | enum libwebsocket_callback_reasons reason, void *user, |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 119 | void *in, size_t len) |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 120 | { |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 121 | #if 0 |
Andy Green | 863d4d2 | 2011-02-13 08:40:37 +0000 | [diff] [blame] | 122 | char client_name[128]; |
| 123 | char client_ip[128]; |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 124 | #endif |
Andy Green | bb2dc8a | 2013-01-16 10:06:28 +0800 | [diff] [blame] | 125 | char buf[256]; |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 126 | int n, m; |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 127 | unsigned char *p; |
Andy Green | 73a820a | 2013-02-18 11:32:49 +0800 | [diff] [blame] | 128 | static unsigned char buffer[4096]; |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 129 | struct stat stat_buf; |
| 130 | struct per_session_data__http *pss = (struct per_session_data__http *)user; |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 131 | #ifdef EXTERNAL_POLL |
Andy Green | 50097dd | 2013-02-15 22:36:30 +0800 | [diff] [blame] | 132 | int fd = (int)(long)in; |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 133 | #endif |
Andy Green | 863d4d2 | 2011-02-13 08:40:37 +0000 | [diff] [blame] | 134 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 135 | switch (reason) { |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 136 | case LWS_CALLBACK_HTTP: |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 137 | |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 138 | /* check for the "send a big file by hand" example case */ |
| 139 | |
| 140 | if (!strcmp((const char *)in, "/leaf.jpg")) { |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 141 | char leaf_path[1024]; |
| 142 | snprintf(leaf_path, sizeof(leaf_path), "%s/leaf.jpg", resource_path); |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 143 | |
| 144 | /* well, let's demonstrate how to send the hard way */ |
| 145 | |
| 146 | p = buffer; |
| 147 | |
Michel Archambault | e8c00aa | 2013-03-14 11:45:49 -0400 | [diff] [blame] | 148 | #ifdef WIN32 |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 149 | pss->fd = open(leaf_path, O_RDONLY | _O_BINARY); |
Michel Archambault | e8c00aa | 2013-03-14 11:45:49 -0400 | [diff] [blame] | 150 | #else |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 151 | pss->fd = open(leaf_path, O_RDONLY); |
Michel Archambault | e8c00aa | 2013-03-14 11:45:49 -0400 | [diff] [blame] | 152 | #endif |
| 153 | |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 154 | if (pss->fd < 0) |
| 155 | return -1; |
| 156 | |
| 157 | fstat(pss->fd, &stat_buf); |
| 158 | |
| 159 | /* |
| 160 | * we will send a big jpeg file, but it could be |
| 161 | * anything. Set the Content-Type: appropriately |
| 162 | * so the browser knows what to do with it. |
| 163 | */ |
| 164 | |
| 165 | p += sprintf((char *)p, |
| 166 | "HTTP/1.0 200 OK\x0d\x0a" |
| 167 | "Server: libwebsockets\x0d\x0a" |
Luc | e46043c | 2013-02-23 11:01:21 +0800 | [diff] [blame] | 168 | "Content-Type: image/jpeg\x0d\x0a" |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 169 | "Content-Length: %u\x0d\x0a\x0d\x0a", |
| 170 | (unsigned int)stat_buf.st_size); |
| 171 | |
| 172 | /* |
| 173 | * send the http headers... |
| 174 | * this won't block since it's the first payload sent |
| 175 | * on the connection since it was established |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 176 | * (too small for partial) |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 177 | */ |
| 178 | |
| 179 | n = libwebsocket_write(wsi, buffer, |
| 180 | p - buffer, LWS_WRITE_HTTP); |
| 181 | |
| 182 | if (n < 0) { |
| 183 | close(pss->fd); |
| 184 | return -1; |
| 185 | } |
| 186 | /* |
| 187 | * book us a LWS_CALLBACK_HTTP_WRITEABLE callback |
| 188 | */ |
| 189 | libwebsocket_callback_on_writable(context, wsi); |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | /* if not, send a file the easy way */ |
| 194 | |
Andy Green | bb2dc8a | 2013-01-16 10:06:28 +0800 | [diff] [blame] | 195 | for (n = 0; n < (sizeof(whitelist) / sizeof(whitelist[0]) - 1); n++) |
Andy Green | 36eb70d | 2013-02-01 08:42:15 +0800 | [diff] [blame] | 196 | if (in && strcmp((const char *)in, whitelist[n].urlpath) == 0) |
Andy Green | bb2dc8a | 2013-01-16 10:06:28 +0800 | [diff] [blame] | 197 | break; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 198 | |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 199 | sprintf(buf, "%s%s", resource_path, whitelist[n].urlpath); |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 200 | |
Andy Green | bb2dc8a | 2013-01-16 10:06:28 +0800 | [diff] [blame] | 201 | if (libwebsockets_serve_http_file(context, wsi, buf, whitelist[n].mimetype)) |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 202 | return -1; /* through completion or error, close the socket */ |
Andy Green | 24b588b | 2013-01-13 09:53:18 +0800 | [diff] [blame] | 203 | |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 204 | /* |
| 205 | * notice that the sending of the file completes asynchronously, |
| 206 | * we'll get a LWS_CALLBACK_HTTP_FILE_COMPLETION callback when |
| 207 | * it's done |
| 208 | */ |
| 209 | |
Andy Green | bb2dc8a | 2013-01-16 10:06:28 +0800 | [diff] [blame] | 210 | break; |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 211 | |
| 212 | case LWS_CALLBACK_HTTP_FILE_COMPLETION: |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 213 | // lwsl_info("LWS_CALLBACK_HTTP_FILE_COMPLETION seen\n"); |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 214 | /* kill the connection after we sent one file */ |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 215 | return -1; |
| 216 | |
| 217 | case LWS_CALLBACK_HTTP_WRITEABLE: |
| 218 | /* |
| 219 | * we can send more of whatever it is we were sending |
| 220 | */ |
| 221 | |
| 222 | do { |
| 223 | n = read(pss->fd, buffer, sizeof buffer); |
| 224 | /* problem reading, close conn */ |
| 225 | if (n < 0) |
| 226 | goto bail; |
| 227 | /* sent it all, close conn */ |
| 228 | if (n == 0) |
| 229 | goto bail; |
| 230 | /* |
| 231 | * because it's HTTP and not websocket, don't need to take |
| 232 | * care about pre and postamble |
| 233 | */ |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 234 | m = libwebsocket_write(wsi, buffer, n, LWS_WRITE_HTTP); |
| 235 | if (m < 0) |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 236 | /* write failed, close conn */ |
| 237 | goto bail; |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 238 | if (m != n) |
| 239 | /* partial write, adjust */ |
| 240 | lseek(pss->fd, m - n, SEEK_CUR); |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 241 | |
| 242 | } while (!lws_send_pipe_choked(wsi)); |
| 243 | libwebsocket_callback_on_writable(context, wsi); |
| 244 | break; |
| 245 | |
| 246 | bail: |
| 247 | close(pss->fd); |
| 248 | return -1; |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame] | 249 | |
Andy Green | 863d4d2 | 2011-02-13 08:40:37 +0000 | [diff] [blame] | 250 | /* |
| 251 | * callback for confirming to continue with client IP appear in |
| 252 | * protocol 0 callback since no websocket protocol has been agreed |
| 253 | * yet. You can just ignore this if you won't filter on client IP |
| 254 | * since the default uhandled callback return is 0 meaning let the |
| 255 | * connection continue. |
| 256 | */ |
| 257 | |
| 258 | case LWS_CALLBACK_FILTER_NETWORK_CONNECTION: |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 259 | #if 0 |
Edwin van den Oetelaar | 8c8a8e1 | 2013-02-20 20:56:59 +0800 | [diff] [blame] | 260 | libwebsockets_get_peer_addresses(context, wsi, (int)(long)in, client_name, |
Andy Green | 863d4d2 | 2011-02-13 08:40:37 +0000 | [diff] [blame] | 261 | sizeof(client_name), client_ip, sizeof(client_ip)); |
| 262 | |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 263 | fprintf(stderr, "Received network connect from %s (%s)\n", |
| 264 | client_name, client_ip); |
| 265 | #endif |
Andy Green | 863d4d2 | 2011-02-13 08:40:37 +0000 | [diff] [blame] | 266 | /* if we returned non-zero from here, we kill the connection */ |
| 267 | break; |
| 268 | |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 269 | #ifdef EXTERNAL_POLL |
| 270 | /* |
| 271 | * callbacks for managing the external poll() array appear in |
| 272 | * protocol 0 callback |
| 273 | */ |
| 274 | |
| 275 | case LWS_CALLBACK_ADD_POLL_FD: |
Edwin van der Oetelaar | 6c720c4 | 2013-01-17 11:16:15 +0800 | [diff] [blame] | 276 | |
Edwin van den Oetelaar | 596b220 | 2013-01-20 20:51:14 +0800 | [diff] [blame] | 277 | if (count_pollfds >= max_poll_elements) { |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 278 | lwsl_err("LWS_CALLBACK_ADD_POLL_FD: too many sockets to track\n"); |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 279 | return 1; |
Andy Green | 5fc460c | 2013-01-15 19:44:33 +0800 | [diff] [blame] | 280 | } |
Andy Green | 5fc460c | 2013-01-15 19:44:33 +0800 | [diff] [blame] | 281 | |
Edwin van der Oetelaar | 6c720c4 | 2013-01-17 11:16:15 +0800 | [diff] [blame] | 282 | fd_lookup[fd] = count_pollfds; |
| 283 | pollfds[count_pollfds].fd = fd; |
| 284 | pollfds[count_pollfds].events = (int)(long)len; |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 285 | pollfds[count_pollfds++].revents = 0; |
| 286 | break; |
| 287 | |
| 288 | case LWS_CALLBACK_DEL_POLL_FD: |
Edwin van der Oetelaar | 6c720c4 | 2013-01-17 11:16:15 +0800 | [diff] [blame] | 289 | if (!--count_pollfds) |
| 290 | break; |
| 291 | m = fd_lookup[fd]; |
| 292 | /* have the last guy take up the vacant slot */ |
| 293 | pollfds[m] = pollfds[count_pollfds]; |
| 294 | fd_lookup[pollfds[count_pollfds].fd] = m; |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 295 | break; |
| 296 | |
| 297 | case LWS_CALLBACK_SET_MODE_POLL_FD: |
Edwin van der Oetelaar | 6c720c4 | 2013-01-17 11:16:15 +0800 | [diff] [blame] | 298 | pollfds[fd_lookup[fd]].events |= (int)(long)len; |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 299 | break; |
| 300 | |
| 301 | case LWS_CALLBACK_CLEAR_MODE_POLL_FD: |
Edwin van der Oetelaar | 6c720c4 | 2013-01-17 11:16:15 +0800 | [diff] [blame] | 302 | pollfds[fd_lookup[fd]].events &= ~(int)(long)len; |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 303 | break; |
| 304 | #endif |
Edwin van der Oetelaar | 6c720c4 | 2013-01-17 11:16:15 +0800 | [diff] [blame] | 305 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 306 | default: |
| 307 | break; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 308 | } |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 309 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 310 | return 0; |
| 311 | } |
| 312 | |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 313 | /* |
| 314 | * this is just an example of parsing handshake headers, you don't need this |
| 315 | * in your code unless you will filter allowing connections by the header |
| 316 | * content |
| 317 | */ |
| 318 | |
| 319 | static void |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 320 | dump_handshake_info(struct libwebsocket *wsi) |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 321 | { |
| 322 | int n; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 323 | static const char *token_names[WSI_TOKEN_COUNT] = { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 324 | /*[WSI_TOKEN_GET_URI] =*/ "GET URI", |
| 325 | /*[WSI_TOKEN_HOST] =*/ "Host", |
| 326 | /*[WSI_TOKEN_CONNECTION] =*/ "Connection", |
| 327 | /*[WSI_TOKEN_KEY1] =*/ "key 1", |
| 328 | /*[WSI_TOKEN_KEY2] =*/ "key 2", |
| 329 | /*[WSI_TOKEN_PROTOCOL] =*/ "Protocol", |
| 330 | /*[WSI_TOKEN_UPGRADE] =*/ "Upgrade", |
| 331 | /*[WSI_TOKEN_ORIGIN] =*/ "Origin", |
| 332 | /*[WSI_TOKEN_DRAFT] =*/ "Draft", |
| 333 | /*[WSI_TOKEN_CHALLENGE] =*/ "Challenge", |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 334 | |
| 335 | /* new for 04 */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 336 | /*[WSI_TOKEN_KEY] =*/ "Key", |
| 337 | /*[WSI_TOKEN_VERSION] =*/ "Version", |
| 338 | /*[WSI_TOKEN_SWORIGIN] =*/ "Sworigin", |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 339 | |
| 340 | /* new for 05 */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 341 | /*[WSI_TOKEN_EXTENSIONS] =*/ "Extensions", |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 342 | |
| 343 | /* client receives these */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 344 | /*[WSI_TOKEN_ACCEPT] =*/ "Accept", |
| 345 | /*[WSI_TOKEN_NONCE] =*/ "Nonce", |
| 346 | /*[WSI_TOKEN_HTTP] =*/ "Http", |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 347 | /*[WSI_TOKEN_MUXURL] =*/ "MuxURL", |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 348 | }; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 349 | char buf[256]; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 350 | |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 351 | for (n = 0; n < WSI_TOKEN_COUNT; n++) { |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 352 | if (!lws_hdr_total_length(wsi, n)) |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 353 | continue; |
| 354 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 355 | lws_hdr_copy(wsi, buf, sizeof buf, n); |
| 356 | |
| 357 | fprintf(stderr, " %s = %s\n", token_names[n], buf); |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 361 | /* dumb_increment protocol */ |
| 362 | |
Andy Green | 0ca6a17 | 2010-12-19 20:50:01 +0000 | [diff] [blame] | 363 | /* |
| 364 | * one of these is auto-created for each connection and a pointer to the |
| 365 | * appropriate instance is passed to the callback in the user parameter |
| 366 | * |
| 367 | * for this example protocol we use it to individualize the count for each |
| 368 | * connection. |
| 369 | */ |
| 370 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 371 | struct per_session_data__dumb_increment { |
| 372 | int number; |
| 373 | }; |
| 374 | |
| 375 | static int |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 376 | callback_dumb_increment(struct libwebsocket_context *context, |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 377 | struct libwebsocket *wsi, |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 378 | enum libwebsocket_callback_reasons reason, |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 379 | void *user, void *in, size_t len) |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 380 | { |
Andy Green | 3390efd | 2013-03-23 09:46:18 +0800 | [diff] [blame^] | 381 | int n, m; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 382 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 + |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 383 | LWS_SEND_BUFFER_POST_PADDING]; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 384 | unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING]; |
Andy Green | 36eb70d | 2013-02-01 08:42:15 +0800 | [diff] [blame] | 385 | struct per_session_data__dumb_increment *pss = (struct per_session_data__dumb_increment *)user; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 386 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 387 | switch (reason) { |
| 388 | |
| 389 | case LWS_CALLBACK_ESTABLISHED: |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 390 | lwsl_info("callback_dumb_increment: " |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 391 | "LWS_CALLBACK_ESTABLISHED\n"); |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 392 | pss->number = 0; |
| 393 | break; |
| 394 | |
Andy Green | 6f520a5 | 2013-01-29 17:57:39 +0800 | [diff] [blame] | 395 | case LWS_CALLBACK_SERVER_WRITEABLE: |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 396 | n = sprintf((char *)p, "%d", pss->number++); |
Andy Green | 3390efd | 2013-03-23 09:46:18 +0800 | [diff] [blame^] | 397 | m = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT); |
| 398 | if (m < n) { |
Andy Green | 51b20ee | 2013-02-17 09:14:08 +0800 | [diff] [blame] | 399 | lwsl_err("ERROR %d writing to di socket\n", n); |
| 400 | return -1; |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 401 | } |
Andy Green | 69758fa | 2011-03-07 07:08:07 +0000 | [diff] [blame] | 402 | if (close_testing && pss->number == 50) { |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 403 | lwsl_info("close tesing limit, closing\n"); |
Andy Green | 508946c | 2013-02-12 10:19:08 +0800 | [diff] [blame] | 404 | return -1; |
Andy Green | 69758fa | 2011-03-07 07:08:07 +0000 | [diff] [blame] | 405 | } |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 406 | break; |
| 407 | |
| 408 | case LWS_CALLBACK_RECEIVE: |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 409 | // fprintf(stderr, "rx %d\n", (int)len); |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 410 | if (len < 6) |
| 411 | break; |
Andy Green | 36eb70d | 2013-02-01 08:42:15 +0800 | [diff] [blame] | 412 | if (strcmp((const char *)in, "reset\n") == 0) |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 413 | pss->number = 0; |
| 414 | break; |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 415 | /* |
| 416 | * this just demonstrates how to use the protocol filter. If you won't |
| 417 | * study and reject connections based on header content, you don't need |
| 418 | * to handle this callback |
| 419 | */ |
| 420 | |
| 421 | case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 422 | dump_handshake_info(wsi); |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 423 | /* you could return non-zero here and kill the connection */ |
| 424 | break; |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 425 | |
| 426 | default: |
| 427 | break; |
| 428 | } |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 434 | /* lws-mirror_protocol */ |
| 435 | |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 436 | #define MAX_MESSAGE_QUEUE 32 |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 437 | |
| 438 | struct per_session_data__lws_mirror { |
| 439 | struct libwebsocket *wsi; |
| 440 | int ringbuffer_tail; |
| 441 | }; |
| 442 | |
| 443 | struct a_message { |
| 444 | void *payload; |
| 445 | size_t len; |
| 446 | }; |
| 447 | |
| 448 | static struct a_message ringbuffer[MAX_MESSAGE_QUEUE]; |
| 449 | static int ringbuffer_head; |
| 450 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 451 | static int |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 452 | callback_lws_mirror(struct libwebsocket_context *context, |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 453 | struct libwebsocket *wsi, |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 454 | enum libwebsocket_callback_reasons reason, |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 455 | void *user, void *in, size_t len) |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 456 | { |
| 457 | int n; |
Andy Green | 36eb70d | 2013-02-01 08:42:15 +0800 | [diff] [blame] | 458 | struct per_session_data__lws_mirror *pss = (struct per_session_data__lws_mirror *)user; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 459 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 460 | switch (reason) { |
| 461 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 462 | case LWS_CALLBACK_ESTABLISHED: |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 463 | lwsl_info("callback_lws_mirror: LWS_CALLBACK_ESTABLISHED\n"); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 464 | pss->ringbuffer_tail = ringbuffer_head; |
| 465 | pss->wsi = wsi; |
| 466 | break; |
| 467 | |
Andy Green | a7109e6 | 2013-02-11 12:05:54 +0800 | [diff] [blame] | 468 | case LWS_CALLBACK_PROTOCOL_DESTROY: |
| 469 | lwsl_notice("mirror protocol cleaning up\n"); |
| 470 | for (n = 0; n < sizeof ringbuffer / sizeof ringbuffer[0]; n++) |
| 471 | if (ringbuffer[n].payload) |
| 472 | free(ringbuffer[n].payload); |
| 473 | break; |
| 474 | |
Andy Green | 9e4c2b6 | 2011-03-07 20:47:39 +0000 | [diff] [blame] | 475 | case LWS_CALLBACK_SERVER_WRITEABLE: |
Andy Green | 69758fa | 2011-03-07 07:08:07 +0000 | [diff] [blame] | 476 | if (close_testing) |
| 477 | break; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 478 | while (pss->ringbuffer_tail != ringbuffer_head) { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 479 | |
| 480 | n = libwebsocket_write(wsi, (unsigned char *) |
| 481 | ringbuffer[pss->ringbuffer_tail].payload + |
| 482 | LWS_SEND_BUFFER_PRE_PADDING, |
| 483 | ringbuffer[pss->ringbuffer_tail].len, |
| 484 | LWS_WRITE_TEXT); |
Andy Green | 3390efd | 2013-03-23 09:46:18 +0800 | [diff] [blame^] | 485 | if (n < ringbuffer[pss->ringbuffer_tail].len) { |
Andy Green | 51b20ee | 2013-02-17 09:14:08 +0800 | [diff] [blame] | 486 | lwsl_err("ERROR %d writing to mirror socket\n", n); |
| 487 | return -1; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 488 | } |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 489 | if (n < ringbuffer[pss->ringbuffer_tail].len) |
| 490 | lwsl_err("mirror partial write %d vs %d\n", |
| 491 | n, ringbuffer[pss->ringbuffer_tail].len); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 492 | |
| 493 | if (pss->ringbuffer_tail == (MAX_MESSAGE_QUEUE - 1)) |
| 494 | pss->ringbuffer_tail = 0; |
| 495 | else |
| 496 | pss->ringbuffer_tail++; |
| 497 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 498 | if (((ringbuffer_head - pss->ringbuffer_tail) & |
Andy Green | b55451c | 2013-03-16 12:32:27 +0800 | [diff] [blame] | 499 | (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 15)) |
| 500 | libwebsocket_rx_flow_allow_all_protocol( |
| 501 | libwebsockets_get_protocol(wsi)); |
| 502 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 503 | // lwsl_debug("tx fifo %d\n", (ringbuffer_head - pss->ringbuffer_tail) & (MAX_MESSAGE_QUEUE - 1)); |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 504 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 505 | if (lws_send_pipe_choked(wsi)) { |
| 506 | libwebsocket_callback_on_writable(context, wsi); |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 507 | break; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 508 | } |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 509 | /* |
| 510 | * for tests with chrome on same machine as client and |
| 511 | * server, this is needed to stop chrome choking |
| 512 | */ |
| 513 | usleep(1); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 514 | } |
| 515 | break; |
| 516 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 517 | case LWS_CALLBACK_RECEIVE: |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 518 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 519 | if (((ringbuffer_head - pss->ringbuffer_tail) & |
| 520 | (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 1)) { |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 521 | lwsl_err("dropping!\n"); |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 522 | goto choke; |
| 523 | } |
| 524 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 525 | if (ringbuffer[ringbuffer_head].payload) |
| 526 | free(ringbuffer[ringbuffer_head].payload); |
| 527 | |
| 528 | ringbuffer[ringbuffer_head].payload = |
| 529 | malloc(LWS_SEND_BUFFER_PRE_PADDING + len + |
| 530 | LWS_SEND_BUFFER_POST_PADDING); |
| 531 | ringbuffer[ringbuffer_head].len = len; |
| 532 | memcpy((char *)ringbuffer[ringbuffer_head].payload + |
| 533 | LWS_SEND_BUFFER_PRE_PADDING, in, len); |
| 534 | if (ringbuffer_head == (MAX_MESSAGE_QUEUE - 1)) |
| 535 | ringbuffer_head = 0; |
| 536 | else |
| 537 | ringbuffer_head++; |
| 538 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 539 | if (((ringbuffer_head - pss->ringbuffer_tail) & |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 540 | (MAX_MESSAGE_QUEUE - 1)) != (MAX_MESSAGE_QUEUE - 2)) |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 541 | goto done; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 542 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 543 | choke: |
Andy Green | b55451c | 2013-03-16 12:32:27 +0800 | [diff] [blame] | 544 | lwsl_debug("LWS_CALLBACK_RECEIVE: throttling %p\n", wsi); |
| 545 | libwebsocket_rx_flow_control(wsi, 0); |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 546 | |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 547 | // lwsl_debug("rx fifo %d\n", (ringbuffer_head - pss->ringbuffer_tail) & (MAX_MESSAGE_QUEUE - 1)); |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 548 | done: |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 549 | libwebsocket_callback_on_writable_all_protocol( |
| 550 | libwebsockets_get_protocol(wsi)); |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 551 | break; |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 552 | |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 553 | /* |
| 554 | * this just demonstrates how to use the protocol filter. If you won't |
| 555 | * study and reject connections based on header content, you don't need |
| 556 | * to handle this callback |
| 557 | */ |
| 558 | |
| 559 | case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 560 | dump_handshake_info(wsi); |
Andy Green | 38c4f0c | 2011-02-13 08:54:05 +0000 | [diff] [blame] | 561 | /* you could return non-zero here and kill the connection */ |
| 562 | break; |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 563 | |
| 564 | default: |
| 565 | break; |
| 566 | } |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 572 | /* list of supported protocols and callbacks */ |
| 573 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 574 | static struct libwebsocket_protocols protocols[] = { |
Andy Green | 0ca6a17 | 2010-12-19 20:50:01 +0000 | [diff] [blame] | 575 | /* first protocol must always be HTTP handler */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 576 | |
| 577 | { |
| 578 | "http-only", /* name */ |
| 579 | callback_http, /* callback */ |
Andy Green | fbf4822 | 2013-02-14 23:06:37 +0800 | [diff] [blame] | 580 | sizeof (struct per_session_data__http), /* per_session_data_size */ |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 581 | 0, /* max frame size / rx buffer */ |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 582 | }, |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 583 | { |
| 584 | "dumb-increment-protocol", |
| 585 | callback_dumb_increment, |
| 586 | sizeof(struct per_session_data__dumb_increment), |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 587 | 10, |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 588 | }, |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 589 | { |
| 590 | "lws-mirror-protocol", |
| 591 | callback_lws_mirror, |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 592 | sizeof(struct per_session_data__lws_mirror), |
Andy Green | e7c97e8 | 2013-02-09 14:07:32 +0800 | [diff] [blame] | 593 | 128, |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame] | 594 | }, |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 595 | { NULL, NULL, 0, 0 } /* terminator */ |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 596 | }; |
| 597 | |
Andy Green | 08f2c01 | 2013-01-30 08:02:26 +0800 | [diff] [blame] | 598 | void sighandler(int sig) |
| 599 | { |
| 600 | force_exit = 1; |
| 601 | } |
| 602 | |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 603 | static struct option options[] = { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 604 | { "help", no_argument, NULL, 'h' }, |
Andy Green | 46ef0cf | 2013-01-10 22:28:59 +0800 | [diff] [blame] | 605 | { "debug", required_argument, NULL, 'd' }, |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 606 | { "port", required_argument, NULL, 'p' }, |
| 607 | { "ssl", no_argument, NULL, 's' }, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 608 | { "interface", required_argument, NULL, 'i' }, |
Andy Green | 69758fa | 2011-03-07 07:08:07 +0000 | [diff] [blame] | 609 | { "closetest", no_argument, NULL, 'c' }, |
Andy Green | 5c54d62 | 2013-01-21 12:58:04 +0800 | [diff] [blame] | 610 | #ifndef LWS_NO_DAEMONIZE |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 611 | { "daemonize", no_argument, NULL, 'D' }, |
| 612 | #endif |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 613 | { "resource_path", required_argument, NULL, 'r' }, |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 614 | { NULL, 0, 0, 0 } |
| 615 | }; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 616 | |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 617 | int main(int argc, char **argv) |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 618 | { |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 619 | char cert_path[1024]; |
| 620 | char key_path[1024]; |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 621 | int n = 0; |
Andy Green | 0ca6a17 | 2010-12-19 20:50:01 +0000 | [diff] [blame] | 622 | int use_ssl = 0; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 623 | struct libwebsocket_context *context; |
Andy Green | 8014b29 | 2011-01-30 20:57:25 +0000 | [diff] [blame] | 624 | int opts = 0; |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 625 | char interface_name[128] = ""; |
Joakim Soderberg | 63ff120 | 2013-02-11 17:52:23 +0100 | [diff] [blame] | 626 | const char *iface = NULL; |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 627 | #ifndef WIN32 |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 628 | int syslog_options = LOG_PID | LOG_PERROR; |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 629 | #endif |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 630 | unsigned int oldus = 0; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 631 | struct lws_context_creation_info info; |
Andy Green | 6f520a5 | 2013-01-29 17:57:39 +0800 | [diff] [blame] | 632 | |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 633 | int debug_level = 7; |
Andy Green | 5c54d62 | 2013-01-21 12:58:04 +0800 | [diff] [blame] | 634 | #ifndef LWS_NO_DAEMONIZE |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 635 | int daemonize = 0; |
| 636 | #endif |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 637 | |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 638 | memset(&info, 0, sizeof info); |
| 639 | info.port = 7681; |
| 640 | |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 641 | while (n >= 0) { |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 642 | n = getopt_long(argc, argv, "ci:hsp:d:Dr:", options, NULL); |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 643 | if (n < 0) |
| 644 | continue; |
| 645 | switch (n) { |
Andy Green | 5c54d62 | 2013-01-21 12:58:04 +0800 | [diff] [blame] | 646 | #ifndef LWS_NO_DAEMONIZE |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 647 | case 'D': |
| 648 | daemonize = 1; |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 649 | #ifndef WIN32 |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 650 | syslog_options &= ~LOG_PERROR; |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 651 | #endif |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 652 | break; |
| 653 | #endif |
Andy Green | 46ef0cf | 2013-01-10 22:28:59 +0800 | [diff] [blame] | 654 | case 'd': |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 655 | debug_level = atoi(optarg); |
Andy Green | 46ef0cf | 2013-01-10 22:28:59 +0800 | [diff] [blame] | 656 | break; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 657 | case 's': |
| 658 | use_ssl = 1; |
| 659 | break; |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 660 | case 'p': |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 661 | info.port = atoi(optarg); |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 662 | break; |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 663 | case 'i': |
| 664 | strncpy(interface_name, optarg, sizeof interface_name); |
| 665 | interface_name[(sizeof interface_name) - 1] = '\0'; |
Joakim Soderberg | 63ff120 | 2013-02-11 17:52:23 +0100 | [diff] [blame] | 666 | iface = interface_name; |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 667 | break; |
Andy Green | 69758fa | 2011-03-07 07:08:07 +0000 | [diff] [blame] | 668 | case 'c': |
| 669 | close_testing = 1; |
| 670 | fprintf(stderr, " Close testing mode -- closes on " |
| 671 | "client after 50 dumb increments" |
| 672 | "and suppresses lws_mirror spam\n"); |
| 673 | break; |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 674 | case 'r': |
| 675 | resource_path = optarg; |
| 676 | printf("Setting resource path to \"%s\"\n", resource_path); |
| 677 | break; |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 678 | case 'h': |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 679 | fprintf(stderr, "Usage: test-server " |
Andy Green | 46ef0cf | 2013-01-10 22:28:59 +0800 | [diff] [blame] | 680 | "[--port=<p>] [--ssl] " |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 681 | "[-d <log bitfield>] " |
| 682 | "[--resource_path <path>]\n"); |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 683 | exit(1); |
| 684 | } |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 685 | } |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 686 | |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 687 | #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32) |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 688 | /* |
| 689 | * normally lock path would be /var/lock/lwsts or similar, to |
| 690 | * simplify getting started without having to take care about |
| 691 | * permissions or running as root, set to /tmp/.lwsts-lock |
| 692 | */ |
| 693 | if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) { |
| 694 | fprintf(stderr, "Failed to daemonize\n"); |
| 695 | return 1; |
| 696 | } |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 697 | #endif |
Andy Green | 08f2c01 | 2013-01-30 08:02:26 +0800 | [diff] [blame] | 698 | |
| 699 | signal(SIGINT, sighandler); |
| 700 | |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 701 | #ifndef WIN32 |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 702 | /* we will only try to log things according to our debug_level */ |
| 703 | setlogmask(LOG_UPTO (LOG_DEBUG)); |
| 704 | openlog("lwsts", syslog_options, LOG_DAEMON); |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 705 | #endif |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 706 | |
| 707 | /* tell the library what debug level to emit and to send it to syslog */ |
| 708 | lws_set_log_level(debug_level, lwsl_emit_syslog); |
| 709 | |
Andy Green | fd6764a | 2013-01-19 11:11:42 +0800 | [diff] [blame] | 710 | lwsl_notice("libwebsockets test server - " |
| 711 | "(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> - " |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 712 | "licensed under LGPL2.1\n"); |
Edwin van den Oetelaar | 596b220 | 2013-01-20 20:51:14 +0800 | [diff] [blame] | 713 | #ifdef EXTERNAL_POLL |
| 714 | max_poll_elements = getdtablesize(); |
| 715 | pollfds = malloc(max_poll_elements * sizeof (struct pollfd)); |
| 716 | fd_lookup = malloc(max_poll_elements * sizeof (int)); |
| 717 | if (pollfds == NULL || fd_lookup == NULL) { |
| 718 | lwsl_err("Out of memory pollfds=%d\n", max_poll_elements); |
| 719 | return -1; |
| 720 | } |
| 721 | #endif |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 722 | |
Joakim Soderberg | 63ff120 | 2013-02-11 17:52:23 +0100 | [diff] [blame] | 723 | info.iface = iface; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 724 | info.protocols = protocols; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 725 | #ifndef LWS_NO_EXTENSIONS |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 726 | info.extensions = libwebsocket_get_internal_extensions(); |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 727 | #endif |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 728 | if (!use_ssl) { |
| 729 | info.ssl_cert_filepath = NULL; |
| 730 | info.ssl_private_key_filepath = NULL; |
| 731 | } else { |
Joakim Soderberg | f0ee669 | 2013-03-18 17:08:25 +0000 | [diff] [blame] | 732 | snprintf(cert_path, sizeof(cert_path), "%s/libwebsockets-test-server.pem", resource_path); |
| 733 | snprintf(key_path, sizeof(cert_path), "%s/libwebsockets-test-server.key.pem", resource_path); |
| 734 | |
| 735 | info.ssl_cert_filepath = cert_path; |
| 736 | info.ssl_private_key_filepath = key_path; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 737 | } |
| 738 | info.gid = -1; |
| 739 | info.uid = -1; |
| 740 | info.options = opts; |
| 741 | |
| 742 | context = libwebsocket_create_context(&info); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 743 | if (context == NULL) { |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 744 | lwsl_err("libwebsocket init failed\n"); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 745 | return -1; |
| 746 | } |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 747 | |
Andy Green | 3928f61 | 2012-07-20 12:58:38 +0800 | [diff] [blame] | 748 | n = 0; |
Andy Green | 08f2c01 | 2013-01-30 08:02:26 +0800 | [diff] [blame] | 749 | while (n >= 0 && !force_exit) { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 750 | struct timeval tv; |
| 751 | |
| 752 | gettimeofday(&tv, NULL); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 753 | |
| 754 | /* |
Andy Green | 6f520a5 | 2013-01-29 17:57:39 +0800 | [diff] [blame] | 755 | * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every |
| 756 | * live websocket connection using the DUMB_INCREMENT protocol, |
| 757 | * as soon as it can take more packets (usually immediately) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 758 | */ |
| 759 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 760 | if (((unsigned int)tv.tv_usec - oldus) > 50000) { |
Andy Green | 6f520a5 | 2013-01-29 17:57:39 +0800 | [diff] [blame] | 761 | libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_DUMB_INCREMENT]); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 762 | oldus = tv.tv_usec; |
| 763 | } |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 764 | |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 765 | #ifdef EXTERNAL_POLL |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 766 | |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 767 | /* |
| 768 | * this represents an existing server's single poll action |
| 769 | * which also includes libwebsocket sockets |
| 770 | */ |
| 771 | |
| 772 | n = poll(pollfds, count_pollfds, 50); |
| 773 | if (n < 0) |
| 774 | continue; |
| 775 | |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 776 | |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 777 | if (n) |
| 778 | for (n = 0; n < count_pollfds; n++) |
| 779 | if (pollfds[n].revents) |
| 780 | /* |
| 781 | * returns immediately if the fd does not |
| 782 | * match anything under libwebsockets |
| 783 | * control |
| 784 | */ |
| 785 | if (libwebsocket_service_fd(context, |
| 786 | &pollfds[n]) < 0) |
| 787 | goto done; |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 788 | #else |
Andy Green | 0d49c8d | 2013-02-02 23:02:56 +0800 | [diff] [blame] | 789 | /* |
| 790 | * If libwebsockets sockets are all we care about, |
| 791 | * you can use this api which takes care of the poll() |
| 792 | * and looping through finding who needed service. |
| 793 | * |
| 794 | * If no socket needs service, it'll return anyway after |
| 795 | * the number of ms in the second argument. |
| 796 | */ |
| 797 | |
Andy Green | 3928f61 | 2012-07-20 12:58:38 +0800 | [diff] [blame] | 798 | n = libwebsocket_service(context, 50); |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 799 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Andy Green | a50dd1a | 2013-01-15 12:39:48 +0800 | [diff] [blame] | 802 | #ifdef EXTERNAL_POLL |
| 803 | done: |
| 804 | #endif |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 805 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 806 | libwebsocket_context_destroy(context); |
| 807 | |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 808 | lwsl_notice("libwebsockets-test-server exited cleanly\n"); |
| 809 | |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 810 | #ifndef WIN32 |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 811 | closelog(); |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 812 | #endif |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 813 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 814 | return 0; |
| 815 | } |