From 5778a7c72635941c69e9bba053e83248bbe90217 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Thu, 21 Apr 2022 12:41:58 -0300 Subject: [PATCH] [tcp_transport] Fix initialition of transport - Foundation was initialized only for SSL. - Removed base from list. - Changed SSL and TCP initialition. - Clean of state data structures. --- components/tcp_transport/CMakeLists.txt | 2 +- .../tcp_transport/include/esp_transport.h | 2 +- .../private_include/esp_transport_internal.h | 79 ++++++++++--- .../private_include/esp_transport_utils.h | 61 ---------- components/tcp_transport/transport.c | 100 +++++------------ components/tcp_transport/transport_internal.c | 37 ++++++ components/tcp_transport/transport_ssl.c | 106 ++++++++++-------- components/tcp_transport/transport_utils.c | 31 ----- components/tcp_transport/transport_ws.c | 7 +- tools/ci/check_copyright_ignore.txt | 1 - 10 files changed, 191 insertions(+), 235 deletions(-) delete mode 100644 components/tcp_transport/private_include/esp_transport_utils.h create mode 100644 components/tcp_transport/transport_internal.c delete mode 100644 components/tcp_transport/transport_utils.c diff --git a/components/tcp_transport/CMakeLists.txt b/components/tcp_transport/CMakeLists.txt index 7eb449365e..65631fb461 100644 --- a/components/tcp_transport/CMakeLists.txt +++ b/components/tcp_transport/CMakeLists.txt @@ -1,7 +1,7 @@ set(srcs "transport.c" "transport_ssl.c" - "transport_utils.c") + "transport_internal.c") if(CONFIG_WS_TRANSPORT) list(APPEND srcs diff --git a/components/tcp_transport/include/esp_transport.h b/components/tcp_transport/include/esp_transport.h index bc1490df06..f1f95ef6b8 100644 --- a/components/tcp_transport/include/esp_transport.h +++ b/components/tcp_transport/include/esp_transport.h @@ -24,7 +24,7 @@ typedef struct esp_transport_keepalive { int keep_alive_count; /*!< Keep-alive packet retry send count */ } esp_transport_keep_alive_t; -typedef struct esp_transport_internal* esp_transport_list_handle_t; +typedef struct esp_transport_list_t* esp_transport_list_handle_t; typedef struct esp_transport_item_t* esp_transport_handle_t; typedef int (*connect_func)(esp_transport_handle_t t, const char *host, int port, int timeout_ms); diff --git a/components/tcp_transport/private_include/esp_transport_internal.h b/components/tcp_transport/private_include/esp_transport_internal.h index 64a52fd38f..9e87d706b1 100644 --- a/components/tcp_transport/private_include/esp_transport_internal.h +++ b/components/tcp_transport/private_include/esp_transport_internal.h @@ -7,9 +7,13 @@ #ifndef _ESP_TRANSPORT_INTERNAL_H_ #define _ESP_TRANSPORT_INTERNAL_H_ +#include "sys/time.h" #include "esp_transport.h" #include "sys/socket.h" #include "sys/queue.h" +#include "esp_log.h" +#include "esp_tls.h" + #ifdef __cplusplus extern "C" { @@ -17,9 +21,18 @@ extern "C" { typedef int (*get_socket_func)(esp_transport_handle_t t); +/** + * Transport layer error structure including + * * esp-tls last error storage + * * sock-errno + */ +typedef struct esp_transport_error_storage { + struct esp_tls_last_error esp_tls_err_h_base; /*!< esp-tls last error container */ + // additional fields + int sock_errno; /*!< last socket error captured for this transport */ +} esp_transport_error_storage; typedef struct esp_foundation_transport { - struct esp_transport_error_storage *error_handle; /*!< Pointer to the transport error container */ - struct transport_esp_tls *transport_esp_tls; /*!< Pointer to the base transport which uses esp-tls */ + esp_transport_error_storage *error_handle; /*!< Pointer to the transport error container */ } esp_foundation_transport_t; /** @@ -40,11 +53,57 @@ struct esp_transport_item_t { payload_transfer_func _parent_transfer; /*!< Function returning underlying transport layer */ get_socket_func _get_socket; /*!< Function returning the transport's socket */ esp_transport_keep_alive_t *keep_alive_cfg; /*!< TCP keep-alive config */ - struct esp_foundation_transport *base; /*!< Foundation transport pointer available from each transport */ + struct esp_foundation_transport *foundation; /*!< Foundation transport pointer available from each transport */ STAILQ_ENTRY(esp_transport_item_t) next; }; +/** + * @brief Utility macro to be used for NULL ptr check after malloc + * + */ +#define ESP_TRANSPORT_MEM_CHECK(TAG, a, action) if (!(a)) { \ + ESP_LOGE(TAG,"%s(%d): %s", __FUNCTION__, __LINE__, "Memory exhausted"); \ + action; \ + } + +/** + * @brief Utility macro for checking the error code of esp_err_t + */ +#define ESP_TRANSPORT_ERR_OK_CHECK(TAG, err, action) \ + { \ + esp_err_t _esp_transport_err_to_check = err; \ + if (_esp_transport_err_to_check != ESP_OK) { \ + ESP_LOGE(TAG,"%s(%d): Expected ESP_OK; reported: %d", __FUNCTION__, __LINE__, _esp_transport_err_to_check); \ + action; \ + } \ + } + + +/** + * @brief Convert milliseconds to timeval struct for valid timeouts, otherwise + * (if "wait forever" requested by timeout_ms=-1) timeval structure is not updated and NULL returned + * + * @param[in] timeout_ms The timeout value in milliseconds or -1 to waiting forever + * @param[out] tv Pointer to timeval struct + * + * @return + * - NULL if timeout_ms=-1 (wait forever) + * - pointer to the updated timeval structure (provided as "tv" argument) with recalculated timeout value + */ +struct timeval* esp_transport_utils_ms_to_timeval(int timeout_ms, struct timeval *tv); + +/** + * @brief Initialize foundation struct + * + * @return + * esp_foundation_transport_t + * NULL in case of errors + */ +esp_foundation_transport_t * esp_transport_init_foundation_transport(void); + +void esp_transport_destroy_foundation_transport(esp_foundation_transport_t *foundation); + /** * @brief Captures internal tcp connection error * @@ -76,20 +135,6 @@ int esp_transport_get_socket(esp_transport_handle_t t); */ void esp_transport_capture_errno(esp_transport_handle_t t, int sock_errno); -/** - * @brief Creates esp-tls transport used in the foundation transport - * - * @return transport esp-tls handle - */ -struct transport_esp_tls* esp_transport_esp_tls_create(void); - -/** - * @brief Destroys esp-tls transport used in the foundation transport - * - * @param[in] transport esp-tls handle - */ -void esp_transport_esp_tls_destroy(struct transport_esp_tls* transport_esp_tls); - /** * @brief Sets error to common transport handle * diff --git a/components/tcp_transport/private_include/esp_transport_utils.h b/components/tcp_transport/private_include/esp_transport_utils.h deleted file mode 100644 index 2c86dbe454..0000000000 --- a/components/tcp_transport/private_include/esp_transport_utils.h +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// 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. - -#ifndef _ESP_TRANSPORT_UTILS_H_ -#define _ESP_TRANSPORT_UTILS_H_ -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @brief Utility macro to be used for NULL ptr check after malloc - * - */ -#define ESP_TRANSPORT_MEM_CHECK(TAG, a, action) if (!(a)) { \ - ESP_LOGE(TAG,"%s(%d): %s", __FUNCTION__, __LINE__, "Memory exhausted"); \ - action; \ - } - -/** - * @brief Utility macro for checking the error code of esp_err_t - */ -#define ESP_TRANSPORT_ERR_OK_CHECK(TAG, err, action) \ - { \ - esp_err_t _esp_transport_err_to_check = err; \ - if (_esp_transport_err_to_check != ESP_OK) { \ - ESP_LOGE(TAG,"%s(%d): Expected ESP_OK; reported: %d", __FUNCTION__, __LINE__, _esp_transport_err_to_check); \ - action; \ - } \ - } - -/** - * @brief Convert milliseconds to timeval struct for valid timeouts, otherwise - * (if "wait forever" requested by timeout_ms=-1) timeval structure is not updated and NULL returned - * - * @param[in] timeout_ms The timeout value in milliseconds or -1 to waiting forever - * @param[out] tv Pointer to timeval struct - * - * @return - * - NULL if timeout_ms=-1 (wait forever) - * - pointer to the updated timeval structure (provided as "tv" argument) with recalculated timeout value - */ -struct timeval* esp_transport_utils_ms_to_timeval(int timeout_ms, struct timeval *tv); - - -#ifdef __cplusplus -} -#endif -#endif /* _ESP_TRANSPORT_UTILS_H_ */ diff --git a/components/tcp_transport/transport.c b/components/tcp_transport/transport.c index 4fa4fa9c36..b7d504e71e 100644 --- a/components/tcp_transport/transport.c +++ b/components/tcp_transport/transport.c @@ -12,23 +12,11 @@ #include "sys/queue.h" #include "esp_log.h" -#include "esp_transport.h" #include "esp_transport_internal.h" -#include "esp_transport_utils.h" +#include "esp_transport.h" static const char *TAG = "TRANSPORT"; -/** - * Transport layer error structure including - * * esp-tls last error storage - * * sock-errno - */ -struct esp_transport_error_storage { - struct esp_tls_last_error esp_tls_err_h_base; /*!< esp-tls last error container */ - // additional fields - int sock_errno; /*!< last socket error captured for this transport */ -}; - /** * This list will hold all transport available */ @@ -36,37 +24,6 @@ STAILQ_HEAD(esp_transport_list_t, esp_transport_item_t); struct transport_esp_tls; -/** - * Internal transport structure holding list of transports and other data common to all transports - */ -typedef struct esp_transport_internal { - struct esp_transport_list_t list; /*!< List of transports */ - struct esp_foundation_transport *base; /*!< Base transport pointer shared for each list item */ -} esp_transport_internal_t; - -static esp_foundation_transport_t * esp_transport_init_foundation_transport(void) -{ - esp_foundation_transport_t *foundation = calloc(1, sizeof(esp_foundation_transport_t)); - ESP_TRANSPORT_MEM_CHECK(TAG, foundation, return NULL); - foundation->error_handle = calloc(1, sizeof(struct esp_transport_error_storage)); - ESP_TRANSPORT_MEM_CHECK(TAG, foundation->error_handle, - free(foundation); - return NULL); - foundation->transport_esp_tls = esp_transport_esp_tls_create(); - ESP_TRANSPORT_MEM_CHECK(TAG, foundation->transport_esp_tls, - free(foundation->error_handle); - free(foundation); - return NULL); - return foundation; -} - -static void esp_transport_destroy_foundation_transport(esp_foundation_transport_t *foundation) -{ - esp_transport_esp_tls_destroy(foundation->transport_esp_tls); - free(foundation->error_handle); - free(foundation); -} - static esp_transport_handle_t esp_transport_get_default_parent(esp_transport_handle_t t) { /* @@ -77,14 +34,10 @@ static esp_transport_handle_t esp_transport_get_default_parent(esp_transport_han esp_transport_list_handle_t esp_transport_list_init(void) { - esp_transport_list_handle_t transport = calloc(1, sizeof(esp_transport_internal_t)); - ESP_TRANSPORT_MEM_CHECK(TAG, transport, return NULL); - STAILQ_INIT(&transport->list); - transport->base = esp_transport_init_foundation_transport(); - ESP_TRANSPORT_MEM_CHECK(TAG, transport->base, - free(transport); - return NULL); - return transport; + esp_transport_list_handle_t transport_list = calloc(1, sizeof(struct esp_transport_list_t)); + ESP_TRANSPORT_MEM_CHECK(TAG, transport_list, return NULL); + STAILQ_INIT(transport_list); + return transport_list; } esp_err_t esp_transport_list_add(esp_transport_list_handle_t h, esp_transport_handle_t t, const char *scheme) @@ -95,9 +48,7 @@ esp_err_t esp_transport_list_add(esp_transport_list_handle_t h, esp_transport_ha t->scheme = calloc(1, strlen(scheme) + 1); ESP_TRANSPORT_MEM_CHECK(TAG, t->scheme, return ESP_ERR_NO_MEM); strcpy(t->scheme, scheme); - STAILQ_INSERT_TAIL(&h->list, t, next); - // Each transport in a list to share the same error tracker - t->base = h->base; + STAILQ_INSERT_TAIL(h, t, next); return ESP_OK; } @@ -107,10 +58,10 @@ esp_transport_handle_t esp_transport_list_get_transport(esp_transport_list_handl return NULL; } if (scheme == NULL) { - return STAILQ_FIRST(&h->list); + return STAILQ_FIRST(h); } esp_transport_handle_t item; - STAILQ_FOREACH(item, &h->list, next) { + STAILQ_FOREACH(item, h, next) { if (strcasecmp(item->scheme, scheme) == 0) { return item; } @@ -121,29 +72,28 @@ esp_transport_handle_t esp_transport_list_get_transport(esp_transport_list_handl esp_err_t esp_transport_list_destroy(esp_transport_list_handle_t h) { esp_transport_list_clean(h); - esp_transport_destroy_foundation_transport(h->base); free(h); return ESP_OK; } esp_err_t esp_transport_list_clean(esp_transport_list_handle_t h) { - esp_transport_handle_t item = STAILQ_FIRST(&h->list); + esp_transport_handle_t item = STAILQ_FIRST(h); esp_transport_handle_t tmp; while (item != NULL) { tmp = STAILQ_NEXT(item, next); esp_transport_destroy(item); item = tmp; } - STAILQ_INIT(&h->list); + STAILQ_INIT(h); return ESP_OK; } esp_transport_handle_t esp_transport_init(void) { - esp_transport_handle_t t = calloc(1, sizeof(struct esp_transport_item_t)); - ESP_TRANSPORT_MEM_CHECK(TAG, t, return NULL); - return t; + esp_transport_handle_t transport = calloc(1, sizeof(struct esp_transport_item_t)); + ESP_TRANSPORT_MEM_CHECK(TAG, transport, return NULL); + return transport; } esp_transport_handle_t esp_transport_get_payload_transport_handle(esp_transport_handle_t t) @@ -156,10 +106,10 @@ esp_transport_handle_t esp_transport_get_payload_transport_handle(esp_transport_ esp_err_t esp_transport_destroy(esp_transport_handle_t t) { - if (t->_destroy) { + if (t && t->_destroy) { t->_destroy(t); } - if (t->scheme) { + if (t && t->scheme) { free(t->scheme); } free(t); @@ -302,17 +252,17 @@ esp_err_t esp_transport_set_parent_transport_func(esp_transport_handle_t t, payl esp_tls_error_handle_t esp_transport_get_error_handle(esp_transport_handle_t t) { - if (t && t->base && t->base->error_handle) { - return &t->base->error_handle->esp_tls_err_h_base; + if (t && t->foundation && t->foundation->error_handle) { + return &t->foundation->error_handle->esp_tls_err_h_base; } return NULL; } int esp_transport_get_errno(esp_transport_handle_t t) { - if (t && t->base && t->base->error_handle) { - int actual_errno = t->base->error_handle->sock_errno; - t->base->error_handle->sock_errno = 0; + if (t && t->foundation && t->foundation->error_handle) { + int actual_errno = t->foundation->error_handle->sock_errno; + t->foundation->error_handle->sock_errno = 0; return actual_errno; } return -1; @@ -339,19 +289,19 @@ void capture_tcp_transport_error(esp_transport_handle_t t, enum esp_tcp_transpor void esp_transport_set_errors(esp_transport_handle_t t, const esp_tls_error_handle_t error_handle) { - if (t && t->base && t->base->error_handle) { - memcpy(&t->base->error_handle->esp_tls_err_h_base, error_handle, sizeof(esp_tls_last_error_t)); + if (t && t->foundation && t->foundation->error_handle) { + memcpy(&t->foundation->error_handle->esp_tls_err_h_base, error_handle, sizeof(esp_tls_last_error_t)); int sock_error; if (esp_tls_get_and_clear_error_type(error_handle, ESP_TLS_ERR_TYPE_SYSTEM, &sock_error) == ESP_OK) { - t->base->error_handle->sock_errno = sock_error; + t->foundation->error_handle->sock_errno = sock_error; } } } void esp_transport_capture_errno(esp_transport_handle_t t, int sock_errno) { - if (t && t->base && t->base->error_handle) { - t->base->error_handle->sock_errno = sock_errno; + if (t && t->foundation && t->foundation->error_handle) { + t->foundation->error_handle->sock_errno = sock_errno; } } diff --git a/components/tcp_transport/transport_internal.c b/components/tcp_transport/transport_internal.c new file mode 100644 index 0000000000..33354686e7 --- /dev/null +++ b/components/tcp_transport/transport_internal.c @@ -0,0 +1,37 @@ +/* + * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "esp_transport_internal.h" + +static const char *TAG = "TRANSPORT"; +struct timeval* esp_transport_utils_ms_to_timeval(int timeout_ms, struct timeval *tv) +{ + if (timeout_ms == -1) { + return NULL; + } + tv->tv_sec = timeout_ms / 1000; + tv->tv_usec = (timeout_ms - (tv->tv_sec * 1000)) * 1000; + return tv; +} + +esp_foundation_transport_t * esp_transport_init_foundation_transport(void) +{ + esp_foundation_transport_t *foundation = calloc(1, sizeof(esp_foundation_transport_t)); + ESP_TRANSPORT_MEM_CHECK(TAG, foundation, return NULL); + foundation->error_handle = calloc(1, sizeof(struct esp_transport_error_storage)); + ESP_TRANSPORT_MEM_CHECK(TAG, foundation->error_handle, + free(foundation); + return NULL); + return foundation; +} + +void esp_transport_destroy_foundation_transport(esp_foundation_transport_t *foundation) +{ + if (foundation) { + free(foundation->error_handle); + } + free(foundation); +} diff --git a/components/tcp_transport/transport_ssl.c b/components/tcp_transport/transport_ssl.c index 507fae3871..bb352461fd 100644 --- a/components/tcp_transport/transport_ssl.c +++ b/components/tcp_transport/transport_ssl.c @@ -12,7 +12,6 @@ #include "esp_transport.h" #include "esp_transport_ssl.h" -#include "esp_transport_utils.h" #include "esp_transport_internal.h" #define INVALID_SOCKET (-1) @@ -39,23 +38,19 @@ typedef struct transport_esp_tls { int sockfd; } transport_esp_tls_t; -static inline struct transport_esp_tls * ssl_get_context_data(esp_transport_handle_t t) +/** + * @brief Destroys esp-tls transport used in the foundation transport + * + * @param[in] transport esp-tls handle + */ +void esp_transport_esp_tls_destroy(struct transport_esp_tls* transport_esp_tls); + +static inline transport_esp_tls_t *ssl_get_context_data(esp_transport_handle_t t) { if (!t) { return NULL; } - if (t->data) { // Prefer internal ssl context (independent from the list) - return (transport_esp_tls_t*)t->data; - } - if (t->base && t->base->transport_esp_tls) { // Next one is the lists inherent context - t->data = t->base->transport_esp_tls; // Optimize: if we have base context, use it as internal - return t->base->transport_esp_tls; - } - // If we don't have a valid context, let's to create one - transport_esp_tls_t *ssl = esp_transport_esp_tls_create(); - ESP_TRANSPORT_MEM_CHECK(TAG, ssl, return NULL) - t->data = ssl; - return ssl; + return (transport_esp_tls_t *)t->data; } static int esp_tls_connect_async(esp_transport_handle_t t, const char *host, int port, int timeout_ms, bool is_plain_tcp) @@ -103,7 +98,6 @@ static int ssl_connect(esp_transport_handle_t t, const char *host, int port, int transport_esp_tls_t *ssl = ssl_get_context_data(t); ssl->cfg.timeout_ms = timeout_ms; - ssl->cfg.is_plain_tcp = false; ssl->ssl_initialized = true; ssl->tls = esp_tls_init(); @@ -236,7 +230,7 @@ static int tcp_write(esp_transport_handle_t t, const char *buffer, int len, int ESP_LOGW(TAG, "Poll timeout or error, errno=%s, fd=%d, timeout_ms=%d", strerror(errno), ssl->sockfd, timeout_ms); return poll; } - int ret = send(ssl->sockfd,(const unsigned char *) buffer, len, 0); + int ret = send(ssl->sockfd, (const unsigned char *) buffer, len, 0); if (ret < 0) { ESP_LOGE(TAG, "tcp_write error, errno=%s", strerror(errno)); esp_transport_capture_errno(t, errno); @@ -325,18 +319,14 @@ static int base_close(esp_transport_handle_t t) return ret; } -static int base_destroy(esp_transport_handle_t t) +static int base_destroy(esp_transport_handle_t transport) { - transport_esp_tls_t *ssl = ssl_get_context_data(t); + transport_esp_tls_t *ssl = ssl_get_context_data(transport); if (ssl) { - esp_transport_close(t); - if (t->base && t->base->transport_esp_tls && - t->data == t->base->transport_esp_tls) { - // if internal ssl the same as the foundation transport, - // just zero out, it will be freed on list destroy - t->data = NULL; - } - esp_transport_esp_tls_destroy(t->data); // okay to pass NULL + esp_transport_close(transport); + esp_transport_destroy_foundation_transport(transport->foundation); + + esp_transport_esp_tls_destroy(transport->data); // okay to pass NULL } return 0; } @@ -348,7 +338,7 @@ void esp_transport_ssl_enable_global_ca_store(esp_transport_handle_t t) } #ifdef CONFIG_ESP_TLS_PSK_VERIFICATION -void esp_transport_ssl_set_psk_key_hint(esp_transport_handle_t t, const psk_hint_key_t* psk_hint_key) +void esp_transport_ssl_set_psk_key_hint(esp_transport_handle_t t, const psk_hint_key_t *psk_hint_key) { GET_SSL_FROM_TRANSPORT_OR_RETURN(ssl, t); ssl->cfg.psk_hint_key = psk_hint_key; @@ -463,19 +453,7 @@ void esp_transport_ssl_set_interface_name(esp_transport_handle_t t, struct ifreq ssl->cfg.if_name = if_name; } -esp_transport_handle_t esp_transport_ssl_init(void) -{ - esp_transport_handle_t t = esp_transport_init(); - if (t == NULL) { - return NULL; - } - esp_transport_set_func(t, ssl_connect, ssl_read, ssl_write, base_close, base_poll_read, base_poll_write, base_destroy); - esp_transport_set_async_connect_func(t, ssl_connect_async); - t->_get_socket = base_get_socket; - return t; -} - -struct transport_esp_tls* esp_transport_esp_tls_create(void) +static transport_esp_tls_t *esp_transport_esp_tls_create(void) { transport_esp_tls_t *transport_esp_tls = calloc(1, sizeof(transport_esp_tls_t)); if (transport_esp_tls == NULL) { @@ -485,21 +463,55 @@ struct transport_esp_tls* esp_transport_esp_tls_create(void) return transport_esp_tls; } -void esp_transport_esp_tls_destroy(struct transport_esp_tls* transport_esp_tls) +static esp_transport_handle_t esp_transport_base_init(void) +{ + esp_transport_handle_t transport = esp_transport_init(); + if (transport == NULL) { + return NULL; + } + transport->foundation = esp_transport_init_foundation_transport(); + ESP_TRANSPORT_MEM_CHECK(TAG, transport->foundation, + free(transport); + return NULL); + + transport->data = esp_transport_esp_tls_create(); + ESP_TRANSPORT_MEM_CHECK(TAG, transport->data, + free(transport->foundation); + free(transport); + return NULL) + return transport; +} + +esp_transport_handle_t esp_transport_ssl_init(void) +{ + esp_transport_handle_t ssl_transport = esp_transport_base_init(); + if (ssl_transport == NULL) { + return NULL; + } + ((transport_esp_tls_t *)ssl_transport->data)->cfg.is_plain_tcp = false; + esp_transport_set_func(ssl_transport, ssl_connect, ssl_read, ssl_write, base_close, base_poll_read, base_poll_write, base_destroy); + esp_transport_set_async_connect_func(ssl_transport, ssl_connect_async); + ssl_transport->_get_socket = base_get_socket; + return ssl_transport; +} + + +void esp_transport_esp_tls_destroy(struct transport_esp_tls *transport_esp_tls) { free(transport_esp_tls); } esp_transport_handle_t esp_transport_tcp_init(void) { - esp_transport_handle_t t = esp_transport_init(); - if (t == NULL) { + esp_transport_handle_t tcp_transport = esp_transport_base_init(); + if (tcp_transport == NULL) { return NULL; } - esp_transport_set_func(t, tcp_connect, tcp_read, tcp_write, base_close, base_poll_read, base_poll_write, base_destroy); - esp_transport_set_async_connect_func(t, tcp_connect_async); - t->_get_socket = base_get_socket; - return t; + ((transport_esp_tls_t *)tcp_transport->data)->cfg.is_plain_tcp = true; + esp_transport_set_func(tcp_transport, tcp_connect, tcp_read, tcp_write, base_close, base_poll_read, base_poll_write, base_destroy); + esp_transport_set_async_connect_func(tcp_transport, tcp_connect_async); + tcp_transport->_get_socket = base_get_socket; + return tcp_transport; } void esp_transport_tcp_set_keep_alive(esp_transport_handle_t t, esp_transport_keep_alive_t *keep_alive_cfg) diff --git a/components/tcp_transport/transport_utils.c b/components/tcp_transport/transport_utils.c deleted file mode 100644 index 5b538186c8..0000000000 --- a/components/tcp_transport/transport_utils.c +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2015-2021 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// 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 -#include -#include -#include -#include - -#include "esp_transport_utils.h" - -struct timeval* esp_transport_utils_ms_to_timeval(int timeout_ms, struct timeval *tv) -{ - if (timeout_ms == -1) { - return NULL; - } - tv->tv_sec = timeout_ms / 1000; - tv->tv_usec = (timeout_ms - (tv->tv_sec * 1000)) * 1000; - return tv; -} diff --git a/components/tcp_transport/transport_ws.c b/components/tcp_transport/transport_ws.c index 518e8a5e26..7004660f45 100644 --- a/components/tcp_transport/transport_ws.c +++ b/components/tcp_transport/transport_ws.c @@ -13,7 +13,6 @@ #include "esp_transport.h" #include "esp_transport_tcp.h" #include "esp_transport_ws.h" -#include "esp_transport_utils.h" #include "esp_transport_internal.h" #include "errno.h" #include "esp_tls_crypto.h" @@ -604,6 +603,10 @@ static int ws_get_socket(esp_transport_handle_t t) esp_transport_handle_t esp_transport_ws_init(esp_transport_handle_t parent_handle) { + if (parent_handle == NULL || parent_handle->foundation == NULL) { + ESP_LOGE(TAG, "Invalid parent ptotocol"); + return NULL; + } esp_transport_handle_t t = esp_transport_init(); if (t == NULL) { return NULL; @@ -614,6 +617,8 @@ esp_transport_handle_t esp_transport_ws_init(esp_transport_handle_t parent_handl return NULL; }); ws->parent = parent_handle; + t->foundation = parent_handle->foundation; + ws->path = strdup("/"); ESP_TRANSPORT_MEM_CHECK(TAG, ws->path, { diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index b9236666e5..52bd2e7363 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -1397,7 +1397,6 @@ components/spi_flash/test/test_spi_flash.c components/tcp_transport/include/esp_transport_ssl.h components/tcp_transport/include/esp_transport_tcp.h components/tcp_transport/include/esp_transport_ws.h -components/tcp_transport/private_include/esp_transport_utils.h components/tcp_transport/test/tcp_transport_fixtures.h components/tcp_transport/test/test_transport_basic.c components/tcp_transport/test/test_transport_connect.c