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