transport: Support bind socket to specified interface in transport and esp-tls

This commit is contained in:
yuanjm 2021-01-19 17:49:42 +08:00 committed by bot
parent e39b475af1
commit c62cbd1254
5 changed files with 41 additions and 1 deletions

View File

@ -231,6 +231,15 @@ static esp_err_t esp_tls_set_socket_options(int fd, const esp_tls_cfg_t *cfg)
return ESP_ERR_ESP_TLS_SOCKET_SETOPT_FAILED;
}
}
if (cfg->if_name) {
if (cfg->if_name->ifr_name[0] != 0) {
ESP_LOGD(TAG, "Bind [sock=%d] to interface %s", fd, cfg->if_name->ifr_name);
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, cfg->if_name, sizeof(struct ifreq)) != 0) {
ESP_LOGE(TAG, "Bind [sock=%d] to interface %s fail", fd, cfg->if_name->ifr_name);
return ESP_ERR_ESP_TLS_SOCKET_SETOPT_FAILED;
}
}
}
}
return ESP_OK;
}
@ -266,7 +275,7 @@ static esp_err_t esp_tcp_connect(const char *host, int hostlen, int port, int *s
return ret;
}
// Set timeout options and keep-alive options if configured
// Set timeout options, keep-alive options and bind device options if configured
ret = esp_tls_set_socket_options(fd, cfg);
if (ret != ESP_OK) {
goto err;

View File

@ -172,6 +172,7 @@ typedef struct esp_tls_cfg {
void *ds_data; /*!< Pointer for digital signature peripheral context */
bool is_plain_tcp; /*!< Use non-TLS connection: When set to true, the esp-tls uses
plain TCP transport rather then TLS/SSL connection */
struct ifreq *if_name; /*!< The name of interface for data to go through. Use the default interface without setting */
} esp_tls_cfg_t;
#ifdef CONFIG_ESP_TLS_SERVER

View File

@ -173,6 +173,15 @@ void esp_transport_ssl_set_psk_key_hint(esp_transport_handle_t t, const psk_hint
*/
void esp_transport_ssl_set_keep_alive(esp_transport_handle_t t, esp_transport_keep_alive_t *keep_alive_cfg);
/**
* @brief Set name of interface that socket can be binded on
* So the data can transport on this interface
*
* @param[in] t The transport handle
* @param[in] if_name The interface name
*/
void esp_transport_ssl_set_interface_name(esp_transport_handle_t t, struct ifreq *if_name);
#ifdef __cplusplus
}
#endif

View File

@ -16,6 +16,7 @@
#define _ESP_TRANSPORT_TCP_H_
#include "esp_transport.h"
#include <sys/socket.h>
#ifdef __cplusplus
extern "C" {
@ -30,6 +31,15 @@ extern "C" {
*/
void esp_transport_tcp_set_keep_alive(esp_transport_handle_t t, esp_transport_keep_alive_t *keep_alive_cfg);
/**
* @brief Set name of interface that socket can be binded on
* So the data can transport on this interface
*
* @param[in] t The transport handle
* @param[in] if_name The interface name
*/
void esp_transport_tcp_set_interface_name(esp_transport_handle_t t, struct ifreq *if_name);
/**
* @brief Create TCP transport, the transport handle must be release esp_transport_destroy callback
*

View File

@ -345,6 +345,12 @@ void esp_transport_ssl_set_keep_alive(esp_transport_handle_t t, esp_transport_ke
ssl->cfg.keep_alive_cfg = (tls_keep_alive_cfg_t *) keep_alive_cfg;
}
void esp_transport_ssl_set_interface_name(esp_transport_handle_t t, struct ifreq *if_name)
{
GET_SSL_FROM_TRANSPORT_OR_RETURN(ssl, t);
ssl->cfg.if_name = if_name;
}
esp_transport_handle_t esp_transport_ssl_init(void)
{
esp_transport_handle_t t = esp_transport_init();
@ -378,3 +384,8 @@ void esp_transport_tcp_set_keep_alive(esp_transport_handle_t t, esp_transport_ke
{
return esp_transport_ssl_set_keep_alive(t, keep_alive_cfg);
}
void esp_transport_tcp_set_interface_name(esp_transport_handle_t t, struct ifreq *if_name)
{
return esp_transport_ssl_set_interface_name(t, if_name);
}