2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010 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"
24 const struct lws_tokens lws_tokens[WSI_TOKEN_COUNT] = {
25 [WSI_TOKEN_GET_URI] = { "GET ", 4 },
26 [WSI_TOKEN_HOST] = { "Host:", 5 },
27 [WSI_TOKEN_CONNECTION] = { "Connection:", 11 },
28 [WSI_TOKEN_KEY1] = { "Sec-WebSocket-Key1:", 19 },
29 [WSI_TOKEN_KEY2] = { "Sec-WebSocket-Key2:", 19 },
30 [WSI_TOKEN_PROTOCOL] = { "Sec-WebSocket-Protocol:", 23 },
31 [WSI_TOKEN_UPGRADE] = { "Upgrade:", 8 },
32 [WSI_TOKEN_ORIGIN] = { "Origin:", 7 },
33 [WSI_TOKEN_DRAFT] = { "Sec-WebSocket-Draft:", 20 },
34 [WSI_TOKEN_CHALLENGE] = { "\x0d\x0a", 2 },
36 [WSI_TOKEN_KEY] = { "Sec-WebSocket-Key:", 18 },
37 [WSI_TOKEN_VERSION] = { "Sec-WebSocket-Version:", 22 },
39 [WSI_TOKEN_ACCEPT] = { "Sec-WebSocket-Accept:", 21 },
40 [WSI_TOKEN_NONCE] = { "Sec-WebSocket-Nonce:", 20 },
41 [WSI_TOKEN_HTTP] = { "HTTP/1.1 ", 9 },
42 [WSI_TOKEN_SWORIGIN] = { "Sec-WebSocket-Origin:", 21 },
46 int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c)
50 switch (wsi->parser_state) {
51 case WSI_TOKEN_GET_URI:
53 case WSI_TOKEN_CONNECTION:
56 case WSI_TOKEN_PROTOCOL:
57 case WSI_TOKEN_UPGRADE:
58 case WSI_TOKEN_ORIGIN:
59 case WSI_TOKEN_SWORIGIN:
61 case WSI_TOKEN_CHALLENGE:
63 case WSI_TOKEN_VERSION:
64 case WSI_TOKEN_ACCEPT:
67 debug("WSI_TOKEN_(%d) '%c'\n", wsi->parser_state, c);
69 /* collect into malloc'd buffers */
70 /* optional space swallow */
71 if (!wsi->utf8_token[wsi->parser_state].token_len && c == ' ')
74 /* special case space terminator for get-uri */
75 if (wsi->parser_state == WSI_TOKEN_GET_URI && c == ' ') {
76 wsi->utf8_token[wsi->parser_state].token[
77 wsi->utf8_token[wsi->parser_state].token_len] = '\0';
78 wsi->parser_state = WSI_TOKEN_SKIPPING;
82 /* allocate appropriate memory */
83 if (wsi->utf8_token[wsi->parser_state].token_len ==
84 wsi->current_alloc_len - 1) {
86 wsi->current_alloc_len += LWS_ADDITIONAL_HDR_ALLOC;
87 if (wsi->current_alloc_len >= LWS_MAX_HEADER_LEN) {
88 /* it's waaay to much payload, fail it */
89 strcpy(wsi->utf8_token[wsi->parser_state].token,
90 "!!! Length exceeded maximum supported !!!");
91 wsi->parser_state = WSI_TOKEN_SKIPPING;
94 wsi->utf8_token[wsi->parser_state].token =
95 realloc(wsi->utf8_token[wsi->parser_state].token,
96 wsi->current_alloc_len);
100 if (wsi->parser_state != WSI_TOKEN_CHALLENGE && c == '\x0d') {
101 wsi->utf8_token[wsi->parser_state].token[
102 wsi->utf8_token[wsi->parser_state].token_len] = '\0';
103 wsi->parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
107 wsi->utf8_token[wsi->parser_state].token[
108 wsi->utf8_token[wsi->parser_state].token_len++] = c;
110 /* per-protocol end of headers management */
112 if (wsi->parser_state != WSI_TOKEN_CHALLENGE)
115 /* -76 has no version header */
116 if (!wsi->utf8_token[WSI_TOKEN_VERSION].token_len &&
117 wsi->utf8_token[wsi->parser_state].token_len != 8)
120 /* <= 03 has old handshake with version header */
121 if (wsi->utf8_token[WSI_TOKEN_VERSION].token_len &&
122 atoi(wsi->utf8_token[WSI_TOKEN_VERSION].token) < 4 &&
123 wsi->utf8_token[wsi->parser_state].token_len != 8)
126 /* For any supported protocol we have enough payload */
128 debug("Setting WSI_PARSING_COMPLETE\n");
129 wsi->parser_state = WSI_PARSING_COMPLETE;
132 /* collecting and checking a name part */
133 case WSI_TOKEN_NAME_PART:
134 debug("WSI_TOKEN_NAME_PART '%c'\n", c);
136 if (wsi->name_buffer_pos == sizeof(wsi->name_buffer) - 1) {
137 /* name bigger than we can handle, skip until next */
138 wsi->parser_state = WSI_TOKEN_SKIPPING;
141 wsi->name_buffer[wsi->name_buffer_pos++] = c;
142 wsi->name_buffer[wsi->name_buffer_pos] = '\0';
144 for (n = 0; n < WSI_TOKEN_COUNT; n++) {
145 if (wsi->name_buffer_pos != lws_tokens[n].token_len)
147 if (strcmp(lws_tokens[n].token, wsi->name_buffer))
149 debug("known hdr '%s'\n", wsi->name_buffer);
150 wsi->parser_state = WSI_TOKEN_GET_URI + n;
151 wsi->current_alloc_len = LWS_INITIAL_HDR_ALLOC;
153 wsi->utf8_token[wsi->parser_state].token =
154 malloc(wsi->current_alloc_len);
155 wsi->utf8_token[wsi->parser_state].token_len = 0;
159 /* colon delimiter means we just don't know this name */
161 if (wsi->parser_state == WSI_TOKEN_NAME_PART && c == ':') {
162 debug("skipping unknown header '%s'\n",
164 wsi->parser_state = WSI_TOKEN_SKIPPING;
168 if (wsi->parser_state != WSI_TOKEN_CHALLENGE)
171 /* don't look for payload when it can just be http headers */
173 if (!wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len) {
174 /* they're HTTP headers, not websocket upgrade! */
175 debug("Setting WSI_PARSING_COMPLETE "
176 "from http headers\n");
177 wsi->parser_state = WSI_PARSING_COMPLETE;
180 /* 04 version has no packet content after end of hdrs */
182 if (wsi->utf8_token[WSI_TOKEN_VERSION].token_len &&
183 atoi(wsi->utf8_token[WSI_TOKEN_VERSION].token) >= 4) {
184 debug("04 header completed\n");
185 wsi->parser_state = WSI_PARSING_COMPLETE;
190 if (wsi->ietf_spec_revision >= 4) {
191 debug("04 header completed\n");
192 wsi->parser_state = WSI_PARSING_COMPLETE;
197 /* skipping arg part of a name we didn't recognize */
198 case WSI_TOKEN_SKIPPING:
199 debug("WSI_TOKEN_SKIPPING '%c'\n", c);
201 wsi->parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
203 case WSI_TOKEN_SKIPPING_SAW_CR:
204 debug("WSI_TOKEN_SKIPPING_SAW_CR '%c'\n", c);
206 wsi->parser_state = WSI_TOKEN_NAME_PART;
208 wsi->parser_state = WSI_TOKEN_SKIPPING;
209 wsi->name_buffer_pos = 0;
211 /* we're done, ignore anything else */
212 case WSI_PARSING_COMPLETE:
213 debug("WSI_PARSING_COMPLETE '%c'\n", c);
216 default: /* keep gcc happy */
223 static unsigned char inline
224 xor_mask(struct libwebsocket *wsi, unsigned char c)
226 c ^= wsi->masking_key_04[wsi->frame_mask_index++];
227 if (wsi->frame_mask_index == 20)
228 wsi->frame_mask_index = 0;
234 static int libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c)
237 unsigned char buf[20 + 4];
239 switch (wsi->lws_rx_parse_state) {
242 switch (wsi->ietf_spec_revision) {
243 /* Firefox 4.0b6 likes this as of 30 Oct */
246 wsi->lws_rx_parse_state = LWS_RXPS_SEEN_76_FF;
248 wsi->lws_rx_parse_state =
249 LWS_RXPS_EAT_UNTIL_76_FF;
250 wsi->rx_user_buffer_head = 0;
254 wsi->frame_masking_nonce_04[0] = c;
255 wsi->lws_rx_parse_state = LWS_RXPS_04_MASK_NONCE_1;
259 case LWS_RXPS_04_MASK_NONCE_1:
260 wsi->frame_masking_nonce_04[1] = c;
261 wsi->lws_rx_parse_state = LWS_RXPS_04_MASK_NONCE_2;
263 case LWS_RXPS_04_MASK_NONCE_2:
264 wsi->frame_masking_nonce_04[2] = c;
265 wsi->lws_rx_parse_state = LWS_RXPS_04_MASK_NONCE_3;
267 case LWS_RXPS_04_MASK_NONCE_3:
268 wsi->frame_masking_nonce_04[3] = c;
271 * we are able to compute the frame key now
272 * it's a SHA1 of ( frame nonce we were just sent, concatenated
273 * with the connection masking key we computed at handshake
274 * time ) -- yeah every frame from the client invokes a SHA1
275 * for no real reason so much for lightweight.
278 buf[0] = wsi->frame_masking_nonce_04[0];
279 buf[1] = wsi->frame_masking_nonce_04[1];
280 buf[2] = wsi->frame_masking_nonce_04[2];
281 buf[3] = wsi->frame_masking_nonce_04[3];
283 memcpy(buf + 4, wsi->masking_key_04, 20);
286 * wsi->frame_mask_04 will be our recirculating 20-byte XOR key
290 SHA1((unsigned char *)buf, 4 + 20, wsi->frame_mask_04);
293 * start from the zero'th byte in the XOR key buffer since
294 * this is the start of a frame with a new key
297 wsi->frame_mask_index = 0;
299 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_1;
303 * 04 logical framing from the spec (all this is masked when incoming
304 * and has to be unmasked)
306 * We ignore the possibility of extension data because we don't
307 * negotiate any extensions at the moment.
310 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
311 * +-+-+-+-+-------+-+-------------+-------------------------------+
312 * |F|R|R|R| opcode|R| Payload len | Extended payload length |
313 * |I|S|S|S| (4) |S| (7) | (16/63) |
314 * |N|V|V|V| |V| | (if payload len==126/127) |
316 * +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
317 * | Extended payload length continued, if payload len == 127 |
318 * + - - - - - - - - - - - - - - - +-------------------------------+
319 * | | Extension data |
320 * +-------------------------------+ - - - - - - - - - - - - - - - +
322 * +---------------------------------------------------------------+
323 * : Application data :
324 * +---------------------------------------------------------------+
326 * We pass payload through to userland as soon as we get it, ignoring
327 * FIN. It's up to userland to buffer it up if it wants to see a
328 * whole unfragmented block of the original size (which may be up to
332 case LWS_RXPS_04_FRAME_HDR_1:
334 * 04 spec defines the opcode like this: (1, 2, and 3 are
335 * "control frame" opcodes which may not be fragmented or
336 * have size larger than 126)
339 * %x0 ; continuation frame
340 * / %x1 ; connection close
344 * / %x5 ; binary frame
350 c = xor_mask(wsi, c);
353 fprintf(stderr, "Frame has extensions set illegally 1\n");
354 /* kill the connection */
358 wsi->opcode = c & 0xf;
359 wsi->final = !!((c >> 7) & 1);
362 wsi->opcode == LWS_WS_OPCODE_04__CONTINUATION &&
363 wsi->rx_packet_length == 0) {
365 "Frame starts with final continuation\n");
366 /* kill the connection */
370 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN;
373 case LWS_RXPS_04_FRAME_HDR_LEN:
374 c = xor_mask(wsi, c);
377 fprintf(stderr, "Frame has extensions "
378 "set illegally 2\n");
379 /* kill the connection */
385 /* control frames are not allowed to have big lengths */
386 switch (wsi->opcode) {
387 case LWS_WS_OPCODE_04__CLOSE:
388 case LWS_WS_OPCODE_04__PING:
389 case LWS_WS_OPCODE_04__PONG:
390 fprintf(stderr, "Control frame asking for "
391 "extended length is illegal\n");
392 /* kill the connection */
397 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_2;
400 /* control frames are not allowed to have big lengths */
401 switch (wsi->opcode) {
402 case LWS_WS_OPCODE_04__CLOSE:
403 case LWS_WS_OPCODE_04__PING:
404 case LWS_WS_OPCODE_04__PONG:
405 fprintf(stderr, "Control frame asking for "
406 "extended length is illegal\n");
407 /* kill the connection */
412 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_8;
415 // fprintf(stderr, "seen incoming 04 frame len %d\n", c);
416 wsi->rx_packet_length = c;
417 wsi->lws_rx_parse_state =
418 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
423 case LWS_RXPS_04_FRAME_HDR_LEN16_2:
424 c = xor_mask(wsi, c);
426 wsi->rx_packet_length = c << 8;
427 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_1;
430 case LWS_RXPS_04_FRAME_HDR_LEN16_1:
431 c = xor_mask(wsi, c);
433 wsi->rx_packet_length |= c;
434 wsi->lws_rx_parse_state =
435 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
438 case LWS_RXPS_04_FRAME_HDR_LEN64_8:
439 c = xor_mask(wsi, c);
441 fprintf(stderr, "b63 of length must be zero\n");
442 /* kill the connection */
445 wsi->rx_packet_length = ((size_t)c) << 56;
446 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_7;
449 case LWS_RXPS_04_FRAME_HDR_LEN64_7:
450 wsi->rx_packet_length |= ((size_t)xor_mask(wsi, c)) << 48;
451 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_6;
454 case LWS_RXPS_04_FRAME_HDR_LEN64_6:
455 wsi->rx_packet_length |= ((size_t)xor_mask(wsi, c)) << 40;
456 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_5;
459 case LWS_RXPS_04_FRAME_HDR_LEN64_5:
460 wsi->rx_packet_length |= ((size_t)xor_mask(wsi, c)) << 32;
461 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_4;
464 case LWS_RXPS_04_FRAME_HDR_LEN64_4:
465 wsi->rx_packet_length |= ((size_t)xor_mask(wsi, c)) << 24;
466 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_3;
469 case LWS_RXPS_04_FRAME_HDR_LEN64_3:
470 wsi->rx_packet_length |= ((size_t)xor_mask(wsi, c)) << 16;
471 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_2;
474 case LWS_RXPS_04_FRAME_HDR_LEN64_2:
475 wsi->rx_packet_length |= ((size_t)xor_mask(wsi, c)) << 8;
476 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_1;
479 case LWS_RXPS_04_FRAME_HDR_LEN64_1:
480 wsi->rx_packet_length |= ((size_t)xor_mask(wsi, c));
481 wsi->lws_rx_parse_state =
482 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
485 case LWS_RXPS_EAT_UNTIL_76_FF:
487 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
490 wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING +
491 (wsi->rx_user_buffer_head++)] = c;
493 if (wsi->rx_user_buffer_head != MAX_USER_RX_BUFFER)
496 if (wsi->protocol->callback)
497 wsi->protocol->callback(wsi, LWS_CALLBACK_RECEIVE,
499 &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
500 wsi->rx_user_buffer_head);
501 wsi->rx_user_buffer_head = 0;
503 case LWS_RXPS_SEEN_76_FF:
507 debug("Seen that client is requesting "
508 "a v76 close, sending ack\n");
511 n = libwebsocket_write(wsi, buf, 2, LWS_WRITE_HTTP);
513 fprintf(stderr, "ERROR writing to socket");
516 debug(" v76 close ack sent, server closing skt\n");
517 /* returning < 0 will get it closed in parent */
520 case LWS_RXPS_PULLING_76_LENGTH:
523 case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED:
524 wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING +
525 (wsi->rx_user_buffer_head++)] = xor_mask(wsi, c);
527 if (--wsi->rx_packet_length == 0) {
528 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
531 if (wsi->rx_user_buffer_head != MAX_USER_RX_BUFFER)
535 * is this frame a control packet we should take care of at this
536 * layer? If so service it and hide it from the user callback
539 switch (wsi->opcode) {
540 case LWS_WS_OPCODE_04__CLOSE:
541 /* parrot the close packet payload back */
542 n = libwebsocket_write(wsi, (unsigned char *)
543 &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
544 wsi->rx_user_buffer_head, LWS_WRITE_CLOSE);
545 /* close the connection */
548 case LWS_WS_OPCODE_04__PING:
549 /* parrot the ping packet payload back as a pong*/
550 n = libwebsocket_write(wsi, (unsigned char *)
551 &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
552 wsi->rx_user_buffer_head, LWS_WRITE_PONG);
555 case LWS_WS_OPCODE_04__PONG:
556 /* keep the statistics... */
557 wsi->pings_vs_pongs--;
558 /* ... then just drop it */
559 wsi->rx_user_buffer_head = 0;
567 * No it's real payload, pass it up to the user callback.
568 * It's nicely buffered with the pre-padding taken care of
569 * so it can be sent straight out again using libwebsocket_write
572 wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING +
573 wsi->rx_user_buffer_head] = '\0';
575 if (wsi->protocol->callback)
576 wsi->protocol->callback(wsi, LWS_CALLBACK_RECEIVE,
578 &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
579 wsi->rx_user_buffer_head);
580 wsi->rx_user_buffer_head = 0;
588 int libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c)
591 unsigned char buf[20 + 4];
593 switch (wsi->lws_rx_parse_state) {
596 switch (wsi->ietf_spec_revision) {
597 /* Firefox 4.0b6 likes this as of 30 Oct */
600 wsi->lws_rx_parse_state = LWS_RXPS_SEEN_76_FF;
602 wsi->lws_rx_parse_state =
603 LWS_RXPS_EAT_UNTIL_76_FF;
604 wsi->rx_user_buffer_head = 0;
609 * 04 logical framing from the spec (all this is masked when incoming
610 * and has to be unmasked)
612 * We ignore the possibility of extension data because we don't
613 * negotiate any extensions at the moment.
616 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
617 * +-+-+-+-+-------+-+-------------+-------------------------------+
618 * |F|R|R|R| opcode|R| Payload len | Extended payload length |
619 * |I|S|S|S| (4) |S| (7) | (16/63) |
620 * |N|V|V|V| |V| | (if payload len==126/127) |
622 * +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
623 * | Extended payload length continued, if payload len == 127 |
624 * + - - - - - - - - - - - - - - - +-------------------------------+
625 * | | Extension data |
626 * +-------------------------------+ - - - - - - - - - - - - - - - +
628 * +---------------------------------------------------------------+
629 * : Application data :
630 * +---------------------------------------------------------------+
632 * We pass payload through to userland as soon as we get it, ignoring
633 * FIN. It's up to userland to buffer it up if it wants to see a
634 * whole unfragmented block of the original size (which may be up to
639 * 04 spec defines the opcode like this: (1, 2, and 3 are
640 * "control frame" opcodes which may not be fragmented or
641 * have size larger than 126)
644 * %x0 ; continuation frame
645 * / %x1 ; connection close
649 * / %x5 ; binary frame
656 fprintf(stderr, "Frame has extensions set "
657 "illegally on first framing byte %02X\n", c);
658 /* kill the connection */
662 wsi->opcode = c & 0xf;
663 wsi->final = !!((c >> 7) & 1);
666 wsi->opcode == LWS_WS_OPCODE_04__CONTINUATION &&
667 wsi->rx_packet_length == 0) {
669 "Frame starts with final continuation\n");
670 /* kill the connection */
674 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN;
680 case LWS_RXPS_04_FRAME_HDR_LEN:
684 "Frame has extensions set illegally 4\n");
685 /* kill the connection */
691 /* control frames are not allowed to have big lengths */
692 switch (wsi->opcode) {
693 case LWS_WS_OPCODE_04__CLOSE:
694 case LWS_WS_OPCODE_04__PING:
695 case LWS_WS_OPCODE_04__PONG:
696 fprintf(stderr, "Control frame asking for "
697 "extended length is illegal\n");
698 /* kill the connection */
703 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_2;
706 /* control frames are not allowed to have big lengths */
707 switch (wsi->opcode) {
708 case LWS_WS_OPCODE_04__CLOSE:
709 case LWS_WS_OPCODE_04__PING:
710 case LWS_WS_OPCODE_04__PONG:
711 fprintf(stderr, "Control frame asking for "
712 "extended length is illegal\n");
713 /* kill the connection */
718 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_8;
721 wsi->rx_packet_length = c;
722 wsi->lws_rx_parse_state =
723 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
728 case LWS_RXPS_04_FRAME_HDR_LEN16_2:
729 wsi->rx_packet_length = c << 8;
730 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_1;
733 case LWS_RXPS_04_FRAME_HDR_LEN16_1:
734 wsi->rx_packet_length |= c;
735 wsi->lws_rx_parse_state =
736 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
739 case LWS_RXPS_04_FRAME_HDR_LEN64_8:
741 fprintf(stderr, "b63 of length must be zero\n");
742 /* kill the connection */
745 wsi->rx_packet_length = ((size_t)c) << 56;
746 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_7;
749 case LWS_RXPS_04_FRAME_HDR_LEN64_7:
750 wsi->rx_packet_length |= ((size_t)c) << 48;
751 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_6;
754 case LWS_RXPS_04_FRAME_HDR_LEN64_6:
755 wsi->rx_packet_length |= ((size_t)c) << 40;
756 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_5;
759 case LWS_RXPS_04_FRAME_HDR_LEN64_5:
760 wsi->rx_packet_length |= ((size_t)c) << 32;
761 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_4;
764 case LWS_RXPS_04_FRAME_HDR_LEN64_4:
765 wsi->rx_packet_length |= ((size_t)c) << 24;
766 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_3;
769 case LWS_RXPS_04_FRAME_HDR_LEN64_3:
770 wsi->rx_packet_length |= ((size_t)c) << 16;
771 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_2;
774 case LWS_RXPS_04_FRAME_HDR_LEN64_2:
775 wsi->rx_packet_length |= ((size_t)c) << 8;
776 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_1;
779 case LWS_RXPS_04_FRAME_HDR_LEN64_1:
780 wsi->rx_packet_length |= (size_t)c;
781 wsi->lws_rx_parse_state =
782 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
785 case LWS_RXPS_EAT_UNTIL_76_FF:
787 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
790 wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING +
791 (wsi->rx_user_buffer_head++)] = c;
793 if (wsi->rx_user_buffer_head != MAX_USER_RX_BUFFER)
796 if (wsi->protocol->callback)
797 wsi->protocol->callback(wsi,
798 LWS_CALLBACK_CLIENT_RECEIVE,
800 &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
801 wsi->rx_user_buffer_head);
802 wsi->rx_user_buffer_head = 0;
804 case LWS_RXPS_SEEN_76_FF:
808 debug("Seen that client is requesting "
809 "a v76 close, sending ack\n");
812 n = libwebsocket_write(wsi, buf, 2, LWS_WRITE_HTTP);
814 fprintf(stderr, "ERROR writing to socket");
817 debug(" v76 close ack sent, server closing skt\n");
818 /* returning < 0 will get it closed in parent */
821 case LWS_RXPS_PULLING_76_LENGTH:
824 case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED:
825 wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING +
826 (wsi->rx_user_buffer_head++)] = c;
827 if (--wsi->rx_packet_length == 0) {
828 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
831 if (wsi->rx_user_buffer_head != MAX_USER_RX_BUFFER)
835 * is this frame a control packet we should take care of at this
836 * layer? If so service it and hide it from the user callback
839 switch (wsi->opcode) {
840 case LWS_WS_OPCODE_04__CLOSE:
841 /* parrot the close packet payload back */
842 n = libwebsocket_write(wsi, (unsigned char *)
843 &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
844 wsi->rx_user_buffer_head, LWS_WRITE_CLOSE);
845 /* close the connection */
848 case LWS_WS_OPCODE_04__PING:
849 /* parrot the ping packet payload back as a pong*/
850 n = libwebsocket_write(wsi, (unsigned char *)
851 &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
852 wsi->rx_user_buffer_head, LWS_WRITE_PONG);
855 case LWS_WS_OPCODE_04__PONG:
856 /* keep the statistics... */
857 wsi->pings_vs_pongs--;
858 /* ... then just drop it */
859 wsi->rx_user_buffer_head = 0;
867 * No it's real payload, pass it up to the user callback.
868 * It's nicely buffered with the pre-padding taken care of
869 * so it can be sent straight out again using libwebsocket_write
872 if (wsi->protocol->callback)
873 wsi->protocol->callback(wsi,
874 LWS_CALLBACK_CLIENT_RECEIVE,
876 &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
877 wsi->rx_user_buffer_head);
878 wsi->rx_user_buffer_head = 0;
881 fprintf(stderr, "client rx illegal state\n");
890 int libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
891 unsigned char *buf, size_t len)
896 fprintf(stderr, "received %d byte packet\n", (int)len);
897 for (n = 0; n < len; n++)
898 fprintf(stderr, "%02X ", buf[n]);
899 fprintf(stderr, "\n");
902 /* let the rx protocol state machine have as much as it needs */
906 if (libwebsocket_rx_sm(wsi, buf[n++]) < 0)
914 libwebsocket_04_frame_mask_generate(struct libwebsocket *wsi)
920 /* fetch the per-frame nonce */
922 fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
924 fprintf(stderr, "Unable to open random device %s\n",
925 SYSTEM_RANDOM_FILEPATH);
928 n = read(fd, wsi->frame_masking_nonce_04, 4);
930 fprintf(stderr, "Unable to read from random device %s %d\n",
931 SYSTEM_RANDOM_FILEPATH, n);
937 * the frame key is the frame nonce (4 bytes) followed by the
938 * connection masking key, hashed by SHA1
941 memcpy(buf, wsi->frame_masking_nonce_04, 4);
942 memcpy(buf + 4, wsi->masking_key_04, 20);
944 /* concatenate the nonce with the connection key then hash it */
946 SHA1((unsigned char *)buf, 4 + 20, wsi->frame_mask_04);
948 /* start masking from first byte of masking key buffer */
949 wsi->frame_mask_index = 0;
956 * libwebsocket_write() - Apply protocol then write data to client
957 * @wsi: Websocket instance (available from user callback)
958 * @buf: The data to send. For data being sent on a websocket
959 * connection (ie, not default http), this buffer MUST have
960 * LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer
961 * and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid
962 * in the buffer after (buf + len). This is so the protocol
963 * header and trailer data can be added in-situ.
964 * @len: Count of the data bytes in the payload starting from buf
965 * @protocol: Use LWS_WRITE_HTTP to reply to an http connection, and one
966 * of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
967 * data on a websockets connection. Remember to allow the extra
968 * bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
971 * This function provides the way to issue data back to the client
972 * for both http and websocket protocols.
974 * In the case of sending using websocket protocol, be sure to allocate
975 * valid storage before and after buf as explained above. This scheme
976 * allows maximum efficiency of sending data and protocol in a single
977 * packet while not burdening the user code with any protocol knowledge.
982 int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf,
983 size_t len, enum libwebsocket_write_protocol protocol)
988 unsigned int shift = 7;
990 if (protocol == LWS_WRITE_HTTP)
993 /* websocket protocol, either binary or text */
995 if (wsi->state != WSI_STATE_ESTABLISHED)
998 switch (wsi->ietf_spec_revision) {
999 /* chrome likes this as of 30 Oct */
1000 /* Firefox 4.0b6 likes this as of 30 Oct */
1002 if (protocol == LWS_WRITE_BINARY) {
1003 /* in binary mode we send 7-bit used length blocks */
1005 while (len & (127 << shift)) {
1011 while (shift >= 0) {
1014 ((len >> shift) & 127) | 0x80;
1017 ((len >> shift) & 127);
1024 /* frame type = text, length-free spam mode */
1027 buf[len] = 0xff; /* EOT marker */
1035 case LWS_WRITE_TEXT:
1036 n = LWS_WS_OPCODE_04__TEXT_FRAME;
1038 case LWS_WRITE_BINARY:
1039 n = LWS_WS_OPCODE_04__BINARY_FRAME;
1041 case LWS_WRITE_CLOSE:
1042 n = LWS_WS_OPCODE_04__CLOSE;
1044 case LWS_WRITE_PING:
1045 n = LWS_WS_OPCODE_04__PING;
1046 wsi->pings_vs_pongs++;
1048 case LWS_WRITE_PONG:
1049 n = LWS_WS_OPCODE_04__PONG;
1052 fprintf(stderr, "libwebsocket_write: unknown write "
1053 "opcode / protocol\n");
1058 * We don't really support the metaframe concept with FIN.
1059 * Just set FIN on every packet for now
1078 #if defined __LP64__
1079 buf[-8] = (len >> 56) & 0x7f;
1080 buf[-7] = len >> 48;
1081 buf[-6] = len >> 40;
1082 buf[-5] = len >> 32;
1089 buf[-4] = len >> 24;
1090 buf[-3] = len >> 16;
1100 for (n = 0; n < (len + pre + post); n++)
1101 fprintf(stderr, "%02X ", buf[n - pre]);
1103 fprintf(stderr, "\n");
1107 * Deal with masking if appropriate
1110 if (wsi->client_mode && wsi->ietf_spec_revision == 4) {
1112 if (libwebsocket_04_frame_mask_generate(wsi)) {
1113 fprintf(stderr, "libwebsocket_write: "
1114 "frame mask generation failed\n");
1119 * use the XOR masking against everything we send
1120 * past the frame nonce
1123 for (n = 0; n < (len + pre + post); n++)
1124 buf[n - pre] = xor_mask(wsi, buf[n - pre]);
1126 /* make space for the frame nonce in clear */
1129 /* copy the frame nonce into place */
1130 memcpy(&buf[0 - pre], wsi->frame_masking_nonce_04, 4);
1135 #ifdef LWS_OPENSSL_SUPPORT
1137 n = SSL_write(wsi->ssl, buf - pre, len + pre + post);
1139 fprintf(stderr, "ERROR writing to socket");
1144 n = send(wsi->sock, buf - pre, len + pre + post, 0);
1146 fprintf(stderr, "ERROR writing to socket");
1149 #ifdef LWS_OPENSSL_SUPPORT
1152 debug("written %d bytes to client\n", (int)len);
1159 * libwebsockets_serve_http_file() - Send a file back to the client using http
1160 * @wsi: Websocket instance (available from user callback)
1161 * @file: The file to issue over http
1162 * @content_type: The http content type, eg, text/html
1164 * This function is intended to be called from the callback in response
1165 * to http requests from the client. It allows the callback to issue
1166 * local files down the http link in a single step.
1169 int libwebsockets_serve_http_file(struct libwebsocket *wsi, const char *file,
1170 const char *content_type)
1178 fd = open(file, O_RDONLY);
1180 p += sprintf(p, "HTTP/1.0 400 Bad\x0d\x0a"
1181 "Server: libwebsockets\x0d\x0a"
1184 libwebsocket_write(wsi, (unsigned char *)buf, p - buf,
1191 p += sprintf(p, "HTTP/1.0 200 OK\x0d\x0a"
1192 "Server: libwebsockets\x0d\x0a"
1193 "Content-Type: %s\x0d\x0a"
1194 "Content-Length: %u\x0d\x0a"
1195 "\x0d\x0a", content_type, (unsigned int)stat.st_size);
1197 libwebsocket_write(wsi, (unsigned char *)buf, p - buf, LWS_WRITE_HTTP);
1201 n = read(fd, buf, 512);
1202 libwebsocket_write(wsi, (unsigned char *)buf, n,
1213 * libwebsockets_remaining_packet_payload() - Bytes to come before "overall"
1214 * rx packet is complete
1215 * @wsi: Websocket instance (available from user callback)
1217 * This function is intended to be called from the callback if the
1218 * user code is interested in "complete packets" from the client.
1219 * libwebsockets just passes through payload as it comes and issues a buffer
1220 * additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE
1221 * callback handler can use this API to find out if the buffer it has just
1222 * been given is the last piece of a "complete packet" from the client --
1223 * when that is the case libwebsockets_remaining_packet_payload() will return
1226 * Many protocols won't care becuse their packets are always small.
1230 libwebsockets_remaining_packet_payload(struct libwebsocket *wsi)
1232 return wsi->rx_packet_length;