2021-12-06 11:15:55 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2021-04-16 05:23:18 -04:00
|
|
|
|
2018-09-25 04:34:04 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2022-11-08 03:54:17 -05:00
|
|
|
#include <unistd.h>
|
2018-09-25 04:34:04 -04:00
|
|
|
#include <ctype.h>
|
2018-09-19 07:52:11 -04:00
|
|
|
#include <sys/random.h>
|
2020-07-17 11:59:05 -04:00
|
|
|
#include <sys/socket.h>
|
2018-09-25 04:34:04 -04:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_transport.h"
|
|
|
|
#include "esp_transport_tcp.h"
|
|
|
|
#include "esp_transport_ws.h"
|
2020-07-17 11:59:05 -04:00
|
|
|
#include "esp_transport_internal.h"
|
|
|
|
#include "errno.h"
|
2020-08-04 03:57:06 -04:00
|
|
|
#include "esp_tls_crypto.h"
|
2022-11-08 03:54:17 -05:00
|
|
|
#include <arpa/inet.h>
|
2018-09-25 04:34:04 -04:00
|
|
|
|
2022-07-29 08:19:30 -04:00
|
|
|
static const char *TAG = "transport_ws";
|
2018-09-25 04:34:04 -04:00
|
|
|
|
2020-06-30 04:55:27 -04:00
|
|
|
#define WS_BUFFER_SIZE CONFIG_WS_BUFFER_SIZE
|
|
|
|
#define WS_FIN 0x80
|
|
|
|
#define WS_OPCODE_CONT 0x00
|
|
|
|
#define WS_OPCODE_TEXT 0x01
|
|
|
|
#define WS_OPCODE_BINARY 0x02
|
|
|
|
#define WS_OPCODE_CLOSE 0x08
|
|
|
|
#define WS_OPCODE_PING 0x09
|
|
|
|
#define WS_OPCODE_PONG 0x0a
|
2021-01-11 12:15:13 -05:00
|
|
|
#define WS_OPCODE_CONTROL_FRAME 0x08
|
2019-11-14 05:09:38 -05:00
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
// Second byte
|
2020-06-30 04:55:27 -04:00
|
|
|
#define WS_MASK 0x80
|
|
|
|
#define WS_SIZE16 126
|
|
|
|
#define WS_SIZE64 127
|
|
|
|
#define MAX_WEBSOCKET_HEADER_SIZE 16
|
|
|
|
#define WS_RESPONSE_OK 101
|
2021-01-11 12:15:13 -05:00
|
|
|
#define WS_TRANSPORT_MAX_CONTROL_FRAME_BUFFER_LEN 125
|
2018-09-25 04:34:04 -04:00
|
|
|
|
2019-11-14 05:09:38 -05:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint8_t opcode;
|
2022-03-15 15:46:37 -04:00
|
|
|
bool fin; /*!< Frame fin flag, for continuations */
|
2019-11-14 05:09:38 -05:00
|
|
|
char mask_key[4]; /*!< Mask key for this payload */
|
|
|
|
int payload_len; /*!< Total length of the payload */
|
|
|
|
int bytes_remaining; /*!< Bytes left to read of the payload */
|
2021-01-11 12:15:13 -05:00
|
|
|
bool header_received; /*!< Flag to indicate that a new message header was received */
|
2019-11-14 05:09:38 -05:00
|
|
|
} ws_transport_frame_state_t;
|
|
|
|
|
2018-09-25 04:34:04 -04:00
|
|
|
typedef struct {
|
|
|
|
char *path;
|
|
|
|
char *buffer;
|
2019-06-20 03:37:40 -04:00
|
|
|
char *sub_protocol;
|
2019-11-15 06:15:55 -05:00
|
|
|
char *user_agent;
|
|
|
|
char *headers;
|
2023-02-02 02:08:28 -05:00
|
|
|
char *auth;
|
2021-01-11 12:15:13 -05:00
|
|
|
bool propagate_control_frames;
|
2019-11-14 05:09:38 -05:00
|
|
|
ws_transport_frame_state_t frame_state;
|
2018-09-26 05:56:47 -04:00
|
|
|
esp_transport_handle_t parent;
|
2018-09-25 04:34:04 -04:00
|
|
|
} transport_ws_t;
|
|
|
|
|
2021-01-11 12:15:13 -05:00
|
|
|
/**
|
|
|
|
* @brief Handles control frames
|
|
|
|
*
|
|
|
|
* This API is used internally to handle control frames at the transport layer.
|
|
|
|
* The API could be possibly promoted to a public API if needed by some clients
|
|
|
|
*
|
|
|
|
* @param t Websocket transport handle
|
|
|
|
* @param buffer Buffer with the actual payload of the control packet to be processed
|
|
|
|
* @param len Length of the buffer (typically the same as the payload buffer)
|
|
|
|
* @param timeout_ms The timeout milliseconds
|
|
|
|
* @param client_closed To indicate that the connection has been closed by the client
|
|
|
|
* (to prevent echoing the CLOSE packet if true, as this is the actual echo from the server)
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* 0 - no activity, or successfully responded to PING
|
|
|
|
* -1 - Failure: Error on read or the actual payload longer then buffer
|
|
|
|
* 1 - Close handshake success
|
|
|
|
* 2 - Got PONG message
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int esp_transport_ws_handle_control_frames(esp_transport_handle_t t, char *buffer, int len, int timeout_ms, bool client_closed);
|
|
|
|
|
2019-09-16 09:22:29 -04:00
|
|
|
static inline uint8_t ws_get_bin_opcode(ws_transport_opcodes_t opcode)
|
|
|
|
{
|
|
|
|
return (uint8_t)opcode;
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
static esp_transport_handle_t ws_get_payload_transport_handle(esp_transport_handle_t t)
|
2018-09-25 04:34:04 -04:00
|
|
|
{
|
2018-09-26 05:56:47 -04:00
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
2019-11-14 05:09:38 -05:00
|
|
|
|
|
|
|
/* Reading parts of a frame directly will disrupt the WS internal frame state,
|
|
|
|
reset bytes_remaining to prepare for reading a new frame */
|
|
|
|
ws->frame_state.bytes_remaining = 0;
|
|
|
|
|
2018-09-25 04:34:04 -04:00
|
|
|
return ws->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *trimwhitespace(const char *str)
|
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
// Trim leading space
|
2021-12-06 11:15:55 -05:00
|
|
|
while (isspace((unsigned char)*str)) {
|
|
|
|
str++;
|
|
|
|
}
|
2018-09-25 04:34:04 -04:00
|
|
|
|
|
|
|
if (*str == 0) {
|
|
|
|
return (char *)str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trim trailing space
|
|
|
|
end = (char *)(str + strlen(str) - 1);
|
2021-12-06 11:15:55 -05:00
|
|
|
while (end > str && isspace((unsigned char)*end)) {
|
|
|
|
end--;
|
|
|
|
}
|
2018-09-25 04:34:04 -04:00
|
|
|
|
|
|
|
// Write new null terminator
|
|
|
|
*(end + 1) = 0;
|
|
|
|
|
|
|
|
return (char *)str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *get_http_header(const char *buffer, const char *key)
|
|
|
|
{
|
2019-08-15 11:51:54 -04:00
|
|
|
char *found = strcasestr(buffer, key);
|
2018-09-25 04:34:04 -04:00
|
|
|
if (found) {
|
|
|
|
found += strlen(key);
|
|
|
|
char *found_end = strstr(found, "\r\n");
|
|
|
|
if (found_end) {
|
|
|
|
found_end[0] = 0;//terminal string
|
|
|
|
|
|
|
|
return trimwhitespace(found);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
static int ws_connect(esp_transport_handle_t t, const char *host, int port, int timeout_ms)
|
2018-09-25 04:34:04 -04:00
|
|
|
{
|
2018-09-26 05:56:47 -04:00
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
|
|
|
if (esp_transport_connect(ws->parent, host, port, timeout_ms) < 0) {
|
2019-06-20 03:37:40 -04:00
|
|
|
ESP_LOGE(TAG, "Error connecting to host %s:%d", host, port);
|
2019-03-12 05:44:27 -04:00
|
|
|
return -1;
|
2018-09-25 04:34:04 -04:00
|
|
|
}
|
2018-09-18 02:02:14 -04:00
|
|
|
|
2018-09-19 07:52:11 -04:00
|
|
|
unsigned char random_key[16];
|
2022-11-08 03:54:17 -05:00
|
|
|
ssize_t rc;
|
|
|
|
if ((rc = getrandom(random_key, sizeof(random_key), 0)) < 0) {
|
|
|
|
ESP_LOGD(TAG, "getrandom() returned %zd", rc);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-09-19 07:52:11 -04:00
|
|
|
|
2018-09-18 02:02:14 -04:00
|
|
|
// Size of base64 coded string is equal '((input_size * 4) / 3) + (input_size / 96) + 6' including Z-term
|
|
|
|
unsigned char client_key[28] = {0};
|
2018-09-19 07:52:11 -04:00
|
|
|
|
2021-12-06 11:15:55 -05:00
|
|
|
const char *user_agent_ptr = (ws->user_agent) ? (ws->user_agent) : "ESP32 Websocket Client";
|
2022-05-16 03:27:32 -04:00
|
|
|
#ifdef CONFIG_WS_DYNAMIC_BUFFER
|
|
|
|
if (!ws->buffer) {
|
|
|
|
ws->buffer = malloc(WS_BUFFER_SIZE);
|
|
|
|
if (!ws->buffer) {
|
|
|
|
ESP_LOGE(TAG, "Cannot allocate buffer for connect, need-%d", WS_BUFFER_SIZE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2019-11-15 06:15:55 -05:00
|
|
|
|
2018-09-25 04:34:04 -04:00
|
|
|
size_t outlen = 0;
|
2020-08-04 03:57:06 -04:00
|
|
|
esp_crypto_base64_encode(client_key, sizeof(client_key), &outlen, random_key, sizeof(random_key));
|
2020-06-30 04:55:27 -04:00
|
|
|
int len = snprintf(ws->buffer, WS_BUFFER_SIZE,
|
2021-12-06 11:15:55 -05:00
|
|
|
"GET %s HTTP/1.1\r\n"
|
|
|
|
"Connection: Upgrade\r\n"
|
|
|
|
"Host: %s:%d\r\n"
|
|
|
|
"User-Agent: %s\r\n"
|
|
|
|
"Upgrade: websocket\r\n"
|
|
|
|
"Sec-WebSocket-Version: 13\r\n"
|
|
|
|
"Sec-WebSocket-Key: %s\r\n",
|
|
|
|
ws->path,
|
|
|
|
host, port, user_agent_ptr,
|
|
|
|
client_key);
|
2020-06-30 04:55:27 -04:00
|
|
|
if (len <= 0 || len >= WS_BUFFER_SIZE) {
|
|
|
|
ESP_LOGE(TAG, "Error in request generation, desired request len: %d, buffer size: %d", len, WS_BUFFER_SIZE);
|
2018-09-18 02:02:14 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2019-08-23 10:36:42 -04:00
|
|
|
if (ws->sub_protocol) {
|
2019-11-15 06:15:55 -05:00
|
|
|
ESP_LOGD(TAG, "sub_protocol: %s", ws->sub_protocol);
|
2020-06-30 04:55:27 -04:00
|
|
|
int r = snprintf(ws->buffer + len, WS_BUFFER_SIZE - len, "Sec-WebSocket-Protocol: %s\r\n", ws->sub_protocol);
|
2019-08-23 10:36:42 -04:00
|
|
|
len += r;
|
2020-06-30 04:55:27 -04:00
|
|
|
if (r <= 0 || len >= WS_BUFFER_SIZE) {
|
2019-08-23 10:36:42 -04:00
|
|
|
ESP_LOGE(TAG, "Error in request generation"
|
2021-12-06 11:15:55 -05:00
|
|
|
"(snprintf of subprotocol returned %d, desired request len: %d, buffer size: %d", r, len, WS_BUFFER_SIZE);
|
2019-08-23 10:36:42 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2023-02-02 02:08:28 -05:00
|
|
|
if (ws->auth) {
|
|
|
|
ESP_LOGD(TAG, "Authorization: %s", ws->auth);
|
|
|
|
int r = snprintf(ws->buffer + len, WS_BUFFER_SIZE - len, "Authorization: %s\r\n", ws->auth);
|
|
|
|
len += r;
|
|
|
|
if (r <= 0 || len >= WS_BUFFER_SIZE) {
|
|
|
|
ESP_LOGE(TAG, "Error in request generation"
|
|
|
|
"(snprintf of authorization returned %d, desired request len: %d, buffer size: %d", r, len, WS_BUFFER_SIZE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2019-11-15 06:15:55 -05:00
|
|
|
if (ws->headers) {
|
|
|
|
ESP_LOGD(TAG, "headers: %s", ws->headers);
|
2020-06-30 04:55:27 -04:00
|
|
|
int r = snprintf(ws->buffer + len, WS_BUFFER_SIZE - len, "%s", ws->headers);
|
2019-11-15 06:15:55 -05:00
|
|
|
len += r;
|
2020-06-30 04:55:27 -04:00
|
|
|
if (r <= 0 || len >= WS_BUFFER_SIZE) {
|
2019-11-15 06:15:55 -05:00
|
|
|
ESP_LOGE(TAG, "Error in request generation"
|
2021-12-06 11:15:55 -05:00
|
|
|
"(strncpy of headers returned %d, desired request len: %d, buffer size: %d", r, len, WS_BUFFER_SIZE);
|
2019-11-15 06:15:55 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2020-06-30 04:55:27 -04:00
|
|
|
int r = snprintf(ws->buffer + len, WS_BUFFER_SIZE - len, "\r\n");
|
2019-08-23 10:36:42 -04:00
|
|
|
len += r;
|
2020-06-30 04:55:27 -04:00
|
|
|
if (r <= 0 || len >= WS_BUFFER_SIZE) {
|
2019-08-23 10:36:42 -04:00
|
|
|
ESP_LOGE(TAG, "Error in request generation"
|
2021-12-06 11:15:55 -05:00
|
|
|
"(snprintf of header terminal returned %d, desired request len: %d, buffer size: %d", r, len, WS_BUFFER_SIZE);
|
2019-08-23 10:36:42 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2020-06-30 04:55:27 -04:00
|
|
|
ESP_LOGD(TAG, "Write upgrade request\r\n%s", ws->buffer);
|
2018-09-26 05:56:47 -04:00
|
|
|
if (esp_transport_write(ws->parent, ws->buffer, len, timeout_ms) <= 0) {
|
2018-09-25 04:34:04 -04:00
|
|
|
ESP_LOGE(TAG, "Error write Upgrade header %s", ws->buffer);
|
|
|
|
return -1;
|
|
|
|
}
|
2019-05-14 09:36:46 -04:00
|
|
|
int header_len = 0;
|
|
|
|
do {
|
2020-06-30 04:55:27 -04:00
|
|
|
if ((len = esp_transport_read(ws->parent, ws->buffer + header_len, WS_BUFFER_SIZE - header_len, timeout_ms)) <= 0) {
|
2019-05-14 09:36:46 -04:00
|
|
|
ESP_LOGE(TAG, "Error read response for Upgrade header %s", ws->buffer);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
header_len += len;
|
|
|
|
ws->buffer[header_len] = '\0';
|
|
|
|
ESP_LOGD(TAG, "Read header chunk %d, current header size: %d", len, header_len);
|
2020-06-30 04:55:27 -04:00
|
|
|
} while (NULL == strstr(ws->buffer, "\r\n\r\n") && header_len < WS_BUFFER_SIZE);
|
2019-05-14 09:36:46 -04:00
|
|
|
|
2018-09-25 04:34:04 -04:00
|
|
|
char *server_key = get_http_header(ws->buffer, "Sec-WebSocket-Accept:");
|
|
|
|
if (server_key == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Sec-WebSocket-Accept not found");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-08-04 03:57:06 -04:00
|
|
|
// See esp_crypto_sha1() arg size
|
2018-09-18 02:02:14 -04:00
|
|
|
unsigned char expected_server_sha1[20];
|
|
|
|
// Size of base64 coded string see above
|
|
|
|
unsigned char expected_server_key[33] = {0};
|
|
|
|
// If you are interested, see https://tools.ietf.org/html/rfc6455
|
|
|
|
const char expected_server_magic[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
|
|
|
unsigned char expected_server_text[sizeof(client_key) + sizeof(expected_server_magic) + 1];
|
2021-12-06 11:15:55 -05:00
|
|
|
strcpy((char *)expected_server_text, (char *)client_key);
|
|
|
|
strcat((char *)expected_server_text, expected_server_magic);
|
2018-09-18 02:02:14 -04:00
|
|
|
|
2021-12-06 11:15:55 -05:00
|
|
|
size_t key_len = strlen((char *)expected_server_text);
|
2020-08-04 03:57:06 -04:00
|
|
|
esp_crypto_sha1(expected_server_text, key_len, expected_server_sha1);
|
|
|
|
esp_crypto_base64_encode(expected_server_key, sizeof(expected_server_key), &outlen, expected_server_sha1, sizeof(expected_server_sha1));
|
2018-09-18 02:02:14 -04:00
|
|
|
expected_server_key[ (outlen < sizeof(expected_server_key)) ? outlen : (sizeof(expected_server_key) - 1) ] = 0;
|
2021-12-06 11:15:55 -05:00
|
|
|
ESP_LOGD(TAG, "server key=%s, send_key=%s, expected_server_key=%s", (char *)server_key, (char *)client_key, expected_server_key);
|
|
|
|
if (strcmp((char *)expected_server_key, (char *)server_key) != 0) {
|
2018-09-25 04:34:04 -04:00
|
|
|
ESP_LOGE(TAG, "Invalid websocket key");
|
|
|
|
return -1;
|
|
|
|
}
|
2022-05-16 03:27:32 -04:00
|
|
|
#ifdef CONFIG_WS_DYNAMIC_BUFFER
|
|
|
|
free(ws->buffer);
|
|
|
|
ws->buffer = NULL;
|
|
|
|
#endif
|
2018-09-25 04:34:04 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-20 03:37:40 -04:00
|
|
|
static int _ws_write(esp_transport_handle_t t, int opcode, int mask_flag, const char *b, int len, int timeout_ms)
|
2018-09-25 04:34:04 -04:00
|
|
|
{
|
2018-09-26 05:56:47 -04:00
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
2019-06-20 03:37:40 -04:00
|
|
|
char *buffer = (char *)b;
|
2018-09-25 04:34:04 -04:00
|
|
|
char ws_header[MAX_WEBSOCKET_HEADER_SIZE];
|
|
|
|
char *mask;
|
|
|
|
int header_len = 0, i;
|
2019-06-20 03:37:40 -04:00
|
|
|
|
2018-09-25 04:34:04 -04:00
|
|
|
int poll_write;
|
2018-09-26 05:56:47 -04:00
|
|
|
if ((poll_write = esp_transport_poll_write(ws->parent, timeout_ms)) <= 0) {
|
2019-06-20 03:37:40 -04:00
|
|
|
ESP_LOGE(TAG, "Error transport_poll_write");
|
2018-09-25 04:34:04 -04:00
|
|
|
return poll_write;
|
|
|
|
}
|
2019-06-20 03:37:40 -04:00
|
|
|
ws_header[header_len++] = opcode;
|
2018-09-25 04:34:04 -04:00
|
|
|
|
2019-06-20 03:37:40 -04:00
|
|
|
if (len <= 125) {
|
|
|
|
ws_header[header_len++] = (uint8_t)(len | mask_flag);
|
|
|
|
} else if (len < 65536) {
|
|
|
|
ws_header[header_len++] = WS_SIZE16 | mask_flag;
|
2018-09-25 04:34:04 -04:00
|
|
|
ws_header[header_len++] = (uint8_t)(len >> 8);
|
|
|
|
ws_header[header_len++] = (uint8_t)(len & 0xFF);
|
|
|
|
} else {
|
2019-06-20 03:37:40 -04:00
|
|
|
ws_header[header_len++] = WS_SIZE64 | mask_flag;
|
|
|
|
/* Support maximum 4 bytes length */
|
|
|
|
ws_header[header_len++] = 0; //(uint8_t)((len >> 56) & 0xFF);
|
|
|
|
ws_header[header_len++] = 0; //(uint8_t)((len >> 48) & 0xFF);
|
|
|
|
ws_header[header_len++] = 0; //(uint8_t)((len >> 40) & 0xFF);
|
|
|
|
ws_header[header_len++] = 0; //(uint8_t)((len >> 32) & 0xFF);
|
|
|
|
ws_header[header_len++] = (uint8_t)((len >> 24) & 0xFF);
|
|
|
|
ws_header[header_len++] = (uint8_t)((len >> 16) & 0xFF);
|
|
|
|
ws_header[header_len++] = (uint8_t)((len >> 8) & 0xFF);
|
|
|
|
ws_header[header_len++] = (uint8_t)((len >> 0) & 0xFF);
|
2018-09-25 04:34:04 -04:00
|
|
|
}
|
|
|
|
|
2019-08-22 11:39:20 -04:00
|
|
|
if (mask_flag) {
|
|
|
|
mask = &ws_header[header_len];
|
2022-11-08 03:54:17 -05:00
|
|
|
ssize_t rc;
|
|
|
|
if ((rc = getrandom(ws_header + header_len, 4, 0)) < 0) {
|
|
|
|
ESP_LOGD(TAG, "getrandom() returned %zd", rc);
|
|
|
|
return -1;
|
|
|
|
}
|
2019-08-22 11:39:20 -04:00
|
|
|
header_len += 4;
|
|
|
|
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
buffer[i] = (buffer[i] ^ mask[i % 4]);
|
|
|
|
}
|
2018-09-25 04:34:04 -04:00
|
|
|
}
|
2019-08-22 11:39:20 -04:00
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
if (esp_transport_write(ws->parent, ws_header, header_len, timeout_ms) != header_len) {
|
2018-09-25 04:34:04 -04:00
|
|
|
ESP_LOGE(TAG, "Error write header");
|
|
|
|
return -1;
|
|
|
|
}
|
2019-06-20 03:37:40 -04:00
|
|
|
if (len == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-07-16 03:47:18 -04:00
|
|
|
|
|
|
|
int ret = esp_transport_write(ws->parent, buffer, len, timeout_ms);
|
|
|
|
// in case of masked transport we have to revert back to the original data, as ws layer
|
|
|
|
// does not create its own copy of data to be sent
|
|
|
|
if (mask_flag) {
|
2021-12-06 11:15:55 -05:00
|
|
|
mask = &ws_header[header_len - 4];
|
2019-07-16 03:47:18 -04:00
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
buffer[i] = (buffer[i] ^ mask[i % 4]);
|
|
|
|
}
|
2019-11-15 06:15:55 -05:00
|
|
|
}
|
2019-07-16 03:47:18 -04:00
|
|
|
return ret;
|
2018-09-25 04:34:04 -04:00
|
|
|
}
|
|
|
|
|
2019-09-16 09:22:29 -04:00
|
|
|
int esp_transport_ws_send_raw(esp_transport_handle_t t, ws_transport_opcodes_t opcode, const char *b, int len, int timeout_ms)
|
|
|
|
{
|
|
|
|
uint8_t op_code = ws_get_bin_opcode(opcode);
|
|
|
|
if (t == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Transport must be a valid ws handle");
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
ESP_LOGD(TAG, "Sending raw ws message with opcode %d", op_code);
|
2020-03-25 16:22:25 -04:00
|
|
|
return _ws_write(t, op_code, WS_MASK, b, len, timeout_ms);
|
2019-09-16 09:22:29 -04:00
|
|
|
}
|
|
|
|
|
2019-06-20 03:37:40 -04:00
|
|
|
static int ws_write(esp_transport_handle_t t, const char *b, int len, int timeout_ms)
|
|
|
|
{
|
|
|
|
if (len == 0) {
|
2019-09-16 09:22:29 -04:00
|
|
|
// Default transport write of zero length in ws layer sends out a ping message.
|
|
|
|
// This behaviour could however be altered in IDF 5.0, since a separate API for sending
|
|
|
|
// messages with user defined opcodes has been introduced.
|
2019-06-20 03:37:40 -04:00
|
|
|
ESP_LOGD(TAG, "Write PING message");
|
2019-08-22 11:39:20 -04:00
|
|
|
return _ws_write(t, WS_OPCODE_PING | WS_FIN, WS_MASK, NULL, 0, timeout_ms);
|
2019-06-20 03:37:40 -04:00
|
|
|
}
|
|
|
|
return _ws_write(t, WS_OPCODE_BINARY | WS_FIN, WS_MASK, b, len, timeout_ms);
|
|
|
|
}
|
|
|
|
|
2019-11-14 05:09:38 -05:00
|
|
|
|
|
|
|
static int ws_read_payload(esp_transport_handle_t t, char *buffer, int len, int timeout_ms)
|
|
|
|
{
|
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
|
|
|
|
|
|
|
int bytes_to_read;
|
|
|
|
int rlen = 0;
|
|
|
|
|
|
|
|
if (ws->frame_state.bytes_remaining > len) {
|
|
|
|
ESP_LOGD(TAG, "Actual data to receive (%d) are longer than ws buffer (%d)", ws->frame_state.bytes_remaining, len);
|
|
|
|
bytes_to_read = len;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
bytes_to_read = ws->frame_state.bytes_remaining;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receive and process payload
|
|
|
|
if (bytes_to_read != 0 && (rlen = esp_transport_read(ws->parent, buffer, bytes_to_read, timeout_ms)) <= 0) {
|
|
|
|
ESP_LOGE(TAG, "Error read data");
|
|
|
|
return rlen;
|
|
|
|
}
|
|
|
|
ws->frame_state.bytes_remaining -= rlen;
|
|
|
|
|
2022-09-27 05:00:37 -04:00
|
|
|
for (int i = 0; i < bytes_to_read; i++) {
|
|
|
|
buffer[i] = (buffer[i] ^ ws->frame_state.mask_key[i % 4]);
|
2019-11-14 05:09:38 -05:00
|
|
|
}
|
|
|
|
return rlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Read and parse the WS header, determine length of payload */
|
|
|
|
static int ws_read_header(esp_transport_handle_t t, char *buffer, int len, int timeout_ms)
|
2018-09-25 04:34:04 -04:00
|
|
|
{
|
2018-09-26 05:56:47 -04:00
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
2018-09-25 04:34:04 -04:00
|
|
|
int payload_len;
|
2019-11-14 05:09:38 -05:00
|
|
|
|
2019-04-30 05:41:39 -04:00
|
|
|
char ws_header[MAX_WEBSOCKET_HEADER_SIZE];
|
2019-11-14 05:09:38 -05:00
|
|
|
char *data_ptr = ws_header, mask;
|
2018-09-25 04:34:04 -04:00
|
|
|
int rlen;
|
|
|
|
int poll_read;
|
2021-01-11 12:15:13 -05:00
|
|
|
ws->frame_state.header_received = false;
|
2018-09-26 05:56:47 -04:00
|
|
|
if ((poll_read = esp_transport_poll_read(ws->parent, timeout_ms)) <= 0) {
|
2018-09-25 04:34:04 -04:00
|
|
|
return poll_read;
|
|
|
|
}
|
2018-11-19 21:31:19 -05:00
|
|
|
|
|
|
|
// Receive and process header first (based on header size)
|
|
|
|
int header = 2;
|
2019-11-14 05:09:38 -05:00
|
|
|
int mask_len = 4;
|
2019-04-30 05:41:39 -04:00
|
|
|
if ((rlen = esp_transport_read(ws->parent, data_ptr, header, timeout_ms)) <= 0) {
|
2018-09-25 04:34:04 -04:00
|
|
|
ESP_LOGE(TAG, "Error read data");
|
|
|
|
return rlen;
|
|
|
|
}
|
2021-01-11 12:15:13 -05:00
|
|
|
ws->frame_state.header_received = true;
|
2022-03-15 15:46:37 -04:00
|
|
|
ws->frame_state.fin = (*data_ptr & 0x80) != 0;
|
2019-11-14 05:09:38 -05:00
|
|
|
ws->frame_state.opcode = (*data_ptr & 0x0F);
|
2018-09-25 04:34:04 -04:00
|
|
|
data_ptr ++;
|
|
|
|
mask = ((*data_ptr >> 7) & 0x01);
|
|
|
|
payload_len = (*data_ptr & 0x7F);
|
|
|
|
data_ptr++;
|
2019-11-14 05:09:38 -05:00
|
|
|
ESP_LOGD(TAG, "Opcode: %d, mask: %d, len: %d\r\n", ws->frame_state.opcode, mask, payload_len);
|
2018-09-25 04:34:04 -04:00
|
|
|
if (payload_len == 126) {
|
|
|
|
// headerLen += 2;
|
2018-11-19 21:31:19 -05:00
|
|
|
if ((rlen = esp_transport_read(ws->parent, data_ptr, header, timeout_ms)) <= 0) {
|
|
|
|
ESP_LOGE(TAG, "Error read data");
|
|
|
|
return rlen;
|
|
|
|
}
|
2018-09-25 04:34:04 -04:00
|
|
|
payload_len = data_ptr[0] << 8 | data_ptr[1];
|
|
|
|
} else if (payload_len == 127) {
|
|
|
|
// headerLen += 8;
|
2018-11-19 21:31:19 -05:00
|
|
|
header = 8;
|
|
|
|
if ((rlen = esp_transport_read(ws->parent, data_ptr, header, timeout_ms)) <= 0) {
|
|
|
|
ESP_LOGE(TAG, "Error read data");
|
|
|
|
return rlen;
|
|
|
|
}
|
2018-09-25 04:34:04 -04:00
|
|
|
|
|
|
|
if (data_ptr[0] != 0 || data_ptr[1] != 0 || data_ptr[2] != 0 || data_ptr[3] != 0) {
|
|
|
|
// really too big!
|
|
|
|
payload_len = 0xFFFFFFFF;
|
|
|
|
} else {
|
|
|
|
payload_len = data_ptr[4] << 24 | data_ptr[5] << 16 | data_ptr[6] << 8 | data_ptr[7];
|
|
|
|
}
|
|
|
|
}
|
2019-02-02 12:35:22 -05:00
|
|
|
|
2019-11-14 05:09:38 -05:00
|
|
|
if (mask) {
|
|
|
|
// Read and store mask
|
|
|
|
if (payload_len != 0 && (rlen = esp_transport_read(ws->parent, buffer, mask_len, timeout_ms)) <= 0) {
|
|
|
|
ESP_LOGE(TAG, "Error read data");
|
|
|
|
return rlen;
|
|
|
|
}
|
|
|
|
memcpy(ws->frame_state.mask_key, buffer, mask_len);
|
|
|
|
} else {
|
|
|
|
memset(ws->frame_state.mask_key, 0, mask_len);
|
2019-02-02 12:35:22 -05:00
|
|
|
}
|
|
|
|
|
2019-11-14 05:09:38 -05:00
|
|
|
ws->frame_state.payload_len = payload_len;
|
|
|
|
ws->frame_state.bytes_remaining = payload_len;
|
2018-09-25 04:34:04 -04:00
|
|
|
|
2019-11-14 05:09:38 -05:00
|
|
|
return payload_len;
|
|
|
|
}
|
|
|
|
|
2021-01-11 12:15:13 -05:00
|
|
|
static int ws_handle_control_frame_internal(esp_transport_handle_t t, int timeout_ms)
|
|
|
|
{
|
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
|
|
|
char *control_frame_buffer = NULL;
|
|
|
|
int control_frame_buffer_len = 0;
|
|
|
|
int payload_len = ws->frame_state.payload_len;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
// If no new header reception in progress, or not a control frame
|
|
|
|
// just pass 0 -> no need to handle control frames
|
|
|
|
if (ws->frame_state.header_received == false ||
|
2021-12-06 11:15:55 -05:00
|
|
|
!(ws->frame_state.opcode & WS_OPCODE_CONTROL_FRAME)) {
|
2021-01-11 12:15:13 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (payload_len > WS_TRANSPORT_MAX_CONTROL_FRAME_BUFFER_LEN) {
|
|
|
|
ESP_LOGE(TAG, "Not enough room for reading control frames (need=%d, max_allowed=%d)",
|
|
|
|
ws->frame_state.payload_len, WS_TRANSPORT_MAX_CONTROL_FRAME_BUFFER_LEN);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we can handle the control frame correctly (either zero payload, or a short one for which we allocate mem)
|
|
|
|
control_frame_buffer_len = payload_len;
|
|
|
|
if (control_frame_buffer_len > 0) {
|
|
|
|
control_frame_buffer = malloc(control_frame_buffer_len);
|
|
|
|
if (control_frame_buffer == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Cannot allocate buffer for control frames, need-%d", control_frame_buffer_len);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
control_frame_buffer_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read the payload of the control frame
|
|
|
|
int actual_len = ws_read_payload(t, control_frame_buffer, control_frame_buffer_len, timeout_ms);
|
|
|
|
if (actual_len != payload_len) {
|
|
|
|
ESP_LOGE(TAG, "Control frame (opcode=%d) payload read failed (payload_len=%d, read_len=%d)",
|
|
|
|
ws->frame_state.opcode, payload_len, actual_len);
|
|
|
|
ret = -1;
|
|
|
|
goto free_payload_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = esp_transport_ws_handle_control_frames(t, control_frame_buffer, control_frame_buffer_len, timeout_ms, false);
|
|
|
|
|
|
|
|
free_payload_buffer:
|
|
|
|
free(control_frame_buffer);
|
|
|
|
return ret > 0 ? 0 : ret; // We don't propagate control frames, pass 0 to upper layers
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-14 05:09:38 -05:00
|
|
|
static int ws_read(esp_transport_handle_t t, char *buffer, int len, int timeout_ms)
|
|
|
|
{
|
|
|
|
int rlen = 0;
|
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
|
|
|
|
|
|
|
// If message exceeds buffer len then subsequent reads will skip reading header and read whatever is left of the payload
|
|
|
|
if (ws->frame_state.bytes_remaining <= 0) {
|
2021-01-11 12:15:13 -05:00
|
|
|
|
|
|
|
if ( (rlen = ws_read_header(t, buffer, len, timeout_ms)) < 0) {
|
2019-11-14 05:09:38 -05:00
|
|
|
// If something when wrong then we prepare for reading a new header
|
|
|
|
ws->frame_state.bytes_remaining = 0;
|
|
|
|
return rlen;
|
2018-09-25 04:34:04 -04:00
|
|
|
}
|
2021-01-11 12:15:13 -05:00
|
|
|
|
|
|
|
// If the new opcode is a control frame and we don't pass it to the app
|
|
|
|
// - try to handle it internally using the application buffer
|
|
|
|
if (ws->frame_state.header_received && (ws->frame_state.opcode & WS_OPCODE_CONTROL_FRAME) &&
|
2021-12-06 11:15:55 -05:00
|
|
|
ws->propagate_control_frames == false) {
|
2021-01-11 12:15:13 -05:00
|
|
|
// automatically handle only 0 payload frames and make the transport read to return 0 on success
|
|
|
|
// which might be interpreted as timeouts
|
|
|
|
return ws_handle_control_frame_internal(t, timeout_ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rlen == 0) {
|
|
|
|
ws->frame_state.bytes_remaining = 0;
|
|
|
|
return 0; // timeout
|
|
|
|
}
|
2018-09-25 04:34:04 -04:00
|
|
|
}
|
2021-01-11 12:15:13 -05:00
|
|
|
|
2019-11-14 05:09:38 -05:00
|
|
|
if (ws->frame_state.payload_len) {
|
|
|
|
if ( (rlen = ws_read_payload(t, buffer, len, timeout_ms)) <= 0) {
|
|
|
|
ESP_LOGE(TAG, "Error reading payload data");
|
|
|
|
ws->frame_state.bytes_remaining = 0;
|
|
|
|
return rlen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rlen;
|
2018-09-25 04:34:04 -04:00
|
|
|
}
|
|
|
|
|
2019-11-14 05:09:38 -05:00
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
static int ws_poll_read(esp_transport_handle_t t, int timeout_ms)
|
2018-09-25 04:34:04 -04:00
|
|
|
{
|
2018-09-26 05:56:47 -04:00
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
|
|
|
return esp_transport_poll_read(ws->parent, timeout_ms);
|
2018-09-25 04:34:04 -04:00
|
|
|
}
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
static int ws_poll_write(esp_transport_handle_t t, int timeout_ms)
|
2018-09-25 04:34:04 -04:00
|
|
|
{
|
2018-09-26 05:56:47 -04:00
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
|
|
|
return esp_transport_poll_write(ws->parent, timeout_ms);;
|
2018-09-25 04:34:04 -04:00
|
|
|
}
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
static int ws_close(esp_transport_handle_t t)
|
2018-09-25 04:34:04 -04:00
|
|
|
{
|
2018-09-26 05:56:47 -04:00
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
|
|
|
return esp_transport_close(ws->parent);
|
2018-09-25 04:34:04 -04:00
|
|
|
}
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
static esp_err_t ws_destroy(esp_transport_handle_t t)
|
2018-09-25 04:34:04 -04:00
|
|
|
{
|
2018-09-26 05:56:47 -04:00
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
2018-09-25 04:34:04 -04:00
|
|
|
free(ws->buffer);
|
|
|
|
free(ws->path);
|
2019-06-20 03:37:40 -04:00
|
|
|
free(ws->sub_protocol);
|
2019-11-15 06:15:55 -05:00
|
|
|
free(ws->user_agent);
|
|
|
|
free(ws->headers);
|
2023-02-02 02:08:28 -05:00
|
|
|
free(ws->auth);
|
2018-09-25 04:34:04 -04:00
|
|
|
free(ws);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-11 12:15:13 -05:00
|
|
|
static esp_err_t internal_esp_transport_ws_set_path(esp_transport_handle_t t, const char *path)
|
2018-09-25 04:34:04 -04:00
|
|
|
{
|
2021-01-11 12:15:13 -05:00
|
|
|
if (t == NULL) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
2018-09-26 05:56:47 -04:00
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
2021-01-11 12:15:13 -05:00
|
|
|
if (ws->path) {
|
|
|
|
free(ws->path);
|
|
|
|
}
|
|
|
|
if (path == NULL) {
|
|
|
|
ws->path = NULL;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
ws->path = strdup(path);
|
|
|
|
if (ws->path == NULL) {
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_transport_ws_set_path(esp_transport_handle_t t, const char *path)
|
|
|
|
{
|
|
|
|
esp_err_t err = internal_esp_transport_ws_set_path(t, path);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "esp_transport_ws_set_path has internally failed with err=%d", err);
|
|
|
|
}
|
2018-09-25 04:34:04 -04:00
|
|
|
}
|
2019-06-20 03:37:40 -04:00
|
|
|
|
2020-07-17 11:59:05 -04:00
|
|
|
static int ws_get_socket(esp_transport_handle_t t)
|
|
|
|
{
|
|
|
|
if (t) {
|
|
|
|
transport_ws_t *ws = t->data;
|
|
|
|
if (ws && ws->parent && ws->parent->_get_socket) {
|
|
|
|
return ws->parent->_get_socket(ws->parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
esp_transport_handle_t esp_transport_ws_init(esp_transport_handle_t parent_handle)
|
2018-09-25 04:34:04 -04:00
|
|
|
{
|
2022-04-21 11:41:58 -04:00
|
|
|
if (parent_handle == NULL || parent_handle->foundation == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Invalid parent ptotocol");
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-09-26 05:56:47 -04:00
|
|
|
esp_transport_handle_t t = esp_transport_init();
|
2021-05-03 03:46:28 -04:00
|
|
|
if (t == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-09-25 04:34:04 -04:00
|
|
|
transport_ws_t *ws = calloc(1, sizeof(transport_ws_t));
|
2021-12-06 11:15:55 -05:00
|
|
|
ESP_TRANSPORT_MEM_CHECK(TAG, ws, {
|
|
|
|
esp_transport_destroy(t);
|
|
|
|
return NULL;
|
|
|
|
});
|
2018-09-25 04:34:04 -04:00
|
|
|
ws->parent = parent_handle;
|
2022-04-21 11:41:58 -04:00
|
|
|
t->foundation = parent_handle->foundation;
|
|
|
|
|
2018-09-25 04:34:04 -04:00
|
|
|
|
|
|
|
ws->path = strdup("/");
|
2019-05-31 09:37:51 -04:00
|
|
|
ESP_TRANSPORT_MEM_CHECK(TAG, ws->path, {
|
|
|
|
free(ws);
|
2021-01-20 06:33:49 -05:00
|
|
|
esp_transport_destroy(t);
|
2019-05-31 09:37:51 -04:00
|
|
|
return NULL;
|
|
|
|
});
|
2020-06-30 04:55:27 -04:00
|
|
|
ws->buffer = malloc(WS_BUFFER_SIZE);
|
2018-09-26 05:56:47 -04:00
|
|
|
ESP_TRANSPORT_MEM_CHECK(TAG, ws->buffer, {
|
2018-09-25 04:34:04 -04:00
|
|
|
free(ws->path);
|
|
|
|
free(ws);
|
2021-01-20 06:33:49 -05:00
|
|
|
esp_transport_destroy(t);
|
2018-09-25 04:34:04 -04:00
|
|
|
return NULL;
|
|
|
|
});
|
|
|
|
|
2018-10-02 09:19:46 -04:00
|
|
|
esp_transport_set_func(t, ws_connect, ws_read, ws_write, ws_close, ws_poll_read, ws_poll_write, ws_destroy);
|
2021-05-19 06:36:36 -04:00
|
|
|
// websocket underlying transfer is the payload transfer handle
|
2018-10-02 09:19:46 -04:00
|
|
|
esp_transport_set_parent_transport_func(t, ws_get_payload_transport_handle);
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
esp_transport_set_context_data(t, ws);
|
2020-07-17 11:59:05 -04:00
|
|
|
t->_get_socket = ws_get_socket;
|
2018-09-25 04:34:04 -04:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2019-06-20 03:37:40 -04:00
|
|
|
esp_err_t esp_transport_ws_set_subprotocol(esp_transport_handle_t t, const char *sub_protocol)
|
|
|
|
{
|
|
|
|
if (t == NULL) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
|
|
|
if (ws->sub_protocol) {
|
|
|
|
free(ws->sub_protocol);
|
|
|
|
}
|
|
|
|
if (sub_protocol == NULL) {
|
|
|
|
ws->sub_protocol = NULL;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
ws->sub_protocol = strdup(sub_protocol);
|
|
|
|
if (ws->sub_protocol == NULL) {
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
2019-09-16 09:22:29 -04:00
|
|
|
|
2019-11-15 06:15:55 -05:00
|
|
|
esp_err_t esp_transport_ws_set_user_agent(esp_transport_handle_t t, const char *user_agent)
|
|
|
|
{
|
|
|
|
if (t == NULL) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
|
|
|
if (ws->user_agent) {
|
|
|
|
free(ws->user_agent);
|
|
|
|
}
|
|
|
|
if (user_agent == NULL) {
|
|
|
|
ws->user_agent = NULL;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
ws->user_agent = strdup(user_agent);
|
|
|
|
if (ws->user_agent == NULL) {
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_transport_ws_set_headers(esp_transport_handle_t t, const char *headers)
|
|
|
|
{
|
|
|
|
if (t == NULL) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
|
|
|
if (ws->headers) {
|
|
|
|
free(ws->headers);
|
|
|
|
}
|
|
|
|
if (headers == NULL) {
|
|
|
|
ws->headers = NULL;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
ws->headers = strdup(headers);
|
|
|
|
if (ws->headers == NULL) {
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2023-02-02 02:08:28 -05:00
|
|
|
esp_err_t esp_transport_ws_set_auth(esp_transport_handle_t t, const char *auth)
|
|
|
|
{
|
|
|
|
if (t == NULL) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
|
|
|
if (ws->auth) {
|
|
|
|
free(ws->auth);
|
|
|
|
}
|
|
|
|
if (auth == NULL) {
|
|
|
|
ws->auth = NULL;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
ws->auth = strdup(auth);
|
|
|
|
if (ws->auth == NULL) {
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2021-01-11 12:15:13 -05:00
|
|
|
esp_err_t esp_transport_ws_set_config(esp_transport_handle_t t, const esp_transport_ws_config_t *config)
|
|
|
|
{
|
|
|
|
if (t == NULL) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
esp_err_t err = ESP_OK;
|
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
|
|
|
if (config->ws_path) {
|
|
|
|
err = internal_esp_transport_ws_set_path(t, config->ws_path);
|
|
|
|
ESP_TRANSPORT_ERR_OK_CHECK(TAG, err, return err;)
|
|
|
|
}
|
|
|
|
if (config->sub_protocol) {
|
|
|
|
err = esp_transport_ws_set_subprotocol(t, config->sub_protocol);
|
|
|
|
ESP_TRANSPORT_ERR_OK_CHECK(TAG, err, return err;)
|
|
|
|
}
|
|
|
|
if (config->user_agent) {
|
|
|
|
err = esp_transport_ws_set_user_agent(t, config->user_agent);
|
|
|
|
ESP_TRANSPORT_ERR_OK_CHECK(TAG, err, return err;)
|
|
|
|
}
|
|
|
|
if (config->headers) {
|
|
|
|
err = esp_transport_ws_set_headers(t, config->headers);
|
|
|
|
ESP_TRANSPORT_ERR_OK_CHECK(TAG, err, return err;)
|
|
|
|
}
|
2023-02-02 02:08:28 -05:00
|
|
|
if (config->auth) {
|
|
|
|
err = esp_transport_ws_set_auth(t, config->auth);
|
|
|
|
ESP_TRANSPORT_ERR_OK_CHECK(TAG, err, return err;)
|
|
|
|
}
|
2021-01-11 12:15:13 -05:00
|
|
|
ws->propagate_control_frames = config->propagate_control_frames;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-03-15 15:46:37 -04:00
|
|
|
bool esp_transport_ws_get_fin_flag(esp_transport_handle_t t)
|
|
|
|
{
|
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
|
|
|
return ws->frame_state.fin;
|
|
|
|
}
|
|
|
|
|
2019-09-16 09:22:29 -04:00
|
|
|
ws_transport_opcodes_t esp_transport_ws_get_read_opcode(esp_transport_handle_t t)
|
|
|
|
{
|
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
2021-01-11 12:15:13 -05:00
|
|
|
if (ws->frame_state.header_received) {
|
|
|
|
// convert the header byte to enum if correctly received
|
|
|
|
return (ws_transport_opcodes_t)ws->frame_state.opcode;
|
|
|
|
}
|
|
|
|
return WS_TRANSPORT_OPCODES_NONE;
|
2019-09-16 09:22:29 -04:00
|
|
|
}
|
2019-11-14 05:09:38 -05:00
|
|
|
|
|
|
|
int esp_transport_ws_get_read_payload_len(esp_transport_handle_t t)
|
|
|
|
{
|
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
|
|
|
return ws->frame_state.payload_len;
|
|
|
|
}
|
|
|
|
|
2021-01-11 12:15:13 -05:00
|
|
|
static int esp_transport_ws_handle_control_frames(esp_transport_handle_t t, char *buffer, int len, int timeout_ms, bool client_closed)
|
|
|
|
{
|
|
|
|
transport_ws_t *ws = esp_transport_get_context_data(t);
|
|
|
|
|
|
|
|
// If no new header reception in progress, or not a control frame
|
|
|
|
// just pass 0 -> no need to handle control frames
|
|
|
|
if (ws->frame_state.header_received == false ||
|
2021-12-06 11:15:55 -05:00
|
|
|
!(ws->frame_state.opcode & WS_OPCODE_CONTROL_FRAME)) {
|
2021-01-11 12:15:13 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int actual_len;
|
|
|
|
int payload_len = ws->frame_state.payload_len;
|
|
|
|
|
|
|
|
ESP_LOGD(TAG, "Handling control frame with %d bytes payload", payload_len);
|
|
|
|
if (payload_len > len) {
|
|
|
|
ESP_LOGE(TAG, "Not enough room for processing the payload (need=%d, available=%d)", payload_len, len);
|
|
|
|
ws->frame_state.bytes_remaining = payload_len - len;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ws->frame_state.opcode == WS_OPCODE_PING) {
|
|
|
|
// handle PING frames internally: just send a PONG with the same payload
|
|
|
|
actual_len = _ws_write(t, WS_OPCODE_PONG | WS_FIN, WS_MASK, buffer,
|
|
|
|
payload_len, timeout_ms);
|
|
|
|
if (actual_len != payload_len) {
|
|
|
|
ESP_LOGE(TAG, "PONG send failed (payload_len=%d, written_len=%d)", payload_len, actual_len);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ESP_LOGD(TAG, "PONG sent correctly (payload_len=%d)", payload_len);
|
|
|
|
|
|
|
|
// control frame handled correctly, reset the flag indicating new header received
|
|
|
|
ws->frame_state.header_received = false;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
} else if (ws->frame_state.opcode == WS_OPCODE_CLOSE) {
|
|
|
|
// handle CLOSE by the server: send a zero payload frame
|
|
|
|
if (buffer && payload_len > 0) { // if some payload, print out the status code
|
|
|
|
uint16_t *code_network_order = (uint16_t *) buffer;
|
|
|
|
ESP_LOGI(TAG, "Got CLOSE frame with status code=%u", ntohs(*code_network_order));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client_closed == false) {
|
|
|
|
// Only echo the closing frame if not initiated by the client
|
2021-12-06 11:15:55 -05:00
|
|
|
if (_ws_write(t, WS_OPCODE_CLOSE | WS_FIN, WS_MASK, NULL, 0, timeout_ms) < 0) {
|
2021-01-11 12:15:13 -05:00
|
|
|
ESP_LOGE(TAG, "Sending CLOSE frame with 0 payload failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ESP_LOGD(TAG, "CLOSE frame with no payload sent correctly");
|
|
|
|
}
|
|
|
|
|
|
|
|
// control frame handled correctly, reset the flag indicating new header received
|
|
|
|
ws->frame_state.header_received = false;
|
|
|
|
int ret = esp_transport_ws_poll_connection_closed(t, timeout_ms);
|
|
|
|
if (ret == 0) {
|
|
|
|
ESP_LOGW(TAG, "Connection cannot be terminated gracefully within timeout=%d", timeout_ms);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ret < 0) {
|
|
|
|
ESP_LOGW(TAG, "Connection terminated while waiting for clean TCP close");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Connection terminated gracefully");
|
|
|
|
return 1;
|
|
|
|
} else if (ws->frame_state.opcode == WS_OPCODE_PONG) {
|
|
|
|
// handle PONG: just indicate return code
|
|
|
|
ESP_LOGD(TAG, "Received PONG frame with payload=%d", payload_len);
|
|
|
|
// control frame handled correctly, reset the flag indicating new header received
|
|
|
|
ws->frame_state.header_received = false;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-17 11:59:05 -04:00
|
|
|
int esp_transport_ws_poll_connection_closed(esp_transport_handle_t t, int timeout_ms)
|
|
|
|
{
|
|
|
|
struct timeval timeout;
|
|
|
|
int sock = esp_transport_get_socket(t);
|
|
|
|
fd_set readset;
|
|
|
|
fd_set errset;
|
|
|
|
FD_ZERO(&readset);
|
|
|
|
FD_ZERO(&errset);
|
|
|
|
FD_SET(sock, &readset);
|
|
|
|
FD_SET(sock, &errset);
|
|
|
|
|
|
|
|
int ret = select(sock + 1, &readset, NULL, &errset, esp_transport_utils_ms_to_timeval(timeout_ms, &timeout));
|
|
|
|
if (ret > 0) {
|
|
|
|
if (FD_ISSET(sock, &readset)) {
|
|
|
|
uint8_t buffer;
|
|
|
|
if (recv(sock, &buffer, 1, MSG_PEEK) <= 0) {
|
|
|
|
// socket is readable, but reads zero bytes -- connection cleanly closed by FIN flag
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ESP_LOGW(TAG, "esp_transport_ws_poll_connection_closed: unexpected data readable on socket=%d", sock);
|
|
|
|
} else if (FD_ISSET(sock, &errset)) {
|
|
|
|
int sock_errno = 0;
|
|
|
|
uint32_t optlen = sizeof(sock_errno);
|
|
|
|
getsockopt(sock, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen);
|
|
|
|
ESP_LOGD(TAG, "esp_transport_ws_poll_connection_closed select error %d, errno = %s, fd = %d", sock_errno, strerror(sock_errno), sock);
|
|
|
|
if (sock_errno == ENOTCONN || sock_errno == ECONNRESET || sock_errno == ECONNABORTED) {
|
|
|
|
// the three err codes above might be caused by connection termination by RTS flag
|
|
|
|
// which we still assume as expected closing sequence of ws-transport connection
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ESP_LOGE(TAG, "esp_transport_ws_poll_connection_closed: unexpected errno=%d on socket=%d", sock_errno, sock);
|
|
|
|
}
|
|
|
|
return -1; // indicates error: socket unexpectedly reads an actual data, or unexpected errno code
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|