-
Notifications
You must be signed in to change notification settings - Fork 6
/
websocketd.c
1423 lines (1122 loc) · 41.3 KB
/
websocketd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// websocketd.c - lwIP websocket daemon implementation
//
// v2.7 / 2023-07-09 / Io Engineering / Terje
//
/*
Copyright (c) 2019-2023, Terje Io
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef ARDUINO
#include "../driver.h"
#else
#include "driver.h"
#endif
#if WEBSOCKET_ENABLE
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "networking.h"
#include "base64.h"
#include "sha1.h"
#include "utils.h"
#include "strutils.h"
#include "websocketd.h"
#include "grbl/grbl.h"
#include "grbl/protocol.h"
//#define WSDEBUG
#define CRLF "\r\n"
#define SOCKET_TIMEOUT 0
#define MAX_HTTP_HEADER_SIZE 512
#define FRAME_NONE 0xFF
#ifndef WEBSOCKETD_TCP_PRIO
#define WEBSOCKETD_TCP_PRIO TCP_PRIO_MIN
#endif
#ifndef WEBSOCKETD_POLL_INTERVAL
#define WEBSOCKETD_POLL_INTERVAL 2
#endif
#ifndef WEBUI_MAX_CLIENTS
#define WEBUI_MAX_CLIENTS 4
#endif
#define WEBSOCKETD_MAGIC 1819047252
PROGMEM static const char WS_GUID[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
PROGMEM static const char WS_KEY[] = "Sec-WebSocket-Key: ";
PROGMEM static const char WS_PROT[] = "Sec-WebSocket-Protocol: ";
PROGMEM static const char WS_RSP[] = "HTTP/1.1 101 Switching Protocols" CRLF \
"Upgrade: websocket" CRLF \
"Connection: Upgrade" CRLF \
"Sec-WebSocket-Accept: ";
PROGMEM static const char HTTP_400[] = "HTTP/1.1 400" CRLF \
"Status: 400 Bad Request" CRLF CRLF;
PROGMEM static const char HTTP_500[] = "HTTP/1.1 500" CRLF \
"Status: 500 Internal Server Error" CRLF CRLF;
typedef enum {
WsOpcode_Continuation = 0x00,
WsOpcode_Text = 0x1,
WsOpcode_Binary = 0x2,
WsOpcode_Close = 0x8,
WsOpcode_Ping = 0x9,
WsOpcode_Pong = 0xA
} websocket_opcode_t;
typedef enum
{
WsState_Free,
WsState_Idle,
WsState_Connecting,
WsState_Connected,
WsState_Closing
} websocket_state_t;
typedef union {
uint8_t token;
struct {
uint8_t opcode :4,
rsv3 :1,
rsv2 :1,
rsv1 :1,
fin :1;
};
} ws_frame_start_t;
typedef struct {
uint32_t idx;
uint32_t payload_len;
uint32_t payload_rem;
uint32_t rx_index;
uint8_t *frame;
uint32_t mask;
bool masked;
bool complete;
uint8_t data[13];
} frame_header_t;
typedef struct {
struct pbuf *p;
struct pbuf *q;
uint_fast16_t len;
void *payload;
} packet_chain_t;
typedef union {
uint8_t value;
struct {
uint8_t connected :1,
webui_connected :1,
unused :6;
};
} ws_stream_state_t;
typedef struct ws_sessiondata
{
uint32_t magic;
const io_stream_t *stream;
ws_stream_state_t stream_state;
websocket_state_t state;
ws_frame_start_t ftype;
websocket_opcode_t fragment_opcode;
ws_frame_start_t start;
frame_header_t header;
uint32_t timeout;
uint32_t timeoutMax;
struct tcp_pcb *pcb;
packet_chain_t packet;
TickType_t lastSendTime;
err_t lastErr;
uint8_t errorCount;
uint8_t pingCount;
char *http_request;
uint8_t *payload;
bool collect_payload;
uint32_t hdrsize;
websocket_on_frame_received_ptr on_txt_frame_received;
websocket_on_frame_received_ptr on_bin_frame_received;
} ws_sessiondata_t;
typedef struct {
ws_sessiondata_t *session;
stream_rx_buffer_t rxbuf;
stream_tx_buffer_t txbuf;
} ws_streambuffers_t;
typedef struct {
ws_stream_state_t state;
io_stream_properties_t prop;
} ws_stream_t;
static void websocket_stream_handler (ws_sessiondata_t *session);
static const ws_frame_start_t wshdr_txt = {
.fin = true,
.opcode = WsOpcode_Text
};
static const ws_frame_start_t wshdr_bin = {
.fin = true,
.opcode = WsOpcode_Binary
};
static const ws_frame_start_t wshdr_ping = {
.fin = true,
.opcode = WsOpcode_Ping
};
static const ws_sessiondata_t defaultSettings =
{
.magic = WEBSOCKETD_MAGIC,
.stream = NULL,
.stream_state.connected = true,
.state = WsState_Free,
.fragment_opcode = WsOpcode_Continuation,
.start.token = FRAME_NONE,
// .ftype = wshdr_txt,
.timeout = 0,
.timeoutMax = SOCKET_TIMEOUT,
.pcb = NULL,
.packet = {0},
.header = {0},
.lastSendTime = 0,
.errorCount = 0,
.pingCount = 0,
.lastErr = ERR_OK,
.http_request = NULL,
.hdrsize = MAX_HTTP_HEADER_SIZE,
.payload = NULL,
.collect_payload = false,
.on_txt_frame_received = NULL,
.on_bin_frame_received = NULL
};
static const io_stream_t *claim_stream (uint32_t baud_rate);
static tcp_server_t ws_server;
static ws_sessiondata_t clients[WEBUI_MAX_CLIENTS] = {0};
static ws_streambuffers_t streambuffers = {0};
static ws_stream_t ws_streams[] = {
{
.state.connected = Off,
.prop = {
.type = StreamType_WebSocket,
.instance = 10,
.flags.claimable = On,
.flags.claimed = Off,
.flags.can_set_baud = Off,
.flags.modbus_ready = Off,
.claim = claim_stream
}
}
};
static enqueue_realtime_command_ptr enqueue_realtime_command = protocol_enqueue_realtime_command;
#if ESP_PLATFORM
static portMUX_TYPE rx_mux = portMUX_INITIALIZER_UNLOCKED;
#endif
websocket_events_t websocket;
//
// streamGetC - returns -1 if no data available
//
static int16_t streamGetC (void)
{
int16_t data;
if(streambuffers.rxbuf.tail == streambuffers.rxbuf.head)
return SERIAL_NO_DATA; // no data available else EOF
data = streambuffers.rxbuf.data[streambuffers.rxbuf.tail]; // Get next character
streambuffers.rxbuf.tail = BUFNEXT(streambuffers.rxbuf.tail, streambuffers.rxbuf); // and update pointer
return data;
}
static inline uint16_t streamRxCount (void)
{
uint_fast16_t head = streambuffers.rxbuf.head, tail = streambuffers.rxbuf.tail;
return BUFCOUNT(head, tail, RX_BUFFER_SIZE);
}
static uint16_t streamRxFree (void)
{
return (RX_BUFFER_SIZE - 1) - streamRxCount();
}
static void streamRxFlush (void)
{
streambuffers.rxbuf.tail = streambuffers.rxbuf.head;
}
static void websocketd_RxCancel (void)
{
streambuffers.rxbuf.data[streambuffers.rxbuf.head] = ASCII_CAN;
streambuffers.rxbuf.tail = streambuffers.rxbuf.head;
streambuffers.rxbuf.head = BUFNEXT(streambuffers.rxbuf.head, streambuffers.rxbuf);
}
static bool streamSuspendInput (bool suspend)
{
return stream_rx_suspend(&streambuffers.rxbuf, suspend);
}
bool websocketd_RxPutC (char c)
{
bool ok, overflow = false;
// discard input if MPG has taken over...
if((ok = streambuffers.session && streambuffers.session->state == WsState_Connected && hal.stream.type != StreamType_MPG)) {
#if ESP_PLATFORM
taskENTER_CRITICAL(&rx_mux);
#else
taskENTER_CRITICAL();
#endif
if(!enqueue_realtime_command(c)) { // If not a real time command attempt to buffer it
uint_fast16_t next_head = BUFNEXT(streambuffers.rxbuf.head, streambuffers.rxbuf);
if((overflow = next_head == streambuffers.rxbuf.tail)) // If buffer full
streambuffers.rxbuf.overflow = true; // flag overflow
streambuffers.rxbuf.data[streambuffers.rxbuf.head] = c; // add data to buffer
streambuffers.rxbuf.head = next_head; // and update pointer
}
#if ESP_PLATFORM
taskEXIT_CRITICAL(&rx_mux);
#else
taskEXIT_CRITICAL();
#endif
}
return ok && !overflow;
}
static bool streamPutC (const char c)
{
uint_fast16_t next_head = BUFNEXT(streambuffers.txbuf.head, streambuffers.txbuf);
while(streambuffers.txbuf.tail == next_head) { // Buffer full, block until space is available...
if(!hal.stream_blocking_callback())
return false;
}
streambuffers.txbuf.data[streambuffers.txbuf.head] = c; // Add data to buffer
streambuffers.txbuf.head = next_head; // and update head pointer
return true;
}
static void streamWriteS (const char *data)
{
char c, *ptr = (char *)data;
while((c = *ptr++) != '\0')
streamPutC(c);
}
static void streamWrite (const char *data, uint16_t length)
{
char *ptr = (char *)data;
while(length--)
streamPutC(*ptr++);
}
static uint16_t streamTxCount (void) {
uint_fast16_t head = streambuffers.txbuf.head, tail = streambuffers.txbuf.tail;
return BUFCOUNT(head, tail, TX_BUFFER_SIZE);
}
static int16_t streamTxGetC (void)
{
int16_t data;
if(streambuffers.txbuf.tail == streambuffers.txbuf.head)
return SERIAL_NO_DATA; // no data available else EOF
data = streambuffers.txbuf.data[streambuffers.txbuf.tail]; // Get next character
streambuffers.txbuf.tail = BUFNEXT(streambuffers.txbuf.tail, streambuffers.txbuf); // and update pointer
return data;
}
static void streamTxFlush (void)
{
streambuffers.txbuf.tail = streambuffers.txbuf.head;
}
static bool streamEnqueueRtCommand (char c)
{
return enqueue_realtime_command(c);
}
static enqueue_realtime_command_ptr streamSetRtHandler (enqueue_realtime_command_ptr handler)
{
enqueue_realtime_command_ptr prev = enqueue_realtime_command;
if(handler)
enqueue_realtime_command = handler;
return prev;
}
static void streamClose (ws_sessiondata_t *session)
{
// Switch I/O stream back to default
if(session->stream) {
stream_disconnect(session->stream);
session->stream = NULL;
streambuffers.session = NULL;
ws_streams[0].state.connected = false;
streamRxFlush();
streamTxFlush();
}
}
bool websocket_register_frame_handler (websocket_t *session, websocket_on_frame_received_ptr handler, bool binary)
{
bool ok;
if((ok = session && ((ws_sessiondata_t *)session)->magic == WEBSOCKETD_MAGIC)) {
if(binary)
((ws_sessiondata_t *)session)->on_bin_frame_received = handler;
else
((ws_sessiondata_t *)session)->on_txt_frame_received = handler;
}
return ok;
}
static bool is_connected (void)
{
return ws_streams[0].state.connected;
}
static const io_stream_t *claim_stream (uint32_t baud_rate)
{
static const io_stream_t stream = {
.type = StreamType_WebSocket,
.is_connected = is_connected,
.read = streamGetC,
.write = streamWriteS,
.write_n = streamWrite,
.write_char = streamPutC,
.enqueue_rt_command = streamEnqueueRtCommand,
.get_rx_buffer_free = streamRxFree,
.reset_write_buffer = streamTxFlush,
.reset_read_buffer = streamRxFlush,
.cancel_read_buffer = websocketd_RxCancel,
.suspend_read = streamSuspendInput,
.set_enqueue_rt_handler = streamSetRtHandler
};
if(ws_streams[0].prop.flags.claimed)
return NULL;
if(baud_rate != 0)
ws_streams[0].prop.flags.claimed = On;
return &stream;
}
bool websocket_send_frame (websocket_t *session, const void *data, size_t size, bool is_binary)
{
uint8_t *msg;
size_t hdr_len = size >= 126 ? 4 : 2;
if(session == NULL || ((ws_sessiondata_t *)session)->magic != WEBSOCKETD_MAGIC)
return false;
if((msg = malloc(size + hdr_len))) {
memcpy(msg + hdr_len, data, size);
msg[0] = is_binary ? wshdr_bin.token : wshdr_txt.token;
msg[1] = size < 126 ? size : 126;
if(size >= 126) {
msg[2] = (size >> 8) & 0xFF;
msg[3] = size & 0xFF;
}
if(tcp_write(((ws_sessiondata_t *)session)->pcb, msg, (u16_t)(size + hdr_len), TCP_WRITE_FLAG_COPY) == ERR_OK)
tcp_output(((ws_sessiondata_t *)session)->pcb);
((ws_sessiondata_t *)session)->lastSendTime = xTaskGetTickCount();
free(msg);
}
return msg != 0;
}
bool websocket_broadcast_frame (const void *data, size_t size, bool is_binary)
{
uint_fast16_t idx = WEBUI_MAX_CLIENTS;
do {
if(clients[--idx].state == WsState_Connected)
websocket_send_frame(&clients[idx], data, size, is_binary);
} while(idx);
return true;
}
bool websocket_set_stream_flags (websocket_t *session, io_stream_state_t stream_state)
{
if(session == NULL || ((ws_sessiondata_t *)session)->magic != WEBSOCKETD_MAGIC)
return false;
if((((ws_sessiondata_t *)session)->stream_state.webui_connected = stream_state.webui_connected))
((ws_sessiondata_t *)session)->stream_state.connected = On;
return true;
}
//
// TCP handlers
//
static void websocket_state_free (ws_sessiondata_t *session)
{
session->magic = 0; // Invalidate session
// Free any buffer chain currently beeing processed
if(session->packet.p) {
pbuf_free(session->packet.p);
session->packet.p = NULL;
}
// Free any http request currently beeing processed
if(session->http_request) {
free(session->http_request);
session->http_request = NULL;
session->hdrsize = MAX_HTTP_HEADER_SIZE;
}
if(session->payload) {
free(session->payload);
session->payload = NULL;
session->collect_payload = false;
}
if(session->header.frame) {
free(session->header.frame);
session->header.frame = NULL;
}
}
static void websocket_unlink_session (ws_sessiondata_t *session)
{
session->magic = 0; // Invalidate session
session->state = WsState_Free;
websocket_state_free(session);
streamClose(session);
if(websocket.on_client_disconnect)
websocket.on_client_disconnect(session);
}
static void websocket_err (void *arg, err_t err)
{
ws_sessiondata_t *session = arg;
session->pcb = NULL;
websocket_unlink_session(session);
}
static err_t websocket_poll (void *arg, struct tcp_pcb *pcb)
{
ws_sessiondata_t *session = arg;
if(!session)
tcp_close(pcb);
else {
session->timeout++;
if(session->timeoutMax && session->timeout > session->timeoutMax)
tcp_abort(pcb);
}
return ERR_OK;
}
static void websocket_close_conn (ws_sessiondata_t *session, struct tcp_pcb *pcb)
{
session->pcb = NULL;
websocket_unlink_session(session);
tcp_arg(pcb, NULL);
tcp_recv(pcb, NULL);
tcp_sent(pcb, NULL);
tcp_err(pcb, NULL);
tcp_poll(pcb, NULL, 0);
if (tcp_close(pcb) != ERR_OK)
tcp_poll(pcb, websocket_poll, WEBSOCKETD_POLL_INTERVAL);
}
//
// Process data for streaming
//
static bool collect_msg_frame (frame_header_t *header, uint8_t *payload, uint32_t len)
{
if(header->payload_rem > len && header->payload_rem == header->payload_len) {
if((header->frame = malloc(header->payload_len + header->idx)))
memcpy(header->frame, &header->data, header->idx);
}
header->payload_rem -= len;
if(header->frame)
memcpy(header->frame + header->idx + header->payload_len - header->payload_rem - 1, payload, len);
return header->frame != NULL;
}
static uint32_t websocket_msg_parse (ws_sessiondata_t *session, uint8_t *payload, uint32_t len)
{
bool frame_done = false;
uint32_t plen = len;
// Collect frame header
while(!session->header.complete && plen) {
session->header.data[session->header.idx++] = *payload++;
if(session->header.idx == 2) {
session->header.masked = session->header.data[1] & 0x80; // always true from client
session->header.payload_len = session->header.data[1] & 0x7F;
}
if(session->header.idx >= 6) {
if((session->header.complete = (session->header.idx == (session->header.payload_len == 126 ? 8 : 6)))) {
if(session->header.payload_len == 126) {
session->header.payload_len = (session->header.data[2] << 8) | session->header.data[3];
memcpy(&session->header.mask, &session->header.data[4], sizeof(uint32_t));
} else
memcpy(&session->header.mask, &session->header.data[2], sizeof(uint32_t));
session->header.payload_rem = session->header.payload_len;
}
}
plen--;
}
// if(session->start.token != FRAME_NONE)
// DEBUG_PRINT("\r\n!span\r\n");
// Process frame
if (session->header.complete && (plen || session->header.payload_rem == 0)) {
bool is_binary = false;
ws_frame_start_t fs = (ws_frame_start_t)session->header.data[0];
if(!fs.fin && (websocket_opcode_t)fs.opcode != WsOpcode_Continuation)
session->fragment_opcode = (websocket_opcode_t)fs.opcode;
if((websocket_opcode_t)fs.opcode == WsOpcode_Continuation)
fs.opcode = session->fragment_opcode;
switch ((websocket_opcode_t)fs.opcode) {
case WsOpcode_Continuation:
// Something went wrong, exit fragment handling mode
session->fragment_opcode = WsOpcode_Continuation;
break;
case WsOpcode_Binary:
is_binary = true;
// session->ftype = wshdr_bin; // Switch to binary responses if client talks binary to us
// No break
case WsOpcode_Text:
if(fs.fin)
session->fragment_opcode = WsOpcode_Continuation;
if(session->header.payload_rem == session->header.payload_len && ((session->on_txt_frame_received && !is_binary) || (session->on_bin_frame_received && is_binary))) {
if((session->collect_payload = frame_done = plen >= session->header.payload_len))
session->payload = payload;
else
session->collect_payload = session->header.payload_len > 0 && (session->payload = malloc(session->header.payload_len + is_binary ? 0 : 1)) != NULL;
//TODO: handle malloc failure?
}
if(session->header.payload_rem) {
uint8_t *mask = (uint8_t *)&session->header.mask;
uint_fast16_t payload_len = session->header.payload_rem > plen ? plen : session->header.payload_rem;
session->start.token = session->header.payload_rem > plen ? fs.token : FRAME_NONE;
/*
if(session->start.token != FRAME_NONE)
DEBUG_PRINT("\r\n!span!\r\n");
if(session->rxbuf.overflow)
DEBUG_PRINT("\r\n!overflow\r\n");
DEBUG_PRINT("\r\nPLEN:");
DEBUG_PRINT(uitoa(session->header.payload_rem));
DEBUG_PRINT(" ");
DEBUG_PRINT(uitoa(payload_len));
DEBUG_PRINT("\r\n");
*/
if(session->collect_payload) { // collect the complete frame before processing
if(session->header.payload_rem && !frame_done) {
plen = 0;
memcpy(session->payload + (session->header.payload_len - session->header.payload_rem), payload, payload_len);
if((frame_done = (session->header.payload_rem = session->header.payload_rem - payload_len) == 0)) {
if(!is_binary)
*(session->payload + session->header.payload_len) = '\0';
}
}
if(frame_done) {
uint_fast16_t i = 0, j;
uint8_t *pm = session->payload;
websocket_on_frame_received_ptr on_received = is_binary ? session->on_bin_frame_received : session->on_txt_frame_received;
// Unmask data
for(j = 0; j < session->header.payload_len; j++)
*pm++ ^= mask[i++ % 4];
on_received(session, session->payload, session->header.payload_len);
if(session->payload == payload) { // not malloc'ed?
plen = 0;
session->payload = NULL;
}
}
} else if(session == streambuffers.session && session->stream_state.connected) { // Unmask and push into RX buffer on the go
uint_fast16_t i = session->header.rx_index;
streambuffers.rxbuf.overflow = false;
while (payload_len--) {
if(!websocketd_RxPutC(*payload++ ^ mask[i % 4]))
break; // If overflow pend buffering rest of data until next polling
plen--;
i++;
}
session->header.rx_index = i;
frame_done = (session->header.payload_rem = session->header.payload_len - session->header.rx_index) == 0;
} else { // No client, sink payload
plen = 0;
frame_done = (session->header.payload_rem = session->header.payload_rem - payload_len) == 0;
}
}
break;
case WsOpcode_Close:
if((frame_done = plen >= session->header.payload_rem)) {
plen -= session->header.payload_rem;
if(collect_msg_frame(&session->header, payload, session->header.payload_rem))
payload = session->header.frame;
tcp_write(session->pcb, payload, session->header.payload_len, TCP_WRITE_FLAG_COPY);
tcp_output(session->pcb);
session->state = WsState_Closing;
} else {
collect_msg_frame(&session->header, payload, plen);
plen = 0;
}
break;
case WsOpcode_Ping:
if((frame_done = plen >= session->header.payload_rem)) {
if(session->state != WsState_Closing) {
plen -= session->header.payload_rem;
if(collect_msg_frame(&session->header, payload, session->header.payload_rem))
payload = session->header.frame;
uint8_t *pong;
uint_fast16_t hdr_len = session->header.payload_len < 126 ? 2 : 4;
if((pong = (uint8_t *)malloc(session->header.payload_len + hdr_len))) {
uint_fast16_t i = session->header.rx_index, j;
uint8_t *mask = (uint8_t *)&session->header.mask, *pm = payload, *buf = pong;
fs.opcode = WsOpcode_Pong;
*buf++ = fs.token;
*buf++ = session->header.payload_len < 126 ? session->header.payload_len : 126;
if(session->header.payload_len >= 126) {
*buf++ = (session->header.payload_len >> 8) & 0xFF;
*buf++ = session->header.payload_len & 0xFF;
}
for(j = 0; j < session->header.payload_len; j++)
*buf++ = *pm++ ^ mask[i++ % 4];
tcp_write(session->pcb, pong, session->header.payload_len + hdr_len, TCP_WRITE_FLAG_COPY);
tcp_output(session->pcb);
free(pong);
}
}
} else {
collect_msg_frame(&session->header, payload, plen);
plen = 0;
}
break;
case WsOpcode_Pong:
if((frame_done = plen >= session->header.payload_rem)) {
session->pingCount = 0;
plen -= session->header.payload_rem;
} else {
session->header.payload_rem -= plen;
plen = 0;
}
break;
default:
// Unsupported/undefined opcode - ditch any payload(?)
if((frame_done = plen >= session->header.payload_rem))
plen -= session->header.payload_rem;
else {
session->header.payload_rem -= plen;
plen = 0;
}
break;
}
if(frame_done) {
if(session->payload) {
free(session->payload);
session->payload = NULL;
session->collect_payload = false;
}
if(session->header.frame)
free(session->header.frame);
memset(&session->header, 0, sizeof(frame_header_t));
}
}
return len - plen;
}
//
// Queue incoming packet for processing
//
static err_t websocket_recv (void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
ws_sessiondata_t *session = arg;
if(err != ERR_OK || p == NULL || session == NULL) {
if (p != NULL) {
tcp_recved(pcb, p->tot_len);
pbuf_free(p);
}
websocket_close_conn(session, pcb);
} else if(session->packet.p == NULL) {
struct pbuf *q = p;
uint8_t *payload = q->payload;
uint_fast16_t len = q->len, processed, taken = 0;
while(q) {
processed = websocket_msg_parse(session, payload, len);
payload += processed;
taken += processed;
len -= processed;
if(streambuffers.rxbuf.overflow)
break;
if(len == 0 && (q = q->next)) {
len = q->len;
payload = q->payload;
}
}
tcp_recved(session->pcb, taken);
if(q == NULL) {
pbuf_free(p);
session->packet.p = NULL;
websocket_stream_handler(session);
} else {
session->packet.p = p;
session->packet.q = q;
session->packet.len = q->len - (payload - (uint8_t *)q->payload);
session->packet.payload = payload;
}
}
return ERR_OK;
}
static err_t websocket_sent (void *arg, struct tcp_pcb *pcb, u16_t ui16len)
{
((ws_sessiondata_t *)arg)->timeout = 0;
return ERR_OK;
}
/** Call tcp_write() in a loop trying smaller and smaller length
*
* @param pcb tcp_pcb to send
* @param ptr Data to send
* @param length Length of data to send (in/out: on return, contains the
* amount of data sent)
* @param apiflags directly passed to tcp_write
* @return the return value of tcp_write
*/
static err_t http_write (struct tcp_pcb *pcb, const void *ptr, u16_t *length, u8_t apiflags)
{
u16_t len;
err_t err;
LWIP_ASSERT("length != NULL", length != NULL);
len = *length;
if (len == 0)
return ERR_OK;
do {
if ((err = tcp_write(pcb, ptr, len, apiflags)) == ERR_MEM) {
if (tcp_sndbuf(pcb) == 0 || tcp_sndqueuelen(pcb) >= TCP_SND_QUEUELEN)
/* no need to try smaller sizes */
len = 1;
else
len /= 2;
}
} while (err == ERR_MEM && len > 1);
*length = len;
return err;
}
static void http_write_error (ws_sessiondata_t *session, const char *status)
{
uint16_t len = strlen(status);
http_write(session->pcb, status, &len, TCP_WRITE_FLAG_COPY);
session->state = WsState_Closing;
}
bool websocket_claim_stream (websocket_t *websocket)
{
const io_stream_t *stream;
ws_sessiondata_t *session = (ws_sessiondata_t *)websocket;
if(session && session->magic == WEBSOCKETD_MAGIC && (stream = claim_stream(0))) {
if(hal.stream.type == StreamType_WebSocket || !session->stream_state.connected)
return session->stream != NULL;
stream_connect(stream);
if(hal.stream.type == StreamType_WebSocket || hal.stream.state.webui_connected) {
session->stream = stream;
streambuffers.session = session;
hal.stream.state.webui_connected = session->stream_state.webui_connected;
}
ws_streams[0].state.connected = true;
}
return hal.stream.type == StreamType_WebSocket;
}
//
// Process connection handshake
//
static err_t http_recv (void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
static uint32_t ptr = 0;
ws_sessiondata_t *session = arg;
bool hdr_ok;
if(err != ERR_OK || p == NULL || session == NULL) {
if (p != NULL) {
tcp_recved(pcb, p->tot_len);
pbuf_free(p);
}
websocket_close_conn(session, pcb);
return ERR_OK;
}
if(session->http_request == NULL) {
ptr = 0;
if((session->http_request = malloc(session->hdrsize)) == NULL) {
http_write_error(session, HTTP_500);
return ERR_MEM;