From 2fc8052b4def9ff6e7d36a80b4e3fa48d066c0f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Wed, 13 Nov 2019 11:38:30 +0000 Subject: [PATCH] Fix cast issues with socket addresses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Rework casts of SOCK_sockaddr in preparation for GNU gcc v9. - Rework code in IP addresses to use formal conversions and keeping the compiler happy in preparation for GBU gcc v9. - Remove ADS_PACKED define from unsupported compiler. Signed-off-by: José Simões --- src/HAL/Include/nanoHAL.h | 2 - src/PAL/COM/sockets/Sockets_debugger.cpp | 52 +++++++++++++++---- src/PAL/Include/nanoPAL_Sockets.h | 10 ++-- src/PAL/Lwip/lwIP_Sockets.cpp | 39 +++++++++++--- .../os/win32/Include/TargetPAL_BlockStorage.h | 1 - 5 files changed, 78 insertions(+), 26 deletions(-) diff --git a/src/HAL/Include/nanoHAL.h b/src/HAL/Include/nanoHAL.h index 9c103e46b6..d006b2ccd8 100644 --- a/src/HAL/Include/nanoHAL.h +++ b/src/HAL/Include/nanoHAL.h @@ -61,7 +61,6 @@ #if defined(_MSC_VER) -#define ADS_PACKED #define GNU_PACKED @@ -71,7 +70,6 @@ #elif defined(__GNUC__) -#define ADS_PACKED #define GNU_PACKED __attribute__((packed)) diff --git a/src/PAL/COM/sockets/Sockets_debugger.cpp b/src/PAL/COM/sockets/Sockets_debugger.cpp index 90ea61481e..17ef409e74 100644 --- a/src/PAL/COM/sockets/Sockets_debugger.cpp +++ b/src/PAL/COM/sockets/Sockets_debugger.cpp @@ -5,7 +5,7 @@ // #include "sockets_lwip.h" - +#include HAL_COMPLETION Sockets_LWIP_Driver::s_DebuggerTimeoutCompletion; @@ -107,6 +107,7 @@ bool Sockets_LWIP_Driver::InitializeDbgListener( int ComPortNum ) int nonblocking = 1; int32_t optval = 1; int32_t optLinger = 0; + void *dummyPtr; SOCKET_CHECK_ENTER(); Debugger_Initialize(); @@ -133,7 +134,16 @@ bool Sockets_LWIP_Driver::InitializeDbgListener( int ComPortNum ) SOCKET_CHECK_RESULT( HAL_SOCK_setsockopt(g_Sockets_LWIP_Driver.m_SocketDebugListener, SOCK_SOL_SOCKET, SOCK_SOCKO_LINGER, (const char*)&optLinger, sizeof(int32_t)) ); - SOCKET_CHECK_RESULT( SOCK_bind( g_Sockets_LWIP_Driver.m_SocketDebugListener, (SOCK_sockaddr*)&sockAddr, sizeof(sockAddr) ) ); + // need this to keep the compiler happy about the cast to SOCK_sockaddr + // which is intended and perfectly safe + dummyPtr = &sockAddr; + + SOCKET_CHECK_RESULT( + SOCK_bind( + g_Sockets_LWIP_Driver.m_SocketDebugListener, + (const SOCK_sockaddr*)dummyPtr, + sizeof(sockAddr)) + ); SOCKET_CHECK_RESULT( SOCK_listen( g_Sockets_LWIP_Driver.m_SocketDebugListener, 1 ) ); @@ -239,8 +249,8 @@ int Sockets_LWIP_Driver::Read( int ComPortNum, char* Data, size_t size ) { NATIVE_PROFILE_PAL_COM(); SOCK_SOCKET sock; - SOCK_sockaddr_in addr; - int len = sizeof(addr); + SOCK_sockaddr addr; + int len; int32_t ret = 0; SOCK_timeval timeout; SOCK_fd_set readSet; @@ -285,9 +295,9 @@ int Sockets_LWIP_Driver::Read( int ComPortNum, char* Data, size_t size ) if(SOCK_SOCKET_ERROR != HAL_SOCK_select( SOCK_FD_SETSIZE, &readSet, NULL, NULL, &timeout)) { // we always perform an accept so that we handle pending connections - // if we alread are connected and the debug stream socket is still active, then we immediately close + // if we already are connected and the debug stream socket is still active, then we immediately close // the pending connection - sock = Accept( g_Sockets_LWIP_Driver.m_SocketDebugListener, (SOCK_sockaddr*)&addr, &len, TRUE ); + sock = Accept( g_Sockets_LWIP_Driver.m_SocketDebugListener, &addr, &len, TRUE ); if(SOCK_SOCKET_ERROR != sock) { @@ -436,6 +446,7 @@ bool Sockets_LWIP_Driver::InitializeMulticastDiscovery() NATIVE_PROFILE_PAL_COM(); SOCKET_CHECK_ENTER(); SOCK_sockaddr_in sockAddr; + void *dummyPtr; int nonblocking = 1; if(g_Sockets_LWIP_Driver.s_discoveryInitialized) return TRUE; @@ -460,8 +471,17 @@ bool Sockets_LWIP_Driver::InitializeMulticastDiscovery() SOCKET_CHECK_RESULT( SOCK_ioctl(g_Sockets_LWIP_Driver.m_multicastSocket, SOCK_FIONBIO, &nonblocking) ); SOCKET_CHECK_RESULT( SOCK_setsockopt( g_Sockets_LWIP_Driver.m_multicastSocket, SOCK_IPPROTO_IP, SOCK_IPO_ADD_MEMBERSHIP, (const char*)&multicast, sizeof(multicast) ) ); + + // need this to keep the compiler happy about the cast to SOCK_sockaddr + // which is intended and perfectly safe + dummyPtr = &sockAddr; - SOCKET_CHECK_RESULT( SOCK_bind( g_Sockets_LWIP_Driver.m_multicastSocket, (SOCK_sockaddr*)&sockAddr, sizeof(sockAddr) ) ); + SOCKET_CHECK_RESULT( + SOCK_bind( + g_Sockets_LWIP_Driver.m_multicastSocket, + (const SOCK_sockaddr*)&dummyPtr, + sizeof(sockAddr)) + ); g_Sockets_LWIP_Driver.s_discoveryInitialized = TRUE; @@ -524,6 +544,7 @@ void Sockets_LWIP_Driver::MulticastDiscoveryRespond(void* arg) NATIVE_PROFILE_PAL_COM(); SOCK_sockaddr from; + void *dummyPtr; char data[64]; int fromlen = sizeof(from); @@ -573,7 +594,7 @@ void Sockets_LWIP_Driver::MulticastDiscoveryRespond(void* arg) memset( &sockAddr, 0, sizeof(sockAddr) ); sockAddr.sin_family = SOCK_AF_INET; sockAddr.sin_port = SOCK_htons(0); - // sockAddr.sin_addr.S_un.S_addr = info.ipaddr; + // sockAddr.sin_addr.S_un.S_addr = info.ipaddr; memset( &sockAddrMulticast, 0, sizeof(sockAddrMulticast) ); sockAddrMulticast.sin_family = SOCK_AF_INET; @@ -582,9 +603,20 @@ void Sockets_LWIP_Driver::MulticastDiscoveryRespond(void* arg) SOCKET_CHECK_RESULT(HAL_SOCK_ioctl(sock, SOCK_FIONBIO, &nonblocking)); SOCKET_CHECK_RESULT(HAL_SOCK_setsockopt(sock, SOCK_IPPROTO_IP, SOCK_IPO_MULTICAST_TTL, (const char *) &opt, sizeof(opt))); - SOCKET_CHECK_RESULT(HAL_SOCK_bind(sock, (SOCK_sockaddr*)&sockAddr, sizeof(sockAddr))); + + // need this to keep the compiler happy about the cast to SOCK_sockaddr + // which is intended and perfectly safe + dummyPtr = &sockAddr; + + SOCKET_CHECK_RESULT( + HAL_SOCK_bind( + sock, + (const SOCK_sockaddr*)&dummyPtr, + sizeof(sockAddr)) + ); + // send a multicast socket back to the caller - // SOCKET_CHECK_RESULT(SendTo(sock, (const char*)&info, sizeof(info), 0, (SOCK_sockaddr*)&sockAddrMulticast, sizeof(sockAddrMulticast))); + // SOCKET_CHECK_RESULT(SendTo(sock, (const char*)&info, sizeof(info), 0, (SOCK_sockaddr*)&sockAddrMulticast, sizeof(sockAddrMulticast))); SOCK_close(sock); diff --git a/src/PAL/Include/nanoPAL_Sockets.h b/src/PAL/Include/nanoPAL_Sockets.h index c1f88ea0f9..57b4deaacf 100644 --- a/src/PAL/Include/nanoPAL_Sockets.h +++ b/src/PAL/Include/nanoPAL_Sockets.h @@ -248,13 +248,13 @@ typedef struct SOCK_sockaddr { CT_ASSERT_UNIQUE_NAME(sizeof(SOCK_sockaddr)==(16), SOCK_SOCKADDR) -typedef ADS_PACKED struct GNU_PACKED SOCK_in_addr{ - ADS_PACKED union GNU_PACKED { - ADS_PACKED struct GNU_PACKED { +typedef struct GNU_PACKED SOCK_in_addr{ + union GNU_PACKED { + struct GNU_PACKED { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b; - ADS_PACKED struct GNU_PACKED { + struct GNU_PACKED { u_short s_w1,s_w2; } S_un_w; @@ -262,7 +262,7 @@ typedef ADS_PACKED struct GNU_PACKED SOCK_in_addr{ } S_un; } SOCK_in_addr; -typedef ADS_PACKED struct GNU_PACKED SOCK_sockaddr_in { +typedef struct GNU_PACKED SOCK_sockaddr_in { short sin_family; u_short sin_port; SOCK_in_addr sin_addr; diff --git a/src/PAL/Lwip/lwIP_Sockets.cpp b/src/PAL/Lwip/lwIP_Sockets.cpp index 245c4ab768..0e5c0c8ba3 100644 --- a/src/PAL/Lwip/lwIP_Sockets.cpp +++ b/src/PAL/Lwip/lwIP_Sockets.cpp @@ -383,6 +383,7 @@ int LWIP_SOCKETS_Driver::GetAddrInfo(const char* nodename, char* servname, const NATIVE_PROFILE_PAL_NETWORK(); SOCK_addrinfo *ai; + void *dummyPtr; SOCK_sockaddr_in *sa = NULL; int total_size = sizeof(SOCK_addrinfo) + sizeof(SOCK_sockaddr_in); struct addrinfo *lwipAddrinfo = NULL; @@ -425,8 +426,12 @@ int LWIP_SOCKETS_Driver::GetAddrInfo(const char* nodename, char* servname, const ai->ai_protocol = hints->ai_protocol; } + // need this to keep the compiler happy about the cast to SOCK_sockaddr + // which is intended and perfectly safe + dummyPtr = sa; + ai->ai_addrlen = sizeof(SOCK_sockaddr_in); - ai->ai_addr = (SOCK_sockaddr*)sa; + ai->ai_addr = (SOCK_sockaddr*)dummyPtr; *res = ai; @@ -467,8 +472,12 @@ int LWIP_SOCKETS_Driver::GetAddrInfo(const char* nodename, char* servname, const ai->ai_protocol = hints->ai_protocol; } + // need this to keep the compiler happy about the cast to SOCK_sockaddr + // which is intended and perfectly safe + dummyPtr = sa; + ai->ai_addrlen = sizeof(SOCK_sockaddr_in); - ai->ai_addr = (SOCK_sockaddr*)sa; + ai->ai_addr = (SOCK_sockaddr*)dummyPtr; *res = ai; @@ -917,15 +926,19 @@ HRESULT LWIP_SOCKETS_Driver::UpdateAdapterConfiguration( uint32_t interfaceIndex // user defined DNS addresses if(config->IPv4DNSAddress1 != 0) { - u8_t idx = 0; - - dns_setserver(idx, (const ip_addr_t *)&config->IPv4DNSAddress1); + // need to convert this first + ip_addr_t dnsServer; + ip_addr_set_ip4_u32(&dnsServer, config->IPv4DNSAddress1); + + dns_setserver(0, &dnsServer); } if(config->IPv4DNSAddress2 != 0) { - u8_t idx = 1; + // need to convert this first + ip_addr_t dnsServer; + ip_addr_set_ip4_u32(&dnsServer, config->IPv4DNSAddress2); - dns_setserver(idx, (const ip_addr_t *)&config->IPv4DNSAddress2); + dns_setserver(1, &dnsServer); } } } @@ -947,8 +960,18 @@ HRESULT LWIP_SOCKETS_Driver::UpdateAdapterConfiguration( uint32_t interfaceIndex // stop DHCP dhcp_stop(networkInterface); + // need to convert these first + ip_addr_t ipAddress, mask, gateway; + ip_addr_set_ip4_u32(&ipAddress, config->IPv4Address); + ip_addr_set_ip4_u32(&mask, config->IPv4NetMask); + ip_addr_set_ip4_u32(&gateway, config->IPv4GatewayAddress); + // set interface with our static IP configs - netif_set_addr(networkInterface, (const ip4_addr_t *) &config->IPv4Address, (const ip4_addr_t *)&config->IPv4NetMask, (const ip4_addr_t *)&config->IPv4GatewayAddress); + netif_set_addr( + networkInterface, + (const ip4_addr_t*)&ipAddress, + (const ip4_addr_t*)&mask, + (const ip4_addr_t*)&gateway); // we should be polite and let the DHCP server that we are now using a static IP dhcp_inform(networkInterface); diff --git a/targets/os/win32/Include/TargetPAL_BlockStorage.h b/targets/os/win32/Include/TargetPAL_BlockStorage.h index 4d6bdb014b..c18cb8086c 100644 --- a/targets/os/win32/Include/TargetPAL_BlockStorage.h +++ b/targets/os/win32/Include/TargetPAL_BlockStorage.h @@ -260,7 +260,6 @@ // // V Byte indicating if the block is valid (a.k.a. bad) // // E Bytes typically used for by a NAND driver for ECC // // -// ADS_PACKED // struct GNU_PACKED SectorMetadata // { // unsigned long dwReserved1; // Used by the FAL to hold the logical to physical sector mapping information.