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