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