forked from espressif/esp-idf
Merge branch 'bugfix/tcp_transport_leak' into 'master'
BUGFIX/tcp_transport: Fix Websocket transport initialization See merge request espressif/esp-idf!16281
This commit is contained in:
@@ -1,16 +1,8 @@
|
|||||||
// Copyright 2015-2021 Espressif Systems (Shanghai) PTE LTD
|
/*
|
||||||
//
|
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
*
|
||||||
// you may not use this file except in compliance with the License.
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
// You may obtain a copy of the License at
|
*/
|
||||||
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -109,7 +101,9 @@ static char *trimwhitespace(const char *str)
|
|||||||
char *end;
|
char *end;
|
||||||
|
|
||||||
// Trim leading space
|
// Trim leading space
|
||||||
while (isspace((unsigned char)*str)) str++;
|
while (isspace((unsigned char)*str)) {
|
||||||
|
str++;
|
||||||
|
}
|
||||||
|
|
||||||
if (*str == 0) {
|
if (*str == 0) {
|
||||||
return (char *)str;
|
return (char *)str;
|
||||||
@@ -117,7 +111,9 @@ static char *trimwhitespace(const char *str)
|
|||||||
|
|
||||||
// Trim trailing space
|
// Trim trailing space
|
||||||
end = (char *)(str + strlen(str) - 1);
|
end = (char *)(str + strlen(str) - 1);
|
||||||
while (end > str && isspace((unsigned char)*end)) end--;
|
while (end > str && isspace((unsigned char)*end)) {
|
||||||
|
end--;
|
||||||
|
}
|
||||||
|
|
||||||
// Write new null terminator
|
// Write new null terminator
|
||||||
*(end + 1) = 0;
|
*(end + 1) = 0;
|
||||||
@@ -154,7 +150,7 @@ static int ws_connect(esp_transport_handle_t t, const char *host, int port, int
|
|||||||
// Size of base64 coded string is equal '((input_size * 4) / 3) + (input_size / 96) + 6' including Z-term
|
// Size of base64 coded string is equal '((input_size * 4) / 3) + (input_size / 96) + 6' including Z-term
|
||||||
unsigned char client_key[28] = {0};
|
unsigned char client_key[28] = {0};
|
||||||
|
|
||||||
const char *user_agent_ptr = (ws->user_agent)?(ws->user_agent):"ESP32 Websocket Client";
|
const char *user_agent_ptr = (ws->user_agent) ? (ws->user_agent) : "ESP32 Websocket Client";
|
||||||
|
|
||||||
size_t outlen = 0;
|
size_t outlen = 0;
|
||||||
esp_crypto_base64_encode(client_key, sizeof(client_key), &outlen, random_key, sizeof(random_key));
|
esp_crypto_base64_encode(client_key, sizeof(client_key), &outlen, random_key, sizeof(random_key));
|
||||||
@@ -229,15 +225,15 @@ static int ws_connect(esp_transport_handle_t t, const char *host, int port, int
|
|||||||
// If you are interested, see https://tools.ietf.org/html/rfc6455
|
// If you are interested, see https://tools.ietf.org/html/rfc6455
|
||||||
const char expected_server_magic[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
const char expected_server_magic[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
||||||
unsigned char expected_server_text[sizeof(client_key) + sizeof(expected_server_magic) + 1];
|
unsigned char expected_server_text[sizeof(client_key) + sizeof(expected_server_magic) + 1];
|
||||||
strcpy((char*)expected_server_text, (char*)client_key);
|
strcpy((char *)expected_server_text, (char *)client_key);
|
||||||
strcat((char*)expected_server_text, expected_server_magic);
|
strcat((char *)expected_server_text, expected_server_magic);
|
||||||
|
|
||||||
size_t key_len = strlen((char*)expected_server_text);
|
size_t key_len = strlen((char *)expected_server_text);
|
||||||
esp_crypto_sha1(expected_server_text, key_len, expected_server_sha1);
|
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));
|
esp_crypto_base64_encode(expected_server_key, sizeof(expected_server_key), &outlen, expected_server_sha1, sizeof(expected_server_sha1));
|
||||||
expected_server_key[ (outlen < sizeof(expected_server_key)) ? outlen : (sizeof(expected_server_key) - 1) ] = 0;
|
expected_server_key[ (outlen < sizeof(expected_server_key)) ? outlen : (sizeof(expected_server_key) - 1) ] = 0;
|
||||||
ESP_LOGD(TAG, "server key=%s, send_key=%s, expected_server_key=%s", (char *)server_key, (char*)client_key, expected_server_key);
|
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) {
|
if (strcmp((char *)expected_server_key, (char *)server_key) != 0) {
|
||||||
ESP_LOGE(TAG, "Invalid websocket key");
|
ESP_LOGE(TAG, "Invalid websocket key");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -300,7 +296,7 @@ static int _ws_write(esp_transport_handle_t t, int opcode, int mask_flag, const
|
|||||||
// in case of masked transport we have to revert back to the original data, as ws layer
|
// 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
|
// does not create its own copy of data to be sent
|
||||||
if (mask_flag) {
|
if (mask_flag) {
|
||||||
mask = &ws_header[header_len-4];
|
mask = &ws_header[header_len - 4];
|
||||||
for (i = 0; i < len; ++i) {
|
for (i = 0; i < len; ++i) {
|
||||||
buffer[i] = (buffer[i] ^ mask[i % 4]);
|
buffer[i] = (buffer[i] ^ mask[i % 4]);
|
||||||
}
|
}
|
||||||
@@ -598,7 +594,10 @@ esp_transport_handle_t esp_transport_ws_init(esp_transport_handle_t parent_handl
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
transport_ws_t *ws = calloc(1, sizeof(transport_ws_t));
|
transport_ws_t *ws = calloc(1, sizeof(transport_ws_t));
|
||||||
ESP_TRANSPORT_MEM_CHECK(TAG, ws, return NULL);
|
ESP_TRANSPORT_MEM_CHECK(TAG, ws, {
|
||||||
|
esp_transport_destroy(t);
|
||||||
|
return NULL;
|
||||||
|
});
|
||||||
ws->parent = parent_handle;
|
ws->parent = parent_handle;
|
||||||
|
|
||||||
ws->path = strdup("/");
|
ws->path = strdup("/");
|
||||||
@@ -771,7 +770,7 @@ static int esp_transport_ws_handle_control_frames(esp_transport_handle_t t, char
|
|||||||
|
|
||||||
if (client_closed == false) {
|
if (client_closed == false) {
|
||||||
// Only echo the closing frame if not initiated by the client
|
// Only echo the closing frame if not initiated by the client
|
||||||
if (_ws_write(t, WS_OPCODE_CLOSE | WS_FIN, WS_MASK, NULL,0, timeout_ms) < 0) {
|
if (_ws_write(t, WS_OPCODE_CLOSE | WS_FIN, WS_MASK, NULL, 0, timeout_ms) < 0) {
|
||||||
ESP_LOGE(TAG, "Sending CLOSE frame with 0 payload failed");
|
ESP_LOGE(TAG, "Sending CLOSE frame with 0 payload failed");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@@ -2160,7 +2160,6 @@ components/tcp_transport/test/test_transport_fixtures.c
|
|||||||
components/tcp_transport/transport.c
|
components/tcp_transport/transport.c
|
||||||
components/tcp_transport/transport_ssl.c
|
components/tcp_transport/transport_ssl.c
|
||||||
components/tcp_transport/transport_utils.c
|
components/tcp_transport/transport_utils.c
|
||||||
components/tcp_transport/transport_ws.c
|
|
||||||
components/tcpip_adapter/include/tcpip_adapter.h
|
components/tcpip_adapter/include/tcpip_adapter.h
|
||||||
components/tcpip_adapter/include/tcpip_adapter_compatible/tcpip_adapter_compat.h
|
components/tcpip_adapter/include/tcpip_adapter_compatible/tcpip_adapter_compat.h
|
||||||
components/tcpip_adapter/include/tcpip_adapter_types.h
|
components/tcpip_adapter/include/tcpip_adapter_types.h
|
||||||
|
Reference in New Issue
Block a user