tcp_transport: close to return error only for socket error or invalid args

Closes https://github.com/espressif/esp-idf/issues/4872
This commit is contained in:
David Cermak
2020-04-28 12:16:03 +02:00
parent 0c7204e934
commit 6182e6f303
4 changed files with 36 additions and 8 deletions

View File

@@ -96,15 +96,23 @@ static ssize_t tcp_write(esp_tls_t *tls, const char *data, size_t datalen)
* @brief Close the TLS connection and free any allocated resources. * @brief Close the TLS connection and free any allocated resources.
*/ */
void esp_tls_conn_delete(esp_tls_t *tls) void esp_tls_conn_delete(esp_tls_t *tls)
{
esp_tls_conn_destroy(tls);
}
int esp_tls_conn_destroy(esp_tls_t *tls)
{ {
if (tls != NULL) { if (tls != NULL) {
int ret = 0;
_esp_tls_conn_delete(tls); _esp_tls_conn_delete(tls);
if (tls->sockfd >= 0) { if (tls->sockfd >= 0) {
close(tls->sockfd); ret = close(tls->sockfd);
} }
free(tls->error_handle); free(tls->error_handle);
free(tls); free(tls);
return ret;
} }
return -1; // invalid argument
} }
esp_tls_t *esp_tls_init(void) esp_tls_t *esp_tls_init(void)

View File

@@ -463,6 +463,15 @@ static inline ssize_t esp_tls_conn_read(esp_tls_t *tls, void *data, size_t data
return tls->read(tls, (char *)data, datalen); return tls->read(tls, (char *)data, datalen);
} }
/**
* @brief Compatible version of esp_tls_conn_destroy() to close the TLS/SSL connection
*
* @note This API will be removed in IDFv5.0
*
* @param[in] tls pointer to esp-tls as esp-tls handle.
*/
void esp_tls_conn_delete(esp_tls_t *tls);
/** /**
* @brief Close the TLS/SSL connection and free any allocated resources. * @brief Close the TLS/SSL connection and free any allocated resources.
* *
@@ -470,8 +479,11 @@ static inline ssize_t esp_tls_conn_read(esp_tls_t *tls, void *data, size_t data
* esp_tls_conn_http_new() APIs. * esp_tls_conn_http_new() APIs.
* *
* @param[in] tls pointer to esp-tls as esp-tls handle. * @param[in] tls pointer to esp-tls as esp-tls handle.
*
* @return - 0 on success
* - -1 if socket error or an invalid argument
*/ */
void esp_tls_conn_delete(esp_tls_t *tls); int esp_tls_conn_destroy(esp_tls_t *tls);
/** /**
* @brief Return the number of application data bytes remaining to be * @brief Return the number of application data bytes remaining to be

View File

@@ -74,7 +74,7 @@ static int ssl_connect(esp_transport_handle_t t, const char *host, int port, int
if (esp_tls_conn_new_sync(host, strlen(host), port, &ssl->cfg, ssl->tls) <= 0) { 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_LOGE(TAG, "Failed to open a new connection");
esp_transport_set_errors(t, ssl->tls->error_handle); esp_transport_set_errors(t, ssl->tls->error_handle);
esp_tls_conn_delete(ssl->tls); esp_tls_conn_destroy(ssl->tls);
ssl->tls = NULL; ssl->tls = NULL;
return -1; return -1;
} }
@@ -170,7 +170,7 @@ static int ssl_close(esp_transport_handle_t t)
int ret = -1; int ret = -1;
transport_ssl_t *ssl = esp_transport_get_context_data(t); transport_ssl_t *ssl = esp_transport_get_context_data(t);
if (ssl->ssl_initialized) { if (ssl->ssl_initialized) {
esp_tls_conn_delete(ssl->tls); ret = esp_tls_conn_destroy(ssl->tls);
ssl->conn_state = TRANS_SSL_INIT; ssl->conn_state = TRANS_SSL_INIT;
ssl->ssl_initialized = false; ssl->ssl_initialized = false;
} }

View File

@@ -83,7 +83,11 @@ static int tcp_connect(esp_transport_handle_t t, const char *host, int port, int
// Set socket to non-blocking // Set socket to non-blocking
int flags; int flags;
if ((flags = fcntl(tcp->sock, F_GETFL, NULL)) < 0 || fcntl(tcp->sock, F_SETFL, flags |= O_NONBLOCK) < 0) { if ((flags = fcntl(tcp->sock, F_GETFL, NULL)) < 0) {
ESP_LOGE(TAG, "[sock=%d] get file flags error: %s", tcp->sock, strerror(errno));
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)); ESP_LOGE(TAG, "[sock=%d] set nonblocking error: %s", tcp->sock, strerror(errno));
goto error; goto error;
} }
@@ -126,7 +130,11 @@ static int tcp_connect(esp_transport_handle_t t, const char *host, int port, int
} }
} }
// Reset socket to blocking // Reset socket to blocking
if ((flags = fcntl(tcp->sock, F_GETFL, NULL)) < 0 || fcntl(tcp->sock, F_SETFL, flags & ~O_NONBLOCK) < 0) { if ((flags = fcntl(tcp->sock, F_GETFL, NULL)) < 0) {
ESP_LOGE(TAG, "[sock=%d] get file flags error: %s", tcp->sock, strerror(errno));
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)); ESP_LOGE(TAG, "[sock=%d] reset blocking error: %s", tcp->sock, strerror(errno));
goto error; goto error;
} }