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