Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 1 | /* |
| 2 | * libwebsockets - small server side websockets and web server implementation |
| 3 | * |
| 4 | * Copyright (C) 2010-2013 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 |
| 20 | */ |
| 21 | |
| 22 | #include "private-libwebsockets.h" |
| 23 | |
| 24 | #ifdef WIN32 |
| 25 | #include <tchar.h> |
| 26 | #include <io.h> |
| 27 | #else |
| 28 | #ifdef LWS_BUILTIN_GETIFADDRS |
| 29 | #include <getifaddrs.h> |
| 30 | #else |
| 31 | #include <ifaddrs.h> |
| 32 | #endif |
| 33 | #include <sys/un.h> |
| 34 | #include <sys/socket.h> |
| 35 | #include <netdb.h> |
| 36 | #endif |
| 37 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 38 | int lws_client_socket_service(struct libwebsocket_context *context, |
| 39 | struct libwebsocket *wsi, struct pollfd *pollfd) |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 40 | { |
| 41 | int n; |
Andy Green | c97067c | 2013-02-10 11:03:32 +0800 | [diff] [blame] | 42 | char *p = (char *)&context->service_buffer[0]; |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 43 | int len; |
| 44 | char c; |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 45 | |
| 46 | switch (wsi->mode) { |
| 47 | |
| 48 | case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY: |
| 49 | |
| 50 | /* handle proxy hung up on us */ |
| 51 | |
| 52 | if (pollfd->revents & (POLLERR | POLLHUP)) { |
| 53 | |
| 54 | lwsl_warn("Proxy connection %p (fd=%d) dead\n", |
| 55 | (void *)wsi, pollfd->fd); |
| 56 | |
| 57 | libwebsocket_close_and_free_session(context, wsi, |
| 58 | LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | 040d2ef | 2013-01-16 13:40:43 +0800 | [diff] [blame] | 59 | return 0; |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 60 | } |
| 61 | |
Andy Green | c97067c | 2013-02-10 11:03:32 +0800 | [diff] [blame] | 62 | n = recv(wsi->sock, context->service_buffer, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 63 | sizeof(context->service_buffer), 0); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 64 | if (n < 0) { |
| 65 | libwebsocket_close_and_free_session(context, wsi, |
| 66 | LWS_CLOSE_STATUS_NOSTATUS); |
| 67 | lwsl_err("ERROR reading from proxy socket\n"); |
Andy Green | 040d2ef | 2013-01-16 13:40:43 +0800 | [diff] [blame] | 68 | return 0; |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 69 | } |
| 70 | |
Andy Green | c97067c | 2013-02-10 11:03:32 +0800 | [diff] [blame] | 71 | context->service_buffer[13] = '\0'; |
| 72 | if (strcmp((char *)context->service_buffer, "HTTP/1.0 200 ")) { |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 73 | libwebsocket_close_and_free_session(context, wsi, |
| 74 | LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 75 | lwsl_err("ERROR proxy: %s\n", context->service_buffer); |
Andy Green | 040d2ef | 2013-01-16 13:40:43 +0800 | [diff] [blame] | 76 | return 0; |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /* clear his proxy connection timeout */ |
| 80 | |
| 81 | libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0); |
| 82 | |
| 83 | /* fallthru */ |
| 84 | |
| 85 | case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE: |
| 86 | |
| 87 | /* |
| 88 | * we are under PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE |
| 89 | * timeout protection set in client-handshake.c |
| 90 | */ |
| 91 | |
| 92 | #ifdef LWS_OPENSSL_SUPPORT |
| 93 | |
| 94 | /* |
| 95 | * take care of our libwebsocket_callback_on_writable |
| 96 | * happening at a time when there's no real connection yet |
| 97 | */ |
| 98 | |
| 99 | pollfd->events &= ~POLLOUT; |
| 100 | |
| 101 | /* external POLL support via protocol 0 */ |
| 102 | context->protocols[0].callback(context, wsi, |
| 103 | LWS_CALLBACK_CLEAR_MODE_POLL_FD, |
Andy Green | 0c2f4d8 | 2013-02-19 19:19:51 +0800 | [diff] [blame] | 104 | wsi->user_space, (void *)(long)wsi->sock, POLLOUT); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 105 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 106 | /* we can retry this... just cook the SSL BIO the first time */ |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 107 | |
| 108 | if (wsi->use_ssl && !wsi->ssl) { |
| 109 | |
| 110 | wsi->ssl = SSL_new(context->ssl_client_ctx); |
Joakim Soderberg | b378ce9 | 2013-02-06 15:29:18 +0900 | [diff] [blame] | 111 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 112 | #ifdef USE_CYASSL |
| 113 | /* |
| 114 | * CyaSSL does certificate verification differently |
| 115 | * from OpenSSL. |
| 116 | * If we should ignore the certificate, we need to set |
| 117 | * this before SSL_new and SSL_connect is called. |
| 118 | * Otherwise the connect will simply fail with error |
| 119 | * code -155 |
| 120 | */ |
| 121 | if (wsi->use_ssl == 2) |
| 122 | CyaSSL_set_verify(wsi->ssl, |
| 123 | SSL_VERIFY_NONE, NULL); |
| 124 | #endif /* USE_CYASSL */ |
Joakim Soderberg | b378ce9 | 2013-02-06 15:29:18 +0900 | [diff] [blame] | 125 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 126 | wsi->client_bio = |
| 127 | BIO_new_socket(wsi->sock, BIO_NOCLOSE); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 128 | SSL_set_bio(wsi->ssl, wsi->client_bio, wsi->client_bio); |
| 129 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 130 | #ifdef USE_CYASSL |
Joakim Soderberg | b378ce9 | 2013-02-06 15:29:18 +0900 | [diff] [blame] | 131 | CyaSSL_set_using_nonblock(wsi->ssl, 1); |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 132 | #else |
Andy Green | c4d05a5 | 2013-01-28 17:48:21 +0800 | [diff] [blame] | 133 | BIO_set_nbio(wsi->client_bio, 1); /* nonblocking */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 134 | #endif |
Andy Green | c4d05a5 | 2013-01-28 17:48:21 +0800 | [diff] [blame] | 135 | |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 136 | SSL_set_ex_data(wsi->ssl, |
| 137 | openssl_websocket_private_data_index, |
| 138 | context); |
Joakim Soderberg | b378ce9 | 2013-02-06 15:29:18 +0900 | [diff] [blame] | 139 | } |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 140 | |
| 141 | if (wsi->use_ssl) { |
Andy Green | e000a70 | 2013-01-29 12:37:35 +0800 | [diff] [blame] | 142 | lws_latency_pre(context, wsi); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 143 | n = SSL_connect(wsi->ssl); |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 144 | lws_latency(context, wsi, |
| 145 | "SSL_connect LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE", |
| 146 | n, n > 0); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 147 | |
| 148 | if (n < 0) { |
| 149 | n = SSL_get_error(wsi->ssl, n); |
| 150 | |
| 151 | if (n == SSL_ERROR_WANT_READ || |
| 152 | n == SSL_ERROR_WANT_WRITE) { |
| 153 | /* |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 154 | * wants us to retry connect due to |
| 155 | * state of the underlying ssl layer... |
| 156 | * but since it may be stalled on |
| 157 | * blocked write, no incoming data may |
| 158 | * arrive to trigger the retry. |
| 159 | * Force (possibly many times if the SSL |
| 160 | * state persists in returning the |
| 161 | * condition code, but other sockets |
| 162 | * are getting serviced inbetweentimes) |
| 163 | * us to get called back when writable. |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 164 | */ |
| 165 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 166 | lwsl_info( |
| 167 | "SSL_connect WANT_... retrying\n"); |
| 168 | libwebsocket_callback_on_writable( |
| 169 | context, wsi); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 170 | |
| 171 | return 0; /* no error */ |
| 172 | } |
| 173 | n = -1; |
| 174 | } |
| 175 | |
| 176 | if (n <= 0) { |
| 177 | /* |
| 178 | * retry if new data comes until we |
| 179 | * run into the connection timeout or win |
| 180 | */ |
| 181 | |
Joakim Soderberg | b82b0dd | 2013-02-22 09:28:15 +0800 | [diff] [blame] | 182 | lwsl_err("SSL connect error %lu: %s\n", |
| 183 | ERR_get_error(), |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 184 | ERR_error_string(ERR_get_error(), |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 185 | (char *)context->service_buffer)); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | |
Joakim Soderberg | b378ce9 | 2013-02-06 15:29:18 +0900 | [diff] [blame] | 189 | #ifndef USE_CYASSL |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 190 | /* |
| 191 | * See comment above about CyaSSL certificate |
| 192 | * verification |
| 193 | */ |
Andy Green | e000a70 | 2013-01-29 12:37:35 +0800 | [diff] [blame] | 194 | lws_latency_pre(context, wsi); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 195 | n = SSL_get_verify_result(wsi->ssl); |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 196 | lws_latency(context, wsi, |
| 197 | "SSL_get_verify_result LWS_CONNMODE..HANDSHAKE", |
| 198 | n, n > 0); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 199 | if ((n != X509_V_OK) && ( |
| 200 | n != X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT || |
| 201 | wsi->use_ssl != 2)) { |
| 202 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 203 | lwsl_err( |
| 204 | "server's cert didn't look good %d\n", n); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 205 | libwebsocket_close_and_free_session(context, |
| 206 | wsi, LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | 040d2ef | 2013-01-16 13:40:43 +0800 | [diff] [blame] | 207 | return 0; |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 208 | } |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 209 | #endif /* USE_CYASSL */ |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 210 | } else |
| 211 | wsi->ssl = NULL; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 212 | #endif |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 213 | |
| 214 | p = libwebsockets_generate_client_handshake(context, wsi, p); |
Andy Green | 040d2ef | 2013-01-16 13:40:43 +0800 | [diff] [blame] | 215 | if (p == NULL) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 216 | lwsl_err("Failed to generate handshake for client\n"); |
Andy Green | 040d2ef | 2013-01-16 13:40:43 +0800 | [diff] [blame] | 217 | libwebsocket_close_and_free_session(context, wsi, |
| 218 | LWS_CLOSE_STATUS_NOSTATUS); |
| 219 | return 0; |
| 220 | } |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 221 | |
| 222 | /* send our request to the server */ |
| 223 | |
Andy Green | e000a70 | 2013-01-29 12:37:35 +0800 | [diff] [blame] | 224 | lws_latency_pre(context, wsi); |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 225 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 226 | if (wsi->use_ssl) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 227 | n = SSL_write(wsi->ssl, context->service_buffer, |
| 228 | p - (char *)context->service_buffer); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 229 | else |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 230 | #endif |
| 231 | n = send(wsi->sock, context->service_buffer, |
| 232 | p - (char *)context->service_buffer, 0); |
| 233 | lws_latency(context, wsi, |
| 234 | "send or SSL_write LWS_CONNMODE...HANDSHAKE", |
| 235 | n, n >= 0); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 236 | |
| 237 | if (n < 0) { |
| 238 | lwsl_debug("ERROR writing to client socket\n"); |
| 239 | libwebsocket_close_and_free_session(context, wsi, |
| 240 | LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | 040d2ef | 2013-01-16 13:40:43 +0800 | [diff] [blame] | 241 | return 0; |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 242 | } |
| 243 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 244 | wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART; |
| 245 | wsi->u.hdr.lextable_pos = 0; |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 246 | wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY; |
| 247 | libwebsocket_set_timeout(wsi, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 248 | PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE, |
| 249 | AWAITING_TIMEOUT); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 250 | break; |
| 251 | |
| 252 | case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY: |
| 253 | |
| 254 | /* handle server hung up on us */ |
| 255 | |
| 256 | if (pollfd->revents & (POLLERR | POLLHUP)) { |
| 257 | |
| 258 | lwsl_debug("Server connection %p (fd=%d) dead\n", |
| 259 | (void *)wsi, pollfd->fd); |
| 260 | |
| 261 | goto bail3; |
| 262 | } |
| 263 | |
Andy Green | 5a4f3ae | 2013-03-09 09:09:46 +0800 | [diff] [blame] | 264 | if (!(pollfd->revents & POLLIN)) |
| 265 | break; |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 266 | |
| 267 | /* interpret the server response */ |
| 268 | |
| 269 | /* |
| 270 | * HTTP/1.1 101 Switching Protocols |
| 271 | * Upgrade: websocket |
| 272 | * Connection: Upgrade |
| 273 | * Sec-WebSocket-Accept: me89jWimTRKTWwrS3aRrL53YZSo= |
| 274 | * Sec-WebSocket-Nonce: AQIDBAUGBwgJCgsMDQ4PEC== |
| 275 | * Sec-WebSocket-Protocol: chat |
| 276 | */ |
| 277 | |
| 278 | /* |
| 279 | * we have to take some care here to only take from the |
| 280 | * socket bytewise. The browser may (and has been seen to |
| 281 | * in the case that onopen() performs websocket traffic) |
| 282 | * coalesce both handshake response and websocket traffic |
| 283 | * in one packet, since at that point the connection is |
| 284 | * definitively ready from browser pov. |
| 285 | */ |
| 286 | |
| 287 | len = 1; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 288 | while (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE && |
| 289 | len > 0) { |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 290 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 96d882a | 2013-01-30 12:27:27 +0800 | [diff] [blame] | 291 | if (wsi->use_ssl) { |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 292 | len = SSL_read(wsi->ssl, &c, 1); |
Andy Green | 96d882a | 2013-01-30 12:27:27 +0800 | [diff] [blame] | 293 | if (len < 0) { |
| 294 | n = SSL_get_error(wsi->ssl, len); |
| 295 | if (n == SSL_ERROR_WANT_READ || |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 296 | n == SSL_ERROR_WANT_WRITE) |
Andy Green | 96d882a | 2013-01-30 12:27:27 +0800 | [diff] [blame] | 297 | return 0; |
| 298 | } |
| 299 | } else |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 300 | #endif |
| 301 | len = recv(wsi->sock, &c, 1, 0); |
| 302 | |
Andy Green | 090789e | 2013-02-11 20:03:59 +0800 | [diff] [blame] | 303 | if (len < 0) { |
| 304 | lwsl_warn("error on parsing recv\n"); |
Andy Green | 96d882a | 2013-01-30 12:27:27 +0800 | [diff] [blame] | 305 | goto bail3; |
Andy Green | 090789e | 2013-02-11 20:03:59 +0800 | [diff] [blame] | 306 | } |
Andy Green | 96d882a | 2013-01-30 12:27:27 +0800 | [diff] [blame] | 307 | |
Andy Green | 3455e67 | 2013-02-04 08:55:42 +0800 | [diff] [blame] | 308 | if (libwebsocket_parse(wsi, c)) { |
Andy Green | 090789e | 2013-02-11 20:03:59 +0800 | [diff] [blame] | 309 | lwsl_warn("problems parsing header\n"); |
Andy Green | 3455e67 | 2013-02-04 08:55:42 +0800 | [diff] [blame] | 310 | goto bail3; |
| 311 | } |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /* |
| 315 | * hs may also be coming in multiple packets, there is a 5-sec |
| 316 | * libwebsocket timeout still active here too, so if parsing did |
| 317 | * not complete just wait for next packet coming in this state |
| 318 | */ |
| 319 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 320 | if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE) |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 321 | break; |
| 322 | |
| 323 | /* |
| 324 | * otherwise deal with the handshake. If there's any |
| 325 | * packet traffic already arrived we'll trigger poll() again |
| 326 | * right away and deal with it that way |
| 327 | */ |
| 328 | |
| 329 | return lws_client_interpret_server_handshake(context, wsi); |
| 330 | |
| 331 | bail3: |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 332 | lwsl_info( |
| 333 | "closing connection at LWS_CONNMODE...SERVER_REPLY\n"); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 334 | libwebsocket_close_and_free_session(context, wsi, |
| 335 | LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | 040d2ef | 2013-01-16 13:40:43 +0800 | [diff] [blame] | 336 | return 0; |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 337 | |
| 338 | case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT: |
| 339 | lwsl_ext("LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT\n"); |
| 340 | break; |
| 341 | |
| 342 | case LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD: |
| 343 | lwsl_ext("LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD\n"); |
| 344 | break; |
| 345 | default: |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | |
| 353 | /* |
| 354 | * In-place str to lower case |
| 355 | */ |
| 356 | |
| 357 | static void |
| 358 | strtolower(char *s) |
| 359 | { |
| 360 | while (*s) { |
| 361 | *s = tolower(*s); |
| 362 | s++; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | int |
| 367 | lws_client_interpret_server_handshake(struct libwebsocket_context *context, |
| 368 | struct libwebsocket *wsi) |
| 369 | { |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 370 | const char *pc; |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 371 | int okay = 0; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 372 | char *p; |
| 373 | int len; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 374 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 375 | char ext_name[128]; |
| 376 | struct libwebsocket_extension *ext; |
| 377 | void *v; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 378 | int more = 1; |
| 379 | const char *c; |
| 380 | #endif |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 381 | int n; |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 382 | |
| 383 | /* |
| 384 | * well, what the server sent looked reasonable for syntax. |
| 385 | * Now let's confirm it sent all the necessary headers |
| 386 | */ |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 387 | |
Andy Green | 090789e | 2013-02-11 20:03:59 +0800 | [diff] [blame] | 388 | if (lws_hdr_total_length(wsi, WSI_TOKEN_ACCEPT) == 0) { |
| 389 | lwsl_info("no ACCEPT\n"); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 390 | goto bail3; |
Andy Green | 090789e | 2013-02-11 20:03:59 +0800 | [diff] [blame] | 391 | } |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 392 | |
| 393 | p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP); |
Andy Green | 090789e | 2013-02-11 20:03:59 +0800 | [diff] [blame] | 394 | if (!p) { |
| 395 | lwsl_info("no URI\n"); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 396 | goto bail3; |
Andy Green | 090789e | 2013-02-11 20:03:59 +0800 | [diff] [blame] | 397 | } |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 398 | if (p && strncmp(p, "101", 3)) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 399 | lwsl_warn( |
| 400 | "lws_client_handshake: got bad HTTP response '%s'\n", p); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 401 | goto bail3; |
| 402 | } |
| 403 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 404 | p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE); |
Andy Green | 090789e | 2013-02-11 20:03:59 +0800 | [diff] [blame] | 405 | if (!p) { |
| 406 | lwsl_info("no UPGRADE\n"); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 407 | goto bail3; |
Andy Green | 090789e | 2013-02-11 20:03:59 +0800 | [diff] [blame] | 408 | } |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 409 | strtolower(p); |
| 410 | if (strcmp(p, "websocket")) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 411 | lwsl_warn( |
| 412 | "lws_client_handshake: got bad Upgrade header '%s'\n", p); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 413 | goto bail3; |
| 414 | } |
| 415 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 416 | p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_CONNECTION); |
Andy Green | 090789e | 2013-02-11 20:03:59 +0800 | [diff] [blame] | 417 | if (!p) { |
| 418 | lwsl_info("no Connection hdr\n"); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 419 | goto bail3; |
Andy Green | 090789e | 2013-02-11 20:03:59 +0800 | [diff] [blame] | 420 | } |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 421 | strtolower(p); |
| 422 | if (strcmp(p, "upgrade")) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 423 | lwsl_warn("lws_client_int_s_hs: bad header %s\n", p); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 424 | goto bail3; |
| 425 | } |
| 426 | |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 427 | pc = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 428 | if (pc == NULL) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 429 | lwsl_parser("lws_client_int_s_hs: no protocol list\n"); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 430 | else |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 431 | lwsl_parser("lws_client_int_s_hs: protocol list '%s'\n", pc); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 432 | |
| 433 | /* |
| 434 | * confirm the protocol the server wants to talk was in the list |
| 435 | * of protocols we offered |
| 436 | */ |
| 437 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 438 | len = lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL); |
| 439 | if (!len) { |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 440 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 441 | lwsl_info("lws_client_int_s_hs: WSI_TOKEN_PROTOCOL is null\n"); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 442 | /* |
| 443 | * no protocol name to work from, |
| 444 | * default to first protocol |
| 445 | */ |
| 446 | wsi->protocol = &context->protocols[0]; |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 447 | goto check_extensions; |
| 448 | } |
| 449 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 450 | p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL); |
| 451 | len = strlen(p); |
| 452 | |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 453 | while (*pc && !okay) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 454 | if (!strncmp(pc, p, len) && |
| 455 | (pc[len] == ',' || pc[len] == '\0')) { |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 456 | okay = 1; |
| 457 | continue; |
| 458 | } |
| 459 | while (*pc && *pc != ',') |
| 460 | pc++; |
| 461 | while (*pc && *pc != ' ') |
| 462 | pc++; |
| 463 | } |
| 464 | |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 465 | if (!okay) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 466 | lwsl_err("lws_client_int_s_hs: got bad protocol %s\n", p); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 467 | goto bail2; |
| 468 | } |
| 469 | |
| 470 | /* |
| 471 | * identify the selected protocol struct and set it |
| 472 | */ |
| 473 | n = 0; |
| 474 | wsi->protocol = NULL; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 475 | while (context->protocols[n].callback && !wsi->protocol) { |
| 476 | if (strcmp(p, context->protocols[n].name) == 0) { |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 477 | wsi->protocol = &context->protocols[n]; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 478 | break; |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 479 | } |
| 480 | n++; |
| 481 | } |
| 482 | |
| 483 | if (wsi->protocol == NULL) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 484 | lwsl_err("lws_client_int_s_hs: fail protocol %s\n", p); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 485 | goto bail2; |
| 486 | } |
| 487 | |
| 488 | |
| 489 | check_extensions: |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 490 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 491 | /* instantiate the accepted extensions */ |
| 492 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 493 | if (!lws_hdr_total_length(wsi, WSI_TOKEN_EXTENSIONS)) { |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 494 | lwsl_ext("no client extenstions allowed by server\n"); |
| 495 | goto check_accept; |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | * break down the list of server accepted extensions |
| 500 | * and go through matching them or identifying bogons |
| 501 | */ |
| 502 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 503 | if (lws_hdr_copy(wsi, (char *)context->service_buffer, |
Andy Green | 090789e | 2013-02-11 20:03:59 +0800 | [diff] [blame] | 504 | sizeof(context->service_buffer), WSI_TOKEN_EXTENSIONS) < 0) { |
| 505 | lwsl_warn("ext list from server failed to copy\n"); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 506 | goto bail2; |
Andy Green | 090789e | 2013-02-11 20:03:59 +0800 | [diff] [blame] | 507 | } |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 508 | |
| 509 | c = (char *)context->service_buffer; |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 510 | n = 0; |
| 511 | while (more) { |
| 512 | |
| 513 | if (*c && (*c != ',' && *c != ' ' && *c != '\t')) { |
| 514 | ext_name[n] = *c++; |
| 515 | if (n < sizeof(ext_name) - 1) |
| 516 | n++; |
| 517 | continue; |
| 518 | } |
| 519 | ext_name[n] = '\0'; |
| 520 | if (!*c) |
| 521 | more = 0; |
| 522 | else { |
| 523 | c++; |
| 524 | if (!n) |
| 525 | continue; |
| 526 | } |
| 527 | |
| 528 | /* check we actually support it */ |
| 529 | |
| 530 | lwsl_ext("checking client ext %s\n", ext_name); |
| 531 | |
| 532 | n = 0; |
| 533 | ext = wsi->protocol->owning_server->extensions; |
| 534 | while (ext && ext->callback) { |
| 535 | |
| 536 | if (strcmp(ext_name, ext->name)) { |
| 537 | ext++; |
| 538 | continue; |
| 539 | } |
| 540 | |
| 541 | n = 1; |
| 542 | |
| 543 | lwsl_ext("instantiating client ext %s\n", ext_name); |
| 544 | |
| 545 | /* instantiate the extension on this conn */ |
| 546 | |
| 547 | wsi->active_extensions_user[ |
| 548 | wsi->count_active_extensions] = |
| 549 | malloc(ext->per_session_data_size); |
| 550 | if (wsi->active_extensions_user[ |
| 551 | wsi->count_active_extensions] == NULL) { |
| 552 | lwsl_err("Out of mem\n"); |
| 553 | goto bail2; |
| 554 | } |
| 555 | memset(wsi->active_extensions_user[ |
| 556 | wsi->count_active_extensions], 0, |
| 557 | ext->per_session_data_size); |
| 558 | wsi->active_extensions[ |
| 559 | wsi->count_active_extensions] = ext; |
| 560 | |
| 561 | /* allow him to construct his context */ |
| 562 | |
| 563 | ext->callback(wsi->protocol->owning_server, |
| 564 | ext, wsi, |
| 565 | LWS_EXT_CALLBACK_CLIENT_CONSTRUCT, |
| 566 | wsi->active_extensions_user[ |
| 567 | wsi->count_active_extensions], |
| 568 | NULL, 0); |
| 569 | |
| 570 | wsi->count_active_extensions++; |
| 571 | |
| 572 | ext++; |
| 573 | } |
| 574 | |
| 575 | if (n == 0) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 576 | lwsl_warn("Unknown ext '%s'!\n", ext_name); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 577 | goto bail2; |
| 578 | } |
| 579 | |
| 580 | n = 0; |
| 581 | } |
| 582 | |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 583 | check_accept: |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 584 | #endif |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 585 | |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 586 | /* |
| 587 | * Confirm his accept token is the one we precomputed |
| 588 | */ |
| 589 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 590 | p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_ACCEPT); |
Andy Green | a7521de | 2013-02-18 10:38:45 +0800 | [diff] [blame] | 591 | if (strcmp(p, wsi->u.hdr.ah->initial_handshake_hash_base64)) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 592 | lwsl_warn("lws_client_int_s_hs: accept %s wrong vs %s\n", p, |
Andy Green | a7521de | 2013-02-18 10:38:45 +0800 | [diff] [blame] | 593 | wsi->u.hdr.ah->initial_handshake_hash_base64); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 594 | goto bail2; |
| 595 | } |
| 596 | |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 597 | /* allocate the per-connection user memory (if any) */ |
Andy Green | 2af4d5b | 2013-02-18 16:30:10 +0800 | [diff] [blame] | 598 | if (libwebsocket_ensure_user_space(wsi)) { |
Andy Green | 090789e | 2013-02-11 20:03:59 +0800 | [diff] [blame] | 599 | lwsl_err("Problem allocating wsi user mem\n"); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 600 | goto bail2; |
Andy Green | 090789e | 2013-02-11 20:03:59 +0800 | [diff] [blame] | 601 | } |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 602 | |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 603 | /* |
| 604 | * we seem to be good to go, give client last chance to check |
| 605 | * headers and OK it |
| 606 | */ |
| 607 | |
| 608 | wsi->protocol->callback(context, wsi, |
| 609 | LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH, |
| 610 | wsi->user_space, NULL, 0); |
| 611 | |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 612 | /* clear his proxy connection timeout */ |
| 613 | |
| 614 | libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0); |
| 615 | |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 616 | /* free up his parsing allocations */ |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 617 | if (wsi->u.hdr.ah) |
| 618 | free(wsi->u.hdr.ah); |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 619 | |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 620 | /* mark him as being alive */ |
| 621 | |
| 622 | wsi->state = WSI_STATE_ESTABLISHED; |
| 623 | wsi->mode = LWS_CONNMODE_WS_CLIENT; |
| 624 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 625 | /* union transition */ |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 626 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 627 | memset(&wsi->u, 0, sizeof(wsi->u)); |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 628 | |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 629 | /* |
| 630 | * create the frame buffer for this connection according to the |
| 631 | * size mentioned in the protocol definition. If 0 there, then |
| 632 | * use a big default for compatibility |
| 633 | */ |
| 634 | |
| 635 | n = wsi->protocol->rx_buffer_size; |
| 636 | if (!n) |
| 637 | n = LWS_MAX_SOCKET_IO_BUF; |
| 638 | n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING; |
| 639 | wsi->u.ws.rx_user_buffer = malloc(n); |
| 640 | if (!wsi->u.ws.rx_user_buffer) { |
| 641 | lwsl_err("Out of Mem allocating rx buffer %d\n", n); |
| 642 | goto bail3; |
| 643 | } |
| 644 | lwsl_info("Allocating client RX buffer %d\n", n); |
| 645 | |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 646 | lwsl_debug("handshake OK for protocol %s\n", wsi->protocol->name); |
| 647 | |
| 648 | /* call him back to inform him he is up */ |
| 649 | |
| 650 | wsi->protocol->callback(context, wsi, |
| 651 | LWS_CALLBACK_CLIENT_ESTABLISHED, |
| 652 | wsi->user_space, NULL, 0); |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 653 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 654 | /* |
| 655 | * inform all extensions, not just active ones since they |
| 656 | * already know |
| 657 | */ |
| 658 | |
| 659 | ext = context->extensions; |
| 660 | |
| 661 | while (ext && ext->callback) { |
| 662 | v = NULL; |
| 663 | for (n = 0; n < wsi->count_active_extensions; n++) |
| 664 | if (wsi->active_extensions[n] == ext) |
| 665 | v = wsi->active_extensions_user[n]; |
| 666 | |
| 667 | ext->callback(context, ext, wsi, |
| 668 | LWS_EXT_CALLBACK_ANY_WSI_ESTABLISHED, v, NULL, 0); |
| 669 | ext++; |
| 670 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 671 | #endif |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 672 | |
| 673 | return 0; |
| 674 | |
| 675 | bail3: |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 676 | |
| 677 | bail2: |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 678 | if (wsi->protocol) |
| 679 | wsi->protocol->callback(context, wsi, |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 680 | LWS_CALLBACK_CLIENT_CONNECTION_ERROR, |
| 681 | wsi->user_space, NULL, 0); |
| 682 | |
Andy Green | 96d882a | 2013-01-30 12:27:27 +0800 | [diff] [blame] | 683 | lwsl_info("closing connection due to bail2 connection error\n"); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 684 | |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 685 | /* free up his parsing allocations */ |
| 686 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 687 | if (wsi->u.hdr.ah) |
| 688 | free(wsi->u.hdr.ah); |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 689 | |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 690 | libwebsocket_close_and_free_session(context, wsi, |
Andy Green | 96d882a | 2013-01-30 12:27:27 +0800 | [diff] [blame] | 691 | LWS_CLOSE_STATUS_PROTOCOL_ERR); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 692 | |
| 693 | return 1; |
| 694 | } |
| 695 | |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 696 | |
| 697 | char * |
| 698 | libwebsockets_generate_client_handshake(struct libwebsocket_context *context, |
| 699 | struct libwebsocket *wsi, char *pkt) |
| 700 | { |
Andy Green | c97067c | 2013-02-10 11:03:32 +0800 | [diff] [blame] | 701 | char buf[128]; |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 702 | char hash[20]; |
Andy Green | 80f168b | 2013-01-21 11:04:49 +0800 | [diff] [blame] | 703 | char key_b64[40]; |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 704 | char *p = pkt; |
| 705 | int n; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 706 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 707 | struct libwebsocket_extension *ext; |
| 708 | struct libwebsocket_extension *ext1; |
| 709 | int ext_count = 0; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 710 | #endif |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 711 | |
| 712 | /* |
| 713 | * create the random key |
| 714 | */ |
| 715 | |
| 716 | n = libwebsockets_get_random(context, hash, 16); |
| 717 | if (n != 16) { |
| 718 | lwsl_err("Unable to read from random dev %s\n", |
| 719 | SYSTEM_RANDOM_FILEPATH); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 720 | libwebsocket_close_and_free_session(context, wsi, |
| 721 | LWS_CLOSE_STATUS_NOSTATUS); |
| 722 | return NULL; |
| 723 | } |
| 724 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 725 | lws_b64_encode_string(hash, 16, key_b64, sizeof(key_b64)); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 726 | |
| 727 | /* |
| 728 | * 00 example client handshake |
| 729 | * |
| 730 | * GET /socket.io/websocket HTTP/1.1 |
| 731 | * Upgrade: WebSocket |
| 732 | * Connection: Upgrade |
| 733 | * Host: 127.0.0.1:9999 |
| 734 | * Origin: http://127.0.0.1 |
| 735 | * Sec-WebSocket-Key1: 1 0 2#0W 9 89 7 92 ^ |
| 736 | * Sec-WebSocket-Key2: 7 7Y 4328 B2v[8(z1 |
| 737 | * Cookie: socketio=websocket |
| 738 | * |
| 739 | * (Á®Ä0¶†≥ |
| 740 | * |
| 741 | * 04 example client handshake |
| 742 | * |
| 743 | * GET /chat HTTP/1.1 |
| 744 | * Host: server.example.com |
| 745 | * Upgrade: websocket |
| 746 | * Connection: Upgrade |
| 747 | * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== |
| 748 | * Sec-WebSocket-Origin: http://example.com |
| 749 | * Sec-WebSocket-Protocol: chat, superchat |
| 750 | * Sec-WebSocket-Version: 4 |
| 751 | */ |
| 752 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 753 | p += sprintf(p, "GET %s HTTP/1.1\x0d\x0a", |
| 754 | lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_URI)); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 755 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 756 | p += sprintf(p, |
| 757 | "Pragma: no-cache\x0d\x0a""Cache-Control: no-cache\x0d\x0a"); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 758 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 759 | p += sprintf(p, "Host: %s\x0d\x0a", |
| 760 | lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_HOST)); |
| 761 | p += sprintf(p, |
Andy Green | 090789e | 2013-02-11 20:03:59 +0800 | [diff] [blame] | 762 | "Upgrade: websocket\x0d\x0a""Connection: Upgrade\x0d\x0a""Sec-WebSocket-Key: "); |
Andy Green | 80f168b | 2013-01-21 11:04:49 +0800 | [diff] [blame] | 763 | strcpy(p, key_b64); |
| 764 | p += strlen(key_b64); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 765 | p += sprintf(p, "\x0d\x0a"); |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 766 | if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN)) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 767 | p += sprintf(p, "Origin: %s\x0d\x0a", |
| 768 | lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN)); |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 769 | |
| 770 | if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS)) |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 771 | p += sprintf(p, "Sec-WebSocket-Protocol: %s\x0d\x0a", |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 772 | lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS)); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 773 | |
| 774 | /* tell the server what extensions we could support */ |
| 775 | |
| 776 | p += sprintf(p, "Sec-WebSocket-Extensions: "); |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 777 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 778 | ext = context->extensions; |
| 779 | while (ext && ext->callback) { |
| 780 | |
| 781 | n = 0; |
| 782 | ext1 = context->extensions; |
| 783 | |
| 784 | while (ext1 && ext1->callback) { |
| 785 | n |= ext1->callback(context, ext1, wsi, |
| 786 | LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION, |
| 787 | NULL, (char *)ext->name, 0); |
| 788 | |
| 789 | ext1++; |
| 790 | } |
| 791 | |
| 792 | if (n) { /* an extension vetos us */ |
| 793 | lwsl_ext("ext %s vetoed\n", (char *)ext->name); |
| 794 | ext++; |
| 795 | continue; |
| 796 | } |
| 797 | |
| 798 | n = context->protocols[0].callback(context, wsi, |
| 799 | LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED, |
| 800 | wsi->user_space, (char *)ext->name, 0); |
| 801 | |
| 802 | /* |
| 803 | * zero return from callback means |
| 804 | * go ahead and allow the extension, |
| 805 | * it's what we get if the callback is |
| 806 | * unhandled |
| 807 | */ |
| 808 | |
| 809 | if (n) { |
| 810 | ext++; |
| 811 | continue; |
| 812 | } |
| 813 | |
| 814 | /* apply it */ |
| 815 | |
| 816 | if (ext_count) |
| 817 | *p++ = ','; |
| 818 | p += sprintf(p, "%s", ext->name); |
| 819 | ext_count++; |
| 820 | |
| 821 | ext++; |
| 822 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 823 | #endif |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 824 | p += sprintf(p, "\x0d\x0a"); |
| 825 | |
| 826 | if (wsi->ietf_spec_revision) |
| 827 | p += sprintf(p, "Sec-WebSocket-Version: %d\x0d\x0a", |
| 828 | wsi->ietf_spec_revision); |
| 829 | |
| 830 | /* give userland a chance to append, eg, cookies */ |
| 831 | |
| 832 | context->protocols[0].callback(context, wsi, |
| 833 | LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER, |
Andy Green | c97067c | 2013-02-10 11:03:32 +0800 | [diff] [blame] | 834 | NULL, &p, (pkt + sizeof(context->service_buffer)) - p - 12); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 835 | |
| 836 | p += sprintf(p, "\x0d\x0a"); |
| 837 | |
| 838 | /* prepare the expected server accept response */ |
| 839 | |
Andy Green | cecf5e7 | 2013-02-12 10:07:22 +0800 | [diff] [blame] | 840 | key_b64[39] = '\0'; /* enforce composed length below buf sizeof */ |
| 841 | n = sprintf(buf, "%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11", key_b64); |
| 842 | |
Andy Green | c97067c | 2013-02-10 11:03:32 +0800 | [diff] [blame] | 843 | SHA1((unsigned char *)buf, n, (unsigned char *)hash); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 844 | |
| 845 | lws_b64_encode_string(hash, 20, |
Andy Green | a7521de | 2013-02-18 10:38:45 +0800 | [diff] [blame] | 846 | wsi->u.hdr.ah->initial_handshake_hash_base64, |
| 847 | sizeof(wsi->u.hdr.ah->initial_handshake_hash_base64)); |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 848 | |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 849 | return p; |
| 850 | } |
| 851 | |