diff --git a/components/esp_http_client/esp_http_client.c b/components/esp_http_client/esp_http_client.c index ccf5ab218b..1c4f7b2d8a 100644 --- a/components/esp_http_client/esp_http_client.c +++ b/components/esp_http_client/esp_http_client.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -15,6 +15,7 @@ #include "http_header.h" #include "esp_transport.h" #include "esp_transport_tcp.h" +#include "esp_transport_ssl.h" #include "http_utils.h" #include "http_auth.h" #include "sdkconfig.h" @@ -667,10 +668,26 @@ static bool init_common_tcp_transport(esp_http_client_handle_t client, const esp return true; } +static esp_err_t http_convert_addr_family_to_tls(esp_http_client_addr_type_t http_addr_family, esp_tls_addr_family_t *tls_addr_family) +{ + esp_err_t ret = ESP_OK; + if (http_addr_family == HTTP_ADDR_TYPE_UNSPEC) { + *tls_addr_family = ESP_TLS_AF_UNSPEC; + } else if (http_addr_family == HTTP_ADDR_TYPE_INET) { + *tls_addr_family = ESP_TLS_AF_INET; + } else if (http_addr_family == HTTP_ADDR_TYPE_INET6) { + *tls_addr_family = ESP_TLS_AF_INET6; + } else { + ret = ESP_ERR_INVALID_ARG; + } + return ret; +} + esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *config) { esp_http_client_handle_t client; + esp_tls_addr_family_t addr_family = ESP_TLS_AF_UNSPEC; esp_err_t ret = ESP_OK; esp_transport_handle_t tcp = NULL; char *host_name; @@ -704,6 +721,8 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co ESP_LOGE(TAG, "Error initialize transport"); goto error; } + ESP_GOTO_ON_ERROR(http_convert_addr_family_to_tls(config->addr_type, &addr_family), error, TAG, "Failed to convert addr type %d", config->addr_type); + esp_transport_ssl_set_addr_family(tcp, addr_family); ESP_GOTO_ON_FALSE(init_common_tcp_transport(client, config, tcp), ESP_FAIL, error, TAG, "Failed to set TCP config"); @@ -719,6 +738,7 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co ESP_LOGE(TAG, "Error initialize SSL Transport"); goto error; } + esp_transport_ssl_set_addr_family(ssl, addr_family); ESP_GOTO_ON_FALSE(init_common_tcp_transport(client, config, ssl), ESP_FAIL, error, TAG, "Failed to set SSL config"); diff --git a/components/esp_http_client/include/esp_http_client.h b/components/esp_http_client/include/esp_http_client.h index bb9319e42d..ea8e336eff 100644 --- a/components/esp_http_client/include/esp_http_client.h +++ b/components/esp_http_client/include/esp_http_client.h @@ -128,6 +128,15 @@ typedef enum { HTTP_AUTH_TYPE_DIGEST, /*!< HTTP Digest authentication */ } esp_http_client_auth_type_t; +/* +* @brief HTTP Address type +*/ +typedef enum { + HTTP_ADDR_TYPE_UNSPEC = AF_UNSPEC, /**< Unspecified address family. */ + HTTP_ADDR_TYPE_INET = AF_INET, /**< IPv4 address family. */ + HTTP_ADDR_TYPE_INET6 = AF_INET6, /**< IPv6 address family. */ +} esp_http_client_addr_type_t; + /** * @brief HTTP configuration */ @@ -189,6 +198,7 @@ typedef struct { #if CONFIG_ESP_HTTP_CLIENT_ENABLE_CUSTOM_TRANSPORT struct esp_transport_item_t *transport; #endif + esp_http_client_addr_type_t addr_type; /*!< Address type used in http client configurations */ } esp_http_client_config_t; /** diff --git a/components/tcp_transport/include/esp_transport_ssl.h b/components/tcp_transport/include/esp_transport_ssl.h index ca2355a11d..ad339396ff 100644 --- a/components/tcp_transport/include/esp_transport_ssl.h +++ b/components/tcp_transport/include/esp_transport_ssl.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -45,7 +45,7 @@ void esp_transport_ssl_set_cert_data(esp_transport_handle_t t, const char *data, void esp_transport_ssl_set_cert_data_der(esp_transport_handle_t t, const char *data, int len); /** - * @brief Enable the use of certification bundle for server verfication for + * @brief Enable the use of certification bundle for server verification for * an SSL connection. * It must be first enabled in menuconfig. * @@ -211,6 +211,14 @@ void esp_transport_ssl_set_keep_alive(esp_transport_handle_t t, esp_transport_ke */ void esp_transport_ssl_set_interface_name(esp_transport_handle_t t, struct ifreq *if_name); +/** + * @brief Set addr family of transport + * + * @param[in] t The transport handle + * @param[in] addr_family The addr family + */ +void esp_transport_ssl_set_addr_family(esp_transport_handle_t t, esp_tls_addr_family_t addr_family); + #ifdef CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS /** * @brief Session ticket operation diff --git a/components/tcp_transport/transport_ssl.c b/components/tcp_transport/transport_ssl.c index 9692385b2d..b9e9b38878 100644 --- a/components/tcp_transport/transport_ssl.c +++ b/components/tcp_transport/transport_ssl.c @@ -385,6 +385,12 @@ void esp_transport_ssl_set_client_cert_data(esp_transport_handle_t t, const char ssl->cfg.clientcert_pem_bytes = len + 1; } +void esp_transport_ssl_set_addr_family(esp_transport_handle_t t, esp_tls_addr_family_t addr_family) +{ + GET_SSL_FROM_TRANSPORT_OR_RETURN(ssl, t); + ssl->cfg.addr_family = addr_family; +} + #ifdef CONFIG_MBEDTLS_HARDWARE_ECDSA_SIGN void esp_transport_ssl_set_client_key_ecdsa_peripheral(esp_transport_handle_t t, uint8_t ecdsa_efuse_blk) {