blob: cd791198968e1675cc8f9aa8a8da15a841f6312f [file] [log] [blame]
Andy Green76f61e72013-01-16 11:53:05 +08001/*
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 Greenb5b23192013-02-11 17:13:32 +080038int lws_client_socket_service(struct libwebsocket_context *context,
39 struct libwebsocket *wsi, struct pollfd *pollfd)
Andy Green76f61e72013-01-16 11:53:05 +080040{
41 int n;
Andy Greenc97067c2013-02-10 11:03:32 +080042 char *p = (char *)&context->service_buffer[0];
Andy Green76f61e72013-01-16 11:53:05 +080043 int len;
44 char c;
Andy Green76f61e72013-01-16 11:53:05 +080045
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 Green040d2ef2013-01-16 13:40:43 +080059 return 0;
Andy Green76f61e72013-01-16 11:53:05 +080060 }
61
Andy Greenc97067c2013-02-10 11:03:32 +080062 n = recv(wsi->sock, context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +080063 sizeof(context->service_buffer), 0);
Andy Green76f61e72013-01-16 11:53:05 +080064 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 Green040d2ef2013-01-16 13:40:43 +080068 return 0;
Andy Green76f61e72013-01-16 11:53:05 +080069 }
70
Andy Greenc97067c2013-02-10 11:03:32 +080071 context->service_buffer[13] = '\0';
72 if (strcmp((char *)context->service_buffer, "HTTP/1.0 200 ")) {
Andy Green76f61e72013-01-16 11:53:05 +080073 libwebsocket_close_and_free_session(context, wsi,
74 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenb5b23192013-02-11 17:13:32 +080075 lwsl_err("ERROR proxy: %s\n", context->service_buffer);
Andy Green040d2ef2013-01-16 13:40:43 +080076 return 0;
Andy Green76f61e72013-01-16 11:53:05 +080077 }
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 Green0c2f4d82013-02-19 19:19:51 +0800104 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Green76f61e72013-01-16 11:53:05 +0800105
Andy Greenb5b23192013-02-11 17:13:32 +0800106 /* we can retry this... just cook the SSL BIO the first time */
Andy Green76f61e72013-01-16 11:53:05 +0800107
108 if (wsi->use_ssl && !wsi->ssl) {
109
110 wsi->ssl = SSL_new(context->ssl_client_ctx);
Joakim Soderbergb378ce92013-02-06 15:29:18 +0900111
Andy Greenb5b23192013-02-11 17:13:32 +0800112#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 Soderbergb378ce92013-02-06 15:29:18 +0900125
Andy Greenb5b23192013-02-11 17:13:32 +0800126 wsi->client_bio =
127 BIO_new_socket(wsi->sock, BIO_NOCLOSE);
Andy Green76f61e72013-01-16 11:53:05 +0800128 SSL_set_bio(wsi->ssl, wsi->client_bio, wsi->client_bio);
129
Andy Greenb5b23192013-02-11 17:13:32 +0800130#ifdef USE_CYASSL
Joakim Soderbergb378ce92013-02-06 15:29:18 +0900131 CyaSSL_set_using_nonblock(wsi->ssl, 1);
Andy Greenb5b23192013-02-11 17:13:32 +0800132#else
Andy Greenc4d05a52013-01-28 17:48:21 +0800133 BIO_set_nbio(wsi->client_bio, 1); /* nonblocking */
Andy Greenb5b23192013-02-11 17:13:32 +0800134#endif
Andy Greenc4d05a52013-01-28 17:48:21 +0800135
Andy Green76f61e72013-01-16 11:53:05 +0800136 SSL_set_ex_data(wsi->ssl,
137 openssl_websocket_private_data_index,
138 context);
Joakim Soderbergb378ce92013-02-06 15:29:18 +0900139 }
Andy Green76f61e72013-01-16 11:53:05 +0800140
141 if (wsi->use_ssl) {
Andy Greene000a702013-01-29 12:37:35 +0800142 lws_latency_pre(context, wsi);
Andy Green76f61e72013-01-16 11:53:05 +0800143 n = SSL_connect(wsi->ssl);
Andy Greenb5b23192013-02-11 17:13:32 +0800144 lws_latency(context, wsi,
145 "SSL_connect LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE",
146 n, n > 0);
Andy Green76f61e72013-01-16 11:53:05 +0800147
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 Greenb5b23192013-02-11 17:13:32 +0800154 * 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 Green76f61e72013-01-16 11:53:05 +0800164 */
165
Andy Greenb5b23192013-02-11 17:13:32 +0800166 lwsl_info(
167 "SSL_connect WANT_... retrying\n");
168 libwebsocket_callback_on_writable(
169 context, wsi);
Andy Green76f61e72013-01-16 11:53:05 +0800170
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 Soderbergb82b0dd2013-02-22 09:28:15 +0800182 lwsl_err("SSL connect error %lu: %s\n",
183 ERR_get_error(),
Andy Green76f61e72013-01-16 11:53:05 +0800184 ERR_error_string(ERR_get_error(),
Andy Greenb5b23192013-02-11 17:13:32 +0800185 (char *)context->service_buffer));
Andy Green76f61e72013-01-16 11:53:05 +0800186 return 0;
187 }
188
Joakim Soderbergb378ce92013-02-06 15:29:18 +0900189 #ifndef USE_CYASSL
Andy Greenb5b23192013-02-11 17:13:32 +0800190 /*
191 * See comment above about CyaSSL certificate
192 * verification
193 */
Andy Greene000a702013-01-29 12:37:35 +0800194 lws_latency_pre(context, wsi);
Andy Green76f61e72013-01-16 11:53:05 +0800195 n = SSL_get_verify_result(wsi->ssl);
Andy Greenb5b23192013-02-11 17:13:32 +0800196 lws_latency(context, wsi,
197 "SSL_get_verify_result LWS_CONNMODE..HANDSHAKE",
198 n, n > 0);
Andy Green76f61e72013-01-16 11:53:05 +0800199 if ((n != X509_V_OK) && (
200 n != X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT ||
201 wsi->use_ssl != 2)) {
202
Andy Greenb5b23192013-02-11 17:13:32 +0800203 lwsl_err(
204 "server's cert didn't look good %d\n", n);
Andy Green76f61e72013-01-16 11:53:05 +0800205 libwebsocket_close_and_free_session(context,
206 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800207 return 0;
Andy Green76f61e72013-01-16 11:53:05 +0800208 }
Andy Greenb5b23192013-02-11 17:13:32 +0800209#endif /* USE_CYASSL */
Andy Green76f61e72013-01-16 11:53:05 +0800210 } else
211 wsi->ssl = NULL;
Andy Greenb5b23192013-02-11 17:13:32 +0800212#endif
Andy Green76f61e72013-01-16 11:53:05 +0800213
214 p = libwebsockets_generate_client_handshake(context, wsi, p);
Andy Green040d2ef2013-01-16 13:40:43 +0800215 if (p == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +0800216 lwsl_err("Failed to generate handshake for client\n");
Andy Green040d2ef2013-01-16 13:40:43 +0800217 libwebsocket_close_and_free_session(context, wsi,
218 LWS_CLOSE_STATUS_NOSTATUS);
219 return 0;
220 }
Andy Green76f61e72013-01-16 11:53:05 +0800221
222 /* send our request to the server */
223
Andy Greene000a702013-01-29 12:37:35 +0800224 lws_latency_pre(context, wsi);
Andy Greenb5b23192013-02-11 17:13:32 +0800225#ifdef LWS_OPENSSL_SUPPORT
Andy Green76f61e72013-01-16 11:53:05 +0800226 if (wsi->use_ssl)
Andy Greenb5b23192013-02-11 17:13:32 +0800227 n = SSL_write(wsi->ssl, context->service_buffer,
228 p - (char *)context->service_buffer);
Andy Green76f61e72013-01-16 11:53:05 +0800229 else
Andy Greenb5b23192013-02-11 17:13:32 +0800230#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 Green76f61e72013-01-16 11:53:05 +0800236
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 Green040d2ef2013-01-16 13:40:43 +0800241 return 0;
Andy Green76f61e72013-01-16 11:53:05 +0800242 }
243
Andy Green623a98d2013-01-21 11:04:23 +0800244 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
245 wsi->u.hdr.lextable_pos = 0;
Andy Green76f61e72013-01-16 11:53:05 +0800246 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY;
247 libwebsocket_set_timeout(wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800248 PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
249 AWAITING_TIMEOUT);
Andy Green76f61e72013-01-16 11:53:05 +0800250 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 Green5a4f3ae2013-03-09 09:09:46 +0800264 if (!(pollfd->revents & POLLIN))
265 break;
Andy Green76f61e72013-01-16 11:53:05 +0800266
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 Greenb5b23192013-02-11 17:13:32 +0800288 while (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE &&
289 len > 0) {
Andy Green76f61e72013-01-16 11:53:05 +0800290#ifdef LWS_OPENSSL_SUPPORT
Andy Green96d882a2013-01-30 12:27:27 +0800291 if (wsi->use_ssl) {
Andy Green76f61e72013-01-16 11:53:05 +0800292 len = SSL_read(wsi->ssl, &c, 1);
Andy Green96d882a2013-01-30 12:27:27 +0800293 if (len < 0) {
294 n = SSL_get_error(wsi->ssl, len);
295 if (n == SSL_ERROR_WANT_READ ||
Andy Greenb5b23192013-02-11 17:13:32 +0800296 n == SSL_ERROR_WANT_WRITE)
Andy Green96d882a2013-01-30 12:27:27 +0800297 return 0;
298 }
299 } else
Andy Green76f61e72013-01-16 11:53:05 +0800300#endif
301 len = recv(wsi->sock, &c, 1, 0);
302
Andy Green090789e2013-02-11 20:03:59 +0800303 if (len < 0) {
304 lwsl_warn("error on parsing recv\n");
Andy Green96d882a2013-01-30 12:27:27 +0800305 goto bail3;
Andy Green090789e2013-02-11 20:03:59 +0800306 }
Andy Green96d882a2013-01-30 12:27:27 +0800307
Andy Green3455e672013-02-04 08:55:42 +0800308 if (libwebsocket_parse(wsi, c)) {
Andy Green090789e2013-02-11 20:03:59 +0800309 lwsl_warn("problems parsing header\n");
Andy Green3455e672013-02-04 08:55:42 +0800310 goto bail3;
311 }
Andy Green76f61e72013-01-16 11:53:05 +0800312 }
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 Green623a98d2013-01-21 11:04:23 +0800320 if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
Andy Green76f61e72013-01-16 11:53:05 +0800321 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
331bail3:
Andy Greenb5b23192013-02-11 17:13:32 +0800332 lwsl_info(
333 "closing connection at LWS_CONNMODE...SERVER_REPLY\n");
Andy Green76f61e72013-01-16 11:53:05 +0800334 libwebsocket_close_and_free_session(context, wsi,
335 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800336 return 0;
Andy Green76f61e72013-01-16 11:53:05 +0800337
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
357static void
358strtolower(char *s)
359{
360 while (*s) {
361 *s = tolower(*s);
362 s++;
363 }
364}
365
366int
367lws_client_interpret_server_handshake(struct libwebsocket_context *context,
368 struct libwebsocket *wsi)
369{
Andy Green76f61e72013-01-16 11:53:05 +0800370 const char *pc;
Andy Green76f61e72013-01-16 11:53:05 +0800371 int okay = 0;
Andy Green16ab3182013-02-10 18:02:31 +0800372 char *p;
373 int len;
Andy Green3182ece2013-01-20 17:08:31 +0800374#ifndef LWS_NO_EXTENSIONS
Andy Green76f61e72013-01-16 11:53:05 +0800375 char ext_name[128];
376 struct libwebsocket_extension *ext;
377 void *v;
Andy Green3182ece2013-01-20 17:08:31 +0800378 int more = 1;
379 const char *c;
380#endif
Andy Green76f61e72013-01-16 11:53:05 +0800381 int n;
Andy Green76f61e72013-01-16 11:53:05 +0800382
383 /*
384 * well, what the server sent looked reasonable for syntax.
385 * Now let's confirm it sent all the necessary headers
386 */
Andy Green76f61e72013-01-16 11:53:05 +0800387
Andy Green090789e2013-02-11 20:03:59 +0800388 if (lws_hdr_total_length(wsi, WSI_TOKEN_ACCEPT) == 0) {
389 lwsl_info("no ACCEPT\n");
Andy Green16ab3182013-02-10 18:02:31 +0800390 goto bail3;
Andy Green090789e2013-02-11 20:03:59 +0800391 }
Andy Green16ab3182013-02-10 18:02:31 +0800392
393 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP);
Andy Green090789e2013-02-11 20:03:59 +0800394 if (!p) {
395 lwsl_info("no URI\n");
Andy Green16ab3182013-02-10 18:02:31 +0800396 goto bail3;
Andy Green090789e2013-02-11 20:03:59 +0800397 }
Andy Green16ab3182013-02-10 18:02:31 +0800398 if (p && strncmp(p, "101", 3)) {
Andy Greenb5b23192013-02-11 17:13:32 +0800399 lwsl_warn(
400 "lws_client_handshake: got bad HTTP response '%s'\n", p);
Andy Green76f61e72013-01-16 11:53:05 +0800401 goto bail3;
402 }
403
Andy Green16ab3182013-02-10 18:02:31 +0800404 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE);
Andy Green090789e2013-02-11 20:03:59 +0800405 if (!p) {
406 lwsl_info("no UPGRADE\n");
Andy Green16ab3182013-02-10 18:02:31 +0800407 goto bail3;
Andy Green090789e2013-02-11 20:03:59 +0800408 }
Andy Green16ab3182013-02-10 18:02:31 +0800409 strtolower(p);
410 if (strcmp(p, "websocket")) {
Andy Greenb5b23192013-02-11 17:13:32 +0800411 lwsl_warn(
412 "lws_client_handshake: got bad Upgrade header '%s'\n", p);
Andy Green76f61e72013-01-16 11:53:05 +0800413 goto bail3;
414 }
415
Andy Green16ab3182013-02-10 18:02:31 +0800416 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_CONNECTION);
Andy Green090789e2013-02-11 20:03:59 +0800417 if (!p) {
418 lwsl_info("no Connection hdr\n");
Andy Green16ab3182013-02-10 18:02:31 +0800419 goto bail3;
Andy Green090789e2013-02-11 20:03:59 +0800420 }
Andy Green16ab3182013-02-10 18:02:31 +0800421 strtolower(p);
422 if (strcmp(p, "upgrade")) {
Andy Greenb5b23192013-02-11 17:13:32 +0800423 lwsl_warn("lws_client_int_s_hs: bad header %s\n", p);
Andy Green76f61e72013-01-16 11:53:05 +0800424 goto bail3;
425 }
426
Andy Greene77fb802013-02-11 13:04:45 +0800427 pc = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS);
Andy Green76f61e72013-01-16 11:53:05 +0800428 if (pc == NULL)
Andy Greenb5b23192013-02-11 17:13:32 +0800429 lwsl_parser("lws_client_int_s_hs: no protocol list\n");
Andy Green76f61e72013-01-16 11:53:05 +0800430 else
Andy Greenb5b23192013-02-11 17:13:32 +0800431 lwsl_parser("lws_client_int_s_hs: protocol list '%s'\n", pc);
Andy Green76f61e72013-01-16 11:53:05 +0800432
433 /*
434 * confirm the protocol the server wants to talk was in the list
435 * of protocols we offered
436 */
437
Andy Green16ab3182013-02-10 18:02:31 +0800438 len = lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL);
439 if (!len) {
Andy Green76f61e72013-01-16 11:53:05 +0800440
Andy Greenb5b23192013-02-11 17:13:32 +0800441 lwsl_info("lws_client_int_s_hs: WSI_TOKEN_PROTOCOL is null\n");
Andy Green76f61e72013-01-16 11:53:05 +0800442 /*
443 * no protocol name to work from,
444 * default to first protocol
445 */
446 wsi->protocol = &context->protocols[0];
Andy Green76f61e72013-01-16 11:53:05 +0800447 goto check_extensions;
448 }
449
Andy Green16ab3182013-02-10 18:02:31 +0800450 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL);
451 len = strlen(p);
452
Andy Green76f61e72013-01-16 11:53:05 +0800453 while (*pc && !okay) {
Andy Greenb5b23192013-02-11 17:13:32 +0800454 if (!strncmp(pc, p, len) &&
455 (pc[len] == ',' || pc[len] == '\0')) {
Andy Green76f61e72013-01-16 11:53:05 +0800456 okay = 1;
457 continue;
458 }
459 while (*pc && *pc != ',')
460 pc++;
461 while (*pc && *pc != ' ')
462 pc++;
463 }
464
Andy Green76f61e72013-01-16 11:53:05 +0800465 if (!okay) {
Andy Greenb5b23192013-02-11 17:13:32 +0800466 lwsl_err("lws_client_int_s_hs: got bad protocol %s\n", p);
Andy Green76f61e72013-01-16 11:53:05 +0800467 goto bail2;
468 }
469
470 /*
471 * identify the selected protocol struct and set it
472 */
473 n = 0;
474 wsi->protocol = NULL;
Andy Green16ab3182013-02-10 18:02:31 +0800475 while (context->protocols[n].callback && !wsi->protocol) {
476 if (strcmp(p, context->protocols[n].name) == 0) {
Andy Green76f61e72013-01-16 11:53:05 +0800477 wsi->protocol = &context->protocols[n];
Andy Green16ab3182013-02-10 18:02:31 +0800478 break;
Andy Green76f61e72013-01-16 11:53:05 +0800479 }
480 n++;
481 }
482
483 if (wsi->protocol == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +0800484 lwsl_err("lws_client_int_s_hs: fail protocol %s\n", p);
Andy Green76f61e72013-01-16 11:53:05 +0800485 goto bail2;
486 }
487
488
489check_extensions:
Andy Green3182ece2013-01-20 17:08:31 +0800490#ifndef LWS_NO_EXTENSIONS
Andy Green76f61e72013-01-16 11:53:05 +0800491 /* instantiate the accepted extensions */
492
Andy Green16ab3182013-02-10 18:02:31 +0800493 if (!lws_hdr_total_length(wsi, WSI_TOKEN_EXTENSIONS)) {
Andy Green76f61e72013-01-16 11:53:05 +0800494 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 Greenb5b23192013-02-11 17:13:32 +0800503 if (lws_hdr_copy(wsi, (char *)context->service_buffer,
Andy Green090789e2013-02-11 20:03:59 +0800504 sizeof(context->service_buffer), WSI_TOKEN_EXTENSIONS) < 0) {
505 lwsl_warn("ext list from server failed to copy\n");
Andy Green16ab3182013-02-10 18:02:31 +0800506 goto bail2;
Andy Green090789e2013-02-11 20:03:59 +0800507 }
Andy Green16ab3182013-02-10 18:02:31 +0800508
509 c = (char *)context->service_buffer;
Andy Green76f61e72013-01-16 11:53:05 +0800510 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 Greenb5b23192013-02-11 17:13:32 +0800576 lwsl_warn("Unknown ext '%s'!\n", ext_name);
Andy Green76f61e72013-01-16 11:53:05 +0800577 goto bail2;
578 }
579
580 n = 0;
581 }
582
Andy Green76f61e72013-01-16 11:53:05 +0800583check_accept:
Andy Green3182ece2013-01-20 17:08:31 +0800584#endif
Andy Green76f61e72013-01-16 11:53:05 +0800585
Andy Green76f61e72013-01-16 11:53:05 +0800586 /*
587 * Confirm his accept token is the one we precomputed
588 */
589
Andy Green16ab3182013-02-10 18:02:31 +0800590 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_ACCEPT);
Andy Greena7521de2013-02-18 10:38:45 +0800591 if (strcmp(p, wsi->u.hdr.ah->initial_handshake_hash_base64)) {
Andy Greenb5b23192013-02-11 17:13:32 +0800592 lwsl_warn("lws_client_int_s_hs: accept %s wrong vs %s\n", p,
Andy Greena7521de2013-02-18 10:38:45 +0800593 wsi->u.hdr.ah->initial_handshake_hash_base64);
Andy Green76f61e72013-01-16 11:53:05 +0800594 goto bail2;
595 }
596
Andy Green76f61e72013-01-16 11:53:05 +0800597 /* allocate the per-connection user memory (if any) */
Andy Green2af4d5b2013-02-18 16:30:10 +0800598 if (libwebsocket_ensure_user_space(wsi)) {
Andy Green090789e2013-02-11 20:03:59 +0800599 lwsl_err("Problem allocating wsi user mem\n");
Andy Green76f61e72013-01-16 11:53:05 +0800600 goto bail2;
Andy Green090789e2013-02-11 20:03:59 +0800601 }
Andy Green76f61e72013-01-16 11:53:05 +0800602
Andy Green2b57a342013-02-06 15:15:25 +0900603 /*
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 Green76f61e72013-01-16 11:53:05 +0800612 /* clear his proxy connection timeout */
613
614 libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
615
Andy Green2b57a342013-02-06 15:15:25 +0900616 /* free up his parsing allocations */
Andy Green16ab3182013-02-10 18:02:31 +0800617 if (wsi->u.hdr.ah)
618 free(wsi->u.hdr.ah);
Andy Green2b57a342013-02-06 15:15:25 +0900619
Andy Green76f61e72013-01-16 11:53:05 +0800620 /* mark him as being alive */
621
622 wsi->state = WSI_STATE_ESTABLISHED;
623 wsi->mode = LWS_CONNMODE_WS_CLIENT;
624
Andy Green623a98d2013-01-21 11:04:23 +0800625 /* union transition */
Andy Green16ab3182013-02-10 18:02:31 +0800626
Andy Greenb5b23192013-02-11 17:13:32 +0800627 memset(&wsi->u, 0, sizeof(wsi->u));
Andy Green623a98d2013-01-21 11:04:23 +0800628
Andy Green54495112013-02-06 21:10:16 +0900629 /*
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 Green76f61e72013-01-16 11:53:05 +0800646 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 Green3182ece2013-01-20 17:08:31 +0800653#ifndef LWS_NO_EXTENSIONS
Andy Green76f61e72013-01-16 11:53:05 +0800654 /*
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 Green3182ece2013-01-20 17:08:31 +0800671#endif
Andy Green76f61e72013-01-16 11:53:05 +0800672
673 return 0;
674
675bail3:
Andy Green76f61e72013-01-16 11:53:05 +0800676
677bail2:
Andy Greene77fb802013-02-11 13:04:45 +0800678 if (wsi->protocol)
679 wsi->protocol->callback(context, wsi,
Andy Green16ab3182013-02-10 18:02:31 +0800680 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
681 wsi->user_space, NULL, 0);
682
Andy Green96d882a2013-01-30 12:27:27 +0800683 lwsl_info("closing connection due to bail2 connection error\n");
Andy Green16ab3182013-02-10 18:02:31 +0800684
Andy Green2b57a342013-02-06 15:15:25 +0900685 /* free up his parsing allocations */
686
Andy Green16ab3182013-02-10 18:02:31 +0800687 if (wsi->u.hdr.ah)
688 free(wsi->u.hdr.ah);
Andy Green2b57a342013-02-06 15:15:25 +0900689
Andy Green76f61e72013-01-16 11:53:05 +0800690 libwebsocket_close_and_free_session(context, wsi,
Andy Green96d882a2013-01-30 12:27:27 +0800691 LWS_CLOSE_STATUS_PROTOCOL_ERR);
Andy Green76f61e72013-01-16 11:53:05 +0800692
693 return 1;
694}
695
Andy Green76f61e72013-01-16 11:53:05 +0800696
697char *
698libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
699 struct libwebsocket *wsi, char *pkt)
700{
Andy Greenc97067c2013-02-10 11:03:32 +0800701 char buf[128];
Andy Green76f61e72013-01-16 11:53:05 +0800702 char hash[20];
Andy Green80f168b2013-01-21 11:04:49 +0800703 char key_b64[40];
Andy Green76f61e72013-01-16 11:53:05 +0800704 char *p = pkt;
705 int n;
Andy Green3182ece2013-01-20 17:08:31 +0800706#ifndef LWS_NO_EXTENSIONS
Andy Green76f61e72013-01-16 11:53:05 +0800707 struct libwebsocket_extension *ext;
708 struct libwebsocket_extension *ext1;
709 int ext_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +0800710#endif
Andy Green76f61e72013-01-16 11:53:05 +0800711
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 Green76f61e72013-01-16 11:53:05 +0800720 libwebsocket_close_and_free_session(context, wsi,
721 LWS_CLOSE_STATUS_NOSTATUS);
722 return NULL;
723 }
724
Andy Greenb5b23192013-02-11 17:13:32 +0800725 lws_b64_encode_string(hash, 16, key_b64, sizeof(key_b64));
Andy Green76f61e72013-01-16 11:53:05 +0800726
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 Greenb5b23192013-02-11 17:13:32 +0800753 p += sprintf(p, "GET %s HTTP/1.1\x0d\x0a",
754 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_URI));
Andy Green76f61e72013-01-16 11:53:05 +0800755
Andy Greenb5b23192013-02-11 17:13:32 +0800756 p += sprintf(p,
757 "Pragma: no-cache\x0d\x0a""Cache-Control: no-cache\x0d\x0a");
Andy Green76f61e72013-01-16 11:53:05 +0800758
Andy Greenb5b23192013-02-11 17:13:32 +0800759 p += sprintf(p, "Host: %s\x0d\x0a",
760 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_HOST));
761 p += sprintf(p,
Andy Green090789e2013-02-11 20:03:59 +0800762"Upgrade: websocket\x0d\x0a""Connection: Upgrade\x0d\x0a""Sec-WebSocket-Key: ");
Andy Green80f168b2013-01-21 11:04:49 +0800763 strcpy(p, key_b64);
764 p += strlen(key_b64);
Andy Green76f61e72013-01-16 11:53:05 +0800765 p += sprintf(p, "\x0d\x0a");
Andy Greene77fb802013-02-11 13:04:45 +0800766 if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN))
Andy Greenb5b23192013-02-11 17:13:32 +0800767 p += sprintf(p, "Origin: %s\x0d\x0a",
768 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN));
Andy Greene77fb802013-02-11 13:04:45 +0800769
770 if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS))
Andy Green76f61e72013-01-16 11:53:05 +0800771 p += sprintf(p, "Sec-WebSocket-Protocol: %s\x0d\x0a",
Andy Greenb5b23192013-02-11 17:13:32 +0800772 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS));
Andy Green76f61e72013-01-16 11:53:05 +0800773
774 /* tell the server what extensions we could support */
775
776 p += sprintf(p, "Sec-WebSocket-Extensions: ");
Andy Green3182ece2013-01-20 17:08:31 +0800777#ifndef LWS_NO_EXTENSIONS
Andy Green76f61e72013-01-16 11:53:05 +0800778 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 Green3182ece2013-01-20 17:08:31 +0800823#endif
Andy Green76f61e72013-01-16 11:53:05 +0800824 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 Greenc97067c2013-02-10 11:03:32 +0800834 NULL, &p, (pkt + sizeof(context->service_buffer)) - p - 12);
Andy Green76f61e72013-01-16 11:53:05 +0800835
836 p += sprintf(p, "\x0d\x0a");
837
838 /* prepare the expected server accept response */
839
Andy Greencecf5e72013-02-12 10:07:22 +0800840 key_b64[39] = '\0'; /* enforce composed length below buf sizeof */
841 n = sprintf(buf, "%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11", key_b64);
842
Andy Greenc97067c2013-02-10 11:03:32 +0800843 SHA1((unsigned char *)buf, n, (unsigned char *)hash);
Andy Green76f61e72013-01-16 11:53:05 +0800844
845 lws_b64_encode_string(hash, 20,
Andy Greena7521de2013-02-18 10:38:45 +0800846 wsi->u.hdr.ah->initial_handshake_hash_base64,
847 sizeof(wsi->u.hdr.ah->initial_handshake_hash_base64));
Andy Green76f61e72013-01-16 11:53:05 +0800848
Andy Green76f61e72013-01-16 11:53:05 +0800849 return p;
850}
851