feat(tcp_transport): add an api to configure the addr family

This commit is contained in:
Xu Si Yu
2024-12-19 12:28:49 +08:00
parent 5ad7de7154
commit 4be36fdb8b
4 changed files with 47 additions and 3 deletions
+21 -1
View File
@@ -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"
@@ -739,10 +740,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;
@@ -776,6 +793,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");
@@ -791,6 +810,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");
@@ -129,6 +129,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
*/
@@ -194,6 +203,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;
/**