From 357b1da140c07bd6691f1fc2cbc13dade572ba4f Mon Sep 17 00:00:00 2001 From: Moandor Date: Wed, 28 Oct 2020 08:11:32 +0800 Subject: [PATCH] [posix] fix cast-align build error on ARM (#5672) --- src/posix/platform/netif.cpp | 25 +++++++++++++++++++++---- src/posix/platform/udp.cpp | 6 +++--- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/posix/platform/netif.cpp b/src/posix/platform/netif.cpp index 0bae6928a..3797c1dab 100644 --- a/src/posix/platform/netif.cpp +++ b/src/posix/platform/netif.cpp @@ -329,6 +329,9 @@ static uint8_t NetmaskToPrefixLength(const struct sockaddr_in6 *netmask) #endif #if defined(__linux__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" + static void UpdateUnicastLinux(const otIp6AddressInfo &aAddressInfo, bool aIsAdded) { struct rtattr *rta; @@ -389,6 +392,8 @@ static void UpdateUnicastLinux(const otIp6AddressInfo &aAddressInfo, bool aIsAdd Ip6AddressString(aAddressInfo.mAddress).AsCString(), aAddressInfo.mPrefixLength); } } + +#pragma GCC diagnostic pop #endif // defined(__linux__) static void UpdateUnicast(otInstance *aInstance, const otIp6AddressInfo &aAddressInfo, bool aIsAdded) @@ -1053,14 +1058,23 @@ static void processNetlinkEvent(otInstance *aInstance) { const size_t kMaxNetifEvent = 8192; ssize_t length; - char buffer[kMaxNetifEvent]; - length = recv(sNetlinkFd, buffer, sizeof(buffer), 0); + union + { +#if defined(__linux__) + nlmsghdr nlMsg; +#else + rt_msghdr rtMsg; +#endif + char buffer[kMaxNetifEvent]; + } msgBuffer; + + length = recv(sNetlinkFd, msgBuffer.buffer, sizeof(msgBuffer.buffer), 0); VerifyOrExit(length > 0); #if defined(__linux__) - for (struct nlmsghdr *msg = reinterpret_cast(buffer); NLMSG_OK(msg, static_cast(length)); + for (struct nlmsghdr *msg = &msgBuffer.nlMsg; NLMSG_OK(msg, static_cast(length)); msg = NLMSG_NEXT(msg, length)) { #else @@ -1068,7 +1082,7 @@ static void processNetlinkEvent(otInstance *aInstance) // BSD sends one message per read to routing socket (see route.c, monitor command) struct rt_msghdr *msg; - msg = (struct rt_msghdr *)buffer; + msg = &msgBuffer.rtMsg; #define nlmsg_type rtm_type @@ -1210,7 +1224,10 @@ static void processMLDEvent(otInstance *aInstance) if (ifAddr->ifa_addr != nullptr && ifAddr->ifa_addr->sa_family == AF_INET6 && strncmp(gNetifName, ifAddr->ifa_name, IFNAMSIZ) == 0) { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" struct sockaddr_in6 *addr6 = reinterpret_cast(ifAddr->ifa_addr); +#pragma GCC diagnostic pop if (memcmp(&addr6->sin6_addr, &srcAddr.sin6_addr, sizeof(in6_addr)) == 0) { diff --git a/src/posix/platform/udp.cpp b/src/posix/platform/udp.cpp index 5151f51fd..151c73640 100644 --- a/src/posix/platform/udp.cpp +++ b/src/posix/platform/udp.cpp @@ -72,9 +72,9 @@ static bool IsLinkLocal(const struct in6_addr &aAddress) return aAddress.s6_addr[0] == 0xfe && aAddress.s6_addr[1] == 0x80; } -static bool IsMulticast(const struct in6_addr &aAddress) +static bool IsMulticast(const otIp6Address &aAddress) { - return aAddress.s6_addr[0] == 0xff; + return aAddress.mFields.m8[0] == 0xff; } static otError transmitPacket(int aFd, uint8_t *aPayload, uint16_t aLength, const otMessageInfo &aMessageInfo) @@ -128,7 +128,7 @@ static otError transmitPacket(int aFd, uint8_t *aPayload, uint16_t aLength, cons controlLength += CMSG_SPACE(sizeof(int)); } - if (!IsMulticast(reinterpret_cast(aMessageInfo.mSockAddr)) && + if (!IsMulticast(aMessageInfo.mSockAddr) && memcmp(&aMessageInfo.mSockAddr, &in6addr_any, sizeof(aMessageInfo.mSockAddr))) { struct in6_pktinfo pktinfo;