From 8bf23e537217b273cca47e7a931e9dd14a306da2 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 27 Nov 2023 10:46:35 +0100 Subject: [PATCH] fix(esp-tls): fix build for IDF_TARGET=linux on macOS --- components/esp-tls/esp_tls.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/components/esp-tls/esp_tls.c b/components/esp-tls/esp_tls.c index 76265587e9..c56bb75c65 100644 --- a/components/esp-tls/esp_tls.c +++ b/components/esp-tls/esp_tls.c @@ -24,9 +24,16 @@ #include #include #include -#include #include +#ifdef __linux__ +#include +#endif + +#ifdef __APPLE__ +#include +#endif + typedef struct in_addr ip_addr_t; typedef struct in6_addr ip6_addr_t; #define ipaddr_ntoa(ipaddr) inet_ntoa(*ipaddr) @@ -278,6 +285,7 @@ static esp_err_t esp_tls_set_socket_options(int fd, const esp_tls_cfg_t *cfg) ESP_LOGE(TAG, "Fail to setsockopt SO_KEEPALIVE"); return ESP_ERR_ESP_TLS_SOCKET_SETOPT_FAILED; } +#ifndef __APPLE__ if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &keep_alive_idle, sizeof(keep_alive_idle)) != 0) { ESP_LOGE(TAG, "Fail to setsockopt TCP_KEEPIDLE"); return ESP_ERR_ESP_TLS_SOCKET_SETOPT_FAILED; @@ -290,11 +298,22 @@ static esp_err_t esp_tls_set_socket_options(int fd, const esp_tls_cfg_t *cfg) ESP_LOGE(TAG, "Fail to setsockopt TCP_KEEPCNT"); return ESP_ERR_ESP_TLS_SOCKET_SETOPT_FAILED; } +#else // __APPLE__ + if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &keep_alive_idle, sizeof(keep_alive_idle)) != 0) { + ESP_LOGE(TAG, "Fail to setsockopt TCP_KEEPALIVE"); + return ESP_ERR_ESP_TLS_SOCKET_SETOPT_FAILED; + } +#endif // __APPLE__ } 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); +#ifndef __APPLE__ if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, cfg->if_name, sizeof(struct ifreq)) != 0) { +#else + int idx = if_nametoindex(cfg->if_name->ifr_name); + if (setsockopt(fd, IPPROTO_IP, IP_BOUND_IF, &idx, sizeof(idx)) != 0) { +#endif ESP_LOGE(TAG, "Bind [sock=%d] to interface %s fail", fd, cfg->if_name->ifr_name); return ESP_ERR_ESP_TLS_SOCKET_SETOPT_FAILED; }