mirror of
https://github.com/espressif/esp-idf.git
synced 2025-09-30 19:19:21 +00:00
tcp_transport: Added internal API for underlying socket, used for custom select on connection end for WS
Internal tcp_transport functions could now use custom socket operations. This is used for WebSocket transport, when we typically wait for clean connection closure, i.e. selecting for read/error with expected errno or recv size=0 while socket readable (=connection terminated by FIN flag)
This commit is contained in:
@@ -310,7 +310,7 @@ esp_err_t esp_transport_set_parent_transport_func(esp_transport_handle_t t, payl
|
||||
* @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);
|
||||
|
||||
|
||||
|
@@ -117,6 +117,21 @@ ws_transport_opcodes_t esp_transport_ws_get_read_opcode(esp_transport_handle_t t
|
||||
*/
|
||||
int esp_transport_ws_get_read_payload_len(esp_transport_handle_t t);
|
||||
|
||||
/**
|
||||
* @brief Polls the active connection for termination
|
||||
*
|
||||
* This API is typically used by the client to wait for clean connection closure
|
||||
* by websocket server
|
||||
*
|
||||
* @param t Websocket transport handle
|
||||
* @param[in] timeout_ms The timeout milliseconds
|
||||
*
|
||||
* @return
|
||||
* 0 - no activity on read and error socket descriptor within timeout
|
||||
* 1 - Success: either connection terminated by FIN or the most common RST err codes
|
||||
* -1 - Failure: Unexpected error code or socket is normally readable
|
||||
*/
|
||||
int esp_transport_ws_poll_connection_closed(esp_transport_handle_t t, int timeout_ms);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@@ -0,0 +1,56 @@
|
||||
// Copyright 2020 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_
|
||||
|
||||
#include "esp_transport.h"
|
||||
#include "sys/queue.h"
|
||||
|
||||
typedef int (*get_socket_func)(esp_transport_handle_t t);
|
||||
|
||||
/**
|
||||
* Transport layer structure, which will provide functions, basic properties for transport types
|
||||
*/
|
||||
struct esp_transport_item_t {
|
||||
int port;
|
||||
char *scheme; /*!< Tag name */
|
||||
void *data; /*!< Additional transport data */
|
||||
connect_func _connect; /*!< Connect function of this transport */
|
||||
io_read_func _read; /*!< Read */
|
||||
io_func _write; /*!< Write */
|
||||
trans_func _close; /*!< Close */
|
||||
poll_func _poll_read; /*!< Poll and read */
|
||||
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 */
|
||||
get_socket_func _get_socket;
|
||||
esp_tls_error_handle_t error_handle; /*!< Pointer to esp-tls error handle */
|
||||
|
||||
STAILQ_ENTRY(esp_transport_item_t) next;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Returns underlying socket for the supplied transport handle
|
||||
*
|
||||
* @param t Transport handle
|
||||
*
|
||||
* @return Socket file descriptor in case of success
|
||||
* -1 in case of error
|
||||
*/
|
||||
int esp_transport_get_socket(esp_transport_handle_t t);
|
||||
|
||||
|
||||
#endif //_ESP_TRANSPORT_INTERNAL_H_
|
@@ -12,8 +12,8 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef _ESP_TRANSPORT_INTERNAL_H_
|
||||
#define _ESP_TRANSPORT_INTERNAL_H_
|
||||
#ifndef _ESP_TRANSPORT_SSL_INTERNAL_H_
|
||||
#define _ESP_TRANSPORT_SSL_INTERNAL_H_
|
||||
|
||||
/**
|
||||
* @brief Sets error to common transport handle
|
||||
@@ -27,4 +27,4 @@
|
||||
void esp_transport_set_errors(esp_transport_handle_t t, const esp_tls_error_handle_t error_handle);
|
||||
|
||||
|
||||
#endif /* _ESP_TRANSPORT_INTERNAL_H_ */
|
||||
#endif /* _ESP_TRANSPORT_SSL_INTERNAL_H_ */
|
||||
|
@@ -21,32 +21,11 @@
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "esp_transport.h"
|
||||
#include "esp_transport_internal.h"
|
||||
#include "esp_transport_utils.h"
|
||||
|
||||
static const char *TAG = "TRANSPORT";
|
||||
|
||||
/**
|
||||
* Transport layer structure, which will provide functions, basic properties for transport types
|
||||
*/
|
||||
struct esp_transport_item_t {
|
||||
int port;
|
||||
int socket; /*!< Socket to use in this transport */
|
||||
char *scheme; /*!< Tag name */
|
||||
void *context; /*!< Context data */
|
||||
void *data; /*!< Additional transport data */
|
||||
connect_func _connect; /*!< Connect function of this transport */
|
||||
io_read_func _read; /*!< Read */
|
||||
io_func _write; /*!< Write */
|
||||
trans_func _close; /*!< Close */
|
||||
poll_func _poll_read; /*!< Poll and read */
|
||||
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 */
|
||||
esp_tls_error_handle_t error_handle; /*!< Pointer to esp-tls error handle */
|
||||
|
||||
STAILQ_ENTRY(esp_transport_item_t) next;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
@@ -305,4 +284,12 @@ void esp_transport_set_errors(esp_transport_handle_t t, const esp_tls_error_hand
|
||||
if (t) {
|
||||
memcpy(t->error_handle, error_handle, sizeof(esp_tls_last_error_t));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int esp_transport_get_socket(esp_transport_handle_t t)
|
||||
{
|
||||
if (t && t->_get_socket) {
|
||||
return t->_get_socket(t);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
@@ -25,6 +25,7 @@
|
||||
#include "esp_transport_ssl.h"
|
||||
#include "esp_transport_utils.h"
|
||||
#include "esp_transport_ssl_internal.h"
|
||||
#include "esp_transport_internal.h"
|
||||
|
||||
static const char *TAG = "TRANS_SSL";
|
||||
|
||||
@@ -288,6 +289,17 @@ void esp_transport_ssl_use_secure_element(esp_transport_handle_t t)
|
||||
}
|
||||
}
|
||||
|
||||
static int ssl_get_socket(esp_transport_handle_t t)
|
||||
{
|
||||
if (t) {
|
||||
transport_ssl_t *ssl = t->data;
|
||||
if (ssl && ssl->tls) {
|
||||
return ssl->tls->sockfd;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
esp_transport_handle_t esp_transport_ssl_init(void)
|
||||
{
|
||||
esp_transport_handle_t t = esp_transport_init();
|
||||
@@ -296,6 +308,7 @@ esp_transport_handle_t esp_transport_ssl_init(void)
|
||||
esp_transport_set_context_data(t, ssl);
|
||||
esp_transport_set_func(t, ssl_connect, ssl_read, ssl_write, ssl_close, ssl_poll_read, ssl_poll_write, ssl_destroy);
|
||||
esp_transport_set_async_connect_func(t, ssl_connect_async);
|
||||
t->_get_socket = ssl_get_socket;
|
||||
return t;
|
||||
}
|
||||
|
||||
|
@@ -25,6 +25,7 @@
|
||||
|
||||
#include "esp_transport_utils.h"
|
||||
#include "esp_transport.h"
|
||||
#include "esp_transport_internal.h"
|
||||
|
||||
static const char *TAG = "TRANS_TCP";
|
||||
|
||||
@@ -234,6 +235,17 @@ static esp_err_t tcp_destroy(esp_transport_handle_t t)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tcp_get_socket(esp_transport_handle_t t)
|
||||
{
|
||||
if (t) {
|
||||
transport_tcp_t *tcp = t->data;
|
||||
if (tcp) {
|
||||
return tcp->sock;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
esp_transport_handle_t esp_transport_tcp_init(void)
|
||||
{
|
||||
esp_transport_handle_t t = esp_transport_init();
|
||||
@@ -242,6 +254,7 @@ esp_transport_handle_t esp_transport_tcp_init(void)
|
||||
tcp->sock = -1;
|
||||
esp_transport_set_func(t, tcp_connect, tcp_read, tcp_write, tcp_close, tcp_poll_read, tcp_poll_write, tcp_destroy);
|
||||
esp_transport_set_context_data(t, tcp);
|
||||
t->_get_socket = tcp_get_socket;
|
||||
|
||||
return t;
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <sys/random.h>
|
||||
#include <sys/socket.h>
|
||||
#include "esp_log.h"
|
||||
#include "esp_transport.h"
|
||||
#include "esp_transport_tcp.h"
|
||||
@@ -9,6 +10,8 @@
|
||||
#include "esp_transport_utils.h"
|
||||
#include "mbedtls/base64.h"
|
||||
#include "mbedtls/sha1.h"
|
||||
#include "esp_transport_internal.h"
|
||||
#include "errno.h"
|
||||
|
||||
static const char *TAG = "TRANSPORT_WS";
|
||||
|
||||
@@ -449,6 +452,17 @@ void esp_transport_ws_set_path(esp_transport_handle_t t, const char *path)
|
||||
strcpy(ws->path, path);
|
||||
}
|
||||
|
||||
static int ws_get_socket(esp_transport_handle_t t)
|
||||
{
|
||||
if (t) {
|
||||
transport_ws_t *ws = t->data;
|
||||
if (ws && ws->parent && ws->parent->_get_socket) {
|
||||
return ws->parent->_get_socket(ws->parent);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
esp_transport_handle_t esp_transport_ws_init(esp_transport_handle_t parent_handle)
|
||||
{
|
||||
esp_transport_handle_t t = esp_transport_init();
|
||||
@@ -473,6 +487,7 @@ esp_transport_handle_t esp_transport_ws_init(esp_transport_handle_t parent_handl
|
||||
esp_transport_set_parent_transport_func(t, ws_get_payload_transport_handle);
|
||||
|
||||
esp_transport_set_context_data(t, ws);
|
||||
t->_get_socket = ws_get_socket;
|
||||
return t;
|
||||
}
|
||||
|
||||
@@ -548,4 +563,41 @@ int esp_transport_ws_get_read_payload_len(esp_transport_handle_t t)
|
||||
return ws->frame_state.payload_len;
|
||||
}
|
||||
|
||||
int esp_transport_ws_poll_connection_closed(esp_transport_handle_t t, int timeout_ms)
|
||||
{
|
||||
struct timeval timeout;
|
||||
int sock = esp_transport_get_socket(t);
|
||||
fd_set readset;
|
||||
fd_set errset;
|
||||
FD_ZERO(&readset);
|
||||
FD_ZERO(&errset);
|
||||
FD_SET(sock, &readset);
|
||||
FD_SET(sock, &errset);
|
||||
|
||||
int ret = select(sock + 1, &readset, NULL, &errset, esp_transport_utils_ms_to_timeval(timeout_ms, &timeout));
|
||||
if (ret > 0) {
|
||||
if (FD_ISSET(sock, &readset)) {
|
||||
uint8_t buffer;
|
||||
if (recv(sock, &buffer, 1, MSG_PEEK) <= 0) {
|
||||
// socket is readable, but reads zero bytes -- connection cleanly closed by FIN flag
|
||||
return 1;
|
||||
}
|
||||
ESP_LOGW(TAG, "esp_transport_ws_poll_connection_closed: unexpected data readable on socket=%d", sock);
|
||||
} else if (FD_ISSET(sock, &errset)) {
|
||||
int sock_errno = 0;
|
||||
uint32_t optlen = sizeof(sock_errno);
|
||||
getsockopt(sock, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen);
|
||||
ESP_LOGD(TAG, "esp_transport_ws_poll_connection_closed select error %d, errno = %s, fd = %d", sock_errno, strerror(sock_errno), sock);
|
||||
if (sock_errno == ENOTCONN || sock_errno == ECONNRESET || sock_errno == ECONNABORTED) {
|
||||
// the three err codes above might be caused by connection termination by RTS flag
|
||||
// which we still assume as expected closing sequence of ws-transport connection
|
||||
return 1;
|
||||
}
|
||||
ESP_LOGE(TAG, "esp_transport_ws_poll_connection_closed: unexpected errno=%d on socket=%d", sock_errno, sock);
|
||||
}
|
||||
return -1; // indicates error: socket unexpectedly reads an actual data, or unexpected errno code
|
||||
}
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user