2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2013 Andy Green <andy@warmcat.com>
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.
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.
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,
22 #include "private-libwebsockets.h"
28 #ifdef LWS_BUILTIN_GETIFADDRS
29 #include <getifaddrs.h>
34 #include <sys/socket.h>
38 int lws_client_socket_service(struct libwebsocket_context *context,
39 struct libwebsocket *wsi, struct pollfd *pollfd)
42 char *p = (char *)&context->service_buffer[0];
48 case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
50 /* handle proxy hung up on us */
52 if (pollfd->revents & (POLLERR | POLLHUP)) {
54 lwsl_warn("Proxy connection %p (fd=%d) dead\n",
55 (void *)wsi, pollfd->fd);
57 libwebsocket_close_and_free_session(context, wsi,
58 LWS_CLOSE_STATUS_NOSTATUS);
62 n = recv(wsi->sock, context->service_buffer,
63 sizeof(context->service_buffer), 0);
65 libwebsocket_close_and_free_session(context, wsi,
66 LWS_CLOSE_STATUS_NOSTATUS);
67 lwsl_err("ERROR reading from proxy socket\n");
71 context->service_buffer[13] = '\0';
72 if (strcmp((char *)context->service_buffer, "HTTP/1.0 200 ")) {
73 libwebsocket_close_and_free_session(context, wsi,
74 LWS_CLOSE_STATUS_NOSTATUS);
75 lwsl_err("ERROR proxy: %s\n", context->service_buffer);
79 /* clear his proxy connection timeout */
81 libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
85 case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
88 * we are under PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE
89 * timeout protection set in client-handshake.c
92 #ifdef LWS_OPENSSL_SUPPORT
95 * take care of our libwebsocket_callback_on_writable
96 * happening at a time when there's no real connection yet
99 pollfd->events &= ~POLLOUT;
101 /* external POLL support via protocol 0 */
102 context->protocols[0].callback(context, wsi,
103 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
104 (void *)(long)wsi->sock, NULL, POLLOUT);
106 /* we can retry this... just cook the SSL BIO the first time */
108 if (wsi->use_ssl && !wsi->ssl) {
110 wsi->ssl = SSL_new(context->ssl_client_ctx);
114 * CyaSSL does certificate verification differently
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
121 if (wsi->use_ssl == 2)
122 CyaSSL_set_verify(wsi->ssl,
123 SSL_VERIFY_NONE, NULL);
124 #endif /* USE_CYASSL */
127 BIO_new_socket(wsi->sock, BIO_NOCLOSE);
128 SSL_set_bio(wsi->ssl, wsi->client_bio, wsi->client_bio);
131 CyaSSL_set_using_nonblock(wsi->ssl, 1);
133 BIO_set_nbio(wsi->client_bio, 1); /* nonblocking */
136 SSL_set_ex_data(wsi->ssl,
137 openssl_websocket_private_data_index,
142 lws_latency_pre(context, wsi);
143 n = SSL_connect(wsi->ssl);
144 lws_latency(context, wsi,
145 "SSL_connect LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE",
149 n = SSL_get_error(wsi->ssl, n);
151 if (n == SSL_ERROR_WANT_READ ||
152 n == SSL_ERROR_WANT_WRITE) {
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.
167 "SSL_connect WANT_... retrying\n");
168 libwebsocket_callback_on_writable(
171 return 0; /* no error */
178 * retry if new data comes until we
179 * run into the connection timeout or win
182 lwsl_err("SSL connect error %s\n",
183 ERR_error_string(ERR_get_error(),
184 (char *)context->service_buffer));
190 * See comment above about CyaSSL certificate
193 lws_latency_pre(context, wsi);
194 n = SSL_get_verify_result(wsi->ssl);
195 lws_latency(context, wsi,
196 "SSL_get_verify_result LWS_CONNMODE..HANDSHAKE",
198 if ((n != X509_V_OK) && (
199 n != X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT ||
200 wsi->use_ssl != 2)) {
203 "server's cert didn't look good %d\n", n);
204 libwebsocket_close_and_free_session(context,
205 wsi, LWS_CLOSE_STATUS_NOSTATUS);
208 #endif /* USE_CYASSL */
213 p = libwebsockets_generate_client_handshake(context, wsi, p);
215 lwsl_err("Failed to generate handshake for client\n");
216 libwebsocket_close_and_free_session(context, wsi,
217 LWS_CLOSE_STATUS_NOSTATUS);
221 /* send our request to the server */
223 lws_latency_pre(context, wsi);
224 #ifdef LWS_OPENSSL_SUPPORT
226 n = SSL_write(wsi->ssl, context->service_buffer,
227 p - (char *)context->service_buffer);
230 n = send(wsi->sock, context->service_buffer,
231 p - (char *)context->service_buffer, 0);
232 lws_latency(context, wsi,
233 "send or SSL_write LWS_CONNMODE...HANDSHAKE",
237 lwsl_debug("ERROR writing to client socket\n");
238 libwebsocket_close_and_free_session(context, wsi,
239 LWS_CLOSE_STATUS_NOSTATUS);
243 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
244 wsi->u.hdr.lextable_pos = 0;
245 wsi->mode = LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY;
246 libwebsocket_set_timeout(wsi,
247 PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
251 case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
253 /* handle server hung up on us */
255 if (pollfd->revents & (POLLERR | POLLHUP)) {
257 lwsl_debug("Server connection %p (fd=%d) dead\n",
258 (void *)wsi, pollfd->fd);
263 if (!(pollfd->revents & POLLIN)) {
264 lwsl_warn("server reply no POLLIN\n");
268 /* interpret the server response */
271 * HTTP/1.1 101 Switching Protocols
273 * Connection: Upgrade
274 * Sec-WebSocket-Accept: me89jWimTRKTWwrS3aRrL53YZSo=
275 * Sec-WebSocket-Nonce: AQIDBAUGBwgJCgsMDQ4PEC==
276 * Sec-WebSocket-Protocol: chat
280 * we have to take some care here to only take from the
281 * socket bytewise. The browser may (and has been seen to
282 * in the case that onopen() performs websocket traffic)
283 * coalesce both handshake response and websocket traffic
284 * in one packet, since at that point the connection is
285 * definitively ready from browser pov.
289 while (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE &&
291 #ifdef LWS_OPENSSL_SUPPORT
293 len = SSL_read(wsi->ssl, &c, 1);
295 n = SSL_get_error(wsi->ssl, len);
296 if (n == SSL_ERROR_WANT_READ ||
297 n == SSL_ERROR_WANT_WRITE)
302 len = recv(wsi->sock, &c, 1, 0);
305 lwsl_warn("error on parsing recv\n");
309 if (libwebsocket_parse(wsi, c)) {
310 lwsl_warn("problems parsing header\n");
316 * hs may also be coming in multiple packets, there is a 5-sec
317 * libwebsocket timeout still active here too, so if parsing did
318 * not complete just wait for next packet coming in this state
321 if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
325 * otherwise deal with the handshake. If there's any
326 * packet traffic already arrived we'll trigger poll() again
327 * right away and deal with it that way
330 return lws_client_interpret_server_handshake(context, wsi);
334 "closing connection at LWS_CONNMODE...SERVER_REPLY\n");
335 libwebsocket_close_and_free_session(context, wsi,
336 LWS_CLOSE_STATUS_NOSTATUS);
339 case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT:
340 lwsl_ext("LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT\n");
343 case LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD:
344 lwsl_ext("LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD\n");
355 * In-place str to lower case
368 lws_client_interpret_server_handshake(struct libwebsocket_context *context,
369 struct libwebsocket *wsi)
375 #ifndef LWS_NO_EXTENSIONS
377 struct libwebsocket_extension *ext;
385 * well, what the server sent looked reasonable for syntax.
386 * Now let's confirm it sent all the necessary headers
389 if (lws_hdr_total_length(wsi, WSI_TOKEN_ACCEPT) == 0) {
390 lwsl_info("no ACCEPT\n");
394 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP);
396 lwsl_info("no URI\n");
399 if (p && strncmp(p, "101", 3)) {
401 "lws_client_handshake: got bad HTTP response '%s'\n", p);
405 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_UPGRADE);
407 lwsl_info("no UPGRADE\n");
411 if (strcmp(p, "websocket")) {
413 "lws_client_handshake: got bad Upgrade header '%s'\n", p);
417 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_CONNECTION);
419 lwsl_info("no Connection hdr\n");
423 if (strcmp(p, "upgrade")) {
424 lwsl_warn("lws_client_int_s_hs: bad header %s\n", p);
428 pc = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS);
430 lwsl_parser("lws_client_int_s_hs: no protocol list\n");
432 lwsl_parser("lws_client_int_s_hs: protocol list '%s'\n", pc);
435 * confirm the protocol the server wants to talk was in the list
436 * of protocols we offered
439 len = lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL);
442 lwsl_info("lws_client_int_s_hs: WSI_TOKEN_PROTOCOL is null\n");
444 * no protocol name to work from,
445 * default to first protocol
447 wsi->protocol = &context->protocols[0];
448 goto check_extensions;
451 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL);
454 while (*pc && !okay) {
455 if (!strncmp(pc, p, len) &&
456 (pc[len] == ',' || pc[len] == '\0')) {
460 while (*pc && *pc != ',')
462 while (*pc && *pc != ' ')
467 lwsl_err("lws_client_int_s_hs: got bad protocol %s\n", p);
472 * identify the selected protocol struct and set it
475 wsi->protocol = NULL;
476 while (context->protocols[n].callback && !wsi->protocol) {
477 if (strcmp(p, context->protocols[n].name) == 0) {
478 wsi->protocol = &context->protocols[n];
484 if (wsi->protocol == NULL) {
485 lwsl_err("lws_client_int_s_hs: fail protocol %s\n", p);
491 #ifndef LWS_NO_EXTENSIONS
492 /* instantiate the accepted extensions */
494 if (!lws_hdr_total_length(wsi, WSI_TOKEN_EXTENSIONS)) {
495 lwsl_ext("no client extenstions allowed by server\n");
500 * break down the list of server accepted extensions
501 * and go through matching them or identifying bogons
504 if (lws_hdr_copy(wsi, (char *)context->service_buffer,
505 sizeof(context->service_buffer), WSI_TOKEN_EXTENSIONS) < 0) {
506 lwsl_warn("ext list from server failed to copy\n");
510 c = (char *)context->service_buffer;
514 if (*c && (*c != ',' && *c != ' ' && *c != '\t')) {
516 if (n < sizeof(ext_name) - 1)
529 /* check we actually support it */
531 lwsl_ext("checking client ext %s\n", ext_name);
534 ext = wsi->protocol->owning_server->extensions;
535 while (ext && ext->callback) {
537 if (strcmp(ext_name, ext->name)) {
544 lwsl_ext("instantiating client ext %s\n", ext_name);
546 /* instantiate the extension on this conn */
548 wsi->active_extensions_user[
549 wsi->count_active_extensions] =
550 malloc(ext->per_session_data_size);
551 if (wsi->active_extensions_user[
552 wsi->count_active_extensions] == NULL) {
553 lwsl_err("Out of mem\n");
556 memset(wsi->active_extensions_user[
557 wsi->count_active_extensions], 0,
558 ext->per_session_data_size);
559 wsi->active_extensions[
560 wsi->count_active_extensions] = ext;
562 /* allow him to construct his context */
564 ext->callback(wsi->protocol->owning_server,
566 LWS_EXT_CALLBACK_CLIENT_CONSTRUCT,
567 wsi->active_extensions_user[
568 wsi->count_active_extensions],
571 wsi->count_active_extensions++;
577 lwsl_warn("Unknown ext '%s'!\n", ext_name);
588 * Confirm his accept token is the one we precomputed
591 p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_ACCEPT);
592 if (strcmp(p, wsi->u.hdr.ah->initial_handshake_hash_base64)) {
593 lwsl_warn("lws_client_int_s_hs: accept %s wrong vs %s\n", p,
594 wsi->u.hdr.ah->initial_handshake_hash_base64);
598 /* allocate the per-connection user memory (if any) */
599 if (wsi->protocol->per_session_data_size &&
600 !libwebsocket_ensure_user_space(wsi)) {
601 lwsl_err("Problem allocating wsi user mem\n");
606 * we seem to be good to go, give client last chance to check
610 wsi->protocol->callback(context, wsi,
611 LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH,
612 wsi->user_space, NULL, 0);
614 /* clear his proxy connection timeout */
616 libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
618 /* free up his parsing allocations */
622 /* mark him as being alive */
624 wsi->state = WSI_STATE_ESTABLISHED;
625 wsi->mode = LWS_CONNMODE_WS_CLIENT;
627 /* union transition */
629 memset(&wsi->u, 0, sizeof(wsi->u));
632 * create the frame buffer for this connection according to the
633 * size mentioned in the protocol definition. If 0 there, then
634 * use a big default for compatibility
637 n = wsi->protocol->rx_buffer_size;
639 n = LWS_MAX_SOCKET_IO_BUF;
640 n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING;
641 wsi->u.ws.rx_user_buffer = malloc(n);
642 if (!wsi->u.ws.rx_user_buffer) {
643 lwsl_err("Out of Mem allocating rx buffer %d\n", n);
646 lwsl_info("Allocating client RX buffer %d\n", n);
648 lwsl_debug("handshake OK for protocol %s\n", wsi->protocol->name);
650 /* call him back to inform him he is up */
652 wsi->protocol->callback(context, wsi,
653 LWS_CALLBACK_CLIENT_ESTABLISHED,
654 wsi->user_space, NULL, 0);
655 #ifndef LWS_NO_EXTENSIONS
657 * inform all extensions, not just active ones since they
661 ext = context->extensions;
663 while (ext && ext->callback) {
665 for (n = 0; n < wsi->count_active_extensions; n++)
666 if (wsi->active_extensions[n] == ext)
667 v = wsi->active_extensions_user[n];
669 ext->callback(context, ext, wsi,
670 LWS_EXT_CALLBACK_ANY_WSI_ESTABLISHED, v, NULL, 0);
681 wsi->protocol->callback(context, wsi,
682 LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
683 wsi->user_space, NULL, 0);
685 lwsl_info("closing connection due to bail2 connection error\n");
687 /* free up his parsing allocations */
692 libwebsocket_close_and_free_session(context, wsi,
693 LWS_CLOSE_STATUS_PROTOCOL_ERR);
700 libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
701 struct libwebsocket *wsi, char *pkt)
708 #ifndef LWS_NO_EXTENSIONS
709 struct libwebsocket_extension *ext;
710 struct libwebsocket_extension *ext1;
715 * create the random key
718 n = libwebsockets_get_random(context, hash, 16);
720 lwsl_err("Unable to read from random dev %s\n",
721 SYSTEM_RANDOM_FILEPATH);
722 libwebsocket_close_and_free_session(context, wsi,
723 LWS_CLOSE_STATUS_NOSTATUS);
727 lws_b64_encode_string(hash, 16, key_b64, sizeof(key_b64));
730 * 00 example client handshake
732 * GET /socket.io/websocket HTTP/1.1
734 * Connection: Upgrade
735 * Host: 127.0.0.1:9999
736 * Origin: http://127.0.0.1
737 * Sec-WebSocket-Key1: 1 0 2#0W 9 89 7 92 ^
738 * Sec-WebSocket-Key2: 7 7Y 4328 B2v[8(z1
739 * Cookie: socketio=websocket
743 * 04 example client handshake
746 * Host: server.example.com
748 * Connection: Upgrade
749 * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
750 * Sec-WebSocket-Origin: http://example.com
751 * Sec-WebSocket-Protocol: chat, superchat
752 * Sec-WebSocket-Version: 4
755 p += sprintf(p, "GET %s HTTP/1.1\x0d\x0a",
756 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_URI));
759 "Pragma: no-cache\x0d\x0a""Cache-Control: no-cache\x0d\x0a");
761 p += sprintf(p, "Host: %s\x0d\x0a",
762 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_HOST));
764 "Upgrade: websocket\x0d\x0a""Connection: Upgrade\x0d\x0a""Sec-WebSocket-Key: ");
766 p += strlen(key_b64);
767 p += sprintf(p, "\x0d\x0a");
768 if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN))
769 p += sprintf(p, "Origin: %s\x0d\x0a",
770 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_ORIGIN));
772 if (lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS))
773 p += sprintf(p, "Sec-WebSocket-Protocol: %s\x0d\x0a",
774 lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS));
776 /* tell the server what extensions we could support */
778 p += sprintf(p, "Sec-WebSocket-Extensions: ");
779 #ifndef LWS_NO_EXTENSIONS
780 ext = context->extensions;
781 while (ext && ext->callback) {
784 ext1 = context->extensions;
786 while (ext1 && ext1->callback) {
787 n |= ext1->callback(context, ext1, wsi,
788 LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION,
789 NULL, (char *)ext->name, 0);
794 if (n) { /* an extension vetos us */
795 lwsl_ext("ext %s vetoed\n", (char *)ext->name);
800 n = context->protocols[0].callback(context, wsi,
801 LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED,
802 wsi->user_space, (char *)ext->name, 0);
805 * zero return from callback means
806 * go ahead and allow the extension,
807 * it's what we get if the callback is
820 p += sprintf(p, "%s", ext->name);
826 p += sprintf(p, "\x0d\x0a");
828 if (wsi->ietf_spec_revision)
829 p += sprintf(p, "Sec-WebSocket-Version: %d\x0d\x0a",
830 wsi->ietf_spec_revision);
832 /* give userland a chance to append, eg, cookies */
834 context->protocols[0].callback(context, wsi,
835 LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER,
836 NULL, &p, (pkt + sizeof(context->service_buffer)) - p - 12);
838 p += sprintf(p, "\x0d\x0a");
840 /* prepare the expected server accept response */
842 key_b64[39] = '\0'; /* enforce composed length below buf sizeof */
843 n = sprintf(buf, "%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11", key_b64);
845 SHA1((unsigned char *)buf, n, (unsigned char *)hash);
847 lws_b64_encode_string(hash, 20,
848 wsi->u.hdr.ah->initial_handshake_hash_base64,
849 sizeof(wsi->u.hdr.ah->initial_handshake_hash_base64));