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