From bead3599abd875d746e64cd6749574ff2c155adb Mon Sep 17 00:00:00 2001 From: yuanjm Date: Tue, 19 Jan 2021 17:50:31 +0800 Subject: [PATCH] components: Support bind socket to specified interface in esp_http_client and esp_websocket_client component --- components/esp_http_client/esp_http_client.c | 14 +++++++++++++- .../esp_http_client/include/esp_http_client.h | 2 ++ .../esp_websocket_client/esp_websocket_client.c | 12 +++++++++++- .../include/esp_websocket_client.h | 2 ++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/components/esp_http_client/esp_http_client.c b/components/esp_http_client/esp_http_client.c index 779033d427..fbdb575376 100644 --- a/components/esp_http_client/esp_http_client.c +++ b/components/esp_http_client/esp_http_client.c @@ -122,6 +122,7 @@ struct esp_http_client { int header_index; bool is_async; esp_transport_keep_alive_t keep_alive_cfg; + struct ifreq *if_name; }; typedef struct esp_http_client esp_http_client_t; @@ -576,6 +577,7 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co ESP_LOGE(TAG, "Error initialize transport"); goto error; } + if (config->keep_alive_enable == true) { client->keep_alive_cfg.keep_alive_enable = true; client->keep_alive_cfg.keep_alive_idle = (config->keep_alive_idle == 0) ? DEFAULT_KEEP_ALIVE_IDLE : config->keep_alive_idle; @@ -583,6 +585,14 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co client->keep_alive_cfg.keep_alive_count = (config->keep_alive_count == 0) ? DEFAULT_KEEP_ALIVE_COUNT : config->keep_alive_count; esp_transport_tcp_set_keep_alive(tcp, &client->keep_alive_cfg); } + + if (config->if_name) { + client->if_name = calloc(1, sizeof(struct ifreq) + 1); + HTTP_MEM_CHECK(TAG, client->if_name, goto error); + memcpy(client->if_name, config->if_name, sizeof(struct ifreq)); + esp_transport_tcp_set_interface_name(tcp, client->if_name); + } + #ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS esp_transport_handle_t ssl = NULL; _success = ( @@ -719,7 +729,9 @@ esp_err_t esp_http_client_cleanup(esp_http_client_handle_t client) free(client->response->buffer); free(client->response); } - + if (client->if_name) { + free(client->if_name); + } free(client->parser); free(client->parser_settings); _clear_connection_info(client); diff --git a/components/esp_http_client/include/esp_http_client.h b/components/esp_http_client/include/esp_http_client.h index 4884a74e0f..ed4e4a3230 100644 --- a/components/esp_http_client/include/esp_http_client.h +++ b/components/esp_http_client/include/esp_http_client.h @@ -19,6 +19,7 @@ #include "http_parser.h" #include "sdkconfig.h" #include "esp_err.h" +#include #ifdef __cplusplus extern "C" { @@ -135,6 +136,7 @@ typedef struct { int keep_alive_idle; /*!< Keep-alive idle time. Default is 5 (second) */ int keep_alive_interval; /*!< Keep-alive interval time. Default is 5 (second) */ int keep_alive_count; /*!< Keep-alive packet retry send count. Default is 3 counts */ + struct ifreq *if_name; /*!< The name of interface for data to go through. Use the default interface without setting */ } esp_http_client_config_t; /** diff --git a/components/esp_websocket_client/esp_websocket_client.c b/components/esp_websocket_client/esp_websocket_client.c index 5055736d43..c9bb0fd324 100644 --- a/components/esp_websocket_client/esp_websocket_client.c +++ b/components/esp_websocket_client/esp_websocket_client.c @@ -121,6 +121,7 @@ struct esp_websocket_client { int payload_len; int payload_offset; esp_transport_keep_alive_t keep_alive_cfg; + struct ifreq *if_name; }; static uint64_t _tick_get_ms(void) @@ -316,6 +317,12 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie client->keep_alive_cfg.keep_alive_count = (config->keep_alive_count == 0) ? WEBSOCKET_KEEP_ALIVE_COUNT : config->keep_alive_count; } + if (config->if_name) { + client->if_name = calloc(1, sizeof(struct ifreq) + 1); + ESP_WS_CLIENT_MEM_CHECK(TAG, client->if_name, goto _websocket_init_fail); + memcpy(client->if_name, config->if_name, sizeof(struct ifreq)); + } + client->lock = xSemaphoreCreateRecursiveMutex(); ESP_WS_CLIENT_MEM_CHECK(TAG, client->lock, goto _websocket_init_fail); @@ -331,7 +338,7 @@ esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_clie esp_transport_set_default_port(tcp, WEBSOCKET_TCP_DEFAULT_PORT); esp_transport_list_add(client->transport_list, tcp, "_tcp"); // need to save to transport list, for cleanup esp_transport_tcp_set_keep_alive(tcp, &client->keep_alive_cfg); - + esp_transport_tcp_set_interface_name(tcp, client->if_name); esp_transport_handle_t ws = esp_transport_ws_init(tcp); ESP_WS_CLIENT_MEM_CHECK(TAG, ws, goto _websocket_init_fail); @@ -448,6 +455,9 @@ esp_err_t esp_websocket_client_destroy(esp_websocket_client_handle_t client) if (client->event_handle) { esp_event_loop_delete(client->event_handle); } + if (client->if_name) { + free(client->if_name); + } esp_websocket_client_destroy_config(client); esp_transport_list_destroy(client->transport_list); vQueueDelete(client->lock); diff --git a/components/esp_websocket_client/include/esp_websocket_client.h b/components/esp_websocket_client/include/esp_websocket_client.h index 13f817685e..5ef08035d4 100644 --- a/components/esp_websocket_client/include/esp_websocket_client.h +++ b/components/esp_websocket_client/include/esp_websocket_client.h @@ -22,6 +22,7 @@ #include "freertos/FreeRTOS.h" #include "esp_err.h" #include "esp_event.h" +#include #ifdef __cplusplus extern "C" { @@ -100,6 +101,7 @@ typedef struct { int keep_alive_interval; /*!< Keep-alive interval time. Default is 5 (second) */ int keep_alive_count; /*!< Keep-alive packet retry send count. Default is 3 counts */ size_t ping_interval_sec; /*!< Websocket ping interval, defaults to 10 seconds if not set */ + struct ifreq *if_name; /*!< The name of interface for data to go through. Use the default interface without setting */ } esp_websocket_client_config_t; /**