From 602eef3e8fa87cecb9256b00a6a4a6445dea7639 Mon Sep 17 00:00:00 2001 From: Mario Kicherer Date: Thu, 11 Jul 2024 14:00:06 +0200 Subject: [PATCH] enable HAVE_STRUCT_CMSGHDR when building with mingw --- CMakeLists.txt | 2 +- CMakeLists.txt.in | 2 +- src/coap_io.c | 24 ++++++++++++++++++++---- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index aaa1f45d61..11d1a143e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -300,7 +300,7 @@ check_function_exists(random HAVE_RANDOM) check_function_exists(if_nametoindex HAVE_IF_NAMETOINDEX) # check for symbols -if(WIN32 AND NOT MINGW) +if(WIN32) set(HAVE_STRUCT_CMSGHDR 1) message(STATUS "setting HAVE_STRUCT_CMSGHDR") elseif(${CMAKE_SYSTEM_NAME} STREQUAL QNX) diff --git a/CMakeLists.txt.in b/CMakeLists.txt.in index 5b9a313baa..fbffe52c50 100644 --- a/CMakeLists.txt.in +++ b/CMakeLists.txt.in @@ -300,7 +300,7 @@ check_function_exists(random HAVE_RANDOM) check_function_exists(if_nametoindex HAVE_IF_NAMETOINDEX) # check for symbols -if(WIN32 AND NOT MINGW) +if(WIN32) set(HAVE_STRUCT_CMSGHDR 1) message(STATUS "setting HAVE_STRUCT_CMSGHDR") elseif(${CMAKE_SYSTEM_NAME} STREQUAL QNX) diff --git a/src/coap_io.c b/src/coap_io.c index 533af38f6c..b34df86d2a 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -86,6 +86,16 @@ coap_mfree_endpoint(coap_endpoint_t *ep) { } #endif /* COAP_SERVER_SUPPORT */ +#if defined(__MINGW32__) +#if(_WIN32_WINNT >= 0x0600) +#define CMSG_FIRSTHDR WSA_CMSG_FIRSTHDR +#define CMSG_NXTHDR WSA_CMSG_NXTHDR +#define CMSG_LEN WSA_CMSG_LEN +#define CMSG_SPACE WSA_CMSG_SPACE +#define cmsghdr _WSACMSGHDR +#endif /* (_WIN32_WINNT>=0x0600) */ +#endif /* defined(__MINGW32__) */ + #if !defined(WITH_CONTIKI) && !defined(WITH_LWIP) && !defined(RIOT_VERSION) #if COAP_SERVER_SUPPORT @@ -734,7 +744,9 @@ struct in_pktinfo { #if defined(_WIN32) #include -#if !defined(__MINGW32__) +#if defined(__MINGW32__) +static __thread LPFN_WSARECVMSG lpWSARecvMsg = NULL; +#else /* ! __MINGW32__ */ static __declspec(thread) LPFN_WSARECVMSG lpWSARecvMsg = NULL; #endif /* ! __MINGW32__ */ /* Map struct WSABUF fields to their posix counterpart */ @@ -783,7 +795,7 @@ coap_socket_send(coap_socket_t *sock, const coap_session_t *session, bytes_written = send(sock->fd, data, datalen, 0); #endif } else { -#if defined(_WIN32) && !defined(__MINGW32__) +#if defined(_WIN32) DWORD dwNumberOfBytesSent = 0; int r; #endif /* _WIN32 && !__MINGW32__ */ @@ -920,7 +932,7 @@ coap_socket_send(coap_socket_t *sock, const coap_session_t *session, } #endif /* HAVE_STRUCT_CMSGHDR */ -#if defined(_WIN32) && !defined(__MINGW32__) +#if defined(_WIN32) r = WSASendMsg(sock->fd, &mhdr, 0 /*dwFlags*/, &dwNumberOfBytesSent, NULL /*lpOverlapped*/, NULL /*lpCompletionRoutine*/); if (r == 0) @@ -1005,7 +1017,7 @@ coap_socket_recv(coap_socket_t *sock, coap_packet_t *packet) { packet->length = (size_t)len; } } else { -#if defined(_WIN32) && !defined(__MINGW32__) +#if defined(_WIN32) DWORD dwNumberOfBytesRecvd = 0; int r; #endif /* _WIN32 && !__MINGW32__ */ @@ -1016,7 +1028,11 @@ coap_socket_recv(coap_socket_t *sock, coap_packet_t *packet) { struct msghdr mhdr; struct iovec iov[1]; +#if defined(__MINGW32__) + iov[0].iov_base = (char *) packet->payload; +#else iov[0].iov_base = packet->payload; +#endif /* defined(__MINGW32__) */ iov[0].iov_len = (iov_len_t)COAP_RXBUFFER_SIZE; memset(&mhdr, 0, sizeof(struct msghdr));