Skip to content

Commit

Permalink
Fix cast issues with socket addresses
Browse files Browse the repository at this point in the history
- 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 <jose.simoes@eclo.solutions>
  • Loading branch information
josesimoes committed Nov 13, 2019
1 parent c1e3073 commit 2fc8052
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 26 deletions.
2 changes: 0 additions & 2 deletions src/HAL/Include/nanoHAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@

#if defined(_MSC_VER)

#define ADS_PACKED
#define GNU_PACKED


Expand All @@ -71,7 +70,6 @@
#elif defined(__GNUC__)


#define ADS_PACKED
#define GNU_PACKED __attribute__((packed))


Expand Down
52 changes: 42 additions & 10 deletions src/PAL/COM/sockets/Sockets_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//

#include "sockets_lwip.h"

#include <lwIP_Sockets.h>

HAL_COMPLETION Sockets_LWIP_Driver::s_DebuggerTimeoutCompletion;

Expand Down Expand Up @@ -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();
Expand All @@ -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 ) );

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand Down
10 changes: 5 additions & 5 deletions src/PAL/Include/nanoPAL_Sockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,21 +248,21 @@ 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;

u_long S_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;
Expand Down
39 changes: 31 additions & 8 deletions src/PAL/Lwip/lwIP_Sockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion targets/os/win32/Include/TargetPAL_BlockStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 2fc8052

Please sign in to comment.