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