From 5fb4cd8a9c1e4d322b4f2758378722ebc4856840 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Sat, 27 Dec 2025 12:18:21 +0000 Subject: [PATCH] getifaddrs(): Check for function support --- CMakeLists.txt | 1 + cmake_coap_config.h.in | 3 +++ configure.ac | 2 +- src/coap_address.c | 12 ++++++------ 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a4509238..f862c103 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -314,6 +314,7 @@ if(MINGW OR WIN32) set(CMAKE_REQUIRED_LIBRARIES "") endif() check_function_exists(getaddrinfo HAVE_GETADDRINFO) +check_function_exists(getifaddrs HAVE_GETIFADDRS) check_function_exists(strnlen HAVE_STRNLEN) check_function_exists(strrchr HAVE_STRRCHR) check_function_exists(getrandom HAVE_GETRANDOM) diff --git a/cmake_coap_config.h.in b/cmake_coap_config.h.in index 4c1895a5..c134572a 100644 --- a/cmake_coap_config.h.in +++ b/cmake_coap_config.h.in @@ -43,6 +43,9 @@ /* Define to 1 if you have the `getaddrinfo' function. */ #cmakedefine HAVE_GETADDRINFO @HAVE_GETADDRINFO@ +/* Define to 1 if you have the `getifaddrs' function. */ +#cmakedefine HAVE_GETIFADDRS @HAVE_GETIFADDRS@ + /* Define to 1 if you have the `getrandom' function. */ #cmakedefine HAVE_GETRANDOM @HAVE_GETRANDOM@ diff --git a/configure.ac b/configure.ac index a8715b8f..9a1c0bf8 100644 --- a/configure.ac +++ b/configure.ac @@ -1242,7 +1242,7 @@ AC_TYPE_SIZE_T AC_TYPE_SSIZE_T # Checks for library functions. -AC_CHECK_FUNCS([memset select socket strcasecmp strrchr getaddrinfo \ +AC_CHECK_FUNCS([memset select socket strcasecmp strrchr getaddrinfo getifaddrs \ strnlen malloc pthread_mutex_lock getrandom random if_nametoindex]) # Check if -lsocket -lnsl is required (specifically Solaris) diff --git a/src/coap_address.c b/src/coap_address.c index 35215832..845a9dec 100644 --- a/src/coap_address.c +++ b/src/coap_address.c @@ -180,20 +180,20 @@ coap_is_mcast(const coap_address_t *a) { #define COAP_BCST_REFRESH_SECS 30 #endif /* COAP_BCST_REFRESH_SECS */ -#if (COAP_IPV4_SUPPORT && defined(HAVE_IFADDRS_H) && !defined(__ZEPHYR__)) || defined(_WIN32) +#if COAP_IPV4_SUPPORT && !defined(__ZEPHYR__) && (defined(HAVE_GETIFADDRS) || defined(_WIN32)) static int bcst_cnt = -1; static coap_tick_t last_refresh; static struct in_addr b_ipv4[COAP_BCST_CNT]; -#endif /* (COAP_IPV4_SUPPORT && HAVE_IFADDRS_H && !defined(__ZEPHYR__)) || defined(_WIN32) */ +#endif /* COAP_IPV4_SUPPORT && !defined(__ZEPHYR__) && (defined(HAVE_GETIFADDRS) || defined(_WIN32)) */ int coap_is_bcast(const coap_address_t *a) { #if COAP_IPV4_SUPPORT struct in_addr ipv4; -#if defined(HAVE_IFADDRS_H) && !defined(__ZEPHYR__) +#if defined(HAVE_GETIFADDRS) && !defined(__ZEPHYR__) int i; coap_tick_t now; -#endif /* HAVE_IFADDRS_H && !defined(__ZEPHYR__) */ +#endif /* HAVE_GETIFADDRS && !defined(__ZEPHYR__) */ #endif /* COAP_IPV4_SUPPORT */ if (!a) @@ -226,7 +226,7 @@ coap_is_bcast(const coap_address_t *a) { if (ipv4.s_addr == INADDR_BROADCAST) return 1; -#if defined(HAVE_IFADDRS_H) && !defined(__ZEPHYR__) && !defined(_WIN32) +#if defined(HAVE_GETIFADDRS) && !defined(__ZEPHYR__) coap_ticks(&now); if (bcst_cnt == -1 || (now - last_refresh) > (COAP_BCST_REFRESH_SECS * COAP_TICKS_PER_SECOND)) { @@ -268,7 +268,7 @@ coap_is_bcast(const coap_address_t *a) { if (ipv4.s_addr == b_ipv4[i].s_addr) return 1; } -#endif /* HAVE_IFADDRS_H && !defined(__ZEPHYR__) && !defined(_WIN32) */ +#endif /* HAVE_GETIFADDRS && !defined(__ZEPHYR__) */ #if defined(_WIN32)