[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.
This commit is contained in:
Euripedes Rocha 2022-04-21 12:41:58 -03:00 committed by David Čermák
parent 7b5a3af407
commit 5778a7c726
10 changed files with 191 additions and 235 deletions

View File

@ -1,7 +1,7 @@
set(srcs set(srcs
"transport.c" "transport.c"
"transport_ssl.c" "transport_ssl.c"
"transport_utils.c") "transport_internal.c")
if(CONFIG_WS_TRANSPORT) if(CONFIG_WS_TRANSPORT)
list(APPEND srcs list(APPEND srcs

View File

@ -24,7 +24,7 @@ typedef struct esp_transport_keepalive {
int keep_alive_count; /*!< Keep-alive packet retry send count */ int keep_alive_count; /*!< Keep-alive packet retry send count */
} esp_transport_keep_alive_t; } 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 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); typedef int (*connect_func)(esp_transport_handle_t t, const char *host, int port, int timeout_ms);

View File

@ -7,9 +7,13 @@
#ifndef _ESP_TRANSPORT_INTERNAL_H_ #ifndef _ESP_TRANSPORT_INTERNAL_H_
#define _ESP_TRANSPORT_INTERNAL_H_ #define _ESP_TRANSPORT_INTERNAL_H_
#include "sys/time.h"
#include "esp_transport.h" #include "esp_transport.h"
#include "sys/socket.h" #include "sys/socket.h"
#include "sys/queue.h" #include "sys/queue.h"
#include "esp_log.h"
#include "esp_tls.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -17,9 +21,18 @@ extern "C" {
typedef int (*get_socket_func)(esp_transport_handle_t t); 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 { typedef struct esp_foundation_transport {
struct esp_transport_error_storage *error_handle; /*!< Pointer to the transport error container */ 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_foundation_transport_t; } esp_foundation_transport_t;
/** /**
@ -40,11 +53,57 @@ struct esp_transport_item_t {
payload_transfer_func _parent_transfer; /*!< Function returning underlying transport layer */ payload_transfer_func _parent_transfer; /*!< Function returning underlying transport layer */
get_socket_func _get_socket; /*!< Function returning the transport's socket */ get_socket_func _get_socket; /*!< Function returning the transport's socket */
esp_transport_keep_alive_t *keep_alive_cfg; /*!< TCP keep-alive config */ 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; 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 * @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); 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 * @brief Sets error to common transport handle
* *

View File

@ -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 <sys/time.h>
#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_ */

View File

@ -12,23 +12,11 @@
#include "sys/queue.h" #include "sys/queue.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_transport.h"
#include "esp_transport_internal.h" #include "esp_transport_internal.h"
#include "esp_transport_utils.h" #include "esp_transport.h"
static const char *TAG = "TRANSPORT"; 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 * 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; 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) 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 esp_transport_list_init(void)
{ {
esp_transport_list_handle_t transport = calloc(1, sizeof(esp_transport_internal_t)); esp_transport_list_handle_t transport_list = calloc(1, sizeof(struct esp_transport_list_t));
ESP_TRANSPORT_MEM_CHECK(TAG, transport, return NULL); ESP_TRANSPORT_MEM_CHECK(TAG, transport_list, return NULL);
STAILQ_INIT(&transport->list); STAILQ_INIT(transport_list);
transport->base = esp_transport_init_foundation_transport(); return transport_list;
ESP_TRANSPORT_MEM_CHECK(TAG, transport->base,
free(transport);
return NULL);
return transport;
} }
esp_err_t esp_transport_list_add(esp_transport_list_handle_t h, esp_transport_handle_t t, const char *scheme) 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); t->scheme = calloc(1, strlen(scheme) + 1);
ESP_TRANSPORT_MEM_CHECK(TAG, t->scheme, return ESP_ERR_NO_MEM); ESP_TRANSPORT_MEM_CHECK(TAG, t->scheme, return ESP_ERR_NO_MEM);
strcpy(t->scheme, scheme); strcpy(t->scheme, scheme);
STAILQ_INSERT_TAIL(&h->list, t, next); STAILQ_INSERT_TAIL(h, t, next);
// Each transport in a list to share the same error tracker
t->base = h->base;
return ESP_OK; return ESP_OK;
} }
@ -107,10 +58,10 @@ esp_transport_handle_t esp_transport_list_get_transport(esp_transport_list_handl
return NULL; return NULL;
} }
if (scheme == NULL) { if (scheme == NULL) {
return STAILQ_FIRST(&h->list); return STAILQ_FIRST(h);
} }
esp_transport_handle_t item; esp_transport_handle_t item;
STAILQ_FOREACH(item, &h->list, next) { STAILQ_FOREACH(item, h, next) {
if (strcasecmp(item->scheme, scheme) == 0) { if (strcasecmp(item->scheme, scheme) == 0) {
return item; 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_err_t esp_transport_list_destroy(esp_transport_list_handle_t h)
{ {
esp_transport_list_clean(h); esp_transport_list_clean(h);
esp_transport_destroy_foundation_transport(h->base);
free(h); free(h);
return ESP_OK; return ESP_OK;
} }
esp_err_t esp_transport_list_clean(esp_transport_list_handle_t h) 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; esp_transport_handle_t tmp;
while (item != NULL) { while (item != NULL) {
tmp = STAILQ_NEXT(item, next); tmp = STAILQ_NEXT(item, next);
esp_transport_destroy(item); esp_transport_destroy(item);
item = tmp; item = tmp;
} }
STAILQ_INIT(&h->list); STAILQ_INIT(h);
return ESP_OK; return ESP_OK;
} }
esp_transport_handle_t esp_transport_init(void) esp_transport_handle_t esp_transport_init(void)
{ {
esp_transport_handle_t t = calloc(1, sizeof(struct esp_transport_item_t)); esp_transport_handle_t transport = calloc(1, sizeof(struct esp_transport_item_t));
ESP_TRANSPORT_MEM_CHECK(TAG, t, return NULL); ESP_TRANSPORT_MEM_CHECK(TAG, transport, return NULL);
return t; return transport;
} }
esp_transport_handle_t esp_transport_get_payload_transport_handle(esp_transport_handle_t t) 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) esp_err_t esp_transport_destroy(esp_transport_handle_t t)
{ {
if (t->_destroy) { if (t && t->_destroy) {
t->_destroy(t); t->_destroy(t);
} }
if (t->scheme) { if (t && t->scheme) {
free(t->scheme); free(t->scheme);
} }
free(t); 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) esp_tls_error_handle_t esp_transport_get_error_handle(esp_transport_handle_t t)
{ {
if (t && t->base && t->base->error_handle) { if (t && t->foundation && t->foundation->error_handle) {
return &t->base->error_handle->esp_tls_err_h_base; return &t->foundation->error_handle->esp_tls_err_h_base;
} }
return NULL; return NULL;
} }
int esp_transport_get_errno(esp_transport_handle_t t) int esp_transport_get_errno(esp_transport_handle_t t)
{ {
if (t && t->base && t->base->error_handle) { if (t && t->foundation && t->foundation->error_handle) {
int actual_errno = t->base->error_handle->sock_errno; int actual_errno = t->foundation->error_handle->sock_errno;
t->base->error_handle->sock_errno = 0; t->foundation->error_handle->sock_errno = 0;
return actual_errno; return actual_errno;
} }
return -1; 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) 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) { if (t && t->foundation && t->foundation->error_handle) {
memcpy(&t->base->error_handle->esp_tls_err_h_base, error_handle, sizeof(esp_tls_last_error_t)); memcpy(&t->foundation->error_handle->esp_tls_err_h_base, error_handle, sizeof(esp_tls_last_error_t));
int sock_error; int sock_error;
if (esp_tls_get_and_clear_error_type(error_handle, ESP_TLS_ERR_TYPE_SYSTEM, &sock_error) == ESP_OK) { 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) void esp_transport_capture_errno(esp_transport_handle_t t, int sock_errno)
{ {
if (t && t->base && t->base->error_handle) { if (t && t->foundation && t->foundation->error_handle) {
t->base->error_handle->sock_errno = sock_errno; t->foundation->error_handle->sock_errno = sock_errno;
} }
} }

View File

@ -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);
}

View File

@ -12,7 +12,6 @@
#include "esp_transport.h" #include "esp_transport.h"
#include "esp_transport_ssl.h" #include "esp_transport_ssl.h"
#include "esp_transport_utils.h"
#include "esp_transport_internal.h" #include "esp_transport_internal.h"
#define INVALID_SOCKET (-1) #define INVALID_SOCKET (-1)
@ -39,23 +38,19 @@ typedef struct transport_esp_tls {
int sockfd; int sockfd;
} transport_esp_tls_t; } 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) { if (!t) {
return NULL; return NULL;
} }
if (t->data) { // Prefer internal ssl context (independent from the list) return (transport_esp_tls_t *)t->data;
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;
} }
static int esp_tls_connect_async(esp_transport_handle_t t, const char *host, int port, int timeout_ms, bool is_plain_tcp) 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); transport_esp_tls_t *ssl = ssl_get_context_data(t);
ssl->cfg.timeout_ms = timeout_ms; ssl->cfg.timeout_ms = timeout_ms;
ssl->cfg.is_plain_tcp = false;
ssl->ssl_initialized = true; ssl->ssl_initialized = true;
ssl->tls = esp_tls_init(); 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); ESP_LOGW(TAG, "Poll timeout or error, errno=%s, fd=%d, timeout_ms=%d", strerror(errno), ssl->sockfd, timeout_ms);
return poll; 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) { if (ret < 0) {
ESP_LOGE(TAG, "tcp_write error, errno=%s", strerror(errno)); ESP_LOGE(TAG, "tcp_write error, errno=%s", strerror(errno));
esp_transport_capture_errno(t, errno); esp_transport_capture_errno(t, errno);
@ -325,18 +319,14 @@ static int base_close(esp_transport_handle_t t)
return ret; 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) { if (ssl) {
esp_transport_close(t); esp_transport_close(transport);
if (t->base && t->base->transport_esp_tls && esp_transport_destroy_foundation_transport(transport->foundation);
t->data == t->base->transport_esp_tls) {
// if internal ssl the same as the foundation transport, esp_transport_esp_tls_destroy(transport->data); // okay to pass NULL
// just zero out, it will be freed on list destroy
t->data = NULL;
}
esp_transport_esp_tls_destroy(t->data); // okay to pass NULL
} }
return 0; 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 #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); GET_SSL_FROM_TRANSPORT_OR_RETURN(ssl, t);
ssl->cfg.psk_hint_key = psk_hint_key; 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; ssl->cfg.if_name = if_name;
} }
esp_transport_handle_t esp_transport_ssl_init(void) static transport_esp_tls_t *esp_transport_esp_tls_create(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)
{ {
transport_esp_tls_t *transport_esp_tls = calloc(1, sizeof(transport_esp_tls_t)); transport_esp_tls_t *transport_esp_tls = calloc(1, sizeof(transport_esp_tls_t));
if (transport_esp_tls == NULL) { if (transport_esp_tls == NULL) {
@ -485,21 +463,55 @@ struct transport_esp_tls* esp_transport_esp_tls_create(void)
return transport_esp_tls; 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); free(transport_esp_tls);
} }
esp_transport_handle_t esp_transport_tcp_init(void) esp_transport_handle_t esp_transport_tcp_init(void)
{ {
esp_transport_handle_t t = esp_transport_init(); esp_transport_handle_t tcp_transport = esp_transport_base_init();
if (t == NULL) { if (tcp_transport == NULL) {
return NULL; return NULL;
} }
esp_transport_set_func(t, tcp_connect, tcp_read, tcp_write, base_close, base_poll_read, base_poll_write, base_destroy); ((transport_esp_tls_t *)tcp_transport->data)->cfg.is_plain_tcp = true;
esp_transport_set_async_connect_func(t, tcp_connect_async); esp_transport_set_func(tcp_transport, tcp_connect, tcp_read, tcp_write, base_close, base_poll_read, base_poll_write, base_destroy);
t->_get_socket = base_get_socket; esp_transport_set_async_connect_func(tcp_transport, tcp_connect_async);
return t; 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) void esp_transport_tcp_set_keep_alive(esp_transport_handle_t t, esp_transport_keep_alive_t *keep_alive_cfg)

View File

@ -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 <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#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;
}

View File

@ -13,7 +13,6 @@
#include "esp_transport.h" #include "esp_transport.h"
#include "esp_transport_tcp.h" #include "esp_transport_tcp.h"
#include "esp_transport_ws.h" #include "esp_transport_ws.h"
#include "esp_transport_utils.h"
#include "esp_transport_internal.h" #include "esp_transport_internal.h"
#include "errno.h" #include "errno.h"
#include "esp_tls_crypto.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) 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(); esp_transport_handle_t t = esp_transport_init();
if (t == NULL) { if (t == NULL) {
return NULL; return NULL;
@ -614,6 +617,8 @@ esp_transport_handle_t esp_transport_ws_init(esp_transport_handle_t parent_handl
return NULL; return NULL;
}); });
ws->parent = parent_handle; ws->parent = parent_handle;
t->foundation = parent_handle->foundation;
ws->path = strdup("/"); ws->path = strdup("/");
ESP_TRANSPORT_MEM_CHECK(TAG, ws->path, { ESP_TRANSPORT_MEM_CHECK(TAG, ws->path, {

View File

@ -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_ssl.h
components/tcp_transport/include/esp_transport_tcp.h components/tcp_transport/include/esp_transport_tcp.h
components/tcp_transport/include/esp_transport_ws.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/tcp_transport_fixtures.h
components/tcp_transport/test/test_transport_basic.c components/tcp_transport/test/test_transport_basic.c
components/tcp_transport/test/test_transport_connect.c components/tcp_transport/test/test_transport_connect.c