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