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