2022-01-14 00:26:59 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2017-11-13 22:16:20 -05:00
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2019-04-16 05:58:38 -04:00
|
|
|
#include <esp_tls.h>
|
2017-11-13 22:16:20 -05:00
|
|
|
|
2019-03-14 05:29:32 -04:00
|
|
|
#include "sys/queue.h"
|
2017-11-13 22:16:20 -05:00
|
|
|
#include "esp_log.h"
|
|
|
|
|
2020-07-17 11:59:05 -04:00
|
|
|
#include "esp_transport_internal.h"
|
2022-04-21 11:41:58 -04:00
|
|
|
#include "esp_transport.h"
|
2017-11-13 22:16:20 -05:00
|
|
|
|
|
|
|
static const char *TAG = "TRANSPORT";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This list will hold all transport available
|
|
|
|
*/
|
2018-09-26 05:56:47 -04:00
|
|
|
STAILQ_HEAD(esp_transport_list_t, esp_transport_item_t);
|
2017-11-13 22:16:20 -05:00
|
|
|
|
2021-01-23 03:46:06 -05:00
|
|
|
struct transport_esp_tls;
|
|
|
|
|
2018-10-02 09:19:46 -04:00
|
|
|
static esp_transport_handle_t esp_transport_get_default_parent(esp_transport_handle_t t)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* By default, the underlying transport layer handle is the handle itself
|
|
|
|
*/
|
|
|
|
return t;
|
|
|
|
}
|
2017-11-13 22:16:20 -05:00
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
esp_transport_list_handle_t esp_transport_list_init(void)
|
2017-11-13 22:16:20 -05:00
|
|
|
{
|
2022-04-21 11:41:58 -04:00
|
|
|
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;
|
2017-11-13 22:16:20 -05:00
|
|
|
}
|
|
|
|
|
2019-04-09 10:08:05 -04:00
|
|
|
esp_err_t esp_transport_list_add(esp_transport_list_handle_t h, esp_transport_handle_t t, const char *scheme)
|
2017-11-13 22:16:20 -05:00
|
|
|
{
|
2019-04-09 10:08:05 -04:00
|
|
|
if (h == NULL || t == NULL) {
|
2017-11-13 22:16:20 -05:00
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
t->scheme = calloc(1, strlen(scheme) + 1);
|
2018-09-26 05:56:47 -04:00
|
|
|
ESP_TRANSPORT_MEM_CHECK(TAG, t->scheme, return ESP_ERR_NO_MEM);
|
2017-11-13 22:16:20 -05:00
|
|
|
strcpy(t->scheme, scheme);
|
2022-04-21 11:41:58 -04:00
|
|
|
STAILQ_INSERT_TAIL(h, t, next);
|
2017-11-13 22:16:20 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2019-04-09 10:08:05 -04:00
|
|
|
esp_transport_handle_t esp_transport_list_get_transport(esp_transport_list_handle_t h, const char *scheme)
|
2017-11-13 22:16:20 -05:00
|
|
|
{
|
2019-04-09 10:08:05 -04:00
|
|
|
if (!h) {
|
2017-11-13 22:16:20 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (scheme == NULL) {
|
2022-04-21 11:41:58 -04:00
|
|
|
return STAILQ_FIRST(h);
|
2017-11-13 22:16:20 -05:00
|
|
|
}
|
2018-09-26 05:56:47 -04:00
|
|
|
esp_transport_handle_t item;
|
2022-04-21 11:41:58 -04:00
|
|
|
STAILQ_FOREACH(item, h, next) {
|
2017-11-13 22:16:20 -05:00
|
|
|
if (strcasecmp(item->scheme, scheme) == 0) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-04-09 10:08:05 -04:00
|
|
|
esp_err_t esp_transport_list_destroy(esp_transport_list_handle_t h)
|
2017-11-13 22:16:20 -05:00
|
|
|
{
|
2019-04-09 10:08:05 -04:00
|
|
|
esp_transport_list_clean(h);
|
|
|
|
free(h);
|
2017-11-13 22:16:20 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2019-04-09 10:08:05 -04:00
|
|
|
esp_err_t esp_transport_list_clean(esp_transport_list_handle_t h)
|
2017-11-13 22:16:20 -05:00
|
|
|
{
|
2022-04-21 11:41:58 -04:00
|
|
|
esp_transport_handle_t item = STAILQ_FIRST(h);
|
2018-09-26 05:56:47 -04:00
|
|
|
esp_transport_handle_t tmp;
|
2017-11-13 22:16:20 -05:00
|
|
|
while (item != NULL) {
|
|
|
|
tmp = STAILQ_NEXT(item, next);
|
2018-09-26 05:56:47 -04:00
|
|
|
esp_transport_destroy(item);
|
2017-11-13 22:16:20 -05:00
|
|
|
item = tmp;
|
|
|
|
}
|
2022-04-21 11:41:58 -04:00
|
|
|
STAILQ_INIT(h);
|
2017-11-13 22:16:20 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
esp_transport_handle_t esp_transport_init(void)
|
2017-11-13 22:16:20 -05:00
|
|
|
{
|
2022-04-21 11:41:58 -04:00
|
|
|
esp_transport_handle_t transport = calloc(1, sizeof(struct esp_transport_item_t));
|
|
|
|
ESP_TRANSPORT_MEM_CHECK(TAG, transport, return NULL);
|
|
|
|
return transport;
|
2017-11-13 22:16:20 -05:00
|
|
|
}
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
esp_transport_handle_t esp_transport_get_payload_transport_handle(esp_transport_handle_t t)
|
2018-07-24 10:59:03 -04:00
|
|
|
{
|
|
|
|
if (t && t->_read) {
|
2018-10-02 09:19:46 -04:00
|
|
|
return t->_parent_transfer(t);
|
2018-07-24 10:59:03 -04:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
esp_err_t esp_transport_destroy(esp_transport_handle_t t)
|
2017-11-13 22:16:20 -05:00
|
|
|
{
|
2022-04-21 11:41:58 -04:00
|
|
|
if (t && t->_destroy) {
|
2019-05-24 17:31:44 -04:00
|
|
|
t->_destroy(t);
|
|
|
|
}
|
2022-04-21 11:41:58 -04:00
|
|
|
if (t && t->scheme) {
|
2017-11-13 22:16:20 -05:00
|
|
|
free(t->scheme);
|
|
|
|
}
|
|
|
|
free(t);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
int esp_transport_connect(esp_transport_handle_t t, const char *host, int port, int timeout_ms)
|
2017-11-13 22:16:20 -05:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
if (t && t->_connect) {
|
|
|
|
return t->_connect(t, host, port, timeout_ms);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
int esp_transport_connect_async(esp_transport_handle_t t, const char *host, int port, int timeout_ms)
|
2018-08-07 13:56:59 -04:00
|
|
|
{
|
|
|
|
int ret = -1;
|
2018-10-24 03:32:47 -04:00
|
|
|
if (t && t->_connect_async) {
|
2018-08-07 13:56:59 -04:00
|
|
|
return t->_connect_async(t, host, port, timeout_ms);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
int esp_transport_read(esp_transport_handle_t t, char *buffer, int len, int timeout_ms)
|
2017-11-13 22:16:20 -05:00
|
|
|
{
|
|
|
|
if (t && t->_read) {
|
|
|
|
return t->_read(t, buffer, len, timeout_ms);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
int esp_transport_write(esp_transport_handle_t t, const char *buffer, int len, int timeout_ms)
|
2017-11-13 22:16:20 -05:00
|
|
|
{
|
|
|
|
if (t && t->_write) {
|
|
|
|
return t->_write(t, buffer, len, timeout_ms);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
int esp_transport_poll_read(esp_transport_handle_t t, int timeout_ms)
|
2017-11-13 22:16:20 -05:00
|
|
|
{
|
|
|
|
if (t && t->_poll_read) {
|
|
|
|
return t->_poll_read(t, timeout_ms);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
int esp_transport_poll_write(esp_transport_handle_t t, int timeout_ms)
|
2017-11-13 22:16:20 -05:00
|
|
|
{
|
|
|
|
if (t && t->_poll_write) {
|
|
|
|
return t->_poll_write(t, timeout_ms);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
int esp_transport_close(esp_transport_handle_t t)
|
2017-11-13 22:16:20 -05:00
|
|
|
{
|
|
|
|
if (t && t->_close) {
|
|
|
|
return t->_close(t);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
void *esp_transport_get_context_data(esp_transport_handle_t t)
|
2017-11-13 22:16:20 -05:00
|
|
|
{
|
|
|
|
if (t) {
|
|
|
|
return t->data;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
esp_err_t esp_transport_set_context_data(esp_transport_handle_t t, void *data)
|
2017-11-13 22:16:20 -05:00
|
|
|
{
|
|
|
|
if (t) {
|
|
|
|
t->data = data;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
esp_err_t esp_transport_set_func(esp_transport_handle_t t,
|
2017-11-13 22:16:20 -05:00
|
|
|
connect_func _connect,
|
|
|
|
io_read_func _read,
|
|
|
|
io_func _write,
|
|
|
|
trans_func _close,
|
|
|
|
poll_func _poll_read,
|
|
|
|
poll_func _poll_write,
|
2018-10-02 09:19:46 -04:00
|
|
|
trans_func _destroy)
|
2017-11-13 22:16:20 -05:00
|
|
|
{
|
|
|
|
if (t == NULL) {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
t->_connect = _connect;
|
|
|
|
t->_read = _read;
|
|
|
|
t->_write = _write;
|
|
|
|
t->_close = _close;
|
|
|
|
t->_poll_read = _poll_read;
|
|
|
|
t->_poll_write = _poll_write;
|
|
|
|
t->_destroy = _destroy;
|
2018-08-07 13:56:59 -04:00
|
|
|
t->_connect_async = NULL;
|
2018-10-02 09:19:46 -04:00
|
|
|
t->_parent_transfer = esp_transport_get_default_parent;
|
2017-11-13 22:16:20 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
int esp_transport_get_default_port(esp_transport_handle_t t)
|
2017-11-13 22:16:20 -05:00
|
|
|
{
|
|
|
|
if (t == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return t->port;
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:56:47 -04:00
|
|
|
esp_err_t esp_transport_set_default_port(esp_transport_handle_t t, int port)
|
2017-11-13 22:16:20 -05:00
|
|
|
{
|
|
|
|
if (t == NULL) {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
t->port = port;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
2018-07-24 10:59:03 -04:00
|
|
|
|
2018-10-02 09:19:46 -04:00
|
|
|
esp_err_t esp_transport_set_async_connect_func(esp_transport_handle_t t, connect_async_func _connect_async_func)
|
2018-07-24 10:59:03 -04:00
|
|
|
{
|
2018-10-02 09:19:46 -04:00
|
|
|
if (t == NULL) {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
t->_connect_async = _connect_async_func;
|
|
|
|
return ESP_OK;
|
2018-08-07 13:56:59 -04:00
|
|
|
}
|
|
|
|
|
2018-10-02 09:19:46 -04:00
|
|
|
esp_err_t esp_transport_set_parent_transport_func(esp_transport_handle_t t, payload_transfer_func _parent_transport)
|
2018-08-07 13:56:59 -04:00
|
|
|
{
|
|
|
|
if (t == NULL) {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2018-10-02 09:19:46 -04:00
|
|
|
t->_parent_transfer = _parent_transport;
|
2018-08-07 13:56:59 -04:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
2019-04-09 10:08:05 -04:00
|
|
|
|
2019-04-16 05:58:38 -04:00
|
|
|
esp_tls_error_handle_t esp_transport_get_error_handle(esp_transport_handle_t t)
|
2019-04-09 10:08:05 -04:00
|
|
|
{
|
2022-04-21 11:41:58 -04:00
|
|
|
if (t && t->foundation && t->foundation->error_handle) {
|
|
|
|
return &t->foundation->error_handle->esp_tls_err_h_base;
|
2019-04-16 05:58:38 -04:00
|
|
|
}
|
|
|
|
return NULL;
|
2019-04-09 10:08:05 -04:00
|
|
|
}
|
|
|
|
|
2020-04-08 14:04:33 -04:00
|
|
|
int esp_transport_get_errno(esp_transport_handle_t t)
|
|
|
|
{
|
2022-04-21 11:41:58 -04:00
|
|
|
if (t && t->foundation && t->foundation->error_handle) {
|
|
|
|
int actual_errno = t->foundation->error_handle->sock_errno;
|
|
|
|
t->foundation->error_handle->sock_errno = 0;
|
2020-04-08 14:04:33 -04:00
|
|
|
return actual_errno;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-03-07 00:29:54 -05:00
|
|
|
void capture_tcp_transport_error(esp_transport_handle_t t, enum esp_tcp_transport_err_t error)
|
2021-01-22 15:00:22 -05:00
|
|
|
{
|
|
|
|
esp_tls_last_error_t *err_handle = esp_transport_get_error_handle(t);
|
|
|
|
switch (error) {
|
|
|
|
case ERR_TCP_TRANSPORT_CONNECTION_CLOSED_BY_FIN:
|
|
|
|
err_handle->last_error = ESP_ERR_ESP_TLS_TCP_CLOSED_FIN;
|
|
|
|
break;
|
2022-03-07 00:29:54 -05:00
|
|
|
case ERR_TCP_TRANSPORT_CONNECTION_TIMEOUT:
|
|
|
|
err_handle->last_error = ESP_ERR_ESP_TLS_CONNECTION_TIMEOUT;
|
|
|
|
break;
|
2021-01-22 15:00:22 -05:00
|
|
|
case ERR_TCP_TRANSPORT_CONNECTION_FAILED:
|
|
|
|
err_handle->last_error = ESP_ERR_ESP_TLS_FAILED_CONNECT_TO_HOST;
|
|
|
|
break;
|
2021-03-19 09:51:43 -04:00
|
|
|
case ERR_TCP_TRANSPORT_NO_MEM:
|
|
|
|
err_handle->last_error = ESP_ERR_NO_MEM;
|
|
|
|
break;
|
2021-01-22 15:00:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 05:58:38 -04:00
|
|
|
void esp_transport_set_errors(esp_transport_handle_t t, const esp_tls_error_handle_t error_handle)
|
2019-04-09 10:08:05 -04:00
|
|
|
{
|
2022-04-21 11:41:58 -04:00
|
|
|
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));
|
2020-04-08 14:04:33 -04:00
|
|
|
int sock_error;
|
|
|
|
if (esp_tls_get_and_clear_error_type(error_handle, ESP_TLS_ERR_TYPE_SYSTEM, &sock_error) == ESP_OK) {
|
2022-04-21 11:41:58 -04:00
|
|
|
t->foundation->error_handle->sock_errno = sock_error;
|
2020-04-08 14:04:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_transport_capture_errno(esp_transport_handle_t t, int sock_errno)
|
|
|
|
{
|
2022-04-21 11:41:58 -04:00
|
|
|
if (t && t->foundation && t->foundation->error_handle) {
|
|
|
|
t->foundation->error_handle->sock_errno = sock_errno;
|
2019-04-09 10:08:05 -04:00
|
|
|
}
|
2020-07-17 11:59:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int esp_transport_get_socket(esp_transport_handle_t t)
|
|
|
|
{
|
|
|
|
if (t && t->_get_socket) {
|
|
|
|
return t->_get_socket(t);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2022-03-07 00:29:54 -05:00
|
|
|
|
|
|
|
esp_err_t esp_transport_translate_error(enum esp_tcp_transport_err_t error)
|
|
|
|
{
|
|
|
|
esp_err_t err = ESP_FAIL;
|
|
|
|
switch (error) {
|
|
|
|
case ERR_TCP_TRANSPORT_CONNECTION_CLOSED_BY_FIN:
|
|
|
|
err = ESP_ERR_TCP_TRANSPORT_CONNECTION_CLOSED_BY_FIN;
|
|
|
|
break;
|
|
|
|
case ERR_TCP_TRANSPORT_CONNECTION_TIMEOUT:
|
|
|
|
err = ESP_ERR_TCP_TRANSPORT_CONNECTION_TIMEOUT;
|
|
|
|
break;
|
|
|
|
case ERR_TCP_TRANSPORT_CONNECTION_FAILED:
|
|
|
|
err = ESP_ERR_TCP_TRANSPORT_CONNECTION_FAILED;
|
|
|
|
break;
|
|
|
|
case ERR_TCP_TRANSPORT_NO_MEM:
|
|
|
|
err = ESP_ERR_TCP_TRANSPORT_NO_MEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|