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