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