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