-
Notifications
You must be signed in to change notification settings - Fork 1
/
fd_recv.c
342 lines (302 loc) · 10.6 KB
/
fd_recv.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
#include "fd.h"
#define PAYLOAD_EXT_16 126
#define PAYLOAD_EXT_64 127
static void unmask_payload(char*, uint64_t, uint32_t);
int fd_recv(fd_socket_t *sock, char *buff){
return 0;
}
static void unmask_payload(char* data, uint64_t len, uint32_t key){
int i;
uint8_t octet;
for(i = 0; i < len; i++){
switch (i % 4){
case 0: octet = (key & 0xFF);
break;
case 1: octet = (key & 0xFF00) >> 8;
break;
case 2: octet = (key & 0xFF0000) >> 16;
break;
case 3: octet = (key & 0xFF000000) >> 24;
break;
}
data[i] = data[i] ^ octet;
}
}
/*
calls recv once, adding the bytes it got into
the socket struct's buffer. returns the number of
bytes that still need to be received.
*/
void fd_recv_nb(struct ev_loop *loop, ev_io *w, int revents){
/*
0 1 2 3
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
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len | Extended payload length |
|I|S|S|S| (4) |A| (7) | (16/64) |
|N|V|V|V| |S| | (if payload len==126/127) |
| |1|2|3| |K| | |
+-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
| Extended payload length continued, if payload len == 127 |
+ - - - - - - - - - - - - - - - +-------------------------------+
| |Masking-key, if MASK set to 1 |
+-------------------------------+-------------------------------+
| Masking-key (continued) | Payload Data |
+-------------------------------- - - - - - - - - - - - - - - - +
: Payload Data continued ... :
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Payload Data continued ... |
+---------------------------------------------------------------+
*/
int status = 0, fin = 0, rsv1 = 0, rsv2 = 0, rsv3 = 0,
is_masked = 0, offset = 0, i;
uint8_t payload_len = 0, temp = 0;
uint16_t ext_payload_len1 = 0, temp1 = 0;
uint32_t mask_key = 0, temp3 = 0;
uint64_t ext_payload_len2 = 0, temp2 = 0;
unsigned int opcode = 0;
char byte;
fd_socket_t *socket = wtos(w, read_w);
assert_event(EV_READ);
/* if it's the first call, receive enough to get the payload size */
if(socket->__internal.bytes_received < MAX_HEADER_LEN){
/* call recv once, saving the byte count received in status */
if((status =
recv(socket->tcp_sock,
socket->__internal.buffer +
socket->__internal.bytes_received,
MAX_HEADER_LEN,
0)) < 0){
/* since we're nonblocking, these are ok */
if(errno != EAGAIN && errno != EWOULDBLOCK){
perror(__FILE__);
exit(errno);
}
fd_log_w("recv invoked, but returned EAGAIN or EWOULDBLOCK\n");
return;
}
}
/* now we know how big the buffer is */
else {
/* call recv once, saving the byte count received in status */
if((status =
recv(socket->tcp_sock,
socket->__internal.buffer +
socket->__internal.bytes_received,
socket->__internal.bytes_expected -
socket->__internal.bytes_received,
0)) < 0){
/* since we're nonblocking, these are ok */
if(errno != EAGAIN && errno != EWOULDBLOCK){
perror(__FILE__);
exit(errno);
}
fd_log_w("recv invoked, but returned EAGAIN or EWOULDBLOCK\n");
return;
}
}
if (status == 0) {
fd_socket_destroy(socket->id);
return;
}
/* increment recv count */
socket->__internal.recvs += 1;
socket->__internal.just_opened = 0;
fd_log_i("bytes received in call #%d: %d\n",
socket->__internal.recvs, status);
/* update the number of received bytes on the socket */
socket->__internal.bytes_received += status;
/* if we have not processed the header */
if(socket->__internal.bytes_expected == 0){
/* if the first two bytes are available,
and we haven't determined the header length */
if(socket->__internal.bytes_received > 2 &&
socket->__internal.header_len == 0){
/* process the first byte */
byte = *(socket->__internal.buffer);
temp = (uint8_t) byte;
fin = (temp & 0x80) >> 7;
rsv1 = (temp & 0x40) >> 6;
rsv2 = (temp & 0x20) >> 5;
rsv3 = (temp & 0x10) >> 4;
opcode = (temp & 0xF);
socket->__internal.last_recv_opcode = opcode;
/* process the second byte */
byte = *(socket->__internal.buffer + 1);
temp = (uint8_t) byte;
is_masked = (temp & 0x80) >> 7;
payload_len = temp & 0x7F;
fd_log_d("is_masked val: %d\n",is_masked);
/* save the opcode and fin bit */
socket->__internal.fin = fin;
socket->__internal.opcode = opcode;
/* assign the header_len on the socket */
if(is_masked){
if(payload_len < PAYLOAD_EXT_16)
socket->__internal.header_len = 6;
else if(payload_len == PAYLOAD_EXT_16)
socket->__internal.header_len = 8;
else if(payload_len == PAYLOAD_EXT_64)
socket->__internal.header_len = 14;
else{
/* what the hell is going on if we're here? */
fd_log_c("unknown payload length in the websocket header"
"and the data is not masked, "
"\"WHAT THE HELL IS GOING ON OUT HERE?\"\n");
}
} else{
/* why would it not be masked?
I guess we should still do this */
if(payload_len < PAYLOAD_EXT_16)
socket->__internal.header_len = 2;
else if(payload_len == PAYLOAD_EXT_16)
socket->__internal.header_len = 4;
else if(payload_len == PAYLOAD_EXT_64)
socket->__internal.header_len = 10;
else{
/* what the hell is going on if we're here? */
fd_log_c("unknown payload length in the websocket header"
"and the data is not masked, "
"\"WHAT THE HELL IS GOING ON OUT HERE?\"\n");
}
}
}
/* if we have received the header,
but we haven't determined the payload length*/
if(socket->__internal.header_len > 0 &&
socket->__internal.bytes_received >=
socket->__internal.header_len){
/* (re)scan the second byte */
byte = *(socket->__internal.buffer + 1);
temp = (uint8_t) byte;
is_masked = (temp & 0x80) >> 7;
payload_len = temp & 0x7F;
/* determine payload length */
if(payload_len < PAYLOAD_EXT_16){
socket->__internal.bytes_expected =
payload_len + socket->__internal.header_len;
offset = 2;
}
else if(payload_len == PAYLOAD_EXT_16){
/* scan 2 bytes out of the buffer */
ext_payload_len1 = 0;
for(i = 0; i < 2; i++){
byte = *(socket->__internal.buffer + 2 + i);
temp1 = (uint16_t) byte & 0xFF;
temp1 = temp1 << (8 - (8*i));
ext_payload_len1 |= temp1;
}
socket->__internal.bytes_expected =
ext_payload_len1 + socket->__internal.header_len;
offset = 4;
}
else if(payload_len == PAYLOAD_EXT_64){
/* scan 8 bytes out of the buffer */
ext_payload_len2 = 0;
for(i = 0; i < 8; i++){
byte = *(socket->__internal.buffer + 2 + i);
temp2 = (uint64_t) byte & 0xFF;
temp2 = temp2 << (56 - (8*i));
ext_payload_len2 |= temp2;
}
socket->__internal.bytes_expected =
ext_payload_len2 + socket->__internal.header_len;
offset = 10;
}
/* adjust the buffer size appropriately */
socket->__internal.buffer =
realloc(socket->__internal.buffer,
socket->__internal.bytes_expected + 1);
if(socket->__internal.buffer == NULL){
/* handle the error */
}
memset(socket->__internal.buffer +
socket->__internal.bytes_received, 0,
socket->__internal.bytes_expected -
socket->__internal.bytes_received);
/* save the mask key */
if(is_masked){
mask_key = 0;
for(i = 0; i < 4; i++){
byte = *(socket->__internal.buffer + offset + i);
temp3 = (uint32_t) byte & 0xFF;
temp3 = temp3 << (24 - (8*i));
mask_key |= temp3;
}
/* Reverse order of the mask */
mask_key =
(0xFF000000 & (mask_key << 24)) |
(0x00FF0000 & (mask_key << 8)) |
(0x0000FF00 & (mask_key >> 8)) |
(0x000000FF & (mask_key >> 24));
socket->__internal.mask_key = mask_key;
socket->__internal.mask_start = 0;
}
}
}
fd_log_i("total bytes_received: %d\n",
(int) socket->__internal.bytes_received);
fd_log_i("total bytes_expected: %d\n",
(int) socket->__internal.bytes_expected);
/* if we have received the full payload */
if(socket->__internal.bytes_received ==
socket->__internal.bytes_expected){
socket->__internal.buffer[socket->__internal.bytes_expected] = '\0';
fd_log_i("done receiving\n");
/* unmask the payload */
if(socket->__internal.mask_key){
unmask_payload(socket->__internal.buffer +
socket->__internal.header_len,
socket->__internal.bytes_received -
socket->__internal.header_len,
socket->__internal.mask_key);
}
/* handle the different known opcodes */
switch(socket->__internal.opcode){
case TEXT:
case BINARY:
case CONTINUATION:
/* notify the "data available" callback that the recv is done */
/* TODO: what if this fails? */
socket->data_cb(socket, socket->__internal.buffer +
socket->__internal.header_len);
break;
case PING:
/* send a pong */
status = fd_send(socket, socket->__internal.buffer, PONG);
break;
case PONG:
/* check APPLICATION data is the same as in the PING (how?) */
break;
case CONNECTION_CLOSE:
/* send a matching CLOSE message and close the socket gracefully */
fd_log_i("close message received\n");
status = fd_send(socket, socket->__internal.buffer +
socket->__internal.header_len,
CONNECTION_CLOSE);
fd_socket_destroy(socket->tcp_sock);
break;
}
/* if this is the final message in a fragment,
and the whole thing fit in the buffer */
/* TODO: handle failure */
if(socket->__internal.fin){
if(socket->end_cb)
socket->end_cb(socket, socket->__internal.buffer +
socket->__internal.header_len);
}
/* Reset data in socket struct to get ready for next recv */
socket->__internal.buffer = realloc(socket->__internal.buffer,
MAX_HEADER_LEN);
if(socket->__internal.buffer == NULL){
/* TODO: handle the error */
}
memset(socket->__internal.buffer, 0, MAX_HEADER_LEN);
socket->__internal.recvs = 0;
socket->__internal.bytes_expected = 0;
socket->__internal.bytes_received = 0;
socket->__internal.header_len = 0;
socket->__internal.mask_key = 0;
socket->__internal.mask_start = 0;
}
}