From 292b0f07904f240a001215b52687e5354ca58596 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Thu, 15 Dec 2016 11:12:22 +0100 Subject: [PATCH] address.[hc]: Fixed byteorder in coap_is_mcast() on platform posix As IN_MULTICAST does a 32 bit comparison in host byte order, sin_addr.s_addr needs to be converted using ntohl(). This change also moves the coap_is_mcast() implementation to address.c because it has become too complex for inlining. --- include/coap/address.h | 26 +++++++++----------------- src/address.c | 17 ++++++++++++++++- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/include/coap/address.h b/include/coap/address.h index 6f662bde..85db2046 100644 --- a/include/coap/address.h +++ b/include/coap/address.h @@ -1,7 +1,7 @@ /* * address.h -- representation of network addresses * - * Copyright (C) 2010-2011,2015 Olaf Bergmann + * Copyright (C) 2010-2011,2015-2016 Olaf Bergmann * * This file is part of the CoAP library libcoap. Please see README for terms * of use. @@ -89,22 +89,6 @@ _coap_address_isany_impl(const coap_address_t *a) { return 0; } - -static inline int -_coap_is_mcast_impl(const coap_address_t *a) { - if (!a) - return 0; - - switch (a->addr.sa.sa_family) { - case AF_INET: - return IN_MULTICAST(a->addr.sin.sin_addr.s_addr); - case AF_INET6: - return IN6_IS_ADDR_MULTICAST(&a->addr.sin6.sin6_addr); - default: /* fall through and signal error */ - ; - } - return 0; -} #endif /* WITH_POSIX */ /** @@ -148,6 +132,13 @@ coap_address_isany(const coap_address_t *a) { return _coap_address_isany_impl(a); } +#ifdef WITH_POSIX +/** + * Checks if given address @p a denotes a multicast address. This function + * returns @c 1 if @p a is multicast, @c 0 otherwise. + */ +int coap_is_mcast(const coap_address_t *a); +#else /* WITH_POSIX */ /** * Checks if given address @p a denotes a multicast address. This function * returns @c 1 if @p a is multicast, @c 0 otherwise. @@ -156,5 +147,6 @@ static inline int coap_is_mcast(const coap_address_t *a) { return a && _coap_is_mcast_impl(a); } +#endif /* WITH_POSIX */ #endif /* _COAP_ADDRESS_H_ */ diff --git a/src/address.c b/src/address.c index e237881a..b4db76f0 100644 --- a/src/address.c +++ b/src/address.c @@ -1,6 +1,6 @@ /* address.c -- representation of network addresses * - * Copyright (C) 2015 Olaf Bergmann + * Copyright (C) 2015-2016 Olaf Bergmann * * This file is part of the CoAP library libcoap. Please see * README for terms of use. @@ -8,6 +8,7 @@ #ifdef WITH_POSIX #include +#include #include #include @@ -37,6 +38,20 @@ coap_address_equals(const coap_address_t *a, const coap_address_t *b) { return 0; } +int coap_is_mcast(const coap_address_t *a) { + if (!a) + return 0; + + switch (a->addr.sa.sa_family) { + case AF_INET: + return IN_MULTICAST(ntohl(a->addr.sin.sin_addr.s_addr)); + case AF_INET6: + return IN6_IS_ADDR_MULTICAST(&a->addr.sin6.sin6_addr); + default: /* fall through and signal error */ + ; + } + return 0; +} #else /* WITH_POSIX */ /* make compilers happy that do not like empty modules */