Skip to content

Commit

Permalink
coap_address.c: Make coap_is_bcast() more generic for different builds
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdeep1 authored and Jon Shallow committed Sep 12, 2023
1 parent e68f745 commit 1db8eda
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ check_include_file(strings.h HAVE_STRINGS_H)
check_include_file(string.h HAVE_STRING_H)
check_include_file(sys/sysctl.h HAVE_SYS_SYSCTL_H)
check_include_file(net/if.h HAVE_NET_IF_H)
check_include_file(ifaddrs.h HAVE_IFADDRS_H)
check_include_file(netinet/in.h HAVE_NETINET_IN_H)
check_include_file(sys/epoll.h HAVE_EPOLL_H)
check_include_file(sys/timerfd.h HAVE_TIMERFD_H)
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ fi
AC_CHECK_HEADERS([assert.h arpa/inet.h limits.h netdb.h netinet/in.h \
pthread.h errno.h winsock2.h ws2tcpip.h \
stdlib.h string.h strings.h sys/socket.h sys/time.h \
time.h unistd.h sys/unistd.h sys/ioctl.h net/if.h])
time.h unistd.h sys/unistd.h sys/ioctl.h net/if.h ifaddrs.h])

# For epoll, need two headers (sys/epoll.h sys/timerfd.h), but set up one #define
AC_CHECK_HEADER([sys/epoll.h])
Expand Down
35 changes: 15 additions & 20 deletions src/coap_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NET_IF_H
#include <net/if.h>
#endif
#ifdef HAVE_IFADDRS_H
#include <ifaddrs.h>
#endif
#ifdef HAVE_WS2TCPIP_H
#include <ws2tcpip.h>
#endif
Expand Down Expand Up @@ -139,8 +145,6 @@ coap_is_mcast(const coap_address_t *a) {
return 0;
}

#if !defined(WIN32) && !defined(__ZEPHYR__)

#ifndef COAP_BCST_CNT
#define COAP_BCST_CNT 15
#endif /* COAP_BCST_CNT */
Expand All @@ -150,25 +154,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(ESPIDF_VERSION)
#if COAP_IPV4_SUPPORT && defined(HAVE_IFADDRS_H)
static int bcst_cnt = -1;
static coap_tick_t last_refresh;
static struct in_addr b_ipv4[COAP_BCST_CNT];
#endif /* COAP_IPV4_SUPPORT && ! ESPIDF_VERSION */

#if !defined(ESPIDF_VERSION)
#include <net/if.h>
#include <ifaddrs.h>
#endif /* ! ESPIDF_VERSION */
#endif /* COAP_IPV4_SUPPORT && HAVE_IFADDRS_H */

int
coap_is_bcast(const coap_address_t *a) {
#if COAP_IPV4_SUPPORT
struct in_addr ipv4;
#if !defined(ESPIDF_VERSION)
#if defined(HAVE_IFADDRS_H)
int i;
coap_tick_t now;
#endif /* ! ESPIDF_VERSION */
#endif /* HAVE_IFADDRS_H */
#endif /* COAP_IPV4_SUPPORT */

if (!a)
Expand All @@ -195,10 +194,13 @@ coap_is_bcast(const coap_address_t *a) {
return 0;
}
#if COAP_IPV4_SUPPORT
#ifndef INADDR_BROADCAST
#define INADDR_BROADCAST ((uint32_t)0xffffffffUL)
#endif /* !INADDR_BROADCAST */
if (ipv4.s_addr == INADDR_BROADCAST)
return 1;

#if !defined(ESPIDF_VERSION)
#if defined(HAVE_IFADDRS_H)
coap_ticks(&now);
if (bcst_cnt == -1 ||
(now - last_refresh) > (COAP_BCST_REFRESH_SECS * COAP_TICKS_PER_SECOND)) {
Expand Down Expand Up @@ -230,17 +232,10 @@ coap_is_bcast(const coap_address_t *a) {
if (ipv4.s_addr == b_ipv4[i].s_addr)
return 1;
}
#endif /* ESPIDF_VERSION */
#endif /* HAVE_IFADDRS_H */
return 0;
#endif /* COAP_IPV4_SUPPORT */
}
#else /* WIN32 || __ZEPHYR__ */
int
coap_is_bcast(const coap_address_t *a) {
(void)a;
return 0;
}
#endif /* WIN32 || __ZEPHYR__ */

#endif /* !defined(WITH_CONTIKI) && !defined(WITH_LWIP) */

Expand Down

0 comments on commit 1db8eda

Please sign in to comment.