Andy Green | 58eaa74 | 2011-03-07 17:54:06 +0000 | [diff] [blame] | 1 | /* |
Andy Green | a0da8a8 | 2010-11-08 17:12:19 +0000 | [diff] [blame] | 2 | * libwebsockets - small server side websockets and web server implementation |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 3 | * |
Andy Green | a0da8a8 | 2010-11-08 17:12:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2010 Andy Green <andy@warmcat.com> |
| 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 |
Andy Green | 05a0a7b | 2010-10-31 17:51:39 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 22 | #include "private-libwebsockets.h" |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 23 | |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 24 | #if defined(WIN32) || defined(_WIN32) |
David Galeano | cb19368 | 2013-01-09 15:29:00 +0800 | [diff] [blame] | 25 | #include <tchar.h> |
| 26 | #include <io.h> |
Joakim Soderberg | 63ff120 | 2013-02-11 17:52:23 +0100 | [diff] [blame] | 27 | #include <mstcpip.h> |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 28 | #else |
David | c4ef7b1 | 2013-01-12 20:39:47 +0800 | [diff] [blame] | 29 | #ifdef LWS_BUILTIN_GETIFADDRS |
| 30 | #include <getifaddrs.h> |
| 31 | #else |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 32 | #include <ifaddrs.h> |
David | c4ef7b1 | 2013-01-12 20:39:47 +0800 | [diff] [blame] | 33 | #endif |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 34 | #include <syslog.h> |
Andy Green | 7627af5 | 2011-03-09 15:13:52 +0000 | [diff] [blame] | 35 | #include <sys/un.h> |
Andy Green | a69f051 | 2012-05-03 12:32:38 +0800 | [diff] [blame] | 36 | #include <sys/socket.h> |
| 37 | #include <netdb.h> |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 38 | #endif |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 39 | |
| 40 | #ifdef LWS_OPENSSL_SUPPORT |
| 41 | int openssl_websocket_private_data_index; |
| 42 | #endif |
| 43 | |
Andy Green | aa6fc44 | 2012-04-12 13:26:49 +0800 | [diff] [blame] | 44 | #ifdef __MINGW32__ |
| 45 | #include "../win32port/win32helpers/websock-w32.c" |
| 46 | #else |
| 47 | #ifdef __MINGW64__ |
| 48 | #include "../win32port/win32helpers/websock-w32.c" |
| 49 | #endif |
| 50 | #endif |
| 51 | |
Andy Green | 7b40545 | 2013-02-01 10:50:15 +0800 | [diff] [blame] | 52 | #ifndef LWS_BUILD_HASH |
| 53 | #define LWS_BUILD_HASH "unknown-build-hash" |
| 54 | #endif |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 55 | |
Andy Green | 7c19c34 | 2013-01-19 12:18:07 +0800 | [diff] [blame] | 56 | static int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE; |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 57 | static void lwsl_emit_stderr(int level, const char *line); |
| 58 | static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr; |
| 59 | |
Andy Green | 7b40545 | 2013-02-01 10:50:15 +0800 | [diff] [blame] | 60 | static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH; |
| 61 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 62 | static const char * const log_level_names[] = { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 63 | "ERR", |
| 64 | "WARN", |
Andy Green | 7c19c34 | 2013-01-19 12:18:07 +0800 | [diff] [blame] | 65 | "NOTICE", |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 66 | "INFO", |
| 67 | "DEBUG", |
| 68 | "PARSER", |
| 69 | "HEADER", |
| 70 | "EXTENSION", |
| 71 | "CLIENT", |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 72 | "LATENCY", |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 73 | }; |
| 74 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 75 | #ifndef LWS_NO_CLIENT |
| 76 | extern int lws_client_socket_service( |
| 77 | struct libwebsocket_context *context, |
| 78 | struct libwebsocket *wsi, struct pollfd *pollfd); |
| 79 | #endif |
| 80 | #ifndef LWS_NO_SERVER |
| 81 | extern int lws_server_socket_service( |
| 82 | struct libwebsocket_context *context, |
| 83 | struct libwebsocket *wsi, struct pollfd *pollfd); |
| 84 | #endif |
| 85 | |
Andy Green | 7b40545 | 2013-02-01 10:50:15 +0800 | [diff] [blame] | 86 | /** |
| 87 | * lws_get_library_version: get version and git hash library built from |
| 88 | * |
| 89 | * returns a const char * to a string like "1.1 178d78c" |
| 90 | * representing the library version followed by the git head hash it |
| 91 | * was built from |
| 92 | */ |
| 93 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 94 | LWS_VISIBLE const char * |
Andy Green | 7b40545 | 2013-02-01 10:50:15 +0800 | [diff] [blame] | 95 | lws_get_library_version(void) |
| 96 | { |
| 97 | return library_version; |
| 98 | } |
| 99 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 100 | int |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 101 | insert_wsi_socket_into_fds(struct libwebsocket_context *context, |
| 102 | struct libwebsocket *wsi) |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 103 | { |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 104 | if (context->fds_count >= context->max_fds) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 105 | lwsl_err("Too many fds (%d)\n", context->max_fds); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 106 | return 1; |
| 107 | } |
| 108 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 109 | if (wsi->sock > context->max_fds) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 110 | lwsl_err("Socket fd %d is too high (%d)\n", |
| 111 | wsi->sock, context->max_fds); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 112 | return 1; |
| 113 | } |
| 114 | |
| 115 | assert(wsi); |
Andy Green | 512c246 | 2013-09-18 12:59:33 +0800 | [diff] [blame] | 116 | assert(wsi->sock >= 0); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 117 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 118 | lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n", |
| 119 | wsi, wsi->sock, context->fds_count); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 120 | |
| 121 | context->lws_lookup[wsi->sock] = wsi; |
| 122 | wsi->position_in_fds_table = context->fds_count; |
| 123 | context->fds[context->fds_count].fd = wsi->sock; |
| 124 | context->fds[context->fds_count].events = POLLIN; |
| 125 | context->fds[context->fds_count++].revents = 0; |
| 126 | |
| 127 | /* external POLL support via protocol 0 */ |
| 128 | context->protocols[0].callback(context, wsi, |
| 129 | LWS_CALLBACK_ADD_POLL_FD, |
Andy Green | 50097dd | 2013-02-15 22:36:30 +0800 | [diff] [blame] | 130 | wsi->user_space, (void *)(long)wsi->sock, POLLIN); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 135 | static int |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 136 | remove_wsi_socket_from_fds(struct libwebsocket_context *context, |
| 137 | struct libwebsocket *wsi) |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 138 | { |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 139 | int m; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 140 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 141 | if (!--context->fds_count) |
| 142 | goto do_ext; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 143 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 144 | if (wsi->sock > context->max_fds) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 145 | lwsl_err("Socket fd %d too high (%d)\n", |
| 146 | wsi->sock, context->max_fds); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 147 | return 1; |
| 148 | } |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 149 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 150 | lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n", |
| 151 | wsi, wsi->sock, wsi->position_in_fds_table); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 152 | |
| 153 | m = wsi->position_in_fds_table; /* replace the contents for this */ |
| 154 | |
| 155 | /* have the last guy take up the vacant slot */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 156 | context->fds[m] = context->fds[context->fds_count]; |
| 157 | /* |
| 158 | * end guy's fds_lookup entry remains unchanged |
| 159 | * (still same fd pointing to same wsi) |
| 160 | */ |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 161 | /* end guy's "position in fds table" changed */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 162 | context->lws_lookup[context->fds[context->fds_count].fd]-> |
| 163 | position_in_fds_table = m; |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 164 | /* deletion guy's lws_lookup entry needs nuking */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 165 | context->lws_lookup[wsi->sock] = NULL; |
| 166 | /* removed wsi has no position any more */ |
| 167 | wsi->position_in_fds_table = -1; |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 168 | |
| 169 | do_ext: |
| 170 | /* remove also from external POLL support via protocol 0 */ |
| 171 | if (wsi->sock) |
| 172 | context->protocols[0].callback(context, wsi, |
Andy Green | 50097dd | 2013-02-15 22:36:30 +0800 | [diff] [blame] | 173 | LWS_CALLBACK_DEL_POLL_FD, wsi->user_space, |
| 174 | (void *)(long)wsi->sock, 0); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 175 | |
| 176 | return 0; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 179 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 180 | void |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 181 | libwebsocket_close_and_free_session(struct libwebsocket_context *context, |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 182 | struct libwebsocket *wsi, enum lws_close_status reason) |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 183 | { |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 184 | int n; |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 185 | int old_state; |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 186 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 + |
| 187 | LWS_SEND_BUFFER_POST_PADDING]; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 188 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 189 | int ret; |
| 190 | int m; |
| 191 | struct lws_tokens eff_buf; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 192 | struct libwebsocket_extension *ext; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 193 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 194 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 195 | if (!wsi) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 196 | return; |
| 197 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 198 | old_state = wsi->state; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 199 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 200 | if (old_state == WSI_STATE_DEAD_SOCKET) |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 201 | return; |
| 202 | |
Andy Green | 22524a6 | 2013-02-15 22:48:58 +0800 | [diff] [blame] | 203 | /* we tried the polite way... */ |
| 204 | if (old_state == WSI_STATE_AWAITING_CLOSE_ACK) |
| 205 | goto just_kill_connection; |
| 206 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 207 | wsi->u.ws.close_reason = reason; |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 208 | |
Andy Green | 5dc62ea | 2013-09-20 20:26:12 +0800 | [diff] [blame] | 209 | if (wsi->mode == LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT || |
| 210 | wsi->mode == LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE) { |
| 211 | |
| 212 | context->protocols[0].callback(context, wsi, |
| 213 | LWS_CALLBACK_CLIENT_CONNECTION_ERROR, NULL, NULL, 0); |
| 214 | |
| 215 | free(wsi->u.hdr.ah); |
| 216 | goto just_kill_connection; |
| 217 | } |
| 218 | |
| 219 | |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 220 | if (wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED) { |
| 221 | if (wsi->u.http.post_buffer) { |
| 222 | free(wsi->u.http.post_buffer); |
| 223 | wsi->u.http.post_buffer = NULL; |
| 224 | } |
| 225 | if (wsi->u.http.fd >= 0) { |
| 226 | lwsl_debug("closing http fd %d\n", wsi->u.http.fd); |
| 227 | close(wsi->u.http.fd); |
| 228 | wsi->u.http.fd = -1; |
| 229 | context->protocols[0].callback(context, wsi, |
| 230 | LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0); |
| 231 | } |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 232 | } |
| 233 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 234 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 235 | /* |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 236 | * are his extensions okay with him closing? Eg he might be a mux |
| 237 | * parent and just his ch1 aspect is closing? |
| 238 | */ |
| 239 | |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 240 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 241 | if (!wsi->active_extensions[n]->callback) |
| 242 | continue; |
| 243 | |
| 244 | m = wsi->active_extensions[n]->callback(context, |
| 245 | wsi->active_extensions[n], wsi, |
| 246 | LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE, |
| 247 | wsi->active_extensions_user[n], NULL, 0); |
| 248 | |
| 249 | /* |
| 250 | * if somebody vetoed actually closing him at this time.... |
| 251 | * up to the extension to track the attempted close, let's |
| 252 | * just bail |
| 253 | */ |
| 254 | |
| 255 | if (m) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 256 | lwsl_ext("extension vetoed close\n"); |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 257 | return; |
| 258 | } |
| 259 | } |
| 260 | |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 261 | /* |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 262 | * flush any tx pending from extensions, since we may send close packet |
| 263 | * if there are problems with send, just nuke the connection |
| 264 | */ |
| 265 | |
| 266 | ret = 1; |
| 267 | while (ret == 1) { |
| 268 | |
| 269 | /* default to nobody has more to spill */ |
| 270 | |
| 271 | ret = 0; |
| 272 | eff_buf.token = NULL; |
| 273 | eff_buf.token_len = 0; |
| 274 | |
| 275 | /* show every extension the new incoming data */ |
| 276 | |
| 277 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 278 | m = wsi->active_extensions[n]->callback( |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 279 | wsi->protocol->owning_server, |
| 280 | wsi->active_extensions[n], wsi, |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 281 | LWS_EXT_CALLBACK_FLUSH_PENDING_TX, |
| 282 | wsi->active_extensions_user[n], &eff_buf, 0); |
| 283 | if (m < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 284 | lwsl_ext("Extension reports fatal error\n"); |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 285 | goto just_kill_connection; |
| 286 | } |
| 287 | if (m) |
| 288 | /* |
| 289 | * at least one extension told us he has more |
| 290 | * to spill, so we will go around again after |
| 291 | */ |
| 292 | ret = 1; |
| 293 | } |
| 294 | |
| 295 | /* assuming they left us something to send, send it */ |
| 296 | |
| 297 | if (eff_buf.token_len) |
| 298 | if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token, |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 299 | eff_buf.token_len) != eff_buf.token_len) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 300 | lwsl_debug("close: ext spill failed\n"); |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 301 | goto just_kill_connection; |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 302 | } |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 303 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 304 | #endif |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 305 | |
| 306 | /* |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 307 | * signal we are closing, libsocket_write will |
| 308 | * add any necessary version-specific stuff. If the write fails, |
| 309 | * no worries we are closing anyway. If we didn't initiate this |
| 310 | * close, then our state has been changed to |
| 311 | * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this. |
| 312 | * |
| 313 | * Likewise if it's a second call to close this connection after we |
| 314 | * sent the close indication to the peer already, we are in state |
| 315 | * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time. |
| 316 | */ |
| 317 | |
| 318 | if (old_state == WSI_STATE_ESTABLISHED && |
| 319 | reason != LWS_CLOSE_STATUS_NOSTATUS) { |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 320 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 321 | lwsl_debug("sending close indication...\n"); |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 322 | |
Andy Green | fdd305a | 2013-02-11 14:32:48 +0800 | [diff] [blame] | 323 | /* make valgrind happy */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 324 | memset(buf, 0, sizeof(buf)); |
| 325 | n = libwebsocket_write(wsi, |
| 326 | &buf[LWS_SEND_BUFFER_PRE_PADDING + 2], |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 327 | 0, LWS_WRITE_CLOSE); |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 328 | if (n >= 0) { |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 329 | /* |
| 330 | * we have sent a nice protocol level indication we |
| 331 | * now wish to close, we should not send anything more |
| 332 | */ |
| 333 | |
| 334 | wsi->state = WSI_STATE_AWAITING_CLOSE_ACK; |
| 335 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 336 | /* |
| 337 | * ...and we should wait for a reply for a bit |
| 338 | * out of politeness |
| 339 | */ |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 340 | |
| 341 | libwebsocket_set_timeout(wsi, |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 342 | PENDING_TIMEOUT_CLOSE_ACK, 1); |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 343 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 344 | lwsl_debug("sent close indication, awaiting ack\n"); |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 345 | |
| 346 | return; |
| 347 | } |
| 348 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 349 | lwsl_info("close: sending close packet failed, hanging up\n"); |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 350 | |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 351 | /* else, the send failed and we should just hang up */ |
| 352 | } |
| 353 | |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 354 | just_kill_connection: |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 355 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 356 | lwsl_debug("close: just_kill_connection\n"); |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 357 | |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 358 | /* |
| 359 | * we won't be servicing or receiving anything further from this guy |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 360 | * delete socket from the internal poll list if still present |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 361 | */ |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 362 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 363 | remove_wsi_socket_from_fds(context, wsi); |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 364 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 365 | wsi->state = WSI_STATE_DEAD_SOCKET; |
| 366 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 367 | if ((old_state == WSI_STATE_ESTABLISHED || |
| 368 | wsi->mode == LWS_CONNMODE_WS_SERVING || |
Andy Green | 3ee9b31 | 2013-02-12 12:52:39 +0800 | [diff] [blame] | 369 | wsi->mode == LWS_CONNMODE_WS_CLIENT)) { |
| 370 | |
| 371 | if (wsi->u.ws.rx_user_buffer) { |
| 372 | free(wsi->u.ws.rx_user_buffer); |
| 373 | wsi->u.ws.rx_user_buffer = NULL; |
| 374 | } |
| 375 | if (wsi->u.ws.rxflow_buffer) { |
| 376 | free(wsi->u.ws.rxflow_buffer); |
| 377 | wsi->u.ws.rxflow_buffer = NULL; |
| 378 | } |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame^] | 379 | if (wsi->truncated_send_malloc) { |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 380 | /* not going to be completed... nuke it */ |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame^] | 381 | free(wsi->truncated_send_malloc); |
| 382 | wsi->truncated_send_malloc = NULL; |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 383 | } |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 384 | } |
| 385 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 386 | /* tell the user it's all over for this guy */ |
| 387 | |
Andy Green | d430273 | 2011-02-28 07:45:29 +0000 | [diff] [blame] | 388 | if (wsi->protocol && wsi->protocol->callback && |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 389 | ((old_state == WSI_STATE_ESTABLISHED) || |
| 390 | (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) || |
| 391 | (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 392 | lwsl_debug("calling back CLOSED\n"); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 393 | wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED, |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 394 | wsi->user_space, NULL, 0); |
Niall T. Davidson | db761be | 2013-06-29 10:16:18 +0800 | [diff] [blame] | 395 | } else if ( wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED ) { |
| 396 | lwsl_debug("calling back CLOSED_HTTP\n"); |
| 397 | context->protocols[0].callback(context, wsi, |
| 398 | LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0 ); |
Andy Green | cc01247 | 2011-11-07 19:53:23 +0800 | [diff] [blame] | 399 | } else |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 400 | lwsl_debug("not calling back closed\n"); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 401 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 402 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | ef660a9 | 2011-03-06 10:29:38 +0000 | [diff] [blame] | 403 | /* deallocate any active extension contexts */ |
| 404 | |
| 405 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 406 | if (!wsi->active_extensions[n]->callback) |
| 407 | continue; |
| 408 | |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 409 | wsi->active_extensions[n]->callback(context, |
| 410 | wsi->active_extensions[n], wsi, |
| 411 | LWS_EXT_CALLBACK_DESTROY, |
| 412 | wsi->active_extensions_user[n], NULL, 0); |
Andy Green | ef660a9 | 2011-03-06 10:29:38 +0000 | [diff] [blame] | 413 | |
| 414 | free(wsi->active_extensions_user[n]); |
| 415 | } |
| 416 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 417 | /* |
| 418 | * inform all extensions in case they tracked this guy out of band |
| 419 | * even though not active on him specifically |
| 420 | */ |
| 421 | |
| 422 | ext = context->extensions; |
| 423 | while (ext && ext->callback) { |
| 424 | ext->callback(context, ext, wsi, |
| 425 | LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING, |
| 426 | NULL, NULL, 0); |
| 427 | ext++; |
| 428 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 429 | #endif |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 430 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 431 | /* lwsl_info("closing fd=%d\n", wsi->sock); */ |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 432 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 433 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 434 | if (wsi->ssl) { |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 435 | n = SSL_get_fd(wsi->ssl); |
| 436 | SSL_shutdown(wsi->ssl); |
Andy Green | 3fc2c65 | 2013-01-14 15:35:02 +0800 | [diff] [blame] | 437 | compatible_close(n); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 438 | SSL_free(wsi->ssl); |
| 439 | } else { |
| 440 | #endif |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 441 | if (wsi->sock) { |
| 442 | n = shutdown(wsi->sock, SHUT_RDWR); |
| 443 | if (n) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 444 | lwsl_debug("closing: shutdown returned %d\n", |
| 445 | errno); |
Andy Green | 3fc2c65 | 2013-01-14 15:35:02 +0800 | [diff] [blame] | 446 | |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 447 | n = compatible_close(wsi->sock); |
| 448 | if (n) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 449 | lwsl_debug("closing: close returned %d\n", |
| 450 | errno); |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 451 | } |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 452 | #ifdef LWS_OPENSSL_SUPPORT |
| 453 | } |
| 454 | #endif |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 455 | if (wsi->protocol && wsi->protocol->per_session_data_size && |
| 456 | wsi->user_space) /* user code may own */ |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 457 | free(wsi->user_space); |
| 458 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 459 | free(wsi); |
| 460 | } |
| 461 | |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 462 | /** |
| 463 | * libwebsockets_get_peer_addresses() - Get client address information |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 464 | * @context: Libwebsockets context |
| 465 | * @wsi: Local struct libwebsocket associated with |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 466 | * @fd: Connection socket descriptor |
| 467 | * @name: Buffer to take client address name |
| 468 | * @name_len: Length of client address name buffer |
| 469 | * @rip: Buffer to take client address IP qotted quad |
| 470 | * @rip_len: Length of client address IP buffer |
| 471 | * |
| 472 | * This function fills in @name and @rip with the name and IP of |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 473 | * the client connected with socket descriptor @fd. Names may be |
| 474 | * truncated if there is not enough room. If either cannot be |
| 475 | * determined, they will be returned as valid zero-length strings. |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 476 | */ |
| 477 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 478 | LWS_VISIBLE void |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 479 | libwebsockets_get_peer_addresses(struct libwebsocket_context *context, |
| 480 | struct libwebsocket *wsi, int fd, char *name, int name_len, |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 481 | char *rip, int rip_len) |
| 482 | { |
Bob Roberts | ac04911 | 2013-04-25 09:16:30 +0800 | [diff] [blame] | 483 | socklen_t len; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 484 | struct sockaddr_in sin; |
| 485 | struct hostent *host; |
| 486 | struct hostent *host1; |
| 487 | char ip[128]; |
Andy Green | f92def7 | 2011-03-09 15:02:20 +0000 | [diff] [blame] | 488 | unsigned char *p; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 489 | int n; |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 490 | int ret = -1; |
David Galeano | cb19368 | 2013-01-09 15:29:00 +0800 | [diff] [blame] | 491 | #ifdef AF_LOCAL |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 492 | struct sockaddr_un *un; |
David Galeano | cb19368 | 2013-01-09 15:29:00 +0800 | [diff] [blame] | 493 | #endif |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 494 | |
| 495 | rip[0] = '\0'; |
| 496 | name[0] = '\0'; |
| 497 | |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 498 | lws_latency_pre(context, wsi); |
| 499 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 500 | len = sizeof(sin); |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 501 | if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) { |
| 502 | perror("getpeername"); |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 503 | goto bail; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 504 | } |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 505 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 506 | host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr), |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 507 | AF_INET); |
| 508 | if (host == NULL) { |
| 509 | perror("gethostbyaddr"); |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 510 | goto bail; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | strncpy(name, host->h_name, name_len); |
| 514 | name[name_len - 1] = '\0'; |
| 515 | |
| 516 | host1 = gethostbyname(host->h_name); |
| 517 | if (host1 == NULL) |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 518 | goto bail; |
Andy Green | f92def7 | 2011-03-09 15:02:20 +0000 | [diff] [blame] | 519 | p = (unsigned char *)host1; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 520 | n = 0; |
| 521 | while (p != NULL) { |
Andy Green | f92def7 | 2011-03-09 15:02:20 +0000 | [diff] [blame] | 522 | p = (unsigned char *)host1->h_addr_list[n++]; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 523 | if (p == NULL) |
| 524 | continue; |
Peter Hinz | bb45a90 | 2011-03-10 18:14:01 +0000 | [diff] [blame] | 525 | if ((host1->h_addrtype != AF_INET) |
| 526 | #ifdef AF_LOCAL |
| 527 | && (host1->h_addrtype != AF_LOCAL) |
| 528 | #endif |
| 529 | ) |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 530 | continue; |
| 531 | |
Andy Green | 7627af5 | 2011-03-09 15:13:52 +0000 | [diff] [blame] | 532 | if (host1->h_addrtype == AF_INET) |
| 533 | sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]); |
Peter Hinz | bb45a90 | 2011-03-10 18:14:01 +0000 | [diff] [blame] | 534 | #ifdef AF_LOCAL |
Andy Green | 7627af5 | 2011-03-09 15:13:52 +0000 | [diff] [blame] | 535 | else { |
| 536 | un = (struct sockaddr_un *)p; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 537 | strncpy(ip, un->sun_path, sizeof(ip) - 1); |
Andy Green | 7627af5 | 2011-03-09 15:13:52 +0000 | [diff] [blame] | 538 | ip[sizeof(ip) - 1] = '\0'; |
| 539 | } |
Peter Hinz | bb45a90 | 2011-03-10 18:14:01 +0000 | [diff] [blame] | 540 | #endif |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 541 | p = NULL; |
| 542 | strncpy(rip, ip, rip_len); |
| 543 | rip[rip_len - 1] = '\0'; |
| 544 | } |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 545 | |
| 546 | ret = 0; |
| 547 | bail: |
| 548 | lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1); |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 549 | } |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 550 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 551 | LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context, |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 552 | void *buf, int len) |
| 553 | { |
| 554 | int n; |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 555 | char *p = (char *)buf; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 556 | |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 557 | #if defined(WIN32) || defined(_WIN32) |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 558 | for (n = 0; n < len; n++) |
| 559 | p[n] = (unsigned char)rand(); |
| 560 | #else |
| 561 | n = read(context->fd_random, p, len); |
| 562 | #endif |
| 563 | |
| 564 | return n; |
| 565 | } |
| 566 | |
Andy Green | a690cd0 | 2013-02-09 12:25:31 +0800 | [diff] [blame] | 567 | int lws_set_socket_options(struct libwebsocket_context *context, int fd) |
| 568 | { |
| 569 | int optval = 1; |
| 570 | socklen_t optlen = sizeof(optval); |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 571 | #if defined(WIN32) || defined(_WIN32) |
Andy Green | a690cd0 | 2013-02-09 12:25:31 +0800 | [diff] [blame] | 572 | unsigned long optl = 0; |
| 573 | #endif |
| 574 | #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) |
| 575 | struct protoent *tcp_proto; |
| 576 | #endif |
| 577 | |
| 578 | if (context->ka_time) { |
| 579 | /* enable keepalive on this socket */ |
| 580 | optval = 1; |
| 581 | if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, |
| 582 | (const void *)&optval, optlen) < 0) |
| 583 | return 1; |
| 584 | |
Bob Roberts | ac04911 | 2013-04-25 09:16:30 +0800 | [diff] [blame] | 585 | #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__) |
Andy Green | a47865f | 2013-02-10 09:39:47 +0800 | [diff] [blame] | 586 | |
| 587 | /* |
| 588 | * didn't find a way to set these per-socket, need to |
| 589 | * tune kernel systemwide values |
| 590 | */ |
Joakim Soderberg | 63ff120 | 2013-02-11 17:52:23 +0100 | [diff] [blame] | 591 | #elif WIN32 |
| 592 | { |
| 593 | DWORD dwBytesRet; |
| 594 | struct tcp_keepalive alive; |
| 595 | alive.onoff = TRUE; |
| 596 | alive.keepalivetime = context->ka_time; |
| 597 | alive.keepaliveinterval = context->ka_interval; |
Andy Green | a47865f | 2013-02-10 09:39:47 +0800 | [diff] [blame] | 598 | |
Joakim Soderberg | 63ff120 | 2013-02-11 17:52:23 +0100 | [diff] [blame] | 599 | if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive), |
| 600 | NULL, 0, &dwBytesRet, NULL, NULL)) |
| 601 | return 1; |
| 602 | } |
Andy Green | a47865f | 2013-02-10 09:39:47 +0800 | [diff] [blame] | 603 | #else |
Andy Green | a690cd0 | 2013-02-09 12:25:31 +0800 | [diff] [blame] | 604 | /* set the keepalive conditions we want on it too */ |
| 605 | optval = context->ka_time; |
| 606 | if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE, |
| 607 | (const void *)&optval, optlen) < 0) |
| 608 | return 1; |
| 609 | |
Larry Hayes | bb66ac6 | 2013-02-22 09:16:20 +0800 | [diff] [blame] | 610 | optval = context->ka_interval; |
Andy Green | a690cd0 | 2013-02-09 12:25:31 +0800 | [diff] [blame] | 611 | if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL, |
| 612 | (const void *)&optval, optlen) < 0) |
| 613 | return 1; |
| 614 | |
Larry Hayes | bb66ac6 | 2013-02-22 09:16:20 +0800 | [diff] [blame] | 615 | optval = context->ka_probes; |
Andy Green | a690cd0 | 2013-02-09 12:25:31 +0800 | [diff] [blame] | 616 | if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT, |
| 617 | (const void *)&optval, optlen) < 0) |
| 618 | return 1; |
Andy Green | a47865f | 2013-02-10 09:39:47 +0800 | [diff] [blame] | 619 | #endif |
Andy Green | a690cd0 | 2013-02-09 12:25:31 +0800 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /* Disable Nagle */ |
| 623 | optval = 1; |
| 624 | #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__) |
| 625 | setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen); |
| 626 | #else |
| 627 | tcp_proto = getprotobyname("TCP"); |
| 628 | setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen); |
| 629 | #endif |
| 630 | |
| 631 | /* We are nonblocking... */ |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 632 | #if defined(WIN32) || defined(_WIN32) |
Andy Green | a690cd0 | 2013-02-09 12:25:31 +0800 | [diff] [blame] | 633 | ioctlsocket(fd, FIONBIO, &optl); |
| 634 | #else |
| 635 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 636 | #endif |
| 637 | |
| 638 | return 0; |
| 639 | } |
| 640 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 641 | LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi) |
Andy Green | 95a7b5d | 2011-03-06 10:29:39 +0000 | [diff] [blame] | 642 | { |
| 643 | struct pollfd fds; |
| 644 | |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame^] | 645 | /* treat the fact we got a truncated send pending as if we're choked */ |
| 646 | if (wsi->truncated_send_malloc) |
| 647 | return 1; |
| 648 | |
Andy Green | 95a7b5d | 2011-03-06 10:29:39 +0000 | [diff] [blame] | 649 | fds.fd = wsi->sock; |
| 650 | fds.events = POLLOUT; |
| 651 | fds.revents = 0; |
| 652 | |
| 653 | if (poll(&fds, 1, 0) != 1) |
| 654 | return 1; |
| 655 | |
| 656 | if ((fds.revents & POLLOUT) == 0) |
| 657 | return 1; |
| 658 | |
| 659 | /* okay to send another packet without blocking */ |
| 660 | |
| 661 | return 0; |
| 662 | } |
| 663 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 664 | int |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 665 | lws_handle_POLLOUT_event(struct libwebsocket_context *context, |
| 666 | struct libwebsocket *wsi, struct pollfd *pollfd) |
| 667 | { |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 668 | int n; |
Andy Green | 6f520a5 | 2013-01-29 17:57:39 +0800 | [diff] [blame] | 669 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 670 | #ifndef LWS_NO_EXTENSIONS |
| 671 | struct lws_tokens eff_buf; |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 672 | int ret; |
| 673 | int m; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 674 | int handled = 0; |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 675 | |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 676 | /* pending truncated sends have uber priority */ |
| 677 | |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame^] | 678 | if (wsi->truncated_send_malloc) { |
| 679 | lws_issue_raw(wsi, wsi->truncated_send_malloc + |
| 680 | wsi->truncated_send_offset, |
| 681 | wsi->truncated_send_len); |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 682 | /* leave POLLOUT active either way */ |
| 683 | return 0; |
| 684 | } |
| 685 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 686 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 687 | if (!wsi->active_extensions[n]->callback) |
| 688 | continue; |
| 689 | |
| 690 | m = wsi->active_extensions[n]->callback(context, |
| 691 | wsi->active_extensions[n], wsi, |
| 692 | LWS_EXT_CALLBACK_IS_WRITEABLE, |
| 693 | wsi->active_extensions_user[n], NULL, 0); |
| 694 | if (m > handled) |
| 695 | handled = m; |
| 696 | } |
| 697 | |
| 698 | if (handled == 1) |
| 699 | goto notify_action; |
| 700 | |
| 701 | if (!wsi->extension_data_pending || handled == 2) |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 702 | goto user_service; |
| 703 | |
| 704 | /* |
| 705 | * check in on the active extensions, see if they |
| 706 | * had pending stuff to spill... they need to get the |
| 707 | * first look-in otherwise sequence will be disordered |
| 708 | * |
| 709 | * NULL, zero-length eff_buf means just spill pending |
| 710 | */ |
| 711 | |
| 712 | ret = 1; |
| 713 | while (ret == 1) { |
| 714 | |
| 715 | /* default to nobody has more to spill */ |
| 716 | |
| 717 | ret = 0; |
| 718 | eff_buf.token = NULL; |
| 719 | eff_buf.token_len = 0; |
| 720 | |
| 721 | /* give every extension a chance to spill */ |
| 722 | |
| 723 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 724 | m = wsi->active_extensions[n]->callback( |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 725 | wsi->protocol->owning_server, |
| 726 | wsi->active_extensions[n], wsi, |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 727 | LWS_EXT_CALLBACK_PACKET_TX_PRESEND, |
| 728 | wsi->active_extensions_user[n], &eff_buf, 0); |
| 729 | if (m < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 730 | lwsl_err("ext reports fatal error\n"); |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 731 | return -1; |
| 732 | } |
| 733 | if (m) |
| 734 | /* |
| 735 | * at least one extension told us he has more |
| 736 | * to spill, so we will go around again after |
| 737 | */ |
| 738 | ret = 1; |
| 739 | } |
| 740 | |
| 741 | /* assuming they gave us something to send, send it */ |
| 742 | |
| 743 | if (eff_buf.token_len) { |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 744 | n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token, |
| 745 | eff_buf.token_len); |
| 746 | if (n < 0) |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 747 | return -1; |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 748 | /* |
| 749 | * Keep amount spilled small to minimize chance of this |
| 750 | */ |
| 751 | if (n != eff_buf.token_len) { |
| 752 | lwsl_err("Unable to spill ext %d vs %s\n", |
| 753 | eff_buf.token_len, n); |
| 754 | return -1; |
| 755 | } |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 756 | } else |
| 757 | continue; |
| 758 | |
| 759 | /* no extension has more to spill */ |
| 760 | |
| 761 | if (!ret) |
| 762 | continue; |
| 763 | |
| 764 | /* |
| 765 | * There's more to spill from an extension, but we just sent |
| 766 | * something... did that leave the pipe choked? |
| 767 | */ |
| 768 | |
| 769 | if (!lws_send_pipe_choked(wsi)) |
| 770 | /* no we could add more */ |
| 771 | continue; |
| 772 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 773 | lwsl_info("choked in POLLOUT service\n"); |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 774 | |
| 775 | /* |
| 776 | * Yes, he's choked. Leave the POLLOUT masked on so we will |
| 777 | * come back here when he is unchoked. Don't call the user |
| 778 | * callback to enforce ordering of spilling, he'll get called |
| 779 | * when we come back here and there's nothing more to spill. |
| 780 | */ |
| 781 | |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | wsi->extension_data_pending = 0; |
| 786 | |
| 787 | user_service: |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 788 | #endif |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 789 | /* one shot */ |
| 790 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 791 | if (pollfd) { |
| 792 | pollfd->events &= ~POLLOUT; |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 793 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 794 | /* external POLL support via protocol 0 */ |
| 795 | context->protocols[0].callback(context, wsi, |
| 796 | LWS_CALLBACK_CLEAR_MODE_POLL_FD, |
Andy Green | 50097dd | 2013-02-15 22:36:30 +0800 | [diff] [blame] | 797 | wsi->user_space, (void *)(long)wsi->sock, POLLOUT); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 798 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 799 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 800 | notify_action: |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 801 | #endif |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 802 | |
Andy Green | 9e4c2b6 | 2011-03-07 20:47:39 +0000 | [diff] [blame] | 803 | if (wsi->mode == LWS_CONNMODE_WS_CLIENT) |
| 804 | n = LWS_CALLBACK_CLIENT_WRITEABLE; |
| 805 | else |
| 806 | n = LWS_CALLBACK_SERVER_WRITEABLE; |
| 807 | |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 808 | return user_callback_handle_rxflow(wsi->protocol->callback, context, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 809 | wsi, (enum libwebsocket_callback_reasons) n, |
| 810 | wsi->user_space, NULL, 0); |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | |
| 814 | |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 815 | int |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 816 | libwebsocket_service_timeout_check(struct libwebsocket_context *context, |
| 817 | struct libwebsocket *wsi, unsigned int sec) |
| 818 | { |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 819 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 820 | int n; |
| 821 | |
| 822 | /* |
| 823 | * if extensions want in on it (eg, we are a mux parent) |
| 824 | * give them a chance to service child timeouts |
| 825 | */ |
| 826 | |
| 827 | for (n = 0; n < wsi->count_active_extensions; n++) |
| 828 | wsi->active_extensions[n]->callback( |
| 829 | context, wsi->active_extensions[n], |
| 830 | wsi, LWS_EXT_CALLBACK_1HZ, |
| 831 | wsi->active_extensions_user[n], NULL, sec); |
| 832 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 833 | #endif |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 834 | if (!wsi->pending_timeout) |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 835 | return 0; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 836 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 837 | /* |
| 838 | * if we went beyond the allowed time, kill the |
| 839 | * connection |
| 840 | */ |
| 841 | |
| 842 | if (sec > wsi->pending_timeout_limit) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 843 | lwsl_info("TIMEDOUT WAITING\n"); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 844 | libwebsocket_close_and_free_session(context, |
| 845 | wsi, LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 846 | return 1; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 847 | } |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 848 | |
| 849 | return 0; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 850 | } |
| 851 | |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 852 | /** |
| 853 | * libwebsocket_service_fd() - Service polled socket with something waiting |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 854 | * @context: Websocket context |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 855 | * @pollfd: The pollfd entry describing the socket fd and which events |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 856 | * happened. |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 857 | * |
Andy Green | 7500617 | 2013-01-22 12:32:11 +0800 | [diff] [blame] | 858 | * This function takes a pollfd that has POLLIN or POLLOUT activity and |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 859 | * services it according to the state of the associated |
| 860 | * struct libwebsocket. |
Andy Green | 7500617 | 2013-01-22 12:32:11 +0800 | [diff] [blame] | 861 | * |
| 862 | * The one call deals with all "service" that might happen on a socket |
| 863 | * including listen accepts, http files as well as websocket protocol. |
Andy Green | 2577c83 | 2013-03-11 20:37:03 +0800 | [diff] [blame] | 864 | * |
| 865 | * If a pollfd says it has something, you can just pass it to |
| 866 | * libwebsocket_serice_fd() whether it is a socket handled by lws or not. |
| 867 | * If it sees it is a lws socket, the traffic will be handled and |
| 868 | * pollfd->revents will be zeroed now. |
| 869 | * |
| 870 | * If the socket is foreign to lws, it leaves revents alone. So you can |
| 871 | * see if you should service yourself by checking the pollfd revents |
| 872 | * after letting lws try to service it. |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 873 | */ |
| 874 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 875 | LWS_VISIBLE int |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 876 | libwebsocket_service_fd(struct libwebsocket_context *context, |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 877 | struct pollfd *pollfd) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 878 | { |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 879 | struct libwebsocket *wsi; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 880 | int n; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 881 | int m; |
Andy Green | f0b79e2 | 2013-02-10 10:46:45 +0800 | [diff] [blame] | 882 | int listen_socket_fds_index = 0; |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 883 | struct timeval tv; |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 884 | int timed_out = 0; |
| 885 | int our_fd = 0; |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 886 | char draining_flow = 0; |
Andy Green | 6f520a5 | 2013-01-29 17:57:39 +0800 | [diff] [blame] | 887 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 888 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 2366b1c | 2011-03-06 13:15:31 +0000 | [diff] [blame] | 889 | int more = 1; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 890 | #endif |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 891 | struct lws_tokens eff_buf; |
Andy Green | f0b79e2 | 2013-02-10 10:46:45 +0800 | [diff] [blame] | 892 | |
| 893 | if (context->listen_service_fd) |
| 894 | listen_socket_fds_index = context->lws_lookup[ |
| 895 | context->listen_service_fd]->position_in_fds_table; |
| 896 | |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 897 | /* |
| 898 | * you can call us with pollfd = NULL to just allow the once-per-second |
| 899 | * global timeout checks; if less than a second since the last check |
| 900 | * it returns immediately then. |
| 901 | */ |
| 902 | |
| 903 | gettimeofday(&tv, NULL); |
| 904 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 905 | if (context->last_timeout_check_s != tv.tv_sec) { |
| 906 | context->last_timeout_check_s = tv.tv_sec; |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 907 | |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 908 | #ifndef WIN32 |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 909 | /* if our parent went down, don't linger around */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 910 | if (context->started_with_parent && |
| 911 | kill(context->started_with_parent, 0) < 0) |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 912 | kill(getpid(), SIGTERM); |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 913 | #endif |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 914 | |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 915 | /* global timeout check once per second */ |
| 916 | |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 917 | if (pollfd) |
| 918 | our_fd = pollfd->fd; |
| 919 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 920 | for (n = 0; n < context->fds_count; n++) { |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 921 | m = context->fds[n].fd; |
| 922 | wsi = context->lws_lookup[m]; |
| 923 | if (!wsi) |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 924 | continue; |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 925 | |
| 926 | if (libwebsocket_service_timeout_check(context, wsi, |
| 927 | tv.tv_sec)) |
| 928 | /* he did time out... */ |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 929 | if (m == our_fd) { |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 930 | /* it was the guy we came to service! */ |
| 931 | timed_out = 1; |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 932 | /* mark as handled */ |
| 933 | pollfd->revents = 0; |
| 934 | } |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 938 | /* the socket we came to service timed out, nothing to do */ |
| 939 | if (timed_out) |
| 940 | return 0; |
| 941 | |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 942 | /* just here for timeout management? */ |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 943 | if (pollfd == NULL) |
| 944 | return 0; |
| 945 | |
| 946 | /* no, here to service a socket descriptor */ |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 947 | wsi = context->lws_lookup[pollfd->fd]; |
| 948 | if (wsi == NULL) |
| 949 | /* not lws connection ... leave revents alone and return */ |
| 950 | return 0; |
| 951 | |
| 952 | /* |
| 953 | * so that caller can tell we handled, past here we need to |
| 954 | * zero down pollfd->revents after handling |
| 955 | */ |
| 956 | |
Andy Green | 65b0e91 | 2013-01-16 07:59:47 +0800 | [diff] [blame] | 957 | /* |
| 958 | * deal with listen service piggybacking |
| 959 | * every listen_service_modulo services of other fds, we |
| 960 | * sneak one in to service the listen socket if there's anything waiting |
| 961 | * |
| 962 | * To handle connection storms, as found in ab, if we previously saw a |
| 963 | * pending connection here, it causes us to check again next time. |
| 964 | */ |
| 965 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 966 | if (context->listen_service_fd && pollfd != |
| 967 | &context->fds[listen_socket_fds_index]) { |
Andy Green | 65b0e91 | 2013-01-16 07:59:47 +0800 | [diff] [blame] | 968 | context->listen_service_count++; |
| 969 | if (context->listen_service_extraseen || |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 970 | context->listen_service_count == |
| 971 | context->listen_service_modulo) { |
Andy Green | 65b0e91 | 2013-01-16 07:59:47 +0800 | [diff] [blame] | 972 | context->listen_service_count = 0; |
| 973 | m = 1; |
| 974 | if (context->listen_service_extraseen > 5) |
| 975 | m = 2; |
| 976 | while (m--) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 977 | /* |
| 978 | * even with extpoll, we prepared this |
| 979 | * internal fds for listen |
| 980 | */ |
| 981 | n = poll(&context->fds[listen_socket_fds_index], |
| 982 | 1, 0); |
| 983 | if (n > 0) { /* there's a conn waiting for us */ |
| 984 | libwebsocket_service_fd(context, |
| 985 | &context-> |
| 986 | fds[listen_socket_fds_index]); |
Andy Green | 65b0e91 | 2013-01-16 07:59:47 +0800 | [diff] [blame] | 987 | context->listen_service_extraseen++; |
| 988 | } else { |
| 989 | if (context->listen_service_extraseen) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 990 | context-> |
| 991 | listen_service_extraseen--; |
Andy Green | 65b0e91 | 2013-01-16 07:59:47 +0800 | [diff] [blame] | 992 | break; |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | } |
| 998 | |
Graham Newton | b1aa1d3 | 2013-11-04 09:47:06 +0800 | [diff] [blame] | 999 | /* handle session socket closed */ |
| 1000 | |
| 1001 | if ((!(pollfd->revents & POLLIN)) && |
| 1002 | (pollfd->revents & (POLLERR | POLLHUP))) { |
| 1003 | |
| 1004 | lwsl_debug("Session Socket %p (fd=%d) dead\n", |
| 1005 | (void *)wsi, pollfd->fd); |
| 1006 | |
| 1007 | goto close_and_handled; |
| 1008 | } |
| 1009 | |
Andy Green | 65b0e91 | 2013-01-16 07:59:47 +0800 | [diff] [blame] | 1010 | /* okay, what we came here to do... */ |
| 1011 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1012 | switch (wsi->mode) { |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 1013 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1014 | #ifndef LWS_NO_SERVER |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 1015 | case LWS_CONNMODE_HTTP_SERVING: |
Andy Green | 7cf6cb0 | 2013-05-19 14:04:10 +0800 | [diff] [blame] | 1016 | case LWS_CONNMODE_HTTP_SERVING_ACCEPTED: |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1017 | case LWS_CONNMODE_SERVER_LISTENER: |
Andy Green | e216071 | 2013-01-28 12:19:10 +0800 | [diff] [blame] | 1018 | case LWS_CONNMODE_SSL_ACK_PENDING: |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 1019 | n = lws_server_socket_service(context, wsi, pollfd); |
| 1020 | goto handled; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1021 | #endif |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1022 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1023 | case LWS_CONNMODE_WS_SERVING: |
| 1024 | case LWS_CONNMODE_WS_CLIENT: |
| 1025 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1026 | /* the guy requested a callback when it was OK to write */ |
| 1027 | |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 1028 | if ((pollfd->revents & POLLOUT) && |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1029 | wsi->state == WSI_STATE_ESTABLISHED && |
| 1030 | lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) { |
Andy Green | 0878b9e | 2013-02-13 11:44:20 +0800 | [diff] [blame] | 1031 | lwsl_info("libwebsocket_service_fd: closing\n"); |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 1032 | goto close_and_handled; |
Graham Newton | b1aa1d3 | 2013-11-04 09:47:06 +0800 | [diff] [blame] | 1033 | } |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1034 | |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1035 | if (wsi->u.ws.rxflow_buffer && |
| 1036 | (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) { |
| 1037 | lwsl_info("draining rxflow\n"); |
| 1038 | /* well, drain it */ |
| 1039 | eff_buf.token = (char *)wsi->u.ws.rxflow_buffer + |
| 1040 | wsi->u.ws.rxflow_pos; |
| 1041 | eff_buf.token_len = wsi->u.ws.rxflow_len - |
| 1042 | wsi->u.ws.rxflow_pos; |
| 1043 | draining_flow = 1; |
| 1044 | goto drain; |
| 1045 | } |
| 1046 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1047 | /* any incoming data ready? */ |
| 1048 | |
| 1049 | if (!(pollfd->revents & POLLIN)) |
| 1050 | break; |
| 1051 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1052 | #ifdef LWS_OPENSSL_SUPPORT |
David Galeano | 7ffbe1b | 2013-01-10 10:35:32 +0800 | [diff] [blame] | 1053 | read_pending: |
Andy Green | 467c7ef | 2013-01-30 12:28:34 +0800 | [diff] [blame] | 1054 | if (wsi->ssl) { |
Andy Green | e84652c | 2013-02-08 13:01:02 +0800 | [diff] [blame] | 1055 | eff_buf.token_len = SSL_read(wsi->ssl, |
| 1056 | context->service_buffer, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1057 | sizeof(context->service_buffer)); |
Andy Green | 467c7ef | 2013-01-30 12:28:34 +0800 | [diff] [blame] | 1058 | if (!eff_buf.token_len) { |
| 1059 | n = SSL_get_error(wsi->ssl, eff_buf.token_len); |
Andy Green | e84652c | 2013-02-08 13:01:02 +0800 | [diff] [blame] | 1060 | lwsl_err("SSL_read returned 0 with reason %s\n", |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1061 | ERR_error_string(n, |
| 1062 | (char *)context->service_buffer)); |
Andy Green | 467c7ef | 2013-01-30 12:28:34 +0800 | [diff] [blame] | 1063 | } |
| 1064 | } else |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1065 | #endif |
Andy Green | e84652c | 2013-02-08 13:01:02 +0800 | [diff] [blame] | 1066 | eff_buf.token_len = recv(pollfd->fd, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1067 | context->service_buffer, |
| 1068 | sizeof(context->service_buffer), 0); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1069 | |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1070 | if (eff_buf.token_len < 0) { |
Andy Green | 98ba9e0 | 2013-03-23 09:44:47 +0800 | [diff] [blame] | 1071 | lwsl_debug("service_fd read ret = %d, errno = %d\n", |
| 1072 | eff_buf.token_len, errno); |
Alon Levy | dc93b7f | 2012-10-19 11:21:57 +0200 | [diff] [blame] | 1073 | if (errno != EINTR && errno != EAGAIN) |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 1074 | goto close_and_handled; |
| 1075 | n = 0; |
| 1076 | goto handled; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1077 | } |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1078 | if (!eff_buf.token_len) { |
Andy Green | 98ba9e0 | 2013-03-23 09:44:47 +0800 | [diff] [blame] | 1079 | lwsl_info("service_fd: closing due to 0 length read\n"); |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 1080 | goto close_and_handled; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1083 | /* |
| 1084 | * give any active extensions a chance to munge the buffer |
| 1085 | * before parse. We pass in a pointer to an lws_tokens struct |
| 1086 | * prepared with the default buffer and content length that's in |
| 1087 | * there. Rather than rewrite the default buffer, extensions |
| 1088 | * that expect to grow the buffer can adapt .token to |
| 1089 | * point to their own per-connection buffer in the extension |
| 1090 | * user allocation. By default with no extensions or no |
| 1091 | * extension callback handling, just the normal input buffer is |
| 1092 | * used then so it is efficient. |
| 1093 | */ |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1094 | |
Andy Green | e84652c | 2013-02-08 13:01:02 +0800 | [diff] [blame] | 1095 | eff_buf.token = (char *)context->service_buffer; |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1096 | drain: |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1097 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1098 | more = 1; |
| 1099 | while (more) { |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1100 | |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1101 | more = 0; |
| 1102 | |
| 1103 | for (n = 0; n < wsi->count_active_extensions; n++) { |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 1104 | m = wsi->active_extensions[n]->callback(context, |
| 1105 | wsi->active_extensions[n], wsi, |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1106 | LWS_EXT_CALLBACK_PACKET_RX_PREPARSE, |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 1107 | wsi->active_extensions_user[n], |
| 1108 | &eff_buf, 0); |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1109 | if (m < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1110 | lwsl_ext( |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1111 | "Extension reports fatal error\n"); |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 1112 | goto close_and_handled; |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1113 | } |
| 1114 | if (m) |
| 1115 | more = 1; |
| 1116 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1117 | #endif |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1118 | /* service incoming data */ |
| 1119 | |
| 1120 | if (eff_buf.token_len) { |
| 1121 | n = libwebsocket_read(context, wsi, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1122 | (unsigned char *)eff_buf.token, |
| 1123 | eff_buf.token_len); |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 1124 | if (n < 0) { |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1125 | /* we closed wsi */ |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 1126 | n = 0; |
| 1127 | goto handled; |
| 1128 | } |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1129 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1130 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1131 | eff_buf.token = NULL; |
| 1132 | eff_buf.token_len = 0; |
| 1133 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1134 | #endif |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1135 | if (draining_flow && wsi->u.ws.rxflow_buffer && |
| 1136 | wsi->u.ws.rxflow_pos == wsi->u.ws.rxflow_len) { |
| 1137 | lwsl_info("flow buffer: drained\n"); |
| 1138 | free(wsi->u.ws.rxflow_buffer); |
| 1139 | wsi->u.ws.rxflow_buffer = NULL; |
| 1140 | /* having drained the rxflow buffer, can rearm POLLIN */ |
| 1141 | _libwebsocket_rx_flow_control(wsi); |
| 1142 | } |
David Galeano | 7ffbe1b | 2013-01-10 10:35:32 +0800 | [diff] [blame] | 1143 | |
| 1144 | #ifdef LWS_OPENSSL_SUPPORT |
| 1145 | if (wsi->ssl && SSL_pending(wsi->ssl)) |
| 1146 | goto read_pending; |
| 1147 | #endif |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1148 | break; |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 1149 | |
| 1150 | default: |
Andy Green | 03674a6 | 2013-01-16 11:47:40 +0800 | [diff] [blame] | 1151 | #ifdef LWS_NO_CLIENT |
| 1152 | break; |
| 1153 | #else |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 1154 | n = lws_client_socket_service(context, wsi, pollfd); |
| 1155 | goto handled; |
Andy Green | 03674a6 | 2013-01-16 11:47:40 +0800 | [diff] [blame] | 1156 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 1159 | n = 0; |
| 1160 | goto handled; |
| 1161 | |
| 1162 | close_and_handled: |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1163 | libwebsocket_close_and_free_session(context, wsi, |
| 1164 | LWS_CLOSE_STATUS_NOSTATUS); |
| 1165 | n = 1; |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 1166 | |
| 1167 | handled: |
| 1168 | pollfd->revents = 0; |
| 1169 | return n; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1172 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1173 | /** |
| 1174 | * libwebsocket_context_destroy() - Destroy the websocket context |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1175 | * @context: Websocket context |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1176 | * |
| 1177 | * This function closes any active connections and then frees the |
| 1178 | * context. After calling this, any further use of the context is |
| 1179 | * undefined. |
| 1180 | */ |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1181 | LWS_VISIBLE void |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1182 | libwebsocket_context_destroy(struct libwebsocket_context *context) |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1183 | { |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1184 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1185 | int n; |
| 1186 | int m; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1187 | struct libwebsocket_extension *ext; |
Andy Green | a7109e6 | 2013-02-11 12:05:54 +0800 | [diff] [blame] | 1188 | struct libwebsocket_protocols *protocol = context->protocols; |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1189 | |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 1190 | #ifdef LWS_LATENCY |
| 1191 | if (context->worst_latency_info[0]) |
| 1192 | lwsl_notice("Worst latency: %s\n", context->worst_latency_info); |
| 1193 | #endif |
| 1194 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1195 | for (n = 0; n < context->fds_count; n++) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1196 | struct libwebsocket *wsi = |
| 1197 | context->lws_lookup[context->fds[n].fd]; |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1198 | libwebsocket_close_and_free_session(context, |
Andy Green | 7b92205 | 2013-02-11 11:43:05 +0800 | [diff] [blame] | 1199 | wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */); |
| 1200 | n--; |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1201 | } |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1202 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1203 | /* |
| 1204 | * give all extensions a chance to clean up any per-context |
| 1205 | * allocations they might have made |
| 1206 | */ |
| 1207 | |
| 1208 | ext = context->extensions; |
| 1209 | m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT; |
| 1210 | if (context->listen_port) |
| 1211 | m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT; |
Paulo Roberto Urio | 1f680ab | 2012-06-04 08:40:28 +0800 | [diff] [blame] | 1212 | while (ext && ext->callback) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1213 | ext->callback(context, ext, NULL, |
| 1214 | (enum libwebsocket_extension_callback_reasons)m, |
| 1215 | NULL, NULL, 0); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1216 | ext++; |
| 1217 | } |
Andy Green | a7109e6 | 2013-02-11 12:05:54 +0800 | [diff] [blame] | 1218 | |
| 1219 | /* |
| 1220 | * inform all the protocols that they are done and will have no more |
| 1221 | * callbacks |
| 1222 | */ |
| 1223 | |
| 1224 | while (protocol->callback) { |
| 1225 | protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY, |
| 1226 | NULL, NULL, 0); |
| 1227 | protocol++; |
| 1228 | } |
| 1229 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1230 | #endif |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1231 | |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 1232 | #if defined(WIN32) || defined(_WIN32) |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1233 | #else |
| 1234 | close(context->fd_random); |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1235 | #endif |
| 1236 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1237 | #ifdef LWS_OPENSSL_SUPPORT |
| 1238 | if (context->ssl_ctx) |
| 1239 | SSL_CTX_free(context->ssl_ctx); |
| 1240 | if (context->ssl_client_ctx) |
| 1241 | SSL_CTX_free(context->ssl_client_ctx); |
Andy Green | ad68639 | 2013-02-11 14:50:45 +0800 | [diff] [blame] | 1242 | |
| 1243 | ERR_remove_state(0); |
| 1244 | ERR_free_strings(); |
| 1245 | EVP_cleanup(); |
| 1246 | CRYPTO_cleanup_all_ex_data(); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1247 | #endif |
| 1248 | |
Andy Green | 4659648 | 2013-02-11 11:04:01 +0800 | [diff] [blame] | 1249 | if (context->fds) |
| 1250 | free(context->fds); |
| 1251 | if (context->lws_lookup) |
| 1252 | free(context->lws_lookup); |
| 1253 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1254 | free(context); |
| 1255 | |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 1256 | #if defined(WIN32) || defined(_WIN32) |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1257 | WSACleanup(); |
| 1258 | #endif |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Andy Green | d88146d | 2013-01-22 12:40:35 +0800 | [diff] [blame] | 1261 | /** |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1262 | * libwebsocket_context_user() - get the user data associated with the context |
Andy Green | d88146d | 2013-01-22 12:40:35 +0800 | [diff] [blame] | 1263 | * @context: Websocket context |
| 1264 | * |
| 1265 | * This returns the optional user allocation that can be attached to |
| 1266 | * the context the sockets live in at context_create time. It's a way |
| 1267 | * to let all sockets serviced in the same context share data without |
| 1268 | * using globals statics in the user code. |
| 1269 | */ |
Alon Levy | 0291eb3 | 2012-10-19 11:21:56 +0200 | [diff] [blame] | 1270 | LWS_EXTERN void * |
| 1271 | libwebsocket_context_user(struct libwebsocket_context *context) |
| 1272 | { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1273 | return context->user_space; |
Alon Levy | 0291eb3 | 2012-10-19 11:21:56 +0200 | [diff] [blame] | 1274 | } |
| 1275 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1276 | /** |
| 1277 | * libwebsocket_service() - Service any pending websocket activity |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1278 | * @context: Websocket context |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1279 | * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed |
| 1280 | * service otherwise block and service immediately, returning |
| 1281 | * after the timeout if nothing needed service. |
| 1282 | * |
| 1283 | * This function deals with any pending websocket traffic, for three |
| 1284 | * kinds of event. It handles these events on both server and client |
| 1285 | * types of connection the same. |
| 1286 | * |
| 1287 | * 1) Accept new connections to our context's server |
| 1288 | * |
Andy Green | 6f520a5 | 2013-01-29 17:57:39 +0800 | [diff] [blame] | 1289 | * 2) Call the receive callback for incoming frame data received by |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1290 | * server or client connections. |
| 1291 | * |
| 1292 | * You need to call this service function periodically to all the above |
| 1293 | * functions to happen; if your application is single-threaded you can |
| 1294 | * just call it in your main event loop. |
| 1295 | * |
| 1296 | * Alternatively you can fork a new process that asynchronously handles |
| 1297 | * calling this service in a loop. In that case you are happy if this |
| 1298 | * call blocks your thread until it needs to take care of something and |
| 1299 | * would call it with a large nonzero timeout. Your loop then takes no |
| 1300 | * CPU while there is nothing happening. |
| 1301 | * |
| 1302 | * If you are calling it in a single-threaded app, you don't want it to |
| 1303 | * wait around blocking other things in your loop from happening, so you |
| 1304 | * would call it with a timeout_ms of 0, so it returns immediately if |
| 1305 | * nothing is pending, or as soon as it services whatever was pending. |
| 1306 | */ |
| 1307 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1308 | LWS_VISIBLE int |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1309 | libwebsocket_service(struct libwebsocket_context *context, int timeout_ms) |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1310 | { |
| 1311 | int n; |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1312 | int m; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1313 | |
| 1314 | /* stay dead once we are dead */ |
| 1315 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1316 | if (context == NULL) |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1317 | return 1; |
| 1318 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1319 | /* wait for something to need service */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1320 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1321 | n = poll(context->fds, context->fds_count, timeout_ms); |
Andy Green | 5dc62ea | 2013-09-20 20:26:12 +0800 | [diff] [blame] | 1322 | if (n == 0) /* poll timeout */ { |
| 1323 | libwebsocket_service_fd(context, NULL); |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1324 | return 0; |
Andy Green | 5dc62ea | 2013-09-20 20:26:12 +0800 | [diff] [blame] | 1325 | } |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1326 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1327 | if (n < 0) |
Andy Green | 3928f61 | 2012-07-20 12:58:38 +0800 | [diff] [blame] | 1328 | return -1; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1329 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1330 | /* any socket with events to service? */ |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1331 | |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1332 | for (n = 0; n < context->fds_count; n++) { |
| 1333 | if (!context->fds[n].revents) |
| 1334 | continue; |
| 1335 | m = libwebsocket_service_fd(context, &context->fds[n]); |
| 1336 | if (m < 0) |
| 1337 | return -1; |
| 1338 | /* if something closed, retry this slot */ |
| 1339 | if (m) |
| 1340 | n--; |
| 1341 | } |
| 1342 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1343 | return 0; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1346 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1347 | int |
| 1348 | lws_any_extension_handled(struct libwebsocket_context *context, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1349 | struct libwebsocket *wsi, |
| 1350 | enum libwebsocket_extension_callback_reasons r, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1351 | void *v, size_t len) |
| 1352 | { |
| 1353 | int n; |
| 1354 | int handled = 0; |
| 1355 | |
| 1356 | /* maybe an extension will take care of it for us */ |
| 1357 | |
| 1358 | for (n = 0; n < wsi->count_active_extensions && !handled; n++) { |
| 1359 | if (!wsi->active_extensions[n]->callback) |
| 1360 | continue; |
| 1361 | |
| 1362 | handled |= wsi->active_extensions[n]->callback(context, |
| 1363 | wsi->active_extensions[n], wsi, |
| 1364 | r, wsi->active_extensions_user[n], v, len); |
| 1365 | } |
| 1366 | |
| 1367 | return handled; |
| 1368 | } |
| 1369 | |
| 1370 | |
| 1371 | void * |
| 1372 | lws_get_extension_user_matching_ext(struct libwebsocket *wsi, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1373 | struct libwebsocket_extension *ext) |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1374 | { |
| 1375 | int n = 0; |
| 1376 | |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 1377 | if (wsi == NULL) |
| 1378 | return NULL; |
| 1379 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1380 | while (n < wsi->count_active_extensions) { |
| 1381 | if (wsi->active_extensions[n] != ext) { |
| 1382 | n++; |
| 1383 | continue; |
| 1384 | } |
| 1385 | return wsi->active_extensions_user[n]; |
| 1386 | } |
| 1387 | |
| 1388 | return NULL; |
| 1389 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1390 | #endif |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1391 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1392 | /** |
| 1393 | * libwebsocket_callback_on_writable() - Request a callback when this socket |
| 1394 | * becomes able to be written to without |
| 1395 | * blocking |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 1396 | * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1397 | * @context: libwebsockets context |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1398 | * @wsi: Websocket connection instance to get callback for |
| 1399 | */ |
| 1400 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1401 | LWS_VISIBLE int |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1402 | libwebsocket_callback_on_writable(struct libwebsocket_context *context, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1403 | struct libwebsocket *wsi) |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1404 | { |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1405 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1406 | int n; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1407 | int handled = 0; |
| 1408 | |
| 1409 | /* maybe an extension will take care of it for us */ |
| 1410 | |
| 1411 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 1412 | if (!wsi->active_extensions[n]->callback) |
| 1413 | continue; |
| 1414 | |
| 1415 | handled |= wsi->active_extensions[n]->callback(context, |
| 1416 | wsi->active_extensions[n], wsi, |
| 1417 | LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE, |
| 1418 | wsi->active_extensions_user[n], NULL, 0); |
| 1419 | } |
| 1420 | |
| 1421 | if (handled) |
| 1422 | return 1; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1423 | #endif |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1424 | if (wsi->position_in_fds_table < 0) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1425 | lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n", |
| 1426 | wsi->sock); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1427 | return -1; |
| 1428 | } |
| 1429 | |
| 1430 | context->fds[wsi->position_in_fds_table].events |= POLLOUT; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1431 | |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1432 | /* external POLL support via protocol 0 */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1433 | context->protocols[0].callback(context, wsi, |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1434 | LWS_CALLBACK_SET_MODE_POLL_FD, |
Andy Green | 50097dd | 2013-02-15 22:36:30 +0800 | [diff] [blame] | 1435 | wsi->user_space, (void *)(long)wsi->sock, POLLOUT); |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1436 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1437 | return 1; |
| 1438 | } |
| 1439 | |
| 1440 | /** |
| 1441 | * libwebsocket_callback_on_writable_all_protocol() - Request a callback for |
| 1442 | * all connections using the given protocol when it |
| 1443 | * becomes possible to write to each socket without |
| 1444 | * blocking in turn. |
| 1445 | * |
| 1446 | * @protocol: Protocol whose connections will get callbacks |
| 1447 | */ |
| 1448 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1449 | LWS_VISIBLE int |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1450 | libwebsocket_callback_on_writable_all_protocol( |
| 1451 | const struct libwebsocket_protocols *protocol) |
| 1452 | { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1453 | struct libwebsocket_context *context = protocol->owning_server; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1454 | int n; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1455 | struct libwebsocket *wsi; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1456 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1457 | for (n = 0; n < context->fds_count; n++) { |
| 1458 | wsi = context->lws_lookup[context->fds[n].fd]; |
| 1459 | if (!wsi) |
| 1460 | continue; |
| 1461 | if (wsi->protocol == protocol) |
| 1462 | libwebsocket_callback_on_writable(context, wsi); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1463 | } |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1464 | |
| 1465 | return 0; |
| 1466 | } |
| 1467 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1468 | /** |
| 1469 | * libwebsocket_set_timeout() - marks the wsi as subject to a timeout |
| 1470 | * |
| 1471 | * You will not need this unless you are doing something special |
| 1472 | * |
| 1473 | * @wsi: Websocket connection instance |
| 1474 | * @reason: timeout reason |
| 1475 | * @secs: how many seconds |
| 1476 | */ |
| 1477 | |
| 1478 | void |
| 1479 | libwebsocket_set_timeout(struct libwebsocket *wsi, |
| 1480 | enum pending_timeout reason, int secs) |
| 1481 | { |
| 1482 | struct timeval tv; |
| 1483 | |
| 1484 | gettimeofday(&tv, NULL); |
| 1485 | |
| 1486 | wsi->pending_timeout_limit = tv.tv_sec + secs; |
| 1487 | wsi->pending_timeout = reason; |
| 1488 | } |
| 1489 | |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 1490 | |
| 1491 | /** |
| 1492 | * libwebsocket_get_socket_fd() - returns the socket file descriptor |
| 1493 | * |
| 1494 | * You will not need this unless you are doing something special |
| 1495 | * |
| 1496 | * @wsi: Websocket connection instance |
| 1497 | */ |
| 1498 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1499 | LWS_VISIBLE int |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 1500 | libwebsocket_get_socket_fd(struct libwebsocket *wsi) |
| 1501 | { |
| 1502 | return wsi->sock; |
| 1503 | } |
| 1504 | |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 1505 | #ifdef LWS_LATENCY |
| 1506 | void |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1507 | lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi, |
| 1508 | const char *action, int ret, int completed) |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 1509 | { |
| 1510 | struct timeval tv; |
| 1511 | unsigned long u; |
| 1512 | char buf[256]; |
| 1513 | |
| 1514 | gettimeofday(&tv, NULL); |
| 1515 | |
| 1516 | u = (tv.tv_sec * 1000000) + tv.tv_usec; |
| 1517 | |
| 1518 | if (action) { |
| 1519 | if (completed) { |
| 1520 | if (wsi->action_start == wsi->latency_start) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1521 | sprintf(buf, |
| 1522 | "Completion first try lat %luus: %p: ret %d: %s\n", |
| 1523 | u - wsi->latency_start, |
| 1524 | (void *)wsi, ret, action); |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 1525 | else |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1526 | sprintf(buf, |
| 1527 | "Completion %luus: lat %luus: %p: ret %d: %s\n", |
| 1528 | u - wsi->action_start, |
| 1529 | u - wsi->latency_start, |
| 1530 | (void *)wsi, ret, action); |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 1531 | wsi->action_start = 0; |
| 1532 | } else |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1533 | sprintf(buf, "lat %luus: %p: ret %d: %s\n", |
| 1534 | u - wsi->latency_start, |
| 1535 | (void *)wsi, ret, action); |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 1536 | if (u - wsi->latency_start > context->worst_latency) { |
| 1537 | context->worst_latency = u - wsi->latency_start; |
| 1538 | strcpy(context->worst_latency_info, buf); |
| 1539 | } |
| 1540 | lwsl_latency("%s", buf); |
| 1541 | } else { |
| 1542 | wsi->latency_start = u; |
| 1543 | if (!wsi->action_start) |
| 1544 | wsi->action_start = u; |
| 1545 | } |
| 1546 | } |
| 1547 | #endif |
| 1548 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1549 | #ifdef LWS_NO_SERVER |
| 1550 | int |
Andy Green | 8d15cf4 | 2013-10-24 21:47:06 +0800 | [diff] [blame] | 1551 | _libwebsocket_rx_flow_control(struct libwebsocket *wsi) |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1552 | { |
| 1553 | return 0; |
| 1554 | } |
| 1555 | #else |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1556 | int |
| 1557 | _libwebsocket_rx_flow_control(struct libwebsocket *wsi) |
| 1558 | { |
| 1559 | struct libwebsocket_context *context = wsi->protocol->owning_server; |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1560 | |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1561 | /* there is no pending change */ |
| 1562 | if (!(wsi->u.ws.rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE)) |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1563 | return 0; |
| 1564 | |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1565 | /* stuff is still buffered, not ready to really accept new input */ |
| 1566 | if (wsi->u.ws.rxflow_buffer) { |
| 1567 | /* get ourselves called back to deal with stashed buffer */ |
| 1568 | libwebsocket_callback_on_writable(context, wsi); |
| 1569 | return 0; |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1570 | } |
| 1571 | |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1572 | /* pending is cleared, we can change rxflow state */ |
| 1573 | |
| 1574 | wsi->u.ws.rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE; |
| 1575 | |
| 1576 | lwsl_info("rxflow: wsi %p change_to %d\n", wsi, |
| 1577 | wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW); |
| 1578 | |
| 1579 | /* adjust the pollfd for this wsi */ |
| 1580 | |
| 1581 | if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW) |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1582 | context->fds[wsi->position_in_fds_table].events |= POLLIN; |
| 1583 | else |
| 1584 | context->fds[wsi->position_in_fds_table].events &= ~POLLIN; |
| 1585 | |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1586 | if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW) |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1587 | /* external POLL support via protocol 0 */ |
| 1588 | context->protocols[0].callback(context, wsi, |
| 1589 | LWS_CALLBACK_SET_MODE_POLL_FD, |
Andy Green | 50097dd | 2013-02-15 22:36:30 +0800 | [diff] [blame] | 1590 | wsi->user_space, (void *)(long)wsi->sock, POLLIN); |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1591 | else |
| 1592 | /* external POLL support via protocol 0 */ |
| 1593 | context->protocols[0].callback(context, wsi, |
| 1594 | LWS_CALLBACK_CLEAR_MODE_POLL_FD, |
Andy Green | 50097dd | 2013-02-15 22:36:30 +0800 | [diff] [blame] | 1595 | wsi->user_space, (void *)(long)wsi->sock, POLLIN); |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1596 | |
| 1597 | return 1; |
| 1598 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1599 | #endif |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1600 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1601 | /** |
| 1602 | * libwebsocket_rx_flow_control() - Enable and disable socket servicing for |
| 1603 | * receieved packets. |
| 1604 | * |
| 1605 | * If the output side of a server process becomes choked, this allows flow |
| 1606 | * control for the input side. |
| 1607 | * |
| 1608 | * @wsi: Websocket connection instance to get callback for |
| 1609 | * @enable: 0 = disable read servicing for this connection, 1 = enable |
| 1610 | */ |
| 1611 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1612 | LWS_VISIBLE int |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1613 | libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable) |
| 1614 | { |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1615 | if (enable == (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) |
| 1616 | return 0; |
| 1617 | |
| 1618 | lwsl_info("libwebsocket_rx_flow_control(0x%p, %d)\n", wsi, enable); |
| 1619 | wsi->u.ws.rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1620 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1621 | return 0; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Andy Green | b55451c | 2013-03-16 12:32:27 +0800 | [diff] [blame] | 1624 | /** |
| 1625 | * libwebsocket_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive |
| 1626 | * |
| 1627 | * When the user server code realizes it can accept more input, it can |
| 1628 | * call this to have the RX flow restriction removed from all connections using |
| 1629 | * the given protocol. |
| 1630 | * |
| 1631 | * @protocol: all connections using this protocol will be allowed to receive |
| 1632 | */ |
| 1633 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1634 | LWS_VISIBLE void |
Andy Green | b55451c | 2013-03-16 12:32:27 +0800 | [diff] [blame] | 1635 | libwebsocket_rx_flow_allow_all_protocol( |
| 1636 | const struct libwebsocket_protocols *protocol) |
| 1637 | { |
| 1638 | struct libwebsocket_context *context = protocol->owning_server; |
| 1639 | int n; |
| 1640 | struct libwebsocket *wsi; |
| 1641 | |
| 1642 | for (n = 0; n < context->fds_count; n++) { |
| 1643 | wsi = context->lws_lookup[context->fds[n].fd]; |
| 1644 | if (!wsi) |
| 1645 | continue; |
| 1646 | if (wsi->protocol == protocol) |
| 1647 | libwebsocket_rx_flow_control(wsi, LWS_RXFLOW_ALLOW); |
| 1648 | } |
| 1649 | } |
| 1650 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1651 | |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 1652 | /** |
| 1653 | * libwebsocket_canonical_hostname() - returns this host's hostname |
| 1654 | * |
| 1655 | * This is typically used by client code to fill in the host parameter |
| 1656 | * when making a client connection. You can only call it after the context |
| 1657 | * has been created. |
| 1658 | * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1659 | * @context: Websocket context |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 1660 | */ |
| 1661 | |
| 1662 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1663 | LWS_VISIBLE extern const char * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1664 | libwebsocket_canonical_hostname(struct libwebsocket_context *context) |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 1665 | { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1666 | return (const char *)context->canonical_hostname; |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1670 | static void sigpipe_handler(int x) |
| 1671 | { |
| 1672 | } |
| 1673 | |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 1674 | #ifdef LWS_OPENSSL_SUPPORT |
| 1675 | static int |
| 1676 | OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx) |
| 1677 | { |
| 1678 | |
| 1679 | SSL *ssl; |
| 1680 | int n; |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 1681 | struct libwebsocket_context *context; |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 1682 | |
| 1683 | ssl = X509_STORE_CTX_get_ex_data(x509_ctx, |
| 1684 | SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 1685 | |
| 1686 | /* |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 1687 | * !!! nasty openssl requires the index to come as a library-scope |
| 1688 | * static |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 1689 | */ |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 1690 | context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index); |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1691 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1692 | n = context->protocols[0].callback(NULL, NULL, |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 1693 | LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION, |
| 1694 | x509_ctx, ssl, preverify_ok); |
| 1695 | |
| 1696 | /* convert return code from 0 = OK to 1 = OK */ |
| 1697 | |
| 1698 | if (!n) |
| 1699 | n = 1; |
| 1700 | else |
| 1701 | n = 0; |
| 1702 | |
| 1703 | return n; |
| 1704 | } |
| 1705 | #endif |
| 1706 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1707 | int user_callback_handle_rxflow(callback_function callback_function, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1708 | struct libwebsocket_context *context, |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1709 | struct libwebsocket *wsi, |
| 1710 | enum libwebsocket_callback_reasons reason, void *user, |
| 1711 | void *in, size_t len) |
| 1712 | { |
| 1713 | int n; |
| 1714 | |
| 1715 | n = callback_function(context, wsi, reason, user, in, len); |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 1716 | if (!n) |
| 1717 | n = _libwebsocket_rx_flow_control(wsi); |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1718 | |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 1719 | return n; |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1720 | } |
| 1721 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1722 | |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 1723 | /** |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1724 | * libwebsocket_create_context() - Create the websocket handler |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 1725 | * @info: pointer to struct with parameters |
Andy Green | 05464c6 | 2010-11-12 10:44:18 +0000 | [diff] [blame] | 1726 | * |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 1727 | * This function creates the listening socket (if serving) and takes care |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1728 | * of all initialization in one step. |
| 1729 | * |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1730 | * After initialization, it returns a struct libwebsocket_context * that |
| 1731 | * represents this server. After calling, user code needs to take care |
| 1732 | * of calling libwebsocket_service() with the context pointer to get the |
| 1733 | * server's sockets serviced. This can be done in the same process context |
| 1734 | * or a forked process, or another thread, |
Andy Green | 05464c6 | 2010-11-12 10:44:18 +0000 | [diff] [blame] | 1735 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1736 | * The protocol callback functions are called for a handful of events |
| 1737 | * including http requests coming in, websocket connections becoming |
| 1738 | * established, and data arriving; it's also called periodically to allow |
| 1739 | * async transmission. |
| 1740 | * |
| 1741 | * HTTP requests are sent always to the FIRST protocol in @protocol, since |
| 1742 | * at that time websocket protocol has not been negotiated. Other |
| 1743 | * protocols after the first one never see any HTTP callack activity. |
| 1744 | * |
| 1745 | * The server created is a simple http server by default; part of the |
| 1746 | * websocket standard is upgrading this http connection to a websocket one. |
| 1747 | * |
| 1748 | * This allows the same server to provide files like scripts and favicon / |
| 1749 | * images or whatever over http and dynamic data over websockets all in |
| 1750 | * one place; they're all handled in the user callback. |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 1751 | */ |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 1752 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1753 | LWS_VISIBLE struct libwebsocket_context * |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 1754 | libwebsocket_create_context(struct lws_context_creation_info *info) |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 1755 | { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1756 | struct libwebsocket_context *context = NULL; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1757 | char *p; |
Andy Green | 14f4729 | 2013-02-11 19:36:15 +0800 | [diff] [blame] | 1758 | int n; |
Joakim Soderberg | 3baa08c | 2013-02-22 09:27:59 +0800 | [diff] [blame] | 1759 | #ifndef LWS_NO_SERVER |
Andy Green | cbb3122 | 2013-01-31 09:57:05 +0800 | [diff] [blame] | 1760 | int opt = 1; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1761 | struct libwebsocket *wsi; |
Andy Green | cbb3122 | 2013-01-31 09:57:05 +0800 | [diff] [blame] | 1762 | struct sockaddr_in serv_addr; |
| 1763 | #endif |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1764 | #ifndef LWS_NO_EXTENSIONS |
| 1765 | int m; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 1766 | struct libwebsocket_extension *ext; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1767 | #endif |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 1768 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1769 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | f2f54d5 | 2010-11-15 22:08:00 +0000 | [diff] [blame] | 1770 | SSL_METHOD *method; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1771 | #endif |
| 1772 | |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 1773 | #ifndef LWS_NO_DAEMONIZE |
Joakim Söderberg | 68e8d73 | 2013-02-06 15:27:39 +0900 | [diff] [blame] | 1774 | int pid_daemon = get_daemonize_pid(); |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 1775 | #endif |
| 1776 | |
Andy Green | b3a614a | 2013-01-19 13:08:17 +0800 | [diff] [blame] | 1777 | lwsl_notice("Initial logging level %d\n", log_level); |
Andy Green | 7b40545 | 2013-02-01 10:50:15 +0800 | [diff] [blame] | 1778 | lwsl_notice("Library version: %s\n", library_version); |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 1779 | lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN); |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 1780 | lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS); |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1781 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1782 | lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n", |
| 1783 | LWS_MAX_EXTENSIONS_ACTIVE); |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1784 | #else |
| 1785 | lwsl_notice(" Configured without extension support\n"); |
| 1786 | #endif |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 1787 | lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED); |
| 1788 | lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT); |
Andy Green | 2672fb2 | 2013-02-22 09:54:35 +0800 | [diff] [blame] | 1789 | if (info->ssl_cipher_list) |
| 1790 | lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list); |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 1791 | lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH); |
| 1792 | lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1793 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1794 | #ifdef _WIN32 |
| 1795 | { |
| 1796 | WORD wVersionRequested; |
| 1797 | WSADATA wsaData; |
| 1798 | int err; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1799 | HMODULE wsdll; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1800 | |
| 1801 | /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */ |
| 1802 | wVersionRequested = MAKEWORD(2, 2); |
| 1803 | |
| 1804 | err = WSAStartup(wVersionRequested, &wsaData); |
| 1805 | if (err != 0) { |
| 1806 | /* Tell the user that we could not find a usable */ |
| 1807 | /* Winsock DLL. */ |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1808 | lwsl_err("WSAStartup failed with error: %d\n", err); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1809 | return NULL; |
| 1810 | } |
David Galeano | 7b11fec | 2011-10-04 19:55:18 +0800 | [diff] [blame] | 1811 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1812 | /* default to a poll() made out of select() */ |
| 1813 | poll = emulated_poll; |
David Galeano | 7b11fec | 2011-10-04 19:55:18 +0800 | [diff] [blame] | 1814 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1815 | /* if windows socket lib available, use his WSAPoll */ |
David Galeano | cb19368 | 2013-01-09 15:29:00 +0800 | [diff] [blame] | 1816 | wsdll = GetModuleHandle(_T("Ws2_32.dll")); |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1817 | if (wsdll) |
| 1818 | poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll"); |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 1819 | |
Joakim Söderberg | 68e8d73 | 2013-02-06 15:27:39 +0900 | [diff] [blame] | 1820 | /* Finally fall back to emulated poll if all else fails */ |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 1821 | if (!poll) |
| 1822 | poll = emulated_poll; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1823 | } |
| 1824 | #endif |
| 1825 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1826 | context = (struct libwebsocket_context *) |
| 1827 | malloc(sizeof(struct libwebsocket_context)); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1828 | if (!context) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1829 | lwsl_err("No memory for websocket context\n"); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1830 | return NULL; |
| 1831 | } |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 1832 | memset(context, 0, sizeof(*context)); |
Andy Green | 35f332b | 2013-01-21 13:06:38 +0800 | [diff] [blame] | 1833 | #ifndef LWS_NO_DAEMONIZE |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 1834 | context->started_with_parent = pid_daemon; |
| 1835 | lwsl_notice(" Started with daemon pid %d\n", pid_daemon); |
| 1836 | #endif |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1837 | |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 1838 | context->listen_service_extraseen = 0; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 1839 | context->protocols = info->protocols; |
| 1840 | context->listen_port = info->port; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1841 | context->http_proxy_port = 0; |
| 1842 | context->http_proxy_address[0] = '\0'; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 1843 | context->options = info->options; |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1844 | /* to reduce this allocation, */ |
| 1845 | context->max_fds = getdtablesize(); |
Andy Green | a86f634 | 2013-02-11 11:04:56 +0800 | [diff] [blame] | 1846 | lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n", |
| 1847 | sizeof(struct libwebsocket_context), |
| 1848 | sizeof(struct pollfd) + sizeof(struct libwebsocket *), |
| 1849 | context->max_fds, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1850 | sizeof(struct libwebsocket_context) + |
| 1851 | ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) * |
| 1852 | context->max_fds)); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1853 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1854 | context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) * |
| 1855 | context->max_fds); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1856 | if (context->fds == NULL) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1857 | lwsl_err("Unable to allocate fds array for %d connections\n", |
| 1858 | context->max_fds); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1859 | free(context); |
| 1860 | return NULL; |
| 1861 | } |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1862 | context->lws_lookup = (struct libwebsocket **) |
| 1863 | malloc(sizeof(struct libwebsocket *) * context->max_fds); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1864 | if (context->lws_lookup == NULL) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1865 | lwsl_err( |
| 1866 | "Unable to allocate lws_lookup array for %d connections\n", |
| 1867 | context->max_fds); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1868 | free(context->fds); |
| 1869 | free(context); |
| 1870 | return NULL; |
| 1871 | } |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 1872 | memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) * |
| 1873 | context->max_fds); |
Andy Green | a17c692 | 2013-01-20 20:21:54 +0800 | [diff] [blame] | 1874 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1875 | context->fds_count = 0; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1876 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 1877 | context->extensions = info->extensions; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1878 | #endif |
Paulo Roberto Urio | 1e32663 | 2012-06-04 10:52:19 +0800 | [diff] [blame] | 1879 | context->last_timeout_check_s = 0; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 1880 | context->user_space = info->user; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1881 | |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 1882 | #if defined(WIN32) || defined(_WIN32) |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1883 | context->fd_random = 0; |
| 1884 | #else |
| 1885 | context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY); |
| 1886 | if (context->fd_random < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1887 | lwsl_err("Unable to open random device %s %d\n", |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1888 | SYSTEM_RANDOM_FILEPATH, context->fd_random); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 1889 | goto bail; |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame] | 1890 | } |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1891 | #endif |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame] | 1892 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1893 | #ifdef LWS_OPENSSL_SUPPORT |
| 1894 | context->use_ssl = 0; |
| 1895 | context->ssl_ctx = NULL; |
| 1896 | context->ssl_client_ctx = NULL; |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 1897 | openssl_websocket_private_data_index = 0; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1898 | #endif |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 1899 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1900 | strcpy(context->canonical_hostname, "unknown"); |
Andy Green | a69f051 | 2012-05-03 12:32:38 +0800 | [diff] [blame] | 1901 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1902 | #ifndef LWS_NO_SERVER |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 1903 | if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) { |
Andy Green | 788c4a8 | 2012-10-22 12:29:57 +0100 | [diff] [blame] | 1904 | /* find canonical hostname */ |
Andy Green | adc7146 | 2013-02-14 17:11:22 +0800 | [diff] [blame] | 1905 | gethostname((char *)context->canonical_hostname, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1906 | sizeof(context->canonical_hostname) - 1); |
Andy Green | 788c4a8 | 2012-10-22 12:29:57 +0100 | [diff] [blame] | 1907 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1908 | lwsl_notice(" canonical_hostname = %s\n", |
| 1909 | context->canonical_hostname); |
Andy Green | a69f051 | 2012-05-03 12:32:38 +0800 | [diff] [blame] | 1910 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1911 | #endif |
Andy Green | a69f051 | 2012-05-03 12:32:38 +0800 | [diff] [blame] | 1912 | |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1913 | /* split the proxy ads:port if given */ |
| 1914 | |
| 1915 | p = getenv("http_proxy"); |
| 1916 | if (p) { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1917 | strncpy(context->http_proxy_address, p, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1918 | sizeof(context->http_proxy_address) - 1); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1919 | context->http_proxy_address[ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1920 | sizeof(context->http_proxy_address) - 1] = '\0'; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1921 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1922 | p = strchr(context->http_proxy_address, ':'); |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1923 | if (p == NULL) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1924 | lwsl_err("http_proxy needs to be ads:port\n"); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 1925 | goto bail; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1926 | } |
| 1927 | *p = '\0'; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1928 | context->http_proxy_port = atoi(p + 1); |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1929 | |
Andy Green | b3a614a | 2013-01-19 13:08:17 +0800 | [diff] [blame] | 1930 | lwsl_notice(" Proxy %s:%u\n", |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1931 | context->http_proxy_address, |
| 1932 | context->http_proxy_port); |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1933 | } |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1934 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1935 | #ifndef LWS_NO_SERVER |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 1936 | if (info->port) { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1937 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1938 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 1939 | context->use_ssl = info->ssl_cert_filepath != NULL && |
| 1940 | info->ssl_private_key_filepath != NULL; |
Andy Green | 23c5f2e | 2013-02-06 15:43:00 +0900 | [diff] [blame] | 1941 | #ifdef USE_CYASSL |
| 1942 | lwsl_notice(" Compiled with CYASSL support\n"); |
| 1943 | #else |
| 1944 | lwsl_notice(" Compiled with OpenSSL support\n"); |
| 1945 | #endif |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1946 | if (context->use_ssl) |
Andy Green | 23c5f2e | 2013-02-06 15:43:00 +0900 | [diff] [blame] | 1947 | lwsl_notice(" Using SSL mode\n"); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1948 | else |
Andy Green | 23c5f2e | 2013-02-06 15:43:00 +0900 | [diff] [blame] | 1949 | lwsl_notice(" Using non-SSL mode\n"); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1950 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1951 | #else |
Andy Green | 2b40b79 | 2013-02-10 22:22:01 +0800 | [diff] [blame] | 1952 | if (info->ssl_cert_filepath != NULL && |
| 1953 | info->ssl_private_key_filepath != NULL) { |
Andy Green | b3a614a | 2013-01-19 13:08:17 +0800 | [diff] [blame] | 1954 | lwsl_notice(" Not compiled for OpenSSl support!\n"); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 1955 | goto bail; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1956 | } |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1957 | lwsl_notice(" Compiled without SSL support\n"); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1958 | #endif |
Andy Green | a17c692 | 2013-01-20 20:21:54 +0800 | [diff] [blame] | 1959 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1960 | lwsl_notice( |
| 1961 | " per-conn mem: %u + %u headers + protocol rx buf\n", |
| 1962 | sizeof(struct libwebsocket), |
| 1963 | sizeof(struct allocated_headers)); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1964 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1965 | #endif |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1966 | |
| 1967 | /* ignore SIGPIPE */ |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 1968 | #if defined(WIN32) || defined(_WIN32) |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1969 | #else |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1970 | signal(SIGPIPE, sigpipe_handler); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1971 | #endif |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1972 | |
| 1973 | |
| 1974 | #ifdef LWS_OPENSSL_SUPPORT |
| 1975 | |
| 1976 | /* basic openssl init */ |
| 1977 | |
| 1978 | SSL_library_init(); |
| 1979 | |
| 1980 | OpenSSL_add_all_algorithms(); |
| 1981 | SSL_load_error_strings(); |
| 1982 | |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 1983 | openssl_websocket_private_data_index = |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 1984 | SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL); |
| 1985 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1986 | /* |
| 1987 | * Firefox insists on SSLv23 not SSLv3 |
| 1988 | * Konq disables SSLv2 by default now, SSLv23 works |
| 1989 | */ |
| 1990 | |
| 1991 | method = (SSL_METHOD *)SSLv23_server_method(); |
| 1992 | if (!method) { |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 1993 | int error = ERR_get_error(); |
Joakim Soderberg | b82b0dd | 2013-02-22 09:28:15 +0800 | [diff] [blame] | 1994 | lwsl_err("problem creating ssl method %lu: %s\n", |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 1995 | error, |
| 1996 | ERR_error_string(error, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1997 | (char *)context->service_buffer)); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 1998 | goto bail; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1999 | } |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2000 | context->ssl_ctx = SSL_CTX_new(method); /* create context */ |
| 2001 | if (!context->ssl_ctx) { |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2002 | int error = ERR_get_error(); |
Joakim Soderberg | b82b0dd | 2013-02-22 09:28:15 +0800 | [diff] [blame] | 2003 | lwsl_err("problem creating ssl context %lu: %s\n", |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2004 | error, |
| 2005 | ERR_error_string(error, |
| 2006 | (char *)context->service_buffer)); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2007 | goto bail; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
David Galeano | cc148e4 | 2013-01-10 10:18:59 +0800 | [diff] [blame] | 2010 | #ifdef SSL_OP_NO_COMPRESSION |
David Galeano | c72f6f9 | 2013-01-10 10:11:57 +0800 | [diff] [blame] | 2011 | SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION); |
David Galeano | cc148e4 | 2013-01-10 10:18:59 +0800 | [diff] [blame] | 2012 | #endif |
David Galeano | 77a677c | 2013-01-10 10:14:12 +0800 | [diff] [blame] | 2013 | SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE); |
Andy Green | 2672fb2 | 2013-02-22 09:54:35 +0800 | [diff] [blame] | 2014 | if (info->ssl_cipher_list) |
| 2015 | SSL_CTX_set_cipher_list(context->ssl_ctx, |
| 2016 | info->ssl_cipher_list); |
David Galeano | c72f6f9 | 2013-01-10 10:11:57 +0800 | [diff] [blame] | 2017 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 2018 | #ifndef LWS_NO_CLIENT |
| 2019 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2020 | /* client context */ |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2021 | |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2022 | if (info->port == CONTEXT_PORT_NO_LISTEN) { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2023 | method = (SSL_METHOD *)SSLv23_client_method(); |
| 2024 | if (!method) { |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2025 | int error = ERR_get_error(); |
Joakim Soderberg | b82b0dd | 2013-02-22 09:28:15 +0800 | [diff] [blame] | 2026 | lwsl_err("problem creating ssl method %lu: %s\n", |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2027 | error, |
| 2028 | ERR_error_string(error, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2029 | (char *)context->service_buffer)); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2030 | goto bail; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2031 | } |
| 2032 | /* create context */ |
| 2033 | context->ssl_client_ctx = SSL_CTX_new(method); |
| 2034 | if (!context->ssl_client_ctx) { |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2035 | int error = ERR_get_error(); |
Joakim Soderberg | b82b0dd | 2013-02-22 09:28:15 +0800 | [diff] [blame] | 2036 | lwsl_err("problem creating ssl context %lu: %s\n", |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2037 | error, |
| 2038 | ERR_error_string(error, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2039 | (char *)context->service_buffer)); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2040 | goto bail; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2041 | } |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2042 | |
David Galeano | cc148e4 | 2013-01-10 10:18:59 +0800 | [diff] [blame] | 2043 | #ifdef SSL_OP_NO_COMPRESSION |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2044 | SSL_CTX_set_options(context->ssl_client_ctx, |
| 2045 | SSL_OP_NO_COMPRESSION); |
David Galeano | cc148e4 | 2013-01-10 10:18:59 +0800 | [diff] [blame] | 2046 | #endif |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2047 | SSL_CTX_set_options(context->ssl_client_ctx, |
| 2048 | SSL_OP_CIPHER_SERVER_PREFERENCE); |
Andy Green | 2672fb2 | 2013-02-22 09:54:35 +0800 | [diff] [blame] | 2049 | if (info->ssl_cipher_list) |
| 2050 | SSL_CTX_set_cipher_list(context->ssl_client_ctx, |
| 2051 | info->ssl_cipher_list); |
David Galeano | c72f6f9 | 2013-01-10 10:11:57 +0800 | [diff] [blame] | 2052 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2053 | /* openssl init for cert verification (for client sockets) */ |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2054 | if (!info->ssl_ca_filepath) { |
David Galeano | 2f82be8 | 2013-01-09 16:25:54 +0800 | [diff] [blame] | 2055 | if (!SSL_CTX_load_verify_locations( |
| 2056 | context->ssl_client_ctx, NULL, |
| 2057 | LWS_OPENSSL_CLIENT_CERTS)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2058 | lwsl_err( |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2059 | "Unable to load SSL Client certs from %s " |
| 2060 | "(set by --with-client-cert-dir= " |
| 2061 | "in configure) -- client ssl isn't " |
| 2062 | "going to work", LWS_OPENSSL_CLIENT_CERTS); |
David Galeano | 2f82be8 | 2013-01-09 16:25:54 +0800 | [diff] [blame] | 2063 | } else |
| 2064 | if (!SSL_CTX_load_verify_locations( |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2065 | context->ssl_client_ctx, info->ssl_ca_filepath, |
David Galeano | 2f82be8 | 2013-01-09 16:25:54 +0800 | [diff] [blame] | 2066 | NULL)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2067 | lwsl_err( |
David Galeano | 2f82be8 | 2013-01-09 16:25:54 +0800 | [diff] [blame] | 2068 | "Unable to load SSL Client certs " |
| 2069 | "file from %s -- client ssl isn't " |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2070 | "going to work", info->ssl_ca_filepath); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2071 | |
| 2072 | /* |
| 2073 | * callback allowing user code to load extra verification certs |
| 2074 | * helping the client to verify server identity |
| 2075 | */ |
| 2076 | |
prasannateamf1 | b353a45 | 2013-11-12 17:21:01 -0800 | [diff] [blame] | 2077 | /* support for client-side certificate authentication */ |
| 2078 | if (info->ssl_cert_filepath) { |
| 2079 | n = SSL_CTX_use_certificate_chain_file( |
| 2080 | context->ssl_client_ctx, |
| 2081 | info->ssl_cert_filepath); |
| 2082 | if (n != 1) { |
| 2083 | lwsl_err("problem getting cert '%s' %lu: %s\n", |
| 2084 | info->ssl_cert_filepath, |
| 2085 | ERR_get_error(), |
| 2086 | ERR_error_string(ERR_get_error(), |
| 2087 | (char *)context->service_buffer)); |
| 2088 | goto bail; |
| 2089 | } |
| 2090 | } |
| 2091 | if (info->ssl_private_key_filepath) { |
| 2092 | /* set the private key from KeyFile */ |
| 2093 | if (SSL_CTX_use_PrivateKey_file(context->ssl_client_ctx, |
| 2094 | info->ssl_private_key_filepath, |
| 2095 | SSL_FILETYPE_PEM) != 1) { |
| 2096 | lwsl_err("use_PrivateKey_file '%s' %lu: %s\n", |
| 2097 | info->ssl_private_key_filepath, |
| 2098 | ERR_get_error(), |
| 2099 | ERR_error_string(ERR_get_error(), |
| 2100 | (char *)context->service_buffer)); |
| 2101 | goto bail; |
| 2102 | } |
| 2103 | |
| 2104 | /* verify private key */ |
| 2105 | if (!SSL_CTX_check_private_key(context->ssl_client_ctx)) { |
| 2106 | lwsl_err("Private SSL key doesn't match cert\n"); |
| 2107 | goto bail; |
| 2108 | } |
| 2109 | } |
| 2110 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2111 | context->protocols[0].callback(context, NULL, |
| 2112 | LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS, |
| 2113 | context->ssl_client_ctx, NULL, 0); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2114 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 2115 | #endif |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2116 | |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 2117 | /* as a server, are we requiring clients to identify themselves? */ |
| 2118 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2119 | if (info->options & |
| 2120 | LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) { |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 2121 | |
| 2122 | /* absolutely require the client cert */ |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2123 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2124 | SSL_CTX_set_verify(context->ssl_ctx, |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 2125 | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, |
| 2126 | OpenSSL_verify_callback); |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 2127 | |
| 2128 | /* |
| 2129 | * give user code a chance to load certs into the server |
| 2130 | * allowing it to verify incoming client certs |
| 2131 | */ |
| 2132 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2133 | context->protocols[0].callback(context, NULL, |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 2134 | LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS, |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2135 | context->ssl_ctx, NULL, 0); |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2138 | if (context->use_ssl) { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2139 | |
| 2140 | /* openssl init for server sockets */ |
| 2141 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2142 | /* set the local certificate from CertFile */ |
David Galeano | 9b3d4b2 | 2013-01-10 10:11:21 +0800 | [diff] [blame] | 2143 | n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx, |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2144 | info->ssl_cert_filepath); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2145 | if (n != 1) { |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2146 | int error = ERR_get_error(); |
Joakim Soderberg | b82b0dd | 2013-02-22 09:28:15 +0800 | [diff] [blame] | 2147 | lwsl_err("problem getting cert '%s' %lu: %s\n", |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2148 | info->ssl_cert_filepath, |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2149 | error, |
| 2150 | ERR_error_string(error, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2151 | (char *)context->service_buffer)); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2152 | goto bail; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2153 | } |
| 2154 | /* set the private key from KeyFile */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2155 | if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx, |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2156 | info->ssl_private_key_filepath, |
| 2157 | SSL_FILETYPE_PEM) != 1) { |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2158 | int error = ERR_get_error(); |
Joakim Soderberg | b82b0dd | 2013-02-22 09:28:15 +0800 | [diff] [blame] | 2159 | lwsl_err("ssl problem getting key '%s' %lu: %s\n", |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2160 | info->ssl_private_key_filepath, |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2161 | error, |
| 2162 | ERR_error_string(error, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2163 | (char *)context->service_buffer)); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2164 | goto bail; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2165 | } |
| 2166 | /* verify private key */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2167 | if (!SSL_CTX_check_private_key(context->ssl_ctx)) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2168 | lwsl_err("Private SSL key doesn't match cert\n"); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2169 | goto bail; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | /* SSL is happy and has a cert it's content with */ |
| 2173 | } |
| 2174 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2175 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 2176 | #ifndef LWS_NO_SERVER |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2177 | /* set up our external listening socket we serve on */ |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2178 | |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2179 | if (info->port) { |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 2180 | int sockfd; |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2181 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2182 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
| 2183 | if (sockfd < 0) { |
Andy Green | f7609e9 | 2013-01-14 13:10:55 +0800 | [diff] [blame] | 2184 | lwsl_err("ERROR opening socket\n"); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2185 | goto bail; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2186 | } |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 2187 | |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 2188 | #ifndef WIN32 |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2189 | /* |
| 2190 | * allow us to restart even if old sockets in TIME_WAIT |
| 2191 | * (REUSEADDR on Unix means, "don't hang on to this |
| 2192 | * address after the listener is closed." On Windows, though, |
| 2193 | * it means "don't keep other processes from binding to |
| 2194 | * this address while we're using it) |
| 2195 | */ |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2196 | setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, |
| 2197 | (const void *)&opt, sizeof(opt)); |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 2198 | #endif |
Andy Green | 6c93955 | 2011-03-08 08:56:57 +0000 | [diff] [blame] | 2199 | |
| 2200 | /* Disable Nagle */ |
| 2201 | opt = 1; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2202 | setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, |
| 2203 | (const void *)&opt, sizeof(opt)); |
Andy Green | 6c93955 | 2011-03-08 08:56:57 +0000 | [diff] [blame] | 2204 | |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 2205 | #if defined(WIN32) || defined(_WIN32) |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 2206 | opt = 0; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2207 | ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt); |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 2208 | #else |
Andy Green | e216071 | 2013-01-28 12:19:10 +0800 | [diff] [blame] | 2209 | fcntl(sockfd, F_SETFL, O_NONBLOCK); |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 2210 | #endif |
Andy Green | e216071 | 2013-01-28 12:19:10 +0800 | [diff] [blame] | 2211 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2212 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
| 2213 | serv_addr.sin_family = AF_INET; |
Joakim Soderberg | 63ff120 | 2013-02-11 17:52:23 +0100 | [diff] [blame] | 2214 | if (info->iface == NULL) |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 2215 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
| 2216 | else |
Andy Green | d1eac60 | 2013-11-09 08:07:38 +0800 | [diff] [blame] | 2217 | if (interface_to_sa(info->iface, &serv_addr, |
| 2218 | sizeof(serv_addr)) < 0) { |
| 2219 | lwsl_err("Unable to find interface %s\n", |
| 2220 | info->iface); |
| 2221 | compatible_close(sockfd); |
| 2222 | goto bail; |
| 2223 | } |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2224 | serv_addr.sin_port = htons(info->port); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2225 | |
| 2226 | n = bind(sockfd, (struct sockaddr *) &serv_addr, |
| 2227 | sizeof(serv_addr)); |
| 2228 | if (n < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2229 | lwsl_err("ERROR on binding to port %d (%d %d)\n", |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2230 | info->port, n, errno); |
Andy Green | 8b2d6f0 | 2013-10-22 06:49:30 +0800 | [diff] [blame] | 2231 | compatible_close(sockfd); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2232 | goto bail; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2233 | } |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2234 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2235 | wsi = (struct libwebsocket *)malloc( |
| 2236 | sizeof(struct libwebsocket)); |
Andy Green | 41c5803 | 2013-01-12 13:21:08 +0800 | [diff] [blame] | 2237 | if (wsi == NULL) { |
| 2238 | lwsl_err("Out of mem\n"); |
Andy Green | 8b2d6f0 | 2013-10-22 06:49:30 +0800 | [diff] [blame] | 2239 | compatible_close(sockfd); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2240 | goto bail; |
Andy Green | 41c5803 | 2013-01-12 13:21:08 +0800 | [diff] [blame] | 2241 | } |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2242 | memset(wsi, 0, sizeof(struct libwebsocket)); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2243 | wsi->sock = sockfd; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 2244 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 2245 | wsi->count_active_extensions = 0; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 2246 | #endif |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2247 | wsi->mode = LWS_CONNMODE_SERVER_LISTENER; |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 2248 | |
| 2249 | insert_wsi_socket_into_fds(context, wsi); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2250 | |
Andy Green | 65b0e91 | 2013-01-16 07:59:47 +0800 | [diff] [blame] | 2251 | context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO; |
| 2252 | context->listen_service_count = 0; |
| 2253 | context->listen_service_fd = sockfd; |
| 2254 | |
Andy Green | a824d18 | 2013-01-15 20:52:29 +0800 | [diff] [blame] | 2255 | listen(sockfd, LWS_SOMAXCONN); |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2256 | lwsl_notice(" Listening on port %d\n", info->port); |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2257 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 2258 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2259 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2260 | /* |
| 2261 | * drop any root privs for this process |
| 2262 | * to listen on port < 1023 we would have needed root, but now we are |
| 2263 | * listening, we don't want the power for anything else |
| 2264 | */ |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 2265 | #if defined(WIN32) || defined(_WIN32) |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2266 | #else |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2267 | if (info->gid != -1) |
| 2268 | if (setgid(info->gid)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2269 | lwsl_warn("setgid: %s\n", strerror(errno)); |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2270 | if (info->uid != -1) |
| 2271 | if (setuid(info->uid)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2272 | lwsl_warn("setuid: %s\n", strerror(errno)); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2273 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2274 | |
Andy Green | 6f520a5 | 2013-01-29 17:57:39 +0800 | [diff] [blame] | 2275 | /* initialize supported protocols */ |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2276 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2277 | for (context->count_protocols = 0; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2278 | info->protocols[context->count_protocols].callback; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2279 | context->count_protocols++) { |
Andy Green | 2d1301e | 2011-05-24 10:14:41 +0100 | [diff] [blame] | 2280 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2281 | lwsl_parser(" Protocol: %s\n", |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2282 | info->protocols[context->count_protocols].name); |
Andy Green | 2d1301e | 2011-05-24 10:14:41 +0100 | [diff] [blame] | 2283 | |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2284 | info->protocols[context->count_protocols].owning_server = |
| 2285 | context; |
| 2286 | info->protocols[context->count_protocols].protocol_index = |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2287 | context->count_protocols; |
Andy Green | a7109e6 | 2013-02-11 12:05:54 +0800 | [diff] [blame] | 2288 | |
| 2289 | /* |
| 2290 | * inform all the protocols that they are doing their one-time |
| 2291 | * initialization if they want to |
| 2292 | */ |
| 2293 | info->protocols[context->count_protocols].callback(context, |
| 2294 | NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2295 | } |
Andy Green | f5bc130 | 2013-01-21 09:09:52 +0800 | [diff] [blame] | 2296 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 2297 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2298 | /* |
| 2299 | * give all extensions a chance to create any per-context |
| 2300 | * allocations they need |
| 2301 | */ |
| 2302 | |
| 2303 | m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2304 | if (info->port) |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2305 | m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2306 | |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2307 | if (info->extensions) { |
| 2308 | ext = info->extensions; |
| 2309 | while (ext->callback) { |
| 2310 | lwsl_ext(" Extension: %s\n", ext->name); |
| 2311 | ext->callback(context, ext, NULL, |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 2312 | (enum libwebsocket_extension_callback_reasons)m, |
| 2313 | NULL, NULL, 0); |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2314 | ext++; |
| 2315 | } |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2316 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 2317 | #endif |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2318 | return context; |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2319 | |
| 2320 | bail: |
| 2321 | libwebsocket_context_destroy(context); |
| 2322 | return NULL; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2323 | } |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2324 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2325 | /** |
shys | b4e800e | 2013-10-24 22:12:03 +0800 | [diff] [blame] | 2326 | * libwebsocket_set_proxy() - Setups proxy to libwebsocket_context. |
| 2327 | * @context: pointer to struct libwebsocket_context you want set proxy to |
| 2328 | * @proxy: pointer to c string containing proxy in format address:port |
| 2329 | * |
| 2330 | * Returns 0 if proxy string was parsed and proxy was setup. |
| 2331 | * Returns -1 if @proxy is NULL or has incorrect format. |
| 2332 | * |
| 2333 | * This is only required if your OS does not provide the http_proxy |
| 2334 | * enviroment variable (eg, OSX) |
| 2335 | * |
| 2336 | * IMPORTANT! You should call this function right after creation of the |
| 2337 | * libwebsocket_context and before call to connect. If you call this |
| 2338 | * function after connect behavior is undefined. |
| 2339 | * This function will override proxy settings made on libwebsocket_context |
| 2340 | * creation with genenv() call. |
| 2341 | */ |
| 2342 | |
| 2343 | LWS_VISIBLE int |
| 2344 | libwebsocket_set_proxy(struct libwebsocket_context *context, const char *proxy) |
| 2345 | { |
| 2346 | char *p; |
| 2347 | |
| 2348 | if (!proxy) |
| 2349 | return -1; |
| 2350 | |
| 2351 | strncpy(context->http_proxy_address, proxy, |
| 2352 | sizeof(context->http_proxy_address) - 1); |
| 2353 | context->http_proxy_address[ |
| 2354 | sizeof(context->http_proxy_address) - 1] = '\0'; |
| 2355 | |
| 2356 | p = strchr(context->http_proxy_address, ':'); |
| 2357 | if (!p) { |
| 2358 | lwsl_err("http_proxy needs to be ads:port\n"); |
| 2359 | |
| 2360 | return -1; |
| 2361 | } |
| 2362 | *p = '\0'; |
| 2363 | context->http_proxy_port = atoi(p + 1); |
| 2364 | |
| 2365 | lwsl_notice(" Proxy %s:%u\n", context->http_proxy_address, |
| 2366 | context->http_proxy_port); |
| 2367 | |
| 2368 | return 0; |
| 2369 | } |
| 2370 | |
| 2371 | /** |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2372 | * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2373 | * connection. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2374 | * @wsi: pointer to struct websocket you want to know the protocol of |
| 2375 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2376 | * |
Andy Green | 6f520a5 | 2013-01-29 17:57:39 +0800 | [diff] [blame] | 2377 | * Some apis can act on all live connections of a given protocol, |
| 2378 | * this is how you can get a pointer to the active protocol if needed. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2379 | */ |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 2380 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 2381 | LWS_VISIBLE const struct libwebsocket_protocols * |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2382 | libwebsockets_get_protocol(struct libwebsocket *wsi) |
| 2383 | { |
| 2384 | return wsi->protocol; |
| 2385 | } |
| 2386 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 2387 | LWS_VISIBLE int |
Andy Green | 82c3d54 | 2011-03-07 21:16:31 +0000 | [diff] [blame] | 2388 | libwebsocket_is_final_fragment(struct libwebsocket *wsi) |
| 2389 | { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 2390 | return wsi->u.ws.final; |
Andy Green | 82c3d54 | 2011-03-07 21:16:31 +0000 | [diff] [blame] | 2391 | } |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 2392 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 2393 | LWS_VISIBLE unsigned char |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 2394 | libwebsocket_get_reserved_bits(struct libwebsocket *wsi) |
| 2395 | { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 2396 | return wsi->u.ws.rsv; |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 2397 | } |
| 2398 | |
Andy Green | 2af4d5b | 2013-02-18 16:30:10 +0800 | [diff] [blame] | 2399 | int |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 2400 | libwebsocket_ensure_user_space(struct libwebsocket *wsi) |
| 2401 | { |
Andy Green | 67d556c | 2013-02-15 22:31:55 +0800 | [diff] [blame] | 2402 | if (!wsi->protocol) |
Andy Green | 2af4d5b | 2013-02-18 16:30:10 +0800 | [diff] [blame] | 2403 | return 1; |
Andy Green | 67d556c | 2013-02-15 22:31:55 +0800 | [diff] [blame] | 2404 | |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 2405 | /* allocate the per-connection user memory (if any) */ |
| 2406 | |
| 2407 | if (wsi->protocol->per_session_data_size && !wsi->user_space) { |
| 2408 | wsi->user_space = malloc( |
| 2409 | wsi->protocol->per_session_data_size); |
| 2410 | if (wsi->user_space == NULL) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2411 | lwsl_err("Out of memory for conn user space\n"); |
Andy Green | 2af4d5b | 2013-02-18 16:30:10 +0800 | [diff] [blame] | 2412 | return 1; |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 2413 | } |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2414 | memset(wsi->user_space, 0, |
| 2415 | wsi->protocol->per_session_data_size); |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 2416 | } |
Andy Green | 2af4d5b | 2013-02-18 16:30:10 +0800 | [diff] [blame] | 2417 | return 0; |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 2418 | } |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2419 | |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 2420 | static void lwsl_emit_stderr(int level, const char *line) |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2421 | { |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 2422 | char buf[300]; |
| 2423 | struct timeval tv; |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 2424 | int n; |
| 2425 | |
| 2426 | gettimeofday(&tv, NULL); |
| 2427 | |
| 2428 | buf[0] = '\0'; |
| 2429 | for (n = 0; n < LLL_COUNT; n++) |
| 2430 | if (level == (1 << n)) { |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 2431 | sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2432 | (int)(tv.tv_usec / 100), log_level_names[n]); |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 2433 | break; |
| 2434 | } |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2435 | |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 2436 | fprintf(stderr, "%s%s", buf, line); |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2437 | } |
| 2438 | |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 2439 | #if defined(WIN32) || defined(_WIN32) |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 2440 | LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line) |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 2441 | { |
| 2442 | lwsl_emit_stderr(level, line); |
| 2443 | } |
| 2444 | #else |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 2445 | LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line) |
Andy Green | c11db20 | 2013-01-19 11:12:16 +0800 | [diff] [blame] | 2446 | { |
| 2447 | int syslog_level = LOG_DEBUG; |
| 2448 | |
| 2449 | switch (level) { |
| 2450 | case LLL_ERR: |
| 2451 | syslog_level = LOG_ERR; |
| 2452 | break; |
| 2453 | case LLL_WARN: |
| 2454 | syslog_level = LOG_WARNING; |
| 2455 | break; |
| 2456 | case LLL_NOTICE: |
| 2457 | syslog_level = LOG_NOTICE; |
| 2458 | break; |
| 2459 | case LLL_INFO: |
| 2460 | syslog_level = LOG_INFO; |
| 2461 | break; |
| 2462 | } |
Edwin van den Oetelaar | f6eeabc | 2013-01-19 20:01:01 +0800 | [diff] [blame] | 2463 | syslog(syslog_level, "%s", line); |
Andy Green | c11db20 | 2013-01-19 11:12:16 +0800 | [diff] [blame] | 2464 | } |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 2465 | #endif |
Andy Green | c11db20 | 2013-01-19 11:12:16 +0800 | [diff] [blame] | 2466 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 2467 | LWS_VISIBLE void _lws_log(int filter, const char *format, ...) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2468 | { |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2469 | char buf[256]; |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2470 | va_list ap; |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2471 | |
| 2472 | if (!(log_level & filter)) |
| 2473 | return; |
| 2474 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2475 | va_start(ap, format); |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2476 | vsnprintf(buf, sizeof(buf), format, ap); |
| 2477 | buf[sizeof(buf) - 1] = '\0'; |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2478 | va_end(ap); |
| 2479 | |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 2480 | lwsl_emit(filter, buf); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | /** |
| 2484 | * lws_set_log_level() - Set the logging bitfield |
| 2485 | * @level: OR together the LLL_ debug contexts you want output from |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2486 | * @log_emit_function: NULL to leave it as it is, or a user-supplied |
| 2487 | * function to perform log string emission instead of |
| 2488 | * the default stderr one. |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2489 | * |
Andy Green | c6511a0 | 2013-02-19 10:01:48 +0800 | [diff] [blame] | 2490 | * log level defaults to "err", "warn" and "notice" contexts enabled and |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2491 | * emission on stderr. |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2492 | */ |
| 2493 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 2494 | LWS_VISIBLE void lws_set_log_level(int level, void (*log_emit_function)(int level, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2495 | const char *line)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2496 | { |
| 2497 | log_level = level; |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2498 | if (log_emit_function) |
| 2499 | lwsl_emit = log_emit_function; |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2500 | } |