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 | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 64 | /* |
| 65 | * In-place str to lower case |
| 66 | */ |
| 67 | |
| 68 | static void |
| 69 | strtolower(char *s) |
| 70 | { |
| 71 | while (*s) { |
| 72 | *s = tolower(*s); |
| 73 | s++; |
| 74 | } |
| 75 | } |
| 76 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 77 | /* file descriptor hash management */ |
| 78 | |
| 79 | struct libwebsocket * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 80 | wsi_from_fd(struct libwebsocket_context *context, int fd) |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 81 | { |
| 82 | int h = LWS_FD_HASH(fd); |
| 83 | int n = 0; |
| 84 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 85 | for (n = 0; n < context->fd_hashtable[h].length; n++) |
| 86 | if (context->fd_hashtable[h].wsi[n]->sock == fd) |
| 87 | return context->fd_hashtable[h].wsi[n]; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 88 | |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | int |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 93 | insert_wsi(struct libwebsocket_context *context, struct libwebsocket *wsi) |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 94 | { |
| 95 | int h = LWS_FD_HASH(wsi->sock); |
| 96 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 97 | if (context->fd_hashtable[h].length == MAX_CLIENTS - 1) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 98 | lwsl_err("hash table overflow\n"); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 99 | return 1; |
| 100 | } |
| 101 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 102 | context->fd_hashtable[h].wsi[context->fd_hashtable[h].length++] = wsi; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | int |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 108 | delete_from_fd(struct libwebsocket_context *context, int fd) |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 109 | { |
| 110 | int h = LWS_FD_HASH(fd); |
| 111 | int n = 0; |
| 112 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 113 | for (n = 0; n < context->fd_hashtable[h].length; n++) |
| 114 | if (context->fd_hashtable[h].wsi[n]->sock == fd) { |
| 115 | while (n < context->fd_hashtable[h].length) { |
| 116 | context->fd_hashtable[h].wsi[n] = |
| 117 | context->fd_hashtable[h].wsi[n + 1]; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 118 | n++; |
| 119 | } |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 120 | context->fd_hashtable[h].length--; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 125 | lwsl_err("Failed to find fd %d requested for " |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 126 | "delete in hashtable\n", fd); |
| 127 | return 1; |
| 128 | } |
| 129 | |
Andy Green | 1f9bf52 | 2011-02-14 21:14:37 +0000 | [diff] [blame] | 130 | #ifdef LWS_OPENSSL_SUPPORT |
| 131 | static void |
| 132 | libwebsockets_decode_ssl_error(void) |
| 133 | { |
| 134 | char buf[256]; |
| 135 | u_long err; |
| 136 | |
| 137 | while ((err = ERR_get_error()) != 0) { |
| 138 | ERR_error_string_n(err, buf, sizeof(buf)); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 139 | lwsl_err("*** %s\n", buf); |
Andy Green | 1f9bf52 | 2011-02-14 21:14:37 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | #endif |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 143 | |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 144 | |
| 145 | static int |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 146 | 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] | 147 | { |
| 148 | int rc = -1; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 149 | #ifdef WIN32 |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 150 | /* TODO */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 151 | #else |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 152 | struct ifaddrs *ifr; |
| 153 | struct ifaddrs *ifc; |
| 154 | struct sockaddr_in *sin; |
| 155 | |
| 156 | getifaddrs(&ifr); |
| 157 | for (ifc = ifr; ifc != NULL; ifc = ifc->ifa_next) { |
| 158 | if (strcmp(ifc->ifa_name, ifname)) |
| 159 | continue; |
| 160 | if (ifc->ifa_addr == NULL) |
| 161 | continue; |
| 162 | sin = (struct sockaddr_in *)ifc->ifa_addr; |
| 163 | if (sin->sin_family != AF_INET) |
| 164 | continue; |
| 165 | memcpy(addr, sin, addrlen); |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 166 | rc = 0; |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | freeifaddrs(ifr); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 170 | #endif |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 171 | return rc; |
| 172 | } |
| 173 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 174 | void |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 175 | libwebsocket_close_and_free_session(struct libwebsocket_context *context, |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 176 | struct libwebsocket *wsi, enum lws_close_status reason) |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 177 | { |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 178 | int n; |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 179 | int old_state; |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 180 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 + |
| 181 | LWS_SEND_BUFFER_POST_PADDING]; |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 182 | int ret; |
| 183 | int m; |
| 184 | struct lws_tokens eff_buf; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 185 | struct libwebsocket_extension *ext; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 186 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 187 | if (!wsi) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 188 | return; |
| 189 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 190 | old_state = wsi->state; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 191 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 192 | if (old_state == WSI_STATE_DEAD_SOCKET) |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 193 | return; |
| 194 | |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 195 | wsi->close_reason = reason; |
| 196 | |
| 197 | /* |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 198 | * are his extensions okay with him closing? Eg he might be a mux |
| 199 | * parent and just his ch1 aspect is closing? |
| 200 | */ |
| 201 | |
| 202 | |
| 203 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 204 | if (!wsi->active_extensions[n]->callback) |
| 205 | continue; |
| 206 | |
| 207 | m = wsi->active_extensions[n]->callback(context, |
| 208 | wsi->active_extensions[n], wsi, |
| 209 | LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE, |
| 210 | wsi->active_extensions_user[n], NULL, 0); |
| 211 | |
| 212 | /* |
| 213 | * if somebody vetoed actually closing him at this time.... |
| 214 | * up to the extension to track the attempted close, let's |
| 215 | * just bail |
| 216 | */ |
| 217 | |
| 218 | if (m) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 219 | lwsl_ext("extension vetoed close\n"); |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 220 | return; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | |
| 226 | /* |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 227 | * flush any tx pending from extensions, since we may send close packet |
| 228 | * if there are problems with send, just nuke the connection |
| 229 | */ |
| 230 | |
| 231 | ret = 1; |
| 232 | while (ret == 1) { |
| 233 | |
| 234 | /* default to nobody has more to spill */ |
| 235 | |
| 236 | ret = 0; |
| 237 | eff_buf.token = NULL; |
| 238 | eff_buf.token_len = 0; |
| 239 | |
| 240 | /* show every extension the new incoming data */ |
| 241 | |
| 242 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 243 | m = wsi->active_extensions[n]->callback( |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 244 | wsi->protocol->owning_server, |
| 245 | wsi->active_extensions[n], wsi, |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 246 | LWS_EXT_CALLBACK_FLUSH_PENDING_TX, |
| 247 | wsi->active_extensions_user[n], &eff_buf, 0); |
| 248 | if (m < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 249 | lwsl_ext("Extension reports fatal error\n"); |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 250 | goto just_kill_connection; |
| 251 | } |
| 252 | if (m) |
| 253 | /* |
| 254 | * at least one extension told us he has more |
| 255 | * to spill, so we will go around again after |
| 256 | */ |
| 257 | ret = 1; |
| 258 | } |
| 259 | |
| 260 | /* assuming they left us something to send, send it */ |
| 261 | |
| 262 | if (eff_buf.token_len) |
| 263 | if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token, |
| 264 | eff_buf.token_len)) |
| 265 | goto just_kill_connection; |
| 266 | } |
| 267 | |
| 268 | /* |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 269 | * signal we are closing, libsocket_write will |
| 270 | * add any necessary version-specific stuff. If the write fails, |
| 271 | * no worries we are closing anyway. If we didn't initiate this |
| 272 | * close, then our state has been changed to |
| 273 | * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this. |
| 274 | * |
| 275 | * Likewise if it's a second call to close this connection after we |
| 276 | * sent the close indication to the peer already, we are in state |
| 277 | * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time. |
| 278 | */ |
| 279 | |
| 280 | if (old_state == WSI_STATE_ESTABLISHED && |
| 281 | reason != LWS_CLOSE_STATUS_NOSTATUS) { |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 282 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 283 | lwsl_debug("sending close indication...\n"); |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 284 | |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 285 | n = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], |
| 286 | 0, LWS_WRITE_CLOSE); |
| 287 | if (!n) { |
| 288 | /* |
| 289 | * we have sent a nice protocol level indication we |
| 290 | * now wish to close, we should not send anything more |
| 291 | */ |
| 292 | |
| 293 | wsi->state = WSI_STATE_AWAITING_CLOSE_ACK; |
| 294 | |
| 295 | /* and we should wait for a reply for a bit */ |
| 296 | |
| 297 | libwebsocket_set_timeout(wsi, |
David Galeano | c9f1ff8 | 2013-01-09 18:01:23 +0800 | [diff] [blame] | 298 | PENDING_TIMEOUT_CLOSE_ACK, AWAITING_TIMEOUT); |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 299 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 300 | lwsl_debug("sent close indication, awaiting ack\n"); |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 301 | |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | /* else, the send failed and we should just hang up */ |
| 306 | } |
| 307 | |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 308 | just_kill_connection: |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 309 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 310 | lwsl_debug("libwebsocket_close_and_free_session: just_kill_connection\n"); |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 311 | |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 312 | /* |
| 313 | * we won't be servicing or receiving anything further from this guy |
| 314 | * remove this fd from wsi mapping hashtable |
| 315 | */ |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 316 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 317 | if (wsi->sock) |
| 318 | delete_from_fd(context, wsi->sock); |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 319 | |
| 320 | /* delete it from the internal poll list if still present */ |
| 321 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 322 | for (n = 0; n < context->fds_count; n++) { |
| 323 | if (context->fds[n].fd != wsi->sock) |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 324 | continue; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 325 | while (n < context->fds_count - 1) { |
| 326 | context->fds[n] = context->fds[n + 1]; |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 327 | n++; |
| 328 | } |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 329 | context->fds_count--; |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 330 | /* we only have to deal with one */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 331 | n = context->fds_count; |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /* remove also from external POLL support via protocol 0 */ |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 335 | if (wsi->sock) |
| 336 | context->protocols[0].callback(context, wsi, |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 337 | LWS_CALLBACK_DEL_POLL_FD, (void *)(long)wsi->sock, NULL, 0); |
| 338 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 339 | wsi->state = WSI_STATE_DEAD_SOCKET; |
| 340 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 341 | /* tell the user it's all over for this guy */ |
| 342 | |
Andy Green | d430273 | 2011-02-28 07:45:29 +0000 | [diff] [blame] | 343 | if (wsi->protocol && wsi->protocol->callback && |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 344 | ((old_state == WSI_STATE_ESTABLISHED) || |
| 345 | (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) || |
| 346 | (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 347 | lwsl_debug("calling back CLOSED\n"); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 348 | wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED, |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 349 | wsi->user_space, NULL, 0); |
Andy Green | cc01247 | 2011-11-07 19:53:23 +0800 | [diff] [blame] | 350 | } else |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 351 | lwsl_debug("not calling back closed, old_state=%d\n", old_state); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 352 | |
Andy Green | ef660a9 | 2011-03-06 10:29:38 +0000 | [diff] [blame] | 353 | /* deallocate any active extension contexts */ |
| 354 | |
| 355 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 356 | if (!wsi->active_extensions[n]->callback) |
| 357 | continue; |
| 358 | |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 359 | wsi->active_extensions[n]->callback(context, |
| 360 | wsi->active_extensions[n], wsi, |
| 361 | LWS_EXT_CALLBACK_DESTROY, |
| 362 | wsi->active_extensions_user[n], NULL, 0); |
Andy Green | ef660a9 | 2011-03-06 10:29:38 +0000 | [diff] [blame] | 363 | |
| 364 | free(wsi->active_extensions_user[n]); |
| 365 | } |
| 366 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 367 | /* |
| 368 | * inform all extensions in case they tracked this guy out of band |
| 369 | * even though not active on him specifically |
| 370 | */ |
| 371 | |
| 372 | ext = context->extensions; |
| 373 | while (ext && ext->callback) { |
| 374 | ext->callback(context, ext, wsi, |
| 375 | LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING, |
| 376 | NULL, NULL, 0); |
| 377 | ext++; |
| 378 | } |
| 379 | |
Andy Green | ef660a9 | 2011-03-06 10:29:38 +0000 | [diff] [blame] | 380 | /* free up his parsing allocations */ |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 381 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 382 | for (n = 0; n < WSI_TOKEN_COUNT; n++) |
| 383 | if (wsi->utf8_token[n].token) |
| 384 | free(wsi->utf8_token[n].token); |
| 385 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 386 | if (wsi->c_address) |
| 387 | free(wsi->c_address); |
| 388 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 389 | /* lwsl_info("closing fd=%d\n", wsi->sock); */ |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 390 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 391 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 392 | if (wsi->ssl) { |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 393 | n = SSL_get_fd(wsi->ssl); |
| 394 | SSL_shutdown(wsi->ssl); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 395 | #ifdef WIN32 |
| 396 | closesocket(n); |
| 397 | #else |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 398 | close(n); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 399 | #endif |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 400 | SSL_free(wsi->ssl); |
| 401 | } else { |
| 402 | #endif |
| 403 | shutdown(wsi->sock, SHUT_RDWR); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 404 | #ifdef WIN32 |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 405 | if (wsi->sock) |
| 406 | closesocket(wsi->sock); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 407 | #else |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 408 | if (wsi->sock) |
| 409 | close(wsi->sock); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 410 | #endif |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 411 | #ifdef LWS_OPENSSL_SUPPORT |
| 412 | } |
| 413 | #endif |
David Brooks | 2c60d95 | 2012-04-20 12:19:01 +0800 | [diff] [blame] | 414 | 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] | 415 | free(wsi->user_space); |
| 416 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 417 | free(wsi); |
| 418 | } |
| 419 | |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 420 | /** |
Andy Green | f7ee549 | 2011-02-13 09:04:21 +0000 | [diff] [blame] | 421 | * libwebsockets_hangup_on_client() - Server calls to terminate client |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 422 | * connection |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 423 | * @context: libwebsockets context |
Andy Green | f7ee549 | 2011-02-13 09:04:21 +0000 | [diff] [blame] | 424 | * @fd: Connection socket descriptor |
| 425 | */ |
| 426 | |
| 427 | void |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 428 | libwebsockets_hangup_on_client(struct libwebsocket_context *context, int fd) |
Andy Green | f7ee549 | 2011-02-13 09:04:21 +0000 | [diff] [blame] | 429 | { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 430 | struct libwebsocket *wsi = wsi_from_fd(context, fd); |
Andy Green | f7ee549 | 2011-02-13 09:04:21 +0000 | [diff] [blame] | 431 | |
| 432 | if (wsi == NULL) |
| 433 | return; |
| 434 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 435 | libwebsocket_close_and_free_session(context, wsi, |
Andy Green | 6da560c | 2011-02-26 11:06:27 +0000 | [diff] [blame] | 436 | LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | f7ee549 | 2011-02-13 09:04:21 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | |
| 440 | /** |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 441 | * libwebsockets_get_peer_addresses() - Get client address information |
| 442 | * @fd: Connection socket descriptor |
| 443 | * @name: Buffer to take client address name |
| 444 | * @name_len: Length of client address name buffer |
| 445 | * @rip: Buffer to take client address IP qotted quad |
| 446 | * @rip_len: Length of client address IP buffer |
| 447 | * |
| 448 | * 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] | 449 | * the client connected with socket descriptor @fd. Names may be |
| 450 | * truncated if there is not enough room. If either cannot be |
| 451 | * determined, they will be returned as valid zero-length strings. |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 452 | */ |
| 453 | |
| 454 | void |
| 455 | libwebsockets_get_peer_addresses(int fd, char *name, int name_len, |
| 456 | char *rip, int rip_len) |
| 457 | { |
| 458 | unsigned int len; |
| 459 | struct sockaddr_in sin; |
| 460 | struct hostent *host; |
| 461 | struct hostent *host1; |
| 462 | char ip[128]; |
Andy Green | f92def7 | 2011-03-09 15:02:20 +0000 | [diff] [blame] | 463 | unsigned char *p; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 464 | int n; |
David Galeano | cb19368 | 2013-01-09 15:29:00 +0800 | [diff] [blame] | 465 | #ifdef AF_LOCAL |
| 466 | struct sockaddr_un *un; |
| 467 | #endif |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 468 | |
| 469 | rip[0] = '\0'; |
| 470 | name[0] = '\0'; |
| 471 | |
| 472 | len = sizeof sin; |
| 473 | if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) { |
| 474 | perror("getpeername"); |
| 475 | return; |
| 476 | } |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 477 | |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 478 | host = gethostbyaddr((char *) &sin.sin_addr, sizeof sin.sin_addr, |
| 479 | AF_INET); |
| 480 | if (host == NULL) { |
| 481 | perror("gethostbyaddr"); |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | strncpy(name, host->h_name, name_len); |
| 486 | name[name_len - 1] = '\0'; |
| 487 | |
| 488 | host1 = gethostbyname(host->h_name); |
| 489 | if (host1 == NULL) |
| 490 | return; |
Andy Green | f92def7 | 2011-03-09 15:02:20 +0000 | [diff] [blame] | 491 | p = (unsigned char *)host1; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 492 | n = 0; |
| 493 | while (p != NULL) { |
Andy Green | f92def7 | 2011-03-09 15:02:20 +0000 | [diff] [blame] | 494 | p = (unsigned char *)host1->h_addr_list[n++]; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 495 | if (p == NULL) |
| 496 | continue; |
Peter Hinz | bb45a90 | 2011-03-10 18:14:01 +0000 | [diff] [blame] | 497 | if ((host1->h_addrtype != AF_INET) |
| 498 | #ifdef AF_LOCAL |
| 499 | && (host1->h_addrtype != AF_LOCAL) |
| 500 | #endif |
| 501 | ) |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 502 | continue; |
| 503 | |
Andy Green | 7627af5 | 2011-03-09 15:13:52 +0000 | [diff] [blame] | 504 | if (host1->h_addrtype == AF_INET) |
| 505 | 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] | 506 | #ifdef AF_LOCAL |
Andy Green | 7627af5 | 2011-03-09 15:13:52 +0000 | [diff] [blame] | 507 | else { |
| 508 | un = (struct sockaddr_un *)p; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 509 | strncpy(ip, un->sun_path, sizeof(ip) - 1); |
Andy Green | 7627af5 | 2011-03-09 15:13:52 +0000 | [diff] [blame] | 510 | ip[sizeof(ip) - 1] = '\0'; |
| 511 | } |
Peter Hinz | bb45a90 | 2011-03-10 18:14:01 +0000 | [diff] [blame] | 512 | #endif |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 513 | p = NULL; |
| 514 | strncpy(rip, ip, rip_len); |
| 515 | rip[rip_len - 1] = '\0'; |
| 516 | } |
| 517 | } |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 518 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 519 | int libwebsockets_get_random(struct libwebsocket_context *context, |
| 520 | void *buf, int len) |
| 521 | { |
| 522 | int n; |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 523 | char *p = (char *)buf; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 524 | |
| 525 | #ifdef WIN32 |
| 526 | for (n = 0; n < len; n++) |
| 527 | p[n] = (unsigned char)rand(); |
| 528 | #else |
| 529 | n = read(context->fd_random, p, len); |
| 530 | #endif |
| 531 | |
| 532 | return n; |
| 533 | } |
| 534 | |
Andy Green | 2836c64 | 2011-03-07 20:47:41 +0000 | [diff] [blame] | 535 | unsigned char * |
| 536 | libwebsockets_SHA1(const unsigned char *d, size_t n, unsigned char *md) |
| 537 | { |
| 538 | return SHA1(d, n, md); |
| 539 | } |
| 540 | |
Andy Green | eeaacb3 | 2011-03-01 20:44:24 +0000 | [diff] [blame] | 541 | void libwebsockets_00_spaceout(char *key, int spaces, int seed) |
| 542 | { |
| 543 | char *p; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 544 | |
Andy Green | eeaacb3 | 2011-03-01 20:44:24 +0000 | [diff] [blame] | 545 | key++; |
| 546 | while (spaces--) { |
| 547 | if (*key && (seed & 1)) |
| 548 | key++; |
| 549 | seed >>= 1; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 550 | |
Andy Green | eeaacb3 | 2011-03-01 20:44:24 +0000 | [diff] [blame] | 551 | p = key + strlen(key); |
| 552 | while (p >= key) { |
| 553 | p[1] = p[0]; |
| 554 | p--; |
| 555 | } |
| 556 | *key++ = ' '; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | void libwebsockets_00_spam(char *key, int count, int seed) |
| 561 | { |
| 562 | char *p; |
| 563 | |
| 564 | key++; |
| 565 | while (count--) { |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 566 | |
Andy Green | eeaacb3 | 2011-03-01 20:44:24 +0000 | [diff] [blame] | 567 | if (*key && (seed & 1)) |
| 568 | key++; |
| 569 | seed >>= 1; |
| 570 | |
| 571 | p = key + strlen(key); |
| 572 | while (p >= key) { |
| 573 | p[1] = p[0]; |
| 574 | p--; |
| 575 | } |
| 576 | *key++ = 0x21 + ((seed & 0xffff) % 15); |
| 577 | /* 4 would use it up too fast.. not like it matters */ |
| 578 | seed >>= 1; |
| 579 | } |
| 580 | } |
| 581 | |
Andy Green | 95a7b5d | 2011-03-06 10:29:39 +0000 | [diff] [blame] | 582 | int lws_send_pipe_choked(struct libwebsocket *wsi) |
| 583 | { |
| 584 | struct pollfd fds; |
| 585 | |
| 586 | fds.fd = wsi->sock; |
| 587 | fds.events = POLLOUT; |
| 588 | fds.revents = 0; |
| 589 | |
| 590 | if (poll(&fds, 1, 0) != 1) |
| 591 | return 1; |
| 592 | |
| 593 | if ((fds.revents & POLLOUT) == 0) |
| 594 | return 1; |
| 595 | |
| 596 | /* okay to send another packet without blocking */ |
| 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 601 | int |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 602 | lws_handle_POLLOUT_event(struct libwebsocket_context *context, |
| 603 | struct libwebsocket *wsi, struct pollfd *pollfd) |
| 604 | { |
| 605 | struct lws_tokens eff_buf; |
| 606 | int n; |
| 607 | int ret; |
| 608 | int m; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 609 | int handled = 0; |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 610 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 611 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 612 | if (!wsi->active_extensions[n]->callback) |
| 613 | continue; |
| 614 | |
| 615 | m = wsi->active_extensions[n]->callback(context, |
| 616 | wsi->active_extensions[n], wsi, |
| 617 | LWS_EXT_CALLBACK_IS_WRITEABLE, |
| 618 | wsi->active_extensions_user[n], NULL, 0); |
| 619 | if (m > handled) |
| 620 | handled = m; |
| 621 | } |
| 622 | |
| 623 | if (handled == 1) |
| 624 | goto notify_action; |
| 625 | |
| 626 | if (!wsi->extension_data_pending || handled == 2) |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 627 | goto user_service; |
| 628 | |
| 629 | /* |
| 630 | * check in on the active extensions, see if they |
| 631 | * had pending stuff to spill... they need to get the |
| 632 | * first look-in otherwise sequence will be disordered |
| 633 | * |
| 634 | * NULL, zero-length eff_buf means just spill pending |
| 635 | */ |
| 636 | |
| 637 | ret = 1; |
| 638 | while (ret == 1) { |
| 639 | |
| 640 | /* default to nobody has more to spill */ |
| 641 | |
| 642 | ret = 0; |
| 643 | eff_buf.token = NULL; |
| 644 | eff_buf.token_len = 0; |
| 645 | |
| 646 | /* give every extension a chance to spill */ |
| 647 | |
| 648 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 649 | m = wsi->active_extensions[n]->callback( |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 650 | wsi->protocol->owning_server, |
| 651 | wsi->active_extensions[n], wsi, |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 652 | LWS_EXT_CALLBACK_PACKET_TX_PRESEND, |
| 653 | wsi->active_extensions_user[n], &eff_buf, 0); |
| 654 | if (m < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 655 | lwsl_err("ext reports fatal error\n"); |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 656 | return -1; |
| 657 | } |
| 658 | if (m) |
| 659 | /* |
| 660 | * at least one extension told us he has more |
| 661 | * to spill, so we will go around again after |
| 662 | */ |
| 663 | ret = 1; |
| 664 | } |
| 665 | |
| 666 | /* assuming they gave us something to send, send it */ |
| 667 | |
| 668 | if (eff_buf.token_len) { |
| 669 | if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token, |
| 670 | eff_buf.token_len)) |
| 671 | return -1; |
| 672 | } else |
| 673 | continue; |
| 674 | |
| 675 | /* no extension has more to spill */ |
| 676 | |
| 677 | if (!ret) |
| 678 | continue; |
| 679 | |
| 680 | /* |
| 681 | * There's more to spill from an extension, but we just sent |
| 682 | * something... did that leave the pipe choked? |
| 683 | */ |
| 684 | |
| 685 | if (!lws_send_pipe_choked(wsi)) |
| 686 | /* no we could add more */ |
| 687 | continue; |
| 688 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 689 | lwsl_info("choked in POLLOUT service\n"); |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 690 | |
| 691 | /* |
| 692 | * Yes, he's choked. Leave the POLLOUT masked on so we will |
| 693 | * come back here when he is unchoked. Don't call the user |
| 694 | * callback to enforce ordering of spilling, he'll get called |
| 695 | * when we come back here and there's nothing more to spill. |
| 696 | */ |
| 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | wsi->extension_data_pending = 0; |
| 702 | |
| 703 | user_service: |
| 704 | /* one shot */ |
| 705 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 706 | if (pollfd) { |
| 707 | pollfd->events &= ~POLLOUT; |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 708 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 709 | /* external POLL support via protocol 0 */ |
| 710 | context->protocols[0].callback(context, wsi, |
| 711 | LWS_CALLBACK_CLEAR_MODE_POLL_FD, |
| 712 | (void *)(long)wsi->sock, NULL, POLLOUT); |
| 713 | } |
| 714 | |
| 715 | notify_action: |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 716 | |
Andy Green | 9e4c2b6 | 2011-03-07 20:47:39 +0000 | [diff] [blame] | 717 | if (wsi->mode == LWS_CONNMODE_WS_CLIENT) |
| 718 | n = LWS_CALLBACK_CLIENT_WRITEABLE; |
| 719 | else |
| 720 | n = LWS_CALLBACK_SERVER_WRITEABLE; |
| 721 | |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 722 | 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] | 723 | |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | |
| 728 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 729 | void |
| 730 | libwebsocket_service_timeout_check(struct libwebsocket_context *context, |
| 731 | struct libwebsocket *wsi, unsigned int sec) |
| 732 | { |
| 733 | int n; |
| 734 | |
| 735 | /* |
| 736 | * if extensions want in on it (eg, we are a mux parent) |
| 737 | * give them a chance to service child timeouts |
| 738 | */ |
| 739 | |
| 740 | for (n = 0; n < wsi->count_active_extensions; n++) |
| 741 | wsi->active_extensions[n]->callback( |
| 742 | context, wsi->active_extensions[n], |
| 743 | wsi, LWS_EXT_CALLBACK_1HZ, |
| 744 | wsi->active_extensions_user[n], NULL, sec); |
| 745 | |
| 746 | if (!wsi->pending_timeout) |
| 747 | return; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 748 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 749 | /* |
| 750 | * if we went beyond the allowed time, kill the |
| 751 | * connection |
| 752 | */ |
| 753 | |
| 754 | if (sec > wsi->pending_timeout_limit) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 755 | lwsl_info("TIMEDOUT WAITING\n"); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 756 | libwebsocket_close_and_free_session(context, |
| 757 | wsi, LWS_CLOSE_STATUS_NOSTATUS); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | struct libwebsocket * |
| 762 | libwebsocket_create_new_server_wsi(struct libwebsocket_context *context) |
| 763 | { |
| 764 | struct libwebsocket *new_wsi; |
| 765 | int n; |
| 766 | |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 767 | new_wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket)); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 768 | if (new_wsi == NULL) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 769 | lwsl_err("Out of memory for new connection\n"); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 770 | return NULL; |
| 771 | } |
| 772 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 773 | memset(new_wsi, 0, sizeof(struct libwebsocket)); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 774 | new_wsi->count_active_extensions = 0; |
| 775 | new_wsi->pending_timeout = NO_PENDING_TIMEOUT; |
| 776 | |
| 777 | /* intialize the instance struct */ |
| 778 | |
| 779 | new_wsi->state = WSI_STATE_HTTP; |
| 780 | new_wsi->name_buffer_pos = 0; |
| 781 | new_wsi->mode = LWS_CONNMODE_WS_SERVING; |
| 782 | |
| 783 | for (n = 0; n < WSI_TOKEN_COUNT; n++) { |
| 784 | new_wsi->utf8_token[n].token = NULL; |
| 785 | new_wsi->utf8_token[n].token_len = 0; |
| 786 | } |
| 787 | |
| 788 | /* |
| 789 | * these can only be set once the protocol is known |
| 790 | * we set an unestablished connection's protocol pointer |
| 791 | * to the start of the supported list, so it can look |
| 792 | * for matching ones during the handshake |
| 793 | */ |
| 794 | new_wsi->protocol = context->protocols; |
| 795 | new_wsi->user_space = NULL; |
| 796 | |
| 797 | /* |
| 798 | * Default protocol is 76 / 00 |
| 799 | * After 76, there's a header specified to inform which |
| 800 | * draft the client wants, when that's seen we modify |
| 801 | * the individual connection's spec revision accordingly |
| 802 | */ |
| 803 | new_wsi->ietf_spec_revision = 0; |
| 804 | |
| 805 | return new_wsi; |
| 806 | } |
| 807 | |
| 808 | char * |
| 809 | libwebsockets_generate_client_handshake(struct libwebsocket_context *context, |
| 810 | struct libwebsocket *wsi, char *pkt) |
| 811 | { |
| 812 | char hash[20]; |
| 813 | char *p = pkt; |
| 814 | int n; |
| 815 | struct libwebsocket_extension *ext; |
Andy Green | 0922650 | 2011-05-28 10:19:19 +0100 | [diff] [blame] | 816 | struct libwebsocket_extension *ext1; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 817 | int ext_count = 0; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 818 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 + |
| 819 | MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING]; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 820 | static const char magic_websocket_guid[] = |
| 821 | "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
| 822 | |
| 823 | /* |
| 824 | * create the random key |
| 825 | */ |
| 826 | |
| 827 | n = libwebsockets_get_random(context, hash, 16); |
| 828 | if (n != 16) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 829 | lwsl_err("Unable to read from random dev %s\n", |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 830 | SYSTEM_RANDOM_FILEPATH); |
| 831 | free(wsi->c_path); |
| 832 | free(wsi->c_host); |
| 833 | if (wsi->c_origin) |
| 834 | free(wsi->c_origin); |
| 835 | if (wsi->c_protocol) |
| 836 | free(wsi->c_protocol); |
| 837 | libwebsocket_close_and_free_session(context, wsi, |
| 838 | LWS_CLOSE_STATUS_NOSTATUS); |
| 839 | return NULL; |
| 840 | } |
| 841 | |
| 842 | lws_b64_encode_string(hash, 16, wsi->key_b64, |
| 843 | sizeof wsi->key_b64); |
| 844 | |
| 845 | /* |
| 846 | * 00 example client handshake |
| 847 | * |
| 848 | * GET /socket.io/websocket HTTP/1.1 |
| 849 | * Upgrade: WebSocket |
| 850 | * Connection: Upgrade |
| 851 | * Host: 127.0.0.1:9999 |
| 852 | * Origin: http://127.0.0.1 |
| 853 | * Sec-WebSocket-Key1: 1 0 2#0W 9 89 7 92 ^ |
| 854 | * Sec-WebSocket-Key2: 7 7Y 4328 B2v[8(z1 |
| 855 | * Cookie: socketio=websocket |
| 856 | * |
| 857 | * (Á®Ä0¶†≥ |
| 858 | * |
| 859 | * 04 example client handshake |
| 860 | * |
| 861 | * GET /chat HTTP/1.1 |
| 862 | * Host: server.example.com |
| 863 | * Upgrade: websocket |
| 864 | * Connection: Upgrade |
| 865 | * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== |
| 866 | * Sec-WebSocket-Origin: http://example.com |
| 867 | * Sec-WebSocket-Protocol: chat, superchat |
| 868 | * Sec-WebSocket-Version: 4 |
| 869 | */ |
| 870 | |
| 871 | p += sprintf(p, "GET %s HTTP/1.1\x0d\x0a", wsi->c_path); |
| 872 | |
David Galeano | 4fbc40c | 2013-01-10 10:26:05 +0800 | [diff] [blame] | 873 | p += sprintf(p, "Pragma: no-cache\x0d\x0a" |
| 874 | "Cache-Control: no-cache\x0d\x0a"); |
| 875 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 876 | if (wsi->ietf_spec_revision == 0) { |
| 877 | unsigned char spaces_1, spaces_2; |
| 878 | unsigned int max_1, max_2; |
| 879 | unsigned int num_1, num_2; |
| 880 | unsigned long product_1, product_2; |
| 881 | char key_1[40]; |
| 882 | char key_2[40]; |
| 883 | unsigned int seed; |
| 884 | unsigned int count; |
| 885 | char challenge[16]; |
| 886 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 887 | libwebsockets_get_random(context, &spaces_1, sizeof(char)); |
| 888 | libwebsockets_get_random(context, &spaces_2, sizeof(char)); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 889 | |
| 890 | spaces_1 = (spaces_1 % 12) + 1; |
| 891 | spaces_2 = (spaces_2 % 12) + 1; |
| 892 | |
| 893 | max_1 = 4294967295 / spaces_1; |
| 894 | max_2 = 4294967295 / spaces_2; |
| 895 | |
| 896 | libwebsockets_get_random(context, &num_1, sizeof(int)); |
| 897 | libwebsockets_get_random(context, &num_2, sizeof(int)); |
| 898 | |
| 899 | num_1 = (num_1 % max_1); |
| 900 | num_2 = (num_2 % max_2); |
| 901 | |
| 902 | challenge[0] = num_1 >> 24; |
| 903 | challenge[1] = num_1 >> 16; |
| 904 | challenge[2] = num_1 >> 8; |
| 905 | challenge[3] = num_1; |
| 906 | challenge[4] = num_2 >> 24; |
| 907 | challenge[5] = num_2 >> 16; |
| 908 | challenge[6] = num_2 >> 8; |
| 909 | challenge[7] = num_2; |
| 910 | |
| 911 | product_1 = num_1 * spaces_1; |
| 912 | product_2 = num_2 * spaces_2; |
| 913 | |
| 914 | sprintf(key_1, "%lu", product_1); |
| 915 | sprintf(key_2, "%lu", product_2); |
| 916 | |
| 917 | libwebsockets_get_random(context, &seed, sizeof(int)); |
| 918 | libwebsockets_get_random(context, &count, sizeof(int)); |
| 919 | |
| 920 | libwebsockets_00_spam(key_1, (count % 12) + 1, seed); |
| 921 | |
| 922 | libwebsockets_get_random(context, &seed, sizeof(int)); |
| 923 | libwebsockets_get_random(context, &count, sizeof(int)); |
| 924 | |
| 925 | libwebsockets_00_spam(key_2, (count % 12) + 1, seed); |
| 926 | |
| 927 | libwebsockets_get_random(context, &seed, sizeof(int)); |
| 928 | |
| 929 | libwebsockets_00_spaceout(key_1, spaces_1, seed); |
| 930 | libwebsockets_00_spaceout(key_2, spaces_2, seed >> 16); |
| 931 | |
| 932 | p += sprintf(p, "Upgrade: WebSocket\x0d\x0a" |
| 933 | "Connection: Upgrade\x0d\x0aHost: %s\x0d\x0a", |
| 934 | wsi->c_host); |
| 935 | if (wsi->c_origin) |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 936 | p += sprintf(p, "Origin: %s\x0d\x0a", wsi->c_origin); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 937 | |
| 938 | if (wsi->c_protocol) |
| 939 | p += sprintf(p, "Sec-WebSocket-Protocol: %s" |
| 940 | "\x0d\x0a", wsi->c_protocol); |
| 941 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 942 | p += sprintf(p, "Sec-WebSocket-Key1: %s\x0d\x0a", key_1); |
| 943 | p += sprintf(p, "Sec-WebSocket-Key2: %s\x0d\x0a", key_2); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 944 | |
| 945 | /* give userland a chance to append, eg, cookies */ |
| 946 | |
| 947 | context->protocols[0].callback(context, wsi, |
| 948 | LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER, |
| 949 | NULL, &p, (pkt + sizeof(pkt)) - p - 12); |
| 950 | |
| 951 | p += sprintf(p, "\x0d\x0a"); |
| 952 | |
| 953 | if (libwebsockets_get_random(context, p, 8) != 8) |
| 954 | return NULL; |
| 955 | memcpy(&challenge[8], p, 8); |
| 956 | p += 8; |
| 957 | |
| 958 | /* precompute what we want to see from the server */ |
| 959 | |
| 960 | MD5((unsigned char *)challenge, 16, |
| 961 | (unsigned char *)wsi->initial_handshake_hash_base64); |
| 962 | |
| 963 | goto issue_hdr; |
| 964 | } |
| 965 | |
| 966 | p += sprintf(p, "Host: %s\x0d\x0a", wsi->c_host); |
David Galeano | 4fbc40c | 2013-01-10 10:26:05 +0800 | [diff] [blame] | 967 | p += sprintf(p, "Upgrade: websocket\x0d\x0a" |
| 968 | "Connection: Upgrade\x0d\x0a" |
| 969 | "Sec-WebSocket-Key: "); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 970 | strcpy(p, wsi->key_b64); |
| 971 | p += strlen(wsi->key_b64); |
| 972 | p += sprintf(p, "\x0d\x0a"); |
David Galeano | aa0bc86 | 2013-01-09 15:31:46 +0800 | [diff] [blame] | 973 | if (wsi->c_origin) { |
| 974 | if (wsi->ietf_spec_revision == 13) { |
| 975 | p += sprintf(p, "Origin: %s\x0d\x0a", |
| 976 | wsi->c_origin); |
| 977 | } |
| 978 | else { |
David Galeano | cb19368 | 2013-01-09 15:29:00 +0800 | [diff] [blame] | 979 | p += sprintf(p, "Sec-WebSocket-Origin: %s\x0d\x0a", |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 980 | wsi->c_origin); |
David Galeano | aa0bc86 | 2013-01-09 15:31:46 +0800 | [diff] [blame] | 981 | } |
| 982 | } |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 983 | if (wsi->c_protocol) |
| 984 | p += sprintf(p, "Sec-WebSocket-Protocol: %s\x0d\x0a", |
| 985 | wsi->c_protocol); |
| 986 | |
| 987 | /* tell the server what extensions we could support */ |
| 988 | |
| 989 | p += sprintf(p, "Sec-WebSocket-Extensions: "); |
| 990 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 991 | ext = context->extensions; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 992 | while (ext && ext->callback) { |
| 993 | |
| 994 | n = 0; |
Andy Green | 0922650 | 2011-05-28 10:19:19 +0100 | [diff] [blame] | 995 | ext1 = context->extensions; |
Andy Green | 0922650 | 2011-05-28 10:19:19 +0100 | [diff] [blame] | 996 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 997 | while (ext1 && ext1->callback) { |
Andy Green | 0922650 | 2011-05-28 10:19:19 +0100 | [diff] [blame] | 998 | n |= ext1->callback(context, ext1, wsi, |
| 999 | LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION, |
| 1000 | NULL, (char *)ext->name, 0); |
| 1001 | |
| 1002 | ext1++; |
| 1003 | } |
| 1004 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1005 | if (n) { /* an extension vetos us */ |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1006 | lwsl_ext("ext %s vetoed\n", (char *)ext->name); |
Andy Green | 0922650 | 2011-05-28 10:19:19 +0100 | [diff] [blame] | 1007 | ext++; |
| 1008 | continue; |
| 1009 | } |
| 1010 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1011 | n = context->protocols[0].callback(context, wsi, |
| 1012 | LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED, |
| 1013 | wsi->user_space, (char *)ext->name, 0); |
| 1014 | |
| 1015 | /* |
| 1016 | * zero return from callback means |
| 1017 | * go ahead and allow the extension, |
| 1018 | * it's what we get if the callback is |
| 1019 | * unhandled |
| 1020 | */ |
| 1021 | |
| 1022 | if (n) { |
| 1023 | ext++; |
| 1024 | continue; |
| 1025 | } |
| 1026 | |
| 1027 | /* apply it */ |
| 1028 | |
| 1029 | if (ext_count) |
| 1030 | *p++ = ','; |
| 1031 | p += sprintf(p, "%s", ext->name); |
| 1032 | ext_count++; |
| 1033 | |
| 1034 | ext++; |
| 1035 | } |
| 1036 | |
| 1037 | p += sprintf(p, "\x0d\x0a"); |
| 1038 | |
| 1039 | if (wsi->ietf_spec_revision) |
| 1040 | p += sprintf(p, "Sec-WebSocket-Version: %d\x0d\x0a", |
| 1041 | wsi->ietf_spec_revision); |
| 1042 | |
| 1043 | /* give userland a chance to append, eg, cookies */ |
| 1044 | |
| 1045 | context->protocols[0].callback(context, wsi, |
| 1046 | LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER, |
| 1047 | NULL, &p, (pkt + sizeof(pkt)) - p - 12); |
| 1048 | |
| 1049 | p += sprintf(p, "\x0d\x0a"); |
| 1050 | |
| 1051 | /* prepare the expected server accept response */ |
| 1052 | |
| 1053 | strcpy((char *)buf, wsi->key_b64); |
| 1054 | strcpy((char *)&buf[strlen((char *)buf)], magic_websocket_guid); |
| 1055 | |
| 1056 | SHA1(buf, strlen((char *)buf), (unsigned char *)hash); |
| 1057 | |
| 1058 | lws_b64_encode_string(hash, 20, |
| 1059 | wsi->initial_handshake_hash_base64, |
| 1060 | sizeof wsi->initial_handshake_hash_base64); |
| 1061 | |
| 1062 | issue_hdr: |
| 1063 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1064 | #if 0 |
| 1065 | puts(pkt); |
| 1066 | #endif |
Andy Green | 0922650 | 2011-05-28 10:19:19 +0100 | [diff] [blame] | 1067 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1068 | /* done with these now */ |
| 1069 | |
| 1070 | free(wsi->c_path); |
| 1071 | free(wsi->c_host); |
| 1072 | if (wsi->c_origin) |
| 1073 | free(wsi->c_origin); |
| 1074 | |
| 1075 | return p; |
| 1076 | } |
| 1077 | |
| 1078 | int |
| 1079 | lws_client_interpret_server_handshake(struct libwebsocket_context *context, |
| 1080 | struct libwebsocket *wsi) |
| 1081 | { |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1082 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 + |
| 1083 | MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING]; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1084 | char pkt[1024]; |
| 1085 | char *p = &pkt[0]; |
| 1086 | const char *pc; |
| 1087 | const char *c; |
| 1088 | int more = 1; |
| 1089 | int okay = 0; |
| 1090 | char ext_name[128]; |
| 1091 | struct libwebsocket_extension *ext; |
| 1092 | void *v; |
Andy Green | c15cb38 | 2011-06-26 10:27:28 +0100 | [diff] [blame] | 1093 | int len = 0; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1094 | int n; |
| 1095 | static const char magic_websocket_04_masking_guid[] = |
| 1096 | "61AC5F19-FBBA-4540-B96F-6561F1AB40A8"; |
| 1097 | |
| 1098 | /* |
| 1099 | * 00 / 76 --> |
| 1100 | * |
| 1101 | * HTTP/1.1 101 WebSocket Protocol Handshake |
| 1102 | * Upgrade: WebSocket |
| 1103 | * Connection: Upgrade |
| 1104 | * Sec-WebSocket-Origin: http://127.0.0.1 |
| 1105 | * Sec-WebSocket-Location: ws://127.0.0.1:9999/socket.io/websocket |
| 1106 | * |
| 1107 | * xxxxxxxxxxxxxxxx |
| 1108 | */ |
| 1109 | |
| 1110 | if (wsi->ietf_spec_revision == 0) { |
| 1111 | if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len || |
| 1112 | !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len || |
| 1113 | !wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len || |
| 1114 | !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len || |
| 1115 | (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len && |
| 1116 | wsi->c_protocol != NULL)) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1117 | lwsl_parser("libwebsocket_client_handshake " |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1118 | "missing required header(s)\n"); |
| 1119 | pkt[len] = '\0'; |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1120 | lwsl_parser("%s", pkt); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1121 | goto bail3; |
| 1122 | } |
| 1123 | |
| 1124 | strtolower(wsi->utf8_token[WSI_TOKEN_HTTP].token); |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1125 | if (strncmp(wsi->utf8_token[WSI_TOKEN_HTTP].token, "101", 3)) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1126 | lwsl_warn("libwebsocket_client_handshake " |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1127 | "server sent bad HTTP response '%s'\n", |
| 1128 | wsi->utf8_token[WSI_TOKEN_HTTP].token); |
| 1129 | goto bail3; |
| 1130 | } |
| 1131 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1132 | if (wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len < 16) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1133 | lwsl_parser("libwebsocket_client_handshake " |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1134 | "challenge reply too short %d\n", |
| 1135 | wsi->utf8_token[ |
| 1136 | WSI_TOKEN_CHALLENGE].token_len); |
| 1137 | pkt[len] = '\0'; |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1138 | lwsl_parser("%s", pkt); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1139 | goto bail3; |
| 1140 | |
| 1141 | } |
| 1142 | |
| 1143 | goto select_protocol; |
| 1144 | } |
| 1145 | |
| 1146 | /* |
| 1147 | * well, what the server sent looked reasonable for syntax. |
| 1148 | * Now let's confirm it sent all the necessary headers |
| 1149 | */ |
| 1150 | #if 0 |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1151 | lwsl_parser("WSI_TOKEN_HTTP: %d\n", |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1152 | wsi->utf8_token[WSI_TOKEN_HTTP].token_len); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1153 | lwsl_parser("WSI_TOKEN_UPGRADE: %d\n", |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1154 | wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1155 | lwsl_parser("WSI_TOKEN_CONNECTION: %d\n", |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1156 | wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1157 | lwsl_parser("WSI_TOKEN_ACCEPT: %d\n", |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1158 | wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1159 | lwsl_parser("WSI_TOKEN_NONCE: %d\n", |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1160 | wsi->utf8_token[WSI_TOKEN_NONCE].token_len); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1161 | lwsl_parser("WSI_TOKEN_PROTOCOL: %d\n", |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1162 | wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1163 | #endif |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1164 | if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len || |
| 1165 | !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len || |
| 1166 | !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len || |
| 1167 | !wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len || |
| 1168 | (!wsi->utf8_token[WSI_TOKEN_NONCE].token_len && |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1169 | wsi->ietf_spec_revision == 4) || |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1170 | (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len && |
| 1171 | wsi->c_protocol != NULL)) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1172 | lwsl_parser("libwebsocket_client_handshake " |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1173 | "missing required header(s)\n"); |
| 1174 | pkt[len] = '\0'; |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1175 | lwsl_parser("%s", pkt); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1176 | goto bail3; |
| 1177 | } |
| 1178 | |
| 1179 | /* |
| 1180 | * Everything seems to be there, now take a closer look at what |
| 1181 | * is in each header |
| 1182 | */ |
| 1183 | |
| 1184 | strtolower(wsi->utf8_token[WSI_TOKEN_HTTP].token); |
Artem Egorkine | d515ddd | 2011-11-23 10:46:24 +0200 | [diff] [blame] | 1185 | if (strncmp(wsi->utf8_token[WSI_TOKEN_HTTP].token, "101", 3)) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1186 | lwsl_warn("libwebsocket_client_handshake " |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1187 | "server sent bad HTTP response '%s'\n", |
| 1188 | wsi->utf8_token[WSI_TOKEN_HTTP].token); |
| 1189 | goto bail3; |
| 1190 | } |
| 1191 | |
| 1192 | strtolower(wsi->utf8_token[WSI_TOKEN_UPGRADE].token); |
| 1193 | if (strcmp(wsi->utf8_token[WSI_TOKEN_UPGRADE].token, |
| 1194 | "websocket")) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1195 | lwsl_warn("libwebsocket_client_handshake server " |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1196 | "sent bad Upgrade header '%s'\n", |
| 1197 | wsi->utf8_token[WSI_TOKEN_UPGRADE].token); |
| 1198 | goto bail3; |
| 1199 | } |
| 1200 | |
| 1201 | strtolower(wsi->utf8_token[WSI_TOKEN_CONNECTION].token); |
| 1202 | if (strcmp(wsi->utf8_token[WSI_TOKEN_CONNECTION].token, |
| 1203 | "upgrade")) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1204 | lwsl_warn("libwebsocket_client_handshake server " |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1205 | "sent bad Connection hdr '%s'\n", |
| 1206 | wsi->utf8_token[WSI_TOKEN_CONNECTION].token); |
| 1207 | goto bail3; |
| 1208 | } |
| 1209 | |
| 1210 | select_protocol: |
| 1211 | pc = wsi->c_protocol; |
| 1212 | if (pc == NULL) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1213 | lwsl_parser("lws_client_interpret_server_handshake: " |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1214 | "NULL c_protocol\n"); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1215 | else |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1216 | lwsl_parser("lws_client_interpret_server_handshake: " |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1217 | "cPprotocol='%s'\n", pc); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1218 | |
| 1219 | /* |
| 1220 | * confirm the protocol the server wants to talk was in the list |
| 1221 | * of protocols we offered |
| 1222 | */ |
| 1223 | |
| 1224 | if (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len) { |
| 1225 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1226 | lwsl_warn("lws_client_interpret_server_handshake " |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1227 | "WSI_TOKEN_PROTOCOL is null\n"); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1228 | /* |
| 1229 | * no protocol name to work from, |
| 1230 | * default to first protocol |
| 1231 | */ |
| 1232 | wsi->protocol = &context->protocols[0]; |
David Brooks | 2c60d95 | 2012-04-20 12:19:01 +0800 | [diff] [blame] | 1233 | wsi->c_callback = wsi->protocol->callback; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1234 | free(wsi->c_protocol); |
| 1235 | |
David Galeano | 4c38f14 | 2013-01-09 19:49:50 +0800 | [diff] [blame] | 1236 | goto check_extensions; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | while (*pc && !okay) { |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1240 | if ((!strncmp(pc, wsi->utf8_token[WSI_TOKEN_PROTOCOL].token, |
| 1241 | wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len)) && |
| 1242 | (pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == ',' || |
| 1243 | pc[wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len] == '\0')) { |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1244 | okay = 1; |
| 1245 | continue; |
| 1246 | } |
| 1247 | while (*pc && *pc != ',') |
| 1248 | pc++; |
| 1249 | while (*pc && *pc != ' ') |
| 1250 | pc++; |
| 1251 | } |
| 1252 | |
| 1253 | /* done with him now */ |
| 1254 | |
| 1255 | if (wsi->c_protocol) |
| 1256 | free(wsi->c_protocol); |
| 1257 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1258 | if (!okay) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1259 | lwsl_err("libwebsocket_client_handshake server " |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1260 | "sent bad protocol '%s'\n", |
| 1261 | wsi->utf8_token[WSI_TOKEN_PROTOCOL].token); |
| 1262 | goto bail2; |
| 1263 | } |
| 1264 | |
| 1265 | /* |
| 1266 | * identify the selected protocol struct and set it |
| 1267 | */ |
| 1268 | n = 0; |
| 1269 | wsi->protocol = NULL; |
David Brooks | 2c60d95 | 2012-04-20 12:19:01 +0800 | [diff] [blame] | 1270 | while (context->protocols[n].callback && !wsi->protocol) { /* Stop after finding first one?? */ |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1271 | if (strcmp(wsi->utf8_token[WSI_TOKEN_PROTOCOL].token, |
David Brooks | 2c60d95 | 2012-04-20 12:19:01 +0800 | [diff] [blame] | 1272 | context->protocols[n].name) == 0) { |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1273 | wsi->protocol = &context->protocols[n]; |
David Brooks | 2c60d95 | 2012-04-20 12:19:01 +0800 | [diff] [blame] | 1274 | wsi->c_callback = wsi->protocol->callback; |
| 1275 | } |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1276 | n++; |
| 1277 | } |
| 1278 | |
| 1279 | if (wsi->protocol == NULL) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1280 | lwsl_err("libwebsocket_client_handshake server " |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1281 | "requested protocol '%s', which we " |
| 1282 | "said we supported but we don't!\n", |
| 1283 | wsi->utf8_token[WSI_TOKEN_PROTOCOL].token); |
| 1284 | goto bail2; |
| 1285 | } |
| 1286 | |
| 1287 | |
David Galeano | 4c38f14 | 2013-01-09 19:49:50 +0800 | [diff] [blame] | 1288 | check_extensions: |
| 1289 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1290 | /* instantiate the accepted extensions */ |
| 1291 | |
| 1292 | if (!wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token_len) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1293 | lwsl_ext("no client extenstions allowed by server\n"); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1294 | goto check_accept; |
| 1295 | } |
| 1296 | |
| 1297 | /* |
| 1298 | * break down the list of server accepted extensions |
| 1299 | * and go through matching them or identifying bogons |
| 1300 | */ |
| 1301 | |
| 1302 | c = wsi->utf8_token[WSI_TOKEN_EXTENSIONS].token; |
| 1303 | n = 0; |
| 1304 | while (more) { |
| 1305 | |
| 1306 | if (*c && (*c != ',' && *c != ' ' && *c != '\t')) { |
| 1307 | ext_name[n] = *c++; |
| 1308 | if (n < sizeof(ext_name) - 1) |
| 1309 | n++; |
| 1310 | continue; |
| 1311 | } |
| 1312 | ext_name[n] = '\0'; |
| 1313 | if (!*c) |
| 1314 | more = 0; |
| 1315 | else { |
| 1316 | c++; |
| 1317 | if (!n) |
| 1318 | continue; |
| 1319 | } |
| 1320 | |
| 1321 | /* check we actually support it */ |
| 1322 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1323 | lwsl_ext("checking client ext %s\n", ext_name); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1324 | |
| 1325 | n = 0; |
| 1326 | ext = wsi->protocol->owning_server->extensions; |
| 1327 | while (ext && ext->callback) { |
| 1328 | |
| 1329 | if (strcmp(ext_name, ext->name)) { |
| 1330 | ext++; |
| 1331 | continue; |
| 1332 | } |
| 1333 | |
| 1334 | n = 1; |
| 1335 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1336 | lwsl_ext("instantiating client ext %s\n", ext_name); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1337 | |
| 1338 | /* instantiate the extension on this conn */ |
| 1339 | |
| 1340 | wsi->active_extensions_user[ |
| 1341 | wsi->count_active_extensions] = |
| 1342 | malloc(ext->per_session_data_size); |
Andy Green | 41c5803 | 2013-01-12 13:21:08 +0800 | [diff] [blame] | 1343 | if (wsi->active_extensions_user[ |
| 1344 | wsi->count_active_extensions] == NULL) { |
| 1345 | lwsl_err("Out of mem\n"); |
| 1346 | goto bail2; |
| 1347 | } |
Andy Green | f665241 | 2011-05-25 20:46:18 +0100 | [diff] [blame] | 1348 | memset(wsi->active_extensions_user[ |
| 1349 | wsi->count_active_extensions], 0, |
| 1350 | ext->per_session_data_size); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1351 | wsi->active_extensions[ |
| 1352 | wsi->count_active_extensions] = ext; |
| 1353 | |
| 1354 | /* allow him to construct his context */ |
| 1355 | |
| 1356 | ext->callback(wsi->protocol->owning_server, |
| 1357 | ext, wsi, |
| 1358 | LWS_EXT_CALLBACK_CLIENT_CONSTRUCT, |
| 1359 | wsi->active_extensions_user[ |
| 1360 | wsi->count_active_extensions], |
| 1361 | NULL, 0); |
| 1362 | |
| 1363 | wsi->count_active_extensions++; |
| 1364 | |
| 1365 | ext++; |
| 1366 | } |
| 1367 | |
| 1368 | if (n == 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1369 | lwsl_warn("Server said we should use" |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1370 | "an unknown extension '%s'!\n", ext_name); |
| 1371 | goto bail2; |
| 1372 | } |
| 1373 | |
| 1374 | n = 0; |
| 1375 | } |
| 1376 | |
| 1377 | |
| 1378 | check_accept: |
| 1379 | |
| 1380 | if (wsi->ietf_spec_revision == 0) { |
| 1381 | |
| 1382 | if (memcmp(wsi->initial_handshake_hash_base64, |
| 1383 | wsi->utf8_token[WSI_TOKEN_CHALLENGE].token, 16)) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1384 | lwsl_warn("libwebsocket_client_handshake " |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1385 | "failed 00 challenge compare\n"); |
| 1386 | pkt[len] = '\0'; |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1387 | lwsl_warn("%s", pkt); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1388 | goto bail2; |
| 1389 | } |
| 1390 | |
| 1391 | goto accept_ok; |
| 1392 | } |
| 1393 | |
| 1394 | /* |
| 1395 | * Confirm his accept token is the one we precomputed |
| 1396 | */ |
| 1397 | |
| 1398 | if (strcmp(wsi->utf8_token[WSI_TOKEN_ACCEPT].token, |
| 1399 | wsi->initial_handshake_hash_base64)) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1400 | lwsl_warn("libwebsocket_client_handshake server " |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1401 | "sent bad ACCEPT '%s' vs computed '%s'\n", |
| 1402 | wsi->utf8_token[WSI_TOKEN_ACCEPT].token, |
| 1403 | wsi->initial_handshake_hash_base64); |
| 1404 | goto bail2; |
| 1405 | } |
| 1406 | |
| 1407 | if (wsi->ietf_spec_revision == 4) { |
| 1408 | /* |
| 1409 | * Calculate the 04 masking key to use when |
| 1410 | * sending data to server |
| 1411 | */ |
| 1412 | |
| 1413 | strcpy((char *)buf, wsi->key_b64); |
| 1414 | p = (char *)buf + strlen(wsi->key_b64); |
| 1415 | strcpy(p, wsi->utf8_token[WSI_TOKEN_NONCE].token); |
| 1416 | p += wsi->utf8_token[WSI_TOKEN_NONCE].token_len; |
| 1417 | strcpy(p, magic_websocket_04_masking_guid); |
| 1418 | SHA1(buf, strlen((char *)buf), wsi->masking_key_04); |
| 1419 | } |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1420 | accept_ok: |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1421 | |
| 1422 | /* allocate the per-connection user memory (if any) */ |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1423 | if (wsi->protocol->per_session_data_size && |
| 1424 | !libwebsocket_ensure_user_space(wsi)) |
| 1425 | goto bail2; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1426 | |
| 1427 | /* clear his proxy connection timeout */ |
| 1428 | |
| 1429 | libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0); |
| 1430 | |
| 1431 | /* mark him as being alive */ |
| 1432 | |
| 1433 | wsi->state = WSI_STATE_ESTABLISHED; |
| 1434 | wsi->mode = LWS_CONNMODE_WS_CLIENT; |
| 1435 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1436 | lwsl_debug("handshake OK for protocol %s\n", wsi->protocol->name); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1437 | |
| 1438 | /* call him back to inform him he is up */ |
| 1439 | |
| 1440 | wsi->protocol->callback(context, wsi, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1441 | LWS_CALLBACK_CLIENT_ESTABLISHED, |
| 1442 | wsi->user_space, NULL, 0); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1443 | |
| 1444 | /* |
| 1445 | * inform all extensions, not just active ones since they |
| 1446 | * already know |
| 1447 | */ |
| 1448 | |
| 1449 | ext = context->extensions; |
| 1450 | |
| 1451 | while (ext && ext->callback) { |
| 1452 | v = NULL; |
| 1453 | for (n = 0; n < wsi->count_active_extensions; n++) |
| 1454 | if (wsi->active_extensions[n] == ext) |
| 1455 | v = wsi->active_extensions_user[n]; |
| 1456 | |
| 1457 | ext->callback(context, ext, wsi, |
| 1458 | LWS_EXT_CALLBACK_ANY_WSI_ESTABLISHED, v, NULL, 0); |
| 1459 | ext++; |
| 1460 | } |
| 1461 | |
| 1462 | return 0; |
| 1463 | |
| 1464 | bail3: |
| 1465 | if (wsi->c_protocol) |
| 1466 | free(wsi->c_protocol); |
| 1467 | |
| 1468 | bail2: |
David Brooks | 80a4497 | 2012-04-20 12:18:47 +0800 | [diff] [blame] | 1469 | if (wsi->c_callback) wsi->c_callback(context, wsi, |
| 1470 | LWS_CALLBACK_CLIENT_CONNECTION_ERROR, |
| 1471 | wsi->user_space, |
| 1472 | NULL, 0); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1473 | libwebsocket_close_and_free_session(context, wsi, |
David Brooks | 80a4497 | 2012-04-20 12:18:47 +0800 | [diff] [blame] | 1474 | LWS_CLOSE_STATUS_NOSTATUS); // But this should be LWS_CLOSE_STATUS_PROTOCOL_ERR |
| 1475 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1476 | return 1; |
| 1477 | } |
| 1478 | |
| 1479 | |
| 1480 | |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 1481 | /** |
| 1482 | * libwebsocket_service_fd() - Service polled socket with something waiting |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1483 | * @context: Websocket context |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 1484 | * @pollfd: The pollfd entry describing the socket fd and which events |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1485 | * happened. |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 1486 | * |
| 1487 | * This function closes any active connections and then frees the |
| 1488 | * context. After calling this, any further use of the context is |
| 1489 | * undefined. |
| 1490 | */ |
| 1491 | |
| 1492 | int |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1493 | libwebsocket_service_fd(struct libwebsocket_context *context, |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1494 | struct pollfd *pollfd) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1495 | { |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1496 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 + |
| 1497 | MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING]; |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 1498 | struct libwebsocket *wsi; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1499 | struct libwebsocket *new_wsi; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1500 | int n; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1501 | int m; |
Tobias Maier | e8c9b56 | 2012-04-05 11:57:12 +0200 | [diff] [blame] | 1502 | ssize_t len; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1503 | int accept_fd; |
| 1504 | unsigned int clilen; |
| 1505 | struct sockaddr_in cli_addr; |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 1506 | struct timeval tv; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1507 | char pkt[1024]; |
| 1508 | char *p = &pkt[0]; |
Andy Green | 2366b1c | 2011-03-06 13:15:31 +0000 | [diff] [blame] | 1509 | int more = 1; |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1510 | struct lws_tokens eff_buf; |
Andy Green | 6c93955 | 2011-03-08 08:56:57 +0000 | [diff] [blame] | 1511 | int opt = 1; |
Yonathan Yusim | 3ae39ff | 2012-04-09 06:42:39 +0800 | [diff] [blame] | 1512 | char c; |
Andy Green | c6517fa | 2011-03-06 13:15:29 +0000 | [diff] [blame] | 1513 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1514 | #ifdef LWS_OPENSSL_SUPPORT |
| 1515 | char ssl_err_buf[512]; |
| 1516 | #endif |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 1517 | /* |
| 1518 | * you can call us with pollfd = NULL to just allow the once-per-second |
| 1519 | * global timeout checks; if less than a second since the last check |
| 1520 | * it returns immediately then. |
| 1521 | */ |
| 1522 | |
| 1523 | gettimeofday(&tv, NULL); |
| 1524 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1525 | if (context->last_timeout_check_s != tv.tv_sec) { |
| 1526 | context->last_timeout_check_s = tv.tv_sec; |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 1527 | |
| 1528 | /* global timeout check once per second */ |
| 1529 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1530 | for (n = 0; n < context->fds_count; n++) { |
| 1531 | wsi = wsi_from_fd(context, context->fds[n].fd); |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 1532 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1533 | libwebsocket_service_timeout_check(context, wsi, |
| 1534 | tv.tv_sec); |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | /* just here for timeout management? */ |
| 1539 | |
| 1540 | if (pollfd == NULL) |
| 1541 | return 0; |
| 1542 | |
| 1543 | /* no, here to service a socket descriptor */ |
| 1544 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1545 | wsi = wsi_from_fd(context, pollfd->fd); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1546 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1547 | if (wsi == NULL) |
Andy Green | fa3f405 | 2012-10-07 20:40:35 +0800 | [diff] [blame] | 1548 | return 0; |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1549 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1550 | switch (wsi->mode) { |
| 1551 | case LWS_CONNMODE_SERVER_LISTENER: |
| 1552 | |
| 1553 | /* pollin means a client has connected to us then */ |
| 1554 | |
David Galeano | b88e096 | 2013-01-10 09:54:10 +0800 | [diff] [blame] | 1555 | if (!(pollfd->revents & POLLIN)) |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1556 | break; |
| 1557 | |
David Galeano | f700935 | 2011-09-26 12:09:54 +0100 | [diff] [blame] | 1558 | if (context->fds_count >= MAX_CLIENTS) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1559 | lwsl_warn("too busy to accept new client\n"); |
David Galeano | f700935 | 2011-09-26 12:09:54 +0100 | [diff] [blame] | 1560 | break; |
| 1561 | } |
| 1562 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1563 | /* listen socket got an unencrypted connection... */ |
| 1564 | |
| 1565 | clilen = sizeof(cli_addr); |
| 1566 | accept_fd = accept(pollfd->fd, (struct sockaddr *)&cli_addr, |
| 1567 | &clilen); |
| 1568 | if (accept_fd < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1569 | lwsl_warn("ERROR on accept: %d\n", strerror(errno)); |
Andy Green | 3928f61 | 2012-07-20 12:58:38 +0800 | [diff] [blame] | 1570 | return -1; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
Andy Green | 6c93955 | 2011-03-08 08:56:57 +0000 | [diff] [blame] | 1573 | /* Disable Nagle */ |
| 1574 | opt = 1; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1575 | setsockopt(accept_fd, IPPROTO_TCP, TCP_NODELAY, |
| 1576 | (const void *)&opt, sizeof(opt)); |
Andy Green | 6c93955 | 2011-03-08 08:56:57 +0000 | [diff] [blame] | 1577 | |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 1578 | /* |
| 1579 | * look at who we connected to and give user code a chance |
| 1580 | * to reject based on client IP. There's no protocol selected |
| 1581 | * yet so we issue this to protocols[0] |
| 1582 | */ |
| 1583 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1584 | if ((context->protocols[0].callback)(context, wsi, |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 1585 | LWS_CALLBACK_FILTER_NETWORK_CONNECTION, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1586 | (void *)(long)accept_fd, NULL, 0)) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1587 | lwsl_debug("Callback denied network connection\n"); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1588 | #ifdef WIN32 |
| 1589 | closesocket(accept_fd); |
| 1590 | #else |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 1591 | close(accept_fd); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1592 | #endif |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 1593 | break; |
| 1594 | } |
| 1595 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1596 | /* accepting connection to main listener */ |
| 1597 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1598 | new_wsi = libwebsocket_create_new_server_wsi(context); |
David Galeano | ed3c840 | 2013-01-10 10:45:24 +0800 | [diff] [blame] | 1599 | if (new_wsi == NULL) { |
| 1600 | #ifdef WIN32 |
| 1601 | closesocket(accept_fd); |
| 1602 | #else |
| 1603 | close(accept_fd); |
| 1604 | #endif |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1605 | break; |
David Galeano | ed3c840 | 2013-01-10 10:45:24 +0800 | [diff] [blame] | 1606 | } |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1607 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1608 | new_wsi->sock = accept_fd; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1609 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1610 | |
| 1611 | #ifdef LWS_OPENSSL_SUPPORT |
| 1612 | new_wsi->ssl = NULL; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1613 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1614 | if (context->use_ssl) { |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1615 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1616 | new_wsi->ssl = SSL_new(context->ssl_ctx); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1617 | if (new_wsi->ssl == NULL) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1618 | lwsl_err("SSL_new failed: %s\n", |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1619 | ERR_error_string(SSL_get_error( |
| 1620 | new_wsi->ssl, 0), NULL)); |
Andy Green | 1f9bf52 | 2011-02-14 21:14:37 +0000 | [diff] [blame] | 1621 | libwebsockets_decode_ssl_error(); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1622 | free(new_wsi); |
David Galeano | ed3c840 | 2013-01-10 10:45:24 +0800 | [diff] [blame] | 1623 | #ifdef WIN32 |
| 1624 | closesocket(accept_fd); |
| 1625 | #else |
| 1626 | close(accept_fd); |
| 1627 | #endif |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1628 | break; |
| 1629 | } |
| 1630 | |
| 1631 | SSL_set_fd(new_wsi->ssl, accept_fd); |
| 1632 | |
| 1633 | n = SSL_accept(new_wsi->ssl); |
| 1634 | if (n != 1) { |
| 1635 | /* |
| 1636 | * browsers seem to probe with various |
| 1637 | * ssl params which fail then retry |
| 1638 | * and succeed |
| 1639 | */ |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1640 | lwsl_debug("SSL_accept failed skt %u: %s\n", |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1641 | pollfd->fd, |
| 1642 | ERR_error_string(SSL_get_error( |
| 1643 | new_wsi->ssl, n), NULL)); |
| 1644 | SSL_free( |
| 1645 | new_wsi->ssl); |
| 1646 | free(new_wsi); |
David Galeano | ed3c840 | 2013-01-10 10:45:24 +0800 | [diff] [blame] | 1647 | #ifdef WIN32 |
| 1648 | closesocket(accept_fd); |
| 1649 | #else |
| 1650 | close(accept_fd); |
| 1651 | #endif |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1652 | break; |
| 1653 | } |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1654 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1655 | lwsl_debug("accepted new SSL conn " |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1656 | "port %u on fd=%d SSL ver %s\n", |
| 1657 | ntohs(cli_addr.sin_port), accept_fd, |
| 1658 | SSL_get_version(new_wsi->ssl)); |
| 1659 | |
| 1660 | } else |
| 1661 | #endif |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1662 | lwsl_debug("accepted new conn port %u on fd=%d\n", |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1663 | ntohs(cli_addr.sin_port), accept_fd); |
| 1664 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1665 | insert_wsi(context, new_wsi); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1666 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1667 | /* |
| 1668 | * make sure NO events are seen yet on this new socket |
| 1669 | * (otherwise we inherit old fds[client].revents from |
| 1670 | * previous socket there and die mysteriously! ) |
| 1671 | */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1672 | context->fds[context->fds_count].revents = 0; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1673 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1674 | context->fds[context->fds_count].events = POLLIN; |
| 1675 | context->fds[context->fds_count++].fd = accept_fd; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1676 | |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1677 | /* external POLL support via protocol 0 */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1678 | context->protocols[0].callback(context, new_wsi, |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1679 | LWS_CALLBACK_ADD_POLL_FD, |
| 1680 | (void *)(long)accept_fd, NULL, POLLIN); |
| 1681 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1682 | break; |
| 1683 | |
| 1684 | case LWS_CONNMODE_BROADCAST_PROXY_LISTENER: |
| 1685 | |
| 1686 | /* as we are listening, POLLIN means accept() is needed */ |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1687 | |
David Galeano | b88e096 | 2013-01-10 09:54:10 +0800 | [diff] [blame] | 1688 | if (!(pollfd->revents & POLLIN)) |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1689 | break; |
| 1690 | |
| 1691 | /* listen socket got an unencrypted connection... */ |
| 1692 | |
| 1693 | clilen = sizeof(cli_addr); |
| 1694 | accept_fd = accept(pollfd->fd, (struct sockaddr *)&cli_addr, |
| 1695 | &clilen); |
| 1696 | if (accept_fd < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1697 | lwsl_warn("ERROR on accept %d\n", accept_fd); |
Andy Green | 3928f61 | 2012-07-20 12:58:38 +0800 | [diff] [blame] | 1698 | return -1; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1701 | if (context->fds_count >= MAX_CLIENTS) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1702 | lwsl_err("too busy to accept new broadcast " |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1703 | "proxy client\n"); |
Andy Green | 41c5803 | 2013-01-12 13:21:08 +0800 | [diff] [blame] | 1704 | goto bail_prox_listener; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | /* create a dummy wsi for the connection and add it */ |
| 1708 | |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 1709 | new_wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket)); |
Andy Green | 41c5803 | 2013-01-12 13:21:08 +0800 | [diff] [blame] | 1710 | if (new_wsi == NULL) { |
| 1711 | lwsl_err("Out of mem\n"); |
| 1712 | goto bail_prox_listener; |
| 1713 | } |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 1714 | memset(new_wsi, 0, sizeof (struct libwebsocket)); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1715 | new_wsi->sock = accept_fd; |
| 1716 | new_wsi->mode = LWS_CONNMODE_BROADCAST_PROXY; |
| 1717 | new_wsi->state = WSI_STATE_ESTABLISHED; |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 1718 | new_wsi->count_active_extensions = 0; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1719 | /* note which protocol we are proxying */ |
| 1720 | new_wsi->protocol_index_for_broadcast_proxy = |
| 1721 | wsi->protocol_index_for_broadcast_proxy; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1722 | insert_wsi(context, new_wsi); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1723 | |
| 1724 | /* add connected socket to internal poll array */ |
| 1725 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1726 | context->fds[context->fds_count].revents = 0; |
| 1727 | context->fds[context->fds_count].events = POLLIN; |
| 1728 | context->fds[context->fds_count++].fd = accept_fd; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1729 | |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1730 | /* external POLL support via protocol 0 */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1731 | context->protocols[0].callback(context, new_wsi, |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1732 | LWS_CALLBACK_ADD_POLL_FD, |
| 1733 | (void *)(long)accept_fd, NULL, POLLIN); |
| 1734 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1735 | break; |
| 1736 | |
Andy Green | 41c5803 | 2013-01-12 13:21:08 +0800 | [diff] [blame] | 1737 | bail_prox_listener: |
| 1738 | #ifdef WIN32 |
| 1739 | closesocket(accept_fd); |
| 1740 | #else |
| 1741 | close(accept_fd); |
| 1742 | #endif |
| 1743 | break; |
| 1744 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1745 | case LWS_CONNMODE_BROADCAST_PROXY: |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1746 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1747 | /* handle session socket closed */ |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1748 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1749 | if (pollfd->revents & (POLLERR | POLLHUP)) { |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1750 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1751 | lwsl_debug("Session Socket %p (fd=%d) dead\n", |
Timothy J Fontaine | b86d64e | 2011-02-14 17:55:27 +0000 | [diff] [blame] | 1752 | (void *)wsi, pollfd->fd); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1753 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1754 | libwebsocket_close_and_free_session(context, wsi, |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 1755 | LWS_CLOSE_STATUS_NORMAL); |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 1756 | return 1; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1757 | } |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1758 | |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 1759 | /* |
| 1760 | * either extension code with stuff to spill, or the user code, |
| 1761 | * requested a callback when it was OK to write |
| 1762 | */ |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1763 | |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 1764 | if (pollfd->revents & POLLOUT) |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1765 | if (lws_handle_POLLOUT_event(context, wsi, |
| 1766 | pollfd) < 0) { |
| 1767 | libwebsocket_close_and_free_session( |
| 1768 | context, wsi, LWS_CLOSE_STATUS_NORMAL); |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 1769 | return 1; |
| 1770 | } |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1771 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1772 | /* any incoming data ready? */ |
| 1773 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1774 | if (!(pollfd->revents & POLLIN)) |
| 1775 | break; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1776 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1777 | /* get the issued broadcast payload from the socket */ |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1778 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1779 | len = read(pollfd->fd, buf + LWS_SEND_BUFFER_PRE_PADDING, |
| 1780 | MAX_BROADCAST_PAYLOAD); |
| 1781 | if (len < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1782 | lwsl_err("Error reading broadcast payload\n"); |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 1783 | break; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1784 | } |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1785 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1786 | /* broadcast it to all guys with this protocol index */ |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1787 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1788 | for (n = 0; n < FD_HASHTABLE_MODULUS; n++) { |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1789 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1790 | for (m = 0; m < context->fd_hashtable[n].length; m++) { |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1791 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1792 | new_wsi = context->fd_hashtable[n].wsi[m]; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1793 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1794 | /* only to clients we are serving to */ |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1795 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1796 | if (new_wsi->mode != LWS_CONNMODE_WS_SERVING) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1797 | continue; |
| 1798 | |
| 1799 | /* |
| 1800 | * never broadcast to non-established |
| 1801 | * connection |
| 1802 | */ |
| 1803 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1804 | if (new_wsi->state != WSI_STATE_ESTABLISHED) |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1805 | continue; |
| 1806 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1807 | /* |
| 1808 | * only broadcast to connections using |
| 1809 | * the requested protocol |
| 1810 | */ |
| 1811 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1812 | if (new_wsi->protocol->protocol_index != |
| 1813 | wsi->protocol_index_for_broadcast_proxy) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1814 | continue; |
| 1815 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1816 | /* broadcast it to this connection */ |
| 1817 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1818 | new_wsi->protocol->callback(context, new_wsi, |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1819 | LWS_CALLBACK_BROADCAST, |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1820 | new_wsi->user_space, |
Andy Green | 0ca6a17 | 2010-12-19 20:50:01 +0000 | [diff] [blame] | 1821 | buf + LWS_SEND_BUFFER_PRE_PADDING, len); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1822 | } |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1823 | } |
| 1824 | break; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1825 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1826 | case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY: |
| 1827 | |
| 1828 | /* handle proxy hung up on us */ |
| 1829 | |
| 1830 | if (pollfd->revents & (POLLERR | POLLHUP)) { |
| 1831 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1832 | lwsl_warn("Proxy connection %p (fd=%d) dead\n", |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1833 | (void *)wsi, pollfd->fd); |
| 1834 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1835 | libwebsocket_close_and_free_session(context, wsi, |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 1836 | LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1837 | return 1; |
| 1838 | } |
| 1839 | |
Andy Green | 72c3432 | 2011-04-16 10:46:21 +0100 | [diff] [blame] | 1840 | n = recv(wsi->sock, pkt, sizeof pkt, 0); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1841 | if (n < 0) { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1842 | libwebsocket_close_and_free_session(context, wsi, |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 1843 | LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1844 | lwsl_err("ERROR reading from proxy socket\n"); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1845 | return 1; |
| 1846 | } |
| 1847 | |
| 1848 | pkt[13] = '\0'; |
| 1849 | if (strcmp(pkt, "HTTP/1.0 200 ") != 0) { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1850 | libwebsocket_close_and_free_session(context, wsi, |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 1851 | LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1852 | lwsl_err("ERROR from proxy: %s\n", pkt); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1853 | return 1; |
| 1854 | } |
| 1855 | |
| 1856 | /* clear his proxy connection timeout */ |
| 1857 | |
| 1858 | libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0); |
| 1859 | |
| 1860 | /* fallthru */ |
| 1861 | |
| 1862 | case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE: |
| 1863 | |
| 1864 | #ifdef LWS_OPENSSL_SUPPORT |
Ken Atherton | 8360a47 | 2012-05-03 11:45:04 +0800 | [diff] [blame] | 1865 | if (wsi->use_ssl && !wsi->ssl) { |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1866 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1867 | wsi->ssl = SSL_new(context->ssl_client_ctx); |
| 1868 | wsi->client_bio = BIO_new_socket(wsi->sock, |
| 1869 | BIO_NOCLOSE); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1870 | SSL_set_bio(wsi->ssl, wsi->client_bio, wsi->client_bio); |
| 1871 | |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 1872 | SSL_set_ex_data(wsi->ssl, |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 1873 | openssl_websocket_private_data_index, |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1874 | context); |
Ken Atherton | 8360a47 | 2012-05-03 11:45:04 +0800 | [diff] [blame] | 1875 | } |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 1876 | |
Ken Atherton | 8360a47 | 2012-05-03 11:45:04 +0800 | [diff] [blame] | 1877 | if (wsi->use_ssl) { |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1878 | if (SSL_connect(wsi->ssl) <= 0) { |
Ken Atherton | 8360a47 | 2012-05-03 11:45:04 +0800 | [diff] [blame] | 1879 | |
| 1880 | /* |
| 1881 | * retry if new data comes until we |
| 1882 | * run into the connection timeout or win |
| 1883 | */ |
| 1884 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1885 | lwsl_err("SSL connect error %s\n", |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 1886 | ERR_error_string(ERR_get_error(), |
| 1887 | ssl_err_buf)); |
Ken Atherton | 8360a47 | 2012-05-03 11:45:04 +0800 | [diff] [blame] | 1888 | return 0; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | n = SSL_get_verify_result(wsi->ssl); |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 1892 | if ((n != X509_V_OK) && ( |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 1893 | n != X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT || |
| 1894 | wsi->use_ssl != 2)) { |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1895 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1896 | lwsl_err("server's cert didn't " |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 1897 | "look good %d\n", n); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1898 | libwebsocket_close_and_free_session(context, |
| 1899 | wsi, LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 1900 | return 1; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1901 | } |
Ken Atherton | 8360a47 | 2012-05-03 11:45:04 +0800 | [diff] [blame] | 1902 | } else |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1903 | wsi->ssl = NULL; |
| 1904 | #endif |
| 1905 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1906 | p = libwebsockets_generate_client_handshake(context, wsi, p); |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1907 | if (p == NULL) |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1908 | return 1; |
Andy Green | eeaacb3 | 2011-03-01 20:44:24 +0000 | [diff] [blame] | 1909 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1910 | /* send our request to the server */ |
| 1911 | |
| 1912 | #ifdef LWS_OPENSSL_SUPPORT |
| 1913 | if (wsi->use_ssl) |
| 1914 | n = SSL_write(wsi->ssl, pkt, p - pkt); |
| 1915 | else |
| 1916 | #endif |
| 1917 | n = send(wsi->sock, pkt, p - pkt, 0); |
| 1918 | |
| 1919 | if (n < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1920 | lwsl_debug("ERROR writing to client socket\n"); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1921 | libwebsocket_close_and_free_session(context, wsi, |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 1922 | LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1923 | return 1; |
| 1924 | } |
| 1925 | |
| 1926 | wsi->parser_state = WSI_TOKEN_NAME_PART; |
| 1927 | wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY; |
| 1928 | libwebsocket_set_timeout(wsi, |
David Galeano | c9f1ff8 | 2013-01-09 18:01:23 +0800 | [diff] [blame] | 1929 | PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE, AWAITING_TIMEOUT); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1930 | |
| 1931 | break; |
| 1932 | |
| 1933 | case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY: |
| 1934 | |
| 1935 | /* handle server hung up on us */ |
| 1936 | |
| 1937 | if (pollfd->revents & (POLLERR | POLLHUP)) { |
| 1938 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1939 | lwsl_debug("Server connection %p (fd=%d) dead\n", |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1940 | (void *)wsi, pollfd->fd); |
| 1941 | |
| 1942 | goto bail3; |
| 1943 | } |
| 1944 | |
| 1945 | |
| 1946 | /* interpret the server response */ |
| 1947 | |
| 1948 | /* |
| 1949 | * HTTP/1.1 101 Switching Protocols |
| 1950 | * Upgrade: websocket |
| 1951 | * Connection: Upgrade |
| 1952 | * Sec-WebSocket-Accept: me89jWimTRKTWwrS3aRrL53YZSo= |
| 1953 | * Sec-WebSocket-Nonce: AQIDBAUGBwgJCgsMDQ4PEC== |
| 1954 | * Sec-WebSocket-Protocol: chat |
| 1955 | */ |
| 1956 | |
Yonathan Yusim | 3ae39ff | 2012-04-09 06:42:39 +0800 | [diff] [blame] | 1957 | /* |
| 1958 | * we have to take some care here to only take from the |
| 1959 | * socket bytewise. The browser may (and has been seen to |
| 1960 | * in the case that onopen() performs websocket traffic) |
| 1961 | * coalesce both handshake response and websocket traffic |
| 1962 | * in one packet, since at that point the connection is |
| 1963 | * definitively ready from browser pov. |
| 1964 | */ |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1965 | |
Andy Green | 7b5af9a | 2012-04-09 15:23:47 +0800 | [diff] [blame] | 1966 | len = 1; |
Yonathan Yusim | 3ae39ff | 2012-04-09 06:42:39 +0800 | [diff] [blame] | 1967 | while (wsi->parser_state != WSI_PARSING_COMPLETE && len > 0) { |
| 1968 | #ifdef LWS_OPENSSL_SUPPORT |
| 1969 | if (wsi->use_ssl) |
| 1970 | len = SSL_read(wsi->ssl, &c, 1); |
| 1971 | else |
| 1972 | #endif |
| 1973 | len = recv(wsi->sock, &c, 1, 0); |
| 1974 | |
| 1975 | libwebsocket_parse(wsi, c); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
Andy Green | 27a0b91 | 2011-04-16 10:54:28 +0100 | [diff] [blame] | 1978 | /* |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1979 | * hs may also be coming in multiple packets, there is a 5-sec |
Andy Green | 27a0b91 | 2011-04-16 10:54:28 +0100 | [diff] [blame] | 1980 | * libwebsocket timeout still active here too, so if parsing did |
| 1981 | * not complete just wait for next packet coming in this state |
| 1982 | */ |
| 1983 | |
| 1984 | if (wsi->parser_state != WSI_PARSING_COMPLETE) |
| 1985 | break; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1986 | |
Yonathan Yusim | 3ae39ff | 2012-04-09 06:42:39 +0800 | [diff] [blame] | 1987 | /* |
| 1988 | * otherwise deal with the handshake. If there's any |
| 1989 | * packet traffic already arrived we'll trigger poll() again |
| 1990 | * right away and deal with it that way |
| 1991 | */ |
| 1992 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1993 | return lws_client_interpret_server_handshake(context, wsi); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1994 | |
| 1995 | bail3: |
| 1996 | if (wsi->c_protocol) |
| 1997 | free(wsi->c_protocol); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1998 | libwebsocket_close_and_free_session(context, wsi, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1999 | LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 2000 | return 1; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2001 | |
| 2002 | case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT: |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2003 | lwsl_ext("LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT\n"); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2004 | break; |
| 2005 | |
| 2006 | case LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD: |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2007 | lwsl_ext("LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD\n"); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2008 | break; |
| 2009 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 2010 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2011 | case LWS_CONNMODE_WS_SERVING: |
| 2012 | case LWS_CONNMODE_WS_CLIENT: |
| 2013 | |
| 2014 | /* handle session socket closed */ |
| 2015 | |
| 2016 | if (pollfd->revents & (POLLERR | POLLHUP)) { |
| 2017 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2018 | lwsl_debug("Session Socket %p (fd=%d) dead\n", |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2019 | (void *)wsi, pollfd->fd); |
| 2020 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2021 | libwebsocket_close_and_free_session(context, wsi, |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 2022 | LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 2023 | return 1; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2026 | /* the guy requested a callback when it was OK to write */ |
| 2027 | |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 2028 | if ((pollfd->revents & POLLOUT) && |
| 2029 | wsi->state == WSI_STATE_ESTABLISHED) |
| 2030 | if (lws_handle_POLLOUT_event(context, wsi, |
| 2031 | pollfd) < 0) { |
| 2032 | libwebsocket_close_and_free_session( |
| 2033 | context, wsi, LWS_CLOSE_STATUS_NORMAL); |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 2034 | return 1; |
| 2035 | } |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2036 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2037 | |
| 2038 | /* any incoming data ready? */ |
| 2039 | |
| 2040 | if (!(pollfd->revents & POLLIN)) |
| 2041 | break; |
| 2042 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2043 | #ifdef LWS_OPENSSL_SUPPORT |
David Galeano | 7ffbe1b | 2013-01-10 10:35:32 +0800 | [diff] [blame] | 2044 | read_pending: |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2045 | if (wsi->ssl) |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 2046 | eff_buf.token_len = SSL_read(wsi->ssl, buf, sizeof buf); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2047 | else |
| 2048 | #endif |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 2049 | eff_buf.token_len = |
Andy Green | 72c3432 | 2011-04-16 10:46:21 +0100 | [diff] [blame] | 2050 | recv(pollfd->fd, buf, sizeof buf, 0); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2051 | |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 2052 | if (eff_buf.token_len < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2053 | lwsl_debug("Socket read returned %d\n", |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 2054 | eff_buf.token_len); |
Alon Levy | dc93b7f | 2012-10-19 11:21:57 +0200 | [diff] [blame] | 2055 | if (errno != EINTR && errno != EAGAIN) |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2056 | libwebsocket_close_and_free_session(context, |
| 2057 | wsi, LWS_CLOSE_STATUS_NOSTATUS); |
Nick Dowell | c04c193 | 2012-04-05 10:29:39 +0800 | [diff] [blame] | 2058 | return 1; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2059 | } |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 2060 | if (!eff_buf.token_len) { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2061 | libwebsocket_close_and_free_session(context, wsi, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2062 | LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | fa3f405 | 2012-10-07 20:40:35 +0800 | [diff] [blame] | 2063 | return 0; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 2066 | /* |
| 2067 | * give any active extensions a chance to munge the buffer |
| 2068 | * before parse. We pass in a pointer to an lws_tokens struct |
| 2069 | * prepared with the default buffer and content length that's in |
| 2070 | * there. Rather than rewrite the default buffer, extensions |
| 2071 | * that expect to grow the buffer can adapt .token to |
| 2072 | * point to their own per-connection buffer in the extension |
| 2073 | * user allocation. By default with no extensions or no |
| 2074 | * extension callback handling, just the normal input buffer is |
| 2075 | * used then so it is efficient. |
| 2076 | */ |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2077 | |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 2078 | eff_buf.token = (char *)buf; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2079 | |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 2080 | more = 1; |
| 2081 | while (more) { |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2082 | |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 2083 | more = 0; |
| 2084 | |
| 2085 | for (n = 0; n < wsi->count_active_extensions; n++) { |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 2086 | m = wsi->active_extensions[n]->callback(context, |
| 2087 | wsi->active_extensions[n], wsi, |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 2088 | LWS_EXT_CALLBACK_PACKET_RX_PREPARSE, |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 2089 | wsi->active_extensions_user[n], |
| 2090 | &eff_buf, 0); |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 2091 | if (m < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2092 | lwsl_ext( |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2093 | "Extension reports fatal error\n"); |
| 2094 | libwebsocket_close_and_free_session( |
| 2095 | context, wsi, |
| 2096 | LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 2097 | return 1; |
| 2098 | } |
| 2099 | if (m) |
| 2100 | more = 1; |
| 2101 | } |
| 2102 | |
| 2103 | /* service incoming data */ |
| 2104 | |
| 2105 | if (eff_buf.token_len) { |
| 2106 | n = libwebsocket_read(context, wsi, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2107 | (unsigned char *)eff_buf.token, |
| 2108 | eff_buf.token_len); |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 2109 | if (n < 0) |
| 2110 | /* we closed wsi */ |
| 2111 | return 1; |
| 2112 | } |
| 2113 | |
| 2114 | eff_buf.token = NULL; |
| 2115 | eff_buf.token_len = 0; |
| 2116 | } |
David Galeano | 7ffbe1b | 2013-01-10 10:35:32 +0800 | [diff] [blame] | 2117 | |
| 2118 | #ifdef LWS_OPENSSL_SUPPORT |
| 2119 | if (wsi->ssl && SSL_pending(wsi->ssl)) |
| 2120 | goto read_pending; |
| 2121 | #endif |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 2122 | break; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | return 0; |
| 2126 | } |
| 2127 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2128 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 2129 | /** |
| 2130 | * libwebsocket_context_destroy() - Destroy the websocket context |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2131 | * @context: Websocket context |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 2132 | * |
| 2133 | * This function closes any active connections and then frees the |
| 2134 | * context. After calling this, any further use of the context is |
| 2135 | * undefined. |
| 2136 | */ |
| 2137 | void |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2138 | libwebsocket_context_destroy(struct libwebsocket_context *context) |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 2139 | { |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2140 | int n; |
| 2141 | int m; |
| 2142 | struct libwebsocket *wsi; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2143 | struct libwebsocket_extension *ext; |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 2144 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 2145 | for (n = 0; n < FD_HASHTABLE_MODULUS; n++) |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2146 | for (m = 0; m < context->fd_hashtable[n].length; m++) { |
| 2147 | wsi = context->fd_hashtable[n].wsi[m]; |
| 2148 | libwebsocket_close_and_free_session(context, wsi, |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 2149 | LWS_CLOSE_STATUS_GOINGAWAY); |
Andy Green | f3d3b40 | 2011-02-09 07:16:34 +0000 | [diff] [blame] | 2150 | } |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 2151 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2152 | /* |
| 2153 | * give all extensions a chance to clean up any per-context |
| 2154 | * allocations they might have made |
| 2155 | */ |
| 2156 | |
| 2157 | ext = context->extensions; |
| 2158 | m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT; |
| 2159 | if (context->listen_port) |
| 2160 | m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT; |
Paulo Roberto Urio | 1f680ab | 2012-06-04 08:40:28 +0800 | [diff] [blame] | 2161 | while (ext && ext->callback) { |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 2162 | 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] | 2163 | ext++; |
| 2164 | } |
| 2165 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2166 | #ifdef WIN32 |
| 2167 | #else |
| 2168 | close(context->fd_random); |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 2169 | #endif |
| 2170 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2171 | #ifdef LWS_OPENSSL_SUPPORT |
| 2172 | if (context->ssl_ctx) |
| 2173 | SSL_CTX_free(context->ssl_ctx); |
| 2174 | if (context->ssl_client_ctx) |
| 2175 | SSL_CTX_free(context->ssl_client_ctx); |
| 2176 | #endif |
| 2177 | |
| 2178 | free(context); |
| 2179 | |
| 2180 | #ifdef WIN32 |
| 2181 | WSACleanup(); |
| 2182 | #endif |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Alon Levy | 0291eb3 | 2012-10-19 11:21:56 +0200 | [diff] [blame] | 2185 | LWS_EXTERN void * |
| 2186 | libwebsocket_context_user(struct libwebsocket_context *context) |
| 2187 | { |
| 2188 | return context->user_space; |
| 2189 | } |
| 2190 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 2191 | /** |
| 2192 | * libwebsocket_service() - Service any pending websocket activity |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2193 | * @context: Websocket context |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 2194 | * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed |
| 2195 | * service otherwise block and service immediately, returning |
| 2196 | * after the timeout if nothing needed service. |
| 2197 | * |
| 2198 | * This function deals with any pending websocket traffic, for three |
| 2199 | * kinds of event. It handles these events on both server and client |
| 2200 | * types of connection the same. |
| 2201 | * |
| 2202 | * 1) Accept new connections to our context's server |
| 2203 | * |
| 2204 | * 2) Perform pending broadcast writes initiated from other forked |
| 2205 | * processes (effectively serializing asynchronous broadcasts) |
| 2206 | * |
| 2207 | * 3) Call the receive callback for incoming frame data received by |
| 2208 | * server or client connections. |
| 2209 | * |
| 2210 | * You need to call this service function periodically to all the above |
| 2211 | * functions to happen; if your application is single-threaded you can |
| 2212 | * just call it in your main event loop. |
| 2213 | * |
| 2214 | * Alternatively you can fork a new process that asynchronously handles |
| 2215 | * calling this service in a loop. In that case you are happy if this |
| 2216 | * call blocks your thread until it needs to take care of something and |
| 2217 | * would call it with a large nonzero timeout. Your loop then takes no |
| 2218 | * CPU while there is nothing happening. |
| 2219 | * |
| 2220 | * If you are calling it in a single-threaded app, you don't want it to |
| 2221 | * wait around blocking other things in your loop from happening, so you |
| 2222 | * would call it with a timeout_ms of 0, so it returns immediately if |
| 2223 | * nothing is pending, or as soon as it services whatever was pending. |
| 2224 | */ |
| 2225 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2226 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2227 | int |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2228 | libwebsocket_service(struct libwebsocket_context *context, int timeout_ms) |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2229 | { |
| 2230 | int n; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2231 | |
| 2232 | /* stay dead once we are dead */ |
| 2233 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2234 | if (context == NULL) |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2235 | return 1; |
| 2236 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2237 | /* wait for something to need service */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2238 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2239 | n = poll(context->fds, context->fds_count, timeout_ms); |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 2240 | if (n == 0) /* poll timeout */ |
| 2241 | return 0; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2242 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 2243 | if (n < 0) { |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 2244 | /* |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2245 | lwsl_err("Listen Socket dead\n"); |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 2246 | */ |
Andy Green | 3928f61 | 2012-07-20 12:58:38 +0800 | [diff] [blame] | 2247 | return -1; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2248 | } |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2249 | |
| 2250 | /* handle accept on listening socket? */ |
| 2251 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2252 | for (n = 0; n < context->fds_count; n++) |
| 2253 | if (context->fds[n].revents) |
Andy Green | 3928f61 | 2012-07-20 12:58:38 +0800 | [diff] [blame] | 2254 | if (libwebsocket_service_fd(context, |
| 2255 | &context->fds[n]) < 0) |
| 2256 | return -1; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2257 | return 0; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2258 | } |
| 2259 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2260 | int |
| 2261 | lws_any_extension_handled(struct libwebsocket_context *context, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2262 | struct libwebsocket *wsi, |
| 2263 | enum libwebsocket_extension_callback_reasons r, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2264 | void *v, size_t len) |
| 2265 | { |
| 2266 | int n; |
| 2267 | int handled = 0; |
| 2268 | |
| 2269 | /* maybe an extension will take care of it for us */ |
| 2270 | |
| 2271 | for (n = 0; n < wsi->count_active_extensions && !handled; n++) { |
| 2272 | if (!wsi->active_extensions[n]->callback) |
| 2273 | continue; |
| 2274 | |
| 2275 | handled |= wsi->active_extensions[n]->callback(context, |
| 2276 | wsi->active_extensions[n], wsi, |
| 2277 | r, wsi->active_extensions_user[n], v, len); |
| 2278 | } |
| 2279 | |
| 2280 | return handled; |
| 2281 | } |
| 2282 | |
| 2283 | |
| 2284 | void * |
| 2285 | lws_get_extension_user_matching_ext(struct libwebsocket *wsi, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2286 | struct libwebsocket_extension *ext) |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2287 | { |
| 2288 | int n = 0; |
| 2289 | |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 2290 | if (wsi == NULL) |
| 2291 | return NULL; |
| 2292 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2293 | while (n < wsi->count_active_extensions) { |
| 2294 | if (wsi->active_extensions[n] != ext) { |
| 2295 | n++; |
| 2296 | continue; |
| 2297 | } |
| 2298 | return wsi->active_extensions_user[n]; |
| 2299 | } |
| 2300 | |
| 2301 | return NULL; |
| 2302 | } |
| 2303 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2304 | /** |
| 2305 | * libwebsocket_callback_on_writable() - Request a callback when this socket |
| 2306 | * becomes able to be written to without |
| 2307 | * blocking |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 2308 | * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2309 | * @context: libwebsockets context |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2310 | * @wsi: Websocket connection instance to get callback for |
| 2311 | */ |
| 2312 | |
| 2313 | int |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2314 | libwebsocket_callback_on_writable(struct libwebsocket_context *context, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2315 | struct libwebsocket *wsi) |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2316 | { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2317 | int n; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2318 | int handled = 0; |
| 2319 | |
| 2320 | /* maybe an extension will take care of it for us */ |
| 2321 | |
| 2322 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 2323 | if (!wsi->active_extensions[n]->callback) |
| 2324 | continue; |
| 2325 | |
| 2326 | handled |= wsi->active_extensions[n]->callback(context, |
| 2327 | wsi->active_extensions[n], wsi, |
| 2328 | LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE, |
| 2329 | wsi->active_extensions_user[n], NULL, 0); |
| 2330 | } |
| 2331 | |
| 2332 | if (handled) |
| 2333 | return 1; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2334 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2335 | for (n = 0; n < context->fds_count; n++) |
| 2336 | if (context->fds[n].fd == wsi->sock) { |
| 2337 | context->fds[n].events |= POLLOUT; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2338 | n = context->fds_count + 1; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2341 | if (n == context->fds_count) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2342 | lwsl_err("libwebsocket_callback_on_writable: " |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2343 | "failed to find socket %d\n", wsi->sock); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2344 | |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 2345 | /* external POLL support via protocol 0 */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2346 | context->protocols[0].callback(context, wsi, |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 2347 | LWS_CALLBACK_SET_MODE_POLL_FD, |
| 2348 | (void *)(long)wsi->sock, NULL, POLLOUT); |
| 2349 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2350 | return 1; |
| 2351 | } |
| 2352 | |
| 2353 | /** |
| 2354 | * libwebsocket_callback_on_writable_all_protocol() - Request a callback for |
| 2355 | * all connections using the given protocol when it |
| 2356 | * becomes possible to write to each socket without |
| 2357 | * blocking in turn. |
| 2358 | * |
| 2359 | * @protocol: Protocol whose connections will get callbacks |
| 2360 | */ |
| 2361 | |
| 2362 | int |
| 2363 | libwebsocket_callback_on_writable_all_protocol( |
| 2364 | const struct libwebsocket_protocols *protocol) |
| 2365 | { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2366 | struct libwebsocket_context *context = protocol->owning_server; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2367 | int n; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2368 | int m; |
| 2369 | struct libwebsocket *wsi; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2370 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2371 | for (n = 0; n < FD_HASHTABLE_MODULUS; n++) { |
| 2372 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2373 | for (m = 0; m < context->fd_hashtable[n].length; m++) { |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2374 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2375 | wsi = context->fd_hashtable[n].wsi[m]; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2376 | |
| 2377 | if (wsi->protocol == protocol) |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2378 | libwebsocket_callback_on_writable(context, wsi); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2379 | } |
| 2380 | } |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2381 | |
| 2382 | return 0; |
| 2383 | } |
| 2384 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 2385 | /** |
| 2386 | * libwebsocket_set_timeout() - marks the wsi as subject to a timeout |
| 2387 | * |
| 2388 | * You will not need this unless you are doing something special |
| 2389 | * |
| 2390 | * @wsi: Websocket connection instance |
| 2391 | * @reason: timeout reason |
| 2392 | * @secs: how many seconds |
| 2393 | */ |
| 2394 | |
| 2395 | void |
| 2396 | libwebsocket_set_timeout(struct libwebsocket *wsi, |
| 2397 | enum pending_timeout reason, int secs) |
| 2398 | { |
| 2399 | struct timeval tv; |
| 2400 | |
| 2401 | gettimeofday(&tv, NULL); |
| 2402 | |
| 2403 | wsi->pending_timeout_limit = tv.tv_sec + secs; |
| 2404 | wsi->pending_timeout = reason; |
| 2405 | } |
| 2406 | |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 2407 | |
| 2408 | /** |
| 2409 | * libwebsocket_get_socket_fd() - returns the socket file descriptor |
| 2410 | * |
| 2411 | * You will not need this unless you are doing something special |
| 2412 | * |
| 2413 | * @wsi: Websocket connection instance |
| 2414 | */ |
| 2415 | |
| 2416 | int |
| 2417 | libwebsocket_get_socket_fd(struct libwebsocket *wsi) |
| 2418 | { |
| 2419 | return wsi->sock; |
| 2420 | } |
| 2421 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2422 | /** |
| 2423 | * libwebsocket_rx_flow_control() - Enable and disable socket servicing for |
| 2424 | * receieved packets. |
| 2425 | * |
| 2426 | * If the output side of a server process becomes choked, this allows flow |
| 2427 | * control for the input side. |
| 2428 | * |
| 2429 | * @wsi: Websocket connection instance to get callback for |
| 2430 | * @enable: 0 = disable read servicing for this connection, 1 = enable |
| 2431 | */ |
| 2432 | |
| 2433 | int |
| 2434 | libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable) |
| 2435 | { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2436 | struct libwebsocket_context *context = wsi->protocol->owning_server; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2437 | int n; |
| 2438 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2439 | for (n = 0; n < context->fds_count; n++) |
| 2440 | if (context->fds[n].fd == wsi->sock) { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2441 | if (enable) |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2442 | context->fds[n].events |= POLLIN; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2443 | else |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2444 | context->fds[n].events &= ~POLLIN; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2445 | |
| 2446 | return 0; |
| 2447 | } |
| 2448 | |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 2449 | if (enable) |
| 2450 | /* external POLL support via protocol 0 */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2451 | context->protocols[0].callback(context, wsi, |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 2452 | LWS_CALLBACK_SET_MODE_POLL_FD, |
| 2453 | (void *)(long)wsi->sock, NULL, POLLIN); |
| 2454 | else |
| 2455 | /* external POLL support via protocol 0 */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2456 | context->protocols[0].callback(context, wsi, |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 2457 | LWS_CALLBACK_CLEAR_MODE_POLL_FD, |
| 2458 | (void *)(long)wsi->sock, NULL, POLLIN); |
| 2459 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2460 | #if 0 |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2461 | lwsl_err("libwebsocket_rx_flow_control unable to find socket\n"); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2462 | #endif |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2463 | return 1; |
| 2464 | } |
| 2465 | |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 2466 | /** |
| 2467 | * libwebsocket_canonical_hostname() - returns this host's hostname |
| 2468 | * |
| 2469 | * This is typically used by client code to fill in the host parameter |
| 2470 | * when making a client connection. You can only call it after the context |
| 2471 | * has been created. |
| 2472 | * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2473 | * @context: Websocket context |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 2474 | */ |
| 2475 | |
| 2476 | |
| 2477 | extern const char * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2478 | libwebsocket_canonical_hostname(struct libwebsocket_context *context) |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 2479 | { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2480 | return (const char *)context->canonical_hostname; |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2484 | static void sigpipe_handler(int x) |
| 2485 | { |
| 2486 | } |
| 2487 | |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 2488 | #ifdef LWS_OPENSSL_SUPPORT |
| 2489 | static int |
| 2490 | OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx) |
| 2491 | { |
| 2492 | |
| 2493 | SSL *ssl; |
| 2494 | int n; |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 2495 | struct libwebsocket_context *context; |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 2496 | |
| 2497 | ssl = X509_STORE_CTX_get_ex_data(x509_ctx, |
| 2498 | SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 2499 | |
| 2500 | /* |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 2501 | * !!! nasty openssl requires the index to come as a library-scope |
| 2502 | * static |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 2503 | */ |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 2504 | context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index); |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2505 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2506 | n = context->protocols[0].callback(NULL, NULL, |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 2507 | LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION, |
| 2508 | x509_ctx, ssl, preverify_ok); |
| 2509 | |
| 2510 | /* convert return code from 0 = OK to 1 = OK */ |
| 2511 | |
| 2512 | if (!n) |
| 2513 | n = 1; |
| 2514 | else |
| 2515 | n = 0; |
| 2516 | |
| 2517 | return n; |
| 2518 | } |
| 2519 | #endif |
| 2520 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2521 | |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 2522 | /** |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2523 | * libwebsocket_create_context() - Create the websocket handler |
| 2524 | * @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] | 2525 | * any port, that's what you want if you are not running a |
| 2526 | * websocket server at all but just using it as a client |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2527 | * @interf: NULL to bind the listen socket to all interfaces, or the |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 2528 | * interface name, eg, "eth2" |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 2529 | * @protocols: Array of structures listing supported protocols and a protocol- |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2530 | * specific callback for each one. The list is ended with an |
| 2531 | * entry that has a NULL callback pointer. |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 2532 | * It's not const because we write the owning_server member |
Andy Green | c511482 | 2011-03-06 10:29:35 +0000 | [diff] [blame] | 2533 | * @extensions: NULL or array of libwebsocket_extension structs listing the |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2534 | * extensions this context supports |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2535 | * @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] | 2536 | * to listen using SSL, set to the filepath to fetch the |
| 2537 | * server cert from, otherwise NULL for unencrypted |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2538 | * @ssl_private_key_filepath: filepath to private key if wanting SSL mode, |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2539 | * else ignored |
David Galeano | 2f82be8 | 2013-01-09 16:25:54 +0800 | [diff] [blame] | 2540 | * @ssl_ca_filepath: CA certificate filepath or NULL |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2541 | * @gid: group id to change to after setting listen socket, or -1. |
| 2542 | * @uid: user id to change to after setting listen socket, or -1. |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 2543 | * @options: 0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK |
Andy Green | 15e31f3 | 2012-10-19 18:36:28 +0800 | [diff] [blame] | 2544 | * @user: optional user pointer that can be recovered via the context |
| 2545 | * pointer using libwebsocket_context_user |
Andy Green | 05464c6 | 2010-11-12 10:44:18 +0000 | [diff] [blame] | 2546 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2547 | * This function creates the listening socket and takes care |
| 2548 | * of all initialization in one step. |
| 2549 | * |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2550 | * After initialization, it returns a struct libwebsocket_context * that |
| 2551 | * represents this server. After calling, user code needs to take care |
| 2552 | * of calling libwebsocket_service() with the context pointer to get the |
| 2553 | * server's sockets serviced. This can be done in the same process context |
| 2554 | * or a forked process, or another thread, |
Andy Green | 05464c6 | 2010-11-12 10:44:18 +0000 | [diff] [blame] | 2555 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2556 | * The protocol callback functions are called for a handful of events |
| 2557 | * including http requests coming in, websocket connections becoming |
| 2558 | * established, and data arriving; it's also called periodically to allow |
| 2559 | * async transmission. |
| 2560 | * |
| 2561 | * HTTP requests are sent always to the FIRST protocol in @protocol, since |
| 2562 | * at that time websocket protocol has not been negotiated. Other |
| 2563 | * protocols after the first one never see any HTTP callack activity. |
| 2564 | * |
| 2565 | * The server created is a simple http server by default; part of the |
| 2566 | * websocket standard is upgrading this http connection to a websocket one. |
| 2567 | * |
| 2568 | * This allows the same server to provide files like scripts and favicon / |
| 2569 | * images or whatever over http and dynamic data over websockets all in |
| 2570 | * one place; they're all handled in the user callback. |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 2571 | */ |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 2572 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2573 | struct libwebsocket_context * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2574 | libwebsocket_create_context(int port, const char *interf, |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2575 | struct libwebsocket_protocols *protocols, |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 2576 | struct libwebsocket_extension *extensions, |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2577 | const char *ssl_cert_filepath, |
| 2578 | const char *ssl_private_key_filepath, |
David Galeano | 2f82be8 | 2013-01-09 16:25:54 +0800 | [diff] [blame] | 2579 | const char *ssl_ca_filepath, |
Alon Levy | 0291eb3 | 2012-10-19 11:21:56 +0200 | [diff] [blame] | 2580 | int gid, int uid, unsigned int options, |
David Galeano | 2f82be8 | 2013-01-09 16:25:54 +0800 | [diff] [blame] | 2581 | void *user) |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 2582 | { |
| 2583 | int n; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2584 | int m; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2585 | int sockfd = 0; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 2586 | int fd; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 2587 | struct sockaddr_in serv_addr, cli_addr; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 2588 | int opt = 1; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2589 | struct libwebsocket_context *context = NULL; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2590 | unsigned int slen; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 2591 | char *p; |
Paulo Roberto Urio | 1e32663 | 2012-06-04 10:52:19 +0800 | [diff] [blame] | 2592 | char hostname[1024] = ""; |
Andy Green | a69f051 | 2012-05-03 12:32:38 +0800 | [diff] [blame] | 2593 | // struct hostent *he; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2594 | struct libwebsocket *wsi; |
Andy Green | a69f051 | 2012-05-03 12:32:38 +0800 | [diff] [blame] | 2595 | struct sockaddr sa; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 2596 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2597 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | f2f54d5 | 2010-11-15 22:08:00 +0000 | [diff] [blame] | 2598 | SSL_METHOD *method; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2599 | char ssl_err_buf[512]; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2600 | #endif |
| 2601 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2602 | lwsl_info("Initial logging level %d\n", log_level); |
| 2603 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2604 | #ifdef _WIN32 |
| 2605 | { |
| 2606 | WORD wVersionRequested; |
| 2607 | WSADATA wsaData; |
| 2608 | int err; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2609 | HMODULE wsdll; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2610 | |
| 2611 | /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */ |
| 2612 | wVersionRequested = MAKEWORD(2, 2); |
| 2613 | |
| 2614 | err = WSAStartup(wVersionRequested, &wsaData); |
| 2615 | if (err != 0) { |
| 2616 | /* Tell the user that we could not find a usable */ |
| 2617 | /* Winsock DLL. */ |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2618 | lwsl_err("WSAStartup failed with error: %d\n", err); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2619 | return NULL; |
| 2620 | } |
David Galeano | 7b11fec | 2011-10-04 19:55:18 +0800 | [diff] [blame] | 2621 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2622 | /* default to a poll() made out of select() */ |
| 2623 | poll = emulated_poll; |
David Galeano | 7b11fec | 2011-10-04 19:55:18 +0800 | [diff] [blame] | 2624 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2625 | /* if windows socket lib available, use his WSAPoll */ |
David Galeano | cb19368 | 2013-01-09 15:29:00 +0800 | [diff] [blame] | 2626 | wsdll = GetModuleHandle(_T("Ws2_32.dll")); |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2627 | if (wsdll) |
| 2628 | poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll"); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2629 | } |
| 2630 | #endif |
| 2631 | |
| 2632 | |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 2633 | context = (struct libwebsocket_context *) malloc(sizeof(struct libwebsocket_context)); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2634 | if (!context) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2635 | lwsl_err("No memory for websocket context\n"); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2636 | return NULL; |
| 2637 | } |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2638 | context->protocols = protocols; |
| 2639 | context->listen_port = port; |
| 2640 | context->http_proxy_port = 0; |
| 2641 | context->http_proxy_address[0] = '\0'; |
| 2642 | context->options = options; |
| 2643 | context->fds_count = 0; |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 2644 | context->extensions = extensions; |
Paulo Roberto Urio | 1e32663 | 2012-06-04 10:52:19 +0800 | [diff] [blame] | 2645 | context->last_timeout_check_s = 0; |
Alon Levy | 0291eb3 | 2012-10-19 11:21:56 +0200 | [diff] [blame] | 2646 | context->user_space = user; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 2647 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2648 | #ifdef WIN32 |
| 2649 | context->fd_random = 0; |
| 2650 | #else |
| 2651 | context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY); |
| 2652 | if (context->fd_random < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2653 | lwsl_err("Unable to open random device %s %d\n", |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2654 | SYSTEM_RANDOM_FILEPATH, context->fd_random); |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame] | 2655 | return NULL; |
| 2656 | } |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2657 | #endif |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame] | 2658 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2659 | #ifdef LWS_OPENSSL_SUPPORT |
| 2660 | context->use_ssl = 0; |
| 2661 | context->ssl_ctx = NULL; |
| 2662 | context->ssl_client_ctx = NULL; |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 2663 | openssl_websocket_private_data_index = 0; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2664 | #endif |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 2665 | |
Andy Green | 788c4a8 | 2012-10-22 12:29:57 +0100 | [diff] [blame] | 2666 | if (options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME) { |
Andy Green | a69f051 | 2012-05-03 12:32:38 +0800 | [diff] [blame] | 2667 | |
Andy Green | 788c4a8 | 2012-10-22 12:29:57 +0100 | [diff] [blame] | 2668 | strcpy(context->canonical_hostname, "unknown"); |
Andy Green | a69f051 | 2012-05-03 12:32:38 +0800 | [diff] [blame] | 2669 | |
Andy Green | 788c4a8 | 2012-10-22 12:29:57 +0100 | [diff] [blame] | 2670 | } else { |
| 2671 | |
| 2672 | /* find canonical hostname */ |
| 2673 | |
| 2674 | hostname[(sizeof hostname) - 1] = '\0'; |
| 2675 | memset(&sa, 0, sizeof(sa)); |
| 2676 | sa.sa_family = AF_INET; |
| 2677 | sa.sa_data[(sizeof sa.sa_data) - 1] = '\0'; |
| 2678 | gethostname(hostname, (sizeof hostname) - 1); |
| 2679 | |
| 2680 | n = 0; |
| 2681 | |
David Galeano | ed3c840 | 2013-01-10 10:45:24 +0800 | [diff] [blame] | 2682 | if (strlen(hostname) < sizeof(sa.sa_data) - 1) { |
Andy Green | 788c4a8 | 2012-10-22 12:29:57 +0100 | [diff] [blame] | 2683 | strcpy(sa.sa_data, hostname); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2684 | // lwsl_debug("my host name is %s\n", sa.sa_data); |
Andy Green | 788c4a8 | 2012-10-22 12:29:57 +0100 | [diff] [blame] | 2685 | n = getnameinfo(&sa, sizeof(sa), hostname, |
| 2686 | (sizeof hostname) - 1, NULL, 0, 0); |
| 2687 | } |
| 2688 | |
| 2689 | if (!n) { |
| 2690 | strncpy(context->canonical_hostname, hostname, |
| 2691 | sizeof context->canonical_hostname - 1); |
| 2692 | context->canonical_hostname[ |
| 2693 | sizeof context->canonical_hostname - 1] = '\0'; |
| 2694 | } else |
| 2695 | strncpy(context->canonical_hostname, hostname, |
| 2696 | sizeof context->canonical_hostname - 1); |
| 2697 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2698 | // lwsl_debug("context->canonical_hostname = %s\n", |
Andy Green | 788c4a8 | 2012-10-22 12:29:57 +0100 | [diff] [blame] | 2699 | // context->canonical_hostname); |
Andy Green | a69f051 | 2012-05-03 12:32:38 +0800 | [diff] [blame] | 2700 | } |
| 2701 | |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 2702 | /* split the proxy ads:port if given */ |
| 2703 | |
| 2704 | p = getenv("http_proxy"); |
| 2705 | if (p) { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2706 | strncpy(context->http_proxy_address, p, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2707 | sizeof context->http_proxy_address - 1); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2708 | context->http_proxy_address[ |
| 2709 | sizeof context->http_proxy_address - 1] = '\0'; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 2710 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2711 | p = strchr(context->http_proxy_address, ':'); |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 2712 | if (p == NULL) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2713 | lwsl_err("http_proxy needs to be ads:port\n"); |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 2714 | return NULL; |
| 2715 | } |
| 2716 | *p = '\0'; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2717 | context->http_proxy_port = atoi(p + 1); |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 2718 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2719 | lwsl_debug("Using proxy %s:%u\n", |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2720 | context->http_proxy_address, |
| 2721 | context->http_proxy_port); |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 2722 | } |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2723 | |
| 2724 | if (port) { |
| 2725 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2726 | #ifdef LWS_OPENSSL_SUPPORT |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2727 | context->use_ssl = ssl_cert_filepath != NULL && |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2728 | ssl_private_key_filepath != NULL; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2729 | if (context->use_ssl) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2730 | lwsl_info(" Compiled with SSL support, using it\n"); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2731 | else |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2732 | lwsl_info(" Compiled with SSL support, not using it\n"); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2733 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2734 | #else |
| 2735 | if (ssl_cert_filepath != NULL && |
| 2736 | ssl_private_key_filepath != NULL) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2737 | lwsl_info(" Not compiled for OpenSSl support!\n"); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2738 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2739 | } |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2740 | lwsl_info(" Compiled without SSL support, " |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2741 | "serving unencrypted\n"); |
| 2742 | #endif |
| 2743 | } |
| 2744 | |
| 2745 | /* ignore SIGPIPE */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2746 | #ifdef WIN32 |
| 2747 | #else |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2748 | signal(SIGPIPE, sigpipe_handler); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2749 | #endif |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2750 | |
| 2751 | |
| 2752 | #ifdef LWS_OPENSSL_SUPPORT |
| 2753 | |
| 2754 | /* basic openssl init */ |
| 2755 | |
| 2756 | SSL_library_init(); |
| 2757 | |
| 2758 | OpenSSL_add_all_algorithms(); |
| 2759 | SSL_load_error_strings(); |
| 2760 | |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 2761 | openssl_websocket_private_data_index = |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 2762 | SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL); |
| 2763 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2764 | /* |
| 2765 | * Firefox insists on SSLv23 not SSLv3 |
| 2766 | * Konq disables SSLv2 by default now, SSLv23 works |
| 2767 | */ |
| 2768 | |
| 2769 | method = (SSL_METHOD *)SSLv23_server_method(); |
| 2770 | if (!method) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2771 | lwsl_err("problem creating ssl method: %s\n", |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2772 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 2773 | return NULL; |
| 2774 | } |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2775 | context->ssl_ctx = SSL_CTX_new(method); /* create context */ |
| 2776 | if (!context->ssl_ctx) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2777 | lwsl_err("problem creating ssl context: %s\n", |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2778 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 2779 | return NULL; |
| 2780 | } |
| 2781 | |
David Galeano | cc148e4 | 2013-01-10 10:18:59 +0800 | [diff] [blame] | 2782 | #ifdef SSL_OP_NO_COMPRESSION |
David Galeano | c72f6f9 | 2013-01-10 10:11:57 +0800 | [diff] [blame] | 2783 | SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION); |
David Galeano | cc148e4 | 2013-01-10 10:18:59 +0800 | [diff] [blame] | 2784 | #endif |
David Galeano | 77a677c | 2013-01-10 10:14:12 +0800 | [diff] [blame] | 2785 | SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE); |
David Galeano | f177f2a | 2013-01-10 10:15:19 +0800 | [diff] [blame] | 2786 | SSL_CTX_set_cipher_list(context->ssl_ctx, CIPHERS_LIST_STRING); |
David Galeano | c72f6f9 | 2013-01-10 10:11:57 +0800 | [diff] [blame] | 2787 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2788 | /* client context */ |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2789 | |
| 2790 | if (port == CONTEXT_PORT_NO_LISTEN) { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2791 | method = (SSL_METHOD *)SSLv23_client_method(); |
| 2792 | if (!method) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2793 | lwsl_err("problem creating ssl method: %s\n", |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2794 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 2795 | return NULL; |
| 2796 | } |
| 2797 | /* create context */ |
| 2798 | context->ssl_client_ctx = SSL_CTX_new(method); |
| 2799 | if (!context->ssl_client_ctx) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2800 | lwsl_err("problem creating ssl context: %s\n", |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2801 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
| 2802 | return NULL; |
| 2803 | } |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2804 | |
David Galeano | cc148e4 | 2013-01-10 10:18:59 +0800 | [diff] [blame] | 2805 | #ifdef SSL_OP_NO_COMPRESSION |
David Galeano | c72f6f9 | 2013-01-10 10:11:57 +0800 | [diff] [blame] | 2806 | SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_NO_COMPRESSION); |
David Galeano | cc148e4 | 2013-01-10 10:18:59 +0800 | [diff] [blame] | 2807 | #endif |
David Galeano | 77a677c | 2013-01-10 10:14:12 +0800 | [diff] [blame] | 2808 | 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] | 2809 | SSL_CTX_set_cipher_list(context->ssl_client_ctx, CIPHERS_LIST_STRING); |
David Galeano | c72f6f9 | 2013-01-10 10:11:57 +0800 | [diff] [blame] | 2810 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2811 | /* openssl init for cert verification (for client sockets) */ |
David Galeano | 2f82be8 | 2013-01-09 16:25:54 +0800 | [diff] [blame] | 2812 | if (!ssl_ca_filepath) { |
| 2813 | if (!SSL_CTX_load_verify_locations( |
| 2814 | context->ssl_client_ctx, NULL, |
| 2815 | LWS_OPENSSL_CLIENT_CERTS)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2816 | lwsl_err( |
David Galeano | 2f82be8 | 2013-01-09 16:25:54 +0800 | [diff] [blame] | 2817 | "Unable to load SSL Client certs from %s " |
| 2818 | "(set by --with-client-cert-dir= in configure) -- " |
| 2819 | " client ssl isn't going to work", |
| 2820 | LWS_OPENSSL_CLIENT_CERTS); |
| 2821 | } else |
| 2822 | if (!SSL_CTX_load_verify_locations( |
| 2823 | context->ssl_client_ctx, ssl_ca_filepath, |
| 2824 | NULL)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2825 | lwsl_err( |
David Galeano | 2f82be8 | 2013-01-09 16:25:54 +0800 | [diff] [blame] | 2826 | "Unable to load SSL Client certs " |
| 2827 | "file from %s -- client ssl isn't " |
| 2828 | "going to work", ssl_ca_filepath); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2829 | |
| 2830 | /* |
| 2831 | * callback allowing user code to load extra verification certs |
| 2832 | * helping the client to verify server identity |
| 2833 | */ |
| 2834 | |
| 2835 | context->protocols[0].callback(context, NULL, |
| 2836 | LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS, |
| 2837 | context->ssl_client_ctx, NULL, 0); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2838 | } |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2839 | |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 2840 | /* as a server, are we requiring clients to identify themselves? */ |
| 2841 | |
| 2842 | if (options & LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) { |
| 2843 | |
| 2844 | /* absolutely require the client cert */ |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2845 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2846 | SSL_CTX_set_verify(context->ssl_ctx, |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 2847 | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, |
| 2848 | OpenSSL_verify_callback); |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 2849 | |
| 2850 | /* |
| 2851 | * give user code a chance to load certs into the server |
| 2852 | * allowing it to verify incoming client certs |
| 2853 | */ |
| 2854 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2855 | context->protocols[0].callback(context, NULL, |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 2856 | LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS, |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2857 | context->ssl_ctx, NULL, 0); |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2860 | if (context->use_ssl) { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2861 | |
| 2862 | /* openssl init for server sockets */ |
| 2863 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2864 | /* set the local certificate from CertFile */ |
David Galeano | 9b3d4b2 | 2013-01-10 10:11:21 +0800 | [diff] [blame] | 2865 | n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx, |
| 2866 | ssl_cert_filepath); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2867 | if (n != 1) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2868 | lwsl_err("problem getting cert '%s': %s\n", |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2869 | ssl_cert_filepath, |
| 2870 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2871 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2872 | } |
| 2873 | /* set the private key from KeyFile */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2874 | if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx, |
| 2875 | ssl_private_key_filepath, SSL_FILETYPE_PEM) != 1) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2876 | lwsl_err("ssl problem getting key '%s': %s\n", |
Andy Green | 018d8eb | 2010-11-08 21:04:23 +0000 | [diff] [blame] | 2877 | ssl_private_key_filepath, |
| 2878 | ERR_error_string(ERR_get_error(), ssl_err_buf)); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2879 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2880 | } |
| 2881 | /* verify private key */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2882 | if (!SSL_CTX_check_private_key(context->ssl_ctx)) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2883 | lwsl_err("Private SSL key doesn't match cert\n"); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2884 | return NULL; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
| 2887 | /* SSL is happy and has a cert it's content with */ |
| 2888 | } |
| 2889 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2890 | |
Andy Green | df73616 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 2891 | /* selftest */ |
| 2892 | |
| 2893 | if (lws_b64_selftest()) |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2894 | return NULL; |
Andy Green | df73616 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 2895 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2896 | /* fd hashtable init */ |
| 2897 | |
| 2898 | for (n = 0; n < FD_HASHTABLE_MODULUS; n++) |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2899 | context->fd_hashtable[n].length = 0; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2900 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2901 | /* set up our external listening socket we serve on */ |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2902 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2903 | if (port) { |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2904 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2905 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
| 2906 | if (sockfd < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2907 | lwsl_err("ERROR opening socket"); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2908 | return NULL; |
| 2909 | } |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 2910 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2911 | /* allow us to restart even if old sockets in TIME_WAIT */ |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2912 | setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, |
| 2913 | (const void *)&opt, sizeof(opt)); |
Andy Green | 6c93955 | 2011-03-08 08:56:57 +0000 | [diff] [blame] | 2914 | |
| 2915 | /* Disable Nagle */ |
| 2916 | opt = 1; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2917 | setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, |
| 2918 | (const void *)&opt, sizeof(opt)); |
Andy Green | 6c93955 | 2011-03-08 08:56:57 +0000 | [diff] [blame] | 2919 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2920 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
| 2921 | serv_addr.sin_family = AF_INET; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2922 | if (interf == NULL) |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 2923 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
| 2924 | else |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2925 | interface_to_sa(interf, &serv_addr, |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 2926 | sizeof(serv_addr)); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2927 | serv_addr.sin_port = htons(port); |
| 2928 | |
| 2929 | n = bind(sockfd, (struct sockaddr *) &serv_addr, |
| 2930 | sizeof(serv_addr)); |
| 2931 | if (n < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2932 | lwsl_err("ERROR on binding to port %d (%d %d)\n", |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2933 | port, n, errno); |
Andy Green | 41c5803 | 2013-01-12 13:21:08 +0800 | [diff] [blame] | 2934 | close(sockfd); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2935 | return NULL; |
| 2936 | } |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2937 | |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 2938 | wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket)); |
Andy Green | 41c5803 | 2013-01-12 13:21:08 +0800 | [diff] [blame] | 2939 | if (wsi == NULL) { |
| 2940 | lwsl_err("Out of mem\n"); |
| 2941 | close(sockfd); |
| 2942 | return NULL; |
| 2943 | } |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 2944 | memset(wsi, 0, sizeof (struct libwebsocket)); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2945 | wsi->sock = sockfd; |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 2946 | wsi->count_active_extensions = 0; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2947 | wsi->mode = LWS_CONNMODE_SERVER_LISTENER; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2948 | insert_wsi(context, wsi); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2949 | |
David Galeano | 3697309 | 2013-01-10 09:58:24 +0800 | [diff] [blame] | 2950 | listen(sockfd, SOMAXCONN); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2951 | lwsl_info(" Listening on port %d\n", port); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2952 | |
| 2953 | /* list in the internal poll array */ |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2954 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2955 | context->fds[context->fds_count].fd = sockfd; |
| 2956 | context->fds[context->fds_count++].events = POLLIN; |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 2957 | |
| 2958 | /* external POLL support via protocol 0 */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2959 | context->protocols[0].callback(context, wsi, |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 2960 | LWS_CALLBACK_ADD_POLL_FD, |
| 2961 | (void *)(long)sockfd, NULL, POLLIN); |
| 2962 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2963 | } |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2964 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2965 | /* |
| 2966 | * drop any root privs for this process |
| 2967 | * to listen on port < 1023 we would have needed root, but now we are |
| 2968 | * listening, we don't want the power for anything else |
| 2969 | */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2970 | #ifdef WIN32 |
| 2971 | #else |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2972 | if (gid != -1) |
| 2973 | if (setgid(gid)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2974 | lwsl_warn("setgid: %s\n", strerror(errno)); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2975 | if (uid != -1) |
| 2976 | if (setuid(uid)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2977 | lwsl_warn("setuid: %s\n", strerror(errno)); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2978 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2979 | |
| 2980 | /* set up our internal broadcast trigger sockets per-protocol */ |
| 2981 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2982 | for (context->count_protocols = 0; |
| 2983 | protocols[context->count_protocols].callback; |
| 2984 | context->count_protocols++) { |
Andy Green | 2d1301e | 2011-05-24 10:14:41 +0100 | [diff] [blame] | 2985 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2986 | lwsl_parser(" Protocol: %s\n", |
| 2987 | protocols[context->count_protocols].name); |
Andy Green | 2d1301e | 2011-05-24 10:14:41 +0100 | [diff] [blame] | 2988 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2989 | protocols[context->count_protocols].owning_server = context; |
| 2990 | protocols[context->count_protocols].protocol_index = |
| 2991 | context->count_protocols; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2992 | |
| 2993 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 2994 | if (fd < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2995 | lwsl_err("ERROR opening socket"); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2996 | return NULL; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2997 | } |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2998 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2999 | /* allow us to restart even if old sockets in TIME_WAIT */ |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 3000 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt, |
| 3001 | sizeof(opt)); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3002 | |
| 3003 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
| 3004 | serv_addr.sin_family = AF_INET; |
| 3005 | serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); |
| 3006 | serv_addr.sin_port = 0; /* pick the port for us */ |
| 3007 | |
| 3008 | n = bind(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); |
| 3009 | if (n < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 3010 | lwsl_err("ERROR on binding to port %d (%d %d)\n", |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3011 | port, n, errno); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 3012 | return NULL; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
| 3015 | slen = sizeof cli_addr; |
| 3016 | n = getsockname(fd, (struct sockaddr *)&cli_addr, &slen); |
| 3017 | if (n < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 3018 | lwsl_err("getsockname failed\n"); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 3019 | return NULL; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3020 | } |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 3021 | protocols[context->count_protocols].broadcast_socket_port = |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3022 | ntohs(cli_addr.sin_port); |
| 3023 | listen(fd, 5); |
| 3024 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 3025 | lwsl_debug(" Protocol %s broadcast socket %d\n", |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 3026 | protocols[context->count_protocols].name, |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3027 | ntohs(cli_addr.sin_port)); |
| 3028 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 3029 | /* dummy wsi per broadcast proxy socket */ |
| 3030 | |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 3031 | wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket)); |
Andy Green | 41c5803 | 2013-01-12 13:21:08 +0800 | [diff] [blame] | 3032 | if (wsi == NULL) { |
| 3033 | lwsl_err("Out of mem\n"); |
| 3034 | close(fd); |
| 3035 | return NULL; |
| 3036 | } |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 3037 | memset(wsi, 0, sizeof (struct libwebsocket)); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 3038 | wsi->sock = fd; |
| 3039 | wsi->mode = LWS_CONNMODE_BROADCAST_PROXY_LISTENER; |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 3040 | wsi->count_active_extensions = 0; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 3041 | /* note which protocol we are proxying */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 3042 | wsi->protocol_index_for_broadcast_proxy = |
| 3043 | context->count_protocols; |
| 3044 | insert_wsi(context, wsi); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 3045 | |
| 3046 | /* list in internal poll array */ |
| 3047 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 3048 | context->fds[context->fds_count].fd = fd; |
| 3049 | context->fds[context->fds_count].events = POLLIN; |
| 3050 | context->fds[context->fds_count].revents = 0; |
| 3051 | context->fds_count++; |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 3052 | |
| 3053 | /* external POLL support via protocol 0 */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 3054 | context->protocols[0].callback(context, wsi, |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 3055 | LWS_CALLBACK_ADD_POLL_FD, |
| 3056 | (void *)(long)fd, NULL, POLLIN); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3057 | } |
| 3058 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 3059 | /* |
| 3060 | * give all extensions a chance to create any per-context |
| 3061 | * allocations they need |
| 3062 | */ |
| 3063 | |
| 3064 | m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT; |
| 3065 | if (port) |
| 3066 | m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT; |
Andrew Chambers | d551217 | 2012-05-20 08:17:09 +0800 | [diff] [blame] | 3067 | |
| 3068 | if (extensions) { |
| 3069 | while (extensions->callback) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 3070 | lwsl_ext(" Extension: %s\n", extensions->name); |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 3071 | extensions->callback(context, extensions, NULL, |
| 3072 | (enum libwebsocket_extension_callback_reasons)m, |
| 3073 | NULL, NULL, 0); |
Andrew Chambers | d551217 | 2012-05-20 08:17:09 +0800 | [diff] [blame] | 3074 | extensions++; |
| 3075 | } |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 3076 | } |
| 3077 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 3078 | return context; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 3079 | } |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3080 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 3081 | |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 3082 | #ifndef LWS_NO_FORK |
| 3083 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 3084 | /** |
| 3085 | * libwebsockets_fork_service_loop() - Optional helper function forks off |
| 3086 | * a process for the websocket server loop. |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 3087 | * You don't have to use this but if not, you |
| 3088 | * have to make sure you are calling |
| 3089 | * libwebsocket_service periodically to service |
| 3090 | * the websocket traffic |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 3091 | * @context: server context returned by creation function |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 3092 | */ |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3093 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 3094 | int |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 3095 | libwebsockets_fork_service_loop(struct libwebsocket_context *context) |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 3096 | { |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 3097 | int fd; |
| 3098 | struct sockaddr_in cli_addr; |
| 3099 | int n; |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 3100 | int p; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3101 | |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 3102 | n = fork(); |
| 3103 | if (n < 0) |
| 3104 | return n; |
| 3105 | |
| 3106 | if (!n) { |
| 3107 | |
| 3108 | /* main process context */ |
| 3109 | |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 3110 | /* |
| 3111 | * set up the proxy sockets to allow broadcast from |
| 3112 | * service process context |
| 3113 | */ |
| 3114 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 3115 | for (p = 0; p < context->count_protocols; p++) { |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 3116 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 3117 | if (fd < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 3118 | lwsl_err("Unable to create socket\n"); |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 3119 | return -1; |
| 3120 | } |
| 3121 | cli_addr.sin_family = AF_INET; |
| 3122 | cli_addr.sin_port = htons( |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 3123 | context->protocols[p].broadcast_socket_port); |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 3124 | cli_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); |
| 3125 | n = connect(fd, (struct sockaddr *)&cli_addr, |
| 3126 | sizeof cli_addr); |
| 3127 | if (n < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 3128 | lwsl_err("Unable to connect to " |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 3129 | "broadcast socket %d, %s\n", |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 3130 | n, strerror(errno)); |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 3131 | return -1; |
| 3132 | } |
| 3133 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 3134 | context->protocols[p].broadcast_socket_user_fd = fd; |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 3135 | } |
| 3136 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 3137 | return 0; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3138 | } |
| 3139 | |
Artem Baguinski | 9153166 | 2011-12-14 22:14:03 +0100 | [diff] [blame] | 3140 | #ifdef HAVE_SYS_PRCTL_H |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3141 | /* we want a SIGHUP when our parent goes down */ |
| 3142 | prctl(PR_SET_PDEATHSIG, SIGHUP); |
Artem Baguinski | 9153166 | 2011-12-14 22:14:03 +0100 | [diff] [blame] | 3143 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3144 | |
| 3145 | /* in this forked process, sit and service websocket connections */ |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 3146 | |
Artem Baguinski | 9153166 | 2011-12-14 22:14:03 +0100 | [diff] [blame] | 3147 | while (1) { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 3148 | if (libwebsocket_service(context, 1000)) |
Andy Green | 3928f61 | 2012-07-20 12:58:38 +0800 | [diff] [blame] | 3149 | break; |
Andy Green | 5e8967a | 2012-10-17 20:10:44 +0800 | [diff] [blame] | 3150 | //#ifndef HAVE_SYS_PRCTL_H |
Artem Baguinski | 9153166 | 2011-12-14 22:14:03 +0100 | [diff] [blame] | 3151 | /* |
| 3152 | * on systems without prctl() (i.e. anything but linux) we can notice that our |
| 3153 | * parent is dead if getppid() returns 1. FIXME apparently this is not true for |
| 3154 | * solaris, could remember ppid right after fork and wait for it to change. |
| 3155 | */ |
| 3156 | |
| 3157 | if (getppid() == 1) |
| 3158 | break; |
Andy Green | 5e8967a | 2012-10-17 20:10:44 +0800 | [diff] [blame] | 3159 | //#endif |
Artem Baguinski | 9153166 | 2011-12-14 22:14:03 +0100 | [diff] [blame] | 3160 | } |
| 3161 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 3162 | |
Andy Green | 3928f61 | 2012-07-20 12:58:38 +0800 | [diff] [blame] | 3163 | return 1; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 3164 | } |
| 3165 | |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 3166 | #endif |
| 3167 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3168 | /** |
| 3169 | * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 3170 | * connection. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3171 | * @wsi: pointer to struct websocket you want to know the protocol of |
| 3172 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 3173 | * |
| 3174 | * 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] | 3175 | * the callback. |
| 3176 | */ |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 3177 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3178 | const struct libwebsocket_protocols * |
| 3179 | libwebsockets_get_protocol(struct libwebsocket *wsi) |
| 3180 | { |
| 3181 | return wsi->protocol; |
| 3182 | } |
| 3183 | |
| 3184 | /** |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 3185 | * libwebsockets_broadcast() - Sends a buffer to the callback for all active |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 3186 | * connections of the given protocol. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3187 | * @protocol: pointer to the protocol you will broadcast to all members of |
| 3188 | * @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] | 3189 | * allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before |
| 3190 | * the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the |
| 3191 | * case you are calling this function from callback context. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3192 | * @len: length of payload data in buf, starting from buf. |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 3193 | * |
| 3194 | * This function allows bulk sending of a packet to every connection using |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3195 | * the given protocol. It does not send the data directly; instead it calls |
| 3196 | * the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback |
| 3197 | * wants to actually send the data for that connection, the callback itself |
| 3198 | * should call libwebsocket_write(). |
| 3199 | * |
| 3200 | * libwebsockets_broadcast() can be called from another fork context without |
| 3201 | * having to take any care about data visibility between the processes, it'll |
| 3202 | * "just work". |
| 3203 | */ |
| 3204 | |
| 3205 | |
| 3206 | int |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 3207 | libwebsockets_broadcast(const struct libwebsocket_protocols *protocol, |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3208 | unsigned char *buf, size_t len) |
| 3209 | { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 3210 | struct libwebsocket_context *context = protocol->owning_server; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3211 | int n; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 3212 | int m; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 3213 | struct libwebsocket *wsi; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3214 | |
| 3215 | if (!protocol->broadcast_socket_user_fd) { |
| 3216 | /* |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 3217 | * We are either running unforked / flat, or we are being |
| 3218 | * called from poll thread context |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3219 | * eg, from a callback. In that case don't use sockets for |
| 3220 | * broadcast IPC (since we can't open a socket connection to |
| 3221 | * a socket listening on our own thread) but directly do the |
| 3222 | * send action. |
| 3223 | * |
| 3224 | * Locking is not needed because we are by definition being |
| 3225 | * called in the poll thread context and are serialized. |
| 3226 | */ |
| 3227 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 3228 | for (n = 0; n < FD_HASHTABLE_MODULUS; n++) { |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3229 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 3230 | for (m = 0; m < context->fd_hashtable[n].length; m++) { |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3231 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 3232 | wsi = context->fd_hashtable[n].wsi[m]; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3233 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 3234 | if (wsi->mode != LWS_CONNMODE_WS_SERVING) |
| 3235 | continue; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3236 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 3237 | /* |
| 3238 | * never broadcast to |
| 3239 | * non-established connections |
| 3240 | */ |
| 3241 | if (wsi->state != WSI_STATE_ESTABLISHED) |
| 3242 | continue; |
| 3243 | |
| 3244 | /* only broadcast to guys using |
| 3245 | * requested protocol |
| 3246 | */ |
| 3247 | if (wsi->protocol != protocol) |
| 3248 | continue; |
| 3249 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 3250 | wsi->protocol->callback(context, wsi, |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 3251 | LWS_CALLBACK_BROADCAST, |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 3252 | wsi->user_space, |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3253 | buf, len); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 3254 | } |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3255 | } |
| 3256 | |
| 3257 | return 0; |
| 3258 | } |
| 3259 | |
Andy Green | 0ca6a17 | 2010-12-19 20:50:01 +0000 | [diff] [blame] | 3260 | /* |
| 3261 | * We're being called from a different process context than the server |
| 3262 | * loop. Instead of broadcasting directly, we send our |
| 3263 | * payload on a socket to do the IPC; the server process will serialize |
| 3264 | * the broadcast action in its main poll() loop. |
| 3265 | * |
| 3266 | * There's one broadcast socket listening for each protocol supported |
| 3267 | * set up when the websocket server initializes |
| 3268 | */ |
| 3269 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 3270 | n = send(protocol->broadcast_socket_user_fd, buf, len, MSG_NOSIGNAL); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 3271 | |
| 3272 | return n; |
| 3273 | } |
Andy Green | 82c3d54 | 2011-03-07 21:16:31 +0000 | [diff] [blame] | 3274 | |
| 3275 | int |
| 3276 | libwebsocket_is_final_fragment(struct libwebsocket *wsi) |
| 3277 | { |
| 3278 | return wsi->final; |
| 3279 | } |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 3280 | |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 3281 | unsigned char |
| 3282 | libwebsocket_get_reserved_bits(struct libwebsocket *wsi) |
| 3283 | { |
| 3284 | return wsi->rsv; |
| 3285 | } |
| 3286 | |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 3287 | void * |
| 3288 | libwebsocket_ensure_user_space(struct libwebsocket *wsi) |
| 3289 | { |
| 3290 | /* allocate the per-connection user memory (if any) */ |
| 3291 | |
| 3292 | if (wsi->protocol->per_session_data_size && !wsi->user_space) { |
| 3293 | wsi->user_space = malloc( |
| 3294 | wsi->protocol->per_session_data_size); |
| 3295 | if (wsi->user_space == NULL) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 3296 | lwsl_err("Out of memory for conn user space\n"); |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 3297 | return NULL; |
| 3298 | } |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 3299 | memset(wsi->user_space, 0, |
| 3300 | wsi->protocol->per_session_data_size); |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 3301 | } |
| 3302 | return wsi->user_space; |
| 3303 | } |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 3304 | |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 3305 | |
| 3306 | static void lwsl_emit_stderr(const char *line) |
| 3307 | { |
| 3308 | fprintf(stderr, "%s", line); |
| 3309 | } |
| 3310 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 3311 | void _lws_log(int filter, const char *format, ...) |
| 3312 | { |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 3313 | char buf[256]; |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 3314 | va_list ap; |
| 3315 | int n; |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 3316 | int pos = 0; |
Andy Green | 8a26509 | 2013-01-12 09:25:07 +0800 | [diff] [blame] | 3317 | struct timeval tv; |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 3318 | |
| 3319 | if (!(log_level & filter)) |
| 3320 | return; |
| 3321 | |
Andy Green | 8a26509 | 2013-01-12 09:25:07 +0800 | [diff] [blame] | 3322 | gettimeofday(&tv, NULL); |
| 3323 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 3324 | for (n = 0; n < LLL_COUNT; n++) |
| 3325 | if (filter == (1 << n)) { |
Andy Green | 8a26509 | 2013-01-12 09:25:07 +0800 | [diff] [blame] | 3326 | pos = sprintf(buf, "[%ld:%04ld] %s: ", tv.tv_sec, |
| 3327 | tv.tv_usec / 100, log_level_names[n]); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 3328 | break; |
| 3329 | } |
| 3330 | |
| 3331 | va_start(ap, format); |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 3332 | vsnprintf(buf + pos, (sizeof buf) - pos, format, ap); |
| 3333 | buf[(sizeof buf) - 1] = '\0'; |
| 3334 | va_end(ap); |
| 3335 | |
| 3336 | lwsl_emit(buf); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 3337 | } |
| 3338 | |
| 3339 | /** |
| 3340 | * lws_set_log_level() - Set the logging bitfield |
| 3341 | * @level: OR together the LLL_ debug contexts you want output from |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 3342 | * @log_emit_function: NULL to leave it as it is, or a user-supplied |
| 3343 | * function to perform log string emission instead of |
| 3344 | * the default stderr one. |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 3345 | * |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 3346 | * log level defaults to "err" and "warn" contexts enabled only and |
| 3347 | * emission on stderr. |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 3348 | */ |
| 3349 | |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 3350 | 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] | 3351 | { |
| 3352 | log_level = level; |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 3353 | if (log_emit_function) |
| 3354 | lwsl_emit = log_emit_function; |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 3355 | } |