From 49f289f8931812c22a8946e2630fcdd2141d3df1 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 27 Nov 2023 10:46:09 +0100 Subject: [PATCH 1/4] fix(esp_http_server): fix build for IDF_TARGET=linux on macOS --- components/esp_http_server/src/httpd_main.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/components/esp_http_server/src/httpd_main.c b/components/esp_http_server/src/httpd_main.c index 5dc4d18243..7b5e956061 100644 --- a/components/esp_http_server/src/httpd_main.c +++ b/components/esp_http_server/src/httpd_main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2018-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -101,6 +101,7 @@ static esp_err_t httpd_accept_conn(struct httpd_data *hd, int listen_fd) ESP_LOGE(TAG, LOG_FMT("error in setsockopt SO_KEEPALIVE (%d)"), errno); goto exit; } +#ifndef __APPLE__ if (setsockopt(new_fd, IPPROTO_TCP, TCP_KEEPIDLE, &keep_alive_idle, sizeof(keep_alive_idle)) < 0) { ESP_LOGE(TAG, LOG_FMT("error in setsockopt TCP_KEEPIDLE (%d)"), errno); goto exit; @@ -113,6 +114,12 @@ static esp_err_t httpd_accept_conn(struct httpd_data *hd, int listen_fd) ESP_LOGE(TAG, LOG_FMT("error in setsockopt TCP_KEEPCNT (%d)"), errno); goto exit; } +#else // __APPLE__ + if (setsockopt(new_fd, IPPROTO_TCP, TCP_KEEPALIVE, &keep_alive_idle, sizeof(keep_alive_idle)) < 0) { + ESP_LOGE(TAG, LOG_FMT("error in setsockopt TCP_KEEPALIVE (%d)"), errno); + goto exit; + } +#endif // __APPLE__ } if (ESP_OK != httpd_sess_new(hd, new_fd)) { ESP_LOGE(TAG, LOG_FMT("session creation failed")); From 8bf23e537217b273cca47e7a931e9dd14a306da2 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 27 Nov 2023 10:46:35 +0100 Subject: [PATCH 2/4] 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; } From a596ca56a873ea6adce8064ac10fca7ac8a7ea43 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 27 Nov 2023 14:37:45 +0100 Subject: [PATCH 3/4] fix(mbedtls): fix -Wstrict-prototypes warning when compiling on Linux --- components/mbedtls/port/esp_platform_time.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mbedtls/port/esp_platform_time.c b/components/mbedtls/port/esp_platform_time.c index 541b664ab9..1b30774b83 100644 --- a/components/mbedtls/port/esp_platform_time.c +++ b/components/mbedtls/port/esp_platform_time.c @@ -8,7 +8,7 @@ #include "mbedtls/platform_time.h" #ifdef MBEDTLS_PLATFORM_MS_TIME_ALT -mbedtls_ms_time_t mbedtls_ms_time() +mbedtls_ms_time_t mbedtls_ms_time(void) { int ret; struct timespec tv = {}; From 7b8f69404fbd524bd1155633f7fc0db302aa62ba Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 15 Dec 2023 13:50:48 +0100 Subject: [PATCH 4/4] feat(linux): provide getrandom implementation for macOS This makes getrandom(2) usable when compiling with IDF_TARGET=linux on a macOS host. --- components/linux/CMakeLists.txt | 7 ++++++- components/linux/getrandom.c | 28 +++++++++++++++++++++++++++ components/linux/include/sys/random.h | 15 ++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 components/linux/getrandom.c create mode 100644 components/linux/include/sys/random.h diff --git a/components/linux/CMakeLists.txt b/components/linux/CMakeLists.txt index b7d2bd46a7..e29218d213 100644 --- a/components/linux/CMakeLists.txt +++ b/components/linux/CMakeLists.txt @@ -3,5 +3,10 @@ if(NOT "${target}" STREQUAL "linux") return() endif() +if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin") + list(APPEND srcs getrandom.c) +endif() + idf_component_register(INCLUDE_DIRS include - REQUIRED_IDF_TARGETS linux) + REQUIRED_IDF_TARGETS linux + SRCS ${srcs}) diff --git a/components/linux/getrandom.c b/components/linux/getrandom.c new file mode 100644 index 0000000000..5f068ee3fd --- /dev/null +++ b/components/linux/getrandom.c @@ -0,0 +1,28 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "sys/random.h" +#include +#include +#include +#include + + +// getrandom() is not available on macOS, so we read from /dev/urandom instead. + +int getrandom(void *buf, size_t buflen, unsigned int flags) +{ + int fd = open("/dev/urandom", O_RDONLY); + if (fd < 0) { + return -1; + } + ssize_t ret = read(fd, buf, buflen); + close(fd); + if (ret < 0) { + return -1; + } + return 0; +} diff --git a/components/linux/include/sys/random.h b/components/linux/include/sys/random.h new file mode 100644 index 0000000000..b495f327bc --- /dev/null +++ b/components/linux/include/sys/random.h @@ -0,0 +1,15 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include_next "sys/random.h" + +#if __APPLE__ +#include + +int getrandom(void *buf, size_t buflen, unsigned int flags); + +#endif // __APPLE__