From 864c59c091f0711d0f7062be919d1e8aae5ec13e Mon Sep 17 00:00:00 2001 From: Laukik Hase Date: Thu, 24 Mar 2022 12:36:00 +0530 Subject: [PATCH] esp_tls: Remove deprecated API - Removed deprecated esp_tls_conn_new() viz. duplicated by esp_tls_conn_new_sync() - Removed deprecated esp_tls_conn_delete() - Marked esp_tls_conn_http_new() as deprecated, added alternative esp_tls_conn_http_new_sync() (similar to esp_tls_conn_http_new_async()) --- components/esp-tls/esp_tls.c | 53 +++++++++-------------------- components/esp-tls/esp_tls.h | 64 +++++++++++++++--------------------- 2 files changed, 42 insertions(+), 75 deletions(-) diff --git a/components/esp-tls/esp_tls.c b/components/esp-tls/esp_tls.c index ee57e0c792..386ecb0883 100644 --- a/components/esp-tls/esp_tls.c +++ b/components/esp-tls/esp_tls.c @@ -441,43 +441,8 @@ esp_err_t esp_tls_plain_tcp_connect(const char *host, int hostlen, int port, con return tcp_connect(host, hostlen, port, cfg, error_handle, sockfd); } -/** - * @brief Create a new TLS/SSL connection - */ -esp_tls_t *esp_tls_conn_new(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg) -{ - esp_tls_t *tls = esp_tls_init(); - if (!tls) { - return NULL; - } - /* esp_tls_conn_new() API establishes connection in a blocking manner thus this loop ensures that esp_tls_conn_new() - API returns only after connection is established unless there is an error*/ - size_t start = xTaskGetTickCount(); - while (1) { - int ret = esp_tls_low_level_conn(hostname, hostlen, port, cfg, tls); - if (ret == 1) { - return tls; - } else if (ret == -1) { - esp_tls_conn_destroy(tls); - ESP_LOGE(TAG, "Failed to open new connection"); - return NULL; - } else if (ret == 0 && cfg->timeout_ms >= 0) { - size_t timeout_ticks = pdMS_TO_TICKS(cfg->timeout_ms); - uint32_t expired = xTaskGetTickCount() - start; - if (expired >= timeout_ticks) { - esp_tls_conn_destroy(tls); - ESP_LOGE(TAG, "Failed to open new connection in specified timeout"); - return NULL; - } - } - } - return NULL; -} - int esp_tls_conn_new_sync(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls) { - /* esp_tls_conn_new_sync() is a sync alternative to esp_tls_conn_new_async() with symmetric function prototype - it is an alternative to esp_tls_conn_new() which is left for compatibility reasons */ size_t start = xTaskGetTickCount(); while (1) { int ret = esp_tls_low_level_conn(hostname, hostlen, port, cfg, tls); @@ -521,9 +486,6 @@ static int get_port(const char *url, struct http_parser_url *u) return 0; } -/** - * @brief Create a new TLS/SSL connection with a given "HTTP" url - */ esp_tls_t *esp_tls_conn_http_new(const char *url, const esp_tls_cfg_t *cfg) { /* Parse URI */ @@ -543,6 +505,21 @@ esp_tls_t *esp_tls_conn_http_new(const char *url, const esp_tls_cfg_t *cfg) return NULL; } +/** + * @brief Create a new TLS/SSL connection with a given "HTTP" url + */ +int esp_tls_conn_http_new_sync(const char *url, const esp_tls_cfg_t *cfg, esp_tls_t *tls) +{ + /* Parse URI */ + struct http_parser_url u; + http_parser_url_init(&u); + http_parser_parse_url(url, strlen(url), 0, &u); + + /* Connect to host */ + return esp_tls_conn_new_sync(&url[u.field_data[UF_HOST].off], u.field_data[UF_HOST].len, + get_port(url, &u), cfg, tls); +} + /** * @brief Create a new non-blocking TLS/SSL connection with a given "HTTP" url */ diff --git a/components/esp-tls/esp_tls.h b/components/esp-tls/esp_tls.h index c147619804..a4bbf8e2f2 100644 --- a/components/esp-tls/esp_tls.h +++ b/components/esp-tls/esp_tls.h @@ -351,7 +351,6 @@ typedef struct esp_tls { } esp_tls_t; - /** * @brief Create TLS connection * @@ -362,29 +361,21 @@ typedef struct esp_tls { */ esp_tls_t *esp_tls_init(void); - - - /** - * @brief Create a new blocking TLS/SSL connection - * - * This function establishes a TLS/SSL connection with the specified host in blocking manner. + * @brief Create a new blocking TLS/SSL connection with a given "HTTP" url * * Note: This API is present for backward compatibility reasons. Alternative function - * with the same functionality is `esp_tls_conn_new_sync` (and its asynchronous version - * `esp_tls_conn_new_async`) - * - * @param[in] hostname Hostname of the host. - * @param[in] hostlen Length of hostname. - * @param[in] port Port number of the host. - * @param[in] cfg TLS configuration as esp_tls_cfg_t. If you wish to open - * non-TLS connection, keep this NULL. For TLS connection, - * a pass pointer to esp_tls_cfg_t. At a minimum, this - * structure should be zero-initialized. + * with the same functionality is `esp_tls_conn_http_new_sync` (and its asynchronous version + * `esp_tls_conn_http_new_async`) * + * @param[in] url url of host. + * @param[in] cfg TLS configuration as esp_tls_cfg_t. If you wish to open + * non-TLS connection, keep this NULL. For TLS connection, + * a pass pointer to 'esp_tls_cfg_t'. At a minimum, this + * structure should be zero-initialized. * @return pointer to esp_tls_t, or NULL if connection couldn't be opened. */ -esp_tls_t *esp_tls_conn_new(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg) __attribute__ ((deprecated)); +esp_tls_t *esp_tls_conn_http_new(const char *url, const esp_tls_cfg_t *cfg) __attribute__((deprecated("Please use esp_tls_conn_http_new_sync (or its asynchronous version esp_tls_conn_http_new_async) instead"))); /** * @brief Create a new blocking TLS/SSL connection @@ -410,16 +401,21 @@ int esp_tls_conn_new_sync(const char *hostname, int hostlen, int port, const esp /** * @brief Create a new blocking TLS/SSL connection with a given "HTTP" url * - * The behaviour is same as esp_tls_conn_new() API. However this API accepts host's url. + * The behaviour is same as esp_tls_conn_new_sync() API. However this API accepts host's url. * - * @param[in] url url of host. - * @param[in] cfg TLS configuration as esp_tls_cfg_t. If you wish to open - * non-TLS connection, keep this NULL. For TLS connection, - * a pass pointer to 'esp_tls_cfg_t'. At a minimum, this - * structure should be zero-initialized. - * @return pointer to esp_tls_t, or NULL if connection couldn't be opened. + * @param[in] url url of host. + * @param[in] cfg TLS configuration as esp_tls_cfg_t. If you wish to open + * non-TLS connection, keep this NULL. For TLS connection, + * a pass pointer to 'esp_tls_cfg_t'. At a minimum, this + * structure should be zero-initialized. + * @param[in] tls Pointer to esp-tls as esp-tls handle. + * + * @return + * - -1 If connection establishment fails. + * - 1 If connection establishment is successful. + * - 0 If connection state is in progress. */ -esp_tls_t *esp_tls_conn_http_new(const char *url, const esp_tls_cfg_t *cfg); +int esp_tls_conn_http_new_sync(const char *url, const esp_tls_cfg_t *cfg, esp_tls_t *tls); /** * @brief Create a new non-blocking TLS/SSL connection @@ -444,7 +440,7 @@ int esp_tls_conn_new_async(const char *hostname, int hostlen, int port, const es /** * @brief Create a new non-blocking TLS/SSL connection with a given "HTTP" url * - * The behaviour is same as esp_tls_conn_new() API. However this API accepts host's url. + * The behaviour is same as esp_tls_conn_new_async() API. However this API accepts host's url. * * @param[in] url url of host. * @param[in] cfg TLS configuration as esp_tls_cfg_t. @@ -499,18 +495,12 @@ static inline ssize_t esp_tls_conn_read(esp_tls_t *tls, void *data, size_t data return tls->read(tls, (char *)data, datalen); } -/** - * @brief Compatible version of esp_tls_conn_destroy() to close the TLS/SSL connection - * - * @param[in] tls pointer to esp-tls as esp-tls handle. - */ -void esp_tls_conn_delete(esp_tls_t *tls) __attribute__((deprecated("Please use esp_tls_conn_destroy() instead"))); - /** * @brief Close the TLS/SSL connection and free any allocated resources. * - * This function should be called to close each tls connection opened with esp_tls_conn_new() or - * esp_tls_conn_http_new() APIs. + * This function should be called to close each tls connection opened with + * esp_tls_conn_new_sync() (or esp_tls_conn_http_new_sync()) and + * esp_tls_conn_new_async() (or esp_tls_conn_http_new_async()) APIs. * * @param[in] tls pointer to esp-tls as esp-tls handle. * @@ -681,7 +671,7 @@ esp_err_t esp_tls_plain_tcp_connect(const char *host, int hostlen, int port, con * @brief Obtain the client session ticket * * This function should be called when the TLS connection is already established. - * This can be passed again in the esp_tls_cfg_t structure, to appropriate tls session create (e.g. esp_tls_conn_http_new) API for session resumption. + * This can be passed again in the esp_tls_cfg_t structure, to appropriate tls session create (e.g. esp_tls_conn_http_new_sync) API for session resumption. * * @param[in] esp_tls context as esp_tls_t * @return