From f5a0b8a5e24cede05e15ab696f0e15257a503525 Mon Sep 17 00:00:00 2001 From: Angelo Mantellini Date: Thu, 31 Jan 2019 18:20:48 +0100 Subject: [HICN24] Windows compatibility for hicn-light Change-Id: I8e19e52c9b4ec0fcbd7344c28765f5da1937569c Signed-off-by: Angelo Mantellini --- hicn-light/src/io/udpListener.c | 44 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) (limited to 'hicn-light/src/io/udpListener.c') diff --git a/hicn-light/src/io/udpListener.c b/hicn-light/src/io/udpListener.c index 31c0e673b..1a16d6161 100644 --- a/hicn-light/src/io/udpListener.c +++ b/hicn-light/src/io/udpListener.c @@ -13,14 +13,16 @@ * limitations under the License. */ +#ifndef _WIN32 #include +#include +#endif #include #include #include #include #include #include -#include #include @@ -83,13 +85,24 @@ ListenerOps *udpListener_CreateInet6(Forwarder *forwarder, parcAssertFalse(udp->udp_socket < 0, "Error opening UDP socket: (%d) %s", errno, strerror(errno)); + int failure = 0; +#ifndef _WIN32 // Set non-blocking flag int flags = fcntl(udp->udp_socket, F_GETFL, NULL); parcAssertTrue(flags != -1, "fcntl failed to obtain file descriptor flags (%d)", errno); - int failure = fcntl(udp->udp_socket, F_SETFL, flags | O_NONBLOCK); + failure = fcntl(udp->udp_socket, F_SETFL, flags | O_NONBLOCK); parcAssertFalse(failure, "fcntl failed to set file descriptor flags (%d)", errno); +#else + // Set non-blocking flag + u_long mode = 1; + int result = ioctlsocket(udp->udp_socket, FIONBIO, &mode); + if (result != NO_ERROR) { + parcAssertTrue(result != NO_ERROR, + "ioctlsocket failed to set file descriptor"); + } +#endif int one = 1; // don't hang onto address after listener has closed @@ -126,8 +139,11 @@ ListenerOps *udpListener_CreateInet6(Forwarder *forwarder, myerrno, strerror(myerrno)); parcMemory_Deallocate((void **)&str); } - +#ifndef _WIN32 close(udp->udp_socket); +#else + closesocket(udp->udp_socket); +#endif addressDestroy(&udp->localAddress); logger_Release(&udp->logger); parcMemory_Deallocate((void **)&udp); @@ -152,13 +168,23 @@ ListenerOps *udpListener_CreateInet(Forwarder *forwarder, parcAssertFalse(udp->udp_socket < 0, "Error opening UDP socket: (%d) %s", errno, strerror(errno)); + int failure = 0; +#ifndef _WIN32 // Set non-blocking flag int flags = fcntl(udp->udp_socket, F_GETFL, NULL); parcAssertTrue(flags != -1, "fcntl failed to obtain file descriptor flags (%d)", errno); - int failure = fcntl(udp->udp_socket, F_SETFL, flags | O_NONBLOCK); + failure = fcntl(udp->udp_socket, F_SETFL, flags | O_NONBLOCK); parcAssertFalse(failure, "fcntl failed to set file descriptor flags (%d)", errno); +#else + u_long mode = 1; + int result = ioctlsocket(udp->udp_socket, FIONBIO, &mode); + if (result != NO_ERROR) { + parcAssertTrue(result != NO_ERROR, + "ioctlsocket failed to set file descriptor"); + } +#endif int one = 1; // don't hang onto address after listener has closed @@ -217,7 +243,12 @@ static void udpListener_Destroy(UdpListener **listenerPtr) { "UdpListener %p destroyed", (void *)udp); } +#ifndef _WIN32 close(udp->udp_socket); +#else + closesocket(udp->udp_socket); +#endif + addressDestroy(&udp->localAddress); dispatcher_DestroyNetworkEvent(forwarder_GetDispatcher(udp->forwarder), &udp->udp_event); @@ -276,7 +307,8 @@ static size_t _peekMessageLength(UdpListener *udp, int fd, size_t packetLength = 0; - uint8_t fixedHeader[messageHandler_GetIPHeaderLength(IPv6)]; + uint8_t *fixedHeader = (uint8_t *)malloc( + sizeof(uint8_t) * messageHandler_GetIPHeaderLength(IPv6)); // peek at the UDP packet and read in the fixed header. // Also returns the socket information for the remote peer @@ -294,6 +326,8 @@ static size_t _peekMessageLength(UdpListener *udp, int fd, } } + free(fixedHeader); + return packetLength; } -- cgit 1.2.3-korg