Merge branch 'bugfix/macos_build_linux_target_transport_tls_http_server' into 'master'

fix(linux): fix several components failing to build with IDF_TARGET=linux on macOS

See merge request espressif/esp-idf!27883
This commit is contained in:
Ivan Grokhotkov
2023-12-20 05:58:29 +08:00
6 changed files with 78 additions and 4 deletions

View File

@@ -24,9 +24,16 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <linux/if.h>
#include <sys/time.h> #include <sys/time.h>
#ifdef __linux__
#include <linux/if.h>
#endif
#ifdef __APPLE__
#include <net/if.h>
#endif
typedef struct in_addr ip_addr_t; typedef struct in_addr ip_addr_t;
typedef struct in6_addr ip6_addr_t; typedef struct in6_addr ip6_addr_t;
#define ipaddr_ntoa(ipaddr) inet_ntoa(*ipaddr) #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"); ESP_LOGE(TAG, "Fail to setsockopt SO_KEEPALIVE");
return ESP_ERR_ESP_TLS_SOCKET_SETOPT_FAILED; 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) { if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &keep_alive_idle, sizeof(keep_alive_idle)) != 0) {
ESP_LOGE(TAG, "Fail to setsockopt TCP_KEEPIDLE"); ESP_LOGE(TAG, "Fail to setsockopt TCP_KEEPIDLE");
return ESP_ERR_ESP_TLS_SOCKET_SETOPT_FAILED; 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"); ESP_LOGE(TAG, "Fail to setsockopt TCP_KEEPCNT");
return ESP_ERR_ESP_TLS_SOCKET_SETOPT_FAILED; 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) {
if (cfg->if_name->ifr_name[0] != 0) { if (cfg->if_name->ifr_name[0] != 0) {
ESP_LOGD(TAG, "Bind [sock=%d] to interface %s", fd, cfg->if_name->ifr_name); 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) { 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); ESP_LOGE(TAG, "Bind [sock=%d] to interface %s fail", fd, cfg->if_name->ifr_name);
return ESP_ERR_ESP_TLS_SOCKET_SETOPT_FAILED; return ESP_ERR_ESP_TLS_SOCKET_SETOPT_FAILED;
} }

View File

@@ -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 * 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); ESP_LOGE(TAG, LOG_FMT("error in setsockopt SO_KEEPALIVE (%d)"), errno);
goto exit; goto exit;
} }
#ifndef __APPLE__
if (setsockopt(new_fd, IPPROTO_TCP, TCP_KEEPIDLE, &keep_alive_idle, sizeof(keep_alive_idle)) < 0) { 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); ESP_LOGE(TAG, LOG_FMT("error in setsockopt TCP_KEEPIDLE (%d)"), errno);
goto exit; 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); ESP_LOGE(TAG, LOG_FMT("error in setsockopt TCP_KEEPCNT (%d)"), errno);
goto exit; 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)) { if (ESP_OK != httpd_sess_new(hd, new_fd)) {
ESP_LOGE(TAG, LOG_FMT("session creation failed")); ESP_LOGE(TAG, LOG_FMT("session creation failed"));

View File

@@ -3,5 +3,10 @@ if(NOT "${target}" STREQUAL "linux")
return() return()
endif() endif()
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
list(APPEND srcs getrandom.c)
endif()
idf_component_register(INCLUDE_DIRS include idf_component_register(INCLUDE_DIRS include
REQUIRED_IDF_TARGETS linux) REQUIRED_IDF_TARGETS linux
SRCS ${srcs})

View File

@@ -0,0 +1,28 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "sys/random.h"
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <unistd.h>
// 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;
}

View File

@@ -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 <stddef.h>
int getrandom(void *buf, size_t buflen, unsigned int flags);
#endif // __APPLE__

View File

@@ -8,7 +8,7 @@
#include "mbedtls/platform_time.h" #include "mbedtls/platform_time.h"
#ifdef MBEDTLS_PLATFORM_MS_TIME_ALT #ifdef MBEDTLS_PLATFORM_MS_TIME_ALT
mbedtls_ms_time_t mbedtls_ms_time() mbedtls_ms_time_t mbedtls_ms_time(void)
{ {
int ret; int ret;
struct timespec tv = {}; struct timespec tv = {};