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