mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
Merge branch 'feature/tcp_transport_pass_errors_stage1' into 'master'
esp-tls: capturing specific errors to be available in tcp_transport and then in application code See merge request espressif/esp-idf!4782
This commit is contained in:
commit
d67b9403e8
@ -1,4 +1,5 @@
|
||||
idf_component_register(SRCS "esp_tls.c"
|
||||
INCLUDE_DIRS "."
|
||||
PRIVATE_INCLUDE_DIRS "private_include"
|
||||
REQUIRES mbedtls
|
||||
PRIV_REQUIRES lwip nghttp)
|
||||
|
@ -1,3 +1,3 @@
|
||||
COMPONENT_SRCDIRS := .
|
||||
|
||||
COMPONENT_ADD_INCLUDEDIRS := .
|
||||
COMPONENT_ADD_INCLUDEDIRS := . private_include
|
||||
|
@ -22,6 +22,7 @@
|
||||
|
||||
#include <http_parser.h>
|
||||
#include "esp_tls.h"
|
||||
#include "esp_tls_error_capture_internal.h"
|
||||
#include <errno.h>
|
||||
|
||||
static const char *TAG = "esp-tls";
|
||||
@ -45,7 +46,7 @@ typedef struct esp_tls_pki_t {
|
||||
unsigned int privkey_password_len;
|
||||
} esp_tls_pki_t;
|
||||
|
||||
static struct addrinfo *resolve_host_name(const char *host, size_t hostlen)
|
||||
static esp_err_t resolve_host_name(const char *host, size_t hostlen, struct addrinfo **address_info)
|
||||
{
|
||||
struct addrinfo hints;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
@ -54,18 +55,17 @@ static struct addrinfo *resolve_host_name(const char *host, size_t hostlen)
|
||||
|
||||
char *use_host = strndup(host, hostlen);
|
||||
if (!use_host) {
|
||||
return NULL;
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG, "host:%s: strlen %lu", use_host, (unsigned long)hostlen);
|
||||
struct addrinfo *res;
|
||||
if (getaddrinfo(use_host, NULL, &hints, &res)) {
|
||||
if (getaddrinfo(use_host, NULL, &hints, address_info)) {
|
||||
ESP_LOGE(TAG, "couldn't get hostname for :%s:", use_host);
|
||||
free(use_host);
|
||||
return NULL;
|
||||
return ESP_ERR_ESP_TLS_CANNOT_RESOLVE_HOSTNAME;
|
||||
}
|
||||
free(use_host);
|
||||
return res;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static ssize_t tcp_read(esp_tls_t *tls, char *data, size_t datalen)
|
||||
@ -81,6 +81,7 @@ static ssize_t tls_read(esp_tls_t *tls, char *data, size_t datalen)
|
||||
return 0;
|
||||
}
|
||||
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
|
||||
ESP_LOGE(TAG, "read error :%d:", ret);
|
||||
}
|
||||
}
|
||||
@ -93,32 +94,35 @@ static void ms_to_timeval(int timeout_ms, struct timeval *tv)
|
||||
tv->tv_usec = (timeout_ms % 1000) * 1000;
|
||||
}
|
||||
|
||||
static int esp_tcp_connect(const char *host, int hostlen, int port, int *sockfd, const esp_tls_cfg_t *cfg)
|
||||
static esp_err_t esp_tcp_connect(const char *host, int hostlen, int port, int *sockfd, const esp_tls_t *tls, const esp_tls_cfg_t *cfg)
|
||||
{
|
||||
int ret = -1;
|
||||
struct addrinfo *res = resolve_host_name(host, hostlen);
|
||||
if (!res) {
|
||||
esp_err_t ret;
|
||||
struct addrinfo *addrinfo;
|
||||
if ((ret = resolve_host_name(host, hostlen, &addrinfo)) != ESP_OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
int fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||
int fd = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol);
|
||||
if (fd < 0) {
|
||||
ESP_LOGE(TAG, "Failed to create socket (family %d socktype %d protocol %d)", res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||
ESP_LOGE(TAG, "Failed to create socket (family %d socktype %d protocol %d)", addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol);
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_SYSTEM, errno);
|
||||
ret = ESP_ERR_ESP_TLS_CANNOT_CREATE_SOCKET;
|
||||
goto err_freeaddr;
|
||||
}
|
||||
|
||||
void *addr_ptr;
|
||||
if (res->ai_family == AF_INET) {
|
||||
struct sockaddr_in *p = (struct sockaddr_in *)res->ai_addr;
|
||||
if (addrinfo->ai_family == AF_INET) {
|
||||
struct sockaddr_in *p = (struct sockaddr_in *)addrinfo->ai_addr;
|
||||
p->sin_port = htons(port);
|
||||
addr_ptr = p;
|
||||
} else if (res->ai_family == AF_INET6) {
|
||||
struct sockaddr_in6 *p = (struct sockaddr_in6 *)res->ai_addr;
|
||||
} else if (addrinfo->ai_family == AF_INET6) {
|
||||
struct sockaddr_in6 *p = (struct sockaddr_in6 *)addrinfo->ai_addr;
|
||||
p->sin6_port = htons(port);
|
||||
p->sin6_family = AF_INET6;
|
||||
addr_ptr = p;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Unsupported protocol family %d", res->ai_family);
|
||||
ESP_LOGE(TAG, "Unsupported protocol family %d", addrinfo->ai_family);
|
||||
ret = ESP_ERR_ESP_TLS_UNSUPPORTED_PROTOCOL_FAMILY;
|
||||
goto err_freesocket;
|
||||
}
|
||||
|
||||
@ -134,21 +138,23 @@ static int esp_tcp_connect(const char *host, int hostlen, int port, int *sockfd,
|
||||
}
|
||||
}
|
||||
|
||||
ret = connect(fd, addr_ptr, res->ai_addrlen);
|
||||
ret = connect(fd, addr_ptr, addrinfo->ai_addrlen);
|
||||
if (ret < 0 && !(errno == EINPROGRESS && cfg && cfg->non_block)) {
|
||||
|
||||
ESP_LOGE(TAG, "Failed to connect to host (errno %d)", errno);
|
||||
ESP_LOGE(TAG, "Failed to connnect to host (errno %d)", errno);
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_SYSTEM, errno);
|
||||
ret = ESP_ERR_ESP_TLS_FAILED_CONNECT_TO_HOST;
|
||||
goto err_freesocket;
|
||||
}
|
||||
|
||||
*sockfd = fd;
|
||||
freeaddrinfo(res);
|
||||
return 0;
|
||||
freeaddrinfo(addrinfo);
|
||||
return ESP_OK;
|
||||
|
||||
err_freesocket:
|
||||
close(fd);
|
||||
err_freeaddr:
|
||||
freeaddrinfo(res);
|
||||
freeaddrinfo(addrinfo);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -186,6 +192,7 @@ esp_err_t esp_tls_set_global_ca_store(const unsigned char *cacert_pem_buf, const
|
||||
return ESP_FAIL;
|
||||
} else if (ret > 0) {
|
||||
ESP_LOGE(TAG, "mbedtls_x509_crt_parse was partly successful. No. of failed certificates: %d", ret);
|
||||
return ESP_ERR_MBEDTLS_CERT_PARTLY_OK;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
@ -209,6 +216,7 @@ static void verify_certificate(esp_tls_t *tls)
|
||||
char buf[100];
|
||||
if ((flags = mbedtls_ssl_get_verify_result(&tls->ssl)) != 0) {
|
||||
ESP_LOGI(TAG, "Failed to verify peer certificate!");
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS_CERT_FLAGS, flags);
|
||||
bzero(buf, sizeof(buf));
|
||||
mbedtls_x509_crt_verify_info(buf, sizeof(buf), " ! ", flags);
|
||||
ESP_LOGI(TAG, "verification info: %s", buf);
|
||||
@ -239,20 +247,20 @@ static void mbedtls_cleanup(esp_tls_t *tls)
|
||||
mbedtls_ssl_free(&tls->ssl);
|
||||
}
|
||||
|
||||
static int set_global_ca_store(esp_tls_t *tls)
|
||||
static esp_err_t set_global_ca_store(esp_tls_t *tls)
|
||||
{
|
||||
assert(tls);
|
||||
if (global_cacert == NULL) {
|
||||
ESP_LOGE(TAG, "global_cacert is NULL");
|
||||
return -1;
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
tls->cacert_ptr = global_cacert;
|
||||
mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
|
||||
mbedtls_ssl_conf_ca_chain(&tls->conf, tls->cacert_ptr, NULL);
|
||||
return 0;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static int set_ca_cert(esp_tls_t *tls, const unsigned char *cacert, size_t cacert_len)
|
||||
static esp_err_t set_ca_cert(esp_tls_t *tls, const unsigned char *cacert, size_t cacert_len)
|
||||
{
|
||||
assert(tls);
|
||||
tls->cacert_ptr = &tls->cacert;
|
||||
@ -260,14 +268,15 @@ static int set_ca_cert(esp_tls_t *tls, const unsigned char *cacert, size_t cacer
|
||||
int ret = mbedtls_x509_crt_parse(tls->cacert_ptr, cacert, cacert_len);
|
||||
if (ret < 0) {
|
||||
ESP_LOGE(TAG, "mbedtls_x509_crt_parse returned -0x%x", -ret);
|
||||
return ret;
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
|
||||
return ESP_ERR_MBEDTLS_X509_CRT_PARSE_FAILED;
|
||||
}
|
||||
mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
|
||||
mbedtls_ssl_conf_ca_chain(&tls->conf, tls->cacert_ptr, NULL);
|
||||
return 0;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static int set_pki_context(esp_tls_t *tls, const esp_tls_pki_t *pki)
|
||||
static esp_err_t set_pki_context(esp_tls_t *tls, const esp_tls_pki_t *pki)
|
||||
{
|
||||
assert(tls);
|
||||
assert(pki);
|
||||
@ -284,39 +293,44 @@ static int set_pki_context(esp_tls_t *tls, const esp_tls_pki_t *pki)
|
||||
ret = mbedtls_x509_crt_parse(pki->public_cert, pki->publiccert_pem_buf, pki->publiccert_pem_bytes);
|
||||
if (ret < 0) {
|
||||
ESP_LOGE(TAG, "mbedtls_x509_crt_parse returned -0x%x", -ret);
|
||||
return ret;
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
|
||||
return ESP_ERR_MBEDTLS_X509_CRT_PARSE_FAILED;
|
||||
}
|
||||
|
||||
ret = mbedtls_pk_parse_key(pki->pk_key, pki->privkey_pem_buf, pki->privkey_pem_bytes,
|
||||
NULL, 0);
|
||||
if (ret < 0) {
|
||||
ESP_LOGE(TAG, "mbedtls_pk_parse_keyfile returned -0x%x", -ret);
|
||||
return ret;
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
|
||||
return ESP_ERR_MBEDTLS_PK_PARSE_KEY_FAILED;
|
||||
}
|
||||
|
||||
ret = mbedtls_ssl_conf_own_cert(&tls->conf, pki->public_cert, pki->pk_key);
|
||||
if (ret < 0) {
|
||||
ESP_LOGE(TAG, "mbedtls_ssl_conf_own_cert returned -0x%x", -ret);
|
||||
return ret;
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
|
||||
return ESP_ERR_MBEDTLS_SSL_CONF_OWN_CERT_FAILED;
|
||||
}
|
||||
} else {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
return 0;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_ESP_TLS_SERVER
|
||||
static int set_server_config(esp_tls_cfg_server_t *cfg, esp_tls_t *tls)
|
||||
static esp_err_t set_server_config(esp_tls_cfg_server_t *cfg, esp_tls_t *tls)
|
||||
{
|
||||
assert(cfg != NULL);
|
||||
assert(tls != NULL);
|
||||
int ret;
|
||||
esp_err_t esp_ret;
|
||||
if ((ret = mbedtls_ssl_config_defaults(&tls->conf,
|
||||
MBEDTLS_SSL_IS_SERVER,
|
||||
MBEDTLS_SSL_TRANSPORT_STREAM,
|
||||
MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
|
||||
ESP_LOGE(TAG, "mbedtls_ssl_config_defaults returned %d", ret);
|
||||
return ret;
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
|
||||
return ESP_ERR_MBEDTLS_SSL_CONFIG_DEFAULTS_FAILED;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_MBEDTLS_SSL_ALPN
|
||||
@ -326,9 +340,9 @@ static int set_server_config(esp_tls_cfg_server_t *cfg, esp_tls_t *tls)
|
||||
#endif
|
||||
|
||||
if (cfg->cacert_pem_buf != NULL) {
|
||||
ret = set_ca_cert(tls, cfg->cacert_pem_buf, cfg->cacert_pem_bytes);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
esp_ret = set_ca_cert(tls, cfg->cacert_pem_buf, cfg->cacert_pem_bytes);
|
||||
if (esp_ret != ESP_OK) {
|
||||
return esp_ret;
|
||||
}
|
||||
} else {
|
||||
mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_NONE);
|
||||
@ -345,20 +359,20 @@ static int set_server_config(esp_tls_cfg_server_t *cfg, esp_tls_t *tls)
|
||||
.privkey_password = cfg->serverkey_password,
|
||||
.privkey_password_len = cfg->serverkey_password_len,
|
||||
};
|
||||
ret = set_pki_context(tls, &pki);
|
||||
if (ret != 0) {
|
||||
esp_ret = set_pki_context(tls, &pki);
|
||||
if (esp_ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to set server pki context");
|
||||
return ret;
|
||||
return esp_ret;
|
||||
}
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Missing server certificate and/or key");
|
||||
return -1;
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
return 0;
|
||||
return ESP_OK;
|
||||
}
|
||||
#endif /* ! CONFIG_ESP_TLS_SERVER */
|
||||
|
||||
static int set_client_config(const char *hostname, size_t hostlen, esp_tls_cfg_t *cfg, esp_tls_t *tls)
|
||||
static esp_err_t set_client_config(const char *hostname, size_t hostlen, esp_tls_cfg_t *cfg, esp_tls_t *tls)
|
||||
{
|
||||
assert(cfg != NULL);
|
||||
assert(tls != NULL);
|
||||
@ -372,13 +386,14 @@ static int set_client_config(const char *hostname, size_t hostlen, esp_tls_cfg_t
|
||||
}
|
||||
|
||||
if (use_host == NULL) {
|
||||
return -1;
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
/* Hostname set here should match CN in server certificate */
|
||||
if ((ret = mbedtls_ssl_set_hostname(&tls->ssl, use_host)) != 0) {
|
||||
ESP_LOGE(TAG, "mbedtls_ssl_set_hostname returned -0x%x", -ret);
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
|
||||
free(use_host);
|
||||
return ret;
|
||||
return ESP_ERR_MBEDTLS_SSL_SET_HOSTNAME_FAILED;
|
||||
}
|
||||
free(use_host);
|
||||
}
|
||||
@ -387,25 +402,29 @@ static int set_client_config(const char *hostname, size_t hostlen, esp_tls_cfg_t
|
||||
MBEDTLS_SSL_IS_CLIENT,
|
||||
MBEDTLS_SSL_TRANSPORT_STREAM,
|
||||
MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
|
||||
ESP_LOGE(TAG, "mbedtls_ssl_config_defaults returned %d", ret);
|
||||
return ret;
|
||||
ESP_LOGE(TAG, "mbedtls_ssl_config_defaults returned -0x%x", -ret);
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
|
||||
return ESP_ERR_MBEDTLS_SSL_CONFIG_DEFAULTS_FAILED;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_MBEDTLS_SSL_ALPN
|
||||
if (cfg->alpn_protos) {
|
||||
mbedtls_ssl_conf_alpn_protocols(&tls->conf, cfg->alpn_protos);
|
||||
if ((ret =mbedtls_ssl_conf_alpn_protocols(&tls->conf, cfg->alpn_protos) != 0)) {
|
||||
ESP_LOGE(TAG, "mbedtls_ssl_conf_alpn_protocols returned -0x%x", -ret);
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
|
||||
return ESP_ERR_MBEDTLS_SSL_CONF_ALPN_PROTOCOLS_FAILED;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (cfg->use_global_ca_store == true) {
|
||||
ret = set_global_ca_store(tls);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
esp_err_t esp_ret = set_global_ca_store(tls);
|
||||
if (esp_ret != ESP_OK) {
|
||||
return esp_ret;
|
||||
}
|
||||
} else if (cfg->cacert_pem_buf != NULL) {
|
||||
ret = set_ca_cert(tls, cfg->cacert_pem_buf, cfg->cacert_pem_bytes);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
esp_err_t esp_ret = set_ca_cert(tls, cfg->cacert_pem_buf, cfg->cacert_pem_bytes);
|
||||
if (esp_ret != ESP_OK) {
|
||||
return esp_ret;
|
||||
}
|
||||
} else {
|
||||
mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_NONE);
|
||||
@ -422,23 +441,24 @@ static int set_client_config(const char *hostname, size_t hostlen, esp_tls_cfg_t
|
||||
.privkey_password = cfg->clientkey_password,
|
||||
.privkey_password_len = cfg->clientkey_password_len,
|
||||
};
|
||||
ret = set_pki_context(tls, &pki);
|
||||
if (ret < 0) {
|
||||
esp_err_t esp_ret = set_pki_context(tls, &pki);
|
||||
if (esp_ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to set server pki context");
|
||||
return ret;
|
||||
return esp_ret;
|
||||
}
|
||||
} else if (cfg->clientcert_pem_buf != NULL || cfg->clientkey_pem_buf != NULL) {
|
||||
ESP_LOGE(TAG, "You have to provide both clientcert_pem_buf and clientkey_pem_buf for mutual authentication");
|
||||
return -1;
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
return 0;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static int create_ssl_handle(const char *hostname, size_t hostlen, const void *cfg, esp_tls_t *tls)
|
||||
static esp_err_t create_ssl_handle(const char *hostname, size_t hostlen, const void *cfg, esp_tls_t *tls)
|
||||
{
|
||||
assert(cfg != NULL);
|
||||
assert(tls != NULL);
|
||||
int ret;
|
||||
esp_err_t esp_ret;
|
||||
tls->server_fd.fd = tls->sockfd;
|
||||
mbedtls_ssl_init(&tls->ssl);
|
||||
mbedtls_ctr_drbg_init(&tls->ctr_drbg);
|
||||
@ -446,15 +466,15 @@ static int create_ssl_handle(const char *hostname, size_t hostlen, const void *c
|
||||
mbedtls_entropy_init(&tls->entropy);
|
||||
|
||||
if (tls->role == ESP_TLS_CLIENT) {
|
||||
ret = set_client_config(hostname, hostlen, (esp_tls_cfg_t *)cfg, tls);
|
||||
if (ret != 0) {
|
||||
esp_ret = set_client_config(hostname, hostlen, (esp_tls_cfg_t *)cfg, tls);
|
||||
if (esp_ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to set client configurations");
|
||||
goto exit;
|
||||
}
|
||||
#ifdef CONFIG_ESP_TLS_SERVER
|
||||
} else if (tls->role == ESP_TLS_SERVER) {
|
||||
ret = set_server_config((esp_tls_cfg_server_t *) cfg, tls);
|
||||
if (ret != 0) {
|
||||
esp_ret = set_server_config((esp_tls_cfg_server_t *) cfg, tls);
|
||||
if (esp_ret != 0) {
|
||||
ESP_LOGE(TAG, "Failed to set server configurations");
|
||||
goto exit;
|
||||
}
|
||||
@ -463,7 +483,9 @@ static int create_ssl_handle(const char *hostname, size_t hostlen, const void *c
|
||||
|
||||
if ((ret = mbedtls_ctr_drbg_seed(&tls->ctr_drbg,
|
||||
mbedtls_entropy_func, &tls->entropy, NULL, 0)) != 0) {
|
||||
ESP_LOGE(TAG, "mbedtls_ctr_drbg_seed returned %d", ret);
|
||||
ESP_LOGE(TAG, "mbedtls_ctr_drbg_seed returned -0x%x", -ret);
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
|
||||
esp_ret = ESP_ERR_MBEDTLS_CTR_DRBG_SEED_FAILED;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@ -475,14 +497,16 @@ static int create_ssl_handle(const char *hostname, size_t hostlen, const void *c
|
||||
|
||||
if ((ret = mbedtls_ssl_setup(&tls->ssl, &tls->conf)) != 0) {
|
||||
ESP_LOGE(TAG, "mbedtls_ssl_setup returned -0x%x", -ret);
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
|
||||
esp_ret = ESP_ERR_MBEDTLS_SSL_SETUP_FAILED;
|
||||
goto exit;
|
||||
}
|
||||
mbedtls_ssl_set_bio(&tls->ssl, &tls->server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
|
||||
|
||||
return 0;
|
||||
return ESP_OK;
|
||||
exit:
|
||||
mbedtls_cleanup(tls);
|
||||
return ret;
|
||||
return esp_ret;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -497,6 +521,7 @@ void esp_tls_conn_delete(esp_tls_t *tls)
|
||||
} else if (tls->sockfd >= 0) {
|
||||
close(tls->sockfd);
|
||||
}
|
||||
free(tls->error_handle);
|
||||
free(tls);
|
||||
}
|
||||
};
|
||||
@ -511,6 +536,8 @@ static ssize_t tls_write(esp_tls_t *tls, const char *data, size_t datalen)
|
||||
ssize_t ret = mbedtls_ssl_write(&tls->ssl, (unsigned char*) data, datalen);
|
||||
if (ret < 0) {
|
||||
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_ESP, ESP_ERR_MBEDTLS_SSL_WRITE_FAILED);
|
||||
ESP_LOGE(TAG, "write error :%d:", ret);
|
||||
}
|
||||
}
|
||||
@ -523,6 +550,9 @@ static int esp_tls_low_level_conn(const char *hostname, int hostlen, int port, c
|
||||
ESP_LOGE(TAG, "empty esp_tls parameter");
|
||||
return -1;
|
||||
}
|
||||
esp_err_t esp_ret;
|
||||
int ret;
|
||||
|
||||
/* These states are used to keep a tab on connection progress in case of non-blocking connect,
|
||||
and in case of blocking connect these cases will get executed one after the other */
|
||||
switch (tls->conn_state) {
|
||||
@ -532,8 +562,8 @@ static int esp_tls_low_level_conn(const char *hostname, int hostlen, int port, c
|
||||
mbedtls_net_init(&tls->server_fd);
|
||||
tls->is_tls = true;
|
||||
}
|
||||
int ret = esp_tcp_connect(hostname, hostlen, port, &tls->sockfd, cfg);
|
||||
if (ret < 0) {
|
||||
if ((esp_ret = esp_tcp_connect(hostname, hostlen, port, &tls->sockfd, tls, cfg)) != ESP_OK) {
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_ESP, esp_ret);
|
||||
return -1;
|
||||
}
|
||||
if (!cfg) {
|
||||
@ -568,15 +598,18 @@ static int esp_tls_low_level_conn(const char *hostname, int hostlen, int port, c
|
||||
/* pending error check */
|
||||
if (getsockopt(tls->sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
|
||||
ESP_LOGD(TAG, "Non blocking connect failed");
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_SYSTEM, errno);
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_ESP, ESP_ERR_ESP_TLS_SOCKET_SETOPT_FAILED);
|
||||
tls->conn_state = ESP_TLS_FAIL;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* By now, the connection has been established */
|
||||
ret = create_ssl_handle(hostname, hostlen, cfg, tls);
|
||||
if (ret != 0) {
|
||||
ESP_LOGD(TAG, "create_ssl_handshake failed");
|
||||
esp_ret = create_ssl_handle(hostname, hostlen, cfg, tls);
|
||||
if (esp_ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "create_ssl_handle failed");
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_ESP, esp_ret);
|
||||
tls->conn_state = ESP_TLS_FAIL;
|
||||
return -1;
|
||||
}
|
||||
@ -593,6 +626,8 @@ static int esp_tls_low_level_conn(const char *hostname, int hostlen, int port, c
|
||||
} else {
|
||||
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
|
||||
ESP_LOGE(TAG, "mbedtls_ssl_handshake returned -0x%x", -ret);
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_MBEDTLS, -ret);
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_ESP, ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED);
|
||||
if (cfg->cacert_pem_buf != NULL || cfg->use_global_ca_store == true) {
|
||||
/* This is to check whether handshake failed due to invalid certificate*/
|
||||
verify_certificate(tls);
|
||||
@ -639,6 +674,22 @@ esp_tls_t *esp_tls_conn_new(const char *hostname, int hostlen, int port, const e
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int esp_tls_conn_new_sync(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls)
|
||||
{
|
||||
/* esp_tls_conn_new_sync() is a sync alternative to esp_tls_conn_new_async() with symetric function prototype
|
||||
it is an alternative to esp_tls_conn_new() which is left for compatibility reasons */
|
||||
while (1) {
|
||||
int ret = esp_tls_low_level_conn(hostname, hostlen, port, cfg, tls);
|
||||
if (ret == 1) {
|
||||
return ret;
|
||||
} else if (ret == -1) {
|
||||
ESP_LOGE(TAG, "Failed to open new connection");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief Create a new TLS/SSL non-blocking connection
|
||||
*/
|
||||
@ -670,17 +721,21 @@ esp_tls_t *esp_tls_conn_http_new(const char *url, const esp_tls_cfg_t *cfg)
|
||||
struct http_parser_url u;
|
||||
http_parser_url_init(&u);
|
||||
http_parser_parse_url(url, strlen(url), 0, &u);
|
||||
|
||||
esp_tls_t *tls = esp_tls_init();
|
||||
if (!tls) return NULL;
|
||||
/* Connect to host */
|
||||
return esp_tls_conn_new(&url[u.field_data[UF_HOST].off], u.field_data[UF_HOST].len,
|
||||
get_port(url, &u), cfg);
|
||||
if (esp_tls_conn_new_sync(&url[u.field_data[UF_HOST].off], u.field_data[UF_HOST].len,
|
||||
get_port(url, &u), cfg, tls) == 1) {
|
||||
return tls;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t esp_tls_get_bytes_avail(esp_tls_t *tls)
|
||||
ssize_t esp_tls_get_bytes_avail(esp_tls_t *tls)
|
||||
{
|
||||
if (!tls) {
|
||||
ESP_LOGE(TAG, "empty arg passed to esp_tls_get_bytes_avail()");
|
||||
return ESP_FAIL;
|
||||
return -1;
|
||||
}
|
||||
return mbedtls_ssl_get_bytes_avail(&tls->ssl);
|
||||
}
|
||||
@ -711,14 +766,16 @@ int esp_tls_server_session_create(esp_tls_cfg_server_t *cfg, int sockfd, esp_tls
|
||||
}
|
||||
tls->role = ESP_TLS_SERVER;
|
||||
tls->sockfd = sockfd;
|
||||
int ret = create_ssl_handle(NULL, 0, cfg, tls);
|
||||
if (ret != 0) {
|
||||
ESP_LOGD(TAG, "create_ssl_handle failed");
|
||||
esp_err_t esp_ret = create_ssl_handle(NULL, 0, cfg, tls);
|
||||
if (esp_ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "create_ssl_handle failed");
|
||||
ESP_INT_EVENT_TRACKER_CAPTURE(tls->error_handle, ERR_TYPE_ESP, esp_ret);
|
||||
tls->conn_state = ESP_TLS_FAIL;
|
||||
return ret;
|
||||
return -1;
|
||||
}
|
||||
tls->read = tls_read;
|
||||
tls->write = tls_write;
|
||||
int ret;
|
||||
while ((ret = mbedtls_ssl_handshake(&tls->ssl)) != 0) {
|
||||
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
|
||||
ESP_LOGE(TAG, "mbedtls_ssl_handshake returned %d", ret);
|
||||
@ -739,3 +796,34 @@ void esp_tls_server_session_delete(esp_tls_t *tls)
|
||||
}
|
||||
};
|
||||
#endif /* ! CONFIG_ESP_TLS_SERVER */
|
||||
|
||||
esp_tls_t *esp_tls_init()
|
||||
{
|
||||
esp_tls_t *tls = (esp_tls_t *)calloc(1, sizeof(esp_tls_t));
|
||||
if (!tls) {
|
||||
return NULL;
|
||||
}
|
||||
tls->error_handle = calloc(1, sizeof(esp_tls_last_error_t));
|
||||
if (!tls->error_handle) {
|
||||
free(tls);
|
||||
return NULL;
|
||||
}
|
||||
tls->server_fd.fd = tls->sockfd = -1;
|
||||
return tls;
|
||||
}
|
||||
|
||||
esp_err_t esp_tls_get_and_clear_last_error(esp_tls_error_handle_t h, int *mbedtls_code, int *mbedtls_flags)
|
||||
{
|
||||
if (!h) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
esp_err_t last_err = h->last_error;
|
||||
if (mbedtls_code) {
|
||||
*mbedtls_code = h->mbedtls_error_code;
|
||||
}
|
||||
if (mbedtls_flags) {
|
||||
*mbedtls_flags = h->mbedtls_flags;
|
||||
}
|
||||
memset(h, 0, sizeof(esp_tls_last_error_t));
|
||||
return last_err;
|
||||
}
|
||||
|
@ -31,6 +31,35 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define ESP_ERR_ESP_TLS_BASE 0x8000 /*!< Starting number of ESP-TLS error codes */
|
||||
#define ESP_ERR_ESP_TLS_CANNOT_RESOLVE_HOSTNAME (ESP_ERR_ESP_TLS_BASE + 0x01) /*!< Error if hostname couldn't be resolved upon tls connection */
|
||||
#define ESP_ERR_ESP_TLS_CANNOT_CREATE_SOCKET (ESP_ERR_ESP_TLS_BASE + 0x02) /*!< Failed to create socket */
|
||||
#define ESP_ERR_ESP_TLS_UNSUPPORTED_PROTOCOL_FAMILY (ESP_ERR_ESP_TLS_BASE + 0x03) /*!< Unsupported protocol family */
|
||||
#define ESP_ERR_ESP_TLS_FAILED_CONNECT_TO_HOST (ESP_ERR_ESP_TLS_BASE + 0x04) /*!< Failed to connect to host */
|
||||
#define ESP_ERR_ESP_TLS_SOCKET_SETOPT_FAILED (ESP_ERR_ESP_TLS_BASE + 0x05) /*!< failed to set socket option */
|
||||
#define ESP_ERR_MBEDTLS_CERT_PARTLY_OK (ESP_ERR_ESP_TLS_BASE + 0x06) /*!< mbedtls parse certificates was partly successful */
|
||||
#define ESP_ERR_MBEDTLS_CTR_DRBG_SEED_FAILED (ESP_ERR_ESP_TLS_BASE + 0x07) /*!< mbedtls api returned error */
|
||||
#define ESP_ERR_MBEDTLS_SSL_SET_HOSTNAME_FAILED (ESP_ERR_ESP_TLS_BASE + 0x08) /*!< mbedtls api returned error */
|
||||
#define ESP_ERR_MBEDTLS_SSL_CONFIG_DEFAULTS_FAILED (ESP_ERR_ESP_TLS_BASE + 0x09) /*!< mbedtls api returned error */
|
||||
#define ESP_ERR_MBEDTLS_SSL_CONF_ALPN_PROTOCOLS_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0A) /*!< mbedtls api returned error */
|
||||
#define ESP_ERR_MBEDTLS_X509_CRT_PARSE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0B) /*!< mbedtls api returned error */
|
||||
#define ESP_ERR_MBEDTLS_SSL_CONF_OWN_CERT_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0C) /*!< mbedtls api returned error */
|
||||
#define ESP_ERR_MBEDTLS_SSL_SETUP_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0D) /*!< mbedtls api returned error */
|
||||
#define ESP_ERR_MBEDTLS_SSL_WRITE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0E) /*!< mbedtls api returned error */
|
||||
#define ESP_ERR_MBEDTLS_PK_PARSE_KEY_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0F) /*!< mbedtls api returned failed */
|
||||
#define ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x10) /*!< mbedtls api returned failed */
|
||||
|
||||
typedef struct esp_tls_last_error* esp_tls_error_handle_t;
|
||||
|
||||
/**
|
||||
* @brief Error structure containing relevant errors in case tls error occurred
|
||||
*/
|
||||
typedef struct esp_tls_last_error {
|
||||
esp_err_t last_error; /*!< error code (based on ESP_ERR_ESP_TLS_BASE) of the last occurred error */
|
||||
int mbedtls_error_code; /*!< mbedtls error code from last mbedtls failed api */
|
||||
int mbedtls_flags; /*!< last certification verification flags */
|
||||
} esp_tls_last_error_t;
|
||||
|
||||
/**
|
||||
* @brief ESP-TLS Connection State
|
||||
*/
|
||||
@ -186,23 +215,66 @@ typedef struct esp_tls {
|
||||
esp_tls_role_t role; /*!< esp-tls role
|
||||
- ESP_TLS_CLIENT
|
||||
- ESP_TLS_SERVER */
|
||||
|
||||
esp_tls_error_handle_t error_handle; /*!< handle to error descriptor */
|
||||
|
||||
} esp_tls_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Create TLS connection
|
||||
*
|
||||
* This function allocates and initializes esp-tls structure handle.
|
||||
*
|
||||
* @return tls Pointer to esp-tls as esp-tls handle if successfully initialized,
|
||||
* NULL if allocation error
|
||||
*/
|
||||
esp_tls_t *esp_tls_init();
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Create a new blocking TLS/SSL connection
|
||||
*
|
||||
* This function establishes a TLS/SSL connection with the specified host in blocking manner.
|
||||
*
|
||||
*
|
||||
* Note: This API is present for backward compatibility reasons. Alternative function
|
||||
* with the same functionality is `esp_tls_conn_new_sync` (and its asynchronous version
|
||||
* `esp_tls_conn_new_async`)
|
||||
*
|
||||
* @param[in] hostname Hostname of the host.
|
||||
* @param[in] hostlen Length of hostname.
|
||||
* @param[in] port Port number of the host.
|
||||
* @param[in] cfg TLS configuration as esp_tls_cfg_t. If you wish to open
|
||||
* @param[in] cfg TLS configuration as esp_tls_cfg_t. If you wish to open
|
||||
* non-TLS connection, keep this NULL. For TLS connection,
|
||||
* a pass pointer to esp_tls_cfg_t. At a minimum, this
|
||||
* structure should be zero-initialized.
|
||||
*
|
||||
* @return pointer to esp_tls_t, or NULL if connection couldn't be opened.
|
||||
*/
|
||||
esp_tls_t *esp_tls_conn_new(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg);
|
||||
esp_tls_t *esp_tls_conn_new(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg) __attribute__ ((deprecated));
|
||||
|
||||
/**
|
||||
* @brief Create a new blocking TLS/SSL connection
|
||||
*
|
||||
* This function establishes a TLS/SSL connection with the specified host in blocking manner.
|
||||
*
|
||||
* @param[in] hostname Hostname of the host.
|
||||
* @param[in] hostlen Length of hostname.
|
||||
* @param[in] port Port number of the host.
|
||||
* @param[in] cfg TLS configuration as esp_tls_cfg_t. If you wish to open
|
||||
* non-TLS connection, keep this NULL. For TLS connection,
|
||||
* a pass pointer to esp_tls_cfg_t. At a minimum, this
|
||||
* structure should be zero-initialized.
|
||||
* @param[in] tls Pointer to esp-tls as esp-tls handle.
|
||||
*
|
||||
* @return
|
||||
* - -1 If connection establishment fails.
|
||||
* - 1 If connection establishment is successful.
|
||||
* - 0 Reserved for connection state is in progress.
|
||||
*/
|
||||
int esp_tls_conn_new_sync(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls);
|
||||
|
||||
/**
|
||||
* @brief Create a new blocking TLS/SSL connection with a given "HTTP" url
|
||||
@ -317,7 +389,7 @@ void esp_tls_conn_delete(esp_tls_t *tls);
|
||||
* - bytes available in the application data
|
||||
* record read buffer
|
||||
*/
|
||||
size_t esp_tls_get_bytes_avail(esp_tls_t *tls);
|
||||
ssize_t esp_tls_get_bytes_avail(esp_tls_t *tls);
|
||||
|
||||
/**
|
||||
* @brief Create a global CA store, initially empty.
|
||||
@ -373,6 +445,23 @@ mbedtls_x509_crt *esp_tls_get_global_ca_store();
|
||||
*/
|
||||
void esp_tls_free_global_ca_store();
|
||||
|
||||
/**
|
||||
* @brief Returns last error in esp_tls with detailed mbedtls related error codes.
|
||||
* The error information is cleared internally upon return
|
||||
*
|
||||
* @param[in] h esp-tls error handle.
|
||||
* @param[out] mbedtls_code last error code returned from mbedtls api (set to zero if none)
|
||||
* This pointer could be NULL if caller does not care about mbedtls_code
|
||||
* @param[out] mbedtls_flags last certification verification flags (set to zero if none)
|
||||
* This pointer could be NULL if caller does not care about mbedtls_flags
|
||||
*
|
||||
* @return
|
||||
* - ESP_ERR_INVALID_STATE if invalid parameters
|
||||
* - ESP_OK (0) if no error occurred
|
||||
* - specific error code (based on ESP_ERR_ESP_TLS_BASE) otherwise
|
||||
*/
|
||||
esp_err_t esp_tls_get_and_clear_last_error(esp_tls_error_handle_t h, int *mbedtls_code, int *mbedtls_flags);
|
||||
|
||||
#ifdef CONFIG_ESP_TLS_SERVER
|
||||
/**
|
||||
* @brief Create TLS/SSL server session
|
||||
|
@ -0,0 +1,61 @@
|
||||
// Copyright 2017-2019 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_TLS_ERROR_CAPTURE_INTERNAL_H__
|
||||
#define __ESP_TLS_ERROR_CAPTURE_INTERNAL_H__
|
||||
/**
|
||||
* Note: this is an implementation placeholder for error logger.
|
||||
* This version is internal to esp-tls component and only saves single esp_err of last occurred error
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Definition of different types/sources of error codes reported
|
||||
* from different components
|
||||
*/
|
||||
typedef enum {
|
||||
ERR_TYPE_UNKNOWN = 0,
|
||||
ERR_TYPE_SYSTEM,
|
||||
ERR_TYPE_MBEDTLS,
|
||||
ERR_TYPE_MBEDTLS_CERT_FLAGS,
|
||||
ERR_TYPE_ESP,
|
||||
} err_type_t;
|
||||
|
||||
/**
|
||||
* Error tracker logging macro, this implementation saves latest errors of
|
||||
* ERR_TYPE_ESP, ERR_TYPE_MBEDTLS and ERR_TYPE_MBEDTLS_CERT_FLAGS types
|
||||
*/
|
||||
#define ESP_INT_EVENT_TRACKER_CAPTURE(h, type, code) esp_int_event_tracker_capture(h, type, code)
|
||||
|
||||
static inline void esp_int_event_tracker_capture(esp_tls_error_handle_t h, uint32_t type, int code)
|
||||
{
|
||||
if (h) {
|
||||
if (type == ERR_TYPE_ESP) {
|
||||
h->last_error = code;
|
||||
} else if (type == ERR_TYPE_MBEDTLS) {
|
||||
h->mbedtls_error_code = code;
|
||||
} else if (type == ERR_TYPE_MBEDTLS_CERT_FLAGS) {
|
||||
h->mbedtls_flags = code;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif //__ESP_TLS_ERROR_CAPTURE_INTERNAL_H__
|
@ -1145,7 +1145,7 @@ int esp_http_client_write(esp_http_client_handle_t client, const char *buffer, i
|
||||
esp_err_t esp_http_client_close(esp_http_client_handle_t client)
|
||||
{
|
||||
if (client->state >= HTTP_STATE_INIT) {
|
||||
http_dispatch_event(client, HTTP_EVENT_DISCONNECTED, NULL, 0);
|
||||
http_dispatch_event(client, HTTP_EVENT_DISCONNECTED, esp_transport_get_error_handle(client->transport), 0);
|
||||
client->state = HTTP_STATE_INIT;
|
||||
return esp_transport_close(client->transport);
|
||||
}
|
||||
|
@ -16,7 +16,7 @@
|
||||
#ifndef _HTTP_UTILS_H_
|
||||
#define _HTTP_UTILS_H_
|
||||
#include <sys/time.h>
|
||||
#include "esp_transport_utils.h"
|
||||
|
||||
/**
|
||||
* @brief Assign new_str to *str pointer, and realloc *str if it not NULL
|
||||
*
|
||||
@ -80,7 +80,9 @@ char *http_utils_join_string(const char *first_str, int len_first, const char *s
|
||||
int http_utils_str_starts_with(const char *str, const char *start);
|
||||
|
||||
|
||||
#define HTTP_MEM_CHECK(TAG, a, action) ESP_TRANSPORT_MEM_CHECK(TAG, a, action)
|
||||
|
||||
#define HTTP_MEM_CHECK(TAG, a, action) if (!(a)) { \
|
||||
ESP_LOGE(TAG,"%s:%d (%s): %s", __FILE__, __LINE__, __FUNCTION__, "Memory exhausted"); \
|
||||
action; \
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -19,7 +19,6 @@
|
||||
#include "esp_transport_tcp.h"
|
||||
#include "esp_transport_ssl.h"
|
||||
#include "esp_transport_ws.h"
|
||||
#include "esp_transport_utils.h"
|
||||
/* using uri parser */
|
||||
#include "http_parser.h"
|
||||
#include "freertos/task.h"
|
||||
@ -42,6 +41,11 @@ static const char *TAG = "WEBSOCKET_CLIENT";
|
||||
#define WEBSOCKET_EVENT_QUEUE_SIZE (1)
|
||||
#define WEBSOCKET_SEND_EVENT_TIMEOUT_MS (1000/portTICK_RATE_MS)
|
||||
|
||||
#define ESP_WS_CLIENT_MEM_CHECK(TAG, a, action) if (!(a)) { \
|
||||
ESP_LOGE(TAG,"%s:%d (%s): %s", __FILE__, __LINE__, __FUNCTION__, "Memory exhausted"); \
|
||||
action; \
|
||||
}
|
||||
|
||||
const static int STOPPED_BIT = BIT0;
|
||||
|
||||
ESP_EVENT_DEFINE_BASE(WEBSOCKET_EVENTS);
|
||||
@ -139,7 +143,7 @@ static esp_err_t esp_websocket_client_set_config(esp_websocket_client_handle_t c
|
||||
|
||||
if (config->host) {
|
||||
cfg->host = strdup(config->host);
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, cfg->host, return ESP_ERR_NO_MEM);
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->host, return ESP_ERR_NO_MEM);
|
||||
}
|
||||
|
||||
if (config->port) {
|
||||
@ -149,7 +153,7 @@ static esp_err_t esp_websocket_client_set_config(esp_websocket_client_handle_t c
|
||||
if (config->username) {
|
||||
free(cfg->username);
|
||||
cfg->username = strdup(config->username);
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, cfg->username, {
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->username, {
|
||||
free(cfg->host);
|
||||
return ESP_ERR_NO_MEM;
|
||||
});
|
||||
@ -158,7 +162,7 @@ static esp_err_t esp_websocket_client_set_config(esp_websocket_client_handle_t c
|
||||
if (config->password) {
|
||||
free(cfg->password);
|
||||
cfg->password = strdup(config->password);
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, cfg->password, {
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->password, {
|
||||
free(cfg->host);
|
||||
free(cfg->username);
|
||||
return ESP_ERR_NO_MEM;
|
||||
@ -168,7 +172,7 @@ static esp_err_t esp_websocket_client_set_config(esp_websocket_client_handle_t c
|
||||
if (config->uri) {
|
||||
free(cfg->uri);
|
||||
cfg->uri = strdup(config->uri);
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, cfg->uri, {
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->uri, {
|
||||
free(cfg->host);
|
||||
free(cfg->username);
|
||||
free(cfg->password);
|
||||
@ -178,7 +182,7 @@ static esp_err_t esp_websocket_client_set_config(esp_websocket_client_handle_t c
|
||||
if (config->path) {
|
||||
free(cfg->path);
|
||||
cfg->path = strdup(config->path);
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, cfg->path, {
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, cfg->path, {
|
||||
free(cfg->uri);
|
||||
free(cfg->host);
|
||||
free(cfg->username);
|
||||
@ -222,7 +226,7 @@ static esp_err_t esp_websocket_client_destroy_config(esp_websocket_client_handle
|
||||
esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_client_config_t *config)
|
||||
{
|
||||
esp_websocket_client_handle_t client = calloc(1, sizeof(struct esp_websocket_client));
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, client, return NULL);
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, client, return NULL);
|
||||
|
||||
esp_event_loop_args_t event_args = {
|
||||
.queue_size = WEBSOCKET_EVENT_QUEUE_SIZE,
|
||||
@ -236,30 +240,30 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
|
||||
}
|
||||
|
||||
client->lock = xSemaphoreCreateMutex();
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, client->lock, goto _websocket_init_fail);
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, client->lock, goto _websocket_init_fail);
|
||||
|
||||
client->transport_list = esp_transport_list_init();
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, client->transport_list, goto _websocket_init_fail);
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, client->transport_list, goto _websocket_init_fail);
|
||||
|
||||
esp_transport_handle_t tcp = esp_transport_tcp_init();
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, tcp, goto _websocket_init_fail);
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, tcp, goto _websocket_init_fail);
|
||||
|
||||
esp_transport_set_default_port(tcp, WEBSOCKET_TCP_DEFAULT_PORT);
|
||||
esp_transport_list_add(client->transport_list, tcp, "_tcp"); // need to save to transport list, for cleanup
|
||||
|
||||
|
||||
esp_transport_handle_t ws = esp_transport_ws_init(tcp);
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, ws, goto _websocket_init_fail);
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, ws, goto _websocket_init_fail);
|
||||
|
||||
esp_transport_set_default_port(ws, WEBSOCKET_TCP_DEFAULT_PORT);
|
||||
esp_transport_list_add(client->transport_list, ws, "ws");
|
||||
if (config->transport == WEBSOCKET_TRANSPORT_OVER_TCP) {
|
||||
asprintf(&client->config->scheme, "ws");
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, client->config->scheme, goto _websocket_init_fail);
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->scheme, goto _websocket_init_fail);
|
||||
}
|
||||
|
||||
esp_transport_handle_t ssl = esp_transport_ssl_init();
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, ssl, goto _websocket_init_fail);
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, ssl, goto _websocket_init_fail);
|
||||
|
||||
esp_transport_set_default_port(ssl, WEBSOCKET_SSL_DEFAULT_PORT);
|
||||
if (config->cert_pem) {
|
||||
@ -268,18 +272,18 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
|
||||
esp_transport_list_add(client->transport_list, ssl, "_ssl"); // need to save to transport list, for cleanup
|
||||
|
||||
esp_transport_handle_t wss = esp_transport_ws_init(ssl);
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, wss, goto _websocket_init_fail);
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, wss, goto _websocket_init_fail);
|
||||
|
||||
esp_transport_set_default_port(wss, WEBSOCKET_SSL_DEFAULT_PORT);
|
||||
|
||||
esp_transport_list_add(client->transport_list, wss, "wss");
|
||||
if (config->transport == WEBSOCKET_TRANSPORT_OVER_TCP) {
|
||||
asprintf(&client->config->scheme, "wss");
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, client->config->scheme, goto _websocket_init_fail);
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->scheme, goto _websocket_init_fail);
|
||||
}
|
||||
|
||||
client->config = calloc(1, sizeof(websocket_config_storage_t));
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, client->config, goto _websocket_init_fail);
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, client->config, goto _websocket_init_fail);
|
||||
|
||||
if (config->uri) {
|
||||
if (esp_websocket_client_set_uri(client, config->uri) != ESP_OK) {
|
||||
@ -295,7 +299,7 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
|
||||
|
||||
if (client->config->scheme == NULL) {
|
||||
asprintf(&client->config->scheme, "ws");
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, client->config->scheme, goto _websocket_init_fail);
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->scheme, goto _websocket_init_fail);
|
||||
}
|
||||
|
||||
client->keepalive_tick_ms = _tick_get_ms();
|
||||
@ -307,15 +311,15 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie
|
||||
buffer_size = WEBSOCKET_BUFFER_SIZE_BYTE;
|
||||
}
|
||||
client->rx_buffer = malloc(buffer_size);
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, client->rx_buffer, {
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, client->rx_buffer, {
|
||||
goto _websocket_init_fail;
|
||||
});
|
||||
client->tx_buffer = malloc(buffer_size);
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, client->tx_buffer, {
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, client->tx_buffer, {
|
||||
goto _websocket_init_fail;
|
||||
});
|
||||
client->status_bits = xEventGroupCreate();
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, client->status_bits, {
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, client->status_bits, {
|
||||
goto _websocket_init_fail;
|
||||
});
|
||||
|
||||
@ -366,20 +370,20 @@ esp_err_t esp_websocket_client_set_uri(esp_websocket_client_handle_t client, con
|
||||
if (puri.field_data[UF_SCHEMA].len) {
|
||||
free(client->config->scheme);
|
||||
asprintf(&client->config->scheme, "%.*s", puri.field_data[UF_SCHEMA].len, uri + puri.field_data[UF_SCHEMA].off);
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, client->config->scheme, return ESP_ERR_NO_MEM);
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->scheme, return ESP_ERR_NO_MEM);
|
||||
}
|
||||
|
||||
if (puri.field_data[UF_HOST].len) {
|
||||
free(client->config->host);
|
||||
asprintf(&client->config->host, "%.*s", puri.field_data[UF_HOST].len, uri + puri.field_data[UF_HOST].off);
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, client->config->host, return ESP_ERR_NO_MEM);
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->host, return ESP_ERR_NO_MEM);
|
||||
}
|
||||
|
||||
|
||||
if (puri.field_data[UF_PATH].len) {
|
||||
free(client->config->path);
|
||||
asprintf(&client->config->path, "%.*s", puri.field_data[UF_PATH].len, uri + puri.field_data[UF_PATH].off);
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, client->config->path, return ESP_ERR_NO_MEM);
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->path, return ESP_ERR_NO_MEM);
|
||||
|
||||
esp_transport_handle_t trans = esp_transport_list_get_transport(client->transport_list, "ws");
|
||||
if (trans) {
|
||||
@ -404,11 +408,11 @@ esp_err_t esp_websocket_client_set_uri(esp_websocket_client_handle_t client, con
|
||||
pass ++;
|
||||
free(client->config->password);
|
||||
client->config->password = strdup(pass);
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, client->config->password, return ESP_ERR_NO_MEM);
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->password, return ESP_ERR_NO_MEM);
|
||||
}
|
||||
free(client->config->username);
|
||||
client->config->username = strdup(user_info);
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, client->config->username, return ESP_ERR_NO_MEM);
|
||||
ESP_WS_CLIENT_MEM_CHECK(TAG, client->config->username, return ESP_ERR_NO_MEM);
|
||||
free(user_info);
|
||||
} else {
|
||||
return ESP_ERR_NO_MEM;
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit 11f884623bd32cb4269f24f47847f5d046da93f5
|
||||
Subproject commit 0cc4077bd3e10bb93456ff2785309ec9237a5906
|
@ -4,5 +4,6 @@ idf_component_register(SRCS "transport.c"
|
||||
"transport_ws.c"
|
||||
"transport_utils.c"
|
||||
"transport_strcasestr.c"
|
||||
INCLUDE_DIRS include
|
||||
REQUIRES lwip esp-tls)
|
||||
INCLUDE_DIRS "include"
|
||||
PRIVATE_INCLUDE_DIRS "private_include"
|
||||
REQUIRES lwip esp-tls)
|
||||
|
@ -1,4 +1,2 @@
|
||||
#
|
||||
# Component Makefile
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
||||
COMPONENT_ADD_INCLUDEDIRS := include
|
||||
COMPONENT_PRIV_INCLUDEDIRS := private_include
|
@ -22,7 +22,7 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct esp_transport_list_t* esp_transport_list_handle_t;
|
||||
typedef struct esp_transport_internal* 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);
|
||||
@ -33,6 +33,8 @@ typedef int (*poll_func)(esp_transport_handle_t t, int timeout_ms);
|
||||
typedef int (*connect_async_func)(esp_transport_handle_t t, const char *host, int port, int timeout_ms);
|
||||
typedef esp_transport_handle_t (*payload_transfer_func)(esp_transport_handle_t);
|
||||
|
||||
typedef struct esp_tls_last_error* esp_tls_error_handle_t;
|
||||
|
||||
/**
|
||||
* @brief Create transport list
|
||||
*
|
||||
@ -298,6 +300,21 @@ esp_err_t esp_transport_set_async_connect_func(esp_transport_handle_t t, connect
|
||||
*/
|
||||
esp_err_t esp_transport_set_parent_transport_func(esp_transport_handle_t t, payload_transfer_func _parent_transport);
|
||||
|
||||
/**
|
||||
* @brief Returns esp_tls error handle.
|
||||
* Warning: The returned pointer is valid only as long as esp_transport_handle_t exists. Once transport
|
||||
* handle gets destroyed, this value (esp_tls_error_handle_t) is freed automatically.
|
||||
*
|
||||
* @param[in] A transport handle
|
||||
*
|
||||
* @return
|
||||
* - valid pointer of esp_error_handle_t
|
||||
* - NULL if invalid transport handle
|
||||
*/
|
||||
esp_tls_error_handle_t esp_transport_get_error_handle(esp_transport_handle_t t);
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -0,0 +1,30 @@
|
||||
// Copyright 2015-2019 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_INTERNAL_H_
|
||||
#define _ESP_TRANSPORT_INTERNAL_H_
|
||||
|
||||
/**
|
||||
* @brief Sets error to common transport handle
|
||||
*
|
||||
* Note: This function copies the supplied error handle object to tcp_transport's internal
|
||||
* error handle object
|
||||
*
|
||||
* @param[in] A transport handle
|
||||
*
|
||||
*/
|
||||
void esp_transport_set_errors(esp_transport_handle_t t, const esp_tls_error_handle_t error_handle);
|
||||
|
||||
|
||||
#endif /* _ESP_TRANSPORT_INTERNAL_H_ */
|
@ -20,6 +20,15 @@
|
||||
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): %s", __FILE__, __LINE__, __FUNCTION__, "Memory exhausted"); \
|
||||
action; \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert milliseconds to timeval struct
|
||||
*
|
||||
@ -29,11 +38,6 @@ extern "C" {
|
||||
void esp_transport_utils_ms_to_timeval(int timeout_ms, struct timeval *tv);
|
||||
|
||||
|
||||
#define ESP_TRANSPORT_MEM_CHECK(TAG, a, action) if (!(a)) { \
|
||||
ESP_LOGE(TAG,"%s:%d (%s): %s", __FILE__, __LINE__, __FUNCTION__, "Memory exhausted"); \
|
||||
action; \
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@ -15,6 +15,7 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <esp_tls.h>
|
||||
|
||||
#include "sys/queue.h"
|
||||
#include "esp_log.h"
|
||||
@ -41,7 +42,8 @@ struct esp_transport_item_t {
|
||||
poll_func _poll_write; /*!< Poll and write */
|
||||
trans_func _destroy; /*!< Destroy and free transport */
|
||||
connect_async_func _connect_async; /*!< non-blocking connect function of this transport */
|
||||
payload_transfer_func _parent_transfer; /*!< Function returning underlying transport layer */
|
||||
payload_transfer_func _parent_transfer; /*!< Function returning underlying transport layer */
|
||||
esp_tls_error_handle_t error_handle; /*!< Pointer to esp-tls error handle */
|
||||
|
||||
STAILQ_ENTRY(esp_transport_item_t) next;
|
||||
};
|
||||
@ -52,6 +54,14 @@ struct esp_transport_item_t {
|
||||
*/
|
||||
STAILQ_HEAD(esp_transport_list_t, esp_transport_item_t);
|
||||
|
||||
/**
|
||||
* 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 */
|
||||
esp_tls_error_handle_t error_handle; /*!< Pointer to the error tracker if enabled */
|
||||
} esp_transport_internal_t;
|
||||
|
||||
static esp_transport_handle_t esp_transport_get_default_parent(esp_transport_handle_t t)
|
||||
{
|
||||
/*
|
||||
@ -62,34 +72,37 @@ static esp_transport_handle_t esp_transport_get_default_parent(esp_transport_han
|
||||
|
||||
esp_transport_list_handle_t esp_transport_list_init()
|
||||
{
|
||||
esp_transport_list_handle_t list = calloc(1, sizeof(struct esp_transport_list_t));
|
||||
ESP_TRANSPORT_MEM_CHECK(TAG, list, return NULL);
|
||||
STAILQ_INIT(list);
|
||||
return list;
|
||||
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->error_handle = calloc(1, sizeof(esp_tls_last_error_t));
|
||||
return transport;
|
||||
}
|
||||
|
||||
esp_err_t esp_transport_list_add(esp_transport_list_handle_t list, 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)
|
||||
{
|
||||
if (list == NULL || t == NULL) {
|
||||
if (h == NULL || t == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
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(list, t, next);
|
||||
STAILQ_INSERT_TAIL(&h->list, t, next);
|
||||
// Each transport in a list to share the same error tracker
|
||||
t->error_handle = h->error_handle;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_transport_handle_t esp_transport_list_get_transport(esp_transport_list_handle_t list, const char *scheme)
|
||||
esp_transport_handle_t esp_transport_list_get_transport(esp_transport_list_handle_t h, const char *scheme)
|
||||
{
|
||||
if (!list) {
|
||||
if (!h) {
|
||||
return NULL;
|
||||
}
|
||||
if (scheme == NULL) {
|
||||
return STAILQ_FIRST(list);
|
||||
return STAILQ_FIRST(&h->list);
|
||||
}
|
||||
esp_transport_handle_t item;
|
||||
STAILQ_FOREACH(item, list, next) {
|
||||
STAILQ_FOREACH(item, &h->list, next) {
|
||||
if (strcasecmp(item->scheme, scheme) == 0) {
|
||||
return item;
|
||||
}
|
||||
@ -97,23 +110,24 @@ esp_transport_handle_t esp_transport_list_get_transport(esp_transport_list_handl
|
||||
return NULL;
|
||||
}
|
||||
|
||||
esp_err_t esp_transport_list_destroy(esp_transport_list_handle_t list)
|
||||
esp_err_t esp_transport_list_destroy(esp_transport_list_handle_t h)
|
||||
{
|
||||
esp_transport_list_clean(list);
|
||||
free(list);
|
||||
esp_transport_list_clean(h);
|
||||
free(h->error_handle);
|
||||
free(h);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_transport_list_clean(esp_transport_list_handle_t list)
|
||||
esp_err_t esp_transport_list_clean(esp_transport_list_handle_t h)
|
||||
{
|
||||
esp_transport_handle_t item = STAILQ_FIRST(list);
|
||||
esp_transport_handle_t item = STAILQ_FIRST(&h->list);
|
||||
esp_transport_handle_t tmp;
|
||||
while (item != NULL) {
|
||||
tmp = STAILQ_NEXT(item, next);
|
||||
esp_transport_destroy(item);
|
||||
item = tmp;
|
||||
}
|
||||
STAILQ_INIT(list);
|
||||
STAILQ_INIT(&h->list);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@ -277,3 +291,18 @@ esp_err_t esp_transport_set_parent_transport_func(esp_transport_handle_t t, payl
|
||||
t->_parent_transfer = _parent_transport;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_tls_error_handle_t esp_transport_get_error_handle(esp_transport_handle_t t)
|
||||
{
|
||||
if (t) {
|
||||
return t->error_handle;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void esp_transport_set_errors(esp_transport_handle_t t, const esp_tls_error_handle_t error_handle)
|
||||
{
|
||||
if (t) {
|
||||
memcpy(t->error_handle, error_handle, sizeof(esp_tls_last_error_t));
|
||||
}
|
||||
}
|
@ -24,6 +24,7 @@
|
||||
#include "esp_transport.h"
|
||||
#include "esp_transport_ssl.h"
|
||||
#include "esp_transport_utils.h"
|
||||
#include "esp_transport_ssl_internal.h"
|
||||
|
||||
static const char *TAG = "TRANS_SSL";
|
||||
|
||||
@ -51,7 +52,7 @@ static int ssl_connect_async(esp_transport_handle_t t, const char *host, int por
|
||||
ssl->cfg.timeout_ms = timeout_ms;
|
||||
ssl->cfg.non_block = true;
|
||||
ssl->ssl_initialized = true;
|
||||
ssl->tls = calloc(1, sizeof(esp_tls_t));
|
||||
ssl->tls = esp_tls_init();
|
||||
if (!ssl->tls) {
|
||||
return -1;
|
||||
}
|
||||
@ -69,9 +70,12 @@ static int ssl_connect(esp_transport_handle_t t, const char *host, int port, int
|
||||
|
||||
ssl->cfg.timeout_ms = timeout_ms;
|
||||
ssl->ssl_initialized = true;
|
||||
ssl->tls = esp_tls_conn_new(host, strlen(host), port, &ssl->cfg);
|
||||
if (!ssl->tls) {
|
||||
ssl->tls = esp_tls_init();
|
||||
if (esp_tls_conn_new_sync(host, strlen(host), port, &ssl->cfg, ssl->tls) < 0) {
|
||||
ESP_LOGE(TAG, "Failed to open a new connection");
|
||||
esp_transport_set_errors(t, ssl->tls->error_handle);
|
||||
esp_tls_conn_delete(ssl->tls);
|
||||
ssl->tls = NULL;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
@ -112,7 +116,7 @@ static int ssl_write(esp_transport_handle_t t, const char *buffer, int len, int
|
||||
ret = esp_tls_conn_write(ssl->tls, (const unsigned char *) buffer, len);
|
||||
if (ret < 0) {
|
||||
ESP_LOGE(TAG, "esp_tls_conn_write error, errno=%s", strerror(errno));
|
||||
return -1;
|
||||
esp_transport_set_errors(t, ssl->tls->error_handle);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@ -130,7 +134,7 @@ static int ssl_read(esp_transport_handle_t t, char *buffer, int len, int timeout
|
||||
ret = esp_tls_conn_read(ssl->tls, (unsigned char *)buffer, len);
|
||||
if (ret < 0) {
|
||||
ESP_LOGE(TAG, "esp_tls_conn_read error, errno=%s", strerror(errno));
|
||||
return -1;
|
||||
esp_transport_set_errors(t, ssl->tls->error_handle);
|
||||
}
|
||||
if (ret == 0) {
|
||||
ret = -1;
|
||||
|
@ -45,8 +45,9 @@ def test_examples_protocol_esp_http_client(env, extra_data):
|
||||
dut1.expect(re.compile(r"HTTP Absolute path redirect Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTPS Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTP redirect to HTTPS Status = 200, content_length = (\d)"), timeout=10)
|
||||
dut1.expect(re.compile(r"HTTP chunk encoding Status = 200, content_length = -1"))
|
||||
dut1.expect(re.compile(r"HTTP chunk encoding Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"HTTP Stream reader Status = 200, content_length = (\d)"))
|
||||
dut1.expect(re.compile(r"Last esp error code: 0x8001"))
|
||||
dut1.expect("Finish http example")
|
||||
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
#include "esp_event.h"
|
||||
#include "tcpip_adapter.h"
|
||||
#include "protocol_examples_common.h"
|
||||
#include "esp_tls.h"
|
||||
|
||||
#include "esp_http_client.h"
|
||||
|
||||
@ -63,7 +64,13 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
|
||||
ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
|
||||
break;
|
||||
case HTTP_EVENT_DISCONNECTED:
|
||||
ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
|
||||
ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
|
||||
int mbedtls_err = 0;
|
||||
esp_err_t err = esp_tls_get_and_clear_last_error(evt->data, &mbedtls_err, NULL);
|
||||
if (err != 0) {
|
||||
ESP_LOGI(TAG, "Last esp error code: 0x%x", err);
|
||||
ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err);
|
||||
}
|
||||
break;
|
||||
}
|
||||
return ESP_OK;
|
||||
@ -485,6 +492,25 @@ static void https_async()
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
static void https_with_invalid_url()
|
||||
{
|
||||
esp_http_client_config_t config = {
|
||||
.url = "https://not.existent.url",
|
||||
.event_handler = _http_event_handler,
|
||||
};
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
esp_err_t err = esp_http_client_perform(client);
|
||||
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %d",
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
|
||||
}
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
|
||||
static void http_test_task(void *pvParameters)
|
||||
{
|
||||
@ -501,6 +527,7 @@ static void http_test_task(void *pvParameters)
|
||||
http_download_chunk();
|
||||
http_perform_as_stream_reader();
|
||||
https_async();
|
||||
https_with_invalid_url();
|
||||
|
||||
ESP_LOGI(TAG, "Finish http example");
|
||||
vTaskDelete(NULL);
|
||||
@ -522,6 +549,7 @@ void app_main()
|
||||
* examples/protocols/README.md for more information about this function.
|
||||
*/
|
||||
ESP_ERROR_CHECK(example_connect());
|
||||
ESP_LOGI(TAG, "Connected to AP, begin http example");
|
||||
|
||||
xTaskCreate(&http_test_task, "http_test_task", 8192, NULL, 5, NULL);
|
||||
}
|
||||
|
@ -29,6 +29,7 @@
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "mqtt_client.h"
|
||||
#include "esp_tls.h"
|
||||
|
||||
static const char *TAG = "MQTTS_EXAMPLE";
|
||||
|
||||
@ -79,6 +80,10 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
|
||||
break;
|
||||
case MQTT_EVENT_ERROR:
|
||||
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
|
||||
int mbedtls_err = 0;
|
||||
esp_err_t err = esp_tls_get_and_clear_last_error(event->error_handle, &mbedtls_err, NULL);
|
||||
ESP_LOGI(TAG, "Last esp error code: 0x%x", err);
|
||||
ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err);
|
||||
break;
|
||||
default:
|
||||
ESP_LOGI(TAG, "Other event id:%d", event->event_id);
|
||||
|
Loading…
x
Reference in New Issue
Block a user