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