Update copyright dates
[src/app-framework-binder.git] / src / websock.c
index 945ccd9..2632260 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 iot.bzh
+ * Copyright (C) 2015-2020 "IoT.bzh"
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -29,7 +29,9 @@
 
 #include "websock.h"
 
-#define BLOCK_DATA_SIZE              4096
+#if !defined(WEBSOCKET_DEFAULT_MAXLENGTH)
+#  define WEBSOCKET_DEFAULT_MAXLENGTH 1048500  /* 76 less than 1M, probably enougth for headers */
+#endif
 
 #define FRAME_GET_FIN(BYTE)         (((BYTE) >> 7) & 0x01)
 #define FRAME_GET_RSV1(BYTE)        (((BYTE) >> 6) & 0x01)
@@ -45,7 +47,7 @@
 #define FRAME_SET_RSV3(BYTE)        (((BYTE) & 0x01) << 4)
 #define FRAME_SET_OPCODE(BYTE)      ((BYTE) & 0x0F)
 #define FRAME_SET_MASK(BYTE)        (((BYTE) & 0x01) << 7)
-#define FRAME_SET_LENGTH(X64, IDX)  (unsigned char)(((X64) >> ((IDX)*8)) & 0xFF)
+#define FRAME_SET_LENGTH(X64, IDX)  (unsigned char)((sizeof(X64)) <= (IDX) ? 0 : (((X64) >> ((IDX)*8)) & 0xFF))
 
 #define OPCODE_CONTINUATION 0x0
 #define OPCODE_TEXT         0x1
@@ -59,6 +61,8 @@
 #define STATE_LENGTH  2
 #define STATE_DATA    3
 
+static size_t default_maxlength = WEBSOCKET_DEFAULT_MAXLENGTH;
+
 struct websock {
        int state;
        uint64_t maxlength;
@@ -80,16 +84,6 @@ static ssize_t ws_readv(struct websock *ws, const struct iovec *iov, int iovcnt)
        return ws->itf->readv(ws->closure, iov, iovcnt);
 }
 
-#if 0
-static ssize_t ws_write(struct websock *ws, const void *buffer, size_t buffer_size)
-{
-       struct iovec iov;
-       iov.iov_base = (void *)buffer;  /* const cast */
-       iov.iov_len = buffer_size;
-       return ws_writev(ws, &iov, 1);
-}
-#endif
-
 static ssize_t ws_read(struct websock *ws, void *buffer, size_t buffer_size)
 {
        struct iovec iov;
@@ -98,13 +92,34 @@ static ssize_t ws_read(struct websock *ws, void *buffer, size_t buffer_size)
        return ws_readv(ws, &iov, 1);
 }
 
-static int websock_send_internal(struct websock *ws, unsigned char first, const void *buffer, size_t size)
+static int websock_send_internal_v(struct websock *ws, unsigned char first, const struct iovec *iovec, int count)
 {
-       struct iovec iov[2];
-       size_t pos;
+       struct iovec iov[32];
+       int i, j;
+       size_t pos, size, len;
        ssize_t rc;
        unsigned char header[32];
 
+       /* checks count */
+       if (count < 0 || (count + 1) > (int)(sizeof iov / sizeof * iov)) {
+               errno = EINVAL;
+               return -1;
+       }
+
+       /* computes the size */
+       size = 0;
+       i = 1;
+       for (j = 0 ; j < count ; j++) {
+               iov[i].iov_base = iovec[j].iov_base;
+               len = iovec[j].iov_len;
+               if (len != 0) {
+                       iov[i].iov_len = len;
+                       size += len;
+                       i++;
+               }
+       }
+
+       /* makes the header */
        pos = 0;
        header[pos++] = first;
        size = (uint64_t) size;
@@ -126,16 +141,33 @@ static int websock_send_internal(struct websock *ws, unsigned char first, const
                header[pos++] = FRAME_SET_LENGTH(size, 0);
        }
 
+       /* allocates the vec */
        iov[0].iov_base = header;
        iov[0].iov_len = pos;
-       iov[1].iov_base = (void *)buffer;       /* const cast */
-       iov[1].iov_len = size;
-
-       rc = ws_writev(ws, iov, 1 + !!size);
+       rc = ws_writev(ws, iov, i);
 
        return rc < 0 ? -1 : 0;
 }
 
+static int websock_send_internal(struct websock *ws, unsigned char first, const void *buffer, size_t size)
+{
+       struct iovec iov;
+
+       iov.iov_base = (void *)buffer;
+       iov.iov_len = size;
+       return websock_send_internal_v(ws, first, &iov, 1);
+}
+
+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)
+{
+       unsigned char first = (unsigned char)(FRAME_SET_FIN(last)
+                               | FRAME_SET_RSV1(rsv1)
+                               | FRAME_SET_RSV1(rsv2)
+                               | FRAME_SET_RSV1(rsv3)
+                               | FRAME_SET_OPCODE(opcode));
+       return websock_send_internal_v(ws, first, iovec, count);
+}
+
 static inline int websock_send(struct websock *ws, int last, int rsv1, int rsv2, int rsv3, int opcode, const void *buffer, size_t size)
 {
        unsigned char first = (unsigned char)(FRAME_SET_FIN(last)
@@ -146,14 +178,18 @@ static inline int websock_send(struct websock *ws, int last, int rsv1, int rsv2,
        return websock_send_internal(ws, first, buffer, size);
 }
 
-int websock_close(struct websock *ws)
+int websock_close_empty(struct websock *ws)
 {
-       return websock_send(ws, 1, 0, 0, 0, OPCODE_CLOSE, NULL, 0);
+       return websock_close(ws, WEBSOCKET_CODE_NOT_SET, NULL, 0);
 }
 
-int websock_close_code(struct websock *ws, uint16_t code, const void *data, size_t length)
+int websock_close(struct websock *ws, uint16_t code, const void *data, size_t length)
 {
-       unsigned char buffer[125];
+       unsigned char buffer[2];
+       struct iovec iov[2];
+
+       if (code == WEBSOCKET_CODE_NOT_SET && length == 0)
+               return websock_send(ws, 1, 0, 0, 0, OPCODE_CLOSE, NULL, 0);
 
        /* checks the length */
        if (length > 123) {
@@ -164,11 +200,13 @@ int websock_close_code(struct websock *ws, uint16_t code, const void *data, size
        /* prepare the buffer */
        buffer[0] = (unsigned char)((code >> 8) & 0xFF);
        buffer[1] = (unsigned char)(code & 0xFF);
-       if (length != 0)
-               memcpy(&buffer[2], data, length);
 
        /* Send server-side closing handshake */
-       return websock_send(ws, 1, 0, 0, 0, OPCODE_CLOSE, buffer, 2 + length);
+       iov[0].iov_base = (void *)buffer;
+       iov[0].iov_len = 2;
+       iov[1].iov_base = (void *)data;
+       iov[1].iov_len = length;
+       return websock_send_v(ws, 1, 0, 0, 0, OPCODE_CLOSE, iov, 2);
 }
 
 int websock_ping(struct websock *ws, const void *data, size_t length)
@@ -193,16 +231,44 @@ int websock_pong(struct websock *ws, const void *data, size_t length)
        return websock_send(ws, 1, 0, 0, 0, OPCODE_PONG, data, length);
 }
 
-int websock_text(struct websock *ws, int last, const char *text, size_t length)
+int websock_text(struct websock *ws, int last, const void *text, size_t length)
 {
        return websock_send(ws, last, 0, 0, 0, OPCODE_TEXT, text, length);
 }
 
+int websock_text_v(struct websock *ws, int last, const struct iovec *iovec, int count)
+{
+       return websock_send_v(ws, last, 0, 0, 0, OPCODE_TEXT, iovec, count);
+}
+
 int websock_binary(struct websock *ws, int last, const void *data, size_t length)
 {
        return websock_send(ws, last, 0, 0, 0, OPCODE_BINARY, data, length);
 }
 
+int websock_binary_v(struct websock *ws, int last, const struct iovec *iovec, int count)
+{
+       return websock_send_v(ws, last, 0, 0, 0, OPCODE_BINARY, iovec, count);
+}
+
+int websock_continue(struct websock *ws, int last, const void *data, size_t length)
+{
+       return websock_send(ws, last, 0, 0, 0, OPCODE_CONTINUATION, data, length);
+}
+
+int websock_continue_v(struct websock *ws, int last, const struct iovec *iovec, int count)
+{
+       return websock_send_v(ws, last, 0, 0, 0, OPCODE_CONTINUATION, iovec, count);
+}
+
+int websock_error(struct websock *ws, uint16_t code, const void *data, size_t size)
+{
+       int rc = websock_close(ws, code, data, size);
+       if (ws->itf->on_error != NULL)
+               ws->itf->on_error(ws->closure, code, data, size);
+       return rc;
+}
+
 static int read_header(struct websock *ws)
 {
        if (ws->lenhead < ws->szhead) {
@@ -231,7 +297,7 @@ static int check_control_header(struct websock *ws)
        return 1;
 }
 
-int websock_dispatch(struct websock *ws)
+int websock_dispatch(struct websock *ws, int loop)
 {
        uint16_t code;
 loop:
@@ -240,6 +306,7 @@ loop:
                ws->lenhead = 0;
                ws->szhead = 2;
                ws->state = STATE_START;
+               /*@fallthrough@*/
 
        case STATE_START:
                /* read the header */
@@ -270,12 +337,15 @@ loop:
                switch (FRAME_GET_PAYLOAD_LEN(ws->header[1])) {
                case 127:
                        ws->szhead += 6;
+                       /*@fallthrough@*/
                case 126:
                        ws->szhead += 2;
+                       /*@fallthrough@*/
                default:
                        ws->szhead += 4 * FRAME_GET_MASK(ws->header[1]);
                }
                ws->state = STATE_LENGTH;
+               /*@fallthrough@*/
 
        case STATE_LENGTH:
                /* continue to read the header */
@@ -346,16 +416,22 @@ loop:
                        ws->itf->on_continue(ws->closure,
                                             FRAME_GET_FIN(ws->header[0]),
                                             (size_t) ws->length);
+                       if (!loop)
+                               return 0;
                        break;
                case OPCODE_TEXT:
                        ws->itf->on_text(ws->closure,
                                         FRAME_GET_FIN(ws->header[0]),
                                         (size_t) ws->length);
+                       if (!loop)
+                               return 0;
                        break;
                case OPCODE_BINARY:
                        ws->itf->on_binary(ws->closure,
                                           FRAME_GET_FIN(ws->header[0]),
                                           (size_t) ws->length);
+                       if (!loop)
+                               return 0;
                        break;
                case OPCODE_CLOSE:
                        if (ws->length == 0)
@@ -375,6 +451,8 @@ loop:
                                websock_pong(ws, NULL, 0);
                        }
                        ws->state = STATE_INIT;
+                       if (!loop)
+                               return 0;
                        break;
                case OPCODE_PONG:
                        if (ws->itf->on_pong)
@@ -382,6 +460,8 @@ loop:
                        else
                                websock_drop(ws);
                        ws->state = STATE_INIT;
+                       if (!loop)
+                               return 0;
                        break;
                default:
                        goto protocol_error;
@@ -397,18 +477,50 @@ loop:
        goto loop;
 
  too_long_error:
-       websock_close_code(ws, WEBSOCKET_CODE_MESSAGE_TOO_LARGE, NULL, 0);
+       websock_error(ws, WEBSOCKET_CODE_MESSAGE_TOO_LARGE, NULL, 0);
        return 0;
 
  protocol_error:
-       websock_close_code(ws, WEBSOCKET_CODE_PROTOCOL_ERROR, NULL, 0);
+       websock_error(ws, WEBSOCKET_CODE_PROTOCOL_ERROR, NULL, 0);
        return 0;
 }
 
-ssize_t websock_read(struct websock * ws, void *buffer, size_t size)
+static void unmask(struct websock * ws, void *buffer, size_t size)
 {
        uint32_t mask, *b32;
        uint8_t m, *b8;
+
+       mask = ws->mask;
+       b8 = buffer;
+       while (size && ((sizeof(uint32_t) - 1) & (uintptr_t) b8)) {
+               m = ((uint8_t *) & mask)[0];
+               ((uint8_t *) & mask)[0] = ((uint8_t *) & mask)[1];
+               ((uint8_t *) & mask)[1] = ((uint8_t *) & mask)[2];
+               ((uint8_t *) & mask)[2] = ((uint8_t *) & mask)[3];
+               ((uint8_t *) & mask)[3] = m;
+               *b8++ ^= m;
+               size--;
+       }
+       b32 = (uint32_t *) b8;
+       while (size >= sizeof(uint32_t)) {
+               *b32++ ^= mask;
+               size -= sizeof(uint32_t);
+       }
+       b8 = (uint8_t *) b32;
+       while (size) {
+               m = ((uint8_t *) & mask)[0];
+               ((uint8_t *) & mask)[0] = ((uint8_t *) & mask)[1];
+               ((uint8_t *) & mask)[1] = ((uint8_t *) & mask)[2];
+               ((uint8_t *) & mask)[2] = ((uint8_t *) & mask)[3];
+               ((uint8_t *) & mask)[3] = m;
+               *b8++ ^= m;
+               size--;
+       }
+       ws->mask = mask;
+}
+
+ssize_t websock_read(struct websock * ws, void *buffer, size_t size)
+{
        ssize_t rc;
 
        if (ws->state != STATE_DATA)
@@ -422,35 +534,8 @@ ssize_t websock_read(struct websock * ws, void *buffer, size_t size)
                size = (size_t) rc;
                ws->length -= size;
 
-               if (ws->mask) {
-                       mask = ws->mask;
-                       b8 = buffer;
-                       while (size && ((sizeof(uint32_t) - 1) & (uintptr_t) b8)) {
-                               m = ((uint8_t *) & mask)[0];
-                               ((uint8_t *) & mask)[0] = ((uint8_t *) & mask)[1];
-                               ((uint8_t *) & mask)[1] = ((uint8_t *) & mask)[2];
-                               ((uint8_t *) & mask)[2] = ((uint8_t *) & mask)[3];
-                               ((uint8_t *) & mask)[3] = m;
-                               *b8++ ^= m;
-                               size--;
-                       }
-                       b32 = (uint32_t *) b8;
-                       while (size >= sizeof(uint32_t)) {
-                               *b32++ ^= mask;
-                               size -= sizeof(uint32_t);
-                       }
-                       b8 = (uint8_t *) b32;
-                       while (size) {
-                               m = ((uint8_t *) & mask)[0];
-                               ((uint8_t *) & mask)[0] = ((uint8_t *) & mask)[1];
-                               ((uint8_t *) & mask)[1] = ((uint8_t *) & mask)[2];
-                               ((uint8_t *) & mask)[2] = ((uint8_t *) & mask)[3];
-                               ((uint8_t *) & mask)[3] = m;
-                               *b8++ ^= m;
-                               size--;
-                       }
-                       ws->mask = mask;
-               }
+               if (ws->mask != 0)
+                       unmask(ws, buffer, size);
        }
        return rc;
 }
@@ -460,7 +545,7 @@ int websock_drop(struct websock *ws)
        char buffer[8000];
 
        while (ws->length)
-               if (ws_read(ws, buffer, sizeof buffer) < 0)
+               if (websock_read(ws, buffer, sizeof buffer) < 0)
                        return -1;
        return 0;
 }
@@ -471,7 +556,7 @@ struct websock *websock_create_v13(const struct websock_itf *itf, void *closure)
        if (result) {
                result->itf = itf;
                result->closure = closure;
-               result->maxlength = 65000;
+               result->maxlength = default_maxlength;
        }
        return result;
 }
@@ -480,3 +565,34 @@ void websock_destroy(struct websock *ws)
 {
        free(ws);
 }
+
+void websock_set_default_max_length(size_t maxlen)
+{
+       default_maxlength = maxlen;
+}
+
+void websock_set_max_length(struct websock *ws, size_t maxlen)
+{
+       ws->maxlength = (uint64_t)maxlen;
+}
+
+const char *websocket_explain_error(uint16_t code)
+{
+       static const char *msgs[] = {
+               "OK",                /* 1000 */
+               "GOING_AWAY",        /* 1001 */
+               "PROTOCOL_ERROR",    /* 1002 */
+               "CANT_ACCEPT",       /* 1003 */
+               "RESERVED",          /* 1004 */
+               "NOT_SET",           /* 1005 */
+               "ABNORMAL",          /* 1006 */
+               "INVALID_UTF8",      /* 1007 */
+               "POLICY_VIOLATION",  /* 1008 */
+               "MESSAGE_TOO_LARGE", /* 1009 */
+               "EXPECT_EXTENSION",  /* 1010 */
+               "INTERNAL_ERROR",    /* 1011 */
+       };
+       if (code < 1000 || (code - 1000) >= (sizeof msgs / sizeof *msgs))
+               return "?";
+       return msgs[code - 1000];
+}