mirror of
https://github.com/espressif/esp-idf.git
synced 2025-09-30 19:19:21 +00:00
tcp_transport: Add TCP transport connection errors
Transport's error_handle is used to capture different types of errors including esp_err_t from esp-tls and socket/system errors. This change adds the following error codes for TCP transport: * connection closed by a FIN flag (clean closure from server) * DNS resolution problem * connection timeout These errors are already defined in esp-tls component and since this component will be used in the future for both TCP and SSL transport, we currently report these issues in transport::error_handle::esp_tls_last_error of standard esp error type (esp_err_t) Closes https://github.com/espressif/esp-mqtt/issues/182
This commit is contained in:
@@ -44,6 +44,29 @@ struct esp_transport_item_t {
|
||||
STAILQ_ENTRY(esp_transport_item_t) next;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Internal error types for TCP connection issues not covered in socket's errno
|
||||
*/
|
||||
enum tcp_transport_errors {
|
||||
ERR_TCP_TRANSPORT_CONNECTION_TIMEOUT,
|
||||
ERR_TCP_TRANSPORT_CANNOT_RESOLVE_HOSTNAME,
|
||||
ERR_TCP_TRANSPORT_CONNECTION_CLOSED_BY_FIN,
|
||||
ERR_TCP_TRANSPORT_CONNECTION_FAILED,
|
||||
ERR_TCP_TRANSPORT_SETOPT_FAILED,
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Captures internal tcp connection error
|
||||
*
|
||||
* This is internally translated to esp-tls return codes of esp_err_t type, since the esp-tls
|
||||
* will be used as TCP transport layer
|
||||
*
|
||||
* @param[in] t The transport handle
|
||||
* @param[in] error Internal tcp-transport's error
|
||||
*
|
||||
*/
|
||||
void capture_tcp_transport_error(esp_transport_handle_t t, enum tcp_transport_errors error);
|
||||
|
||||
/**
|
||||
* @brief Returns underlying socket for the supplied transport handle
|
||||
*
|
||||
|
@@ -23,6 +23,7 @@
|
||||
#include "esp_transport.h"
|
||||
#include "esp_transport_internal.h"
|
||||
#include "esp_transport_utils.h"
|
||||
#include "esp_tls_errors.h"
|
||||
|
||||
static const char *TAG = "TRANSPORT";
|
||||
|
||||
@@ -298,6 +299,28 @@ int esp_transport_get_errno(esp_transport_handle_t t)
|
||||
return -1;
|
||||
}
|
||||
|
||||
void capture_tcp_transport_error(esp_transport_handle_t t, enum tcp_transport_errors error)
|
||||
{
|
||||
esp_tls_last_error_t *err_handle = esp_transport_get_error_handle(t);
|
||||
switch (error) {
|
||||
case ERR_TCP_TRANSPORT_CONNECTION_TIMEOUT:
|
||||
err_handle->last_error = ESP_ERR_ESP_TLS_CONNECTION_TIMEOUT;
|
||||
break;
|
||||
case ERR_TCP_TRANSPORT_CANNOT_RESOLVE_HOSTNAME:
|
||||
err_handle->last_error = ESP_ERR_ESP_TLS_CANNOT_RESOLVE_HOSTNAME;
|
||||
break;
|
||||
case ERR_TCP_TRANSPORT_CONNECTION_CLOSED_BY_FIN:
|
||||
err_handle->last_error = ESP_ERR_ESP_TLS_TCP_CLOSED_FIN;
|
||||
break;
|
||||
case ERR_TCP_TRANSPORT_CONNECTION_FAILED:
|
||||
err_handle->last_error = ESP_ERR_ESP_TLS_FAILED_CONNECT_TO_HOST;
|
||||
break;
|
||||
case ERR_TCP_TRANSPORT_SETOPT_FAILED:
|
||||
err_handle->last_error = ESP_ERR_ESP_TLS_SOCKET_SETOPT_FAILED;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void esp_transport_set_errors(esp_transport_handle_t t, const esp_tls_error_handle_t error_handle)
|
||||
{
|
||||
if (t && t->error_handle) {
|
||||
|
@@ -166,6 +166,10 @@ static int ssl_read(esp_transport_handle_t t, char *buffer, int len, int timeout
|
||||
esp_transport_set_errors(t, ssl->tls->error_handle);
|
||||
}
|
||||
if (ret == 0) {
|
||||
if (poll > 0) {
|
||||
// no error, socket reads 0 while previously detected as readable -> connection has been closed cleanly
|
||||
capture_tcp_transport_error(t, ERR_TCP_TRANSPORT_CONNECTION_CLOSED_BY_FIN);
|
||||
}
|
||||
ret = -1;
|
||||
}
|
||||
return ret;
|
||||
|
@@ -26,6 +26,7 @@
|
||||
#include "esp_transport_utils.h"
|
||||
#include "esp_transport.h"
|
||||
#include "esp_transport_internal.h"
|
||||
#include "esp_tls_errors.h"
|
||||
|
||||
static const char *TAG = "TRANS_TCP";
|
||||
|
||||
@@ -33,6 +34,7 @@ typedef struct {
|
||||
int sock;
|
||||
} transport_tcp_t;
|
||||
|
||||
|
||||
static int resolve_dns(const char *host, struct sockaddr_in *ip)
|
||||
{
|
||||
const struct addrinfo hints = {
|
||||
@@ -91,6 +93,7 @@ static int tcp_connect(esp_transport_handle_t t, const char *host, int port, int
|
||||
//if stream_host is not ip address, resolve it AF_INET,servername,&serveraddr.sin_addr
|
||||
if (inet_pton(AF_INET, host, &remote_ip.sin_addr) != 1) {
|
||||
if (resolve_dns(host, &remote_ip) < 0) {
|
||||
capture_tcp_transport_error(t, ERR_TCP_TRANSPORT_CANNOT_RESOLVE_HOSTNAME);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -120,10 +123,12 @@ static int tcp_connect(esp_transport_handle_t t, const char *host, int port, int
|
||||
int flags;
|
||||
if ((flags = fcntl(tcp->sock, F_GETFL, NULL)) < 0) {
|
||||
ESP_LOGE(TAG, "[sock=%d] get file flags error: %s", tcp->sock, strerror(errno));
|
||||
capture_tcp_transport_error(t, ERR_TCP_TRANSPORT_SETOPT_FAILED);
|
||||
goto error;
|
||||
}
|
||||
if (fcntl(tcp->sock, F_SETFL, flags |= O_NONBLOCK) < 0) {
|
||||
ESP_LOGE(TAG, "[sock=%d] set nonblocking error: %s", tcp->sock, strerror(errno));
|
||||
capture_tcp_transport_error(t, ERR_TCP_TRANSPORT_SETOPT_FAILED);
|
||||
goto error;
|
||||
}
|
||||
|
||||
@@ -146,7 +151,7 @@ static int tcp_connect(esp_transport_handle_t t, const char *host, int port, int
|
||||
}
|
||||
else if (res == 0) {
|
||||
ESP_LOGE(TAG, "[sock=%d] select() timeout", tcp->sock);
|
||||
esp_transport_capture_errno(t, EINPROGRESS); // errno=EINPROGRESS indicates connection timeout
|
||||
capture_tcp_transport_error(t, ERR_TCP_TRANSPORT_CONNECTION_TIMEOUT);
|
||||
goto error;
|
||||
} else {
|
||||
int sockerr;
|
||||
@@ -154,11 +159,13 @@ static int tcp_connect(esp_transport_handle_t t, const char *host, int port, int
|
||||
|
||||
if (getsockopt(tcp->sock, SOL_SOCKET, SO_ERROR, (void*)(&sockerr), &len) < 0) {
|
||||
ESP_LOGE(TAG, "[sock=%d] getsockopt() error: %s", tcp->sock, strerror(errno));
|
||||
capture_tcp_transport_error(t, ERR_TCP_TRANSPORT_SETOPT_FAILED);
|
||||
goto error;
|
||||
}
|
||||
else if (sockerr) {
|
||||
esp_transport_capture_errno(t, sockerr);
|
||||
ESP_LOGE(TAG, "[sock=%d] delayed connect error: %s", tcp->sock, strerror(sockerr));
|
||||
capture_tcp_transport_error(t, ERR_TCP_TRANSPORT_CONNECTION_FAILED);
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
@@ -170,10 +177,12 @@ static int tcp_connect(esp_transport_handle_t t, const char *host, int port, int
|
||||
// Reset socket to blocking
|
||||
if ((flags = fcntl(tcp->sock, F_GETFL, NULL)) < 0) {
|
||||
ESP_LOGE(TAG, "[sock=%d] get file flags error: %s", tcp->sock, strerror(errno));
|
||||
capture_tcp_transport_error(t, ERR_TCP_TRANSPORT_SETOPT_FAILED);
|
||||
goto error;
|
||||
}
|
||||
if (fcntl(tcp->sock, F_SETFL, flags & ~O_NONBLOCK) < 0) {
|
||||
ESP_LOGE(TAG, "[sock=%d] reset blocking error: %s", tcp->sock, strerror(errno));
|
||||
capture_tcp_transport_error(t, ERR_TCP_TRANSPORT_SETOPT_FAILED);
|
||||
goto error;
|
||||
}
|
||||
return tcp->sock;
|
||||
@@ -202,6 +211,10 @@ static int tcp_read(esp_transport_handle_t t, char *buffer, int len, int timeout
|
||||
}
|
||||
int read_len = read(tcp->sock, buffer, len);
|
||||
if (read_len == 0) {
|
||||
if (poll > 0) {
|
||||
// no error, socket reads 0 while previously detected as readable -> connection has been closed cleanly
|
||||
capture_tcp_transport_error(t, ERR_TCP_TRANSPORT_CONNECTION_CLOSED_BY_FIN);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
return read_len;
|
||||
|
Reference in New Issue
Block a user