2 * Copyright (C) 2016 "IoT.bzh"
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * This work is a far adaptation of apache-websocket:
19 * origin: https://github.com/disconnect/apache-websocket
20 * commit: cfaef071223f11ba016bff7e1e4b7c9e5df45b50
21 * Copyright 2010-2012 self.disconnect (APACHE-2)
32 #define BLOCK_DATA_SIZE 4096
34 #define FRAME_GET_FIN(BYTE) (((BYTE) >> 7) & 0x01)
35 #define FRAME_GET_RSV1(BYTE) (((BYTE) >> 6) & 0x01)
36 #define FRAME_GET_RSV2(BYTE) (((BYTE) >> 5) & 0x01)
37 #define FRAME_GET_RSV3(BYTE) (((BYTE) >> 4) & 0x01)
38 #define FRAME_GET_OPCODE(BYTE) ( (BYTE) & 0x0F)
39 #define FRAME_GET_MASK(BYTE) (((BYTE) >> 7) & 0x01)
40 #define FRAME_GET_PAYLOAD_LEN(BYTE) ( (BYTE) & 0x7F)
42 #define FRAME_SET_FIN(BYTE) (((BYTE) & 0x01) << 7)
43 #define FRAME_SET_RSV1(BYTE) (((BYTE) & 0x01) << 6)
44 #define FRAME_SET_RSV2(BYTE) (((BYTE) & 0x01) << 5)
45 #define FRAME_SET_RSV3(BYTE) (((BYTE) & 0x01) << 4)
46 #define FRAME_SET_OPCODE(BYTE) ((BYTE) & 0x0F)
47 #define FRAME_SET_MASK(BYTE) (((BYTE) & 0x01) << 7)
48 #define FRAME_SET_LENGTH(X64, IDX) (unsigned char)(((X64) >> ((IDX)*8)) & 0xFF)
50 #define OPCODE_CONTINUATION 0x0
51 #define OPCODE_TEXT 0x1
52 #define OPCODE_BINARY 0x2
53 #define OPCODE_CLOSE 0x8
54 #define OPCODE_PING 0x9
55 #define OPCODE_PONG 0xA
59 #define STATE_LENGTH 2
68 unsigned char header[14]; /* 2 + 8 + 4 */
69 const struct websock_itf *itf;
73 static ssize_t ws_writev(struct websock *ws, const struct iovec *iov, int iovcnt)
75 return ws->itf->writev(ws->closure, iov, iovcnt);
78 static ssize_t ws_readv(struct websock *ws, const struct iovec *iov, int iovcnt)
80 return ws->itf->readv(ws->closure, iov, iovcnt);
84 static ssize_t ws_write(struct websock *ws, const void *buffer, size_t buffer_size)
87 iov.iov_base = (void *)buffer; /* const cast */
88 iov.iov_len = buffer_size;
89 return ws_writev(ws, &iov, 1);
93 static ssize_t ws_read(struct websock *ws, void *buffer, size_t buffer_size)
96 iov.iov_base = buffer;
97 iov.iov_len = buffer_size;
98 return ws_readv(ws, &iov, 1);
101 static int websock_send_internal_v(struct websock *ws, unsigned char first, const struct iovec *iovec, int count)
103 struct iovec iov[32];
105 size_t pos, size, len;
107 unsigned char header[32];
110 if (count < 0 || (count + 1) > (int)(sizeof iov / sizeof * iov)) {
115 /* computes the size */
118 for (j = 0 ; j < count ; j++) {
119 iov[i].iov_base = iovec[j].iov_base;
120 len = iovec[j].iov_len;
122 iov[i].iov_len = len;
128 /* makes the header */
130 header[pos++] = first;
131 size = (uint64_t) size;
133 header[pos++] = FRAME_SET_MASK(0) | FRAME_SET_LENGTH(size, 0);
136 header[pos++] = FRAME_SET_MASK(0) | 126;
138 header[pos++] = FRAME_SET_MASK(0) | 127;
139 header[pos++] = FRAME_SET_LENGTH(size, 7);
140 header[pos++] = FRAME_SET_LENGTH(size, 6);
141 header[pos++] = FRAME_SET_LENGTH(size, 5);
142 header[pos++] = FRAME_SET_LENGTH(size, 4);
143 header[pos++] = FRAME_SET_LENGTH(size, 3);
144 header[pos++] = FRAME_SET_LENGTH(size, 2);
146 header[pos++] = FRAME_SET_LENGTH(size, 1);
147 header[pos++] = FRAME_SET_LENGTH(size, 0);
150 /* allocates the vec */
151 iov[0].iov_base = header;
152 iov[0].iov_len = pos;
153 rc = ws_writev(ws, iov, i);
155 return rc < 0 ? -1 : 0;
158 static int websock_send_internal(struct websock *ws, unsigned char first, const void *buffer, size_t size)
162 iov.iov_base = (void *)buffer;
164 return websock_send_internal_v(ws, first, &iov, 1);
167 static inline int websock_send_v(struct websock *ws, int last, int rsv1, int rsv2, int rsv3, int opcode, const struct iovec *iovec, int count)
169 unsigned char first = (unsigned char)(FRAME_SET_FIN(last)
170 | FRAME_SET_RSV1(rsv1)
171 | FRAME_SET_RSV1(rsv2)
172 | FRAME_SET_RSV1(rsv3)
173 | FRAME_SET_OPCODE(opcode));
174 return websock_send_internal_v(ws, first, iovec, count);
177 static inline int websock_send(struct websock *ws, int last, int rsv1, int rsv2, int rsv3, int opcode, const void *buffer, size_t size)
179 unsigned char first = (unsigned char)(FRAME_SET_FIN(last)
180 | FRAME_SET_RSV1(rsv1)
181 | FRAME_SET_RSV1(rsv2)
182 | FRAME_SET_RSV1(rsv3)
183 | FRAME_SET_OPCODE(opcode));
184 return websock_send_internal(ws, first, buffer, size);
187 int websock_close_empty(struct websock *ws)
189 return websock_close(ws, WEBSOCKET_CODE_NOT_SET, NULL, 0);
192 int websock_close(struct websock *ws, uint16_t code, const void *data, size_t length)
194 unsigned char buffer[2];
197 if (code == WEBSOCKET_CODE_NOT_SET && length == 0)
198 return websock_send(ws, 1, 0, 0, 0, OPCODE_CLOSE, NULL, 0);
200 /* checks the length */
206 /* prepare the buffer */
207 buffer[0] = (unsigned char)((code >> 8) & 0xFF);
208 buffer[1] = (unsigned char)(code & 0xFF);
210 /* Send server-side closing handshake */
211 iov[0].iov_base = (void *)buffer;
213 iov[1].iov_base = (void *)data;
214 iov[1].iov_len = length;
215 return websock_send_v(ws, 1, 0, 0, 0, OPCODE_CLOSE, iov, 2);
218 int websock_ping(struct websock *ws, const void *data, size_t length)
220 /* checks the length */
226 return websock_send(ws, 1, 0, 0, 0, OPCODE_PING, data, length);
229 int websock_pong(struct websock *ws, const void *data, size_t length)
231 /* checks the length */
237 return websock_send(ws, 1, 0, 0, 0, OPCODE_PONG, data, length);
240 int websock_text(struct websock *ws, int last, const void *text, size_t length)
242 return websock_send(ws, last, 0, 0, 0, OPCODE_TEXT, text, length);
245 int websock_text_v(struct websock *ws, int last, const struct iovec *iovec, int count)
247 return websock_send_v(ws, last, 0, 0, 0, OPCODE_TEXT, iovec, count);
250 int websock_binary(struct websock *ws, int last, const void *data, size_t length)
252 return websock_send(ws, last, 0, 0, 0, OPCODE_BINARY, data, length);
255 int websock_binary_v(struct websock *ws, int last, const struct iovec *iovec, int count)
257 return websock_send_v(ws, last, 0, 0, 0, OPCODE_BINARY, iovec, count);
260 int websock_continue(struct websock *ws, int last, const void *data, size_t length)
262 return websock_send(ws, last, 0, 0, 0, OPCODE_CONTINUATION, data, length);
265 int websock_continue_v(struct websock *ws, int last, const struct iovec *iovec, int count)
267 return websock_send_v(ws, last, 0, 0, 0, OPCODE_CONTINUATION, iovec, count);
270 int websock_error(struct websock *ws, uint16_t code, const void *data, size_t size)
272 int rc = websock_close(ws, code, data, size);
273 if (ws->itf->on_error != NULL)
274 ws->itf->on_error(ws->closure, code, data, size);
278 static int read_header(struct websock *ws)
280 if (ws->lenhead < ws->szhead) {
282 ws_read(ws, &ws->header[ws->lenhead], (size_t)(ws->szhead - ws->lenhead));
285 ws->lenhead += (int)rbc;
290 static int check_control_header(struct websock *ws)
293 if (FRAME_GET_RSV1(ws->header[0]) != 0)
295 if (FRAME_GET_RSV2(ws->header[0]) != 0)
297 if (FRAME_GET_RSV3(ws->header[0]) != 0)
299 if (FRAME_GET_PAYLOAD_LEN(ws->header[1]) > 125)
301 if (FRAME_GET_OPCODE(ws->header[0]) == OPCODE_CLOSE)
302 return FRAME_GET_PAYLOAD_LEN(ws->header[1]) != 1;
306 int websock_dispatch(struct websock *ws)
314 ws->state = STATE_START;
317 /* read the header */
320 else if (ws->lenhead < ws->szhead)
323 switch (FRAME_GET_OPCODE(ws->header[0])) {
324 case OPCODE_CONTINUATION:
329 if (!check_control_header(ws))
331 if (FRAME_GET_PAYLOAD_LEN(ws->header[1]))
336 if (!check_control_header(ws))
341 /* update heading size */
342 switch (FRAME_GET_PAYLOAD_LEN(ws->header[1])) {
348 ws->szhead += 4 * FRAME_GET_MASK(ws->header[1]);
350 ws->state = STATE_LENGTH;
353 /* continue to read the header */
356 else if (ws->lenhead < ws->szhead)
360 switch (FRAME_GET_PAYLOAD_LEN(ws->header[1])) {
362 ws->length = (((uint64_t) ws->header[2]) << 56)
363 | (((uint64_t) ws->header[3]) << 48)
364 | (((uint64_t) ws->header[4]) << 40)
365 | (((uint64_t) ws->header[5]) << 32)
366 | (((uint64_t) ws->header[6]) << 24)
367 | (((uint64_t) ws->header[7]) << 16)
368 | (((uint64_t) ws->header[8]) << 8)
369 | (uint64_t) ws->header[9];
372 ws->length = (((uint64_t) ws->header[2]) << 8)
373 | (uint64_t) ws->header[3];
376 ws->length = FRAME_GET_PAYLOAD_LEN(ws->header[1]);
379 if (FRAME_GET_OPCODE(ws->header[0]) == OPCODE_CLOSE && ws->length != 0)
381 if (ws->length > ws->maxlength)
385 if (FRAME_GET_MASK(ws->header[1])) {
386 ((unsigned char *)&ws->mask)[0] = ws->header[ws->szhead - 4];
387 ((unsigned char *)&ws->mask)[1] = ws->header[ws->szhead - 3];
388 ((unsigned char *)&ws->mask)[2] = ws->header[ws->szhead - 2];
389 ((unsigned char *)&ws->mask)[3] = ws->header[ws->szhead - 1];
393 /* all heading fields are known, process */
394 ws->state = STATE_DATA;
395 if (ws->itf->on_extension != NULL) {
396 if (ws->itf->on_extension(ws->closure,
397 FRAME_GET_FIN(ws->header[0]),
398 FRAME_GET_RSV1(ws->header[0]),
399 FRAME_GET_RSV2(ws->header[0]),
400 FRAME_GET_RSV3(ws->header[0]),
401 FRAME_GET_OPCODE(ws->header[0]),
402 (size_t) ws->length)) {
407 /* not an extension case */
408 if (FRAME_GET_RSV1(ws->header[0]) != 0)
410 if (FRAME_GET_RSV2(ws->header[0]) != 0)
412 if (FRAME_GET_RSV3(ws->header[0]) != 0)
416 switch (FRAME_GET_OPCODE(ws->header[0])) {
417 case OPCODE_CONTINUATION:
418 ws->itf->on_continue(ws->closure,
419 FRAME_GET_FIN(ws->header[0]),
420 (size_t) ws->length);
423 ws->itf->on_text(ws->closure,
424 FRAME_GET_FIN(ws->header[0]),
425 (size_t) ws->length);
428 ws->itf->on_binary(ws->closure,
429 FRAME_GET_FIN(ws->header[0]),
430 (size_t) ws->length);
434 code = WEBSOCKET_CODE_NOT_SET;
436 code = (uint16_t)(ws->header[ws->szhead - 2] & 0xff);
437 code = (uint16_t)(code << 8);
438 code = (uint16_t)(code | (uint16_t)(ws->header[ws->szhead - 1] & 0xff));
440 ws->itf->on_close(ws->closure, code, (size_t) ws->length);
443 if (ws->itf->on_ping)
444 ws->itf->on_ping(ws->closure, ws->length);
447 websock_pong(ws, NULL, 0);
449 ws->state = STATE_INIT;
452 if (ws->itf->on_pong)
453 ws->itf->on_pong(ws->closure, ws->length);
456 ws->state = STATE_INIT;
466 ws->state = STATE_INIT;
472 websock_error(ws, WEBSOCKET_CODE_MESSAGE_TOO_LARGE, NULL, 0);
476 websock_error(ws, WEBSOCKET_CODE_PROTOCOL_ERROR, NULL, 0);
480 static void unmask(struct websock * ws, void *buffer, size_t size)
487 while (size && ((sizeof(uint32_t) - 1) & (uintptr_t) b8)) {
488 m = ((uint8_t *) & mask)[0];
489 ((uint8_t *) & mask)[0] = ((uint8_t *) & mask)[1];
490 ((uint8_t *) & mask)[1] = ((uint8_t *) & mask)[2];
491 ((uint8_t *) & mask)[2] = ((uint8_t *) & mask)[3];
492 ((uint8_t *) & mask)[3] = m;
496 b32 = (uint32_t *) b8;
497 while (size >= sizeof(uint32_t)) {
499 size -= sizeof(uint32_t);
501 b8 = (uint8_t *) b32;
503 m = ((uint8_t *) & mask)[0];
504 ((uint8_t *) & mask)[0] = ((uint8_t *) & mask)[1];
505 ((uint8_t *) & mask)[1] = ((uint8_t *) & mask)[2];
506 ((uint8_t *) & mask)[2] = ((uint8_t *) & mask)[3];
507 ((uint8_t *) & mask)[3] = m;
514 ssize_t websock_read(struct websock * ws, void *buffer, size_t size)
518 if (ws->state != STATE_DATA)
521 if (size > ws->length)
522 size = (size_t) ws->length;
524 rc = ws_read(ws, buffer, size);
530 unmask(ws, buffer, size);
535 int websock_drop(struct websock *ws)
540 if (ws_read(ws, buffer, sizeof buffer) < 0)
545 struct websock *websock_create_v13(const struct websock_itf *itf, void *closure)
547 struct websock *result = calloc(1, sizeof *result);
550 result->closure = closure;
551 result->maxlength = 65000;
556 void websock_destroy(struct websock *ws)